Skip to content

Commit 8e01014

Browse files
authored
Reduce try context stack consumption to 1 from 2. (#3898)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 5bcb784 commit 8e01014

File tree

5 files changed

+64
-31
lines changed

5 files changed

+64
-31
lines changed

jerry-core/parser/js/byte-code.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@
189189
/* Stack consumption of opcodes with context. */
190190

191191
/* PARSER_TRY_CONTEXT_STACK_ALLOCATION must be <= 3 */
192-
#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 2
192+
#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 1
193+
/* PARSER_FINALLY_CONTEXT_STACK_ALLOCATION must be <= 3 */
194+
#define PARSER_FINALLY_CONTEXT_STACK_ALLOCATION 2
193195
/* PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION must be <= 4 */
194196
#define PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION 4
195197
/* PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION must be <= 3 */
@@ -199,6 +201,12 @@
199201
/* PARSER_BLOCK_CONTEXT_STACK_ALLOCATION must be <= 3 */
200202
#define PARSER_BLOCK_CONTEXT_STACK_ALLOCATION 1
201203

204+
/**
205+
* Extra stack consumption for finally context.
206+
*/
207+
#define PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION \
208+
(PARSER_FINALLY_CONTEXT_STACK_ALLOCATION - PARSER_TRY_CONTEXT_STACK_ALLOCATION)
209+
202210
/**
203211
* Opcode definitions.
204212
*/
@@ -552,7 +560,7 @@
552560
VM_OC_CATCH) \
553561
CBC_OPCODE (CBC_EXT_RESOLVE_BASE, CBC_NO_FLAG, 0, \
554562
VM_OC_RESOLVE_BASE_FOR_CALL) \
555-
CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, 0, \
563+
CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION, \
556564
VM_OC_FINALLY) \
557565
CBC_OPCODE (CBC_EXT_INITIALIZER_PUSH_PROP, CBC_NO_FLAG, 0, \
558566
VM_OC_INITIALIZER_PUSH_PROP | VM_OC_GET_STACK) \

jerry-core/parser/js/js-parser-statm.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,9 +1886,9 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */
18861886
if (try_statement.type == parser_finally_block)
18871887
{
18881888
parser_flush_cbc (context_p);
1889-
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
1889+
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
18901890
#ifndef JERRY_NDEBUG
1891-
PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
1891+
PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
18921892
#endif /* !JERRY_NDEBUG */
18931893

18941894
parser_emit_cbc (context_p, CBC_CONTEXT_END);
@@ -1919,11 +1919,15 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */
19191919
try_statement.type = parser_finally_block;
19201920
}
19211921
}
1922-
else if (try_statement.type == parser_try_block
1923-
&& context_p->token.type != LEXER_KEYW_CATCH
1924-
&& context_p->token.type != LEXER_KEYW_FINALLY)
1922+
else
19251923
{
1926-
parser_raise_error (context_p, PARSER_ERR_CATCH_FINALLY_EXPECTED);
1924+
JERRY_ASSERT (try_statement.type == parser_try_block);
1925+
1926+
if (context_p->token.type != LEXER_KEYW_CATCH
1927+
&& context_p->token.type != LEXER_KEYW_FINALLY)
1928+
{
1929+
parser_raise_error (context_p, PARSER_ERR_CATCH_FINALLY_EXPECTED);
1930+
}
19271931
}
19281932
}
19291933

@@ -2029,6 +2033,10 @@ parser_parse_try_statement_end (parser_context_t *context_p) /**< context */
20292033
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
20302034
}
20312035

2036+
#ifndef JERRY_NDEBUG
2037+
PARSER_PLUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION);
2038+
#endif /* !JERRY_NDEBUG */
2039+
20322040
try_statement.type = parser_finally_block;
20332041
parser_emit_cbc_ext_forward_branch (context_p,
20342042
CBC_EXT_FINALLY,

jerry-core/parser/js/js-parser.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,11 @@ parser_post_processing (parser_context_t *context_p) /**< context */
964964
PARSER_MINUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
965965
#endif /* !JERRY_NDEBUG */
966966

967+
if (context_p->stack_limit < PARSER_FINALLY_CONTEXT_STACK_ALLOCATION)
968+
{
969+
context_p->stack_limit = PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
970+
}
971+
967972
parser_branch_t branch;
968973

969974
parser_stack_pop (context_p, &branch, sizeof (parser_branch_t));

jerry-core/vm/vm-stack.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_BLOCK_CONTEXT_STACK_ALLOCATION,
3131
parser_with_context_stack_allocation_must_be_equal_to_parser_block_context_stack_allocation);
3232

33+
JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_TRY_CONTEXT_STACK_ALLOCATION,
34+
parser_with_context_stack_allocation_must_be_equal_to_parser_block_context_stack_allocation);
35+
3336
/**
3437
* Abort (finalize) the current stack context, and remove it.
3538
*
@@ -58,13 +61,13 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
5861
/* FALLTHRU */
5962
}
6063
case VM_CONTEXT_FINALLY_JUMP:
61-
case VM_CONTEXT_TRY:
62-
case VM_CONTEXT_CATCH:
6364
{
64-
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
65-
vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
65+
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
66+
vm_stack_top_p -= PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
6667
break;
6768
}
69+
case VM_CONTEXT_TRY:
70+
case VM_CONTEXT_CATCH:
6871
#if ENABLED (JERRY_ESNEXT)
6972
case VM_CONTEXT_BLOCK:
7073
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -243,6 +246,8 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
243246
}
244247
else
245248
{
249+
JERRY_ASSERT (context_type == VM_CONTEXT_CATCH);
250+
246251
#if !ENABLED (JERRY_ESNEXT)
247252
if (vm_stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
248253
{
@@ -263,6 +268,9 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
263268

264269
JERRY_ASSERT (byte_code_p[0] == CBC_EXT_OPCODE);
265270

271+
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION);
272+
vm_stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION;
273+
266274
#if ENABLED (JERRY_ESNEXT)
267275
if (JERRY_UNLIKELY (byte_code_p[1] == CBC_EXT_ASYNC_EXIT))
268276
{
@@ -316,14 +324,14 @@ vm_get_context_value_offsets (ecma_value_t *context_item_p) /**< any item of a c
316324
case VM_CONTEXT_FINALLY_THROW:
317325
case VM_CONTEXT_FINALLY_RETURN:
318326
{
319-
return (2 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_TRY_CONTEXT_STACK_ALLOCATION;
327+
return (2 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
320328
}
321329
case VM_CONTEXT_FINALLY_JUMP:
322-
case VM_CONTEXT_TRY:
323-
case VM_CONTEXT_CATCH:
324330
{
325-
return PARSER_TRY_CONTEXT_STACK_ALLOCATION;
331+
return PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
326332
}
333+
case VM_CONTEXT_TRY:
334+
case VM_CONTEXT_CATCH:
327335
case VM_CONTEXT_BLOCK:
328336
case VM_CONTEXT_WITH:
329337
{

jerry-core/vm/vm.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
22552255
}
22562256
case VM_OC_ASYNC_EXIT:
22572257
{
2258-
JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_TRY_CONTEXT_STACK_ALLOCATION);
22592258
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
22602259

22612260
result = frame_ctx_p->block_result;
@@ -2271,22 +2270,26 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
22712270
JERRY_CONTEXT (current_new_target) = old_new_target_p;
22722271
}
22732272

2274-
left_value = stack_top_p[-2];
2273+
vm_stack_context_type_t context_type = VM_GET_CONTEXT_TYPE (stack_top_p[-1]);
22752274

2276-
if (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_FINALLY_THROW)
2275+
if (context_type == VM_CONTEXT_TRY)
22772276
{
2278-
ecma_reject_promise (result, left_value);
2277+
JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_TRY_CONTEXT_STACK_ALLOCATION);
2278+
left_value = ECMA_VALUE_UNDEFINED;
22792279
}
22802280
else
22812281
{
2282-
JERRY_ASSERT (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_TRY
2283-
|| VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_FINALLY_RETURN);
2284-
2285-
if (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_TRY)
2286-
{
2287-
left_value = ECMA_VALUE_UNDEFINED;
2288-
}
2282+
JERRY_ASSERT (frame_ctx_p->context_depth == PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
2283+
left_value = stack_top_p[-2];
2284+
}
22892285

2286+
if (context_type == VM_CONTEXT_FINALLY_THROW)
2287+
{
2288+
ecma_reject_promise (result, left_value);
2289+
}
2290+
else
2291+
{
2292+
JERRY_ASSERT (context_type == VM_CONTEXT_TRY || context_type == VM_CONTEXT_FINALLY_RETURN);
22902293
ecma_fulfill_promise (result, left_value);
22912294
}
22922295

@@ -3835,10 +3838,11 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
38353838
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL);
38363839
frame_ctx_p->lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp);
38373840
ecma_deref_object (lex_env_p);
3838-
3839-
stack_top_p[-1] &= (ecma_value_t) ~VM_CONTEXT_HAS_LEX_ENV;
38403841
}
38413842

3843+
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION);
3844+
stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION;
3845+
38423846
stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_FINALLY_JUMP, branch_offset);
38433847
stack_top_p[-2] = (ecma_value_t) branch_offset;
38443848
continue;
@@ -3869,8 +3873,8 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
38693873
#endif /* ENABLED (JERRY_ESNEXT) */
38703874

38713875
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth,
3872-
PARSER_TRY_CONTEXT_STACK_ALLOCATION);
3873-
stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
3876+
PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
3877+
stack_top_p -= PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
38743878

38753879
if (context_type == VM_CONTEXT_FINALLY_RETURN)
38763880
{

0 commit comments

Comments
 (0)