diff --git a/chore/llgen/llgen.go b/chore/llgen/llgen.go index d93e8d9d..4f14892b 100644 --- a/chore/llgen/llgen.go +++ b/chore/llgen/llgen.go @@ -20,7 +20,6 @@ import ( "fmt" "os" - "github.com/goplus/llgo/internal/build" "github.com/goplus/llgo/internal/llgen" ) @@ -29,7 +28,7 @@ func main() { fmt.Fprintln(os.Stderr, "Usage: llgen [flags] [pkgPath]") return } - llgen.Init(build.IsDebugEnabled()) + llgen.Init() args := os.Args[1:] llgen.SmartDoFile(args[0], args[1:]...) } diff --git a/cl/compile.go b/cl/compile.go index cb697c7e..296ac061 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -298,7 +298,11 @@ func (p *context) debugParams(b llssa.Builder, f *ssa.Function) { ty := param.Type() argNo := i + 1 div := b.DIVarParam(p.fn, pos, param.Name(), p.prog.Type(ty, llssa.InGo), argNo) - b.DIDeclare(v, div, p.fn, pos, p.fn.Block(0)) + if _, ok := param.Type().(*types.Pointer); ok { + b.DIDeclare(v, div, p.fn, pos, p.fn.Block(0)) + } else { + b.DIDeclare(v, div, p.fn, pos, p.fn.Block(0)) + } } } diff --git a/internal/build/build.go b/internal/build/build.go index 7e970a78..b377b325 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -187,7 +187,14 @@ func Do(args []string, conf *Config) { return dedup.Check(llssa.PkgPython).Types }) - progSSA := ssa.NewProgram(initial[0].Fset, ssaBuildMode) + buildMode := ssaBuildMode + if cl.DebugSymbols() { + buildMode |= ssa.GlobalDebug + } + if !IsOptimizeEnabled() { + buildMode |= ssa.NaiveForm + } + progSSA := ssa.NewProgram(initial[0].Fset, buildMode) patches := make(cl.Patches, len(altPkgPaths)) altSSAPkgs(progSSA, patches, altPkgs[1:], verbose) @@ -237,7 +244,7 @@ func isNeedRuntimeOrPyInit(pkg *packages.Package) (needRuntime, needPyInit bool) } const ( - ssaBuildMode = ssa.SanityCheckFunctions | ssa.InstantiateGenerics | ssa.GlobalDebug + ssaBuildMode = ssa.SanityCheckFunctions | ssa.InstantiateGenerics ) type context struct { @@ -609,10 +616,22 @@ var ( ) const llgoDebug = "LLGO_DEBUG" +const llgoOptimize = "LLGO_OPTIMIZE" + +func isEnvOn(env string, defVal bool) bool { + envVal := strings.ToLower(os.Getenv(env)) + if envVal == "" { + return defVal + } + return envVal == "1" || envVal == "true" || envVal == "on" +} func IsDebugEnabled() bool { - llgoDbgVal := strings.ToLower(os.Getenv(llgoDebug)) - return llgoDbgVal == "1" || llgoDbgVal == "true" || llgoDbgVal == "on" + return isEnvOn(llgoDebug, false) +} + +func IsOptimizeEnabled() bool { + return isEnvOn(llgoOptimize, true) } func ParseArgs(args []string, swflags map[string]bool) (flags, patterns []string, verbose bool) { diff --git a/internal/llgen/llgen.go b/internal/llgen/llgen.go index 9b6de2e6..14440c42 100644 --- a/internal/llgen/llgen.go +++ b/internal/llgen/llgen.go @@ -20,16 +20,17 @@ import ( "os" "github.com/goplus/llgo/cl" + "github.com/goplus/llgo/internal/build" "github.com/goplus/llgo/internal/mod" llssa "github.com/goplus/llgo/ssa" ) -func Init(enableDbg bool) { +func Init() { llssa.Initialize(llssa.InitAll) llssa.SetDebug(llssa.DbgFlagAll) cl.SetDebug(cl.DbgFlagAll) - cl.EnableDebugSymbols(enableDbg) + cl.EnableDebugSymbols(build.IsDebugEnabled()) } func PkgPath(dir string) string { diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index a55bbfb5..cf55c856 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -25,6 +25,7 @@ import ( "strings" "github.com/goplus/llgo/cl" + "github.com/goplus/llgo/internal/build" "github.com/goplus/llgo/internal/packages" "golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa/ssautil" @@ -90,7 +91,14 @@ func genFrom(fileOrPkg string, pkgPath string) string { initial, err := packages.LoadEx(dedup, prog.TypeSizes, cfg, fileOrPkg) check(err) - _, pkgs := ssautil.AllPackages(initial, ssa.SanityCheckFunctions|ssa.InstantiateGenerics|ssa.GlobalDebug) + buildMode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics + if build.IsDebugEnabled() { + buildMode |= ssa.GlobalDebug + } + if !build.IsOptimizeEnabled() { + buildMode |= ssa.NaiveForm + } + _, pkgs := ssautil.AllPackages(initial, buildMode) pkg := initial[0] ssaPkg := pkgs[0] diff --git a/ssa/eh.go b/ssa/eh.go index 902aabac..2e30e867 100644 --- a/ssa/eh.go +++ b/ssa/eh.go @@ -141,6 +141,10 @@ const ( ) func (b Builder) getDefer(kind DoAction) *aDefer { + if b.Func.recov == nil { + // b.Func.recov maybe nil in ssa.NaiveForm + return nil + } self := b.Func if self.defer_ == nil { // TODO(xsw): check if in pkg.init @@ -241,6 +245,9 @@ func (b Builder) Defer(kind DoAction, fn Expr, args ...Expr) { // RunDefers emits instructions to run deferred instructions. func (b Builder) RunDefers() { self := b.getDefer(DeferInCond) + if self == nil { + return + } blk := b.Func.MakeBlock() self.rundsNext = append(self.rundsNext, blk)