-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.go
50 lines (42 loc) · 1.09 KB
/
interpreter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"HTVM/classfile"
"HTVM/runtime"
"fmt"
"HTVM/instructions/base"
"HTVM/instructions"
)
func interpreter(methodInfo *classfile.MemberInfo) {
codeAttr := methodInfo.CodeAttibutes()
maxLocals := uint(codeAttr.MaxLocals())
maxStack := uint(codeAttr.MaxStack())
bytecode := codeAttr.Code()
thread := runtime.NewThread()
frame := thread.NewFrame(maxLocals, maxStack)
thread.PushFrame(frame)
defer catchErr(frame)
loop(thread, bytecode)
}
func catchErr(frame *runtime.Frame) {
if r := recover(); r != nil {
fmt.Printf("Local Vars:%v\n", frame.LocalVars())
fmt.Printf("Operate Stack:%v\n", frame.OperateStack())
panic(r)
}
}
func loop(thread *runtime.Thread, bytecode []byte) {
frame := thread.PopFrame()
reader := &base.BytecodeReader{}
for {
pc := frame.NextPc()
thread.SetPC(pc)
reader.Reset(bytecode, pc)
opcode := reader.ReadUint8()
//fmt.Printf("opcode = %v\n", opcode)
inst := instructions.NewInstruction(opcode)
inst.FetchOperands(reader)
frame.SetNextPc(reader.PC())
fmt.Printf("pc:%2d inst:%T %v\n", pc, inst, inst)
inst.Execute(frame)
}
}