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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#ifndef HAREC_QBE_H
#define HAREC_QBE_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "util.h"
enum qbe_stype {
Q__VOID = 'V',
Q_BYTE = 'b',
Q_HALF = 'h',
Q_WORD = 'w',
Q_LONG = 'l',
Q_SINGLE = 's',
Q_DOUBLE = 'd',
Q__AGGREGATE = 'A',
Q__UNION = 'U',
};
struct qbe_type;
struct qbe_field {
const struct qbe_type *type;
size_t count;
struct qbe_field *next;
};
struct qbe_type {
enum qbe_stype stype;
bool sgn; // for sub-word types
size_t size;
// Aggregate types only:
char *name;
struct qbe_field fields;
const struct type *base;
};
// Simple type singletons
extern const struct qbe_type
qbe_sbyte,
qbe_ubyte,
qbe_shalf,
qbe_uhalf,
qbe_word,
qbe_long,
qbe_single,
qbe_double,
qbe_void;
enum qbe_value_kind {
QV_CONST,
QV_GLOBAL,
QV_LABEL,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be_statement_type {
Q_COMMENT,
Q_INSTR,
Q_LABEL,
};
struct qbe_arguments {
struct qbe_value value;
struct qbe_arguments *next;
};
struct qbe_statement {
enum qbe_statement_type type;
union {
struct {
enum qbe_instr instr;
struct qbe_value *out;
struct qbe_arguments *args;
};
char *label;
char *comment;
};
};
struct qbe_func_param {
char *name;
const struct qbe_type *type;
struct qbe_func_param *next;
};
struct qbe_statements {
size_t ln, sz;
struct qbe_statement *stmts;
};
struct qbe_func {
const struct qbe_type *returns;
struct qbe_func_param *params;
bool variadic;
struct qbe_statements prelude, body;
};
enum qbe_datatype {
QD_ZEROED,
QD_VALUE,
QD_STRING,
QD_SYMOFFS,
};
struct qbe_data_item {
enum qbe_datatype type;
union {
struct qbe_value value;
size_t zeroed;
struct {
char *str;
size_t sz;
};
struct {
const char *sym;
int64_t offset;
};
};
struct qbe_data_item *next;
};
struct qbe_data {
size_t align;
char *section, *secflags;
bool threadlocal;
struct qbe_data_item items;
};
enum qbe_defkind {
Q_TYPE,
Q_FUNC,
Q_DATA,
};
struct qbe_def {
const char *name;
int file;
enum qbe_defkind kind;
bool exported;
union {
struct qbe_func func;
struct qbe_type type;
struct qbe_data data;
};
struct qbe_def *next;
};
struct qbe_program {
struct qbe_def *defs;
struct qbe_def **next;
};
void qbe_append_def(struct qbe_program *prog, struct qbe_def *def);
void pushi(struct qbe_func *func, const struct qbe_value *out, enum qbe_instr instr, ...);
void pushprei(struct qbe_func *func, const struct qbe_value *out, enum qbe_instr instr, ...);
void pushc(struct qbe_func *func, const char *fmt, ...) FORMAT(2, 3);
void push(struct qbe_statements *stmts, struct qbe_statement *stmt);
struct qbe_value constl(uint64_t l);
struct qbe_value constw(uint32_t w);
struct qbe_value consts(float s);
struct qbe_value constd(double d);
#endif
|