-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathnjs_generator.c
5626 lines (4287 loc) · 171 KB
/
njs_generator.c
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
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Dmitry Volyntsev
* Copyright (C) Alexander Borisov
* Copyright (C) NGINX, Inc.
*/
#include <njs_main.h>
#define NJS_FUNCTION_MAX_DEPTH 128
typedef struct njs_generator_patch_s njs_generator_patch_t;
typedef enum {
NJS_GENERATOR_LOOP = 1,
NJS_GENERATOR_SWITCH = 2,
NJS_GENERATOR_BLOCK = 4,
NJS_GENERATOR_TRY = 8,
#define NJS_GENERATOR_ALL (NJS_GENERATOR_LOOP | NJS_GENERATOR_SWITCH)
} njs_generator_block_type_t;
typedef enum {
NJS_GENERATOR_CONTINUATION = 1,
NJS_GENERATOR_EXIT = 2,
} njs_generator_patch_type_t;
struct njs_generator_patch_s {
/*
* The jump_offset field points to jump offset field which contains a small
* adjustment and the adjustment should be added as (njs_jump_off_t *)
* because pointer to u_char accesses only one byte so this does not
* work on big endian platforms.
*/
njs_jump_off_t jump_offset;
njs_generator_patch_t *next;
njs_str_t label;
};
struct njs_generator_block_s {
njs_generator_block_type_t type; /* 4 bits */
njs_str_t label;
/* list of "continue" instruction offsets to be patched. */
njs_generator_patch_t *continuation;
/*
* list of "return" from try-catch block and "break"
* instruction offsets to be patched.
*/
njs_generator_patch_t *exit;
njs_generator_block_t *next;
/* exit value index, used only for NJS_GENERATOR_TRY blocks. */
njs_index_t index;
};
typedef struct {
njs_generator_state_func_t state;
njs_queue_link_t link;
njs_parser_node_t *node;
void *context;
} njs_generator_stack_entry_t;
typedef struct {
njs_generator_patch_t *patch;
njs_generator_patch_t **last;
njs_vmcode_jump_t *jump;
njs_jump_off_t jump_offset;
njs_index_t index;
} njs_generator_switch_ctx_t;
typedef struct {
njs_jump_off_t jump_offset;
njs_jump_off_t loop_offset;
njs_vmcode_jump_t *jump;
njs_variable_t *var;
njs_index_t index;
njs_index_t index_next_value;
} njs_generator_loop_ctx_t;
typedef struct {
njs_index_t exception_index;
njs_jump_off_t try_offset;
njs_jump_off_t catch_offset;
njs_generator_block_t *try_block;
njs_generator_block_t *catch_block;
njs_str_t try_cont_label;
njs_str_t try_exit_label;
njs_str_t catch_cont_label;
njs_str_t catch_exit_label;
} njs_generator_try_ctx_t;
static u_char *njs_generate_reserve(njs_vm_t *vm, njs_generator_t *generator,
size_t size);
static njs_int_t njs_generate_code_map(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, u_char *code);
static njs_int_t njs_generate_name(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_variable(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_reference_type_t type,
njs_variable_t **retvar);
static njs_int_t njs_generate_var_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_var_statement_after(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_let(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_variable_t *var);
static njs_int_t njs_generate_if_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_if_statement_cond(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_if_statement_then(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_if_statement_else(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_cond_expression(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_cond_expression_handler(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_cond_expression_true(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_cond_expression_false(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_switch_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_switch_expression(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *swtch);
static njs_int_t njs_generate_switch_case(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *swtch);
static njs_int_t njs_generate_switch_case_after(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *branch);
static njs_int_t njs_generate_switch_case_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *branch);
static njs_int_t njs_generate_switch_default(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *branch);
static njs_int_t njs_generate_switch_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *swtch);
static njs_int_t njs_generate_while_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_while_condition(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_while_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_do_while_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_do_while_condition(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_do_while_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_init(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_for_body(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_for_update(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_end(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_for_let_update(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_resolve_closure(njs_vm_t *vm,
njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_set_prop_block(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_name_assign(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_object(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_object_wo_decl(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_object_left_hand_expr(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_body(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_body_wo_decl(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_for_in_body_left_hand_expr(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_start_block(njs_vm_t *vm,
njs_generator_t *generator, njs_generator_block_type_t type,
const njs_str_t *label);
static njs_generator_block_t *njs_generate_lookup_block(
njs_generator_block_t *block, uint32_t mask, const njs_str_t *label);
static njs_generator_block_t *njs_generate_find_block(njs_vm_t *vm,
njs_generator_block_t *block, uint32_t mask, const njs_str_t *label);
static void njs_generate_patch_block(njs_vm_t *vm, njs_generator_t *generator,
njs_generator_block_t *block, unsigned type);
static njs_generator_patch_t *njs_generate_make_continuation_patch(njs_vm_t *vm,
njs_generator_block_t *block, const njs_str_t *label,
njs_jump_off_t offset);
static njs_generator_patch_t *njs_generate_make_exit_patch(njs_vm_t *vm,
njs_generator_block_t *block, const njs_str_t *label,
njs_jump_off_t offset);
static void njs_generate_patch_block_exit(njs_vm_t *vm,
njs_generator_t *generator);
static const njs_str_t *njs_generate_jump_destination(njs_vm_t *vm,
njs_generator_block_t *block, const char *inst_type, uint32_t mask,
const njs_str_t *label1, const njs_str_t *label2);
static njs_int_t njs_generate_continue_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_break_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_debugger_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_block_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_block_statement_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_children(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_stop_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_stop_statement_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_comma_expression(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_comma_expression_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_global_property_set(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node_dst,
njs_parser_node_t *node_src);
static njs_int_t njs_generate_assignment(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_assignment_name(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_assignment_prop(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_assignment_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_operation_assignment(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_operation_assignment_name(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_operation_assignment_prop(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_operation_assignment_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_object(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_property_accessor(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_property_accessor_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_array(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_function_expression(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_regexp(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_template_literal(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_template_literal_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_test_jump_expression(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_test_jump_expression_after(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_test_jump_expression_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_3addr_operation(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node, njs_bool_t swap);
static njs_int_t njs_generate_3addr_operation_name(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_3addr_operation_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_2addr_operation(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_2addr_operation_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_typeof_operation(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_typeof_operation_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_inc_dec_operation(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node, njs_bool_t post);
static njs_int_t njs_generate_inc_dec_operation_prop(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function_declaration(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function_scope(njs_vm_t *vm,
njs_generator_t *generator, njs_function_lambda_t *lambda,
njs_parser_node_t *node, const njs_str_t *name);
static njs_int_t njs_generate_scope_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static int64_t njs_generate_lambda_variables(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_return_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_return_statement_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function_call(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function_call_arguments(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_function_call_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_method_call(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_method_call_arguments(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_method_call_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_call(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_move_arguments(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_try_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_try_left(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_try_catch(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_try_finally(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_try_end(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_throw_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_throw_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_import_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_export_statement(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_export_statement_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_await(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node);
static njs_int_t njs_generate_await_end(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_wo_dest(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_wo_dest_after(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_global_reference(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node, njs_bool_t exception);
static njs_int_t njs_generate_reference_error(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_index_t njs_generate_dest_index(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_index_t njs_generate_object_dest_index(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_index_t njs_generate_node_temp_index_get(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_index_t njs_generate_temp_index_get(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_children_indexes_release(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_node_index_release(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_node_index_release_pop(njs_vm_t *vm,
njs_generator_t *generator, njs_parser_node_t *node);
static njs_int_t njs_generate_index_release(njs_vm_t *vm,
njs_generator_t *generator, njs_index_t index);
#define njs_generate_code(generator, type, _code, _op, nd) \
do { \
_code = (type *) njs_generate_reserve(vm, generator, sizeof(type)); \
if (njs_slow_path(_code == NULL)) { \
return NJS_ERROR; \
} \
\
if (njs_generate_code_map(vm, generator, nd, (u_char *) _code) \
!= NJS_OK) \
{ \
return NJS_ERROR; \
} \
\
generator->code_end += sizeof(type); \
\
_code->code = _op; \
} while (0)
#define njs_generate_code_jump(generator, _code, _offset) \
do { \
njs_generate_code(generator, njs_vmcode_jump_t, _code, \
NJS_VMCODE_JUMP, NULL); \
_code->offset = _offset; \
} while (0)
#define njs_generate_code_move(generator, _code, _dst, _src, node) \
do { \
njs_generate_code(generator, njs_vmcode_move_t, _code, \
NJS_VMCODE_MOVE, node); \
_code->dst = _dst; \
_code->src = _src; \
} while (0)
#define njs_code_offset(generator, code) \
((u_char *) code - generator->code_start)
#define njs_code_ptr(generator, type, offset) \
(type *) (generator->code_start + offset)
#define njs_code_jump_ptr(generator, offset) \
(njs_jump_off_t *) (generator->code_start + offset)
#define njs_code_offset_diff(generator, offset) \
((generator->code_end - generator->code_start) - offset)
#define njs_code_set_offset(generator, offset, target) \
*(njs_code_jump_ptr(generator, offset)) \
= njs_code_offset_diff(generator, target)
#define njs_code_set_jump_offset(generator, type, code_offset) \
*(njs_code_jump_ptr(generator, code_offset + offsetof(type, offset))) \
= njs_code_offset_diff(generator, code_offset)
#define njs_code_update_offset(generator, patch) \
*(njs_code_jump_ptr(generator, patch->jump_offset)) += \
njs_code_offset_diff(generator, patch->jump_offset)
#define njs_generate_syntax_error(vm, node, file, fmt, ...) \
njs_parser_node_error(vm, NJS_OBJ_TYPE_SYNTAX_ERROR, node, file, fmt, \
##__VA_ARGS__)
#ifdef NJS_DEBUG_GENERATOR
#define njs_debug_generator(vm, msg, ...) \
if (vm->options.generator_debug) \
njs_printf("GENERATOR " msg "\n", ##__VA_ARGS__)
#define njs_debug_generator_code(code) \
if (vm->options.generator_debug) \
njs_disassemble((u_char *) code, NULL, 1, NULL)
#else
#define njs_debug_generator(vm, msg, ...)
#define njs_debug_generator_code(code)
#endif
static const njs_str_t no_label = njs_str("");
static const njs_str_t return_label = njs_str("@return");
/* GCC and Clang complain about NULL argument passed to memcmp(). */
static const njs_str_t undef_label = { 0xffffffff, (u_char *) "" };
njs_int_t
njs_generator_init(njs_generator_t *generator, njs_str_t *file,
njs_int_t depth, njs_bool_t runtime)
{
njs_memzero(generator, sizeof(njs_generator_t));
njs_queue_init(&generator->stack);
generator->file = *file;
generator->depth = depth;
generator->runtime = runtime;
return NJS_OK;
}
njs_inline void
njs_generator_next(njs_generator_t *generator, njs_generator_state_func_t state,
njs_parser_node_t *node)
{
generator->state = state;
generator->node = node;
}
njs_inline njs_int_t
njs_generator_after(njs_vm_t *vm, njs_generator_t *generator,
njs_queue_link_t *link, njs_parser_node_t *node,
njs_generator_state_func_t state, void *ctx, size_t size)
{
njs_generator_stack_entry_t *entry;
entry = njs_mp_alloc(vm->mem_pool, sizeof(njs_generator_stack_entry_t));
if (njs_slow_path(entry == NULL)) {
return NJS_ERROR;
}
entry->state = state;
entry->node = node;
entry->context = ctx;
njs_queue_insert_before(link, &entry->link);
if (size > 0) {
entry->context = njs_mp_alloc(vm->mem_pool, size);
if (njs_slow_path(entry->context == NULL)) {
return NJS_ERROR;
}
memcpy(entry->context, ctx, size);
}
return NJS_OK;
}
njs_inline njs_int_t
njs_generator_stack_pop(njs_vm_t *vm, njs_generator_t *generator, void *ctx)
{
njs_queue_link_t *link;
njs_generator_stack_entry_t *entry;
entry = njs_queue_link_data(njs_queue_first(&generator->stack),
njs_generator_stack_entry_t, link);
link = njs_queue_first(&generator->stack);
njs_queue_remove(link);
if (ctx != NULL) {
njs_mp_free(vm->mem_pool, ctx);
}
generator->context = entry->context;
njs_generator_next(generator, entry->state, entry->node);
njs_mp_free(vm->mem_pool, entry);
return NJS_OK;
}
static njs_int_t
njs_generate(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node)
{
if (node == NULL) {
return njs_generator_stack_pop(vm, generator, NULL);
}
switch (node->token_type) {
case NJS_TOKEN_VAR:
case NJS_TOKEN_LET:
case NJS_TOKEN_CONST:
return njs_generate_var_statement(vm, generator, node);
case NJS_TOKEN_IF:
return njs_generate_if_statement(vm, generator, node);
case NJS_TOKEN_CONDITIONAL:
return njs_generate_cond_expression(vm, generator, node);
case NJS_TOKEN_SWITCH:
return njs_generate_switch_statement(vm, generator, node);
case NJS_TOKEN_WHILE:
return njs_generate_while_statement(vm, generator, node);
case NJS_TOKEN_DO:
return njs_generate_do_while_statement(vm, generator, node);
case NJS_TOKEN_FOR:
return njs_generate_for_statement(vm, generator, node);
case NJS_TOKEN_FOR_IN:
return njs_generate_for_in_statement(vm, generator, node);
case NJS_TOKEN_CONTINUE:
return njs_generate_continue_statement(vm, generator, node);
case NJS_TOKEN_BREAK:
return njs_generate_break_statement(vm, generator, node);
case NJS_TOKEN_DEBUGGER:
return njs_generate_debugger_statement(vm, generator, node);
case NJS_TOKEN_STATEMENT:
return njs_generate_statement(vm, generator, node);
case NJS_TOKEN_BLOCK:
return njs_generate_block_statement(vm, generator, node);
case NJS_TOKEN_END:
return njs_generate_stop_statement(vm, generator, node);
case NJS_TOKEN_COMMA:
return njs_generate_comma_expression(vm, generator, node);
case NJS_TOKEN_ASSIGNMENT:
return njs_generate_assignment(vm, generator, node);
case NJS_TOKEN_BITWISE_OR_ASSIGNMENT:
case NJS_TOKEN_BITWISE_XOR_ASSIGNMENT:
case NJS_TOKEN_BITWISE_AND_ASSIGNMENT:
case NJS_TOKEN_LEFT_SHIFT_ASSIGNMENT:
case NJS_TOKEN_RIGHT_SHIFT_ASSIGNMENT:
case NJS_TOKEN_UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
case NJS_TOKEN_ADDITION_ASSIGNMENT:
case NJS_TOKEN_SUBTRACTION_ASSIGNMENT:
case NJS_TOKEN_MULTIPLICATION_ASSIGNMENT:
case NJS_TOKEN_EXPONENTIATION_ASSIGNMENT:
case NJS_TOKEN_DIVISION_ASSIGNMENT:
case NJS_TOKEN_REMAINDER_ASSIGNMENT:
return njs_generate_operation_assignment(vm, generator, node);
case NJS_TOKEN_BITWISE_OR:
case NJS_TOKEN_BITWISE_XOR:
case NJS_TOKEN_BITWISE_AND:
case NJS_TOKEN_EQUAL:
case NJS_TOKEN_NOT_EQUAL:
case NJS_TOKEN_STRICT_EQUAL:
case NJS_TOKEN_STRICT_NOT_EQUAL:
case NJS_TOKEN_INSTANCEOF:
case NJS_TOKEN_LESS:
case NJS_TOKEN_LESS_OR_EQUAL:
case NJS_TOKEN_GREATER:
case NJS_TOKEN_GREATER_OR_EQUAL:
case NJS_TOKEN_LEFT_SHIFT:
case NJS_TOKEN_RIGHT_SHIFT:
case NJS_TOKEN_UNSIGNED_RIGHT_SHIFT:
case NJS_TOKEN_ADDITION:
case NJS_TOKEN_SUBTRACTION:
case NJS_TOKEN_MULTIPLICATION:
case NJS_TOKEN_EXPONENTIATION:
case NJS_TOKEN_DIVISION:
case NJS_TOKEN_REMAINDER:
case NJS_TOKEN_PROPERTY_DELETE:
case NJS_TOKEN_PROPERTY:
return njs_generate_3addr_operation(vm, generator, node, 0);
case NJS_TOKEN_IN:
/*
* An "in" operation is parsed as standard binary expression
* by njs_parser_binary_expression(). However, its operands
* should be swapped to be uniform with other property operations
* (get/set and delete) to use the property trap.
*/
return njs_generate_3addr_operation(vm, generator, node, 1);
case NJS_TOKEN_LOGICAL_AND:
case NJS_TOKEN_LOGICAL_OR:
case NJS_TOKEN_COALESCE:
return njs_generate_test_jump_expression(vm, generator, node);
case NJS_TOKEN_DELETE:
case NJS_TOKEN_VOID:
case NJS_TOKEN_UNARY_PLUS:
case NJS_TOKEN_UNARY_NEGATION:
case NJS_TOKEN_LOGICAL_NOT:
case NJS_TOKEN_BITWISE_NOT:
return njs_generate_2addr_operation(vm, generator, node);
case NJS_TOKEN_TYPEOF:
return njs_generate_typeof_operation(vm, generator, node);
case NJS_TOKEN_INCREMENT:
case NJS_TOKEN_DECREMENT:
return njs_generate_inc_dec_operation(vm, generator, node, 0);
case NJS_TOKEN_POST_INCREMENT:
case NJS_TOKEN_POST_DECREMENT:
return njs_generate_inc_dec_operation(vm, generator, node, 1);
case NJS_TOKEN_NULL:
case NJS_TOKEN_TRUE:
case NJS_TOKEN_FALSE:
case NJS_TOKEN_NUMBER:
case NJS_TOKEN_STRING:
node->index = njs_scope_global_index(vm, &node->u.value,
generator->runtime);
if (njs_slow_path(node->index == NJS_INDEX_ERROR)) {
return NJS_ERROR;
}
return njs_generator_stack_pop(vm, generator, NULL);
case NJS_TOKEN_OBJECT_VALUE:
node->index = node->u.object->index;
return njs_generator_stack_pop(vm, generator, NULL);
case NJS_TOKEN_OBJECT:
return njs_generate_object(vm, generator, node);
case NJS_TOKEN_PROPERTY_GETTER:
case NJS_TOKEN_PROPERTY_SETTER:
return njs_generate_property_accessor(vm, generator, node);
case NJS_TOKEN_ARRAY:
return njs_generate_array(vm, generator, node);
case NJS_TOKEN_FUNCTION_EXPRESSION:
case NJS_TOKEN_ASYNC_FUNCTION_EXPRESSION:
return njs_generate_function_expression(vm, generator, node);
case NJS_TOKEN_FUNCTION:
case NJS_TOKEN_ASYNC_FUNCTION:
return njs_generate_function(vm, generator, node);
case NJS_TOKEN_REGEXP:
return njs_generate_regexp(vm, generator, node);
case NJS_TOKEN_TEMPLATE_LITERAL:
return njs_generate_template_literal(vm, generator, node);
case NJS_TOKEN_EXTERNAL:
return njs_generator_stack_pop(vm, generator, NULL);
case NJS_TOKEN_NAME:
case NJS_TOKEN_ARGUMENTS:
case NJS_TOKEN_EVAL:
case NJS_TOKEN_THIS:
return njs_generate_name(vm, generator, node);
case NJS_TOKEN_FUNCTION_DECLARATION:
case NJS_TOKEN_ASYNC_FUNCTION_DECLARATION:
return njs_generate_function_declaration(vm, generator, node);
case NJS_TOKEN_FUNCTION_CALL:
return njs_generate_function_call(vm, generator, node);
case NJS_TOKEN_RETURN:
return njs_generate_return_statement(vm, generator, node);
case NJS_TOKEN_METHOD_CALL:
return njs_generate_method_call(vm, generator, node);
case NJS_TOKEN_TRY:
return njs_generate_try_statement(vm, generator, node);
case NJS_TOKEN_THROW:
return njs_generate_throw_statement(vm, generator, node);
case NJS_TOKEN_IMPORT:
return njs_generate_import_statement(vm, generator, node);
case NJS_TOKEN_EXPORT:
return njs_generate_export_statement(vm, generator, node);
case NJS_TOKEN_AWAIT:
return njs_generate_await(vm, generator, node);
default:
njs_internal_error(vm, "Generator failed: unknown token");
return NJS_ERROR;
}
}
static njs_int_t
njs_generator_pop(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
return njs_generator_stack_pop(vm, generator, NULL);
}
static u_char *
njs_generate_reserve(njs_vm_t *vm, njs_generator_t *generator, size_t size)
{
u_char *p;
if (generator->code_end + size <=
generator->code_start + generator->code_size)
{
return generator->code_end;
}
size = njs_max(generator->code_end - generator->code_start + size,
generator->code_size);
if (size < 1024) {
size *= 2;
} else {
size += size / 2;
}
p = njs_mp_alloc(vm->mem_pool, size);
if (njs_slow_path(p == NULL)) {
njs_memory_error(vm);
return NULL;
}
generator->code_size = size;
size = generator->code_end - generator->code_start;
memcpy(p, generator->code_start, size);
njs_mp_free(vm->mem_pool, generator->code_start);
generator->code_start = p;
generator->code_end = p + size;
return generator->code_end;
}
static njs_int_t
njs_generate_code_map(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, u_char *code)
{
njs_arr_t *map;
njs_vm_line_num_t *last;
map = generator->lines;
if (map != NULL && node != NULL) {
last = (map->items != 0) ? njs_arr_last(map) : NULL;
if (last == NULL || (node->token_line != last->line)) {
last = njs_arr_add(map);
if (njs_slow_path(last == NULL)) {
return NJS_ERROR;
}
last->line = node->token_line;
last->offset = njs_code_offset(generator, code);
}
}
return NJS_OK;
}
njs_vm_code_t *
njs_lookup_code(njs_vm_t *vm, u_char *pc)
{
njs_uint_t i;
njs_vm_code_t *code;
code = vm->codes->start;
for (i = 0; i < vm->codes->items; i++, code++) {
if (code->start <= pc && pc < code->end) {
return code;
}
}
return NULL;
}
uint32_t
njs_lookup_line(njs_arr_t *lines, uint32_t offset)
{
njs_uint_t n;
njs_vm_line_num_t *map;
n = 0;
map = NULL;
if (lines != NULL) {
n = lines->items;
map = (njs_vm_line_num_t *) lines->start;
}
while (n != 0) {
if (offset >= map->offset && (n == 1 || offset < map[1].offset)) {
return map->line;
}
map++;
n--;
}
return 0;
}
static njs_int_t
njs_generate_name(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
njs_int_t ret;
njs_variable_t *var;
njs_parser_scope_t *scope;
njs_vmcode_variable_t *variable;
njs_vmcode_function_copy_t *copy;
var = njs_variable_reference(vm, node);
if (njs_slow_path(var == NULL)) {
ret = njs_generate_global_reference(vm, generator, node, 1);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
return njs_generator_stack_pop(vm, generator, NULL);
}
if (var->function && var->type == NJS_VARIABLE_FUNCTION) {
njs_generate_code(generator, njs_vmcode_function_copy_t, copy,
NJS_VMCODE_FUNCTION_COPY, node);
copy->function = &var->value;
copy->retval = node->index;
}
if (var->init) {
return njs_generator_stack_pop(vm, generator, NULL);
}
if (var->type == NJS_VARIABLE_LET || var->type == NJS_VARIABLE_CONST) {
scope = njs_function_scope(node->scope);
if (scope->dest_disable) {
njs_generate_code(generator, njs_vmcode_variable_t, variable,
NJS_VMCODE_NOT_INITIALIZED, node);
variable->dst = node->index;
}
}
return njs_generator_stack_pop(vm, generator, NULL);
}
static njs_int_t
njs_generate_variable(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_reference_type_t type, njs_variable_t **retvar)
{
njs_variable_t *var;
njs_parser_scope_t *scope;
njs_vmcode_variable_t *variable;
njs_vmcode_function_copy_t *copy;
var = njs_variable_reference(vm, node);
if (retvar != NULL) {
*retvar = var;
}
if (njs_slow_path(var == NULL)) {
switch (type) {
case NJS_DECLARATION:
return njs_generate_reference_error(vm, generator, node);
case NJS_REFERENCE:
case NJS_TYPEOF:
return njs_generate_global_reference(vm, generator, node,
type == NJS_REFERENCE);
}
}
if (var->function && var->type == NJS_VARIABLE_FUNCTION) {
njs_generate_code(generator, njs_vmcode_function_copy_t, copy,
NJS_VMCODE_FUNCTION_COPY, node);
copy->function = &var->value;
copy->retval = node->index;
}
if (var->init) {
return NJS_OK;
}
if (var->type == NJS_VARIABLE_LET || var->type == NJS_VARIABLE_CONST) {
scope = njs_function_scope(node->scope);
if ((!scope->dest_disable && njs_function_scope(var->scope) == scope)) {
njs_generate_code(generator, njs_vmcode_variable_t, variable,
NJS_VMCODE_NOT_INITIALIZED, node);
variable->dst = node->index;
}
}
return NJS_OK;
}
static njs_int_t
njs_generate_variable_wo_dest(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_reference_type_t type, njs_variable_t **retvar)
{
njs_int_t ret;
njs_parser_scope_t *scope;
scope = njs_function_scope(node->scope);
scope->dest_disable = 1;
ret = njs_generate_variable(vm, generator, node, type, retvar);
scope->dest_disable = 0;