Skip to content

Commit

Permalink
[truffle] Super simple lexical variables
Browse files Browse the repository at this point in the history
(sub () {
    my $hello := sub () {
        nqp::say("Hello World")
    };
    $hello();
})();

works
  • Loading branch information
pmurias committed Jun 24, 2018
1 parent 8bc0dbe commit 303e232
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 15 deletions.
52 changes: 51 additions & 1 deletion nqp-truffle.nqp
Expand Up @@ -91,6 +91,19 @@ class QAST::OperationsTruffle {
$ret;
});

add_op('bind', sub ($comp, $node, :$want) {
my @children := $node.list;
if +@children != 2 {
nqp::die("A 'bind' op must have exactly two children");
}
unless nqp::istype(@children[0], QAST::Var) {
nqp::die("First child of a 'bind' op must be a QAST::Var");
}

my $*BINDVAL := @children[1];
$comp.as_truffle(@children[0], :want($want));
});

method compile_op($comp, $op, $hll, :$want) {
my str $name := $op.op;
if nqp::existskey(%hll_ops, $hll) && nqp::existskey(%hll_ops{$hll}, $name) {
Expand All @@ -110,6 +123,7 @@ class QAST::TruffleCompiler {
TAST.new(tree => self.as_truffle($cu, :want($T_OBJ)));
}


proto method as_truffle($node, :$want) {
{*}
}
Expand All @@ -132,6 +146,7 @@ class QAST::TruffleCompiler {
}

multi method as_truffle(QAST::Block $node, :$want) {
my $*BINDVAL := 0;
my $ret := ['block'];
for $node.list -> $child {
nqp::push($ret, self.as_truffle($child));
Expand All @@ -143,12 +158,47 @@ class QAST::TruffleCompiler {
['sval', $node.value];
}

multi method as_truffle(QAST::Var $node, :$want) {
my $action;

if $node.scope eq 'lexical' {
if $*BINDVAL {
my $value := self.as_truffle_clear_bindval($*BINDVAL, :want($T_OBJ));
$action := ['bind-lexical', $node.name, $value];
} else {
$action := ['get-lexical', $node.name];
}

if $node.decl eq '' {
return $action;
}
elsif $node.decl eq 'var' {
return ['declare-lexical', $node.name, $action];
}
else {
nqp::die("Unimplemented var declaration type {$node.decl}");
}
}
else {
nqp::die("Unimplemented var scope {$node.scope}");
}
}

multi method as_truffle(QAST::Op $node, :$want) {
QAST::OperationsTruffle.compile_op(self, $node, $*HLL, :$want);
}

method as_truffle_clear_bindval($node, :$want) {
my $*BINDVAL := 0;
self.as_truffle($node, :$want);
}

multi method as_truffle(QAST::Node $node, :$want) {
nqp::die('NYI QAST node: ' ~ $node.HOW.name($node));
self.NYI('QAST node: ' ~ $node.HOW.name($node));
}

method NYI($msg) {
nqp::die("NYI: $msg");
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPScope.java
@@ -0,0 +1,8 @@
package org.perl6.nqp.truffle;

import com.oracle.truffle.api.frame.FrameSlot;

public abstract class NQPScope {
public abstract void addLexical(String name);
public abstract FrameSlot findLexical(String name);
}
25 changes: 25 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPScopeWithFrame.java
@@ -0,0 +1,25 @@
package org.perl6.nqp.truffle;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.FrameSlotKind;
import com.oracle.truffle.api.frame.FrameSlot;

public class NQPScopeWithFrame extends NQPScope {
FrameDescriptor frameDescriptor;

NQPScopeWithFrame(FrameDescriptor frameDescriptor) {
this.frameDescriptor = frameDescriptor;
}

public void addLexical(String name) {
frameDescriptor.addFrameSlot(name, FrameSlotKind.Object);
}

public FrameSlot findLexical(String name) {
FrameSlot found = frameDescriptor.findFrameSlot(name);
if (found == null) {
throw new RuntimeException("Can't find lexical: " + name);
}
return found;
}
}

49 changes: 35 additions & 14 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/TruffleCompiler.java
Expand Up @@ -4,57 +4,78 @@
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.FrameSlot;

import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

import org.perl6.nqp.truffle.nodes.NQPExpressionNode;
import org.perl6.nqp.truffle.nodes.NQPBlockBodyNode;
import org.perl6.nqp.truffle.nodes.NQPNotClosureNode;
import org.perl6.nqp.truffle.NQPRootNode;
import org.perl6.nqp.truffle.nodes.call.NQPInvokeNode;
import org.perl6.nqp.truffle.nodes.variables.NQPReadLocalVariableNodeGen;
import org.perl6.nqp.truffle.nodes.variables.NQPBindLocalVariableNodeGen;
import org.perl6.nqp.truffle.NQPRootNode;
import org.perl6.nqp.truffle.runtime.NQPCodeRef;

public class TruffleCompiler {
public static void run(SixModelObject node, ThreadContext tc) {
TruffleCompiler compiler = new TruffleCompiler();
RootNode rootNode = new NQPRootNode(null, new FrameDescriptor(), compiler.build(node, tc));

FrameDescriptor frameDescriptor = new FrameDescriptor();
RootNode rootNode = new NQPRootNode(null, frameDescriptor, compiler.build(node, new NQPScopeWithFrame(frameDescriptor), tc));


CallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);
callTarget.call();
}

private NQPExpressionNode[] expressions(SixModelObject node, ThreadContext tc) {
return expressions(node, 1, tc);
private NQPExpressionNode[] expressions(SixModelObject node, NQPScope scope, ThreadContext tc) {
return expressions(node, 1, scope, tc);
}
private NQPExpressionNode[] expressions(SixModelObject node, int from, ThreadContext tc) {

private NQPExpressionNode[] expressions(SixModelObject node, int from, NQPScope scope, ThreadContext tc) {
int elems = (int) node.elems(tc);
NQPExpressionNode children[] = new NQPExpressionNode[elems - from];
for (int i = from; i < elems; i++) {
children[i-from] = build(node.at_pos_boxed(tc, i), tc);
children[i-from] = build(node.at_pos_boxed(tc, i), scope, tc);
}
return children;
}

public NQPExpressionNode build(SixModelObject node, ThreadContext tc) {
public NQPExpressionNode build(SixModelObject node, NQPScope scope, ThreadContext tc) {
switch (node.at_pos_boxed(tc, 0).get_str(tc)) {
case "say":
return new NQPSayNode(build(node.at_pos_boxed(tc, 1), tc));
return new NQPSayNode(build(node.at_pos_boxed(tc, 1), scope, tc));
case "print":
return new NQPPrintNode(build(node.at_pos_boxed(tc, 1), tc));
return new NQPPrintNode(build(node.at_pos_boxed(tc, 1), scope, tc));
case "stmts": {
NQPExpressionNode children[] = expressions(node, tc);
NQPExpressionNode children[] = expressions(node, scope, tc);
return new NQPStmts(children);
}
case "sval":
return new NQPSVal(node.at_pos_boxed(tc, 1).get_str(tc));
case "call":
NQPExpressionNode codeRef = build(node.at_pos_boxed(tc, 1), tc);
NQPExpressionNode args[] = expressions(node, 2, tc);
NQPExpressionNode codeRef = build(node.at_pos_boxed(tc, 1), scope, tc);
NQPExpressionNode args[] = expressions(node, 2, scope, tc);
return new NQPInvokeNode(codeRef, args);
case "declare-lexical":
scope.addLexical(node.at_pos_boxed(tc, 1).get_str(tc));
return build(node.at_pos_boxed(tc, 2), scope, tc);
case "get-lexical": {
FrameSlot frameSlot = scope.findLexical(node.at_pos_boxed(tc, 1).get_str(tc));
return NQPReadLocalVariableNodeGen.create(frameSlot);
}
case "bind-lexical": {
FrameSlot frameSlot = scope.findLexical(node.at_pos_boxed(tc, 1).get_str(tc));
NQPExpressionNode valueNode = build(node.at_pos_boxed(tc, 2), scope, tc);
return NQPBindLocalVariableNodeGen.create(valueNode, frameSlot);
}
case "block": {
NQPExpressionNode children[] = expressions(node, tc);
FrameDescriptor frameDescriptor = new FrameDescriptor();
NQPExpressionNode children[] = expressions(node, new NQPScopeWithFrame(frameDescriptor), tc);
return new NQPNotClosureNode(new NQPCodeRef(
new NQPRootNode(null, new NQPBlockBodyNode(children))
new NQPRootNode(null, frameDescriptor, new NQPBlockBodyNode(children))
));
}
default:
Expand Down
Expand Up @@ -44,17 +44,31 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.FrameSlot;

import org.perl6.nqp.truffle.nodes.NQPExpressionNode;

import java.util.Map;

@NodeInfo(shortName = "block body", description = "The node implementing the inside of a QAST::Block")
public final class NQPBlockBodyNode extends NQPExpressionNode {
@Children private final NQPExpressionNode[] bodyNodes;

private Map<String, FrameSlot> varSlots;

public NQPBlockBodyNode(NQPExpressionNode[] bodyNodes) {
this.bodyNodes = bodyNodes;
}

public void addVar(String name, FrameSlot slot) {
varSlots.put(name, slot);
}

public FrameSlot lookupVar(String name) {
return varSlots.get(name);
}

@Override
@ExplodeLoop
public Object executeGeneric(VirtualFrame frame) {
Expand Down
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.perl6.nqp.truffle.nodes.variables;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.FrameSlotKind;
import com.oracle.truffle.api.frame.VirtualFrame;
import org.perl6.nqp.truffle.nodes.NQPExpressionNode;

@NodeField(name = "slot", type = FrameSlot.class)
@NodeChild("valueNode")
public abstract class NQPBindLocalVariableNode extends NQPExpressionNode {
protected abstract FrameSlot getSlot();

@Override
public abstract Object executeGeneric(VirtualFrame frame);

@Specialization()
protected Object write(VirtualFrame frame, Object value) {
frame.setObject(getSlot(), value);
return value;
}
}
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.perl6.nqp.truffle.nodes.variables;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.dsl.Introspectable;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.FrameSlotKind;
import com.oracle.truffle.api.frame.FrameUtil;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.dsl.TypeSystemReference;
import org.perl6.nqp.truffle.nodes.NQPExpressionNode;
import org.perl6.nqp.truffle.NQPTypes;

@NodeField(name = "slot", type = FrameSlot.class)
public abstract class NQPReadLocalVariableNode extends NQPExpressionNode {
protected abstract FrameSlot getSlot();

public abstract Object executeGeneric(VirtualFrame frame);

@Specialization
public Object run(VirtualFrame frame) {
return FrameUtil.getObjectSafe(frame, getSlot());
}
}

0 comments on commit 303e232

Please sign in to comment.