/
DefaultCodeGenerator.java
2797 lines (2518 loc) · 96.9 KB
/
DefaultCodeGenerator.java
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) André Bargull
* Alle Rechte vorbehalten / All Rights Reserved. Use is subject to license terms.
*
* <https://github.com/anba/es6draft>
*/
package com.github.anba.es6draft.compiler;
import static com.github.anba.es6draft.compiler.ClassPropertyGenerator.ClassPropertyEvaluation;
import static com.github.anba.es6draft.semantics.StaticSemantics.DecoratedMethods;
import static com.github.anba.es6draft.semantics.StaticSemantics.PrivateBoundNames;
import java.math.BigInteger;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.PrimitiveIterator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import com.github.anba.es6draft.ast.*;
import com.github.anba.es6draft.ast.AbruptNode.Abrupt;
import com.github.anba.es6draft.ast.scope.BlockScope;
import com.github.anba.es6draft.ast.scope.ModuleScope;
import com.github.anba.es6draft.ast.scope.Name;
import com.github.anba.es6draft.ast.scope.Scope;
import com.github.anba.es6draft.ast.scope.ScriptScope;
import com.github.anba.es6draft.ast.scope.WithScope;
import com.github.anba.es6draft.ast.synthetic.MethodDefinitionsMethod;
import com.github.anba.es6draft.compiler.CodeVisitor.GeneratorState;
import com.github.anba.es6draft.compiler.CodeVisitor.LabelState;
import com.github.anba.es6draft.compiler.CodeVisitor.LabelledHashKey;
import com.github.anba.es6draft.compiler.CodeVisitor.OutlinedCall;
import com.github.anba.es6draft.compiler.StatementGenerator.Completion;
import com.github.anba.es6draft.compiler.assembler.*;
import com.github.anba.es6draft.compiler.assembler.Code.MethodCode;
import com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord;
import com.github.anba.es6draft.runtime.EnvironmentRecord;
import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.GlobalEnvironmentRecord;
import com.github.anba.es6draft.runtime.LexicalEnvironment;
import com.github.anba.es6draft.runtime.ModuleEnvironmentRecord;
import com.github.anba.es6draft.runtime.ObjectEnvironmentRecord;
import com.github.anba.es6draft.runtime.internal.Bootstrap;
import com.github.anba.es6draft.runtime.internal.CompatibilityOption;
import com.github.anba.es6draft.runtime.internal.ScriptException;
import com.github.anba.es6draft.runtime.internal.ScriptIterator;
import com.github.anba.es6draft.runtime.types.Callable;
import com.github.anba.es6draft.runtime.types.Null;
import com.github.anba.es6draft.runtime.types.Reference;
import com.github.anba.es6draft.runtime.types.ScriptObject;
import com.github.anba.es6draft.runtime.types.Undefined;
import com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction;
import com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject;
/**
* Abstract base class for specialised generators
*/
abstract class DefaultCodeGenerator<RETURN> extends DefaultNodeVisitor<RETURN, CodeVisitor> {
private static final class Fields {
static final FieldName Double_NaN = FieldName.findStatic(Types.Double, "NaN", Type.DOUBLE_TYPE);
static final FieldName CallOperations_EMPTY_ARRAY = FieldName.findStatic(Types.CallOperations, "EMPTY_ARRAY",
Types.Object_);
}
private static final class Methods {
// class: AbstractOperations
static final MethodName AbstractOperations_CreateIterResultObject = MethodName.findStatic(
Types.AbstractOperations, "CreateIterResultObject",
Type.methodType(Types.OrdinaryObject, Types.ExecutionContext, Types.Object, Type.BOOLEAN_TYPE));
static final MethodName AbstractOperations_HasOwnProperty = MethodName.findStatic(Types.AbstractOperations,
"HasOwnProperty",
Type.methodType(Type.BOOLEAN_TYPE, Types.ExecutionContext, Types.ScriptObject, Types.String));
static final MethodName AbstractOperations_GetMethod = MethodName.findStatic(Types.AbstractOperations,
"GetMethod", Type.methodType(Types.Callable, Types.ExecutionContext, Types.ScriptObject, Types.String));
static final MethodName AbstractOperations_Invoke = MethodName.findStatic(Types.AbstractOperations, "Invoke",
Type.methodType(Types.Object, Types.ExecutionContext, Types.ScriptObject, Types.String, Types.Object_));
static final MethodName AbstractOperations_IteratorComplete = MethodName.findStatic(Types.AbstractOperations,
"IteratorComplete", Type.methodType(Type.BOOLEAN_TYPE, Types.ExecutionContext, Types.ScriptObject));
static final MethodName AbstractOperations_IteratorNext_Object = MethodName.findStatic(Types.AbstractOperations,
"IteratorNext",
Type.methodType(Types.ScriptObject, Types.ExecutionContext, Types.ScriptIterator, Types.Object));
static final MethodName AbstractOperations_IteratorValue = MethodName.findStatic(Types.AbstractOperations,
"IteratorValue", Type.methodType(Types.Object, Types.ExecutionContext, Types.ScriptObject));
static final MethodName AbstractOperations_ToPrimitive = MethodName.findStatic(Types.AbstractOperations,
"ToPrimitive", Type.methodType(Types.Object, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToBoolean = MethodName.findStatic(Types.AbstractOperations,
"ToBoolean", Type.methodType(Type.BOOLEAN_TYPE, Types.Object));
static final MethodName AbstractOperations_ToBoolean_int = MethodName.findStatic(Types.AbstractOperations,
"ToBoolean", Type.methodType(Type.BOOLEAN_TYPE, Type.INT_TYPE));
static final MethodName AbstractOperations_ToBoolean_long = MethodName.findStatic(Types.AbstractOperations,
"ToBoolean", Type.methodType(Type.BOOLEAN_TYPE, Type.LONG_TYPE));
static final MethodName AbstractOperations_ToBoolean_double = MethodName.findStatic(Types.AbstractOperations,
"ToBoolean", Type.methodType(Type.BOOLEAN_TYPE, Type.DOUBLE_TYPE));
static final MethodName AbstractOperations_ToBoolean_BigInteger = MethodName.findStatic(
Types.AbstractOperations, "ToBoolean", Type.methodType(Type.BOOLEAN_TYPE, Types.BigInteger));
static final MethodName AbstractOperations_ToFlatString = MethodName.findStatic(Types.AbstractOperations,
"ToFlatString", Type.methodType(Types.String, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToNumber = MethodName.findStatic(Types.AbstractOperations,
"ToNumber", Type.methodType(Type.DOUBLE_TYPE, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToNumber_CharSequence = MethodName.findStatic(
Types.AbstractOperations, "ToNumber", Type.methodType(Type.DOUBLE_TYPE, Types.CharSequence));
static final MethodName AbstractOperations_ToNumeric = MethodName.findStatic(Types.AbstractOperations,
"ToNumeric", Type.methodType(Types.Number, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToNumericInt32 = MethodName.findStatic(Types.AbstractOperations,
"ToNumericInt32", Type.methodType(Types.Number, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToInt32 = MethodName.findStatic(Types.AbstractOperations, "ToInt32",
Type.methodType(Type.INT_TYPE, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToInt32_double = MethodName.findStatic(Types.AbstractOperations,
"ToInt32", Type.methodType(Type.INT_TYPE, Type.DOUBLE_TYPE));
static final MethodName AbstractOperations_ToUint32 = MethodName.findStatic(Types.AbstractOperations,
"ToUint32", Type.methodType(Type.LONG_TYPE, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToUint32_double = MethodName.findStatic(Types.AbstractOperations,
"ToUint32", Type.methodType(Type.LONG_TYPE, Type.DOUBLE_TYPE));
static final MethodName AbstractOperations_ToObject = MethodName.findStatic(Types.AbstractOperations,
"ToObject", Type.methodType(Types.ScriptObject, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToPropertyKey = MethodName.findStatic(Types.AbstractOperations,
"ToPropertyKey", Type.methodType(Types.Object, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToString = MethodName.findStatic(Types.AbstractOperations,
"ToString", Type.methodType(Types.CharSequence, Types.ExecutionContext, Types.Object));
static final MethodName AbstractOperations_ToString_int = MethodName.findStatic(Types.AbstractOperations,
"ToString", Type.methodType(Types.String, Type.INT_TYPE));
static final MethodName AbstractOperations_ToString_long = MethodName.findStatic(Types.AbstractOperations,
"ToString", Type.methodType(Types.String, Type.LONG_TYPE));
static final MethodName AbstractOperations_ToString_double = MethodName.findStatic(Types.AbstractOperations,
"ToString", Type.methodType(Types.String, Type.DOUBLE_TYPE));
static final MethodName AbstractOperations_ToString_BigInteger = MethodName.findStatic(Types.AbstractOperations,
"ToString", Type.methodType(Types.String, Types.BigInteger));
// class: AsyncAbstractOperations
static final MethodName AsyncAbstractOperations_AsyncFunctionAwait = MethodName.findStatic(
Types.AsyncAbstractOperations, "AsyncFunctionAwait",
Type.methodType(Type.VOID_TYPE, Types.ExecutionContext, Types.Object));
// class: Boolean
static final MethodName Boolean_toString = MethodName.findStatic(Types.Boolean, "toString",
Type.methodType(Types.String, Type.BOOLEAN_TYPE));
// class: CharSequence
static final MethodName CharSequence_length = MethodName.findInterface(Types.CharSequence, "length",
Type.methodType(Type.INT_TYPE));
static final MethodName CharSequence_toString = MethodName.findInterface(Types.CharSequence, "toString",
Type.methodType(Types.String));
// class: ExecutionContext
static final MethodName ExecutionContext_getLexicalEnvironment = MethodName.findVirtual(Types.ExecutionContext,
"getLexicalEnvironment", Type.methodType(Types.LexicalEnvironment));
static final MethodName ExecutionContext_getLexicalEnvironmentRecord = MethodName.findVirtual(
Types.ExecutionContext, "getLexicalEnvironmentRecord", Type.methodType(Types.EnvironmentRecord));
static final MethodName ExecutionContext_getVariableEnvironmentRecord = MethodName.findVirtual(
Types.ExecutionContext, "getVariableEnvironmentRecord", Type.methodType(Types.EnvironmentRecord));
static final MethodName ExecutionContext_pushLexicalEnvironment = MethodName.findVirtual(Types.ExecutionContext,
"pushLexicalEnvironment", Type.methodType(Type.VOID_TYPE, Types.LexicalEnvironment));
static final MethodName ExecutionContext_popLexicalEnvironment = MethodName.findVirtual(Types.ExecutionContext,
"popLexicalEnvironment", Type.methodType(Type.VOID_TYPE));
static final MethodName ExecutionContext_replaceLexicalEnvironment = MethodName.findVirtual(
Types.ExecutionContext, "replaceLexicalEnvironment",
Type.methodType(Type.VOID_TYPE, Types.LexicalEnvironment));
static final MethodName ExecutionContext_restoreLexicalEnvironment = MethodName.findVirtual(
Types.ExecutionContext, "restoreLexicalEnvironment",
Type.methodType(Type.VOID_TYPE, Types.LexicalEnvironment));
// class: LexicalEnvironment
static final MethodName LexicalEnvironment_getEnvRec = MethodName.findVirtual(Types.LexicalEnvironment,
"getEnvRec", Type.methodType(Types.EnvironmentRecord));
static final MethodName LexicalEnvironment_cloneDeclarativeEnvironment = MethodName.findStatic(
Types.LexicalEnvironment, "cloneDeclarativeEnvironment",
Type.methodType(Types.LexicalEnvironment, Types.LexicalEnvironment));
static final MethodName LexicalEnvironment_newDeclarativeEnvironment = MethodName.findStatic(
Types.LexicalEnvironment, "newDeclarativeEnvironment",
Type.methodType(Types.LexicalEnvironment, Types.LexicalEnvironment));
static final MethodName LexicalEnvironment_newCatchDeclarativeEnvironment = MethodName.findStatic(
Types.LexicalEnvironment, "newCatchDeclarativeEnvironment",
Type.methodType(Types.LexicalEnvironment, Types.LexicalEnvironment));
static final MethodName LexicalEnvironment_newObjectEnvironment = MethodName
.findStatic(Types.LexicalEnvironment, "newObjectEnvironment", Type.methodType(Types.LexicalEnvironment,
Types.ScriptObject, Types.LexicalEnvironment, Type.BOOLEAN_TYPE));
// class: FunctionObject
static final MethodName FunctionObject_SetFunctionName = MethodName.findStatic(Types.FunctionObject,
"SetFunctionName", Type.methodType(Type.VOID_TYPE, Types.OrdinaryObject, Types.Object));
static final MethodName FunctionObject_SetFunctionName_String = MethodName.findStatic(Types.FunctionObject,
"SetFunctionName", Type.methodType(Type.VOID_TYPE, Types.OrdinaryObject, Types.String));
// class: ReturnValue
static final MethodName ReturnValue_new = MethodName.findConstructor(Types.ReturnValue,
Type.methodType(Type.VOID_TYPE, Types.Object));
static final MethodName ReturnValue_getValue = MethodName.findVirtual(Types.ReturnValue, "getValue",
Type.methodType(Types.Object));
// class: ScriptException
static final MethodName ScriptException_getValue = MethodName.findVirtual(Types.ScriptException, "getValue",
Type.methodType(Types.Object));
// class: CallOperations
static final MethodName CallOperations_CheckCallable = MethodName.findStatic(Types.CallOperations,
"CheckCallable", Type.methodType(Types.Callable, Types.Object, Types.ExecutionContext));
// class: ClassOperations
static final MethodName ClassOperations_CreateStaticClassFieldInitializer = MethodName.findStatic(
Types.ClassOperations, "CreateStaticClassFieldInitializer", Type.methodType(Types.OrdinaryFunction,
Types.ScriptObject, Types.RuntimeInfo$Function, Types.Object_, Types.ExecutionContext));
static final MethodName ClassOperations_CreateClassFieldInitializer = MethodName.findStatic(
Types.ClassOperations, "CreateClassFieldInitializer",
Type.methodType(Types.OrdinaryFunction, Types.ScriptObject, Types.RuntimeInfo$Function, Types.Object_,
Types.ClassOperations$InstanceMethod_, Types.ExecutionContext));
static final MethodName ClassOperations_EvaluateConstructorMethod = MethodName.findStatic(Types.ClassOperations,
"EvaluateConstructorMethod", Type.methodType(Types.OrdinaryConstructorFunction, Types.ScriptObject,
Types.OrdinaryObject, Types.RuntimeInfo$Function, Type.BOOLEAN_TYPE, Types.ExecutionContext));
static final MethodName ClassOperations_createProto = MethodName.findStatic(Types.ClassOperations,
"createProto", Type.methodType(Types.OrdinaryObject, Types.ScriptObject, Types.ExecutionContext));
static final MethodName ClassOperations_getClassProto = MethodName.findStatic(Types.ClassOperations,
"getClassProto", Type.methodType(Types.ScriptObject_, Types.Object, Types.ExecutionContext));
static final MethodName ClassOperations_getClassProto_Null = MethodName.findStatic(Types.ClassOperations,
"getClassProto", Type.methodType(Types.ScriptObject_, Types.ExecutionContext));
static final MethodName ClassOperations_getDefaultClassProto = MethodName.findStatic(Types.ClassOperations,
"getDefaultClassProto", Type.methodType(Types.ScriptObject_, Types.ExecutionContext));
static final MethodName ClassOperations_setInstanceFieldsInitializer = MethodName.findStatic(
Types.ClassOperations, "setInstanceFieldsInitializer",
Type.methodType(Type.VOID_TYPE, Types.OrdinaryConstructorFunction, Types.OrdinaryFunction));
// class: DecoratorOperations
static final MethodName DecoratorOperations_propertyDescriptor = MethodName.findStatic(
Types.DecoratorOperations, "propertyDescriptor",
Type.methodType(Types.Object, Types.OrdinaryObject, Types.Object, Types.ExecutionContext));
static final MethodName DecoratorOperations_defineProperty = MethodName.findStatic(Types.DecoratorOperations,
"defineProperty", Type.methodType(Type.VOID_TYPE, Types.OrdinaryObject, Types.Object, Types.Object,
Types.ExecutionContext));
// class: IteratorOperations
static final MethodName IteratorOperations_iterate = MethodName.findStatic(Types.IteratorOperations, "iterate",
Type.methodType(Types.ScriptIterator, Types.Object, Types.ExecutionContext));
static final MethodName IteratorOperations_asyncIterate = MethodName.findStatic(Types.IteratorOperations,
"asyncIterate", Type.methodType(Types.ScriptIterator, Types.Object, Types.ExecutionContext));
// class: Operators
static final MethodName Operators_yieldThrowCompletion = MethodName.findStatic(Types.Operators,
"yieldThrowCompletion",
Type.methodType(Types.ScriptObject, Types.ExecutionContext, Types.ScriptObject, Types.ScriptException));
static final MethodName Operators_yieldReturnCompletion = MethodName.findStatic(Types.Operators,
"yieldReturnCompletion",
Type.methodType(Types.ScriptObject, Types.ExecutionContext, Types.ScriptObject, Types.ReturnValue));
static final MethodName Operators_reportPropertyNotCallable = MethodName.findStatic(Types.Operators,
"reportPropertyNotCallable",
Type.methodType(Types.ScriptException, Types.String, Types.ExecutionContext));
static final MethodName Operators_requireObjectResult = MethodName.findStatic(Types.Operators,
"requireObjectResult",
Type.methodType(Types.ScriptObject, Types.Object, Types.String, Types.ExecutionContext));
// class: Type
static final MethodName Type_isObject = MethodName.findStatic(Types._Type, "isObject",
Type.methodType(Type.BOOLEAN_TYPE, Types.Object));
static final MethodName Type_isUndefinedOrNull = MethodName.findStatic(Types._Type, "isUndefinedOrNull",
Type.methodType(Type.BOOLEAN_TYPE, Types.Object));
// class: ScriptIterator
static final MethodName ScriptIterator_getScriptObject = MethodName.findInterface(Types.ScriptIterator,
"getScriptObject", Type.methodType(Types.ScriptObject));
static final MethodName ScriptIterator_nextIterResult = MethodName.findInterface(Types.ScriptIterator,
"nextIterResult", Type.methodType(Types.Object, Types.Object));
// class: Throwable
static final MethodName Throwable_addSuppressed = MethodName.findVirtual(Types.Throwable, "addSuppressed",
Type.methodType(Type.VOID_TYPE, Types.Throwable));
}
protected final CodeGenerator codegen;
protected DefaultCodeGenerator(CodeGenerator codegen) {
this.codegen = codegen;
}
/**
* stack: [] {@literal ->} [value|reference]
*
* @param node
* the expression node
* @param mv
* the code visitor
* @return the value type returned by the expression
*/
protected final ValType expression(Expression node, CodeVisitor mv) {
return codegen.expression(node, mv);
}
/**
* stack: [] {@literal ->} [boxed(value)]
*
* @param node
* the expression node
* @param mv
* the code visitor
* @return the value type returned by the expression
*/
protected final ValType expressionBoxed(Expression node, CodeVisitor mv) {
return codegen.expressionBoxed(node, mv);
}
/**
* stack: [] {@literal ->} []
*
* @param node
* the module-item node
* @param mv
* the code visitor
* @return the completion type
*/
protected final Completion statement(ModuleItem node, CodeVisitor mv) {
return codegen.statement(node, mv);
}
/**
* stack: [] {@literal ->} []
*
* @param statements
* the statements list
* @param mv
* the code visitor
* @return the completion type
*/
protected final Completion statements(List<? extends ModuleItem> statements, CodeVisitor mv) {
return codegen.statements(statements, mv);
}
/**
* stack: [] {@literal ->} []
*
* @param node
* the abrupt node
* @param mv
* the code visitor
* @return the variable holding the saved environment or {@code null}
*/
protected final Variable<LexicalEnvironment<?>> saveEnvironment(AbruptNode node, CodeVisitor mv) {
EnumSet<Abrupt> abrupt = node.getAbrupt();
if (abrupt.contains(Abrupt.Break) || abrupt.contains(Abrupt.Continue)) {
return saveEnvironment(mv);
}
return null;
}
/**
* stack: [] {@literal ->} []
*
* @param mv
* the code visitor
* @return the variable holding the saved environment
*/
protected final Variable<LexicalEnvironment<?>> saveEnvironment(CodeVisitor mv) {
Variable<LexicalEnvironment<?>> savedEnv = mv.newVariable("savedEnv", LexicalEnvironment.class).uncheckedCast();
getLexicalEnvironment(mv);
mv.store(savedEnv);
return savedEnv;
}
/**
* stack: [] {@literal ->} []
*
* @param savedEnv
* the variable which holds the saved environment
* @param mv
* the code visitor
*/
protected final void restoreEnvironment(Variable<LexicalEnvironment<?>> savedEnv, CodeVisitor mv) {
mv.loadExecutionContext();
mv.load(savedEnv);
mv.invoke(Methods.ExecutionContext_restoreLexicalEnvironment);
}
/**
* stack: [] {@literal ->} []
*
* @param savedEnv
* the variable which holds the saved environment
* @param mv
* the code visitor
*/
protected final void replaceLexicalEnvironment(Variable<LexicalEnvironment<?>> savedEnv, CodeVisitor mv) {
mv.loadExecutionContext();
mv.load(savedEnv);
mv.invoke(Methods.ExecutionContext_replaceLexicalEnvironment);
}
/**
* stack: [] {@literal ->} [lexEnv]
*
* @param mv
* the code visitor
*/
protected final void getLexicalEnvironment(CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironment);
}
/**
* stack: [] {@literal ->} []
*
* @param envRec
* the variable which holds the lexical environment record
* @param mv
* the code visitor
*/
protected final <R extends EnvironmentRecord> void getLexicalEnvironmentRecord(Variable<? extends R> envRec,
CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironmentRecord);
if (envRec.getType() != Types.EnvironmentRecord) {
mv.checkcast(envRec.getType());
}
mv.store(envRec);
}
/**
* stack: [] {@literal ->} []
*
* @param <R>
* the environment record type
* @param type
* the environment record type
* @param mv
* the code visitor
*/
protected final <R extends EnvironmentRecord> Value<R> getLexicalEnvironmentRecord(Type type, CodeVisitor mv) {
return __ -> {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironmentRecord);
if (type != Types.EnvironmentRecord) {
mv.checkcast(type);
}
};
}
/**
* stack: [] {@literal ->} []
*
* @param envRec
* the variable which holds the variable environment record
* @param mv
* the code visitor
*/
protected final <R extends EnvironmentRecord> void getVariableEnvironmentRecord(Variable<? extends R> envRec,
CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getVariableEnvironmentRecord);
if (envRec.getType() != Types.EnvironmentRecord) {
mv.checkcast(envRec.getType());
}
mv.store(envRec);
}
/**
* stack: [] {@literal ->} []
*
* @param <R>
* the environment record type
* @param type
* the environment record type
* @param mv
* the code visitor
*/
protected final <R extends EnvironmentRecord> Value<R> getVariableEnvironmentRecord(Type type, CodeVisitor mv) {
return __ -> {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getVariableEnvironmentRecord);
if (type != Types.EnvironmentRecord) {
mv.checkcast(type);
}
};
}
/**
* Returns the current environment record type.
*
* @param mv
* the code visitor
* @return the current environment record type
*/
protected final Class<? extends EnvironmentRecord> getEnvironmentRecordClass(CodeVisitor mv) {
Scope scope = mv.getScope();
while (!scope.isPresent()) {
scope = scope.getParent();
}
if (scope instanceof ScriptScope) {
Script script = ((ScriptScope) scope).getNode();
if (!(script.isEvalScript() || script.isScripting())) {
return GlobalEnvironmentRecord.class;
}
} else if (scope instanceof ModuleScope) {
return ModuleEnvironmentRecord.class;
} else if (scope instanceof WithScope) {
return ObjectEnvironmentRecord.class;
}
return DeclarativeEnvironmentRecord.class;
}
/**
* Creates a new object environment.
* <p>
* stack: [obj] {@literal ->} [lexEnv]
*
* @param mv
* the code visitor
* @param withEnvironment
* the withEnvironment flag
*/
protected final void newObjectEnvironment(CodeVisitor mv, boolean withEnvironment) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironment);
mv.iconst(withEnvironment);
mv.invoke(Methods.LexicalEnvironment_newObjectEnvironment);
}
/**
* Creates a new declarative environment.
* <p>
* stack: [] {@literal ->} [lexEnv]
*
* @param mv
* the code visitor
*/
protected final void newDeclarativeEnvironment(BlockScope scope, CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironment);
mv.invoke(Methods.LexicalEnvironment_newDeclarativeEnvironment);
}
/**
* Creates a new declarative environment for a {@code Catch} clause.
* <p>
* stack: [] {@literal ->} [lexEnv]
*
* @param mv
* the code visitor
*/
protected final void newCatchDeclarativeEnvironment(BlockScope scope, CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironment);
mv.invoke(Methods.LexicalEnvironment_newCatchDeclarativeEnvironment);
}
/**
* stack: [] {@literal ->} [lexEnv]
*
* @param mv
* the code visitor
*/
protected final void cloneDeclarativeEnvironment(CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_getLexicalEnvironment);
mv.invoke(Methods.LexicalEnvironment_cloneDeclarativeEnvironment);
}
/**
* stack: [lexEnv] {@literal ->} []
*
* @param mv
* the code visitor
*/
protected final void pushLexicalEnvironment(CodeVisitor mv) {
mv.loadExecutionContext();
mv.swap();
mv.invoke(Methods.ExecutionContext_pushLexicalEnvironment);
}
/**
* Restores the previous lexical environment.
* <p>
* stack: [] {@literal ->} []
*
* @param mv
* the code visitor
*/
protected final void popLexicalEnvironment(CodeVisitor mv) {
mv.loadExecutionContext();
mv.invoke(Methods.ExecutionContext_popLexicalEnvironment);
}
/**
* Emit function call for: {@link LexicalEnvironment#getEnvRec()}
* <p>
* stack: [] {@literal ->} []
*
* @param env
* the variable which holds the lexical environment
* @param envRec
* the variable which holds the environment record
* @param mv
* the instruction visitor
*/
protected final <R extends EnvironmentRecord, R2 extends R> void getEnvRec(
Variable<? extends LexicalEnvironment<? extends R2>> env, Variable<? extends R> envRec,
InstructionVisitor mv) {
mv.load(env);
mv.invoke(Methods.LexicalEnvironment_getEnvRec);
if (envRec.getType() != Types.EnvironmentRecord) {
mv.checkcast(envRec.getType());
}
mv.store(envRec);
}
/**
* Emit function call for: {@link LexicalEnvironment#getEnvRec()}
* <p>
* stack: [env] {@literal ->} [env]
*
* @param env
* the variable which holds the lexical environment
* @param envRec
* the variable which holds the environment record
* @param mv
* the instruction visitor
*/
protected final <R extends EnvironmentRecord> void getEnvRec(Variable<? extends R> envRec, InstructionVisitor mv) {
mv.dup(); // TODO: Remove dup?!
mv.invoke(Methods.LexicalEnvironment_getEnvRec);
if (envRec.getType() != Types.EnvironmentRecord) {
mv.checkcast(envRec.getType());
}
mv.store(envRec);
}
/**
* stack: [object] {@literal ->} [boolean]
*
* @param mv
* the code visitor
*/
protected final void isUndefinedOrNull(CodeVisitor mv) {
mv.invoke(Methods.Type_isUndefinedOrNull);
}
enum ValType {
Undefined, Null, Boolean, Number, Number_int, Number_uint, BigInt, String, Object, Reference, Any, Empty;
public int size() {
switch (this) {
case Number:
case Number_uint:
return 2;
case Number_int:
case BigInt:
case Undefined:
case Null:
case Boolean:
case String:
case Object:
case Reference:
case Any:
return 1;
case Empty:
default:
return 0;
}
}
public boolean isNumeric() {
switch (this) {
case Number:
case Number_int:
case Number_uint:
case BigInt:
return true;
case Undefined:
case Null:
case Boolean:
case String:
case Object:
case Reference:
case Any:
case Empty:
default:
return false;
}
}
public boolean isNumber() {
switch (this) {
case Number:
case Number_int:
case Number_uint:
return true;
case BigInt:
case Undefined:
case Null:
case Boolean:
case String:
case Object:
case Reference:
case Any:
case Empty:
default:
return false;
}
}
public boolean isPrimitive() {
switch (this) {
case Undefined:
case Null:
case Boolean:
case Number:
case Number_int:
case Number_uint:
case BigInt:
case String:
return true;
case Object:
case Reference:
case Any:
case Empty:
default:
return false;
}
}
public boolean isJavaPrimitive() {
switch (this) {
case Boolean:
case Number:
case Number_int:
case Number_uint:
return true;
case BigInt:
case Undefined:
case Null:
case String:
case Object:
case Reference:
case Any:
case Empty:
default:
return false;
}
}
public Class<?> toClass() {
switch (this) {
case Boolean:
return boolean.class;
case String:
return CharSequence.class;
case Number:
return double.class;
case Number_int:
return int.class;
case Number_uint:
return long.class;
case BigInt:
return BigInteger.class;
case Object:
return ScriptObject.class;
case Reference:
return Reference.class;
case Null:
return Null.class;
case Undefined:
return Undefined.class;
case Any:
return Object.class;
case Empty:
default:
throw new AssertionError();
}
}
public Type toType() {
switch (this) {
case Boolean:
return Type.BOOLEAN_TYPE;
case String:
return Types.CharSequence;
case Number:
return Type.DOUBLE_TYPE;
case Number_int:
return Type.INT_TYPE;
case Number_uint:
return Type.LONG_TYPE;
case BigInt:
return Types.BigInteger;
case Object:
return Types.ScriptObject;
case Reference:
return Types.Reference;
case Null:
return Types.Null;
case Undefined:
return Types.Undefined;
case Any:
return Types.Object;
case Empty:
default:
throw new AssertionError();
}
}
public Type toBoxedType() {
switch (this) {
case Boolean:
return Types.Boolean;
case String:
return Types.CharSequence;
case Number:
return Types.Double;
case Number_int:
return Types.Integer;
case Number_uint:
return Types.Long;
case BigInt:
return Types.BigInteger;
case Object:
return Types.ScriptObject;
case Reference:
return Types.Reference;
case Null:
return Types.Null;
case Undefined:
return Types.Undefined;
case Any:
return Types.Object;
case Empty:
default:
throw new AssertionError();
}
}
public static ValType of(Type type) {
if (type.isPrimitive()) {
if (Type.BOOLEAN_TYPE.equals(type)) {
return ValType.Boolean;
}
if (Type.INT_TYPE.equals(type)) {
return ValType.Number_int;
}
if (Type.LONG_TYPE.equals(type)) {
return ValType.Number_uint;
}
if (Type.DOUBLE_TYPE.equals(type)) {
return ValType.Number;
}
return ValType.Any;
}
if (Types.Boolean.equals(type)) {
return ValType.Boolean;
}
if (Types.Integer.equals(type)) {
return ValType.Number_int;
}
if (Types.Long.equals(type)) {
return ValType.Number_uint;
}
if (Types.Double.equals(type)) {
return ValType.Number;
}
if (Types.BigInteger.equals(type)) {
return ValType.BigInt;
}
if (Types.Null.equals(type)) {
return ValType.Null;
}
if (Types.Undefined.equals(type)) {
return ValType.Undefined;
}
if (Types.String.equals(type) || Types.CharSequence.equals(type)) {
return ValType.String;
}
if (Types.ScriptObject.equals(type)) {
return ValType.Object;
}
if (Types.OrdinaryObject.equals(type)) {
return ValType.Object;
}
return ValType.Any;
}
}
/**
* stack: [Object] {@literal ->} [boolean]
*
* @param from
* the input value type
* @param mv
* the code visitor
* @return the returned value type
*/
protected static final ValType ToPrimitive(ValType from, CodeVisitor mv) {
switch (from) {
case Number:
case Number_int:
case Number_uint:
case BigInt:
case Undefined:
case Null:
case Boolean:
case String:
return from;
case Object:
case Any:
mv.loadExecutionContext();
mv.swap();
mv.invoke(Methods.AbstractOperations_ToPrimitive);
return ValType.Any;
case Empty:
case Reference:
default:
throw new AssertionError();
}
}
/**
* stack: [Object] {@literal ->} [boolean]
*
* @param from
* the input value type
* @param mv
* the code visitor
*/
protected final void ToBoolean(ValType from, CodeVisitor mv) {
switch (from) {
case Number:
mv.invoke(Methods.AbstractOperations_ToBoolean_double);
return;
case Number_int:
mv.invoke(Methods.AbstractOperations_ToBoolean_int);
return;
case Number_uint:
mv.invoke(Methods.AbstractOperations_ToBoolean_long);
return;
case BigInt:
mv.invoke(Methods.AbstractOperations_ToBoolean_BigInteger);
return;
case Undefined:
case Null:
mv.pop();
mv.iconst(false);
return;
case Boolean:
return;
case String: {
Jump l0 = new Jump(), l1 = new Jump();
mv.invoke(Methods.CharSequence_length);
mv.ifeq(l0);
mv.iconst(true);