Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| software:deep:dev:crosscompiler:arrays [2013-12-16 11:41] – Externe Bearbeitung 127.0.0.1 | software:deep:dev:crosscompiler:arrays [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== Arrays ====== | ||
| - | The following figure demonstrates how arrays are implemented in the memory. | ||
| - | [{{ .: | ||
| - | An array extends // | ||
| - | The field //length// denotes the number of elements of the array (16 bit). //heap// is used for the garbage collection ([[deep: | ||
| - | When accessing array elements the code generator inserts code to check if the element index is in the range 0 to // | ||
| - | If the field //length// is used in Java the Bytecode instruktion // | ||
| - | ===== Generating Arrays ===== | ||
| - | The Java code | ||
| - | <code java> | ||
| - | static int[]a1 = new int[4]; | ||
| - | </ | ||
| - | will be translated into Bytecode | ||
| - | < | ||
| - | 0 iconst_4 | ||
| - | 1 newarray int | ||
| - | 3 putstatic Test.a1 : int[] | ||
| - | </ | ||
| - | When regular objects are created the constructor of the supertype is automatically inserted into the Bytecode. When arrays are created there is no such call. The constructor of //Object// has to be inserted by the code generator and the heap manager has to add the size of // | ||
| - | In the Bytecode // | ||
| - | \\ | ||
| - | For | ||
| - | <code java> | ||
| - | static Object[] a1 = new ClassA[4]; | ||
| - | </ | ||
| - | the Bytecode looks like: | ||
| - | < | ||
| - | 0 iconst_4 | ||
| - | 1 anewarray ch/ | ||
| - | 4 putstatic ch/ | ||
| - | </ | ||
| - | Here, as well, the reference to //[ClassA// has to be fetched from the object directory when generating the SSA. There are cases when there is no entry for // | ||
| - | |||
| - | ===== Mehrdimensionale Arrays ===== | ||
| - | Betrachten wir den folgenden Fall | ||
| - | <code java> | ||
| - | int[][] a = new int[2][3] | ||
| - | </ | ||
| - | Im Speicher ergibt sich folgende Situation | ||
| - | [{{ .: | ||
| - | [[I und [I sind die entsprechenden Typdescriptoren. | ||
| - | Bei einem Arrayzugriff muss also zuerst in der ersten Dimension eine Referenz geladen werden und damit ein zweiter Arrayzugriff gemacht werden. | ||
| - | |||
| - | ==== Erzeugung von mehrdimensionalen Arrays von Basistypen ==== | ||
| - | Der folgende Code | ||
| - | <code java> | ||
| - | short[][] a = new short[2][3] | ||
| - | </ | ||
| - | führt auf den Bytecode | ||
| - | < | ||
| - | 0 iconst_2 | ||
| - | 1 iconst_3 | ||
| - | 2 multianewarray short[][] | ||
| - | 6 putstatic ch/ | ||
| - | </ | ||
| - | Wenn aber ein Array gleich initialisiert wird, z.B. mit | ||
| - | <code java> | ||
| - | short[][] a2 = {{1, | ||
| - | </ | ||
| - | führt das auf den Bytecode | ||
| - | < | ||
| - | 9 iconst_2 | ||
| - | 10 anewarray [S | ||
| - | 13 dup | ||
| - | 14 iconst_0 | ||
| - | 15 iconst_2 | ||
| - | 16 newarray short | ||
| - | 18 dup | ||
| - | 19 iconst_0 | ||
| - | 20 iconst_1 | ||
| - | 21 sastore | ||
| - | 22 dup | ||
| - | 23 iconst_1 | ||
| - | 24 iconst_2 | ||
| - | 25 sastore | ||
| - | 26 aastore | ||
| - | 27 dup | ||
| - | 28 iconst_1 | ||
| - | 29 iconst_2 | ||
| - | 30 newarray short | ||
| - | usw. | ||
| - | </ | ||
| - | Das Problem ist im ersten Fall, dass nur der Typdescriptor von [[I gelinkt würde. [I muss ebenfalls erzeugt werden. Im Heapmanager muss die Methode // | ||
| - | Die gleichen Überlegungen gelten auch für Arrays mit höheren Dimensionen. Auch dort wird // | ||
| - | ==== Erzeugung von mehrdimensionalen Arrays von Objekten ==== | ||
| - | Hier gelten grundsätzlich die gleichen Bedingungen wie oben. | ||
| - | Der folgende Javacode | ||
| - | <code java> | ||
| - | ClassA[][][] a1 = new ClassA[3][2][2]; | ||
| - | </ | ||
| - | wird in den Bytecode | ||
| - | < | ||
| - | 0 iconst_3 | ||
| - | 1 iconst_2 | ||
| - | 2 iconst_2 | ||
| - | 3 multianewarray Lch/ | ||
| - | 7 astore_1 [a1] | ||
| - | </ | ||
| - | übersetzt. Hier wird also bereits also bereits die korrekte Referenz auf // | ||