Skip to content

Commit

Permalink
CORE: Improve debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 27, 2024
1 parent f150c85 commit fb84e9a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions core/codegen/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codegen

import (
"fireball/core/ast"
"fireball/core/cst"
"fireball/core/ir"
"fireball/core/scanner"
)
Expand Down Expand Up @@ -40,9 +41,9 @@ func (c *codegen) VisitFunc(decl *ast.Func) {
// Add this variable
if struct_ := decl.Method(); struct_ != nil {
name := scanner.Token{Kind: scanner.Identifier, Lexeme: "this"}
type_ := ast.Pointer{Pointee: struct_}
node := cst.Node{Kind: cst.IdentifierNode, Token: name, Range: decl.Name.Cst().Range}

c.scopes.addVariable(&ast.Token{Token_: name}, &type_, exprValue{v: function.Typ.Params[0]}, 1)
c.scopes.addVariable(ast.NewToken(node, name), struct_, exprValue{v: function.Typ.Params[0]}, 1)
}

// Copy parameters
Expand Down
10 changes: 5 additions & 5 deletions core/codegen/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func (c *codegen) VisitCall(expr *ast.Call) {

for i, arg := range expr.Args {
if hasThis {
args[i+1] = c.loadExpr(arg).v
args[i+1] = c.implicitCastLoadExpr(function.Params[i].Type, arg).v
} else if i >= len(function.Params) {
args[i] = c.loadExpr(arg).v
} else {
Expand All @@ -657,7 +657,7 @@ func (c *codegen) VisitCall(expr *ast.Call) {
Args: args,
})

c.setLocationMeta(result, expr)
c.setLocationMetaCst(result, expr, scanner.LeftParen)
c.exprResult = exprValue{v: result}

// If the function returns a constant-sized array and the array is immediately indexed then store it in an alloca first
Expand Down Expand Up @@ -696,7 +696,7 @@ func (c *codegen) VisitIndex(expr *ast.Index) {
Inbounds: true,
})

c.setLocationMeta(result, expr)
c.setLocationMetaCst(result, expr, scanner.LeftBracket)

c.exprResult = exprValue{
v: result,
Expand Down Expand Up @@ -739,7 +739,7 @@ func (c *codegen) VisitMember(expr *ast.Member) {
Inbounds: true,
})

c.setLocationMeta(result, expr)
c.setLocationMeta(result, expr.Name)

c.exprResult = exprValue{
v: result,
Expand All @@ -751,7 +751,7 @@ func (c *codegen) VisitMember(expr *ast.Member) {
Indices: []uint32{uint32(node.Index())},
})

c.setLocationMeta(result, expr)
c.setLocationMeta(result, expr.Name)
c.exprResult = exprValue{v: result}
}
}
Expand Down
20 changes: 20 additions & 0 deletions core/codegen/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"fireball/core/ast"
"fireball/core/ir"
"fireball/core/scanner"
)

func (c *codegen) alloca(type_ ast.Type, name string, node ast.Node) ir.Value {
Expand Down Expand Up @@ -40,3 +41,22 @@ func (c *codegen) setLocationMeta(value ir.MetaValue, node ast.Node) {

value.SetMeta(c.module.Meta(meta))
}

func (c *codegen) setLocationMetaCst(value ir.MetaValue, node ast.Node, kind scanner.TokenKind) {
if node == nil {
return
}

meta := &ir.LocationMeta{
Scope: c.scopes.getMeta(),
}

if node.Cst() != nil {
token := node.Cst().Get(kind)

meta.Line = uint32(token.Range.Start.Line)
meta.Column = uint32(token.Range.Start.Column)
}

value.SetMeta(c.module.Meta(meta))
}
3 changes: 3 additions & 0 deletions core/codegen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ func (t *types) getMeta(type_ ast.Type) ir.MetaID {
Elements: []ir.MetaID{
t.module.Meta(&ir.DerivedTypeMeta{
Tag: ir.MemberTag,
Name: "vtable",
BaseType: t.getMeta(&ptr),
Offset: 0,
}),
t.module.Meta(&ir.DerivedTypeMeta{
Tag: ir.MemberTag,
Name: "data",
BaseType: t.getMeta(&ptr),
Offset: ptr.Size() * 8,
}),
Expand Down Expand Up @@ -380,6 +382,7 @@ func (t *types) getMeta(type_ ast.Type) ir.MetaID {

fields[i] = t.module.Meta(&ir.DerivedTypeMeta{
Tag: ir.MemberTag,
Name: field.Name.String(),
BaseType: t.getMeta(field.Type),
Offset: offset * 8,
})
Expand Down
1 change: 1 addition & 0 deletions core/ir/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ type DerivedTypeMeta struct {
baseMeta

Tag DerivedTagKind
Name string
BaseType MetaID

Size uint32
Expand Down
7 changes: 6 additions & 1 deletion core/llvm/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (w *textWriter) writeMeta(meta ir.Meta) {
w.writeString("tag: ")
w.writeString(meta.Tag.String())

if meta.Name != "" {
w.writeString(", name: ")
w.writeQuotedString(meta.Name)
}

w.writeString(", baseType: ")
w.writeMetaRef(meta.BaseType)

Expand Down Expand Up @@ -340,7 +345,7 @@ func (w *textWriter) writeMeta(meta ir.Meta) {
w.writeUint(uint64(meta.Line), 10)

w.writeString(", column: ")
w.writeUint(uint64(meta.Column), 10)
w.writeUint(uint64(meta.Column)+1, 10)

w.writeRune(')')

Expand Down

0 comments on commit fb84e9a

Please sign in to comment.