Debian Codegraph @

terps/tads/tads3/tcjsdrv.h

raw symbols SCIP
1 /* Copyright (c) 2009 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 tcjsdrv.h - tads 3 compiler - javascript code generator - parse node classes
5 Function
6
7 Notes
8
9 Modified
10 02/17/09 MJRoberts - Creation
11 */
12
13 #ifndef TCJSDRV_H
14 #define TCJSDRV_H
15
16 #include "tcjsint.h"
17 #include "tcpndrv.h"
18
19 /* ------------------------------------------------------------------------ */
20 /*
21 * "self"
22 */
23 class CTPNSelf: public CTPNSelfBase
24 {
25 public:
26 /* generate code */
27 void gen_code(int discard, int for_condition);
28
29 /* generate a member expression */
30 void gen_code_member(int discard,
31 class CTcPrsNode *prop_expr, int prop_is_expr,
32 int argc, int varargs);
33
34 /* get the object value for a '.' expression */
35 vm_obj_id_t gen_code_obj_predot(int *is_self);
36 };
37
38 /* ------------------------------------------------------------------------ */
39 /*
40 * "replaced"
41 */
42 class CTPNReplaced: public CTPNReplacedBase
43 {
44 public:
45 /* generate code */
46 void gen_code(int discard, int for_condition);
47
48 /* generate a function call expression */
49 void gen_code_call(int discard, int argc, int varargs);
50 };
51
52 /* ------------------------------------------------------------------------ */
53 /*
54 * "targetprop"
55 */
56 class CTPNTargetprop: public CTPNTargetpropBase
57 {
58 public:
59 /* generate code */
60 void gen_code(int discard, int for_condition);
61 };
62
63
64 /* ------------------------------------------------------------------------ */
65 /*
66 * "targetobj"
67 */
68 class CTPNTargetobj: public CTPNTargetobjBase
69 {
70 public:
71 /* generate code */
72 void gen_code(int discard, int for_condition);
73 };
74
75 /* ------------------------------------------------------------------------ */
76 /*
77 * "definingobj"
78 */
79 class CTPNDefiningobj: public CTPNDefiningobjBase
80 {
81 public:
82 /* generate code */
83 void gen_code(int discard, int for_condition);
84 };
85
86
87 /* ------------------------------------------------------------------------ */
88 /*
89 * "inherited"
90 */
91 class CTPNInh: public CTPNInhBase
92 {
93 public:
94 /* generate code */
95 void gen_code(int discard, int for_condition);
96
97 /* generate a member expression */
98 void gen_code_member(int discard,
99 class CTcPrsNode *prop_expr, int prop_is_expr,
100 int argc, int varargs);
101 };
102
103 /*
104 * "inherited" with explicit superclass
105 */
106 class CTPNInhClass: public CTPNInhClassBase
107 {
108 public:
109 CTPNInhClass(const char *sym, size_t len)
110 : CTPNInhClassBase(sym, len) { }
111
112 /* generate code */
113 void gen_code(int discard, int for_condition);
114
115 /* generate a member expression */
116 void gen_code_member(int discard,
117 class CTcPrsNode *prop_expr, int prop_is_expr,
118 int argc, int varargs);
119 };
120
121 /*
122 * "delegated"
123 */
124 class CTPNDelegated: public CTPNDelegatedBase
125 {
126 public:
127 CTPNDelegated(CTcPrsNode *delegatee)
128 : CTPNDelegatedBase(delegatee) { }
129
130 /* generate code */
131 void gen_code(int discard, int for_condition);
132
133 /* generate a member expression */
134 void gen_code_member(int discard,
135 class CTcPrsNode *prop_expr, int prop_is_expr,
136 int argc, int varargs);
137 };
138
139
140 /* ------------------------------------------------------------------------ */
141 /*
142 * "argcount" node
143 */
144 class CTPNArgc: public CTPNArgcBase
145 {
146 public:
147 void gen_code(int discard, int for_condition);
148 };
149
150
151 /* ------------------------------------------------------------------------ */
152 /*
153 * constant node
154 */
155 class CTPNConst: public CTPNConstBase
156 {
157 public:
158 CTPNConst(CTcConstVal *val) : CTPNConstBase(val) { }
159
160 /* generate code for the constant */
161 void gen_code(int discard, int for_condition);
162
163 /* generate code for operator 'new' applied to this expression */
164 void gen_code_new(int discard, int argc, int varargs, int from_call,
165 int is_transient);
166
167 /* evaluate a property ID */
168 vm_prop_id_t gen_code_propid(int check_only, int is_expr);
169
170 /* generate a member expression */
171 void gen_code_member(int discard,
172 class CTcPrsNode *prop_expr, int prop_is_expr,
173 int argc, int varargs);
174
175 /* generate a function call expression */
176 void gen_code_call(int discard, int argc, int varargs);
177
178 /* get the object value for a '.' expression */
179 vm_obj_id_t gen_code_obj_predot(int *is_self);
180
181 /* generate code to push an integer constant */
182 static void s_gen_code_int(class js_expr_buf &expr, long intval);
183
184 /* generate code into an expression buffer */
185 void gen_code(class js_expr_buf &expr);
186 };
187
188 /*
189 * Debugger constant node
190 */
191 class CTPNDebugConst: public CTPNConst
192 {
193 public:
194 CTPNDebugConst(CTcConstVal *val) : CTPNConst(val) { }
195
196 /* generate code for the constant */
197 void gen_code(int discard, int for_condition);
198 };
199
200 /* ------------------------------------------------------------------------ */
201 /*
202 * Unary Operators
203 */
204
205 /* bitwise NOT */
206 CTPNUnary_def(CTPNBNot);
207
208 /* arithmetic positive */
209 CTPNUnary_def(CTPNPos);
210
211 /* arithmetic negative */
212 CTPNUnary_def(CTPNNeg);
213
214 /* pre-increment */
215 CTPNUnary_side_def(CTPNPreInc);
216
217 /* pre-decrement */
218 CTPNUnary_side_def(CTPNPreDec);
219
220 /* post-increment */
221 CTPNUnary_side_def(CTPNPostInc);
222
223 /* post-decrement */
224 CTPNUnary_side_def(CTPNPostDec);
225
226 /* delete */
227 CTPNUnary_side_def(CTPNDelete);
228
229 /* boolean-ize */
230 CTPNUnary_def(CTPNBoolize);
231
232 /* ------------------------------------------------------------------------ */
233 /*
234 * NEW operator
235 */
236 class CTPNNew: public CTPNUnary
237 {
238 public:
239 CTPNNew(class CTcPrsNode *sub, int is_transient)
240 : CTPNUnary(sub)
241 {
242 /* remember 'transient' status */
243 transient_ = is_transient;
244 }
245
246 /* generate code */
247 void gen_code(int discard, int for_condition);
248
249 /* note that we have side effects */
250 virtual int has_side_effects() const { return TRUE; }
251
252 protected:
253 /* flag: it's a 'new transient' operator */
254 int transient_;
255 };
256
257
258 /* ------------------------------------------------------------------------ */
259 /*
260 * NOT operator
261 */
262 class CTPNNot: public CTPNNotBase
263 {
264 public:
265 CTPNNot(CTcPrsNode *sub) : CTPNNotBase(sub) { }
266
267 /* generate code */
268 void gen_code(int discard, int for_condition);
269 };
270
271
272 /* ------------------------------------------------------------------------ */
273 /*
274 * Binary Operators
275 */
276
277 class CTPNComma: public CTPNCommaBase
278 {
279 public:
280 CTPNComma(class CTcPrsNode *lhs, CTcPrsNode *rhs)
281 : CTPNCommaBase(lhs, rhs) { }
282
283 /* generate code */
284 void gen_code(int discard, int for_condition);
285 };
286
287 /* bitwise OR */
288 CTPNBin_def(CTPNBOr);
289
290 /* bitwise AND */
291 CTPNBin_def(CTPNBAnd);
292
293 /* bitwise XOR */
294 CTPNBin_def(CTPNBXor);
295
296 /* greater than */
297 CTPNBin_def(CTPNGt);
298
299 /* greater or equal */
300 CTPNBin_def(CTPNGe);
301
302 /* less than */
303 CTPNBin_def(CTPNLt);
304
305 /* less or equal */
306 CTPNBin_def(CTPNLe);
307
308 /* bit shift left */
309 CTPNBin_def(CTPNShl);
310
311 /* bit shift right */
312 CTPNBin_def(CTPNShr);
313
314 /* multiply */
315 CTPNBin_def(CTPNMul);
316
317 /* divide */
318 CTPNBin_def(CTPNDiv);
319
320 /* modulo */
321 CTPNBin_def(CTPNMod);
322
323 /* ------------------------------------------------------------------------ */
324 /*
325 * Addition
326 */
327
328 class CTPNAdd: public CTPNAddBase
329 {
330 public:
331 CTPNAdd(class CTcPrsNode *left, class CTcPrsNode *right)
332 : CTPNAddBase(left, right) { }
333
334 /* generate code */
335 void gen_code(int discard, int for_condition);
336 };
337
338
339 /* ------------------------------------------------------------------------ */
340 /*
341 * Subtraction
342 */
343
344 class CTPNSub: public CTPNSubBase
345 {
346 public:
347 CTPNSub(class CTcPrsNode *left, class CTcPrsNode *right)
348 : CTPNSubBase(left, right) { }
349
350 /* generate code */
351 void gen_code(int discard, int for_condition);
352 };
353
354 /* ------------------------------------------------------------------------ */
355 /*
356 * Equality Comparison
357 */
358
359 class CTPNEq: public CTPNEqBase
360 {
361 public:
362 CTPNEq(class CTcPrsNode *left, class CTcPrsNode *right)
363 : CTPNEqBase(left, right) { }
364
365 /* generate code */
366 void gen_code(int discard, int for_condition);
367 };
368
369
370 /* ------------------------------------------------------------------------ */
371 /*
372 * Inequality Comparison
373 */
374
375 class CTPNNe: public CTPNNeBase
376 {
377 public:
378 CTPNNe(class CTcPrsNode *left, class CTcPrsNode *right)
379 : CTPNNeBase(left, right) { }
380
381 /* generate code */
382 void gen_code(int discard, int for_condition);
383 };
384
385 /* ------------------------------------------------------------------------ */
386 /*
387 * 'is in'
388 */
389
390 class CTPNIsIn: public CTPNIsInBase
391 {
392 public:
393 CTPNIsIn(class CTcPrsNode *left, class CTPNArglist *right)
394 : CTPNIsInBase(left, right) { }
395
396 /* generate code */
397 void gen_code(int discard, int for_condition);
398 };
399
400
401 /* ------------------------------------------------------------------------ */
402 /*
403 * 'not in'
404 */
405
406 class CTPNNotIn: public CTPNNotInBase
407 {
408 public:
409 CTPNNotIn(class CTcPrsNode *left, class CTPNArglist *right)
410 : CTPNNotInBase(left, right) { }
411
412 /* generate code */
413 void gen_code(int discard, int for_condition);
414 };
415
416
417 /* ------------------------------------------------------------------------ */
418 /*
419 * Logical AND (short-circuit logic)
420 */
421
422 class CTPNAnd: public CTPNAndBase
423 {
424 public:
425 CTPNAnd(class CTcPrsNode *left, class CTcPrsNode *right)
426 : CTPNAndBase(left, right) { }
427
428 /* generate code */
429 void gen_code(int discard, int for_condition);
430 };
431
432
433 /* ------------------------------------------------------------------------ */
434 /*
435 * Logical OR (short-circuit logic)
436 */
437
438 class CTPNOr: public CTPNOrBase
439 {
440 public:
441 CTPNOr(class CTcPrsNode *left, class CTcPrsNode *right)
442 : CTPNOrBase(left, right) { }
443
444 /* generate code */
445 void gen_code(int discard, int for_condition);
446 };
447
448
449 /* ------------------------------------------------------------------------ */
450 /*
451 * Assignment Operators
452 */
453
454 /* simple assignment */
455 class CTPNAsi: public CTPNAsiBase
456 {
457 public:
458 CTPNAsi(class CTcPrsNode *left, class CTcPrsNode *right)
459 : CTPNAsiBase(left, right) { }
460
461 /* generate code */
462 void gen_code(int discard, int for_condition);
463 };
464
465 /* add and assign */
466 CTPNBin_side_def(CTPNAddAsi);
467
468 /* subtract and assign */
469 CTPNBin_side_def(CTPNSubAsi);
470
471 /* multiple and assign */
472 CTPNBin_side_def(CTPNMulAsi);
473
474 /* divide and assign */
475 CTPNBin_side_def(CTPNDivAsi);
476
477 /* modulo and assign */
478 CTPNBin_side_def(CTPNModAsi);
479
480 /* bitwise AND and assign */
481 CTPNBin_side_def(CTPNBAndAsi);
482
483 /* bitwise OR and assign */
484 CTPNBin_side_def(CTPNBOrAsi);
485
486 /* bitwise XOR and assign */
487 CTPNBin_side_def(CTPNBXorAsi);
488
489 /* bit shift left and assign */
490 CTPNBin_side_def(CTPNShlAsi);
491
492 /* bit shift right and assign */
493 CTPNBin_side_def(CTPNShrAsi);
494
495 /* ------------------------------------------------------------------------ */
496 /*
497 * Array/list Subscript
498 */
499 class CTPNSubscript: public CTPNSubscriptBase
500 {
501 public:
502 CTPNSubscript(CTcPrsNode *lhs, CTcPrsNode *rhs)
503 : CTPNSubscriptBase(lhs, rhs) { }
504
505 /* generate code */
506 void gen_code(int discard, int for_condition);
507
508 /* generate code to assign a value to the symbol */
509 int gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs,
510 int ignore_errors);
511 };
512
513
514 /* ------------------------------------------------------------------------ */
515 /*
516 * Address-of Operator
517 */
518
519 class CTPNAddr: public CTPNAddrBase
520 {
521 public:
522 CTPNAddr(CTcPrsNode *sub)
523 : CTPNAddrBase(sub) { }
524
525 /* generate code */
526 void gen_code(int discard, int for_condition)
527 {
528 /*
529 * if we're not discarding the value, tell the subnode to
530 * generate its address; if the value is being discarded, do
531 * nothing, since taking an address has no side effects
532 */
533 if (!discard)
534 sub_->gen_code_addr();
535 }
536
537 /* generate a member expression */
538 void gen_code_member(int discard,
539 class CTcPrsNode *prop_expr, int prop_is_expr,
540 int argc, int varargs)
541 {
542 /*
543 * let the subnode do the work, since "&obj" is the same as
544 * "obj" in any context
545 */
546 sub_->gen_code_member(discard, prop_expr, prop_is_expr,
547 argc, varargs);
548 }
549
550 /* evaluate a property ID */
551 vm_prop_id_t gen_code_propid(int check_only, int is_expr)
552 {
553 /*
554 * let the subexpression handle it, so that we treat "x.&prop"
555 * the same as "x.prop"
556 */
557 return sub_->gen_code_propid(check_only, is_expr);
558 }
559
560 /* get the object value for a '.' expression */
561 vm_obj_id_t gen_code_obj_predot(int *is_self)
562 {
563 /*
564 * let the subexpression handle it, since "x.&prop" is the same
565 * as "x.prop"
566 */
567 return sub_->gen_code_obj_predot(is_self);
568 }
569 };
570
571 /* ------------------------------------------------------------------------ */
572 /*
573 * Conditional Operator
574 */
575 class CTPNIf: public CTPNIfBase
576 {
577 public:
578 CTPNIf(class CTcPrsNode *first, class CTcPrsNode *second,
579 class CTcPrsNode *third)
580 : CTPNIfBase(first, second, third) { }
581
582 /* generate code for the conditional */
583 void gen_code(int discard, int for_condition);
584 };
585
586 /* ------------------------------------------------------------------------ */
587 /*
588 * Symbol node
589 */
590 class CTPNSym: public CTPNSymBase
591 {
592 public:
593 CTPNSym(const char *sym, size_t len)
594 : CTPNSymBase(sym, len) { }
595
596 /* generate code for the symbol evaluated as an rvalue */
597 void gen_code(int discard, int for_condition);
598
599 /* generate code to assign a value to the symbol */
600 int gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs,
601 int ignore_errors);
602
603 /* generate code to take the address of the symbol */
604 void gen_code_addr();
605
606 /* generate code to call the symbol */
607 void gen_code_call(int discard, int argc, int varargs);
608
609 /* generate code for operator 'new' applied to this expression */
610 void gen_code_new(int discard, int argc, int varargs, int from_call,
611 int is_transient);
612
613 /* evaluate a property ID */
614 vm_prop_id_t gen_code_propid(int check_only, int is_expr);
615
616 /* generate a member expression */
617 void gen_code_member(int discard,
618 class CTcPrsNode *prop_expr, int prop_is_expr,
619 int argc, int varargs);
620
621 /* get the object value for a '.' expression */
622 vm_obj_id_t gen_code_obj_predot(int *is_self);
623 };
624
625 /* ------------------------------------------------------------------------ */
626 /*
627 * Resolved symbol
628 */
629 class CTPNSymResolved: public CTPNSymResolvedBase
630 {
631 public:
632 CTPNSymResolved(CTcSymbol *sym)
633 : CTPNSymResolvedBase(sym) { }
634
635 /* generate code for the symbol evaluated as an rvalue */
636 void gen_code(int discard, int for_condition);
637
638 /* generate code to assign a value to the symbol */
639 int gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs,
640 int ignore_errors);
641
642 /* generate code to take the address of the symbol */
643 void gen_code_addr();
644
645 /* generate code to call the symbol */
646 void gen_code_call(int discard, int argc, int varargs);
647
648 /* generate code for operator 'new' applied to this expression */
649 void gen_code_new(int discard, int argc, int varargs, int from_call,
650 int is_transient);
651
652 /* evaluate a property ID */
653 vm_prop_id_t gen_code_propid(int check_only, int is_expr);
654
655 /* generate a member expression */
656 void gen_code_member(int discard,
657 class CTcPrsNode *prop_expr, int prop_is_expr,
658 int argc, int varargs);
659
660 /* get the object value for a '.' expression */
661 vm_obj_id_t gen_code_obj_predot(int *is_self);
662 };
663
664 /* ------------------------------------------------------------------------ */
665 /*
666 * Resolved debugger local variable symbol
667 */
668 class CTPNSymDebugLocal: public CTPNSymDebugLocalBase
669 {
670 public:
671 CTPNSymDebugLocal(const struct tcprsdbg_sym_info *info)
672 : CTPNSymDebugLocalBase(info)
673 {
674 }
675
676 /* generate code */
677 void gen_code(int discard, int for_condition);
678
679 /* generate code for assigning into this node */
680 int gen_code_asi(int discard, tc_asitype_t typ,
681 class CTcPrsNode *rhs, int ignore_errors);
682 };
683
684 /* ------------------------------------------------------------------------ */
685 /*
686 * double-quoted string expression
687 */
688 class CTPNDstr: public CTPNDstrBase
689 {
690 public:
691 CTPNDstr(const char *str, size_t len)
692 : CTPNDstrBase(str, len) { }
693
694 void gen_code(int discard, int for_condition);
695 };
696
697 /*
698 * debug version of string
699 */
700 class CTPNDebugDstr: public CTPNDstr
701 {
702 public:
703 CTPNDebugDstr(const char *str, size_t len)
704 : CTPNDstr(str, len) { }
705
706 void gen_code(int discard, int for_condition);
707 };
708
709 /*
710 * double-quoted string embedding
711 */
712 class CTPNDstrEmbed: public CTPNDstrEmbedBase
713 {
714 public:
715 CTPNDstrEmbed(CTcPrsNode *sub);
716
717 void gen_code(int discard, int for_condition);
718 };
719
720 /* ------------------------------------------------------------------------ */
721 /*
722 * Argument List
723 */
724 class CTPNArglist: public CTPNArglistBase
725 {
726 public:
727 CTPNArglist(int argc, class CTPNArg *list_head)
728 : CTPNArglistBase(argc, list_head) { }
729
730 void gen_code(int, int)
731 {
732 /*
733 * this isn't used - we always generate explicitly via
734 * gen_code_arglist()
735 */
736 assert(FALSE);
737 }
738
739 /* generate code for an argument list */
740 void gen_code_arglist(int *varargs);
741 };
742
743 /*
744 * Argument List Entry
745 */
746 class CTPNArg: public CTPNArgBase
747 {
748 public:
749 CTPNArg(CTcPrsNode *expr)
750 : CTPNArgBase(expr) { }
751
752 void gen_code(int discard, int for_condition);
753 };
754
755 /* ------------------------------------------------------------------------ */
756 /*
757 * Function/method call
758 */
759 class CTPNCall: public CTPNCallBase
760 {
761 public:
762 CTPNCall(CTcPrsNode *func, class CTPNArglist *arglist);
763
764 /* generate code */
765 void gen_code(int discard, int for_condition);
766
767 /* generate code for operator 'new' applied to this expression */
768 void gen_code_new(int discard, int argc, int varargs, int from_call,
769 int is_transient);
770 };
771
772
773 /* ------------------------------------------------------------------------ */
774 /*
775 * Member expression with no arguments
776 */
777 class CTPNMember: public CTPNMemberBase
778 {
779 public:
780 CTPNMember(CTcPrsNode *lhs, CTcPrsNode *rhs, int rhs_is_expr)
781 : CTPNMemberBase(lhs, rhs, rhs_is_expr) { }
782
783 /* generate code */
784 void gen_code(int discard, int for_condition);
785
786 /* generate code to assign a value to the symbol */
787 int gen_code_asi(int discard, tc_asitype_t typ, CTcPrsNode *rhs,
788 int ignore_errors);
789 };
790
791 /*
792 * Member evaluation with argument list
793 */
794 class CTPNMemArg: public CTPNMemArgBase
795 {
796 public:
797 CTPNMemArg(CTcPrsNode *obj_expr, CTcPrsNode *prop_expr, int prop_is_expr,
798 class CTPNArglist *arglist)
799 : CTPNMemArgBase(obj_expr, prop_expr, prop_is_expr, arglist) { }
800
801 void gen_code(int discard, int for_condition);
802 };
803
804
805 /* ------------------------------------------------------------------------ */
806 /*
807 * List
808 */
809 class CTPNList: public CTPNListBase
810 {
811 public:
812 void gen_code(int discard, int for_condition);
813 };
814
815 /*
816 * List Element
817 */
818 class CTPNListEle: public CTPNListEleBase
819 {
820 public:
821 CTPNListEle(CTcPrsNode *expr)
822 : CTPNListEleBase(expr) { }
823
824 void gen_code(int discard, int for_condition);
825 };
826
827 /* ------------------------------------------------------------------------ */
828 /*
829 * Derived T3-specific symbol classes
830 */
831
832 /*
833 * undefined symbol
834 */
835 class CTcSymUndef: public CTcSymUndefBase
836 {
837 public:
838 CTcSymUndef(const char *str, size_t len, int copy)
839 : CTcSymUndefBase(str, len, copy) { }
840
841 /*
842 * generate code to evaluate the symbol; since it's undefined, this
843 * doesn't generate any code at all
844 */
845 virtual void gen_code(int) { }
846
847 /*
848 * Generate code to assign to the symbol. The symbol is undefined,
849 * so we can't generate any code; but we'll indicate to the caller
850 * that the assignment is fully processed anyway, since there's no
851 * need for the caller to try generating any code either.
852 */
853 virtual int gen_code_asi(int, tc_asitype_t, class CTcPrsNode *, int)
854 {
855 return TRUE;
856 }
857
858 /*
859 * Generate code for taking the address of the symbol. The symbol
860 * has no address, so this generates no code.
861 */
862 virtual void gen_code_addr() { }
863
864 /*
865 * generate code to call the symbol - we can't generate any code for
866 * this, but don't bother issuing an error since we will have shown
867 * an error for the undefined symbol in the first place
868 */
869 virtual void gen_code_call(int, int, int) { }
870
871 /*
872 * generate code for operator 'new' applied to this expression - we
873 * can't generate any code, but suppress errors as usual
874 */
875 void gen_code_new(int, int, int, int) { }
876
877 /* generate a member expression */
878 void gen_code_member(int, class CTcPrsNode *, int, int, int) { }
879
880 /* get the object value for a '.' expression */
881 vm_obj_id_t gen_code_obj_predot(int *is_self)
882 {
883 *is_self = FALSE;
884 return VM_INVALID_OBJ;
885 }
886 };
887
888
889 /*
890 * function
891 */
892 class CTcSymFunc: public CTcSymFuncBase
893 {
894 public:
895 CTcSymFunc(const char *str, size_t len, int copy,
896 int argc, int varargs, int has_retval,
897 int is_multimethod, int is_mm_base, int is_extern)
898 : CTcSymFuncBase(str, len, copy, argc, varargs, has_retval,
899 is_multimethod, is_mm_base, is_extern)
900 {
901 /*
902 * we don't have a valid absolute address - by default, we use
903 * the anchor
904 */
905 abs_addr_valid_ = FALSE;
906 abs_addr_ = 0;
907 }
908
909 /* generate code to evaluate the symbol */
910 virtual void gen_code(int discard);
911
912 /* we can take the address of this symbol type */
913 virtual void gen_code_addr();
914
915 /* generate a call */
916 virtual void gen_code_call(int discard, int argc, int varargs);
917
918 /*
919 * Get my code pool address. This is valid only after the linking
920 * phase has been completed and all fixups have been applied. (The
921 * normal use for this is to generate image file elements not
922 * subject to fixup, such as the entrypoint record. Normally,
923 * references to the symbol that require the code pool address would
924 * simply generate a fixup rather than asking for the true address,
925 * since a fixup can be generated at any time during code
926 * generation.)
927 */
928 ulong get_code_pool_addr() const;
929
930 /* write the symbol to an image file's global symbol table */
931 int write_to_image_file_global(class CVmImageWriter *image_writer);
932
933 /*
934 * Set the absolute code pool address. This can only be used when
935 * the symbol is loaded from a fully-linked image file; in most
936 * cases, this routine is not needed, because the function's address
937 * will be kept with its associated anchor to allow for relocation
938 * of the function during the linking process.
939 */
940 void set_abs_addr(ulong addr)
941 {
942 /* remember the address */
943 abs_addr_ = addr;
944
945 /* note that the absolute address is valid */
946 abs_addr_valid_ = TRUE;
947 }
948
949 /* add a runtime symbol table entry */
950 void add_runtime_symbol(class CVmRuntimeSymbols *symtab);
951
952 protected:
953 /*
954 * Absolute code pool address, and a flag indicating whether the
955 * address is valid. When we load a function symbol from a
956 * fully-linked image file (specifically, when we load the symbol
957 * from the debug records of an image file), the address of the
958 * function is fully resolved and doesn't require fixups via the
959 * anchor mechanism. In these cases, we'll simply remember the
960 * resolved address here.
961 */
962 ulong abs_addr_;
963 uint abs_addr_valid_ : 1;
964 };
965
966 /*
967 * metaclass
968 */
969 class CTcSymMetaclass: public CTcSymMetaclassBase
970 {
971 public:
972 CTcSymMetaclass(const char *str, size_t len, int copy, int meta_idx,
973 tctarg_obj_id_t class_obj)
974 : CTcSymMetaclassBase(str, len, copy, meta_idx, class_obj)
975 {
976 }
977
978 /* generate code to evaluate the symbol */
979 virtual void gen_code(int discard);
980
981 /* generate code for operator 'new' */
982 void gen_code_new(int discard, int argc, int varargs, int is_transient);
983
984 /* generate a member expression */
985 void gen_code_member(int discard,
986 class CTcPrsNode *prop_expr, int prop_is_expr,
987 int argc, int varargs);
988
989 /* get the object value for a '.' expression */
990 vm_obj_id_t gen_code_obj_predot(int *is_self)
991 {
992 /* simply generate our code as normal */
993 gen_code(FALSE);
994
995 /* indicate that the caller must use generated code */
996 *is_self = FALSE;
997 return VM_INVALID_OBJ;
998 }
999
1000 /* write the symbol to an image file's global symbol table */
1001 int write_to_image_file_global(class CVmImageWriter *image_writer);
1002
1003 /*
1004 * fix up the inheritance chain in the modifier objects - each
1005 * object file has its own independent inheritance list, so we can
1006 * only build the complete and connected list after loading them all
1007 */
1008 void fix_mod_obj_sc_list();
1009
1010 /* add a runtime symbol table entry */
1011 void add_runtime_symbol(class CVmRuntimeSymbols *symtab);
1012 };
1013
1014 /*
1015 * object
1016 */
1017 class CTcSymObj: public CTcSymObjBase
1018 {
1019 public:
1020 CTcSymObj(const char *str, size_t len, int copy, vm_obj_id_t obj_id,
1021 int is_extern, tc_metaclass_t meta, class CTcDictEntry *dict)
1022 : CTcSymObjBase(str, len, copy, obj_id, is_extern, meta, dict)
1023 {
1024 }
1025
1026 /* we have a valid object value */
1027 virtual vm_obj_id_t get_val_obj() const { return get_obj_id(); }
1028
1029 /* generate code to evaluate the symbol */
1030 virtual void gen_code(int discard);
1031
1032 /* we can take the address of this symbol type */
1033 virtual void gen_code_addr();
1034
1035 /* generate code for operator 'new' */
1036 void gen_code_new(int discard, int argc, int varargs, int is_transient);
1037
1038 /* static code to generate code for 'new' */
1039 static void s_gen_code_new(int discard, vm_obj_id_t obj_id,
1040 tc_metaclass_t meta,
1041 int argc, int varargs, int is_transient);
1042
1043 /* generate a member expression */
1044 void gen_code_member(int discard,
1045 class CTcPrsNode *prop_expr, int prop_is_expr,
1046 int argc, int varargs);
1047
1048 /* static code to generate a member expression */
1049 static void s_gen_code_member(int discard,
1050 class CTcPrsNode *prop_expr,
1051 int prop_is_expr, int argc,
1052 vm_obj_id_t obj_id, int varargs);
1053
1054 /* get the object value for a '.' expression */
1055 vm_obj_id_t gen_code_obj_predot(int *is_self);
1056
1057 /* delete a property from our modified base classes */
1058 virtual void delete_prop_from_mod_base(tctarg_prop_id_t prop_id);
1059
1060 /* mark the compiled data for the object as a 'class' object */
1061 virtual void mark_compiled_as_class();
1062
1063 /* write the symbol to an image file's global symbol table */
1064 int write_to_image_file_global(class CVmImageWriter *image_writer);
1065
1066 /* build my dictionary */
1067 virtual void build_dictionary();
1068
1069 /* add a runtime symbol table entry */
1070 void add_runtime_symbol(class CVmRuntimeSymbols *symtab);
1071 };
1072
1073 /*
1074 * property
1075 */
1076 class CTcSymProp: public CTcSymPropBase
1077 {
1078 public:
1079 CTcSymProp(const char *str, size_t len, int copy, tctarg_prop_id_t prop)
1080 : CTcSymPropBase(str, len, copy, prop) { }
1081
1082 /* generate code to evaluate the symbol */
1083 virtual void gen_code(int discard);
1084
1085 /* we can take the address of this symbol type */
1086 virtual void gen_code_addr();
1087
1088 /* assign to the property */
1089 virtual int gen_code_asi(int discard, tc_asitype_t typ,
1090 class CTcPrsNode *rhs, int ignore_errors);
1091
1092 /* generate a call */
1093 virtual void gen_code_call(int discard, int argc, int varargs);
1094
1095 /* evaluate a property ID */
1096 vm_prop_id_t gen_code_propid(int check_only, int is_expr);
1097
1098 /* generate a member expression */
1099 void gen_code_member(int discard,
1100 class CTcPrsNode *prop_expr, int prop_is_expr,
1101 int argc, int varargs);
1102
1103 /* get the object value for a '.' expression */
1104 vm_obj_id_t gen_code_obj_predot(int *is_self)
1105 {
1106 /* simply generate our code as normal */
1107 gen_code(FALSE);
1108
1109 /* indicate that the caller must use generated code */
1110 *is_self = FALSE;
1111 return VM_INVALID_OBJ;
1112 }
1113
1114 /* write the symbol to an image file's global symbol table */
1115 int write_to_image_file_global(class CVmImageWriter *image_writer);
1116
1117 /* add a runtime symbol table entry */
1118 void add_runtime_symbol(class CVmRuntimeSymbols *symtab);
1119 };
1120
1121 /*
1122 * Enumerator
1123 */
1124 class CTcSymEnum: public CTcSymEnumBase
1125 {
1126 public:
1127 CTcSymEnum(const char *str, size_t len, int copy, ulong id, int is_token)
1128 : CTcSymEnumBase(str, len, copy, id, is_token)
1129 {
1130 }
1131
1132 /* generate code */
1133 virtual void gen_code(int discard);
1134
1135 /* write the symbol to an image file's global symbol table */
1136 int write_to_image_file_global(class CVmImageWriter *image_writer);
1137
1138 /* add a runtime symbol table entry */
1139 void add_runtime_symbol(class CVmRuntimeSymbols *symtab);
1140 };
1141
1142 /*
1143 * local variable/parameter
1144 */
1145 class CTcSymLocal: public CTcSymLocalBase
1146 {
1147 public:
1148 CTcSymLocal(const char *str, size_t len, int copy,
1149 int is_param, int var_num)
1150 : CTcSymLocalBase(str, len, copy, is_param, var_num) { }
1151
1152 /* generate code to evaluate the symbol */
1153 virtual void gen_code(int discard);
1154
1155 /* generate code to store the value at top of stack in the variable */
1156 void gen_code_setlcl_stk() { s_gen_code_setlcl_stk(var_num_, is_param_); }
1157
1158 /* generate code to get a local with a given ID */
1159 static void s_gen_code_getlcl(int local_id, int is_param);
1160
1161 /* generate code to store a local with a given ID */
1162 static void s_gen_code_setlcl_stk(int local_id, int is_param);
1163
1164 /* generate code to store the value at top of stack in the local */
1165 void gen_code_setlcl();
1166
1167 /* assign to the variable */
1168 virtual int gen_code_asi(int discard, tc_asitype_t typ,
1169 class CTcPrsNode *rhs, int ignore_errors);
1170
1171 /*
1172 * generate a call - invoking a local as a function assumes that the
1173 * local contains a method or function pointer
1174 */
1175 virtual void gen_code_call(int discard, int argc, int varargs);
1176
1177 /* evaluate a property ID */
1178 vm_prop_id_t gen_code_propid(int check_only, int is_expr);
1179
1180 /* generate a member expression */
1181 void gen_code_member(int discard,
1182 class CTcPrsNode *prop_expr, int prop_is_expr,
1183 int argc, int varargs);
1184
1185 /* get the object value for a '.' expression */
1186 vm_obj_id_t gen_code_obj_predot(int *is_self)
1187 {
1188 /* simply generate our code as normal */
1189 gen_code(FALSE);
1190
1191 /* indicate that the caller must use generated code */
1192 *is_self = FALSE;
1193 return VM_INVALID_OBJ;
1194 }
1195
1196 /* write the symbol to a debug frame */
1197 virtual int write_to_debug_frame();
1198 };
1199
1200 /*
1201 * built-in function
1202 */
1203 class CTcSymBif: public CTcSymBifBase
1204 {
1205 public:
1206 CTcSymBif(const char *str, size_t len, int copy,
1207 int func_set_id, int func_idx, int has_retval,
1208 int min_argc, int max_argc, int varargs)
1209 : CTcSymBifBase(str, len, copy, func_set_id, func_idx,
1210 has_retval, min_argc, max_argc, varargs) { }
1211
1212 /* generate code to evaluate the symbol */
1213 virtual void gen_code(int discard);
1214
1215 /* generate a call */
1216 virtual void gen_code_call(int discard, int argc, int varargs);
1217
1218 /* write the symbol to an image file's global symbol table */
1219 int write_to_image_file_global(class CVmImageWriter *image_writer);
1220 };
1221
1222 /*
1223 * external function
1224 */
1225 class CTcSymExtfn: public CTcSymExtfnBase
1226 {
1227 public:
1228 CTcSymExtfn(const char *str, size_t len, int copy,
1229 int argc, int varargs, int has_retval)
1230 : CTcSymExtfnBase(str, len, copy, argc, varargs, has_retval) { }
1231
1232 /* generate code to evaluate the symbol */
1233 virtual void gen_code(int discard);
1234
1235 /* generate a call */
1236 virtual void gen_code_call(int discard, int argc, int varargs);
1237
1238 /* write the symbol to an image file's global symbol table */
1239 int write_to_image_file_global(class CVmImageWriter *image_writer);
1240 };
1241
1242 /*
1243 * code label
1244 */
1245 class CTcSymLabel: public CTcSymLabelBase
1246 {
1247 public:
1248 CTcSymLabel(const char *str, size_t len, int copy)
1249 : CTcSymLabelBase(str, len, copy) { }
1250
1251 /* generate code to evaluate the symbol */
1252 virtual void gen_code(int discard);
1253 };
1254
1255 /* ------------------------------------------------------------------------ */
1256 /*
1257 * Anonymous function
1258 */
1259 class CTPNAnonFunc: public CTPNAnonFuncBase
1260 {
1261 public:
1262 CTPNAnonFunc(class CTPNCodeBody *code_body, int has_retval)
1263 : CTPNAnonFuncBase(code_body, has_retval)
1264 {
1265 }
1266
1267 /* generate code */
1268 virtual void gen_code(int discard, int for_condition);
1269 };
1270
1271 /* ------------------------------------------------------------------------ */
1272 /*
1273 * Code Body
1274 */
1275 class CTPNCodeBody: public CTPNCodeBodyBase
1276 {
1277 public:
1278 CTPNCodeBody(class CTcPrsSymtab *lcltab, class CTcPrsSymtab *gototab,
1279 class CTPNStm *stm, int argc, int varargs,
1280 int varargs_list, class CTcSymLocal *varargs_list_local,
1281 int local_cnt, int self_valid,
1282 struct CTcCodeBodyRef *enclosing_code_body)
1283 : CTPNCodeBodyBase(lcltab, gototab, stm, argc, varargs,
1284 varargs_list, varargs_list_local,
1285 local_cnt, self_valid, enclosing_code_body)
1286 {
1287 /* presume I'm not a constructor */
1288 is_constructor_ = FALSE;
1289
1290 /* presume I won't need a 'finally' return value holder local */
1291 allocated_fin_ret_lcl_ = FALSE;
1292 fin_ret_lcl_ = 0;
1293
1294 /* presume it won't be static */
1295 is_static_ = FALSE;
1296
1297 /* no nested symbol table list yet */
1298 first_nested_symtab_ = 0;
1299 }
1300
1301 /*
1302 * Mark the code body as belonging to a constructor. A constructor
1303 * must return its 'self' object as the return value.
1304 */
1305 void set_constructor(int f) { is_constructor_ = f; }
1306
1307 /*
1308 * mark the code body as static - this indicates that it is a static
1309 * initializer
1310 */
1311 void set_static() { is_static_ = TRUE; }
1312
1313 /* generate code */
1314 virtual void gen_code(int discard, int for_condition);
1315
1316 /* check locals */
1317 virtual void check_locals();
1318
1319 /*
1320 * Allocate a local variable for temporarily saving the expression
1321 * value of a 'return' statement while calling the enclosing
1322 * 'finally' block(s). Returns the variable ID. Only one such
1323 * local is needed per code body, so if we've already allocated one
1324 * for another 'return' statement, we'll just return the same one we
1325 * allocated previously.
1326 */
1327 uint alloc_fin_ret_lcl()
1328 {
1329 /* if we haven't allocated it yet, do so now */
1330 if (!allocated_fin_ret_lcl_)
1331 {
1332 /* use the next available local */
1333 fin_ret_lcl_ = local_cnt_++;
1334
1335 /* note that we've allocated it */
1336 allocated_fin_ret_lcl_ = TRUE;
1337 }
1338
1339 /* return the local ID */
1340 return fin_ret_lcl_;
1341 }
1342
1343 /* generate as an anonymous function */
1344 void js_anon_func();
1345
1346 protected:
1347 /*
1348 * callback for enumerating local frame symbol table entries - write
1349 * each entry to the code stream as a debug record
1350 */
1351 static void write_local_to_debug_frame(void *ctx, class CTcSymbol *sym);
1352
1353 /*
1354 * enumerator for checking for parameter symbols that belong in the
1355 * local context
1356 */
1357 static void enum_for_param_ctx(void *ctx, class CTcSymbol *sym);
1358
1359 /* write the debug information table to the code stream */
1360 void build_debug_table(ulong start_ofs);
1361
1362 /* show disassembly */
1363 void show_disassembly(unsigned long start_ofs,
1364 unsigned long code_start_ofs,
1365 unsigned long code_end_ofs);
1366
1367 /* head of list of nested symbol tables */
1368 class CTcPrsSymtab *first_nested_symtab_;
1369
1370 /*
1371 * ID of local variable for temporarily storing the expression value
1372 * of a 'return' statement while calling the enclosing 'finally'
1373 * block(s); valid only when allocated_fin_ret_lcl_ is true
1374 */
1375 uint fin_ret_lcl_;
1376
1377 /* flag: I'm a constructor */
1378 uint is_constructor_ : 1;
1379
1380 /*
1381 * flag: I've allocated a local for saving the expression value of a
1382 * 'return' statement while calling the enclosing 'finally' block(s)
1383 */
1384 uint allocated_fin_ret_lcl_ : 1;
1385
1386 /* flag: I'm a static property initializer */
1387 uint is_static_ : 1;
1388 };
1389
1390 /* ------------------------------------------------------------------------ */
1391 /*
1392 * Compound Statement
1393 */
1394 class CTPNStmComp: public CTPNStmCompBase
1395 {
1396 public:
1397 CTPNStmComp(class CTPNStm *first_stm, class CTcPrsSymtab *symtab)
1398 : CTPNStmCompBase(first_stm, symtab) { }
1399 };
1400
1401 /* ------------------------------------------------------------------------ */
1402 /*
1403 * Null statement
1404 */
1405 class CTPNStmNull: public CTPNStmNullBase
1406 {
1407 public:
1408 CTPNStmNull() { }
1409 };
1410
1411 /* ------------------------------------------------------------------------ */
1412 /*
1413 * Expression statement
1414 */
1415 class CTPNStmExpr: public CTPNStmExprBase
1416 {
1417 public:
1418 CTPNStmExpr(class CTcPrsNode *expr)
1419 : CTPNStmExprBase(expr) { }
1420 };
1421
1422 /* ------------------------------------------------------------------------ */
1423 /*
1424 * Static property initializer statement
1425 */
1426 class CTPNStmStaticPropInit: public CTPNStmStaticPropInitBase
1427 {
1428 public:
1429 CTPNStmStaticPropInit(class CTcPrsNode *expr,
1430 tctarg_prop_id_t prop)
1431 : CTPNStmStaticPropInitBase(expr, prop) { }
1432
1433 /* generate code */
1434 virtual void gen_code(int discard, int for_condition);
1435 };
1436
1437 /* ------------------------------------------------------------------------ */
1438 /*
1439 * 'if' statement
1440 */
1441 class CTPNStmIf: public CTPNStmIfBase
1442 {
1443 public:
1444 CTPNStmIf(class CTcPrsNode *cond_expr,
1445 class CTPNStm *then_part, class CTPNStm *else_part)
1446 : CTPNStmIfBase(cond_expr, then_part, else_part) { }
1447
1448 /* generate code */
1449 virtual void gen_code(int discard, int for_condition);
1450 };
1451
1452 /* ------------------------------------------------------------------------ */
1453 /*
1454 * 'for' statement
1455 */
1456 class CTPNStmFor: public CTPNStmForBase
1457 {
1458 public:
1459 CTPNStmFor(class CTcPrsNode *init_expr,
1460 class CTcPrsNode *cond_expr,
1461 class CTcPrsNode *reinit_expr,
1462 class CTcPrsSymtab *symtab,
1463 class CTPNStmEnclosing *enclosing_stm)
1464 : CTPNStmForBase(init_expr, cond_expr, reinit_expr,
1465 symtab, enclosing_stm)
1466 {
1467 }
1468
1469 /* generate code */
1470 virtual void gen_code(int discard, int for_condition);
1471
1472 /* generate code for 'break' */
1473 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
1474 { return gen_code_break_loop(break_lbl_, lbl, lbl_len); }
1475
1476 /* generate code for 'continue' */
1477 virtual int gen_code_labeled_continue()
1478 { return gen_code_continue_loop(cont_lbl_, 0, 0); }
1479
1480 /* generate code for a labeled 'continue' */
1481 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len)
1482 { return gen_code_continue_loop(cont_lbl_, lbl, lbl_len); }
1483
1484 protected:
1485 /* our 'break' label */
1486 struct CTcCodeLabel *break_lbl_;
1487
1488 /* our 'continue' label */
1489 struct CTcCodeLabel *cont_lbl_;
1490 };
1491
1492 /* ------------------------------------------------------------------------ */
1493 /*
1494 * 'for' statement
1495 */
1496 class CTPNStmForeach: public CTPNStmForeachBase
1497 {
1498 public:
1499 CTPNStmForeach(class CTcPrsNode *iter_expr,
1500 class CTcPrsNode *coll_expr,
1501 class CTcPrsSymtab *symtab,
1502 class CTPNStmEnclosing *enclosing_stm,
1503 int iter_local_id)
1504 : CTPNStmForeachBase(iter_expr, coll_expr, symtab, enclosing_stm,
1505 iter_local_id)
1506 {
1507 }
1508
1509 /* generate code */
1510 virtual void gen_code(int discard, int for_condition);
1511
1512 /* generate code for 'break' */
1513 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
1514 { return gen_code_break_loop(break_lbl_, lbl, lbl_len); }
1515
1516 /* generate code for 'continue' */
1517 virtual int gen_code_labeled_continue()
1518 { return gen_code_continue_loop(cont_lbl_, 0, 0); }
1519
1520 /* generate code for a labeled 'continue' */
1521 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len)
1522 { return gen_code_continue_loop(cont_lbl_, lbl, lbl_len); }
1523
1524 protected:
1525 /* our 'break' label */
1526 struct CTcCodeLabel *break_lbl_;
1527
1528 /* our 'continue' label */
1529 struct CTcCodeLabel *cont_lbl_;
1530 };
1531
1532 /* ------------------------------------------------------------------------ */
1533 /*
1534 * 'break' statement
1535 */
1536 class CTPNStmBreak: public CTPNStmBreakBase
1537 {
1538 public:
1539 CTPNStmBreak() { }
1540
1541 /* generate code */
1542 virtual void gen_code(int discard, int for_condition);
1543 };
1544
1545 /* ------------------------------------------------------------------------ */
1546 /*
1547 * 'continue' statement
1548 */
1549 class CTPNStmContinue: public CTPNStmContinueBase
1550 {
1551 public:
1552 CTPNStmContinue() { }
1553
1554 /* generate code */
1555 virtual void gen_code(int discard, int for_condition);
1556 };
1557
1558 /* ------------------------------------------------------------------------ */
1559 /*
1560 * 'while' statement
1561 */
1562 class CTPNStmWhile: public CTPNStmWhileBase
1563 {
1564 public:
1565 CTPNStmWhile(class CTcPrsNode *cond_expr,
1566 class CTPNStmEnclosing *enclosing_stm)
1567 : CTPNStmWhileBase(cond_expr, enclosing_stm) { }
1568
1569 /* generate code */
1570 virtual void gen_code(int discard, int for_condition);
1571
1572 /* generate code for 'break' */
1573 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
1574 { return gen_code_break_loop(break_lbl_, lbl, lbl_len); }
1575
1576 /* generate code for 'continue' */
1577 virtual int gen_code_labeled_continue()
1578 { return gen_code_continue_loop(cont_lbl_, 0, 0); }
1579
1580 /* generate code for a labeled 'continue' */
1581 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len)
1582 { return gen_code_continue_loop(cont_lbl_, lbl, lbl_len); }
1583
1584 protected:
1585 /* our 'break' label */
1586 struct CTcCodeLabel *break_lbl_;
1587
1588 /* our 'continue' label */
1589 struct CTcCodeLabel *cont_lbl_;
1590 };
1591
1592 /* ------------------------------------------------------------------------ */
1593 /*
1594 * 'do-while' statement
1595 */
1596 class CTPNStmDoWhile: public CTPNStmDoWhileBase
1597 {
1598 public:
1599 CTPNStmDoWhile(class CTPNStmEnclosing *enclosing_stm)
1600 : CTPNStmDoWhileBase(enclosing_stm) { }
1601
1602 /* generate code */
1603 virtual void gen_code(int discard, int for_condition);
1604
1605 /* generate code for 'break' */
1606 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
1607 { return gen_code_break_loop(break_lbl_, lbl, lbl_len); }
1608
1609 /* generate code for 'continue' */
1610 virtual int gen_code_labeled_continue()
1611 { return gen_code_continue_loop(cont_lbl_, 0, 0); }
1612
1613 /* generate code for a labeled 'continue' */
1614 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len)
1615 { return gen_code_continue_loop(cont_lbl_, lbl, lbl_len); }
1616
1617 protected:
1618 /* our 'break' label */
1619 struct CTcCodeLabel *break_lbl_;
1620
1621 /* our 'continue' label */
1622 struct CTcCodeLabel *cont_lbl_;
1623 };
1624
1625 /* ------------------------------------------------------------------------ */
1626 /*
1627 * 'return' statement
1628 */
1629 class CTPNStmReturn: public CTPNStmReturnBase
1630 {
1631 public:
1632 CTPNStmReturn(class CTcPrsNode *expr)
1633 : CTPNStmReturnBase(expr) { }
1634
1635 /* generate code */
1636 virtual void gen_code(int discard, int for_condition);
1637 };
1638
1639 /* ------------------------------------------------------------------------ */
1640 /*
1641 * 'switch' statement
1642 */
1643 class CTPNStmSwitch: public CTPNStmSwitchBase
1644 {
1645 public:
1646 CTPNStmSwitch(class CTPNStmEnclosing *enclosing_stm)
1647 : CTPNStmSwitchBase(enclosing_stm)
1648 {
1649 /* the 'case' and 'default' slot pointers aren't valid yet */
1650 case_slot_ofs_ = 0;
1651 default_slot_ofs_ = 0;
1652 }
1653
1654 /* generate code */
1655 virtual void gen_code(int discard, int for_condition);
1656
1657 /*
1658 * Get the next case slot offset, and consume the slot. Each call
1659 * consumes one slot. This is valid only during code generation in
1660 * the switch body; 'case' labels use this to figure out where to
1661 * write their data.
1662 */
1663 ulong alloc_case_slot()
1664 {
1665 ulong ret;
1666
1667 /* note the value to return */
1668 ret = case_slot_ofs_;
1669
1670 /* move our internal counter to the next slot */
1671 case_slot_ofs_ += VMB_DATAHOLDER + VMB_UINT2;
1672
1673 /* return the allocated slot */
1674 return ret;
1675 }
1676
1677 /* get the code offset of the 'default' case slot */
1678 ulong get_default_slot() const { return default_slot_ofs_; }
1679
1680 /* generate code for 'break' */
1681 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
1682 {
1683 /*
1684 * if there's no label, and we don't have a 'break' label
1685 * ourselves, it must mean that we're processing an unreachable
1686 * 'break' - we can simply do nothing in this case and pretend we
1687 * succeeded
1688 */
1689 if (lbl == 0 && break_lbl_ == 0)
1690 return TRUE;
1691
1692 /* break to our break label */
1693 return gen_code_break_loop(break_lbl_, lbl, lbl_len);
1694 }
1695
1696 protected:
1697 /*
1698 * code offset of the next 'case' slot to be allocated from the
1699 * switch's case table - this is only valid during generation of the
1700 * body, because we set this up during generation of the switch
1701 * itself
1702 */
1703 ulong case_slot_ofs_;
1704
1705 /* code offset of the 'default' slot */
1706 ulong default_slot_ofs_;
1707
1708 /* our 'break' label */
1709 struct CTcCodeLabel *break_lbl_;
1710 };
1711
1712
1713 /* ------------------------------------------------------------------------ */
1714 /*
1715 * code label statement
1716 */
1717 class CTPNStmLabel: public CTPNStmLabelBase
1718 {
1719 public:
1720 CTPNStmLabel(class CTcSymLabel *lbl, class CTPNStmEnclosing *enclosing);
1721
1722 /* generate code */
1723 virtual void gen_code(int discard, int for_condition);
1724
1725 /* generate code for 'break' */
1726 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len);
1727
1728 /* generate code for 'continue' */
1729 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len);
1730
1731 /*
1732 * Generate code for a labeled 'continue'. We'll simply generate
1733 * code for a labeled continue in our enclosed statement.
1734 *
1735 * (This is normally a routine that *we* call on our contained
1736 * statement in response to a 'continue' being targeted at this
1737 * label. This can be invoked on us, however, when a statement has
1738 * multiple labels, and the outer label is mentioned in the 'break'
1739 * -- the outer label will call this routine on the inner label,
1740 * which is us, so we simply need to pass it along to the enclosed
1741 * statement.)
1742 */
1743 virtual int gen_code_labeled_continue()
1744 { return (stm_ != 0 ? stm_->gen_code_labeled_continue() : FALSE); }
1745
1746 /* get my 'goto' label */
1747 struct CTcCodeLabel *get_goto_label();
1748
1749 protected:
1750 /* the byte label to jump to this statement */
1751 struct CTcCodeLabel *goto_label_;
1752
1753 /* the byte label to break from this statement */
1754 struct CTcCodeLabel *break_label_;
1755 };
1756
1757 /* ------------------------------------------------------------------------ */
1758 /*
1759 * 'case' label statement
1760 */
1761 class CTPNStmCase: public CTPNStmCaseBase
1762 {
1763 public:
1764 CTPNStmCase() { }
1765
1766 /* generate code */
1767 virtual void gen_code(int discard, int for_condition);
1768 };
1769
1770 /* ------------------------------------------------------------------------ */
1771 /*
1772 * 'default' label statement
1773 */
1774 class CTPNStmDefault: public CTPNStmDefaultBase
1775 {
1776 public:
1777 CTPNStmDefault() { }
1778
1779 /* generate code */
1780 virtual void gen_code(int discard, int for_condition);
1781 };
1782
1783 /* ------------------------------------------------------------------------ */
1784 /*
1785 * 'try' block
1786 */
1787 class CTPNStmTry: public CTPNStmTryBase
1788 {
1789 public:
1790 CTPNStmTry(class CTPNStmEnclosing *enclosing)
1791 : CTPNStmTryBase(enclosing)
1792 {
1793 }
1794
1795 /* generate code */
1796 virtual void gen_code(int discard, int for_condition);
1797
1798 /* generate code for 'break' */
1799 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len);
1800
1801 /* generate code for 'continue' */
1802 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len);
1803
1804 /*
1805 * Generate the code necessary to unwind for returning. We must
1806 * generate a call to our 'finally' code, then unwind any enclosing
1807 * statements.
1808 */
1809 virtual void gen_code_unwind_for_return()
1810 {
1811 /* call 'finally' on the way out */
1812 gen_jsr_finally();
1813
1814 /* inherit default to unwind enclosing statements */
1815 CTPNStmTryBase::gen_code_unwind_for_return();
1816 }
1817
1818 /* determine if we'll generate code for unwinding from a return */
1819 virtual int will_gen_code_unwind_for_return() const
1820 {
1821 /* we'll generate a call to our 'finally' block if we have one */
1822 if (finally_stm_ != 0)
1823 return TRUE;
1824
1825 /* we'll also generate code if any enclosing statement does */
1826 return CTPNStmTryBase::will_gen_code_unwind_for_return();
1827 }
1828
1829 /* generate the code necessary to transfer out of the block */
1830 virtual void gen_code_for_transfer_out()
1831 {
1832 /* call 'finally' on the way out */
1833 gen_jsr_finally();
1834 }
1835
1836 /*
1837 * generate a local subroutine call to our 'finally' block, if we
1838 * have a 'finally' block - this should be invoked whenever code
1839 * within the protected block or a 'catch' block executes a break,
1840 * continue, return, or goto out of the protected region, or when
1841 * merely falling off the end of the protected block or a 'catch'
1842 * block.
1843 */
1844 void gen_jsr_finally();
1845
1846 /* get my 'finally' label */
1847 struct CTcCodeLabel *get_finally_lbl() const { return finally_lbl_; }
1848
1849 protected:
1850 /* our 'finally' label */
1851 struct CTcCodeLabel *finally_lbl_;
1852 };
1853
1854 /* ------------------------------------------------------------------------ */
1855 /*
1856 * 'catch'
1857 */
1858 class CTPNStmCatch: public CTPNStmCatchBase
1859 {
1860 public:
1861 CTPNStmCatch() { }
1862
1863 /* generate code */
1864 virtual void gen_code(int discard, int for_condition);
1865
1866 /* generate code for the 'catch' clause */
1867 virtual void gen_code_catch(ulong protected_start_ofs,
1868 ulong protected_end_ofs);
1869 };
1870
1871 /* ------------------------------------------------------------------------ */
1872 /*
1873 * 'finally'
1874 */
1875 class CTPNStmFinally: public CTPNStmFinallyBase
1876 {
1877 public:
1878 CTPNStmFinally(CTPNStmEnclosing *enclosing,
1879 int exc_local_id, int jsr_local_id)
1880 : CTPNStmFinallyBase(enclosing, exc_local_id, jsr_local_id)
1881 {
1882 }
1883
1884 /* generate code */
1885 virtual void gen_code(int discard, int for_condition);
1886
1887 /* generate code for the 'finally' clause */
1888 virtual void gen_code_finally(ulong protected_start_ofs,
1889 ulong protected_end_ofs,
1890 class CTPNStmTry *try_stm);
1891
1892 /* check for transfer in via 'goto' statements */
1893 virtual int check_enter_by_goto(class CTPNStmGoto *goto_stm,
1894 class CTPNStmLabel *target);
1895 };
1896
1897
1898 /* ------------------------------------------------------------------------ */
1899 /*
1900 * 'throw' statement
1901 */
1902 class CTPNStmThrow: public CTPNStmThrowBase
1903 {
1904 public:
1905 CTPNStmThrow(class CTcPrsNode *expr)
1906 : CTPNStmThrowBase(expr)
1907 {
1908 }
1909
1910 /* generate code */
1911 virtual void gen_code(int discard, int for_condition);
1912 };
1913
1914
1915 /* ------------------------------------------------------------------------ */
1916 /*
1917 * 'goto' statement
1918 */
1919 class CTPNStmGoto: public CTPNStmGotoBase
1920 {
1921 public:
1922 CTPNStmGoto(const textchar_t *lbl, size_t lbl_len)
1923 : CTPNStmGotoBase(lbl, lbl_len)
1924 {
1925 }
1926
1927 /* generate code */
1928 virtual void gen_code(int discard, int for_condition);
1929 };
1930
1931 /* ------------------------------------------------------------------------ */
1932 /*
1933 * 'dictionary' statement
1934 */
1935 class CTPNStmDict: public CTPNStmDictBase
1936 {
1937 public:
1938 CTPNStmDict(class CTcDictEntry *dict)
1939 : CTPNStmDictBase(dict)
1940 {
1941 }
1942
1943 /* generate code */
1944 virtual void gen_code(int, int) { }
1945 };
1946
1947 /* ------------------------------------------------------------------------ */
1948 /*
1949 * Object Definition
1950 */
1951 class CTPNStmObject: public CTPNStmObjectBase
1952 {
1953 public:
1954 CTPNStmObject(class CTcSymObj *obj_sym, int is_class)
1955 : CTPNStmObjectBase(obj_sym, is_class)
1956 {
1957 }
1958
1959 /* generate code */
1960 virtual void gen_code(int discard, int for_condition);
1961
1962 /* check locals */
1963 virtual void check_locals();
1964
1965 /*
1966 * given the stream offset for the start of the object data in the
1967 * stream, get information on the object from the stream data:
1968 *
1969 *. - get the number of properties in the stream data
1970 *. - set the number of properties in the stream data, updating the
1971 * stored data size in the metaclass header in the stream; returns
1972 * the new data size
1973 *. - get the number of superclasses in the stream data
1974 *. - get a superclass object ID
1975 *. - get the offset of the first property in the stream data
1976 *. - get the offset of the property at the given index
1977 *. - get the ID of the object's property at the given index
1978 *. - set the ID of the object's property at the given index
1979 *. - get the object flags from the tads-object header
1980 *. - set the object flags in the tads-object heade
1981 */
1982 static uint get_stream_prop_cnt(class CTcDataStream *stream,
1983 ulong obj_ofs);
1984 static size_t set_stream_prop_cnt(class CTcDataStream *stream,
1985 ulong obj_ofs, uint prop_cnt);
1986 static uint get_stream_sc_cnt(class CTcDataStream *stream,
1987 ulong obj_ofs);
1988 static vm_obj_id_t get_stream_sc(class CTcDataStream *stream,
1989 ulong obj_ofs, uint sc_idx);
1990 static void set_stream_sc(class CTcDataStream *stream,
1991 ulong obj_ofs, uint sc_idx, vm_obj_id_t new_sc);
1992 static ulong get_stream_first_prop_ofs(class CTcDataStream *stream,
1993 ulong obj_ofs);
1994 static ulong get_stream_prop_ofs(class CTcDataStream *stream,
1995 ulong obj_ofs, uint prop_idx);
1996 static vm_prop_id_t get_stream_prop_id(class CTcDataStream *stream,
1997 ulong obj_ofs, uint prop_idx);
1998 static enum vm_datatype_t
1999 get_stream_prop_type(class CTcDataStream *stream,
2000 ulong obj_ofs, uint prop_idx);
2001 static ulong get_stream_prop_val_ofs(class CTcDataStream *stream,
2002 ulong obj_ofs, uint prop_idx);
2003 static void set_stream_prop_id(class CTcDataStream *stream,
2004 ulong obj_ofs, uint prop_idx,
2005 vm_prop_id_t new_id);
2006 static uint get_stream_obj_flags(class CTcDataStream *stream,
2007 ulong obj_ofs);
2008 static void set_stream_obj_flags(class CTcDataStream *stream,
2009 ulong obj_ofs, uint flags);
2010 };
2011
2012 /*
2013 * Property Value list entry
2014 */
2015 class CTPNObjProp: public CTPNObjPropBase
2016 {
2017 public:
2018 CTPNObjProp(class CTPNStmObject *obj_stm, class CTcSymProp *prop_sym,
2019 class CTcPrsNode *expr, class CTPNCodeBody *code_body,
2020 int is_static)
2021 : CTPNObjPropBase(obj_stm, prop_sym, expr, code_body, is_static)
2022 {
2023 }
2024
2025 /* generate code */
2026 virtual void gen_code(int discard, int for_condition);
2027
2028 /* check locals */
2029 void check_locals();
2030 };
2031
2032 /*
2033 * Implicit constructor
2034 */
2035 class CTPNStmImplicitCtor: public CTPNStmImplicitCtorBase
2036 {
2037 public:
2038 CTPNStmImplicitCtor(class CTPNStmObject *obj_stm)
2039 : CTPNStmImplicitCtorBase(obj_stm)
2040 {
2041 }
2042
2043 /* generate code */
2044 virtual void gen_code(int discard, int for_condition);
2045 };
2046
2047
2048 #endif /* TCJSDRV_H */