📄 compilestack.java
字号:
/* $Id: CompileStack.java,v 1.7 2006/06/06 14:29:36 blackdrag Exp $ Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. Redistribution and use of this software and associated documentation ("Software"), with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain copyright statements and notices. Redistributions must also contain a copy of this document. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name "groovy" must not be used to endorse or promote products derived from this Software without prior written permission of The Codehaus. For written permission, please contact info@codehaus.org. 4. Products derived from this Software may not be called "groovy" nor may "groovy" appear in their names without prior written permission of The Codehaus. "groovy" is a registered trademark of The Codehaus. 5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/ THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.codehaus.groovy.classgen;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import org.codehaus.groovy.GroovyBugError;import org.codehaus.groovy.ast.ClassHelper;import org.codehaus.groovy.ast.ClassNode;import org.codehaus.groovy.ast.Parameter;import org.codehaus.groovy.ast.VariableScope;import org.objectweb.asm.Label;import org.objectweb.asm.MethodVisitor;import org.objectweb.asm.Opcodes;/** * This class is a helper for AsmClassGenerator. It manages * different aspects of the code of a code block like * handling labels, defining variables, and scopes. * After a MethodNode is visited clear should be called, for * initialization the method init should be used. * <p> * Some Notes: * <ul> * <li> every push method will require a later pop call * <li> method parameters may define a category 2 variable, so * don't ignore the type stored in the variable object * <li> the index of the variable may not be as assumed when * the variable is a parameter of a method because the * parameter may be used in a closure, so don't ignore * the stored variable index * </ul> * * * @see org.codehaus.groovy.classgen.AsmClassGenerator * @author Jochen Theodorou */public class CompileStack implements Opcodes { /** * @TODO remove optimization of this.foo -> this.@foo * */ // state flag private boolean clear=true; // current scope private VariableScope scope; // current label for continue private Label continueLabel; // current label for break private Label breakLabel; // current label for finally private Label finallyLabel; // available variables on stack private HashMap stackVariables = new HashMap(); // index of the last variable on stack private int currentVariableIndex = 1; // index for the next variable on stack private int nextVariableIndex = 1; // currently temporary variables in use private LinkedList temporaryVariables = new LinkedList(); // overall used variables for a method/constructor private LinkedList usedVariables = new LinkedList(); // map containing named labels of parenting blocks private HashMap superBlockNamedLabels = new HashMap(); // map containing named labels of current block private HashMap currentBlockNamedLabels = new HashMap(); private Label thisStartLabel, thisEndLabel; private MethodVisitor mv; private BytecodeHelper helper; // helper to handle different stack based variables private LinkedList stateStack = new LinkedList(); // defines the first variable index useable after // all parameters of a method private int localVariableOffset; // this is used to store the goals for a "break foo" call // in a loop where foo is a label. private HashMap namedLoopBreakLabel = new HashMap(); //this is used to store the goals for a "continue foo" call // in a loop where foo is a label. private HashMap namedLoopContinueLabel = new HashMap(); private String className; private class StateStackElement { VariableScope _scope; Label _continueLabel; Label _breakLabel; Label _finallyLabel; int _lastVariableIndex; int _nextVariableIndex; HashMap _stackVariables; LinkedList _temporaryVariables = new LinkedList(); LinkedList _usedVariables = new LinkedList(); HashMap _superBlockNamedLabels; HashMap _currentBlockNamedLabels; StateStackElement() { _scope = CompileStack.this.scope; _continueLabel = CompileStack.this.continueLabel; _breakLabel = CompileStack.this.breakLabel; _lastVariableIndex = CompileStack.this.currentVariableIndex; _stackVariables = CompileStack.this.stackVariables; _temporaryVariables = CompileStack.this.temporaryVariables; _nextVariableIndex = nextVariableIndex; _finallyLabel = finallyLabel; _superBlockNamedLabels = superBlockNamedLabels; _currentBlockNamedLabels = currentBlockNamedLabels; } } private void pushState() { stateStack.add(new StateStackElement()); stackVariables = new HashMap(stackVariables); } private void popState() { if (stateStack.size()==0) { throw new GroovyBugError("Tried to do a pop on the compile stack without push."); } StateStackElement element = (StateStackElement) stateStack.removeLast(); scope = element._scope; continueLabel = element._continueLabel; breakLabel = element._breakLabel; finallyLabel = element._finallyLabel; currentVariableIndex = element._lastVariableIndex; stackVariables = element._stackVariables; nextVariableIndex = element._nextVariableIndex; } public Label getContinueLabel() { return continueLabel; } public Label getBreakLabel() { return breakLabel; } public void removeVar(int tempIndex) { for (Iterator iter = temporaryVariables.iterator(); iter.hasNext();) { Variable element = (Variable) iter.next(); if (element.getIndex()==tempIndex) { iter.remove(); return; } } throw new GroovyBugError("CompileStack#removeVar: tried to remove a temporary variable with a non existent index"); } private void setEndLabels(){ Label endLabel = new Label(); mv.visitLabel(endLabel); for (Iterator iter = stackVariables.values().iterator(); iter.hasNext();) { Variable var = (Variable) iter.next(); var.setEndLabel(endLabel); } thisEndLabel = endLabel; } public void pop() { setEndLabels(); popState(); } public VariableScope getScope() { return scope; } /** * creates a temporary variable. * * @param var defines type and name * @param store defines if the toplevel argument of the stack should be stored * @return the index used for this temporary variable */ public int defineTemporaryVariable(org.codehaus.groovy.ast.Variable var, boolean store) { return defineTemporaryVariable(var.getName(), var.getType(),store); } public Variable getVariable(String variableName ) { return getVariable(variableName,true); } public Variable getVariable(String variableName, boolean mustExist) { if (variableName.equals("this")) return Variable.THIS_VARIABLE; if (variableName.equals("super")) return Variable.SUPER_VARIABLE; Variable v = (Variable) stackVariables.get(variableName); if (v==null && mustExist) throw new GroovyBugError("tried to get a variable with the name "+variableName+" as stack variable, but a variable with this name was not created"); return v; } /** * creates a temporary variable. * * @param name defines type and name * @param store defines if the toplevel argument of the stack should be stored * @return the index used for this temporary variable */ public int defineTemporaryVariable(String name,boolean store) { return defineTemporaryVariable(name, ClassHelper.DYNAMIC_TYPE,store); } /** * creates a temporary variable. * * @param name defines the name * @param type defines the type * @param store defines if the toplevel argument of the stack should be stored * @return the index used for this temporary variable */ public int defineTemporaryVariable(String name, ClassNode node, boolean store) { Variable answer = defineVar(name,node,false); temporaryVariables.add(answer); usedVariables.removeLast(); if (store) mv.visitVarInsn(ASTORE, currentVariableIndex); return answer.getIndex(); } private void resetVariableIndex(boolean isStatic) { if (!isStatic) { currentVariableIndex=1; nextVariableIndex=1; } else { currentVariableIndex=0; nextVariableIndex=0; } } /** * Clears the state of the class. This method should be called * after a MethodNode is visited. Note that a call to init will * fail if clear is not called before */ public void clear() { if (stateStack.size()>1) { int size = stateStack.size()-1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -