This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:crosscompiler:arrays [2015/03/29 09:46] – [Multidimensional Arrays] ursgraf | dev:crosscompiler:arrays [2022/12/20 11:24] (current) – ursgraf | ||
---|---|---|---|
Line 2: | Line 2: | ||
The following figure demonstrates how arrays are implemented in the memory. | The following figure demonstrates how arrays are implemented in the memory. | ||
[{{ .: | [{{ .: | ||
- | An array extends // | + | An array extends // |
The field //length// denotes the number of elements of the array (16 bit). //heap// is used for the garbage collection ([[..: | The field //length// denotes the number of elements of the array (16 bit). //heap// is used for the garbage collection ([[..: | ||
When accessing array elements the code generator inserts code to check if the element index is in the range 0 to // | 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 // | + | If the field //length// is used in Java the Bytecode instruktion // |
===== Generating Arrays ===== | ===== Generating Arrays ===== | ||
Line 28: | Line 28: | ||
< | < | ||
0 iconst_4 | 0 iconst_4 | ||
- | 1 anewarray | + | 1 anewarray |
- | 4 putstatic | + | 4 putstatic |
</ | </ | ||
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 // | 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 // | ||
Line 47: | Line 47: | ||
short[][] a = new short[2][3] | short[][] a = new short[2][3] | ||
</ | </ | ||
- | gives a Bytecode | + | leads to the Bytecode |
< | < | ||
0 iconst_2 | 0 iconst_2 | ||
1 iconst_3 | 1 iconst_3 | ||
2 multianewarray short[][] | 2 multianewarray short[][] | ||
- | 6 putstatic | + | 6 putstatic |
</ | </ | ||
If an array is immediately initialized | If an array is immediately initialized | ||
Line 81: | Line 81: | ||
etc. | etc. | ||
</ | </ | ||
- | The problem with the first case is, that only the type descriptor //[[I// would be linked. However, //[I// must be created as well. Im Heapmanager muss die Methode | + | The problem with the first case is, that only the type descriptor //[[I// would be linked. However, //[I// must be created as well. In the method |
- | Die gleichen Überlegungen gelten auch für Arrays mit höheren Dimensionen. Auch dort wird // | + | Similar thoughts are true for arrays with higher dimensions. As well, the instruction |
==== Generating an Multidimensional Array of Objects ==== | ==== Generating an Multidimensional Array of Objects ==== | ||
- | Hier gelten grundsätzlich die gleichen Bedingungen wie oben. | + | Here, the same conditions are true as described above. |
- | Der folgende Javacode | + | |
<code java> | <code java> | ||
ClassA[][][] a1 = new ClassA[3][2][2]; | ClassA[][][] a1 = new ClassA[3][2][2]; | ||
</ | </ | ||
- | wird in den Bytecode | + | This leads to Bytecode |
< | < | ||
0 iconst_3 | 0 iconst_3 | ||
Line 97: | Line 96: | ||
7 astore_1 [a1] | 7 astore_1 [a1] | ||
</ | </ | ||
- | übersetzt. Hier wird also bereits also bereits die korrekte Referenz auf // | + | The reference to // |