harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit ce4f47284a6be1724af247120b6142f40affc749
parent 8472620074ff6434c3dacafdb411b15794a42815
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 20 Dec 2020 11:10:41 -0500

gen: move type mapping to separate file

Diffstat:
Mconfigure | 1+
Minclude/gen.h | 17++++++++++++++++-
Msrc/gen.c | 130-------------------------------------------------------------------------------
Asrc/qtype.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 145 insertions(+), 131 deletions(-)

diff --git a/configure b/configure @@ -12,6 +12,7 @@ harec() { src/main.c \ src/parse.c \ src/qbe.c \ + src/qtype.c \ src/trace.c \ src/type_store.c \ src/types.c \ diff --git a/include/gen.h b/include/gen.h @@ -1,10 +1,25 @@ #ifndef HAREC_GEN_H #define HAREC_GEN_H #include <stdio.h> +#include <stdint.h> +#include "identifier.h" +#include "qbe.h" struct unit; -struct qbe_program; void gen(const struct unit *unit, struct qbe_program *out); +struct gen_context { + struct qbe_program *out; + struct identifier *ns; + uint64_t id; +}; + +struct type; + +enum qbe_stype qstype_for_type(const struct type *type); +enum qbe_stype qxtype_for_type(const struct type *type); +const struct qbe_type *qtype_for_type(struct gen_context *ctx, + const struct type *type, bool extended); + #endif diff --git a/src/gen.c b/src/gen.c @@ -12,12 +12,6 @@ #include "trace.h" #include "types.h" -struct gen_context { - struct qbe_program *out; - struct identifier *ns; - uint64_t id; -}; - static char * ident_to_sym(const struct identifier *ident) { @@ -36,130 +30,6 @@ ident_to_sym(const struct identifier *ident) return strdup(ident->name); } -static enum qbe_stype -qstype_for_type(const struct type *type) -{ - switch (type->storage) { - case TYPE_STORAGE_CHAR: - case TYPE_STORAGE_I8: - case TYPE_STORAGE_U8: - // Implemented as Q_WORD - case TYPE_STORAGE_I16: - case TYPE_STORAGE_U16: - // Implemented as Q_WORD - case TYPE_STORAGE_BOOL: - case TYPE_STORAGE_I32: - case TYPE_STORAGE_U32: - case TYPE_STORAGE_RUNE: - case TYPE_STORAGE_INT: // XXX: Architecture dependent - case TYPE_STORAGE_UINT: // XXX: Architecture dependent - return Q_WORD; - case TYPE_STORAGE_I64: - case TYPE_STORAGE_U64: - case TYPE_STORAGE_SIZE: - case TYPE_STORAGE_UINTPTR: // XXX: Architecture dependent - case TYPE_STORAGE_POINTER: // XXX: Architecture dependent - return Q_LONG; - case TYPE_STORAGE_F32: - return Q_SINGLE; - case TYPE_STORAGE_F64: - return Q_DOUBLE; - case TYPE_STORAGE_VOID: - return Q__VOID; - case TYPE_STORAGE_ALIAS: - assert(0); // TODO - case TYPE_STORAGE_ARRAY: - case TYPE_STORAGE_SLICE: - case TYPE_STORAGE_STRING: - case TYPE_STORAGE_STRUCT: - case TYPE_STORAGE_TAGGED_UNION: - case TYPE_STORAGE_UNION: - case TYPE_STORAGE_FUNCTION: - assert(0); // Invariant - } - assert(0); -} - -static enum qbe_stype -qxtype_for_type(const struct type *type) -{ - switch (type->storage) { - case TYPE_STORAGE_CHAR: - case TYPE_STORAGE_I8: - case TYPE_STORAGE_U8: - return Q_BYTE; - case TYPE_STORAGE_I16: - case TYPE_STORAGE_U16: - return Q_HALF; - case TYPE_STORAGE_BOOL: - case TYPE_STORAGE_I32: - case TYPE_STORAGE_U32: - case TYPE_STORAGE_RUNE: - case TYPE_STORAGE_INT: // XXX: Architecture dependent - case TYPE_STORAGE_UINT: // XXX: Architecture dependent - case TYPE_STORAGE_I64: - case TYPE_STORAGE_U64: - case TYPE_STORAGE_SIZE: - case TYPE_STORAGE_UINTPTR: // XXX: Architecture dependent - case TYPE_STORAGE_POINTER: // XXX: Architecture dependent - case TYPE_STORAGE_F32: - case TYPE_STORAGE_F64: - case TYPE_STORAGE_VOID: - case TYPE_STORAGE_ALIAS: - case TYPE_STORAGE_ARRAY: - case TYPE_STORAGE_SLICE: - case TYPE_STORAGE_STRING: - case TYPE_STORAGE_STRUCT: - case TYPE_STORAGE_TAGGED_UNION: - case TYPE_STORAGE_UNION: - case TYPE_STORAGE_FUNCTION: - return qstype_for_type(type); - } - assert(0); -} - -static const struct qbe_type * -qtype_for_type(struct gen_context *ctx, const struct type *type, bool extended) -{ - switch (type->storage) { - case TYPE_STORAGE_CHAR: - case TYPE_STORAGE_I8: - case TYPE_STORAGE_U8: - case TYPE_STORAGE_I16: - case TYPE_STORAGE_U16: - if (extended) { - return qtype_for_xtype(qxtype_for_type(type)); - } - // Fallthrough - case TYPE_STORAGE_BOOL: - case TYPE_STORAGE_I32: - case TYPE_STORAGE_U32: - case TYPE_STORAGE_RUNE: - case TYPE_STORAGE_INT: - case TYPE_STORAGE_UINT: - case TYPE_STORAGE_I64: - case TYPE_STORAGE_U64: - case TYPE_STORAGE_SIZE: - case TYPE_STORAGE_UINTPTR: - case TYPE_STORAGE_POINTER: - case TYPE_STORAGE_F32: - case TYPE_STORAGE_F64: - case TYPE_STORAGE_VOID: - return qtype_for_xtype(qstype_for_type(type)); - case TYPE_STORAGE_ALIAS: - case TYPE_STORAGE_ARRAY: - case TYPE_STORAGE_SLICE: - case TYPE_STORAGE_STRING: - case TYPE_STORAGE_STRUCT: - case TYPE_STORAGE_TAGGED_UNION: - case TYPE_STORAGE_UNION: - assert(0); // TODO - case TYPE_STORAGE_FUNCTION: - assert(0); // Invariant - } - assert(0); // Unreachable -} - static void gen_function_decl(struct gen_context *ctx, const struct declaration *decl) { diff --git a/src/qtype.c b/src/qtype.c @@ -0,0 +1,128 @@ +#include <assert.h> +#include "gen.h" +#include "qbe.h" +#include "types.h" + +enum qbe_stype +qstype_for_type(const struct type *type) +{ + switch (type->storage) { + case TYPE_STORAGE_CHAR: + case TYPE_STORAGE_I8: + case TYPE_STORAGE_U8: + // Implemented as Q_WORD + case TYPE_STORAGE_I16: + case TYPE_STORAGE_U16: + // Implemented as Q_WORD + case TYPE_STORAGE_BOOL: + case TYPE_STORAGE_I32: + case TYPE_STORAGE_U32: + case TYPE_STORAGE_RUNE: + case TYPE_STORAGE_INT: // XXX: Architecture dependent + case TYPE_STORAGE_UINT: // XXX: Architecture dependent + return Q_WORD; + case TYPE_STORAGE_I64: + case TYPE_STORAGE_U64: + case TYPE_STORAGE_SIZE: + case TYPE_STORAGE_UINTPTR: // XXX: Architecture dependent + case TYPE_STORAGE_POINTER: // XXX: Architecture dependent + return Q_LONG; + case TYPE_STORAGE_F32: + return Q_SINGLE; + case TYPE_STORAGE_F64: + return Q_DOUBLE; + case TYPE_STORAGE_VOID: + return Q__VOID; + case TYPE_STORAGE_ALIAS: + assert(0); // TODO + case TYPE_STORAGE_ARRAY: + case TYPE_STORAGE_SLICE: + case TYPE_STORAGE_STRING: + case TYPE_STORAGE_STRUCT: + case TYPE_STORAGE_TAGGED_UNION: + case TYPE_STORAGE_UNION: + case TYPE_STORAGE_FUNCTION: + assert(0); // Invariant + } + assert(0); +} + +enum qbe_stype +qxtype_for_type(const struct type *type) +{ + switch (type->storage) { + case TYPE_STORAGE_CHAR: + case TYPE_STORAGE_I8: + case TYPE_STORAGE_U8: + return Q_BYTE; + case TYPE_STORAGE_I16: + case TYPE_STORAGE_U16: + return Q_HALF; + case TYPE_STORAGE_BOOL: + case TYPE_STORAGE_I32: + case TYPE_STORAGE_U32: + case TYPE_STORAGE_RUNE: + case TYPE_STORAGE_INT: // XXX: Architecture dependent + case TYPE_STORAGE_UINT: // XXX: Architecture dependent + case TYPE_STORAGE_I64: + case TYPE_STORAGE_U64: + case TYPE_STORAGE_SIZE: + case TYPE_STORAGE_UINTPTR: // XXX: Architecture dependent + case TYPE_STORAGE_POINTER: // XXX: Architecture dependent + case TYPE_STORAGE_F32: + case TYPE_STORAGE_F64: + case TYPE_STORAGE_VOID: + case TYPE_STORAGE_ALIAS: + case TYPE_STORAGE_ARRAY: + case TYPE_STORAGE_SLICE: + case TYPE_STORAGE_STRING: + case TYPE_STORAGE_STRUCT: + case TYPE_STORAGE_TAGGED_UNION: + case TYPE_STORAGE_UNION: + case TYPE_STORAGE_FUNCTION: + return qstype_for_type(type); + } + assert(0); +} + +const struct qbe_type * +qtype_for_type(struct gen_context *ctx, const struct type *type, bool extended) +{ + switch (type->storage) { + case TYPE_STORAGE_CHAR: + case TYPE_STORAGE_I8: + case TYPE_STORAGE_U8: + case TYPE_STORAGE_I16: + case TYPE_STORAGE_U16: + if (extended) { + return qtype_for_xtype(qxtype_for_type(type)); + } + // Fallthrough + case TYPE_STORAGE_BOOL: + case TYPE_STORAGE_I32: + case TYPE_STORAGE_U32: + case TYPE_STORAGE_RUNE: + case TYPE_STORAGE_INT: + case TYPE_STORAGE_UINT: + case TYPE_STORAGE_I64: + case TYPE_STORAGE_U64: + case TYPE_STORAGE_SIZE: + case TYPE_STORAGE_UINTPTR: + case TYPE_STORAGE_POINTER: + case TYPE_STORAGE_F32: + case TYPE_STORAGE_F64: + case TYPE_STORAGE_VOID: + return qtype_for_xtype(qstype_for_type(type)); + case TYPE_STORAGE_ALIAS: + case TYPE_STORAGE_ARRAY: + case TYPE_STORAGE_SLICE: + case TYPE_STORAGE_STRING: + case TYPE_STORAGE_STRUCT: + case TYPE_STORAGE_TAGGED_UNION: + case TYPE_STORAGE_UNION: + assert(0); // TODO + case TYPE_STORAGE_FUNCTION: + assert(0); // Invariant + } + assert(0); // Unreachable +}