Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void setStackMapTable(StackMapTable t) {
public String toString(ConstantPool pool) {
StringBuffer buffer = new StringBuffer();
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("Code:").append(code).append("\n");
buffer.append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,9 @@ public Method getMethod(String methodName, String paramAndReturnType) {
return null;
}


public Method getMainMethod() {
for (Method m : methods) {
int nameIndex = m.getNameIndex();
int descIndex = m.getDescriptorIndex();
String name = this.getConstantPool().getUTF8String(nameIndex);
String desc = this.getConstantPool().getUTF8String(descIndex);
if (name.equals("main") && desc.equals("([Ljava/lang/String;)V")) {
return m;
}
}
return null;
return getMethod("main", "([Ljava/lang/String;)V");
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.github.HarryHook.coding2017.jvm.cmd;

import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
import com.github.HarryHook.coding2017.jvm.constant.ConstantInfo;
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;

import com.github.HarryHook.coding2017.jvm.engine.ExecutionResult;
import com.github.HarryHook.coding2017.jvm.engine.Heap;
import com.github.HarryHook.coding2017.jvm.engine.JavaObject;
import com.github.HarryHook.coding2017.jvm.engine.StackFrame;

public class BiPushCmd extends OneOperandCmd {

public BiPushCmd(ClassFile clzFile,String opCode) {
super(clzFile,opCode);

}

@Override
public String toString(ConstantPool pool) {

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


public BiPushCmd(ClassFile clzFile, String opCode) {
super(clzFile, opCode);

}

@Override
public String toString() {

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

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,123 +6,141 @@
import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
import com.github.HarryHook.coding2017.jvm.constant.ConstantInfo;
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;
import com.github.HarryHook.coding2017.jvm.engine.ExecutionResult;
import com.github.HarryHook.coding2017.jvm.engine.StackFrame;

public abstract class ByteCodeCommand {

public abstract class ByteCodeCommand {

String opCode;
ClassFile clzFile;
private int offset;

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");
}


String opCode;
ClassFile clzFile;
private int offset;


public static final String aconst_null = "01";
public static final String new_object = "BB";
public static final String lstore = "37";
public static final String invokespecial = "B7";
public static final String invokevirtual = "B6";
public static final String getfield = "B4";
public static final String putfield = "B5";
public static final String getstatic = "B2";
public static final String ldc = "12";
public static final String dup = "59";
public static final String bipush = "10";
public static final String aload_0 = "2A";
public static final String aload_1 = "2B";
public static final String aload_2 = "2C";
public static final String iload = "15";
public static final String iload_1 = "1B";
public static final String iload_2 = "1C";
public static final String iload_3 = "1D";
public static final String fload_3 = "25";

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

protected ClassFile getClassFile() {
return clzFile;
}

public int getOffset() {
return offset;
}
public static final String voidreturn = "B1";
public static final String ireturn = "AC";
public static final String freturn = "AE";

public void setOffset(int offset) {
this.offset = 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 static final String astore_1 = "4C";
public static final String if_icmp_ge = "A2";
public static final String if_icmple = "A4";
public static final String goto_no_condition = "A7";
public static final String iconst_0 = "03";
public static final String iconst_1 = "04";
public static final String istore_1 = "3C";
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>();

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){
return opCode;
}
return txt;
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;
}

public void setOffset(int offset) {
this.offset = 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 getReadableCodeText() {
String txt = codeMap.get(opCode);
if (txt == null) {
return opCode;
}

//public abstract void execute(StackFrame frame,FrameResult result);
return txt;
}

public abstract void execute(StackFrame frame, ExecutionResult result);
}
Loading