Skip to content

Commit

Permalink
ssa: debug info of function/closure
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Sep 18, 2024
1 parent 68a2d86 commit 5f2a0fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 64 deletions.
21 changes: 13 additions & 8 deletions cl/_testdata/debug/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ type StructWithAllTypeFields struct {
m map[string]uint64
c chan int
err error
// fn func(string) (int, error)
fn func(string) (int, error)
pad1 int
pad2 int
}

type Interface interface {
Expand Down Expand Up @@ -84,7 +86,7 @@ func FuncWithAllTypeParams(
m map[string]uint64,
c chan int,
err error,
// fn func(string) (int, error),
fn func(string) (int, error),
) (int, error) {
println(
i8, i16, i32, i64, i, u8, u16, u32, u64, u,
Expand All @@ -96,7 +98,7 @@ func FuncWithAllTypeParams(
&f, pf, pi, intr, m,
c,
err,
// fn,
fn,
)
return 1, errors.New("Some error")
}
Expand Down Expand Up @@ -130,10 +132,13 @@ func main() {
m: map[string]uint64{"a": 31, "b": 32},
c: make(chan int),
err: errors.New("Test error"),
// fn: func(s string) (int, error) {
// println("fn:", s)
// return 1, errors.New("fn error")
// },
fn: func(s string) (int, error) {
println("fn:", s)
i = 201
return 1, errors.New("fn error")
},
pad1: 100,
pad2: 200,
}
println("s:", &s)
FuncWithAllTypeStructParam(s)
Expand All @@ -150,7 +155,7 @@ func main() {
s.m,
s.c,
s.err,
// s.fn,
s.fn,
)
println(i, err)
println("called function with types")
Expand Down
36 changes: 1 addition & 35 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
return
}

pos := p.goProg.Fset.Position(getPos(v))
pos := p.goProg.Fset.Position(v.Pos())
value := p.compileValue(b, v.X)
fn := v.Parent()
dbgVar := p.getLocalVariable(b, fn, variable)
Expand All @@ -754,40 +754,6 @@ type poser interface {
Pos() token.Pos
}

func getPos(v poser) token.Pos {
pos := v.Pos()
if pos.IsValid() {
return pos
}
panic(fmt.Errorf("getPos: invalid position - %T", v))

// switch v := v.(type) {
// case *ssa.MakeInterface:
// return getPos(v.X)
// case *ssa.MakeClosure:
// return v.Fn.(*ssa.Function).Pos()
// case *ssa.Return:
// syntax := v.Parent().Syntax()
// if syntax != nil {
// return syntax.End()
// }
// return token.NoPos
// case *ssa.FieldAddr:
// return getPos(v.X)
// case *ssa.IndexAddr:
// return getPos(v.X)
// case *ssa.Slice:
// return getPos(v.X)
// case *ssa.Store:
// return getPos(v.Addr)
// case *ssa.Extract:
// return getPos(v.Tuple)
// default:
// fmt.Printf("getPos: unknown instr - %T\n", v)
// return token.NoPos
// }
}

func (p *context) getLocalVariable(b llssa.Builder, fn *ssa.Function, v *types.Var) llssa.DIVar {
pos := p.fset.Position(v.Pos())
t := b.Prog.Type(v.Type(), llssa.InGo)
Expand Down
28 changes: 7 additions & 21 deletions ssa/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (b diBuilder) createType(ty Type, pos token.Position) DIType {
case *types.Struct:
return b.createStructType(ty, pos)
case *types.Signature:
return b.createFuncPtrType(b.prog.rawType(t), pos)
tyFn := b.prog.Closure(ty)
return b.createFuncPtrType(tyFn, pos)
case *types.Tuple:
return b.createBasicType(ty)

Check warning on line 158 in ssa/di.go

View check run for this annotation

Codecov / codecov/patch

ssa/di.go#L157-L158

Added lines #L157 - L158 were not covered by tests
case *types.Array:
Expand Down Expand Up @@ -366,8 +367,6 @@ func (b diBuilder) createMapType(tyMap, tk, tv Type) DIType {
}

func (b diBuilder) createChanType(t Type) DIType {
tyElem := b.prog.rawType(t.RawType().(*types.Chan).Elem())
fmt.Printf("tyElem: %v, %T\n", tyElem, tyElem)
return &aDIType{ll: b.di.CreateStructType(
llvm.Metadata{},
llvm.DIStructType{
Expand Down Expand Up @@ -452,18 +451,11 @@ func (b diBuilder) createStructType(ty Type, pos token.Position) (ret DIType) {
}

func (b diBuilder) createFuncPtrType(ty Type, pos token.Position) DIType {
sig := ty.RawType().(*types.Signature)
retTy := b.diType(b.prog.rawType(sig.Results()), pos)
paramTys := make([]DIType, sig.Params().Len())
for i := 0; i < sig.Params().Len(); i++ {
paramTys[i] = b.diType(b.prog.rawType(sig.Params().At(i).Type()), pos)
}
rt := b.createSubroutineType(b.file(pos.Filename), retTy, paramTys)
ptr := b.prog.VoidPtr()
return &aDIType{ll: b.di.CreatePointerType(llvm.DIPointerType{
Pointee: rt.ll,
SizeInBits: b.prog.SizeOf(ty) * 8,
AlignInBits: 8,
AddressSpace: 0,
Pointee: b.diType(ptr, pos).ll,
SizeInBits: b.prog.SizeOf(ptr) * 8,
AlignInBits: uint32(b.prog.sizes.Alignof(ptr.RawType()) * 8),
})}
}

Expand Down Expand Up @@ -583,16 +575,14 @@ func (b Builder) debug(v Expr) (dbgPtr Expr, dbgVal Expr, deref bool) {
case *types.Slice:
ty = b.Prog.Type(b.Prog.rtType("Slice").RawType().Underlying(), InGo)
case *types.Signature:
fmt.Printf("t: %T, %v\n", t, t)
ty = b.Prog.Type(b.Prog.rtType("Func").RawType().Underlying(), InGo)
ty = b.Prog.Closure(b.Prog.rawType(t))
case *types.Named:
ty = b.Prog.Type(t.Underlying(), InGo)

Check warning on line 580 in ssa/di.go

View check run for this annotation

Codecov / codecov/patch

ssa/di.go#L577-L580

Added lines #L577 - L580 were not covered by tests
case *types.Map:
ty = b.Prog.Type(b.Prog.rtType("Map").RawType().Underlying(), InGo)
default:
ty = v.Type
}
// fmt.Printf("ty: %T, %v, %T, %v\n", ty.RawType(), ty.RawType(), t, t)
dbgPtr = b.AllocaT(ty)
dbgPtr.Type = b.Prog.Pointer(v.Type)
b.Store(dbgPtr, v)
Expand All @@ -606,10 +596,6 @@ const (
)

func skipType(t types.Type) bool {
switch t.(type) {
case *types.Signature:
return true
}
return false
}

Expand Down

0 comments on commit 5f2a0fb

Please sign in to comment.