aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-02-25 13:10:29 +0100
committerAndrew Kelley <andrew@ziglang.org>2020-02-25 17:38:56 -0500
commit55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370 (patch)
tree8b08c867108b6e589ea5e6504728199a4482fbd5 /src
parent89812217b4e5fee7e2851266c17c9d47204a1573 (diff)
downloadzig-55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370.tar.gz
zig-55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370.zip
ir: Various fixes for comptime ptr handling
* Correctly fold ptrToInt on optional types * Generate null as ConstPtrSpecialNull in intToPtr * Correctly stop ptrToInt on ?*T where T is zero-sized Closes #4535
Diffstat (limited to 'src')
-rw-r--r--src/ir.cpp28
1 files changed, 21 insertions, 7 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index b0fc8dccd7..77c7c74197 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -27851,9 +27851,15 @@ static IrInstGen *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInst* source_instr, Ir
}
IrInstGen *result = ir_const(ira, source_instr, ptr_type);
- result->value->data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
- result->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
- result->value->data.x_ptr.data.hard_coded_addr.addr = addr;
+ if (ptr_type->id == ZigTypeIdOptional && addr == 0) {
+ result->value->data.x_ptr.special = ConstPtrSpecialNull;
+ result->value->data.x_ptr.mut = ConstPtrMutComptimeConst;
+ } else {
+ result->value->data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
+ result->value->data.x_ptr.mut = ConstPtrMutRuntimeVar;
+ result->value->data.x_ptr.data.hard_coded_addr.addr = addr;
+ }
+
return result;
}
@@ -27911,15 +27917,15 @@ static IrInstGen *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstSrcPtr
ZigType *usize = ira->codegen->builtin_types.entry_usize;
- // We check size explicitly so we can use get_src_ptr_type here.
- if (get_src_ptr_type(target->value->type) == nullptr) {
+ ZigType *src_ptr_type = get_src_ptr_type(target->value->type);
+ if (src_ptr_type == nullptr) {
ir_add_error(ira, &target->base,
buf_sprintf("expected pointer, found '%s'", buf_ptr(&target->value->type->name)));
return ira->codegen->invalid_inst_gen;
}
bool has_bits;
- if ((err = type_has_bits2(ira->codegen, target->value->type, &has_bits)))
+ if ((err = type_has_bits2(ira->codegen, src_ptr_type, &has_bits)))
return ira->codegen->invalid_inst_gen;
if (!has_bits) {
@@ -27932,11 +27938,19 @@ static IrInstGen *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstSrcPtr
ZigValue *val = ir_resolve_const(ira, target, UndefBad);
if (!val)
return ira->codegen->invalid_inst_gen;
- if (val->type->id == ZigTypeIdPointer && val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
+
+ // Since we've already run this type trough get_codegen_ptr_type it is
+ // safe to access the x_ptr fields
+ if (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
IrInstGen *result = ir_const(ira, &instruction->base.base, usize);
bigint_init_unsigned(&result->value->data.x_bigint, val->data.x_ptr.data.hard_coded_addr.addr);
result->value->type = usize;
return result;
+ } else if (val->data.x_ptr.special == ConstPtrSpecialNull) {
+ IrInstGen *result = ir_const(ira, &instruction->base.base, usize);
+ bigint_init_unsigned(&result->value->data.x_bigint, 0);
+ result->value->type = usize;
+ return result;
}
}