Introduce parser-time optimization that moves function's argument values to registers.
For functions with the optimization applied: - vm puts arguments values immediately to registers without putting them to variables in lexical environment; - number of arguments is extracted from corresponding 'reg_var_decl' instruction's argument; - for functions that also don't have local variables, lexical environments are not created. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
This commit is contained in:
@@ -42,10 +42,24 @@ static vm_idx_t jsp_reg_max_for_temps;
|
||||
* so the value, if not equal to VM_IDX_EMPTY, is always greater than jsp_reg_max_for_temps.
|
||||
*
|
||||
* See also:
|
||||
* dumper_try_replace_var_with_reg
|
||||
* dumper_try_replace_identifier_name_with_reg
|
||||
*/
|
||||
static vm_idx_t jsp_reg_max_for_local_var;
|
||||
|
||||
/**
|
||||
* Maximum identifier of a register, allocated for storage of an argument value.
|
||||
*
|
||||
* The value can be VM_IDX_EMPTY, indicating that no registers were allocated for argument values.
|
||||
*
|
||||
* Note:
|
||||
* Registers for argument values are always allocated after registers for variable values,
|
||||
* so the value, if not equal to VM_IDX_EMPTY, is always greater than jsp_reg_max_for_local_var.
|
||||
*
|
||||
* See also:
|
||||
* dumper_try_replace_identifier_name_with_reg
|
||||
*/
|
||||
static vm_idx_t jsp_reg_max_for_args;
|
||||
|
||||
enum
|
||||
{
|
||||
U8_global_size
|
||||
@@ -130,12 +144,6 @@ enum
|
||||
};
|
||||
STATIC_STACK (jsp_reg_id_stack, vm_idx_t)
|
||||
|
||||
enum
|
||||
{
|
||||
reg_var_decls_global_size
|
||||
};
|
||||
STATIC_STACK (reg_var_decls, vm_instr_counter_t)
|
||||
|
||||
/**
|
||||
* Allocate next register for intermediate value
|
||||
*
|
||||
@@ -145,6 +153,7 @@ static vm_idx_t
|
||||
jsp_alloc_reg_for_temp (void)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var == VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args == VM_IDX_EMPTY);
|
||||
|
||||
vm_idx_t next_reg = jsp_reg_next++;
|
||||
|
||||
@@ -166,6 +175,51 @@ jsp_alloc_reg_for_temp (void)
|
||||
} /* jsp_alloc_reg_for_temp */
|
||||
|
||||
#ifdef CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER
|
||||
/**
|
||||
* Start move of variable values to registers optimization pass
|
||||
*/
|
||||
void
|
||||
dumper_start_move_of_vars_to_regs ()
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var == VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args == VM_IDX_EMPTY);
|
||||
|
||||
jsp_reg_max_for_local_var = jsp_reg_max_for_temps;
|
||||
} /* dumper_start_move_of_vars_to_regs */
|
||||
|
||||
/**
|
||||
* Start move of argument values to registers optimization pass
|
||||
*
|
||||
* @return true - if optimization can be performed successfully (i.e. there are enough free registers),
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
dumper_start_move_of_args_to_regs (uint32_t args_num) /**< number of arguments */
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_args == VM_IDX_EMPTY);
|
||||
|
||||
if (jsp_reg_max_for_local_var == VM_IDX_EMPTY)
|
||||
{
|
||||
if (args_num + jsp_reg_max_for_temps >= VM_REG_GENERAL_LAST)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
jsp_reg_max_for_args = jsp_reg_max_for_temps;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args_num + jsp_reg_max_for_local_var >= VM_REG_GENERAL_LAST)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
jsp_reg_max_for_args = jsp_reg_max_for_local_var;
|
||||
}
|
||||
|
||||
return true;
|
||||
} /* dumper_start_move_of_args_to_regs */
|
||||
|
||||
/**
|
||||
* Try to move local variable to a register
|
||||
*
|
||||
@@ -183,72 +237,63 @@ jsp_alloc_reg_for_temp (void)
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
dumper_try_replace_var_with_reg (scopes_tree tree, /**< a function scope, created for
|
||||
* function declaration or function expresssion */
|
||||
op_meta *var_decl_om_p) /**< operation meta of corresponding variable declaration */
|
||||
dumper_try_replace_identifier_name_with_reg (scopes_tree tree, /**< a function scope, created for
|
||||
* function declaration or function expresssion */
|
||||
op_meta *om_p) /**< operation meta of corresponding
|
||||
* variable declaration */
|
||||
{
|
||||
JERRY_ASSERT (tree->type == SCOPE_TYPE_FUNCTION);
|
||||
|
||||
JERRY_ASSERT (var_decl_om_p->op.op_idx == VM_OP_VAR_DECL);
|
||||
JERRY_ASSERT (var_decl_om_p->lit_id[0].packed_value != NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (var_decl_om_p->lit_id[1].packed_value == NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (var_decl_om_p->lit_id[2].packed_value == NOT_A_LITERAL.packed_value);
|
||||
lit_cpointer_t lit_cp;
|
||||
bool is_arg;
|
||||
|
||||
vm_instr_counter_t instr_pos = 0;
|
||||
|
||||
op_meta header_opm = scopes_tree_op_meta (tree, instr_pos++);
|
||||
JERRY_ASSERT (header_opm.op.op_idx == VM_OP_FUNC_EXPR_N || header_opm.op.op_idx == VM_OP_FUNC_DECL_N);
|
||||
|
||||
while (true)
|
||||
if (om_p->op.op_idx == VM_OP_VAR_DECL)
|
||||
{
|
||||
op_meta meta_opm = scopes_tree_op_meta (tree, instr_pos++);
|
||||
JERRY_ASSERT (meta_opm.op.op_idx == VM_OP_META);
|
||||
JERRY_ASSERT (om_p->lit_id[0].packed_value != NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (om_p->lit_id[1].packed_value == NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (om_p->lit_id[2].packed_value == NOT_A_LITERAL.packed_value);
|
||||
|
||||
opcode_meta_type meta_type = (opcode_meta_type) meta_opm.op.data.meta.type;
|
||||
lit_cp = om_p->lit_id[0];
|
||||
|
||||
if (meta_type == OPCODE_META_TYPE_FUNCTION_END)
|
||||
is_arg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (om_p->op.op_idx == VM_OP_META);
|
||||
|
||||
JERRY_ASSERT (om_p->op.data.meta.type == OPCODE_META_TYPE_VARG);
|
||||
JERRY_ASSERT (om_p->lit_id[0].packed_value == NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (om_p->lit_id[1].packed_value != NOT_A_LITERAL.packed_value);
|
||||
JERRY_ASSERT (om_p->lit_id[2].packed_value == NOT_A_LITERAL.packed_value);
|
||||
|
||||
lit_cp = om_p->lit_id[1];
|
||||
|
||||
is_arg = true;
|
||||
}
|
||||
|
||||
vm_idx_t reg;
|
||||
|
||||
if (is_arg)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_args != VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args < VM_REG_GENERAL_LAST);
|
||||
|
||||
reg = ++jsp_reg_max_for_args;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var != VM_IDX_EMPTY);
|
||||
|
||||
if (jsp_reg_max_for_local_var == VM_REG_GENERAL_LAST)
|
||||
{
|
||||
/* marker of function argument list end reached */
|
||||
break;
|
||||
/* not enough registers */
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (meta_type == OPCODE_META_TYPE_VARG);
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var < VM_REG_GENERAL_LAST);
|
||||
|
||||
/* the varg specifies argument name, and so should be a string literal */
|
||||
JERRY_ASSERT (meta_opm.op.data.meta.data_1 == VM_IDX_REWRITE_LITERAL_UID);
|
||||
JERRY_ASSERT (meta_opm.lit_id[1].packed_value != NOT_A_LITERAL.packed_value);
|
||||
|
||||
if (meta_opm.lit_id[1].packed_value == var_decl_om_p->lit_id[0].packed_value)
|
||||
{
|
||||
/*
|
||||
* Optimization is not performed, because the variable's name is equal to an argument name,
|
||||
* and the argument's value would be initialized by its name in run-time.
|
||||
*
|
||||
* See also:
|
||||
* parser_parse_program
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
reg = ++jsp_reg_max_for_local_var;
|
||||
}
|
||||
|
||||
if (jsp_reg_max_for_local_var == VM_IDX_EMPTY)
|
||||
{
|
||||
jsp_reg_max_for_local_var = jsp_reg_max_for_temps;
|
||||
}
|
||||
|
||||
if (jsp_reg_max_for_local_var == VM_REG_GENERAL_LAST)
|
||||
{
|
||||
/* not enough registers */
|
||||
return false;
|
||||
}
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var < VM_REG_GENERAL_LAST);
|
||||
|
||||
vm_idx_t reg = ++jsp_reg_max_for_local_var;
|
||||
|
||||
lit_cpointer_t lit_cp = var_decl_om_p->lit_id[0];
|
||||
|
||||
for (vm_instr_counter_t instr_pos = 0;
|
||||
instr_pos < tree->instrs_count;
|
||||
instr_pos++)
|
||||
@@ -320,9 +365,21 @@ dumper_try_replace_var_with_reg (scopes_tree tree, /**< a function scope, create
|
||||
}
|
||||
|
||||
return true;
|
||||
} /* dumper_try_replace_var_with_reg */
|
||||
} /* dumper_try_replace_identifier_name_with_reg */
|
||||
#endif /* CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER */
|
||||
|
||||
/**
|
||||
* Just allocate register for argument that is never used due to duplicated argument names
|
||||
*/
|
||||
void
|
||||
dumper_alloc_reg_for_unused_arg (void)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_args != VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args < VM_REG_GENERAL_LAST);
|
||||
|
||||
++jsp_reg_max_for_args;
|
||||
} /* dumper_alloc_reg_for_unused_arg */
|
||||
|
||||
/**
|
||||
* Generate instruction with specified opcode and operands
|
||||
*
|
||||
@@ -650,6 +707,7 @@ void
|
||||
dumper_new_scope (void)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var == VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args == VM_IDX_EMPTY);
|
||||
|
||||
STACK_PUSH (jsp_reg_id_stack, jsp_reg_next);
|
||||
STACK_PUSH (jsp_reg_id_stack, jsp_reg_max_for_temps);
|
||||
@@ -662,6 +720,7 @@ void
|
||||
dumper_finish_scope (void)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_local_var == VM_IDX_EMPTY);
|
||||
JERRY_ASSERT (jsp_reg_max_for_args == VM_IDX_EMPTY);
|
||||
|
||||
jsp_reg_max_for_temps = STACK_TOP (jsp_reg_id_stack);
|
||||
STACK_DROP (jsp_reg_id_stack, 1);
|
||||
@@ -1182,6 +1241,23 @@ rewrite_function_end ()
|
||||
STACK_DROP (function_ends, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement position of 'function_end' instruction (the position is stored in "function_ends" stack)
|
||||
*
|
||||
* Note:
|
||||
* The operation is used upon deleting a 'varg' meta, describing element of a function's formal parameters list
|
||||
*/
|
||||
void
|
||||
dumper_decrement_function_end_pos (void)
|
||||
{
|
||||
vm_instr_counter_t oc = STACK_TOP (function_ends);
|
||||
|
||||
oc--;
|
||||
|
||||
STACK_DROP (function_ends, 1);
|
||||
STACK_PUSH (function_ends, oc);
|
||||
} /* dumper_decrement_function_end_pos */
|
||||
|
||||
jsp_operand_t
|
||||
dump_this_res (void)
|
||||
{
|
||||
@@ -2308,27 +2384,6 @@ dump_throw (jsp_operand_t op)
|
||||
dump_single_address (VM_OP_THROW_VALUE, op);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if variable is already declared
|
||||
*
|
||||
* @return true if variable declaration already exists
|
||||
* false otherwise
|
||||
*/
|
||||
bool
|
||||
dumper_variable_declaration_exists (lit_cpointer_t lit_id) /**< literal which holds variable's name */
|
||||
{
|
||||
vm_instr_counter_t var_decls_count = (vm_instr_counter_t) serializer_get_current_var_decls_counter ();
|
||||
for (vm_instr_counter_t oc = (vm_instr_counter_t) (0); oc < var_decls_count; oc++)
|
||||
{
|
||||
const op_meta var_decl_op_meta = serializer_get_var_decl (oc);
|
||||
if (var_decl_op_meta.lit_id[0].packed_value == lit_id.packed_value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} /* dumper_variable_declaration_exists */
|
||||
|
||||
/**
|
||||
* Dump instruction designating variable declaration
|
||||
*/
|
||||
@@ -2386,20 +2441,30 @@ dump_ret (void)
|
||||
serializer_dump_op_meta (jsp_dmp_create_op_meta_0 (VM_OP_RET));
|
||||
}
|
||||
|
||||
void
|
||||
/**
|
||||
* Dump 'reg_var_decl' instruction template
|
||||
*
|
||||
* @return position of the dumped instruction
|
||||
*/
|
||||
vm_instr_counter_t
|
||||
dump_reg_var_decl_for_rewrite (void)
|
||||
{
|
||||
STACK_PUSH (reg_var_decls, serializer_get_current_instr_counter ());
|
||||
dump_double_address (VM_OP_REG_VAR_DECL,
|
||||
vm_instr_counter_t oc = serializer_get_current_instr_counter ();
|
||||
|
||||
dump_triple_address (VM_OP_REG_VAR_DECL,
|
||||
jsp_operand_t::make_unknown_operand (),
|
||||
jsp_operand_t::make_unknown_operand (),
|
||||
jsp_operand_t::make_unknown_operand ());
|
||||
}
|
||||
|
||||
return oc;
|
||||
} /* dump_reg_var_decl_for_rewrite */
|
||||
|
||||
/**
|
||||
* Rewrite 'reg_var_decl' instruction's template with current scope's register counts
|
||||
*/
|
||||
void
|
||||
rewrite_reg_var_decl (void)
|
||||
rewrite_reg_var_decl (vm_instr_counter_t reg_var_decl_oc) /**< position of dumped 'reg_var_decl' template */
|
||||
{
|
||||
vm_instr_counter_t reg_var_decl_oc = STACK_TOP (reg_var_decls);
|
||||
|
||||
op_meta opm = serializer_get_op_meta (reg_var_decl_oc);
|
||||
JERRY_ASSERT (opm.op.op_idx == VM_OP_REG_VAR_DECL);
|
||||
|
||||
@@ -2417,10 +2482,28 @@ rewrite_reg_var_decl (void)
|
||||
opm.op.data.reg_var_decl.local_var_regs_num = 0;
|
||||
}
|
||||
|
||||
serializer_rewrite_op_meta (reg_var_decl_oc, opm);
|
||||
if (jsp_reg_max_for_args != VM_IDX_EMPTY)
|
||||
{
|
||||
if (jsp_reg_max_for_local_var != VM_IDX_EMPTY)
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_args >= jsp_reg_max_for_local_var);
|
||||
opm.op.data.reg_var_decl.arg_regs_num = (vm_idx_t) (jsp_reg_max_for_args - jsp_reg_max_for_local_var);
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (jsp_reg_max_for_args >= jsp_reg_max_for_temps);
|
||||
opm.op.data.reg_var_decl.arg_regs_num = (vm_idx_t) (jsp_reg_max_for_args - jsp_reg_max_for_temps);
|
||||
}
|
||||
|
||||
STACK_DROP (reg_var_decls, 1);
|
||||
}
|
||||
jsp_reg_max_for_args = VM_IDX_EMPTY;
|
||||
}
|
||||
else
|
||||
{
|
||||
opm.op.data.reg_var_decl.arg_regs_num = 0;
|
||||
}
|
||||
|
||||
serializer_rewrite_op_meta (reg_var_decl_oc, opm);
|
||||
} /* rewrite_reg_var_decl */
|
||||
|
||||
void
|
||||
dump_retval (jsp_operand_t op)
|
||||
@@ -2434,6 +2517,7 @@ dumper_init (void)
|
||||
jsp_reg_next = VM_REG_GENERAL_FIRST;
|
||||
jsp_reg_max_for_temps = VM_REG_GENERAL_FIRST;
|
||||
jsp_reg_max_for_local_var = VM_IDX_EMPTY;
|
||||
jsp_reg_max_for_args = VM_IDX_EMPTY;
|
||||
|
||||
STACK_INIT (U8);
|
||||
STACK_INIT (varg_headers);
|
||||
@@ -2449,7 +2533,6 @@ dumper_init (void)
|
||||
STACK_INIT (finallies);
|
||||
STACK_INIT (tries);
|
||||
STACK_INIT (jsp_reg_id_stack);
|
||||
STACK_INIT (reg_var_decls);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -2469,5 +2552,4 @@ dumper_free (void)
|
||||
STACK_FREE (finallies);
|
||||
STACK_FREE (tries);
|
||||
STACK_FREE (jsp_reg_id_stack);
|
||||
STACK_FREE (reg_var_decls);
|
||||
}
|
||||
|
||||
@@ -305,7 +305,10 @@ bool operand_is_empty (jsp_operand_t);
|
||||
void dumper_init (void);
|
||||
void dumper_free (void);
|
||||
|
||||
bool dumper_try_replace_var_with_reg (scopes_tree, op_meta *);
|
||||
void dumper_start_move_of_vars_to_regs ();
|
||||
bool dumper_start_move_of_args_to_regs (uint32_t args_num);
|
||||
bool dumper_try_replace_identifier_name_with_reg (scopes_tree, op_meta *);
|
||||
void dumper_alloc_reg_for_unused_arg (void);
|
||||
|
||||
void dumper_new_statement (void);
|
||||
void dumper_new_scope (void);
|
||||
@@ -347,6 +350,7 @@ void dump_prop_setter (jsp_operand_t, jsp_operand_t, jsp_operand_t);
|
||||
|
||||
void dump_function_end_for_rewrite (void);
|
||||
void rewrite_function_end ();
|
||||
void dumper_decrement_function_end_pos (void);
|
||||
|
||||
jsp_operand_t dump_this_res (void);
|
||||
|
||||
@@ -474,14 +478,13 @@ void rewrite_finally (void);
|
||||
void dump_end_try_catch_finally (void);
|
||||
void dump_throw (jsp_operand_t);
|
||||
|
||||
bool dumper_variable_declaration_exists (lit_cpointer_t);
|
||||
void dump_variable_declaration (lit_cpointer_t);
|
||||
|
||||
vm_instr_counter_t dump_scope_code_flags_for_rewrite (void);
|
||||
void rewrite_scope_code_flags (vm_instr_counter_t, opcode_scope_code_flags_t);
|
||||
|
||||
void dump_reg_var_decl_for_rewrite (void);
|
||||
void rewrite_reg_var_decl (void);
|
||||
vm_instr_counter_t dump_reg_var_decl_for_rewrite (void);
|
||||
void rewrite_reg_var_decl (vm_instr_counter_t);
|
||||
|
||||
void dump_ret (void);
|
||||
void dump_retval (jsp_operand_t);
|
||||
|
||||
+165
-28
@@ -1852,18 +1852,19 @@ static jsp_operand_t
|
||||
parse_variable_declaration (void)
|
||||
{
|
||||
current_token_must_be (TOK_NAME);
|
||||
const jsp_operand_t name = literal_operand (token_data_as_lit_cp ());
|
||||
|
||||
if (!dumper_variable_declaration_exists (token_data_as_lit_cp ()))
|
||||
const lit_cpointer_t lit_cp = token_data_as_lit_cp ();
|
||||
const jsp_operand_t name = literal_operand (lit_cp);
|
||||
|
||||
if (!scopes_tree_variable_declaration_exists (STACK_TOP (scopes), lit_cp))
|
||||
{
|
||||
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (literal_operand (token_data_as_lit_cp ()),
|
||||
is_strict_mode (),
|
||||
tok.loc);
|
||||
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (name, is_strict_mode (), tok.loc);
|
||||
|
||||
dump_variable_declaration (token_data_as_lit_cp ());
|
||||
dump_variable_declaration (lit_cp);
|
||||
}
|
||||
|
||||
skip_newlines ();
|
||||
|
||||
if (token_is (TOK_EQ))
|
||||
{
|
||||
skip_newlines ();
|
||||
@@ -3014,7 +3015,7 @@ parse_source_element_list (bool is_global, /**< flag, indicating that we parsing
|
||||
|
||||
check_directive_prologue_for_use_strict ();
|
||||
|
||||
dump_reg_var_decl_for_rewrite ();
|
||||
vm_instr_counter_t reg_var_decl_oc = dump_reg_var_decl_for_rewrite ();
|
||||
|
||||
if (inside_eval
|
||||
&& !inside_function)
|
||||
@@ -3054,8 +3055,6 @@ parse_source_element_list (bool is_global, /**< flag, indicating that we parsing
|
||||
scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER);
|
||||
}
|
||||
|
||||
rewrite_scope_code_flags (scope_code_flags_oc, scope_flags);
|
||||
|
||||
#ifdef CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER
|
||||
if (is_try_replace_local_vars_with_regs
|
||||
&& fe_scope_tree->type == SCOPE_TYPE_FUNCTION)
|
||||
@@ -3083,30 +3082,167 @@ parse_source_element_list (bool is_global, /**< flag, indicating that we parsing
|
||||
/* no subscopes, as no function declarations / eval etc. in the scope */
|
||||
JERRY_ASSERT (fe_scope_tree->t.children_num == 0);
|
||||
|
||||
bool are_all_vars_replaced = true;
|
||||
for (vm_instr_counter_t var_decl_pos = 0;
|
||||
var_decl_pos < linked_list_get_length (fe_scope_tree->var_decls);
|
||||
var_decl_pos++)
|
||||
{
|
||||
op_meta *om_p = (op_meta *) linked_list_element (fe_scope_tree->var_decls, var_decl_pos);
|
||||
vm_instr_counter_t instr_pos = 0u;
|
||||
|
||||
if (!dumper_try_replace_var_with_reg (fe_scope_tree, om_p))
|
||||
const vm_instr_counter_t header_oc = instr_pos++;
|
||||
op_meta header_opm = scopes_tree_op_meta (fe_scope_tree, header_oc);
|
||||
JERRY_ASSERT (header_opm.op.op_idx == VM_OP_FUNC_EXPR_N || header_opm.op.op_idx == VM_OP_FUNC_DECL_N);
|
||||
|
||||
vm_instr_counter_t function_end_pos = instr_pos;
|
||||
while (true)
|
||||
{
|
||||
op_meta meta_opm = scopes_tree_op_meta (fe_scope_tree, function_end_pos);
|
||||
JERRY_ASSERT (meta_opm.op.op_idx == VM_OP_META);
|
||||
|
||||
opcode_meta_type meta_type = (opcode_meta_type) meta_opm.op.data.meta.type;
|
||||
|
||||
if (meta_type == OPCODE_META_TYPE_FUNCTION_END)
|
||||
{
|
||||
are_all_vars_replaced = false;
|
||||
/* marker of function argument list end reached */
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (meta_type == OPCODE_META_TYPE_VARG);
|
||||
|
||||
function_end_pos++;
|
||||
}
|
||||
}
|
||||
|
||||
if (are_all_vars_replaced)
|
||||
{
|
||||
/*
|
||||
* All local variables were replaced with registers, so variable declarations could be removed.
|
||||
*
|
||||
* TODO:
|
||||
* Support removal of a particular variable declaration, without removing the whole list.
|
||||
*/
|
||||
uint32_t args_num = (uint32_t) (function_end_pos - instr_pos);
|
||||
|
||||
linked_list_free (fe_scope_tree->var_decls);
|
||||
fe_scope_tree->var_decls = linked_list_init (sizeof (op_meta));
|
||||
dumper_start_move_of_vars_to_regs ();
|
||||
|
||||
/* remove declarations of variables with names equal to an argument's name */
|
||||
vm_instr_counter_t var_decl_pos = 0;
|
||||
while (var_decl_pos < linked_list_get_length (fe_scope_tree->var_decls))
|
||||
{
|
||||
op_meta *om_p = (op_meta *) linked_list_element (fe_scope_tree->var_decls, var_decl_pos);
|
||||
bool is_removed = false;
|
||||
|
||||
for (vm_instr_counter_t arg_index = instr_pos;
|
||||
arg_index < function_end_pos;
|
||||
arg_index++)
|
||||
{
|
||||
op_meta meta_opm = scopes_tree_op_meta (fe_scope_tree, arg_index);
|
||||
JERRY_ASSERT (meta_opm.op.op_idx == VM_OP_META);
|
||||
|
||||
JERRY_ASSERT (meta_opm.op.data.meta.data_1 == VM_IDX_REWRITE_LITERAL_UID);
|
||||
JERRY_ASSERT (om_p->op.data.var_decl.variable_name == VM_IDX_REWRITE_LITERAL_UID);
|
||||
|
||||
if (meta_opm.lit_id[1].packed_value == om_p->lit_id[0].packed_value)
|
||||
{
|
||||
linked_list_remove_element (fe_scope_tree->var_decls, var_decl_pos);
|
||||
|
||||
is_removed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_removed)
|
||||
{
|
||||
if (!dumper_try_replace_identifier_name_with_reg (fe_scope_tree, om_p))
|
||||
{
|
||||
var_decl_pos++;
|
||||
}
|
||||
else
|
||||
{
|
||||
linked_list_remove_element (fe_scope_tree->var_decls, var_decl_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dumper_start_move_of_args_to_regs (args_num))
|
||||
{
|
||||
scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS);
|
||||
|
||||
JERRY_ASSERT (linked_list_get_length (fe_scope_tree->var_decls) == 0);
|
||||
scope_flags = (opcode_scope_code_flags_t) (scope_flags | OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV);
|
||||
|
||||
/* at this point all arguments can be moved to registers */
|
||||
if (header_opm.op.op_idx == VM_OP_FUNC_EXPR_N)
|
||||
{
|
||||
header_opm.op.data.func_expr_n.arg_list = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (header_opm.op.op_idx == VM_OP_FUNC_DECL_N);
|
||||
|
||||
header_opm.op.data.func_decl_n.arg_list = 0;
|
||||
}
|
||||
|
||||
scopes_tree_set_op_meta (fe_scope_tree, header_oc, header_opm);
|
||||
|
||||
/*
|
||||
* Mark duplicated arguments names as empty,
|
||||
* leaving only last declaration for each duplicated
|
||||
* argument name
|
||||
*/
|
||||
for (vm_instr_counter_t arg1_index = instr_pos;
|
||||
arg1_index < function_end_pos;
|
||||
arg1_index++)
|
||||
{
|
||||
op_meta meta_opm1 = scopes_tree_op_meta (fe_scope_tree, arg1_index);
|
||||
JERRY_ASSERT (meta_opm1.op.op_idx == VM_OP_META);
|
||||
|
||||
for (vm_instr_counter_t arg2_index = (vm_instr_counter_t) (arg1_index + 1u);
|
||||
arg2_index < function_end_pos;
|
||||
arg2_index++)
|
||||
{
|
||||
op_meta meta_opm2 = scopes_tree_op_meta (fe_scope_tree, arg2_index);
|
||||
JERRY_ASSERT (meta_opm2.op.op_idx == VM_OP_META);
|
||||
|
||||
if (meta_opm1.lit_id[1].packed_value == meta_opm2.lit_id[1].packed_value)
|
||||
{
|
||||
meta_opm1.op.data.meta.data_1 = VM_IDX_EMPTY;
|
||||
meta_opm1.lit_id[1] = NOT_A_LITERAL;
|
||||
|
||||
scopes_tree_set_op_meta (fe_scope_tree, arg1_index, meta_opm1);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
op_meta meta_opm = scopes_tree_op_meta (fe_scope_tree, instr_pos);
|
||||
JERRY_ASSERT (meta_opm.op.op_idx == VM_OP_META);
|
||||
|
||||
opcode_meta_type meta_type = (opcode_meta_type) meta_opm.op.data.meta.type;
|
||||
|
||||
if (meta_type == OPCODE_META_TYPE_FUNCTION_END)
|
||||
{
|
||||
/* marker of function argument list end reached */
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (meta_type == OPCODE_META_TYPE_VARG);
|
||||
|
||||
if (meta_opm.op.data.meta.data_1 == VM_IDX_EMPTY)
|
||||
{
|
||||
JERRY_ASSERT (meta_opm.lit_id[1].packed_value == NOT_A_LITERAL.packed_value);
|
||||
|
||||
dumper_alloc_reg_for_unused_arg ();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* the varg specifies argument name, and so should be a string literal */
|
||||
JERRY_ASSERT (meta_opm.op.data.meta.data_1 == VM_IDX_REWRITE_LITERAL_UID);
|
||||
JERRY_ASSERT (meta_opm.lit_id[1].packed_value != NOT_A_LITERAL.packed_value);
|
||||
|
||||
bool is_replaced = dumper_try_replace_identifier_name_with_reg (fe_scope_tree, &meta_opm);
|
||||
JERRY_ASSERT (is_replaced);
|
||||
}
|
||||
|
||||
scopes_tree_remove_op_meta (fe_scope_tree, instr_pos);
|
||||
|
||||
reg_var_decl_oc--;
|
||||
scope_code_flags_oc--;
|
||||
dumper_decrement_function_end_pos ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3114,7 +3250,8 @@ parse_source_element_list (bool is_global, /**< flag, indicating that we parsing
|
||||
(void) is_try_replace_local_vars_with_regs;
|
||||
#endif /* !CONFIG_PARSER_ENABLE_PARSE_TIME_BYTE_CODE_OPTIMIZER */
|
||||
|
||||
rewrite_reg_var_decl ();
|
||||
rewrite_scope_code_flags (scope_code_flags_oc, scope_flags);
|
||||
rewrite_reg_var_decl (reg_var_decl_oc);
|
||||
dumper_finish_scope ();
|
||||
} /* parse_source_element_list */
|
||||
|
||||
|
||||
@@ -118,6 +118,20 @@ scopes_tree_var_decl (scopes_tree tree, /**< scope, from which variable declarat
|
||||
return *(op_meta *) linked_list_element (tree->var_decls, oc);
|
||||
} /* scopes_tree_var_decl */
|
||||
|
||||
/**
|
||||
* Remove specified instruction from scopes tree node's instructions list
|
||||
*/
|
||||
void
|
||||
scopes_tree_remove_op_meta (scopes_tree tree, /**< scopes tree node */
|
||||
vm_instr_counter_t oc) /**< position of instruction to remove */
|
||||
{
|
||||
assert_tree (tree);
|
||||
JERRY_ASSERT (oc < tree->instrs_count);
|
||||
|
||||
linked_list_remove_element (tree->instrs, oc);
|
||||
tree->instrs_count--;
|
||||
} /* scopes_tree_remove_op_meta */
|
||||
|
||||
vm_instr_counter_t
|
||||
scopes_tree_count_instructions (scopes_tree t)
|
||||
{
|
||||
@@ -132,6 +146,35 @@ scopes_tree_count_instructions (scopes_tree t)
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if variable declaration exists in the scope
|
||||
*
|
||||
* @return true / false
|
||||
*/
|
||||
bool
|
||||
scopes_tree_variable_declaration_exists (scopes_tree tree, /**< scope */
|
||||
lit_cpointer_t lit_id) /**< literal which holds variable's name */
|
||||
{
|
||||
assert_tree (tree);
|
||||
|
||||
for (vm_instr_counter_t oc = 0u;
|
||||
oc < linked_list_get_length (tree->var_decls);
|
||||
oc++)
|
||||
{
|
||||
const op_meta* var_decl_om_p = (op_meta *) linked_list_element (tree->var_decls, oc);
|
||||
|
||||
JERRY_ASSERT (var_decl_om_p->op.op_idx == VM_OP_VAR_DECL);
|
||||
JERRY_ASSERT (var_decl_om_p->op.data.var_decl.variable_name == VM_IDX_REWRITE_LITERAL_UID);
|
||||
|
||||
if (var_decl_om_p->lit_id[0].packed_value == lit_id.packed_value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} /* scopes_tree_variable_declaration_exists */
|
||||
|
||||
static uint16_t
|
||||
lit_id_hash (void * lit_id)
|
||||
{
|
||||
|
||||
@@ -90,8 +90,10 @@ void scopes_tree_set_op_meta (scopes_tree, vm_instr_counter_t, op_meta);
|
||||
void scopes_tree_set_instrs_num (scopes_tree, vm_instr_counter_t);
|
||||
op_meta scopes_tree_op_meta (scopes_tree, vm_instr_counter_t);
|
||||
op_meta scopes_tree_var_decl (scopes_tree, vm_instr_counter_t);
|
||||
void scopes_tree_remove_op_meta (scopes_tree tree, vm_instr_counter_t oc);
|
||||
size_t scopes_tree_count_literals_in_blocks (scopes_tree);
|
||||
vm_instr_counter_t scopes_tree_count_instructions (scopes_tree);
|
||||
bool scopes_tree_variable_declaration_exists (scopes_tree, lit_cpointer_t);
|
||||
vm_instr_t *scopes_tree_raw_data (scopes_tree, uint8_t *, size_t, lit_id_hash_table *);
|
||||
void scopes_tree_set_strict_mode (scopes_tree, bool);
|
||||
void scopes_tree_set_arguments_used (scopes_tree);
|
||||
|
||||
@@ -34,18 +34,6 @@ serializer_get_op_meta (vm_instr_counter_t oc)
|
||||
return scopes_tree_op_meta (current_scope, oc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get variable declaration of the current scope
|
||||
*
|
||||
* @return variable declaration instruction
|
||||
*/
|
||||
op_meta
|
||||
serializer_get_var_decl (vm_instr_counter_t oc) /**< index of variable declaration */
|
||||
{
|
||||
JERRY_ASSERT (current_scope);
|
||||
return scopes_tree_var_decl (current_scope, oc);
|
||||
} /* serializer_get_var_decl */
|
||||
|
||||
/**
|
||||
* Get byte-code instruction from current scope, or specified byte-code array
|
||||
*
|
||||
@@ -231,17 +219,6 @@ serializer_get_current_instr_counter (void)
|
||||
return scopes_tree_instrs_num (current_scope);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of variable declarations in the current scope
|
||||
*
|
||||
* @return count of variable declarations
|
||||
*/
|
||||
vm_instr_counter_t
|
||||
serializer_get_current_var_decls_counter (void)
|
||||
{
|
||||
return scopes_tree_var_decls_num (current_scope);
|
||||
} /* serializer_get_current_var_decls_counter */
|
||||
|
||||
vm_instr_counter_t
|
||||
serializer_count_instrs_in_subscopes (void)
|
||||
{
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
void serializer_init ();
|
||||
void serializer_set_show_instrs (bool);
|
||||
op_meta serializer_get_op_meta (vm_instr_counter_t);
|
||||
op_meta serializer_get_var_decl (vm_instr_counter_t);
|
||||
vm_instr_t serializer_get_instr (const bytecode_data_header_t *, vm_instr_counter_t);
|
||||
lit_cpointer_t serializer_get_literal_cp_by_uid (uint8_t, const bytecode_data_header_t *, vm_instr_counter_t);
|
||||
void serializer_set_scope (scopes_tree);
|
||||
@@ -37,7 +36,6 @@ const bytecode_data_header_t *serializer_merge_scopes_into_bytecode (void);
|
||||
void serializer_dump_op_meta (op_meta);
|
||||
void serializer_dump_var_decl (op_meta);
|
||||
vm_instr_counter_t serializer_get_current_instr_counter (void);
|
||||
vm_instr_counter_t serializer_get_current_var_decls_counter (void);
|
||||
vm_instr_counter_t serializer_count_instrs_in_subscopes (void);
|
||||
void serializer_set_writing_position (vm_instr_counter_t);
|
||||
void serializer_rewrite_op_meta (vm_instr_counter_t, op_meta);
|
||||
|
||||
Reference in New Issue
Block a user