commit bcca07ce73c29891b482e8206f9792f92557727c
parent 63d84a6ad670c83a4e8eaf145bfde82061f3a6bf
Author: Eyal Sawady <ecs@d2evs.net>
Date: Tue, 19 Jan 2021 11:21:10 -0500
gen: implement enums
Diffstat:
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -899,6 +899,7 @@ gen_expr_cast(struct gen_context *ctx,
switch (to->storage) {
case TYPE_STORAGE_CHAR:
+ case TYPE_STORAGE_ENUM:
case TYPE_STORAGE_U8:
case TYPE_STORAGE_I8:
case TYPE_STORAGE_I16:
@@ -912,9 +913,9 @@ gen_expr_cast(struct gen_context *ctx,
case TYPE_STORAGE_UINTPTR: // XXX: ARCH
case TYPE_STORAGE_RUNE:
case TYPE_STORAGE_SIZE: // XXX: ARCH
- if (type_is_integer(to) && to->size <= from->size) {
+ if (type_is_integer(from) && to->size <= from->size) {
op = Q_COPY;
- } else if (type_is_integer(to) && to->size > from->size) {
+ } else if (type_is_integer(from) && to->size > from->size) {
switch (from->size) {
case 4:
op = is_signed ? Q_EXTSW : Q_EXTUW;
@@ -931,8 +932,9 @@ gen_expr_cast(struct gen_context *ctx,
} else if (from->storage == TYPE_STORAGE_POINTER) {
assert(to->storage == TYPE_STORAGE_UINTPTR);
op = Q_COPY;
- } else if (type_is_float(from)) {
- assert(0); // TODO
+ } else if (from->storage == TYPE_STORAGE_RUNE) {
+ assert(to->storage == TYPE_STORAGE_U32);
+ op = Q_COPY;
} else {
assert(0); // Invariant
}
@@ -940,7 +942,6 @@ gen_expr_cast(struct gen_context *ctx,
break;
case TYPE_STORAGE_F32:
case TYPE_STORAGE_F64:
- case TYPE_STORAGE_ENUM:
assert(0); // TODO
case TYPE_STORAGE_ARRAY:
if (from->storage == TYPE_STORAGE_ARRAY) {
diff --git a/src/qtype.c b/src/qtype.c
@@ -3,6 +3,7 @@
#include <stdio.h>
#include "gen.h"
#include "qbe.h"
+#include "type_store.h"
#include "types.h"
#include "util.h"
@@ -39,7 +40,7 @@ qstype_for_type(const struct type *type)
return Q__VOID;
case TYPE_STORAGE_ALIAS:
case TYPE_STORAGE_ENUM:
- assert(0); // TODO
+ return qstype_for_type(builtin_type_for_storage(type->_enum.storage, true));
case TYPE_STORAGE_ARRAY:
case TYPE_STORAGE_SLICE:
case TYPE_STORAGE_STRING:
@@ -88,7 +89,7 @@ qxtype_for_type(const struct type *type)
case TYPE_STORAGE_FUNCTION:
return qstype_for_type(type);
case TYPE_STORAGE_ENUM:
- assert(0); // TODO
+ return qxtype_for_type(builtin_type_for_storage(type->_enum.storage, true));
}
assert(0);
}
@@ -197,7 +198,6 @@ lookup_aggregate(struct gen_context *ctx, const struct type *type)
}
break;
case TYPE_STORAGE_ENUM:
- assert(0); // TODO
case TYPE_STORAGE_ARRAY:
case TYPE_STORAGE_ALIAS:
case TYPE_STORAGE_CHAR:
@@ -244,6 +244,7 @@ qtype_for_type(struct gen_context *ctx, const struct type *type, bool extended)
}
// Fallthrough
case TYPE_STORAGE_BOOL:
+ case TYPE_STORAGE_ENUM:
case TYPE_STORAGE_I32:
case TYPE_STORAGE_U32:
case TYPE_STORAGE_RUNE:
@@ -260,7 +261,6 @@ qtype_for_type(struct gen_context *ctx, const struct type *type, bool extended)
case TYPE_STORAGE_VOID:
return qtype_for_xtype(qstype_for_type(type), false);
case TYPE_STORAGE_ARRAY:
- case TYPE_STORAGE_ENUM:
case TYPE_STORAGE_SLICE:
case TYPE_STORAGE_STRING:
case TYPE_STORAGE_STRUCT: