-
Notifications
You must be signed in to change notification settings - Fork 0
/
tct3stm.cpp
2471 lines (2128 loc) · 75.7 KB
/
tct3stm.cpp
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
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/TCT3STM.CPP,v 1.1 1999/07/11 00:46:57 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tct3stm.cpp - TADS 3 Compiler - T3 VM Code Generator - statement classes
Function
Generate code for the T3 VM. This file contains statement classes,
in order to segregate the code generation classes required for the
full compiler from those required for subsets that require only
expression parsing (such as debuggers).
Notes
Modified
05/08/99 MJRoberts - Creation
*/
#include <stdio.h>
#include "t3std.h"
#include "os.h"
#include "tcprs.h"
#include "tct3.h"
#include "tcgen.h"
#include "vmtype.h"
#include "vmwrtimg.h"
#include "vmfile.h"
#include "tcmain.h"
#include "tcerr.h"
/* ------------------------------------------------------------------------ */
/*
* 'if' statement
*/
/*
* generate code
*/
void CTPNStmIf::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* if the condition has a constant value, don't bother generating
* code for both branches
*/
if (cond_expr_->is_const())
{
int val;
/* determine whether it's true or false */
val = cond_expr_->get_const_val()->get_val_bool();
/*
* Warn about it if it's always false (in which case the 'then'
* code is unreachable); or it's always true and we have an
* 'else' part (since the 'else' part is unreachable). Don't
* warn if it's true and there's no 'else' part, since this
* merely means that there's some redundant source code, but
* will have no effect on the generated code.
*/
if (!val)
{
/*
* It's false - the 'then' part cannot be executed. If this
* isn't a compile-time constant expression, warn about it.
*/
if (!cond_expr_->get_const_val()->is_ctc())
log_warning(TCERR_IF_ALWAYS_FALSE);
/* generate the 'else' part if there is one */
if (else_part_ != 0)
gen_code_substm(else_part_);
}
else
{
/* it's true - the 'else' part cannot be executed */
if (else_part_ != 0)
log_warning(TCERR_IF_ALWAYS_TRUE);
/* generate the 'then' part */
if (then_part_ != 0)
gen_code_substm(then_part_);
}
/* we're done */
return;
}
/*
* If both the 'then' and 'else' parts are null statements, we're
* evaluating the condition purely for side effects. Simply
* evaluate the condition in this case, since there's no need to so
* much as test the condition once evaluated.
*/
if (then_part_ == 0 && else_part_ == 0)
{
/* generate the condition, discarding the result */
cond_expr_->gen_code(TRUE, TRUE);
/* we're done */
return;
}
/*
* The condition is non-constant, and we have at least one subclause,
* so we must evaluate the condition expression. To minimize the
* amount of jumping, check whether we have a true part, else part, or
* both, and generate the branching accordingly.
*/
if (then_part_ != 0)
{
CTcCodeLabel *lbl_else;
CTcCodeLabel *lbl_end;
/*
* We have a true part, so we will want to evaluate the expression
* and jump past the true part if the expression is false. Create
* a label for the false branch.
*/
lbl_else = G_cs->new_label_fwd();
/* generate the condition expression */
cond_expr_->gen_code_cond(0, lbl_else);
/* generate the 'then' part */
gen_code_substm(then_part_);
/* if there's an 'else' part, generate it */
if (else_part_ != 0)
{
/* at the end of the 'then' part, jump past the 'else' part */
lbl_end = gen_jump_ahead(OPC_JMP);
/* this is the start of the 'else' part */
def_label_pos(lbl_else);
/* generate the 'else' part */
gen_code_substm(else_part_);
/* set the label for the jump over the 'else' part */
def_label_pos(lbl_end);
}
else
{
/*
* there's no 'else' part - set the label for the jump past the
* 'then' part
*/
def_label_pos(lbl_else);
}
}
else
{
CTcCodeLabel *lbl_end;
/*
* There's no 'then' part, so there must be an 'else' part (we
* wouldn't have gotten this far if both 'then' and 'else' are
* empty). To minimize branching, evaluate the condition and jump
* past the 'else' part if the condition is true, falling through
* to the 'else' part otherwise. Create a label for the end of the
* statement, which is also the empty 'then' part.
*/
lbl_end = G_cs->new_label_fwd();
/* evaluate the condition and jump to the end if it's true */
cond_expr_->gen_code_cond(lbl_end, 0);
/* generate the 'else' part */
gen_code_substm(else_part_);
/* set the label for the jump over the 'else' part */
def_label_pos(lbl_end);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'for' statement
*/
/*
* generate code
*/
void CTPNStmFor::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTcCodeLabel *cont_lbl;
CTPNStmEnclosing *old_enclosing;
CTcPrsSymtab *old_frame;
/* set my local frame if necessary */
old_frame = G_cs->set_local_frame(symtab_);
/*
* add a line record - note that we add the line record after
* setting up the local frame, so that the 'for' statement itself
* appears within its own inner scope
*/
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* if there's an initializer expression, generate it */
if (init_expr_ != 0)
init_expr_->gen_code(TRUE, TRUE);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* allocate a forward label for 'continue' jumps */
cont_lbl = G_cs->new_label_fwd();
/* allocate a forward label for the end of the loop */
end_lbl = G_cs->new_label_fwd();
/* generate the implicit conditions for any 'in' clauses */
for (CTPNForIn *i = in_exprs_ ; i != 0 ; i = i->getnxt())
i->gen_forstm_cond(end_lbl);
/*
* If there's an explicit condition clause, generate its code, jumping
* to the end of the loop if the condition is false.
*/
if (cond_expr_ != 0)
cond_expr_->gen_code_cond(0, end_lbl);
/*
* set our labels, so that 'break' and 'continue' statements in our
* body will know where to go
*/
break_lbl_ = end_lbl;
cont_lbl_ = cont_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - we're now generating code again for the
* original 'for' line, even though it's after the body
*/
//$$$ add_debug_line_rec();
/* this is where we come for 'continue' statements */
def_label_pos(cont_lbl);
/* generate the reinitializer expression, if we have one */
if (reinit_expr_ != 0)
reinit_expr_->gen_code(TRUE, TRUE);
/* generate the implicit reinitializers for any 'in' clauses */
for (CTPNForIn *i = in_exprs_ ; i != 0 ; i = i->getnxt())
i->gen_forstm_reinit();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
/* restore the enclosing local scope */
G_cs->set_local_frame(old_frame);
}
/* ------------------------------------------------------------------------ */
/*
* 'for var in expr' node
*/
/*
* Generate code. This is called during the initializer phase of the 'for'
* that we're part of. We create the collection enumerator and assign the
* first value from the enumerator to the lvalue.
*/
void CTPNVarIn::gen_code(int, int)
{
/* generate the iterator initializer */
gen_iter_init(expr_, iter_local_id_, "for");
}
/*
* Static method: generate the iterator initializer
*/
void CTPNVarIn::gen_iter_init(CTcPrsNode *coll_expr, int iter_local_id,
const char *kw)
{
/*
* Look up the createIterator property of the Collection metaclass.
* This property is defined by the Collection specification as the
* property in the first (zeroeth) slot in the method table for
* Collection. If Collection isn't defined, or this slot isn't
* defined, it's an error.
*/
CTcPrsNode *create_iter = G_cg->get_metaclass_prop("collection", 0);
/* if we didn't find the property, it's an error */
if (create_iter != VM_INVALID_PROP)
{
/*
* generate a call to the createIterator() property on the
* collection expression
*/
if (coll_expr != 0)
coll_expr->gen_code_member(FALSE, create_iter, FALSE, 0, FALSE, 0);
/* assign the result to the internal iterator stack local */
CTcSymLocal::s_gen_code_setlcl_stk(iter_local_id, FALSE);
}
else
{
/* tell them about the problem */
G_tok->log_error(TCERR_FOREACH_NO_CREATEITER, kw);
}
}
/*
* Generate code for the condition portion of the 'for'. We check to see
* if there's a next value: if so we fetch it into our lvalue, it not we
* break the loop.
*/
void CTPNVarIn::gen_forstm_cond(CTcCodeLabel *endlbl)
{
/* generate the code */
gen_iter_cond(lval_, iter_local_id_, endlbl, "for");
}
/*
* Generate code for the condition portion of a for..in or a foreach..in.
*/
void CTPNVarIn::gen_iter_cond(CTcPrsNode *lval, int iter_local_id,
CTcCodeLabel *&end_lbl, const char *kw)
{
#if 1
/* if the caller didn't provide an end-of-loop label, create one */
if (end_lbl == 0)
end_lbl = G_cs->new_label_fwd();
/*
* Generate the ITERNEXT <iterator local>, <end offset>. This pushes
* the next iterator value if available, otherwise jumps to the end
* label (without pushing anything onto the stack).
*/
G_cg->write_op(OPC_ITERNEXT);
G_cs->write2(iter_local_id);
G_cs->write_ofs2(end_lbl, 0);
/* we're on the looping branch - ITERNEXT pushed the next value */
G_cg->note_push();
/* assign the next value to the loop control lvalue */
if (lval != 0)
lval->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", 0, FALSE, TRUE, 0);
else
{
G_cg->write_op(OPC_DISC);
G_cg->note_pop();
}
#else
/*
* Look up Iterator.getNext() (iterator metaclass method index 0) and
* Iterator.isNextAvailable() (index 1).
*/
CTcPrsNode *get_next = G_cg->get_metaclass_prop("iterator", 0);
CTcPrsNode *next_avail = G_cg->get_metaclass_prop("iterator", 1);
/* generate the isNextAvailable test */
if (next_avail != 0)
{
/* get the internal iterator local */
CTcSymLocal::s_gen_code_getlcl(iter_local_id, FALSE);
/* generate a call to the property */
CTcPrsNode::s_gen_member_rhs(
FALSE, next_avail, FALSE, 0, FALSE, FALSE);
/*
* generate a jump to the end of the loop, creating the label if
* the caller didn't already
*/
if (end_lbl != 0)
{
/* jump to the caller's end label */
G_cg->write_op(OPC_JF);
G_cs->write_ofs2(end_lbl, 0);
}
else
{
/* we need a new label */
end_lbl = gen_jump_ahead(OPC_JF);
}
/* the JF pops an element off the stack */
G_cg->note_pop();
}
else
{
/* this property is required to be defined - this is an error */
G_tok->log_error(TCERR_FOREACH_NO_ISNEXTAVAIL, kw);
/*
* generate an arbitrary 'end' label if the caller didn't already
* create one - we're not going to end up generating valid code
* anyway, but since we're not going to abort code generation,
* it'll avoid problems elsewhere if we have a valid label assigned
*/
if (end_lbl == 0)
end_lbl = new_label_here();
}
/* generate the code to get the next element of the iteration */
if (get_next != 0)
{
/* get the internal iterator local */
CTcSymLocal::s_gen_code_getlcl(iter_local_id, FALSE);
/* generate a call to the property */
CTcPrsNode::s_gen_member_rhs(FALSE, get_next, FALSE, 0, FALSE, FALSE);
/* assign the result to the iterator lvalue */
if (lval != 0)
lval->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", 0, FALSE, TRUE, 0);
}
else
{
/* this property is required to be defined - this is an error */
G_tok->log_error(TCERR_FOREACH_NO_GETNEXT, kw);
}
#endif
}
/*
* Generate code for the reinit portion of the 'for'. We do nothing on
* this step; we do the test and fetch as one operation in the condition
* instead, since the iterator test has to be done ahead of the fetch.
*/
void CTPNVarIn::gen_forstm_reinit()
{
/* do nothing on this phase */
}
/* ------------------------------------------------------------------------ */
/*
* 'for var in from..to' node
*/
/*
* Generate code. This is called during the initializer pharse of the
* 'for' that we're part of. We evaluate the 'from' and 'to' expressions,
* assign the 'from' to the lvalue, and save the 'to' in a private local
* variable.
*/
void CTPNVarInRange::gen_code(int, int)
{
/* assign the 'from' expression to the lvalue */
lval_->gen_code_asi(TRUE, 1, TC_ASI_SIMPLE, "=", from_expr_,
FALSE, TRUE, 0);
/*
* if the 'to' expression is non-constant, evaluate it and save it in a
* local, so that we don't have to re-evaluate it on each iteration
*/
if (!to_expr_->is_const())
{
/* evaluate the 'to' expression */
to_expr_->gen_code(FALSE, FALSE);
/* assign it to our stack local */
CTcSymLocal::s_gen_code_setlcl_stk(to_local_id_, FALSE);
}
/*
* likewise, if there's a non-constant 'step' expression, evaluate it
* and store it in our step stack local
*/
if (step_expr_ != 0 && !step_expr_->is_const())
{
/* generate the step expression value */
step_expr_->gen_code(FALSE, FALSE);
/* generate the assignment to our local */
CTcSymLocal::s_gen_code_setlcl_stk(step_local_id_, FALSE);
/* replace the step expression with the local */
step_expr_ = new CTPNSymResolved(new CTcSymLocal(
".step", 5, FALSE, FALSE, step_local_id_));
}
}
/*
* Generate code for the condition portion of the 'for'. We test our
* lvalue to see if we've passed the 'to' limit, in which case we break out
* of the loop.
*/
void CTPNVarInRange::gen_forstm_cond(CTcCodeLabel *endlbl)
{
/* push the lvalue */
lval_->gen_code(FALSE, FALSE);
/*
* push the 'to' expression - if it's constant, generate the constant
* expression value, otherwise get the local
*/
if (to_expr_->is_const())
to_expr_->gen_code(FALSE, FALSE);
else
CTcSymLocal::s_gen_code_getlcl(to_local_id_, FALSE);
/*
* If the step expression is a constant, compare based on the sign of
* the constant expression: if positive, we stop when the lval is
* greater than the 'to' limit; if negative, we stop when the lval is
* less than to 'to' limit. If the expression isn't a constant, we
* have to test the sign at run-time.
*/
int s = 0;
if (step_expr_ == 0)
{
/* no step expression -> default step is 1, sign is positive */
s = 1;
}
else if (step_expr_->is_const())
{
/* explicit step expression with a constant value - check the type */
switch (step_expr_->get_const_val()->get_type())
{
case TC_CVT_INT:
/* integer - get its sign */
s = step_expr_->get_const_val()->get_val_int() < 0 ? -1 : 1;
break;
case TC_CVT_FLOAT:
/* BigNumber value - check for a '-' */
s = step_expr_->get_const_val()->get_val_float()[0] == '-'
? -1 : 1;
break;
default:
/* others will have to be interpreted at run-time */
s = 0;
break;
}
}
/*
* if we have a known sign, generate a fixed test, otherwise generate a
* run-time test
*/
if (s > 0)
{
/* positive constant - we're looping up to the 'to' limit */
G_cg->write_op(OPC_JGT);
G_cs->write_ofs2(endlbl, 0);
/* JGT pops 2 */
G_cg->note_pop(2);
}
else if (s < 0)
{
/* negative constant - we're looping down to the 'to' limit */
G_cg->write_op(OPC_JLT);
G_cs->write_ofs2(endlbl, 0);
/* JLT pops 2 */
G_cg->note_pop(2);
}
else
{
/*
* Variable step - generate a run-time test:
*
*. ; test the sign of the step expression: if step < 0 goto $1
*. push <step>
*. push 0
*. jlt $1
*. ; positive branch: if lval > to goto endlbl else goto $2
*. jgt endlbl
*. jmp $2
*. ; negative branch: if lval < to goto endlbl
*. $1:
*. jlt endlbl
*. ; end of test
*. $2:
*/
/* push <step>; push 0; jlt $1 */
step_expr_->gen_code(FALSE, FALSE);
G_cg->write_op(OPC_PUSH_0);
G_cg->note_push();
CTcCodeLabel *l1 = gen_jump_ahead(OPC_JLT);
G_cg->note_pop(2);
/* positive branch: jgt endlbl; jmp $2 */
G_cg->write_op(OPC_JGT);
G_cs->write_ofs2(endlbl, 0);
CTcCodeLabel *l2 = gen_jump_ahead(OPC_JMP);
/* negative branch: $1: jlt endlbl */
def_label_pos(l1);
G_cg->write_op(OPC_JLT);
G_cs->write_ofs2(endlbl, 0);
/* $2: */
def_label_pos(l2);
/*
* we took one or the other of the positive or negative branch, and
* each one did a JxT that popped two elements
*/
G_cg->note_pop(2);
}
}
/*
* Generate code for the reinit portion of the 'for'. We add 'step' to our
* lvalue.
*/
void CTPNVarInRange::gen_forstm_reinit()
{
/*
* If we have a step expression, generate "lval += step". Otherwise
* just generate "++lval".
*/
if (step_expr_ != 0)
{
/* generate "lval += step" */
CTPNAddAsi_gen_code(lval_, step_expr_, TRUE);
}
else
{
/* increment the control variable */
CTPNPreInc_gen_code(lval_, TRUE);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'foreach' statement
*/
/*
* generate code
*/
void CTPNStmForeach::gen_code(int, int)
{
/* set my local frame if necessary */
CTcPrsSymtab *old_frame = G_cs->set_local_frame(symtab_);
/*
* add a line record - note that we add the line record after
* setting up the local frame, so that the 'for' statement itself
* appears within its own inner scope
*/
add_debug_line_rec();
/* push the enclosing statement */
CTPNStmEnclosing *old_enclosing = G_cs->set_enclosing(this);
/* if there's a collection expression, generate it */
if (coll_expr_ != 0)
CTPNVarIn::gen_iter_init(coll_expr_, iter_local_id_, "foreach");
/* set the label for the top of the loop */
CTcCodeLabel *top_lbl = new_label_here();
/* check for and fetch the next value */
CTcCodeLabel *end_lbl = 0;
CTPNVarIn::gen_iter_cond(iter_expr_, iter_local_id_, end_lbl, "foreach");
/*
* set our labels, so that 'break' and 'continue' statements in our
* body will know where to go
*/
break_lbl_ = end_lbl;
cont_lbl_ = top_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - we're now generating code again for the
* original 'foreach' line, even though it's after the body
*/
//$$$ add_debug_line_rec();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
if (end_lbl != 0)
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
/* restore the enclosing local scope */
G_cs->set_local_frame(old_frame);
}
/* ------------------------------------------------------------------------ */
/*
* 'while' statement
*/
/*
* generate code
*/
void CTPNStmWhile::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* generate a label for the end of the loop */
end_lbl = G_cs->new_label_fwd();
/* generate the condition, jumping to the end of the loop if false */
cond_expr_->gen_code_cond(0, end_lbl);
/*
* set the 'break' and 'continue' label in our node, so that 'break'
* and 'continue' statements in subnodes can find the labels during
* code generation
*/
break_lbl_ = end_lbl;
cont_lbl_ = top_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/*
* add another line record - the jump back to the top of the loop is
* part of the 'while' itself
*/
//$$$ add_debug_line_rec();
/* jump back to the top of the loop */
G_cg->write_op(OPC_JMP);
G_cs->write_ofs2(top_lbl, 0);
/*
* we're at the end of the loop - this is where we jump for 'break'
* and when the condition becomes false
*/
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/* ------------------------------------------------------------------------ */
/*
* 'do-while' statement
*/
/*
* generate code
*/
void CTPNStmDoWhile::gen_code(int, int)
{
CTcCodeLabel *top_lbl;
CTcCodeLabel *end_lbl;
CTcCodeLabel *cont_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/* set the label for the top of the loop */
top_lbl = new_label_here();
/* create a label for after the loop, for any enclosed 'break's */
end_lbl = G_cs->new_label_fwd();
/*
* create a label for just before the expression, for any enclosed
* 'continue' statements
*/
cont_lbl = G_cs->new_label_fwd();
/* set our 'break' and 'continue' labels in our node */
break_lbl_ = end_lbl;
cont_lbl_ = cont_lbl;
/* if we have a body, generate it */
if (body_stm_ != 0)
gen_code_substm(body_stm_);
/* set the debug source position to the 'while' clause's location */
add_debug_line_rec(while_desc_, while_linenum_);
/* put the 'continue' label here, just before the condition */
def_label_pos(cont_lbl);
/*
* Generate the condition. If the condition is true, jump back to the
* top label; otherwise fall through out of the loop structure.
*/
cond_expr_->gen_code_cond(top_lbl, 0);
/* we're past the end of the loop - this is where we jump for 'break' */
def_label_pos(end_lbl);
/* restore the enclosing statement */
G_cs->set_enclosing(old_enclosing);
}
/* ------------------------------------------------------------------------ */
/*
* 'break' statement
*/
/*
* generate code
*/
void CTPNStmBreak::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* ask the enclosing statement to do the work - if there's no
* enclosing statement, or none of the enclosing statements can
* perform the break, it's an error
*/
if (G_cs->get_enclosing() == 0
|| !G_cs->get_enclosing()->gen_code_break(lbl_, lbl_len_))
{
/*
* log the error - if there's a label, the problem is that we
* couldn't find the label, otherwise it's that we can't perform
* a 'break' here at all
*/
if (lbl_ == 0)
G_tok->log_error(TCERR_INVALID_BREAK);
else
G_tok->log_error(TCERR_INVALID_BREAK_LBL, (int)lbl_len_, lbl_);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'continue' statement
*/
/*
* generate code
*/
void CTPNStmContinue::gen_code(int, int)
{
/* add a line record */
add_debug_line_rec();
/*
* ask the enclosing statement to do the work - if there's no
* enclosing statement, or none of the enclosing statements can
* perform the break, it's an error
*/
if (G_cs->get_enclosing() == 0
|| !G_cs->get_enclosing()->gen_code_continue(lbl_, lbl_len_))
{
/*
* log the error - if there's a label, the problem is that we
* couldn't find the label, otherwise it's that we can't perform
* a 'break' here at all
*/
if (lbl_ == 0)
G_tok->log_error(TCERR_INVALID_CONTINUE);
else
G_tok->log_error(TCERR_INVALID_CONT_LBL, (int)lbl_len_, lbl_);
}
}
/* ------------------------------------------------------------------------ */
/*
* 'switch' statement
*/
/*
* generate code
*/
void CTPNStmSwitch::gen_code(int, int)
{
CTPNStmSwitch *enclosing_switch;
int i;
char buf[VMB_DATAHOLDER + VMB_UINT2];
CTcCodeLabel *end_lbl;
CTPNStmEnclosing *old_enclosing;
/* add a line record */
add_debug_line_rec();
/* push the enclosing statement */
old_enclosing = G_cs->set_enclosing(this);
/*
* Generate the controlling expression. We want to keep the value,
* hence 'discard' is false, and we need assignment (not 'for
* condition') conversion rules, because we're going to use the
* value in direct comparisons
*/
expr_->gen_code(FALSE, FALSE);
/* make myself the current innermost switch */
enclosing_switch = G_cs->set_switch(this);
/*
* if we can flow out of the switch, allocate a label for the end of
* the switch body
*/
if ((get_control_flow(FALSE) & TCPRS_FLOW_NEXT) != 0)
end_lbl = G_cs->new_label_fwd();
else
end_lbl = 0;
/* the end label is the 'break' location for subnodes */
break_lbl_ = end_lbl;
/*
* Write my SWITCH opcode, and the placeholder case table. We'll
* fill in the case table with its real values as we encounter the
* cases in the course of generating the code. For now, all we know
* is the number of cases we need to put into the table.
*/
G_cg->write_op(OPC_SWITCH);
/* the SWITCH opcode pops the controlling expression value */
G_cg->note_pop();
/* write the number of cases */
G_cs->write2(case_cnt_);
/*
* remember where the first case slot is - the 'case' parse nodes
* will use this to figure out where to write their slot data
*/
case_slot_ofs_ = G_cs->get_ofs();
/*
* Write the placeholder case slots - each case slot gets a
* DATA_HOLDER for the case value, plus an INT2 for the branch
* offset. For now, completely zero each case slot.
*/
memset(buf, 0, VMB_DATAHOLDER + VMB_UINT2);
for (i = 0 ; i < case_cnt_ ; ++i)
G_cs->write(buf, VMB_DATAHOLDER + VMB_UINT2);
/* write a placeholder for the default jump */
if (has_default_)
{
/*
* remember where the 'default' slot is, so that the 'default'
* parse node can figure out where to write its branch offset
*/
default_slot_ofs_ = G_cs->get_ofs();
/*