Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

acme (189)

acme
  (email not shown publicly)
http://www.astray.com/

Leon Brocard (aka acme) is an orange-loving Perl eurohacker with many varied contributions to the Perl community, including the GraphViz module on the CPAN. YAPC::Europe was all his fault. He is still looking for a Perl Monger group he can start which begins with the letter 'D'.

Journal of acme (189)

Thursday May 02, 2002
10:48 AM

More parrot hackery

[ #4615 ]
I've been playing with Parrot some more recently, and thinking about converting Java bytecode to Parrot bytecode. My thoughts went something like this: Parrot is a register machine. The Java virtual machine is a stack machine. Parrot is also a stack machine (it's only a register machine for speed, there is still a stack). Instead of converting Java bytecode to Parrot bytecode, I can make Parrot into a JVM. I can achieve this by adding new JVM-like opcodes to Parrot (really quite trivial, all I coded up was a new jvm.ops file and reconfigured and typed "make". The 14 new opcodes where generated, added to Parrot, and the assembler automagically supported them.)

Have I confused you already? Well, let me explain it. Here is a piece of Java source code:

public static void spin() {
  int i;
  for(i = 0, i < 12345678; i++);
  System.out.print(i);
}

javac turns that into the following bytecode:

   0 iconst_0
   1 istore_0
   2 goto 8
   5 iinc 0 1
   8 iload_0
   9 sipush 1000
  12 if_icmplt 5
  15 getstatic #3 <Field java.io.PrintStream out>
  18 iload_0
  19 invokevirtual #4 <Method void print(int)>
  22 return

Now, after I've added my jvm.ops file to Parrot, I can code this in Parrot assembler as the following code:

# This is Parrot assembler:
      iconst_0
      istore_0
      goto    IN
REDO: iinc      0, 1
IN:   iload_0
      sipush    12345678
      if_icmplt REDO
      iload_0
      jvm_print
      end

... and the usual ./assemble.pl jvm.pasm > jvm.pbc; ./parrot jvm.pbc will compile and run the JVM-like Parrot opcodes.

That is, I've implemented "iconst_0" (a JVM bytecode which pushes zero onto the stack) and so on in Parrot, and Parrot is running JVM-like bytecode as well as Parrot bytecode (of course, I've only implemented a couple of JVM opcodes). This shows how flexible Parrot is, and is the method that will be used when multiple language target Parrot I imagine.

So I posted the code to perl6-internals and got little response back, apart from the "you're crazy" and "wow". It's a cute idea and it leads on to issues raised by Dan such as sharing objects between Java and other languages hosted on Parrot, or maybe doing wild JNI stuff. [Note: we can't go much further at the moment as Parrot doesn't have a symbol table, objects, methods, or method calls yet - but it will ;-]

This is all research for my Targeting Parrot talk at OSCON. Is there anything you'd like to know about targeting languages at Parrot? Ask away!

Anyway, off to a London.pm social meeting. I'm wearing my Parrot tshirt today...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • First up: Very Cool. However, I'm getting a bit lost on the hand waving here.

    What actually converts the JBC into PBC? Are you doing this by hand atm (i.e. converting JBC to parrot assembler, then assembling it,) or is there some form of automated system?

    As I understand it you're adding new op codes to the pvm so that you have a one to one translation for JBC and PBC. Doesn't this add more operations to the VM? Don't adding more operations slow down the VM implementation (because each operation takes

    • Yes, there is a lot of handwaving ;-)

      It was just weekend project, so I converted the JBC into PBC by hand. As I work on this more, I'll be using Java::JVM::Classfile to do it automatically.

      This does indeed mean adding more opcodes to Parrot. Parrot has around 600 opcodes already and I added 14. If I added all ~255 JVM opcodes, it would indeed increase the total number of Parrot opcodes, but the slowdown is pretty small. In fact, Parrot will have dynamically loadable opcode libraries, so in future this slo
      • Does this mean that you can have numerous different Parrot Byte Codes and a PBC file will have to say what encoding scheme (what opcodes mean what) it is?

        /me waves hands too