Compare commits
5 Commits
a6667a8f5d
...
8b188ddb37
Author | SHA1 | Date |
---|---|---|
Augusto Gunsch | 8b188ddb37 | |
Augusto Gunsch | ca2c128e67 | |
Augusto Gunsch | 6262faf28a | |
Augusto Gunsch | d15bd48aba | |
Augusto Gunsch | 4e532a5cb9 |
|
@ -0,0 +1,3 @@
|
|||
*.h linguist-language=C
|
||||
*.c linguist-language=C
|
||||
Makefile -linguist-detectable
|
|
@ -1 +1,2 @@
|
|||
vmtranslator
|
||||
tags
|
||||
|
|
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
|||
FILES = parser.c main.c translator.c util.c
|
||||
INCLUDES = -I.
|
||||
CFLAGS = -std=c99 -g
|
||||
CFLAGS = -std=c99
|
||||
OUTFILE = vmtranslator
|
||||
|
||||
main: ${FILES}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
# vm-translator
|
||||
Virtual machine language translator, as specified in [nand2tetris project 8](https://www.nand2tetris.org/project08).<br>
|
66
main.c
66
main.c
|
@ -4,23 +4,34 @@
|
|||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include "parser.h"
|
||||
#include "translator.h"
|
||||
#include "bootstrap.h"
|
||||
#include "util.h"
|
||||
|
||||
struct TranslationList {
|
||||
#include <limits.h>
|
||||
#ifndef PATH_MAX
|
||||
#ifdef __linux__
|
||||
#include <linux/limits.h>
|
||||
#else
|
||||
#define PATH_MAX 512
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char** files;
|
||||
char** fnames;
|
||||
int filecount;
|
||||
int filessz;
|
||||
char* output;
|
||||
};
|
||||
} TRANSLATIONLIST;
|
||||
|
||||
char* getname(char* f, int len) {
|
||||
int startind = 0;
|
||||
int endind = len - 1;
|
||||
short readsmt = 0;
|
||||
bool readsmt = false;
|
||||
|
||||
for(int i = endind; i >= 0; i--) {
|
||||
if(f[i] == '/') {
|
||||
|
@ -34,7 +45,7 @@ char* getname(char* f, int len) {
|
|||
}
|
||||
if(f[i] == '.')
|
||||
endind = i-1;
|
||||
readsmt = 1;
|
||||
readsmt = true;
|
||||
}
|
||||
|
||||
int size = sizeof(char)*(endind - startind + 2);
|
||||
|
@ -46,7 +57,7 @@ char* getname(char* f, int len) {
|
|||
|
||||
char* getfullname(char* f, int len) {
|
||||
int endind = len - 1;
|
||||
short readsmt = 0;
|
||||
bool readsmt = false;
|
||||
|
||||
for(int i = endind; i >= 0; i--) {
|
||||
if(f[i] == '/') {
|
||||
|
@ -58,7 +69,7 @@ char* getfullname(char* f, int len) {
|
|||
}
|
||||
if(f[i] == '.')
|
||||
endind = i-1;
|
||||
readsmt = 1;
|
||||
readsmt = true;
|
||||
}
|
||||
|
||||
int size = sizeof(char)*(endind + 2);
|
||||
|
@ -67,30 +78,39 @@ char* getfullname(char* f, int len) {
|
|||
return retstr;
|
||||
}
|
||||
|
||||
short isdotvm(char* f, int extind) {
|
||||
bool isdotvm(char* f, int extind) {
|
||||
char* extstr = f + (sizeof(char) * extind);
|
||||
return strcmp(extstr, ".vm") == 0;
|
||||
}
|
||||
|
||||
short isdir(char* f, int len) {
|
||||
short readsmt = 0;
|
||||
bool isdir(char* f, int len) {
|
||||
bool readsmt = false;
|
||||
for(int i = len-1; i >= 0; i--) {
|
||||
if(f[i] == '.')
|
||||
if(readsmt)
|
||||
return 0;
|
||||
return false;
|
||||
else
|
||||
continue;
|
||||
if(f[i] == '/')
|
||||
return 1;
|
||||
readsmt = 1;
|
||||
readsmt = true;
|
||||
}
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
char* getoutname(char* input, int len, short isdir) {
|
||||
char* getoutname(char* input, int len, bool isdir) {
|
||||
char* outname;
|
||||
if(isdir) {
|
||||
char* name = getname(input, len);
|
||||
char olddir[PATH_MAX];
|
||||
getcwd(olddir, PATH_MAX);
|
||||
|
||||
chdir(input);
|
||||
char buf[PATH_MAX];
|
||||
getcwd(buf, PATH_MAX);
|
||||
|
||||
chdir(olddir);
|
||||
|
||||
char* name = getname(buf, strlen(buf));
|
||||
int sz = sizeof(char) * (strlen(name)+len+6);
|
||||
outname = (char*)malloc(sz);
|
||||
snprintf(outname, sz, "%s/%s.asm", input, name);
|
||||
|
@ -106,7 +126,7 @@ char* getoutname(char* input, int len, short isdir) {
|
|||
return outname;
|
||||
}
|
||||
|
||||
void addfile(struct TranslationList* l, char* fullname, char* name) {
|
||||
void addfile(TRANSLATIONLIST* l, char* fullname, char* name) {
|
||||
int count = l->filecount;
|
||||
int targsize = (count + 1) * sizeof(char*);
|
||||
|
||||
|
@ -122,16 +142,16 @@ void addfile(struct TranslationList* l, char* fullname, char* name) {
|
|||
l->filecount++;
|
||||
}
|
||||
|
||||
struct TranslationList* getfiles(char* input) {
|
||||
TRANSLATIONLIST* getfiles(char* input) {
|
||||
int filessz = sizeof(char*) * 16;
|
||||
struct TranslationList* filelist = (struct TranslationList*)malloc(sizeof(struct TranslationList));
|
||||
TRANSLATIONLIST* filelist = (TRANSLATIONLIST*)malloc(sizeof(TRANSLATIONLIST));
|
||||
filelist->files = (char**)malloc(filessz);
|
||||
filelist->fnames = (char**)malloc(filessz);
|
||||
filelist->filessz = filessz;
|
||||
filelist->filecount = 0;
|
||||
|
||||
int inplen = strlen(input);
|
||||
short isitdir = isdir(input, inplen);
|
||||
bool isitdir = isdir(input, inplen);
|
||||
if(isitdir) {
|
||||
DIR* dir = opendir(input);
|
||||
|
||||
|
@ -182,7 +202,7 @@ struct TranslationList* getfiles(char* input) {
|
|||
return filelist;
|
||||
}
|
||||
|
||||
void freetranslationlist(struct TranslationList* ls) {
|
||||
void freetranslationlist(TRANSLATIONLIST* ls) {
|
||||
for(int i = 0; i < ls->filecount; i++) {
|
||||
free(ls->files[i]);
|
||||
free(ls->fnames[i]);
|
||||
|
@ -199,7 +219,7 @@ int main(int argc, char* argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
struct TranslationList* ls = getfiles(argv[1]);
|
||||
TRANSLATIONLIST* ls = getfiles(argv[1]);
|
||||
FILE* output = fopen(ls->output, "w");
|
||||
|
||||
for(int i = 0; i < BOOTSTRAPN; i++) {
|
||||
|
@ -218,15 +238,15 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
// parsing
|
||||
struct Parser* p = mkparser(input);
|
||||
PARSER* p = mkparser(input);
|
||||
parse(p);
|
||||
|
||||
// translating
|
||||
struct Translator* t = mktranslator(p->lns, fname);
|
||||
TRANSLATOR* t = mktranslator(p->lns, fname);
|
||||
translate(t);
|
||||
freeparser(p);
|
||||
|
||||
printasmlns(t, output);
|
||||
printlns(t->output, output);
|
||||
|
||||
// freeing rest
|
||||
freetranslator(t);
|
||||
|
|
21
parser.c
21
parser.c
|
@ -2,9 +2,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "parser.h"
|
||||
|
||||
void freelns(struct lnarray* lns) {
|
||||
void freelnarray(LINEARRAY* lns) {
|
||||
for(int i = 0; i < lns->count; i++) {
|
||||
int tkcount = lns->lns[i]->tokenscount;
|
||||
for(int j = 0; j < tkcount; j++) {
|
||||
|
@ -17,8 +18,8 @@ void freelns(struct lnarray* lns) {
|
|||
free(lns);
|
||||
}
|
||||
|
||||
void freeparser(struct Parser* p) {
|
||||
freelns(p->lns);
|
||||
void freeparser(PARSER* p) {
|
||||
freelnarray(p->lns);
|
||||
free(p);
|
||||
}
|
||||
|
||||
|
@ -32,7 +33,7 @@ void gountilbrk (FILE* input) {
|
|||
}
|
||||
}
|
||||
|
||||
void getinfo(struct Parser* p) {
|
||||
void getinfo(PARSER* p) {
|
||||
p->lns->count = 0;
|
||||
p->maxtokens = 0;
|
||||
p->widestln = 0;
|
||||
|
@ -74,17 +75,17 @@ void getinfo(struct Parser* p) {
|
|||
rewind(p->input);
|
||||
}
|
||||
|
||||
struct Parser* mkparser(FILE* input) {
|
||||
struct Parser* p = (struct Parser*)malloc(sizeof(struct Parser));
|
||||
struct lnarray* lns = (struct lnarray*)malloc(sizeof(struct lnarray));
|
||||
PARSER* mkparser(FILE* input) {
|
||||
PARSER* p = (PARSER*)malloc(sizeof(PARSER));
|
||||
LINEARRAY* lns = (LINEARRAY*)malloc(sizeof(LINEARRAY));
|
||||
p->input = input;
|
||||
p->lns = lns;
|
||||
getinfo(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
void parse(struct Parser* p) {
|
||||
struct line** lns = (struct line**)malloc(sizeof(struct line*)*p->lns->count);
|
||||
void parse(PARSER* p) {
|
||||
LINE** lns = (LINE**)malloc(sizeof(LINE*)*p->lns->count);
|
||||
p->lns->lns = lns;
|
||||
p->lns->count = 0;
|
||||
|
||||
|
@ -111,7 +112,7 @@ void parse(struct Parser* p) {
|
|||
if(c == '\n') {
|
||||
truelncount++;
|
||||
if(tokensind > 0) {
|
||||
struct line* newln = (struct line*)malloc(sizeof(struct line));
|
||||
LINE* newln = (LINE*)malloc(sizeof(LINE));
|
||||
newln->tokens = (char**)malloc(sizeof(char*)*tokensind);
|
||||
for(int i = 0; i < tokensind; i++) {
|
||||
newln->tokens[i] = tokens[i];
|
||||
|
|
26
parser.h
26
parser.h
|
@ -1,24 +1,24 @@
|
|||
#ifndef parser
|
||||
#define parser
|
||||
struct line {
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
typedef struct {
|
||||
char** tokens;
|
||||
int tokenscount;
|
||||
int truen;
|
||||
};
|
||||
} LINE;
|
||||
|
||||
struct lnarray {
|
||||
struct line** lns;
|
||||
typedef struct {
|
||||
LINE** lns;
|
||||
int count;
|
||||
};
|
||||
} LINEARRAY;
|
||||
|
||||
struct Parser {
|
||||
typedef struct {
|
||||
FILE* input;
|
||||
struct lnarray* lns;
|
||||
LINEARRAY* lns;
|
||||
int widestln;
|
||||
int maxtokens;
|
||||
};
|
||||
} PARSER;
|
||||
|
||||
struct Parser* mkparser(FILE* input);
|
||||
void freeparser(struct Parser* p);
|
||||
void parse(struct Parser* p);
|
||||
PARSER* mkparser(FILE* input);
|
||||
void freeparser(PARSER* p);
|
||||
void parse(PARSER* p);
|
||||
#endif
|
||||
|
|
183
templates.h
183
templates.h
|
@ -1,8 +1,12 @@
|
|||
#ifndef templates
|
||||
#define templates
|
||||
|
||||
#define TPUSHN 11
|
||||
char* tpush[TPUSHN] = {
|
||||
typedef struct {
|
||||
char** items;
|
||||
int count;
|
||||
} TEMPLATE;
|
||||
|
||||
char* tpushlns[] = {
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
|
@ -15,9 +19,12 @@ char* tpush[TPUSHN] = {
|
|||
"@SP",
|
||||
"M=M+1",
|
||||
};
|
||||
TEMPLATE tpush = {
|
||||
.items = tpushlns,
|
||||
.count = sizeof(tpushlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPUSHCONSN 8
|
||||
char* tpushcons[TPUSHCONSN] = {
|
||||
char* tpushconslns[] = {
|
||||
"",
|
||||
"",
|
||||
"D=A",
|
||||
|
@ -27,9 +34,12 @@ char* tpushcons[TPUSHCONSN] = {
|
|||
"@SP",
|
||||
"M=M+1",
|
||||
};
|
||||
TEMPLATE tpushcons = {
|
||||
.items = tpushconslns,
|
||||
.count = sizeof(tpushconslns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPUSHSTATN 8
|
||||
char* tpushstat[TPUSHSTATN] = {
|
||||
char* tpushstatlns[] = {
|
||||
"",
|
||||
"",
|
||||
"D=M",
|
||||
|
@ -39,15 +49,22 @@ char* tpushstat[TPUSHSTATN] = {
|
|||
"@SP",
|
||||
"M=M+1",
|
||||
};
|
||||
TEMPLATE tpushstat = {
|
||||
.items = tpushstatlns,
|
||||
.count = sizeof(tpushstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPUSHTEMPN TPUSHSTATN
|
||||
char** tpushtemp = tpushstat;
|
||||
TEMPLATE tpushtemp = {
|
||||
.items = tpushstatlns,
|
||||
.count = sizeof(tpushstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPUSHPOINTERN TPUSHSTATN
|
||||
char** tpushpointer = tpushstat;
|
||||
TEMPLATE tpushpointer = {
|
||||
.items = tpushstatlns,
|
||||
.count = sizeof(tpushstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPOPN 13
|
||||
char* tpop[TPOPN] = {
|
||||
char* tpoplns[] = {
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
|
@ -62,9 +79,12 @@ char* tpop[TPOPN] = {
|
|||
"A=M",
|
||||
"M=D"
|
||||
};
|
||||
TEMPLATE tpop = {
|
||||
.items = tpoplns,
|
||||
.count = sizeof(tpoplns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPOPSTATN 6
|
||||
char* tpopstat[TPOPSTATN] = {
|
||||
char* tpopstatlns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"AM=M-1",
|
||||
|
@ -72,15 +92,22 @@ char* tpopstat[TPOPSTATN] = {
|
|||
"",
|
||||
""
|
||||
};
|
||||
TEMPLATE tpopstat = {
|
||||
.items = tpopstatlns,
|
||||
.count = sizeof(tpopstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPOPTEMPN TPOPSTATN
|
||||
char** tpoptemp = tpopstat;
|
||||
TEMPLATE tpoptemp = {
|
||||
.items = tpopstatlns,
|
||||
.count = sizeof(tpopstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TPOPPOINTERN TPOPSTATN
|
||||
char** tpoppointer = tpopstat;
|
||||
TEMPLATE tpoppointer = {
|
||||
.items = tpopstatlns,
|
||||
.count = sizeof(tpopstatlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TARITHN 6
|
||||
char* tarith[TARITHN] = {
|
||||
char* tarithlns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"AM=M-1",
|
||||
|
@ -88,25 +115,34 @@ char* tarith[TARITHN] = {
|
|||
"A=A-1",
|
||||
""
|
||||
};
|
||||
TEMPLATE tarith = {
|
||||
.items = tarithlns,
|
||||
.count = sizeof(tarithlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TNEGN 4
|
||||
char* tneg[TNEGN] = {
|
||||
char* tneglns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"A=M-1",
|
||||
"M=-M",
|
||||
};
|
||||
TEMPLATE tneg = {
|
||||
.items = tneglns,
|
||||
.count = sizeof(tneglns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TNOTN 4
|
||||
char* tnot[TNOTN] = {
|
||||
char* tnotlns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"A=M-1",
|
||||
"M=!M",
|
||||
};
|
||||
TEMPLATE tnot = {
|
||||
.items = tnotlns,
|
||||
.count = sizeof(tnotlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TCOMPN 13
|
||||
char* tcomp[TCOMPN] = {
|
||||
char* tcomplns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"AM=M-1",
|
||||
|
@ -121,22 +157,31 @@ char* tcomp[TCOMPN] = {
|
|||
"M=0",
|
||||
""
|
||||
};
|
||||
TEMPLATE tcomp = {
|
||||
.items = tcomplns,
|
||||
.count = sizeof(tcomplns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TLABELN 2
|
||||
char* tlabel[TLABELN] = {
|
||||
char* tlabellns[] = {
|
||||
"",
|
||||
""
|
||||
};
|
||||
TEMPLATE tlabel = {
|
||||
.items = tlabellns,
|
||||
.count = sizeof(tlabellns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TGOTON 3
|
||||
char* tgoto[TGOTON] = {
|
||||
char* tgotolns[] = {
|
||||
"",
|
||||
"",
|
||||
"0;JMP"
|
||||
};
|
||||
TEMPLATE tgoto = {
|
||||
.items = tgotolns,
|
||||
.count = sizeof(tgotolns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TIFGOTON 6
|
||||
char* tifgoto[TIFGOTON] = {
|
||||
char* tifgotolns[] = {
|
||||
"",
|
||||
"@SP",
|
||||
"AM=M-1",
|
||||
|
@ -144,9 +189,12 @@ char* tifgoto[TIFGOTON] = {
|
|||
"",
|
||||
"D;JNE"
|
||||
};
|
||||
TEMPLATE tifgoto = {
|
||||
.items = tifgotolns,
|
||||
.count = sizeof(tifgotolns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TCALLSTARTN 8
|
||||
char* tcallstart[TCALLSTARTN] = {
|
||||
char* tcallstartlns[] = {
|
||||
"",
|
||||
"",
|
||||
"D=A",
|
||||
|
@ -156,9 +204,12 @@ char* tcallstart[TCALLSTARTN] = {
|
|||
"@SP",
|
||||
"M=M+1",
|
||||
};
|
||||
TEMPLATE tcallstart = {
|
||||
.items = tcallstartlns,
|
||||
.count = sizeof(tcallstartlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TCALLPUSHN 7
|
||||
char* tcallpush[TCALLPUSHN] = {
|
||||
char* tcallpushlns[] = {
|
||||
"",
|
||||
"D=M",
|
||||
"@SP",
|
||||
|
@ -167,9 +218,12 @@ char* tcallpush[TCALLPUSHN] = {
|
|||
"@SP",
|
||||
"M=M+1",
|
||||
};
|
||||
TEMPLATE tcallpush = {
|
||||
.items = tcallpushlns,
|
||||
.count = sizeof(tcallpushlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TCALLSETARGN 8
|
||||
char* tcallsetarg[TCALLSETARGN] = {
|
||||
char* tcallsetarglns[] = {
|
||||
"@SP",
|
||||
"D=M",
|
||||
"@LCL",
|
||||
|
@ -179,39 +233,54 @@ char* tcallsetarg[TCALLSETARGN] = {
|
|||
"@ARG",
|
||||
"M=D"
|
||||
};
|
||||
TEMPLATE tcallsetarg = {
|
||||
.items = tcallsetarglns,
|
||||
.count = sizeof(tcallsetarglns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TCALLJMPN 3
|
||||
char* tcalljmp[TCALLJMPN] = {
|
||||
char* tcalljmplns[] = {
|
||||
"",
|
||||
"0;JMP",
|
||||
""
|
||||
};
|
||||
TEMPLATE tcalljmp = {
|
||||
.items = tcalljmplns,
|
||||
.count = sizeof(tcalljmplns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TFRAMEVARSN 4
|
||||
char* tframevars[TFRAMEVARSN] = {
|
||||
char* tframevarslns[] = {
|
||||
"@LCL",
|
||||
"@ARG",
|
||||
"@THIS",
|
||||
"@THAT"
|
||||
};
|
||||
TEMPLATE tframevars = {
|
||||
.items = tframevarslns,
|
||||
.count = sizeof(tframevarslns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TFUNCTIONN 2
|
||||
char* tfunction[TFUNCTIONN] = {
|
||||
char* tfunctionlns[] = {
|
||||
"",
|
||||
""
|
||||
};
|
||||
TEMPLATE tfunction = {
|
||||
.items = tfunctionlns,
|
||||
.count = sizeof(tfunctionlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TFUNCTIONPUSHN 5
|
||||
char* tfunctionpush[TFUNCTIONPUSHN] = {
|
||||
char* tfunctionpushlns[] = {
|
||||
"@SP",
|
||||
"A=M",
|
||||
"M=0",
|
||||
"@SP",
|
||||
"M=M+1"
|
||||
};
|
||||
TEMPLATE tfunctionpush = {
|
||||
.items = tfunctionpushlns,
|
||||
.count = sizeof(tfunctionpushlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TSTARTRETURNN 18
|
||||
char* tstartreturn[TSTARTRETURNN] = {
|
||||
char* tstartreturnlns[] = {
|
||||
"",
|
||||
"@LCL",
|
||||
"D=M",
|
||||
|
@ -231,21 +300,31 @@ char* tstartreturn[TSTARTRETURNN] = {
|
|||
"@SP",
|
||||
"M=D"
|
||||
};
|
||||
TEMPLATE tstartreturn = {
|
||||
.items = tstartreturnlns,
|
||||
.count = sizeof(tstartreturnlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TRETPOPN 5
|
||||
char* tretpop[TRETPOPN] = {
|
||||
char* tretpoplns[] = {
|
||||
"@LCL",
|
||||
"AM=M-1",
|
||||
"D=M",
|
||||
"",
|
||||
"M=D",
|
||||
};
|
||||
TEMPLATE tretpop = {
|
||||
.items = tretpoplns,
|
||||
.count = sizeof(tretpoplns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#define TENDRETURNN 3
|
||||
char* tendreturn[TENDRETURNN] = {
|
||||
char* tendreturnlns[] = {
|
||||
"@R13",
|
||||
"A=M",
|
||||
"0;JMP"
|
||||
};
|
||||
TEMPLATE tendreturn = {
|
||||
.items = tendreturnlns,
|
||||
.count = sizeof(tendreturnlns) / sizeof(char*)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
323
translator.c
323
translator.c
|
@ -6,40 +6,38 @@
|
|||
#include "templates.h"
|
||||
#include "util.h"
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
t->toclean[t->tocleanind] = topush;
|
||||
t->tocleanind++;
|
||||
t->toclean->items[t->toclean->count] = topush;
|
||||
t->toclean->count++;
|
||||
}
|
||||
|
||||
void freetoclean(struct Translator* t) {
|
||||
for(int i = 0; i < t->tocleanind; i++)
|
||||
free(t->toclean[i]);
|
||||
void freetoclean(TRANSLATOR* t) {
|
||||
for(int i = 0; i < t->toclean->count; i++)
|
||||
free(t->toclean->items[i]);
|
||||
free(t->toclean->items);
|
||||
free(t->toclean);
|
||||
}
|
||||
|
||||
void freetranslator(struct Translator* t) {
|
||||
void freetranslator(TRANSLATOR* t) {
|
||||
free(t->asmlns->items);
|
||||
free(t->asmlns);
|
||||
freelns(t->output);
|
||||
freetoclean(t);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void printasmlns(struct Translator* t, FILE* stream) {
|
||||
for(int i = 0; i < t->asmind; i++)
|
||||
fprintf(stream, "%s\n", t->asmlns[i]);
|
||||
}
|
||||
|
||||
char* heapstrtoclean(struct Translator* t, const char* input) {
|
||||
char* heapstrtoclean(TRANSLATOR* t, const char* input) {
|
||||
char* newstr = heapstr(input, strlen(input));
|
||||
pushtoclean(t, newstr);
|
||||
return newstr;
|
||||
}
|
||||
|
||||
char* switchseg(struct Translator* t, struct line* ln) {
|
||||
char* switchseg(TRANSLATOR* t, LINE* ln) {
|
||||
char* seg = ln->tokens[1];
|
||||
if(!strcmp(seg, "local"))
|
||||
return heapstrtoclean(t, "@LCL");
|
||||
|
@ -53,7 +51,7 @@ char* switchseg(struct Translator* t, struct line* ln) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
int lnlen(int* out, struct line* ln) {
|
||||
int lnlen(int* out, LINE* ln) {
|
||||
int len = 0;
|
||||
for(int i = 0; i < ln->tokenscount; i++) {
|
||||
int l = strlen(ln->tokens[i]);
|
||||
|
@ -65,7 +63,7 @@ int lnlen(int* out, struct line* ln) {
|
|||
|
||||
// produce comment as follows:
|
||||
// pop/push segment i
|
||||
char* mkcom(struct Translator* t, struct line* ln) {
|
||||
char* mkcom(TRANSLATOR* t, LINE* ln) {
|
||||
int lens[ln->tokenscount];
|
||||
int comlen = sizeof(char) * lnlen(lens, ln) + ln->tokenscount + 3;
|
||||
char* comment = (char*)malloc(comlen);
|
||||
|
@ -87,7 +85,7 @@ char* mkcom(struct Translator* t, struct line* ln) {
|
|||
return comment;
|
||||
}
|
||||
|
||||
void checknumber(struct Translator* t, struct line* ln, int indlen, char* name, char* n) {
|
||||
void checknumber(TRANSLATOR* t, LINE* ln, int indlen, char* name, char* n) {
|
||||
for(int i = 0; i < indlen; i++)
|
||||
if(!isdigit(n[i])) {
|
||||
fprintf(stderr, "Invalid %s '%s'; file %s.vm, line %i\n", t->fname, name, n, ln->truen);
|
||||
|
@ -95,40 +93,26 @@ void checknumber(struct Translator* t, struct line* ln, int indlen, char* name,
|
|||
}
|
||||
}
|
||||
|
||||
void checknargs(struct Translator* t, struct line* ln, int nargslen) {
|
||||
void checknargs(TRANSLATOR* t, LINE* ln, int nargslen) {
|
||||
checknumber(t, ln, nargslen, "argument number", ln->tokens[2]);
|
||||
}
|
||||
|
||||
void checknlocals(struct Translator* t, struct line* ln, int nlocalslen) {
|
||||
void checknlocals(TRANSLATOR* t, LINE* ln, int nlocalslen) {
|
||||
checknumber(t, ln, nlocalslen, "local variable number", ln->tokens[2]);
|
||||
}
|
||||
|
||||
void checkind(struct Translator* t, struct line* ln, int indlen) {
|
||||
void checkind(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
checknumber(t, ln, indlen, "index", ln->tokens[2]);
|
||||
}
|
||||
|
||||
int countplaces(int n) {
|
||||
int places = 1;
|
||||
int divisor = 1;
|
||||
if(n < 0) {
|
||||
n = -n;
|
||||
places++;
|
||||
}
|
||||
while(n / divisor >= 10) {
|
||||
places++;
|
||||
divisor *= 10;
|
||||
}
|
||||
return places;
|
||||
}
|
||||
|
||||
void checkinfun(struct Translator* t, struct line* ln) {
|
||||
void checkinfun(TRANSLATOR* t, LINE* ln) {
|
||||
if(t->funcount <= 0) {
|
||||
fprintf(stderr, "Instruction should be part of a function; file %s.vm, line %i\n", t->fname, ln->truen);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
char* mkspeciallab(struct Translator* t, struct line* ln, char* suffix, int* ind) {
|
||||
char* mkspeciallab(TRANSLATOR* t, LINE* ln, char* suffix, int* ind) {
|
||||
checkinfun(t, ln);
|
||||
(*ind)++;
|
||||
int sz = (t->lastfunlen + countplaces(*ind) + strlen(suffix) + 3) * sizeof(char);
|
||||
|
@ -138,15 +122,15 @@ char* mkspeciallab(struct Translator* t, struct line* ln, char* suffix, int* ind
|
|||
return lab;
|
||||
}
|
||||
|
||||
char* mkcmplab(struct Translator* t, struct line* ln) {
|
||||
char* mkcmplab(TRANSLATOR* t, LINE* ln) {
|
||||
return mkspeciallab(t, ln, "cmp", &(t->cmpind));
|
||||
}
|
||||
|
||||
char* mkretlab(struct Translator* t, struct line* ln) {
|
||||
char* mkretlab(TRANSLATOR* t, LINE* ln) {
|
||||
return mkspeciallab(t, ln, "ret", &(t->retind));
|
||||
}
|
||||
|
||||
char* mkind(struct Translator* t, struct line* ln, int indlen) {
|
||||
char* mkind(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
checkind(t, ln, indlen);
|
||||
int newsz = sizeof(char) * (indlen + 2);
|
||||
char* newind = (char*)malloc(newsz);
|
||||
|
@ -155,7 +139,7 @@ char* mkind(struct Translator* t, struct line* ln, int indlen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* atlab(struct Translator* t, char* label, int labellen) {
|
||||
char* atlab(TRANSLATOR* t, char* label, int labellen) {
|
||||
int newsz = sizeof(char) * (labellen + 2);
|
||||
char* newind = (char*)malloc(newsz);
|
||||
snprintf(newind, newsz, "@%s", label);
|
||||
|
@ -163,7 +147,7 @@ char* atlab(struct Translator* t, char* label, int labellen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* atn(struct Translator* t, int n) {
|
||||
char* atn(TRANSLATOR* t, int n) {
|
||||
int newsz = sizeof(char) * (countplaces(n) + 2);
|
||||
char* newind = (char*)malloc(newsz);
|
||||
snprintf(newind, newsz, "@%i", n);
|
||||
|
@ -171,7 +155,7 @@ char* atn(struct Translator* t, int n) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* mklab(struct Translator* t, char* label, int labellen) {
|
||||
char* mklab(TRANSLATOR* t, char* label, int labellen) {
|
||||
int newsz = sizeof(char) * (labellen + 3);
|
||||
char* newind = (char*)malloc(newsz);
|
||||
snprintf(newind, newsz, "(%s)", label);
|
||||
|
@ -179,7 +163,7 @@ char* mklab(struct Translator* t, char* label, int labellen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* mkgotolab(struct Translator* t, struct line* ln) {
|
||||
char* mkgotolab(TRANSLATOR* t, LINE* ln) {
|
||||
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]);
|
||||
|
@ -187,7 +171,7 @@ char* mkgotolab(struct Translator* t, struct line* ln) {
|
|||
return lab;
|
||||
}
|
||||
|
||||
char* mkstatind(struct Translator* t, struct line* ln, int indlen) {
|
||||
char* mkstatind(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
checkind(t, ln, indlen);
|
||||
int newsz = sizeof(char) * (t->fnamelen + indlen + 3);
|
||||
char* newind = (char*)malloc(newsz);
|
||||
|
@ -196,7 +180,7 @@ char* mkstatind(struct Translator* t, struct line* ln, int indlen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* mktempind(struct Translator* t, struct line* ln, int indlen) {
|
||||
char* mktempind(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
checkind(t, ln, indlen);
|
||||
int intind = atoi(ln->tokens[2]);
|
||||
int newsz = sizeof(char) * (indlen + 3);
|
||||
|
@ -206,7 +190,7 @@ char* mktempind(struct Translator* t, struct line* ln, int indlen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
char* mkpointerind(struct Translator* t, struct line* ln, int indlen) {
|
||||
char* mkpointerind(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
if(indlen > 1) {
|
||||
fprintf(stderr, "Invalid index '%s'; file %s.vm, line %i\n", t->fname, ln->tokens[2], ln->truen);
|
||||
exit(1);
|
||||
|
@ -230,15 +214,15 @@ char* mkpointerind(struct Translator* t, struct line* ln, int indlen) {
|
|||
return newind;
|
||||
}
|
||||
|
||||
void checkasmsize(struct Translator* t, int toadd) {
|
||||
int targ = sizeof(char*)*(t->asmind+toadd);
|
||||
if(targ >= t->asmsize) {
|
||||
t->asmsize = targ * 2;
|
||||
t->asmlns = (char**)realloc(t->asmlns, t->asmsize);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void checkopamnt(struct Translator* t, int amnt, struct line* ln) {
|
||||
void checkopamnt(TRANSLATOR* t, int amnt, LINE* ln) {
|
||||
if(ln->tokenscount < 2) {
|
||||
fprintf(stderr, "Missing memory segment; file %s.vm, line %i\n", t->fname, ln->truen);
|
||||
exit(1);
|
||||
|
@ -250,14 +234,14 @@ void checkopamnt(struct Translator* t, int amnt, struct line* ln) {
|
|||
}
|
||||
}
|
||||
|
||||
void checklab(struct Translator* t, struct line* ln) {
|
||||
void checklab(TRANSLATOR* t, LINE* ln) {
|
||||
if(ln->tokenscount < 2) {
|
||||
fprintf(stderr, "Expected label; file %s.vm, line %i\n", t->fname, ln->truen);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void checkfun(struct Translator* t, struct line* ln) {
|
||||
void checkfun(TRANSLATOR* t, LINE* ln) {
|
||||
if(ln->tokenscount < 2) {
|
||||
fprintf(stderr, "Expected function; file %s.vm, line %i\n", t->fname, ln->truen);
|
||||
exit(1);
|
||||
|
@ -269,130 +253,138 @@ void checkfun(struct Translator* t, struct line* ln) {
|
|||
}
|
||||
}
|
||||
|
||||
void addasm(struct Translator* t, char** insts, int instcount) {
|
||||
checkasmsize(t, instcount);
|
||||
|
||||
for(int i = 0; i < instcount; i++) {
|
||||
t->asmlns[t->asmind] = insts[i];
|
||||
t->asmind++;
|
||||
}
|
||||
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++;
|
||||
}
|
||||
|
||||
void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) {
|
||||
void addasm(TRANSLATOR* t, TEMPLATE* tp) {
|
||||
checkasmsize(t, tp->count);
|
||||
|
||||
for(int i = 0; i < tp->count; i++)
|
||||
pushln(t, tp->items[i]);
|
||||
}
|
||||
|
||||
void addasmlns(TRANSLATOR* t, LINE* ln, TEMPLATE* tp) {
|
||||
// instruction comment
|
||||
insts[0] = mkcom(t, ln);
|
||||
tp->items[0] = mkcom(t, ln);
|
||||
|
||||
addasm(t, insts, instcount);
|
||||
addasm(t, tp);
|
||||
}
|
||||
|
||||
void startpoppush(struct Translator* t, struct line* ln, int indlen, char** insts) {
|
||||
void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, TEMPLATE* tp) {
|
||||
// @segment
|
||||
insts[1] = switchseg(t, ln);
|
||||
tp->items[1] = switchseg(t, ln);
|
||||
|
||||
// D=M
|
||||
insts[2] = heapstrtoclean(t, "D=M");
|
||||
tp->items[2] = heapstrtoclean(t, "D=M");
|
||||
|
||||
// @i
|
||||
insts[3] = mkind(t, ln, indlen);
|
||||
tp->items[3] = mkind(t, ln, indlen);
|
||||
}
|
||||
|
||||
void pushcons(struct Translator* t, struct line* ln, int indlen) {
|
||||
void pushcons(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @i
|
||||
tpushcons[1] = mkind(t, ln, indlen);
|
||||
tpushcons.items[1] = mkind(t, ln, indlen);
|
||||
|
||||
addasmlns(t, ln, tpushcons, TPUSHCONSN);
|
||||
addasmlns(t, ln, &tpushcons);
|
||||
}
|
||||
|
||||
void pushstat(struct Translator* t, struct line* ln, int indlen) {
|
||||
void pushstat(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @fname.i
|
||||
tpushstat[1] = mkstatind(t, ln, indlen);
|
||||
tpushstat.items[1] = mkstatind(t, ln, indlen);
|
||||
|
||||
addasmlns(t, ln, tpushstat, TPUSHSTATN);
|
||||
addasmlns(t, ln, &tpushstat);
|
||||
}
|
||||
|
||||
void pushtemp(struct Translator* t, struct line* ln, int indlen) {
|
||||
void pushtemp(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @5+i
|
||||
tpushtemp[1] = mktempind(t, ln, indlen);
|
||||
tpushtemp.items[1] = mktempind(t, ln, indlen);
|
||||
|
||||
addasmlns(t, ln, tpushtemp, TPUSHTEMPN);
|
||||
addasmlns(t, ln, &tpushtemp);
|
||||
}
|
||||
|
||||
void pushpointer(struct Translator* t, struct line* ln, int indlen) {
|
||||
void pushpointer(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @THIS/@THAT
|
||||
tpushpointer[1] = mkpointerind(t, ln, indlen);
|
||||
tpushpointer.items[1] = mkpointerind(t, ln, indlen);
|
||||
|
||||
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN);
|
||||
addasmlns(t, ln, &tpushpointer);
|
||||
}
|
||||
|
||||
void push(struct Translator* t, struct line* ln, int indlen) {
|
||||
startpoppush(t, ln, indlen, tpush);
|
||||
void push(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
startpoppush(t, ln, indlen, &tpush);
|
||||
|
||||
addasmlns(t, ln, tpush, TPUSHN);
|
||||
addasmlns(t, ln, &tpush);
|
||||
}
|
||||
|
||||
void popstat(struct Translator* t, struct line* ln, int indlen) {
|
||||
void popstat(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @fname.i
|
||||
tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen);
|
||||
tpopstat.items[tpopstat.count-2] = mkstatind(t, ln, indlen);
|
||||
|
||||
// M=D
|
||||
tpopstat[TPOPSTATN-1] = heapstrtoclean(t, "M=D");
|
||||
tpopstat.items[tpopstat.count-1] = heapstrtoclean(t, "M=D");
|
||||
|
||||
addasmlns(t, ln, tpopstat, TPOPSTATN);
|
||||
addasmlns(t, ln, &tpopstat);
|
||||
}
|
||||
|
||||
void poptemp(struct Translator* t, struct line* ln, int indlen) {
|
||||
void poptemp(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @5+i
|
||||
tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen);
|
||||
tpoptemp.items[tpoptemp.count-2] = mktempind(t, ln, indlen);
|
||||
|
||||
// M=D
|
||||
tpoptemp[TPOPTEMPN-1] = heapstrtoclean(t, "M=D");
|
||||
tpoptemp.items[tpoptemp.count-1] = heapstrtoclean(t, "M=D");
|
||||
|
||||
addasmlns(t, ln, tpoptemp, TPOPTEMPN);
|
||||
addasmlns(t, ln, &tpoptemp);
|
||||
}
|
||||
|
||||
void poppointer(struct Translator* t, struct line* ln, int indlen) {
|
||||
void poppointer(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
// @THIS/@THAT
|
||||
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen);
|
||||
tpoppointer.items[tpoppointer.count-2] = mkpointerind(t, ln, indlen);
|
||||
|
||||
// M=D
|
||||
tpoppointer[TPOPPOINTERN-1] = heapstrtoclean(t, "M=D");
|
||||
tpoppointer.items[tpoppointer.count-1] = heapstrtoclean(t, "M=D");
|
||||
|
||||
addasmlns(t, ln, tpoppointer, TPOPPOINTERN);
|
||||
addasmlns(t, ln, &tpoppointer);
|
||||
}
|
||||
|
||||
void pop(struct Translator* t, struct line* ln, int indlen) {
|
||||
startpoppush(t, ln, indlen, tpop);
|
||||
void pop(TRANSLATOR* t, LINE* ln, int indlen) {
|
||||
startpoppush(t, ln, indlen, &tpop);
|
||||
|
||||
addasmlns(t, ln, tpop, TPOPN);
|
||||
addasmlns(t, ln, &tpop);
|
||||
}
|
||||
|
||||
void arith(struct Translator* t, struct line* ln, char* op) {
|
||||
tarith[TARITHN-1] = heapstrtoclean(t, op);
|
||||
void arith(TRANSLATOR* t, LINE* ln, char* op) {
|
||||
tarith.items[tarith.count-1] = heapstrtoclean(t, op);
|
||||
|
||||
addasmlns(t, ln, tarith, TARITHN);
|
||||
addasmlns(t, ln, &tarith);
|
||||
}
|
||||
|
||||
void comp(struct Translator* t, struct line* ln, char* op) {
|
||||
void comp(TRANSLATOR* t, LINE* ln, char* op) {
|
||||
char* label = mkcmplab(t, ln);
|
||||
int labellen = strlen(label);
|
||||
|
||||
// @label
|
||||
tcomp[TCOMPN-6] = atlab(t, label, labellen);
|
||||
tcomp.items[tcomp.count-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;
|
||||
tcomp.items[tcomp.count-5] = trueop;
|
||||
pushtoclean(t, trueop);
|
||||
|
||||
// (label)
|
||||
tcomp[TCOMPN-1] = mklab(t, label, labellen);
|
||||
tcomp.items[tcomp.count-1] = mklab(t, label, labellen);
|
||||
|
||||
addasmlns(t, ln, tcomp, TCOMPN);
|
||||
addasmlns(t, ln, &tcomp);
|
||||
};
|
||||
|
||||
void label(struct Translator* t, struct line* ln) {
|
||||
void label(TRANSLATOR* t, LINE* ln) {
|
||||
checklab(t, ln);
|
||||
|
||||
// (funcname$label)
|
||||
|
@ -401,43 +393,43 @@ void label(struct Translator* t, struct line* ln) {
|
|||
char* lab = (char*)malloc(sz);
|
||||
snprintf(lab, sz, "(%s$%s)", t->lastfun, ln->tokens[1]);
|
||||
pushtoclean(t, lab);
|
||||
tlabel[TLABELN-1] = lab;
|
||||
tlabel.items[tlabel.count-1] = lab;
|
||||
|
||||
addasmlns(t, ln, tlabel, TLABELN);
|
||||
addasmlns(t, ln, &tlabel);
|
||||
}
|
||||
|
||||
void mygoto(struct Translator* t, struct line* ln) {
|
||||
void mygoto(TRANSLATOR* t, LINE* ln) {
|
||||
checklab(t, ln);
|
||||
|
||||
// @label
|
||||
tgoto[TGOTON-2] = mkgotolab(t, ln);
|
||||
tgoto.items[tgoto.count-2] = mkgotolab(t, ln);
|
||||
|
||||
addasmlns(t, ln, tgoto, TGOTON);
|
||||
addasmlns(t, ln, &tgoto);
|
||||
}
|
||||
|
||||
void ifgoto(struct Translator* t, struct line* ln) {
|
||||
void ifgoto(TRANSLATOR* t, LINE* ln) {
|
||||
checklab(t, ln);
|
||||
|
||||
// @label
|
||||
tifgoto[TIFGOTON-2] = mkgotolab(t, ln);
|
||||
tifgoto.items[tifgoto.count-2] = mkgotolab(t, ln);
|
||||
|
||||
addasmlns(t, ln, tifgoto, TIFGOTON);
|
||||
addasmlns(t, ln, &tifgoto);
|
||||
}
|
||||
|
||||
int pushframe(struct Translator* t, struct line* ln, char* retlab, int retlablen) {
|
||||
tcallstart[1] = atlab(t, retlab, retlablen);
|
||||
int pushframe(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) {
|
||||
tcallstart.items[1] = atlab(t, retlab, retlablen);
|
||||
|
||||
addasmlns(t, ln, tcallstart, TCALLSTARTN);
|
||||
addasmlns(t, ln, &tcallstart);
|
||||
|
||||
for(int i = 0; i < TFRAMEVARSN; i++) {
|
||||
tcallpush[0] = tframevars[i];
|
||||
addasm(t, tcallpush, TCALLPUSHN);
|
||||
for(int i = 0; i < tframevars.count; i++) {
|
||||
tcallpush.items[0] = tframevars.items[i];
|
||||
addasm(t, &tcallpush);
|
||||
}
|
||||
|
||||
return TFRAMEVARSN + 1;
|
||||
return tframevars.count + 1;
|
||||
}
|
||||
|
||||
void call(struct Translator* t, struct line* ln) {
|
||||
void call(TRANSLATOR* t, LINE* ln) {
|
||||
checkfun(t, ln);
|
||||
|
||||
// return label
|
||||
|
@ -451,17 +443,17 @@ void call(struct Translator* t, struct line* ln) {
|
|||
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);
|
||||
tcallsetarg.items[tcallsetarg.count-4] = atn(t, nargs + framesize);
|
||||
addasm(t, &tcallsetarg);
|
||||
|
||||
// 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);
|
||||
tcalljmp.items[tcalljmp.count-3] = atlab(t, ln->tokens[1], jmplen);
|
||||
tcalljmp.items[tcalljmp.count-1] = mklab(t, retlab, retlablen);
|
||||
addasm(t, &tcalljmp);
|
||||
}
|
||||
|
||||
void function(struct Translator* t, struct line* ln) {
|
||||
void function(TRANSLATOR* t, LINE* ln) {
|
||||
if(!t->returned) {
|
||||
fprintf(stderr, "Last function did not return; file %s.vm, line %i\n", t->fname, ln->truen);
|
||||
exit(1);
|
||||
|
@ -477,8 +469,8 @@ void function(struct Translator* t, struct line* ln) {
|
|||
t->cmpind = 0;
|
||||
|
||||
// (funcname)
|
||||
tfunction[1] = mklab(t, ln->tokens[1], funlen);
|
||||
addasmlns(t, ln, tfunction, TFUNCTIONN);
|
||||
tfunction.items[1] = mklab(t, ln->tokens[1], funlen);
|
||||
addasmlns(t, ln, &tfunction);
|
||||
|
||||
// repeat nVars times:
|
||||
int nlocalslen = strlen(ln->tokens[2]);
|
||||
|
@ -486,22 +478,22 @@ void function(struct Translator* t, struct line* ln) {
|
|||
int nlocals = atoi(ln->tokens[2]);
|
||||
|
||||
for(int i = 0; i < nlocals; i++) {
|
||||
addasm(t, tfunctionpush, TFUNCTIONPUSHN);
|
||||
addasm(t, &tfunctionpush);
|
||||
}
|
||||
}
|
||||
|
||||
void myreturn(struct Translator* t, struct line* ln) {
|
||||
addasmlns(t, ln, tstartreturn, TSTARTRETURNN);
|
||||
void myreturn(TRANSLATOR* t, LINE* ln) {
|
||||
addasmlns(t, ln, &tstartreturn);
|
||||
|
||||
for(int i = TFRAMEVARSN-1; i >= 0; i--) {
|
||||
tretpop[TRETPOPN-2] = tframevars[i];
|
||||
addasm(t, tretpop, TRETPOPN);
|
||||
for(int i = tframevars.count-1; i >= 0; i--) {
|
||||
tretpop.items[tretpop.count-2] = tframevars.items[i];
|
||||
addasm(t, &tretpop);
|
||||
}
|
||||
|
||||
addasm(t, tendreturn, TENDRETURNN);
|
||||
addasm(t, &tendreturn);
|
||||
}
|
||||
|
||||
void switchpush(struct Translator* t, struct line* ln) {
|
||||
void switchpush(TRANSLATOR* t, LINE* ln) {
|
||||
checkopamnt(t, 3, ln);
|
||||
char* seg = ln->tokens[1];
|
||||
int indlen = strlen(ln->tokens[2]);
|
||||
|
@ -518,7 +510,7 @@ void switchpush(struct Translator* t, struct line* ln) {
|
|||
push(t, ln, indlen);
|
||||
}
|
||||
|
||||
void switchpop(struct Translator* t, struct line* ln) {
|
||||
void switchpop(TRANSLATOR* t, LINE* ln) {
|
||||
checkopamnt(t, 3, ln);
|
||||
char* seg = ln->tokens[1];
|
||||
int indlen = strlen(ln->tokens[2]);
|
||||
|
@ -533,9 +525,9 @@ void switchpop(struct Translator* t, struct line* ln) {
|
|||
pop(t, ln, indlen);
|
||||
}
|
||||
|
||||
void switchop(struct Translator* t, struct line* ln) {
|
||||
void switchop(TRANSLATOR* t, LINE* ln) {
|
||||
char* op = ln->tokens[0];
|
||||
short returned = 0;
|
||||
bool returned = false;
|
||||
|
||||
if(!strcmp(op, "push"))
|
||||
switchpush(t, ln);
|
||||
|
@ -546,7 +538,7 @@ void switchop(struct Translator* t, struct line* ln) {
|
|||
else if(!strcmp(op, "sub"))
|
||||
arith(t, ln, "M=M-D");
|
||||
else if(!strcmp(op, "neg"))
|
||||
addasmlns(t, ln, tneg, TNEGN);
|
||||
addasmlns(t, ln, &tneg);
|
||||
else if(!strcmp(op, "eq"))
|
||||
comp(t, ln, "EQ");
|
||||
else if(!strcmp(op, "gt"))
|
||||
|
@ -558,7 +550,7 @@ void switchop(struct Translator* t, struct line* ln) {
|
|||
else if(!strcmp(op, "or"))
|
||||
arith(t, ln, "M=D|M");
|
||||
else if(!strcmp(op, "not"))
|
||||
addasmlns(t, ln, tnot, TNOTN);
|
||||
addasmlns(t, ln, &tnot);
|
||||
else if(!strcmp(op, "label"))
|
||||
label(t, ln);
|
||||
else if(!strcmp(op, "goto"))
|
||||
|
@ -567,7 +559,7 @@ void switchop(struct Translator* t, struct line* ln) {
|
|||
ifgoto(t, ln);
|
||||
else if(!strcmp(op, "return")) {
|
||||
myreturn(t, ln);
|
||||
returned = 1;
|
||||
returned = true;
|
||||
}
|
||||
else if(!strcmp(op, "function"))
|
||||
function(t, ln);
|
||||
|
@ -581,7 +573,7 @@ void switchop(struct Translator* t, struct line* ln) {
|
|||
t->returned = returned;
|
||||
}
|
||||
|
||||
void translate(struct Translator* t) {
|
||||
void translate(TRANSLATOR* t) {
|
||||
for(int i = 0; i < t->lns->count; i++)
|
||||
switchop(t, t->lns->lns[i]);
|
||||
|
||||
|
@ -589,21 +581,32 @@ void translate(struct Translator* t) {
|
|||
fprintf(stderr, "Expected return before end of file; file %s.vm, line %i\n", t->fname, t->lns->count-1);
|
||||
exit(1);
|
||||
}
|
||||
t->lastln->next = NULL;
|
||||
free(t->curln);
|
||||
}
|
||||
|
||||
struct Translator* mktranslator(struct lnarray* lns, char* fname) {
|
||||
struct Translator* t = (struct Translator*)malloc(sizeof(struct Translator));
|
||||
t->asmsize = sizeof(char*)*(lns->count * 15);
|
||||
t->asmind = 0;
|
||||
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);
|
||||
|
||||
LINELIST* newln = (LINELIST*)malloc(sizeof(LINELIST));
|
||||
t->output = newln;
|
||||
t->curln = newln;
|
||||
t->lncount = 0;
|
||||
|
||||
t->funcount = 0;
|
||||
t->retind = 0;
|
||||
t->cmpind = 0;
|
||||
t->returned = 1;
|
||||
t->asmlns = (char**)malloc(t->asmsize);
|
||||
|
||||
t->tocleanind = 0;
|
||||
t->tocleansize = sizeof(char*)*(lns->count * 5);
|
||||
t->toclean = (char**)malloc(t->tocleansize);
|
||||
t->returned = true;
|
||||
|
||||
t->lns = lns;
|
||||
t->fname = fname;
|
||||
|
|
40
translator.h
40
translator.h
|
@ -1,15 +1,19 @@
|
|||
#ifndef translator
|
||||
#define translator
|
||||
#ifndef TRANSLATOR_H
|
||||
#define TRANSLATOR_H
|
||||
#include <stdbool.h>
|
||||
#include "parser.h"
|
||||
#include "util.h"
|
||||
|
||||
struct Translator {
|
||||
char** asmlns;
|
||||
int asmind;
|
||||
int asmsize;
|
||||
char** toclean;
|
||||
int tocleansize;
|
||||
int tocleanind;
|
||||
struct lnarray* lns;
|
||||
typedef struct {
|
||||
char** items;
|
||||
int count;
|
||||
int size;
|
||||
} STRLIST;
|
||||
|
||||
typedef struct {
|
||||
STRLIST* asmlns;
|
||||
STRLIST* toclean;
|
||||
LINEARRAY* lns;
|
||||
char* fname;
|
||||
int fnamelen;
|
||||
char* lastfun;
|
||||
|
@ -17,11 +21,15 @@ struct Translator {
|
|||
int funcount;
|
||||
int retind;
|
||||
int cmpind;
|
||||
short returned;
|
||||
};
|
||||
bool returned;
|
||||
|
||||
void freetranslator(struct Translator* t);
|
||||
void printasmlns(struct Translator* t, FILE* stream);
|
||||
void translate(struct Translator* t);
|
||||
struct Translator* mktranslator(struct lnarray* lns, char* fname);
|
||||
LINELIST* output;
|
||||
LINELIST* curln;
|
||||
LINELIST* lastln;
|
||||
int lncount;
|
||||
} TRANSLATOR;
|
||||
|
||||
void freetranslator(TRANSLATOR* t);
|
||||
void translate(TRANSLATOR* t);
|
||||
TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname);
|
||||
#endif
|
||||
|
|
29
util.c
29
util.c
|
@ -8,3 +8,32 @@ char* heapstr(const char* str, int len) {
|
|||
strcpy(outstr, str);
|
||||
return outstr;
|
||||
}
|
||||
|
||||
int countplaces(int n) {
|
||||
int places = 1;
|
||||
int divisor = 1;
|
||||
if(n < 0) {
|
||||
n = -n;
|
||||
places++;
|
||||
}
|
||||
while(n / divisor >= 10) {
|
||||
places++;
|
||||
divisor *= 10;
|
||||
}
|
||||
return places;
|
||||
}
|
||||
|
||||
void printlns(LINELIST* lns, FILE* stream) {
|
||||
LINELIST* curln = lns;
|
||||
while(curln != NULL) {
|
||||
fprintf(stream, "%s\n", curln->content);
|
||||
curln = curln->next;
|
||||
}
|
||||
}
|
||||
|
||||
void freelns(LINELIST* lns) {
|
||||
LINELIST* next = lns->next;
|
||||
free(lns);
|
||||
if(next != NULL)
|
||||
freelns(next);
|
||||
}
|
||||
|
|
16
util.h
16
util.h
|
@ -1 +1,17 @@
|
|||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
char* heapstr(const char* str, int len);
|
||||
int countplaces(int n);
|
||||
|
||||
typedef struct lnls {
|
||||
char* content;
|
||||
int truen;
|
||||
struct lnls* next;
|
||||
} LINELIST;
|
||||
|
||||
void printlns(LINELIST* lns, FILE* stream);
|
||||
void freelns(LINELIST* lns);
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue