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

#include "generator.h"
#include <stdio.h>
#include "symbol_table.h"

void prologue() {
  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");
}

void const_declaration() {
  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();
}

void declaration(const char name[], int type, Scope scope) {
  switch (scope) {
  case GLOBAL:
    glo_addVar(name, type);
    break;
  case LOCAL:
    loc_addVar(name, type);
    break;
  }

  printf("push 0\n");
}