Commit 56e0cd8a authored by Akshay Yeluri's avatar Akshay Yeluri
Browse files

Implemented stage 7, added the prime-generating program as a test

parent aafe6666
Showing with 30 additions and 2 deletions
+30 -2
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "compile.h" #include "compile.h"
static int nextLabel=0;
/** /**
* Compiles the provided AST printing the resulting ASM to * Compiles the provided AST printing the resulting ASM to
...@@ -48,6 +49,11 @@ bool compile_ast(node_t *node) { ...@@ -48,6 +49,11 @@ bool compile_ast(node_t *node) {
printf(" cqto\n"); printf(" cqto\n");
printf(" idivq %%rcx\n"); printf(" idivq %%rcx\n");
return true; return true;
case '>':
case '<':
case '=':
printf(" cmp %%rax, %%rcx\n");
return true;
default: default:
return false; return false;
} }
...@@ -67,12 +73,34 @@ bool compile_ast(node_t *node) { ...@@ -67,12 +73,34 @@ bool compile_ast(node_t *node) {
} }
else if (node->type == LABEL) { else if (node->type == LABEL) {
label_node_t *p = (label_node_t*)node; label_node_t *p = (label_node_t*)node;
printf("%s:\n", p->label); printf("L%s:\n", p->label);
return true; return true;
} }
else if (node->type == GOTO) { else if (node->type == GOTO) {
goto_node_t *p = (goto_node_t*)node; goto_node_t *p = (goto_node_t*)node;
printf("jmp %s\n", p->label); printf(" jmp L%s\n", p->label);
return true;
}
else if (node->type == COND) {
cond_node_t *cond = (cond_node_t *)node;
if (!compile_ast(cond->cond)) { return false; }
binary_node_t *bin = (binary_node_t *)cond->cond;
char op = bin->op;
switch (op) {
case '>':
printf(" jng L%d\n", nextLabel);
break;
case '<':
printf(" jnl L%d\n", nextLabel);
break;
case '=':
printf(" jne L%d\n", nextLabel);
break;
default:
return false;
}
if (!compile_ast(cond->if_branch)) { return false; }
printf("L%d:\n", nextLabel++);
return true; return true;
} }
return false; return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment