forked from JavaTX/JavaCompilerCore
232 lines
7.4 KiB
Java
Executable File
232 lines
7.4 KiB
Java
Executable File
package de.dhbwstuttgart.JvmDisassembler;
|
|
|
|
import java.util.*;
|
|
import java.io.*;
|
|
|
|
import de.dhbwstuttgart.typeinference.Menge;
|
|
|
|
//GenricsTest.java Testfile fuer jvmDisassembler
|
|
class jvmDisassembler {
|
|
|
|
static int makePosByte(int code) {
|
|
return (code & 0x7f | code & 0x80);
|
|
}
|
|
|
|
static int makePosShort(int code) {
|
|
return (code & 0x7fff | code & 0x8000);
|
|
}
|
|
|
|
static long makePosInt(long code) {
|
|
return (code & 0x7fffffff | code & 0x80000000);
|
|
}
|
|
|
|
static void parse_attribute(DataInputStream d, Menge<String> const_pool) throws IOException, Exception {
|
|
int attribut_count = d.readShort();
|
|
System.out.println("attribut_count = " + attribut_count);
|
|
if (attribut_count == 0) System.out.println("attributes = {}");
|
|
else {
|
|
for (int j = 0; j < attribut_count; j++) {
|
|
System.out.println("attribut[" + j + "] =\n{");
|
|
int attribut_name_index = d.readShort();
|
|
System.out.println("attribut_name_index = " + attribut_name_index
|
|
+ " //" + const_pool.elementAt(makePosShort(attribut_name_index)));
|
|
|
|
if (const_pool.elementAt(makePosShort(attribut_name_index)).equals("Code")) {
|
|
parse_code(d, const_pool);
|
|
int exception_table_length = d.readShort();
|
|
System.out.println("exception_table_length = " + exception_table_length);
|
|
if (exception_table_length == 0) System.out.println("exception_table = {}");
|
|
else throw new Exception("exceptiones not implemented");
|
|
|
|
parse_attribute(d, const_pool);
|
|
|
|
} else {
|
|
if ((const_pool.elementAt(makePosShort(attribut_name_index)).equals("Signature"))) {
|
|
long attribut_length = d.readInt();
|
|
System.out.println("attribut_length = " + attribut_length);
|
|
|
|
int sigdescriptor_index = d.readShort();
|
|
System.out.println("descriptor_index = " + sigdescriptor_index
|
|
+ " //" + const_pool.elementAt(makePosShort(sigdescriptor_index)));
|
|
} else {
|
|
long attribut_length = d.readInt();
|
|
System.out.println("attribut_length = " + attribut_length);
|
|
|
|
for(int ll = 0; ll < attribut_length; ll++) {
|
|
System.out.println(Integer.toHexString(makePosByte(d.readByte())));
|
|
}
|
|
System.out.println("}");
|
|
}
|
|
}
|
|
System.out.println("}");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
static void parse_code(DataInputStream d, Menge<String> const_pool) throws IOException {
|
|
long attribut_length = d.readInt();
|
|
System.out.println("attribut_length = " + attribut_length);
|
|
|
|
int max_stack = d.readShort();
|
|
System.out.println("max_stack = " + max_stack);
|
|
|
|
int max_locals = d.readShort();
|
|
System.out.println("max_locals = " + max_locals);
|
|
|
|
long code_length = makePosInt(d.readInt());
|
|
System.out.println("code_length = " + code_length);
|
|
System.out.println("code =\n{");
|
|
|
|
int code;
|
|
int codenr = 1;
|
|
for(; codenr <= code_length;) {
|
|
//CODE VERVOLLSTAENDIGEN PL 06-04-01
|
|
code = makePosByte(d.readByte());
|
|
//System.out.println("DEBUG: " + Integer.toHexString(code));
|
|
switch(code) {
|
|
case 0x4: System.out.println(" " + codenr + " iconst_1");
|
|
codenr++;
|
|
break;
|
|
case 0x2a: System.out.println(" " + codenr + " aload_0");
|
|
codenr++;
|
|
break;
|
|
case 0x2b: System.out.println(" " + codenr + " aload_1");
|
|
codenr++;
|
|
break;
|
|
case 0x2c: System.out.println(" " + codenr + " aload_2");
|
|
codenr++;
|
|
break;
|
|
case 0xac: System.out.println(" " + codenr + " ireturn");
|
|
codenr++;
|
|
break;
|
|
case 0xb0: System.out.println(" " + codenr + " areturn");
|
|
codenr++;
|
|
break;
|
|
case 0xb1: System.out.println(" " + codenr + " return");
|
|
codenr++;
|
|
break;
|
|
case 0xb7: System.out.println(" " + codenr + " invokespecial " +
|
|
d.readShort());
|
|
codenr+=3;
|
|
break;
|
|
default: System.out.println(" " + Integer.toHexString(code));
|
|
codenr++;
|
|
break;
|
|
}
|
|
}
|
|
System.out.println("}");
|
|
}
|
|
|
|
|
|
public static void main (String[] args) throws FileNotFoundException, IOException, Exception {
|
|
|
|
DataInputStream d = new DataInputStream(new FileInputStream(new File (args[0])));
|
|
|
|
int magic = d.readInt();
|
|
System.out.println("magic = 0x " + Integer.toHexString(magic));
|
|
|
|
int minor = d.readShort();
|
|
System.out.println("minor_version = " + minor);
|
|
int major = d.readShort();
|
|
System.out.println("major_version = " + major);
|
|
|
|
//CONSTANT POOL START
|
|
int constant_pool_count = makePosShort(d.readShort());
|
|
System.out.println("constant_pool_count = " + constant_pool_count);
|
|
|
|
Menge<String> const_pool = new Menge<String>();
|
|
const_pool.addElement(""); // Konstatenpool beginnt bei 1, Stelle 0 auffuellen
|
|
|
|
System.out.println("constant_pool =\n{");
|
|
short constant_pool_tag;
|
|
for (int i = 1; i < constant_pool_count; i++) {
|
|
constant_pool_tag = d.readByte();
|
|
switch(constant_pool_tag) {
|
|
//Tags vervollstaendigen PL 06-04-01
|
|
case 1: System.out.print(i + "| tag = CONSTANT_Utf8, length = ");
|
|
int length = makePosShort(d.readShort());
|
|
System.out.print(length + ", ");
|
|
byte[] bytes = new byte[length];
|
|
d.read(bytes, 0, length);
|
|
System.out.println(new String(bytes));
|
|
const_pool.addElement(new String(bytes));
|
|
break;
|
|
case 7: System.out.println(i + "| tag = CONSTANT_Class, name_index = " + d.readShort());
|
|
const_pool.addElement("");
|
|
break;
|
|
case 10: System.out.println(i + "| tag = CONSTANT_Methodref, class_index = " +
|
|
d.readShort() + ", name_and_type_index = " + d.readShort());
|
|
const_pool.addElement("");
|
|
break;
|
|
case 12:System.out.println(i + "| tag = CONSTANT_NameAndType, name_index = " +
|
|
d.readShort() + ", descriptor_index = " + d.readShort());
|
|
const_pool.addElement("");
|
|
break;
|
|
}
|
|
}
|
|
//CONSTANT POOL END
|
|
|
|
int access_flags = d.readShort();
|
|
System.out.println("access_flags = " + access_flags);
|
|
int this_class = d.readShort();
|
|
System.out.println("this_class = " + this_class);
|
|
int super_class = d.readShort();
|
|
System.out.println("super_class = " + super_class);
|
|
|
|
//INTERFACE START
|
|
int interface_count = d.readShort();
|
|
System.out.println("interface_count = " + interface_count);
|
|
if (interface_count == 0) System.out.println("interfaces ={}");
|
|
else
|
|
//Interface implementieren PL 06-04-01
|
|
throw new Exception("no interfaces implemented");
|
|
//INTERFACE END
|
|
|
|
//FIELD START
|
|
int field_count = d.readShort();
|
|
System.out.println("field_count = " + field_count);
|
|
if (field_count == 0) System.out.println("fields = {}");
|
|
else {
|
|
for(int o = 1; o <= field_count; o++) {
|
|
System.out.println("field[" + o + "] =\n{");
|
|
int faccess_flags = d.readShort();
|
|
System.out.println("access_flags = " + faccess_flags);
|
|
int fname_index = d.readShort();
|
|
System.out.println("name_index = " + fname_index);
|
|
int fdescriptor_index = d.readShort();
|
|
System.out.println("descriptor_index = " + fdescriptor_index);
|
|
|
|
parse_attribute(d, const_pool);
|
|
System.out.println("}");
|
|
}
|
|
}
|
|
//FIELD END
|
|
|
|
//METHOD START
|
|
int method_count = makePosShort(d.readShort());
|
|
System.out.println("method_count = " + method_count);
|
|
if (method_count == 0) System.out.println("methods ={}");
|
|
else {
|
|
for(int i = 1; i <= method_count; i++) {
|
|
System.out.println("method[" + i + "] =\n{");
|
|
int maccess_flags = d.readShort();
|
|
System.out.println("access_flags = " + maccess_flags);
|
|
int mname_index = d.readShort();
|
|
System.out.println("name_index = " + mname_index);
|
|
int descriptor_index = d.readShort();
|
|
System.out.println("descriptor_index = " + descriptor_index);
|
|
|
|
parse_attribute(d, const_pool);
|
|
|
|
System.out.println("}");
|
|
}
|
|
}
|
|
//METHOD END
|
|
|
|
//FILE ATTRIBUTES START
|
|
parse_attribute(d, const_pool);
|
|
//FILE ATTRIBUTES END
|
|
}
|
|
}
|