From 4be9ffda842c3d4d3810f107f38f5dc9a2f34da5 Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Fri, 17 Apr 2020 13:05:07 +0200 Subject: [PATCH] Use symbolic constants for code size checks. (#3682) JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/js-parser-internal.h | 12 ++++++ jerry-core/parser/js/js-parser-util.c | 47 +++++++++-------------- jerry-core/parser/js/js-parser.c | 15 ++------ 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index 16c13f406..28aa0deb9 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -124,6 +124,18 @@ typedef enum */ #define PARSER_FUNCTION_CLOSURE (PARSER_IS_FUNCTION | PARSER_IS_CLOSURE) +#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX +/** + * Maximum number of bytes for branch target. + */ +#define PARSER_MAX_BRANCH_LENGTH 2 +#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ +/** + * Maximum number of bytes for branch target. + */ +#define PARSER_MAX_BRANCH_LENGTH 3 +#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */ + #if ENABLED (JERRY_ES2015) /** * Offset between PARSER_CLASS_CONSTRUCTOR and ECMA_PARSE_CLASS_CONSTRUCTOR diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index 352656e5e..09140e0ba 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -566,11 +566,7 @@ parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */ } #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 - opcode++; -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ - PARSER_PLUS_EQUAL_U16 (opcode, 2); -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ + PARSER_PLUS_EQUAL_U16 (opcode, PARSER_MAX_BRANCH_LENGTH - 1); parser_emit_two_bytes (context_p, (uint8_t) opcode, 0); branch_p->page_p = context_p->byte_code.last_p; @@ -578,13 +574,13 @@ parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */ context_p->byte_code_size += extra_byte_code_increase; -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 +#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX PARSER_APPEND_TO_BYTE_CODE (context_p, 0); - context_p->byte_code_size += 3; -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ +#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ parser_emit_two_bytes (context_p, 0, 0); - context_p->byte_code_size += 4; -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ +#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */ + + context_p->byte_code_size += PARSER_MAX_BRANCH_LENGTH + 1; if (context_p->stack_depth > context_p->stack_limit) { @@ -681,35 +677,30 @@ parser_emit_cbc_backward_branch (parser_context_t *context_p, /**< context */ #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ context_p->byte_code_size += 2; -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 - if (offset > 255) +#if PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX + if (offset > UINT16_MAX) { opcode++; context_p->byte_code_size++; } -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ - if (offset > 65535) - { - PARSER_PLUS_EQUAL_U16 (opcode, 2); - context_p->byte_code_size += 2; - } - else if (offset > 255) +#endif /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ + + if (offset > UINT8_MAX) { opcode++; context_p->byte_code_size++; } -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ PARSER_APPEND_TO_BYTE_CODE (context_p, (uint8_t) opcode); -#if PARSER_MAXIMUM_CODE_SIZE > 65535 - if (offset > 65535) +#if PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX + if (offset > UINT16_MAX) { PARSER_APPEND_TO_BYTE_CODE (context_p, offset >> 16); } -#endif /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ +#endif /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ - if (offset > 255) + if (offset > UINT8_MAX) { PARSER_APPEND_TO_BYTE_CODE (context_p, (offset >> 8) & 0xff); } @@ -745,14 +736,14 @@ parser_set_branch_to_current_position (parser_context_t *context_p, /**< context JERRY_ASSERT (delta <= PARSER_MAXIMUM_CODE_SIZE); -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 +#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX page_p->bytes[offset++] = (uint8_t) (delta >> 8); if (offset >= PARSER_CBC_STREAM_PAGE_SIZE) { page_p = page_p->next_p; offset = 0; } -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ +#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ page_p->bytes[offset++] = (uint8_t) (delta >> 16); if (offset >= PARSER_CBC_STREAM_PAGE_SIZE) { @@ -765,8 +756,8 @@ parser_set_branch_to_current_position (parser_context_t *context_p, /**< context page_p = page_p->next_p; offset = 0; } -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ - page_p->bytes[offset++] = delta & 0xff; +#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */ + page_p->bytes[offset] = delta & 0xff; } /* parser_set_branch_to_current_position */ /** diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 0b55643c5..8206dd87b 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1087,11 +1087,6 @@ parser_post_processing (parser_context_t *context_p) /**< context */ if (flags & CBC_HAS_BRANCH_ARG) { bool prefix_zero = true; -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 - cbc_opcode_t jump_forward = CBC_JUMP_FORWARD_2; -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ - cbc_opcode_t jump_forward = CBC_JUMP_FORWARD_3; -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ /* The leading zeroes are dropped from the stream. * Although dropping these zeroes for backward @@ -1114,9 +1109,9 @@ parser_post_processing (parser_context_t *context_p) /**< context */ PARSER_NEXT_BYTE (page_p, offset); } - if (last_opcode == jump_forward + if (last_opcode == (cbc_opcode_t) (CBC_JUMP_FORWARD + PARSER_MAX_BRANCH_LENGTH - 1) && prefix_zero - && page_p->bytes[offset] == CBC_BRANCH_OFFSET_LENGTH (jump_forward) + 1) + && page_p->bytes[offset] == PARSER_MAX_BRANCH_LENGTH + 1) { /* Uncoditional jumps which jump right after the instruction * are effectively NOPs. These jumps are removed from the @@ -1345,11 +1340,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */ if (opcode == CBC_JUMP_FORWARD) { /* These opcodes are deleted from the stream. */ -#if PARSER_MAXIMUM_CODE_SIZE <= 65535 - size_t counter = 3; -#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */ - size_t counter = 4; -#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */ + size_t counter = PARSER_MAX_BRANCH_LENGTH + 1; do {