|
@ -362,9 +362,15 @@ func (c *Compiler) Compile(mainPath string) error { |
|
|
|
|
|
|
|
|
// After all packages are imported, add a synthetic initializer function
|
|
|
// After all packages are imported, add a synthetic initializer function
|
|
|
// that calls the initializer of each package.
|
|
|
// that calls the initializer of each package.
|
|
|
initFn := c.mod.NamedFunction("runtime.initAll") |
|
|
initFn := c.ir.GetFunction(c.ir.Program.ImportedPackage("runtime").Members["initAll"].(*ssa.Function)) |
|
|
initFn.SetLinkage(llvm.InternalLinkage) |
|
|
initFn.LLVMFn.SetLinkage(llvm.InternalLinkage) |
|
|
block := c.ctx.AddBasicBlock(initFn, "entry") |
|
|
difunc, err := c.attachDebugInfo(initFn) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
pos := c.ir.Program.Fset.Position(initFn.Pos()) |
|
|
|
|
|
c.builder.SetCurrentDebugLocation(uint(pos.Line), uint(pos.Column), difunc, llvm.Metadata{}) |
|
|
|
|
|
block := c.ctx.AddBasicBlock(initFn.LLVMFn, "entry") |
|
|
c.builder.SetInsertPointAtEnd(block) |
|
|
c.builder.SetInsertPointAtEnd(block) |
|
|
for _, fn := range c.initFuncs { |
|
|
for _, fn := range c.initFuncs { |
|
|
c.builder.CreateCall(fn, nil, "") |
|
|
c.builder.CreateCall(fn, nil, "") |
|
@ -811,6 +817,17 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) (*Frame, error) { |
|
|
|
|
|
|
|
|
if c.Debug && f.Syntax() != nil && len(f.Blocks) != 0 { |
|
|
if c.Debug && f.Syntax() != nil && len(f.Blocks) != 0 { |
|
|
// Create debug info file if needed.
|
|
|
// Create debug info file if needed.
|
|
|
|
|
|
difunc, err := c.attachDebugInfo(f) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
frame.difunc = difunc |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return frame, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *Compiler) attachDebugInfo(f *ir.Function) (llvm.Metadata, error) { |
|
|
pos := c.ir.Program.Fset.Position(f.Syntax().Pos()) |
|
|
pos := c.ir.Program.Fset.Position(f.Syntax().Pos()) |
|
|
if _, ok := c.difiles[pos.Filename]; !ok { |
|
|
if _, ok := c.difiles[pos.Filename]; !ok { |
|
|
dir, file := filepath.Split(pos.Filename) |
|
|
dir, file := filepath.Split(pos.Filename) |
|
@ -822,7 +839,7 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) (*Frame, error) { |
|
|
for _, param := range f.Params { |
|
|
for _, param := range f.Params { |
|
|
ditype, err := c.getDIType(param.Type()) |
|
|
ditype, err := c.getDIType(param.Type()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return llvm.Metadata{}, err |
|
|
} |
|
|
} |
|
|
diparams = append(diparams, ditype) |
|
|
diparams = append(diparams, ditype) |
|
|
} |
|
|
} |
|
@ -831,7 +848,7 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) (*Frame, error) { |
|
|
Parameters: diparams, |
|
|
Parameters: diparams, |
|
|
Flags: 0, // ?
|
|
|
Flags: 0, // ?
|
|
|
}) |
|
|
}) |
|
|
frame.difunc = c.dibuilder.CreateFunction(c.difiles[pos.Filename], llvm.DIFunction{ |
|
|
difunc := c.dibuilder.CreateFunction(c.difiles[pos.Filename], llvm.DIFunction{ |
|
|
Name: f.RelString(nil), |
|
|
Name: f.RelString(nil), |
|
|
LinkageName: f.LinkName(), |
|
|
LinkageName: f.LinkName(), |
|
|
File: c.difiles[pos.Filename], |
|
|
File: c.difiles[pos.Filename], |
|
@ -843,10 +860,8 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) (*Frame, error) { |
|
|
Flags: llvm.FlagPrototyped, |
|
|
Flags: llvm.FlagPrototyped, |
|
|
Optimized: true, |
|
|
Optimized: true, |
|
|
}) |
|
|
}) |
|
|
frame.fn.LLVMFn.SetSubprogram(frame.difunc) |
|
|
f.LLVMFn.SetSubprogram(difunc) |
|
|
} |
|
|
return difunc, nil |
|
|
|
|
|
|
|
|
return frame, nil |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Create a new global hashmap bucket, for map initialization.
|
|
|
// Create a new global hashmap bucket, for map initialization.
|
|
|