mips/masm/tab.h

99 lines
1.8 KiB
C
Raw Normal View History

2024-10-04 23:41:10 +00:00
/* Copyright (c) 2024 Freya Murphy */
#ifndef __TAB_H__
#define __TAB_H__
#include <stdint.h>
#include <stddef.h>
#include "lex.h"
///
/// Symbol table
///
#define SYM_SEC_STUB (UINT32_MAX)
enum symbol_type {
SYM_LOCAL,
SYM_GLOBAL,
SYM_EXTERN,
};
struct symbol {
// the offset of the symbol in a section
uint32_t offset;
// the index of section the symbol is in
uint32_t secidx;
// index into this table
uint32_t tabidx;
// the name of the symbol
struct string name;
// type
enum symbol_type type;
};
struct symbol_table {
// length in size in sym ammt
size_t len;
size_t size;
// symbols
struct symbol *symbols;
};
/* initalize a symbol table */
int symtab_init(struct symbol_table *symtab);
/* free the symbol table */
void symtab_free(struct symbol_table *symtab);
/* add a symbol to the symbol tbl */
int symtab_push(struct symbol_table *symtab, struct symbol *sym);
/* find a symbol by name in the symbol table */
int symtab_find(struct symbol_table *symtab, struct symbol **sym,
const char *name);
/* find an existing symbol with a name or stub a temp one */
int symtab_find_or_stub(struct symbol_table *symtab, struct symbol **sym,
const struct string *const name);
///
/// Reference table
///
enum reference_type {
REF_NONE,
REF_MIPS_16,
REF_MIPS_26,
REF_MIPS_PC16,
REF_MIPS_LO16,
REF_MIPS_HI16,
};
struct reference {
enum reference_type type;
struct symbol *symbol;
uint32_t offset;
};
struct reference_table {
// size
size_t len;
size_t size;
// references
struct reference *references;
};
/* initalize a reference table */
int reftab_init(struct reference_table *reftab);
/* free the reference table */
void reftab_free(struct reference_table *reftab);
/* add a reference to the reference tbl */
int reftab_push(struct reference_table *reftab, struct reference *ref);
#endif /* __TAB_H__ */