Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   ***** BEGIN LICENSE BLOCK *****
   * Version: EPL 1.0/GPL 2.0/LGPL 2.1
   *
   * The contents of this file are subject to the Eclipse Public
   * License Version 1.0 (the "License"); you may not use this file
   * except in compliance with the License. You may obtain a copy of
   * the License at http://www.eclipse.org/legal/epl-v10.html
   *
  * Software distributed under the License is distributed on an "AS
  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  * implied. See the License for the specific language governing
  * rights and limitations under the License.
  * 
  * Alternatively, the contents of this file may be used under the terms of
  * either of the GNU General Public License Version 2 or later (the "GPL"),
  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the EPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the EPL, the GPL or the LGPL.
  ***** END LICENSE BLOCK *****/
 
 package org.jruby.compiler.impl;
 
 import org.jruby.Ruby;
 import static org.jruby.util.CodegenUtils.*;
 
 import  org.objectweb.asm.Label;

Author(s):
headius
 
 public abstract class AbstractVariableCompiler implements VariableCompiler {
     protected SkinnyMethodAdapter method;
     protected BaseBodyCompiler methodCompiler;
     protected int argsIndex;
     protected int tempVariableIndex;
     protected Arity arity;
     protected StaticScope scope;
     protected boolean specificArity;
 
     public AbstractVariableCompiler(
             BaseBodyCompiler methodCompiler,
             SkinnyMethodAdapter method,
             StaticScope scope,
             boolean specificArity,
             int argsIndex,
             int firstTempIndex) {
         this. = methodCompiler;
         this. = method;
         this. = argsIndex;
         this. = firstTempIndex;
         this. = scope;
         this. = specificArity;
     }
     
     public SkinnyMethodAdapter getMethodAdapter() {
         return this.;
     }
 
     public void setMethodAdapter(SkinnyMethodAdapter sma) {
         this. = sma;
     }
 
     public void assignLastLine() {
         .loadRuntime();
         .swap();
         .loadThreadContext();
         .swap();
         .invokestatic(p(Helpers.class), "setLastLine"sig(IRubyObject.classRuby.classThreadContext.classIRubyObject.class));
     }
 
     public void assignLastLine(CompilerCallback value) {
         .loadRuntime();
         .loadThreadContext();
         value.call();
         .invokestatic(p(Helpers.class), "setLastLine"sig(IRubyObject.classRuby.classThreadContext.classIRubyObject.class));
     }
 
     public void retrieveLastLine() {
         .loadRuntime();
         .loadThreadContext();
        .invokestatic(p(Helpers.class), "getLastLine"sig(IRubyObject.classRuby.classThreadContext.class));
    }
    public void assignBackRef() {
        .loadRuntime();
        .swap();
        .swap();
        .invokestatic(p(Helpers.class), "setBackref"sig(IRubyObject.classRuby.classThreadContext.classIRubyObject.class));
    }    
    public void assignBackRef(CompilerCallback value) {
        .loadRuntime();
        value.call();
        .invokestatic(p(Helpers.class), "setBackref"sig(IRubyObject.classRuby.classThreadContext.classIRubyObject.class));
    }    
    public void retrieveBackRef() {
        .loadRuntime();
        .invokestatic(p(Helpers.class), "getBackref"sig(IRubyObject.classRuby.classThreadContext.class));
    }
    public void checkMethodArity(int requiredArgsint optArgsint restArg) {
        if () {
            // do nothing; arity check is done before call
        } else {
            boolean needsError = false;
            if (restArg != -1) {
                if (requiredArgs > 0) {
                    needsError = true;
                    // just confirm minimum args provided
                    .loadRuntime();
                    .aload();
                    .pushInt(requiredArgs);
                    .pushInt(-1);
                }
            } else if (optArgs > 0) {
                needsError = true;
                .loadRuntime();
                .aload();
                .pushInt(requiredArgs);
                .pushInt(requiredArgs + optArgs);
            } else {
                needsError = true;
                // just confirm args length == required
                .loadRuntime();
                .aload();
                .pushInt(requiredArgs);
                .pushInt(requiredArgs);
            }
            if (needsError) {
                .invokestatic(p(Arity.class), "raiseArgumentError"sig(void.classRuby.classIRubyObject[].classint.classint.class));
            }
        }
    }
    public void assignMethodArguments(
            Object requiredArgs,
            int requiredArgsCount,
            Object optArgs,
            int optArgsCount,
            ArrayCallback requiredAssignment,
            ArrayCallback optGivenAssignment,
            ArrayCallback optNotGivenAssignment,
            CompilerCallback restAssignment,
            CompilerCallback blockAssignment) {
        if () {
            int currentArgElement = 0;
            for (; currentArgElement < .getRequiredArgs(); currentArgElement++) {
                .aload( + currentArgElement);
                requiredAssignment.nextValue(requiredArgscurrentArgElement);
            }
        } else {
            if (requiredArgsCount > 0 || optArgsCount > 0 || restAssignment != null) {
                // first, iterate over all required args
                int currentArgElement = 0;
                for (; currentArgElement < requiredArgsCountcurrentArgElement++) {
                    // extract item from array
                    .aload();
                    .pushInt(currentArgElement); // index for the item
                    .arrayload();
                    requiredAssignment.nextValue(requiredArgscurrentArgElement);
                }
                if (optArgsCount > 0) {
                    // prepare labels for opt logic
                    Label doneWithOpt = new Label();
                    Label[] optLabels = new Label[optArgsCount];
                    for (int i = 0; i < optLabels.length; i ++) optLabels[i] = new Label();
                    // next, iterate over all optional args, until no more arguments
                    for (int optArgElement = 0; optArgElement < optArgsCountcurrentArgElement++, optArgElement++) {
                        .aload();
                        .pushInt(currentArgElement); // index for the item
                        .invokeUtilityMethod("elementOrNull"sig(IRubyObject.classIRubyObject[].classint.class));
                        .dup();
                        .ifnull(optLabels[optArgElement]);
                        optGivenAssignment.nextValue(optArgsoptArgElement);
                    }
                    .go_to(doneWithOpt);
                    // now logic for each optional value
                    for (int optArgElement = 0; optArgElement < optArgsCountoptArgElement++) {
                        // otherwise no items left available, use the code for default
                        .label(optLabels[optArgElement]);
                        optNotGivenAssignment.nextValue(optArgsoptArgElement);
                    }
                    // pop extra failed value from first cycle and we're done
                    .pop();
                    .label(doneWithOpt);
                }
                // if there's args left and we want them, assign to rest arg
                if (restAssignment != null) {
                    // assign remaining elements as an array for rest args (or empty array)
                    .aload();
                    .loadRuntime();
                    .pushInt(currentArgElement);
                    .invokeUtilityMethod("createSubarray"sig(RubyArray.classIRubyObject[].classRuby.classint.class));
                    restAssignment.call();
                }
            }
        }
        
        // block argument assignment, if there's a block arg
        if (blockAssignment != null) {
            .loadRuntime();
            .aload(.getClosureIndex());
            .invokeUtilityMethod("processBlockArgument"sig(IRubyObject.classparams(Ruby.classBlock.class)));
            blockAssignment.call();
        }
    }
    public void assignMethodArguments19(
            Object preArgs,
            int preArgsCount,
            Object postArgs,
            int postArgsCount,
            int postArgsIndex,
            Object optArgs,
            int optArgsCount,
            ArrayCallback requiredAssignment,
            ArrayCallback optGivenAssignment,
            ArrayCallback optNotGivenAssignment,
            CompilerCallback restAssignment,
            CompilerCallback blockAssignment) {
        
        if () {
            int currentArgElement = 0;
            for (; currentArgElement < .getRequiredArgs(); currentArgElement++) {
                .aload( + currentArgElement);
                requiredAssignment.nextValue(preArgscurrentArgElement);
            }
        } else {
            if (preArgsCount > 0 || postArgsCount > 0 || optArgsCount > 0 || restAssignment != null) {
                // first, iterate over all pre args
                int currentArgElement = 0;
                for (; currentArgElement < preArgsCountcurrentArgElement++) {
                    // extract item from array
                    .aload();
                    .pushInt(currentArgElement); // index for the item
                    // this could probably be more efficient, bailing out on assigning args past the end?
                    .loadNil();
                    .invokeUtilityMethod("elementOrNil"sig(IRubyObject.classIRubyObject[].classint.classIRubyObject.class));
                    requiredAssignment.nextValue(preArgscurrentArgElement);
                }
                // then optional args
                if (optArgsCount > 0) {
                    // prepare labels for opt logic
                    Label doneWithOpt = new Label();
                    Label[] optLabels = new Label[optArgsCount];
                    for (int i = 0; i < optLabels.length; i ++) optLabels[i] = new Label();
                    // next, iterate over all optional args, until no more arguments
                    for (int optArgElement = 0; optArgElement < optArgsCountcurrentArgElement++, optArgElement++) {
                        .aload();
                        .pushInt(currentArgElement); // index for the item
                        .pushInt(postArgsCount);
                        .invokeUtilityMethod("optElementOrNull"sig(IRubyObject.classIRubyObject[].classint.classint.class));
                        .dup();
                        .ifnull(optLabels[optArgElement]);
                        optGivenAssignment.nextValue(optArgsoptArgElement);
                    }
                    .go_to(doneWithOpt);
                    // now logic for each optional value
                    for (int optArgElement = 0; optArgElement < optArgsCountoptArgElement++) {
                        // otherwise no items left available, use the code for default
                        .label(optLabels[optArgElement]);
                        optNotGivenAssignment.nextValue(optArgsoptArgElement);
                    }
                    // pop extra failed value from first cycle and we're done
                    .pop();
                    .label(doneWithOpt);
                }
                // if rest args, excluding post args
                if (restAssignment != null) {
                    // assign remaining elements as an array for rest args (or empty array)
                    .aload();
                    .loadRuntime();
                    .pushInt(currentArgElement);
                    .pushInt(postArgsCount);
                    .invokeUtilityMethod("createSubarray"sig(RubyArray.classIRubyObject[].classRuby.classint.classint.class));
                    restAssignment.call();
                }
                // finally, post args
                for (int postArgIndex = 0; postArgIndex < postArgsCountpostArgIndex++) {
                    // extract item from array
                    .aload();
                    .pushInt(postArgsCount);
                    .pushInt(postArgIndex); // index for the item
                    // this could probably be more efficient, bailing out on assigning args past the end?
                    .loadNil();
                    .invokeUtilityMethod("postElementOrNil"sig(IRubyObject.classIRubyObject[].classint.classint.classIRubyObject.class));
                    requiredAssignment.nextValue(postArgspostArgIndex);
                }
            }
        }
        // block argument assignment, if there's a block arg
        if (blockAssignment != null) {
            .loadRuntime();
            .aload(.getClosureIndex());
            .invokeUtilityMethod("processBlockArgument"sig(IRubyObject.classparams(Ruby.classBlock.class)));
            blockAssignment.call();
        }
    }
    public void assignClosureArguments(CompilerCallback masgnCallbackCompilerCallback blockAssignment) {
        // args are already on stack, call masgnCallback
        masgnCallback.call();
        // block argument assignment, if there's a block arg
        if (blockAssignment != null) {
            .loadRuntime();
            .aload(.getClosureIndex());
            .invokeUtilityMethod("processBlockArgument"sig(IRubyObject.classparams(Ruby.classBlock.class)));
            blockAssignment.call();
        }
    }
        
    public int grabTempLocal() {
        return ++;
    }
    public void setTempLocal(int index) {
        .astore(index);
    }
    public void getTempLocal(int index) {
        .aload(index);
    }
    public void releaseTempLocal() {
        --;
    }
    public boolean isHeap() {
        return false;
    }
    protected void assignHeapLocal(CompilerCallback valueint depthint indexboolean expr) {
        switch (index) {
        case 0:
            unwrapParentScopes(depth);
            value.call();
            .invokevirtual(p(DynamicScope.class), "setValueZeroDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 1:
            unwrapParentScopes(depth);
            value.call();
            .invokevirtual(p(DynamicScope.class), "setValueOneDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 2:
            unwrapParentScopes(depth);
            value.call();
            .invokevirtual(p(DynamicScope.class), "setValueTwoDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 3:
            unwrapParentScopes(depth);
            value.call();
            .invokevirtual(p(DynamicScope.class), "setValueThreeDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        default:
            .pushInt(index);
            value.call();
            .pushInt(depth);
            .invokevirtual(p(DynamicScope.class), "setValue"sig(IRubyObject.classparams(.IRubyObject.class.)));
        }
        if (!expr) {
            // not an expression, don't want result; pop it
            .pop();
        }
    }
    protected void assignHeapLocal(int depthint indexboolean expr) {
        
        switch (index) {
        case 0:
            unwrapParentScopes(depth);
            .swap();
            .invokevirtual(p(DynamicScope.class), "setValueZeroDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 1:
            unwrapParentScopes(depth);
            .swap();
            .invokevirtual(p(DynamicScope.class), "setValueOneDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 2:
            unwrapParentScopes(depth);
            .swap();
            .invokevirtual(p(DynamicScope.class), "setValueTwoDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        case 3:
            unwrapParentScopes(depth);
            .swap();
            .invokevirtual(p(DynamicScope.class), "setValueThreeDepthZero"sig(IRubyObject.classparams(IRubyObject.class)));
            break;
        default:
            .swap();
            .pushInt(index);
            .pushInt(depth);
            .invokevirtual(p(DynamicScope.class), "setValue"sig(IRubyObject.classparams(IRubyObject.class..)));
        }
        if (!expr) {
            // not an expression, don't want result; pop it
            .pop();
        }
    }
    protected void retrieveHeapLocal(int depthint index) {
        switch (index) {
        case 0:
            unwrapParentScopes(depth);
            .loadNil();
            .invokevirtual(p(DynamicScope.class), "getValueZeroDepthZeroOrNil"sig(IRubyObject.classIRubyObject.class));
            break;
        case 1:
            unwrapParentScopes(depth);
            .loadNil();
            .invokevirtual(p(DynamicScope.class), "getValueOneDepthZeroOrNil"sig(IRubyObject.classIRubyObject.class));
            break;
        case 2:
            unwrapParentScopes(depth);
            .loadNil();
            .invokevirtual(p(DynamicScope.class), "getValueTwoDepthZeroOrNil"sig(IRubyObject.classIRubyObject.class));
            break;
        case 3:
            unwrapParentScopes(depth);
            .loadNil();
            .invokevirtual(p(DynamicScope.class), "getValueThreeDepthZeroOrNil"sig(IRubyObject.classIRubyObject.class));
            break;
        default:
            .pushInt(index);
            .pushInt(depth);
            .loadNil();
            .invokevirtual(p(DynamicScope.class), "getValueOrNil"sig(IRubyObject.classparams(..IRubyObject.class)));
        }
    }
    protected void unwrapParentScopes(int depth) {
        // unwrap scopes to appropriate depth
        while (depth > 0) {
            .invokevirtual(p(DynamicScope.class), "getNextCapturedScope"sig(DynamicScope.class));
            depth--;
        }
    }
New to GrepCode? Check out our FAQ X