Skip to content

Commit

Permalink
Merge pull request jerryscript-project#24 from sand1k/statements_parse
Browse files Browse the repository at this point in the history
Preparations for enabling PREPARSE version of jsp_parse_source_element_list.
  • Loading branch information
ruben-ayrapetyan committed Dec 3, 2015
2 parents c1b63a7 + b497b77 commit b88bb13
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 46 deletions.
191 changes: 148 additions & 43 deletions jerry-core/parser/js/opcodes-dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ static vm_idx_t jsp_reg_max_for_args;

bool is_print_instrs = false;
scopes_tree current_scope_p = NULL;
/**
* Flag, indicating if bytecode should be generated
*/
bool is_generate_bytecode = false;

void dumper_dump_op_meta (op_meta);
void dumper_rewrite_op_meta (vm_instr_counter_t, op_meta);

void dumper_set_generate_bytecode (bool generate_bytecode)
{
is_generate_bytecode = generate_bytecode;
} /* dumper_set_generate_bytecode */

/**
* Allocate next register for intermediate value
*
Expand Down Expand Up @@ -112,33 +121,51 @@ dumper_get_current_instr_counter (void)
return scopes_tree_instrs_num (current_scope_p);
}

static op_meta
dumper_get_op_meta (vm_instr_counter_t pos)
{
op_meta opm;
if (is_generate_bytecode)
{
opm = scopes_tree_op_meta (current_scope_p, pos);
}

return opm;
}

void
dumper_dump_op_meta (op_meta op)
{
JERRY_ASSERT (scopes_tree_instrs_num (current_scope_p) < MAX_OPCODES);

scopes_tree_add_op_meta (current_scope_p, op);
if (is_generate_bytecode)
{
scopes_tree_add_op_meta (current_scope_p, op);

#ifdef JERRY_ENABLE_PRETTY_PRINTER
if (is_print_instrs)
{
pp_op_meta (NULL, (vm_instr_counter_t) (scopes_tree_instrs_num (current_scope_p) - 1), op, false);
}
if (is_print_instrs)
{
pp_op_meta (NULL, (vm_instr_counter_t) (scopes_tree_instrs_num (current_scope_p) - 1), op, false);
}
#endif
}
} /* dumper_dump_op_meta */

void
dumper_rewrite_op_meta (const vm_instr_counter_t loc,
op_meta op)
{
scopes_tree_set_op_meta (current_scope_p, loc, op);
if (is_generate_bytecode)
{
scopes_tree_set_op_meta (current_scope_p, loc, op);

#ifdef JERRY_ENABLE_PRETTY_PRINTER
if (is_print_instrs)
{
pp_op_meta (NULL, loc, op, true);
}
if (is_print_instrs)
{
pp_op_meta (NULL, loc, op, true);
}
#endif
}
} /* dumper_rewrite_op_meta */

#ifdef CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER
Expand Down Expand Up @@ -807,6 +834,87 @@ dump_varg_header_for_rewrite (varg_list_type vlt, jsp_operand_t obj)
return pos;
}

typedef enum
{
REWRITE_VARG_HEADER,
REWRITE_FUNCTION_END,
REWRITE_CONDITIONAL_CHECK,
REWRITE_JUMP_TO_END,
REWRITE_SIMPLE_OR_NESTED_JUMP,
REWRITE_CASE_CLAUSE,
REWRITE_DEFAULT_CLAUSE,
REWRITE_WITH,
REWRITE_FOR_IN,
REWRITE_TRY,
REWRITE_CATCH,
REWRITE_FINALLY,
REWRITE_SCOPE_CODE_FLAGS,
REWRITE_REG_VAR_DECL,
} rewrite_type_t;

static void
dumper_assert_op_fields (rewrite_type_t rewrite_type,
op_meta meta)
{
if (!is_generate_bytecode)
{
return;
}

if (rewrite_type == REWRITE_FUNCTION_END)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_META);
JERRY_ASSERT (meta.op.data.meta.type == OPCODE_META_TYPE_FUNCTION_END);
JERRY_ASSERT (meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
JERRY_ASSERT (meta.op.data.meta.data_2 == VM_IDX_REWRITE_GENERAL_CASE);
}
else if (rewrite_type == REWRITE_CONDITIONAL_CHECK)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_IS_FALSE_JMP_DOWN);
}
else if (rewrite_type == REWRITE_JUMP_TO_END)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_JMP_DOWN);
}
else if (rewrite_type == REWRITE_CASE_CLAUSE)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_IS_TRUE_JMP_DOWN);
}
else if (rewrite_type == REWRITE_DEFAULT_CLAUSE)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_JMP_DOWN);
}
else if (rewrite_type == REWRITE_TRY)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_TRY_BLOCK);
}
else if (rewrite_type == REWRITE_CATCH)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_META
&& meta.op.data.meta.type == OPCODE_META_TYPE_CATCH);
}
else if (rewrite_type == REWRITE_FINALLY)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_META
&& meta.op.data.meta.type == OPCODE_META_TYPE_FINALLY);
}
else if (rewrite_type == REWRITE_SCOPE_CODE_FLAGS)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_META);
JERRY_ASSERT (meta.op.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS);
JERRY_ASSERT (meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
JERRY_ASSERT (meta.op.data.meta.data_2 == VM_IDX_EMPTY);
}
else if (rewrite_type == REWRITE_REG_VAR_DECL)
{
JERRY_ASSERT (meta.op.op_idx == VM_OP_REG_VAR_DECL);
}
else
{
JERRY_UNREACHABLE ();
}
} /* dumper_assert_op_fields */

void
rewrite_varg_header_set_args_count (jsp_operand_t ret,
size_t args_count,
Expand All @@ -820,7 +928,12 @@ rewrite_varg_header_set_args_count (jsp_operand_t ret,
* argument / formal parameter name to values collection.
*/

op_meta om = scopes_tree_op_meta (current_scope_p, pos);
if (!is_generate_bytecode)
{
return;
}

op_meta om = dumper_get_op_meta (pos);

switch (om.op.op_idx)
{
Expand Down Expand Up @@ -990,11 +1103,8 @@ rewrite_function_end (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (oc, &id1, &id2);

op_meta function_end_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (function_end_op_meta.op.op_idx == VM_OP_META);
JERRY_ASSERT (function_end_op_meta.op.data.meta.type == OPCODE_META_TYPE_FUNCTION_END);
JERRY_ASSERT (function_end_op_meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
JERRY_ASSERT (function_end_op_meta.op.data.meta.data_2 == VM_IDX_REWRITE_GENERAL_CASE);
op_meta function_end_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_FUNCTION_END, function_end_op_meta);

function_end_op_meta.op.data.meta.data_1 = id1;
function_end_op_meta.op.data.meta.data_2 = id2;
Expand Down Expand Up @@ -1220,8 +1330,8 @@ rewrite_conditional_check (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (pos), &id1, &id2);

op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_IS_FALSE_JMP_DOWN);
op_meta jmp_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_CONDITIONAL_CHECK, jmp_op_meta);

jmp_op_meta.op.data.is_false_jmp_down.oc_idx_1 = id1;
jmp_op_meta.op.data.is_false_jmp_down.oc_idx_2 = id2;
Expand All @@ -1247,8 +1357,8 @@ rewrite_jump_to_end (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (pos), &id1, &id2);

op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_JMP_DOWN);
op_meta jmp_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_JUMP_TO_END, jmp_op_meta);

jmp_op_meta.op.data.jmp_down.oc_idx_1 = id1;
jmp_op_meta.op.data.jmp_down.oc_idx_2 = id2;
Expand Down Expand Up @@ -1342,7 +1452,7 @@ vm_instr_counter_t
rewrite_simple_or_nested_jump_and_get_next (vm_instr_counter_t jump_oc, /**< position of jump to rewrite */
vm_instr_counter_t target_oc) /**< the jump's target */
{
op_meta jump_op_meta = scopes_tree_op_meta (current_scope_p, jump_oc);
op_meta jump_op_meta = dumper_get_op_meta (jump_oc);

vm_op_t jmp_opcode = (vm_op_t) jump_op_meta.op.op_idx;

Expand Down Expand Up @@ -1399,7 +1509,7 @@ rewrite_simple_or_nested_jump_and_get_next (vm_instr_counter_t jump_oc, /**< pos
}
else
{
JERRY_ASSERT (jmp_opcode == VM_OP_JMP_BREAK_CONTINUE);
JERRY_ASSERT (!is_generate_bytecode || (jmp_opcode == VM_OP_JMP_BREAK_CONTINUE));

id1_prev = jump_op_meta.op.data.jmp_break_continue.oc_idx_1;
id2_prev = jump_op_meta.op.data.jmp_break_continue.oc_idx_2;
Expand Down Expand Up @@ -1449,8 +1559,8 @@ rewrite_case_clause (vm_instr_counter_t jmp_oc)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (jmp_oc), &id1, &id2);

op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, jmp_oc);
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_IS_TRUE_JMP_DOWN);
op_meta jmp_op_meta = dumper_get_op_meta (jmp_oc);
dumper_assert_op_fields (REWRITE_CASE_CLAUSE, jmp_op_meta);

jmp_op_meta.op.data.is_true_jmp_down.oc_idx_1 = id1;
jmp_op_meta.op.data.is_true_jmp_down.oc_idx_2 = id2;
Expand All @@ -1464,8 +1574,8 @@ rewrite_default_clause (vm_instr_counter_t jmp_oc)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (jmp_oc), &id1, &id2);

op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, jmp_oc);
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_JMP_DOWN);
op_meta jmp_op_meta = dumper_get_op_meta (jmp_oc);
dumper_assert_op_fields (REWRITE_DEFAULT_CLAUSE, jmp_op_meta);

jmp_op_meta.op.data.jmp_down.oc_idx_1 = id1;
jmp_op_meta.op.data.jmp_down.oc_idx_2 = id2;
Expand Down Expand Up @@ -1510,7 +1620,7 @@ rewrite_with (vm_instr_counter_t oc) /**< instr counter of the instruction templ
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (oc), &id1, &id2);

op_meta with_op_meta = scopes_tree_op_meta (current_scope_p, oc);
op_meta with_op_meta = dumper_get_op_meta (oc);

with_op_meta.op.data.with.oc_idx_1 = id1;
with_op_meta.op.data.with.oc_idx_2 = id2;
Expand Down Expand Up @@ -1562,7 +1672,7 @@ rewrite_for_in (vm_instr_counter_t oc) /**< instr counter of the instruction tem
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (oc), &id1, &id2);

op_meta for_in_op_meta = scopes_tree_op_meta (current_scope_p, oc);
op_meta for_in_op_meta = dumper_get_op_meta (oc);

for_in_op_meta.op.data.for_in.oc_idx_1 = id1;
for_in_op_meta.op.data.for_in.oc_idx_2 = id2;
Expand Down Expand Up @@ -1600,8 +1710,8 @@ rewrite_try (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (pos), &id1, &id2);

op_meta try_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (try_op_meta.op.op_idx == VM_OP_TRY_BLOCK);
op_meta try_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_TRY, try_op_meta);

try_op_meta.op.data.try_block.oc_idx_1 = id1;
try_op_meta.op.data.try_block.oc_idx_2 = id2;
Expand Down Expand Up @@ -1635,9 +1745,8 @@ rewrite_catch (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (pos), &id1, &id2);

op_meta catch_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (catch_op_meta.op.op_idx == VM_OP_META
&& catch_op_meta.op.data.meta.type == OPCODE_META_TYPE_CATCH);
op_meta catch_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_CATCH, catch_op_meta);

catch_op_meta.op.data.meta.data_1 = id1;
catch_op_meta.op.data.meta.data_2 = id2;
Expand All @@ -1664,9 +1773,8 @@ rewrite_finally (vm_instr_counter_t pos)
vm_idx_t id1, id2;
split_instr_counter (get_diff_from (pos), &id1, &id2);

op_meta finally_op_meta = scopes_tree_op_meta (current_scope_p, pos);
JERRY_ASSERT (finally_op_meta.op.op_idx == VM_OP_META
&& finally_op_meta.op.data.meta.type == OPCODE_META_TYPE_FINALLY);
op_meta finally_op_meta = dumper_get_op_meta (pos);
dumper_assert_op_fields (REWRITE_FINALLY, finally_op_meta);

finally_op_meta.op.data.meta.data_1 = id1;
finally_op_meta.op.data.meta.data_2 = id2;
Expand Down Expand Up @@ -1735,11 +1843,8 @@ rewrite_scope_code_flags (vm_instr_counter_t scope_code_flags_oc, /**< position
{
JERRY_ASSERT ((vm_idx_t) scope_flags == scope_flags);

op_meta opm = scopes_tree_op_meta (current_scope_p, scope_code_flags_oc);
JERRY_ASSERT (opm.op.op_idx == VM_OP_META);
JERRY_ASSERT (opm.op.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS);
JERRY_ASSERT (opm.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
JERRY_ASSERT (opm.op.data.meta.data_2 == VM_IDX_EMPTY);
op_meta opm = dumper_get_op_meta (scope_code_flags_oc);
dumper_assert_op_fields (REWRITE_SCOPE_CODE_FLAGS, opm);

opm.op.data.meta.data_1 = (vm_idx_t) scope_flags;
dumper_rewrite_op_meta (scope_code_flags_oc, opm);
Expand Down Expand Up @@ -1775,8 +1880,8 @@ dump_reg_var_decl_for_rewrite (void)
void
rewrite_reg_var_decl (vm_instr_counter_t reg_var_decl_oc) /**< position of dumped 'reg_var_decl' template */
{
op_meta opm = scopes_tree_op_meta (current_scope_p, reg_var_decl_oc);
JERRY_ASSERT (opm.op.op_idx == VM_OP_REG_VAR_DECL);
op_meta opm = dumper_get_op_meta (reg_var_decl_oc);
dumper_assert_op_fields (REWRITE_REG_VAR_DECL, opm);

opm.op.data.reg_var_decl.tmp_regs_num = (vm_idx_t) (jsp_reg_max_for_temps - VM_REG_GENERAL_FIRST + 1);

Expand Down
1 change: 1 addition & 0 deletions jerry-core/parser/js/opcodes-dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ bool operand_is_empty (jsp_operand_t);

void dumper_init (bool);
void dumper_free (void);
void dumper_set_generate_bytecode (bool);

scopes_tree dumper_get_scope (void);
void dumper_set_scope (scopes_tree scope_p);
Expand Down
15 changes: 12 additions & 3 deletions jerry-core/parser/js/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ typedef enum __attr_packed___
JSP_STATE_STAT_NAMED_LABEL
} jsp_state_expr_t;

static void jsp_parse_source_element_list (void);

typedef enum
{
PREPARSE,
DUMP
} jsp_parse_mode_t;

static void jsp_parse_source_element_list (jsp_parse_mode_t);
static void skip_case_clause_body (void);

static bool
Expand Down Expand Up @@ -1487,9 +1494,10 @@ while (0)
* Parse source element list
*/
static void __attr_noinline___
jsp_parse_source_element_list (void)
jsp_parse_source_element_list (jsp_parse_mode_t parse_mode)
{
jsp_stack_init ();
dumper_set_generate_bytecode (parse_mode == DUMP);

jsp_start_statement_parse (JSP_STATE_SOURCE_ELEMENTS_INIT);
jsp_state_top ()->req_state = JSP_STATE_SOURCE_ELEMENTS;
Expand Down Expand Up @@ -4575,7 +4583,8 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer

jsp_parse_directive_prologue ();

jsp_parse_source_element_list ();
// jsp_parse_source_element_list (PREPARSE);
jsp_parse_source_element_list (DUMP);

JERRY_ASSERT (token_is (TOK_EOF));

Expand Down

0 comments on commit b88bb13

Please sign in to comment.