Skip to content

Commit 58500c6

Browse files
committed
misc: add lconst & dconst opcode
1 parent 04599cc commit 58500c6

File tree

9 files changed

+157
-47
lines changed

9 files changed

+157
-47
lines changed

instructions/constants/constants.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package constants
22

33
import (
4+
"fmt"
45
"github.com/tinycedar/vanilla/instructions"
56
"github.com/tinycedar/vanilla/runtime/thread"
67
)
@@ -31,12 +32,31 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
3132
return &iconst_x{opCode, 4}
3233
case 0x08:
3334
return &iconst_x{opCode, 5}
34-
//TODO
35+
case 0x09:
36+
return &lconst_x{opCode, 0}
37+
case 0x0a:
38+
return &lconst_x{opCode, 1}
39+
case 0x0b:
40+
return &fconst_x{opCode, 0.0}
41+
case 0x0c:
42+
return &fconst_x{opCode, 1.0}
43+
case 0x0d:
44+
return &fconst_x{opCode, 2.0}
45+
case 0x0e:
46+
return &dconst_x{opCode, 0.0}
47+
case 0x0f:
48+
return &dconst_x{opCode, 1.0}
3549
case 0x10:
3650
return &bipush{opCode, reader.ReadInt8()}
51+
case 0x11:
52+
return &sipush{opCode, reader.ReadInt16()}
3753
case 0x12:
38-
return &ldc{opCode, reader.ReadUint8()}
54+
return &ldc_x{opCode, uint16(reader.ReadUint8())}
55+
case 0x13:
56+
fallthrough
57+
case 0x14:
58+
return &ldc_x{opCode, reader.ReadUint16()}
3959
default:
40-
return instructions.NewNotImplemented(opCode)
60+
panic(fmt.Sprintf("invalid opCode: %d", opCode))
4161
}
4262
}

instructions/constants/dconst_x.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package constants
2+
3+
import (
4+
"github.com/tinycedar/vanilla/runtime/thread"
5+
)
6+
7+
type dconst_x struct {
8+
opCode uint8
9+
constVal float64
10+
}
11+
12+
func (l *dconst_x) Execute(f *thread.Frame) {
13+
f.OperandStack().PushDouble(l.constVal)
14+
}

instructions/constants/fconst_x.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package constants
2+
3+
import "github.com/tinycedar/vanilla/runtime/thread"
4+
5+
type fconst_x struct {
6+
opCode uint8
7+
constVal float32
8+
}
9+
10+
func (l *fconst_x) Execute(f *thread.Frame) {
11+
f.OperandStack().PushFloat(l.constVal)
12+
}

instructions/constants/iconst_x.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,3 @@ func (i *iconst_x) Execute(f *thread.Frame) {
1717
func (i *iconst_x) String() string {
1818
return fmt.Sprintf("{opcode: 0x%x, iconst_%d}", i.opCode, i.constVal)
1919
}
20-
21-
type bipush struct {
22-
opCode uint8
23-
value int8
24-
}
25-
26-
func (i *bipush) Execute(f *thread.Frame) {
27-
f.OperandStack().PushInt(int32(i.value))
28-
}
29-
30-
func (i *bipush) String() string {
31-
return fmt.Sprintf("{opcode: 0x%x, bipush}", i.opCode)
32-
}

instructions/constants/lconst_x.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package constants
2+
3+
import (
4+
"github.com/tinycedar/vanilla/runtime/thread"
5+
)
6+
7+
type lconst_x struct {
8+
opCode uint8
9+
constVal int64
10+
}
11+
12+
func (l *lconst_x) Execute(f *thread.Frame) {
13+
f.OperandStack().PushLong(l.constVal)
14+
}

instructions/constants/ldc.go

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

instructions/constants/ldc_x.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package constants
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/classp/classfile"
6+
"github.com/tinycedar/vanilla/runtime/thread"
7+
)
8+
9+
type ldc_x struct {
10+
opCode uint8
11+
index uint16
12+
}
13+
14+
func (i *ldc_x) Execute(f *thread.Frame) {
15+
if cp, ok := f.Method().Cp[i.index].(*classfile.ConstantStringInfo); ok {
16+
f.OperandStack().PushString(cp.String(f.Method().Cp))
17+
}
18+
//TODO
19+
}
20+
21+
func (i *ldc_x) String() string {
22+
return fmt.Sprintf("{opcode: 0x%x, ldc_x}", i.opCode)
23+
}

instructions/constants/xipush.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package constants
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type bipush struct {
9+
opCode uint8
10+
value int8
11+
}
12+
13+
type sipush struct {
14+
opCode uint8
15+
value int16
16+
}
17+
18+
func (i *bipush) Execute(f *thread.Frame) {
19+
f.OperandStack().PushInt(int32(i.value))
20+
}
21+
22+
func (i *sipush) Execute(f *thread.Frame) {
23+
f.OperandStack().PushInt(int32(i.value))
24+
}
25+
26+
func (i *bipush) String() string {
27+
return fmt.Sprintf("{opcode: 0x%x, bipush}", i.opCode)
28+
}
29+
30+
func (i *sipush) String() string {
31+
return fmt.Sprintf("{opcode: 0x%x, sipush}", i.opCode)
32+
}

runtime/thread/operand_stack.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package thread
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"math"
6+
)
47

58
type OperandStack struct {
69
size int
@@ -13,8 +16,8 @@ func newOperandStack(maxStack uint) *OperandStack {
1316
}
1417
}
1518

16-
func (s *OperandStack) PushInt(num int32) {
17-
s.slots[s.size].num = num
19+
func (s *OperandStack) PushInt(val int32) {
20+
s.slots[s.size].num = val
1821
s.size++
1922
}
2023

@@ -23,6 +26,39 @@ func (s *OperandStack) PopInt() int32 {
2326
return s.slots[s.size].num
2427
}
2528

29+
func (s *OperandStack) PushLong(val int64) {
30+
s.slots[s.size].num = int32(val >> 32) // high
31+
s.slots[s.size+1].num = int32(val) // low
32+
s.size += 2
33+
}
34+
35+
func (s *OperandStack) PopLong() int64 {
36+
s.size -= 2
37+
high := s.slots[s.size].num
38+
low := s.slots[s.size+1].num
39+
return int64(high)<<32 | int64(low)
40+
}
41+
42+
func (s *OperandStack) PushFloat(val float32) {
43+
s.slots[s.size].num = int32(math.Float32bits(val))
44+
s.size++
45+
}
46+
47+
func (s *OperandStack) PopFloat() float32 {
48+
s.size--
49+
return math.Float32frombits(uint32(s.slots[s.size].num))
50+
}
51+
52+
func (s *OperandStack) PushDouble(val float64) {
53+
s.slots[s.size].num = int32(math.Float64bits(val))
54+
s.size++
55+
}
56+
57+
func (s *OperandStack) PopDouble() float64 {
58+
s.size--
59+
return math.Float64frombits(uint64(s.slots[s.size].num))
60+
}
61+
2662
func (s *OperandStack) PushString(str string) {
2763
s.slots[s.size] = Slot{ref: &Object{str}}
2864
s.size++

0 commit comments

Comments
 (0)