Translating Java Bytecode to X86 Assembly Code

Translating Java Bytecode to X86 Assembly Code

Translating Java bytecode to X86 assembly code Chun-Yuan Chen, Zong Qiang Chen, and Wuu Yang National Chiao-Tung University, Hsin-Chu, Taiwan, R.O.C. Abstract An alternative is to create and manipulate objects purely in assembly code. This alternative approach may result in ABSTRACT better performance. However, all the facilities provided by Java has become one of the most popular languages for JVM cannot be utilized easily. network applications. The main drawback of Java is con- cerned with the execution speed due to interpreted execu- tion. We implemented a translation system that converts Our approach is an off-line translator, which allows the Java bytecode to X86 assembly code. The main approach possibilities of thorough optimizations. In contrast, the just- of our translation system is that objects are still created in-time (JIT) compiler is an run-time translator, which can and manipulated inside the (run-time) Java virtual ma- spend but a little time on optimization. Furthermore, the chine. Other ordinary operations are translated into as- compilation result of JIT is not saved across execution. This sembly code. Some pattern-based optimizations are applied means that repeated execution of the same bytecode incurs during the translation process. Faster execution of Java repeated JIT compilation, which is a serious overhead for programs will certainly make Java more attractive to net- frequently used programs. Many other research results are work applications. also focused on improving Java’s performance. KEY WORDS Java, assembly, translation, JIT, compilation, network ap- Java virtual machine is a stack machine. We use the na- plication tive stack in the X86 architecture to emulate the operand stack in JVM. Objects are allocated inside JVM heap. Aux- 1 iliary data structures created by our translator are allocated in the .data section. Constants, which are used in, for instance, the ldc instructions, in the constant pool of a 1 Introduction class file are also allocated in he .data section. Reg- isters in X86 architecture are used for scratch registers. Java has become one of the most popular languages for network applications. The main drawback of Java is con- cerned with the execution speed due to interpreted execu- Our translator first scans the bytecode file to extract tion. In order to speed up the execution of a Java pro- relevant information. The translator builds several tables gram, we implemented a translation system that converts for calculating addresses and for generating assembly in- Java bytecode to X86 assembly code. structions. In the simplest case, a bytecode instruction The main approach of the translation system is that ob- is translated into one or more assembly instructions. On jects are still created and manipulated inside the (run-time) the other hand, certain patterns—a sequence of 2 or more Java virtual machine (JVM). This approach avoids the bur- bytecode instructions—are recognized by our translator and den of garbage collection and most of the Java run-time se- they are translated into more efficient assembly instruction curity facilities are available. The overhead of this approach sequences. is that the resulting assembly program still makes frequent calls to JVM functions (through Java Native Interface). The rest of the paper is organized as follows: Section 1The work reported here is supported by National Science Coun- 2 introduces operations for creating and manipulating ob- cil, Taiwan, R.O.C., under grant NSC 93-2213-E-009-092. Due to jects. In section 3, we present a template-based optimiza- the page limit of the conference, the sections on exceptions and ordi- tion technique. Section 4 compares the performance of our nary instructions and a complete list of references are omitted. A 36- page full paper is available on the web at www.cis.nctu.edu.tw/ wu- system with that of Java interpreter and Just-In-Time (JIT) uyang/papers/bytecode2X86.FULL.pdf. compiler. 2 Operations on Objects 2.3 getfield and putfield 2.1 Starting Up a JVM To access an instance variable i of object x (whose class is fieldexample) is done by the getfield and putfield instructions in bytecode. In creating and manipulating objects, we need to use the services provided JVM. Therefore, we start up a JVM at the fieldexample x=new beginning of the resulting assembly program. The assembly fieldexample(); program obtains a reference to the JVM when a JVM starts x.i++; up. This reference is used to invoke JNI functions. Note that our translator allows mixed execution of assembly code and The corresponding bytecode is as follows: bytecode. JNI functions are the interface between the two aload 1 ; x forms of code. dup getfield #2 ; <Field int i> 2.2 Creating an Instance of a Class iconst 1 iadd The following Java code for creating an instance of putfield #2 ; <Field int i> a class is translated into the corresponding bytecode se- In order to translate the two bytecode instructions, an quence: area in .data is reserved of which the layout is follows StringBuffer x = new index 2 clsname db StringBuffer(100); ’fieldexample’, 0 index 2 cls dword ? new #2 index 2 fidname db ’i’, 0 dup index 2 fidsig db ’I’, 0 bipush 100 index 2 fid dword ? invokespecial #3 argreversebuf dword 20 dup(?) astore 1 Since the created object resides in the heap of the JVM, where #2 and #3 are the entries of the con- we need to use JNI functions to access the fields of an ob- stant pool java/lang/StringBuffer and ject. java/lang/StringBuffer<init>(I)V, re- The instructions getstatic and putstatic are spectively. similar to getfield and putfield. The differ- In order to translate these bytecode instructions, we re- ence is that the call to JNI functions GetFieldID, serve an area in the .data area of which the layout is as Get<Type>Field and Set<Type>Field are replaced follows: by GetStaticFieldID, GetStatic<Type>Field and SetStatic<Type>Field, respectively. Further- index 2 clsname db more, we use the jclass reference, instead of the object ’java/lang/StringBuffer’, 0 reference, to access the static fields. index 2 cls dword ? index 3 midname db ’<init>’, 0 2.4 instanceof index 3 midsig db ’(I)V’, 0 index 3 mid dword ? --pointer to The bytecode instanceof checks the class of an ob- the constructor ject. argreversebuf dword 20 dup(?) if (a instanceof ABC) . Note that the number 3 in the names index 3 mid, etc. is determined by the the #3 argument in the bytecode in- aload 1 ; a struction invokespecial #3. The same convention is instanceof #2 ; <Class ABC> followed through the generated X86 assembly code. ifeq 15 Then 3 JNI functions—FindClass, GetMethodID, NewObject—are called to create an instance. The created The instanceof instruction is implemented with the object resides in the heap of the JVM, not in the assembly JNI function IsIntanceOf. The result (1 for yes and 0 code. The result is a pointer to the instance. for no) is pushed back on the stack. 2.5 Calling a Method push offset aa objmapclass push eax ; object reference is Java dynamically dispatches instance methods. Dynamic saved in eax dispatching is implemented in X86 assembly code. 2 call search objmapclasstable There is one allclassmethodtable in the transla- tor. Its layout is as follows: The search objmapclasstable procedure seaches the aa objmapclass table and returns the idofclass classname idofclass superclass of the created object. The translator then searches the methodname signature classname allclassmethodtable with the method name and the nameInasm signature. The result is a set of methods that match the methodname signature classname name and signature of the called method and that depend nameInasm on the idofclass as the entry point. According to this set, ... the translator generate a block of assembly code that selects Aclass 3 Bclass the appropriate method from this set of methods. xMethod xMethod-sig Aclass A xMethod 1 For instance, suppose that class A is a subclass of class yMethod yMethod-sig Bclass B yMethod 1 B. Both A and B define a method called show. The show zMethod zMethod-sig Bclass B zMethod 2 method in A is renamed A show 2 while that in B is re- Bclass 7 java.lang.Object named B show 2 in the assembly code. The index of the yMethod yMethod-sig Bclass B yMethod 1 entry of class A in the constant pool is 2 while that of B zMethod zMethod-sig Bclass B zMethod 2 is 18. When a method myObj.show(. .) is exe- cuted, the translator first determines the actual class of the The allclassmethodtable lists all the classes in a object stored in myObj and then finds the index (stored in program and all the methods defined in each class, includ- the eax register) of the entry of that class in the constant ing all the methods inherited from the superclasses. This pool and generates the following X86 code: table serves the purpose of dynamically dispatching an ap- propriate method. .if eax == 2 In order to invoke a method, a structure, called the call A show 2 objmapclass table, is reserved in the assembly code: mov eax, 2 .endif objmapclass table struct .if eax == 18 jobj dword ? call B show 2 class dword ? mov eax, 18 objmapclass table ends In summary, for the invokevirtual and aa objmapclass objmapclass table invokeinterface instructions, the translator uses 30 dup(<-1, -1>) ; the method name and method signature from constant The jobj field is a reference to the object and the class pool table to search the allclassmethodtable for field is the index of the entry of the object in the constant the appropriate methods, and generates assembly code for pool.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us