Skip to content

Commit

Permalink
[truffle] Implement nqp::getcurhllsym, nqp::bindcurhllsym, nqp::gethl…
Browse files Browse the repository at this point in the history
…lsym, nqp::bindhllsym
  • Loading branch information
pmurias committed Aug 8, 2018
1 parent 979590f commit ee63d0e
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/vm/jvm/Truffle.nqp
Expand Up @@ -135,6 +135,12 @@ class QAST::OperationsTruffle {
});
}

add_simple_op('getcurhllsym', $OBJ, [$STR]);
add_simple_op('bindcurhllsym', $OBJ, [$STR, $OBJ]);

add_simple_op('gethllsym', $OBJ, [$STR, $STR]);
add_simple_op('bindhllsym', $OBJ, [$STR, $STR, $OBJ]);

add_simple_op('say', $STR, [$STR], :side_effects);
add_simple_op('print', $STR, [$STR], :side_effects);

Expand Down Expand Up @@ -672,7 +678,7 @@ class QAST::TruffleCompiler {

my $*BLOCK := BlockInfo.new(NQPMu, NQPMu);

TAST.new($OBJ, ['stmts', self.as_truffle($node[0][1], :want($VOID)).tree, self.as_truffle($node[0][3], :want($OBJ)).tree]);
TAST.new($OBJ, ['comp-unit', $node.hll, ['stmts', self.as_truffle($node[0][1], :want($VOID)).tree, self.as_truffle($node[0][3], :want($OBJ)).tree]]);
}

multi method as_truffle(QAST::VM $node, :$want) {
Expand Down
15 changes: 15 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/GlobalContext.java
@@ -0,0 +1,15 @@
package org.perl6.nqp.truffle;

import org.perl6.nqp.truffle.runtime.HLL;
import java.util.HashMap;

public class GlobalContext {
static GlobalContext SINGLETON = new GlobalContext();

public HashMap<String, HLL> hlls;

public GlobalContext() {
hlls = new HashMap<String, HLL>();
}
}

10 changes: 10 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/ManageScopes.java
Expand Up @@ -61,5 +61,15 @@ public static NQPScope declareLocal(NQPScope scope, long type, String name) {
public static NQPNode createDeclareLocal(NQPScope scope, long type, String name, NQPNode inner) {
return inner;
}

@Predeserializer("comp-unit")
public static NQPScope setupCompUnit(NQPScope scope, String hllName) {
return new NQPCompUnitScope(scope, GlobalContext.SINGLETON.hlls, hllName);
}

@Deserializer("comp-unit")
public static NQPNode compUnitContents(NQPScope scope, String hllName, NQPNode content) {
return content;
}
}

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

import org.perl6.nqp.truffle.runtime.HLL;
import java.util.HashMap;

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

import java.util.HashMap;

public class NQPCompUnitScope extends NQPScope {
NQPScope outer;
HLL currentHLL;
HashMap<String, HLL> hlls;

public NQPCompUnitScope(NQPScope outer, HashMap<String, HLL> hlls, String hll) {
this.outer = outer;
this.hlls = hlls;

if (!this.hlls.containsKey(hll)) {
hlls.put(hll, new HLL());
}

this.currentHLL = hlls.get(hll);

This comment has been minimized.

Copy link
@vendethiel

vendethiel Aug 8, 2018

Member

how performance-sensitive is this? wouldn't it make more sense to assign it to the new HLL() in the !containsKey case?

This comment has been minimized.

Copy link
@pmurias

pmurias Aug 8, 2018

Author Contributor

It's completely not performance-sensitive. It happens once per comp-unit compilation.

}

@Override
public HashMap<String, HLL> getHLLs() {
return hlls;
}

@Override
public HLL getCurrentHLL() {
return currentHLL;
}

@Override
public FrameSlot addLexical(String name) {
return outer.addLexical(name);
}

@Override
public FoundLexical findLexical(String name, int depth) {
return outer.findLexical(name, depth);
}

@Override
public FrameSlot addLocal(String name) {
return outer.addLocal(name);
}

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

import java.util.HashMap;
import org.perl6.nqp.truffle.runtime.HLL;

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

public abstract class NQPScope {
Expand All @@ -12,4 +15,6 @@ public FoundLexical findLexical(String name) {
public abstract FrameSlot addLocal(String name);
public abstract FrameSlot findLocal(String name);

public abstract HLL getCurrentHLL();
public abstract HashMap<String, HLL> getHLLs();
}
22 changes: 22 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/NQPScopeWithFrame.java
Expand Up @@ -3,6 +3,10 @@
import com.oracle.truffle.api.frame.FrameSlotKind;
import com.oracle.truffle.api.frame.FrameSlot;

import java.util.HashMap;

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

public class NQPScopeWithFrame extends NQPScope {

static class NQPLocalVariable {
Expand Down Expand Up @@ -70,5 +74,23 @@ public FrameSlot findLocal(String name) {
public FrameDescriptor getFrameDescriptor() {
return this.frameDescriptor;
}

@Override
public HLL getCurrentHLL() {
if (outer != null) {
return outer.getCurrentHLL();
} else {
throw new RuntimeException("Can't get current HLL");
}
}

@Override
public HashMap<String, HLL> getHLLs() {
if (outer != null) {
return outer.getHLLs();
} else {
throw new RuntimeException("Can't get current HLL");
}
}
}

@@ -0,0 +1,31 @@
package org.perl6.nqp.truffle.nodes.expression;
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.HLL;
import org.perl6.nqp.truffle.NQPScope;
import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "bindcurhllsym")
public final class NQPBindcurhllsymNode extends NQPObjNode {
private final HLL currentHLL;
@Child private NQPNode symbolNode;
@Child private NQPNode valueNode;

public NQPBindcurhllsymNode(HLL currentHLL, NQPNode symbolNode, NQPNode valueNode) {
this.currentHLL = currentHLL;
this.symbolNode = symbolNode;
this.valueNode = valueNode;
}

@Override
public Object execute(VirtualFrame frame) {
return currentHLL.bindSymbol(symbolNode.executeStr(frame), valueNode.execute(frame));
}

@Deserializer("bindcurhllsym")
public static NQPBindcurhllsymNode deserialize(NQPScope scope, NQPNode symbolNode, NQPNode valueNode) {
return new NQPBindcurhllsymNode(scope.getCurrentHLL(), symbolNode, valueNode);
}
}
@@ -0,0 +1,43 @@
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.runtime.HLL;
import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "bindhllsym")
public final class NQPBindhllsymNode extends NQPObjNode {
public final HashMap<String, HLL> hlls;
@Child private NQPNode hllNameNode;
@Child private NQPNode symbolNode;
@Child private NQPNode valueNode;

public NQPBindhllsymNode(HashMap<String, HLL> hlls, NQPNode hllNameNode, NQPNode symbolNode, NQPNode valueNode) {
this.hlls = hlls;
this.hllNameNode = hllNameNode;
this.symbolNode = symbolNode;
this.valueNode = valueNode;
}

@Override
public Object execute(VirtualFrame frame) {
String hllName = hllNameNode.executeStr(frame);
String symbol = symbolNode.executeStr(frame);
Object value = valueNode.execute(frame);

if (!hlls.containsKey(hllName)) {
hlls.put(hllName, new HLL());
}

return hlls.get(hllName).bindSymbol(symbol, value);
}

@Deserializer("bindhllsym")
public static NQPBindhllsymNode deserialize(NQPScope scope, NQPNode hllNameNode, NQPNode symbolNode, NQPNode valueNode) {
return new NQPBindhllsymNode(scope.getHLLs(), hllNameNode, symbolNode, valueNode);
}
}
@@ -0,0 +1,29 @@
package org.perl6.nqp.truffle.nodes.expression;
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.HLL;
import org.perl6.nqp.truffle.NQPScope;
import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "getcurhllsym")
public final class NQPGetcurhllsymNode extends NQPObjNode {
private final HLL currentHLL;
@Child private NQPNode symNode;

public NQPGetcurhllsymNode(HLL currentHLL, NQPNode symNode) {
this.currentHLL = currentHLL;
this.symNode = symNode;
}

@Override
public Object execute(VirtualFrame frame) {
return currentHLL.getSymbol(symNode.executeStr(frame));
}

@Deserializer("getcurhllsym")
public static NQPGetcurhllsymNode deserialize(NQPScope scope, NQPNode symbolNode) {
return new NQPGetcurhllsymNode(scope.getCurrentHLL(), symbolNode);
}
}
@@ -0,0 +1,40 @@
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.NQPScope;
import org.perl6.nqp.truffle.nodes.NQPNode;
import org.perl6.nqp.truffle.nodes.NQPObjNode;
import org.perl6.nqp.truffle.runtime.HLL;
import org.perl6.nqp.truffle.runtime.NQPNull;
import org.perl6.nqp.dsl.Deserializer;

@NodeInfo(shortName = "gethllsym")
public final class NQPGethllsymNode extends NQPObjNode {
public final HashMap<String, HLL> hlls;
@Child private NQPNode hllNameNode;
@Child private NQPNode symbolNode;

public NQPGethllsymNode(HashMap<String, HLL> hlls, NQPNode hllNameNode, NQPNode symbolNode) {
this.hlls = hlls;
this.hllNameNode = hllNameNode;
this.symbolNode = symbolNode;
}

@Override
public Object execute(VirtualFrame frame) {
String hllName = hllNameNode.executeStr(frame);
String symbolName = symbolNode.executeStr(frame);

if (!hlls.containsKey(hllName)) {
return NQPNull.SINGLETON;
}

return hlls.get(hllName).getSymbol(symbolName);
}

@Deserializer("gethllsym")
public static NQPGethllsymNode deserialize(NQPScope scope, NQPNode hllNameNode, NQPNode symbolNode) {
return new NQPGethllsymNode(scope.getHLLs(), hllNameNode, symbolNode);
}
}
26 changes: 26 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/truffle/runtime/HLL.java
@@ -0,0 +1,26 @@
package org.perl6.nqp.truffle.runtime;

import org.perl6.nqp.truffle.runtime.NQPNull;
import java.util.HashMap;

public final class HLL {
HashMap<String, Object> symbols;

public HLL() {
symbols = new HashMap<String, Object>();
}

public Object getSymbol(String key) {
Object value = symbols.get(key);
if (value == null) {
return NQPNull.SINGLETON;
} else {
return value;
}
}

public Object bindSymbol(String key, Object value) {
symbols.put(key, value);
return value;
}
}

0 comments on commit ee63d0e

Please sign in to comment.