Simple dynamic language using invokedynamic
Rémi Forax
forax at univ-mlv.fr
Sat Jun 27 07:37:32 PDT 2009
I've played a little with Juby just to see if the backport is able to
work with JRuby env.
It takes me some times to setup the environment, discovered how ruby
gems works,
use the right version of jruby, etc :)
Ok, the backport works even if I've discovered
that current recompilation thresholds are harmful.
Two remarks on Juby :
First, I think there is an error in the way you register the bootstrap
method,
in compiler.rb::boostrap you do generate a code that do a
Class.forName() on
a class name containing 'slash' and not 'dot'.
(btw a LDC on a class in possible since 1.5).
About the generated code, fib looks like something like that :
ACONST_NULL
ALOAD 0
LDC 1
INVOKESTATIC java/lang/Long.valueOf(J)Ljava/lang/Long;
INVOKEDYNAMIC -(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEDYNAMIC
fib(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ACONST_NULL
ALOAD 0
LDC 2
INVOKESTATIC java/lang/Long.valueOf(J)Ljava/lang/Long;
INVOKEDYNAMIC -(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEDYNAMIC
fib(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEDYNAMIC +(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
I think the code can be improved, the idea is that the descriptor of an
invokedynamic
can better reflect the knowledge on the compiler,
because 1 and 2 are constant corresponding invokedynamic calls should
take a long
as 2th parameter type.
ACONST_NULL
ALOAD 0
LDC 1 // or LCONST...
I2L
INVOKEDYNAMIC -(Ljava/lang/Object;J)Ljava/lang/Object;
INVOKEDYNAMIC
fib(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ACONST_NULL
ALOAD 0
LDC 2
I2L
INVOKEDYNAMIC -(Ljava/lang/Object;J)Ljava/lang/Object;
INVOKEDYNAMIC
fib(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEDYNAMIC +(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
This will allow the runtime to do more optimisations.
cheers,
Rémi
Charles Oliver Nutter a écrit :
> I thought I'd try building a simple Ruby-like dynamic language that uses
> all Java types and invokedynamic. Turns out it's only a couple hour job.
>
> http://gist.github.com/129973
>
> This is "Juby", a language that simply compiles Ruby's syntax to Java's
> type system. All calls are made using invokedynamic. Operators are also
> calls, and there's some special-cased logic in them to bootstrap
> appropriate utility methods for doing +, ==, etc. Other than primitive
> logics like "puts" compiling as a System.out.println, all calls are made
> through invokedyanamic. This makes the bytecode trivial to construct,
> and only requires a bit of work on the bootstrap side.
>
> http://github.com/headius/juby/tree/master
>
> A few points to note:
>
> * The first dyncall at a call site uses reflection to get an exact match
> for the incoming *runtime* argument types, but unreflects and uses the
> resulting handle for both the first invocation and all subsequent
> invocations. Handles all the way down!
> * The handle recovered by reflection has only convertArguments applied,
> since in this language there are not yet any implicit coercion rules
> (there must be an exact match on the target type).
> * collectArguments is being used to allow a single "fallback" path for
> the first time through.
> * Once the correct target method is installed, no further checks are
> made; this means that if the method is polymorphic, it would blow up
> currently.
>
> The compiler is trivial, since it just uses invokedynamic and then
> normal Java logic. If you want to have a look, go ahead...it's a good
> example of BiteScript in action.
>
> The interesting bit for you folks may be
> src/com/headius/juby/SimpleJavaBootstrap.java.
>
> It's obviously not covering a lot of cases, but it's a good example of
> how trivially one can write a dynlang atop invokedynamic. It took me
> literally only a couple hours to do this, aided by JRuby and BiteScript.
>
> This week I hope to get all the new handle types wired into JRuby, and
> then I will cover the JRuby indy conversion process and Juby in a new
> blog post. And assuming indy can be made as fast as interface impls, we
> may build Juby out as an implementation language for parts of JRuby.
>
> - Charlie
>
> _______________________________________________
> mlvm-dev mailing list
> mlvm-dev at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
>
More information about the mlvm-dev
mailing list