aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
blob: 41fa1d26e39a4ba52552cc8ee68339ea2936c3bb (plain)
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
%{
/*
 * UPEM / Compilation / Projet
 * Pacien TRAN-GIRARD, Adam NAILI
 *
 *
 *
 *
 *
 *
 *	TODO : 
 * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.)
 * - Evaluation paresseuse 
 * - Gestion des tableaux
 *
 * 
 *
 */

#include <stdio.h>
#include "symboltable.h"

extern int lineno;
int yylex();
void yyerror(char *);
#define GLOBAL 0
#define LOCAL 1
static int status = GLOBAL;

%}

%union {
    char caractere;
    int num;
    char ident[64];
    int type;
    char comp[3];
    char addsub;
    char divstar;
}
%token <caractere> CARACTERE
%token <num> NUM
%token <ident> IDENT
%token <comp> ORDER EQ
%token <addsub> ADDSUB
%token <divstar> DIVSTAR
%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE
%token <type> TYPE

%type <num> Exp EB TB FB M E T F 
%type <ident> LValue

%left ','
%precedence ')'
%precedence ELSE

%%
Prog:{printf("extern printf\n");
			printf("section .data\n");
			printf("format_int db \"%%d\",10,0\n\n");
			printf("section .bss\nsection .text\n\nglobal _start\n");
			printf("print: ;print needs an argument in rax\n");
			printf("push rbp\n");
			printf("mov rbp, rsp\n");
			printf("push rsi\n");
			printf("mov rsi, rax\n");
			printf("mov rdi, format_int\n");
			printf("mov rax, 0\n");
		  printf("call printf WRT ..plt\n");	
			printf("pop rsi\n");
			printf("pop rbp\n");
			printf("ret\n");
			printf("\n_start:\n");	
      printf("push rbp\nmov rbp, rsp\n\n");
			}
     DeclConsts DeclVars DeclFoncts 
     {
        printf("mov rax,60 \n");
        printf("mov rdi,0 \n");  
        printf("syscall \n");
        printf(";global table\n");
        glo_display_table();
        printf(";local table\n");
        loc_display_table();
     }
  ;
DeclConsts:
     DeclConsts CONST ListConst ';'
  |
  ;
ListConst:
     ListConst ',' IDENT '=' Litteral
  |  IDENT '=' Litteral
  ;
Litteral:
     NombreSigne
  |  CARACTERE
  ;
NombreSigne:
     NUM
  |  ADDSUB NUM
  ;
DeclVars:
     DeclVars TYPE Declarateurs ';'
  |
  ;
Declarateurs:
     Declarateurs ',' Declarateur   {if(status == GLOBAL) glo_addVar($<ident>3, $<type>0);
                                     else loc_addVar($<ident>3, $<type>0);
                                     printf("push 0\n");}
  |  Declarateur                    {if(status == GLOBAL) glo_addVar($<ident>1, $<type>0);
                                     else loc_addVar($<ident>1, $<type>0);
                                     printf("push 0\n");}
  ;
Declarateur:
     IDENT
  |  IDENT '[' NUM ']'
  ;
DeclFoncts:
     DeclFoncts DeclFonct
  |  DeclFonct
  ;
DeclFonct:
     EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;}
  ;
EnTeteFonct:
     TYPE IDENT '(' Parametres ')'
  |  VOID IDENT '(' Parametres ')'
  ;
Parametres:
     VOID
  |  ListTypVar
  ;
ListTypVar:
     ListTypVar ',' TYPE IDENT    {if(status == GLOBAL) glo_addVar($<ident>4, $<type>3);
                                     else loc_addVar($<ident>4, $<type>3);
                                   printf("push 0\n");}
  |  TYPE IDENT                   {if(status == GLOBAL) glo_addVar($<ident>2, $<type>1);
                                     else loc_addVar($<ident>2, $<type>1);
                                   printf("push 0\n");}
  ;
Corps:
     '{' DeclConsts DeclVars SuiteInstr '}'
  ;
SuiteInstr:
     SuiteInstr Instr
  |  ;
Instr:
     Exp ';'
  |  ';'
  |  RETURN Exp ';'
  |  RETURN ';'
  |  READE '(' IDENT ')' ';'      {if(status == GLOBAL) glo_lookup($<ident>3);
                                  else loc_lookup($<ident>3);}
  |  READC '(' IDENT ')' ';'      {if(status == GLOBAL) glo_lookup($<ident>3);
                                  else loc_lookup($<ident>3);}
  |  PRINT '(' Exp ')' ';'        {printf("pop rax\ncall print\n");}
  |  IF '(' Exp ')' Instr
  |  IF '(' Exp ')' Instr ELSE Instr
  |  WHILE '(' Exp ')' Instr
  |  '{' SuiteInstr '}'
  ;
Exp:
     LValue '=' Exp               {if(status == GLOBAL){
                                    $$ = glo_lookup($<ident>1);
                                    printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
                                  }
                                  else {
                                    $$ = loc_lookup($<ident>1);
                                    printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
                                  }
                                  }
  |  EB
  ;
EB:
     EB {check_expected_type($1,INT); printf("pop rax\n");} 
     OR TB {check_expected_type($4,INT);} 
  |  TB
  ;
TB:
     TB AND FB                    {check_expected_type($1,INT);check_expected_type($3,INT);}
  |  FB
  ;
FB:
     FB EQ M                      {check_expected_type($1,INT);check_expected_type($3,INT);}
  |  M
  ;
M:
     M ORDER E                    {check_expected_type($1,INT);check_expected_type($3,INT);}
  |  E
  ;
E:
     E ADDSUB T                   {check_expected_type($1,INT);check_expected_type($3,INT);
                                  if($2 == '+'){
                                    printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n");
                                  }
                                  else{
                                    printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n");
                                  }
                                  }
  |  T
  ;
T:
     T DIVSTAR F                  {check_expected_type($1,INT);check_expected_type($3,INT);
                                  if($2 == '*'){
                                    printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n");
                                  }
                                  else{
                                    printf(";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n");
                                    if ($2 == '/'){
                                    	printf("push rax\n");
                                    }
                                    else{
                                    	printf("push rdx\n");
                                    }
                                  }
                                  }
  |  F
  ;
F:
     ADDSUB F                     {$$ = $2;//on fait remonter le type
                                  if($1 == '+') {
                                    printf(";+F\n");
                                  }
                                  else{
                                    printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n");
                                  }
                                  } 
  |  '!' F                        {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} 
  |  '(' Exp ')'                  {$$ = $2;}
  |  LValue                       {if(status == GLOBAL) {$$ = glo_lookup($<ident>1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);}
                                  else {$$ = loc_lookup($<ident>1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);}}
  |  NUM                          {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique
  |  CARACTERE                    {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);}
  |  IDENT '(' Arguments  ')'     {$$ = INT;} //tableau d'entiers uniquement
  ;
LValue:
     IDENT                        {if(status == GLOBAL) {glo_lookup($<ident>1);}
                                  else {loc_lookup($<ident>1);}}
  |  IDENT  '[' Exp  ']'          {if(status == GLOBAL) {glo_lookup($<ident>1);}
                                  else {loc_lookup($<ident>1);}}
  ;
Arguments:
     ListExp
  |
  ;
ListExp:
     ListExp ',' Exp
  |  Exp
  ;
%%

void yyerror(char *msg){
  fprintf(stderr, "%s at line %d\n", msg, lineno);
}

int main(int argc, char **argv) {
  return yyparse();
}