mips/masm/asm.h
2024-09-10 18:23:46 -04:00

104 lines
2.1 KiB
C

/* Copyright (c) 2024 Freya Murphy */
#ifndef __ASM_H__
#define __ASM_H__
#include <stddef.h>
#include "lex.h"
#include "parse.h"
enum symbol_flag {
SYM_LOCAL,
SYM_GLOBAL,
SYM_EXTERNAL,
};
struct symbol {
char name[MAX_LEX_LENGTH];
uint32_t index;
struct section *sec;
enum symbol_flag flag;
};
struct symbol_table {
uint32_t count;
uint32_t len;
struct symbol *symbols;
};
int symtbl_init(struct symbol_table *sym_tbl);
void symtbl_free(struct symbol_table *sym_tbl);
int symtbl_push(struct symbol_table *sym_tbl, struct symbol sym);
int symtbl_find(struct symbol_table *sym_tbl, struct symbol **sym,
const char name[MAX_LEX_LENGTH]);
struct str_table {
char *ptr;
size_t size;
};
/* initalize a string table */
int strtbl_init(struct str_table *str_tbl);
/* free a string table */
void strtbl_free(struct str_table *str_tbl);
/* get a string form the string table */
int strtbl_get_str(struct str_table *str_tbl, const char *str, size_t *res);
/* get or append a string into the string table */
int strtbl_write_str(struct str_table *str_tbl, const char *str, size_t *res);
struct section_meta {
void *reltbl;
uint32_t reltbl_len;
uint32_t reltbl_idx; // reltbl idx in shdr
uint32_t shdr_idx; // sec idx in shdr
uint32_t v_addr;
};
struct assembler {
// the token lexer
struct lexer lexer;
// the expression parser
struct parser parser;
// shdr indexes
struct section_meta *meta;
size_t shstrtbl_idx;
size_t strtbl_idx;
size_t symtab_idx;
// symbols and strings
struct symbol_table sym_tbl;
struct str_table shstr_tbl;
struct str_table str_tbl;
// elf data
void *phdr; // void* since could be Elf32 or Elf64
void *shdr;
void *symtab;
uint32_t phdr_len;
uint32_t shdr_len;
uint32_t symtab_len;
};
struct assembler_arguments {
char *in_file;
char *out_file;
enum mips_isa isa;
};
int assembler_init(struct assembler *assembler, const char *path);
void assembler_free(struct assembler *assembler);
int assemble_file(struct assembler_arguments args);
/* assemble a mips32 file*/
int assemble_file_mips32(struct assembler_arguments args);
#endif /* __ASM_H__ */