forked from PureKrome/EcmaScript.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cs
More file actions
4663 lines (3880 loc) · 190 KB
/
Copy pathInterpreter.cs
File metadata and controls
4663 lines (3880 loc) · 190 KB
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
//------------------------------------------------------------------------------
// <license file="Interpreter.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using EcmaScript.NET.Debugging;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
public class Interpreter
{
// Additional interpreter-specific codes
const int Icode_DUP = -1;
const int Icode_DUP2 = -2;
const int Icode_SWAP = -3;
const int Icode_POP = -4;
const int Icode_POP_RESULT = -5;
const int Icode_IFEQ_POP = -6;
const int Icode_VAR_INC_DEC = -7;
const int Icode_NAME_INC_DEC = -8;
const int Icode_PROP_INC_DEC = -9;
const int Icode_ELEM_INC_DEC = -10;
const int Icode_REF_INC_DEC = -11;
const int Icode_SCOPE_LOAD = -12;
const int Icode_SCOPE_SAVE = -13;
const int Icode_TYPEOFNAME = -14;
const int Icode_NAME_AND_THIS = -15;
const int Icode_PROP_AND_THIS = -16;
const int Icode_ELEM_AND_THIS = -17;
const int Icode_VALUE_AND_THIS = -18;
const int Icode_CLOSURE_EXPR = -19;
const int Icode_CLOSURE_STMT = -20;
const int Icode_CALLSPECIAL = -21;
const int Icode_RETUNDEF = -22;
const int Icode_GOSUB = -23;
const int Icode_STARTSUB = -24;
const int Icode_RETSUB = -25;
const int Icode_LINE = -26;
const int Icode_SHORTNUMBER = -27;
const int Icode_INTNUMBER = -28;
const int Icode_LITERAL_NEW = -29;
const int Icode_LITERAL_SET = -30;
const int Icode_SPARE_ARRAYLIT = -31;
const int Icode_REG_IND_C0 = -32;
const int Icode_REG_IND_C1 = -33;
const int Icode_REG_IND_C2 = -34;
const int Icode_REG_IND_C3 = -35;
const int Icode_REG_IND_C4 = -36;
const int Icode_REG_IND_C5 = -37;
const int Icode_REG_IND1 = -38;
const int Icode_REG_IND2 = -39;
const int Icode_REG_IND4 = -40;
const int Icode_REG_STR_C0 = -41;
const int Icode_REG_STR_C1 = -42;
const int Icode_REG_STR_C2 = -43;
const int Icode_REG_STR_C3 = -44;
const int Icode_REG_STR1 = -45;
const int Icode_REG_STR2 = -46;
const int Icode_REG_STR4 = -47;
const int Icode_GETVAR1 = -48;
const int Icode_SETVAR1 = -49;
const int Icode_UNDEF = -50;
const int Icode_ZERO = -51;
const int Icode_ONE = -52;
const int Icode_ENTERDQ = -53;
const int Icode_LEAVEDQ = -54;
const int Icode_TAIL_CALL = -55;
const int Icode_LOCAL_CLEAR = -56;
const int Icode_DEBUGGER = -57;
// Last icode
const int MIN_ICODE = -57;
// data for parsing
CompilerEnvirons compilerEnv;
bool itsInFunctionFlag;
InterpreterData itsData;
ScriptOrFnNode scriptOrFn;
int itsICodeTop;
int itsStackDepth;
int itsLineNumber;
int itsDoubleTableTop;
ObjToIntMap itsStrings = new ObjToIntMap (20);
int itsLocalTop;
const int MIN_LABEL_TABLE_SIZE = 32;
const int MIN_FIXUP_TABLE_SIZE = 40;
int [] itsLabelTable;
int itsLabelTableTop;
// itsFixupTable[i] = (label_index << 32) | fixup_site
long [] itsFixupTable;
int itsFixupTableTop;
ObjArray itsLiteralIds = new ObjArray ();
int itsExceptionTableTop;
const int EXCEPTION_TRY_START_SLOT = 0;
const int EXCEPTION_TRY_END_SLOT = 1;
const int EXCEPTION_HANDLER_SLOT = 2;
const int EXCEPTION_TYPE_SLOT = 3;
const int EXCEPTION_LOCAL_SLOT = 4;
const int EXCEPTION_SCOPE_SLOT = 5;
// SLOT_SIZE: space for try start/end, handler, start, handler type,
// exception local and scope local
const int EXCEPTION_SLOT_SIZE = 6;
// ECF_ or Expression Context Flags constants: for now only TAIL is available
const int ECF_TAIL = 1 << 0;
internal class CallFrame : System.ICloneable
{
internal CallFrame parentFrame;
// amount of stack frames before this one on the interpretation stack
internal int frameIndex;
// If true indicates read-only frame that is a part of continuation
internal bool frozen;
internal InterpretedFunction fnOrScript;
internal InterpreterData idata;
// Stack structure
// stack[0 <= i < localShift]: arguments and local variables
// stack[localShift <= i <= emptyStackTop]: used for local temporaries
// stack[emptyStackTop < i < stack.length]: stack data
// sDbl[i]: if stack[i] is UniqueTag.DoubleMark, sDbl[i] holds the number value
internal object [] stack;
internal double [] sDbl;
internal CallFrame varSource; // defaults to this unless continuation frame
internal int localShift;
internal int emptyStackTop;
internal DebugFrame debuggerFrame;
internal bool useActivation;
internal IScriptable thisObj;
internal IScriptable [] scriptRegExps;
// The values that change during interpretation
internal object result;
internal double resultDbl;
internal int pc;
internal int pcPrevBranch;
internal int pcSourceLineStart;
internal IScriptable scope;
internal int savedStackTop;
internal int savedCallOp;
internal virtual CallFrame cloneFrozen ()
{
if (!frozen)
Context.CodeBug ();
CallFrame copy = (CallFrame)Clone ();
// clone stack but keep varSource to point to values
// from this frame to share variables.
// TODO: STACK
copy.stack = new object [stack.Length];
stack.CopyTo (copy.stack, 0);
copy.sDbl = new double [sDbl.Length];
sDbl.CopyTo (copy.sDbl, 0);
copy.frozen = false;
return copy;
}
virtual public object Clone ()
{
return base.MemberwiseClone ();
}
}
sealed class ContinuationJump
{
internal CallFrame capturedFrame;
internal CallFrame branchFrame;
internal object result;
internal double resultDbl;
internal ContinuationJump (Continuation c, CallFrame current)
{
this.capturedFrame = (CallFrame)c.Implementation;
if (this.capturedFrame == null || current == null) {
// Continuation and current execution does not share
// any frames if there is nothing to capture or
// if there is no currently executed frames
this.branchFrame = null;
}
else {
// Search for branch frame where parent frame chains starting
// from captured and current meet.
CallFrame chain1 = this.capturedFrame;
CallFrame chain2 = current;
// First work parents of chain1 or chain2 until the same
// frame depth.
int diff = chain1.frameIndex - chain2.frameIndex;
if (diff != 0) {
if (diff < 0) {
// swap to make sure that
// chain1.frameIndex > chain2.frameIndex and diff > 0
chain1 = current;
chain2 = this.capturedFrame;
diff = -diff;
}
do {
chain1 = chain1.parentFrame;
}
while (--diff != 0);
if (chain1.frameIndex != chain2.frameIndex)
Context.CodeBug ();
}
// Now walk parents in parallel until a shared frame is found
// or until the root is reached.
while (chain1 != chain2 && chain1 != null) {
chain1 = chain1.parentFrame;
chain2 = chain2.parentFrame;
}
this.branchFrame = chain1;
if (this.branchFrame != null && !this.branchFrame.frozen)
Context.CodeBug ();
}
}
}
static string bytecodeName (int bytecode)
{
if (!validBytecode (bytecode)) {
throw new ArgumentException (Convert.ToString (bytecode));
}
if (!Token.printICode) {
return Convert.ToString (bytecode);
}
if (ValidTokenCode (bytecode)) {
return Token.GetName (bytecode);
}
switch (bytecode) {
case Icode_DUP:
return "DUP";
case Icode_DUP2:
return "DUP2";
case Icode_SWAP:
return "SWAP";
case Icode_POP:
return "POP";
case Icode_POP_RESULT:
return "POP_RESULT";
case Icode_IFEQ_POP:
return "IFEQ_POP";
case Icode_VAR_INC_DEC:
return "VAR_INC_DEC";
case Icode_NAME_INC_DEC:
return "NAME_INC_DEC";
case Icode_PROP_INC_DEC:
return "PROP_INC_DEC";
case Icode_ELEM_INC_DEC:
return "ELEM_INC_DEC";
case Icode_REF_INC_DEC:
return "REF_INC_DEC";
case Icode_SCOPE_LOAD:
return "SCOPE_LOAD";
case Icode_SCOPE_SAVE:
return "SCOPE_SAVE";
case Icode_TYPEOFNAME:
return "TYPEOFNAME";
case Icode_NAME_AND_THIS:
return "NAME_AND_THIS";
case Icode_PROP_AND_THIS:
return "PROP_AND_THIS";
case Icode_ELEM_AND_THIS:
return "ELEM_AND_THIS";
case Icode_VALUE_AND_THIS:
return "VALUE_AND_THIS";
case Icode_CLOSURE_EXPR:
return "CLOSURE_EXPR";
case Icode_CLOSURE_STMT:
return "CLOSURE_STMT";
case Icode_CALLSPECIAL:
return "CALLSPECIAL";
case Icode_RETUNDEF:
return "RETUNDEF";
case Icode_GOSUB:
return "GOSUB";
case Icode_STARTSUB:
return "STARTSUB";
case Icode_RETSUB:
return "RETSUB";
case Icode_LINE:
return "LINE";
case Icode_SHORTNUMBER:
return "SHORTNUMBER";
case Icode_INTNUMBER:
return "INTNUMBER";
case Icode_LITERAL_NEW:
return "LITERAL_NEW";
case Icode_LITERAL_SET:
return "LITERAL_SET";
case Icode_SPARE_ARRAYLIT:
return "SPARE_ARRAYLIT";
case Icode_REG_IND_C0:
return "REG_IND_C0";
case Icode_REG_IND_C1:
return "REG_IND_C1";
case Icode_REG_IND_C2:
return "REG_IND_C2";
case Icode_REG_IND_C3:
return "REG_IND_C3";
case Icode_REG_IND_C4:
return "REG_IND_C4";
case Icode_REG_IND_C5:
return "REG_IND_C5";
case Icode_REG_IND1:
return "LOAD_IND1";
case Icode_REG_IND2:
return "LOAD_IND2";
case Icode_REG_IND4:
return "LOAD_IND4";
case Icode_REG_STR_C0:
return "REG_STR_C0";
case Icode_REG_STR_C1:
return "REG_STR_C1";
case Icode_REG_STR_C2:
return "REG_STR_C2";
case Icode_REG_STR_C3:
return "REG_STR_C3";
case Icode_REG_STR1:
return "LOAD_STR1";
case Icode_REG_STR2:
return "LOAD_STR2";
case Icode_REG_STR4:
return "LOAD_STR4";
case Icode_GETVAR1:
return "GETVAR1";
case Icode_SETVAR1:
return "SETVAR1";
case Icode_UNDEF:
return "UNDEF";
case Icode_ZERO:
return "ZERO";
case Icode_ONE:
return "ONE";
case Icode_ENTERDQ:
return "ENTERDQ";
case Icode_LEAVEDQ:
return "LEAVEDQ";
case Icode_TAIL_CALL:
return "TAIL_CALL";
case Icode_LOCAL_CLEAR:
return "LOCAL_CLEAR";
case Icode_DEBUGGER:
return "DEBUGGER";
}
// icode without name
throw new ApplicationException (Convert.ToString (bytecode));
}
static bool validIcode (int icode)
{
return MIN_ICODE <= icode && icode <= -1;
}
static bool ValidTokenCode (int token)
{
return Token.FIRST_BYTECODE_TOKEN <= token && token <= Token.LAST_BYTECODE_TOKEN;
}
static bool validBytecode (int bytecode)
{
return validIcode (bytecode) || ValidTokenCode (bytecode);
}
public virtual object Compile (CompilerEnvirons compilerEnv, ScriptOrFnNode tree, string encodedSource, bool returnFunction)
{
this.compilerEnv = compilerEnv;
new NodeTransformer ().transform (tree);
if (Token.printTrees) {
System.Console.Out.WriteLine (tree.toStringTree (tree));
}
if (returnFunction) {
tree = tree.getFunctionNode (0);
}
scriptOrFn = tree;
itsData = new InterpreterData (compilerEnv.LanguageVersion, scriptOrFn.SourceName, encodedSource);
itsData.topLevel = true;
if (returnFunction) {
generateFunctionICode ();
}
else {
generateICodeFromTree (scriptOrFn);
}
return itsData;
}
public virtual IScript CreateScriptObject (object bytecode, object staticSecurityDomain)
{
var idata = (InterpreterData)bytecode;
return InterpretedFunction.createScript (itsData, staticSecurityDomain);
}
public virtual IFunction CreateFunctionObject (Context cx, IScriptable scope, object bytecode, object staticSecurityDomain)
{
var idata = (InterpreterData)bytecode;
return InterpretedFunction.createFunction (cx, scope, itsData, staticSecurityDomain);
}
void generateFunctionICode ()
{
itsInFunctionFlag = true;
var theFunction = (FunctionNode)scriptOrFn;
itsData.itsFunctionType = theFunction.FunctionType;
itsData.itsNeedsActivation = theFunction.RequiresActivation;
itsData.itsName = theFunction.FunctionName;
if (!theFunction.IgnoreDynamicScope && compilerEnv.UseDynamicScope)
{
itsData.useDynamicScope = true;
}
generateICodeFromTree (theFunction.LastChild);
}
void generateICodeFromTree (Node tree)
{
generateNestedFunctions ();
generateRegExpLiterals ();
VisitStatement (tree);
fixLabelGotos ();
// add RETURN_RESULT only to scripts as function always ends with RETURN
if (itsData.itsFunctionType == 0) {
addToken (Token.RETURN_RESULT);
}
if (itsData.itsICode.Length != itsICodeTop) {
// Make itsData.itsICode length exactly itsICodeTop to save memory
// and catch bugs with jumps beyound icode as early as possible
var tmp = new sbyte [itsICodeTop];
Array.Copy (itsData.itsICode, 0, tmp, 0, itsICodeTop);
itsData.itsICode = tmp;
}
if (itsStrings.size () == 0) {
itsData.itsStringTable = null;
}
else {
itsData.itsStringTable = new string [itsStrings.size ()];
ObjToIntMap.Iterator iter = itsStrings.newIterator ();
for (iter.start (); !iter.done (); iter.next ()) {
string str = (string)iter.Key;
int index = iter.Value;
if (itsData.itsStringTable [index] != null)
Context.CodeBug ();
itsData.itsStringTable [index] = str;
}
}
if (itsDoubleTableTop == 0) {
itsData.itsDoubleTable = null;
}
else if (itsData.itsDoubleTable.Length != itsDoubleTableTop) {
double [] tmp = new double [itsDoubleTableTop];
Array.Copy (itsData.itsDoubleTable, 0, tmp, 0, itsDoubleTableTop);
itsData.itsDoubleTable = tmp;
}
if (itsExceptionTableTop != 0 && itsData.itsExceptionTable.Length != itsExceptionTableTop) {
int [] tmp = new int [itsExceptionTableTop];
Array.Copy (itsData.itsExceptionTable, 0, tmp, 0, itsExceptionTableTop);
itsData.itsExceptionTable = tmp;
}
itsData.itsMaxVars = scriptOrFn.ParamAndVarCount;
// itsMaxFrameArray: interpret method needs this amount for its
// stack and sDbl arrays
itsData.itsMaxFrameArray = itsData.itsMaxVars + itsData.itsMaxLocals + itsData.itsMaxStack;
itsData.argNames = scriptOrFn.ParamAndVarNames;
itsData.argCount = scriptOrFn.ParamCount;
itsData.encodedSourceStart = scriptOrFn.EncodedSourceStart;
itsData.encodedSourceEnd = scriptOrFn.EncodedSourceEnd;
if (itsLiteralIds.GetSize () != 0) {
itsData.literalIds = itsLiteralIds.ToArray ();
}
if (Token.printICode)
dumpICode (itsData);
}
void generateNestedFunctions ()
{
int functionCount = scriptOrFn.FunctionCount;
if (functionCount == 0)
return;
InterpreterData [] array = new InterpreterData [functionCount];
for (int i = 0; i != functionCount; i++) {
FunctionNode def = scriptOrFn.getFunctionNode (i);
Interpreter jsi = new Interpreter ();
jsi.compilerEnv = compilerEnv;
jsi.scriptOrFn = def;
jsi.itsData = new InterpreterData (itsData);
jsi.generateFunctionICode ();
array [i] = jsi.itsData;
}
itsData.itsNestedFunctions = array;
}
void generateRegExpLiterals ()
{
int N = scriptOrFn.RegexpCount;
if (N == 0)
return;
Context cx = Context.CurrentContext;
RegExpProxy rep = cx.RegExpProxy;
object [] array = new object [N];
for (int i = 0; i != N; i++) {
string str = scriptOrFn.getRegexpString (i);
string flags = scriptOrFn.getRegexpFlags (i);
array [i] = rep.Compile (cx, str, flags);
}
itsData.itsRegExpLiterals = array;
}
void updateLineNumber (Node node)
{
int lineno = node.Lineno;
if (lineno != itsLineNumber && lineno >= 0) {
if (itsData.firstLinePC < 0) {
itsData.firstLinePC = lineno;
}
itsLineNumber = lineno;
addIcode (Icode_LINE);
addUint16 (lineno & 0xFFFF);
}
}
ApplicationException badTree (Node node)
{
throw new ApplicationException (node.ToString ());
}
void VisitStatement (Node node)
{
int type = node.Type;
Node child = node.FirstChild;
switch (type) {
case Token.FUNCTION: {
int fnIndex = node.getExistingIntProp (Node.FUNCTION_PROP);
int fnType = scriptOrFn.getFunctionNode (fnIndex).FunctionType;
// Only function expressions or function expression
// statements needs closure code creating new function
// object on stack as function statements are initialized
// at script/function start
// In addition function expression can not present here
// at statement level, they must only present as expressions.
if (fnType == FunctionNode.FUNCTION_EXPRESSION_STATEMENT) {
addIndexOp (Icode_CLOSURE_STMT, fnIndex);
}
else {
if (fnType != FunctionNode.FUNCTION_STATEMENT) {
throw Context.CodeBug ();
}
}
}
break;
case Token.SCRIPT:
case Token.LABEL:
case Token.LOOP:
case Token.BLOCK:
case Token.EMPTY:
case Token.WITH:
updateLineNumber (node);
while (child != null) {
VisitStatement (child);
child = child.Next;
}
break;
case Token.ENTERWITH:
VisitExpression (child, 0);
addToken (Token.ENTERWITH);
stackChange (-1);
break;
case Token.LEAVEWITH:
addToken (Token.LEAVEWITH);
break;
case Token.LOCAL_BLOCK: {
int local = allocLocal ();
node.putIntProp (Node.LOCAL_PROP, local);
updateLineNumber (node);
while (child != null) {
VisitStatement (child);
child = child.Next;
}
addIndexOp (Icode_LOCAL_CLEAR, local);
releaseLocal (local);
}
break;
case Token.DEBUGGER:
updateLineNumber (node);
addIcode (Icode_DEBUGGER);
break;
case Token.SWITCH:
updateLineNumber (node); {
// See comments in IRFactory.createSwitch() for description
// of SWITCH node
Node switchNode = (Node.Jump)node;
VisitExpression (child, 0);
for (Node.Jump caseNode = (Node.Jump)child.Next; caseNode != null; caseNode = (Node.Jump)caseNode.Next) {
if (caseNode.Type != Token.CASE)
throw badTree (caseNode);
var test = caseNode.FirstChild;
addIcode (Icode_DUP);
stackChange (1);
VisitExpression (test, 0);
addToken (Token.SHEQ);
stackChange (-1);
// If true, Icode_IFEQ_POP will jump and remove case
// value from stack
addGoto (caseNode.target, Icode_IFEQ_POP);
stackChange (-1);
}
addIcode (Icode_POP);
stackChange (-1);
}
break;
case Token.TARGET:
markTargetLabel (node);
break;
case Token.IFEQ:
case Token.IFNE: {
Node target = ((Node.Jump)node).target;
VisitExpression (child, 0);
addGoto (target, type);
stackChange (-1);
}
break;
case Token.GOTO: {
Node target = ((Node.Jump)node).target;
addGoto (target, type);
}
break;
case Token.JSR: {
Node target = ((Node.Jump)node).target;
addGoto (target, Icode_GOSUB);
}
break;
case Token.FINALLY: {
// Account for incomming GOTOSUB address
stackChange (1);
int finallyRegister = getLocalBlockRef (node);
addIndexOp (Icode_STARTSUB, finallyRegister);
stackChange (-1);
while (child != null) {
VisitStatement (child);
child = child.Next;
}
addIndexOp (Icode_RETSUB, finallyRegister);
}
break;
case Token.EXPR_VOID:
case Token.EXPR_RESULT:
updateLineNumber (node);
VisitExpression (child, 0);
addIcode ((type == Token.EXPR_VOID) ? Icode_POP : Icode_POP_RESULT);
stackChange (-1);
break;
case Token.TRY: {
Node.Jump tryNode = (Node.Jump)node;
int exceptionObjectLocal = getLocalBlockRef (tryNode);
int scopeLocal = allocLocal ();
addIndexOp (Icode_SCOPE_SAVE, scopeLocal);
int tryStart = itsICodeTop;
while (child != null) {
VisitStatement (child);
child = child.Next;
}
Node catchTarget = tryNode.target;
if (catchTarget != null) {
int catchStartPC = itsLabelTable [getTargetLabel (catchTarget)];
addExceptionHandler (tryStart, catchStartPC, catchStartPC, false, exceptionObjectLocal, scopeLocal);
}
Node finallyTarget = tryNode.Finally;
if (finallyTarget != null) {
int finallyStartPC = itsLabelTable [getTargetLabel (finallyTarget)];
addExceptionHandler (tryStart, finallyStartPC, finallyStartPC, true, exceptionObjectLocal, scopeLocal);
}
addIndexOp (Icode_LOCAL_CLEAR, scopeLocal);
releaseLocal (scopeLocal);
}
break;
case Token.CATCH_SCOPE: {
int localIndex = getLocalBlockRef (node);
int scopeIndex = node.getExistingIntProp (Node.CATCH_SCOPE_PROP);
string name = child.String;
child = child.Next;
VisitExpression (child, 0); // load expression object
addStringPrefix (name);
addIndexPrefix (localIndex);
addToken (Token.CATCH_SCOPE);
addUint8 (scopeIndex != 0 ? 1 : 0);
stackChange (-1);
}
break;
case Token.THROW:
updateLineNumber (node);
VisitExpression (child, 0);
addToken (Token.THROW);
addUint16 (itsLineNumber & 0xFFFF);
stackChange (-1);
break;
case Token.RETHROW:
updateLineNumber (node);
addIndexOp (Token.RETHROW, getLocalBlockRef (node));
break;
case Token.RETURN:
updateLineNumber (node);
if (child != null) {
VisitExpression (child, ECF_TAIL);
addToken (Token.RETURN);
stackChange (-1);
}
else {
addIcode (Icode_RETUNDEF);
}
break;
case Token.RETURN_RESULT:
updateLineNumber (node);
addToken (Token.RETURN_RESULT);
break;
case Token.ENUM_INIT_KEYS:
case Token.ENUM_INIT_VALUES:
VisitExpression (child, 0);
addIndexOp (type, getLocalBlockRef (node));
stackChange (-1);
break;
default:
throw badTree (node);
}
if (itsStackDepth != 0) {
throw Context.CodeBug ();
}
}
bool VisitExpressionOptimized (Node node, int contextFlags)
{
return false;
#if FALKSE
if (node.Type == Token.ADD) {
Node next = node.Next;
if (next == null)
return false;
switch (next.Type) {
case Token.NAME:
case Token.STRING:
return true;
}
}
return false;
#endif
}
void VisitExpression (Node node, int contextFlags)
{
if (VisitExpressionOptimized (node, contextFlags))
return;
int type = node.Type;
Node child = node.FirstChild;
int savedStackDepth = itsStackDepth;
switch (type) {
case Token.FUNCTION: {
int fnIndex = node.getExistingIntProp (Node.FUNCTION_PROP);
FunctionNode fn = scriptOrFn.getFunctionNode (fnIndex);
// See comments in visitStatement for Token.FUNCTION case
switch (fn.FunctionType) {
case FunctionNode.FUNCTION_EXPRESSION:
addIndexOp (Icode_CLOSURE_EXPR, fnIndex);
break;
default:
throw Context.CodeBug ();
}
stackChange (1);
}
break;
case Token.LOCAL_LOAD: {
int localIndex = getLocalBlockRef (node);
addIndexOp (Token.LOCAL_LOAD, localIndex);
stackChange (1);
}
break;
case Token.COMMA: {
Node lastChild = node.LastChild;
while (child != lastChild) {
VisitExpression (child, 0);
addIcode (Icode_POP);
stackChange (-1);
child = child.Next;
}
// Preserve tail context flag if any
VisitExpression (child, contextFlags & ECF_TAIL);
}
break;
case Token.USE_STACK:
// Indicates that stack was modified externally,
// like placed catch object
stackChange (1);
break;
case Token.REF_CALL:
case Token.CALL:
case Token.NEW: {
if (type == Token.NEW) {
VisitExpression (child, 0);
}
else {
generateCallFunAndThis (child);
}
int argCount = 0;
while ((child = child.Next) != null) {
VisitExpression (child, 0);
++argCount;
}
int callType = node.getIntProp (Node.SPECIALCALL_PROP, Node.NON_SPECIALCALL);
if (callType != Node.NON_SPECIALCALL) {
// embed line number and source filename
addIndexOp (Icode_CALLSPECIAL, argCount);
addUint8 (callType);
addUint8 (type == Token.NEW ? 1 : 0);
addUint16 (itsLineNumber & 0xFFFF);
}
else {
if (type == Token.CALL) {
if ((contextFlags & ECF_TAIL) != 0) {
type = Icode_TAIL_CALL;
}
}
addIndexOp (type, argCount);
}
// adjust stack
if (type == Token.NEW) {
// new: f, args -> result
stackChange (-argCount);
}
else {
// call: f, thisObj, args -> result
// ref_call: f, thisObj, args -> ref