-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.y
1151 lines (1013 loc) · 32.1 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
/* ===== Definition Section ===== */
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "symboltable.h"
int tokens = 0, error = 0;
static int linenumber = 1;
int cur_scope = 0;
int is_nt_func = 0;
int tmp_array_dim = 0;
int last_array_num = 0;
int ifnum = 0;
int m = 1;
int param_num = 0;
int isif = 0;
int ifnumbegin = 0;
int allLabel[256];
int temp_place = 8;
int fp_pos = 0;
char pt13lula[665];
enum all_type cur_type, fn_return_type;
struct var_type{
ptr p; //pointer to symbol table entry
int arrayoffset;
};
struct const_type{
int con_type; /*0: Int, 1: Float, -1: String*/
int place;
union {
int ival;
float fval;
char *sc;
} const_u;
};
struct f_l{ //To get details of function arguments.
char name[256];
int func_list_num;
int func_arg_type[256];
enum all_type nt_type;
int place;
int is_lib_func;
int arrayoffset;
};
%}
%union{
struct var_type *variable;
enum all_type nt_type;
int num;
struct const_type *con_pt; /*Constant*/
struct f_l func_list;
}
%token <variable> ID
%token <con_pt> CONST
%token <variable> COMMENT
%token VOID
%token INT
%token FLOAT
%token IF
%token ELSE
%token WHILE
%token FOR
%token STRUCT
%token TYPEDEF
%token OP_ASSIGN
%token OP_OR
%token OP_AND
%token OP_NOT
%token OP_EQ
%token OP_NE
%token OP_GT
%token OP_LT
%token OP_GE
%token OP_LE
%token OP_PLUS
%token OP_MINUS
%token OP_TIMES
%token OP_DIVIDE
%token MK_LB
%token MK_RB
%token MK_LPAREN
%token MK_RPAREN
%token MK_LBRACE
%token MK_RBRACE
%token MK_COMMA
%token MK_SEMICOLON
%token MK_DOT
%token ERROR
%token RETURN
%type <num> dim_fn dimfn1 param
%type <func_list> param_list dim
%type <func_list> relop_expr_list nonempty_relop_expr_list relop_expr relop_term relop_factor
%type <func_list> expr term factor dim_decl cexpr mcexpr cfactor ifbegin whilebegin forbegin
%type <nt_type> type
//%type <nt_type> relop_expr_list nonempty_relop_expr_list relop_expr relop_term relop_factor
//%type <nt_type> expr term factor dim_decl cexpr mcexpr cfactor type
%type <variable> var_ref mul_op add_op rel_op
%type <variable> init_id
%start program
%%
/* ==== Grammar Section ==== */
/* Productions */ /* Semantic actions */
program : global_decl_list
;
global_decl_list: global_decl_list global_decl
|
;
global_decl : decl_list function_decl
| function_decl
;
/*Vineeth: Scoping rule is not correct!*/
function_decl : type ID MK_LPAREN param_list MK_RPAREN MK_LBRACE {param_num = 0;generateFuncio($2->p->id);cur_scope++;
} block {cleanup_symtab(cur_scope);cur_scope--;} MK_RBRACE
{
// printf("func_defn_x: %s, decl: %d, num: %d\n", $2->p->id, $2->p->arg_num, $4.func_list_num);
if(($1 != $2->p->return_type) && ($2->p->is_lib_func == 0) && ($2->p->type == type_func)) { /*Comparing with declaration.*/
printf("Decl: Incompatible return type. Fn = %s\n", $2->p->id);
error = 1;
}
if(($2->p->arg_num != $4.func_list_num) && ($2->p->is_lib_func == 0) && ($2->p->type == type_func)) {
if($2->p->arg_num < $4.func_list_num)
printf("func_defn_x: too many arguments in function (%s)\n", $2->p->id);
else
printf("func_defn_x: too few arguments in function (%s)\n", $2->p->id);
error = 1; /*Set Error Flag.*/
}
if($1 != fn_return_type) { /*Comparing with return type from function body.*/
printf("Block: Incompatible return type. Fn = %s\n", $2->p->id);
// printf("fn_return_type = %d, p->return_type = %d, type = %d.\n", fn_return_type, $2->p->return_type, $1);
error = 1;
}
check_func_details($2, $4);
endFuncio($2->p->id);
}
//Vineeth: Function declarations. The above ones are function definitions
| type ID MK_LPAREN param_list MK_RPAREN MK_SEMICOLON /*Scope = 0 and populating function parameters*/
{
$2->p->return_type = $1;
$2->p->type = type_func;
$2->p->arg_num = $4.func_list_num;
copy_func_details($2, $4);
/*
for(int i = 1; i <= $4.func_list_num; i++) {
$1.func_dim_list[i] = $4.func_arg_type[i];
printf("func_decl: func_dim_list = %d\n", $1.func_dim_list[i]);
}
*/
// printf("func_decl: func_dim_list = %d\n", $2->p->func_dim_list[1]);
// printf("func_decl: func_dim_list = %d\n", $2->p->func_dim_list[2]);
/*
printf("Function decl: %s\t\t", $2->p->id);
printf("Return type: %d\t\t", $2->p->return_type);
printf("Param # = %d\n", $4);
*/
}
;
param_list : param_list MK_COMMA param
{
$$.func_list_num = $1.func_list_num + 1;
$$.func_arg_type[$$.func_list_num] = $3; //Populating dimension for each function argument.
// printf("param_list_1: func_list_num = %d, func_arg_type = %d.\n", $$.func_list_num, $$.func_arg_type[$$.func_list_num]);
}
| param
{
$$.func_list_num = 1;
$$.func_arg_type[$$.func_list_num] = $1; //Populating dimension for each function argument.
// printf("param_list_2: func_list_num = %d, func_arg_type = %d.\n", $$.func_list_num, $$.func_arg_type[$$.func_list_num]);
}
| {$$.func_list_num = 0;} /*To avoid two productions for function decl and defn.*/
;
param : type ID
{
$2->p->scope = 1;
int aux = insert_inRegister($2->p->id);
char buf[20]; sprintf(buf, " move $%d, $a%d\n", aux, param_num++);//emit(buf);
//char* kth23 = "Hello";pt13lula
//char dest[12];
//strcpy( dest, kth23 );
strcat( pt13lula, buf );
$2->p->type = $1;
$2->p->first_time = 0;
// printf("param: scope = 1 for ID %s\n", $2->p->id);
$$ = 0; //Passing 0 as it's not an array, it's a scalar.
// printf("param: scope = 1 for ID %s, array_dim = %d.\n", $2->p->id, $$);
}
| struct_type ID
| type ID dim_fn
{
$2->p->scope = 1;
$2->p->type = $1;
$2->p->first_time = 0;
//printf("param: scope = 1 for ID %s\n", $2->p->id);
$$ = $3; //Passing on array dimensions.
// printf("param: scope = 1 for ID %s, array_dim = %d.\n", $2->p->id, $$);
}
| struct_type ID dim_fn
;
dim_fn :MK_LB expr_null MK_RB dimfn1 {$$ = $4 + 1;}
;
dimfn1 :MK_LB expr MK_RB dimfn1 {$$ = $4 + 1;}
| {$$ = 0;}
;
expr_null :expr
|
;
block : decl_list stmt_list
| stmt_list
| decl_list
|
;
decl_list : decl_list decl
| decl
;
decl : type_decl
| var_decl
;
type_decl : TYPEDEF type id_list MK_SEMICOLON
| TYPEDEF VOID id_list MK_SEMICOLON
| TYPEDEF struct_type id_list MK_SEMICOLON
| struct_type MK_SEMICOLON
;
var_decl : type init_id_list MK_SEMICOLON
/*Struct?*/
| struct_type id_list MK_SEMICOLON
/*Typedef?*/
| ID id_list MK_SEMICOLON
;
/*What types do we support? */
/* Vineeth: Suppported types. */
type : INT {$$ = type_int; cur_type=type_int;} /*{cur_type=type_int;}*/
| FLOAT {$$ = type_float; cur_type=type_float;} /*{cur_type=type_float;}*/
| VOID {$$ = type_void; cur_type=type_void;} /*{cur_type=type_void;}*/
;
struct_type : STRUCT tag
;
/*Vineeth: Struct variable body. */
tag : ID MK_LBRACE {cur_scope++;} decl_list {cleanup_symtab(cur_scope); cur_scope--;} MK_RBRACE
| MK_LBRACE {cur_scope++;} decl_list {cleanup_symtab(cur_scope); cur_scope--;} MK_RBRACE
| ID MK_LBRACE MK_RBRACE
| MK_LBRACE MK_RBRACE
| ID
;
id_list : ID
| id_list MK_COMMA ID
| id_list MK_COMMA ID dim_decl
{
printf("id_list_3: #dims = %d\n", $4); /*Array? When is this used?*/
}
| ID dim_decl
{
printf("id_list_4: #dims = %d\n", $2); /*Array? When is this used?*/
}
;
dim_decl : MK_LB cexpr MK_RB
{
//VM $$ = 1; /*Dimension count.*/
$$.func_list_num = 1; /*Argument count.*/
$$.place = $2.place;
//printf("place:%d", $2.place); //el par
// $$.func_arg_type[$$.func_list_num] = 1; /*Dimension count.*/
//VM if($2 != type_int) { /*Dimension type check.*/
if($2.nt_type != type_int) { /*Dimension type check.*/
printf("dim_decl_1: Array subscript is not an integer.\n");
// printf("dim_decl_1: cexpr = %d\n", $2);
error = 1;
}
}
| dim_decl MK_LB cexpr MK_RB
{
//VM $$ = $1 + 1; /*Dimension count.*/
$$.func_list_num = $1.func_list_num + 1; /*Argument count.*/
//printf("func_listnum:%d\n", $3.place);
$$.place = $1.place * $3.place;
// $$.func_arg_type[$$.func_list_num] = $$.func_arg_type[$$.func_list_num] + 1; /*Dimension count.*/
//VM if($3 != type_int) { /*Dimension type check.*/
if($3.nt_type != type_int) { /*Dimension type check.*/
printf("dim_decl_1: Array subscript is not an integer.\n");
// printf("dim_decl_1: cexpr = %d\n", $3);
error = 1;
}
}
;
cexpr : cexpr add_op mcexpr
| mcexpr {$$.nt_type = $1.nt_type; $$.place = $1.place;}
;
mcexpr : mcexpr mul_op cfactor
| cfactor {$$.nt_type = $1.nt_type; $$.place = $1.place;}
;
cfactor: CONST {$$.nt_type = $1->con_type; $$.place = $1->const_u.ival;}
| MK_LPAREN cexpr MK_RPAREN
;
init_id_list : init_id
| init_id_list MK_COMMA init_id
;
init_id : ID
{
if($1->p->first_time==0) {
printf("ID (%s) redeclared\n",$1->p->id);
error = 1;
}
else {//ID being seen for the first time
fp_pos -= 4;
$1->p->stkPos = fp_pos;
$1->p->first_time=0;
$$ = $1;
// printf("init_id_1: id seen for first time.\n");
}
$1->p->type=cur_type;
/*Variable declaration upgraded?*/
/*
printf("Scope: %d - %s is of type ", cur_scope, $1->p->id);
if($1->p->type==type_int)
printf("int\n");
if($1->p->type==type_float)
printf("float\n");
*/
}
/*Array declared here. Alone??*/
| ID dim_decl
{
if($1->p->first_time==0) {
printf("ID (%s) redeclared\n",$1->p->id);
error = 1;
}
else //ID being seen for the first time
$1->p->first_time=0;
$1->p->type=cur_type;
/*Variable declaration upgraded?*/
/*
printf("Scope: %d - %s is of type ", cur_scope, $1->p->id);
if($1->p->type==type_int)
printf("int\n");
if($1->p->type==type_float)
printf("float\n");
*/
$1->p->is_array = 1;
$1->p->array_dim = $2.func_list_num;
//printf("arraysize:%d\n", $2.place);
insertArray($1->p->id, $2.place);
}
| ID OP_ASSIGN relop_expr
{
//printf("%s ------\n" , $1->p->id);
fp_pos -= 4;
$1->p->stkPos = fp_pos;
if($1->p->scope != 0 && $1->p->is_array == 0){
if($3.place != type_func && !$3.is_lib_func){
char buf[20];
sprintf(buf, " sw $%d, %d($fp)", $3.place, $1->p->stkPos);
emit(buf);
delete_inregister($3.place);
}
}
if($1->p->first_time==0) {
printf("ID (%s) redeclared\n",$1->p->id);
error = 1;
}
else{ //ID being seen for the first time
$1->p->first_time=0;
}
$1->p->type=cur_type;
//printf("%s\n", $1->p->id); //$$ = $1;
/*Variable declaration upgraded?*/
/*
printf("Scope: %d - %s is of type ", cur_scope, $1->p->id);
if($1->p->type==type_int)
printf("int\n");
if($1->p->type==type_float)
printf("float\n");
*/
}
;
ifbegin: IF {isif = 1; char buf[20];sprintf(buf,"test%d:", ifnum);emit(buf);}MK_LPAREN relop_expr
{isif = 0; $$.place = ifnum; ifnum++;}
;
whilebegin: WHILE {$$.place = ifnum; char buf[20];sprintf(buf,"test%d:", ifnum);emit(buf); isif = 1;}
;
forbegin: FOR MK_LPAREN assign_expr_list MK_SEMICOLON {$$.place = ifnum;
char buf[20];sprintf(buf,"test%d:", ifnum);emit(buf); isif = 1;}
;
stmt_list : stmt_list stmt
| stmt
;
stmt : MK_LBRACE {cur_scope++;} block {cleanup_symtab(cur_scope);cur_scope--;} MK_RBRACE
/*Vineeth: Addition for stmt like while(), if() etc.*/
/* | While Statement here */
| whilebegin MK_LPAREN relop_expr_list
{ifnum++; isif = 0;}MK_RPAREN stmt
{char buf[20];sprintf(buf," j test%d", $1.place);emit(buf);}{char buf[20];sprintf(buf,"lexit%d:", $1.place);emit(buf);}
| forbegin relop_expr_list
{isif = 0; ifnum++;
char bufd[20];sprintf(bufd," j body%d", $1.place);emit(bufd);
{char buff[20];sprintf(buff,"inc%d:", $1.place);emit(buff);}
}
MK_SEMICOLON assign_expr_list
{char buf[20];sprintf(buf," j test%d", $1.place);emit(buf);}
MK_RPAREN {char buf[20];sprintf(buf,"body%d:", $1.place);emit(buf);}
stmt
{char buf[20];sprintf(buf," j inc%d", $1.place);emit(buf);
char buff[20];sprintf(buff,"lexit%d:", $1.place);emit(buff);}
/* | If then else here */
| ifbegin MK_RPAREN stmt {char buf[20];sprintf(buf," j lexit%d", $1.place); emit(buf);}ELSE {char buf[20];sprintf(buf,"lelse%d:", $1.place);emit(buf);} stmt
//exit ifelse
{char buf[20];sprintf(buf,"lexit%d:", $1.place);emit(buf);}
/* | If statement here */
| ifbegin MK_RPAREN stmt {
//endif
char buf[20];sprintf(buf,"lelse%d:", $1.place);emit(buf);
}
/* | read and write library calls -- note that read/write are not keywords */
| ID MK_LPAREN relop_expr_list MK_RPAREN /*Function calls: need to check parameter number, type and return types.*/
{
//printf("func_call-stmt: %s, decl: %d, num: %d\n", $1->p->id, $1->p->arg_num, $3.func_list_num);
//printf("func_call-stmt: %s, decl: %s, %d\n", $1->p->id, $3.name, $3.place);
// print_func_details($3);
//Argument # check.
if(($1->p->arg_num != $3.func_list_num) && ($1->p->is_lib_func == 0) && ($1->p->type == type_func)) {
if($1->p->arg_num < $3.func_list_num)
printf("func_call-stmt: too many arguments in function (%s)\n", $1->p->id);
else
printf("func_call-stmt: too few arguments in function (%s)\n", $1->p->id);
error = 1; /*Set Error Flag.*/
}
//Scalar or Pointer?
// ptr p=lookup($3.name, 1);
}
| var_ref OP_ASSIGN relop_expr MK_SEMICOLON
{ /*Function return type comparison goes here?*/
if($1->p->type == type_int && $3.place < 32){ // integer miguel
if($1->p->scope != 0 && $1->p->is_array == 0){
if($3.is_lib_func){
char buf[20]; sprintf(buf, " li $v0, 5");emit(buf);
char buff[20]; sprintf(buff, " syscall");emit(buff);
char bufff[20]; sprintf(bufff, " sw $v0, %d($fp)", $1->p->stkPos);emit(bufff);
}else if ($3.place == type_func){
char buf[20]; sprintf(buf, " move $%d, $v0", insert_inRegister($1->p->id));emit(buf);
}else{
char buf[20]; sprintf(buf, " sw $%d, %d($fp)", $3.place, $1->p->stkPos);emit(buf);
delete_inregister($3.place);
}
}else if($1->p->is_array == 1){
char buff[20];
sprintf(buff, "%d", $1->arrayoffset);
char buf[20]; sprintf(buf, " sw $%d, _%s($%d)", $3.place, $1->p->id, find_inRegister(buff));
emit(buf);
delete_inregister($3.place);
delete_inregister(temp_place);
}else{
char buf[20]; sprintf(buf, " sw $%d, _%s", $3.place, $1->p->id);emit(buf);
delete_inregister($3.place);
}
}else{ //float
if($1->p->scope != 0){
char buf[20]; sprintf(buf, " s.s $f%d, %d($fp)", $3.place - 32, $1->p->stkPos);emit(buf);
delete_inregister($3.place);
}else{
char buf[20]; sprintf(buf, " s.s $f%d, _%s", $3.place, $1->p->id);emit(buf);
delete_inregister($3.place);
}
}
// printf("stmt: ID = %s, type = %d, return_type = %d\n", $1->p->id, $1->p->type, $3);
if($1->p->type != $3.nt_type && (is_nt_func == 1)) {
printf("ID = %s : Incompatible return types\n", $1->p->id);
error = 1; /*Set Error Flag.*/
}
is_nt_func = 0; /*Reset flag so that non functions are not checked for return type*/
/*Need to handle read (int) and fread (float).*/
}
| relop_expr_list MK_SEMICOLON
| MK_SEMICOLON
| RETURN MK_SEMICOLON /*Cross check return type.*/
{
fn_return_type = type_void;
}
| RETURN relop_expr MK_SEMICOLON /*Cross check return type.*/
{
fn_return_type = $2.nt_type;
char buf[20]; sprintf(buf, " move $v0, $%d", $2.place);emit(buf);
}
;
assign_expr_list : nonempty_assign_expr_list
|
;
nonempty_assign_expr_list : nonempty_assign_expr_list MK_COMMA assign_expr
| assign_expr
assign_expr : ID OP_ASSIGN relop_expr
{
//char bufff[20]; sprintf(bufff, " sw $v0, %d($fp)", $1->p->stkPos);emit(bufff);
char buf[20];
sprintf(buf, " sw $%d, %d($fp)", $3.place, $1->p->stkPos);
emit(buf);
delete_inregister($3.place);
}
| relop_expr
;
relop_expr : relop_term
{
$$ = $1;strcpy($$.name, $1.name);;
// printf("relop_expr: array arg # = %d.\n", $$.func_list_num);
//printf("%s\n" , $1.name);
}
| relop_expr OP_OR relop_term {$$ = $1;strcpy($$.name, $1.name);
}
;
relop_term : relop_factor {
$$ = $1;strcpy($$.name, $1.name);
if(isif){
char buff[20];sprintf(buff," beqz $%d, lelse%d", $1.place, ifnum);emit(buff);
}
}//
| relop_term OP_AND relop_factor {
$$ = $3;strcpy($$.name, $3.name);
if(isif){
char buff[20];sprintf(buff," beqz $%d, lelse%d", $3.place, ifnum);emit(buff);
}
}//printf("%d:" , $1.place);printf("%d\n" , $3.place);
;
relop_factor : expr {$$ = $1;strcpy($$.name, $1.name);}//printf("%s\n" , $1.name);
| expr rel_op expr {int auxplace;int buf [20]; int bugg[20];
sprintf(bugg, "%s,%d,%d", $2,$1.place, $3.place);
auxplace = insert_inRegister(bugg);
sprintf(buf," %s $%d, $%d, $%d", $2,auxplace, $1.place, $3.place);
emit(buf);$$.place = auxplace;
if(isif){
//char buff[20];sprintf(buff," beqz $%d, lelse%d", auxplace, ifnum);emit(buff);
}
strcpy($$.name, $1.name);
}
;
/* what relation operators do we support ? */
/*Vineeth: Relational operators added.*/
rel_op : OP_LT {$$ = "slt";}
| OP_LE {$$ = "sle";}
| OP_GT {$$ = "sgt";}
| OP_GE {$$ = "sge";}
| OP_EQ {$$ = "seq";}
| OP_NE {$$ = "sne";}
;
relop_expr_list : nonempty_relop_expr_list {$$ = $1;}
| {$$.func_list_num = 0;} /*For function parameter number?*/ //PROBLEM???
;
nonempty_relop_expr_list : nonempty_relop_expr_list MK_COMMA relop_expr
{
$$.func_list_num = $1.func_list_num + 1;
$$.func_arg_type[$$.func_list_num] = $3.func_list_num;
strcpy($$.name, $3.name);
//printf("aqui:%s\n", $3.name);
if(find_inRegister($3.name) && $1.func_list_num <=4){
char buf[20];
sprintf(buf, " move $a%d, $%d", $1.func_list_num,insert_inRegister($3.name));
emit(buf);
}else{
char buf[20];
sprintf(buf, " move $a%d, $%d", $1.func_list_num, temp_place);
emit(buf);
}
//printf("nonempty_relop_expr_list_1: name = %s, arg # = %d, array dim# = %d & %d.\n", $$.name, $$.func_list_num, $$.func_arg_type[1], $$.func_arg_type[2]);
//Scalar or Pointer?
/*
ptr p=lookup($3.name, 1);
if(p->array_dim == 0) {
if(p->array_dim != $3.func_list_num) {
printf("nonempty_relop_expr_list_1: Scalar Issue.\n");
error = 1;
}
}
*/
}
| relop_expr
{ /*For function parameter number?*/ //PROBLEM???
if(find_inRegister($1.name)){
char buf[20];
sprintf(buf, " move $a0, $%d", insert_inRegister($1.name));
emit(buf);
}
$$.func_list_num = 1;
$$.func_arg_type[$$.func_list_num] = $1.func_list_num;
strcpy($$.name, $1.name);
// printf("nonempty_relop_expr_list_2: name = %s, arg # = %d, array dim# = %d & %d.\n", $$.name, $$.func_list_num, $$.func_arg_type[1], $$.func_arg_type[2]);
}
;
expr : expr add_op term {temp_place = insert_inRegister($1.name);
char buf[20];
if(temp_place < 32){
sprintf(buf, " %s $%d, $%d, $%d ", $2, temp_place, $1.place, $3.place);
}else{
if($2 == "add"){
sprintf(buf, " add.s $f%d, $f%d, $%d ", temp_place-32, $1.place-32, $3.place);
}else{
sprintf(buf, " sub.s $f%d, $f%d, $%d ", temp_place-32, $1.place-32, $3.place);
}
}
emit(buf);
$$ = $1;
$$.place = temp_place; }
| term
{
$$ = $1;
//printf("expr:%s\n", $1.name);
} /*For function return type?*/
;
add_op : OP_PLUS {$$ = "add";}
| OP_MINUS {$$ = "sub";}
;
term : term mul_op factor {temp_place = insert_inRegister($1.name);
char buf[20];
if(temp_place < 32){
sprintf(buf, " %s $%d, $%d, $%d", $2, temp_place, $1.place, $3.place);
emit(buf);
}else{
if($2 == "mul"){
sprintf(buf, " mul.s $f%d, $f%d, $%d", temp_place - 32, $1.place - 32, $3.place);
emit(buf);
}else{
sprintf(buf, " div.s $f%d, $f%d, $%d", temp_place - 32, $1.place -32, $3.place);
emit(buf);
}
}
strcpy($$.name, $1.name);
$$.place = temp_place;
} //
| factor
{$$ = $1;
strcpy($$.name, $1.name);
}
;
mul_op : OP_TIMES {$$ = "mul";}
| OP_DIVIDE {$$ = "div";}
;
factor : MK_LPAREN relop_expr MK_RPAREN /*How to check Array subscript?*/
/* | -(<relop_expr>) */
| OP_NOT MK_LPAREN relop_expr MK_RPAREN /*How to check Array subscript?*/
/*Vineeth: OP_MINUS condition added as C could have a condition like:
"if(-(i < 10))". */
| OP_MINUS MK_LPAREN relop_expr MK_RPAREN /*How to check Array subscript?*/
| CONST {$$.nt_type = $1->con_type; $$.arrayoffset = $1->const_u.ival;
char na[256];
sprintf(na, "%d", $1->const_u.ival);
if(!find_inRegister(na)){
if($1->con_type != -1){
if($1->const_u.ival){
$1->place = insert_inRegister(na);
char buf[20];
sprintf(buf, " li $%d, %d", $1->place, $1->const_u.ival);
emit(buf);
}else{
$1->place = 0;
}
}else{
$1->place = insert_inRegister(na);
char buff[20];
sprintf(buff, " li $v0, 4");
emit(buff);
char buf[20];
strcpy(strings[m], $1->const_u.sc);
sprintf(buf, " la $a0, m%d", m++);
emit(buf);
char bufff[20];
sprintf(bufff, " syscall");
emit(bufff);
}
}else{
$1->place = insert_inRegister(na);
}
//printf("id:%d place:%d\n ", $1->const_u.ival,$1->place);
$$.place = $1->place;} /*Checking Array subscript.*/
//{printf("ID: %d\n", $1->const_u.ival);}
/* | - constant, here - is an Unary operator */
| OP_NOT CONST {$$.nt_type = $2->con_type;} /*Checking Array subscript.*/
//Vineeth: OP_MINUS condition added as C could have a condition like: "if(-10)".
| OP_MINUS CONST {$$.nt_type = $2->con_type;} /*Checking Array subscript.*/
| ID MK_LPAREN relop_expr_list MK_RPAREN
/* | - func ( <relop_expr_list> ) */
{
printf("func_call-factor: %s, decl: %d, num: %d\n", $1->p->id, $1->p->arg_num, $3.func_list_num);
print_func_details($3);
if(($1->p->arg_num != $3.func_list_num) && ($1->p->is_lib_func == 0) && ($1->p->type == type_func)) {
if($1->p->arg_num < $3.func_list_num)
printf("func_call-factor: too many arguments in function (%s)\n", $1->p->id);
else
printf("func_call-factor: too few arguments in function (%s)\n", $1->p->id);
error = 1; /*Set Error Flag.*/
}
// printf("factor: ID type - %d\n", $1->p->type);
if($1->p->type == type_func) {
is_nt_func = 1; /*Set flint vector[3];ag so that only functions are checked for return type.*/
if(!strcmp($1->p->id, "read")) {
$1->p->return_type = type_int;
// printf("read() function\n");
}
if(!strcmp($1->p->id, "fread")) {
$1->p->return_type = type_float;
// printf("fread() function\n");
}
}
$$.nt_type = $1->p->return_type; /*Function return type passed to factor.*/
if(!strcmp($1->p->id,"read") || !strcmp($1->p->id,"write")){
$$.is_lib_func = 1;
}else{
$$.place = type_func;
char buf[20]; sprintf(buf," jal %s", $1->p->id);
emit(buf);
}
printf("factor: return type of (%s) is: %d\n", $1->p->id, $$.nt_type);
}
| OP_NOT ID MK_LPAREN relop_expr_list MK_RPAREN
//Vineeth: OP_MINUS condition added as C could have a condition like: "if(-read(i))".
| OP_MINUS ID MK_LPAREN relop_expr_list MK_RPAREN
| var_ref
/* | - var-reference */
{
int prev_temp = temp_place;
temp_place = find_inRegister($1->p->id);
//printf("%s: %d\n", $1->p->id, $1->p->is_array);
if($1->p->scope == 0 && !temp_place){
char buf[20];
temp_place = insert_inRegister($1->p->id);
if(temp_place < 32){
sprintf(buf, " lw $%d, _%s", temp_place, $1->p->id);
emit(buf);
}else{
sprintf(buf, " l.s $f%d, _%s", temp_place-32, $1->p->id);
emit(buf);
}
}else if (!temp_place){
char buf[20];
if($1->p->is_array){
//printf("%d", last_array_num);
char bufff[20];
sprintf(bufff, "%d", $1->arrayoffset);
char buff[20];
sprintf(buff, "%s%d", $1->p->id, $1->arrayoffset);
printf("arrayoffset:%d\n", $1->arrayoffset);
temp_place = insert_inRegister(buff);
if(temp_place < 32){
sprintf(buf, " lw $%d, _%s($%d)", temp_place, $1->p->id, find_inRegister(bufff));
emit(buf);
delete_inregister(prev_temp);
}else{
sprintf(buf, " l.s $f%d, _%s($%d)", temp_place-32, $1->p->id, find_inRegister(bufff));
emit(buf);
delete_inregister(prev_temp);
}
}else{
temp_place = insert_inRegister($1->p->id);
if(temp_place < 32){
sprintf(buf, " lw $%d, %d($fp)", temp_place, $1->p->stkPos);
emit(buf);
}else{
sprintf(buf, " l.s $f%d, %d($fp)", temp_place-32, $1->p->stkPos);
emit(buf);
}
}
}
$$.place = temp_place;
$$.nt_type = $1->p->type; /*Type to be used in array subscript.*/
$$.func_list_num = tmp_array_dim; //# of Dimensions.
strcpy($$.name, $1->p->id);
// printf("factor-var_ref: variable is = %s, tmp_array_dim = %d.\n", $1->p->id, tmp_array_dim);
if($1->p->is_array == 1) { /*It's array.*/
if($1->p->array_dim != tmp_array_dim) {
printf("Incompatible array dimensions.%d:%d\n", $1->p->array_dim, tmp_array_dim);
error = 1;
}
}
tmp_array_dim = 0; /*Reset global variable*/
}
| OP_NOT var_ref {$$.nt_type = $2->p->type;} /*Type to be used in array subscript.*/
//Vineeth: OP_MINUS condition added as C could have a condition like: "if(-a)".
| OP_MINUS var_ref {$$.nt_type = $2->p->type;} /*Type to be used in array subscript.*/
;
var_ref : ID
{
$$ = $1;
// printf("var_ref: ID: %s\n", $1->p->id);
if($1->p->type == type_undef)
printf("ID (%s) undeclared.\n", $1->p->id);
}
| var_ref dim /*Array.*/
{
// printf("var_ref: variable is = %s\n", $1->p->id);
//printf("dim_decl_1: expr = %d\n", $2.arrayoffset);
$$->arrayoffset = $2.arrayoffset;
}
| var_ref struct_tail
;
dim : MK_LB expr MK_RB
{
tmp_array_dim++;
//
$$ = $2;
//printf("array:%d dim:%d \n", $2.arrayoffset, tmp_array_dim);
if($2.nt_type != type_int) { /*Array dimension check in stmt.*/
printf("dim: Array subscript is not an integer.\n");
error = 1;
}
}
;
struct_tail : MK_DOT ID
;
%%
#include "lex.yy.c"
FILE *f;
void generateFuncio(char *func)
{
int x = 0;
emit("\n.text");
char buf[2000];
char laBuferizacionSeletiva[999];
for(x=0; x < strlen(pt13lula) ; x++)
{
if(strlen(pt13lula)-1 == x) break;
laBuferizacionSeletiva[x] = pt13lula[x];
}
sprintf(buf, "%s:\n%s\n%s\n%s\n%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n_begin_%s:\n%s",func,
" sw $ra, 0($sp)",
" sw $fp, -4($sp)",
" add $fp, $sp, -4",
" add $sp, $sp, -8",
" lw $2, _framesize_", func,
" sub $sp, $sp, $2",
" sw $8, 32($sp)",
" sw $9, 28($sp)",
" sw $10, 24($sp)",
" sw $11, 20($sp)",
" sw $12, 16($sp)",
" sw $13, 12($sp)",
" sw $14, 8($sp)",
" sw $15, 4($sp)",
func,
laBuferizacionSeletiva
);
for(x=0; x< sizeof(pt13lula); x++ )
{
pt13lula[x]=0;
}
for(x=0; x< sizeof(laBuferizacionSeletiva); x++ )
{
laBuferizacionSeletiva[x]=0;
}
emit(buf);
}
void endFuncio(char * func)
{
char buf[2000];
sprintf(buf, "\n\n_end_%s:\n%s%s%s%s%s%s%s%s%s%s%s",
func,
" lw $8, 32($sp)\n",
" lw $9, 28($sp)\n",
" lw $10, 24($sp)\n",
" lw $11, 20($sp)\n",
" lw $12, 16($sp)\n",
" lw $13, 12($sp)\n",
" lw $14, 8($sp)\n",
" lw $15, 4($sp)\n",
" lw $ra, 4($fp)\n",
" add $sp, $fp, 4\n",
" lw $fp, 0($fp)"
);
emit(buf);
if(strcmp(func, "main")){
emit(" jr $ra");
}else{
emit(" li $v0, 10");
emit(" syscall");
}
emit("\n.data");
char buff[20];
sprintf(buff,"_framesize_%s: .word 36", func);
emit(buff);
}
int main (int argc, char *argv[])
{
init_symtab();
f = fopen("file.spim", "w");
printf("Symbol Table initialized\n");
if(argc>0)
yyin = fopen(argv[1],"r");
else
yyin=stdin;
registr aux = arrays;