mips/masm/asm.h

105 lines
2.1 KiB
C
Raw Normal View History

/* Copyright (c) 2024 Freya Murphy */
#ifndef __ASM_H__
#define __ASM_H__
#include <stddef.h>
2024-09-10 00:48:08 +00:00
#include "lex.h"
#include "parse.h"
2024-09-10 22:23:46 +00:00
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 {
2024-09-10 00:48:08 +00:00
char *ptr;
size_t size;
};
/* initalize a string table */
2024-09-10 00:48:08 +00:00
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);
2024-09-10 00:48:08 +00:00
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 {
2024-09-10 22:23:46 +00:00
// the token lexer
2024-09-10 00:48:08 +00:00
struct lexer lexer;
2024-09-10 22:23:46 +00:00
// the expression parser
2024-09-10 00:48:08 +00:00
struct parser parser;
2024-09-10 22:23:46 +00:00
// shdr indexes
2024-09-10 00:48:08 +00:00
struct section_meta *meta;
size_t shstrtbl_idx;
size_t strtbl_idx;
2024-09-10 22:23:46 +00:00
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;
};
2024-09-10 22:23:46 +00:00
int assembler_init(struct assembler *assembler, const char *path);
void assembler_free(struct assembler *assembler);
int assemble_file(struct assembler_arguments args);
2024-09-10 00:48:08 +00:00
/* assemble a mips32 file*/
2024-09-10 22:23:46 +00:00
int assemble_file_mips32(struct assembler_arguments args);
#endif /* __ASM_H__ */