Skip to content

Commit

Permalink
Attempt to get a basic truffle build going
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Mar 24, 2023
1 parent 7dd3f9d commit 34be752
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
41 changes: 37 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,41 @@ lazy val bench = project
.settings(
publish := {},
publishLocal := {},
libraryDependencies ++=
Seq(),
publishArtifact := false)
.settings(coverageEnabled := false)
libraryDependencies ++= Seq(),
publishArtifact := false,
coverageEnabled := false
)
.enablePlugins(JmhPlugin)

lazy val pathToTruffleDsl = taskKey[String]("Path to truffle-dsl")

lazy val truffle = project
.dependsOn(core.jvm, cli)
.settings(moduleName := "bosatsu-truffle")
.settings(commonSettings)
.settings(
pathToTruffleDsl := {
val classpath = (Compile / dependencyClasspath).value
val myLibraryDependency = classpath.find(_.data.getName.contains("truffle-dsl-processor")).getOrElse(
throw new RuntimeException("truffle-dsl-processor dependency not found")
)
println(myLibraryDependency.data.getAbsolutePath())
myLibraryDependency.data.getAbsolutePath
},
javacOptions ++= Seq(
"-processorpath",
pathToTruffleDsl.value,
"-Xlint:processing"
),
libraryDependencies ++= Seq(
"org.graalvm.truffle" % "truffle-api" % "22.3.1",
"org.graalvm.truffle" % "truffle-dsl-processor" % "22.3.1",
"org.scalameta" %% "munit" % "0.7.29" % Test
),
run / fork := true,
javaOptions ++= Seq("-ea",
"--add-exports", "org.graalvm.truffle/com.oracle.truffle.api=ALL-UNNAMED",
"--add-exports", "org.graalvm.truffle/com.oracle.truffle.api.nodes=ALL-UNNAMED",
"--add-exports", "org.graalvm.truffle/com.oracle.truffle.api.staticobject=ALL-UNNAMED"
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.bykn.bosatsu.truffle;
import com.oracle.truffle.api.dsl.NodeChild;

@NodeChild("leftNode") @NodeChild("rightNode")
public abstract class AdditionNode extends BosatsuNode {
protected int addInts(int leftValue, int rightValue) {
return leftValue + rightValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.bykn.bosatsu.truffle;

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

public abstract class BosatsuNode extends Node {
public abstract int executeInt(VirtualFrame frame);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.bykn.bosatsu.truffle;

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

class BosatsuRootNode extends RootNode {
@SuppressWarnings("FieldMayBeFinal")
@Child
private BosatsuNode exprNode;

public BosatsuRootNode(BosatsuNode exprNode) {
super(null);

this.exprNode = exprNode;
}

@Override
public Object execute(VirtualFrame frame) {
return this.exprNode.executeInt(frame);
}
}
16 changes: 16 additions & 0 deletions truffle/src/main/java/org/bykn/bosatsu/truffle/IntNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.bykn.bosatsu.truffle;

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

public class IntNode extends BosatsuNode {
private final int value;

public IntNode(int value) {
this.value = value;
}

@Override
public int executeInt(VirtualFrame frame) {
return this.value;
}
}
15 changes: 15 additions & 0 deletions truffle/src/test/scala/org/bykn/bosatsu/truffle/TruffleTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bykn.bosatsu.truffle

class TruffleTest extends munit.FunSuite {
test("add 12+32") {
val exprNode = AdditionNodeGen.create(
new IntNode(12),
new IntNode(34));
val rootNode = new BosatsuRootNode(exprNode)
val callTarget = rootNode.getCallTarget()

val result = callTarget.call()

assertEquals(46, result);
}
}

0 comments on commit 34be752

Please sign in to comment.