Permalink
Browse files

Refactor optarg logic (no fixes)

  • Loading branch information...
1 parent 28476d3 commit 18352a28824e894ccf5faaddf90854aa3eeccba8 @enebo enebo committed Mar 6, 2014
@@ -10,34 +10,34 @@
import org.jruby.runtime.builtin.IRubyObject;
public class ReceiveOptArgInstr extends ReceiveArgBase implements FixedArityInstr {
- /** Starting offset into the args array*/
- public final int argOffset;
+ /** opt args follow pre args **/
+ public final int preArgs;
- /** Number of arguments already accounted for */
- public final int numUsedArgs;
+ /** total number of required args (pre + post) **/
+ public final int requiredArgs;
- public ReceiveOptArgInstr(Variable result, int numUsedArgs, int argOffset, int optArgIndex) {
+ public ReceiveOptArgInstr(Variable result, int requiredArgs, int preArgs, int optArgIndex) {
super(Operation.RECV_OPT_ARG, result, optArgIndex);
- this.argOffset = argOffset;
- this.numUsedArgs = numUsedArgs;
+ this.preArgs = preArgs;
+ this.requiredArgs = requiredArgs;
}
@Override
public Operand[] getOperands() {
- return new Operand[] { new Fixnum(numUsedArgs), new Fixnum(argOffset), new Fixnum(argIndex) };
+ return new Operand[] { new Fixnum(requiredArgs), new Fixnum(preArgs), new Fixnum(argIndex) };
}
@Override
public String toString() {
- return (isDead() ? "[DEAD]" : "") + (hasUnusedResult() ? "[DEAD-RESULT]" : "") + getResult() + " = " + getOperation() + "(" + numUsedArgs + "," + argOffset + "," + argIndex + ")";
+ return (isDead() ? "[DEAD]" : "") + (hasUnusedResult() ? "[DEAD-RESULT]" : "") + getResult() + " = " + getOperation() + "(" + requiredArgs + "," + preArgs + "," + argIndex + ")";
}
- public int getArgOffset() {
- return argOffset;
+ public int getPreArgs() {
+ return preArgs;
}
- public int getNumUsedArgs() {
- return numUsedArgs;
+ public int getRequiredArgs() {
+ return requiredArgs;
}
@Override
@@ -49,24 +49,26 @@ public Instr cloneForInlining(InlinerInfo ii) {
switch (ii.getCloneMode()) {
case ENSURE_BLOCK_CLONE:
case NORMAL_CLONE:
- return new ReceiveOptArgInstr(ii.getRenamedVariable(result), numUsedArgs, argOffset, optArgIndex);
+ return new ReceiveOptArgInstr(ii.getRenamedVariable(result), requiredArgs, preArgs, optArgIndex);
default: {
- int minReqdArgs = optArgIndex + numUsedArgs;
+ int minReqdArgs = optArgIndex + requiredArgs;
if (ii.canMapArgsStatically()) {
int n = ii.getArgsCount();
- return new CopyInstr(ii.getRenamedVariable(result), minReqdArgs < n ? ii.getArg(argOffset + optArgIndex) : UndefinedValue.UNDEFINED);
+ return new CopyInstr(ii.getRenamedVariable(result), minReqdArgs < n ? ii.getArg(preArgs + optArgIndex) : UndefinedValue.UNDEFINED);
} else {
- return new OptArgMultipleAsgnInstr(ii.getRenamedVariable(result), ii.getArgs(), argOffset + optArgIndex, minReqdArgs);
+ return new OptArgMultipleAsgnInstr(ii.getRenamedVariable(result), ii.getArgs(), preArgs + optArgIndex, minReqdArgs);
}
}
}
}
- public IRubyObject receiveOptArg(IRubyObject[] args, int kwArgHashCount) {
- // Added this copy for code clarity
- // argIndex is relative to start of opt args and not the start of arg array
- int optArgIndex = this.argIndex;
- return (optArgIndex + numUsedArgs + kwArgHashCount < args.length ? args[argOffset + optArgIndex] : UndefinedValue.UNDEFINED);
+ public IRubyObject receiveOptArg(IRubyObject[] args, boolean keywordArgumentSupplied) {
+ int optArgIndex = argIndex; // which opt arg we are processing? (first one has index 0, second 1, ...).
+ int argsLength = keywordArgumentSupplied ? args.length - 1 : args.length;
+
+ if (requiredArgs + optArgIndex >= argsLength) return UndefinedValue.UNDEFINED; // No more args left
+
+ return args[preArgs + optArgIndex];
}
@Override
@@ -309,7 +309,7 @@ private static void receiveArg(ThreadContext context, Instr i, Operation operati
setResult(temp, currDynScope, instr.getResult(), result);
return;
case RECV_OPT_ARG:
- result = ((ReceiveOptArgInstr)instr).receiveOptArg(args, kwArgHashCount);
+ result = ((ReceiveOptArgInstr)instr).receiveOptArg(args, kwArgHashCount > 0);
// For blocks, missing arg translates to nil
setResult(temp, currDynScope, instr.getResult(), result);
return;
@@ -534,8 +534,8 @@ private void encodeReceiveKeywordRestArgInstr(ReceiveKeywordRestArgInstr instr)
}
private void encodeReceiveOptArgInstr(ReceiveOptArgInstr instr) {
- e.encode(instr.numUsedArgs);
- e.encode(instr.getArgOffset());
+ e.encode(instr.requiredArgs);
+ e.encode(instr.getPreArgs());
e.encode(instr.getArgIndex());
}
@@ -1446,8 +1446,8 @@ public void ReceivePreReqdArgInstr(ReceivePreReqdArgInstr instr) {
public void ReceiveOptArgInstr(ReceiveOptArgInstr instr) {
// FIXME: Only works when args is in an array rather than being flattened out
// FIXME: Missing kwargs 2.0 support (kwArgHashCount value)
- jvm.method().adapter.pushInt(instr.getArgIndex() + instr.numUsedArgs); // MIN reqd args
- jvm.method().adapter.pushInt(instr.getArgIndex() + instr.argOffset); // args array offset
+ jvm.method().adapter.pushInt(instr.getArgIndex() + instr.requiredArgs); // MIN reqd args
+ jvm.method().adapter.pushInt(instr.getArgIndex() + instr.preArgs); // args array offset
jvm.method().adapter.aload(3); // index of arg array
jvm.method().invokeHelper("irLoadOptArg", IRubyObject.class, int.class, int.class, IRubyObject[].class);
jvmStoreLocal(instr.getResult());

0 comments on commit 18352a2

Please sign in to comment.