Skip to content
Merged
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 @@ -652,6 +652,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 @@ -597,20 +597,6 @@ func (b *builder) createUnwindReturnOrUnreachable() {
}
}

// 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 @@ -625,34 +611,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 @@ -679,8 +667,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
60 changes: 37 additions & 23 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type compilerContext struct {
directCatchers map[llvm.Value]llvm.Value
indirectCatchers map[llvm.Type]llvm.Value
asyncifyReplays map[llvm.Type]llvm.Value
functionABIs map[functionABIKey]functionABI
astComments map[string]*ast.CommentGroup
embedGlobals map[string][]*loader.EmbedFile
pkg *types.Package
Expand All @@ -118,6 +119,7 @@ func newCompilerContext(moduleName string, machine llvm.TargetMachine, config *C
directCatchers: map[llvm.Value]llvm.Value{},
indirectCatchers: map[llvm.Type]llvm.Value{},
asyncifyReplays: map[llvm.Type]llvm.Value{},
functionABIs: map[functionABIKey]functionABI{},
astComments: map[string]*ast.CommentGroup{},
}

Expand Down Expand Up @@ -805,18 +807,26 @@ func (b *builder) getLocalVariable(variable *types.Var) llvm.Metadata {
return dilocal
}

// attachDebugInfo adds debug info to a function declaration. It returns the
// attachDebugInfo adds debug info to a function. It returns the
// DISubprogram metadata node.
func (c *compilerContext) attachDebugInfo(f *ssa.Function) llvm.Metadata {
pos := c.program.Fset.Position(f.Syntax().Pos())
_, fn := c.getFunction(f)
return c.attachDebugInfoRaw(f, fn, "", pos.Filename, pos.Line)
}

// attachDebugInfo adds debug info to a function declaration. It returns the
// attachDebugInfoRaw adds debug info to a function. It returns the
// DISubprogram metadata node. This method allows some more control over how
// debug info is added to the function.
func (c *compilerContext) attachDebugInfoRaw(f *ssa.Function, llvmFn llvm.Value, suffix, filename string, line int) llvm.Metadata {
return c.attachDebugInfoRawWithDefinition(f, llvmFn, suffix, filename, line, true)
}

func (c *compilerContext) attachDebugInfoDeclarationRaw(f *ssa.Function, llvmFn llvm.Value, suffix, filename string, line int) llvm.Metadata {
return c.attachDebugInfoRawWithDefinition(f, llvmFn, suffix, filename, line, false)
}

func (c *compilerContext) attachDebugInfoRawWithDefinition(f *ssa.Function, llvmFn llvm.Value, suffix, filename string, line int, isDefinition bool) llvm.Metadata {
// Debug info for this function.
params := getParams(f.Signature)
diparams := make([]llvm.Metadata, 0, len(params))
Expand All @@ -835,7 +845,7 @@ func (c *compilerContext) attachDebugInfoRaw(f *ssa.Function, llvmFn llvm.Value,
Line: line,
Type: diFuncType,
LocalToUnit: true,
IsDefinition: true,
IsDefinition: isDefinition,
ScopeLine: 0,
Flags: llvm.FlagPrototyped,
Optimized: true,
Expand Down Expand Up @@ -1300,27 +1310,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 @@ -1459,14 +1465,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 @@ -1697,9 +1709,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 @@ -2344,18 +2355,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, "", instr)
Expand Down Expand Up @@ -2736,7 +2750,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