[657] in java-interest
Re: Has the bytecode gone native?
daemon@ATHENA.MIT.EDU (Thomas Hickey)
Wed Jul 12 11:23:28 1995
From: hickey@oclc.org (Thomas Hickey)
Date: Wed, 12 Jul 1995 10:05:52 -0400
To: java-interest@java.sun.com
Given sbk@netcom.com's posting on generating machine code from
bytecode, I couldn't resist trying it myself. I took a class file
parser (written in Java, of course) and made it generate simple
C-macros for the code attributes. sbk's example:
class Main2 {
public static void main(String argv[]) {
int a[], b[], c[], i, j;
a = new int[10000];
b = new int[10000];
c = new int[10000];
for (j=0; j<1000; j++)
for (i=0; i<10000; i++)
a[i] = b[i]+c[i];
}
}
generates code that looks like:
#include "java.h"
void main(java_lang_String[]) {
STACK(5);
LOCAL_DEF(0);
LOCAL_DEF(1);
LOCAL_DEF(2);
LOCAL_DEF(3);
LOCAL_DEF(4);
LOCAL_DEF(5);
SIPUSH(10000);
NEWARRAY(int);
ASTORE_N(1);
SIPUSH(10000);
NEWARRAY(int);
ASTORE_N(2);
SIPUSH(10000);
NEWARRAY(int);
ASTORE_N(3);
ICONST_0();
ISTORE(5);
GOTO(57);
LABEL(24):
ICONST_0();
.
.
.
and then by writing a bunch of macro's, I get compilable C++ code.
Here are sbk's times for his version (no bounds checking, no
optimizations):
C, gcc2.5.6 -O : 2.0u 0.0s 0:02 86% 0+0k 0+0io 0pf+0w
translator: 10.0u 0.0s 0:10 94% 0+0k 0+0io 0pf+0w
java interpreter: 60.0u 0.0s 1:03 94% 0+0k 0+0io 0pf+0w
Actually my java interpreter (alpha3 on a Sparc-10 running SunOS 5.4) takes
about 68 seconds to run the example.
The C++ version (unoptimized) takes 11 seconds WITHOUT doing bounds
checking, but by adding a couple of simple peephole optimizations (e.g.
collapsing the (ALOAD_N, ILOAD, IALOAD) sequence into a single macro
and thereby avoiding some pushes and pops, the execution time is
reduced to 8 seconds using gcc -O, and 7 seconds with gcc -O3, both
WITH bounds checking done on the array accesses.
This code isn't handling exceptions, but they shouldn't really slow
it down. Obviously, generating the machine code directly has the
advantage of avoiding the C++ compile, but in terms of efficiency,
I don't see much difference, and the C++ wins in portability.
--Th
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com