Skip to content

Commit 53ccb4c

Browse files
committed
misc: code refactor
1 parent d2ab084 commit 53ccb4c

File tree

31 files changed

+398
-144
lines changed

31 files changed

+398
-144
lines changed

instructions/bytecode_reader.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

instructions/code_reader.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package instructions
2+
3+
type CodeReader struct {
4+
code []byte
5+
offset int
6+
}
7+
8+
func NewByteCodeReader(code []byte, offset int) *CodeReader {
9+
return &CodeReader{code, offset}
10+
}
11+
12+
func (r *CodeReader) Offset() int {
13+
return r.offset
14+
}
15+
16+
func (r *CodeReader) ReadUint8() uint8 {
17+
return uint8(r.ReadBytes(1)[0])
18+
}
19+
20+
func (r *CodeReader) ReadUint16() uint16 {
21+
byte1 := uint16(r.ReadUint8())
22+
byte2 := uint16(r.ReadUint8())
23+
return (byte1 << 8) | byte2
24+
}
25+
26+
func (r *CodeReader) ReadInt8() int8 {
27+
return int8(r.ReadBytes(1)[0])
28+
}
29+
30+
func (r *CodeReader) ReadInt16() int16 {
31+
byte1 := int16(r.ReadUint8())
32+
byte2 := int16(r.ReadUint8())
33+
return (byte1 << 8) | byte2
34+
}
35+
36+
func (r *CodeReader) ReadBytes(len int) []byte {
37+
result := r.code[r.offset : r.offset+len]
38+
r.offset += len
39+
return result
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package comparisons
2+
3+
import (
4+
"github.com/tinycedar/vanilla/instructions"
5+
)
6+
7+
func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instructions.Instruction {
8+
switch opCode {
9+
case 0xa2:
10+
return &if_icmpge{opCode, reader.ReadUint16()}
11+
default:
12+
return instructions.NewNotImplemented(opCode)
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package comparisons
2+
3+
import (
4+
"github.com/tinycedar/vanilla/runtime/thread"
5+
)
6+
7+
type if_icmpge struct {
8+
opCode uint8
9+
offset uint16
10+
}
11+
12+
func (i *if_icmpge) Execute(f *thread.Frame) {
13+
value2 := f.OperandStack().PopInt()
14+
value1 := f.OperandStack().PopInt()
15+
if value1 >= value2 {
16+
f.SetPC(f.GetPC() + int(i.offset))
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package constants
2+
3+
import (
4+
"github.com/tinycedar/vanilla/instructions"
5+
)
6+
7+
func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instructions.Instruction {
8+
switch opCode {
9+
case 0x03:
10+
return &iconst_0{opCode}
11+
case 0x04:
12+
return &iconst_1{opCode}
13+
case 0x05:
14+
return &iconst_2{opCode}
15+
case 0x10:
16+
return &bipush{opCode, reader.ReadInt8()}
17+
case 0x12:
18+
return &ldc{opCode, reader.ReadUint8()}
19+
default:
20+
return instructions.NewNotImplemented(opCode)
21+
}
22+
}

instructions/const.go renamed to instructions/constants/iconst.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
package instructions
1+
package constants
22

33
import (
44
"fmt"
55
"github.com/tinycedar/vanilla/runtime/thread"
66
)
77

8+
type iconst_0 struct {
9+
opCode uint8
10+
}
11+
812
type iconst_1 struct {
913
opCode uint8
1014
}
@@ -13,6 +17,14 @@ type iconst_2 struct {
1317
opCode uint8
1418
}
1519

20+
func (i *iconst_0) Execute(f *thread.Frame) {
21+
f.OperandStack().PushInt(0)
22+
}
23+
24+
func (i *iconst_0) String() string {
25+
return fmt.Sprintf("{opcode: 0x%x, iconst_0}", i.opCode)
26+
}
27+
1628
func (i *iconst_1) Execute(f *thread.Frame) {
1729
f.OperandStack().PushInt(1)
1830
}
@@ -28,3 +40,16 @@ func (i *iconst_2) Execute(f *thread.Frame) {
2840
func (i *iconst_2) String() string {
2941
return fmt.Sprintf("{opcode: 0x%x, iconst_2}", i.opCode)
3042
}
43+
44+
type bipush struct {
45+
opCode uint8
46+
value int8
47+
}
48+
49+
func (i *bipush) Execute(f *thread.Frame) {
50+
f.OperandStack().PushInt(int32(i.value))
51+
}
52+
53+
func (i *bipush) String() string {
54+
return fmt.Sprintf("{opcode: 0x%x, bipush}", i.opCode)
55+
}

instructions/ldc.go renamed to instructions/constants/ldc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package instructions
1+
package constants
22

33
import (
44
"fmt"

instructions/control/control.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package control
2+
3+
import (
4+
"github.com/tinycedar/vanilla/instructions"
5+
)
6+
7+
func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instructions.Instruction {
8+
switch opCode {
9+
case 0xa7:
10+
return &_goto{opCode, reader.ReadInt16()}
11+
case 0xac:
12+
return &ireturn{opCode}
13+
case 0xb1:
14+
return &_return{opCode}
15+
default:
16+
return instructions.NewNotImplemented(opCode)
17+
}
18+
}

instructions/control/goto.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package control
2+
3+
import (
4+
"github.com/tinycedar/vanilla/runtime/thread"
5+
)
6+
7+
type _goto struct {
8+
opCode uint8
9+
branchoffs int16
10+
}
11+
12+
func (i *_goto) Execute(f *thread.Frame) {
13+
f.SetPC(f.GetPC() + int(i.branchoffs))
14+
}

instructions/return.go renamed to instructions/control/return.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
package instructions
1+
package control
22

33
import (
44
"fmt"
55
"github.com/tinycedar/vanilla/runtime/thread"
66
)
77

8+
type ireturn struct {
9+
opCode uint8
10+
}
11+
12+
func (i *ireturn) Execute(f *thread.Frame) {
13+
val := f.OperandStack().PopInt()
14+
f.NextFrame().OperandStack().PushInt(val)
15+
f.Thread().Pop()
16+
}
17+
818
type _return struct {
919
opCode uint8
1020
}

0 commit comments

Comments
 (0)