Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
if err != nil {
return err
}
if strings.HasPrefix(config.Triple(), "wasm") {
if err := compiler.ValidateWasmFunctionParameters(mod); err != nil {
Comment thread
jakebailey marked this conversation as resolved.
return err
}
}

// Make sure stack sizes are loaded from a separate section so they can be
// modified after linking.
Expand Down
50 changes: 19 additions & 31 deletions compiler/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,6 @@ func (b *builder) createInvoke(fnType llvm.Type, fn llvm.Value, args []llvm.Valu
return b.createCall(fnType, fn, args, name)
}

// Expand an argument type to a list that can be used in a function call
// parameter list.
func (c *compilerContext) expandFormalParamType(t llvm.Type, name string, goType types.Type) []paramInfo {
if c.isIndirectAggregate(t) {
return []paramInfo{{
llvmType: c.dataPtrType,
name: name,
elemSize: c.targetData.TypeAllocSize(t),
flags: paramIsGoParam | paramIsReadonly | paramIsIndirect,
}}
}
return c.expandDirectFormalParamType(t, name, goType)
}

func (c *compilerContext) expandDirectFormalParamType(t llvm.Type, name string, goType types.Type) []paramInfo {
switch t.TypeKind() {
case llvm.StructTypeKind:
Expand All @@ -130,34 +116,36 @@ func (c *compilerContext) expandDirectFormalParamType(t llvm.Type, name string,
return []paramInfo{c.getParamInfo(t, name, goType)}
}

func (c *compilerContext) storedParamType(t llvm.Type, exported bool) llvm.Type {
if c.isIndirectParam(t, exported) {
func (c *compilerContext) storedParamType(t llvm.Type) llvm.Type {
if c.isIndirectAggregate(t) {
return c.dataPtrType
}
return t
}

func (c *compilerContext) isIndirectParam(t llvm.Type, exported bool) bool {
return !exported && c.isIndirectAggregate(t)
}

func (b *builder) appendStoredValueTypes(valueTypes []llvm.Type, values []ssa.Value, exported bool) []llvm.Type {
for _, value := range values {
valueTypes = append(valueTypes, b.storedParamType(b.getLLVMType(value.Type()), exported))
func (b *builder) appendStoredParamTypes(valueTypes []llvm.Type, params []functionABIParam) []llvm.Type {
for _, param := range params {
if param.indirect {
valueTypes = append(valueTypes, b.dataPtrType)
} else {
valueTypes = append(valueTypes, param.llvmType)
}
}
return valueTypes
}

func (b *builder) appendStoredParamTypes(valueTypes []llvm.Type, params []*types.Var, exported bool) []llvm.Type {
for _, param := range params {
valueTypes = append(valueTypes, b.storedParamType(b.getLLVMType(param.Type()), exported))
func (b *builder) getCallArguments(values []ssa.Value, params []functionABIParam) []llvm.Value {
args := make([]llvm.Value, len(values))
for i, value := range values {
args[i] = b.getCallArgument(value, params[i].indirect)
}
return valueTypes
return args
}

func (b *builder) prependIndirectResult(sig *types.Signature, exported bool, params []llvm.Value, name string) []llvm.Value {
if resultType, indirect := b.hasIndirectResult(sig); !exported && indirect {
return append([]llvm.Value{b.createIndirectStorage(resultType, name)}, params...)
abi := b.getFunctionABI(sig, exported)
if abi.indirectResult {
return append([]llvm.Value{b.createIndirectStorage(abi.resultType, name)}, params...)
}
return params
}
Expand All @@ -184,8 +172,8 @@ func (b *builder) expandFormalParamOffsets(t llvm.Type) []uint64 {

// expandFormalParam splits a formal param value into pieces, so it can be
// passed directly as part of a function call. For example, it splits up small
// structs into individual fields. It is the equivalent of expandFormalParamType
// for parameter values.
// structs into individual fields. It is the equivalent of
// expandDirectFormalParamType for parameter values.
func (b *builder) expandFormalParam(v llvm.Value) []llvm.Value {
switch v.Type().TypeKind() {
case llvm.StructTypeKind:
Expand Down
46 changes: 26 additions & 20 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type compilerContext struct {
program *ssa.Program
diagnostics []error
functionInfos map[*ssa.Function]functionInfo
functionABIs map[functionABIKey]functionABI
astComments map[string]*ast.CommentGroup
embedGlobals map[string][]*loader.EmbedFile
pkg *types.Package
Expand All @@ -107,6 +108,7 @@ func newCompilerContext(moduleName string, machine llvm.TargetMachine, config *C
machine: machine,
targetData: machine.CreateTargetData(),
functionInfos: map[*ssa.Function]functionInfo{},
functionABIs: map[functionABIKey]functionABI{},
astComments: map[string]*ast.CommentGroup{},
}

Expand Down Expand Up @@ -1284,27 +1286,23 @@ func (b *builder) createFunctionStart(intrinsic bool) {
}

// Load function parameters
abi := b.getFunctionABI(b.fn.Signature, b.info.exported)
llvmParamIndex := 0
if _, indirectResult := b.hasIndirectResult(b.fn.Signature); indirectResult && !b.info.exported {
if abi.indirectResult {
b.indirectReturn = b.llvmFn.Param(llvmParamIndex)
b.indirectReturn.SetName("return")
llvmParamIndex++
}
for _, param := range b.fn.Params {
llvmType := b.getLLVMType(param.Type())
if b.isIndirectParam(llvmType, b.info.exported) {
for i, param := range b.fn.Params {
llvmType := abi.params[i].llvmType
if abi.params[i].indirect {
llvmParam := b.llvmFn.Param(llvmParamIndex)
llvmParam.SetName(param.Name())
b.indirectValues[param] = llvmParam
llvmParamIndex++
continue
}
var paramInfos []paramInfo
if b.info.exported {
paramInfos = b.expandDirectFormalParamType(llvmType, param.Name(), param.Type())
} else {
paramInfos = b.expandFormalParamType(llvmType, param.Name(), param.Type())
}
paramInfos := b.expandDirectFormalParamType(llvmType, param.Name(), param.Type())
fields := make([]llvm.Value, 0, 1)
for _, info := range paramInfos {
param := b.llvmFn.Param(llvmParamIndex)
Expand Down Expand Up @@ -1441,14 +1439,20 @@ func (b *builder) createFunction() {
}

// Resolve phi nodes
phiBuilder := b.ctx.NewBuilder()
originalBuilder := b.Builder
b.Builder = phiBuilder
for _, phi := range b.phis {
block := phi.ssa.Block()
for i, edge := range phi.ssa.Edges {
llvmVal := b.getCallArgument(edge, false)
llvmBlock := b.blockInfo[block.Preds[i].Index].exit
b.SetInsertPointBefore(llvmBlock.LastInstruction())
llvmVal := b.getCallArgument(edge, b.isIndirectAggregate(b.getLLVMType(edge.Type())))
phi.llvm.AddIncoming([]llvm.Value{llvmVal}, []llvm.BasicBlock{llvmBlock})
}
}
b.Builder = originalBuilder
phiBuilder.Dispose()

if b.NeedsStackObjects {
// Track phi nodes.
Expand Down Expand Up @@ -1676,9 +1680,8 @@ func (b *builder) getValuePointer(value ssa.Value) llvm.Value {
return ptr
}

func (b *builder) getCallArgument(value ssa.Value, exported bool) llvm.Value {
paramType := b.getLLVMType(value.Type())
if b.isIndirectParam(paramType, exported) {
func (b *builder) getCallArgument(value ssa.Value, indirect bool) llvm.Value {
if indirect {
return b.getValuePointer(value)
}
return b.getValue(value, getPos(value))
Expand Down Expand Up @@ -2323,18 +2326,21 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
b.createNilCheck(instr.Value, callee, "fpcall")
}

var params []llvm.Value
for _, param := range instr.Args {
params = append(params, b.getCallArgument(param, exported))
abi := b.getFunctionABI(instr.Signature(), exported)
paramOffset := 0
if instr.IsInvoke() {
abi = b.getInterfaceFunctionABI(instr.Signature())
paramOffset = 1
}
params := b.getCallArguments(instr.Args, abi.params[paramOffset:])
if instr.IsInvoke() {
params = append([]llvm.Value{invokeReceiver}, params...)
params = append(params, invokeTypecode)
}

if !exported {
if resultType, indirectResult := b.hasIndirectResult(instr.Signature()); indirectResult {
result := b.createIndirectStorage(resultType, "call.result")
if abi.indirectResult {
result := b.createIndirectStorage(abi.resultType, "call.result")
params = append([]llvm.Value{result}, params...)
params = append(params, context)
b.createInvoke(calleeType, callee, params, "")
Expand Down Expand Up @@ -2715,7 +2721,7 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) {
return b.createMapIteratorNext(rangeVal, llvmRangeVal, it), nil
}
case *ssa.Phi:
phiType := b.storedParamType(b.getLLVMType(expr.Type()), false)
phiType := b.storedParamType(b.getLLVMType(expr.Type()))
phi := b.CreatePHI(phiType, "")
b.phis = append(b.phis, phiNode{expr, phi})
return phi, nil
Expand Down
Loading
Loading