-
Notifications
You must be signed in to change notification settings - Fork 20
/
parser.y
1532 lines (1269 loc) · 35.7 KB
/
parser.y
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
%token ASM ASSIGN BREAK CLOSEPAREN CLOSESQ.
%token COLON CONST DOT ELSE END EXTERN.
%token IF LOOP MINUS NOT OPENPAREN OPENSQ.
%token PERCENT PLUS RECORD RETURN SEMICOLON SLASH STAR.
%token SUB THEN TILDE VAR WHILE TYPE.
%token OPENBR CLOSEBR ID NUMBER AT BYTESOF ELSEIF.
%token INT TYPEDEF SIZEOF STRING.
%token IMPL DECL EXTERN INTERFACE.
%token NIL.
%left COMMA.
%left AND.
%left OR.
%left PIPE.
%left CARET.
%nonassoc LTOP LEOP GTOP GEOP EQOP NEOP.
%left LSHIFT RSHIFT.
%left PLUS MINUS.
%left STAR SLASH PERCENT.
%left AS.
%left AMPERSAND.
%right NEXT PREV.
%right NOT TILDE.
%right BYTESOF INDEXOF.
%right OPENSQ CLOSESQ.
%right DOT.
%token_type {Token}
%syntax_error
{
StartError();
print("unexpected ");
print(yyTokenName[yymajor]);
EndError();
}
%stack_size 100
%stack_overflow
{
StartError();
print("parser stack overflow");
EndError();
}
%token_destructor
{
if (yymajor == STRING) or (yymajor == ID) then
print("free unused string\n");
Free($$.string as [uint8]);
end if;
}
program ::= statements.
statements ::= /* empty */.
statements ::= statements statement.
/* --- Top-level statements ---------------------------------------------- */
statement ::= SEMICOLON.
/* --- Simple statements ------------------------------------------------- */
statement ::= RETURN SEMICOLON.
{
Generate(MidReturn());
}
/* --- Variable declaration ---------------------------------------------- */
statement ::= VAR newid(S) COLON typeref(T) SEMICOLON.
{
InitVariable(current_subr, S, T);
}
statement ::= VAR newid(S) COLON typeref(T) ASSIGN expression(E) SEMICOLON.
{
InitVariable(current_subr, S, T);
CheckExpressionType(E, S.vardata.type);
var w := E.type.width as uint8;
Generate(MidStore(w, E, MidDeref(w, MidAddress(S, 0))));
}
statement ::= VAR newid(S) ASSIGN expression(E) SEMICOLON.
{
var type := E.type;
if type == (0 as [Type]) then
SimpleError("types cannot be inferred for untyped constants");
end if;
if IsScalar(type) == 0 then
SimpleError("you can only assign scalars");
end if;
InitVariable(current_subr, S, type);
CheckExpressionType(E, S.vardata.type);
var w := E.type.width as uint8;
Generate(MidStore(w, E, MidDeref(w, MidAddress(S, 0))));
}
/* --- Assignments ------------------------------------------------------- */
statement ::= expression(E1) ASSIGN expression(E2) SEMICOLON.
{
var type := E1.type;
var address := UndoLValue(E1);
CheckExpressionType(E2, type);
var w := type.width as uint8;
Generate(MidStore(w, E2, MidDeref(w, address)));
}
/* --- Simple loops ------------------------------------------------------ */
%include
{
sub BeginNormalLoop(): (ll: [LoopLabels]) is
ll := InternalAlloc(@bytesof LoopLabels) as [LoopLabels];
ll.loop_label := AllocLabel();
ll.exit_label := AllocLabel();
ll.old_break_label := break_label;
ll.old_continue_label := continue_label;
break_label := ll.exit_label;
continue_label := ll.loop_label;
end sub;
sub TerminateNormalLoop(ll: [LoopLabels]) is
Generate(MidJump(continue_label));
Generate(MidLabel(break_label));
break_label := ll.old_break_label;
continue_label := ll.old_continue_label;
Free(ll as [uint8]);
end sub;
}
statement ::= startloopstatement(LL) statements END LOOP.
{
TerminateNormalLoop(LL);
}
%type startloopstatement {[LoopLabels]}
startloopstatement(LL) ::= LOOP.
{
LL := BeginNormalLoop();
Generate(MidLabel(continue_label));
}
/* --- While loops ------------------------------------------------------- */
statement ::= startwhilestatement(LL) statements END LOOP.
{
TerminateNormalLoop(LL);
}
%type initwhilestatement {[LoopLabels]}
initwhilestatement(LL) ::= WHILE.
{
LL := BeginNormalLoop();
Generate(MidLabel(continue_label));
}
%type startwhilestatement {[LoopLabels]}
startwhilestatement(LL) ::= initwhilestatement(LL1) conditional(C) LOOP.
{
LL := LL1;
var t := AllocLabel();
break_label := AllocLabel();
C.beq.truelabel := t;
C.beq.falselabel := break_label;
C.beq.fallthrough := t;
GenerateConditional(C);
LL.exit_label := break_label;
}
/* --- Simple jumps ------------------------------------------------------ */
statement ::= BREAK SEMICOLON.
{
if break_label == 0 then
SimpleError("nothing to break to");
end if;
Generate(MidJump(break_label));
}
statement ::= CONTINUE SEMICOLON.
{
if continue_label == 0 then
SimpleError("nothing to continue to");
end if;
Generate(MidJump(continue_label));
}
/* --- If...Then...Else...End if ----------------------------------------- */
statement ::= IF if_begin if_conditional THEN statements if_optional_else END IF.
{
Generate(MidLabel(current_if.exit_label));
var oldif := current_if;
current_if := current_if.next;
Free(oldif as [uint8]);
}
if_begin ::= .
{
var newif := InternalAlloc(@bytesof IfLabels) as [IfLabels];
newif.next := current_if;
current_if := newif;
current_if.exit_label := AllocLabel();
}
if_conditional ::= conditional(C).
{
var t := AllocLabel();
var f := AllocLabel();
current_if.true_label := t;
current_if.false_label := f;
C.beq.truelabel := t;
C.beq.falselabel := f;
C.beq.fallthrough := t;
GenerateConditional(C);
}
if_optional_else ::= .
{
Generate(MidLabel(current_if.false_label));
}
if_optional_else ::= if_else statements.
if_optional_else ::= if_elseif if_conditional THEN statements if_optional_else.
if_else ::= ELSE.
{
Generate(MidJump(current_if.exit_label));
Generate(MidLabel(current_if.false_label));
}
if_elseif ::= ELSEIF.
{
Generate(MidJump(current_if.exit_label));
Generate(MidLabel(current_if.false_label));
}
/* --- Case -------------------------------------------------------------- */
statement ::= startcase whens END CASE SEMICOLON.
{
if (current_case.seenelse == 0) and (current_case.next_label != 0) then
Generate(MidLabel(current_case.next_label));
end if;
Generate(MidLabel(current_case.end_label));
Generate(MidEndcase(current_case.width));
var c := current_case;
current_case := c.old_case;
Free(c as [uint8]);
}
startcase ::= CASE expression(E) IS.
{
var c := InternalAlloc(@bytesof CaseLabels) as [CaseLabels];
c.old_case := current_case;
c.end_label := AllocLabel();
current_case := c;
if IsNum(E.type) == 0 then
SimpleError("case only works on numbers");
end if;
c.width := NodeWidth(E);
Generate(MidStartcase(c.width, E));
}
whens ::= .
whens ::= whens when.
when ::= beginwhen statements.
beginwhen ::= WHEN cvalue(C) COLON.
{
if current_case.seenelse != 0 then
SimpleError("when else must go last");
end if;
if current_case.next_label != 0 then
Generate(MidJump(current_case.end_label));
Generate(MidLabel(current_case.next_label));
end if;
current_case.next_label := AllocLabel();
Generate(MidWhencase(current_case.width, C, current_case.next_label));
}
beginwhen ::= WHEN ELSE COLON.
{
if current_case.seenelse != 0 then
SimpleError("only one when else allowed");
end if;
if current_case.next_label != 0 then
Generate(MidJump(current_case.end_label));
Generate(MidLabel(current_case.next_label));
end if;
current_case.next_label := 0;
current_case.seenelse := 1;
}
/* --- Conditional expressions ------------------------------------------- */
%type conditional {[Node]}
conditional(R) ::= OPENPAREN conditional(C) CLOSEPAREN.
{
R := C;
}
%include
{
sub Negate(node: [Node]) is
node.beq.negated := node.beq.negated ^ 1;
end sub;
}
conditional(R) ::= NOT conditional(C).
{
R := C;
Negate(C);
}
conditional(R) ::= conditional(C1) AND conditional(C2).
{
R := MidBand(C1, C2, 0, 0, 0, 0);
}
conditional(R) ::= conditional(C1) OR conditional(C2).
{
R := MidBor(C1, C2, 0, 0, 0, 0);
}
%include
{
sub ConditionalEq(lhs: [Node], rhs: [Node], negated: uint8): (result: [Node]) is
CondSimple(lhs, rhs);
var truelabel := AllocLabel();
var falselabel := AllocLabel();
var w := NodeWidth(lhs);
result := MidBeq(w, lhs, rhs, truelabel, falselabel, 0, negated);
end sub;
sub ConditionalLt(lhs: [Node], rhs: [Node], negated: uint8): (result: [Node]) is
CondSimple(lhs, rhs);
var truelabel := AllocLabel();
var falselabel := AllocLabel();
var w := NodeWidth(lhs);
if IsSNum(lhs.type) != 0 then
result := MidBlts(w, lhs, rhs, truelabel, falselabel, 0, negated);
else
result := MidBltu(w, lhs, rhs, truelabel, falselabel, 0, negated);
end if;
end sub;
}
conditional(R) ::= expression(T1) EQOP expression(T2).
{
R := ConditionalEq(T1, T2, 0);
}
conditional(R) ::= expression(T1) NEOP expression(T2).
{
R := ConditionalEq(T1, T2, 1);
}
conditional(R) ::= expression(T1) LTOP expression(T2).
{
R := ConditionalLt(T1, T2, 0);
}
conditional(R) ::= expression(T1) GEOP expression(T2).
{
R := ConditionalLt(T1, T2, 1);
}
conditional(R) ::= expression(T1) GTOP expression(T2).
{
R := ConditionalLt(T2, T1, 0);
}
conditional(R) ::= expression(T1) LEOP expression(T2).
{
R := ConditionalLt(T2, T1, 1);
}
/* --- Expressions ------------------------------------------------------- */
%type leafexpression {[Node]}
leafexpression(E) ::= NUMBER(T). { E := MidConstant(T.number); }
leafexpression(E) ::= OPENPAREN expression(E1) CLOSEPAREN. { E := E1; }
leafexpression(E) ::= NIL. { E := MidConstant(0); }
%type expression {[Node]}
expression(E) ::= leafexpression(E1). { E := E1; }
expression(E) ::= MINUS expression(E1). { E := Expr1Simple(MIDCODE_NEG0, E1); }
expression(E) ::= TILDE expression(E1). { E := Expr1Simple(MIDCODE_NOT0, E1); }
expression(E) ::= expression(E1) PLUS expression(E2). { E := ExprAdd(E1, E2); }
expression(E) ::= expression(E1) MINUS expression(E2). { E := ExprSub(E1, E2); }
expression(E) ::= expression(E1) STAR expression(E2). { E := Expr2Simple(MIDCODE_MUL0, MIDCODE_MUL0, E1, E2); }
expression(E) ::= expression(E1) SLASH expression(E2). { E := Expr2Simple(MIDCODE_DIVS0, MIDCODE_DIVU0, E1, E2); }
expression(E) ::= expression(E1) PERCENT expression(E2). { E := Expr2Simple(MIDCODE_REMS0, MIDCODE_REMU0, E1, E2); }
expression(E) ::= expression(E1) CARET expression(E2). { E := Expr2Simple(MIDCODE_EOR0, MIDCODE_EOR0, E1, E2); }
expression(E) ::= expression(E1) AMPERSAND expression(E2). { E := Expr2Simple(MIDCODE_AND0, MIDCODE_AND0, E1, E2); }
expression(E) ::= expression(E1) PIPE expression(E2). { E := Expr2Simple(MIDCODE_OR0, MIDCODE_OR0, E1, E2); }
expression(E) ::= expression(E1) LSHIFT expression(E2). { E := ExprShift(MIDCODE_LSHIFT0, MIDCODE_LSHIFT0, E1, E2); }
expression(E) ::= expression(E1) RSHIFT expression(E2). { E := ExprShift(MIDCODE_RSHIFTS0, MIDCODE_RSHIFTU0, E1, E2); }
expression(E) ::= expression(E1) AS typeref(T).
{
CheckNotPartialType(T);
if (E1.op != MIDCODE_CONSTANT) and (E1.type.width != T.width) then
if (IsPtr(E1.type) != 0) or (IsPtr(T) != 0) then
SimpleError("cast between pointer and non-pointer of different size");
end if;
E := MidCCast(T.width as uint8, E1, IsSNum(E1.type));
else
E := E1;
end if;
E.type := T;
}
expression(E) ::= AMPERSAND expression(E1).
{
E := UndoLValue(E1);
if E.op == MIDCODE_ADDRESS then
var sym := E.address.sym;
if IsScalar(sym.vardata.type) != 0 then
SimpleError("you cannot take the address of scalar variables");
end if;
end if;
}
expression(E) ::= ALIAS AMPERSAND expression(E1).
{
E := UndoLValue(E1);
}
%include
{
sub parser_i_bad_next_prev() is
SimpleError("@next and @prev only work on pointers");
end sub;
}
expression(E) ::= NEXT expression(E1).
{
if IsPtr(E1.type) == 0 then
parser_i_bad_next_prev();
end if;
E := MidC2Op(MIDCODE_ADD0,
intptr_type.width as uint8,
E1,
MidConstant(E1.type.pointertype.element.stride as Arith)
);
E.type := E1.type;
}
expression(E) ::= PREV expression(E1).
{
if IsPtr(E1.type) == 0 then
parser_i_bad_next_prev();
end if;
E := MidC2Op(MIDCODE_SUB0,
intptr_type.width as uint8,
E1,
MidConstant(E1.type.pointertype.element.stride as Arith)
);
E.type := E1.type;
}
expression(E) ::= BYTESOF varortypeid(T).
{
E := MidConstant(T.width as Arith);
}
expression(E) ::= SIZEOF varortypeid(T).
{
if IsArray(T) == 0 then
SimpleError("array expected");
end if;
E := MidConstant(T.arraytype.size as Arith);
}
leafexpression(E) ::= oldid(S).
{
sub not_a_value() is
StartError();
print(S.name);
print(" is not a value");
EndError();
end sub;
case S.kind is
when CONST:
E := MidConstant(S.constant);
when VAR:
E := MidAddress(S, 0);
E.type := MakePointerType(S.vardata.type);
E := MakeLValue(E);
when TYPE:
# Subroutine instances are passed around as their type, even if it
# does not *really* make sense. Every type must have a symbol, so
# by using the type as a subroutine literal, we save on having to
# create an extra symbol for it.
if S.typedata.kind == TYPE_SUBROUTINE then
E := MidSubref(S.typedata.subrtype.subr);
E.type := S.typedata.subrtype.subr.intfsubr.type;
else
not_a_value();
end if;
when else:
not_a_value();
end case;
}
leafexpression(E) ::= OPENSQ expression(E1) CLOSESQ.
{
if IsPtr(E1.type) == 0 then
SimpleError("cannot dereference non-pointers");
end if;
E := MakeLValue(E1);
}
expression(E) ::= expression(E1) OPENSQ expression(E2) CLOSESQ.
{
var type := E1.type;
var address := MaybeUndoLValue(E1);
if IsArray(type) == 0 then
StartError();
print("you can only index an array, not a ");
print(type.symbol.name);
EndError();
end if;
CheckExpressionType(E2, type.arraytype.indextype);
if IsNum(E2.type) == 0 then
SimpleError("array indices must be numbers");
end if;
var elementtype := type.arraytype.element;
var w := intptr_type.width as uint8;
var displacement := MidC2Op(MIDCODE_MUL0, w,
MidCCast(intptr_type.width as uint8, E2, 0),
MidConstant(elementtype.stride as int32));
displacement.type := intptr_type;
var adjustedaddress := MidC2Op(MIDCODE_ADD0, w, address, displacement);
adjustedaddress.type := MakePointerType(elementtype);
E := MakeLValue(adjustedaddress);
}
expression(E) ::= expression(E1) DOT ID(X).
{
var type := E1.type;
var address := E1;
sub BadType() is
StartError();
print(type.symbol.name);
print(" is not a record or pointer to a record");
EndError();
end sub;
if IsLValue(address) != 0 then
address := MaybeUndoLValue(E1);
# Dereference pointers.
while IsPtr(type) != 0 loop
type := type.pointertype.element;
CheckNotPartialType(type);
address := MidDeref(intptr_type.width as uint8, address);
end loop;
elseif IsPtr(type) != 0 then
type := type.pointertype.element;
else
BadType();
end if;
CheckNotPartialType(type);
if IsRecord(type) == 0 then
BadType();
end if;
var member := LookupSymbol(&type.recordtype.namespace, X.string);
if member == (0 as [Symbol]) then
StartError();
print(type.symbol.name);
print(" does not contain a member '");
print(X.string);
print("'");
EndError();
end if;
Free(X.string as [uint8]);
E := MidC2Op(MIDCODE_ADD0,
intptr_type.width as uint8,
address, MidConstant(member.vardata.offset as Arith));
E.type := MakePointerType(member.vardata.type);
E := MakeLValue(E);
}
leafexpression(E) ::= STRING(S).
{
# consumes S
E := MidString(S.string);
E.type := MakePointerType(uint8_type);
}
%include
{
sub parser_i_constant_error() is
SimpleError("only constant values are allowed here");
end sub;
}
%type cvalue {Arith}
cvalue(C) ::= expression(E).
{
if E.op != MIDCODE_CONSTANT then
parser_i_constant_error();
end if;
C := E.constant.value;
Discard(E);
}
statement ::= CONST newid(S) ASSIGN cvalue(C) SEMICOLON.
{
S.kind := CONST;
S.constant := C;
}
/* --- Types ------------------------------------------------------------- */
%type typeref {[Type]}
typeref(S) ::= INT OPENPAREN cvalue(MIN) COMMA cvalue(MAX) CLOSEPAREN.
{
if MAX <= MIN then
SimpleError("invalid integer type range");
end if;
S := ArchGuessIntType(MIN, MAX);
}
typeref(S) ::= eitherid(ID).
{
var sym := ID;
if sym.kind == 0 then
# Create a partial type.
var type := AllocNewType();
type.kind := TYPE_PARTIAL;
sym.kind := TYPE;
sym.typedata := type;
type.symbol := sym;
end if;
if sym.kind != TYPE then
StartError();
print("expected ");
print(sym.name);
print(" to be a type");
EndError();
end if;
S := sym.typedata;
}
typeref(S) ::= OPENSQ typeref(S1) CLOSESQ.
{
S := MakePointerType(S1);
}
typeref(S) ::= typeref(S1) OPENSQ cvalue(C) CLOSESQ.
{
S := MakeArrayType(S1, C as uint16);
}
typeref(S) ::= typeref(S1) OPENSQ CLOSESQ.
{
S := MakeArrayType(S1, 0);
}
typeref(S) ::= INDEXOF varortypeid(S1).
{
if IsArray(S1) == 0 then
StartError();
print(S1.symbol.name);
print(" is not an array");
EndError();
end if;
S := S1.arraytype.indextype;
}
statement ::= TYPEDEF ID(X) IS typeref(T) SEMICOLON.
{
# consumes X
var sym := AddAlias(0 as [Namespace], X.string, T.symbol);
}
/* --- Symbols ----------------------------------------------------------- */
%type newid {[Symbol]}
newid(S) ::= ID(T).
{
# consumes X
S := AddSymbol(0 as [Namespace], T.string);
}
%type oldid {[Symbol]}
oldid(S) ::= ID(T).
{
var name := T.string;
var sym := LookupSymbol(0 as [Namespace], name);
if sym == (0 as [Symbol]) then
StartError();
print("symbol '");
print(name);
print("' not found");
EndError();
end if;
Free(name);
S := sym;
}
%type eitherid {[Symbol]}
eitherid(S) ::= ID(T).
{
var name := T.string;
var sym := LookupSymbol(0 as [Namespace], name);
if sym == (0 as [Symbol]) then
sym := AddSymbol(0 as [Namespace], name);
else
Free(name);
end if;
S := sym;
}
%type varortypeid {[Type]}
varortypeid(T) ::= oldid(S).
{
case S.kind is
when VAR:
T := S.vardata.type;
when TYPE:
T := S.typedata;
when else:
SimpleError("simple type or variable name expected");
end case;
}
varortypeid(T) ::= OPENPAREN typeref(T1) CLOSEPAREN.
{
T := T1;
}
/* --- Subroutine calls -------------------------------------------------- */
%include
{
sub i_check_sub_call_args() is
var subr := current_call.intfsubr;
if current_call.num_input_args != subr.num_input_parameters then
StartError();
print("subroutine ");
print(subr.symbol.name);
print(" takes ");
print_i8(subr.num_input_parameters);
print(" but was given ");
print_i8(current_call.num_input_args);
EndError();
end if;
end sub;
sub i_end_call() is
EmitterReferenceSubroutine(current_subr, current_call.intfsubr);
var call := current_call;
current_call := call.parent;
Free(call as [uint8]);
end sub;
}
expression(E) ::= startsubcall inputargs(INA).
{
var intfsubr := current_call.intfsubr;
current_call.num_output_args := 1;
i_check_sub_call_args();
if intfsubr.num_output_parameters != 1 then
SimpleError("subroutines called as functions must have exactly one output parameter");
end if;
var param := GetOutputParameter(intfsubr, 0);
var w := param.vardata.type.width as uint8;
var temp := AddSymbol(¤t_subr.namespace, 0 as string);
InitVariable(current_subr, temp, param.vardata.type);
Generate(MidCall(INA, current_call.expr, intfsubr));
Generate(MidStore(w, MidPoparg(w, intfsubr, param, 0), MidDeref(w, MidAddress(temp, 0))));
i_end_call();
E := MidDeref(w, MidAddress(temp, 0));
E.type := param.vardata.type;
}
statement ::= startsubcall inputargs(INA) SEMICOLON.
{
var intfsubr := current_call.intfsubr;
i_check_sub_call_args();
if intfsubr.num_output_parameters != 0 then
SimpleError("subroutine requires output arguments");
end if;
Generate(MidCall(INA, current_call.expr, intfsubr));
i_end_call();
}
statement ::= outputargs(OUTA) ASSIGN startsubcall inputargs(INA) SEMICOLON.
{
var intfsubr := current_call.intfsubr;
i_check_sub_call_args();
Generate(MidCall(INA, current_call.expr, intfsubr));
var paramindex := intfsubr.num_output_parameters;
var count: uint8 := 0;
var node := OUTA;
while node != (0 as [Node]) loop
if paramindex == 0 then
SimpleError("too many output arguments");
end if;
paramindex := paramindex - 1;
var param := GetOutputParameter(intfsubr, paramindex);
var arg := node.left;
node.left := (0 as [Node]);
node := node.right;
if IsPtr(arg.type) == 0 then
SimpleError("you can only assign to lvalues");
end if;
CheckExpressionType(arg, MakePointerType(param.vardata.type));
CheckNotPartialType(param.vardata.type);
CheckNotPartialType(arg.type);
var w := param.vardata.type.width as uint8;
Generate(
MidStore(
w,
MidPoparg(w, intfsubr, param, count),
MidDeref(w, arg)
)
);
count := count + 1;
param := param.vardata.next_parameter;
end loop;
Discard(OUTA);
if count != intfsubr.num_output_parameters then
SimpleError("too few output arguments");
end if;
i_end_call();
}
startsubcall ::= leafexpression(E).
{
if IsSubroutine(E.type) == 0 then
StartError();
print("expected ");
print(E.type.symbol.name);
print(" to be a subroutine");
EndError();
end if;
var call := InternalAlloc(@bytesof SubroutineCall) as [SubroutineCall];
call.parent := current_call;
var subr := E.type.subrtype.subr;
call.expr := E;
call.intfsubr := subr;
call.input_parameter := GetInputParameter(subr, 0);
call.output_parameter := GetOutputParameter(subr, 0);
current_call := call;
}
/* Produces a list of arguments to be passed into a CALL. The leftmost
* argument is the *deepest*, so the top of the list is the rightmost.
*/
%type inputargs {[Node]}
inputargs(R) ::= OPENPAREN inputarglist(L) CLOSEPAREN.
{ R := L; }
inputargs(R) ::= OPENPAREN CLOSEPAREN.
{ R := MidEnd(); }
%type inputarglist {[Node]}
inputarglist(R) ::= inputarg(A).
{ R := A; }
inputarglist(R) ::= inputarglist(L) COMMA inputarg(A).
{
Discard(A.left);
A.left := L;
R := A;
}
%type inputarg {[Node]}
inputarg(R) ::= expression(E).
{
var param := current_call.input_parameter;
if param == (0 as [Symbol]) then
StartError();
print("too many parameters in call to ");
print(current_call.intfsubr.symbol.name);
EndError();
end if;
current_call.input_parameter := current_call.input_parameter.vardata.next_parameter;
CheckExpressionType(E, param.vardata.type);
CheckNotPartialType(param.vardata.type);
CheckNotPartialType(E.type);
current_call.num_input_args := current_call.num_input_args + 1;
R := MidArg(NodeWidth(E), MidEnd(), E,
current_call.intfsubr,
param,
current_call.intfsubr.num_input_parameters - current_call.num_input_args);
}
/* Output arguments get parsed before we know what subroutine they belong
* to. So, we can't type check and emit code for them. Instead we assemble
* them into a chain of PAIRs so that the rule above which handles multi-
* return-value statements can do the type checking and codegen.
*
* Note that this list is left-recursive, which means that the *last*
* parameter is on the *head* of the list.
*/
%type outputargs {[Node]}
outputargs(R) ::= OPENPAREN outputarglist(L) COMMA outputarg(E) CLOSEPAREN.
{
R := MidPair(E, L);
}
%type outputarglist {[Node]}
outputarglist(A) ::= outputarg(E).
{
A := MidPair(E, 0 as [Node]);
}
outputarglist(A) ::= outputarglist(L) COMMA outputarg(E).
{
A := MidPair(E, L);
}
%type outputarg {[Node]}
outputarg(E) ::= expression(E1).
{
E := UndoLValue(E1);
}
/* --- Subroutine definitions -------------------------------------------- */
%include
{
var preparing_subr: [Subroutine];
}
// Declare and implement a subroutine.
statement ::= SUB newsubid subparams submodifiers substart statements subend SEMICOLON.
// Declare a subroutine but don't implement it.
statement ::= DECL SUB newsubid subparams submodifiers SEMICOLON.
// Implement a previously declared subroutine.
statement ::= subimpldecl substart statements subend SEMICOLON.
// Declare an interface.
statement ::= INTERFACE newsubid subparams submodifiers SEMICOLON.