Severe refactoring

This commit is contained in:
Augusto Gunsch 2020-10-31 22:53:11 -03:00
parent 46c2a83735
commit 41e322825f
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
4 changed files with 257 additions and 247 deletions

9
main.c
View File

@ -53,20 +53,17 @@ int main(int argc, char* argv[]) {
fclose(input); fclose(input);
// translating // translating
int asmcount = 0; translate(lns, lncount, fname);
struct asmln** asmlns = translate(lns, lncount, &asmcount, fname);
// freeing lns // freeing lns
freelns(lns, lncount); freelns(lns, lncount);
// printing // printing
for(int i = 0; i < asmcount; i++) { printasmlns();
printf("%s\n", asmlns[i]->instr);
}
// freeing asmlns // freeing asmlns
free(fname); free(fname);
freeasmlns(asmlns, asmcount); freeasmlns();
return 0; return 0;
} }

96
templates.h Normal file
View File

@ -0,0 +1,96 @@
#ifndef templates
#define templates
#define TPUSHN 11
char* tpush[TPUSHN] = {
"",
"",
"",
"",
"A=D+A",
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
#define TPUSHCONSN 8
char* tpushcons[TPUSHCONSN] = {
"",
"",
"D=A",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
#define TPUSHSTATN 8
char* tpushstat[TPUSHSTATN] = {
"",
"",
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
#define TPUSHTEMP 8
char* tpushtemp[TPUSHTEMP] = {
"",
"",
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
#define TPOPN 14
char* tpop[TPOPN] = {
"",
"",
"",
"",
"D=D+A",
"@R13",
"M=D",
"@SP",
"M=M-1",
"A=M",
"D=M",
"@R13",
"A=M",
"M=D"
};
#define TPOPSTATN 8
char* tpopstat[TPOPSTATN] = {
"",
"@SP",
"D=M",
"M=M-1",
"A=D",
"D=M",
"",
""
};
#define TPOPTEMPN 7
char* tpoptemp[TPOPTEMPN] = {
"",
"@SP",
"M=M-1",
"A=M",
"D=M",
"",
""
};
#endif

View File

@ -3,18 +3,54 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "translator.h" #include "translator.h"
#include "templates.h"
void freeasmlns(struct asmln** lns, int count) { struct asmln** asmlns;
for(int i = 0; i < count; i++) { int asmind;
free(lns[i]->instr); int asmsize;
free(lns[i]); char** toclean;
int tocleansize;
int tocleanind;
void pushtoclean(char* topush) {
int nextsz = sizeof(char*)*(tocleanind+1);
if(nextsz >= tocleansize) {
tocleansize = nextsz * 2;
toclean = realloc(toclean, tocleansize);
}
toclean[tocleanind] = topush;
tocleanind++;
}
void freetoclean() {
for(int i = 0; i < tocleanind; i++) {
free(toclean[i]);
}
free(toclean);
tocleansize = 0;
tocleanind = 0;
}
void freeasmlns() {
for(int i = 0; i < asmind; i++) {
free(asmlns[i]);
}
free(asmlns);
asmsize = 0;
asmind = 0;
freetoclean();
}
void printasmlns() {
for(int i = 0; i < asmind; i++) {
printf("%s\n", asmlns[i]->instr);
} }
free(lns);
} }
char* heapstr(const char* input) { char* heapstr(const char* input) {
char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1)); char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1));
strcpy(newstr, input); strcpy(newstr, input);
pushtoclean(newstr);
return newstr; return newstr;
} }
@ -38,6 +74,7 @@ char* mkcom(struct line* ln, int indlen) {
int comlen = sizeof(char) * (strlen(ln->tokens[0]) + strlen(ln->tokens[1]) + indlen + 6); int comlen = sizeof(char) * (strlen(ln->tokens[0]) + strlen(ln->tokens[1]) + indlen + 6);
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]); snprintf(comment, comlen, "// %s %s %s", ln->tokens[0], ln->tokens[1], ln->tokens[2]);
pushtoclean(comment);
return comment; return comment;
} }
@ -50,33 +87,37 @@ void checkind(struct line* ln, int indsz) {
} }
} }
char* mkind(char* ind, int indsz) { char* mkind(struct line* ln, int indsz) {
int newsz = sizeof(char) * (indsz + 2); int newsz = sizeof(char) * (indsz + 2);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ind); snprintf(newind, newsz, "@%s", ln->tokens[2]);
pushtoclean(newind);
return newind; return newind;
} }
char* mkstatind(char* ind, int indsz, char* fname) { char* mkstatind(struct line* ln, int indsz, char* fname) {
int fnamelen = strlen(fname); int fnamelen = strlen(fname);
int newsz = sizeof(char) * (fnamelen + indsz + 3); int newsz = sizeof(char) * (fnamelen + indsz + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s.%s", fname, ind); snprintf(newind, newsz, "@%s.%s", fname, ln->tokens[2]);
pushtoclean(newind);
return newind; return newind;
} }
char* mktempind(char* ind, int indsz) { char* mktempind(struct line* ln, int indsz) {
int intind = atoi(ind); int intind = atoi(ln->tokens[2]);
int newsz = sizeof(char) * (indsz + 3); int newsz = sizeof(char) * (indsz + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", intind+5); snprintf(newind, newsz, "@%i", intind+5);
pushtoclean(newind);
return newind; return newind;
} }
void checkasmsize(int* size, struct asmln*** lns, int targ) { void checkasmsize(int toadd) {
if(targ >= (*size)) { int targ = sizeof(struct asmln*)*(asmind+toadd);
(*size)=targ*2; if(targ >= asmsize) {
(*lns) = (struct asmln**)realloc((*lns), (*size)); asmsize = targ * 2;
asmlns = (struct asmln**)realloc(asmlns, asmsize);
} }
} }
@ -99,288 +140,162 @@ void checkopamnt(int amnt, struct line* ln) {
} }
} }
void startpoppush(struct line* ln, struct asmln*** asmlns, int* asmind) { void addasmlns(struct line* ln, char** insts, int instcount) {
checkopamnt(3, ln); for(int i = 0; i < instcount; i++) {
int asmi = *asmind; asmlns[asmind] = mkasmln(ln, insts[i]);
asmind++;
}
}
void startpoppush(struct line* ln, int indlen, char** insts) {
// // operation segment i // // operation segment i
int indlen = strlen(ln->tokens[2]); insts[0] = mkcom(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @segment // @segment
(*asmlns)[asmi] = mkasmln(ln, switchseg(ln)); insts[1] = switchseg(ln);
asmi++;
// D=M // D=M
(*asmlns)[asmi] = mkasmln(ln, heapstr("D=M")); insts[2] = heapstr("D=M");
asmi++;
// @i // @i
checkind(ln, indlen); checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen)); insts[3] = mkind(ln, indlen);
asmi++;
(*asmind) = asmi;
} }
void pushcons(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) { void pushcons(struct line* ln, int indlen) {
checkopamnt(3, ln); checkasmsize(TPUSHCONSN);
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // push constant i // // push constant i
int indlen = strlen(ln->tokens[2]); tpushcons[0] = mkcom(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @i // @i
checkind(ln, indlen); checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen)); tpushcons[1] = mkind(ln, indlen);
asmi++;
const int instcount = 6; addasmlns(ln, tpushcons, TPUSHCONSN);
const char* instrs[] = {
"D=A",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
} }
(*asmind) = asmi; void pushstat(struct line* ln, int indlen, char* fname) {
} checkasmsize(TPUSHSTATN);
void pushstat(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) {
checkopamnt(3, ln);
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // push static i // // push static i
int indlen = strlen(ln->tokens[2]); tpushstat[0] = mkcom(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @fname.i // @fname.i
checkind(ln, indlen); checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkstatind(ln->tokens[2], indlen, fname)); tpushstat[1] = mkstatind(ln, indlen, fname);
asmi++;
const int instcount = 6; addasmlns(ln, tpushstat, TPUSHSTATN);
const char* instrs[] = {
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
} }
(*asmind) = asmi; void pushtemp(struct line* ln, int indlen) {
checkasmsize(TPUSHSTATN);
// // pop static i
tpushtemp[0] = mkcom(ln, indlen);
// @5+i
checkind(ln, indlen);
tpushtemp[1] = mktempind(ln, indlen);
addasmlns(ln, tpushtemp, TPUSHTEMP);
} }
void popstat(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) { void mkpush(struct line* ln, int indlen) {
checkasmsize(TPUSHN);
startpoppush(ln, indlen, tpush);
addasmlns(ln, tpush, TPUSHN);
}
void popstat(struct line* ln, int indlen, char* fname) {
checkasmsize(TPOPSTATN);
// // pop static i
tpopstat[0] = mkcom(ln, indlen);
// @fname.i
checkind(ln, indlen);
tpopstat[6] = mkstatind(ln, indlen, fname);
// M=D
tpopstat[7] = heapstr("M=D");
addasmlns(ln, tpopstat, TPOPSTATN);
}
void poptemp(struct line* ln, int indlen) {
checkasmsize(TPOPTEMPN);
// // pop static i
tpoptemp[0] = mkcom(ln, indlen);
// @5+i
checkind(ln, indlen);
tpoptemp[5] = mktempind(ln, indlen);
// M=D
tpoptemp[6] = heapstr("M=D");
addasmlns(ln, tpoptemp, TPOPTEMPN);
}
void mkpop(struct line* ln, int indlen) {
checkasmsize(TPOPN);
startpoppush(ln, indlen, tpop);
addasmlns(ln, tpop, TPOPN);
}
void switchop(struct line* ln, char* fname) {
char* op = ln->tokens[0];
if(strcmp(op, "push") == 0) {
checkopamnt(3, ln); checkopamnt(3, ln);
int asmi = *asmind; char* seg = ln->tokens[1];
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // pop static i
int indlen = strlen(ln->tokens[2]); int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
const int instcount = 5; if(strcmp(seg, "constant") == 0)
const char* instrs[] = { pushcons(ln, indlen);
"@SP",
"D=M",
"M=M-1",
"A=D",
"D=M"
};
for(int i = 0; i < instcount; i++) { else if(strcmp(seg, "static") == 0)
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i])); pushstat(ln, indlen, fname);
asmi++;
}
// @fname.i else if(strcmp(seg, "temp") == 0)
checkind(ln, indlen); pushtemp(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkstatind(ln->tokens[2], indlen, fname));
asmi++;
// M=D
(*asmlns)[asmi] = mkasmln(ln, heapstr("M=D"));
asmi++;
(*asmind) = asmi;
}
void pushtemp(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // pop static i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @5+i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mktempind(ln->tokens[2], indlen));
asmi++;
const int instcount = 6;
const char* instrs[] = {
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
}
(*asmind) = asmi;
}
void mkpush(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int totalinstrs = 11;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
startpoppush(ln, asmlns, &asmi);
const int instcount = 7;
const char* instrs[] = {
"A=D+A",
"D=M",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
}
(*asmind) = asmi;
}
void poptemp(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int totalinstrs = 7;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // pop static i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
const int instcount = 4;
const char* instrs[] = {
"@SP",
"M=M-1",
"A=M",
"D=M"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
}
// @5+i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mktempind(ln->tokens[2], indlen));
asmi++;
// M=D
(*asmlns)[asmi] = mkasmln(ln, heapstr("M=D"));
asmi++;
(*asmind) = asmi;
}
void mkpop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int totalinstrs = 14;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
startpoppush(ln, asmlns, &asmi);
const int instcount = 10;
const char* instrs[] = {
"D=D+A",
"@R13",
"M=D",
"@SP",
"M=M-1",
"A=M",
"D=M",
"@R13",
"A=M",
"M=D"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
}
(*asmind) = asmi;
}
void switchop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) {
if(strcmp(ln->tokens[0], "push") == 0)
if(strcmp(ln->tokens[1], "constant") == 0)
pushcons(ln, asmlns, asmind, asmsize);
else if(strcmp(ln->tokens[1], "static") == 0)
pushstat(ln, asmlns, asmind, asmsize, fname);
else if(strcmp(ln->tokens[1], "temp") == 0)
pushtemp(ln, asmlns, asmind, asmsize);
else else
mkpush(ln, asmlns, asmind, asmsize); mkpush(ln, indlen);
else if(strcmp(ln->tokens[0], "pop") == 0) }
if(strcmp(ln->tokens[1], "static") == 0) else if(strcmp(op, "pop") == 0) {
popstat(ln, asmlns, asmind, asmsize, fname); checkopamnt(3, ln);
else if(strcmp(ln->tokens[1], "temp") == 0) char* seg = ln->tokens[1];
poptemp(ln, asmlns, asmind, asmsize); int indlen = strlen(ln->tokens[2]);
if(strcmp(seg, "static") == 0)
popstat(ln, indlen, fname);
else if(strcmp(seg, "temp") == 0)
poptemp(ln, indlen);
else else
mkpop(ln, asmlns, asmind, asmsize); mkpop(ln, indlen);
}
} }
struct asmln** translate(struct line** lns, int lnscount, int* asmcount, char* fname) { void translate(struct line** lns, int lnscount, char* fname) {
int sizebet = sizeof(struct asmln*)*(lnscount * 15); asmsize = sizeof(struct asmln*)*(lnscount * 15);
int asmi = (*asmcount); asmind = 0;
struct asmln** asmlns = (struct asmln**)malloc(sizebet); asmlns = (struct asmln**)malloc(asmsize);
tocleanind = 0;
tocleansize = sizeof(char*)*(lnscount * 5);
toclean = (char**)malloc(tocleansize);
for(int i = 0; i < lnscount; i++) { for(int i = 0; i < lnscount; i++) {
struct line* ln = lns[i]; struct line* ln = lns[i];
switchop(ln, &asmlns, &asmi, &sizebet, fname); switchop(ln, fname);
} }
(*asmcount) = asmi;
return asmlns;
} }

View File

@ -1,11 +1,13 @@
#ifndef translator #ifndef translator
#define translator #define translator
#include "parser.h" #include "parser.h"
struct asmln { struct asmln {
char* instr; char* instr;
int truen; int truen;
}; };
void freeasmlns(struct asmln** lns, int count); void freeasmlns();
struct asmln** translate(struct line** lns, int lnscount, int* asmcount, char* fname); void printasmlns();
void translate(struct line** lns, int lnscount, char* fname);
#endif #endif