Severe refactoring
This commit is contained in:
parent
46c2a83735
commit
41e322825f
9
main.c
9
main.c
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
393
translator.c
393
translator.c
|
@ -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, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) {
|
void pushstat(struct line* ln, int indlen, char* fname) {
|
||||||
checkopamnt(3, ln);
|
checkasmsize(TPUSHSTATN);
|
||||||
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 popstat(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) {
|
void pushtemp(struct line* ln, int indlen) {
|
||||||
checkopamnt(3, ln);
|
checkasmsize(TPUSHSTATN);
|
||||||
int asmi = *asmind;
|
|
||||||
int totalinstrs = 8;
|
|
||||||
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
|
|
||||||
|
|
||||||
// // pop static i
|
// // pop static i
|
||||||
int indlen = strlen(ln->tokens[2]);
|
tpushtemp[0] = mkcom(ln, indlen);
|
||||||
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
|
|
||||||
asmi++;
|
// @5+i
|
||||||
|
checkind(ln, indlen);
|
||||||
|
tpushtemp[1] = mktempind(ln, indlen);
|
||||||
|
|
||||||
const int instcount = 5;
|
addasmlns(ln, tpushtemp, TPUSHTEMP);
|
||||||
const char* instrs[] = {
|
}
|
||||||
"@SP",
|
|
||||||
"D=M",
|
|
||||||
"M=M-1",
|
|
||||||
"A=D",
|
|
||||||
"D=M"
|
|
||||||
};
|
|
||||||
|
|
||||||
for(int i = 0; i < instcount; i++) {
|
void mkpush(struct line* ln, int indlen) {
|
||||||
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
|
checkasmsize(TPUSHN);
|
||||||
asmi++;
|
|
||||||
}
|
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
|
// @fname.i
|
||||||
checkind(ln, indlen);
|
checkind(ln, indlen);
|
||||||
(*asmlns)[asmi] = mkasmln(ln, mkstatind(ln->tokens[2], indlen, fname));
|
tpopstat[6] = mkstatind(ln, indlen, fname);
|
||||||
asmi++;
|
|
||||||
|
|
||||||
// M=D
|
// M=D
|
||||||
(*asmlns)[asmi] = mkasmln(ln, heapstr("M=D"));
|
tpopstat[7] = heapstr("M=D");
|
||||||
asmi++;
|
|
||||||
|
addasmlns(ln, tpopstat, TPOPSTATN);
|
||||||
(*asmind) = asmi;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushtemp(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
|
void poptemp(struct line* ln, int indlen) {
|
||||||
int asmi = *asmind;
|
checkasmsize(TPOPTEMPN);
|
||||||
int totalinstrs = 8;
|
|
||||||
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
|
|
||||||
|
|
||||||
// // pop static i
|
// // pop static i
|
||||||
int indlen = strlen(ln->tokens[2]);
|
tpoptemp[0] = mkcom(ln, indlen);
|
||||||
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
|
|
||||||
asmi++;
|
|
||||||
|
|
||||||
// @5+i
|
// @5+i
|
||||||
checkind(ln, indlen);
|
checkind(ln, indlen);
|
||||||
(*asmlns)[asmi] = mkasmln(ln, mktempind(ln->tokens[2], indlen));
|
tpoptemp[5] = mktempind(ln, 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
|
// M=D
|
||||||
(*asmlns)[asmi] = mkasmln(ln, heapstr("M=D"));
|
tpoptemp[6] = heapstr("M=D");
|
||||||
asmi++;
|
|
||||||
|
|
||||||
(*asmind) = asmi;
|
addasmlns(ln, tpoptemp, TPOPTEMPN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mkpop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
|
void mkpop(struct line* ln, int indlen) {
|
||||||
int asmi = *asmind;
|
checkasmsize(TPOPN);
|
||||||
int totalinstrs = 14;
|
|
||||||
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
|
|
||||||
|
|
||||||
startpoppush(ln, asmlns, &asmi);
|
startpoppush(ln, indlen, tpop);
|
||||||
|
|
||||||
const int instcount = 10;
|
addasmlns(ln, tpop, TPOPN);
|
||||||
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++) {
|
void switchop(struct line* ln, char* fname) {
|
||||||
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
|
char* op = ln->tokens[0];
|
||||||
asmi++;
|
|
||||||
|
if(strcmp(op, "push") == 0) {
|
||||||
|
checkopamnt(3, ln);
|
||||||
|
char* seg = ln->tokens[1];
|
||||||
|
int indlen = strlen(ln->tokens[2]);
|
||||||
|
|
||||||
|
if(strcmp(seg, "constant") == 0)
|
||||||
|
pushcons(ln, indlen);
|
||||||
|
|
||||||
|
else if(strcmp(seg, "static") == 0)
|
||||||
|
pushstat(ln, indlen, fname);
|
||||||
|
|
||||||
|
else if(strcmp(seg, "temp") == 0)
|
||||||
|
pushtemp(ln, indlen);
|
||||||
|
|
||||||
|
else
|
||||||
|
mkpush(ln, indlen);
|
||||||
}
|
}
|
||||||
|
else if(strcmp(op, "pop") == 0) {
|
||||||
|
checkopamnt(3, ln);
|
||||||
|
char* seg = ln->tokens[1];
|
||||||
|
int indlen = strlen(ln->tokens[2]);
|
||||||
|
|
||||||
(*asmind) = asmi;
|
if(strcmp(seg, "static") == 0)
|
||||||
|
popstat(ln, indlen, fname);
|
||||||
|
|
||||||
|
else if(strcmp(seg, "temp") == 0)
|
||||||
|
poptemp(ln, indlen);
|
||||||
|
|
||||||
|
else
|
||||||
|
mkpop(ln, indlen);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void switchop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, char* fname) {
|
void translate(struct line** lns, int lnscount, char* fname) {
|
||||||
if(strcmp(ln->tokens[0], "push") == 0)
|
asmsize = sizeof(struct asmln*)*(lnscount * 15);
|
||||||
if(strcmp(ln->tokens[1], "constant") == 0)
|
asmind = 0;
|
||||||
pushcons(ln, asmlns, asmind, asmsize);
|
asmlns = (struct asmln**)malloc(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);
|
|
||||||
else
|
|
||||||
mkpop(ln, asmlns, asmind, asmsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct asmln** translate(struct line** lns, int lnscount, int* asmcount, char* fname) {
|
tocleanind = 0;
|
||||||
int sizebet = sizeof(struct asmln*)*(lnscount * 15);
|
tocleansize = sizeof(char*)*(lnscount * 5);
|
||||||
int asmi = (*asmcount);
|
toclean = (char**)malloc(tocleansize);
|
||||||
struct asmln** asmlns = (struct asmln**)malloc(sizebet);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue