Skip to content

Commit 22c0be3

Browse files
committed
misc: add fload_x/fstore_x
1 parent 0425535 commit 22c0be3

File tree

15 files changed

+142
-33
lines changed

15 files changed

+142
-33
lines changed

instructions/constants/ldc_x.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ type ldc_x struct {
1212
}
1313

1414
func (i *ldc_x) Execute(f *thread.Frame) {
15-
if cp, ok := f.Method().Cp[i.index].(*classfile.ConstantStringInfo); ok {
15+
cpInfo := f.Method().Cp[i.index]
16+
if cp, ok := cpInfo.(*classfile.ConstantStringInfo); ok {
1617
f.OperandStack().PushString(cp.String(f.Method().Cp))
1718
}
19+
if cp, ok := cpInfo.(*classfile.ConstantFloatInfo); ok {
20+
f.OperandStack().PushFloat(cp.Value())
21+
}
1822
//TODO
1923
}
2024

instructions/control/control.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
)
66

77
func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instructions.Instruction {
8-
switch opCode {
9-
case 0xa7:
8+
switch {
9+
case opCode == 0xa7:
1010
return &_goto{opCode, reader.ReadInt16()}
11-
case 0xac:
12-
return &ireturn{opCode}
13-
case 0xb1:
11+
case 0xac <= opCode && opCode <= 0xaf:
12+
return &xreturn{opCode}
13+
case opCode == 0xb1:
1414
return &_return{opCode}
1515
default:
1616
return instructions.NewNotImplemented(opCode)

instructions/control/return.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@ import (
55
"github.com/tinycedar/vanilla/runtime/thread"
66
)
77

8-
type ireturn struct {
8+
type xreturn struct {
99
opCode uint8
1010
}
1111

12-
func (i *ireturn) Execute(f *thread.Frame) {
13-
val := f.OperandStack().PopInt()
14-
f.NextFrame().OperandStack().PushInt(val)
12+
func (i *xreturn) Execute(f *thread.Frame) {
13+
switch i.opCode {
14+
case 0xac:
15+
val := f.OperandStack().PopInt()
16+
f.NextFrame().OperandStack().PushInt(val)
17+
case 0xad:
18+
val := f.OperandStack().PopLong()
19+
f.NextFrame().OperandStack().PushLong(val)
20+
case 0xae:
21+
val := f.OperandStack().PopFloat()
22+
f.NextFrame().OperandStack().PushFloat(val)
23+
case 0xaf:
24+
val := f.OperandStack().PopDouble()
25+
f.NextFrame().OperandStack().PushDouble(val)
26+
}
1527
f.Thread().Pop()
1628
}
1729

30+
//TODO to be removed
1831
type _return struct {
1932
opCode uint8
2033
}
@@ -24,10 +37,5 @@ func (i *_return) Execute(f *thread.Frame) {
2437
}
2538

2639
func (i *_return) String() string {
27-
return fmt.Sprintf("{opcode: 0x%x, return}", i.opCode)
28-
}
29-
30-
// type assertion
31-
func __return() {
32-
40+
return fmt.Sprintf("{opcode: 0x%x, xeturn}", i.opCode)
3341
}

instructions/loads/fload_x.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package loads
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type fload_x struct {
9+
opCode uint8
10+
index uint8
11+
}
12+
13+
func (fl *fload_x) Execute(f *thread.Frame) {
14+
val := f.LocalVars().GetFloat(fl.index)
15+
f.OperandStack().PushFloat(val)
16+
}
17+
18+
func (fl *fload_x) String() string {
19+
return fmt.Sprintf("{opcode: 0x%x, fload_%d}", fl.opCode, fl.index)
20+
}

instructions/loads/loads.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
1010
return &iload_x{opCode, reader.ReadUint8()}
1111
case 0x16:
1212
return &lload_x{opCode, reader.ReadUint8()}
13+
case 0x17:
14+
return &fload_x{opCode, reader.ReadUint8()}
15+
// iload_<n>
1316
case 0x1a:
1417
return &iload_x{opCode, 0}
1518
case 0x1b:
@@ -18,6 +21,7 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
1821
return &iload_x{opCode, 2}
1922
case 0x1d:
2023
return &iload_x{opCode, 3}
24+
// lload_<n>
2125
case 0x1e:
2226
return &lload_x{opCode, 0}
2327
case 0x1f:
@@ -26,6 +30,15 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
2630
return &lload_x{opCode, 2}
2731
case 0x21:
2832
return &lload_x{opCode, 3}
33+
// fload_<n>
34+
case 0x22:
35+
return &fload_x{opCode, 0}
36+
case 0x23:
37+
return &fload_x{opCode, 1}
38+
case 0x24:
39+
return &fload_x{opCode, 2}
40+
case 0x25:
41+
return &fload_x{opCode, 3}
2942
default:
3043
return instructions.NewNotImplemented(opCode)
3144
}

instructions/math/add.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ import (
55
"github.com/tinycedar/vanilla/runtime/thread"
66
)
77

8-
type iadd struct {
8+
type xadd struct {
99
opCode uint8
1010
}
1111

12-
func (i *iadd) Execute(f *thread.Frame) {
12+
func (i *xadd) Execute(f *thread.Frame) {
1313
stack := f.OperandStack()
14-
stack.PushInt(stack.PopInt() + stack.PopInt())
14+
switch i.opCode {
15+
case 0x60:
16+
stack.PushInt(stack.PopInt() + stack.PopInt())
17+
case 0x61:
18+
stack.PushLong(stack.PopLong() + stack.PopLong())
19+
case 0x62:
20+
stack.PushFloat(stack.PopFloat() + stack.PopFloat())
21+
case 0x63:
22+
stack.PushDouble(stack.PopDouble() + stack.PopDouble())
23+
}
1524
}
1625

17-
func (i *iadd) String() string {
18-
return fmt.Sprintf("{opcode: 0x%x, iadd}", i.opCode)
26+
func (i *xadd) String() string {
27+
return fmt.Sprintf("{opcode: 0x%x, xadd}", i.opCode)
1928
}

instructions/math/math.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instructions.Instruction {
8-
switch opCode {
9-
case 0x60:
10-
return &iadd{opCode}
11-
case 0x84:
8+
switch {
9+
case 0x60 <= opCode && opCode <= 0x63:
10+
return &xadd{opCode}
11+
case opCode == 0x84:
1212
return &iinc{opCode, reader.ReadUint8(), reader.ReadInt8()}
1313
default:
1414
return instructions.NewNotImplemented(opCode)

instructions/references/invoke_virtual.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (i *invokevirtual) Execute(f *thread.Frame) {
2121
fmt.Println(f.OperandStack().PopString())
2222
} else if strings.LastIndex(invoked, "(I)V") >= 0 {
2323
fmt.Println(f.OperandStack().PopInt())
24+
} else if strings.LastIndex(invoked, "(F)V") >= 0 {
25+
fmt.Println(f.OperandStack().PopFloat())
26+
} else {
27+
panic("Not implemented return value: " + invoked)
2428
}
2529
}
2630
}

instructions/stores/fstore_x.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package stores
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type fstore_x struct {
9+
opCode uint8
10+
index uint8
11+
}
12+
13+
func (fs *fstore_x) Execute(f *thread.Frame) {
14+
val := f.OperandStack().PopFloat()
15+
f.LocalVars().SetFloat(fs.index, val)
16+
}
17+
18+
func (fs *fstore_x) String() string {
19+
return fmt.Sprintf("{opcode: 0x%x, fstore_%d}", fs.opCode, fs.index)
20+
}

instructions/stores/stores.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
1010
return &istore_x{opCode, reader.ReadUint8()}
1111
case 0x37:
1212
return &lstore_x{opCode, reader.ReadUint8()}
13-
case 0x3f:
14-
return &lstore_x{opCode, 0}
15-
case 0x40:
16-
return &lstore_x{opCode, 1}
17-
case 0x41:
18-
return &lstore_x{opCode, 2}
19-
case 0x42:
20-
return &lstore_x{opCode, 3}
13+
case 0x38:
14+
return &fstore_x{opCode, reader.ReadUint8()}
15+
// istore_<n>
2116
case 0x3b:
2217
return &istore_x{opCode, 0}
2318
case 0x3c:
@@ -26,6 +21,25 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
2621
return &istore_x{opCode, 2}
2722
case 0x3e:
2823
return &istore_x{opCode, 3}
24+
// lstore_<n>
25+
case 0x3f:
26+
return &lstore_x{opCode, 0}
27+
case 0x40:
28+
return &lstore_x{opCode, 1}
29+
case 0x41:
30+
return &lstore_x{opCode, 2}
31+
case 0x42:
32+
return &lstore_x{opCode, 3}
33+
// fstore_<n>
34+
case 0x43:
35+
return &fstore_x{opCode, 0}
36+
case 0x44:
37+
return &fstore_x{opCode, 1}
38+
case 0x45:
39+
return &fstore_x{opCode, 2}
40+
case 0x46:
41+
return &fstore_x{opCode, 3}
42+
2943
default:
3044
return instructions.NewNotImplemented(opCode)
3145
}

0 commit comments

Comments
 (0)