Fix style in liboptimizer

This commit is contained in:
e.gavrin
2014-08-13 16:41:39 +04:00
parent c5f33d184d
commit 350580c49e
5 changed files with 418 additions and 376 deletions
+24 -16
View File
@@ -26,12 +26,16 @@ deserialize_string_by_id (uint8_t id)
uint16_t offset; uint16_t offset;
if (bytecode_data == NULL) if (bytecode_data == NULL)
{
return NULL; return NULL;
}
size = *bytecode_data; size = *bytecode_data;
if (id >= size) if (id >= size)
{
return NULL; return NULL;
}
data = bytecode_data; data = bytecode_data;
@@ -49,27 +53,31 @@ deserialize_num_by_id (uint8_t id)
str_size = *bytecode_data; str_size = *bytecode_data;
if (id < str_size) if (id < str_size)
{ {
return 0; return 0;
} }
id = (uint8_t) (id - str_size); id = (uint8_t) (id - str_size);
if (num_data == NULL) if (num_data == NULL)
{
// Go to last string's offset
uint8_t *data = (uint8_t *) (bytecode_data + str_size * 2 - 1);
str_offset = *((uint16_t *) data);
data = bytecode_data + str_offset;
while (*data)
{ {
// Go to last string's offset data++;
uint8_t *data = (uint8_t *) (bytecode_data + str_size * 2 - 1);
str_offset = *((uint16_t *) data);
data = bytecode_data + str_offset;
while (*data)
data++;
num_size = *(++data);
num_data = (ecma_number_t *) ++data;
} }
num_size = *(++data);
num_data = (ecma_number_t *) ++data;
}
if (id >= num_size) if (id >= num_size)
{
return 0; return 0;
}
return num_data[id]; return num_data[id];
} }
@@ -86,9 +94,9 @@ deserialize_min_temp (void)
uint8_t str_size = *bytecode_data; uint8_t str_size = *bytecode_data;
if (num_size == 0) if (num_size == 0)
{ {
deserialize_num_by_id (str_size); // Init num_data and num_size deserialize_num_by_id (str_size); // Init num_data and num_size
} }
return (uint8_t) (str_size + num_size); return (uint8_t) (str_size + num_size);
} }
+301 -285
View File
@@ -32,7 +32,7 @@
assignment ... + assignment ... +
call_n ... call_n ...
var_?_end var_?_end
*/ */
static void static void
optimize_calls (OPCODE *opcodes) optimize_calls (OPCODE *opcodes)
{ {
@@ -41,15 +41,15 @@ optimize_calls (OPCODE *opcodes)
for (current_opcode = opcodes; for (current_opcode = opcodes;
current_opcode->op_idx != NAME_TO_ID (exitval); current_opcode->op_idx != NAME_TO_ID (exitval);
current_opcode++) current_opcode++)
{
if (current_opcode->op_idx == NAME_TO_ID (call_n)
&& (current_opcode + 1)->op_idx == NAME_TO_ID (assignment))
{ {
if (current_opcode->op_idx == NAME_TO_ID (call_n) OPCODE temp = *current_opcode;
&& (current_opcode + 1)->op_idx == NAME_TO_ID (assignment)) *current_opcode = *(current_opcode + 1);
{ *(current_opcode + 1) = temp;
OPCODE temp = *current_opcode;
*current_opcode = *(current_opcode + 1);
*(current_opcode + 1) = temp;
}
} }
}
} }
/* Move NUMBER opcodes from FROM to TO and adjust opcodes beetwen FROM and TO. */ /* Move NUMBER opcodes from FROM to TO and adjust opcodes beetwen FROM and TO. */
@@ -60,44 +60,50 @@ optimizer_move_opcodes (OPCODE *from, OPCODE *to, uint16_t number)
uint16_t i; uint16_t i;
if (to == from) if (to == from)
{
return; return;
}
for (i = 0; i < number; i++) for (i = 0; i < number; i++)
{
temp[i] = from[i]; temp[i] = from[i];
}
if (to > from) if (to > from)
{
if (number <= to - from)
{ {
if (number <= to - from) // Adjust opcodes up
{ for (current_opcode = from; current_opcode != to; current_opcode++)
// Adjust opcodes up {
for (current_opcode = from; current_opcode != to; current_opcode++) *current_opcode = *(current_opcode + number);
{ }
*current_opcode = *(current_opcode + number);
}
}
else
{
optimizer_move_opcodes (from + number, from, (uint16_t) (to - from));
}
} }
else
{
optimizer_move_opcodes (from + number, from, (uint16_t) (to - from));
}
}
else else
{
if (number <= from - to)
{ {
if (number <= from - to) // Adjust opcodes down
{ for (current_opcode = from; current_opcode != to; current_opcode--)
// Adjust opcodes down {
for (current_opcode = from; current_opcode != to; current_opcode--) *current_opcode = *(current_opcode - number);
{ }
*current_opcode = *(current_opcode - number);
}
}
else
{
optimizer_move_opcodes (to, to + number, (uint16_t) (from - to));
}
} }
else
{
optimizer_move_opcodes (to, to + number, (uint16_t) (from - to));
}
}
for (i = 0; i < number; i++) for (i = 0; i < number; i++)
{
to[i] = temp[i]; to[i] = temp[i];
}
} }
static uint16_t static uint16_t
@@ -115,198 +121,206 @@ optimizer_adjust_jumps (OPCODE *first_opcode, OPCODE *last_opcode, int16_t value
JERRY_ASSERT (first_opcode <= last_opcode); JERRY_ASSERT (first_opcode <= last_opcode);
for (current_opcode = first_opcode; current_opcode != last_opcode; current_opcode++) for (current_opcode = first_opcode; current_opcode != last_opcode; current_opcode++)
{
if (current_opcode->op_idx == NAME_TO_ID (is_true_jmp))
{ {
if (current_opcode->op_idx == NAME_TO_ID (is_true_jmp)) /* 19: is_true_jmp 2
{ 20: var_decl ...
/* 19: is_true_jmp 2
20: var_decl ...
becomes becomes
19: var_decl ... 19: var_decl ...
20: is_true_jmp 2 20: is_true_jmp 2
*/ */
if (current_opcode->data.is_true_jmp.opcode >= opcode_to_counter (last_opcode) if (current_opcode->data.is_true_jmp.opcode >= opcode_to_counter (last_opcode)
|| current_opcode->data.is_true_jmp.opcode < opcode_to_counter (first_opcode) - value) || current_opcode->data.is_true_jmp.opcode < opcode_to_counter (first_opcode) - value)
continue; {
continue;
}
/* 19: is_true_jmp 20 /* 19: is_true_jmp 20
20: assignment 20: assignment
21: var_decl 21: var_decl
becomes becomes
19: var_decl 19: var_decl
20: is_true_jmp 21 20: is_true_jmp 21
21: assignment 21: assignment
*/ */
if (current_opcode->data.is_true_jmp.opcode >= opcode_to_counter (first_opcode) if (current_opcode->data.is_true_jmp.opcode >= opcode_to_counter (first_opcode)
&& current_opcode->data.is_true_jmp.opcode <= opcode_to_counter (last_opcode) - value) && current_opcode->data.is_true_jmp.opcode <= opcode_to_counter (last_opcode) - value)
{ {
current_opcode->data.is_true_jmp.opcode = (T_IDX) (current_opcode->data.is_true_jmp.opcode + value); current_opcode->data.is_true_jmp.opcode = (T_IDX) (current_opcode->data.is_true_jmp.opcode + value);
continue; continue;
} }
/* 19: is_true_jmp 22 /* 19: is_true_jmp 22
20: assignment 20: assignment
21: var_decl 21: var_decl
22: var_decl 22: var_decl
becomes becomes
19: var_decl 19: var_decl
20: var_decl 20: var_decl
21: is_true_jmp 23 21: is_true_jmp 23
22: assignment 22: assignment
*/ */
if (current_opcode->data.is_true_jmp.opcode < opcode_to_counter (last_opcode)) if (current_opcode->data.is_true_jmp.opcode < opcode_to_counter (last_opcode))
{ {
current_opcode->data.is_true_jmp.opcode = (T_IDX) opcode_to_counter (last_opcode); current_opcode->data.is_true_jmp.opcode = (T_IDX) opcode_to_counter (last_opcode);
continue; continue;
} }
JERRY_UNREACHABLE (); JERRY_UNREACHABLE ();
}
if (current_opcode->op_idx == NAME_TO_ID (is_false_jmp))
{
/* 19: is_false_jmp 2
20: var_decl ...
becomes
19: var_decl ...
20: is_false_jmp 2
*/
if (current_opcode->data.is_false_jmp.opcode >= opcode_to_counter (last_opcode)
|| current_opcode->data.is_false_jmp.opcode < opcode_to_counter (first_opcode) - value)
continue;
/* 19: is_false_jmp 20
20: assignment
21: var_decl
becomes
19: var_decl
20: is_false_jmp 21
21: assignment
*/
if (current_opcode->data.is_false_jmp.opcode >= opcode_to_counter (first_opcode)
&& current_opcode->data.is_false_jmp.opcode <= opcode_to_counter (last_opcode) - value)
{
current_opcode->data.is_false_jmp.opcode = (T_IDX) (current_opcode->data.is_false_jmp.opcode + value);
continue;
}
/* 19: is_false_jmp 22
20: assignment
21: var_decl
22: var_decl
becomes
19: var_decl
20: var_decl
21: is_false_jmp 23
22: assignment
*/
if (current_opcode->data.is_false_jmp.opcode < opcode_to_counter (last_opcode))
{
current_opcode->data.is_false_jmp.opcode = (T_IDX) opcode_to_counter (last_opcode);
continue;
}
JERRY_UNREACHABLE ();
}
if (current_opcode->op_idx == NAME_TO_ID (jmp_down))
{
/* 19: jmp_down 1
20: assignment
21: var_decl ...
becomes
19: var_decl ...
20: jmp_down 1
21: assignment
*/
if (current_opcode->data.jmp_down.opcode_count < last_opcode - current_opcode)
continue;
/* 19: jmp_down 3
20: assignment
21: var_decl
becomes
19: var_decl
20: jmp_down 2
21: assignment
*/
if (current_opcode->data.jmp_down.opcode_count >= last_opcode - current_opcode + value)
{
current_opcode->data.jmp_down.opcode_count = (T_IDX) (current_opcode->data.jmp_down.opcode_count - value);
continue;
}
/* 19: jmp_down 3
20: assignment
21: var_decl
22: var_decl
becomes
19: var_decl
20: var_decl
21: jmp_down 2
22: assignment
*/
if (current_opcode->data.jmp_down.opcode_count >= last_opcode - current_opcode
&& current_opcode->data.jmp_down.opcode_count < last_opcode - current_opcode + value)
{
current_opcode->data.jmp_down.opcode_count = (T_IDX) (last_opcode - current_opcode);
continue;
}
JERRY_UNREACHABLE ();
}
if (current_opcode->op_idx == NAME_TO_ID (jmp_up))
{
/* 19: assignment
20: jmp_up 1
21: var_decl ...
becomes
19: var_decl ...
20: assignment
21: jmp_up 1
*/
if (current_opcode->data.jmp_up.opcode_count < current_opcode - first_opcode)
continue;
/* 19: jmp_up 1
20: assignment
21: var_decl
becomes
19: var_decl
20: jmp_up 2
21: assignment
*/
if (current_opcode->data.jmp_up.opcode_count >= current_opcode - first_opcode)
{
current_opcode->data.jmp_up.opcode_count = (T_IDX) (current_opcode->data.jmp_up.opcode_count + value);
continue;
}
JERRY_UNREACHABLE ();
}
} }
if (current_opcode->op_idx == NAME_TO_ID (is_false_jmp))
{
/* 19: is_false_jmp 2
20: var_decl ...
becomes
19: var_decl ...
20: is_false_jmp 2
*/
if (current_opcode->data.is_false_jmp.opcode >= opcode_to_counter (last_opcode)
|| current_opcode->data.is_false_jmp.opcode < opcode_to_counter (first_opcode) - value)
{
continue;
}
/* 19: is_false_jmp 20
20: assignment
21: var_decl
becomes
19: var_decl
20: is_false_jmp 21
21: assignment
*/
if (current_opcode->data.is_false_jmp.opcode >= opcode_to_counter (first_opcode)
&& current_opcode->data.is_false_jmp.opcode <= opcode_to_counter (last_opcode) - value)
{
current_opcode->data.is_false_jmp.opcode = (T_IDX) (current_opcode->data.is_false_jmp.opcode + value);
continue;
}
/* 19: is_false_jmp 22
20: assignment
21: var_decl
22: var_decl
becomes
19: var_decl
20: var_decl
21: is_false_jmp 23
22: assignment
*/
if (current_opcode->data.is_false_jmp.opcode < opcode_to_counter (last_opcode))
{
current_opcode->data.is_false_jmp.opcode = (T_IDX) opcode_to_counter (last_opcode);
continue;
}
JERRY_UNREACHABLE ();
}
if (current_opcode->op_idx == NAME_TO_ID (jmp_down))
{
/* 19: jmp_down 1
20: assignment
21: var_decl ...
becomes
19: var_decl ...
20: jmp_down 1
21: assignment
*/
if (current_opcode->data.jmp_down.opcode_count < last_opcode - current_opcode)
{
continue;
}
/* 19: jmp_down 3
20: assignment
21: var_decl
becomes
19: var_decl
20: jmp_down 2
21: assignment
*/
if (current_opcode->data.jmp_down.opcode_count >= last_opcode - current_opcode + value)
{
current_opcode->data.jmp_down.opcode_count = (T_IDX) (current_opcode->data.jmp_down.opcode_count - value);
continue;
}
/* 19: jmp_down 3
20: assignment
21: var_decl
22: var_decl
becomes
19: var_decl
20: var_decl
21: jmp_down 2
22: assignment
*/
if (current_opcode->data.jmp_down.opcode_count >= last_opcode - current_opcode
&& current_opcode->data.jmp_down.opcode_count < last_opcode - current_opcode + value)
{
current_opcode->data.jmp_down.opcode_count = (T_IDX) (last_opcode - current_opcode);
continue;
}
JERRY_UNREACHABLE ();
}
if (current_opcode->op_idx == NAME_TO_ID (jmp_up))
{
/* 19: assignment
20: jmp_up 1
21: var_decl ...
becomes
19: var_decl ...
20: assignment
21: jmp_up 1
*/
if (current_opcode->data.jmp_up.opcode_count < current_opcode - first_opcode)
{
continue;
}
/* 19: jmp_up 1
20: assignment
21: var_decl
becomes
19: var_decl
20: jmp_up 2
21: assignment
*/
if (current_opcode->data.jmp_up.opcode_count >= current_opcode - first_opcode)
{
current_opcode->data.jmp_up.opcode_count = (T_IDX) (current_opcode->data.jmp_up.opcode_count + value);
continue;
}
JERRY_UNREACHABLE ();
}
}
} }
/* Reorder scope like /* Reorder scope like
@@ -314,7 +328,7 @@ optimizer_adjust_jumps (OPCODE *first_opcode, OPCODE *last_opcode, int16_t value
func_decl func_decl
var_decl var_decl
other opcodes other opcodes
*/ */
void void
optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end) optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end)
{ {
@@ -325,92 +339,94 @@ optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end)
OPCODE *var_decls_start; OPCODE *var_decls_start;
for (current_opcode = processed_opcode; current_opcode != last_opcode; current_opcode++) for (current_opcode = processed_opcode; current_opcode != last_opcode; current_opcode++)
{
if (current_opcode->op_idx == NAME_TO_ID (assignment)
&& current_opcode->data.assignment.type_value_right == OPCODE_ARG_TYPE_STRING
&& !__strcmp ("use strict", (char *) deserialize_string_by_id (current_opcode->data.assignment.value_right)))
{ {
if (current_opcode->op_idx == NAME_TO_ID (assignment) optimizer_move_opcodes (current_opcode, processed_opcode, 1);
&& current_opcode->data.assignment.type_value_right == OPCODE_ARG_TYPE_STRING optimizer_adjust_jumps (processed_opcode + 1, current_opcode + 1, 1);
&& !__strcmp ("use strict", (char *) deserialize_string_by_id (current_opcode->data.assignment.value_right))) processed_opcode++;
{ break;
optimizer_move_opcodes (current_opcode, processed_opcode, 1);
optimizer_adjust_jumps (processed_opcode + 1, current_opcode + 1, 1);
processed_opcode++;
break;
}
} }
}
for (current_opcode = processed_opcode; current_opcode != last_opcode;) for (current_opcode = processed_opcode; current_opcode != last_opcode;)
{
if (current_opcode->op_idx == NAME_TO_ID (func_decl_0)
|| current_opcode->op_idx == NAME_TO_ID (func_decl_1)
|| current_opcode->op_idx == NAME_TO_ID (func_decl_2)
|| current_opcode->op_idx == NAME_TO_ID (func_decl_n))
{ {
if (current_opcode->op_idx == NAME_TO_ID (func_decl_0) __printf ("%p\n", processed_opcode);
|| current_opcode->op_idx == NAME_TO_ID (func_decl_1) OPCODE *fun_opcode;
|| current_opcode->op_idx == NAME_TO_ID (func_decl_2) int16_t value, jmp_offset = 0;
|| current_opcode->op_idx == NAME_TO_ID (func_decl_n)) for (fun_opcode = current_opcode + 1; fun_opcode != last_opcode; fun_opcode++)
{
if (fun_opcode->op_idx == NAME_TO_ID (jmp_down))
{ {
__printf ("%p\n", processed_opcode); jmp_offset = (int16_t) (fun_opcode - current_opcode);
OPCODE *fun_opcode; fun_opcode += fun_opcode->data.jmp_down.opcode_count;
int16_t value, jmp_offset = 0; break;
for (fun_opcode = current_opcode + 1; fun_opcode != last_opcode; fun_opcode++)
{
if (fun_opcode->op_idx == NAME_TO_ID (jmp_down))
{
jmp_offset = (int16_t) (fun_opcode - current_opcode);
fun_opcode += fun_opcode->data.jmp_down.opcode_count;
break;
}
}
JERRY_ASSERT (fun_opcode <= last_opcode);
value = (int16_t) (fun_opcode - current_opcode);
optimizer_move_opcodes (current_opcode, processed_opcode, (uint16_t) value);
// Adjust jumps inside func_decl except end's jmp_down
optimizer_adjust_jumps (processed_opcode + jmp_offset + 1,
processed_opcode + value,
(int16_t) (processed_opcode - current_opcode));
optimizer_adjust_jumps (processed_opcode + value,
fun_opcode,
value);
processed_opcode += value;
current_opcode = fun_opcode;
continue;
} }
else }
current_opcode++; JERRY_ASSERT (fun_opcode <= last_opcode);
value = (int16_t) (fun_opcode - current_opcode);
optimizer_move_opcodes (current_opcode, processed_opcode, (uint16_t) value);
// Adjust jumps inside func_decl except end's jmp_down
optimizer_adjust_jumps (processed_opcode + jmp_offset + 1,
processed_opcode + value,
(int16_t) (processed_opcode - current_opcode));
optimizer_adjust_jumps (processed_opcode + value,
fun_opcode,
value);
processed_opcode += value;
current_opcode = fun_opcode;
continue;
} }
else
{
current_opcode++;
}
}
var_decls_start = processed_opcode; var_decls_start = processed_opcode;
for (current_opcode = processed_opcode; current_opcode != last_opcode; current_opcode++) for (current_opcode = processed_opcode; current_opcode != last_opcode; current_opcode++)
{
if (current_opcode->op_idx == NAME_TO_ID (var_decl))
{ {
if (current_opcode->op_idx == NAME_TO_ID (var_decl)) // If variable already declared, replace it with nop
bool was_decl = false;
if (var_decls_start->op_idx == NAME_TO_ID (var_decl) && var_decls_start != current_opcode)
{
OPCODE *var_decls_iterator;
for (var_decls_iterator = var_decls_start;
var_decls_iterator != processed_opcode;
var_decls_iterator++)
{ {
// If variable already declared, replace it with nop JERRY_ASSERT (var_decls_iterator->op_idx == NAME_TO_ID (var_decl));
bool was_decl = false; if (var_decls_iterator->data.var_decl.variable_name
if (var_decls_start->op_idx == NAME_TO_ID (var_decl) && var_decls_start != current_opcode) == current_opcode->data.var_decl.variable_name)
{ {
OPCODE *var_decls_iterator; was_decl = true;
for (var_decls_iterator = var_decls_start; break;
var_decls_iterator != processed_opcode; }
var_decls_iterator++)
{
JERRY_ASSERT (var_decls_iterator->op_idx == NAME_TO_ID (var_decl));
if (var_decls_iterator->data.var_decl.variable_name
== current_opcode->data.var_decl.variable_name)
{
was_decl = true;
break;
}
}
}
if (was_decl)
{
current_opcode->op_idx = NAME_TO_ID (nop);
}
else
{
optimizer_move_opcodes (current_opcode, processed_opcode, 1);
optimizer_adjust_jumps (processed_opcode + 1, current_opcode + 1, 1);
processed_opcode++;
}
} }
}
if (was_decl)
{
current_opcode->op_idx = NAME_TO_ID (nop);
}
else
{
optimizer_move_opcodes (current_opcode, processed_opcode, 1);
optimizer_adjust_jumps (processed_opcode + 1, current_opcode + 1, 1);
processed_opcode++;
}
} }
}
} }
void void
+19 -11
View File
@@ -27,12 +27,14 @@
#define OPCODE_SIZE(op) \ #define OPCODE_SIZE(op) \
sizeof (struct __op_##op) + 1, sizeof (struct __op_##op) + 1,
static char* opcode_names[] = { static char* opcode_names[] =
{
OP_LIST (OPCODE_STR) OP_LIST (OPCODE_STR)
"" ""
}; };
static uint8_t opcode_sizes[] = { static uint8_t opcode_sizes[] =
{
OP_LIST (OPCODE_SIZE) OP_LIST (OPCODE_SIZE)
0 0
}; };
@@ -45,10 +47,10 @@ pp_strings (const char *strings[], uint8_t size)
__printf ("STRINGS %d:\n", size); __printf ("STRINGS %d:\n", size);
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
__printf ("%3d %5d %20s\n", i, offset, strings[i]); __printf ("%3d %5d %20s\n", i, offset, strings[i]);
offset = (uint16_t) (offset + __strlen (strings[i]) + 1); offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
} }
} }
void void
@@ -58,9 +60,9 @@ pp_nums (const ecma_number_t nums[], uint8_t size, uint8_t strings_num)
__printf ("NUMS %d:\n", size); __printf ("NUMS %d:\n", size);
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
__printf ("%3d %7d\n", i + strings_num, (int) nums[i]); __printf ("%3d %7d\n", i + strings_num, (int) nums[i]);
} }
__printf ("\n"); __printf ("\n");
} }
@@ -355,13 +357,17 @@ pp_opcode (opcode_counter_t oc, OPCODE opcode, bool is_rewrite)
__printf ("%03d: %20s ", oc, opcode_names[opcode_num]); __printf ("%03d: %20s ", oc, opcode_names[opcode_num]);
if (opcode_num != NAME_TO_ID (nop) && opcode_num != NAME_TO_ID (ret) if (opcode_num != NAME_TO_ID (nop) && opcode_num != NAME_TO_ID (ret)
&& opcode_num != NAME_TO_ID (end_with)) && opcode_num != NAME_TO_ID (end_with))
{
for (i = 1; i < opcode_sizes[opcode_num]; i++)
{ {
for (i = 1; i < opcode_sizes[opcode_num]; i++) __printf ("%4d ", ((uint8_t*) & opcode)[i]);
__printf ("%4d ", ((uint8_t*)&opcode)[i]);
} }
}
for (; i < 4; i++) for (; i < 4; i++)
{
__printf (" "); __printf (" ");
}
__printf (" // "); __printf (" // ");
@@ -447,7 +453,9 @@ pp_opcode (opcode_counter_t oc, OPCODE opcode, bool is_rewrite)
} }
if (is_rewrite) if (is_rewrite)
{
__printf (" // REWRITE"); __printf (" // REWRITE");
}
__printf ("\n"); __printf ("\n");
} }
+40 -30
View File
@@ -37,12 +37,14 @@ serializer_dump_strings (const char *strings[], uint8_t size)
uint16_t offset = (uint16_t) (size * 2 + 1), res; uint16_t offset = (uint16_t) (size * 2 + 1), res;
if (print_opcodes) if (print_opcodes)
{
pp_strings (strings, size); pp_strings (strings, size);
}
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
offset = (uint16_t) (offset + __strlen (strings[i]) + 1); offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
} }
bytecode_data = mem_heap_alloc_block (offset, MEM_HEAP_ALLOC_SHORT_TERM); bytecode_data = mem_heap_alloc_block (offset, MEM_HEAP_ALLOC_SHORT_TERM);
res = offset; res = offset;
@@ -50,22 +52,22 @@ serializer_dump_strings (const char *strings[], uint8_t size)
bytecode_data[0] = size; bytecode_data[0] = size;
offset = (uint16_t) (size * 2 + 1); offset = (uint16_t) (size * 2 + 1);
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
*((uint16_t *) (bytecode_data + i * 2 + 1)) = offset; *((uint16_t *) (bytecode_data + i * 2 + 1)) = offset;
offset = (uint16_t) (offset + __strlen (strings[i]) + 1); offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
} }
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
offset = *((uint16_t *) (bytecode_data + i * 2 + 1)); offset = *((uint16_t *) (bytecode_data + i * 2 + 1));
__strncpy ((char *) (bytecode_data + offset), strings[i], __strlen (strings[i]) + 1); __strncpy ((char *) (bytecode_data + offset), strings[i], __strlen (strings[i]) + 1);
} }
#ifndef JERRY_NDEBUG #ifndef JERRY_NDEBUG
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
JERRY_ASSERT (!__strcmp (strings[i], (const char *) deserialize_string_by_id (i))); JERRY_ASSERT (!__strcmp (strings[i], (const char *) deserialize_string_by_id (i)));
} }
#endif #endif
return res; return res;
@@ -77,11 +79,15 @@ serializer_dump_nums (const ecma_number_t nums[], uint8_t size, uint16_t offset,
uint8_t i, *data, type_size = sizeof (ecma_number_t); uint8_t i, *data, type_size = sizeof (ecma_number_t);
if (print_opcodes) if (print_opcodes)
{
pp_nums (nums, size, strings_num); pp_nums (nums, size, strings_num);
}
data = mem_heap_alloc_block ((size_t) (offset + size * type_size + 1), MEM_HEAP_ALLOC_LONG_TERM); data = mem_heap_alloc_block ((size_t) (offset + size * type_size + 1), MEM_HEAP_ALLOC_LONG_TERM);
if (!data) if (!data)
{
parser_fatal (ERR_MEMORY); parser_fatal (ERR_MEMORY);
}
__memcpy (data, bytecode_data, offset); __memcpy (data, bytecode_data, offset);
mem_heap_free_block (bytecode_data); mem_heap_free_block (bytecode_data);
@@ -90,16 +96,16 @@ serializer_dump_nums (const ecma_number_t nums[], uint8_t size, uint16_t offset,
data[0] = size; data[0] = size;
data++; data++;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
__memcpy (data, nums + i, type_size); __memcpy (data, nums + i, type_size);
data += type_size; data += type_size;
} }
#ifndef JERRY_NDEBUG #ifndef JERRY_NDEBUG
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
JERRY_ASSERT (nums[i] == deserialize_num_by_id ((uint8_t) (i + strings_num))); JERRY_ASSERT (nums[i] == deserialize_num_by_id ((uint8_t) (i + strings_num)));
} }
JERRY_ASSERT (deserialize_min_temp () == (uint8_t) (size + strings_num)); JERRY_ASSERT (deserialize_min_temp () == (uint8_t) (size + strings_num));
#endif #endif
@@ -111,22 +117,24 @@ void
serializer_dump_opcode (OPCODE opcode) serializer_dump_opcode (OPCODE opcode)
{ {
if (print_opcodes) if (print_opcodes)
{
pp_opcode (opcode_counter, opcode, false); pp_opcode (opcode_counter, opcode, false);
}
JERRY_ASSERT( opcode_counter < MAX_OPCODES ); JERRY_ASSERT (opcode_counter < MAX_OPCODES);
bytecode_opcodes[opcode_counter++] = opcode; bytecode_opcodes[opcode_counter++] = opcode;
} }
void void
serializer_rewrite_opcode (const opcode_counter_t loc, OPCODE opcode) serializer_rewrite_opcode (const opcode_counter_t loc, OPCODE opcode)
{ {
JERRY_ASSERT( loc < MAX_OPCODES ); JERRY_ASSERT (loc < MAX_OPCODES);
bytecode_opcodes[loc] = opcode; bytecode_opcodes[loc] = opcode;
if (print_opcodes) if (print_opcodes)
{ {
pp_opcode (loc, opcode, true); pp_opcode (loc, opcode, true);
} }
} }
void void
@@ -135,19 +143,21 @@ serializer_print_opcodes (void)
opcode_counter_t loc; opcode_counter_t loc;
if (!print_opcodes) if (!print_opcodes)
{
return; return;
}
__printf ("AFTER OPTIMIZER:\n"); __printf ("AFTER OPTIMIZER:\n");
for (loc = 0; (*(uint32_t *) (bytecode_opcodes + loc) != 0x0); loc++) for (loc = 0; (*(uint32_t *) (bytecode_opcodes + loc) != 0x0); loc++)
{ {
pp_opcode (loc, bytecode_opcodes[loc], false); pp_opcode (loc, bytecode_opcodes[loc], false);
} }
} }
void void
serializer_free (void) serializer_free (void)
{ {
mem_heap_free_block( bytecode_data); mem_heap_free_block (bytecode_data);
bytecode_data = NULL; bytecode_data = NULL;
} }
+1 -1
View File
@@ -32,6 +32,6 @@ void serializer_rewrite_opcode (const opcode_counter_t, OPCODE);
void serializer_print_opcodes (void); void serializer_print_opcodes (void);
void serializer_free(void); void serializer_free (void);
#endif // SERIALIZER_H #endif // SERIALIZER_H