From 4b325ea9e3f96a070e095bd04babb52b98fc305a Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Thu, 13 Feb 2020 17:39:56 +0100 Subject: [PATCH] Rework map_to member of the scope chain that it can store flags. (#3552) JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/js-lexer.c | 8 +-- jerry-core/parser/js/js-parser-internal.h | 28 +++++++-- jerry-core/parser/js/js-parser-limits.h | 2 +- jerry-core/parser/js/js-parser-statm.c | 2 +- jerry-core/parser/js/js-parser-util.c | 24 ++++--- jerry-core/parser/js/js-parser.c | 6 +- jerry-core/parser/js/js-scanner-util.c | 77 +++++++++++++++++++---- 7 files changed, 116 insertions(+), 31 deletions(-) diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 4739f60bf..e50ad141e 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -2199,8 +2199,8 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */ if (search_scope_stack) { - parser_scope_stack *scope_stack_start_p = context_p->scope_stack_p; - parser_scope_stack *scope_stack_p = scope_stack_start_p + context_p->scope_stack_top; + parser_scope_stack_t *scope_stack_start_p = context_p->scope_stack_p; + parser_scope_stack_t *scope_stack_p = scope_stack_start_p + context_p->scope_stack_top; while (scope_stack_p > scope_stack_start_p) { @@ -2215,9 +2215,9 @@ lexer_construct_literal_object (parser_context_t *context_p, /**< context */ if (cond) { - JERRY_ASSERT (scope_stack_p->map_to >= PARSER_REGISTER_START + JERRY_ASSERT (scanner_decode_map_to (scope_stack_p) >= PARSER_REGISTER_START || (literal_p->status_flags & LEXER_FLAG_USED)); - context_p->lit_object.index = scope_stack_p->map_to; + context_p->lit_object.index = scanner_decode_map_to (scope_stack_p); return; } } diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index a5577f0e4..39e1b0467 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -352,8 +352,8 @@ typedef struct parser_branch_node_t typedef struct { uint16_t map_from; /**< original literal index */ - uint16_t map_to; /**< register or literal index */ -} parser_scope_stack; + uint16_t map_to; /**< encoded register or literal index and flags */ +} parser_scope_stack_t; /** * This item represents a function literal in the scope stack. @@ -368,6 +368,25 @@ typedef struct */ #define PARSER_SCOPE_STACK_FUNC 0xffff +#if ENABLED (JERRY_ES2015) + +/** + * Mask for decoding the register index of map_to + */ +#define PARSER_SCOPE_STACK_REGISTER_MASK 0x3fff + +/** + * The scope stack item represents a lexical declaration (let/const) + */ +#define PARSER_SCOPE_STACK_IS_LEXICAL 0x4000 + +/** + * The scope stack item represents a const declaration + */ +#define PARSER_SCOPE_STACK_IS_CONST 0x8000 + +#endif /* ENABLED (JERRY_ES2015) */ + /** * Starting literal index for registers. */ @@ -416,7 +435,7 @@ typedef struct parser_saved_context_t parser_mem_data_t byte_code; /**< byte code buffer */ uint32_t byte_code_size; /**< byte code size for branches */ parser_mem_data_t literal_pool_data; /**< literal list */ - parser_scope_stack *scope_stack_p; /**< scope stack */ + parser_scope_stack_t *scope_stack_p; /**< scope stack */ uint16_t scope_stack_size; /**< size of scope stack */ uint16_t scope_stack_top; /**< preserved top of scope stack */ uint16_t scope_stack_reg_top; /**< preserved top register of scope stack */ @@ -486,7 +505,7 @@ typedef struct uint32_t byte_code_size; /**< current byte code size for branches */ parser_list_t literal_pool; /**< literal list */ parser_mem_data_t stack; /**< storage space */ - parser_scope_stack *scope_stack_p; /**< scope stack */ + parser_scope_stack_t *scope_stack_p; /**< scope stack */ parser_mem_page_t *free_page_p; /**< space for fast allocation */ uint16_t scope_stack_size; /**< size of scope stack */ uint16_t scope_stack_top; /**< current top of scope stack */ @@ -713,6 +732,7 @@ void scanner_create_variables (parser_context_t *context_p, uint32_t option_flag void scanner_get_location (scanner_location_t *location_p, parser_context_t *context_p); void scanner_set_location (parser_context_t *context_p, scanner_location_t *location_p); +uint16_t scanner_decode_map_to (parser_scope_stack_t *stack_item_p); void scanner_scan_all (parser_context_t *context_p, const uint8_t *arg_list_p, const uint8_t *arg_list_end_p, const uint8_t *source_p, const uint8_t *source_end_p); diff --git a/jerry-core/parser/js/js-parser-limits.h b/jerry-core/parser/js/js-parser-limits.h index d37ccd6c7..b150b2e80 100644 --- a/jerry-core/parser/js/js-parser-limits.h +++ b/jerry-core/parser/js/js-parser-limits.h @@ -54,7 +54,7 @@ /** * Maximum number of registers. - * Limit: min: 256, max: PARSER_MAXIMUM_NUMBER_OF_LITERALS / 2 + * Limit: min: 256, max: min(PARSER_MAXIMUM_NUMBER_OF_LITERALS / 2, 16383) */ #ifndef PARSER_MAXIMUM_NUMBER_OF_REGISTERS #define PARSER_MAXIMUM_NUMBER_OF_REGISTERS 256 diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index 967e562d5..33be9acee 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -698,7 +698,7 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ #endif /* ENABLED (JERRY_DEBUGGER) */ JERRY_ASSERT (context_p->scope_stack_top >= 2); - parser_scope_stack *scope_stack_p = context_p->scope_stack_p + context_p->scope_stack_top - 2; + parser_scope_stack_t *scope_stack_p = context_p->scope_stack_p + context_p->scope_stack_top - 2; uint16_t literal_index = context_p->lit_object.index; diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index 1601cb28d..b198d4cce 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -94,29 +94,39 @@ static void parser_print_literal (parser_context_t *context_p, /**< context */ uint16_t literal_index) /**< index of literal */ { - parser_scope_stack *scope_stack_p = context_p->scope_stack_p; - parser_scope_stack *scope_stack_end_p = scope_stack_p + context_p->scope_stack_top; - int32_t scope_literal_index = -1; + parser_scope_stack_t *scope_stack_p = context_p->scope_stack_p; + parser_scope_stack_t *scope_stack_end_p = scope_stack_p + context_p->scope_stack_top; + bool in_scope_literal = false; while (scope_stack_p < scope_stack_end_p) { scope_stack_end_p--; - if (literal_index == scope_stack_end_p->map_to) + if (scope_stack_end_p->map_from == PARSER_SCOPE_STACK_FUNC) { - scope_literal_index = scope_stack_end_p->map_from; + if (literal_index == scope_stack_end_p->map_to) + { + in_scope_literal = true; + break; + } + } + else if (scope_stack_end_p->map_to != PARSER_SCOPE_STACK_FUNC + && literal_index == scanner_decode_map_to (scope_stack_end_p)) + { + in_scope_literal = true; + break; } } if (literal_index < PARSER_REGISTER_START) { - JERRY_DEBUG_MSG (scope_literal_index != -1 ? " IDX:%d->" : " idx:%d->", literal_index); + JERRY_DEBUG_MSG (in_scope_literal ? " IDX:%d->" : " idx:%d->", literal_index); lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index); util_print_literal (literal_p); return; } - if (scope_literal_index == -1) + if (!in_scope_literal) { JERRY_DEBUG_MSG (" reg:%d", (int) (literal_index - PARSER_REGISTER_START)); return; diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index f385eee56..665e74800 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2194,7 +2194,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ if (context.scope_stack_p != NULL) { - parser_free (context.scope_stack_p, context.scope_stack_size * sizeof (parser_scope_stack)); + parser_free (context.scope_stack_p, context.scope_stack_size * sizeof (parser_scope_stack_t)); } #if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) @@ -2297,7 +2297,7 @@ parser_restore_context (parser_context_t *context_p, /**< context */ if (context_p->scope_stack_p != NULL) { - parser_free (context_p->scope_stack_p, context_p->scope_stack_size * sizeof (parser_scope_stack)); + parser_free (context_p->scope_stack_p, context_p->scope_stack_size * sizeof (parser_scope_stack_t)); } /* Restore private part of the context. */ @@ -2568,7 +2568,7 @@ parser_raise_error (parser_context_t *context_p, /**< context */ if (context_p->scope_stack_p != NULL) { - parser_free (context_p->scope_stack_p, context_p->scope_stack_size * sizeof (parser_scope_stack)); + parser_free (context_p->scope_stack_p, context_p->scope_stack_size * sizeof (parser_scope_stack_t)); } context_p->scope_stack_p = saved_context_p->scope_stack_p; context_p->scope_stack_size = saved_context_p->scope_stack_size; diff --git a/jerry-core/parser/js/js-scanner-util.c b/jerry-core/parser/js/js-scanner-util.c index b10776b01..35eb9ed24 100644 --- a/jerry-core/parser/js/js-scanner-util.c +++ b/jerry-core/parser/js/js-scanner-util.c @@ -1542,8 +1542,8 @@ static int32_t scanner_get_function_target (parser_context_t *context_p) /**< context */ { uint16_t literal_index = context_p->lit_object.index; - parser_scope_stack *scope_stack_start_p = context_p->scope_stack_p; - parser_scope_stack *scope_stack_p = scope_stack_start_p + context_p->scope_stack_top; + parser_scope_stack_t *scope_stack_start_p = context_p->scope_stack_p; + parser_scope_stack_t *scope_stack_p = scope_stack_start_p + context_p->scope_stack_top; while (scope_stack_p > scope_stack_start_p) { @@ -1558,7 +1558,7 @@ scanner_get_function_target (parser_context_t *context_p) /**< context */ return -1; } - return scope_stack_p->map_to; + return scanner_decode_map_to (scope_stack_p); } } @@ -1826,8 +1826,8 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ const uint8_t *next_data_p = (const uint8_t *) (info_p + 1); uint8_t info_type = info_p->type; lexer_lit_location_t literal; - parser_scope_stack *scope_stack_p; - parser_scope_stack *scope_stack_end_p; + parser_scope_stack_t *scope_stack_p; + parser_scope_stack_t *scope_stack_end_p; JERRY_ASSERT (info_type == SCANNER_TYPE_FUNCTION || info_type == SCANNER_TYPE_BLOCK); JERRY_ASSERT (!(option_flags & SCANNER_CREATE_VARS_IS_FUNCTION_ARGS) @@ -1839,14 +1839,14 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ { JERRY_ASSERT (context_p->scope_stack_p == NULL); - size_t stack_size = info_p->u16_arg * sizeof (parser_scope_stack); + size_t stack_size = info_p->u16_arg * sizeof (parser_scope_stack_t); context_p->scope_stack_size = info_p->u16_arg; scope_stack_p = NULL; if (stack_size > 0) { - scope_stack_p = (parser_scope_stack *) parser_malloc (context_p, stack_size); + scope_stack_p = (parser_scope_stack_t *) parser_malloc (context_p, stack_size); } context_p->scope_stack_p = scope_stack_p; @@ -1954,7 +1954,7 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ { JERRY_ASSERT (scope_stack_p >= context_p->scope_stack_p + 2); - parser_scope_stack *function_map_p = scope_stack_p - 2; + parser_scope_stack_t *function_map_p = scope_stack_p - 2; uint16_t literal_index = context_p->lit_object.index; while (literal_index != function_map_p->map_from) @@ -1966,7 +1966,10 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ JERRY_ASSERT (function_map_p[1].map_from == PARSER_SCOPE_STACK_FUNC); - parser_emit_cbc_literal_value (context_p, CBC_SET_VAR_FUNC, function_map_p[1].map_to, function_map_p[0].map_to); + parser_emit_cbc_literal_value (context_p, + CBC_SET_VAR_FUNC, + function_map_p[1].map_to, + scanner_decode_map_to (function_map_p)); continue; } @@ -2014,9 +2017,28 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ { map_to = (uint16_t) (PARSER_REGISTER_START + scope_stack_reg_top); +#if ENABLED (JERRY_ES2015) + scope_stack_p->map_to = (uint16_t) (scope_stack_reg_top + 1); +#else /* !ENABLED (JERRY_ES2015) */ scope_stack_p->map_to = map_to; +#endif /* ENABLED (JERRY_ES2015) */ + scope_stack_reg_top++; #if ENABLED (JERRY_ES2015) + switch (type) + { + case SCANNER_STREAM_TYPE_CONST: + { + scope_stack_p->map_to |= PARSER_SCOPE_STACK_IS_CONST; + /* FALLTHRU */ + } + case SCANNER_STREAM_TYPE_LET: + { + scope_stack_p->map_to |= PARSER_SCOPE_STACK_IS_LEXICAL; + break; + } + } + func_init_opcode = CBC_SET_VAR_FUNC; #endif /* ENABLED (JERRY_ES2015) */ } @@ -2025,7 +2047,11 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_USED; map_to = context_p->lit_object.index; +#if ENABLED (JERRY_ES2015) + scope_stack_p->map_to = 0; +#else /* !ENABLED (JERRY_ES2015) */ scope_stack_p->map_to = map_to; +#endif /* ENABLED (JERRY_ES2015) */ if (info_type == SCANNER_TYPE_FUNCTION) { @@ -2034,14 +2060,22 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ switch (type) { - case SCANNER_STREAM_TYPE_VAR: #if ENABLED (JERRY_ES2015) - case SCANNER_STREAM_TYPE_LET: case SCANNER_STREAM_TYPE_CONST: + { + scope_stack_p->map_to |= PARSER_SCOPE_STACK_IS_CONST; + /* FALLTHRU */ + } + case SCANNER_STREAM_TYPE_LET: + { + scope_stack_p->map_to |= PARSER_SCOPE_STACK_IS_LEXICAL; + /* FALLTHRU */ + } case SCANNER_STREAM_TYPE_LOCAL: case SCANNER_STREAM_TYPE_DESTRUCTURED_ARG: case SCANNER_STREAM_TYPE_DESTRUCTURED_ARG_FUNC: #endif /* ENABLED (JERRY_ES2015) */ + case SCANNER_STREAM_TYPE_VAR: { #if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) context_p->scope_stack_top = (uint16_t) (scope_stack_p - context_p->scope_stack_p); @@ -2157,7 +2191,11 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ lexer_construct_literal_object (context_p, &lexer_arguments_literal, lexer_arguments_literal.type); scope_stack_p->map_from = context_p->lit_object.index; +#if ENABLED (JERRY_ES2015) + scope_stack_p->map_to = 0; +#else /* !ENABLED (JERRY_ES2015) */ scope_stack_p->map_to = context_p->lit_object.index; +#endif /* ENABLED (JERRY_ES2015) */ scope_stack_p++; } @@ -2207,6 +2245,23 @@ scanner_set_location (parser_context_t *context_p, /**< context */ context_p->column = location_p->column; } /* scanner_set_location */ +/** + * Get the real map_to value. + */ +inline uint16_t JERRY_ATTR_ALWAYS_INLINE +scanner_decode_map_to (parser_scope_stack_t *stack_item_p) /**< scope stack item */ +{ + JERRY_ASSERT (stack_item_p->map_from != PARSER_SCOPE_STACK_FUNC + && stack_item_p->map_to != PARSER_SCOPE_STACK_FUNC); + +#if ENABLED (JERRY_ES2015) + uint16_t value = (stack_item_p->map_to & PARSER_SCOPE_STACK_REGISTER_MASK); + return (value == 0) ? stack_item_p->map_from : (uint16_t) (value + (PARSER_REGISTER_START - 1)); +#else /* !ENABLED (JERRY_ES2015) */ + return stack_item_p->map_to; +#endif /* ENABLED (JERRY_ES2015) */ +} /* scanner_decode_map_to */ + /** * @} * @}