Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.coding.mini_jvm.src.com.coderising.jvm.attr;

public class ConstantValue extends AttributeInfo {

private int constValueIndex;

public ConstantValue(int attrNameIndex, int attrLen) {
super(attrNameIndex, attrLen);
}


public int getConstValueIndex() {
return constValueIndex;
}
public void setConstValueIndex(int constValueIndex) {
this.constValueIndex = constValueIndex;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public abstract class ByteCodeCommand {

static {
codeMap.put("01", "aconst_null");

codeMap.put("A2", "if_icmp_ge");
codeMap.put("A4", "if_icmple");
codeMap.put("A7", "goto");
codeMap.put("BB", "new");
codeMap.put("37", "lstore");
codeMap.put("B7", "invokespecial");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CommandParser {

public static final String astore_1 = "4C";
public static final String if_icmp_ge = "A2";
public static final String if_icmp_gt = "A3";
public static final String if_icmple = "A4";
public static final String goto_no_condition = "A7";
public static final String iconst_0 = "03";
Expand Down Expand Up @@ -99,12 +100,33 @@ public static ByteCodeCommand[] parse(ClassFile clzFile, String codes) {
case aload_1:
case iload_1:
case iload_2:
case iload_3:
case istore_1:
case voidreturn:
case iconst_0:
case iconst_1:
case istore_2:
case iadd:
case ireturn:
case dup:
NoOperandCmd noOperandCmd = new NoOperandCmd(clzFile, operCode);
cmds.add(noOperandCmd);
break;
case if_icmp_ge:
case if_icmple:
case if_icmp_gt:
case goto_no_condition:
ComparisonCmd cmd1 = new ComparisonCmd(clzFile,operCode);
cmd1.setOprand1(cmdIter.next2CharAsInt());
cmd1.setOprand2(cmdIter.next2CharAsInt());
cmds.add(cmd1);
break;
case iinc:
IncrementCmd incrementCmd = new IncrementCmd(clzFile,operCode);
incrementCmd.setOprand1(cmdIter.next2CharAsInt());
incrementCmd.setOprand2(cmdIter.next2CharAsInt());
cmds.add(incrementCmd);
break;
default:
throw new RuntimeException("this oper [ " +operCode+ " ]not impl yet");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.coding.mini_jvm.src.com.coderising.jvm.cmd;


import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassFile;
import com.coding.mini_jvm.src.com.coderising.jvm.constant.ConstantPool;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.ExecutionResult;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.JavaObject;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.StackFrame;

public class ComparisonCmd extends TwoOperandCmd {

protected ComparisonCmd(ClassFile clzFile, String opCode) {
super(clzFile, opCode);

}


@Override
public void execute(StackFrame frame,ExecutionResult result) {

if(CommandParser.if_icmp_ge.equals(this.getOpCode())){
//注意次序
JavaObject jo2 = frame.getOprandStack().pop();
JavaObject jo1 = frame.getOprandStack().pop();

if(jo1.getIntValue() >= jo2.getIntValue()){

this.setJumpResult(result);

}

} else if(CommandParser.if_icmple.equals(this.getOpCode())){
//注意次序
JavaObject jo2 = frame.getOprandStack().pop();
JavaObject jo1 = frame.getOprandStack().pop();

if(jo1.getIntValue() <= jo2.getIntValue()){
this.setJumpResult(result);
}

} else if(CommandParser.goto_no_condition.equals(this.getOpCode())){
this.setJumpResult(result);

} else{
throw new RuntimeException(this.getOpCode() + "has not been implemented");
}




}

private int getOffsetFromStartCmd(){
//If the comparison succeeds, the unsigned branchbyte1 and branchbyte2
//are used to construct a signed 16-bit offset, where the offset is calculated
//to be (branchbyte1 << 8) | branchbyte2. Execution then proceeds at that
//offset from the address of the opcode of this if_icmp<cond> instruction


int index1 = this.getOprand1();
int index2 = this.getOprand2();
short offsetFromCurrent = (short)(index1 << 8 | index2);
return this.getOffset() + offsetFromCurrent ;
}
private void setJumpResult(ExecutionResult result){

int offsetFromStartCmd = this.getOffsetFromStartCmd();

result.setNextAction(ExecutionResult.JUMP);
result.setNextCmdOffset(offsetFromStartCmd);
}

@Override
public String toString() {
int index = this.getIndex();
String text = this.getReadableCodeText();
return this.getOffset()+":"+ this.getOpCode() + " "+text + " " + this.getOffsetFromStartCmd();
}

@Override
public String toString(ConstantPool pool) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.coding.mini_jvm.src.com.coderising.jvm.cmd;


import com.coding.mini_jvm.src.com.coderising.jvm.clz.ClassFile;
import com.coding.mini_jvm.src.com.coderising.jvm.constant.ConstantPool;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.ExecutionResult;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.Heap;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.JavaObject;
import com.coding.mini_jvm.src.com.coderising.jvm.engine.StackFrame;

public class IncrementCmd extends TwoOperandCmd {

public IncrementCmd(ClassFile clzFile, String opCode) {
super(clzFile, opCode);

}

@Override
public String toString() {

return this.getOffset()+":"+this.getOpCode()+ " " +this.getReadableCodeText();
}

@Override
public String toString(ConstantPool pool) {
return null;
}

@Override
public void execute(StackFrame frame, ExecutionResult result) {

int index = this.getOprand1();

int constValue = this.getOprand2();

int currentValue = frame.getLocalVariableValue(index).getIntValue();

JavaObject jo = Heap.getInstance().newInt(constValue+currentValue);

frame.setLocalVariableValue(index, jo);


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public void execute(StackFrame frame, ExecutionResult result) {
} else if (CommandParser.iload_2.equals(this.getOpCode())) {
JavaObject jo = frame.getLocalVariableValue(2);
frame.getOprandStack().push(jo);
} else if (CommandParser.iload_3.equals(this.getOpCode())) {
JavaObject jo = frame.getLocalVariableValue(3);
frame.getOprandStack().push(jo);
} else if (CommandParser.istore_1.equals(this.getOpCode())) {
JavaObject jo = frame.getOprandStack().pop();
frame.setLocalVariableValue(1, jo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.coding.mini_jvm.src.com.coderising.jvm.field;


import com.coding.mini_jvm.src.com.coderising.jvm.attr.AttributeInfo;
import com.coding.mini_jvm.src.com.coderising.jvm.attr.ConstantValue;
import com.coding.mini_jvm.src.com.coderising.jvm.constant.ConstantPool;
import com.coding.mini_jvm.src.com.coderising.jvm.loader.ByteCodeIterator;

public class Field {
private int accessFlag;
private int nameIndex;
private int descriptorIndex;


public class Field {
private int accessFlag;
private int nameIndex;
private int descriptorIndex;
private ConstantValue constValue;

private ConstantPool pool;

public Field(int accessFlag, int nameIndex, int descriptorIndex, ConstantPool pool) {
Expand All @@ -20,17 +24,33 @@ public Field(int accessFlag, int nameIndex, int descriptorIndex, ConstantPool po
}


public static Field parse(ConstantPool pool,ByteCodeIterator iter){
public static Field parse(ConstantPool pool, ByteCodeIterator iter) {
int accessFlag = iter.readTwoBytesToInt();
int nameIndex = iter.readTwoBytesToInt();
int descIndex = iter.readTwoBytesToInt();
int attributesCount = iter.readTwoBytesToInt();
if (attributesCount > 0)
throw new RuntimeException("attributeCount of field not impl");
return new Field(accessFlag, nameIndex, descIndex, pool);
Field f = new Field(accessFlag, nameIndex, descIndex, pool);
for( int i=1; i<= attributesCount; i++){
int attrNameIndex = iter.readTwoBytesToInt();
String attrName = pool.getUTF8String(attrNameIndex);

if(AttributeInfo.CONST_VALUE.equals(attrName)){
int attrLen = iter.readFourBytesToInt();
ConstantValue constValue = new ConstantValue(attrNameIndex, attrLen);
constValue.setConstValueIndex(iter.readTwoBytesToInt());
f.setConstValue(constValue);
} else{
throw new RuntimeException("the attribute " + attrName + " has not been implemented yet.");
}
}
return f;
}


public void setConstValue(ConstantValue constValue) {
this.constValue = constValue;
}

@Override
public String toString() {
return pool.getUTF8String(nameIndex)+":"+pool.getUTF8String(descriptorIndex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.coding.mini_jvm.src.com.coderising.jvm;
package com.coding.mini_jvm.src.com.coderising.jvm.test;

public class EmployeeV1 {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.coding.mini_jvm.src.com.coderising.jvm.test;

public class EmployeeV2 {

public final static String TEAM_NAME = "Dev Team";
private String name;
private int age;
public EmployeeV2(String name, int age) {
this.name = name;
this.age = age;
}

public void sayHello() {
System.out.println("Hello , this is class Employee ");
System.out.println(TEAM_NAME);
System.out.println(this.name);
}

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}



public void isYouth() {
if (age < 40) {
System.out.println("You're still young");
} else {
System.out.println("You're old");
}
}



public void testAdd() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
}


public static void main(String[] args) {
EmployeeV2 p = new EmployeeV2("Andy", 35);
p.sayHello();
p.isYouth();
p.testAdd();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.coding.mini_jvm.src.com.coderising.jvm.test;

public class Example {
public void disp(char c){
System.out.println(c);
}
public void disp(int c){
System.out.println(c );
}
public static void main(String args[]){
Example obj = new Example();
obj.disp('a');
obj.disp(5);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.coding.mini_jvm.src.com.coderising.jvm.test;

public class HourlyEmployee extends EmployeeV2 {

int hourlySalary;

public HourlyEmployee(String name,
int age, int hourlySalary) {
super(name, age);
this.hourlySalary = hourlySalary;
}

public void sayHello(){
System.out.println("Hello , this is Hourly Employee");
}
public static void main(String[] args){
EmployeeV2 e = new HourlyEmployee("Lisa", 20, 40);
e.sayHello();
}

public int getHourlySalary(){
return this.hourlySalary;
}



}
Loading