-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookupswitch.go
38 lines (29 loc) · 1.13 KB
/
lookupswitch.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
package control_instructions
import (
"github.com/Frederick-S/jvmgo/instructions/base_instructions"
"github.com/Frederick-S/jvmgo/runtime_data_area"
)
// lookupswitch
// Access jump table by key match and jump
type LookupSwitch struct {
defaultOffset int32
numberOfMatchOffsetPairss int32
matchOffsetPairss []int32
}
func (lookupSwitch *LookupSwitch) FetchOperands(bytecodeReader *base_instructions.BytecodeReader) {
bytecodeReader.SkipPadding()
lookupSwitch.defaultOffset = bytecodeReader.ReadInt32()
lookupSwitch.numberOfMatchOffsetPairss = bytecodeReader.ReadInt32()
lookupSwitch.matchOffsetPairss = bytecodeReader.ReadInt32Table(lookupSwitch.numberOfMatchOffsetPairss * 2)
}
func (lookupSwitch *LookupSwitch) Execute(frame *runtime_data_area.Frame) {
key := frame.GetOperandStack().PopIntegerValue()
for i := int32(0); i < lookupSwitch.numberOfMatchOffsetPairss*2; i += 2 {
if lookupSwitch.matchOffsetPairss[i] == key {
offset := lookupSwitch.matchOffsetPairss[i+1]
base_instructions.JumpToBranch(frame, int(offset))
return
}
}
base_instructions.JumpToBranch(frame, int(lookupSwitch.defaultOffset))
}