Finish stack manipulation

This commit is contained in:
Augusto Gunsch 2020-11-01 12:40:43 -03:00
parent b5408d3a7e
commit 54eb1daee6
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 159 additions and 117 deletions

View File

@ -1,7 +1,7 @@
#ifndef templates #ifndef templates
#define templates #define templates
#define TPUSHN 11 #define TPUSHN 10
char* tpush[TPUSHN] = { char* tpush[TPUSHN] = {
"", "",
"", "",
@ -10,47 +10,38 @@ char* tpush[TPUSHN] = {
"A=D+A", "A=D+A",
"D=M", "D=M",
"@SP", "@SP",
"A=M", "M=M+1",
"M=D", "A=M-1",
"@SP", "M=D"
"M=M+1"
}; };
#define TPUSHCONSN 8 #define TPUSHCONSN 7
char* tpushcons[TPUSHCONSN] = { char* tpushcons[TPUSHCONSN] = {
"", "",
"", "",
"D=A", "D=A",
"@SP", "@SP",
"A=M", "M=M+1",
"M=D", "A=M-1",
"@SP", "M=D"
"M=M+1"
}; };
#define TPUSHSTATN 8 #define TPUSHSTATN 7
char* tpushstat[TPUSHSTATN] = { char* tpushstat[TPUSHSTATN] = {
"", "",
"", "",
"D=M", "D=M",
"@SP", "@SP",
"A=M", "M=M+1",
"M=D", "A=M-1",
"@SP", "M=D"
"M=M+1"
}; };
#define TPUSHTEMP 8 #define TPUSHTEMPN TPUSHSTATN
char* tpushtemp[TPUSHTEMP] = { char** tpushtemp = tpushstat;
"",
"", #define TPUSHPOINTERN TPUSHSTATN
"D=M", char** tpushpointer = tpushstat;
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
#define TPOPN 14 #define TPOPN 14
char* tpop[TPOPN] = { char* tpop[TPOPN] = {
@ -70,13 +61,12 @@ char* tpop[TPOPN] = {
"M=D" "M=D"
}; };
#define TPOPSTATN 8 #define TPOPSTATN 7
char* tpopstat[TPOPSTATN] = { char* tpopstat[TPOPSTATN] = {
"", "",
"@SP", "@SP",
"D=M",
"M=M-1", "M=M-1",
"A=D", "A=M+1",
"D=M", "D=M",
"", "",
"" ""
@ -93,4 +83,15 @@ char* tpoptemp[TPOPTEMPN] = {
"" ""
}; };
#define TPOPPOINTERN 7
char* tpoppointer[TPOPPOINTERN] = {
"",
"@SP",
"M=M-1",
"A=M",
"D=M",
"",
"M=D"
};
#endif #endif

View File

@ -51,62 +51,113 @@ char* heapstr(struct Translator* t, const char* input) {
char* switchseg(struct Translator* t, struct line* ln) { char* switchseg(struct Translator* t, struct line* ln) {
char* seg = ln->tokens[1]; char* seg = ln->tokens[1];
if(strcmp(seg, "local") == 0) if(!strcmp(seg, "local"))
return heapstr(t, "@LCL"); return heapstr(t, "@LCL");
if(strcmp(seg, "argument") == 0) if(!strcmp(seg, "argument"))
return heapstr(t, "@ARG"); return heapstr(t, "@ARG");
if(strcmp(seg, "this") == 0) if(!strcmp(seg, "this"))
return heapstr(t, "@THIS"); return heapstr(t, "@THIS");
if(strcmp(seg, "that") == 0) if(!strcmp(seg, "that"))
return heapstr(t, "@THAT"); return heapstr(t, "@THAT");
fprintf(stderr, "Unrecognized segment '%s'; line %i\n", seg, ln->truen); fprintf(stderr, "Unrecognized segment '%s'; line %i\n", seg, ln->truen);
exit(1); exit(1);
} }
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;
}
// produce comment as follows: // produce comment as follows:
// pop/push segment i // pop/push segment i
char* mkcom(struct Translator* t, struct line* ln, int indlen) { char* mkcom(struct Translator* t, struct line* ln) {
int comlen = sizeof(char) * (strlen(ln->tokens[0]) + strlen(ln->tokens[1]) + indlen + 6); int lens[ln->tokenscount];
int comlen = sizeof(char) * lnlen(lens, ln) + ln->tokenscount + 3;
char* comment = (char*)malloc(comlen); char* comment = (char*)malloc(comlen);
snprintf(comment, comlen, "// %s %s %s", ln->tokens[0], ln->tokens[1], ln->tokens[2]);
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';
pushtoclean(t, comment); pushtoclean(t, comment);
return comment; return comment;
} }
void checkind(struct line* ln, int indsz) { void checkind(struct line* ln, int indlen) {
for(int i = 0; i < indsz; i++) for(int i = 0; i < indlen; i++)
if(!isdigit(ln->tokens[2][i])) { if(!isdigit(ln->tokens[2][i])) {
fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen); fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen);
exit(1); exit(1);
} }
} }
char* mkind(struct Translator* t, struct line* ln, int indsz) { char* mkind(struct Translator* t, struct line* ln, int indlen) {
int newsz = sizeof(char) * (indsz + 2); checkind(ln, indlen);
int newsz = sizeof(char) * (indlen + 2);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ln->tokens[2]); snprintf(newind, newsz, "@%s", ln->tokens[2]);
pushtoclean(t, newind); pushtoclean(t, newind);
return newind; return newind;
} }
char* mkstatind(struct Translator* t, struct line* ln, int indsz) { char* mkstatind(struct Translator* t, struct line* ln, int indlen) {
checkind(ln, indlen);
int fnamelen = strlen(t->fname); int fnamelen = strlen(t->fname);
int newsz = sizeof(char) * (fnamelen + indsz + 3); int newsz = sizeof(char) * (fnamelen + indlen + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s.%s", t->fname, ln->tokens[2]); snprintf(newind, newsz, "@%s.%s", t->fname, ln->tokens[2]);
pushtoclean(t, newind); pushtoclean(t, newind);
return newind; return newind;
} }
char* mktempind(struct Translator* t, struct line* ln, int indsz) { char* mktempind(struct Translator* t, struct line* ln, int indlen) {
checkind(ln, indlen);
int intind = atoi(ln->tokens[2]); int intind = atoi(ln->tokens[2]);
int newsz = sizeof(char) * (indsz + 3); int newsz = sizeof(char) * (indlen + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", intind+5); snprintf(newind, newsz, "@%i", intind+5);
pushtoclean(t, newind); pushtoclean(t, newind);
return newind; return newind;
} }
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;
}
void checkasmsize(struct Translator* t, int toadd) { void checkasmsize(struct Translator* t, int toadd) {
int targ = sizeof(struct asmln*)*(t->asmind+toadd); int targ = sizeof(struct asmln*)*(t->asmind+toadd);
if(targ >= t->asmsize) { if(targ >= t->asmsize) {
@ -135,6 +186,11 @@ void checkopamnt(int amnt, struct line* ln) {
} }
void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) { void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) {
checkasmsize(t, instcount);
// instruction comment
insts[0] = mkcom(t, ln);
for(int i = 0; i < instcount; i++) { for(int i = 0; i < instcount; i++) {
t->asmlns[t->asmind] = mkasmln(ln, insts[i]); t->asmlns[t->asmind] = mkasmln(ln, insts[i]);
t->asmind++; t->asmind++;
@ -142,9 +198,6 @@ void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcoun
} }
void startpoppush(struct Translator* t, struct line* ln, int indlen, char** insts) { void startpoppush(struct Translator* t, struct line* ln, int indlen, char** insts) {
// // operation segment i
insts[0] = mkcom(t, ln, indlen);
// @segment // @segment
insts[1] = switchseg(t, ln); insts[1] = switchseg(t, ln);
@ -152,130 +205,118 @@ void startpoppush(struct Translator* t, struct line* ln, int indlen, char** inst
insts[2] = heapstr(t, "D=M"); insts[2] = heapstr(t, "D=M");
// @i // @i
checkind(ln, indlen);
insts[3] = mkind(t, ln, indlen); insts[3] = mkind(t, ln, indlen);
} }
void pushcons(struct Translator* t, struct line* ln, int indlen) { void pushcons(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPUSHCONSN);
// // push constant i
tpushcons[0] = mkcom(t, ln, indlen);
// @i // @i
checkind(ln, indlen);
tpushcons[1] = mkind(t, ln, indlen); tpushcons[1] = mkind(t, ln, indlen);
addasmlns(t, ln, tpushcons, TPUSHCONSN); addasmlns(t, ln, tpushcons, TPUSHCONSN);
} }
void pushstat(struct Translator* t, struct line* ln, int indlen) { void pushstat(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPUSHSTATN);
// // push static i
tpushstat[0] = mkcom(t, ln, indlen);
// @fname.i // @fname.i
checkind(ln, indlen);
tpushstat[1] = mkstatind(t, ln, indlen); tpushstat[1] = mkstatind(t, ln, indlen);
addasmlns(t, ln, tpushstat, TPUSHSTATN); addasmlns(t, ln, tpushstat, TPUSHSTATN);
} }
void pushtemp(struct Translator* t, struct line* ln, int indlen) { void pushtemp(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPUSHSTATN);
// // pop static i
tpushtemp[0] = mkcom(t, ln, indlen);
// @5+i // @5+i
checkind(ln, indlen);
tpushtemp[1] = mktempind(t, ln, indlen); tpushtemp[1] = mktempind(t, ln, indlen);
addasmlns(t, ln, tpushtemp, TPUSHTEMP); addasmlns(t, ln, tpushtemp, TPUSHTEMPN);
}
void pushpointer(struct Translator* t, struct line* ln, int indlen) {
// @THIS/@THAT
tpushpointer[1] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN);
} }
void push(struct Translator* t, struct line* ln, int indlen) { void push(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPUSHN);
startpoppush(t, ln, indlen, tpush); startpoppush(t, ln, indlen, tpush);
addasmlns(t, ln, tpush, TPUSHN); addasmlns(t, ln, tpush, TPUSHN);
} }
void popstat(struct Translator* t, struct line* ln, int indlen) { void popstat(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPOPSTATN);
// // pop static i
tpopstat[0] = mkcom(t, ln, indlen);
// @fname.i // @fname.i
checkind(ln, indlen); tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen);
tpopstat[6] = mkstatind(t, ln, indlen);
// M=D // M=D
tpopstat[7] = heapstr(t, "M=D"); tpopstat[TPOPSTATN-1] = heapstr(t, "M=D");
addasmlns(t, ln, tpopstat, TPOPSTATN); addasmlns(t, ln, tpopstat, TPOPSTATN);
} }
void poptemp(struct Translator* t, struct line* ln, int indlen) { void poptemp(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPOPTEMPN);
// // pop static i
tpoptemp[0] = mkcom(t, ln, indlen);
// @5+i // @5+i
checkind(ln, indlen); tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen);
tpoptemp[5] = mktempind(t, ln, indlen);
// M=D // M=D
tpoptemp[6] = heapstr(t, "M=D"); tpoptemp[TPOPTEMPN-1] = heapstr(t, "M=D");
addasmlns(t, ln, tpoptemp, TPOPTEMPN); addasmlns(t, ln, tpoptemp, TPOPTEMPN);
} }
void pop(struct Translator* t, struct line* ln, int indlen) { void poppointer(struct Translator* t, struct line* ln, int indlen) {
checkasmsize(t, TPOPN); // @THIS/@THAT
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpoppointer, TPOPPOINTERN);
}
void pop(struct Translator* t, struct line* ln, int indlen) {
startpoppush(t, ln, indlen, tpop); startpoppush(t, ln, indlen, tpop);
addasmlns(t, ln, tpop, TPOPN); addasmlns(t, ln, tpop, TPOPN);
} }
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);
}
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);
}
void switchop(struct Translator* t, struct line* ln) { void switchop(struct Translator* t, struct line* ln) {
char* op = ln->tokens[0]; char* op = ln->tokens[0];
if(strcmp(op, "push") == 0) { if(!strcmp(op, "push"))
checkopamnt(3, ln); switchpush(t, ln);
char* seg = ln->tokens[1]; else if(!strcmp(op, "pop"))
int indlen = strlen(ln->tokens[2]); switchpop(t, ln);
else {
if(strcmp(seg, "constant") == 0) fprintf(stderr, "Unrecognized operation '%s'; line %i\n", op, ln->truen);
pushcons(t, ln, indlen); exit(1);
else if(strcmp(seg, "static") == 0)
pushstat(t, ln, indlen);
else if(strcmp(seg, "temp") == 0)
pushtemp(t, ln, indlen);
else
push(t, ln, indlen);
}
else if(strcmp(op, "pop") == 0) {
checkopamnt(3, ln);
char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]);
if(strcmp(seg, "static") == 0)
popstat(t, ln, indlen);
else if(strcmp(seg, "temp") == 0)
poptemp(t, ln, indlen);
else
pop(t, ln, indlen);
} }
} }