1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| package org.pt.jvm;
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Stack;
public class SimpleJvmDemo {
static class Slot { int value; boolean isReference;
Slot(int value, boolean isReference) { this.value = value; this.isReference = isReference; }
@Override public String toString() { return isReference ? "Ref(@" + value + ")" : String.valueOf(value); } }
static class ClassInfo { String name; Map<String, Integer> fieldOffsets = new HashMap<>(); int fieldCount = 0;
ClassInfo(String name) { this.name = name; }
void addField(String fieldName) { fieldOffsets.put(fieldName, fieldCount++); } int getFieldOffset(String fieldName) { return fieldOffsets.get(fieldName); } }
static class ClassArea { Map<String, ClassInfo> classes = new HashMap<>(); public void loadClass(ClassInfo ci) { classes.put(ci.name, ci); } public ClassInfo findClass(String name) { return classes.get(name); } }
static class MethodInfo { String name; List<String> instructions; int maxLocals; MethodInfo(String name, List<String> instructions, int maxLocals) { this.name = name; this.instructions = instructions; this.maxLocals = maxLocals; } }
static class MethodArea { Map<String, MethodInfo> methods = new HashMap<>(); public void loadMethod(MethodInfo method) { methods.put(method.name, method); } public MethodInfo findMethod(String name) { return methods.get(name); } }
static class ObjectInstance { ClassInfo classInfo; Slot[] fields;
ObjectInstance(ClassInfo classInfo) { this.classInfo = classInfo; this.fields = new Slot[classInfo.fieldCount]; } }
static class Heap { Map<Integer, ObjectInstance> memory = new HashMap<>(); int nextAddress = 1;
public int allocate(ObjectInstance obj) { int address = nextAddress; memory.put(address, obj); nextAddress++; System.out.println(" [堆] 分配对象 " + obj.classInfo.name + " at @" + address); return address; }
public ObjectInstance get(int address) { return memory.get(address); } }
static class StackFrame { Slot[] localVariables; Stack<Slot> operandStack; MethodInfo methodInfo; int pc = 0;
StackFrame(MethodInfo methodInfo) { this.methodInfo = methodInfo; this.localVariables = new Slot[methodInfo.maxLocals]; this.operandStack = new Stack<>(); } public String getCurrentInstruction() { return methodInfo.instructions.get(pc); } }
static class JvmStack { Stack<StackFrame> frames = new Stack<>(); public void push(StackFrame frame) { frames.push(frame); } public StackFrame pop() { return frames.pop(); } public StackFrame peek() { return frames.peek(); } public boolean isEmpty() { return frames.isEmpty(); } }
private JvmStack jvmStack; private MethodArea methodArea; private ClassArea classArea; private Heap heap;
public SimpleJvmDemo() { this.jvmStack = new JvmStack(); this.methodArea = new MethodArea(); this.classArea = new ClassArea(); this.heap = new Heap(); }
public void loadProgram() { ClassInfo personClass = new ClassInfo("Person"); personClass.addField("age"); classArea.loadClass(personClass);
MethodInfo mainMethod = new MethodInfo("main", Arrays.asList( "NEW Person", "STORE 0", "LOAD 0", "PUSH 30", "PUTFIELD age", "LOAD 0", "GETFIELD age", "STORE 1", "LOAD 1", "PRINT", "VRETURN" ), 2);
methodArea.loadMethod(mainMethod); System.out.println("✅ V2 程序加载完毕!(包含 'Person' 类和 'main' 方法)"); System.out.println("------------------------------------"); }
public void run() { System.out.println("🚀 V2 JVM 启动,开始执行 'main' 方法..."); StackFrame mainFrame = new StackFrame(methodArea.findMethod("main")); jvmStack.push(mainFrame);
while (!jvmStack.isEmpty()) { StackFrame currentFrame = jvmStack.peek(); String instruction = currentFrame.getCurrentInstruction(); currentFrame.pc++;
executeInstruction(instruction, currentFrame); } System.out.println("------------------------------------"); System.out.println("🏁 'main' 方法执行完毕,JVM 关闭。"); }
private void executeInstruction(String instruction, StackFrame currentFrame) { String[] parts = instruction.split(" "); String opcode = parts[0];
System.out.println(" [执行] -> " + instruction);
switch (opcode) { case "PUSH": currentFrame.operandStack.push(new Slot(Integer.parseInt(parts[1]), false)); break; case "STORE": currentFrame.localVariables[Integer.parseInt(parts[1])] = currentFrame.operandStack.pop(); break; case "LOAD": currentFrame.operandStack.push(currentFrame.localVariables[Integer.parseInt(parts[1])]); break; case "PRINT": Slot printVal = currentFrame.operandStack.pop(); System.out.println(" ***************"); System.out.println(" * [输出] " + printVal.value); System.out.println(" ***************"); break; case "VRETURN": System.out.println(" (弹出栈帧: " + currentFrame.methodInfo.name + ")"); jvmStack.pop(); break;
case "NEW": ClassInfo classInfo = classArea.findClass(parts[1]); ObjectInstance newObj = new ObjectInstance(classInfo); int address = heap.allocate(newObj); currentFrame.operandStack.push(new Slot(address, true)); break;
case "PUTFIELD": Slot value = currentFrame.operandStack.pop(); Slot objRef = currentFrame.operandStack.pop(); ObjectInstance objToSet = heap.get(objRef.value); int offsetSet = objToSet.classInfo.getFieldOffset(parts[1]); objToSet.fields[offsetSet] = value; System.out.println(" [堆] 设置 Ref@" + objRef.value + "." + parts[1] + " = " + value.value); break;
case "GETFIELD": Slot objRefGet = currentFrame.operandStack.pop(); ObjectInstance objToGet = heap.get(objRefGet.value); int offsetGet = objToGet.classInfo.getFieldOffset(parts[1]); Slot valueGet = objToGet.fields[offsetGet]; currentFrame.operandStack.push(valueGet); System.out.println(" [堆] 读取 Ref@" + objRefGet.value + "." + parts[1] + " (值=" + valueGet.value + ")"); break; }
System.out.println(" | OpStack: " + currentFrame.operandStack); System.out.println(" | Locals: " + Arrays.toString(currentFrame.localVariables)); }
public static void main(String[] args) { SimpleJvmDemo jvm = new SimpleJvmDemo(); jvm.loadProgram(); jvm.run(); } }
|