Skip to content

Commit 6cc8327

Browse files
committed
Parser improvements.
The number of delete opcodes is reduced to two from six. The range of numbers which can be included in the byte code is doubled from (-127,127) to (-256,256). JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent d953fbb commit 6cc8327

File tree

8 files changed

+79
-64
lines changed

8 files changed

+79
-64
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@
144144
*/
145145

146146
#define CBC_NO_RESULT_OPERATION(opcode) \
147-
((opcode) >= CBC_DELETE && (opcode) < CBC_END)
147+
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_END)
148148

149149
#define CBC_NO_RESULT_BLOCK(opcode) \
150-
((opcode) >= CBC_DELETE && (opcode) < CBC_ASSIGN_ADD)
150+
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_ASSIGN_ADD)
151151

152152
#define CBC_NO_RESULT_COMPOUND_ASSIGMENT(opcode) \
153153
((opcode) >= CBC_ASSIGN_ADD && (opcode) < CBC_END)
@@ -271,9 +271,11 @@
271271
CBC_OPCODE (CBC_PUSH_THIS_LITERAL, CBC_HAS_LITERAL_ARG, 2, \
272272
VM_OC_PUSH_TWO | VM_OC_GET_THIS_LITERAL) \
273273
CBC_OPCODE (CBC_PUSH_NUMBER_0, CBC_NO_FLAG, 1, \
274-
VM_OC_PUSH_NUMBER | VM_OC_PUT_STACK) \
275-
CBC_OPCODE (CBC_PUSH_NUMBER_1, CBC_HAS_BYTE_ARG, 1, \
276-
VM_OC_PUSH_NUMBER | VM_OC_PUT_STACK) \
274+
VM_OC_PUSH_NUMBER_0 | VM_OC_PUT_STACK) \
275+
CBC_OPCODE (CBC_PUSH_NUMBER_POS_BYTE, CBC_HAS_BYTE_ARG, 1, \
276+
VM_OC_PUSH_NUMBER_POS_BYTE | VM_OC_PUT_STACK) \
277+
CBC_OPCODE (CBC_PUSH_NUMBER_NEG_BYTE, CBC_HAS_BYTE_ARG, 1, \
278+
VM_OC_PUSH_NUMBER_NEG_BYTE | VM_OC_PUT_STACK) \
277279
CBC_OPCODE (CBC_PUSH_PROP, CBC_NO_FLAG, -1, \
278280
VM_OC_PROP_GET | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
279281
CBC_OPCODE (CBC_PUSH_PROP_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
@@ -376,18 +378,10 @@
376378
MOD) \
377379
\
378380
/* Unary lvalue opcodes. */ \
379-
CBC_OPCODE (CBC_DELETE, CBC_NO_FLAG, -2, \
380-
VM_OC_PROP_DELETE | VM_OC_GET_STACK_STACK) \
381381
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
382382
VM_OC_PROP_DELETE | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
383-
CBC_OPCODE (CBC_DELETE_BLOCK, CBC_NO_FLAG, -2, \
384-
VM_OC_PROP_DELETE | VM_OC_GET_STACK_STACK | VM_OC_PUT_BLOCK) \
385-
CBC_OPCODE (CBC_DELETE_IDENT, CBC_HAS_LITERAL_ARG, 0, \
386-
VM_OC_DELETE) \
387383
CBC_OPCODE (CBC_DELETE_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 1, \
388384
VM_OC_DELETE | VM_OC_PUT_STACK) \
389-
CBC_OPCODE (CBC_DELETE_IDENT_BLOCK, CBC_HAS_LITERAL_ARG, 0, \
390-
VM_OC_DELETE | VM_OC_PUT_BLOCK) \
391385
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_INCR, \
392386
PRE_INCR) \
393387
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_DECR, \
@@ -597,7 +591,7 @@
597591
#define CBC_MAXIMUM_SMALL_VALUE 510
598592
#define CBC_MAXIMUM_FULL_VALUE 32767
599593

600-
#define CBC_PUSH_NUMBER_1_RANGE_END 128
594+
#define CBC_PUSH_NUMBER_BYTE_RANGE_END 256
601595

602596
#define CBC_HIGHEST_BIT_MASK 0x80
603597
#define CBC_LOWER_SEVEN_BIT_MASK 0x7f

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ lexer_construct_number_object (parser_context_t *context_p, /**< context */
15531553

15541554
if (int_num == num)
15551555
{
1556-
if (int_num < CBC_PUSH_NUMBER_1_RANGE_END
1556+
if (int_num <= CBC_PUSH_NUMBER_BYTE_RANGE_END
15571557
&& (int_num != 0 || !is_negative_number))
15581558
{
15591559
context_p->lit_object.index = (uint16_t) int_num;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ typedef enum
185185
((((token_type) - LEXER_PLUS) * 2) + CBC_PLUS)
186186

187187
#define LEXER_UNARY_LVALUE_OP_TOKEN_TO_OPCODE(token_type) \
188-
((((token_type) - LEXER_KEYW_DELETE) * 6) + CBC_DELETE)
188+
((((token_type) - LEXER_INCREASE) * 6) + CBC_PRE_INCR)
189189

190190
#define LEXER_BINARY_OP_TOKEN_TO_OPCODE(token_type) \
191191
((cbc_opcode_t) ((((token_type) - LEXER_BIT_OR) * 3) + CBC_BIT_OR))

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
8888
if (PARSER_IS_PUSH_LITERAL (context_p->last_cbc_opcode)
8989
&& context_p->last_cbc.literal_type == LEXER_IDENT_LITERAL)
9090
{
91-
JERRY_ASSERT (CBC_SAME_ARGS (CBC_PUSH_LITERAL, opcode + CBC_UNARY_LVALUE_WITH_IDENT));
92-
9391
if (context_p->status_flags & PARSER_IS_STRICT)
9492
{
9593
if (context_p->last_cbc.literal_object_type != LEXER_LITERAL_OBJECT_ANY)
@@ -107,17 +105,41 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
107105
}
108106
parser_raise_error (context_p, error);
109107
}
110-
if (opcode == CBC_DELETE)
108+
if (opcode == CBC_DELETE_PUSH_RESULT)
111109
{
112110
parser_raise_error (context_p, PARSER_ERR_DELETE_IDENT_NOT_ALLOWED);
113111
}
114112
}
115113

116-
if (opcode == CBC_DELETE)
114+
if (opcode == CBC_DELETE_PUSH_RESULT)
117115
{
118116
context_p->status_flags |= PARSER_LEXICAL_ENV_NEEDED;
117+
118+
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
119+
{
120+
context_p->last_cbc_opcode = CBC_DELETE_IDENT_PUSH_RESULT;
121+
}
122+
else if (context_p->last_cbc_opcode == CBC_PUSH_TWO_LITERALS)
123+
{
124+
context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
125+
parser_emit_cbc_literal (context_p,
126+
CBC_DELETE_IDENT_PUSH_RESULT,
127+
context_p->last_cbc.value);
128+
}
129+
else
130+
{
131+
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_THREE_LITERALS);
132+
133+
context_p->last_cbc_opcode = CBC_PUSH_TWO_LITERALS;
134+
parser_emit_cbc_literal (context_p,
135+
CBC_DELETE_IDENT_PUSH_RESULT,
136+
context_p->last_cbc.third_literal_index);
137+
}
138+
return;
119139
}
120140

141+
JERRY_ASSERT (CBC_SAME_ARGS (CBC_PUSH_LITERAL, opcode + CBC_UNARY_LVALUE_WITH_IDENT));
142+
121143
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
122144
{
123145
context_p->last_cbc_opcode = (uint16_t) (opcode + CBC_UNARY_LVALUE_WITH_IDENT);
@@ -169,8 +191,8 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
169191
default:
170192
{
171193
/* Invalid LeftHandSide expression. */
172-
parser_emit_cbc_ext (context_p, (opcode == CBC_DELETE) ? CBC_EXT_PUSH_UNDEFINED_BASE
173-
: CBC_EXT_THROW_REFERENCE_ERROR);
194+
parser_emit_cbc_ext (context_p, (opcode == CBC_DELETE_PUSH_RESULT) ? CBC_EXT_PUSH_UNDEFINED_BASE
195+
: CBC_EXT_THROW_REFERENCE_ERROR);
174196
break;
175197
}
176198
}
@@ -505,7 +527,7 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
505527

506528
if (lexer_construct_number_object (context_p, PARSER_TRUE, is_negative_number))
507529
{
508-
JERRY_ASSERT (context_p->lit_object.index < CBC_PUSH_NUMBER_1_RANGE_END);
530+
JERRY_ASSERT (context_p->lit_object.index <= CBC_PUSH_NUMBER_BYTE_RANGE_END);
509531

510532
if (context_p->lit_object.index == 0)
511533
{
@@ -949,7 +971,14 @@ parser_process_unary_expression (parser_context_t *context_p) /**< context */
949971

950972
if (LEXER_IS_UNARY_LVALUE_OP_TOKEN (token))
951973
{
952-
token = (uint8_t) (LEXER_UNARY_LVALUE_OP_TOKEN_TO_OPCODE (token));
974+
if (token == LEXER_KEYW_DELETE)
975+
{
976+
token = CBC_DELETE_PUSH_RESULT;
977+
}
978+
else
979+
{
980+
token = (uint8_t) (LEXER_UNARY_LVALUE_OP_TOKEN_TO_OPCODE (token));
981+
}
953982
parser_emit_unary_lvalue_opcode (context_p, (cbc_opcode_t) token);
954983
}
955984
else

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,10 @@ parser_emit_cbc_push_number (parser_context_t *context_p, /**< context */
310310
parser_flush_cbc (context_p);
311311
}
312312

313-
JERRY_ASSERT (value < CBC_PUSH_NUMBER_1_RANGE_END);
314-
JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (cbc_flags[CBC_PUSH_NUMBER_1]) == 1);
313+
cbc_opcode_t opcode = is_negative_number ? CBC_PUSH_NUMBER_NEG_BYTE : CBC_PUSH_NUMBER_POS_BYTE;
314+
315+
JERRY_ASSERT (value > 0 && value <= CBC_PUSH_NUMBER_BYTE_RANGE_END);
316+
JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (cbc_flags[opcode]) == 1);
315317

316318
context_p->stack_depth++;
317319

@@ -325,18 +327,11 @@ parser_emit_cbc_push_number (parser_context_t *context_p, /**< context */
325327
real_value = -real_value;
326328
}
327329

328-
printf (" [%3d] %s number:%d\n", (int) context_p->stack_depth, cbc_names[CBC_PUSH_NUMBER_1], real_value);
330+
printf (" [%3d] %s number:%d\n", (int) context_p->stack_depth, cbc_names[opcode], real_value);
329331
}
330332
#endif /* PARSER_DUMP_BYTE_CODE */
331333

332-
if (is_negative_number)
333-
{
334-
PARSER_PLUS_EQUAL_U16 (value, CBC_PUSH_NUMBER_1_RANGE_END);
335-
}
336-
337-
parser_emit_two_bytes (context_p,
338-
CBC_PUSH_NUMBER_1,
339-
(uint8_t) value);
334+
parser_emit_two_bytes (context_p, opcode, (uint8_t) (value - 1));
340335

341336
context_p->byte_code_size += 2;
342337

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,16 +1135,17 @@ parse_print_final_cbc (ecma_compiled_code_t *compiled_code_p, /**< compiled code
11351135
continue;
11361136
}
11371137

1138-
if (opcode == CBC_PUSH_NUMBER_1)
1138+
if (opcode == CBC_PUSH_NUMBER_POS_BYTE)
11391139
{
11401140
int value = *byte_code_p++;
1141+
printf (" number:%d\n", value + 1);
1142+
continue;
1143+
}
11411144

1142-
if (value >= CBC_PUSH_NUMBER_1_RANGE_END)
1143-
{
1144-
value = -(value - CBC_PUSH_NUMBER_1_RANGE_END);
1145-
}
1146-
1147-
printf (" number:%d\n", value);
1145+
if (opcode == CBC_PUSH_NUMBER_NEG_BYTE)
1146+
{
1147+
int value = *byte_code_p++;
1148+
printf (" number:%d\n", -(value + 1));
11481149
continue;
11491150
}
11501151
}

jerry-core/vm/vm.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -910,28 +910,22 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
910910
result = ecma_copy_value (frame_ctx_p->this_binding);
911911
break;
912912
}
913-
case VM_OC_PUSH_NUMBER:
913+
case VM_OC_PUSH_NUMBER_0:
914914
{
915-
ecma_integer_value_t number;
916-
917-
if (opcode == CBC_PUSH_NUMBER_0)
918-
{
919-
number = 0;
920-
}
921-
else
922-
{
923-
number = *byte_code_p++;
924-
925-
JERRY_ASSERT (opcode == CBC_PUSH_NUMBER_1);
926-
927-
if (number >= CBC_PUSH_NUMBER_1_RANGE_END)
928-
{
929-
number = -(number - CBC_PUSH_NUMBER_1_RANGE_END);
930-
}
931-
}
932-
933-
result = ecma_make_integer_value (number);
934-
break;
915+
*stack_top_p++ = ecma_make_integer_value (0);
916+
continue;
917+
}
918+
case VM_OC_PUSH_NUMBER_POS_BYTE:
919+
{
920+
ecma_integer_value_t number = *byte_code_p++;
921+
*stack_top_p++ = ecma_make_integer_value (number + 1);
922+
continue;
923+
}
924+
case VM_OC_PUSH_NUMBER_NEG_BYTE:
925+
{
926+
ecma_integer_value_t number = *byte_code_p++;
927+
*stack_top_p++ = ecma_make_integer_value (-(number + 1));
928+
continue;
935929
}
936930
case VM_OC_PUSH_OBJECT:
937931
{

jerry-core/vm/vm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ typedef enum
115115
VM_OC_PUSH_FALSE, /**< push false value */
116116
VM_OC_PUSH_NULL, /**< push null value */
117117
VM_OC_PUSH_THIS, /**< push this */
118-
VM_OC_PUSH_NUMBER, /**< push number */
118+
VM_OC_PUSH_NUMBER_0, /**< push number zero */
119+
VM_OC_PUSH_NUMBER_POS_BYTE, /**< push number between 1 and 256 */
120+
VM_OC_PUSH_NUMBER_NEG_BYTE, /**< push number between -1 and -256 */
119121
VM_OC_PUSH_OBJECT, /**< push object */
120122
VM_OC_SET_PROPERTY, /**< set property */
121123
VM_OC_SET_GETTER, /**< set getter */

0 commit comments

Comments
 (0)