deep

a Cross Development Platform for Java

User Tools

Site Tools


dev:crosscompiler:class_initialization

Differences

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

Link to this comparison view

Next revision
Previous revision
dev:crosscompiler:class_initialization [2014/11/03 13:31] – external edit 127.0.0.1dev:crosscompiler:class_initialization [2021/05/10 19:52] (current) ursgraf
Line 14: Line 14:
 </code> </code>
 ==== When will a class be initialized? ==== ==== When will a class be initialized? ====
-On the host we have a situation like this. The class constructor of a top-level class will be called first. Then follows the method //main//. The class constructor of an imported class will be called when:+On the host we have a situation as follows. The class constructor of a top-level class will be called first. Then follows the method //main//. The class constructor of an imported class will be called when:
   * accessing a static field of an imported class   * accessing a static field of an imported class
   * calling a static method of an imported class   * calling a static method of an imported class
Line 32: Line 32:
   }   }
 </code> </code>
-Class A starts initializing, //a1 = b1 + 1// causes //clinit// of class B to be executed after which //clinit// of class A finishes. //a1// will be assigned the correct value of 101, but //b2// will be assigned 1 instaed of 201.+Class A starts initializing, //a1 = b1 + 1// causes //clinit// of class B to be executed after which //clinit// of class A finishes. //a1// will be assigned the correct value of 101, but //b2// will be assigned 1 instead of 201.
 This error goes undetected. A user has to make sure that the initialization is done in a correct way.  This error goes undetected. A user has to make sure that the initialization is done in a correct way. 
  
 ==== Our Solution ==== ==== Our Solution ====
-Beim Linken sollen alle Top-Level Klassen als Parameter angegeben werden könnenDiese Liste wird von links nach rechts geparst und die Imports in der richtigen Reihenfolge aufgelöstWenn sich Probleme mit zyklischen Imports ergebensoll der Linker eine Warnung ausgebenIm ausführbaren Image sollen die Klassen der Importreihenfolge nach geordnet sein, sodass das Laufzeitsystem die Konstruktoren nacheinander aufrufen kannDas bietet auch die Möglichkeit, beispielsweise diese Konstruktoren im Supervisor-Modus auszuführen (Konfiguration von Hardwareund anschliessend in den User-Modus zu wechseln. It's important that the heap class and the kernel class are initialized first. For this they are put at the beginning of the list.\\ +Before compilation all top-level classes (also called root classes) can be specifiedThis list will be parsed from left to right and the imports will be resolved in this orderIf there are problems with cyclic importsthe linker will output a warningIn the target image the classes should be ordered in the same mannerThe kernel will call the class constructors accordingly. This would allow to call the constructors in the supervisor mode (configuration of hardwareand subsequently change to user mode\\ 
-In Java ist die Initialisierungsreihenfolge nicht klar definiertFalls Test auf dem Host und dem Target stets das gleiche Resultat ergeben sollenbietet sich folgende Lösung anIm Klassenkonstruktor einer obersten Hauptklasse (die ansonsten vollständig leer ist) werden alle anderen Klassenkonstruktoren aufgerufenBei dieser Lösung entfällt auch die Notwendigkeit einer Systemtabelle.+It's important that the heap class and the kernel class are initialized first. For this they are put at the beginning of the list.\\ 
 +Java does not define the order of initialization clearlyIf tests on the host and the target should lead to the same resultthe following solution would be appropriatethe class constructor of a otherwise empty helper class calls all the other class constructorsThis solution would also allow to get rid of the system table.
    
-===== Initialisierung von Objekten ===== +===== Initialization of Objects ===== 
-Die Initialisierung von Objekten wird in den Objektkonstruktoren zusammengefasst.+The initialization of objects is done in the object constructors.
 <code java> <code java>
   int len = 4;  // block 1   int len = 4;  // block 1
Line 49: Line 50:
   }   }
 </code> </code>
-Die Objektkonstruktoren heissen alle //init// im Bytecode und unterscheiden sich in der Parameterliste. Block 1 und werden in //init// zusammengefasst+In the Bytecode these constructors carry the name //init// and vary in the parameter list. Block 1 and block in the above code block will be combined in //init//. 
-Beim Erzeugen eines Objektes wird stets zuerst mit Hilfe von //new// ein Block auf dem Heap alloziertAnschliessend wird der Konstruktor der Oberklasse aufgerufen (dieser ruft seinerseits den Konstruktor der Oberklasse auf). Diese Aufrufe sind im Bytecode enthalten+When creating an object a call to //new// allocates a block on the heapNext, the constructor of the super class is called (which itself calls its super class constructor). These calls can be found in the Bytecode. 
-Klassen dürfen auch Instanzinitialisierer enthaltenz.B. +Classes might also have instance initializerssuch as: 
 <code java> <code java>
   int[] data = new int[10];     int[] data = new int[10];  
   {for (int i = 0; i < 10; i++) data[i] = i;}   {for (int i = 0; i < 10; i++) data[i] = i;}
 </code> </code>
-Auch diese Instanzinitialisierer befinden sich im Bytecode in den Objektkonstruktoren.+These initializers go into the object constructors as well.
  
dev/crosscompiler/class_initialization.1415017861.txt.gz · Last modified: 2016/02/25 13:33 (external edit)