19
19
20
20
static bytecode_data_header_t *first_bytecode_header_p = NULL ;
21
21
22
- bytecode_data_header_t *
23
- bc_get_first_bytecode_data_header ()
24
- {
25
- return first_bytecode_header_p;
26
- } /* bc_get_first_bytecode_header */
27
-
28
22
static void
29
23
bc_add_bytecode_data (bytecode_data_header_t *bc_header_p,
30
24
lit_id_hash_table *lit_id_hash_table_p,
@@ -45,16 +39,50 @@ bc_add_bytecode_data (bytecode_data_header_t *bc_header_p,
45
39
MEM_CP_SET_POINTER (bc_header_p->declarations_cp , declarations_p);
46
40
bc_header_p->func_scopes_count = func_scopes_count;
47
41
bc_header_p->var_decls_count = var_decls_count;
48
- MEM_CP_SET_POINTER ( bc_header_p->next_header_cp , first_bytecode_header_p) ;
42
+ bc_header_p->next_header_cp = MEM_CP_NULL ;
49
43
50
44
bc_header_p->is_strict = is_strict;
51
45
bc_header_p->is_ref_arguments_identifier = is_ref_arguments_identifier;
52
46
bc_header_p->is_ref_eval_identifier = is_ref_eval_identifier;
53
47
bc_header_p->is_args_moved_to_regs = is_arguments_moved_to_regs;
54
48
bc_header_p->is_no_lex_env = is_no_lex_env;
49
+ } /* bc_add_bytecode_data */
55
50
56
- first_bytecode_header_p = bc_header_p;
57
- } /* bc_add_bytecode */
51
+ static void
52
+ bc_free_bytecode_data (bytecode_data_header_t *bytecode_data_p)
53
+ {
54
+ bytecode_data_header_t * next_to_handle_list_p = bytecode_data_p;
55
+
56
+ while (next_to_handle_list_p != NULL )
57
+ {
58
+ bytecode_data_header_t *bc_header_list_iter_p = next_to_handle_list_p;
59
+ next_to_handle_list_p = NULL ;
60
+
61
+ while (bc_header_list_iter_p != NULL )
62
+ {
63
+ bytecode_data_header_t *header_p = bc_header_list_iter_p;
64
+
65
+ bc_header_list_iter_p = MEM_CP_GET_POINTER (bytecode_data_header_t , header_p->next_header_cp );
66
+
67
+ mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t , header_p->declarations_cp );
68
+
69
+ for (uint32_t index = 0 ; index < header_p->func_scopes_count ; index++)
70
+ {
71
+ bytecode_data_header_t *child_scope_header_p = MEM_CP_GET_NON_NULL_POINTER (bytecode_data_header_t ,
72
+ declarations_p[index]);
73
+ JERRY_ASSERT (child_scope_header_p->next_header_cp == MEM_CP_NULL);
74
+
75
+ MEM_CP_SET_POINTER (child_scope_header_p->next_header_cp , next_to_handle_list_p);
76
+
77
+ next_to_handle_list_p = child_scope_header_p;
78
+ }
79
+
80
+ mem_heap_free_block (header_p);
81
+ }
82
+
83
+ JERRY_ASSERT (bc_header_list_iter_p == NULL );
84
+ }
85
+ } /* bc_free_bytecode_data */
58
86
59
87
/* *
60
88
* Deletes bytecode and associated hash table
@@ -77,7 +105,11 @@ bc_remove_bytecode_data (const bytecode_data_header_t *bytecode_data_p)
77
105
{
78
106
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t , cur_header_p->next_header_cp );
79
107
}
80
- mem_heap_free_block (cur_header_p);
108
+
109
+ cur_header_p->next_header_cp = MEM_CP_NULL;
110
+
111
+ bc_free_bytecode_data (cur_header_p);
112
+
81
113
break ;
82
114
}
83
115
@@ -126,7 +158,7 @@ bc_dump_single_scope (scopes_tree scope_p,
126
158
const size_t entries_count = scope_p->max_uniq_literals_num ;
127
159
const vm_instr_counter_t instrs_count = scopes_tree_count_instructions_in_single_scope (scope_p);
128
160
const size_t blocks_count = JERRY_ALIGNUP (instrs_count, BLOCK_SIZE) / BLOCK_SIZE;
129
- const uint16_t func_scopes_count = scope_p-> t . children ? linked_list_get_length (scope_p-> t . children ) : 0 ;
161
+ const size_t func_scopes_count = scopes_tree_child_scopes_num (scope_p) ;
130
162
const uint16_t var_decls_count = linked_list_get_length (scope_p->var_decls );
131
163
const size_t bytecode_size = JERRY_ALIGNUP (instrs_count * sizeof (vm_instr_t ), MEM_ALIGNMENT);
132
164
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (entries_count, blocks_count);
@@ -158,11 +190,16 @@ bc_dump_single_scope (scopes_tree scope_p,
158
190
159
191
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
160
192
193
+ if ((uint16_t ) func_scopes_count != func_scopes_count)
194
+ {
195
+ jerry_fatal (ERR_OUT_OF_MEMORY);
196
+ }
197
+
161
198
bc_add_bytecode_data (header_p,
162
199
lit_id_hash, bytecode_p,
163
200
instrs_count,
164
201
declarations_p,
165
- func_scopes_count,
202
+ ( uint16_t ) func_scopes_count,
166
203
var_decls_count,
167
204
scope_p->strict_mode ,
168
205
scope_p->ref_arguments ,
@@ -179,41 +216,99 @@ bc_dump_single_scope (scopes_tree scope_p,
179
216
return header_p;
180
217
} /* bc_dump_single_scope */
181
218
182
-
183
- static void
184
- bc_data_header_set_child (bytecode_data_header_t *header_p,
185
- bytecode_data_header_t *child_p,
186
- uint32_t i)
219
+ static bytecode_data_header_t *
220
+ bc_dump_scope_and_prepare_header (scopes_tree scope_p,
221
+ bool is_show_instrs)
187
222
{
188
- JERRY_ASSERT (i < header_p->func_scopes_count );
223
+ JERRY_ASSERT (scope_p->next_scope_cp == MEM_CP_NULL);
224
+
225
+ bytecode_data_header_t *header_p = bc_dump_single_scope (scope_p, is_show_instrs);
226
+ JERRY_ASSERT (header_p->next_header_cp == MEM_CP_NULL);
189
227
190
228
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t , header_p->declarations_cp );
191
- MEM_CP_SET_POINTER (declarations_p[i], child_p);
192
- } /* bc_data_header_set_child */
229
+
230
+ uint32_t index = 0 ;
231
+ scopes_tree child_scopes_iter_p = MEM_CP_GET_POINTER (scopes_tree_int, scope_p->children_list_cp );
232
+
233
+ while (child_scopes_iter_p != NULL )
234
+ {
235
+ JERRY_ASSERT (index < header_p->func_scopes_count );
236
+
237
+ MEM_CP_SET_POINTER (declarations_p[index], child_scopes_iter_p);
238
+
239
+ index++;
240
+
241
+ scopes_tree next_child_scope_p = MEM_CP_GET_POINTER (scopes_tree_int, child_scopes_iter_p->next_scope_cp );
242
+
243
+ child_scopes_iter_p->next_scope_cp = MEM_CP_NULL;
244
+
245
+ child_scopes_iter_p = next_child_scope_p;
246
+ }
247
+
248
+ JERRY_ASSERT (index == header_p->func_scopes_count );
249
+
250
+ return header_p;
251
+ } /* bc_dump_scope_and_prepare_header */
193
252
194
253
/* *
195
254
* Dump scopes tree into bytecode
196
255
*
256
+ * See also:
257
+ * Note to declarations_cp field of bytecode_data_header_t
258
+ *
197
259
* @return pointer to bytecode header of the outer most scope
198
260
*/
199
261
bytecode_data_header_t *
200
- bc_dump_scopes (scopes_tree scope_p,
201
- bool is_show_instrs)
262
+ bc_dump_and_free_scopes_tree (scopes_tree scope_p,
263
+ bool is_show_instrs)
202
264
{
203
- bytecode_data_header_t *header_p = bc_dump_single_scope (scope_p, is_show_instrs);
265
+ bytecode_data_header_t *bc_header_p = bc_dump_scope_and_prepare_header (scope_p, is_show_instrs);
266
+ scopes_tree_free (scope_p);
267
+
268
+ bytecode_data_header_t * next_to_handle_list_p = bc_header_p;
204
269
205
- if (scope_p-> t . children != null_list )
270
+ while (next_to_handle_list_p != NULL )
206
271
{
207
- for (uint32_t i = 0 ; i < linked_list_get_length (scope_p->t .children ); ++i)
272
+ bytecode_data_header_t *bc_header_list_iter_p = next_to_handle_list_p;
273
+
274
+ next_to_handle_list_p = NULL ;
275
+
276
+ while (bc_header_list_iter_p != NULL )
208
277
{
209
- bytecode_data_header_t *child_p;
210
- child_p = bc_dump_scopes (*(scopes_tree *) linked_list_element (scope_p->t .children , i), is_show_instrs);
211
- bc_data_header_set_child (header_p, child_p, i);
278
+ mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t , bc_header_list_iter_p->declarations_cp );
279
+
280
+ for (uint32_t index = 0 ; index < bc_header_list_iter_p->func_scopes_count ; index++)
281
+ {
282
+ scopes_tree next_tree_p = MEM_CP_GET_NON_NULL_POINTER (scopes_tree_int, declarations_p[index]);
283
+
284
+ bytecode_data_header_t *next_header_p = bc_dump_scope_and_prepare_header (next_tree_p, is_show_instrs);
285
+
286
+ scopes_tree_free (next_tree_p);
287
+
288
+ MEM_CP_SET_NON_NULL_POINTER (declarations_p [index], next_header_p);
289
+
290
+ JERRY_ASSERT (next_header_p->next_header_cp == MEM_CP_NULL);
291
+
292
+ MEM_CP_SET_POINTER (next_header_p->next_header_cp , next_to_handle_list_p);
293
+ next_to_handle_list_p = next_header_p;
294
+ }
295
+
296
+ bytecode_data_header_t *next_bc_header_list_iter_p = MEM_CP_GET_POINTER (bytecode_data_header_t ,
297
+ bc_header_list_iter_p->next_header_cp );
298
+ bc_header_list_iter_p->next_header_cp = MEM_CP_NULL;
299
+
300
+ bc_header_list_iter_p = next_bc_header_list_iter_p;
212
301
}
302
+
303
+ JERRY_ASSERT (bc_header_list_iter_p == NULL );
304
+ bc_header_list_iter_p = next_to_handle_list_p;
213
305
}
214
306
215
- return header_p;
216
- } /* bc_dump_scopes */
307
+ MEM_CP_SET_POINTER (bc_header_p->next_header_cp , first_bytecode_header_p);
308
+ first_bytecode_header_p = bc_header_p;
309
+
310
+ return bc_header_p;
311
+ } /* bc_dump_and_free_scopes_tree */
217
312
218
313
void
219
314
bc_finalize ()
@@ -223,7 +318,9 @@ bc_finalize ()
223
318
bytecode_data_header_t *header_p = first_bytecode_header_p;
224
319
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t , header_p->next_header_cp );
225
320
226
- mem_heap_free_block (header_p);
321
+ header_p->next_header_cp = MEM_CP_NULL;
322
+
323
+ bc_free_bytecode_data (header_p);
227
324
}
228
325
} /* bc_finalize */
229
326
0 commit comments