Skip to content

Commit

Permalink
Merge pull request #39 from cyuria/merge-passes
Browse files Browse the repository at this point in the history
feat: merge assembly passes into one
  • Loading branch information
cyuria committed Jun 11, 2024
2 parents 9ead6d2 + f700ea9 commit 4b62b1b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- run: sudo apt install qemu-user lld
- run: sudo apt update && sudo apt install qemu-user lld

- uses: ./.github/setup_cc_env
with:
Expand Down
1 change: 0 additions & 1 deletion h/generation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
void parse_file(FILE *, FILE *);
int parse_line(char *, struct sectionpos);

int symbol_forward_declare(char *);
int parse_label(char *, struct sectionpos);
1 change: 1 addition & 0 deletions h/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern struct symbolmap symbols[SYMBOLMAP_ENTRIES];

struct symbol *get_symbol(const char *);
struct symbol *create_symbol(const char *, enum symbol_types);
struct symbol *get_or_create_symbol(const char *, enum symbol_types);

struct elf64sym create_symtab_entry(const char *);

Expand Down
7 changes: 7 additions & 0 deletions src/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "debug.h"
#include "elf/output.h"
#include "form/generic.h"
#include "symbols.h"
#include "xmalloc.h"

static struct instruction *instructions = NULL;
Expand Down Expand Up @@ -69,6 +70,12 @@ int write_instruction(struct instruction i)
"Generating bytecode for %s instruction (offset: %zu)",
i.formation.name, i.position.offset);
set_section(i.position.section);

if (i.args.sym)
if (i.args.sym->section == SECTION_NULL)
logger(ERROR, error_unknown, "Symbol %s not found",
i.args.sym->name);

struct bytecode bytecode =
i.formation.form_handler(i.formation.name, i.formation.idata,
i.args, calc_fileoffset(i.position));
Expand Down
6 changes: 3 additions & 3 deletions src/directives.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ int parse_section(const char *str)
}
int parse_global(const char *str)
{
struct symbol *sym = get_symbol(str);
struct symbol *sym = get_or_create_symbol(str, SYMBOL_LABEL);
if (!sym) {
logger(ERROR, error_unknown, "Uknown symbol (%s) encountered",
sym->name);
logger(ERROR, error_internal, "Uknown symbol %s encountered",
str);
return 1;
}
sym->binding = 0x10;
Expand Down
53 changes: 14 additions & 39 deletions src/generation.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void parse_file(FILE *ifp, FILE *ofp)

linenumber = 0;

while ((nread = getl(&line, &linesize, ifp)) != (size_t)-1)
symbol_forward_declare(line);
fseek(ifp, 0L, SEEK_SET);

while ((nread = getl(&line, &linesize, ifp)) != (size_t)-1) {
Expand Down Expand Up @@ -104,7 +102,7 @@ void parse_file(FILE *ifp, FILE *ofp)
free_symbols();
}

static int parse_line_trimmed(char *, struct sectionpos);
static inline int parse_line_trimmed(char *, struct sectionpos);
int parse_line(char *line, struct sectionpos position)
{
char *trimmed_line = trim_whitespace(line);
Expand All @@ -113,63 +111,40 @@ int parse_line(char *line, struct sectionpos position)
return result;
}

static int parse_line_trimmed(char *line, struct sectionpos position)
static inline int parse_line_trimmed(char *line, struct sectionpos position)
{
logger(DEBUG, no_error, " |-> \"%s\"", line);

if (*line == '\0')
switch (*line) {
case '\0':
case ';':
return 0;

if (*line == '.' || *line == '[')
case '.':
case '[':
return parse_directive(line);
if (*line == ';')
return 0;
if (*line == '/' && *(line + 1) == '/')
return 0;
case '/':
if (line[1] == '/')
return 0;
}

if (strchr(line, ':'))
return parse_label(line, position);

return parse_asm(line, position);
}

int symbol_forward_declare(char *line)
{
char *colon = strchr(line, ':');
if (colon == NULL)
return 0;
*colon = '\0';
char *name = trim_whitespace(line);
struct symbol *sym = create_symbol(name, SYMBOL_LABEL);
free(name);
return !sym;
}

int parse_label(char *line, struct sectionpos position)
{
char *end = strchr(line, ':');
/* check for invalid label definition */
if (end == NULL)
return 0;

logger(DEBUG, no_error, "Setting position of label (%s)", line);
logger(DEBUG, no_error, "Creating label (%s)", line);

*(end++) = '\0';
char *name = trim_whitespace(line);

struct symbol *label = get_symbol(name);
if (!label) {
logger(ERROR, error_internal, "Unknown label encountered (%s)",
name);
free(name);
return 1;
}
if (label->type != SYMBOL_LABEL) {
logger(ERROR, error_internal,
"%s is defined but is not a label", name);
free(name);
return 1;
}

struct symbol *label = get_or_create_symbol(name, SYMBOL_LABEL);
free(name);

const struct sectionpos fpos = get_outputpos();
Expand Down
29 changes: 14 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ FILE *inputfile = NULL;
FILE *outputtempfile = NULL;
FILE *outputfile = NULL;

static int open(FILE **f, const char *filename, const char *flags)
{
#ifdef __STDC_LIB_EXT1__
return fopen_s(f, filename, flags));
#else
*f = fopen(filename, flags);
return !*f;
#endif
}

void closefiles(void)
{
if (inputfile)
Expand All @@ -35,13 +45,8 @@ void open_files(void)
}

logger(DEBUG, no_error, "Opening %s", *cmdargs.inputfile->filename);
#ifdef __STDC_LIB_EXT1__
if (fopen_s(&inputfile, *cmdargs.inputfile->filename, "r"))
#else
inputfile = fopen(*cmdargs.inputfile->filename, "r");
if (!inputfile)
#endif
{
if (open(&inputfile, *cmdargs.inputfile->filename, "r")) {
perror("Error: ");
logger(ERROR, error_system, "Unable to open input file");
exit(EXIT_FAILURE);
}
Expand All @@ -53,15 +58,9 @@ void open_files(void)
}

logger(DEBUG, no_error, "Opening %s", *cmdargs.outputfile->filename);
#ifdef __STDC_LIB_EXT1__
if (fopen_s(&outputfile, *cmdargs.outputfile->filename, "wb"))
#else
outputfile = fopen(*cmdargs.outputfile->filename, "wb");
if (!outputfile)
#endif
{
logger(ERROR, error_system, "Unable to open output file");
if (open(&outputfile, *cmdargs.outputfile->filename, "wb")) {
perror("Error: ");
logger(ERROR, error_system, "Unable to open output file");
exit(EXIT_FAILURE);
}

Expand Down
35 changes: 14 additions & 21 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
#include "symbols.h"
#include "xmalloc.h"

const struct args empty_args = { .rd = 0,
.rs1 = 0,
.rs2 = 0,
.imm = 0,
.sym = NULL };
const struct args empty_args = { .sym = NULL };

static char *trim_arg(char *s)
{
Expand Down Expand Up @@ -82,15 +78,6 @@ static uint32_t expect_imm(char *arg)
return (uint32_t)imm;
}

static struct symbol *expect_sym(char *arg)
{
struct symbol *sym = get_symbol(arg);
if (!sym)
logger(ERROR, error_instruction_other,
"Expected symbol but got %s", arg);
return sym;
}

static int expect_one_arg(char *first)
{
if (strtok(NULL, ",")) {
Expand Down Expand Up @@ -220,6 +207,7 @@ struct args parse_rtype(char *argstr)
.rd = expect_reg(first),
.rs1 = expect_reg(second),
.rs2 = expect_reg(third),
.sym = NULL,
};

free(first);
Expand Down Expand Up @@ -248,6 +236,7 @@ struct args parse_itype(char *argstr)
.rd = expect_reg(first),
.rs1 = expect_reg(second),
.imm = expect_imm(third),
.sym = NULL,
};

free(first);
Expand All @@ -273,6 +262,7 @@ struct args parse_ltype(char *argstr)

struct args args = {
.rd = expect_reg(first),
.sym = NULL,
};

expect_offreg(second, &args.imm, &args.rs1);
Expand All @@ -299,6 +289,7 @@ struct args parse_stype(char *argstr)

struct args args = {
.rs2 = expect_reg(second),
.sym = NULL,
};

expect_offreg(first, &args.imm, &args.rs1);
Expand Down Expand Up @@ -326,6 +317,7 @@ struct args parse_utype(char *argstr)
struct args args = {
.rd = expect_reg(first),
.imm = expect_imm(second),
.sym = NULL,
};

free(first);
Expand All @@ -351,7 +343,7 @@ struct args parse_btype(char *argstr)
struct args args = {
.rs1 = expect_reg(first),
.rs2 = expect_reg(second),
.sym = expect_sym(third),
.sym = get_or_create_symbol(third, SYMBOL_LABEL),
};

free(first);
Expand All @@ -377,7 +369,7 @@ struct args parse_bztype(char *argstr)

struct args args = {
.rs1 = expect_reg(first),
.sym = expect_sym(second),
.sym = get_or_create_symbol(second, SYMBOL_LABEL),
};

free(first);
Expand All @@ -403,6 +395,7 @@ struct args parse_pseudo(char *argstr)
struct args args = {
.rd = expect_reg(first),
.rs1 = expect_reg(second),
.sym = NULL,
};

free(first);
Expand Down Expand Up @@ -444,6 +437,7 @@ struct args parse_fence(char *argstr)
.rd = 0x0,
.rs1 = 0x0,
.imm = 0xFF,
.sym = NULL,
};

if (strtok(NULL, ","))
Expand All @@ -466,6 +460,7 @@ struct args parse_fence(char *argstr)
.rd = 0x0,
.rs1 = 0x0,
.imm = (predecessor << 4) | successor,
.sym = NULL,
};
}

Expand Down Expand Up @@ -499,7 +494,7 @@ struct args parse_jal(char *argstr)
"Expected a second argument");
}

args.sym = expect_sym(sym);
args.sym = get_or_create_symbol(sym, SYMBOL_LABEL);
free(sym);

logger(DEBUG, no_error, "Registers parsed, x%d, %s", args.rd,
Expand Down Expand Up @@ -572,7 +567,7 @@ struct args parse_la(char *argstr)

struct args args = {
.rd = expect_reg(first),
.sym = expect_sym(second),
.sym = get_or_create_symbol(second, SYMBOL_LABEL),
};

free(first);
Expand Down Expand Up @@ -618,11 +613,9 @@ struct args parse_j(char *argstr)
return empty_args;

struct args args = {
.sym = expect_sym(first),
.sym = get_or_create_symbol(first, SYMBOL_LABEL),
};

free(first);

logger(DEBUG, no_error, "Symbol parsed %s", args.sym->name);

return args;
Expand Down
17 changes: 17 additions & 0 deletions src/symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,28 @@ struct symbol *get_symbol(const char *name)
return NULL;
}

struct symbol *get_or_create_symbol(const char *name, enum symbol_types type)
{
struct symbol *sym = get_symbol(name);
if (sym)
return sym;
return create_symbol(name, type);
}

struct symbol *create_symbol(const char *name, enum symbol_types type)
{
const size_t hash = hash_str(name);

const size_t index = symbols[hash].count;

for (size_t i = 0; i < symbols[hash].count; i++) {
if (!strcmp(name, symbols[hash].data[i].name)) {
logger(ERROR, error_invalid_syntax,
"Duplicate symbol %s encountered");
return NULL;
}
}

symbols[hash].count++;
symbols[hash].data =
xrealloc(symbols[hash].data,
Expand Down
Loading

0 comments on commit 4b62b1b

Please sign in to comment.