-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilateur.cpp
More file actions
983 lines (919 loc) · 31 KB
/
Copy pathcompilateur.cpp
File metadata and controls
983 lines (919 loc) · 31 KB
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
// A compiler from a very simple Pascal-like structured language LL(k)
// to 64-bit 80x86 Assembly langage
// Copyright (C) 2019 Pierre Jourlin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Build with "make compilateur"
#include <string>
#include <iostream>
#include <cstdlib>
#include <set>
#include <map>
#include <FlexLexer.h>
#include "tokeniser.h"
#include <cstring>
#include <string>
using namespace std;
enum KEYWORDS {IF, THEN, ELSE, WHILE, FOR, DO, TO, BEGIN, END, VAR, DISPLAY, DOWNTO,TRUE, FALSE, NONE, CASE, OF, DEFAULT};
enum OPREL {EQU, DIFF, INF, SUP, INFE, SUPE, WTFR};
enum OPADD {ADD, SUB, OR, WTFA};
enum OPMUL {MUL, DIV, MOD, AND ,WTFM};
enum TYPES {INTEGER, BOOLEAN, DOUBLE, CHAR, STRING};
TOKEN current; // Current token
FlexLexer* lexer = new yyFlexLexer; // This is the flex tokeniser
// tokens can be read using lexer->yylex()
// lexer->yylex() returns the type of the lexicon entry (see enum TOKEN in tkeniser.h)
// and lexer->YYText() returns the lexicon entry as a string
map<string, enum TYPES> DeclaredVariables; // Set of declared variables and their types
unsigned long TagNumber=0;
bool IsDeclared(const char *id){
return DeclaredVariables.find(id)!=DeclaredVariables.end();
}
void Error(string s){
cerr << "Ligne n°"<<lexer->lineno()<<", lu : '"<<lexer->YYText()<<"'("<<current<<"), mais "; // lineno() retourne le numéro de la ligne actuelle
cerr<< s << endl;
exit(-1);
}
// Program := [DeclarationPart] StatementPart
// DeclarationPart := "[" Letter {"," Letter} "]"
// VarDeclarationPart := "VAR" VarDeclaration {";" VarDeclaration} "."
// VarDeclaration := Ident {"," Ident} ":" Type
// StatementPart := Statement {";" Statement} "."
// Statement := AssignementStatement | IfStatement | WhileStatement | ForStatement | BlockStatement | DisplayStatement | NotStatement | CaseStatement
// AssignementStatement := Letter "=" Expression
// IfStatement := "IF" Expression "THEN" Statement [ "ELSE" Statement ]
// WhileStatement := "WHILE" Expression "DO" Statement
// ForStatement := "FOR" AssignementStatement "To" Expression "DO" Statement
// BlockStatement := "BEGIN" Statement { ";" Statement } "END"
// CaseStatement ::= "CASE" Expression "OF" CaseListElement {; CaseListElement } "END"
// CaseListElement ::= CaseLabelList ":" Statement | Empty
// CaseLabelList ::= Factor {, Factor }
// DisplayStatement := "DISPLAY" Expression
// NotStatement := "NOT" Factor
// Expression := SimpleExpression [RelationalOperator SimpleExpression]
// SimpleExpression := Term {AdditiveOperator Term}
// Term := Factor {MultiplicativeOperator Factor}
// Factor := Number | Letter | "(" Expression ")"| "!" Factor
// Number := Digit{Digit}
// AdditiveOperator := "+" | "-" | "||"
// MultiplicativeOperator := "*" | "/" | "%" | "&&"
// RelationalOperator := "==" | "!=" | "<" | ">" | "<=" | ">="
// Digit := "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
// Letter := "a"|...|"z"
enum TYPES Expression(void);
void Statement(void);
void StatementPart(void);
KEYWORDS getKeyword(void);
TYPES Identifier(void){
enum TYPES type;
if(!IsDeclared(lexer->YYText())){
cerr << "Erreur : Variable '"<<lexer->YYText()<<"' non déclarée"<<endl;
exit(-1);
}
type=DeclaredVariables[lexer->YYText()];
cout << "\tpush "<<lexer->YYText()<<endl;
current=(TOKEN) lexer->yylex();
return type;
}
TYPES Number(void){
double floatNumber;
unsigned int *p;
string nbr = lexer->YYText();
if (current!=NUMBER)
Error("Chiffre attendu");
if (nbr.find(".") != string::npos){
floatNumber = atof(lexer->YYText());
p = (unsigned int *) &floatNumber;
cout << "\tsubq $8, %rsp"<<endl;
cout << "\tmovl $"<<*p<<", (%rsp) \t# Conversion of "<<floatNumber<<" (32 bit high part)"<<endl;
cout << "\tmovl $"<<*(p+1)<<", 4(%rsp) \t# Conversion of "<<floatNumber<<" (32 bit low part)"<<endl;
current=(TOKEN) lexer->yylex();
return DOUBLE;
} else {
cout <<"\tpush $"<<atoi(lexer->YYText())<<endl; // YYText =/ readCHAR car on accede a la valeur du token sans avancer le curseur
current=(TOKEN) lexer->yylex(); // YYlex envoie le type du token actuel et avance le curseur
return INTEGER;
}
}
TYPES Factor(void){
enum TYPES type;
if(current==RPARENT){
current=(TOKEN) lexer->yylex();
type = Expression();
if(current!=LPARENT)
Error("')' était attendu");
else
current=(TOKEN) lexer->yylex();
} else if (current==NUMBER){
type = Number();
} else if(current==ID) {
type = Identifier();
} else if (current==CHARCONST){
cout << "\tpush $"<<lexer->YYText()<<endl;
current=(TOKEN) lexer->yylex();
type = CHAR;
}else if (getKeyword()==TRUE){
cout << "\tpush $0xFFFFFFFFFFFFFFFF"<<endl;
current=(TOKEN) lexer->yylex();
type = BOOLEAN;
}else if (getKeyword()==FALSE){
cout << "\tpush $0"<<endl;
current=(TOKEN) lexer->yylex();
type = BOOLEAN;
} else if (current==NOT){
current=(TOKEN) lexer->yylex();
type = Factor();
if (type!=BOOLEAN)
Error("Booléen attendu pour la négation");
cout << "\tpop %rax"<<endl;
cout << "\txorq $0xFFFFFFFFFFFFFFFF, %rax"<<endl;
cout << "\tpush %rax"<<endl;
} else if (current==STRINGCONST){
string str = lexer->YYText();
int length = str.length() + 1;
cout << "\tmov $" << length << ", %rdi" << endl;
cout << "\tcall malloc" << endl;
cout << "\tmov %rax, %rbx" << endl;
for (int i = 0; i < length; ++i) {
int char_value = static_cast<int>(str[i]);
cout << "\tmovb $" << char_value << ", %al" << endl;
cout << "\tmovb %al, " << i << "(%rbx)" << endl;
}
cout << "\tmovb $0, %al" << endl;
cout << "\tmovb %al, " << length - 1 << "(%rbx)" << endl;
cout << "\tpush %rbx" << endl;
current = (TOKEN) lexer->yylex();
type = STRING;
} else {
Error("Chiffre, lettre ou '(' attendu");
}
return type;
}
// MultiplicativeOperator := "*" | "/" | "%" | "&&"
OPMUL MultiplicativeOperator(void){
OPMUL opmul;
if(strcmp(lexer->YYText(),"*")==0)
opmul=MUL;
else if(strcmp(lexer->YYText(),"/")==0)
opmul=DIV;
else if(strcmp(lexer->YYText(),"%")==0)
opmul=MOD;
else if(strcmp(lexer->YYText(),"&&")==0)
opmul=AND;
else opmul=WTFM;
current=(TOKEN) lexer->yylex();
return opmul;
}
// Term := Factor {MultiplicativeOperator Factor}
TYPES Term(void){
TYPES typeA, typeB;
OPMUL mulop;
typeA = Factor();
while(current==MULOP){
mulop=MultiplicativeOperator();
typeB = Factor();
if (typeA!=typeB && !((typeA==DOUBLE && typeB==INTEGER) || (typeA==INTEGER && typeB==DOUBLE)))
Error("Memes types ou double et entier attendus");
if (typeA==DOUBLE) {
if (typeB==INTEGER) {
cout << "\tfildl (%rsp)\t" << endl;
} else { // DOUBLE
cout<<"\tfldl (%rsp)\t"<<endl;
}
cout<<"\tfldl 8(%rsp)"<<endl;
} else if (typeA==INTEGER || typeA==BOOLEAN) {
if (typeB==DOUBLE) {
cout << "\tfldl (%rsp)\t" << endl;
cout << "\tfildl 8(%rsp)\t" << endl;
} else {
cout << "\tpop %rbx"<<endl;
cout << "\tpop %rax"<<endl;
}
}
switch(mulop){
case AND:
if (typeA!=BOOLEAN)
Error("Booléens attendus pour AND (&&) ");
cout << "\tandq %rbx, %rax\t# AND"<<endl;
cout << "\tpush %rax"<<endl;
break;
case MUL:
if (typeA == DOUBLE && typeB == INTEGER) {
cout << "\tfmulp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA == INTEGER && typeB == DOUBLE) {
cout << "\tfmulp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA==INTEGER) {
cout << "\tmulq %rbx"<<endl; // a * b -> %rdx:%rax
cout << "\tpush %rax\t# MUL"<<endl; // store result
} else if (typeA == DOUBLE) {
cout<<"\tfmulp %st(0),%st(1)"<<endl;
cout<<"\tfstpl 8(%rsp)"<<endl;
cout<<"\taddq $8, %rsp"<<endl;
} else {
Error("Entiers ou flottant attendus pour la multiplication");
}
break;
case DIV:
if (typeA == DOUBLE && typeB == INTEGER) {
cout << "\tfdivp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA == INTEGER && typeB == DOUBLE) {
cout << "\tfdivp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA==INTEGER) {
cout << "\tmovq $0, %rdx"<<endl;
cout << "\tdiv %rbx"<<endl;
cout << "\tpush %rax\t# DIV"<<endl;
} else if (typeA == DOUBLE) {
cout<<"\tfdivp %st(0),%st(1)"<<endl;
cout<<"\tfstpl 8(%rsp)"<<endl;
cout<<"\taddq $8, %rsp"<<endl;
} else {
Error("Entiers ou flottant attendus pour la division");
}
break;
case MOD:
if (typeA==INTEGER) {
cout << "\tmovq $0, %rdx"<<endl;
cout << "\tdiv %rbx"<<endl;
cout << "\tpush %rdx\t# MOD"<<endl;
} else {
Error("Entiers attendus pour le modulo");
}
break;
default:
Error("opérateur multiplicatif attendu");
}
}
return typeA;
}
// AdditiveOperator := "+" | "-" | "||"
OPADD AdditiveOperator(void){
OPADD opadd;
if(strcmp(lexer->YYText(),"+")==0)
opadd=ADD;
else if(strcmp(lexer->YYText(),"-")==0)
opadd=SUB;
else if(strcmp(lexer->YYText(),"||")==0)
opadd=OR;
else opadd=WTFA;
current=(TOKEN) lexer->yylex();
return opadd;
}
// Expression:= Term {AdditiveOperator Term}
TYPES SimpleExpression(void){
enum TYPES typeA, typeB;
OPADD adop;
typeA = Term();
while(current==ADDOP){
adop=AdditiveOperator();
typeB = Term();
if (typeA!=typeB && !((typeA==DOUBLE && typeB==INTEGER) || (typeA==INTEGER && typeB==DOUBLE)))
Error("assignation de meme type ou double et entier attendu");
if (typeA!=INTEGER && typeA!=BOOLEAN && typeA!=DOUBLE)
Error("Entier, booléen ou double attendu");
if (typeA==DOUBLE) {
if (typeB==INTEGER) {
cout << "\tfildl (%rsp)\t" << endl;
} else { // DOUBLE
cout<<"\tfldl (%rsp)\t"<<endl;
}
cout<<"\tfldl 8(%rsp)"<<endl;
} else if (typeA==INTEGER || typeA==BOOLEAN) { // BOOLEAN et DOUBLE impossible car déjà traité
if (typeB==DOUBLE) {
cout << "\tfldl (%rsp)\t" << endl;
cout << "\tfildl 8(%rsp)\t" << endl;
} else {
cout << "\tpopq %rbx"<<endl;
cout << "\tpopq %rax"<<endl;
}
} else {
Error("Entier, booléen ou double attendu");
}
switch(adop){
case OR:
if (typeA!=BOOLEAN)
Error("Booléens attendus pour OR (||) ");
cout << "\taddq %rbx, %rax\t# OR"<<endl;
cout << "\tpush %rax"<<endl;
break;
case ADD:
if (typeA == DOUBLE && typeB == INTEGER) {
cout << "\tfaddp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA == INTEGER && typeB == DOUBLE) {
cout << "\tfaddp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA==INTEGER) {
cout << "\taddq %rbx, %rax\t# ADD"<<endl;
cout << "\tpush %rax"<<endl;
} else if (typeA == DOUBLE) {
cout<<"\tfaddp %st(0),%st(1)"<<endl;
cout<<"\tfstpl 8(%rsp)"<<endl;
cout<<"\taddq $8, %rsp"<<endl;
} else {
Error("Entiers ou doubles attendus");
}
break;
case SUB:
if (typeA!=INTEGER && typeA!=DOUBLE)
Error("Entiers attendus pour la soustraction");
if (typeA == DOUBLE && typeB == INTEGER) {
cout << "\tfsubp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA == INTEGER && typeB == DOUBLE) {
cout << "\tfsubp %st(0),%st(1)"<<endl;
cout << "\tfstpl 8(%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else if (typeA == DOUBLE) {
cout<<"\tfsubp \t%st(0),%st(1)" << endl;
cout<<"\tfstpl 8(%rsp)"<<endl;
cout<<"\taddq $8, %rsp"<<endl;
} else if (typeA==INTEGER) {
cout << "\tsubq %rbx, %rax"<<endl;
cout << "\tpushq %rax"<<endl;
}
break;
default:
Error("opérateur additif inconnu");
}
}
return typeA;
}
TYPES Type(void){
if (current!=KEYWORD){
Error("Type attendu");
}
if(strcmp(lexer->YYText(),"INTEGER")==0){
current=(TOKEN) lexer->yylex();
return INTEGER;
}
else if(strcmp(lexer->YYText(),"BOOLEAN")==0) {
current=(TOKEN) lexer->yylex();
return BOOLEAN;
} else if (strcmp(lexer->YYText(),"DOUBLE")==0) {
current=(TOKEN) lexer->yylex();
return DOUBLE;
} else if (strcmp(lexer->YYText(),"CHAR")==0) {
current=(TOKEN) lexer->yylex();
return CHAR;
} else if (strcmp(lexer->YYText(),"STRING")==0) {
current=(TOKEN) lexer->yylex();
return STRING;
}
Error("Type attendu");
return INTEGER;
}
// VarDeclaration := Ident {"," Ident} ":" Type
void VarDeclaration(void){
set <string> idents;
enum TYPES type;
if(current!=ID)
Error("Identificateur attendu");
idents.insert(lexer->YYText());
current=(TOKEN) lexer->yylex();
while(current==COMMA){
current=(TOKEN) lexer->yylex();
if(current!=ID)
Error("Identificateur attendu");
if(idents.find(lexer->YYText())!=idents.end())
Error("Identificateur déjà déclaré");
idents.insert(lexer->YYText());
current=(TOKEN) lexer->yylex();
}
if (current!=COLON)
Error("caractère ':' attendu");
current=(TOKEN) lexer->yylex();
type=Type();
for(set<string>::iterator it=idents.begin(); it!=idents.end(); it++){
if(DeclaredVariables.find(*it)!=DeclaredVariables.end())
Error("Identificateur déjà déclaré");
switch(type){
case BOOLEAN:
case INTEGER:
cout << *it << ":\t.quad 0"<<endl;
break;
case DOUBLE:
cout << *it << ":\t.double 0.0"<<endl;
break;
case CHAR:
cout << *it << ":\t.byte 0"<<endl;
break;
case STRING:
cout << *it << ":\t.string \"\""<<endl;
break;
default:
Error("Type inconnu");
}
DeclaredVariables[*it]=type;
}
}
// VarDeclarationPart := "VAR" VarDeclaration {";" VarDeclaration} "."
void VarDeclarationPart(void) {
current=(TOKEN) lexer->yylex();
VarDeclaration();
while(current==SEMICOLON){
current=(TOKEN) lexer->yylex();
VarDeclaration();
}
if (current!=DOT)
Error("caractère '.' attendu pour finir la déclaration des variables");
current=(TOKEN) lexer->yylex();
}
// RelationalOperator := "==" | "!=" | "<" | ">" | "<=" | ">="
OPREL RelationalOperator(void){
OPREL oprel;
if(strcmp(lexer->YYText(),"==")==0)
oprel=EQU;
else if(strcmp(lexer->YYText(),"!=")==0)
oprel=DIFF;
else if(strcmp(lexer->YYText(),"<")==0)
oprel=INF;
else if(strcmp(lexer->YYText(),">")==0)
oprel=SUP;
else if(strcmp(lexer->YYText(),"<=")==0)
oprel=INFE;
else if(strcmp(lexer->YYText(),">=")==0)
oprel=SUPE;
else oprel=WTFR;
current=(TOKEN) lexer->yylex();
return oprel;
}
// Expression := SimpleExpression [RelationalOperator SimpleExpression]
TYPES Expression(void){
enum TYPES typeA, typeB;
OPREL oprel;
typeA=SimpleExpression();
if(current==RELOP){
oprel=RelationalOperator();
typeB=SimpleExpression();
if (typeA!=typeB && !((typeA==DOUBLE && typeB==INTEGER) || (typeA==INTEGER && typeB==DOUBLE)))
Error("Memes types attendus ou double et entier attendus");
if (typeA==INTEGER || typeA==BOOLEAN){
if (typeB==DOUBLE) {
cout << "\tfldl (%rsp)\t" << endl;
cout << "\tfildl 8(%rsp)\t" << endl;
cout << "\taddq $16, %rsp"<<endl;
cout << "\tfcomip %st(1)"<<endl;
cout << "\tfaddp %st(1)\t# pop nothing"<<endl;
} else {
cout << "\tpop %rax"<<endl;
cout << "\tpop %rbx"<<endl;
cout << "\tcmpq %rax, %rbx"<<endl;
}
} else if (typeA==DOUBLE){
if (typeB==INTEGER) {
cout << "\tfildl (%rsp)\t" << endl;
} else {
cout << "\tfldl (%rsp)\t" << endl;
}
cout << "\tfldl 8(%rsp)\t" << endl;
cout << "\taddq $16, %rsp"<<endl;
cout << "\tfcomip %st(1)"<<endl;
cout << "\tfaddp %st(1)\t# pop nothing"<<endl;
} else if (typeA==CHAR){
cout << "\tpop %rax"<<endl;
cout << "\tpop %rbx"<<endl;
cout << "\tcmpb %al, %bl"<<endl;
} else {
Error("Entier, booléen, char ou double attendu");
}
switch(oprel){
case EQU:
cout << "\tje Vrai"<<++TagNumber<<"\t# If equal"<<endl;
break;
case DIFF:
cout << "\tjne Vrai"<<++TagNumber<<"\t# If different"<<endl;
break;
case SUPE:
cout << "\tjae Vrai"<<++TagNumber<<"\t# If above or equal"<<endl;
break;
case INFE:
cout << "\tjbe Vrai"<<++TagNumber<<"\t# If below or equal"<<endl;
break;
case INF:
cout << "\tjb Vrai"<<++TagNumber<<"\t# If below"<<endl;
break;
case SUP:
cout << "\tja Vrai"<<++TagNumber<<"\t# If above"<<endl;
break;
default:
Error("Opérateur de comparaison inconnu");
}
cout << "\tpush $0\t\t# False"<<endl;
cout << "\tjmp Suite"<<TagNumber<<endl;
cout << "Vrai"<<TagNumber<<":\tpush $0xFFFFFFFFFFFFFFFF\t\t# True"<<endl;
cout << "Suite"<<TagNumber<<":"<<endl;
return BOOLEAN;
}
return typeA;
}
KEYWORDS getKeyword(void){
KEYWORDS kw;
if(strcmp(lexer->YYText(),"IF")==0)
kw=IF;
else if(strcmp(lexer->YYText(),"THEN")==0)
kw=THEN;
else if(strcmp(lexer->YYText(),"ELSE")==0)
kw=ELSE;
else if(strcmp(lexer->YYText(),"WHILE")==0)
kw=WHILE;
else if(strcmp(lexer->YYText(),"FOR")==0)
kw=FOR;
else if(strcmp(lexer->YYText(),"DO")==0)
kw=DO;
else if(strcmp(lexer->YYText(),"TO")==0)
kw=TO;
else if(strcmp(lexer->YYText(),"BEGIN")==0)
kw=BEGIN;
else if(strcmp(lexer->YYText(),"END")==0)
kw=END;
else if (strcmp(lexer->YYText(),"VAR")==0)
kw=VAR;
else if (strcmp(lexer->YYText(),"DISPLAY")==0)
kw=DISPLAY;
else if (strcmp(lexer->YYText(),"DOWNTO")==0)
kw=DOWNTO;
else if (strcmp(lexer->YYText(),"TRUE")==0)
kw=TRUE;
else if (strcmp(lexer->YYText(),"FALSE")==0)
kw=FALSE;
else if (strcmp(lexer->YYText(),"CASE")==0)
kw=CASE;
else if (strcmp(lexer->YYText(),"OF")==0)
kw=OF;
else if (strcmp(lexer->YYText(),"DEFAULT")==0)
kw=DEFAULT;
else
kw=NONE;
return kw;
}
TYPES getType (string variable){
if (IsDeclared(variable.c_str()))
return DeclaredVariables[variable];
else
Error("Variable non déclarée");
return INTEGER;
}
// AssignementStatement := Identifier ":=" Expression
string AssignementStatement(void){
cout << "#-----------------------AssignementStatement-----------------------#"<<endl;
enum TYPES type1, type2;
string variable;
if(current!=ID)
Error("Identificateur attendu");
if (!IsDeclared(lexer->YYText()))
Error("Variable non déclarée");
variable=lexer->YYText();
current=(TOKEN) lexer->yylex();
type1=DeclaredVariables[variable];
if(current!=ASSIGN)
Error("':=' attendu pour l'assignation");
current=(TOKEN) lexer->yylex();
type2=Expression();
if (type1!=type2 && !((type1==DOUBLE && type2==INTEGER) || (type1==INTEGER && type2==DOUBLE)))
Error("Memes types attendus ou double et entier attendus");
cout << "\tpop "<<variable<<endl;
return variable;
}
// DisplayStatement := "DISPLAY" Expression
void DisplayStatement(void){
cout << "#-----------------------DisplayStatement"<< TagNumber << "-----------------------#"<<endl;
enum TYPES type;
current=(TOKEN) lexer->yylex();
type = Expression();
if (type == INTEGER){
cout << "\tpop %rsi \t# The value to be displayed"<<endl;
cout << "\tmovq $FormatStringSignedInt, %rdi \t# \"%llu\\n\""<<endl;
cout << "\txorl %eax, %eax \t# No floating point arguments"<<endl;
} else if (type == BOOLEAN){
cout << "\tpop %rsi \t# The value to be displayed"<<endl;
cout << "\tcmpq $0, %rsi \t# Compare with 0"<<endl;
cout << "\tjne displayVrai"<<++TagNumber<<"\t# If not equal to 0"<<endl;
cout << "\tmovq $FormatStringFalse, %rdi \t# \"FALSE\\n\""<<endl;
cout << "\tjmp Suite"<<TagNumber<<endl;
cout << "displayVrai"<<TagNumber<<":\tmovq $FormatStringTrue, %rdi \t# \"TRUE\\n\""<<endl;
cout << "Suite"<<TagNumber<<":"<<endl;
cout << "\txorl %eax, %eax \t# No floating point arguments"<<endl;
} else if (type == DOUBLE){
cout << "\tmovsd (%rsp), %xmm0 \t# L'adresse de la valeur dans le registre xmm0"<<endl;
cout << "\tsubq $16 , %rsp \t# Allocate 16 bytes on stack's top"<<endl;
cout << "\tmovsd %xmm0, 8(%rsp) \t# Store the value on the stack"<<endl;
cout << "\tmovq $FormatStringDouble, %rdi \t# \"%f\\n\""<<endl;
cout << "\tadd $24, %rsp \t# pop nothing"<<endl;
} else if (type == CHAR){
cout << "\tpop %rsi \t# The value to be displayed"<<endl;
cout << "\tmovq $FormatStringCHAR, %rdi \t# \"%c\\n\""<<endl;
cout << "\txorl %eax, %eax \t# No floating point arguments"<<endl;
} else if (type == STRING){
cout << "\tpop %rsi \t# The address of the string to be displayed" << endl;
cout << "\tmovq $FormatStringString, %rdi \t# \"%s\\n\"" << endl;
cout << "\txorl %eax, %eax \t# No floating point arguments" << endl;
} else {
Error("Entier ou booléen attendu");
}
cout << "\tcall printf@PLT \t # Display the value"<<endl;
}
// IfStatement := "IF" Expression "THEN" Statement [ "ELSE" Statement ]
void IfStatement(void){
unsigned long TagNumber1 = ++TagNumber;
cout << "#-----------------------IfStatement"<< TagNumber1 << "-----------------------#"<<endl;
if(current!=KEYWORD && getKeyword()!=IF)
Error("Mot clé 'IF' attendu");
current=(TOKEN) lexer->yylex();
Expression();
cout << "\tpop %rax"<<endl;
cout << "\tcmpq $0, %rax"<<endl;
cout << "\tje ELSE"<<TagNumber1<<endl;
if(current!=KEYWORD && getKeyword()!=THEN)
Error("Mot clé 'THEN' attendu");
current=(TOKEN) lexer->yylex();
Statement();
cout << "\tjmp FINIF"<<TagNumber1<<endl;
cout << "ELSE"<<TagNumber1<<":"<<endl;
if(current!=KEYWORD && getKeyword()!=ELSE)
Error("Mot clé 'ELSE' attendu");
current=(TOKEN) lexer->yylex();
Statement();
cout << "FINIF"<<TagNumber1<<":"<<endl;
}
// WhileStatement := "WHILE" Expression "DO" Statement
void WhileStatement(void){
unsigned long TagNumber1=++TagNumber;
cout << "#-----------------------WhileStatement"<< TagNumber1 << "-----------------------#"<<endl;
if(current!=KEYWORD && getKeyword()!=WHILE)
Error("Mot clé 'WHILE' attendu");
current=(TOKEN) lexer->yylex();
cout << "TESTWHILE"<<TagNumber1<<":"<<endl;
Expression();
cout << "\tpop %rax"<<endl;
cout << "\tcmpq $0, %rax"<<endl;
cout << "\tje FINWHILE"<<TagNumber1<<endl;
if(current!=KEYWORD && getKeyword()!=DO)
Error("Mot clé 'DO' attendu");
current=(TOKEN) lexer->yylex();
Statement();
cout << "\tjmp TESTWHILE"<<TagNumber1<<endl;
cout << "FINWHILE"<<TagNumber1<<":"<<endl;
}
// FORStatement := "FOR" AssignementStatement ("TO"|"DOWNTO") Expression "DO" Statement
void ForStatement(void){
unsigned long TagNumber1=++TagNumber;
cout << "#-----------------------ForStatement"<< TagNumber1 << "-----------------------#"<<endl;
enum TYPES type1, type2;
bool to = false; // true if the keyword is TO, false if it is DOWNTO
string variable;
if(current!=KEYWORD && getKeyword()!=FOR)
Error("Mot clé 'FOR' attendu");
current=(TOKEN) lexer->yylex();
variable = AssignementStatement();
type1 = getType(variable);
if(current!=KEYWORD && getKeyword()!=TO && getKeyword()!=DOWNTO) {
Error("Mot clé 'TO' ou 'DOWNTO' attend");
} else if (getKeyword()==TO){
to = true;
}
current=(TOKEN) lexer->yylex();
cout << "DEBUTFOR"<<TagNumber1<<":"<<endl;
type2 = Expression();
if (type1!=type2 || type1!=INTEGER) // les variables de la boucle FOR doivent être des entiers
Error("Les paramètres de la boucle FOR doivent être des entiers");
cout << "\tpop %rbx #\t valeur de l'expression"<<endl;
cout << "\tcmpq %rbx, "<<variable<<endl;
if (to){
cout << "\tjae FINFOR"<<TagNumber1<<endl;
} else {
cout << "\tjbe FINFOR"<<TagNumber1<<endl;
}
if(current!=KEYWORD && getKeyword()!=DO)
Error("Mot clé 'DO' attendu");
current=(TOKEN) lexer->yylex();
Statement();
if (to){
cout << "\tincq "<<variable<<endl;
} else {
cout << "\tdecq "<<variable<<endl;
}
cout << "\tjmp DEBUTFOR"<<TagNumber1<<endl;
cout << "FINFOR"<<TagNumber1<<":"<<endl;
}
// BlockStatement := "BEGIN" Statement { ";" Statement } "END"
void BlockStatement(void){
cout << "#-----------------------BlockStatement"<< TagNumber << "-----------------------#"<<endl;
if(current!=KEYWORD && getKeyword()!=BEGIN)
Error("Mot clé 'BEGIN' attendu");
current=(TOKEN) lexer->yylex();
Statement();
while(current==SEMICOLON){
current=(TOKEN) lexer->yylex();
Statement();
}
if(current!=KEYWORD && getKeyword()!=END)
Error("Mot clé 'END' attendu");
current=(TOKEN) lexer->yylex();
}
void CaseComparaison (TYPES typeExpression){
TYPES typeFactor = Factor();
if (typeFactor!=typeExpression)
Error("Cas de même type que l'expression attendu (case)");
if (typeFactor==INTEGER || typeFactor==BOOLEAN){
cout << "\tpop %rax"<<endl;
cout << "\tcmpq %rax, %rbx"<<endl;
cout << "\tje CASEVRAIS"<<TagNumber<<endl;
} else if (typeFactor==CHAR){
cout << "\tpop %rax"<<endl;
cout << "\tcmpb %al, %bl"<<endl;
cout << "\tje CASEVRAIS"<<TagNumber<<endl;
} else if (typeFactor==DOUBLE){
cout << "\tfldl (%rsp)"<<endl;
cout << "\tfucomip %st(1), %st(0)"<<endl;
cout << "\tje CASEVRAIS"<<TagNumber<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else {
Error("Entier, booléen ou char attendu pour les cases");
}
}
// CaseLabelList ::= Factor {, Factor }
void CaseLabelList(TYPES typeExpression){
unsigned long ThisCase=++TagNumber;
CaseComparaison(typeExpression);
while(current==COMMA){
CaseComparaison(typeExpression);
}
cout << "\tjmp ENDCASE"<<ThisCase<<endl;
}
// CaseListElement ::= CaseLabelList ":" Statement | Empty
void CaseListElement(TYPES typeExpression, int caseTagNumber){
CaseLabelList(typeExpression);
if (current!=COLON){
Error("':' attendu après la liste des cas");
}
current=(TOKEN) lexer->yylex();
if ((current==KEYWORD && getKeyword()!=END) && (strcmp(lexer->YYText(),";")!=0)){ // si le statement n'est pas vide
cout << "CASEVRAIS"<<TagNumber<<":"<<endl;
cout << "\taddq $8, %rsp"<<endl;
Statement();
cout << "\tjmp CASEFINISHED"<<caseTagNumber<<endl;
} else if (strcmp(lexer->YYText(),";")==0){ // si le statement est vide
cout << "CASEVRAIS"<<TagNumber<<":"<<endl;
cout << "\tjmp CASEFINISHED"<<caseTagNumber<<endl;
} else {
Error("Instruction attendue après les cas");
}
}
// CaseStatement ::= "CASE" Expression "OF" CaseListElement {";" CaseListElement } [";" DEFAULT Statement] "END"
void CaseStatement(void){
unsigned long TagNumber1=++TagNumber;// TagNumber1 est le numéro du CASE nous permettant de sauter les cases si une condition est vérifiée
cout << "#-----------------------CaseStatement"<< TagNumber1 << "-----------------------#"<<endl;
if(current!=KEYWORD && getKeyword()!=CASE)
Error("Mot clé 'CASE' attendu");
current=(TOKEN) lexer->yylex();
TYPES typeExpression = Expression();
if(current!=KEYWORD && getKeyword()!=OF)
Error("Mot clé 'OF' attendu");
current=(TOKEN) lexer->yylex();
if (typeExpression==DOUBLE){
cout << "\tfldl (%rsp)"<<endl;
cout << "\taddq $8, %rsp"<<endl;
} else {
cout << "\tpop %rbx"<<endl;
}
CaseListElement(typeExpression, TagNumber1); // type = pour eviter les comparaisons de types différents, TagNumber1 = permettre le jmp à la fin du casestatement si une condition est vérifiée
cout << "ENDCASE"<<TagNumber<<":"<<endl;
while(current==SEMICOLON){
current=(TOKEN) lexer->yylex();
if (current==KEYWORD && getKeyword()==DEFAULT){
current=(TOKEN) lexer->yylex();
if (current!=COLON)
Error("':' attendu après le mot clé 'DEFAULT'");
current=(TOKEN) lexer->yylex();
Statement();
if (current!=KEYWORD && getKeyword()!=END)
Error("Mot clé 'END' attendu après le mot clé 'DEFAULT'");
break;
} else {
CaseListElement(typeExpression, TagNumber1);
cout << "ENDCASE"<<TagNumber<<":"<<endl;
}
}
cout << "CASEFINISHED"<<TagNumber1<<":"<<endl;
if(current!=KEYWORD && getKeyword()!=END)
Error("Mot clé 'END' attendu");
current=(TOKEN) lexer->yylex();
}
// NotStatement ::= "NOT" Expression
void NotStatement(void){
cout << "#-----------------------NotStatement"<< TagNumber << "-----------------------#"<<endl;
if(current!=NOT)
Error("Mot clé 'NOT' attendu");
current=(TOKEN) lexer->yylex();
Expression();
cout << "\tpop %rax"<<endl;
cout << "\txorq $0xFFFFFFFFFFFFFFFF, %rax"<<endl;
cout << "\tpush %rax"<<endl;
}
// Statement := AssignementStatement | IfStatement | WhileStatement | ForStatement | BlockStatement | DisplayStatement | CaseStatement
void Statement(void){
switch(current){
case ID:
AssignementStatement();
break;
case KEYWORD:
switch(getKeyword()){
case IF:
IfStatement();
break;
case WHILE:
WhileStatement();
break;
case FOR:
ForStatement();
break;
case BEGIN:
BlockStatement();
break;
case DISPLAY:
DisplayStatement();
break;
case CASE:
CaseStatement();
break;
case NOT:
NotStatement();
break;
default:
Error("Instruction non reconnue");
}
break;
case NOT:
NotStatement();
break;
default:
Error("Instruction non reconnue");
}
}
// StatementPart := Statement {";" Statement} "."
void StatementPart(void){
cout << "\t.text\t\t# The following lines contain the program"<<endl;
cout << "\t.globl main\t# The main function must be visible from outside"<<endl;
cout << "main:\t\t\t# The main function body :"<<endl;
cout << ".cfi_startproc"<<endl;
cout << "endbr64"<<endl;
cout << "\tpushq %rbp \t# Save the position of the stack's top"<<endl;
Statement();
while(current==SEMICOLON){
current=(TOKEN) lexer->yylex();
Statement();
}
if(current!=DOT)
Error("caractère '.' attendu");
current=(TOKEN) lexer->yylex();
}
// Program := [VarDeclarationPart] StatementPart
void Program(void){
if(current==KEYWORD && getKeyword()==VAR)
VarDeclarationPart();
StatementPart();
}
int main(void){ // First version : Source code on standard input and assembly code on standard output
// Header for gcc assembler / linker
cout << "\t\t\t# This code was produced by the CERI Compiler"<<endl;
cout << ".data"<<endl;
cout << "FormatStringSignedInt:\t.string \"%lld\\n\"\t# used by printf to display 64-bit unsigned integers"<<endl;
cout << "FormatStringDouble:\t.string \"%f\\n\"\t# used by printf to display double precision floating point numbers"<<endl;
cout << "FormatStringCHAR:\t.string \"%c\\n\"\t# used by printf to display CHARacters"<<endl;
cout << "FormatStringTrue:\t.string \"TRUE\\n\"\t# used by printf to display the boolean value TRUE"<<endl;
cout << "FormatStringFalse:\t.string \"FALSE\\n\"\t# used by printf to display the boolean value FALSE"<<endl;
cout << "FormatStringString:\t.string \"%s\\n\"\t# used by printf to display strings"<<endl;
// Let's proceed to the analysis and code production
current=(TOKEN) lexer->yylex();
Program();
// Trailer for the gcc assembler / linker
cout << "\tpopq %rbp\t\t# Restore the position of the stack's top"<<endl;
cout << "\tret\t\t\t# Return from main function"<<endl;
cout << ".cfi_endproc"<<endl;
if(current!=FEOF){
cerr <<"Caractères en trop à la fin du programme : ["<<current<<"]";
Error("."); // unexpected CHARacters at the end of program
}
}