Comment

Comment by chrisaycock

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-...

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

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

The full tutorial is at:

https://github.com/oracle/graal/blob/master/truffle/docs/Lan...

parent chrisaycock 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton