From 5b216153e9bb3fada1dd5ece228dacca52851320 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 10 Feb 2025 22:28:25 +0800 Subject: [PATCH] cl: LLGO_TRACE --- compiler/cl/compile.go | 8 ++++++++ compiler/internal/build/build.go | 6 ++++++ compiler/ssa/expr.go | 15 +++++++++++++++ compiler/ssa/package.go | 2 ++ 4 files changed, 31 insertions(+) diff --git a/compiler/cl/compile.go b/compiler/cl/compile.go index 67dbdaf2..8ca765e1 100644 --- a/compiler/cl/compile.go +++ b/compiler/cl/compile.go @@ -49,6 +49,7 @@ var ( debugInstr bool debugGoSSA bool debugSymbols bool + debugTrace bool ) // SetDebug sets debug flags. @@ -61,6 +62,10 @@ func EnableDebugSymbols(b bool) { debugSymbols = b } +func EnableTrace(b bool) { + debugTrace = b +} + // ----------------------------------------------------------------------------- type instrOrValue interface { @@ -380,6 +385,9 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do var instrs = block.Instrs[n:] var ret = fn.Block(block.Index) b.SetBlock(ret) + if block.Index == 0 && debugTrace && !strings.HasPrefix(fn.Name(), "github.com/goplus/llgo/runtime/internal/runtime.Print") { + b.Printf("call " + fn.Name() + "\n\x00") + } // place here to avoid wrong current-block if debugSymbols && block.Index == 0 { p.debugParams(b, block.Parent()) diff --git a/compiler/internal/build/build.go b/compiler/internal/build/build.go index b66d5beb..abae1436 100644 --- a/compiler/internal/build/build.go +++ b/compiler/internal/build/build.go @@ -142,6 +142,7 @@ func Do(args []string, conf *Config) ([]Package, error) { } cl.EnableDebugSymbols(IsDebugEnabled()) + cl.EnableTrace(IsTraceEnabled()) llssa.Initialize(llssa.InitAll) target := &llssa.Target{ @@ -731,6 +732,7 @@ var ( ) const llgoDebug = "LLGO_DEBUG" +const llgoTrace = "LLGO_TRACE" const llgoOptimize = "LLGO_OPTIMIZE" const llgoCheck = "LLGO_CHECK" @@ -742,6 +744,10 @@ func isEnvOn(env string, defVal bool) bool { return envVal == "1" || envVal == "true" || envVal == "on" } +func IsTraceEnabled() bool { + return isEnvOn(llgoTrace, false) +} + func IsDebugEnabled() bool { return isEnvOn(llgoDebug, false) } diff --git a/compiler/ssa/expr.go b/compiler/ssa/expr.go index fa076253..31214271 100644 --- a/compiler/ssa/expr.go +++ b/compiler/ssa/expr.go @@ -1254,6 +1254,21 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) { panic("todo: " + fn) } +func (p Program) tyPrintf() *types.Signature { + if p.printfTy == nil { + pchar := types.NewPointer(types.Typ[types.Int8]) + params := types.NewTuple(types.NewVar(0, nil, "format", pchar), VArg()) + rets := types.NewTuple(types.NewVar(0, nil, "", types.Typ[types.Int32])) + p.printfTy = types.NewSignatureType(nil, nil, nil, params, rets, true) + } + return p.printfTy +} + +func (b Builder) Printf(fmt string, args ...Expr) Expr { + fn := b.Pkg.cFunc("printf", b.Prog.tyPrintf()) + return b.Call(fn, append([]Expr{b.CStr(fmt)}, args...)...) +} + // Println prints the arguments to stderr, followed by a newline. func (b Builder) Println(args ...Expr) (ret Expr) { return b.PrintEx(true, args...) diff --git a/compiler/ssa/package.go b/compiler/ssa/package.go index 8b4369cb..86ea3406 100644 --- a/compiler/ssa/package.go +++ b/compiler/ssa/package.go @@ -197,6 +197,8 @@ type aProgram struct { sigsetjmpTy *types.Signature sigljmpTy *types.Signature + printfTy *types.Signature + paramObjPtr_ *types.Var linkname map[string]string // pkgPath.nameInPkg => linkname