| ... | ... | @@ -19,14 +19,15 @@ |
| 19 | 19 | |
| 20 | 20 | static const size_t default_backward_branch_quota = 1000; |
| 21 | 21 | |
| 22 | | static Error resolve_enum_type(CodeGen *g, ZigType *enum_type); |
| 23 | 22 | static Error resolve_struct_type(CodeGen *g, ZigType *struct_type); |
| 24 | 23 | |
| 25 | 24 | static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type); |
| 26 | 25 | static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type); |
| 27 | 26 | static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type); |
| 28 | 27 | static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type); |
| 28 | static Error ATTRIBUTE_MUST_USE resolve_union_alignment(CodeGen *g, ZigType *union_type); |
| 29 | 29 | static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry); |
| 30 | static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status); |
| 30 | 31 | |
| 31 | 32 | static bool is_top_level_struct(ZigType *import) { |
| 32 | 33 | return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr; |
| ... | ... | @@ -264,6 +265,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) { |
| 264 | 265 | zig_unreachable(); |
| 265 | 266 | case ZigTypeIdStruct: |
| 266 | 267 | return type_entry->data.structure.resolve_status >= status; |
| 268 | case ZigTypeIdUnion: |
| 269 | return type_entry->data.unionation.resolve_status >= status; |
| 267 | 270 | case ZigTypeIdEnum: |
| 268 | 271 | switch (status) { |
| 269 | 272 | case ResolveStatusUnstarted: |
| ... | ... | @@ -276,20 +279,9 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) { |
| 276 | 279 | return type_entry->data.enumeration.zero_bits_known; |
| 277 | 280 | case ResolveStatusSizeKnown: |
| 278 | 281 | return type_entry->data.enumeration.complete; |
| 279 | | } |
| 280 | | zig_unreachable(); |
| 281 | | case ZigTypeIdUnion: |
| 282 | | switch (status) { |
| 283 | | case ResolveStatusUnstarted: |
| 284 | | return true; |
| 285 | | case ResolveStatusInvalid: |
| 286 | | zig_unreachable(); |
| 287 | | case ResolveStatusZeroBitsKnown: |
| 288 | | return type_entry->data.unionation.zero_bits_known; |
| 289 | | case ResolveStatusAlignmentKnown: |
| 290 | | return type_entry->data.unionation.zero_bits_known; |
| 291 | | case ResolveStatusSizeKnown: |
| 292 | | return type_entry->data.unionation.complete; |
| 282 | case ResolveStatusLLVMFwdDecl: |
| 283 | case ResolveStatusLLVMFull: |
| 284 | return type_entry->llvm_di_type != nullptr; |
| 293 | 285 | } |
| 294 | 286 | zig_unreachable(); |
| 295 | 287 | case ZigTypeIdOpaque: |
| ... | ... | @@ -325,49 +317,18 @@ bool type_is_complete(ZigType *type_entry) { |
| 325 | 317 | } |
| 326 | 318 | |
| 327 | 319 | uint64_t type_size(CodeGen *g, ZigType *type_entry) { |
| 328 | | assert(type_is_complete(type_entry)); |
| 329 | | |
| 330 | | if (!type_has_bits(type_entry)) |
| 331 | | return 0; |
| 332 | | |
| 333 | | if (type_entry->id == ZigTypeIdStruct && type_entry->data.structure.layout == ContainerLayoutPacked) { |
| 334 | | uint64_t size_in_bits = type_size_bits(g, type_entry); |
| 335 | | return (size_in_bits + 7) / 8; |
| 336 | | } else if (type_entry->id == ZigTypeIdArray) { |
| 337 | | ZigType *child_type = type_entry->data.array.child_type; |
| 338 | | if (child_type->id == ZigTypeIdStruct && |
| 339 | | child_type->data.structure.layout == ContainerLayoutPacked) |
| 340 | | { |
| 341 | | uint64_t size_in_bits = type_size_bits(g, type_entry); |
| 342 | | return (size_in_bits + 7) / 8; |
| 343 | | } |
| 344 | | } |
| 345 | | |
| 346 | | return LLVMABISizeOfType(g->target_data_ref, type_entry->type_ref); |
| 320 | assert(type_is_resolved(type_entry, ResolveStatusSizeKnown)); |
| 321 | return type_entry->abi_size; |
| 347 | 322 | } |
| 348 | 323 | |
| 349 | 324 | uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) { |
| 350 | | assert(type_is_complete(type_entry)); |
| 351 | | |
| 352 | | if (!type_has_bits(type_entry)) |
| 353 | | return 0; |
| 354 | | |
| 355 | | if (type_entry->id == ZigTypeIdStruct) { |
| 356 | | if (type_entry->data.structure.layout == ContainerLayoutPacked) { |
| 357 | | uint64_t result = 0; |
| 358 | | for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) { |
| 359 | | result += type_size_bits(g, type_entry->data.structure.fields[i].type_entry); |
| 360 | | } |
| 361 | | return result; |
| 362 | | } else if (type_entry->data.structure.layout == ContainerLayoutExtern) { |
| 363 | | return type_size(g, type_entry) * 8; |
| 364 | | } |
| 365 | | } else if (type_entry->id == ZigTypeIdArray) { |
| 366 | | ZigType *child_type = type_entry->data.array.child_type; |
| 367 | | return type_entry->data.array.len * type_size_bits(g, child_type); |
| 368 | | } |
| 325 | assert(type_is_resolved(type_entry, ResolveStatusSizeKnown)); |
| 326 | return type_entry->size_in_bits; |
| 327 | } |
| 369 | 328 | |
| 370 | | return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref); |
| 329 | uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) { |
| 330 | assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown)); |
| 331 | return type_entry->abi_align; |
| 371 | 332 | } |
| 372 | 333 | |
| 373 | 334 | static bool is_slice(ZigType *type) { |
| ... | ... | @@ -385,16 +346,15 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) { |
| 385 | 346 | return g->builtin_types.entry_promise; |
| 386 | 347 | } |
| 387 | 348 | |
| 388 | | ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); |
| 389 | 349 | ZigType *entry = new_type_table_entry(ZigTypeIdPromise); |
| 390 | | entry->type_ref = u8_ptr_type->type_ref; |
| 391 | | entry->zero_bits = false; |
| 350 | entry->abi_size = g->builtin_types.entry_usize->abi_size; |
| 351 | entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits; |
| 352 | entry->abi_align = g->builtin_types.entry_usize->abi_align; |
| 392 | 353 | entry->data.promise.result_type = result_type; |
| 393 | 354 | buf_init_from_str(&entry->name, "promise"); |
| 394 | 355 | if (result_type != nullptr) { |
| 395 | 356 | buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name)); |
| 396 | 357 | } |
| 397 | | entry->di_type = u8_ptr_type->di_type; |
| 398 | 358 | |
| 399 | 359 | if (result_type != nullptr) { |
| 400 | 360 | result_type->promise_parent = entry; |
| ... | ... | @@ -496,36 +456,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons |
| 496 | 456 | |
| 497 | 457 | assert(child_type->id != ZigTypeIdInvalid); |
| 498 | 458 | |
| 499 | | entry->zero_bits = !type_has_bits(child_type); |
| 500 | | |
| 501 | | if (!entry->zero_bits) { |
| 502 | | if (is_const || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle || |
| 503 | | bit_offset_in_host != 0 || allow_zero) |
| 504 | | { |
| 505 | | ZigType *peer_type = get_pointer_to_type_extra(g, child_type, false, false, |
| 506 | | PtrLenSingle, 0, 0, host_int_bytes, false); |
| 507 | | entry->type_ref = peer_type->type_ref; |
| 508 | | entry->di_type = peer_type->di_type; |
| 509 | | } else { |
| 510 | | if (host_int_bytes == 0) { |
| 511 | | entry->type_ref = LLVMPointerType(child_type->type_ref, 0); |
| 512 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 513 | | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref); |
| 514 | | assert(child_type->di_type); |
| 515 | | entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, child_type->di_type, |
| 516 | | debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name)); |
| 517 | | } else { |
| 518 | | ZigType *host_int_type = get_int_type(g, false, host_int_bytes * 8); |
| 519 | | entry->type_ref = LLVMPointerType(host_int_type->type_ref, 0); |
| 520 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_type->type_ref); |
| 521 | | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_type->type_ref); |
| 522 | | entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, host_int_type->di_type, |
| 523 | | debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name)); |
| 524 | | } |
| 525 | | } |
| 459 | if (type_has_bits(child_type)) { |
| 460 | entry->abi_size = g->builtin_types.entry_usize->abi_size; |
| 461 | entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits; |
| 462 | entry->abi_align = g->builtin_types.entry_usize->abi_align; |
| 526 | 463 | } else { |
| 527 | 464 | assert(byte_alignment == 0); |
| 528 | | entry->di_type = g->builtin_types.entry_void->di_type; |
| 465 | entry->abi_size = 0; |
| 466 | entry->size_in_bits = 0; |
| 467 | entry->abi_align = 0; |
| 529 | 468 | } |
| 530 | 469 | |
| 531 | 470 | entry->data.pointer.ptr_len = ptr_len; |
| ... | ... | @@ -586,89 +525,60 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) { |
| 586 | 525 | } |
| 587 | 526 | |
| 588 | 527 | ZigType *get_optional_type(CodeGen *g, ZigType *child_type) { |
| 589 | | if (child_type->optional_parent) { |
| 590 | | ZigType *entry = child_type->optional_parent; |
| 591 | | return entry; |
| 592 | | } else { |
| 593 | | assertNoError(ensure_complete_type(g, child_type)); |
| 594 | | |
| 595 | | ZigType *entry = new_type_table_entry(ZigTypeIdOptional); |
| 596 | | assert(child_type->type_ref || child_type->zero_bits); |
| 597 | | |
| 598 | | buf_resize(&entry->name, 0); |
| 599 | | buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name)); |
| 600 | | |
| 601 | | if (child_type->zero_bits) { |
| 602 | | entry->type_ref = LLVMInt1Type(); |
| 603 | | entry->di_type = g->builtin_types.entry_bool->di_type; |
| 604 | | } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) { |
| 605 | | assert(child_type->di_type); |
| 606 | | // this is an optimization but also is necessary for calling C |
| 607 | | // functions where all pointers are maybe pointers |
| 608 | | // function types are technically pointers |
| 609 | | entry->type_ref = child_type->type_ref; |
| 610 | | entry->di_type = child_type->di_type; |
| 611 | | if (entry->di_type == g->builtin_types.entry_global_error_set->di_type) { |
| 612 | | g->error_di_types.append(&entry->di_type); |
| 613 | | } |
| 614 | | } else { |
| 615 | | assert(child_type->di_type); |
| 616 | | // create a struct with a boolean whether this is the null value |
| 617 | | LLVMTypeRef elem_types[] = { |
| 618 | | child_type->type_ref, |
| 619 | | LLVMInt1Type(), |
| 620 | | }; |
| 621 | | entry->type_ref = LLVMStructType(elem_types, 2, false); |
| 622 | | |
| 623 | | |
| 624 | | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 625 | | ZigLLVMDIFile *di_file = nullptr; |
| 626 | | unsigned line = 0; |
| 627 | | entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 628 | | ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name), |
| 629 | | compile_unit_scope, di_file, line); |
| 630 | | |
| 631 | | uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref); |
| 632 | | uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_type->type_ref); |
| 633 | | uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0); |
| 634 | | |
| 635 | | ZigType *bool_type = g->builtin_types.entry_bool; |
| 636 | | uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_type->type_ref); |
| 637 | | uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_type->type_ref); |
| 638 | | uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1); |
| 639 | | |
| 640 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 641 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref); |
| 642 | | |
| 643 | | ZigLLVMDIType *di_element_types[] = { |
| 644 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 645 | | "val", di_file, line, |
| 646 | | val_debug_size_in_bits, |
| 647 | | val_debug_align_in_bits, |
| 648 | | val_offset_in_bits, |
| 649 | | 0, child_type->di_type), |
| 650 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 651 | | "maybe", di_file, line, |
| 652 | | maybe_debug_size_in_bits, |
| 653 | | maybe_debug_align_in_bits, |
| 654 | | maybe_offset_in_bits, |
| 655 | | 0, bool_type->di_type), |
| 656 | | }; |
| 657 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 658 | | compile_unit_scope, |
| 659 | | buf_ptr(&entry->name), |
| 660 | | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 661 | | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 528 | if (child_type->optional_parent != nullptr) { |
| 529 | return child_type->optional_parent; |
| 530 | } |
| 662 | 531 | |
| 663 | | ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type); |
| 664 | | entry->di_type = replacement_di_type; |
| 665 | | } |
| 532 | assert(type_is_resolved(child_type, ResolveStatusSizeKnown)); |
| 666 | 533 | |
| 667 | | entry->data.maybe.child_type = child_type; |
| 534 | ZigType *entry = new_type_table_entry(ZigTypeIdOptional); |
| 668 | 535 | |
| 669 | | child_type->optional_parent = entry; |
| 670 | | return entry; |
| 536 | buf_resize(&entry->name, 0); |
| 537 | buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name)); |
| 538 | |
| 539 | if (!type_has_bits(child_type)) { |
| 540 | entry->size_in_bits = g->builtin_types.entry_bool->size_in_bits; |
| 541 | entry->abi_size = g->builtin_types.entry_bool->abi_size; |
| 542 | entry->abi_align = g->builtin_types.entry_bool->abi_align; |
| 543 | } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) { |
| 544 | // This is an optimization but also is necessary for calling C |
| 545 | // functions where all pointers are optional pointers. |
| 546 | // Function types are technically pointers. |
| 547 | entry->size_in_bits = child_type->size_in_bits; |
| 548 | entry->abi_size = child_type->abi_size; |
| 549 | entry->abi_align = child_type->abi_align; |
| 550 | } else { |
| 551 | // This value only matters if the type is legal in a packed struct, which is not |
| 552 | // true for optional types which did not fit the above 2 categories (zero bit child type, |
| 553 | // or nonnull ptr child type, or error set child type). |
| 554 | entry->size_in_bits = child_type->size_in_bits + 1; |
| 555 | |
| 556 | // We're going to make a struct with the child type as the first field, |
| 557 | // and a bool as the second. Since the child type's abi alignment is guaranteed |
| 558 | // to be >= the bool's abi size (1 byte), the added size is exactly equal to the |
| 559 | // child type's ABI alignment. |
| 560 | assert(child_type->abi_align >= g->builtin_types.entry_bool->abi_size); |
| 561 | entry->abi_align = child_type->abi_align; |
| 562 | entry->abi_size = child_type->abi_size + child_type->abi_align; |
| 671 | 563 | } |
| 564 | |
| 565 | entry->data.maybe.child_type = child_type; |
| 566 | |
| 567 | child_type->optional_parent = entry; |
| 568 | return entry; |
| 569 | } |
| 570 | |
| 571 | static size_t align_forward(size_t addr, size_t alignment) { |
| 572 | return (addr + alignment - 1) & ~(alignment - 1); |
| 573 | } |
| 574 | |
| 575 | static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t next_field_align) { |
| 576 | // Convert offset to a pretend address which has the specified alignment. |
| 577 | size_t addr = offset + align_from_zero; |
| 578 | // March the address forward to respect the field alignment. |
| 579 | size_t aligned_addr = align_forward(addr + field_size, next_field_align); |
| 580 | // Convert back from pretend address to offset. |
| 581 | return aligned_addr - align_from_zero; |
| 672 | 582 | } |
| 673 | 583 | |
| 674 | 584 | ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) { |
| ... | ... | @@ -686,8 +596,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa |
| 686 | 596 | } |
| 687 | 597 | |
| 688 | 598 | ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion); |
| 689 | | assert(payload_type->di_type); |
| 690 | | assert(type_is_complete(payload_type)); |
| 599 | assert(type_is_resolved(payload_type, ResolveStatusSizeKnown)); |
| 691 | 600 | |
| 692 | 601 | buf_resize(&entry->name, 0); |
| 693 | 602 | buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name)); |
| ... | ... | @@ -697,68 +606,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa |
| 697 | 606 | |
| 698 | 607 | if (!type_has_bits(payload_type)) { |
| 699 | 608 | if (type_has_bits(err_set_type)) { |
| 700 | | entry->type_ref = err_set_type->type_ref; |
| 701 | | entry->di_type = err_set_type->di_type; |
| 702 | | g->error_di_types.append(&entry->di_type); |
| 609 | entry->size_in_bits = err_set_type->size_in_bits; |
| 610 | entry->abi_size = err_set_type->abi_size; |
| 611 | entry->abi_align = err_set_type->abi_align; |
| 703 | 612 | } else { |
| 704 | | entry->zero_bits = true; |
| 705 | | entry->di_type = g->builtin_types.entry_void->di_type; |
| 613 | entry->size_in_bits = 0; |
| 614 | entry->abi_size = 0; |
| 615 | entry->abi_align = 0; |
| 706 | 616 | } |
| 707 | 617 | } else if (!type_has_bits(err_set_type)) { |
| 708 | | entry->type_ref = payload_type->type_ref; |
| 709 | | entry->di_type = payload_type->di_type; |
| 618 | entry->size_in_bits = payload_type->size_in_bits; |
| 619 | entry->abi_size = payload_type->abi_size; |
| 620 | entry->abi_align = payload_type->abi_align; |
| 710 | 621 | } else { |
| 711 | | LLVMTypeRef elem_types[] = { |
| 712 | | err_set_type->type_ref, |
| 713 | | payload_type->type_ref, |
| 714 | | }; |
| 715 | | entry->type_ref = LLVMStructType(elem_types, 2, false); |
| 716 | | |
| 717 | | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 718 | | ZigLLVMDIFile *di_file = nullptr; |
| 719 | | unsigned line = 0; |
| 720 | | entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 721 | | ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name), |
| 722 | | compile_unit_scope, di_file, line); |
| 723 | | |
| 724 | | uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_type->type_ref); |
| 725 | | uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_type->type_ref); |
| 726 | | uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, err_union_err_index); |
| 727 | | |
| 728 | | uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_type->type_ref); |
| 729 | | uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_type->type_ref); |
| 730 | | uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, |
| 731 | | err_union_payload_index); |
| 732 | | |
| 733 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 734 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref); |
| 735 | | |
| 736 | | ZigLLVMDIType *di_element_types[] = { |
| 737 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 738 | | "tag", di_file, line, |
| 739 | | tag_debug_size_in_bits, |
| 740 | | tag_debug_align_in_bits, |
| 741 | | tag_offset_in_bits, |
| 742 | | 0, err_set_type->di_type), |
| 743 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 744 | | "value", di_file, line, |
| 745 | | value_debug_size_in_bits, |
| 746 | | value_debug_align_in_bits, |
| 747 | | value_offset_in_bits, |
| 748 | | 0, payload_type->di_type), |
| 749 | | }; |
| 750 | | |
| 751 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 752 | | compile_unit_scope, |
| 753 | | buf_ptr(&entry->name), |
| 754 | | di_file, line, |
| 755 | | debug_size_in_bits, |
| 756 | | debug_align_in_bits, |
| 757 | | 0, |
| 758 | | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 759 | | |
| 760 | | ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type); |
| 761 | | entry->di_type = replacement_di_type; |
| 622 | entry->abi_align = max(err_set_type->abi_align, payload_type->abi_align); |
| 623 | size_t field_sizes[2]; |
| 624 | size_t field_aligns[2]; |
| 625 | field_sizes[err_union_err_index] = err_set_type->abi_size; |
| 626 | field_aligns[err_union_err_index] = err_set_type->abi_align; |
| 627 | field_sizes[err_union_payload_index] = payload_type->abi_size; |
| 628 | field_aligns[err_union_payload_index] = payload_type->abi_align; |
| 629 | size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[1]); |
| 630 | entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], entry->abi_align); |
| 631 | entry->size_in_bits = entry->abi_size * 8; |
| 762 | 632 | } |
| 763 | 633 | |
| 764 | 634 | g->type_table.put(type_id, entry); |
| ... | ... | @@ -772,31 +642,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) { |
| 772 | 642 | type_id.data.array.size = array_size; |
| 773 | 643 | auto existing_entry = g->type_table.maybe_get(type_id); |
| 774 | 644 | if (existing_entry) { |
| 775 | | ZigType *entry = existing_entry->value; |
| 776 | | return entry; |
| 645 | return existing_entry->value; |
| 777 | 646 | } |
| 778 | 647 | |
| 779 | | assertNoError(ensure_complete_type(g, child_type)); |
| 648 | assert(type_is_resolved(child_type, ResolveStatusSizeKnown)); |
| 780 | 649 | |
| 781 | 650 | ZigType *entry = new_type_table_entry(ZigTypeIdArray); |
| 782 | | entry->zero_bits = (array_size == 0) || child_type->zero_bits; |
| 783 | 651 | |
| 784 | 652 | buf_resize(&entry->name, 0); |
| 785 | 653 | buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name)); |
| 786 | 654 | |
| 787 | | if (entry->zero_bits) { |
| 788 | | entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, 0, |
| 789 | | 0, child_type->di_type, 0); |
| 790 | | } else { |
| 791 | | entry->type_ref = child_type->type_ref ? LLVMArrayType(child_type->type_ref, |
| 792 | | (unsigned int)array_size) : nullptr; |
| 793 | | |
| 794 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 795 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref); |
| 655 | entry->size_in_bits = child_type->size_in_bits * array_size; |
| 656 | entry->abi_align = child_type->abi_align; |
| 657 | entry->abi_size = child_type->abi_size * array_size; |
| 796 | 658 | |
| 797 | | entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits, |
| 798 | | debug_align_in_bits, child_type->di_type, (int)array_size); |
| 799 | | } |
| 800 | 659 | entry->data.array.child_type = child_type; |
| 801 | 660 | entry->data.array.len = array_size; |
| 802 | 661 | |
| ... | ... | @@ -804,11 +663,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) { |
| 804 | 663 | return entry; |
| 805 | 664 | } |
| 806 | 665 | |
| 807 | | static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *entry) { |
| 666 | ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) { |
| 667 | assert(ptr_type->id == ZigTypeIdPointer); |
| 668 | assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown); |
| 669 | |
| 670 | ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent; |
| 671 | if (*parent_pointer) { |
| 672 | return *parent_pointer; |
| 673 | } |
| 674 | |
| 675 | ZigType *entry = new_type_table_entry(ZigTypeIdStruct); |
| 676 | |
| 677 | // replace the & with [] to go from a ptr type name to a slice type name |
| 678 | buf_resize(&entry->name, 0); |
| 679 | size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3; |
| 680 | buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset); |
| 681 | |
| 808 | 682 | unsigned element_count = 2; |
| 809 | 683 | Buf *ptr_field_name = buf_create_from_str("ptr"); |
| 810 | 684 | Buf *len_field_name = buf_create_from_str("len"); |
| 811 | 685 | |
| 686 | entry->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 812 | 687 | entry->data.structure.layout = ContainerLayoutAuto; |
| 813 | 688 | entry->data.structure.is_slice = true; |
| 814 | 689 | entry->data.structure.src_field_count = element_count; |
| ... | ... | @@ -816,7 +691,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e |
| 816 | 691 | entry->data.structure.fields = allocate<TypeStructField>(element_count); |
| 817 | 692 | entry->data.structure.fields_by_name.init(element_count); |
| 818 | 693 | entry->data.structure.fields[slice_ptr_index].name = ptr_field_name; |
| 819 | | entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type; |
| 694 | entry->data.structure.fields[slice_ptr_index].type_entry = ptr_type; |
| 820 | 695 | entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index; |
| 821 | 696 | entry->data.structure.fields[slice_ptr_index].gen_index = 0; |
| 822 | 697 | entry->data.structure.fields[slice_len_index].name = len_field_name; |
| ... | ... | @@ -827,28 +702,20 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e |
| 827 | 702 | entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]); |
| 828 | 703 | entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]); |
| 829 | 704 | |
| 830 | | if (!type_has_bits(pointer_type->data.pointer.child_type)) { |
| 705 | switch (type_requires_comptime(g, ptr_type)) { |
| 706 | case ReqCompTimeInvalid: |
| 707 | zig_unreachable(); |
| 708 | case ReqCompTimeNo: |
| 709 | break; |
| 710 | case ReqCompTimeYes: |
| 711 | entry->data.structure.requires_comptime = true; |
| 712 | } |
| 713 | |
| 714 | if (!type_has_bits(ptr_type)) { |
| 831 | 715 | entry->data.structure.gen_field_count = 1; |
| 832 | 716 | entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX; |
| 833 | 717 | entry->data.structure.fields[slice_len_index].gen_index = 0; |
| 834 | 718 | } |
| 835 | | } |
| 836 | | |
| 837 | | ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) { |
| 838 | | assert(ptr_type->id == ZigTypeIdPointer); |
| 839 | | assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown); |
| 840 | | |
| 841 | | ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent; |
| 842 | | if (*parent_pointer) { |
| 843 | | return *parent_pointer; |
| 844 | | } |
| 845 | | |
| 846 | | ZigType *entry = new_type_table_entry(ZigTypeIdStruct); |
| 847 | | |
| 848 | | // replace the & with [] to go from a ptr type name to a slice type name |
| 849 | | buf_resize(&entry->name, 0); |
| 850 | | size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3; |
| 851 | | buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset); |
| 852 | 719 | |
| 853 | 720 | ZigType *child_type = ptr_type->data.pointer.child_type; |
| 854 | 721 | if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile || |
| ... | ... | @@ -858,134 +725,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) { |
| 858 | 725 | PtrLenUnknown, 0, 0, 0, false); |
| 859 | 726 | ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type); |
| 860 | 727 | |
| 861 | | slice_type_common_init(g, ptr_type, entry); |
| 862 | | |
| 863 | | entry->type_ref = peer_slice_type->type_ref; |
| 864 | | entry->di_type = peer_slice_type->di_type; |
| 865 | | entry->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 866 | | entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment; |
| 728 | entry->size_in_bits = peer_slice_type->size_in_bits; |
| 729 | entry->abi_size = peer_slice_type->abi_size; |
| 730 | entry->abi_align = peer_slice_type->abi_align; |
| 867 | 731 | |
| 868 | 732 | *parent_pointer = entry; |
| 869 | 733 | return entry; |
| 870 | 734 | } |
| 871 | 735 | |
| 872 | | // If the child type is []const T then we need to make sure the type ref |
| 873 | | // and debug info is the same as if the child type were []T. |
| 874 | | if (is_slice(child_type)) { |
| 875 | | ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry; |
| 876 | | assert(child_ptr_type->id == ZigTypeIdPointer); |
| 877 | | if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile || |
| 878 | | child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero) |
| 879 | | { |
| 880 | | ZigType *grand_child_type = child_ptr_type->data.pointer.child_type; |
| 881 | | ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false, |
| 882 | | PtrLenUnknown, 0, 0, 0, false); |
| 883 | | ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type); |
| 884 | | ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false, |
| 885 | | PtrLenUnknown, 0, 0, 0, false); |
| 886 | | ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type); |
| 887 | | |
| 888 | | entry->type_ref = peer_slice_type->type_ref; |
| 889 | | entry->di_type = peer_slice_type->di_type; |
| 890 | | entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment; |
| 891 | | } |
| 892 | | } |
| 893 | | |
| 894 | | slice_type_common_init(g, ptr_type, entry); |
| 895 | | |
| 896 | | if (!entry->type_ref) { |
| 897 | | entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&entry->name)); |
| 898 | | |
| 899 | | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 900 | | ZigLLVMDIFile *di_file = nullptr; |
| 901 | | unsigned line = 0; |
| 902 | | entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 903 | | ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name), |
| 904 | | compile_unit_scope, di_file, line); |
| 905 | | |
| 906 | | if (child_type->zero_bits) { |
| 907 | | LLVMTypeRef element_types[] = { |
| 908 | | g->builtin_types.entry_usize->type_ref, |
| 909 | | }; |
| 910 | | LLVMStructSetBody(entry->type_ref, element_types, 1, false); |
| 911 | | |
| 912 | | ZigType *usize_type = g->builtin_types.entry_usize; |
| 913 | | uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref); |
| 914 | | uint64_t len_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref); |
| 915 | | uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0); |
| 916 | | |
| 917 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 918 | | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref); |
| 919 | | |
| 920 | | ZigLLVMDIType *di_element_types[] = { |
| 921 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 922 | | "len", di_file, line, |
| 923 | | len_debug_size_in_bits, |
| 924 | | len_debug_align_in_bits, |
| 925 | | len_offset_in_bits, |
| 926 | | 0, usize_type->di_type), |
| 927 | | }; |
| 928 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 929 | | compile_unit_scope, |
| 930 | | buf_ptr(&entry->name), |
| 931 | | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 932 | | nullptr, di_element_types, 1, 0, nullptr, ""); |
| 933 | | |
| 934 | | ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type); |
| 935 | | entry->di_type = replacement_di_type; |
| 936 | | |
| 937 | | entry->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref); |
| 938 | | } else { |
| 939 | | unsigned element_count = 2; |
| 940 | | LLVMTypeRef element_types[] = { |
| 941 | | ptr_type->type_ref, |
| 942 | | g->builtin_types.entry_usize->type_ref, |
| 943 | | }; |
| 944 | | LLVMStructSetBody(entry->type_ref, element_types, element_count, false); |
| 945 | | |
| 946 | | |
| 947 | | uint64_t ptr_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, ptr_type->type_ref); |
| 948 | | uint64_t ptr_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, ptr_type->type_ref); |
| 949 | | uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0); |
| 950 | | |
| 951 | | ZigType *usize_type = g->builtin_types.entry_usize; |
| 952 | | uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref); |
| 953 | | uint64_t len_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref); |
| 954 | | uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1); |
| 955 | | |
| 956 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref); |
| 957 | | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref); |
| 958 | | |
| 959 | | ZigLLVMDIType *di_element_types[] = { |
| 960 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 961 | | "ptr", di_file, line, |
| 962 | | ptr_debug_size_in_bits, |
| 963 | | ptr_debug_align_in_bits, |
| 964 | | ptr_offset_in_bits, |
| 965 | | 0, ptr_type->di_type), |
| 966 | | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type), |
| 967 | | "len", di_file, line, |
| 968 | | len_debug_size_in_bits, |
| 969 | | len_debug_align_in_bits, |
| 970 | | len_offset_in_bits, |
| 971 | | 0, usize_type->di_type), |
| 972 | | }; |
| 973 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 974 | | compile_unit_scope, |
| 975 | | buf_ptr(&entry->name), |
| 976 | | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 977 | | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 978 | | |
| 979 | | ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type); |
| 980 | | entry->di_type = replacement_di_type; |
| 981 | | |
| 982 | | entry->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref); |
| 983 | | } |
| 736 | if (type_has_bits(ptr_type)) { |
| 737 | entry->size_in_bits = ptr_type->size_in_bits + g->builtin_types.entry_usize->size_in_bits; |
| 738 | entry->abi_size = ptr_type->abi_size + g->builtin_types.entry_usize->abi_size; |
| 739 | entry->abi_align = ptr_type->abi_align; |
| 740 | } else { |
| 741 | entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits; |
| 742 | entry->abi_size = g->builtin_types.entry_usize->abi_size; |
| 743 | entry->abi_align = g->builtin_types.entry_usize->abi_align; |
| 984 | 744 | } |
| 985 | 745 | |
| 986 | | |
| 987 | | entry->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 988 | | |
| 989 | 746 | *parent_pointer = entry; |
| 990 | 747 | return entry; |
| 991 | 748 | } |
| ... | ... | @@ -998,15 +755,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c |
| 998 | 755 | ZigType *import = scope ? get_scope_import(scope) : nullptr; |
| 999 | 756 | unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0; |
| 1000 | 757 | |
| 1001 | | entry->type_ref = LLVMInt8Type(); |
| 1002 | | entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder, |
| 758 | entry->llvm_type = LLVMInt8Type(); |
| 759 | entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder, |
| 1003 | 760 | ZigLLVMTag_DW_structure_type(), full_name, |
| 1004 | 761 | import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr, |
| 1005 | 762 | import ? import->data.structure.root_struct->di_file : nullptr, |
| 1006 | 763 | line); |
| 1007 | | entry->zero_bits = false; |
| 1008 | 764 | entry->data.opaque.bare_name = bare_name; |
| 1009 | 765 | |
| 766 | // The actual size is unknown, but the value must not be 0 because that |
| 767 | // is how type_has_bits is determined. |
| 768 | entry->abi_size = SIZE_MAX; |
| 769 | entry->size_in_bits = SIZE_MAX; |
| 770 | entry->abi_align = 1; |
| 771 | |
| 1010 | 772 | return entry; |
| 1011 | 773 | } |
| 1012 | 774 | |
| ... | ... | @@ -1018,7 +780,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) { |
| 1018 | 780 | |
| 1019 | 781 | ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn); |
| 1020 | 782 | bound_fn_type->data.bound_fn.fn_type = fn_type; |
| 1021 | | bound_fn_type->zero_bits = true; |
| 1022 | 783 | |
| 1023 | 784 | buf_resize(&bound_fn_type->name, 0); |
| 1024 | 785 | buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name)); |
| ... | ... | @@ -1114,8 +875,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { |
| 1114 | 875 | ZigType *fn_type = new_type_table_entry(ZigTypeIdFn); |
| 1115 | 876 | fn_type->data.fn.fn_type_id = *fn_type_id; |
| 1116 | 877 | |
| 1117 | | bool skip_debug_info = false; |
| 1118 | | |
| 1119 | 878 | // populate the name of the type |
| 1120 | 879 | buf_resize(&fn_type->name, 0); |
| 1121 | 880 | if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) { |
| ... | ... | @@ -1133,8 +892,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { |
| 1133 | 892 | const char *comma = (i == 0) ? "" : ", "; |
| 1134 | 893 | const char *noalias_str = param_info->is_noalias ? "noalias " : ""; |
| 1135 | 894 | buf_appendf(&fn_type->name, "%s%s%s", comma, noalias_str, buf_ptr(&param_type->name)); |
| 1136 | | |
| 1137 | | skip_debug_info = skip_debug_info || !param_type->di_type; |
| 1138 | 895 | } |
| 1139 | 896 | |
| 1140 | 897 | if (fn_type_id->is_var_args) { |
| ... | ... | @@ -1146,116 +903,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { |
| 1146 | 903 | buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment); |
| 1147 | 904 | } |
| 1148 | 905 | buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name)); |
| 1149 | | skip_debug_info = skip_debug_info || !fn_type_id->return_type->di_type; |
| 1150 | | |
| 1151 | | // next, loop over the parameters again and compute debug information |
| 1152 | | // and codegen information |
| 1153 | | if (!skip_debug_info) { |
| 1154 | | bool first_arg_return = want_first_arg_sret(g, fn_type_id); |
| 1155 | | bool is_async = fn_type_id->cc == CallingConventionAsync; |
| 1156 | | bool is_c_abi = fn_type_id->cc == CallingConventionC; |
| 1157 | | bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id); |
| 1158 | | // +1 for maybe making the first argument the return value |
| 1159 | | // +1 for maybe first argument the error return trace |
| 1160 | | // +2 for maybe arguments async allocator and error code pointer |
| 1161 | | ZigList<LLVMTypeRef> gen_param_types = {}; |
| 1162 | | // +1 because 0 is the return type and |
| 1163 | | // +1 for maybe making first arg ret val and |
| 1164 | | // +1 for maybe first argument the error return trace |
| 1165 | | // +2 for maybe arguments async allocator and error code pointer |
| 1166 | | ZigList<ZigLLVMDIType *> param_di_types = {}; |
| 1167 | | param_di_types.append(fn_type_id->return_type->di_type); |
| 1168 | | ZigType *gen_return_type; |
| 1169 | | if (is_async) { |
| 1170 | | gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); |
| 1171 | | } else if (!type_has_bits(fn_type_id->return_type)) { |
| 1172 | | gen_return_type = g->builtin_types.entry_void; |
| 1173 | | } else if (first_arg_return) { |
| 1174 | | ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false); |
| 1175 | | gen_param_types.append(gen_type->type_ref); |
| 1176 | | param_di_types.append(gen_type->di_type); |
| 1177 | | gen_return_type = g->builtin_types.entry_void; |
| 1178 | | } else { |
| 1179 | | gen_return_type = fn_type_id->return_type; |
| 1180 | | } |
| 1181 | | fn_type->data.fn.gen_return_type = gen_return_type; |
| 1182 | | |
| 1183 | | if (prefix_arg_error_return_trace) { |
| 1184 | | ZigType *gen_type = get_ptr_to_stack_trace_type(g); |
| 1185 | | gen_param_types.append(gen_type->type_ref); |
| 1186 | | param_di_types.append(gen_type->di_type); |
| 1187 | | } |
| 1188 | | if (is_async) { |
| 1189 | | { |
| 1190 | | // async allocator param |
| 1191 | | ZigType *gen_type = fn_type_id->async_allocator_type; |
| 1192 | | gen_param_types.append(gen_type->type_ref); |
| 1193 | | param_di_types.append(gen_type->di_type); |
| 1194 | | } |
| 1195 | | |
| 1196 | | { |
| 1197 | | // error code pointer |
| 1198 | | ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); |
| 1199 | | gen_param_types.append(gen_type->type_ref); |
| 1200 | | param_di_types.append(gen_type->di_type); |
| 1201 | | } |
| 1202 | | } |
| 1203 | | |
| 1204 | | fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count); |
| 1205 | | for (size_t i = 0; i < fn_type_id->param_count; i += 1) { |
| 1206 | | FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i]; |
| 1207 | | ZigType *type_entry = src_param_info->type; |
| 1208 | | FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i]; |
| 1209 | | |
| 1210 | | gen_param_info->src_index = i; |
| 1211 | | gen_param_info->gen_index = SIZE_MAX; |
| 1212 | | |
| 1213 | | if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown))) |
| 1214 | | return g->builtin_types.entry_invalid; |
| 1215 | | |
| 1216 | | if (is_c_abi) { |
| 1217 | | if ((err = type_resolve(g, type_entry, ResolveStatusSizeKnown))) |
| 1218 | | return g->builtin_types.entry_invalid; |
| 1219 | | continue; |
| 1220 | | } |
| 1221 | 906 | |
| 1222 | | if (type_has_bits(type_entry)) { |
| 1223 | | ZigType *gen_type; |
| 1224 | | if (handle_is_ptr(type_entry)) { |
| 1225 | | gen_type = get_pointer_to_type(g, type_entry, true); |
| 1226 | | gen_param_info->is_byval = true; |
| 1227 | | } else { |
| 1228 | | gen_type = type_entry; |
| 1229 | | } |
| 1230 | | gen_param_info->gen_index = gen_param_types.length; |
| 1231 | | gen_param_info->type = gen_type; |
| 1232 | | gen_param_types.append(gen_type->type_ref); |
| 1233 | | |
| 1234 | | param_di_types.append(gen_type->di_type); |
| 1235 | | } |
| 1236 | | } |
| 1237 | | |
| 1238 | | if (is_c_abi) { |
| 1239 | | FnWalk fn_walk = {}; |
| 1240 | | fn_walk.id = FnWalkIdTypes; |
| 1241 | | fn_walk.data.types.param_di_types = &param_di_types; |
| 1242 | | fn_walk.data.types.gen_param_types = &gen_param_types; |
| 1243 | | walk_function_params(g, fn_type, &fn_walk); |
| 1244 | | } |
| 1245 | | |
| 1246 | | fn_type->data.fn.gen_param_count = gen_param_types.length; |
| 1247 | | |
| 1248 | | for (size_t i = 0; i < gen_param_types.length; i += 1) { |
| 1249 | | assert(gen_param_types.items[i] != nullptr); |
| 1250 | | } |
| 1251 | | fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref, |
| 1252 | | gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args); |
| 1253 | | fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0); |
| 1254 | | fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0); |
| 1255 | | fn_type->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type, |
| 1256 | | LLVMStoreSizeOfType(g->target_data_ref, fn_type->type_ref), |
| 1257 | | LLVMABIAlignmentOfType(g->target_data_ref, fn_type->type_ref), ""); |
| 1258 | | } |
| 907 | // The fn_type is a pointer; not to be confused with the raw function type. |
| 908 | fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits; |
| 909 | fn_type->abi_size = g->builtin_types.entry_usize->abi_size; |
| 910 | fn_type->abi_align = g->builtin_types.entry_usize->abi_align; |
| 1259 | 911 | |
| 1260 | 912 | g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type); |
| 1261 | 913 | |
| ... | ... | @@ -1282,14 +934,6 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf * |
| 1282 | 934 | entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name); |
| 1283 | 935 | entry->data.structure.root_struct = root_struct; |
| 1284 | 936 | entry->data.structure.layout = ContainerLayoutAuto; |
| 1285 | | entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name); |
| 1286 | | |
| 1287 | | size_t line = 0; // root therefore first line |
| 1288 | | unsigned dwarf_kind = ZigLLVMTag_DW_structure_type(); |
| 1289 | | |
| 1290 | | entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 1291 | | dwarf_kind, full_name, |
| 1292 | | ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1)); |
| 1293 | 937 | |
| 1294 | 938 | buf_init_from_str(&entry->name, full_name); |
| 1295 | 939 | return entry; |
| ... | ... | @@ -1316,16 +960,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind |
| 1316 | 960 | break; |
| 1317 | 961 | } |
| 1318 | 962 | |
| 1319 | | size_t line = decl_node ? decl_node->line : 0; |
| 1320 | | unsigned dwarf_kind = ZigLLVMTag_DW_structure_type(); |
| 1321 | | |
| 1322 | | ZigType *import = get_scope_import(scope); |
| 1323 | | entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name); |
| 1324 | | entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 1325 | | dwarf_kind, full_name, |
| 1326 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 1327 | | import->data.structure.root_struct->di_file, (unsigned)(line + 1)); |
| 1328 | | |
| 1329 | 963 | buf_init_from_str(&entry->name, full_name); |
| 1330 | 964 | |
| 1331 | 965 | return entry; |
| ... | ... | @@ -1375,7 +1009,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) { |
| 1375 | 1009 | |
| 1376 | 1010 | fn_type->data.fn.fn_type_id = *fn_type_id; |
| 1377 | 1011 | fn_type->data.fn.is_generic = true; |
| 1378 | | fn_type->zero_bits = true; |
| 1012 | fn_type->abi_size = 0; |
| 1013 | fn_type->size_in_bits = 0; |
| 1014 | fn_type->abi_align = 0; |
| 1379 | 1015 | return fn_type; |
| 1380 | 1016 | } |
| 1381 | 1017 | |
| ... | ... | @@ -1488,6 +1124,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType |
| 1488 | 1124 | ZigType *elem_type = type_entry->data.array.child_type; |
| 1489 | 1125 | if ((err = emit_error_unless_type_allowed_in_packed_struct(g, elem_type, source_node))) |
| 1490 | 1126 | return err; |
| 1127 | // TODO revisit this when doing https://github.com/ziglang/zig/issues/1512 |
| 1491 | 1128 | if (type_size(g, type_entry) * 8 == type_size_bits(g, type_entry)) |
| 1492 | 1129 | return ErrorNone; |
| 1493 | 1130 | add_node_error(g, source_node, |
| ... | ... | @@ -1611,13 +1248,12 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) { |
| 1611 | 1248 | ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet); |
| 1612 | 1249 | buf_resize(&err_set_type->name, 0); |
| 1613 | 1250 | buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name)); |
| 1614 | | err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref; |
| 1615 | | err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type; |
| 1616 | 1251 | err_set_type->data.error_set.err_count = 0; |
| 1617 | 1252 | err_set_type->data.error_set.errors = nullptr; |
| 1618 | 1253 | err_set_type->data.error_set.infer_fn = fn_entry; |
| 1619 | | |
| 1620 | | g->error_di_types.append(&err_set_type->di_type); |
| 1254 | err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits; |
| 1255 | err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align; |
| 1256 | err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size; |
| 1621 | 1257 | |
| 1622 | 1258 | return err_set_type; |
| 1623 | 1259 | } |
| ... | ... | @@ -1858,10 +1494,10 @@ bool type_is_invalid(ZigType *type_entry) { |
| 1858 | 1494 | return true; |
| 1859 | 1495 | case ZigTypeIdStruct: |
| 1860 | 1496 | return type_entry->data.structure.resolve_status == ResolveStatusInvalid; |
| 1497 | case ZigTypeIdUnion: |
| 1498 | return type_entry->data.unionation.resolve_status == ResolveStatusInvalid; |
| 1861 | 1499 | case ZigTypeIdEnum: |
| 1862 | 1500 | return type_entry->data.enumeration.is_invalid; |
| 1863 | | case ZigTypeIdUnion: |
| 1864 | | return type_entry->data.unionation.is_invalid; |
| 1865 | 1501 | default: |
| 1866 | 1502 | return false; |
| 1867 | 1503 | } |
| ... | ... | @@ -1869,178 +1505,80 @@ bool type_is_invalid(ZigType *type_entry) { |
| 1869 | 1505 | } |
| 1870 | 1506 | |
| 1871 | 1507 | |
| 1872 | | static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) { |
| 1873 | | assert(enum_type->id == ZigTypeIdEnum); |
| 1874 | | |
| 1875 | | if (enum_type->data.enumeration.is_invalid) |
| 1876 | | return ErrorSemanticAnalyzeFail; |
| 1508 | ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[], |
| 1509 | ZigType *field_types[], size_t field_count) |
| 1510 | { |
| 1511 | ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct); |
| 1877 | 1512 | |
| 1878 | | if (enum_type->data.enumeration.complete) |
| 1879 | | return ErrorNone; |
| 1513 | buf_init_from_str(&struct_type->name, type_name); |
| 1880 | 1514 | |
| 1881 | | Error err; |
| 1882 | | if ((err = resolve_enum_zero_bits(g, enum_type))) |
| 1883 | | return err; |
| 1515 | struct_type->data.structure.src_field_count = field_count; |
| 1516 | struct_type->data.structure.gen_field_count = 0; |
| 1517 | struct_type->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 1518 | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); |
| 1519 | struct_type->data.structure.fields_by_name.init(field_count); |
| 1884 | 1520 | |
| 1885 | | AstNode *decl_node = enum_type->data.enumeration.decl_node; |
| 1521 | size_t abi_align = 0; |
| 1522 | for (size_t i = 0; i < field_count; i += 1) { |
| 1523 | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 1524 | field->name = buf_create_from_str(field_names[i]); |
| 1525 | field->type_entry = field_types[i]; |
| 1526 | field->src_index = i; |
| 1886 | 1527 | |
| 1887 | | if (enum_type->data.enumeration.embedded_in_current) { |
| 1888 | | if (!enum_type->data.enumeration.reported_infinite_err) { |
| 1889 | | enum_type->data.enumeration.is_invalid = true; |
| 1890 | | enum_type->data.enumeration.reported_infinite_err = true; |
| 1891 | | ErrorMsg *msg = add_node_error(g, decl_node, |
| 1892 | | buf_sprintf("enum '%s' contains itself", buf_ptr(&enum_type->name))); |
| 1893 | | emit_error_notes_for_ref_stack(g, msg); |
| 1528 | if (type_has_bits(field->type_entry)) { |
| 1529 | assert(type_is_resolved(field->type_entry, ResolveStatusSizeKnown)); |
| 1530 | if (field->type_entry->abi_align > abi_align) { |
| 1531 | abi_align = field->type_entry->abi_align; |
| 1532 | } |
| 1533 | field->gen_index = struct_type->data.structure.gen_field_count; |
| 1534 | struct_type->data.structure.gen_field_count += 1; |
| 1535 | } else { |
| 1536 | field->gen_index = SIZE_MAX; |
| 1894 | 1537 | } |
| 1895 | | return ErrorSemanticAnalyzeFail; |
| 1538 | |
| 1539 | auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field); |
| 1540 | assert(prev_entry == nullptr); |
| 1896 | 1541 | } |
| 1897 | 1542 | |
| 1898 | | assert(!enum_type->data.enumeration.zero_bits_loop_flag); |
| 1899 | | assert(decl_node->type == NodeTypeContainerDecl); |
| 1900 | | assert(enum_type->di_type); |
| 1543 | size_t next_offset = 0; |
| 1544 | for (size_t i = 0; i < field_count; i += 1) { |
| 1545 | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 1546 | if (field->gen_index == SIZE_MAX) |
| 1547 | continue; |
| 1548 | field->offset = next_offset; |
| 1549 | size_t next_src_field_index = i + 1; |
| 1550 | for (; next_src_field_index < field_count; next_src_field_index += 1) { |
| 1551 | if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) { |
| 1552 | break; |
| 1553 | } |
| 1554 | } |
| 1555 | size_t next_abi_align = (next_src_field_index == field_count) ? |
| 1556 | abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align; |
| 1557 | next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align); |
| 1558 | } |
| 1901 | 1559 | |
| 1902 | | uint32_t field_count = enum_type->data.enumeration.src_field_count; |
| 1560 | struct_type->abi_align = abi_align; |
| 1561 | struct_type->abi_size = next_offset; |
| 1562 | struct_type->size_in_bits = next_offset * 8; |
| 1903 | 1563 | |
| 1904 | | assert(enum_type->data.enumeration.fields); |
| 1905 | | ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count); |
| 1906 | | |
| 1907 | | Scope *scope = &enum_type->data.enumeration.decls_scope->base; |
| 1908 | | ZigType *import = get_scope_import(scope); |
| 1909 | | |
| 1910 | | // set temporary flag |
| 1911 | | enum_type->data.enumeration.embedded_in_current = true; |
| 1912 | | |
| 1913 | | for (uint32_t i = 0; i < field_count; i += 1) { |
| 1914 | | TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i]; |
| 1915 | | |
| 1916 | | // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t |
| 1917 | | // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html |
| 1918 | | di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name), |
| 1919 | | bigint_as_signed(&enum_field->value)); |
| 1920 | | } |
| 1921 | | |
| 1922 | | // unset temporary flag |
| 1923 | | enum_type->data.enumeration.embedded_in_current = false; |
| 1924 | | enum_type->data.enumeration.complete = true; |
| 1925 | | |
| 1926 | | if (enum_type->data.enumeration.is_invalid) |
| 1927 | | return ErrorSemanticAnalyzeFail; |
| 1928 | | |
| 1929 | | if (enum_type->zero_bits) { |
| 1930 | | enum_type->type_ref = LLVMVoidType(); |
| 1931 | | |
| 1932 | | uint64_t debug_size_in_bits = 0; |
| 1933 | | uint64_t debug_align_in_bits = 0; |
| 1934 | | ZigLLVMDIType **di_root_members = nullptr; |
| 1935 | | size_t debug_member_count = 0; |
| 1936 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 1937 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 1938 | | buf_ptr(&enum_type->name), |
| 1939 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 1940 | | debug_size_in_bits, |
| 1941 | | debug_align_in_bits, |
| 1942 | | 0, nullptr, di_root_members, (int)debug_member_count, 0, nullptr, ""); |
| 1943 | | |
| 1944 | | ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, replacement_di_type); |
| 1945 | | enum_type->di_type = replacement_di_type; |
| 1946 | | return ErrorNone; |
| 1947 | | } |
| 1948 | | |
| 1949 | | ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type; |
| 1950 | | |
| 1951 | | // create debug type for tag |
| 1952 | | uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_int_type->type_ref); |
| 1953 | | uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref); |
| 1954 | | ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder, |
| 1955 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name), |
| 1956 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 1957 | | tag_debug_size_in_bits, |
| 1958 | | tag_debug_align_in_bits, |
| 1959 | | di_enumerators, field_count, |
| 1960 | | tag_int_type->di_type, ""); |
| 1961 | | |
| 1962 | | ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, tag_di_type); |
| 1963 | | enum_type->di_type = tag_di_type; |
| 1964 | | return ErrorNone; |
| 1564 | return struct_type; |
| 1965 | 1565 | } |
| 1966 | 1566 | |
| 1567 | static size_t get_store_size_bytes(size_t size_in_bits) { |
| 1568 | return (size_in_bits + 7) / 8; |
| 1569 | } |
| 1967 | 1570 | |
| 1968 | | ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[], |
| 1969 | | ZigType *field_types[], size_t field_count) |
| 1970 | | { |
| 1971 | | ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct); |
| 1972 | | |
| 1973 | | buf_init_from_str(&struct_type->name, type_name); |
| 1974 | | |
| 1975 | | struct_type->data.structure.src_field_count = field_count; |
| 1976 | | struct_type->data.structure.gen_field_count = 0; |
| 1977 | | struct_type->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 1978 | | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); |
| 1979 | | struct_type->data.structure.fields_by_name.init(field_count); |
| 1980 | | |
| 1981 | | ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count); |
| 1982 | | LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count); |
| 1983 | | for (size_t i = 0; i < field_count; i += 1) { |
| 1984 | | element_types[struct_type->data.structure.gen_field_count] = field_types[i]->type_ref; |
| 1985 | | |
| 1986 | | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 1987 | | field->name = buf_create_from_str(field_names[i]); |
| 1988 | | field->type_entry = field_types[i]; |
| 1989 | | field->src_index = i; |
| 1990 | | |
| 1991 | | if (type_has_bits(field->type_entry)) { |
| 1992 | | field->gen_index = struct_type->data.structure.gen_field_count; |
| 1993 | | struct_type->data.structure.gen_field_count += 1; |
| 1994 | | } else { |
| 1995 | | field->gen_index = SIZE_MAX; |
| 1996 | | } |
| 1997 | | |
| 1998 | | auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field); |
| 1999 | | assert(prev_entry == nullptr); |
| 2000 | | } |
| 2001 | | |
| 2002 | | struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name); |
| 2003 | | LLVMStructSetBody(struct_type->type_ref, element_types, struct_type->data.structure.gen_field_count, false); |
| 2004 | | |
| 2005 | | struct_type->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 2006 | | ZigLLVMTag_DW_structure_type(), type_name, |
| 2007 | | ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0); |
| 2008 | | |
| 2009 | | for (size_t i = 0; i < field_count; i += 1) { |
| 2010 | | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 2011 | | if (type_struct_field->gen_index == SIZE_MAX) { |
| 2012 | | continue; |
| 2013 | | } |
| 2014 | | ZigType *field_type = type_struct_field->type_entry; |
| 2015 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref); |
| 2016 | | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref); |
| 2017 | | uint64_t debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref, type_struct_field->gen_index); |
| 2018 | | di_element_types[type_struct_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 2019 | | ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name), |
| 2020 | | nullptr, 0, |
| 2021 | | debug_size_in_bits, |
| 2022 | | debug_align_in_bits, |
| 2023 | | debug_offset_in_bits, |
| 2024 | | 0, field_type->di_type); |
| 2025 | | |
| 2026 | | assert(di_element_types[type_struct_field->gen_index]); |
| 2027 | | } |
| 2028 | | |
| 2029 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref); |
| 2030 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref); |
| 2031 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 2032 | | ZigLLVMCompileUnitToScope(g->compile_unit), |
| 2033 | | type_name, nullptr, 0, |
| 2034 | | debug_size_in_bits, |
| 2035 | | debug_align_in_bits, |
| 2036 | | 0, |
| 2037 | | nullptr, di_element_types, struct_type->data.structure.gen_field_count, 0, nullptr, ""); |
| 2038 | | |
| 2039 | | ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type); |
| 2040 | | struct_type->di_type = replacement_di_type; |
| 2041 | | struct_type->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, struct_type->type_ref); |
| 1571 | static size_t get_abi_align_bytes(size_t size_in_bits, size_t pointer_size_bytes) { |
| 1572 | size_t store_size_bytes = get_store_size_bytes(size_in_bits); |
| 1573 | if (store_size_bytes >= pointer_size_bytes) |
| 1574 | return pointer_size_bytes; |
| 1575 | return round_to_next_power_of_2(store_size_bytes); |
| 1576 | } |
| 2042 | 1577 | |
| 2043 | | return struct_type; |
| 1578 | static size_t get_abi_size_bytes(size_t size_in_bits, size_t pointer_size_bytes) { |
| 1579 | size_t store_size_bytes = get_store_size_bytes(size_in_bits); |
| 1580 | size_t abi_align = get_abi_align_bytes(size_in_bits, pointer_size_bytes); |
| 1581 | return align_forward(store_size_bytes, abi_align); |
| 2044 | 1582 | } |
| 2045 | 1583 | |
| 2046 | 1584 | static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { |
| ... | ... | @@ -2068,454 +1606,313 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { |
| 2068 | 1606 | return ErrorSemanticAnalyzeFail; |
| 2069 | 1607 | } |
| 2070 | 1608 | |
| 2071 | | struct_type->data.structure.resolve_loop_flag = true; |
| 2072 | | |
| 2073 | 1609 | assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0); |
| 2074 | 1610 | assert(decl_node->type == NodeTypeContainerDecl); |
| 2075 | 1611 | |
| 2076 | 1612 | size_t field_count = struct_type->data.structure.src_field_count; |
| 2077 | 1613 | |
| 2078 | | size_t gen_field_count = struct_type->data.structure.gen_field_count; |
| 2079 | | LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count); |
| 2080 | | |
| 2081 | | Scope *scope = &struct_type->data.structure.decls_scope->base; |
| 2082 | | |
| 2083 | | size_t gen_field_index = 0; |
| 2084 | 1614 | bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked); |
| 2085 | | size_t packed_bits_offset = 0; |
| 2086 | | size_t first_packed_bits_offset_misalign = SIZE_MAX; |
| 2087 | | size_t debug_field_count = 0; |
| 1615 | struct_type->data.structure.resolve_loop_flag = true; |
| 2088 | 1616 | |
| 2089 | | for (size_t i = 0; i < field_count; i += 1) { |
| 2090 | | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 2091 | | ZigType *field_type = type_struct_field->type_entry; |
| 1617 | uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr; |
| 2092 | 1618 | |
| 2093 | | if ((err = ensure_complete_type(g, field_type))) { |
| 1619 | // Resolve sizes of all the field types. Done before the offset loop because the offset |
| 1620 | // loop has to look ahead. |
| 1621 | for (size_t i = 0; i < field_count; i += 1) { |
| 1622 | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 1623 | if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) { |
| 2094 | 1624 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2095 | | break; |
| 1625 | return ErrorSemanticAnalyzeFail; |
| 2096 | 1626 | } |
| 1627 | } |
| 2097 | 1628 | |
| 2098 | | if (struct_type->data.structure.layout == ContainerLayoutExtern) { |
| 2099 | | if (!type_allowed_in_extern(g, field_type)) { |
| 2100 | | AstNode *field_source_node = decl_node->data.container_decl.fields.at(i); |
| 2101 | | add_node_error(g, field_source_node, |
| 2102 | | buf_sprintf("extern structs cannot contain fields of type '%s'", |
| 2103 | | buf_ptr(&field_type->name))); |
| 2104 | | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2105 | | break; |
| 2106 | | } |
| 2107 | | } |
| 1629 | size_t packed_bits_offset = 0; |
| 1630 | size_t next_offset = 0; |
| 1631 | size_t first_packed_bits_offset_misalign = SIZE_MAX; |
| 1632 | size_t gen_field_index = 0; |
| 1633 | size_t size_in_bits = 0; |
| 1634 | size_t abi_align = struct_type->abi_align; |
| 2108 | 1635 | |
| 2109 | | if (!type_has_bits(field_type)) |
| 1636 | // Calculate offsets |
| 1637 | for (size_t i = 0; i < field_count; i += 1) { |
| 1638 | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 1639 | if (field->gen_index == SIZE_MAX) |
| 2110 | 1640 | continue; |
| 1641 | ZigType *field_type = field->type_entry; |
| 1642 | assert(field_type != nullptr); |
| 2111 | 1643 | |
| 2112 | | type_struct_field->gen_index = gen_field_index; |
| 1644 | field->gen_index = gen_field_index; |
| 1645 | field->offset = next_offset; |
| 2113 | 1646 | |
| 2114 | 1647 | if (packed) { |
| 2115 | | AstNode *field_source_node = decl_node->data.container_decl.fields.at(i); |
| 2116 | | if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) { |
| 2117 | | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2118 | | break; |
| 2119 | | } |
| 2120 | | |
| 2121 | 1648 | size_t field_size_in_bits = type_size_bits(g, field_type); |
| 2122 | 1649 | size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits; |
| 2123 | 1650 | |
| 1651 | size_in_bits += field_size_in_bits; |
| 1652 | |
| 2124 | 1653 | if (first_packed_bits_offset_misalign != SIZE_MAX) { |
| 2125 | 1654 | // this field is not byte-aligned; it is part of the previous field with a bit offset |
| 2126 | | type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign; |
| 1655 | field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign; |
| 2127 | 1656 | |
| 2128 | 1657 | size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign; |
| 2129 | | LLVMTypeRef int_type_ref = LLVMIntType((unsigned)(full_bit_count)); |
| 2130 | | if (8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref) == full_bit_count) { |
| 2131 | | // next field recovers store alignment |
| 2132 | | element_types[gen_field_index] = int_type_ref; |
| 1658 | size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes); |
| 1659 | if (full_abi_size * 8 == full_bit_count) { |
| 1660 | // next field recovers ABI alignment |
| 1661 | host_int_bytes[gen_field_index] = full_abi_size; |
| 2133 | 1662 | gen_field_index += 1; |
| 1663 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 1664 | next_offset = next_field_offset(next_offset, abi_align, full_abi_size, 1); |
| 1665 | size_in_bits = next_offset * 8; |
| 2134 | 1666 | |
| 2135 | 1667 | first_packed_bits_offset_misalign = SIZE_MAX; |
| 2136 | 1668 | } |
| 2137 | | } else if (8 * LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref) != field_size_in_bits) { |
| 1669 | } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) { |
| 2138 | 1670 | first_packed_bits_offset_misalign = packed_bits_offset; |
| 2139 | | type_struct_field->bit_offset_in_host = 0; |
| 1671 | field->bit_offset_in_host = 0; |
| 2140 | 1672 | } else { |
| 2141 | 1673 | // This is a byte-aligned field (both start and end) in a packed struct. |
| 2142 | | element_types[gen_field_index] = field_type->type_ref; |
| 2143 | | type_struct_field->bit_offset_in_host = 0; |
| 1674 | host_int_bytes[gen_field_index] = field_type->size_in_bits / 8; |
| 1675 | field->bit_offset_in_host = 0; |
| 2144 | 1676 | gen_field_index += 1; |
| 1677 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 1678 | next_offset = next_field_offset(next_offset, abi_align, field_type->size_in_bits / 8, 1); |
| 1679 | size_in_bits = next_offset * 8; |
| 2145 | 1680 | } |
| 2146 | 1681 | packed_bits_offset = next_packed_bits_offset; |
| 2147 | 1682 | } else { |
| 2148 | | element_types[gen_field_index] = field_type->type_ref; |
| 2149 | | assert(element_types[gen_field_index]); |
| 2150 | | |
| 2151 | 1683 | gen_field_index += 1; |
| 1684 | size_t next_src_field_index = i + 1; |
| 1685 | for (; next_src_field_index < field_count; next_src_field_index += 1) { |
| 1686 | if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) { |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | size_t next_abi_align = (next_src_field_index == field_count) ? |
| 1691 | abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align; |
| 1692 | next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align); |
| 1693 | size_in_bits = next_offset * 8; |
| 2152 | 1694 | } |
| 2153 | | debug_field_count += 1; |
| 2154 | 1695 | } |
| 2155 | 1696 | if (first_packed_bits_offset_misalign != SIZE_MAX) { |
| 2156 | 1697 | size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign; |
| 2157 | | LLVMTypeRef int_type_ref = LLVMIntType((unsigned)full_bit_count); |
| 2158 | | size_t store_bit_count = 8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref); |
| 2159 | | element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count); |
| 1698 | size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes); |
| 1699 | next_offset = next_field_offset(next_offset, abi_align, full_abi_size, abi_align); |
| 1700 | host_int_bytes[gen_field_index] = full_abi_size; |
| 2160 | 1701 | gen_field_index += 1; |
| 2161 | 1702 | } |
| 2162 | 1703 | |
| 1704 | struct_type->abi_size = next_offset; |
| 1705 | struct_type->size_in_bits = size_in_bits; |
| 1706 | struct_type->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 1707 | struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index; |
| 2163 | 1708 | struct_type->data.structure.resolve_loop_flag = false; |
| 1709 | struct_type->data.structure.host_int_bytes = host_int_bytes; |
| 2164 | 1710 | |
| 2165 | | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) |
| 2166 | | return ErrorSemanticAnalyzeFail; |
| 1711 | return ErrorNone; |
| 1712 | } |
| 2167 | 1713 | |
| 2168 | | struct_type->data.structure.resolve_status = ResolveStatusSizeKnown; |
| 1714 | static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { |
| 1715 | assert(union_type->id == ZigTypeIdUnion); |
| 2169 | 1716 | |
| 2170 | | if (struct_type->zero_bits) { |
| 2171 | | struct_type->type_ref = LLVMVoidType(); |
| 1717 | Error err; |
| 2172 | 1718 | |
| 2173 | | ZigType *import = get_scope_import(scope); |
| 2174 | | uint64_t debug_size_in_bits = 0; |
| 2175 | | uint64_t debug_align_in_bits = 0; |
| 2176 | | ZigLLVMDIType **di_element_types = nullptr; |
| 2177 | | size_t debug_field_count = 0; |
| 2178 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 2179 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 2180 | | buf_ptr(&struct_type->name), |
| 2181 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2182 | | debug_size_in_bits, |
| 2183 | | debug_align_in_bits, |
| 2184 | | 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, ""); |
| 2185 | | ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type); |
| 2186 | | struct_type->di_type = replacement_di_type; |
| 1719 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 1720 | return ErrorSemanticAnalyzeFail; |
| 1721 | if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown) |
| 1722 | return ErrorNone; |
| 1723 | if ((err = resolve_union_zero_bits(g, union_type))) |
| 1724 | return err; |
| 1725 | if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown) |
| 2187 | 1726 | return ErrorNone; |
| 1727 | |
| 1728 | if (union_type->data.unionation.resolve_loop_flag) { |
| 1729 | if (!union_type->data.unionation.reported_infinite_err) { |
| 1730 | AstNode *decl_node = union_type->data.unionation.decl_node; |
| 1731 | union_type->data.unionation.reported_infinite_err = true; |
| 1732 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 1733 | ErrorMsg *msg = add_node_error(g, decl_node, |
| 1734 | buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name))); |
| 1735 | emit_error_notes_for_ref_stack(g, msg); |
| 1736 | } |
| 1737 | return ErrorSemanticAnalyzeFail; |
| 2188 | 1738 | } |
| 2189 | | assert(struct_type->di_type); |
| 2190 | 1739 | |
| 1740 | // set temporary flag |
| 1741 | union_type->data.unionation.resolve_loop_flag = true; |
| 2191 | 1742 | |
| 2192 | | // the count may have been adjusting from packing bit fields |
| 2193 | | gen_field_count = gen_field_index; |
| 2194 | | struct_type->data.structure.gen_field_count = (uint32_t)gen_field_count; |
| 1743 | ZigType *most_aligned_union_member = nullptr; |
| 1744 | uint32_t field_count = union_type->data.unionation.src_field_count; |
| 1745 | bool packed = union_type->data.unionation.layout == ContainerLayoutPacked; |
| 2195 | 1746 | |
| 2196 | | LLVMStructSetBody(struct_type->type_ref, element_types, (unsigned)gen_field_count, packed); |
| 1747 | for (uint32_t i = 0; i < field_count; i += 1) { |
| 1748 | TypeUnionField *field = &union_type->data.unionation.fields[i]; |
| 1749 | if (field->gen_index == UINT32_MAX) |
| 1750 | continue; |
| 2197 | 1751 | |
| 2198 | | // if you hit this assert then probably this type or a related type didn't |
| 2199 | | // get ensure_complete_type called on it before using it with something that |
| 2200 | | // requires a complete type |
| 2201 | | assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0); |
| 1752 | size_t this_field_align; |
| 1753 | if (packed) { |
| 1754 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 1755 | this_field_align = 1; |
| 1756 | // This is the same hack as resolve_struct_alignment. See the comment there. |
| 1757 | } else if (field->type_entry == nullptr) { |
| 1758 | this_field_align = g->builtin_types.entry_usize->abi_align; |
| 1759 | } else { |
| 1760 | if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) { |
| 1761 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 1762 | return ErrorSemanticAnalyzeFail; |
| 1763 | } |
| 2202 | 1764 | |
| 2203 | | ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count); |
| 1765 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 1766 | return ErrorSemanticAnalyzeFail; |
| 2204 | 1767 | |
| 2205 | | ZigType *import = get_scope_import(scope); |
| 2206 | | size_t debug_field_index = 0; |
| 2207 | | for (size_t i = 0; i < field_count; i += 1) { |
| 2208 | | AstNode *field_node = decl_node->data.container_decl.fields.at(i); |
| 2209 | | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 2210 | | size_t gen_field_index = type_struct_field->gen_index; |
| 2211 | | if (gen_field_index == SIZE_MAX) { |
| 2212 | | continue; |
| 1768 | this_field_align = field->type_entry->abi_align; |
| 2213 | 1769 | } |
| 2214 | 1770 | |
| 2215 | | ZigType *field_type = type_struct_field->type_entry; |
| 2216 | | |
| 2217 | | // if the field is a function, actually the debug info should be a pointer. |
| 2218 | | ZigLLVMDIType *field_di_type; |
| 2219 | | if (field_type->id == ZigTypeIdFn) { |
| 2220 | | ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true); |
| 2221 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_ptr_type->type_ref); |
| 2222 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_ptr_type->type_ref); |
| 2223 | | field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, field_type->di_type, |
| 2224 | | debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name)); |
| 2225 | | } else { |
| 2226 | | field_di_type = field_type->di_type; |
| 1771 | if (most_aligned_union_member == nullptr || |
| 1772 | this_field_align > most_aligned_union_member->abi_align) |
| 1773 | { |
| 1774 | most_aligned_union_member = field->type_entry; |
| 2227 | 1775 | } |
| 1776 | } |
| 2228 | 1777 | |
| 2229 | | assert(field_type->type_ref); |
| 2230 | | assert(struct_type->type_ref); |
| 2231 | | assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown); |
| 2232 | | uint64_t debug_size_in_bits; |
| 2233 | | uint64_t debug_align_in_bits; |
| 2234 | | uint64_t debug_offset_in_bits; |
| 2235 | | if (packed) { |
| 2236 | | debug_size_in_bits = type_size_bits(g, type_struct_field->type_entry); |
| 2237 | | debug_align_in_bits = 1; |
| 2238 | | debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref, |
| 2239 | | (unsigned)gen_field_index) + type_struct_field->bit_offset_in_host; |
| 1778 | // unset temporary flag |
| 1779 | union_type->data.unionation.resolve_loop_flag = false; |
| 1780 | union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown; |
| 1781 | union_type->data.unionation.most_aligned_union_member = most_aligned_union_member; |
| 1782 | |
| 1783 | ZigType *tag_type = union_type->data.unionation.tag_type; |
| 1784 | if (tag_type != nullptr && type_has_bits(tag_type)) { |
| 1785 | if ((err = type_resolve(g, tag_type, ResolveStatusAlignmentKnown))) { |
| 1786 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 1787 | return ErrorSemanticAnalyzeFail; |
| 1788 | } |
| 1789 | if (most_aligned_union_member == nullptr) { |
| 1790 | union_type->abi_align = tag_type->abi_align; |
| 1791 | union_type->data.unionation.gen_tag_index = SIZE_MAX; |
| 1792 | union_type->data.unionation.gen_union_index = SIZE_MAX; |
| 1793 | } else if (tag_type->abi_align > most_aligned_union_member->abi_align) { |
| 1794 | union_type->abi_align = tag_type->abi_align; |
| 1795 | union_type->data.unionation.gen_tag_index = 0; |
| 1796 | union_type->data.unionation.gen_union_index = 1; |
| 2240 | 1797 | } else { |
| 2241 | | debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref); |
| 2242 | | debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_type->type_ref); |
| 2243 | | debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref, |
| 2244 | | (unsigned)gen_field_index); |
| 1798 | union_type->abi_align = most_aligned_union_member->abi_align; |
| 1799 | union_type->data.unionation.gen_union_index = 0; |
| 1800 | union_type->data.unionation.gen_tag_index = 1; |
| 2245 | 1801 | } |
| 2246 | | di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 2247 | | ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name), |
| 2248 | | import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1), |
| 2249 | | debug_size_in_bits, |
| 2250 | | debug_align_in_bits, |
| 2251 | | debug_offset_in_bits, |
| 2252 | | 0, field_di_type); |
| 2253 | | assert(di_element_types[debug_field_index]); |
| 2254 | | debug_field_index += 1; |
| 1802 | } else { |
| 1803 | assert(most_aligned_union_member != nullptr); |
| 1804 | union_type->abi_align = most_aligned_union_member->abi_align; |
| 1805 | union_type->data.unionation.gen_union_index = SIZE_MAX; |
| 1806 | union_type->data.unionation.gen_tag_index = SIZE_MAX; |
| 2255 | 1807 | } |
| 2256 | 1808 | |
| 2257 | | |
| 2258 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref); |
| 2259 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref); |
| 2260 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 2261 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 2262 | | buf_ptr(&struct_type->name), |
| 2263 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2264 | | debug_size_in_bits, |
| 2265 | | debug_align_in_bits, |
| 2266 | | 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, ""); |
| 2267 | | |
| 2268 | | ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type); |
| 2269 | | struct_type->di_type = replacement_di_type; |
| 2270 | | |
| 2271 | 1809 | return ErrorNone; |
| 2272 | 1810 | } |
| 2273 | 1811 | |
| 2274 | 1812 | static Error resolve_union_type(CodeGen *g, ZigType *union_type) { |
| 2275 | 1813 | assert(union_type->id == ZigTypeIdUnion); |
| 2276 | 1814 | |
| 2277 | | if (union_type->data.unionation.complete) |
| 1815 | Error err; |
| 1816 | |
| 1817 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 1818 | return ErrorSemanticAnalyzeFail; |
| 1819 | if (union_type->data.unionation.resolve_status >= ResolveStatusSizeKnown) |
| 2278 | 1820 | return ErrorNone; |
| 2279 | 1821 | |
| 2280 | | Error err; |
| 2281 | | if ((err = resolve_union_zero_bits(g, union_type))) |
| 1822 | if ((err = resolve_union_alignment(g, union_type))) |
| 2282 | 1823 | return err; |
| 2283 | 1824 | |
| 2284 | 1825 | AstNode *decl_node = union_type->data.unionation.decl_node; |
| 2285 | 1826 | |
| 2286 | | if (union_type->data.unionation.embedded_in_current) { |
| 1827 | |
| 1828 | assert(decl_node->type == NodeTypeContainerDecl); |
| 1829 | |
| 1830 | uint32_t field_count = union_type->data.unionation.src_field_count; |
| 1831 | ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; |
| 1832 | |
| 1833 | assert(union_type->data.unionation.fields); |
| 1834 | |
| 1835 | size_t union_abi_size = 0; |
| 1836 | size_t union_size_in_bits = 0; |
| 1837 | |
| 1838 | if (union_type->data.unionation.resolve_loop_flag) { |
| 2287 | 1839 | if (!union_type->data.unionation.reported_infinite_err) { |
| 2288 | 1840 | union_type->data.unionation.reported_infinite_err = true; |
| 2289 | | union_type->data.unionation.is_invalid = true; |
| 1841 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2290 | 1842 | ErrorMsg *msg = add_node_error(g, decl_node, |
| 2291 | | buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name))); |
| 1843 | buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name))); |
| 2292 | 1844 | emit_error_notes_for_ref_stack(g, msg); |
| 2293 | 1845 | } |
| 2294 | 1846 | return ErrorSemanticAnalyzeFail; |
| 2295 | 1847 | } |
| 2296 | 1848 | |
| 2297 | | assert(!union_type->data.unionation.zero_bits_loop_flag); |
| 2298 | | assert(decl_node->type == NodeTypeContainerDecl); |
| 2299 | | assert(union_type->di_type); |
| 2300 | | |
| 2301 | | uint32_t field_count = union_type->data.unionation.src_field_count; |
| 1849 | // set temporary flag |
| 1850 | union_type->data.unionation.resolve_loop_flag = true; |
| 2302 | 1851 | |
| 2303 | | assert(union_type->data.unionation.fields); |
| 1852 | for (uint32_t i = 0; i < field_count; i += 1) { |
| 1853 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 1854 | ZigType *field_type = union_field->type_entry; |
| 2304 | 1855 | |
| 2305 | | uint32_t gen_field_count = union_type->data.unionation.gen_field_count; |
| 2306 | | ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count); |
| 2307 | | |
| 2308 | | ZigType *most_aligned_union_member = nullptr; |
| 2309 | | uint64_t size_of_most_aligned_member_in_bits = 0; |
| 2310 | | uint64_t biggest_align_in_bits = 0; |
| 2311 | | uint64_t biggest_size_in_bits = 0; |
| 2312 | | |
| 2313 | | Scope *scope = &union_type->data.unionation.decls_scope->base; |
| 2314 | | ZigType *import = get_scope_import(scope); |
| 2315 | | |
| 2316 | | // set temporary flag |
| 2317 | | union_type->data.unionation.embedded_in_current = true; |
| 2318 | | |
| 2319 | | |
| 2320 | | for (uint32_t i = 0; i < field_count; i += 1) { |
| 2321 | | AstNode *field_node = decl_node->data.container_decl.fields.at(i); |
| 2322 | | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 2323 | | ZigType *field_type = union_field->type_entry; |
| 2324 | | |
| 2325 | | if ((err = ensure_complete_type(g, field_type))) { |
| 2326 | | union_type->data.unionation.is_invalid = true; |
| 2327 | | continue; |
| 1856 | if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) { |
| 1857 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 1858 | return ErrorSemanticAnalyzeFail; |
| 2328 | 1859 | } |
| 2329 | 1860 | |
| 1861 | if (type_is_invalid(union_type)) |
| 1862 | return ErrorSemanticAnalyzeFail; |
| 1863 | |
| 2330 | 1864 | if (!type_has_bits(field_type)) |
| 2331 | 1865 | continue; |
| 2332 | 1866 | |
| 2333 | | uint64_t store_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref); |
| 2334 | | uint64_t abi_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref); |
| 2335 | | |
| 2336 | | assert(store_size_in_bits > 0); |
| 2337 | | assert(abi_align_in_bits > 0); |
| 2338 | | |
| 2339 | | union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 2340 | | ZigLLVMTypeToScope(union_type->di_type), buf_ptr(union_field->enum_field->name), |
| 2341 | | import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1), |
| 2342 | | store_size_in_bits, |
| 2343 | | abi_align_in_bits, |
| 2344 | | 0, |
| 2345 | | 0, field_type->di_type); |
| 2346 | | |
| 2347 | | biggest_size_in_bits = max(biggest_size_in_bits, store_size_in_bits); |
| 2348 | | |
| 2349 | | if (!most_aligned_union_member || abi_align_in_bits > biggest_align_in_bits) { |
| 2350 | | most_aligned_union_member = field_type; |
| 2351 | | biggest_align_in_bits = abi_align_in_bits; |
| 2352 | | size_of_most_aligned_member_in_bits = store_size_in_bits; |
| 2353 | | } |
| 1867 | union_abi_size = max(union_abi_size, field_type->abi_size); |
| 1868 | union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits); |
| 2354 | 1869 | } |
| 2355 | 1870 | |
| 2356 | | |
| 2357 | | // unset temporary flag |
| 2358 | | union_type->data.unionation.embedded_in_current = false; |
| 2359 | | union_type->data.unionation.complete = true; |
| 2360 | | union_type->data.unionation.union_size_bytes = biggest_size_in_bits / 8; |
| 2361 | | union_type->data.unionation.most_aligned_union_member = most_aligned_union_member; |
| 2362 | | |
| 2363 | | if (union_type->data.unionation.is_invalid) |
| 2364 | | return ErrorSemanticAnalyzeFail; |
| 2365 | | |
| 2366 | | if (union_type->zero_bits) { |
| 2367 | | union_type->type_ref = LLVMVoidType(); |
| 2368 | | |
| 2369 | | uint64_t debug_size_in_bits = 0; |
| 2370 | | uint64_t debug_align_in_bits = 0; |
| 2371 | | ZigLLVMDIType **di_root_members = nullptr; |
| 2372 | | size_t debug_member_count = 0; |
| 2373 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, |
| 2374 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 2375 | | buf_ptr(&union_type->name), |
| 2376 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2377 | | debug_size_in_bits, |
| 2378 | | debug_align_in_bits, |
| 2379 | | 0, di_root_members, (int)debug_member_count, 0, ""); |
| 2380 | | |
| 2381 | | ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type); |
| 2382 | | union_type->di_type = replacement_di_type; |
| 2383 | | return ErrorNone; |
| 1871 | // The union itself for now has to be treated as being independently aligned. |
| 1872 | // See https://github.com/ziglang/zig/issues/2166. |
| 1873 | if (most_aligned_union_member != nullptr) { |
| 1874 | union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align); |
| 2384 | 1875 | } |
| 2385 | 1876 | |
| 2386 | | uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits; |
| 1877 | // unset temporary flag |
| 1878 | union_type->data.unionation.resolve_loop_flag = false; |
| 1879 | union_type->data.unionation.resolve_status = ResolveStatusSizeKnown; |
| 1880 | union_type->data.unionation.union_abi_size = union_abi_size; |
| 2387 | 1881 | |
| 2388 | 1882 | ZigType *tag_type = union_type->data.unionation.tag_type; |
| 2389 | | if (tag_type == nullptr || tag_type->zero_bits) { |
| 2390 | | assert(most_aligned_union_member != nullptr); |
| 2391 | | |
| 2392 | | if (padding_in_bits > 0) { |
| 2393 | | ZigType *u8_type = get_int_type(g, false, 8); |
| 2394 | | ZigType *padding_array = get_array_type(g, u8_type, padding_in_bits / 8); |
| 2395 | | LLVMTypeRef union_element_types[] = { |
| 2396 | | most_aligned_union_member->type_ref, |
| 2397 | | padding_array->type_ref, |
| 2398 | | }; |
| 2399 | | LLVMStructSetBody(union_type->type_ref, union_element_types, 2, false); |
| 1883 | if (tag_type != nullptr && type_has_bits(tag_type)) { |
| 1884 | if ((err = type_resolve(g, tag_type, ResolveStatusSizeKnown))) { |
| 1885 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 1886 | return ErrorSemanticAnalyzeFail; |
| 1887 | } |
| 1888 | if (most_aligned_union_member == nullptr) { |
| 1889 | union_type->abi_size = tag_type->abi_size; |
| 1890 | union_type->size_in_bits = tag_type->size_in_bits; |
| 2400 | 1891 | } else { |
| 2401 | | LLVMStructSetBody(union_type->type_ref, &most_aligned_union_member->type_ref, 1, false); |
| 1892 | size_t field_sizes[2]; |
| 1893 | size_t field_aligns[2]; |
| 1894 | field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size; |
| 1895 | field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align; |
| 1896 | field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size; |
| 1897 | field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align; |
| 1898 | size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]); |
| 1899 | union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align); |
| 1900 | union_type->size_in_bits = union_type->abi_size * 8; |
| 2402 | 1901 | } |
| 2403 | | union_type->data.unionation.union_type_ref = union_type->type_ref; |
| 2404 | | union_type->data.unionation.gen_tag_index = SIZE_MAX; |
| 2405 | | union_type->data.unionation.gen_union_index = SIZE_MAX; |
| 2406 | | |
| 2407 | | assert(8*LLVMABIAlignmentOfType(g->target_data_ref, union_type->type_ref) >= biggest_align_in_bits); |
| 2408 | | assert(8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref) >= biggest_size_in_bits); |
| 2409 | | |
| 2410 | | // create debug type for union |
| 2411 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, |
| 2412 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name), |
| 2413 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2414 | | biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types, |
| 2415 | | gen_field_count, 0, ""); |
| 2416 | | |
| 2417 | | ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type); |
| 2418 | | union_type->di_type = replacement_di_type; |
| 2419 | | return ErrorNone; |
| 2420 | | } |
| 2421 | | |
| 2422 | | LLVMTypeRef union_type_ref; |
| 2423 | | if (padding_in_bits > 0) { |
| 2424 | | ZigType *u8_type = get_int_type(g, false, 8); |
| 2425 | | ZigType *padding_array = get_array_type(g, u8_type, padding_in_bits / 8); |
| 2426 | | LLVMTypeRef union_element_types[] = { |
| 2427 | | most_aligned_union_member->type_ref, |
| 2428 | | padding_array->type_ref, |
| 2429 | | }; |
| 2430 | | union_type_ref = LLVMStructType(union_element_types, 2, false); |
| 2431 | | } else if (most_aligned_union_member == nullptr) { |
| 2432 | | union_type->data.unionation.gen_tag_index = SIZE_MAX; |
| 2433 | | union_type->data.unionation.gen_union_index = SIZE_MAX; |
| 2434 | | union_type->type_ref = tag_type->type_ref; |
| 2435 | | |
| 2436 | | ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, tag_type->di_type); |
| 2437 | | union_type->di_type = tag_type->di_type; |
| 2438 | | return ErrorNone; |
| 2439 | | } else { |
| 2440 | | union_type_ref = most_aligned_union_member->type_ref; |
| 2441 | | } |
| 2442 | | union_type->data.unionation.union_type_ref = union_type_ref; |
| 2443 | | |
| 2444 | | assert(8*LLVMABIAlignmentOfType(g->target_data_ref, union_type_ref) >= biggest_align_in_bits); |
| 2445 | | assert(8*LLVMStoreSizeOfType(g->target_data_ref, union_type_ref) >= biggest_size_in_bits); |
| 2446 | | |
| 2447 | | // create llvm type for root struct |
| 2448 | | ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type; |
| 2449 | | uint64_t align_of_tag_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref); |
| 2450 | | |
| 2451 | | if (align_of_tag_in_bits >= biggest_align_in_bits) { |
| 2452 | | union_type->data.unionation.gen_tag_index = 0; |
| 2453 | | union_type->data.unionation.gen_union_index = 1; |
| 2454 | 1902 | } else { |
| 2455 | | union_type->data.unionation.gen_union_index = 0; |
| 2456 | | union_type->data.unionation.gen_tag_index = 1; |
| 1903 | union_type->abi_size = union_abi_size; |
| 1904 | union_type->size_in_bits = union_size_in_bits; |
| 2457 | 1905 | } |
| 2458 | 1906 | |
| 2459 | | LLVMTypeRef root_struct_element_types[2]; |
| 2460 | | root_struct_element_types[union_type->data.unionation.gen_tag_index] = tag_type->type_ref; |
| 2461 | | root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref; |
| 2462 | | LLVMStructSetBody(union_type->type_ref, root_struct_element_types, 2, false); |
| 2463 | | |
| 2464 | | |
| 2465 | | // create debug type for union |
| 2466 | | ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, |
| 2467 | | ZigLLVMTypeToScope(union_type->di_type), "AnonUnion", |
| 2468 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2469 | | biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types, |
| 2470 | | gen_field_count, 0, ""); |
| 2471 | | |
| 2472 | | uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref, |
| 2473 | | union_type->data.unionation.gen_union_index); |
| 2474 | | uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref, |
| 2475 | | union_type->data.unionation.gen_tag_index); |
| 2476 | | |
| 2477 | | ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 2478 | | ZigLLVMTypeToScope(union_type->di_type), "payload", |
| 2479 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2480 | | biggest_size_in_bits, |
| 2481 | | biggest_align_in_bits, |
| 2482 | | union_offset_in_bits, |
| 2483 | | 0, union_di_type); |
| 2484 | | |
| 2485 | | uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref); |
| 2486 | | uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref); |
| 2487 | | |
| 2488 | | ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 2489 | | ZigLLVMTypeToScope(union_type->di_type), "tag", |
| 2490 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2491 | | tag_debug_size_in_bits, |
| 2492 | | tag_debug_align_in_bits, |
| 2493 | | tag_offset_in_bits, |
| 2494 | | 0, tag_type->di_type); |
| 2495 | | |
| 2496 | | ZigLLVMDIType *di_root_members[2]; |
| 2497 | | di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type; |
| 2498 | | di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type; |
| 2499 | | |
| 2500 | | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref); |
| 2501 | | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, union_type->type_ref); |
| 2502 | | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 2503 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 2504 | | buf_ptr(&union_type->name), |
| 2505 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 2506 | | debug_size_in_bits, |
| 2507 | | debug_align_in_bits, |
| 2508 | | 0, nullptr, di_root_members, 2, 0, nullptr, ""); |
| 2509 | | |
| 2510 | | ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type); |
| 2511 | | union_type->di_type = replacement_di_type; |
| 2512 | | |
| 2513 | 1907 | return ErrorNone; |
| 2514 | 1908 | } |
| 2515 | 1909 | |
| 2516 | 1910 | static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2517 | 1911 | assert(enum_type->id == ZigTypeIdEnum); |
| 2518 | 1912 | |
| 1913 | if (enum_type->data.enumeration.is_invalid) |
| 1914 | return ErrorSemanticAnalyzeFail; |
| 1915 | |
| 2519 | 1916 | if (enum_type->data.enumeration.zero_bits_known) |
| 2520 | 1917 | return ErrorNone; |
| 2521 | 1918 | |
| ... | ... | @@ -2531,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2531 | 1928 | |
| 2532 | 1929 | AstNode *decl_node = enum_type->data.enumeration.decl_node; |
| 2533 | 1930 | assert(decl_node->type == NodeTypeContainerDecl); |
| 2534 | | assert(enum_type->di_type); |
| 2535 | 1931 | |
| 2536 | 1932 | assert(!enum_type->data.enumeration.fields); |
| 2537 | 1933 | uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length; |
| ... | ... | @@ -2564,6 +1960,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2564 | 1960 | tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1); |
| 2565 | 1961 | } |
| 2566 | 1962 | |
| 1963 | enum_type->size_in_bits = tag_int_type->size_in_bits; |
| 1964 | enum_type->abi_size = tag_int_type->abi_size; |
| 1965 | enum_type->abi_align = tag_int_type->abi_align; |
| 1966 | |
| 2567 | 1967 | // TODO: Are extern enums allowed to have an init_arg_expr? |
| 2568 | 1968 | if (decl_node->data.container_decl.init_arg_expr != nullptr) { |
| 2569 | 1969 | ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr); |
| ... | ... | @@ -2587,7 +1987,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2587 | 1987 | } |
| 2588 | 1988 | } |
| 2589 | 1989 | enum_type->data.enumeration.tag_int_type = tag_int_type; |
| 2590 | | enum_type->type_ref = tag_int_type->type_ref; |
| 1990 | enum_type->size_in_bits = tag_int_type->size_in_bits; |
| 1991 | enum_type->abi_size = tag_int_type->abi_size; |
| 1992 | enum_type->abi_align = tag_int_type->abi_align; |
| 2591 | 1993 | |
| 2592 | 1994 | for (uint32_t field_i = 0; field_i < field_count; field_i += 1) { |
| 2593 | 1995 | AstNode *field_node = decl_node->data.container_decl.fields.at(field_i); |
| ... | ... | @@ -2671,8 +2073,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2671 | 2073 | } |
| 2672 | 2074 | |
| 2673 | 2075 | enum_type->data.enumeration.zero_bits_loop_flag = false; |
| 2674 | | enum_type->zero_bits = !type_has_bits(tag_int_type); |
| 2675 | 2076 | enum_type->data.enumeration.zero_bits_known = true; |
| 2077 | enum_type->data.enumeration.complete = true; |
| 2676 | 2078 | |
| 2677 | 2079 | if (enum_type->data.enumeration.is_invalid) |
| 2678 | 2080 | return ErrorSemanticAnalyzeFail; |
| ... | ... | @@ -2683,12 +2085,20 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2683 | 2085 | static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2684 | 2086 | assert(struct_type->id == ZigTypeIdStruct); |
| 2685 | 2087 | |
| 2088 | Error err; |
| 2089 | |
| 2686 | 2090 | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) |
| 2687 | 2091 | return ErrorSemanticAnalyzeFail; |
| 2688 | 2092 | if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown) |
| 2689 | 2093 | return ErrorNone; |
| 2690 | 2094 | |
| 2095 | AstNode *decl_node = struct_type->data.structure.decl_node; |
| 2096 | assert(decl_node->type == NodeTypeContainerDecl); |
| 2097 | |
| 2691 | 2098 | if (struct_type->data.structure.resolve_loop_flag) { |
| 2099 | // TODO This is a problem. I believe it can be solved with lazy values. |
| 2100 | struct_type->size_in_bits = SIZE_MAX; |
| 2101 | struct_type->abi_size = SIZE_MAX; |
| 2692 | 2102 | struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown; |
| 2693 | 2103 | struct_type->data.structure.resolve_loop_flag = false; |
| 2694 | 2104 | return ErrorNone; |
| ... | ... | @@ -2696,10 +2106,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2696 | 2106 | |
| 2697 | 2107 | struct_type->data.structure.resolve_loop_flag = true; |
| 2698 | 2108 | |
| 2699 | | AstNode *decl_node = struct_type->data.structure.decl_node; |
| 2700 | | assert(decl_node->type == NodeTypeContainerDecl); |
| 2701 | | assert(struct_type->di_type); |
| 2702 | | |
| 2703 | 2109 | assert(!struct_type->data.structure.fields); |
| 2704 | 2110 | size_t field_count = decl_node->data.container_decl.fields.length; |
| 2705 | 2111 | struct_type->data.structure.src_field_count = (uint32_t)field_count; |
| ... | ... | @@ -2718,7 +2124,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2718 | 2124 | if (field_node->data.struct_field.type == nullptr) { |
| 2719 | 2125 | add_node_error(g, field_node, buf_sprintf("struct field missing type")); |
| 2720 | 2126 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2721 | | continue; |
| 2127 | return ErrorSemanticAnalyzeFail; |
| 2722 | 2128 | } |
| 2723 | 2129 | |
| 2724 | 2130 | auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field); |
| ... | ... | @@ -2727,11 +2133,33 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2727 | 2133 | buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name))); |
| 2728 | 2134 | add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); |
| 2729 | 2135 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2730 | | continue; |
| 2136 | return ErrorSemanticAnalyzeFail; |
| 2731 | 2137 | } |
| 2732 | 2138 | |
| 2733 | 2139 | ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); |
| 2734 | 2140 | type_struct_field->type_entry = field_type; |
| 2141 | if (type_is_invalid(field_type)) { |
| 2142 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2143 | return ErrorSemanticAnalyzeFail; |
| 2144 | } |
| 2145 | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) |
| 2146 | return ErrorSemanticAnalyzeFail; |
| 2147 | |
| 2148 | if (struct_type->data.structure.layout == ContainerLayoutExtern && |
| 2149 | !type_allowed_in_extern(g, field_type)) |
| 2150 | { |
| 2151 | add_node_error(g, field_node, |
| 2152 | buf_sprintf("extern structs cannot contain fields of type '%s'", |
| 2153 | buf_ptr(&field_type->name))); |
| 2154 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2155 | return ErrorSemanticAnalyzeFail; |
| 2156 | } else if (struct_type->data.structure.layout == ContainerLayoutPacked) { |
| 2157 | if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) { |
| 2158 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2159 | return ErrorSemanticAnalyzeFail; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2735 | 2163 | type_struct_field->src_index = i; |
| 2736 | 2164 | type_struct_field->gen_index = SIZE_MAX; |
| 2737 | 2165 | |
| ... | ... | @@ -2739,21 +2167,19 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2739 | 2167 | add_node_error(g, field_node->data.struct_field.value, |
| 2740 | 2168 | buf_sprintf("enums, not structs, support field assignment")); |
| 2741 | 2169 | } |
| 2742 | | |
| 2743 | 2170 | if (field_type->id == ZigTypeIdOpaque) { |
| 2744 | 2171 | add_node_error(g, field_node->data.struct_field.type, |
| 2745 | 2172 | buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs")); |
| 2746 | 2173 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2747 | | continue; |
| 2174 | return ErrorSemanticAnalyzeFail; |
| 2748 | 2175 | } |
| 2749 | | |
| 2750 | 2176 | switch (type_requires_comptime(g, field_type)) { |
| 2751 | 2177 | case ReqCompTimeYes: |
| 2752 | 2178 | struct_type->data.structure.requires_comptime = true; |
| 2753 | 2179 | break; |
| 2754 | 2180 | case ReqCompTimeInvalid: |
| 2755 | 2181 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2756 | | continue; |
| 2182 | return ErrorSemanticAnalyzeFail; |
| 2757 | 2183 | case ReqCompTimeNo: |
| 2758 | 2184 | break; |
| 2759 | 2185 | } |
| ... | ... | @@ -2767,7 +2193,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2767 | 2193 | |
| 2768 | 2194 | struct_type->data.structure.resolve_loop_flag = false; |
| 2769 | 2195 | struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index; |
| 2770 | | struct_type->zero_bits = (gen_field_index == 0); |
| 2196 | if (gen_field_index != 0) { |
| 2197 | struct_type->abi_size = SIZE_MAX; |
| 2198 | struct_type->size_in_bits = SIZE_MAX; |
| 2199 | } |
| 2771 | 2200 | |
| 2772 | 2201 | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) |
| 2773 | 2202 | return ErrorSemanticAnalyzeFail; |
| ... | ... | @@ -2785,9 +2214,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { |
| 2785 | 2214 | return ErrorSemanticAnalyzeFail; |
| 2786 | 2215 | if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown) |
| 2787 | 2216 | return ErrorNone; |
| 2788 | | |
| 2789 | 2217 | if ((err = resolve_struct_zero_bits(g, struct_type))) |
| 2790 | 2218 | return err; |
| 2219 | if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown) |
| 2220 | return ErrorNone; |
| 2791 | 2221 | |
| 2792 | 2222 | AstNode *decl_node = struct_type->data.structure.decl_node; |
| 2793 | 2223 | |
| ... | ... | @@ -2803,22 +2233,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { |
| 2803 | 2233 | |
| 2804 | 2234 | struct_type->data.structure.resolve_loop_flag = true; |
| 2805 | 2235 | assert(decl_node->type == NodeTypeContainerDecl); |
| 2806 | | assert(struct_type->di_type); |
| 2807 | 2236 | |
| 2808 | 2237 | size_t field_count = struct_type->data.structure.src_field_count; |
| 2809 | | if (struct_type->data.structure.layout == ContainerLayoutPacked) { |
| 2810 | | struct_type->data.structure.abi_alignment = 1; |
| 2811 | | for (size_t i = 0; i < field_count; i += 1) { |
| 2812 | | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 2813 | | if (field->type_entry != nullptr && type_is_invalid(field->type_entry)) { |
| 2814 | | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2815 | | break; |
| 2816 | | } |
| 2817 | | } |
| 2818 | | } else for (size_t i = 0; i < field_count; i += 1) { |
| 2238 | bool packed = struct_type->data.structure.layout == ContainerLayoutPacked; |
| 2239 | |
| 2240 | for (size_t i = 0; i < field_count; i += 1) { |
| 2819 | 2241 | TypeStructField *field = &struct_type->data.structure.fields[i]; |
| 2820 | | uint32_t this_field_align; |
| 2242 | if (field->gen_index == SIZE_MAX) |
| 2243 | continue; |
| 2821 | 2244 | |
| 2245 | size_t this_field_align; |
| 2246 | if (packed) { |
| 2247 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 2248 | this_field_align = 1; |
| 2822 | 2249 | // TODO If we have no type_entry for the field, we've already failed to |
| 2823 | 2250 | // compile the program correctly. This stage1 compiler needs a deeper |
| 2824 | 2251 | // reworking to make this correct, or we can ignore the problem |
| ... | ... | @@ -2827,28 +2254,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { |
| 2827 | 2254 | // on itself. When this false positive happens we assume a pointer-aligned |
| 2828 | 2255 | // field, which is usually fine but could be incorrectly over-aligned or |
| 2829 | 2256 | // even under-aligned. See https://github.com/ziglang/zig/issues/1512 |
| 2830 | | if (field->type_entry == nullptr) { |
| 2831 | | this_field_align = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0)); |
| 2257 | } else if (field->type_entry == nullptr) { |
| 2258 | this_field_align = g->builtin_types.entry_usize->abi_align; |
| 2832 | 2259 | } else { |
| 2833 | | if (type_is_invalid(field->type_entry)) { |
| 2834 | | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2835 | | break; |
| 2836 | | } |
| 2837 | | |
| 2838 | | if (!type_has_bits(field->type_entry)) |
| 2839 | | continue; |
| 2840 | | |
| 2841 | 2260 | if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) { |
| 2842 | 2261 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2843 | | break; |
| 2262 | return ErrorSemanticAnalyzeFail; |
| 2844 | 2263 | } |
| 2845 | | |
| 2846 | | this_field_align = get_abi_alignment(g, field->type_entry); |
| 2847 | | assert(this_field_align != 0); |
| 2264 | this_field_align = field->type_entry->abi_align; |
| 2848 | 2265 | } |
| 2849 | | // alignment of structs is the alignment of the most-aligned field |
| 2850 | | if (this_field_align > struct_type->data.structure.abi_alignment) { |
| 2851 | | struct_type->data.structure.abi_alignment = this_field_align; |
| 2266 | |
| 2267 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 2268 | if (this_field_align > struct_type->abi_align) { |
| 2269 | struct_type->abi_align = this_field_align; |
| 2852 | 2270 | } |
| 2853 | 2271 | } |
| 2854 | 2272 | |
| ... | ... | @@ -2867,57 +2285,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2867 | 2285 | |
| 2868 | 2286 | Error err; |
| 2869 | 2287 | |
| 2870 | | if (union_type->data.unionation.is_invalid) |
| 2288 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 2871 | 2289 | return ErrorSemanticAnalyzeFail; |
| 2872 | 2290 | |
| 2873 | | if (union_type->data.unionation.zero_bits_known) |
| 2291 | if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown) |
| 2874 | 2292 | return ErrorNone; |
| 2875 | 2293 | |
| 2876 | | if (type_is_invalid(union_type)) |
| 2877 | | return ErrorSemanticAnalyzeFail; |
| 2878 | | |
| 2879 | | if (union_type->data.unionation.zero_bits_loop_flag) { |
| 2294 | if (union_type->data.unionation.resolve_loop_flag) { |
| 2880 | 2295 | // If we get here it's due to recursion. From this we conclude that the struct is |
| 2881 | | // not zero bits, and if abi_alignment == 0 we further conclude that the first field |
| 2882 | | // is a pointer to this very struct, or a function pointer with parameters that |
| 2883 | | // reference such a type. |
| 2884 | | union_type->data.unionation.zero_bits_known = true; |
| 2885 | | union_type->data.unionation.zero_bits_loop_flag = false; |
| 2886 | | if (union_type->data.unionation.abi_alignment == 0) { |
| 2887 | | if (union_type->data.unionation.layout == ContainerLayoutPacked) { |
| 2888 | | union_type->data.unionation.abi_alignment = 1; |
| 2889 | | } else { |
| 2890 | | union_type->data.unionation.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, |
| 2891 | | LLVMPointerType(LLVMInt8Type(), 0)); |
| 2892 | | } |
| 2893 | | } |
| 2296 | // not zero bits. |
| 2297 | // TODO actually it could still be zero bits. Here we should continue analyzing |
| 2298 | // the union from the next field index. |
| 2299 | union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown; |
| 2300 | union_type->data.unionation.resolve_loop_flag = false; |
| 2301 | union_type->abi_size = SIZE_MAX; |
| 2302 | union_type->size_in_bits = SIZE_MAX; |
| 2894 | 2303 | return ErrorNone; |
| 2895 | 2304 | } |
| 2896 | 2305 | |
| 2897 | | union_type->data.unionation.zero_bits_loop_flag = true; |
| 2306 | union_type->data.unionation.resolve_loop_flag = true; |
| 2898 | 2307 | |
| 2899 | 2308 | AstNode *decl_node = union_type->data.unionation.decl_node; |
| 2900 | 2309 | assert(decl_node->type == NodeTypeContainerDecl); |
| 2901 | | assert(union_type->di_type); |
| 2902 | 2310 | |
| 2903 | | assert(!union_type->data.unionation.fields); |
| 2311 | assert(union_type->data.unionation.fields == nullptr); |
| 2904 | 2312 | uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length; |
| 2905 | 2313 | if (field_count == 0) { |
| 2906 | 2314 | add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields")); |
| 2907 | | |
| 2908 | 2315 | union_type->data.unionation.src_field_count = field_count; |
| 2909 | | union_type->data.unionation.fields = nullptr; |
| 2910 | | union_type->data.unionation.is_invalid = true; |
| 2911 | | union_type->data.unionation.zero_bits_loop_flag = false; |
| 2912 | | union_type->data.unionation.zero_bits_known = true; |
| 2316 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2913 | 2317 | return ErrorSemanticAnalyzeFail; |
| 2914 | 2318 | } |
| 2915 | 2319 | union_type->data.unionation.src_field_count = field_count; |
| 2916 | 2320 | union_type->data.unionation.fields = allocate<TypeUnionField>(field_count); |
| 2917 | 2321 | union_type->data.unionation.fields_by_name.init(field_count); |
| 2918 | 2322 | |
| 2919 | | uint32_t biggest_align_bytes = 0; |
| 2920 | | |
| 2921 | 2323 | Scope *scope = &union_type->data.unionation.decls_scope->base; |
| 2922 | 2324 | |
| 2923 | 2325 | HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {}; |
| ... | ... | @@ -2931,7 +2333,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2931 | 2333 | bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety); |
| 2932 | 2334 | bool *covered_enum_fields; |
| 2933 | 2335 | ZigLLVMDIEnumerator **di_enumerators; |
| 2934 | | uint32_t abi_alignment_so_far; |
| 2935 | 2336 | if (create_enum_type) { |
| 2936 | 2337 | occupied_tag_values.init(field_count); |
| 2937 | 2338 | |
| ... | ... | @@ -2941,13 +2342,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2941 | 2342 | if (enum_type_node != nullptr) { |
| 2942 | 2343 | tag_int_type = analyze_type_expr(g, scope, enum_type_node); |
| 2943 | 2344 | if (type_is_invalid(tag_int_type)) { |
| 2944 | | union_type->data.unionation.is_invalid = true; |
| 2345 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2945 | 2346 | return ErrorSemanticAnalyzeFail; |
| 2946 | 2347 | } |
| 2947 | 2348 | if (tag_int_type->id != ZigTypeIdInt) { |
| 2948 | 2349 | add_node_error(g, enum_type_node, |
| 2949 | 2350 | buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name))); |
| 2950 | | union_type->data.unionation.is_invalid = true; |
| 2351 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2951 | 2352 | return ErrorSemanticAnalyzeFail; |
| 2952 | 2353 | } |
| 2953 | 2354 | } else if (auto_layout && field_count == 1) { |
| ... | ... | @@ -2955,13 +2356,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2955 | 2356 | } else { |
| 2956 | 2357 | tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1); |
| 2957 | 2358 | } |
| 2958 | | abi_alignment_so_far = get_abi_alignment(g, tag_int_type); |
| 2959 | 2359 | |
| 2960 | 2360 | tag_type = new_type_table_entry(ZigTypeIdEnum); |
| 2961 | 2361 | buf_resize(&tag_type->name, 0); |
| 2962 | 2362 | buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name)); |
| 2963 | | tag_type->type_ref = tag_int_type->type_ref; |
| 2964 | | tag_type->zero_bits = tag_int_type->zero_bits; |
| 2363 | tag_type->llvm_type = tag_int_type->llvm_type; |
| 2364 | tag_type->llvm_di_type = tag_int_type->llvm_di_type; |
| 2365 | tag_type->abi_size = tag_int_type->abi_size; |
| 2366 | tag_type->abi_align = tag_int_type->abi_align; |
| 2367 | tag_type->size_in_bits = tag_int_type->size_in_bits; |
| 2965 | 2368 | |
| 2966 | 2369 | tag_type->data.enumeration.tag_int_type = tag_int_type; |
| 2967 | 2370 | tag_type->data.enumeration.zero_bits_known = true; |
| ... | ... | @@ -2975,11 +2378,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2975 | 2378 | } else if (enum_type_node != nullptr) { |
| 2976 | 2379 | ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node); |
| 2977 | 2380 | if (type_is_invalid(enum_type)) { |
| 2978 | | union_type->data.unionation.is_invalid = true; |
| 2381 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2979 | 2382 | return ErrorSemanticAnalyzeFail; |
| 2980 | 2383 | } |
| 2981 | 2384 | if (enum_type->id != ZigTypeIdEnum) { |
| 2982 | | union_type->data.unionation.is_invalid = true; |
| 2385 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2983 | 2386 | add_node_error(g, enum_type_node, |
| 2984 | 2387 | buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name))); |
| 2985 | 2388 | return ErrorSemanticAnalyzeFail; |
| ... | ... | @@ -2989,11 +2392,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2989 | 2392 | return err; |
| 2990 | 2393 | } |
| 2991 | 2394 | tag_type = enum_type; |
| 2992 | | abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count |
| 2993 | 2395 | covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count); |
| 2994 | 2396 | } else { |
| 2995 | 2397 | tag_type = nullptr; |
| 2996 | | abi_alignment_so_far = 0; |
| 2997 | 2398 | } |
| 2998 | 2399 | union_type->data.unionation.tag_type = tag_type; |
| 2999 | 2400 | |
| ... | ... | @@ -3004,14 +2405,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3004 | 2405 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 3005 | 2406 | union_field->name = field_node->data.struct_field.name; |
| 3006 | 2407 | union_field->decl_node = field_node; |
| 2408 | union_field->gen_index = UINT32_MAX; |
| 3007 | 2409 | |
| 3008 | 2410 | auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field); |
| 3009 | 2411 | if (field_entry != nullptr) { |
| 3010 | 2412 | ErrorMsg *msg = add_node_error(g, field_node, |
| 3011 | 2413 | buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name))); |
| 3012 | 2414 | add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); |
| 3013 | | union_type->data.unionation.is_invalid = true; |
| 3014 | | continue; |
| 2415 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2416 | return ErrorSemanticAnalyzeFail; |
| 3015 | 2417 | } |
| 3016 | 2418 | |
| 3017 | 2419 | ZigType *field_type; |
| ... | ... | @@ -3020,29 +2422,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3020 | 2422 | field_type = g->builtin_types.entry_void; |
| 3021 | 2423 | } else { |
| 3022 | 2424 | add_node_error(g, field_node, buf_sprintf("union field missing type")); |
| 3023 | | union_type->data.unionation.is_invalid = true; |
| 3024 | | continue; |
| 2425 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2426 | return ErrorSemanticAnalyzeFail; |
| 3025 | 2427 | } |
| 3026 | 2428 | } else { |
| 3027 | 2429 | field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); |
| 3028 | 2430 | if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) { |
| 3029 | | union_type->data.unionation.is_invalid = true; |
| 3030 | | continue; |
| 2431 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2432 | return ErrorSemanticAnalyzeFail; |
| 3031 | 2433 | } |
| 2434 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 2435 | return ErrorSemanticAnalyzeFail; |
| 3032 | 2436 | } |
| 3033 | 2437 | union_field->type_entry = field_type; |
| 3034 | 2438 | |
| 3035 | 2439 | if (field_type->id == ZigTypeIdOpaque) { |
| 3036 | 2440 | add_node_error(g, field_node->data.struct_field.type, |
| 3037 | 2441 | buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions")); |
| 3038 | | union_type->data.unionation.is_invalid = true; |
| 3039 | | continue; |
| 2442 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2443 | return ErrorSemanticAnalyzeFail; |
| 3040 | 2444 | } |
| 3041 | 2445 | |
| 3042 | 2446 | switch (type_requires_comptime(g, field_type)) { |
| 3043 | 2447 | case ReqCompTimeInvalid: |
| 3044 | | union_type->data.unionation.is_invalid = true; |
| 3045 | | continue; |
| 2448 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2449 | return ErrorSemanticAnalyzeFail; |
| 3046 | 2450 | case ReqCompTimeYes: |
| 3047 | 2451 | union_type->data.unionation.requires_comptime = true; |
| 3048 | 2452 | break; |
| ... | ... | @@ -3074,8 +2478,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3074 | 2478 | ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type; |
| 3075 | 2479 | ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr); |
| 3076 | 2480 | if (type_is_invalid(result->type)) { |
| 3077 | | union_type->data.unionation.is_invalid = true; |
| 3078 | | continue; |
| 2481 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2482 | return ErrorSemanticAnalyzeFail; |
| 3079 | 2483 | } |
| 3080 | 2484 | assert(result->special != ConstValSpecialRuntime); |
| 3081 | 2485 | assert(result->type->id == ZigTypeIdInt); |
| ... | ... | @@ -3090,8 +2494,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3090 | 2494 | buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf))); |
| 3091 | 2495 | add_error_note(g, msg, entry->value, |
| 3092 | 2496 | buf_sprintf("other occurrence here")); |
| 3093 | | union_type->data.unionation.is_invalid = true; |
| 3094 | | continue; |
| 2497 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2498 | return ErrorSemanticAnalyzeFail; |
| 3095 | 2499 | } |
| 3096 | 2500 | } |
| 3097 | 2501 | } else if (enum_type_node != nullptr) { |
| ... | ... | @@ -3101,8 +2505,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3101 | 2505 | buf_sprintf("enum field not found: '%s'", buf_ptr(field_name))); |
| 3102 | 2506 | add_error_note(g, msg, tag_type->data.enumeration.decl_node, |
| 3103 | 2507 | buf_sprintf("enum declared here")); |
| 3104 | | union_type->data.unionation.is_invalid = true; |
| 3105 | | continue; |
| 2508 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 2509 | return ErrorSemanticAnalyzeFail; |
| 3106 | 2510 | } |
| 3107 | 2511 | covered_enum_fields[union_field->enum_field->decl_index] = true; |
| 3108 | 2512 | } else { |
| ... | ... | @@ -3118,21 +2522,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3118 | 2522 | |
| 3119 | 2523 | union_field->gen_index = gen_field_index; |
| 3120 | 2524 | gen_field_index += 1; |
| 3121 | | |
| 3122 | | uint32_t field_align_bytes = get_abi_alignment(g, field_type); |
| 3123 | | if (field_align_bytes > biggest_align_bytes) { |
| 3124 | | biggest_align_bytes = field_align_bytes; |
| 3125 | | if (biggest_align_bytes > abi_alignment_so_far) { |
| 3126 | | abi_alignment_so_far = biggest_align_bytes; |
| 3127 | | } |
| 3128 | | } |
| 3129 | 2525 | } |
| 3130 | 2526 | |
| 3131 | | union_type->data.unionation.abi_alignment = abi_alignment_so_far; |
| 3132 | | |
| 3133 | | if (union_type->data.unionation.is_invalid) |
| 3134 | | return ErrorSemanticAnalyzeFail; |
| 3135 | | |
| 3136 | 2527 | bool src_have_tag = decl_node->data.container_decl.auto_enum || |
| 3137 | 2528 | decl_node->data.container_decl.init_arg_expr != nullptr; |
| 3138 | 2529 | |
| ... | ... | @@ -3152,7 +2543,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3152 | 2543 | decl_node->data.container_decl.init_arg_expr : decl_node; |
| 3153 | 2544 | add_node_error(g, source_node, |
| 3154 | 2545 | buf_sprintf("%s union does not support enum tag type", qual_str)); |
| 3155 | | union_type->data.unionation.is_invalid = true; |
| 2546 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3156 | 2547 | return ErrorSemanticAnalyzeFail; |
| 3157 | 2548 | } |
| 3158 | 2549 | |
| ... | ... | @@ -3194,33 +2585,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3194 | 2585 | buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name))); |
| 3195 | 2586 | add_error_note(g, msg, field_node, |
| 3196 | 2587 | buf_sprintf("declared here")); |
| 3197 | | union_type->data.unionation.is_invalid = true; |
| 2588 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3198 | 2589 | } |
| 3199 | 2590 | } |
| 3200 | 2591 | } |
| 3201 | 2592 | |
| 3202 | | if (create_enum_type) { |
| 3203 | | ZigType *import = get_scope_import(scope); |
| 3204 | | uint64_t tag_debug_size_in_bits = tag_type->zero_bits ? 0 : |
| 3205 | | 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref); |
| 3206 | | uint64_t tag_debug_align_in_bits = tag_type->zero_bits ? 0 : |
| 3207 | | 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref); |
| 3208 | | // TODO get a more accurate debug scope |
| 3209 | | ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder, |
| 3210 | | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&tag_type->name), |
| 3211 | | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 3212 | | tag_debug_size_in_bits, tag_debug_align_in_bits, di_enumerators, field_count, |
| 3213 | | tag_type->di_type, ""); |
| 3214 | | tag_type->di_type = tag_di_type; |
| 2593 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) { |
| 2594 | return ErrorSemanticAnalyzeFail; |
| 3215 | 2595 | } |
| 3216 | 2596 | |
| 3217 | | union_type->data.unionation.zero_bits_loop_flag = false; |
| 3218 | | union_type->data.unionation.gen_field_count = gen_field_index; |
| 3219 | | union_type->zero_bits = (gen_field_index == 0 && (field_count < 2 || !src_have_tag)); |
| 3220 | | union_type->data.unionation.zero_bits_known = true; |
| 2597 | union_type->data.unionation.resolve_loop_flag = false; |
| 3221 | 2598 | |
| 3222 | | if (union_type->data.unionation.is_invalid) |
| 3223 | | return ErrorSemanticAnalyzeFail; |
| 2599 | union_type->data.unionation.gen_field_count = gen_field_index; |
| 2600 | bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag); |
| 2601 | if (!zero_bits) { |
| 2602 | union_type->abi_size = SIZE_MAX; |
| 2603 | union_type->size_in_bits = SIZE_MAX; |
| 2604 | } |
| 2605 | union_type->data.unionation.resolve_status = zero_bits ? ResolveStatusSizeKnown : ResolveStatusZeroBitsKnown; |
| 3224 | 2606 | |
| 3225 | 2607 | return ErrorNone; |
| 3226 | 2608 | } |
| ... | ... | @@ -3630,20 +3012,17 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) { |
| 3630 | 3012 | } |
| 3631 | 3013 | } |
| 3632 | 3014 | |
| 3633 | | static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) { |
| 3015 | static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) { |
| 3634 | 3016 | ZigType *type_entry = tld_container->type_entry; |
| 3635 | 3017 | assert(type_entry); |
| 3636 | 3018 | |
| 3637 | 3019 | switch (type_entry->id) { |
| 3638 | 3020 | case ZigTypeIdStruct: |
| 3639 | | resolve_struct_type(g, tld_container->type_entry); |
| 3640 | | return; |
| 3021 | return resolve_struct_type(g, tld_container->type_entry); |
| 3641 | 3022 | case ZigTypeIdEnum: |
| 3642 | | resolve_enum_type(g, tld_container->type_entry); |
| 3643 | | return; |
| 3023 | return resolve_enum_zero_bits(g, tld_container->type_entry); |
| 3644 | 3024 | case ZigTypeIdUnion: |
| 3645 | | resolve_union_type(g, tld_container->type_entry); |
| 3646 | | return; |
| 3025 | return resolve_union_type(g, tld_container->type_entry); |
| 3647 | 3026 | default: |
| 3648 | 3027 | zig_unreachable(); |
| 3649 | 3028 | } |
| ... | ... | @@ -4007,7 +3386,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) { |
| 4007 | 3386 | |
| 4008 | 3387 | TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) { |
| 4009 | 3388 | assert(type_entry->id == ZigTypeIdUnion); |
| 4010 | | assert(type_entry->data.unionation.zero_bits_known); |
| 3389 | assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown)); |
| 4011 | 3390 | if (type_entry->data.unionation.src_field_count == 0) |
| 4012 | 3391 | return nullptr; |
| 4013 | 3392 | auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name); |
| ... | ... | @@ -4018,7 +3397,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) { |
| 4018 | 3397 | |
| 4019 | 3398 | TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) { |
| 4020 | 3399 | assert(type_entry->id == ZigTypeIdUnion); |
| 4021 | | assert(type_entry->data.unionation.zero_bits_known); |
| 3400 | assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown)); |
| 4022 | 3401 | for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) { |
| 4023 | 3402 | TypeUnionField *field = &type_entry->data.unionation.fields[i]; |
| 4024 | 3403 | if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) { |
| ... | ... | @@ -4096,17 +3475,14 @@ ZigType *container_ref_type(ZigType *type_entry) { |
| 4096 | 3475 | type_entry->data.pointer.child_type : type_entry; |
| 4097 | 3476 | } |
| 4098 | 3477 | |
| 4099 | | void resolve_container_type(CodeGen *g, ZigType *type_entry) { |
| 3478 | Error resolve_container_type(CodeGen *g, ZigType *type_entry) { |
| 4100 | 3479 | switch (type_entry->id) { |
| 4101 | 3480 | case ZigTypeIdStruct: |
| 4102 | | resolve_struct_type(g, type_entry); |
| 4103 | | break; |
| 3481 | return resolve_struct_type(g, type_entry); |
| 4104 | 3482 | case ZigTypeIdEnum: |
| 4105 | | resolve_enum_type(g, type_entry); |
| 4106 | | break; |
| 3483 | return resolve_enum_zero_bits(g, type_entry); |
| 4107 | 3484 | case ZigTypeIdUnion: |
| 4108 | | resolve_union_type(g, type_entry); |
| 4109 | | break; |
| 3485 | return resolve_union_type(g, type_entry); |
| 4110 | 3486 | case ZigTypeIdPointer: |
| 4111 | 3487 | case ZigTypeIdMetaType: |
| 4112 | 3488 | case ZigTypeIdVoid: |
| ... | ... | @@ -4132,6 +3508,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) { |
| 4132 | 3508 | case ZigTypeIdVector: |
| 4133 | 3509 | zig_unreachable(); |
| 4134 | 3510 | } |
| 3511 | zig_unreachable(); |
| 4135 | 3512 | } |
| 4136 | 3513 | |
| 4137 | 3514 | ZigType *get_src_ptr_type(ZigType *type) { |
| ... | ... | @@ -4233,10 +3610,6 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) { |
| 4233 | 3610 | if (type_has_bits(param_type)) { |
| 4234 | 3611 | fn_table_entry->variable_list.append(var); |
| 4235 | 3612 | } |
| 4236 | | |
| 4237 | | if (fn_type->data.fn.gen_param_info) { |
| 4238 | | var->gen_arg_index = fn_type->data.fn.gen_param_info[i].gen_index; |
| 4239 | | } |
| 4240 | 3613 | } |
| 4241 | 3614 | } |
| 4242 | 3615 | |
| ... | ... | @@ -4626,18 +3999,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) { |
| 4626 | 3999 | } |
| 4627 | 4000 | |
| 4628 | 4001 | ZigType *entry = new_type_table_entry(ZigTypeIdVector); |
| 4629 | | entry->zero_bits = (len == 0) || !type_has_bits(elem_type); |
| 4630 | | entry->type_ref = entry->zero_bits ? LLVMVoidType() : LLVMVectorType(elem_type->type_ref, len); |
| 4002 | if ((len != 0) && type_has_bits(elem_type)) { |
| 4003 | // Vectors can only be ints, floats, or pointers. ints and floats have trivially resolvable |
| 4004 | // llvm type refs. pointers we will use usize instead. |
| 4005 | LLVMTypeRef example_vector_llvm_type; |
| 4006 | if (elem_type->id == ZigTypeIdPointer) { |
| 4007 | example_vector_llvm_type = LLVMVectorType(g->builtin_types.entry_usize->llvm_type, len); |
| 4008 | } else { |
| 4009 | example_vector_llvm_type = LLVMVectorType(elem_type->llvm_type, len); |
| 4010 | } |
| 4011 | assert(example_vector_llvm_type != nullptr); |
| 4012 | entry->size_in_bits = elem_type->size_in_bits * len; |
| 4013 | entry->abi_size = LLVMABISizeOfType(g->target_data_ref, example_vector_llvm_type); |
| 4014 | entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, example_vector_llvm_type); |
| 4015 | } |
| 4631 | 4016 | entry->data.vector.len = len; |
| 4632 | 4017 | entry->data.vector.elem_type = elem_type; |
| 4633 | 4018 | |
| 4634 | 4019 | buf_resize(&entry->name, 0); |
| 4635 | 4020 | buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name)); |
| 4636 | 4021 | |
| 4637 | | entry->di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, |
| 4638 | | len * type_size_bits(g, elem_type), |
| 4639 | | LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref), elem_type->di_type, len); |
| 4640 | | |
| 4641 | 4022 | g->type_table.put(type_id, entry); |
| 4642 | 4023 | return entry; |
| 4643 | 4024 | } |
| ... | ... | @@ -4685,12 +4066,7 @@ bool handle_is_ptr(ZigType *type_entry) { |
| 4685 | 4066 | !type_is_nonnull_ptr(type_entry->data.maybe.child_type) && |
| 4686 | 4067 | type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet; |
| 4687 | 4068 | case ZigTypeIdUnion: |
| 4688 | | assert(type_entry->data.unionation.zero_bits_known); |
| 4689 | | if (type_entry->data.unionation.gen_field_count == 0) |
| 4690 | | return false; |
| 4691 | | if (!type_has_bits(type_entry)) |
| 4692 | | return false; |
| 4693 | | return true; |
| 4069 | return type_has_bits(type_entry) && type_entry->data.unionation.gen_field_count != 0; |
| 4694 | 4070 | |
| 4695 | 4071 | } |
| 4696 | 4072 | zig_unreachable(); |
| ... | ... | @@ -5165,10 +4541,10 @@ bool fn_eval_eql(Scope *a, Scope *b) { |
| 5165 | 4541 | |
| 5166 | 4542 | // Whether the type has bits at runtime. |
| 5167 | 4543 | bool type_has_bits(ZigType *type_entry) { |
| 5168 | | assert(type_entry); |
| 4544 | assert(type_entry != nullptr); |
| 5169 | 4545 | assert(!type_is_invalid(type_entry)); |
| 5170 | 4546 | assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown)); |
| 5171 | | return !type_entry->zero_bits; |
| 4547 | return type_entry->abi_size != 0; |
| 5172 | 4548 | } |
| 5173 | 4549 | |
| 5174 | 4550 | // Whether you can infer the value based solely on the type. |
| ... | ... | @@ -5629,18 +5005,22 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) { |
| 5629 | 5005 | } else if (ty->id == ZigTypeIdEnum) { |
| 5630 | 5006 | return resolve_enum_zero_bits(g, ty); |
| 5631 | 5007 | } else if (ty->id == ZigTypeIdUnion) { |
| 5632 | | return resolve_union_zero_bits(g, ty); |
| 5008 | return resolve_union_alignment(g, ty); |
| 5633 | 5009 | } |
| 5634 | 5010 | return ErrorNone; |
| 5635 | 5011 | case ResolveStatusSizeKnown: |
| 5636 | 5012 | if (ty->id == ZigTypeIdStruct) { |
| 5637 | 5013 | return resolve_struct_type(g, ty); |
| 5638 | 5014 | } else if (ty->id == ZigTypeIdEnum) { |
| 5639 | | return resolve_enum_type(g, ty); |
| 5015 | return resolve_enum_zero_bits(g, ty); |
| 5640 | 5016 | } else if (ty->id == ZigTypeIdUnion) { |
| 5641 | 5017 | return resolve_union_type(g, ty); |
| 5642 | 5018 | } |
| 5643 | 5019 | return ErrorNone; |
| 5020 | case ResolveStatusLLVMFwdDecl: |
| 5021 | case ResolveStatusLLVMFull: |
| 5022 | resolve_llvm_types(g, ty, status); |
| 5023 | return ErrorNone; |
| 5644 | 5024 | } |
| 5645 | 5025 | zig_unreachable(); |
| 5646 | 5026 | } |
| ... | ... | @@ -6192,31 +5572,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { |
| 6192 | 5572 | ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) { |
| 6193 | 5573 | assert(size_in_bits <= 65535); |
| 6194 | 5574 | ZigType *entry = new_type_table_entry(ZigTypeIdInt); |
| 6195 | | entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits); |
| 6196 | | entry->zero_bits = (size_in_bits == 0); |
| 5575 | |
| 5576 | entry->size_in_bits = size_in_bits; |
| 5577 | if (size_in_bits != 0) { |
| 5578 | entry->llvm_type = LLVMIntType(size_in_bits); |
| 5579 | entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type); |
| 5580 | entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type); |
| 5581 | } |
| 6197 | 5582 | |
| 6198 | 5583 | const char u_or_i = is_signed ? 'i' : 'u'; |
| 6199 | 5584 | buf_resize(&entry->name, 0); |
| 6200 | 5585 | buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits); |
| 6201 | 5586 | |
| 6202 | | unsigned dwarf_tag; |
| 6203 | | if (is_signed) { |
| 6204 | | if (size_in_bits == 8) { |
| 6205 | | dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char(); |
| 6206 | | } else { |
| 6207 | | dwarf_tag = ZigLLVMEncoding_DW_ATE_signed(); |
| 6208 | | } |
| 6209 | | } else { |
| 6210 | | if (size_in_bits == 8) { |
| 6211 | | dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char(); |
| 6212 | | } else { |
| 6213 | | dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned(); |
| 6214 | | } |
| 6215 | | } |
| 6216 | | |
| 6217 | | uint64_t debug_size_in_bits = (size_in_bits == 0) ? |
| 6218 | | 0 : (8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref)); |
| 6219 | | entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), debug_size_in_bits, dwarf_tag); |
| 6220 | 5587 | entry->data.integral.is_signed = is_signed; |
| 6221 | 5588 | entry->data.integral.bit_count = size_in_bits; |
| 6222 | 5589 | return entry; |
| ... | ... | @@ -6620,33 +5987,13 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) { |
| 6620 | 5987 | return link_lib; |
| 6621 | 5988 | } |
| 6622 | 5989 | |
| 6623 | | uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) { |
| 6624 | | assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown)); |
| 6625 | | if (type_entry->zero_bits) return 0; |
| 6626 | | |
| 6627 | | // We need to make this function work without requiring ensure_complete_type |
| 6628 | | // so that we can have structs with fields that are pointers to their own type. |
| 6629 | | if (type_entry->id == ZigTypeIdStruct) { |
| 6630 | | assert(type_entry->data.structure.abi_alignment != 0); |
| 6631 | | return type_entry->data.structure.abi_alignment; |
| 6632 | | } else if (type_entry->id == ZigTypeIdUnion) { |
| 6633 | | assert(type_entry->data.unionation.abi_alignment != 0); |
| 6634 | | return type_entry->data.unionation.abi_alignment; |
| 6635 | | } else if (type_entry->id == ZigTypeIdOpaque) { |
| 6636 | | return 1; |
| 6637 | | } else { |
| 6638 | | uint32_t llvm_alignment = LLVMABIAlignmentOfType(g->target_data_ref, type_entry->type_ref); |
| 6639 | | return llvm_alignment; |
| 6640 | | } |
| 6641 | | } |
| 6642 | | |
| 6643 | | ZigType *get_align_amt_type(CodeGen *g) { |
| 6644 | | if (g->align_amt_type == nullptr) { |
| 6645 | | // according to LLVM the maximum alignment is 1 << 29. |
| 6646 | | g->align_amt_type = get_int_type(g, false, 29); |
| 6647 | | } |
| 6648 | | return g->align_amt_type; |
| 6649 | | } |
| 5990 | ZigType *get_align_amt_type(CodeGen *g) { |
| 5991 | if (g->align_amt_type == nullptr) { |
| 5992 | // according to LLVM the maximum alignment is 1 << 29. |
| 5993 | g->align_amt_type = get_int_type(g, false, 29); |
| 5994 | } |
| 5995 | return g->align_amt_type; |
| 5996 | } |
| 6650 | 5997 | |
| 6651 | 5998 | uint32_t type_ptr_hash(const ZigType *ptr) { |
| 6652 | 5999 | return hash_ptr((void*)ptr); |
| ... | ... | @@ -6841,11 +6188,10 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) { |
| 6841 | 6188 | |
| 6842 | 6189 | uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) { |
| 6843 | 6190 | assert(struct_type->id == ZigTypeIdStruct); |
| 6844 | | if (struct_type->data.structure.layout != ContainerLayoutPacked) { |
| 6191 | assert(type_is_resolved(struct_type, ResolveStatusSizeKnown)); |
| 6192 | if (struct_type->data.structure.host_int_bytes == nullptr) |
| 6845 | 6193 | return 0; |
| 6846 | | } |
| 6847 | | LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index); |
| 6848 | | return LLVMStoreSizeOfType(g->target_data_ref, field_type); |
| 6194 | return struct_type->data.structure.host_int_bytes[field->gen_index]; |
| 6849 | 6195 | } |
| 6850 | 6196 | |
| 6851 | 6197 | Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, |
| ... | ... | @@ -6911,3 +6257,968 @@ Buf *type_bare_name(ZigType *type_entry) { |
| 6911 | 6257 | Buf *type_h_name(ZigType *t) { |
| 6912 | 6258 | return type_bare_name(t); |
| 6913 | 6259 | } |
| 6260 | |
| 6261 | static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) { |
| 6262 | if (type->data.structure.resolve_status >= wanted_resolve_status) return; |
| 6263 | |
| 6264 | ZigType *ptr_type = type->data.structure.fields[slice_ptr_index].type_entry; |
| 6265 | ZigType *child_type = ptr_type->data.pointer.child_type; |
| 6266 | ZigType *usize_type = g->builtin_types.entry_usize; |
| 6267 | |
| 6268 | bool done = false; |
| 6269 | if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile || |
| 6270 | ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero) |
| 6271 | { |
| 6272 | ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false, |
| 6273 | PtrLenUnknown, 0, 0, 0, false); |
| 6274 | ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type); |
| 6275 | |
| 6276 | assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status)); |
| 6277 | type->llvm_type = peer_slice_type->llvm_type; |
| 6278 | type->llvm_di_type = peer_slice_type->llvm_di_type; |
| 6279 | type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status; |
| 6280 | done = true; |
| 6281 | } |
| 6282 | |
| 6283 | // If the child type is []const T then we need to make sure the type ref |
| 6284 | // and debug info is the same as if the child type were []T. |
| 6285 | if (is_slice(child_type)) { |
| 6286 | ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry; |
| 6287 | assert(child_ptr_type->id == ZigTypeIdPointer); |
| 6288 | if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile || |
| 6289 | child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero) |
| 6290 | { |
| 6291 | ZigType *grand_child_type = child_ptr_type->data.pointer.child_type; |
| 6292 | ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false, |
| 6293 | PtrLenUnknown, 0, 0, 0, false); |
| 6294 | ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type); |
| 6295 | ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false, |
| 6296 | PtrLenUnknown, 0, 0, 0, false); |
| 6297 | ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type); |
| 6298 | |
| 6299 | assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status)); |
| 6300 | type->llvm_type = peer_slice_type->llvm_type; |
| 6301 | type->llvm_di_type = peer_slice_type->llvm_di_type; |
| 6302 | type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status; |
| 6303 | done = true; |
| 6304 | } |
| 6305 | } |
| 6306 | |
| 6307 | if (done) return; |
| 6308 | |
| 6309 | LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type); |
| 6310 | ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type); |
| 6311 | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 6312 | ZigLLVMDIFile *di_file = nullptr; |
| 6313 | unsigned line = 0; |
| 6314 | |
| 6315 | if (type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) { |
| 6316 | type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name)); |
| 6317 | |
| 6318 | type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 6319 | ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name), |
| 6320 | compile_unit_scope, di_file, line); |
| 6321 | |
| 6322 | type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl; |
| 6323 | if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return; |
| 6324 | } |
| 6325 | |
| 6326 | if (!type_has_bits(child_type)) { |
| 6327 | LLVMTypeRef element_types[] = { |
| 6328 | usize_llvm_type, |
| 6329 | }; |
| 6330 | LLVMStructSetBody(type->llvm_type, element_types, 1, false); |
| 6331 | |
| 6332 | uint64_t len_debug_size_in_bits = usize_type->size_in_bits; |
| 6333 | uint64_t len_debug_align_in_bits = 8*usize_type->abi_align; |
| 6334 | uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0); |
| 6335 | |
| 6336 | uint64_t debug_size_in_bits = type->size_in_bits; |
| 6337 | uint64_t debug_align_in_bits = 8*type->abi_align; |
| 6338 | |
| 6339 | ZigLLVMDIType *di_element_types[] = { |
| 6340 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6341 | "len", di_file, line, |
| 6342 | len_debug_size_in_bits, |
| 6343 | len_debug_align_in_bits, |
| 6344 | len_offset_in_bits, |
| 6345 | 0, usize_llvm_di_type), |
| 6346 | }; |
| 6347 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6348 | compile_unit_scope, |
| 6349 | buf_ptr(&type->name), |
| 6350 | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 6351 | nullptr, di_element_types, 1, 0, nullptr, ""); |
| 6352 | |
| 6353 | ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type); |
| 6354 | type->llvm_di_type = replacement_di_type; |
| 6355 | type->data.structure.resolve_status = ResolveStatusLLVMFull; |
| 6356 | return; |
| 6357 | } |
| 6358 | |
| 6359 | LLVMTypeRef element_types[2]; |
| 6360 | element_types[slice_ptr_index] = get_llvm_type(g, ptr_type); |
| 6361 | element_types[slice_len_index] = get_llvm_type(g, g->builtin_types.entry_usize); |
| 6362 | if (type->data.structure.resolve_status >= wanted_resolve_status) return; |
| 6363 | LLVMStructSetBody(type->llvm_type, element_types, 2, false); |
| 6364 | |
| 6365 | uint64_t ptr_debug_size_in_bits = ptr_type->size_in_bits; |
| 6366 | uint64_t ptr_debug_align_in_bits = 8*ptr_type->abi_align; |
| 6367 | uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0); |
| 6368 | |
| 6369 | uint64_t len_debug_size_in_bits = usize_type->size_in_bits; |
| 6370 | uint64_t len_debug_align_in_bits = 8*usize_type->abi_align; |
| 6371 | uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1); |
| 6372 | |
| 6373 | uint64_t debug_size_in_bits = type->size_in_bits; |
| 6374 | uint64_t debug_align_in_bits = 8*type->abi_align; |
| 6375 | |
| 6376 | ZigLLVMDIType *di_element_types[] = { |
| 6377 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6378 | "ptr", di_file, line, |
| 6379 | ptr_debug_size_in_bits, |
| 6380 | ptr_debug_align_in_bits, |
| 6381 | ptr_offset_in_bits, |
| 6382 | 0, get_llvm_di_type(g, ptr_type)), |
| 6383 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6384 | "len", di_file, line, |
| 6385 | len_debug_size_in_bits, |
| 6386 | len_debug_align_in_bits, |
| 6387 | len_offset_in_bits, |
| 6388 | 0, usize_llvm_di_type), |
| 6389 | }; |
| 6390 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6391 | compile_unit_scope, |
| 6392 | buf_ptr(&type->name), |
| 6393 | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 6394 | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 6395 | |
| 6396 | ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type); |
| 6397 | type->llvm_di_type = replacement_di_type; |
| 6398 | type->data.structure.resolve_status = ResolveStatusLLVMFull; |
| 6399 | } |
| 6400 | |
| 6401 | static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveStatus wanted_resolve_status) { |
| 6402 | assert(struct_type->id == ZigTypeIdStruct); |
| 6403 | assert(struct_type->data.structure.resolve_status != ResolveStatusInvalid); |
| 6404 | assert(struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown); |
| 6405 | assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0); |
| 6406 | if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return; |
| 6407 | |
| 6408 | AstNode *decl_node = struct_type->data.structure.decl_node; |
| 6409 | ZigLLVMDIFile *di_file; |
| 6410 | ZigLLVMDIScope *di_scope; |
| 6411 | unsigned line; |
| 6412 | if (decl_node != nullptr) { |
| 6413 | assert(decl_node->type == NodeTypeContainerDecl); |
| 6414 | Scope *scope = &struct_type->data.structure.decls_scope->base; |
| 6415 | ZigType *import = get_scope_import(scope); |
| 6416 | di_file = import->data.structure.root_struct->di_file; |
| 6417 | di_scope = ZigLLVMFileToScope(di_file); |
| 6418 | line = decl_node->line + 1; |
| 6419 | } else { |
| 6420 | di_file = nullptr; |
| 6421 | di_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 6422 | line = 0; |
| 6423 | } |
| 6424 | |
| 6425 | if (struct_type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) { |
| 6426 | struct_type->llvm_type = type_has_bits(struct_type) ? |
| 6427 | LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType(); |
| 6428 | unsigned dwarf_kind = ZigLLVMTag_DW_structure_type(); |
| 6429 | struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 6430 | dwarf_kind, buf_ptr(&struct_type->name), |
| 6431 | di_scope, di_file, line); |
| 6432 | |
| 6433 | struct_type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl; |
| 6434 | if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return; |
| 6435 | } |
| 6436 | |
| 6437 | size_t field_count = struct_type->data.structure.src_field_count; |
| 6438 | size_t gen_field_count = struct_type->data.structure.gen_field_count; |
| 6439 | LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count); |
| 6440 | |
| 6441 | size_t gen_field_index = 0; |
| 6442 | bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked); |
| 6443 | size_t packed_bits_offset = 0; |
| 6444 | size_t first_packed_bits_offset_misalign = SIZE_MAX; |
| 6445 | size_t debug_field_count = 0; |
| 6446 | |
| 6447 | // trigger all the recursive get_llvm_type calls |
| 6448 | for (size_t i = 0; i < field_count; i += 1) { |
| 6449 | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 6450 | ZigType *field_type = type_struct_field->type_entry; |
| 6451 | if (!type_has_bits(field_type)) |
| 6452 | continue; |
| 6453 | (void)get_llvm_type(g, field_type); |
| 6454 | if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return; |
| 6455 | } |
| 6456 | |
| 6457 | for (size_t i = 0; i < field_count; i += 1) { |
| 6458 | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 6459 | ZigType *field_type = type_struct_field->type_entry; |
| 6460 | |
| 6461 | if (!type_has_bits(field_type)) |
| 6462 | continue; |
| 6463 | |
| 6464 | if (packed) { |
| 6465 | size_t field_size_in_bits = type_size_bits(g, field_type); |
| 6466 | size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits; |
| 6467 | |
| 6468 | if (first_packed_bits_offset_misalign != SIZE_MAX) { |
| 6469 | // this field is not byte-aligned; it is part of the previous field with a bit offset |
| 6470 | |
| 6471 | size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign; |
| 6472 | size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes); |
| 6473 | if (full_abi_size * 8 == full_bit_count) { |
| 6474 | // next field recovers ABI alignment |
| 6475 | element_types[gen_field_index] = LLVMIntType((unsigned)(full_bit_count)); |
| 6476 | gen_field_index += 1; |
| 6477 | |
| 6478 | first_packed_bits_offset_misalign = SIZE_MAX; |
| 6479 | } |
| 6480 | } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) { |
| 6481 | first_packed_bits_offset_misalign = packed_bits_offset; |
| 6482 | } else { |
| 6483 | // This is a byte-aligned field (both start and end) in a packed struct. |
| 6484 | element_types[gen_field_index] = get_llvm_type(g, field_type); |
| 6485 | gen_field_index += 1; |
| 6486 | } |
| 6487 | packed_bits_offset = next_packed_bits_offset; |
| 6488 | } else { |
| 6489 | element_types[gen_field_index] = get_llvm_type(g, field_type); |
| 6490 | |
| 6491 | gen_field_index += 1; |
| 6492 | } |
| 6493 | debug_field_count += 1; |
| 6494 | } |
| 6495 | if (first_packed_bits_offset_misalign != SIZE_MAX) { |
| 6496 | size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign; |
| 6497 | size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes); |
| 6498 | element_types[gen_field_index] = LLVMIntType((unsigned)full_abi_size * 8); |
| 6499 | gen_field_index += 1; |
| 6500 | } |
| 6501 | |
| 6502 | if (type_has_bits(struct_type)) { |
| 6503 | LLVMStructSetBody(struct_type->llvm_type, element_types, (unsigned)gen_field_count, packed); |
| 6504 | } |
| 6505 | |
| 6506 | ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count); |
| 6507 | size_t debug_field_index = 0; |
| 6508 | for (size_t i = 0; i < field_count; i += 1) { |
| 6509 | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 6510 | size_t gen_field_index = type_struct_field->gen_index; |
| 6511 | if (gen_field_index == SIZE_MAX) { |
| 6512 | continue; |
| 6513 | } |
| 6514 | |
| 6515 | ZigType *field_type = type_struct_field->type_entry; |
| 6516 | |
| 6517 | // if the field is a function, actually the debug info should be a pointer. |
| 6518 | ZigLLVMDIType *field_di_type; |
| 6519 | if (field_type->id == ZigTypeIdFn) { |
| 6520 | ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true); |
| 6521 | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type)); |
| 6522 | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type)); |
| 6523 | field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, field_type), |
| 6524 | debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name)); |
| 6525 | } else { |
| 6526 | field_di_type = get_llvm_di_type(g, field_type); |
| 6527 | } |
| 6528 | |
| 6529 | uint64_t debug_size_in_bits; |
| 6530 | uint64_t debug_align_in_bits; |
| 6531 | uint64_t debug_offset_in_bits; |
| 6532 | if (packed) { |
| 6533 | debug_size_in_bits = type_struct_field->type_entry->size_in_bits; |
| 6534 | debug_align_in_bits = 8 * type_struct_field->type_entry->abi_align; |
| 6535 | debug_offset_in_bits = 8 * type_struct_field->offset + type_struct_field->bit_offset_in_host; |
| 6536 | } else { |
| 6537 | debug_size_in_bits = 8 * get_store_size_bytes(field_type->size_in_bits); |
| 6538 | debug_align_in_bits = 8 * field_type->abi_align; |
| 6539 | debug_offset_in_bits = 8 * type_struct_field->offset; |
| 6540 | } |
| 6541 | unsigned line; |
| 6542 | if (decl_node != nullptr) { |
| 6543 | AstNode *field_node = decl_node->data.container_decl.fields.at(i); |
| 6544 | line = field_node->line + 1; |
| 6545 | } else { |
| 6546 | line = 0; |
| 6547 | } |
| 6548 | di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 6549 | ZigLLVMTypeToScope(struct_type->llvm_di_type), buf_ptr(type_struct_field->name), |
| 6550 | di_file, line, |
| 6551 | debug_size_in_bits, |
| 6552 | debug_align_in_bits, |
| 6553 | debug_offset_in_bits, |
| 6554 | 0, field_di_type); |
| 6555 | assert(di_element_types[debug_field_index]); |
| 6556 | debug_field_index += 1; |
| 6557 | } |
| 6558 | |
| 6559 | uint64_t debug_size_in_bits = 8*get_store_size_bytes(struct_type->size_in_bits); |
| 6560 | uint64_t debug_align_in_bits = 8*struct_type->abi_align; |
| 6561 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6562 | di_scope, |
| 6563 | buf_ptr(&struct_type->name), |
| 6564 | di_file, line, |
| 6565 | debug_size_in_bits, |
| 6566 | debug_align_in_bits, |
| 6567 | 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, ""); |
| 6568 | |
| 6569 | ZigLLVMReplaceTemporary(g->dbuilder, struct_type->llvm_di_type, replacement_di_type); |
| 6570 | struct_type->llvm_di_type = replacement_di_type; |
| 6571 | struct_type->data.structure.resolve_status = ResolveStatusLLVMFull; |
| 6572 | } |
| 6573 | |
| 6574 | static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) { |
| 6575 | assert(!enum_type->data.enumeration.is_invalid); |
| 6576 | assert(enum_type->data.enumeration.complete); |
| 6577 | if (enum_type->llvm_di_type != nullptr) return; |
| 6578 | |
| 6579 | Scope *scope = &enum_type->data.enumeration.decls_scope->base; |
| 6580 | ZigType *import = get_scope_import(scope); |
| 6581 | AstNode *decl_node = enum_type->data.enumeration.decl_node; |
| 6582 | |
| 6583 | if (!type_has_bits(enum_type)) { |
| 6584 | enum_type->llvm_type = g->builtin_types.entry_void->llvm_type; |
| 6585 | |
| 6586 | uint64_t debug_size_in_bits = 0; |
| 6587 | uint64_t debug_align_in_bits = 0; |
| 6588 | ZigLLVMDIType **di_element_types = nullptr; |
| 6589 | size_t debug_field_count = 0; |
| 6590 | enum_type->llvm_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6591 | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 6592 | buf_ptr(&enum_type->name), |
| 6593 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6594 | debug_size_in_bits, |
| 6595 | debug_align_in_bits, |
| 6596 | 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, ""); |
| 6597 | return; |
| 6598 | } |
| 6599 | |
| 6600 | uint32_t field_count = enum_type->data.enumeration.src_field_count; |
| 6601 | |
| 6602 | assert(enum_type->data.enumeration.fields); |
| 6603 | ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count); |
| 6604 | |
| 6605 | for (uint32_t i = 0; i < field_count; i += 1) { |
| 6606 | TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i]; |
| 6607 | |
| 6608 | // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t |
| 6609 | // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html |
| 6610 | di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name), |
| 6611 | bigint_as_signed(&enum_field->value)); |
| 6612 | } |
| 6613 | |
| 6614 | ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type; |
| 6615 | enum_type->llvm_type = get_llvm_type(g, tag_int_type); |
| 6616 | |
| 6617 | // create debug type for tag |
| 6618 | uint64_t tag_debug_size_in_bits = tag_int_type->size_in_bits; |
| 6619 | uint64_t tag_debug_align_in_bits = 8*tag_int_type->abi_align; |
| 6620 | ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder, |
| 6621 | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name), |
| 6622 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6623 | tag_debug_size_in_bits, |
| 6624 | tag_debug_align_in_bits, |
| 6625 | di_enumerators, field_count, |
| 6626 | get_llvm_di_type(g, tag_int_type), ""); |
| 6627 | |
| 6628 | enum_type->llvm_di_type = tag_di_type; |
| 6629 | } |
| 6630 | |
| 6631 | static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) { |
| 6632 | if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return; |
| 6633 | |
| 6634 | ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; |
| 6635 | ZigType *tag_type = union_type->data.unionation.tag_type; |
| 6636 | if (most_aligned_union_member == nullptr) { |
| 6637 | union_type->llvm_type = get_llvm_type(g, tag_type); |
| 6638 | union_type->llvm_di_type = get_llvm_di_type(g, tag_type); |
| 6639 | union_type->data.unionation.resolve_status = ResolveStatusLLVMFull; |
| 6640 | return; |
| 6641 | } |
| 6642 | |
| 6643 | Scope *scope = &union_type->data.unionation.decls_scope->base; |
| 6644 | ZigType *import = get_scope_import(scope); |
| 6645 | AstNode *decl_node = union_type->data.unionation.decl_node; |
| 6646 | |
| 6647 | if (union_type->data.unionation.resolve_status < ResolveStatusLLVMFwdDecl) { |
| 6648 | union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name)); |
| 6649 | size_t line = decl_node ? decl_node->line : 0; |
| 6650 | unsigned dwarf_kind = ZigLLVMTag_DW_structure_type(); |
| 6651 | union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 6652 | dwarf_kind, buf_ptr(&union_type->name), |
| 6653 | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 6654 | import->data.structure.root_struct->di_file, (unsigned)(line + 1)); |
| 6655 | |
| 6656 | union_type->data.unionation.resolve_status = ResolveStatusLLVMFwdDecl; |
| 6657 | if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return; |
| 6658 | } |
| 6659 | |
| 6660 | uint32_t gen_field_count = union_type->data.unionation.gen_field_count; |
| 6661 | ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count); |
| 6662 | uint32_t field_count = union_type->data.unionation.src_field_count; |
| 6663 | for (uint32_t i = 0; i < field_count; i += 1) { |
| 6664 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 6665 | if (!type_has_bits(union_field->type_entry)) |
| 6666 | continue; |
| 6667 | |
| 6668 | ZigLLVMDIType *field_di_type = get_llvm_di_type(g, union_field->type_entry); |
| 6669 | if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return; |
| 6670 | |
| 6671 | uint64_t store_size_in_bits = union_field->type_entry->size_in_bits; |
| 6672 | uint64_t abi_align_in_bits = 8*union_field->type_entry->abi_align; |
| 6673 | AstNode *field_node = decl_node->data.container_decl.fields.at(i); |
| 6674 | union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 6675 | ZigLLVMTypeToScope(union_type->llvm_di_type), buf_ptr(union_field->enum_field->name), |
| 6676 | import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1), |
| 6677 | store_size_in_bits, |
| 6678 | abi_align_in_bits, |
| 6679 | 0, |
| 6680 | 0, field_di_type); |
| 6681 | |
| 6682 | } |
| 6683 | |
| 6684 | if (tag_type == nullptr || !type_has_bits(tag_type)) { |
| 6685 | assert(most_aligned_union_member != nullptr); |
| 6686 | |
| 6687 | size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size; |
| 6688 | if (padding_bytes > 0) { |
| 6689 | ZigType *u8_type = get_int_type(g, false, 8); |
| 6690 | ZigType *padding_array = get_array_type(g, u8_type, padding_bytes); |
| 6691 | LLVMTypeRef union_element_types[] = { |
| 6692 | most_aligned_union_member->llvm_type, |
| 6693 | get_llvm_type(g, padding_array), |
| 6694 | }; |
| 6695 | LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false); |
| 6696 | } else { |
| 6697 | LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false); |
| 6698 | } |
| 6699 | union_type->data.unionation.union_llvm_type = union_type->llvm_type; |
| 6700 | union_type->data.unionation.gen_tag_index = SIZE_MAX; |
| 6701 | union_type->data.unionation.gen_union_index = SIZE_MAX; |
| 6702 | |
| 6703 | // create debug type for union |
| 6704 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, |
| 6705 | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name), |
| 6706 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6707 | union_type->data.unionation.union_abi_size * 8, |
| 6708 | most_aligned_union_member->abi_align * 8, |
| 6709 | 0, union_inner_di_types, |
| 6710 | gen_field_count, 0, ""); |
| 6711 | |
| 6712 | ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type); |
| 6713 | union_type->llvm_di_type = replacement_di_type; |
| 6714 | union_type->data.unionation.resolve_status = ResolveStatusLLVMFull; |
| 6715 | return; |
| 6716 | } |
| 6717 | |
| 6718 | LLVMTypeRef union_type_ref; |
| 6719 | size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size; |
| 6720 | if (padding_bytes == 0) { |
| 6721 | union_type_ref = get_llvm_type(g, most_aligned_union_member); |
| 6722 | } else { |
| 6723 | ZigType *u8_type = get_int_type(g, false, 8); |
| 6724 | ZigType *padding_array = get_array_type(g, u8_type, padding_bytes); |
| 6725 | LLVMTypeRef union_element_types[] = { |
| 6726 | get_llvm_type(g, most_aligned_union_member), |
| 6727 | get_llvm_type(g, padding_array), |
| 6728 | }; |
| 6729 | union_type_ref = LLVMStructType(union_element_types, 2, false); |
| 6730 | } |
| 6731 | union_type->data.unionation.union_llvm_type = union_type_ref; |
| 6732 | |
| 6733 | LLVMTypeRef root_struct_element_types[2]; |
| 6734 | root_struct_element_types[union_type->data.unionation.gen_tag_index] = get_llvm_type(g, tag_type); |
| 6735 | root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref; |
| 6736 | LLVMStructSetBody(union_type->llvm_type, root_struct_element_types, 2, false); |
| 6737 | |
| 6738 | // create debug type for union |
| 6739 | ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, |
| 6740 | ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion", |
| 6741 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6742 | most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align, |
| 6743 | 0, union_inner_di_types, gen_field_count, 0, ""); |
| 6744 | |
| 6745 | uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type, |
| 6746 | union_type->data.unionation.gen_union_index); |
| 6747 | uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type, |
| 6748 | union_type->data.unionation.gen_tag_index); |
| 6749 | |
| 6750 | ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 6751 | ZigLLVMTypeToScope(union_type->llvm_di_type), "payload", |
| 6752 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6753 | most_aligned_union_member->size_in_bits, |
| 6754 | 8*most_aligned_union_member->abi_align, |
| 6755 | union_offset_in_bits, |
| 6756 | 0, union_di_type); |
| 6757 | |
| 6758 | uint64_t tag_debug_size_in_bits = tag_type->size_in_bits; |
| 6759 | uint64_t tag_debug_align_in_bits = 8*tag_type->abi_align; |
| 6760 | |
| 6761 | ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder, |
| 6762 | ZigLLVMTypeToScope(union_type->llvm_di_type), "tag", |
| 6763 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6764 | tag_debug_size_in_bits, |
| 6765 | tag_debug_align_in_bits, |
| 6766 | tag_offset_in_bits, |
| 6767 | 0, get_llvm_di_type(g, tag_type)); |
| 6768 | |
| 6769 | ZigLLVMDIType *di_root_members[2]; |
| 6770 | di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type; |
| 6771 | di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type; |
| 6772 | |
| 6773 | uint64_t debug_size_in_bits = union_type->size_in_bits; |
| 6774 | uint64_t debug_align_in_bits = 8*union_type->abi_align; |
| 6775 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6776 | ZigLLVMFileToScope(import->data.structure.root_struct->di_file), |
| 6777 | buf_ptr(&union_type->name), |
| 6778 | import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), |
| 6779 | debug_size_in_bits, |
| 6780 | debug_align_in_bits, |
| 6781 | 0, nullptr, di_root_members, 2, 0, nullptr, ""); |
| 6782 | |
| 6783 | ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type); |
| 6784 | union_type->llvm_di_type = replacement_di_type; |
| 6785 | union_type->data.unionation.resolve_status = ResolveStatusLLVMFull; |
| 6786 | } |
| 6787 | |
| 6788 | static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) { |
| 6789 | if (type->llvm_di_type != nullptr) return; |
| 6790 | |
| 6791 | if (!type_has_bits(type)) { |
| 6792 | type->llvm_type = g->builtin_types.entry_void->llvm_type; |
| 6793 | type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type; |
| 6794 | return; |
| 6795 | } |
| 6796 | |
| 6797 | ZigType *elem_type = type->data.pointer.child_type; |
| 6798 | |
| 6799 | if (type->data.pointer.is_const || type->data.pointer.is_volatile || |
| 6800 | type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle || |
| 6801 | type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero) |
| 6802 | { |
| 6803 | ZigType *peer_type = get_pointer_to_type_extra(g, elem_type, false, false, |
| 6804 | PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false); |
| 6805 | type->llvm_type = get_llvm_type(g, peer_type); |
| 6806 | type->llvm_di_type = get_llvm_di_type(g, peer_type); |
| 6807 | return; |
| 6808 | } |
| 6809 | |
| 6810 | if (type->data.pointer.host_int_bytes == 0) { |
| 6811 | assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl)); |
| 6812 | type->llvm_type = LLVMPointerType(elem_type->llvm_type, 0); |
| 6813 | uint64_t debug_size_in_bits = 8*get_store_size_bytes(type->size_in_bits); |
| 6814 | uint64_t debug_align_in_bits = 8*type->abi_align; |
| 6815 | type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, elem_type->llvm_di_type, |
| 6816 | debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name)); |
| 6817 | assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFull)); |
| 6818 | } else { |
| 6819 | ZigType *host_int_type = get_int_type(g, false, type->data.pointer.host_int_bytes * 8); |
| 6820 | LLVMTypeRef host_int_llvm_type = get_llvm_type(g, host_int_type); |
| 6821 | type->llvm_type = LLVMPointerType(host_int_llvm_type, 0); |
| 6822 | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_llvm_type); |
| 6823 | uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_llvm_type); |
| 6824 | type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, host_int_type), |
| 6825 | debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name)); |
| 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) { |
| 6830 | if (type->llvm_di_type != nullptr) return; |
| 6831 | |
| 6832 | if (!type_has_bits(type)) { |
| 6833 | type->llvm_type = g->builtin_types.entry_void->llvm_type; |
| 6834 | type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type; |
| 6835 | return; |
| 6836 | } |
| 6837 | |
| 6838 | unsigned dwarf_tag; |
| 6839 | if (type->data.integral.is_signed) { |
| 6840 | if (type->size_in_bits == 8) { |
| 6841 | dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char(); |
| 6842 | } else { |
| 6843 | dwarf_tag = ZigLLVMEncoding_DW_ATE_signed(); |
| 6844 | } |
| 6845 | } else { |
| 6846 | if (type->size_in_bits == 8) { |
| 6847 | dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char(); |
| 6848 | } else { |
| 6849 | dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned(); |
| 6850 | } |
| 6851 | } |
| 6852 | |
| 6853 | type->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&type->name), type->size_in_bits, dwarf_tag); |
| 6854 | type->llvm_type = LLVMIntType(type->size_in_bits); |
| 6855 | } |
| 6856 | |
| 6857 | static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) { |
| 6858 | if (type->llvm_di_type != nullptr) return; |
| 6859 | |
| 6860 | LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool); |
| 6861 | ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool); |
| 6862 | |
| 6863 | ZigType *child_type = type->data.maybe.child_type; |
| 6864 | if (!type_has_bits(child_type)) { |
| 6865 | type->llvm_type = bool_llvm_type; |
| 6866 | type->llvm_di_type = bool_llvm_di_type; |
| 6867 | return; |
| 6868 | } |
| 6869 | |
| 6870 | LLVMTypeRef child_llvm_type = get_llvm_type(g, child_type); |
| 6871 | ZigLLVMDIType *child_llvm_di_type = get_llvm_di_type(g, child_type); |
| 6872 | |
| 6873 | if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) { |
| 6874 | type->llvm_type = child_llvm_type; |
| 6875 | type->llvm_di_type = child_llvm_di_type; |
| 6876 | return; |
| 6877 | } |
| 6878 | |
| 6879 | LLVMTypeRef elem_types[] = { |
| 6880 | get_llvm_type(g, child_type), |
| 6881 | LLVMInt1Type(), |
| 6882 | }; |
| 6883 | type->llvm_type = LLVMStructType(elem_types, 2, false); |
| 6884 | |
| 6885 | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 6886 | ZigLLVMDIFile *di_file = nullptr; |
| 6887 | unsigned line = 0; |
| 6888 | type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 6889 | ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name), |
| 6890 | compile_unit_scope, di_file, line); |
| 6891 | |
| 6892 | uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_llvm_type); |
| 6893 | uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_llvm_type); |
| 6894 | uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0); |
| 6895 | |
| 6896 | uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_llvm_type); |
| 6897 | uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_llvm_type); |
| 6898 | uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1); |
| 6899 | |
| 6900 | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type); |
| 6901 | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type); |
| 6902 | |
| 6903 | ZigLLVMDIType *di_element_types[] = { |
| 6904 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6905 | "val", di_file, line, |
| 6906 | val_debug_size_in_bits, |
| 6907 | val_debug_align_in_bits, |
| 6908 | val_offset_in_bits, |
| 6909 | 0, child_llvm_di_type), |
| 6910 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6911 | "maybe", di_file, line, |
| 6912 | maybe_debug_size_in_bits, |
| 6913 | maybe_debug_align_in_bits, |
| 6914 | maybe_offset_in_bits, |
| 6915 | 0, bool_llvm_di_type), |
| 6916 | }; |
| 6917 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6918 | compile_unit_scope, |
| 6919 | buf_ptr(&type->name), |
| 6920 | di_file, line, debug_size_in_bits, debug_align_in_bits, 0, |
| 6921 | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 6922 | |
| 6923 | ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type); |
| 6924 | type->llvm_di_type = replacement_di_type; |
| 6925 | } |
| 6926 | |
| 6927 | static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) { |
| 6928 | if (type->llvm_di_type != nullptr) return; |
| 6929 | |
| 6930 | ZigType *payload_type = type->data.error_union.payload_type; |
| 6931 | ZigType *err_set_type = type->data.error_union.err_set_type; |
| 6932 | |
| 6933 | if (!type_has_bits(payload_type)) { |
| 6934 | assert(type_has_bits(err_set_type)); |
| 6935 | type->llvm_type = get_llvm_type(g, err_set_type); |
| 6936 | type->llvm_di_type = get_llvm_di_type(g, err_set_type); |
| 6937 | } else if (!type_has_bits(err_set_type)) { |
| 6938 | type->llvm_type = get_llvm_type(g, payload_type); |
| 6939 | type->llvm_di_type = get_llvm_di_type(g, payload_type); |
| 6940 | } else { |
| 6941 | LLVMTypeRef err_set_llvm_type = get_llvm_type(g, err_set_type); |
| 6942 | LLVMTypeRef payload_llvm_type = get_llvm_type(g, payload_type); |
| 6943 | LLVMTypeRef elem_types[2]; |
| 6944 | elem_types[err_union_err_index] = err_set_llvm_type; |
| 6945 | elem_types[err_union_payload_index] = payload_llvm_type; |
| 6946 | type->llvm_type = LLVMStructType(elem_types, 2, false); |
| 6947 | |
| 6948 | ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); |
| 6949 | ZigLLVMDIFile *di_file = nullptr; |
| 6950 | unsigned line = 0; |
| 6951 | type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, |
| 6952 | ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name), |
| 6953 | compile_unit_scope, di_file, line); |
| 6954 | |
| 6955 | uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_llvm_type); |
| 6956 | uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_llvm_type); |
| 6957 | uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, err_union_err_index); |
| 6958 | |
| 6959 | uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_llvm_type); |
| 6960 | uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_llvm_type); |
| 6961 | uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, |
| 6962 | err_union_payload_index); |
| 6963 | |
| 6964 | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type); |
| 6965 | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type); |
| 6966 | |
| 6967 | ZigLLVMDIType *di_element_types[] = { |
| 6968 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6969 | "tag", di_file, line, |
| 6970 | tag_debug_size_in_bits, |
| 6971 | tag_debug_align_in_bits, |
| 6972 | tag_offset_in_bits, |
| 6973 | 0, get_llvm_di_type(g, err_set_type)), |
| 6974 | ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), |
| 6975 | "value", di_file, line, |
| 6976 | value_debug_size_in_bits, |
| 6977 | value_debug_align_in_bits, |
| 6978 | value_offset_in_bits, |
| 6979 | 0, get_llvm_di_type(g, payload_type)), |
| 6980 | }; |
| 6981 | |
| 6982 | ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder, |
| 6983 | compile_unit_scope, |
| 6984 | buf_ptr(&type->name), |
| 6985 | di_file, line, |
| 6986 | debug_size_in_bits, |
| 6987 | debug_align_in_bits, |
| 6988 | 0, |
| 6989 | nullptr, di_element_types, 2, 0, nullptr, ""); |
| 6990 | |
| 6991 | ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type); |
| 6992 | type->llvm_di_type = replacement_di_type; |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | static void resolve_llvm_types_array(CodeGen *g, ZigType *type) { |
| 6997 | if (type->llvm_di_type != nullptr) return; |
| 6998 | |
| 6999 | if (!type_has_bits(type)) { |
| 7000 | type->llvm_type = g->builtin_types.entry_void->llvm_type; |
| 7001 | type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type; |
| 7002 | return; |
| 7003 | } |
| 7004 | |
| 7005 | ZigType *elem_type = type->data.array.child_type; |
| 7006 | |
| 7007 | // TODO https://github.com/ziglang/zig/issues/1424 |
| 7008 | type->llvm_type = LLVMArrayType(get_llvm_type(g, elem_type), (unsigned)type->data.array.len); |
| 7009 | |
| 7010 | uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type); |
| 7011 | uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type); |
| 7012 | |
| 7013 | type->llvm_di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits, |
| 7014 | debug_align_in_bits, get_llvm_di_type(g, elem_type), (int)type->data.array.len); |
| 7015 | } |
| 7016 | |
| 7017 | static void resolve_llvm_types_fn(CodeGen *g, ZigType *fn_type) { |
| 7018 | if (fn_type->llvm_di_type != nullptr) return; |
| 7019 | |
| 7020 | FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id; |
| 7021 | bool first_arg_return = want_first_arg_sret(g, fn_type_id); |
| 7022 | bool is_async = fn_type_id->cc == CallingConventionAsync; |
| 7023 | bool is_c_abi = fn_type_id->cc == CallingConventionC; |
| 7024 | bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id); |
| 7025 | // +1 for maybe making the first argument the return value |
| 7026 | // +1 for maybe first argument the error return trace |
| 7027 | // +2 for maybe arguments async allocator and error code pointer |
| 7028 | ZigList<LLVMTypeRef> gen_param_types = {}; |
| 7029 | // +1 because 0 is the return type and |
| 7030 | // +1 for maybe making first arg ret val and |
| 7031 | // +1 for maybe first argument the error return trace |
| 7032 | // +2 for maybe arguments async allocator and error code pointer |
| 7033 | ZigList<ZigLLVMDIType *> param_di_types = {}; |
| 7034 | param_di_types.append(get_llvm_di_type(g, fn_type_id->return_type)); |
| 7035 | ZigType *gen_return_type; |
| 7036 | if (is_async) { |
| 7037 | gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); |
| 7038 | } else if (!type_has_bits(fn_type_id->return_type)) { |
| 7039 | gen_return_type = g->builtin_types.entry_void; |
| 7040 | } else if (first_arg_return) { |
| 7041 | ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false); |
| 7042 | gen_param_types.append(get_llvm_type(g, gen_type)); |
| 7043 | param_di_types.append(get_llvm_di_type(g, gen_type)); |
| 7044 | gen_return_type = g->builtin_types.entry_void; |
| 7045 | } else { |
| 7046 | gen_return_type = fn_type_id->return_type; |
| 7047 | } |
| 7048 | fn_type->data.fn.gen_return_type = gen_return_type; |
| 7049 | |
| 7050 | if (prefix_arg_error_return_trace) { |
| 7051 | ZigType *gen_type = get_ptr_to_stack_trace_type(g); |
| 7052 | gen_param_types.append(get_llvm_type(g, gen_type)); |
| 7053 | param_di_types.append(get_llvm_di_type(g, gen_type)); |
| 7054 | } |
| 7055 | if (is_async) { |
| 7056 | { |
| 7057 | // async allocator param |
| 7058 | ZigType *gen_type = fn_type_id->async_allocator_type; |
| 7059 | gen_param_types.append(get_llvm_type(g, gen_type)); |
| 7060 | param_di_types.append(get_llvm_di_type(g, gen_type)); |
| 7061 | } |
| 7062 | |
| 7063 | { |
| 7064 | // error code pointer |
| 7065 | ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false); |
| 7066 | gen_param_types.append(get_llvm_type(g, gen_type)); |
| 7067 | param_di_types.append(get_llvm_di_type(g, gen_type)); |
| 7068 | } |
| 7069 | } |
| 7070 | |
| 7071 | fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count); |
| 7072 | for (size_t i = 0; i < fn_type_id->param_count; i += 1) { |
| 7073 | FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i]; |
| 7074 | ZigType *type_entry = src_param_info->type; |
| 7075 | FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i]; |
| 7076 | |
| 7077 | gen_param_info->src_index = i; |
| 7078 | gen_param_info->gen_index = SIZE_MAX; |
| 7079 | |
| 7080 | if (is_c_abi || !type_has_bits(type_entry)) |
| 7081 | continue; |
| 7082 | |
| 7083 | ZigType *gen_type; |
| 7084 | if (handle_is_ptr(type_entry)) { |
| 7085 | gen_type = get_pointer_to_type(g, type_entry, true); |
| 7086 | gen_param_info->is_byval = true; |
| 7087 | } else { |
| 7088 | gen_type = type_entry; |
| 7089 | } |
| 7090 | gen_param_info->gen_index = gen_param_types.length; |
| 7091 | gen_param_info->type = gen_type; |
| 7092 | gen_param_types.append(get_llvm_type(g, gen_type)); |
| 7093 | |
| 7094 | param_di_types.append(get_llvm_di_type(g, gen_type)); |
| 7095 | } |
| 7096 | |
| 7097 | if (is_c_abi) { |
| 7098 | FnWalk fn_walk = {}; |
| 7099 | fn_walk.id = FnWalkIdTypes; |
| 7100 | fn_walk.data.types.param_di_types = &param_di_types; |
| 7101 | fn_walk.data.types.gen_param_types = &gen_param_types; |
| 7102 | walk_function_params(g, fn_type, &fn_walk); |
| 7103 | } |
| 7104 | |
| 7105 | fn_type->data.fn.gen_param_count = gen_param_types.length; |
| 7106 | |
| 7107 | for (size_t i = 0; i < gen_param_types.length; i += 1) { |
| 7108 | assert(gen_param_types.items[i] != nullptr); |
| 7109 | } |
| 7110 | fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type), |
| 7111 | gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args); |
| 7112 | fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0); |
| 7113 | fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0); |
| 7114 | fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type, |
| 7115 | LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type), |
| 7116 | LLVMABIAlignmentOfType(g->target_data_ref, fn_type->llvm_type), ""); |
| 7117 | } |
| 7118 | |
| 7119 | static void resolve_llvm_types_anyerror(CodeGen *g) { |
| 7120 | ZigType *entry = g->builtin_types.entry_global_error_set; |
| 7121 | entry->llvm_type = get_llvm_type(g, g->err_tag_type); |
| 7122 | ZigList<ZigLLVMDIEnumerator *> err_enumerators = {}; |
| 7123 | // reserve index 0 to indicate no error |
| 7124 | err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0)); |
| 7125 | for (size_t i = 1; i < g->errors_by_index.length; i += 1) { |
| 7126 | ErrorTableEntry *error_entry = g->errors_by_index.at(i); |
| 7127 | err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(&error_entry->name), i)); |
| 7128 | } |
| 7129 | |
| 7130 | // create debug type for error sets |
| 7131 | uint64_t tag_debug_size_in_bits = g->err_tag_type->size_in_bits; |
| 7132 | uint64_t tag_debug_align_in_bits = 8*g->err_tag_type->abi_align; |
| 7133 | ZigLLVMDIFile *err_set_di_file = nullptr; |
| 7134 | entry->llvm_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder, |
| 7135 | ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&entry->name), |
| 7136 | err_set_di_file, 0, |
| 7137 | tag_debug_size_in_bits, |
| 7138 | tag_debug_align_in_bits, |
| 7139 | err_enumerators.items, err_enumerators.length, |
| 7140 | get_llvm_di_type(g, g->err_tag_type), ""); |
| 7141 | } |
| 7142 | |
| 7143 | static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) { |
| 7144 | assert(type->id == ZigTypeIdOpaque || type_is_resolved(type, ResolveStatusSizeKnown)); |
| 7145 | assert(wanted_resolve_status > ResolveStatusSizeKnown); |
| 7146 | switch (type->id) { |
| 7147 | case ZigTypeIdInvalid: |
| 7148 | case ZigTypeIdMetaType: |
| 7149 | case ZigTypeIdComptimeFloat: |
| 7150 | case ZigTypeIdComptimeInt: |
| 7151 | case ZigTypeIdEnumLiteral: |
| 7152 | case ZigTypeIdUndefined: |
| 7153 | case ZigTypeIdNull: |
| 7154 | case ZigTypeIdBoundFn: |
| 7155 | case ZigTypeIdArgTuple: |
| 7156 | zig_unreachable(); |
| 7157 | case ZigTypeIdFloat: |
| 7158 | case ZigTypeIdOpaque: |
| 7159 | case ZigTypeIdVoid: |
| 7160 | case ZigTypeIdBool: |
| 7161 | case ZigTypeIdUnreachable: |
| 7162 | assert(type->llvm_di_type != nullptr); |
| 7163 | return; |
| 7164 | case ZigTypeIdStruct: |
| 7165 | if (type->data.structure.is_slice) |
| 7166 | return resolve_llvm_types_slice(g, type, wanted_resolve_status); |
| 7167 | else |
| 7168 | return resolve_llvm_types_struct(g, type, wanted_resolve_status); |
| 7169 | case ZigTypeIdEnum: |
| 7170 | return resolve_llvm_types_enum(g, type); |
| 7171 | case ZigTypeIdUnion: |
| 7172 | return resolve_llvm_types_union(g, type, wanted_resolve_status); |
| 7173 | case ZigTypeIdPointer: |
| 7174 | return resolve_llvm_types_pointer(g, type); |
| 7175 | case ZigTypeIdPromise: { |
| 7176 | if (type->llvm_di_type != nullptr) return; |
| 7177 | ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false); |
| 7178 | type->llvm_type = get_llvm_type(g, u8_ptr_type); |
| 7179 | type->llvm_di_type = get_llvm_di_type(g, u8_ptr_type); |
| 7180 | return; |
| 7181 | } |
| 7182 | case ZigTypeIdInt: |
| 7183 | return resolve_llvm_types_integer(g, type); |
| 7184 | case ZigTypeIdOptional: |
| 7185 | return resolve_llvm_types_optional(g, type); |
| 7186 | case ZigTypeIdErrorUnion: |
| 7187 | return resolve_llvm_types_error_union(g, type); |
| 7188 | case ZigTypeIdArray: |
| 7189 | return resolve_llvm_types_array(g, type); |
| 7190 | case ZigTypeIdFn: |
| 7191 | return resolve_llvm_types_fn(g, type); |
| 7192 | case ZigTypeIdErrorSet: { |
| 7193 | if (type->llvm_di_type != nullptr) return; |
| 7194 | |
| 7195 | if (g->builtin_types.entry_global_error_set->llvm_type == nullptr) { |
| 7196 | resolve_llvm_types_anyerror(g); |
| 7197 | } |
| 7198 | type->llvm_type = g->builtin_types.entry_global_error_set->llvm_type; |
| 7199 | type->llvm_di_type = g->builtin_types.entry_global_error_set->llvm_di_type; |
| 7200 | return; |
| 7201 | } |
| 7202 | case ZigTypeIdVector: { |
| 7203 | if (type->llvm_di_type != nullptr) return; |
| 7204 | |
| 7205 | type->llvm_type = LLVMVectorType(get_llvm_type(g, type->data.vector.elem_type), type->data.vector.len); |
| 7206 | type->llvm_di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, type->size_in_bits, |
| 7207 | type->abi_align, get_llvm_di_type(g, type->data.vector.elem_type), type->data.vector.len); |
| 7208 | return; |
| 7209 | } |
| 7210 | } |
| 7211 | zig_unreachable(); |
| 7212 | } |
| 7213 | |
| 7214 | LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) { |
| 7215 | assertNoError(type_resolve(g, type, ResolveStatusLLVMFull)); |
| 7216 | assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type)); |
| 7217 | assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type)); |
| 7218 | return type->llvm_type; |
| 7219 | } |
| 7220 | |
| 7221 | ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) { |
| 7222 | assertNoError(type_resolve(g, type, ResolveStatusLLVMFull)); |
| 7223 | return type->llvm_di_type; |
| 7224 | } |