3 ms·
Truffle makes it possible to implement a JITed dynamically-typed language simply by annotating actions on the AST. http://www.graalvm.org/docs/graalvm-as-a-pla
by chrisaycock 9y ago
Truffle makes it possible to implement a JITed dynamically-typed language simply by annotating actions on the AST.
http://www.graalvm.org/docs/graalvm-as-a-platform/implement-language/ http://www.graalvm.org/docs/graalvm-as-a-platform/implement-...
Here's an example of the addition operator from Graal's SimpleLanguage:
@NodeInfo(shortName = "+")
public abstract class SLAddNode extends SLBinaryNode {
@Specialization(rewriteOn = ArithmeticException.class)
protected long add(long left, long right) {
return Math.addExact(left, right);
}
@Specialization
@TruffleBoundary
protected SLBigNumber add(SLBigNumber left, SLBigNumber right) {
return new SLBigNumber(left.getValue().add(right.getValue()));
}
@Specialization(guards = "isString(left, right)")
@TruffleBoundary
protected String add(Object left, Object right) {
return left.toString() + right.toString();
}
protected boolean isString(Object a, Object b) {
return a instanceof String || b instanceof String;
}
}
https://github.com/graalvm/simplelanguage https://github.com/graalvm/simplelanguage
By default it uses an arbitrary precision adder. But node rewriting on the AST (predicated by the "guard") provides specializations for long and String types. The long-type execution is a "fast path" that executes machine instructions directly; the String-type execution is a string concatenation.
A research paper provides further details:
https://dl.acm.org/citation.cfm?id=2658776 https://dl.acm.org/citation.cfm?id=2658776
The full tutorial is at:
https://github.com/oracle/graal/blob/master/truffle/docs/LanguageTutorial.md https://github.com/oracle/graal/blob/master/truffle/docs/Lan...
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- chc4 9y agoSomeone I know wrote a Java implementation of the small VM for an open source project that had a simple C interpreter. My first thought was "wait, why?", until they mentioned that it was more than 10x faster and much, much easier to write and extend. You basically get a very good JIT engine for free. Not gonna underestimate Truffle after that.
- grashalm 9y agoWhich one was it? Can you remember? One of these? https://github.com/oracle/graal/blob/master/truffle/docs/Languages.md https://github.com/oracle/graal/blob/master/truffle/docs/Lan... If not. Please let me know.
- chc4 9y agoIt's not listed there, and didn't reach full feature parity (and seems to be dead now). https://github.com/frodwith/jaque https://github.com/frodwith/jaque is the project though, which is an implementation of the Nock VM from Urbit
- grashalm 9y agoHo wow, never heard of it. Will put it on the experiments list. Thanks a lot!
- urbit 9y agoDefinitely not dead -- it got stalled for a while because Urbit tends to use deep stacks and both Graal and the JVM seem to favor shallow ones. The author (not me) came up with a "meta-stack" design that may mitigate this issue and will go back and try it soon. But the performance seen on a trivial Ackerman function was very good -- Nock via Graal got close to C performance. That's impressive for what's basically a Lisp.
- frodwith 9y agoHi, author of jaque here. I've been working on some other things for a little while, but I'll be getting back to jaque soon. It's quite out of date and needs to be brought up to 1.0 and cleaned up a bit before I start grousing about the stack issues I was running into on the mailing list, but when I last was working on it I was running into issues with Graal when trying to increase the java stack size. Deep but bounded head recursion is quite common in nock, and I seem to recall I needed about 8 megs of stack space (I just passed -Xss 8m or something on the command line) and Graal slowed waaaay down. That's where I left it.