aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-02-01 11:10:28 +0000
committergingerBill <bill@gingerbill.org>2020-02-01 11:10:28 +0000
commit0f399a72941c7cebcb5ad0580a9d94d1a7a37ac0 (patch)
tree249d37e76b25472674b4a8e7cb8aabec1ba6ab02 /src/ir.cpp
parent4bcb667e97fc84d3eb5d7d4df34d566baddfaa1c (diff)
Add `union #maybe`
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp54
1 files changed, 44 insertions, 10 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 5d075d7dd..2f4d491e7 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -1222,7 +1222,11 @@ irValue *ir_instr_union_tag_ptr(irProcedure *p, irValue *address) {
// i->UnionTagPtr.type = alloc_type_pointer(t_type_info_ptr);
Type *u = type_deref(ir_type(address));
+ if (is_type_union_maybe_pointer(u)) {
+ GB_PANIC("union #maybe UnionTagPtr");
+ }
i->UnionTagPtr.type = alloc_type_pointer(union_tag_type(u));
+
return v;
}
@@ -1234,6 +1238,9 @@ irValue *ir_instr_union_tag_value(irProcedure *p, irValue *address) {
if (address) address->uses += 1;
Type *u = type_deref(ir_type(address));
+ if (is_type_union_maybe_pointer(u)) {
+ GB_PANIC("union #maybe UnionTagValue");
+ }
i->UnionTagPtr.type = union_tag_type(u);
return v;
}
@@ -4351,7 +4358,9 @@ irValue *ir_emit_union_tag_value(irProcedure *proc, irValue *u) {
irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue *x) {
Type *t = ir_type(x);
- if (is_type_cstring(t)) {
+ if (is_type_pointer(t)) {
+ return ir_emit_comp(proc, op_kind, x, v_raw_nil);
+ } else if (is_type_cstring(t)) {
irValue *ptr = ir_emit_conv(proc, x, t_u8_ptr);
return ir_emit_comp(proc, op_kind, ptr, v_raw_nil);
} else if (is_type_any(t)) {
@@ -5232,8 +5241,12 @@ void ir_emit_store_union_variant(irProcedure *proc, irValue *parent, irValue *va
Type *t = type_deref(ir_type(parent));
- irValue *tag_ptr = ir_emit_union_tag_ptr(proc, parent);
- ir_emit_store(proc, tag_ptr, ir_const_union_tag(t, variant_type));
+ if (is_type_union_maybe_pointer(t)) {
+ // No tag needed!
+ } else {
+ irValue *tag_ptr = ir_emit_union_tag_ptr(proc, parent);
+ ir_emit_store(proc, tag_ptr, ir_const_union_tag(t, variant_type));
+ }
}
irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) {
@@ -5728,22 +5741,41 @@ irValue *ir_emit_union_cast(irProcedure *proc, irValue *value, Type *type, Token
GB_ASSERT_MSG(is_type_union(src), "%s", type_to_string(src_type));
Type *dst = tuple->Tuple.variables[0]->type;
-
irValue *value_ = ir_address_from_load_or_generate_local(proc, value);
- irValue *tag = ir_emit_load(proc, ir_emit_union_tag_ptr(proc, value_));
- irValue *dst_tag = ir_const_union_tag(src, dst);
+ irValue *tag = nullptr;
+ irValue *dst_tag = nullptr;
+ irValue *cond = nullptr;
+ irValue *data = nullptr;
+
+ irValue *gep0 = ir_emit_struct_ep(proc, v, 0);
+ irValue *gep1 = ir_emit_struct_ep(proc, v, 1);
+
+ if (is_type_union_maybe_pointer(src)) {
+ data = ir_emit_load(proc, ir_emit_conv(proc, value_, ir_type(gep0)));
+ } else {
+ tag = ir_emit_load(proc, ir_emit_union_tag_ptr(proc, value_));
+ dst_tag = ir_const_union_tag(src, dst);
+ }
irBlock *ok_block = ir_new_block(proc, nullptr, "union_cast.ok");
irBlock *end_block = ir_new_block(proc, nullptr, "union_cast.end");
- irValue *cond = ir_emit_comp(proc, Token_CmpEq, tag, dst_tag);
+
+ if (data != nullptr) {
+ GB_ASSERT(is_type_union_maybe_pointer(src));
+ cond = ir_emit_comp_against_nil(proc, Token_NotEq, data);
+ } else {
+ cond = ir_emit_comp(proc, Token_CmpEq, tag, dst_tag);
+ }
+
ir_emit_if(proc, cond, ok_block, end_block);
ir_start_block(proc, ok_block);
- irValue *gep0 = ir_emit_struct_ep(proc, v, 0);
- irValue *gep1 = ir_emit_struct_ep(proc, v, 1);
- irValue *data = ir_emit_load(proc, ir_emit_conv(proc, value_, ir_type(gep0)));
+
+ if (data == nullptr) {
+ data = ir_emit_load(proc, ir_emit_conv(proc, value_, ir_type(gep0)));
+ }
ir_emit_store(proc, gep0, data);
ir_emit_store(proc, gep1, v_true);
@@ -11465,6 +11497,7 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
irValue *tag_type_ptr = ir_emit_struct_ep(proc, tag, 2);
irValue *custom_align_ptr = ir_emit_struct_ep(proc, tag, 3);
irValue *no_nil_ptr = ir_emit_struct_ep(proc, tag, 4);
+ irValue *maybe_ptr = ir_emit_struct_ep(proc, tag, 5);
isize variant_count = gb_max(0, t->Union.variants.count);
irValue *memory_types = ir_type_info_member_types_offset(proc, variant_count);
@@ -11494,6 +11527,7 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
ir_emit_store(proc, custom_align_ptr, is_custom_align);
ir_emit_store(proc, no_nil_ptr, ir_const_bool(t->Union.no_nil));
+ ir_emit_store(proc, maybe_ptr, ir_const_bool(t->Union.maybe));
}
break;