Skip to content

Commit

Permalink
[truffle] Implement running things as VOID and returning stuff from subs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Jul 5, 2018
1 parent b427898 commit e393c03
Show file tree
Hide file tree
Showing 108 changed files with 541 additions and 99 deletions.
46 changes: 37 additions & 9 deletions nqp-truffle.nqp
Expand Up @@ -356,6 +356,21 @@ class QAST::TruffleCompiler {
}
}

if $desired == $RETVAL {
if $got == $INT {
return TAST.new($RETVAL, ['retval-int', $tast.tree]);
}
if $got == $STR {
return TAST.new($RETVAL, ['retval-str', $tast.tree]);
}
if $got == $NUM {
return TAST.new($RETVAL, ['retval-num', $tast.tree]);
}
elsif $got == $OBJ {
return TAST.new($RETVAL, $tast.tree);
}
}

if $desired == $NUM {
if $got == $INT {
return TAST.new($NUM, ['coerce-int-to-num', $tast.tree]);
Expand Down Expand Up @@ -417,11 +432,9 @@ class QAST::TruffleCompiler {
}

multi method as_truffle(QAST::Stmts $node, :$want) {
my $ret := ['stmts'];
for $node.list -> $child {
nqp::push($ret, self.as_truffle($child, :want($VOID)).tree);
}
TAST.new($VOID, $ret);
my @tree := ['stmts'];
self.compile_all_the_children($node, $want, @tree);
TAST.new($want, @tree);
}

method compile_params(@params) {
Expand All @@ -434,6 +447,24 @@ class QAST::TruffleCompiler {
@ret;
}

method compile_all_the_children($node, $want, @tree, :$result_child) {
my @stmts := $node.list;

if $want == $VOID {
$result_child := -1;
}
elsif !nqp::defined($result_child) {
$result_child := +@stmts - 1;
}

my int $i := 0;
for @stmts -> $stmt {
my $tast := self.as_truffle(@stmts[$i], :want($i == $result_child ?? $want !! $VOID));
nqp::push(@tree, $tast.tree);
$i := $i + 1;
}
}

multi method as_truffle(QAST::Block $node, :$want) {
my $outer := try $*BLOCK;
my $block := BlockInfo.new($node, $outer);
Expand All @@ -442,10 +473,7 @@ class QAST::TruffleCompiler {
my $*BINDVAL := 0;
my @ret := ['block'];


for $node.list -> $child {
nqp::push(@ret, self.as_truffle($child, :want($VOID)).tree);
}
self.compile_all_the_children($node, $RETVAL, @ret);

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

Expand Down
16 changes: 15 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPStmts.java
Expand Up @@ -70,10 +70,24 @@ public Object execute(VirtualFrame frame) {

CompilerAsserts.compilationConstant(bodyNodes.length);

int i = 0;
for (NQPNode statement : bodyNodes) {
ret = statement.execute(frame);
if (i == bodyNodes.length - 1) {
ret = statement.execute(frame);
} else {
statement.executeVoid(frame);
}
i++;
}

return ret;
}

@Override
@ExplodeLoop
public void executeVoid(VirtualFrame frame) {
for (NQPNode statement : bodyNodes) {
statement.executeVoid(frame);
}
}
}
Expand Up @@ -76,10 +76,24 @@ public Object execute(VirtualFrame frame) {

CompilerAsserts.compilationConstant(bodyNodes.length);

int i = 0;
for (NQPNode statement : bodyNodes) {
ret = statement.execute(frame);
if (i == bodyNodes.length - 1) {
ret = statement.execute(frame);
} else {
statement.executeVoid(frame);
}
i++;
}

return ret;
}

@Override
@ExplodeLoop
public void executeVoid(VirtualFrame frame) {
for (NQPNode statement : bodyNodes) {
statement.executeVoid(frame);
}
}
}
@@ -0,0 +1,9 @@
package org.perl6.nqp.truffle.nodes;

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

public abstract class NQPIntNode extends NQPNode {
public void executeVoid(VirtualFrame frame) {
executeInt(frame);
}
}
2 changes: 2 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/nodes/NQPNode.java
Expand Up @@ -59,4 +59,6 @@ public long executeInt(VirtualFrame frame) {
public double executeNum(VirtualFrame frame) {
throw new MalformedAstException("Expected an AST node that produces a num");
}

abstract public void executeVoid(VirtualFrame frame);
}
Expand Up @@ -15,4 +15,8 @@ public NQPNotClosureNode(NQPCodeRef codeRef) {
public Object execute(VirtualFrame frame) {
return codeRef;
}

@Override
public void executeVoid(VirtualFrame frame) {
}
}
@@ -0,0 +1,9 @@
package org.perl6.nqp.truffle.nodes;

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

public abstract class NQPNumNode extends NQPNode {
public void executeVoid(VirtualFrame frame) {
executeNum(frame);
}
}
@@ -0,0 +1,9 @@
package org.perl6.nqp.truffle.nodes;

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

public abstract class NQPObjNode extends NQPNode {
public void executeVoid(VirtualFrame frame) {
execute(frame);
}
}
@@ -0,0 +1,9 @@
package org.perl6.nqp.truffle.nodes;

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

public abstract class NQPStrNode extends NQPNode {
public void executeVoid(VirtualFrame frame) {
executeStr(frame);
}
}
Expand Up @@ -62,4 +62,9 @@ public NQPIntArgNode(NQPNode valueNode) {
public Object execute(VirtualFrame frame) {
return valueNode.executeInt(frame);
}

@Override
public void executeVoid(VirtualFrame frame) {
throw new RuntimeException("IntArgNode shouldn't be used as void");
}
}
Expand Up @@ -48,12 +48,13 @@
import com.oracle.truffle.api.nodes.NodeInfo;

import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;

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

@NodeInfo(shortName = "invoke")
public final class NQPInvokeNode extends NQPNode {
public final class NQPInvokeNode extends NQPObjNode {

@Child private NQPNode functionNode;
@Children private final NQPNode[] argumentNodes;
Expand Down
Expand Up @@ -62,4 +62,9 @@ public NQPNumArgNode(NQPNode valueNode) {
public Object execute(VirtualFrame frame) {
return valueNode.executeNum(frame);
}

@Override
public void executeVoid(VirtualFrame frame) {
throw new RuntimeException("NumArgNode shouldn't be used as void");
}
}
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2012, 2015, 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.call;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;

import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;

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

@NodeInfo(shortName = "return int")
public final class NQPRetvalIntNode extends NQPObjNode {
@Child private NQPNode valueNode;

@Deserializer("retval-int")
public NQPRetvalIntNode(NQPNode valueNode) {
this.valueNode = valueNode;
}

@Override
public Object execute(VirtualFrame frame) {
long value = valueNode.executeInt(frame);
return value;
}
}
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2012, 2015, 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.call;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;

import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;

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

@NodeInfo(shortName = "return num")
public final class NQPRetvalNumNode extends NQPObjNode {
@Child private NQPNode valueNode;

@Deserializer("retval-num")
public NQPRetvalNumNode(NQPNode valueNode) {
this.valueNode = valueNode;
}

@Override
public Object execute(VirtualFrame frame) {
double value = valueNode.executeNum(frame);
return value;
}
}

0 comments on commit e393c03

Please sign in to comment.