Add try-catch-finally support: parse and generate opcodes for this construct
Fix varg generation: generate *_n opcodes with parameters in following meta opcodes Add stack internal structure: dimanically allocated stack. Use dynamically allocated memory in parser: every local and global variables are stored in dinamically allocated stacks. Use dynamically allocated memory in serializer: opcodes are also stored in stack. Change is_true_jmp and is_false_jmp opcodes to relative. Change *jmp* opcodes to be able to store opcode_counter_t instead of idx_t.
This commit is contained in:
@@ -13,12 +13,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef BYTECODE_LINUX_H
|
||||
#define BYTECODE_LINUX_H
|
||||
#ifndef BYTECODE_DATA_H
|
||||
#define BYTECODE_DATA_H
|
||||
|
||||
#include "opcodes.h"
|
||||
#include "stack.h"
|
||||
#include "jerry-libc.h"
|
||||
|
||||
#define MAX_OPCODES 255
|
||||
#define MAX_OPCODES (256*256 - 1)
|
||||
|
||||
#ifndef OPCODE_T_STACK_DEFINED
|
||||
DEFINE_STACK_TYPE (opcode_counter_t, opcode_t)
|
||||
#define OPCODE_T_STACK_DEFINED
|
||||
#endif
|
||||
|
||||
/* bytecode_data contains identifiers, string and num literals.
|
||||
Memory map if the following.
|
||||
@@ -32,6 +39,11 @@
|
||||
U32 nums[nums_count];
|
||||
} */
|
||||
extern uint8_t *bytecode_data;
|
||||
opcode_t bytecode_opcodes[MAX_OPCODES];
|
||||
|
||||
#endif // BYTECODE_LINUX_H
|
||||
enum
|
||||
{
|
||||
bytecode_opcodes_global_size
|
||||
};
|
||||
STACK (opcode_t, bytecode_opcodes)
|
||||
|
||||
#endif // BYTECODE_DATA_H
|
||||
|
||||
@@ -85,7 +85,8 @@ deserialize_num_by_id (uint8_t id)
|
||||
const void *
|
||||
deserialize_bytecode (void)
|
||||
{
|
||||
return bytecode_opcodes;
|
||||
JERRY_ASSERT (STACK_SIZE (bytecode_opcodes) > 0);
|
||||
return bytecode_opcodes.data;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
|
||||
@@ -52,382 +52,6 @@ optimize_calls (opcode_t *opcodes)
|
||||
}
|
||||
}
|
||||
|
||||
/* Move NUMBER opcodes from FROM to TO and adjust opcodes beetwen FROM and TO. */
|
||||
void
|
||||
optimizer_move_opcodes (opcode_t *from, opcode_t *to, uint16_t number)
|
||||
{
|
||||
opcode_t temp[number], *current_opcode;
|
||||
uint16_t i;
|
||||
|
||||
if (to == from)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < number; i++)
|
||||
{
|
||||
temp[i] = from[i];
|
||||
}
|
||||
|
||||
if (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));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < number; i++)
|
||||
{
|
||||
to[i] = temp[i];
|
||||
}
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
opcode_to_counter (opcode_t *opcode)
|
||||
{
|
||||
JERRY_ASSERT (opcode > (opcode_t *) deserialize_bytecode ());
|
||||
return (uint16_t) (opcode - (opcode_t *) deserialize_bytecode ());
|
||||
}
|
||||
|
||||
void
|
||||
optimizer_adjust_jumps (opcode_t *first_opcode, opcode_t *last_opcode, int16_t value)
|
||||
{
|
||||
opcode_t *current_opcode;
|
||||
|
||||
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))
|
||||
{
|
||||
/* 19: is_true_jmp 2
|
||||
20: var_decl ...
|
||||
|
||||
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: is_true_jmp 20
|
||||
20: assignment
|
||||
21: var_decl
|
||||
|
||||
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 = (idx_t) (current_opcode->data.is_true_jmp.opcode + value);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 19: is_true_jmp 22
|
||||
20: assignment
|
||||
21: var_decl
|
||||
22: var_decl
|
||||
|
||||
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 = (idx_t) 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 = (idx_t) (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 = (idx_t) 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 = (idx_t) (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 = (idx_t) (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 = (idx_t) (current_opcode->data.jmp_up.opcode_count + value);
|
||||
continue;
|
||||
}
|
||||
|
||||
JERRY_UNREACHABLE ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Reorder scope like
|
||||
"use strict"
|
||||
func_decl
|
||||
var_decl
|
||||
other opcodes
|
||||
*/
|
||||
void
|
||||
optimizer_reorder_scope (uint16_t scope_start, uint16_t scope_end)
|
||||
{
|
||||
opcode_t *opcodes = (opcode_t *) deserialize_bytecode ();
|
||||
opcode_t *first_opcode = opcodes + scope_start;
|
||||
opcode_t *last_opcode = opcodes + scope_end;
|
||||
opcode_t *current_opcode, *processed_opcode = first_opcode;
|
||||
opcode_t *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)))
|
||||
{
|
||||
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))
|
||||
{
|
||||
opcode_t *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;
|
||||
}
|
||||
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 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_t *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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
optimizer_run_passes (opcode_t *opcodes)
|
||||
{
|
||||
|
||||
@@ -19,9 +19,6 @@
|
||||
#include "globals.h"
|
||||
#include "opcodes.h"
|
||||
|
||||
void optimizer_move_opcodes (opcode_t *, opcode_t *, uint16_t);
|
||||
void optimizer_adjust_jumps (opcode_t *, opcode_t *, int16_t);
|
||||
void optimizer_reorder_scope (uint16_t, uint16_t);
|
||||
void optimizer_run_passes (opcode_t *);
|
||||
|
||||
#endif // OPTIMIZER_PASSES_H
|
||||
|
||||
@@ -91,18 +91,20 @@ dump_variable (idx_t id)
|
||||
}
|
||||
}
|
||||
|
||||
#define CASE_CONDITIONAL_JUMP(op, string1, field1, string2, field2) \
|
||||
#define CASE_CONDITIONAL_JUMP(op, string1, field1, string2, oc, sign, field2, field3) \
|
||||
case NAME_TO_ID (op): \
|
||||
__printf (string1); \
|
||||
dump_variable (opcode.data.op.field1); \
|
||||
__printf (string2); \
|
||||
__printf (" %d;", opcode.data.op.field2); \
|
||||
__printf (" %d;", oc sign ((opcode.data.op.field2 << JERRY_BITSINBYTE) \
|
||||
+ opcode.data.op.field3)); \
|
||||
break;
|
||||
|
||||
#define CASE_UNCONDITIONAL_JUMP(op, string, oc, oper, field) \
|
||||
#define CASE_UNCONDITIONAL_JUMP(op, string, oc, sign, field2, field3) \
|
||||
case NAME_TO_ID (op): \
|
||||
__printf (string); \
|
||||
__printf (" %d;", oc oper opcode.data.op.field); \
|
||||
__printf (" %d;", oc sign ((opcode.data.op.field2 << JERRY_BITSINBYTE) \
|
||||
+ opcode.data.op.field3)); \
|
||||
break;
|
||||
|
||||
#define CASE_TRIPLE_ADDRESS(op, lhs, equals, op1, oper, op2) \
|
||||
@@ -366,12 +368,13 @@ pp_opcode (opcode_counter_t oc, opcode_t opcode, bool is_rewrite)
|
||||
|
||||
switch (opcode_num)
|
||||
{
|
||||
CASE_CONDITIONAL_JUMP (is_true_jmp, "if (", value, ") goto", opcode)
|
||||
CASE_CONDITIONAL_JUMP (is_false_jmp, "if (", value, " == false) goto", opcode)
|
||||
CASE_CONDITIONAL_JUMP (is_true_jmp_up, "if (", value, ") goto", oc, -, opcode_1, opcode_2)
|
||||
CASE_CONDITIONAL_JUMP (is_false_jmp_up, "if (", value, " == false) goto", oc, -, opcode_1, opcode_2)
|
||||
CASE_CONDITIONAL_JUMP (is_true_jmp_down, "if (", value, ") goto", oc, +, opcode_1, opcode_2)
|
||||
CASE_CONDITIONAL_JUMP (is_false_jmp_down, "if (", value, " == false) goto", oc, +, opcode_1, opcode_2)
|
||||
|
||||
CASE_UNCONDITIONAL_JUMP (jmp, "goto", 0, +, opcode_idx)
|
||||
CASE_UNCONDITIONAL_JUMP (jmp_up, "goto", oc, -, opcode_count)
|
||||
CASE_UNCONDITIONAL_JUMP (jmp_down, "goto", oc, +, opcode_count)
|
||||
CASE_UNCONDITIONAL_JUMP (jmp_up, "goto", oc, -, opcode_1, opcode_2)
|
||||
CASE_UNCONDITIONAL_JUMP (jmp_down, "goto", oc, +, opcode_1, opcode_2)
|
||||
|
||||
CASE_TRIPLE_ADDRESS (addition, dst, "=", var_left, "+", var_right)
|
||||
CASE_TRIPLE_ADDRESS (substraction, dst, "=", var_left, "-", var_right)
|
||||
|
||||
@@ -28,6 +28,7 @@ void
|
||||
serializer_init (bool show_opcodes)
|
||||
{
|
||||
print_opcodes = show_opcodes;
|
||||
INIT_STACK (opcode_t, bytecode_opcodes);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
@@ -111,25 +112,24 @@ serializer_dump_nums (const ecma_number_t nums[], uint8_t size, uint16_t offset,
|
||||
#endif
|
||||
}
|
||||
|
||||
static opcode_counter_t opcode_counter = 0;
|
||||
|
||||
void
|
||||
serializer_dump_opcode (opcode_t opcode)
|
||||
{
|
||||
JERRY_ASSERT (STACK_SIZE (bytecode_opcodes) < MAX_OPCODES);
|
||||
|
||||
if (print_opcodes)
|
||||
{
|
||||
pp_opcode (opcode_counter, opcode, false);
|
||||
pp_opcode (STACK_SIZE (bytecode_opcodes), opcode, false);
|
||||
}
|
||||
|
||||
JERRY_ASSERT (opcode_counter < MAX_OPCODES);
|
||||
bytecode_opcodes[opcode_counter++] = opcode;
|
||||
PUSH (bytecode_opcodes, opcode)
|
||||
}
|
||||
|
||||
void
|
||||
serializer_rewrite_opcode (const opcode_counter_t loc, opcode_t opcode)
|
||||
{
|
||||
JERRY_ASSERT (loc < MAX_OPCODES);
|
||||
bytecode_opcodes[loc] = opcode;
|
||||
JERRY_ASSERT (loc < STACK_SIZE (bytecode_opcodes));
|
||||
bytecode_opcodes.data[loc] = opcode;
|
||||
|
||||
if (print_opcodes)
|
||||
{
|
||||
@@ -149,9 +149,9 @@ serializer_print_opcodes (void)
|
||||
|
||||
__printf ("AFTER OPTIMIZER:\n");
|
||||
|
||||
for (loc = 0; (*(uint32_t *) (bytecode_opcodes + loc) != 0x0); loc++)
|
||||
for (loc = 0; loc < STACK_SIZE (bytecode_opcodes); loc++)
|
||||
{
|
||||
pp_opcode (loc, bytecode_opcodes[loc], false);
|
||||
pp_opcode (loc, bytecode_opcodes.data[loc], false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,4 +160,9 @@ serializer_free (void)
|
||||
{
|
||||
mem_heap_free_block (bytecode_data);
|
||||
bytecode_data = NULL;
|
||||
|
||||
if (bytecode_opcodes.data)
|
||||
{
|
||||
FREE_STACK (bytecode_opcodes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user