harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

qbe.h (4101B)


      1 #ifndef HAREC_QBE_H
      2 #define HAREC_QBE_H
      3 #include <stdarg.h>
      4 #include <stdbool.h>
      5 #include <stddef.h>
      6 #include <stdint.h>
      7 
      8 enum qbe_stype {
      9 	Q__VOID = 'V',
     10 	Q_BYTE = 'b',
     11 	Q_HALF = 'h',
     12 	Q_WORD = 'w',
     13 	Q_LONG = 'l',
     14 	Q_SINGLE = 's',
     15 	Q_DOUBLE = 'd',
     16 	Q__AGGREGATE = 'A',
     17 };
     18 
     19 struct qbe_type;
     20 
     21 struct qbe_field {
     22 	const struct qbe_type *type;
     23 	size_t count;
     24 	struct qbe_field *next;
     25 };
     26 
     27 struct qbe_type {
     28 	enum qbe_stype stype;
     29 	size_t size;
     30 
     31 	// Aggregate types only:
     32 	char *name;
     33 	struct qbe_field fields;
     34 	const struct type *base;
     35 };
     36 
     37 // Simple type singletons
     38 extern const struct qbe_type
     39 	qbe_byte,
     40 	qbe_half,
     41 	qbe_word,
     42 	qbe_long,
     43 	qbe_single,
     44 	qbe_double,
     45 	qbe_void,
     46 	qbe_aggregate;
     47 
     48 enum qbe_value_kind {
     49 	QV_CONST,
     50 	QV_GLOBAL,
     51 	QV_LABEL,
     52 	QV_TEMPORARY,
     53 	QV_VARIADIC,
     54 };
     55 
     56 struct qbe_value {
     57 	enum qbe_value_kind kind;
     58 	const struct qbe_type *type;
     59 	union {
     60 		char *name;
     61 		uint32_t wval;
     62 		uint64_t lval;
     63 		float sval;
     64 		double dval;
     65 	};
     66 };
     67 
     68 enum qbe_instr {
     69 	Q_ADD,
     70 	Q_ALLOC16,
     71 	Q_ALLOC4,
     72 	Q_ALLOC8,
     73 	Q_AND,
     74 	Q_CALL,
     75 	Q_CAST,
     76 	Q_CEQD,
     77 	Q_CEQL,
     78 	Q_CEQS,
     79 	Q_CEQW,
     80 	Q_CGED,
     81 	Q_CGES,
     82 	Q_CGTD,
     83 	Q_CGTS,
     84 	Q_CLED,
     85 	Q_CLES,
     86 	Q_CLTD,
     87 	Q_CLTS,
     88 	Q_CNED,
     89 	Q_CNEL,
     90 	Q_CNES,
     91 	Q_CNEW,
     92 	Q_COD,
     93 	Q_COPY,
     94 	Q_COS,
     95 	Q_CSGEL,
     96 	Q_CSGEW,
     97 	Q_CSGTL,
     98 	Q_CSGTW,
     99 	Q_CSLEL,
    100 	Q_CSLEW,
    101 	Q_CSLTL,
    102 	Q_CSLTW,
    103 	Q_CUGEL,
    104 	Q_CUGEW,
    105 	Q_CUGTL,
    106 	Q_CUGTW,
    107 	Q_CULEL,
    108 	Q_CULEW,
    109 	Q_CULTL,
    110 	Q_CULTW,
    111 	Q_CUOD,
    112 	Q_CUOS,
    113 	Q_DIV,
    114 	Q_DTOSI,
    115 	Q_DTOUI,
    116 	Q_EXTS,
    117 	Q_EXTSB,
    118 	Q_EXTSH,
    119 	Q_EXTSW,
    120 	Q_EXTUB,
    121 	Q_EXTUH,
    122 	Q_EXTUW,
    123 	Q_JMP,
    124 	Q_JNZ,
    125 	Q_LOADD,
    126 	Q_LOADL,
    127 	Q_LOADS,
    128 	Q_LOADSB,
    129 	Q_LOADSH,
    130 	Q_LOADSW,
    131 	Q_LOADUB,
    132 	Q_LOADUH,
    133 	Q_LOADUW,
    134 	Q_MUL,
    135 	Q_OR,
    136 	Q_REM,
    137 	Q_RET,
    138 	Q_NEG,
    139 	Q_SAR,
    140 	Q_SHL,
    141 	Q_SHR,
    142 	Q_SLTOF,
    143 	Q_STOREB,
    144 	Q_STORED,
    145 	Q_STOREH,
    146 	Q_STOREL,
    147 	Q_STORES,
    148 	Q_STOREW,
    149 	Q_STOSI,
    150 	Q_STOUI,
    151 	Q_SUB,
    152 	Q_SWTOF,
    153 	Q_TRUNCD,
    154 	Q_UDIV,
    155 	Q_ULTOF,
    156 	Q_UREM,
    157 	Q_UWTOF,
    158 	Q_VAARG,
    159 	Q_VASTART,
    160 	Q_XOR,
    161 
    162 	Q_LAST_INSTR,
    163 };
    164 
    165 extern const char *qbe_instr[Q_LAST_INSTR];
    166 
    167 enum qbe_statement_type {
    168 	Q_COMMENT,
    169 	Q_INSTR,
    170 	Q_LABEL,
    171 };
    172 
    173 struct qbe_arguments {
    174 	struct qbe_value value;
    175 	struct qbe_arguments *next;
    176 };
    177 
    178 struct qbe_statement {
    179 	enum qbe_statement_type type;
    180 	union {
    181 		struct {
    182 			enum qbe_instr instr;
    183 			struct qbe_value *out;
    184 			struct qbe_arguments *args;
    185 		};
    186 		char *label;
    187 		char *comment;
    188 	};
    189 };
    190 
    191 struct qbe_func_param {
    192 	char *name;
    193 	const struct qbe_type *type;
    194 	struct qbe_func_param *next;
    195 };
    196 
    197 struct qbe_statements {
    198 	size_t ln, sz;
    199 	struct qbe_statement *stmts;
    200 };
    201 
    202 struct qbe_func {
    203 	const struct qbe_type *returns;
    204 	struct qbe_func_param *params;
    205 	bool variadic;
    206 	struct qbe_statements prelude, body;
    207 };
    208 
    209 enum qbe_datatype {
    210 	QD_VALUE,
    211 	QD_ZEROED,
    212 	QD_STRING,
    213 	QD_SYMOFFS,
    214 };
    215 
    216 struct qbe_data_item {
    217 	enum qbe_datatype type;
    218 	union {
    219 		struct qbe_value value;
    220 		size_t zeroed;
    221 		struct {
    222 			char *str;
    223 			size_t sz;
    224 		};
    225 		struct {
    226 			char *sym;
    227 			long offset;
    228 		};
    229 	};
    230 	struct qbe_data_item *next;
    231 };
    232 
    233 struct qbe_data {
    234 	size_t align;
    235 	char *section, *secflags;
    236 	struct qbe_data_item items;
    237 };
    238 
    239 enum qbe_defkind {
    240 	Q_TYPE,
    241 	Q_FUNC,
    242 	Q_DATA,
    243 };
    244 
    245 struct qbe_def {
    246 	char *name;
    247 	enum qbe_defkind kind;
    248 	bool exported;
    249 	union {
    250 		struct qbe_func func;
    251 		struct qbe_type type;
    252 		struct qbe_data data;
    253 	};
    254 	struct qbe_def *next;
    255 };
    256 
    257 struct qbe_program {
    258 	struct qbe_def *defs;
    259 	struct qbe_def **next;
    260 };
    261 
    262 void qbe_append_def(struct qbe_program *prog, struct qbe_def *def);
    263 
    264 void geni(struct qbe_statement *stmt, const struct qbe_value *out, enum qbe_instr instr, ...);
    265 const char *genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt);
    266 void pushi(struct qbe_func *func, const struct qbe_value *out, enum qbe_instr instr, ...);
    267 void pushprei(struct qbe_func *func, const struct qbe_value *out, enum qbe_instr instr, ...);
    268 const char *pushl(struct qbe_func *func, uint64_t *id, const char *fmt);
    269 void pushc(struct qbe_func *func, const char *fmt, ...);
    270 void push(struct qbe_statements *stmts, struct qbe_statement *stmt);
    271 
    272 struct qbe_value *qval_dup(const struct qbe_value *val);
    273 
    274 struct qbe_value constl(uint64_t l);
    275 struct qbe_value constw(uint32_t w);
    276 struct qbe_value consts(float s);
    277 struct qbe_value constd(double d);
    278 
    279 #endif