From 61e6f7cf5bffb549ef3c84a252afae78ed42f7ed Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 11 Sep 2018 17:00:24 +0200 Subject: [PATCH] compiler: add debug information for function parameters Add 'optimized-out' parameters to functions. This might make debugging slightly easier by showing parameter names. Sadly I din't get setting variables to work. It might be a bug in CreateExpression. --- compiler.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/compiler.go b/compiler.go index e215e75c..d90fe789 100644 --- a/compiler.go +++ b/compiler.go @@ -1182,11 +1182,30 @@ func (c *Compiler) parseFunc(frame *Frame) error { frame.cleanupBlock = c.ctx.AddBasicBlock(frame.fn.llvmFn, "task.cleanup") frame.suspendBlock = c.ctx.AddBasicBlock(frame.fn.llvmFn, "task.suspend") } + entryBlock := frame.blocks[frame.fn.fn.Blocks[0]] // Load function parameters - for _, param := range frame.fn.fn.Params { + for i, param := range frame.fn.fn.Params { llvmParam := frame.fn.llvmFn.Param(frame.params[param]) frame.locals[param] = llvmParam + + // Add debug information to this parameter (if available) + if c.debug && frame.fn.fn.Syntax() != nil { + pos := c.ir.program.Fset.Position(frame.fn.fn.Syntax().Pos()) + dityp, err := c.getDIType(param.Type()) + if err != nil { + return err + } + c.dibuilder.CreateParameterVariable(frame.difunc, llvm.DIParameterVariable{ + Name: param.Name(), + File: c.difiles[pos.Filename], + Line: pos.Line, + Type: dityp, + AlwaysPreserve: true, + ArgNo: i + 1, + }) + // TODO: set the value of this parameter. + } } // Load free variables from the context. This is a closure (or bound @@ -1195,7 +1214,7 @@ func (c *Compiler) parseFunc(frame *Frame) error { if !c.ir.FunctionNeedsContext(frame.fn) { panic("free variables on function without context") } - c.builder.SetInsertPointAtEnd(frame.blocks[frame.fn.fn.Blocks[0]]) + c.builder.SetInsertPointAtEnd(entryBlock) context := frame.fn.llvmFn.Param(len(frame.fn.fn.Params)) // Determine the context type. It's a struct containing all variables. @@ -1237,7 +1256,7 @@ func (c *Compiler) parseFunc(frame *Frame) error { } } - c.builder.SetInsertPointAtEnd(frame.blocks[frame.fn.fn.Blocks[0]]) + c.builder.SetInsertPointAtEnd(entryBlock) if frame.fn.fn.Recover != nil { // Create defer list pointer.