Update bytecode header structure so that bytecode could be stored independently from hash table and bytecode header.
JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov a.shitov@samsung.com
This commit is contained in:
committed by
Ruben Ayrapetyan
parent
6a6fb3fdfa
commit
443673fc5d
@@ -19,13 +19,12 @@
|
||||
#include "array-list.h"
|
||||
#include "scopes-tree.h"
|
||||
|
||||
static bytecode_data_t bytecode_data;
|
||||
static bytecode_data_header_t *first_bytecode_header_p;
|
||||
static scopes_tree current_scope;
|
||||
static bool print_instrs;
|
||||
|
||||
static void
|
||||
serializer_print_instrs (const vm_instr_t *instrs_p,
|
||||
size_t instrs_count);
|
||||
serializer_print_instrs (const bytecode_data_header_t *);
|
||||
|
||||
op_meta
|
||||
serializer_get_op_meta (vm_instr_counter_t oc)
|
||||
@@ -52,19 +51,19 @@ serializer_get_var_decl (vm_instr_counter_t oc) /**< index of variable declarati
|
||||
* @return byte-code instruction
|
||||
*/
|
||||
vm_instr_t
|
||||
serializer_get_instr (const vm_instr_t *instrs_p, /**< pointer to byte-code array (or NULL,
|
||||
* if instruction should be taken from
|
||||
* instruction list of current scope) */
|
||||
serializer_get_instr (const bytecode_data_header_t *bytecode_data_p, /**< pointer to byte-code data (or NULL,
|
||||
* if instruction should be taken from
|
||||
* instruction list of current scope) */
|
||||
vm_instr_counter_t oc) /**< position of the intruction */
|
||||
{
|
||||
if (instrs_p == NULL)
|
||||
if (bytecode_data_p == NULL)
|
||||
{
|
||||
return serializer_get_op_meta (oc).op;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (oc < GET_BYTECODE_HEADER (instrs_p)->instructions_number);
|
||||
return instrs_p[oc];
|
||||
JERRY_ASSERT (oc < bytecode_data_p->instrs_count);
|
||||
return bytecode_data_p->instrs_p[oc];
|
||||
}
|
||||
} /* serializer_get_instr */
|
||||
|
||||
@@ -79,23 +78,27 @@ serializer_get_instr (const vm_instr_t *instrs_p, /**< pointer to byte-code arra
|
||||
*/
|
||||
lit_cpointer_t
|
||||
serializer_get_literal_cp_by_uid (uint8_t id, /**< literal idx */
|
||||
const vm_instr_t *instrs_p, /**< pointer to bytecode */
|
||||
const bytecode_data_header_t *bytecode_data_p, /**< pointer to bytecode */
|
||||
vm_instr_counter_t oc) /**< position in the bytecode */
|
||||
{
|
||||
lit_id_hash_table *lit_id_hash = GET_HASH_TABLE_FOR_BYTECODE (instrs_p == NULL ? bytecode_data.instrs_p : instrs_p);
|
||||
lit_id_hash_table *lit_id_hash = null_hash;
|
||||
if (bytecode_data_p)
|
||||
{
|
||||
lit_id_hash = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
|
||||
}
|
||||
else
|
||||
{
|
||||
lit_id_hash = MEM_CP_GET_POINTER (lit_id_hash_table, first_bytecode_header_p->lit_id_hash_cp);
|
||||
}
|
||||
|
||||
if (lit_id_hash == null_hash)
|
||||
{
|
||||
return INVALID_LITERAL;
|
||||
}
|
||||
|
||||
return lit_id_hash_table_lookup (lit_id_hash, id, oc);
|
||||
} /* serializer_get_literal_cp_by_uid */
|
||||
|
||||
void
|
||||
serializer_set_strings_buffer (const ecma_char_t *s)
|
||||
{
|
||||
bytecode_data.strings_buffer = s;
|
||||
}
|
||||
|
||||
void
|
||||
serializer_set_scope (scopes_tree new_scope)
|
||||
{
|
||||
@@ -145,43 +148,52 @@ serializer_dump_subscope (scopes_tree tree) /**< scope to dump */
|
||||
}
|
||||
} /* serializer_dump_subscope */
|
||||
|
||||
const vm_instr_t *
|
||||
|
||||
/**
|
||||
* Merge scopes tree into bytecode
|
||||
*
|
||||
* @return pointer to generated bytecode
|
||||
*/
|
||||
const bytecode_data_header_t *
|
||||
serializer_merge_scopes_into_bytecode (void)
|
||||
{
|
||||
bytecode_data.instrs_count = scopes_tree_count_instructions (current_scope);
|
||||
|
||||
const size_t buckets_count = scopes_tree_count_literals_in_blocks (current_scope);
|
||||
const size_t blocks_count = (size_t) bytecode_data.instrs_count / BLOCK_SIZE + 1;
|
||||
const vm_instr_counter_t instrs_count = scopes_tree_count_instructions (current_scope);
|
||||
const size_t blocks_count = JERRY_ALIGNUP (instrs_count, BLOCK_SIZE) / BLOCK_SIZE;
|
||||
|
||||
const size_t bytecode_array_size = JERRY_ALIGNUP (sizeof (insts_data_header_t) + instrs_count * sizeof (vm_instr_t),
|
||||
MEM_ALIGNMENT);
|
||||
const size_t lit_id_hash_table_size = JERRY_ALIGNUP (lit_id_hash_table_get_size_for_table (buckets_count,
|
||||
blocks_count),
|
||||
MEM_ALIGNMENT);
|
||||
const size_t bytecode_size = JERRY_ALIGNUP (instrs_count * sizeof (vm_instr_t), MEM_ALIGNMENT);
|
||||
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (buckets_count, blocks_count);
|
||||
const size_t header_and_hash_table_size = JERRY_ALIGNUP (sizeof (bytecode_data_header_t) + hash_table_size,
|
||||
MEM_ALIGNMENT);
|
||||
|
||||
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (bytecode_array_size + lit_id_hash_table_size,
|
||||
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (bytecode_size + header_and_hash_table_size,
|
||||
MEM_HEAP_ALLOC_LONG_TERM);
|
||||
|
||||
lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (buffer_p + bytecode_array_size,
|
||||
lit_id_hash_table_size,
|
||||
lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (buffer_p + sizeof (bytecode_data_header_t),
|
||||
hash_table_size,
|
||||
buckets_count, blocks_count);
|
||||
|
||||
const vm_instr_t *instrs_p = scopes_tree_raw_data (current_scope, buffer_p, bytecode_array_size, lit_id_hash);
|
||||
vm_instr_t *bytecode_p = scopes_tree_raw_data (current_scope,
|
||||
buffer_p + header_and_hash_table_size,
|
||||
bytecode_size,
|
||||
lit_id_hash);
|
||||
|
||||
insts_data_header_t *header_p = (insts_data_header_t*) buffer_p;
|
||||
MEM_CP_SET_POINTER (header_p->next_instrs_cp, bytecode_data.instrs_p);
|
||||
header_p->instructions_number = instrs_count;
|
||||
bytecode_data.instrs_p = instrs_p;
|
||||
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
|
||||
MEM_CP_SET_POINTER (header_p->lit_id_hash_cp, lit_id_hash);
|
||||
header_p->instrs_p = bytecode_p;
|
||||
header_p->instrs_count = instrs_count;
|
||||
MEM_CP_SET_POINTER (header_p->next_header_cp, first_bytecode_header_p);
|
||||
|
||||
first_bytecode_header_p = header_p;
|
||||
|
||||
if (print_instrs)
|
||||
{
|
||||
lit_dump_literals ();
|
||||
serializer_print_instrs (instrs_p, bytecode_data.instrs_count);
|
||||
serializer_print_instrs (header_p);
|
||||
}
|
||||
|
||||
return instrs_p;
|
||||
}
|
||||
return header_p;
|
||||
} /* serializer_merge_scopes_into_bytecode */
|
||||
|
||||
void
|
||||
serializer_dump_op_meta (op_meta op)
|
||||
@@ -252,25 +264,23 @@ serializer_rewrite_op_meta (const vm_instr_counter_t loc, op_meta op)
|
||||
}
|
||||
|
||||
static void
|
||||
serializer_print_instrs (const vm_instr_t *instrs_p,
|
||||
size_t instrs_count)
|
||||
serializer_print_instrs (const bytecode_data_header_t *bytecode_data_p)
|
||||
{
|
||||
#ifdef JERRY_ENABLE_PRETTY_PRINTER
|
||||
for (vm_instr_counter_t loc = 0; loc < instrs_count; loc++)
|
||||
for (vm_instr_counter_t loc = 0; loc < bytecode_data_p->instrs_count; loc++)
|
||||
{
|
||||
op_meta opm;
|
||||
|
||||
opm.op = instrs_p[loc];
|
||||
opm.op = bytecode_data_p->instrs_p[loc];
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
opm.lit_id[i] = NOT_A_LITERAL;
|
||||
}
|
||||
|
||||
pp_op_meta (instrs_p, loc, opm, false);
|
||||
pp_op_meta (bytecode_data_p, loc, opm, false);
|
||||
}
|
||||
#else
|
||||
(void) instrs_p;
|
||||
(void) instrs_count;
|
||||
(void) bytecode_data_p;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -280,8 +290,7 @@ serializer_init ()
|
||||
current_scope = NULL;
|
||||
print_instrs = false;
|
||||
|
||||
bytecode_data.strings_buffer = NULL;
|
||||
bytecode_data.instrs_p = NULL;
|
||||
first_bytecode_header_p = NULL;
|
||||
|
||||
lit_init ();
|
||||
}
|
||||
@@ -295,47 +304,41 @@ void serializer_set_show_instrs (bool show_instrs)
|
||||
* Deletes bytecode and associated hash table
|
||||
*/
|
||||
void
|
||||
serializer_remove_instructions (const vm_instr_t *instrs_p) /**< pointer to instructions which should be deleted */
|
||||
serializer_remove_bytecode_data (const bytecode_data_header_t *bytecode_data_p) /**< pointer to bytecode data which
|
||||
* should be deleted */
|
||||
{
|
||||
insts_data_header_t *prev_header = NULL;
|
||||
const vm_instr_t *cur_instrs_p = bytecode_data.instrs_p;
|
||||
while (cur_instrs_p != NULL)
|
||||
{
|
||||
insts_data_header_t *cur_header_p = GET_BYTECODE_HEADER (cur_instrs_p);
|
||||
bytecode_data_header_t *prev_header = NULL;
|
||||
bytecode_data_header_t *cur_header_p = first_bytecode_header_p;
|
||||
|
||||
if (cur_instrs_p == instrs_p)
|
||||
while (cur_header_p != NULL)
|
||||
{
|
||||
if (cur_header_p == bytecode_data_p)
|
||||
{
|
||||
if (prev_header)
|
||||
{
|
||||
prev_header->next_instrs_cp = cur_header_p->next_instrs_cp;
|
||||
prev_header->next_header_cp = cur_header_p->next_header_cp;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytecode_data.instrs_p = MEM_CP_GET_POINTER (vm_instr_t, cur_header_p->next_instrs_cp);
|
||||
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
|
||||
}
|
||||
mem_heap_free_block (cur_header_p);
|
||||
break;
|
||||
}
|
||||
|
||||
prev_header = GET_BYTECODE_HEADER (cur_instrs_p);
|
||||
cur_instrs_p = MEM_CP_GET_POINTER (vm_instr_t, cur_header_p->next_instrs_cp);
|
||||
prev_header = cur_header_p;
|
||||
}
|
||||
} /* serializer_remove_instructions */
|
||||
|
||||
void
|
||||
serializer_free (void)
|
||||
{
|
||||
if (bytecode_data.strings_buffer)
|
||||
{
|
||||
mem_heap_free_block ((uint8_t *) bytecode_data.strings_buffer);
|
||||
}
|
||||
|
||||
lit_finalize ();
|
||||
|
||||
while (bytecode_data.instrs_p != NULL)
|
||||
while (first_bytecode_header_p != NULL)
|
||||
{
|
||||
insts_data_header_t *header_p = GET_BYTECODE_HEADER (bytecode_data.instrs_p);
|
||||
bytecode_data.instrs_p = MEM_CP_GET_POINTER (vm_instr_t, header_p->next_instrs_cp);
|
||||
bytecode_data_header_t *header_p = first_bytecode_header_p;
|
||||
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, header_p->next_header_cp);
|
||||
|
||||
mem_heap_free_block (header_p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user