2020-10-24 21:06:07 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2020-11-18 17:53:23 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "tables.h"
|
|
|
|
#include "assembler.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
void expandsymbols(SYMBOLARRAY* a, int toaddn);
|
|
|
|
void pushsymbol(SYMBOLARRAY* a, SYMBOL* s);
|
|
|
|
void freesymbol(SYMBOL* s);
|
|
|
|
SYMBOL* mksymbol(char* name, int namesize, int val);
|
|
|
|
int getsymbol(ASSEMBLER* a, char* name);
|
|
|
|
void skipln(ASSEMBLER* a);
|
|
|
|
void readrest(ASSEMBLER* a, int trueln);
|
|
|
|
int isvar(char* var);
|
|
|
|
void initsymbols(SYMBOLARRAY* s);
|
|
|
|
ASSEMBLER* mkassembler(FILE* input);
|
|
|
|
void populatevars(ASSEMBLER* a);
|
|
|
|
SYMBOL* readlabel(ASSEMBLER* a, int trueln);
|
|
|
|
void chop(ASSEMBLER* a);
|
|
|
|
void replacevar(SYMBOL* ln, int val);
|
|
|
|
void preprocess(ASSEMBLER* a);
|
|
|
|
void transa(SYMBOL* ln);
|
|
|
|
char* lookctable(TABLE* t, bool cond, char* token, const char* fieldname, int trueln);
|
|
|
|
void transb(SYMBOL* ln);
|
|
|
|
void translate(ASSEMBLER* a);
|
|
|
|
void gatherinfo(ASSEMBLER* a);
|
|
|
|
void freeassembler(ASSEMBLER* a);
|
|
|
|
|
|
|
|
void expandsymbols(SYMBOLARRAY* a, int toaddn) {
|
|
|
|
int sum = a->count + toaddn;
|
|
|
|
if(sizeof(SYMBOL*) * sum > a->size) {
|
|
|
|
a->size = sizeof(SYMBOL*) * sum * 3;
|
|
|
|
a->items = (SYMBOL**)realloc(a->items, a->size);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void pushsymbol(SYMBOLARRAY* a, SYMBOL* s) {
|
|
|
|
expandsymbols(a, 1);
|
|
|
|
a->items[a->count] = s;
|
|
|
|
a->count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freesymbol(SYMBOL* s) {
|
|
|
|
free(s->name);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freesymbols(SYMBOLARRAY* a) {
|
|
|
|
for(int i = 0; i < a->count; i++)
|
|
|
|
freesymbol(a->items[i]);
|
|
|
|
free(a->items);
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYMBOL* mksymbol(char* name, int namesize, int val) {
|
|
|
|
SYMBOL* s = (SYMBOL*)malloc(sizeof(SYMBOL));
|
2020-10-24 21:06:07 -04:00
|
|
|
char* heapname = (char*)malloc(namesize);
|
|
|
|
strcpy(heapname, name);
|
|
|
|
s->name = heapname;
|
|
|
|
s->value = val;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
int getsymbol(ASSEMBLER* a, char* name) {
|
|
|
|
for(int i = 0; i < a->vars->count; i++)
|
|
|
|
if(strcmp(a->vars->items[i]->name, name) == 0)
|
|
|
|
return a->vars->items[i]->value;
|
2020-10-24 21:06:07 -04:00
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
for(int i = 0; i < a->labels->count; i++)
|
|
|
|
if(strcmp(a->labels->items[i]->name, name) == 0)
|
|
|
|
return a->labels->items[i]->value;
|
2020-10-24 21:06:07 -04:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void skipln(ASSEMBLER* a) {
|
2020-10-24 21:06:07 -04:00
|
|
|
char c;
|
2020-11-18 17:53:23 -05:00
|
|
|
while(c = fgetc(a->input), c != -1)
|
2020-10-24 21:06:07 -04:00
|
|
|
if(c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void readrest(ASSEMBLER* a, int trueln) {
|
2020-10-24 21:06:07 -04:00
|
|
|
char c;
|
2020-11-18 17:53:23 -05:00
|
|
|
while(c = fgetc(a->input), c != -1) {
|
2020-10-24 21:06:07 -04:00
|
|
|
if(c == '\n')
|
|
|
|
break;
|
|
|
|
if(isspace(c))
|
|
|
|
continue;
|
|
|
|
if(c == '/') {
|
2020-11-18 17:53:23 -05:00
|
|
|
char nc = fgetc(a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
if(nc == '/') {
|
2020-11-18 17:53:23 -05:00
|
|
|
skipln(a);
|
2020-10-24 21:06:07 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
ungetc(nc, a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
fprintf(stderr, "Unexpected '%c' at line '%i'\n", c, trueln);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int isvar(char* var) {
|
|
|
|
int i = 0;
|
|
|
|
while(1) {
|
|
|
|
if(var[i] == '\0')
|
|
|
|
break;
|
|
|
|
if(!isdigit(var[i]))
|
|
|
|
return 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void initsymbols(SYMBOLARRAY* s) {
|
|
|
|
s->size = s->count * sizeof(SYMBOL*);
|
|
|
|
s->items = (SYMBOL**)malloc(s->size);
|
|
|
|
s->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSEMBLER* mkassembler(FILE* input) {
|
|
|
|
ASSEMBLER* a = (ASSEMBLER*)malloc(sizeof(ASSEMBLER));
|
|
|
|
a->lns = (SYMBOLARRAY*)malloc(sizeof(SYMBOLARRAY));
|
|
|
|
a->labels = (SYMBOLARRAY*)malloc(sizeof(SYMBOLARRAY));
|
|
|
|
a->vars = (SYMBOLARRAY*)malloc(sizeof(SYMBOLARRAY));
|
|
|
|
a->input = input;
|
|
|
|
|
|
|
|
gatherinfo(a);
|
|
|
|
|
|
|
|
initsymbols(a->lns);
|
|
|
|
initsymbols(a->labels);
|
|
|
|
a->vars->count = 80; // arbitrary number for initial size
|
|
|
|
initsymbols(a->vars);
|
|
|
|
|
|
|
|
populatevars(a);
|
|
|
|
chop(a);
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void populatevars(ASSEMBLER* a) {
|
2020-10-24 21:06:07 -04:00
|
|
|
const int firstamnt = 5;
|
2020-11-18 17:53:23 -05:00
|
|
|
const int ramvamnt = 16; //max: 19
|
|
|
|
const int specialamt = 2;
|
|
|
|
const int sum = firstamnt + ramvamnt + specialamt;
|
|
|
|
|
|
|
|
// realloc if necessary
|
|
|
|
expandsymbols(a->vars, sum);
|
|
|
|
|
|
|
|
// update varscount to new index
|
|
|
|
a->vars->count = sum;
|
|
|
|
|
|
|
|
// first five
|
2020-10-24 21:06:07 -04:00
|
|
|
char* labels[] = { "SP", "LCL", "ARG", "THIS", "THAT" };
|
|
|
|
for(int i = 0; i < firstamnt; i++) {
|
2020-11-18 17:53:23 -05:00
|
|
|
a->vars->items[i] = mksymbol(labels[i], strlen(labels[i])+1, i);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RAM variables (R0-R15)
|
|
|
|
const int asciioff = 48;
|
|
|
|
char ramvname[4];
|
|
|
|
ramvname[0] = 'R';
|
|
|
|
ramvname[2] = '\0';
|
2020-11-18 17:53:23 -05:00
|
|
|
int tmptarg = (ramvamnt/10)*10;
|
|
|
|
for(int i = 0; i < tmptarg; i++) {
|
2020-10-24 21:06:07 -04:00
|
|
|
ramvname[1] = (char)(i+asciioff);
|
2020-11-18 17:53:23 -05:00
|
|
|
a->vars->items[firstamnt+i] = mksymbol(ramvname, 3, i);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
ramvname[1] = '1';
|
|
|
|
ramvname[3] = '\0';
|
|
|
|
for(int i = 10; i < ramvamnt; i++) {
|
|
|
|
ramvname[2] = (char)((i%10)+asciioff);
|
2020-11-18 17:53:23 -05:00
|
|
|
a->vars->items[firstamnt+i] = mksymbol(ramvname, 4, i);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SCREEN var
|
2020-11-18 17:53:23 -05:00
|
|
|
a->vars->items[firstamnt+ramvamnt] = mksymbol("SCREEN", 7, 16384);
|
2020-10-24 21:06:07 -04:00
|
|
|
// KBD var
|
2020-11-18 17:53:23 -05:00
|
|
|
a->vars->items[firstamnt+ramvamnt+1] = mksymbol("KBD", 4, 24576);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
SYMBOL* readlabel(ASSEMBLER* a, int trueln) {
|
|
|
|
char* name = (char*)malloc(sizeof(char)*(a->maxwidth-1));
|
2020-10-24 21:06:07 -04:00
|
|
|
int i = 0;
|
|
|
|
char c;
|
2020-11-18 17:53:23 -05:00
|
|
|
int maxind = a->maxwidth-2;
|
|
|
|
while(c = fgetc(a->input), c != -1) {
|
2020-10-24 21:06:07 -04:00
|
|
|
if(c == ')')
|
|
|
|
break;
|
2020-11-18 17:53:23 -05:00
|
|
|
if(i == maxind) {
|
2020-10-24 21:06:07 -04:00
|
|
|
fprintf(stderr, "Label width bigger than the maximum (%i characters); line %i\n",
|
2020-11-18 17:53:23 -05:00
|
|
|
maxind, trueln+1);
|
2020-10-24 21:06:07 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(c == '\n') {
|
|
|
|
fprintf(stderr, "Unexpected end of line; line %i\n", trueln+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(isspace(c) || c == '(') {
|
|
|
|
fprintf(stderr, "Unexpected '%c'; line %i\n", c, trueln+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
name[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
name[i] = '\0';
|
2020-11-18 17:53:23 -05:00
|
|
|
readrest(a, trueln);
|
|
|
|
SYMBOL* l = (SYMBOL*)malloc(sizeof(SYMBOL));
|
2020-10-24 21:06:07 -04:00
|
|
|
l->name = name;
|
2020-11-18 17:53:23 -05:00
|
|
|
l->value = a->lns->count;
|
2020-10-24 21:06:07 -04:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splits the stream into an array of strings, stripping comments, white spaces and labels
|
|
|
|
// Requires vars array to check for duplicate symbols, but doesn't modify it
|
2020-11-18 17:53:23 -05:00
|
|
|
void chop(ASSEMBLER* a) {
|
2020-10-24 21:06:07 -04:00
|
|
|
char c;
|
2020-11-18 17:53:23 -05:00
|
|
|
char tmpln[a->maxwidth];
|
|
|
|
int lnind = 0;
|
2020-10-24 21:06:07 -04:00
|
|
|
int lnscount = 0;
|
|
|
|
int truelnscount = 1;
|
2020-11-18 17:53:23 -05:00
|
|
|
|
|
|
|
bool comment = false;
|
|
|
|
bool spacedln = false;
|
|
|
|
while(c = fgetc(a->input), c != -1) {
|
2020-10-24 21:06:07 -04:00
|
|
|
if(c == '\n') {
|
|
|
|
if(comment) {
|
2020-11-18 17:53:23 -05:00
|
|
|
comment = false;
|
|
|
|
ungetc(c, a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
truelnscount++;
|
|
|
|
if(!lnind)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tmpln[lnind] = '\0';
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
pushsymbol(a->lns, mksymbol(tmpln, lnind+1, truelnscount));
|
2020-10-24 21:06:07 -04:00
|
|
|
|
|
|
|
lnind = 0;
|
2020-11-18 17:53:23 -05:00
|
|
|
spacedln = false;
|
|
|
|
lnscount++;
|
2020-10-24 21:06:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(comment)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(isspace(c)) {
|
|
|
|
if(lnind)
|
2020-11-18 17:53:23 -05:00
|
|
|
spacedln = true;
|
2020-10-24 21:06:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c == '(') {
|
2020-11-18 17:53:23 -05:00
|
|
|
if(lnind) {
|
2020-10-24 21:06:07 -04:00
|
|
|
fprintf(stderr, "Unexpected char '%c'; line %i:%i\n", c, truelnscount, lnind+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
SYMBOL* l = readlabel(a, truelnscount);
|
|
|
|
if(getsymbol(a, l->name) != -1) {
|
2020-10-24 21:06:07 -04:00
|
|
|
fprintf(stderr, "Already defined symbol '%s'; line %i\n", l->name, truelnscount);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
pushsymbol(a->labels, l);
|
2020-10-24 21:06:07 -04:00
|
|
|
truelnscount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c == '/') {
|
2020-11-18 17:53:23 -05:00
|
|
|
char nc = fgetc(a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
if(nc == '/') {
|
2020-11-18 17:53:23 -05:00
|
|
|
comment = true;
|
2020-10-24 21:06:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
ungetc(nc, a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(spacedln) {
|
|
|
|
fprintf(stderr, "Unexpected char '%c'; line %i:%i\n", c, lnscount+1, lnind+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpln[lnind] = c;
|
|
|
|
lnind++;
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
fclose(a->input);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void replacevar(SYMBOL* ln, int val) {
|
|
|
|
free(ln->content);
|
|
|
|
int size = sizeof(char)*(countplaces(val) + 2);
|
|
|
|
char* newln = (char *)malloc(size);
|
|
|
|
snprintf(newln, size, "@%i", val);
|
|
|
|
ln->content = newln;
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void preprocess(ASSEMBLER* a) {
|
|
|
|
int varsramind = BOTTOM_VAR;
|
|
|
|
for(int i = 0; i < a->lncount; i++) {
|
|
|
|
if(a->lns->items[i]->content[0] == '@') {
|
|
|
|
char* afterat = a->lns->items[i]->content+sizeof(char);
|
2020-10-24 21:06:07 -04:00
|
|
|
if(isvar(afterat)) {
|
2020-11-18 17:53:23 -05:00
|
|
|
int val = getsymbol(a, afterat);
|
2020-10-24 21:06:07 -04:00
|
|
|
if(val == -1) {
|
|
|
|
if(varsramind == RAM_LIMIT) {
|
2020-11-18 17:53:23 -05:00
|
|
|
fprintf(stderr, "Variable amount reached RAM limit (%i); line %i\n", RAM_LIMIT, a->lns->items[i]->truen);
|
2020-10-24 21:06:07 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
SYMBOL* var = mksymbol(afterat, strlen(afterat)+1, varsramind);
|
2020-10-24 21:06:07 -04:00
|
|
|
varsramind++;
|
2020-11-18 17:53:23 -05:00
|
|
|
pushsymbol(a->vars, var);
|
2020-10-24 21:06:07 -04:00
|
|
|
val = var->value;
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
replacevar(a->lns->items[i], val);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void transa(SYMBOL* ln) {
|
|
|
|
int add = atoi(ln->content+sizeof(char));
|
|
|
|
|
2020-10-25 09:51:08 -04:00
|
|
|
if(add >= INST_LIMIT) {
|
2020-11-18 17:53:23 -05:00
|
|
|
fprintf(stderr, "'A' instruction cannot reference addresses bigger than %i; line %i\n", INST_LIMIT-1, ln->truen);
|
2020-10-25 09:51:08 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
|
|
|
|
char* out = (char*)malloc(sizeof(char) * INST_SIZE);
|
|
|
|
|
2020-10-24 21:06:07 -04:00
|
|
|
int lastbit = 1 << 15;
|
2020-11-18 17:53:23 -05:00
|
|
|
for(int i = INST_SIZE-2; i > 0; i--) {
|
2020-10-24 21:06:07 -04:00
|
|
|
if(add & (lastbit >> i))
|
|
|
|
out[i] = '1';
|
|
|
|
else
|
|
|
|
out[i] = '0';
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
|
2020-10-24 21:06:07 -04:00
|
|
|
out[INST_SIZE-1] = '\0';
|
|
|
|
out[0] = '0';
|
2020-11-18 17:53:23 -05:00
|
|
|
|
|
|
|
free(ln->content);
|
|
|
|
ln->content = out;
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
char* lookctable(TABLE* t, bool cond, char* token, const char* fieldname, int trueln) {
|
|
|
|
char* out = (char*)malloc(t->instsize);
|
|
|
|
|
2020-10-24 21:06:07 -04:00
|
|
|
if(!cond) {
|
2020-11-18 17:53:23 -05:00
|
|
|
int targsize = t->instsize - 1;
|
|
|
|
for(int i = 0; i < targsize; i++)
|
2020-10-24 21:06:07 -04:00
|
|
|
out[i] = '0';
|
2020-11-18 17:53:23 -05:00
|
|
|
out[t->instsize-1] = '\0';
|
|
|
|
return out;
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
for(int i = 0; i < t->size; i++)
|
|
|
|
if(strcmp(t->table[2*i], token) == 0) {
|
|
|
|
strcpy(out, t->table[(2*i)+1]);
|
|
|
|
return out;
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
|
2020-10-24 21:06:07 -04:00
|
|
|
fprintf(stderr, "Unexpected token '%s' for %s field; line %i\n", token, fieldname, trueln);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void transb(SYMBOL* ln) {
|
|
|
|
bool hasjmp = false;
|
|
|
|
bool hasdest = false;
|
|
|
|
bool hascmp = false;
|
2020-10-24 21:06:07 -04:00
|
|
|
int i = 0;
|
|
|
|
int tmpi = 0;
|
|
|
|
char tmp[C_TOKEN_SIZE], dest[C_TOKEN_SIZE], cmp[C_TOKEN_SIZE], jmp[C_TOKEN_SIZE];
|
|
|
|
|
|
|
|
while(1) {
|
2020-11-18 17:53:23 -05:00
|
|
|
if(ln->content[i] == '\0') {
|
2020-10-24 21:06:07 -04:00
|
|
|
tmp[tmpi] = '\0';
|
|
|
|
if(hasjmp)
|
|
|
|
strcpy(jmp, tmp);
|
|
|
|
else
|
|
|
|
strcpy(cmp, tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tmpi == C_TOKEN_SIZE-1) {
|
2020-11-18 17:53:23 -05:00
|
|
|
fprintf(stderr, "Unexpected char '%c'; line %i:%i;\n", ln->content[i], ln->truen, i+1);
|
2020-10-24 21:06:07 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
if(ln->content[i] == '=' && !hasdest && hascmp) {
|
|
|
|
hascmp = false;
|
|
|
|
hasdest = true;
|
2020-10-24 21:06:07 -04:00
|
|
|
tmp[tmpi] = '\0';
|
|
|
|
strcpy(dest, tmp);
|
|
|
|
tmpi = 0;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
if(ln->content[i] == ';' && !hasjmp && hascmp) {
|
|
|
|
hascmp = false;
|
|
|
|
hasjmp = true;
|
2020-10-24 21:06:07 -04:00
|
|
|
tmp[tmpi] = '\0';
|
|
|
|
strcpy(cmp, tmp);
|
|
|
|
tmpi = 0;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hascmp = 1;
|
2020-11-18 17:53:23 -05:00
|
|
|
tmp[tmpi] = ln->content[i];
|
2020-10-24 21:06:07 -04:00
|
|
|
tmpi++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
char* rawdest = lookctable(&desttable, hasdest, dest, "dest", ln->truen);
|
|
|
|
char* rawjmp = lookctable(&jmptable, hasjmp, jmp, "jump", ln->truen);
|
|
|
|
char* rawcmp = lookctable(&cmptable, 1, cmp, "comp", ln->truen);
|
|
|
|
|
|
|
|
int sz = sizeof(char) * INST_SIZE;
|
|
|
|
char* out = (char*)malloc(sz);
|
|
|
|
snprintf(out, sz, "111%s%s%s", rawcmp, rawdest, rawjmp);
|
|
|
|
|
|
|
|
free(ln->content);
|
|
|
|
ln->content = out;
|
|
|
|
free(rawdest);
|
|
|
|
free(rawjmp);
|
|
|
|
free(rawcmp);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void translate(ASSEMBLER* a) {
|
|
|
|
for(int i = 0; i < a->lns->count; i++)
|
|
|
|
if(a->lns->items[i]->content[0] == '@')
|
|
|
|
transa(a->lns->items[i]);
|
2020-10-24 21:06:07 -04:00
|
|
|
else
|
2020-11-18 17:53:23 -05:00
|
|
|
transb(a->lns->items[i]);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void gatherinfo(ASSEMBLER* a) {
|
|
|
|
char c;
|
|
|
|
bool readsmt = false;
|
|
|
|
bool comment = false;
|
|
|
|
int lnwidth = 1;
|
2020-10-24 21:06:07 -04:00
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
a->truelnscount = 1;
|
|
|
|
a->maxwidth = 0;
|
|
|
|
a->labels->count = 0;
|
|
|
|
a->lns->count = 0;
|
2020-10-24 21:06:07 -04:00
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
while(c = fgetc(a->input), c != -1) {
|
|
|
|
if(c == '\n') {
|
|
|
|
a->truelnscount++;
|
|
|
|
comment = false;
|
|
|
|
if(lnwidth > a->maxwidth)
|
|
|
|
a->maxwidth = lnwidth;
|
|
|
|
if(readsmt) {
|
|
|
|
if(a->lns->count == INST_LIMIT) {
|
|
|
|
fprintf(stderr, "Reached instruction limit (%i); line %i\n", INST_LIMIT, a->truelnscount);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
a->lns->count++;
|
|
|
|
}
|
|
|
|
readsmt = false;
|
|
|
|
lnwidth = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(comment)
|
|
|
|
continue;
|
|
|
|
if(c == '(') {
|
|
|
|
a->labels->count++;
|
|
|
|
comment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(c == '/') {
|
|
|
|
char nc = fgetc(a->input);
|
|
|
|
if(nc == '/') {
|
|
|
|
comment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ungetc(nc, a->input);
|
|
|
|
}
|
|
|
|
if(isspace(c)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
readsmt = true;
|
|
|
|
lnwidth++;
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|
2020-11-18 17:53:23 -05:00
|
|
|
rewind(a->input);
|
|
|
|
a->lncount = a->lns->count;
|
|
|
|
}
|
2020-10-24 21:06:07 -04:00
|
|
|
|
2020-11-18 17:53:23 -05:00
|
|
|
void freeassembler(ASSEMBLER* a) {
|
|
|
|
freesymbols(a->lns);
|
|
|
|
freesymbols(a->vars);
|
|
|
|
freesymbols(a->labels);
|
|
|
|
free(a);
|
2020-10-24 21:06:07 -04:00
|
|
|
}
|