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

View File

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