vm-translator/translator.c

418 lines
9.9 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-01 16:57:56 -05:00
#define CMPLIMIT 1000
#define CMPLEN 4
2020-10-31 21:53:11 -04:00
2020-11-01 08:53:20 -05:00
void pushtoclean(struct Translator* t, char* topush) {
int nextsz = sizeof(char*)*(t->tocleanind+1);
if(nextsz >= t->tocleansize) {
t->tocleansize = nextsz * 2;
t->toclean = realloc(t->toclean, t->tocleansize);
2020-10-31 21:53:11 -04:00
}
2020-11-01 08:53:20 -05:00
t->toclean[t->tocleanind] = topush;
t->tocleanind++;
2020-10-31 21:53:11 -04:00
}
2020-11-01 08:53:20 -05:00
void freetoclean(struct Translator* t) {
for(int i = 0; i < t->tocleanind; i++)
free(t->toclean[i]);
free(t->toclean);
t->tocleansize = 0;
t->tocleanind = 0;
2020-10-31 21:53:11 -04:00
}
2020-11-01 08:53:20 -05:00
void freeasmlns(struct Translator* t) {
for(int i = 0; i < t->asmind; i++)
free(t->asmlns[i]);
free(t->asmlns);
t->asmsize = 0;
t->asmind = 0;
2020-10-31 21:53:11 -04:00
}
2020-10-30 13:06:02 -04:00
2020-11-01 08:53:20 -05:00
void freetranslator(struct Translator* t) {
freeasmlns(t);
freetoclean(t);
free(t);
2020-10-31 11:40:06 -04:00
}
2020-11-01 08:53:20 -05:00
void printasmlns(struct Translator* t) {
for(int i = 0; i < t->asmind; i++)
printf("%s\n", t->asmlns[i]->instr);
}
char* heapstr(struct Translator* t, const char* input) {
2020-10-30 13:06:02 -04:00
char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1));
strcpy(newstr, input);
2020-11-01 08:53:20 -05:00
pushtoclean(t, newstr);
2020-10-30 13:06:02 -04:00
return newstr;
}
2020-11-01 08:53:20 -05:00
char* switchseg(struct Translator* t, struct 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-01 08:53:20 -05:00
return heapstr(t, "@LCL");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "argument"))
2020-11-01 08:53:20 -05:00
return heapstr(t, "@ARG");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "this"))
2020-11-01 08:53:20 -05:00
return heapstr(t, "@THIS");
2020-11-01 10:40:43 -05:00
if(!strcmp(seg, "that"))
2020-11-01 08:53:20 -05:00
return heapstr(t, "@THAT");
2020-10-31 19:25:00 -04:00
fprintf(stderr, "Unrecognized segment '%s'; line %i\n", seg, ln->truen);
exit(1);
2020-10-30 13:06:02 -04:00
}
2020-11-01 10:40:43 -05:00
int lnlen(int* out, struct line* ln) {
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-01 10:40:43 -05:00
char* mkcom(struct Translator* t, struct line* ln) {
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-01 10:40:43 -05:00
void checkind(struct line* ln, int indlen) {
for(int i = 0; i < indlen; i++)
2020-10-30 13:06:02 -04:00
if(!isdigit(ln->tokens[2][i])) {
fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen);
exit(1);
}
}
2020-11-01 16:57:56 -05:00
char* mkcmplab(struct Translator* t, struct line* ln) {
t->compcount++;
if(t->compcount > CMPLIMIT) {
fprintf(stderr, "Reached comparison limit (%i); line %i\n", CMPLIMIT, ln->truen);
exit(1);
}
int newsz = (CMPLEN+13)*sizeof(char);
char* label = (char*)malloc(newsz);
snprintf(label, newsz, "COMPARISON-%i", t->compcount);
pushtoclean(t, label);
return label;
}
2020-11-01 10:40:43 -05:00
char* mkind(struct Translator* t, struct line* ln, int indlen) {
checkind(ln, indlen);
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-01 16:57:56 -05:00
char* atlab(struct Translator* t, char* label, int labellen) {
int newsz = sizeof(char) * (labellen + 2);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", label);
pushtoclean(t, newind);
return newind;
}
char* mklab(struct Translator* t, char* label, int labellen) {
int newsz = sizeof(char) * (labellen + 3);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "(%s)", label);
pushtoclean(t, newind);
return newind;
}
2020-11-01 10:40:43 -05:00
char* mkstatind(struct Translator* t, struct line* ln, int indlen) {
checkind(ln, indlen);
2020-11-01 08:53:20 -05:00
int fnamelen = strlen(t->fname);
2020-11-01 10:40:43 -05:00
int newsz = sizeof(char) * (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-01 10:40:43 -05:00
char* mktempind(struct Translator* t, struct line* ln, int indlen) {
checkind(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-01 10:40:43 -05:00
char* mkpointerind(struct Translator* t, struct line* ln, int indlen) {
if(indlen > 1) {
fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen);
exit(1);
}
char* ptr;
switch(ln->tokens[2][0]) {
case '0':
ptr = "THIS";
break;
case '1':
ptr = "THAT";
break;
default:
fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen);
exit(1);
}
int newsz = sizeof(char) * 6;
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ptr);
pushtoclean(t, newind);
return newind;
}
2020-11-01 08:53:20 -05:00
void checkasmsize(struct Translator* t, int toadd) {
int targ = sizeof(struct asmln*)*(t->asmind+toadd);
if(targ >= t->asmsize) {
t->asmsize = targ * 2;
t->asmlns = (struct asmln**)realloc(t->asmlns, t->asmsize);
2020-10-30 13:06:02 -04:00
}
}
struct asmln* mkasmln(struct line* ln, char* content) {
struct asmln* newln = (struct asmln*)malloc(sizeof(struct asmln));
newln->truen = ln->truen;
newln->instr = content;
return newln;
}
2020-10-31 11:40:06 -04:00
void checkopamnt(int amnt, struct line* ln) {
if(ln->tokenscount < 2) {
fprintf(stderr, "Missing memory segment; line %i", ln->truen);
exit(1);
}
if(amnt > 2)
if(ln->tokenscount < 3) {
fprintf(stderr, "Missing operation index; line %i", ln->truen);
exit(1);
}
}
2020-11-01 08:53:20 -05:00
void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) {
2020-11-01 10:40:43 -05:00
checkasmsize(t, instcount);
// instruction comment
insts[0] = mkcom(t, ln);
2020-10-31 21:53:11 -04:00
for(int i = 0; i < instcount; i++) {
2020-11-01 08:53:20 -05:00
t->asmlns[t->asmind] = mkasmln(ln, insts[i]);
t->asmind++;
2020-10-31 21:53:11 -04:00
}
}
2020-10-30 13:06:02 -04:00
2020-11-01 08:53:20 -05:00
void startpoppush(struct Translator* t, struct 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-01 08:53:20 -05:00
insts[2] = heapstr(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-01 08:53:20 -05:00
void pushcons(struct Translator* t, struct 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-01 08:53:20 -05:00
void pushstat(struct Translator* t, struct 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-01 08:53:20 -05:00
void pushtemp(struct Translator* t, struct 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-01 10:40:43 -05:00
void pushpointer(struct Translator* t, struct line* ln, int indlen) {
// @THIS/@THAT
tpushpointer[1] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN);
}
2020-10-31 19:25:00 -04:00
2020-11-01 10:40:43 -05:00
void push(struct Translator* t, struct 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-01 08:53:20 -05:00
void popstat(struct Translator* t, struct 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-01 10:40:43 -05:00
tpopstat[TPOPSTATN-1] = heapstr(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-01 08:53:20 -05:00
void poptemp(struct Translator* t, struct 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-01 10:40:43 -05:00
tpoptemp[TPOPTEMPN-1] = heapstr(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-01 10:40:43 -05:00
void poppointer(struct Translator* t, struct line* ln, int indlen) {
// @THIS/@THAT
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpoppointer, TPOPPOINTERN);
}
2020-10-30 13:06:02 -04:00
2020-11-01 10:40:43 -05:00
void pop(struct Translator* t, struct 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-01 16:57:56 -05:00
void arith(struct Translator* t, struct line* ln, char* op) {
tarith[TARITHN-1] = heapstr(t, op);
addasmlns(t, ln, tarith, TARITHN);
}
void comp(struct Translator* t, struct line* ln, char* op) {
char* label = mkcmplab(t, ln);
int labellen = strlen(label);
// @label
tcomp[TCOMPN-6] = atlab(t, label, labellen);
// D;J(op)
int opsz = sizeof(char)*6;
char* trueop = (char*)malloc(opsz);
snprintf(trueop, opsz, "D;J%s", op);
tcomp[TCOMPN-5] = trueop;
// (label)
tcomp[TCOMPN-1] = mklab(t, label, labellen);
free(label);
addasmlns(t, ln, tcomp, TCOMPN);
};
2020-11-01 10:40:43 -05:00
void switchpush(struct Translator* t, struct line* ln) {
checkopamnt(3, ln);
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-01 10:40:43 -05:00
void switchpop(struct Translator* t, struct line* ln) {
checkopamnt(3, ln);
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-01 10:40:43 -05:00
void switchop(struct Translator* t, struct line* ln) {
char* op = ln->tokens[0];
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"))
arith(t, ln, "M=M+D");
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, "GT");
else if(!strcmp(op, "lt"))
comp(t, ln, "LT");
else if(!strcmp(op, "and"))
arith(t, ln, "M=M&D");
else if(!strcmp(op, "or"))
arith(t, ln, "M=M|D");
else if(!strcmp(op, "not"))
addasmlns(t, ln, tnot, TNOTN);
2020-11-01 10:40:43 -05:00
else {
fprintf(stderr, "Unrecognized operation '%s'; line %i\n", op, ln->truen);
exit(1);
2020-10-31 21:53:11 -04:00
}
2020-10-30 13:06:02 -04:00
}
2020-11-01 08:53:20 -05:00
void translate(struct Translator* t) {
for(int i = 0; i < t->lns->count; i++)
switchop(t, t->lns->lns[i]);
}
2020-10-31 21:53:11 -04:00
2020-11-01 08:53:20 -05:00
struct Translator* mktranslator(struct lnarray* lns, char* fname) {
struct Translator* t = (struct Translator*)malloc(sizeof(struct Translator));
t->asmsize = sizeof(struct asmln*)*(lns->count * 15);
t->asmind = 0;
t->asmlns = (struct asmln**)malloc(t->asmsize);
t->tocleanind = 0;
t->tocleansize = sizeof(char*)*(lns->count * 5);
t->toclean = (char**)malloc(t->tocleansize);
t->lns = lns;
t->fname = fname;
return t;
2020-10-30 13:06:02 -04:00
}