Merge remote-tracking branch 'origin/kill-ConstGlobalRefs' into remove-array-type-coercion

This commit is contained in:
Andrew Kelley 2019-12-01 09:58:32 -05:00
commit 78811ff4ac
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
4 changed files with 100 additions and 161 deletions

View File

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

View File

@ -5909,12 +5909,7 @@ ZigValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_
ZigValue *create_const_vals(size_t count) {
ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count, "ConstGlobalRefs");
ZigValue *vals = allocate<ZigValue>(count, "ZigValue");
for (size_t i = 0; i < count; i += 1) {
vals[i].global_refs = &global_refs[i];
}
return vals;
return allocate<ZigValue>(count, "ZigValue");
}
ZigValue **alloc_const_vals_ptrs(size_t count) {
@ -6492,20 +6487,14 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) {
return false;
return true;
case ConstPtrSpecialBaseArray:
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)
{
if (a->data.x_ptr.data.base_array.array_val != b->data.x_ptr.data.base_array.array_val) {
return false;
}
if (a->data.x_ptr.data.base_array.elem_index != b->data.x_ptr.data.base_array.elem_index)
return false;
return true;
case ConstPtrSpecialBaseStruct:
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)
{
if (a->data.x_ptr.data.base_struct.struct_val != b->data.x_ptr.data.base_struct.struct_val) {
return false;
}
if (a->data.x_ptr.data.base_struct.field_index != b->data.x_ptr.data.base_struct.field_index)
@ -6513,27 +6502,21 @@ bool const_values_equal_ptr(ZigValue *a, ZigValue *b) {
return true;
case ConstPtrSpecialBaseErrorUnionCode:
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 &&
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)
b->data.x_ptr.data.base_err_union_code.err_union_val)
{
return false;
}
return true;
case ConstPtrSpecialBaseErrorUnionPayload:
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 &&
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)
b->data.x_ptr.data.base_err_union_payload.err_union_val)
{
return false;
}
return true;
case ConstPtrSpecialBaseOptionalPayload:
if (a->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)
b->data.x_ptr.data.base_optional_payload.optional_val)
{
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) {
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);
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_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,
PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
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) {
@ -1727,9 +1727,9 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
if (handle_is_ptr(instruction->value->type)) {
render_const_val_global(g, instruction->value, "");
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 {
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), "");
}
assert(instruction->llvm_value);
@ -6374,7 +6374,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren
case ConstParentIdNone:
render_const_val(g, val, "");
render_const_val_global(g, val, "");
return val->global_refs->llvm_global;
return val->llvm_global;
case ConstParentIdStruct:
return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val,
parent->data.p_struct.field_index);
@ -6392,7 +6392,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren
case ConstParentIdScalar:
render_const_val(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();
}
@ -6623,17 +6623,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ZigValue *const_val, const cha
zig_unreachable();
case ConstPtrSpecialRef:
{
assert(const_val->global_refs != nullptr);
ZigValue *pointee = const_val->data.x_ptr.data.ref.pointee;
render_const_val(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));
return const_val->global_refs->llvm_value;
return const_val->llvm_value;
}
case ConstPtrSpecialBaseArray:
{
assert(const_val->global_refs != nullptr);
ZigValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
assert(array_const_val->type->id == ZigTypeIdArray);
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;
render_const_val(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));
return const_val->global_refs->llvm_value;
return const_val->llvm_value;
} else {
// make this a null pointer
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));
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;
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));
const_val->global_refs->llvm_value = ptr_val;
const_val->llvm_value = ptr_val;
return ptr_val;
}
case ConstPtrSpecialBaseStruct:
{
assert(const_val->global_refs != nullptr);
ZigValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
assert(struct_const_val->type->id == ZigTypeIdStruct);
if (!type_has_bits(struct_const_val->type)) {
// make this a null pointer
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));
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 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,
gen_field_index);
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;
}
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;
assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
if (!type_has_bits(err_union_const_val->type)) {
// make this a null pointer
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));
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 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;
}
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;
assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
if (!type_has_bits(err_union_const_val->type)) {
// make this a null pointer
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));
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 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;
}
case ConstPtrSpecialBaseOptionalPayload:
{
assert(const_val->global_refs != nullptr);
ZigValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
assert(optional_const_val->type->id == ZigTypeIdOptional);
if (!type_has_bits(optional_const_val->type)) {
// make this a null pointer
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));
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 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;
}
case ConstPtrSpecialHardCodedAddr:
{
assert(const_val->global_refs != nullptr);
uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
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));
return const_val->global_refs->llvm_value;
return const_val->llvm_value;
}
case ConstPtrSpecialFunction:
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) {
if (!const_val->global_refs)
const_val->global_refs = allocate<ConstGlobalRefs>(1);
if (!const_val->global_refs->llvm_value)
const_val->global_refs->llvm_value = gen_const_val(g, const_val, name);
if (!const_val->llvm_value)
const_val->llvm_value = gen_const_val(g, const_val, name);
if (const_val->global_refs->llvm_global)
LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value);
if (const_val->llvm_global)
LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);
}
static void render_const_val_global(CodeGen *g, ZigValue *const_val, const char *name) {
if (!const_val->global_refs)
const_val->global_refs = allocate<ConstGlobalRefs>(1);
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);
if (!const_val->llvm_global) {
LLVMTypeRef type_ref = const_val->llvm_value ?
LLVMTypeOf(const_val->llvm_value) : get_llvm_type(g, const_val->type);
LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
LLVMSetLinkage(global_value, LLVMInternalLinkage);
LLVMSetGlobalConstant(global_value, true);
LLVMSetUnnamedAddr(global_value, true);
LLVMSetAlignment(global_value, (const_val->global_refs->align == 0) ?
get_abi_alignment(g, const_val->type) : const_val->global_refs->align);
LLVMSetAlignment(global_value, (const_val->llvm_align == 0) ?
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)
LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value);
if (const_val->llvm_value)
LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);
}
static void generate_error_name_table(CodeGen *g) {
@ -7403,7 +7391,7 @@ static void do_code_gen(CodeGen *g) {
bool exported = (linkage != GlobalLinkageIdInternal);
render_const_val(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) {
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,
// which we const ptr cast upon use to whatever it needs to be.
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);
@ -8012,31 +8000,26 @@ static void define_intern_values(CodeGen *g) {
{
auto& value = g->intern.x_undefined;
value.type = g->builtin_types.entry_undef;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.undefined");
value.special = ConstValSpecialStatic;
}
{
auto& value = g->intern.x_void;
value.type = g->builtin_types.entry_void;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.void");
value.special = ConstValSpecialStatic;
}
{
auto& value = g->intern.x_null;
value.type = g->builtin_types.entry_null;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.null");
value.special = ConstValSpecialStatic;
}
{
auto& value = g->intern.x_unreachable;
value.type = g->builtin_types.entry_unreachable;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.unreachable");
value.special = ConstValSpecialStatic;
}
{
auto& value = g->intern.zero_byte;
value.type = g->builtin_types.entry_u8;
value.global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs.zero_byte");
value.special = ConstValSpecialStatic;
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->value = allocate<ZigValue>(1, "ZigValue");
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->value = allocate<ZigValue>(1, "ZigValue");
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);
Error err;

View File

@ -223,7 +223,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,
ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on);
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 IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
ZigType *ptr_type);
@ -1545,7 +1545,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.owner_bb = irb->current_basic_block;
special_instruction->base.value = allocate<ZigValue>(1, "ZigValue");
special_instruction->base.value->global_refs = allocate<ConstGlobalRefs>(1, "ConstGlobalRefs");
return special_instruction;
}
@ -9122,7 +9121,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)))
return err;
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;
}
@ -11348,18 +11347,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) {
ConstGlobalRefs *global_refs = dest->global_refs;
static void copy_const_val(ZigValue *dest, ZigValue *src) {
memcpy(dest, src, sizeof(ZigValue));
if (!same_global_refs) {
dest->global_refs = global_refs;
if (src->special != ConstValSpecialStatic)
return;
if (dest->type->id == ZigTypeIdStruct) {
dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
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);
}
if (src->special != ConstValSpecialStatic)
return;
if (dest->type->id == ZigTypeIdStruct) {
dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
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]);
}
}
}
@ -11378,13 +11373,11 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInstruction *source_
case CastOpErrSet:
case CastOpBitCast:
zig_panic("TODO");
case CastOpNoop:
{
bool same_global_refs = other_val->special == ConstValSpecialStatic;
copy_const_val(const_val, other_val, same_global_refs);
const_val->type = new_type;
break;
}
case CastOpNoop: {
copy_const_val(const_val, other_val);
const_val->type = new_type;
break;
}
case CastOpNumLitToConcrete:
if (other_val->type->id == ZigTypeIdComptimeFloat) {
assert(new_type->id == ZigTypeIdFloat);
@ -12105,7 +12098,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
source_instr->scope, source_instr->source_node);
const_instruction->base.value->special = ConstValSpecialStatic;
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 {
const_instruction->base.value->data.x_optional = val;
}
@ -13063,7 +13056,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction *
if (instr_is_comptime(array)) {
// arrays and vectors have the same ZigValue representation
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;
return result;
}
@ -13076,7 +13069,7 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
if (instr_is_comptime(vector)) {
// arrays and vectors have the same ZigValue representation
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;
return result;
}
@ -13364,7 +13357,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) {
IrInstruction *result = ir_const(ira, source_instr, wanted_type);
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;
} else {
float_init_bigint(&result->value->data.x_bigint, value->value);
@ -14224,7 +14217,7 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
static IrInstruction *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *instruction) {
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;
}
@ -14763,7 +14756,7 @@ never_mind_just_calculate_it_normally:
&op1_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);
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;
}
@ -15629,21 +15622,21 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
size_t next_index = 0;
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];
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.data.p_array.array_val = out_array_val;
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) {
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.data.p_array.array_val = out_array_val;
elem_dest_val->parent.data.p_array.elem_index = next_index;
}
if (next_index < full_len) {
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.data.p_array.array_val = out_array_val;
elem_dest_val->parent.data.p_array.elem_index = next_index;
@ -15728,7 +15721,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
for (uint64_t x = 0; x < mult_amt; x += 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];
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.data.p_array.array_val = out_val;
elem_dest_val->parent.data.p_array.elem_index = i;
@ -15739,7 +15732,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
if (array_type->data.array.sentinel != nullptr) {
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.data.p_array.array_val = out_val;
elem_dest_val->parent.data.p_array.elem_index = i;
@ -15889,7 +15882,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
var->const_value = init_val;
} else {
var->const_value = create_const_vals(1);
copy_const_val(var->const_value, init_val, false);
copy_const_val(var->const_value, init_val);
}
}
}
@ -15999,7 +15992,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) {
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);
copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const);
copy_const_val(mem_slot, init_val);
ira_ref(var->owner_exec->analysis);
if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
@ -16480,8 +16473,8 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
}
IrInstruction *alloca_gen;
if (is_comptime && value != nullptr) {
if (align > value->value->global_refs->align) {
value->value->global_refs->align = align;
if (align > value->value->llvm_align) {
value->value->llvm_align = align;
}
alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false);
} else {
@ -17045,7 +17038,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
arg_val = create_const_runtime(casted_arg->value->type);
}
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;
}
@ -17226,7 +17219,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
IrInstruction *casted_ptr;
if (instr_is_comptime(ptr)) {
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;
} else {
casted_ptr = ir_build_cast(&ira->new_irb, source_instr->scope,
@ -17289,14 +17282,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
if (dest_val == nullptr)
return ira->codegen->invalid_instruction;
if (dest_val->special != ConstValSpecialRuntime) {
// TODO this allows a value stored to have the original value modified and then
// 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);
copy_const_val(dest_val, value->value);
if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar &&
!ira->new_irb.current_basic_block->must_be_comptime_source_instr)
{
@ -17571,7 +17557,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
}
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;
return ir_finish_anal(ira, new_instruction);
}
@ -17728,7 +17714,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
nullptr, UndefBad);
IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
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;
ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);
@ -18058,7 +18044,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
if (dst_size <= src_size) {
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;
}
Buf buf = BUF_INIT;
@ -18421,7 +18407,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
if (value->value->special != ConstValSpecialRuntime) {
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;
} else {
return value;
@ -18814,7 +18800,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
if (index == array_len && array_type->data.array.sentinel != nullptr) {
ZigType *elem_type = array_type->data.array.child_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);
}
if (index >= array_len) {
@ -19305,7 +19291,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
if (instr_is_comptime(container_ptr)) {
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;
return result;
}
@ -20737,7 +20723,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
case ZigTypeIdErrorSet: {
if (pointee_val) {
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;
return result;
}
@ -21233,7 +21219,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
return ira->codegen->invalid_instruction;
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,
container_type, true);
@ -21493,7 +21479,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);
}
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;
return result;
}
@ -22978,7 +22964,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));
}
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;
}
@ -23678,7 +23664,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
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];
copy_const_val(ptr_val, target_ptr_val, false);
copy_const_val(ptr_val, target_ptr_val);
ptr_val->type = dest_ptr_type;
ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
@ -23971,7 +23957,7 @@ static IrInstruction *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInstruction *s
ZigValue *src_elem_val = (v >= 0) ?
&a->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);
}
@ -24066,7 +24052,7 @@ static IrInstruction *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstruction
IrInstruction *result = ir_const(ira, &instruction->base, return_type);
result->value->data.x_array.data.s_none.elements = create_const_vals(len_int);
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;
}
@ -24207,7 +24193,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
}
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);
@ -24386,7 +24372,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
// TODO check for noalias violations - this should be generalized to work for any function
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);
@ -25828,7 +25814,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
}
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;
return result;
}
@ -25910,7 +25896,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
} else {
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;
// Keep the bigger alignment, it can only help-