Add pop/push translating capabilities

This commit is contained in:
Augusto Gunsch 2020-10-30 14:06:02 -03:00
parent 9c5b35ebf5
commit 30ea5c33e3
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
5 changed files with 215 additions and 5 deletions

View File

@ -1,6 +1,6 @@
FILES = parser.c main.c FILES = parser.c main.c translator.c
INCLUDES = -I. INCLUDES = -I.
CFLAGS = -std=c99 CFLAGS = -std=c99 -g
OUTFILE = vmtranslator OUTFILE = vmtranslator
main: ${FILES} main: ${FILES}

10
main.c
View File

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "parser.h" #include "parser.h"
#include "translator.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if(argc < 2) { if(argc < 2) {
@ -23,8 +24,16 @@ int main(int argc, char* argv[]) {
// parsing // parsing
struct line** lns = parse(input, lncount, widestln, maxtokens); struct line** lns = parse(input, lncount, widestln, maxtokens);
fclose(input); fclose(input);
// translating
int asmind = 0;
struct asmln** asmlns = translate(lns, lncount, &asmind);
// printing // printing
for(int i = 0; i < asmind; i++) {
printf("%s\n", asmlns[i]->instr);
}
/*
for(int i = 0; i < lncount; i++) { for(int i = 0; i < lncount; i++) {
int tkcount = lns[i]->tokenscount; int tkcount = lns[i]->tokenscount;
for(int j = 0; j < tkcount; j++) { for(int j = 0; j < tkcount; j++) {
@ -32,6 +41,7 @@ int main(int argc, char* argv[]) {
} }
printf("\n"); printf("\n");
} }
*/
// freeing // freeing
freelns(lns, lncount); freelns(lns, lncount);

View File

@ -1,6 +1,5 @@
#include <stdio.h> #ifndef parser
#include <stdlib.h> #define parser
struct line { struct line {
char** tokens; char** tokens;
int tokenscount; int tokenscount;
@ -10,3 +9,4 @@ struct line {
void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens); void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens);
void freelns(struct line** lns, int lnscount); void freelns(struct line** lns, int lnscount);
struct line** parse(FILE* input, int lncount, int widestln, int maxtokens); struct line** parse(FILE* input, int lncount, int widestln, int maxtokens);
#endif

190
translator.c Normal file
View File

@ -0,0 +1,190 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "translator.h"
enum operation { push, pop };
enum operation getop(struct line* ln) {
char* tok = ln->tokens[0];
if(strcmp(tok, "push") == 0)
return push;
if(strcmp(tok, "pop") == 0)
return pop;
fprintf(stderr, "Invalid operation '%s'; line %i", tok, ln->truen);
exit(1);
}
char* heapstr(const char* input) {
char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1));
strcpy(newstr, input);
return newstr;
}
char* switchseg(struct line* ln) {
char* seg = ln->tokens[1];
if(strcmp(seg, "local") == 0)
return heapstr("@LCL");
if(strcmp(seg, "argument") == 0)
return heapstr("@ARG");
if(strcmp(seg, "this") == 0)
return heapstr("@THIS");
if(strcmp(seg, "that") == 0)
return heapstr("@THAT");
}
// produce comment as follows:
// pop/push segment i
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]);
return comment;
}
void checkind(struct line* ln, int indsz) {
for(int i = 0; i < indsz; i++) {
if(!isdigit(ln->tokens[2][i])) {
fprintf(stderr, "Invalid index '%s'; line %i\n", ln->tokens[2], ln->truen);
exit(1);
}
}
}
char* mkind(char* ind, int indsz) {
int newsz = sizeof(char) * (indsz + 2);
char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", ind);
return newind;
}
void checkasmsize(int* size, struct asmln** lns, int targ) {
if(targ >= (*size)) {
printf("realloc'd");
(*size)=targ*2;
(*lns) = realloc((*lns), (*size));
}
}
struct asmln* mkasmln(struct line* ln, char* content) {
struct asmln* newln = (struct asmln*)malloc(sizeof(struct asmln));
newln->truen = ln->truen;
newln->instr = content;
return newln;
}
void startpoppush(struct line* ln, struct asmln** asmlns, int* asmind) {
int asmi = *asmind;
// //operation segment i
int indlen = strlen(ln->tokens[2]);
asmlns[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @segment
asmlns[asmi] = mkasmln(ln, switchseg(ln));
asmi++;
// D=M
asmlns[asmi] = mkasmln(ln, heapstr("D=M"));
asmi++;
// @i
checkind(ln, indlen);
asmlns[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen));
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 mkpop(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int finalasmi = 14;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+finalasmi));
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 mkpoppush(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize, enum operation op) {
if(op == push)
mkpush(ln, asmlns, asmind, asmsize);
else if(op == pop)
mkpop(ln, asmlns, asmind, asmsize);
}
void checkopamnt(struct line* ln) {
if(ln->tokenscount < 2) {
fprintf(stderr, "Missing memory segment; line %i", ln->truen);
exit(1);
}
if(ln->tokenscount < 3) {
fprintf(stderr, "Missing operation index; line %i", ln->truen);
exit(1);
}
}
struct asmln** translate(struct line** lns, int lnscount, int* asmind) {
int sizebet = sizeof(struct asmln*)*(lnscount * 15);
int asmi = (*asmind);
struct asmln** asmlns = (struct asmln**)malloc(sizebet);
for(int i = 0; i < lnscount; i++) {
struct line* ln = lns[i];
enum operation op = getop(ln);
if(op == pop || op == push) {
checkopamnt(ln);
mkpoppush(ln, asmlns, &asmi, &sizebet, op);
}
}
(*asmind) = asmi;
return asmlns;
}

10
translator.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef translator
#define translator
#include "parser.h"
struct asmln {
char* instr;
int truen;
};
struct asmln** translate(struct line** lns, int lnscount, int* asmind);
#endif