Skip to content

Commit 4be9ffd

Browse files
authored
Use symbolic constants for code size checks. (jerryscript-project#3682)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 895973c commit 4be9ffd

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

jerry-core/parser/js/js-parser-internal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ typedef enum
124124
*/
125125
#define PARSER_FUNCTION_CLOSURE (PARSER_IS_FUNCTION | PARSER_IS_CLOSURE)
126126

127+
#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX
128+
/**
129+
* Maximum number of bytes for branch target.
130+
*/
131+
#define PARSER_MAX_BRANCH_LENGTH 2
132+
#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */
133+
/**
134+
* Maximum number of bytes for branch target.
135+
*/
136+
#define PARSER_MAX_BRANCH_LENGTH 3
137+
#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */
138+
127139
#if ENABLED (JERRY_ES2015)
128140
/**
129141
* Offset between PARSER_CLASS_CONSTRUCTOR and ECMA_PARSE_CLASS_CONSTRUCTOR

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

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -566,25 +566,21 @@ parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */
566566
}
567567
#endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
568568

569-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
570-
opcode++;
571-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
572-
PARSER_PLUS_EQUAL_U16 (opcode, 2);
573-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
569+
PARSER_PLUS_EQUAL_U16 (opcode, PARSER_MAX_BRANCH_LENGTH - 1);
574570

575571
parser_emit_two_bytes (context_p, (uint8_t) opcode, 0);
576572
branch_p->page_p = context_p->byte_code.last_p;
577573
branch_p->offset = (context_p->byte_code.last_position - 1) | (context_p->byte_code_size << 8);
578574

579575
context_p->byte_code_size += extra_byte_code_increase;
580576

581-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
577+
#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX
582578
PARSER_APPEND_TO_BYTE_CODE (context_p, 0);
583-
context_p->byte_code_size += 3;
584-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
579+
#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */
585580
parser_emit_two_bytes (context_p, 0, 0);
586-
context_p->byte_code_size += 4;
587-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
581+
#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */
582+
583+
context_p->byte_code_size += PARSER_MAX_BRANCH_LENGTH + 1;
588584

589585
if (context_p->stack_depth > context_p->stack_limit)
590586
{
@@ -681,35 +677,30 @@ parser_emit_cbc_backward_branch (parser_context_t *context_p, /**< context */
681677
#endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
682678

683679
context_p->byte_code_size += 2;
684-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
685-
if (offset > 255)
680+
#if PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX
681+
if (offset > UINT16_MAX)
686682
{
687683
opcode++;
688684
context_p->byte_code_size++;
689685
}
690-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
691-
if (offset > 65535)
692-
{
693-
PARSER_PLUS_EQUAL_U16 (opcode, 2);
694-
context_p->byte_code_size += 2;
695-
}
696-
else if (offset > 255)
686+
#endif /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */
687+
688+
if (offset > UINT8_MAX)
697689
{
698690
opcode++;
699691
context_p->byte_code_size++;
700692
}
701-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
702693

703694
PARSER_APPEND_TO_BYTE_CODE (context_p, (uint8_t) opcode);
704695

705-
#if PARSER_MAXIMUM_CODE_SIZE > 65535
706-
if (offset > 65535)
696+
#if PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX
697+
if (offset > UINT16_MAX)
707698
{
708699
PARSER_APPEND_TO_BYTE_CODE (context_p, offset >> 16);
709700
}
710-
#endif /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
701+
#endif /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */
711702

712-
if (offset > 255)
703+
if (offset > UINT8_MAX)
713704
{
714705
PARSER_APPEND_TO_BYTE_CODE (context_p, (offset >> 8) & 0xff);
715706
}
@@ -745,14 +736,14 @@ parser_set_branch_to_current_position (parser_context_t *context_p, /**< context
745736

746737
JERRY_ASSERT (delta <= PARSER_MAXIMUM_CODE_SIZE);
747738

748-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
739+
#if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX
749740
page_p->bytes[offset++] = (uint8_t) (delta >> 8);
750741
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
751742
{
752743
page_p = page_p->next_p;
753744
offset = 0;
754745
}
755-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
746+
#else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */
756747
page_p->bytes[offset++] = (uint8_t) (delta >> 16);
757748
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
758749
{
@@ -765,8 +756,8 @@ parser_set_branch_to_current_position (parser_context_t *context_p, /**< context
765756
page_p = page_p->next_p;
766757
offset = 0;
767758
}
768-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
769-
page_p->bytes[offset++] = delta & 0xff;
759+
#endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */
760+
page_p->bytes[offset] = delta & 0xff;
770761
} /* parser_set_branch_to_current_position */
771762

772763
/**

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,11 +1087,6 @@ parser_post_processing (parser_context_t *context_p) /**< context */
10871087
if (flags & CBC_HAS_BRANCH_ARG)
10881088
{
10891089
bool prefix_zero = true;
1090-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
1091-
cbc_opcode_t jump_forward = CBC_JUMP_FORWARD_2;
1092-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
1093-
cbc_opcode_t jump_forward = CBC_JUMP_FORWARD_3;
1094-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
10951090

10961091
/* The leading zeroes are dropped from the stream.
10971092
* Although dropping these zeroes for backward
@@ -1114,9 +1109,9 @@ parser_post_processing (parser_context_t *context_p) /**< context */
11141109
PARSER_NEXT_BYTE (page_p, offset);
11151110
}
11161111

1117-
if (last_opcode == jump_forward
1112+
if (last_opcode == (cbc_opcode_t) (CBC_JUMP_FORWARD + PARSER_MAX_BRANCH_LENGTH - 1)
11181113
&& prefix_zero
1119-
&& page_p->bytes[offset] == CBC_BRANCH_OFFSET_LENGTH (jump_forward) + 1)
1114+
&& page_p->bytes[offset] == PARSER_MAX_BRANCH_LENGTH + 1)
11201115
{
11211116
/* Uncoditional jumps which jump right after the instruction
11221117
* are effectively NOPs. These jumps are removed from the
@@ -1345,11 +1340,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
13451340
if (opcode == CBC_JUMP_FORWARD)
13461341
{
13471342
/* These opcodes are deleted from the stream. */
1348-
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
1349-
size_t counter = 3;
1350-
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
1351-
size_t counter = 4;
1352-
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
1343+
size_t counter = PARSER_MAX_BRANCH_LENGTH + 1;
13531344

13541345
do
13551346
{

0 commit comments

Comments
 (0)