Modify the snapshot API functions to expect a 32-bit aligned buffer pointer (#1655)

JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
This commit is contained in:
Robert Sipka
2017-03-17 01:01:59 +01:00
committed by yichoi
parent 05bb5d7890
commit 23ac60915f
7 changed files with 120 additions and 97 deletions
+27 -20
View File
@@ -226,7 +226,7 @@ ecma_find_or_create_literal_number (ecma_number_t number_arg) /**< number to be
* false - otherwise.
*/
bool
ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot buffer */
ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot buffer */
size_t buffer_size, /**< size of the buffer */
size_t *in_out_buffer_offset_p, /**< [in,out] write position in the buffer */
lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal identifiers
@@ -294,7 +294,8 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b
map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t));
/* Set return values (no error is possible from here). */
buffer_p += *in_out_buffer_offset_p;
JERRY_ASSERT ((*in_out_buffer_offset_p % sizeof (uint32_t)) == 0);
buffer_p += *in_out_buffer_offset_p / sizeof (uint32_t);
*in_out_buffer_offset_p += lit_table_size;
*out_map_p = map_p;
*out_map_len_p = total_count;
@@ -306,12 +307,14 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b
* constant so the first literal must have offset one. */
uint32_t literal_offset = JERRY_SNAPSHOT_LITERAL_ALIGNMENT;
((uint32_t *) buffer_p)[0] = string_count;
((uint32_t *) buffer_p)[1] = number_count;
buffer_p += 2 * sizeof (uint32_t);
buffer_p[0] = string_count;
buffer_p[1] = number_count;
buffer_p += 2;
string_list_p = JERRY_CONTEXT (string_list_first_p);
uint16_t *destination_p = (uint16_t *) buffer_p;
while (string_list_p != NULL)
{
for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
@@ -327,13 +330,14 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b
ecma_length_t length = ecma_string_get_size (string_p);
*((uint16_t *) buffer_p) = (uint16_t) length;
ecma_string_to_utf8_bytes (string_p, buffer_p + sizeof (uint16_t), length);
*destination_p = (uint16_t) length;
ecma_string_to_utf8_bytes (string_p, ((lit_utf8_byte_t *) destination_p) + sizeof (uint16_t), length);
length = JERRY_ALIGNUP (sizeof (uint16_t) + length,
JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
buffer_p += length;
JERRY_ASSERT ((length % sizeof (uint16_t)) == 0);
destination_p += length / sizeof (uint16_t);
literal_offset += length;
}
}
@@ -359,12 +363,13 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (value_p) == ECMA_STRING_LITERAL_NUMBER);
ecma_number_t num = ecma_get_number_from_value (value_p->u.lit_number);
memcpy (buffer_p, &num, sizeof (ecma_number_t));
memcpy (destination_p, &num, sizeof (ecma_number_t));
ecma_length_t length = JERRY_ALIGNUP (sizeof (ecma_number_t),
JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
buffer_p += length;
JERRY_ASSERT ((length % sizeof (uint16_t)) == 0);
destination_p += length / sizeof (uint16_t);
literal_offset += length;
}
}
@@ -388,7 +393,7 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b
* false - otherwise (i.e. buffer length is incorrect).
*/
static inline bool __attr_always_inline___
ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with literal table in snapshot */
ecma_load_literals_from_buffer (const uint16_t *buffer_p, /**< buffer with literal table in snapshot */
uint32_t lit_table_size, /**< size of literal table in snapshot */
lit_mem_to_snapshot_id_map_entry_t *map_p, /**< literal map */
uint32_t string_count, /**< number of strings */
@@ -407,7 +412,7 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera
return false;
}
lit_utf8_size_t length = *((uint16_t *) buffer_p);
lit_utf8_size_t length = *buffer_p;
lit_utf8_size_t aligned_length = JERRY_ALIGNUP (sizeof (uint16_t) + length,
JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
@@ -417,11 +422,12 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera
return false;
}
map_p->literal_id = ecma_find_or_create_literal_string (buffer_p + sizeof (uint16_t), length);
map_p->literal_id = ecma_find_or_create_literal_string (((lit_utf8_byte_t *) buffer_p) + sizeof (uint16_t), length);
map_p->literal_offset = (jmem_cpointer_t) (literal_offset >> JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG);
map_p++;
buffer_p += aligned_length;
JERRY_ASSERT ((aligned_length % sizeof (uint16_t)) == 0);
buffer_p += aligned_length / sizeof (uint16_t);
literal_offset += aligned_length;
string_count--;
@@ -446,7 +452,8 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera
ecma_length_t length = JERRY_ALIGNUP (sizeof (ecma_number_t),
JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
buffer_p += length;
JERRY_ASSERT ((length % sizeof (uint16_t)) == 0);
buffer_p += length / sizeof (uint16_t);
literal_offset += length;
number_count--;
@@ -462,7 +469,7 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera
* false - otherwise (i.e. snapshot is incorrect).
*/
bool
ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with literal table in snapshot */
ecma_load_literals_from_snapshot (const uint32_t *buffer_p, /**< buffer with literal table in snapshot */
uint32_t lit_table_size, /**< size of literal table in snapshot */
lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal offsets
* in snapshot to identifiers
@@ -478,9 +485,9 @@ ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with lite
return false;
}
uint32_t string_count = ((uint32_t *) buffer_p)[0];
uint32_t number_count = ((uint32_t *) buffer_p)[1];
buffer_p += 2 * sizeof (uint32_t);
uint32_t string_count = buffer_p[0];
uint32_t number_count = buffer_p[1];
buffer_p += 2;
uint32_t total_count = string_count + number_count;
lit_mem_to_snapshot_id_map_entry_t *map_p;
@@ -495,7 +502,7 @@ ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with lite
map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t));
*out_map_p = map_p;
if (ecma_load_literals_from_buffer (buffer_p, lit_table_size, map_p, string_count, number_count))
if (ecma_load_literals_from_buffer ((uint16_t *) buffer_p, lit_table_size, map_p, string_count, number_count))
{
return true;
}
+2 -2
View File
@@ -43,13 +43,13 @@ jmem_cpointer_t ecma_find_or_create_literal_number (ecma_number_t number_arg);
#ifdef JERRY_ENABLE_SNAPSHOT_SAVE
bool
ecma_save_literals_for_snapshot (uint8_t *, size_t, size_t *,
ecma_save_literals_for_snapshot (uint32_t *, size_t, size_t *,
lit_mem_to_snapshot_id_map_entry_t **, uint32_t *, uint32_t *);
#endif /* JERRY_ENABLE_SNAPSHOT_SAVE */
#ifdef JERRY_ENABLE_SNAPSHOT_EXEC
bool
ecma_load_literals_from_snapshot (const uint8_t *, uint32_t,
ecma_load_literals_from_snapshot (const uint32_t *, uint32_t,
lit_mem_to_snapshot_id_map_entry_t **, uint32_t *);
#endif /* JERRY_ENABLE_SNAPSHOT_EXEC */
+51 -46
View File
@@ -208,7 +208,7 @@ snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< compiled
* Set the uint16_t offsets in the code area.
*/
static void
jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */
jerry_snapshot_set_offsets (uint32_t *buffer_p, /**< buffer */
uint32_t size, /**< buffer size */
lit_mem_to_snapshot_id_map_entry_t *lit_map_p) /**< literal map */
{
@@ -228,7 +228,7 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */
if (bytecode_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
literal_start_p = (jmem_cpointer_t *) (buffer_p + sizeof (cbc_uint16_arguments_t));
literal_start_p = (jmem_cpointer_t *) (((uint8_t *) buffer_p) + sizeof (cbc_uint16_arguments_t));
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) buffer_p;
argument_end = args_p->argument_end;
@@ -237,7 +237,7 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */
}
else
{
literal_start_p = (jmem_cpointer_t *) (buffer_p + sizeof (cbc_uint8_arguments_t));
literal_start_p = (jmem_cpointer_t *) (((uint8_t *) buffer_p) + sizeof (cbc_uint8_arguments_t));
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) buffer_p;
argument_end = args_p->argument_end;
@@ -292,7 +292,8 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */
bytecode_p->refs = 1;
}
buffer_p += code_size;
JERRY_ASSERT ((code_size % sizeof (uint32_t)) == 0);
buffer_p += code_size / sizeof (uint32_t);
size -= code_size;
}
while (size > 0);
@@ -458,7 +459,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source
bool is_for_global, /**< snapshot would be executed as global (true)
* or eval (false) */
bool is_strict, /**< strict mode */
uint8_t *buffer_p, /**< buffer to save snapshot to */
uint32_t *buffer_p, /**< buffer to save snapshot to */
size_t buffer_size) /**< the buffer's size */
{
#ifdef JERRY_ENABLE_SNAPSHOT_SAVE
@@ -481,7 +482,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source
return 0;
}
snapshot_add_compiled_code (bytecode_data_p, buffer_p, buffer_size, &globals);
snapshot_add_compiled_code (bytecode_data_p, (uint8_t *) buffer_p, buffer_size, &globals);
if (globals.snapshot_error_occured)
{
@@ -507,13 +508,14 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source
return 0;
}
jerry_snapshot_set_offsets (buffer_p + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), JMEM_ALIGNMENT),
jerry_snapshot_set_offsets (buffer_p + (JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t),
JMEM_ALIGNMENT) / sizeof (uint32_t)),
(uint32_t) (header.lit_table_offset - sizeof (jerry_snapshot_header_t)),
lit_map_p);
size_t header_offset = 0;
snapshot_write_to_buffer_by_offset (buffer_p,
snapshot_write_to_buffer_by_offset ((uint8_t *) buffer_p,
buffer_size,
&header_offset,
&header,
@@ -549,7 +551,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source
* thrown error - otherwise
*/
jerry_value_t
jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
size_t snapshot_size, /**< size of snapshot */
bool copy_bytecode) /**< flag, indicating whether the passed snapshot
* buffer should be copied to the engine's memory.
@@ -586,7 +588,8 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
return ecma_raise_type_error (invalid_version_error_p);
}
if (!ecma_load_literals_from_snapshot (snapshot_data_p + header_p->lit_table_offset,
JERRY_ASSERT ((header_p->lit_table_offset % sizeof (uint32_t)) == 0);
if (!ecma_load_literals_from_snapshot ((uint32_t *) (snapshot_data_p + header_p->lit_table_offset),
header_p->lit_table_size,
&lit_map_p,
&literals_num))
@@ -882,7 +885,7 @@ size_t
jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source */
size_t source_size, /**< script source size */
bool is_strict, /**< strict mode */
uint8_t *buffer_p, /**< [out] buffer to save literals to */
uint32_t *buffer_p, /**< [out] buffer to save literals to */
size_t buffer_size, /**< the buffer's size */
bool is_c_format) /**< format-flag */
{
@@ -934,8 +937,10 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
return 0;
}
uint8_t * const buffer_start_p = buffer_p;
uint8_t * const buffer_end_p = buffer_p + buffer_size;
uint8_t *destination_p = (uint8_t *) buffer_p;
uint8_t *const buffer_start_p = destination_p;
uint8_t *const buffer_end_p = destination_p + buffer_size;
JMEM_DEFINE_LOCAL_ARRAY (literal_array, literal_count, ecma_string_t *);
lit_utf8_size_t literal_idx = 0;
@@ -968,43 +973,43 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
if (is_c_format)
{
/* Save literal count. */
buffer_p = jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
(const char *) "jerry_length_t literal_count = ",
0);
destination_p = jerry_append_chars_to_buffer (destination_p,
buffer_end_p,
(const char *) "jerry_length_t literal_count = ",
0);
buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count);
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
/* Save the array of literals. */
buffer_p = jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
";\n\njerry_char_ptr_t literals[",
0);
destination_p = jerry_append_chars_to_buffer (destination_p,
buffer_end_p,
";\n\njerry_char_ptr_t literals[",
0);
buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count);
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "] =\n{\n", 0);
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0);
for (lit_utf8_size_t i = 0; i < literal_count; i++)
{
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " \"", 0);
buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]);
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\"", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " \"", 0);
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\"", 0);
if (i < literal_count - 1)
{
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, ",", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0);
}
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\n", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0);
}
buffer_p = jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
(const char *) "};\n\njerry_length_t literal_sizes[",
0);
destination_p = jerry_append_chars_to_buffer (destination_p,
buffer_end_p,
(const char *) "};\n\njerry_length_t literal_sizes[",
0);
buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count);
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "] =\n{\n", 0);
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0);
}
/* Save the literal sizes respectively. */
@@ -1014,40 +1019,40 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
if (is_c_format)
{
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " ", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0);
}
buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, str_size);
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " ", 0);
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, str_size);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0);
if (is_c_format)
{
/* Show the given string as a comment. */
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "/* ", 0);
buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]);
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " */", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "/* ", 0);
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " */", 0);
if (i < literal_count - 1)
{
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, ",", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0);
}
}
else
{
buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]);
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
}
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\n", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0);
}
if (is_c_format)
{
buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) "};\n", 0);
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, (const char *) "};\n", 0);
}
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);
return buffer_p <= buffer_end_p ? (size_t) (buffer_p - buffer_start_p) : 0;
return destination_p <= buffer_end_p ? (size_t) (destination_p - buffer_start_p) : 0;
#else /* !JERRY_ENABLE_SNAPSHOT_SAVE */
JERRY_UNUSED (source_p);
JERRY_UNUSED (source_size);
+3 -3
View File
@@ -339,10 +339,10 @@ bool jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, jerry_size_t
* Snapshot functions
*/
size_t jerry_parse_and_save_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global,
bool is_strict, uint8_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_exec_snapshot (const void *snapshot_p, size_t snapshot_size, bool copy_bytecode);
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_size, bool copy_bytecode);
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
uint8_t *buffer_p, size_t buffer_size, bool is_c_format);
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);
/**
* @}