Add union #maybe

This commit is contained in:
gingerBill
2020-02-01 11:10:28 +00:00
parent 4bcb667e97
commit 0f399a7294
10 changed files with 244 additions and 66 deletions
+47 -13
View File
@@ -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);
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);
ir_emit_if(proc, cond, ok_block, end_block);
ir_start_block(proc, ok_block);
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);
irValue *data = ir_emit_load(proc, ir_emit_conv(proc, value_, ir_type(gep0)));
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");
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);
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;