aboutsummaryrefslogtreecommitdiff
path: root/jerry-core/parser/js/js-parser-util.c
blob: 2aadd2744bf70cc87920ea1f0daa4bf4e8866329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "js-parser-internal.h"

#ifndef JERRY_DISABLE_JS_PARSER

#ifdef JERRY_ENABLE_LINE_INFO
#include "jcontext.h"
#endif /* JERRY_ENABLE_LINE_INFO */

/** \addtogroup parser Parser
 * @{
 *
 * \addtogroup jsparser JavaScript
 * @{
 *
 * \addtogroup jsparser_utils Utility
 * @{
 */

/**********************************************************************/
/* Emitting byte codes                                                */
/**********************************************************************/

/**
 * Append two bytes to the cbc stream.
 */
static void
parser_emit_two_bytes (parser_context_t *context_p, /**< context */
                       uint8_t first_byte, /**< first byte */
                       uint8_t second_byte) /**< second byte */
{
  uint32_t last_position = context_p->byte_code.last_position;

  if (last_position + 2 <= PARSER_CBC_STREAM_PAGE_SIZE)
  {
    parser_mem_page_t *page_p = context_p->byte_code.last_p;

    page_p->bytes[last_position] = first_byte;
    page_p->bytes[last_position + 1] = second_byte;
    context_p->byte_code.last_position = last_position + 2;
  }
  else if (last_position >= PARSER_CBC_STREAM_PAGE_SIZE)
  {
    parser_mem_page_t *page_p;

    parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
    page_p = context_p->byte_code.last_p;
    page_p->bytes[0] = first_byte;
    page_p->bytes[1] = second_byte;
    context_p->byte_code.last_position = 2;
  }
  else
  {
    context_p->byte_code.last_p->bytes[PARSER_CBC_STREAM_PAGE_SIZE - 1] = first_byte;
    parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
    context_p->byte_code.last_p->bytes[0] = second_byte;
    context_p->byte_code.last_position = 1;
  }
} /* parser_emit_two_bytes */

/**
 * Append byte to the end of the current byte code stream.
 *
 * @param context_p parser context
 * @param byte byte
 */
#define PARSER_APPEND_TO_BYTE_CODE(context_p, byte) \
  if ((context_p)->byte_code.last_position >= PARSER_CBC_STREAM_PAGE_SIZE) \
  { \
    parser_cbc_stream_alloc_page ((context_p), &(context_p)->byte_code); \
  } \
  (context_p)->byte_code.last_p->bytes[(context_p)->byte_code.last_position++] = (uint8_t) (byte)

/**
 * Append the current byte code to the stream
 */
void
parser_flush_cbc (parser_context_t *context_p) /**< context */
{
  uint8_t flags;
  uint16_t last_opcode = context_p->last_cbc_opcode;

  if (last_opcode == PARSER_CBC_UNAVAILABLE)
  {
    return;
  }

  context_p->status_flags |= PARSER_NO_END_LABEL;

  if (PARSER_IS_BASIC_OPCODE (last_opcode))
  {
    cbc_opcode_t opcode = (cbc_opcode_t) last_opcode;

    JERRY_ASSERT (opcode < CBC_END);
    flags = cbc_flags[opcode];

    PARSER_APPEND_TO_BYTE_CODE (context_p, opcode);
    context_p->byte_code_size++;
  }
  else
  {
    cbc_ext_opcode_t opcode = (cbc_ext_opcode_t) PARSER_GET_EXT_OPCODE (last_opcode);

    JERRY_ASSERT (opcode < CBC_EXT_END);
    flags = cbc_ext_flags[opcode];
    parser_emit_two_bytes (context_p, CBC_EXT_OPCODE, opcode);
    context_p->byte_code_size += 2;
  }

  JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
                 || (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
  PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));

  if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
  {
    uint16_t literal_index = context_p->last_cbc.literal_index;

    parser_emit_two_bytes (context_p,
                           (uint8_t) (literal_index & 0xff),
                           (uint8_t) (literal_index >> 8));
    context_p->byte_code_size += 2;
  }

  if (flags & CBC_HAS_LITERAL_ARG2)
  {
    uint16_t literal_index = context_p->last_cbc.value;

    parser_emit_two_bytes (context_p,
                           (uint8_t) (literal_index & 0xff),
                           (uint8_t) (literal_index >> 8));
    context_p->byte_code_size += 2;

    if (!(flags & CBC_HAS_LITERAL_ARG))
    {
      literal_index = context_p->last_cbc.third_literal_index;

      parser_emit_two_bytes (context_p,
                             (uint8_t) (literal_index & 0xff),
                             (uint8_t) (literal_index >> 8));
      context_p->byte_code_size += 2;
    }
  }

  if (flags & CBC_HAS_BYTE_ARG)
  {
    uint8_t byte_argument = (uint8_t) context_p->last_cbc.value;

    JERRY_ASSERT (context_p->last_cbc.value <= CBC_MAXIMUM_BYTE_VALUE);

    if (flags & CBC_POP_STACK_BYTE_ARG)
    {
      JERRY_ASSERT (context_p->stack_depth >= byte_argument);
      PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, byte_argument);
    }

    PARSER_APPEND_TO_BYTE_CODE (context_p, byte_argument);
    context_p->byte_code_size++;
  }

#ifdef PARSER_DUMP_BYTE_CODE
  if (context_p->is_show_opcodes)
  {
    JERRY_DEBUG_MSG ("  [%3d] %s",
                     (int) context_p->stack_depth,
                     PARSER_IS_BASIC_OPCODE (last_opcode) ? cbc_names[last_opcode]
                                                          : cbc_ext_names[PARSER_GET_EXT_OPCODE (last_opcode)]);

    if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
    {
      uint16_t literal_index = context_p->last_cbc.literal_index;
      lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
      JERRY_DEBUG_MSG (" idx:%d->", literal_index);
      util_print_literal (literal_p);
    }

    if (flags & CBC_HAS_LITERAL_ARG2)
    {
      uint16_t literal_index = context_p->last_cbc.value;
      lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
      JERRY_DEBUG_MSG (" idx:%d->", literal_index);
      util_print_literal (literal_p);

      if (!(flags & CBC_HAS_LITERAL_ARG))
      {
        literal_index = context_p->last_cbc.third_literal_index;

        literal_p = PARSER_GET_LITERAL (literal_index);
        JERRY_DEBUG_MSG (" idx:%d->", literal_index);
        util_print_literal (literal_p);
      }
    }

    if (flags & CBC_HAS_BYTE_ARG)
    {
      if ((last_opcode == CBC_PUSH_NUMBER_POS_BYTE)
          || (last_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_POS_BYTE)))
      {
        JERRY_DEBUG_MSG (" number:%d", (int) context_p->last_cbc.value + 1);
      }
      else if ((last_opcode == CBC_PUSH_NUMBER_NEG_BYTE)
               || (last_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_NEG_BYTE)))
      {
        JERRY_DEBUG_MSG (" number:%d", -((int) context_p->last_cbc.value + 1));
      }
      else
      {
        JERRY_DEBUG_MSG (" byte_arg:%d", (int) context_p->last_cbc.value);
      }
    }

    JERRY_DEBUG_MSG ("\n");
  }
#endif /* PARSER_DUMP_BYTE_CODE */

  if (context_p->stack_depth > context_p->stack_limit)
  {
    context_p->stack_limit = context_p->stack_depth;
    if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
    {
      parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
    }
  }

  context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
} /* parser_flush_cbc */

/**
 * Append a byte code
 */
void
parser_emit_cbc (parser_context_t *context_p, /**< context */
                 uint16_t opcode) /**< opcode */
{
  JERRY_ASSERT (PARSER_ARGS_EQ (opcode, 0));

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->last_cbc_opcode = opcode;
} /* parser_emit_cbc */

/**
 * Append a byte code with a literal argument
 */
void
parser_emit_cbc_literal (parser_context_t *context_p, /**< context */
                         uint16_t opcode, /**< opcode */
                         uint16_t literal_index) /**< literal index */
{
  JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->last_cbc_opcode = opcode;
  context_p->last_cbc.literal_index = literal_index;
  context_p->last_cbc.literal_type = LEXER_UNUSED_LITERAL;
  context_p->last_cbc.literal_object_type = LEXER_LITERAL_OBJECT_ANY;
} /* parser_emit_cbc_literal */

/**
 * Append a byte code with the current literal argument
 */
void
parser_emit_cbc_literal_from_token (parser_context_t *context_p, /**< context */
                                    uint16_t opcode) /**< opcode */
{
  JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->last_cbc_opcode = opcode;
  context_p->last_cbc.literal_index = context_p->lit_object.index;
  context_p->last_cbc.literal_type = context_p->token.lit_location.type;
  context_p->last_cbc.literal_object_type = context_p->lit_object.type;
} /* parser_emit_cbc_literal_from_token */

/**
 * Append a byte code with a call argument
 */
void
parser_emit_cbc_call (parser_context_t *context_p, /**< context */
                      uint16_t opcode, /**< opcode */
                      size_t call_arguments) /**< number of arguments */
{
  JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_BYTE_ARG));
  JERRY_ASSERT (call_arguments <= CBC_MAXIMUM_BYTE_VALUE);

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->last_cbc_opcode = opcode;
  context_p->last_cbc.value = (uint16_t) call_arguments;
} /* parser_emit_cbc_call */

/**
 * Append a push number 1/2 byte code
 */
void
parser_emit_cbc_push_number (parser_context_t *context_p, /**< context */
                             bool is_negative_number) /**< sign is negative */
{
  uint16_t value = context_p->lit_object.index;
  uint16_t lit_value = UINT16_MAX;

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
    {
      lit_value = context_p->last_cbc.literal_index;
    }
    else
    {
      if (context_p->last_cbc_opcode == CBC_PUSH_TWO_LITERALS)
      {
        context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
        lit_value = context_p->last_cbc.value;
      }
      else if (context_p->last_cbc_opcode == CBC_PUSH_THREE_LITERALS)
      {
        context_p->last_cbc_opcode = CBC_PUSH_TWO_LITERALS;
        lit_value = context_p->last_cbc.third_literal_index;
      }

      parser_flush_cbc (context_p);
    }
  }

  if (value == 0)
  {
    if (lit_value == UINT16_MAX)
    {
      context_p->last_cbc_opcode = CBC_PUSH_NUMBER_0;
      return;
    }

    context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_0);
    context_p->last_cbc.literal_index = lit_value;
    return;
  }

  uint16_t opcode;

  if (lit_value == UINT16_MAX)
  {
    opcode = (is_negative_number ? CBC_PUSH_NUMBER_NEG_BYTE
                                 : CBC_PUSH_NUMBER_POS_BYTE);

    JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (PARSER_GET_FLAGS (opcode)) == 1);
  }
  else
  {
    opcode = PARSER_TO_EXT_OPCODE (is_negative_number ? CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_NEG_BYTE
                                                      : CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_POS_BYTE);
    JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (PARSER_GET_FLAGS (opcode)) == 2);

    context_p->last_cbc.literal_index = lit_value;
  }

  JERRY_ASSERT (value > 0 && value <= CBC_PUSH_NUMBER_BYTE_RANGE_END);

  context_p->last_cbc_opcode = opcode;
  context_p->last_cbc.value = (uint16_t) (value - 1);
} /* parser_emit_cbc_push_number */

#ifdef JERRY_ENABLE_LINE_INFO

/**
 * Append a line info data
 */
void
parser_emit_line_info (parser_context_t *context_p, /**< context */
                       uint32_t line, /**< current line */
                       bool flush_cbc) /**< flush last byte code */
{
  if (JERRY_CONTEXT (resource_name) == ECMA_VALUE_UNDEFINED)
  {
    return;
  }

  if (flush_cbc && context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

#ifdef PARSER_DUMP_BYTE_CODE
  if (context_p->is_show_opcodes)
  {
    JERRY_DEBUG_MSG ("  [%3d] CBC_EXT_LINE %d\n", (int) context_p->stack_depth, line);
  }
#endif /* PARSER_DUMP_BYTE_CODE */

  parser_emit_two_bytes (context_p, CBC_EXT_OPCODE, CBC_EXT_LINE);
  context_p->byte_code_size += 2;

  context_p->last_line_info_line = line;

  const uint32_t max_shift_plus_7 = 7 * 5;
  uint32_t shift = 7;

  while (shift < max_shift_plus_7 && (line >> shift) > 0)
  {
    shift += 7;
  }

  do
  {
    shift -= 7;

    uint8_t byte = (uint8_t) ((line >> shift) & CBC_LOWER_SEVEN_BIT_MASK);

    if (shift > 0)
    {
      byte = (uint8_t) (byte | CBC_HIGHEST_BIT_MASK);
    }

    PARSER_APPEND_TO_BYTE_CODE (context_p, byte);
    context_p->byte_code_size++;
  }
  while (shift > 0);
} /* parser_emit_line_info */

#endif /* JERRY_ENABLE_LINE_INFO */

/**
 * Append a byte code with a branch argument
 */
void
parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */
                                uint16_t opcode, /**< opcode */
                                parser_branch_t *branch_p) /**< branch result */
{
  uint8_t flags;
  uint32_t extra_byte_code_increase;

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->status_flags |= PARSER_NO_END_LABEL;

  if (PARSER_IS_BASIC_OPCODE (opcode))
  {
    JERRY_ASSERT (opcode < CBC_END);
    flags = cbc_flags[opcode];
    extra_byte_code_increase = 0;
  }
  else
  {
    PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
    opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);

    JERRY_ASSERT (opcode < CBC_EXT_END);
    flags = cbc_ext_flags[opcode];
    extra_byte_code_increase = 1;
  }

  JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
  JERRY_ASSERT (CBC_BRANCH_IS_FORWARD (flags));
  JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);

  /* Branch opcodes never push anything onto the stack. */
  JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
                 || (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
  PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));

#ifdef PARSER_DUMP_BYTE_CODE
  if (context_p->is_show_opcodes)
  {
    JERRY_DEBUG_MSG ("  [%3d] %s\n",
                     (int) context_p->stack_depth,
                     extra_byte_code_increase == 0 ? cbc_names[opcode] : cbc_ext_names[opcode]);
  }
#endif /* 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_emit_two_bytes (context_p, (uint8_t) opcode, 0);
  branch_p->page_p = context_p->byte_code.last_p;
  branch_p->offset = (context_p->byte_code.last_position - 1) | (context_p->byte_code_size << 8);

  context_p->byte_code_size += extra_byte_code_increase;

#if PARSER_MAXIMUM_CODE_SIZE <= 65535
  PARSER_APPEND_TO_BYTE_CODE (context_p, 0);
  context_p->byte_code_size += 3;
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
  parser_emit_two_bytes (context_p, 0, 0);
  context_p->byte_code_size += 4;
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */

  if (context_p->stack_depth > context_p->stack_limit)
  {
    context_p->stack_limit = context_p->stack_depth;
    if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
    {
      parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
    }
  }
} /* parser_emit_cbc_forward_branch */

/**
 * Append a branch byte code and create an item.
 *
 * @return newly created parser branch node
 */
parser_branch_node_t *
parser_emit_cbc_forward_branch_item (parser_context_t *context_p, /**< context */
                                     uint16_t opcode, /**< opcode */
                                     parser_branch_node_t *next_p) /**< next branch */
{
  parser_branch_t branch;
  parser_branch_node_t *new_item;

  /* Since byte code insertion may throw an out-of-memory error,
   * the branch is constructed locally, and copied later. */
  parser_emit_cbc_forward_branch (context_p, opcode, &branch);

  new_item = (parser_branch_node_t *) parser_malloc (context_p, sizeof (parser_branch_node_t));
  new_item->branch = branch;
  new_item->next_p = next_p;
  return new_item;
} /* parser_emit_cbc_forward_branch_item */

/**
 * Append a byte code with a branch argument
 */
void
parser_emit_cbc_backward_branch (parser_context_t *context_p, /**< context */
                                 uint16_t opcode, /**< opcode */
                                 uint32_t offset) /**< destination offset */
{
  uint8_t flags;
#ifdef PARSER_DUMP_BYTE_CODE
  const char *name;
#endif /* PARSER_DUMP_BYTE_CODE */

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->status_flags |= PARSER_NO_END_LABEL;
  offset = context_p->byte_code_size - offset;

  if (PARSER_IS_BASIC_OPCODE (opcode))
  {
    JERRY_ASSERT (opcode < CBC_END);
    flags = cbc_flags[opcode];

#ifdef PARSER_DUMP_BYTE_CODE
    name = cbc_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
  }
  else
  {
    PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
    opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);

    JERRY_ASSERT (opcode < CBC_EXT_END);
    flags = cbc_ext_flags[opcode];
    context_p->byte_code_size++;

#ifdef PARSER_DUMP_BYTE_CODE
    name = cbc_ext_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
  }

  JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
  JERRY_ASSERT (CBC_BRANCH_IS_BACKWARD (flags));
  JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);
  JERRY_ASSERT (offset <= context_p->byte_code_size);

  /* Branch opcodes never push anything onto the stack. */
  JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
                 || (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
  PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));

#ifdef PARSER_DUMP_BYTE_CODE
  if (context_p->is_show_opcodes)
  {
    JERRY_DEBUG_MSG ("  [%3d] %s\n", (int) context_p->stack_depth, name);
  }
#endif /* PARSER_DUMP_BYTE_CODE */

  context_p->byte_code_size += 2;
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
  if (offset > 255)
  {
    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)
  {
    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)
  {
    PARSER_APPEND_TO_BYTE_CODE (context_p, offset >> 16);
  }
#endif /* PARSER_MAXIMUM_CODE_SIZE > 65535 */

  if (offset > 255)
  {
    PARSER_APPEND_TO_BYTE_CODE (context_p, (offset >> 8) & 0xff);
  }

  PARSER_APPEND_TO_BYTE_CODE (context_p, offset & 0xff);
} /* parser_emit_cbc_backward_branch */

#undef PARSER_CHECK_LAST_POSITION
#undef PARSER_APPEND_TO_BYTE_CODE

/**
 * Set a branch to the current byte code position
 */
void
parser_set_branch_to_current_position (parser_context_t *context_p, /**< context */
                                       parser_branch_t *branch_p) /**< branch result */
{
  uint32_t delta;
  size_t offset;
  parser_mem_page_t *page_p = branch_p->page_p;

  if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
  {
    parser_flush_cbc (context_p);
  }

  context_p->status_flags &= (uint32_t) ~PARSER_NO_END_LABEL;

  JERRY_ASSERT (context_p->byte_code_size > (branch_p->offset >> 8));

  delta = context_p->byte_code_size - (branch_p->offset >> 8);
  offset = (branch_p->offset & CBC_LOWER_SEVEN_BIT_MASK);

  JERRY_ASSERT (delta <= PARSER_MAXIMUM_CODE_SIZE);

#if PARSER_MAXIMUM_CODE_SIZE <= 65535
  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 */
  page_p->bytes[offset++] = (uint8_t) (delta >> 16);
  if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
  {
    page_p = page_p->next_p;
    offset = 0;
  }
  page_p->bytes[offset++] = (uint8_t) ((delta >> 8) & 0xff);
  if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
  {
    page_p = page_p->next_p;
    offset = 0;
  }
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
  page_p->bytes[offset++] = delta & 0xff;
} /* parser_set_branch_to_current_position */

/**
 * Set breaks to the current byte code position
 */
void
parser_set_breaks_to_current_position (parser_context_t *context_p, /**< context */
                                       parser_branch_node_t *current_p) /**< branch list */
{
  while (current_p != NULL)
  {
    parser_branch_node_t *next_p = current_p->next_p;

    if (!(current_p->branch.offset & CBC_HIGHEST_BIT_MASK))
    {
      parser_set_branch_to_current_position (context_p, &current_p->branch);
    }
    parser_free (current_p, sizeof (parser_branch_node_t));
    current_p = next_p;
  }
} /* parser_set_breaks_to_current_position */

/**
 * Set continues to the current byte code position
 */
void
parser_set_continues_to_current_position (parser_context_t *context_p, /**< context */
                                          parser_branch_node_t *current_p) /**< branch list */
{
  while (current_p != NULL)
  {
    if (current_p->branch.offset & CBC_HIGHEST_BIT_MASK)
    {
      parser_set_branch_to_current_position (context_p, &current_p->branch);
    }
    current_p = current_p->next_p;
  }
} /* parser_set_continues_to_current_position */

#ifdef JERRY_ENABLE_ERROR_MESSAGES
/**
 * Returns with the string representation of the error
 */
const char *
parser_error_to_string (parser_error_t error) /**< error code */
{
  switch (error)
  {
    case PARSER_ERR_OUT_OF_MEMORY:
    {
      return "Out of memory.";
    }
    case PARSER_ERR_LITERAL_LIMIT_REACHED:
    {
      return "Maximum number of literals reached.";
    }
    case PARSER_ERR_ARGUMENT_LIMIT_REACHED:
    {
      return "Maximum number of function arguments reached.";
    }
    case PARSER_ERR_STACK_LIMIT_REACHED:
    {
      return "Maximum function stack size reached.";
    }
    case PARSER_ERR_REGISTER_LIMIT_REACHED:
    {
      return "Maximum number of registers is reached.";
    }
    case PARSER_ERR_INVALID_CHARACTER:
    {
      return "Invalid (unexpected) character.";
    }
    case PARSER_ERR_INVALID_HEX_DIGIT:
    {
      return "Invalid hexadecimal digit.";
    }
    case PARSER_ERR_INVALID_ESCAPE_SEQUENCE:
    {
      return "Invalid escape sequence.";
    }
    case PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE:
    {
      return "Invalid unicode escape sequence.";
    }
    case PARSER_ERR_INVALID_IDENTIFIER_START:
    {
      return "Character cannot be start of an identifier.";
    }
    case PARSER_ERR_INVALID_IDENTIFIER_PART:
    {
      return "Character cannot be part of an identifier.";
    }
    case PARSER_ERR_INVALID_NUMBER:
    {
      return "Invalid number.";
    }
    case PARSER_ERR_MISSING_EXPONENT:
    {
      return "Missing exponent part.";
    }
    case PARSER_ERR_IDENTIFIER_AFTER_NUMBER:
    {
      return "Identifier cannot start after a number.";
    }
    case PARSER_ERR_INVALID_REGEXP:
    {
      return "Invalid regular expression.";
    }
    case PARSER_ERR_UNKNOWN_REGEXP_FLAG:
    {
      return "Unknown regexp flag.";
    }
    case PARSER_ERR_DUPLICATED_REGEXP_FLAG:
    {
      return "Duplicated regexp flag.";
    }
    case PARSER_ERR_UNSUPPORTED_REGEXP:
    {
      return "Regexp is not supported in the selected profile.";
    }
    case PARSER_ERR_IDENTIFIER_TOO_LONG:
    {
      return "Identifier is too long.";
    }
    case PARSER_ERR_STRING_TOO_LONG:
    {
      return "String is too long.";
    }
    case PARSER_ERR_NUMBER_TOO_LONG:
    {
      return "Number is too long.";
    }
    case PARSER_ERR_REGEXP_TOO_LONG:
    {
      return "Regexp is too long.";
    }
    case PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT:
    {
      return "Unterminated multiline comment.";
    }
    case PARSER_ERR_UNTERMINATED_STRING:
    {
      return "Unterminated string literal.";
    }
    case PARSER_ERR_UNTERMINATED_REGEXP:
    {
      return "Unterminated regexp literal.";
    }
    case PARSER_ERR_NEWLINE_NOT_ALLOWED:
    {
      return "Newline is not allowed in strings or regexps.";
    }
    case PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED:
    {
      return "Octal numbers are not allowed in strict mode.";
    }
    case PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED:
    {
      return "Octal escape sequences are not allowed in strict mode.";
    }
    case PARSER_ERR_STRICT_IDENT_NOT_ALLOWED:
    {
      return "Identifier name is reserved in strict mode.";
    }
    case PARSER_ERR_EVAL_NOT_ALLOWED:
    {
      return "Eval is not allowed to be used here in strict mode.";
    }
    case PARSER_ERR_ARGUMENTS_NOT_ALLOWED:
    {
      return "Arguments is not allowed to be used here in strict mode.";
    }
    case PARSER_ERR_DELETE_IDENT_NOT_ALLOWED:
    {
      return "Deleting identifier is not allowed in strict mode.";
    }
    case PARSER_ERR_EVAL_CANNOT_ASSIGNED:
    {
      return "Eval cannot be assigned to in strict mode.";
    }
    case PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED:
    {
      return "Arguments cannot be assigned to in strict mode.";
    }
    case PARSER_ERR_WITH_NOT_ALLOWED:
    {
      return "With statement not allowed in strict mode.";
    }
    case PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED:
    {
      return "Multiple default cases are not allowed.";
    }
    case PARSER_ERR_DEFAULT_NOT_IN_SWITCH:
    {
      return "Default statement must be in a switch block.";
    }
    case PARSER_ERR_CASE_NOT_IN_SWITCH:
    {
      return "Case statement must be in a switch block.";
    }
    case PARSER_ERR_LEFT_PAREN_EXPECTED:
    {
      return "Expected '(' token.";
    }
    case PARSER_ERR_LEFT_BRACE_EXPECTED:
    {
      return "Expected '{' token.";
    }
    case PARSER_ERR_RIGHT_PAREN_EXPECTED:
    {
      return "Expected ')' token.";
    }
    case PARSER_ERR_RIGHT_SQUARE_EXPECTED:
    {
      return "Expected ']' token.";
    }
#ifndef CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
    case PARSER_ERR_RIGHT_BRACE_EXPECTED:
    {
      return "Expected '}' token.";
    }
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
#ifndef CONFIG_DISABLE_ES2015_CLASS
    case PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR:
    {
      return "Multiple constructors are not allowed.";
    }
    case PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR:
    {
      return "Class constructor may not be an accessor.";
    }
    case PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE:
    {
      return "Classes may not have a static property called 'prototype'.";
    }
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
    case PARSER_ERR_COLON_EXPECTED:
    {
      return "Expected ':' token.";
    }
    case PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED:
    {
      return "Expected ':' token for ?: conditional expression.";
    }
    case PARSER_ERR_SEMICOLON_EXPECTED:
    {
      return "Expected ';' token.";
    }
    case PARSER_ERR_IN_EXPECTED:
    {
      return "Expected 'in' token.";
    }
    case PARSER_ERR_WHILE_EXPECTED:
    {
      return "While expected for do-while loop.";
    }
    case PARSER_ERR_CATCH_FINALLY_EXPECTED:
    {
      return "Catch or finally block expected.";
    }
    case PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED:
    {
      return "Expected ',' or ']' after an array item.";
    }
    case PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED:
    {
      return "Expected ',' or '}' after a property definition.";
    }
    case PARSER_ERR_IDENTIFIER_EXPECTED:
    {
      return "Identifier expected.";
    }
    case PARSER_ERR_EXPRESSION_EXPECTED:
    {
      return "Expression expected.";
    }
    case PARSER_ERR_PRIMARY_EXP_EXPECTED:
    {
      return "Primary expression expected.";
    }
    case PARSER_ERR_STATEMENT_EXPECTED:
    {
      return "Statement expected.";
    }
    case PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED:
    {
      return "Property identifier expected.";
    }
    case PARSER_ERR_ARGUMENT_LIST_EXPECTED:
    {
      return "Expected argument list.";
    }
    case PARSER_ERR_NO_ARGUMENTS_EXPECTED:
    {
      return "Property getters must have no arguments.";
    }
    case PARSER_ERR_ONE_ARGUMENT_EXPECTED:
    {
      return "Property setters must have one argument.";
    }
    case PARSER_ERR_INVALID_EXPRESSION:
    {
      return "Invalid expression.";
    }
    case PARSER_ERR_INVALID_SWITCH:
    {
      return "Invalid switch body.";
    }
    case PARSER_ERR_INVALID_BREAK:
    {
      return "Break statement must be inside a loop or switch.";
    }
    case PARSER_ERR_INVALID_BREAK_LABEL:
    {
      return "Labeled statement targeted by a break not found.";
    }
    case PARSER_ERR_INVALID_CONTINUE:
    {
      return "Continue statement must be inside a loop.";
    }
    case PARSER_ERR_INVALID_CONTINUE_LABEL:
    {
      return "Labeled statement targeted by a continue not found.";
    }
    case PARSER_ERR_INVALID_RETURN:
    {
      return "Return statement must be inside a function body.";
    }
    case PARSER_ERR_INVALID_RIGHT_SQUARE:
    {
      return "Unexpected '}' token.";
    }
    case PARSER_ERR_DUPLICATED_LABEL:
    {
      return "Duplicated label.";
    }
    case PARSER_ERR_OBJECT_PROPERTY_REDEFINED:
    {
      return "Property of object literal redefined.";
    }
    case PARSER_ERR_NON_STRICT_ARG_DEFINITION:
    {
      return "Non-strict argument definition.";
    }
    default:
    {
      JERRY_ASSERT (error == PARSER_ERR_NO_ERROR);
      return "No error.";
    }
  }
} /* parser_error_to_string */
#endif /* JERRY_ENABLE_ERROR_MESSAGES */

/**
 * @}
 * @}
 * @}
 */

#endif /* !JERRY_DISABLE_JS_PARSER */