From c6bb4a23ae5f6508e88010d5ed3eaa232990da39 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 22 Sep 2024 17:43:31 +0800 Subject: [PATCH] ssa: add debug info of function return type --- ssa/decl.go | 7 +++++-- ssa/di.go | 28 ++++++++++++++++++++++++---- ssa/type_cvt.go | 2 ++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ssa/decl.go b/ssa/decl.go index 18b194b6..91bb19a7 100644 --- a/ssa/decl.go +++ b/ssa/decl.go @@ -329,9 +329,12 @@ func (p Function) SetRecover(blk BasicBlock) { func (p Function) scopeMeta(b diBuilder, pos token.Position) DIScopeMeta { if p.diFunc == nil { - paramTypes := make([]llvm.Metadata, len(p.params)) + sig := p.Type.raw.Type.(*types.Signature) + rt := p.Prog.Type(sig.Results(), InGo) + paramTypes := make([]llvm.Metadata, len(p.params)+1) + paramTypes[0] = b.diType(rt, pos).ll for i, t := range p.params { - paramTypes[i] = b.diType(t, pos).ll + paramTypes[i+1] = b.diType(t, pos).ll } diFuncType := b.di.CreateSubroutineType(llvm.DISubroutineType{ File: b.file(pos.Filename).ll, diff --git a/ssa/di.go b/ssa/di.go index ccb0dd54..1e09801d 100644 --- a/ssa/di.go +++ b/ssa/di.go @@ -128,7 +128,6 @@ func (b diBuilder) createType(name string, ty Type, pos token.Position) DIType { Name: name, SizeInBits: b.prog.SizeOf(b.prog.rawType(t)) * 8, AlignInBits: uint32(b.prog.sizes.Alignof(t) * 8), - AddressSpace: 0, }) return &aDIType{typ} } @@ -180,6 +179,8 @@ func (b diBuilder) createType(name string, ty Type, pos token.Position) DIType { case *types.Map: ty := b.prog.rtType("Map") return b.createMapType(name, ty, pos) + case *types.Tuple: + return b.createTupleType(name, ty, pos) default: panic(fmt.Errorf("can't create debug info of type: %v, %T", ty.RawType(), ty.RawType())) } @@ -365,9 +366,8 @@ func (b diBuilder) createPointerType(name string, ty Type, pos token.Position) D return &aDIType{ll: b.di.CreatePointerType(llvm.DIPointerType{ Name: name, Pointee: b.diType(ty, pos).ll, - SizeInBits: b.prog.SizeOf(ty) * 8, - AlignInBits: uint32(b.prog.sizes.Alignof(ty.RawType())) * 8, - AddressSpace: 0, + SizeInBits: b.prog.SizeOf(b.prog.VoidPtr()) * 8, + AlignInBits: uint32(b.prog.sizes.Alignof(b.prog.VoidPtr().RawType())) * 8, })} } @@ -421,6 +421,26 @@ func (b diBuilder) createStructType(name string, ty Type, pos token.Position) (r }) } +func (b diBuilder) createTupleType(name string, ty Type, pos token.Position) DIType { + tupleType := ty.RawType().(*types.Tuple) + if tupleType.Len() == 0 { + return &aDIType{} + } + if tupleType.Len() == 1 { + t := b.prog.rawType(tupleType.At(0).Type()) + return b.diType(t, pos) + } + return b.doCreateStructType(name, ty, pos, func(ditStruct DIType) []llvm.Metadata { + fields := make([]llvm.Metadata, ty.RawType().(*types.Tuple).Len()) + for i := 0; i < ty.RawType().(*types.Tuple).Len(); i++ { + field := ty.RawType().(*types.Tuple).At(i) + tyField := b.prog.rawType(field.Type()) + fields[i] = b.createMemberTypeEx(field.Name(), ty, tyField, i, pos, 0) + } + return fields + }) +} + func (b diBuilder) createFuncPtrType(name string, ty Type, pos token.Position) DIType { ptr := b.prog.VoidPtr() return &aDIType{ll: b.di.CreatePointerType(llvm.DIPointerType{ diff --git a/ssa/type_cvt.go b/ssa/type_cvt.go index b696cfeb..5abc75c2 100644 --- a/ssa/type_cvt.go +++ b/ssa/type_cvt.go @@ -109,6 +109,8 @@ func (p goTypes) cvtType(typ types.Type) (raw types.Type, cvt bool) { if elem, cvt := p.cvtType(t.Elem()); cvt { return types.NewChan(t.Dir(), elem), true } + case *types.Tuple: + return p.cvtTuple(t) default: panic(fmt.Sprintf("cvtType: unexpected type - %T", typ)) }