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

#include "generator.h"
#include "symbol_table.h"

// ----- GLOBAL FUNCTIONS -----

void gen_prologue() {
  fprintf(output, "extern printf\n");
  fprintf(output, "section .data\n");
  fprintf(output, "format_int db \"%%d\",10,0\n\n");
  fprintf(output, "section .bss\n");
}
void gen_prologue_continue(){
  fprintf(output, "globals: resq %d\n", nb_globals);
  fprintf(output, "section .text\n\nglobal _start\n");
  fprintf(output, "print: ;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, "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 rsi\n");
  fprintf(output, "pop rbp\n");
  fprintf(output, "ret\n");
  fprintf(output, "\n_start:\n");
  fprintf(output, "push rbp\nmov rbp, rsp\n\n");
}
void gen_const_declaration() {
  fprintf(output, "mov rax,60 \n");
  fprintf(output, "mov rdi,0 \n");
  fprintf(output, "syscall \n");
  fprintf(output, ";global table\n");
  glo_display_table();
  fprintf(output, ";local table\n");
  loc_display_table();
}

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;
  }

}

// ----- READ AND PRINT FUNCTIONS -----

void gen_read(const char name[], Scope scope) {
  switch (scope) {
  case GLOBAL:
    glo_lookup(name);
    break;
  case LOCAL:
    loc_lookup(name);
    break;
  }
}

void gen_print() {
  fprintf(output, "pop rax\n");
  fprintf(output, "call print\n");
}

// ----- 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 -----

int gen_assign(const char ident[], Scope scope) {
  const char instr_fmt[] = "pop QWORD [rbp - %d] ;%s\n";

  switch (scope) {
  case GLOBAL:
    fprintf(output, instr_fmt, glo_get_addr(ident), ident);
    return glo_lookup(ident);
  case LOCAL:
    fprintf(output, instr_fmt, loc_get_addr(ident), ident);
    return loc_lookup(ident);
  default:
    exit(1);
  }
}

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

  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:", idx);
}

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

  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:", idx);
}

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

  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:", idx, idx, idx);
}

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

  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:", idx, idx, idx);
}

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

  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_type(left, INT);
  check_expected_type(right, INT);

  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) {
  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) {
  fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n");
  return type;
}

int gen_value(const char ident[], Scope scope) {
  switch (scope) {
  case GLOBAL:
    fprintf(output, "push QWORD [rbp - %d] ;%s\n", glo_get_addr(ident), ident);
    return glo_lookup(ident);
  case LOCAL:
    fprintf(output, "push QWORD [rbp - %d] ;%s\n", loc_get_addr(ident), ident);
    return loc_lookup(ident);
  default:
    exit(1);
  }
}

int gen_num(int value, Scope scope) {
  if (scope == LOCAL) fprintf(output, "push %d\n", value); // TODO: remove if?
  // stored for the semantic analysis.

  return INT;
}

int gen_char(int value, Scope scope) {
  if(scope == LOCAL) fprintf(output, "push %d\n", value); // TODO: remove if?
  return CHAR;
}