compilestack.java
来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 653 行 · 第 1/2 页
JAVA
653 行
/*
$Id: CompileStack.java 4352 2006-12-13 15:58:48Z blackdrag $
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.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
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
* <li> the names of temporary variables can be ignored. The names
* are only used for debugging and do not conflict with each
* other or normal variables. For accessing the index of the
* variable must be used.
* </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;
// 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();
// list containing runnables representing a finally block
// such a block is created by synchronized or finally and
// must be called for break/continue/return
private LinkedList finallyBlocks = new LinkedList();
// a list of blocks already visiting.
private List visitedBlocks = new LinkedList();
private Label thisStartLabel, thisEndLabel;
// current class index
private int currentClassIndex , currentMetaClassIndex;
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;
LinkedList _finallyBlocks;
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;
_superBlockNamedLabels = superBlockNamedLabels;
_currentBlockNamedLabels = currentBlockNamedLabels;
_finallyBlocks = finallyBlocks;
}
}
private void pushState() {
stateStack.add(new StateStackElement());
stackVariables = new HashMap(stackVariables);
finallyBlocks = new LinkedList(finallyBlocks);
}
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;
currentVariableIndex = element._lastVariableIndex;
stackVariables = element._stackVariables;
nextVariableIndex = element._nextVariableIndex;
finallyBlocks = element._finallyBlocks;
}
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 node defines the node
* @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;
throw new GroovyBugError("the compile stack contains "+size+" more push instruction"+(size==1?"":"s")+" than pops.");
}
clear = true;
// br experiment with local var table so debuggers can retrieve variable names
if (true) {//AsmClassGenerator.CREATE_DEBUG_INFO) {
if (thisEndLabel==null) setEndLabels();
if (!scope.isInStaticContext()) {
// write "this"
mv.visitLocalVariable("this", className, null, thisStartLabel, thisEndLabel, 0);
}
for (Iterator iterator = usedVariables.iterator(); iterator.hasNext();) {
Variable v = (Variable) iterator.next();
String type = BytecodeHelper.getTypeDescription(v.getType());
Label start = v.getStartLabel();
Label end = v.getEndLabel();
mv.visitLocalVariable(v.getName(), type, null, start, end, v.getIndex());
}
}
pop();
stackVariables.clear();
usedVariables.clear();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?