Skip to content

Commit

Permalink
Argument evalutor nodes example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Mar 26, 2019
1 parent 2b6bf35 commit f316b42
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
48 changes: 42 additions & 6 deletions src/main/java/org/apidesign/demo/talk2compiler/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.apidesign.demo.talk2compiler;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;

public class Main extends RootNode {
static final Main MAIN = new Main();
static final CallTarget CODE = Truffle.getRuntime().createCallTarget(MAIN);

@Child
private Compute compute;

private Main() {
super(null);
}
Expand All @@ -21,12 +24,45 @@ public static void main(String... args) {

@Override
public Object execute(VirtualFrame frame) {
final String name = (String) frame.getArguments()[0];
return formatGreeting("Hello from %s!", name);
final int[] name = (int[]) frame.getArguments()[0];
return compute.compute(name);
}

void setProgram(Compute program) {
compute = insert(program);
}

@TruffleBoundary
private static String formatGreeting(String msg, String name) {
return String.format(msg, name);
public static abstract class Compute extends Node {
public abstract int compute(int[] arr);
}

public static final class Plus extends Compute {
@Child Compute left;
@Child Compute right;

public Plus(Compute left, Compute right) {
this.left = left;
this.right = right;
}

@Override
public int compute(int[] arr) {
int leftValue = left.compute(arr);
int rightValue = right.compute(arr);
return leftValue + rightValue;
}
}

public static final class Arg extends Compute {
private final int index;

public Arg(int index) {
this.index = index;
}

@Override
public int compute(int[] arr) {
return arr[index];
}
}
}
13 changes: 10 additions & 3 deletions src/test/java/org/apidesign/demo/talk2compiler/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
public class MainTest {
@Before
public void warmingUp() {
Main.Plus program = new Main.Plus(
new Main.Plus(new Main.Arg(0), new Main.Arg(1)),
new Main.Arg(2)
);
Main.MAIN.setProgram(program);

int count;
if (Boolean.getBoolean("noigv")) {
// Skip warmup if IGV dump isn't requested
count = 1;
} else {
count = 1000000;
count = 10000000;
}
for (int i = 0; i < count; i++) {
sayHelloTruffle();
Expand All @@ -21,10 +27,11 @@ public void warmingUp() {

@Test
public void checkSayHello() {
Assert.assertEquals("Hello from Truffle!", sayHelloTruffle());
Assert.assertEquals(7 + 8 + 2, sayHelloTruffle());
}

private static Object sayHelloTruffle() {
return Main.CODE.call("Truffle");
final Object arr = new int[] { 7, 8, 2, 4 };
return Main.CODE.call(arr);
}
}

0 comments on commit f316b42

Please sign in to comment.