Skip to content

Commit 9ecef10

Browse files
committed
updates
1 parent 24daa82 commit 9ecef10

File tree

10 files changed

+239
-6
lines changed

10 files changed

+239
-6
lines changed

hasnaer/java/bytecode/ClassFile.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,49 +256,66 @@ private String getInterface(int index) {
256256
}
257257

258258
private void loadConstantPool(int constant_pool_count) throws IOException {
259+
System.err.println("load constant_pool size: " + constant_pool_count);
259260
for (int i = 1; i < constant_pool_count; i++) {
260261
int tag = this.readUnsignedByte();
261262
CP_Info.Tag ctag = CP_Info.Tag.fromValue(tag);
262263

264+
System.err.print(i + " tag= " + tag + " ");
265+
263266
switch (ctag) {
264267
case CLASS:
268+
System.err.println("CLASS_Info");
265269
this.getConstant_pool().add(new Class_Info(ctag,
266270
this.readUnsignedShort()));
267271
break;
268272

269273
case FIELDREF:
270274
case METHODREF:
271275
case INTERFACEMETHODREF:
276+
System.err.println("FMIref_Info");
272277
this.getConstant_pool().add(new FMIref_Info(ctag,
273278
this.readUnsignedShort(), this.readUnsignedShort()));
274279
break;
275280

276281
case STRING:
282+
System.err.println("STRING_Info");
277283
this.getConstant_pool().add(new String_Info(ctag,
278284
this.readUnsignedShort()));
279285
break;
280286

281287
case INTEGER:
288+
System.err.println("INTEGER_Info");
282289
this.getConstant_pool().add(new Integer_Info(ctag,
283290
this.readInt()));
284291
break;
285292
case FLOAT:
293+
System.err.println("FLOAT_Info");
286294
this.getConstant_pool().add(new Float_Info(ctag,
287295
this.readFloat()));
288296
break;
289297
case LONG:
298+
System.err.println("LONG_Info");
290299
this.getConstant_pool().add(new Long_Info(ctag,
291300
this.readLong()));
301+
// add padding
302+
this.getConstant_pool().add(new Padding());
303+
i++;
292304
break;
293305
case DOUBLE:
306+
System.err.println("DOUBLE_Info");
294307
this.getConstant_pool().add(new Double_Info(ctag,
295308
this.readDouble()));
309+
this.getConstant_pool().add(new Padding());
310+
i++;
296311
break;
297312
case NAME_AND_TYPE:
313+
System.err.println("NameAndType_Info");
298314
this.getConstant_pool().add(new NameAndType_Info(ctag,
299315
this.readUnsignedShort(), this.readUnsignedShort()));
300316
break;
301317
case UTF8:
318+
System.err.println("UTF8_Info");
302319
this.getConstant_pool().add(new UTF8_Info(ctag, this.readUTF()));
303320
break;
304321
}

hasnaer/java/bytecode/Opcode.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public class Opcode {
2121
public static final int BIPUSH = 0x10;
2222

2323
public static final int LDC = 0x12;
24-
24+
public static final int LDC_W = 0x13;
25+
public static final int LDC2_W = 0x14;
26+
2527
public static final int ILOAD = 0x15;
2628
public static final int LLOAD = 0x16;
2729
public static final int FLOAD = 0x17;
@@ -53,7 +55,7 @@ public class Opcode {
5355
public static final int ALOAD_1 = 0x2b;
5456
public static final int ALOAD_2 = 0x2c;
5557
public static final int ALOAD_3 = 0x2d;
56-
58+
5759
public static final int ISTORE = 0x36;
5860
public static final int LSTORE = 0x37;
5961
public static final int FSTORE = 0x38;
@@ -85,7 +87,35 @@ public class Opcode {
8587
public static final int ASTORE_2 = 0x4d;
8688
public static final int ASTORE_3 = 0x4e;
8789

90+
8891
public static final int DUP = 0x59;
92+
93+
public static final int IADD = 0x60;
94+
public static final int LADD = 0x61;
95+
public static final int FADD = 0x62;
96+
public static final int DADD = 0x63;
97+
98+
public static final int ISUB = 0x64;
99+
public static final int LSUB = 0x65;
100+
public static final int FSUB = 0x66;
101+
public static final int DSUB = 0x67;
102+
103+
public static final int IMUL = 0x68;
104+
public static final int LMUL = 0x69;
105+
public static final int FMUL = 0x6a;
106+
public static final int DMUL = 0x6b;
107+
108+
public static final int IDIV = 0x6c;
109+
public static final int LDIV = 0x6d;
110+
public static final int FDIV = 0x6e;
111+
public static final int DDIV = 0x6f;
112+
113+
public static final int IREM = 0x70;
114+
public static final int LREM = 0x71;
115+
public static final int FREM = 0x72;
116+
public static final int DREM = 0x73;
117+
118+
89119
public static final int RETURN = 0xb1;
90120
public static final int GETSTATIC = 0xb2;
91121

hasnaer/java/bytecode/StatementBuilder.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import hasnaer.java.bytecode.attribute.LocalVariableTable;
55
import hasnaer.java.bytecode.cp.CP_Info;
66
import hasnaer.java.bytecode.cp.ConstantPool;
7+
import hasnaer.java.bytecode.cp.Double_Info;
78
import hasnaer.java.bytecode.cp.Float_Info;
89
import hasnaer.java.bytecode.cp.Integer_Info;
10+
import hasnaer.java.bytecode.cp.Long_Info;
911
import hasnaer.java.bytecode.cp.String_Info;
1012
import hasnaer.java.bytecode.nodes.AssignNode;
1113
import hasnaer.java.bytecode.nodes.FieldNode;
1214
import hasnaer.java.bytecode.nodes.InvocationNode;
1315
import hasnaer.java.bytecode.nodes.JVMNode;
1416
import hasnaer.java.bytecode.nodes.MethodNode;
17+
import hasnaer.java.bytecode.nodes.OperationNode;
1518
import hasnaer.java.bytecode.nodes.PrimitiveNode;
1619
import hasnaer.java.bytecode.nodes.ReferenceNode;
1720
import hasnaer.java.bytecode.nodes.ValueNode;
@@ -140,6 +143,12 @@ public List<JVMNode> build() {
140143
i = consumeLDC(i, code()[i + 1]);
141144
break;
142145

146+
case Opcode.LDC_W:
147+
case Opcode.LDC2_W:
148+
System.err.println("consumeLDC");
149+
i = consumeLDC(i + 1, unsignedShortAt(i + 1));
150+
break;
151+
143152
case Opcode.INVOKESPECIAL:
144153
System.err.println("consumeINVOKESPECIAL");
145154
i = consumeINVOKEMETHOD(i);
@@ -248,6 +257,47 @@ public List<JVMNode> build() {
248257
System.err.println("consumeLOADNULLCONST");
249258
i = consumeLOADNULLCONST(i);
250259
break;
260+
261+
case Opcode.IADD:
262+
case Opcode.FADD:
263+
case Opcode.DADD:
264+
case Opcode.LADD:
265+
System.err.println("consumeOPERATION");
266+
i = consumeOPERATION(i, OperationNode.Type.ADD);
267+
break;
268+
269+
case Opcode.ISUB:
270+
case Opcode.LSUB:
271+
case Opcode.DSUB:
272+
case Opcode.FSUB:
273+
System.err.println("consumeOPERATION");
274+
i = consumeOPERATION(i, OperationNode.Type.SUB);
275+
break;
276+
277+
case Opcode.IDIV:
278+
case Opcode.LDIV:
279+
case Opcode.DDIV:
280+
case Opcode.FDIV:
281+
System.err.println("consumeOPERATION");
282+
i = consumeOPERATION(i, OperationNode.Type.DIV);
283+
break;
284+
285+
case Opcode.IMUL:
286+
case Opcode.FMUL:
287+
case Opcode.DMUL:
288+
case Opcode.LMUL:
289+
System.err.println("consumeOPERATION");
290+
i = consumeOPERATION(i, OperationNode.Type.MUL);
291+
break;
292+
293+
case Opcode.IREM:
294+
case Opcode.FREM:
295+
case Opcode.DREM:
296+
case Opcode.LREM:
297+
System.err.println("consumeOPERATION");
298+
i = consumeOPERATION(i, OperationNode.Type.REM);
299+
break;
300+
251301
}
252302
}
253303

@@ -257,6 +307,15 @@ public List<JVMNode> build() {
257307
return statements;
258308
}
259309

310+
private int consumeOPERATION(int pos, OperationNode.Type type){
311+
ValueNode right = (ValueNode) stack.pop();
312+
ValueNode left = (ValueNode) stack.pop();
313+
314+
stack.push(new OperationNode(left, right, type));
315+
316+
return pos + 1;
317+
}
318+
260319
private int consumeLOADNULLCONST(int pos){
261320
stack.push(new ReferenceNode("null", "null"));
262321
return pos + 1;
@@ -357,8 +416,6 @@ private int consumeINVOKEMETHOD(int pos) {
357416

358417
private int consumeLDC(int pos, int index) {
359418

360-
361-
362419
CP_Info cp_info = pool().getCP_Info(index);
363420
System.err.println("index= " + index);
364421
System.err.println("tag= " + cp_info.getTag());
@@ -376,6 +433,14 @@ private int consumeLDC(int pos, int index) {
376433
String_Info str_info = (String_Info) cp_info;
377434
stack.push(new ReferenceNode("java.lang.String", escapeSTR(pool().getUTF8_Info(str_info.getString_Index()).getValue())));
378435
break;
436+
case DOUBLE:
437+
Double_Info double_info = (Double_Info) cp_info;
438+
stack.push(new PrimitiveNode(PrimitiveNode.Type.DOUBLE, double_info.getStringValue()));
439+
break;
440+
case LONG:
441+
Long_Info long_info = (Long_Info) cp_info;
442+
stack.push(new PrimitiveNode(PrimitiveNode.Type.LONG, long_info.getStringValue()));
443+
break;
379444
}
380445

381446
return pos + 2;
@@ -427,6 +492,7 @@ private int consumeSTOREVARIABLE(int pos, int index) {
427492

428493
if (value instanceof PrimitiveNode) {
429494
variable = new PrimitiveNode(type_name[0], type_name[1]);
495+
value = new PrimitiveNode(type_name[0], value.getName());
430496
} else {
431497
variable = new ReferenceNode(type_name[0], type_name[1]);
432498
}

hasnaer/java/bytecode/cp/CP_Info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public enum Tag {
2727
LONG(5),
2828
DOUBLE(6),
2929
NAME_AND_TYPE(12),
30-
UTF8(1);
30+
UTF8(1),
31+
PADDING(0);
3132

3233
public int value;
3334

hasnaer/java/bytecode/cp/Double_Info.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ public Double_Info(Tag ctag, double value) {
1212
super(ctag);
1313
this.value = value;
1414
}
15+
16+
public double getDoubleValue(){
17+
return value;
18+
}
1519

20+
public String getStringValue(){
21+
return Double.toString(value);
22+
}
1623
}

hasnaer/java/bytecode/cp/Long_Info.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ public Long_Info(Tag ctag, long value) {
1212
super(ctag);
1313
this.value = value;
1414
}
15+
16+
public long getLongValue(){
17+
return value;
18+
}
19+
20+
public String getStringValue() {
21+
return Long.toString(value);
22+
}
1523

1624
}

hasnaer/java/bytecode/cp/Padding.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package hasnaer.java.bytecode.cp;
2+
3+
/**
4+
*
5+
* @author hasnae rehioui
6+
*/
7+
public class Padding extends CP_Info {
8+
9+
public Padding(){
10+
super(CP_Info.Tag.PADDING);
11+
}
12+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package hasnaer.java.bytecode.nodes;
2+
3+
/**
4+
*
5+
* @author hasnae rehioui
6+
*/
7+
public class OperationNode extends ValueNode {
8+
9+
private ValueNode left;
10+
private ValueNode right;
11+
private Type op;
12+
13+
public OperationNode(ValueNode left, ValueNode right, Type op){
14+
super(null, null);
15+
this.left = left;
16+
this.right = right;
17+
this.op = op;
18+
}
19+
20+
public enum Type{
21+
ADD(" + "),
22+
SUB(" - "),
23+
MUL(" * "),
24+
DIV(" / "),
25+
REM(" % "),
26+
27+
NE(" != "),
28+
EQ(" == "),
29+
LT(" < "),
30+
GE(" >= "),
31+
GT(" > "),
32+
LE(" <= "),
33+
34+
AND(" & "),
35+
OR(" | "),
36+
XOR(" ^ "),
37+
38+
LSH(" >> "),
39+
RSH(" << ");
40+
String value;
41+
Type(String value){
42+
this.value = value;
43+
}
44+
}
45+
46+
@Override
47+
public String toString(){
48+
StringBuilder builder = new StringBuilder();
49+
builder.append(left.toString());
50+
builder.append(op.value);
51+
builder.append(right.toString());
52+
53+
return builder.toString();
54+
}
55+
}

hasnaer/java/bytecode/nodes/PrimitiveNode.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ public String value(){
3838

3939
@Override
4040
public String toString(){
41-
return name;
41+
Type t = Type.valueOf(type.trim().toUpperCase());
42+
43+
switch(t){
44+
case BOOLEAN:
45+
if(name.equals("0")){
46+
return "false";
47+
}
48+
else if(name.equals("1")){
49+
return "true";
50+
}
51+
52+
case CHAR:
53+
try{
54+
char c = (char) Integer.parseInt(name);
55+
return "'" + String.valueOf(c) + "'";
56+
} catch(Exception ex){
57+
58+
}
59+
60+
default:
61+
return name;
62+
63+
}
64+
65+
}
66+
67+
public static void main(String[] args){
68+
Type t = Type.valueOf("DOUBLE");
69+
70+
System.err.println(t);
4271
}
4372
}

0 commit comments

Comments
 (0)