vm-translator/translator.c

606 lines
14 KiB
C
Raw Normal View History

2020-10-30 13:06:02 -04:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "translator.h"
2020-10-31 21:53:11 -04:00
#include "templates.h"
2020-11-17 12:12:39 -05:00
#include "util.h"
2020-10-31 21:53:11 -04:00
2020-11-19 06:10:51 -05:00
void pushtoclean(TRANSLATOR* t, char* topush) {
int nextsz = sizeof(char*)*(t->toclean->count+1);
if(nextsz >= t->toclean->size) {
t->toclean->size = nextsz * 2;
t->toclean->items = realloc(t->toclean->items, t->toclean->size);
2020-10-31 21:53:11 -04:00
}
2020-11-19 06:10:51 -05:00
t->toclean->items[t->toclean->count] = topush;
t->toclean->count++;
2020-10-31 21:53:11 -04:00
}
2020-11-19 06:10:51 -05:00
void freetoclean(TRANSLATOR* t) {
for(int i = 0; i < t->toclean->count; i++)
free(t->toclean->items[i]);
free(t->toclean->items);
2020-11-01 08:53:20 -05:00
free(t->toclean);
2020-10-31 21:53:11 -04:00
}
2020-10-30 13:06:02 -04:00
2020-11-19 06:10:51 -05:00
void freetranslator(TRANSLATOR* t) {
free(t->asmlns->items);
2020-11-17 12:12:39 -05:00
free(t->asmlns);
2020-11-01 08:53:20 -05:00
freetoclean(t);
free(t);
2020-10-31 11:40:06 -04:00
}
2020-11-19 06:10:51 -05:00
void printasmlns(TRANSLATOR* t, FILE* stream) {
for(int i = 0; i < t->asmlns->count; i++)
fprintf(stream, "%s\n", t->asmlns->items[i]);
2020-11-01 08:53:20 -05:00
}
2020-11-19 06:10:51 -05:00
char* heapstrtoclean(TRANSLATOR* t, const char* input) {
2020-11-17 12:12:39 -05:00
char* newstr = heapstr(input, strlen(input));
2020-11-01 08:53:20 -05:00
pushtoclean(t, newstr);
2020-10-30 13:06:02 -04:00
return newstr;
}
2020-11-19 06:10:51 -05:00
char* switchseg(TRANSLATOR* t, LINE* ln) {
2020-10-30 13:06:02 -04:00
char* seg = ln->tokens[1];
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "local"))
2020-11-17 12:12:39 -05:00
return heapstrtoclean(t, "@LCL");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "argument"))
2020-11-17 12:12:39 -05:00
return heapstrtoclean(t, "@ARG");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "this"))
2020-11-17 12:12:39 -05:00
return heapstrtoclean(t, "@THIS");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "that"))
2020-11-17 12:12:39 -05:00
return heapstrtoclean(t, "@THAT");
fprintf(stderr, "Unrecognized segment '%s'; file %s.vm, line %i\n", t->fname, seg, ln->truen);
2020-10-31 19:25:00 -04:00
exit(1);
2020-10-30 13:06:02 -04:00
}
2020-11-19 06:10:51 -05:00
int lnlen(int* out, LINE* ln) {
2020-11-01 10:40:43 -05:00
int len = 0;
for(int i = 0; i < ln->tokenscount; i++) {
int l = strlen(ln->tokens[i]);
out[i] = l;
len += l;
}
return len;
}
2020-10-30 13:06:02 -04:00
// produce comment as follows:
// pop/push segment i
2020-11-19 06:10:51 -05:00
char* mkcom(TRANSLATOR* t, LINE* ln) {
2020-11-01 10:40:43 -05:00
int lens[ln->tokenscount];
int comlen = sizeof(char) * lnlen(lens, ln) + ln->tokenscount + 3;
2020-10-30 13:06:02 -04:00
char* comment = (char*)malloc(comlen);
2020-11-01 10:40:43 -05:00
comment[0] = '/';
comment[1] = '/';
char* tmp = comment + sizeof(char)*2;
for(int i = 0; i < ln->tokenscount; i++) {
tmp[0] = ' ';
tmp += sizeof(char);
strcpy(tmp, ln->tokens[i]);
tmp += sizeof(char)*lens[i];
}
tmp[0] = '\0';
2020-11-01 08:53:20 -05:00
pushtoclean(t, comment);
2020-10-30 13:06:02 -04:00
return comment;
}
2020-11-19 06:10:51 -05:00
void checknumber(TRANSLATOR* t, LINE* ln, int indlen, char* name, char* n) {
2020-11-01 10:40:43 -05:00
for(int i = 0; i < indlen; i++)
2020-11-17 12:12:39 -05:00
if(!isdigit(n[i])) {
fprintf(stderr, "Invalid %s '%s'; file %s.vm, line %i\n", t->fname, name, n, ln->truen);
2020-10-30 13:06:02 -04:00
exit(1);
}
}
2020-11-19 06:10:51 -05:00
void checknargs(TRANSLATOR* t, LINE* ln, int nargslen) {
2020-11-17 12:12:39 -05:00
checknumber(t, ln, nargslen, "argument number", ln->tokens[2]);
}
2020-11-19 06:10:51 -05:00
void checknlocals(TRANSLATOR* t, LINE* ln, int nlocalslen) {
2020-11-17 12:12:39 -05:00
checknumber(t, ln, nlocalslen, "local variable number", ln->tokens[2]);
}
2020-11-19 06:10:51 -05:00
void checkind(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-17 12:12:39 -05:00
checknumber(t, ln, indlen, "index", ln->tokens[2]);
}
2020-11-19 06:10:51 -05:00
void checkinfun(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
if(t->funcount <= 0) {
fprintf(stderr, "Instruction should be part of a function; file %s.vm, line %i\n", t->fname, ln->truen);
2020-11-01 16:57:56 -05:00
exit(1);
}
2020-11-17 12:12:39 -05:00
}
2020-11-19 06:10:51 -05:00
char* mkspeciallab(TRANSLATOR* t, LINE* ln, char* suffix, int* ind) {
2020-11-17 12:12:39 -05:00
checkinfun(t, ln);
(*ind)++;
int sz = (t->lastfunlen + countplaces(*ind) + strlen(suffix) + 3) * sizeof(char);
char* lab = (char*)malloc(sz);
snprintf(lab, sz, "%s$%s.%i", t->lastfun, suffix, (*ind));
pushtoclean(t, lab);
return lab;
}
2020-11-19 06:10:51 -05:00
char* mkcmplab(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
return mkspeciallab(t, ln, "cmp", &(t->cmpind));
}
2020-11-19 06:10:51 -05:00
char* mkretlab(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
return mkspeciallab(t, ln, "ret", &(t->retind));
2020-11-01 16:57:56 -05:00
}
2020-11-19 06:10:51 -05:00
char* mkind(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-17 12:12:39 -05:00
checkind(t, ln, indlen);
2020-11-01 10:40:43 -05:00
int newsz = sizeof(char) * (indlen + 2);
2020-10-30 13:06:02 -04:00
char* newind = (char*)malloc(newsz);
2020-10-31 21:53:11 -04:00
snprintf(newind, newsz, "@%s", ln->tokens[2]);
2020-11-01 08:53:20 -05:00
pushtoclean(t, newind);
2020-10-30 13:06:02 -04:00
return newind;
}
2020-11-19 06:10:51 -05:00
char* atlab(TRANSLATOR* t, char* label, int labellen) {
2020-11-01 16:57:56 -05:00
int newsz = sizeof(char) * (labellen + 2);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", label);
pushtoclean(t, newind);
return newind;
}
2020-11-19 06:10:51 -05:00
char* atn(TRANSLATOR* t, int n) {
2020-11-17 12:12:39 -05:00
int newsz = sizeof(char) * (countplaces(n) + 2);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", n);
pushtoclean(t, newind);
return newind;
}
2020-11-19 06:10:51 -05:00
char* mklab(TRANSLATOR* t, char* label, int labellen) {
2020-11-01 16:57:56 -05:00
int newsz = sizeof(char) * (labellen + 3);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "(%s)", label);
pushtoclean(t, newind);
return newind;
}
2020-11-19 06:10:51 -05:00
char* mkgotolab(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
int sz = sizeof(char) * (t->lastfunlen + strlen(ln->tokens[1]) + 3);
char* lab = (char*)malloc(sz);
snprintf(lab, sz, "@%s$%s", t->lastfun, ln->tokens[1]);
pushtoclean(t, lab);
return lab;
}
2020-11-19 06:10:51 -05:00
char* mkstatind(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-17 12:12:39 -05:00
checkind(t, ln, indlen);
2020-11-14 10:31:57 -05:00
int newsz = sizeof(char) * (t->fnamelen + indlen + 3);
2020-10-31 19:25:00 -04:00
char* newind = (char*)malloc(newsz);
2020-11-01 08:53:20 -05:00
snprintf(newind, newsz, "@%s.%s", t->fname, ln->tokens[2]);
pushtoclean(t, newind);
2020-10-31 19:25:00 -04:00
return newind;
}
2020-11-19 06:10:51 -05:00
char* mktempind(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-17 12:12:39 -05:00
checkind(t, ln, indlen);
2020-10-31 21:53:11 -04:00
int intind = atoi(ln->tokens[2]);
2020-11-01 10:40:43 -05:00
int newsz = sizeof(char) * (indlen + 3);
2020-10-31 19:25:00 -04:00
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", intind+5);
2020-11-01 08:53:20 -05:00
pushtoclean(t, newind);
2020-10-31 19:25:00 -04:00
return newind;
}
2020-11-19 06:10:51 -05:00
char* mkpointerind(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-01 10:40:43 -05:00
if(indlen > 1) {
2020-11-17 12:12:39 -05:00
fprintf(stderr, "Invalid index '%s'; file %s.vm, line %i\n", t->fname, ln->tokens[2], ln->truen);
2020-11-01 10:40:43 -05:00
exit(1);
}
char* ptr;
switch(ln->tokens[2][0]) {
case '0':
ptr = "THIS";
break;
case '1':
ptr = "THAT";
break;
default:
2020-11-17 12:12:39 -05:00
fprintf(stderr, "Invalid index '%s'; file %s.vm, line %i\n", t->fname, ln->tokens[2], ln->truen);
2020-11-01 10:40:43 -05:00
exit(1);
}
int newsz = sizeof(char) * 6;
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ptr);
pushtoclean(t, newind);
return newind;
}
2020-11-19 06:10:51 -05:00
void checkasmsize(TRANSLATOR* t, int toadd) {
int targ = sizeof(char*)*(t->asmlns->count+toadd);
if(targ >= t->asmlns->size) {
t->asmlns->size = targ * 2;
t->asmlns->items = (char**)realloc(t->asmlns->items, t->asmlns->size);
2020-10-30 13:06:02 -04:00
}
}
2020-11-19 06:10:51 -05:00
void checkopamnt(TRANSLATOR* t, int amnt, LINE* ln) {
2020-10-31 11:40:06 -04:00
if(ln->tokenscount < 2) {
2020-11-17 12:12:39 -05:00
fprintf(stderr, "Missing memory segment; file %s.vm, line %i\n", t->fname, ln->truen);
2020-10-31 11:40:06 -04:00
exit(1);
}
if(amnt > 2)
if(ln->tokenscount < 3) {
2020-11-17 12:12:39 -05:00
fprintf(stderr, "Missing operation index; file %s.vm, line %i\n", t->fname, ln->truen);
2020-10-31 11:40:06 -04:00
exit(1);
}
}
2020-11-19 06:10:51 -05:00
void checklab(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
if(ln->tokenscount < 2) {
fprintf(stderr, "Expected label; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1);
}
}
2020-11-01 10:40:43 -05:00
2020-11-19 06:10:51 -05:00
void checkfun(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
if(ln->tokenscount < 2) {
fprintf(stderr, "Expected function; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1);
}
if(ln->tokenscount < 3) {
fprintf(stderr, "Expected argument amount; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1);
}
}
2020-11-19 06:10:51 -05:00
void addasm(TRANSLATOR* t, char** insts, int instcount) {
2020-11-17 12:12:39 -05:00
checkasmsize(t, instcount);
2020-11-01 10:40:43 -05:00
2020-10-31 21:53:11 -04:00
for(int i = 0; i < instcount; i++) {
2020-11-19 06:10:51 -05:00
t->asmlns->items[t->asmlns->count] = insts[i];
t->asmlns->count++;
2020-10-31 21:53:11 -04:00
}
}
2020-10-30 13:06:02 -04:00
2020-11-19 06:10:51 -05:00
void addasmlns(TRANSLATOR* t, LINE* ln, char** insts, int instcount) {
2020-11-17 12:12:39 -05:00
// instruction comment
insts[0] = mkcom(t, ln);
addasm(t, insts, instcount);
}
2020-11-19 06:10:51 -05:00
void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, char** insts) {
2020-10-30 13:06:02 -04:00
// @segment
2020-11-01 08:53:20 -05:00
insts[1] = switchseg(t, ln);
2020-10-30 13:06:02 -04:00
// D=M
2020-11-17 12:12:39 -05:00
insts[2] = heapstrtoclean(t, "D=M");
2020-10-30 13:06:02 -04:00
// @i
2020-11-01 08:53:20 -05:00
insts[3] = mkind(t, ln, indlen);
2020-10-30 13:06:02 -04:00
}
2020-11-19 06:10:51 -05:00
void pushcons(TRANSLATOR* t, LINE* ln, int indlen) {
2020-10-31 18:00:09 -04:00
// @i
2020-11-01 08:53:20 -05:00
tpushcons[1] = mkind(t, ln, indlen);
2020-10-31 18:00:09 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpushcons, TPUSHCONSN);
2020-10-31 18:00:09 -04:00
}
2020-11-19 06:10:51 -05:00
void pushstat(TRANSLATOR* t, LINE* ln, int indlen) {
2020-10-31 19:25:00 -04:00
// @fname.i
2020-11-01 08:53:20 -05:00
tpushstat[1] = mkstatind(t, ln, indlen);
2020-10-31 19:25:00 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpushstat, TPUSHSTATN);
2020-10-31 19:25:00 -04:00
}
2020-11-19 06:10:51 -05:00
void pushtemp(TRANSLATOR* t, LINE* ln, int indlen) {
2020-10-31 21:53:11 -04:00
// @5+i
2020-11-01 08:53:20 -05:00
tpushtemp[1] = mktempind(t, ln, indlen);
2020-10-31 19:25:00 -04:00
2020-11-01 10:40:43 -05:00
addasmlns(t, ln, tpushtemp, TPUSHTEMPN);
2020-10-31 19:25:00 -04:00
}
2020-11-19 06:10:51 -05:00
void pushpointer(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-01 10:40:43 -05:00
// @THIS/@THAT
tpushpointer[1] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN);
}
2020-10-31 19:25:00 -04:00
2020-11-19 06:10:51 -05:00
void push(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-01 08:53:20 -05:00
startpoppush(t, ln, indlen, tpush);
2020-10-31 19:25:00 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpush, TPUSHN);
2020-10-31 19:25:00 -04:00
}
2020-11-19 06:10:51 -05:00
void popstat(TRANSLATOR* t, LINE* ln, int indlen) {
2020-10-31 21:53:11 -04:00
// @fname.i
2020-11-01 10:40:43 -05:00
tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen);
2020-10-30 13:06:02 -04:00
2020-10-31 21:53:11 -04:00
// M=D
2020-11-17 12:12:39 -05:00
tpopstat[TPOPSTATN-1] = heapstrtoclean(t, "M=D");
2020-10-30 13:06:02 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpopstat, TPOPSTATN);
2020-10-30 13:06:02 -04:00
}
2020-11-19 06:10:51 -05:00
void poptemp(TRANSLATOR* t, LINE* ln, int indlen) {
2020-10-31 19:25:00 -04:00
// @5+i
2020-11-01 10:40:43 -05:00
tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen);
2020-10-31 19:25:00 -04:00
// M=D
2020-11-17 12:12:39 -05:00
tpoptemp[TPOPTEMPN-1] = heapstrtoclean(t, "M=D");
2020-10-31 19:25:00 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpoptemp, TPOPTEMPN);
2020-10-31 19:25:00 -04:00
}
2020-11-19 06:10:51 -05:00
void poppointer(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-01 10:40:43 -05:00
// @THIS/@THAT
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen);
2020-11-01 17:39:27 -05:00
// M=D
2020-11-17 12:12:39 -05:00
tpoppointer[TPOPPOINTERN-1] = heapstrtoclean(t, "M=D");
2020-11-01 17:39:27 -05:00
2020-11-01 10:40:43 -05:00
addasmlns(t, ln, tpoppointer, TPOPPOINTERN);
}
2020-10-30 13:06:02 -04:00
2020-11-19 06:10:51 -05:00
void pop(TRANSLATOR* t, LINE* ln, int indlen) {
2020-11-01 08:53:20 -05:00
startpoppush(t, ln, indlen, tpop);
2020-10-30 13:06:02 -04:00
2020-11-01 08:53:20 -05:00
addasmlns(t, ln, tpop, TPOPN);
2020-10-30 13:06:02 -04:00
}
2020-11-19 06:10:51 -05:00
void arith(TRANSLATOR* t, LINE* ln, char* op) {
2020-11-17 12:12:39 -05:00
tarith[TARITHN-1] = heapstrtoclean(t, op);
2020-11-01 16:57:56 -05:00
addasmlns(t, ln, tarith, TARITHN);
}
2020-11-19 06:10:51 -05:00
void comp(TRANSLATOR* t, LINE* ln, char* op) {
2020-11-01 16:57:56 -05:00
char* label = mkcmplab(t, ln);
int labellen = strlen(label);
// @label
tcomp[TCOMPN-6] = atlab(t, label, labellen);
// D;J(op)
2020-11-17 12:12:39 -05:00
int opsz = sizeof(char) * 6;
2020-11-01 16:57:56 -05:00
char* trueop = (char*)malloc(opsz);
snprintf(trueop, opsz, "D;J%s", op);
tcomp[TCOMPN-5] = trueop;
2020-11-14 10:31:57 -05:00
pushtoclean(t, trueop);
2020-11-01 16:57:56 -05:00
// (label)
tcomp[TCOMPN-1] = mklab(t, label, labellen);
addasmlns(t, ln, tcomp, TCOMPN);
};
2020-11-19 06:10:51 -05:00
void label(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checklab(t, ln);
2020-11-14 10:31:57 -05:00
2020-11-17 12:12:39 -05:00
// (funcname$label)
checkinfun(t, ln);
int sz = (t->lastfunlen + strlen(ln->tokens[1]) + 4) * sizeof(char);
char* lab = (char*)malloc(sz);
snprintf(lab, sz, "(%s$%s)", t->lastfun, ln->tokens[1]);
pushtoclean(t, lab);
tlabel[TLABELN-1] = lab;
2020-11-14 10:31:57 -05:00
addasmlns(t, ln, tlabel, TLABELN);
}
2020-11-19 06:10:51 -05:00
void mygoto(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checklab(t, ln);
2020-11-14 10:31:57 -05:00
// @label
2020-11-17 12:12:39 -05:00
tgoto[TGOTON-2] = mkgotolab(t, ln);
2020-11-14 10:31:57 -05:00
addasmlns(t, ln, tgoto, TGOTON);
}
2020-11-19 06:10:51 -05:00
void ifgoto(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checklab(t, ln);
2020-11-14 10:31:57 -05:00
// @label
2020-11-17 12:12:39 -05:00
tifgoto[TIFGOTON-2] = mkgotolab(t, ln);
2020-11-14 10:31:57 -05:00
addasmlns(t, ln, tifgoto, TIFGOTON);
}
2020-11-19 06:10:51 -05:00
int pushframe(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) {
2020-11-17 12:12:39 -05:00
tcallstart[1] = atlab(t, retlab, retlablen);
addasmlns(t, ln, tcallstart, TCALLSTARTN);
for(int i = 0; i < TFRAMEVARSN; i++) {
tcallpush[0] = tframevars[i];
addasm(t, tcallpush, TCALLPUSHN);
}
return TFRAMEVARSN + 1;
}
2020-11-19 06:10:51 -05:00
void call(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checkfun(t, ln);
// return label
char* retlab = mkretlab(t, ln);
int retlablen = strlen(retlab);
// push frame
int framesize = pushframe(t, ln, retlab, retlablen);
// setting ARG
int nargslen = strlen(ln->tokens[2]);
checknargs(t, ln, nargslen);
int nargs = atoi(ln->tokens[2]);
tcallsetarg[TCALLSETARGN-4] = atn(t, nargs + framesize);
addasm(t, tcallsetarg, TCALLSETARGN);
// jmp
int jmplen = strlen(ln->tokens[1]);
tcalljmp[TCALLJMPN-3] = atlab(t, ln->tokens[1], jmplen);
tcalljmp[TCALLJMPN-1] = mklab(t, retlab, retlablen);
addasm(t, tcalljmp, TCALLJMPN);
}
2020-11-19 06:10:51 -05:00
void function(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
if(!t->returned) {
fprintf(stderr, "Last function did not return; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1);
}
checkfun(t, ln);
t->lastfun = ln->tokens[1];
int funlen = strlen(ln->tokens[1]);
t->lastfunlen = funlen;
t->funcount++;
t->retind = 0;
t->cmpind = 0;
// (funcname)
tfunction[1] = mklab(t, ln->tokens[1], funlen);
addasmlns(t, ln, tfunction, TFUNCTIONN);
// repeat nVars times:
int nlocalslen = strlen(ln->tokens[2]);
checknlocals(t, ln, nlocalslen);
int nlocals = atoi(ln->tokens[2]);
for(int i = 0; i < nlocals; i++) {
addasm(t, tfunctionpush, TFUNCTIONPUSHN);
}
}
2020-11-19 06:10:51 -05:00
void myreturn(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
addasmlns(t, ln, tstartreturn, TSTARTRETURNN);
for(int i = TFRAMEVARSN-1; i >= 0; i--) {
tretpop[TRETPOPN-2] = tframevars[i];
addasm(t, tretpop, TRETPOPN);
}
addasm(t, tendreturn, TENDRETURNN);
}
2020-11-19 06:10:51 -05:00
void switchpush(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checkopamnt(t, 3, ln);
2020-11-01 10:40:43 -05:00
char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]);
if(!strcmp(seg, "constant"))
pushcons(t, ln, indlen);
else if(!strcmp(seg, "static"))
pushstat(t, ln, indlen);
else if(!strcmp(seg, "temp"))
pushtemp(t, ln, indlen);
else if(!strcmp(seg, "pointer"))
pushpointer(t, ln, indlen);
else
push(t, ln, indlen);
}
2020-10-31 21:53:11 -04:00
2020-11-19 06:10:51 -05:00
void switchpop(TRANSLATOR* t, LINE* ln) {
2020-11-17 12:12:39 -05:00
checkopamnt(t, 3, ln);
2020-11-01 10:40:43 -05:00
char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]);
if(!strcmp(seg, "static"))
popstat(t, ln, indlen);
else if(!strcmp(seg, "temp"))
poptemp(t, ln, indlen);
else if(!strcmp(seg, "pointer"))
poppointer(t, ln, indlen);
else
pop(t, ln, indlen);
}
2020-10-31 21:53:11 -04:00
2020-11-19 06:10:51 -05:00
void switchop(TRANSLATOR* t, LINE* ln) {
2020-11-01 10:40:43 -05:00
char* op = ln->tokens[0];
2020-11-19 06:10:51 -05:00
bool returned = false;
2020-10-31 21:53:11 -04:00
2020-11-01 10:40:43 -05:00
if(!strcmp(op, "push"))
switchpush(t, ln);
else if(!strcmp(op, "pop"))
switchpop(t, ln);
2020-11-01 16:57:56 -05:00
else if(!strcmp(op, "add"))
2020-11-14 10:31:57 -05:00
arith(t, ln, "M=D+M");
2020-11-01 16:57:56 -05:00
else if(!strcmp(op, "sub"))
arith(t, ln, "M=M-D");
else if(!strcmp(op, "neg"))
addasmlns(t, ln, tneg, TNEGN);
else if(!strcmp(op, "eq"))
comp(t, ln, "EQ");
else if(!strcmp(op, "gt"))
comp(t, ln, "LT");
2020-11-01 17:39:27 -05:00
else if(!strcmp(op, "lt"))
comp(t, ln, "GT");
2020-11-01 16:57:56 -05:00
else if(!strcmp(op, "and"))
2020-11-14 10:31:57 -05:00
arith(t, ln, "M=D&M");
2020-11-01 16:57:56 -05:00
else if(!strcmp(op, "or"))
2020-11-14 10:31:57 -05:00
arith(t, ln, "M=D|M");
2020-11-01 16:57:56 -05:00
else if(!strcmp(op, "not"))
addasmlns(t, ln, tnot, TNOTN);
2020-11-14 10:31:57 -05:00
else if(!strcmp(op, "label"))
label(t, ln);
else if(!strcmp(op, "goto"))
mygoto(t, ln);
else if(!strcmp(op, "if-goto"))
ifgoto(t, ln);
2020-11-17 12:12:39 -05:00
else if(!strcmp(op, "return")) {
myreturn(t, ln);
2020-11-19 06:10:51 -05:00
returned = true;
2020-11-17 12:12:39 -05:00
}
else if(!strcmp(op, "function"))
function(t, ln);
else if(!strcmp(op, "call"))
call(t, ln);
2020-11-01 10:40:43 -05:00
else {
2020-11-17 12:12:39 -05:00
fprintf(stderr, "Unrecognized operation '%s'; file %s.vm, line %i\n", t->fname, op, ln->truen);
2020-11-01 10:40:43 -05:00
exit(1);
2020-10-31 21:53:11 -04:00
}
2020-11-17 12:12:39 -05:00
t->returned = returned;
2020-10-30 13:06:02 -04:00
}
2020-11-19 06:10:51 -05:00
void translate(TRANSLATOR* t) {
2020-11-01 08:53:20 -05:00
for(int i = 0; i < t->lns->count; i++)
switchop(t, t->lns->lns[i]);
2020-11-17 12:12:39 -05:00
if(!t->returned) {
fprintf(stderr, "Expected return before end of file; file %s.vm, line %i\n", t->fname, t->lns->count-1);
exit(1);
}
2020-11-01 08:53:20 -05:00
}
2020-10-31 21:53:11 -04:00
2020-11-19 06:10:51 -05:00
TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname) {
TRANSLATOR* t = (TRANSLATOR*)malloc(sizeof(TRANSLATOR));
t->asmlns = (STRLIST*)malloc(sizeof(STRLIST));
t->asmlns->count = 0;
t->asmlns->size = sizeof(char*)*(lns->count * 15);
t->asmlns->items = (char**)malloc(t->asmlns->size);
t->toclean = (STRLIST*)malloc(sizeof(STRLIST));
t->toclean->count = 0;
t->toclean->size = sizeof(char*)*(lns->count * 5);
t->toclean->items = (char**)malloc(t->toclean->size);
2020-11-17 12:12:39 -05:00
t->funcount = 0;
t->retind = 0;
t->cmpind = 0;
2020-11-19 06:10:51 -05:00
t->returned = true;
2020-11-01 08:53:20 -05:00
t->lns = lns;
t->fname = fname;
2020-11-14 10:31:57 -05:00
t->fnamelen = strlen(fname);
2020-11-01 08:53:20 -05:00
return t;
2020-10-30 13:06:02 -04:00
}