Skip to content

Commit 3460fad

Browse files
committed
misc: add l2x/f2x/d2x instruction
1 parent 3ae6703 commit 3460fad

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

instructions/conversions/conversions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ func FetchInstruction(opCode uint8, reader *instructions.CodeReader) instruction
88
switch {
99
case opCode == 0x85 || opCode == 0x86 || opCode == 0x87:
1010
return &i2x{opCode}
11+
case opCode == 0x88 || opCode == 0x89 || opCode == 0x8a:
12+
return &l2x{opCode}
13+
case opCode == 0x8b || opCode == 0x8c || opCode == 0x8d:
14+
return &f2x{opCode}
15+
case opCode == 0x8e || opCode == 0x8f || opCode == 0x90:
16+
return &d2x{opCode}
1117
case opCode == 0x91 || opCode == 0x92 || opCode == 0x93:
1218
return &i2x{opCode}
1319
default:

instructions/conversions/d2x.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package conversions
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type d2x struct {
9+
opCode uint8
10+
}
11+
12+
func (i *d2x) Execute(f *thread.Frame) {
13+
stack := f.OperandStack()
14+
doubleValue := stack.PopLong()
15+
16+
switch i.opCode {
17+
case 0x8e:
18+
// d2i
19+
stack.PushInt(int32(doubleValue))
20+
case 0x8f:
21+
// d2l
22+
stack.PushLong(int64(doubleValue))
23+
case 0x90:
24+
// d2f
25+
stack.PushFloat(float32(doubleValue))
26+
}
27+
}
28+
29+
func (i *d2x) String() string {
30+
return fmt.Sprintf("{opcode: 0x%x, d2x}", i.opCode)
31+
}

instructions/conversions/f2x.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package conversions
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type f2x struct {
9+
opCode uint8
10+
}
11+
12+
func (i *f2x) Execute(f *thread.Frame) {
13+
stack := f.OperandStack()
14+
floatVal := stack.PopFloat()
15+
16+
switch i.opCode {
17+
case 0x8b:
18+
// f2i
19+
stack.PushInt(int32(floatVal))
20+
case 0x8c:
21+
// f2l
22+
stack.PushLong(int64(floatVal))
23+
case 0x8d:
24+
// f2d
25+
stack.PushDouble(float64(floatVal))
26+
}
27+
}
28+
29+
func (i *f2x) String() string {
30+
return fmt.Sprintf("{opcode: 0x%x, f2x}", i.opCode)
31+
}

instructions/conversions/l2x.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package conversions
2+
3+
import (
4+
"fmt"
5+
"github.com/tinycedar/vanilla/runtime/thread"
6+
)
7+
8+
type l2x struct {
9+
opCode uint8
10+
}
11+
12+
func (i *l2x) Execute(f *thread.Frame) {
13+
stack := f.OperandStack()
14+
longVal := stack.PopLong()
15+
16+
switch i.opCode {
17+
case 0x88:
18+
// l2i
19+
stack.PushInt(int32(longVal))
20+
case 0x89:
21+
// l2f
22+
stack.PushFloat(float32(longVal))
23+
case 0x8a:
24+
// l2d
25+
stack.PushDouble(float64(longVal))
26+
}
27+
}
28+
29+
func (i *l2x) String() string {
30+
return fmt.Sprintf("{opcode: 0x%x, l2x}", i.opCode)
31+
}

0 commit comments

Comments
 (0)