Skip to content

Commit

Permalink
ASM memleak fix, error processing improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mmamayka committed Nov 14, 2020
1 parent 322537c commit 6cd3a95
Show file tree
Hide file tree
Showing 27 changed files with 672 additions and 700 deletions.
Binary file modified Processor/Assembler/bin/assembler
Binary file not shown.
2 changes: 1 addition & 1 deletion Processor/Assembler/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BINNAME := assembler
DOXCONF := doxygen

run: $(BINPATH)/$(BINNAME)
./$< example.asm example.bin


build: $(BINPATH)/$(BINNAME)

Expand Down
9 changes: 8 additions & 1 deletion Processor/Assembler/src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ttrack/dbg.h>
#include <ttrack/binbuf.h>
#include <libcommon/labeldic.h>
#include <stdio.h>
#include <stdlib.h>

Expand All @@ -23,11 +24,17 @@ int main(int argc, char* argv[])
exit(EXIT_FAILURE);
}

if(!parser_init) {
exit(EXIT_FAILURE);
}

if(!parser_pass(argv[1], 0) ||
(binbuf_reset(), !parser_pass(argv[1], 1))) {
exit(EXIT_FAILURE);
}


parser_free();

FILE* ofile = fopen(argv[2], "wb");
if(ofile == NULL || binbuf_flush(ofile) != BINBUF_ERR_OK) {
fprintf(stderr, "[ERROR] Failed to write output file\n");
Expand Down
21 changes: 20 additions & 1 deletion Processor/Assembler/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ static int const format_if_label(char* str)
ASSERT(str != NULL);

size_t l = strlen(str);
ASSERT(l != 0);

if(str[l - 1] == ':') {
str[l - 1] = '\0';
RETURN(1);
Expand Down Expand Up @@ -60,7 +62,7 @@ static int const parse()
while((terrc = tokenizer_nextline(PARSER_MAX_TOKENS, tokens, &ntokens))
== TOKENIZER_ERR_OK)
{
char* const* passtokens = tokens;
char** passtokens = tokens;

if(ntokens == 0) { continue; }

Expand All @@ -70,6 +72,7 @@ static int const parse()
--ntokens;
}


if(ntokens == 0) { continue; }

cmd_parser_t* cmd_parser = find_cmd_parser(passtokens[0]);
Expand All @@ -84,6 +87,15 @@ static int const parse()
RETURN(0);
}
}

tokenizer_err_t err = tokenizer_error();
if(err != TOKENIZER_ERR_EOF) {
fprintf(stderr, "[ERROR] Tokenizer failed, reason: %s\n",
tokenizer_errstr(err));
RETURN(0);
}
tokenizer_clear_error();

RETURN(1);
}

Expand Down Expand Up @@ -117,3 +129,10 @@ int const parser_pass(char const* fname, int pedantic)
RETURN(1);
}

int const parser_init() {
return 1;
}

void parser_free() {
labeldic_free();
}
2 changes: 2 additions & 0 deletions Processor/Assembler/src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

int const parser_pass(char const* fname, int pedantic);

int const parser_init();
void parser_free();
#endif
48 changes: 35 additions & 13 deletions Processor/Assembler/src/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include <string.h>
#include "tokenizer.h"

static int const tokenizer__error_ok(tokenizer_err_t errc) {
return errc >= 0 && errc < TOKENIZER_NERRORS;
}

char const* tokenizer_errstr(tokenizer_err_t errc)
{$_
ASSERT(errc < 0 || errc >= TOKENIZER_NERRORS);
ASSERT(tokenizer__error_ok(errc));

char const* TABLE[] = {
"ok",
Expand All @@ -20,6 +24,7 @@ char const* tokenizer_errstr(tokenizer_err_t errc)
}

typedef struct {
tokenizer_err_t err;
char* text;
string_t* lines;
size_t nlines;
Expand All @@ -28,6 +33,11 @@ typedef struct {

tokenizer_t tokenizer = {};

static tokenizer_err_t tokenizer__set_error(tokenizer_err_t err)
{$_
return tokenizer.err = err;
}

static void tokenizer__remove_comments()
{$_
for(string_t* line = tokenizer.lines; line < tokenizer.lines + tokenizer.nlines;
Expand All @@ -51,10 +61,10 @@ tokenizer_err_t const tokenizer_init(FILE* file)
tokenizer.text = read_text(file, &text_size, &rt_errc);
switch(rt_errc) {
case RT_MEMORY:
RETURN(TOKENIZER_ERR_MEM);
RETURN(tokenizer__set_error(TOKENIZER_ERR_MEM));
break;
case RT_IO:
RETURN(TOKENIZER_ERR_STDIO);
RETURN(tokenizer__set_error(TOKENIZER_ERR_STDIO));
break;
default:
break;
Expand All @@ -63,7 +73,7 @@ tokenizer_err_t const tokenizer_init(FILE* file)
tokenizer.lines = get_text_lines(tokenizer.text, text_size, '\n', &tokenizer.nlines);
if(tokenizer.lines == NULL) {
free(tokenizer.text);
RETURN(TOKENIZER_ERR_MEM);
RETURN(tokenizer__set_error(TOKENIZER_ERR_MEM));
}

tokenizer.curline = 0;
Expand All @@ -88,7 +98,7 @@ tokenizer_err_t const tokenizer_nextline(size_t max_args, char* args[], size_t*
char const* SPACES = " \t";

if(tokenizer.curline == tokenizer.nlines) {
RETURN(TOKENIZER_ERR_EOF);
RETURN(tokenizer__set_error(TOKENIZER_ERR_EOF));
}

string_t* line = tokenizer.lines + tokenizer.curline;
Expand All @@ -105,7 +115,7 @@ tokenizer_err_t const tokenizer_nextline(size_t max_args, char* args[], size_t*
++tokenizer.curline;

if(pos != NULL) {
RETURN(TOKENIZER_ERR_OVERFLOW);
RETURN(tokenizer__set_error(TOKENIZER_ERR_OVERFLOW));
}

RETURN(TOKENIZER_ERR_OK);
Expand All @@ -117,16 +127,26 @@ size_t const tokenizer_nline()
RETURN(tokenizer.curline);
}

void tokenizer_reset()
tokenizer_err_t const tokenizer_error()
{$_
tokenizer_assert();
tokenizer.curline = 0;
RETURN(tokenizer.err);
$$
}
void tokenizer_clear_error()
{$_
tokenizer.err = TOKENIZER_ERR_OK;
$$
}

tokenizer_err_t const tokenizer_check()
{$_
if(tokenizer.text == NULL || tokenizer.lines == NULL ||
tokenizer.curline > tokenizer.nlines) { RETURN(TOKENIZER_ERR_STATE); }
tokenizer.curline > tokenizer.nlines) {
RETURN(tokenizer__set_error(TOKENIZER_ERR_STATE));
}
if(tokenizer.err != TOKENIZER_ERR_OK) {
return tokenizer.err;
}
RETURN(TOKENIZER_ERR_OK);
}

Expand All @@ -135,11 +155,13 @@ void tokenizer__dump(char const* funcname, char const* filename, size_t nline)
ASSERT(funcname != NULL);
ASSERT(filename != NULL);

dump("tokenizer_t dump from %s (%s %zu)\n", funcname, filename, nline);
dump("tokenizer_t dump from %s (%s %zu), reason %i (%s)\n",
funcname, filename, nline, tokenizer.err, tokenizer_errstr(tokenizer.err));
dump("{\n");

dump("\ttext [%p] = \"\n%s\n\"\n", tokenizer.text, tokenizer.text);
dump("\tlines [%p] = TODO!!!!!11!!!\n", tokenizer.lines);
dump("\terr = %i (%s)\n", tokenizer.err, tokenizer_errstr(tokenizer.err));
dump("\ttext [%p]\n", tokenizer.text);
dump("\tlines [%p]\n", tokenizer.lines);
dump("\tnlines = %zu\n", tokenizer.nlines);
dump("\tcurline = %zu\n", tokenizer.curline);

Expand Down
3 changes: 2 additions & 1 deletion Processor/Assembler/src/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ void tokenizer_free();
tokenizer_err_t const tokenizer_nextline(size_t max_args, char* args[], size_t* num);
size_t const tokenizer_nline();

void tokenizer_reset();
tokenizer_err_t const tokenizer_error();
void tokenizer_clear_error();

tokenizer_err_t const tokenizer_check();
void tokenizer__dump(char const* funcname, char const* filename, size_t nline);
Expand Down
Binary file removed Processor/Disassembler/example.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion Processor/Disassembler/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BINNAME := disassembler
DOXCONF := doxygen

run: $(BINPATH)/$(BINNAME)
./$< example.bin example.asm


build: $(BINPATH)/$(BINNAME)

Expand Down
26 changes: 12 additions & 14 deletions Processor/Disassembler/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,12 @@ int const cmd_disasm_val(char* line)

#define LABELNAME_LEN 10

char* gen_labelname()
int const gen_labelname(char* name)
{$_
char* name = (char*)malloc(sizeof(char) * LABELNAME_LEN);
if(name == NULL) {
RETURN(NULL);
}

if(snprintf(name, LABELNAME_LEN, "L_%.6zu", labeldic_size()) < 0) {
free(name);
RETURN(NULL);
RETURN(0);
}
RETURN(name);
RETURN(1);
}

int const cmd_disasm_label(char* line)
Expand All @@ -82,19 +76,21 @@ int const cmd_disasm_label(char* line)
if(binbuf_read_value(offset_t, offset) != BINBUF_ERR_OK) {
RETURN(0);
}
char const* labelname;
char const* labelname = NULL;
if((labelname = labeldic_addrname((size_t)offset)) != NULL) {
if(snprintf(line, MAX_LINE_LEN, labelname) < 0) {
RETURN(0);
}
}
else {
labelname = gen_labelname();
if(labeldic_setaddr(labelname, (size_t)offset) != LABELDIC_ERR_OK) {
free(labelname);
char nlabelname[LABELNAME_LEN];
if(!gen_labelname(nlabelname)) {
RETURN(0);
}
if(snprintf(line, MAX_LINE_LEN, labelname, (size_t)offset) < 0) {
if(labeldic_setaddr(nlabelname, (size_t)offset) != LABELDIC_ERR_OK) {
RETURN(0);
}
if(snprintf(line, MAX_LINE_LEN, nlabelname, (size_t)offset) < 0) {
RETURN(0);
}
}
Expand Down Expand Up @@ -203,6 +199,8 @@ int main(int argc, char* argv[])

fclose(ofile);

labeldic_free();

binbuf_clearerr();
binbuf_free();
$$
Expand Down
9 changes: 9 additions & 0 deletions Processor/LibCommon/hdr/libcommon/labeldic.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ typedef enum {
LABELDIC_ERR_OK,
LABELDIC_ERR_OVERFLOW,
LABELDIC_ERR_STATE,
LABELDIC_ERR_MEM,
LABELDIC_ERR_ERRANGE,
LABELDIC_NERRORS
} labeldic_err_t;

char const* labeldic_errstr(labeldic_err_t errc);

labeldic_err_t const labeldic_getaddr(char const* name, size_t* addr);
labeldic_err_t const labeldic_setaddr(char const* name, size_t addr);

char const* labeldic_addrname(size_t addr);

size_t const labeldic_size();

labeldic_err_t const labeldic_error();
void const labeldic_clear_error();

labeldic_err_t const labeldic_check();
void labeldic__dump(char const* funcname, char const* filename, size_t nline);
void labeldic__assert(char const* funcname, char const* filename, size_t nline);

void labeldic_free();

#define labeldic_dump() \
labeldic__dump(__func__, __FILE__, __LINE__)

Expand Down
2 changes: 2 additions & 0 deletions Processor/LibCommon/hdr/libcommon/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ typedef enum {
typedef unsigned char opcode_t;
typedef unsigned short offset_t;

char const* opcode_str(opcode_t opcode);

#endif
Loading

0 comments on commit 6cd3a95

Please sign in to comment.