diff --git a/src/liboptimizer/deserializer.c b/src/liboptimizer/deserializer.c index 4ec0a3824..2c4e64077 100644 --- a/src/liboptimizer/deserializer.c +++ b/src/liboptimizer/deserializer.c @@ -26,12 +26,16 @@ deserialize_string_by_id (uint8_t id) uint16_t offset; if (bytecode_data == NULL) + { return NULL; - + } + size = *bytecode_data; if (id >= size) + { return NULL; + } data = bytecode_data; @@ -42,34 +46,38 @@ deserialize_string_by_id (uint8_t id) return ((const ecma_char_t *) bytecode_data + offset); } -ecma_number_t +ecma_number_t deserialize_num_by_id (uint8_t id) { uint16_t str_size, str_offset; str_size = *bytecode_data; if (id < str_size) - { - return 0; - } + { + return 0; + } id = (uint8_t) (id - str_size); - - 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) - data++; - - num_size = *(++data); - num_data = (ecma_number_t *) ++data; + 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) + { + data++; } + num_size = *(++data); + num_data = (ecma_number_t *) ++data; + } + if (id >= num_size) + { return 0; + } return num_data[id]; } @@ -80,15 +88,15 @@ deserialize_bytecode (void) return bytecode_opcodes; } -uint8_t +uint8_t deserialize_min_temp (void) { uint8_t str_size = *bytecode_data; - + 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); } diff --git a/src/liboptimizer/optimizer-passes.c b/src/liboptimizer/optimizer-passes.c index 449323c33..3314865e3 100644 --- a/src/liboptimizer/optimizer-passes.c +++ b/src/liboptimizer/optimizer-passes.c @@ -21,8 +21,8 @@ #define NAME_TO_ID(op) (__op__idx_##op) -/* Reorder bytecode like - +/* Reorder bytecode like + call_n ... assignment ... + var_?_end @@ -32,24 +32,24 @@ assignment ... + call_n ... var_?_end -*/ + */ static void optimize_calls (OPCODE *opcodes) { OPCODE *current_opcode; for (current_opcode = opcodes; - current_opcode->op_idx != NAME_TO_ID (exitval); + current_opcode->op_idx != NAME_TO_ID (exitval); 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) - && (current_opcode + 1)->op_idx == NAME_TO_ID (assignment)) - { - OPCODE temp = *current_opcode; - *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. */ @@ -60,44 +60,50 @@ optimizer_move_opcodes (OPCODE *from, OPCODE *to, uint16_t number) uint16_t i; if (to == from) + { return; + } for (i = 0; i < number; i++) + { temp[i] = from[i]; + } if (to > from) + { + if (number <= to - from) { - if (number <= to - from) - { - // Adjust opcodes up - for (current_opcode = from; current_opcode != to; current_opcode++) - { - *current_opcode = *(current_opcode + number); - } - } - else - { - optimizer_move_opcodes (from + number, from, (uint16_t) (to - from)); - } + // Adjust opcodes up + for (current_opcode = from; current_opcode != to; current_opcode++) + { + *current_opcode = *(current_opcode + number); + } } + else + { + optimizer_move_opcodes (from + number, from, (uint16_t) (to - from)); + } + } else + { + if (number <= from - to) { - if (number <= from - to) - { - // Adjust opcodes down - for (current_opcode = from; current_opcode != to; current_opcode--) - { - *current_opcode = *(current_opcode - number); - } - } - else - { - optimizer_move_opcodes (to, to + number, (uint16_t) (from - to)); - } + // Adjust opcodes down + for (current_opcode = from; current_opcode != to; current_opcode--) + { + *current_opcode = *(current_opcode - number); + } } + else + { + optimizer_move_opcodes (to, to + number, (uint16_t) (from - to)); + } + } for (i = 0; i < number; i++) + { to[i] = temp[i]; + } } 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); 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 ... - 20: is_true_jmp 2 - */ - 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) - continue; + 19: var_decl ... + 20: is_true_jmp 2 + */ + 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) + { + continue; + } - /* 19: is_true_jmp 20 - 20: assignment - 21: var_decl + /* 19: is_true_jmp 20 + 20: assignment + 21: var_decl - becomes + becomes - 19: var_decl - 20: is_true_jmp 21 - 21: assignment - */ - 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 = (T_IDX) (current_opcode->data.is_true_jmp.opcode + value); - continue; - } + 19: var_decl + 20: is_true_jmp 21 + 21: assignment + */ + 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 = (T_IDX) (current_opcode->data.is_true_jmp.opcode + value); + continue; + } - /* 19: is_true_jmp 22 - 20: assignment - 21: var_decl - 22: var_decl + /* 19: is_true_jmp 22 + 20: assignment + 21: var_decl + 22: var_decl - becomes + becomes - 19: var_decl - 20: var_decl - 21: is_true_jmp 23 - 22: assignment - */ - 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); - continue; - } + 19: var_decl + 20: var_decl + 21: is_true_jmp 23 + 22: assignment + */ + 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); + 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 (); - } + 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 @@ -314,7 +328,7 @@ optimizer_adjust_jumps (OPCODE *first_opcode, OPCODE *last_opcode, int16_t value func_decl var_decl other opcodes -*/ + */ void optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end) { @@ -325,96 +339,98 @@ optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end) OPCODE *var_decls_start; 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) - && 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))) - { - optimizer_move_opcodes (current_opcode, processed_opcode, 1); - optimizer_adjust_jumps (processed_opcode + 1, current_opcode + 1, 1); - 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;) + { + 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) - || 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)) + __printf ("%p\n", processed_opcode); + OPCODE *fun_opcode; + int16_t value, jmp_offset = 0; + 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); - OPCODE *fun_opcode; - int16_t value, jmp_offset = 0; - 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; + jmp_offset = (int16_t) (fun_opcode - current_opcode); + fun_opcode += fun_opcode->data.jmp_down.opcode_count; + break; } - 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; 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 - 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++) - { - 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++; - } + 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++; + } } + } } void optimizer_run_passes (OPCODE* opcodes) { optimize_calls (opcodes); -} \ No newline at end of file +} diff --git a/src/liboptimizer/pretty-printer.c b/src/liboptimizer/pretty-printer.c index f03e631e3..e1ca1837e 100644 --- a/src/liboptimizer/pretty-printer.c +++ b/src/liboptimizer/pretty-printer.c @@ -27,12 +27,14 @@ #define OPCODE_SIZE(op) \ sizeof (struct __op_##op) + 1, -static char* opcode_names[] = { +static char* opcode_names[] = +{ OP_LIST (OPCODE_STR) "" }; -static uint8_t opcode_sizes[] = { +static uint8_t opcode_sizes[] = +{ OP_LIST (OPCODE_SIZE) 0 }; @@ -45,22 +47,22 @@ pp_strings (const char *strings[], uint8_t size) __printf ("STRINGS %d:\n", size); for (i = 0; i < size; i++) - { - __printf ("%3d %5d %20s\n", i, offset, strings[i]); - offset = (uint16_t) (offset + __strlen (strings[i]) + 1); - } + { + __printf ("%3d %5d %20s\n", i, offset, strings[i]); + offset = (uint16_t) (offset + __strlen (strings[i]) + 1); + } } -void +void pp_nums (const ecma_number_t nums[], uint8_t size, uint8_t strings_num) { uint8_t i; __printf ("NUMS %d:\n", size); 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"); } @@ -167,7 +169,7 @@ dump_variable (T_IDX id) __printf (" " equals " " new " "); \ dump_variable (opcode.data.op.name); \ __printf (start end ";"); \ - break; + break; #define CASE_VARG_1_NAME_LHS(op, lhs, equals, new, name, start, arg, end) \ case NAME_TO_ID (op): \ @@ -177,7 +179,7 @@ dump_variable (T_IDX id) __printf (start); \ dump_variable (opcode.data.op.arg); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_N_NAME_LHS(op, lhs, equals, new, name, start, arg, end) \ case NAME_TO_ID (op): \ @@ -188,14 +190,14 @@ dump_variable (T_IDX id) dump_variable (opcode.data.op.arg); \ __printf (" ..."); \ varg_end = end; \ - break; + break; #define CASE_VARG_0_NAME(op, new, name, start, end) \ case NAME_TO_ID (op): \ __printf (new " "); \ dump_variable (opcode.data.op.name); \ __printf (start end ";"); \ - break; + break; #define CASE_VARG_1_NAME(op, new, name, start, arg1, end) \ case NAME_TO_ID (op): \ @@ -204,7 +206,7 @@ dump_variable (T_IDX id) __printf (start); \ dump_variable (opcode.data.op.arg1); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_2_NAME(op, new, name, start, arg1, arg2, end) \ case NAME_TO_ID (op): \ @@ -215,7 +217,7 @@ dump_variable (T_IDX id) __printf (", "); \ dump_variable (opcode.data.op.arg2); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_N_NAME(op, new, name, start, arg1, arg2, end) \ case NAME_TO_ID (op): \ @@ -227,14 +229,14 @@ dump_variable (T_IDX id) dump_variable (opcode.data.op.arg2); \ __printf (" ..."); \ varg_end = end; \ - break; + break; #define CASE_VARG_0_LHS(op, lhs, equals, start, end) \ case NAME_TO_ID (op): \ dump_variable (opcode.data.op.lhs); \ __printf (" " equals " " start); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_1_LHS(op, lhs, equals, start, arg1, end) \ case NAME_TO_ID (op): \ @@ -242,7 +244,7 @@ dump_variable (T_IDX id) __printf (" " equals " " start); \ dump_variable (opcode.data.op.arg1); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_2_LHS(op, lhs, equals, start, arg1, arg2, end) \ case NAME_TO_ID (op): \ @@ -252,7 +254,7 @@ dump_variable (T_IDX id) __printf (", "); \ dump_variable (opcode.data.op.arg2); \ __printf (end ";"); \ - break; + break; #define CASE_VARG_N_LHS(op, lhs, equals, start, arg1, arg2, end) \ case NAME_TO_ID (op): \ @@ -263,14 +265,14 @@ dump_variable (T_IDX id) dump_variable (opcode.data.op.arg2); \ __printf (" ..."); \ varg_end = end; \ - break; + break; #define CASE_VARG_1_END(op, arg1) \ case NAME_TO_ID (op): \ __printf ("... "); \ dump_variable (opcode.data.op.arg1); \ __printf ("%s;", varg_end); \ - break; + break; #define CASE_VARG_2_END(op, arg1, arg2) \ case NAME_TO_ID (op): \ @@ -279,7 +281,7 @@ dump_variable (T_IDX id) __printf (", "); \ dump_variable (opcode.data.op.arg2); \ __printf ("%s;", varg_end); \ - break; + break; #define CASE_VARG_3_END(op, arg1, arg2, arg3) \ case NAME_TO_ID (op): \ @@ -290,7 +292,7 @@ dump_variable (T_IDX id) __printf (", "); \ dump_variable (opcode.data.op.arg3); \ __printf ("%s;", varg_end); \ - break; + break; #define CASE_VARG_3(op, arg1, arg2, arg3) \ case NAME_TO_ID (op): \ @@ -301,20 +303,20 @@ dump_variable (T_IDX id) __printf (", "); \ dump_variable (opcode.data.op.arg3); \ __printf (" ..."); \ - break; + break; #define CASE_EXIT(op, name, field) \ case NAME_TO_ID (op): \ __printf (name " "); \ __printf ("%d;", opcode.data.op.field); \ - break; + break; #define CASE_SINGLE_ADDRESS(op, name, field) \ case NAME_TO_ID (op): \ __printf (name " "); \ dump_variable (opcode.data.op.field); \ __printf (";"); \ - break; + break; #define CASE_ZERO_ADDRESS(op, name) \ case NAME_TO_ID (op): \ @@ -353,15 +355,19 @@ pp_opcode (opcode_counter_t oc, OPCODE opcode, bool is_rewrite) uint8_t opcode_num = opcode.op_idx; __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)) + { + 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++) + { __printf (" "); + } __printf (" // "); @@ -447,7 +453,9 @@ pp_opcode (opcode_counter_t oc, OPCODE opcode, bool is_rewrite) } if (is_rewrite) + { __printf (" // REWRITE"); + } __printf ("\n"); } diff --git a/src/liboptimizer/serializer.c b/src/liboptimizer/serializer.c index 55debc942..d0f5c4b60 100644 --- a/src/liboptimizer/serializer.c +++ b/src/liboptimizer/serializer.c @@ -37,12 +37,14 @@ serializer_dump_strings (const char *strings[], uint8_t size) uint16_t offset = (uint16_t) (size * 2 + 1), res; if (print_opcodes) + { pp_strings (strings, size); + } 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); res = offset; @@ -50,38 +52,42 @@ serializer_dump_strings (const char *strings[], uint8_t size) bytecode_data[0] = size; offset = (uint16_t) (size * 2 + 1); for (i = 0; i < size; i++) - { - *((uint16_t *) (bytecode_data + i * 2 + 1)) = offset; - offset = (uint16_t) (offset + __strlen (strings[i]) + 1); - } + { + *((uint16_t *) (bytecode_data + i * 2 + 1)) = offset; + offset = (uint16_t) (offset + __strlen (strings[i]) + 1); + } for (i = 0; i < size; i++) - { - offset = *((uint16_t *) (bytecode_data + i * 2 + 1)); - __strncpy ((char *) (bytecode_data + offset), strings[i], __strlen (strings[i]) + 1); - } + { + offset = *((uint16_t *) (bytecode_data + i * 2 + 1)); + __strncpy ((char *) (bytecode_data + offset), strings[i], __strlen (strings[i]) + 1); + } #ifndef JERRY_NDEBUG 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 return res; } -void +void serializer_dump_nums (const ecma_number_t nums[], uint8_t size, uint16_t offset, uint8_t strings_num) { uint8_t i, *data, type_size = sizeof (ecma_number_t); if (print_opcodes) + { pp_nums (nums, size, strings_num); + } data = mem_heap_alloc_block ((size_t) (offset + size * type_size + 1), MEM_HEAP_ALLOC_LONG_TERM); if (!data) + { parser_fatal (ERR_MEMORY); + } __memcpy (data, bytecode_data, offset); 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++; for (i = 0; i < size; i++) - { - __memcpy (data, nums + i, type_size); - data += type_size; - } + { + __memcpy (data, nums + i, type_size); + data += type_size; + } #ifndef JERRY_NDEBUG 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)); #endif @@ -111,22 +117,24 @@ void serializer_dump_opcode (OPCODE opcode) { if (print_opcodes) + { pp_opcode (opcode_counter, opcode, false); + } - JERRY_ASSERT( opcode_counter < MAX_OPCODES ); + JERRY_ASSERT (opcode_counter < MAX_OPCODES); bytecode_opcodes[opcode_counter++] = opcode; } void serializer_rewrite_opcode (const opcode_counter_t loc, OPCODE opcode) { - JERRY_ASSERT( loc < MAX_OPCODES ); + JERRY_ASSERT (loc < MAX_OPCODES); bytecode_opcodes[loc] = opcode; if (print_opcodes) - { - pp_opcode (loc, opcode, true); - } + { + pp_opcode (loc, opcode, true); + } } void @@ -135,19 +143,21 @@ serializer_print_opcodes (void) opcode_counter_t loc; if (!print_opcodes) + { return; - + } + __printf ("AFTER OPTIMIZER:\n"); 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 serializer_free (void) { - mem_heap_free_block( bytecode_data); + mem_heap_free_block (bytecode_data); bytecode_data = NULL; } diff --git a/src/liboptimizer/serializer.h b/src/liboptimizer/serializer.h index 38a8eb7a6..5dabb1dc9 100644 --- a/src/liboptimizer/serializer.h +++ b/src/liboptimizer/serializer.h @@ -14,7 +14,7 @@ */ #ifndef SERIALIZER_H -#define SERIALIZER_H +#define SERIALIZER_H #include "globals.h" #include "opcodes.h" @@ -32,6 +32,6 @@ void serializer_rewrite_opcode (const opcode_counter_t, OPCODE); void serializer_print_opcodes (void); -void serializer_free(void); +void serializer_free (void); #endif // SERIALIZER_H