mirror of
https://github.com/FranzHaidnor/haidnorJVM.git
synced 2026-03-13 21:43:42 +08:00
@@ -526,10 +526,10 @@ public abstract class InstructionFactory {
|
||||
return new RET(codeStream);
|
||||
}
|
||||
case Const.TABLESWITCH -> {
|
||||
return new TABLESWITCH(codeStream); // TODO
|
||||
return new TABLESWITCH(codeStream);
|
||||
}
|
||||
case Const.LOOKUPSWITCH -> {
|
||||
return new LOOKUPSWITCH(codeStream); // TODO
|
||||
return new LOOKUPSWITCH(codeStream);
|
||||
}
|
||||
case Const.IRETURN -> {
|
||||
return new IRETURN(codeStream);
|
||||
|
||||
@@ -1,19 +1,47 @@
|
||||
package haidnor.jvm.instruction.control;
|
||||
|
||||
|
||||
import haidnor.jvm.instruction.Instruction;
|
||||
import haidnor.jvm.runtime.Frame;
|
||||
import haidnor.jvm.core.CodeStream;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@Slf4j
|
||||
public class LOOKUPSWITCH extends Instruction {
|
||||
|
||||
private final int count;//索引数量
|
||||
private final int jump_default;//默认跳转
|
||||
private final Map<Integer,Integer> map;//映射表
|
||||
|
||||
|
||||
public LOOKUPSWITCH(CodeStream codeStream) {
|
||||
super(codeStream);
|
||||
throw new UnsupportedOperationException("LOOKUPSWITCH");
|
||||
this.jump_default = codeStream.readInt(this);
|
||||
|
||||
this.count = codeStream.readInt(this);
|
||||
|
||||
this.map = new LinkedHashMap<>();
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
map.put(codeStream.readInt(this),codeStream.readInt(this));
|
||||
//创建映射表
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Frame frame) {
|
||||
throw new UnsupportedOperationException("LOOKUPSWITCH");
|
||||
int value = frame.popInt();//取出index
|
||||
|
||||
Integer offSet = map.getOrDefault(value, jump_default);//得到跳转值
|
||||
|
||||
super.setOffSet(offSet);
|
||||
|
||||
/* frame.push(new StackValue(Const.T_INT,offSet+super.getOffSet()));//把跳转值压入栈中*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,17 +3,37 @@ package haidnor.jvm.instruction.control;
|
||||
import haidnor.jvm.instruction.Instruction;
|
||||
import haidnor.jvm.runtime.Frame;
|
||||
import haidnor.jvm.core.CodeStream;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@Slf4j
|
||||
public class TABLESWITCH extends Instruction {
|
||||
|
||||
private final int low_case_value;//最小值
|
||||
private final int high_case_value;//最大值
|
||||
private final int jump_default;//默认跳转
|
||||
private final Map<Integer,Integer> map;//映射表
|
||||
|
||||
public TABLESWITCH(CodeStream codeStream) {
|
||||
super(codeStream);
|
||||
throw new UnsupportedOperationException("TABLESWITCH");
|
||||
this.jump_default = codeStream.readInt(this);
|
||||
this.low_case_value = codeStream.readInt(this);
|
||||
this.high_case_value = codeStream.readInt(this);
|
||||
this.map = new LinkedHashMap<>();
|
||||
for(int i=low_case_value;i<=high_case_value;i++)
|
||||
{
|
||||
map.put(i,codeStream.readInt(this));
|
||||
//创建映射表
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Frame frame) {
|
||||
throw new UnsupportedOperationException("TABLESWITCH");
|
||||
|
||||
int value = frame.popInt();//取出index
|
||||
Integer offSet = map.getOrDefault(value, jump_default);//得到跳转值
|
||||
super.setOffSet(offSet);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -158,6 +158,14 @@ public class TestJVM {
|
||||
public void test_Array() throws Exception {
|
||||
HaidnorJVM.testRun(Array.class);
|
||||
}
|
||||
@Test
|
||||
public void test_Switch_table() throws Exception {
|
||||
HaidnorJVM.testRun(demo_switch_table.class);
|
||||
}
|
||||
@Test
|
||||
public void test_Switch_lookup() throws Exception {
|
||||
HaidnorJVM.testRun(demo_switch_lookup.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_() throws Exception {
|
||||
|
||||
23
src/test/java/haidnor/jvm/test/demo/demo_switch_lookup.java
Normal file
23
src/test/java/haidnor/jvm/test/demo/demo_switch_lookup.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package haidnor.jvm.test.demo;
|
||||
|
||||
public class demo_switch_lookup {
|
||||
public static void main(String[] args) {
|
||||
//LOOKUPSWITCH的demo
|
||||
int i = 1;
|
||||
switch (i) {
|
||||
case 1:
|
||||
System.out.println("1");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("2");
|
||||
break;
|
||||
case 10:
|
||||
System.out.println("10");
|
||||
break;
|
||||
default:
|
||||
System.out.println("default");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
23
src/test/java/haidnor/jvm/test/demo/demo_switch_table.java
Normal file
23
src/test/java/haidnor/jvm/test/demo/demo_switch_table.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package haidnor.jvm.test.demo;
|
||||
|
||||
public class demo_switch_table {
|
||||
public static void main(String[] args) {
|
||||
int i = 3;
|
||||
switch (i) {
|
||||
case 1:
|
||||
System.out.println("1");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("2");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("3");
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("4");
|
||||
break;
|
||||
default:
|
||||
System.out.println("0");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user