Skip to content

Commit 177d72a

Browse files
author
thk123
committed
Add overview documentation for the remove_java_new
1 parent dbd6988 commit 177d72a

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

jbmc/src/java_bytecode/README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,95 @@ To be documented.
3737

3838
\section java-bytecode-remove-java-new Remove java new
3939

40-
To be documented.
40+
\ref remove_java_new.h is responsible for converting the `new`, `newarray` and `multianewarray` Java bytecode operation into codet. Specifically it converts the bytecode instruction into:
41+
- An ALLOC with the size of the object being created
42+
- An assignment to the value zeroing its contents
43+
- If an array, initializing the size and data components
44+
- If a multi-dimensional array, recursively calling `java_new` on each sub array
45+
46+
_Note: it does not call the constructor as this is done by a separate java bytecode operation._
47+
48+
\subsection java_objects Java Objects (`new`)
49+
50+
The basic `new` operation is represented in Java bytecode by the `new` op
51+
52+
These are converted by \ref remove_java_newt::lower_java_new
53+
54+
For example, the following Java code:
55+
56+
```java
57+
TestClass f = new TestClass();
58+
```
59+
60+
Which is represented as the following Java bytecode:
61+
62+
```
63+
0: new #2 // class TestClass
64+
3: dup
65+
4: invokespecial #3 // Method "<init>":()V
66+
```
67+
68+
The first instruction only is translated into the following `codet`s:
69+
70+
```cpp
71+
tmp_object1 = side_effect_exprt(ALLOC, sizeof(TestClass))
72+
*tmp_object1 = struct_exprt{.component = 0, ... }
73+
```
74+
75+
For more details about the zero expression see \ref expr_initializert
76+
77+
\subsection oned_arrays Single Dimensional Arrays (`newarray`)
78+
79+
The new Java array operation is represented in Java bytecode by the `newarray` operation.
80+
81+
These are converted by \ref remove_java_newt::lower_java_new_array
82+
83+
See TODO:details about java arrays for details on how arrays are represented in codet. It first allocates the array object as with a regular Java object. Then the size component is set to be the size of the array and the data component is also initialized.
84+
85+
For example the following Java:
86+
87+
```java
88+
TestClass[] tArray = new TestClass[5];
89+
```
90+
91+
Which is compiled into the following Java bytecode:
92+
93+
```
94+
8: iconst_5
95+
9: anewarray #2 // class TestClass
96+
```
97+
98+
Is translated into the following `codet`s:
99+
100+
```cpp
101+
tmp_object1 = side_effect_exprt(ALLOC, sizeof(java::array[referenence]))
102+
*tmp_object1 = struct_exprt{.size = 0, .data = null}
103+
tmp_object1->length = length
104+
tmp_new_data_array1 = side_effect_exprt(java_new_array_data, TestClass)
105+
tmp_object1->data = tmp_new_data_array1
106+
ARRAY_SET(tmp_new_data_array1, NULL)
107+
```
108+
109+
The `ARRAY_SET` `codet` sets all the values to null.
110+
111+
\subsection multidarrays Multi Dimensional Arrays (`newmultiarray`)
112+
113+
The new Java multi dimensional array operation is represented in bytecode by `multianewarray`
114+
115+
These are also by \ref remove_java_newt::lower_java_new_array
116+
117+
They work the same as single dimensional arrays but create a for loop for each element in the array since these start initialized.
118+
119+
```cpp
120+
for(tmp_index = 0; tmp_index < dim_size; ++tmp_index)
121+
{
122+
struct java::array[reference] *subarray_init;
123+
subarray_init = java_new_array
124+
newarray_tmp1->data[tmp_index] = (void *)subarray_init;
125+
}
126+
```
127+
128+
`remove_java_new` is then recursively applied to the new `subarray`.
41129

42130
\section java-bytecode-remove-exceptions remove_exceptions
43131

0 commit comments

Comments
 (0)