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);
// translating
int asmcount = 0;
struct asmln** asmlns = translate(lns, lncount, &asmcount, fname);
translate(lns, lncount, fname);
// freeing lns
freelns(lns, lncount);
// printing
for(int i = 0; i < asmcount; i++) {
printf("%s\n", asmlns[i]->instr);
}
printasmlns();
// freeing asmlns
free(fname);
freeasmlns(asmlns, asmcount);
freeasmlns();
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 <ctype.h>
#include "translator.h"
#include "templates.h"
void freeasmlns(struct asmln** lns, int count) {
for(int i = 0; i < count; i++) {
free(lns[i]->instr);
free(lns[i]);
struct asmln** asmlns;
int asmind;
int asmsize;
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* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1));
strcpy(newstr, input);
pushtoclean(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);
char* comment = (char*)malloc(comlen);
snprintf(comment, comlen, "// %s %s %s", ln->tokens[0], ln->tokens[1], ln->tokens[2]);
pushtoclean(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);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ind);
snprintf(newind, newsz, "@%s", ln->tokens[2]);
pushtoclean(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 newsz = sizeof(char) * (fnamelen + indsz + 3);
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;
}
char* mktempind(char* ind, int indsz) {
int intind = atoi(ind);
char* mktempind(struct line* ln, int indsz) {
int intind = atoi(ln->tokens[2]);
int newsz = sizeof(char) * (indsz + 3);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", intind+5);
pushtoclean(newind);
return newind;
}
void checkasmsize(int* size, struct asmln*** lns, int targ) {
if(targ >= (*size)) {
(*size)=targ*2;
(*lns) = (struct asmln**)realloc((*lns), (*size));
void checkasmsize(int toadd) {
int targ = sizeof(struct asmln*)*(asmind+toadd);
if(targ >= asmsize) {
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) {
checkopamnt(3, ln);
int asmi = *asmind;
void addasmlns(struct line* ln, char** insts, int instcount) {
for(int i = 0; i < instcount; i++) {
asmlns[asmind] = mkasmln(ln, insts[i]);
asmind++;
}
}
void startpoppush(struct line* ln, int indlen, char** insts) {
// // operation segment i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
insts[0] = mkcom(ln, indlen);
// @segment
(*asmlns)[asmi] = mkasmln(ln, switchseg(ln));
asmi++;
insts[1] = switchseg(ln);
// D=M
(*asmlns)[asmi] = mkasmln(ln, heapstr("D=M"));
asmi++;
insts[2] = heapstr("D=M");
// @i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen));
asmi++;
(*asmind) = asmi;
insts[3] = mkind(ln, indlen);
}
void pushcons(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
checkopamnt(3, ln);
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
void pushcons(struct line* ln, int indlen) {
checkasmsize(TPUSHCONSN);
// // push constant i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
tpushcons[0] = mkcom(ln, indlen);
// @i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen));
asmi++;
tpushcons[1] = mkind(ln, indlen);
const int instcount = 6;
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++;
addasmlns(ln, tpushcons, TPUSHCONSN);
}
(*asmind) = asmi;
}
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));
void pushstat(struct line* ln, int indlen, char* fname) {
checkasmsize(TPUSHSTATN);
// // push static i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
tpushstat[0] = mkcom(ln, indlen);
// @fname.i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkstatind(ln->tokens[2], indlen, fname));
asmi++;
tpushstat[1] = mkstatind(ln, indlen, fname);
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++;
addasmlns(ln, tpushstat, TPUSHSTATN);
}
(*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);
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // pop static i
char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
const int instcount = 5;
const char* instrs[] = {
"@SP",
"D=M",
"M=M-1",
"A=D",
"D=M"
};
if(strcmp(seg, "constant") == 0)
pushcons(ln, indlen);
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++;
}
else if(strcmp(seg, "static") == 0)
pushstat(ln, indlen, fname);
// @fname.i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkstatind(ln->tokens[2], indlen, fname));
asmi++;
else if(strcmp(seg, "temp") == 0)
pushtemp(ln, indlen);
// 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
mkpush(ln, asmlns, asmind, asmsize);
else if(strcmp(ln->tokens[0], "pop") == 0)
if(strcmp(ln->tokens[1], "static") == 0)
popstat(ln, asmlns, asmind, asmsize, fname);
else if(strcmp(ln->tokens[1], "temp") == 0)
poptemp(ln, asmlns, asmind, asmsize);
mkpush(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(ln, indlen, fname);
else if(strcmp(seg, "temp") == 0)
poptemp(ln, indlen);
else
mkpop(ln, asmlns, asmind, asmsize);
mkpop(ln, indlen);
}
}
struct asmln** translate(struct line** lns, int lnscount, int* asmcount, char* fname) {
int sizebet = sizeof(struct asmln*)*(lnscount * 15);
int asmi = (*asmcount);
struct asmln** asmlns = (struct asmln**)malloc(sizebet);
void translate(struct line** lns, int lnscount, char* fname) {
asmsize = sizeof(struct asmln*)*(lnscount * 15);
asmind = 0;
asmlns = (struct asmln**)malloc(asmsize);
tocleanind = 0;
tocleansize = sizeof(char*)*(lnscount * 5);
toclean = (char**)malloc(tocleansize);
for(int i = 0; i < lnscount; 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
#define translator
#include "parser.h"
struct asmln {
char* instr;
int truen;
};
void freeasmlns(struct asmln** lns, int count);
struct asmln** translate(struct line** lns, int lnscount, int* asmcount, char* fname);
void freeasmlns();
void printasmlns();
void translate(struct line** lns, int lnscount, char* fname);
#endif