-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo2asm.y
1304 lines (1224 loc) · 30.6 KB
/
algo2asm.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
%define parse.error verbose
%locations
%{
#define _POSIX_C_SOURCE 200809L
#ifndef LABEL_SIZE
#define LABEL_SIZE 128
#endif // LABEL_SIZE
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "status.h"
#include "types.h"
#include "stable.h"
#include "stack.h"
#define C_RED "\x1B[31m"
#define C_RESET "\x1B[0m"
// Variables labels [[
static unsigned int new_label_number() {
static unsigned int current_label_number = 0u;
if ( current_label_number == UINT_MAX ) {
fail_with("Error: maximum label number reached!\n");
}
return current_label_number++;
}
static void create_label(char *buf, size_t buf_size, const char *format, ...) {
va_list ap;
va_start(ap, format);
if ( vsnprintf(buf, buf_size, format, ap) >= buf_size ) {
va_end(ap);
fail_with("Error in label generation: size of label exceeds maximum size!\n");
}
va_end(ap);
}
// ]]
int yylex(void);
void yyerror(char const *);
extern FILE *yyin;
extern int yylineno;
static int fd, offset = 0;
char *target_name = NULL;
symbol_table_entry *curr_fun = NULL;
enum reg {
AX,
BX,
CX,
DX
};
/**
* @function fail_with
* @abstract Terminate the program outputing an error like fprintf.
* @param format... defines the format of the output like printf
*/
void fail_with(const char *format, ...);
/**
* @function show_error
* @abstract show the error and continue reading the file.
* @param format... defines the format of the output like printf
*/
void show_error(const char *format, ...);
/**
* @function true_from_positive
* @abstract load the value on top of the stack as a boolean
* @param r the register used to store the boolean
*/
void true_from_positive(enum reg r);
/**
* @function load_addr
* @abstract store the addr of the var or parameter defined in ste in the register r
* @param r the register used to store the addr
* @param ste the symbol table entry of the var or parameter
*/
void load_addr(enum reg r, symbol_table_entry *ste);
%}
%union {
int integer;
char id[64];
status s;
}
%type<s> func lparams id linst inst call_params param expr
dofori_b doford_b doforis_b dofords_b
%token ALGO_B ALGO_E
IF ELSE DOWHILE DOFORI DOFORIS DOFORD DOFORDS DO REPEAT
FI OD WHILEOD UNTIL
CALL RETURN
IGNORE INVALID
TIMES INCR DECR
TRUE FALSE AND OR NOT EQ NEQ LTH GTH LEQ GEQ
%token<id> ID
%token<integer> NUMBER
%left AFFECT
%left OR
%left AND
%left EQ NEQ LTH GTH LEQ GEQ
%left '+' '-'
%left '*' TIMES '/'
%right UNOT
%right UMINUS
%start func
%%
func:
algo_b '{' lparams '}' linst algo_e {
if ($3 >= ERR_TYP || $5 >= ERR_TYP) {
fail_with("invalid algorithm, exiting...\n");
}
}
;
algo_b:
ALGO_B '{' ID '}' {
curr_fun = new_symbol_table_entry($3);
curr_fun->class = FUNCTION;
curr_fun->nParams = 0;
curr_fun->desc[0] = INT_T;
dprintf(fd, ":%s\n", $3);
}
;
lparams:
%empty { $$ = STATEMENT; }
| id ',' lparams {
status s = 0;
if ((s = $3) >= ERR_TYP || (s = $1) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| id { $$ = $1; }
;
id:
ID {
if (search_symbol_table($1) != NULL) {
$$ = ERR_DEC;
show_error("line %d: duplicated var \"%s\".\n", yylineno,$1);
} else {
$$ = STATEMENT;
symbol_table_entry *ste = new_symbol_table_entry($1);
ste->add = ++curr_fun->nParams;
curr_fun->desc[ste->add] = INT_T;
ste->class = PARAMETER;
ste->desc[0] = INT_T;
}
}
;
algo_e:
ALGO_E {
for(size_t i = 0; i < curr_fun->nParams + curr_fun->nLocalVariables; i++) {
free_first_symbol_table_entry();
}
}
;
linst:
inst {
if ($1 >= ERR_TYP) {
$$ = $1;
switch ($1) {
case ERR_TYP:
yyerror("incompatible types.");
break;
case ERR_DIV:
yyerror("division by 0.");
break;
case ERR_DEC:
yyerror("undeclared variable.");
break;
default:;
}
yyerrok;
} else {
$$ = STATEMENT;
}
}
| error {
$$ = ERR_SYN;
yyerrok;
}
| error linst {
$$ = ERR_SYN;
yyerrok;
}
| inst linst {
status s = 0;
if ((s = $2) >= ERR_TYP || (s = $1) >= ERR_TYP) {
$$ = s;
switch (s) {
case ERR_TYP:
yyerror("incompatible types.");
break;
case ERR_DIV:
yyerror("division by 0.");
break;
case ERR_DEC:
yyerror("undeclared variable.");
break;
default:;
}
yyerrok;
} else {
$$ = STATEMENT;
}
}
;
inst:
AFFECT '{' ID '}' '{' expr '}' {
if ($6 >= ERR_TYP) {
$$ = $6;
} else {
symbol_table_entry *ste;
if ((ste = search_symbol_table($3)) == NULL) {
$$ = STATEMENT;
symbol_table_entry *ste = new_symbol_table_entry($3);
ste->class = LOCAL_VARIABLE;
ste->add = ++curr_fun->nLocalVariables;
ste->desc[0] = INT_T;
--offset;
} else {
$$ = STATEMENT;
dprintf(fd, "\tpop ax\n");
--offset;
load_addr(CX, ste);
dprintf(fd, "\tstorew ax,cx\n");
}
}
}
| INCR '{' ID '}' {
symbol_table_entry *ste;
if ((ste = search_symbol_table($3)) == NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
load_addr(CX, ste);
dprintf(fd, "\tloadw ax,cx\n"
"\tconst bx,1\n"
"\tadd ax,bx\n"
"\tstorew ax,cx\n");
}
}
| DECR '{' ID '}' {
symbol_table_entry *ste;
if ((ste = search_symbol_table($3)) == NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
load_addr(CX, ste);
dprintf(fd, "\tloadw ax,cx\n"
"\tconst bx,1\n"
"\tsub ax,bx\n"
"\tstorew ax,cx\n");
}
}
| IF '{' expr '}' if_b linst if_e end FI {
status s = 0;
if ((s = $3) >= ERR_TYP || (s = $6) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| IF '{' expr '}' if_b linst ELSE else_b if_e linst else_e end FI {
status s = 0;
if ((s = $3) >= ERR_TYP || (s = $6) >= ERR_TYP || (s = $10) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DOFORI dofori_b linst dofor_e end OD {
status s = 0;
if ((s = $2) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DOFORIS doforis_b linst dofor_e end OD {
status s = 0;
if ((s = $2) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DOFORD doford_b linst dofor_e end OD {
status s = 0;
if ((s = $2) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DOFORDS dofords_b linst dofor_e end OD {
status s = 0;
if ((s = $2) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DOWHILE dowhile_t '{' expr '}' dowhile_b linst dowhile_e end OD {
status s = 0;
if ((s = $4) >= ERR_TYP || (s = $7) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| DO do_b linst WHILEOD '{' expr '}' do_e end {
status s = 0;
if ((s = $3) >= ERR_TYP || (s = $6) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| REPEAT repeat_b linst UNTIL '{' expr '}' repeat_e end {
status s = 0;
if ((s = $3) >= ERR_TYP || (s = $6) >= ERR_TYP) {
$$ = s;
} else {
$$ = STATEMENT;
}
}
| RETURN '{' expr '}' {
if ($3 >= ERR_TYP) {
$$ = $3;
} else {
$$ = STATEMENT;
--offset;
dprintf(fd, "\tpop ax\n");
for (size_t i = 0; i < curr_fun->nLocalVariables; i++) {
dprintf(fd, "\tpop dx\n");
}
dprintf(fd, "\tret\n");
}
}
| IGNORE {
$$ = STATEMENT;
}
| INVALID {
$$ = ERR_SYN;
fail_with("Instruction not implementable.\n");
}
;
if_b: %empty {
push(new_label_number());
unsigned int n = top();
char nlabel[LABEL_SIZE] = {0};
create_label(nlabel, LABEL_SIZE, "if_f%u", n);
--offset;
dprintf(fd, "\tpop ax\n"
"\tconst bx,0\n"
"\tconst cx,%s\n", nlabel);
dprintf(fd, "\tcmp ax,bx\n"
"\tjmpc cx\n");
}
;
if_e: %empty {
unsigned int n = top();
char nlabel[LABEL_SIZE] = {0};
create_label(nlabel, LABEL_SIZE, "if_f%u", n);
dprintf(fd, ":%s\n", nlabel);
}
;
else_b: %empty {
unsigned int n = top();
char nlabel[LABEL_SIZE] = {0};
create_label(nlabel, LABEL_SIZE, "j_else%u", n);
dprintf(fd, "\tconst cx,%s\n", nlabel);
dprintf(fd, "\tjmp cx\n");
}
;
else_e: %empty {
unsigned int n = top();
char nlabel[LABEL_SIZE] = {0};
create_label(nlabel, LABEL_SIZE, "j_else%u", n);
dprintf(fd, ":%s\n", nlabel);
}
;
dofori_b: '{' ID '}' '{' expr '}' '{' expr '}' {
if ($5 >= ERR_TYP) {
$$ = $5;
} else if ($5 != INT) {
$$ = ERR_TYP;
} else {
if (search_symbol_table($2) != NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
push(new_label_number());
unsigned int n = top();
// INIT LOOP VAR
symbol_table_entry *var = new_symbol_table_entry($2);
var->class = LOCAL_VARIABLE;
var->add = ++curr_fun->nLocalVariables;
var->desc[0] = INT_T;
--offset;
// SAVE END VAL
char buf[16] = {0};
snprintf(buf, sizeof(buf), "%u", n);
symbol_table_entry *endval = new_symbol_table_entry(buf);
endval->class = LOCAL_VARIABLE;
endval->add = ++curr_fun->nLocalVariables;
endval->desc[0] = INT_T;
--offset;
char label_start[LABEL_SIZE] = {0};
create_label(label_start, LABEL_SIZE, "dofor_s%u", n);
char label_loop[LABEL_SIZE] = {0};
create_label(label_loop, LABEL_SIZE, "dofor_l%u", n);
char label_next[LABEL_SIZE] = {0};
create_label(label_next, LABEL_SIZE, "dofor_n%u", n);
dprintf(fd, "\tconst cx,%s\n", label_start);
dprintf(fd, "\tjmp cx\n"
":%s\n", label_loop);
// ++
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n"
"\tconst bx,1\n"
"\tadd ax,bx\n"
"\tstorew ax,cx\n");
dprintf(fd, ":%s\n", label_start);
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
load_addr(CX, endval);
dprintf(fd, "\tloadw bx,cx\n");
dprintf(fd, "\tconst cx,%s\n", label_next);
dprintf(fd, "\tsless ax,bx\n"
"\tjmpc cx\n");
}
}
}
;
doforis_b: '{' ID '}' '{' expr '}' '{' expr '}' '{' expr '}' {
if ($5 >= ERR_TYP) {
$$ = $5;
} else if ($5 != INT) {
$$ = ERR_TYP;
} else {
if (search_symbol_table($2) != NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
push(new_label_number());
unsigned int n = top();
// INIT LOOP VAR
symbol_table_entry *var = new_symbol_table_entry($2);
var->class = LOCAL_VARIABLE;
var->add = ++curr_fun->nLocalVariables;
var->desc[0] = INT_T;
--offset;
// SAVE END VAL
char endn[16] = {0};
snprintf(endn, sizeof(endn), "%ue", n);
symbol_table_entry *endval = new_symbol_table_entry(endn);
endval->class = LOCAL_VARIABLE;
endval->add = ++curr_fun->nLocalVariables;
endval->desc[0] = INT_T;
--offset;
// SAVE STEP VAL
char stepn[16] = {0};
snprintf(stepn, sizeof(stepn), "%us", n);
symbol_table_entry *stepval = new_symbol_table_entry(stepn);
stepval->class = LOCAL_VARIABLE;
stepval->add = ++curr_fun->nLocalVariables;
stepval->desc[0] = INT_T;
--offset;
char label_start[LABEL_SIZE] = {0};
create_label(label_start, LABEL_SIZE, "dofor_s%u", n);
char label_loop[LABEL_SIZE] = {0};
create_label(label_loop, LABEL_SIZE, "dofor_l%u", n);
char label_next[LABEL_SIZE] = {0};
create_label(label_next, LABEL_SIZE, "dofor_n%u", n);
dprintf(fd, "\tconst cx,%s\n", label_start);
dprintf(fd, "\tjmp cx\n"
":%s\n", label_loop);
// LOOP VAR
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
// STEP VAL
load_addr(BX, stepval);
dprintf(fd, "\tloadw bx,bx\n");
dprintf(fd, "\tadd ax,bx\n");
dprintf(fd, "\tstorew ax,cx\n");
dprintf(fd, ":%s\n", label_start);
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
load_addr(CX, endval);
dprintf(fd, "\tloadw bx,cx\n");
// VERIFIY END CONDITION
dprintf(fd, "\tconst cx,%s\n", label_next);
dprintf(fd, "\tsless bx,ax\n"
"\tjmpc cx\n");
}
}
}
;
doford_b: '{' ID '}' '{' expr '}' '{' expr '}' {
if ($5 >= ERR_TYP) {
$$ = $5;
} else if ($5 != INT) {
$$ = ERR_TYP;
} else {
if (search_symbol_table($2) != NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
push(new_label_number());
unsigned int n = top();
// INIT LOOP VAR
symbol_table_entry *var = new_symbol_table_entry($2);
var->class = LOCAL_VARIABLE;
var->add = ++curr_fun->nLocalVariables;
var->desc[0] = INT_T;
--offset;
// SAVE END VAL
char buf[16] = {0};
snprintf(buf, sizeof(buf), "%u", n);
symbol_table_entry *endval = new_symbol_table_entry(buf);
endval->class = LOCAL_VARIABLE;
endval->add = ++curr_fun->nLocalVariables;
endval->desc[0] = INT_T;
--offset;
char label_start[LABEL_SIZE] = {0};
create_label(label_start, LABEL_SIZE, "dofor_s%u", n);
char label_loop[LABEL_SIZE] = {0};
create_label(label_loop, LABEL_SIZE, "dofor_l%u", n);
char label_next[LABEL_SIZE] = {0};
create_label(label_next, LABEL_SIZE, "dofor_n%u", n);
dprintf(fd, "\tconst cx,%s\n", label_start);
dprintf(fd, "\tjmp cx\n"
":%s\n", label_loop);
// ++
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n"
"\tconst bx,1\n"
"\tsub ax,bx\n"
"\tstorew ax,cx\n");
dprintf(fd, ":%s\n", label_start);
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
load_addr(CX, endval);
dprintf(fd, "\tloadw bx,cx\n");
dprintf(fd, "\tconst cx,%s\n", label_next);
dprintf(fd, "\tsless ax,bx\n"
"\tjmpc cx\n");
}
}
}
;
dofords_b: '{' ID '}' '{' expr '}' '{' expr '}' '{' expr '}' {
if ($5 >= ERR_TYP) {
$$ = $5;
} else if ($5 != INT) {
$$ = ERR_TYP;
} else {
if (search_symbol_table($2) != NULL) {
$$ = ERR_DEC;
} else {
$$ = STATEMENT;
push(new_label_number());
unsigned int n = top();
// INIT LOOP VAR
symbol_table_entry *var = new_symbol_table_entry($2);
var->class = LOCAL_VARIABLE;
var->add = ++curr_fun->nLocalVariables;
var->desc[0] = INT_T;
--offset;
// SAVE END VAL
char endn[16] = {0};
snprintf(endn, sizeof(endn), "%ue", n);
symbol_table_entry *endval = new_symbol_table_entry(endn);
endval->class = LOCAL_VARIABLE;
endval->add = ++curr_fun->nLocalVariables;
endval->desc[0] = INT_T;
--offset;
// SAVE STEP VAL
char stepn[16] = {0};
snprintf(stepn, sizeof(stepn), "%us", n);
symbol_table_entry *stepval = new_symbol_table_entry(stepn);
stepval->class = LOCAL_VARIABLE;
stepval->add = ++curr_fun->nLocalVariables;
stepval->desc[0] = INT_T;
--offset;
char label_start[LABEL_SIZE] = {0};
create_label(label_start, LABEL_SIZE, "dofor_s%u", n);
char label_loop[LABEL_SIZE] = {0};
create_label(label_loop, LABEL_SIZE, "dofor_l%u", n);
char label_next[LABEL_SIZE] = {0};
create_label(label_next, LABEL_SIZE, "dofor_n%u", n);
dprintf(fd, "\tconst cx,%s\n", label_start);
dprintf(fd, "\tjmp cx\n"
":%s\n", label_loop);
// LOOP VAR
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
// STEP VAL
load_addr(BX, stepval);
dprintf(fd, "\tloadw bx,bx\n");
dprintf(fd, "\tsub ax,bx\n");
dprintf(fd, "\tstorew ax,cx\n");
dprintf(fd, ":%s\n", label_start);
load_addr(CX, var);
dprintf(fd, "\tloadw ax,cx\n");
load_addr(CX, endval);
dprintf(fd, "\tloadw bx,cx\n");
// VERIFIY END CONDITION
dprintf(fd, "\tconst cx,%s\n", label_next);
dprintf(fd, "\tsless bx,ax\n"
"\tjmpc cx\n");
}
}
}
;
dofor_e: %empty {
unsigned int n = top();
char label_loop[LABEL_SIZE] = {0};
create_label(label_loop, LABEL_SIZE, "dofor_l%u", n);
char label_next[LABEL_SIZE] = {0};
create_label(label_next, LABEL_SIZE, "dofor_n%u", n);
dprintf(fd, "\tconst ax,%s\n", label_loop);
dprintf(fd, "\tjmp ax\n"
":%s\n", label_next);
}
;
dowhile_t: %empty {
push(new_label_number());
unsigned int n = top();
char label[LABEL_SIZE] = {0};
create_label(label, LABEL_SIZE, "w_test%u", n);
dprintf(fd, ":%s\n", label);
}
;
dowhile_b: %empty {
unsigned int n = top();
char label[LABEL_SIZE] = {0};
create_label(label, LABEL_SIZE, "w_end%u", n);
--offset;
dprintf(fd, "\tpop ax\n"
"\tconst bx,0\n"
"\tconst cx,%s\n", label);
dprintf(fd, "\tcmp ax,bx\n"
"\tjmpc cx\n");
}
;
dowhile_e: %empty {
unsigned int n = top();
char tlabel[LABEL_SIZE] = {0};
create_label(tlabel, LABEL_SIZE, "w_test%u", n);
char elabel[LABEL_SIZE] = {0};
create_label(elabel, LABEL_SIZE, "w_end%u", n);
dprintf(fd, "\tconst ax,%s\n", tlabel);
dprintf(fd, "\tjmp ax\n");
dprintf(fd, ":%s\n", elabel);
}
;
do_b: %empty {
push(new_label_number());
unsigned int n = top();
char label[LABEL_SIZE] = {0};
create_label(label, LABEL_SIZE, "do_b%u", n);
dprintf(fd, ":%s\n", label);
}
;
do_e: %empty {
unsigned int n = top();
char loop[LABEL_SIZE] = {0};
create_label(loop, LABEL_SIZE, "do_b%u", n);
char elabel[LABEL_SIZE] = {0};
create_label(elabel, LABEL_SIZE, "do_e%u", n);
--offset;
dprintf(fd, "\tpop ax\n"
"\tconst bx,0\n"
"\tconst cx,%s\n", elabel);
dprintf(fd, "\tcmp ax,bx\n"
"\tjmpc cx\n");
dprintf(fd, "\tconst ax,%s\n", loop);
dprintf(fd, "\tjmp ax\n");
dprintf(fd, ":%s\n", elabel);
}
;
repeat_b: %empty {
push(new_label_number());
unsigned int n = top();
char label[LABEL_SIZE] = {0};
create_label(label, LABEL_SIZE, "repeat_b%u", n);
dprintf(fd, ":%s\n", label);
}
;
repeat_e: %empty {
unsigned int n = top();
char loop[LABEL_SIZE] = {0};
create_label(loop, LABEL_SIZE, "repeat_b%u", n);
--offset;
dprintf(fd, "\tpop ax\n"
"\tconst bx,0\n"
"\tconst cx,%s\n", loop);
dprintf(fd, "\tcmp ax,bx\n"
"\tjmpc cx\n");
}
;
end: %empty {
pop();
}
;
call_params:
%empty {
$$ = STATEMENT;
}
| param ',' call_params {
status s = 0;
--offset;
if ((s = $1) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = $1;
}
}
| param {
--offset;
$$ = $1;
}
;
param: expr {
$$ = $1;
push(pop() + 1);
}
;
expr :
CALL '{' ID {
symbol_table_entry *ste = search_symbol_table($3);
if (ste == NULL){
$<s>$ = ERR_DEC;
} else if (ste->class != FUNCTION) {
$<s>$ = ERR_TYP;
} else {
push(0);
}
} '}' '{' call_params '}' {
status s = 0;
if ((s = $<s>4) >= ERR_TYP || (s = $7) >= ERR_TYP) {
$$ = s;
} else {
symbol_table_entry *ste = search_symbol_table($3);
size_t n = pop();
if (ste->nParams != n) {
$$ = ERR_SYN;
} else {
$$ = INT;
dprintf(fd, "\tconst ax,%s\n", $3);
dprintf(fd, "\tcall ax\n");
for(size_t i = 0; i < n; ++i) {
dprintf(fd, "\tpop dx\n");
}
dprintf(fd, "\tpush ax\n");
++offset;
}
}
}
| ID {
symbol_table_entry *ste = search_symbol_table($1);
if (ste == NULL) {
$$ = ERR_DEC;
} else {
$$ = INT;
load_addr(CX, ste);
dprintf(fd, "\tloadw ax,cx\n"
"\tpush ax\n");
++offset;
}
}
| NUMBER {
++offset;
dprintf(fd, "\tconst ax,%d\n", $1);
dprintf(fd, "\tpush ax\n");
$$ = INT;
}
| TRUE {
++offset;
dprintf(fd, "\tconst ax,1\n"
"\tpush ax\n");
$$ = INT;
}
| FALSE {
++offset;
dprintf(fd, "\tconst ax,0\n"
"\tpush ax\n");
$$ = INT;
}
| '(' expr ')' {
$$ = $2;
}
| expr '+' expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || ( s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
dprintf(fd, "\tpop ax\n"
"\tpop bx\n"
"\tadd ax,bx\n"
"\tpush ax\n");
}
}
| expr '-' expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || ( s = $3) >= ERR_TYP) {
$$ = s;
} else {
if ($1 == INT && $3 == INT) {
$$ = INT;
dprintf(fd, "\tpop ax\n"
"\tpop bx\n"
"\tsub bx,ax\n"
"\tpush bx\n");
} else {
$$ = ERR_TYP;
}
}
}
| expr '*' expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || ( s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
dprintf(fd, "\tpop ax\n"
"\tpop bx\n"
"\tmul ax,bx\n"
"\tpush ax\n");
}
}
| expr TIMES expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || ( s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
dprintf(fd, "\tpop ax\n"
"\tpop bx\n"
"\tmul ax,bx\n"
"\tpush ax\n");
}
}
| expr '/' expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || ( s = $3) >= ERR_TYP) {
$$ = s;
} else {
if ($3 == 0) {
$$ = ERR_DIV;
} else {
$$ = INT;
}
dprintf(fd, "\tpop bx\n"
"\tpop cx\n"
"\tconst ax,diverr\n"
"\tconst dx,error\n"
"\tdiv cx,bx\n"
"\tjmpe dx\n"
"\tpush cx\n");
}
}
| '-' expr %prec UMINUS {
if ($2 >= ERR_TYP) {
$$ = $2;
} else {
$$ = INT;
dprintf(fd, "\tpop ax\n"
"\tconst bx,-1\n"
"\tmul ax,bx\n"
"\tpush ax\n");
}
}
| expr AND expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
true_from_positive(AX);
true_from_positive(BX);
dprintf(fd, "\tand ax,bx\n"
"\tpush ax\n");
}
}
| expr OR expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
true_from_positive(AX);
true_from_positive(BX);
dprintf(fd, "\tor ax,bx\n"
"\tpush ax\n");
}
}
| expr EQ expr {
--offset;
status s = 0;
if ((s = $1) >= ERR_TYP || (s = $3) >= ERR_TYP) {
$$ = s;
} else {
$$ = INT;
int n = new_label_number();
char tlabel[LABEL_SIZE] = {0};
create_label(tlabel, LABEL_SIZE, "jmptrue%u", n);
char nlabel[LABEL_SIZE] = {0};
create_label(nlabel, LABEL_SIZE, "jmpnext%u", n);
dprintf(fd, "\tconst dx,%s\n", tlabel);
dprintf(fd, "\tconst cx,%s\n", nlabel);
dprintf(fd, "\tpop ax\n"
"\tpop bx\n"
"\tcmp ax,bx\n"
"\tjmpc dx\n"
"\tconst ax,0\n"
"\tpush ax\n"
"\tjmp cx\n");
dprintf(fd, ":%s\n", tlabel);
dprintf(fd, "\tconst ax,1\n"
"\tpush ax\n");
dprintf(fd, ":%s\n", nlabel);
}