Skip to content

Commit

Permalink
Eighth week homework finished
Browse files Browse the repository at this point in the history
  • Loading branch information
DonaldY committed Apr 29, 2017
1 parent 44ecabb commit 0880c4d
Show file tree
Hide file tree
Showing 20 changed files with 1,373 additions and 232 deletions.
2 changes: 1 addition & 1 deletion group24/448641125/src/com/donaldy/jvm/attr/CodeAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String toString(ConstantPool pool) {
StringBuilder buffer = new StringBuilder();
//buffer.append("Code:").append(code).append("\n");
for(int i=0;i<cmds.length;i++){
buffer.append(cmds[i].toString(pool)).append("\n");
buffer.append(cmds[i].toString()).append("\n");
}
buffer.append("\n");
buffer.append(this.lineNumTable.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public static LineNumberTable parse(ByteCodeIterator iter){
int lnTableLen = iter.nextU2ToInt();
LineNumberTable lnTable = new LineNumberTable(attrNameIndex, attributeLen);

System.out.println("LineNumberTable.lnTableLen : " + lnTableLen);

for (int i = 0; i < lnTableLen; ++i) {
LineNumberItem lnItem = new LineNumberItem();

Expand Down
11 changes: 5 additions & 6 deletions group24/448641125/src/com/donaldy/jvm/cmd/BiPushCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ public BiPushCmd(ClassFile clzFile,String opCode) {
}

@Override
public String toString(ConstantPool pool) {
public String toString() {

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

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

public void execute(StackFrame frame,ExecutionResult result){
int value = this.getOperand();
JavaObject jo = Heap.getInstance().newInt(value);
frame.getOprandStack().push(jo);

}


}
72 changes: 29 additions & 43 deletions group24/448641125/src/com/donaldy/jvm/cmd/ByteCodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import com.donaldy.jvm.engine.StackFrame;


public abstract class ByteCodeCommand {
public abstract class ByteCodeCommand {

String opCode;
ClassFile clzFile;
ClassFile clzFile;
private int offset;

private static Map<String,String> codeMap = new HashMap<String,String>();

public static final String aconst_null = "01";
public static final String new_object = "BB";
Expand Down Expand Up @@ -52,73 +50,73 @@ public abstract class ByteCodeCommand {
public static final String istore_2 = "3D";
public static final String iadd = "60";
public static final String iinc = "84";

private static Map<String,String> codeMap = new HashMap<String,String>();

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

codeMap.put("BB", "new");
codeMap.put("37", "lstore");
codeMap.put("B7", "invokespecial");
codeMap.put("B6", "invokevirtual");
codeMap.put("B4", "getfield");
codeMap.put("B5", "putfield");
codeMap.put("B2", "getstatic");

codeMap.put("2A", "aload_0");
codeMap.put("2B", "aload_1");
codeMap.put("2C", "aload_2");

codeMap.put("10", "bipush");
codeMap.put("15", "iload");
codeMap.put("1A", "iload_0");
codeMap.put("1B", "iload_1");
codeMap.put("1C", "iload_2");
codeMap.put("1D", "iload_3");

codeMap.put("25", "fload_3");

codeMap.put("1E", "lload_0");

codeMap.put("24", "fload_2");
codeMap.put("4C", "astore_1");

codeMap.put("A2", "if_icmp_ge");
codeMap.put("A4", "if_icmple");

codeMap.put("A7", "goto");

codeMap.put("B1", "return");
codeMap.put("AC", "ireturn");
codeMap.put("AE", "freturn");

codeMap.put("03", "iconst_0");
codeMap.put("04", "iconst_1");

codeMap.put("3C", "istore_1");
codeMap.put("3D", "istore_2");

codeMap.put("59", "dup");

codeMap.put("60", "iadd");
codeMap.put("84", "iinc");

codeMap.put("12", "ldc");
}








protected ByteCodeCommand(ClassFile clzFile, String opCode){
this.clzFile = clzFile;
this.opCode = opCode;
}

protected ClassFile getClassFile() {
return clzFile;
}

public int getOffset() {
return offset;
}
Expand All @@ -129,31 +127,19 @@ public void setOffset(int offset) {
protected ConstantInfo getConstantInfo(int index){
return this.getClassFile().getConstantPool().getConstantInfo(index);
}

protected ConstantPool getConstantPool(){
return this.getClassFile().getConstantPool();
}



public String getOpCode() {
return opCode;
}

public abstract int getLength();




public String toString(){

StringBuffer buffer = new StringBuffer();
buffer.append(this.opCode);

return buffer.toString();
}
public abstract String toString(ConstantPool pool);


public String getReadableCodeText(){
String txt = codeMap.get(opCode);
if(txt == null){
Expand All @@ -163,4 +149,4 @@ public String getReadableCodeText(){
}

public abstract void execute(StackFrame frame,ExecutionResult result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static ByteCodeCommand[] parse(ClassFile clzFile, String codes) {

cmds.add(cmd);
} else if (invokevirtual.equals(opCode)) {
InvokeSpecialCmd cmd = new InvokeSpecialCmd(clzFile, opCode);
InvokeVirtualCmd cmd = new InvokeVirtualCmd(clzFile, opCode);

cmd.setOprand1(iter.next2CharAsInt());
cmd.setOprand2(iter.next2CharAsInt());
Expand Down
14 changes: 8 additions & 6 deletions group24/448641125/src/com/donaldy/jvm/cmd/GetFieldCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ public GetFieldCmd(ClassFile clzFile,String opCode) {
}

@Override
public String toString(ConstantPool pool) {
public String toString() {

return super.getOperandAsField(pool);
return super.getOperandAsField();
}

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

public void execute(StackFrame frame,ExecutionResult result) {
FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex());
String fieldName = fieldRef.getFieldName();
JavaObject jo = frame.getOprandStack().pop();
JavaObject fieldValue = jo.getFieldValue(fieldName);

frame.getOprandStack().push(fieldValue);




}


Expand Down
20 changes: 9 additions & 11 deletions group24/448641125/src/com/donaldy/jvm/cmd/GetStaticFieldCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@ public GetStaticFieldCmd(ClassFile clzFile,String opCode) {
}

@Override
public String toString(ConstantPool pool) {
public String toString() {

return super.getOperandAsField(pool);
return super.getOperandAsField();
}

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


@Override
public void execute(StackFrame frame,ExecutionResult result) {
FieldRefInfo info = (FieldRefInfo)this.getConstantInfo(this.getIndex());
String className = info.getClassName();
String fieldName = info.getFieldName();
String fieldType = info.getFieldType();

if("java/lang/System".equals(className)
&& "out".equals(fieldName)
&& "Ljava/io/PrintStream;".equals(fieldType)){
if("java/lang/System".equals(className)
&& "out".equals(fieldName)
&& "Ljava/io/PrintStream;".equals(fieldType)){
JavaObject jo = Heap.getInstance().newObject(className);
frame.getOprandStack().push(jo);
}
//TODO 处理非System.out的情况
}

}
24 changes: 13 additions & 11 deletions group24/448641125/src/com/donaldy/jvm/cmd/InvokeSpecialCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@ public InvokeSpecialCmd(ClassFile clzFile,String opCode) {
}

@Override
public String toString(ConstantPool pool) {
public String toString() {

return super.getOperandAsMethod(pool);
return super.getOperandAsMethod();
}

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

MethodRefInfo methodRefInfo = (MethodRefInfo)this.getConstantInfo(this.getIndex());

// 我们不用实现jang.lang.Object 的init方法
if(methodRefInfo.getClassName().equals("java/lang/Object")
&& methodRefInfo.getMethodName().equals("<init>")){
if(methodRefInfo.getClassName().equals("java/lang/Object")
&& methodRefInfo.getMethodName().equals("<init>")){
return ;

}
Method nextMethod = MethodArea.getInstance().getMethod(methodRefInfo);


result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME);
result.setNextMethod(nextMethod);




}


}
Loading

0 comments on commit 0880c4d

Please sign in to comment.