2nd attempt at datatype-agnostic bytecode (via MethodHandles)

139 views
Skip to first unread message

Miguel Garcia

unread,
May 5, 2012, 4:28:10 PM5/5/12
to jvm-la...@googlegroups.com

After exploring the JSR-292 API, looks like dataflow-style keeps the code free from hardcoded signatures that expose datatypes (int, float, etc). It would be great to hear from  projects testing this approach. For example, adding three operands (either ints or floats) in a datatype-agnostic manner becomes:

@ForceInlining public final
MethodHandle add3(MethodHandle rand1,
                  MethodHandle rand2,
                  MethodHandle rand3) {
  return add2(add2(rand1, rand2),
              rand3);
}

@ForceInlining public final
MethodHandle add2(MethodHandle a,
                  MethodHandle b) {
  // assert: both operands have the same datatype (int or float)
  if(a.type().returnType() == INT) {
    int result = (   (int) a.invokeExact() // this triggers all side-effects of evaluating the first operand
                   + (int) b.invokeExact()
                  )
    return constantMH(result);
  } else {
    ... // ditto for float
}

The compilation scheme above does without reifying everything into MHs, e.g. loops, try-catch-finally, and all of switch, ternary operation, if-then stmt; can appear in their plain formulation. On the other hand, accesses to datatype-dependent fields and local vars has to be mediated via MHs.

I really hope the above can be made to run at native speed (after inlining and tracing).

P.S.: It would be great to have an API for the above besides the current one ;)

Miguel
http://lampwww.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/


Miguel Garcia

unread,
May 14, 2012, 5:26:27 AM5/14/12
to jvm-la...@googlegroups.com

I've concretized the ideas around using MethodHandles for "runtime specialization" (to avoid autoboxing, an aspect very close to our hears when compiling Scala code). It's all still experimental but the approach is definitely multi-language, comments are welcome:

Write-up: http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/2012Q2/RuntimeMP.pdf

Prototype: https://github.com/magarciaEPFL/MethodHandleUtils


Miguel
http://lampwww.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/


Reply all
Reply to author
Forward
0 new messages