Skip to content

Commit

Permalink
cmd/starlark: -disassemble flag causes compiler to print disassembly (g…
Browse files Browse the repository at this point in the history
…oogle#207)

Also, print file:line:column in disassembly output.
  • Loading branch information
adonovan authored Jul 12, 2019
1 parent 77c1099 commit d6561f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cmd/starlark/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"runtime/pprof"
"strings"

"go.starlark.net/internal/compile"
"go.starlark.net/repl"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
Expand All @@ -29,8 +30,10 @@ var (
execprog = flag.String("c", "", "execute program `prog`")
)

// non-standard dialect flags
func init() {
flag.BoolVar(&compile.Disassemble, "disassemble", compile.Disassemble, "show disassembly during compilation of each function")

// non-standard dialect flags
flag.BoolVar(&resolve.AllowFloat, "float", resolve.AllowFloat, "allow floating-point numbers")
flag.BoolVar(&resolve.AllowSet, "set", resolve.AllowSet, "allow set data type")
flag.BoolVar(&resolve.AllowLambda, "lambda", resolve.AllowLambda, "allow lambda expressions")
Expand Down
20 changes: 12 additions & 8 deletions internal/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import (
"go.starlark.net/syntax"
)

const debug = false // TODO(adonovan): use a bitmap of options; and regexp to match files
// Disassemble causes the assembly code for each function
// to be printed to stderr as it is generated.
var Disassemble = false

const debug = false // make code generation verbose, for debugging the compiler

// Increment this to force recompilation of saved bytecode files.
const Version = 10
Expand Down Expand Up @@ -645,7 +649,7 @@ func (pcomp *pcomp) function(name string, pos syntax.Position, stmts []syntax.St
fn.MaxStack = maxstack

// Emit bytecode (and position table).
if debug {
if Disassemble {
fmt.Fprintf(os.Stderr, "Function %s: (%d blocks, %d bytes)\n", name, len(blocks), pc)
}
fcomp.generate(blocks, pc)
Expand Down Expand Up @@ -726,7 +730,7 @@ func (fcomp *fcomp) generate(blocks []*block, codelen uint32) {
}

for _, b := range blocks {
if debug {
if Disassemble {
fmt.Fprintf(os.Stderr, "%d:\n", b.index)
}
pc := b.addr
Expand Down Expand Up @@ -766,12 +770,12 @@ func (fcomp *fcomp) generate(blocks []*block, codelen uint32) {
}
}

if debug {
fmt.Fprintf(os.Stderr, "\t\t\t\t\t; %s %d\n",
filepath.Base(fcomp.fn.Pos.Filename()), insn.line)
if Disassemble {
fmt.Fprintf(os.Stderr, "\t\t\t\t\t; %s:%d:%d\n",
filepath.Base(fcomp.fn.Pos.Filename()), insn.line, insn.col)
}
}
if debug {
if Disassemble {
PrintOp(fcomp.fn, pc, insn.op, insn.arg)
}
code = append(code, byte(insn.op))
Expand All @@ -788,7 +792,7 @@ func (fcomp *fcomp) generate(blocks []*block, codelen uint32) {

if b.jmp != nil && b.jmp.index != b.index+1 {
addr := b.jmp.addr
if debug {
if Disassemble {
fmt.Fprintf(os.Stderr, "\t%d\tjmp\t\t%d\t; block %d\n",
pc, addr, b.jmp.index)
}
Expand Down

0 comments on commit d6561f8

Please sign in to comment.