⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/***
 * ASM: a very small and fast Java bytecode manipulation framework
 * Copyright (c) 2000-2005 INRIA, France Telecom
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 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. Neither the name of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.drools.asm.tree.analysis;

import java.util.ArrayList;
import java.util.List;

import org.drools.asm.Opcodes;
import org.drools.asm.Type;
import org.drools.asm.tree.AbstractInsnNode;
import org.drools.asm.tree.IincInsnNode;
import org.drools.asm.tree.MethodInsnNode;
import org.drools.asm.tree.MultiANewArrayInsnNode;
import org.drools.asm.tree.VarInsnNode;

/**
 * A symbolic execution stack frame. A stack frame contains a set of local
 * variable slots, and an operand stack. Warning: long and double values are
 * represented by <i>two</i> slots in local variables, and by <i>one</i> slot
 * in the operand stack.
 * 
 * @author Eric Bruneton
 */
public class Frame {

    /**
     * The local variables and operand stack of this frame.
     */
    private Value[] values;

    /**
     * The number of local variables of this frame.
     */
    private int     locals;

    /**
     * The number of elements in the operand stack.
     */
    private int     top;

    /**
     * Constructs a new frame with the given size.
     * 
     * @param nLocals the maximum number of local variables of the frame.
     * @param nStack the maximum stack size of the frame.
     */
    public Frame(final int nLocals,
                 final int nStack) {
        this.values = new Value[nLocals + nStack];
        this.locals = nLocals;
    }

    /**
     * Constructs a new frame that is identical to the given frame.
     * 
     * @param src a frame.
     */
    public Frame(final Frame src) {
        this( src.locals,
              src.values.length - src.locals );
        init( src );
    }

    /**
     * Copies the state of the given frame into this frame.
     * 
     * @param src a frame.
     * @return this frame.
     */
    public Frame init(final Frame src) {
        System.arraycopy( src.values,
                          0,
                          this.values,
                          0,
                          this.values.length );
        this.top = src.top;
        return this;
    }

    /**
     * Returns the maximum number of local variables of this frame.
     * 
     * @return the maximum number of local variables of this frame.
     */
    public int getLocals() {
        return this.locals;
    }

    /**
     * Returns the value of the given local variable.
     * 
     * @param i a local variable index.
     * @return the value of the given local variable.
     * @throws IndexOutOfBoundsException if the variable does not exist.
     */
    public Value getLocal(final int i) throws IndexOutOfBoundsException {
        if ( i >= this.locals ) {
            throw new IndexOutOfBoundsException( "Trying to access an inexistant local variable" );
        }
        return this.values[i];
    }

    /**
     * Sets the value of the given local variable.
     * 
     * @param i a local variable index.
     * @param value the new value of this local variable.
     * @throws IndexOutOfBoundsException if the variable does not exist.
     */
    public void setLocal(final int i,
                         final Value value) throws IndexOutOfBoundsException {
        if ( i >= this.locals ) {
            throw new IndexOutOfBoundsException( "Trying to access an inexistant local variable" );
        }
        this.values[i] = value;
    }

    /**
     * Returns the number of values in the operand stack of this frame. Long and
     * double values are treated as single values.
     * 
     * @return the number of values in the operand stack of this frame.
     */
    public int getStackSize() {
        return this.top;
    }

    /**
     * Returns the value of the given operand stack slot.
     * 
     * @param i the index of an operand stack slot.
     * @return the value of the given operand stack slot.
     * @throws IndexOutOfBoundsException if the operand stack slot does not
     *         exist.
     */
    public Value getStack(final int i) throws IndexOutOfBoundsException {
        if ( i >= this.top ) {
            throw new IndexOutOfBoundsException( "Trying to access an inexistant stack element" );
        }
        return this.values[i + this.locals];
    }

    /**
     * Clears the operand stack of this frame.
     */
    public void clearStack() {
        this.top = 0;
    }

    /**
     * Pops a value from the operand stack of this frame.
     * 
     * @return the value that has been popped from the stack.
     * @throws IndexOutOfBoundsException if the operand stack is empty.
     */
    public Value pop() throws IndexOutOfBoundsException {
        if ( this.top == 0 ) {
            throw new IndexOutOfBoundsException( "Cannot pop operand off an empty stack." );
        }
        return this.values[--this.top + this.locals];
    }

    /**
     * Pushes a value into the operand stack of this frame.
     * 
     * @param value the value that must be pushed into the stack.
     * @throws IndexOutOfBoundsException if the operand stack is full.
     */
    public void push(final Value value) throws IndexOutOfBoundsException {
        if ( this.top + this.locals >= this.values.length ) {
            throw new IndexOutOfBoundsException( "Insufficient maximum stack size." );
        }
        this.values[this.top++ + this.locals] = value;
    }

    public void execute(final AbstractInsnNode insn,
                        final Interpreter interpreter) throws AnalyzerException {
        Value value1, value2, value3, value4;
        List values;
        int var;

        switch ( insn.getOpcode() ) {
            case Opcodes.NOP :
                break;
            case Opcodes.ACONST_NULL :
            case Opcodes.ICONST_M1 :
            case Opcodes.ICONST_0 :
            case Opcodes.ICONST_1 :
            case Opcodes.ICONST_2 :
            case Opcodes.ICONST_3 :
            case Opcodes.ICONST_4 :
            case Opcodes.ICONST_5 :
            case Opcodes.LCONST_0 :
            case Opcodes.LCONST_1 :
            case Opcodes.FCONST_0 :
            case Opcodes.FCONST_1 :
            case Opcodes.FCONST_2 :
            case Opcodes.DCONST_0 :
            case Opcodes.DCONST_1 :
            case Opcodes.BIPUSH :
            case Opcodes.SIPUSH :
            case Opcodes.LDC :
                push( interpreter.newOperation( insn ) );
                break;
            case Opcodes.ILOAD :
            case Opcodes.LLOAD :
            case Opcodes.FLOAD :
            case Opcodes.DLOAD :
            case Opcodes.ALOAD :
                push( interpreter.copyOperation( insn,
                                                 getLocal( ((VarInsnNode) insn).var ) ) );
                break;
            case Opcodes.IALOAD :
            case Opcodes.LALOAD :
            case Opcodes.FALOAD :
            case Opcodes.DALOAD :
            case Opcodes.AALOAD :
            case Opcodes.BALOAD :
            case Opcodes.CALOAD :
            case Opcodes.SALOAD :
                value2 = pop();
                value1 = pop();
                push( interpreter.binaryOperation( insn,
                                                   value1,
                                                   value2 ) );
                break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -