aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
blob: 1b11b675f2f16497edbb03c63b2fe22f72fd1024 (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
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
/**
 * UPEM / Compilation / Projet
 * Pacien TRAN-GIRARD, Adam NAILI
 */

#include "generator.h"
void yyerror(char *);
// ----- GLOBAL FUNCTIONS -----

void gen_prologue() {
  fprintf(output, "extern printf\nextern scanf\n");
  fprintf(output, "section .data\n");
}

void gen_prologue_continue(int *bss_done) {
  if (*bss_done != 0)
    return;

  fprintf(output, "format_int db \"%%d\",10,0\n");
  fprintf(output, "format_char db \"%%c\",10,0\n");
  fprintf(output, "format_int_in: db \"%%d\", 0\n");
  fprintf(output, "format_char_in: db \"%%c\", 0\n\n");
  fprintf(output, "section .bss\n");
  fprintf(output, "globals: resq %d\n", nb_globals);
  fprintf(output, "section .text\n\nglobal _start\n");
  fprintf(output, "\nprinte: ;print needs an argument in rax\n");
  fprintf(output, "push rbp\n");
  fprintf(output, "mov rbp, rsp\n");
  fprintf(output, "push rsi\n");
  fprintf(output, "push rdi\n");
  fprintf(output, "mov rsi, rax\n");
  fprintf(output, "mov rdi, format_int\n");
  fprintf(output, "mov rax, 0\n");
  fprintf(output, "call printf WRT ..plt\n");
  fprintf(output, "pop rdi\n");
  fprintf(output, "pop rsi\n");
  fprintf(output, "pop rbp\n");
  fprintf(output, "ret\n");
  fprintf(output, "\nprintc: ;print needs an argument in rax\n");
  fprintf(output, "push rbp\n");
  fprintf(output, "mov rbp, rsp\n");
  fprintf(output, "push rsi\n");
  fprintf(output, "push rdi\n");
  fprintf(output, "mov rsi, rax\n");
  fprintf(output, "mov rdi, format_char\n");
  fprintf(output, "mov rax, 0\n");
  fprintf(output, "call printf WRT ..plt\n");
  fprintf(output, "pop rdi\n");
  fprintf(output, "pop rsi\n");
  fprintf(output, "pop rbp\n");
  fprintf(output, "ret\n");
  fprintf(output, "\nreade: ;read needs an adress in rax\n");
  fprintf(output, "push rbp\n");
  fprintf(output, "mov rbp, rsp\n");
  fprintf(output, "push rsi\n");
  fprintf(output, "push rdi\n");
  fprintf(output, "mov rsi, rax\n");
  fprintf(output, "mov rdi, format_int_in\n");
  fprintf(output, "mov rax, 0\n");
  fprintf(output, "call scanf\n");
  fprintf(output, "pop rdi\n");
  fprintf(output, "pop rsi\n");
  fprintf(output, "pop rbp\n");
  fprintf(output, "ret\n");
  fprintf(output, "\nreadc: ;read needs an adress in rax\n");
  fprintf(output, "push rbp\n");
  fprintf(output, "mov rbp, rsp\n");
  fprintf(output, "push rsi\n");
  fprintf(output, "push rdi\n");
  fprintf(output, "mov rsi, rax\n");
  fprintf(output, "mov rdi, format_char_in\n");
  fprintf(output, "mov rax, 0\n");
  fprintf(output, "call scanf\n");
  fprintf(output, "pop rdi\n");
  fprintf(output, "pop rsi\n");
  fprintf(output, "pop rbp\n");
  fprintf(output, "ret\n");
  *bss_done = 1;
}

void gen_epilogue() {
  fprintf(output, "\n_start:\n");
  fprintf(output, "push rbp\nmov rbp, rsp\n\n");
  fprintf(output, "call main\n");
  fprintf(output, "\nmov rax,60 \n");
  fprintf(output, "mov rdi,0 \n");
  fprintf(output, "syscall \n\n");
  fprintf(output, ";global table\n");
  glo_display_table();
  fprintf(output, ";local table\n");
  loc_display_table();
  fprintf(output, ";function table\n");
  fun_display_table();

  // require a main function
  fun_lookup("main", 0);
}

void gen_const(const char name[], int value, Scope scope) {
  switch (scope) {
  case LOCAL:
    loc_addConst(name);
    fprintf(output, "push %d\n", value);
    return;

  case GLOBAL:
    glo_addConst(name);
    fprintf(output, "%s dq %d\n", name, value);
    return;
  }
}

Type gen_function_declaration(const char name[], int return_type) {
  fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name);
  return return_type;
}

void gen_function_end_declaration(const char name[], int return_type,
                                  int nb_param) {
  fun_add(name, return_type, nb_param);
  fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n");
}

void gen_function_return(Type expect, Type actual) {
  if (actual != expect) {
    yyerror("Return type mismatch");
    return;
  }
  if (actual != VOID_T)
    fprintf(output, "pop rax\n");
  fprintf(output, "mov rsp, rbp\npop rbp\nret\n");
}

Type gen_function_call(const char name[], int nb_param) {
  Type return_type = fun_lookup(name, nb_param);
  fprintf(output, "call %s\n", name);
  if (return_type != VOID_T)
    fprintf(output, "push rax\n");
  return return_type;
}

void gen_declaration(const char name[], int type, Scope scope) {
  switch (scope) {
  case GLOBAL:
    glo_addVar(name, type);
    nb_globals++;
    break;
  case LOCAL:
    loc_addVar(name, type);
    fprintf(output, "push 0\n");
    break;
  }
}

void gen_tab_declaration(const char name[], Scope scope, int size) {
  switch (scope) {
  case GLOBAL:
    glo_addVar(name, TAB);
    nb_globals = nb_globals + size;
    break;
  case LOCAL:
    loc_addVar(name, TAB);
    for (int i = 0; i < size; ++i) {
      fprintf(output, "push 0\n");
    }
    break;
  }
}

// Verifies that the variable exists
void gen_check(const char name[], Scope scope) {
  switch (scope) {
  case GLOBAL:
    glo_lookup(name);
    break;
  case LOCAL:
    loc_lookup(name);
    break;
  }
}

// ----- READ AND PRINT FUNCTIONS -----
void gen_reade(const char name[], Scope scope) {
  if (is_read_only(name, scope)) {
    yyerror("Symbol is read only");
    return;
  }

  if (loc_lookup(name) != INT) {
    yyerror("Need to be a INT in the reade() function");
    return;
  }
  int l_addr = loc_get_addr(name);
  int g_addr = glo_get_addr(name);
  if (l_addr != -1) {
    fprintf(output, "mov rax,rbp\nsub rax,%d\ncall reade\n",
            loc_get_addr(name));
    return;
  }
  fprintf(output, "mov rax,globals\nadd rax,%d\ncall reade\n", g_addr);
}

void gen_readc(const char name[], Scope scope) {
  if (is_read_only(name, scope)) {
    yyerror("Symbol is read only");
    return;
  }

  if (loc_lookup(name) != CHAR) {
    yyerror("Need to be a CHAR in the readc() function");
    return;
  }
  int l_addr = loc_get_addr(name);
  int g_addr = glo_get_addr(name);
  if (l_addr != -1) {
    fprintf(output, "mov rax,rbp\nsub rax,%d\ncall readc\n",
            loc_get_addr(name));
    return;
  }
  fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr);
}

void gen_print(int type) {
  // check if the name exists in both tables
  fprintf(output, "pop rax\n");
  switch (type) {
  case INT:
    fprintf(output, "call printe\n");
    break;
  case CHAR:
    fprintf(output, "call printc\n");
    break;
  case TAB:
    fprintf(output, "call printe\n");
    break;
  default:
    yyerror("Error print, supposed to have type CHAR or INT or TAB");
  }
}

// ----- CONDITIONAL BRANCHING ------

void gen_if_label(int idx) {
  fprintf(output, ".end_if%d:\n", idx);
  fprintf(output, ";ENDIF\n\n");
}

void gen_if_start(int idx) {
  fprintf(output, "\n;BEGINIF\n");
  fprintf(output, "pop rax\n");
  fprintf(output, "cmp rax,0\n");
  fprintf(output, "jz .end_if%d\n", idx);
}

void gen_if_end(int idx) {
  fprintf(output, "jmp .end_ifelse%d\n", idx);
  fprintf(output, ".end_if%d:\n", idx);
}

void gen_ifelse_end(int idx) {
  fprintf(output, ".end_ifelse%d:\n", idx);
  fprintf(output, ";ENDIF\n\n");
}

// ----- OPERATORS -----

static int gen_assign_simple(const char ident[], Scope scope) {
  int l_addr = loc_get_addr(ident);
  int g_addr = glo_get_addr(ident);

  if (is_read_only(ident, scope)) {
    yyerror("Symbol is read only");
    return loc_lookup(ident);
  }

  switch (scope) {
  case GLOBAL:
    fprintf(output, "pop QWORD [globals + %d] ;%s\n", g_addr, ident);
    return glo_lookup(ident);
  case LOCAL:
    if (l_addr != -1) {
      fprintf(output, "pop QWORD [rbp - %d] ;%s\n", l_addr, ident);
      return loc_lookup(ident);
    } else {
      fprintf(output, "pop QWORD [globals + %d] ;%s\n", g_addr, ident);
      return glo_lookup(ident);
    }
  default:
    exit(1);
  }
}

static int gen_assign_tab(const char ident[], Scope scope) {
  int l_addr = loc_get_addr(ident);
  int g_addr = glo_get_addr(ident);

  if (is_read_only(ident, scope)) {
    yyerror("Symbol is read only");
    return loc_lookup(ident);
  }

  switch (scope) {
  case GLOBAL:
    fprintf(output, "pop r8 ;EXP\npop rcx ;offset\npush r8\nimul rcx,8\nmov "
                    "rax,globals\n add rax,rcx\npop QWORD [rax + %d] ;%s\n",
            g_addr, ident);
    return glo_lookup(ident);
  case LOCAL:
    if (l_addr != -1) {
      fprintf(output, "pop r8 ;EXP\npop rcx ;offset\npush r8\nimul rcx,8\nmov "
                      "rax,rbp\nsub rax,rcx\npop QWORD [rax - %d] ;%s\n",
              l_addr, ident);
      return loc_lookup(ident);
    } else {
      fprintf(output, "pop r8 ;EXP\npop rcx ;offset\npush r8\nimul rcx,8\nmov "
                      "rax,globals\n add rax,rcx\npop QWORD [rax + %d] ;%s\n",
              g_addr, ident);
      return glo_lookup(ident);
    }
  default:
    exit(1);
  }
}

int gen_assign(const char ident[], Scope scope) {
  switch (loc_lookup(ident)) {
  case TAB: return gen_assign_tab(ident, scope);
  default: return gen_assign_simple(ident, scope);
  }
}

void gen_or(int left, int right, int idx) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  fprintf(output, ";a OR c\n");
  fprintf(output, "pop rax\n");
  fprintf(output, "cmp rax,1\n");
  fprintf(output, "je .true%d\n", idx);
  fprintf(output, "pop rcx\n");
  fprintf(output, "cmp rcx,1\n");
  fprintf(output, "jz .true%d\n", idx);
  fprintf(output, "push 0\n");
  fprintf(output, "jmp .false%d\n", idx);
  fprintf(output, ".true%d:\n", idx);
  fprintf(output, "push 1\n");
  fprintf(output, ".false%d:\n", idx);
}

void gen_and(int left, int right, int idx) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  fprintf(output, ";a AND c\n");
  fprintf(output, "pop rax\n");
  fprintf(output, "cmp rax,0\n");
  fprintf(output, "jz .false%d\n", idx);
  fprintf(output, "pop rcx\n");
  fprintf(output, "cmp rcx,0\n");
  fprintf(output, "jz .false%d\n", idx);
  fprintf(output, "push 1\n");
  fprintf(output, "jmp .true%d\n", idx);
  fprintf(output, ".false%d:\n", idx);
  fprintf(output, "push 0\n");
  fprintf(output, ".true%d:\n", idx);
}

void gen_eq(const char op[], int left, int right, int idx) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  fprintf(output, ";a EQ c\npop rax\npop rcx\ncmp rax,rcx\n");

  if (!strcmp(op, "=="))
    fprintf(output, "je .true%d\n", idx);
  else if (!strcmp(op, "!="))
    fprintf(output, "jne .true%d\n", idx);
  else
    exit(1); // TODO: error on unexpected op

  fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx,
          idx, idx);
}

void gen_order(const char op[], int left, int right, int idx) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  fprintf(output, ";a ORDER c\npop rcx\npop rax\ncmp rax,rcx\n");

  if (!strcmp(op, "<"))
    fprintf(output, "jl .true%d\n", idx);
  else if (!strcmp(op, "<="))
    fprintf(output, "jle .true%d\n", idx);
  else if (!strcmp(op, ">"))
    fprintf(output, "jg .true%d\n", idx);
  else if (!strcmp(op, "<="))
    fprintf(output, "jge .true%d\n", idx);
  else
    exit(1); // TODO: error on unexpected op

  fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx,
          idx, idx);
}

void gen_addsub(char op, int left, int right) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  switch (op) {
  case '+':
    fprintf(output, ";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n");
    break;
  case '-':
    fprintf(output, ";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n");
    break;
  default:
    exit(1);
  }
}

void gen_divstar(char op, int left, int right) {
  check_expected_types(left, INT, TAB);
  check_expected_types(right, INT, TAB);

  switch (op) {
  case '*':
    fprintf(output, ";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n");
    break;
  case '/':
  case '%':
    fprintf(output, ";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n");
    fprintf(output, "push %s\n", op == '/' ? "rax" : "rdx");
    break;
  default:
    exit(1);
  }
}

int gen_signed_expr(char op, int type) {
  check_expected_types(type, INT, TAB);
  switch (op) {
  case '+':
    fprintf(output, ";+F\n");
    break;
  case '-':
    fprintf(output, ";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n");
    break;
  default:
    exit(1);
  }

  return type;
}

int gen_negate_expr(int type) {
  check_expected_types(type, INT, TAB);
  fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n");
  return type;
}

static int gen_value_simple(const char ident[], Scope scope) {
  int l_addr = loc_get_addr(ident);

  switch (scope) {
  case LOCAL:
    if (l_addr != -1) {
      fprintf(output, "push QWORD [rbp - %d] ;%s\n", l_addr, ident);
      return loc_lookup(ident);
    }

  case GLOBAL:
    if (is_read_only(ident, scope))
      fprintf(output, "push QWORD [%s]\n", ident);
    else
      fprintf(output, "push QWORD [globals + %d] ;%s\n", glo_get_addr(ident),
              ident);

    return glo_lookup(ident);

  default:
    exit(1);
  }
}

static int gen_value_tab(const char ident[], Scope scope) {
  int l_addr = loc_get_addr(ident);
  int g_addr = glo_get_addr(ident);
  switch (scope) {
  case GLOBAL:
    fprintf(output, "pop rcx ;offset\nimul rcx,8\nmov rax,globals\n add "
                    "rax,rcx\npush QWORD [rax + %d] ;%s\n",
            g_addr, ident);
    return glo_lookup(ident);
  case LOCAL:
    if (l_addr != -1) {
      fprintf(output, "pop rcx ;offset\nimul rcx,8\nmov rax,rbp\nsub "
                      "rax,rcx\npush QWORD [rax - %d] ;%s\n",
              l_addr, ident);
      return loc_lookup(ident);
    } else {
      fprintf(output, "pop rcx ;offset\nimul rcx,8\nmov rax,globals\n add "
                      "rax,rcx\npush QWORD [rax + %d] ;%s\n",
              g_addr, ident);
      return glo_lookup(ident);
    }
  default:
    exit(1);
  }
}

int gen_value(const char ident[], Scope scope) {
  switch (loc_lookup(ident)) {
  case TAB: return gen_value_tab(ident, scope);
  default: return gen_value_simple(ident, scope);
  }
}

int gen_num(int value, Scope scope) {
  fprintf(output, "push %d\n", value);
  return INT;
}

int gen_char(int value, Scope scope) {
  fprintf(output, "push %d\n", value);
  return CHAR;
}