Skip to content

Commit

Permalink
上传第二次作业的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyliuxin committed Apr 9, 2017
1 parent 8eb5ba7 commit f462045
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 200 deletions.
65 changes: 2 additions & 63 deletions liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,76 +41,15 @@ public void setLocalVariableTable(LocalVariableTable t) {

public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){

int attrNameIndex = iter.nextU2ToInt();
int attrLen = iter.nextU4ToInt();
int maxStack = iter.nextU2ToInt();
int maxLocals = iter.nextU2ToInt();
int codeLen = iter.nextU4ToInt();

String code = iter.nextUxToHexString(codeLen);

System.out.println(code);

//ByteCodeCommand[] cmds = ByteCodeCommand.parse(clzFile,code);

CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen, maxStack,maxLocals,codeLen,code);

int exceptionTableLen = iter.nextU2ToInt();
//TODO 处理exception
if(exceptionTableLen>0){
String exTable = iter.nextUxToHexString(exceptionTableLen);
System.out.println("Encountered exception table , just ignore it :" + exTable);

}


int subAttrCount = iter.nextU2ToInt();

for(int x=1; x<=subAttrCount; x++){
int subAttrIndex = iter.nextU2ToInt();
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex);

//已经向前移动了U2, 现在退回去。
iter.back(2);
//line item table
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){

LineNumberTable t = LineNumberTable.parse(iter);
codeAttr.setLineNumberTable(t);
}
else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){
LocalVariableTable t = LocalVariableTable.parse(iter);
codeAttr.setLocalVariableTable(t);
}
else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){
StackMapTable t = StackMapTable.parse(iter);
codeAttr.setStackMapTable(t);
}
else{
throw new RuntimeException("Need code to process " + subAttrName);
}


}

return codeAttr;
return null;
}
private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

}

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("\n");
buffer.append(this.lineNumTable.toString());
buffer.append(this.localVarTable.toString(pool));
return buffer.toString();
}




Expand Down
27 changes: 2 additions & 25 deletions liuxin/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,9 @@ public LineNumberTable(int attrNameIndex, int attrLen) {

public static LineNumberTable parse(ByteCodeIterator iter){

int index = iter.nextU2ToInt();
int len = iter.nextU4ToInt();

LineNumberTable table = new LineNumberTable(index,len);

int itemLen = iter.nextU2ToInt();

for(int i=1; i<=itemLen; i++){
LineNumberItem item = new LineNumberItem();
item.setStartPC(iter.nextU2ToInt());
item.setLineNum(iter.nextU2ToInt());
table.addLineNumberItem(item);
}
return table;
return null;
}

public String toString(){
StringBuilder buffer = new StringBuilder();
buffer.append("Line Number Table:\n");
for(LineNumberItem item : items){
buffer.append("startPC:"+item.getStartPC()).append(",");
buffer.append("lineNum:"+item.getLineNum()).append("\n");
}
buffer.append("\n");
return buffer.toString();

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,11 @@ public LocalVariableTable(int attrNameIndex, int attrLen) {

public static LocalVariableTable parse(ByteCodeIterator iter){

int index = iter.nextU2ToInt();
int len = iter.nextU4ToInt();

LocalVariableTable table = new LocalVariableTable(index,len);

int itemLen = iter.nextU2ToInt();

for(int i=1; i<=itemLen; i++){
LocalVariableItem item = new LocalVariableItem();
item.setStartPC(iter.nextU2ToInt());
item.setLength(iter.nextU2ToInt());
item.setNameIndex(iter.nextU2ToInt());
item.setDescIndex(iter.nextU2ToInt());
item.setIndex(iter.nextU2ToInt());
table.addLocalVariableItem(item);
}
return table;
return null;
}
private void addLocalVariableItem(LocalVariableItem item) {
this.items.add(item);
}

public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
buffer.append("Local Variable Table:\n");
for(LocalVariableItem item : items){
buffer.append("startPC:"+item.getStartPC()).append(",");
buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(",");
buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(",");
buffer.append("slotIndex:"+ item.getIndex()).append("\n");
}
buffer.append("\n");
return buffer.toString();
}

}
20 changes: 1 addition & 19 deletions liuxin/mini-jvm/src/com/coderising/jvm/field/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,10 @@ public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool po



public String toString() {
String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue();

String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue();
return name +":"+ desc;
}

public static Field parse(ConstantPool pool,ByteCodeIterator iter){

int accessFlag = iter.nextU2ToInt();
int nameIndex = iter.nextU2ToInt();
int descIndex = iter.nextU2ToInt();
int attribCount = iter.nextU2ToInt();
System.out.println("field attribute count:"+ attribCount);

Field f = new Field(accessFlag, nameIndex, descIndex,pool);

if(attribCount > 0){
throw new RuntimeException("Field Attribute has not been implemented");
}

return f;
return null;
}

}
27 changes: 2 additions & 25 deletions liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public ClassFile parse(byte[] codes) {

parseInterfaces(iter);

parseFileds(clzFile, iter);

parseMethods(clzFile, iter);


return clzFile;
}

Expand Down Expand Up @@ -149,26 +146,6 @@ private void parseInterfaces(ByteCodeIterator iter) {
// TODO : 如果实现了interface, 这里需要解析
}

private void parseFileds(ClassFile clzFile, ByteCodeIterator iter) {
int fieldCount = iter.nextU2ToInt();
System.out.println("Field count:" + fieldCount);
for (int i = 1; i <= fieldCount; i++) {
Field f = Field.parse(clzFile.getConstantPool(), iter);
clzFile.addField(f);

}

}

private void parseMethods(ClassFile clzFile, ByteCodeIterator iter) {

int methodCount = iter.nextU2ToInt();

for (int i = 1; i <= methodCount; i++) {
Method m = Method.parse(clzFile, iter);
clzFile.addMethod(m);
}

}


}
41 changes: 2 additions & 39 deletions liuxin/mini-jvm/src/com/coderising/jvm/method/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,10 @@ public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorInd



public String toString() {

ConstantPool pool = this.clzFile.getConstantPool();
StringBuilder buffer = new StringBuilder();

String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue();

String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue();

buffer.append(name).append(":").append(desc).append("\n");

buffer.append(this.codeAttr.toString(pool));

return buffer.toString();
}


public static Method parse(ClassFile clzFile, ByteCodeIterator iter){
int accessFlag = iter.nextU2ToInt();
int nameIndex = iter.nextU2ToInt();
int descIndex = iter.nextU2ToInt();
int attribCount = iter.nextU2ToInt();


Method m = new Method(clzFile, accessFlag, nameIndex, descIndex);

for( int j=1; j<= attribCount; j++){

int attrNameIndex = iter.nextU2ToInt();
String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex);
iter.back(2);

if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){
CodeAttr codeAttr = CodeAttr.parse(clzFile, iter);
m.setCodeAttr(codeAttr);
} else{
throw new RuntimeException("only CODE attribute is implemented , please implement the "+ attrName);
}

}
//System.out.println("method:"+ m.toString(clzFile.getConstPool()));
return m ;
return null;

}
}

0 comments on commit f462045

Please sign in to comment.