harec

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

gen.h (2661B)


      1 #ifndef HAREC_GEN_H
      2 #define HAREC_GEN_H
      3 #include <stddef.h>
      4 #include "identifier.h"
      5 #include "qbe.h"
      6 #include "type_store.h"
      7 #include "types.h"
      8 #include "scope.h"
      9 
     10 enum fixed_aborts {
     11 	ABORT_OOB = 0,
     12 	ABORT_TYPE_ASSERTION = 1,
     13 	ABORT_ALLOC_FAILURE = 2,
     14 	ABORT_STATIC_EXCEEDED = 3,
     15 	ABORT_UNREACHABLE = 4,
     16 };
     17 
     18 struct gen_arch {
     19 	const struct qbe_type *ptr;
     20 	const struct qbe_type *sz;
     21 };
     22 
     23 enum gen_value_kind {
     24 	GV_CONST,
     25 	GV_GLOBAL,
     26 	GV_TEMP,
     27 };
     28 
     29 struct gen_value {
     30 	enum gen_value_kind kind;
     31 	bool threadlocal;
     32 	const struct type *type;
     33 	union {
     34 		char *name;
     35 		uint32_t wval;
     36 		uint64_t lval;
     37 		float sval;
     38 		double dval;
     39 	};
     40 };
     41 
     42 struct gen_binding {
     43 	const struct scope_object *object;
     44 	struct gen_value value;
     45 	struct gen_binding *next;
     46 };
     47 
     48 struct gen_defer {
     49 	const struct expression *expr;
     50 	struct gen_defer *next;
     51 };
     52 
     53 struct gen_scope {
     54 	const char *label;
     55 	const struct scope *scope;
     56 	struct gen_value result;
     57 	struct gen_value *out;
     58 	struct qbe_value *after;
     59 	struct qbe_value *end;
     60 	struct gen_defer *defers;
     61 	struct gen_scope *parent;
     62 };
     63 
     64 struct gen_context {
     65 	struct qbe_program *out;
     66 	struct gen_arch arch;
     67 	struct type_store *store;
     68 	struct identifier *ns;
     69 
     70 	int id;
     71 
     72 	struct qbe_func *current;
     73 	const struct type *functype;
     74 	struct gen_binding *bindings;
     75 	struct gen_scope *scope;
     76 	bool deferring;
     77 };
     78 
     79 struct unit;
     80 
     81 void gen(const struct unit *unit,
     82 		struct type_store *store,
     83 		struct qbe_program *out);
     84 
     85 // genutil.c
     86 struct gen_value mkgtemp(struct gen_context *ctx,
     87 	const struct type *type, const char *fmt);
     88 struct qbe_value mkqval(struct gen_context *ctx, struct gen_value *value);
     89 struct qbe_value mklval(struct gen_context *ctx, struct gen_value *value);
     90 struct qbe_value mkcopy(struct gen_context *ctx,
     91 	struct gen_value *value, const char *fmt);
     92 struct qbe_value mkqtmp(struct gen_context *ctx,
     93 	const struct qbe_type *qtype, const char *fmt);
     94 struct qbe_value mkrtfunc(struct gen_context *ctx, const char *name);
     95 struct qbe_value mklabel(struct gen_context *ctx,
     96 	struct qbe_statement *stmt, const char *fmt);
     97 void branch_copyresult(struct gen_context *ctx, struct gen_value result,
     98 	struct gen_value merged, struct gen_value *out);
     99 
    100 // qinstr.c
    101 enum qbe_instr alloc_for_align(size_t align);
    102 enum qbe_instr store_for_type(struct gen_context *ctx, const struct type *type);
    103 enum qbe_instr load_for_type(struct gen_context *ctx, const struct type *type);
    104 enum qbe_instr binarithm_for_op(struct gen_context *ctx,
    105 	enum binarithm_operator op, const struct type *type);
    106 
    107 // qtype.c
    108 const struct qbe_type *qtype_lookup(struct gen_context *ctx,
    109 	const struct type *type, bool xtype);
    110 bool type_is_aggregate(const struct type *type);
    111 
    112 #endif