aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang/vm/code
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-05-27 16:44:22 +0000
committerGitHub <noreply@github.com>2022-05-27 16:49:02 +0000
commit47c4cd3705bee9d7154c42ce95aef6f8a19e0661 (patch)
treef6b2d502cbc256914a6f7181e2cf623460f9d912 /pkg/lang/vm/code
parentc2d4bf51de9a2d721168c62b14b89f5281ed366e (diff)
downloadjinx-47c4cd3705bee9d7154c42ce95aef6f8a19e0661.tar.zst
jinx-47c4cd3705bee9d7154c42ce95aef6f8a19e0661.zip
Add debug info to compiled VM code
Diffstat (limited to 'pkg/lang/vm/code')
-rw-r--r--pkg/lang/vm/code/code.go12
-rw-r--r--pkg/lang/vm/code/debug.go31
2 files changed, 40 insertions, 3 deletions
diff --git a/pkg/lang/vm/code/code.go b/pkg/lang/vm/code/code.go
index bd25fcd..38bbcf5 100644
--- a/pkg/lang/vm/code/code.go
+++ b/pkg/lang/vm/code/code.go
@@ -8,12 +8,14 @@ import (
)
type Code struct {
- code []byte
+ code []byte
+ debugInfo DebugInfo
}
-func New(code []byte) Code {
+func New(code []byte, info DebugInfo) Code {
return Code{
- code: code,
+ code: code,
+ debugInfo: info,
}
}
@@ -25,6 +27,10 @@ func (c *Code) Code() []byte {
return c.code
}
+func (c *Code) Debug() *DebugInfo {
+ return &c.debugInfo
+}
+
func (c *Code) GetOp(at int) (Op, int) {
return Op(c.code[at]), 1
}
diff --git a/pkg/lang/vm/code/debug.go b/pkg/lang/vm/code/debug.go
new file mode 100644
index 0000000..b96c834
--- /dev/null
+++ b/pkg/lang/vm/code/debug.go
@@ -0,0 +1,31 @@
+package code
+
+import "jinx/pkg/libs/rangemap"
+
+type DebugInfo struct {
+ file string
+ pcToLine rangemap.RangeMap[int]
+}
+
+func NewDebugInfo(file string) DebugInfo {
+ return DebugInfo{
+ file: file,
+ pcToLine: rangemap.New[int](),
+ }
+}
+
+func (di *DebugInfo) File() string {
+ return di.file
+}
+
+func (di *DebugInfo) PCToLine(pc int) int {
+ line, ok := di.pcToLine.Get(pc)
+ if !ok {
+ return -1
+ }
+ return *line
+}
+
+func (di *DebugInfo) AppendLine(uptoPc, line int) {
+ di.pcToLine.AppendToLast(uptoPc, line)
+}