2020-10-07 14:11:16 +03:00
|
|
|
#include "chibicc.h"
|
|
|
|
|
|
|
|
static int depth;
|
2020-08-27 15:04:17 +03:00
|
|
|
static char *argreg8[] = {"%dil", "%sil", "%dl", "%cl", "%r8b", "%r9b"};
|
|
|
|
static char *argreg64[] = {"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"};
|
2020-09-04 11:58:53 +03:00
|
|
|
static Obj *current_fn;
|
2020-10-07 14:11:16 +03:00
|
|
|
|
2019-08-05 15:12:44 +03:00
|
|
|
static void gen_expr(Node *node);
|
|
|
|
|
2020-10-07 06:47:09 +03:00
|
|
|
static int count(void) {
|
|
|
|
static int i = 1;
|
|
|
|
return i++;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
static void push(void) {
|
|
|
|
printf(" push %%rax\n");
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop(char *arg) {
|
|
|
|
printf(" pop %s\n", arg);
|
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
// Round up `n` to the nearest multiple of `align`. For instance,
|
|
|
|
// align_to(5, 8) returns 8 and align_to(11, 8) returns 16.
|
|
|
|
static int align_to(int n, int align) {
|
|
|
|
return (n + align - 1) / align * align;
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
// Compute the absolute address of a given node.
|
|
|
|
// It's an error if a given node does not reside in memory.
|
|
|
|
static void gen_addr(Node *node) {
|
2019-08-05 15:12:44 +03:00
|
|
|
switch (node->kind) {
|
|
|
|
case ND_VAR:
|
2020-09-05 02:43:21 +03:00
|
|
|
if (node->var->is_local) {
|
|
|
|
// Local variable
|
|
|
|
printf(" lea %d(%%rbp), %%rax\n", node->var->offset);
|
|
|
|
} else {
|
|
|
|
// Global variable
|
|
|
|
printf(" lea %s(%%rip), %%rax\n", node->var->name);
|
|
|
|
}
|
2020-09-26 02:59:56 +03:00
|
|
|
return;
|
2019-08-05 15:12:44 +03:00
|
|
|
case ND_DEREF:
|
|
|
|
gen_expr(node->lhs);
|
|
|
|
return;
|
2020-09-26 02:59:56 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
error_tok(node->tok, "not an lvalue");
|
2020-09-26 02:59:56 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
// Load a value from where %rax is pointing to.
|
|
|
|
static void load(Type *ty) {
|
|
|
|
if (ty->kind == TY_ARRAY) {
|
|
|
|
// If it is an array, do not attempt to load a value to the
|
|
|
|
// register because in general we can't load an entire array to a
|
|
|
|
// register. As a result, the result of an evaluation of an array
|
|
|
|
// becomes not the array itself but the address of the array.
|
|
|
|
// This is where "array is automatically converted to a pointer to
|
|
|
|
// the first element of the array in C" occurs.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:04:17 +03:00
|
|
|
if (ty->size == 1)
|
|
|
|
printf(" movsbq (%%rax), %%rax\n");
|
|
|
|
else
|
|
|
|
printf(" mov (%%rax), %%rax\n");
|
2020-09-26 04:15:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store %rax to an address that the stack top is pointing to.
|
2020-08-27 15:04:17 +03:00
|
|
|
static void store(Type *ty) {
|
2020-09-26 04:15:32 +03:00
|
|
|
pop("%rdi");
|
2020-08-27 15:04:17 +03:00
|
|
|
|
|
|
|
if (ty->size == 1)
|
|
|
|
printf(" mov %%al, (%%rdi)\n");
|
|
|
|
else
|
|
|
|
printf(" mov %%rax, (%%rdi)\n");
|
2020-09-26 04:15:32 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
// Generate code for a given node.
|
2020-10-07 14:11:16 +03:00
|
|
|
static void gen_expr(Node *node) {
|
|
|
|
switch (node->kind) {
|
|
|
|
case ND_NUM:
|
|
|
|
printf(" mov $%d, %%rax\n", node->val);
|
|
|
|
return;
|
|
|
|
case ND_NEG:
|
|
|
|
gen_expr(node->lhs);
|
|
|
|
printf(" neg %%rax\n");
|
|
|
|
return;
|
2020-09-26 02:59:56 +03:00
|
|
|
case ND_VAR:
|
|
|
|
gen_addr(node);
|
2020-09-26 04:15:32 +03:00
|
|
|
load(node->ty);
|
2020-09-26 02:59:56 +03:00
|
|
|
return;
|
2019-08-05 15:12:44 +03:00
|
|
|
case ND_DEREF:
|
|
|
|
gen_expr(node->lhs);
|
2020-09-26 04:15:32 +03:00
|
|
|
load(node->ty);
|
2019-08-05 15:12:44 +03:00
|
|
|
return;
|
|
|
|
case ND_ADDR:
|
|
|
|
gen_addr(node->lhs);
|
|
|
|
return;
|
2020-09-26 02:59:56 +03:00
|
|
|
case ND_ASSIGN:
|
|
|
|
gen_addr(node->lhs);
|
|
|
|
push();
|
|
|
|
gen_expr(node->rhs);
|
2020-08-27 15:04:17 +03:00
|
|
|
store(node->ty);
|
2020-09-26 02:59:56 +03:00
|
|
|
return;
|
2019-08-04 13:03:46 +03:00
|
|
|
case ND_FUNCALL: {
|
|
|
|
int nargs = 0;
|
|
|
|
for (Node *arg = node->args; arg; arg = arg->next) {
|
|
|
|
gen_expr(arg);
|
|
|
|
push();
|
|
|
|
nargs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = nargs - 1; i >= 0; i--)
|
2020-08-27 15:04:17 +03:00
|
|
|
pop(argreg64[i]);
|
2019-08-04 13:03:46 +03:00
|
|
|
|
2019-08-04 12:25:20 +03:00
|
|
|
printf(" mov $0, %%rax\n");
|
|
|
|
printf(" call %s\n", node->funcname);
|
|
|
|
return;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
2019-08-04 13:03:46 +03:00
|
|
|
}
|
2020-10-07 14:11:16 +03:00
|
|
|
|
|
|
|
gen_expr(node->rhs);
|
|
|
|
push();
|
|
|
|
gen_expr(node->lhs);
|
|
|
|
pop("%rdi");
|
|
|
|
|
|
|
|
switch (node->kind) {
|
|
|
|
case ND_ADD:
|
|
|
|
printf(" add %%rdi, %%rax\n");
|
|
|
|
return;
|
|
|
|
case ND_SUB:
|
|
|
|
printf(" sub %%rdi, %%rax\n");
|
|
|
|
return;
|
|
|
|
case ND_MUL:
|
|
|
|
printf(" imul %%rdi, %%rax\n");
|
|
|
|
return;
|
|
|
|
case ND_DIV:
|
|
|
|
printf(" cqo\n");
|
|
|
|
printf(" idiv %%rdi\n");
|
|
|
|
return;
|
|
|
|
case ND_EQ:
|
|
|
|
case ND_NE:
|
|
|
|
case ND_LT:
|
|
|
|
case ND_LE:
|
|
|
|
printf(" cmp %%rdi, %%rax\n");
|
|
|
|
|
|
|
|
if (node->kind == ND_EQ)
|
|
|
|
printf(" sete %%al\n");
|
|
|
|
else if (node->kind == ND_NE)
|
|
|
|
printf(" setne %%al\n");
|
|
|
|
else if (node->kind == ND_LT)
|
|
|
|
printf(" setl %%al\n");
|
|
|
|
else if (node->kind == ND_LE)
|
|
|
|
printf(" setle %%al\n");
|
|
|
|
|
|
|
|
printf(" movzb %%al, %%rax\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
error_tok(node->tok, "invalid expression");
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 02:50:44 +03:00
|
|
|
static void gen_stmt(Node *node) {
|
2020-10-07 14:12:57 +03:00
|
|
|
switch (node->kind) {
|
2020-10-07 06:47:09 +03:00
|
|
|
case ND_IF: {
|
|
|
|
int c = count();
|
|
|
|
gen_expr(node->cond);
|
|
|
|
printf(" cmp $0, %%rax\n");
|
|
|
|
printf(" je .L.else.%d\n", c);
|
|
|
|
gen_stmt(node->then);
|
|
|
|
printf(" jmp .L.end.%d\n", c);
|
|
|
|
printf(".L.else.%d:\n", c);
|
|
|
|
if (node->els)
|
|
|
|
gen_stmt(node->els);
|
|
|
|
printf(".L.end.%d:\n", c);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-04 11:35:53 +03:00
|
|
|
case ND_FOR: {
|
|
|
|
int c = count();
|
2019-08-04 11:24:03 +03:00
|
|
|
if (node->init)
|
|
|
|
gen_stmt(node->init);
|
2019-08-04 11:35:53 +03:00
|
|
|
printf(".L.begin.%d:\n", c);
|
|
|
|
if (node->cond) {
|
|
|
|
gen_expr(node->cond);
|
|
|
|
printf(" cmp $0, %%rax\n");
|
|
|
|
printf(" je .L.end.%d\n", c);
|
|
|
|
}
|
|
|
|
gen_stmt(node->then);
|
|
|
|
if (node->inc)
|
|
|
|
gen_expr(node->inc);
|
|
|
|
printf(" jmp .L.begin.%d\n", c);
|
|
|
|
printf(".L.end.%d:\n", c);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-04 07:38:41 +03:00
|
|
|
case ND_BLOCK:
|
|
|
|
for (Node *n = node->body; n; n = n->next)
|
|
|
|
gen_stmt(n);
|
|
|
|
return;
|
2020-10-07 14:12:57 +03:00
|
|
|
case ND_RETURN:
|
|
|
|
gen_expr(node->lhs);
|
2020-09-04 13:01:33 +03:00
|
|
|
printf(" jmp .L.return.%s\n", current_fn->name);
|
2020-10-07 14:12:57 +03:00
|
|
|
return;
|
|
|
|
case ND_EXPR_STMT:
|
2020-09-26 02:50:44 +03:00
|
|
|
gen_expr(node->lhs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
error_tok(node->tok, "invalid statement");
|
2020-09-26 02:50:44 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
// Assign offsets to local variables.
|
2020-09-04 11:58:53 +03:00
|
|
|
static void assign_lvar_offsets(Obj *prog) {
|
|
|
|
for (Obj *fn = prog; fn; fn = fn->next) {
|
|
|
|
if (!fn->is_function)
|
|
|
|
continue;
|
|
|
|
|
2020-09-04 13:01:33 +03:00
|
|
|
int offset = 0;
|
|
|
|
for (Obj *var = fn->locals; var; var = var->next) {
|
2020-09-26 04:15:32 +03:00
|
|
|
offset += var->ty->size;
|
2020-09-04 13:01:33 +03:00
|
|
|
var->offset = -offset;
|
|
|
|
}
|
|
|
|
fn->stack_size = align_to(offset, 16);
|
2020-10-07 14:12:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:43:21 +03:00
|
|
|
static void emit_data(Obj *prog) {
|
|
|
|
for (Obj *var = prog; var; var = var->next) {
|
|
|
|
if (var->is_function)
|
|
|
|
continue;
|
2020-10-07 14:12:19 +03:00
|
|
|
|
2020-09-05 02:43:21 +03:00
|
|
|
printf(" .data\n");
|
|
|
|
printf(" .globl %s\n", var->name);
|
|
|
|
printf("%s:\n", var->name);
|
2020-10-07 06:49:08 +03:00
|
|
|
|
|
|
|
if (var->init_data) {
|
|
|
|
for (int i = 0; i < var->ty->size; i++)
|
|
|
|
printf(" .byte %d\n", var->init_data[i]);
|
|
|
|
} else {
|
|
|
|
printf(" .zero %d\n", var->ty->size);
|
|
|
|
}
|
2020-09-05 02:43:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_text(Obj *prog) {
|
2020-09-04 11:58:53 +03:00
|
|
|
for (Obj *fn = prog; fn; fn = fn->next) {
|
|
|
|
if (!fn->is_function)
|
|
|
|
continue;
|
|
|
|
|
2020-09-04 13:01:33 +03:00
|
|
|
printf(" .globl %s\n", fn->name);
|
2020-09-04 11:58:53 +03:00
|
|
|
printf(" .text\n");
|
2020-09-04 13:01:33 +03:00
|
|
|
printf("%s:\n", fn->name);
|
|
|
|
current_fn = fn;
|
|
|
|
|
|
|
|
// Prologue
|
|
|
|
printf(" push %%rbp\n");
|
|
|
|
printf(" mov %%rsp, %%rbp\n");
|
|
|
|
printf(" sub $%d, %%rsp\n", fn->stack_size);
|
|
|
|
|
2020-09-04 07:39:48 +03:00
|
|
|
// Save passed-by-register arguments to the stack
|
|
|
|
int i = 0;
|
2020-08-27 15:04:17 +03:00
|
|
|
for (Obj *var = fn->params; var; var = var->next) {
|
|
|
|
if (var->ty->size == 1)
|
|
|
|
printf(" mov %s, %d(%%rbp)\n", argreg8[i++], var->offset);
|
|
|
|
else
|
|
|
|
printf(" mov %s, %d(%%rbp)\n", argreg64[i++], var->offset);
|
|
|
|
}
|
2020-09-04 07:39:48 +03:00
|
|
|
|
2020-09-04 13:01:33 +03:00
|
|
|
// Emit code
|
|
|
|
gen_stmt(fn->body);
|
|
|
|
assert(depth == 0);
|
|
|
|
|
|
|
|
// Epilogue
|
|
|
|
printf(".L.return.%s:\n", fn->name);
|
|
|
|
printf(" mov %%rbp, %%rsp\n");
|
|
|
|
printf(" pop %%rbp\n");
|
|
|
|
printf(" ret\n");
|
|
|
|
}
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
2020-09-05 02:43:21 +03:00
|
|
|
|
|
|
|
void codegen(Obj *prog) {
|
|
|
|
assign_lvar_offsets(prog);
|
|
|
|
emit_data(prog);
|
|
|
|
emit_text(prog);
|
|
|
|
}
|