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-21 09:25:30 -05:00
|
|
|
freelns(t->output);
|
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
|
|
|
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-21 09:25:30 -05:00
|
|
|
void pushln(TRANSLATOR* t, char* content) {
|
|
|
|
t->curln->content = content;
|
|
|
|
t->curln->truen = t->lncount;
|
|
|
|
LINELIST* nextln = (LINELIST*)malloc(sizeof(LINELIST));
|
|
|
|
t->curln->next = nextln;
|
|
|
|
t->lastln = t->curln;
|
|
|
|
t->curln = nextln;
|
|
|
|
t->lncount++;
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
void addasm(TRANSLATOR* t, TEMPLATE* tp) {
|
|
|
|
checkasmsize(t, tp->count);
|
2020-11-01 10:40:43 -05:00
|
|
|
|
2020-11-21 09:25:30 -05:00
|
|
|
for(int i = 0; i < tp->count; i++)
|
|
|
|
pushln(t, tp->items[i]);
|
2020-10-31 21:53:11 -04:00
|
|
|
}
|
2020-10-30 13:06:02 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
void addasmlns(TRANSLATOR* t, LINE* ln, TEMPLATE* tp) {
|
2020-11-17 12:12:39 -05:00
|
|
|
// instruction comment
|
2020-11-19 08:43:25 -05:00
|
|
|
tp->items[0] = mkcom(t, ln);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasm(t, tp);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, TEMPLATE* tp) {
|
2020-10-30 13:06:02 -04:00
|
|
|
// @segment
|
2020-11-19 08:43:25 -05:00
|
|
|
tp->items[1] = switchseg(t, ln);
|
2020-10-30 13:06:02 -04:00
|
|
|
|
|
|
|
// D=M
|
2020-11-19 08:43:25 -05:00
|
|
|
tp->items[2] = heapstrtoclean(t, "D=M");
|
2020-10-30 13:06:02 -04:00
|
|
|
|
|
|
|
// @i
|
2020-11-19 08:43:25 -05:00
|
|
|
tp->items[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-19 08:43:25 -05:00
|
|
|
tpushcons.items[1] = mkind(t, ln, indlen);
|
2020-10-31 18:00:09 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpushcons);
|
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-19 08:43:25 -05:00
|
|
|
tpushstat.items[1] = mkstatind(t, ln, indlen);
|
2020-10-31 19:25:00 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpushstat);
|
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-19 08:43:25 -05:00
|
|
|
tpushtemp.items[1] = mktempind(t, ln, indlen);
|
2020-10-31 19:25:00 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpushtemp);
|
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
|
2020-11-19 08:43:25 -05:00
|
|
|
tpushpointer.items[1] = mkpointerind(t, ln, indlen);
|
2020-11-01 10:40:43 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpushpointer);
|
2020-11-01 10:40:43 -05:00
|
|
|
}
|
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-19 08:43:25 -05:00
|
|
|
startpoppush(t, ln, indlen, &tpush);
|
2020-10-31 19:25:00 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpush);
|
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-19 08:43:25 -05:00
|
|
|
tpopstat.items[tpopstat.count-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-19 08:43:25 -05:00
|
|
|
tpopstat.items[tpopstat.count-1] = heapstrtoclean(t, "M=D");
|
2020-10-30 13:06:02 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpopstat);
|
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-19 08:43:25 -05:00
|
|
|
tpoptemp.items[tpoptemp.count-2] = mktempind(t, ln, indlen);
|
2020-10-31 19:25:00 -04:00
|
|
|
|
|
|
|
// M=D
|
2020-11-19 08:43:25 -05:00
|
|
|
tpoptemp.items[tpoptemp.count-1] = heapstrtoclean(t, "M=D");
|
2020-10-31 19:25:00 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpoptemp);
|
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
|
2020-11-19 08:43:25 -05:00
|
|
|
tpoppointer.items[tpoppointer.count-2] = mkpointerind(t, ln, indlen);
|
2020-11-01 10:40:43 -05:00
|
|
|
|
2020-11-01 17:39:27 -05:00
|
|
|
// M=D
|
2020-11-19 08:43:25 -05:00
|
|
|
tpoppointer.items[tpoppointer.count-1] = heapstrtoclean(t, "M=D");
|
2020-11-01 17:39:27 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpoppointer);
|
2020-11-01 10:40:43 -05:00
|
|
|
}
|
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-19 08:43:25 -05:00
|
|
|
startpoppush(t, ln, indlen, &tpop);
|
2020-10-30 13:06:02 -04:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tpop);
|
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-19 08:43:25 -05:00
|
|
|
tarith.items[tarith.count-1] = heapstrtoclean(t, op);
|
2020-11-01 16:57:56 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tarith);
|
2020-11-01 16:57:56 -05:00
|
|
|
}
|
|
|
|
|
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
|
2020-11-19 08:43:25 -05:00
|
|
|
tcomp.items[tcomp.count-6] = atlab(t, label, labellen);
|
2020-11-01 16:57:56 -05:00
|
|
|
|
|
|
|
// 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);
|
2020-11-19 08:43:25 -05:00
|
|
|
tcomp.items[tcomp.count-5] = trueop;
|
2020-11-14 10:31:57 -05:00
|
|
|
pushtoclean(t, trueop);
|
2020-11-01 16:57:56 -05:00
|
|
|
|
|
|
|
// (label)
|
2020-11-19 08:43:25 -05:00
|
|
|
tcomp.items[tcomp.count-1] = mklab(t, label, labellen);
|
2020-11-01 16:57:56 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tcomp);
|
2020-11-01 16:57:56 -05:00
|
|
|
};
|
|
|
|
|
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);
|
2020-11-19 08:43:25 -05:00
|
|
|
tlabel.items[tlabel.count-1] = lab;
|
2020-11-14 10:31:57 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tlabel);
|
2020-11-14 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
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-19 08:43:25 -05:00
|
|
|
tgoto.items[tgoto.count-2] = mkgotolab(t, ln);
|
2020-11-14 10:31:57 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tgoto);
|
2020-11-14 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
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-19 08:43:25 -05:00
|
|
|
tifgoto.items[tifgoto.count-2] = mkgotolab(t, ln);
|
2020-11-14 10:31:57 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tifgoto);
|
2020-11-14 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 06:10:51 -05:00
|
|
|
int pushframe(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) {
|
2020-11-19 08:43:25 -05:00
|
|
|
tcallstart.items[1] = atlab(t, retlab, retlablen);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tcallstart);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
for(int i = 0; i < tframevars.count; i++) {
|
|
|
|
tcallpush.items[0] = tframevars.items[i];
|
|
|
|
addasm(t, &tcallpush);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
return tframevars.count + 1;
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
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]);
|
2020-11-19 08:43:25 -05:00
|
|
|
tcallsetarg.items[tcallsetarg.count-4] = atn(t, nargs + framesize);
|
|
|
|
addasm(t, &tcallsetarg);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
|
|
|
// jmp
|
|
|
|
int jmplen = strlen(ln->tokens[1]);
|
2020-11-19 08:43:25 -05:00
|
|
|
tcalljmp.items[tcalljmp.count-3] = atlab(t, ln->tokens[1], jmplen);
|
|
|
|
tcalljmp.items[tcalljmp.count-1] = mklab(t, retlab, retlablen);
|
|
|
|
addasm(t, &tcalljmp);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2020-11-19 08:43:25 -05:00
|
|
|
tfunction.items[1] = mklab(t, ln->tokens[1], funlen);
|
|
|
|
addasmlns(t, ln, &tfunction);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
|
|
|
// 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++) {
|
2020-11-19 08:43:25 -05:00
|
|
|
addasm(t, &tfunctionpush);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 06:10:51 -05:00
|
|
|
void myreturn(TRANSLATOR* t, LINE* ln) {
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tstartreturn);
|
2020-11-17 12:12:39 -05:00
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
for(int i = tframevars.count-1; i >= 0; i--) {
|
|
|
|
tretpop.items[tretpop.count-2] = tframevars.items[i];
|
|
|
|
addasm(t, &tretpop);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 08:43:25 -05:00
|
|
|
addasm(t, &tendreturn);
|
2020-11-17 12:12:39 -05:00
|
|
|
}
|
|
|
|
|
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"))
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tneg);
|
2020-11-01 16:57:56 -05:00
|
|
|
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"))
|
2020-11-19 08:43:25 -05:00
|
|
|
addasmlns(t, ln, &tnot);
|
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-21 09:25:30 -05:00
|
|
|
t->lastln->next = NULL;
|
|
|
|
free(t->curln);
|
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-21 09:25:30 -05:00
|
|
|
LINELIST* newln = (LINELIST*)malloc(sizeof(LINELIST));
|
|
|
|
t->output = newln;
|
|
|
|
t->curln = newln;
|
|
|
|
t->lncount = 0;
|
|
|
|
|
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
|
|
|
}
|