Jeroen Frijters (pronounced Yeroon Frighters) delivered a paper called On the Fly Byte Code Generation at the 1999 Colorado Software Conference. He wrote classes for creating byte codes. His email address is jeroen@sumatra.nl and his website is http://weblog.ikvm.net/.
Probably the easiest way to understand byte codes is to write sample Java code that does what you want, then disassemble it. Then it should become clear what you need to generate. Sun now has some classes for assembling byte code: see sun.reflect.ClassFileAssembler.
| aastore | store a reference in an array, making sure the type of the reference is compatible with the run-time type of the array. |
| athrow | throw an exception. |
| checkcast | checks that a cast to another reference type is valid. |
| fadd | adds top two floats on the stack and replaces them with the result. The JVM does not have a separate floating point stack. |
| goto | At the byte code level there has to be a goto to implmement if/else branching. |
| iadd | adds top two ints on the stack and replaces them with the result. |
| iload | push the value of a local integer variable to the top of the stack. |
| iconst | push an integer literal to the stack. |
| instanceof | replaces reference on top of stack with a boolean. |
| invokeinterface | call a method when all you have is an interface reference to it. |
| invokespecial | née invokenonvirtual in JDK 1.0. Call a private instance method, and final instance methods where you can tell ahead of time which variant of the method you want. Also used to call clinit, the code to initialise a new object prior to the constructor running. Also used to call super. someMethod, where you also know the class of the method to invoke at compile time. |
| invokestatic | call a static method. |
| invokevirtual | call a non-final instance method. |
| lookupswitch | used when switch case labels are not dense. Searches for a matching key and jumps. |
| new | allocates RAM for objects. |
| pop | drops the top element of the stack. |
| swap | exchanges the top two elements of the stack. |
| tableswitch | used when switch case labels are dense. Indexes into table for offset. |
![]() |
recommend book⇒Java(TM) Virtual Machine Specification, The 2nd Edition | |
| paperback | ||
|---|---|---|
| ISBN13: | 978-0-201-43294-7 | |
| publisher: | Prentice Hall | |
| published: | 1999-04-24 | |
| by: | Tim Lindholm and Frank Yellin | |
| Make sure you get the second edition. This book has an unusual copyright notice. You might want to read a different book if you plan to write a clean-room JVM. Read the book online. | ||
![]() |
recommend book⇒The Java Virtual Machine | |
| paperback | ||
|---|---|---|
| ISBN13: | 978-1-56592-194-8 | |
| publisher: | O’Reilly | |
| published: | 1997-04 | |
| by: | Joshua Engel | |
| Known known as the goldfish bowl book because of its cover. The book is frustrating because it spends so much time with the irrelevant Jasmin assembler and its syntax. You are interested in generating byte codes directly, not assembler. It leaves out much you must discover by experiment looking at generated class files, such as whether offsets are signed or absolute, where the base is etc. In its next revision, it should set the Jasmin aside in an appendix, and include examples and more precise documentation on the binary formats. The book is still valuable because it gives a fair bit of background exposition you will not find in the vmspec itself. You would use this book to understand the VM, then the vmspec to actually write code that generated or modified class files. | ||
![]() |
recommend book⇒Programming For The Java Virtual Machine | |
| paperback | ||
|---|---|---|
| ISBN13: | 978-0-201-30972-0 | |
| publisher: | Addison-Wesley | |
| published: | 1999-07-02 | |
| by: | Joshua Engel | |
| From the reviews it looks like it too gets hung up on a propietary assembler called Oolong. He does provide a code-generating class and spends one chapter on using it. I think the book would have been more useful if all the examples were in code-generator code, rather than an obscure dialect of assembler. | ||
![]() |
recommend book⇒Inside The Java Virtual Machine | |
| paperback | ||
|---|---|---|
| ISBN13: | 978-0-07-135093-8 | |
| publisher: | McGraw-Hill | |
| published: | 2000-01-06 | |
| by: | Bill Venners | |
| Covers Java 2 JVM internals. ISBN of first edition was 0-07-913248-0. See the blurb. | ||
| You can get the freshest copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | |
| http://mindprod.com/jgloss/jasm.html | J:\mindprod\jgloss\jasm.html | |
![]() | ||
| Canadian Mind Products | ||
| mindprod.com IP:[65.110.21.43] | ||
| view Blog | Your face IP:[38.107.191.109] | |
| Feedback | You are visitor number 24,647. | |