authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2020-04-01 20:42:43+02:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2020-04-01 20:50:13+02:00
logd33766e6c7289b79256b2e50d0dc2344729ff710
tree24da3981755618f82cfec5da88ebe5df791a708b
parentae6965a4e73cd5aad04e1c6831f48e7f0ecafc04

Make sure that ZigTypeVector and ZigTypeArray have the same memory layout

Throughout the stage1 code it is assumed that these have the same layout, but that was not the case. This caused an issue on 32-bit hardware.

4 files changed, 10 insertions(+), 3 deletions(-)

src/all_types.hpp+7-1
......@@ -1324,6 +1324,7 @@ struct ZigTypeFloat {
13241324 size_t bit_count;
13251325};
13261326
1327// Needs to have the same memory layout as ZigTypeVector
13271328struct ZigTypeArray {
13281329 ZigType *child_type;
13291330 uint64_t len;
......@@ -1512,12 +1513,17 @@ struct ZigTypeBoundFn {
15121513 ZigType *fn_type;
15131514};
15141515
1516// Needs to have the same memory layout as ZigTypeArray
15151517struct ZigTypeVector {
15161518 // The type must be a pointer, integer, bool, or float
15171519 ZigType *elem_type;
1518 uint32_t len;
1520 uint64_t len;
1521 size_t padding;
15191522};
15201523
1524// A lot of code is relying on ZigTypeArray and ZigTypeVector having the same layout/size
1525static_assert(sizeof(ZigTypeVector) == sizeof(ZigTypeArray), "Size of ZigTypeVector and ZigTypeArray do not match!");
1526
15211527enum ZigTypeId {
15221528 ZigTypeIdInvalid,
15231529 ZigTypeIdMetaType,
src/analyze.cpp+1
......@@ -5156,6 +5156,7 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {
51565156 }
51575157 entry->data.vector.len = len;
51585158 entry->data.vector.elem_type = elem_type;
5159 entry->data.vector.padding = 0;
51595160
51605161 buf_resize(&entry->name, 0);
51615162 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));
src/codegen.cpp+1-1
......@@ -714,7 +714,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
714714 };
715715
716716 if (operand_type->id == ZigTypeIdVector) {
717 sprintf(fn_name, "llvm.%s.with.overflow.v%" PRIu32 "i%" PRIu32, signed_str,
717 sprintf(fn_name, "llvm.%s.with.overflow.v%" PRIu64 "i%" PRIu32, signed_str,
718718 operand_type->data.vector.len, int_type->data.integral.bit_count);
719719
720720 LLVMTypeRef return_elem_types[] = {
src/ir.cpp+1-1
......@@ -15953,7 +15953,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1595315953 if (op1->value->type->id == ZigTypeIdVector && op2->value->type->id == ZigTypeIdVector) {
1595415954 if (op1->value->type->data.vector.len != op2->value->type->data.vector.len) {
1595515955 ir_add_error(ira, source_instr,
15956 buf_sprintf("vector length mismatch: %" PRIu32 " and %" PRIu32,
15956 buf_sprintf("vector length mismatch: %" PRIu64 " and %" PRIu64,
1595715957 op1->value->type->data.vector.len, op2->value->type->data.vector.len));
1595815958 return ira->codegen->invalid_inst_gen;
1595915959 }