Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Small rethink of method handles for reentry
Turns out we can't just use invokeExact on the original method because the
type is different for every compilation unit, duh.  So let's pre-curry and
cache an appropriate form of the method on the StaticCodeInfo.
  • Loading branch information
sorear committed Jun 12, 2013
1 parent 9514dbe commit a7d9742
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -62,6 +62,7 @@ my $TYPE_EX_CONT := 'Lorg/perl6/nqp/runtime/ControlException;';
my $TYPE_EX_RT := 'Ljava/lang/RuntimeException;';
my $TYPE_EX_SAVE := 'Lorg/perl6/nqp/runtime/SaveStackException;';
my $TYPE_THROWABLE := 'Ljava/lang/Throwable;';
my $TYPE_RESUME := 'Lorg/perl6/nqp/runtime/ResumeStatus;';

# Exception handler categories.
my $EX_CAT_CATCH := 1;
Expand Down Expand Up @@ -3054,6 +3055,7 @@ class QAST::CompilerJAST {
$*JMETH.add_argument('cr', $TYPE_CR);
$*JMETH.add_argument('csd', $TYPE_CSD);
$*JMETH.add_argument('__args', "[$TYPE_OBJ");
$*JMETH.add_argument('resume', $TYPE_RESUME);

# Set up temporaries allocator.
my $*BLOCK_TA := BlockTempAlloc.new();
Expand Down Expand Up @@ -3264,7 +3266,7 @@ class QAST::CompilerJAST {
if $block.num_save_sites {
my $saver := JAST::InstructionList.new;
$saver.append(JAST::Label.new( :name( 'SAVER' ) ));
$saver.append(JAST::PushCurMeth.new);
$saver.append($ACONST_NULL);

my @merged;
for $*JMETH.arguments { nqp::push(@merged, $_); }
Expand Down Expand Up @@ -3338,12 +3340,13 @@ class QAST::CompilerJAST {
$TYPE_OPS, 'emptyCallSite', $TYPE_CSD ));
$il.append(JAST::Instruction.new( :op('getstatic'),
$TYPE_OPS, 'emptyArgList', "[$TYPE_OBJ" ));
$il.append($ACONST_NULL);

# Emit the virtual call.
$il.append(savesite(JAST::Instruction.new( :op('invokevirtual'),
'L' ~ $*JCLASS.name ~ ';',
$*CODEREFS.cuid_to_jastmethname($node.cuid),
'V', $TYPE_TC, $TYPE_CR, $TYPE_CSD, "[$TYPE_OBJ" )));
'V', $TYPE_TC, $TYPE_CR, $TYPE_CSD, "[$TYPE_OBJ", $TYPE_RESUME )));

# Load result onto the stack, unless in void context.
if $*WANT != $RT_VOID {
Expand Down
9 changes: 0 additions & 9 deletions src/vm/jvm/QAST/JASTNodes.nqp
Expand Up @@ -367,15 +367,6 @@ class JAST::PushCVal is JAST::Node {
method dump() { ".push_cc $!value" }
}

class JAST::PushCurMeth is JAST::Node {
method new() {
my $node := nqp::create(JAST::PushCurMeth);
$node
}

method dump() { ".push_cur_meth" }
}

class JAST::TryCatch is JAST::Node {
has $!try;
has $!catch;
Expand Down
Expand Up @@ -16,7 +16,9 @@ public SaveStackException(int tag) {
}

public SaveStackException pushFrame(int resumePoint, MethodHandle method, Object[] saveSpace, CallFrame callFrame) {
if (method == null) method = callFrame.codeRef.staticInfo.mhResume;
top = new ResumeStatus.Frame(method, resumePoint, saveSpace, callFrame, top);
if (callFrame != null) callFrame.tc.curFrame = callFrame.caller;
return this;
}
}
12 changes: 12 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/StaticCodeInfo.java
@@ -1,6 +1,8 @@
package org.perl6.nqp.runtime;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.HashMap;

import org.perl6.nqp.sixmodel.SixModelObject;
Expand All @@ -15,6 +17,11 @@ public class StaticCodeInfo implements Cloneable {
* Method handle for the code ref.
*/
MethodHandle mh;

/**
* Curried method handle for resuming.
*/
MethodHandle mhResume;

/**
* The (human-readable) name of the code-ref.
Expand Down Expand Up @@ -139,6 +146,11 @@ public StaticCodeInfo(CompilationUnit compUnit, MethodHandle mh,
this.oLexStatic = new SixModelObject[oLexicalNames.length];
this.oLexStaticFlags = new byte[oLexicalNames.length];
}
MethodType t = mh.type();
if (t.parameterCount() == 5 && t.parameterType(4) == ResumeStatus.class) {
mhResume = MethodHandles.insertArguments(mh, 0, null, null, null, null);
this.mh = MethodHandles.insertArguments(mh, 4, (Object)null);
}
}

public StaticCodeInfo clone() {
Expand Down

0 comments on commit a7d9742

Please sign in to comment.