A native-code Java compiler in Forth 
Author Message
 A native-code Java compiler in Forth

After reading the Java Virtual Machine specification, it struck me that since
both Forth and Java are largely stack-based, it should be relatively easy to get
Java bytecode "converted" on the fly to Forth, and then run in a threaded
fashion. The main points that I am unsure about are:

-Garbage Collection. Is there anything out there that uses this already? Forth
probably isn't the kind of language where this is really needed, but Java
requires it.

-exceptions. Is the ANS Exception word set pretty common? Ie would a generic
C-coded PC version be hard to come by?

-fixups. The VM needs to ensure type safety, which can largely be done at load
time, but certain operations can only be checked at run time. Sun, in thier VM
spec, mentions optimizations that involve overwriting the opcodes with
(transparent) less costly ones. Can somebody give me an working example of this?
ie, I want to do:

\ this code would be hand coded

: invokevirtual ( _constant_ref -- item ) \ load constant data
  ( check that constant[_constant_ref] is a class )
  ( check that the class is loaded )
  { check that the virtual method exists )

: invokevirtual_quick ( _constant_ref -- item )
  ( do normal processing )

\ this code would be machine generated from Java .class file (bytecode)

: java-method ( -- )  \ method parameters are passed into local variables
  <op> <op> invokevirtual <op> ...

Outline of JVM and why I'm thinking Forth:
Java, a C++-like language compiles into JVM bytecode in a .class file. The file
contains a header portion defining some attributes and some constants, mainly
the names of classes, methods, etc. used. The bytecode is mostly one byte
opcodes, some with operands, but the operations are mainly stack-based. Runtime
type checking of objects only is defined in the spec and provided for in the
virtual machine. try/finally and try/except blocks are provided for. Garbage
collected objects and array referencing operators make pointers unnecessary in
the JVM. All integers are stored in big-endian order. The stack is 32-bits wide.
Floating point arithmetic is done with IEEE-754 numbers. (does this have any
significance to a Forth implementation? ie Do recent Intel processors use
My plan is to build a disassembler, then make words for each of the opcodes that
act as necessary. Current implementations of Java use a switch statement to
dispatch on opcodes.
Only one namespace for the Forth code is necessary, so I can handle that. I
haven't checked in great detail, but I think objects could be reference-counted
Jump opcodes are another tricky part I haven't resolved in my mind yet.

Any help appreciated,

-Tony Lownds

Sun, 05 Jul 1998 03:00:00 GMT  
 A native-code Java compiler in Forth

Why ist it, that everyone who encounters garbage collection for the first
time, thinks that reference counting will better do the trick ? You should
take a look at a recent thread on c.l.lisp and see why this assumption is

What's the scope of your project ? You only want to provide the Java VM ?
Right ?
For inclusion in what ? A browser of your own ? A general VM to execute
Java applets within ?

Obviously, you don't seem to be interested in getting Java source code and
compile the corresponding byte-codes ?

Maybe your best bet would be to make a token-threaded forth, where all the
opcode of Java byte-code would magically happen to be the token of the
corresponding forth word ?
Finaly beware that Java does support 64 bit integers, and that making the
stack 32 bits wide is not a so clear cut choice... Finally, since Java is
a more ``traditional'' language than Forth, with respect to algebra, the
choice of making a unique stack for both integer and floating points or
separate stacks plus special opcodes to convert back and forth is not so
clear either... maybe a unique stack with 80 bits cells
(sizeof(IEEE-754-double)) ?


Mon, 06 Jul 1998 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Re(try): A native-code compiler in Forth

2. Linking Forth to Java Native Class?

3. CRC-32 native code for VFX Forth

4. Forth-like Simplifications for Native One-Stack Code

5. F68K - a native code Forth for 68000

6. Forth to native code generation; one more iteration

7. Native code compilers

8. QuickSort(), native code, Force compiler

9. What is a 'native code' compiler

10. public relase of HiPE native-code Erlang compiler

11. XDS native code compiler for NT

12. XDS native code Modula/Oberon compilers: DEMO is available for MS-DOS


Powered by phpBB® Forum Software