Skip to content

Commit 6ee2fc4

Browse files
committed
goobj: spell wasm statements with a writer type
wasmStatements had grown to about 470 lines of closures sharing the stack, the open blocks, Go's SP and the reads of the current row, which pushExpr picked up from variables the loop and local.tee reset by hand. The state now lives in stmtWriter, whose methods spell the blocks, branches, calls and values apart, and goSP follows Go's SP on its own. The precedences have names, a block opener is recognised by one function, and wasmBranches reports the blocks that branches go to instead of a second walk finding them again. Blocks now take their parameters and leave several values: an else branch starts from its if's parameters again, a block's values read L1.0 and L1.1, and a call with several results assigns a temporary each, "t1, t2 = g()", where it showed "?".
1 parent 1ef992f commit 6ee2fc4

3 files changed

Lines changed: 755 additions & 567 deletions

File tree

‎internal/goobj/wasm.go‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ func disassembleWasm(file *File, fn *Func, opts disasm.Options) (*disasm.Code, e
3131
// steps holds each open block's step, 0 or 1.
3232
level := 0
3333
var steps []int
34-
opener := func(op string) bool {
35-
switch op {
36-
// watgo prints try_table's catch clauses inline and has no
37-
// legacy try, so try_table is the only other opener.
38-
case "block", "loop", "if", "try_table":
39-
return true
40-
}
41-
return false
42-
}
4334
for i, in := range decoded {
4435
switch {
4536
case in.Op == "end" && len(steps) > 0:
@@ -50,9 +41,9 @@ func disassembleWasm(file *File, fn *Func, opts disasm.Options) (*disasm.Code, e
5041
}
5142
inst := disasm.Inst{PC: uint64(i), Text: in.Text, Indent: min(level, maxIndent)}
5243
switch {
53-
case opener(in.Op):
44+
case isWasmOpener(in.Op):
5445
step := 1
55-
if i > 0 && opener(decoded[i-1].Op) && in.Op != "if" {
46+
if i > 0 && isWasmOpener(decoded[i-1].Op) && in.Op != "if" {
5647
step = 0
5748
}
5849
steps = append(steps, step)
@@ -71,13 +62,13 @@ func disassembleWasm(file *File, fn *Func, opts disasm.Options) (*disasm.Code, e
7162
refs.Add(inst.File, inst.Line, i)
7263
code.Insts = append(code.Insts, inst)
7364
}
74-
wasmBranches(code.Insts)
65+
targeted := wasmBranches(code.Insts)
7566
if info := file.wasmModule(); info != nil {
7667
var goArgs map[int64]string
7768
if info.goWasm {
7869
goArgs = goArgNames(code.File, fn.Name())
7970
}
80-
wasmStatements(info, int(fn.fn.Addr), code.Insts, goArgs)
71+
wasmStatements(info, int(fn.fn.Addr), code.Insts, targeted, goArgs)
8172
code.Statements = true
8273
}
8374
code.Source = source.Load(fn.file.files, refs, code.File, opts.Context)
@@ -88,7 +79,10 @@ func disassembleWasm(file *File, fn *Func, opts disasm.Options) (*disasm.Code, e
8879
// so the view draws it as a jump. A branch names its target by how many
8980
// blocks out it is; a loop's label is its start and a block's or an
9081
// if's is its end. br_table points at its default target.
91-
func wasmBranches(insts []disasm.Inst) {
82+
//
83+
// It returns the rows of the blocks that branches go to.
84+
func wasmBranches(insts []disasm.Inst) map[int]bool {
85+
targeted := map[int]bool{}
9286
type label struct {
9387
at int // the opener's row
9488
loop bool // branches go back to at
@@ -98,10 +92,10 @@ func wasmBranches(insts []disasm.Inst) {
9892
stack := []*label{{at: -1}}
9993
for i := range insts {
10094
op, arg, _ := strings.Cut(insts[i].Text, " ")
101-
switch op {
102-
case "block", "loop", "if", "try_table":
95+
switch {
96+
case isWasmOpener(op):
10397
stack = append(stack, &label{at: i, loop: op == "loop"})
104-
case "end":
98+
case op == "end":
10599
if len(stack) > 1 || i == len(insts)-1 {
106100
l := stack[len(stack)-1]
107101
for _, b := range l.branches {
@@ -112,11 +106,17 @@ func wasmBranches(insts []disasm.Inst) {
112106
stack = stack[:len(stack)-1]
113107
}
114108
}
115-
case "br", "br_if", "br_table":
109+
case op == "br" || op == "br_if" || op == "br_table":
116110
fields := strings.Fields(arg)
117111
if len(fields) == 0 {
118112
continue
119113
}
114+
// The jump goes to br_table's default, the last depth.
115+
for _, f := range fields {
116+
if d, err := strconv.Atoi(f); err == nil && d < len(stack) && stack[len(stack)-1-d].at >= 0 {
117+
targeted[stack[len(stack)-1-d].at] = true
118+
}
119+
}
120120
depth, err := strconv.Atoi(fields[len(fields)-1])
121121
if err != nil || depth >= len(stack) {
122122
continue
@@ -129,6 +129,7 @@ func wasmBranches(insts []disasm.Inst) {
129129
}
130130
}
131131
}
132+
return targeted
132133
}
133134

134135
// wasmCall names the callee of a call. objfile names it in Text from
@@ -145,3 +146,20 @@ func wasmCall(in objfile.Inst) string {
145146
}
146147
return ""
147148
}
149+
150+
// isWasmOpener reports whether a wasm instruction opens a block. watgo
151+
// prints try_table's catch clauses inline and has no legacy try, so
152+
// try_table is the only other opener.
153+
func isWasmOpener(op string) bool {
154+
switch op {
155+
case "block", "loop", "if", "try_table":
156+
return true
157+
}
158+
return false
159+
}
160+
161+
// wasmOp is the instruction in a row's text.
162+
func wasmOp(text string) string {
163+
op, _, _ := strings.Cut(text, " ")
164+
return op
165+
}

0 commit comments

Comments
 (0)