Skip to content

Commit

Permalink
[truffle] WIP work on calling method
Browse files Browse the repository at this point in the history
Argument less method call on type object works
  • Loading branch information
pmurias committed Sep 20, 2018
1 parent 0bcee93 commit fb21ef9
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 80 deletions.
39 changes: 33 additions & 6 deletions src/vm/jvm/Truffle.nqp
Expand Up @@ -145,6 +145,14 @@ class QAST::OperationsTruffle {

add_simple_op('deserialize', $STR, [$STR, $OBJ, $OBJ, $OBJ, $OBJ]);

add_op('list_b', sub ($comp, $node, :$want) {
my @cuids;
for $node.list -> $block {
@cuids.push($block.cuid);
}
TAST.new($OBJ, ['list_b', @cuids]);
});

add_simple_op('die', $VOID, [$STR]);
%ops<die_s> := %ops<die>;

Expand Down Expand Up @@ -792,6 +800,9 @@ class QAST::TruffleCompiler does SerializeOnce {
}
$sh_ast := QAST::Block.new( :blocktype('immediate'), $sh_ast );

my @code_ref_blocks := $cu.code_ref_blocks;
my $cr_ast := QAST::Op.new( :op('list_b'), |@code_ref_blocks );

my $repo_conflict_resolver;

# Handle repossession conflict resolution code, if any.
Expand Down Expand Up @@ -828,7 +839,7 @@ class QAST::TruffleCompiler does SerializeOnce {
nqp::isnull($serialized) ?? QAST::Op.new( :op('null_s') ) !! QAST::SVal.new( :value($serialized) ),
QAST::Var.new( :name('cur_sc'), :scope('local') ),
$sh_ast,
QAST::Op.new( :op('null') ),
$cr_ast,
QAST::Var.new( :name('conflicts'), :scope('local') )
),
QAST::Op.new(
Expand Down Expand Up @@ -964,16 +975,32 @@ class QAST::TruffleCompiler does SerializeOnce {
{
my $*BLOCK := $block;
my $*BINDVAL := 0;
my @ret := ['block'];

self.compile_all_the_children($node, $RETVAL, @ret);
my @body;
my @tree;

if $node.blocktype eq 'immediate' {
@body := ['block'];
@tree := ['call', @body, [], []];
}
elsif $node.blocktype eq 'declaration' || $node.blocktype eq '' {
@body := ['block'];
@tree := @body;
}
elsif $node.blocktype eq 'declaration_static' {
@body := ['block-static', $node.cuid];
@tree := @body;
}

my int $start_of_body := +@body;

self.compile_all_the_children($node, $RETVAL, @body);

my @compiled_params := self.compile_params($*BLOCK.params);

nqp::splice(@ret, @compiled_params, 1, 0);
nqp::splice(@body, @compiled_params, $start_of_body, 0);

TAST.new($OBJ,
$node.blocktype eq 'immediate' ?? ['call', @ret, [], []] !! @ret);
TAST.new($OBJ, @tree);
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/ManageScopes.java
Expand Up @@ -3,24 +3,34 @@
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.FrameSlotKind;
import com.oracle.truffle.api.nodes.RootNode;


import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPBlockBodyNode;
import org.perl6.nqp.truffle.nodes.control.NQPBlockNode;
import org.perl6.nqp.truffle.nodes.control.NQPStaticBlockNode;

import org.perl6.nqp.truffle.runtime.NQPCodeRef;

import org.perl6.nqp.truffle.MalformedAstException;

import org.perl6.nqp.truffle.sixmodel.Bootstrapper;



import org.perl6.nqp.dsl.Predeserializer;
import org.perl6.nqp.dsl.Deserializer;

public class ManageScopes {
@Predeserializer("block")
public static NQPScope createNewScope(NQPScope scope) {
FrameDescriptor frameDescriptor = new FrameDescriptor();
return new NQPScopeWithFrame(frameDescriptor, scope);
return new NQPScopeWithFrame(new FrameDescriptor(), scope);
}

@Predeserializer("block-static")
public static NQPScope createNewStaticScope(NQPScope scope) {
return new NQPScopeWithFrame(new FrameDescriptor(), scope);
}

@Deserializer("block")
Expand All @@ -31,6 +41,16 @@ public static NQPNode createBlock(NQPScope scope, NQPNode[] children) {
);
}

@Deserializer("block-static")
public static NQPNode createBlock(NQPScope scope, String cuid, NQPNode[] children) {
FrameDescriptor frameDescriptor = ((NQPScopeWithFrame) scope).getFrameDescriptor();
RootNode rootNode = new NQPRootNode(null, frameDescriptor, new NQPBlockBodyNode(children));
NQPCodeRef code = new NQPCodeRef(rootNode, null);
System.out.println("adding cuids");
scope.addCuid(cuid, code);
return new NQPStaticBlockNode(code);
}

private static FrameSlotKind kindFromType(long type) {
switch ((int)type) {
case 0: return FrameSlotKind.Object;
Expand Down
21 changes: 21 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPCompUnitScope.java
@@ -1,7 +1,9 @@
package org.perl6.nqp.truffle;

import com.oracle.truffle.api.frame.FrameSlot;
import java.util.HashMap;
import org.perl6.nqp.truffle.GlobalContext;
import org.perl6.nqp.truffle.runtime.NQPCodeRef;
import org.perl6.nqp.truffle.runtime.HLL;
import org.perl6.nqp.truffle.sixmodel.SerializationContext;

Expand All @@ -11,6 +13,8 @@ public class NQPCompUnitScope extends NQPScope {

GlobalContext globalContext;

HashMap<String, NQPCodeRef> cuids;

public NQPCompUnitScope(NQPScope outer, String hll, GlobalContext globalContext) {
this.outer = outer;

Expand All @@ -23,6 +27,8 @@ public NQPCompUnitScope(NQPScope outer, String hll, GlobalContext globalContext)
}

this.currentHLL = hlls.get(hll);

cuids = new HashMap<String, NQPCodeRef>();
}

@Override
Expand All @@ -36,6 +42,21 @@ public HLL getCurrentHLL() {
return currentHLL;
}

@Override
public NQPCodeRef getCuid(String cuid) {
NQPCodeRef got = cuids.get(cuid);
if (got != null) {
return got;
} else {
throw new RuntimeException("Can't get cuid: " + cuid);
}
}

@Override
public void addCuid(String cuid, NQPCodeRef codeRef) {
cuids.put(cuid, codeRef);
}

@Override
public FrameSlot addLexical(String name) {
return outer.addLexical(name);
Expand Down
5 changes: 5 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPScope.java
@@ -1,5 +1,6 @@
package org.perl6.nqp.truffle;

import org.perl6.nqp.truffle.runtime.NQPCodeRef;
import org.perl6.nqp.truffle.runtime.HLL;
import org.perl6.nqp.truffle.sixmodel.SerializationContext;
import org.perl6.nqp.truffle.GlobalContext;
Expand All @@ -17,5 +18,9 @@ public FoundLexical findLexical(String name) {
public abstract FrameSlot findLocal(String name);

public abstract HLL getCurrentHLL();

public abstract NQPCodeRef getCuid(String cuid);
public abstract void addCuid(String cuid, NQPCodeRef codeRef);

public abstract GlobalContext getGlobalContext();
}
19 changes: 19 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPScopeWithFrame.java
Expand Up @@ -5,6 +5,7 @@

import java.util.HashMap;

import org.perl6.nqp.truffle.runtime.NQPCodeRef;
import org.perl6.nqp.truffle.runtime.HLL;
import org.perl6.nqp.truffle.sixmodel.SerializationContext;
import org.perl6.nqp.truffle.GlobalContext;
Expand Down Expand Up @@ -94,5 +95,23 @@ public GlobalContext getGlobalContext() {
throw new RuntimeException("Can't get HLLs");
}
}

@Override
public NQPCodeRef getCuid(String cuid) {
if (outer != null) {
return outer.getCuid(cuid);
} else {
throw new RuntimeException("Can't get cuid");
}
}

@Override
public void addCuid(String cuid, NQPCodeRef codeRef) {
if (outer != null) {
outer.addCuid(cuid, codeRef);
} else {
throw new RuntimeException("Can't add cuid");
}
}
}

Expand Up @@ -5,6 +5,11 @@
import com.oracle.truffle.api.nodes.NodeInfo;
import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import org.perl6.nqp.truffle.sixmodel.TypeObject;
import org.perl6.nqp.truffle.runtime.NQPArguments;
import org.perl6.nqp.truffle.runtime.NQPCodeRef;

import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "callmethod")
Expand Down Expand Up @@ -37,8 +42,27 @@ public NQPCallmethodNode(NQPNode invocantNode, NQPNode methodNode, long[] argume
public Object execute(VirtualFrame frame) {
Object invocant = invocantNode.execute(frame);
String method = this.methodNode.executeStr(frame);
System.out.println("callmethod NYI: " + method);
return org.perl6.nqp.truffle.runtime.NQPNull.SINGLETON;

/* TODO - specialization and all the cool inline caching */
System.out.println("callmethod on: " + invocant.getClass().getName());
if (invocant instanceof TypeObject) {
System.out.println("callmethod on TypeObject: " + method);
TypeObject typeObject = (TypeObject) invocant;
Object foundMethod = typeObject.stable.methodCache.get(method);
if (foundMethod != null) {
Object[] arguments = NQPArguments.createInitial(1);
NQPArguments.setUserArgument(arguments, 0, invocant);

NQPCodeRef function = (NQPCodeRef) foundMethod;
NQPArguments.setOuterFrame(arguments, function.getOuterFrame());
IndirectCallNode callNode = IndirectCallNode.create();
return callNode.call(function.getCallTarget(), arguments);
}
return org.perl6.nqp.truffle.runtime.NQPNull.SINGLETON;
} else {
System.out.println("callmethod NYI: " + method);
return org.perl6.nqp.truffle.runtime.NQPNull.SINGLETON;
}
}
}

@@ -1,24 +1,35 @@
package org.perl6.nqp.truffle.nodes.expression;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import java.util.HashMap;
import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;
import org.perl6.nqp.truffle.NQPScope;

import org.perl6.nqp.truffle.sixmodel.SerializationContext;

import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "createsc")
public final class NQPCreatescNode extends NQPObjNode {
private final HashMap<String, SerializationContext> scs;
@Child private NQPNode handleNode;

@Deserializer
public NQPCreatescNode(NQPNode handleNode) {
public NQPCreatescNode(HashMap<String, SerializationContext> scs, NQPNode handleNode) {
this.scs = scs;
this.handleNode = handleNode;
}

@Deserializer("createsc")
public static NQPCreatescNode deserialize(NQPScope scope, NQPNode handleNode) {
return new NQPCreatescNode(scope.getGlobalContext().scs, handleNode);
}

@Override
public Object execute(VirtualFrame frame) {
return new SerializationContext(handleNode.executeStr(frame));
String handle = handleNode.executeStr(frame);
SerializationContext sc = new SerializationContext(handle);
scs.put(handle, sc);
return sc;
}
}
Expand Up @@ -9,6 +9,8 @@
import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPStrNode;
import org.perl6.nqp.truffle.runtime.NQPListStr;
import org.perl6.nqp.truffle.runtime.NQPList;
import org.perl6.nqp.truffle.runtime.NQPCodeRef;
import org.perl6.nqp.truffle.runtime.Base64;
import org.perl6.nqp.truffle.sixmodel.SerializationReader;
import org.perl6.nqp.truffle.sixmodel.SerializationContext;
Expand Down Expand Up @@ -42,17 +44,27 @@ public String executeStr(VirtualFrame frame) {
String blob = blobNode.executeStr(frame);
SerializationContext sc = (SerializationContext) scNode.execute(frame);
NQPListStr shList = (NQPListStr) shNode.execute(frame);
Object cr = crNode.execute(frame);
NQPList crList = (NQPList) crNode.execute(frame);
Object conflict = conflictNode.execute(frame);

System.out.println("deserializing");
ByteBuffer binaryBlob = Base64.decode(blob);


String[] sh = new String[shList.elems()];
for (int i = 0; i < shList.elems(); i++) {
sh[i] = shList.atposStr(i);
}

SerializationReader sr = new SerializationReader(sc, sh, binaryBlob, scs);
// tc, sc, shArray, crArray, crCount, binaryBlob);
System.out.println("got code refs: " + crList.elems());

NQPCodeRef[] cr = new NQPCodeRef[crList.elems()];
for (int i = 0; i < crList.elems(); i++) {
cr[i] = (NQPCodeRef) crList.atpos(i);
}


SerializationReader sr = new SerializationReader(sc, sh, cr, binaryBlob, scs);
sr.deserialize();

return blob;
Expand Down
@@ -1,24 +1,37 @@
package org.perl6.nqp.truffle.nodes.expression;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import java.util.HashMap;
import org.perl6.nqp.dsl.Deserializer;
import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;
import org.perl6.nqp.truffle.runtime.NQPNull;
import org.perl6.nqp.truffle.NQPScope;
import org.perl6.nqp.truffle.sixmodel.SerializationContext;

@NodeInfo(shortName = "wval")
public final class NQPWValNode extends NQPObjNode {
private final String handle;
private final long idx;
private final int index;

@Deserializer
public NQPWValNode(String handle, long idx) {
public HashMap<String, SerializationContext> scs;

public NQPWValNode(HashMap<String, SerializationContext> scs, String handle, int index) {
this.scs = scs;
this.handle = handle;
this.idx = idx;
this.index = index;
}

@Deserializer("wval")
public static NQPWValNode deserialize(NQPScope scope, String handle, long index) {
return new NQPWValNode(scope.getGlobalContext().scs, handle, (int) index);
}

/* TODO - do the lookup only once*/
@Override
public Object execute(VirtualFrame frame) {
return NQPNull.SINGLETON;
if (scs.get(handle) == null) {
System.out.println("no sc:" + handle);
}
return scs.get(handle).getObject(index);
}
}

0 comments on commit fb21ef9

Please sign in to comment.