backend.h (3454B)
1 #ifndef BACKEND_H 2 #define BACKEND_H 3 4 typedef struct Ir_program Ir_program; 5 typedef bool (*Compile_fn)(Ir_program, Sb*); 6 7 typedef struct { 8 const char *name; 9 Compile_fn compile; 10 } Backend; 11 12 typedef enum { 13 TYPE_UNKNOWN, 14 TYPE_VOID, 15 TYPE_I8, 16 TYPE_I16, 17 TYPE_I32, 18 TYPE_I64, 19 TYPE_PTR, 20 TYPE_F32, 21 TYPE_F64, 22 } Ir_type; 23 24 typedef enum Ir_kind { 25 IR_LOAD, 26 IR_STORE, 27 IR_ASSIGN, 28 IR_BINOP, 29 IR_CALL, 30 IR_SYSCALL, 31 IR_RETURN, 32 /* IR_LABEL, */ 33 IR_JUMP, 34 IR_BRANCH, 35 IR_PHI, 36 } Ir_kind; 37 38 typedef enum Ir_arg_kind { 39 ARG_NONE, 40 ARG_INT_LITERAL, 41 /*ARG_FLOAT_LITERAL,*/ 42 ARG_CSTRING_LITERAL, 43 ARG_TEMP_STORAGE, 44 ARG_ADDRESS, 45 ARG_STATIC_VARIABLE, 46 ARG_REGISTER, 47 } Ir_arg_kind; 48 49 typedef enum Ir_binop { 50 OP_ADD, 51 OP_SUB, 52 OP_MUL, 53 OP_DIV, 54 OP_LT, 55 OP_GT, 56 OP_EQ, 57 OP_LEQ, 58 OP_GEQ, 59 } Ir_binop; 60 61 typedef struct { 62 bool resolved; 63 union { 64 Sv sv; 65 size_t id; 66 }; 67 } Label; 68 69 typedef struct { 70 enum Ir_arg_kind kind; 71 Ir_type type; 72 union { 73 int64_t i64; 74 /* double f64; */ 75 ssize_t temp_id; 76 void *addr; 77 /* char *cstr; */ 78 Sv sv; 79 Sv name; 80 }; 81 } Ir_arg; 82 83 typedef struct { 84 Ir_arg *items; 85 size_t count; 86 size_t capacity; 87 } Ir_arg_list; 88 89 typedef struct { 90 Label label; 91 ssize_t temp_id; 92 } Phi_entry; 93 94 typedef struct { 95 Phi_entry *items; 96 unsigned int count; 97 unsigned int capacity; 98 } Phi_entry_list; 99 100 typedef struct { 101 Ir_arg indirect; 102 Sv name; 103 Ir_arg_list args; 104 } Ir_call_args; 105 106 typedef struct { 107 Ir_arg syscall; 108 Ir_arg_list args; 109 } Ir_syscall_args; 110 111 typedef struct { 112 enum Ir_kind kind; 113 Ir_arg result; 114 union { 115 struct { 116 Ir_binop op; 117 Ir_type op_type; 118 Ir_arg left; 119 Ir_arg right; 120 } binop; 121 struct { 122 Ir_type type; 123 Ir_arg loc; 124 } load; 125 struct { 126 Ir_arg loc; 127 Ir_arg data; 128 } store; 129 Ir_arg assign; 130 Label label; 131 struct { 132 Label label; 133 } jump; 134 struct { 135 Ir_arg cond; 136 Label true_label; 137 Label false_label; 138 } branch; 139 Ir_arg ret; 140 Ir_call_args call; 141 Ir_syscall_args syscall; 142 Phi_entry_list phi_list; 143 }; 144 } Ir; 145 146 typedef struct { 147 Ir *items; 148 size_t count; 149 size_t capacity; 150 } Ir_block; 151 152 typedef struct { 153 Ir_type *items; 154 size_t count; 155 size_t capacity; 156 } Ir_type_list; 157 158 typedef struct { 159 Ir_type type; 160 union { 161 int8_t i8; 162 uint8_t u8; 163 int16_t i16; 164 uint16_t u16; 165 int32_t i32; 166 uint32_t u32; 167 int64_t i64; 168 uint64_t u64; 169 /* double f64; 170 float f32; */ 171 Sv str; 172 char *cstr; 173 }; 174 } Ir_data; 175 176 typedef struct { 177 Sv name; 178 Ir_type type; 179 Ir_arg default_value; 180 } Ir_static_data; 181 182 typedef struct { 183 Ir_static_data *items; 184 size_t count; 185 size_t capacity; 186 } Ir_static_data_list; 187 188 typedef struct { 189 Ir_block *items; 190 size_t count; 191 size_t capacity; 192 } Ir_block_list; 193 194 typedef struct { 195 Sv *items; 196 size_t count; 197 size_t capacity; 198 } Sv_list; 199 200 typedef struct { 201 bool resolved_labels; 202 Sv name; 203 Ir_type return_type; 204 Ir_type_list argument_types; 205 Ir_block_list body; 206 Sv_list labels; 207 } Ir_function; 208 209 typedef struct { 210 Ir_function *items; 211 size_t count; 212 size_t capacity; 213 } Ir_function_list; 214 215 typedef struct { 216 Sv symbol; 217 } Ir_extern; 218 219 typedef struct { 220 Ir_extern *items; 221 size_t count; 222 size_t capacity; 223 } Ir_extern_list; 224 225 struct Ir_program { 226 Ir_function_list functions; 227 Ir_static_data_list const_data; 228 Ir_static_data_list variable_data; 229 Ir_extern_list externs; 230 }; 231 232 #endif // BACKEND_H