@@ -62,10 +62,6 @@ type Compiler struct {
6262 initFuncs []llvm.Value
6363}
6464
65- type Frame struct {
66- builder
67- }
68-
6965// builder contains all information relevant to build a single function.
7066type builder struct {
7167 * compilerContext
@@ -263,8 +259,6 @@ func (c *Compiler) Compile(mainPath string) []error {
263259 })
264260 }
265261
266- var frames []* Frame
267-
268262 c .loadASTComments (lprogram )
269263
270264 // Declare runtime types.
@@ -283,21 +277,32 @@ func (c *Compiler) Compile(mainPath string) []error {
283277
284278 // Declare all functions.
285279 for _ , f := range c .ir .Functions {
286- frames = append ( frames , c . parseFuncDecl ( f ) )
280+ c . createFunctionDeclaration ( f )
287281 }
288282
289283 // Add definitions to declarations.
290- for _ , frame := range frames {
291- if frame . fn .Synthetic == "package initializer" {
292- c .initFuncs = append (c .initFuncs , frame . fn .LLVMFn )
284+ for _ , f := range c . ir . Functions {
285+ if f .Synthetic == "package initializer" {
286+ c .initFuncs = append (c .initFuncs , f .LLVMFn )
293287 }
294- if frame . fn .CName () != "" {
288+ if f .CName () != "" {
295289 continue
296290 }
297- if frame . fn .Blocks == nil {
291+ if f .Blocks == nil {
298292 continue // external function
299293 }
300- frame .createFunctionDefinition ()
294+
295+ // Create the function definition.
296+ b := builder {
297+ compilerContext : & c .compilerContext ,
298+ Builder : c .builder ,
299+ fn : f ,
300+ locals : make (map [ssa.Value ]llvm.Value ),
301+ dilocals : make (map [* types.Var ]llvm.Metadata ),
302+ blockEntries : make (map [* ssa.BasicBlock ]llvm.BasicBlock ),
303+ blockExits : make (map [* ssa.BasicBlock ]llvm.BasicBlock ),
304+ }
305+ b .createFunctionDefinition ()
301306 }
302307
303308 // After all packages are imported, add a synthetic initializer function
@@ -726,19 +731,9 @@ func (b *builder) getLocalVariable(variable *types.Var) llvm.Metadata {
726731 return dilocal
727732}
728733
729- func (c * Compiler ) parseFuncDecl (f * ir.Function ) * Frame {
730- frame := & Frame {
731- builder : builder {
732- compilerContext : & c .compilerContext ,
733- Builder : c .builder , // TODO: use a separate builder per function
734- fn : f ,
735- locals : make (map [ssa.Value ]llvm.Value ),
736- dilocals : make (map [* types.Var ]llvm.Metadata ),
737- blockEntries : make (map [* ssa.BasicBlock ]llvm.BasicBlock ),
738- blockExits : make (map [* ssa.BasicBlock ]llvm.BasicBlock ),
739- },
740- }
741-
734+ // createFunctionDeclaration creates a LLVM function declaration without body.
735+ // It can later be filled with frame.createFunctionDefinition().
736+ func (c * compilerContext ) createFunctionDeclaration (f * ir.Function ) {
742737 var retType llvm.Type
743738 if f .Signature .Results () == nil {
744739 retType = c .ctx .VoidType ()
@@ -769,9 +764,9 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) *Frame {
769764 fnType := llvm .FunctionType (retType , paramTypes , false )
770765
771766 name := f .LinkName ()
772- frame . fn .LLVMFn = c .mod .NamedFunction (name )
773- if frame . fn .LLVMFn .IsNil () {
774- frame . fn .LLVMFn = llvm .AddFunction (c .mod , name , fnType )
767+ f .LLVMFn = c .mod .NamedFunction (name )
768+ if f .LLVMFn .IsNil () {
769+ f .LLVMFn = llvm .AddFunction (c .mod , name , fnType )
775770 }
776771
777772 // External/exported functions may not retain pointer values.
@@ -780,18 +775,16 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) *Frame {
780775 // Set the wasm-import-module attribute if the function's module is set.
781776 if f .Module () != "" {
782777 wasmImportModuleAttr := c .ctx .CreateStringAttribute ("wasm-import-module" , f .Module ())
783- frame . fn .LLVMFn .AddFunctionAttr (wasmImportModuleAttr )
778+ f .LLVMFn .AddFunctionAttr (wasmImportModuleAttr )
784779 }
785780 nocaptureKind := llvm .AttributeKindID ("nocapture" )
786781 nocapture := c .ctx .CreateEnumAttribute (nocaptureKind , 0 )
787782 for i , typ := range paramTypes {
788783 if typ .TypeKind () == llvm .PointerTypeKind {
789- frame . fn .LLVMFn .AddAttributeAtIndex (i + 1 , nocapture )
784+ f .LLVMFn .AddAttributeAtIndex (i + 1 , nocapture )
790785 }
791786 }
792787 }
793-
794- return frame
795788}
796789
797790// attachDebugInfo adds debug info to a function declaration. It returns the
0 commit comments