Improve templates

This commit is contained in:
Augusto Gunsch 2020-11-19 10:43:25 -03:00
parent d15bd48aba
commit 6262faf28a
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 196 additions and 117 deletions

View File

@ -1,8 +1,12 @@
#ifndef templates #ifndef templates
#define templates #define templates
#define TPUSHN 11 typedef struct {
char* tpush[TPUSHN] = { char** items;
int count;
} TEMPLATE;
char* tpushlns[] = {
"", "",
"", "",
"", "",
@ -15,9 +19,12 @@ char* tpush[TPUSHN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpush = {
.items = tpushlns,
.count = sizeof(tpushlns) / sizeof(char*)
};
#define TPUSHCONSN 8 char* tpushconslns[] = {
char* tpushcons[TPUSHCONSN] = {
"", "",
"", "",
"D=A", "D=A",
@ -27,9 +34,12 @@ char* tpushcons[TPUSHCONSN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpushcons = {
.items = tpushconslns,
.count = sizeof(tpushconslns) / sizeof(char*)
};
#define TPUSHSTATN 8 char* tpushstatlns[] = {
char* tpushstat[TPUSHSTATN] = {
"", "",
"", "",
"D=M", "D=M",
@ -39,15 +49,22 @@ char* tpushstat[TPUSHSTATN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpushstat = {
.items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPUSHTEMPN TPUSHSTATN TEMPLATE tpushtemp = {
char** tpushtemp = tpushstat; .items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPUSHPOINTERN TPUSHSTATN TEMPLATE tpushpointer = {
char** tpushpointer = tpushstat; .items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPOPN 13 char* tpoplns[] = {
char* tpop[TPOPN] = {
"", "",
"", "",
"", "",
@ -62,9 +79,12 @@ char* tpop[TPOPN] = {
"A=M", "A=M",
"M=D" "M=D"
}; };
TEMPLATE tpop = {
.items = tpoplns,
.count = sizeof(tpoplns) / sizeof(char*)
};
#define TPOPSTATN 6 char* tpopstatlns[] = {
char* tpopstat[TPOPSTATN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -72,15 +92,22 @@ char* tpopstat[TPOPSTATN] = {
"", "",
"" ""
}; };
TEMPLATE tpopstat = {
.items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TPOPTEMPN TPOPSTATN TEMPLATE tpoptemp = {
char** tpoptemp = tpopstat; .items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TPOPPOINTERN TPOPSTATN TEMPLATE tpoppointer = {
char** tpoppointer = tpopstat; .items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TARITHN 6 char* tarithlns[] = {
char* tarith[TARITHN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -88,25 +115,34 @@ char* tarith[TARITHN] = {
"A=A-1", "A=A-1",
"" ""
}; };
TEMPLATE tarith = {
.items = tarithlns,
.count = sizeof(tarithlns) / sizeof(char*)
};
#define TNEGN 4 char* tneglns[] = {
char* tneg[TNEGN] = {
"", "",
"@SP", "@SP",
"A=M-1", "A=M-1",
"M=-M", "M=-M",
}; };
TEMPLATE tneg = {
.items = tneglns,
.count = sizeof(tneglns) / sizeof(char*)
};
#define TNOTN 4 char* tnotlns[] = {
char* tnot[TNOTN] = {
"", "",
"@SP", "@SP",
"A=M-1", "A=M-1",
"M=!M", "M=!M",
}; };
TEMPLATE tnot = {
.items = tnotlns,
.count = sizeof(tnotlns) / sizeof(char*)
};
#define TCOMPN 13 char* tcomplns[] = {
char* tcomp[TCOMPN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -121,22 +157,31 @@ char* tcomp[TCOMPN] = {
"M=0", "M=0",
"" ""
}; };
TEMPLATE tcomp = {
.items = tcomplns,
.count = sizeof(tcomplns) / sizeof(char*)
};
#define TLABELN 2 char* tlabellns[] = {
char* tlabel[TLABELN] = {
"", "",
"" ""
}; };
TEMPLATE tlabel = {
.items = tlabellns,
.count = sizeof(tlabellns) / sizeof(char*)
};
#define TGOTON 3 char* tgotolns[] = {
char* tgoto[TGOTON] = {
"", "",
"", "",
"0;JMP" "0;JMP"
}; };
TEMPLATE tgoto = {
.items = tgotolns,
.count = sizeof(tgotolns) / sizeof(char*)
};
#define TIFGOTON 6 char* tifgotolns[] = {
char* tifgoto[TIFGOTON] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -144,9 +189,12 @@ char* tifgoto[TIFGOTON] = {
"", "",
"D;JNE" "D;JNE"
}; };
TEMPLATE tifgoto = {
.items = tifgotolns,
.count = sizeof(tifgotolns) / sizeof(char*)
};
#define TCALLSTARTN 8 char* tcallstartlns[] = {
char* tcallstart[TCALLSTARTN] = {
"", "",
"", "",
"D=A", "D=A",
@ -156,9 +204,12 @@ char* tcallstart[TCALLSTARTN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tcallstart = {
.items = tcallstartlns,
.count = sizeof(tcallstartlns) / sizeof(char*)
};
#define TCALLPUSHN 7 char* tcallpushlns[] = {
char* tcallpush[TCALLPUSHN] = {
"", "",
"D=M", "D=M",
"@SP", "@SP",
@ -167,9 +218,12 @@ char* tcallpush[TCALLPUSHN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tcallpush = {
.items = tcallpushlns,
.count = sizeof(tcallpushlns) / sizeof(char*)
};
#define TCALLSETARGN 8 char* tcallsetarglns[] = {
char* tcallsetarg[TCALLSETARGN] = {
"@SP", "@SP",
"D=M", "D=M",
"@LCL", "@LCL",
@ -179,39 +233,54 @@ char* tcallsetarg[TCALLSETARGN] = {
"@ARG", "@ARG",
"M=D" "M=D"
}; };
TEMPLATE tcallsetarg = {
.items = tcallsetarglns,
.count = sizeof(tcallsetarglns) / sizeof(char*)
};
#define TCALLJMPN 3 char* tcalljmplns[] = {
char* tcalljmp[TCALLJMPN] = {
"", "",
"0;JMP", "0;JMP",
"" ""
}; };
TEMPLATE tcalljmp = {
.items = tcalljmplns,
.count = sizeof(tcalljmplns) / sizeof(char*)
};
#define TFRAMEVARSN 4 char* tframevarslns[] = {
char* tframevars[TFRAMEVARSN] = {
"@LCL", "@LCL",
"@ARG", "@ARG",
"@THIS", "@THIS",
"@THAT" "@THAT"
}; };
TEMPLATE tframevars = {
.items = tframevarslns,
.count = sizeof(tframevarslns) / sizeof(char*)
};
#define TFUNCTIONN 2 char* tfunctionlns[] = {
char* tfunction[TFUNCTIONN] = {
"", "",
"" ""
}; };
TEMPLATE tfunction = {
.items = tfunctionlns,
.count = sizeof(tfunctionlns) / sizeof(char*)
};
#define TFUNCTIONPUSHN 5 char* tfunctionpushlns[] = {
char* tfunctionpush[TFUNCTIONPUSHN] = {
"@SP", "@SP",
"A=M", "A=M",
"M=0", "M=0",
"@SP", "@SP",
"M=M+1" "M=M+1"
}; };
TEMPLATE tfunctionpush = {
.items = tfunctionpushlns,
.count = sizeof(tfunctionpushlns) / sizeof(char*)
};
#define TSTARTRETURNN 18 char* tstartreturnlns[] = {
char* tstartreturn[TSTARTRETURNN] = {
"", "",
"@LCL", "@LCL",
"D=M", "D=M",
@ -231,21 +300,31 @@ char* tstartreturn[TSTARTRETURNN] = {
"@SP", "@SP",
"M=D" "M=D"
}; };
TEMPLATE tstartreturn = {
.items = tstartreturnlns,
.count = sizeof(tstartreturnlns) / sizeof(char*)
};
#define TRETPOPN 5 char* tretpoplns[] = {
char* tretpop[TRETPOPN] = {
"@LCL", "@LCL",
"AM=M-1", "AM=M-1",
"D=M", "D=M",
"", "",
"M=D", "M=D",
}; };
TEMPLATE tretpop = {
.items = tretpoplns,
.count = sizeof(tretpoplns) / sizeof(char*)
};
#define TENDRETURNN 3 char* tendreturnlns[] = {
char* tendreturn[TENDRETURNN] = {
"@R13", "@R13",
"A=M", "A=M",
"0;JMP" "0;JMP"
}; };
TEMPLATE tendreturn = {
.items = tendreturnlns,
.count = sizeof(tendreturnlns) / sizeof(char*)
};
#endif #endif

View File

@ -257,107 +257,107 @@ void checkfun(TRANSLATOR* t, LINE* ln) {
} }
} }
void addasm(TRANSLATOR* t, char** insts, int instcount) { void addasm(TRANSLATOR* t, TEMPLATE* tp) {
checkasmsize(t, instcount); checkasmsize(t, tp->count);
for(int i = 0; i < instcount; i++) { for(int i = 0; i < tp->count; i++) {
t->asmlns->items[t->asmlns->count] = insts[i]; t->asmlns->items[t->asmlns->count] = tp->items[i];
t->asmlns->count++; t->asmlns->count++;
} }
} }
void addasmlns(TRANSLATOR* t, LINE* ln, char** insts, int instcount) { void addasmlns(TRANSLATOR* t, LINE* ln, TEMPLATE* tp) {
// instruction comment // instruction comment
insts[0] = mkcom(t, ln); tp->items[0] = mkcom(t, ln);
addasm(t, insts, instcount); addasm(t, tp);
} }
void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, char** insts) { void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, TEMPLATE* tp) {
// @segment // @segment
insts[1] = switchseg(t, ln); tp->items[1] = switchseg(t, ln);
// D=M // D=M
insts[2] = heapstrtoclean(t, "D=M"); tp->items[2] = heapstrtoclean(t, "D=M");
// @i // @i
insts[3] = mkind(t, ln, indlen); tp->items[3] = mkind(t, ln, indlen);
} }
void pushcons(TRANSLATOR* t, LINE* ln, int indlen) { void pushcons(TRANSLATOR* t, LINE* ln, int indlen) {
// @i // @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(TRANSLATOR* t, LINE* ln, int indlen) { void pushstat(TRANSLATOR* t, LINE* ln, int indlen) {
// @fname.i // @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(TRANSLATOR* t, LINE* ln, int indlen) { void pushtemp(TRANSLATOR* t, LINE* ln, int indlen) {
// @5+i // @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(TRANSLATOR* t, LINE* ln, int indlen) { void pushpointer(TRANSLATOR* t, LINE* ln, int indlen) {
// @THIS/@THAT // @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(TRANSLATOR* t, LINE* ln, int indlen) { void push(TRANSLATOR* t, LINE* ln, int indlen) {
startpoppush(t, ln, indlen, tpush); startpoppush(t, ln, indlen, &tpush);
addasmlns(t, ln, tpush, TPUSHN); addasmlns(t, ln, &tpush);
} }
void popstat(TRANSLATOR* t, LINE* ln, int indlen) { void popstat(TRANSLATOR* t, LINE* ln, int indlen) {
// @fname.i // @fname.i
tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen); tpopstat.items[tpopstat.count-2] = mkstatind(t, ln, indlen);
// M=D // 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(TRANSLATOR* t, LINE* ln, int indlen) { void poptemp(TRANSLATOR* t, LINE* ln, int indlen) {
// @5+i // @5+i
tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen); tpoptemp.items[tpoptemp.count-2] = mktempind(t, ln, indlen);
// M=D // 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(TRANSLATOR* t, LINE* ln, int indlen) { void poppointer(TRANSLATOR* t, LINE* ln, int indlen) {
// @THIS/@THAT // @THIS/@THAT
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen); tpoppointer.items[tpoppointer.count-2] = mkpointerind(t, ln, indlen);
// M=D // 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(TRANSLATOR* t, LINE* ln, int indlen) { void pop(TRANSLATOR* t, LINE* ln, int indlen) {
startpoppush(t, ln, indlen, tpop); startpoppush(t, ln, indlen, &tpop);
addasmlns(t, ln, tpop, TPOPN); addasmlns(t, ln, &tpop);
} }
void arith(TRANSLATOR* t, LINE* ln, char* op) { void arith(TRANSLATOR* t, LINE* ln, char* op) {
tarith[TARITHN-1] = heapstrtoclean(t, op); tarith.items[tarith.count-1] = heapstrtoclean(t, op);
addasmlns(t, ln, tarith, TARITHN); addasmlns(t, ln, &tarith);
} }
void comp(TRANSLATOR* t, LINE* ln, char* op) { void comp(TRANSLATOR* t, LINE* ln, char* op) {
@ -365,19 +365,19 @@ void comp(TRANSLATOR* t, LINE* ln, char* op) {
int labellen = strlen(label); int labellen = strlen(label);
// @label // @label
tcomp[TCOMPN-6] = atlab(t, label, labellen); tcomp.items[tcomp.count-6] = atlab(t, label, labellen);
// D;J(op) // D;J(op)
int opsz = sizeof(char) * 6; int opsz = sizeof(char) * 6;
char* trueop = (char*)malloc(opsz); char* trueop = (char*)malloc(opsz);
snprintf(trueop, opsz, "D;J%s", op); snprintf(trueop, opsz, "D;J%s", op);
tcomp[TCOMPN-5] = trueop; tcomp.items[tcomp.count-5] = trueop;
pushtoclean(t, trueop); pushtoclean(t, trueop);
// (label) // (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(TRANSLATOR* t, LINE* ln) { void label(TRANSLATOR* t, LINE* ln) {
@ -389,40 +389,40 @@ void label(TRANSLATOR* t, LINE* ln) {
char* lab = (char*)malloc(sz); char* lab = (char*)malloc(sz);
snprintf(lab, sz, "(%s$%s)", t->lastfun, ln->tokens[1]); snprintf(lab, sz, "(%s$%s)", t->lastfun, ln->tokens[1]);
pushtoclean(t, lab); pushtoclean(t, lab);
tlabel[TLABELN-1] = lab; tlabel.items[tlabel.count-1] = lab;
addasmlns(t, ln, tlabel, TLABELN); addasmlns(t, ln, &tlabel);
} }
void mygoto(TRANSLATOR* t, LINE* ln) { void mygoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @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(TRANSLATOR* t, LINE* ln) { void ifgoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @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(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) { int pushframe(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) {
tcallstart[1] = atlab(t, retlab, retlablen); tcallstart.items[1] = atlab(t, retlab, retlablen);
addasmlns(t, ln, tcallstart, TCALLSTARTN); addasmlns(t, ln, &tcallstart);
for(int i = 0; i < TFRAMEVARSN; i++) { for(int i = 0; i < tframevars.count; i++) {
tcallpush[0] = tframevars[i]; tcallpush.items[0] = tframevars.items[i];
addasm(t, tcallpush, TCALLPUSHN); addasm(t, &tcallpush);
} }
return TFRAMEVARSN + 1; return tframevars.count + 1;
} }
void call(TRANSLATOR* t, LINE* ln) { void call(TRANSLATOR* t, LINE* ln) {
@ -439,14 +439,14 @@ void call(TRANSLATOR* t, LINE* ln) {
int nargslen = strlen(ln->tokens[2]); int nargslen = strlen(ln->tokens[2]);
checknargs(t, ln, nargslen); checknargs(t, ln, nargslen);
int nargs = atoi(ln->tokens[2]); int nargs = atoi(ln->tokens[2]);
tcallsetarg[TCALLSETARGN-4] = atn(t, nargs + framesize); tcallsetarg.items[tcallsetarg.count-4] = atn(t, nargs + framesize);
addasm(t, tcallsetarg, TCALLSETARGN); addasm(t, &tcallsetarg);
// jmp // jmp
int jmplen = strlen(ln->tokens[1]); int jmplen = strlen(ln->tokens[1]);
tcalljmp[TCALLJMPN-3] = atlab(t, ln->tokens[1], jmplen); tcalljmp.items[tcalljmp.count-3] = atlab(t, ln->tokens[1], jmplen);
tcalljmp[TCALLJMPN-1] = mklab(t, retlab, retlablen); tcalljmp.items[tcalljmp.count-1] = mklab(t, retlab, retlablen);
addasm(t, tcalljmp, TCALLJMPN); addasm(t, &tcalljmp);
} }
void function(TRANSLATOR* t, LINE* ln) { void function(TRANSLATOR* t, LINE* ln) {
@ -465,8 +465,8 @@ void function(TRANSLATOR* t, LINE* ln) {
t->cmpind = 0; t->cmpind = 0;
// (funcname) // (funcname)
tfunction[1] = mklab(t, ln->tokens[1], funlen); tfunction.items[1] = mklab(t, ln->tokens[1], funlen);
addasmlns(t, ln, tfunction, TFUNCTIONN); addasmlns(t, ln, &tfunction);
// repeat nVars times: // repeat nVars times:
int nlocalslen = strlen(ln->tokens[2]); int nlocalslen = strlen(ln->tokens[2]);
@ -474,19 +474,19 @@ void function(TRANSLATOR* t, LINE* ln) {
int nlocals = atoi(ln->tokens[2]); int nlocals = atoi(ln->tokens[2]);
for(int i = 0; i < nlocals; i++) { for(int i = 0; i < nlocals; i++) {
addasm(t, tfunctionpush, TFUNCTIONPUSHN); addasm(t, &tfunctionpush);
} }
} }
void myreturn(TRANSLATOR* t, LINE* ln) { void myreturn(TRANSLATOR* t, LINE* ln) {
addasmlns(t, ln, tstartreturn, TSTARTRETURNN); addasmlns(t, ln, &tstartreturn);
for(int i = TFRAMEVARSN-1; i >= 0; i--) { for(int i = tframevars.count-1; i >= 0; i--) {
tretpop[TRETPOPN-2] = tframevars[i]; tretpop.items[tretpop.count-2] = tframevars.items[i];
addasm(t, tretpop, TRETPOPN); addasm(t, &tretpop);
} }
addasm(t, tendreturn, TENDRETURNN); addasm(t, &tendreturn);
} }
void switchpush(TRANSLATOR* t, LINE* ln) { void switchpush(TRANSLATOR* t, LINE* ln) {
@ -534,7 +534,7 @@ void switchop(TRANSLATOR* t, LINE* ln) {
else if(!strcmp(op, "sub")) else if(!strcmp(op, "sub"))
arith(t, ln, "M=M-D"); arith(t, ln, "M=M-D");
else if(!strcmp(op, "neg")) else if(!strcmp(op, "neg"))
addasmlns(t, ln, tneg, TNEGN); addasmlns(t, ln, &tneg);
else if(!strcmp(op, "eq")) else if(!strcmp(op, "eq"))
comp(t, ln, "EQ"); comp(t, ln, "EQ");
else if(!strcmp(op, "gt")) else if(!strcmp(op, "gt"))
@ -546,7 +546,7 @@ void switchop(TRANSLATOR* t, LINE* ln) {
else if(!strcmp(op, "or")) else if(!strcmp(op, "or"))
arith(t, ln, "M=D|M"); arith(t, ln, "M=D|M");
else if(!strcmp(op, "not")) else if(!strcmp(op, "not"))
addasmlns(t, ln, tnot, TNOTN); addasmlns(t, ln, &tnot);
else if(!strcmp(op, "label")) else if(!strcmp(op, "label"))
label(t, ln); label(t, ln);
else if(!strcmp(op, "goto")) else if(!strcmp(op, "goto"))