Skip to content

Commit b88bb13

Browse files
Merge pull request jerryscript-project#24 from sand1k/statements_parse
Preparations for enabling PREPARSE version of jsp_parse_source_element_list.
2 parents c1b63a7 + b497b77 commit b88bb13

File tree

3 files changed

+161
-46
lines changed

3 files changed

+161
-46
lines changed

jerry-core/parser/js/opcodes-dumper.cpp

Lines changed: 148 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,19 @@ static vm_idx_t jsp_reg_max_for_args;
6060

6161
bool is_print_instrs = false;
6262
scopes_tree current_scope_p = NULL;
63+
/**
64+
* Flag, indicating if bytecode should be generated
65+
*/
66+
bool is_generate_bytecode = false;
6367

6468
void dumper_dump_op_meta (op_meta);
6569
void dumper_rewrite_op_meta (vm_instr_counter_t, op_meta);
6670

71+
void dumper_set_generate_bytecode (bool generate_bytecode)
72+
{
73+
is_generate_bytecode = generate_bytecode;
74+
} /* dumper_set_generate_bytecode */
75+
6776
/**
6877
* Allocate next register for intermediate value
6978
*
@@ -112,33 +121,51 @@ dumper_get_current_instr_counter (void)
112121
return scopes_tree_instrs_num (current_scope_p);
113122
}
114123

124+
static op_meta
125+
dumper_get_op_meta (vm_instr_counter_t pos)
126+
{
127+
op_meta opm;
128+
if (is_generate_bytecode)
129+
{
130+
opm = scopes_tree_op_meta (current_scope_p, pos);
131+
}
132+
133+
return opm;
134+
}
135+
115136
void
116137
dumper_dump_op_meta (op_meta op)
117138
{
118139
JERRY_ASSERT (scopes_tree_instrs_num (current_scope_p) < MAX_OPCODES);
119140

120-
scopes_tree_add_op_meta (current_scope_p, op);
141+
if (is_generate_bytecode)
142+
{
143+
scopes_tree_add_op_meta (current_scope_p, op);
121144

122145
#ifdef JERRY_ENABLE_PRETTY_PRINTER
123-
if (is_print_instrs)
124-
{
125-
pp_op_meta (NULL, (vm_instr_counter_t) (scopes_tree_instrs_num (current_scope_p) - 1), op, false);
126-
}
146+
if (is_print_instrs)
147+
{
148+
pp_op_meta (NULL, (vm_instr_counter_t) (scopes_tree_instrs_num (current_scope_p) - 1), op, false);
149+
}
127150
#endif
151+
}
128152
} /* dumper_dump_op_meta */
129153

130154
void
131155
dumper_rewrite_op_meta (const vm_instr_counter_t loc,
132156
op_meta op)
133157
{
134-
scopes_tree_set_op_meta (current_scope_p, loc, op);
158+
if (is_generate_bytecode)
159+
{
160+
scopes_tree_set_op_meta (current_scope_p, loc, op);
135161

136162
#ifdef JERRY_ENABLE_PRETTY_PRINTER
137-
if (is_print_instrs)
138-
{
139-
pp_op_meta (NULL, loc, op, true);
140-
}
163+
if (is_print_instrs)
164+
{
165+
pp_op_meta (NULL, loc, op, true);
166+
}
141167
#endif
168+
}
142169
} /* dumper_rewrite_op_meta */
143170

144171
#ifdef CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER
@@ -807,6 +834,87 @@ dump_varg_header_for_rewrite (varg_list_type vlt, jsp_operand_t obj)
807834
return pos;
808835
}
809836

837+
typedef enum
838+
{
839+
REWRITE_VARG_HEADER,
840+
REWRITE_FUNCTION_END,
841+
REWRITE_CONDITIONAL_CHECK,
842+
REWRITE_JUMP_TO_END,
843+
REWRITE_SIMPLE_OR_NESTED_JUMP,
844+
REWRITE_CASE_CLAUSE,
845+
REWRITE_DEFAULT_CLAUSE,
846+
REWRITE_WITH,
847+
REWRITE_FOR_IN,
848+
REWRITE_TRY,
849+
REWRITE_CATCH,
850+
REWRITE_FINALLY,
851+
REWRITE_SCOPE_CODE_FLAGS,
852+
REWRITE_REG_VAR_DECL,
853+
} rewrite_type_t;
854+
855+
static void
856+
dumper_assert_op_fields (rewrite_type_t rewrite_type,
857+
op_meta meta)
858+
{
859+
if (!is_generate_bytecode)
860+
{
861+
return;
862+
}
863+
864+
if (rewrite_type == REWRITE_FUNCTION_END)
865+
{
866+
JERRY_ASSERT (meta.op.op_idx == VM_OP_META);
867+
JERRY_ASSERT (meta.op.data.meta.type == OPCODE_META_TYPE_FUNCTION_END);
868+
JERRY_ASSERT (meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
869+
JERRY_ASSERT (meta.op.data.meta.data_2 == VM_IDX_REWRITE_GENERAL_CASE);
870+
}
871+
else if (rewrite_type == REWRITE_CONDITIONAL_CHECK)
872+
{
873+
JERRY_ASSERT (meta.op.op_idx == VM_OP_IS_FALSE_JMP_DOWN);
874+
}
875+
else if (rewrite_type == REWRITE_JUMP_TO_END)
876+
{
877+
JERRY_ASSERT (meta.op.op_idx == VM_OP_JMP_DOWN);
878+
}
879+
else if (rewrite_type == REWRITE_CASE_CLAUSE)
880+
{
881+
JERRY_ASSERT (meta.op.op_idx == VM_OP_IS_TRUE_JMP_DOWN);
882+
}
883+
else if (rewrite_type == REWRITE_DEFAULT_CLAUSE)
884+
{
885+
JERRY_ASSERT (meta.op.op_idx == VM_OP_JMP_DOWN);
886+
}
887+
else if (rewrite_type == REWRITE_TRY)
888+
{
889+
JERRY_ASSERT (meta.op.op_idx == VM_OP_TRY_BLOCK);
890+
}
891+
else if (rewrite_type == REWRITE_CATCH)
892+
{
893+
JERRY_ASSERT (meta.op.op_idx == VM_OP_META
894+
&& meta.op.data.meta.type == OPCODE_META_TYPE_CATCH);
895+
}
896+
else if (rewrite_type == REWRITE_FINALLY)
897+
{
898+
JERRY_ASSERT (meta.op.op_idx == VM_OP_META
899+
&& meta.op.data.meta.type == OPCODE_META_TYPE_FINALLY);
900+
}
901+
else if (rewrite_type == REWRITE_SCOPE_CODE_FLAGS)
902+
{
903+
JERRY_ASSERT (meta.op.op_idx == VM_OP_META);
904+
JERRY_ASSERT (meta.op.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS);
905+
JERRY_ASSERT (meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
906+
JERRY_ASSERT (meta.op.data.meta.data_2 == VM_IDX_EMPTY);
907+
}
908+
else if (rewrite_type == REWRITE_REG_VAR_DECL)
909+
{
910+
JERRY_ASSERT (meta.op.op_idx == VM_OP_REG_VAR_DECL);
911+
}
912+
else
913+
{
914+
JERRY_UNREACHABLE ();
915+
}
916+
} /* dumper_assert_op_fields */
917+
810918
void
811919
rewrite_varg_header_set_args_count (jsp_operand_t ret,
812920
size_t args_count,
@@ -820,7 +928,12 @@ rewrite_varg_header_set_args_count (jsp_operand_t ret,
820928
* argument / formal parameter name to values collection.
821929
*/
822930

823-
op_meta om = scopes_tree_op_meta (current_scope_p, pos);
931+
if (!is_generate_bytecode)
932+
{
933+
return;
934+
}
935+
936+
op_meta om = dumper_get_op_meta (pos);
824937

825938
switch (om.op.op_idx)
826939
{
@@ -990,11 +1103,8 @@ rewrite_function_end (vm_instr_counter_t pos)
9901103
vm_idx_t id1, id2;
9911104
split_instr_counter (oc, &id1, &id2);
9921105

993-
op_meta function_end_op_meta = scopes_tree_op_meta (current_scope_p, pos);
994-
JERRY_ASSERT (function_end_op_meta.op.op_idx == VM_OP_META);
995-
JERRY_ASSERT (function_end_op_meta.op.data.meta.type == OPCODE_META_TYPE_FUNCTION_END);
996-
JERRY_ASSERT (function_end_op_meta.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
997-
JERRY_ASSERT (function_end_op_meta.op.data.meta.data_2 == VM_IDX_REWRITE_GENERAL_CASE);
1106+
op_meta function_end_op_meta = dumper_get_op_meta (pos);
1107+
dumper_assert_op_fields (REWRITE_FUNCTION_END, function_end_op_meta);
9981108

9991109
function_end_op_meta.op.data.meta.data_1 = id1;
10001110
function_end_op_meta.op.data.meta.data_2 = id2;
@@ -1220,8 +1330,8 @@ rewrite_conditional_check (vm_instr_counter_t pos)
12201330
vm_idx_t id1, id2;
12211331
split_instr_counter (get_diff_from (pos), &id1, &id2);
12221332

1223-
op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, pos);
1224-
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_IS_FALSE_JMP_DOWN);
1333+
op_meta jmp_op_meta = dumper_get_op_meta (pos);
1334+
dumper_assert_op_fields (REWRITE_CONDITIONAL_CHECK, jmp_op_meta);
12251335

12261336
jmp_op_meta.op.data.is_false_jmp_down.oc_idx_1 = id1;
12271337
jmp_op_meta.op.data.is_false_jmp_down.oc_idx_2 = id2;
@@ -1247,8 +1357,8 @@ rewrite_jump_to_end (vm_instr_counter_t pos)
12471357
vm_idx_t id1, id2;
12481358
split_instr_counter (get_diff_from (pos), &id1, &id2);
12491359

1250-
op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, pos);
1251-
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_JMP_DOWN);
1360+
op_meta jmp_op_meta = dumper_get_op_meta (pos);
1361+
dumper_assert_op_fields (REWRITE_JUMP_TO_END, jmp_op_meta);
12521362

12531363
jmp_op_meta.op.data.jmp_down.oc_idx_1 = id1;
12541364
jmp_op_meta.op.data.jmp_down.oc_idx_2 = id2;
@@ -1342,7 +1452,7 @@ vm_instr_counter_t
13421452
rewrite_simple_or_nested_jump_and_get_next (vm_instr_counter_t jump_oc, /**< position of jump to rewrite */
13431453
vm_instr_counter_t target_oc) /**< the jump's target */
13441454
{
1345-
op_meta jump_op_meta = scopes_tree_op_meta (current_scope_p, jump_oc);
1455+
op_meta jump_op_meta = dumper_get_op_meta (jump_oc);
13461456

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

@@ -1399,7 +1509,7 @@ rewrite_simple_or_nested_jump_and_get_next (vm_instr_counter_t jump_oc, /**< pos
13991509
}
14001510
else
14011511
{
1402-
JERRY_ASSERT (jmp_opcode == VM_OP_JMP_BREAK_CONTINUE);
1512+
JERRY_ASSERT (!is_generate_bytecode || (jmp_opcode == VM_OP_JMP_BREAK_CONTINUE));
14031513

14041514
id1_prev = jump_op_meta.op.data.jmp_break_continue.oc_idx_1;
14051515
id2_prev = jump_op_meta.op.data.jmp_break_continue.oc_idx_2;
@@ -1449,8 +1559,8 @@ rewrite_case_clause (vm_instr_counter_t jmp_oc)
14491559
vm_idx_t id1, id2;
14501560
split_instr_counter (get_diff_from (jmp_oc), &id1, &id2);
14511561

1452-
op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, jmp_oc);
1453-
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_IS_TRUE_JMP_DOWN);
1562+
op_meta jmp_op_meta = dumper_get_op_meta (jmp_oc);
1563+
dumper_assert_op_fields (REWRITE_CASE_CLAUSE, jmp_op_meta);
14541564

14551565
jmp_op_meta.op.data.is_true_jmp_down.oc_idx_1 = id1;
14561566
jmp_op_meta.op.data.is_true_jmp_down.oc_idx_2 = id2;
@@ -1464,8 +1574,8 @@ rewrite_default_clause (vm_instr_counter_t jmp_oc)
14641574
vm_idx_t id1, id2;
14651575
split_instr_counter (get_diff_from (jmp_oc), &id1, &id2);
14661576

1467-
op_meta jmp_op_meta = scopes_tree_op_meta (current_scope_p, jmp_oc);
1468-
JERRY_ASSERT (jmp_op_meta.op.op_idx == VM_OP_JMP_DOWN);
1577+
op_meta jmp_op_meta = dumper_get_op_meta (jmp_oc);
1578+
dumper_assert_op_fields (REWRITE_DEFAULT_CLAUSE, jmp_op_meta);
14691579

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

1513-
op_meta with_op_meta = scopes_tree_op_meta (current_scope_p, oc);
1623+
op_meta with_op_meta = dumper_get_op_meta (oc);
15141624

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

1565-
op_meta for_in_op_meta = scopes_tree_op_meta (current_scope_p, oc);
1675+
op_meta for_in_op_meta = dumper_get_op_meta (oc);
15661676

15671677
for_in_op_meta.op.data.for_in.oc_idx_1 = id1;
15681678
for_in_op_meta.op.data.for_in.oc_idx_2 = id2;
@@ -1600,8 +1710,8 @@ rewrite_try (vm_instr_counter_t pos)
16001710
vm_idx_t id1, id2;
16011711
split_instr_counter (get_diff_from (pos), &id1, &id2);
16021712

1603-
op_meta try_op_meta = scopes_tree_op_meta (current_scope_p, pos);
1604-
JERRY_ASSERT (try_op_meta.op.op_idx == VM_OP_TRY_BLOCK);
1713+
op_meta try_op_meta = dumper_get_op_meta (pos);
1714+
dumper_assert_op_fields (REWRITE_TRY, try_op_meta);
16051715

16061716
try_op_meta.op.data.try_block.oc_idx_1 = id1;
16071717
try_op_meta.op.data.try_block.oc_idx_2 = id2;
@@ -1635,9 +1745,8 @@ rewrite_catch (vm_instr_counter_t pos)
16351745
vm_idx_t id1, id2;
16361746
split_instr_counter (get_diff_from (pos), &id1, &id2);
16371747

1638-
op_meta catch_op_meta = scopes_tree_op_meta (current_scope_p, pos);
1639-
JERRY_ASSERT (catch_op_meta.op.op_idx == VM_OP_META
1640-
&& catch_op_meta.op.data.meta.type == OPCODE_META_TYPE_CATCH);
1748+
op_meta catch_op_meta = dumper_get_op_meta (pos);
1749+
dumper_assert_op_fields (REWRITE_CATCH, catch_op_meta);
16411750

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

1667-
op_meta finally_op_meta = scopes_tree_op_meta (current_scope_p, pos);
1668-
JERRY_ASSERT (finally_op_meta.op.op_idx == VM_OP_META
1669-
&& finally_op_meta.op.data.meta.type == OPCODE_META_TYPE_FINALLY);
1776+
op_meta finally_op_meta = dumper_get_op_meta (pos);
1777+
dumper_assert_op_fields (REWRITE_FINALLY, finally_op_meta);
16701778

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

1738-
op_meta opm = scopes_tree_op_meta (current_scope_p, scope_code_flags_oc);
1739-
JERRY_ASSERT (opm.op.op_idx == VM_OP_META);
1740-
JERRY_ASSERT (opm.op.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS);
1741-
JERRY_ASSERT (opm.op.data.meta.data_1 == VM_IDX_REWRITE_GENERAL_CASE);
1742-
JERRY_ASSERT (opm.op.data.meta.data_2 == VM_IDX_EMPTY);
1846+
op_meta opm = dumper_get_op_meta (scope_code_flags_oc);
1847+
dumper_assert_op_fields (REWRITE_SCOPE_CODE_FLAGS, opm);
17431848

17441849
opm.op.data.meta.data_1 = (vm_idx_t) scope_flags;
17451850
dumper_rewrite_op_meta (scope_code_flags_oc, opm);
@@ -1775,8 +1880,8 @@ dump_reg_var_decl_for_rewrite (void)
17751880
void
17761881
rewrite_reg_var_decl (vm_instr_counter_t reg_var_decl_oc) /**< position of dumped 'reg_var_decl' template */
17771882
{
1778-
op_meta opm = scopes_tree_op_meta (current_scope_p, reg_var_decl_oc);
1779-
JERRY_ASSERT (opm.op.op_idx == VM_OP_REG_VAR_DECL);
1883+
op_meta opm = dumper_get_op_meta (reg_var_decl_oc);
1884+
dumper_assert_op_fields (REWRITE_REG_VAR_DECL, opm);
17801885

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

jerry-core/parser/js/opcodes-dumper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ bool operand_is_empty (jsp_operand_t);
349349

350350
void dumper_init (bool);
351351
void dumper_free (void);
352+
void dumper_set_generate_bytecode (bool);
352353

353354
scopes_tree dumper_get_scope (void);
354355
void dumper_set_scope (scopes_tree scope_p);

jerry-core/parser/js/parser.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@ typedef enum __attr_packed___
125125
JSP_STATE_STAT_NAMED_LABEL
126126
} jsp_state_expr_t;
127127

128-
static void jsp_parse_source_element_list (void);
128+
129+
typedef enum
130+
{
131+
PREPARSE,
132+
DUMP
133+
} jsp_parse_mode_t;
134+
135+
static void jsp_parse_source_element_list (jsp_parse_mode_t);
129136
static void skip_case_clause_body (void);
130137

131138
static bool
@@ -1487,9 +1494,10 @@ while (0)
14871494
* Parse source element list
14881495
*/
14891496
static void __attr_noinline___
1490-
jsp_parse_source_element_list (void)
1497+
jsp_parse_source_element_list (jsp_parse_mode_t parse_mode)
14911498
{
14921499
jsp_stack_init ();
1500+
dumper_set_generate_bytecode (parse_mode == DUMP);
14931501

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

45764584
jsp_parse_directive_prologue ();
45774585

4578-
jsp_parse_source_element_list ();
4586+
// jsp_parse_source_element_list (PREPARSE);
4587+
jsp_parse_source_element_list (DUMP);
45794588

45804589
JERRY_ASSERT (token_is (TOK_EOF));
45814590

0 commit comments

Comments
 (0)