jack-compiler/compiler.c

322 lines
8.0 KiB
C
Raw Normal View History

2020-12-20 13:58:10 -05:00
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
2020-12-22 13:45:12 -05:00
LINEBLOCK* compilestatements(SCOPE* s, CLASS* c, STATEMENT* sts);
2020-12-20 13:58:10 -05:00
int countparameters(EXPRESSIONLIST* params) {
int i = 0;
while(params != NULL) {
i++;
params = params->next;
}
return i;
}
int countlocalvars(VARDEC* decs) {
int i = 0;
while(decs != NULL) {
i++;
decs = decs->next;
}
return i;
}
char* dotlabel(char* n1, char* n2) {
int sz = (strlen(n1) + strlen(n2) + 2) * sizeof(char);
char* result = (char*)malloc(sz);
2020-12-21 13:05:49 -05:00
sprintf(result, "%s.%s", n1, n2);
2020-12-20 13:58:10 -05:00
return result;
}
2020-12-22 13:45:12 -05:00
char* mkcondlabel(SCOPE* s) {
char* label = dotlabel("cond-label", itoa(s->condlabelcount));
s->condlabelcount++;
return label;
2020-12-20 13:58:10 -05:00
}
LINE* onetoken(char* str) {
LINE* ln = mkline(1);
addtoken(ln, ezheapstr(str));
2020-12-21 13:05:49 -05:00
ln->next = NULL;
2020-12-20 13:58:10 -05:00
return ln;
}
2020-12-21 13:05:49 -05:00
LINE* mksimpleln(char** tokens, int count) {
2020-12-20 13:58:10 -05:00
LINE* ln = mkline(count);
for(int i = 0; i < count; i++)
addtoken(ln, ezheapstr(tokens[i]));
2020-12-21 13:21:37 -05:00
ln->next = NULL;
2020-12-20 13:58:10 -05:00
return ln;
}
LINE* mathopln(char op) {
if(op == '+')
return onetoken("add");
if(op == '-')
return onetoken("sub");
if(op == '=')
return onetoken("eq");
if(op == '>')
return onetoken("gt");
if(op == '<')
return onetoken("lt");
if(op == '|')
return onetoken("or");
if(op == '&')
return onetoken("and");
if(op == '/') {
char* tokens[] = { "call", "Math.divide", "2" };
2020-12-21 16:11:23 -05:00
return mksimpleln(tokens, strcount(tokens));
2020-12-20 13:58:10 -05:00
}
if(op == '*') {
char* tokens[] = { "call", "Math.multiply", "2" };
2020-12-21 16:11:23 -05:00
return mksimpleln(tokens, strcount(tokens));
2020-12-20 13:58:10 -05:00
}
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileexpression(SCOPE* s, TERM* e) {
LINEBLOCK* myblk;
LINEBLOCK* next = NULL;
2020-12-20 13:58:10 -05:00
if(e->type == intconstant) {
2020-12-21 13:21:37 -05:00
char* tokens[] = { "push", "constant", itoa(e->integer) };
2020-12-21 16:11:23 -05:00
myblk = mklnblk(mksimpleln(tokens, strcount(tokens)));
2020-12-20 13:58:10 -05:00
}
else if(e->type == unaryopterm) {
2020-12-21 13:05:49 -05:00
myblk = compileexpression(s, e->expression);
LINE* neg = onetoken("neg");
appendln(myblk, neg);
2020-12-20 13:58:10 -05:00
}
else if(e->type == innerexpression) {
2020-12-21 13:05:49 -05:00
myblk = compileexpression(s, e->expression);
2020-12-20 13:58:10 -05:00
}
2020-12-22 13:45:12 -05:00
else if(e->type == varname) {
eprintf("TO BE IMPLEMENTED\n");
exit(1);
}
2020-12-20 13:58:10 -05:00
else {
2020-12-21 16:11:23 -05:00
eprintf("Unsupported term yet %i\n", e->type);
2020-12-20 13:58:10 -05:00
exit(1);
}
2020-12-21 13:05:49 -05:00
if(e->next != NULL) {
next = compileexpression(s, e->next);
2020-12-21 13:21:37 -05:00
appendln(next, mathopln(e->op));
2020-12-21 13:05:49 -05:00
myblk = mergelnblks(myblk, next);
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
return myblk;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileparameters(SCOPE* s, EXPRESSIONLIST* params) {
LINEBLOCK* head = NULL;
while(params != NULL) {
head = mergelnblks(head, compileexpression(s, params->expression));
params = params->next;
2020-12-20 13:58:10 -05:00
}
return head;
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compilecallln(CLASS* c, SUBROUTCALL* call) {
LINE* ln = mkline(3);
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
addtoken(ln, ezheapstr("call"));
2020-12-20 13:58:10 -05:00
if(call->parentname != NULL)
2020-12-21 13:05:49 -05:00
addtoken(ln, dotlabel(call->parentname, call->name));
2020-12-20 13:58:10 -05:00
else
2020-12-21 13:05:49 -05:00
addtoken(ln, dotlabel(c->name, call->name));
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
addtoken(ln, itoa(countparameters(call->parameters)));
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
return mklnblk(ln);
}
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
// temporary ignore list for OS functions
2020-12-21 18:35:41 -05:00
char* ignoresubroutdecs[] = {
2020-12-21 14:49:37 -05:00
"printInt", "void", "peek", "int"
2020-12-21 13:05:49 -05:00
};
2020-12-21 18:35:41 -05:00
int ignorecount = sizeof(ignoresubroutdecs) / sizeof(char*);
2020-12-21 13:05:49 -05:00
LINEBLOCK* compilesubroutcall(SCOPE* s, CLASS* c, SUBROUTCALL* call) {
LINEBLOCK* block = compilecallln(c, call);
if(call->parameters != NULL)
block = mergelnblks(compileparameters(s, call->parameters), block);
// void functions always return 0
// therefore must be thrown away
// gambiarra
char* type = NULL;
for(int i = 0; i < ignorecount; i += 2) {
2020-12-21 18:35:41 -05:00
if(!strcmp(call->name, ignoresubroutdecs[i])) {
type = ignoresubroutdecs[i+1];
2020-12-21 13:05:49 -05:00
break;
}
}
if(type == NULL)
2020-12-21 18:35:41 -05:00
type = getsubroutdecfromcall(s, call)->type;
2020-12-21 13:05:49 -05:00
if(!strcmp(type, "void")) {
char* tokens[] = { "pop", "temp", "0" };
appendln(block, mksimpleln(tokens, sizeof(tokens) / sizeof(char*)));
}
return block;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileret(SCOPE* s, TERM* e) {
LINE* ret = onetoken("return");
LINEBLOCK* block = mklnblk(ret);
2020-12-21 18:35:41 -05:00
// void subroutdecs return 0
2020-12-20 13:58:10 -05:00
if(e == NULL) {
2020-12-21 13:05:49 -05:00
char* tokens[] = { "push", "constant", "0" };
2020-12-22 13:45:12 -05:00
appendlnbefore(block, mksimpleln(tokens, strcount(tokens)));
2020-12-21 13:05:49 -05:00
} else
block = mergelnblks(compileexpression(s, e), block);
return block;
2020-12-20 13:58:10 -05:00
}
2020-12-22 13:45:12 -05:00
LINEBLOCK* compileif(SCOPE* s, CLASS* c, IFSTATEMENT* st) {
LINEBLOCK* block = compileexpression(s, st->base->expression);
appendln(block, onetoken("not"));
char* label1 = mkcondlabel(s);
char* ifgoto[] = { "if-goto", label1 };
appendln(block, mksimpleln(ifgoto, strcount(ifgoto)));
block = mergelnblks(block, compilestatements(s, c, st->base->statements));
char* label2;
bool haselse = st->elsestatements != NULL;
if(haselse) {
char* label2 = mkcondlabel(s);
char* gotoln[] = { "goto", label2 };
appendln(block, mksimpleln(gotoln, strcount(gotoln)));
2020-12-20 13:58:10 -05:00
}
2020-12-22 13:45:12 -05:00
char* label1ln[] = { "label", label1 };
appendln(block, mksimpleln(label1ln, strcount(label1ln)));
if(haselse) {
block = mergelnblks(block, compilestatements(s, c, st->elsestatements));
char* label2ln[] = { "label", label2 };
appendln(block, mksimpleln(label2ln, strcount(label2ln)));
}
return block;
}
LINEBLOCK* compilewhile(SCOPE* s, CLASS* c, CONDSTATEMENT* w) {
LINEBLOCK* block = compileexpression(s, w->expression);
char* label1 = mkcondlabel(s);
char* label1ln[] = { "label", label1 };
appendlnbefore(block, mksimpleln(label1ln, strcount(label1ln)));
appendln(block, onetoken("not"));
char* label2 = mkcondlabel(s);
char* ifgoto[] = { "if-goto", label2 };
appendln(block, mksimpleln(ifgoto, strcount(ifgoto)));
block = mergelnblks(block, compilestatements(s, c, w->statements));
char* gotoln[] = { "goto", label1 };
appendln(block, mksimpleln(gotoln, strcount(gotoln)));
char* label2ln[] = { "label", label2 };
appendln(block, mksimpleln(label2ln, strcount(label2ln)));
return block;
}
LINEBLOCK* compilelet(SCOPE* s, CLASS* c, LETSTATEMENT* l) {
}
LINEBLOCK* compilestatement(SCOPE* s, CLASS* c, STATEMENT* st) {
if(st->type == dostatement) return compilesubroutcall(s, c, st->dostatement);
if(st->type == returnstatement) return compileret(s, st->retstatement);
if(st->type == ifstatement) return compileif(s, c, st->ifstatement);
if(st->type == whilestatement) return compilewhile(s, c, st->whilestatement);
if(st->type == letstatement) return compilelet(s, c, st->letstatement);
eprintf("UNSUPPORTED type %i\n", st->type);
exit(1);
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compilestatements(SCOPE* s, CLASS* c, STATEMENT* sts) {
LINEBLOCK* head = NULL;
while(sts != NULL) {
head = mergelnblks(head, compilestatement(s, c, sts));
2020-12-20 13:58:10 -05:00
sts = sts->next;
}
return head;
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compilefunbody(SCOPE* s, CLASS* c, SUBROUTBODY* b) {
2020-12-20 13:58:10 -05:00
// missing scope and vardecs handling
2020-12-21 13:05:49 -05:00
LINEBLOCK* head = compilestatements(s, c, b->statements);
2020-12-20 13:58:10 -05:00
return head;
}
2020-12-21 18:35:41 -05:00
LINEBLOCK* compilefundec(SCOPE* s, CLASS* c, SUBROUTDEC* f) {
2020-12-21 13:05:49 -05:00
LINE* label = mkline(3);
addtoken(label, ezheapstr("function"));
2020-12-22 13:45:12 -05:00
addtoken(label, dotlabel(c->name, f->name));
2020-12-21 13:05:49 -05:00
addtoken(label, itoa(countlocalvars(f->body->vardecs)));
label->next = NULL;
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
if(f->body->statements != NULL) {
LINEBLOCK* body = compilefunbody(s, c, f->body);
appendlnbefore(body, label);
return body;
}
else
return mklnblk(label);
2020-12-20 13:58:10 -05:00
}
2020-12-21 18:35:41 -05:00
LINEBLOCK* compilesubroutdec(SCOPE* s, CLASS* c, SUBROUTDEC* sd) {
2020-12-20 13:58:10 -05:00
// 'this' and arguments are pushed by caller
// Must have a 'return' at the end
// Label names must have class name too (see mapping)
// types: method, function, constructor
// must switch all of these
if(sd->subroutclass == function)
return compilefundec(s, c, sd);
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileclass(COMPILER* c, CLASS* class) {
2020-12-20 13:58:10 -05:00
SCOPE* topscope = mkscope(c->globalscope);
addclassvardecs(topscope, class->vardecs);
2020-12-21 18:35:41 -05:00
addsubroutdecs(topscope, class->subroutdecs);
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
LINEBLOCK* output = NULL;
2020-12-21 18:35:41 -05:00
SUBROUTDEC* curr = class->subroutdecs;
2020-12-21 13:05:49 -05:00
while(curr != NULL) {
2020-12-21 18:35:41 -05:00
output = mergelnblks(output, compilesubroutdec(topscope, class, curr));
2020-12-21 13:05:49 -05:00
curr = curr->next;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
return output;
2020-12-20 13:58:10 -05:00
}
void compile(COMPILER* c) {
2020-12-21 13:05:49 -05:00
LINEBLOCK* output = NULL;
CLASS* curr = c->globalscope->classes;
while(curr != NULL) {
output = mergelnblks(output, compileclass(c, curr));
curr = curr->next;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
c->output = output;
2020-12-20 13:58:10 -05:00
}
COMPILER* mkcompiler(CLASS* classes) {
COMPILER* c = (COMPILER*)malloc(sizeof(COMPILER));
c->globalscope = mkscope(NULL);
addclasses(c->globalscope, classes);
return c;
}