harec

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

gen.h (2705B)


      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 	const struct type *type;
     32 	union {
     33 		char *name;
     34 		uint32_t wval;
     35 		uint64_t lval;
     36 		float sval;
     37 		double dval;
     38 	};
     39 };
     40 
     41 struct gen_binding {
     42 	const struct scope_object *object;
     43 	struct gen_value value;
     44 	struct gen_binding *next;
     45 };
     46 
     47 struct gen_defer {
     48 	const struct expression *expr;
     49 	struct gen_defer *next;
     50 };
     51 
     52 struct gen_scope {
     53 	const char *label;
     54 	const struct scope *scope;
     55 	struct gen_value result;
     56 	struct gen_value *out;
     57 	struct qbe_value *after;
     58 	struct qbe_value *end;
     59 	struct gen_defer *defers;
     60 	struct gen_scope *parent;
     61 };
     62 
     63 struct gen_context {
     64 	struct qbe_program *out;
     65 	struct gen_arch arch;
     66 	struct type_store *store;
     67 	struct identifier *ns;
     68 
     69 	uint64_t id;
     70 
     71 	struct qbe_func *current;
     72 	const struct type *functype;
     73 	struct gen_binding *bindings;
     74 	struct gen_scope *scope;
     75 	bool deferring;
     76 };
     77 
     78 struct unit;
     79 
     80 void gen(const struct unit *unit,
     81 		struct type_store *store,
     82 		struct qbe_program *out);
     83 
     84 // genutil.c
     85 char *gen_name(struct gen_context *ctx, const char *fmt);
     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