Skip to content

Commit 1351169

Browse files
author
Thomas Kiley
authored
Merge pull request #2773 from thk123/doc/remove-java-new
Add overview documentation for the remove_java_new [DOC-25]
2 parents 7aa61d4 + fa6ed33 commit 1351169

File tree

1 file changed

+108
-2
lines changed

1 file changed

+108
-2
lines changed

jbmc/src/java_bytecode/README.md

+108-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ This module provides a front end for Java.
77

88
To be documented.
99

10+
\section java-bytecode-array-representation How are Java arrays represented in GOTO
11+
12+
To be documented.
13+
1014
\section java-bytecode-object-factory Object Factory
1115

1216
To be documented.
@@ -116,9 +120,111 @@ To be documented.
116120

117121
To be documented.
118122

119-
\section java-bytecode-remove-java-new Remove java new
123+
\section java-bytecode-remove-java-new Remove `new`, `newarray` and `multianewarray` bytecode operators
120124

121-
To be documented.
125+
\ref remove_java_new.h is responsible for converting the `new`, `newarray` and
126+
`multianewarray` Java bytecode operation into \ref codet. Specifically it
127+
converts the bytecode instruction into: - An ALLOCATE with the size of the
128+
object being created - An assignment to the value zeroing its contents - If an
129+
array, initializing the size and data components - If a multi-dimensional
130+
array, recursively calling `java_new` on each sub array
131+
132+
An ALLOCATE is a \ref side_effect_exprt that is interpreted by \ref
133+
goto_symext::symex_allocate
134+
135+
_Note: it does not call the constructor as this is done by a separate
136+
java_bytecode operation._
137+
138+
\subsection java_objects Java Objects (`new`)
139+
140+
The basic `new` operation is represented in Java bytecode by the `new` op
141+
142+
These are converted by \ref remove_java_newt::lower_java_new
143+
144+
For example, the following Java code:
145+
146+
```java
147+
TestClass f = new TestClass();
148+
```
149+
150+
Which is represented as the following Java bytecode:
151+
152+
```
153+
0: new #2 // class TestClass
154+
3: dup
155+
4: invokespecial #3 // Method "<init>":()V
156+
```
157+
158+
The first instruction only is translated into the following `codet`s:
159+
160+
```cpp
161+
tmp_object1 = side_effect_exprt(ALLOCATE, sizeof(TestClass))
162+
*tmp_object1 = struct_exprt{.component = 0, ... }
163+
```
164+
165+
For more details about the zero expression see \ref expr_initializert
166+
167+
\subsection oned_arrays Single Dimensional Arrays (`newarray`)
168+
169+
The new Java array operation is represented in Java bytecode by the `newarray`
170+
operation.
171+
172+
These are converted by \ref remove_java_newt::lower_java_new_array
173+
174+
See \ref java-bytecode-array-representation for details on how arrays are
175+
represented in codet.
176+
177+
A `newarray` is represented as:
178+
- an allocation of the array object (the same as with a regular Java object).
179+
- Initialize the size component
180+
- Initialize the data component
181+
182+
For example the following Java:
183+
184+
```java
185+
TestClass[] tArray = new TestClass[5];
186+
```
187+
188+
Which is compiled into the following Java bytecode:
189+
190+
```
191+
8: iconst_5
192+
9: anewarray #2 // class TestClass
193+
```
194+
195+
Is translated into the following `codet`s:
196+
197+
```cpp
198+
tmp_object1 = side_effect_exprt(ALLOCATE, sizeof(java::array[referenence]))
199+
*tmp_object1 = struct_exprt{.size = 0, .data = null}
200+
tmp_object1->length = length
201+
tmp_new_data_array1 = side_effect_exprt(java_new_array_data, TestClass)
202+
tmp_object1->data = tmp_new_data_array1
203+
ARRAY_SET(tmp_new_data_array1, NULL)
204+
```
205+
206+
The `ARRAY_SET` `codet` sets all the values to null.
207+
208+
\subsection multidarrays Multi Dimensional Arrays (`newmultiarray`)
209+
210+
The new Java multi dimensional array operation is represented in bytecode by
211+
`multianewarray`
212+
213+
These are also by \ref remove_java_newt::lower_java_new_array
214+
215+
They work the same as single dimensional arrays but create a for loop for each
216+
element in the array since these start initialized.
217+
218+
```cpp
219+
for(tmp_index = 0; tmp_index < dim_size; ++tmp_index)
220+
{
221+
struct java::array[reference] *subarray_init;
222+
subarray_init = java_new_array
223+
newarray_tmp1->data[tmp_index] = (void *)subarray_init;
224+
}
225+
```
226+
227+
`remove_java_new` is then recursively applied to the new `subarray`.
122228

123229
\section java-bytecode-remove-exceptions remove_exceptions
124230

0 commit comments

Comments
 (0)