authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-26 14:24:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-26 14:54:52-04:00
logdcfd15a7f0ed5159afad47915a11d1dd8315cbe8
treef582f0697e06ae95d9962d4720ac3573a8be4be6
parent631851f8b55b78363c9bc773230d9ee1e7122f5e
signaturelock-open Commit is signed but in an unrecognized format.

the last number in a packed ptr is host int bytes

See #1121

11 files changed, 124 insertions(+), 95 deletions(-)

src/all_types.hpp+7-10
......@@ -664,7 +664,7 @@ struct AstNodePointerType {
664664 Token *star_token;
665665 AstNode *align_expr;
666666 BigInt *bit_offset_start;
667 BigInt *bit_offset_end;
667 BigInt *host_int_bytes;
668668 bool is_const;
669669 bool is_volatile;
670670 AstNode *op_expr;
......@@ -1020,8 +1020,8 @@ struct ZigTypePointer {
10201020 ZigType *slice_parent;
10211021 PtrLen ptr_len;
10221022 uint32_t explicit_alignment; // 0 means use ABI alignment
1023 uint32_t bit_offset;
1024 uint32_t unaligned_bit_count;
1023 uint32_t bit_offset_in_host;
1024 uint32_t host_int_bytes; // size of host integer. 0 means no host integer; this field is aligned
10251025 bool is_const;
10261026 bool is_volatile;
10271027};
......@@ -1045,10 +1045,7 @@ struct TypeStructField {
10451045 ZigType *type_entry;
10461046 size_t src_index;
10471047 size_t gen_index;
1048 // offset from the memory at gen_index
1049 size_t packed_bits_offset;
1050 size_t packed_bits_size;
1051 size_t unaligned_bit_count;
1048 uint32_t bit_offset_in_host; // offset from the memory at gen_index
10521049 AstNode *decl_node;
10531050};
10541051
......@@ -1470,8 +1467,8 @@ struct TypeId {
14701467 bool is_const;
14711468 bool is_volatile;
14721469 uint32_t alignment;
1473 uint32_t bit_offset;
1474 uint32_t unaligned_bit_count;
1470 uint32_t bit_offset_in_host;
1471 uint32_t host_int_bytes;
14751472 } pointer;
14761473 struct {
14771474 ZigType *child_type;
......@@ -2510,7 +2507,7 @@ struct IrInstructionPtrType {
25102507 IrInstruction *align_value;
25112508 IrInstruction *child_type;
25122509 uint32_t bit_offset_start;
2513 uint32_t bit_offset_end;
2510 uint32_t host_int_bytes;
25142511 PtrLen ptr_len;
25152512 bool is_const;
25162513 bool is_volatile;
src/analyze.cpp+39-26
......@@ -419,7 +419,7 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
419419}
420420
421421ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
422 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count)
422 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment, uint32_t bit_offset_in_host, uint32_t host_int_bytes)
423423{
424424 assert(!type_is_invalid(child_type));
425425 assert(ptr_len == PtrLenSingle || child_type->id != ZigTypeIdOpaque);
......@@ -430,23 +430,31 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
430430 byte_alignment = 0;
431431 }
432432
433 if (host_int_bytes != 0) {
434 uint32_t child_type_bits = type_size_bits(g, child_type);
435 if (host_int_bytes * 8 == child_type_bits) {
436 assert(bit_offset_in_host == 0);
437 host_int_bytes = 0;
438 }
439 }
440
433441 TypeId type_id = {};
434442 ZigType **parent_pointer = nullptr;
435 if (unaligned_bit_count != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle) {
443 if (host_int_bytes != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle) {
436444 type_id.id = ZigTypeIdPointer;
437445 type_id.data.pointer.child_type = child_type;
438446 type_id.data.pointer.is_const = is_const;
439447 type_id.data.pointer.is_volatile = is_volatile;
440448 type_id.data.pointer.alignment = byte_alignment;
441 type_id.data.pointer.bit_offset = bit_offset;
442 type_id.data.pointer.unaligned_bit_count = unaligned_bit_count;
449 type_id.data.pointer.bit_offset_in_host = bit_offset_in_host;
450 type_id.data.pointer.host_int_bytes = host_int_bytes;
443451 type_id.data.pointer.ptr_len = ptr_len;
444452
445453 auto existing_entry = g->type_table.maybe_get(type_id);
446454 if (existing_entry)
447455 return existing_entry->value;
448456 } else {
449 assert(bit_offset == 0);
457 assert(bit_offset_in_host == 0);
450458 parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
451459 if (*parent_pointer) {
452460 assert((*parent_pointer)->data.pointer.explicit_alignment == 0);
......@@ -463,17 +471,17 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
463471 const char *const_str = is_const ? "const " : "";
464472 const char *volatile_str = is_volatile ? "volatile " : "";
465473 buf_resize(&entry->name, 0);
466 if (unaligned_bit_count == 0 && byte_alignment == 0) {
474 if (host_int_bytes == 0 && byte_alignment == 0) {
467475 buf_appendf(&entry->name, "%s%s%s%s", star_str, const_str, volatile_str, buf_ptr(&child_type->name));
468 } else if (unaligned_bit_count == 0) {
476 } else if (host_int_bytes == 0) {
469477 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s", star_str, byte_alignment,
470478 const_str, volatile_str, buf_ptr(&child_type->name));
471479 } else if (byte_alignment == 0) {
472480 buf_appendf(&entry->name, "%salign(:%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str,
473 bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
481 bit_offset_in_host, host_int_bytes, const_str, volatile_str, buf_ptr(&child_type->name));
474482 } else {
475483 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str, byte_alignment,
476 bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
484 bit_offset_in_host, host_int_bytes, const_str, volatile_str, buf_ptr(&child_type->name));
477485 }
478486
479487 assert(child_type->id != ZigTypeIdInvalid);
......@@ -481,7 +489,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
481489 entry->zero_bits = !type_has_bits(child_type);
482490
483491 if (!entry->zero_bits) {
484 if (is_const || is_volatile || unaligned_bit_count != 0 || byte_alignment != 0 ||
492 if (is_const || is_volatile || host_int_bytes != 0 || byte_alignment != 0 ||
485493 ptr_len != PtrLenSingle)
486494 {
487495 ZigType *peer_type = get_pointer_to_type(g, child_type, false);
......@@ -506,8 +514,8 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
506514 entry->data.pointer.is_const = is_const;
507515 entry->data.pointer.is_volatile = is_volatile;
508516 entry->data.pointer.explicit_alignment = byte_alignment;
509 entry->data.pointer.bit_offset = bit_offset;
510 entry->data.pointer.unaligned_bit_count = unaligned_bit_count;
517 entry->data.pointer.bit_offset_in_host = bit_offset_in_host;
518 entry->data.pointer.host_int_bytes = host_int_bytes;
511519
512520 if (parent_pointer) {
513521 *parent_pointer = entry;
......@@ -2007,12 +2015,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
20072015 size_t field_size_in_bits = type_size_bits(g, field_type);
20082016 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
20092017
2010 type_struct_field->packed_bits_size = field_size_in_bits;
2011
20122018 if (first_packed_bits_offset_misalign != SIZE_MAX) {
20132019 // this field is not byte-aligned; it is part of the previous field with a bit offset
2014 type_struct_field->packed_bits_offset = packed_bits_offset - first_packed_bits_offset_misalign;
2015 type_struct_field->unaligned_bit_count = field_size_in_bits;
2020 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
20162021
20172022 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
20182023 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)(full_bit_count));
......@@ -2025,13 +2030,11 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
20252030 }
20262031 } else if (8 * LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref) != field_size_in_bits) {
20272032 first_packed_bits_offset_misalign = packed_bits_offset;
2028 type_struct_field->packed_bits_offset = 0;
2029 type_struct_field->unaligned_bit_count = field_size_in_bits;
2033 type_struct_field->bit_offset_in_host = 0;
20302034 } else {
20312035 // This is a byte-aligned field (both start and end) in a packed struct.
20322036 element_types[gen_field_index] = field_type->type_ref;
2033 type_struct_field->packed_bits_offset = 0;
2034 type_struct_field->unaligned_bit_count = 0;
2037 type_struct_field->bit_offset_in_host = 0;
20352038 gen_field_index += 1;
20362039 }
20372040 packed_bits_offset = next_packed_bits_offset;
......@@ -2124,10 +2127,10 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
21242127 uint64_t debug_align_in_bits;
21252128 uint64_t debug_offset_in_bits;
21262129 if (packed) {
2127 debug_size_in_bits = type_struct_field->packed_bits_size;
2130 debug_size_in_bits = type_size_bits(g, type_struct_field->type_entry);
21282131 debug_align_in_bits = 1;
21292132 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,
2130 (unsigned)gen_field_index) + type_struct_field->packed_bits_offset;
2133 (unsigned)gen_field_index) + type_struct_field->bit_offset_in_host;
21312134 } else {
21322135 debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);
21332136 debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_type->type_ref);
......@@ -6007,8 +6010,8 @@ uint32_t type_id_hash(TypeId x) {
60076010 (x.data.pointer.is_const ? (uint32_t)2749109194 : (uint32_t)4047371087) +
60086011 (x.data.pointer.is_volatile ? (uint32_t)536730450 : (uint32_t)1685612214) +
60096012 (((uint32_t)x.data.pointer.alignment) ^ (uint32_t)0x777fbe0e) +
6010 (((uint32_t)x.data.pointer.bit_offset) ^ (uint32_t)2639019452) +
6011 (((uint32_t)x.data.pointer.unaligned_bit_count) ^ (uint32_t)529908881);
6013 (((uint32_t)x.data.pointer.bit_offset_in_host) ^ (uint32_t)2639019452) +
6014 (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881);
60126015 case ZigTypeIdArray:
60136016 return hash_ptr(x.data.array.child_type) +
60146017 ((uint32_t)x.data.array.size ^ (uint32_t)2122979968);
......@@ -6055,8 +6058,8 @@ bool type_id_eql(TypeId a, TypeId b) {
60556058 a.data.pointer.is_const == b.data.pointer.is_const &&
60566059 a.data.pointer.is_volatile == b.data.pointer.is_volatile &&
60576060 a.data.pointer.alignment == b.data.pointer.alignment &&
6058 a.data.pointer.bit_offset == b.data.pointer.bit_offset &&
6059 a.data.pointer.unaligned_bit_count == b.data.pointer.unaligned_bit_count;
6061 a.data.pointer.bit_offset_in_host == b.data.pointer.bit_offset_in_host &&
6062 a.data.pointer.host_int_bytes == b.data.pointer.host_int_bytes;
60606063 case ZigTypeIdArray:
60616064 return a.data.array.child_type == b.data.array.child_type &&
60626065 a.data.array.size == b.data.array.size;
......@@ -6534,3 +6537,13 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
65346537 ty->id == ZigTypeIdUnreachable ||
65356538 get_codegen_ptr_type(ty) != nullptr);
65366539}
6540
6541uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
6542 assert(struct_type->id == ZigTypeIdStruct);
6543 if (struct_type->data.structure.layout != ContainerLayoutPacked) {
6544 return 0;
6545 }
6546 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
6547 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
6548}
6549
src/analyze.hpp+2
......@@ -216,4 +216,6 @@ X64CABIClass type_c_abi_x86_64_class(CodeGen *g, ZigType *ty);
216216bool type_is_c_abi_int(CodeGen *g, ZigType *ty);
217217bool want_first_arg_sret(CodeGen *g, FnTypeId *fn_type_id);
218218
219uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field);
220
219221#endif
src/ast_render.cpp+2-2
......@@ -635,7 +635,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
635635 fprintf(ar->f, "align(");
636636 render_node_grouped(ar, node->data.pointer_type.align_expr);
637637 if (node->data.pointer_type.bit_offset_start != nullptr) {
638 assert(node->data.pointer_type.bit_offset_end != nullptr);
638 assert(node->data.pointer_type.host_int_bytes != nullptr);
639639
640640 Buf offset_start_buf = BUF_INIT;
641641 buf_resize(&offset_start_buf, 0);
......@@ -643,7 +643,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
643643
644644 Buf offset_end_buf = BUF_INIT;
645645 buf_resize(&offset_end_buf, 0);
646 bigint_append_buf(&offset_end_buf, node->data.pointer_type.bit_offset_end, 10);
646 bigint_append_buf(&offset_end_buf, node->data.pointer_type.host_int_bytes, 10);
647647
648648 fprintf(ar->f, ":%s:%s ", buf_ptr(&offset_start_buf), buf_ptr(&offset_end_buf));
649649 }
src/codegen.cpp+30-21
......@@ -1795,8 +1795,8 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
17951795 return nullptr;
17961796 }
17971797
1798 uint32_t unaligned_bit_count = ptr_type->data.pointer.unaligned_bit_count;
1799 if (unaligned_bit_count == 0) {
1798 uint32_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
1799 if (host_int_bytes == 0) {
18001800 gen_store(g, value, ptr, ptr_type);
18011801 return nullptr;
18021802 }
......@@ -1804,10 +1804,12 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
18041804 bool big_endian = g->is_big_endian;
18051805
18061806 LLVMValueRef containing_int = gen_load(g, ptr, ptr_type, "");
1807
1808 uint32_t bit_offset = ptr_type->data.pointer.bit_offset;
18091807 uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int));
1810 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - unaligned_bit_count : bit_offset;
1808 assert(host_bit_count == host_int_bytes * 8);
1809 uint32_t size_in_bits = type_size_bits(g, child_type);
1810
1811 uint32_t bit_offset = ptr_type->data.pointer.bit_offset_in_host;
1812 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;
18111813 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);
18121814
18131815 LLVMValueRef mask_val = LLVMConstAllOnes(child_type->type_ref);
......@@ -3209,18 +3211,20 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
32093211 ZigType *ptr_type = instruction->ptr->value.type;
32103212 assert(ptr_type->id == ZigTypeIdPointer);
32113213
3212 uint32_t unaligned_bit_count = ptr_type->data.pointer.unaligned_bit_count;
3213 if (unaligned_bit_count == 0)
3214 uint32_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
3215 if (host_int_bytes == 0)
32143216 return get_handle_value(g, ptr, child_type, ptr_type);
32153217
32163218 bool big_endian = g->is_big_endian;
32173219
32183220 assert(!handle_is_ptr(child_type));
32193221 LLVMValueRef containing_int = gen_load(g, ptr, ptr_type, "");
3220
3221 uint32_t bit_offset = ptr_type->data.pointer.bit_offset;
32223222 uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int));
3223 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - unaligned_bit_count : bit_offset;
3223 assert(host_bit_count == host_int_bytes * 8);
3224 uint32_t size_in_bits = type_size_bits(g, child_type);
3225
3226 uint32_t bit_offset = ptr_type->data.pointer.bit_offset_in_host;
3227 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;
32243228
32253229 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);
32263230 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
......@@ -3276,20 +3280,22 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
32763280 array_type->data.array.len, false);
32773281 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
32783282 }
3279 if (array_ptr_type->data.pointer.unaligned_bit_count != 0) {
3283 if (array_ptr_type->data.pointer.host_int_bytes != 0) {
32803284 return array_ptr_ptr;
32813285 }
32823286 ZigType *child_type = array_type->data.array.child_type;
32833287 if (child_type->id == ZigTypeIdStruct &&
32843288 child_type->data.structure.layout == ContainerLayoutPacked)
32853289 {
3286 size_t unaligned_bit_count = instruction->base.value.type->data.pointer.unaligned_bit_count;
3287 if (unaligned_bit_count != 0) {
3290 ZigType *ptr_type = instruction->base.value.type;
3291 size_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
3292 if (host_int_bytes != 0) {
3293 uint32_t size_in_bits = type_size_bits(g, ptr_type->data.pointer.child_type);
32883294 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
32893295 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
3290 assert(unaligned_bit_count % 8 == 0);
3296 assert(size_in_bits % 8 == 0);
32913297 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3292 unaligned_bit_count / 8, false);
3298 size_in_bits / 8, false);
32933299 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
32943300 LLVMValueRef indices[] = {
32953301 byte_offset
......@@ -3505,7 +3511,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
35053511 return nullptr;
35063512
35073513 if (struct_ptr_type->id == ZigTypeIdPointer &&
3508 struct_ptr_type->data.pointer.unaligned_bit_count != 0)
3514 struct_ptr_type->data.pointer.host_int_bytes != 0)
35093515 {
35103516 return struct_ptr;
35113517 }
......@@ -4671,10 +4677,11 @@ static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable,
46714677 LLVMValueRef value = ir_llvm_value(g, field->value);
46724678
46734679 uint32_t field_align_bytes = get_abi_alignment(g, type_struct_field->type_entry);
4680 uint32_t host_int_bytes = get_host_int_bytes(g, instruction->struct_type, type_struct_field);
46744681
46754682 ZigType *ptr_type = get_pointer_to_type_extra(g, type_struct_field->type_entry,
46764683 false, false, PtrLenSingle, field_align_bytes,
4677 (uint32_t)type_struct_field->packed_bits_offset, (uint32_t)type_struct_field->unaligned_bit_count);
4684 (uint32_t)type_struct_field->bit_offset_in_host, host_int_bytes);
46784685
46794686 gen_assign_raw(g, field_ptr, ptr_type, value);
46804687 }
......@@ -5459,15 +5466,16 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
54595466 continue;
54605467 }
54615468 LLVMValueRef child_val = pack_const_int(g, big_int_type_ref, &const_val->data.x_struct.fields[i]);
5469 uint32_t packed_bits_size = type_size_bits(g, field->type_entry);
54625470 if (is_big_endian) {
5463 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, field->packed_bits_size, false);
5471 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, packed_bits_size, false);
54645472 val = LLVMConstShl(val, shift_amt);
54655473 val = LLVMConstOr(val, child_val);
54665474 } else {
54675475 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, used_bits, false);
54685476 LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt);
54695477 val = LLVMConstOr(val, child_val_shifted);
5470 used_bits += field->packed_bits_size;
5478 used_bits += packed_bits_size;
54715479 }
54725480 }
54735481 return val;
......@@ -5677,16 +5685,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
56775685 }
56785686 LLVMValueRef child_val = pack_const_int(g, big_int_type_ref,
56795687 &const_val->data.x_struct.fields[i]);
5688 uint32_t packed_bits_size = type_size_bits(g, it_field->type_entry);
56805689 if (is_big_endian) {
56815690 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref,
5682 it_field->packed_bits_size, false);
5691 packed_bits_size, false);
56835692 val = LLVMConstShl(val, shift_amt);
56845693 val = LLVMConstOr(val, child_val);
56855694 } else {
56865695 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, used_bits, false);
56875696 LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt);
56885697 val = LLVMConstOr(val, child_val_shifted);
5689 used_bits += it_field->packed_bits_size;
5698 used_bits += packed_bits_size;
56905699 }
56915700 }
56925701 fields[type_struct_field->gen_index] = val;
src/ir.cpp+31-31
......@@ -1296,7 +1296,7 @@ static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instru
12961296
12971297static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
12981298 IrInstruction *child_type, bool is_const, bool is_volatile, PtrLen ptr_len,
1299 IrInstruction *align_value, uint32_t bit_offset_start, uint32_t bit_offset_end)
1299 IrInstruction *align_value, uint32_t bit_offset_start, uint32_t host_int_bytes)
13001300{
13011301 IrInstructionPtrType *ptr_type_of_instruction = ir_build_instruction<IrInstructionPtrType>(irb, scope, source_node);
13021302 ptr_type_of_instruction->align_value = align_value;
......@@ -1305,7 +1305,7 @@ static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *s
13051305 ptr_type_of_instruction->is_volatile = is_volatile;
13061306 ptr_type_of_instruction->ptr_len = ptr_len;
13071307 ptr_type_of_instruction->bit_offset_start = bit_offset_start;
1308 ptr_type_of_instruction->bit_offset_end = bit_offset_end;
1308 ptr_type_of_instruction->host_int_bytes = host_int_bytes;
13091309
13101310 if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
13111311 ir_ref_instruction(child_type, irb->current_basic_block);
......@@ -5154,26 +5154,26 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
51545154 bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start);
51555155 }
51565156
5157 uint32_t bit_offset_end = 0;
5158 if (node->data.pointer_type.bit_offset_end != nullptr) {
5159 if (!bigint_fits_in_bits(node->data.pointer_type.bit_offset_end, 32, false)) {
5157 uint32_t host_int_bytes = 0;
5158 if (node->data.pointer_type.host_int_bytes != nullptr) {
5159 if (!bigint_fits_in_bits(node->data.pointer_type.host_int_bytes, 32, false)) {
51605160 Buf *val_buf = buf_alloc();
5161 bigint_append_buf(val_buf, node->data.pointer_type.bit_offset_end, 10);
5161 bigint_append_buf(val_buf, node->data.pointer_type.host_int_bytes, 10);
51625162 exec_add_error_node(irb->codegen, irb->exec, node,
5163 buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf)));
5163 buf_sprintf("value %s too large for u32 byte count", buf_ptr(val_buf)));
51645164 return irb->codegen->invalid_instruction;
51655165 }
5166 bit_offset_end = bigint_as_unsigned(node->data.pointer_type.bit_offset_end);
5166 host_int_bytes = bigint_as_unsigned(node->data.pointer_type.host_int_bytes);
51675167 }
51685168
5169 if ((bit_offset_start != 0 || bit_offset_end != 0) && bit_offset_start >= bit_offset_end) {
5169 if (host_int_bytes != 0 && bit_offset_start >= host_int_bytes * 8) {
51705170 exec_add_error_node(irb->codegen, irb->exec, node,
5171 buf_sprintf("bit offset start must be less than bit offset end"));
5171 buf_sprintf("bit offset starts after end of host integer"));
51725172 return irb->codegen->invalid_instruction;
51735173 }
51745174
51755175 return ir_build_ptr_type(irb, scope, node, child_type, is_const, is_volatile,
5176 ptr_len, align_value, bit_offset_start, bit_offset_end);
5176 ptr_len, align_value, bit_offset_start, host_int_bytes);
51775177}
51785178
51795179static IrInstruction *ir_gen_err_assert_ok(IrBuilder *irb, Scope *scope, AstNode *source_node, AstNode *expr_node,
......@@ -8600,8 +8600,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
86008600 if ((actual_type->data.pointer.ptr_len == wanted_type->data.pointer.ptr_len) &&
86018601 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
86028602 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile) &&
8603 actual_type->data.pointer.bit_offset == wanted_type->data.pointer.bit_offset &&
8604 actual_type->data.pointer.unaligned_bit_count == wanted_type->data.pointer.unaligned_bit_count &&
8603 actual_type->data.pointer.bit_offset_in_host == wanted_type->data.pointer.bit_offset_in_host &&
8604 actual_type->data.pointer.host_int_bytes == wanted_type->data.pointer.host_int_bytes &&
86058605 get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, wanted_type))
86068606 {
86078607 return result;
......@@ -8622,8 +8622,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
86228622 }
86238623 if ((!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&
86248624 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile) &&
8625 actual_ptr_type->data.pointer.bit_offset == wanted_ptr_type->data.pointer.bit_offset &&
8626 actual_ptr_type->data.pointer.unaligned_bit_count == wanted_ptr_type->data.pointer.unaligned_bit_count &&
8625 actual_ptr_type->data.pointer.bit_offset_in_host == wanted_ptr_type->data.pointer.bit_offset_in_host &&
8626 actual_ptr_type->data.pointer.host_int_bytes == wanted_ptr_type->data.pointer.host_int_bytes &&
86278627 get_ptr_align(g, actual_ptr_type) >= get_ptr_align(g, wanted_ptr_type))
86288628 {
86298629 ConstCastOnly child = types_match_const_cast_only(ira, wanted_ptr_type->data.pointer.child_type,
......@@ -11166,8 +11166,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1116611166 if (dest_ptr_type != nullptr &&
1116711167 (!actual_type->data.pointer.is_const || dest_ptr_type->data.pointer.is_const) &&
1116811168 (!actual_type->data.pointer.is_volatile || dest_ptr_type->data.pointer.is_volatile) &&
11169 actual_type->data.pointer.bit_offset == dest_ptr_type->data.pointer.bit_offset &&
11170 actual_type->data.pointer.unaligned_bit_count == dest_ptr_type->data.pointer.unaligned_bit_count &&
11169 actual_type->data.pointer.bit_offset_in_host == dest_ptr_type->data.pointer.bit_offset_in_host &&
11170 actual_type->data.pointer.host_int_bytes == dest_ptr_type->data.pointer.host_int_bytes &&
1117111171 get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, dest_ptr_type))
1117211172 {
1117311173 return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr);
......@@ -14359,7 +14359,7 @@ static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_ali
1435914359 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1436014360 ptr_type->data.pointer.ptr_len,
1436114361 new_align,
14362 ptr_type->data.pointer.bit_offset, ptr_type->data.pointer.unaligned_bit_count);
14362 ptr_type->data.pointer.bit_offset_in_host, ptr_type->data.pointer.host_int_bytes);
1436314363}
1436414364
1436514365static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align) {
......@@ -14376,7 +14376,7 @@ static ZigType *adjust_ptr_len(CodeGen *g, ZigType *ptr_type, PtrLen ptr_len) {
1437614376 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1437714377 ptr_len,
1437814378 ptr_type->data.pointer.explicit_alignment,
14379 ptr_type->data.pointer.bit_offset, ptr_type->data.pointer.unaligned_bit_count);
14379 ptr_type->data.pointer.bit_offset_in_host, ptr_type->data.pointer.host_int_bytes);
1438014380}
1438114381
1438214382static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {
......@@ -14423,7 +14423,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1442314423 return ira->codegen->builtin_types.entry_invalid;
1442414424 }
1442514425 ZigType *child_type = array_type->data.array.child_type;
14426 if (ptr_type->data.pointer.unaligned_bit_count == 0) {
14426 if (ptr_type->data.pointer.host_int_bytes == 0) {
1442714427 return_type = get_pointer_to_type_extra(ira->codegen, child_type,
1442814428 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1442914429 elem_ptr_instruction->ptr_len,
......@@ -14439,7 +14439,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1443914439 return_type = get_pointer_to_type_extra(ira->codegen, child_type,
1444014440 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1444114441 elem_ptr_instruction->ptr_len,
14442 1, (uint32_t)bit_offset, (uint32_t)bit_width);
14442 1, (uint32_t)bit_offset, ptr_type->data.pointer.host_int_bytes);
1444314443 }
1444414444 } else if (array_type->id == ZigTypeIdPointer) {
1444514445 if (array_type->data.pointer.ptr_len == PtrLenSingle) {
......@@ -14740,10 +14740,10 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1474014740 if (field) {
1474114741 bool is_packed = (bare_type->data.structure.layout == ContainerLayoutPacked);
1474214742 uint32_t align_bytes = is_packed ? 1 : get_abi_alignment(ira->codegen, field->type_entry);
14743 size_t ptr_bit_offset = container_ptr->value.type->data.pointer.bit_offset;
14744 size_t ptr_unaligned_bit_count = container_ptr->value.type->data.pointer.unaligned_bit_count;
14745 size_t unaligned_bit_count_for_result_type = (ptr_unaligned_bit_count == 0) ?
14746 field->unaligned_bit_count : type_size_bits(ira->codegen, field->type_entry);
14743 uint32_t ptr_bit_offset = container_ptr->value.type->data.pointer.bit_offset_in_host;
14744 uint32_t ptr_host_int_bytes = container_ptr->value.type->data.pointer.host_int_bytes;
14745 uint32_t host_int_bytes_for_result_type = (ptr_host_int_bytes == 0) ?
14746 get_host_int_bytes(ira->codegen, bare_type, field) : ptr_host_int_bytes;
1474714747 if (instr_is_comptime(container_ptr)) {
1474814748 ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
1474914749 if (!ptr_val)
......@@ -14758,8 +14758,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1475814758 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
1475914759 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type,
1476014760 is_const, is_volatile, PtrLenSingle, align_bytes,
14761 (uint32_t)(ptr_bit_offset + field->packed_bits_offset),
14762 (uint32_t)unaligned_bit_count_for_result_type);
14761 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
14762 (uint32_t)host_int_bytes_for_result_type);
1476314763 IrInstruction *result = ir_get_const(ira, source_instr);
1476414764 ConstExprValue *const_val = &result->value;
1476514765 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
......@@ -14775,8 +14775,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1477514775 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
1477614776 PtrLenSingle,
1477714777 align_bytes,
14778 (uint32_t)(ptr_bit_offset + field->packed_bits_offset),
14779 (uint32_t)unaligned_bit_count_for_result_type);
14778 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
14779 host_int_bytes_for_result_type);
1478014780 return result;
1478114781 } else {
1478214782 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
......@@ -17135,7 +17135,7 @@ static ZigType *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
1713517135 if (!(field = validate_byte_offset(ira, type_value, field_name_value, &byte_offset)))
1713617136 return ira->codegen->builtin_types.entry_invalid;
1713717137
17138 size_t bit_offset = byte_offset * 8 + field->packed_bits_offset;
17138 size_t bit_offset = byte_offset * 8 + field->bit_offset_in_host;
1713917139 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1714017140 bigint_init_unsigned(&out_val->data.x_bigint, bit_offset);
1714117141 return ira->codegen->builtin_types.entry_num_lit_int;
......@@ -20758,7 +20758,7 @@ static ZigType *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtr
2075820758 out_val->data.x_type = get_pointer_to_type_extra(ira->codegen, child_type,
2075920759 instruction->is_const, instruction->is_volatile,
2076020760 instruction->ptr_len, align_bytes,
20761 instruction->bit_offset_start, instruction->bit_offset_end - instruction->bit_offset_start);
20761 instruction->bit_offset_start, instruction->host_int_bytes);
2076220762
2076320763 return ira->codegen->builtin_types.entry_type;
2076420764}
src/ir_print.cpp+1-1
......@@ -1015,7 +1015,7 @@ static void ir_print_ptr_type(IrPrint *irp, IrInstructionPtrType *instruction) {
10151015 }
10161016 const char *const_str = instruction->is_const ? "const " : "";
10171017 const char *volatile_str = instruction->is_volatile ? "volatile " : "";
1018 fprintf(irp->f, ":%" PRIu32 ":%" PRIu32 " %s%s", instruction->bit_offset_start, instruction->bit_offset_end,
1018 fprintf(irp->f, ":%" PRIu32 ":%" PRIu32 " %s%s", instruction->bit_offset_start, instruction->host_int_bytes,
10191019 const_str, volatile_str);
10201020 ir_print_other_instruction(irp, instruction->child_type);
10211021}
src/parser.cpp+2-2
......@@ -1161,10 +1161,10 @@ static AstNode *ast_parse_pointer_type(ParseContext *pc, size_t *token_index, To
11611161 *token_index += 1;
11621162 Token *bit_offset_start_tok = ast_eat_token(pc, token_index, TokenIdIntLiteral);
11631163 ast_eat_token(pc, token_index, TokenIdColon);
1164 Token *bit_offset_end_tok = ast_eat_token(pc, token_index, TokenIdIntLiteral);
1164 Token *host_int_bytes_tok = ast_eat_token(pc, token_index, TokenIdIntLiteral);
11651165
11661166 node->data.pointer_type.bit_offset_start = token_bigint(bit_offset_start_tok);
1167 node->data.pointer_type.bit_offset_end = token_bigint(bit_offset_end_tok);
1167 node->data.pointer_type.host_int_bytes = token_bigint(host_int_bytes_tok);
11681168 }
11691169 ast_eat_token(pc, token_index, TokenIdRParen);
11701170 token = &pc->tokens->at(*token_index);
test/cases/align.zig+1-1
......@@ -40,7 +40,7 @@ const blah: packed struct {
4040} = undefined;
4141
4242test "bit field alignment" {
43 assert(@typeOf(&blah.b) == *align(1:3:6) const u3);
43 assert(@typeOf(&blah.b) == *align(1:3:1) const u3);
4444}
4545
4646test "default alignment allows unspecified in type syntax" {
test/cases/eval.zig+8
......@@ -754,3 +754,11 @@ test "comptime bitwise operators" {
754754 assert(~u128(0) == 0xffffffffffffffffffffffffffffffff);
755755 }
756756}
757
758test "*align(1) u16 is the same as *align(1:0:2) u16" {
759 comptime {
760 assert(*align(1:0:2) u16 == *align(1) u16);
761 // TODO add parsing support for this syntax
762 //assert(*align(:0:2) u16 == *u16);
763 }
764}
test/compile_errors.zig+1-1
......@@ -3536,7 +3536,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35363536 \\
35373537 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
35383538 ,
3539 ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(:3:6) const u3'",
3539 ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(:3:1) const u3'",
35403540 );
35413541
35423542 cases.add(