📄 jvmsimulator.java
字号:
/** Copyright (c) 1996-1999 Bill Venners. All Rights Reserved.** This Java source file is part of the Interactive Illustrations Web* Site, which is delivered in the applets directory of the CD-ROM* that accompanies the book "Inside the Java 2 Virtual Machine" by Bill* Venners, published by McGraw-Hill, 1999, ISBN: 0-07-135093-4. This* source file is provided for evaluation purposes only, but you can* redistribute it under certain conditions, described in the full* copyright notice below.** Full Copyright Notice:** All the web pages and Java applets delivered in the applets* directory of the CD-ROM, consisting of ".html," ".gif," ".class,"* and ".java" files, are copyrighted (c) 1996-1999 by Bill* Venners, and all rights are reserved. This material may be copied* and placed on any commercial or non-commercial web server on any* network (including the internet) provided that the following* guidelines are followed:** a. All the web pages and Java Applets (".html," ".gif," ".class,"* and ".java" files), including the source code, that are delivered* in the applets directory of the CD-ROM that* accompanies the book must be published together on the same web* site.** b. All the web pages and Java Applets (".html," ".gif," ".class,"* and ".java" files) must be published "as is" and may not be altered* in any way.** c. All use and access to this web site must be free, and no fees* can be charged to view these materials, unless express written* permission is obtained from Bill Venners.** d. The web pages and Java Applets may not be distributed on any* media, other than a web server on a network, and may not accompany* any book or publication.** BILL VENNERS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,* FITNESS FOR PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BILL VENNERS* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY A LICENSEE AS A* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS* DERIVATIVES.*/package COM.artima.jvmsim;import java.awt.*;import java.applet.*;/*** An applet that simulates the Java Virtual Machine executing a* sequence of bytecodes.** @author Bill Venners*/public abstract class JVMSimulator extends Applet implements Runnable { ColoredLabel explanationLabel = new ColoredLabel("", Label.CENTER, Color.lightGray); GrayButton stepButton = new GrayButton(StringTable.step); GrayButton resetButton = new GrayButton(StringTable.reset); GrayButton runButton = new GrayButton(StringTable.run); GrayButton stopButton = new GrayButton(StringTable.stop); MethodAreaPanel methodAreaPanel; LocalVarsPanel localVarsPanel; OperandStackPanel operandStackPanel; int pcRegister; int optopRegister; StackFrame currentFrame; Method currentMethod; int bytecodeViewSize; int[] theProgram; String[] bytecodeMnemonics; int maxStack; int maxLocals; ExceptionTableEntry[] exceptionTable; ConstantPoolEntry[] constantPool; boolean stoppedThreadWhenLeftPage = false; // If the "run" button is pushed, a separate thread will be invoked that // will cause the JVM to execute until the "stop" button is pressed. Thread runner; final int millisecondDelayBetweenSteps = 250; public void init() { theProgram = getTheProgram(); bytecodeMnemonics = getBytecodeMnemonics(); bytecodeViewSize = getBytecodeViewSize(); maxStack = getMaxStack(); maxLocals = getMaxLocals(); exceptionTable = getExceptionTable(); constantPool = getConstantPool(); methodAreaPanel = new MethodAreaPanel(bytecodeViewSize, theProgram, bytecodeMnemonics); localVarsPanel = new LocalVarsPanel(maxLocals); operandStackPanel = new OperandStackPanel(maxStack); // Set background and layout for the applet panel setBackground(getAppletBackgroundColor()); setLayout(new BorderLayout(5, 5)); // Initialize the title bar ColoredLabel title = new ColoredLabel(getAppletTitle(), Label.CENTER, getTitleColor()); title.setFont(new Font("Helvetica", Font.BOLD, 12)); // Initialize the control panel Panel controlPanel = new Panel(); controlPanel.setLayout(new BorderLayout(5, 5)); // Build the panel of four buttons Panel leftButtonPanel = new Panel(); leftButtonPanel.setLayout(new GridLayout(2,2,5,5)); leftButtonPanel.add(stepButton); resetButton.disable(); leftButtonPanel.add(runButton); leftButtonPanel.add(resetButton); leftButtonPanel.add(stopButton); stopButton.disable(); // Initialize the explanation label explanationLabel.setBackground(getExplanationLabelColor()); Font plainFont = new Font("TimesRoman", Font.ITALIC, 12); explanationLabel.setFont(plainFont); // Place the four button panel and explanation label onto // the control panel controlPanel.add("West", leftButtonPanel); controlPanel.add("Center", explanationLabel); // Initialize the panel that holds both the stack and // the method area (the twoParts panel) Panel twoParts = new Panel(); twoParts.setLayout(new GridLayout(1, 2, 5, 5)); Panel stackPanel = new Panel(); stackPanel.setLayout(new GridLayout(2, 1, 5, 5)); stackPanel.add(localVarsPanel); stackPanel.add(operandStackPanel); stackPanel.setBackground(getStackAreaColor()); methodAreaPanel.setBackground(getMethodAreaColor()); // Add stack and methodarea panels to the twoParts panel twoParts.add(stackPanel); twoParts.add(methodAreaPanel); // Place the title, controlPanel, and twoParts onto the applet // panel. add("North", title); add("South", controlPanel); add("Center", twoParts); // Place the bytecodes into a byte array. byte[] ba = new byte[theProgram.length]; for (int i = 0; i < ba.length; ++i) { ba[i] = (byte) theProgram[i]; } // Pass byte array to new StackFrame currentFrame = new StackFrame(maxStack, maxLocals); currentMethod = new Method(currentFrame, ba, exceptionTable, constantPool); resetState(); updateStateDisplay(); } // If they leave the page, stop a Run button press, then restart // automatically if they come back. In effect, leaving the page is like clicking // the Stop button. Returning to the page is like clicking the Run button. public void start() { if (runner == null && stoppedThreadWhenLeftPage) { stopButton.enable(); runButton.disable(); stepButton.disable(); resetButton.disable(); stoppedThreadWhenLeftPage = false; runner = new Thread(this); runner.start(); } } public void stop() { runButton.enable(); stepButton.enable(); resetButton.enable(); stopButton.disable(); if (runner != null) { // Synchronize on currentMethod to allow the thread // to complete execution of the current instruction before // killing it. synchronized (currentMethod) { runner.stop(); } runner = null; stoppedThreadWhenLeftPage = true; } } // Pushing the Reset button will cause resetState() to be executed, which will // reset all the data to its initial values. void resetState() { pcRegister = 0; optopRegister = 0; currentMethod.resetState(); } // updateStateDisplay writes the current state of the JVM to the UI. void updateStateDisplay() { methodAreaPanel.updateProgramCounter(pcRegister); updateExplanationText(); localVarsPanel.updateView(currentFrame.getLocalVars()); operandStackPanel.updateView(currentFrame.getOptop(), currentFrame.getOperandStack()); } public void run() { while (true) { try { pcRegister = currentMethod.executeNextInstruction(); } catch (BreakpointException be) { // On a breakpoint, kill the thread. runButton.enable(); stepButton.enable(); resetButton.enable(); stopButton.disable(); if (runner != null) { Thread runnerAboutToStop = runner; runner = null; runnerAboutToStop.stop(); } } updateStateDisplay(); try { Thread.sleep(millisecondDelayBetweenSteps); } catch (InterruptedException e) { } } } // updateExplanationText() prints out a line of text to the bottom of the // applet that describes the next opcode to execute. private void updateExplanationText() { int nextOpCode = theProgram[pcRegister]; switch (nextOpCode) { case OpCode.AALOAD: explanationLabel.setLabelText(StringTable.aaloadText); break; case OpCode.ALOAD_0: explanationLabel.setLabelText(StringTable.aload_0Text); break; case OpCode.ALOAD_1: explanationLabel.setLabelText(StringTable.aload_1Text); break; case OpCode.ALOAD_2: explanationLabel.setLabelText(StringTable.aload_2Text); break; case OpCode.ALOAD_3: explanationLabel.setLabelText(StringTable.aload_3Text); break; case OpCode.ASTORE: explanationLabel.setLabelText(StringTable.astoreText); break; case OpCode.ASTORE_0: explanationLabel.setLabelText(StringTable.astore_0Text); break; case OpCode.ASTORE_1: explanationLabel.setLabelText(StringTable.astore_1Text); break; case OpCode.ASTORE_2: explanationLabel.setLabelText(StringTable.astore_2Text); break; case OpCode.ASTORE_3: explanationLabel.setLabelText(StringTable.astore_3Text); break; case OpCode.BIPUSH: explanationLabel.setLabelText(StringTable.bipushText); break; case OpCode.BREAKPOINT: explanationLabel.setLabelText(StringTable.breakpointText); break; case OpCode.DCMPG: explanationLabel.setLabelText(StringTable.dcmpgText); break; case OpCode.DCONST_0: explanationLabel.setLabelText(StringTable.dconst_0Text); break; case OpCode.DCONST_1: explanationLabel.setLabelText(StringTable.dconst_1Text); break; case OpCode.DDIV: explanationLabel.setLabelText(StringTable.ddivText); break; case OpCode.DLOAD: explanationLabel.setLabelText(StringTable.dloadText); break; case OpCode.DLOAD_0: explanationLabel.setLabelText(StringTable.dload_0Text); break; case OpCode.DLOAD_2: explanationLabel.setLabelText(StringTable.dload_2Text); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -