aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang')
-rw-r--r--pkg/lang/compiler/compiler_test.go4
-rw-r--r--pkg/lang/vm/text/decompiler.go16
-rw-r--r--pkg/lang/vm/text/decompiler_test.go2
3 files changed, 16 insertions, 6 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 2e4e6e7..533bb43 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -687,8 +687,8 @@ func mustCompileTo(t *testing.T, src, expected string) {
require.NoError(t, err)
if !assert.Equal(t, expectedResult.Code(), testResult.Code().Code()) {
- d1 := text.NewDecompiler(expectedResult)
- d2 := text.NewDecompiler(*testResult.Code())
+ d1 := text.NewDecompiler(expectedResult, false)
+ d2 := text.NewDecompiler(*testResult.Code(), false)
require.Equal(t, d1.Decompile(), d2.Decompile())
}
}
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)
}