Skip to content

Commit 2a027d4

Browse files
Merge pull request jerryscript-project#38 from ruben-ayrapetyan/experiments-dev
Experiments dev
2 parents fa0ce23 + 77a4818 commit 2a027d4

File tree

7 files changed

+265
-130
lines changed

7 files changed

+265
-130
lines changed

jerry-core/parser/js/bc/bytecode-data.cpp

Lines changed: 128 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919

2020
static bytecode_data_header_t *first_bytecode_header_p = NULL;
2121

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-
2822
static void
2923
bc_add_bytecode_data (bytecode_data_header_t *bc_header_p,
3024
lit_id_hash_table *lit_id_hash_table_p,
@@ -45,16 +39,50 @@ bc_add_bytecode_data (bytecode_data_header_t *bc_header_p,
4539
MEM_CP_SET_POINTER (bc_header_p->declarations_cp, declarations_p);
4640
bc_header_p->func_scopes_count = func_scopes_count;
4741
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;
4943

5044
bc_header_p->is_strict = is_strict;
5145
bc_header_p->is_ref_arguments_identifier = is_ref_arguments_identifier;
5246
bc_header_p->is_ref_eval_identifier = is_ref_eval_identifier;
5347
bc_header_p->is_args_moved_to_regs = is_arguments_moved_to_regs;
5448
bc_header_p->is_no_lex_env = is_no_lex_env;
49+
} /* bc_add_bytecode_data */
5550

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 */
5886

5987
/**
6088
* Deletes bytecode and associated hash table
@@ -77,7 +105,11 @@ bc_remove_bytecode_data (const bytecode_data_header_t *bytecode_data_p)
77105
{
78106
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
79107
}
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+
81113
break;
82114
}
83115

@@ -126,7 +158,7 @@ bc_dump_single_scope (scopes_tree scope_p,
126158
const size_t entries_count = scope_p->max_uniq_literals_num;
127159
const vm_instr_counter_t instrs_count = scopes_tree_count_instructions_in_single_scope (scope_p);
128160
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);
130162
const uint16_t var_decls_count = linked_list_get_length (scope_p->var_decls);
131163
const size_t bytecode_size = JERRY_ALIGNUP (instrs_count * sizeof (vm_instr_t), MEM_ALIGNMENT);
132164
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,
158190

159191
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
160192

193+
if ((uint16_t) func_scopes_count != func_scopes_count)
194+
{
195+
jerry_fatal (ERR_OUT_OF_MEMORY);
196+
}
197+
161198
bc_add_bytecode_data (header_p,
162199
lit_id_hash, bytecode_p,
163200
instrs_count,
164201
declarations_p,
165-
func_scopes_count,
202+
(uint16_t) func_scopes_count,
166203
var_decls_count,
167204
scope_p->strict_mode,
168205
scope_p->ref_arguments,
@@ -179,41 +216,99 @@ bc_dump_single_scope (scopes_tree scope_p,
179216
return header_p;
180217
} /* bc_dump_single_scope */
181218

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)
187222
{
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);
189227

190228
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 */
193252

194253
/**
195254
* Dump scopes tree into bytecode
196255
*
256+
* See also:
257+
* Note to declarations_cp field of bytecode_data_header_t
258+
*
197259
* @return pointer to bytecode header of the outer most scope
198260
*/
199261
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)
202264
{
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;
204269

205-
if (scope_p->t.children != null_list)
270+
while (next_to_handle_list_p != NULL)
206271
{
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)
208277
{
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;
212301
}
302+
303+
JERRY_ASSERT (bc_header_list_iter_p == NULL);
304+
bc_header_list_iter_p = next_to_handle_list_p;
213305
}
214306

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 */
217312

218313
void
219314
bc_finalize ()
@@ -223,7 +318,9 @@ bc_finalize ()
223318
bytecode_data_header_t *header_p = first_bytecode_header_p;
224319
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, header_p->next_header_cp);
225320

226-
mem_heap_free_block (header_p);
321+
header_p->next_header_cp = MEM_CP_NULL;
322+
323+
bc_free_bytecode_data (header_p);
227324
}
228325
} /* bc_finalize */
229326

jerry-core/parser/js/bc/bytecode-data.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) bytecode_data_header_t
4646
mem_cpointer_t lit_id_hash_cp; /**< pointer to literal identifiers hash table
4747
* See also: lit_id_hash_table_init */
4848

49-
mem_cpointer_t declarations_cp; /**< function scopes and variable declarations inside current scope */
49+
mem_cpointer_t declarations_cp; /**< function scopes and variable declarations inside current scope
50+
*
51+
* Note:
52+
* During bc_dump_and_free_scopes_tree the function scopes links point to
53+
* scopes_tree, and after - to bytecode_data_header_t.
54+
*/
5055
uint16_t func_scopes_count; /**< count of function scopes inside current scope */
5156
uint16_t var_decls_count; /**< count of variable declrations inside current scope */
5257

@@ -62,16 +67,14 @@ typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) bytecode_data_header_t
6267
} bytecode_data_header_t;
6368

6469

65-
bytecode_data_header_t * bc_get_first_bytecode_data_header ();
66-
6770
void bc_remove_bytecode_data (const bytecode_data_header_t *);
6871

6972
vm_instr_t bc_get_instr (const bytecode_data_header_t *,
7073
vm_instr_counter_t);
7174

7275
void bc_print_instrs (const bytecode_data_header_t *bytecode_data_p);
7376

74-
bytecode_data_header_t *bc_dump_scopes (scopes_tree, bool);
77+
bytecode_data_header_t *bc_dump_and_free_scopes_tree (scopes_tree, bool);
7578

7679
void bc_finalize ();
7780

jerry-core/parser/js/jsp-mm.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@
2828
/**
2929
* Header of a managed block, allocated by parser
3030
*/
31-
typedef struct __attribute__ ((alignment (MEM_ALIGNMENT)))
31+
typedef struct
3232
{
3333
mem_cpointer_t prev_block_cp; /**< previous managed block */
3434
mem_cpointer_t next_block_cp; /**< next managed block */
35+
36+
uint32_t padding; /**< padding for alignment */
3537
} jsp_mm_header_t;
3638

39+
/**
40+
* Check that alignment of jsp_mm_header_t is MEM_ALIGNMENT
41+
*/
42+
JERRY_STATIC_ASSERT ((sizeof (jsp_mm_header_t) % MEM_ALIGNMENT) == 0);
43+
3744
/**
3845
* List used for tracking memory blocks
3946
*/

0 commit comments

Comments
 (0)