Skip to content

Commit 280c488

Browse files
author
Rong Huang
authored
Merge pull request onlyliuxin#80 from HarryHook/master
add jvm_4 and Expression evaluates
2 parents 33ac13a + 18f0c6c commit 280c488

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1491
-86
lines changed

group02/727171008/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="lib" path="F:/jdom-2.0.6/jdom-2.0.6.jar"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>
Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.HarryHook.coding2017.jvm.attr;
22

33
import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
4+
import com.github.HarryHook.coding2017.jvm.cmd.ByteCodeCommand;
5+
import com.github.HarryHook.coding2017.jvm.cmd.CommandParser;
46
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;
57
import com.github.HarryHook.coding2017.jvm.loader.ByteCodeIterator;
68
import com.sun.org.apache.bcel.internal.generic.NEW;
@@ -15,22 +17,24 @@ public String getCode() {
1517
return code;
1618
}
1719

18-
// private ByteCodeCommand[] cmds ;
19-
// public ByteCodeCommand[] getCmds() {
20-
// return cmds;
21-
// }
20+
private ByteCodeCommand[] cmds;
21+
22+
public ByteCodeCommand[] getCmds() {
23+
return cmds;
24+
}
25+
2226
private LineNumberTable lineNumTable;
2327
private LocalVariableTable localVarTable;
2428
private StackMapTable stackMapTable;
2529

26-
public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,
27-
String code /* ByteCodeCommand[] cmds */) {
30+
public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen, String code,
31+
ByteCodeCommand[] cmds) {
2832
super(attrNameIndex, attrLen);
2933
this.maxStack = maxStack;
3034
this.maxLocals = maxLocals;
3135
this.codeLen = codeLen;
3236
this.code = code;
33-
// this.cmds = cmds;
37+
this.cmds = cmds;
3438
}
3539

3640
public void setLineNumberTable(LineNumberTable t) {
@@ -42,40 +46,44 @@ public void setLocalVariableTable(LocalVariableTable t) {
4246
}
4347

4448
public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter) {
45-
49+
4650
int attrNameIndex = iter.nextU2ToInt();
4751
int attrLen = iter.nextU4ToInt();
4852
int maxStack = iter.nextU2ToInt();
4953
int maxLocals = iter.nextU2ToInt();
5054
int codeLen = iter.nextU4ToInt();
51-
55+
5256
String code = iter.nextUxToHexString(codeLen);
5357
System.out.println(code);
54-
55-
CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code);
56-
58+
59+
ByteCodeCommand[] cmds = CommandParser.parse(clzFile, code);
60+
CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code, cmds);
61+
5762
int exceptionLength = iter.nextU2ToInt();
58-
if(exceptionLength > 0) {
63+
if (exceptionLength > 0) {
5964
String exceptionTable = iter.nextUxToHexString(exceptionLength);
6065
System.out.println("exception Table has not complemented" + exceptionTable);
6166
}
62-
//解析子属性
67+
// 解析子属性
6368
int subAttrCount = iter.nextU2ToInt();
64-
65-
for(int j=1; j<=subAttrCount; j++) {
66-
69+
70+
for (int j = 1; j <= subAttrCount; j++) {
71+
6772
int subAttrIndex = iter.nextU2ToInt();
6873
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex);
6974
iter.back(2);
70-
71-
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)) {
75+
76+
if (AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)) {
7277
LineNumberTable t = LineNumberTable.parse(iter);
7378
codeAttr.setLineNumberTable(t);
74-
75-
} else if(AttributeInfo.LOCAL_VAR_TABLE.equals(subAttrName)) {
79+
80+
} else if (AttributeInfo.LOCAL_VAR_TABLE.equals(subAttrName)) {
7681
LocalVariableTable t = LocalVariableTable.parse(iter);
7782
codeAttr.setLocalVariableTable(t);
78-
83+
84+
} else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)) {
85+
StackMapTable t = StackMapTable.parse(iter);
86+
codeAttr.setStackMapTable(t);
7987
} else {
8088
throw new RuntimeException("Need implement" + subAttrName);
8189
}
@@ -87,14 +95,17 @@ private void setStackMapTable(StackMapTable t) {
8795
this.stackMapTable = t;
8896

8997
}
98+
9099
public String toString(ConstantPool pool) {
91100
StringBuffer buffer = new StringBuffer();
92-
buffer.append("Code:").append(code).append("\n");
101+
for(int i=0;i<cmds.length;i++){
102+
buffer.append(cmds[i].toString(pool)).append("\n");
103+
}
104+
//buffer.append("Code:").append(code).append("\n");
93105
buffer.append("\n");
94106
buffer.append(this.lineNumTable.toString());
95-
//buffer.append(this.localVarTable.toString(pool));
107+
buffer.append(this.localVarTable.toString(pool));
96108
return buffer.toString();
97109
}
98110

99-
100111
}

group02/727171008/src/com/github/HarryHook/coding2017/jvm/attr/LineNumberTable.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,27 @@ public static LineNumberTable parse(ByteCodeIterator iter) {
4343
int attrLength = iter.nextU4ToInt();
4444
LineNumberTable table = new LineNumberTable(attrNameIndex, attrLength);
4545
int itemLength = iter.nextU2ToInt();
46-
47-
for(int i=1; i<=itemLength; i++) {
46+
47+
for (int i = 1; i <= itemLength; i++) {
4848
LineNumberItem item = new LineNumberItem();
4949
item.setStartPC(iter.nextU2ToInt());
5050
item.setLineNum(iter.nextU2ToInt());
5151
table.addLineNumberItem(item);
5252
}
53-
53+
5454
return table;
5555
}
5656

57+
public String toString() {
58+
StringBuilder buffer = new StringBuilder();
59+
buffer.append("Line Number Table:\n");
60+
for (LineNumberItem item : items) {
61+
buffer.append("startPC:" + item.getStartPC()).append(",");
62+
buffer.append("lineNum:" + item.getLineNum()).append("\n");
63+
}
64+
buffer.append("\n");
65+
return buffer.toString();
66+
67+
}
68+
5769
}

group02/727171008/src/com/github/HarryHook/coding2017/jvm/attr/LocalVariableTable.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,16 @@ public static LocalVariableTable parse(ByteCodeIterator iter) {
3636
private void addLocalVariableItem(LocalVariableItem item) {
3737
this.items.add(item);
3838
}
39-
39+
public String toString(ConstantPool pool){
40+
StringBuilder buffer = new StringBuilder();
41+
buffer.append("Local Variable Table:\n");
42+
for(LocalVariableItem item : items){
43+
buffer.append("startPC:"+item.getStartPC()).append(",");
44+
buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(",");
45+
buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(",");
46+
buffer.append("slotIndex:"+ item.getIndex()).append("\n");
47+
}
48+
buffer.append("\n");
49+
return buffer.toString();
50+
}
4051
}

group02/727171008/src/com/github/HarryHook/coding2017/jvm/clz/ClassFile.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.HarryHook.coding2017.jvm.clz;
22

3-
43
import java.util.ArrayList;
54
import java.util.List;
65

@@ -18,14 +17,16 @@ public class ClassFile {
1817
private ClassIndex clzIndex;
1918
private ConstantPool pool;
2019
private List<Field> fields = new ArrayList<Field>();
21-
private List<Method> methods = new ArrayList<Method>();;
20+
private List<Method> methods = new ArrayList<Method>();
2221

2322
public ClassIndex getClzIndex() {
2423
return clzIndex;
2524
}
25+
2626
public void setClzIndex(ClassIndex clzIndex) {
2727
this.clzIndex = clzIndex;
2828
}
29+
2930
public AccessFlag getAccessFlag() {
3031
return accessFlag;
3132
}
@@ -72,32 +73,61 @@ public void print() {
7273

7374
}
7475

75-
private String getClassName() {
76+
public String getClassName() {
7677
int thisClassIndex = clzIndex.getThisClassIndex();
7778
ClassInfo thisClass = (ClassInfo) this.getConstantPool().getConstantInfo(thisClassIndex);
7879
return thisClass.getClassName();
7980
}
8081

81-
private String getSuperClassName() {
82+
public String getSuperClassName() {
8283
ClassInfo superClass = (ClassInfo) this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
8384
return superClass.getClassName();
8485
}
85-
86+
8687
public void addField(Field f) {
8788
fields.add(f);
8889
}
89-
90+
9091
public List<Field> getFields() {
9192
return fields;
9293
}
93-
94+
9495
public void addMethod(Method m) {
9596
methods.add(m);
96-
97+
9798
}
99+
98100
public List<Method> getMethods() {
99101
return methods;
100102
}
101103

104+
public Method getMethod(String methodName, String paramAndReturnType) {
105+
106+
for (Method m : methods) {
107+
108+
int nameIndex = m.getNameIndex();
109+
int descriptionIndex = m.getDescriptorIndex();
110+
111+
String name = this.getConstantPool().getUTF8String(nameIndex);
112+
String desc = this.getConstantPool().getUTF8String(descriptionIndex);
113+
if (name.equals(methodName) && desc.equals(paramAndReturnType)) {
114+
return m;
115+
}
116+
}
117+
return null;
118+
}
119+
120+
public Method getMainMethod() {
121+
for (Method m : methods) {
122+
int nameIndex = m.getNameIndex();
123+
int descIndex = m.getDescriptorIndex();
124+
String name = this.getConstantPool().getUTF8String(nameIndex);
125+
String desc = this.getConstantPool().getUTF8String(descIndex);
126+
if (name.equals("main") && desc.equals("([Ljava/lang/String;)V")) {
127+
return m;
128+
}
129+
}
130+
return null;
131+
}
102132

103133
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.HarryHook.coding2017.jvm.cmd;
2+
3+
import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
4+
import com.github.HarryHook.coding2017.jvm.constant.ConstantInfo;
5+
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;
6+
7+
8+
public class BiPushCmd extends OneOperandCmd {
9+
10+
public BiPushCmd(ClassFile clzFile,String opCode) {
11+
super(clzFile,opCode);
12+
13+
}
14+
15+
@Override
16+
public String toString(ConstantPool pool) {
17+
18+
return this.getOffset()+": "+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
19+
}
20+
21+
22+
23+
}

0 commit comments

Comments
 (0)