develooper Front page | perl.perl6.internals | Postings from September 2001

JavaVM -> ParrotVM

From:
Leon Brocard
Date:
September 11, 2001 04:20
Subject:
JavaVM -> ParrotVM
Message ID:
20010911122041.A4122@ns0.astray.com
Seeing as Simon's gone to all the trouble of releasing the source to
Parrot now, I wondered what I could have a play with. I've did simple
Parrot assembler and thought about different types of bytecode.  Like
bytecode for the Java virtual machine, which it turns out is stack
based. Could I convert JVM->PVM? I think so ;-)

[Yes, this is a proof of concept, and the parrot assembler will change
in many ways rsn, so this is just a head up to show it's possible]

Given the following Java code:

class HelloWorld {
    public static void main (String args[]) {
	int i = 1;
	int j = 1;
	while(i < 10) {
	    System.out.print(i);
	    System.out.print(j);
	    i++;
	    j += i;
	}
    }
}

(which prints out: 112336410515621728836945)

That actually gets translated into the the following JVM bytecode:

   0 iconst_1
   1 istore_1
   2 iconst_1
   3 istore_2
   4 goto 28
   7 getstatic #2 <Field java.io.PrintStream out>
  10 iload_1
  11 invokevirtual #3 <Method void print(int)>
  14 getstatic #2 <Field java.io.PrintStream out>
  17 iload_2
  18 invokevirtual #3 <Method void print(int)>
  21 iinc 1 1
  24 iload_2
  25 iload_1
  26 iadd
  27 istore_2
  28 iload_1
  29 bipush 10
  31 if_icmplt 7
  34 return

And my magic script (http://astray.com/java2parrot.pl.txt)
converts it to the following Parrot assembler code:

	set_i_ic I1, 1
	set_i I63, I1
	set_i_ic I1, 1
	set_i I62, I1
	branch_ic LAAA
LAAB: 	set_i I1, I63
	print_i I1
	set_i I1, I62
	print_i I1
	inc_i I63
	set_i I1, I62
	set_i I2, I63
	add_i I1, I1, I2
	set_i I62, I1
LAAA: 	set_i I3, I63
	set_i_ic I4, 10
	lt_i_ic I3, I4, LAAB, LAAC
LAAC: 	end

which outputs:

I reg 1 is 1
I reg 1 is 1
I reg 1 is 2
I reg 1 is 3
I reg 1 is 3
I reg 1 is 6
I reg 1 is 4
I reg 1 is 10
I reg 1 is 5
I reg 1 is 15
I reg 1 is 6
I reg 1 is 21
I reg 1 is 7
I reg 1 is 28
I reg 1 is 8
I reg 1 is 36
I reg 1 is 9
I reg 1 is 45

Cool, huh. Much more on this soon! Leon
-- 
Leon Brocard.............................http://www.astray.com/
Iterative Software...........http://www.iterative-software.com/

... If you eat yogurt you'll have lots of culture



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About