inline ConstGlobalRefs into ZigValue

Having ConstGlobalRefs be a pointer in ZigValue was a hack that caused
plenty of bugs. It was used to work around difficulties in type coercing
array values into slices.

However, after #3787 is merged, array values no longer type coerce into
slices, and so this provided an opportunity to clean up the code.

This has the nice effect of reducing stage1 peak RAM usage during the
std lib tests from 3.443 GiB to 3.405 GiB (saving 39 MiB).

There is one behavior test failing in this branch, which I plan to debug
after merging #3787.
master
Andrew Kelley 2019-12-01 00:29:16 -05:00
parent 951dc451d6
commit b5df18c8fd
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
6 changed files with 103 additions and 164 deletions

View File

@ -313,12 +313,6 @@ struct RuntimeHintSlice {
uint64_t len; uint64_t len;
}; };
struct ConstGlobalRefs {
LLVMValueRef llvm_value;
LLVMValueRef llvm_global;
uint32_t align;
};
enum LazyValueId { enum LazyValueId {
LazyValueIdInvalid, LazyValueIdInvalid,
LazyValueIdAlignOf, LazyValueIdAlignOf,
@ -409,8 +403,10 @@ struct LazyValueErrUnionType {
struct ZigValue { struct ZigValue {
ZigType *type; ZigType *type;
ConstValSpecial special; ConstValSpecial special;
uint32_t llvm_align;
ConstParent parent; ConstParent parent;
ConstGlobalRefs *global_refs; LLVMValueRef llvm_value;
LLVMValueRef llvm_global;
union { union {
// populated if special == ConstValSpecialStatic // populated if special == ConstValSpecialStatic

View File

@ -5908,12 +5908,7 @@ ZigValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_
ZigValue *create_const_vals(size_t count) { ZigValue *create_const_vals(size_t count) {
ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count, "ConstGlobalRefs"); return allocate<ZigValue>(count, "ZigValue");
ZigValue *vals = allocate<ZigValue>(count, "ZigValue");
for (size_t i = 0; i < count; i += 1) {
vals[i].global_refs = &global_refs[i];
}
return vals;
} }
ZigValue **alloc_const_vals_ptrs(size_t count) { ZigValue **alloc_const_vals_ptrs(size_t count) {
@ -6480,20 +6475,14 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) {
return false; return false;
return true; return true;
case ConstPtrSpecialBaseArray: case ConstPtrSpecialBaseArray:
if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val && if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val) {
a->data.x_ptr.data.base_array.array_val->global_refs !=
b->data.x_ptr.data.base_array.array_val->global_refs)
{
return false; return false;
} }
if (a->data.x_ptr.data.base_array.elem_index != b->data.x_ptr.data.base_array.elem_index) if (a->data.x_ptr.data.base_array.elem_index != b->data.x_ptr.data.base_array.elem_index)
return false; return false;
return true; return true;
case ConstPtrSpecialBaseStruct: case ConstPtrSpecialBaseStruct:
if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val && if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val) {
a->data.x_ptr.data.base_struct.struct_val->global_refs !=
b->data.x_ptr.data.base_struct.struct_val->global_refs)
{
return false; return false;
} }
if (a->data.x_ptr.data.base_struct.field_index != b->data.x_ptr.data.base_struct.field_index) if (a->data.x_ptr.data.base_struct.field_index != b->data.x_ptr.data.base_struct.field_index)
@ -6501,27 +6490,21 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) {
return true; return true;
case ConstPtrSpecialBaseErrorUnionCode: case ConstPtrSpecialBaseErrorUnionCode:
if (a->data.x_ptr.data.base_err_union_code.err_union_val != if (a->data.x_ptr.data.base_err_union_code.err_union_val !=
b->data.x_ptr.data.base_err_union_code.err_union_val && b->data.x_ptr.data.base_err_union_code.err_union_val)
a->data.x_ptr.data.base_err_union_code.err_union_val->global_refs !=
b->data.x_ptr.data.base_err_union_code.err_union_val->global_refs)
{ {
return false; return false;
} }
return true; return true;
case ConstPtrSpecialBaseErrorUnionPayload: case ConstPtrSpecialBaseErrorUnionPayload:
if (a->data.x_ptr.data.base_err_union_payload.err_union_val != if (a->data.x_ptr.data.base_err_union_payload.err_union_val !=
b->data.x_ptr.data.base_err_union_payload.err_union_val && b->data.x_ptr.data.base_err_union_payload.err_union_val)
a->data.x_ptr.data.base_err_union_payload.err_union_val->global_refs !=
b->data.x_ptr.data.base_err_union_payload.err_union_val->global_refs)
{ {
return false; return false;
} }
return true; return true;
case ConstPtrSpecialBaseOptionalPayload: case ConstPtrSpecialBaseOptionalPayload:
if (a->data.x_ptr.data.base_optional_payload.optional_val != if (a->data.x_ptr.data.base_optional_payload.optional_val !=
b->data.x_ptr.data.base_optional_payload.optional_val && b->data.x_ptr.data.base_optional_payload.optional_val)
a->data.x_ptr.data.base_optional_payload.optional_val->global_refs !=
b->data.x_ptr.data.base_optional_payload.optional_val->global_refs)
{ {
return false; return false;
} }

View File

@ -946,7 +946,7 @@ static Buf *panic_msg_buf(PanicMsgId msg_id) {
static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) { static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
ZigValue *val = &g->panic_msg_vals[msg_id]; ZigValue *val = &g->panic_msg_vals[msg_id];
if (!val->global_refs->llvm_global) { if (!val->llvm_global) {
Buf *buf_msg = panic_msg_buf(msg_id); Buf *buf_msg = panic_msg_buf(msg_id);
ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee; ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee;
@ -955,13 +955,13 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
render_const_val(g, val, ""); render_const_val(g, val, "");
render_const_val_global(g, val, ""); render_const_val_global(g, val, "");
assert(val->global_refs->llvm_global); assert(val->llvm_global);
} }
ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false, ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false); PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
ZigType *str_type = get_slice_type(g, u8_ptr_type); ZigType *str_type = get_slice_type(g, u8_ptr_type);
return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0)); return LLVMConstBitCast(val->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0));
} }
static ZigType *ptr_to_stack_trace_type(CodeGen *g) { static ZigType *ptr_to_stack_trace_type(CodeGen *g) {
@ -1727,9 +1727,9 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
if (handle_is_ptr(instruction->value->type)) { if (handle_is_ptr(instruction->value->type)) {
render_const_val_global(g, instruction->value, ""); render_const_val_global(g, instruction->value, "");
ZigType *ptr_type = get_pointer_to_type(g, instruction->value->type, true); ZigType *ptr_type = get_pointer_to_type(g, instruction->value->type, true);
instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->global_refs->llvm_global, get_llvm_type(g, ptr_type), ""); instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->llvm_global, get_llvm_type(g, ptr_type), "");
} else { } else {
instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->global_refs->llvm_value, instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value->llvm_value,
get_llvm_type(g, instruction->value->type), ""); get_llvm_type(g, instruction->value->type), "");
} }
assert(instruction->llvm_value); assert(instruction->llvm_value);
@ -6374,7 +6374,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren
case ConstParentIdNone: case ConstParentIdNone:
render_const_val(g, val, ""); render_const_val(g, val, "");
render_const_val_global(g, val, ""); render_const_val_global(g, val, "");
return val->global_refs->llvm_global; return val->llvm_global;
case ConstParentIdStruct: case ConstParentIdStruct:
return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val, return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val,
parent->data.p_struct.field_index); parent->data.p_struct.field_index);
@ -6392,7 +6392,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren
case ConstParentIdScalar: case ConstParentIdScalar:
render_const_val(g, parent->data.p_scalar.scalar_val, ""); render_const_val(g, parent->data.p_scalar.scalar_val, "");
render_const_val_global(g, parent->data.p_scalar.scalar_val, ""); render_const_val_global(g, parent->data.p_scalar.scalar_val, "");
return parent->data.p_scalar.scalar_val->global_refs->llvm_global; return parent->data.p_scalar.scalar_val->llvm_global;
} }
zig_unreachable(); zig_unreachable();
} }
@ -6623,17 +6623,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha
zig_unreachable(); zig_unreachable();
case ConstPtrSpecialRef: case ConstPtrSpecialRef:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *pointee = const_val->data.x_ptr.data.ref.pointee; ZigValue *pointee = const_val->data.x_ptr.data.ref.pointee;
render_const_val(g, pointee, ""); render_const_val(g, pointee, "");
render_const_val_global(g, pointee, ""); render_const_val_global(g, pointee, "");
const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->llvm_value = LLVMConstBitCast(pointee->llvm_global,
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
case ConstPtrSpecialBaseArray: case ConstPtrSpecialBaseArray:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val; ZigValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
assert(array_const_val->type->id == ZigTypeIdArray); assert(array_const_val->type->id == ZigTypeIdArray);
if (!type_has_bits(array_const_val->type)) { if (!type_has_bits(array_const_val->type)) {
@ -6641,102 +6639,97 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha
ZigValue *pointee = array_const_val->type->data.array.sentinel; ZigValue *pointee = array_const_val->type->data.array.sentinel;
render_const_val(g, pointee, ""); render_const_val(g, pointee, "");
render_const_val_global(g, pointee, ""); render_const_val_global(g, pointee, "");
const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->llvm_value = LLVMConstBitCast(pointee->llvm_global,
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} else { } else {
// make this a null pointer // make this a null pointer
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
} }
size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index; size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index); LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);
LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
const_val->global_refs->llvm_value = ptr_val; const_val->llvm_value = ptr_val;
return ptr_val; return ptr_val;
} }
case ConstPtrSpecialBaseStruct: case ConstPtrSpecialBaseStruct:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val; ZigValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
assert(struct_const_val->type->id == ZigTypeIdStruct); assert(struct_const_val->type->id == ZigTypeIdStruct);
if (!type_has_bits(struct_const_val->type)) { if (!type_has_bits(struct_const_val->type)) {
// make this a null pointer // make this a null pointer
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index; size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index]->gen_index; size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index]->gen_index;
LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val, LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
gen_field_index); gen_field_index);
LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
const_val->global_refs->llvm_value = ptr_val; const_val->llvm_value = ptr_val;
return ptr_val; return ptr_val;
} }
case ConstPtrSpecialBaseErrorUnionCode: case ConstPtrSpecialBaseErrorUnionCode:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val; ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val;
assert(err_union_const_val->type->id == ZigTypeIdErrorUnion); assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
if (!type_has_bits(err_union_const_val->type)) { if (!type_has_bits(err_union_const_val->type)) {
// make this a null pointer // make this a null pointer
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val); LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);
LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
const_val->global_refs->llvm_value = ptr_val; const_val->llvm_value = ptr_val;
return ptr_val; return ptr_val;
} }
case ConstPtrSpecialBaseErrorUnionPayload: case ConstPtrSpecialBaseErrorUnionPayload:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val; ZigValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
assert(err_union_const_val->type->id == ZigTypeIdErrorUnion); assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
if (!type_has_bits(err_union_const_val->type)) { if (!type_has_bits(err_union_const_val->type)) {
// make this a null pointer // make this a null pointer
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val); LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);
LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
const_val->global_refs->llvm_value = ptr_val; const_val->llvm_value = ptr_val;
return ptr_val; return ptr_val;
} }
case ConstPtrSpecialBaseOptionalPayload: case ConstPtrSpecialBaseOptionalPayload:
{ {
assert(const_val->global_refs != nullptr);
ZigValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val; ZigValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
assert(optional_const_val->type->id == ZigTypeIdOptional); assert(optional_const_val->type->id == ZigTypeIdOptional);
if (!type_has_bits(optional_const_val->type)) { if (!type_has_bits(optional_const_val->type)) {
// make this a null pointer // make this a null pointer
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type), const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
get_llvm_type(g, const_val->type)); get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val); LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);
LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type)); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
const_val->global_refs->llvm_value = ptr_val; const_val->llvm_value = ptr_val;
return ptr_val; return ptr_val;
} }
case ConstPtrSpecialHardCodedAddr: case ConstPtrSpecialHardCodedAddr:
{ {
assert(const_val->global_refs != nullptr);
uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr; uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
ZigType *usize = g->builtin_types.entry_usize; ZigType *usize = g->builtin_types.entry_usize;
const_val->global_refs->llvm_value = LLVMConstIntToPtr( const_val->llvm_value = LLVMConstIntToPtr(
LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type)); LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type));
return const_val->global_refs->llvm_value; return const_val->llvm_value;
} }
case ConstPtrSpecialFunction: case ConstPtrSpecialFunction:
return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry), return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry),
@ -7175,34 +7168,29 @@ check: switch (const_val->special) {
} }
static void render_const_val(CodeGen *g, ZigValue *const_val, const char *name) { static void render_const_val(CodeGen *g, ZigValue *const_val, const char *name) {
if (!const_val->global_refs) if (!const_val->llvm_value)
const_val->global_refs = allocate<ConstGlobalRefs>(1); const_val->llvm_value = gen_const_val(g, const_val, name);
if (!const_val->global_refs->llvm_value)
const_val->global_refs->llvm_value = gen_const_val(g, const_val, name);
if (const_val->global_refs->llvm_global) if (const_val->llvm_global)
LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value); LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);
} }
static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char *name) { static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char *name) {
if (!const_val->global_refs) if (!const_val->llvm_global) {
const_val->global_refs = allocate<ConstGlobalRefs>(1); LLVMTypeRef type_ref = const_val->llvm_value ?
LLVMTypeOf(const_val->llvm_value) : get_llvm_type(g, const_val->type);
if (!const_val->global_refs->llvm_global) {
LLVMTypeRef type_ref = const_val->global_refs->llvm_value ?
LLVMTypeOf(const_val->global_refs->llvm_value) : get_llvm_type(g, const_val->type);
LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name); LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
LLVMSetLinkage(global_value, LLVMInternalLinkage); LLVMSetLinkage(global_value, LLVMInternalLinkage);
LLVMSetGlobalConstant(global_value, true); LLVMSetGlobalConstant(global_value, true);
LLVMSetUnnamedAddr(global_value, true); LLVMSetUnnamedAddr(global_value, true);
LLVMSetAlignment(global_value, (const_val->global_refs->align == 0) ? LLVMSetAlignment(global_value, (const_val->llvm_align == 0) ?
get_abi_alignment(g, const_val->type) : const_val->global_refs->align); get_abi_alignment(g, const_val->type) : const_val->llvm_align);
const_val->global_refs->llvm_global = global_value; const_val->llvm_global = global_value;
} }
if (const_val->global_refs->llvm_value) if (const_val->llvm_value)
LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value); LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);
} }
static void generate_error_name_table(CodeGen *g) { static void generate_error_name_table(CodeGen *g) {
@ -7403,7 +7391,7 @@ static void do_code_gen(CodeGen *g) {
bool exported = (linkage != GlobalLinkageIdInternal); bool exported = (linkage != GlobalLinkageIdInternal);
render_const_val(g, var->const_value, symbol_name); render_const_val(g, var->const_value, symbol_name);
render_const_val_global(g, var->const_value, symbol_name); render_const_val_global(g, var->const_value, symbol_name);
global_value = var->const_value->global_refs->llvm_global; global_value = var->const_value->llvm_global;
if (exported) { if (exported) {
LLVMSetLinkage(global_value, to_llvm_linkage(linkage)); LLVMSetLinkage(global_value, to_llvm_linkage(linkage));
@ -7418,7 +7406,7 @@ static void do_code_gen(CodeGen *g) {
// Here we use const_value->type because that's the type of the llvm global, // Here we use const_value->type because that's the type of the llvm global,
// which we const ptr cast upon use to whatever it needs to be. // which we const ptr cast upon use to whatever it needs to be.
if (var->gen_is_const && var->const_value->type->id != ZigTypeIdFn) { if (var->gen_is_const && var->const_value->type->id != ZigTypeIdFn) {
gen_global_var(g, var, var->const_value->global_refs->llvm_value, var->const_value->type); gen_global_var(g, var, var->const_value->llvm_value, var->const_value->type);
} }
LLVMSetGlobalConstant(global_value, var->gen_is_const); LLVMSetGlobalConstant(global_value, var->gen_is_const);
@ -8012,31 +8000,26 @@ static void define_intern_values(CodeGen *g) {
{ {
auto& value = g->intern.x_undefined; auto& value = g->intern.x_undefined;
value.type = g->builtin_types.entry_undef; value.type = g->builtin_types.entry_undef;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.undefined");
value.special = ConstValSpecialStatic; value.special = ConstValSpecialStatic;
} }
{ {
auto& value = g->intern.x_void; auto& value = g->intern.x_void;
value.type = g->builtin_types.entry_void; value.type = g->builtin_types.entry_void;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.void");
value.special = ConstValSpecialStatic; value.special = ConstValSpecialStatic;
} }
{ {
auto& value = g->intern.x_null; auto& value = g->intern.x_null;
value.type = g->builtin_types.entry_null; value.type = g->builtin_types.entry_null;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.null");
value.special = ConstValSpecialStatic; value.special = ConstValSpecialStatic;
} }
{ {
auto& value = g->intern.x_unreachable; auto& value = g->intern.x_unreachable;
value.type = g->builtin_types.entry_unreachable; value.type = g->builtin_types.entry_unreachable;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.unreachable");
value.special = ConstValSpecialStatic; value.special = ConstValSpecialStatic;
} }
{ {
auto& value = g->intern.zero_byte; auto& value = g->intern.zero_byte;
value.type = g->builtin_types.entry_u8; value.type = g->builtin_types.entry_u8;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.zero_byte");
value.special = ConstValSpecialStatic; value.special = ConstValSpecialStatic;
bigint_init_unsigned(&value.data.x_bigint, 0); bigint_init_unsigned(&value.data.x_bigint, 0);
} }
@ -8669,19 +8652,10 @@ static void init(CodeGen *g) {
g->invalid_instruction = &sentinel_instructions[0]; g->invalid_instruction = &sentinel_instructions[0];
g->invalid_instruction->value = allocate<ZigValue>(1, "ZigValue"); g->invalid_instruction->value = allocate<ZigValue>(1, "ZigValue");
g->invalid_instruction->value->type = g->builtin_types.entry_invalid; g->invalid_instruction->value->type = g->builtin_types.entry_invalid;
g->invalid_instruction->value->global_refs = allocate<ConstGlobalRefs>(1);
g->unreach_instruction = &sentinel_instructions[1]; g->unreach_instruction = &sentinel_instructions[1];
g->unreach_instruction->value = allocate<ZigValue>(1, "ZigValue"); g->unreach_instruction->value = allocate<ZigValue>(1, "ZigValue");
g->unreach_instruction->value->type = g->builtin_types.entry_unreachable; g->unreach_instruction->value->type = g->builtin_types.entry_unreachable;
g->unreach_instruction->value->global_refs = allocate<ConstGlobalRefs>(1);
{
ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(PanicMsgIdCount);
for (size_t i = 0; i < PanicMsgIdCount; i += 1) {
g->panic_msg_vals[i].global_refs = &global_refs[i];
}
}
define_builtin_fns(g); define_builtin_fns(g);
Error err; Error err;

View File

@ -219,7 +219,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr,
ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on); ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on);
static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed); static ZigValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed);
static void copy_const_val(ZigValue *dest, ZigValue *src, bool same_global_refs); static void copy_const_val(ZigValue *dest, ZigValue *src);
static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align); static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);
static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target, static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
ZigType *ptr_type); ZigType *ptr_type);
@ -1165,7 +1165,6 @@ static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_no
special_instruction->base.debug_id = exec_next_debug_id(irb->exec); special_instruction->base.debug_id = exec_next_debug_id(irb->exec);
special_instruction->base.owner_bb = irb->current_basic_block; special_instruction->base.owner_bb = irb->current_basic_block;
special_instruction->base.value = allocate<ZigValue>(1, "ZigValue"); special_instruction->base.value = allocate<ZigValue>(1, "ZigValue");
special_instruction->base.value->global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs");
return special_instruction; return special_instruction;
} }
@ -8735,7 +8734,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast
if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val))) if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val)))
return err; return err;
ZigValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val); ZigValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val);
copy_const_val(child_val, &tmp, false); copy_const_val(child_val, &tmp);
return ErrorNone; return ErrorNone;
} }
@ -11018,18 +11017,14 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
} }
} }
static void copy_const_val(ZigValue *dest, ZigValue *src, bool same_global_refs) { static void copy_const_val(ZigValue *dest, ZigValue *src) {
ConstGlobalRefs *global_refs = dest->global_refs;
memcpy(dest, src, sizeof(ZigValue)); memcpy(dest, src, sizeof(ZigValue));
if (!same_global_refs) { if (src->special != ConstValSpecialStatic)
dest->global_refs = global_refs; return;
if (src->special != ConstValSpecialStatic) if (dest->type->id == ZigTypeIdStruct) {
return; dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
if (dest->type->id == ZigTypeIdStruct) { for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count); copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i]);
for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i], false);
}
} }
} }
} }
@ -11048,13 +11043,11 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInstruction *source_
case CastOpErrSet: case CastOpErrSet:
case CastOpBitCast: case CastOpBitCast:
zig_panic("TODO"); zig_panic("TODO");
case CastOpNoop: case CastOpNoop: {
{ copy_const_val(const_val, other_val);
bool same_global_refs = other_val->special == ConstValSpecialStatic; const_val->type = new_type;
copy_const_val(const_val, other_val, same_global_refs); break;
const_val->type = new_type; }
break;
}
case CastOpNumLitToConcrete: case CastOpNumLitToConcrete:
if (other_val->type->id == ZigTypeIdComptimeFloat) { if (other_val->type->id == ZigTypeIdComptimeFloat) {
assert(new_type->id == ZigTypeIdFloat); assert(new_type->id == ZigTypeIdFloat);
@ -11775,7 +11768,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
source_instr->scope, source_instr->source_node); source_instr->scope, source_instr->source_node);
const_instruction->base.value->special = ConstValSpecialStatic; const_instruction->base.value->special = ConstValSpecialStatic;
if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) { if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) {
copy_const_val(const_instruction->base.value, val, val->data.x_ptr.mut == ConstPtrMutComptimeConst); copy_const_val(const_instruction->base.value, val);
} else { } else {
const_instruction->base.value->data.x_optional = val; const_instruction->base.value->data.x_optional = val;
} }
@ -12779,7 +12772,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction *
if (instr_is_comptime(array)) { if (instr_is_comptime(array)) {
// arrays and vectors have the same ZigValue representation // arrays and vectors have the same ZigValue representation
IrInstruction *result = ir_const(ira, source_instr, vector_type); IrInstruction *result = ir_const(ira, source_instr, vector_type);
copy_const_val(result->value, array->value, false); copy_const_val(result->value, array->value);
result->value->type = vector_type; result->value->type = vector_type;
return result; return result;
} }
@ -12792,7 +12785,7 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
if (instr_is_comptime(vector)) { if (instr_is_comptime(vector)) {
// arrays and vectors have the same ZigValue representation // arrays and vectors have the same ZigValue representation
IrInstruction *result = ir_const(ira, source_instr, array_type); IrInstruction *result = ir_const(ira, source_instr, array_type);
copy_const_val(result->value, vector->value, false); copy_const_val(result->value, vector->value);
result->value->type = array_type; result->value->type = array_type;
return result; return result;
} }
@ -13080,7 +13073,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) { if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) {
IrInstruction *result = ir_const(ira, source_instr, wanted_type); IrInstruction *result = ir_const(ira, source_instr, wanted_type);
if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) { if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) {
copy_const_val(result->value, value->value, false); copy_const_val(result->value, value->value);
result->value->type = wanted_type; result->value->type = wanted_type;
} else { } else {
float_init_bigint(&result->value->data.x_bigint, value->value); float_init_bigint(&result->value->data.x_bigint, value->value);
@ -13963,7 +13956,7 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
static IrInstruction *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *instruction) { static IrInstruction *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *instruction) {
IrInstruction *result = ir_const(ira, &instruction->base, nullptr); IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
copy_const_val(result->value, instruction->base.value, true); copy_const_val(result->value, instruction->base.value);
return result; return result;
} }
@ -14502,7 +14495,7 @@ never_mind_just_calculate_it_normally:
&op1_val->data.x_array.data.s_none.elements[i], &op1_val->data.x_array.data.s_none.elements[i],
&op2_val->data.x_array.data.s_none.elements[i], &op2_val->data.x_array.data.s_none.elements[i],
bin_op_instruction, op_id, one_possible_value); bin_op_instruction, op_id, one_possible_value);
copy_const_val(&result->value->data.x_array.data.s_none.elements[i], cur_res->value, false); copy_const_val(&result->value->data.x_array.data.s_none.elements[i], cur_res->value);
} }
return result; return result;
} }
@ -15368,21 +15361,21 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
size_t next_index = 0; size_t next_index = 0;
for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) { for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i], false); copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i]);
elem_dest_val->parent.id = ConstParentIdArray; elem_dest_val->parent.id = ConstParentIdArray;
elem_dest_val->parent.data.p_array.array_val = out_array_val; elem_dest_val->parent.data.p_array.array_val = out_array_val;
elem_dest_val->parent.data.p_array.elem_index = next_index; elem_dest_val->parent.data.p_array.elem_index = next_index;
} }
for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) { for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i], false); copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i]);
elem_dest_val->parent.id = ConstParentIdArray; elem_dest_val->parent.id = ConstParentIdArray;
elem_dest_val->parent.data.p_array.array_val = out_array_val; elem_dest_val->parent.data.p_array.array_val = out_array_val;
elem_dest_val->parent.data.p_array.elem_index = next_index; elem_dest_val->parent.data.p_array.elem_index = next_index;
} }
if (next_index < full_len) { if (next_index < full_len) {
ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index]; ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
copy_const_val(elem_dest_val, sentinel, false); copy_const_val(elem_dest_val, sentinel);
elem_dest_val->parent.id = ConstParentIdArray; elem_dest_val->parent.id = ConstParentIdArray;
elem_dest_val->parent.data.p_array.array_val = out_array_val; elem_dest_val->parent.data.p_array.array_val = out_array_val;
elem_dest_val->parent.data.p_array.elem_index = next_index; elem_dest_val->parent.data.p_array.elem_index = next_index;
@ -15467,7 +15460,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
for (uint64_t x = 0; x < mult_amt; x += 1) { for (uint64_t x = 0; x < mult_amt; x += 1) {
for (uint64_t y = 0; y < old_array_len; y += 1) { for (uint64_t y = 0; y < old_array_len; y += 1) {
ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i]; ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y], false); copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y]);
elem_dest_val->parent.id = ConstParentIdArray; elem_dest_val->parent.id = ConstParentIdArray;
elem_dest_val->parent.data.p_array.array_val = out_val; elem_dest_val->parent.data.p_array.array_val = out_val;
elem_dest_val->parent.data.p_array.elem_index = i; elem_dest_val->parent.data.p_array.elem_index = i;
@ -15478,7 +15471,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
if (array_type->data.array.sentinel != nullptr) { if (array_type->data.array.sentinel != nullptr) {
ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i]; ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
copy_const_val(elem_dest_val, array_type->data.array.sentinel, false); copy_const_val(elem_dest_val, array_type->data.array.sentinel);
elem_dest_val->parent.id = ConstParentIdArray; elem_dest_val->parent.id = ConstParentIdArray;
elem_dest_val->parent.data.p_array.array_val = out_val; elem_dest_val->parent.data.p_array.array_val = out_val;
elem_dest_val->parent.data.p_array.elem_index = i; elem_dest_val->parent.data.p_array.elem_index = i;
@ -15628,7 +15621,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
var->const_value = init_val; var->const_value = init_val;
} else { } else {
var->const_value = create_const_vals(1); var->const_value = create_const_vals(1);
copy_const_val(var->const_value, init_val, false); copy_const_val(var->const_value, init_val);
} }
} }
} }
@ -15738,7 +15731,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) { if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {
assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length); assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length);
ZigValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index); ZigValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index);
copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const); copy_const_val(mem_slot, init_val);
if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) { if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
return ir_const_void(ira, &decl_var_instruction->base); return ir_const_void(ira, &decl_var_instruction->base);
@ -16217,8 +16210,8 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
} }
IrInstruction *alloca_gen; IrInstruction *alloca_gen;
if (is_comptime && value != nullptr) { if (is_comptime && value != nullptr) {
if (align > value->value->global_refs->align) { if (align > value->value->llvm_align) {
value->value->global_refs->align = align; value->value->llvm_align = align;
} }
alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false); alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false);
} else { } else {
@ -16782,7 +16775,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
arg_val = create_const_runtime(casted_arg->value->type); arg_val = create_const_runtime(casted_arg->value->type);
} }
if (arg_part_of_generic_id) { if (arg_part_of_generic_id) {
copy_const_val(&generic_id->params[generic_id->param_count], arg_val, true); copy_const_val(&generic_id->params[generic_id->param_count], arg_val);
generic_id->param_count += 1; generic_id->param_count += 1;
} }
@ -16963,7 +16956,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
IrInstruction *casted_ptr; IrInstruction *casted_ptr;
if (instr_is_comptime(ptr)) { if (instr_is_comptime(ptr)) {
casted_ptr = ir_const(ira, source_instr, struct_ptr_type); casted_ptr = ir_const(ira, source_instr, struct_ptr_type);
copy_const_val(casted_ptr->value, ptr->value, false); copy_const_val(casted_ptr->value, ptr->value);
casted_ptr->value->type = struct_ptr_type; casted_ptr->value->type = struct_ptr_type;
} else { } else {
casted_ptr = ir_build_cast(&ira->new_irb, source_instr->scope, casted_ptr = ir_build_cast(&ira->new_irb, source_instr->scope,
@ -17026,14 +17019,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
if (dest_val == nullptr) if (dest_val == nullptr)
return ira->codegen->invalid_instruction; return ira->codegen->invalid_instruction;
if (dest_val->special != ConstValSpecialRuntime) { if (dest_val->special != ConstValSpecialRuntime) {
// TODO this allows a value stored to have the original value modified and then copy_const_val(dest_val, value->value);
// have that affect what should be a copy. We need some kind of advanced copy-on-write
// system to make these two tests pass at the same time:
// * "string literal used as comptime slice is memoized"
// * "comptime modification of const struct field" - except modified to avoid
// ConstPtrMutComptimeVar, thus defeating the logic below.
bool same_global_refs = ptr->value->data.x_ptr.mut != ConstPtrMutComptimeVar;
copy_const_val(dest_val, value->value, same_global_refs);
if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar && if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar &&
!ira->new_irb.current_basic_block->must_be_comptime_source_instr) !ira->new_irb.current_basic_block->must_be_comptime_source_instr)
{ {
@ -17308,7 +17294,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
} }
IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type); IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);
copy_const_val(new_instruction->value, result, true); copy_const_val(new_instruction->value, result);
new_instruction->value->type = return_type; new_instruction->value->type = return_type;
return ir_finish_anal(ira, new_instruction); return ir_finish_anal(ira, new_instruction);
} }
@ -17465,7 +17451,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
nullptr, UndefBad); nullptr, UndefBad);
IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb, IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr); impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
copy_const_val(const_instruction->base.value, align_result, true); copy_const_val(const_instruction->base.value, align_result);
uint32_t align_bytes = 0; uint32_t align_bytes = 0;
ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes); ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);
@ -17795,7 +17781,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
if (dst_size <= src_size) { if (dst_size <= src_size) {
if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) { if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) {
copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut != ConstPtrMutComptimeVar); copy_const_val(out_val, pointee);
return ErrorNone; return ErrorNone;
} }
Buf buf = BUF_INIT; Buf buf = BUF_INIT;
@ -18158,7 +18144,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
if (value->value->special != ConstValSpecialRuntime) { if (value->value->special != ConstValSpecialRuntime) {
IrInstruction *result = ir_const(ira, &phi_instruction->base, nullptr); IrInstruction *result = ir_const(ira, &phi_instruction->base, nullptr);
copy_const_val(result->value, value->value, true); copy_const_val(result->value, value->value);
return result; return result;
} else { } else {
return value; return value;
@ -18551,7 +18537,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
if (index == array_len && array_type->data.array.sentinel != nullptr) { if (index == array_len && array_type->data.array.sentinel != nullptr) {
ZigType *elem_type = array_type->data.array.child_type; ZigType *elem_type = array_type->data.array.child_type;
IrInstruction *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base, elem_type); IrInstruction *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base, elem_type);
copy_const_val(sentinel_elem->value, array_type->data.array.sentinel, false); copy_const_val(sentinel_elem->value, array_type->data.array.sentinel);
return ir_get_ref(ira, &elem_ptr_instruction->base, sentinel_elem, true, false); return ir_get_ref(ira, &elem_ptr_instruction->base, sentinel_elem, true, false);
} }
if (index >= array_len) { if (index >= array_len) {
@ -19042,7 +19028,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
if (instr_is_comptime(container_ptr)) { if (instr_is_comptime(container_ptr)) {
IrInstruction *result = ir_const(ira, source_instr, field_ptr_type); IrInstruction *result = ir_const(ira, source_instr, field_ptr_type);
copy_const_val(result->value, container_ptr->value, false); copy_const_val(result->value, container_ptr->value);
result->value->type = field_ptr_type; result->value->type = field_ptr_type;
return result; return result;
} }
@ -20474,7 +20460,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
case ZigTypeIdErrorSet: { case ZigTypeIdErrorSet: {
if (pointee_val) { if (pointee_val) {
IrInstruction *result = ir_const(ira, &switch_target_instruction->base, nullptr); IrInstruction *result = ir_const(ira, &switch_target_instruction->base, nullptr);
copy_const_val(result->value, pointee_val, true); copy_const_val(result->value, pointee_val);
result->value->type = target_type; result->value->type = target_type;
return result; return result;
} }
@ -20970,7 +20956,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
return ira->codegen->invalid_instruction; return ira->codegen->invalid_instruction;
IrInstruction *runtime_inst = ir_const(ira, instruction, field->init_val->type); IrInstruction *runtime_inst = ir_const(ira, instruction, field->init_val->type);
copy_const_val(runtime_inst->value, field->init_val, true); copy_const_val(runtime_inst->value, field->init_val);
IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc, IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc,
container_type, true); container_type, true);
@ -21228,7 +21214,7 @@ static IrInstruction *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruct
err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true); err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true);
} }
IrInstruction *result = ir_const(ira, &instruction->base, nullptr); IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
copy_const_val(result->value, err->cached_error_name_val, true); copy_const_val(result->value, err->cached_error_name_val);
result->value->type = str_type; result->value->type = str_type;
return result; return result;
} }
@ -22665,7 +22651,7 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc
type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry)); type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry));
} }
IrInstruction *result = ir_const(ira, &instruction->base, nullptr); IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
copy_const_val(result->value, type_entry->cached_const_name_val, true); copy_const_val(result->value, type_entry->cached_const_name_val);
return result; return result;
} }
@ -23365,7 +23351,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index]; ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index];
ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index]; ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index];
copy_const_val(ptr_val, target_ptr_val, false); copy_const_val(ptr_val, target_ptr_val);
ptr_val->type = dest_ptr_type; ptr_val->type = dest_ptr_type;
ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index]; ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
@ -23658,7 +23644,7 @@ static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *s
ZigValue *src_elem_val = (v >= 0) ? ZigValue *src_elem_val = (v >= 0) ?
&a->value->data.x_array.data.s_none.elements[v] : &a->value->data.x_array.data.s_none.elements[v] :
&b->value->data.x_array.data.s_none.elements[~v]; &b->value->data.x_array.data.s_none.elements[~v];
copy_const_val(result_elem_val, src_elem_val, false); copy_const_val(result_elem_val, src_elem_val);
ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr); ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr);
} }
@ -23753,7 +23739,7 @@ static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstruction
IrInstruction *result = ir_const(ira, &instruction->base, return_type); IrInstruction *result = ir_const(ira, &instruction->base, return_type);
result->value->data.x_array.data.s_none.elements = create_const_vals(len_int); result->value->data.x_array.data.s_none.elements = create_const_vals(len_int);
for (uint32_t i = 0; i < len_int; i += 1) { for (uint32_t i = 0; i < len_int; i += 1) {
copy_const_val(&result->value->data.x_array.data.s_none.elements[i], scalar_val, false); copy_const_val(&result->value->data.x_array.data.s_none.elements[i], scalar_val);
} }
return result; return result;
} }
@ -23894,7 +23880,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
} }
for (size_t i = start; i < end; i += 1) { for (size_t i = start; i < end; i += 1) {
copy_const_val(&dest_elements[i], byte_val, true); copy_const_val(&dest_elements[i], byte_val);
} }
return ir_const_void(ira, &instruction->base); return ir_const_void(ira, &instruction->base);
@ -24073,7 +24059,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
// TODO check for noalias violations - this should be generalized to work for any function // TODO check for noalias violations - this should be generalized to work for any function
for (size_t i = 0; i < count; i += 1) { for (size_t i = 0; i < count; i += 1) {
copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true); copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i]);
} }
return ir_const_void(ira, &instruction->base); return ir_const_void(ira, &instruction->base);
@ -25515,7 +25501,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
} }
IrInstruction *result = ir_const(ira, target, result_type); IrInstruction *result = ir_const(ira, target, result_type);
copy_const_val(result->value, val, true); copy_const_val(result->value, val);
result->value->type = result_type; result->value->type = result_type;
return result; return result;
} }
@ -25597,7 +25583,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
} else { } else {
result = ir_const(ira, source_instr, dest_type); result = ir_const(ira, source_instr, dest_type);
} }
copy_const_val(result->value, val, true); copy_const_val(result->value, val);
result->value->type = dest_type; result->value->type = dest_type;
// Keep the bigger alignment, it can only help- // Keep the bigger alignment, it can only help-

View File

@ -10,6 +10,6 @@ fn checkAddress(s: []const u8) void {
} }
test "slices pointing at the same address as global array." { test "slices pointing at the same address as global array." {
checkAddress(a); checkAddress(&a);
comptime checkAddress(a); comptime checkAddress(&a);
} }

View File

@ -783,7 +783,7 @@ test "struct with var field" {
x: var, x: var,
y: var, y: var,
}; };
const pt = Point { const pt = Point{
.x = 1, .x = 1,
.y = 2, .y = 2,
}; };