11package compiler
22
33import (
4- "fmt"
5- "golang.org/x/tools/go/ssa"
64 "tinygo.org/x/go-llvm"
75)
86
@@ -13,24 +11,6 @@ import (
1311// a struct contains more fields, it is passed as a struct without expanding.
1412const MaxFieldsPerParam = 3
1513
16- // Shortcut: create a call to runtime.<fnName> with the given arguments.
17- func (c * Compiler ) createRuntimeCall (fnName string , args []llvm.Value , name string ) llvm.Value {
18- runtimePkg := c .ir .Program .ImportedPackage ("runtime" )
19- member := runtimePkg .Members [fnName ]
20- if member == nil {
21- panic ("trying to call runtime." + fnName )
22- }
23- fn := c .ir .GetFunction (member .(* ssa.Function ))
24- if fn .LLVMFn .IsNil () {
25- panic (fmt .Errorf ("function %s does not appear in LLVM IR" , fnName ))
26- }
27- if ! fn .IsExported () {
28- args = append (args , llvm .Undef (c .i8ptrType )) // unused context parameter
29- args = append (args , llvm .ConstPointerNull (c .i8ptrType )) // coroutine handle
30- }
31- return c .createCall (fn .LLVMFn , args , name )
32- }
33-
3414// createCall creates a new call to runtime.<fnName> with the given arguments.
3515func (b * builder ) createRuntimeCall (fnName string , args []llvm.Value , name string ) llvm.Value {
3616 fullName := "runtime." + fnName
@@ -43,16 +23,6 @@ func (b *builder) createRuntimeCall(fnName string, args []llvm.Value, name strin
4323 return b .createCall (fn , args , name )
4424}
4525
46- // Create a call to the given function with the arguments possibly expanded.
47- func (c * Compiler ) createCall (fn llvm.Value , args []llvm.Value , name string ) llvm.Value {
48- expanded := make ([]llvm.Value , 0 , len (args ))
49- for _ , arg := range args {
50- fragments := c .expandFormalParam (arg )
51- expanded = append (expanded , fragments ... )
52- }
53- return c .builder .CreateCall (fn , expanded , name )
54- }
55-
5626// createCall creates a call to the given function with the arguments possibly
5727// expanded.
5828func (b * builder ) createCall (fn llvm.Value , args []llvm.Value , name string ) llvm.Value {
@@ -102,27 +72,6 @@ func (b *builder) expandFormalParamOffsets(t llvm.Type) []uint64 {
10272 }
10373}
10474
105- // Equivalent of expandFormalParamType for parameter values.
106- func (c * Compiler ) expandFormalParam (v llvm.Value ) []llvm.Value {
107- switch v .Type ().TypeKind () {
108- case llvm .StructTypeKind :
109- fieldTypes := flattenAggregateType (v .Type ())
110- if len (fieldTypes ) <= MaxFieldsPerParam {
111- fields := c .flattenAggregate (v )
112- if len (fields ) != len (fieldTypes ) {
113- panic ("type and value param lowering don't match" )
114- }
115- return fields
116- } else {
117- // failed to lower
118- return []llvm.Value {v }
119- }
120- default :
121- // TODO: split small arrays
122- return []llvm.Value {v }
123- }
124- }
125-
12675// expandFormalParam splits a formal param value into pieces, so it can be
12776// passed directly as part of a function call. For example, it splits up small
12877// structs into individual fields. It is the equivalent of expandFormalParamType
@@ -187,23 +136,6 @@ func (c *compilerContext) flattenAggregateTypeOffsets(t llvm.Type) []uint64 {
187136 }
188137}
189138
190- // Break down a struct into its elementary types for argument passing. The value
191- // equivalent of flattenAggregateType
192- func (c * Compiler ) flattenAggregate (v llvm.Value ) []llvm.Value {
193- switch v .Type ().TypeKind () {
194- case llvm .StructTypeKind :
195- fields := make ([]llvm.Value , 0 , v .Type ().StructElementTypesCount ())
196- for i := range v .Type ().StructElementTypes () {
197- subfield := c .builder .CreateExtractValue (v , i , "" )
198- subfields := c .flattenAggregate (subfield )
199- fields = append (fields , subfields ... )
200- }
201- return fields
202- default :
203- return []llvm.Value {v }
204- }
205- }
206-
207139// flattenAggregate breaks down a struct into its elementary values for argument
208140// passing. It is the value equivalent of flattenAggregateType
209141func (b * builder ) flattenAggregate (v llvm.Value ) []llvm.Value {
0 commit comments