Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // assembly
- //
- // Created by Ethan Laur on 3/11/14.
- // Copyright (c) 2014 Ethan Laur. All rights reserved.
- //
- #include <iostream>
- #include <queue>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "Symtab.h"
- #define DBGOUT(a); if (MDEBUG) { a; }
- #define MDEBUG 0x01
- #define MAX_LINE_LENGTH 1024
- Symtab *symtab;
- typedef std::queue<char *> lineQueue;
- lineQueue * readlines(char *buf)
- {
- lineQueue *ret = new lineQueue;
- char *str = strdup((const char *)buf);
- char *tok = strtok(str, "\n");
- do
- {
- ret->push(tok);
- } while ((tok = strtok(NULL, "\n")) != NULL);
- return ret;
- }
- char *getlinefromqueue(lineQueue * in)
- {
- if (in->size() <= 0) return NULL;
- char *str = (char *)malloc(MAX_LINE_LENGTH);
- strcpy(str, in->front());
- in->pop();
- return str;
- }
- void build_symtab(char *buf)
- {
- lineQueue *lq = readlines(buf);
- char *line, tmp[30], sym_name[28], sym_name_tmp[28];
- int curr_location = 0, i;
- char *repl = NULL, rbuf[30];
- while ((line = getlinefromqueue(lq)) != NULL)
- {
- if (sscanf(line, "%s", tmp) > 0)
- {
- if (tmp[0] == ':')
- {
- for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
- strncpy(sym_name_tmp, tmp + 1, i - 1); //grab the symbol
- sprintf(rbuf, "%s", sym_name_tmp);
- DBGOUT(printf("[DEBUG build_symtab] Inserting symbol %s to location 0x%04x\n",
- sym_name, curr_location););
- sprintf(sym_name, "$%s", sym_name_tmp);
- symtab->insertNode(sym_name, curr_location);
- //while ((repl = strstr(buf, rbuf)) != NULL)
- //{
- // repl[0] = ';';
- //}
- }
- else if (tmp[0] == ';') {} //ignore comments
- else //we have an instruction
- {
- curr_location += 4;
- }
- }
- free(line);
- }
- delete lq;
- if (line != NULL)
- free(line);
- }
- char * insert_symtab(char *buf)
- {
- char *ret = (char *)malloc(strlen(buf) + 512);
- char *tmp = NULL;
- char *repl = NULL;
- char obuf[32]; //for converting int to hex
- Symtab_struct *cursym;
- lineQueue *lq = readlines(buf);
- int i;
- strcpy(ret, "");
- while (!lq->empty())
- {
- tmp = getlinefromqueue(lq);
- for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
- {
- sprintf(obuf, "0x%04x", cursym->sym_location);
- //printf("-->looking for %s\n", cursym->sym_name);
- if ((repl = strstr(tmp, "$start")) != NULL)
- {
- printf("---->Found %s\n", cursym->sym_name);
- strncpy (repl, obuf, strlen(cursym->sym_name));
- }
- }
- strcat(ret, tmp);
- strcat(ret, "\n");
- }
- return ret;
- }
- char *process_includes(char *buf)
- {
- char *ret = NULL;
- char *tmplstr = (char *)malloc(MAX_LINE_LENGTH);
- char *tmpstr = (char *)malloc(30);
- char *finpstr = (char *)malloc(MAX_LINE_LENGTH);
- char *includepos = NULL;
- FILE *f = NULL;
- FILE *o = NULL;
- struct stat st;
- lineQueue *lq = readlines(buf);
- lineQueue *tmplq = new lineQueue; //for reading files
- ssize_t retsize;
- int read_ret, num_inc = 0;
- while (!lq->empty())
- {
- strcpy(tmplstr, lq->front());
- lq->pop();
- if ((includepos = strstr(tmplstr, "%include")) != NULL)
- {
- sscanf(tmplstr, "%%include '%s'", tmpstr);
- tmpstr[strlen(tmpstr) - 1] = 0; //trim the '
- if ((f = fopen(tmpstr, "r")) == NULL)
- {
- printf("[WARN process_includes] Cannot find file included %s\n", tmpstr);
- }
- else
- {
- read_ret = 0;
- while (read_ret != EOF)
- {
- int sp;
- for (sp = 0; sp < MAX_LINE_LENGTH - 1 && (read_ret = fscanf(f, "%c", &finpstr[sp])) != EOF;sp++);
- finpstr[sp] = 0;
- tmplq->push(finpstr);
- }
- if ((o = fopen("/tmp/as.tmp", "a")) == NULL)
- {
- if ((o = fopen("/tmp/as.tmp", "w")) == NULL)
- {
- printf("[ERR process_includes] Cannot write temporary files\n");
- exit(1);
- }
- }
- while (!tmplq->empty())
- {
- fprintf(o, "%s", tmplq->front());
- tmplq->pop();
- }
- fflush(o);
- fclose(o);
- ++num_inc;
- }
- }
- }
- if (stat("/tmp/as.tmp", &st) == 0)
- retsize = strlen(buf) + st.st_size + 256;
- else
- retsize = strlen(buf) + 256;
- ret = (char *)malloc(retsize);
- memset(ret, 0, retsize);
- strcpy(ret, buf);
- if (num_inc > 0)
- {
- if ((o = fopen("/tmp/as.tmp", "r")) == NULL)
- {
- printf("[ERR process_includes] Cannot read temproary files\n");
- exit(1);
- }
- fread(ret + strlen(buf), st.st_size, 1, o);
- fclose(o);
- }
- unlink("/tmp/as.tmp");
- while ((includepos = strstr(ret, "%include")) != NULL)
- {
- includepos[0] = ';';
- }
- //delete tmplq;
- //delete lq;
- return ret;
- }
- int main(int argc, const char * argv[])
- {
- symtab = new Symtab();
- FILE *f = fopen(argv[1], "rb");
- char buf[1024] = {0};
- int sp = 0;
- for (sp = 0; fscanf(f, "%c", &buf[sp]) != EOF; sp++);
- char *buf2 = process_includes(buf);
- build_symtab(buf2);
- char *buf3 = insert_symtab(buf2);
- printf("%s\n", buf3);
- }
- //:start:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement