diff options
| author | Mel <einebeere@gmail.com> | 2022-08-22 23:55:57 +0000 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-08-22 23:55:57 +0000 |
| commit | 0b227d990f56b3ea059d13584ff9102ee5039a0b (patch) | |
| tree | 738f99368cbfd47b02290f53f2876faaae4dbd75 /pkg/lang | |
| parent | e22df631c4428f8dea4d5784a8a7840d2f84c6be (diff) | |
| download | jinx-0b227d990f56b3ea059d13584ff9102ee5039a0b.tar.zst jinx-0b227d990f56b3ea059d13584ff9102ee5039a0b.zip | |
Add flag to generate PCs markers when decompiling
Diffstat (limited to 'pkg/lang')
| -rw-r--r-- | pkg/lang/compiler/compiler_test.go | 4 | ||||
| -rw-r--r-- | pkg/lang/vm/text/decompiler.go | 16 | ||||
| -rw-r--r-- | pkg/lang/vm/text/decompiler_test.go | 2 |
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) } |
