From 3fb714685ee52eecc84c70d3127248e02730a91a Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Fri, 12 Jun 2020 00:07:58 -0700 Subject: [PATCH] Reduce try context stack consumption to 1 from 2. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/byte-code.h | 12 +++++++-- jerry-core/parser/js/js-parser-statm.c | 20 ++++++++++----- jerry-core/parser/js/js-parser.c | 5 ++++ jerry-core/vm/vm-stack.c | 24 ++++++++++++------ jerry-core/vm/vm.c | 34 ++++++++++++++------------ 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 0d0b29d6a8..9a4696dc6b 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -189,7 +189,9 @@ /* Stack consumption of opcodes with context. */ /* PARSER_TRY_CONTEXT_STACK_ALLOCATION must be <= 3 */ -#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 2 +#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 1 +/* PARSER_FINALLY_CONTEXT_STACK_ALLOCATION must be <= 3 */ +#define PARSER_FINALLY_CONTEXT_STACK_ALLOCATION 2 /* PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION must be <= 4 */ #define PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION 4 /* PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION must be <= 3 */ @@ -199,6 +201,12 @@ /* PARSER_BLOCK_CONTEXT_STACK_ALLOCATION must be <= 3 */ #define PARSER_BLOCK_CONTEXT_STACK_ALLOCATION 1 +/** + * Extra stack consumption for finally context. + */ +#define PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION \ + (PARSER_FINALLY_CONTEXT_STACK_ALLOCATION - PARSER_TRY_CONTEXT_STACK_ALLOCATION) + /** * Opcode definitions. */ @@ -552,7 +560,7 @@ VM_OC_CATCH) \ CBC_OPCODE (CBC_EXT_RESOLVE_BASE, CBC_NO_FLAG, 0, \ VM_OC_RESOLVE_BASE_FOR_CALL) \ - CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, 0, \ + CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION, \ VM_OC_FINALLY) \ CBC_OPCODE (CBC_EXT_INITIALIZER_PUSH_PROP, CBC_NO_FLAG, 0, \ VM_OC_INITIALIZER_PUSH_PROP | VM_OC_GET_STACK) \ diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index 48b69dbf5e..2ee4675777 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -1886,9 +1886,9 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */ if (try_statement.type == parser_finally_block) { parser_flush_cbc (context_p); - PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION); + PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION); #ifndef JERRY_NDEBUG - PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION); + PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION); #endif /* !JERRY_NDEBUG */ parser_emit_cbc (context_p, CBC_CONTEXT_END); @@ -1919,11 +1919,15 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */ try_statement.type = parser_finally_block; } } - else if (try_statement.type == parser_try_block - && context_p->token.type != LEXER_KEYW_CATCH - && context_p->token.type != LEXER_KEYW_FINALLY) + else { - parser_raise_error (context_p, PARSER_ERR_CATCH_FINALLY_EXPECTED); + JERRY_ASSERT (try_statement.type == parser_try_block); + + if (context_p->token.type != LEXER_KEYW_CATCH + && context_p->token.type != LEXER_KEYW_FINALLY) + { + parser_raise_error (context_p, PARSER_ERR_CATCH_FINALLY_EXPECTED); + } } } @@ -2029,6 +2033,10 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */ parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED); } +#ifndef JERRY_NDEBUG + PARSER_PLUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION); +#endif /* !JERRY_NDEBUG */ + try_statement.type = parser_finally_block; parser_emit_cbc_ext_forward_branch (context_p, CBC_EXT_FINALLY, diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 8babadceab..329ece3ecb 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -964,6 +964,11 @@ parser_post_processing (parser_context_t *context_p) /**< context */ PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION); #endif /* !JERRY_NDEBUG */ + if (context_p->stack_limit < PARSER_FINALLY_CONTEXT_STACK_ALLOCATION) + { + context_p->stack_limit = PARSER_FINALLY_CONTEXT_STACK_ALLOCATION; + } + parser_branch_t branch; parser_stack_pop (context_p, &branch, sizeof (parser_branch_t)); diff --git a/jerry-core/vm/vm-stack.c b/jerry-core/vm/vm-stack.c index d8dd6bbe4b..3840bb34e0 100644 --- a/jerry-core/vm/vm-stack.c +++ b/jerry-core/vm/vm-stack.c @@ -30,6 +30,9 @@ JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_BLOCK_CONTEXT_STACK_ALLOCATION, parser_with_context_stack_allocation_must_be_equal_to_parser_block_context_stack_allocation); +JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_TRY_CONTEXT_STACK_ALLOCATION, + parser_with_context_stack_allocation_must_be_equal_to_parser_block_context_stack_allocation); + /** * Abort (finalize) the current stack context, and remove it. * @@ -58,13 +61,13 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */ /* FALLTHRU */ } case VM_CONTEXT_FINALLY_JUMP: - case VM_CONTEXT_TRY: - case VM_CONTEXT_CATCH: { - VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION); - vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION; + VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION); + vm_stack_top_p -= PARSER_FINALLY_CONTEXT_STACK_ALLOCATION; break; } + case VM_CONTEXT_TRY: + case VM_CONTEXT_CATCH: #if ENABLED (JERRY_ESNEXT) case VM_CONTEXT_BLOCK: #endif /* ENABLED (JERRY_ESNEXT) */ @@ -243,6 +246,8 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */ } else { + JERRY_ASSERT (context_type == VM_CONTEXT_CATCH); + #if !ENABLED (JERRY_ESNEXT) if (vm_stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV) { @@ -263,6 +268,9 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */ JERRY_ASSERT (byte_code_p[0] == CBC_EXT_OPCODE); + VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION); + vm_stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION; + #if ENABLED (JERRY_ESNEXT) if (JERRY_UNLIKELY (byte_code_p[1] == CBC_EXT_ASYNC_EXIT)) { @@ -316,14 +324,14 @@ vm_get_context_value_offsets (ecma_value_t *context_item_p) /**< any item of a c case VM_CONTEXT_FINALLY_THROW: case VM_CONTEXT_FINALLY_RETURN: { - return (2 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_TRY_CONTEXT_STACK_ALLOCATION; + return (2 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_FINALLY_CONTEXT_STACK_ALLOCATION; } case VM_CONTEXT_FINALLY_JUMP: - case VM_CONTEXT_TRY: - case VM_CONTEXT_CATCH: { - return PARSER_TRY_CONTEXT_STACK_ALLOCATION; + return PARSER_FINALLY_CONTEXT_STACK_ALLOCATION; } + case VM_CONTEXT_TRY: + case VM_CONTEXT_CATCH: case VM_CONTEXT_BLOCK: case VM_CONTEXT_WITH: { diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index ce9c8ff619..f3cee79f69 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -2255,7 +2255,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } case VM_OC_ASYNC_EXIT: { - JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_TRY_CONTEXT_STACK_ALLOCATION); JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p); result = frame_ctx_p->block_result; @@ -2271,22 +2270,26 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ JERRY_CONTEXT (current_new_target) = old_new_target_p; } - left_value = stack_top_p[-2]; + vm_stack_context_type_t context_type = VM_GET_CONTEXT_TYPE (stack_top_p[-1]); - if (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_FINALLY_THROW) + if (context_type == VM_CONTEXT_TRY) { - ecma_reject_promise (result, left_value); + JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_TRY_CONTEXT_STACK_ALLOCATION); + left_value = ECMA_VALUE_UNDEFINED; } else { - JERRY_ASSERT (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_TRY - || VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_FINALLY_RETURN); - - if (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_TRY) - { - left_value = ECMA_VALUE_UNDEFINED; - } + JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_FINALLY_CONTEXT_STACK_ALLOCATION); + left_value = stack_top_p[-2]; + } + if (context_type == VM_CONTEXT_FINALLY_THROW) + { + ecma_reject_promise (result, left_value); + } + else + { + JERRY_ASSERT (context_type == VM_CONTEXT_TRY || context_type == VM_CONTEXT_FINALLY_RETURN); ecma_fulfill_promise (result, left_value); } @@ -3835,10 +3838,11 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL); frame_ctx_p->lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp); ecma_deref_object (lex_env_p); - - stack_top_p[-1] &= (ecma_value_t) ~VM_CONTEXT_HAS_LEX_ENV; } + VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION); + stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION; + stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_FINALLY_JUMP, branch_offset); stack_top_p[-2] = (ecma_value_t) branch_offset; continue; @@ -3869,8 +3873,8 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ #endif /* ENABLED (JERRY_ESNEXT) */ VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, - PARSER_TRY_CONTEXT_STACK_ALLOCATION); - stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION; + PARSER_FINALLY_CONTEXT_STACK_ALLOCATION); + stack_top_p -= PARSER_FINALLY_CONTEXT_STACK_ALLOCATION; if (context_type == VM_CONTEXT_FINALLY_RETURN) {