deep

a Cross Development Platform for Java

User Tools

Site Tools


dev:crosscompiler:backend_arm:code_generator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev:crosscompiler:backend_arm:code_generator [2019/03/27 16:18] – [Code Generator for ARM] ursgrafdev:crosscompiler:backend_arm:code_generator [2022/12/20 11:34] (current) – [Accessing the Hardware] ursgraf
Line 1: Line 1:
 ====== Code Generator for ARM ====== ====== Code Generator for ARM ======
-Everything which is not fully implemented is listed below 
-^topic^I^L^F^remarks^ 
-|convLong|y|x|n| | 
-|convFloat|y|n|y| | 
-|convDouble|y|n|y| | 
-|instanceof| ||| | 
-|checkcast| ||| | 
-|throw| ||| | 
-|call| |||class & instance methods can be called| 
-|new| |||primitive array & array of objects & string implemented| 
-|exceptions| ||| | 
- 
 All results of all SSA instructions have an assigned register. Now, each SSA instruction can be translated into one or a sequence of machine instructions. In order to do this, we must define the stackframe, which is used when calling a method. All results of all SSA instructions have an assigned register. Now, each SSA instruction can be translated into one or a sequence of machine instructions. In order to do this, we must define the stackframe, which is used when calling a method.
  
Line 19: Line 7:
  
 Explanation: Explanation:
-LR is saved onto the stack together with the necessary nonvolatile GPRs. Even if method is a leaf method LR must be saved onto the stack because LR serves as a scratch register within a method. Considering the GPR's and EXTRs, all nonvolatile register, which are used within this method, must be saved on the stack. To simplify things, nonvolatiles in EXTR registers are considered to be of double precision. E.g. if a method uses three nonvolatile EXTR registers such as D31, S29, and S28 the prologue of the method will store D31, D30, and D29. \\+LR is saved onto the stack together with the necessary nonvolatile GPRs. Even if method is a leaf method LR must be saved onto the stack because LR serves as a scratch register within a method. Considering the GPR's and EXTRs, all nonvolatile register, which are used within this method, must be saved on the stack. Nonvolatiles in EXTR registers occupy either 8 bytes (EXTRD) or 4 bytes (EXTRS). \\
 Important: volatile EXTR's must be saved as well if ''US.ENABLE_FLOATS()'' is called in this method (see [[.:exceptions|Exceptions]]). Important: volatile EXTR's must be saved as well if ''US.ENABLE_FLOATS()'' is called in this method (see [[.:exceptions|Exceptions]]).
  
-The field //local variables// is only used if the number of registers does not suffice and locals must be assigned a slot on the stack. When dealing with FPR'some temporary space on the stack might be necessary. Some compiler specific subroutines dealing with //long// operations need some temporary space (//temp. memory//on the stack for saving and restoring some registers which are used therein \\  +The field //local variables// is only used if the number of registers does not suffice and locals must be assigned a slot on the stack. Variables of type //long// or //double// occupy two slots. \\ 
-The field //parameter// serves to hold parameters for method calls, which can not be placed directly into registers. The size of the field //parameters// is determined by considering all the calls to other methods within this method and taking the maximum size of their parameters. //parameters// must be at the top of the stack! This garantees that in all called methods these parameters can be accessed with the same offset. \\ +When calling interface methods some temporary space on the stack might be necessary (see below). \\  
 +The field //parameter// serves to hold parameters for method calls, which can not be placed directly into registers. The size of the field //parameters// is determined by considering all the calls to other methods within this method and taking the maximum size of their parameters. //parameters// must be at the top of the stack! This garantees that in all called methods these parameters can be accessed with the same offset. Each floating point parameter on the stack occupies 8 bytes (EXTRS or EXTRD).\\ 
 The stack pointer always points to the top of the actual frame. At the top the stack pointer of the caller has to be stored. The back chain pointer is used for the debugger, for exceptions and for the garbage collection. The stack pointer always points to the top of the actual frame. At the top the stack pointer of the caller has to be stored. The back chain pointer is used for the debugger, for exceptions and for the garbage collection.
 \\ \\
 ===== Exception Stackframe ===== ===== Exception Stackframe =====
-In case of an exception the registers LR, CTR, CR, XER, SRR0 and SRR1 must be additionally saved. Then follow all GPRs. In fact, it would be sufficient to store all the volatile GPR's and the nonvolatile GPR'which are used within this method. But //stmw// and //lmw// can be applied very efficiently but allow for storing a whole row only.\\  +In case of an exception all volatile GPRs together with LR must be saved. The scratch register R6 must be saved firstbecause it is used to save the original stack pointer before any other registers can be pushed. The nonvolatile GPRs which are used in this method must be saved as well. \\  
-FPR's need no saving, as they are normally not allowed to be used in exceptions (FP bit in MSR = 0). Here again, if ''US.ENABLE_FLOATS()'' is called in this method, the FPR's must be saved as well.+EXTR's need no saving, as they are not allowed to be used in exceptions. If an exception method calls a method where EXTR are used (e.g. in an interrupt handler or in a timer subclassyou have to use ''US.ENABLE_FLOATS()'' to store them.
  
-[{{ .:exceptionstackframe.png?250&direct | //Stack frame for exception method//}}]+[{{ :dev:crosscompiler:backend_arm:exceptionstackframearm.png?350&direct | //Stack frame for exception method//}}]
  
-Optimization: The PPC architecture has lots of FPR's. One could use half of them in normal methods and the other half in exception methods. For such a case all normal methods which could be called from within exception methods must be translated a second time with the second set. The compiler would have to find out how to handle each method.+For efficiency, the exception stack frame does not contain space for locals. Hence, you are not allowed to use so many locals that this becomes necessary. This will be checked for when compiling exception methods. Storage for interface methods as well as the area for parameter passing are not necessary as well. 
 + 
 + 
 +Optimization: The ARM architecture has lots of EXTR's. One could use half of them in normal methods and the other half in exception methods. For such a case all normal methods which could be called from within exception methods must be translated a second time with the second set. The compiler would have to find out how to handle each method.
  
 ===== Method Call ===== ===== Method Call =====
  
 ==== Parameter Passing ==== ==== Parameter Passing ====
-All parameters must be copied in the appropriate registers, see [[.:register_allocation|Register Allocation]]. During this it might be necessary that two or more registers must be swapped in a cycle. For this purpose two arrays //destGPR// and //destFPR// are determined. They show which source register goes into which destination register, if the register holds a parameter. If a cycle is found it will be solved through the aid of R0 or FR0, respectively.+All parameters must be copied in the appropriate registers, see [[.:register_allocation|Register Allocation]]. During this it might be necessary that two or more registers must be swapped in a cycle. For this purpose two arrays //destGPR// and //destFPR// are determined. They show which source register goes into which destination register, if the register holds a parameter.
  
 ==== Return Value ==== ==== Return Value ====
Line 47: Line 39:
   * Addresses of class variables   * Addresses of class variables
   * Addresses of constants (floats, strings, type descriptors)   * Addresses of constants (floats, strings, type descriptors)
-Such addresses must be loaded with the aid of an auxiliary (with //addi// and //addis//). After linking these addresses are known and must be corrected in the code. For this purpose a table called //fixups// is maintained. It contains all references to the objects, which were created by the class file reader and whose final addresses must be inserted into the code. In order to know at what position in the machine code array a certain correction has to be made, the next position is stored in the instruction //addi// as an immediate operand beginning with the last position (//lastFixup//).+Such addresses must be loaded with the aid of an auxiliary registers. After linking these addresses are known and must be corrected in the code. For this purpose a table called //fixups// is maintained. It contains all references to the objects, which were created by the class file reader and whose final addresses must be inserted into the code. In order to know at what position in the machine code array a certain correction has to be made, the next position is stored in the instruction //movw// as an immediate operand beginning with the last position (//lastFixup//).
  
 ===== Accessing the Hardware ===== ===== Accessing the Hardware =====
-Java does not allow direct access and manipulation of absolute memory locations. Nevertheless this is essential for embedded programming. We therefore include this possibility by using a special class //ch.ntb.inf.deep.unsafe.arm.US.java// (US stand for unsafe). Wenn methods of this class are used the code generator has to insert machine code directly. The register allocator does not have to allocate registers for this instructions. //US.java// therefore serves as simple stubs. +Java does not allow direct access and manipulation of absolute memory locations. Nevertheless this is essential for embedded programming. We therefore include this possibility by using a special class //org.deepjava.unsafe.arm.US.java// (US stand for unsafe). When methods of this class are used the code generator has to insert machine code directly. The register allocator does not have to allocate registers for this instructions. //US.java// therefore serves as simple stubs. 
  
 ===== Low Level Classes ===== ===== Low Level Classes =====
Line 56: Line 48:
  
 ===== Compiler Specific Subroutines ===== ===== Compiler Specific Subroutines =====
-Subroutines are methods for which there is no Java code (and hence no Bytecode or SSA) but only machine code. This is useful for the delegation of interface methods (see [[.:..:interfaces|Interfaces]]) or for dividing longs. \\+Subroutines are methods for which there is no Java code (and hence no Bytecode or SSA) but only machine code. This is useful for the delegation of interface methods (see [[.:..:interfaces|Interfaces]]) or for exception handling. \\
 Such methods are listed in //Method.compSpecSubroutines// and the code generator issues code (if subroutines are used). These subroutines must be linked as well. Such methods are listed in //Method.compSpecSubroutines// and the code generator issues code (if subroutines are used). These subroutines must be linked as well.
-Currently, there are three types of compiler specific methods +Currently, there are two types of compiler specific methods
-  * Arithmetic and conversion: call to this methods inserted by the code generator+
   * Delegation of interface methods: address of this method inserted into the type descriptor by the linker   * Delegation of interface methods: address of this method inserted into the type descriptor by the linker
   * Exception handling: call to this methods inserted by the code generator   * Exception handling: call to this methods inserted by the code generator
 +
 +==== Practical Implementation of Searching the Correct Interface Method ====
 +imDelegIiMm needs 3 auxiliary registers. At compile time we cannot reserve such auxiliary registers as these registers must always be the same. Therefore, we use a special chunk of memory on the stack. This storage is only necessary and allocated for methods invoking interface methods. At the start of the delegate method three volatile registers which might contain parameters are saved onto the stack and reloaded at the end of the delegate method.
dev/crosscompiler/backend_arm/code_generator.1553699883.txt.gz · Last modified: 2019/03/27 16:18 by ursgraf