From 716f2c9ad261a368dd1be2a4e7638dd5e400a274 Mon Sep 17 00:00:00 2001 From: Imgaojp Date: Sat, 18 Feb 2017 16:34:50 +0800 Subject: [PATCH] Run-time Data Area --- src/com/gabongao/jvm/Jvm.java | 49 +++++++++ src/com/gabongao/jvm/rtda/Frame.java | 60 +++++++++++ src/com/gabongao/jvm/rtda/LocalVars.java | 93 +++++++++++++++++ src/com/gabongao/jvm/rtda/Object.java | 37 +++++++ src/com/gabongao/jvm/rtda/OperandStack.java | 105 ++++++++++++++++++++ src/com/gabongao/jvm/rtda/Slot.java | 54 ++++++++++ src/com/gabongao/jvm/rtda/Stack.java | 71 +++++++++++++ src/com/gabongao/jvm/rtda/Thread.java | 62 ++++++++++++ 8 files changed, 531 insertions(+) create mode 100644 src/com/gabongao/jvm/rtda/Frame.java create mode 100644 src/com/gabongao/jvm/rtda/LocalVars.java create mode 100644 src/com/gabongao/jvm/rtda/Object.java create mode 100644 src/com/gabongao/jvm/rtda/OperandStack.java create mode 100644 src/com/gabongao/jvm/rtda/Slot.java create mode 100644 src/com/gabongao/jvm/rtda/Stack.java create mode 100644 src/com/gabongao/jvm/rtda/Thread.java diff --git a/src/com/gabongao/jvm/Jvm.java b/src/com/gabongao/jvm/Jvm.java index 8671419..c8eaf5d 100644 --- a/src/com/gabongao/jvm/Jvm.java +++ b/src/com/gabongao/jvm/Jvm.java @@ -2,6 +2,9 @@ package com.gabongao.jvm; import com.gabongao.jvm.classfile.ClassFile; import com.gabongao.jvm.classpath.ClassPath; +import com.gabongao.jvm.rtda.Frame; +import com.gabongao.jvm.rtda.LocalVars; +import com.gabongao.jvm.rtda.OperandStack; import java.io.File; import java.util.Arrays; @@ -64,6 +67,8 @@ public class Jvm { } */ + + /* ch03 ClassPath cp = new ClassPath(); cp.parse(cmd.jreOption, cmd.cpOption); String className = cmd.className.replace(".", "/").concat(".class"); @@ -90,7 +95,51 @@ public class Jvm { for (int i = 0; i < classFile.getMethods().length; i++) { System.out.printf("\t\t%s\n", classFile.getMethods()[i].getName()); } + */ + + Frame frame = new Frame(100, 100); + testLocalVars(frame.getLocalVars()); + testOperandStack(frame.getOperandStack()); + } + + + private static void testLocalVars(LocalVars localVars) { + localVars.setInt(0, 100); + localVars.setInt(1, -100); + localVars.setInt(2, -100000); + localVars.setLong(3, 2997924580L); + localVars.setLong(5, -2997924580L); + localVars.setFloat(7, 3.1415926f); + localVars.setDouble(8, -2.7814897347028520974); + localVars.setRef(10, null); + System.out.println(localVars.getInt(0)); + System.out.println(localVars.getInt(1)); + System.out.println(localVars.getInt(2)); + System.out.println(localVars.getLong(3)); + System.out.println(localVars.getLong(5)); + System.out.println(localVars.getFloat(7)); + System.out.println(localVars.getDouble(8)); + System.out.println(localVars.getRef(10)); + } + + private static void testOperandStack(OperandStack operandStack) { + operandStack.pushInt(100); + operandStack.pushInt(-100); + operandStack.pushInt(-100000); + operandStack.pushLong(2997924580L); + operandStack.pushLong(-2997924580L); + operandStack.pushFloat(3.1415926f); + operandStack.pushDouble(-2.7814897347028520974); + operandStack.pushRef(null); + System.out.println(operandStack.popRef()); + System.out.println(operandStack.popDouble()); + System.out.println(operandStack.popFloat()); + System.out.println(operandStack.popLong()); + System.out.println(operandStack.popLong()); + System.out.println(operandStack.popInt()); + System.out.println(operandStack.popInt()); + System.out.println(operandStack.popInt()); } } diff --git a/src/com/gabongao/jvm/rtda/Frame.java b/src/com/gabongao/jvm/rtda/Frame.java new file mode 100644 index 0000000..bab7e66 --- /dev/null +++ b/src/com/gabongao/jvm/rtda/Frame.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class Frame { + private Frame lower; + private LocalVars localVars; + private OperandStack operandStack; + + public Frame(int maxLocals, int maxStack) { + localVars = new LocalVars(maxLocals); + operandStack = new OperandStack(maxStack); + } + + public LocalVars getLocalVars() { + return localVars; + } + + public OperandStack getOperandStack() { + return operandStack; + } + + Frame getLower() { + return lower; + } + + void setLower(Frame lower) { + this.lower = lower; + } +} diff --git a/src/com/gabongao/jvm/rtda/LocalVars.java b/src/com/gabongao/jvm/rtda/LocalVars.java new file mode 100644 index 0000000..11bf944 --- /dev/null +++ b/src/com/gabongao/jvm/rtda/LocalVars.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class LocalVars { + private Slot[] slots; + + LocalVars(int maxLocals) { + if (maxLocals > 0) { + slots = new Slot[maxLocals]; + } else { + throw new RuntimeException("LocalVars Count Error"); + } + } + + public void setInt(int index, int val) { + slots[index] = new Slot(); + slots[index].setNum(val); + } + + public int getInt(int index) { + return slots[index].getNum(); + } + + public void setFloat(int index, float val) { + slots[index] = new Slot(); + slots[index].setNum(Float.floatToRawIntBits(val)); + } + + public float getFloat(int index) { + return Float.intBitsToFloat(slots[index].getNum()); + } + + public void setLong(int index, long val) { + slots[index] = new Slot(); + slots[index].setNum((int) val); + slots[index + 1] = new Slot(); + slots[index + 1].setNum((int) (val >> 32)); + } + + public long getLong(int index) { + int low = slots[index].getNum(); + int high = slots[index + 1].getNum(); + return ((((long) high) << 32) & 0xffffffff00000000L) | (((long) low) & 0x00000000ffffffffL); + } + + public void setRef(int index, Object ref) { + slots[index] = new Slot(); + slots[index].setRef(ref); + } + + public Object getRef(int index) { + return slots[index].getRef(); + } + + public void setDouble(int index, double val) { + setLong(index, Double.doubleToRawLongBits(val)); + } + + public double getDouble(int index) { + return Double.longBitsToDouble(getLong(index)); + } +} diff --git a/src/com/gabongao/jvm/rtda/Object.java b/src/com/gabongao/jvm/rtda/Object.java new file mode 100644 index 0000000..0dd1b0c --- /dev/null +++ b/src/com/gabongao/jvm/rtda/Object.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class Object { + //TODO +} diff --git a/src/com/gabongao/jvm/rtda/OperandStack.java b/src/com/gabongao/jvm/rtda/OperandStack.java new file mode 100644 index 0000000..8b0225d --- /dev/null +++ b/src/com/gabongao/jvm/rtda/OperandStack.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class OperandStack { + private int size = 0; + private Slot[] slots; + + public OperandStack(int maxStacks) { + if (maxStacks > 0) { + slots = new Slot[maxStacks]; + } else { + throw new RuntimeException("Operands Count Error"); + } + } + + public void pushInt(int val) { + slots[size] = new Slot(); + slots[size].setNum(val); + size++; + } + + public int popInt() { + size--; + return slots[size].getNum(); + } + + public void pushFloat(float val) { + slots[size] = new Slot(); + slots[size].setNum(Float.floatToRawIntBits(val)); + size++; + } + + public float popFloat() { + size--; + return Float.intBitsToFloat(slots[size].getNum()); + } + + public void pushLong(long val) { + slots[size] = new Slot(); + slots[size].setNum((int) val); + slots[size + 1] = new Slot(); + slots[size + 1].setNum((int) (val >> 32)); + size += 2; + } + + public long popLong() { + size -= 2; + int high = slots[size + 1].getNum(); + int low = slots[size].getNum(); + return ((((long) high) << 32) & 0xffffffff00000000L) | (((long) low) & 0x00000000ffffffffL); + } + + public void pushRef(Object ref) { + slots[size] = new Slot(); + slots[size].setRef(ref); + size++; + } + + public Object popRef() { + size--; + Object ref = slots[size].getRef(); + slots[size].setRef(null); + return ref; + } + + + public void pushDouble(double val) { + pushLong(Double.doubleToRawLongBits(val)); + } + + public double popDouble() { + return Double.longBitsToDouble(popLong()); + } +} diff --git a/src/com/gabongao/jvm/rtda/Slot.java b/src/com/gabongao/jvm/rtda/Slot.java new file mode 100644 index 0000000..69f769b --- /dev/null +++ b/src/com/gabongao/jvm/rtda/Slot.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class Slot { + private int num; + private Object ref; + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + public Object getRef() { + return ref; + } + + public void setRef(Object ref) { + this.ref = ref; + } +} diff --git a/src/com/gabongao/jvm/rtda/Stack.java b/src/com/gabongao/jvm/rtda/Stack.java new file mode 100644 index 0000000..e265815 --- /dev/null +++ b/src/com/gabongao/jvm/rtda/Stack.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class Stack { + int maxSize, size = 0; + Frame _top; + + public Stack(int maxSize) { + this.maxSize = maxSize; + } + + public void push(Frame frame) { + if (size >= maxSize) { + throw new RuntimeException("StackOverFlowError"); + } + if (_top != null) { + frame.setLower(_top); + } + _top = frame; + size++; + } + + public Frame pop() { + if (_top == null) { + throw new RuntimeException("jvm stack is empty"); + } + Frame top = _top; + _top = top.getLower(); + top.setLower(null); + size--; + return top; + } + + public Frame top() { + if (_top == null) { + throw new RuntimeException("jvm stack is empty"); + } + return _top; + } +} diff --git a/src/com/gabongao/jvm/rtda/Thread.java b/src/com/gabongao/jvm/rtda/Thread.java new file mode 100644 index 0000000..9abfe0a --- /dev/null +++ b/src/com/gabongao/jvm/rtda/Thread.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.gabongao.jvm.rtda; + +/** + *         ┏┓   ┏┓+ + + *        ┏┛┻━━━┛┻┓ + + + *        ┃       ┃ + *        ┃   ━   ┃ ++ + + + + *        ████━████ ┃+ + *        ┃       ┃ + + *        ┃   ┻   ┃ + *        ┃       ┃ + + + *        ┗━┓   ┏━┛ + *          ┃   ┃ + *          ┃   ┃ + + + + + *          ┃   ┃    Code is far away from bug with the animal protecting + *          ┃   ┃ +     神兽保佑,代码无bug + *          ┃   ┃ + *          ┃   ┃  + + *          ┃    ┗━━━┓ + + + *          ┃        ┣┓ + *          ┃        ┏┛ + *          ┗┓┓┏━┳┓┏┛ + + + + + *           ┃┫┫ ┃┫┫ + *           ┗┻┛ ┗┻┛+ + + + + * Created by Imgaojp on 2017/2/18. + */ +public class Thread { + private int pc; + private Stack stack; + + public Thread() { + this.stack = new Stack(1024); + } + + public int getPc() { + return pc; + } + + public void setPc(int pc) { + this.pc = pc; + } + + public void pushFrame(Frame frame) { + stack.push(frame); + } + + public Frame popFrame() { + return stack.pop(); + } + + public Frame currentFrame() { + return stack.top(); + } +}