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:
Andrey Shitov
2015-09-03 18:11:27 +03:00
committed by Ruben Ayrapetyan
parent 6a6fb3fdfa
commit 443673fc5d
22 changed files with 207 additions and 194 deletions
+7 -26
View File
@@ -17,7 +17,6 @@
#define BYTECODE_DATA_H
#include "opcodes.h"
#include "lit-id-hash-table.h"
#include "mem-allocator.h"
/*
@@ -33,36 +32,18 @@
*
* To map uid to literal id 'lit_id_hash' table is used.
*/
#define BLOCK_SIZE 64
#define BLOCK_SIZE 64u
/**
* Header of byte-code memory region, containing byte-code array and literal identifiers hash table
*/
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT)))
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) bytecode_data_header_t
{
vm_instr_t *instrs_p; /**< pointer to the bytecode */
vm_instr_counter_t instrs_count; /**< number of instructions in the byte-code array */
mem_cpointer_t lit_id_hash_cp; /**< pointer to literal identifiers hash table
* See also: lit_id_hash_table_init */
mem_cpointer_t next_instrs_cp; /**< pointer to next byte-code memory region */
vm_instr_counter_t instructions_number; /**< number of instructions in the byte-code array */
} insts_data_header_t;
mem_cpointer_t next_header_cp; /**< pointer to next instructions data header */
} bytecode_data_header_t;
typedef struct
{
const ecma_char_t *strings_buffer;
const vm_instr_t *instrs_p;
vm_instr_counter_t instrs_count;
} bytecode_data_t;
/**
* Macros to get a pointer to bytecode header by pointer to instructions array start
*/
#define GET_BYTECODE_HEADER(instrs) ((insts_data_header_t *) (((uint8_t *) (instrs)) - sizeof (insts_data_header_t)))
/**
* Macros to get a hash table corresponding to a bytecode region
*/
#define GET_HASH_TABLE_FOR_BYTECODE(instrs) (MEM_CP_GET_POINTER (lit_id_hash_table, \
GET_BYTECODE_HEADER (instrs)->lit_id_hash_cp))
#endif // BYTECODE_DATA_H
#endif /* BYTECODE_DATA_H */
+21 -14
View File
@@ -3101,13 +3101,13 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
bool in_eval, /**< flag indicating if we are parsing body of eval code */
bool is_strict, /**< flag, indicating whether current code
* inherited strict mode from code of an outer scope */
const vm_instr_t **out_instrs_p, /**< out: generated byte-code array
* (in case there were no syntax errors) */
const bytecode_data_header_t **out_bytecode_data_p, /**< out: generated byte-code array
* (in case there were no syntax errors) */
bool *out_contains_functions_p) /**< out: optional (can be NULL, if the output is not needed)
* flag, indicating whether the compiled byte-code
* contains a function declaration / expression */
{
JERRY_ASSERT (out_instrs_p != NULL);
JERRY_ASSERT (out_bytecode_data_p != NULL);
JERRY_ASSERT (!(in_dyn_constructed_function && in_eval));
@@ -3189,7 +3189,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
jsp_early_error_free ();
*out_instrs_p = serializer_merge_scopes_into_bytecode ();
*out_bytecode_data_p = serializer_merge_scopes_into_bytecode ();
dumper_free ();
@@ -3215,7 +3215,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
JERRY_ASSERT (!is_parse_finished);
#endif /* !JERRY_NDEBUG */
*out_instrs_p = NULL;
*out_bytecode_data_p = NULL;
jsp_label_remove_all_labels ();
jsp_mm_free_all ();
@@ -3249,10 +3249,10 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
jsp_status_t
parser_parse_script (const jerry_api_char_t *source, /**< source script */
size_t source_size, /**< source script size it bytes */
const vm_instr_t **out_instrs_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
const bytecode_data_header_t **out_bytecode_data_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
{
return parser_parse_program (source, source_size, false, false, false, out_instrs_p, NULL);
return parser_parse_program (source, source_size, false, false, false, out_bytecode_data_p, NULL);
} /* parser_parse_script */
/**
@@ -3266,14 +3266,20 @@ parser_parse_eval (const jerry_api_char_t *source, /**< string passed to eval()
size_t source_size, /**< string size in bytes */
bool is_strict, /**< flag, indicating whether eval is called
* from strict code in direct mode */
const vm_instr_t **out_instrs_p, /**< out: generated byte-code array
* (in case there were no syntax errors) */
const bytecode_data_header_t **out_bytecode_data_p, /**< out: generated byte-code array
* (in case there were no syntax errors) */
bool *out_contains_functions_p) /**< out: flag, indicating whether the compiled byte-code
* contains a function declaration / expression */
{
JERRY_ASSERT (out_contains_functions_p != NULL);
return parser_parse_program (source, source_size, false, true, is_strict, out_instrs_p, out_contains_functions_p);
return parser_parse_program (source,
source_size,
false,
true,
is_strict,
out_bytecode_data_p,
out_contains_functions_p);
} /* parser_parse_eval */
/**
@@ -3291,8 +3297,9 @@ parser_parse_new_function (const jerry_api_char_t **params, /**< array of argume
* body) call */
const size_t *params_size, /**< sizes of arguments strings */
size_t params_count, /**< total number of arguments passed to new Function (...) */
const vm_instr_t **out_instrs_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
const bytecode_data_header_t **out_bytecode_data_p) /**< out: generated byte-code array
* (in case there were no syntax
* errors) */
{
// Process arguments
JERRY_ASSERT (params_count > 0);
@@ -3306,7 +3313,7 @@ parser_parse_new_function (const jerry_api_char_t **params, /**< array of argume
true,
false,
false,
out_instrs_p,
out_bytecode_data_p,
NULL);
} /* parser_parse_new_function */
+4 -3
View File
@@ -29,8 +29,9 @@ typedef enum
} jsp_status_t;
void parser_set_show_instrs (bool);
jsp_status_t parser_parse_script (const jerry_api_char_t *, size_t, const vm_instr_t **);
jsp_status_t parser_parse_eval (const jerry_api_char_t *, size_t, bool, const vm_instr_t **, bool *);
jsp_status_t parser_parse_new_function (const jerry_api_char_t **, const size_t *, size_t, const vm_instr_t **);
jsp_status_t parser_parse_script (const jerry_api_char_t *, size_t, const bytecode_data_header_t **);
jsp_status_t parser_parse_eval (const jerry_api_char_t *, size_t, bool, const bytecode_data_header_t **, bool *);
jsp_status_t parser_parse_new_function (const jerry_api_char_t **, const size_t *, size_t,
const bytecode_data_header_t **);
#endif /* PARSER_H */
+3 -7
View File
@@ -691,13 +691,11 @@ scopes_tree_raw_data (scopes_tree tree, /**< scopes tree to convert to byte-code
global_oc = 0;
/* Dump bytecode and fill literal indexes 'hash' table. */
JERRY_ASSERT (instructions_array_size >=
sizeof (insts_data_header_t) + (size_t) (scopes_tree_count_instructions (tree)) * sizeof (vm_instr_t));
JERRY_ASSERT (instructions_array_size >= (size_t) (scopes_tree_count_instructions (tree)) * sizeof (vm_instr_t));
insts_data_header_t *opcodes_data = (insts_data_header_t *) buffer_p;
memset (opcodes_data, 0, instructions_array_size);
vm_instr_t *instrs = (vm_instr_t *) buffer_p;
memset (instrs, 0, instructions_array_size);
vm_instr_t *instrs = (vm_instr_t *)(((uint8_t*) opcodes_data) + sizeof (insts_data_header_t));
merge_subscopes (tree, instrs, lit_ids);
if (lit_id_to_uid != null_hash)
{
@@ -705,8 +703,6 @@ scopes_tree_raw_data (scopes_tree tree, /**< scopes tree to convert to byte-code
lit_id_to_uid = null_hash;
}
MEM_CP_SET_POINTER (opcodes_data->lit_id_hash_cp, lit_ids);
return instrs;
} /* scopes_tree_raw_data */
+68 -65
View File
@@ -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);
}
+5 -5
View File
@@ -16,6 +16,7 @@
#ifndef SERIALIZER_H
#define SERIALIZER_H
#include "bytecode-data.h"
#include "jrt.h"
#include "ecma-globals.h"
#include "opcodes.h"
@@ -28,12 +29,11 @@ void serializer_init ();
void serializer_set_show_instrs (bool show_instrs);
op_meta serializer_get_op_meta (vm_instr_counter_t);
op_meta serializer_get_var_decl (vm_instr_counter_t);
vm_instr_t serializer_get_instr (const vm_instr_t*, vm_instr_counter_t);
lit_cpointer_t serializer_get_literal_cp_by_uid (uint8_t, const vm_instr_t*, vm_instr_counter_t);
void serializer_set_strings_buffer (const ecma_char_t *);
vm_instr_t serializer_get_instr (const bytecode_data_header_t *, vm_instr_counter_t);
lit_cpointer_t serializer_get_literal_cp_by_uid (uint8_t, const bytecode_data_header_t *, vm_instr_counter_t);
void serializer_set_scope (scopes_tree);
void serializer_dump_subscope (scopes_tree);
const vm_instr_t *serializer_merge_scopes_into_bytecode (void);
const bytecode_data_header_t *serializer_merge_scopes_into_bytecode (void);
void serializer_dump_op_meta (op_meta);
void serializer_dump_var_decl (op_meta);
vm_instr_counter_t serializer_get_current_instr_counter (void);
@@ -41,7 +41,7 @@ vm_instr_counter_t serializer_get_current_var_decls_counter (void);
vm_instr_counter_t serializer_count_instrs_in_subscopes (void);
void serializer_set_writing_position (vm_instr_counter_t);
void serializer_rewrite_op_meta (vm_instr_counter_t, op_meta);
void serializer_remove_instructions (const vm_instr_t *instrs_p);
void serializer_remove_bytecode_data (const bytecode_data_header_t *);
void serializer_free (void);
#endif // SERIALIZER_H