1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* 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__ */
|