aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang/vm/text
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-08-22 23:55:57 +0000
committerMel <einebeere@gmail.com>2022-08-22 23:55:57 +0000
commit0b227d990f56b3ea059d13584ff9102ee5039a0b (patch)
tree738f99368cbfd47b02290f53f2876faaae4dbd75 /pkg/lang/vm/text
parente22df631c4428f8dea4d5784a8a7840d2f84c6be (diff)
downloadjinx-0b227d990f56b3ea059d13584ff9102ee5039a0b.tar.zst
jinx-0b227d990f56b3ea059d13584ff9102ee5039a0b.zip
Add flag to generate PCs markers when decompiling
Diffstat (limited to 'pkg/lang/vm/text')
-rw-r--r--pkg/lang/vm/text/decompiler.go16
-rw-r--r--pkg/lang/vm/text/decompiler_test.go2
2 files changed, 14 insertions, 4 deletions
diff --git a/pkg/lang/vm/text/decompiler.go b/pkg/lang/vm/text/decompiler.go
index df9e1ed..ed48412 100644
--- a/pkg/lang/vm/text/decompiler.go
+++ b/pkg/lang/vm/text/decompiler.go
@@ -14,12 +14,15 @@ type Decompiler struct {
c code.Code
pcToLine rangemap.RangeMap[int]
+
+ generatePCs bool
}
-func NewDecompiler(c code.Code) *Decompiler {
+func NewDecompiler(c code.Code, generatePCs bool) *Decompiler {
return &Decompiler{
- c: c,
- pcToLine: rangemap.New[int](),
+ c: c,
+ pcToLine: rangemap.New[int](),
+ generatePCs: generatePCs,
}
}
@@ -27,12 +30,19 @@ func (d *Decompiler) Decompile() string {
lines := make([]string, 0)
bc := d.c.Code()
+ longestPcStringLen := len(strconv.FormatInt(int64(len(bc)), 10))
+
for len(bc) != 0 {
+ startingPc := len(d.c.Code()) - len(bc)
+
line, rest := d.decompileInstruction(bc)
bc = rest
d.pcToLine.AppendToLast(d.c.Len()-len(bc), len(lines))
+ if d.generatePCs {
+ line = fmt.Sprintf("%*d: %s", longestPcStringLen, startingPc, line)
+ }
lines = append(lines, line)
}
diff --git a/pkg/lang/vm/text/decompiler_test.go b/pkg/lang/vm/text/decompiler_test.go
index 01e49b0..5b9a0d7 100644
--- a/pkg/lang/vm/text/decompiler_test.go
+++ b/pkg/lang/vm/text/decompiler_test.go
@@ -69,7 +69,7 @@ func test(t *testing.T, code string, expected string) {
resCompiled, err := comp.Compile()
require.NoError(t, err)
- decomp := text.NewDecompiler(resCompiled)
+ decomp := text.NewDecompiler(resCompiled, false)
resDecompiled := decomp.Decompile()
require.Equal(t, trimmedExpected, resDecompiled)
}