gen/
.clang-format
570 B
.gitignore
30 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
911 B
README
1.8 KiB
ast.c
5.0 KiB
ast.h
15.1 KiB
desugar.c
23.1 KiB
desugar.h
286 B
gen.c
108.5 KiB
gen.h
4.9 KiB
io.c
1.1 KiB
io.h
444 B
limits.h
1.3 KiB
module.c
10.0 KiB
module.h
2.2 KiB
options.c
1.4 KiB
options.h
472 B
parser.c
68.3 KiB
parser.h
942 B
radiance.c
3.7 KiB
ralloc.c
2.0 KiB
ralloc.h
1.1 KiB
resolver.c
109.7 KiB
resolver.h
5.6 KiB
riscv.c
12.0 KiB
riscv.h
12.0 KiB
scanner.c
10.2 KiB
scanner.h
3.2 KiB
strings.c
2.6 KiB
strings.h
407 B
symtab.c
5.7 KiB
symtab.h
4.6 KiB
types.h
1.0 KiB
util.h
1.5 KiB
symtab.h
raw
| 1 | #ifndef SYMTAB_H |
| 2 | #define SYMTAB_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include "limits.h" |
| 7 | #include "riscv.h" |
| 8 | #include "types.h" |
| 9 | |
| 10 | struct symbol_t; |
| 11 | struct node_t; |
| 12 | struct scope_t; |
| 13 | struct type_t; |
| 14 | |
| 15 | /* Type classes for values. */ |
| 16 | typedef enum { |
| 17 | TYPE_VOID = 0, /* Special value for nodes without a type. */ |
| 18 | TYPE_I8 = 1, /* Primitive types. */ |
| 19 | TYPE_I16 = 2, |
| 20 | TYPE_I32 = 3, |
| 21 | TYPE_U8 = 4, |
| 22 | TYPE_U16 = 5, |
| 23 | TYPE_U32 = 6, |
| 24 | TYPE_F32 = 7, |
| 25 | TYPE_BOOL = 8, |
| 26 | TYPE_FN = 9, /* Complex types. */ |
| 27 | TYPE_UNION = 10, |
| 28 | TYPE_RESULT = 11, |
| 29 | TYPE_RECORD = 12, |
| 30 | TYPE_ARRAY = 13, |
| 31 | TYPE_PTR = 14, |
| 32 | TYPE_SLICE = 15, |
| 33 | TYPE_OPT = 16, |
| 34 | TYPE_NEVER = 17, |
| 35 | TYPE_OPAQUE = 18 |
| 36 | } typeclass_t; |
| 37 | |
| 38 | typedef enum { |
| 39 | SYM_ANY = 0, /* Match any symbol type */ |
| 40 | SYM_VARIABLE, |
| 41 | SYM_CONSTANT, /* Constant value */ |
| 42 | SYM_FIELD, |
| 43 | SYM_VARIANT, |
| 44 | SYM_FUNCTION, |
| 45 | SYM_TYPE, |
| 46 | SYM_MODULE, |
| 47 | } symkind_t; |
| 48 | |
| 49 | /* Symbol table scope. */ |
| 50 | typedef struct scope_t { |
| 51 | struct module_t *mod; |
| 52 | struct scope_t *parent; |
| 53 | struct symbol_t *symbols[MAX_SCOPE_SYMBOLS]; |
| 54 | u16 nsymbols; |
| 55 | } scope_t; |
| 56 | |
| 57 | /* Field, function or module attributes. */ |
| 58 | typedef enum { |
| 59 | ATTRIB_NONE = 0, |
| 60 | ATTRIB_PUB = 1 << 0, |
| 61 | ATTRIB_DEFAULT = 1 << 1, |
| 62 | ATTRIB_EXTERN = 1 << 2, |
| 63 | ATTRIB_TEST = 1 << 3, |
| 64 | ATTRIB_INTRINSIC = 1 << 4, |
| 65 | } attrib_t; |
| 66 | |
| 67 | /* Stack frame. */ |
| 68 | typedef struct frame_t { |
| 69 | i32 size; /* Maximum temporary stack usage (includes frame header). */ |
| 70 | i32 sp; /* Temporary allocation cursor. */ |
| 71 | } frame_t; |
| 72 | |
| 73 | /* Memory location for a value. */ |
| 74 | typedef enum { LOC_NONE = 0, LOC_REG, LOC_STACK, LOC_IMM, LOC_ADDR } memloc_t; |
| 75 | |
| 76 | /* Offset from register, on stack. */ |
| 77 | typedef struct { |
| 78 | reg_t base; |
| 79 | int offset; |
| 80 | } offset_t; |
| 81 | |
| 82 | /* Address in memory with optional offset */ |
| 83 | typedef struct { |
| 84 | usize base; |
| 85 | int offset; |
| 86 | } addr_t; |
| 87 | |
| 88 | /* Immediates. */ |
| 89 | typedef union { |
| 90 | bool b; |
| 91 | i32 i; |
| 92 | u32 u; |
| 93 | f32 f; |
| 94 | } imm_t; |
| 95 | |
| 96 | /* Value handled by code generator. */ |
| 97 | typedef struct value_t { |
| 98 | struct type_t *type; |
| 99 | memloc_t loc; |
| 100 | bool temp; |
| 101 | union { |
| 102 | reg_t reg; /* Register. */ |
| 103 | offset_t off; /* Offset from a base register. */ |
| 104 | imm_t imm; /* Stored as an immediate. */ |
| 105 | addr_t adr; /* Stored at a static address */ |
| 106 | } as; |
| 107 | } value_t; |
| 108 | |
| 109 | typedef struct symbol_t { |
| 110 | const char *name; /* Symbol name. */ |
| 111 | u16 length; /* Symbol name length. */ |
| 112 | char qualified[MAX_QUALIFIED_NAME]; /* Fully qualified name */ |
| 113 | struct node_t *node; /* Node defining the symbol. */ |
| 114 | struct scope_t *scope; /* Scope where the symbol is defined */ |
| 115 | symkind_t kind; /* Kind of symbol. */ |
| 116 | union { |
| 117 | struct { /* Variable entry. */ |
| 118 | struct type_t *typ; /* Variable type. */ |
| 119 | struct value_t val; /* Variable memory value. */ |
| 120 | i32 align; /* Alignment override (bytes). */ |
| 121 | } var; |
| 122 | |
| 123 | struct { |
| 124 | struct type_t *typ; /* Variable type. */ |
| 125 | i32 offset; /* Offset from start of object. */ |
| 126 | } field; |
| 127 | |
| 128 | struct { |
| 129 | struct type_t *typ; |
| 130 | i32 tag; |
| 131 | } variant; |
| 132 | |
| 133 | struct { /* Function entry. */ |
| 134 | scope_t *scope; /* Inner scope. */ |
| 135 | usize addr; /* Address in memory. */ |
| 136 | frame_t frame; /* Stack frame information. */ |
| 137 | attrib_t attribs; /* Attributes */ |
| 138 | bool used; /* Whether function is called/used */ |
| 139 | } fn; |
| 140 | |
| 141 | struct { /* Type entry. */ |
| 142 | struct type_t *info; |
| 143 | } typ; |
| 144 | |
| 145 | struct module_t *mod; /* Module entry. */ |
| 146 | } e; |
| 147 | } symbol_t; |
| 148 | |
| 149 | /* Allocate a symbol. */ |
| 150 | symbol_t *alloc_symbol(symbol_t); |
| 151 | /* Insert a symbol into a scope. */ |
| 152 | bool symtab_insert(scope_t *s, const char *name, u16 length, struct node_t *n); |
| 153 | /* Insert an identifier into a scope. */ |
| 154 | bool symtab_add_ident(scope_t *s, struct node_t *ident, struct node_t *n); |
| 155 | /* Add an imported symbol as an alias in the current scope. */ |
| 156 | bool symtab_add_alias(scope_t *s, struct node_t *ident, symbol_t *original); |
| 157 | /* Add a symbol directly to a scope. */ |
| 158 | void symtab_add_symbol(scope_t *s, symbol_t *sym); |
| 159 | /* Lookup a symbol in the given scope only. */ |
| 160 | symbol_t *symtab_scope_lookup( |
| 161 | scope_t *s, const char *name, u16 length, symkind_t kind |
| 162 | ); |
| 163 | /* Lookup a symbol in current and parent scopes. */ |
| 164 | symbol_t *symtab_lookup( |
| 165 | scope_t *s, const char *name, u16 length, symkind_t kind |
| 166 | ); |
| 167 | /* Create a new scope with the given parent. */ |
| 168 | scope_t *symtab_scope(scope_t *parent, struct module_t *mod); |
| 169 | |
| 170 | #endif |