| ... | @@ -7,7 +7,11 @@ const math = std.math; | ... | @@ -7,7 +7,11 @@ const math = std.math; |
| 7 | const native_endian = builtin.cpu.arch.endian(); | 7 | const native_endian = builtin.cpu.arch.endian(); |
| 8 | const DW = std.dwarf; | 8 | const DW = std.dwarf; |
| 9 | | 9 | |
| 10 | const llvm = @import("llvm/bindings.zig"); | 10 | const Builder = @import("llvm/Builder.zig"); |
| | 11 | const llvm = if (build_options.have_llvm or true) |
| | 12 | @import("llvm/bindings.zig") |
| | 13 | else |
| | 14 | @compileError("LLVM unavailable"); |
| 11 | const link = @import("../link.zig"); | 15 | const link = @import("../link.zig"); |
| 12 | const Compilation = @import("../Compilation.zig"); | 16 | const Compilation = @import("../Compilation.zig"); |
| 13 | const build_options = @import("build_options"); | 17 | const build_options = @import("build_options"); |
| ... | @@ -34,7 +38,7 @@ const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev; | ... | @@ -34,7 +38,7 @@ const compilerRtIntAbbrev = target_util.compilerRtIntAbbrev; |
| 34 | | 38 | |
| 35 | const Error = error{ OutOfMemory, CodegenFail }; | 39 | const Error = error{ OutOfMemory, CodegenFail }; |
| 36 | | 40 | |
| 37 | pub fn targetTriple(allocator: Allocator, target: std.Target) ![:0]u8 { | 41 | pub fn targetTriple(allocator: Allocator, target: std.Target) ![]const u8 { |
| 38 | var llvm_triple = std.ArrayList(u8).init(allocator); | 42 | var llvm_triple = std.ArrayList(u8).init(allocator); |
| 39 | defer llvm_triple.deinit(); | 43 | defer llvm_triple.deinit(); |
| 40 | | 44 | |
| ... | @@ -207,7 +211,7 @@ pub fn targetTriple(allocator: Allocator, target: std.Target) ![:0]u8 { | ... | @@ -207,7 +211,7 @@ pub fn targetTriple(allocator: Allocator, target: std.Target) ![:0]u8 { |
| 207 | }; | 211 | }; |
| 208 | try llvm_triple.appendSlice(llvm_abi); | 212 | try llvm_triple.appendSlice(llvm_abi); |
| 209 | | 213 | |
| 210 | return llvm_triple.toOwnedSliceSentinel(0); | 214 | return llvm_triple.toOwnedSlice(); |
| 211 | } | 215 | } |
| 212 | | 216 | |
| 213 | pub fn targetOs(os_tag: std.Target.Os.Tag) llvm.OSType { | 217 | pub fn targetOs(os_tag: std.Target.Os.Tag) llvm.OSType { |
| ... | @@ -327,17 +331,363 @@ pub fn supportsTailCall(target: std.Target) bool { | ... | @@ -327,17 +331,363 @@ pub fn supportsTailCall(target: std.Target) bool { |
| 327 | } | 331 | } |
| 328 | } | 332 | } |
| 329 | | 333 | |
| 330 | /// TODO can this be done with simpler logic / different API binding? | 334 | const DataLayoutBuilder = struct { |
| 331 | fn deleteLlvmGlobal(llvm_global: *llvm.Value) void { | 335 | target: std.Target, |
| 332 | if (llvm_global.globalGetValueType().getTypeKind() == .Function) { | 336 | |
| 333 | llvm_global.deleteFunction(); | 337 | pub fn format( |
| 334 | return; | 338 | self: DataLayoutBuilder, |
| | 339 | comptime _: []const u8, |
| | 340 | _: std.fmt.FormatOptions, |
| | 341 | writer: anytype, |
| | 342 | ) @TypeOf(writer).Error!void { |
| | 343 | const is_aarch64_windows = self.target.cpu.arch == .aarch64 and self.target.os.tag == .windows; |
| | 344 | try writer.writeByte(switch (self.target.cpu.arch.endian()) { |
| | 345 | .Little => 'e', |
| | 346 | .Big => 'E', |
| | 347 | }); |
| | 348 | switch (self.target.cpu.arch) { |
| | 349 | .amdgcn, |
| | 350 | .nvptx, |
| | 351 | .nvptx64, |
| | 352 | => {}, |
| | 353 | .avr => try writer.writeAll("-P1"), |
| | 354 | else => try writer.print("-m:{c}", .{@as(u8, switch (self.target.cpu.arch) { |
| | 355 | .mips, .mipsel => 'm', // Mips mangling: Private symbols get a $ prefix. |
| | 356 | else => switch (self.target.ofmt) { |
| | 357 | .elf => 'e', // ELF mangling: Private symbols get a `.L` prefix. |
| | 358 | //.goff => 'l', // GOFF mangling: Private symbols get a `@` prefix. |
| | 359 | .macho => 'o', // Mach-O mangling: Private symbols get `L` prefix. |
| | 360 | // Other symbols get a `_` prefix. |
| | 361 | .coff => switch (self.target.os.tag) { |
| | 362 | .windows => switch (self.target.cpu.arch) { |
| | 363 | .x86 => 'x', // Windows x86 COFF mangling: Private symbols get the usual |
| | 364 | // prefix. Regular C symbols get a `_` prefix. Functions with `__stdcall`, |
| | 365 | //`__fastcall`, and `__vectorcall` have custom mangling that appends `@N` |
| | 366 | // where N is the number of bytes used to pass parameters. C++ symbols |
| | 367 | // starting with `?` are not mangled in any way. |
| | 368 | else => 'w', // Windows COFF mangling: Similar to x, except that normal C |
| | 369 | // symbols do not receive a `_` prefix. |
| | 370 | }, |
| | 371 | else => 'e', |
| | 372 | }, |
| | 373 | //.xcoff => 'a', // XCOFF mangling: Private symbols get a `L..` prefix. |
| | 374 | else => 'e', |
| | 375 | }, |
| | 376 | })}), |
| | 377 | } |
| | 378 | var any_non_integral = false; |
| | 379 | const ptr_bit_width = self.target.ptrBitWidth(); |
| | 380 | var default_info = struct { size: u16, abi: u16, pref: u16, idx: u16 }{ |
| | 381 | .size = 64, |
| | 382 | .abi = 64, |
| | 383 | .pref = 64, |
| | 384 | .idx = 64, |
| | 385 | }; |
| | 386 | const addr_space_info = llvmAddrSpaceInfo(self.target); |
| | 387 | for (addr_space_info, 0..) |info, i| { |
| | 388 | assert((info.llvm == .default) == (i == 0)); |
| | 389 | if (info.non_integral) { |
| | 390 | assert(info.llvm != .default); |
| | 391 | any_non_integral = true; |
| | 392 | } |
| | 393 | const size = info.size orelse ptr_bit_width; |
| | 394 | const abi = info.abi orelse ptr_bit_width; |
| | 395 | const pref = info.pref orelse abi; |
| | 396 | const idx = info.idx orelse size; |
| | 397 | const matches_default = |
| | 398 | size == default_info.size and |
| | 399 | abi == default_info.abi and |
| | 400 | pref == default_info.pref and |
| | 401 | idx == default_info.idx; |
| | 402 | if (info.llvm == .default) default_info = .{ |
| | 403 | .size = size, |
| | 404 | .abi = abi, |
| | 405 | .pref = pref, |
| | 406 | .idx = idx, |
| | 407 | }; |
| | 408 | if (self.target.cpu.arch == .aarch64_32) continue; |
| | 409 | if (!info.force_in_data_layout and matches_default and |
| | 410 | self.target.cpu.arch != .riscv64 and !is_aarch64_windows and |
| | 411 | self.target.cpu.arch != .bpfeb and self.target.cpu.arch != .bpfel) continue; |
| | 412 | try writer.writeAll("-p"); |
| | 413 | if (info.llvm != .default) try writer.print("{d}", .{@intFromEnum(info.llvm)}); |
| | 414 | try writer.print(":{d}:{d}", .{ size, abi }); |
| | 415 | if (pref != abi or idx != size or self.target.cpu.arch == .hexagon) { |
| | 416 | try writer.print(":{d}", .{pref}); |
| | 417 | if (idx != size) try writer.print(":{d}", .{idx}); |
| | 418 | } |
| | 419 | } |
| | 420 | if (self.target.cpu.arch.isARM() or self.target.cpu.arch.isThumb()) |
| | 421 | try writer.writeAll("-Fi8"); // for thumb interwork |
| | 422 | if (self.target.cpu.arch != .hexagon) { |
| | 423 | if (self.target.cpu.arch == .s390x) try self.typeAlignment(.integer, 1, 8, 8, false, writer); |
| | 424 | try self.typeAlignment(.integer, 8, 8, 8, false, writer); |
| | 425 | try self.typeAlignment(.integer, 16, 16, 16, false, writer); |
| | 426 | try self.typeAlignment(.integer, 32, if (is_aarch64_windows) 0 else 32, 32, false, writer); |
| | 427 | try self.typeAlignment(.integer, 64, 32, 64, false, writer); |
| | 428 | try self.typeAlignment(.integer, 128, 32, 64, false, writer); |
| | 429 | if (backendSupportsF16(self.target)) try self.typeAlignment(.float, 16, 16, 16, false, writer); |
| | 430 | try self.typeAlignment(.float, 32, 32, 32, false, writer); |
| | 431 | try self.typeAlignment(.float, 64, 64, 64, false, writer); |
| | 432 | if (backendSupportsF80(self.target)) try self.typeAlignment(.float, 80, 0, 0, false, writer); |
| | 433 | try self.typeAlignment(.float, 128, 128, 128, false, writer); |
| | 434 | } |
| | 435 | switch (self.target.cpu.arch) { |
| | 436 | .amdgcn => { |
| | 437 | try self.typeAlignment(.vector, 16, 16, 16, false, writer); |
| | 438 | try self.typeAlignment(.vector, 24, 32, 32, false, writer); |
| | 439 | try self.typeAlignment(.vector, 32, 32, 32, false, writer); |
| | 440 | try self.typeAlignment(.vector, 48, 64, 64, false, writer); |
| | 441 | try self.typeAlignment(.vector, 96, 128, 128, false, writer); |
| | 442 | try self.typeAlignment(.vector, 192, 256, 256, false, writer); |
| | 443 | try self.typeAlignment(.vector, 256, 256, 256, false, writer); |
| | 444 | try self.typeAlignment(.vector, 512, 512, 512, false, writer); |
| | 445 | try self.typeAlignment(.vector, 1024, 1024, 1024, false, writer); |
| | 446 | try self.typeAlignment(.vector, 2048, 2048, 2048, false, writer); |
| | 447 | }, |
| | 448 | .ve => {}, |
| | 449 | else => { |
| | 450 | try self.typeAlignment(.vector, 16, 32, 32, false, writer); |
| | 451 | try self.typeAlignment(.vector, 32, 32, 32, false, writer); |
| | 452 | try self.typeAlignment(.vector, 64, 64, 64, false, writer); |
| | 453 | try self.typeAlignment(.vector, 128, 128, 128, true, writer); |
| | 454 | }, |
| | 455 | } |
| | 456 | if (self.target.os.tag != .windows and self.target.cpu.arch != .avr) |
| | 457 | try self.typeAlignment(.aggregate, 0, 0, 64, false, writer); |
| | 458 | for (@as([]const u24, switch (self.target.cpu.arch) { |
| | 459 | .avr => &.{8}, |
| | 460 | .msp430 => &.{ 8, 16 }, |
| | 461 | .arm, |
| | 462 | .armeb, |
| | 463 | .mips, |
| | 464 | .mipsel, |
| | 465 | .powerpc, |
| | 466 | .powerpcle, |
| | 467 | .riscv32, |
| | 468 | .sparc, |
| | 469 | .sparcel, |
| | 470 | .thumb, |
| | 471 | .thumbeb, |
| | 472 | => &.{32}, |
| | 473 | .aarch64, |
| | 474 | .aarch64_be, |
| | 475 | .aarch64_32, |
| | 476 | .amdgcn, |
| | 477 | .bpfeb, |
| | 478 | .bpfel, |
| | 479 | .mips64, |
| | 480 | .mips64el, |
| | 481 | .powerpc64, |
| | 482 | .powerpc64le, |
| | 483 | .riscv64, |
| | 484 | .s390x, |
| | 485 | .sparc64, |
| | 486 | .ve, |
| | 487 | .wasm32, |
| | 488 | .wasm64, |
| | 489 | => &.{ 32, 64 }, |
| | 490 | .hexagon => &.{ 16, 32 }, |
| | 491 | .x86 => &.{ 8, 16, 32 }, |
| | 492 | .nvptx, |
| | 493 | .nvptx64, |
| | 494 | => &.{ 16, 32, 64 }, |
| | 495 | .x86_64 => &.{ 8, 16, 32, 64 }, |
| | 496 | else => &.{}, |
| | 497 | }), 0..) |natural, index| switch (index) { |
| | 498 | 0 => try writer.print("-n{d}", .{natural}), |
| | 499 | else => try writer.print(":{d}", .{natural}), |
| | 500 | }; |
| | 501 | if (self.target.cpu.arch == .hexagon) { |
| | 502 | try self.typeAlignment(.integer, 64, 64, 64, true, writer); |
| | 503 | try self.typeAlignment(.integer, 32, 32, 32, true, writer); |
| | 504 | try self.typeAlignment(.integer, 16, 16, 16, true, writer); |
| | 505 | try self.typeAlignment(.integer, 1, 8, 8, true, writer); |
| | 506 | try self.typeAlignment(.float, 32, 32, 32, true, writer); |
| | 507 | try self.typeAlignment(.float, 64, 64, 64, true, writer); |
| | 508 | } |
| | 509 | if (self.target.os.tag == .windows or self.target.cpu.arch == .avr) |
| | 510 | try self.typeAlignment(.aggregate, 0, 0, 64, false, writer); |
| | 511 | const stack_abi = self.target.stackAlignment() * 8; |
| | 512 | if (self.target.os.tag == .windows or self.target.cpu.arch == .msp430 or |
| | 513 | stack_abi != ptr_bit_width) |
| | 514 | try writer.print("-S{d}", .{stack_abi}); |
| | 515 | switch (self.target.cpu.arch) { |
| | 516 | .hexagon, .ve => { |
| | 517 | try self.typeAlignment(.vector, 32, 128, 128, true, writer); |
| | 518 | try self.typeAlignment(.vector, 64, 128, 128, true, writer); |
| | 519 | try self.typeAlignment(.vector, 128, 128, 128, true, writer); |
| | 520 | }, |
| | 521 | else => {}, |
| | 522 | } |
| | 523 | if (self.target.cpu.arch != .amdgcn) { |
| | 524 | try self.typeAlignment(.vector, 256, 128, 128, true, writer); |
| | 525 | try self.typeAlignment(.vector, 512, 128, 128, true, writer); |
| | 526 | try self.typeAlignment(.vector, 1024, 128, 128, true, writer); |
| | 527 | try self.typeAlignment(.vector, 2048, 128, 128, true, writer); |
| | 528 | try self.typeAlignment(.vector, 4096, 128, 128, true, writer); |
| | 529 | try self.typeAlignment(.vector, 8192, 128, 128, true, writer); |
| | 530 | try self.typeAlignment(.vector, 16384, 128, 128, true, writer); |
| | 531 | } |
| | 532 | const alloca_addr_space = llvmAllocaAddressSpace(self.target); |
| | 533 | if (alloca_addr_space != .default) try writer.print("-A{d}", .{@intFromEnum(alloca_addr_space)}); |
| | 534 | const global_addr_space = llvmDefaultGlobalAddressSpace(self.target); |
| | 535 | if (global_addr_space != .default) try writer.print("-G{d}", .{@intFromEnum(global_addr_space)}); |
| | 536 | if (any_non_integral) { |
| | 537 | try writer.writeAll("-ni"); |
| | 538 | for (addr_space_info) |info| if (info.non_integral) |
| | 539 | try writer.print(":{d}", .{@intFromEnum(info.llvm)}); |
| | 540 | } |
| | 541 | } |
| | 542 | |
| | 543 | fn typeAlignment( |
| | 544 | self: DataLayoutBuilder, |
| | 545 | kind: enum { integer, vector, float, aggregate }, |
| | 546 | size: u24, |
| | 547 | default_abi: u24, |
| | 548 | default_pref: u24, |
| | 549 | default_force_pref: bool, |
| | 550 | writer: anytype, |
| | 551 | ) @TypeOf(writer).Error!void { |
| | 552 | var abi = default_abi; |
| | 553 | var pref = default_pref; |
| | 554 | var force_abi = false; |
| | 555 | var force_pref = default_force_pref; |
| | 556 | if (kind == .float and size == 80) { |
| | 557 | abi = 128; |
| | 558 | pref = 128; |
| | 559 | } |
| | 560 | for (@as([]const std.Target.CType, switch (kind) { |
| | 561 | .integer => &.{ .char, .short, .int, .long, .longlong }, |
| | 562 | .float => &.{ .float, .double, .longdouble }, |
| | 563 | .vector, .aggregate => &.{}, |
| | 564 | })) |cty| { |
| | 565 | if (self.target.c_type_bit_size(cty) != size) continue; |
| | 566 | abi = self.target.c_type_alignment(cty) * 8; |
| | 567 | pref = self.target.c_type_preferred_alignment(cty) * 8; |
| | 568 | break; |
| | 569 | } |
| | 570 | switch (kind) { |
| | 571 | .integer => { |
| | 572 | if (self.target.ptrBitWidth() <= 16 and size >= 128) return; |
| | 573 | abi = @min(abi, self.target.maxIntAlignment() * 8); |
| | 574 | switch (self.target.os.tag) { |
| | 575 | .linux => switch (self.target.cpu.arch) { |
| | 576 | .aarch64, |
| | 577 | .aarch64_be, |
| | 578 | .aarch64_32, |
| | 579 | .mips, |
| | 580 | .mipsel, |
| | 581 | => pref = @max(pref, 32), |
| | 582 | else => {}, |
| | 583 | }, |
| | 584 | else => {}, |
| | 585 | } |
| | 586 | switch (self.target.cpu.arch) { |
| | 587 | .aarch64, |
| | 588 | .aarch64_be, |
| | 589 | .aarch64_32, |
| | 590 | .bpfeb, |
| | 591 | .bpfel, |
| | 592 | .nvptx, |
| | 593 | .nvptx64, |
| | 594 | .riscv64, |
| | 595 | => if (size == 128) { |
| | 596 | abi = size; |
| | 597 | pref = size; |
| | 598 | }, |
| | 599 | .hexagon => force_abi = true, |
| | 600 | .mips64, |
| | 601 | .mips64el, |
| | 602 | => if (size <= 32) { |
| | 603 | pref = 32; |
| | 604 | }, |
| | 605 | .s390x => if (size <= 16) { |
| | 606 | pref = 16; |
| | 607 | }, |
| | 608 | .ve => if (size == 64) { |
| | 609 | abi = size; |
| | 610 | pref = size; |
| | 611 | }, |
| | 612 | else => {}, |
| | 613 | } |
| | 614 | }, |
| | 615 | .vector => if (self.target.cpu.arch.isARM() or self.target.cpu.arch.isThumb()) { |
| | 616 | switch (size) { |
| | 617 | 128 => abi = 64, |
| | 618 | else => {}, |
| | 619 | } |
| | 620 | } else if ((self.target.cpu.arch.isPPC64() and (size == 256 or size == 512)) or |
| | 621 | (self.target.cpu.arch.isNvptx() and (size == 16 or size == 32))) |
| | 622 | { |
| | 623 | force_abi = true; |
| | 624 | abi = size; |
| | 625 | pref = size; |
| | 626 | } else if (self.target.cpu.arch == .amdgcn and size <= 2048) { |
| | 627 | force_abi = true; |
| | 628 | } else if (self.target.cpu.arch == .hexagon and |
| | 629 | ((size >= 32 and size <= 64) or (size >= 512 and size <= 2048))) |
| | 630 | { |
| | 631 | abi = size; |
| | 632 | pref = size; |
| | 633 | force_pref = true; |
| | 634 | } else if (self.target.cpu.arch == .s390x and size == 128) { |
| | 635 | abi = 64; |
| | 636 | pref = 64; |
| | 637 | force_pref = false; |
| | 638 | } else if (self.target.cpu.arch == .ve and (size >= 64 and size <= 16384)) { |
| | 639 | abi = 64; |
| | 640 | pref = 64; |
| | 641 | force_abi = true; |
| | 642 | force_pref = true; |
| | 643 | }, |
| | 644 | .float => switch (self.target.cpu.arch) { |
| | 645 | .avr, .msp430, .sparc64 => if (size != 32 and size != 64) return, |
| | 646 | .hexagon => if (size == 32 or size == 64) { |
| | 647 | force_abi = true; |
| | 648 | }, |
| | 649 | .aarch64_32 => if (size == 128) { |
| | 650 | abi = size; |
| | 651 | pref = size; |
| | 652 | }, |
| | 653 | .ve => if (size == 64) { |
| | 654 | abi = size; |
| | 655 | pref = size; |
| | 656 | }, |
| | 657 | else => {}, |
| | 658 | }, |
| | 659 | .aggregate => if (self.target.os.tag == .windows or |
| | 660 | self.target.cpu.arch.isARM() or self.target.cpu.arch.isThumb()) |
| | 661 | { |
| | 662 | pref = @min(pref, self.target.ptrBitWidth()); |
| | 663 | } else if (self.target.cpu.arch == .hexagon) { |
| | 664 | abi = 0; |
| | 665 | pref = 0; |
| | 666 | } else if (self.target.cpu.arch == .s390x) { |
| | 667 | abi = 8; |
| | 668 | pref = 16; |
| | 669 | } else if (self.target.cpu.arch == .msp430) { |
| | 670 | abi = 8; |
| | 671 | pref = 8; |
| | 672 | }, |
| | 673 | } |
| | 674 | if (kind != .vector and self.target.cpu.arch == .avr) { |
| | 675 | force_abi = true; |
| | 676 | abi = 8; |
| | 677 | pref = 8; |
| | 678 | } |
| | 679 | if (!force_abi and abi == default_abi and pref == default_pref) return; |
| | 680 | try writer.print("-{c}", .{@tagName(kind)[0]}); |
| | 681 | if (size != 0) try writer.print("{d}", .{size}); |
| | 682 | try writer.print(":{d}", .{abi}); |
| | 683 | if (pref != abi or force_pref) try writer.print(":{d}", .{pref}); |
| 335 | } | 684 | } |
| 336 | return llvm_global.deleteGlobal(); | 685 | }; |
| 337 | } | | |
| 338 | | 686 | |
| 339 | pub const Object = struct { | 687 | pub const Object = struct { |
| 340 | gpa: Allocator, | 688 | gpa: Allocator, |
| | 689 | builder: Builder, |
| | 690 | |
| 341 | module: *Module, | 691 | module: *Module, |
| 342 | llvm_module: *llvm.Module, | 692 | llvm_module: *llvm.Module, |
| 343 | di_builder: ?*llvm.DIBuilder, | 693 | di_builder: ?*llvm.DIBuilder, |
| ... | @@ -347,7 +697,6 @@ pub const Object = struct { | ... | @@ -347,7 +697,6 @@ pub const Object = struct { |
| 347 | /// - *Module.Decl (Non-Fn) => *DIGlobalVariable | 697 | /// - *Module.Decl (Non-Fn) => *DIGlobalVariable |
| 348 | di_map: std.AutoHashMapUnmanaged(*const anyopaque, *llvm.DINode), | 698 | di_map: std.AutoHashMapUnmanaged(*const anyopaque, *llvm.DINode), |
| 349 | di_compile_unit: ?*llvm.DICompileUnit, | 699 | di_compile_unit: ?*llvm.DICompileUnit, |
| 350 | context: *llvm.Context, | | |
| 351 | target_machine: *llvm.TargetMachine, | 700 | target_machine: *llvm.TargetMachine, |
| 352 | target_data: *llvm.TargetData, | 701 | target_data: *llvm.TargetData, |
| 353 | target: std.Target, | 702 | target: std.Target, |
| ... | @@ -359,9 +708,9 @@ pub const Object = struct { | ... | @@ -359,9 +708,9 @@ pub const Object = struct { |
| 359 | /// version of the name and incorrectly get function not found in the llvm module. | 708 | /// version of the name and incorrectly get function not found in the llvm module. |
| 360 | /// * it works for functions not all globals. | 709 | /// * it works for functions not all globals. |
| 361 | /// Therefore, this table keeps track of the mapping. | 710 | /// Therefore, this table keeps track of the mapping. |
| 362 | decl_map: std.AutoHashMapUnmanaged(Module.Decl.Index, *llvm.Value), | 711 | decl_map: std.AutoHashMapUnmanaged(Module.Decl.Index, Builder.Global.Index), |
| 363 | /// Serves the same purpose as `decl_map` but only used for the `is_named_enum_value` instruction. | 712 | /// Serves the same purpose as `decl_map` but only used for the `is_named_enum_value` instruction. |
| 364 | named_enum_map: std.AutoHashMapUnmanaged(Module.Decl.Index, *llvm.Value), | 713 | named_enum_map: std.AutoHashMapUnmanaged(Module.Decl.Index, Builder.Function.Index), |
| 365 | /// Maps Zig types to LLVM types. The table memory is backed by the GPA of | 714 | /// Maps Zig types to LLVM types. The table memory is backed by the GPA of |
| 366 | /// the compiler. | 715 | /// the compiler. |
| 367 | /// TODO when InternPool garbage collection is implemented, this map needs | 716 | /// TODO when InternPool garbage collection is implemented, this map needs |
| ... | @@ -371,16 +720,16 @@ pub const Object = struct { | ... | @@ -371,16 +720,16 @@ pub const Object = struct { |
| 371 | /// The LLVM global table which holds the names corresponding to Zig errors. | 720 | /// The LLVM global table which holds the names corresponding to Zig errors. |
| 372 | /// Note that the values are not added until flushModule, when all errors in | 721 | /// Note that the values are not added until flushModule, when all errors in |
| 373 | /// the compilation are known. | 722 | /// the compilation are known. |
| 374 | error_name_table: ?*llvm.Value, | 723 | error_name_table: Builder.Variable.Index, |
| 375 | /// This map is usually very close to empty. It tracks only the cases when a | 724 | /// This map is usually very close to empty. It tracks only the cases when a |
| 376 | /// second extern Decl could not be emitted with the correct name due to a | 725 | /// second extern Decl could not be emitted with the correct name due to a |
| 377 | /// name collision. | 726 | /// name collision. |
| 378 | extern_collisions: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, void), | 727 | extern_collisions: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, void), |
| 379 | | 728 | |
| 380 | /// Memoizes a null `?usize` value. | 729 | /// Memoizes a null `?usize` value. |
| 381 | null_opt_addr: ?*llvm.Value, | 730 | null_opt_usize: Builder.Constant, |
| 382 | | 731 | |
| 383 | pub const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, *llvm.Type); | 732 | pub const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, Builder.Type); |
| 384 | | 733 | |
| 385 | /// This is an ArrayHashMap as opposed to a HashMap because in `flushModule` we | 734 | /// This is an ArrayHashMap as opposed to a HashMap because in `flushModule` we |
| 386 | /// want to iterate over it while adding entries to it. | 735 | /// want to iterate over it while adding entries to it. |
| ... | @@ -394,138 +743,137 @@ pub const Object = struct { | ... | @@ -394,138 +743,137 @@ pub const Object = struct { |
| 394 | } | 743 | } |
| 395 | | 744 | |
| 396 | pub fn init(gpa: Allocator, options: link.Options) !Object { | 745 | pub fn init(gpa: Allocator, options: link.Options) !Object { |
| 397 | const context = llvm.Context.create(); | | |
| 398 | errdefer context.dispose(); | | |
| 399 | | | |
| 400 | initializeLLVMTarget(options.target.cpu.arch); | | |
| 401 | | | |
| 402 | const llvm_module = llvm.Module.createWithName(options.root_name.ptr, context); | | |
| 403 | errdefer llvm_module.dispose(); | | |
| 404 | | | |
| 405 | const llvm_target_triple = try targetTriple(gpa, options.target); | 746 | const llvm_target_triple = try targetTriple(gpa, options.target); |
| 406 | defer gpa.free(llvm_target_triple); | 747 | defer gpa.free(llvm_target_triple); |
| 407 | | 748 | |
| 408 | var error_message: [*:0]const u8 = undefined; | 749 | var builder = try Builder.init(.{ |
| 409 | var target: *llvm.Target = undefined; | 750 | .allocator = gpa, |
| 410 | if (llvm.Target.getFromTriple(llvm_target_triple.ptr, &target, &error_message).toBool()) { | 751 | .use_lib_llvm = options.use_lib_llvm, |
| 411 | defer llvm.disposeMessage(error_message); | 752 | .strip = options.strip, |
| 412 | | 753 | .name = options.root_name, |
| 413 | log.err("LLVM failed to parse '{s}': {s}", .{ llvm_target_triple, error_message }); | 754 | .target = options.target, |
| 414 | return error.InvalidLlvmTriple; | 755 | .triple = llvm_target_triple, |
| 415 | } | 756 | }); |
| | 757 | errdefer builder.deinit(); |
| | 758 | |
| | 759 | var target_machine: *llvm.TargetMachine = undefined; |
| | 760 | var target_data: *llvm.TargetData = undefined; |
| | 761 | if (builder.useLibLlvm()) { |
| | 762 | if (!options.strip) { |
| | 763 | switch (options.target.ofmt) { |
| | 764 | .coff => builder.llvm.module.?.addModuleCodeViewFlag(), |
| | 765 | else => builder.llvm.module.?.addModuleDebugInfoFlag(options.dwarf_format == std.dwarf.Format.@"64"), |
| | 766 | } |
| | 767 | builder.llvm.di_builder = builder.llvm.module.?.createDIBuilder(true); |
| | 768 | |
| | 769 | // Don't use the version string here; LLVM misparses it when it |
| | 770 | // includes the git revision. |
| | 771 | const producer = try builder.fmt("zig {d}.{d}.{d}", .{ |
| | 772 | build_options.semver.major, |
| | 773 | build_options.semver.minor, |
| | 774 | build_options.semver.patch, |
| | 775 | }); |
| 416 | | 776 | |
| 417 | llvm_module.setTarget(llvm_target_triple.ptr); | 777 | // We fully resolve all paths at this point to avoid lack of source line info in stack |
| 418 | var opt_di_builder: ?*llvm.DIBuilder = null; | 778 | // traces or lack of debugging information which, if relative paths were used, would |
| 419 | errdefer if (opt_di_builder) |di_builder| di_builder.dispose(); | 779 | // be very location dependent. |
| | 780 | // TODO: the only concern I have with this is WASI as either host or target, should |
| | 781 | // we leave the paths as relative then? |
| | 782 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| | 783 | const compile_unit_dir = blk: { |
| | 784 | const path = d: { |
| | 785 | const mod = options.module orelse break :d "."; |
| | 786 | break :d mod.root_pkg.root_src_directory.path orelse "."; |
| | 787 | }; |
| | 788 | if (std.fs.path.isAbsolute(path)) break :blk path; |
| | 789 | break :blk std.os.realpath(path, &buf) catch path; // If realpath fails, fallback to whatever path was |
| | 790 | }; |
| | 791 | const compile_unit_dir_z = try builder.gpa.dupeZ(u8, compile_unit_dir); |
| | 792 | defer builder.gpa.free(compile_unit_dir_z); |
| | 793 | |
| | 794 | builder.llvm.di_compile_unit = builder.llvm.di_builder.?.createCompileUnit( |
| | 795 | DW.LANG.C99, |
| | 796 | builder.llvm.di_builder.?.createFile(options.root_name, compile_unit_dir_z), |
| | 797 | producer.toSlice(&builder).?, |
| | 798 | options.optimize_mode != .Debug, |
| | 799 | "", // flags |
| | 800 | 0, // runtime version |
| | 801 | "", // split name |
| | 802 | 0, // dwo id |
| | 803 | true, // emit debug info |
| | 804 | ); |
| | 805 | } |
| 420 | | 806 | |
| 421 | var di_compile_unit: ?*llvm.DICompileUnit = null; | 807 | const opt_level: llvm.CodeGenOptLevel = if (options.optimize_mode == .Debug) |
| | 808 | .None |
| | 809 | else |
| | 810 | .Aggressive; |
| 422 | | 811 | |
| 423 | if (!options.strip) { | 812 | const reloc_mode: llvm.RelocMode = if (options.pic) |
| 424 | switch (options.target.ofmt) { | 813 | .PIC |
| 425 | .coff => llvm_module.addModuleCodeViewFlag(), | 814 | else if (options.link_mode == .Dynamic) |
| 426 | else => llvm_module.addModuleDebugInfoFlag(options.dwarf_format == std.dwarf.Format.@"64"), | 815 | llvm.RelocMode.DynamicNoPIC |
| 427 | } | 816 | else |
| 428 | const di_builder = llvm_module.createDIBuilder(true); | 817 | .Static; |
| 429 | opt_di_builder = di_builder; | 818 | |
| 430 | | 819 | const code_model: llvm.CodeModel = switch (options.machine_code_model) { |
| 431 | // Don't use the version string here; LLVM misparses it when it | 820 | .default => .Default, |
| 432 | // includes the git revision. | 821 | .tiny => .Tiny, |
| 433 | const producer = try std.fmt.allocPrintZ(gpa, "zig {d}.{d}.{d}", .{ | 822 | .small => .Small, |
| 434 | build_options.semver.major, | 823 | .kernel => .Kernel, |
| 435 | build_options.semver.minor, | 824 | .medium => .Medium, |
| 436 | build_options.semver.patch, | 825 | .large => .Large, |
| 437 | }); | | |
| 438 | defer gpa.free(producer); | | |
| 439 | | | |
| 440 | // We fully resolve all paths at this point to avoid lack of source line info in stack | | |
| 441 | // traces or lack of debugging information which, if relative paths were used, would | | |
| 442 | // be very location dependent. | | |
| 443 | // TODO: the only concern I have with this is WASI as either host or target, should | | |
| 444 | // we leave the paths as relative then? | | |
| 445 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | | |
| 446 | const compile_unit_dir = blk: { | | |
| 447 | const path = d: { | | |
| 448 | const mod = options.module orelse break :d "."; | | |
| 449 | break :d mod.root_pkg.root_src_directory.path orelse "."; | | |
| 450 | }; | | |
| 451 | if (std.fs.path.isAbsolute(path)) break :blk path; | | |
| 452 | break :blk std.os.realpath(path, &buf) catch path; // If realpath fails, fallback to whatever path was | | |
| 453 | }; | 826 | }; |
| 454 | const compile_unit_dir_z = try gpa.dupeZ(u8, compile_unit_dir); | | |
| 455 | defer gpa.free(compile_unit_dir_z); | | |
| 456 | | | |
| 457 | di_compile_unit = di_builder.createCompileUnit( | | |
| 458 | DW.LANG.C99, | | |
| 459 | di_builder.createFile(options.root_name, compile_unit_dir_z), | | |
| 460 | producer, | | |
| 461 | options.optimize_mode != .Debug, | | |
| 462 | "", // flags | | |
| 463 | 0, // runtime version | | |
| 464 | "", // split name | | |
| 465 | 0, // dwo id | | |
| 466 | true, // emit debug info | | |
| 467 | ); | | |
| 468 | } | | |
| 469 | | | |
| 470 | const opt_level: llvm.CodeGenOptLevel = if (options.optimize_mode == .Debug) | | |
| 471 | .None | | |
| 472 | else | | |
| 473 | .Aggressive; | | |
| 474 | | 827 | |
| 475 | const reloc_mode: llvm.RelocMode = if (options.pic) | 828 | // TODO handle float ABI better- it should depend on the ABI portion of std.Target |
| 476 | .PIC | 829 | const float_abi: llvm.ABIType = .Default; |
| 477 | else if (options.link_mode == .Dynamic) | 830 | |
| 478 | llvm.RelocMode.DynamicNoPIC | 831 | target_machine = llvm.TargetMachine.create( |
| 479 | else | 832 | builder.llvm.target.?, |
| 480 | .Static; | 833 | builder.target_triple.toSlice(&builder).?, |
| 481 | | 834 | if (options.target.cpu.model.llvm_name) |s| s.ptr else null, |
| 482 | const code_model: llvm.CodeModel = switch (options.machine_code_model) { | 835 | options.llvm_cpu_features, |
| 483 | .default => .Default, | 836 | opt_level, |
| 484 | .tiny => .Tiny, | 837 | reloc_mode, |
| 485 | .small => .Small, | 838 | code_model, |
| 486 | .kernel => .Kernel, | 839 | options.function_sections, |
| 487 | .medium => .Medium, | 840 | float_abi, |
| 488 | .large => .Large, | 841 | if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null, |
| 489 | }; | 842 | ); |
| | 843 | errdefer target_machine.dispose(); |
| 490 | | 844 | |
| 491 | // TODO handle float ABI better- it should depend on the ABI portion of std.Target | 845 | target_data = target_machine.createTargetDataLayout(); |
| 492 | const float_abi: llvm.ABIType = .Default; | 846 | errdefer target_data.dispose(); |
| 493 | | | |
| 494 | const target_machine = llvm.TargetMachine.create( | | |
| 495 | target, | | |
| 496 | llvm_target_triple.ptr, | | |
| 497 | if (options.target.cpu.model.llvm_name) |s| s.ptr else null, | | |
| 498 | options.llvm_cpu_features, | | |
| 499 | opt_level, | | |
| 500 | reloc_mode, | | |
| 501 | code_model, | | |
| 502 | options.function_sections, | | |
| 503 | float_abi, | | |
| 504 | if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null, | | |
| 505 | ); | | |
| 506 | errdefer target_machine.dispose(); | | |
| 507 | | 847 | |
| 508 | const target_data = target_machine.createTargetDataLayout(); | 848 | builder.llvm.module.?.setModuleDataLayout(target_data); |
| 509 | errdefer target_data.dispose(); | | |
| 510 | | 849 | |
| 511 | llvm_module.setModuleDataLayout(target_data); | 850 | if (options.pic) builder.llvm.module.?.setModulePICLevel(); |
| | 851 | if (options.pie) builder.llvm.module.?.setModulePIELevel(); |
| | 852 | if (code_model != .Default) builder.llvm.module.?.setModuleCodeModel(code_model); |
| 512 | | 853 | |
| 513 | if (options.pic) llvm_module.setModulePICLevel(); | 854 | if (options.opt_bisect_limit >= 0) { |
| 514 | if (options.pie) llvm_module.setModulePIELevel(); | 855 | builder.llvm.context.setOptBisectLimit(std.math.lossyCast(c_int, options.opt_bisect_limit)); |
| 515 | if (code_model != .Default) llvm_module.setModuleCodeModel(code_model); | 856 | } |
| 516 | | 857 | |
| 517 | if (options.opt_bisect_limit >= 0) { | 858 | builder.data_layout = try builder.fmt("{}", .{DataLayoutBuilder{ .target = options.target }}); |
| 518 | context.setOptBisectLimit(std.math.lossyCast(c_int, options.opt_bisect_limit)); | 859 | if (std.debug.runtime_safety) { |
| | 860 | const rep = target_data.stringRep(); |
| | 861 | defer llvm.disposeMessage(rep); |
| | 862 | std.testing.expectEqualStrings( |
| | 863 | std.mem.span(rep), |
| | 864 | builder.data_layout.toSlice(&builder).?, |
| | 865 | ) catch unreachable; |
| | 866 | } |
| 519 | } | 867 | } |
| 520 | | 868 | |
| 521 | return Object{ | 869 | return .{ |
| 522 | .gpa = gpa, | 870 | .gpa = gpa, |
| | 871 | .builder = builder, |
| 523 | .module = options.module.?, | 872 | .module = options.module.?, |
| 524 | .llvm_module = llvm_module, | 873 | .llvm_module = builder.llvm.module.?, |
| 525 | .di_map = .{}, | 874 | .di_map = .{}, |
| 526 | .di_builder = opt_di_builder, | 875 | .di_builder = builder.llvm.di_builder, |
| 527 | .di_compile_unit = di_compile_unit, | 876 | .di_compile_unit = builder.llvm.di_compile_unit, |
| 528 | .context = context, | | |
| 529 | .target_machine = target_machine, | 877 | .target_machine = target_machine, |
| 530 | .target_data = target_data, | 878 | .target_data = target_data, |
| 531 | .target = options.target, | 879 | .target = options.target, |
| ... | @@ -533,22 +881,17 @@ pub const Object = struct { | ... | @@ -533,22 +881,17 @@ pub const Object = struct { |
| 533 | .named_enum_map = .{}, | 881 | .named_enum_map = .{}, |
| 534 | .type_map = .{}, | 882 | .type_map = .{}, |
| 535 | .di_type_map = .{}, | 883 | .di_type_map = .{}, |
| 536 | .error_name_table = null, | 884 | .error_name_table = .none, |
| 537 | .extern_collisions = .{}, | 885 | .extern_collisions = .{}, |
| 538 | .null_opt_addr = null, | 886 | .null_opt_usize = .no_init, |
| 539 | }; | 887 | }; |
| 540 | } | 888 | } |
| 541 | | 889 | |
| 542 | pub fn deinit(self: *Object, gpa: Allocator) void { | 890 | pub fn deinit(self: *Object, gpa: Allocator) void { |
| 543 | if (self.di_builder) |dib| { | 891 | self.di_map.deinit(gpa); |
| 544 | dib.dispose(); | 892 | self.di_type_map.deinit(gpa); |
| 545 | self.di_map.deinit(gpa); | | |
| 546 | self.di_type_map.deinit(gpa); | | |
| 547 | } | | |
| 548 | self.target_data.dispose(); | 893 | self.target_data.dispose(); |
| 549 | self.target_machine.dispose(); | 894 | self.target_machine.dispose(); |
| 550 | self.llvm_module.dispose(); | | |
| 551 | self.context.dispose(); | | |
| 552 | self.decl_map.deinit(gpa); | 895 | self.decl_map.deinit(gpa); |
| 553 | self.named_enum_map.deinit(gpa); | 896 | self.named_enum_map.deinit(gpa); |
| 554 | self.type_map.deinit(gpa); | 897 | self.type_map.deinit(gpa); |
| ... | @@ -572,85 +915,108 @@ pub const Object = struct { | ... | @@ -572,85 +915,108 @@ pub const Object = struct { |
| 572 | return slice.ptr; | 915 | return slice.ptr; |
| 573 | } | 916 | } |
| 574 | | 917 | |
| 575 | fn genErrorNameTable(o: *Object) !void { | 918 | fn genErrorNameTable(o: *Object) Allocator.Error!void { |
| 576 | // If o.error_name_table is null, there was no instruction that actually referenced the error table. | 919 | // If o.error_name_table is null, there was no instruction that actually referenced the error table. |
| 577 | const error_name_table_ptr_global = o.error_name_table orelse return; | 920 | const error_name_table_ptr_global = o.error_name_table; |
| | 921 | if (error_name_table_ptr_global == .none) return; |
| 578 | | 922 | |
| 579 | const mod = o.module; | 923 | const mod = o.module; |
| 580 | const target = mod.getTarget(); | | |
| 581 | | | |
| 582 | const llvm_ptr_ty = o.context.pointerType(0); // TODO: Address space | | |
| 583 | const llvm_usize_ty = o.context.intType(target.ptrBitWidth()); | | |
| 584 | const type_fields = [_]*llvm.Type{ | | |
| 585 | llvm_ptr_ty, | | |
| 586 | llvm_usize_ty, | | |
| 587 | }; | | |
| 588 | const llvm_slice_ty = o.context.structType(&type_fields, type_fields.len, .False); | | |
| 589 | const slice_ty = Type.slice_const_u8_sentinel_0; | | |
| 590 | const slice_alignment = slice_ty.abiAlignment(mod); | | |
| 591 | | 924 | |
| 592 | const error_name_list = mod.global_error_set.keys(); | 925 | const error_name_list = mod.global_error_set.keys(); |
| 593 | const llvm_errors = try mod.gpa.alloc(*llvm.Value, error_name_list.len); | 926 | const llvm_errors = try mod.gpa.alloc(Builder.Constant, error_name_list.len); |
| 594 | defer mod.gpa.free(llvm_errors); | 927 | defer mod.gpa.free(llvm_errors); |
| 595 | | 928 | |
| 596 | llvm_errors[0] = llvm_slice_ty.getUndef(); | 929 | // TODO: Address space |
| | 930 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| | 931 | const slice_alignment = slice_ty.abiAlignment(mod); |
| | 932 | const llvm_usize_ty = try o.lowerType(Type.usize); |
| | 933 | const llvm_slice_ty = try o.lowerType(slice_ty); |
| | 934 | const llvm_table_ty = try o.builder.arrayType(error_name_list.len, llvm_slice_ty); |
| | 935 | |
| | 936 | llvm_errors[0] = try o.builder.undefConst(llvm_slice_ty); |
| 597 | for (llvm_errors[1..], error_name_list[1..]) |*llvm_error, name_nts| { | 937 | for (llvm_errors[1..], error_name_list[1..]) |*llvm_error, name_nts| { |
| 598 | const name = mod.intern_pool.stringToSlice(name_nts); | 938 | const name = try o.builder.string(mod.intern_pool.stringToSlice(name_nts)); |
| 599 | const str_init = o.context.constString(name.ptr, @as(c_uint, @intCast(name.len)), .False); | 939 | const str_init = try o.builder.stringNullConst(name); |
| 600 | const str_global = o.llvm_module.addGlobal(str_init.typeOf(), ""); | 940 | const str_ty = str_init.typeOf(&o.builder); |
| 601 | str_global.setInitializer(str_init); | 941 | const str_llvm_global = o.llvm_module.addGlobal(str_ty.toLlvm(&o.builder), ""); |
| 602 | str_global.setLinkage(.Private); | 942 | str_llvm_global.setInitializer(str_init.toLlvm(&o.builder)); |
| 603 | str_global.setGlobalConstant(.True); | 943 | str_llvm_global.setLinkage(.Private); |
| 604 | str_global.setUnnamedAddr(.True); | 944 | str_llvm_global.setGlobalConstant(.True); |
| 605 | str_global.setAlignment(1); | 945 | str_llvm_global.setUnnamedAddr(.True); |
| 606 | | 946 | str_llvm_global.setAlignment(1); |
| 607 | const slice_fields = [_]*llvm.Value{ | 947 | |
| 608 | str_global, | 948 | var str_global = Builder.Global{ |
| 609 | llvm_usize_ty.constInt(name.len, .False), | 949 | .linkage = .private, |
| | 950 | .unnamed_addr = .unnamed_addr, |
| | 951 | .type = str_ty, |
| | 952 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| 610 | }; | 953 | }; |
| 611 | llvm_error.* = llvm_slice_ty.constNamedStruct(&slice_fields, slice_fields.len); | 954 | var str_variable = Builder.Variable{ |
| 612 | } | 955 | .global = @enumFromInt(o.builder.globals.count()), |
| | 956 | .mutability = .constant, |
| | 957 | .init = str_init, |
| | 958 | .alignment = comptime Builder.Alignment.fromByteUnits(1), |
| | 959 | }; |
| | 960 | try o.builder.llvm.globals.append(o.gpa, str_llvm_global); |
| | 961 | const global_index = try o.builder.addGlobal(.empty, str_global); |
| | 962 | try o.builder.variables.append(o.gpa, str_variable); |
| 613 | | 963 | |
| 614 | const error_name_table_init = llvm_slice_ty.constArray(llvm_errors.ptr, @as(c_uint, @intCast(error_name_list.len))); | 964 | llvm_error.* = try o.builder.structConst(llvm_slice_ty, &.{ |
| | 965 | global_index.toConst(), |
| | 966 | try o.builder.intConst(llvm_usize_ty, name.toSlice(&o.builder).?.len), |
| | 967 | }); |
| | 968 | } |
| 615 | | 969 | |
| 616 | const error_name_table_global = o.llvm_module.addGlobal(error_name_table_init.typeOf(), ""); | 970 | const error_name_table_init = try o.builder.arrayConst(llvm_table_ty, llvm_errors); |
| 617 | error_name_table_global.setInitializer(error_name_table_init); | 971 | const error_name_table_global = o.llvm_module.addGlobal(llvm_table_ty.toLlvm(&o.builder), ""); |
| | 972 | error_name_table_global.setInitializer(error_name_table_init.toLlvm(&o.builder)); |
| 618 | error_name_table_global.setLinkage(.Private); | 973 | error_name_table_global.setLinkage(.Private); |
| 619 | error_name_table_global.setGlobalConstant(.True); | 974 | error_name_table_global.setGlobalConstant(.True); |
| 620 | error_name_table_global.setUnnamedAddr(.True); | 975 | error_name_table_global.setUnnamedAddr(.True); |
| 621 | error_name_table_global.setAlignment(slice_alignment); // TODO: Dont hardcode | 976 | error_name_table_global.setAlignment(slice_alignment); // TODO: Dont hardcode |
| 622 | | 977 | |
| | 978 | var global = Builder.Global{ |
| | 979 | .linkage = .private, |
| | 980 | .unnamed_addr = .unnamed_addr, |
| | 981 | .type = llvm_table_ty, |
| | 982 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| | 983 | }; |
| | 984 | var variable = Builder.Variable{ |
| | 985 | .global = @enumFromInt(o.builder.globals.count()), |
| | 986 | .mutability = .constant, |
| | 987 | .init = error_name_table_init, |
| | 988 | .alignment = Builder.Alignment.fromByteUnits(slice_alignment), |
| | 989 | }; |
| | 990 | try o.builder.llvm.globals.append(o.gpa, error_name_table_global); |
| | 991 | _ = try o.builder.addGlobal(.empty, global); |
| | 992 | try o.builder.variables.append(o.gpa, variable); |
| | 993 | |
| 623 | const error_name_table_ptr = error_name_table_global; | 994 | const error_name_table_ptr = error_name_table_global; |
| 624 | error_name_table_ptr_global.setInitializer(error_name_table_ptr); | 995 | error_name_table_ptr_global.ptr(&o.builder).init = variable.global.toConst(); |
| | 996 | error_name_table_ptr_global.toLlvm(&o.builder).setInitializer(error_name_table_ptr); |
| 625 | } | 997 | } |
| 626 | | 998 | |
| 627 | fn genCmpLtErrorsLenFunction(object: *Object) !void { | 999 | fn genCmpLtErrorsLenFunction(o: *Object) !void { |
| 628 | // If there is no such function in the module, it means the source code does not need it. | 1000 | // If there is no such function in the module, it means the source code does not need it. |
| 629 | const llvm_fn = object.llvm_module.getNamedFunction(lt_errors_fn_name) orelse return; | 1001 | const name = o.builder.stringIfExists(lt_errors_fn_name) orelse return; |
| 630 | const mod = object.module; | 1002 | const llvm_fn = o.builder.getGlobal(name) orelse return; |
| | 1003 | const mod = o.module; |
| 631 | const errors_len = mod.global_error_set.count(); | 1004 | const errors_len = mod.global_error_set.count(); |
| 632 | | 1005 | |
| 633 | // Delete previous implementation. We replace it with every flush() because the | 1006 | var wip = try Builder.WipFunction.init(&o.builder, llvm_fn.ptrConst(&o.builder).kind.function); |
| 634 | // total number of errors may have changed. | 1007 | defer wip.deinit(); |
| 635 | while (llvm_fn.getFirstBasicBlock()) |bb| { | 1008 | wip.cursor = .{ .block = try wip.block(0, "Entry") }; |
| 636 | bb.deleteBasicBlock(); | | |
| 637 | } | | |
| 638 | | | |
| 639 | const builder = object.context.createBuilder(); | | |
| 640 | | | |
| 641 | const entry_block = object.context.appendBasicBlock(llvm_fn, "Entry"); | | |
| 642 | builder.positionBuilderAtEnd(entry_block); | | |
| 643 | builder.clearCurrentDebugLocation(); | | |
| 644 | | 1009 | |
| 645 | // Example source of the following LLVM IR: | 1010 | // Example source of the following LLVM IR: |
| 646 | // fn __zig_lt_errors_len(index: u16) bool { | 1011 | // fn __zig_lt_errors_len(index: u16) bool { |
| 647 | // return index < total_errors_len; | 1012 | // return index < total_errors_len; |
| 648 | // } | 1013 | // } |
| 649 | | 1014 | |
| 650 | const lhs = llvm_fn.getParam(0); | 1015 | const lhs = wip.arg(0); |
| 651 | const rhs = lhs.typeOf().constInt(errors_len, .False); | 1016 | const rhs = try o.builder.intValue(Builder.Type.err_int, errors_len); |
| 652 | const is_lt = builder.buildICmp(.ULT, lhs, rhs, ""); | 1017 | const is_lt = try wip.icmp(.ult, lhs, rhs, ""); |
| 653 | _ = builder.buildRet(is_lt); | 1018 | _ = try wip.ret(is_lt); |
| | 1019 | try wip.finish(); |
| 654 | } | 1020 | } |
| 655 | | 1021 | |
| 656 | fn genModuleLevelAssembly(object: *Object) !void { | 1022 | fn genModuleLevelAssembly(object: *Object) !void { |
| ... | @@ -671,34 +1037,28 @@ pub const Object = struct { | ... | @@ -671,34 +1037,28 @@ pub const Object = struct { |
| 671 | | 1037 | |
| 672 | // This map has externs with incorrect symbol names. | 1038 | // This map has externs with incorrect symbol names. |
| 673 | for (object.extern_collisions.keys()) |decl_index| { | 1039 | for (object.extern_collisions.keys()) |decl_index| { |
| 674 | const entry = object.decl_map.getEntry(decl_index) orelse continue; | 1040 | const global = object.decl_map.get(decl_index) orelse continue; |
| 675 | const llvm_global = entry.value_ptr.*; | | |
| 676 | // Same logic as below but for externs instead of exports. | 1041 | // Same logic as below but for externs instead of exports. |
| 677 | const decl = mod.declPtr(decl_index); | 1042 | const decl_name = object.builder.stringIfExists(mod.intern_pool.stringToSlice(mod.declPtr(decl_index).name)) orelse continue; |
| 678 | const other_global = object.getLlvmGlobal(mod.intern_pool.stringToSlice(decl.name)) orelse continue; | 1043 | const other_global = object.builder.getGlobal(decl_name) orelse continue; |
| 679 | if (other_global == llvm_global) continue; | 1044 | if (other_global.eql(global, &object.builder)) continue; |
| 680 | | 1045 | |
| 681 | llvm_global.replaceAllUsesWith(other_global); | 1046 | try global.replace(other_global, &object.builder); |
| 682 | deleteLlvmGlobal(llvm_global); | | |
| 683 | entry.value_ptr.* = other_global; | | |
| 684 | } | 1047 | } |
| 685 | object.extern_collisions.clearRetainingCapacity(); | 1048 | object.extern_collisions.clearRetainingCapacity(); |
| 686 | | 1049 | |
| 687 | const export_keys = mod.decl_exports.keys(); | 1050 | for (mod.decl_exports.keys(), mod.decl_exports.values()) |decl_index, export_list| { |
| 688 | for (mod.decl_exports.values(), 0..) |export_list, i| { | 1051 | const global = object.decl_map.get(decl_index) orelse continue; |
| 689 | const decl_index = export_keys[i]; | | |
| 690 | const llvm_global = object.decl_map.get(decl_index) orelse continue; | | |
| 691 | for (export_list.items) |exp| { | 1052 | for (export_list.items) |exp| { |
| 692 | // Detect if the LLVM global has already been created as an extern. In such | 1053 | // Detect if the LLVM global has already been created as an extern. In such |
| 693 | // case, we need to replace all uses of it with this exported global. | 1054 | // case, we need to replace all uses of it with this exported global. |
| 694 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); | 1055 | const exp_name = object.builder.stringIfExists(mod.intern_pool.stringToSlice(exp.opts.name)) orelse continue; |
| 695 | | 1056 | |
| 696 | const other_global = object.getLlvmGlobal(exp_name.ptr) orelse continue; | 1057 | const other_global = object.builder.getGlobal(exp_name) orelse continue; |
| 697 | if (other_global == llvm_global) continue; | 1058 | if (other_global.eql(global, &object.builder)) continue; |
| 698 | | 1059 | |
| 699 | other_global.replaceAllUsesWith(llvm_global); | 1060 | try global.takeName(other_global, &object.builder); |
| 700 | llvm_global.takeName(other_global); | 1061 | try other_global.replace(global, &object.builder); |
| 701 | deleteLlvmGlobal(other_global); | | |
| 702 | // Problem: now we need to replace in the decl_map that | 1062 | // Problem: now we need to replace in the decl_map that |
| 703 | // the extern decl index points to this new global. However we don't | 1063 | // the extern decl index points to this new global. However we don't |
| 704 | // know the decl index. | 1064 | // know the decl index. |
| ... | @@ -744,20 +1104,9 @@ pub const Object = struct { | ... | @@ -744,20 +1104,9 @@ pub const Object = struct { |
| 744 | | 1104 | |
| 745 | if (comp.verbose_llvm_ir) |path| { | 1105 | if (comp.verbose_llvm_ir) |path| { |
| 746 | if (std.mem.eql(u8, path, "-")) { | 1106 | if (std.mem.eql(u8, path, "-")) { |
| 747 | self.llvm_module.dump(); | 1107 | self.builder.dump(); |
| 748 | } else { | 1108 | } else { |
| 749 | const path_z = try comp.gpa.dupeZ(u8, path); | 1109 | _ = try self.builder.printToFile(path); |
| 750 | defer comp.gpa.free(path_z); | | |
| 751 | | | |
| 752 | var error_message: [*:0]const u8 = undefined; | | |
| 753 | | | |
| 754 | if (self.llvm_module.printModuleToFile(path_z, &error_message).toBool()) { | | |
| 755 | defer llvm.disposeMessage(error_message); | | |
| 756 | | | |
| 757 | log.err("dump LLVM module failed ir={s}: {s}", .{ | | |
| 758 | path, error_message, | | |
| 759 | }); | | |
| 760 | } | | |
| 761 | } | 1110 | } |
| 762 | } | 1111 | } |
| 763 | | 1112 | |
| ... | @@ -884,7 +1233,9 @@ pub const Object = struct { | ... | @@ -884,7 +1233,9 @@ pub const Object = struct { |
| 884 | .err_msg = null, | 1233 | .err_msg = null, |
| 885 | }; | 1234 | }; |
| 886 | | 1235 | |
| 887 | const llvm_func = try o.resolveLlvmFunction(decl_index); | 1236 | const function = try o.resolveLlvmFunction(decl_index); |
| | 1237 | const global = function.ptrConst(&o.builder).global; |
| | 1238 | const llvm_func = global.toLlvm(&o.builder); |
| 888 | | 1239 | |
| 889 | if (func.analysis(ip).is_noinline) { | 1240 | if (func.analysis(ip).is_noinline) { |
| 890 | o.addFnAttr(llvm_func, "noinline"); | 1241 | o.addFnAttr(llvm_func, "noinline"); |
| ... | @@ -921,24 +1272,27 @@ pub const Object = struct { | ... | @@ -921,24 +1272,27 @@ pub const Object = struct { |
| 921 | o.addFnAttrString(llvm_func, "no-stack-arg-probe", ""); | 1272 | o.addFnAttrString(llvm_func, "no-stack-arg-probe", ""); |
| 922 | } | 1273 | } |
| 923 | | 1274 | |
| 924 | if (ip.stringToSliceUnwrap(decl.@"linksection")) |section| | 1275 | if (ip.stringToSliceUnwrap(decl.@"linksection")) |section| { |
| | 1276 | function.ptr(&o.builder).section = try o.builder.string(section); |
| 925 | llvm_func.setSection(section); | 1277 | llvm_func.setSection(section); |
| 926 | | | |
| 927 | // Remove all the basic blocks of a function in order to start over, generating | | |
| 928 | // LLVM IR from an empty function body. | | |
| 929 | while (llvm_func.getFirstBasicBlock()) |bb| { | | |
| 930 | bb.deleteBasicBlock(); | | |
| 931 | } | 1278 | } |
| 932 | | 1279 | |
| 933 | const builder = o.context.createBuilder(); | 1280 | var deinit_wip = true; |
| | 1281 | var wip = try Builder.WipFunction.init(&o.builder, function); |
| | 1282 | defer if (deinit_wip) wip.deinit(); |
| | 1283 | wip.cursor = .{ .block = try wip.block(0, "Entry") }; |
| 934 | | 1284 | |
| 935 | const entry_block = o.context.appendBasicBlock(llvm_func, "Entry"); | 1285 | const builder = wip.llvm.builder; |
| 936 | builder.positionBuilderAtEnd(entry_block); | 1286 | var llvm_arg_i: u32 = 0; |
| 937 | | 1287 | |
| 938 | // This gets the LLVM values from the function and stores them in `dg.args`. | 1288 | // This gets the LLVM values from the function and stores them in `dg.args`. |
| 939 | const fn_info = mod.typeToFunc(decl.ty).?; | 1289 | const fn_info = mod.typeToFunc(decl.ty).?; |
| 940 | const sret = firstParamSRet(fn_info, mod); | 1290 | const sret = firstParamSRet(fn_info, mod); |
| 941 | const ret_ptr = if (sret) llvm_func.getParam(0) else null; | 1291 | const ret_ptr: Builder.Value = if (sret) param: { |
| | 1292 | const param = wip.arg(llvm_arg_i); |
| | 1293 | llvm_arg_i += 1; |
| | 1294 | break :param param; |
| | 1295 | } else .none; |
| 942 | const gpa = o.gpa; | 1296 | const gpa = o.gpa; |
| 943 | | 1297 | |
| 944 | if (ccAbiPromoteInt(fn_info.cc, mod, fn_info.return_type.toType())) |s| switch (s) { | 1298 | if (ccAbiPromoteInt(fn_info.cc, mod, fn_info.return_type.toType())) |s| switch (s) { |
| ... | @@ -949,207 +1303,183 @@ pub const Object = struct { | ... | @@ -949,207 +1303,183 @@ pub const Object = struct { |
| 949 | const err_return_tracing = fn_info.return_type.toType().isError(mod) and | 1303 | const err_return_tracing = fn_info.return_type.toType().isError(mod) and |
| 950 | mod.comp.bin_file.options.error_return_tracing; | 1304 | mod.comp.bin_file.options.error_return_tracing; |
| 951 | | 1305 | |
| 952 | const err_ret_trace = if (err_return_tracing) | 1306 | const err_ret_trace: Builder.Value = if (err_return_tracing) param: { |
| 953 | llvm_func.getParam(@intFromBool(ret_ptr != null)) | 1307 | const param = wip.arg(llvm_arg_i); |
| 954 | else | 1308 | llvm_arg_i += 1; |
| 955 | null; | 1309 | break :param param; |
| | 1310 | } else .none; |
| 956 | | 1311 | |
| 957 | // This is the list of args we will use that correspond directly to the AIR arg | 1312 | // This is the list of args we will use that correspond directly to the AIR arg |
| 958 | // instructions. Depending on the calling convention, this list is not necessarily | 1313 | // instructions. Depending on the calling convention, this list is not necessarily |
| 959 | // a bijection with the actual LLVM parameters of the function. | 1314 | // a bijection with the actual LLVM parameters of the function. |
| 960 | var args = std.ArrayList(*llvm.Value).init(gpa); | 1315 | var args: std.ArrayListUnmanaged(Builder.Value) = .{}; |
| 961 | defer args.deinit(); | 1316 | defer args.deinit(gpa); |
| 962 | | 1317 | |
| 963 | { | 1318 | { |
| 964 | var llvm_arg_i = @as(c_uint, @intFromBool(ret_ptr != null)) + @intFromBool(err_return_tracing); | | |
| 965 | var it = iterateParamTypes(o, fn_info); | 1319 | var it = iterateParamTypes(o, fn_info); |
| 966 | while (it.next()) |lowering| switch (lowering) { | 1320 | while (try it.next()) |lowering| { |
| 967 | .no_bits => continue, | 1321 | try args.ensureUnusedCapacity(gpa, 1); |
| 968 | .byval => { | 1322 | |
| 969 | assert(!it.byval_attr); | 1323 | switch (lowering) { |
| 970 | const param_index = it.zig_index - 1; | 1324 | .no_bits => continue, |
| 971 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); | 1325 | .byval => { |
| 972 | const param = llvm_func.getParam(llvm_arg_i); | 1326 | assert(!it.byval_attr); |
| 973 | try args.ensureUnusedCapacity(1); | 1327 | const param_index = it.zig_index - 1; |
| 974 | | 1328 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); |
| 975 | if (isByRef(param_ty, mod)) { | 1329 | const param = wip.arg(llvm_arg_i); |
| 976 | const alignment = param_ty.abiAlignment(mod); | 1330 | |
| 977 | const param_llvm_ty = param.typeOf(); | 1331 | if (isByRef(param_ty, mod)) { |
| 978 | const arg_ptr = buildAllocaInner(o.context, builder, llvm_func, false, param_llvm_ty, alignment, target); | 1332 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 979 | const store_inst = builder.buildStore(param, arg_ptr); | 1333 | const param_llvm_ty = param.typeOfWip(&wip); |
| 980 | store_inst.setAlignment(alignment); | 1334 | const arg_ptr = try buildAllocaInner(&wip, false, param_llvm_ty, alignment, target); |
| 981 | args.appendAssumeCapacity(arg_ptr); | 1335 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 982 | } else { | 1336 | args.appendAssumeCapacity(arg_ptr); |
| 983 | args.appendAssumeCapacity(param); | 1337 | } else { |
| 984 | | 1338 | args.appendAssumeCapacity(param); |
| 985 | o.addByValParamAttrs(llvm_func, param_ty, param_index, fn_info, llvm_arg_i); | | |
| 986 | } | | |
| 987 | llvm_arg_i += 1; | | |
| 988 | }, | | |
| 989 | .byref => { | | |
| 990 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 991 | const param_llvm_ty = try o.lowerType(param_ty); | | |
| 992 | const param = llvm_func.getParam(llvm_arg_i); | | |
| 993 | const alignment = param_ty.abiAlignment(mod); | | |
| 994 | | | |
| 995 | o.addByRefParamAttrs(llvm_func, llvm_arg_i, alignment, it.byval_attr, param_llvm_ty); | | |
| 996 | llvm_arg_i += 1; | | |
| 997 | | | |
| 998 | try args.ensureUnusedCapacity(1); | | |
| 999 | | | |
| 1000 | if (isByRef(param_ty, mod)) { | | |
| 1001 | args.appendAssumeCapacity(param); | | |
| 1002 | } else { | | |
| 1003 | const load_inst = builder.buildLoad(param_llvm_ty, param, ""); | | |
| 1004 | load_inst.setAlignment(alignment); | | |
| 1005 | args.appendAssumeCapacity(load_inst); | | |
| 1006 | } | | |
| 1007 | }, | | |
| 1008 | .byref_mut => { | | |
| 1009 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 1010 | const param_llvm_ty = try o.lowerType(param_ty); | | |
| 1011 | const param = llvm_func.getParam(llvm_arg_i); | | |
| 1012 | const alignment = param_ty.abiAlignment(mod); | | |
| 1013 | | | |
| 1014 | o.addArgAttr(llvm_func, llvm_arg_i, "noundef"); | | |
| 1015 | llvm_arg_i += 1; | | |
| 1016 | | | |
| 1017 | try args.ensureUnusedCapacity(1); | | |
| 1018 | | 1339 | |
| 1019 | if (isByRef(param_ty, mod)) { | 1340 | o.addByValParamAttrs(llvm_func, param_ty, param_index, fn_info, @intCast(llvm_arg_i)); |
| 1020 | args.appendAssumeCapacity(param); | 1341 | } |
| 1021 | } else { | 1342 | llvm_arg_i += 1; |
| 1022 | const load_inst = builder.buildLoad(param_llvm_ty, param, ""); | 1343 | }, |
| 1023 | load_inst.setAlignment(alignment); | 1344 | .byref => { |
| 1024 | args.appendAssumeCapacity(load_inst); | 1345 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 1025 | } | 1346 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1026 | }, | 1347 | const param = wip.arg(llvm_arg_i); |
| 1027 | .abi_sized_int => { | 1348 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 1028 | assert(!it.byval_attr); | | |
| 1029 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 1030 | const param = llvm_func.getParam(llvm_arg_i); | | |
| 1031 | llvm_arg_i += 1; | | |
| 1032 | | 1349 | |
| 1033 | const param_llvm_ty = try o.lowerType(param_ty); | 1350 | o.addByRefParamAttrs(llvm_func, @intCast(llvm_arg_i), @intCast(alignment.toByteUnits() orelse 0), it.byval_attr, param_llvm_ty); |
| 1034 | const abi_size = @as(c_uint, @intCast(param_ty.abiSize(mod))); | 1351 | llvm_arg_i += 1; |
| 1035 | const int_llvm_ty = o.context.intType(abi_size * 8); | | |
| 1036 | const alignment = @max( | | |
| 1037 | param_ty.abiAlignment(mod), | | |
| 1038 | o.target_data.abiAlignmentOfType(int_llvm_ty), | | |
| 1039 | ); | | |
| 1040 | const arg_ptr = buildAllocaInner(o.context, builder, llvm_func, false, param_llvm_ty, alignment, target); | | |
| 1041 | const store_inst = builder.buildStore(param, arg_ptr); | | |
| 1042 | store_inst.setAlignment(alignment); | | |
| 1043 | | 1352 | |
| 1044 | try args.ensureUnusedCapacity(1); | 1353 | if (isByRef(param_ty, mod)) { |
| | 1354 | args.appendAssumeCapacity(param); |
| | 1355 | } else { |
| | 1356 | args.appendAssumeCapacity(try wip.load(.normal, param_llvm_ty, param, alignment, "")); |
| | 1357 | } |
| | 1358 | }, |
| | 1359 | .byref_mut => { |
| | 1360 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| | 1361 | const param_llvm_ty = try o.lowerType(param_ty); |
| | 1362 | const param = wip.arg(llvm_arg_i); |
| | 1363 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 1045 | | 1364 | |
| 1046 | if (isByRef(param_ty, mod)) { | 1365 | o.addArgAttr(llvm_func, @intCast(llvm_arg_i), "noundef"); |
| 1047 | args.appendAssumeCapacity(arg_ptr); | 1366 | llvm_arg_i += 1; |
| 1048 | } else { | | |
| 1049 | const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, ""); | | |
| 1050 | load_inst.setAlignment(alignment); | | |
| 1051 | args.appendAssumeCapacity(load_inst); | | |
| 1052 | } | | |
| 1053 | }, | | |
| 1054 | .slice => { | | |
| 1055 | assert(!it.byval_attr); | | |
| 1056 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 1057 | const ptr_info = param_ty.ptrInfo(mod); | | |
| 1058 | | 1367 | |
| 1059 | if (math.cast(u5, it.zig_index - 1)) |i| { | 1368 | if (isByRef(param_ty, mod)) { |
| 1060 | if (@as(u1, @truncate(fn_info.noalias_bits >> i)) != 0) { | 1369 | args.appendAssumeCapacity(param); |
| 1061 | o.addArgAttr(llvm_func, llvm_arg_i, "noalias"); | 1370 | } else { |
| | 1371 | args.appendAssumeCapacity(try wip.load(.normal, param_llvm_ty, param, alignment, "")); |
| 1062 | } | 1372 | } |
| 1063 | } | 1373 | }, |
| 1064 | if (param_ty.zigTypeTag(mod) != .Optional) { | 1374 | .abi_sized_int => { |
| 1065 | o.addArgAttr(llvm_func, llvm_arg_i, "nonnull"); | 1375 | assert(!it.byval_attr); |
| 1066 | } | 1376 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 1067 | if (ptr_info.flags.is_const) { | 1377 | const param = wip.arg(llvm_arg_i); |
| 1068 | o.addArgAttr(llvm_func, llvm_arg_i, "readonly"); | | |
| 1069 | } | | |
| 1070 | const elem_align = ptr_info.flags.alignment.toByteUnitsOptional() orelse | | |
| 1071 | @max(ptr_info.child.toType().abiAlignment(mod), 1); | | |
| 1072 | o.addArgAttrInt(llvm_func, llvm_arg_i, "align", elem_align); | | |
| 1073 | const ptr_param = llvm_func.getParam(llvm_arg_i); | | |
| 1074 | llvm_arg_i += 1; | | |
| 1075 | const len_param = llvm_func.getParam(llvm_arg_i); | | |
| 1076 | llvm_arg_i += 1; | | |
| 1077 | | | |
| 1078 | const slice_llvm_ty = try o.lowerType(param_ty); | | |
| 1079 | const partial = builder.buildInsertValue(slice_llvm_ty.getUndef(), ptr_param, 0, ""); | | |
| 1080 | const aggregate = builder.buildInsertValue(partial, len_param, 1, ""); | | |
| 1081 | try args.append(aggregate); | | |
| 1082 | }, | | |
| 1083 | .multiple_llvm_types => { | | |
| 1084 | assert(!it.byval_attr); | | |
| 1085 | const field_types = it.llvm_types_buffer[0..it.llvm_types_len]; | | |
| 1086 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 1087 | const param_llvm_ty = try o.lowerType(param_ty); | | |
| 1088 | const param_alignment = param_ty.abiAlignment(mod); | | |
| 1089 | const arg_ptr = buildAllocaInner(o.context, builder, llvm_func, false, param_llvm_ty, param_alignment, target); | | |
| 1090 | const llvm_ty = o.context.structType(field_types.ptr, @as(c_uint, @intCast(field_types.len)), .False); | | |
| 1091 | for (field_types, 0..) |_, field_i_usize| { | | |
| 1092 | const field_i = @as(c_uint, @intCast(field_i_usize)); | | |
| 1093 | const param = llvm_func.getParam(llvm_arg_i); | | |
| 1094 | llvm_arg_i += 1; | 1378 | llvm_arg_i += 1; |
| 1095 | const field_ptr = builder.buildStructGEP(llvm_ty, arg_ptr, field_i, ""); | | |
| 1096 | const store_inst = builder.buildStore(param, field_ptr); | | |
| 1097 | store_inst.setAlignment(target.ptrBitWidth() / 8); | | |
| 1098 | } | | |
| 1099 | | 1379 | |
| 1100 | const is_by_ref = isByRef(param_ty, mod); | 1380 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1101 | const loaded = if (is_by_ref) arg_ptr else l: { | 1381 | const int_llvm_ty = try o.builder.intType(@intCast(param_ty.abiSize(mod) * 8)); |
| 1102 | const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, ""); | 1382 | const alignment = Builder.Alignment.fromByteUnits(@max( |
| 1103 | load_inst.setAlignment(param_alignment); | 1383 | param_ty.abiAlignment(mod), |
| 1104 | break :l load_inst; | 1384 | o.target_data.abiAlignmentOfType(int_llvm_ty.toLlvm(&o.builder)), |
| 1105 | }; | 1385 | )); |
| 1106 | try args.append(loaded); | 1386 | const arg_ptr = try buildAllocaInner(&wip, false, param_llvm_ty, alignment, target); |
| 1107 | }, | 1387 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1108 | .as_u16 => { | 1388 | |
| 1109 | assert(!it.byval_attr); | 1389 | args.appendAssumeCapacity(if (isByRef(param_ty, mod)) |
| 1110 | const param = llvm_func.getParam(llvm_arg_i); | 1390 | arg_ptr |
| 1111 | llvm_arg_i += 1; | 1391 | else |
| 1112 | const casted = builder.buildBitCast(param, o.context.halfType(), ""); | 1392 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| 1113 | try args.ensureUnusedCapacity(1); | 1393 | }, |
| 1114 | args.appendAssumeCapacity(casted); | 1394 | .slice => { |
| 1115 | }, | 1395 | assert(!it.byval_attr); |
| 1116 | .float_array => { | 1396 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 1117 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | 1397 | const ptr_info = param_ty.ptrInfo(mod); |
| 1118 | const param_llvm_ty = try o.lowerType(param_ty); | 1398 | |
| 1119 | const param = llvm_func.getParam(llvm_arg_i); | 1399 | if (math.cast(u5, it.zig_index - 1)) |i| { |
| 1120 | llvm_arg_i += 1; | 1400 | if (@as(u1, @truncate(fn_info.noalias_bits >> i)) != 0) { |
| | 1401 | o.addArgAttr(llvm_func, @intCast(llvm_arg_i), "noalias"); |
| | 1402 | } |
| | 1403 | } |
| | 1404 | if (param_ty.zigTypeTag(mod) != .Optional) { |
| | 1405 | o.addArgAttr(llvm_func, @intCast(llvm_arg_i), "nonnull"); |
| | 1406 | } |
| | 1407 | if (ptr_info.flags.is_const) { |
| | 1408 | o.addArgAttr(llvm_func, @intCast(llvm_arg_i), "readonly"); |
| | 1409 | } |
| | 1410 | const elem_align = ptr_info.flags.alignment.toByteUnitsOptional() orelse |
| | 1411 | @max(ptr_info.child.toType().abiAlignment(mod), 1); |
| | 1412 | o.addArgAttrInt(llvm_func, @intCast(llvm_arg_i), "align", elem_align); |
| | 1413 | const ptr_param = wip.arg(llvm_arg_i + 0); |
| | 1414 | const len_param = wip.arg(llvm_arg_i + 1); |
| | 1415 | llvm_arg_i += 2; |
| | 1416 | |
| | 1417 | const slice_llvm_ty = try o.lowerType(param_ty); |
| | 1418 | args.appendAssumeCapacity( |
| | 1419 | try wip.buildAggregate(slice_llvm_ty, &.{ ptr_param, len_param }, ""), |
| | 1420 | ); |
| | 1421 | }, |
| | 1422 | .multiple_llvm_types => { |
| | 1423 | assert(!it.byval_attr); |
| | 1424 | const field_types = it.types_buffer[0..it.types_len]; |
| | 1425 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| | 1426 | const param_llvm_ty = try o.lowerType(param_ty); |
| | 1427 | const param_alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| | 1428 | const arg_ptr = try buildAllocaInner(&wip, false, param_llvm_ty, param_alignment, target); |
| | 1429 | const llvm_ty = try o.builder.structType(.normal, field_types); |
| | 1430 | for (0..field_types.len) |field_i| { |
| | 1431 | const param = wip.arg(llvm_arg_i); |
| | 1432 | llvm_arg_i += 1; |
| | 1433 | const field_ptr = try wip.gepStruct(llvm_ty, arg_ptr, field_i, ""); |
| | 1434 | const alignment = |
| | 1435 | Builder.Alignment.fromByteUnits(@divExact(target.ptrBitWidth(), 8)); |
| | 1436 | _ = try wip.store(.normal, param, field_ptr, alignment); |
| | 1437 | } |
| 1121 | | 1438 | |
| 1122 | const alignment = param_ty.abiAlignment(mod); | 1439 | const is_by_ref = isByRef(param_ty, mod); |
| 1123 | const arg_ptr = buildAllocaInner(o.context, builder, llvm_func, false, param_llvm_ty, alignment, target); | 1440 | args.appendAssumeCapacity(if (is_by_ref) |
| 1124 | _ = builder.buildStore(param, arg_ptr); | 1441 | arg_ptr |
| | 1442 | else |
| | 1443 | try wip.load(.normal, param_llvm_ty, arg_ptr, param_alignment, "")); |
| | 1444 | }, |
| | 1445 | .as_u16 => { |
| | 1446 | assert(!it.byval_attr); |
| | 1447 | const param = wip.arg(llvm_arg_i); |
| | 1448 | llvm_arg_i += 1; |
| | 1449 | args.appendAssumeCapacity(try wip.cast(.bitcast, param, .half, "")); |
| | 1450 | }, |
| | 1451 | .float_array => { |
| | 1452 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| | 1453 | const param_llvm_ty = try o.lowerType(param_ty); |
| | 1454 | const param = wip.arg(llvm_arg_i); |
| | 1455 | llvm_arg_i += 1; |
| 1125 | | 1456 | |
| 1126 | if (isByRef(param_ty, mod)) { | 1457 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 1127 | try args.append(arg_ptr); | 1458 | const arg_ptr = try buildAllocaInner(&wip, false, param_llvm_ty, alignment, target); |
| 1128 | } else { | 1459 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1129 | const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, ""); | | |
| 1130 | load_inst.setAlignment(alignment); | | |
| 1131 | try args.append(load_inst); | | |
| 1132 | } | | |
| 1133 | }, | | |
| 1134 | .i32_array, .i64_array => { | | |
| 1135 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | | |
| 1136 | const param_llvm_ty = try o.lowerType(param_ty); | | |
| 1137 | const param = llvm_func.getParam(llvm_arg_i); | | |
| 1138 | llvm_arg_i += 1; | | |
| 1139 | | 1460 | |
| 1140 | const alignment = param_ty.abiAlignment(mod); | 1461 | args.appendAssumeCapacity(if (isByRef(param_ty, mod)) |
| 1141 | const arg_ptr = buildAllocaInner(o.context, builder, llvm_func, false, param_llvm_ty, alignment, target); | 1462 | arg_ptr |
| 1142 | _ = builder.buildStore(param, arg_ptr); | 1463 | else |
| | 1464 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| | 1465 | }, |
| | 1466 | .i32_array, .i64_array => { |
| | 1467 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| | 1468 | const param_llvm_ty = try o.lowerType(param_ty); |
| | 1469 | const param = wip.arg(llvm_arg_i); |
| | 1470 | llvm_arg_i += 1; |
| 1143 | | 1471 | |
| 1144 | if (isByRef(param_ty, mod)) { | 1472 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 1145 | try args.append(arg_ptr); | 1473 | const arg_ptr = try buildAllocaInner(&wip, false, param_llvm_ty, alignment, target); |
| 1146 | } else { | 1474 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1147 | const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, ""); | 1475 | |
| 1148 | load_inst.setAlignment(alignment); | 1476 | args.appendAssumeCapacity(if (isByRef(param_ty, mod)) |
| 1149 | try args.append(load_inst); | 1477 | arg_ptr |
| 1150 | } | 1478 | else |
| 1151 | }, | 1479 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| 1152 | }; | 1480 | }, |
| | 1481 | } |
| | 1482 | } |
| 1153 | } | 1483 | } |
| 1154 | | 1484 | |
| 1155 | var di_file: ?*llvm.DIFile = null; | 1485 | var di_file: ?*llvm.DIFile = null; |
| ... | @@ -1191,16 +1521,15 @@ pub const Object = struct { | ... | @@ -1191,16 +1521,15 @@ pub const Object = struct { |
| 1191 | .gpa = gpa, | 1521 | .gpa = gpa, |
| 1192 | .air = air, | 1522 | .air = air, |
| 1193 | .liveness = liveness, | 1523 | .liveness = liveness, |
| 1194 | .context = o.context, | | |
| 1195 | .dg = &dg, | 1524 | .dg = &dg, |
| | 1525 | .wip = wip, |
| 1196 | .builder = builder, | 1526 | .builder = builder, |
| 1197 | .ret_ptr = ret_ptr, | 1527 | .ret_ptr = ret_ptr, |
| 1198 | .args = args.items, | 1528 | .args = args.items, |
| 1199 | .arg_index = 0, | 1529 | .arg_index = 0, |
| 1200 | .func_inst_table = .{}, | 1530 | .func_inst_table = .{}, |
| 1201 | .llvm_func = llvm_func, | | |
| 1202 | .blocks = .{}, | 1531 | .blocks = .{}, |
| 1203 | .single_threaded = mod.comp.bin_file.options.single_threaded, | 1532 | .sync_scope = if (mod.comp.bin_file.options.single_threaded) .singlethread else .system, |
| 1204 | .di_scope = di_scope, | 1533 | .di_scope = di_scope, |
| 1205 | .di_file = di_file, | 1534 | .di_file = di_file, |
| 1206 | .base_line = dg.decl.src_line, | 1535 | .base_line = dg.decl.src_line, |
| ... | @@ -1209,6 +1538,7 @@ pub const Object = struct { | ... | @@ -1209,6 +1538,7 @@ pub const Object = struct { |
| 1209 | .err_ret_trace = err_ret_trace, | 1538 | .err_ret_trace = err_ret_trace, |
| 1210 | }; | 1539 | }; |
| 1211 | defer fg.deinit(); | 1540 | defer fg.deinit(); |
| | 1541 | deinit_wip = false; |
| 1212 | | 1542 | |
| 1213 | fg.genBody(air.getMainBody()) catch |err| switch (err) { | 1543 | fg.genBody(air.getMainBody()) catch |err| switch (err) { |
| 1214 | error.CodegenFail => { | 1544 | error.CodegenFail => { |
| ... | @@ -1220,6 +1550,8 @@ pub const Object = struct { | ... | @@ -1220,6 +1550,8 @@ pub const Object = struct { |
| 1220 | else => |e| return e, | 1550 | else => |e| return e, |
| 1221 | }; | 1551 | }; |
| 1222 | | 1552 | |
| | 1553 | try fg.wip.finish(); |
| | 1554 | |
| 1223 | try o.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index)); | 1555 | try o.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index)); |
| 1224 | } | 1556 | } |
| 1225 | | 1557 | |
| ... | @@ -1243,14 +1575,6 @@ pub const Object = struct { | ... | @@ -1243,14 +1575,6 @@ pub const Object = struct { |
| 1243 | try self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index)); | 1575 | try self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index)); |
| 1244 | } | 1576 | } |
| 1245 | | 1577 | |
| 1246 | /// TODO replace this with a call to `Module::getNamedValue`. This will require adding | | |
| 1247 | /// a new wrapper in zig_llvm.h/zig_llvm.cpp. | | |
| 1248 | fn getLlvmGlobal(o: Object, name: [*:0]const u8) ?*llvm.Value { | | |
| 1249 | if (o.llvm_module.getNamedFunction(name)) |x| return x; | | |
| 1250 | if (o.llvm_module.getNamedGlobal(name)) |x| return x; | | |
| 1251 | return null; | | |
| 1252 | } | | |
| 1253 | | | |
| 1254 | pub fn updateDeclExports( | 1578 | pub fn updateDeclExports( |
| 1255 | self: *Object, | 1579 | self: *Object, |
| 1256 | mod: *Module, | 1580 | mod: *Module, |
| ... | @@ -1260,93 +1584,133 @@ pub const Object = struct { | ... | @@ -1260,93 +1584,133 @@ pub const Object = struct { |
| 1260 | const gpa = mod.gpa; | 1584 | const gpa = mod.gpa; |
| 1261 | // If the module does not already have the function, we ignore this function call | 1585 | // If the module does not already have the function, we ignore this function call |
| 1262 | // because we call `updateDeclExports` at the end of `updateFunc` and `updateDecl`. | 1586 | // because we call `updateDeclExports` at the end of `updateFunc` and `updateDecl`. |
| 1263 | const llvm_global = self.decl_map.get(decl_index) orelse return; | 1587 | const global = self.decl_map.get(decl_index) orelse return; |
| | 1588 | const llvm_global = global.toLlvm(&self.builder); |
| 1264 | const decl = mod.declPtr(decl_index); | 1589 | const decl = mod.declPtr(decl_index); |
| 1265 | if (decl.isExtern(mod)) { | 1590 | if (decl.isExtern(mod)) { |
| 1266 | var free_decl_name = false; | | |
| 1267 | const decl_name = decl_name: { | 1591 | const decl_name = decl_name: { |
| 1268 | const decl_name = mod.intern_pool.stringToSlice(decl.name); | 1592 | const decl_name = mod.intern_pool.stringToSlice(decl.name); |
| 1269 | | 1593 | |
| 1270 | if (mod.getTarget().isWasm() and try decl.isFunction(mod)) { | 1594 | if (mod.getTarget().isWasm() and try decl.isFunction(mod)) { |
| 1271 | if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| { | 1595 | if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| { |
| 1272 | if (!std.mem.eql(u8, lib_name, "c")) { | 1596 | if (!std.mem.eql(u8, lib_name, "c")) { |
| 1273 | free_decl_name = true; | 1597 | break :decl_name try self.builder.fmt("{s}|{s}", .{ decl_name, lib_name }); |
| 1274 | break :decl_name try std.fmt.allocPrintZ(gpa, "{s}|{s}", .{ | | |
| 1275 | decl_name, lib_name, | | |
| 1276 | }); | | |
| 1277 | } | 1598 | } |
| 1278 | } | 1599 | } |
| 1279 | } | 1600 | } |
| 1280 | | 1601 | |
| 1281 | break :decl_name decl_name; | 1602 | break :decl_name try self.builder.string(decl_name); |
| 1282 | }; | 1603 | }; |
| 1283 | defer if (free_decl_name) gpa.free(decl_name); | | |
| 1284 | | 1604 | |
| 1285 | llvm_global.setValueName(decl_name); | 1605 | if (self.builder.getGlobal(decl_name)) |other_global| { |
| 1286 | if (self.getLlvmGlobal(decl_name)) |other_global| { | 1606 | if (other_global.toLlvm(&self.builder) != llvm_global) { |
| 1287 | if (other_global != llvm_global) { | | |
| 1288 | try self.extern_collisions.put(gpa, decl_index, {}); | 1607 | try self.extern_collisions.put(gpa, decl_index, {}); |
| 1289 | } | 1608 | } |
| 1290 | } | 1609 | } |
| | 1610 | |
| | 1611 | try global.rename(decl_name, &self.builder); |
| | 1612 | global.ptr(&self.builder).unnamed_addr = .default; |
| 1291 | llvm_global.setUnnamedAddr(.False); | 1613 | llvm_global.setUnnamedAddr(.False); |
| | 1614 | global.ptr(&self.builder).linkage = .external; |
| 1292 | llvm_global.setLinkage(.External); | 1615 | llvm_global.setLinkage(.External); |
| 1293 | if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default); | 1616 | if (mod.wantDllExports()) { |
| | 1617 | global.ptr(&self.builder).dll_storage_class = .default; |
| | 1618 | llvm_global.setDLLStorageClass(.Default); |
| | 1619 | } |
| 1294 | if (self.di_map.get(decl)) |di_node| { | 1620 | if (self.di_map.get(decl)) |di_node| { |
| | 1621 | const decl_name_slice = decl_name.toSlice(&self.builder).?; |
| 1295 | if (try decl.isFunction(mod)) { | 1622 | if (try decl.isFunction(mod)) { |
| 1296 | const di_func = @as(*llvm.DISubprogram, @ptrCast(di_node)); | 1623 | const di_func: *llvm.DISubprogram = @ptrCast(di_node); |
| 1297 | const linkage_name = llvm.MDString.get(self.context, decl_name.ptr, decl_name.len); | 1624 | const linkage_name = llvm.MDString.get(self.builder.llvm.context, decl_name_slice.ptr, decl_name_slice.len); |
| 1298 | di_func.replaceLinkageName(linkage_name); | 1625 | di_func.replaceLinkageName(linkage_name); |
| 1299 | } else { | 1626 | } else { |
| 1300 | const di_global = @as(*llvm.DIGlobalVariable, @ptrCast(di_node)); | 1627 | const di_global: *llvm.DIGlobalVariable = @ptrCast(di_node); |
| 1301 | const linkage_name = llvm.MDString.get(self.context, decl_name.ptr, decl_name.len); | 1628 | const linkage_name = llvm.MDString.get(self.builder.llvm.context, decl_name_slice.ptr, decl_name_slice.len); |
| 1302 | di_global.replaceLinkageName(linkage_name); | 1629 | di_global.replaceLinkageName(linkage_name); |
| 1303 | } | 1630 | } |
| 1304 | } | 1631 | } |
| 1305 | if (decl.val.getVariable(mod)) |variable| { | 1632 | if (decl.val.getVariable(mod)) |decl_var| { |
| 1306 | if (variable.is_threadlocal) { | 1633 | if (decl_var.is_threadlocal) { |
| | 1634 | global.ptrConst(&self.builder).kind.variable.ptr(&self.builder).thread_local = |
| | 1635 | .generaldynamic; |
| 1307 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); | 1636 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); |
| 1308 | } else { | 1637 | } else { |
| | 1638 | global.ptrConst(&self.builder).kind.variable.ptr(&self.builder).thread_local = |
| | 1639 | .default; |
| 1309 | llvm_global.setThreadLocalMode(.NotThreadLocal); | 1640 | llvm_global.setThreadLocalMode(.NotThreadLocal); |
| 1310 | } | 1641 | } |
| 1311 | if (variable.is_weak_linkage) { | 1642 | if (decl_var.is_weak_linkage) { |
| | 1643 | global.ptr(&self.builder).linkage = .extern_weak; |
| 1312 | llvm_global.setLinkage(.ExternalWeak); | 1644 | llvm_global.setLinkage(.ExternalWeak); |
| 1313 | } | 1645 | } |
| 1314 | } | 1646 | } |
| | 1647 | global.ptr(&self.builder).updateAttributes(); |
| 1315 | } else if (exports.len != 0) { | 1648 | } else if (exports.len != 0) { |
| 1316 | const exp_name = mod.intern_pool.stringToSlice(exports[0].opts.name); | 1649 | const exp_name = try self.builder.string(mod.intern_pool.stringToSlice(exports[0].opts.name)); |
| 1317 | llvm_global.setValueName2(exp_name.ptr, exp_name.len); | 1650 | try global.rename(exp_name, &self.builder); |
| | 1651 | global.ptr(&self.builder).unnamed_addr = .default; |
| 1318 | llvm_global.setUnnamedAddr(.False); | 1652 | llvm_global.setUnnamedAddr(.False); |
| 1319 | if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.DLLExport); | 1653 | if (mod.wantDllExports()) { |
| | 1654 | global.ptr(&self.builder).dll_storage_class = .dllexport; |
| | 1655 | llvm_global.setDLLStorageClass(.DLLExport); |
| | 1656 | } |
| 1320 | if (self.di_map.get(decl)) |di_node| { | 1657 | if (self.di_map.get(decl)) |di_node| { |
| | 1658 | const exp_name_slice = exp_name.toSlice(&self.builder).?; |
| 1321 | if (try decl.isFunction(mod)) { | 1659 | if (try decl.isFunction(mod)) { |
| 1322 | const di_func = @as(*llvm.DISubprogram, @ptrCast(di_node)); | 1660 | const di_func: *llvm.DISubprogram = @ptrCast(di_node); |
| 1323 | const linkage_name = llvm.MDString.get(self.context, exp_name.ptr, exp_name.len); | 1661 | const linkage_name = llvm.MDString.get(self.builder.llvm.context, exp_name_slice.ptr, exp_name_slice.len); |
| 1324 | di_func.replaceLinkageName(linkage_name); | 1662 | di_func.replaceLinkageName(linkage_name); |
| 1325 | } else { | 1663 | } else { |
| 1326 | const di_global = @as(*llvm.DIGlobalVariable, @ptrCast(di_node)); | 1664 | const di_global: *llvm.DIGlobalVariable = @ptrCast(di_node); |
| 1327 | const linkage_name = llvm.MDString.get(self.context, exp_name.ptr, exp_name.len); | 1665 | const linkage_name = llvm.MDString.get(self.builder.llvm.context, exp_name_slice.ptr, exp_name_slice.len); |
| 1328 | di_global.replaceLinkageName(linkage_name); | 1666 | di_global.replaceLinkageName(linkage_name); |
| 1329 | } | 1667 | } |
| 1330 | } | 1668 | } |
| 1331 | switch (exports[0].opts.linkage) { | 1669 | switch (exports[0].opts.linkage) { |
| 1332 | .Internal => unreachable, | 1670 | .Internal => unreachable, |
| 1333 | .Strong => llvm_global.setLinkage(.External), | 1671 | .Strong => { |
| 1334 | .Weak => llvm_global.setLinkage(.WeakODR), | 1672 | global.ptr(&self.builder).linkage = .external; |
| 1335 | .LinkOnce => llvm_global.setLinkage(.LinkOnceODR), | 1673 | llvm_global.setLinkage(.External); |
| | 1674 | }, |
| | 1675 | .Weak => { |
| | 1676 | global.ptr(&self.builder).linkage = .weak_odr; |
| | 1677 | llvm_global.setLinkage(.WeakODR); |
| | 1678 | }, |
| | 1679 | .LinkOnce => { |
| | 1680 | global.ptr(&self.builder).linkage = .linkonce_odr; |
| | 1681 | llvm_global.setLinkage(.LinkOnceODR); |
| | 1682 | }, |
| 1336 | } | 1683 | } |
| 1337 | switch (exports[0].opts.visibility) { | 1684 | switch (exports[0].opts.visibility) { |
| 1338 | .default => llvm_global.setVisibility(.Default), | 1685 | .default => { |
| 1339 | .hidden => llvm_global.setVisibility(.Hidden), | 1686 | global.ptr(&self.builder).visibility = .default; |
| 1340 | .protected => llvm_global.setVisibility(.Protected), | 1687 | llvm_global.setVisibility(.Default); |
| | 1688 | }, |
| | 1689 | .hidden => { |
| | 1690 | global.ptr(&self.builder).visibility = .hidden; |
| | 1691 | llvm_global.setVisibility(.Hidden); |
| | 1692 | }, |
| | 1693 | .protected => { |
| | 1694 | global.ptr(&self.builder).visibility = .protected; |
| | 1695 | llvm_global.setVisibility(.Protected); |
| | 1696 | }, |
| 1341 | } | 1697 | } |
| 1342 | if (mod.intern_pool.stringToSliceUnwrap(exports[0].opts.section)) |section| { | 1698 | if (mod.intern_pool.stringToSliceUnwrap(exports[0].opts.section)) |section| { |
| | 1699 | switch (global.ptrConst(&self.builder).kind) { |
| | 1700 | inline .variable, .function => |impl_index| impl_index.ptr(&self.builder).section = |
| | 1701 | try self.builder.string(section), |
| | 1702 | else => unreachable, |
| | 1703 | } |
| 1343 | llvm_global.setSection(section); | 1704 | llvm_global.setSection(section); |
| 1344 | } | 1705 | } |
| 1345 | if (decl.val.getVariable(mod)) |variable| { | 1706 | if (decl.val.getVariable(mod)) |decl_var| { |
| 1346 | if (variable.is_threadlocal) { | 1707 | if (decl_var.is_threadlocal) { |
| | 1708 | global.ptrConst(&self.builder).kind.variable.ptr(&self.builder).thread_local = |
| | 1709 | .generaldynamic; |
| 1347 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); | 1710 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); |
| 1348 | } | 1711 | } |
| 1349 | } | 1712 | } |
| | 1713 | global.ptr(&self.builder).updateAttributes(); |
| 1350 | | 1714 | |
| 1351 | // If a Decl is exported more than one time (which is rare), | 1715 | // If a Decl is exported more than one time (which is rare), |
| 1352 | // we add aliases for all but the first export. | 1716 | // we add aliases for all but the first export. |
| ... | @@ -1361,7 +1725,7 @@ pub const Object = struct { | ... | @@ -1361,7 +1725,7 @@ pub const Object = struct { |
| 1361 | alias.setAliasee(llvm_global); | 1725 | alias.setAliasee(llvm_global); |
| 1362 | } else { | 1726 | } else { |
| 1363 | _ = self.llvm_module.addAlias( | 1727 | _ = self.llvm_module.addAlias( |
| 1364 | llvm_global.globalGetValueType(), | 1728 | global.ptrConst(&self.builder).type.toLlvm(&self.builder), |
| 1365 | 0, | 1729 | 0, |
| 1366 | llvm_global, | 1730 | llvm_global, |
| 1367 | exp_name_z, | 1731 | exp_name_z, |
| ... | @@ -1369,32 +1733,42 @@ pub const Object = struct { | ... | @@ -1369,32 +1733,42 @@ pub const Object = struct { |
| 1369 | } | 1733 | } |
| 1370 | } | 1734 | } |
| 1371 | } else { | 1735 | } else { |
| 1372 | const fqn = mod.intern_pool.stringToSlice(try decl.getFullyQualifiedName(mod)); | 1736 | const fqn = try self.builder.string(mod.intern_pool.stringToSlice(try decl.getFullyQualifiedName(mod))); |
| 1373 | llvm_global.setValueName2(fqn.ptr, fqn.len); | 1737 | try global.rename(fqn, &self.builder); |
| | 1738 | global.ptr(&self.builder).linkage = .internal; |
| 1374 | llvm_global.setLinkage(.Internal); | 1739 | llvm_global.setLinkage(.Internal); |
| 1375 | if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default); | 1740 | if (mod.wantDllExports()) { |
| | 1741 | global.ptr(&self.builder).dll_storage_class = .default; |
| | 1742 | llvm_global.setDLLStorageClass(.Default); |
| | 1743 | } |
| | 1744 | global.ptr(&self.builder).unnamed_addr = .unnamed_addr; |
| 1376 | llvm_global.setUnnamedAddr(.True); | 1745 | llvm_global.setUnnamedAddr(.True); |
| 1377 | if (decl.val.getVariable(mod)) |variable| { | 1746 | if (decl.val.getVariable(mod)) |decl_var| { |
| 1378 | const single_threaded = mod.comp.bin_file.options.single_threaded; | 1747 | const single_threaded = mod.comp.bin_file.options.single_threaded; |
| 1379 | if (variable.is_threadlocal and !single_threaded) { | 1748 | if (decl_var.is_threadlocal and !single_threaded) { |
| | 1749 | global.ptrConst(&self.builder).kind.variable.ptr(&self.builder).thread_local = |
| | 1750 | .generaldynamic; |
| 1380 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); | 1751 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); |
| 1381 | } else { | 1752 | } else { |
| | 1753 | global.ptrConst(&self.builder).kind.variable.ptr(&self.builder).thread_local = |
| | 1754 | .default; |
| 1382 | llvm_global.setThreadLocalMode(.NotThreadLocal); | 1755 | llvm_global.setThreadLocalMode(.NotThreadLocal); |
| 1383 | } | 1756 | } |
| 1384 | } | 1757 | } |
| | 1758 | global.ptr(&self.builder).updateAttributes(); |
| 1385 | } | 1759 | } |
| 1386 | } | 1760 | } |
| 1387 | | 1761 | |
| 1388 | pub fn freeDecl(self: *Object, decl_index: Module.Decl.Index) void { | 1762 | pub fn freeDecl(self: *Object, decl_index: Module.Decl.Index) void { |
| 1389 | const llvm_value = self.decl_map.get(decl_index) orelse return; | 1763 | const global = self.decl_map.get(decl_index) orelse return; |
| 1390 | llvm_value.deleteGlobal(); | 1764 | global.toLlvm(&self.builder).deleteGlobal(); |
| 1391 | } | 1765 | } |
| 1392 | | 1766 | |
| 1393 | fn getDIFile(o: *Object, gpa: Allocator, file: *const Module.File) !*llvm.DIFile { | 1767 | fn getDIFile(o: *Object, gpa: Allocator, file: *const Module.File) !*llvm.DIFile { |
| 1394 | const gop = try o.di_map.getOrPut(gpa, file); | 1768 | const gop = try o.di_map.getOrPut(gpa, file); |
| 1395 | errdefer assert(o.di_map.remove(file)); | 1769 | errdefer assert(o.di_map.remove(file)); |
| 1396 | if (gop.found_existing) { | 1770 | if (gop.found_existing) { |
| 1397 | return @as(*llvm.DIFile, @ptrCast(gop.value_ptr.*)); | 1771 | return @ptrCast(gop.value_ptr.*); |
| 1398 | } | 1772 | } |
| 1399 | const dir_path_z = d: { | 1773 | const dir_path_z = d: { |
| 1400 | var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; | 1774 | var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | @@ -1542,7 +1916,7 @@ pub const Object = struct { | ... | @@ -1542,7 +1916,7 @@ pub const Object = struct { |
| 1542 | ty.abiSize(mod) * 8, | 1916 | ty.abiSize(mod) * 8, |
| 1543 | ty.abiAlignment(mod) * 8, | 1917 | ty.abiAlignment(mod) * 8, |
| 1544 | enumerators.ptr, | 1918 | enumerators.ptr, |
| 1545 | @as(c_int, @intCast(enumerators.len)), | 1919 | @intCast(enumerators.len), |
| 1546 | try o.lowerDebugType(int_ty, .full), | 1920 | try o.lowerDebugType(int_ty, .full), |
| 1547 | "", | 1921 | "", |
| 1548 | ); | 1922 | ); |
| ... | @@ -1717,7 +2091,7 @@ pub const Object = struct { | ... | @@ -1717,7 +2091,7 @@ pub const Object = struct { |
| 1717 | ty.abiSize(mod) * 8, | 2091 | ty.abiSize(mod) * 8, |
| 1718 | ty.abiAlignment(mod) * 8, | 2092 | ty.abiAlignment(mod) * 8, |
| 1719 | try o.lowerDebugType(ty.childType(mod), .full), | 2093 | try o.lowerDebugType(ty.childType(mod), .full), |
| 1720 | @as(i64, @intCast(ty.arrayLen(mod))), | 2094 | @intCast(ty.arrayLen(mod)), |
| 1721 | ); | 2095 | ); |
| 1722 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. | 2096 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1723 | try o.di_type_map.put(gpa, ty.toIntern(), AnnotatedDITypePtr.initFull(array_di_ty)); | 2097 | try o.di_type_map.put(gpa, ty.toIntern(), AnnotatedDITypePtr.initFull(array_di_ty)); |
| ... | @@ -2022,7 +2396,7 @@ pub const Object = struct { | ... | @@ -2022,7 +2396,7 @@ pub const Object = struct { |
| 2022 | 0, // flags | 2396 | 0, // flags |
| 2023 | null, // derived from | 2397 | null, // derived from |
| 2024 | di_fields.items.ptr, | 2398 | di_fields.items.ptr, |
| 2025 | @as(c_int, @intCast(di_fields.items.len)), | 2399 | @intCast(di_fields.items.len), |
| 2026 | 0, // run time lang | 2400 | 0, // run time lang |
| 2027 | null, // vtable holder | 2401 | null, // vtable holder |
| 2028 | "", // unique id | 2402 | "", // unique id |
| ... | @@ -2109,7 +2483,7 @@ pub const Object = struct { | ... | @@ -2109,7 +2483,7 @@ pub const Object = struct { |
| 2109 | 0, // flags | 2483 | 0, // flags |
| 2110 | null, // derived from | 2484 | null, // derived from |
| 2111 | di_fields.items.ptr, | 2485 | di_fields.items.ptr, |
| 2112 | @as(c_int, @intCast(di_fields.items.len)), | 2486 | @intCast(di_fields.items.len), |
| 2113 | 0, // run time lang | 2487 | 0, // run time lang |
| 2114 | null, // vtable holder | 2488 | null, // vtable holder |
| 2115 | "", // unique id | 2489 | "", // unique id |
| ... | @@ -2221,7 +2595,7 @@ pub const Object = struct { | ... | @@ -2221,7 +2595,7 @@ pub const Object = struct { |
| 2221 | ty.abiAlignment(mod) * 8, // align in bits | 2595 | ty.abiAlignment(mod) * 8, // align in bits |
| 2222 | 0, // flags | 2596 | 0, // flags |
| 2223 | di_fields.items.ptr, | 2597 | di_fields.items.ptr, |
| 2224 | @as(c_int, @intCast(di_fields.items.len)), | 2598 | @intCast(di_fields.items.len), |
| 2225 | 0, // run time lang | 2599 | 0, // run time lang |
| 2226 | "", // unique id | 2600 | "", // unique id |
| 2227 | ); | 2601 | ); |
| ... | @@ -2334,7 +2708,7 @@ pub const Object = struct { | ... | @@ -2334,7 +2708,7 @@ pub const Object = struct { |
| 2334 | | 2708 | |
| 2335 | const fn_di_ty = dib.createSubroutineType( | 2709 | const fn_di_ty = dib.createSubroutineType( |
| 2336 | param_di_types.items.ptr, | 2710 | param_di_types.items.ptr, |
| 2337 | @as(c_int, @intCast(param_di_types.items.len)), | 2711 | @intCast(param_di_types.items.len), |
| 2338 | 0, | 2712 | 0, |
| 2339 | ); | 2713 | ); |
| 2340 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. | 2714 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| ... | @@ -2420,52 +2794,16 @@ pub const Object = struct { | ... | @@ -2420,52 +2794,16 @@ pub const Object = struct { |
| 2420 | return buffer.toOwnedSliceSentinel(0); | 2794 | return buffer.toOwnedSliceSentinel(0); |
| 2421 | } | 2795 | } |
| 2422 | | 2796 | |
| 2423 | fn getNullOptAddr(o: *Object) !*llvm.Value { | | |
| 2424 | if (o.null_opt_addr) |global| return global; | | |
| 2425 | | | |
| 2426 | const mod = o.module; | | |
| 2427 | const target = mod.getTarget(); | | |
| 2428 | const ty = try mod.intern(.{ .opt_type = .usize_type }); | | |
| 2429 | const null_opt_usize = try mod.intern(.{ .opt = .{ | | |
| 2430 | .ty = ty, | | |
| 2431 | .val = .none, | | |
| 2432 | } }); | | |
| 2433 | | | |
| 2434 | const llvm_init = try o.lowerValue(.{ | | |
| 2435 | .ty = ty.toType(), | | |
| 2436 | .val = null_opt_usize.toValue(), | | |
| 2437 | }); | | |
| 2438 | const llvm_wanted_addrspace = toLlvmAddressSpace(.generic, target); | | |
| 2439 | const llvm_actual_addrspace = toLlvmGlobalAddressSpace(.generic, target); | | |
| 2440 | const global = o.llvm_module.addGlobalInAddressSpace( | | |
| 2441 | llvm_init.typeOf(), | | |
| 2442 | "", | | |
| 2443 | llvm_actual_addrspace, | | |
| 2444 | ); | | |
| 2445 | global.setLinkage(.Internal); | | |
| 2446 | global.setUnnamedAddr(.True); | | |
| 2447 | global.setAlignment(ty.toType().abiAlignment(mod)); | | |
| 2448 | global.setInitializer(llvm_init); | | |
| 2449 | | | |
| 2450 | const addrspace_casted_global = if (llvm_wanted_addrspace != llvm_actual_addrspace) | | |
| 2451 | global.constAddrSpaceCast(o.context.pointerType(llvm_wanted_addrspace)) | | |
| 2452 | else | | |
| 2453 | global; | | |
| 2454 | | | |
| 2455 | o.null_opt_addr = addrspace_casted_global; | | |
| 2456 | return addrspace_casted_global; | | |
| 2457 | } | | |
| 2458 | | | |
| 2459 | /// If the llvm function does not exist, create it. | 2797 | /// If the llvm function does not exist, create it. |
| 2460 | /// Note that this can be called before the function's semantic analysis has | 2798 | /// Note that this can be called before the function's semantic analysis has |
| 2461 | /// completed, so if any attributes rely on that, they must be done in updateFunc, not here. | 2799 | /// completed, so if any attributes rely on that, they must be done in updateFunc, not here. |
| 2462 | fn resolveLlvmFunction(o: *Object, decl_index: Module.Decl.Index) !*llvm.Value { | 2800 | fn resolveLlvmFunction(o: *Object, decl_index: Module.Decl.Index) Allocator.Error!Builder.Function.Index { |
| 2463 | const mod = o.module; | 2801 | const mod = o.module; |
| 2464 | const gpa = o.gpa; | 2802 | const gpa = o.gpa; |
| 2465 | const decl = mod.declPtr(decl_index); | 2803 | const decl = mod.declPtr(decl_index); |
| 2466 | const zig_fn_type = decl.ty; | 2804 | const zig_fn_type = decl.ty; |
| 2467 | const gop = try o.decl_map.getOrPut(gpa, decl_index); | 2805 | const gop = try o.decl_map.getOrPut(gpa, decl_index); |
| 2468 | if (gop.found_existing) return gop.value_ptr.*; | 2806 | if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.function; |
| 2469 | | 2807 | |
| 2470 | assert(decl.has_tv); | 2808 | assert(decl.has_tv); |
| 2471 | const fn_info = mod.typeToFunc(zig_fn_type).?; | 2809 | const fn_info = mod.typeToFunc(zig_fn_type).?; |
| ... | @@ -2474,16 +2812,25 @@ pub const Object = struct { | ... | @@ -2474,16 +2812,25 @@ pub const Object = struct { |
| 2474 | | 2812 | |
| 2475 | const fn_type = try o.lowerType(zig_fn_type); | 2813 | const fn_type = try o.lowerType(zig_fn_type); |
| 2476 | | 2814 | |
| 2477 | const fqn = try decl.getFullyQualifiedName(mod); | | |
| 2478 | const ip = &mod.intern_pool; | 2815 | const ip = &mod.intern_pool; |
| | 2816 | const fqn = try o.builder.string(ip.stringToSlice(try decl.getFullyQualifiedName(mod))); |
| 2479 | | 2817 | |
| 2480 | const llvm_addrspace = toLlvmAddressSpace(decl.@"addrspace", target); | 2818 | const llvm_addrspace = toLlvmAddressSpace(decl.@"addrspace", target); |
| 2481 | const llvm_fn = o.llvm_module.addFunctionInAddressSpace(ip.stringToSlice(fqn), fn_type, llvm_addrspace); | 2819 | const llvm_fn = o.llvm_module.addFunctionInAddressSpace(fqn.toSlice(&o.builder).?, fn_type.toLlvm(&o.builder), @intFromEnum(llvm_addrspace)); |
| 2482 | gop.value_ptr.* = llvm_fn; | 2820 | |
| | 2821 | var global = Builder.Global{ |
| | 2822 | .type = fn_type, |
| | 2823 | .kind = .{ .function = @enumFromInt(o.builder.functions.items.len) }, |
| | 2824 | }; |
| | 2825 | var function = Builder.Function{ |
| | 2826 | .global = @enumFromInt(o.builder.globals.count()), |
| | 2827 | }; |
| 2483 | | 2828 | |
| 2484 | const is_extern = decl.isExtern(mod); | 2829 | const is_extern = decl.isExtern(mod); |
| 2485 | if (!is_extern) { | 2830 | if (!is_extern) { |
| | 2831 | global.linkage = .internal; |
| 2486 | llvm_fn.setLinkage(.Internal); | 2832 | llvm_fn.setLinkage(.Internal); |
| | 2833 | global.unnamed_addr = .unnamed_addr; |
| 2487 | llvm_fn.setUnnamedAddr(.True); | 2834 | llvm_fn.setUnnamedAddr(.True); |
| 2488 | } else { | 2835 | } else { |
| 2489 | if (target.isWasm()) { | 2836 | if (target.isWasm()) { |
| ... | @@ -2500,7 +2847,7 @@ pub const Object = struct { | ... | @@ -2500,7 +2847,7 @@ pub const Object = struct { |
| 2500 | o.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0 | 2847 | o.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0 |
| 2501 | o.addArgAttr(llvm_fn, 0, "noalias"); | 2848 | o.addArgAttr(llvm_fn, 0, "noalias"); |
| 2502 | | 2849 | |
| 2503 | const raw_llvm_ret_ty = try o.lowerType(fn_info.return_type.toType()); | 2850 | const raw_llvm_ret_ty = (try o.lowerType(fn_info.return_type.toType())).toLlvm(&o.builder); |
| 2504 | llvm_fn.addSretAttr(raw_llvm_ret_ty); | 2851 | llvm_fn.addSretAttr(raw_llvm_ret_ty); |
| 2505 | } | 2852 | } |
| 2506 | | 2853 | |
| ... | @@ -2528,7 +2875,8 @@ pub const Object = struct { | ... | @@ -2528,7 +2875,8 @@ pub const Object = struct { |
| 2528 | } | 2875 | } |
| 2529 | | 2876 | |
| 2530 | if (fn_info.alignment.toByteUnitsOptional()) |a| { | 2877 | if (fn_info.alignment.toByteUnitsOptional()) |a| { |
| 2531 | llvm_fn.setAlignment(@as(c_uint, @intCast(a))); | 2878 | function.alignment = Builder.Alignment.fromByteUnits(a); |
| | 2879 | llvm_fn.setAlignment(@intCast(a)); |
| 2532 | } | 2880 | } |
| 2533 | | 2881 | |
| 2534 | // Function attributes that are independent of analysis results of the function body. | 2882 | // Function attributes that are independent of analysis results of the function body. |
| ... | @@ -2544,7 +2892,7 @@ pub const Object = struct { | ... | @@ -2544,7 +2892,7 @@ pub const Object = struct { |
| 2544 | var it = iterateParamTypes(o, fn_info); | 2892 | var it = iterateParamTypes(o, fn_info); |
| 2545 | it.llvm_index += @intFromBool(sret); | 2893 | it.llvm_index += @intFromBool(sret); |
| 2546 | it.llvm_index += @intFromBool(err_return_tracing); | 2894 | it.llvm_index += @intFromBool(err_return_tracing); |
| 2547 | while (it.next()) |lowering| switch (lowering) { | 2895 | while (try it.next()) |lowering| switch (lowering) { |
| 2548 | .byval => { | 2896 | .byval => { |
| 2549 | const param_index = it.zig_index - 1; | 2897 | const param_index = it.zig_index - 1; |
| 2550 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); | 2898 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); |
| ... | @@ -2576,7 +2924,10 @@ pub const Object = struct { | ... | @@ -2576,7 +2924,10 @@ pub const Object = struct { |
| 2576 | }; | 2924 | }; |
| 2577 | } | 2925 | } |
| 2578 | | 2926 | |
| 2579 | return llvm_fn; | 2927 | try o.builder.llvm.globals.append(o.gpa, llvm_fn); |
| | 2928 | gop.value_ptr.* = try o.builder.addGlobal(fqn, global); |
| | 2929 | try o.builder.functions.append(o.gpa, function); |
| | 2930 | return global.kind.function; |
| 2580 | } | 2931 | } |
| 2581 | | 2932 | |
| 2582 | fn addCommonFnAttributes(o: *Object, llvm_fn: *llvm.Value) void { | 2933 | fn addCommonFnAttributes(o: *Object, llvm_fn: *llvm.Value) void { |
| ... | @@ -2622,65 +2973,80 @@ pub const Object = struct { | ... | @@ -2622,65 +2973,80 @@ pub const Object = struct { |
| 2622 | } | 2973 | } |
| 2623 | } | 2974 | } |
| 2624 | | 2975 | |
| 2625 | fn resolveGlobalDecl(o: *Object, decl_index: Module.Decl.Index) Error!*llvm.Value { | 2976 | fn resolveGlobalDecl(o: *Object, decl_index: Module.Decl.Index) Allocator.Error!Builder.Variable.Index { |
| 2626 | const gop = try o.decl_map.getOrPut(o.gpa, decl_index); | 2977 | const gop = try o.decl_map.getOrPut(o.gpa, decl_index); |
| 2627 | if (gop.found_existing) return gop.value_ptr.*; | 2978 | if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable; |
| 2628 | errdefer assert(o.decl_map.remove(decl_index)); | 2979 | errdefer assert(o.decl_map.remove(decl_index)); |
| 2629 | | 2980 | |
| 2630 | const mod = o.module; | 2981 | const mod = o.module; |
| 2631 | const decl = mod.declPtr(decl_index); | 2982 | const decl = mod.declPtr(decl_index); |
| 2632 | const fqn = try decl.getFullyQualifiedName(mod); | 2983 | const fqn = try o.builder.string(mod.intern_pool.stringToSlice( |
| | 2984 | try decl.getFullyQualifiedName(mod), |
| | 2985 | )); |
| 2633 | | 2986 | |
| 2634 | const target = mod.getTarget(); | 2987 | const target = mod.getTarget(); |
| 2635 | | 2988 | |
| 2636 | const llvm_type = try o.lowerType(decl.ty); | 2989 | var global = Builder.Global{ |
| 2637 | const llvm_actual_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target); | 2990 | .addr_space = toLlvmGlobalAddressSpace(decl.@"addrspace", target), |
| | 2991 | .type = try o.lowerType(decl.ty), |
| | 2992 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| | 2993 | }; |
| | 2994 | var variable = Builder.Variable{ |
| | 2995 | .global = @enumFromInt(o.builder.globals.count()), |
| | 2996 | }; |
| 2638 | | 2997 | |
| | 2998 | const is_extern = decl.isExtern(mod); |
| | 2999 | const name = if (is_extern) |
| | 3000 | try o.builder.string(mod.intern_pool.stringToSlice(decl.name)) |
| | 3001 | else |
| | 3002 | fqn; |
| 2639 | const llvm_global = o.llvm_module.addGlobalInAddressSpace( | 3003 | const llvm_global = o.llvm_module.addGlobalInAddressSpace( |
| 2640 | llvm_type, | 3004 | global.type.toLlvm(&o.builder), |
| 2641 | mod.intern_pool.stringToSlice(fqn), | 3005 | fqn.toSlice(&o.builder).?, |
| 2642 | llvm_actual_addrspace, | 3006 | @intFromEnum(global.addr_space), |
| 2643 | ); | 3007 | ); |
| 2644 | gop.value_ptr.* = llvm_global; | | |
| 2645 | | 3008 | |
| 2646 | // This is needed for declarations created by `@extern`. | 3009 | // This is needed for declarations created by `@extern`. |
| 2647 | if (decl.isExtern(mod)) { | 3010 | if (is_extern) { |
| 2648 | llvm_global.setValueName(mod.intern_pool.stringToSlice(decl.name)); | 3011 | global.unnamed_addr = .default; |
| 2649 | llvm_global.setUnnamedAddr(.False); | 3012 | llvm_global.setUnnamedAddr(.False); |
| | 3013 | global.linkage = .external; |
| 2650 | llvm_global.setLinkage(.External); | 3014 | llvm_global.setLinkage(.External); |
| 2651 | if (decl.val.getVariable(mod)) |variable| { | 3015 | if (decl.val.getVariable(mod)) |decl_var| { |
| 2652 | const single_threaded = mod.comp.bin_file.options.single_threaded; | 3016 | const single_threaded = mod.comp.bin_file.options.single_threaded; |
| 2653 | if (variable.is_threadlocal and !single_threaded) { | 3017 | if (decl_var.is_threadlocal and !single_threaded) { |
| | 3018 | variable.thread_local = .generaldynamic; |
| 2654 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); | 3019 | llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel); |
| 2655 | } else { | 3020 | } else { |
| | 3021 | variable.thread_local = .default; |
| 2656 | llvm_global.setThreadLocalMode(.NotThreadLocal); | 3022 | llvm_global.setThreadLocalMode(.NotThreadLocal); |
| 2657 | } | 3023 | } |
| 2658 | if (variable.is_weak_linkage) llvm_global.setLinkage(.ExternalWeak); | 3024 | if (decl_var.is_weak_linkage) { |
| | 3025 | global.linkage = .extern_weak; |
| | 3026 | llvm_global.setLinkage(.ExternalWeak); |
| | 3027 | } |
| 2659 | } | 3028 | } |
| 2660 | } else { | 3029 | } else { |
| | 3030 | global.linkage = .internal; |
| 2661 | llvm_global.setLinkage(.Internal); | 3031 | llvm_global.setLinkage(.Internal); |
| | 3032 | global.unnamed_addr = .unnamed_addr; |
| 2662 | llvm_global.setUnnamedAddr(.True); | 3033 | llvm_global.setUnnamedAddr(.True); |
| 2663 | } | 3034 | } |
| 2664 | | 3035 | |
| 2665 | return llvm_global; | 3036 | try o.builder.llvm.globals.append(o.gpa, llvm_global); |
| 2666 | } | 3037 | gop.value_ptr.* = try o.builder.addGlobal(name, global); |
| 2667 | | 3038 | try o.builder.variables.append(o.gpa, variable); |
| 2668 | fn isUnnamedType(o: *Object, ty: Type, val: *llvm.Value) bool { | 3039 | return global.kind.variable; |
| 2669 | // Once `lowerType` succeeds, successive calls to it with the same Zig type | | |
| 2670 | // are guaranteed to succeed. So if a call to `lowerType` fails here it means | | |
| 2671 | // it is the first time lowering the type, which means the value can't possible | | |
| 2672 | // have that type. | | |
| 2673 | const llvm_ty = o.lowerType(ty) catch return true; | | |
| 2674 | return val.typeOf() != llvm_ty; | | |
| 2675 | } | 3040 | } |
| 2676 | | 3041 | |
| 2677 | fn lowerType(o: *Object, t: Type) Allocator.Error!*llvm.Type { | 3042 | fn lowerType(o: *Object, t: Type) Allocator.Error!Builder.Type { |
| 2678 | const llvm_ty = try lowerTypeInner(o, t); | 3043 | const ty = try o.lowerTypeInner(t); |
| 2679 | const mod = o.module; | 3044 | const mod = o.module; |
| 2680 | if (std.debug.runtime_safety and false) check: { | 3045 | if (std.debug.runtime_safety and false) check: { |
| | 3046 | const llvm_ty = ty.toLlvm(&o.builder); |
| 2681 | if (t.zigTypeTag(mod) == .Opaque) break :check; | 3047 | if (t.zigTypeTag(mod) == .Opaque) break :check; |
| 2682 | if (!t.hasRuntimeBits(mod)) break :check; | 3048 | if (!t.hasRuntimeBits(mod)) break :check; |
| 2683 | if (!llvm_ty.isSized().toBool()) break :check; | 3049 | if (!try ty.isSized(&o.builder)) break :check; |
| 2684 | | 3050 | |
| 2685 | const zig_size = t.abiSize(mod); | 3051 | const zig_size = t.abiSize(mod); |
| 2686 | const llvm_size = o.target_data.abiSizeOfType(llvm_ty); | 3052 | const llvm_size = o.target_data.abiSizeOfType(llvm_ty); |
| ... | @@ -2690,456 +3056,511 @@ pub const Object = struct { | ... | @@ -2690,456 +3056,511 @@ pub const Object = struct { |
| 2690 | }); | 3056 | }); |
| 2691 | } | 3057 | } |
| 2692 | } | 3058 | } |
| 2693 | return llvm_ty; | 3059 | return ty; |
| 2694 | } | 3060 | } |
| 2695 | | 3061 | |
| 2696 | fn lowerTypeInner(o: *Object, t: Type) Allocator.Error!*llvm.Type { | 3062 | fn lowerTypeInner(o: *Object, t: Type) Allocator.Error!Builder.Type { |
| 2697 | const gpa = o.gpa; | | |
| 2698 | const mod = o.module; | 3063 | const mod = o.module; |
| 2699 | const target = mod.getTarget(); | 3064 | const target = mod.getTarget(); |
| 2700 | switch (t.zigTypeTag(mod)) { | 3065 | return switch (t.toIntern()) { |
| 2701 | .Void, .NoReturn => return o.context.voidType(), | 3066 | .u0_type, .i0_type => unreachable, |
| 2702 | .Int => { | 3067 | inline .u1_type, |
| 2703 | const info = t.intInfo(mod); | 3068 | .u8_type, |
| 2704 | assert(info.bits != 0); | 3069 | .i8_type, |
| 2705 | return o.context.intType(info.bits); | 3070 | .u16_type, |
| 2706 | }, | 3071 | .i16_type, |
| 2707 | .Enum => { | 3072 | .u29_type, |
| 2708 | const int_ty = t.intTagType(mod); | 3073 | .u32_type, |
| 2709 | const bit_count = int_ty.intInfo(mod).bits; | 3074 | .i32_type, |
| 2710 | assert(bit_count != 0); | 3075 | .u64_type, |
| 2711 | return o.context.intType(bit_count); | 3076 | .i64_type, |
| 2712 | }, | 3077 | .u80_type, |
| 2713 | .Float => switch (t.floatBits(target)) { | 3078 | .u128_type, |
| 2714 | 16 => return if (backendSupportsF16(target)) o.context.halfType() else o.context.intType(16), | 3079 | .i128_type, |
| 2715 | 32 => return o.context.floatType(), | 3080 | => |tag| @field(Builder.Type, "i" ++ @tagName(tag)[1 .. @tagName(tag).len - "_type".len]), |
| 2716 | 64 => return o.context.doubleType(), | 3081 | .usize_type, .isize_type => try o.builder.intType(target.ptrBitWidth()), |
| 2717 | 80 => return if (backendSupportsF80(target)) o.context.x86FP80Type() else o.context.intType(80), | 3082 | inline .c_char_type, |
| 2718 | 128 => return o.context.fp128Type(), | 3083 | .c_short_type, |
| | 3084 | .c_ushort_type, |
| | 3085 | .c_int_type, |
| | 3086 | .c_uint_type, |
| | 3087 | .c_long_type, |
| | 3088 | .c_ulong_type, |
| | 3089 | .c_longlong_type, |
| | 3090 | .c_ulonglong_type, |
| | 3091 | => |tag| try o.builder.intType(target.c_type_bit_size( |
| | 3092 | @field(std.Target.CType, @tagName(tag)["c_".len .. @tagName(tag).len - "_type".len]), |
| | 3093 | )), |
| | 3094 | .c_longdouble_type, |
| | 3095 | .f16_type, |
| | 3096 | .f32_type, |
| | 3097 | .f64_type, |
| | 3098 | .f80_type, |
| | 3099 | .f128_type, |
| | 3100 | => switch (t.floatBits(target)) { |
| | 3101 | 16 => if (backendSupportsF16(target)) .half else .i16, |
| | 3102 | 32 => .float, |
| | 3103 | 64 => .double, |
| | 3104 | 80 => if (backendSupportsF80(target)) .x86_fp80 else .i80, |
| | 3105 | 128 => .fp128, |
| 2719 | else => unreachable, | 3106 | else => unreachable, |
| 2720 | }, | 3107 | }, |
| 2721 | .Bool => return o.context.intType(1), | 3108 | .anyopaque_type => unreachable, |
| 2722 | .Pointer => { | 3109 | .bool_type => .i1, |
| 2723 | if (t.isSlice(mod)) { | 3110 | .void_type => .void, |
| 2724 | const ptr_type = t.slicePtrFieldType(mod); | 3111 | .type_type => unreachable, |
| 2725 | | 3112 | .anyerror_type => Builder.Type.err_int, |
| 2726 | const fields: [2]*llvm.Type = .{ | 3113 | .comptime_int_type, |
| 2727 | try o.lowerType(ptr_type), | 3114 | .comptime_float_type, |
| 2728 | try o.lowerType(Type.usize), | 3115 | .noreturn_type, |
| | 3116 | => unreachable, |
| | 3117 | .anyframe_type => @panic("TODO implement lowerType for AnyFrame types"), |
| | 3118 | .null_type, |
| | 3119 | .undefined_type, |
| | 3120 | .enum_literal_type, |
| | 3121 | .atomic_order_type, |
| | 3122 | .atomic_rmw_op_type, |
| | 3123 | .calling_convention_type, |
| | 3124 | .address_space_type, |
| | 3125 | .float_mode_type, |
| | 3126 | .reduce_op_type, |
| | 3127 | .call_modifier_type, |
| | 3128 | .prefetch_options_type, |
| | 3129 | .export_options_type, |
| | 3130 | .extern_options_type, |
| | 3131 | .type_info_type, |
| | 3132 | => unreachable, |
| | 3133 | .manyptr_u8_type, |
| | 3134 | .manyptr_const_u8_type, |
| | 3135 | .manyptr_const_u8_sentinel_0_type, |
| | 3136 | .single_const_pointer_to_comptime_int_type, |
| | 3137 | => .ptr, |
| | 3138 | .slice_const_u8_type, |
| | 3139 | .slice_const_u8_sentinel_0_type, |
| | 3140 | => try o.builder.structType(.normal, &.{ .ptr, try o.lowerType(Type.usize) }), |
| | 3141 | .optional_noreturn_type => unreachable, |
| | 3142 | .anyerror_void_error_union_type, |
| | 3143 | .adhoc_inferred_error_set_type, |
| | 3144 | => Builder.Type.err_int, |
| | 3145 | .generic_poison_type, |
| | 3146 | .empty_struct_type, |
| | 3147 | => unreachable, |
| | 3148 | // values, not types |
| | 3149 | .undef, |
| | 3150 | .zero, |
| | 3151 | .zero_usize, |
| | 3152 | .zero_u8, |
| | 3153 | .one, |
| | 3154 | .one_usize, |
| | 3155 | .one_u8, |
| | 3156 | .four_u8, |
| | 3157 | .negative_one, |
| | 3158 | .calling_convention_c, |
| | 3159 | .calling_convention_inline, |
| | 3160 | .void_value, |
| | 3161 | .unreachable_value, |
| | 3162 | .null_value, |
| | 3163 | .bool_true, |
| | 3164 | .bool_false, |
| | 3165 | .empty_struct, |
| | 3166 | .generic_poison, |
| | 3167 | .var_args_param_type, |
| | 3168 | .none, |
| | 3169 | => unreachable, |
| | 3170 | else => switch (mod.intern_pool.indexToKey(t.toIntern())) { |
| | 3171 | .int_type => |int_type| try o.builder.intType(int_type.bits), |
| | 3172 | .ptr_type => |ptr_type| type: { |
| | 3173 | const ptr_ty = try o.builder.ptrType( |
| | 3174 | toLlvmAddressSpace(ptr_type.flags.address_space, target), |
| | 3175 | ); |
| | 3176 | break :type switch (ptr_type.flags.size) { |
| | 3177 | .One, .Many, .C => ptr_ty, |
| | 3178 | .Slice => try o.builder.structType(.normal, &.{ |
| | 3179 | ptr_ty, |
| | 3180 | try o.lowerType(Type.usize), |
| | 3181 | }), |
| 2729 | }; | 3182 | }; |
| 2730 | return o.context.structType(&fields, fields.len, .False); | 3183 | }, |
| 2731 | } | 3184 | .array_type => |array_type| o.builder.arrayType( |
| 2732 | const ptr_info = t.ptrInfo(mod); | 3185 | array_type.len + @intFromBool(array_type.sentinel != .none), |
| 2733 | const llvm_addrspace = toLlvmAddressSpace(ptr_info.flags.address_space, target); | 3186 | try o.lowerType(array_type.child.toType()), |
| 2734 | return o.context.pointerType(llvm_addrspace); | 3187 | ), |
| 2735 | }, | 3188 | .vector_type => |vector_type| o.builder.vectorType( |
| 2736 | .Opaque => { | 3189 | .normal, |
| 2737 | if (t.toIntern() == .anyopaque_type) return o.context.intType(8); | 3190 | vector_type.len, |
| 2738 | | 3191 | try o.lowerType(vector_type.child.toType()), |
| 2739 | const gop = try o.type_map.getOrPut(gpa, t.toIntern()); | 3192 | ), |
| 2740 | if (gop.found_existing) return gop.value_ptr.*; | 3193 | .opt_type => |child_ty| { |
| 2741 | | 3194 | if (!child_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) return .i8; |
| 2742 | const opaque_type = mod.intern_pool.indexToKey(t.toIntern()).opaque_type; | 3195 | |
| 2743 | const name = mod.intern_pool.stringToSlice(try mod.opaqueFullyQualifiedName(opaque_type)); | 3196 | const payload_ty = try o.lowerType(child_ty.toType()); |
| 2744 | | 3197 | if (t.optionalReprIsPayload(mod)) return payload_ty; |
| 2745 | const llvm_struct_ty = o.context.structCreateNamed(name); | 3198 | |
| 2746 | gop.value_ptr.* = llvm_struct_ty; // must be done before any recursive calls | 3199 | comptime assert(optional_layout_version == 3); |
| 2747 | return llvm_struct_ty; | 3200 | var fields: [3]Builder.Type = .{ payload_ty, .i8, undefined }; |
| 2748 | }, | 3201 | var fields_len: usize = 2; |
| 2749 | .Array => { | 3202 | const offset = child_ty.toType().abiSize(mod) + 1; |
| 2750 | const elem_ty = t.childType(mod); | 3203 | const abi_size = t.abiSize(mod); |
| 2751 | if (std.debug.runtime_safety) assert((try elem_ty.onePossibleValue(mod)) == null); | 3204 | const padding_len = abi_size - offset; |
| 2752 | const elem_llvm_ty = try o.lowerType(elem_ty); | 3205 | if (padding_len > 0) { |
| 2753 | const total_len = t.arrayLen(mod) + @intFromBool(t.sentinel(mod) != null); | 3206 | fields[2] = try o.builder.arrayType(padding_len, .i8); |
| 2754 | return elem_llvm_ty.arrayType(@as(c_uint, @intCast(total_len))); | 3207 | fields_len = 3; |
| 2755 | }, | | |
| 2756 | .Vector => { | | |
| 2757 | const elem_type = try o.lowerType(t.childType(mod)); | | |
| 2758 | return elem_type.vectorType(t.vectorLen(mod)); | | |
| 2759 | }, | | |
| 2760 | .Optional => { | | |
| 2761 | const child_ty = t.optionalChild(mod); | | |
| 2762 | if (!child_ty.hasRuntimeBitsIgnoreComptime(mod)) { | | |
| 2763 | return o.context.intType(8); | | |
| 2764 | } | | |
| 2765 | const payload_llvm_ty = try o.lowerType(child_ty); | | |
| 2766 | if (t.optionalReprIsPayload(mod)) { | | |
| 2767 | return payload_llvm_ty; | | |
| 2768 | } | | |
| 2769 | | | |
| 2770 | comptime assert(optional_layout_version == 3); | | |
| 2771 | var fields_buf: [3]*llvm.Type = .{ | | |
| 2772 | payload_llvm_ty, o.context.intType(8), undefined, | | |
| 2773 | }; | | |
| 2774 | const offset = child_ty.abiSize(mod) + 1; | | |
| 2775 | const abi_size = t.abiSize(mod); | | |
| 2776 | const padding = @as(c_uint, @intCast(abi_size - offset)); | | |
| 2777 | if (padding == 0) { | | |
| 2778 | return o.context.structType(&fields_buf, 2, .False); | | |
| 2779 | } | | |
| 2780 | fields_buf[2] = o.context.intType(8).arrayType(padding); | | |
| 2781 | return o.context.structType(&fields_buf, 3, .False); | | |
| 2782 | }, | | |
| 2783 | .ErrorUnion => { | | |
| 2784 | const payload_ty = t.errorUnionPayload(mod); | | |
| 2785 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | | |
| 2786 | return try o.lowerType(Type.anyerror); | | |
| 2787 | } | | |
| 2788 | const llvm_error_type = try o.lowerType(Type.anyerror); | | |
| 2789 | const llvm_payload_type = try o.lowerType(payload_ty); | | |
| 2790 | | | |
| 2791 | const payload_align = payload_ty.abiAlignment(mod); | | |
| 2792 | const error_align = Type.anyerror.abiAlignment(mod); | | |
| 2793 | | | |
| 2794 | const payload_size = payload_ty.abiSize(mod); | | |
| 2795 | const error_size = Type.anyerror.abiSize(mod); | | |
| 2796 | | | |
| 2797 | var fields_buf: [3]*llvm.Type = undefined; | | |
| 2798 | if (error_align > payload_align) { | | |
| 2799 | fields_buf[0] = llvm_error_type; | | |
| 2800 | fields_buf[1] = llvm_payload_type; | | |
| 2801 | const payload_end = | | |
| 2802 | std.mem.alignForward(u64, error_size, payload_align) + | | |
| 2803 | payload_size; | | |
| 2804 | const abi_size = std.mem.alignForward(u64, payload_end, error_align); | | |
| 2805 | const padding = @as(c_uint, @intCast(abi_size - payload_end)); | | |
| 2806 | if (padding == 0) { | | |
| 2807 | return o.context.structType(&fields_buf, 2, .False); | | |
| 2808 | } | 3208 | } |
| 2809 | fields_buf[2] = o.context.intType(8).arrayType(padding); | 3209 | return o.builder.structType(.normal, fields[0..fields_len]); |
| 2810 | return o.context.structType(&fields_buf, 3, .False); | 3210 | }, |
| 2811 | } else { | 3211 | .anyframe_type => @panic("TODO implement lowerType for AnyFrame types"), |
| 2812 | fields_buf[0] = llvm_payload_type; | 3212 | .error_union_type => |error_union_type| { |
| 2813 | fields_buf[1] = llvm_error_type; | 3213 | const error_type = Builder.Type.err_int; |
| 2814 | const error_end = | 3214 | if (!error_union_type.payload_type.toType().hasRuntimeBitsIgnoreComptime(mod)) |
| 2815 | std.mem.alignForward(u64, payload_size, error_align) + | 3215 | return error_type; |
| 2816 | error_size; | 3216 | const payload_type = try o.lowerType(error_union_type.payload_type.toType()); |
| 2817 | const abi_size = std.mem.alignForward(u64, error_end, payload_align); | 3217 | |
| 2818 | const padding = @as(c_uint, @intCast(abi_size - error_end)); | 3218 | const payload_align = error_union_type.payload_type.toType().abiAlignment(mod); |
| 2819 | if (padding == 0) { | 3219 | const error_align = Type.err_int.abiAlignment(mod); |
| 2820 | return o.context.structType(&fields_buf, 2, .False); | 3220 | |
| | 3221 | const payload_size = error_union_type.payload_type.toType().abiSize(mod); |
| | 3222 | const error_size = Type.err_int.abiSize(mod); |
| | 3223 | |
| | 3224 | var fields: [3]Builder.Type = undefined; |
| | 3225 | var fields_len: usize = 2; |
| | 3226 | const padding_len = if (error_align > payload_align) pad: { |
| | 3227 | fields[0] = error_type; |
| | 3228 | fields[1] = payload_type; |
| | 3229 | const payload_end = |
| | 3230 | std.mem.alignForward(u64, error_size, payload_align) + |
| | 3231 | payload_size; |
| | 3232 | const abi_size = std.mem.alignForward(u64, payload_end, error_align); |
| | 3233 | break :pad abi_size - payload_end; |
| | 3234 | } else pad: { |
| | 3235 | fields[0] = payload_type; |
| | 3236 | fields[1] = error_type; |
| | 3237 | const error_end = |
| | 3238 | std.mem.alignForward(u64, payload_size, error_align) + |
| | 3239 | error_size; |
| | 3240 | const abi_size = std.mem.alignForward(u64, error_end, payload_align); |
| | 3241 | break :pad abi_size - error_end; |
| | 3242 | }; |
| | 3243 | if (padding_len > 0) { |
| | 3244 | fields[2] = try o.builder.arrayType(padding_len, .i8); |
| | 3245 | fields_len = 3; |
| 2821 | } | 3246 | } |
| 2822 | fields_buf[2] = o.context.intType(8).arrayType(padding); | 3247 | return o.builder.structType(.normal, fields[0..fields_len]); |
| 2823 | return o.context.structType(&fields_buf, 3, .False); | 3248 | }, |
| 2824 | } | 3249 | .simple_type => unreachable, |
| 2825 | }, | 3250 | .struct_type => |struct_type| { |
| 2826 | .ErrorSet => return o.context.intType(16), | 3251 | const gop = try o.type_map.getOrPut(o.gpa, t.toIntern()); |
| 2827 | .Struct => { | 3252 | if (gop.found_existing) return gop.value_ptr.*; |
| 2828 | const gop = try o.type_map.getOrPut(gpa, t.toIntern()); | | |
| 2829 | if (gop.found_existing) return gop.value_ptr.*; | | |
| 2830 | | | |
| 2831 | const struct_type = switch (mod.intern_pool.indexToKey(t.toIntern())) { | | |
| 2832 | .anon_struct_type => |tuple| { | | |
| 2833 | const llvm_struct_ty = o.context.structCreateNamed(""); | | |
| 2834 | gop.value_ptr.* = llvm_struct_ty; // must be done before any recursive calls | | |
| 2835 | | 3253 | |
| 2836 | var llvm_field_types: std.ArrayListUnmanaged(*llvm.Type) = .{}; | 3254 | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; |
| 2837 | defer llvm_field_types.deinit(gpa); | 3255 | if (struct_obj.layout == .Packed) { |
| | 3256 | assert(struct_obj.haveLayout()); |
| | 3257 | const int_ty = try o.lowerType(struct_obj.backing_int_ty); |
| | 3258 | gop.value_ptr.* = int_ty; |
| | 3259 | return int_ty; |
| | 3260 | } |
| 2838 | | 3261 | |
| 2839 | try llvm_field_types.ensureUnusedCapacity(gpa, tuple.types.len); | 3262 | const name = try o.builder.string(mod.intern_pool.stringToSlice( |
| | 3263 | try struct_obj.getFullyQualifiedName(mod), |
| | 3264 | )); |
| | 3265 | const ty = try o.builder.opaqueType(name); |
| | 3266 | gop.value_ptr.* = ty; // must be done before any recursive calls |
| 2840 | | 3267 | |
| 2841 | comptime assert(struct_layout_version == 2); | 3268 | assert(struct_obj.haveFieldTypes()); |
| 2842 | var offset: u64 = 0; | | |
| 2843 | var big_align: u32 = 0; | | |
| 2844 | | 3269 | |
| 2845 | for (tuple.types, tuple.values) |field_ty, field_val| { | 3270 | var llvm_field_types = std.ArrayListUnmanaged(Builder.Type){}; |
| 2846 | if (field_val != .none or !field_ty.toType().hasRuntimeBits(mod)) continue; | 3271 | defer llvm_field_types.deinit(o.gpa); |
| | 3272 | try llvm_field_types.ensureUnusedCapacity(o.gpa, struct_obj.fields.count()); |
| 2847 | | 3273 | |
| 2848 | const field_align = field_ty.toType().abiAlignment(mod); | 3274 | comptime assert(struct_layout_version == 2); |
| 2849 | big_align = @max(big_align, field_align); | 3275 | var offset: u64 = 0; |
| 2850 | const prev_offset = offset; | 3276 | var big_align: u32 = 1; |
| 2851 | offset = std.mem.alignForward(u64, offset, field_align); | 3277 | var struct_kind: Builder.Type.Structure.Kind = .normal; |
| 2852 | | | |
| 2853 | const padding_len = offset - prev_offset; | | |
| 2854 | if (padding_len > 0) { | | |
| 2855 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | | |
| 2856 | try llvm_field_types.append(gpa, llvm_array_ty); | | |
| 2857 | } | | |
| 2858 | const field_llvm_ty = try o.lowerType(field_ty.toType()); | | |
| 2859 | try llvm_field_types.append(gpa, field_llvm_ty); | | |
| 2860 | | 3278 | |
| 2861 | offset += field_ty.toType().abiSize(mod); | 3279 | var it = struct_obj.runtimeFieldIterator(mod); |
| 2862 | } | 3280 | while (it.next()) |field_and_index| { |
| 2863 | { | 3281 | const field = field_and_index.field; |
| 2864 | const prev_offset = offset; | 3282 | const field_align = field.alignment(mod, struct_obj.layout); |
| 2865 | offset = std.mem.alignForward(u64, offset, big_align); | 3283 | const field_ty_align = field.ty.abiAlignment(mod); |
| 2866 | const padding_len = offset - prev_offset; | 3284 | if (field_align < field_ty_align) struct_kind = .@"packed"; |
| 2867 | if (padding_len > 0) { | 3285 | big_align = @max(big_align, field_align); |
| 2868 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | 3286 | const prev_offset = offset; |
| 2869 | try llvm_field_types.append(gpa, llvm_array_ty); | 3287 | offset = std.mem.alignForward(u64, offset, field_align); |
| 2870 | } | | |
| 2871 | } | | |
| 2872 | | 3288 | |
| 2873 | llvm_struct_ty.structSetBody( | 3289 | const padding_len = offset - prev_offset; |
| 2874 | llvm_field_types.items.ptr, | 3290 | if (padding_len > 0) try llvm_field_types.append( |
| 2875 | @as(c_uint, @intCast(llvm_field_types.items.len)), | 3291 | o.gpa, |
| 2876 | .False, | 3292 | try o.builder.arrayType(padding_len, .i8), |
| 2877 | ); | 3293 | ); |
| | 3294 | try llvm_field_types.append(o.gpa, try o.lowerType(field.ty)); |
| 2878 | | 3295 | |
| 2879 | return llvm_struct_ty; | 3296 | offset += field.ty.abiSize(mod); |
| 2880 | }, | 3297 | } |
| 2881 | .struct_type => |struct_type| struct_type, | 3298 | { |
| 2882 | else => unreachable, | 3299 | const prev_offset = offset; |
| 2883 | }; | 3300 | offset = std.mem.alignForward(u64, offset, big_align); |
| 2884 | | 3301 | const padding_len = offset - prev_offset; |
| 2885 | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; | 3302 | if (padding_len > 0) try llvm_field_types.append( |
| 2886 | | 3303 | o.gpa, |
| 2887 | if (struct_obj.layout == .Packed) { | 3304 | try o.builder.arrayType(padding_len, .i8), |
| 2888 | assert(struct_obj.haveLayout()); | 3305 | ); |
| 2889 | const int_llvm_ty = try o.lowerType(struct_obj.backing_int_ty); | 3306 | } |
| 2890 | gop.value_ptr.* = int_llvm_ty; | | |
| 2891 | return int_llvm_ty; | | |
| 2892 | } | | |
| 2893 | | | |
| 2894 | const name = mod.intern_pool.stringToSlice(try struct_obj.getFullyQualifiedName(mod)); | | |
| 2895 | | 3307 | |
| 2896 | const llvm_struct_ty = o.context.structCreateNamed(name); | 3308 | try o.builder.namedTypeSetBody( |
| 2897 | gop.value_ptr.* = llvm_struct_ty; // must be done before any recursive calls | 3309 | ty, |
| | 3310 | try o.builder.structType(struct_kind, llvm_field_types.items), |
| | 3311 | ); |
| | 3312 | return ty; |
| | 3313 | }, |
| | 3314 | .anon_struct_type => |anon_struct_type| { |
| | 3315 | var llvm_field_types: std.ArrayListUnmanaged(Builder.Type) = .{}; |
| | 3316 | defer llvm_field_types.deinit(o.gpa); |
| | 3317 | try llvm_field_types.ensureUnusedCapacity(o.gpa, anon_struct_type.types.len); |
| 2898 | | 3318 | |
| 2899 | assert(struct_obj.haveFieldTypes()); | 3319 | comptime assert(struct_layout_version == 2); |
| | 3320 | var offset: u64 = 0; |
| | 3321 | var big_align: u32 = 0; |
| 2900 | | 3322 | |
| 2901 | var llvm_field_types: std.ArrayListUnmanaged(*llvm.Type) = .{}; | 3323 | for (anon_struct_type.types, anon_struct_type.values) |field_ty, field_val| { |
| 2902 | defer llvm_field_types.deinit(gpa); | 3324 | if (field_val != .none or !field_ty.toType().hasRuntimeBits(mod)) continue; |
| 2903 | | 3325 | |
| 2904 | try llvm_field_types.ensureUnusedCapacity(gpa, struct_obj.fields.count()); | 3326 | const field_align = field_ty.toType().abiAlignment(mod); |
| | 3327 | big_align = @max(big_align, field_align); |
| | 3328 | const prev_offset = offset; |
| | 3329 | offset = std.mem.alignForward(u64, offset, field_align); |
| 2905 | | 3330 | |
| 2906 | comptime assert(struct_layout_version == 2); | 3331 | const padding_len = offset - prev_offset; |
| 2907 | var offset: u64 = 0; | 3332 | if (padding_len > 0) try llvm_field_types.append( |
| 2908 | var big_align: u32 = 1; | 3333 | o.gpa, |
| 2909 | var any_underaligned_fields = false; | 3334 | try o.builder.arrayType(padding_len, .i8), |
| | 3335 | ); |
| | 3336 | try llvm_field_types.append(o.gpa, try o.lowerType(field_ty.toType())); |
| 2910 | | 3337 | |
| 2911 | var it = struct_obj.runtimeFieldIterator(mod); | 3338 | offset += field_ty.toType().abiSize(mod); |
| 2912 | while (it.next()) |field_and_index| { | | |
| 2913 | const field = field_and_index.field; | | |
| 2914 | const field_align = field.alignment(mod, struct_obj.layout); | | |
| 2915 | const field_ty_align = field.ty.abiAlignment(mod); | | |
| 2916 | any_underaligned_fields = any_underaligned_fields or | | |
| 2917 | field_align < field_ty_align; | | |
| 2918 | big_align = @max(big_align, field_align); | | |
| 2919 | const prev_offset = offset; | | |
| 2920 | offset = std.mem.alignForward(u64, offset, field_align); | | |
| 2921 | | | |
| 2922 | const padding_len = offset - prev_offset; | | |
| 2923 | if (padding_len > 0) { | | |
| 2924 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | | |
| 2925 | try llvm_field_types.append(gpa, llvm_array_ty); | | |
| 2926 | } | 3339 | } |
| 2927 | const field_llvm_ty = try o.lowerType(field.ty); | 3340 | { |
| 2928 | try llvm_field_types.append(gpa, field_llvm_ty); | 3341 | const prev_offset = offset; |
| 2929 | | 3342 | offset = std.mem.alignForward(u64, offset, big_align); |
| 2930 | offset += field.ty.abiSize(mod); | 3343 | const padding_len = offset - prev_offset; |
| 2931 | } | 3344 | if (padding_len > 0) try llvm_field_types.append( |
| 2932 | { | 3345 | o.gpa, |
| 2933 | const prev_offset = offset; | 3346 | try o.builder.arrayType(padding_len, .i8), |
| 2934 | offset = std.mem.alignForward(u64, offset, big_align); | 3347 | ); |
| 2935 | const padding_len = offset - prev_offset; | | |
| 2936 | if (padding_len > 0) { | | |
| 2937 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | | |
| 2938 | try llvm_field_types.append(gpa, llvm_array_ty); | | |
| 2939 | } | 3348 | } |
| 2940 | } | 3349 | return o.builder.structType(.normal, llvm_field_types.items); |
| 2941 | | 3350 | }, |
| 2942 | llvm_struct_ty.structSetBody( | 3351 | .union_type => |union_type| { |
| 2943 | llvm_field_types.items.ptr, | 3352 | const gop = try o.type_map.getOrPut(o.gpa, t.toIntern()); |
| 2944 | @as(c_uint, @intCast(llvm_field_types.items.len)), | 3353 | if (gop.found_existing) return gop.value_ptr.*; |
| 2945 | llvm.Bool.fromBool(any_underaligned_fields), | | |
| 2946 | ); | | |
| 2947 | | | |
| 2948 | return llvm_struct_ty; | | |
| 2949 | }, | | |
| 2950 | .Union => { | | |
| 2951 | const gop = try o.type_map.getOrPut(gpa, t.toIntern()); | | |
| 2952 | if (gop.found_existing) return gop.value_ptr.*; | | |
| 2953 | | 3354 | |
| 2954 | const layout = t.unionGetLayout(mod); | 3355 | const union_obj = mod.unionPtr(union_type.index); |
| 2955 | const union_obj = mod.typeToUnion(t).?; | 3356 | const layout = union_obj.getLayout(mod, union_type.hasTag()); |
| 2956 | | 3357 | |
| 2957 | if (union_obj.layout == .Packed) { | 3358 | if (union_obj.layout == .Packed) { |
| 2958 | const bitsize = @as(c_uint, @intCast(t.bitSize(mod))); | 3359 | const int_ty = try o.builder.intType(@intCast(t.bitSize(mod))); |
| 2959 | const int_llvm_ty = o.context.intType(bitsize); | 3360 | gop.value_ptr.* = int_ty; |
| 2960 | gop.value_ptr.* = int_llvm_ty; | 3361 | return int_ty; |
| 2961 | return int_llvm_ty; | 3362 | } |
| 2962 | } | | |
| 2963 | | | |
| 2964 | if (layout.payload_size == 0) { | | |
| 2965 | const enum_tag_llvm_ty = try o.lowerType(union_obj.tag_ty); | | |
| 2966 | gop.value_ptr.* = enum_tag_llvm_ty; | | |
| 2967 | return enum_tag_llvm_ty; | | |
| 2968 | } | | |
| 2969 | | 3363 | |
| 2970 | const name = mod.intern_pool.stringToSlice(try union_obj.getFullyQualifiedName(mod)); | 3364 | if (layout.payload_size == 0) { |
| | 3365 | const enum_tag_ty = try o.lowerType(union_obj.tag_ty); |
| | 3366 | gop.value_ptr.* = enum_tag_ty; |
| | 3367 | return enum_tag_ty; |
| | 3368 | } |
| 2971 | | 3369 | |
| 2972 | const llvm_union_ty = o.context.structCreateNamed(name); | 3370 | const name = try o.builder.string(mod.intern_pool.stringToSlice( |
| 2973 | gop.value_ptr.* = llvm_union_ty; // must be done before any recursive calls | 3371 | try union_obj.getFullyQualifiedName(mod), |
| | 3372 | )); |
| | 3373 | const ty = try o.builder.opaqueType(name); |
| | 3374 | gop.value_ptr.* = ty; // must be done before any recursive calls |
| 2974 | | 3375 | |
| 2975 | const aligned_field = union_obj.fields.values()[layout.most_aligned_field]; | 3376 | const aligned_field = union_obj.fields.values()[layout.most_aligned_field]; |
| 2976 | const llvm_aligned_field_ty = try o.lowerType(aligned_field.ty); | 3377 | const aligned_field_ty = try o.lowerType(aligned_field.ty); |
| 2977 | | 3378 | |
| 2978 | const llvm_payload_ty = t: { | 3379 | const payload_ty = ty: { |
| 2979 | if (layout.most_aligned_field_size == layout.payload_size) { | 3380 | if (layout.most_aligned_field_size == layout.payload_size) { |
| 2980 | break :t llvm_aligned_field_ty; | 3381 | break :ty aligned_field_ty; |
| 2981 | } | 3382 | } |
| 2982 | const padding_len = if (layout.tag_size == 0) | 3383 | const padding_len = if (layout.tag_size == 0) |
| 2983 | @as(c_uint, @intCast(layout.abi_size - layout.most_aligned_field_size)) | 3384 | layout.abi_size - layout.most_aligned_field_size |
| 2984 | else | 3385 | else |
| 2985 | @as(c_uint, @intCast(layout.payload_size - layout.most_aligned_field_size)); | 3386 | layout.payload_size - layout.most_aligned_field_size; |
| 2986 | const fields: [2]*llvm.Type = .{ | 3387 | break :ty try o.builder.structType(.@"packed", &.{ |
| 2987 | llvm_aligned_field_ty, | 3388 | aligned_field_ty, |
| 2988 | o.context.intType(8).arrayType(padding_len), | 3389 | try o.builder.arrayType(padding_len, .i8), |
| | 3390 | }); |
| 2989 | }; | 3391 | }; |
| 2990 | break :t o.context.structType(&fields, fields.len, .True); | | |
| 2991 | }; | | |
| 2992 | | 3392 | |
| 2993 | if (layout.tag_size == 0) { | 3393 | if (layout.tag_size == 0) { |
| 2994 | var llvm_fields: [1]*llvm.Type = .{llvm_payload_ty}; | 3394 | try o.builder.namedTypeSetBody( |
| 2995 | llvm_union_ty.structSetBody(&llvm_fields, llvm_fields.len, .False); | 3395 | ty, |
| 2996 | return llvm_union_ty; | 3396 | try o.builder.structType(.normal, &.{payload_ty}), |
| 2997 | } | 3397 | ); |
| 2998 | const enum_tag_llvm_ty = try o.lowerType(union_obj.tag_ty); | 3398 | return ty; |
| | 3399 | } |
| | 3400 | const enum_tag_ty = try o.lowerType(union_obj.tag_ty); |
| 2999 | | 3401 | |
| 3000 | // Put the tag before or after the payload depending on which one's | 3402 | // Put the tag before or after the payload depending on which one's |
| 3001 | // alignment is greater. | 3403 | // alignment is greater. |
| 3002 | var llvm_fields: [3]*llvm.Type = undefined; | 3404 | var llvm_fields: [3]Builder.Type = undefined; |
| 3003 | var llvm_fields_len: c_uint = 2; | 3405 | var llvm_fields_len: usize = 2; |
| 3004 | | 3406 | |
| 3005 | if (layout.tag_align >= layout.payload_align) { | 3407 | if (layout.tag_align >= layout.payload_align) { |
| 3006 | llvm_fields = .{ enum_tag_llvm_ty, llvm_payload_ty, undefined }; | 3408 | llvm_fields = .{ enum_tag_ty, payload_ty, .none }; |
| 3007 | } else { | 3409 | } else { |
| 3008 | llvm_fields = .{ llvm_payload_ty, enum_tag_llvm_ty, undefined }; | 3410 | llvm_fields = .{ payload_ty, enum_tag_ty, .none }; |
| 3009 | } | 3411 | } |
| 3010 | | 3412 | |
| 3011 | // Insert padding to make the LLVM struct ABI size match the Zig union ABI size. | 3413 | // Insert padding to make the LLVM struct ABI size match the Zig union ABI size. |
| 3012 | if (layout.padding != 0) { | 3414 | if (layout.padding != 0) { |
| 3013 | llvm_fields[2] = o.context.intType(8).arrayType(layout.padding); | 3415 | llvm_fields[llvm_fields_len] = try o.builder.arrayType(layout.padding, .i8); |
| 3014 | llvm_fields_len = 3; | 3416 | llvm_fields_len += 1; |
| 3015 | } | 3417 | } |
| 3016 | | 3418 | |
| 3017 | llvm_union_ty.structSetBody(&llvm_fields, llvm_fields_len, .False); | 3419 | try o.builder.namedTypeSetBody( |
| 3018 | return llvm_union_ty; | 3420 | ty, |
| | 3421 | try o.builder.structType(.normal, llvm_fields[0..llvm_fields_len]), |
| | 3422 | ); |
| | 3423 | return ty; |
| | 3424 | }, |
| | 3425 | .opaque_type => |opaque_type| { |
| | 3426 | const gop = try o.type_map.getOrPut(o.gpa, t.toIntern()); |
| | 3427 | if (!gop.found_existing) { |
| | 3428 | const name = try o.builder.string(mod.intern_pool.stringToSlice( |
| | 3429 | try mod.opaqueFullyQualifiedName(opaque_type), |
| | 3430 | )); |
| | 3431 | gop.value_ptr.* = try o.builder.opaqueType(name); |
| | 3432 | } |
| | 3433 | return gop.value_ptr.*; |
| | 3434 | }, |
| | 3435 | .enum_type => |enum_type| try o.lowerType(enum_type.tag_ty.toType()), |
| | 3436 | .func_type => |func_type| try o.lowerTypeFn(func_type), |
| | 3437 | .error_set_type, .inferred_error_set_type => Builder.Type.err_int, |
| | 3438 | // values, not types |
| | 3439 | .undef, |
| | 3440 | .runtime_value, |
| | 3441 | .simple_value, |
| | 3442 | .variable, |
| | 3443 | .extern_func, |
| | 3444 | .func, |
| | 3445 | .int, |
| | 3446 | .err, |
| | 3447 | .error_union, |
| | 3448 | .enum_literal, |
| | 3449 | .enum_tag, |
| | 3450 | .empty_enum_value, |
| | 3451 | .float, |
| | 3452 | .ptr, |
| | 3453 | .opt, |
| | 3454 | .aggregate, |
| | 3455 | .un, |
| | 3456 | // memoization, not types |
| | 3457 | .memoized_call, |
| | 3458 | => unreachable, |
| 3019 | }, | 3459 | }, |
| 3020 | .Fn => return lowerTypeFn(o, t), | 3460 | }; |
| 3021 | .ComptimeInt => unreachable, | 3461 | } |
| 3022 | .ComptimeFloat => unreachable, | | |
| 3023 | .Type => unreachable, | | |
| 3024 | .Undefined => unreachable, | | |
| 3025 | .Null => unreachable, | | |
| 3026 | .EnumLiteral => unreachable, | | |
| 3027 | | 3462 | |
| 3028 | .Frame => @panic("TODO implement llvmType for Frame types"), | 3463 | /// Use this instead of lowerType when you want to handle correctly the case of elem_ty |
| 3029 | .AnyFrame => @panic("TODO implement llvmType for AnyFrame types"), | 3464 | /// being a zero bit type, but it should still be lowered as an i8 in such case. |
| 3030 | } | 3465 | /// There are other similar cases handled here as well. |
| | 3466 | fn lowerPtrElemTy(o: *Object, elem_ty: Type) Allocator.Error!Builder.Type { |
| | 3467 | const mod = o.module; |
| | 3468 | const lower_elem_ty = switch (elem_ty.zigTypeTag(mod)) { |
| | 3469 | .Opaque => true, |
| | 3470 | .Fn => !mod.typeToFunc(elem_ty).?.is_generic, |
| | 3471 | .Array => elem_ty.childType(mod).hasRuntimeBitsIgnoreComptime(mod), |
| | 3472 | else => elem_ty.hasRuntimeBitsIgnoreComptime(mod), |
| | 3473 | }; |
| | 3474 | return if (lower_elem_ty) try o.lowerType(elem_ty) else .i8; |
| 3031 | } | 3475 | } |
| 3032 | | 3476 | |
| 3033 | fn lowerTypeFn(o: *Object, fn_ty: Type) Allocator.Error!*llvm.Type { | 3477 | fn lowerTypeFn(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 3034 | const mod = o.module; | 3478 | const mod = o.module; |
| 3035 | const ip = &mod.intern_pool; | 3479 | const ip = &mod.intern_pool; |
| 3036 | const fn_info = mod.typeToFunc(fn_ty).?; | 3480 | const target = mod.getTarget(); |
| 3037 | const llvm_ret_ty = try lowerFnRetTy(o, fn_info); | 3481 | const ret_ty = try lowerFnRetTy(o, fn_info); |
| 3038 | | 3482 | |
| 3039 | var llvm_params = std.ArrayList(*llvm.Type).init(o.gpa); | 3483 | var llvm_params = std.ArrayListUnmanaged(Builder.Type){}; |
| 3040 | defer llvm_params.deinit(); | 3484 | defer llvm_params.deinit(o.gpa); |
| 3041 | | 3485 | |
| 3042 | if (firstParamSRet(fn_info, mod)) { | 3486 | if (firstParamSRet(fn_info, mod)) { |
| 3043 | try llvm_params.append(o.context.pointerType(0)); | 3487 | try llvm_params.append(o.gpa, .ptr); |
| 3044 | } | 3488 | } |
| 3045 | | 3489 | |
| 3046 | if (fn_info.return_type.toType().isError(mod) and | 3490 | if (fn_info.return_type.toType().isError(mod) and |
| 3047 | mod.comp.bin_file.options.error_return_tracing) | 3491 | mod.comp.bin_file.options.error_return_tracing) |
| 3048 | { | 3492 | { |
| 3049 | const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType()); | 3493 | const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType()); |
| 3050 | try llvm_params.append(try o.lowerType(ptr_ty)); | 3494 | try llvm_params.append(o.gpa, try o.lowerType(ptr_ty)); |
| 3051 | } | 3495 | } |
| 3052 | | 3496 | |
| 3053 | var it = iterateParamTypes(o, fn_info); | 3497 | var it = iterateParamTypes(o, fn_info); |
| 3054 | while (it.next()) |lowering| switch (lowering) { | 3498 | while (try it.next()) |lowering| switch (lowering) { |
| 3055 | .no_bits => continue, | 3499 | .no_bits => continue, |
| 3056 | .byval => { | 3500 | .byval => { |
| 3057 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | 3501 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 3058 | try llvm_params.append(try o.lowerType(param_ty)); | 3502 | try llvm_params.append(o.gpa, try o.lowerType(param_ty)); |
| 3059 | }, | 3503 | }, |
| 3060 | .byref, .byref_mut => { | 3504 | .byref, .byref_mut => { |
| 3061 | try llvm_params.append(o.context.pointerType(0)); | 3505 | try llvm_params.append(o.gpa, .ptr); |
| 3062 | }, | 3506 | }, |
| 3063 | .abi_sized_int => { | 3507 | .abi_sized_int => { |
| 3064 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | 3508 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 3065 | const abi_size = @as(c_uint, @intCast(param_ty.abiSize(mod))); | 3509 | try llvm_params.append(o.gpa, try o.builder.intType( |
| 3066 | try llvm_params.append(o.context.intType(abi_size * 8)); | 3510 | @intCast(param_ty.abiSize(mod) * 8), |
| | 3511 | )); |
| 3067 | }, | 3512 | }, |
| 3068 | .slice => { | 3513 | .slice => { |
| 3069 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | 3514 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 3070 | const ptr_ty = if (param_ty.zigTypeTag(mod) == .Optional) | 3515 | try llvm_params.appendSlice(o.gpa, &.{ |
| 3071 | param_ty.optionalChild(mod).slicePtrFieldType(mod) | 3516 | try o.builder.ptrType(toLlvmAddressSpace(param_ty.ptrAddressSpace(mod), target)), |
| 3072 | else | 3517 | try o.lowerType(Type.usize), |
| 3073 | param_ty.slicePtrFieldType(mod); | 3518 | }); |
| 3074 | const ptr_llvm_ty = try o.lowerType(ptr_ty); | | |
| 3075 | const len_llvm_ty = try o.lowerType(Type.usize); | | |
| 3076 | | | |
| 3077 | try llvm_params.ensureUnusedCapacity(2); | | |
| 3078 | llvm_params.appendAssumeCapacity(ptr_llvm_ty); | | |
| 3079 | llvm_params.appendAssumeCapacity(len_llvm_ty); | | |
| 3080 | }, | 3519 | }, |
| 3081 | .multiple_llvm_types => { | 3520 | .multiple_llvm_types => { |
| 3082 | try llvm_params.appendSlice(it.llvm_types_buffer[0..it.llvm_types_len]); | 3521 | try llvm_params.appendSlice(o.gpa, it.types_buffer[0..it.types_len]); |
| 3083 | }, | 3522 | }, |
| 3084 | .as_u16 => { | 3523 | .as_u16 => { |
| 3085 | try llvm_params.append(o.context.intType(16)); | 3524 | try llvm_params.append(o.gpa, .i16); |
| 3086 | }, | 3525 | }, |
| 3087 | .float_array => |count| { | 3526 | .float_array => |count| { |
| 3088 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); | 3527 | const param_ty = fn_info.param_types.get(ip)[it.zig_index - 1].toType(); |
| 3089 | const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(param_ty, mod).?); | 3528 | const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(param_ty, mod).?); |
| 3090 | const field_count = @as(c_uint, @intCast(count)); | 3529 | try llvm_params.append(o.gpa, try o.builder.arrayType(count, float_ty)); |
| 3091 | const arr_ty = float_ty.arrayType(field_count); | | |
| 3092 | try llvm_params.append(arr_ty); | | |
| 3093 | }, | 3530 | }, |
| 3094 | .i32_array, .i64_array => |arr_len| { | 3531 | .i32_array, .i64_array => |arr_len| { |
| 3095 | const elem_size: u8 = if (lowering == .i32_array) 32 else 64; | 3532 | try llvm_params.append(o.gpa, try o.builder.arrayType(arr_len, switch (lowering) { |
| 3096 | const arr_ty = o.context.intType(elem_size).arrayType(arr_len); | 3533 | .i32_array => .i32, |
| 3097 | try llvm_params.append(arr_ty); | 3534 | .i64_array => .i64, |
| | 3535 | else => unreachable, |
| | 3536 | })); |
| 3098 | }, | 3537 | }, |
| 3099 | }; | 3538 | }; |
| 3100 | | 3539 | |
| 3101 | return llvm.functionType( | 3540 | return o.builder.fnType( |
| 3102 | llvm_ret_ty, | 3541 | ret_ty, |
| 3103 | llvm_params.items.ptr, | 3542 | llvm_params.items, |
| 3104 | @as(c_uint, @intCast(llvm_params.items.len)), | 3543 | if (fn_info.is_var_args) .vararg else .normal, |
| 3105 | llvm.Bool.fromBool(fn_info.is_var_args), | | |
| 3106 | ); | 3544 | ); |
| 3107 | } | 3545 | } |
| 3108 | | 3546 | |
| 3109 | /// Use this instead of lowerType when you want to handle correctly the case of elem_ty | 3547 | fn lowerValue(o: *Object, arg_val: InternPool.Index) Error!Builder.Constant { |
| 3110 | /// being a zero bit type, but it should still be lowered as an i8 in such case. | | |
| 3111 | /// There are other similar cases handled here as well. | | |
| 3112 | fn lowerPtrElemTy(o: *Object, elem_ty: Type) Allocator.Error!*llvm.Type { | | |
| 3113 | const mod = o.module; | 3548 | const mod = o.module; |
| 3114 | const lower_elem_ty = switch (elem_ty.zigTypeTag(mod)) { | | |
| 3115 | .Opaque => true, | | |
| 3116 | .Fn => !mod.typeToFunc(elem_ty).?.is_generic, | | |
| 3117 | .Array => elem_ty.childType(mod).hasRuntimeBitsIgnoreComptime(mod), | | |
| 3118 | else => elem_ty.hasRuntimeBitsIgnoreComptime(mod), | | |
| 3119 | }; | | |
| 3120 | const llvm_elem_ty = if (lower_elem_ty) | | |
| 3121 | try o.lowerType(elem_ty) | | |
| 3122 | else | | |
| 3123 | o.context.intType(8); | | |
| 3124 | | | |
| 3125 | return llvm_elem_ty; | | |
| 3126 | } | | |
| 3127 | | | |
| 3128 | fn lowerValue(o: *Object, arg_tv: TypedValue) Error!*llvm.Value { | | |
| 3129 | const mod = o.module; | | |
| 3130 | const gpa = o.gpa; | | |
| 3131 | const target = mod.getTarget(); | 3549 | const target = mod.getTarget(); |
| 3132 | var tv = arg_tv; | 3550 | |
| 3133 | switch (mod.intern_pool.indexToKey(tv.val.toIntern())) { | 3551 | var val = arg_val.toValue(); |
| 3134 | .runtime_value => |rt| tv.val = rt.val.toValue(), | 3552 | const arg_val_key = mod.intern_pool.indexToKey(arg_val); |
| | 3553 | switch (arg_val_key) { |
| | 3554 | .runtime_value => |rt| val = rt.val.toValue(), |
| 3135 | else => {}, | 3555 | else => {}, |
| 3136 | } | 3556 | } |
| 3137 | if (tv.val.isUndefDeep(mod)) { | 3557 | if (val.isUndefDeep(mod)) { |
| 3138 | const llvm_type = try o.lowerType(tv.ty); | 3558 | return o.builder.undefConst(try o.lowerType(arg_val_key.typeOf().toType())); |
| 3139 | return llvm_type.getUndef(); | | |
| 3140 | } | 3559 | } |
| 3141 | | 3560 | |
| 3142 | switch (mod.intern_pool.indexToKey(tv.val.toIntern())) { | 3561 | const val_key = mod.intern_pool.indexToKey(val.toIntern()); |
| | 3562 | const ty = val_key.typeOf().toType(); |
| | 3563 | return switch (val_key) { |
| 3143 | .int_type, | 3564 | .int_type, |
| 3144 | .ptr_type, | 3565 | .ptr_type, |
| 3145 | .array_type, | 3566 | .array_type, |
| ... | @@ -3167,10 +3588,8 @@ pub const Object = struct { | ... | @@ -3167,10 +3588,8 @@ pub const Object = struct { |
| 3167 | .@"unreachable", | 3588 | .@"unreachable", |
| 3168 | .generic_poison, | 3589 | .generic_poison, |
| 3169 | => unreachable, // non-runtime values | 3590 | => unreachable, // non-runtime values |
| 3170 | .false, .true => { | 3591 | .false => .false, |
| 3171 | const llvm_type = try o.lowerType(tv.ty); | 3592 | .true => .true, |
| 3172 | return if (tv.val.toBool()) llvm_type.constAllOnes() else llvm_type.constNull(); | | |
| 3173 | }, | | |
| 3174 | }, | 3593 | }, |
| 3175 | .variable, | 3594 | .variable, |
| 3176 | .enum_literal, | 3595 | .enum_literal, |
| ... | @@ -3180,309 +3599,276 @@ pub const Object = struct { | ... | @@ -3180,309 +3599,276 @@ pub const Object = struct { |
| 3180 | const fn_decl_index = extern_func.decl; | 3599 | const fn_decl_index = extern_func.decl; |
| 3181 | const fn_decl = mod.declPtr(fn_decl_index); | 3600 | const fn_decl = mod.declPtr(fn_decl_index); |
| 3182 | try mod.markDeclAlive(fn_decl); | 3601 | try mod.markDeclAlive(fn_decl); |
| 3183 | return o.resolveLlvmFunction(fn_decl_index); | 3602 | const function_index = try o.resolveLlvmFunction(fn_decl_index); |
| | 3603 | return function_index.ptrConst(&o.builder).global.toConst(); |
| 3184 | }, | 3604 | }, |
| 3185 | .func => |func| { | 3605 | .func => |func| { |
| 3186 | const fn_decl_index = func.owner_decl; | 3606 | const fn_decl_index = func.owner_decl; |
| 3187 | const fn_decl = mod.declPtr(fn_decl_index); | 3607 | const fn_decl = mod.declPtr(fn_decl_index); |
| 3188 | try mod.markDeclAlive(fn_decl); | 3608 | try mod.markDeclAlive(fn_decl); |
| 3189 | return o.resolveLlvmFunction(fn_decl_index); | 3609 | const function_index = try o.resolveLlvmFunction(fn_decl_index); |
| | 3610 | return function_index.ptrConst(&o.builder).global.toConst(); |
| 3190 | }, | 3611 | }, |
| 3191 | .int => { | 3612 | .int => { |
| 3192 | var bigint_space: Value.BigIntSpace = undefined; | 3613 | var bigint_space: Value.BigIntSpace = undefined; |
| 3193 | const bigint = tv.val.toBigInt(&bigint_space, mod); | 3614 | const bigint = val.toBigInt(&bigint_space, mod); |
| 3194 | return lowerBigInt(o, tv.ty, bigint); | 3615 | return lowerBigInt(o, ty, bigint); |
| 3195 | }, | 3616 | }, |
| 3196 | .err => |err| { | 3617 | .err => |err| { |
| 3197 | const llvm_ty = try o.lowerType(Type.anyerror); | | |
| 3198 | const int = try mod.getErrorValue(err.name); | 3618 | const int = try mod.getErrorValue(err.name); |
| 3199 | return llvm_ty.constInt(int, .False); | 3619 | const llvm_int = try o.builder.intConst(Builder.Type.err_int, int); |
| | 3620 | return llvm_int; |
| 3200 | }, | 3621 | }, |
| 3201 | .error_union => |error_union| { | 3622 | .error_union => |error_union| { |
| 3202 | const err_tv: TypedValue = switch (error_union.val) { | 3623 | const err_val = switch (error_union.val) { |
| 3203 | .err_name => |err_name| .{ | 3624 | .err_name => |err_name| try mod.intern(.{ .err = .{ |
| 3204 | .ty = tv.ty.errorUnionSet(mod), | 3625 | .ty = ty.errorUnionSet(mod).toIntern(), |
| 3205 | .val = (try mod.intern(.{ .err = .{ | 3626 | .name = err_name, |
| 3206 | .ty = tv.ty.errorUnionSet(mod).toIntern(), | 3627 | } }), |
| 3207 | .name = err_name, | 3628 | .payload => (try mod.intValue(Type.err_int, 0)).toIntern(), |
| 3208 | } })).toValue(), | | |
| 3209 | }, | | |
| 3210 | .payload => .{ | | |
| 3211 | .ty = Type.err_int, | | |
| 3212 | .val = try mod.intValue(Type.err_int, 0), | | |
| 3213 | }, | | |
| 3214 | }; | 3629 | }; |
| 3215 | const payload_type = tv.ty.errorUnionPayload(mod); | 3630 | const payload_type = ty.errorUnionPayload(mod); |
| 3216 | if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) { | 3631 | if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3217 | // We use the error type directly as the type. | 3632 | // We use the error type directly as the type. |
| 3218 | return o.lowerValue(err_tv); | 3633 | return o.lowerValue(err_val); |
| 3219 | } | 3634 | } |
| 3220 | | 3635 | |
| 3221 | const payload_align = payload_type.abiAlignment(mod); | 3636 | const payload_align = payload_type.abiAlignment(mod); |
| 3222 | const error_align = err_tv.ty.abiAlignment(mod); | 3637 | const error_align = Type.err_int.abiAlignment(mod); |
| 3223 | const llvm_error_value = try o.lowerValue(err_tv); | 3638 | const llvm_error_value = try o.lowerValue(err_val); |
| 3224 | const llvm_payload_value = try o.lowerValue(.{ | 3639 | const llvm_payload_value = try o.lowerValue(switch (error_union.val) { |
| 3225 | .ty = payload_type, | 3640 | .err_name => try mod.intern(.{ .undef = payload_type.toIntern() }), |
| 3226 | .val = switch (error_union.val) { | 3641 | .payload => |payload| payload, |
| 3227 | .err_name => try mod.intern(.{ .undef = payload_type.toIntern() }), | | |
| 3228 | .payload => |payload| payload, | | |
| 3229 | }.toValue(), | | |
| 3230 | }); | 3642 | }); |
| 3231 | var fields_buf: [3]*llvm.Value = undefined; | | |
| 3232 | | | |
| 3233 | const llvm_ty = try o.lowerType(tv.ty); | | |
| 3234 | const llvm_field_count = llvm_ty.countStructElementTypes(); | | |
| 3235 | if (llvm_field_count > 2) { | | |
| 3236 | assert(llvm_field_count == 3); | | |
| 3237 | fields_buf[2] = llvm_ty.structGetTypeAtIndex(2).getUndef(); | | |
| 3238 | } | | |
| 3239 | | 3643 | |
| | 3644 | var fields: [3]Builder.Type = undefined; |
| | 3645 | var vals: [3]Builder.Constant = undefined; |
| 3240 | if (error_align > payload_align) { | 3646 | if (error_align > payload_align) { |
| 3241 | fields_buf[0] = llvm_error_value; | 3647 | vals[0] = llvm_error_value; |
| 3242 | fields_buf[1] = llvm_payload_value; | 3648 | vals[1] = llvm_payload_value; |
| 3243 | return o.context.constStruct(&fields_buf, llvm_field_count, .False); | | |
| 3244 | } else { | 3649 | } else { |
| 3245 | fields_buf[0] = llvm_payload_value; | 3650 | vals[0] = llvm_payload_value; |
| 3246 | fields_buf[1] = llvm_error_value; | 3651 | vals[1] = llvm_error_value; |
| 3247 | return o.context.constStruct(&fields_buf, llvm_field_count, .False); | | |
| 3248 | } | 3652 | } |
| 3249 | }, | 3653 | fields[0] = vals[0].typeOf(&o.builder); |
| 3250 | .enum_tag => { | 3654 | fields[1] = vals[1].typeOf(&o.builder); |
| 3251 | const int_val = try tv.intFromEnum(mod); | 3655 | |
| 3252 | | 3656 | const llvm_ty = try o.lowerType(ty); |
| 3253 | var bigint_space: Value.BigIntSpace = undefined; | 3657 | const llvm_ty_fields = llvm_ty.structFields(&o.builder); |
| 3254 | const bigint = int_val.toBigInt(&bigint_space, mod); | 3658 | if (llvm_ty_fields.len > 2) { |
| 3255 | | 3659 | assert(llvm_ty_fields.len == 3); |
| 3256 | const int_info = tv.ty.intInfo(mod); | 3660 | fields[2] = llvm_ty_fields[2]; |
| 3257 | const llvm_type = o.context.intType(int_info.bits); | 3661 | vals[2] = try o.builder.undefConst(fields[2]); |
| 3258 | | | |
| 3259 | const unsigned_val = v: { | | |
| 3260 | if (bigint.limbs.len == 1) { | | |
| 3261 | break :v llvm_type.constInt(bigint.limbs[0], .False); | | |
| 3262 | } | | |
| 3263 | if (@sizeOf(usize) == @sizeOf(u64)) { | | |
| 3264 | break :v llvm_type.constIntOfArbitraryPrecision( | | |
| 3265 | @as(c_uint, @intCast(bigint.limbs.len)), | | |
| 3266 | bigint.limbs.ptr, | | |
| 3267 | ); | | |
| 3268 | } | | |
| 3269 | @panic("TODO implement bigint to llvm int for 32-bit compiler builds"); | | |
| 3270 | }; | | |
| 3271 | if (!bigint.positive) { | | |
| 3272 | return llvm.constNeg(unsigned_val); | | |
| 3273 | } | 3662 | } |
| 3274 | return unsigned_val; | 3663 | return o.builder.structConst(try o.builder.structType( |
| | 3664 | llvm_ty.structKind(&o.builder), |
| | 3665 | fields[0..llvm_ty_fields.len], |
| | 3666 | ), vals[0..llvm_ty_fields.len]); |
| 3275 | }, | 3667 | }, |
| 3276 | .float => { | 3668 | .enum_tag => |enum_tag| o.lowerValue(enum_tag.int), |
| 3277 | const llvm_ty = try o.lowerType(tv.ty); | 3669 | .float => switch (ty.floatBits(target)) { |
| 3278 | switch (tv.ty.floatBits(target)) { | 3670 | 16 => if (backendSupportsF16(target)) |
| 3279 | 16 => { | 3671 | try o.builder.halfConst(val.toFloat(f16, mod)) |
| 3280 | const repr = @as(u16, @bitCast(tv.val.toFloat(f16, mod))); | 3672 | else |
| 3281 | const llvm_i16 = o.context.intType(16); | 3673 | try o.builder.intConst(.i16, @as(i16, @bitCast(val.toFloat(f16, mod)))), |
| 3282 | const int = llvm_i16.constInt(repr, .False); | 3674 | 32 => try o.builder.floatConst(val.toFloat(f32, mod)), |
| 3283 | return int.constBitCast(llvm_ty); | 3675 | 64 => try o.builder.doubleConst(val.toFloat(f64, mod)), |
| 3284 | }, | 3676 | 80 => if (backendSupportsF80(target)) |
| 3285 | 32 => { | 3677 | try o.builder.x86_fp80Const(val.toFloat(f80, mod)) |
| 3286 | const repr = @as(u32, @bitCast(tv.val.toFloat(f32, mod))); | 3678 | else |
| 3287 | const llvm_i32 = o.context.intType(32); | 3679 | try o.builder.intConst(.i80, @as(i80, @bitCast(val.toFloat(f80, mod)))), |
| 3288 | const int = llvm_i32.constInt(repr, .False); | 3680 | 128 => try o.builder.fp128Const(val.toFloat(f128, mod)), |
| 3289 | return int.constBitCast(llvm_ty); | 3681 | else => unreachable, |
| 3290 | }, | | |
| 3291 | 64 => { | | |
| 3292 | const repr = @as(u64, @bitCast(tv.val.toFloat(f64, mod))); | | |
| 3293 | const llvm_i64 = o.context.intType(64); | | |
| 3294 | const int = llvm_i64.constInt(repr, .False); | | |
| 3295 | return int.constBitCast(llvm_ty); | | |
| 3296 | }, | | |
| 3297 | 80 => { | | |
| 3298 | const float = tv.val.toFloat(f80, mod); | | |
| 3299 | const repr = std.math.break_f80(float); | | |
| 3300 | const llvm_i80 = o.context.intType(80); | | |
| 3301 | var x = llvm_i80.constInt(repr.exp, .False); | | |
| 3302 | x = x.constShl(llvm_i80.constInt(64, .False)); | | |
| 3303 | x = x.constOr(llvm_i80.constInt(repr.fraction, .False)); | | |
| 3304 | if (backendSupportsF80(target)) { | | |
| 3305 | return x.constBitCast(llvm_ty); | | |
| 3306 | } else { | | |
| 3307 | return x; | | |
| 3308 | } | | |
| 3309 | }, | | |
| 3310 | 128 => { | | |
| 3311 | var buf: [2]u64 = @as([2]u64, @bitCast(tv.val.toFloat(f128, mod))); | | |
| 3312 | // LLVM seems to require that the lower half of the f128 be placed first | | |
| 3313 | // in the buffer. | | |
| 3314 | if (native_endian == .Big) { | | |
| 3315 | std.mem.swap(u64, &buf[0], &buf[1]); | | |
| 3316 | } | | |
| 3317 | const int = o.context.intType(128).constIntOfArbitraryPrecision(buf.len, &buf); | | |
| 3318 | return int.constBitCast(llvm_ty); | | |
| 3319 | }, | | |
| 3320 | else => unreachable, | | |
| 3321 | } | | |
| 3322 | }, | 3682 | }, |
| 3323 | .ptr => |ptr| { | 3683 | .ptr => |ptr| { |
| 3324 | const ptr_tv: TypedValue = switch (ptr.len) { | 3684 | const ptr_ty = switch (ptr.len) { |
| 3325 | .none => tv, | 3685 | .none => ty, |
| 3326 | else => .{ .ty = tv.ty.slicePtrFieldType(mod), .val = tv.val.slicePtr(mod) }, | 3686 | else => ty.slicePtrFieldType(mod), |
| 3327 | }; | 3687 | }; |
| 3328 | const llvm_ptr_val = switch (ptr.addr) { | 3688 | const ptr_val = switch (ptr.addr) { |
| 3329 | .decl => |decl| try o.lowerDeclRefValue(ptr_tv, decl), | 3689 | .decl => |decl| try o.lowerDeclRefValue(ptr_ty, decl), |
| 3330 | .mut_decl => |mut_decl| try o.lowerDeclRefValue(ptr_tv, mut_decl.decl), | 3690 | .mut_decl => |mut_decl| try o.lowerDeclRefValue(ptr_ty, mut_decl.decl), |
| 3331 | .int => |int| try o.lowerIntAsPtr(int.toValue()), | 3691 | .int => |int| try o.lowerIntAsPtr(int), |
| 3332 | .eu_payload, | 3692 | .eu_payload, |
| 3333 | .opt_payload, | 3693 | .opt_payload, |
| 3334 | .elem, | 3694 | .elem, |
| 3335 | .field, | 3695 | .field, |
| 3336 | => try o.lowerParentPtr(ptr_tv.val, ptr_tv.ty.ptrInfo(mod).packed_offset.bit_offset % 8 == 0), | 3696 | => try o.lowerParentPtr(val, ty.ptrInfo(mod).packed_offset.bit_offset % 8 == 0), |
| 3337 | .comptime_field => unreachable, | 3697 | .comptime_field => unreachable, |
| 3338 | }; | 3698 | }; |
| 3339 | switch (ptr.len) { | 3699 | switch (ptr.len) { |
| 3340 | .none => return llvm_ptr_val, | 3700 | .none => return ptr_val, |
| 3341 | else => { | 3701 | else => return o.builder.structConst(try o.lowerType(ty), &.{ |
| 3342 | const fields: [2]*llvm.Value = .{ | 3702 | ptr_val, try o.lowerValue(ptr.len), |
| 3343 | llvm_ptr_val, | 3703 | }), |
| 3344 | try o.lowerValue(.{ .ty = Type.usize, .val = ptr.len.toValue() }), | | |
| 3345 | }; | | |
| 3346 | return o.context.constStruct(&fields, fields.len, .False); | | |
| 3347 | }, | | |
| 3348 | } | 3704 | } |
| 3349 | }, | 3705 | }, |
| 3350 | .opt => |opt| { | 3706 | .opt => |opt| { |
| 3351 | comptime assert(optional_layout_version == 3); | 3707 | comptime assert(optional_layout_version == 3); |
| 3352 | const payload_ty = tv.ty.optionalChild(mod); | 3708 | const payload_ty = ty.optionalChild(mod); |
| 3353 | | 3709 | |
| 3354 | const llvm_i8 = o.context.intType(8); | 3710 | const non_null_bit = try o.builder.intConst(.i8, @intFromBool(opt.val != .none)); |
| 3355 | const non_null_bit = switch (opt.val) { | | |
| 3356 | .none => llvm_i8.constNull(), | | |
| 3357 | else => llvm_i8.constInt(1, .False), | | |
| 3358 | }; | | |
| 3359 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 3711 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3360 | return non_null_bit; | 3712 | return non_null_bit; |
| 3361 | } | 3713 | } |
| 3362 | const llvm_ty = try o.lowerType(tv.ty); | 3714 | const llvm_ty = try o.lowerType(ty); |
| 3363 | if (tv.ty.optionalReprIsPayload(mod)) return switch (opt.val) { | 3715 | if (ty.optionalReprIsPayload(mod)) return switch (opt.val) { |
| 3364 | .none => llvm_ty.constNull(), | 3716 | .none => switch (llvm_ty.tag(&o.builder)) { |
| 3365 | else => |payload| o.lowerValue(.{ .ty = payload_ty, .val = payload.toValue() }), | 3717 | .integer => try o.builder.intConst(llvm_ty, 0), |
| | 3718 | .pointer => try o.builder.nullConst(llvm_ty), |
| | 3719 | .structure => try o.builder.zeroInitConst(llvm_ty), |
| | 3720 | else => unreachable, |
| | 3721 | }, |
| | 3722 | else => |payload| try o.lowerValue(payload), |
| 3366 | }; | 3723 | }; |
| 3367 | assert(payload_ty.zigTypeTag(mod) != .Fn); | 3724 | assert(payload_ty.zigTypeTag(mod) != .Fn); |
| 3368 | | 3725 | |
| 3369 | const llvm_field_count = llvm_ty.countStructElementTypes(); | 3726 | var fields: [3]Builder.Type = undefined; |
| 3370 | var fields_buf: [3]*llvm.Value = undefined; | 3727 | var vals: [3]Builder.Constant = undefined; |
| 3371 | fields_buf[0] = try o.lowerValue(.{ | 3728 | vals[0] = try o.lowerValue(switch (opt.val) { |
| 3372 | .ty = payload_ty, | 3729 | .none => try mod.intern(.{ .undef = payload_ty.toIntern() }), |
| 3373 | .val = switch (opt.val) { | 3730 | else => |payload| payload, |
| 3374 | .none => try mod.intern(.{ .undef = payload_ty.toIntern() }), | | |
| 3375 | else => |payload| payload, | | |
| 3376 | }.toValue(), | | |
| 3377 | }); | 3731 | }); |
| 3378 | fields_buf[1] = non_null_bit; | 3732 | vals[1] = non_null_bit; |
| 3379 | if (llvm_field_count > 2) { | 3733 | fields[0] = vals[0].typeOf(&o.builder); |
| 3380 | assert(llvm_field_count == 3); | 3734 | fields[1] = vals[1].typeOf(&o.builder); |
| 3381 | fields_buf[2] = llvm_ty.structGetTypeAtIndex(2).getUndef(); | 3735 | |
| | 3736 | const llvm_ty_fields = llvm_ty.structFields(&o.builder); |
| | 3737 | if (llvm_ty_fields.len > 2) { |
| | 3738 | assert(llvm_ty_fields.len == 3); |
| | 3739 | fields[2] = llvm_ty_fields[2]; |
| | 3740 | vals[2] = try o.builder.undefConst(fields[2]); |
| 3382 | } | 3741 | } |
| 3383 | return o.context.constStruct(&fields_buf, llvm_field_count, .False); | 3742 | return o.builder.structConst(try o.builder.structType( |
| | 3743 | llvm_ty.structKind(&o.builder), |
| | 3744 | fields[0..llvm_ty_fields.len], |
| | 3745 | ), vals[0..llvm_ty_fields.len]); |
| 3384 | }, | 3746 | }, |
| 3385 | .aggregate => |aggregate| switch (mod.intern_pool.indexToKey(tv.ty.toIntern())) { | 3747 | .aggregate => |aggregate| switch (mod.intern_pool.indexToKey(ty.toIntern())) { |
| 3386 | .array_type => switch (aggregate.storage) { | 3748 | .array_type => |array_type| switch (aggregate.storage) { |
| 3387 | .bytes => |bytes| return o.context.constString( | 3749 | .bytes => |bytes| try o.builder.stringConst(try o.builder.string(bytes)), |
| 3388 | bytes.ptr, | 3750 | .elems => |elems| { |
| 3389 | @as(c_uint, @intCast(tv.ty.arrayLenIncludingSentinel(mod))), | 3751 | const array_ty = try o.lowerType(ty); |
| 3390 | .True, // Don't null terminate. Bytes has the sentinel, if any. | 3752 | const elem_ty = array_ty.childType(&o.builder); |
| 3391 | ), | 3753 | assert(elems.len == array_ty.aggregateLen(&o.builder)); |
| 3392 | .elems => |elem_vals| { | 3754 | |
| 3393 | const elem_ty = tv.ty.childType(mod); | 3755 | const ExpectedContents = extern struct { |
| 3394 | const llvm_elems = try gpa.alloc(*llvm.Value, elem_vals.len); | 3756 | vals: [Builder.expected_fields_len]Builder.Constant, |
| 3395 | defer gpa.free(llvm_elems); | 3757 | fields: [Builder.expected_fields_len]Builder.Type, |
| | 3758 | }; |
| | 3759 | var stack align(@max( |
| | 3760 | @alignOf(std.heap.StackFallbackAllocator(0)), |
| | 3761 | @alignOf(ExpectedContents), |
| | 3762 | )) = std.heap.stackFallback(@sizeOf(ExpectedContents), o.gpa); |
| | 3763 | const allocator = stack.get(); |
| | 3764 | const vals = try allocator.alloc(Builder.Constant, elems.len); |
| | 3765 | defer allocator.free(vals); |
| | 3766 | const fields = try allocator.alloc(Builder.Type, elems.len); |
| | 3767 | defer allocator.free(fields); |
| | 3768 | |
| 3396 | var need_unnamed = false; | 3769 | var need_unnamed = false; |
| 3397 | for (elem_vals, 0..) |elem_val, i| { | 3770 | for (vals, fields, elems) |*result_val, *result_field, elem| { |
| 3398 | llvm_elems[i] = try o.lowerValue(.{ .ty = elem_ty, .val = elem_val.toValue() }); | 3771 | result_val.* = try o.lowerValue(elem); |
| 3399 | need_unnamed = need_unnamed or o.isUnnamedType(elem_ty, llvm_elems[i]); | 3772 | result_field.* = result_val.typeOf(&o.builder); |
| 3400 | } | 3773 | if (result_field.* != elem_ty) need_unnamed = true; |
| 3401 | if (need_unnamed) { | | |
| 3402 | return o.context.constStruct( | | |
| 3403 | llvm_elems.ptr, | | |
| 3404 | @as(c_uint, @intCast(llvm_elems.len)), | | |
| 3405 | .True, | | |
| 3406 | ); | | |
| 3407 | } else { | | |
| 3408 | const llvm_elem_ty = try o.lowerType(elem_ty); | | |
| 3409 | return llvm_elem_ty.constArray( | | |
| 3410 | llvm_elems.ptr, | | |
| 3411 | @as(c_uint, @intCast(llvm_elems.len)), | | |
| 3412 | ); | | |
| 3413 | } | 3774 | } |
| | 3775 | return if (need_unnamed) try o.builder.structConst( |
| | 3776 | try o.builder.structType(.normal, fields), |
| | 3777 | vals, |
| | 3778 | ) else try o.builder.arrayConst(array_ty, vals); |
| 3414 | }, | 3779 | }, |
| 3415 | .repeated_elem => |val| { | 3780 | .repeated_elem => |elem| { |
| 3416 | const elem_ty = tv.ty.childType(mod); | 3781 | const len: usize = @intCast(array_type.len); |
| 3417 | const sentinel = tv.ty.sentinel(mod); | 3782 | const len_including_sentinel: usize = |
| 3418 | const len = @as(usize, @intCast(tv.ty.arrayLen(mod))); | 3783 | @intCast(len + @intFromBool(array_type.sentinel != .none)); |
| 3419 | const len_including_sent = len + @intFromBool(sentinel != null); | 3784 | const array_ty = try o.lowerType(ty); |
| 3420 | const llvm_elems = try gpa.alloc(*llvm.Value, len_including_sent); | 3785 | const elem_ty = array_ty.childType(&o.builder); |
| 3421 | defer gpa.free(llvm_elems); | 3786 | |
| | 3787 | const ExpectedContents = extern struct { |
| | 3788 | vals: [Builder.expected_fields_len]Builder.Constant, |
| | 3789 | fields: [Builder.expected_fields_len]Builder.Type, |
| | 3790 | }; |
| | 3791 | var stack align(@max( |
| | 3792 | @alignOf(std.heap.StackFallbackAllocator(0)), |
| | 3793 | @alignOf(ExpectedContents), |
| | 3794 | )) = std.heap.stackFallback(@sizeOf(ExpectedContents), o.gpa); |
| | 3795 | const allocator = stack.get(); |
| | 3796 | const vals = try allocator.alloc(Builder.Constant, len_including_sentinel); |
| | 3797 | defer allocator.free(vals); |
| | 3798 | const fields = try allocator.alloc(Builder.Type, len_including_sentinel); |
| | 3799 | defer allocator.free(fields); |
| 3422 | | 3800 | |
| 3423 | var need_unnamed = false; | 3801 | var need_unnamed = false; |
| 3424 | if (len != 0) { | 3802 | @memset(vals[0..len], try o.lowerValue(elem)); |
| 3425 | for (llvm_elems[0..len]) |*elem| { | 3803 | @memset(fields[0..len], vals[0].typeOf(&o.builder)); |
| 3426 | elem.* = try o.lowerValue(.{ .ty = elem_ty, .val = val.toValue() }); | 3804 | if (fields[0] != elem_ty) need_unnamed = true; |
| 3427 | } | 3805 | |
| 3428 | need_unnamed = need_unnamed or o.isUnnamedType(elem_ty, llvm_elems[0]); | 3806 | if (array_type.sentinel != .none) { |
| 3429 | } | 3807 | vals[len] = try o.lowerValue(array_type.sentinel); |
| 3430 | | 3808 | fields[len] = vals[len].typeOf(&o.builder); |
| 3431 | if (sentinel) |sent| { | 3809 | if (fields[len] != elem_ty) need_unnamed = true; |
| 3432 | llvm_elems[len] = try o.lowerValue(.{ .ty = elem_ty, .val = sent }); | | |
| 3433 | need_unnamed = need_unnamed or o.isUnnamedType(elem_ty, llvm_elems[len]); | | |
| 3434 | } | 3810 | } |
| 3435 | | 3811 | |
| 3436 | if (need_unnamed) { | 3812 | return if (need_unnamed) try o.builder.structConst( |
| 3437 | return o.context.constStruct( | 3813 | try o.builder.structType(.@"packed", fields), |
| 3438 | llvm_elems.ptr, | 3814 | vals, |
| 3439 | @as(c_uint, @intCast(llvm_elems.len)), | 3815 | ) else try o.builder.arrayConst(array_ty, vals); |
| 3440 | .True, | | |
| 3441 | ); | | |
| 3442 | } else { | | |
| 3443 | const llvm_elem_ty = try o.lowerType(elem_ty); | | |
| 3444 | return llvm_elem_ty.constArray( | | |
| 3445 | llvm_elems.ptr, | | |
| 3446 | @as(c_uint, @intCast(llvm_elems.len)), | | |
| 3447 | ); | | |
| 3448 | } | | |
| 3449 | }, | 3816 | }, |
| 3450 | }, | 3817 | }, |
| 3451 | .vector_type => |vector_type| { | 3818 | .vector_type => |vector_type| { |
| 3452 | const elem_ty = vector_type.child.toType(); | 3819 | const vector_ty = try o.lowerType(ty); |
| 3453 | const llvm_elems = try gpa.alloc(*llvm.Value, vector_type.len); | 3820 | switch (aggregate.storage) { |
| 3454 | defer gpa.free(llvm_elems); | 3821 | .bytes, .elems => { |
| 3455 | const llvm_i8 = o.context.intType(8); | 3822 | const ExpectedContents = [Builder.expected_fields_len]Builder.Constant; |
| 3456 | for (llvm_elems, 0..) |*llvm_elem, i| { | 3823 | var stack align(@max( |
| 3457 | llvm_elem.* = switch (aggregate.storage) { | 3824 | @alignOf(std.heap.StackFallbackAllocator(0)), |
| 3458 | .bytes => |bytes| llvm_i8.constInt(bytes[i], .False), | 3825 | @alignOf(ExpectedContents), |
| 3459 | .elems => |elems| try o.lowerValue(.{ | 3826 | )) = std.heap.stackFallback(@sizeOf(ExpectedContents), o.gpa); |
| 3460 | .ty = elem_ty, | 3827 | const allocator = stack.get(); |
| 3461 | .val = elems[i].toValue(), | 3828 | const vals = try allocator.alloc(Builder.Constant, vector_type.len); |
| 3462 | }), | 3829 | defer allocator.free(vals); |
| 3463 | .repeated_elem => |elem| try o.lowerValue(.{ | 3830 | |
| 3464 | .ty = elem_ty, | 3831 | switch (aggregate.storage) { |
| 3465 | .val = elem.toValue(), | 3832 | .bytes => |bytes| for (vals, bytes) |*result_val, byte| { |
| 3466 | }), | 3833 | result_val.* = try o.builder.intConst(.i8, byte); |
| 3467 | }; | 3834 | }, |
| | 3835 | .elems => |elems| for (vals, elems) |*result_val, elem| { |
| | 3836 | result_val.* = try o.lowerValue(elem); |
| | 3837 | }, |
| | 3838 | .repeated_elem => unreachable, |
| | 3839 | } |
| | 3840 | return o.builder.vectorConst(vector_ty, vals); |
| | 3841 | }, |
| | 3842 | .repeated_elem => |elem| return o.builder.splatConst( |
| | 3843 | vector_ty, |
| | 3844 | try o.lowerValue(elem), |
| | 3845 | ), |
| 3468 | } | 3846 | } |
| 3469 | return llvm.constVector( | | |
| 3470 | llvm_elems.ptr, | | |
| 3471 | @as(c_uint, @intCast(llvm_elems.len)), | | |
| 3472 | ); | | |
| 3473 | }, | 3847 | }, |
| 3474 | .anon_struct_type => |tuple| { | 3848 | .anon_struct_type => |tuple| { |
| 3475 | var llvm_fields: std.ArrayListUnmanaged(*llvm.Value) = .{}; | 3849 | const struct_ty = try o.lowerType(ty); |
| 3476 | defer llvm_fields.deinit(gpa); | 3850 | const llvm_len = struct_ty.aggregateLen(&o.builder); |
| 3477 | | 3851 | |
| 3478 | try llvm_fields.ensureUnusedCapacity(gpa, tuple.types.len); | 3852 | const ExpectedContents = extern struct { |
| | 3853 | vals: [Builder.expected_fields_len]Builder.Constant, |
| | 3854 | fields: [Builder.expected_fields_len]Builder.Type, |
| | 3855 | }; |
| | 3856 | var stack align(@max( |
| | 3857 | @alignOf(std.heap.StackFallbackAllocator(0)), |
| | 3858 | @alignOf(ExpectedContents), |
| | 3859 | )) = std.heap.stackFallback(@sizeOf(ExpectedContents), o.gpa); |
| | 3860 | const allocator = stack.get(); |
| | 3861 | const vals = try allocator.alloc(Builder.Constant, llvm_len); |
| | 3862 | defer allocator.free(vals); |
| | 3863 | const fields = try allocator.alloc(Builder.Type, llvm_len); |
| | 3864 | defer allocator.free(fields); |
| 3479 | | 3865 | |
| 3480 | comptime assert(struct_layout_version == 2); | 3866 | comptime assert(struct_layout_version == 2); |
| | 3867 | var llvm_index: usize = 0; |
| 3481 | var offset: u64 = 0; | 3868 | var offset: u64 = 0; |
| 3482 | var big_align: u32 = 0; | 3869 | var big_align: u32 = 0; |
| 3483 | var need_unnamed = false; | 3870 | var need_unnamed = false; |
| 3484 | | 3871 | for (tuple.types, tuple.values, 0..) |field_ty, field_val, field_index| { |
| 3485 | for (tuple.types, tuple.values, 0..) |field_ty, field_val, i| { | | |
| 3486 | if (field_val != .none) continue; | 3872 | if (field_val != .none) continue; |
| 3487 | if (!field_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; | 3873 | if (!field_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 3488 | | 3874 | |
| ... | @@ -3493,20 +3879,20 @@ pub const Object = struct { | ... | @@ -3493,20 +3879,20 @@ pub const Object = struct { |
| 3493 | | 3879 | |
| 3494 | const padding_len = offset - prev_offset; | 3880 | const padding_len = offset - prev_offset; |
| 3495 | if (padding_len > 0) { | 3881 | if (padding_len > 0) { |
| 3496 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | | |
| 3497 | // TODO make this and all other padding elsewhere in debug | 3882 | // TODO make this and all other padding elsewhere in debug |
| 3498 | // builds be 0xaa not undef. | 3883 | // builds be 0xaa not undef. |
| 3499 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); | 3884 | fields[llvm_index] = try o.builder.arrayType(padding_len, .i8); |
| | 3885 | vals[llvm_index] = try o.builder.undefConst(fields[llvm_index]); |
| | 3886 | assert(fields[llvm_index] == struct_ty.structFields(&o.builder)[llvm_index]); |
| | 3887 | llvm_index += 1; |
| 3500 | } | 3888 | } |
| 3501 | | 3889 | |
| 3502 | const field_llvm_val = try o.lowerValue(.{ | 3890 | vals[llvm_index] = |
| 3503 | .ty = field_ty.toType(), | 3891 | try o.lowerValue((try val.fieldValue(mod, field_index)).toIntern()); |
| 3504 | .val = try tv.val.fieldValue(mod, i), | 3892 | fields[llvm_index] = vals[llvm_index].typeOf(&o.builder); |
| 3505 | }); | 3893 | if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index]) |
| 3506 | | 3894 | need_unnamed = true; |
| 3507 | need_unnamed = need_unnamed or o.isUnnamedType(field_ty.toType(), field_llvm_val); | 3895 | llvm_index += 1; |
| 3508 | | | |
| 3509 | llvm_fields.appendAssumeCapacity(field_llvm_val); | | |
| 3510 | | 3896 | |
| 3511 | offset += field_ty.toType().abiSize(mod); | 3897 | offset += field_ty.toType().abiSize(mod); |
| 3512 | } | 3898 | } |
| ... | @@ -3515,73 +3901,71 @@ pub const Object = struct { | ... | @@ -3515,73 +3901,71 @@ pub const Object = struct { |
| 3515 | offset = std.mem.alignForward(u64, offset, big_align); | 3901 | offset = std.mem.alignForward(u64, offset, big_align); |
| 3516 | const padding_len = offset - prev_offset; | 3902 | const padding_len = offset - prev_offset; |
| 3517 | if (padding_len > 0) { | 3903 | if (padding_len > 0) { |
| 3518 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | 3904 | fields[llvm_index] = try o.builder.arrayType(padding_len, .i8); |
| 3519 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); | 3905 | vals[llvm_index] = try o.builder.undefConst(fields[llvm_index]); |
| | 3906 | assert(fields[llvm_index] == struct_ty.structFields(&o.builder)[llvm_index]); |
| | 3907 | llvm_index += 1; |
| 3520 | } | 3908 | } |
| 3521 | } | 3909 | } |
| | 3910 | assert(llvm_index == llvm_len); |
| 3522 | | 3911 | |
| 3523 | if (need_unnamed) { | 3912 | return o.builder.structConst(if (need_unnamed) |
| 3524 | return o.context.constStruct( | 3913 | try o.builder.structType(struct_ty.structKind(&o.builder), fields) |
| 3525 | llvm_fields.items.ptr, | 3914 | else |
| 3526 | @as(c_uint, @intCast(llvm_fields.items.len)), | 3915 | struct_ty, vals); |
| 3527 | .False, | | |
| 3528 | ); | | |
| 3529 | } else { | | |
| 3530 | const llvm_struct_ty = try o.lowerType(tv.ty); | | |
| 3531 | return llvm_struct_ty.constNamedStruct( | | |
| 3532 | llvm_fields.items.ptr, | | |
| 3533 | @as(c_uint, @intCast(llvm_fields.items.len)), | | |
| 3534 | ); | | |
| 3535 | } | | |
| 3536 | }, | 3916 | }, |
| 3537 | .struct_type => |struct_type| { | 3917 | .struct_type => |struct_type| { |
| 3538 | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; | 3918 | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; |
| 3539 | const llvm_struct_ty = try o.lowerType(tv.ty); | 3919 | assert(struct_obj.haveLayout()); |
| 3540 | | 3920 | const struct_ty = try o.lowerType(ty); |
| 3541 | if (struct_obj.layout == .Packed) { | 3921 | if (struct_obj.layout == .Packed) { |
| 3542 | assert(struct_obj.haveLayout()); | | |
| 3543 | const big_bits = struct_obj.backing_int_ty.bitSize(mod); | | |
| 3544 | const int_llvm_ty = o.context.intType(@as(c_uint, @intCast(big_bits))); | | |
| 3545 | const fields = struct_obj.fields.values(); | | |
| 3546 | comptime assert(Type.packed_struct_layout_version == 2); | 3922 | comptime assert(Type.packed_struct_layout_version == 2); |
| 3547 | var running_int: *llvm.Value = int_llvm_ty.constNull(); | 3923 | var running_int = try o.builder.intConst(struct_ty, 0); |
| 3548 | var running_bits: u16 = 0; | 3924 | var running_bits: u16 = 0; |
| 3549 | for (fields, 0..) |field, i| { | 3925 | for (struct_obj.fields.values(), 0..) |field, field_index| { |
| 3550 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | 3926 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 3551 | | 3927 | |
| 3552 | const non_int_val = try o.lowerValue(.{ | 3928 | const non_int_val = |
| 3553 | .ty = field.ty, | 3929 | try o.lowerValue((try val.fieldValue(mod, field_index)).toIntern()); |
| 3554 | .val = try tv.val.fieldValue(mod, i), | 3930 | const ty_bit_size: u16 = @intCast(field.ty.bitSize(mod)); |
| 3555 | }); | 3931 | const small_int_ty = try o.builder.intType(ty_bit_size); |
| 3556 | const ty_bit_size = @as(u16, @intCast(field.ty.bitSize(mod))); | 3932 | const small_int_val = try o.builder.castConst( |
| 3557 | const small_int_ty = o.context.intType(ty_bit_size); | 3933 | if (field.ty.isPtrAtRuntime(mod)) .ptrtoint else .bitcast, |
| 3558 | const small_int_val = if (field.ty.isPtrAtRuntime(mod)) | 3934 | non_int_val, |
| 3559 | non_int_val.constPtrToInt(small_int_ty) | 3935 | small_int_ty, |
| 3560 | else | 3936 | ); |
| 3561 | non_int_val.constBitCast(small_int_ty); | 3937 | const shift_rhs = try o.builder.intConst(struct_ty, running_bits); |
| 3562 | const shift_rhs = int_llvm_ty.constInt(running_bits, .False); | 3938 | const extended_int_val = |
| 3563 | // If the field is as large as the entire packed struct, this | 3939 | try o.builder.convConst(.unsigned, small_int_val, struct_ty); |
| 3564 | // zext would go from, e.g. i16 to i16. This is legal with | 3940 | const shifted = try o.builder.binConst(.shl, extended_int_val, shift_rhs); |
| 3565 | // constZExtOrBitCast but not legal with constZExt. | 3941 | running_int = try o.builder.binConst(.@"or", running_int, shifted); |
| 3566 | const extended_int_val = small_int_val.constZExtOrBitCast(int_llvm_ty); | | |
| 3567 | const shifted = extended_int_val.constShl(shift_rhs); | | |
| 3568 | running_int = running_int.constOr(shifted); | | |
| 3569 | running_bits += ty_bit_size; | 3942 | running_bits += ty_bit_size; |
| 3570 | } | 3943 | } |
| 3571 | return running_int; | 3944 | return running_int; |
| 3572 | } | 3945 | } |
| | 3946 | const llvm_len = struct_ty.aggregateLen(&o.builder); |
| 3573 | | 3947 | |
| 3574 | const llvm_field_count = llvm_struct_ty.countStructElementTypes(); | 3948 | const ExpectedContents = extern struct { |
| 3575 | var llvm_fields = try std.ArrayListUnmanaged(*llvm.Value).initCapacity(gpa, llvm_field_count); | 3949 | vals: [Builder.expected_fields_len]Builder.Constant, |
| 3576 | defer llvm_fields.deinit(gpa); | 3950 | fields: [Builder.expected_fields_len]Builder.Type, |
| | 3951 | }; |
| | 3952 | var stack align(@max( |
| | 3953 | @alignOf(std.heap.StackFallbackAllocator(0)), |
| | 3954 | @alignOf(ExpectedContents), |
| | 3955 | )) = std.heap.stackFallback(@sizeOf(ExpectedContents), o.gpa); |
| | 3956 | const allocator = stack.get(); |
| | 3957 | const vals = try allocator.alloc(Builder.Constant, llvm_len); |
| | 3958 | defer allocator.free(vals); |
| | 3959 | const fields = try allocator.alloc(Builder.Type, llvm_len); |
| | 3960 | defer allocator.free(fields); |
| 3577 | | 3961 | |
| 3578 | comptime assert(struct_layout_version == 2); | 3962 | comptime assert(struct_layout_version == 2); |
| | 3963 | var llvm_index: usize = 0; |
| 3579 | var offset: u64 = 0; | 3964 | var offset: u64 = 0; |
| 3580 | var big_align: u32 = 0; | 3965 | var big_align: u32 = 0; |
| 3581 | var need_unnamed = false; | 3966 | var need_unnamed = false; |
| 3582 | | 3967 | var field_it = struct_obj.runtimeFieldIterator(mod); |
| 3583 | var it = struct_obj.runtimeFieldIterator(mod); | 3968 | while (field_it.next()) |field_and_index| { |
| 3584 | while (it.next()) |field_and_index| { | | |
| 3585 | const field = field_and_index.field; | 3969 | const field = field_and_index.field; |
| 3586 | const field_align = field.alignment(mod, struct_obj.layout); | 3970 | const field_align = field.alignment(mod, struct_obj.layout); |
| 3587 | big_align = @max(big_align, field_align); | 3971 | big_align = @max(big_align, field_align); |
| ... | @@ -3590,20 +3974,22 @@ pub const Object = struct { | ... | @@ -3590,20 +3974,22 @@ pub const Object = struct { |
| 3590 | | 3974 | |
| 3591 | const padding_len = offset - prev_offset; | 3975 | const padding_len = offset - prev_offset; |
| 3592 | if (padding_len > 0) { | 3976 | if (padding_len > 0) { |
| 3593 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | | |
| 3594 | // TODO make this and all other padding elsewhere in debug | 3977 | // TODO make this and all other padding elsewhere in debug |
| 3595 | // builds be 0xaa not undef. | 3978 | // builds be 0xaa not undef. |
| 3596 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); | 3979 | fields[llvm_index] = try o.builder.arrayType(padding_len, .i8); |
| | 3980 | vals[llvm_index] = try o.builder.undefConst(fields[llvm_index]); |
| | 3981 | assert(fields[llvm_index] == |
| | 3982 | struct_ty.structFields(&o.builder)[llvm_index]); |
| | 3983 | llvm_index += 1; |
| 3597 | } | 3984 | } |
| 3598 | | 3985 | |
| 3599 | const field_llvm_val = try o.lowerValue(.{ | 3986 | vals[llvm_index] = try o.lowerValue( |
| 3600 | .ty = field.ty, | 3987 | (try val.fieldValue(mod, field_and_index.index)).toIntern(), |
| 3601 | .val = try tv.val.fieldValue(mod, field_and_index.index), | 3988 | ); |
| 3602 | }); | 3989 | fields[llvm_index] = vals[llvm_index].typeOf(&o.builder); |
| 3603 | | 3990 | if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index]) |
| 3604 | need_unnamed = need_unnamed or o.isUnnamedType(field.ty, field_llvm_val); | 3991 | need_unnamed = true; |
| 3605 | | 3992 | llvm_index += 1; |
| 3606 | llvm_fields.appendAssumeCapacity(field_llvm_val); | | |
| 3607 | | 3993 | |
| 3608 | offset += field.ty.abiSize(mod); | 3994 | offset += field.ty.abiSize(mod); |
| 3609 | } | 3995 | } |
| ... | @@ -3612,202 +3998,158 @@ pub const Object = struct { | ... | @@ -3612,202 +3998,158 @@ pub const Object = struct { |
| 3612 | offset = std.mem.alignForward(u64, offset, big_align); | 3998 | offset = std.mem.alignForward(u64, offset, big_align); |
| 3613 | const padding_len = offset - prev_offset; | 3999 | const padding_len = offset - prev_offset; |
| 3614 | if (padding_len > 0) { | 4000 | if (padding_len > 0) { |
| 3615 | const llvm_array_ty = o.context.intType(8).arrayType(@as(c_uint, @intCast(padding_len))); | 4001 | fields[llvm_index] = try o.builder.arrayType(padding_len, .i8); |
| 3616 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); | 4002 | vals[llvm_index] = try o.builder.undefConst(fields[llvm_index]); |
| | 4003 | assert(fields[llvm_index] == struct_ty.structFields(&o.builder)[llvm_index]); |
| | 4004 | llvm_index += 1; |
| 3617 | } | 4005 | } |
| 3618 | } | 4006 | } |
| | 4007 | assert(llvm_index == llvm_len); |
| 3619 | | 4008 | |
| 3620 | if (need_unnamed) { | 4009 | return o.builder.structConst(if (need_unnamed) |
| 3621 | return o.context.constStruct( | 4010 | try o.builder.structType(struct_ty.structKind(&o.builder), fields) |
| 3622 | llvm_fields.items.ptr, | 4011 | else |
| 3623 | @as(c_uint, @intCast(llvm_fields.items.len)), | 4012 | struct_ty, vals); |
| 3624 | .False, | | |
| 3625 | ); | | |
| 3626 | } else { | | |
| 3627 | return llvm_struct_ty.constNamedStruct( | | |
| 3628 | llvm_fields.items.ptr, | | |
| 3629 | @as(c_uint, @intCast(llvm_fields.items.len)), | | |
| 3630 | ); | | |
| 3631 | } | | |
| 3632 | }, | 4013 | }, |
| 3633 | else => unreachable, | 4014 | else => unreachable, |
| 3634 | }, | 4015 | }, |
| 3635 | .un => { | 4016 | .un => |un| { |
| 3636 | const llvm_union_ty = try o.lowerType(tv.ty); | 4017 | const union_ty = try o.lowerType(ty); |
| 3637 | const tag_and_val: Value.Payload.Union.Data = switch (tv.val.toIntern()) { | 4018 | const layout = ty.unionGetLayout(mod); |
| 3638 | .none => tv.val.castTag(.@"union").?.data, | 4019 | if (layout.payload_size == 0) return o.lowerValue(un.tag); |
| 3639 | else => switch (mod.intern_pool.indexToKey(tv.val.toIntern())) { | | |
| 3640 | .un => |un| .{ .tag = un.tag.toValue(), .val = un.val.toValue() }, | | |
| 3641 | else => unreachable, | | |
| 3642 | }, | | |
| 3643 | }; | | |
| 3644 | | | |
| 3645 | const layout = tv.ty.unionGetLayout(mod); | | |
| 3646 | | 4020 | |
| 3647 | if (layout.payload_size == 0) { | 4021 | const union_obj = mod.typeToUnion(ty).?; |
| 3648 | return lowerValue(o, .{ | 4022 | const field_index = ty.unionTagFieldIndex(un.tag.toValue(), o.module).?; |
| 3649 | .ty = tv.ty.unionTagTypeSafety(mod).?, | | |
| 3650 | .val = tag_and_val.tag, | | |
| 3651 | }); | | |
| 3652 | } | | |
| 3653 | const union_obj = mod.typeToUnion(tv.ty).?; | | |
| 3654 | const field_index = tv.ty.unionTagFieldIndex(tag_and_val.tag, o.module).?; | | |
| 3655 | assert(union_obj.haveFieldTypes()); | 4023 | assert(union_obj.haveFieldTypes()); |
| 3656 | | 4024 | |
| 3657 | const field_ty = union_obj.fields.values()[field_index].ty; | 4025 | const field_ty = union_obj.fields.values()[field_index].ty; |
| 3658 | if (union_obj.layout == .Packed) { | 4026 | if (union_obj.layout == .Packed) { |
| 3659 | if (!field_ty.hasRuntimeBits(mod)) | 4027 | if (!field_ty.hasRuntimeBits(mod)) return o.builder.intConst(union_ty, 0); |
| 3660 | return llvm_union_ty.constNull(); | 4028 | const small_int_val = try o.builder.castConst( |
| 3661 | const non_int_val = try lowerValue(o, .{ .ty = field_ty, .val = tag_and_val.val }); | 4029 | if (field_ty.isPtrAtRuntime(mod)) .ptrtoint else .bitcast, |
| 3662 | const ty_bit_size = @as(u16, @intCast(field_ty.bitSize(mod))); | 4030 | try o.lowerValue(un.val), |
| 3663 | const small_int_ty = o.context.intType(ty_bit_size); | 4031 | try o.builder.intType(@intCast(field_ty.bitSize(mod))), |
| 3664 | const small_int_val = if (field_ty.isPtrAtRuntime(mod)) | 4032 | ); |
| 3665 | non_int_val.constPtrToInt(small_int_ty) | 4033 | return o.builder.convConst(.unsigned, small_int_val, union_ty); |
| 3666 | else | | |
| 3667 | non_int_val.constBitCast(small_int_ty); | | |
| 3668 | return small_int_val.constZExtOrBitCast(llvm_union_ty); | | |
| 3669 | } | 4034 | } |
| 3670 | | 4035 | |
| 3671 | // Sometimes we must make an unnamed struct because LLVM does | 4036 | // Sometimes we must make an unnamed struct because LLVM does |
| 3672 | // not support bitcasting our payload struct to the true union payload type. | 4037 | // not support bitcasting our payload struct to the true union payload type. |
| 3673 | // Instead we use an unnamed struct and every reference to the global | 4038 | // Instead we use an unnamed struct and every reference to the global |
| 3674 | // must pointer cast to the expected type before accessing the union. | 4039 | // must pointer cast to the expected type before accessing the union. |
| 3675 | var need_unnamed: bool = layout.most_aligned_field != field_index; | 4040 | var need_unnamed = layout.most_aligned_field != field_index; |
| 3676 | const payload = p: { | 4041 | const payload = p: { |
| 3677 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 4042 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3678 | const padding_len = @as(c_uint, @intCast(layout.payload_size)); | 4043 | const padding_len = layout.payload_size; |
| 3679 | break :p o.context.intType(8).arrayType(padding_len).getUndef(); | 4044 | break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8)); |
| 3680 | } | 4045 | } |
| 3681 | const field = try lowerValue(o, .{ .ty = field_ty, .val = tag_and_val.val }); | 4046 | const payload = try o.lowerValue(un.val); |
| 3682 | need_unnamed = need_unnamed or o.isUnnamedType(field_ty, field); | 4047 | const payload_ty = payload.typeOf(&o.builder); |
| | 4048 | if (payload_ty != union_ty.structFields(&o.builder)[ |
| | 4049 | @intFromBool(layout.tag_align >= layout.payload_align) |
| | 4050 | ]) need_unnamed = true; |
| 3683 | const field_size = field_ty.abiSize(mod); | 4051 | const field_size = field_ty.abiSize(mod); |
| 3684 | if (field_size == layout.payload_size) { | 4052 | if (field_size == layout.payload_size) break :p payload; |
| 3685 | break :p field; | 4053 | const padding_len = layout.payload_size - field_size; |
| 3686 | } | 4054 | const padding_ty = try o.builder.arrayType(padding_len, .i8); |
| 3687 | const padding_len = @as(c_uint, @intCast(layout.payload_size - field_size)); | 4055 | break :p try o.builder.structConst( |
| 3688 | const fields: [2]*llvm.Value = .{ | 4056 | try o.builder.structType(.@"packed", &.{ payload_ty, padding_ty }), |
| 3689 | field, o.context.intType(8).arrayType(padding_len).getUndef(), | 4057 | &.{ payload, try o.builder.undefConst(padding_ty) }, |
| 3690 | }; | 4058 | ); |
| 3691 | break :p o.context.constStruct(&fields, fields.len, .True); | | |
| 3692 | }; | 4059 | }; |
| | 4060 | const payload_ty = payload.typeOf(&o.builder); |
| 3693 | | 4061 | |
| 3694 | if (layout.tag_size == 0) { | 4062 | if (layout.tag_size == 0) return o.builder.structConst(if (need_unnamed) |
| 3695 | const fields: [1]*llvm.Value = .{payload}; | 4063 | try o.builder.structType(union_ty.structKind(&o.builder), &.{payload_ty}) |
| 3696 | if (need_unnamed) { | 4064 | else |
| 3697 | return o.context.constStruct(&fields, fields.len, .False); | 4065 | union_ty, &.{payload}); |
| 3698 | } else { | 4066 | const tag = try o.lowerValue(un.tag); |
| 3699 | return llvm_union_ty.constNamedStruct(&fields, fields.len); | 4067 | const tag_ty = tag.typeOf(&o.builder); |
| 3700 | } | 4068 | var fields: [3]Builder.Type = undefined; |
| 3701 | } | 4069 | var vals: [3]Builder.Constant = undefined; |
| 3702 | const llvm_tag_value = try lowerValue(o, .{ | 4070 | var len: usize = 2; |
| 3703 | .ty = tv.ty.unionTagTypeSafety(mod).?, | | |
| 3704 | .val = tag_and_val.tag, | | |
| 3705 | }); | | |
| 3706 | var fields: [3]*llvm.Value = undefined; | | |
| 3707 | var fields_len: c_uint = 2; | | |
| 3708 | if (layout.tag_align >= layout.payload_align) { | 4071 | if (layout.tag_align >= layout.payload_align) { |
| 3709 | fields = .{ llvm_tag_value, payload, undefined }; | 4072 | fields = .{ tag_ty, payload_ty, undefined }; |
| | 4073 | vals = .{ tag, payload, undefined }; |
| 3710 | } else { | 4074 | } else { |
| 3711 | fields = .{ payload, llvm_tag_value, undefined }; | 4075 | fields = .{ payload_ty, tag_ty, undefined }; |
| | 4076 | vals = .{ payload, tag, undefined }; |
| 3712 | } | 4077 | } |
| 3713 | if (layout.padding != 0) { | 4078 | if (layout.padding != 0) { |
| 3714 | fields[2] = o.context.intType(8).arrayType(layout.padding).getUndef(); | 4079 | fields[2] = try o.builder.arrayType(layout.padding, .i8); |
| 3715 | fields_len = 3; | 4080 | vals[2] = try o.builder.undefConst(fields[2]); |
| 3716 | } | 4081 | len = 3; |
| 3717 | if (need_unnamed) { | | |
| 3718 | return o.context.constStruct(&fields, fields_len, .False); | | |
| 3719 | } else { | | |
| 3720 | return llvm_union_ty.constNamedStruct(&fields, fields_len); | | |
| 3721 | } | 4082 | } |
| | 4083 | return o.builder.structConst(if (need_unnamed) |
| | 4084 | try o.builder.structType(union_ty.structKind(&o.builder), fields[0..len]) |
| | 4085 | else |
| | 4086 | union_ty, vals[0..len]); |
| 3722 | }, | 4087 | }, |
| 3723 | .memoized_call => unreachable, | 4088 | .memoized_call => unreachable, |
| 3724 | } | 4089 | }; |
| 3725 | } | 4090 | } |
| 3726 | | 4091 | |
| 3727 | fn lowerIntAsPtr(o: *Object, val: Value) Error!*llvm.Value { | 4092 | fn lowerIntAsPtr(o: *Object, val: InternPool.Index) Allocator.Error!Builder.Constant { |
| 3728 | const mod = o.module; | 4093 | const mod = o.module; |
| 3729 | switch (mod.intern_pool.indexToKey(val.toIntern())) { | 4094 | switch (mod.intern_pool.indexToKey(val)) { |
| 3730 | .undef => return o.context.pointerType(0).getUndef(), | 4095 | .undef => return o.builder.undefConst(.ptr), |
| 3731 | .int => { | 4096 | .int => { |
| 3732 | var bigint_space: Value.BigIntSpace = undefined; | 4097 | var bigint_space: Value.BigIntSpace = undefined; |
| 3733 | const bigint = val.toBigInt(&bigint_space, mod); | 4098 | const bigint = val.toValue().toBigInt(&bigint_space, mod); |
| 3734 | const llvm_int = lowerBigInt(o, Type.usize, bigint); | 4099 | const llvm_int = try lowerBigInt(o, Type.usize, bigint); |
| 3735 | return llvm_int.constIntToPtr(o.context.pointerType(0)); | 4100 | return o.builder.castConst(.inttoptr, llvm_int, .ptr); |
| 3736 | }, | 4101 | }, |
| 3737 | else => unreachable, | 4102 | else => unreachable, |
| 3738 | } | 4103 | } |
| 3739 | } | 4104 | } |
| 3740 | | 4105 | |
| 3741 | fn lowerBigInt(o: *Object, ty: Type, bigint: std.math.big.int.Const) *llvm.Value { | 4106 | fn lowerBigInt( |
| | 4107 | o: *Object, |
| | 4108 | ty: Type, |
| | 4109 | bigint: std.math.big.int.Const, |
| | 4110 | ) Allocator.Error!Builder.Constant { |
| 3742 | const mod = o.module; | 4111 | const mod = o.module; |
| 3743 | const int_info = ty.intInfo(mod); | 4112 | return o.builder.bigIntConst(try o.builder.intType(ty.intInfo(mod).bits), bigint); |
| 3744 | assert(int_info.bits != 0); | | |
| 3745 | const llvm_type = o.context.intType(int_info.bits); | | |
| 3746 | | | |
| 3747 | const unsigned_val = v: { | | |
| 3748 | if (bigint.limbs.len == 1) { | | |
| 3749 | break :v llvm_type.constInt(bigint.limbs[0], .False); | | |
| 3750 | } | | |
| 3751 | if (@sizeOf(usize) == @sizeOf(u64)) { | | |
| 3752 | break :v llvm_type.constIntOfArbitraryPrecision( | | |
| 3753 | @as(c_uint, @intCast(bigint.limbs.len)), | | |
| 3754 | bigint.limbs.ptr, | | |
| 3755 | ); | | |
| 3756 | } | | |
| 3757 | @panic("TODO implement bigint to llvm int for 32-bit compiler builds"); | | |
| 3758 | }; | | |
| 3759 | if (!bigint.positive) { | | |
| 3760 | return llvm.constNeg(unsigned_val); | | |
| 3761 | } | | |
| 3762 | return unsigned_val; | | |
| 3763 | } | 4113 | } |
| 3764 | | 4114 | |
| 3765 | const ParentPtr = struct { | 4115 | const ParentPtr = struct { |
| 3766 | ty: Type, | 4116 | ty: Type, |
| 3767 | llvm_ptr: *llvm.Value, | 4117 | llvm_ptr: Builder.Value, |
| 3768 | }; | 4118 | }; |
| 3769 | | 4119 | |
| 3770 | fn lowerParentPtrDecl( | 4120 | fn lowerParentPtrDecl(o: *Object, decl_index: Module.Decl.Index) Allocator.Error!Builder.Constant { |
| 3771 | o: *Object, | | |
| 3772 | ptr_val: Value, | | |
| 3773 | decl_index: Module.Decl.Index, | | |
| 3774 | ) Error!*llvm.Value { | | |
| 3775 | const mod = o.module; | 4121 | const mod = o.module; |
| 3776 | const decl = mod.declPtr(decl_index); | 4122 | const decl = mod.declPtr(decl_index); |
| 3777 | try mod.markDeclAlive(decl); | 4123 | try mod.markDeclAlive(decl); |
| 3778 | const ptr_ty = try mod.singleMutPtrType(decl.ty); | 4124 | const ptr_ty = try mod.singleMutPtrType(decl.ty); |
| 3779 | return try o.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index); | 4125 | return o.lowerDeclRefValue(ptr_ty, decl_index); |
| 3780 | } | 4126 | } |
| 3781 | | 4127 | |
| 3782 | fn lowerParentPtr(o: *Object, ptr_val: Value, byte_aligned: bool) Error!*llvm.Value { | 4128 | fn lowerParentPtr(o: *Object, ptr_val: Value, byte_aligned: bool) Allocator.Error!Builder.Constant { |
| 3783 | const mod = o.module; | 4129 | const mod = o.module; |
| 3784 | const target = mod.getTarget(); | | |
| 3785 | return switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) { | 4130 | return switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) { |
| 3786 | .decl => |decl| o.lowerParentPtrDecl(ptr_val, decl), | 4131 | .decl => |decl| o.lowerParentPtrDecl(decl), |
| 3787 | .mut_decl => |mut_decl| o.lowerParentPtrDecl(ptr_val, mut_decl.decl), | 4132 | .mut_decl => |mut_decl| o.lowerParentPtrDecl(mut_decl.decl), |
| 3788 | .int => |int| o.lowerIntAsPtr(int.toValue()), | 4133 | .int => |int| try o.lowerIntAsPtr(int), |
| 3789 | .eu_payload => |eu_ptr| { | 4134 | .eu_payload => |eu_ptr| { |
| 3790 | const parent_llvm_ptr = try o.lowerParentPtr(eu_ptr.toValue(), true); | 4135 | const parent_ptr = try o.lowerParentPtr(eu_ptr.toValue(), true); |
| 3791 | | 4136 | |
| 3792 | const eu_ty = mod.intern_pool.typeOf(eu_ptr).toType().childType(mod); | 4137 | const eu_ty = mod.intern_pool.typeOf(eu_ptr).toType().childType(mod); |
| 3793 | const payload_ty = eu_ty.errorUnionPayload(mod); | 4138 | const payload_ty = eu_ty.errorUnionPayload(mod); |
| 3794 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 4139 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3795 | // In this case, we represent pointer to error union the same as pointer | 4140 | // In this case, we represent pointer to error union the same as pointer |
| 3796 | // to the payload. | 4141 | // to the payload. |
| 3797 | return parent_llvm_ptr; | 4142 | return parent_ptr; |
| 3798 | } | 4143 | } |
| 3799 | | 4144 | |
| 3800 | const payload_offset: u8 = if (payload_ty.abiAlignment(mod) > Type.anyerror.abiSize(mod)) 2 else 1; | 4145 | const index: u32 = |
| 3801 | const llvm_u32 = o.context.intType(32); | 4146 | if (payload_ty.abiAlignment(mod) > Type.err_int.abiSize(mod)) 2 else 1; |
| 3802 | const indices: [2]*llvm.Value = .{ | 4147 | return o.builder.gepConst(.inbounds, try o.lowerType(eu_ty), parent_ptr, null, &.{ |
| 3803 | llvm_u32.constInt(0, .False), | 4148 | try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, index), |
| 3804 | llvm_u32.constInt(payload_offset, .False), | 4149 | }); |
| 3805 | }; | | |
| 3806 | const eu_llvm_ty = try o.lowerType(eu_ty); | | |
| 3807 | return eu_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | | |
| 3808 | }, | 4150 | }, |
| 3809 | .opt_payload => |opt_ptr| { | 4151 | .opt_payload => |opt_ptr| { |
| 3810 | const parent_llvm_ptr = try o.lowerParentPtr(opt_ptr.toValue(), true); | 4152 | const parent_ptr = try o.lowerParentPtr(opt_ptr.toValue(), true); |
| 3811 | | 4153 | |
| 3812 | const opt_ty = mod.intern_pool.typeOf(opt_ptr).toType().childType(mod); | 4154 | const opt_ty = mod.intern_pool.typeOf(opt_ptr).toType().childType(mod); |
| 3813 | const payload_ty = opt_ty.optionalChild(mod); | 4155 | const payload_ty = opt_ty.optionalChild(mod); |
| ... | @@ -3816,99 +4158,89 @@ pub const Object = struct { | ... | @@ -3816,99 +4158,89 @@ pub const Object = struct { |
| 3816 | { | 4158 | { |
| 3817 | // In this case, we represent pointer to optional the same as pointer | 4159 | // In this case, we represent pointer to optional the same as pointer |
| 3818 | // to the payload. | 4160 | // to the payload. |
| 3819 | return parent_llvm_ptr; | 4161 | return parent_ptr; |
| 3820 | } | 4162 | } |
| 3821 | | 4163 | |
| 3822 | const llvm_u32 = o.context.intType(32); | 4164 | return o.builder.gepConst(.inbounds, try o.lowerType(opt_ty), parent_ptr, null, &.{ |
| 3823 | const indices: [2]*llvm.Value = .{ | 4165 | try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, 0), |
| 3824 | llvm_u32.constInt(0, .False), | 4166 | }); |
| 3825 | llvm_u32.constInt(0, .False), | | |
| 3826 | }; | | |
| 3827 | const opt_llvm_ty = try o.lowerType(opt_ty); | | |
| 3828 | return opt_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | | |
| 3829 | }, | 4167 | }, |
| 3830 | .comptime_field => unreachable, | 4168 | .comptime_field => unreachable, |
| 3831 | .elem => |elem_ptr| { | 4169 | .elem => |elem_ptr| { |
| 3832 | const parent_llvm_ptr = try o.lowerParentPtr(elem_ptr.base.toValue(), true); | 4170 | const parent_ptr = try o.lowerParentPtr(elem_ptr.base.toValue(), true); |
| 3833 | | | |
| 3834 | const llvm_usize = try o.lowerType(Type.usize); | | |
| 3835 | const indices: [1]*llvm.Value = .{ | | |
| 3836 | llvm_usize.constInt(elem_ptr.index, .False), | | |
| 3837 | }; | | |
| 3838 | const elem_ty = mod.intern_pool.typeOf(elem_ptr.base).toType().elemType2(mod); | 4171 | const elem_ty = mod.intern_pool.typeOf(elem_ptr.base).toType().elemType2(mod); |
| 3839 | const elem_llvm_ty = try o.lowerType(elem_ty); | 4172 | |
| 3840 | return elem_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4173 | return o.builder.gepConst(.inbounds, try o.lowerType(elem_ty), parent_ptr, null, &.{ |
| | 4174 | try o.builder.intConst(try o.lowerType(Type.usize), elem_ptr.index), |
| | 4175 | }); |
| 3841 | }, | 4176 | }, |
| 3842 | .field => |field_ptr| { | 4177 | .field => |field_ptr| { |
| 3843 | const parent_llvm_ptr = try o.lowerParentPtr(field_ptr.base.toValue(), byte_aligned); | 4178 | const parent_ptr = try o.lowerParentPtr(field_ptr.base.toValue(), byte_aligned); |
| 3844 | const parent_ty = mod.intern_pool.typeOf(field_ptr.base).toType().childType(mod); | 4179 | const parent_ty = mod.intern_pool.typeOf(field_ptr.base).toType().childType(mod); |
| 3845 | | 4180 | |
| 3846 | const field_index = @as(u32, @intCast(field_ptr.index)); | 4181 | const field_index: u32 = @intCast(field_ptr.index); |
| 3847 | const llvm_u32 = o.context.intType(32); | | |
| 3848 | switch (parent_ty.zigTypeTag(mod)) { | 4182 | switch (parent_ty.zigTypeTag(mod)) { |
| 3849 | .Union => { | 4183 | .Union => { |
| 3850 | if (parent_ty.containerLayout(mod) == .Packed) { | 4184 | if (parent_ty.containerLayout(mod) == .Packed) { |
| 3851 | return parent_llvm_ptr; | 4185 | return parent_ptr; |
| 3852 | } | 4186 | } |
| 3853 | | 4187 | |
| 3854 | const layout = parent_ty.unionGetLayout(mod); | 4188 | const layout = parent_ty.unionGetLayout(mod); |
| 3855 | if (layout.payload_size == 0) { | 4189 | if (layout.payload_size == 0) { |
| 3856 | // In this case a pointer to the union and a pointer to any | 4190 | // In this case a pointer to the union and a pointer to any |
| 3857 | // (void) payload is the same. | 4191 | // (void) payload is the same. |
| 3858 | return parent_llvm_ptr; | 4192 | return parent_ptr; |
| 3859 | } | 4193 | } |
| 3860 | const llvm_pl_index = if (layout.tag_size == 0) | 4194 | |
| 3861 | 0 | | |
| 3862 | else | | |
| 3863 | @intFromBool(layout.tag_align >= layout.payload_align); | | |
| 3864 | const indices: [2]*llvm.Value = .{ | | |
| 3865 | llvm_u32.constInt(0, .False), | | |
| 3866 | llvm_u32.constInt(llvm_pl_index, .False), | | |
| 3867 | }; | | |
| 3868 | const parent_llvm_ty = try o.lowerType(parent_ty); | 4195 | const parent_llvm_ty = try o.lowerType(parent_ty); |
| 3869 | return parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4196 | return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{ |
| | 4197 | try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, @intFromBool( |
| | 4198 | layout.tag_size > 0 and layout.tag_align >= layout.payload_align, |
| | 4199 | )), |
| | 4200 | }); |
| 3870 | }, | 4201 | }, |
| 3871 | .Struct => { | 4202 | .Struct => { |
| 3872 | if (parent_ty.containerLayout(mod) == .Packed) { | 4203 | if (parent_ty.containerLayout(mod) == .Packed) { |
| 3873 | if (!byte_aligned) return parent_llvm_ptr; | 4204 | if (!byte_aligned) return parent_ptr; |
| 3874 | const llvm_usize = o.context.intType(target.ptrBitWidth()); | 4205 | const llvm_usize = try o.lowerType(Type.usize); |
| 3875 | const base_addr = parent_llvm_ptr.constPtrToInt(llvm_usize); | 4206 | const base_addr = |
| | 4207 | try o.builder.castConst(.ptrtoint, parent_ptr, llvm_usize); |
| 3876 | // count bits of fields before this one | 4208 | // count bits of fields before this one |
| 3877 | const prev_bits = b: { | 4209 | const prev_bits = b: { |
| 3878 | var b: usize = 0; | 4210 | var b: usize = 0; |
| 3879 | for (parent_ty.structFields(mod).values()[0..field_index]) |field| { | 4211 | for (parent_ty.structFields(mod).values()[0..field_index]) |field| { |
| 3880 | if (field.is_comptime or !field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | 4212 | if (field.is_comptime) continue; |
| 3881 | b += @as(usize, @intCast(field.ty.bitSize(mod))); | 4213 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| | 4214 | b += @intCast(field.ty.bitSize(mod)); |
| 3882 | } | 4215 | } |
| 3883 | break :b b; | 4216 | break :b b; |
| 3884 | }; | 4217 | }; |
| 3885 | const byte_offset = llvm_usize.constInt(prev_bits / 8, .False); | 4218 | const byte_offset = try o.builder.intConst(llvm_usize, prev_bits / 8); |
| 3886 | const field_addr = base_addr.constAdd(byte_offset); | 4219 | const field_addr = try o.builder.binConst(.add, base_addr, byte_offset); |
| 3887 | const final_llvm_ty = o.context.pointerType(0); | 4220 | return o.builder.castConst(.inttoptr, field_addr, .ptr); |
| 3888 | return field_addr.constIntToPtr(final_llvm_ty); | | |
| 3889 | } | 4221 | } |
| 3890 | | 4222 | |
| 3891 | const parent_llvm_ty = try o.lowerType(parent_ty); | 4223 | return o.builder.gepConst( |
| 3892 | if (llvmField(parent_ty, field_index, mod)) |llvm_field| { | 4224 | .inbounds, |
| 3893 | const indices: [2]*llvm.Value = .{ | 4225 | try o.lowerType(parent_ty), |
| 3894 | llvm_u32.constInt(0, .False), | 4226 | parent_ptr, |
| 3895 | llvm_u32.constInt(llvm_field.index, .False), | 4227 | null, |
| 3896 | }; | 4228 | if (llvmField(parent_ty, field_index, mod)) |llvm_field| &.{ |
| 3897 | return parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4229 | try o.builder.intConst(.i32, 0), |
| 3898 | } else { | 4230 | try o.builder.intConst(.i32, llvm_field.index), |
| 3899 | const llvm_index = llvm_u32.constInt(@intFromBool(parent_ty.hasRuntimeBitsIgnoreComptime(mod)), .False); | 4231 | } else &.{ |
| 3900 | const indices: [1]*llvm.Value = .{llvm_index}; | 4232 | try o.builder.intConst(.i32, @intFromBool( |
| 3901 | return parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4233 | parent_ty.hasRuntimeBitsIgnoreComptime(mod), |
| 3902 | } | 4234 | )), |
| | 4235 | }, |
| | 4236 | ); |
| 3903 | }, | 4237 | }, |
| 3904 | .Pointer => { | 4238 | .Pointer => { |
| 3905 | assert(parent_ty.isSlice(mod)); | 4239 | assert(parent_ty.isSlice(mod)); |
| 3906 | const indices: [2]*llvm.Value = .{ | | |
| 3907 | llvm_u32.constInt(0, .False), | | |
| 3908 | llvm_u32.constInt(field_index, .False), | | |
| 3909 | }; | | |
| 3910 | const parent_llvm_ty = try o.lowerType(parent_ty); | 4240 | const parent_llvm_ty = try o.lowerType(parent_ty); |
| 3911 | return parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4241 | return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{ |
| | 4242 | try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, field_index), |
| | 4243 | }); |
| 3912 | }, | 4244 | }, |
| 3913 | else => unreachable, | 4245 | else => unreachable, |
| 3914 | } | 4246 | } |
| ... | @@ -3916,11 +4248,7 @@ pub const Object = struct { | ... | @@ -3916,11 +4248,7 @@ pub const Object = struct { |
| 3916 | }; | 4248 | }; |
| 3917 | } | 4249 | } |
| 3918 | | 4250 | |
| 3919 | fn lowerDeclRefValue( | 4251 | fn lowerDeclRefValue(o: *Object, ty: Type, decl_index: Module.Decl.Index) Allocator.Error!Builder.Constant { |
| 3920 | o: *Object, | | |
| 3921 | tv: TypedValue, | | |
| 3922 | decl_index: Module.Decl.Index, | | |
| 3923 | ) Error!*llvm.Value { | | |
| 3924 | const mod = o.module; | 4252 | const mod = o.module; |
| 3925 | | 4253 | |
| 3926 | // In the case of something like: | 4254 | // In the case of something like: |
| ... | @@ -3931,69 +4259,59 @@ pub const Object = struct { | ... | @@ -3931,69 +4259,59 @@ pub const Object = struct { |
| 3931 | const decl = mod.declPtr(decl_index); | 4259 | const decl = mod.declPtr(decl_index); |
| 3932 | if (decl.val.getFunction(mod)) |func| { | 4260 | if (decl.val.getFunction(mod)) |func| { |
| 3933 | if (func.owner_decl != decl_index) { | 4261 | if (func.owner_decl != decl_index) { |
| 3934 | return o.lowerDeclRefValue(tv, func.owner_decl); | 4262 | return o.lowerDeclRefValue(ty, func.owner_decl); |
| 3935 | } | 4263 | } |
| 3936 | } else if (decl.val.getExternFunc(mod)) |func| { | 4264 | } else if (decl.val.getExternFunc(mod)) |func| { |
| 3937 | if (func.decl != decl_index) { | 4265 | if (func.decl != decl_index) { |
| 3938 | return o.lowerDeclRefValue(tv, func.decl); | 4266 | return o.lowerDeclRefValue(ty, func.decl); |
| 3939 | } | 4267 | } |
| 3940 | } | 4268 | } |
| 3941 | | 4269 | |
| 3942 | const is_fn_body = decl.ty.zigTypeTag(mod) == .Fn; | 4270 | const is_fn_body = decl.ty.zigTypeTag(mod) == .Fn; |
| 3943 | if ((!is_fn_body and !decl.ty.hasRuntimeBits(mod)) or | 4271 | if ((!is_fn_body and !decl.ty.hasRuntimeBits(mod)) or |
| 3944 | (is_fn_body and mod.typeToFunc(decl.ty).?.is_generic)) | 4272 | (is_fn_body and mod.typeToFunc(decl.ty).?.is_generic)) return o.lowerPtrToVoid(ty); |
| 3945 | { | | |
| 3946 | return o.lowerPtrToVoid(tv.ty); | | |
| 3947 | } | | |
| 3948 | | 4273 | |
| 3949 | try mod.markDeclAlive(decl); | 4274 | try mod.markDeclAlive(decl); |
| 3950 | | 4275 | |
| 3951 | const llvm_decl_val = if (is_fn_body) | 4276 | const llvm_global = if (is_fn_body) |
| 3952 | try o.resolveLlvmFunction(decl_index) | 4277 | (try o.resolveLlvmFunction(decl_index)).ptrConst(&o.builder).global |
| 3953 | else | 4278 | else |
| 3954 | try o.resolveGlobalDecl(decl_index); | 4279 | (try o.resolveGlobalDecl(decl_index)).ptrConst(&o.builder).global; |
| 3955 | | 4280 | |
| 3956 | const target = mod.getTarget(); | 4281 | const llvm_val = try o.builder.convConst( |
| 3957 | const llvm_wanted_addrspace = toLlvmAddressSpace(decl.@"addrspace", target); | 4282 | .unneeded, |
| 3958 | const llvm_actual_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target); | 4283 | llvm_global.toConst(), |
| 3959 | const llvm_val = if (llvm_wanted_addrspace != llvm_actual_addrspace) blk: { | 4284 | try o.builder.ptrType(toLlvmAddressSpace(decl.@"addrspace", mod.getTarget())), |
| 3960 | const llvm_decl_wanted_ptr_ty = o.context.pointerType(llvm_wanted_addrspace); | 4285 | ); |
| 3961 | break :blk llvm_decl_val.constAddrSpaceCast(llvm_decl_wanted_ptr_ty); | 4286 | |
| 3962 | } else llvm_decl_val; | 4287 | return o.builder.convConst(if (ty.isAbiInt(mod)) switch (ty.intInfo(mod).signedness) { |
| 3963 | | 4288 | .signed => .signed, |
| 3964 | const llvm_type = try o.lowerType(tv.ty); | 4289 | .unsigned => .unsigned, |
| 3965 | if (tv.ty.zigTypeTag(mod) == .Int) { | 4290 | } else .unneeded, llvm_val, try o.lowerType(ty)); |
| 3966 | return llvm_val.constPtrToInt(llvm_type); | | |
| 3967 | } else { | | |
| 3968 | return llvm_val.constBitCast(llvm_type); | | |
| 3969 | } | | |
| 3970 | } | 4291 | } |
| 3971 | | 4292 | |
| 3972 | fn lowerPtrToVoid(o: *Object, ptr_ty: Type) !*llvm.Value { | 4293 | fn lowerPtrToVoid(o: *Object, ptr_ty: Type) Allocator.Error!Builder.Constant { |
| 3973 | const mod = o.module; | 4294 | const mod = o.module; |
| 3974 | // Even though we are pointing at something which has zero bits (e.g. `void`), | 4295 | // Even though we are pointing at something which has zero bits (e.g. `void`), |
| 3975 | // Pointers are defined to have bits. So we must return something here. | 4296 | // Pointers are defined to have bits. So we must return something here. |
| 3976 | // The value cannot be undefined, because we use the `nonnull` annotation | 4297 | // The value cannot be undefined, because we use the `nonnull` annotation |
| 3977 | // for non-optional pointers. We also need to respect the alignment, even though | 4298 | // for non-optional pointers. We also need to respect the alignment, even though |
| 3978 | // the address will never be dereferenced. | 4299 | // the address will never be dereferenced. |
| 3979 | const llvm_usize = try o.lowerType(Type.usize); | 4300 | const int: u64 = ptr_ty.ptrInfo(mod).flags.alignment.toByteUnitsOptional() orelse |
| 3980 | const llvm_ptr_ty = try o.lowerType(ptr_ty); | 4301 | // Note that these 0xaa values are appropriate even in release-optimized builds |
| 3981 | if (ptr_ty.ptrInfo(mod).flags.alignment.toByteUnitsOptional()) |alignment| { | 4302 | // because we need a well-defined value that is not null, and LLVM does not |
| 3982 | return llvm_usize.constInt(alignment, .False).constIntToPtr(llvm_ptr_ty); | 4303 | // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR |
| 3983 | } | 4304 | // instruction is followed by a `wrap_optional`, it will return this value |
| 3984 | // Note that these 0xaa values are appropriate even in release-optimized builds | 4305 | // verbatim, and the result should test as non-null. |
| 3985 | // because we need a well-defined value that is not null, and LLVM does not | 4306 | switch (mod.getTarget().ptrBitWidth()) { |
| 3986 | // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR | 4307 | 16 => 0xaaaa, |
| 3987 | // instruction is followed by a `wrap_optional`, it will return this value | 4308 | 32 => 0xaaaaaaaa, |
| 3988 | // verbatim, and the result should test as non-null. | 4309 | 64 => 0xaaaaaaaa_aaaaaaaa, |
| 3989 | const target = mod.getTarget(); | | |
| 3990 | const int = switch (target.ptrBitWidth()) { | | |
| 3991 | 16 => llvm_usize.constInt(0xaaaa, .False), | | |
| 3992 | 32 => llvm_usize.constInt(0xaaaaaaaa, .False), | | |
| 3993 | 64 => llvm_usize.constInt(0xaaaaaaaa_aaaaaaaa, .False), | | |
| 3994 | else => unreachable, | 4310 | else => unreachable, |
| 3995 | }; | 4311 | }; |
| 3996 | return int.constIntToPtr(llvm_ptr_ty); | 4312 | const llvm_usize = try o.lowerType(Type.usize); |
| | 4313 | const llvm_ptr_ty = try o.lowerType(ptr_ty); |
| | 4314 | return o.builder.castConst(.inttoptr, try o.builder.intConst(llvm_usize, int), llvm_ptr_ty); |
| 3997 | } | 4315 | } |
| 3998 | | 4316 | |
| 3999 | fn addAttr(o: *Object, val: *llvm.Value, index: llvm.AttributeIndex, name: []const u8) void { | 4317 | fn addAttr(o: *Object, val: *llvm.Value, index: llvm.AttributeIndex, name: []const u8) void { |
| ... | @@ -4023,7 +4341,7 @@ pub const Object = struct { | ... | @@ -4023,7 +4341,7 @@ pub const Object = struct { |
| 4023 | ) void { | 4341 | ) void { |
| 4024 | const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len); | 4342 | const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len); |
| 4025 | assert(kind_id != 0); | 4343 | assert(kind_id != 0); |
| 4026 | const llvm_attr = o.context.createEnumAttribute(kind_id, int); | 4344 | const llvm_attr = o.builder.llvm.context.createEnumAttribute(kind_id, int); |
| 4027 | val.addAttributeAtIndex(index, llvm_attr); | 4345 | val.addAttributeAtIndex(index, llvm_attr); |
| 4028 | } | 4346 | } |
| 4029 | | 4347 | |
| ... | @@ -4034,11 +4352,11 @@ pub const Object = struct { | ... | @@ -4034,11 +4352,11 @@ pub const Object = struct { |
| 4034 | name: []const u8, | 4352 | name: []const u8, |
| 4035 | value: []const u8, | 4353 | value: []const u8, |
| 4036 | ) void { | 4354 | ) void { |
| 4037 | const llvm_attr = o.context.createStringAttribute( | 4355 | const llvm_attr = o.builder.llvm.context.createStringAttribute( |
| 4038 | name.ptr, | 4356 | name.ptr, |
| 4039 | @as(c_uint, @intCast(name.len)), | 4357 | @intCast(name.len), |
| 4040 | value.ptr, | 4358 | value.ptr, |
| 4041 | @as(c_uint, @intCast(value.len)), | 4359 | @intCast(value.len), |
| 4042 | ); | 4360 | ); |
| 4043 | val.addAttributeAtIndex(index, llvm_attr); | 4361 | val.addAttributeAtIndex(index, llvm_attr); |
| 4044 | } | 4362 | } |
| ... | @@ -4063,23 +4381,23 @@ pub const Object = struct { | ... | @@ -4063,23 +4381,23 @@ pub const Object = struct { |
| 4063 | /// widen it before using it and then truncate the result. | 4381 | /// widen it before using it and then truncate the result. |
| 4064 | /// RMW exchange of floating-point values is bitcasted to same-sized integer | 4382 | /// RMW exchange of floating-point values is bitcasted to same-sized integer |
| 4065 | /// types to work around a LLVM deficiency when targeting ARM/AArch64. | 4383 | /// types to work around a LLVM deficiency when targeting ARM/AArch64. |
| 4066 | fn getAtomicAbiType(o: *Object, ty: Type, is_rmw_xchg: bool) ?*llvm.Type { | 4384 | fn getAtomicAbiType(o: *Object, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type { |
| 4067 | const mod = o.module; | 4385 | const mod = o.module; |
| 4068 | const int_ty = switch (ty.zigTypeTag(mod)) { | 4386 | const int_ty = switch (ty.zigTypeTag(mod)) { |
| 4069 | .Int => ty, | 4387 | .Int => ty, |
| 4070 | .Enum => ty.intTagType(mod), | 4388 | .Enum => ty.intTagType(mod), |
| 4071 | .Float => { | 4389 | .Float => { |
| 4072 | if (!is_rmw_xchg) return null; | 4390 | if (!is_rmw_xchg) return .none; |
| 4073 | return o.context.intType(@as(c_uint, @intCast(ty.abiSize(mod) * 8))); | 4391 | return o.builder.intType(@intCast(ty.abiSize(mod) * 8)); |
| 4074 | }, | 4392 | }, |
| 4075 | .Bool => return o.context.intType(8), | 4393 | .Bool => return .i8, |
| 4076 | else => return null, | 4394 | else => return .none, |
| 4077 | }; | 4395 | }; |
| 4078 | const bit_count = int_ty.intInfo(mod).bits; | 4396 | const bit_count = int_ty.intInfo(mod).bits; |
| 4079 | if (!std.math.isPowerOfTwo(bit_count) or (bit_count % 8) != 0) { | 4397 | if (!std.math.isPowerOfTwo(bit_count) or (bit_count % 8) != 0) { |
| 4080 | return o.context.intType(@as(c_uint, @intCast(int_ty.abiSize(mod) * 8))); | 4398 | return o.builder.intType(@intCast(int_ty.abiSize(mod) * 8)); |
| 4081 | } else { | 4399 | } else { |
| 4082 | return null; | 4400 | return .none; |
| 4083 | } | 4401 | } |
| 4084 | } | 4402 | } |
| 4085 | | 4403 | |
| ... | @@ -4120,13 +4438,13 @@ pub const Object = struct { | ... | @@ -4120,13 +4438,13 @@ pub const Object = struct { |
| 4120 | llvm_arg_i: u32, | 4438 | llvm_arg_i: u32, |
| 4121 | alignment: u32, | 4439 | alignment: u32, |
| 4122 | byval_attr: bool, | 4440 | byval_attr: bool, |
| 4123 | param_llvm_ty: *llvm.Type, | 4441 | param_llvm_ty: Builder.Type, |
| 4124 | ) void { | 4442 | ) void { |
| 4125 | o.addArgAttr(llvm_fn, llvm_arg_i, "nonnull"); | 4443 | o.addArgAttr(llvm_fn, llvm_arg_i, "nonnull"); |
| 4126 | o.addArgAttr(llvm_fn, llvm_arg_i, "readonly"); | 4444 | o.addArgAttr(llvm_fn, llvm_arg_i, "readonly"); |
| 4127 | o.addArgAttrInt(llvm_fn, llvm_arg_i, "align", alignment); | 4445 | o.addArgAttrInt(llvm_fn, llvm_arg_i, "align", alignment); |
| 4128 | if (byval_attr) { | 4446 | if (byval_attr) { |
| 4129 | llvm_fn.addByValAttr(llvm_arg_i, param_llvm_ty); | 4447 | llvm_fn.addByValAttr(llvm_arg_i, param_llvm_ty.toLlvm(&o.builder)); |
| 4130 | } | 4448 | } |
| 4131 | } | 4449 | } |
| 4132 | }; | 4450 | }; |
| ... | @@ -4159,20 +4477,26 @@ pub const DeclGen = struct { | ... | @@ -4159,20 +4477,26 @@ pub const DeclGen = struct { |
| 4159 | _ = try o.resolveLlvmFunction(extern_func.decl); | 4477 | _ = try o.resolveLlvmFunction(extern_func.decl); |
| 4160 | } else { | 4478 | } else { |
| 4161 | const target = mod.getTarget(); | 4479 | const target = mod.getTarget(); |
| 4162 | var global = try o.resolveGlobalDecl(decl_index); | 4480 | const variable = try o.resolveGlobalDecl(decl_index); |
| 4163 | global.setAlignment(decl.getAlignment(mod)); | 4481 | const global = variable.ptrConst(&o.builder).global; |
| 4164 | if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| global.setSection(s); | 4482 | var llvm_global = global.toLlvm(&o.builder); |
| | 4483 | variable.ptr(&o.builder).alignment = Builder.Alignment.fromByteUnits(decl.getAlignment(mod)); |
| | 4484 | llvm_global.setAlignment(decl.getAlignment(mod)); |
| | 4485 | if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |section| { |
| | 4486 | variable.ptr(&o.builder).section = try o.builder.string(section); |
| | 4487 | llvm_global.setSection(section); |
| | 4488 | } |
| 4165 | assert(decl.has_tv); | 4489 | assert(decl.has_tv); |
| 4166 | const init_val = if (decl.val.getVariable(mod)) |variable| init_val: { | 4490 | const init_val = if (decl.val.getVariable(mod)) |decl_var| decl_var.init else init_val: { |
| 4167 | break :init_val variable.init; | 4491 | variable.ptr(&o.builder).mutability = .constant; |
| 4168 | } else init_val: { | 4492 | llvm_global.setGlobalConstant(.True); |
| 4169 | global.setGlobalConstant(.True); | | |
| 4170 | break :init_val decl.val.toIntern(); | 4493 | break :init_val decl.val.toIntern(); |
| 4171 | }; | 4494 | }; |
| 4172 | if (init_val != .none) { | 4495 | if (init_val != .none) { |
| 4173 | const llvm_init = try o.lowerValue(.{ .ty = decl.ty, .val = init_val.toValue() }); | 4496 | const llvm_init = try o.lowerValue(init_val); |
| 4174 | if (global.globalGetValueType() == llvm_init.typeOf()) { | 4497 | const llvm_init_ty = llvm_init.typeOf(&o.builder); |
| 4175 | global.setInitializer(llvm_init); | 4498 | if (global.ptrConst(&o.builder).type == llvm_init_ty) { |
| | 4499 | llvm_global.setInitializer(llvm_init.toLlvm(&o.builder)); |
| 4176 | } else { | 4500 | } else { |
| 4177 | // LLVM does not allow us to change the type of globals. So we must | 4501 | // LLVM does not allow us to change the type of globals. So we must |
| 4178 | // create a new global with the correct type, copy all its attributes, | 4502 | // create a new global with the correct type, copy all its attributes, |
| ... | @@ -4189,23 +4513,27 @@ pub const DeclGen = struct { | ... | @@ -4189,23 +4513,27 @@ pub const DeclGen = struct { |
| 4189 | // Related: https://github.com/ziglang/zig/issues/13265 | 4513 | // Related: https://github.com/ziglang/zig/issues/13265 |
| 4190 | const llvm_global_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target); | 4514 | const llvm_global_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target); |
| 4191 | const new_global = o.llvm_module.addGlobalInAddressSpace( | 4515 | const new_global = o.llvm_module.addGlobalInAddressSpace( |
| 4192 | llvm_init.typeOf(), | 4516 | llvm_init_ty.toLlvm(&o.builder), |
| 4193 | "", | 4517 | "", |
| 4194 | llvm_global_addrspace, | 4518 | @intFromEnum(llvm_global_addrspace), |
| 4195 | ); | 4519 | ); |
| 4196 | new_global.setLinkage(global.getLinkage()); | 4520 | new_global.setLinkage(llvm_global.getLinkage()); |
| 4197 | new_global.setUnnamedAddr(global.getUnnamedAddress()); | 4521 | new_global.setUnnamedAddr(llvm_global.getUnnamedAddress()); |
| 4198 | new_global.setAlignment(global.getAlignment()); | 4522 | new_global.setAlignment(llvm_global.getAlignment()); |
| 4199 | if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| | 4523 | if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |section| |
| 4200 | new_global.setSection(s); | 4524 | new_global.setSection(section); |
| 4201 | new_global.setInitializer(llvm_init); | 4525 | new_global.setInitializer(llvm_init.toLlvm(&o.builder)); |
| 4202 | // TODO: How should this work then the address space of a global changed? | 4526 | // TODO: How should this work then the address space of a global changed? |
| 4203 | global.replaceAllUsesWith(new_global); | 4527 | llvm_global.replaceAllUsesWith(new_global); |
| 4204 | o.decl_map.putAssumeCapacity(decl_index, new_global); | 4528 | new_global.takeName(llvm_global); |
| 4205 | new_global.takeName(global); | 4529 | o.builder.llvm.globals.items[@intFromEnum(variable.ptrConst(&o.builder).global)] = |
| 4206 | global.deleteGlobal(); | 4530 | new_global; |
| 4207 | global = new_global; | 4531 | llvm_global.deleteGlobal(); |
| | 4532 | llvm_global = new_global; |
| | 4533 | variable.ptr(&o.builder).mutability = .global; |
| | 4534 | global.ptr(&o.builder).type = llvm_init_ty; |
| 4208 | } | 4535 | } |
| | 4536 | variable.ptr(&o.builder).init = llvm_init; |
| 4209 | } | 4537 | } |
| 4210 | | 4538 | |
| 4211 | if (o.di_builder) |dib| { | 4539 | if (o.di_builder) |dib| { |
| ... | @@ -4216,7 +4544,7 @@ pub const DeclGen = struct { | ... | @@ -4216,7 +4544,7 @@ pub const DeclGen = struct { |
| 4216 | const di_global = dib.createGlobalVariableExpression( | 4544 | const di_global = dib.createGlobalVariableExpression( |
| 4217 | di_file.toScope(), | 4545 | di_file.toScope(), |
| 4218 | mod.intern_pool.stringToSlice(decl.name), | 4546 | mod.intern_pool.stringToSlice(decl.name), |
| 4219 | global.getValueName(), | 4547 | llvm_global.getValueName(), |
| 4220 | di_file, | 4548 | di_file, |
| 4221 | line_number, | 4549 | line_number, |
| 4222 | try o.lowerDebugType(decl.ty, .full), | 4550 | try o.lowerDebugType(decl.ty, .full), |
| ... | @@ -4224,7 +4552,7 @@ pub const DeclGen = struct { | ... | @@ -4224,7 +4552,7 @@ pub const DeclGen = struct { |
| 4224 | ); | 4552 | ); |
| 4225 | | 4553 | |
| 4226 | try o.di_map.put(o.gpa, dg.decl, di_global.getVariable().toNode()); | 4554 | try o.di_map.put(o.gpa, dg.decl, di_global.getVariable().toNode()); |
| 4227 | if (!is_internal_linkage or decl.isExtern(mod)) global.attachMetaData(di_global); | 4555 | if (!is_internal_linkage or decl.isExtern(mod)) llvm_global.attachMetaData(di_global); |
| 4228 | } | 4556 | } |
| 4229 | } | 4557 | } |
| 4230 | } | 4558 | } |
| ... | @@ -4235,7 +4563,7 @@ pub const FuncGen = struct { | ... | @@ -4235,7 +4563,7 @@ pub const FuncGen = struct { |
| 4235 | dg: *DeclGen, | 4563 | dg: *DeclGen, |
| 4236 | air: Air, | 4564 | air: Air, |
| 4237 | liveness: Liveness, | 4565 | liveness: Liveness, |
| 4238 | context: *llvm.Context, | 4566 | wip: Builder.WipFunction, |
| 4239 | builder: *llvm.Builder, | 4567 | builder: *llvm.Builder, |
| 4240 | di_scope: ?*llvm.DIScope, | 4568 | di_scope: ?*llvm.DIScope, |
| 4241 | di_file: ?*llvm.DIFile, | 4569 | di_file: ?*llvm.DIFile, |
| ... | @@ -4252,43 +4580,44 @@ pub const FuncGen = struct { | ... | @@ -4252,43 +4580,44 @@ pub const FuncGen = struct { |
| 4252 | | 4580 | |
| 4253 | /// This stores the LLVM values used in a function, such that they can be referred to | 4581 | /// This stores the LLVM values used in a function, such that they can be referred to |
| 4254 | /// in other instructions. This table is cleared before every function is generated. | 4582 | /// in other instructions. This table is cleared before every function is generated. |
| 4255 | func_inst_table: std.AutoHashMapUnmanaged(Air.Inst.Ref, *llvm.Value), | 4583 | func_inst_table: std.AutoHashMapUnmanaged(Air.Inst.Ref, Builder.Value), |
| 4256 | | 4584 | |
| 4257 | /// If the return type is sret, this is the result pointer. Otherwise null. | 4585 | /// If the return type is sret, this is the result pointer. Otherwise null. |
| 4258 | /// Note that this can disagree with isByRef for the return type in the case | 4586 | /// Note that this can disagree with isByRef for the return type in the case |
| 4259 | /// of C ABI functions. | 4587 | /// of C ABI functions. |
| 4260 | ret_ptr: ?*llvm.Value, | 4588 | ret_ptr: Builder.Value, |
| 4261 | /// Any function that needs to perform Valgrind client requests needs an array alloca | 4589 | /// Any function that needs to perform Valgrind client requests needs an array alloca |
| 4262 | /// instruction, however a maximum of one per function is needed. | 4590 | /// instruction, however a maximum of one per function is needed. |
| 4263 | valgrind_client_request_array: ?*llvm.Value = null, | 4591 | valgrind_client_request_array: Builder.Value = .none, |
| 4264 | /// These fields are used to refer to the LLVM value of the function parameters | 4592 | /// These fields are used to refer to the LLVM value of the function parameters |
| 4265 | /// in an Arg instruction. | 4593 | /// in an Arg instruction. |
| 4266 | /// This list may be shorter than the list according to the zig type system; | 4594 | /// This list may be shorter than the list according to the zig type system; |
| 4267 | /// it omits 0-bit types. If the function uses sret as the first parameter, | 4595 | /// it omits 0-bit types. If the function uses sret as the first parameter, |
| 4268 | /// this slice does not include it. | 4596 | /// this slice does not include it. |
| 4269 | args: []const *llvm.Value, | 4597 | args: []const Builder.Value, |
| 4270 | arg_index: c_uint, | 4598 | arg_index: usize, |
| 4271 | | 4599 | |
| 4272 | llvm_func: *llvm.Value, | 4600 | err_ret_trace: Builder.Value = .none, |
| 4273 | | | |
| 4274 | err_ret_trace: ?*llvm.Value = null, | | |
| 4275 | | 4601 | |
| 4276 | /// This data structure is used to implement breaking to blocks. | 4602 | /// This data structure is used to implement breaking to blocks. |
| 4277 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, struct { | 4603 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, struct { |
| 4278 | parent_bb: *llvm.BasicBlock, | 4604 | parent_bb: Builder.Function.Block.Index, |
| 4279 | breaks: *BreakList, | 4605 | breaks: *BreakList, |
| 4280 | }), | 4606 | }), |
| 4281 | | 4607 | |
| 4282 | single_threaded: bool, | 4608 | sync_scope: Builder.SyncScope, |
| 4283 | | 4609 | |
| 4284 | const DbgState = struct { loc: *llvm.DILocation, scope: *llvm.DIScope, base_line: u32 }; | 4610 | const DbgState = struct { loc: *llvm.DILocation, scope: *llvm.DIScope, base_line: u32 }; |
| 4285 | const BreakList = std.MultiArrayList(struct { | 4611 | const BreakList = union { |
| 4286 | bb: *llvm.BasicBlock, | 4612 | list: std.MultiArrayList(struct { |
| 4287 | val: *llvm.Value, | 4613 | bb: Builder.Function.Block.Index, |
| 4288 | }); | 4614 | val: Builder.Value, |
| | 4615 | }), |
| | 4616 | len: usize, |
| | 4617 | }; |
| 4289 | | 4618 | |
| 4290 | fn deinit(self: *FuncGen) void { | 4619 | fn deinit(self: *FuncGen) void { |
| 4291 | self.builder.dispose(); | 4620 | self.wip.deinit(); |
| 4292 | self.dbg_inlined.deinit(self.gpa); | 4621 | self.dbg_inlined.deinit(self.gpa); |
| 4293 | self.dbg_block_stack.deinit(self.gpa); | 4622 | self.dbg_block_stack.deinit(self.gpa); |
| 4294 | self.func_inst_table.deinit(self.gpa); | 4623 | self.func_inst_table.deinit(self.gpa); |
| ... | @@ -4300,7 +4629,7 @@ pub const FuncGen = struct { | ... | @@ -4300,7 +4629,7 @@ pub const FuncGen = struct { |
| 4300 | return self.dg.todo(format, args); | 4629 | return self.dg.todo(format, args); |
| 4301 | } | 4630 | } |
| 4302 | | 4631 | |
| 4303 | fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !*llvm.Value { | 4632 | fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !Builder.Value { |
| 4304 | const gpa = self.gpa; | 4633 | const gpa = self.gpa; |
| 4305 | const gop = try self.func_inst_table.getOrPut(gpa, inst); | 4634 | const gop = try self.func_inst_table.getOrPut(gpa, inst); |
| 4306 | if (gop.found_existing) return gop.value_ptr.*; | 4635 | if (gop.found_existing) return gop.value_ptr.*; |
| ... | @@ -4311,14 +4640,14 @@ pub const FuncGen = struct { | ... | @@ -4311,14 +4640,14 @@ pub const FuncGen = struct { |
| 4311 | .ty = self.typeOf(inst), | 4640 | .ty = self.typeOf(inst), |
| 4312 | .val = (try self.air.value(inst, mod)).?, | 4641 | .val = (try self.air.value(inst, mod)).?, |
| 4313 | }); | 4642 | }); |
| 4314 | gop.value_ptr.* = llvm_val; | 4643 | gop.value_ptr.* = llvm_val.toValue(); |
| 4315 | return llvm_val; | 4644 | return llvm_val.toValue(); |
| 4316 | } | 4645 | } |
| 4317 | | 4646 | |
| 4318 | fn resolveValue(self: *FuncGen, tv: TypedValue) !*llvm.Value { | 4647 | fn resolveValue(self: *FuncGen, tv: TypedValue) Error!Builder.Constant { |
| 4319 | const o = self.dg.object; | 4648 | const o = self.dg.object; |
| 4320 | const mod = o.module; | 4649 | const mod = o.module; |
| 4321 | const llvm_val = try o.lowerValue(tv); | 4650 | const llvm_val = try o.lowerValue(tv.val.toIntern()); |
| 4322 | if (!isByRef(tv.ty, mod)) return llvm_val; | 4651 | if (!isByRef(tv.ty, mod)) return llvm_val; |
| 4323 | | 4652 | |
| 4324 | // We have an LLVM value but we need to create a global constant and | 4653 | // We have an LLVM value but we need to create a global constant and |
| ... | @@ -4326,17 +4655,50 @@ pub const FuncGen = struct { | ... | @@ -4326,17 +4655,50 @@ pub const FuncGen = struct { |
| 4326 | const target = mod.getTarget(); | 4655 | const target = mod.getTarget(); |
| 4327 | const llvm_wanted_addrspace = toLlvmAddressSpace(.generic, target); | 4656 | const llvm_wanted_addrspace = toLlvmAddressSpace(.generic, target); |
| 4328 | const llvm_actual_addrspace = toLlvmGlobalAddressSpace(.generic, target); | 4657 | const llvm_actual_addrspace = toLlvmGlobalAddressSpace(.generic, target); |
| 4329 | const global = o.llvm_module.addGlobalInAddressSpace(llvm_val.typeOf(), "", llvm_actual_addrspace); | 4658 | const llvm_ty = llvm_val.typeOf(&o.builder); |
| 4330 | global.setInitializer(llvm_val); | 4659 | const llvm_alignment = tv.ty.abiAlignment(mod); |
| 4331 | global.setLinkage(.Private); | 4660 | const llvm_global = o.llvm_module.addGlobalInAddressSpace(llvm_ty.toLlvm(&o.builder), "", @intFromEnum(llvm_actual_addrspace)); |
| 4332 | global.setGlobalConstant(.True); | 4661 | llvm_global.setInitializer(llvm_val.toLlvm(&o.builder)); |
| 4333 | global.setUnnamedAddr(.True); | 4662 | llvm_global.setLinkage(.Private); |
| 4334 | global.setAlignment(tv.ty.abiAlignment(mod)); | 4663 | llvm_global.setGlobalConstant(.True); |
| 4335 | const addrspace_casted_ptr = if (llvm_actual_addrspace != llvm_wanted_addrspace) | 4664 | llvm_global.setUnnamedAddr(.True); |
| 4336 | global.constAddrSpaceCast(self.context.pointerType(llvm_wanted_addrspace)) | 4665 | llvm_global.setAlignment(llvm_alignment); |
| 4337 | else | 4666 | |
| 4338 | global; | 4667 | var global = Builder.Global{ |
| 4339 | return addrspace_casted_ptr; | 4668 | .linkage = .private, |
| | 4669 | .unnamed_addr = .unnamed_addr, |
| | 4670 | .addr_space = llvm_actual_addrspace, |
| | 4671 | .type = llvm_ty, |
| | 4672 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| | 4673 | }; |
| | 4674 | var variable = Builder.Variable{ |
| | 4675 | .global = @enumFromInt(o.builder.globals.count()), |
| | 4676 | .mutability = .constant, |
| | 4677 | .init = llvm_val, |
| | 4678 | .alignment = Builder.Alignment.fromByteUnits(llvm_alignment), |
| | 4679 | }; |
| | 4680 | try o.builder.llvm.globals.append(o.gpa, llvm_global); |
| | 4681 | const global_index = try o.builder.addGlobal(.empty, global); |
| | 4682 | try o.builder.variables.append(o.gpa, variable); |
| | 4683 | |
| | 4684 | return o.builder.convConst( |
| | 4685 | .unneeded, |
| | 4686 | global_index.toConst(), |
| | 4687 | try o.builder.ptrType(llvm_wanted_addrspace), |
| | 4688 | ); |
| | 4689 | } |
| | 4690 | |
| | 4691 | fn resolveNullOptUsize(self: *FuncGen) Error!Builder.Constant { |
| | 4692 | const o = self.dg.object; |
| | 4693 | const mod = o.module; |
| | 4694 | if (o.null_opt_usize == .no_init) { |
| | 4695 | const ty = try mod.intern(.{ .opt_type = .usize_type }); |
| | 4696 | o.null_opt_usize = try self.resolveValue(.{ |
| | 4697 | .ty = ty.toType(), |
| | 4698 | .val = (try mod.intern(.{ .opt = .{ .ty = ty, .val = .none } })).toValue(), |
| | 4699 | }); |
| | 4700 | } |
| | 4701 | return o.null_opt_usize; |
| 4340 | } | 4702 | } |
| 4341 | | 4703 | |
| 4342 | fn genBody(self: *FuncGen, body: []const Air.Inst.Index) Error!void { | 4704 | fn genBody(self: *FuncGen, body: []const Air.Inst.Index) Error!void { |
| ... | @@ -4345,10 +4707,9 @@ pub const FuncGen = struct { | ... | @@ -4345,10 +4707,9 @@ pub const FuncGen = struct { |
| 4345 | const ip = &mod.intern_pool; | 4707 | const ip = &mod.intern_pool; |
| 4346 | const air_tags = self.air.instructions.items(.tag); | 4708 | const air_tags = self.air.instructions.items(.tag); |
| 4347 | for (body, 0..) |inst, i| { | 4709 | for (body, 0..) |inst, i| { |
| 4348 | if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) | 4710 | if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) continue; |
| 4349 | continue; | | |
| 4350 | | 4711 | |
| 4351 | const opt_value: ?*llvm.Value = switch (air_tags[inst]) { | 4712 | const val: Builder.Value = switch (air_tags[inst]) { |
| 4352 | // zig fmt: off | 4713 | // zig fmt: off |
| 4353 | .add => try self.airAdd(inst, false), | 4714 | .add => try self.airAdd(inst, false), |
| 4354 | .add_optimized => try self.airAdd(inst, true), | 4715 | .add_optimized => try self.airAdd(inst, true), |
| ... | @@ -4439,15 +4800,15 @@ pub const FuncGen = struct { | ... | @@ -4439,15 +4800,15 @@ pub const FuncGen = struct { |
| 4439 | .cmp_vector_optimized => try self.airCmpVector(inst, true), | 4800 | .cmp_vector_optimized => try self.airCmpVector(inst, true), |
| 4440 | .cmp_lt_errors_len => try self.airCmpLtErrorsLen(inst), | 4801 | .cmp_lt_errors_len => try self.airCmpLtErrorsLen(inst), |
| 4441 | | 4802 | |
| 4442 | .is_non_null => try self.airIsNonNull(inst, false, .NE), | 4803 | .is_non_null => try self.airIsNonNull(inst, false, .ne), |
| 4443 | .is_non_null_ptr => try self.airIsNonNull(inst, true , .NE), | 4804 | .is_non_null_ptr => try self.airIsNonNull(inst, true , .ne), |
| 4444 | .is_null => try self.airIsNonNull(inst, false, .EQ), | 4805 | .is_null => try self.airIsNonNull(inst, false, .eq), |
| 4445 | .is_null_ptr => try self.airIsNonNull(inst, true , .EQ), | 4806 | .is_null_ptr => try self.airIsNonNull(inst, true , .eq), |
| 4446 | | 4807 | |
| 4447 | .is_non_err => try self.airIsErr(inst, .EQ, false), | 4808 | .is_non_err => try self.airIsErr(inst, .eq, false), |
| 4448 | .is_non_err_ptr => try self.airIsErr(inst, .EQ, true), | 4809 | .is_non_err_ptr => try self.airIsErr(inst, .eq, true), |
| 4449 | .is_err => try self.airIsErr(inst, .NE, false), | 4810 | .is_err => try self.airIsErr(inst, .ne, false), |
| 4450 | .is_err_ptr => try self.airIsErr(inst, .NE, true), | 4811 | .is_err_ptr => try self.airIsErr(inst, .ne, true), |
| 4451 | | 4812 | |
| 4452 | .alloc => try self.airAlloc(inst), | 4813 | .alloc => try self.airAlloc(inst), |
| 4453 | .ret_ptr => try self.airRetPtr(inst), | 4814 | .ret_ptr => try self.airRetPtr(inst), |
| ... | @@ -4524,10 +4885,10 @@ pub const FuncGen = struct { | ... | @@ -4524,10 +4885,10 @@ pub const FuncGen = struct { |
| 4524 | .reduce => try self.airReduce(inst, false), | 4885 | .reduce => try self.airReduce(inst, false), |
| 4525 | .reduce_optimized => try self.airReduce(inst, true), | 4886 | .reduce_optimized => try self.airReduce(inst, true), |
| 4526 | | 4887 | |
| 4527 | .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered), | 4888 | .atomic_store_unordered => try self.airAtomicStore(inst, .unordered), |
| 4528 | .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic), | 4889 | .atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic), |
| 4529 | .atomic_store_release => try self.airAtomicStore(inst, .Release), | 4890 | .atomic_store_release => try self.airAtomicStore(inst, .release), |
| 4530 | .atomic_store_seq_cst => try self.airAtomicStore(inst, .SequentiallyConsistent), | 4891 | .atomic_store_seq_cst => try self.airAtomicStore(inst, .seq_cst), |
| 4531 | | 4892 | |
| 4532 | .struct_field_ptr => try self.airStructFieldPtr(inst), | 4893 | .struct_field_ptr => try self.airStructFieldPtr(inst), |
| 4533 | .struct_field_val => try self.airStructFieldVal(body[i..]), | 4894 | .struct_field_val => try self.airStructFieldVal(body[i..]), |
| ... | @@ -4569,8 +4930,8 @@ pub const FuncGen = struct { | ... | @@ -4569,8 +4930,8 @@ pub const FuncGen = struct { |
| 4569 | | 4930 | |
| 4570 | .inferred_alloc, .inferred_alloc_comptime => unreachable, | 4931 | .inferred_alloc, .inferred_alloc_comptime => unreachable, |
| 4571 | | 4932 | |
| 4572 | .unreach => self.airUnreach(inst), | 4933 | .unreach => try self.airUnreach(inst), |
| 4573 | .dbg_stmt => self.airDbgStmt(inst), | 4934 | .dbg_stmt => try self.airDbgStmt(inst), |
| 4574 | .dbg_inline_begin => try self.airDbgInlineBegin(inst), | 4935 | .dbg_inline_begin => try self.airDbgInlineBegin(inst), |
| 4575 | .dbg_inline_end => try self.airDbgInlineEnd(inst), | 4936 | .dbg_inline_end => try self.airDbgInlineEnd(inst), |
| 4576 | .dbg_block_begin => try self.airDbgBlockBegin(), | 4937 | .dbg_block_begin => try self.airDbgBlockBegin(), |
| ... | @@ -4588,17 +4949,14 @@ pub const FuncGen = struct { | ... | @@ -4588,17 +4949,14 @@ pub const FuncGen = struct { |
| 4588 | .work_group_id => try self.airWorkGroupId(inst), | 4949 | .work_group_id => try self.airWorkGroupId(inst), |
| 4589 | // zig fmt: on | 4950 | // zig fmt: on |
| 4590 | }; | 4951 | }; |
| 4591 | if (opt_value) |val| { | 4952 | if (val != .none) try self.func_inst_table.putNoClobber(self.gpa, Air.indexToRef(inst), val); |
| 4592 | const ref = Air.indexToRef(inst); | | |
| 4593 | try self.func_inst_table.putNoClobber(self.gpa, ref, val); | | |
| 4594 | } | | |
| 4595 | } | 4953 | } |
| 4596 | } | 4954 | } |
| 4597 | | 4955 | |
| 4598 | fn airCall(self: *FuncGen, inst: Air.Inst.Index, attr: llvm.CallAttr) !?*llvm.Value { | 4956 | fn airCall(self: *FuncGen, inst: Air.Inst.Index, attr: llvm.CallAttr) !Builder.Value { |
| 4599 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4957 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 4600 | const extra = self.air.extraData(Air.Call, pl_op.payload); | 4958 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 4601 | const args = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len])); | 4959 | const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| 4602 | const o = self.dg.object; | 4960 | const o = self.dg.object; |
| 4603 | const mod = o.module; | 4961 | const mod = o.module; |
| 4604 | const ip = &mod.intern_pool; | 4962 | const ip = &mod.intern_pool; |
| ... | @@ -4619,19 +4977,21 @@ pub const FuncGen = struct { | ... | @@ -4619,19 +4977,21 @@ pub const FuncGen = struct { |
| 4619 | | 4977 | |
| 4620 | const ret_ptr = if (!sret) null else blk: { | 4978 | const ret_ptr = if (!sret) null else blk: { |
| 4621 | const llvm_ret_ty = try o.lowerType(return_type); | 4979 | const llvm_ret_ty = try o.lowerType(return_type); |
| 4622 | const ret_ptr = self.buildAlloca(llvm_ret_ty, return_type.abiAlignment(mod)); | 4980 | const alignment = Builder.Alignment.fromByteUnits(return_type.abiAlignment(mod)); |
| 4623 | try llvm_args.append(ret_ptr); | 4981 | const ret_ptr = try self.buildAlloca(llvm_ret_ty, alignment); |
| | 4982 | try llvm_args.append(ret_ptr.toLlvm(&self.wip)); |
| 4624 | break :blk ret_ptr; | 4983 | break :blk ret_ptr; |
| 4625 | }; | 4984 | }; |
| 4626 | | 4985 | |
| 4627 | const err_return_tracing = return_type.isError(mod) and | 4986 | const err_return_tracing = return_type.isError(mod) and |
| 4628 | o.module.comp.bin_file.options.error_return_tracing; | 4987 | o.module.comp.bin_file.options.error_return_tracing; |
| 4629 | if (err_return_tracing) { | 4988 | if (err_return_tracing) { |
| 4630 | try llvm_args.append(self.err_ret_trace.?); | 4989 | assert(self.err_ret_trace != .none); |
| | 4990 | try llvm_args.append(self.err_ret_trace.toLlvm(&self.wip)); |
| 4631 | } | 4991 | } |
| 4632 | | 4992 | |
| 4633 | var it = iterateParamTypes(o, fn_info); | 4993 | var it = iterateParamTypes(o, fn_info); |
| 4634 | while (it.nextCall(self, args)) |lowering| switch (lowering) { | 4994 | while (try it.nextCall(self, args)) |lowering| switch (lowering) { |
| 4635 | .no_bits => continue, | 4995 | .no_bits => continue, |
| 4636 | .byval => { | 4996 | .byval => { |
| 4637 | const arg = args[it.zig_index - 1]; | 4997 | const arg = args[it.zig_index - 1]; |
| ... | @@ -4639,12 +4999,11 @@ pub const FuncGen = struct { | ... | @@ -4639,12 +4999,11 @@ pub const FuncGen = struct { |
| 4639 | const llvm_arg = try self.resolveInst(arg); | 4999 | const llvm_arg = try self.resolveInst(arg); |
| 4640 | const llvm_param_ty = try o.lowerType(param_ty); | 5000 | const llvm_param_ty = try o.lowerType(param_ty); |
| 4641 | if (isByRef(param_ty, mod)) { | 5001 | if (isByRef(param_ty, mod)) { |
| 4642 | const alignment = param_ty.abiAlignment(mod); | 5002 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 4643 | const load_inst = self.builder.buildLoad(llvm_param_ty, llvm_arg, ""); | 5003 | const loaded = try self.wip.load(.normal, llvm_param_ty, llvm_arg, alignment, ""); |
| 4644 | load_inst.setAlignment(alignment); | 5004 | try llvm_args.append(loaded.toLlvm(&self.wip)); |
| 4645 | try llvm_args.append(load_inst); | | |
| 4646 | } else { | 5005 | } else { |
| 4647 | try llvm_args.append(llvm_arg); | 5006 | try llvm_args.append(llvm_arg.toLlvm(&self.wip)); |
| 4648 | } | 5007 | } |
| 4649 | }, | 5008 | }, |
| 4650 | .byref => { | 5009 | .byref => { |
| ... | @@ -4652,14 +5011,13 @@ pub const FuncGen = struct { | ... | @@ -4652,14 +5011,13 @@ pub const FuncGen = struct { |
| 4652 | const param_ty = self.typeOf(arg); | 5011 | const param_ty = self.typeOf(arg); |
| 4653 | const llvm_arg = try self.resolveInst(arg); | 5012 | const llvm_arg = try self.resolveInst(arg); |
| 4654 | if (isByRef(param_ty, mod)) { | 5013 | if (isByRef(param_ty, mod)) { |
| 4655 | try llvm_args.append(llvm_arg); | 5014 | try llvm_args.append(llvm_arg.toLlvm(&self.wip)); |
| 4656 | } else { | 5015 | } else { |
| 4657 | const alignment = param_ty.abiAlignment(mod); | 5016 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 4658 | const param_llvm_ty = llvm_arg.typeOf(); | 5017 | const param_llvm_ty = llvm_arg.typeOfWip(&self.wip); |
| 4659 | const arg_ptr = self.buildAlloca(param_llvm_ty, alignment); | 5018 | const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment); |
| 4660 | const store_inst = self.builder.buildStore(llvm_arg, arg_ptr); | 5019 | _ = try self.wip.store(.normal, llvm_arg, arg_ptr, alignment); |
| 4661 | store_inst.setAlignment(alignment); | 5020 | try llvm_args.append(arg_ptr.toLlvm(&self.wip)); |
| 4662 | try llvm_args.append(arg_ptr); | | |
| 4663 | } | 5021 | } |
| 4664 | }, | 5022 | }, |
| 4665 | .byref_mut => { | 5023 | .byref_mut => { |
| ... | @@ -4667,134 +5025,124 @@ pub const FuncGen = struct { | ... | @@ -4667,134 +5025,124 @@ pub const FuncGen = struct { |
| 4667 | const param_ty = self.typeOf(arg); | 5025 | const param_ty = self.typeOf(arg); |
| 4668 | const llvm_arg = try self.resolveInst(arg); | 5026 | const llvm_arg = try self.resolveInst(arg); |
| 4669 | | 5027 | |
| 4670 | const alignment = param_ty.abiAlignment(mod); | 5028 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 4671 | const param_llvm_ty = try o.lowerType(param_ty); | 5029 | const param_llvm_ty = try o.lowerType(param_ty); |
| 4672 | const arg_ptr = self.buildAlloca(param_llvm_ty, alignment); | 5030 | const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment); |
| 4673 | if (isByRef(param_ty, mod)) { | 5031 | if (isByRef(param_ty, mod)) { |
| 4674 | const load_inst = self.builder.buildLoad(param_llvm_ty, llvm_arg, ""); | 5032 | const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, ""); |
| 4675 | load_inst.setAlignment(alignment); | 5033 | _ = try self.wip.store(.normal, loaded, arg_ptr, alignment); |
| 4676 | | | |
| 4677 | const store_inst = self.builder.buildStore(load_inst, arg_ptr); | | |
| 4678 | store_inst.setAlignment(alignment); | | |
| 4679 | try llvm_args.append(arg_ptr); | | |
| 4680 | } else { | 5034 | } else { |
| 4681 | const store_inst = self.builder.buildStore(llvm_arg, arg_ptr); | 5035 | _ = try self.wip.store(.normal, llvm_arg, arg_ptr, alignment); |
| 4682 | store_inst.setAlignment(alignment); | | |
| 4683 | try llvm_args.append(arg_ptr); | | |
| 4684 | } | 5036 | } |
| | 5037 | try llvm_args.append(arg_ptr.toLlvm(&self.wip)); |
| 4685 | }, | 5038 | }, |
| 4686 | .abi_sized_int => { | 5039 | .abi_sized_int => { |
| 4687 | const arg = args[it.zig_index - 1]; | 5040 | const arg = args[it.zig_index - 1]; |
| 4688 | const param_ty = self.typeOf(arg); | 5041 | const param_ty = self.typeOf(arg); |
| 4689 | const llvm_arg = try self.resolveInst(arg); | 5042 | const llvm_arg = try self.resolveInst(arg); |
| 4690 | const abi_size = @as(c_uint, @intCast(param_ty.abiSize(mod))); | 5043 | const int_llvm_ty = try o.builder.intType(@intCast(param_ty.abiSize(mod) * 8)); |
| 4691 | const int_llvm_ty = self.context.intType(abi_size * 8); | | |
| 4692 | | 5044 | |
| 4693 | if (isByRef(param_ty, mod)) { | 5045 | if (isByRef(param_ty, mod)) { |
| 4694 | const alignment = param_ty.abiAlignment(mod); | 5046 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 4695 | const load_inst = self.builder.buildLoad(int_llvm_ty, llvm_arg, ""); | 5047 | const loaded = try self.wip.load(.normal, int_llvm_ty, llvm_arg, alignment, ""); |
| 4696 | load_inst.setAlignment(alignment); | 5048 | try llvm_args.append(loaded.toLlvm(&self.wip)); |
| 4697 | try llvm_args.append(load_inst); | | |
| 4698 | } else { | 5049 | } else { |
| 4699 | // LLVM does not allow bitcasting structs so we must allocate | 5050 | // LLVM does not allow bitcasting structs so we must allocate |
| 4700 | // a local, store as one type, and then load as another type. | 5051 | // a local, store as one type, and then load as another type. |
| 4701 | const alignment = @max( | 5052 | const alignment = Builder.Alignment.fromByteUnits(@max( |
| 4702 | param_ty.abiAlignment(mod), | 5053 | param_ty.abiAlignment(mod), |
| 4703 | o.target_data.abiAlignmentOfType(int_llvm_ty), | 5054 | o.target_data.abiAlignmentOfType(int_llvm_ty.toLlvm(&o.builder)), |
| 4704 | ); | 5055 | )); |
| 4705 | const int_ptr = self.buildAlloca(int_llvm_ty, alignment); | 5056 | const int_ptr = try self.buildAlloca(int_llvm_ty, alignment); |
| 4706 | const store_inst = self.builder.buildStore(llvm_arg, int_ptr); | 5057 | _ = try self.wip.store(.normal, llvm_arg, int_ptr, alignment); |
| 4707 | store_inst.setAlignment(alignment); | 5058 | const loaded = try self.wip.load(.normal, int_llvm_ty, int_ptr, alignment, ""); |
| 4708 | const load_inst = self.builder.buildLoad(int_llvm_ty, int_ptr, ""); | 5059 | try llvm_args.append(loaded.toLlvm(&self.wip)); |
| 4709 | load_inst.setAlignment(alignment); | | |
| 4710 | try llvm_args.append(load_inst); | | |
| 4711 | } | 5060 | } |
| 4712 | }, | 5061 | }, |
| 4713 | .slice => { | 5062 | .slice => { |
| 4714 | const arg = args[it.zig_index - 1]; | 5063 | const arg = args[it.zig_index - 1]; |
| 4715 | const llvm_arg = try self.resolveInst(arg); | 5064 | const llvm_arg = try self.resolveInst(arg); |
| 4716 | const ptr = self.builder.buildExtractValue(llvm_arg, 0, ""); | 5065 | const ptr = try self.wip.extractValue(llvm_arg, &.{0}, ""); |
| 4717 | const len = self.builder.buildExtractValue(llvm_arg, 1, ""); | 5066 | const len = try self.wip.extractValue(llvm_arg, &.{1}, ""); |
| 4718 | try llvm_args.ensureUnusedCapacity(2); | 5067 | try llvm_args.appendSlice(&.{ ptr.toLlvm(&self.wip), len.toLlvm(&self.wip) }); |
| 4719 | llvm_args.appendAssumeCapacity(ptr); | | |
| 4720 | llvm_args.appendAssumeCapacity(len); | | |
| 4721 | }, | 5068 | }, |
| 4722 | .multiple_llvm_types => { | 5069 | .multiple_llvm_types => { |
| 4723 | const arg = args[it.zig_index - 1]; | 5070 | const arg = args[it.zig_index - 1]; |
| 4724 | const param_ty = self.typeOf(arg); | 5071 | const param_ty = self.typeOf(arg); |
| 4725 | const llvm_types = it.llvm_types_buffer[0..it.llvm_types_len]; | 5072 | const llvm_types = it.types_buffer[0..it.types_len]; |
| 4726 | const llvm_arg = try self.resolveInst(arg); | 5073 | const llvm_arg = try self.resolveInst(arg); |
| 4727 | const is_by_ref = isByRef(param_ty, mod); | 5074 | const is_by_ref = isByRef(param_ty, mod); |
| 4728 | const arg_ptr = if (is_by_ref) llvm_arg else p: { | 5075 | const arg_ptr = if (is_by_ref) llvm_arg else ptr: { |
| 4729 | const p = self.buildAlloca(llvm_arg.typeOf(), null); | 5076 | const alignment = Builder.Alignment.fromByteUnits(param_ty.abiAlignment(mod)); |
| 4730 | const store_inst = self.builder.buildStore(llvm_arg, p); | 5077 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 4731 | store_inst.setAlignment(param_ty.abiAlignment(mod)); | 5078 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 4732 | break :p p; | 5079 | break :ptr ptr; |
| 4733 | }; | 5080 | }; |
| 4734 | | 5081 | |
| 4735 | const llvm_ty = self.context.structType(llvm_types.ptr, @as(c_uint, @intCast(llvm_types.len)), .False); | 5082 | const llvm_ty = try o.builder.structType(.normal, llvm_types); |
| 4736 | try llvm_args.ensureUnusedCapacity(it.llvm_types_len); | 5083 | try llvm_args.ensureUnusedCapacity(it.types_len); |
| 4737 | for (llvm_types, 0..) |field_ty, i_usize| { | 5084 | for (llvm_types, 0..) |field_ty, i| { |
| 4738 | const i = @as(c_uint, @intCast(i_usize)); | 5085 | const alignment = |
| 4739 | const field_ptr = self.builder.buildStructGEP(llvm_ty, arg_ptr, i, ""); | 5086 | Builder.Alignment.fromByteUnits(@divExact(target.ptrBitWidth(), 8)); |
| 4740 | const load_inst = self.builder.buildLoad(field_ty, field_ptr, ""); | 5087 | const field_ptr = try self.wip.gepStruct(llvm_ty, arg_ptr, i, ""); |
| 4741 | load_inst.setAlignment(target.ptrBitWidth() / 8); | 5088 | const loaded = try self.wip.load(.normal, field_ty, field_ptr, alignment, ""); |
| 4742 | llvm_args.appendAssumeCapacity(load_inst); | 5089 | llvm_args.appendAssumeCapacity(loaded.toLlvm(&self.wip)); |
| 4743 | } | 5090 | } |
| 4744 | }, | 5091 | }, |
| 4745 | .as_u16 => { | 5092 | .as_u16 => { |
| 4746 | const arg = args[it.zig_index - 1]; | 5093 | const arg = args[it.zig_index - 1]; |
| 4747 | const llvm_arg = try self.resolveInst(arg); | 5094 | const llvm_arg = try self.resolveInst(arg); |
| 4748 | const casted = self.builder.buildBitCast(llvm_arg, self.context.intType(16), ""); | 5095 | const casted = try self.wip.cast(.bitcast, llvm_arg, .i16, ""); |
| 4749 | try llvm_args.append(casted); | 5096 | try llvm_args.append(casted.toLlvm(&self.wip)); |
| 4750 | }, | 5097 | }, |
| 4751 | .float_array => |count| { | 5098 | .float_array => |count| { |
| 4752 | const arg = args[it.zig_index - 1]; | 5099 | const arg = args[it.zig_index - 1]; |
| 4753 | const arg_ty = self.typeOf(arg); | 5100 | const arg_ty = self.typeOf(arg); |
| 4754 | var llvm_arg = try self.resolveInst(arg); | 5101 | var llvm_arg = try self.resolveInst(arg); |
| | 5102 | const alignment = Builder.Alignment.fromByteUnits(arg_ty.abiAlignment(mod)); |
| 4755 | if (!isByRef(arg_ty, mod)) { | 5103 | if (!isByRef(arg_ty, mod)) { |
| 4756 | const p = self.buildAlloca(llvm_arg.typeOf(), null); | 5104 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 4757 | const store_inst = self.builder.buildStore(llvm_arg, p); | 5105 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 4758 | store_inst.setAlignment(arg_ty.abiAlignment(mod)); | 5106 | llvm_arg = ptr; |
| 4759 | llvm_arg = store_inst; | | |
| 4760 | } | 5107 | } |
| 4761 | | 5108 | |
| 4762 | const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(arg_ty, mod).?); | 5109 | const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(arg_ty, mod).?); |
| 4763 | const array_llvm_ty = float_ty.arrayType(count); | 5110 | const array_ty = try o.builder.arrayType(count, float_ty); |
| 4764 | | 5111 | |
| 4765 | const alignment = arg_ty.abiAlignment(mod); | 5112 | const loaded = try self.wip.load(.normal, array_ty, llvm_arg, alignment, ""); |
| 4766 | const load_inst = self.builder.buildLoad(array_llvm_ty, llvm_arg, ""); | 5113 | try llvm_args.append(loaded.toLlvm(&self.wip)); |
| 4767 | load_inst.setAlignment(alignment); | | |
| 4768 | try llvm_args.append(load_inst); | | |
| 4769 | }, | 5114 | }, |
| 4770 | .i32_array, .i64_array => |arr_len| { | 5115 | .i32_array, .i64_array => |arr_len| { |
| 4771 | const elem_size: u8 = if (lowering == .i32_array) 32 else 64; | 5116 | const elem_size: u8 = if (lowering == .i32_array) 32 else 64; |
| 4772 | const arg = args[it.zig_index - 1]; | 5117 | const arg = args[it.zig_index - 1]; |
| 4773 | const arg_ty = self.typeOf(arg); | 5118 | const arg_ty = self.typeOf(arg); |
| 4774 | var llvm_arg = try self.resolveInst(arg); | 5119 | var llvm_arg = try self.resolveInst(arg); |
| | 5120 | const alignment = Builder.Alignment.fromByteUnits(arg_ty.abiAlignment(mod)); |
| 4775 | if (!isByRef(arg_ty, mod)) { | 5121 | if (!isByRef(arg_ty, mod)) { |
| 4776 | const p = self.buildAlloca(llvm_arg.typeOf(), null); | 5122 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 4777 | const store_inst = self.builder.buildStore(llvm_arg, p); | 5123 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 4778 | store_inst.setAlignment(arg_ty.abiAlignment(mod)); | 5124 | llvm_arg = ptr; |
| 4779 | llvm_arg = store_inst; | | |
| 4780 | } | 5125 | } |
| 4781 | | 5126 | |
| 4782 | const array_llvm_ty = self.context.intType(elem_size).arrayType(arr_len); | 5127 | const array_ty = |
| 4783 | const alignment = arg_ty.abiAlignment(mod); | 5128 | try o.builder.arrayType(arr_len, try o.builder.intType(@intCast(elem_size))); |
| 4784 | const load_inst = self.builder.buildLoad(array_llvm_ty, llvm_arg, ""); | 5129 | const loaded = try self.wip.load(.normal, array_ty, llvm_arg, alignment, ""); |
| 4785 | load_inst.setAlignment(alignment); | 5130 | try llvm_args.append(loaded.toLlvm(&self.wip)); |
| 4786 | try llvm_args.append(load_inst); | | |
| 4787 | }, | 5131 | }, |
| 4788 | }; | 5132 | }; |
| 4789 | | 5133 | |
| 4790 | const call = self.builder.buildCall( | 5134 | const llvm_fn_ty = try o.lowerType(zig_fn_ty); |
| 4791 | try o.lowerType(zig_fn_ty), | 5135 | const call = (try self.wip.unimplemented(llvm_fn_ty.functionReturn(&o.builder), "")).finish( |
| 4792 | llvm_fn, | 5136 | self.builder.buildCall( |
| 4793 | llvm_args.items.ptr, | 5137 | llvm_fn_ty.toLlvm(&o.builder), |
| 4794 | @as(c_uint, @intCast(llvm_args.items.len)), | 5138 | llvm_fn.toLlvm(&self.wip), |
| 4795 | toLlvmCallConv(fn_info.cc, target), | 5139 | llvm_args.items.ptr, |
| 4796 | attr, | 5140 | @intCast(llvm_args.items.len), |
| 4797 | "", | 5141 | toLlvmCallConv(fn_info.cc, target), |
| | 5142 | attr, |
| | 5143 | "", |
| | 5144 | ), |
| | 5145 | &self.wip, |
| 4798 | ); | 5146 | ); |
| 4799 | | 5147 | |
| 4800 | if (callee_ty.zigTypeTag(mod) == .Pointer) { | 5148 | if (callee_ty.zigTypeTag(mod) == .Pointer) { |
| ... | @@ -4802,12 +5150,12 @@ pub const FuncGen = struct { | ... | @@ -4802,12 +5150,12 @@ pub const FuncGen = struct { |
| 4802 | it = iterateParamTypes(o, fn_info); | 5150 | it = iterateParamTypes(o, fn_info); |
| 4803 | it.llvm_index += @intFromBool(sret); | 5151 | it.llvm_index += @intFromBool(sret); |
| 4804 | it.llvm_index += @intFromBool(err_return_tracing); | 5152 | it.llvm_index += @intFromBool(err_return_tracing); |
| 4805 | while (it.next()) |lowering| switch (lowering) { | 5153 | while (try it.next()) |lowering| switch (lowering) { |
| 4806 | .byval => { | 5154 | .byval => { |
| 4807 | const param_index = it.zig_index - 1; | 5155 | const param_index = it.zig_index - 1; |
| 4808 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); | 5156 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); |
| 4809 | if (!isByRef(param_ty, mod)) { | 5157 | if (!isByRef(param_ty, mod)) { |
| 4810 | o.addByValParamAttrs(call, param_ty, param_index, fn_info, it.llvm_index - 1); | 5158 | o.addByValParamAttrs(call.toLlvm(&self.wip), param_ty, param_index, fn_info, it.llvm_index - 1); |
| 4811 | } | 5159 | } |
| 4812 | }, | 5160 | }, |
| 4813 | .byref => { | 5161 | .byref => { |
| ... | @@ -4815,10 +5163,10 @@ pub const FuncGen = struct { | ... | @@ -4815,10 +5163,10 @@ pub const FuncGen = struct { |
| 4815 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); | 5163 | const param_ty = fn_info.param_types.get(ip)[param_index].toType(); |
| 4816 | const param_llvm_ty = try o.lowerType(param_ty); | 5164 | const param_llvm_ty = try o.lowerType(param_ty); |
| 4817 | const alignment = param_ty.abiAlignment(mod); | 5165 | const alignment = param_ty.abiAlignment(mod); |
| 4818 | o.addByRefParamAttrs(call, it.llvm_index - 1, alignment, it.byval_attr, param_llvm_ty); | 5166 | o.addByRefParamAttrs(call.toLlvm(&self.wip), it.llvm_index - 1, alignment, it.byval_attr, param_llvm_ty); |
| 4819 | }, | 5167 | }, |
| 4820 | .byref_mut => { | 5168 | .byref_mut => { |
| 4821 | o.addArgAttr(call, it.llvm_index - 1, "noundef"); | 5169 | o.addArgAttr(call.toLlvm(&self.wip), it.llvm_index - 1, "noundef"); |
| 4822 | }, | 5170 | }, |
| 4823 | // No attributes needed for these. | 5171 | // No attributes needed for these. |
| 4824 | .no_bits, | 5172 | .no_bits, |
| ... | @@ -4838,41 +5186,40 @@ pub const FuncGen = struct { | ... | @@ -4838,41 +5186,40 @@ pub const FuncGen = struct { |
| 4838 | | 5186 | |
| 4839 | if (math.cast(u5, it.zig_index - 1)) |i| { | 5187 | if (math.cast(u5, it.zig_index - 1)) |i| { |
| 4840 | if (@as(u1, @truncate(fn_info.noalias_bits >> i)) != 0) { | 5188 | if (@as(u1, @truncate(fn_info.noalias_bits >> i)) != 0) { |
| 4841 | o.addArgAttr(call, llvm_arg_i, "noalias"); | 5189 | o.addArgAttr(call.toLlvm(&self.wip), llvm_arg_i, "noalias"); |
| 4842 | } | 5190 | } |
| 4843 | } | 5191 | } |
| 4844 | if (param_ty.zigTypeTag(mod) != .Optional) { | 5192 | if (param_ty.zigTypeTag(mod) != .Optional) { |
| 4845 | o.addArgAttr(call, llvm_arg_i, "nonnull"); | 5193 | o.addArgAttr(call.toLlvm(&self.wip), llvm_arg_i, "nonnull"); |
| 4846 | } | 5194 | } |
| 4847 | if (ptr_info.flags.is_const) { | 5195 | if (ptr_info.flags.is_const) { |
| 4848 | o.addArgAttr(call, llvm_arg_i, "readonly"); | 5196 | o.addArgAttr(call.toLlvm(&self.wip), llvm_arg_i, "readonly"); |
| 4849 | } | 5197 | } |
| 4850 | const elem_align = ptr_info.flags.alignment.toByteUnitsOptional() orelse | 5198 | const elem_align = ptr_info.flags.alignment.toByteUnitsOptional() orelse |
| 4851 | @max(ptr_info.child.toType().abiAlignment(mod), 1); | 5199 | @max(ptr_info.child.toType().abiAlignment(mod), 1); |
| 4852 | o.addArgAttrInt(call, llvm_arg_i, "align", elem_align); | 5200 | o.addArgAttrInt(call.toLlvm(&self.wip), llvm_arg_i, "align", elem_align); |
| 4853 | }, | 5201 | }, |
| 4854 | }; | 5202 | }; |
| 4855 | } | 5203 | } |
| 4856 | | 5204 | |
| 4857 | if (fn_info.return_type == .noreturn_type and attr != .AlwaysTail) { | 5205 | if (fn_info.return_type == .noreturn_type and attr != .AlwaysTail) { |
| 4858 | return null; | 5206 | return .none; |
| 4859 | } | 5207 | } |
| 4860 | | 5208 | |
| 4861 | if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBitsIgnoreComptime(mod)) { | 5209 | if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBitsIgnoreComptime(mod)) { |
| 4862 | return null; | 5210 | return .none; |
| 4863 | } | 5211 | } |
| 4864 | | 5212 | |
| 4865 | const llvm_ret_ty = try o.lowerType(return_type); | 5213 | const llvm_ret_ty = try o.lowerType(return_type); |
| 4866 | | 5214 | |
| 4867 | if (ret_ptr) |rp| { | 5215 | if (ret_ptr) |rp| { |
| 4868 | call.setCallSret(llvm_ret_ty); | 5216 | call.toLlvm(&self.wip).setCallSret(llvm_ret_ty.toLlvm(&o.builder)); |
| 4869 | if (isByRef(return_type, mod)) { | 5217 | if (isByRef(return_type, mod)) { |
| 4870 | return rp; | 5218 | return rp; |
| 4871 | } else { | 5219 | } else { |
| 4872 | // our by-ref status disagrees with sret so we must load. | 5220 | // our by-ref status disagrees with sret so we must load. |
| 4873 | const loaded = self.builder.buildLoad(llvm_ret_ty, rp, ""); | 5221 | const return_alignment = Builder.Alignment.fromByteUnits(return_type.abiAlignment(mod)); |
| 4874 | loaded.setAlignment(return_type.abiAlignment(mod)); | 5222 | return self.wip.load(.normal, llvm_ret_ty, rp, return_alignment, ""); |
| 4875 | return loaded; | | |
| 4876 | } | 5223 | } |
| 4877 | } | 5224 | } |
| 4878 | | 5225 | |
| ... | @@ -4882,26 +5229,23 @@ pub const FuncGen = struct { | ... | @@ -4882,26 +5229,23 @@ pub const FuncGen = struct { |
| 4882 | // In this case the function return type is honoring the calling convention by having | 5229 | // In this case the function return type is honoring the calling convention by having |
| 4883 | // a different LLVM type than the usual one. We solve this here at the callsite | 5230 | // a different LLVM type than the usual one. We solve this here at the callsite |
| 4884 | // by using our canonical type, then loading it if necessary. | 5231 | // by using our canonical type, then loading it if necessary. |
| 4885 | const alignment = o.target_data.abiAlignmentOfType(abi_ret_ty); | 5232 | const alignment = Builder.Alignment.fromByteUnits( |
| 4886 | const rp = self.buildAlloca(llvm_ret_ty, alignment); | 5233 | o.target_data.abiAlignmentOfType(abi_ret_ty.toLlvm(&o.builder)), |
| 4887 | const store_inst = self.builder.buildStore(call, rp); | 5234 | ); |
| 4888 | store_inst.setAlignment(alignment); | 5235 | const rp = try self.buildAlloca(llvm_ret_ty, alignment); |
| 4889 | if (isByRef(return_type, mod)) { | 5236 | _ = try self.wip.store(.normal, call, rp, alignment); |
| 4890 | return rp; | 5237 | return if (isByRef(return_type, mod)) |
| 4891 | } else { | 5238 | rp |
| 4892 | const load_inst = self.builder.buildLoad(llvm_ret_ty, rp, ""); | 5239 | else |
| 4893 | load_inst.setAlignment(alignment); | 5240 | try self.wip.load(.normal, llvm_ret_ty, rp, alignment, ""); |
| 4894 | return load_inst; | | |
| 4895 | } | | |
| 4896 | } | 5241 | } |
| 4897 | | 5242 | |
| 4898 | if (isByRef(return_type, mod)) { | 5243 | if (isByRef(return_type, mod)) { |
| 4899 | // our by-ref status disagrees with sret so we must allocate, store, | 5244 | // our by-ref status disagrees with sret so we must allocate, store, |
| 4900 | // and return the allocation pointer. | 5245 | // and return the allocation pointer. |
| 4901 | const alignment = return_type.abiAlignment(mod); | 5246 | const alignment = Builder.Alignment.fromByteUnits(return_type.abiAlignment(mod)); |
| 4902 | const rp = self.buildAlloca(llvm_ret_ty, alignment); | 5247 | const rp = try self.buildAlloca(llvm_ret_ty, alignment); |
| 4903 | const store_inst = self.builder.buildStore(call, rp); | 5248 | _ = try self.wip.store(.normal, call, rp, alignment); |
| 4904 | store_inst.setAlignment(alignment); | | |
| 4905 | return rp; | 5249 | return rp; |
| 4906 | } else { | 5250 | } else { |
| 4907 | return call; | 5251 | return call; |
| ... | @@ -4914,13 +5258,10 @@ pub const FuncGen = struct { | ... | @@ -4914,13 +5258,10 @@ pub const FuncGen = struct { |
| 4914 | const msg_decl_index = mod.panic_messages[@intFromEnum(panic_id)].unwrap().?; | 5258 | const msg_decl_index = mod.panic_messages[@intFromEnum(panic_id)].unwrap().?; |
| 4915 | const msg_decl = mod.declPtr(msg_decl_index); | 5259 | const msg_decl = mod.declPtr(msg_decl_index); |
| 4916 | const msg_len = msg_decl.ty.childType(mod).arrayLen(mod); | 5260 | const msg_len = msg_decl.ty.childType(mod).arrayLen(mod); |
| 4917 | const msg_ptr = try o.lowerValue(.{ | 5261 | const msg_ptr = try o.lowerValue(try msg_decl.internValue(mod)); |
| 4918 | .ty = msg_decl.ty, | 5262 | const null_opt_addr_global = try fg.resolveNullOptUsize(); |
| 4919 | .val = msg_decl.val, | | |
| 4920 | }); | | |
| 4921 | const null_opt_addr_global = try o.getNullOptAddr(); | | |
| 4922 | const target = mod.getTarget(); | 5263 | const target = mod.getTarget(); |
| 4923 | const llvm_usize = fg.context.intType(target.ptrBitWidth()); | 5264 | const llvm_usize = try o.lowerType(Type.usize); |
| 4924 | // example: | 5265 | // example: |
| 4925 | // call fastcc void @test2.panic( | 5266 | // call fastcc void @test2.panic( |
| 4926 | // ptr @builtin.panic_messages.integer_overflow__anon_987, ; msg.ptr | 5267 | // ptr @builtin.panic_messages.integer_overflow__anon_987, ; msg.ptr |
| ... | @@ -4929,38 +5270,38 @@ pub const FuncGen = struct { | ... | @@ -4929,38 +5270,38 @@ pub const FuncGen = struct { |
| 4929 | // ptr @2, ; addr (null ?usize) | 5270 | // ptr @2, ; addr (null ?usize) |
| 4930 | // ) | 5271 | // ) |
| 4931 | const args = [4]*llvm.Value{ | 5272 | const args = [4]*llvm.Value{ |
| 4932 | msg_ptr, | 5273 | msg_ptr.toLlvm(&o.builder), |
| 4933 | llvm_usize.constInt(msg_len, .False), | 5274 | (try o.builder.intConst(llvm_usize, msg_len)).toLlvm(&o.builder), |
| 4934 | fg.context.pointerType(0).constNull(), | 5275 | (try o.builder.nullConst(.ptr)).toLlvm(&o.builder), |
| 4935 | null_opt_addr_global, | 5276 | null_opt_addr_global.toLlvm(&o.builder), |
| 4936 | }; | 5277 | }; |
| 4937 | const panic_func = mod.funcInfo(mod.panic_func_index); | 5278 | const panic_func = mod.funcInfo(mod.panic_func_index); |
| 4938 | const panic_decl = mod.declPtr(panic_func.owner_decl); | 5279 | const panic_decl = mod.declPtr(panic_func.owner_decl); |
| 4939 | const fn_info = mod.typeToFunc(panic_decl.ty).?; | 5280 | const fn_info = mod.typeToFunc(panic_decl.ty).?; |
| 4940 | const panic_global = try o.resolveLlvmFunction(panic_func.owner_decl); | 5281 | const panic_global = try o.resolveLlvmFunction(panic_func.owner_decl); |
| 4941 | _ = fg.builder.buildCall( | 5282 | _ = (try fg.wip.unimplemented(.void, "")).finish(fg.builder.buildCall( |
| 4942 | try o.lowerType(panic_decl.ty), | 5283 | (try o.lowerType(panic_decl.ty)).toLlvm(&o.builder), |
| 4943 | panic_global, | 5284 | panic_global.toLlvm(&o.builder), |
| 4944 | &args, | 5285 | &args, |
| 4945 | args.len, | 5286 | args.len, |
| 4946 | toLlvmCallConv(fn_info.cc, target), | 5287 | toLlvmCallConv(fn_info.cc, target), |
| 4947 | .Auto, | 5288 | .Auto, |
| 4948 | "", | 5289 | "", |
| 4949 | ); | 5290 | ), &fg.wip); |
| 4950 | _ = fg.builder.buildUnreachable(); | 5291 | _ = try fg.wip.@"unreachable"(); |
| 4951 | } | 5292 | } |
| 4952 | | 5293 | |
| 4953 | fn airRet(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5294 | fn airRet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 4954 | const o = self.dg.object; | 5295 | const o = self.dg.object; |
| 4955 | const mod = o.module; | 5296 | const mod = o.module; |
| 4956 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 5297 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 4957 | const ret_ty = self.typeOf(un_op); | 5298 | const ret_ty = self.typeOf(un_op); |
| 4958 | if (self.ret_ptr) |ret_ptr| { | 5299 | if (self.ret_ptr != .none) { |
| 4959 | const operand = try self.resolveInst(un_op); | 5300 | const operand = try self.resolveInst(un_op); |
| 4960 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | 5301 | const ptr_ty = try mod.singleMutPtrType(ret_ty); |
| 4961 | try self.store(ret_ptr, ptr_ty, operand, .NotAtomic); | 5302 | try self.store(self.ret_ptr, ptr_ty, operand, .none); |
| 4962 | _ = self.builder.buildRetVoid(); | 5303 | _ = try self.wip.retVoid(); |
| 4963 | return null; | 5304 | return .none; |
| 4964 | } | 5305 | } |
| 4965 | const fn_info = mod.typeToFunc(self.dg.decl.ty).?; | 5306 | const fn_info = mod.typeToFunc(self.dg.decl.ty).?; |
| 4966 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 5307 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| ... | @@ -4968,43 +5309,37 @@ pub const FuncGen = struct { | ... | @@ -4968,43 +5309,37 @@ pub const FuncGen = struct { |
| 4968 | // Functions with an empty error set are emitted with an error code | 5309 | // Functions with an empty error set are emitted with an error code |
| 4969 | // return type and return zero so they can be function pointers coerced | 5310 | // return type and return zero so they can be function pointers coerced |
| 4970 | // to functions that return anyerror. | 5311 | // to functions that return anyerror. |
| 4971 | const err_int = try o.lowerType(Type.anyerror); | 5312 | _ = try self.wip.ret(try o.builder.intValue(Builder.Type.err_int, 0)); |
| 4972 | _ = self.builder.buildRet(err_int.constInt(0, .False)); | | |
| 4973 | } else { | 5313 | } else { |
| 4974 | _ = self.builder.buildRetVoid(); | 5314 | _ = try self.wip.retVoid(); |
| 4975 | } | 5315 | } |
| 4976 | return null; | 5316 | return .none; |
| 4977 | } | 5317 | } |
| 4978 | | 5318 | |
| 4979 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); | 5319 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); |
| 4980 | const operand = try self.resolveInst(un_op); | 5320 | const operand = try self.resolveInst(un_op); |
| 4981 | const alignment = ret_ty.abiAlignment(mod); | 5321 | const alignment = Builder.Alignment.fromByteUnits(ret_ty.abiAlignment(mod)); |
| 4982 | | 5322 | |
| 4983 | if (isByRef(ret_ty, mod)) { | 5323 | if (isByRef(ret_ty, mod)) { |
| 4984 | // operand is a pointer however self.ret_ptr is null so that means | 5324 | // operand is a pointer however self.ret_ptr is null so that means |
| 4985 | // we need to return a value. | 5325 | // we need to return a value. |
| 4986 | const load_inst = self.builder.buildLoad(abi_ret_ty, operand, ""); | 5326 | _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, operand, alignment, "")); |
| 4987 | load_inst.setAlignment(alignment); | 5327 | return .none; |
| 4988 | _ = self.builder.buildRet(load_inst); | | |
| 4989 | return null; | | |
| 4990 | } | 5328 | } |
| 4991 | | 5329 | |
| 4992 | const llvm_ret_ty = operand.typeOf(); | 5330 | const llvm_ret_ty = operand.typeOfWip(&self.wip); |
| 4993 | if (abi_ret_ty == llvm_ret_ty) { | 5331 | if (abi_ret_ty == llvm_ret_ty) { |
| 4994 | _ = self.builder.buildRet(operand); | 5332 | _ = try self.wip.ret(operand); |
| 4995 | return null; | 5333 | return .none; |
| 4996 | } | 5334 | } |
| 4997 | | 5335 | |
| 4998 | const rp = self.buildAlloca(llvm_ret_ty, alignment); | 5336 | const rp = try self.buildAlloca(llvm_ret_ty, alignment); |
| 4999 | const store_inst = self.builder.buildStore(operand, rp); | 5337 | _ = try self.wip.store(.normal, operand, rp, alignment); |
| 5000 | store_inst.setAlignment(alignment); | 5338 | _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, rp, alignment, "")); |
| 5001 | const load_inst = self.builder.buildLoad(abi_ret_ty, rp, ""); | 5339 | return .none; |
| 5002 | load_inst.setAlignment(alignment); | | |
| 5003 | _ = self.builder.buildRet(load_inst); | | |
| 5004 | return null; | | |
| 5005 | } | 5340 | } |
| 5006 | | 5341 | |
| 5007 | fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5342 | fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5008 | const o = self.dg.object; | 5343 | const o = self.dg.object; |
| 5009 | const mod = o.module; | 5344 | const mod = o.module; |
| 5010 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 5345 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| ... | @@ -5016,36 +5351,34 @@ pub const FuncGen = struct { | ... | @@ -5016,36 +5351,34 @@ pub const FuncGen = struct { |
| 5016 | // Functions with an empty error set are emitted with an error code | 5351 | // Functions with an empty error set are emitted with an error code |
| 5017 | // return type and return zero so they can be function pointers coerced | 5352 | // return type and return zero so they can be function pointers coerced |
| 5018 | // to functions that return anyerror. | 5353 | // to functions that return anyerror. |
| 5019 | const err_int = try o.lowerType(Type.anyerror); | 5354 | _ = try self.wip.ret(try o.builder.intValue(Builder.Type.err_int, 0)); |
| 5020 | _ = self.builder.buildRet(err_int.constInt(0, .False)); | | |
| 5021 | } else { | 5355 | } else { |
| 5022 | _ = self.builder.buildRetVoid(); | 5356 | _ = try self.wip.retVoid(); |
| 5023 | } | 5357 | } |
| 5024 | return null; | 5358 | return .none; |
| 5025 | } | 5359 | } |
| 5026 | if (self.ret_ptr != null) { | 5360 | if (self.ret_ptr != .none) { |
| 5027 | _ = self.builder.buildRetVoid(); | 5361 | _ = try self.wip.retVoid(); |
| 5028 | return null; | 5362 | return .none; |
| 5029 | } | 5363 | } |
| 5030 | const ptr = try self.resolveInst(un_op); | 5364 | const ptr = try self.resolveInst(un_op); |
| 5031 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); | 5365 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); |
| 5032 | const loaded = self.builder.buildLoad(abi_ret_ty, ptr, ""); | 5366 | const alignment = Builder.Alignment.fromByteUnits(ret_ty.abiAlignment(mod)); |
| 5033 | loaded.setAlignment(ret_ty.abiAlignment(mod)); | 5367 | _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, ptr, alignment, "")); |
| 5034 | _ = self.builder.buildRet(loaded); | 5368 | return .none; |
| 5035 | return null; | | |
| 5036 | } | 5369 | } |
| 5037 | | 5370 | |
| 5038 | fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5371 | fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5039 | const o = self.dg.object; | 5372 | const o = self.dg.object; |
| 5040 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5373 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5041 | const list = try self.resolveInst(ty_op.operand); | 5374 | const list = try self.resolveInst(ty_op.operand); |
| 5042 | const arg_ty = self.air.getRefType(ty_op.ty); | 5375 | const arg_ty = self.air.getRefType(ty_op.ty); |
| 5043 | const llvm_arg_ty = try o.lowerType(arg_ty); | 5376 | const llvm_arg_ty = try o.lowerType(arg_ty); |
| 5044 | | 5377 | |
| 5045 | return self.builder.buildVAArg(list, llvm_arg_ty, ""); | 5378 | return self.wip.vaArg(list, llvm_arg_ty, ""); |
| 5046 | } | 5379 | } |
| 5047 | | 5380 | |
| 5048 | fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5381 | fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5049 | const o = self.dg.object; | 5382 | const o = self.dg.object; |
| 5050 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5383 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5051 | const src_list = try self.resolveInst(ty_op.operand); | 5384 | const src_list = try self.resolveInst(ty_op.operand); |
| ... | @@ -5053,75 +5386,86 @@ pub const FuncGen = struct { | ... | @@ -5053,75 +5386,86 @@ pub const FuncGen = struct { |
| 5053 | const llvm_va_list_ty = try o.lowerType(va_list_ty); | 5386 | const llvm_va_list_ty = try o.lowerType(va_list_ty); |
| 5054 | const mod = o.module; | 5387 | const mod = o.module; |
| 5055 | | 5388 | |
| 5056 | const result_alignment = va_list_ty.abiAlignment(mod); | 5389 | const result_alignment = Builder.Alignment.fromByteUnits(va_list_ty.abiAlignment(mod)); |
| 5057 | const dest_list = self.buildAlloca(llvm_va_list_ty, result_alignment); | 5390 | const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment); |
| 5058 | | 5391 | |
| 5059 | const llvm_fn_name = "llvm.va_copy"; | 5392 | const llvm_fn_name = "llvm.va_copy"; |
| 5060 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { | 5393 | const llvm_fn_ty = try o.builder.fnType(.void, &.{ .ptr, .ptr }, .normal); |
| 5061 | const param_types = [_]*llvm.Type{ | 5394 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse |
| 5062 | self.context.pointerType(0), | 5395 | o.llvm_module.addFunction(llvm_fn_name, llvm_fn_ty.toLlvm(&o.builder)); |
| 5063 | self.context.pointerType(0), | | |
| 5064 | }; | | |
| 5065 | const fn_type = llvm.functionType(self.context.voidType(), &param_types, param_types.len, .False); | | |
| 5066 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type); | | |
| 5067 | }; | | |
| 5068 | | 5396 | |
| 5069 | const args: [2]*llvm.Value = .{ dest_list, src_list }; | 5397 | const args: [2]*llvm.Value = .{ dest_list.toLlvm(&self.wip), src_list.toLlvm(&self.wip) }; |
| 5070 | _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 5398 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| | 5399 | llvm_fn_ty.toLlvm(&o.builder), |
| | 5400 | llvm_fn, |
| | 5401 | &args, |
| | 5402 | args.len, |
| | 5403 | .Fast, |
| | 5404 | .Auto, |
| | 5405 | "", |
| | 5406 | ), &self.wip); |
| 5071 | | 5407 | |
| 5072 | if (isByRef(va_list_ty, mod)) { | 5408 | return if (isByRef(va_list_ty, mod)) |
| 5073 | return dest_list; | 5409 | dest_list |
| 5074 | } else { | 5410 | else |
| 5075 | const loaded = self.builder.buildLoad(llvm_va_list_ty, dest_list, ""); | 5411 | try self.wip.load(.normal, llvm_va_list_ty, dest_list, result_alignment, ""); |
| 5076 | loaded.setAlignment(result_alignment); | | |
| 5077 | return loaded; | | |
| 5078 | } | | |
| 5079 | } | 5412 | } |
| 5080 | | 5413 | |
| 5081 | fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5414 | fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5082 | const o = self.dg.object; | 5415 | const o = self.dg.object; |
| 5083 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 5416 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 5084 | const list = try self.resolveInst(un_op); | 5417 | const list = try self.resolveInst(un_op); |
| 5085 | | 5418 | |
| 5086 | const llvm_fn_name = "llvm.va_end"; | 5419 | const llvm_fn_name = "llvm.va_end"; |
| 5087 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { | 5420 | const llvm_fn_ty = try o.builder.fnType(.void, &.{.ptr}, .normal); |
| 5088 | const param_types = [_]*llvm.Type{self.context.pointerType(0)}; | 5421 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse |
| 5089 | const fn_type = llvm.functionType(self.context.voidType(), &param_types, param_types.len, .False); | 5422 | o.llvm_module.addFunction(llvm_fn_name, llvm_fn_ty.toLlvm(&o.builder)); |
| 5090 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type); | 5423 | |
| 5091 | }; | 5424 | const args: [1]*llvm.Value = .{list.toLlvm(&self.wip)}; |
| 5092 | const args: [1]*llvm.Value = .{list}; | 5425 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| 5093 | _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 5426 | llvm_fn_ty.toLlvm(&o.builder), |
| 5094 | return null; | 5427 | llvm_fn, |
| | 5428 | &args, |
| | 5429 | args.len, |
| | 5430 | .Fast, |
| | 5431 | .Auto, |
| | 5432 | "", |
| | 5433 | ), &self.wip); |
| | 5434 | return .none; |
| 5095 | } | 5435 | } |
| 5096 | | 5436 | |
| 5097 | fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5437 | fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5098 | const o = self.dg.object; | 5438 | const o = self.dg.object; |
| 5099 | const mod = o.module; | 5439 | const mod = o.module; |
| 5100 | const va_list_ty = self.typeOfIndex(inst); | 5440 | const va_list_ty = self.typeOfIndex(inst); |
| 5101 | const llvm_va_list_ty = try o.lowerType(va_list_ty); | 5441 | const llvm_va_list_ty = try o.lowerType(va_list_ty); |
| 5102 | | 5442 | |
| 5103 | const result_alignment = va_list_ty.abiAlignment(mod); | 5443 | const result_alignment = Builder.Alignment.fromByteUnits(va_list_ty.abiAlignment(mod)); |
| 5104 | const list = self.buildAlloca(llvm_va_list_ty, result_alignment); | 5444 | const list = try self.buildAlloca(llvm_va_list_ty, result_alignment); |
| 5105 | | 5445 | |
| 5106 | const llvm_fn_name = "llvm.va_start"; | 5446 | const llvm_fn_name = "llvm.va_start"; |
| 5107 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { | 5447 | const llvm_fn_ty = try o.builder.fnType(.void, &.{.ptr}, .normal); |
| 5108 | const param_types = [_]*llvm.Type{self.context.pointerType(0)}; | 5448 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse |
| 5109 | const fn_type = llvm.functionType(self.context.voidType(), &param_types, param_types.len, .False); | 5449 | o.llvm_module.addFunction(llvm_fn_name, llvm_fn_ty.toLlvm(&o.builder)); |
| 5110 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type); | | |
| 5111 | }; | | |
| 5112 | const args: [1]*llvm.Value = .{list}; | | |
| 5113 | _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | | |
| 5114 | | 5450 | |
| 5115 | if (isByRef(va_list_ty, mod)) { | 5451 | const args: [1]*llvm.Value = .{list.toLlvm(&self.wip)}; |
| 5116 | return list; | 5452 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| 5117 | } else { | 5453 | llvm_fn_ty.toLlvm(&o.builder), |
| 5118 | const loaded = self.builder.buildLoad(llvm_va_list_ty, list, ""); | 5454 | llvm_fn, |
| 5119 | loaded.setAlignment(result_alignment); | 5455 | &args, |
| 5120 | return loaded; | 5456 | args.len, |
| 5121 | } | 5457 | .Fast, |
| | 5458 | .Auto, |
| | 5459 | "", |
| | 5460 | ), &self.wip); |
| | 5461 | |
| | 5462 | return if (isByRef(va_list_ty, mod)) |
| | 5463 | list |
| | 5464 | else |
| | 5465 | try self.wip.load(.normal, llvm_va_list_ty, list, result_alignment, ""); |
| 5122 | } | 5466 | } |
| 5123 | | 5467 | |
| 5124 | fn airCmp(self: *FuncGen, inst: Air.Inst.Index, op: math.CompareOperator, want_fast_math: bool) !?*llvm.Value { | 5468 | fn airCmp(self: *FuncGen, inst: Air.Inst.Index, op: math.CompareOperator, want_fast_math: bool) !Builder.Value { |
| 5125 | self.builder.setFastMath(want_fast_math); | 5469 | self.builder.setFastMath(want_fast_math); |
| 5126 | | 5470 | |
| 5127 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 5471 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -5132,7 +5476,7 @@ pub const FuncGen = struct { | ... | @@ -5132,7 +5476,7 @@ pub const FuncGen = struct { |
| 5132 | return self.cmp(lhs, rhs, operand_ty, op); | 5476 | return self.cmp(lhs, rhs, operand_ty, op); |
| 5133 | } | 5477 | } |
| 5134 | | 5478 | |
| 5135 | fn airCmpVector(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 5479 | fn airCmpVector(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 5136 | self.builder.setFastMath(want_fast_math); | 5480 | self.builder.setFastMath(want_fast_math); |
| 5137 | | 5481 | |
| 5138 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 5482 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -5146,21 +5490,30 @@ pub const FuncGen = struct { | ... | @@ -5146,21 +5490,30 @@ pub const FuncGen = struct { |
| 5146 | return self.cmp(lhs, rhs, vec_ty, cmp_op); | 5490 | return self.cmp(lhs, rhs, vec_ty, cmp_op); |
| 5147 | } | 5491 | } |
| 5148 | | 5492 | |
| 5149 | fn airCmpLtErrorsLen(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5493 | fn airCmpLtErrorsLen(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| | 5494 | const o = self.dg.object; |
| 5150 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 5495 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 5151 | const operand = try self.resolveInst(un_op); | 5496 | const operand = try self.resolveInst(un_op); |
| 5152 | const llvm_fn = try self.getCmpLtErrorsLenFunction(); | 5497 | const llvm_fn = try self.getCmpLtErrorsLenFunction(); |
| 5153 | const args: [1]*llvm.Value = .{operand}; | 5498 | const args: [1]*llvm.Value = .{operand.toLlvm(&self.wip)}; |
| 5154 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 5499 | return (try self.wip.unimplemented(.i1, "")).finish(self.builder.buildCall( |
| | 5500 | llvm_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 5501 | llvm_fn.toLlvm(&o.builder), |
| | 5502 | &args, |
| | 5503 | args.len, |
| | 5504 | .Fast, |
| | 5505 | .Auto, |
| | 5506 | "", |
| | 5507 | ), &self.wip); |
| 5155 | } | 5508 | } |
| 5156 | | 5509 | |
| 5157 | fn cmp( | 5510 | fn cmp( |
| 5158 | self: *FuncGen, | 5511 | self: *FuncGen, |
| 5159 | lhs: *llvm.Value, | 5512 | lhs: Builder.Value, |
| 5160 | rhs: *llvm.Value, | 5513 | rhs: Builder.Value, |
| 5161 | operand_ty: Type, | 5514 | operand_ty: Type, |
| 5162 | op: math.CompareOperator, | 5515 | op: math.CompareOperator, |
| 5163 | ) Allocator.Error!*llvm.Value { | 5516 | ) Allocator.Error!Builder.Value { |
| 5164 | const o = self.dg.object; | 5517 | const o = self.dg.object; |
| 5165 | const mod = o.module; | 5518 | const mod = o.module; |
| 5166 | const scalar_ty = operand_ty.scalarType(mod); | 5519 | const scalar_ty = operand_ty.scalarType(mod); |
| ... | @@ -5178,46 +5531,47 @@ pub const FuncGen = struct { | ... | @@ -5178,46 +5531,47 @@ pub const FuncGen = struct { |
| 5178 | // of optionals that are not pointers. | 5531 | // of optionals that are not pointers. |
| 5179 | const is_by_ref = isByRef(scalar_ty, mod); | 5532 | const is_by_ref = isByRef(scalar_ty, mod); |
| 5180 | const opt_llvm_ty = try o.lowerType(scalar_ty); | 5533 | const opt_llvm_ty = try o.lowerType(scalar_ty); |
| 5181 | const lhs_non_null = self.optIsNonNull(opt_llvm_ty, lhs, is_by_ref); | 5534 | const lhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, lhs, is_by_ref); |
| 5182 | const rhs_non_null = self.optIsNonNull(opt_llvm_ty, rhs, is_by_ref); | 5535 | const rhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, rhs, is_by_ref); |
| 5183 | const llvm_i2 = self.context.intType(2); | 5536 | const llvm_i2 = try o.builder.intType(2); |
| 5184 | const lhs_non_null_i2 = self.builder.buildZExt(lhs_non_null, llvm_i2, ""); | 5537 | const lhs_non_null_i2 = try self.wip.cast(.zext, lhs_non_null, llvm_i2, ""); |
| 5185 | const rhs_non_null_i2 = self.builder.buildZExt(rhs_non_null, llvm_i2, ""); | 5538 | const rhs_non_null_i2 = try self.wip.cast(.zext, rhs_non_null, llvm_i2, ""); |
| 5186 | const lhs_shifted = self.builder.buildShl(lhs_non_null_i2, llvm_i2.constInt(1, .False), ""); | 5539 | const lhs_shifted = try self.wip.bin(.shl, lhs_non_null_i2, try o.builder.intValue(llvm_i2, 1), ""); |
| 5187 | const lhs_rhs_ored = self.builder.buildOr(lhs_shifted, rhs_non_null_i2, ""); | 5540 | const lhs_rhs_ored = try self.wip.bin(.@"or", lhs_shifted, rhs_non_null_i2, ""); |
| 5188 | const both_null_block = self.context.appendBasicBlock(self.llvm_func, "BothNull"); | 5541 | const both_null_block = try self.wip.block(1, "BothNull"); |
| 5189 | const mixed_block = self.context.appendBasicBlock(self.llvm_func, "Mixed"); | 5542 | const mixed_block = try self.wip.block(1, "Mixed"); |
| 5190 | const both_pl_block = self.context.appendBasicBlock(self.llvm_func, "BothNonNull"); | 5543 | const both_pl_block = try self.wip.block(1, "BothNonNull"); |
| 5191 | const end_block = self.context.appendBasicBlock(self.llvm_func, "End"); | 5544 | const end_block = try self.wip.block(3, "End"); |
| 5192 | const llvm_switch = self.builder.buildSwitch(lhs_rhs_ored, mixed_block, 2); | 5545 | var wip_switch = try self.wip.@"switch"(lhs_rhs_ored, mixed_block, 2); |
| 5193 | const llvm_i2_00 = llvm_i2.constInt(0b00, .False); | 5546 | defer wip_switch.finish(&self.wip); |
| 5194 | const llvm_i2_11 = llvm_i2.constInt(0b11, .False); | 5547 | try wip_switch.addCase( |
| 5195 | llvm_switch.addCase(llvm_i2_00, both_null_block); | 5548 | try o.builder.intConst(llvm_i2, 0b00), |
| 5196 | llvm_switch.addCase(llvm_i2_11, both_pl_block); | 5549 | both_null_block, |
| 5197 | | 5550 | &self.wip, |
| 5198 | self.builder.positionBuilderAtEnd(both_null_block); | 5551 | ); |
| 5199 | _ = self.builder.buildBr(end_block); | 5552 | try wip_switch.addCase( |
| 5200 | | 5553 | try o.builder.intConst(llvm_i2, 0b11), |
| 5201 | self.builder.positionBuilderAtEnd(mixed_block); | 5554 | both_pl_block, |
| 5202 | _ = self.builder.buildBr(end_block); | 5555 | &self.wip, |
| 5203 | | 5556 | ); |
| 5204 | self.builder.positionBuilderAtEnd(both_pl_block); | 5557 | |
| | 5558 | self.wip.cursor = .{ .block = both_null_block }; |
| | 5559 | _ = try self.wip.br(end_block); |
| | 5560 | |
| | 5561 | self.wip.cursor = .{ .block = mixed_block }; |
| | 5562 | _ = try self.wip.br(end_block); |
| | 5563 | |
| | 5564 | self.wip.cursor = .{ .block = both_pl_block }; |
| 5205 | const lhs_payload = try self.optPayloadHandle(opt_llvm_ty, lhs, scalar_ty, true); | 5565 | const lhs_payload = try self.optPayloadHandle(opt_llvm_ty, lhs, scalar_ty, true); |
| 5206 | const rhs_payload = try self.optPayloadHandle(opt_llvm_ty, rhs, scalar_ty, true); | 5566 | const rhs_payload = try self.optPayloadHandle(opt_llvm_ty, rhs, scalar_ty, true); |
| 5207 | const payload_cmp = try self.cmp(lhs_payload, rhs_payload, payload_ty, op); | 5567 | const payload_cmp = try self.cmp(lhs_payload, rhs_payload, payload_ty, op); |
| 5208 | _ = self.builder.buildBr(end_block); | 5568 | _ = try self.wip.br(end_block); |
| 5209 | const both_pl_block_end = self.builder.getInsertBlock(); | 5569 | const both_pl_block_end = self.wip.cursor.block; |
| 5210 | | 5570 | |
| 5211 | self.builder.positionBuilderAtEnd(end_block); | 5571 | self.wip.cursor = .{ .block = end_block }; |
| 5212 | const incoming_blocks: [3]*llvm.BasicBlock = .{ | 5572 | const llvm_i1_0 = try o.builder.intValue(.i1, 0); |
| 5213 | both_null_block, | 5573 | const llvm_i1_1 = try o.builder.intValue(.i1, 1); |
| 5214 | mixed_block, | 5574 | const incoming_values: [3]Builder.Value = .{ |
| 5215 | both_pl_block_end, | | |
| 5216 | }; | | |
| 5217 | const llvm_i1 = self.context.intType(1); | | |
| 5218 | const llvm_i1_0 = llvm_i1.constInt(0, .False); | | |
| 5219 | const llvm_i1_1 = llvm_i1.constInt(1, .False); | | |
| 5220 | const incoming_values: [3]*llvm.Value = .{ | | |
| 5221 | switch (op) { | 5575 | switch (op) { |
| 5222 | .eq => llvm_i1_1, | 5576 | .eq => llvm_i1_1, |
| 5223 | .neq => llvm_i1_0, | 5577 | .neq => llvm_i1_0, |
| ... | @@ -5231,47 +5585,48 @@ pub const FuncGen = struct { | ... | @@ -5231,47 +5585,48 @@ pub const FuncGen = struct { |
| 5231 | payload_cmp, | 5585 | payload_cmp, |
| 5232 | }; | 5586 | }; |
| 5233 | | 5587 | |
| 5234 | const phi_node = self.builder.buildPhi(llvm_i1, ""); | 5588 | const phi = try self.wip.phi(.i1, ""); |
| 5235 | comptime assert(incoming_values.len == incoming_blocks.len); | 5589 | try phi.finish( |
| 5236 | phi_node.addIncoming( | | |
| 5237 | &incoming_values, | 5590 | &incoming_values, |
| 5238 | &incoming_blocks, | 5591 | &.{ both_null_block, mixed_block, both_pl_block_end }, |
| 5239 | incoming_values.len, | 5592 | &self.wip, |
| 5240 | ); | 5593 | ); |
| 5241 | return phi_node; | 5594 | return phi.toValue(); |
| 5242 | }, | 5595 | }, |
| 5243 | .Float => return self.buildFloatCmp(op, operand_ty, .{ lhs, rhs }), | 5596 | .Float => return self.buildFloatCmp(op, operand_ty, .{ lhs, rhs }), |
| 5244 | else => unreachable, | 5597 | else => unreachable, |
| 5245 | }; | 5598 | }; |
| 5246 | const is_signed = int_ty.isSignedInt(mod); | 5599 | const is_signed = int_ty.isSignedInt(mod); |
| 5247 | const operation: llvm.IntPredicate = switch (op) { | 5600 | const cond: Builder.IntegerCondition = switch (op) { |
| 5248 | .eq => .EQ, | 5601 | .eq => .eq, |
| 5249 | .neq => .NE, | 5602 | .neq => .ne, |
| 5250 | .lt => if (is_signed) llvm.IntPredicate.SLT else .ULT, | 5603 | .lt => if (is_signed) .slt else .ult, |
| 5251 | .lte => if (is_signed) llvm.IntPredicate.SLE else .ULE, | 5604 | .lte => if (is_signed) .sle else .ule, |
| 5252 | .gt => if (is_signed) llvm.IntPredicate.SGT else .UGT, | 5605 | .gt => if (is_signed) .sgt else .ugt, |
| 5253 | .gte => if (is_signed) llvm.IntPredicate.SGE else .UGE, | 5606 | .gte => if (is_signed) .sge else .uge, |
| 5254 | }; | 5607 | }; |
| 5255 | return self.builder.buildICmp(operation, lhs, rhs, ""); | 5608 | return self.wip.icmp(cond, lhs, rhs, ""); |
| 5256 | } | 5609 | } |
| 5257 | | 5610 | |
| 5258 | fn airBlock(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5611 | fn airBlock(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5259 | const o = self.dg.object; | 5612 | const o = self.dg.object; |
| 5260 | const mod = o.module; | 5613 | const mod = o.module; |
| 5261 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 5614 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 5262 | const extra = self.air.extraData(Air.Block, ty_pl.payload); | 5615 | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 5263 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; | 5616 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| 5264 | const inst_ty = self.typeOfIndex(inst); | 5617 | const inst_ty = self.typeOfIndex(inst); |
| 5265 | const parent_bb = self.context.createBasicBlock("Block"); | | |
| 5266 | | 5618 | |
| 5267 | if (inst_ty.isNoReturn(mod)) { | 5619 | if (inst_ty.isNoReturn(mod)) { |
| 5268 | try self.genBody(body); | 5620 | try self.genBody(body); |
| 5269 | return null; | 5621 | return .none; |
| 5270 | } | 5622 | } |
| 5271 | | 5623 | |
| 5272 | var breaks: BreakList = .{}; | 5624 | const have_block_result = inst_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod); |
| 5273 | defer breaks.deinit(self.gpa); | | |
| 5274 | | 5625 | |
| | 5626 | var breaks: BreakList = if (have_block_result) .{ .list = .{} } else .{ .len = 0 }; |
| | 5627 | defer if (have_block_result) breaks.list.deinit(self.gpa); |
| | 5628 | |
| | 5629 | const parent_bb = try self.wip.block(0, "Block"); |
| 5275 | try self.blocks.putNoClobber(self.gpa, inst, .{ | 5630 | try self.blocks.putNoClobber(self.gpa, inst, .{ |
| 5276 | .parent_bb = parent_bb, | 5631 | .parent_bb = parent_bb, |
| 5277 | .breaks = &breaks, | 5632 | .breaks = &breaks, |
| ... | @@ -5280,36 +5635,33 @@ pub const FuncGen = struct { | ... | @@ -5280,36 +5635,33 @@ pub const FuncGen = struct { |
| 5280 | | 5635 | |
| 5281 | try self.genBody(body); | 5636 | try self.genBody(body); |
| 5282 | | 5637 | |
| 5283 | self.llvm_func.appendExistingBasicBlock(parent_bb); | 5638 | self.wip.cursor = .{ .block = parent_bb }; |
| 5284 | self.builder.positionBuilderAtEnd(parent_bb); | | |
| 5285 | | 5639 | |
| 5286 | // Create a phi node only if the block returns a value. | 5640 | // Create a phi node only if the block returns a value. |
| 5287 | const is_body = inst_ty.zigTypeTag(mod) == .Fn; | 5641 | if (have_block_result) { |
| 5288 | if (!is_body and !inst_ty.hasRuntimeBitsIgnoreComptime(mod)) return null; | 5642 | const raw_llvm_ty = try o.lowerType(inst_ty); |
| 5289 | | 5643 | const llvm_ty: Builder.Type = ty: { |
| 5290 | const raw_llvm_ty = try o.lowerType(inst_ty); | 5644 | // If the zig tag type is a function, this represents an actual function body; not |
| 5291 | | 5645 | // a pointer to it. LLVM IR allows the call instruction to use function bodies instead |
| 5292 | const llvm_ty = ty: { | 5646 | // of function pointers, however the phi makes it a runtime value and therefore |
| 5293 | // If the zig tag type is a function, this represents an actual function body; not | 5647 | // the LLVM type has to be wrapped in a pointer. |
| 5294 | // a pointer to it. LLVM IR allows the call instruction to use function bodies instead | 5648 | if (inst_ty.zigTypeTag(mod) == .Fn or isByRef(inst_ty, mod)) { |
| 5295 | // of function pointers, however the phi makes it a runtime value and therefore | 5649 | break :ty .ptr; |
| 5296 | // the LLVM type has to be wrapped in a pointer. | 5650 | } |
| 5297 | if (is_body or isByRef(inst_ty, mod)) { | 5651 | break :ty raw_llvm_ty; |
| 5298 | break :ty self.context.pointerType(0); | 5652 | }; |
| 5299 | } | | |
| 5300 | break :ty raw_llvm_ty; | | |
| 5301 | }; | | |
| 5302 | | 5653 | |
| 5303 | const phi_node = self.builder.buildPhi(llvm_ty, ""); | 5654 | parent_bb.ptr(&self.wip).incoming = @intCast(breaks.list.len); |
| 5304 | phi_node.addIncoming( | 5655 | const phi = try self.wip.phi(llvm_ty, ""); |
| 5305 | breaks.items(.val).ptr, | 5656 | try phi.finish(breaks.list.items(.val), breaks.list.items(.bb), &self.wip); |
| 5306 | breaks.items(.bb).ptr, | 5657 | return phi.toValue(); |
| 5307 | @as(c_uint, @intCast(breaks.len)), | 5658 | } else { |
| 5308 | ); | 5659 | parent_bb.ptr(&self.wip).incoming = @intCast(breaks.len); |
| 5309 | return phi_node; | 5660 | return .none; |
| | 5661 | } |
| 5310 | } | 5662 | } |
| 5311 | | 5663 | |
| 5312 | fn airBr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5664 | fn airBr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5313 | const o = self.dg.object; | 5665 | const o = self.dg.object; |
| 5314 | const branch = self.air.instructions.items(.data)[inst].br; | 5666 | const branch = self.air.instructions.items(.data)[inst].br; |
| 5315 | const block = self.blocks.get(branch.block_inst).?; | 5667 | const block = self.blocks.get(branch.block_inst).?; |
| ... | @@ -5317,42 +5669,39 @@ pub const FuncGen = struct { | ... | @@ -5317,42 +5669,39 @@ pub const FuncGen = struct { |
| 5317 | // Add the values to the lists only if the break provides a value. | 5669 | // Add the values to the lists only if the break provides a value. |
| 5318 | const operand_ty = self.typeOf(branch.operand); | 5670 | const operand_ty = self.typeOf(branch.operand); |
| 5319 | const mod = o.module; | 5671 | const mod = o.module; |
| 5320 | if (operand_ty.hasRuntimeBitsIgnoreComptime(mod) or operand_ty.zigTypeTag(mod) == .Fn) { | 5672 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 5321 | const val = try self.resolveInst(branch.operand); | 5673 | const val = try self.resolveInst(branch.operand); |
| 5322 | | 5674 | |
| 5323 | // For the phi node, we need the basic blocks and the values of the | 5675 | // For the phi node, we need the basic blocks and the values of the |
| 5324 | // break instructions. | 5676 | // break instructions. |
| 5325 | try block.breaks.append(self.gpa, .{ | 5677 | try block.breaks.list.append(self.gpa, .{ .bb = self.wip.cursor.block, .val = val }); |
| 5326 | .bb = self.builder.getInsertBlock(), | 5678 | } else block.breaks.len += 1; |
| 5327 | .val = val, | 5679 | _ = try self.wip.br(block.parent_bb); |
| 5328 | }); | 5680 | return .none; |
| 5329 | } | | |
| 5330 | _ = self.builder.buildBr(block.parent_bb); | | |
| 5331 | return null; | | |
| 5332 | } | 5681 | } |
| 5333 | | 5682 | |
| 5334 | fn airCondBr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5683 | fn airCondBr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5335 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 5684 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 5336 | const cond = try self.resolveInst(pl_op.operand); | 5685 | const cond = try self.resolveInst(pl_op.operand); |
| 5337 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); | 5686 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 5338 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; | 5687 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 5339 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; | 5688 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 5340 | | 5689 | |
| 5341 | const then_block = self.context.appendBasicBlock(self.llvm_func, "Then"); | 5690 | const then_block = try self.wip.block(1, "Then"); |
| 5342 | const else_block = self.context.appendBasicBlock(self.llvm_func, "Else"); | 5691 | const else_block = try self.wip.block(1, "Else"); |
| 5343 | _ = self.builder.buildCondBr(cond, then_block, else_block); | 5692 | _ = try self.wip.brCond(cond, then_block, else_block); |
| 5344 | | 5693 | |
| 5345 | self.builder.positionBuilderAtEnd(then_block); | 5694 | self.wip.cursor = .{ .block = then_block }; |
| 5346 | try self.genBody(then_body); | 5695 | try self.genBody(then_body); |
| 5347 | | 5696 | |
| 5348 | self.builder.positionBuilderAtEnd(else_block); | 5697 | self.wip.cursor = .{ .block = else_block }; |
| 5349 | try self.genBody(else_body); | 5698 | try self.genBody(else_body); |
| 5350 | | 5699 | |
| 5351 | // No need to reset the insert cursor since this instruction is noreturn. | 5700 | // No need to reset the insert cursor since this instruction is noreturn. |
| 5352 | return null; | 5701 | return .none; |
| 5353 | } | 5702 | } |
| 5354 | | 5703 | |
| 5355 | fn airTry(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 5704 | fn airTry(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 5356 | const o = self.dg.object; | 5705 | const o = self.dg.object; |
| 5357 | const mod = o.module; | 5706 | const mod = o.module; |
| 5358 | const inst = body_tail[0]; | 5707 | const inst = body_tail[0]; |
| ... | @@ -5367,7 +5716,7 @@ pub const FuncGen = struct { | ... | @@ -5367,7 +5716,7 @@ pub const FuncGen = struct { |
| 5367 | return lowerTry(self, err_union, body, err_union_ty, false, can_elide_load, is_unused); | 5716 | return lowerTry(self, err_union, body, err_union_ty, false, can_elide_load, is_unused); |
| 5368 | } | 5717 | } |
| 5369 | | 5718 | |
| 5370 | fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5719 | fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5371 | const o = self.dg.object; | 5720 | const o = self.dg.object; |
| 5372 | const mod = o.module; | 5721 | const mod = o.module; |
| 5373 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 5722 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -5381,13 +5730,13 @@ pub const FuncGen = struct { | ... | @@ -5381,13 +5730,13 @@ pub const FuncGen = struct { |
| 5381 | | 5730 | |
| 5382 | fn lowerTry( | 5731 | fn lowerTry( |
| 5383 | fg: *FuncGen, | 5732 | fg: *FuncGen, |
| 5384 | err_union: *llvm.Value, | 5733 | err_union: Builder.Value, |
| 5385 | body: []const Air.Inst.Index, | 5734 | body: []const Air.Inst.Index, |
| 5386 | err_union_ty: Type, | 5735 | err_union_ty: Type, |
| 5387 | operand_is_ptr: bool, | 5736 | operand_is_ptr: bool, |
| 5388 | can_elide_load: bool, | 5737 | can_elide_load: bool, |
| 5389 | is_unused: bool, | 5738 | is_unused: bool, |
| 5390 | ) !?*llvm.Value { | 5739 | ) !Builder.Value { |
| 5391 | const o = fg.dg.object; | 5740 | const o = fg.dg.object; |
| 5392 | const mod = o.module; | 5741 | const mod = o.module; |
| 5393 | const payload_ty = err_union_ty.errorUnionPayload(mod); | 5742 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| ... | @@ -5395,122 +5744,135 @@ pub const FuncGen = struct { | ... | @@ -5395,122 +5744,135 @@ pub const FuncGen = struct { |
| 5395 | const err_union_llvm_ty = try o.lowerType(err_union_ty); | 5744 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 5396 | | 5745 | |
| 5397 | if (!err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { | 5746 | if (!err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 5398 | const is_err = err: { | 5747 | const loaded = loaded: { |
| 5399 | const err_set_ty = try o.lowerType(Type.anyerror); | | |
| 5400 | const zero = err_set_ty.constNull(); | | |
| 5401 | if (!payload_has_bits) { | 5748 | if (!payload_has_bits) { |
| 5402 | // TODO add alignment to this load | 5749 | // TODO add alignment to this load |
| 5403 | const loaded = if (operand_is_ptr) | 5750 | break :loaded if (operand_is_ptr) |
| 5404 | fg.builder.buildLoad(err_set_ty, err_union, "") | 5751 | try fg.wip.load(.normal, Builder.Type.err_int, err_union, .default, "") |
| 5405 | else | 5752 | else |
| 5406 | err_union; | 5753 | err_union; |
| 5407 | break :err fg.builder.buildICmp(.NE, loaded, zero, ""); | | |
| 5408 | } | 5754 | } |
| 5409 | const err_field_index = errUnionErrorOffset(payload_ty, mod); | 5755 | const err_field_index = errUnionErrorOffset(payload_ty, mod); |
| 5410 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { | 5756 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { |
| 5411 | const err_field_ptr = fg.builder.buildStructGEP(err_union_llvm_ty, err_union, err_field_index, ""); | 5757 | const err_field_ptr = |
| | 5758 | try fg.wip.gepStruct(err_union_llvm_ty, err_union, err_field_index, ""); |
| 5412 | // TODO add alignment to this load | 5759 | // TODO add alignment to this load |
| 5413 | const loaded = fg.builder.buildLoad(err_set_ty, err_field_ptr, ""); | 5760 | break :loaded try fg.wip.load( |
| 5414 | break :err fg.builder.buildICmp(.NE, loaded, zero, ""); | 5761 | .normal, |
| | 5762 | Builder.Type.err_int, |
| | 5763 | err_field_ptr, |
| | 5764 | .default, |
| | 5765 | "", |
| | 5766 | ); |
| 5415 | } | 5767 | } |
| 5416 | const loaded = fg.builder.buildExtractValue(err_union, err_field_index, ""); | 5768 | break :loaded try fg.wip.extractValue(err_union, &.{err_field_index}, ""); |
| 5417 | break :err fg.builder.buildICmp(.NE, loaded, zero, ""); | | |
| 5418 | }; | 5769 | }; |
| | 5770 | const zero = try o.builder.intValue(Builder.Type.err_int, 0); |
| | 5771 | const is_err = try fg.wip.icmp(.ne, loaded, zero, ""); |
| 5419 | | 5772 | |
| 5420 | const return_block = fg.context.appendBasicBlock(fg.llvm_func, "TryRet"); | 5773 | const return_block = try fg.wip.block(1, "TryRet"); |
| 5421 | const continue_block = fg.context.appendBasicBlock(fg.llvm_func, "TryCont"); | 5774 | const continue_block = try fg.wip.block(1, "TryCont"); |
| 5422 | _ = fg.builder.buildCondBr(is_err, return_block, continue_block); | 5775 | _ = try fg.wip.brCond(is_err, return_block, continue_block); |
| 5423 | | 5776 | |
| 5424 | fg.builder.positionBuilderAtEnd(return_block); | 5777 | fg.wip.cursor = .{ .block = return_block }; |
| 5425 | try fg.genBody(body); | 5778 | try fg.genBody(body); |
| 5426 | | 5779 | |
| 5427 | fg.builder.positionBuilderAtEnd(continue_block); | 5780 | fg.wip.cursor = .{ .block = continue_block }; |
| 5428 | } | | |
| 5429 | if (is_unused) { | | |
| 5430 | return null; | | |
| 5431 | } | | |
| 5432 | if (!payload_has_bits) { | | |
| 5433 | return if (operand_is_ptr) err_union else null; | | |
| 5434 | } | 5781 | } |
| | 5782 | if (is_unused) return .none; |
| | 5783 | if (!payload_has_bits) return if (operand_is_ptr) err_union else .none; |
| 5435 | const offset = errUnionPayloadOffset(payload_ty, mod); | 5784 | const offset = errUnionPayloadOffset(payload_ty, mod); |
| 5436 | if (operand_is_ptr) { | 5785 | if (operand_is_ptr) { |
| 5437 | return fg.builder.buildStructGEP(err_union_llvm_ty, err_union, offset, ""); | 5786 | return fg.wip.gepStruct(err_union_llvm_ty, err_union, offset, ""); |
| 5438 | } else if (isByRef(err_union_ty, mod)) { | 5787 | } else if (isByRef(err_union_ty, mod)) { |
| 5439 | const payload_ptr = fg.builder.buildStructGEP(err_union_llvm_ty, err_union, offset, ""); | 5788 | const payload_ptr = try fg.wip.gepStruct(err_union_llvm_ty, err_union, offset, ""); |
| | 5789 | const payload_alignment = Builder.Alignment.fromByteUnits(payload_ty.abiAlignment(mod)); |
| 5440 | if (isByRef(payload_ty, mod)) { | 5790 | if (isByRef(payload_ty, mod)) { |
| 5441 | if (can_elide_load) | 5791 | if (can_elide_load) |
| 5442 | return payload_ptr; | 5792 | return payload_ptr; |
| 5443 | | 5793 | |
| 5444 | return fg.loadByRef(payload_ptr, payload_ty, payload_ty.abiAlignment(mod), false); | 5794 | return fg.loadByRef(payload_ptr, payload_ty, payload_alignment, false); |
| 5445 | } | 5795 | } |
| 5446 | const load_inst = fg.builder.buildLoad(err_union_llvm_ty.structGetTypeAtIndex(offset), payload_ptr, ""); | 5796 | const load_ty = err_union_llvm_ty.structFields(&o.builder)[offset]; |
| 5447 | load_inst.setAlignment(payload_ty.abiAlignment(mod)); | 5797 | return fg.wip.load(.normal, load_ty, payload_ptr, payload_alignment, ""); |
| 5448 | return load_inst; | | |
| 5449 | } | 5798 | } |
| 5450 | return fg.builder.buildExtractValue(err_union, offset, ""); | 5799 | return fg.wip.extractValue(err_union, &.{offset}, ""); |
| 5451 | } | 5800 | } |
| 5452 | | 5801 | |
| 5453 | fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5802 | fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5454 | const o = self.dg.object; | 5803 | const o = self.dg.object; |
| 5455 | const mod = o.module; | | |
| 5456 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 5804 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 5457 | const cond = try self.resolveInst(pl_op.operand); | 5805 | const cond = try self.resolveInst(pl_op.operand); |
| 5458 | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); | 5806 | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); |
| 5459 | const else_block = self.context.appendBasicBlock(self.llvm_func, "Else"); | 5807 | const else_block = try self.wip.block(1, "Default"); |
| 5460 | const target = mod.getTarget(); | 5808 | const llvm_usize = try o.lowerType(Type.usize); |
| 5461 | const llvm_usize = self.context.intType(target.ptrBitWidth()); | 5809 | const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder)) |
| 5462 | const cond_int = if (cond.typeOf().getTypeKind() == .Pointer) | 5810 | try self.wip.cast(.ptrtoint, cond, llvm_usize, "") |
| 5463 | self.builder.buildPtrToInt(cond, llvm_usize, "") | | |
| 5464 | else | 5811 | else |
| 5465 | cond; | 5812 | cond; |
| 5466 | const llvm_switch = self.builder.buildSwitch(cond_int, else_block, switch_br.data.cases_len); | | |
| 5467 | | 5813 | |
| 5468 | var extra_index: usize = switch_br.end; | 5814 | var extra_index: usize = switch_br.end; |
| 5469 | var case_i: u32 = 0; | 5815 | var case_i: u32 = 0; |
| | 5816 | var llvm_cases_len: u32 = 0; |
| | 5817 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| | 5818 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| | 5819 | const items: []const Air.Inst.Ref = |
| | 5820 | @ptrCast(self.air.extra[case.end..][0..case.data.items_len]); |
| | 5821 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| | 5822 | extra_index = case.end + case.data.items_len + case_body.len; |
| | 5823 | |
| | 5824 | llvm_cases_len += @intCast(items.len); |
| | 5825 | } |
| | 5826 | |
| | 5827 | var wip_switch = try self.wip.@"switch"(cond_int, else_block, llvm_cases_len); |
| | 5828 | defer wip_switch.finish(&self.wip); |
| 5470 | | 5829 | |
| | 5830 | extra_index = switch_br.end; |
| | 5831 | case_i = 0; |
| 5471 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { | 5832 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| 5472 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); | 5833 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 5473 | const items = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[case.end..][0..case.data.items_len])); | 5834 | const items: []const Air.Inst.Ref = |
| | 5835 | @ptrCast(self.air.extra[case.end..][0..case.data.items_len]); |
| 5474 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; | 5836 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 5475 | extra_index = case.end + case.data.items_len + case_body.len; | 5837 | extra_index = case.end + case.data.items_len + case_body.len; |
| 5476 | | 5838 | |
| 5477 | const case_block = self.context.appendBasicBlock(self.llvm_func, "Case"); | 5839 | const case_block = try self.wip.block(@intCast(items.len), "Case"); |
| 5478 | | 5840 | |
| 5479 | for (items) |item| { | 5841 | for (items) |item| { |
| 5480 | const llvm_item = try self.resolveInst(item); | 5842 | const llvm_item = (try self.resolveInst(item)).toConst().?; |
| 5481 | const llvm_int_item = if (llvm_item.typeOf().getTypeKind() == .Pointer) | 5843 | const llvm_int_item = if (llvm_item.typeOf(&o.builder).isPointer(&o.builder)) |
| 5482 | llvm_item.constPtrToInt(llvm_usize) | 5844 | try o.builder.castConst(.ptrtoint, llvm_item, llvm_usize) |
| 5483 | else | 5845 | else |
| 5484 | llvm_item; | 5846 | llvm_item; |
| 5485 | llvm_switch.addCase(llvm_int_item, case_block); | 5847 | try wip_switch.addCase(llvm_int_item, case_block, &self.wip); |
| 5486 | } | 5848 | } |
| 5487 | | 5849 | |
| 5488 | self.builder.positionBuilderAtEnd(case_block); | 5850 | self.wip.cursor = .{ .block = case_block }; |
| 5489 | try self.genBody(case_body); | 5851 | try self.genBody(case_body); |
| 5490 | } | 5852 | } |
| 5491 | | 5853 | |
| 5492 | self.builder.positionBuilderAtEnd(else_block); | 5854 | self.wip.cursor = .{ .block = else_block }; |
| 5493 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; | 5855 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 5494 | if (else_body.len != 0) { | 5856 | if (else_body.len != 0) { |
| 5495 | try self.genBody(else_body); | 5857 | try self.genBody(else_body); |
| 5496 | } else { | 5858 | } else { |
| 5497 | _ = self.builder.buildUnreachable(); | 5859 | _ = try self.wip.@"unreachable"(); |
| 5498 | } | 5860 | } |
| 5499 | | 5861 | |
| 5500 | // No need to reset the insert cursor since this instruction is noreturn. | 5862 | // No need to reset the insert cursor since this instruction is noreturn. |
| 5501 | return null; | 5863 | return .none; |
| 5502 | } | 5864 | } |
| 5503 | | 5865 | |
| 5504 | fn airLoop(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5866 | fn airLoop(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5505 | const o = self.dg.object; | 5867 | const o = self.dg.object; |
| 5506 | const mod = o.module; | 5868 | const mod = o.module; |
| 5507 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 5869 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 5508 | const loop = self.air.extraData(Air.Block, ty_pl.payload); | 5870 | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 5509 | const body = self.air.extra[loop.end..][0..loop.data.body_len]; | 5871 | const body = self.air.extra[loop.end..][0..loop.data.body_len]; |
| 5510 | const loop_block = self.context.appendBasicBlock(self.llvm_func, "Loop"); | 5872 | const loop_block = try self.wip.block(2, "Loop"); |
| 5511 | _ = self.builder.buildBr(loop_block); | 5873 | _ = try self.wip.br(loop_block); |
| 5512 | | 5874 | |
| 5513 | self.builder.positionBuilderAtEnd(loop_block); | 5875 | self.wip.cursor = .{ .block = loop_block }; |
| 5514 | try self.genBody(body); | 5876 | try self.genBody(body); |
| 5515 | | 5877 | |
| 5516 | // TODO instead of this logic, change AIR to have the property that | 5878 | // TODO instead of this logic, change AIR to have the property that |
| ... | @@ -5520,35 +5882,30 @@ pub const FuncGen = struct { | ... | @@ -5520,35 +5882,30 @@ pub const FuncGen = struct { |
| 5520 | // be while(true) instead of for(body), which will eliminate 1 branch on | 5882 | // be while(true) instead of for(body), which will eliminate 1 branch on |
| 5521 | // a hot path. | 5883 | // a hot path. |
| 5522 | if (body.len == 0 or !self.typeOfIndex(body[body.len - 1]).isNoReturn(mod)) { | 5884 | if (body.len == 0 or !self.typeOfIndex(body[body.len - 1]).isNoReturn(mod)) { |
| 5523 | _ = self.builder.buildBr(loop_block); | 5885 | _ = try self.wip.br(loop_block); |
| 5524 | } | 5886 | } |
| 5525 | return null; | 5887 | return .none; |
| 5526 | } | 5888 | } |
| 5527 | | 5889 | |
| 5528 | fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5890 | fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5529 | const o = self.dg.object; | 5891 | const o = self.dg.object; |
| 5530 | const mod = o.module; | 5892 | const mod = o.module; |
| 5531 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5893 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5532 | const operand_ty = self.typeOf(ty_op.operand); | 5894 | const operand_ty = self.typeOf(ty_op.operand); |
| 5533 | const array_ty = operand_ty.childType(mod); | 5895 | const array_ty = operand_ty.childType(mod); |
| 5534 | const llvm_usize = try o.lowerType(Type.usize); | 5896 | const llvm_usize = try o.lowerType(Type.usize); |
| 5535 | const len = llvm_usize.constInt(array_ty.arrayLen(mod), .False); | 5897 | const len = try o.builder.intValue(llvm_usize, array_ty.arrayLen(mod)); |
| 5536 | const slice_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); | 5898 | const slice_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); |
| 5537 | const operand = try self.resolveInst(ty_op.operand); | 5899 | const operand = try self.resolveInst(ty_op.operand); |
| 5538 | if (!array_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 5900 | if (!array_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 5539 | const partial = self.builder.buildInsertValue(slice_llvm_ty.getUndef(), operand, 0, ""); | 5901 | return self.wip.buildAggregate(slice_llvm_ty, &.{ operand, len }, ""); |
| 5540 | return self.builder.buildInsertValue(partial, len, 1, ""); | 5902 | const ptr = try self.wip.gep(.inbounds, try o.lowerType(array_ty), operand, &.{ |
| 5541 | } | 5903 | try o.builder.intValue(llvm_usize, 0), try o.builder.intValue(llvm_usize, 0), |
| 5542 | const indices: [2]*llvm.Value = .{ | 5904 | }, ""); |
| 5543 | llvm_usize.constNull(), llvm_usize.constNull(), | 5905 | return self.wip.buildAggregate(slice_llvm_ty, &.{ ptr, len }, ""); |
| 5544 | }; | | |
| 5545 | const array_llvm_ty = try o.lowerType(array_ty); | | |
| 5546 | const ptr = self.builder.buildInBoundsGEP(array_llvm_ty, operand, &indices, indices.len, ""); | | |
| 5547 | const partial = self.builder.buildInsertValue(slice_llvm_ty.getUndef(), ptr, 0, ""); | | |
| 5548 | return self.builder.buildInsertValue(partial, len, 1, ""); | | |
| 5549 | } | 5906 | } |
| 5550 | | 5907 | |
| 5551 | fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 5908 | fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5552 | const o = self.dg.object; | 5909 | const o = self.dg.object; |
| 5553 | const mod = o.module; | 5910 | const mod = o.module; |
| 5554 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5911 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -5562,51 +5919,53 @@ pub const FuncGen = struct { | ... | @@ -5562,51 +5919,53 @@ pub const FuncGen = struct { |
| 5562 | const dest_llvm_ty = try o.lowerType(dest_ty); | 5919 | const dest_llvm_ty = try o.lowerType(dest_ty); |
| 5563 | const target = mod.getTarget(); | 5920 | const target = mod.getTarget(); |
| 5564 | | 5921 | |
| 5565 | if (intrinsicsAllowed(dest_scalar_ty, target)) { | 5922 | if (intrinsicsAllowed(dest_scalar_ty, target)) return self.wip.conv( |
| 5566 | if (operand_scalar_ty.isSignedInt(mod)) { | 5923 | if (operand_scalar_ty.isSignedInt(mod)) .signed else .unsigned, |
| 5567 | return self.builder.buildSIToFP(operand, dest_llvm_ty, ""); | 5924 | operand, |
| 5568 | } else { | 5925 | dest_llvm_ty, |
| 5569 | return self.builder.buildUIToFP(operand, dest_llvm_ty, ""); | 5926 | "", |
| 5570 | } | 5927 | ); |
| 5571 | } | | |
| 5572 | | 5928 | |
| 5573 | const operand_bits = @as(u16, @intCast(operand_scalar_ty.bitSize(mod))); | 5929 | const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(mod))); |
| 5574 | const rt_int_bits = compilerRtIntBits(operand_bits); | 5930 | const rt_int_ty = try o.builder.intType(rt_int_bits); |
| 5575 | const rt_int_ty = self.context.intType(rt_int_bits); | 5931 | var extended = try self.wip.conv( |
| 5576 | var extended = e: { | 5932 | if (operand_scalar_ty.isSignedInt(mod)) .signed else .unsigned, |
| 5577 | if (operand_scalar_ty.isSignedInt(mod)) { | 5933 | operand, |
| 5578 | break :e self.builder.buildSExtOrBitCast(operand, rt_int_ty, ""); | 5934 | rt_int_ty, |
| 5579 | } else { | 5935 | "", |
| 5580 | break :e self.builder.buildZExtOrBitCast(operand, rt_int_ty, ""); | 5936 | ); |
| 5581 | } | | |
| 5582 | }; | | |
| 5583 | const dest_bits = dest_scalar_ty.floatBits(target); | 5937 | const dest_bits = dest_scalar_ty.floatBits(target); |
| 5584 | const compiler_rt_operand_abbrev = compilerRtIntAbbrev(rt_int_bits); | 5938 | const compiler_rt_operand_abbrev = compilerRtIntAbbrev(rt_int_bits); |
| 5585 | const compiler_rt_dest_abbrev = compilerRtFloatAbbrev(dest_bits); | 5939 | const compiler_rt_dest_abbrev = compilerRtFloatAbbrev(dest_bits); |
| 5586 | const sign_prefix = if (operand_scalar_ty.isSignedInt(mod)) "" else "un"; | 5940 | const sign_prefix = if (operand_scalar_ty.isSignedInt(mod)) "" else "un"; |
| 5587 | var fn_name_buf: [64]u8 = undefined; | 5941 | const fn_name = try o.builder.fmt("__float{s}{s}i{s}f", .{ |
| 5588 | const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__float{s}{s}i{s}f", .{ | | |
| 5589 | sign_prefix, | 5942 | sign_prefix, |
| 5590 | compiler_rt_operand_abbrev, | 5943 | compiler_rt_operand_abbrev, |
| 5591 | compiler_rt_dest_abbrev, | 5944 | compiler_rt_dest_abbrev, |
| 5592 | }) catch unreachable; | 5945 | }); |
| 5593 | | 5946 | |
| 5594 | var param_types = [1]*llvm.Type{rt_int_ty}; | 5947 | var param_type = rt_int_ty; |
| 5595 | if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) { | 5948 | if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) { |
| 5596 | // On Windows x86-64, "ti" functions must use Vector(2, u64) instead of the standard | 5949 | // On Windows x86-64, "ti" functions must use Vector(2, u64) instead of the standard |
| 5597 | // i128 calling convention to adhere to the ABI that LLVM expects compiler-rt to have. | 5950 | // i128 calling convention to adhere to the ABI that LLVM expects compiler-rt to have. |
| 5598 | const v2i64 = self.context.intType(64).vectorType(2); | 5951 | param_type = try o.builder.vectorType(.normal, 2, .i64); |
| 5599 | extended = self.builder.buildBitCast(extended, v2i64, ""); | 5952 | extended = try self.wip.cast(.bitcast, extended, param_type, ""); |
| 5600 | param_types = [1]*llvm.Type{v2i64}; | | |
| 5601 | } | 5953 | } |
| 5602 | | 5954 | |
| 5603 | const libc_fn = self.getLibcFunction(fn_name, &param_types, dest_llvm_ty); | 5955 | const libc_fn = try self.getLibcFunction(fn_name, &.{param_type}, dest_llvm_ty); |
| 5604 | const params = [1]*llvm.Value{extended}; | 5956 | const params = [1]*llvm.Value{extended.toLlvm(&self.wip)}; |
| 5605 | | 5957 | return (try self.wip.unimplemented(dest_llvm_ty, "")).finish(self.builder.buildCall( |
| 5606 | return self.builder.buildCall(libc_fn.globalGetValueType(), libc_fn, &params, params.len, .C, .Auto, ""); | 5958 | libc_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 5959 | libc_fn.toLlvm(&o.builder), |
| | 5960 | &params, |
| | 5961 | params.len, |
| | 5962 | .C, |
| | 5963 | .Auto, |
| | 5964 | "", |
| | 5965 | ), &self.wip); |
| 5607 | } | 5966 | } |
| 5608 | | 5967 | |
| 5609 | fn airIntFromFloat(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 5968 | fn airIntFromFloat(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 5610 | self.builder.setFastMath(want_fast_math); | 5969 | self.builder.setFastMath(want_fast_math); |
| 5611 | | 5970 | |
| 5612 | const o = self.dg.object; | 5971 | const o = self.dg.object; |
| ... | @@ -5624,19 +5983,20 @@ pub const FuncGen = struct { | ... | @@ -5624,19 +5983,20 @@ pub const FuncGen = struct { |
| 5624 | | 5983 | |
| 5625 | if (intrinsicsAllowed(operand_scalar_ty, target)) { | 5984 | if (intrinsicsAllowed(operand_scalar_ty, target)) { |
| 5626 | // TODO set fast math flag | 5985 | // TODO set fast math flag |
| 5627 | if (dest_scalar_ty.isSignedInt(mod)) { | 5986 | return self.wip.conv( |
| 5628 | return self.builder.buildFPToSI(operand, dest_llvm_ty, ""); | 5987 | if (dest_scalar_ty.isSignedInt(mod)) .signed else .unsigned, |
| 5629 | } else { | 5988 | operand, |
| 5630 | return self.builder.buildFPToUI(operand, dest_llvm_ty, ""); | 5989 | dest_llvm_ty, |
| 5631 | } | 5990 | "", |
| | 5991 | ); |
| 5632 | } | 5992 | } |
| 5633 | | 5993 | |
| 5634 | const rt_int_bits = compilerRtIntBits(@as(u16, @intCast(dest_scalar_ty.bitSize(mod)))); | 5994 | const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(mod))); |
| 5635 | const ret_ty = self.context.intType(rt_int_bits); | 5995 | const ret_ty = try o.builder.intType(rt_int_bits); |
| 5636 | const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: { | 5996 | const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: { |
| 5637 | // On Windows x86-64, "ti" functions must use Vector(2, u64) instead of the standard | 5997 | // On Windows x86-64, "ti" functions must use Vector(2, u64) instead of the standard |
| 5638 | // i128 calling convention to adhere to the ABI that LLVM expects compiler-rt to have. | 5998 | // i128 calling convention to adhere to the ABI that LLVM expects compiler-rt to have. |
| 5639 | break :b self.context.intType(64).vectorType(2); | 5999 | break :b try o.builder.vectorType(.normal, 2, .i64); |
| 5640 | } else ret_ty; | 6000 | } else ret_ty; |
| 5641 | | 6001 | |
| 5642 | const operand_bits = operand_scalar_ty.floatBits(target); | 6002 | const operand_bits = operand_scalar_ty.floatBits(target); |
| ... | @@ -5645,66 +6005,66 @@ pub const FuncGen = struct { | ... | @@ -5645,66 +6005,66 @@ pub const FuncGen = struct { |
| 5645 | const compiler_rt_dest_abbrev = compilerRtIntAbbrev(rt_int_bits); | 6005 | const compiler_rt_dest_abbrev = compilerRtIntAbbrev(rt_int_bits); |
| 5646 | const sign_prefix = if (dest_scalar_ty.isSignedInt(mod)) "" else "uns"; | 6006 | const sign_prefix = if (dest_scalar_ty.isSignedInt(mod)) "" else "uns"; |
| 5647 | | 6007 | |
| 5648 | var fn_name_buf: [64]u8 = undefined; | 6008 | const fn_name = try o.builder.fmt("__fix{s}{s}f{s}i", .{ |
| 5649 | const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__fix{s}{s}f{s}i", .{ | | |
| 5650 | sign_prefix, | 6009 | sign_prefix, |
| 5651 | compiler_rt_operand_abbrev, | 6010 | compiler_rt_operand_abbrev, |
| 5652 | compiler_rt_dest_abbrev, | 6011 | compiler_rt_dest_abbrev, |
| 5653 | }) catch unreachable; | 6012 | }); |
| 5654 | | 6013 | |
| 5655 | const operand_llvm_ty = try o.lowerType(operand_ty); | 6014 | const operand_llvm_ty = try o.lowerType(operand_ty); |
| 5656 | const param_types = [1]*llvm.Type{operand_llvm_ty}; | 6015 | const libc_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, libc_ret_ty); |
| 5657 | const libc_fn = self.getLibcFunction(fn_name, &param_types, libc_ret_ty); | 6016 | const params = [1]*llvm.Value{operand.toLlvm(&self.wip)}; |
| 5658 | const params = [1]*llvm.Value{operand}; | 6017 | var result = (try self.wip.unimplemented(libc_ret_ty, "")).finish(self.builder.buildCall( |
| 5659 | | 6018 | libc_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| 5660 | var result = self.builder.buildCall(libc_fn.globalGetValueType(), libc_fn, &params, params.len, .C, .Auto, ""); | 6019 | libc_fn.toLlvm(&o.builder), |
| | 6020 | &params, |
| | 6021 | params.len, |
| | 6022 | .C, |
| | 6023 | .Auto, |
| | 6024 | "", |
| | 6025 | ), &self.wip); |
| 5661 | | 6026 | |
| 5662 | if (libc_ret_ty != ret_ty) result = self.builder.buildBitCast(result, ret_ty, ""); | 6027 | if (libc_ret_ty != ret_ty) result = try self.wip.cast(.bitcast, result, ret_ty, ""); |
| 5663 | if (ret_ty != dest_llvm_ty) result = self.builder.buildTrunc(result, dest_llvm_ty, ""); | 6028 | if (ret_ty != dest_llvm_ty) result = try self.wip.cast(.trunc, result, dest_llvm_ty, ""); |
| 5664 | return result; | 6029 | return result; |
| 5665 | } | 6030 | } |
| 5666 | | 6031 | |
| 5667 | fn sliceOrArrayPtr(fg: *FuncGen, ptr: *llvm.Value, ty: Type) *llvm.Value { | 6032 | fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value { |
| 5668 | const o = fg.dg.object; | 6033 | const o = fg.dg.object; |
| 5669 | const mod = o.module; | 6034 | const mod = o.module; |
| 5670 | if (ty.isSlice(mod)) { | 6035 | return if (ty.isSlice(mod)) fg.wip.extractValue(ptr, &.{0}, "") else ptr; |
| 5671 | return fg.builder.buildExtractValue(ptr, 0, ""); | | |
| 5672 | } else { | | |
| 5673 | return ptr; | | |
| 5674 | } | | |
| 5675 | } | 6036 | } |
| 5676 | | 6037 | |
| 5677 | fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: *llvm.Value, ty: Type) *llvm.Value { | 6038 | fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value { |
| 5678 | const o = fg.dg.object; | 6039 | const o = fg.dg.object; |
| 5679 | const mod = o.module; | 6040 | const mod = o.module; |
| 5680 | const target = mod.getTarget(); | 6041 | const llvm_usize = try o.lowerType(Type.usize); |
| 5681 | const llvm_usize_ty = fg.context.intType(target.ptrBitWidth()); | | |
| 5682 | switch (ty.ptrSize(mod)) { | 6042 | switch (ty.ptrSize(mod)) { |
| 5683 | .Slice => { | 6043 | .Slice => { |
| 5684 | const len = fg.builder.buildExtractValue(ptr, 1, ""); | 6044 | const len = try fg.wip.extractValue(ptr, &.{1}, ""); |
| 5685 | const elem_ty = ty.childType(mod); | 6045 | const elem_ty = ty.childType(mod); |
| 5686 | const abi_size = elem_ty.abiSize(mod); | 6046 | const abi_size = elem_ty.abiSize(mod); |
| 5687 | if (abi_size == 1) return len; | 6047 | if (abi_size == 1) return len; |
| 5688 | const abi_size_llvm_val = llvm_usize_ty.constInt(abi_size, .False); | 6048 | const abi_size_llvm_val = try o.builder.intValue(llvm_usize, abi_size); |
| 5689 | return fg.builder.buildMul(len, abi_size_llvm_val, ""); | 6049 | return fg.wip.bin(.@"mul nuw", len, abi_size_llvm_val, ""); |
| 5690 | }, | 6050 | }, |
| 5691 | .One => { | 6051 | .One => { |
| 5692 | const array_ty = ty.childType(mod); | 6052 | const array_ty = ty.childType(mod); |
| 5693 | const elem_ty = array_ty.childType(mod); | 6053 | const elem_ty = array_ty.childType(mod); |
| 5694 | const abi_size = elem_ty.abiSize(mod); | 6054 | const abi_size = elem_ty.abiSize(mod); |
| 5695 | return llvm_usize_ty.constInt(array_ty.arrayLen(mod) * abi_size, .False); | 6055 | return o.builder.intValue(llvm_usize, array_ty.arrayLen(mod) * abi_size); |
| 5696 | }, | 6056 | }, |
| 5697 | .Many, .C => unreachable, | 6057 | .Many, .C => unreachable, |
| 5698 | } | 6058 | } |
| 5699 | } | 6059 | } |
| 5700 | | 6060 | |
| 5701 | fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: c_uint) !?*llvm.Value { | 6061 | fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) !Builder.Value { |
| 5702 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 6062 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5703 | const operand = try self.resolveInst(ty_op.operand); | 6063 | const operand = try self.resolveInst(ty_op.operand); |
| 5704 | return self.builder.buildExtractValue(operand, index, ""); | 6064 | return self.wip.extractValue(operand, &.{index}, ""); |
| 5705 | } | 6065 | } |
| 5706 | | 6066 | |
| 5707 | fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: c_uint) !?*llvm.Value { | 6067 | fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: c_uint) !Builder.Value { |
| 5708 | const o = self.dg.object; | 6068 | const o = self.dg.object; |
| 5709 | const mod = o.module; | 6069 | const mod = o.module; |
| 5710 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 6070 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -5712,10 +6072,10 @@ pub const FuncGen = struct { | ... | @@ -5712,10 +6072,10 @@ pub const FuncGen = struct { |
| 5712 | const slice_ptr_ty = self.typeOf(ty_op.operand); | 6072 | const slice_ptr_ty = self.typeOf(ty_op.operand); |
| 5713 | const slice_llvm_ty = try o.lowerPtrElemTy(slice_ptr_ty.childType(mod)); | 6073 | const slice_llvm_ty = try o.lowerPtrElemTy(slice_ptr_ty.childType(mod)); |
| 5714 | | 6074 | |
| 5715 | return self.builder.buildStructGEP(slice_llvm_ty, slice_ptr, index, ""); | 6075 | return self.wip.gepStruct(slice_llvm_ty, slice_ptr, index, ""); |
| 5716 | } | 6076 | } |
| 5717 | | 6077 | |
| 5718 | fn airSliceElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 6078 | fn airSliceElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 5719 | const o = self.dg.object; | 6079 | const o = self.dg.object; |
| 5720 | const mod = o.module; | 6080 | const mod = o.module; |
| 5721 | const inst = body_tail[0]; | 6081 | const inst = body_tail[0]; |
| ... | @@ -5725,20 +6085,20 @@ pub const FuncGen = struct { | ... | @@ -5725,20 +6085,20 @@ pub const FuncGen = struct { |
| 5725 | const index = try self.resolveInst(bin_op.rhs); | 6085 | const index = try self.resolveInst(bin_op.rhs); |
| 5726 | const elem_ty = slice_ty.childType(mod); | 6086 | const elem_ty = slice_ty.childType(mod); |
| 5727 | const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty); | 6087 | const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty); |
| 5728 | const base_ptr = self.builder.buildExtractValue(slice, 0, ""); | 6088 | const base_ptr = try self.wip.extractValue(slice, &.{0}, ""); |
| 5729 | const indices: [1]*llvm.Value = .{index}; | 6089 | const ptr = try self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, &.{index}, ""); |
| 5730 | const ptr = self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | | |
| 5731 | if (isByRef(elem_ty, mod)) { | 6090 | if (isByRef(elem_ty, mod)) { |
| 5732 | if (self.canElideLoad(body_tail)) | 6091 | if (self.canElideLoad(body_tail)) |
| 5733 | return ptr; | 6092 | return ptr; |
| 5734 | | 6093 | |
| 5735 | return self.loadByRef(ptr, elem_ty, elem_ty.abiAlignment(mod), false); | 6094 | const elem_alignment = Builder.Alignment.fromByteUnits(elem_ty.abiAlignment(mod)); |
| | 6095 | return self.loadByRef(ptr, elem_ty, elem_alignment, false); |
| 5736 | } | 6096 | } |
| 5737 | | 6097 | |
| 5738 | return self.load(ptr, slice_ty); | 6098 | return self.load(ptr, slice_ty); |
| 5739 | } | 6099 | } |
| 5740 | | 6100 | |
| 5741 | fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6101 | fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5742 | const o = self.dg.object; | 6102 | const o = self.dg.object; |
| 5743 | const mod = o.module; | 6103 | const mod = o.module; |
| 5744 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 6104 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -5748,12 +6108,11 @@ pub const FuncGen = struct { | ... | @@ -5748,12 +6108,11 @@ pub const FuncGen = struct { |
| 5748 | const slice = try self.resolveInst(bin_op.lhs); | 6108 | const slice = try self.resolveInst(bin_op.lhs); |
| 5749 | const index = try self.resolveInst(bin_op.rhs); | 6109 | const index = try self.resolveInst(bin_op.rhs); |
| 5750 | const llvm_elem_ty = try o.lowerPtrElemTy(slice_ty.childType(mod)); | 6110 | const llvm_elem_ty = try o.lowerPtrElemTy(slice_ty.childType(mod)); |
| 5751 | const base_ptr = self.builder.buildExtractValue(slice, 0, ""); | 6111 | const base_ptr = try self.wip.extractValue(slice, &.{0}, ""); |
| 5752 | const indices: [1]*llvm.Value = .{index}; | 6112 | return self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, &.{index}, ""); |
| 5753 | return self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | | |
| 5754 | } | 6113 | } |
| 5755 | | 6114 | |
| 5756 | fn airArrayElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 6115 | fn airArrayElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 5757 | const o = self.dg.object; | 6116 | const o = self.dg.object; |
| 5758 | const mod = o.module; | 6117 | const mod = o.module; |
| 5759 | const inst = body_tail[0]; | 6118 | const inst = body_tail[0]; |
| ... | @@ -5765,13 +6124,15 @@ pub const FuncGen = struct { | ... | @@ -5765,13 +6124,15 @@ pub const FuncGen = struct { |
| 5765 | const array_llvm_ty = try o.lowerType(array_ty); | 6124 | const array_llvm_ty = try o.lowerType(array_ty); |
| 5766 | const elem_ty = array_ty.childType(mod); | 6125 | const elem_ty = array_ty.childType(mod); |
| 5767 | if (isByRef(array_ty, mod)) { | 6126 | if (isByRef(array_ty, mod)) { |
| 5768 | const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs }; | 6127 | const indices: [2]Builder.Value = .{ |
| | 6128 | try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs, |
| | 6129 | }; |
| 5769 | if (isByRef(elem_ty, mod)) { | 6130 | if (isByRef(elem_ty, mod)) { |
| 5770 | const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, ""); | 6131 | const elem_ptr = |
| 5771 | if (canElideLoad(self, body_tail)) | 6132 | try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, ""); |
| 5772 | return elem_ptr; | 6133 | if (canElideLoad(self, body_tail)) return elem_ptr; |
| 5773 | | 6134 | const elem_alignment = Builder.Alignment.fromByteUnits(elem_ty.abiAlignment(mod)); |
| 5774 | return self.loadByRef(elem_ptr, elem_ty, elem_ty.abiAlignment(mod), false); | 6135 | return self.loadByRef(elem_ptr, elem_ty, elem_alignment, false); |
| 5775 | } else { | 6136 | } else { |
| 5776 | const elem_llvm_ty = try o.lowerType(elem_ty); | 6137 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 5777 | if (Air.refToIndex(bin_op.lhs)) |lhs_index| { | 6138 | if (Air.refToIndex(bin_op.lhs)) |lhs_index| { |
| ... | @@ -5781,26 +6142,38 @@ pub const FuncGen = struct { | ... | @@ -5781,26 +6142,38 @@ pub const FuncGen = struct { |
| 5781 | if (Air.refToIndex(load_ptr)) |load_ptr_index| { | 6142 | if (Air.refToIndex(load_ptr)) |load_ptr_index| { |
| 5782 | const load_ptr_tag = self.air.instructions.items(.tag)[load_ptr_index]; | 6143 | const load_ptr_tag = self.air.instructions.items(.tag)[load_ptr_index]; |
| 5783 | switch (load_ptr_tag) { | 6144 | switch (load_ptr_tag) { |
| 5784 | .struct_field_ptr, .struct_field_ptr_index_0, .struct_field_ptr_index_1, .struct_field_ptr_index_2, .struct_field_ptr_index_3 => { | 6145 | .struct_field_ptr, |
| | 6146 | .struct_field_ptr_index_0, |
| | 6147 | .struct_field_ptr_index_1, |
| | 6148 | .struct_field_ptr_index_2, |
| | 6149 | .struct_field_ptr_index_3, |
| | 6150 | => { |
| 5785 | const load_ptr_inst = try self.resolveInst(load_ptr); | 6151 | const load_ptr_inst = try self.resolveInst(load_ptr); |
| 5786 | const gep = self.builder.buildInBoundsGEP(array_llvm_ty, load_ptr_inst, &indices, indices.len, ""); | 6152 | const gep = try self.wip.gep( |
| 5787 | return self.builder.buildLoad(elem_llvm_ty, gep, ""); | 6153 | .inbounds, |
| | 6154 | array_llvm_ty, |
| | 6155 | load_ptr_inst, |
| | 6156 | &indices, |
| | 6157 | "", |
| | 6158 | ); |
| | 6159 | return self.wip.load(.normal, elem_llvm_ty, gep, .default, ""); |
| 5788 | }, | 6160 | }, |
| 5789 | else => {}, | 6161 | else => {}, |
| 5790 | } | 6162 | } |
| 5791 | } | 6163 | } |
| 5792 | } | 6164 | } |
| 5793 | } | 6165 | } |
| 5794 | const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, array_llvm_val, &indices, indices.len, ""); | 6166 | const elem_ptr = |
| 5795 | return self.builder.buildLoad(elem_llvm_ty, elem_ptr, ""); | 6167 | try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, ""); |
| | 6168 | return self.wip.load(.normal, elem_llvm_ty, elem_ptr, .default, ""); |
| 5796 | } | 6169 | } |
| 5797 | } | 6170 | } |
| 5798 | | 6171 | |
| 5799 | // This branch can be reached for vectors, which are always by-value. | 6172 | // This branch can be reached for vectors, which are always by-value. |
| 5800 | return self.builder.buildExtractElement(array_llvm_val, rhs, ""); | 6173 | return self.wip.extractElement(array_llvm_val, rhs, ""); |
| 5801 | } | 6174 | } |
| 5802 | | 6175 | |
| 5803 | fn airPtrElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 6176 | fn airPtrElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 5804 | const o = self.dg.object; | 6177 | const o = self.dg.object; |
| 5805 | const mod = o.module; | 6178 | const mod = o.module; |
| 5806 | const inst = body_tail[0]; | 6179 | const inst = body_tail[0]; |
| ... | @@ -5811,32 +6184,28 @@ pub const FuncGen = struct { | ... | @@ -5811,32 +6184,28 @@ pub const FuncGen = struct { |
| 5811 | const base_ptr = try self.resolveInst(bin_op.lhs); | 6184 | const base_ptr = try self.resolveInst(bin_op.lhs); |
| 5812 | const rhs = try self.resolveInst(bin_op.rhs); | 6185 | const rhs = try self.resolveInst(bin_op.rhs); |
| 5813 | // TODO: when we go fully opaque pointers in LLVM 16 we can remove this branch | 6186 | // TODO: when we go fully opaque pointers in LLVM 16 we can remove this branch |
| 5814 | const ptr = if (ptr_ty.isSinglePointer(mod)) ptr: { | 6187 | const ptr = try self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, if (ptr_ty.isSinglePointer(mod)) |
| 5815 | // If this is a single-item pointer to an array, we need another index in the GEP. | 6188 | // If this is a single-item pointer to an array, we need another index in the GEP. |
| 5816 | const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs }; | 6189 | &.{ try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs } |
| 5817 | break :ptr self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | 6190 | else |
| 5818 | } else ptr: { | 6191 | &.{rhs}, ""); |
| 5819 | const indices: [1]*llvm.Value = .{rhs}; | | |
| 5820 | break :ptr self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | | |
| 5821 | }; | | |
| 5822 | if (isByRef(elem_ty, mod)) { | 6192 | if (isByRef(elem_ty, mod)) { |
| 5823 | if (self.canElideLoad(body_tail)) | 6193 | if (self.canElideLoad(body_tail)) return ptr; |
| 5824 | return ptr; | 6194 | const elem_alignment = Builder.Alignment.fromByteUnits(elem_ty.abiAlignment(mod)); |
| 5825 | | 6195 | return self.loadByRef(ptr, elem_ty, elem_alignment, false); |
| 5826 | return self.loadByRef(ptr, elem_ty, elem_ty.abiAlignment(mod), false); | | |
| 5827 | } | 6196 | } |
| 5828 | | 6197 | |
| 5829 | return self.load(ptr, ptr_ty); | 6198 | return self.load(ptr, ptr_ty); |
| 5830 | } | 6199 | } |
| 5831 | | 6200 | |
| 5832 | fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6201 | fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5833 | const o = self.dg.object; | 6202 | const o = self.dg.object; |
| 5834 | const mod = o.module; | 6203 | const mod = o.module; |
| 5835 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 6204 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 5836 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 6205 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 5837 | const ptr_ty = self.typeOf(bin_op.lhs); | 6206 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 5838 | const elem_ty = ptr_ty.childType(mod); | 6207 | const elem_ty = ptr_ty.childType(mod); |
| 5839 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return o.lowerPtrToVoid(ptr_ty); | 6208 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return (try o.lowerPtrToVoid(ptr_ty)).toValue(); |
| 5840 | | 6209 | |
| 5841 | const base_ptr = try self.resolveInst(bin_op.lhs); | 6210 | const base_ptr = try self.resolveInst(bin_op.lhs); |
| 5842 | const rhs = try self.resolveInst(bin_op.rhs); | 6211 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | @@ -5845,17 +6214,14 @@ pub const FuncGen = struct { | ... | @@ -5845,17 +6214,14 @@ pub const FuncGen = struct { |
| 5845 | if (elem_ptr.ptrInfo(mod).flags.vector_index != .none) return base_ptr; | 6214 | if (elem_ptr.ptrInfo(mod).flags.vector_index != .none) return base_ptr; |
| 5846 | | 6215 | |
| 5847 | const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty); | 6216 | const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty); |
| 5848 | if (ptr_ty.isSinglePointer(mod)) { | 6217 | return try self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, if (ptr_ty.isSinglePointer(mod)) |
| 5849 | // If this is a single-item pointer to an array, we need another index in the GEP. | 6218 | // If this is a single-item pointer to an array, we need another index in the GEP. |
| 5850 | const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), rhs }; | 6219 | &.{ try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs } |
| 5851 | return self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | 6220 | else |
| 5852 | } else { | 6221 | &.{rhs}, ""); |
| 5853 | const indices: [1]*llvm.Value = .{rhs}; | | |
| 5854 | return self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, ""); | | |
| 5855 | } | | |
| 5856 | } | 6222 | } |
| 5857 | | 6223 | |
| 5858 | fn airStructFieldPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6224 | fn airStructFieldPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5859 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 6225 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 5860 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; | 6226 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 5861 | const struct_ptr = try self.resolveInst(struct_field.struct_operand); | 6227 | const struct_ptr = try self.resolveInst(struct_field.struct_operand); |
| ... | @@ -5867,14 +6233,14 @@ pub const FuncGen = struct { | ... | @@ -5867,14 +6233,14 @@ pub const FuncGen = struct { |
| 5867 | self: *FuncGen, | 6233 | self: *FuncGen, |
| 5868 | inst: Air.Inst.Index, | 6234 | inst: Air.Inst.Index, |
| 5869 | field_index: u32, | 6235 | field_index: u32, |
| 5870 | ) !?*llvm.Value { | 6236 | ) !Builder.Value { |
| 5871 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 6237 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5872 | const struct_ptr = try self.resolveInst(ty_op.operand); | 6238 | const struct_ptr = try self.resolveInst(ty_op.operand); |
| 5873 | const struct_ptr_ty = self.typeOf(ty_op.operand); | 6239 | const struct_ptr_ty = self.typeOf(ty_op.operand); |
| 5874 | return self.fieldPtr(inst, struct_ptr, struct_ptr_ty, field_index); | 6240 | return self.fieldPtr(inst, struct_ptr, struct_ptr_ty, field_index); |
| 5875 | } | 6241 | } |
| 5876 | | 6242 | |
| 5877 | fn airStructFieldVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 6243 | fn airStructFieldVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 5878 | const o = self.dg.object; | 6244 | const o = self.dg.object; |
| 5879 | const mod = o.module; | 6245 | const mod = o.module; |
| 5880 | const inst = body_tail[0]; | 6246 | const inst = body_tail[0]; |
| ... | @@ -5884,9 +6250,7 @@ pub const FuncGen = struct { | ... | @@ -5884,9 +6250,7 @@ pub const FuncGen = struct { |
| 5884 | const struct_llvm_val = try self.resolveInst(struct_field.struct_operand); | 6250 | const struct_llvm_val = try self.resolveInst(struct_field.struct_operand); |
| 5885 | const field_index = struct_field.field_index; | 6251 | const field_index = struct_field.field_index; |
| 5886 | const field_ty = struct_ty.structFieldType(field_index, mod); | 6252 | const field_ty = struct_ty.structFieldType(field_index, mod); |
| 5887 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 6253 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; |
| 5888 | return null; | | |
| 5889 | } | | |
| 5890 | | 6254 | |
| 5891 | if (!isByRef(struct_ty, mod)) { | 6255 | if (!isByRef(struct_ty, mod)) { |
| 5892 | assert(!isByRef(field_ty, mod)); | 6256 | assert(!isByRef(field_ty, mod)); |
| ... | @@ -5896,25 +6260,26 @@ pub const FuncGen = struct { | ... | @@ -5896,25 +6260,26 @@ pub const FuncGen = struct { |
| 5896 | const struct_obj = mod.typeToStruct(struct_ty).?; | 6260 | const struct_obj = mod.typeToStruct(struct_ty).?; |
| 5897 | const bit_offset = struct_obj.packedFieldBitOffset(mod, field_index); | 6261 | const bit_offset = struct_obj.packedFieldBitOffset(mod, field_index); |
| 5898 | const containing_int = struct_llvm_val; | 6262 | const containing_int = struct_llvm_val; |
| 5899 | const shift_amt = containing_int.typeOf().constInt(bit_offset, .False); | 6263 | const shift_amt = |
| 5900 | const shifted_value = self.builder.buildLShr(containing_int, shift_amt, ""); | 6264 | try o.builder.intValue(containing_int.typeOfWip(&self.wip), bit_offset); |
| | 6265 | const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, ""); |
| 5901 | const elem_llvm_ty = try o.lowerType(field_ty); | 6266 | const elem_llvm_ty = try o.lowerType(field_ty); |
| 5902 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { | 6267 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { |
| 5903 | const elem_bits = @as(c_uint, @intCast(field_ty.bitSize(mod))); | 6268 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); |
| 5904 | const same_size_int = self.context.intType(elem_bits); | 6269 | const truncated_int = |
| 5905 | const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, ""); | 6270 | try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 5906 | return self.builder.buildBitCast(truncated_int, elem_llvm_ty, ""); | 6271 | return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, ""); |
| 5907 | } else if (field_ty.isPtrAtRuntime(mod)) { | 6272 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 5908 | const elem_bits = @as(c_uint, @intCast(field_ty.bitSize(mod))); | 6273 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); |
| 5909 | const same_size_int = self.context.intType(elem_bits); | 6274 | const truncated_int = |
| 5910 | const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, ""); | 6275 | try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 5911 | return self.builder.buildIntToPtr(truncated_int, elem_llvm_ty, ""); | 6276 | return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, ""); |
| 5912 | } | 6277 | } |
| 5913 | return self.builder.buildTrunc(shifted_value, elem_llvm_ty, ""); | 6278 | return self.wip.cast(.trunc, shifted_value, elem_llvm_ty, ""); |
| 5914 | }, | 6279 | }, |
| 5915 | else => { | 6280 | else => { |
| 5916 | const llvm_field_index = llvmField(struct_ty, field_index, mod).?.index; | 6281 | const llvm_field_index = llvmField(struct_ty, field_index, mod).?.index; |
| 5917 | return self.builder.buildExtractValue(struct_llvm_val, llvm_field_index, ""); | 6282 | return self.wip.extractValue(struct_llvm_val, &.{llvm_field_index}, ""); |
| 5918 | }, | 6283 | }, |
| 5919 | }, | 6284 | }, |
| 5920 | .Union => { | 6285 | .Union => { |
| ... | @@ -5922,17 +6287,17 @@ pub const FuncGen = struct { | ... | @@ -5922,17 +6287,17 @@ pub const FuncGen = struct { |
| 5922 | const containing_int = struct_llvm_val; | 6287 | const containing_int = struct_llvm_val; |
| 5923 | const elem_llvm_ty = try o.lowerType(field_ty); | 6288 | const elem_llvm_ty = try o.lowerType(field_ty); |
| 5924 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { | 6289 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { |
| 5925 | const elem_bits = @as(c_uint, @intCast(field_ty.bitSize(mod))); | 6290 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); |
| 5926 | const same_size_int = self.context.intType(elem_bits); | 6291 | const truncated_int = |
| 5927 | const truncated_int = self.builder.buildTrunc(containing_int, same_size_int, ""); | 6292 | try self.wip.cast(.trunc, containing_int, same_size_int, ""); |
| 5928 | return self.builder.buildBitCast(truncated_int, elem_llvm_ty, ""); | 6293 | return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, ""); |
| 5929 | } else if (field_ty.isPtrAtRuntime(mod)) { | 6294 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 5930 | const elem_bits = @as(c_uint, @intCast(field_ty.bitSize(mod))); | 6295 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); |
| 5931 | const same_size_int = self.context.intType(elem_bits); | 6296 | const truncated_int = |
| 5932 | const truncated_int = self.builder.buildTrunc(containing_int, same_size_int, ""); | 6297 | try self.wip.cast(.trunc, containing_int, same_size_int, ""); |
| 5933 | return self.builder.buildIntToPtr(truncated_int, elem_llvm_ty, ""); | 6298 | return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, ""); |
| 5934 | } | 6299 | } |
| 5935 | return self.builder.buildTrunc(containing_int, elem_llvm_ty, ""); | 6300 | return self.wip.cast(.trunc, containing_int, elem_llvm_ty, ""); |
| 5936 | }, | 6301 | }, |
| 5937 | else => unreachable, | 6302 | else => unreachable, |
| 5938 | } | 6303 | } |
| ... | @@ -5943,7 +6308,8 @@ pub const FuncGen = struct { | ... | @@ -5943,7 +6308,8 @@ pub const FuncGen = struct { |
| 5943 | assert(struct_ty.containerLayout(mod) != .Packed); | 6308 | assert(struct_ty.containerLayout(mod) != .Packed); |
| 5944 | const llvm_field = llvmField(struct_ty, field_index, mod).?; | 6309 | const llvm_field = llvmField(struct_ty, field_index, mod).?; |
| 5945 | const struct_llvm_ty = try o.lowerType(struct_ty); | 6310 | const struct_llvm_ty = try o.lowerType(struct_ty); |
| 5946 | const field_ptr = self.builder.buildStructGEP(struct_llvm_ty, struct_llvm_val, llvm_field.index, ""); | 6311 | const field_ptr = |
| | 6312 | try self.wip.gepStruct(struct_llvm_ty, struct_llvm_val, llvm_field.index, ""); |
| 5947 | const field_ptr_ty = try mod.ptrType(.{ | 6313 | const field_ptr_ty = try mod.ptrType(.{ |
| 5948 | .child = llvm_field.ty.toIntern(), | 6314 | .child = llvm_field.ty.toIntern(), |
| 5949 | .flags = .{ | 6315 | .flags = .{ |
| ... | @@ -5955,7 +6321,8 @@ pub const FuncGen = struct { | ... | @@ -5955,7 +6321,8 @@ pub const FuncGen = struct { |
| 5955 | return field_ptr; | 6321 | return field_ptr; |
| 5956 | | 6322 | |
| 5957 | assert(llvm_field.alignment != 0); | 6323 | assert(llvm_field.alignment != 0); |
| 5958 | return self.loadByRef(field_ptr, field_ty, llvm_field.alignment, false); | 6324 | const field_alignment = Builder.Alignment.fromByteUnits(llvm_field.alignment); |
| | 6325 | return self.loadByRef(field_ptr, field_ty, field_alignment, false); |
| 5959 | } else { | 6326 | } else { |
| 5960 | return self.load(field_ptr, field_ptr_ty); | 6327 | return self.load(field_ptr, field_ptr_ty); |
| 5961 | } | 6328 | } |
| ... | @@ -5964,22 +6331,22 @@ pub const FuncGen = struct { | ... | @@ -5964,22 +6331,22 @@ pub const FuncGen = struct { |
| 5964 | const union_llvm_ty = try o.lowerType(struct_ty); | 6331 | const union_llvm_ty = try o.lowerType(struct_ty); |
| 5965 | const layout = struct_ty.unionGetLayout(mod); | 6332 | const layout = struct_ty.unionGetLayout(mod); |
| 5966 | const payload_index = @intFromBool(layout.tag_align >= layout.payload_align); | 6333 | const payload_index = @intFromBool(layout.tag_align >= layout.payload_align); |
| 5967 | const field_ptr = self.builder.buildStructGEP(union_llvm_ty, struct_llvm_val, payload_index, ""); | 6334 | const field_ptr = |
| | 6335 | try self.wip.gepStruct(union_llvm_ty, struct_llvm_val, payload_index, ""); |
| 5968 | const llvm_field_ty = try o.lowerType(field_ty); | 6336 | const llvm_field_ty = try o.lowerType(field_ty); |
| | 6337 | const payload_alignment = Builder.Alignment.fromByteUnits(layout.payload_align); |
| 5969 | if (isByRef(field_ty, mod)) { | 6338 | if (isByRef(field_ty, mod)) { |
| 5970 | if (canElideLoad(self, body_tail)) | 6339 | if (canElideLoad(self, body_tail)) return field_ptr; |
| 5971 | return field_ptr; | 6340 | return self.loadByRef(field_ptr, field_ty, payload_alignment, false); |
| 5972 | | | |
| 5973 | return self.loadByRef(field_ptr, field_ty, layout.payload_align, false); | | |
| 5974 | } else { | 6341 | } else { |
| 5975 | return self.builder.buildLoad(llvm_field_ty, field_ptr, ""); | 6342 | return self.wip.load(.normal, llvm_field_ty, field_ptr, payload_alignment, ""); |
| 5976 | } | 6343 | } |
| 5977 | }, | 6344 | }, |
| 5978 | else => unreachable, | 6345 | else => unreachable, |
| 5979 | } | 6346 | } |
| 5980 | } | 6347 | } |
| 5981 | | 6348 | |
| 5982 | fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6349 | fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5983 | const o = self.dg.object; | 6350 | const o = self.dg.object; |
| 5984 | const mod = o.module; | 6351 | const mod = o.module; |
| 5985 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 6352 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -5987,50 +6354,52 @@ pub const FuncGen = struct { | ... | @@ -5987,50 +6354,52 @@ pub const FuncGen = struct { |
| 5987 | | 6354 | |
| 5988 | const field_ptr = try self.resolveInst(extra.field_ptr); | 6355 | const field_ptr = try self.resolveInst(extra.field_ptr); |
| 5989 | | 6356 | |
| 5990 | const target = o.module.getTarget(); | | |
| 5991 | const parent_ty = self.air.getRefType(ty_pl.ty).childType(mod); | 6357 | const parent_ty = self.air.getRefType(ty_pl.ty).childType(mod); |
| 5992 | const field_offset = parent_ty.structFieldOffset(extra.field_index, mod); | 6358 | const field_offset = parent_ty.structFieldOffset(extra.field_index, mod); |
| | 6359 | if (field_offset == 0) return field_ptr; |
| 5993 | | 6360 | |
| 5994 | const res_ty = try o.lowerType(self.air.getRefType(ty_pl.ty)); | 6361 | const res_ty = try o.lowerType(self.air.getRefType(ty_pl.ty)); |
| 5995 | if (field_offset == 0) { | 6362 | const llvm_usize = try o.lowerType(Type.usize); |
| 5996 | return field_ptr; | | |
| 5997 | } | | |
| 5998 | const llvm_usize_ty = self.context.intType(target.ptrBitWidth()); | | |
| 5999 | | 6363 | |
| 6000 | const field_ptr_int = self.builder.buildPtrToInt(field_ptr, llvm_usize_ty, ""); | 6364 | const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, ""); |
| 6001 | const base_ptr_int = self.builder.buildNUWSub(field_ptr_int, llvm_usize_ty.constInt(field_offset, .False), ""); | 6365 | const base_ptr_int = try self.wip.bin( |
| 6002 | return self.builder.buildIntToPtr(base_ptr_int, res_ty, ""); | 6366 | .@"sub nuw", |
| | 6367 | field_ptr_int, |
| | 6368 | try o.builder.intValue(llvm_usize, field_offset), |
| | 6369 | "", |
| | 6370 | ); |
| | 6371 | return self.wip.cast(.inttoptr, base_ptr_int, res_ty, ""); |
| 6003 | } | 6372 | } |
| 6004 | | 6373 | |
| 6005 | fn airNot(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6374 | fn airNot(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6006 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 6375 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6007 | const operand = try self.resolveInst(ty_op.operand); | 6376 | const operand = try self.resolveInst(ty_op.operand); |
| 6008 | | 6377 | |
| 6009 | return self.builder.buildNot(operand, ""); | 6378 | return self.wip.not(operand, ""); |
| 6010 | } | 6379 | } |
| 6011 | | 6380 | |
| 6012 | fn airUnreach(self: *FuncGen, inst: Air.Inst.Index) ?*llvm.Value { | 6381 | fn airUnreach(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6013 | _ = inst; | 6382 | _ = inst; |
| 6014 | _ = self.builder.buildUnreachable(); | 6383 | _ = try self.wip.@"unreachable"(); |
| 6015 | return null; | 6384 | return .none; |
| 6016 | } | 6385 | } |
| 6017 | | 6386 | |
| 6018 | fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) ?*llvm.Value { | 6387 | fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6019 | const di_scope = self.di_scope orelse return null; | 6388 | const di_scope = self.di_scope orelse return .none; |
| 6020 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; | 6389 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; |
| 6021 | self.prev_dbg_line = @as(c_uint, @intCast(self.base_line + dbg_stmt.line + 1)); | 6390 | self.prev_dbg_line = @intCast(self.base_line + dbg_stmt.line + 1); |
| 6022 | self.prev_dbg_column = @as(c_uint, @intCast(dbg_stmt.column + 1)); | 6391 | self.prev_dbg_column = @intCast(dbg_stmt.column + 1); |
| 6023 | const inlined_at = if (self.dbg_inlined.items.len > 0) | 6392 | const inlined_at = if (self.dbg_inlined.items.len > 0) |
| 6024 | self.dbg_inlined.items[self.dbg_inlined.items.len - 1].loc | 6393 | self.dbg_inlined.items[self.dbg_inlined.items.len - 1].loc |
| 6025 | else | 6394 | else |
| 6026 | null; | 6395 | null; |
| 6027 | self.builder.setCurrentDebugLocation(self.prev_dbg_line, self.prev_dbg_column, di_scope, inlined_at); | 6396 | self.builder.setCurrentDebugLocation(self.prev_dbg_line, self.prev_dbg_column, di_scope, inlined_at); |
| 6028 | return null; | 6397 | return .none; |
| 6029 | } | 6398 | } |
| 6030 | | 6399 | |
| 6031 | fn airDbgInlineBegin(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6400 | fn airDbgInlineBegin(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6032 | const o = self.dg.object; | 6401 | const o = self.dg.object; |
| 6033 | const dib = o.di_builder orelse return null; | 6402 | const dib = o.di_builder orelse return .none; |
| 6034 | const ty_fn = self.air.instructions.items(.data)[inst].ty_fn; | 6403 | const ty_fn = self.air.instructions.items(.data)[inst].ty_fn; |
| 6035 | | 6404 | |
| 6036 | const mod = o.module; | 6405 | const mod = o.module; |
| ... | @@ -6083,12 +6452,12 @@ pub const FuncGen = struct { | ... | @@ -6083,12 +6452,12 @@ pub const FuncGen = struct { |
| 6083 | const lexical_block = dib.createLexicalBlock(subprogram.toScope(), di_file, line_number, 1); | 6452 | const lexical_block = dib.createLexicalBlock(subprogram.toScope(), di_file, line_number, 1); |
| 6084 | self.di_scope = lexical_block.toScope(); | 6453 | self.di_scope = lexical_block.toScope(); |
| 6085 | self.base_line = decl.src_line; | 6454 | self.base_line = decl.src_line; |
| 6086 | return null; | 6455 | return .none; |
| 6087 | } | 6456 | } |
| 6088 | | 6457 | |
| 6089 | fn airDbgInlineEnd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6458 | fn airDbgInlineEnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6090 | const o = self.dg.object; | 6459 | const o = self.dg.object; |
| 6091 | if (o.di_builder == null) return null; | 6460 | if (o.di_builder == null) return .none; |
| 6092 | const ty_fn = self.air.instructions.items(.data)[inst].ty_fn; | 6461 | const ty_fn = self.air.instructions.items(.data)[inst].ty_fn; |
| 6093 | | 6462 | |
| 6094 | const mod = o.module; | 6463 | const mod = o.module; |
| ... | @@ -6098,30 +6467,30 @@ pub const FuncGen = struct { | ... | @@ -6098,30 +6467,30 @@ pub const FuncGen = struct { |
| 6098 | const old = self.dbg_inlined.pop(); | 6467 | const old = self.dbg_inlined.pop(); |
| 6099 | self.di_scope = old.scope; | 6468 | self.di_scope = old.scope; |
| 6100 | self.base_line = old.base_line; | 6469 | self.base_line = old.base_line; |
| 6101 | return null; | 6470 | return .none; |
| 6102 | } | 6471 | } |
| 6103 | | 6472 | |
| 6104 | fn airDbgBlockBegin(self: *FuncGen) !?*llvm.Value { | 6473 | fn airDbgBlockBegin(self: *FuncGen) !Builder.Value { |
| 6105 | const o = self.dg.object; | 6474 | const o = self.dg.object; |
| 6106 | const dib = o.di_builder orelse return null; | 6475 | const dib = o.di_builder orelse return .none; |
| 6107 | const old_scope = self.di_scope.?; | 6476 | const old_scope = self.di_scope.?; |
| 6108 | try self.dbg_block_stack.append(self.gpa, old_scope); | 6477 | try self.dbg_block_stack.append(self.gpa, old_scope); |
| 6109 | const lexical_block = dib.createLexicalBlock(old_scope, self.di_file.?, self.prev_dbg_line, self.prev_dbg_column); | 6478 | const lexical_block = dib.createLexicalBlock(old_scope, self.di_file.?, self.prev_dbg_line, self.prev_dbg_column); |
| 6110 | self.di_scope = lexical_block.toScope(); | 6479 | self.di_scope = lexical_block.toScope(); |
| 6111 | return null; | 6480 | return .none; |
| 6112 | } | 6481 | } |
| 6113 | | 6482 | |
| 6114 | fn airDbgBlockEnd(self: *FuncGen) !?*llvm.Value { | 6483 | fn airDbgBlockEnd(self: *FuncGen) !Builder.Value { |
| 6115 | const o = self.dg.object; | 6484 | const o = self.dg.object; |
| 6116 | if (o.di_builder == null) return null; | 6485 | if (o.di_builder == null) return .none; |
| 6117 | self.di_scope = self.dbg_block_stack.pop(); | 6486 | self.di_scope = self.dbg_block_stack.pop(); |
| 6118 | return null; | 6487 | return .none; |
| 6119 | } | 6488 | } |
| 6120 | | 6489 | |
| 6121 | fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6490 | fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6122 | const o = self.dg.object; | 6491 | const o = self.dg.object; |
| 6123 | const mod = o.module; | 6492 | const mod = o.module; |
| 6124 | const dib = o.di_builder orelse return null; | 6493 | const dib = o.di_builder orelse return .none; |
| 6125 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 6494 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 6126 | const operand = try self.resolveInst(pl_op.operand); | 6495 | const operand = try self.resolveInst(pl_op.operand); |
| 6127 | const name = self.air.nullTerminatedString(pl_op.payload); | 6496 | const name = self.air.nullTerminatedString(pl_op.payload); |
| ... | @@ -6141,22 +6510,20 @@ pub const FuncGen = struct { | ... | @@ -6141,22 +6510,20 @@ pub const FuncGen = struct { |
| 6141 | else | 6510 | else |
| 6142 | null; | 6511 | null; |
| 6143 | const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?, inlined_at); | 6512 | const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?, inlined_at); |
| 6144 | const insert_block = self.builder.getInsertBlock(); | 6513 | const insert_block = self.wip.cursor.block.toLlvm(&self.wip); |
| 6145 | _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block); | 6514 | _ = dib.insertDeclareAtEnd(operand.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 6146 | return null; | 6515 | return .none; |
| 6147 | } | 6516 | } |
| 6148 | | 6517 | |
| 6149 | fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6518 | fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6150 | const o = self.dg.object; | 6519 | const o = self.dg.object; |
| 6151 | const dib = o.di_builder orelse return null; | 6520 | const dib = o.di_builder orelse return .none; |
| 6152 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 6521 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 6153 | const operand = try self.resolveInst(pl_op.operand); | 6522 | const operand = try self.resolveInst(pl_op.operand); |
| 6154 | const operand_ty = self.typeOf(pl_op.operand); | 6523 | const operand_ty = self.typeOf(pl_op.operand); |
| 6155 | const name = self.air.nullTerminatedString(pl_op.payload); | 6524 | const name = self.air.nullTerminatedString(pl_op.payload); |
| 6156 | | 6525 | |
| 6157 | if (needDbgVarWorkaround(o)) { | 6526 | if (needDbgVarWorkaround(o)) return .none; |
| 6158 | return null; | | |
| 6159 | } | | |
| 6160 | | 6527 | |
| 6161 | const di_local_var = dib.createAutoVariable( | 6528 | const di_local_var = dib.createAutoVariable( |
| 6162 | self.di_scope.?, | 6529 | self.di_scope.?, |
| ... | @@ -6172,23 +6539,22 @@ pub const FuncGen = struct { | ... | @@ -6172,23 +6539,22 @@ pub const FuncGen = struct { |
| 6172 | else | 6539 | else |
| 6173 | null; | 6540 | null; |
| 6174 | const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?, inlined_at); | 6541 | const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?, inlined_at); |
| 6175 | const insert_block = self.builder.getInsertBlock(); | 6542 | const insert_block = self.wip.cursor.block.toLlvm(&self.wip); |
| 6176 | const mod = o.module; | 6543 | const mod = o.module; |
| 6177 | if (isByRef(operand_ty, mod)) { | 6544 | if (isByRef(operand_ty, mod)) { |
| 6178 | _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block); | 6545 | _ = dib.insertDeclareAtEnd(operand.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 6179 | } else if (o.module.comp.bin_file.options.optimize_mode == .Debug) { | 6546 | } else if (o.module.comp.bin_file.options.optimize_mode == .Debug) { |
| 6180 | const alignment = operand_ty.abiAlignment(mod); | 6547 | const alignment = Builder.Alignment.fromByteUnits(operand_ty.abiAlignment(mod)); |
| 6181 | const alloca = self.buildAlloca(operand.typeOf(), alignment); | 6548 | const alloca = try self.buildAlloca(operand.typeOfWip(&self.wip), alignment); |
| 6182 | const store_inst = self.builder.buildStore(operand, alloca); | 6549 | _ = try self.wip.store(.normal, operand, alloca, alignment); |
| 6183 | store_inst.setAlignment(alignment); | 6550 | _ = dib.insertDeclareAtEnd(alloca.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 6184 | _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block); | | |
| 6185 | } else { | 6551 | } else { |
| 6186 | _ = dib.insertDbgValueIntrinsicAtEnd(operand, di_local_var, debug_loc, insert_block); | 6552 | _ = dib.insertDbgValueIntrinsicAtEnd(operand.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 6187 | } | 6553 | } |
| 6188 | return null; | 6554 | return .none; |
| 6189 | } | 6555 | } |
| 6190 | | 6556 | |
| 6191 | fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6557 | fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6192 | // Eventually, the Zig compiler needs to be reworked to have inline | 6558 | // Eventually, the Zig compiler needs to be reworked to have inline |
| 6193 | // assembly go through the same parsing code regardless of backend, and | 6559 | // assembly go through the same parsing code regardless of backend, and |
| 6194 | // have LLVM-flavored inline assembly be *output* from that assembler. | 6560 | // have LLVM-flavored inline assembly be *output* from that assembler. |
| ... | @@ -6199,12 +6565,12 @@ pub const FuncGen = struct { | ... | @@ -6199,12 +6565,12 @@ pub const FuncGen = struct { |
| 6199 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 6565 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 6200 | const extra = self.air.extraData(Air.Asm, ty_pl.payload); | 6566 | const extra = self.air.extraData(Air.Asm, ty_pl.payload); |
| 6201 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; | 6567 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| 6202 | const clobbers_len = @as(u31, @truncate(extra.data.flags)); | 6568 | const clobbers_len: u31 = @truncate(extra.data.flags); |
| 6203 | var extra_i: usize = extra.end; | 6569 | var extra_i: usize = extra.end; |
| 6204 | | 6570 | |
| 6205 | const outputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len])); | 6571 | const outputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 6206 | extra_i += outputs.len; | 6572 | extra_i += outputs.len; |
| 6207 | const inputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len])); | 6573 | const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 6208 | extra_i += inputs.len; | 6574 | extra_i += inputs.len; |
| 6209 | | 6575 | |
| 6210 | var llvm_constraints: std.ArrayListUnmanaged(u8) = .{}; | 6576 | var llvm_constraints: std.ArrayListUnmanaged(u8) = .{}; |
| ... | @@ -6217,15 +6583,15 @@ pub const FuncGen = struct { | ... | @@ -6217,15 +6583,15 @@ pub const FuncGen = struct { |
| 6217 | // The exact number of return / parameter values depends on which output values | 6583 | // The exact number of return / parameter values depends on which output values |
| 6218 | // are passed by reference as indirect outputs (determined below). | 6584 | // are passed by reference as indirect outputs (determined below). |
| 6219 | const max_return_count = outputs.len; | 6585 | const max_return_count = outputs.len; |
| 6220 | const llvm_ret_types = try arena.alloc(*llvm.Type, max_return_count); | 6586 | const llvm_ret_types = try arena.alloc(Builder.Type, max_return_count); |
| 6221 | const llvm_ret_indirect = try arena.alloc(bool, max_return_count); | 6587 | const llvm_ret_indirect = try arena.alloc(bool, max_return_count); |
| 6222 | | 6588 | |
| 6223 | const max_param_count = inputs.len + outputs.len; | 6589 | const max_param_count = inputs.len + outputs.len; |
| 6224 | const llvm_param_types = try arena.alloc(*llvm.Type, max_param_count); | 6590 | const llvm_param_types = try arena.alloc(Builder.Type, max_param_count); |
| 6225 | const llvm_param_values = try arena.alloc(*llvm.Value, max_param_count); | 6591 | const llvm_param_values = try arena.alloc(*llvm.Value, max_param_count); |
| 6226 | // This stores whether we need to add an elementtype attribute and | 6592 | // This stores whether we need to add an elementtype attribute and |
| 6227 | // if so, the element type itself. | 6593 | // if so, the element type itself. |
| 6228 | const llvm_param_attrs = try arena.alloc(?*llvm.Type, max_param_count); | 6594 | const llvm_param_attrs = try arena.alloc(Builder.Type, max_param_count); |
| 6229 | const mod = o.module; | 6595 | const mod = o.module; |
| 6230 | const target = mod.getTarget(); | 6596 | const target = mod.getTarget(); |
| 6231 | | 6597 | |
| ... | @@ -6262,8 +6628,8 @@ pub const FuncGen = struct { | ... | @@ -6262,8 +6628,8 @@ pub const FuncGen = struct { |
| 6262 | // Pass the result by reference as an indirect output (e.g. "=*m") | 6628 | // Pass the result by reference as an indirect output (e.g. "=*m") |
| 6263 | llvm_constraints.appendAssumeCapacity('*'); | 6629 | llvm_constraints.appendAssumeCapacity('*'); |
| 6264 | | 6630 | |
| 6265 | llvm_param_values[llvm_param_i] = output_inst; | 6631 | llvm_param_values[llvm_param_i] = output_inst.toLlvm(&self.wip); |
| 6266 | llvm_param_types[llvm_param_i] = output_inst.typeOf(); | 6632 | llvm_param_types[llvm_param_i] = output_inst.typeOfWip(&self.wip); |
| 6267 | llvm_param_attrs[llvm_param_i] = elem_llvm_ty; | 6633 | llvm_param_attrs[llvm_param_i] = elem_llvm_ty; |
| 6268 | llvm_param_i += 1; | 6634 | llvm_param_i += 1; |
| 6269 | } else { | 6635 | } else { |
| ... | @@ -6308,31 +6674,30 @@ pub const FuncGen = struct { | ... | @@ -6308,31 +6674,30 @@ pub const FuncGen = struct { |
| 6308 | | 6674 | |
| 6309 | const arg_llvm_value = try self.resolveInst(input); | 6675 | const arg_llvm_value = try self.resolveInst(input); |
| 6310 | const arg_ty = self.typeOf(input); | 6676 | const arg_ty = self.typeOf(input); |
| 6311 | var llvm_elem_ty: ?*llvm.Type = null; | 6677 | var llvm_elem_ty: Builder.Type = .none; |
| 6312 | if (isByRef(arg_ty, mod)) { | 6678 | if (isByRef(arg_ty, mod)) { |
| 6313 | llvm_elem_ty = try o.lowerPtrElemTy(arg_ty); | 6679 | llvm_elem_ty = try o.lowerPtrElemTy(arg_ty); |
| 6314 | if (constraintAllowsMemory(constraint)) { | 6680 | if (constraintAllowsMemory(constraint)) { |
| 6315 | llvm_param_values[llvm_param_i] = arg_llvm_value; | 6681 | llvm_param_values[llvm_param_i] = arg_llvm_value.toLlvm(&self.wip); |
| 6316 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOf(); | 6682 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip); |
| 6317 | } else { | 6683 | } else { |
| 6318 | const alignment = arg_ty.abiAlignment(mod); | 6684 | const alignment = Builder.Alignment.fromByteUnits(arg_ty.abiAlignment(mod)); |
| 6319 | const arg_llvm_ty = try o.lowerType(arg_ty); | 6685 | const arg_llvm_ty = try o.lowerType(arg_ty); |
| 6320 | const load_inst = self.builder.buildLoad(arg_llvm_ty, arg_llvm_value, ""); | 6686 | const load_inst = |
| 6321 | load_inst.setAlignment(alignment); | 6687 | try self.wip.load(.normal, arg_llvm_ty, arg_llvm_value, alignment, ""); |
| 6322 | llvm_param_values[llvm_param_i] = load_inst; | 6688 | llvm_param_values[llvm_param_i] = load_inst.toLlvm(&self.wip); |
| 6323 | llvm_param_types[llvm_param_i] = arg_llvm_ty; | 6689 | llvm_param_types[llvm_param_i] = arg_llvm_ty; |
| 6324 | } | 6690 | } |
| 6325 | } else { | 6691 | } else { |
| 6326 | if (constraintAllowsRegister(constraint)) { | 6692 | if (constraintAllowsRegister(constraint)) { |
| 6327 | llvm_param_values[llvm_param_i] = arg_llvm_value; | 6693 | llvm_param_values[llvm_param_i] = arg_llvm_value.toLlvm(&self.wip); |
| 6328 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOf(); | 6694 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip); |
| 6329 | } else { | 6695 | } else { |
| 6330 | const alignment = arg_ty.abiAlignment(mod); | 6696 | const alignment = Builder.Alignment.fromByteUnits(arg_ty.abiAlignment(mod)); |
| 6331 | const arg_ptr = self.buildAlloca(arg_llvm_value.typeOf(), alignment); | 6697 | const arg_ptr = try self.buildAlloca(arg_llvm_value.typeOfWip(&self.wip), alignment); |
| 6332 | const store_inst = self.builder.buildStore(arg_llvm_value, arg_ptr); | 6698 | _ = try self.wip.store(.normal, arg_llvm_value, arg_ptr, alignment); |
| 6333 | store_inst.setAlignment(alignment); | 6699 | llvm_param_values[llvm_param_i] = arg_ptr.toLlvm(&self.wip); |
| 6334 | llvm_param_values[llvm_param_i] = arg_ptr; | 6700 | llvm_param_types[llvm_param_i] = arg_ptr.typeOfWip(&self.wip); |
| 6335 | llvm_param_types[llvm_param_i] = arg_ptr.typeOf(); | | |
| 6336 | } | 6701 | } |
| 6337 | } | 6702 | } |
| 6338 | | 6703 | |
| ... | @@ -6356,10 +6721,12 @@ pub const FuncGen = struct { | ... | @@ -6356,10 +6721,12 @@ pub const FuncGen = struct { |
| 6356 | // In the case of indirect inputs, LLVM requires the callsite to have | 6721 | // In the case of indirect inputs, LLVM requires the callsite to have |
| 6357 | // an elementtype(<ty>) attribute. | 6722 | // an elementtype(<ty>) attribute. |
| 6358 | if (constraint[0] == '*') { | 6723 | if (constraint[0] == '*') { |
| 6359 | llvm_param_attrs[llvm_param_i] = llvm_elem_ty orelse | 6724 | llvm_param_attrs[llvm_param_i] = if (llvm_elem_ty != .none) |
| | 6725 | llvm_elem_ty |
| | 6726 | else |
| 6360 | try o.lowerPtrElemTy(arg_ty.childType(mod)); | 6727 | try o.lowerPtrElemTy(arg_ty.childType(mod)); |
| 6361 | } else { | 6728 | } else { |
| 6362 | llvm_param_attrs[llvm_param_i] = null; | 6729 | llvm_param_attrs[llvm_param_i] = .none; |
| 6363 | } | 6730 | } |
| 6364 | | 6731 | |
| 6365 | llvm_param_i += 1; | 6732 | llvm_param_i += 1; |
| ... | @@ -6477,23 +6844,14 @@ pub const FuncGen = struct { | ... | @@ -6477,23 +6844,14 @@ pub const FuncGen = struct { |
| 6477 | } | 6844 | } |
| 6478 | | 6845 | |
| 6479 | const ret_llvm_ty = switch (return_count) { | 6846 | const ret_llvm_ty = switch (return_count) { |
| 6480 | 0 => self.context.voidType(), | 6847 | 0 => .void, |
| 6481 | 1 => llvm_ret_types[0], | 6848 | 1 => llvm_ret_types[0], |
| 6482 | else => self.context.structType( | 6849 | else => try o.builder.structType(.normal, llvm_ret_types), |
| 6483 | llvm_ret_types.ptr, | | |
| 6484 | @as(c_uint, @intCast(return_count)), | | |
| 6485 | .False, | | |
| 6486 | ), | | |
| 6487 | }; | 6850 | }; |
| 6488 | | 6851 | |
| 6489 | const llvm_fn_ty = llvm.functionType( | 6852 | const llvm_fn_ty = try o.builder.fnType(ret_llvm_ty, llvm_param_types[0..param_count], .normal); |
| 6490 | ret_llvm_ty, | | |
| 6491 | llvm_param_types.ptr, | | |
| 6492 | @as(c_uint, @intCast(param_count)), | | |
| 6493 | .False, | | |
| 6494 | ); | | |
| 6495 | const asm_fn = llvm.getInlineAsm( | 6853 | const asm_fn = llvm.getInlineAsm( |
| 6496 | llvm_fn_ty, | 6854 | llvm_fn_ty.toLlvm(&o.builder), |
| 6497 | rendered_template.items.ptr, | 6855 | rendered_template.items.ptr, |
| 6498 | rendered_template.items.len, | 6856 | rendered_template.items.len, |
| 6499 | llvm_constraints.items.ptr, | 6857 | llvm_constraints.items.ptr, |
| ... | @@ -6503,18 +6861,18 @@ pub const FuncGen = struct { | ... | @@ -6503,18 +6861,18 @@ pub const FuncGen = struct { |
| 6503 | .ATT, | 6861 | .ATT, |
| 6504 | .False, | 6862 | .False, |
| 6505 | ); | 6863 | ); |
| 6506 | const call = self.builder.buildCall( | 6864 | const call = (try self.wip.unimplemented(ret_llvm_ty, "")).finish(self.builder.buildCall( |
| 6507 | llvm_fn_ty, | 6865 | llvm_fn_ty.toLlvm(&o.builder), |
| 6508 | asm_fn, | 6866 | asm_fn, |
| 6509 | llvm_param_values.ptr, | 6867 | llvm_param_values.ptr, |
| 6510 | @as(c_uint, @intCast(param_count)), | 6868 | @intCast(param_count), |
| 6511 | .C, | 6869 | .C, |
| 6512 | .Auto, | 6870 | .Auto, |
| 6513 | "", | 6871 | "", |
| 6514 | ); | 6872 | ), &self.wip); |
| 6515 | for (llvm_param_attrs[0..param_count], 0..) |llvm_elem_ty, i| { | 6873 | for (llvm_param_attrs[0..param_count], 0..) |llvm_elem_ty, i| { |
| 6516 | if (llvm_elem_ty) |llvm_ty| { | 6874 | if (llvm_elem_ty != .none) { |
| 6517 | llvm.setCallElemTypeAttr(call, i, llvm_ty); | 6875 | llvm.setCallElemTypeAttr(call.toLlvm(&self.wip), i, llvm_elem_ty.toLlvm(&o.builder)); |
| 6518 | } | 6876 | } |
| 6519 | } | 6877 | } |
| 6520 | | 6878 | |
| ... | @@ -6523,16 +6881,17 @@ pub const FuncGen = struct { | ... | @@ -6523,16 +6881,17 @@ pub const FuncGen = struct { |
| 6523 | for (outputs, 0..) |output, i| { | 6881 | for (outputs, 0..) |output, i| { |
| 6524 | if (llvm_ret_indirect[i]) continue; | 6882 | if (llvm_ret_indirect[i]) continue; |
| 6525 | | 6883 | |
| 6526 | const output_value = if (return_count > 1) b: { | 6884 | const output_value = if (return_count > 1) |
| 6527 | break :b self.builder.buildExtractValue(call, @as(c_uint, @intCast(llvm_ret_i)), ""); | 6885 | try self.wip.extractValue(call, &[_]u32{@intCast(llvm_ret_i)}, "") |
| 6528 | } else call; | 6886 | else |
| | 6887 | call; |
| 6529 | | 6888 | |
| 6530 | if (output != .none) { | 6889 | if (output != .none) { |
| 6531 | const output_ptr = try self.resolveInst(output); | 6890 | const output_ptr = try self.resolveInst(output); |
| 6532 | const output_ptr_ty = self.typeOf(output); | 6891 | const output_ptr_ty = self.typeOf(output); |
| 6533 | | 6892 | |
| 6534 | const store_inst = self.builder.buildStore(output_value, output_ptr); | 6893 | const alignment = Builder.Alignment.fromByteUnits(output_ptr_ty.ptrAlignment(mod)); |
| 6535 | store_inst.setAlignment(output_ptr_ty.ptrAlignment(mod)); | 6894 | _ = try self.wip.store(.normal, output_value, output_ptr, alignment); |
| 6536 | } else { | 6895 | } else { |
| 6537 | ret_val = output_value; | 6896 | ret_val = output_value; |
| 6538 | } | 6897 | } |
| ... | @@ -6546,8 +6905,8 @@ pub const FuncGen = struct { | ... | @@ -6546,8 +6905,8 @@ pub const FuncGen = struct { |
| 6546 | self: *FuncGen, | 6905 | self: *FuncGen, |
| 6547 | inst: Air.Inst.Index, | 6906 | inst: Air.Inst.Index, |
| 6548 | operand_is_ptr: bool, | 6907 | operand_is_ptr: bool, |
| 6549 | pred: llvm.IntPredicate, | 6908 | cond: Builder.IntegerCondition, |
| 6550 | ) !?*llvm.Value { | 6909 | ) !Builder.Value { |
| 6551 | const o = self.dg.object; | 6910 | const o = self.dg.object; |
| 6552 | const mod = o.module; | 6911 | const mod = o.module; |
| 6553 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 6912 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| ... | @@ -6558,43 +6917,40 @@ pub const FuncGen = struct { | ... | @@ -6558,43 +6917,40 @@ pub const FuncGen = struct { |
| 6558 | const payload_ty = optional_ty.optionalChild(mod); | 6917 | const payload_ty = optional_ty.optionalChild(mod); |
| 6559 | if (optional_ty.optionalReprIsPayload(mod)) { | 6918 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 6560 | const loaded = if (operand_is_ptr) | 6919 | const loaded = if (operand_is_ptr) |
| 6561 | self.builder.buildLoad(optional_llvm_ty, operand, "") | 6920 | try self.wip.load(.normal, optional_llvm_ty, operand, .default, "") |
| 6562 | else | 6921 | else |
| 6563 | operand; | 6922 | operand; |
| 6564 | if (payload_ty.isSlice(mod)) { | 6923 | if (payload_ty.isSlice(mod)) { |
| 6565 | const slice_ptr = self.builder.buildExtractValue(loaded, 0, ""); | 6924 | const slice_ptr = try self.wip.extractValue(loaded, &.{0}, ""); |
| 6566 | const ptr_ty = try o.lowerType(payload_ty.slicePtrFieldType(mod)); | 6925 | const ptr_ty = try o.builder.ptrType(toLlvmAddressSpace( |
| 6567 | return self.builder.buildICmp(pred, slice_ptr, ptr_ty.constNull(), ""); | 6926 | payload_ty.ptrAddressSpace(mod), |
| | 6927 | mod.getTarget(), |
| | 6928 | )); |
| | 6929 | return self.wip.icmp(cond, slice_ptr, try o.builder.nullValue(ptr_ty), ""); |
| 6568 | } | 6930 | } |
| 6569 | return self.builder.buildICmp(pred, loaded, optional_llvm_ty.constNull(), ""); | 6931 | return self.wip.icmp(cond, loaded, try o.builder.zeroInitValue(optional_llvm_ty), ""); |
| 6570 | } | 6932 | } |
| 6571 | | 6933 | |
| 6572 | comptime assert(optional_layout_version == 3); | 6934 | comptime assert(optional_layout_version == 3); |
| 6573 | | 6935 | |
| 6574 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 6936 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6575 | const loaded = if (operand_is_ptr) | 6937 | const loaded = if (operand_is_ptr) |
| 6576 | self.builder.buildLoad(optional_llvm_ty, operand, "") | 6938 | try self.wip.load(.normal, optional_llvm_ty, operand, .default, "") |
| 6577 | else | 6939 | else |
| 6578 | operand; | 6940 | operand; |
| 6579 | const llvm_i8 = self.context.intType(8); | 6941 | return self.wip.icmp(cond, loaded, try o.builder.intValue(.i8, 0), ""); |
| 6580 | return self.builder.buildICmp(pred, loaded, llvm_i8.constNull(), ""); | | |
| 6581 | } | 6942 | } |
| 6582 | | 6943 | |
| 6583 | const is_by_ref = operand_is_ptr or isByRef(optional_ty, mod); | 6944 | const is_by_ref = operand_is_ptr or isByRef(optional_ty, mod); |
| 6584 | const non_null_bit = self.optIsNonNull(optional_llvm_ty, operand, is_by_ref); | 6945 | return self.optCmpNull(cond, optional_llvm_ty, operand, is_by_ref); |
| 6585 | if (pred == .EQ) { | | |
| 6586 | return self.builder.buildNot(non_null_bit, ""); | | |
| 6587 | } else { | | |
| 6588 | return non_null_bit; | | |
| 6589 | } | | |
| 6590 | } | 6946 | } |
| 6591 | | 6947 | |
| 6592 | fn airIsErr( | 6948 | fn airIsErr( |
| 6593 | self: *FuncGen, | 6949 | self: *FuncGen, |
| 6594 | inst: Air.Inst.Index, | 6950 | inst: Air.Inst.Index, |
| 6595 | op: llvm.IntPredicate, | 6951 | cond: Builder.IntegerCondition, |
| 6596 | operand_is_ptr: bool, | 6952 | operand_is_ptr: bool, |
| 6597 | ) !?*llvm.Value { | 6953 | ) !Builder.Value { |
| 6598 | const o = self.dg.object; | 6954 | const o = self.dg.object; |
| 6599 | const mod = o.module; | 6955 | const mod = o.module; |
| 6600 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 6956 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| ... | @@ -6602,40 +6958,37 @@ pub const FuncGen = struct { | ... | @@ -6602,40 +6958,37 @@ pub const FuncGen = struct { |
| 6602 | const operand_ty = self.typeOf(un_op); | 6958 | const operand_ty = self.typeOf(un_op); |
| 6603 | const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty; | 6959 | const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty; |
| 6604 | const payload_ty = err_union_ty.errorUnionPayload(mod); | 6960 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 6605 | const err_set_ty = try o.lowerType(Type.anyerror); | 6961 | const zero = try o.builder.intValue(Builder.Type.err_int, 0); |
| 6606 | const zero = err_set_ty.constNull(); | | |
| 6607 | | 6962 | |
| 6608 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { | 6963 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 6609 | const llvm_i1 = self.context.intType(1); | 6964 | const val: Builder.Constant = switch (cond) { |
| 6610 | switch (op) { | 6965 | .eq => .true, // 0 == 0 |
| 6611 | .EQ => return llvm_i1.constInt(1, .False), // 0 == 0 | 6966 | .ne => .false, // 0 != 0 |
| 6612 | .NE => return llvm_i1.constInt(0, .False), // 0 != 0 | | |
| 6613 | else => unreachable, | 6967 | else => unreachable, |
| 6614 | } | 6968 | }; |
| | 6969 | return val.toValue(); |
| 6615 | } | 6970 | } |
| 6616 | | 6971 | |
| 6617 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 6972 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6618 | const loaded = if (operand_is_ptr) | 6973 | const loaded = if (operand_is_ptr) |
| 6619 | self.builder.buildLoad(try o.lowerType(err_union_ty), operand, "") | 6974 | try self.wip.load(.normal, try o.lowerType(err_union_ty), operand, .default, "") |
| 6620 | else | 6975 | else |
| 6621 | operand; | 6976 | operand; |
| 6622 | return self.builder.buildICmp(op, loaded, zero, ""); | 6977 | return self.wip.icmp(cond, loaded, zero, ""); |
| 6623 | } | 6978 | } |
| 6624 | | 6979 | |
| 6625 | const err_field_index = errUnionErrorOffset(payload_ty, mod); | 6980 | const err_field_index = errUnionErrorOffset(payload_ty, mod); |
| 6626 | | 6981 | |
| 6627 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { | 6982 | const loaded = if (operand_is_ptr or isByRef(err_union_ty, mod)) loaded: { |
| 6628 | const err_union_llvm_ty = try o.lowerType(err_union_ty); | 6983 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 6629 | const err_field_ptr = self.builder.buildStructGEP(err_union_llvm_ty, operand, err_field_index, ""); | 6984 | const err_field_ptr = |
| 6630 | const loaded = self.builder.buildLoad(err_set_ty, err_field_ptr, ""); | 6985 | try self.wip.gepStruct(err_union_llvm_ty, operand, err_field_index, ""); |
| 6631 | return self.builder.buildICmp(op, loaded, zero, ""); | 6986 | break :loaded try self.wip.load(.normal, Builder.Type.err_int, err_field_ptr, .default, ""); |
| 6632 | } | 6987 | } else try self.wip.extractValue(operand, &.{err_field_index}, ""); |
| 6633 | | 6988 | return self.wip.icmp(cond, loaded, zero, ""); |
| 6634 | const loaded = self.builder.buildExtractValue(operand, err_field_index, ""); | | |
| 6635 | return self.builder.buildICmp(op, loaded, zero, ""); | | |
| 6636 | } | 6989 | } |
| 6637 | | 6990 | |
| 6638 | fn airOptionalPayloadPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 6991 | fn airOptionalPayloadPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6639 | const o = self.dg.object; | 6992 | const o = self.dg.object; |
| 6640 | const mod = o.module; | 6993 | const mod = o.module; |
| 6641 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 6994 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -6651,11 +7004,10 @@ pub const FuncGen = struct { | ... | @@ -6651,11 +7004,10 @@ pub const FuncGen = struct { |
| 6651 | // The payload and the optional are the same value. | 7004 | // The payload and the optional are the same value. |
| 6652 | return operand; | 7005 | return operand; |
| 6653 | } | 7006 | } |
| 6654 | const optional_llvm_ty = try o.lowerType(optional_ty); | 7007 | return self.wip.gepStruct(try o.lowerType(optional_ty), operand, 0, ""); |
| 6655 | return self.builder.buildStructGEP(optional_llvm_ty, operand, 0, ""); | | |
| 6656 | } | 7008 | } |
| 6657 | | 7009 | |
| 6658 | fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7010 | fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6659 | comptime assert(optional_layout_version == 3); | 7011 | comptime assert(optional_layout_version == 3); |
| 6660 | | 7012 | |
| 6661 | const o = self.dg.object; | 7013 | const o = self.dg.object; |
| ... | @@ -6664,10 +7016,10 @@ pub const FuncGen = struct { | ... | @@ -6664,10 +7016,10 @@ pub const FuncGen = struct { |
| 6664 | const operand = try self.resolveInst(ty_op.operand); | 7016 | const operand = try self.resolveInst(ty_op.operand); |
| 6665 | const optional_ty = self.typeOf(ty_op.operand).childType(mod); | 7017 | const optional_ty = self.typeOf(ty_op.operand).childType(mod); |
| 6666 | const payload_ty = optional_ty.optionalChild(mod); | 7018 | const payload_ty = optional_ty.optionalChild(mod); |
| 6667 | const non_null_bit = self.context.intType(8).constInt(1, .False); | 7019 | const non_null_bit = try o.builder.intValue(.i8, 1); |
| 6668 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7020 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6669 | // We have a pointer to a i8. We need to set it to 1 and then return the same pointer. | 7021 | // We have a pointer to a i8. We need to set it to 1 and then return the same pointer. |
| 6670 | _ = self.builder.buildStore(non_null_bit, operand); | 7022 | _ = try self.wip.store(.normal, non_null_bit, operand, .default); |
| 6671 | return operand; | 7023 | return operand; |
| 6672 | } | 7024 | } |
| 6673 | if (optional_ty.optionalReprIsPayload(mod)) { | 7025 | if (optional_ty.optionalReprIsPayload(mod)) { |
| ... | @@ -6678,18 +7030,17 @@ pub const FuncGen = struct { | ... | @@ -6678,18 +7030,17 @@ pub const FuncGen = struct { |
| 6678 | | 7030 | |
| 6679 | // First set the non-null bit. | 7031 | // First set the non-null bit. |
| 6680 | const optional_llvm_ty = try o.lowerType(optional_ty); | 7032 | const optional_llvm_ty = try o.lowerType(optional_ty); |
| 6681 | const non_null_ptr = self.builder.buildStructGEP(optional_llvm_ty, operand, 1, ""); | 7033 | const non_null_ptr = try self.wip.gepStruct(optional_llvm_ty, operand, 1, ""); |
| 6682 | // TODO set alignment on this store | 7034 | // TODO set alignment on this store |
| 6683 | _ = self.builder.buildStore(non_null_bit, non_null_ptr); | 7035 | _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default); |
| 6684 | | 7036 | |
| 6685 | // Then return the payload pointer (only if it's used). | 7037 | // Then return the payload pointer (only if it's used). |
| 6686 | if (self.liveness.isUnused(inst)) | 7038 | if (self.liveness.isUnused(inst)) return .none; |
| 6687 | return null; | | |
| 6688 | | 7039 | |
| 6689 | return self.builder.buildStructGEP(optional_llvm_ty, operand, 0, ""); | 7040 | return self.wip.gepStruct(optional_llvm_ty, operand, 0, ""); |
| 6690 | } | 7041 | } |
| 6691 | | 7042 | |
| 6692 | fn airOptionalPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 7043 | fn airOptionalPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6693 | const o = self.dg.object; | 7044 | const o = self.dg.object; |
| 6694 | const mod = o.module; | 7045 | const mod = o.module; |
| 6695 | const inst = body_tail[0]; | 7046 | const inst = body_tail[0]; |
| ... | @@ -6697,7 +7048,7 @@ pub const FuncGen = struct { | ... | @@ -6697,7 +7048,7 @@ pub const FuncGen = struct { |
| 6697 | const operand = try self.resolveInst(ty_op.operand); | 7048 | const operand = try self.resolveInst(ty_op.operand); |
| 6698 | const optional_ty = self.typeOf(ty_op.operand); | 7049 | const optional_ty = self.typeOf(ty_op.operand); |
| 6699 | const payload_ty = self.typeOfIndex(inst); | 7050 | const payload_ty = self.typeOfIndex(inst); |
| 6700 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return null; | 7051 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; |
| 6701 | | 7052 | |
| 6702 | if (optional_ty.optionalReprIsPayload(mod)) { | 7053 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 6703 | // Payload value is the same as the optional value. | 7054 | // Payload value is the same as the optional value. |
| ... | @@ -6713,7 +7064,7 @@ pub const FuncGen = struct { | ... | @@ -6713,7 +7064,7 @@ pub const FuncGen = struct { |
| 6713 | self: *FuncGen, | 7064 | self: *FuncGen, |
| 6714 | body_tail: []const Air.Inst.Index, | 7065 | body_tail: []const Air.Inst.Index, |
| 6715 | operand_is_ptr: bool, | 7066 | operand_is_ptr: bool, |
| 6716 | ) !?*llvm.Value { | 7067 | ) !Builder.Value { |
| 6717 | const o = self.dg.object; | 7068 | const o = self.dg.object; |
| 6718 | const mod = o.module; | 7069 | const mod = o.module; |
| 6719 | const inst = body_tail[0]; | 7070 | const inst = body_tail[0]; |
| ... | @@ -6725,32 +7076,30 @@ pub const FuncGen = struct { | ... | @@ -6725,32 +7076,30 @@ pub const FuncGen = struct { |
| 6725 | const payload_ty = if (operand_is_ptr) result_ty.childType(mod) else result_ty; | 7076 | const payload_ty = if (operand_is_ptr) result_ty.childType(mod) else result_ty; |
| 6726 | | 7077 | |
| 6727 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7078 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6728 | return if (operand_is_ptr) operand else null; | 7079 | return if (operand_is_ptr) operand else .none; |
| 6729 | } | 7080 | } |
| 6730 | const offset = errUnionPayloadOffset(payload_ty, mod); | 7081 | const offset = errUnionPayloadOffset(payload_ty, mod); |
| 6731 | const err_union_llvm_ty = try o.lowerType(err_union_ty); | 7082 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 6732 | if (operand_is_ptr) { | 7083 | if (operand_is_ptr) { |
| 6733 | return self.builder.buildStructGEP(err_union_llvm_ty, operand, offset, ""); | 7084 | return self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 6734 | } else if (isByRef(err_union_ty, mod)) { | 7085 | } else if (isByRef(err_union_ty, mod)) { |
| 6735 | const payload_ptr = self.builder.buildStructGEP(err_union_llvm_ty, operand, offset, ""); | 7086 | const payload_alignment = Builder.Alignment.fromByteUnits(payload_ty.abiAlignment(mod)); |
| | 7087 | const payload_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 6736 | if (isByRef(payload_ty, mod)) { | 7088 | if (isByRef(payload_ty, mod)) { |
| 6737 | if (self.canElideLoad(body_tail)) | 7089 | if (self.canElideLoad(body_tail)) return payload_ptr; |
| 6738 | return payload_ptr; | 7090 | return self.loadByRef(payload_ptr, payload_ty, payload_alignment, false); |
| 6739 | | | |
| 6740 | return self.loadByRef(payload_ptr, payload_ty, payload_ty.abiAlignment(mod), false); | | |
| 6741 | } | 7091 | } |
| 6742 | const load_inst = self.builder.buildLoad(err_union_llvm_ty.structGetTypeAtIndex(offset), payload_ptr, ""); | 7092 | const payload_llvm_ty = err_union_llvm_ty.structFields(&o.builder)[offset]; |
| 6743 | load_inst.setAlignment(payload_ty.abiAlignment(mod)); | 7093 | return self.wip.load(.normal, payload_llvm_ty, payload_ptr, payload_alignment, ""); |
| 6744 | return load_inst; | | |
| 6745 | } | 7094 | } |
| 6746 | return self.builder.buildExtractValue(operand, offset, ""); | 7095 | return self.wip.extractValue(operand, &.{offset}, ""); |
| 6747 | } | 7096 | } |
| 6748 | | 7097 | |
| 6749 | fn airErrUnionErr( | 7098 | fn airErrUnionErr( |
| 6750 | self: *FuncGen, | 7099 | self: *FuncGen, |
| 6751 | inst: Air.Inst.Index, | 7100 | inst: Air.Inst.Index, |
| 6752 | operand_is_ptr: bool, | 7101 | operand_is_ptr: bool, |
| 6753 | ) !?*llvm.Value { | 7102 | ) !Builder.Value { |
| 6754 | const o = self.dg.object; | 7103 | const o = self.dg.object; |
| 6755 | const mod = o.module; | 7104 | const mod = o.module; |
| 6756 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 7105 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -6758,34 +7107,31 @@ pub const FuncGen = struct { | ... | @@ -6758,34 +7107,31 @@ pub const FuncGen = struct { |
| 6758 | const operand_ty = self.typeOf(ty_op.operand); | 7107 | const operand_ty = self.typeOf(ty_op.operand); |
| 6759 | const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty; | 7108 | const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty; |
| 6760 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { | 7109 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 6761 | const err_llvm_ty = try o.lowerType(Type.anyerror); | | |
| 6762 | if (operand_is_ptr) { | 7110 | if (operand_is_ptr) { |
| 6763 | return operand; | 7111 | return operand; |
| 6764 | } else { | 7112 | } else { |
| 6765 | return err_llvm_ty.constInt(0, .False); | 7113 | return o.builder.intValue(Builder.Type.err_int, 0); |
| 6766 | } | 7114 | } |
| 6767 | } | 7115 | } |
| 6768 | | 7116 | |
| 6769 | const err_set_llvm_ty = try o.lowerType(Type.anyerror); | | |
| 6770 | | | |
| 6771 | const payload_ty = err_union_ty.errorUnionPayload(mod); | 7117 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 6772 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7118 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6773 | if (!operand_is_ptr) return operand; | 7119 | if (!operand_is_ptr) return operand; |
| 6774 | return self.builder.buildLoad(err_set_llvm_ty, operand, ""); | 7120 | return self.wip.load(.normal, Builder.Type.err_int, operand, .default, ""); |
| 6775 | } | 7121 | } |
| 6776 | | 7122 | |
| 6777 | const offset = errUnionErrorOffset(payload_ty, mod); | 7123 | const offset = errUnionErrorOffset(payload_ty, mod); |
| 6778 | | 7124 | |
| 6779 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { | 7125 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { |
| 6780 | const err_union_llvm_ty = try o.lowerType(err_union_ty); | 7126 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 6781 | const err_field_ptr = self.builder.buildStructGEP(err_union_llvm_ty, operand, offset, ""); | 7127 | const err_field_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 6782 | return self.builder.buildLoad(err_set_llvm_ty, err_field_ptr, ""); | 7128 | return self.wip.load(.normal, Builder.Type.err_int, err_field_ptr, .default, ""); |
| 6783 | } | 7129 | } |
| 6784 | | 7130 | |
| 6785 | return self.builder.buildExtractValue(operand, offset, ""); | 7131 | return self.wip.extractValue(operand, &.{offset}, ""); |
| 6786 | } | 7132 | } |
| 6787 | | 7133 | |
| 6788 | fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7134 | fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6789 | const o = self.dg.object; | 7135 | const o = self.dg.object; |
| 6790 | const mod = o.module; | 7136 | const mod = o.module; |
| 6791 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 7137 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -6793,49 +7139,49 @@ pub const FuncGen = struct { | ... | @@ -6793,49 +7139,49 @@ pub const FuncGen = struct { |
| 6793 | const err_union_ty = self.typeOf(ty_op.operand).childType(mod); | 7139 | const err_union_ty = self.typeOf(ty_op.operand).childType(mod); |
| 6794 | | 7140 | |
| 6795 | const payload_ty = err_union_ty.errorUnionPayload(mod); | 7141 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 6796 | const non_error_val = try o.lowerValue(.{ .ty = Type.anyerror, .val = try mod.intValue(Type.err_int, 0) }); | 7142 | const non_error_val = try o.builder.intValue(Builder.Type.err_int, 0); |
| 6797 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7143 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6798 | _ = self.builder.buildStore(non_error_val, operand); | 7144 | _ = try self.wip.store(.normal, non_error_val, operand, .default); |
| 6799 | return operand; | 7145 | return operand; |
| 6800 | } | 7146 | } |
| 6801 | const err_union_llvm_ty = try o.lowerType(err_union_ty); | 7147 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 6802 | { | 7148 | { |
| | 7149 | const error_alignment = Builder.Alignment.fromByteUnits(Type.err_int.abiAlignment(mod)); |
| 6803 | const error_offset = errUnionErrorOffset(payload_ty, mod); | 7150 | const error_offset = errUnionErrorOffset(payload_ty, mod); |
| 6804 | // First set the non-error value. | 7151 | // First set the non-error value. |
| 6805 | const non_null_ptr = self.builder.buildStructGEP(err_union_llvm_ty, operand, error_offset, ""); | 7152 | const non_null_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, error_offset, ""); |
| 6806 | const store_inst = self.builder.buildStore(non_error_val, non_null_ptr); | 7153 | _ = try self.wip.store(.normal, non_error_val, non_null_ptr, error_alignment); |
| 6807 | store_inst.setAlignment(Type.anyerror.abiAlignment(mod)); | | |
| 6808 | } | 7154 | } |
| 6809 | // Then return the payload pointer (only if it is used). | 7155 | // Then return the payload pointer (only if it is used). |
| 6810 | if (self.liveness.isUnused(inst)) | 7156 | if (self.liveness.isUnused(inst)) return .none; |
| 6811 | return null; | | |
| 6812 | | 7157 | |
| 6813 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); | 7158 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); |
| 6814 | return self.builder.buildStructGEP(err_union_llvm_ty, operand, payload_offset, ""); | 7159 | return self.wip.gepStruct(err_union_llvm_ty, operand, payload_offset, ""); |
| 6815 | } | 7160 | } |
| 6816 | | 7161 | |
| 6817 | fn airErrReturnTrace(self: *FuncGen, _: Air.Inst.Index) !?*llvm.Value { | 7162 | fn airErrReturnTrace(self: *FuncGen, _: Air.Inst.Index) !Builder.Value { |
| 6818 | return self.err_ret_trace.?; | 7163 | assert(self.err_ret_trace != .none); |
| | 7164 | return self.err_ret_trace; |
| 6819 | } | 7165 | } |
| 6820 | | 7166 | |
| 6821 | fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7167 | fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6822 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 7168 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 6823 | const operand = try self.resolveInst(un_op); | 7169 | self.err_ret_trace = try self.resolveInst(un_op); |
| 6824 | self.err_ret_trace = operand; | 7170 | return .none; |
| 6825 | return null; | | |
| 6826 | } | 7171 | } |
| 6827 | | 7172 | |
| 6828 | fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7173 | fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6829 | const o = self.dg.object; | 7174 | const o = self.dg.object; |
| 6830 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 7175 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 6831 | //const struct_ty = try self.resolveInst(ty_pl.ty); | | |
| 6832 | const struct_ty = self.air.getRefType(ty_pl.ty); | 7176 | const struct_ty = self.air.getRefType(ty_pl.ty); |
| 6833 | const field_index = ty_pl.payload; | 7177 | const field_index = ty_pl.payload; |
| 6834 | | 7178 | |
| 6835 | const mod = o.module; | 7179 | const mod = o.module; |
| 6836 | const llvm_field = llvmField(struct_ty, field_index, mod).?; | 7180 | const llvm_field = llvmField(struct_ty, field_index, mod).?; |
| 6837 | const struct_llvm_ty = try o.lowerType(struct_ty); | 7181 | const struct_llvm_ty = try o.lowerType(struct_ty); |
| 6838 | const field_ptr = self.builder.buildStructGEP(struct_llvm_ty, self.err_ret_trace.?, llvm_field.index, ""); | 7182 | assert(self.err_ret_trace != .none); |
| | 7183 | const field_ptr = |
| | 7184 | try self.wip.gepStruct(struct_llvm_ty, self.err_ret_trace, llvm_field.index, ""); |
| 6839 | const field_ptr_ty = try mod.ptrType(.{ | 7185 | const field_ptr_ty = try mod.ptrType(.{ |
| 6840 | .child = llvm_field.ty.toIntern(), | 7186 | .child = llvm_field.ty.toIntern(), |
| 6841 | .flags = .{ | 7187 | .flags = .{ |
| ... | @@ -6845,34 +7191,32 @@ pub const FuncGen = struct { | ... | @@ -6845,34 +7191,32 @@ pub const FuncGen = struct { |
| 6845 | return self.load(field_ptr, field_ptr_ty); | 7191 | return self.load(field_ptr, field_ptr_ty); |
| 6846 | } | 7192 | } |
| 6847 | | 7193 | |
| 6848 | fn airWrapOptional(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7194 | fn airWrapOptional(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6849 | const o = self.dg.object; | 7195 | const o = self.dg.object; |
| 6850 | const mod = o.module; | 7196 | const mod = o.module; |
| 6851 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 7197 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6852 | const payload_ty = self.typeOf(ty_op.operand); | 7198 | const payload_ty = self.typeOf(ty_op.operand); |
| 6853 | const non_null_bit = self.context.intType(8).constInt(1, .False); | 7199 | const non_null_bit = try o.builder.intValue(.i8, 1); |
| 6854 | comptime assert(optional_layout_version == 3); | 7200 | comptime assert(optional_layout_version == 3); |
| 6855 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return non_null_bit; | 7201 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return non_null_bit; |
| 6856 | const operand = try self.resolveInst(ty_op.operand); | 7202 | const operand = try self.resolveInst(ty_op.operand); |
| 6857 | const optional_ty = self.typeOfIndex(inst); | 7203 | const optional_ty = self.typeOfIndex(inst); |
| 6858 | if (optional_ty.optionalReprIsPayload(mod)) { | 7204 | if (optional_ty.optionalReprIsPayload(mod)) return operand; |
| 6859 | return operand; | | |
| 6860 | } | | |
| 6861 | const llvm_optional_ty = try o.lowerType(optional_ty); | 7205 | const llvm_optional_ty = try o.lowerType(optional_ty); |
| 6862 | if (isByRef(optional_ty, mod)) { | 7206 | if (isByRef(optional_ty, mod)) { |
| 6863 | const optional_ptr = self.buildAlloca(llvm_optional_ty, optional_ty.abiAlignment(mod)); | 7207 | const alignment = Builder.Alignment.fromByteUnits(optional_ty.abiAlignment(mod)); |
| 6864 | const payload_ptr = self.builder.buildStructGEP(llvm_optional_ty, optional_ptr, 0, ""); | 7208 | const optional_ptr = try self.buildAlloca(llvm_optional_ty, alignment); |
| | 7209 | const payload_ptr = try self.wip.gepStruct(llvm_optional_ty, optional_ptr, 0, ""); |
| 6865 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | 7210 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); |
| 6866 | try self.store(payload_ptr, payload_ptr_ty, operand, .NotAtomic); | 7211 | try self.store(payload_ptr, payload_ptr_ty, operand, .none); |
| 6867 | const non_null_ptr = self.builder.buildStructGEP(llvm_optional_ty, optional_ptr, 1, ""); | 7212 | const non_null_ptr = try self.wip.gepStruct(llvm_optional_ty, optional_ptr, 1, ""); |
| 6868 | _ = self.builder.buildStore(non_null_bit, non_null_ptr); | 7213 | _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default); |
| 6869 | return optional_ptr; | 7214 | return optional_ptr; |
| 6870 | } | 7215 | } |
| 6871 | const partial = self.builder.buildInsertValue(llvm_optional_ty.getUndef(), operand, 0, ""); | 7216 | return self.wip.buildAggregate(llvm_optional_ty, &.{ operand, non_null_bit }, ""); |
| 6872 | return self.builder.buildInsertValue(partial, non_null_bit, 1, ""); | | |
| 6873 | } | 7217 | } |
| 6874 | | 7218 | |
| 6875 | fn airWrapErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7219 | fn airWrapErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6876 | const o = self.dg.object; | 7220 | const o = self.dg.object; |
| 6877 | const mod = o.module; | 7221 | const mod = o.module; |
| 6878 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 7222 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -6882,46 +7226,47 @@ pub const FuncGen = struct { | ... | @@ -6882,46 +7226,47 @@ pub const FuncGen = struct { |
| 6882 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7226 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6883 | return operand; | 7227 | return operand; |
| 6884 | } | 7228 | } |
| 6885 | const ok_err_code = (try o.lowerType(Type.anyerror)).constNull(); | 7229 | const ok_err_code = try o.builder.intValue(Builder.Type.err_int, 0); |
| 6886 | const err_un_llvm_ty = try o.lowerType(err_un_ty); | 7230 | const err_un_llvm_ty = try o.lowerType(err_un_ty); |
| 6887 | | 7231 | |
| 6888 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); | 7232 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); |
| 6889 | const error_offset = errUnionErrorOffset(payload_ty, mod); | 7233 | const error_offset = errUnionErrorOffset(payload_ty, mod); |
| 6890 | if (isByRef(err_un_ty, mod)) { | 7234 | if (isByRef(err_un_ty, mod)) { |
| 6891 | const result_ptr = self.buildAlloca(err_un_llvm_ty, err_un_ty.abiAlignment(mod)); | 7235 | const alignment = Builder.Alignment.fromByteUnits(err_un_ty.abiAlignment(mod)); |
| 6892 | const err_ptr = self.builder.buildStructGEP(err_un_llvm_ty, result_ptr, error_offset, ""); | 7236 | const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment); |
| 6893 | const store_inst = self.builder.buildStore(ok_err_code, err_ptr); | 7237 | const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, ""); |
| 6894 | store_inst.setAlignment(Type.anyerror.abiAlignment(mod)); | 7238 | const error_alignment = Builder.Alignment.fromByteUnits(Type.err_int.abiAlignment(mod)); |
| 6895 | const payload_ptr = self.builder.buildStructGEP(err_un_llvm_ty, result_ptr, payload_offset, ""); | 7239 | _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment); |
| | 7240 | const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, ""); |
| 6896 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | 7241 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); |
| 6897 | try self.store(payload_ptr, payload_ptr_ty, operand, .NotAtomic); | 7242 | try self.store(payload_ptr, payload_ptr_ty, operand, .none); |
| 6898 | return result_ptr; | 7243 | return result_ptr; |
| 6899 | } | 7244 | } |
| 6900 | | 7245 | var fields: [2]Builder.Value = undefined; |
| 6901 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), ok_err_code, error_offset, ""); | 7246 | fields[payload_offset] = operand; |
| 6902 | return self.builder.buildInsertValue(partial, operand, payload_offset, ""); | 7247 | fields[error_offset] = ok_err_code; |
| | 7248 | return self.wip.buildAggregate(err_un_llvm_ty, &fields, ""); |
| 6903 | } | 7249 | } |
| 6904 | | 7250 | |
| 6905 | fn airWrapErrUnionErr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7251 | fn airWrapErrUnionErr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6906 | const o = self.dg.object; | 7252 | const o = self.dg.object; |
| 6907 | const mod = o.module; | 7253 | const mod = o.module; |
| 6908 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 7254 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6909 | const err_un_ty = self.typeOfIndex(inst); | 7255 | const err_un_ty = self.typeOfIndex(inst); |
| 6910 | const payload_ty = err_un_ty.errorUnionPayload(mod); | 7256 | const payload_ty = err_un_ty.errorUnionPayload(mod); |
| 6911 | const operand = try self.resolveInst(ty_op.operand); | 7257 | const operand = try self.resolveInst(ty_op.operand); |
| 6912 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | 7258 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return operand; |
| 6913 | return operand; | | |
| 6914 | } | | |
| 6915 | const err_un_llvm_ty = try o.lowerType(err_un_ty); | 7259 | const err_un_llvm_ty = try o.lowerType(err_un_ty); |
| 6916 | | 7260 | |
| 6917 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); | 7261 | const payload_offset = errUnionPayloadOffset(payload_ty, mod); |
| 6918 | const error_offset = errUnionErrorOffset(payload_ty, mod); | 7262 | const error_offset = errUnionErrorOffset(payload_ty, mod); |
| 6919 | if (isByRef(err_un_ty, mod)) { | 7263 | if (isByRef(err_un_ty, mod)) { |
| 6920 | const result_ptr = self.buildAlloca(err_un_llvm_ty, err_un_ty.abiAlignment(mod)); | 7264 | const alignment = Builder.Alignment.fromByteUnits(err_un_ty.abiAlignment(mod)); |
| 6921 | const err_ptr = self.builder.buildStructGEP(err_un_llvm_ty, result_ptr, error_offset, ""); | 7265 | const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment); |
| 6922 | const store_inst = self.builder.buildStore(operand, err_ptr); | 7266 | const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, ""); |
| 6923 | store_inst.setAlignment(Type.anyerror.abiAlignment(mod)); | 7267 | const error_alignment = Builder.Alignment.fromByteUnits(Type.err_int.abiAlignment(mod)); |
| 6924 | const payload_ptr = self.builder.buildStructGEP(err_un_llvm_ty, result_ptr, payload_offset, ""); | 7268 | _ = try self.wip.store(.normal, operand, err_ptr, error_alignment); |
| | 7269 | const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, ""); |
| 6925 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | 7270 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); |
| 6926 | // TODO store undef to payload_ptr | 7271 | // TODO store undef to payload_ptr |
| 6927 | _ = payload_ptr; | 7272 | _ = payload_ptr; |
| ... | @@ -6929,34 +7274,52 @@ pub const FuncGen = struct { | ... | @@ -6929,34 +7274,52 @@ pub const FuncGen = struct { |
| 6929 | return result_ptr; | 7274 | return result_ptr; |
| 6930 | } | 7275 | } |
| 6931 | | 7276 | |
| 6932 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), operand, error_offset, ""); | | |
| 6933 | // TODO set payload bytes to undef | 7277 | // TODO set payload bytes to undef |
| 6934 | return partial; | 7278 | const undef = try o.builder.undefValue(err_un_llvm_ty); |
| | 7279 | return self.wip.insertValue(undef, operand, &.{error_offset}, ""); |
| 6935 | } | 7280 | } |
| 6936 | | 7281 | |
| 6937 | fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7282 | fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| | 7283 | const o = self.dg.object; |
| 6938 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 7284 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 6939 | const index = pl_op.payload; | 7285 | const index = pl_op.payload; |
| 6940 | const llvm_u32 = self.context.intType(32); | 7286 | const llvm_fn = try self.getIntrinsic("llvm.wasm.memory.size", &.{.i32}); |
| 6941 | const llvm_fn = self.getIntrinsic("llvm.wasm.memory.size", &.{llvm_u32}); | 7287 | const args: [1]*llvm.Value = .{ |
| 6942 | const args: [1]*llvm.Value = .{llvm_u32.constInt(index, .False)}; | 7288 | (try o.builder.intConst(.i32, index)).toLlvm(&o.builder), |
| 6943 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 7289 | }; |
| | 7290 | return (try self.wip.unimplemented(.i32, "")).finish(self.builder.buildCall( |
| | 7291 | (try o.builder.fnType(.i32, &.{.i32}, .normal)).toLlvm(&o.builder), |
| | 7292 | llvm_fn, |
| | 7293 | &args, |
| | 7294 | args.len, |
| | 7295 | .Fast, |
| | 7296 | .Auto, |
| | 7297 | "", |
| | 7298 | ), &self.wip); |
| 6944 | } | 7299 | } |
| 6945 | | 7300 | |
| 6946 | fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7301 | fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| | 7302 | const o = self.dg.object; |
| 6947 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 7303 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 6948 | const index = pl_op.payload; | 7304 | const index = pl_op.payload; |
| 6949 | const operand = try self.resolveInst(pl_op.operand); | 7305 | const operand = try self.resolveInst(pl_op.operand); |
| 6950 | const llvm_u32 = self.context.intType(32); | 7306 | const llvm_fn = try self.getIntrinsic("llvm.wasm.memory.grow", &.{.i32}); |
| 6951 | const llvm_fn = self.getIntrinsic("llvm.wasm.memory.grow", &.{llvm_u32}); | | |
| 6952 | const args: [2]*llvm.Value = .{ | 7307 | const args: [2]*llvm.Value = .{ |
| 6953 | llvm_u32.constInt(index, .False), | 7308 | (try o.builder.intConst(.i32, index)).toLlvm(&o.builder), |
| 6954 | operand, | 7309 | operand.toLlvm(&self.wip), |
| 6955 | }; | 7310 | }; |
| 6956 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 7311 | return (try self.wip.unimplemented(.i32, "")).finish(self.builder.buildCall( |
| | 7312 | (try o.builder.fnType(.i32, &.{ .i32, .i32 }, .normal)).toLlvm(&o.builder), |
| | 7313 | llvm_fn, |
| | 7314 | &args, |
| | 7315 | args.len, |
| | 7316 | .Fast, |
| | 7317 | .Auto, |
| | 7318 | "", |
| | 7319 | ), &self.wip); |
| 6957 | } | 7320 | } |
| 6958 | | 7321 | |
| 6959 | fn airVectorStoreElem(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7322 | fn airVectorStoreElem(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6960 | const o = self.dg.object; | 7323 | const o = self.dg.object; |
| 6961 | const mod = o.module; | 7324 | const mod = o.module; |
| 6962 | const data = self.air.instructions.items(.data)[inst].vector_store_elem; | 7325 | const data = self.air.instructions.items(.data)[inst].vector_store_elem; |
| ... | @@ -6967,19 +7330,20 @@ pub const FuncGen = struct { | ... | @@ -6967,19 +7330,20 @@ pub const FuncGen = struct { |
| 6967 | const index = try self.resolveInst(extra.lhs); | 7330 | const index = try self.resolveInst(extra.lhs); |
| 6968 | const operand = try self.resolveInst(extra.rhs); | 7331 | const operand = try self.resolveInst(extra.rhs); |
| 6969 | | 7332 | |
| 6970 | const loaded_vector = blk: { | 7333 | const kind: Builder.MemoryAccessKind = switch (vector_ptr_ty.isVolatilePtr(mod)) { |
| 6971 | const elem_llvm_ty = try o.lowerType(vector_ptr_ty.childType(mod)); | 7334 | false => .normal, |
| 6972 | const load_inst = self.builder.buildLoad(elem_llvm_ty, vector_ptr, ""); | 7335 | true => .@"volatile", |
| 6973 | load_inst.setAlignment(vector_ptr_ty.ptrAlignment(mod)); | | |
| 6974 | load_inst.setVolatile(llvm.Bool.fromBool(vector_ptr_ty.isVolatilePtr(mod))); | | |
| 6975 | break :blk load_inst; | | |
| 6976 | }; | 7336 | }; |
| 6977 | const modified_vector = self.builder.buildInsertElement(loaded_vector, operand, index, ""); | 7337 | const elem_llvm_ty = try o.lowerType(vector_ptr_ty.childType(mod)); |
| 6978 | try self.store(vector_ptr, vector_ptr_ty, modified_vector, .NotAtomic); | 7338 | const alignment = Builder.Alignment.fromByteUnits(vector_ptr_ty.ptrAlignment(mod)); |
| 6979 | return null; | 7339 | const loaded = try self.wip.load(kind, elem_llvm_ty, vector_ptr, alignment, ""); |
| | 7340 | |
| | 7341 | const new_vector = try self.wip.insertElement(loaded, operand, index, ""); |
| | 7342 | _ = try self.store(vector_ptr, vector_ptr_ty, new_vector, .none); |
| | 7343 | return .none; |
| 6980 | } | 7344 | } |
| 6981 | | 7345 | |
| 6982 | fn airMin(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7346 | fn airMin(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6983 | const o = self.dg.object; | 7347 | const o = self.dg.object; |
| 6984 | const mod = o.module; | 7348 | const mod = o.module; |
| 6985 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7349 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -6988,11 +7352,13 @@ pub const FuncGen = struct { | ... | @@ -6988,11 +7352,13 @@ pub const FuncGen = struct { |
| 6988 | const scalar_ty = self.typeOfIndex(inst).scalarType(mod); | 7352 | const scalar_ty = self.typeOfIndex(inst).scalarType(mod); |
| 6989 | | 7353 | |
| 6990 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.fmin, scalar_ty, 2, .{ lhs, rhs }); | 7354 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.fmin, scalar_ty, 2, .{ lhs, rhs }); |
| 6991 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSMin(lhs, rhs, ""); | 7355 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 6992 | return self.builder.buildUMin(lhs, rhs, ""); | 7356 | .@"llvm.smin." |
| | 7357 | else |
| | 7358 | .@"llvm.umin.", lhs, rhs, ""); |
| 6993 | } | 7359 | } |
| 6994 | | 7360 | |
| 6995 | fn airMax(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7361 | fn airMax(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6996 | const o = self.dg.object; | 7362 | const o = self.dg.object; |
| 6997 | const mod = o.module; | 7363 | const mod = o.module; |
| 6998 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7364 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7001,26 +7367,23 @@ pub const FuncGen = struct { | ... | @@ -7001,26 +7367,23 @@ pub const FuncGen = struct { |
| 7001 | const scalar_ty = self.typeOfIndex(inst).scalarType(mod); | 7367 | const scalar_ty = self.typeOfIndex(inst).scalarType(mod); |
| 7002 | | 7368 | |
| 7003 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.fmax, scalar_ty, 2, .{ lhs, rhs }); | 7369 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.fmax, scalar_ty, 2, .{ lhs, rhs }); |
| 7004 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSMax(lhs, rhs, ""); | 7370 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7005 | return self.builder.buildUMax(lhs, rhs, ""); | 7371 | .@"llvm.smax." |
| | 7372 | else |
| | 7373 | .@"llvm.umax.", lhs, rhs, ""); |
| 7006 | } | 7374 | } |
| 7007 | | 7375 | |
| 7008 | fn airSlice(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7376 | fn airSlice(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7009 | const o = self.dg.object; | 7377 | const o = self.dg.object; |
| 7010 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 7378 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 7011 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 7379 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 7012 | const ptr = try self.resolveInst(bin_op.lhs); | 7380 | const ptr = try self.resolveInst(bin_op.lhs); |
| 7013 | const len = try self.resolveInst(bin_op.rhs); | 7381 | const len = try self.resolveInst(bin_op.rhs); |
| 7014 | const inst_ty = self.typeOfIndex(inst); | 7382 | const inst_ty = self.typeOfIndex(inst); |
| 7015 | const llvm_slice_ty = try o.lowerType(inst_ty); | 7383 | return self.wip.buildAggregate(try o.lowerType(inst_ty), &.{ ptr, len }, ""); |
| 7016 | | | |
| 7017 | // In case of slicing a global, the result type looks something like `{ i8*, i64 }` | | |
| 7018 | // but `ptr` is pointing to the global directly. | | |
| 7019 | const partial = self.builder.buildInsertValue(llvm_slice_ty.getUndef(), ptr, 0, ""); | | |
| 7020 | return self.builder.buildInsertValue(partial, len, 1, ""); | | |
| 7021 | } | 7384 | } |
| 7022 | | 7385 | |
| 7023 | fn airAdd(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7386 | fn airAdd(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7024 | self.builder.setFastMath(want_fast_math); | 7387 | self.builder.setFastMath(want_fast_math); |
| 7025 | | 7388 | |
| 7026 | const o = self.dg.object; | 7389 | const o = self.dg.object; |
| ... | @@ -7032,8 +7395,7 @@ pub const FuncGen = struct { | ... | @@ -7032,8 +7395,7 @@ pub const FuncGen = struct { |
| 7032 | const scalar_ty = inst_ty.scalarType(mod); | 7395 | const scalar_ty = inst_ty.scalarType(mod); |
| 7033 | | 7396 | |
| 7034 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.add, inst_ty, 2, .{ lhs, rhs }); | 7397 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.add, inst_ty, 2, .{ lhs, rhs }); |
| 7035 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildNSWAdd(lhs, rhs, ""); | 7398 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) .@"add nsw" else .@"add nuw", lhs, rhs, ""); |
| 7036 | return self.builder.buildNUWAdd(lhs, rhs, ""); | | |
| 7037 | } | 7399 | } |
| 7038 | | 7400 | |
| 7039 | fn airSafeArithmetic( | 7401 | fn airSafeArithmetic( |
| ... | @@ -7041,7 +7403,7 @@ pub const FuncGen = struct { | ... | @@ -7041,7 +7403,7 @@ pub const FuncGen = struct { |
| 7041 | inst: Air.Inst.Index, | 7403 | inst: Air.Inst.Index, |
| 7042 | signed_intrinsic: []const u8, | 7404 | signed_intrinsic: []const u8, |
| 7043 | unsigned_intrinsic: []const u8, | 7405 | unsigned_intrinsic: []const u8, |
| 7044 | ) !?*llvm.Value { | 7406 | ) !Builder.Value { |
| 7045 | const o = fg.dg.object; | 7407 | const o = fg.dg.object; |
| 7046 | const mod = o.module; | 7408 | const mod = o.module; |
| 7047 | | 7409 | |
| ... | @@ -7057,42 +7419,50 @@ pub const FuncGen = struct { | ... | @@ -7057,42 +7419,50 @@ pub const FuncGen = struct { |
| 7057 | false => unsigned_intrinsic, | 7419 | false => unsigned_intrinsic, |
| 7058 | }; | 7420 | }; |
| 7059 | const llvm_inst_ty = try o.lowerType(inst_ty); | 7421 | const llvm_inst_ty = try o.lowerType(inst_ty); |
| 7060 | const llvm_fn = fg.getIntrinsic(intrinsic_name, &.{llvm_inst_ty}); | 7422 | const llvm_ret_ty = try o.builder.structType(.normal, &.{ |
| 7061 | const result_struct = fg.builder.buildCall( | 7423 | llvm_inst_ty, |
| 7062 | llvm_fn.globalGetValueType(), | 7424 | try llvm_inst_ty.changeScalar(.i1, &o.builder), |
| | 7425 | }); |
| | 7426 | const llvm_fn_ty = try o.builder.fnType(llvm_ret_ty, &.{ llvm_inst_ty, llvm_inst_ty }, .normal); |
| | 7427 | const llvm_fn = try fg.getIntrinsic(intrinsic_name, &.{llvm_inst_ty}); |
| | 7428 | const result_struct = (try fg.wip.unimplemented(llvm_ret_ty, "")).finish(fg.builder.buildCall( |
| | 7429 | llvm_fn_ty.toLlvm(&o.builder), |
| 7063 | llvm_fn, | 7430 | llvm_fn, |
| 7064 | &[_]*llvm.Value{ lhs, rhs }, | 7431 | &[_]*llvm.Value{ lhs.toLlvm(&fg.wip), rhs.toLlvm(&fg.wip) }, |
| 7065 | 2, | 7432 | 2, |
| 7066 | .Fast, | 7433 | .Fast, |
| 7067 | .Auto, | 7434 | .Auto, |
| 7068 | "", | 7435 | "", |
| 7069 | ); | 7436 | ), &fg.wip); |
| 7070 | const overflow_bit = fg.builder.buildExtractValue(result_struct, 1, ""); | 7437 | const overflow_bit = try fg.wip.extractValue(result_struct, &.{1}, ""); |
| 7071 | const scalar_overflow_bit = switch (is_scalar) { | 7438 | const scalar_overflow_bit = switch (is_scalar) { |
| 7072 | true => overflow_bit, | 7439 | true => overflow_bit, |
| 7073 | false => fg.builder.buildOrReduce(overflow_bit), | 7440 | false => (try fg.wip.unimplemented(.i1, "")).finish( |
| | 7441 | fg.builder.buildOrReduce(overflow_bit.toLlvm(&fg.wip)), |
| | 7442 | &fg.wip, |
| | 7443 | ), |
| 7074 | }; | 7444 | }; |
| 7075 | | 7445 | |
| 7076 | const fail_block = fg.context.appendBasicBlock(fg.llvm_func, "OverflowFail"); | 7446 | const fail_block = try fg.wip.block(1, "OverflowFail"); |
| 7077 | const ok_block = fg.context.appendBasicBlock(fg.llvm_func, "OverflowOk"); | 7447 | const ok_block = try fg.wip.block(1, "OverflowOk"); |
| 7078 | _ = fg.builder.buildCondBr(scalar_overflow_bit, fail_block, ok_block); | 7448 | _ = try fg.wip.brCond(scalar_overflow_bit, fail_block, ok_block); |
| 7079 | | 7449 | |
| 7080 | fg.builder.positionBuilderAtEnd(fail_block); | 7450 | fg.wip.cursor = .{ .block = fail_block }; |
| 7081 | try fg.buildSimplePanic(.integer_overflow); | 7451 | try fg.buildSimplePanic(.integer_overflow); |
| 7082 | | 7452 | |
| 7083 | fg.builder.positionBuilderAtEnd(ok_block); | 7453 | fg.wip.cursor = .{ .block = ok_block }; |
| 7084 | return fg.builder.buildExtractValue(result_struct, 0, ""); | 7454 | return fg.wip.extractValue(result_struct, &.{0}, ""); |
| 7085 | } | 7455 | } |
| 7086 | | 7456 | |
| 7087 | fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7457 | fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7088 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7458 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7089 | const lhs = try self.resolveInst(bin_op.lhs); | 7459 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7090 | const rhs = try self.resolveInst(bin_op.rhs); | 7460 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7091 | | 7461 | |
| 7092 | return self.builder.buildAdd(lhs, rhs, ""); | 7462 | return self.wip.bin(.add, lhs, rhs, ""); |
| 7093 | } | 7463 | } |
| 7094 | | 7464 | |
| 7095 | fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7465 | fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7096 | const o = self.dg.object; | 7466 | const o = self.dg.object; |
| 7097 | const mod = o.module; | 7467 | const mod = o.module; |
| 7098 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7468 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7102,12 +7472,13 @@ pub const FuncGen = struct { | ... | @@ -7102,12 +7472,13 @@ pub const FuncGen = struct { |
| 7102 | const scalar_ty = inst_ty.scalarType(mod); | 7472 | const scalar_ty = inst_ty.scalarType(mod); |
| 7103 | | 7473 | |
| 7104 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float add", .{}); | 7474 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float add", .{}); |
| 7105 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSAddSat(lhs, rhs, ""); | 7475 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7106 | | 7476 | .@"llvm.sadd.sat." |
| 7107 | return self.builder.buildUAddSat(lhs, rhs, ""); | 7477 | else |
| | 7478 | .@"llvm.uadd.sat.", lhs, rhs, ""); |
| 7108 | } | 7479 | } |
| 7109 | | 7480 | |
| 7110 | fn airSub(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7481 | fn airSub(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7111 | self.builder.setFastMath(want_fast_math); | 7482 | self.builder.setFastMath(want_fast_math); |
| 7112 | | 7483 | |
| 7113 | const o = self.dg.object; | 7484 | const o = self.dg.object; |
| ... | @@ -7119,19 +7490,18 @@ pub const FuncGen = struct { | ... | @@ -7119,19 +7490,18 @@ pub const FuncGen = struct { |
| 7119 | const scalar_ty = inst_ty.scalarType(mod); | 7490 | const scalar_ty = inst_ty.scalarType(mod); |
| 7120 | | 7491 | |
| 7121 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.sub, inst_ty, 2, .{ lhs, rhs }); | 7492 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.sub, inst_ty, 2, .{ lhs, rhs }); |
| 7122 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildNSWSub(lhs, rhs, ""); | 7493 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) .@"sub nsw" else .@"sub nuw", lhs, rhs, ""); |
| 7123 | return self.builder.buildNUWSub(lhs, rhs, ""); | | |
| 7124 | } | 7494 | } |
| 7125 | | 7495 | |
| 7126 | fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7496 | fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7127 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7497 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7128 | const lhs = try self.resolveInst(bin_op.lhs); | 7498 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7129 | const rhs = try self.resolveInst(bin_op.rhs); | 7499 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7130 | | 7500 | |
| 7131 | return self.builder.buildSub(lhs, rhs, ""); | 7501 | return self.wip.bin(.sub, lhs, rhs, ""); |
| 7132 | } | 7502 | } |
| 7133 | | 7503 | |
| 7134 | fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7504 | fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7135 | const o = self.dg.object; | 7505 | const o = self.dg.object; |
| 7136 | const mod = o.module; | 7506 | const mod = o.module; |
| 7137 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7507 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7141,11 +7511,13 @@ pub const FuncGen = struct { | ... | @@ -7141,11 +7511,13 @@ pub const FuncGen = struct { |
| 7141 | const scalar_ty = inst_ty.scalarType(mod); | 7511 | const scalar_ty = inst_ty.scalarType(mod); |
| 7142 | | 7512 | |
| 7143 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float sub", .{}); | 7513 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float sub", .{}); |
| 7144 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSSubSat(lhs, rhs, ""); | 7514 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7145 | return self.builder.buildUSubSat(lhs, rhs, ""); | 7515 | .@"llvm.ssub.sat." |
| | 7516 | else |
| | 7517 | .@"llvm.usub.sat.", lhs, rhs, ""); |
| 7146 | } | 7518 | } |
| 7147 | | 7519 | |
| 7148 | fn airMul(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7520 | fn airMul(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7149 | self.builder.setFastMath(want_fast_math); | 7521 | self.builder.setFastMath(want_fast_math); |
| 7150 | | 7522 | |
| 7151 | const o = self.dg.object; | 7523 | const o = self.dg.object; |
| ... | @@ -7157,19 +7529,18 @@ pub const FuncGen = struct { | ... | @@ -7157,19 +7529,18 @@ pub const FuncGen = struct { |
| 7157 | const scalar_ty = inst_ty.scalarType(mod); | 7529 | const scalar_ty = inst_ty.scalarType(mod); |
| 7158 | | 7530 | |
| 7159 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.mul, inst_ty, 2, .{ lhs, rhs }); | 7531 | if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.mul, inst_ty, 2, .{ lhs, rhs }); |
| 7160 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildNSWMul(lhs, rhs, ""); | 7532 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) .@"mul nsw" else .@"mul nuw", lhs, rhs, ""); |
| 7161 | return self.builder.buildNUWMul(lhs, rhs, ""); | | |
| 7162 | } | 7533 | } |
| 7163 | | 7534 | |
| 7164 | fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7535 | fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7165 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7536 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7166 | const lhs = try self.resolveInst(bin_op.lhs); | 7537 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7167 | const rhs = try self.resolveInst(bin_op.rhs); | 7538 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7168 | | 7539 | |
| 7169 | return self.builder.buildMul(lhs, rhs, ""); | 7540 | return self.wip.bin(.mul, lhs, rhs, ""); |
| 7170 | } | 7541 | } |
| 7171 | | 7542 | |
| 7172 | fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7543 | fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7173 | const o = self.dg.object; | 7544 | const o = self.dg.object; |
| 7174 | const mod = o.module; | 7545 | const mod = o.module; |
| 7175 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7546 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7179,11 +7550,13 @@ pub const FuncGen = struct { | ... | @@ -7179,11 +7550,13 @@ pub const FuncGen = struct { |
| 7179 | const scalar_ty = inst_ty.scalarType(mod); | 7550 | const scalar_ty = inst_ty.scalarType(mod); |
| 7180 | | 7551 | |
| 7181 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float mul", .{}); | 7552 | if (scalar_ty.isAnyFloat()) return self.todo("saturating float mul", .{}); |
| 7182 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSMulFixSat(lhs, rhs, ""); | 7553 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7183 | return self.builder.buildUMulFixSat(lhs, rhs, ""); | 7554 | .@"llvm.smul.fix.sat." |
| | 7555 | else |
| | 7556 | .@"llvm.umul.fix.sat.", lhs, rhs, ""); |
| 7184 | } | 7557 | } |
| 7185 | | 7558 | |
| 7186 | fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7559 | fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7187 | self.builder.setFastMath(want_fast_math); | 7560 | self.builder.setFastMath(want_fast_math); |
| 7188 | | 7561 | |
| 7189 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 7562 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7194,7 +7567,7 @@ pub const FuncGen = struct { | ... | @@ -7194,7 +7567,7 @@ pub const FuncGen = struct { |
| 7194 | return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); | 7567 | return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); |
| 7195 | } | 7568 | } |
| 7196 | | 7569 | |
| 7197 | fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7570 | fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7198 | self.builder.setFastMath(want_fast_math); | 7571 | self.builder.setFastMath(want_fast_math); |
| 7199 | | 7572 | |
| 7200 | const o = self.dg.object; | 7573 | const o = self.dg.object; |
| ... | @@ -7209,11 +7582,10 @@ pub const FuncGen = struct { | ... | @@ -7209,11 +7582,10 @@ pub const FuncGen = struct { |
| 7209 | const result = try self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); | 7582 | const result = try self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); |
| 7210 | return self.buildFloatOp(.trunc, inst_ty, 1, .{result}); | 7583 | return self.buildFloatOp(.trunc, inst_ty, 1, .{result}); |
| 7211 | } | 7584 | } |
| 7212 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSDiv(lhs, rhs, ""); | 7585 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) .sdiv else .udiv, lhs, rhs, ""); |
| 7213 | return self.builder.buildUDiv(lhs, rhs, ""); | | |
| 7214 | } | 7586 | } |
| 7215 | | 7587 | |
| 7216 | fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7588 | fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7217 | self.builder.setFastMath(want_fast_math); | 7589 | self.builder.setFastMath(want_fast_math); |
| 7218 | | 7590 | |
| 7219 | const o = self.dg.object; | 7591 | const o = self.dg.object; |
| ... | @@ -7230,31 +7602,24 @@ pub const FuncGen = struct { | ... | @@ -7230,31 +7602,24 @@ pub const FuncGen = struct { |
| 7230 | } | 7602 | } |
| 7231 | if (scalar_ty.isSignedInt(mod)) { | 7603 | if (scalar_ty.isSignedInt(mod)) { |
| 7232 | const inst_llvm_ty = try o.lowerType(inst_ty); | 7604 | const inst_llvm_ty = try o.lowerType(inst_ty); |
| 7233 | const scalar_bit_size_minus_one = scalar_ty.bitSize(mod) - 1; | 7605 | const bit_size_minus_one = try o.builder.splatValue(inst_llvm_ty, try o.builder.intConst( |
| 7234 | const bit_size_minus_one = if (inst_ty.zigTypeTag(mod) == .Vector) const_vector: { | 7606 | inst_llvm_ty.scalarType(&o.builder), |
| 7235 | const vec_len = inst_ty.vectorLen(mod); | 7607 | inst_llvm_ty.scalarBits(&o.builder) - 1, |
| 7236 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | 7608 | )); |
| 7237 | | | |
| 7238 | const shifts = try self.gpa.alloc(*llvm.Value, vec_len); | | |
| 7239 | defer self.gpa.free(shifts); | | |
| 7240 | | 7609 | |
| 7241 | @memset(shifts, scalar_llvm_ty.constInt(scalar_bit_size_minus_one, .False)); | 7610 | const div = try self.wip.bin(.sdiv, lhs, rhs, ""); |
| 7242 | break :const_vector llvm.constVector(shifts.ptr, vec_len); | 7611 | const rem = try self.wip.bin(.srem, lhs, rhs, ""); |
| 7243 | } else inst_llvm_ty.constInt(scalar_bit_size_minus_one, .False); | 7612 | const div_sign = try self.wip.bin(.xor, lhs, rhs, ""); |
| 7244 | | 7613 | const div_sign_mask = try self.wip.bin(.ashr, div_sign, bit_size_minus_one, ""); |
| 7245 | const div = self.builder.buildSDiv(lhs, rhs, ""); | 7614 | const zero = try o.builder.zeroInitValue(inst_llvm_ty); |
| 7246 | const rem = self.builder.buildSRem(lhs, rhs, ""); | 7615 | const rem_nonzero = try self.wip.icmp(.ne, rem, zero, ""); |
| 7247 | const div_sign = self.builder.buildXor(lhs, rhs, ""); | 7616 | const correction = try self.wip.select(rem_nonzero, div_sign_mask, zero, ""); |
| 7248 | const div_sign_mask = self.builder.buildAShr(div_sign, bit_size_minus_one, ""); | 7617 | return self.wip.bin(.@"add nsw", div, correction, ""); |
| 7249 | const zero = inst_llvm_ty.constNull(); | | |
| 7250 | const rem_nonzero = self.builder.buildICmp(.NE, rem, zero, ""); | | |
| 7251 | const correction = self.builder.buildSelect(rem_nonzero, div_sign_mask, zero, ""); | | |
| 7252 | return self.builder.buildNSWAdd(div, correction, ""); | | |
| 7253 | } | 7618 | } |
| 7254 | return self.builder.buildUDiv(lhs, rhs, ""); | 7619 | return self.wip.bin(.udiv, lhs, rhs, ""); |
| 7255 | } | 7620 | } |
| 7256 | | 7621 | |
| 7257 | fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7622 | fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7258 | self.builder.setFastMath(want_fast_math); | 7623 | self.builder.setFastMath(want_fast_math); |
| 7259 | | 7624 | |
| 7260 | const o = self.dg.object; | 7625 | const o = self.dg.object; |
| ... | @@ -7266,11 +7631,13 @@ pub const FuncGen = struct { | ... | @@ -7266,11 +7631,13 @@ pub const FuncGen = struct { |
| 7266 | const scalar_ty = inst_ty.scalarType(mod); | 7631 | const scalar_ty = inst_ty.scalarType(mod); |
| 7267 | | 7632 | |
| 7268 | if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); | 7633 | if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs }); |
| 7269 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildExactSDiv(lhs, rhs, ""); | 7634 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7270 | return self.builder.buildExactUDiv(lhs, rhs, ""); | 7635 | .@"sdiv exact" |
| | 7636 | else |
| | 7637 | .@"udiv exact", lhs, rhs, ""); |
| 7271 | } | 7638 | } |
| 7272 | | 7639 | |
| 7273 | fn airRem(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7640 | fn airRem(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7274 | self.builder.setFastMath(want_fast_math); | 7641 | self.builder.setFastMath(want_fast_math); |
| 7275 | | 7642 | |
| 7276 | const o = self.dg.object; | 7643 | const o = self.dg.object; |
| ... | @@ -7282,11 +7649,13 @@ pub const FuncGen = struct { | ... | @@ -7282,11 +7649,13 @@ pub const FuncGen = struct { |
| 7282 | const scalar_ty = inst_ty.scalarType(mod); | 7649 | const scalar_ty = inst_ty.scalarType(mod); |
| 7283 | | 7650 | |
| 7284 | if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs }); | 7651 | if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs }); |
| 7285 | if (scalar_ty.isSignedInt(mod)) return self.builder.buildSRem(lhs, rhs, ""); | 7652 | return self.wip.bin(if (scalar_ty.isSignedInt(mod)) |
| 7286 | return self.builder.buildURem(lhs, rhs, ""); | 7653 | .srem |
| | 7654 | else |
| | 7655 | .urem, lhs, rhs, ""); |
| 7287 | } | 7656 | } |
| 7288 | | 7657 | |
| 7289 | fn airMod(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 7658 | fn airMod(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 7290 | self.builder.setFastMath(want_fast_math); | 7659 | self.builder.setFastMath(want_fast_math); |
| 7291 | | 7660 | |
| 7292 | const o = self.dg.object; | 7661 | const o = self.dg.object; |
| ... | @@ -7302,36 +7671,29 @@ pub const FuncGen = struct { | ... | @@ -7302,36 +7671,29 @@ pub const FuncGen = struct { |
| 7302 | const a = try self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs }); | 7671 | const a = try self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs }); |
| 7303 | const b = try self.buildFloatOp(.add, inst_ty, 2, .{ a, rhs }); | 7672 | const b = try self.buildFloatOp(.add, inst_ty, 2, .{ a, rhs }); |
| 7304 | const c = try self.buildFloatOp(.fmod, inst_ty, 2, .{ b, rhs }); | 7673 | const c = try self.buildFloatOp(.fmod, inst_ty, 2, .{ b, rhs }); |
| 7305 | const zero = inst_llvm_ty.constNull(); | 7674 | const zero = try o.builder.zeroInitValue(inst_llvm_ty); |
| 7306 | const ltz = try self.buildFloatCmp(.lt, inst_ty, .{ lhs, zero }); | 7675 | const ltz = try self.buildFloatCmp(.lt, inst_ty, .{ lhs, zero }); |
| 7307 | return self.builder.buildSelect(ltz, c, a, ""); | 7676 | return self.wip.select(ltz, c, a, ""); |
| 7308 | } | 7677 | } |
| 7309 | if (scalar_ty.isSignedInt(mod)) { | 7678 | if (scalar_ty.isSignedInt(mod)) { |
| 7310 | const scalar_bit_size_minus_one = scalar_ty.bitSize(mod) - 1; | 7679 | const bit_size_minus_one = try o.builder.splatValue(inst_llvm_ty, try o.builder.intConst( |
| 7311 | const bit_size_minus_one = if (inst_ty.zigTypeTag(mod) == .Vector) const_vector: { | 7680 | inst_llvm_ty.scalarType(&o.builder), |
| 7312 | const vec_len = inst_ty.vectorLen(mod); | 7681 | inst_llvm_ty.scalarBits(&o.builder) - 1, |
| 7313 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | 7682 | )); |
| 7314 | | | |
| 7315 | const shifts = try self.gpa.alloc(*llvm.Value, vec_len); | | |
| 7316 | defer self.gpa.free(shifts); | | |
| 7317 | | 7683 | |
| 7318 | @memset(shifts, scalar_llvm_ty.constInt(scalar_bit_size_minus_one, .False)); | 7684 | const rem = try self.wip.bin(.srem, lhs, rhs, ""); |
| 7319 | break :const_vector llvm.constVector(shifts.ptr, vec_len); | 7685 | const div_sign = try self.wip.bin(.xor, lhs, rhs, ""); |
| 7320 | } else inst_llvm_ty.constInt(scalar_bit_size_minus_one, .False); | 7686 | const div_sign_mask = try self.wip.bin(.ashr, div_sign, bit_size_minus_one, ""); |
| 7321 | | 7687 | const rhs_masked = try self.wip.bin(.@"and", rhs, div_sign_mask, ""); |
| 7322 | const rem = self.builder.buildSRem(lhs, rhs, ""); | 7688 | const zero = try o.builder.zeroInitValue(inst_llvm_ty); |
| 7323 | const div_sign = self.builder.buildXor(lhs, rhs, ""); | 7689 | const rem_nonzero = try self.wip.icmp(.ne, rem, zero, ""); |
| 7324 | const div_sign_mask = self.builder.buildAShr(div_sign, bit_size_minus_one, ""); | 7690 | const correction = try self.wip.select(rem_nonzero, rhs_masked, zero, ""); |
| 7325 | const rhs_masked = self.builder.buildAnd(rhs, div_sign_mask, ""); | 7691 | return self.wip.bin(.@"add nsw", rem, correction, ""); |
| 7326 | const zero = inst_llvm_ty.constNull(); | | |
| 7327 | const rem_nonzero = self.builder.buildICmp(.NE, rem, zero, ""); | | |
| 7328 | const correction = self.builder.buildSelect(rem_nonzero, rhs_masked, zero, ""); | | |
| 7329 | return self.builder.buildNSWAdd(rem, correction, ""); | | |
| 7330 | } | 7692 | } |
| 7331 | return self.builder.buildURem(lhs, rhs, ""); | 7693 | return self.wip.bin(.urem, lhs, rhs, ""); |
| 7332 | } | 7694 | } |
| 7333 | | 7695 | |
| 7334 | fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7696 | fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7335 | const o = self.dg.object; | 7697 | const o = self.dg.object; |
| 7336 | const mod = o.module; | 7698 | const mod = o.module; |
| 7337 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 7699 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -7341,49 +7703,37 @@ pub const FuncGen = struct { | ... | @@ -7341,49 +7703,37 @@ pub const FuncGen = struct { |
| 7341 | const ptr_ty = self.typeOf(bin_op.lhs); | 7703 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 7342 | const llvm_elem_ty = try o.lowerPtrElemTy(ptr_ty.childType(mod)); | 7704 | const llvm_elem_ty = try o.lowerPtrElemTy(ptr_ty.childType(mod)); |
| 7343 | switch (ptr_ty.ptrSize(mod)) { | 7705 | switch (ptr_ty.ptrSize(mod)) { |
| 7344 | .One => { | 7706 | // It's a pointer to an array, so according to LLVM we need an extra GEP index. |
| 7345 | // It's a pointer to an array, so according to LLVM we need an extra GEP index. | 7707 | .One => return self.wip.gep(.inbounds, llvm_elem_ty, ptr, &.{ |
| 7346 | const indices: [2]*llvm.Value = .{ self.context.intType(32).constNull(), offset }; | 7708 | try o.builder.intValue(try o.lowerType(Type.usize), 0), offset, |
| 7347 | return self.builder.buildInBoundsGEP(llvm_elem_ty, ptr, &indices, indices.len, ""); | 7709 | }, ""), |
| 7348 | }, | 7710 | .C, .Many => return self.wip.gep(.inbounds, llvm_elem_ty, ptr, &.{offset}, ""), |
| 7349 | .C, .Many => { | | |
| 7350 | const indices: [1]*llvm.Value = .{offset}; | | |
| 7351 | return self.builder.buildInBoundsGEP(llvm_elem_ty, ptr, &indices, indices.len, ""); | | |
| 7352 | }, | | |
| 7353 | .Slice => { | 7711 | .Slice => { |
| 7354 | const base = self.builder.buildExtractValue(ptr, 0, ""); | 7712 | const base = try self.wip.extractValue(ptr, &.{0}, ""); |
| 7355 | const indices: [1]*llvm.Value = .{offset}; | 7713 | return self.wip.gep(.inbounds, llvm_elem_ty, base, &.{offset}, ""); |
| 7356 | return self.builder.buildInBoundsGEP(llvm_elem_ty, base, &indices, indices.len, ""); | | |
| 7357 | }, | 7714 | }, |
| 7358 | } | 7715 | } |
| 7359 | } | 7716 | } |
| 7360 | | 7717 | |
| 7361 | fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 7718 | fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7362 | const o = self.dg.object; | 7719 | const o = self.dg.object; |
| 7363 | const mod = o.module; | 7720 | const mod = o.module; |
| 7364 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 7721 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 7365 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 7722 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 7366 | const ptr = try self.resolveInst(bin_op.lhs); | 7723 | const ptr = try self.resolveInst(bin_op.lhs); |
| 7367 | const offset = try self.resolveInst(bin_op.rhs); | 7724 | const offset = try self.resolveInst(bin_op.rhs); |
| 7368 | const negative_offset = self.builder.buildNeg(offset, ""); | 7725 | const negative_offset = try self.wip.neg(offset, ""); |
| 7369 | const ptr_ty = self.typeOf(bin_op.lhs); | 7726 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 7370 | const llvm_elem_ty = try o.lowerPtrElemTy(ptr_ty.childType(mod)); | 7727 | const llvm_elem_ty = try o.lowerPtrElemTy(ptr_ty.childType(mod)); |
| 7371 | switch (ptr_ty.ptrSize(mod)) { | 7728 | switch (ptr_ty.ptrSize(mod)) { |
| 7372 | .One => { | 7729 | // It's a pointer to an array, so according to LLVM we need an extra GEP index. |
| 7373 | // It's a pointer to an array, so according to LLVM we need an extra GEP index. | 7730 | .One => return self.wip.gep(.inbounds, llvm_elem_ty, ptr, &.{ |
| 7374 | const indices: [2]*llvm.Value = .{ | 7731 | try o.builder.intValue(try o.lowerType(Type.usize), 0), negative_offset, |
| 7375 | self.context.intType(32).constNull(), negative_offset, | 7732 | }, ""), |
| 7376 | }; | 7733 | .C, .Many => return self.wip.gep(.inbounds, llvm_elem_ty, ptr, &.{negative_offset}, ""), |
| 7377 | return self.builder.buildInBoundsGEP(llvm_elem_ty, ptr, &indices, indices.len, ""); | | |
| 7378 | }, | | |
| 7379 | .C, .Many => { | | |
| 7380 | const indices: [1]*llvm.Value = .{negative_offset}; | | |
| 7381 | return self.builder.buildInBoundsGEP(llvm_elem_ty, ptr, &indices, indices.len, ""); | | |
| 7382 | }, | | |
| 7383 | .Slice => { | 7734 | .Slice => { |
| 7384 | const base = self.builder.buildExtractValue(ptr, 0, ""); | 7735 | const base = try self.wip.extractValue(ptr, &.{0}, ""); |
| 7385 | const indices: [1]*llvm.Value = .{negative_offset}; | 7736 | return self.wip.gep(.inbounds, llvm_elem_ty, base, &.{negative_offset}, ""); |
| 7386 | return self.builder.buildInBoundsGEP(llvm_elem_ty, base, &indices, indices.len, ""); | | |
| 7387 | }, | 7737 | }, |
| 7388 | } | 7738 | } |
| 7389 | } | 7739 | } |
| ... | @@ -7393,7 +7743,7 @@ pub const FuncGen = struct { | ... | @@ -7393,7 +7743,7 @@ pub const FuncGen = struct { |
| 7393 | inst: Air.Inst.Index, | 7743 | inst: Air.Inst.Index, |
| 7394 | signed_intrinsic: []const u8, | 7744 | signed_intrinsic: []const u8, |
| 7395 | unsigned_intrinsic: []const u8, | 7745 | unsigned_intrinsic: []const u8, |
| 7396 | ) !?*llvm.Value { | 7746 | ) !Builder.Value { |
| 7397 | const o = self.dg.object; | 7747 | const o = self.dg.object; |
| 7398 | const mod = o.module; | 7748 | const mod = o.module; |
| 7399 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 7749 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -7408,81 +7758,123 @@ pub const FuncGen = struct { | ... | @@ -7408,81 +7758,123 @@ pub const FuncGen = struct { |
| 7408 | | 7758 | |
| 7409 | const intrinsic_name = if (scalar_ty.isSignedInt(mod)) signed_intrinsic else unsigned_intrinsic; | 7759 | const intrinsic_name = if (scalar_ty.isSignedInt(mod)) signed_intrinsic else unsigned_intrinsic; |
| 7410 | | 7760 | |
| 7411 | const llvm_lhs_ty = try o.lowerType(lhs_ty); | | |
| 7412 | const llvm_dest_ty = try o.lowerType(dest_ty); | 7761 | const llvm_dest_ty = try o.lowerType(dest_ty); |
| | 7762 | const llvm_lhs_ty = try o.lowerType(lhs_ty); |
| 7413 | | 7763 | |
| 7414 | const llvm_fn = self.getIntrinsic(intrinsic_name, &.{llvm_lhs_ty}); | 7764 | const llvm_fn = try self.getIntrinsic(intrinsic_name, &.{llvm_lhs_ty}); |
| 7415 | const result_struct = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &[_]*llvm.Value{ lhs, rhs }, 2, .Fast, .Auto, ""); | 7765 | const llvm_ret_ty = try o.builder.structType( |
| | 7766 | .normal, |
| | 7767 | &.{ llvm_lhs_ty, try llvm_lhs_ty.changeScalar(.i1, &o.builder) }, |
| | 7768 | ); |
| | 7769 | const llvm_fn_ty = try o.builder.fnType(llvm_ret_ty, &.{ llvm_lhs_ty, llvm_lhs_ty }, .normal); |
| | 7770 | const result_struct = (try self.wip.unimplemented(llvm_ret_ty, "")).finish( |
| | 7771 | self.builder.buildCall( |
| | 7772 | llvm_fn_ty.toLlvm(&o.builder), |
| | 7773 | llvm_fn, |
| | 7774 | &[_]*llvm.Value{ lhs.toLlvm(&self.wip), rhs.toLlvm(&self.wip) }, |
| | 7775 | 2, |
| | 7776 | .Fast, |
| | 7777 | .Auto, |
| | 7778 | "", |
| | 7779 | ), |
| | 7780 | &self.wip, |
| | 7781 | ); |
| 7416 | | 7782 | |
| 7417 | const result = self.builder.buildExtractValue(result_struct, 0, ""); | 7783 | const result = try self.wip.extractValue(result_struct, &.{0}, ""); |
| 7418 | const overflow_bit = self.builder.buildExtractValue(result_struct, 1, ""); | 7784 | const overflow_bit = try self.wip.extractValue(result_struct, &.{1}, ""); |
| 7419 | | 7785 | |
| 7420 | const result_index = llvmField(dest_ty, 0, mod).?.index; | 7786 | const result_index = llvmField(dest_ty, 0, mod).?.index; |
| 7421 | const overflow_index = llvmField(dest_ty, 1, mod).?.index; | 7787 | const overflow_index = llvmField(dest_ty, 1, mod).?.index; |
| 7422 | | 7788 | |
| 7423 | if (isByRef(dest_ty, mod)) { | 7789 | if (isByRef(dest_ty, mod)) { |
| 7424 | const result_alignment = dest_ty.abiAlignment(mod); | 7790 | const result_alignment = Builder.Alignment.fromByteUnits(dest_ty.abiAlignment(mod)); |
| 7425 | const alloca_inst = self.buildAlloca(llvm_dest_ty, result_alignment); | 7791 | const alloca_inst = try self.buildAlloca(llvm_dest_ty, result_alignment); |
| 7426 | { | 7792 | { |
| 7427 | const field_ptr = self.builder.buildStructGEP(llvm_dest_ty, alloca_inst, result_index, ""); | 7793 | const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, result_index, ""); |
| 7428 | const store_inst = self.builder.buildStore(result, field_ptr); | 7794 | _ = try self.wip.store(.normal, result, field_ptr, result_alignment); |
| 7429 | store_inst.setAlignment(result_alignment); | | |
| 7430 | } | 7795 | } |
| 7431 | { | 7796 | { |
| 7432 | const field_ptr = self.builder.buildStructGEP(llvm_dest_ty, alloca_inst, overflow_index, ""); | 7797 | const overflow_alignment = comptime Builder.Alignment.fromByteUnits(1); |
| 7433 | const store_inst = self.builder.buildStore(overflow_bit, field_ptr); | 7798 | const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, overflow_index, ""); |
| 7434 | store_inst.setAlignment(1); | 7799 | _ = try self.wip.store(.normal, overflow_bit, field_ptr, overflow_alignment); |
| 7435 | } | 7800 | } |
| 7436 | | 7801 | |
| 7437 | return alloca_inst; | 7802 | return alloca_inst; |
| 7438 | } | 7803 | } |
| 7439 | | 7804 | |
| 7440 | const partial = self.builder.buildInsertValue(llvm_dest_ty.getUndef(), result, result_index, ""); | 7805 | var fields: [2]Builder.Value = undefined; |
| 7441 | return self.builder.buildInsertValue(partial, overflow_bit, overflow_index, ""); | 7806 | fields[result_index] = result; |
| | 7807 | fields[overflow_index] = overflow_bit; |
| | 7808 | return self.wip.buildAggregate(llvm_dest_ty, &fields, ""); |
| 7442 | } | 7809 | } |
| 7443 | | 7810 | |
| 7444 | fn buildElementwiseCall( | 7811 | fn buildElementwiseCall( |
| 7445 | self: *FuncGen, | 7812 | self: *FuncGen, |
| 7446 | llvm_fn: *llvm.Value, | 7813 | llvm_fn: Builder.Function.Index, |
| 7447 | args_vectors: []const *llvm.Value, | 7814 | args_vectors: []const Builder.Value, |
| 7448 | result_vector: *llvm.Value, | 7815 | result_vector: Builder.Value, |
| 7449 | vector_len: usize, | 7816 | vector_len: usize, |
| 7450 | ) !*llvm.Value { | 7817 | ) !Builder.Value { |
| 7451 | const args_len = @as(c_uint, @intCast(args_vectors.len)); | 7818 | const o = self.dg.object; |
| 7452 | const llvm_i32 = self.context.intType(32); | 7819 | assert(args_vectors.len <= 3); |
| 7453 | assert(args_len <= 3); | 7820 | |
| | 7821 | const llvm_fn_ty = llvm_fn.typeOf(&o.builder); |
| | 7822 | const llvm_scalar_ty = llvm_fn_ty.functionReturn(&o.builder); |
| 7454 | | 7823 | |
| 7455 | var i: usize = 0; | 7824 | var i: usize = 0; |
| 7456 | var result = result_vector; | 7825 | var result = result_vector; |
| 7457 | while (i < vector_len) : (i += 1) { | 7826 | while (i < vector_len) : (i += 1) { |
| 7458 | const index_i32 = llvm_i32.constInt(i, .False); | 7827 | const index_i32 = try o.builder.intValue(.i32, i); |
| 7459 | | 7828 | |
| 7460 | var args: [3]*llvm.Value = undefined; | 7829 | var args: [3]*llvm.Value = undefined; |
| 7461 | for (args_vectors, 0..) |arg_vector, k| { | 7830 | for (args[0..args_vectors.len], args_vectors) |*arg_elem, arg_vector| { |
| 7462 | args[k] = self.builder.buildExtractElement(arg_vector, index_i32, ""); | 7831 | arg_elem.* = (try self.wip.extractElement(arg_vector, index_i32, "")).toLlvm(&self.wip); |
| 7463 | } | 7832 | } |
| 7464 | const result_elem = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args_len, .C, .Auto, ""); | 7833 | const result_elem = (try self.wip.unimplemented(llvm_scalar_ty, "")).finish( |
| 7465 | result = self.builder.buildInsertElement(result, result_elem, index_i32, ""); | 7834 | self.builder.buildCall( |
| | 7835 | llvm_fn_ty.toLlvm(&o.builder), |
| | 7836 | llvm_fn.toLlvm(&o.builder), |
| | 7837 | &args, |
| | 7838 | @intCast(args_vectors.len), |
| | 7839 | .C, |
| | 7840 | .Auto, |
| | 7841 | "", |
| | 7842 | ), |
| | 7843 | &self.wip, |
| | 7844 | ); |
| | 7845 | result = try self.wip.insertElement(result, result_elem, index_i32, ""); |
| 7466 | } | 7846 | } |
| 7467 | return result; | 7847 | return result; |
| 7468 | } | 7848 | } |
| 7469 | | 7849 | |
| 7470 | fn getLibcFunction( | 7850 | fn getLibcFunction( |
| 7471 | self: *FuncGen, | 7851 | self: *FuncGen, |
| 7472 | fn_name: [:0]const u8, | 7852 | fn_name: Builder.String, |
| 7473 | param_types: []const *llvm.Type, | 7853 | param_types: []const Builder.Type, |
| 7474 | return_type: *llvm.Type, | 7854 | return_type: Builder.Type, |
| 7475 | ) *llvm.Value { | 7855 | ) Allocator.Error!Builder.Function.Index { |
| 7476 | const o = self.dg.object; | 7856 | const o = self.dg.object; |
| 7477 | return o.llvm_module.getNamedFunction(fn_name.ptr) orelse b: { | 7857 | if (o.builder.getGlobal(fn_name)) |global| return switch (global.ptrConst(&o.builder).kind) { |
| 7478 | const alias = o.llvm_module.getNamedGlobalAlias(fn_name.ptr, fn_name.len); | 7858 | .alias => |alias| alias.getAliasee(&o.builder).ptrConst(&o.builder).kind.function, |
| 7479 | break :b if (alias) |a| a.getAliasee() else null; | 7859 | .function => |function| function, |
| 7480 | } orelse b: { | 7860 | else => unreachable, |
| 7481 | const params_len = @as(c_uint, @intCast(param_types.len)); | 7861 | }; |
| 7482 | const fn_type = llvm.functionType(return_type, param_types.ptr, params_len, .False); | 7862 | |
| 7483 | const f = o.llvm_module.addFunction(fn_name, fn_type); | 7863 | const fn_type = try o.builder.fnType(return_type, param_types, .normal); |
| 7484 | break :b f; | 7864 | const f = o.llvm_module.addFunction(fn_name.toSlice(&o.builder).?, fn_type.toLlvm(&o.builder)); |
| | 7865 | |
| | 7866 | var global = Builder.Global{ |
| | 7867 | .type = fn_type, |
| | 7868 | .kind = .{ .function = @enumFromInt(o.builder.functions.items.len) }, |
| | 7869 | }; |
| | 7870 | var function = Builder.Function{ |
| | 7871 | .global = @enumFromInt(o.builder.globals.count()), |
| 7485 | }; | 7872 | }; |
| | 7873 | |
| | 7874 | try o.builder.llvm.globals.append(self.gpa, f); |
| | 7875 | _ = try o.builder.addGlobal(fn_name, global); |
| | 7876 | try o.builder.functions.append(self.gpa, function); |
| | 7877 | return global.kind.function; |
| 7486 | } | 7878 | } |
| 7487 | | 7879 | |
| 7488 | /// Creates a floating point comparison by lowering to the appropriate | 7880 | /// Creates a floating point comparison by lowering to the appropriate |
| ... | @@ -7491,8 +7883,8 @@ pub const FuncGen = struct { | ... | @@ -7491,8 +7883,8 @@ pub const FuncGen = struct { |
| 7491 | self: *FuncGen, | 7883 | self: *FuncGen, |
| 7492 | pred: math.CompareOperator, | 7884 | pred: math.CompareOperator, |
| 7493 | ty: Type, | 7885 | ty: Type, |
| 7494 | params: [2]*llvm.Value, | 7886 | params: [2]Builder.Value, |
| 7495 | ) !*llvm.Value { | 7887 | ) !Builder.Value { |
| 7496 | const o = self.dg.object; | 7888 | const o = self.dg.object; |
| 7497 | const mod = o.module; | 7889 | const mod = o.module; |
| 7498 | const target = o.module.getTarget(); | 7890 | const target = o.module.getTarget(); |
| ... | @@ -7500,20 +7892,19 @@ pub const FuncGen = struct { | ... | @@ -7500,20 +7892,19 @@ pub const FuncGen = struct { |
| 7500 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | 7892 | const scalar_llvm_ty = try o.lowerType(scalar_ty); |
| 7501 | | 7893 | |
| 7502 | if (intrinsicsAllowed(scalar_ty, target)) { | 7894 | if (intrinsicsAllowed(scalar_ty, target)) { |
| 7503 | const llvm_predicate: llvm.RealPredicate = switch (pred) { | 7895 | const cond: Builder.FloatCondition = switch (pred) { |
| 7504 | .eq => .OEQ, | 7896 | .eq => .oeq, |
| 7505 | .neq => .UNE, | 7897 | .neq => .une, |
| 7506 | .lt => .OLT, | 7898 | .lt => .olt, |
| 7507 | .lte => .OLE, | 7899 | .lte => .ole, |
| 7508 | .gt => .OGT, | 7900 | .gt => .ogt, |
| 7509 | .gte => .OGE, | 7901 | .gte => .oge, |
| 7510 | }; | 7902 | }; |
| 7511 | return self.builder.buildFCmp(llvm_predicate, params[0], params[1], ""); | 7903 | return self.wip.fcmp(cond, params[0], params[1], ""); |
| 7512 | } | 7904 | } |
| 7513 | | 7905 | |
| 7514 | const float_bits = scalar_ty.floatBits(target); | 7906 | const float_bits = scalar_ty.floatBits(target); |
| 7515 | const compiler_rt_float_abbrev = compilerRtFloatAbbrev(float_bits); | 7907 | const compiler_rt_float_abbrev = compilerRtFloatAbbrev(float_bits); |
| 7516 | var fn_name_buf: [64]u8 = undefined; | | |
| 7517 | const fn_base_name = switch (pred) { | 7908 | const fn_base_name = switch (pred) { |
| 7518 | .neq => "ne", | 7909 | .neq => "ne", |
| 7519 | .eq => "eq", | 7910 | .eq => "eq", |
| ... | @@ -7522,37 +7913,50 @@ pub const FuncGen = struct { | ... | @@ -7522,37 +7913,50 @@ pub const FuncGen = struct { |
| 7522 | .gt => "gt", | 7913 | .gt => "gt", |
| 7523 | .gte => "ge", | 7914 | .gte => "ge", |
| 7524 | }; | 7915 | }; |
| 7525 | const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__{s}{s}f2", .{ | 7916 | const fn_name = try o.builder.fmt("__{s}{s}f2", .{ fn_base_name, compiler_rt_float_abbrev }); |
| 7526 | fn_base_name, compiler_rt_float_abbrev, | 7917 | |
| 7527 | }) catch unreachable; | 7918 | const libc_fn = try self.getLibcFunction( |
| 7528 | | 7919 | fn_name, |
| 7529 | const param_types = [2]*llvm.Type{ scalar_llvm_ty, scalar_llvm_ty }; | 7920 | ([1]Builder.Type{scalar_llvm_ty} ** 2)[0..], |
| 7530 | const llvm_i32 = self.context.intType(32); | 7921 | .i32, |
| 7531 | const libc_fn = self.getLibcFunction(fn_name, param_types[0..], llvm_i32); | 7922 | ); |
| 7532 | | 7923 | |
| 7533 | const zero = llvm_i32.constInt(0, .False); | 7924 | const zero = try o.builder.intConst(.i32, 0); |
| 7534 | const int_pred: llvm.IntPredicate = switch (pred) { | 7925 | const int_cond: Builder.IntegerCondition = switch (pred) { |
| 7535 | .eq => .EQ, | 7926 | .eq => .eq, |
| 7536 | .neq => .NE, | 7927 | .neq => .ne, |
| 7537 | .lt => .SLT, | 7928 | .lt => .slt, |
| 7538 | .lte => .SLE, | 7929 | .lte => .sle, |
| 7539 | .gt => .SGT, | 7930 | .gt => .sgt, |
| 7540 | .gte => .SGE, | 7931 | .gte => .sge, |
| 7541 | }; | 7932 | }; |
| 7542 | | 7933 | |
| 7543 | if (ty.zigTypeTag(mod) == .Vector) { | 7934 | if (ty.zigTypeTag(mod) == .Vector) { |
| 7544 | const vec_len = ty.vectorLen(mod); | 7935 | const vec_len = ty.vectorLen(mod); |
| 7545 | const vector_result_ty = llvm_i32.vectorType(vec_len); | 7936 | const vector_result_ty = try o.builder.vectorType(.normal, vec_len, .i32); |
| 7546 | | 7937 | |
| 7547 | var result = vector_result_ty.getUndef(); | 7938 | const init = try o.builder.poisonValue(vector_result_ty); |
| 7548 | result = try self.buildElementwiseCall(libc_fn, &params, result, vec_len); | 7939 | const result = try self.buildElementwiseCall(libc_fn, &params, init, vec_len); |
| 7549 | | 7940 | |
| 7550 | const zero_vector = self.builder.buildVectorSplat(vec_len, zero, ""); | 7941 | const zero_vector = try o.builder.splatValue(vector_result_ty, zero); |
| 7551 | return self.builder.buildICmp(int_pred, result, zero_vector, ""); | 7942 | return self.wip.icmp(int_cond, result, zero_vector, ""); |
| 7552 | } | 7943 | } |
| 7553 | | 7944 | |
| 7554 | const result = self.builder.buildCall(libc_fn.globalGetValueType(), libc_fn, &params, params.len, .C, .Auto, ""); | 7945 | const llvm_fn_ty = libc_fn.typeOf(&o.builder); |
| 7555 | return self.builder.buildICmp(int_pred, result, zero, ""); | 7946 | const llvm_params = [2]*llvm.Value{ params[0].toLlvm(&self.wip), params[1].toLlvm(&self.wip) }; |
| | 7947 | const result = (try self.wip.unimplemented( |
| | 7948 | llvm_fn_ty.functionReturn(&o.builder), |
| | 7949 | "", |
| | 7950 | )).finish(self.builder.buildCall( |
| | 7951 | libc_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 7952 | libc_fn.toLlvm(&o.builder), |
| | 7953 | &llvm_params, |
| | 7954 | llvm_params.len, |
| | 7955 | .C, |
| | 7956 | .Auto, |
| | 7957 | "", |
| | 7958 | ), &self.wip); |
| | 7959 | return self.wip.icmp(int_cond, result, zero.toValue(), ""); |
| 7556 | } | 7960 | } |
| 7557 | | 7961 | |
| 7558 | const FloatOp = enum { | 7962 | const FloatOp = enum { |
| ... | @@ -7583,7 +7987,7 @@ pub const FuncGen = struct { | ... | @@ -7583,7 +7987,7 @@ pub const FuncGen = struct { |
| 7583 | | 7987 | |
| 7584 | const FloatOpStrat = union(enum) { | 7988 | const FloatOpStrat = union(enum) { |
| 7585 | intrinsic: []const u8, | 7989 | intrinsic: []const u8, |
| 7586 | libc: [:0]const u8, | 7990 | libc: Builder.String, |
| 7587 | }; | 7991 | }; |
| 7588 | | 7992 | |
| 7589 | /// Creates a floating point operation (add, sub, fma, sqrt, exp, etc.) | 7993 | /// Creates a floating point operation (add, sub, fma, sqrt, exp, etc.) |
| ... | @@ -7594,27 +7998,25 @@ pub const FuncGen = struct { | ... | @@ -7594,27 +7998,25 @@ pub const FuncGen = struct { |
| 7594 | comptime op: FloatOp, | 7998 | comptime op: FloatOp, |
| 7595 | ty: Type, | 7999 | ty: Type, |
| 7596 | comptime params_len: usize, | 8000 | comptime params_len: usize, |
| 7597 | params: [params_len]*llvm.Value, | 8001 | params: [params_len]Builder.Value, |
| 7598 | ) !*llvm.Value { | 8002 | ) !Builder.Value { |
| 7599 | const o = self.dg.object; | 8003 | const o = self.dg.object; |
| 7600 | const mod = o.module; | 8004 | const mod = o.module; |
| 7601 | const target = mod.getTarget(); | 8005 | const target = mod.getTarget(); |
| 7602 | const scalar_ty = ty.scalarType(mod); | 8006 | const scalar_ty = ty.scalarType(mod); |
| 7603 | const llvm_ty = try o.lowerType(ty); | 8007 | const llvm_ty = try o.lowerType(ty); |
| 7604 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | | |
| 7605 | | 8008 | |
| 7606 | const intrinsics_allowed = op != .tan and intrinsicsAllowed(scalar_ty, target); | 8009 | const intrinsics_allowed = op != .tan and intrinsicsAllowed(scalar_ty, target); |
| 7607 | var fn_name_buf: [64]u8 = undefined; | | |
| 7608 | const strat: FloatOpStrat = if (intrinsics_allowed) switch (op) { | 8010 | const strat: FloatOpStrat = if (intrinsics_allowed) switch (op) { |
| 7609 | // Some operations are dedicated LLVM instructions, not available as intrinsics | 8011 | // Some operations are dedicated LLVM instructions, not available as intrinsics |
| 7610 | .neg => return self.builder.buildFNeg(params[0], ""), | 8012 | .neg => return self.wip.un(.fneg, params[0], ""), |
| 7611 | .add => return self.builder.buildFAdd(params[0], params[1], ""), | 8013 | .add => return self.wip.bin(.fadd, params[0], params[1], ""), |
| 7612 | .sub => return self.builder.buildFSub(params[0], params[1], ""), | 8014 | .sub => return self.wip.bin(.fsub, params[0], params[1], ""), |
| 7613 | .mul => return self.builder.buildFMul(params[0], params[1], ""), | 8015 | .mul => return self.wip.bin(.fmul, params[0], params[1], ""), |
| 7614 | .div => return self.builder.buildFDiv(params[0], params[1], ""), | 8016 | .div => return self.wip.bin(.fdiv, params[0], params[1], ""), |
| 7615 | .fmod => return self.builder.buildFRem(params[0], params[1], ""), | 8017 | .fmod => return self.wip.bin(.frem, params[0], params[1], ""), |
| 7616 | .fmax => return self.builder.buildMaxNum(params[0], params[1], ""), | 8018 | .fmax => return self.wip.bin(.@"llvm.maxnum.", params[0], params[1], ""), |
| 7617 | .fmin => return self.builder.buildMinNum(params[0], params[1], ""), | 8019 | .fmin => return self.wip.bin(.@"llvm.minnum.", params[0], params[1], ""), |
| 7618 | else => .{ .intrinsic = "llvm." ++ @tagName(op) }, | 8020 | else => .{ .intrinsic = "llvm." ++ @tagName(op) }, |
| 7619 | } else b: { | 8021 | } else b: { |
| 7620 | const float_bits = scalar_ty.floatBits(target); | 8022 | const float_bits = scalar_ty.floatBits(target); |
| ... | @@ -7622,26 +8024,19 @@ pub const FuncGen = struct { | ... | @@ -7622,26 +8024,19 @@ pub const FuncGen = struct { |
| 7622 | .neg => { | 8024 | .neg => { |
| 7623 | // In this case we can generate a softfloat negation by XORing the | 8025 | // In this case we can generate a softfloat negation by XORing the |
| 7624 | // bits with a constant. | 8026 | // bits with a constant. |
| 7625 | const int_llvm_ty = self.context.intType(float_bits); | 8027 | const int_ty = try o.builder.intType(@intCast(float_bits)); |
| 7626 | const one = int_llvm_ty.constInt(1, .False); | 8028 | const cast_ty = try llvm_ty.changeScalar(int_ty, &o.builder); |
| 7627 | const shift_amt = int_llvm_ty.constInt(float_bits - 1, .False); | 8029 | const sign_mask = try o.builder.splatValue( |
| 7628 | const sign_mask = one.constShl(shift_amt); | 8030 | cast_ty, |
| 7629 | const result = if (ty.zigTypeTag(mod) == .Vector) blk: { | 8031 | try o.builder.intConst(int_ty, @as(u128, 1) << @intCast(float_bits - 1)), |
| 7630 | const splat_sign_mask = self.builder.buildVectorSplat(ty.vectorLen(mod), sign_mask, ""); | 8032 | ); |
| 7631 | const cast_ty = int_llvm_ty.vectorType(ty.vectorLen(mod)); | 8033 | const bitcasted_operand = try self.wip.cast(.bitcast, params[0], cast_ty, ""); |
| 7632 | const bitcasted_operand = self.builder.buildBitCast(params[0], cast_ty, ""); | 8034 | const result = try self.wip.bin(.xor, bitcasted_operand, sign_mask, ""); |
| 7633 | break :blk self.builder.buildXor(bitcasted_operand, splat_sign_mask, ""); | 8035 | return self.wip.cast(.bitcast, result, llvm_ty, ""); |
| 7634 | } else blk: { | | |
| 7635 | const bitcasted_operand = self.builder.buildBitCast(params[0], int_llvm_ty, ""); | | |
| 7636 | break :blk self.builder.buildXor(bitcasted_operand, sign_mask, ""); | | |
| 7637 | }; | | |
| 7638 | return self.builder.buildBitCast(result, llvm_ty, ""); | | |
| 7639 | }, | | |
| 7640 | .add, .sub, .div, .mul => FloatOpStrat{ | | |
| 7641 | .libc = std.fmt.bufPrintZ(&fn_name_buf, "__{s}{s}f3", .{ | | |
| 7642 | @tagName(op), compilerRtFloatAbbrev(float_bits), | | |
| 7643 | }) catch unreachable, | | |
| 7644 | }, | 8036 | }, |
| | 8037 | .add, .sub, .div, .mul => .{ .libc = try o.builder.fmt("__{s}{s}f3", .{ |
| | 8038 | @tagName(op), compilerRtFloatAbbrev(float_bits), |
| | 8039 | }) }, |
| 7645 | .ceil, | 8040 | .ceil, |
| 7646 | .cos, | 8041 | .cos, |
| 7647 | .exp, | 8042 | .exp, |
| ... | @@ -7660,31 +8055,48 @@ pub const FuncGen = struct { | ... | @@ -7660,31 +8055,48 @@ pub const FuncGen = struct { |
| 7660 | .sqrt, | 8055 | .sqrt, |
| 7661 | .tan, | 8056 | .tan, |
| 7662 | .trunc, | 8057 | .trunc, |
| 7663 | => FloatOpStrat{ | 8058 | => .{ .libc = try o.builder.fmt("{s}{s}{s}", .{ |
| 7664 | .libc = std.fmt.bufPrintZ(&fn_name_buf, "{s}{s}{s}", .{ | 8059 | libcFloatPrefix(float_bits), @tagName(op), libcFloatSuffix(float_bits), |
| 7665 | libcFloatPrefix(float_bits), @tagName(op), libcFloatSuffix(float_bits), | 8060 | }) }, |
| 7666 | }) catch unreachable, | | |
| 7667 | }, | | |
| 7668 | }; | 8061 | }; |
| 7669 | }; | 8062 | }; |
| 7670 | | 8063 | |
| 7671 | const llvm_fn: *llvm.Value = switch (strat) { | 8064 | const llvm_fn = switch (strat) { |
| 7672 | .intrinsic => |fn_name| self.getIntrinsic(fn_name, &.{llvm_ty}), | 8065 | .intrinsic => |fn_name| try self.getIntrinsic(fn_name, &.{llvm_ty}), |
| 7673 | .libc => |fn_name| b: { | 8066 | .libc => |fn_name| b: { |
| 7674 | const param_types = [3]*llvm.Type{ scalar_llvm_ty, scalar_llvm_ty, scalar_llvm_ty }; | 8067 | const scalar_llvm_ty = llvm_ty.scalarType(&o.builder); |
| 7675 | const libc_fn = self.getLibcFunction(fn_name, param_types[0..params.len], scalar_llvm_ty); | 8068 | const libc_fn = try self.getLibcFunction( |
| | 8069 | fn_name, |
| | 8070 | ([1]Builder.Type{scalar_llvm_ty} ** 3)[0..params.len], |
| | 8071 | scalar_llvm_ty, |
| | 8072 | ); |
| 7676 | if (ty.zigTypeTag(mod) == .Vector) { | 8073 | if (ty.zigTypeTag(mod) == .Vector) { |
| 7677 | const result = llvm_ty.getUndef(); | 8074 | const result = try o.builder.poisonValue(llvm_ty); |
| 7678 | return self.buildElementwiseCall(libc_fn, &params, result, ty.vectorLen(mod)); | 8075 | return self.buildElementwiseCall(libc_fn, &params, result, ty.vectorLen(mod)); |
| 7679 | } | 8076 | } |
| 7680 | | 8077 | |
| 7681 | break :b libc_fn; | 8078 | break :b libc_fn.toLlvm(&o.builder); |
| 7682 | }, | 8079 | }, |
| 7683 | }; | 8080 | }; |
| 7684 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params_len, .C, .Auto, ""); | 8081 | const llvm_fn_ty = try o.builder.fnType( |
| | 8082 | llvm_ty, |
| | 8083 | ([1]Builder.Type{llvm_ty} ** 3)[0..params.len], |
| | 8084 | .normal, |
| | 8085 | ); |
| | 8086 | var llvm_params: [params_len]*llvm.Value = undefined; |
| | 8087 | for (&llvm_params, params) |*llvm_param, param| llvm_param.* = param.toLlvm(&self.wip); |
| | 8088 | return (try self.wip.unimplemented(llvm_ty, "")).finish(self.builder.buildCall( |
| | 8089 | llvm_fn_ty.toLlvm(&o.builder), |
| | 8090 | llvm_fn, |
| | 8091 | &llvm_params, |
| | 8092 | params_len, |
| | 8093 | .C, |
| | 8094 | .Auto, |
| | 8095 | "", |
| | 8096 | ), &self.wip); |
| 7685 | } | 8097 | } |
| 7686 | | 8098 | |
| 7687 | fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8099 | fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7688 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 8100 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 7689 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; | 8101 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 7690 | | 8102 | |
| ... | @@ -7696,7 +8108,7 @@ pub const FuncGen = struct { | ... | @@ -7696,7 +8108,7 @@ pub const FuncGen = struct { |
| 7696 | return self.buildFloatOp(.fma, ty, 3, .{ mulend1, mulend2, addend }); | 8108 | return self.buildFloatOp(.fma, ty, 3, .{ mulend1, mulend2, addend }); |
| 7697 | } | 8109 | } |
| 7698 | | 8110 | |
| 7699 | fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8111 | fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7700 | const o = self.dg.object; | 8112 | const o = self.dg.object; |
| 7701 | const mod = o.module; | 8113 | const mod = o.module; |
| 7702 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 8114 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -7706,72 +8118,67 @@ pub const FuncGen = struct { | ... | @@ -7706,72 +8118,67 @@ pub const FuncGen = struct { |
| 7706 | const rhs = try self.resolveInst(extra.rhs); | 8118 | const rhs = try self.resolveInst(extra.rhs); |
| 7707 | | 8119 | |
| 7708 | const lhs_ty = self.typeOf(extra.lhs); | 8120 | const lhs_ty = self.typeOf(extra.lhs); |
| 7709 | const rhs_ty = self.typeOf(extra.rhs); | | |
| 7710 | const lhs_scalar_ty = lhs_ty.scalarType(mod); | 8121 | const lhs_scalar_ty = lhs_ty.scalarType(mod); |
| 7711 | const rhs_scalar_ty = rhs_ty.scalarType(mod); | | |
| 7712 | | 8122 | |
| 7713 | const dest_ty = self.typeOfIndex(inst); | 8123 | const dest_ty = self.typeOfIndex(inst); |
| 7714 | const llvm_dest_ty = try o.lowerType(dest_ty); | 8124 | const llvm_dest_ty = try o.lowerType(dest_ty); |
| 7715 | | 8125 | |
| 7716 | const casted_rhs = if (rhs_scalar_ty.bitSize(mod) < lhs_scalar_ty.bitSize(mod)) | 8126 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), ""); |
| 7717 | self.builder.buildZExt(rhs, try o.lowerType(lhs_ty), "") | | |
| 7718 | else | | |
| 7719 | rhs; | | |
| 7720 | | 8127 | |
| 7721 | const result = self.builder.buildShl(lhs, casted_rhs, ""); | 8128 | const result = try self.wip.bin(.shl, lhs, casted_rhs, ""); |
| 7722 | const reconstructed = if (lhs_scalar_ty.isSignedInt(mod)) | 8129 | const reconstructed = try self.wip.bin(if (lhs_scalar_ty.isSignedInt(mod)) |
| 7723 | self.builder.buildAShr(result, casted_rhs, "") | 8130 | .ashr |
| 7724 | else | 8131 | else |
| 7725 | self.builder.buildLShr(result, casted_rhs, ""); | 8132 | .lshr, result, casted_rhs, ""); |
| 7726 | | 8133 | |
| 7727 | const overflow_bit = self.builder.buildICmp(.NE, lhs, reconstructed, ""); | 8134 | const overflow_bit = try self.wip.icmp(.ne, lhs, reconstructed, ""); |
| 7728 | | 8135 | |
| 7729 | const result_index = llvmField(dest_ty, 0, mod).?.index; | 8136 | const result_index = llvmField(dest_ty, 0, mod).?.index; |
| 7730 | const overflow_index = llvmField(dest_ty, 1, mod).?.index; | 8137 | const overflow_index = llvmField(dest_ty, 1, mod).?.index; |
| 7731 | | 8138 | |
| 7732 | if (isByRef(dest_ty, mod)) { | 8139 | if (isByRef(dest_ty, mod)) { |
| 7733 | const result_alignment = dest_ty.abiAlignment(mod); | 8140 | const result_alignment = Builder.Alignment.fromByteUnits(dest_ty.abiAlignment(mod)); |
| 7734 | const alloca_inst = self.buildAlloca(llvm_dest_ty, result_alignment); | 8141 | const alloca_inst = try self.buildAlloca(llvm_dest_ty, result_alignment); |
| 7735 | { | 8142 | { |
| 7736 | const field_ptr = self.builder.buildStructGEP(llvm_dest_ty, alloca_inst, result_index, ""); | 8143 | const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, result_index, ""); |
| 7737 | const store_inst = self.builder.buildStore(result, field_ptr); | 8144 | _ = try self.wip.store(.normal, result, field_ptr, result_alignment); |
| 7738 | store_inst.setAlignment(result_alignment); | | |
| 7739 | } | 8145 | } |
| 7740 | { | 8146 | { |
| 7741 | const field_ptr = self.builder.buildStructGEP(llvm_dest_ty, alloca_inst, overflow_index, ""); | 8147 | const field_alignment = comptime Builder.Alignment.fromByteUnits(1); |
| 7742 | const store_inst = self.builder.buildStore(overflow_bit, field_ptr); | 8148 | const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, overflow_index, ""); |
| 7743 | store_inst.setAlignment(1); | 8149 | _ = try self.wip.store(.normal, overflow_bit, field_ptr, field_alignment); |
| 7744 | } | 8150 | } |
| 7745 | | | |
| 7746 | return alloca_inst; | 8151 | return alloca_inst; |
| 7747 | } | 8152 | } |
| 7748 | | 8153 | |
| 7749 | const partial = self.builder.buildInsertValue(llvm_dest_ty.getUndef(), result, result_index, ""); | 8154 | var fields: [2]Builder.Value = undefined; |
| 7750 | return self.builder.buildInsertValue(partial, overflow_bit, overflow_index, ""); | 8155 | fields[result_index] = result; |
| | 8156 | fields[overflow_index] = overflow_bit; |
| | 8157 | return self.wip.buildAggregate(llvm_dest_ty, &fields, ""); |
| 7751 | } | 8158 | } |
| 7752 | | 8159 | |
| 7753 | fn airAnd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8160 | fn airAnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7754 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8161 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7755 | const lhs = try self.resolveInst(bin_op.lhs); | 8162 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7756 | const rhs = try self.resolveInst(bin_op.rhs); | 8163 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7757 | return self.builder.buildAnd(lhs, rhs, ""); | 8164 | return self.wip.bin(.@"and", lhs, rhs, ""); |
| 7758 | } | 8165 | } |
| 7759 | | 8166 | |
| 7760 | fn airOr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8167 | fn airOr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7761 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8168 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7762 | const lhs = try self.resolveInst(bin_op.lhs); | 8169 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7763 | const rhs = try self.resolveInst(bin_op.rhs); | 8170 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7764 | return self.builder.buildOr(lhs, rhs, ""); | 8171 | return self.wip.bin(.@"or", lhs, rhs, ""); |
| 7765 | } | 8172 | } |
| 7766 | | 8173 | |
| 7767 | fn airXor(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8174 | fn airXor(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7768 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8175 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7769 | const lhs = try self.resolveInst(bin_op.lhs); | 8176 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7770 | const rhs = try self.resolveInst(bin_op.rhs); | 8177 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7771 | return self.builder.buildXor(lhs, rhs, ""); | 8178 | return self.wip.bin(.xor, lhs, rhs, ""); |
| 7772 | } | 8179 | } |
| 7773 | | 8180 | |
| 7774 | fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8181 | fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7775 | const o = self.dg.object; | 8182 | const o = self.dg.object; |
| 7776 | const mod = o.module; | 8183 | const mod = o.module; |
| 7777 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8184 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7780,39 +8187,29 @@ pub const FuncGen = struct { | ... | @@ -7780,39 +8187,29 @@ pub const FuncGen = struct { |
| 7780 | const rhs = try self.resolveInst(bin_op.rhs); | 8187 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7781 | | 8188 | |
| 7782 | const lhs_ty = self.typeOf(bin_op.lhs); | 8189 | const lhs_ty = self.typeOf(bin_op.lhs); |
| 7783 | const rhs_ty = self.typeOf(bin_op.rhs); | | |
| 7784 | const lhs_scalar_ty = lhs_ty.scalarType(mod); | 8190 | const lhs_scalar_ty = lhs_ty.scalarType(mod); |
| 7785 | const rhs_scalar_ty = rhs_ty.scalarType(mod); | | |
| 7786 | | 8191 | |
| 7787 | const casted_rhs = if (rhs_scalar_ty.bitSize(mod) < lhs_scalar_ty.bitSize(mod)) | 8192 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), ""); |
| 7788 | self.builder.buildZExt(rhs, try o.lowerType(lhs_ty), "") | 8193 | return self.wip.bin(if (lhs_scalar_ty.isSignedInt(mod)) |
| | 8194 | .@"shl nsw" |
| 7789 | else | 8195 | else |
| 7790 | rhs; | 8196 | .@"shl nuw", lhs, casted_rhs, ""); |
| 7791 | if (lhs_scalar_ty.isSignedInt(mod)) return self.builder.buildNSWShl(lhs, casted_rhs, ""); | | |
| 7792 | return self.builder.buildNUWShl(lhs, casted_rhs, ""); | | |
| 7793 | } | 8197 | } |
| 7794 | | 8198 | |
| 7795 | fn airShl(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8199 | fn airShl(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7796 | const o = self.dg.object; | 8200 | const o = self.dg.object; |
| 7797 | const mod = o.module; | | |
| 7798 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8201 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 7799 | | 8202 | |
| 7800 | const lhs = try self.resolveInst(bin_op.lhs); | 8203 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7801 | const rhs = try self.resolveInst(bin_op.rhs); | 8204 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7802 | | 8205 | |
| 7803 | const lhs_type = self.typeOf(bin_op.lhs); | 8206 | const lhs_type = self.typeOf(bin_op.lhs); |
| 7804 | const rhs_type = self.typeOf(bin_op.rhs); | | |
| 7805 | const lhs_scalar_ty = lhs_type.scalarType(mod); | | |
| 7806 | const rhs_scalar_ty = rhs_type.scalarType(mod); | | |
| 7807 | | 8207 | |
| 7808 | const casted_rhs = if (rhs_scalar_ty.bitSize(mod) < lhs_scalar_ty.bitSize(mod)) | 8208 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_type), ""); |
| 7809 | self.builder.buildZExt(rhs, try o.lowerType(lhs_type), "") | 8209 | return self.wip.bin(.shl, lhs, casted_rhs, ""); |
| 7810 | else | | |
| 7811 | rhs; | | |
| 7812 | return self.builder.buildShl(lhs, casted_rhs, ""); | | |
| 7813 | } | 8210 | } |
| 7814 | | 8211 | |
| 7815 | fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8212 | fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7816 | const o = self.dg.object; | 8213 | const o = self.dg.object; |
| 7817 | const mod = o.module; | 8214 | const mod = o.module; |
| 7818 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8215 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7821,42 +8218,36 @@ pub const FuncGen = struct { | ... | @@ -7821,42 +8218,36 @@ pub const FuncGen = struct { |
| 7821 | const rhs = try self.resolveInst(bin_op.rhs); | 8218 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7822 | | 8219 | |
| 7823 | const lhs_ty = self.typeOf(bin_op.lhs); | 8220 | const lhs_ty = self.typeOf(bin_op.lhs); |
| 7824 | const rhs_ty = self.typeOf(bin_op.rhs); | | |
| 7825 | const lhs_scalar_ty = lhs_ty.scalarType(mod); | 8221 | const lhs_scalar_ty = lhs_ty.scalarType(mod); |
| 7826 | const rhs_scalar_ty = rhs_ty.scalarType(mod); | | |
| 7827 | const lhs_bits = lhs_scalar_ty.bitSize(mod); | 8222 | const lhs_bits = lhs_scalar_ty.bitSize(mod); |
| 7828 | | 8223 | |
| 7829 | const casted_rhs = if (rhs_scalar_ty.bitSize(mod) < lhs_bits) | 8224 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), ""); |
| 7830 | self.builder.buildZExt(rhs, lhs.typeOf(), "") | | |
| 7831 | else | | |
| 7832 | rhs; | | |
| 7833 | | 8225 | |
| 7834 | const result = if (lhs_scalar_ty.isSignedInt(mod)) | 8226 | const result = try self.wip.bin(if (lhs_scalar_ty.isSignedInt(mod)) |
| 7835 | self.builder.buildSShlSat(lhs, casted_rhs, "") | 8227 | .@"llvm.sshl.sat." |
| 7836 | else | 8228 | else |
| 7837 | self.builder.buildUShlSat(lhs, casted_rhs, ""); | 8229 | .@"llvm.ushl.sat.", lhs, casted_rhs, ""); |
| 7838 | | 8230 | |
| 7839 | // LLVM langref says "If b is (statically or dynamically) equal to or | 8231 | // LLVM langref says "If b is (statically or dynamically) equal to or |
| 7840 | // larger than the integer bit width of the arguments, the result is a | 8232 | // larger than the integer bit width of the arguments, the result is a |
| 7841 | // poison value." | 8233 | // poison value." |
| 7842 | // However Zig semantics says that saturating shift left can never produce | 8234 | // However Zig semantics says that saturating shift left can never produce |
| 7843 | // undefined; instead it saturates. | 8235 | // undefined; instead it saturates. |
| 7844 | const lhs_scalar_llvm_ty = try o.lowerType(lhs_scalar_ty); | 8236 | const lhs_llvm_ty = try o.lowerType(lhs_ty); |
| 7845 | const bits = lhs_scalar_llvm_ty.constInt(lhs_bits, .False); | 8237 | const lhs_scalar_llvm_ty = lhs_llvm_ty.scalarType(&o.builder); |
| 7846 | const lhs_max = lhs_scalar_llvm_ty.constAllOnes(); | 8238 | const bits = try o.builder.splatValue( |
| 7847 | if (rhs_ty.zigTypeTag(mod) == .Vector) { | 8239 | lhs_llvm_ty, |
| 7848 | const vec_len = rhs_ty.vectorLen(mod); | 8240 | try o.builder.intConst(lhs_scalar_llvm_ty, lhs_bits), |
| 7849 | const bits_vec = self.builder.buildVectorSplat(vec_len, bits, ""); | 8241 | ); |
| 7850 | const lhs_max_vec = self.builder.buildVectorSplat(vec_len, lhs_max, ""); | 8242 | const lhs_max = try o.builder.splatValue( |
| 7851 | const in_range = self.builder.buildICmp(.ULT, rhs, bits_vec, ""); | 8243 | lhs_llvm_ty, |
| 7852 | return self.builder.buildSelect(in_range, result, lhs_max_vec, ""); | 8244 | try o.builder.intConst(lhs_scalar_llvm_ty, -1), |
| 7853 | } else { | 8245 | ); |
| 7854 | const in_range = self.builder.buildICmp(.ULT, rhs, bits, ""); | 8246 | const in_range = try self.wip.icmp(.ult, rhs, bits, ""); |
| 7855 | return self.builder.buildSelect(in_range, result, lhs_max, ""); | 8247 | return self.wip.select(in_range, result, lhs_max, ""); |
| 7856 | } | | |
| 7857 | } | 8248 | } |
| 7858 | | 8249 | |
| 7859 | fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !?*llvm.Value { | 8250 | fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !Builder.Value { |
| 7860 | const o = self.dg.object; | 8251 | const o = self.dg.object; |
| 7861 | const mod = o.module; | 8252 | const mod = o.module; |
| 7862 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8253 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -7865,63 +8256,41 @@ pub const FuncGen = struct { | ... | @@ -7865,63 +8256,41 @@ pub const FuncGen = struct { |
| 7865 | const rhs = try self.resolveInst(bin_op.rhs); | 8256 | const rhs = try self.resolveInst(bin_op.rhs); |
| 7866 | | 8257 | |
| 7867 | const lhs_ty = self.typeOf(bin_op.lhs); | 8258 | const lhs_ty = self.typeOf(bin_op.lhs); |
| 7868 | const rhs_ty = self.typeOf(bin_op.rhs); | | |
| 7869 | const lhs_scalar_ty = lhs_ty.scalarType(mod); | 8259 | const lhs_scalar_ty = lhs_ty.scalarType(mod); |
| 7870 | const rhs_scalar_ty = rhs_ty.scalarType(mod); | | |
| 7871 | | 8260 | |
| 7872 | const casted_rhs = if (rhs_scalar_ty.bitSize(mod) < lhs_scalar_ty.bitSize(mod)) | 8261 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), ""); |
| 7873 | self.builder.buildZExt(rhs, try o.lowerType(lhs_ty), "") | | |
| 7874 | else | | |
| 7875 | rhs; | | |
| 7876 | const is_signed_int = lhs_scalar_ty.isSignedInt(mod); | 8262 | const is_signed_int = lhs_scalar_ty.isSignedInt(mod); |
| 7877 | | 8263 | |
| 7878 | if (is_exact) { | 8264 | return self.wip.bin(if (is_exact) |
| 7879 | if (is_signed_int) { | 8265 | if (is_signed_int) .@"ashr exact" else .@"lshr exact" |
| 7880 | return self.builder.buildAShrExact(lhs, casted_rhs, ""); | 8266 | else if (is_signed_int) .ashr else .lshr, lhs, casted_rhs, ""); |
| 7881 | } else { | | |
| 7882 | return self.builder.buildLShrExact(lhs, casted_rhs, ""); | | |
| 7883 | } | | |
| 7884 | } else { | | |
| 7885 | if (is_signed_int) { | | |
| 7886 | return self.builder.buildAShr(lhs, casted_rhs, ""); | | |
| 7887 | } else { | | |
| 7888 | return self.builder.buildLShr(lhs, casted_rhs, ""); | | |
| 7889 | } | | |
| 7890 | } | | |
| 7891 | } | 8267 | } |
| 7892 | | 8268 | |
| 7893 | fn airIntCast(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8269 | fn airIntCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7894 | const o = self.dg.object; | 8270 | const o = self.dg.object; |
| 7895 | const mod = o.module; | 8271 | const mod = o.module; |
| 7896 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 8272 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 7897 | const dest_ty = self.typeOfIndex(inst); | 8273 | const dest_ty = self.typeOfIndex(inst); |
| 7898 | const dest_info = dest_ty.intInfo(mod); | | |
| 7899 | const dest_llvm_ty = try o.lowerType(dest_ty); | 8274 | const dest_llvm_ty = try o.lowerType(dest_ty); |
| 7900 | const operand = try self.resolveInst(ty_op.operand); | 8275 | const operand = try self.resolveInst(ty_op.operand); |
| 7901 | const operand_ty = self.typeOf(ty_op.operand); | 8276 | const operand_ty = self.typeOf(ty_op.operand); |
| 7902 | const operand_info = operand_ty.intInfo(mod); | 8277 | const operand_info = operand_ty.intInfo(mod); |
| 7903 | | 8278 | |
| 7904 | if (operand_info.bits < dest_info.bits) { | 8279 | return self.wip.conv(switch (operand_info.signedness) { |
| 7905 | switch (operand_info.signedness) { | 8280 | .signed => .signed, |
| 7906 | .signed => return self.builder.buildSExt(operand, dest_llvm_ty, ""), | 8281 | .unsigned => .unsigned, |
| 7907 | .unsigned => return self.builder.buildZExt(operand, dest_llvm_ty, ""), | 8282 | }, operand, dest_llvm_ty, ""); |
| 7908 | } | | |
| 7909 | } else if (operand_info.bits > dest_info.bits) { | | |
| 7910 | return self.builder.buildTrunc(operand, dest_llvm_ty, ""); | | |
| 7911 | } else { | | |
| 7912 | return operand; | | |
| 7913 | } | | |
| 7914 | } | 8283 | } |
| 7915 | | 8284 | |
| 7916 | fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8285 | fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7917 | const o = self.dg.object; | 8286 | const o = self.dg.object; |
| 7918 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 8287 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 7919 | const operand = try self.resolveInst(ty_op.operand); | 8288 | const operand = try self.resolveInst(ty_op.operand); |
| 7920 | const dest_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); | 8289 | const dest_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); |
| 7921 | return self.builder.buildTrunc(operand, dest_llvm_ty, ""); | 8290 | return self.wip.cast(.trunc, operand, dest_llvm_ty, ""); |
| 7922 | } | 8291 | } |
| 7923 | | 8292 | |
| 7924 | fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8293 | fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7925 | const o = self.dg.object; | 8294 | const o = self.dg.object; |
| 7926 | const mod = o.module; | 8295 | const mod = o.module; |
| 7927 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 8296 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -7933,26 +8302,30 @@ pub const FuncGen = struct { | ... | @@ -7933,26 +8302,30 @@ pub const FuncGen = struct { |
| 7933 | const src_bits = operand_ty.floatBits(target); | 8302 | const src_bits = operand_ty.floatBits(target); |
| 7934 | | 8303 | |
| 7935 | if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) { | 8304 | if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) { |
| 7936 | const dest_llvm_ty = try o.lowerType(dest_ty); | 8305 | return self.wip.cast(.fptrunc, operand, try o.lowerType(dest_ty), ""); |
| 7937 | return self.builder.buildFPTrunc(operand, dest_llvm_ty, ""); | | |
| 7938 | } else { | 8306 | } else { |
| 7939 | const operand_llvm_ty = try o.lowerType(operand_ty); | 8307 | const operand_llvm_ty = try o.lowerType(operand_ty); |
| 7940 | const dest_llvm_ty = try o.lowerType(dest_ty); | 8308 | const dest_llvm_ty = try o.lowerType(dest_ty); |
| 7941 | | 8309 | |
| 7942 | var fn_name_buf: [64]u8 = undefined; | 8310 | const fn_name = try o.builder.fmt("__trunc{s}f{s}f2", .{ |
| 7943 | const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__trunc{s}f{s}f2", .{ | | |
| 7944 | compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits), | 8311 | compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits), |
| 7945 | }) catch unreachable; | 8312 | }); |
| 7946 | | | |
| 7947 | const params = [1]*llvm.Value{operand}; | | |
| 7948 | const param_types = [1]*llvm.Type{operand_llvm_ty}; | | |
| 7949 | const llvm_fn = self.getLibcFunction(fn_name, &param_types, dest_llvm_ty); | | |
| 7950 | | 8313 | |
| 7951 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .C, .Auto, ""); | 8314 | const llvm_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty); |
| | 8315 | const params = [1]*llvm.Value{operand.toLlvm(&self.wip)}; |
| | 8316 | return (try self.wip.unimplemented(dest_llvm_ty, "")).finish(self.builder.buildCall( |
| | 8317 | llvm_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 8318 | llvm_fn.toLlvm(&o.builder), |
| | 8319 | &params, |
| | 8320 | params.len, |
| | 8321 | .C, |
| | 8322 | .Auto, |
| | 8323 | "", |
| | 8324 | ), &self.wip); |
| 7952 | } | 8325 | } |
| 7953 | } | 8326 | } |
| 7954 | | 8327 | |
| 7955 | fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8328 | fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7956 | const o = self.dg.object; | 8329 | const o = self.dg.object; |
| 7957 | const mod = o.module; | 8330 | const mod = o.module; |
| 7958 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 8331 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -7964,36 +8337,40 @@ pub const FuncGen = struct { | ... | @@ -7964,36 +8337,40 @@ pub const FuncGen = struct { |
| 7964 | const src_bits = operand_ty.floatBits(target); | 8337 | const src_bits = operand_ty.floatBits(target); |
| 7965 | | 8338 | |
| 7966 | if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) { | 8339 | if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) { |
| 7967 | const dest_llvm_ty = try o.lowerType(dest_ty); | 8340 | return self.wip.cast(.fpext, operand, try o.lowerType(dest_ty), ""); |
| 7968 | return self.builder.buildFPExt(operand, dest_llvm_ty, ""); | | |
| 7969 | } else { | 8341 | } else { |
| 7970 | const operand_llvm_ty = try o.lowerType(operand_ty); | 8342 | const operand_llvm_ty = try o.lowerType(operand_ty); |
| 7971 | const dest_llvm_ty = try o.lowerType(dest_ty); | 8343 | const dest_llvm_ty = try o.lowerType(dest_ty); |
| 7972 | | 8344 | |
| 7973 | var fn_name_buf: [64]u8 = undefined; | 8345 | const fn_name = try o.builder.fmt("__extend{s}f{s}f2", .{ |
| 7974 | const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__extend{s}f{s}f2", .{ | | |
| 7975 | compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits), | 8346 | compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits), |
| 7976 | }) catch unreachable; | 8347 | }); |
| 7977 | | | |
| 7978 | const params = [1]*llvm.Value{operand}; | | |
| 7979 | const param_types = [1]*llvm.Type{operand_llvm_ty}; | | |
| 7980 | const llvm_fn = self.getLibcFunction(fn_name, &param_types, dest_llvm_ty); | | |
| 7981 | | 8348 | |
| 7982 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .C, .Auto, ""); | 8349 | const llvm_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty); |
| | 8350 | const params = [1]*llvm.Value{operand.toLlvm(&self.wip)}; |
| | 8351 | return (try self.wip.unimplemented(dest_llvm_ty, "")).finish(self.builder.buildCall( |
| | 8352 | llvm_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 8353 | llvm_fn.toLlvm(&o.builder), |
| | 8354 | &params, |
| | 8355 | params.len, |
| | 8356 | .C, |
| | 8357 | .Auto, |
| | 8358 | "", |
| | 8359 | ), &self.wip); |
| 7983 | } | 8360 | } |
| 7984 | } | 8361 | } |
| 7985 | | 8362 | |
| 7986 | fn airIntFromPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8363 | fn airIntFromPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7987 | const o = self.dg.object; | 8364 | const o = self.dg.object; |
| 7988 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 8365 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 7989 | const operand = try self.resolveInst(un_op); | 8366 | const operand = try self.resolveInst(un_op); |
| 7990 | const ptr_ty = self.typeOf(un_op); | 8367 | const ptr_ty = self.typeOf(un_op); |
| 7991 | const operand_ptr = self.sliceOrArrayPtr(operand, ptr_ty); | 8368 | const operand_ptr = try self.sliceOrArrayPtr(operand, ptr_ty); |
| 7992 | const dest_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); | 8369 | const dest_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); |
| 7993 | return self.builder.buildPtrToInt(operand_ptr, dest_llvm_ty, ""); | 8370 | return self.wip.cast(.ptrtoint, operand_ptr, dest_llvm_ty, ""); |
| 7994 | } | 8371 | } |
| 7995 | | 8372 | |
| 7996 | fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !*llvm.Value { | 8373 | fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7997 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 8374 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 7998 | const operand_ty = self.typeOf(ty_op.operand); | 8375 | const operand_ty = self.typeOf(ty_op.operand); |
| 7999 | const inst_ty = self.typeOfIndex(inst); | 8376 | const inst_ty = self.typeOfIndex(inst); |
| ... | @@ -8001,7 +8378,7 @@ pub const FuncGen = struct { | ... | @@ -8001,7 +8378,7 @@ pub const FuncGen = struct { |
| 8001 | return self.bitCast(operand, operand_ty, inst_ty); | 8378 | return self.bitCast(operand, operand_ty, inst_ty); |
| 8002 | } | 8379 | } |
| 8003 | | 8380 | |
| 8004 | fn bitCast(self: *FuncGen, operand: *llvm.Value, operand_ty: Type, inst_ty: Type) !*llvm.Value { | 8381 | fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) !Builder.Value { |
| 8005 | const o = self.dg.object; | 8382 | const o = self.dg.object; |
| 8006 | const mod = o.module; | 8383 | const mod = o.module; |
| 8007 | const operand_is_ref = isByRef(operand_ty, mod); | 8384 | const operand_is_ref = isByRef(operand_ty, mod); |
| ... | @@ -8013,14 +8390,14 @@ pub const FuncGen = struct { | ... | @@ -8013,14 +8390,14 @@ pub const FuncGen = struct { |
| 8013 | return operand; | 8390 | return operand; |
| 8014 | } | 8391 | } |
| 8015 | | 8392 | |
| 8016 | if (llvm_dest_ty.getTypeKind() == .Integer and | 8393 | if (llvm_dest_ty.isInteger(&o.builder) and |
| 8017 | operand.typeOf().getTypeKind() == .Integer) | 8394 | operand.typeOfWip(&self.wip).isInteger(&o.builder)) |
| 8018 | { | 8395 | { |
| 8019 | return self.builder.buildZExtOrBitCast(operand, llvm_dest_ty, ""); | 8396 | return self.wip.conv(.unsigned, operand, llvm_dest_ty, ""); |
| 8020 | } | 8397 | } |
| 8021 | | 8398 | |
| 8022 | if (operand_ty.zigTypeTag(mod) == .Int and inst_ty.isPtrAtRuntime(mod)) { | 8399 | if (operand_ty.zigTypeTag(mod) == .Int and inst_ty.isPtrAtRuntime(mod)) { |
| 8023 | return self.builder.buildIntToPtr(operand, llvm_dest_ty, ""); | 8400 | return self.wip.cast(.inttoptr, operand, llvm_dest_ty, ""); |
| 8024 | } | 8401 | } |
| 8025 | | 8402 | |
| 8026 | if (operand_ty.zigTypeTag(mod) == .Vector and inst_ty.zigTypeTag(mod) == .Array) { | 8403 | if (operand_ty.zigTypeTag(mod) == .Vector and inst_ty.zigTypeTag(mod) == .Array) { |
| ... | @@ -8028,104 +8405,97 @@ pub const FuncGen = struct { | ... | @@ -8028,104 +8405,97 @@ pub const FuncGen = struct { |
| 8028 | if (!result_is_ref) { | 8405 | if (!result_is_ref) { |
| 8029 | return self.dg.todo("implement bitcast vector to non-ref array", .{}); | 8406 | return self.dg.todo("implement bitcast vector to non-ref array", .{}); |
| 8030 | } | 8407 | } |
| 8031 | const array_ptr = self.buildAlloca(llvm_dest_ty, null); | 8408 | const array_ptr = try self.buildAlloca(llvm_dest_ty, .default); |
| 8032 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; | 8409 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; |
| 8033 | if (bitcast_ok) { | 8410 | if (bitcast_ok) { |
| 8034 | const llvm_store = self.builder.buildStore(operand, array_ptr); | 8411 | const alignment = Builder.Alignment.fromByteUnits(inst_ty.abiAlignment(mod)); |
| 8035 | llvm_store.setAlignment(inst_ty.abiAlignment(mod)); | 8412 | _ = try self.wip.store(.normal, operand, array_ptr, alignment); |
| 8036 | } else { | 8413 | } else { |
| 8037 | // If the ABI size of the element type is not evenly divisible by size in bits; | 8414 | // If the ABI size of the element type is not evenly divisible by size in bits; |
| 8038 | // a simple bitcast will not work, and we fall back to extractelement. | 8415 | // a simple bitcast will not work, and we fall back to extractelement. |
| 8039 | const llvm_usize = try o.lowerType(Type.usize); | 8416 | const llvm_usize = try o.lowerType(Type.usize); |
| 8040 | const llvm_u32 = self.context.intType(32); | 8417 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| 8041 | const zero = llvm_usize.constNull(); | | |
| 8042 | const vector_len = operand_ty.arrayLen(mod); | 8418 | const vector_len = operand_ty.arrayLen(mod); |
| 8043 | var i: u64 = 0; | 8419 | var i: u64 = 0; |
| 8044 | while (i < vector_len) : (i += 1) { | 8420 | while (i < vector_len) : (i += 1) { |
| 8045 | const index_usize = llvm_usize.constInt(i, .False); | 8421 | const elem_ptr = try self.wip.gep(.inbounds, llvm_dest_ty, array_ptr, &.{ |
| 8046 | const index_u32 = llvm_u32.constInt(i, .False); | 8422 | usize_zero, try o.builder.intValue(llvm_usize, i), |
| 8047 | const indexes: [2]*llvm.Value = .{ zero, index_usize }; | 8423 | }, ""); |
| 8048 | const elem_ptr = self.builder.buildInBoundsGEP(llvm_dest_ty, array_ptr, &indexes, indexes.len, ""); | 8424 | const elem = |
| 8049 | const elem = self.builder.buildExtractElement(operand, index_u32, ""); | 8425 | try self.wip.extractElement(operand, try o.builder.intValue(.i32, i), ""); |
| 8050 | _ = self.builder.buildStore(elem, elem_ptr); | 8426 | _ = try self.wip.store(.normal, elem, elem_ptr, .default); |
| 8051 | } | 8427 | } |
| 8052 | } | 8428 | } |
| 8053 | return array_ptr; | 8429 | return array_ptr; |
| 8054 | } else if (operand_ty.zigTypeTag(mod) == .Array and inst_ty.zigTypeTag(mod) == .Vector) { | 8430 | } else if (operand_ty.zigTypeTag(mod) == .Array and inst_ty.zigTypeTag(mod) == .Vector) { |
| 8055 | const elem_ty = operand_ty.childType(mod); | 8431 | const elem_ty = operand_ty.childType(mod); |
| 8056 | const llvm_vector_ty = try o.lowerType(inst_ty); | 8432 | const llvm_vector_ty = try o.lowerType(inst_ty); |
| 8057 | if (!operand_is_ref) { | 8433 | if (!operand_is_ref) return self.dg.todo("implement bitcast non-ref array to vector", .{}); |
| 8058 | return self.dg.todo("implement bitcast non-ref array to vector", .{}); | | |
| 8059 | } | | |
| 8060 | | 8434 | |
| 8061 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; | 8435 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; |
| 8062 | if (bitcast_ok) { | 8436 | if (bitcast_ok) { |
| 8063 | const vector = self.builder.buildLoad(llvm_vector_ty, operand, ""); | | |
| 8064 | // The array is aligned to the element's alignment, while the vector might have a completely | 8437 | // The array is aligned to the element's alignment, while the vector might have a completely |
| 8065 | // different alignment. This means we need to enforce the alignment of this load. | 8438 | // different alignment. This means we need to enforce the alignment of this load. |
| 8066 | vector.setAlignment(elem_ty.abiAlignment(mod)); | 8439 | const alignment = Builder.Alignment.fromByteUnits(elem_ty.abiAlignment(mod)); |
| 8067 | return vector; | 8440 | return self.wip.load(.normal, llvm_vector_ty, operand, alignment, ""); |
| 8068 | } else { | 8441 | } else { |
| 8069 | // If the ABI size of the element type is not evenly divisible by size in bits; | 8442 | // If the ABI size of the element type is not evenly divisible by size in bits; |
| 8070 | // a simple bitcast will not work, and we fall back to extractelement. | 8443 | // a simple bitcast will not work, and we fall back to extractelement. |
| 8071 | const array_llvm_ty = try o.lowerType(operand_ty); | 8444 | const array_llvm_ty = try o.lowerType(operand_ty); |
| 8072 | const elem_llvm_ty = try o.lowerType(elem_ty); | 8445 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 8073 | const llvm_usize = try o.lowerType(Type.usize); | 8446 | const llvm_usize = try o.lowerType(Type.usize); |
| 8074 | const llvm_u32 = self.context.intType(32); | 8447 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| 8075 | const zero = llvm_usize.constNull(); | | |
| 8076 | const vector_len = operand_ty.arrayLen(mod); | 8448 | const vector_len = operand_ty.arrayLen(mod); |
| 8077 | var vector = llvm_vector_ty.getUndef(); | 8449 | var vector = try o.builder.poisonValue(llvm_vector_ty); |
| 8078 | var i: u64 = 0; | 8450 | var i: u64 = 0; |
| 8079 | while (i < vector_len) : (i += 1) { | 8451 | while (i < vector_len) : (i += 1) { |
| 8080 | const index_usize = llvm_usize.constInt(i, .False); | 8452 | const elem_ptr = try self.wip.gep(.inbounds, array_llvm_ty, operand, &.{ |
| 8081 | const index_u32 = llvm_u32.constInt(i, .False); | 8453 | usize_zero, try o.builder.intValue(llvm_usize, i), |
| 8082 | const indexes: [2]*llvm.Value = .{ zero, index_usize }; | 8454 | }, ""); |
| 8083 | const elem_ptr = self.builder.buildInBoundsGEP(array_llvm_ty, operand, &indexes, indexes.len, ""); | 8455 | const elem = try self.wip.load(.normal, elem_llvm_ty, elem_ptr, .default, ""); |
| 8084 | const elem = self.builder.buildLoad(elem_llvm_ty, elem_ptr, ""); | 8456 | vector = |
| 8085 | vector = self.builder.buildInsertElement(vector, elem, index_u32, ""); | 8457 | try self.wip.insertElement(vector, elem, try o.builder.intValue(.i32, i), ""); |
| 8086 | } | 8458 | } |
| 8087 | | | |
| 8088 | return vector; | 8459 | return vector; |
| 8089 | } | 8460 | } |
| 8090 | } | 8461 | } |
| 8091 | | 8462 | |
| 8092 | if (operand_is_ref) { | 8463 | if (operand_is_ref) { |
| 8093 | const load_inst = self.builder.buildLoad(llvm_dest_ty, operand, ""); | 8464 | const alignment = Builder.Alignment.fromByteUnits(operand_ty.abiAlignment(mod)); |
| 8094 | load_inst.setAlignment(operand_ty.abiAlignment(mod)); | 8465 | return self.wip.load(.normal, llvm_dest_ty, operand, alignment, ""); |
| 8095 | return load_inst; | | |
| 8096 | } | 8466 | } |
| 8097 | | 8467 | |
| 8098 | if (result_is_ref) { | 8468 | if (result_is_ref) { |
| 8099 | const alignment = @max(operand_ty.abiAlignment(mod), inst_ty.abiAlignment(mod)); | 8469 | const alignment = Builder.Alignment.fromByteUnits( |
| 8100 | const result_ptr = self.buildAlloca(llvm_dest_ty, alignment); | 8470 | @max(operand_ty.abiAlignment(mod), inst_ty.abiAlignment(mod)), |
| 8101 | const store_inst = self.builder.buildStore(operand, result_ptr); | 8471 | ); |
| 8102 | store_inst.setAlignment(alignment); | 8472 | const result_ptr = try self.buildAlloca(llvm_dest_ty, alignment); |
| | 8473 | _ = try self.wip.store(.normal, operand, result_ptr, alignment); |
| 8103 | return result_ptr; | 8474 | return result_ptr; |
| 8104 | } | 8475 | } |
| 8105 | | 8476 | |
| 8106 | if (llvm_dest_ty.getTypeKind() == .Struct) { | 8477 | if (llvm_dest_ty.isStruct(&o.builder)) { |
| 8107 | // Both our operand and our result are values, not pointers, | 8478 | // Both our operand and our result are values, not pointers, |
| 8108 | // but LLVM won't let us bitcast struct values. | 8479 | // but LLVM won't let us bitcast struct values. |
| 8109 | // Therefore, we store operand to alloca, then load for result. | 8480 | // Therefore, we store operand to alloca, then load for result. |
| 8110 | const alignment = @max(operand_ty.abiAlignment(mod), inst_ty.abiAlignment(mod)); | 8481 | const alignment = Builder.Alignment.fromByteUnits( |
| 8111 | const result_ptr = self.buildAlloca(llvm_dest_ty, alignment); | 8482 | @max(operand_ty.abiAlignment(mod), inst_ty.abiAlignment(mod)), |
| 8112 | const store_inst = self.builder.buildStore(operand, result_ptr); | 8483 | ); |
| 8113 | store_inst.setAlignment(alignment); | 8484 | const result_ptr = try self.buildAlloca(llvm_dest_ty, alignment); |
| 8114 | const load_inst = self.builder.buildLoad(llvm_dest_ty, result_ptr, ""); | 8485 | _ = try self.wip.store(.normal, operand, result_ptr, alignment); |
| 8115 | load_inst.setAlignment(alignment); | 8486 | return self.wip.load(.normal, llvm_dest_ty, result_ptr, alignment, ""); |
| 8116 | return load_inst; | | |
| 8117 | } | 8487 | } |
| 8118 | | 8488 | |
| 8119 | return self.builder.buildBitCast(operand, llvm_dest_ty, ""); | 8489 | return self.wip.cast(.bitcast, operand, llvm_dest_ty, ""); |
| 8120 | } | 8490 | } |
| 8121 | | 8491 | |
| 8122 | fn airIntFromBool(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8492 | fn airIntFromBool(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8123 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 8493 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 8124 | const operand = try self.resolveInst(un_op); | 8494 | const operand = try self.resolveInst(un_op); |
| 8125 | return operand; | 8495 | return operand; |
| 8126 | } | 8496 | } |
| 8127 | | 8497 | |
| 8128 | fn airArg(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8498 | fn airArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8129 | const o = self.dg.object; | 8499 | const o = self.dg.object; |
| 8130 | const mod = o.module; | 8500 | const mod = o.module; |
| 8131 | const arg_val = self.args[self.arg_index]; | 8501 | const arg_val = self.args[self.arg_index]; |
| ... | @@ -8133,9 +8503,7 @@ pub const FuncGen = struct { | ... | @@ -8133,9 +8503,7 @@ pub const FuncGen = struct { |
| 8133 | | 8503 | |
| 8134 | const inst_ty = self.typeOfIndex(inst); | 8504 | const inst_ty = self.typeOfIndex(inst); |
| 8135 | if (o.di_builder) |dib| { | 8505 | if (o.di_builder) |dib| { |
| 8136 | if (needDbgVarWorkaround(o)) { | 8506 | if (needDbgVarWorkaround(o)) return arg_val; |
| 8137 | return arg_val; | | |
| 8138 | } | | |
| 8139 | | 8507 | |
| 8140 | const src_index = self.air.instructions.items(.data)[inst].arg.src_index; | 8508 | const src_index = self.air.instructions.items(.data)[inst].arg.src_index; |
| 8141 | const func_index = self.dg.decl.getOwnedFunctionIndex(); | 8509 | const func_index = self.dg.decl.getOwnedFunctionIndex(); |
| ... | @@ -8150,61 +8518,64 @@ pub const FuncGen = struct { | ... | @@ -8150,61 +8518,64 @@ pub const FuncGen = struct { |
| 8150 | try o.lowerDebugType(inst_ty, .full), | 8518 | try o.lowerDebugType(inst_ty, .full), |
| 8151 | true, // always preserve | 8519 | true, // always preserve |
| 8152 | 0, // flags | 8520 | 0, // flags |
| 8153 | self.arg_index, // includes +1 because 0 is return type | 8521 | @intCast(self.arg_index), // includes +1 because 0 is return type |
| 8154 | ); | 8522 | ); |
| 8155 | | 8523 | |
| 8156 | const debug_loc = llvm.getDebugLoc(lbrace_line, lbrace_col, self.di_scope.?, null); | 8524 | const debug_loc = llvm.getDebugLoc(lbrace_line, lbrace_col, self.di_scope.?, null); |
| 8157 | const insert_block = self.builder.getInsertBlock(); | 8525 | const insert_block = self.wip.cursor.block.toLlvm(&self.wip); |
| 8158 | if (isByRef(inst_ty, mod)) { | 8526 | if (isByRef(inst_ty, mod)) { |
| 8159 | _ = dib.insertDeclareAtEnd(arg_val, di_local_var, debug_loc, insert_block); | 8527 | _ = dib.insertDeclareAtEnd(arg_val.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 8160 | } else if (o.module.comp.bin_file.options.optimize_mode == .Debug) { | 8528 | } else if (o.module.comp.bin_file.options.optimize_mode == .Debug) { |
| 8161 | const alignment = inst_ty.abiAlignment(mod); | 8529 | const alignment = Builder.Alignment.fromByteUnits(inst_ty.abiAlignment(mod)); |
| 8162 | const alloca = self.buildAlloca(arg_val.typeOf(), alignment); | 8530 | const alloca = try self.buildAlloca(arg_val.typeOfWip(&self.wip), alignment); |
| 8163 | const store_inst = self.builder.buildStore(arg_val, alloca); | 8531 | _ = try self.wip.store(.normal, arg_val, alloca, alignment); |
| 8164 | store_inst.setAlignment(alignment); | 8532 | _ = dib.insertDeclareAtEnd(alloca.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 8165 | _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block); | | |
| 8166 | } else { | 8533 | } else { |
| 8167 | _ = dib.insertDbgValueIntrinsicAtEnd(arg_val, di_local_var, debug_loc, insert_block); | 8534 | _ = dib.insertDbgValueIntrinsicAtEnd(arg_val.toLlvm(&self.wip), di_local_var, debug_loc, insert_block); |
| 8168 | } | 8535 | } |
| 8169 | } | 8536 | } |
| 8170 | | 8537 | |
| 8171 | return arg_val; | 8538 | return arg_val; |
| 8172 | } | 8539 | } |
| 8173 | | 8540 | |
| 8174 | fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8541 | fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8175 | const o = self.dg.object; | 8542 | const o = self.dg.object; |
| 8176 | const mod = o.module; | 8543 | const mod = o.module; |
| 8177 | const ptr_ty = self.typeOfIndex(inst); | 8544 | const ptr_ty = self.typeOfIndex(inst); |
| 8178 | const pointee_type = ptr_ty.childType(mod); | 8545 | const pointee_type = ptr_ty.childType(mod); |
| 8179 | if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(mod)) | 8546 | if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(mod)) |
| 8180 | return o.lowerPtrToVoid(ptr_ty); | 8547 | return (try o.lowerPtrToVoid(ptr_ty)).toValue(); |
| 8181 | | 8548 | |
| 8182 | const pointee_llvm_ty = try o.lowerType(pointee_type); | 8549 | const pointee_llvm_ty = try o.lowerType(pointee_type); |
| 8183 | const alignment = ptr_ty.ptrAlignment(mod); | 8550 | const alignment = Builder.Alignment.fromByteUnits(ptr_ty.ptrAlignment(mod)); |
| 8184 | return self.buildAlloca(pointee_llvm_ty, alignment); | 8551 | return self.buildAlloca(pointee_llvm_ty, alignment); |
| 8185 | } | 8552 | } |
| 8186 | | 8553 | |
| 8187 | fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8554 | fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8188 | const o = self.dg.object; | 8555 | const o = self.dg.object; |
| 8189 | const mod = o.module; | 8556 | const mod = o.module; |
| 8190 | const ptr_ty = self.typeOfIndex(inst); | 8557 | const ptr_ty = self.typeOfIndex(inst); |
| 8191 | const ret_ty = ptr_ty.childType(mod); | 8558 | const ret_ty = ptr_ty.childType(mod); |
| 8192 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return o.lowerPtrToVoid(ptr_ty); | 8559 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) |
| 8193 | if (self.ret_ptr) |ret_ptr| return ret_ptr; | 8560 | return (try o.lowerPtrToVoid(ptr_ty)).toValue(); |
| | 8561 | if (self.ret_ptr != .none) return self.ret_ptr; |
| 8194 | const ret_llvm_ty = try o.lowerType(ret_ty); | 8562 | const ret_llvm_ty = try o.lowerType(ret_ty); |
| 8195 | return self.buildAlloca(ret_llvm_ty, ptr_ty.ptrAlignment(mod)); | 8563 | const alignment = Builder.Alignment.fromByteUnits(ptr_ty.ptrAlignment(mod)); |
| | 8564 | return self.buildAlloca(ret_llvm_ty, alignment); |
| 8196 | } | 8565 | } |
| 8197 | | 8566 | |
| 8198 | /// Use this instead of builder.buildAlloca, because this function makes sure to | 8567 | /// Use this instead of builder.buildAlloca, because this function makes sure to |
| 8199 | /// put the alloca instruction at the top of the function! | 8568 | /// put the alloca instruction at the top of the function! |
| 8200 | fn buildAlloca(self: *FuncGen, llvm_ty: *llvm.Type, alignment: ?c_uint) *llvm.Value { | 8569 | fn buildAlloca( |
| 8201 | const o = self.dg.object; | 8570 | self: *FuncGen, |
| 8202 | const mod = o.module; | 8571 | llvm_ty: Builder.Type, |
| 8203 | const target = mod.getTarget(); | 8572 | alignment: Builder.Alignment, |
| 8204 | return buildAllocaInner(self.context, self.builder, self.llvm_func, self.di_scope != null, llvm_ty, alignment, target); | 8573 | ) Allocator.Error!Builder.Value { |
| | 8574 | const target = self.dg.object.module.getTarget(); |
| | 8575 | return buildAllocaInner(&self.wip, self.di_scope != null, llvm_ty, alignment, target); |
| 8205 | } | 8576 | } |
| 8206 | | 8577 | |
| 8207 | fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !?*llvm.Value { | 8578 | fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value { |
| 8208 | const o = self.dg.object; | 8579 | const o = self.dg.object; |
| 8209 | const mod = o.module; | 8580 | const mod = o.module; |
| 8210 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8581 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -8217,25 +8588,30 @@ pub const FuncGen = struct { | ... | @@ -8217,25 +8588,30 @@ pub const FuncGen = struct { |
| 8217 | // Even if safety is disabled, we still emit a memset to undefined since it conveys | 8588 | // Even if safety is disabled, we still emit a memset to undefined since it conveys |
| 8218 | // extra information to LLVM. However, safety makes the difference between using | 8589 | // extra information to LLVM. However, safety makes the difference between using |
| 8219 | // 0xaa or actual undefined for the fill byte. | 8590 | // 0xaa or actual undefined for the fill byte. |
| 8220 | const u8_llvm_ty = self.context.intType(8); | | |
| 8221 | const fill_byte = if (safety) | 8591 | const fill_byte = if (safety) |
| 8222 | u8_llvm_ty.constInt(0xaa, .False) | 8592 | try o.builder.intConst(.i8, 0xaa) |
| 8223 | else | 8593 | else |
| 8224 | u8_llvm_ty.getUndef(); | 8594 | try o.builder.undefConst(.i8); |
| 8225 | const operand_size = operand_ty.abiSize(mod); | 8595 | const operand_size = operand_ty.abiSize(mod); |
| 8226 | const usize_llvm_ty = try o.lowerType(Type.usize); | 8596 | const usize_ty = try o.lowerType(Type.usize); |
| 8227 | const len = usize_llvm_ty.constInt(operand_size, .False); | 8597 | const len = try o.builder.intValue(usize_ty, operand_size); |
| 8228 | const dest_ptr_align = ptr_ty.ptrAlignment(mod); | 8598 | const dest_ptr_align = Builder.Alignment.fromByteUnits(ptr_ty.ptrAlignment(mod)); |
| 8229 | _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr(mod)); | 8599 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemSet( |
| | 8600 | dest_ptr.toLlvm(&self.wip), |
| | 8601 | fill_byte.toLlvm(&o.builder), |
| | 8602 | len.toLlvm(&self.wip), |
| | 8603 | @intCast(dest_ptr_align.toByteUnits() orelse 0), |
| | 8604 | ptr_ty.isVolatilePtr(mod), |
| | 8605 | ), &self.wip); |
| 8230 | if (safety and mod.comp.bin_file.options.valgrind) { | 8606 | if (safety and mod.comp.bin_file.options.valgrind) { |
| 8231 | self.valgrindMarkUndef(dest_ptr, len); | 8607 | try self.valgrindMarkUndef(dest_ptr, len); |
| 8232 | } | 8608 | } |
| 8233 | return null; | 8609 | return .none; |
| 8234 | } | 8610 | } |
| 8235 | | 8611 | |
| 8236 | const src_operand = try self.resolveInst(bin_op.rhs); | 8612 | const src_operand = try self.resolveInst(bin_op.rhs); |
| 8237 | try self.store(dest_ptr, ptr_ty, src_operand, .NotAtomic); | 8613 | try self.store(dest_ptr, ptr_ty, src_operand, .none); |
| 8238 | return null; | 8614 | return .none; |
| 8239 | } | 8615 | } |
| 8240 | | 8616 | |
| 8241 | /// As an optimization, we want to avoid unnecessary copies of isByRef=true | 8617 | /// As an optimization, we want to avoid unnecessary copies of isByRef=true |
| ... | @@ -8260,7 +8636,7 @@ pub const FuncGen = struct { | ... | @@ -8260,7 +8636,7 @@ pub const FuncGen = struct { |
| 8260 | return false; | 8636 | return false; |
| 8261 | } | 8637 | } |
| 8262 | | 8638 | |
| 8263 | fn airLoad(fg: *FuncGen, body_tail: []const Air.Inst.Index) !?*llvm.Value { | 8639 | fn airLoad(fg: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 8264 | const o = fg.dg.object; | 8640 | const o = fg.dg.object; |
| 8265 | const mod = o.module; | 8641 | const mod = o.module; |
| 8266 | const inst = body_tail[0]; | 8642 | const inst = body_tail[0]; |
| ... | @@ -8277,22 +8653,40 @@ pub const FuncGen = struct { | ... | @@ -8277,22 +8653,40 @@ pub const FuncGen = struct { |
| 8277 | return fg.load(ptr, ptr_ty); | 8653 | return fg.load(ptr, ptr_ty); |
| 8278 | } | 8654 | } |
| 8279 | | 8655 | |
| 8280 | fn airTrap(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8656 | fn airTrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8281 | _ = inst; | 8657 | _ = inst; |
| 8282 | const llvm_fn = self.getIntrinsic("llvm.trap", &.{}); | 8658 | const o = self.dg.object; |
| 8283 | _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, undefined, 0, .Cold, .Auto, ""); | 8659 | const llvm_fn = try self.getIntrinsic("llvm.trap", &.{}); |
| 8284 | _ = self.builder.buildUnreachable(); | 8660 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| 8285 | return null; | 8661 | (try o.builder.fnType(.void, &.{}, .normal)).toLlvm(&o.builder), |
| | 8662 | llvm_fn, |
| | 8663 | undefined, |
| | 8664 | 0, |
| | 8665 | .Cold, |
| | 8666 | .Auto, |
| | 8667 | "", |
| | 8668 | ), &self.wip); |
| | 8669 | _ = try self.wip.@"unreachable"(); |
| | 8670 | return .none; |
| 8286 | } | 8671 | } |
| 8287 | | 8672 | |
| 8288 | fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8673 | fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8289 | _ = inst; | 8674 | _ = inst; |
| 8290 | const llvm_fn = self.getIntrinsic("llvm.debugtrap", &.{}); | 8675 | const o = self.dg.object; |
| 8291 | _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, undefined, 0, .C, .Auto, ""); | 8676 | const llvm_fn = try self.getIntrinsic("llvm.debugtrap", &.{}); |
| 8292 | return null; | 8677 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| | 8678 | (try o.builder.fnType(.void, &.{}, .normal)).toLlvm(&o.builder), |
| | 8679 | llvm_fn, |
| | 8680 | undefined, |
| | 8681 | 0, |
| | 8682 | .C, |
| | 8683 | .Auto, |
| | 8684 | "", |
| | 8685 | ), &self.wip); |
| | 8686 | return .none; |
| 8293 | } | 8687 | } |
| 8294 | | 8688 | |
| 8295 | fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8689 | fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8296 | _ = inst; | 8690 | _ = inst; |
| 8297 | const o = self.dg.object; | 8691 | const o = self.dg.object; |
| 8298 | const mod = o.module; | 8692 | const mod = o.module; |
| ... | @@ -8300,43 +8694,61 @@ pub const FuncGen = struct { | ... | @@ -8300,43 +8694,61 @@ pub const FuncGen = struct { |
| 8300 | const target = mod.getTarget(); | 8694 | const target = mod.getTarget(); |
| 8301 | if (!target_util.supportsReturnAddress(target)) { | 8695 | if (!target_util.supportsReturnAddress(target)) { |
| 8302 | // https://github.com/ziglang/zig/issues/11946 | 8696 | // https://github.com/ziglang/zig/issues/11946 |
| 8303 | return llvm_usize.constNull(); | 8697 | return o.builder.intValue(llvm_usize, 0); |
| 8304 | } | 8698 | } |
| 8305 | | 8699 | |
| 8306 | const llvm_i32 = self.context.intType(32); | 8700 | const llvm_fn = try self.getIntrinsic("llvm.returnaddress", &.{}); |
| 8307 | const llvm_fn = self.getIntrinsic("llvm.returnaddress", &.{}); | 8701 | const params = [_]*llvm.Value{ |
| 8308 | const params = [_]*llvm.Value{llvm_i32.constNull()}; | 8702 | (try o.builder.intConst(.i32, 0)).toLlvm(&o.builder), |
| 8309 | const ptr_val = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .Fast, .Auto, ""); | 8703 | }; |
| 8310 | return self.builder.buildPtrToInt(ptr_val, llvm_usize, ""); | 8704 | const ptr_val = (try self.wip.unimplemented(.ptr, "")).finish(self.builder.buildCall( |
| | 8705 | (try o.builder.fnType(.ptr, &.{.i32}, .normal)).toLlvm(&o.builder), |
| | 8706 | llvm_fn, |
| | 8707 | &params, |
| | 8708 | params.len, |
| | 8709 | .Fast, |
| | 8710 | .Auto, |
| | 8711 | "", |
| | 8712 | ), &self.wip); |
| | 8713 | return self.wip.cast(.ptrtoint, ptr_val, llvm_usize, ""); |
| 8311 | } | 8714 | } |
| 8312 | | 8715 | |
| 8313 | fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8716 | fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8314 | _ = inst; | 8717 | _ = inst; |
| 8315 | const o = self.dg.object; | 8718 | const o = self.dg.object; |
| 8316 | const llvm_i32 = self.context.intType(32); | | |
| 8317 | const llvm_fn_name = "llvm.frameaddress.p0"; | 8719 | const llvm_fn_name = "llvm.frameaddress.p0"; |
| 8318 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { | 8720 | const llvm_fn = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { |
| 8319 | const llvm_p0i8 = self.context.pointerType(0); | 8721 | const fn_type = try o.builder.fnType(.ptr, &.{.i32}, .normal); |
| 8320 | const param_types = [_]*llvm.Type{llvm_i32}; | 8722 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type.toLlvm(&o.builder)); |
| 8321 | const fn_type = llvm.functionType(llvm_p0i8, &param_types, param_types.len, .False); | | |
| 8322 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type); | | |
| 8323 | }; | 8723 | }; |
| | 8724 | const llvm_fn_ty = try o.builder.fnType(.ptr, &.{.i32}, .normal); |
| 8324 | | 8725 | |
| 8325 | const params = [_]*llvm.Value{llvm_i32.constNull()}; | 8726 | const params = [_]*llvm.Value{ |
| 8326 | const ptr_val = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .Fast, .Auto, ""); | 8727 | (try o.builder.intConst(.i32, 0)).toLlvm(&o.builder), |
| 8327 | const llvm_usize = try o.lowerType(Type.usize); | 8728 | }; |
| 8328 | return self.builder.buildPtrToInt(ptr_val, llvm_usize, ""); | 8729 | const ptr_val = (try self.wip.unimplemented(llvm_fn_ty.functionReturn(&o.builder), "")).finish( |
| | 8730 | self.builder.buildCall( |
| | 8731 | llvm_fn_ty.toLlvm(&o.builder), |
| | 8732 | llvm_fn, |
| | 8733 | &params, |
| | 8734 | params.len, |
| | 8735 | .Fast, |
| | 8736 | .Auto, |
| | 8737 | "", |
| | 8738 | ), |
| | 8739 | &self.wip, |
| | 8740 | ); |
| | 8741 | return self.wip.cast(.ptrtoint, ptr_val, try o.lowerType(Type.usize), ""); |
| 8329 | } | 8742 | } |
| 8330 | | 8743 | |
| 8331 | fn airFence(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8744 | fn airFence(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8332 | const atomic_order = self.air.instructions.items(.data)[inst].fence; | 8745 | const atomic_order = self.air.instructions.items(.data)[inst].fence; |
| 8333 | const llvm_memory_order = toLlvmAtomicOrdering(atomic_order); | 8746 | const ordering = toLlvmAtomicOrdering(atomic_order); |
| 8334 | const single_threaded = llvm.Bool.fromBool(self.single_threaded); | 8747 | _ = try self.wip.fence(self.sync_scope, ordering); |
| 8335 | _ = self.builder.buildFence(llvm_memory_order, single_threaded, ""); | 8748 | return .none; |
| 8336 | return null; | | |
| 8337 | } | 8749 | } |
| 8338 | | 8750 | |
| 8339 | fn airCmpxchg(self: *FuncGen, inst: Air.Inst.Index, is_weak: bool) !?*llvm.Value { | 8751 | fn airCmpxchg(self: *FuncGen, inst: Air.Inst.Index, is_weak: bool) !Builder.Value { |
| 8340 | const o = self.dg.object; | 8752 | const o = self.dg.object; |
| 8341 | const mod = o.module; | 8753 | const mod = o.module; |
| 8342 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 8754 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -8345,46 +8757,51 @@ pub const FuncGen = struct { | ... | @@ -8345,46 +8757,51 @@ pub const FuncGen = struct { |
| 8345 | var expected_value = try self.resolveInst(extra.expected_value); | 8757 | var expected_value = try self.resolveInst(extra.expected_value); |
| 8346 | var new_value = try self.resolveInst(extra.new_value); | 8758 | var new_value = try self.resolveInst(extra.new_value); |
| 8347 | const operand_ty = self.typeOf(extra.ptr).childType(mod); | 8759 | const operand_ty = self.typeOf(extra.ptr).childType(mod); |
| 8348 | const opt_abi_ty = o.getAtomicAbiType(operand_ty, false); | 8760 | const llvm_operand_ty = try o.lowerType(operand_ty); |
| 8349 | if (opt_abi_ty) |abi_ty| { | 8761 | const llvm_abi_ty = try o.getAtomicAbiType(operand_ty, false); |
| | 8762 | if (llvm_abi_ty != .none) { |
| 8350 | // operand needs widening and truncating | 8763 | // operand needs widening and truncating |
| 8351 | if (operand_ty.isSignedInt(mod)) { | 8764 | const signedness: Builder.Function.Instruction.Cast.Signedness = |
| 8352 | expected_value = self.builder.buildSExt(expected_value, abi_ty, ""); | 8765 | if (operand_ty.isSignedInt(mod)) .signed else .unsigned; |
| 8353 | new_value = self.builder.buildSExt(new_value, abi_ty, ""); | 8766 | expected_value = try self.wip.conv(signedness, expected_value, llvm_abi_ty, ""); |
| 8354 | } else { | 8767 | new_value = try self.wip.conv(signedness, new_value, llvm_abi_ty, ""); |
| 8355 | expected_value = self.builder.buildZExt(expected_value, abi_ty, ""); | | |
| 8356 | new_value = self.builder.buildZExt(new_value, abi_ty, ""); | | |
| 8357 | } | | |
| 8358 | } | 8768 | } |
| 8359 | const result = self.builder.buildAtomicCmpXchg( | 8769 | |
| 8360 | ptr, | 8770 | const llvm_result_ty = try o.builder.structType(.normal, &.{ |
| 8361 | expected_value, | 8771 | if (llvm_abi_ty != .none) llvm_abi_ty else llvm_operand_ty, |
| 8362 | new_value, | 8772 | .i1, |
| 8363 | toLlvmAtomicOrdering(extra.successOrder()), | 8773 | }); |
| 8364 | toLlvmAtomicOrdering(extra.failureOrder()), | 8774 | const result = (try self.wip.unimplemented(llvm_result_ty, "")).finish( |
| 8365 | llvm.Bool.fromBool(self.single_threaded), | 8775 | self.builder.buildAtomicCmpXchg( |
| | 8776 | ptr.toLlvm(&self.wip), |
| | 8777 | expected_value.toLlvm(&self.wip), |
| | 8778 | new_value.toLlvm(&self.wip), |
| | 8779 | @enumFromInt(@intFromEnum(toLlvmAtomicOrdering(extra.successOrder()))), |
| | 8780 | @enumFromInt(@intFromEnum(toLlvmAtomicOrdering(extra.failureOrder()))), |
| | 8781 | llvm.Bool.fromBool(self.sync_scope == .singlethread), |
| | 8782 | ), |
| | 8783 | &self.wip, |
| 8366 | ); | 8784 | ); |
| 8367 | result.setWeak(llvm.Bool.fromBool(is_weak)); | 8785 | result.toLlvm(&self.wip).setWeak(llvm.Bool.fromBool(is_weak)); |
| 8368 | | 8786 | |
| 8369 | const optional_ty = self.typeOfIndex(inst); | 8787 | const optional_ty = self.typeOfIndex(inst); |
| 8370 | | 8788 | |
| 8371 | var payload = self.builder.buildExtractValue(result, 0, ""); | 8789 | var payload = try self.wip.extractValue(result, &.{0}, ""); |
| 8372 | if (opt_abi_ty != null) { | 8790 | if (llvm_abi_ty != .none) payload = try self.wip.cast(.trunc, payload, llvm_operand_ty, ""); |
| 8373 | payload = self.builder.buildTrunc(payload, try o.lowerType(operand_ty), ""); | 8791 | const success_bit = try self.wip.extractValue(result, &.{1}, ""); |
| 8374 | } | | |
| 8375 | const success_bit = self.builder.buildExtractValue(result, 1, ""); | | |
| 8376 | | 8792 | |
| 8377 | if (optional_ty.optionalReprIsPayload(mod)) { | 8793 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 8378 | return self.builder.buildSelect(success_bit, payload.typeOf().constNull(), payload, ""); | 8794 | const zero = try o.builder.zeroInitValue(payload.typeOfWip(&self.wip)); |
| | 8795 | return self.wip.select(success_bit, zero, payload, ""); |
| 8379 | } | 8796 | } |
| 8380 | | 8797 | |
| 8381 | comptime assert(optional_layout_version == 3); | 8798 | comptime assert(optional_layout_version == 3); |
| 8382 | | 8799 | |
| 8383 | const non_null_bit = self.builder.buildNot(success_bit, ""); | 8800 | const non_null_bit = try self.wip.not(success_bit, ""); |
| 8384 | return buildOptional(self, optional_ty, payload, non_null_bit); | 8801 | return buildOptional(self, optional_ty, payload, non_null_bit); |
| 8385 | } | 8802 | } |
| 8386 | | 8803 | |
| 8387 | fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8804 | fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8388 | const o = self.dg.object; | 8805 | const o = self.dg.object; |
| 8389 | const mod = o.module; | 8806 | const mod = o.module; |
| 8390 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 8807 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| ... | @@ -8397,120 +8814,146 @@ pub const FuncGen = struct { | ... | @@ -8397,120 +8814,146 @@ pub const FuncGen = struct { |
| 8397 | const is_float = operand_ty.isRuntimeFloat(); | 8814 | const is_float = operand_ty.isRuntimeFloat(); |
| 8398 | const op = toLlvmAtomicRmwBinOp(extra.op(), is_signed_int, is_float); | 8815 | const op = toLlvmAtomicRmwBinOp(extra.op(), is_signed_int, is_float); |
| 8399 | const ordering = toLlvmAtomicOrdering(extra.ordering()); | 8816 | const ordering = toLlvmAtomicOrdering(extra.ordering()); |
| 8400 | const single_threaded = llvm.Bool.fromBool(self.single_threaded); | 8817 | const single_threaded = llvm.Bool.fromBool(self.sync_scope == .singlethread); |
| 8401 | const opt_abi_ty = o.getAtomicAbiType(operand_ty, op == .Xchg); | 8818 | const llvm_abi_ty = try o.getAtomicAbiType(operand_ty, op == .Xchg); |
| 8402 | if (opt_abi_ty) |abi_ty| { | 8819 | const llvm_operand_ty = try o.lowerType(operand_ty); |
| | 8820 | if (llvm_abi_ty != .none) { |
| 8403 | // operand needs widening and truncating or bitcasting. | 8821 | // operand needs widening and truncating or bitcasting. |
| 8404 | const casted_operand = if (is_float) | 8822 | const casted_operand = try self.wip.cast( |
| 8405 | self.builder.buildBitCast(operand, abi_ty, "") | 8823 | if (is_float) .bitcast else if (is_signed_int) .sext else .zext, |
| 8406 | else if (is_signed_int) | 8824 | @enumFromInt(@intFromEnum(operand)), |
| 8407 | self.builder.buildSExt(operand, abi_ty, "") | 8825 | llvm_abi_ty, |
| 8408 | else | 8826 | "", |
| 8409 | self.builder.buildZExt(operand, abi_ty, ""); | 8827 | ); |
| 8410 | | 8828 | |
| 8411 | const uncasted_result = self.builder.buildAtomicRmw( | 8829 | const uncasted_result = (try self.wip.unimplemented(llvm_abi_ty, "")).finish( |
| 8412 | op, | 8830 | self.builder.buildAtomicRmw( |
| 8413 | ptr, | 8831 | op, |
| 8414 | casted_operand, | 8832 | ptr.toLlvm(&self.wip), |
| 8415 | ordering, | 8833 | casted_operand.toLlvm(&self.wip), |
| 8416 | single_threaded, | 8834 | @enumFromInt(@intFromEnum(ordering)), |
| | 8835 | single_threaded, |
| | 8836 | ), |
| | 8837 | &self.wip, |
| 8417 | ); | 8838 | ); |
| 8418 | const operand_llvm_ty = try o.lowerType(operand_ty); | 8839 | |
| 8419 | if (is_float) { | 8840 | if (is_float) { |
| 8420 | return self.builder.buildBitCast(uncasted_result, operand_llvm_ty, ""); | 8841 | return self.wip.cast(.bitcast, uncasted_result, llvm_operand_ty, ""); |
| 8421 | } else { | 8842 | } else { |
| 8422 | return self.builder.buildTrunc(uncasted_result, operand_llvm_ty, ""); | 8843 | return self.wip.cast(.trunc, uncasted_result, llvm_operand_ty, ""); |
| 8423 | } | 8844 | } |
| 8424 | } | 8845 | } |
| 8425 | | 8846 | |
| 8426 | if (operand.typeOf().getTypeKind() != .Pointer) { | 8847 | if (!llvm_operand_ty.isPointer(&o.builder)) { |
| 8427 | return self.builder.buildAtomicRmw(op, ptr, operand, ordering, single_threaded); | 8848 | return (try self.wip.unimplemented(llvm_operand_ty, "")).finish( |
| | 8849 | self.builder.buildAtomicRmw( |
| | 8850 | op, |
| | 8851 | ptr.toLlvm(&self.wip), |
| | 8852 | operand.toLlvm(&self.wip), |
| | 8853 | @enumFromInt(@intFromEnum(ordering)), |
| | 8854 | single_threaded, |
| | 8855 | ), |
| | 8856 | &self.wip, |
| | 8857 | ); |
| 8428 | } | 8858 | } |
| 8429 | | 8859 | |
| 8430 | // It's a pointer but we need to treat it as an int. | 8860 | // It's a pointer but we need to treat it as an int. |
| 8431 | const usize_llvm_ty = try o.lowerType(Type.usize); | 8861 | const llvm_usize = try o.lowerType(Type.usize); |
| 8432 | const casted_operand = self.builder.buildPtrToInt(operand, usize_llvm_ty, ""); | 8862 | const casted_operand = try self.wip.cast(.ptrtoint, operand, llvm_usize, ""); |
| 8433 | const uncasted_result = self.builder.buildAtomicRmw( | 8863 | const uncasted_result = (try self.wip.unimplemented(llvm_usize, "")).finish( |
| 8434 | op, | 8864 | self.builder.buildAtomicRmw( |
| 8435 | ptr, | 8865 | op, |
| 8436 | casted_operand, | 8866 | ptr.toLlvm(&self.wip), |
| 8437 | ordering, | 8867 | casted_operand.toLlvm(&self.wip), |
| 8438 | single_threaded, | 8868 | @enumFromInt(@intFromEnum(ordering)), |
| | 8869 | single_threaded, |
| | 8870 | ), |
| | 8871 | &self.wip, |
| 8439 | ); | 8872 | ); |
| 8440 | const operand_llvm_ty = try o.lowerType(operand_ty); | 8873 | return self.wip.cast(.inttoptr, uncasted_result, llvm_operand_ty, ""); |
| 8441 | return self.builder.buildIntToPtr(uncasted_result, operand_llvm_ty, ""); | | |
| 8442 | } | 8874 | } |
| 8443 | | 8875 | |
| 8444 | fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 8876 | fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8445 | const o = self.dg.object; | 8877 | const o = self.dg.object; |
| 8446 | const mod = o.module; | 8878 | const mod = o.module; |
| 8447 | const atomic_load = self.air.instructions.items(.data)[inst].atomic_load; | 8879 | const atomic_load = self.air.instructions.items(.data)[inst].atomic_load; |
| 8448 | const ptr = try self.resolveInst(atomic_load.ptr); | 8880 | const ptr = try self.resolveInst(atomic_load.ptr); |
| 8449 | const ptr_ty = self.typeOf(atomic_load.ptr); | 8881 | const ptr_ty = self.typeOf(atomic_load.ptr); |
| 8450 | const ptr_info = ptr_ty.ptrInfo(mod); | 8882 | const info = ptr_ty.ptrInfo(mod); |
| 8451 | const elem_ty = ptr_info.child.toType(); | 8883 | const elem_ty = info.child.toType(); |
| 8452 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) | 8884 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; |
| 8453 | return null; | | |
| 8454 | const ordering = toLlvmAtomicOrdering(atomic_load.order); | 8885 | const ordering = toLlvmAtomicOrdering(atomic_load.order); |
| 8455 | const opt_abi_llvm_ty = o.getAtomicAbiType(elem_ty, false); | 8886 | const llvm_abi_ty = try o.getAtomicAbiType(elem_ty, false); |
| 8456 | const ptr_alignment = @as(u32, @intCast(ptr_info.flags.alignment.toByteUnitsOptional() orelse | 8887 | const ptr_alignment = Builder.Alignment.fromByteUnits( |
| 8457 | ptr_info.child.toType().abiAlignment(mod))); | 8888 | info.flags.alignment.toByteUnitsOptional() orelse info.child.toType().abiAlignment(mod), |
| 8458 | const ptr_volatile = llvm.Bool.fromBool(ptr_info.flags.is_volatile); | 8889 | ); |
| | 8890 | const ptr_kind: Builder.MemoryAccessKind = switch (info.flags.is_volatile) { |
| | 8891 | false => .normal, |
| | 8892 | true => .@"volatile", |
| | 8893 | }; |
| 8459 | const elem_llvm_ty = try o.lowerType(elem_ty); | 8894 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 8460 | | 8895 | |
| 8461 | if (opt_abi_llvm_ty) |abi_llvm_ty| { | 8896 | if (llvm_abi_ty != .none) { |
| 8462 | // operand needs widening and truncating | 8897 | // operand needs widening and truncating |
| 8463 | const load_inst = self.builder.buildLoad(abi_llvm_ty, ptr, ""); | 8898 | const loaded = try self.wip.loadAtomic( |
| 8464 | load_inst.setAlignment(ptr_alignment); | 8899 | ptr_kind, |
| 8465 | load_inst.setVolatile(ptr_volatile); | 8900 | llvm_abi_ty, |
| 8466 | load_inst.setOrdering(ordering); | 8901 | ptr, |
| 8467 | return self.builder.buildTrunc(load_inst, elem_llvm_ty, ""); | 8902 | self.sync_scope, |
| | 8903 | ordering, |
| | 8904 | ptr_alignment, |
| | 8905 | "", |
| | 8906 | ); |
| | 8907 | return self.wip.cast(.trunc, loaded, elem_llvm_ty, ""); |
| 8468 | } | 8908 | } |
| 8469 | const load_inst = self.builder.buildLoad(elem_llvm_ty, ptr, ""); | 8909 | return self.wip.loadAtomic( |
| 8470 | load_inst.setAlignment(ptr_alignment); | 8910 | ptr_kind, |
| 8471 | load_inst.setVolatile(ptr_volatile); | 8911 | elem_llvm_ty, |
| 8472 | load_inst.setOrdering(ordering); | 8912 | ptr, |
| 8473 | return load_inst; | 8913 | self.sync_scope, |
| | 8914 | ordering, |
| | 8915 | ptr_alignment, |
| | 8916 | "", |
| | 8917 | ); |
| 8474 | } | 8918 | } |
| 8475 | | 8919 | |
| 8476 | fn airAtomicStore( | 8920 | fn airAtomicStore( |
| 8477 | self: *FuncGen, | 8921 | self: *FuncGen, |
| 8478 | inst: Air.Inst.Index, | 8922 | inst: Air.Inst.Index, |
| 8479 | ordering: llvm.AtomicOrdering, | 8923 | ordering: Builder.AtomicOrdering, |
| 8480 | ) !?*llvm.Value { | 8924 | ) !Builder.Value { |
| 8481 | const o = self.dg.object; | 8925 | const o = self.dg.object; |
| 8482 | const mod = o.module; | 8926 | const mod = o.module; |
| 8483 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8927 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 8484 | const ptr_ty = self.typeOf(bin_op.lhs); | 8928 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 8485 | const operand_ty = ptr_ty.childType(mod); | 8929 | const operand_ty = ptr_ty.childType(mod); |
| 8486 | if (!operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return null; | 8930 | if (!operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return .none; |
| 8487 | const ptr = try self.resolveInst(bin_op.lhs); | 8931 | const ptr = try self.resolveInst(bin_op.lhs); |
| 8488 | var element = try self.resolveInst(bin_op.rhs); | 8932 | var element = try self.resolveInst(bin_op.rhs); |
| 8489 | const opt_abi_ty = o.getAtomicAbiType(operand_ty, false); | 8933 | const llvm_abi_ty = try o.getAtomicAbiType(operand_ty, false); |
| 8490 | | 8934 | |
| 8491 | if (opt_abi_ty) |abi_ty| { | 8935 | if (llvm_abi_ty != .none) { |
| 8492 | // operand needs widening | 8936 | // operand needs widening |
| 8493 | if (operand_ty.isSignedInt(mod)) { | 8937 | element = try self.wip.conv( |
| 8494 | element = self.builder.buildSExt(element, abi_ty, ""); | 8938 | if (operand_ty.isSignedInt(mod)) .signed else .unsigned, |
| 8495 | } else { | 8939 | element, |
| 8496 | element = self.builder.buildZExt(element, abi_ty, ""); | 8940 | llvm_abi_ty, |
| 8497 | } | 8941 | "", |
| | 8942 | ); |
| 8498 | } | 8943 | } |
| 8499 | try self.store(ptr, ptr_ty, element, ordering); | 8944 | try self.store(ptr, ptr_ty, element, ordering); |
| 8500 | return null; | 8945 | return .none; |
| 8501 | } | 8946 | } |
| 8502 | | 8947 | |
| 8503 | fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !?*llvm.Value { | 8948 | fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value { |
| 8504 | const o = self.dg.object; | 8949 | const o = self.dg.object; |
| 8505 | const mod = o.module; | 8950 | const mod = o.module; |
| 8506 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 8951 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 8507 | const dest_slice = try self.resolveInst(bin_op.lhs); | 8952 | const dest_slice = try self.resolveInst(bin_op.lhs); |
| 8508 | const ptr_ty = self.typeOf(bin_op.lhs); | 8953 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 8509 | const elem_ty = self.typeOf(bin_op.rhs); | 8954 | const elem_ty = self.typeOf(bin_op.rhs); |
| 8510 | const target = mod.getTarget(); | 8955 | const dest_ptr_align = Builder.Alignment.fromByteUnits(ptr_ty.ptrAlignment(mod)); |
| 8511 | const dest_ptr_align = ptr_ty.ptrAlignment(mod); | 8956 | const dest_ptr = try self.sliceOrArrayPtr(dest_slice, ptr_ty); |
| 8512 | const u8_llvm_ty = self.context.intType(8); | | |
| 8513 | const dest_ptr = self.sliceOrArrayPtr(dest_slice, ptr_ty); | | |
| 8514 | const is_volatile = ptr_ty.isVolatilePtr(mod); | 8957 | const is_volatile = ptr_ty.isVolatilePtr(mod); |
| 8515 | | 8958 | |
| 8516 | // Any WebAssembly runtime will trap when the destination pointer is out-of-bounds, regardless | 8959 | // Any WebAssembly runtime will trap when the destination pointer is out-of-bounds, regardless |
| ... | @@ -8527,20 +8970,26 @@ pub const FuncGen = struct { | ... | @@ -8527,20 +8970,26 @@ pub const FuncGen = struct { |
| 8527 | // extra information to LLVM. However, safety makes the difference between using | 8970 | // extra information to LLVM. However, safety makes the difference between using |
| 8528 | // 0xaa or actual undefined for the fill byte. | 8971 | // 0xaa or actual undefined for the fill byte. |
| 8529 | const fill_byte = if (safety) | 8972 | const fill_byte = if (safety) |
| 8530 | u8_llvm_ty.constInt(0xaa, .False) | 8973 | try o.builder.intValue(.i8, 0xaa) |
| 8531 | else | 8974 | else |
| 8532 | u8_llvm_ty.getUndef(); | 8975 | try o.builder.undefValue(.i8); |
| 8533 | const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); | 8976 | const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); |
| 8534 | if (intrinsic_len0_traps) { | 8977 | if (intrinsic_len0_traps) { |
| 8535 | try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 8978 | try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); |
| 8536 | } else { | 8979 | } else { |
| 8537 | _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 8980 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemSet( |
| | 8981 | dest_ptr.toLlvm(&self.wip), |
| | 8982 | fill_byte.toLlvm(&self.wip), |
| | 8983 | len.toLlvm(&self.wip), |
| | 8984 | @intCast(dest_ptr_align.toByteUnits() orelse 0), |
| | 8985 | is_volatile, |
| | 8986 | ), &self.wip); |
| 8538 | } | 8987 | } |
| 8539 | | 8988 | |
| 8540 | if (safety and mod.comp.bin_file.options.valgrind) { | 8989 | if (safety and mod.comp.bin_file.options.valgrind) { |
| 8541 | self.valgrindMarkUndef(dest_ptr, len); | 8990 | try self.valgrindMarkUndef(dest_ptr, len); |
| 8542 | } | 8991 | } |
| 8543 | return null; | 8992 | return .none; |
| 8544 | } | 8993 | } |
| 8545 | | 8994 | |
| 8546 | // Test if the element value is compile-time known to be a | 8995 | // Test if the element value is compile-time known to be a |
| ... | @@ -8548,18 +8997,21 @@ pub const FuncGen = struct { | ... | @@ -8548,18 +8997,21 @@ pub const FuncGen = struct { |
| 8548 | // repeating byte pattern of 0 bytes. In such case, the memset | 8997 | // repeating byte pattern of 0 bytes. In such case, the memset |
| 8549 | // intrinsic can be used. | 8998 | // intrinsic can be used. |
| 8550 | if (try elem_val.hasRepeatedByteRepr(elem_ty, mod)) |byte_val| { | 8999 | if (try elem_val.hasRepeatedByteRepr(elem_ty, mod)) |byte_val| { |
| 8551 | const fill_byte = try self.resolveValue(.{ | 9000 | const fill_byte = try self.resolveValue(.{ .ty = Type.u8, .val = byte_val }); |
| 8552 | .ty = Type.u8, | 9001 | const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); |
| 8553 | .val = byte_val, | | |
| 8554 | }); | | |
| 8555 | const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); | | |
| 8556 | | 9002 | |
| 8557 | if (intrinsic_len0_traps) { | 9003 | if (intrinsic_len0_traps) { |
| 8558 | try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 9004 | try self.safeWasmMemset(dest_ptr, fill_byte.toValue(), len, dest_ptr_align, is_volatile); |
| 8559 | } else { | 9005 | } else { |
| 8560 | _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 9006 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemSet( |
| | 9007 | dest_ptr.toLlvm(&self.wip), |
| | 9008 | fill_byte.toLlvm(&o.builder), |
| | 9009 | len.toLlvm(&self.wip), |
| | 9010 | @intCast(dest_ptr_align.toByteUnits() orelse 0), |
| | 9011 | is_volatile, |
| | 9012 | ), &self.wip); |
| 8561 | } | 9013 | } |
| 8562 | return null; | 9014 | return .none; |
| 8563 | } | 9015 | } |
| 8564 | } | 9016 | } |
| 8565 | | 9017 | |
| ... | @@ -8569,14 +9021,20 @@ pub const FuncGen = struct { | ... | @@ -8569,14 +9021,20 @@ pub const FuncGen = struct { |
| 8569 | if (elem_abi_size == 1) { | 9021 | if (elem_abi_size == 1) { |
| 8570 | // In this case we can take advantage of LLVM's intrinsic. | 9022 | // In this case we can take advantage of LLVM's intrinsic. |
| 8571 | const fill_byte = try self.bitCast(value, elem_ty, Type.u8); | 9023 | const fill_byte = try self.bitCast(value, elem_ty, Type.u8); |
| 8572 | const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); | 9024 | const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); |
| 8573 | | 9025 | |
| 8574 | if (intrinsic_len0_traps) { | 9026 | if (intrinsic_len0_traps) { |
| 8575 | try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 9027 | try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); |
| 8576 | } else { | 9028 | } else { |
| 8577 | _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 9029 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemSet( |
| | 9030 | dest_ptr.toLlvm(&self.wip), |
| | 9031 | fill_byte.toLlvm(&self.wip), |
| | 9032 | len.toLlvm(&self.wip), |
| | 9033 | @intCast(dest_ptr_align.toByteUnits() orelse 0), |
| | 9034 | is_volatile, |
| | 9035 | ), &self.wip); |
| 8578 | } | 9036 | } |
| 8579 | return null; | 9037 | return .none; |
| 8580 | } | 9038 | } |
| 8581 | | 9039 | |
| 8582 | // non-byte-sized element. lower with a loop. something like this: | 9040 | // non-byte-sized element. lower with a loop. something like this: |
| ... | @@ -8584,88 +9042,92 @@ pub const FuncGen = struct { | ... | @@ -8584,88 +9042,92 @@ pub const FuncGen = struct { |
| 8584 | // entry: | 9042 | // entry: |
| 8585 | // ... | 9043 | // ... |
| 8586 | // %end_ptr = getelementptr %ptr, %len | 9044 | // %end_ptr = getelementptr %ptr, %len |
| 8587 | // br loop | 9045 | // br %loop |
| 8588 | // loop: | 9046 | // loop: |
| 8589 | // %it_ptr = phi body %next_ptr, entry %ptr | 9047 | // %it_ptr = phi body %next_ptr, entry %ptr |
| 8590 | // %end = cmp eq %it_ptr, %end_ptr | 9048 | // %end = cmp eq %it_ptr, %end_ptr |
| 8591 | // cond_br %end body, end | 9049 | // br %end, %body, %end |
| 8592 | // body: | 9050 | // body: |
| 8593 | // store %it_ptr, %value | 9051 | // store %it_ptr, %value |
| 8594 | // %next_ptr = getelementptr %it_ptr, 1 | 9052 | // %next_ptr = getelementptr %it_ptr, 1 |
| 8595 | // br loop | 9053 | // br %loop |
| 8596 | // end: | 9054 | // end: |
| 8597 | // ... | 9055 | // ... |
| 8598 | const entry_block = self.builder.getInsertBlock(); | 9056 | const entry_block = self.wip.cursor.block; |
| 8599 | const loop_block = self.context.appendBasicBlock(self.llvm_func, "InlineMemsetLoop"); | 9057 | const loop_block = try self.wip.block(2, "InlineMemsetLoop"); |
| 8600 | const body_block = self.context.appendBasicBlock(self.llvm_func, "InlineMemsetBody"); | 9058 | const body_block = try self.wip.block(1, "InlineMemsetBody"); |
| 8601 | const end_block = self.context.appendBasicBlock(self.llvm_func, "InlineMemsetEnd"); | 9059 | const end_block = try self.wip.block(1, "InlineMemsetEnd"); |
| 8602 | | 9060 | |
| 8603 | const llvm_usize_ty = self.context.intType(target.ptrBitWidth()); | 9061 | const usize_ty = try o.lowerType(Type.usize); |
| 8604 | const len = switch (ptr_ty.ptrSize(mod)) { | 9062 | const len = switch (ptr_ty.ptrSize(mod)) { |
| 8605 | .Slice => self.builder.buildExtractValue(dest_slice, 1, ""), | 9063 | .Slice => try self.wip.extractValue(dest_slice, &.{1}, ""), |
| 8606 | .One => llvm_usize_ty.constInt(ptr_ty.childType(mod).arrayLen(mod), .False), | 9064 | .One => try o.builder.intValue(usize_ty, ptr_ty.childType(mod).arrayLen(mod)), |
| 8607 | .Many, .C => unreachable, | 9065 | .Many, .C => unreachable, |
| 8608 | }; | 9066 | }; |
| 8609 | const elem_llvm_ty = try o.lowerType(elem_ty); | 9067 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 8610 | const len_gep = [_]*llvm.Value{len}; | 9068 | const end_ptr = try self.wip.gep(.inbounds, elem_llvm_ty, dest_ptr, &.{len}, ""); |
| 8611 | const end_ptr = self.builder.buildInBoundsGEP(elem_llvm_ty, dest_ptr, &len_gep, len_gep.len, ""); | 9069 | _ = try self.wip.br(loop_block); |
| 8612 | _ = self.builder.buildBr(loop_block); | | |
| 8613 | | 9070 | |
| 8614 | self.builder.positionBuilderAtEnd(loop_block); | 9071 | self.wip.cursor = .{ .block = loop_block }; |
| 8615 | const it_ptr = self.builder.buildPhi(self.context.pointerType(0), ""); | 9072 | const it_ptr = try self.wip.phi(.ptr, ""); |
| 8616 | const end = self.builder.buildICmp(.NE, it_ptr, end_ptr, ""); | 9073 | const end = try self.wip.icmp(.ne, it_ptr.toValue(), end_ptr, ""); |
| 8617 | _ = self.builder.buildCondBr(end, body_block, end_block); | 9074 | _ = try self.wip.brCond(end, body_block, end_block); |
| 8618 | | 9075 | |
| 8619 | self.builder.positionBuilderAtEnd(body_block); | 9076 | self.wip.cursor = .{ .block = body_block }; |
| 8620 | const elem_abi_alignment = elem_ty.abiAlignment(mod); | 9077 | const elem_abi_alignment = elem_ty.abiAlignment(mod); |
| 8621 | const it_ptr_alignment = @min(elem_abi_alignment, dest_ptr_align); | 9078 | const it_ptr_alignment = Builder.Alignment.fromByteUnits( |
| | 9079 | @min(elem_abi_alignment, dest_ptr_align.toByteUnits() orelse std.math.maxInt(u64)), |
| | 9080 | ); |
| 8622 | if (isByRef(elem_ty, mod)) { | 9081 | if (isByRef(elem_ty, mod)) { |
| 8623 | _ = self.builder.buildMemCpy( | 9082 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemCpy( |
| 8624 | it_ptr, | 9083 | it_ptr.toValue().toLlvm(&self.wip), |
| 8625 | it_ptr_alignment, | 9084 | @intCast(it_ptr_alignment.toByteUnits() orelse 0), |
| 8626 | value, | 9085 | value.toLlvm(&self.wip), |
| 8627 | elem_abi_alignment, | 9086 | elem_abi_alignment, |
| 8628 | llvm_usize_ty.constInt(elem_abi_size, .False), | 9087 | (try o.builder.intConst(usize_ty, elem_abi_size)).toLlvm(&o.builder), |
| 8629 | is_volatile, | 9088 | is_volatile, |
| 8630 | ); | 9089 | ), &self.wip); |
| 8631 | } else { | 9090 | } else _ = try self.wip.store(switch (is_volatile) { |
| 8632 | const store_inst = self.builder.buildStore(value, it_ptr); | 9091 | false => .normal, |
| 8633 | store_inst.setAlignment(it_ptr_alignment); | 9092 | true => .@"volatile", |
| 8634 | store_inst.setVolatile(llvm.Bool.fromBool(is_volatile)); | 9093 | }, value, it_ptr.toValue(), it_ptr_alignment); |
| 8635 | } | 9094 | const next_ptr = try self.wip.gep(.inbounds, elem_llvm_ty, it_ptr.toValue(), &.{ |
| 8636 | const one_gep = [_]*llvm.Value{llvm_usize_ty.constInt(1, .False)}; | 9095 | try o.builder.intValue(usize_ty, 1), |
| 8637 | const next_ptr = self.builder.buildInBoundsGEP(elem_llvm_ty, it_ptr, &one_gep, one_gep.len, ""); | 9096 | }, ""); |
| 8638 | _ = self.builder.buildBr(loop_block); | 9097 | _ = try self.wip.br(loop_block); |
| 8639 | | | |
| 8640 | self.builder.positionBuilderAtEnd(end_block); | | |
| 8641 | | 9098 | |
| 8642 | const incoming_values: [2]*llvm.Value = .{ next_ptr, dest_ptr }; | 9099 | self.wip.cursor = .{ .block = end_block }; |
| 8643 | const incoming_blocks: [2]*llvm.BasicBlock = .{ body_block, entry_block }; | 9100 | try it_ptr.finish(&.{ next_ptr, dest_ptr }, &.{ body_block, entry_block }, &self.wip); |
| 8644 | it_ptr.addIncoming(&incoming_values, &incoming_blocks, 2); | 9101 | return .none; |
| 8645 | | | |
| 8646 | return null; | | |
| 8647 | } | 9102 | } |
| 8648 | | 9103 | |
| 8649 | fn safeWasmMemset( | 9104 | fn safeWasmMemset( |
| 8650 | self: *FuncGen, | 9105 | self: *FuncGen, |
| 8651 | dest_ptr: *llvm.Value, | 9106 | dest_ptr: Builder.Value, |
| 8652 | fill_byte: *llvm.Value, | 9107 | fill_byte: Builder.Value, |
| 8653 | len: *llvm.Value, | 9108 | len: Builder.Value, |
| 8654 | dest_ptr_align: u32, | 9109 | dest_ptr_align: Builder.Alignment, |
| 8655 | is_volatile: bool, | 9110 | is_volatile: bool, |
| 8656 | ) !void { | 9111 | ) !void { |
| 8657 | const llvm_usize_ty = self.context.intType(self.dg.object.target.ptrBitWidth()); | 9112 | const o = self.dg.object; |
| 8658 | const cond = try self.cmp(len, llvm_usize_ty.constInt(0, .False), Type.usize, .neq); | 9113 | const llvm_usize_ty = try o.lowerType(Type.usize); |
| 8659 | const memset_block = self.context.appendBasicBlock(self.llvm_func, "MemsetTrapSkip"); | 9114 | const cond = try self.cmp(len, try o.builder.intValue(llvm_usize_ty, 0), Type.usize, .neq); |
| 8660 | const end_block = self.context.appendBasicBlock(self.llvm_func, "MemsetTrapEnd"); | 9115 | const memset_block = try self.wip.block(1, "MemsetTrapSkip"); |
| 8661 | _ = self.builder.buildCondBr(cond, memset_block, end_block); | 9116 | const end_block = try self.wip.block(2, "MemsetTrapEnd"); |
| 8662 | self.builder.positionBuilderAtEnd(memset_block); | 9117 | _ = try self.wip.brCond(cond, memset_block, end_block); |
| 8663 | _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile); | 9118 | self.wip.cursor = .{ .block = memset_block }; |
| 8664 | _ = self.builder.buildBr(end_block); | 9119 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemSet( |
| 8665 | self.builder.positionBuilderAtEnd(end_block); | 9120 | dest_ptr.toLlvm(&self.wip), |
| | 9121 | fill_byte.toLlvm(&self.wip), |
| | 9122 | len.toLlvm(&self.wip), |
| | 9123 | @intCast(dest_ptr_align.toByteUnits() orelse 0), |
| | 9124 | is_volatile, |
| | 9125 | ), &self.wip); |
| | 9126 | _ = try self.wip.br(end_block); |
| | 9127 | self.wip.cursor = .{ .block = end_block }; |
| 8666 | } | 9128 | } |
| 8667 | | 9129 | |
| 8668 | fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9130 | fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8669 | const o = self.dg.object; | 9131 | const o = self.dg.object; |
| 8670 | const mod = o.module; | 9132 | const mod = o.module; |
| 8671 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 9133 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| ... | @@ -8673,9 +9135,9 @@ pub const FuncGen = struct { | ... | @@ -8673,9 +9135,9 @@ pub const FuncGen = struct { |
| 8673 | const dest_ptr_ty = self.typeOf(bin_op.lhs); | 9135 | const dest_ptr_ty = self.typeOf(bin_op.lhs); |
| 8674 | const src_slice = try self.resolveInst(bin_op.rhs); | 9136 | const src_slice = try self.resolveInst(bin_op.rhs); |
| 8675 | const src_ptr_ty = self.typeOf(bin_op.rhs); | 9137 | const src_ptr_ty = self.typeOf(bin_op.rhs); |
| 8676 | const src_ptr = self.sliceOrArrayPtr(src_slice, src_ptr_ty); | 9138 | const src_ptr = try self.sliceOrArrayPtr(src_slice, src_ptr_ty); |
| 8677 | const len = self.sliceOrArrayLenInBytes(dest_slice, dest_ptr_ty); | 9139 | const len = try self.sliceOrArrayLenInBytes(dest_slice, dest_ptr_ty); |
| 8678 | const dest_ptr = self.sliceOrArrayPtr(dest_slice, dest_ptr_ty); | 9140 | const dest_ptr = try self.sliceOrArrayPtr(dest_slice, dest_ptr_ty); |
| 8679 | const is_volatile = src_ptr_ty.isVolatilePtr(mod) or dest_ptr_ty.isVolatilePtr(mod); | 9141 | const is_volatile = src_ptr_ty.isVolatilePtr(mod) or dest_ptr_ty.isVolatilePtr(mod); |
| 8680 | | 9142 | |
| 8681 | // When bulk-memory is enabled, this will be lowered to WebAssembly's memory.copy instruction. | 9143 | // When bulk-memory is enabled, this will be lowered to WebAssembly's memory.copy instruction. |
| ... | @@ -8687,84 +9149,81 @@ pub const FuncGen = struct { | ... | @@ -8687,84 +9149,81 @@ pub const FuncGen = struct { |
| 8687 | std.Target.wasm.featureSetHas(o.target.cpu.features, .bulk_memory) and | 9149 | std.Target.wasm.featureSetHas(o.target.cpu.features, .bulk_memory) and |
| 8688 | dest_ptr_ty.isSlice(mod)) | 9150 | dest_ptr_ty.isSlice(mod)) |
| 8689 | { | 9151 | { |
| 8690 | const llvm_usize_ty = self.context.intType(self.dg.object.target.ptrBitWidth()); | 9152 | const zero_usize = try o.builder.intValue(try o.lowerType(Type.usize), 0); |
| 8691 | const cond = try self.cmp(len, llvm_usize_ty.constInt(0, .False), Type.usize, .neq); | 9153 | const cond = try self.cmp(len, zero_usize, Type.usize, .neq); |
| 8692 | const memcpy_block = self.context.appendBasicBlock(self.llvm_func, "MemcpyTrapSkip"); | 9154 | const memcpy_block = try self.wip.block(1, "MemcpyTrapSkip"); |
| 8693 | const end_block = self.context.appendBasicBlock(self.llvm_func, "MemcpyTrapEnd"); | 9155 | const end_block = try self.wip.block(2, "MemcpyTrapEnd"); |
| 8694 | _ = self.builder.buildCondBr(cond, memcpy_block, end_block); | 9156 | _ = try self.wip.brCond(cond, memcpy_block, end_block); |
| 8695 | self.builder.positionBuilderAtEnd(memcpy_block); | 9157 | self.wip.cursor = .{ .block = memcpy_block }; |
| 8696 | _ = self.builder.buildMemCpy( | 9158 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemCpy( |
| 8697 | dest_ptr, | 9159 | dest_ptr.toLlvm(&self.wip), |
| 8698 | dest_ptr_ty.ptrAlignment(mod), | 9160 | dest_ptr_ty.ptrAlignment(mod), |
| 8699 | src_ptr, | 9161 | src_ptr.toLlvm(&self.wip), |
| 8700 | src_ptr_ty.ptrAlignment(mod), | 9162 | src_ptr_ty.ptrAlignment(mod), |
| 8701 | len, | 9163 | len.toLlvm(&self.wip), |
| 8702 | is_volatile, | 9164 | is_volatile, |
| 8703 | ); | 9165 | ), &self.wip); |
| 8704 | _ = self.builder.buildBr(end_block); | 9166 | _ = try self.wip.br(end_block); |
| 8705 | self.builder.positionBuilderAtEnd(end_block); | 9167 | self.wip.cursor = .{ .block = end_block }; |
| 8706 | return null; | 9168 | return .none; |
| 8707 | } | 9169 | } |
| 8708 | | 9170 | |
| 8709 | _ = self.builder.buildMemCpy( | 9171 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemCpy( |
| 8710 | dest_ptr, | 9172 | dest_ptr.toLlvm(&self.wip), |
| 8711 | dest_ptr_ty.ptrAlignment(mod), | 9173 | dest_ptr_ty.ptrAlignment(mod), |
| 8712 | src_ptr, | 9174 | src_ptr.toLlvm(&self.wip), |
| 8713 | src_ptr_ty.ptrAlignment(mod), | 9175 | src_ptr_ty.ptrAlignment(mod), |
| 8714 | len, | 9176 | len.toLlvm(&self.wip), |
| 8715 | is_volatile, | 9177 | is_volatile, |
| 8716 | ); | 9178 | ), &self.wip); |
| 8717 | return null; | 9179 | return .none; |
| 8718 | } | 9180 | } |
| 8719 | | 9181 | |
| 8720 | fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9182 | fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8721 | const o = self.dg.object; | 9183 | const o = self.dg.object; |
| 8722 | const mod = o.module; | 9184 | const mod = o.module; |
| 8723 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 9185 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 8724 | const un_ty = self.typeOf(bin_op.lhs).childType(mod); | 9186 | const un_ty = self.typeOf(bin_op.lhs).childType(mod); |
| 8725 | const layout = un_ty.unionGetLayout(mod); | 9187 | const layout = un_ty.unionGetLayout(mod); |
| 8726 | if (layout.tag_size == 0) return null; | 9188 | if (layout.tag_size == 0) return .none; |
| 8727 | const union_ptr = try self.resolveInst(bin_op.lhs); | 9189 | const union_ptr = try self.resolveInst(bin_op.lhs); |
| 8728 | const new_tag = try self.resolveInst(bin_op.rhs); | 9190 | const new_tag = try self.resolveInst(bin_op.rhs); |
| 8729 | if (layout.payload_size == 0) { | 9191 | if (layout.payload_size == 0) { |
| 8730 | // TODO alignment on this store | 9192 | // TODO alignment on this store |
| 8731 | _ = self.builder.buildStore(new_tag, union_ptr); | 9193 | _ = try self.wip.store(.normal, new_tag, union_ptr, .default); |
| 8732 | return null; | 9194 | return .none; |
| 8733 | } | 9195 | } |
| 8734 | const un_llvm_ty = try o.lowerType(un_ty); | | |
| 8735 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); | 9196 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); |
| 8736 | const tag_field_ptr = self.builder.buildStructGEP(un_llvm_ty, union_ptr, tag_index, ""); | 9197 | const tag_field_ptr = try self.wip.gepStruct(try o.lowerType(un_ty), union_ptr, tag_index, ""); |
| 8737 | // TODO alignment on this store | 9198 | // TODO alignment on this store |
| 8738 | _ = self.builder.buildStore(new_tag, tag_field_ptr); | 9199 | _ = try self.wip.store(.normal, new_tag, tag_field_ptr, .default); |
| 8739 | return null; | 9200 | return .none; |
| 8740 | } | 9201 | } |
| 8741 | | 9202 | |
| 8742 | fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9203 | fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8743 | const o = self.dg.object; | 9204 | const o = self.dg.object; |
| 8744 | const mod = o.module; | 9205 | const mod = o.module; |
| 8745 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9206 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 8746 | const un_ty = self.typeOf(ty_op.operand); | 9207 | const un_ty = self.typeOf(ty_op.operand); |
| 8747 | const layout = un_ty.unionGetLayout(mod); | 9208 | const layout = un_ty.unionGetLayout(mod); |
| 8748 | if (layout.tag_size == 0) return null; | 9209 | if (layout.tag_size == 0) return .none; |
| 8749 | const union_handle = try self.resolveInst(ty_op.operand); | 9210 | const union_handle = try self.resolveInst(ty_op.operand); |
| 8750 | if (isByRef(un_ty, mod)) { | 9211 | if (isByRef(un_ty, mod)) { |
| 8751 | const llvm_un_ty = try o.lowerType(un_ty); | 9212 | const llvm_un_ty = try o.lowerType(un_ty); |
| 8752 | if (layout.payload_size == 0) { | 9213 | if (layout.payload_size == 0) |
| 8753 | return self.builder.buildLoad(llvm_un_ty, union_handle, ""); | 9214 | return self.wip.load(.normal, llvm_un_ty, union_handle, .default, ""); |
| 8754 | } | | |
| 8755 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); | 9215 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); |
| 8756 | const tag_field_ptr = self.builder.buildStructGEP(llvm_un_ty, union_handle, tag_index, ""); | 9216 | const tag_field_ptr = try self.wip.gepStruct(llvm_un_ty, union_handle, tag_index, ""); |
| 8757 | return self.builder.buildLoad(llvm_un_ty.structGetTypeAtIndex(tag_index), tag_field_ptr, ""); | 9217 | const llvm_tag_ty = llvm_un_ty.structFields(&o.builder)[tag_index]; |
| | 9218 | return self.wip.load(.normal, llvm_tag_ty, tag_field_ptr, .default, ""); |
| 8758 | } else { | 9219 | } else { |
| 8759 | if (layout.payload_size == 0) { | 9220 | if (layout.payload_size == 0) return union_handle; |
| 8760 | return union_handle; | | |
| 8761 | } | | |
| 8762 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); | 9221 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); |
| 8763 | return self.builder.buildExtractValue(union_handle, tag_index, ""); | 9222 | return self.wip.extractValue(union_handle, &.{tag_index}, ""); |
| 8764 | } | 9223 | } |
| 8765 | } | 9224 | } |
| 8766 | | 9225 | |
| 8767 | fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) !?*llvm.Value { | 9226 | fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) !Builder.Value { |
| 8768 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 9227 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 8769 | const operand = try self.resolveInst(un_op); | 9228 | const operand = try self.resolveInst(un_op); |
| 8770 | const operand_ty = self.typeOf(un_op); | 9229 | const operand_ty = self.typeOf(un_op); |
| ... | @@ -8772,7 +9231,7 @@ pub const FuncGen = struct { | ... | @@ -8772,7 +9231,7 @@ pub const FuncGen = struct { |
| 8772 | return self.buildFloatOp(op, operand_ty, 1, .{operand}); | 9231 | return self.buildFloatOp(op, operand_ty, 1, .{operand}); |
| 8773 | } | 9232 | } |
| 8774 | | 9233 | |
| 8775 | fn airNeg(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 9234 | fn airNeg(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 8776 | self.builder.setFastMath(want_fast_math); | 9235 | self.builder.setFastMath(want_fast_math); |
| 8777 | | 9236 | |
| 8778 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 9237 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| ... | @@ -8782,60 +9241,64 @@ pub const FuncGen = struct { | ... | @@ -8782,60 +9241,64 @@ pub const FuncGen = struct { |
| 8782 | return self.buildFloatOp(.neg, operand_ty, 1, .{operand}); | 9241 | return self.buildFloatOp(.neg, operand_ty, 1, .{operand}); |
| 8783 | } | 9242 | } |
| 8784 | | 9243 | |
| 8785 | fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !?*llvm.Value { | 9244 | fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !Builder.Value { |
| 8786 | const o = self.dg.object; | 9245 | const o = self.dg.object; |
| 8787 | const mod = o.module; | | |
| 8788 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9246 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 8789 | const operand_ty = self.typeOf(ty_op.operand); | 9247 | const operand_ty = self.typeOf(ty_op.operand); |
| 8790 | const operand = try self.resolveInst(ty_op.operand); | 9248 | const operand = try self.resolveInst(ty_op.operand); |
| 8791 | | 9249 | |
| 8792 | const llvm_i1 = self.context.intType(1); | 9250 | const llvm_operand_ty = try o.lowerType(operand_ty); |
| 8793 | const operand_llvm_ty = try o.lowerType(operand_ty); | 9251 | const llvm_fn_ty = try o.builder.fnType(llvm_operand_ty, &.{ llvm_operand_ty, .i1 }, .normal); |
| 8794 | const fn_val = self.getIntrinsic(llvm_fn_name, &.{operand_llvm_ty}); | 9252 | const fn_val = try self.getIntrinsic(llvm_fn_name, &.{llvm_operand_ty}); |
| 8795 | | 9253 | |
| 8796 | const params = [_]*llvm.Value{ operand, llvm_i1.constNull() }; | 9254 | const params = [_]*llvm.Value{ |
| 8797 | const wrong_size_result = self.builder.buildCall(fn_val.globalGetValueType(), fn_val, &params, params.len, .C, .Auto, ""); | 9255 | operand.toLlvm(&self.wip), |
| | 9256 | Builder.Constant.false.toLlvm(&o.builder), |
| | 9257 | }; |
| | 9258 | const wrong_size_result = (try self.wip.unimplemented(llvm_operand_ty, "")).finish( |
| | 9259 | self.builder.buildCall( |
| | 9260 | llvm_fn_ty.toLlvm(&o.builder), |
| | 9261 | fn_val, |
| | 9262 | &params, |
| | 9263 | params.len, |
| | 9264 | .C, |
| | 9265 | .Auto, |
| | 9266 | "", |
| | 9267 | ), |
| | 9268 | &self.wip, |
| | 9269 | ); |
| 8798 | const result_ty = self.typeOfIndex(inst); | 9270 | const result_ty = self.typeOfIndex(inst); |
| 8799 | const result_llvm_ty = try o.lowerType(result_ty); | 9271 | return self.wip.conv(.unsigned, wrong_size_result, try o.lowerType(result_ty), ""); |
| 8800 | | | |
| 8801 | const bits = operand_ty.intInfo(mod).bits; | | |
| 8802 | const result_bits = result_ty.intInfo(mod).bits; | | |
| 8803 | if (bits > result_bits) { | | |
| 8804 | return self.builder.buildTrunc(wrong_size_result, result_llvm_ty, ""); | | |
| 8805 | } else if (bits < result_bits) { | | |
| 8806 | return self.builder.buildZExt(wrong_size_result, result_llvm_ty, ""); | | |
| 8807 | } else { | | |
| 8808 | return wrong_size_result; | | |
| 8809 | } | | |
| 8810 | } | 9272 | } |
| 8811 | | 9273 | |
| 8812 | fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !?*llvm.Value { | 9274 | fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !Builder.Value { |
| 8813 | const o = self.dg.object; | 9275 | const o = self.dg.object; |
| 8814 | const mod = o.module; | | |
| 8815 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9276 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 8816 | const operand_ty = self.typeOf(ty_op.operand); | 9277 | const operand_ty = self.typeOf(ty_op.operand); |
| 8817 | const operand = try self.resolveInst(ty_op.operand); | 9278 | const operand = try self.resolveInst(ty_op.operand); |
| 8818 | | 9279 | |
| 8819 | const params = [_]*llvm.Value{operand}; | 9280 | const llvm_operand_ty = try o.lowerType(operand_ty); |
| 8820 | const operand_llvm_ty = try o.lowerType(operand_ty); | 9281 | const llvm_fn_ty = try o.builder.fnType(llvm_operand_ty, &.{llvm_operand_ty}, .normal); |
| 8821 | const fn_val = self.getIntrinsic(llvm_fn_name, &.{operand_llvm_ty}); | 9282 | const fn_val = try self.getIntrinsic(llvm_fn_name, &.{llvm_operand_ty}); |
| 8822 | | 9283 | |
| 8823 | const wrong_size_result = self.builder.buildCall(fn_val.globalGetValueType(), fn_val, &params, params.len, .C, .Auto, ""); | 9284 | const params = [_]*llvm.Value{operand.toLlvm(&self.wip)}; |
| | 9285 | const wrong_size_result = (try self.wip.unimplemented(llvm_operand_ty, "")).finish( |
| | 9286 | self.builder.buildCall( |
| | 9287 | llvm_fn_ty.toLlvm(&o.builder), |
| | 9288 | fn_val, |
| | 9289 | &params, |
| | 9290 | params.len, |
| | 9291 | .C, |
| | 9292 | .Auto, |
| | 9293 | "", |
| | 9294 | ), |
| | 9295 | &self.wip, |
| | 9296 | ); |
| 8824 | const result_ty = self.typeOfIndex(inst); | 9297 | const result_ty = self.typeOfIndex(inst); |
| 8825 | const result_llvm_ty = try o.lowerType(result_ty); | 9298 | return self.wip.conv(.unsigned, wrong_size_result, try o.lowerType(result_ty), ""); |
| 8826 | | | |
| 8827 | const bits = operand_ty.intInfo(mod).bits; | | |
| 8828 | const result_bits = result_ty.intInfo(mod).bits; | | |
| 8829 | if (bits > result_bits) { | | |
| 8830 | return self.builder.buildTrunc(wrong_size_result, result_llvm_ty, ""); | | |
| 8831 | } else if (bits < result_bits) { | | |
| 8832 | return self.builder.buildZExt(wrong_size_result, result_llvm_ty, ""); | | |
| 8833 | } else { | | |
| 8834 | return wrong_size_result; | | |
| 8835 | } | | |
| 8836 | } | 9299 | } |
| 8837 | | 9300 | |
| 8838 | fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !?*llvm.Value { | 9301 | fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !Builder.Value { |
| 8839 | const o = self.dg.object; | 9302 | const o = self.dg.object; |
| 8840 | const mod = o.module; | 9303 | const mod = o.module; |
| 8841 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9304 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -8844,52 +9307,47 @@ pub const FuncGen = struct { | ... | @@ -8844,52 +9307,47 @@ pub const FuncGen = struct { |
| 8844 | assert(bits % 8 == 0); | 9307 | assert(bits % 8 == 0); |
| 8845 | | 9308 | |
| 8846 | var operand = try self.resolveInst(ty_op.operand); | 9309 | var operand = try self.resolveInst(ty_op.operand); |
| 8847 | var operand_llvm_ty = try o.lowerType(operand_ty); | 9310 | var llvm_operand_ty = try o.lowerType(operand_ty); |
| 8848 | | 9311 | |
| 8849 | if (bits % 16 == 8) { | 9312 | if (bits % 16 == 8) { |
| 8850 | // If not an even byte-multiple, we need zero-extend + shift-left 1 byte | 9313 | // If not an even byte-multiple, we need zero-extend + shift-left 1 byte |
| 8851 | // The truncated result at the end will be the correct bswap | 9314 | // The truncated result at the end will be the correct bswap |
| 8852 | const scalar_llvm_ty = self.context.intType(bits + 8); | 9315 | const scalar_ty = try o.builder.intType(@intCast(bits + 8)); |
| 8853 | if (operand_ty.zigTypeTag(mod) == .Vector) { | 9316 | if (operand_ty.zigTypeTag(mod) == .Vector) { |
| 8854 | const vec_len = operand_ty.vectorLen(mod); | 9317 | const vec_len = operand_ty.vectorLen(mod); |
| 8855 | operand_llvm_ty = scalar_llvm_ty.vectorType(vec_len); | 9318 | llvm_operand_ty = try o.builder.vectorType(.normal, vec_len, scalar_ty); |
| | 9319 | } else llvm_operand_ty = scalar_ty; |
| 8856 | | 9320 | |
| 8857 | const shifts = try self.gpa.alloc(*llvm.Value, vec_len); | 9321 | const shift_amt = |
| 8858 | defer self.gpa.free(shifts); | 9322 | try o.builder.splatValue(llvm_operand_ty, try o.builder.intConst(scalar_ty, 8)); |
| 8859 | | 9323 | const extended = try self.wip.cast(.zext, operand, llvm_operand_ty, ""); |
| 8860 | for (shifts) |*elem| { | 9324 | operand = try self.wip.bin(.shl, extended, shift_amt, ""); |
| 8861 | elem.* = scalar_llvm_ty.constInt(8, .False); | | |
| 8862 | } | | |
| 8863 | const shift_vec = llvm.constVector(shifts.ptr, vec_len); | | |
| 8864 | | 9325 | |
| 8865 | const extended = self.builder.buildZExt(operand, operand_llvm_ty, ""); | | |
| 8866 | operand = self.builder.buildShl(extended, shift_vec, ""); | | |
| 8867 | } else { | | |
| 8868 | const extended = self.builder.buildZExt(operand, scalar_llvm_ty, ""); | | |
| 8869 | operand = self.builder.buildShl(extended, scalar_llvm_ty.constInt(8, .False), ""); | | |
| 8870 | operand_llvm_ty = scalar_llvm_ty; | | |
| 8871 | } | | |
| 8872 | bits = bits + 8; | 9326 | bits = bits + 8; |
| 8873 | } | 9327 | } |
| 8874 | | 9328 | |
| 8875 | const params = [_]*llvm.Value{operand}; | 9329 | const llvm_fn_ty = try o.builder.fnType(llvm_operand_ty, &.{llvm_operand_ty}, .normal); |
| 8876 | const fn_val = self.getIntrinsic(llvm_fn_name, &.{operand_llvm_ty}); | 9330 | const fn_val = try self.getIntrinsic(llvm_fn_name, &.{llvm_operand_ty}); |
| 8877 | | 9331 | |
| 8878 | const wrong_size_result = self.builder.buildCall(fn_val.globalGetValueType(), fn_val, &params, params.len, .C, .Auto, ""); | 9332 | const params = [_]*llvm.Value{operand.toLlvm(&self.wip)}; |
| | 9333 | const wrong_size_result = (try self.wip.unimplemented(llvm_operand_ty, "")).finish( |
| | 9334 | self.builder.buildCall( |
| | 9335 | llvm_fn_ty.toLlvm(&o.builder), |
| | 9336 | fn_val, |
| | 9337 | &params, |
| | 9338 | params.len, |
| | 9339 | .C, |
| | 9340 | .Auto, |
| | 9341 | "", |
| | 9342 | ), |
| | 9343 | &self.wip, |
| | 9344 | ); |
| 8879 | | 9345 | |
| 8880 | const result_ty = self.typeOfIndex(inst); | 9346 | const result_ty = self.typeOfIndex(inst); |
| 8881 | const result_llvm_ty = try o.lowerType(result_ty); | 9347 | return self.wip.conv(.unsigned, wrong_size_result, try o.lowerType(result_ty), ""); |
| 8882 | const result_bits = result_ty.intInfo(mod).bits; | | |
| 8883 | if (bits > result_bits) { | | |
| 8884 | return self.builder.buildTrunc(wrong_size_result, result_llvm_ty, ""); | | |
| 8885 | } else if (bits < result_bits) { | | |
| 8886 | return self.builder.buildZExt(wrong_size_result, result_llvm_ty, ""); | | |
| 8887 | } else { | | |
| 8888 | return wrong_size_result; | | |
| 8889 | } | | |
| 8890 | } | 9348 | } |
| 8891 | | 9349 | |
| 8892 | fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9350 | fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8893 | const o = self.dg.object; | 9351 | const o = self.dg.object; |
| 8894 | const mod = o.module; | 9352 | const mod = o.module; |
| 8895 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9353 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -8897,50 +9355,53 @@ pub const FuncGen = struct { | ... | @@ -8897,50 +9355,53 @@ pub const FuncGen = struct { |
| 8897 | const error_set_ty = self.air.getRefType(ty_op.ty); | 9355 | const error_set_ty = self.air.getRefType(ty_op.ty); |
| 8898 | | 9356 | |
| 8899 | const names = error_set_ty.errorSetNames(mod); | 9357 | const names = error_set_ty.errorSetNames(mod); |
| 8900 | const valid_block = self.context.appendBasicBlock(self.llvm_func, "Valid"); | 9358 | const valid_block = try self.wip.block(@intCast(names.len), "Valid"); |
| 8901 | const invalid_block = self.context.appendBasicBlock(self.llvm_func, "Invalid"); | 9359 | const invalid_block = try self.wip.block(1, "Invalid"); |
| 8902 | const end_block = self.context.appendBasicBlock(self.llvm_func, "End"); | 9360 | const end_block = try self.wip.block(2, "End"); |
| 8903 | const switch_instr = self.builder.buildSwitch(operand, invalid_block, @as(c_uint, @intCast(names.len))); | 9361 | var wip_switch = try self.wip.@"switch"(operand, invalid_block, @intCast(names.len)); |
| | 9362 | defer wip_switch.finish(&self.wip); |
| 8904 | | 9363 | |
| 8905 | for (names) |name| { | 9364 | for (names) |name| { |
| 8906 | const err_int = @as(Module.ErrorInt, @intCast(mod.global_error_set.getIndex(name).?)); | 9365 | const err_int = mod.global_error_set.getIndex(name).?; |
| 8907 | const this_tag_int_value = try o.lowerValue(.{ | 9366 | const this_tag_int_value = try o.builder.intConst(Builder.Type.err_int, err_int); |
| 8908 | .ty = Type.err_int, | 9367 | try wip_switch.addCase(this_tag_int_value, valid_block, &self.wip); |
| 8909 | .val = try mod.intValue(Type.err_int, err_int), | 9368 | } |
| 8910 | }); | 9369 | self.wip.cursor = .{ .block = valid_block }; |
| 8911 | switch_instr.addCase(this_tag_int_value, valid_block); | 9370 | _ = try self.wip.br(end_block); |
| 8912 | } | 9371 | |
| 8913 | self.builder.positionBuilderAtEnd(valid_block); | 9372 | self.wip.cursor = .{ .block = invalid_block }; |
| 8914 | _ = self.builder.buildBr(end_block); | 9373 | _ = try self.wip.br(end_block); |
| 8915 | | 9374 | |
| 8916 | self.builder.positionBuilderAtEnd(invalid_block); | 9375 | self.wip.cursor = .{ .block = end_block }; |
| 8917 | _ = self.builder.buildBr(end_block); | 9376 | const phi = try self.wip.phi(.i1, ""); |
| 8918 | | 9377 | try phi.finish( |
| 8919 | self.builder.positionBuilderAtEnd(end_block); | 9378 | &.{ Builder.Constant.true.toValue(), Builder.Constant.false.toValue() }, |
| 8920 | | 9379 | &.{ valid_block, invalid_block }, |
| 8921 | const llvm_type = self.context.intType(1); | 9380 | &self.wip, |
| 8922 | const incoming_values: [2]*llvm.Value = .{ | 9381 | ); |
| 8923 | llvm_type.constInt(1, .False), llvm_type.constInt(0, .False), | 9382 | return phi.toValue(); |
| 8924 | }; | | |
| 8925 | const incoming_blocks: [2]*llvm.BasicBlock = .{ | | |
| 8926 | valid_block, invalid_block, | | |
| 8927 | }; | | |
| 8928 | const phi_node = self.builder.buildPhi(llvm_type, ""); | | |
| 8929 | phi_node.addIncoming(&incoming_values, &incoming_blocks, 2); | | |
| 8930 | return phi_node; | | |
| 8931 | } | 9383 | } |
| 8932 | | 9384 | |
| 8933 | fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9385 | fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| | 9386 | const o = self.dg.object; |
| 8934 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 9387 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 8935 | const operand = try self.resolveInst(un_op); | 9388 | const operand = try self.resolveInst(un_op); |
| 8936 | const enum_ty = self.typeOf(un_op); | 9389 | const enum_ty = self.typeOf(un_op); |
| 8937 | | 9390 | |
| 8938 | const llvm_fn = try self.getIsNamedEnumValueFunction(enum_ty); | 9391 | const llvm_fn = try self.getIsNamedEnumValueFunction(enum_ty); |
| 8939 | const params = [_]*llvm.Value{operand}; | 9392 | const params = [_]*llvm.Value{operand.toLlvm(&self.wip)}; |
| 8940 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .Fast, .Auto, ""); | 9393 | return (try self.wip.unimplemented(.i1, "")).finish(self.builder.buildCall( |
| | 9394 | llvm_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 9395 | llvm_fn.toLlvm(&o.builder), |
| | 9396 | &params, |
| | 9397 | params.len, |
| | 9398 | .Fast, |
| | 9399 | .Auto, |
| | 9400 | "", |
| | 9401 | ), &self.wip); |
| 8941 | } | 9402 | } |
| 8942 | | 9403 | |
| 8943 | fn getIsNamedEnumValueFunction(self: *FuncGen, enum_ty: Type) !*llvm.Value { | 9404 | fn getIsNamedEnumValueFunction(self: *FuncGen, enum_ty: Type) !Builder.Function.Index { |
| 8944 | const o = self.dg.object; | 9405 | const o = self.dg.object; |
| 8945 | const mod = o.module; | 9406 | const mod = o.module; |
| 8946 | const enum_type = mod.intern_pool.indexToKey(enum_ty.toIntern()).enum_type; | 9407 | const enum_type = mod.intern_pool.indexToKey(enum_ty.toIntern()).enum_type; |
| ... | @@ -8950,185 +9411,207 @@ pub const FuncGen = struct { | ... | @@ -8950,185 +9411,207 @@ pub const FuncGen = struct { |
| 8950 | if (gop.found_existing) return gop.value_ptr.*; | 9411 | if (gop.found_existing) return gop.value_ptr.*; |
| 8951 | errdefer assert(o.named_enum_map.remove(enum_type.decl)); | 9412 | errdefer assert(o.named_enum_map.remove(enum_type.decl)); |
| 8952 | | 9413 | |
| 8953 | var arena_allocator = std.heap.ArenaAllocator.init(self.gpa); | | |
| 8954 | defer arena_allocator.deinit(); | | |
| 8955 | const arena = arena_allocator.allocator(); | | |
| 8956 | | | |
| 8957 | const fqn = try mod.declPtr(enum_type.decl).getFullyQualifiedName(mod); | 9414 | const fqn = try mod.declPtr(enum_type.decl).getFullyQualifiedName(mod); |
| 8958 | const llvm_fn_name = try std.fmt.allocPrintZ(arena, "__zig_is_named_enum_value_{}", .{fqn.fmt(&mod.intern_pool)}); | 9415 | const llvm_fn_name = try o.builder.fmt("__zig_is_named_enum_value_{}", .{ |
| 8959 | | 9416 | fqn.fmt(&mod.intern_pool), |
| 8960 | const param_types = [_]*llvm.Type{try o.lowerType(enum_type.tag_ty.toType())}; | 9417 | }); |
| 8961 | | 9418 | |
| 8962 | const llvm_ret_ty = try o.lowerType(Type.bool); | 9419 | const fn_type = try o.builder.fnType(.i1, &.{ |
| 8963 | const fn_type = llvm.functionType(llvm_ret_ty, &param_types, param_types.len, .False); | 9420 | try o.lowerType(enum_type.tag_ty.toType()), |
| 8964 | const fn_val = o.llvm_module.addFunction(llvm_fn_name, fn_type); | 9421 | }, .normal); |
| | 9422 | const fn_val = o.llvm_module.addFunction(llvm_fn_name.toSlice(&o.builder).?, fn_type.toLlvm(&o.builder)); |
| 8965 | fn_val.setLinkage(.Internal); | 9423 | fn_val.setLinkage(.Internal); |
| 8966 | fn_val.setFunctionCallConv(.Fast); | 9424 | fn_val.setFunctionCallConv(.Fast); |
| 8967 | o.addCommonFnAttributes(fn_val); | 9425 | o.addCommonFnAttributes(fn_val); |
| 8968 | gop.value_ptr.* = fn_val; | | |
| 8969 | | 9426 | |
| 8970 | const prev_block = self.builder.getInsertBlock(); | 9427 | var global = Builder.Global{ |
| 8971 | const prev_debug_location = self.builder.getCurrentDebugLocation2(); | 9428 | .linkage = .internal, |
| 8972 | defer { | 9429 | .type = fn_type, |
| 8973 | self.builder.positionBuilderAtEnd(prev_block); | 9430 | .kind = .{ .function = @enumFromInt(o.builder.functions.items.len) }, |
| 8974 | if (self.di_scope != null) { | 9431 | }; |
| 8975 | self.builder.setCurrentDebugLocation2(prev_debug_location); | 9432 | var function = Builder.Function{ |
| 8976 | } | 9433 | .global = @enumFromInt(o.builder.globals.count()), |
| | 9434 | }; |
| | 9435 | try o.builder.llvm.globals.append(self.gpa, fn_val); |
| | 9436 | _ = try o.builder.addGlobal(llvm_fn_name, global); |
| | 9437 | try o.builder.functions.append(self.gpa, function); |
| | 9438 | gop.value_ptr.* = global.kind.function; |
| | 9439 | |
| | 9440 | var wip = try Builder.WipFunction.init(&o.builder, global.kind.function); |
| | 9441 | defer wip.deinit(); |
| | 9442 | wip.cursor = .{ .block = try wip.block(0, "Entry") }; |
| | 9443 | |
| | 9444 | const named_block = try wip.block(@intCast(enum_type.names.len), "Named"); |
| | 9445 | const unnamed_block = try wip.block(1, "Unnamed"); |
| | 9446 | const tag_int_value = wip.arg(0); |
| | 9447 | var wip_switch = try wip.@"switch"(tag_int_value, unnamed_block, @intCast(enum_type.names.len)); |
| | 9448 | defer wip_switch.finish(&wip); |
| | 9449 | |
| | 9450 | for (0..enum_type.names.len) |field_index| { |
| | 9451 | const this_tag_int_value = try o.lowerValue( |
| | 9452 | (try mod.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), |
| | 9453 | ); |
| | 9454 | try wip_switch.addCase(this_tag_int_value, named_block, &wip); |
| 8977 | } | 9455 | } |
| | 9456 | wip.cursor = .{ .block = named_block }; |
| | 9457 | _ = try wip.ret(Builder.Constant.true.toValue()); |
| 8978 | | 9458 | |
| 8979 | const entry_block = self.context.appendBasicBlock(fn_val, "Entry"); | 9459 | wip.cursor = .{ .block = unnamed_block }; |
| 8980 | self.builder.positionBuilderAtEnd(entry_block); | 9460 | _ = try wip.ret(Builder.Constant.false.toValue()); |
| 8981 | self.builder.clearCurrentDebugLocation(); | | |
| 8982 | | | |
| 8983 | const named_block = self.context.appendBasicBlock(fn_val, "Named"); | | |
| 8984 | const unnamed_block = self.context.appendBasicBlock(fn_val, "Unnamed"); | | |
| 8985 | const tag_int_value = fn_val.getParam(0); | | |
| 8986 | const switch_instr = self.builder.buildSwitch(tag_int_value, unnamed_block, @as(c_uint, @intCast(enum_type.names.len))); | | |
| 8987 | | | |
| 8988 | for (enum_type.names, 0..) |_, field_index_usize| { | | |
| 8989 | const field_index = @as(u32, @intCast(field_index_usize)); | | |
| 8990 | const this_tag_int_value = int: { | | |
| 8991 | break :int try o.lowerValue(.{ | | |
| 8992 | .ty = enum_ty, | | |
| 8993 | .val = try mod.enumValueFieldIndex(enum_ty, field_index), | | |
| 8994 | }); | | |
| 8995 | }; | | |
| 8996 | switch_instr.addCase(this_tag_int_value, named_block); | | |
| 8997 | } | | |
| 8998 | self.builder.positionBuilderAtEnd(named_block); | | |
| 8999 | _ = self.builder.buildRet(self.context.intType(1).constInt(1, .False)); | | |
| 9000 | | 9461 | |
| 9001 | self.builder.positionBuilderAtEnd(unnamed_block); | 9462 | try wip.finish(); |
| 9002 | _ = self.builder.buildRet(self.context.intType(1).constInt(0, .False)); | 9463 | return global.kind.function; |
| 9003 | return fn_val; | | |
| 9004 | } | 9464 | } |
| 9005 | | 9465 | |
| 9006 | fn airTagName(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9466 | fn airTagName(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| | 9467 | const o = self.dg.object; |
| 9007 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 9468 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 9008 | const operand = try self.resolveInst(un_op); | 9469 | const operand = try self.resolveInst(un_op); |
| 9009 | const enum_ty = self.typeOf(un_op); | 9470 | const enum_ty = self.typeOf(un_op); |
| 9010 | | 9471 | |
| 9011 | const llvm_fn = try self.getEnumTagNameFunction(enum_ty); | 9472 | const llvm_fn = try self.getEnumTagNameFunction(enum_ty); |
| 9012 | const params = [_]*llvm.Value{operand}; | 9473 | const llvm_fn_ty = llvm_fn.typeOf(&o.builder); |
| 9013 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .Fast, .Auto, ""); | 9474 | const params = [_]*llvm.Value{operand.toLlvm(&self.wip)}; |
| | 9475 | return (try self.wip.unimplemented(llvm_fn_ty.functionReturn(&o.builder), "")).finish( |
| | 9476 | self.builder.buildCall( |
| | 9477 | llvm_fn_ty.toLlvm(&o.builder), |
| | 9478 | llvm_fn.toLlvm(&o.builder), |
| | 9479 | &params, |
| | 9480 | params.len, |
| | 9481 | .Fast, |
| | 9482 | .Auto, |
| | 9483 | "", |
| | 9484 | ), |
| | 9485 | &self.wip, |
| | 9486 | ); |
| 9014 | } | 9487 | } |
| 9015 | | 9488 | |
| 9016 | fn getEnumTagNameFunction(self: *FuncGen, enum_ty: Type) !*llvm.Value { | 9489 | fn getEnumTagNameFunction(self: *FuncGen, enum_ty: Type) !Builder.Function.Index { |
| 9017 | const o = self.dg.object; | 9490 | const o = self.dg.object; |
| 9018 | const mod = o.module; | 9491 | const mod = o.module; |
| 9019 | const enum_type = mod.intern_pool.indexToKey(enum_ty.toIntern()).enum_type; | 9492 | const enum_type = mod.intern_pool.indexToKey(enum_ty.toIntern()).enum_type; |
| 9020 | | 9493 | |
| 9021 | // TODO: detect when the type changes and re-emit this function. | 9494 | // TODO: detect when the type changes and re-emit this function. |
| 9022 | const gop = try o.decl_map.getOrPut(o.gpa, enum_type.decl); | 9495 | const gop = try o.decl_map.getOrPut(o.gpa, enum_type.decl); |
| 9023 | if (gop.found_existing) return gop.value_ptr.*; | 9496 | if (gop.found_existing) return gop.value_ptr.ptrConst(&o.builder).kind.function; |
| 9024 | errdefer assert(o.decl_map.remove(enum_type.decl)); | 9497 | errdefer assert(o.decl_map.remove(enum_type.decl)); |
| 9025 | | 9498 | |
| 9026 | var arena_allocator = std.heap.ArenaAllocator.init(self.gpa); | | |
| 9027 | defer arena_allocator.deinit(); | | |
| 9028 | const arena = arena_allocator.allocator(); | | |
| 9029 | | | |
| 9030 | const fqn = try mod.declPtr(enum_type.decl).getFullyQualifiedName(mod); | 9499 | const fqn = try mod.declPtr(enum_type.decl).getFullyQualifiedName(mod); |
| 9031 | const llvm_fn_name = try std.fmt.allocPrintZ(arena, "__zig_tag_name_{}", .{fqn.fmt(&mod.intern_pool)}); | 9500 | const llvm_fn_name = try o.builder.fmt("__zig_tag_name_{}", .{fqn.fmt(&mod.intern_pool)}); |
| 9032 | | | |
| 9033 | const slice_ty = Type.slice_const_u8_sentinel_0; | | |
| 9034 | const llvm_ret_ty = try o.lowerType(slice_ty); | | |
| 9035 | const usize_llvm_ty = try o.lowerType(Type.usize); | | |
| 9036 | const slice_alignment = slice_ty.abiAlignment(mod); | | |
| 9037 | | 9501 | |
| 9038 | const param_types = [_]*llvm.Type{try o.lowerType(enum_type.tag_ty.toType())}; | 9502 | const ret_ty = try o.lowerType(Type.slice_const_u8_sentinel_0); |
| | 9503 | const usize_ty = try o.lowerType(Type.usize); |
| 9039 | | 9504 | |
| 9040 | const fn_type = llvm.functionType(llvm_ret_ty, &param_types, param_types.len, .False); | 9505 | const fn_type = try o.builder.fnType(ret_ty, &.{ |
| 9041 | const fn_val = o.llvm_module.addFunction(llvm_fn_name, fn_type); | 9506 | try o.lowerType(enum_type.tag_ty.toType()), |
| | 9507 | }, .normal); |
| | 9508 | const fn_val = o.llvm_module.addFunction(llvm_fn_name.toSlice(&o.builder).?, fn_type.toLlvm(&o.builder)); |
| 9042 | fn_val.setLinkage(.Internal); | 9509 | fn_val.setLinkage(.Internal); |
| 9043 | fn_val.setFunctionCallConv(.Fast); | 9510 | fn_val.setFunctionCallConv(.Fast); |
| 9044 | o.addCommonFnAttributes(fn_val); | 9511 | o.addCommonFnAttributes(fn_val); |
| 9045 | gop.value_ptr.* = fn_val; | | |
| 9046 | | | |
| 9047 | const prev_block = self.builder.getInsertBlock(); | | |
| 9048 | const prev_debug_location = self.builder.getCurrentDebugLocation2(); | | |
| 9049 | defer { | | |
| 9050 | self.builder.positionBuilderAtEnd(prev_block); | | |
| 9051 | if (self.di_scope != null) { | | |
| 9052 | self.builder.setCurrentDebugLocation2(prev_debug_location); | | |
| 9053 | } | | |
| 9054 | } | | |
| 9055 | | | |
| 9056 | const entry_block = self.context.appendBasicBlock(fn_val, "Entry"); | | |
| 9057 | self.builder.positionBuilderAtEnd(entry_block); | | |
| 9058 | self.builder.clearCurrentDebugLocation(); | | |
| 9059 | | 9512 | |
| 9060 | const bad_value_block = self.context.appendBasicBlock(fn_val, "BadValue"); | 9513 | var global = Builder.Global{ |
| 9061 | const tag_int_value = fn_val.getParam(0); | 9514 | .linkage = .internal, |
| 9062 | const switch_instr = self.builder.buildSwitch(tag_int_value, bad_value_block, @as(c_uint, @intCast(enum_type.names.len))); | 9515 | .type = fn_type, |
| 9063 | | 9516 | .kind = .{ .function = @enumFromInt(o.builder.functions.items.len) }, |
| 9064 | const array_ptr_indices = [_]*llvm.Value{ | | |
| 9065 | usize_llvm_ty.constNull(), usize_llvm_ty.constNull(), | | |
| 9066 | }; | 9517 | }; |
| 9067 | | 9518 | var function = Builder.Function{ |
| 9068 | for (enum_type.names, 0..) |name_ip, field_index_usize| { | 9519 | .global = @enumFromInt(o.builder.globals.count()), |
| 9069 | const field_index = @as(u32, @intCast(field_index_usize)); | 9520 | }; |
| 9070 | const name = mod.intern_pool.stringToSlice(name_ip); | 9521 | try o.builder.llvm.globals.append(self.gpa, fn_val); |
| 9071 | const str_init = self.context.constString(name.ptr, @as(c_uint, @intCast(name.len)), .False); | 9522 | gop.value_ptr.* = try o.builder.addGlobal(llvm_fn_name, global); |
| 9072 | const str_init_llvm_ty = str_init.typeOf(); | 9523 | try o.builder.functions.append(self.gpa, function); |
| 9073 | const str_global = o.llvm_module.addGlobal(str_init_llvm_ty, ""); | 9524 | |
| 9074 | str_global.setInitializer(str_init); | 9525 | var wip = try Builder.WipFunction.init(&o.builder, global.kind.function); |
| 9075 | str_global.setLinkage(.Private); | 9526 | defer wip.deinit(); |
| 9076 | str_global.setGlobalConstant(.True); | 9527 | wip.cursor = .{ .block = try wip.block(0, "Entry") }; |
| 9077 | str_global.setUnnamedAddr(.True); | 9528 | |
| 9078 | str_global.setAlignment(1); | 9529 | const bad_value_block = try wip.block(1, "BadValue"); |
| 9079 | | 9530 | const tag_int_value = wip.arg(0); |
| 9080 | const slice_fields = [_]*llvm.Value{ | 9531 | var wip_switch = |
| 9081 | str_init_llvm_ty.constInBoundsGEP(str_global, &array_ptr_indices, array_ptr_indices.len), | 9532 | try wip.@"switch"(tag_int_value, bad_value_block, @intCast(enum_type.names.len)); |
| 9082 | usize_llvm_ty.constInt(name.len, .False), | 9533 | defer wip_switch.finish(&wip); |
| | 9534 | |
| | 9535 | for (enum_type.names, 0..) |name_ip, field_index| { |
| | 9536 | const name = try o.builder.string(mod.intern_pool.stringToSlice(name_ip)); |
| | 9537 | const str_init = try o.builder.stringNullConst(name); |
| | 9538 | const str_ty = str_init.typeOf(&o.builder); |
| | 9539 | const str_llvm_global = o.llvm_module.addGlobal(str_ty.toLlvm(&o.builder), ""); |
| | 9540 | str_llvm_global.setInitializer(str_init.toLlvm(&o.builder)); |
| | 9541 | str_llvm_global.setLinkage(.Private); |
| | 9542 | str_llvm_global.setGlobalConstant(.True); |
| | 9543 | str_llvm_global.setUnnamedAddr(.True); |
| | 9544 | str_llvm_global.setAlignment(1); |
| | 9545 | |
| | 9546 | var str_global = Builder.Global{ |
| | 9547 | .linkage = .private, |
| | 9548 | .unnamed_addr = .unnamed_addr, |
| | 9549 | .type = str_ty, |
| | 9550 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| | 9551 | }; |
| | 9552 | var str_variable = Builder.Variable{ |
| | 9553 | .global = @enumFromInt(o.builder.globals.count()), |
| | 9554 | .mutability = .constant, |
| | 9555 | .init = str_init, |
| | 9556 | .alignment = comptime Builder.Alignment.fromByteUnits(1), |
| 9083 | }; | 9557 | }; |
| 9084 | const slice_init = llvm_ret_ty.constNamedStruct(&slice_fields, slice_fields.len); | 9558 | try o.builder.llvm.globals.append(o.gpa, str_llvm_global); |
| 9085 | const slice_global = o.llvm_module.addGlobal(slice_init.typeOf(), ""); | 9559 | const global_index = try o.builder.addGlobal(.empty, str_global); |
| 9086 | slice_global.setInitializer(slice_init); | 9560 | try o.builder.variables.append(o.gpa, str_variable); |
| 9087 | slice_global.setLinkage(.Private); | 9561 | |
| 9088 | slice_global.setGlobalConstant(.True); | 9562 | const slice_val = try o.builder.structValue(ret_ty, &.{ |
| 9089 | slice_global.setUnnamedAddr(.True); | 9563 | global_index.toConst(), |
| 9090 | slice_global.setAlignment(slice_alignment); | 9564 | try o.builder.intConst(usize_ty, name.toSlice(&o.builder).?.len), |
| 9091 | | | |
| 9092 | const return_block = self.context.appendBasicBlock(fn_val, "Name"); | | |
| 9093 | const this_tag_int_value = try o.lowerValue(.{ | | |
| 9094 | .ty = enum_ty, | | |
| 9095 | .val = try mod.enumValueFieldIndex(enum_ty, field_index), | | |
| 9096 | }); | 9565 | }); |
| 9097 | switch_instr.addCase(this_tag_int_value, return_block); | | |
| 9098 | | 9566 | |
| 9099 | self.builder.positionBuilderAtEnd(return_block); | 9567 | const return_block = try wip.block(1, "Name"); |
| 9100 | const loaded = self.builder.buildLoad(llvm_ret_ty, slice_global, ""); | 9568 | const this_tag_int_value = try o.lowerValue( |
| 9101 | loaded.setAlignment(slice_alignment); | 9569 | (try mod.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), |
| 9102 | _ = self.builder.buildRet(loaded); | 9570 | ); |
| | 9571 | try wip_switch.addCase(this_tag_int_value, return_block, &wip); |
| | 9572 | |
| | 9573 | wip.cursor = .{ .block = return_block }; |
| | 9574 | _ = try wip.ret(slice_val); |
| 9103 | } | 9575 | } |
| 9104 | | 9576 | |
| 9105 | self.builder.positionBuilderAtEnd(bad_value_block); | 9577 | wip.cursor = .{ .block = bad_value_block }; |
| 9106 | _ = self.builder.buildUnreachable(); | 9578 | _ = try wip.@"unreachable"(); |
| 9107 | return fn_val; | 9579 | |
| | 9580 | try wip.finish(); |
| | 9581 | return global.kind.function; |
| 9108 | } | 9582 | } |
| 9109 | | 9583 | |
| 9110 | fn getCmpLtErrorsLenFunction(self: *FuncGen) !*llvm.Value { | 9584 | fn getCmpLtErrorsLenFunction(self: *FuncGen) !Builder.Function.Index { |
| 9111 | const o = self.dg.object; | 9585 | const o = self.dg.object; |
| 9112 | | 9586 | |
| 9113 | if (o.llvm_module.getNamedFunction(lt_errors_fn_name)) |llvm_fn| { | 9587 | const name = try o.builder.string(lt_errors_fn_name); |
| 9114 | return llvm_fn; | 9588 | if (o.builder.getGlobal(name)) |llvm_fn| return llvm_fn.ptrConst(&o.builder).kind.function; |
| 9115 | } | | |
| 9116 | | 9589 | |
| 9117 | // Function signature: fn (anyerror) bool | 9590 | // Function signature: fn (anyerror) bool |
| 9118 | | 9591 | |
| 9119 | const ret_llvm_ty = try o.lowerType(Type.bool); | 9592 | const fn_type = try o.builder.fnType(.i1, &.{Builder.Type.err_int}, .normal); |
| 9120 | const anyerror_llvm_ty = try o.lowerType(Type.anyerror); | 9593 | const llvm_fn = o.llvm_module.addFunction(name.toSlice(&o.builder).?, fn_type.toLlvm(&o.builder)); |
| 9121 | const param_types = [_]*llvm.Type{anyerror_llvm_ty}; | | |
| 9122 | | 9594 | |
| 9123 | const fn_type = llvm.functionType(ret_llvm_ty, &param_types, param_types.len, .False); | | |
| 9124 | const llvm_fn = o.llvm_module.addFunction(lt_errors_fn_name, fn_type); | | |
| 9125 | llvm_fn.setLinkage(.Internal); | 9595 | llvm_fn.setLinkage(.Internal); |
| 9126 | llvm_fn.setFunctionCallConv(.Fast); | 9596 | llvm_fn.setFunctionCallConv(.Fast); |
| 9127 | o.addCommonFnAttributes(llvm_fn); | 9597 | o.addCommonFnAttributes(llvm_fn); |
| 9128 | return llvm_fn; | 9598 | |
| | 9599 | var global = Builder.Global{ |
| | 9600 | .linkage = .internal, |
| | 9601 | .type = fn_type, |
| | 9602 | .kind = .{ .function = @enumFromInt(o.builder.functions.items.len) }, |
| | 9603 | }; |
| | 9604 | var function = Builder.Function{ |
| | 9605 | .global = @enumFromInt(o.builder.globals.count()), |
| | 9606 | }; |
| | 9607 | |
| | 9608 | try o.builder.llvm.globals.append(self.gpa, llvm_fn); |
| | 9609 | _ = try o.builder.addGlobal(name, global); |
| | 9610 | try o.builder.functions.append(self.gpa, function); |
| | 9611 | return global.kind.function; |
| 9129 | } | 9612 | } |
| 9130 | | 9613 | |
| 9131 | fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9614 | fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9132 | const o = self.dg.object; | 9615 | const o = self.dg.object; |
| 9133 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 9616 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 9134 | const operand = try self.resolveInst(un_op); | 9617 | const operand = try self.resolveInst(un_op); |
| ... | @@ -9136,34 +9619,32 @@ pub const FuncGen = struct { | ... | @@ -9136,34 +9619,32 @@ pub const FuncGen = struct { |
| 9136 | const slice_llvm_ty = try o.lowerType(slice_ty); | 9619 | const slice_llvm_ty = try o.lowerType(slice_ty); |
| 9137 | | 9620 | |
| 9138 | const error_name_table_ptr = try self.getErrorNameTable(); | 9621 | const error_name_table_ptr = try self.getErrorNameTable(); |
| 9139 | const ptr_slice_llvm_ty = self.context.pointerType(0); | 9622 | const error_name_table = |
| 9140 | const error_name_table = self.builder.buildLoad(ptr_slice_llvm_ty, error_name_table_ptr, ""); | 9623 | try self.wip.load(.normal, .ptr, error_name_table_ptr.toValue(&o.builder), .default, ""); |
| 9141 | const indices = [_]*llvm.Value{operand}; | 9624 | const error_name_ptr = |
| 9142 | const error_name_ptr = self.builder.buildInBoundsGEP(slice_llvm_ty, error_name_table, &indices, indices.len, ""); | 9625 | try self.wip.gep(.inbounds, slice_llvm_ty, error_name_table, &.{operand}, ""); |
| 9143 | return self.builder.buildLoad(slice_llvm_ty, error_name_ptr, ""); | 9626 | return self.wip.load(.normal, slice_llvm_ty, error_name_ptr, .default, ""); |
| 9144 | } | 9627 | } |
| 9145 | | 9628 | |
| 9146 | fn airSplat(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9629 | fn airSplat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9147 | const o = self.dg.object; | 9630 | const o = self.dg.object; |
| 9148 | const mod = o.module; | | |
| 9149 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 9631 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 9150 | const scalar = try self.resolveInst(ty_op.operand); | 9632 | const scalar = try self.resolveInst(ty_op.operand); |
| 9151 | const vector_ty = self.typeOfIndex(inst); | 9633 | const vector_ty = self.typeOfIndex(inst); |
| 9152 | const len = vector_ty.vectorLen(mod); | 9634 | return self.wip.splatVector(try o.lowerType(vector_ty), scalar, ""); |
| 9153 | return self.builder.buildVectorSplat(len, scalar, ""); | | |
| 9154 | } | 9635 | } |
| 9155 | | 9636 | |
| 9156 | fn airSelect(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9637 | fn airSelect(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9157 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 9638 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 9158 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; | 9639 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 9159 | const pred = try self.resolveInst(pl_op.operand); | 9640 | const pred = try self.resolveInst(pl_op.operand); |
| 9160 | const a = try self.resolveInst(extra.lhs); | 9641 | const a = try self.resolveInst(extra.lhs); |
| 9161 | const b = try self.resolveInst(extra.rhs); | 9642 | const b = try self.resolveInst(extra.rhs); |
| 9162 | | 9643 | |
| 9163 | return self.builder.buildSelect(pred, a, b, ""); | 9644 | return self.wip.select(pred, a, b, ""); |
| 9164 | } | 9645 | } |
| 9165 | | 9646 | |
| 9166 | fn airShuffle(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9647 | fn airShuffle(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9167 | const o = self.dg.object; | 9648 | const o = self.dg.object; |
| 9168 | const mod = o.module; | 9649 | const mod = o.module; |
| 9169 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 9650 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -9179,24 +9660,25 @@ pub const FuncGen = struct { | ... | @@ -9179,24 +9660,25 @@ pub const FuncGen = struct { |
| 9179 | // when changing code, so Zig uses negative numbers to index the | 9660 | // when changing code, so Zig uses negative numbers to index the |
| 9180 | // second vector. These start at -1 and go down, and are easiest to use | 9661 | // second vector. These start at -1 and go down, and are easiest to use |
| 9181 | // with the ~ operator. Here we convert between the two formats. | 9662 | // with the ~ operator. Here we convert between the two formats. |
| 9182 | const values = try self.gpa.alloc(*llvm.Value, mask_len); | 9663 | const values = try self.gpa.alloc(Builder.Constant, mask_len); |
| 9183 | defer self.gpa.free(values); | 9664 | defer self.gpa.free(values); |
| 9184 | | 9665 | |
| 9185 | const llvm_i32 = self.context.intType(32); | | |
| 9186 | | | |
| 9187 | for (values, 0..) |*val, i| { | 9666 | for (values, 0..) |*val, i| { |
| 9188 | const elem = try mask.elemValue(mod, i); | 9667 | const elem = try mask.elemValue(mod, i); |
| 9189 | if (elem.isUndef(mod)) { | 9668 | if (elem.isUndef(mod)) { |
| 9190 | val.* = llvm_i32.getUndef(); | 9669 | val.* = try o.builder.undefConst(.i32); |
| 9191 | } else { | 9670 | } else { |
| 9192 | const int = elem.toSignedInt(mod); | 9671 | const int = elem.toSignedInt(mod); |
| 9193 | const unsigned = if (int >= 0) @as(u32, @intCast(int)) else @as(u32, @intCast(~int + a_len)); | 9672 | const unsigned: u32 = @intCast(if (int >= 0) int else ~int + a_len); |
| 9194 | val.* = llvm_i32.constInt(unsigned, .False); | 9673 | val.* = try o.builder.intConst(.i32, unsigned); |
| 9195 | } | 9674 | } |
| 9196 | } | 9675 | } |
| 9197 | | 9676 | |
| 9198 | const llvm_mask_value = llvm.constVector(values.ptr, mask_len); | 9677 | const llvm_mask_value = try o.builder.vectorValue( |
| 9199 | return self.builder.buildShuffleVector(a, b, llvm_mask_value, ""); | 9678 | try o.builder.vectorType(.normal, mask_len, .i32), |
| | 9679 | values, |
| | 9680 | ); |
| | 9681 | return self.wip.shuffleVector(a, b, llvm_mask_value, ""); |
| 9200 | } | 9682 | } |
| 9201 | | 9683 | |
| 9202 | /// Reduce a vector by repeatedly applying `llvm_fn` to produce an accumulated result. | 9684 | /// Reduce a vector by repeatedly applying `llvm_fn` to produce an accumulated result. |
| ... | @@ -9213,58 +9695,69 @@ pub const FuncGen = struct { | ... | @@ -9213,58 +9695,69 @@ pub const FuncGen = struct { |
| 9213 | /// | 9695 | /// |
| 9214 | fn buildReducedCall( | 9696 | fn buildReducedCall( |
| 9215 | self: *FuncGen, | 9697 | self: *FuncGen, |
| 9216 | llvm_fn: *llvm.Value, | 9698 | llvm_fn: Builder.Function.Index, |
| 9217 | operand_vector: *llvm.Value, | 9699 | operand_vector: Builder.Value, |
| 9218 | vector_len: usize, | 9700 | vector_len: usize, |
| 9219 | accum_init: *llvm.Value, | 9701 | accum_init: Builder.Value, |
| 9220 | ) !*llvm.Value { | 9702 | ) !Builder.Value { |
| 9221 | const o = self.dg.object; | 9703 | const o = self.dg.object; |
| 9222 | const llvm_usize_ty = try o.lowerType(Type.usize); | 9704 | const usize_ty = try o.lowerType(Type.usize); |
| 9223 | const llvm_vector_len = llvm_usize_ty.constInt(vector_len, .False); | 9705 | const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len); |
| 9224 | const llvm_result_ty = accum_init.typeOf(); | 9706 | const llvm_result_ty = accum_init.typeOfWip(&self.wip); |
| 9225 | | 9707 | |
| 9226 | // Allocate and initialize our mutable variables | 9708 | // Allocate and initialize our mutable variables |
| 9227 | const i_ptr = self.buildAlloca(llvm_usize_ty, null); | 9709 | const i_ptr = try self.buildAlloca(usize_ty, .default); |
| 9228 | _ = self.builder.buildStore(llvm_usize_ty.constInt(0, .False), i_ptr); | 9710 | _ = try self.wip.store(.normal, try o.builder.intValue(usize_ty, 0), i_ptr, .default); |
| 9229 | const accum_ptr = self.buildAlloca(llvm_result_ty, null); | 9711 | const accum_ptr = try self.buildAlloca(llvm_result_ty, .default); |
| 9230 | _ = self.builder.buildStore(accum_init, accum_ptr); | 9712 | _ = try self.wip.store(.normal, accum_init, accum_ptr, .default); |
| 9231 | | 9713 | |
| 9232 | // Setup the loop | 9714 | // Setup the loop |
| 9233 | const loop = self.context.appendBasicBlock(self.llvm_func, "ReduceLoop"); | 9715 | const loop = try self.wip.block(2, "ReduceLoop"); |
| 9234 | const loop_exit = self.context.appendBasicBlock(self.llvm_func, "AfterReduce"); | 9716 | const loop_exit = try self.wip.block(1, "AfterReduce"); |
| 9235 | _ = self.builder.buildBr(loop); | 9717 | _ = try self.wip.br(loop); |
| 9236 | { | 9718 | { |
| 9237 | self.builder.positionBuilderAtEnd(loop); | 9719 | self.wip.cursor = .{ .block = loop }; |
| 9238 | | 9720 | |
| 9239 | // while (i < vec.len) | 9721 | // while (i < vec.len) |
| 9240 | const i = self.builder.buildLoad(llvm_usize_ty, i_ptr, ""); | 9722 | const i = try self.wip.load(.normal, usize_ty, i_ptr, .default, ""); |
| 9241 | const cond = self.builder.buildICmp(.ULT, i, llvm_vector_len, ""); | 9723 | const cond = try self.wip.icmp(.ult, i, llvm_vector_len, ""); |
| 9242 | const loop_then = self.context.appendBasicBlock(self.llvm_func, "ReduceLoopThen"); | 9724 | const loop_then = try self.wip.block(1, "ReduceLoopThen"); |
| 9243 | | 9725 | |
| 9244 | _ = self.builder.buildCondBr(cond, loop_then, loop_exit); | 9726 | _ = try self.wip.brCond(cond, loop_then, loop_exit); |
| 9245 | | 9727 | |
| 9246 | { | 9728 | { |
| 9247 | self.builder.positionBuilderAtEnd(loop_then); | 9729 | self.wip.cursor = .{ .block = loop_then }; |
| 9248 | | 9730 | |
| 9249 | // accum = f(accum, vec[i]); | 9731 | // accum = f(accum, vec[i]); |
| 9250 | const accum = self.builder.buildLoad(llvm_result_ty, accum_ptr, ""); | 9732 | const accum = try self.wip.load(.normal, llvm_result_ty, accum_ptr, .default, ""); |
| 9251 | const element = self.builder.buildExtractElement(operand_vector, i, ""); | 9733 | const element = try self.wip.extractElement(operand_vector, i, ""); |
| 9252 | const params = [2]*llvm.Value{ accum, element }; | 9734 | const params = [2]*llvm.Value{ accum.toLlvm(&self.wip), element.toLlvm(&self.wip) }; |
| 9253 | const new_accum = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &params, params.len, .C, .Auto, ""); | 9735 | const new_accum = (try self.wip.unimplemented(llvm_result_ty, "")).finish( |
| 9254 | _ = self.builder.buildStore(new_accum, accum_ptr); | 9736 | self.builder.buildCall( |
| | 9737 | llvm_fn.typeOf(&o.builder).toLlvm(&o.builder), |
| | 9738 | llvm_fn.toLlvm(&o.builder), |
| | 9739 | &params, |
| | 9740 | params.len, |
| | 9741 | .C, |
| | 9742 | .Auto, |
| | 9743 | "", |
| | 9744 | ), |
| | 9745 | &self.wip, |
| | 9746 | ); |
| | 9747 | _ = try self.wip.store(.normal, new_accum, accum_ptr, .default); |
| 9255 | | 9748 | |
| 9256 | // i += 1 | 9749 | // i += 1 |
| 9257 | const new_i = self.builder.buildAdd(i, llvm_usize_ty.constInt(1, .False), ""); | 9750 | const new_i = try self.wip.bin(.add, i, try o.builder.intValue(usize_ty, 1), ""); |
| 9258 | _ = self.builder.buildStore(new_i, i_ptr); | 9751 | _ = try self.wip.store(.normal, new_i, i_ptr, .default); |
| 9259 | _ = self.builder.buildBr(loop); | 9752 | _ = try self.wip.br(loop); |
| 9260 | } | 9753 | } |
| 9261 | } | 9754 | } |
| 9262 | | 9755 | |
| 9263 | self.builder.positionBuilderAtEnd(loop_exit); | 9756 | self.wip.cursor = .{ .block = loop_exit }; |
| 9264 | return self.builder.buildLoad(llvm_result_ty, accum_ptr, ""); | 9757 | return self.wip.load(.normal, llvm_result_ty, accum_ptr, .default, ""); |
| 9265 | } | 9758 | } |
| 9266 | | 9759 | |
| 9267 | fn airReduce(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !?*llvm.Value { | 9760 | fn airReduce(self: *FuncGen, inst: Air.Inst.Index, want_fast_math: bool) !Builder.Value { |
| 9268 | self.builder.setFastMath(want_fast_math); | 9761 | self.builder.setFastMath(want_fast_math); |
| 9269 | const o = self.dg.object; | 9762 | const o = self.dg.object; |
| 9270 | const mod = o.module; | 9763 | const mod = o.module; |
| ... | @@ -9274,40 +9767,70 @@ pub const FuncGen = struct { | ... | @@ -9274,40 +9767,70 @@ pub const FuncGen = struct { |
| 9274 | const operand = try self.resolveInst(reduce.operand); | 9767 | const operand = try self.resolveInst(reduce.operand); |
| 9275 | const operand_ty = self.typeOf(reduce.operand); | 9768 | const operand_ty = self.typeOf(reduce.operand); |
| 9276 | const scalar_ty = self.typeOfIndex(inst); | 9769 | const scalar_ty = self.typeOfIndex(inst); |
| | 9770 | const llvm_scalar_ty = try o.lowerType(scalar_ty); |
| 9277 | | 9771 | |
| 9278 | switch (reduce.operation) { | 9772 | switch (reduce.operation) { |
| 9279 | .And => return self.builder.buildAndReduce(operand), | 9773 | .And => return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| 9280 | .Or => return self.builder.buildOrReduce(operand), | 9774 | .finish(self.builder.buildAndReduce(operand.toLlvm(&self.wip)), &self.wip), |
| 9281 | .Xor => return self.builder.buildXorReduce(operand), | 9775 | .Or => return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9776 | .finish(self.builder.buildOrReduce(operand.toLlvm(&self.wip)), &self.wip), |
| | 9777 | .Xor => return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9778 | .finish(self.builder.buildXorReduce(operand.toLlvm(&self.wip)), &self.wip), |
| 9282 | .Min => switch (scalar_ty.zigTypeTag(mod)) { | 9779 | .Min => switch (scalar_ty.zigTypeTag(mod)) { |
| 9283 | .Int => return self.builder.buildIntMinReduce(operand, scalar_ty.isSignedInt(mod)), | 9780 | .Int => return (try self.wip.unimplemented(llvm_scalar_ty, "")).finish( |
| | 9781 | self.builder.buildIntMinReduce( |
| | 9782 | operand.toLlvm(&self.wip), |
| | 9783 | scalar_ty.isSignedInt(mod), |
| | 9784 | ), |
| | 9785 | &self.wip, |
| | 9786 | ), |
| 9284 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { | 9787 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { |
| 9285 | return self.builder.buildFPMinReduce(operand); | 9788 | return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9789 | .finish(self.builder.buildFPMinReduce(operand.toLlvm(&self.wip)), &self.wip); |
| 9286 | }, | 9790 | }, |
| 9287 | else => unreachable, | 9791 | else => unreachable, |
| 9288 | }, | 9792 | }, |
| 9289 | .Max => switch (scalar_ty.zigTypeTag(mod)) { | 9793 | .Max => switch (scalar_ty.zigTypeTag(mod)) { |
| 9290 | .Int => return self.builder.buildIntMaxReduce(operand, scalar_ty.isSignedInt(mod)), | 9794 | .Int => return (try self.wip.unimplemented(llvm_scalar_ty, "")).finish( |
| | 9795 | self.builder.buildIntMaxReduce( |
| | 9796 | operand.toLlvm(&self.wip), |
| | 9797 | scalar_ty.isSignedInt(mod), |
| | 9798 | ), |
| | 9799 | &self.wip, |
| | 9800 | ), |
| 9291 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { | 9801 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { |
| 9292 | return self.builder.buildFPMaxReduce(operand); | 9802 | return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9803 | .finish(self.builder.buildFPMaxReduce(operand.toLlvm(&self.wip)), &self.wip); |
| 9293 | }, | 9804 | }, |
| 9294 | else => unreachable, | 9805 | else => unreachable, |
| 9295 | }, | 9806 | }, |
| 9296 | .Add => switch (scalar_ty.zigTypeTag(mod)) { | 9807 | .Add => switch (scalar_ty.zigTypeTag(mod)) { |
| 9297 | .Int => return self.builder.buildAddReduce(operand), | 9808 | .Int => return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9809 | .finish(self.builder.buildAddReduce(operand.toLlvm(&self.wip)), &self.wip), |
| 9298 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { | 9810 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { |
| 9299 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | 9811 | const neutral_value = try o.builder.fpConst(llvm_scalar_ty, -0.0); |
| 9300 | const neutral_value = scalar_llvm_ty.constReal(-0.0); | 9812 | return (try self.wip.unimplemented(llvm_scalar_ty, "")).finish( |
| 9301 | return self.builder.buildFPAddReduce(neutral_value, operand); | 9813 | self.builder.buildFPAddReduce( |
| | 9814 | neutral_value.toLlvm(&o.builder), |
| | 9815 | operand.toLlvm(&self.wip), |
| | 9816 | ), |
| | 9817 | &self.wip, |
| | 9818 | ); |
| 9302 | }, | 9819 | }, |
| 9303 | else => unreachable, | 9820 | else => unreachable, |
| 9304 | }, | 9821 | }, |
| 9305 | .Mul => switch (scalar_ty.zigTypeTag(mod)) { | 9822 | .Mul => switch (scalar_ty.zigTypeTag(mod)) { |
| 9306 | .Int => return self.builder.buildMulReduce(operand), | 9823 | .Int => return (try self.wip.unimplemented(llvm_scalar_ty, "")) |
| | 9824 | .finish(self.builder.buildMulReduce(operand.toLlvm(&self.wip)), &self.wip), |
| 9307 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { | 9825 | .Float => if (intrinsicsAllowed(scalar_ty, target)) { |
| 9308 | const scalar_llvm_ty = try o.lowerType(scalar_ty); | 9826 | const neutral_value = try o.builder.fpConst(llvm_scalar_ty, 1.0); |
| 9309 | const neutral_value = scalar_llvm_ty.constReal(1.0); | 9827 | return (try self.wip.unimplemented(llvm_scalar_ty, "")).finish( |
| 9310 | return self.builder.buildFPMulReduce(neutral_value, operand); | 9828 | self.builder.buildFPMulReduce( |
| | 9829 | neutral_value.toLlvm(&o.builder), |
| | 9830 | operand.toLlvm(&self.wip), |
| | 9831 | ), |
| | 9832 | &self.wip, |
| | 9833 | ); |
| 9311 | }, | 9834 | }, |
| 9312 | else => unreachable, | 9835 | else => unreachable, |
| 9313 | }, | 9836 | }, |
| ... | @@ -9315,58 +9838,71 @@ pub const FuncGen = struct { | ... | @@ -9315,58 +9838,71 @@ pub const FuncGen = struct { |
| 9315 | | 9838 | |
| 9316 | // Reduction could not be performed with intrinsics. | 9839 | // Reduction could not be performed with intrinsics. |
| 9317 | // Use a manual loop over a softfloat call instead. | 9840 | // Use a manual loop over a softfloat call instead. |
| 9318 | var fn_name_buf: [64]u8 = undefined; | | |
| 9319 | const float_bits = scalar_ty.floatBits(target); | 9841 | const float_bits = scalar_ty.floatBits(target); |
| 9320 | const fn_name = switch (reduce.operation) { | 9842 | const fn_name = switch (reduce.operation) { |
| 9321 | .Min => std.fmt.bufPrintZ(&fn_name_buf, "{s}fmin{s}", .{ | 9843 | .Min => try o.builder.fmt("{s}fmin{s}", .{ |
| 9322 | libcFloatPrefix(float_bits), libcFloatSuffix(float_bits), | 9844 | libcFloatPrefix(float_bits), libcFloatSuffix(float_bits), |
| 9323 | }) catch unreachable, | 9845 | }), |
| 9324 | .Max => std.fmt.bufPrintZ(&fn_name_buf, "{s}fmax{s}", .{ | 9846 | .Max => try o.builder.fmt("{s}fmax{s}", .{ |
| 9325 | libcFloatPrefix(float_bits), libcFloatSuffix(float_bits), | 9847 | libcFloatPrefix(float_bits), libcFloatSuffix(float_bits), |
| 9326 | }) catch unreachable, | 9848 | }), |
| 9327 | .Add => std.fmt.bufPrintZ(&fn_name_buf, "__add{s}f3", .{ | 9849 | .Add => try o.builder.fmt("__add{s}f3", .{ |
| 9328 | compilerRtFloatAbbrev(float_bits), | 9850 | compilerRtFloatAbbrev(float_bits), |
| 9329 | }) catch unreachable, | 9851 | }), |
| 9330 | .Mul => std.fmt.bufPrintZ(&fn_name_buf, "__mul{s}f3", .{ | 9852 | .Mul => try o.builder.fmt("__mul{s}f3", .{ |
| 9331 | compilerRtFloatAbbrev(float_bits), | 9853 | compilerRtFloatAbbrev(float_bits), |
| 9332 | }) catch unreachable, | 9854 | }), |
| 9333 | else => unreachable, | 9855 | else => unreachable, |
| 9334 | }; | 9856 | }; |
| 9335 | | 9857 | |
| 9336 | const param_llvm_ty = try o.lowerType(scalar_ty); | 9858 | const libc_fn = |
| 9337 | const param_types = [2]*llvm.Type{ param_llvm_ty, param_llvm_ty }; | 9859 | try self.getLibcFunction(fn_name, &.{ llvm_scalar_ty, llvm_scalar_ty }, llvm_scalar_ty); |
| 9338 | const libc_fn = self.getLibcFunction(fn_name, &param_types, param_llvm_ty); | 9860 | const init_val = switch (llvm_scalar_ty) { |
| 9339 | const init_value = try o.lowerValue(.{ | 9861 | .i16 => try o.builder.intValue(.i16, @as(i16, @bitCast( |
| 9340 | .ty = scalar_ty, | 9862 | @as(f16, switch (reduce.operation) { |
| 9341 | .val = try mod.floatValue(scalar_ty, switch (reduce.operation) { | 9863 | .Min, .Max => std.math.nan(f16), |
| 9342 | .Min => std.math.nan(f32), | 9864 | .Add => -0.0, |
| 9343 | .Max => std.math.nan(f32), | 9865 | .Mul => 1.0, |
| 9344 | .Add => -0.0, | 9866 | else => unreachable, |
| 9345 | .Mul => 1.0, | 9867 | }), |
| 9346 | else => unreachable, | 9868 | ))), |
| 9347 | }), | 9869 | .i80 => try o.builder.intValue(.i80, @as(i80, @bitCast( |
| 9348 | }); | 9870 | @as(f80, switch (reduce.operation) { |
| 9349 | return self.buildReducedCall(libc_fn, operand, operand_ty.vectorLen(mod), init_value); | 9871 | .Min, .Max => std.math.nan(f80), |
| | 9872 | .Add => -0.0, |
| | 9873 | .Mul => 1.0, |
| | 9874 | else => unreachable, |
| | 9875 | }), |
| | 9876 | ))), |
| | 9877 | .i128 => try o.builder.intValue(.i128, @as(i128, @bitCast( |
| | 9878 | @as(f128, switch (reduce.operation) { |
| | 9879 | .Min, .Max => std.math.nan(f128), |
| | 9880 | .Add => -0.0, |
| | 9881 | .Mul => 1.0, |
| | 9882 | else => unreachable, |
| | 9883 | }), |
| | 9884 | ))), |
| | 9885 | else => unreachable, |
| | 9886 | }; |
| | 9887 | return self.buildReducedCall(libc_fn, operand, operand_ty.vectorLen(mod), init_val); |
| 9350 | } | 9888 | } |
| 9351 | | 9889 | |
| 9352 | fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 9890 | fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9353 | const o = self.dg.object; | 9891 | const o = self.dg.object; |
| 9354 | const mod = o.module; | 9892 | const mod = o.module; |
| 9355 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 9893 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 9356 | const result_ty = self.typeOfIndex(inst); | 9894 | const result_ty = self.typeOfIndex(inst); |
| 9357 | const len = @as(usize, @intCast(result_ty.arrayLen(mod))); | 9895 | const len: usize = @intCast(result_ty.arrayLen(mod)); |
| 9358 | const elements = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[ty_pl.payload..][0..len])); | 9896 | const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]); |
| 9359 | const llvm_result_ty = try o.lowerType(result_ty); | 9897 | const llvm_result_ty = try o.lowerType(result_ty); |
| 9360 | | 9898 | |
| 9361 | switch (result_ty.zigTypeTag(mod)) { | 9899 | switch (result_ty.zigTypeTag(mod)) { |
| 9362 | .Vector => { | 9900 | .Vector => { |
| 9363 | const llvm_u32 = self.context.intType(32); | 9901 | var vector = try o.builder.poisonValue(llvm_result_ty); |
| 9364 | | | |
| 9365 | var vector = llvm_result_ty.getUndef(); | | |
| 9366 | for (elements, 0..) |elem, i| { | 9902 | for (elements, 0..) |elem, i| { |
| 9367 | const index_u32 = llvm_u32.constInt(i, .False); | 9903 | const index_u32 = try o.builder.intValue(.i32, i); |
| 9368 | const llvm_elem = try self.resolveInst(elem); | 9904 | const llvm_elem = try self.resolveInst(elem); |
| 9369 | vector = self.builder.buildInsertElement(vector, llvm_elem, index_u32, ""); | 9905 | vector = try self.wip.insertElement(vector, llvm_elem, index_u32, ""); |
| 9370 | } | 9906 | } |
| 9371 | return vector; | 9907 | return vector; |
| 9372 | }, | 9908 | }, |
| ... | @@ -9375,48 +9911,47 @@ pub const FuncGen = struct { | ... | @@ -9375,48 +9911,47 @@ pub const FuncGen = struct { |
| 9375 | const struct_obj = mod.typeToStruct(result_ty).?; | 9911 | const struct_obj = mod.typeToStruct(result_ty).?; |
| 9376 | assert(struct_obj.haveLayout()); | 9912 | assert(struct_obj.haveLayout()); |
| 9377 | const big_bits = struct_obj.backing_int_ty.bitSize(mod); | 9913 | const big_bits = struct_obj.backing_int_ty.bitSize(mod); |
| 9378 | const int_llvm_ty = self.context.intType(@as(c_uint, @intCast(big_bits))); | 9914 | const int_ty = try o.builder.intType(@intCast(big_bits)); |
| 9379 | const fields = struct_obj.fields.values(); | 9915 | const fields = struct_obj.fields.values(); |
| 9380 | comptime assert(Type.packed_struct_layout_version == 2); | 9916 | comptime assert(Type.packed_struct_layout_version == 2); |
| 9381 | var running_int: *llvm.Value = int_llvm_ty.constNull(); | 9917 | var running_int = try o.builder.intValue(int_ty, 0); |
| 9382 | var running_bits: u16 = 0; | 9918 | var running_bits: u16 = 0; |
| 9383 | for (elements, 0..) |elem, i| { | 9919 | for (elements, 0..) |elem, i| { |
| 9384 | const field = fields[i]; | 9920 | const field = fields[i]; |
| 9385 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | 9921 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 9386 | | 9922 | |
| 9387 | const non_int_val = try self.resolveInst(elem); | 9923 | const non_int_val = try self.resolveInst(elem); |
| 9388 | const ty_bit_size = @as(u16, @intCast(field.ty.bitSize(mod))); | 9924 | const ty_bit_size: u16 = @intCast(field.ty.bitSize(mod)); |
| 9389 | const small_int_ty = self.context.intType(ty_bit_size); | 9925 | const small_int_ty = try o.builder.intType(ty_bit_size); |
| 9390 | const small_int_val = if (field.ty.isPtrAtRuntime(mod)) | 9926 | const small_int_val = if (field.ty.isPtrAtRuntime(mod)) |
| 9391 | self.builder.buildPtrToInt(non_int_val, small_int_ty, "") | 9927 | try self.wip.cast(.ptrtoint, non_int_val, small_int_ty, "") |
| 9392 | else | 9928 | else |
| 9393 | self.builder.buildBitCast(non_int_val, small_int_ty, ""); | 9929 | try self.wip.cast(.bitcast, non_int_val, small_int_ty, ""); |
| 9394 | const shift_rhs = int_llvm_ty.constInt(running_bits, .False); | 9930 | const shift_rhs = try o.builder.intValue(int_ty, running_bits); |
| 9395 | // If the field is as large as the entire packed struct, this | 9931 | // If the field is as large as the entire packed struct, this |
| 9396 | // zext would go from, e.g. i16 to i16. This is legal with | 9932 | // zext would go from, e.g. i16 to i16. This is legal with |
| 9397 | // constZExtOrBitCast but not legal with constZExt. | 9933 | // constZExtOrBitCast but not legal with constZExt. |
| 9398 | const extended_int_val = self.builder.buildZExtOrBitCast(small_int_val, int_llvm_ty, ""); | 9934 | const extended_int_val = try self.wip.conv(.unsigned, small_int_val, int_ty, ""); |
| 9399 | const shifted = self.builder.buildShl(extended_int_val, shift_rhs, ""); | 9935 | const shifted = try self.wip.bin(.shl, extended_int_val, shift_rhs, ""); |
| 9400 | running_int = self.builder.buildOr(running_int, shifted, ""); | 9936 | running_int = try self.wip.bin(.@"or", running_int, shifted, ""); |
| 9401 | running_bits += ty_bit_size; | 9937 | running_bits += ty_bit_size; |
| 9402 | } | 9938 | } |
| 9403 | return running_int; | 9939 | return running_int; |
| 9404 | } | 9940 | } |
| 9405 | | 9941 | |
| 9406 | if (isByRef(result_ty, mod)) { | 9942 | if (isByRef(result_ty, mod)) { |
| 9407 | const llvm_u32 = self.context.intType(32); | | |
| 9408 | // TODO in debug builds init to undef so that the padding will be 0xaa | 9943 | // TODO in debug builds init to undef so that the padding will be 0xaa |
| 9409 | // even if we fully populate the fields. | 9944 | // even if we fully populate the fields. |
| 9410 | const alloca_inst = self.buildAlloca(llvm_result_ty, result_ty.abiAlignment(mod)); | 9945 | const alignment = Builder.Alignment.fromByteUnits(result_ty.abiAlignment(mod)); |
| | 9946 | const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment); |
| 9411 | | 9947 | |
| 9412 | var indices: [2]*llvm.Value = .{ llvm_u32.constNull(), undefined }; | | |
| 9413 | for (elements, 0..) |elem, i| { | 9948 | for (elements, 0..) |elem, i| { |
| 9414 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | 9949 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; |
| 9415 | | 9950 | |
| 9416 | const llvm_elem = try self.resolveInst(elem); | 9951 | const llvm_elem = try self.resolveInst(elem); |
| 9417 | const llvm_i = llvmField(result_ty, i, mod).?.index; | 9952 | const llvm_i = llvmField(result_ty, i, mod).?.index; |
| 9418 | indices[1] = llvm_u32.constInt(llvm_i, .False); | 9953 | const field_ptr = |
| 9419 | const field_ptr = self.builder.buildInBoundsGEP(llvm_result_ty, alloca_inst, &indices, indices.len, ""); | 9954 | try self.wip.gepStruct(llvm_result_ty, alloca_inst, llvm_i, ""); |
| 9420 | const field_ptr_ty = try mod.ptrType(.{ | 9955 | const field_ptr_ty = try mod.ptrType(.{ |
| 9421 | .child = self.typeOf(elem).toIntern(), | 9956 | .child = self.typeOf(elem).toIntern(), |
| 9422 | .flags = .{ | 9957 | .flags = .{ |
| ... | @@ -9425,18 +9960,18 @@ pub const FuncGen = struct { | ... | @@ -9425,18 +9960,18 @@ pub const FuncGen = struct { |
| 9425 | ), | 9960 | ), |
| 9426 | }, | 9961 | }, |
| 9427 | }); | 9962 | }); |
| 9428 | try self.store(field_ptr, field_ptr_ty, llvm_elem, .NotAtomic); | 9963 | try self.store(field_ptr, field_ptr_ty, llvm_elem, .none); |
| 9429 | } | 9964 | } |
| 9430 | | 9965 | |
| 9431 | return alloca_inst; | 9966 | return alloca_inst; |
| 9432 | } else { | 9967 | } else { |
| 9433 | var result = llvm_result_ty.getUndef(); | 9968 | var result = try o.builder.poisonValue(llvm_result_ty); |
| 9434 | for (elements, 0..) |elem, i| { | 9969 | for (elements, 0..) |elem, i| { |
| 9435 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | 9970 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; |
| 9436 | | 9971 | |
| 9437 | const llvm_elem = try self.resolveInst(elem); | 9972 | const llvm_elem = try self.resolveInst(elem); |
| 9438 | const llvm_i = llvmField(result_ty, i, mod).?.index; | 9973 | const llvm_i = llvmField(result_ty, i, mod).?.index; |
| 9439 | result = self.builder.buildInsertValue(result, llvm_elem, llvm_i, ""); | 9974 | result = try self.wip.insertValue(result, llvm_elem, &.{llvm_i}, ""); |
| 9440 | } | 9975 | } |
| 9441 | return result; | 9976 | return result; |
| 9442 | } | 9977 | } |
| ... | @@ -9445,7 +9980,9 @@ pub const FuncGen = struct { | ... | @@ -9445,7 +9980,9 @@ pub const FuncGen = struct { |
| 9445 | assert(isByRef(result_ty, mod)); | 9980 | assert(isByRef(result_ty, mod)); |
| 9446 | | 9981 | |
| 9447 | const llvm_usize = try o.lowerType(Type.usize); | 9982 | const llvm_usize = try o.lowerType(Type.usize); |
| 9448 | const alloca_inst = self.buildAlloca(llvm_result_ty, result_ty.abiAlignment(mod)); | 9983 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| | 9984 | const alignment = Builder.Alignment.fromByteUnits(result_ty.abiAlignment(mod)); |
| | 9985 | const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment); |
| 9449 | | 9986 | |
| 9450 | const array_info = result_ty.arrayInfo(mod); | 9987 | const array_info = result_ty.arrayInfo(mod); |
| 9451 | const elem_ptr_ty = try mod.ptrType(.{ | 9988 | const elem_ptr_ty = try mod.ptrType(.{ |
| ... | @@ -9453,26 +9990,21 @@ pub const FuncGen = struct { | ... | @@ -9453,26 +9990,21 @@ pub const FuncGen = struct { |
| 9453 | }); | 9990 | }); |
| 9454 | | 9991 | |
| 9455 | for (elements, 0..) |elem, i| { | 9992 | for (elements, 0..) |elem, i| { |
| 9456 | const indices: [2]*llvm.Value = .{ | 9993 | const elem_ptr = try self.wip.gep(.inbounds, llvm_result_ty, alloca_inst, &.{ |
| 9457 | llvm_usize.constNull(), | 9994 | usize_zero, try o.builder.intValue(llvm_usize, i), |
| 9458 | llvm_usize.constInt(@as(c_uint, @intCast(i)), .False), | 9995 | }, ""); |
| 9459 | }; | | |
| 9460 | const elem_ptr = self.builder.buildInBoundsGEP(llvm_result_ty, alloca_inst, &indices, indices.len, ""); | | |
| 9461 | const llvm_elem = try self.resolveInst(elem); | 9996 | const llvm_elem = try self.resolveInst(elem); |
| 9462 | try self.store(elem_ptr, elem_ptr_ty, llvm_elem, .NotAtomic); | 9997 | try self.store(elem_ptr, elem_ptr_ty, llvm_elem, .none); |
| 9463 | } | 9998 | } |
| 9464 | if (array_info.sentinel) |sent_val| { | 9999 | if (array_info.sentinel) |sent_val| { |
| 9465 | const indices: [2]*llvm.Value = .{ | 10000 | const elem_ptr = try self.wip.gep(.inbounds, llvm_result_ty, alloca_inst, &.{ |
| 9466 | llvm_usize.constNull(), | 10001 | usize_zero, try o.builder.intValue(llvm_usize, array_info.len), |
| 9467 | llvm_usize.constInt(@as(c_uint, @intCast(array_info.len)), .False), | 10002 | }, ""); |
| 9468 | }; | | |
| 9469 | const elem_ptr = self.builder.buildInBoundsGEP(llvm_result_ty, alloca_inst, &indices, indices.len, ""); | | |
| 9470 | const llvm_elem = try self.resolveValue(.{ | 10003 | const llvm_elem = try self.resolveValue(.{ |
| 9471 | .ty = array_info.elem_type, | 10004 | .ty = array_info.elem_type, |
| 9472 | .val = sent_val, | 10005 | .val = sent_val, |
| 9473 | }); | 10006 | }); |
| 9474 | | 10007 | try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none); |
| 9475 | try self.store(elem_ptr, elem_ptr_ty, llvm_elem, .NotAtomic); | | |
| 9476 | } | 10008 | } |
| 9477 | | 10009 | |
| 9478 | return alloca_inst; | 10010 | return alloca_inst; |
| ... | @@ -9481,7 +10013,7 @@ pub const FuncGen = struct { | ... | @@ -9481,7 +10013,7 @@ pub const FuncGen = struct { |
| 9481 | } | 10013 | } |
| 9482 | } | 10014 | } |
| 9483 | | 10015 | |
| 9484 | fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10016 | fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9485 | const o = self.dg.object; | 10017 | const o = self.dg.object; |
| 9486 | const mod = o.module; | 10018 | const mod = o.module; |
| 9487 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 10019 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| ... | @@ -9493,16 +10025,15 @@ pub const FuncGen = struct { | ... | @@ -9493,16 +10025,15 @@ pub const FuncGen = struct { |
| 9493 | | 10025 | |
| 9494 | if (union_obj.layout == .Packed) { | 10026 | if (union_obj.layout == .Packed) { |
| 9495 | const big_bits = union_ty.bitSize(mod); | 10027 | const big_bits = union_ty.bitSize(mod); |
| 9496 | const int_llvm_ty = self.context.intType(@as(c_uint, @intCast(big_bits))); | 10028 | const int_llvm_ty = try o.builder.intType(@intCast(big_bits)); |
| 9497 | const field = union_obj.fields.values()[extra.field_index]; | 10029 | const field = union_obj.fields.values()[extra.field_index]; |
| 9498 | const non_int_val = try self.resolveInst(extra.init); | 10030 | const non_int_val = try self.resolveInst(extra.init); |
| 9499 | const ty_bit_size = @as(u16, @intCast(field.ty.bitSize(mod))); | 10031 | const small_int_ty = try o.builder.intType(@intCast(field.ty.bitSize(mod))); |
| 9500 | const small_int_ty = self.context.intType(ty_bit_size); | | |
| 9501 | const small_int_val = if (field.ty.isPtrAtRuntime(mod)) | 10032 | const small_int_val = if (field.ty.isPtrAtRuntime(mod)) |
| 9502 | self.builder.buildPtrToInt(non_int_val, small_int_ty, "") | 10033 | try self.wip.cast(.ptrtoint, non_int_val, small_int_ty, "") |
| 9503 | else | 10034 | else |
| 9504 | self.builder.buildBitCast(non_int_val, small_int_ty, ""); | 10035 | try self.wip.cast(.bitcast, non_int_val, small_int_ty, ""); |
| 9505 | return self.builder.buildZExtOrBitCast(small_int_val, int_llvm_ty, ""); | 10036 | return self.wip.conv(.unsigned, small_int_val, int_llvm_ty, ""); |
| 9506 | } | 10037 | } |
| 9507 | | 10038 | |
| 9508 | const tag_int = blk: { | 10039 | const tag_int = blk: { |
| ... | @@ -9515,106 +10046,96 @@ pub const FuncGen = struct { | ... | @@ -9515,106 +10046,96 @@ pub const FuncGen = struct { |
| 9515 | }; | 10046 | }; |
| 9516 | if (layout.payload_size == 0) { | 10047 | if (layout.payload_size == 0) { |
| 9517 | if (layout.tag_size == 0) { | 10048 | if (layout.tag_size == 0) { |
| 9518 | return null; | 10049 | return .none; |
| 9519 | } | 10050 | } |
| 9520 | assert(!isByRef(union_ty, mod)); | 10051 | assert(!isByRef(union_ty, mod)); |
| 9521 | return union_llvm_ty.constInt(tag_int, .False); | 10052 | return o.builder.intValue(union_llvm_ty, tag_int); |
| 9522 | } | 10053 | } |
| 9523 | assert(isByRef(union_ty, mod)); | 10054 | assert(isByRef(union_ty, mod)); |
| 9524 | // The llvm type of the alloca will be the named LLVM union type, and will not | 10055 | // The llvm type of the alloca will be the named LLVM union type, and will not |
| 9525 | // necessarily match the format that we need, depending on which tag is active. | 10056 | // necessarily match the format that we need, depending on which tag is active. |
| 9526 | // We must construct the correct unnamed struct type here, in order to then set | 10057 | // We must construct the correct unnamed struct type here, in order to then set |
| 9527 | // the fields appropriately. | 10058 | // the fields appropriately. |
| 9528 | const result_ptr = self.buildAlloca(union_llvm_ty, layout.abi_align); | 10059 | const alignment = Builder.Alignment.fromByteUnits(layout.abi_align); |
| | 10060 | const result_ptr = try self.buildAlloca(union_llvm_ty, alignment); |
| 9529 | const llvm_payload = try self.resolveInst(extra.init); | 10061 | const llvm_payload = try self.resolveInst(extra.init); |
| 9530 | assert(union_obj.haveFieldTypes()); | 10062 | assert(union_obj.haveFieldTypes()); |
| 9531 | const field = union_obj.fields.values()[extra.field_index]; | 10063 | const field = union_obj.fields.values()[extra.field_index]; |
| 9532 | const field_llvm_ty = try o.lowerType(field.ty); | 10064 | const field_llvm_ty = try o.lowerType(field.ty); |
| 9533 | const field_size = field.ty.abiSize(mod); | 10065 | const field_size = field.ty.abiSize(mod); |
| 9534 | const field_align = field.normalAlignment(mod); | 10066 | const field_align = field.normalAlignment(mod); |
| | 10067 | const llvm_usize = try o.lowerType(Type.usize); |
| | 10068 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| | 10069 | const i32_zero = try o.builder.intValue(.i32, 0); |
| 9535 | | 10070 | |
| 9536 | const llvm_union_ty = t: { | 10071 | const llvm_union_ty = t: { |
| 9537 | const payload = p: { | 10072 | const payload_ty = p: { |
| 9538 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) { | 10073 | if (!field.ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 9539 | const padding_len = @as(c_uint, @intCast(layout.payload_size)); | 10074 | const padding_len = layout.payload_size; |
| 9540 | break :p self.context.intType(8).arrayType(padding_len); | 10075 | break :p try o.builder.arrayType(padding_len, .i8); |
| 9541 | } | 10076 | } |
| 9542 | if (field_size == layout.payload_size) { | 10077 | if (field_size == layout.payload_size) { |
| 9543 | break :p field_llvm_ty; | 10078 | break :p field_llvm_ty; |
| 9544 | } | 10079 | } |
| 9545 | const padding_len = @as(c_uint, @intCast(layout.payload_size - field_size)); | 10080 | const padding_len = layout.payload_size - field_size; |
| 9546 | const fields: [2]*llvm.Type = .{ | 10081 | break :p try o.builder.structType(.@"packed", &.{ |
| 9547 | field_llvm_ty, self.context.intType(8).arrayType(padding_len), | 10082 | field_llvm_ty, try o.builder.arrayType(padding_len, .i8), |
| 9548 | }; | 10083 | }); |
| 9549 | break :p self.context.structType(&fields, fields.len, .True); | | |
| 9550 | }; | 10084 | }; |
| 9551 | if (layout.tag_size == 0) { | 10085 | if (layout.tag_size == 0) break :t try o.builder.structType(.normal, &.{payload_ty}); |
| 9552 | const fields: [1]*llvm.Type = .{payload}; | 10086 | const tag_ty = try o.lowerType(union_obj.tag_ty); |
| 9553 | break :t self.context.structType(&fields, fields.len, .False); | 10087 | var fields: [3]Builder.Type = undefined; |
| 9554 | } | 10088 | var fields_len: usize = 2; |
| 9555 | const tag_llvm_ty = try o.lowerType(union_obj.tag_ty); | | |
| 9556 | var fields: [3]*llvm.Type = undefined; | | |
| 9557 | var fields_len: c_uint = 2; | | |
| 9558 | if (layout.tag_align >= layout.payload_align) { | 10089 | if (layout.tag_align >= layout.payload_align) { |
| 9559 | fields = .{ tag_llvm_ty, payload, undefined }; | 10090 | fields = .{ tag_ty, payload_ty, undefined }; |
| 9560 | } else { | 10091 | } else { |
| 9561 | fields = .{ payload, tag_llvm_ty, undefined }; | 10092 | fields = .{ payload_ty, tag_ty, undefined }; |
| 9562 | } | 10093 | } |
| 9563 | if (layout.padding != 0) { | 10094 | if (layout.padding != 0) { |
| 9564 | fields[2] = self.context.intType(8).arrayType(layout.padding); | 10095 | fields[fields_len] = try o.builder.arrayType(layout.padding, .i8); |
| 9565 | fields_len = 3; | 10096 | fields_len += 1; |
| 9566 | } | 10097 | } |
| 9567 | break :t self.context.structType(&fields, fields_len, .False); | 10098 | break :t try o.builder.structType(.normal, fields[0..fields_len]); |
| 9568 | }; | 10099 | }; |
| 9569 | | 10100 | |
| 9570 | // Now we follow the layout as expressed above with GEP instructions to set the | 10101 | // Now we follow the layout as expressed above with GEP instructions to set the |
| 9571 | // tag and the payload. | 10102 | // tag and the payload. |
| 9572 | const index_type = self.context.intType(32); | | |
| 9573 | | | |
| 9574 | const field_ptr_ty = try mod.ptrType(.{ | 10103 | const field_ptr_ty = try mod.ptrType(.{ |
| 9575 | .child = field.ty.toIntern(), | 10104 | .child = field.ty.toIntern(), |
| 9576 | .flags = .{ | 10105 | .flags = .{ .alignment = InternPool.Alignment.fromNonzeroByteUnits(field_align) }, |
| 9577 | .alignment = InternPool.Alignment.fromNonzeroByteUnits(field_align), | | |
| 9578 | }, | | |
| 9579 | }); | 10106 | }); |
| 9580 | if (layout.tag_size == 0) { | 10107 | if (layout.tag_size == 0) { |
| 9581 | const indices: [3]*llvm.Value = .{ | 10108 | const indices = [3]Builder.Value{ usize_zero, i32_zero, i32_zero }; |
| 9582 | index_type.constNull(), | 10109 | const len: usize = if (field_size == layout.payload_size) 2 else 3; |
| 9583 | index_type.constNull(), | 10110 | const field_ptr = |
| 9584 | index_type.constNull(), | 10111 | try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], ""); |
| 9585 | }; | 10112 | try self.store(field_ptr, field_ptr_ty, llvm_payload, .none); |
| 9586 | const len: c_uint = if (field_size == layout.payload_size) 2 else 3; | | |
| 9587 | const field_ptr = self.builder.buildInBoundsGEP(llvm_union_ty, result_ptr, &indices, len, ""); | | |
| 9588 | try self.store(field_ptr, field_ptr_ty, llvm_payload, .NotAtomic); | | |
| 9589 | return result_ptr; | 10113 | return result_ptr; |
| 9590 | } | 10114 | } |
| 9591 | | 10115 | |
| 9592 | { | 10116 | { |
| 9593 | const indices: [3]*llvm.Value = .{ | 10117 | const payload_index = @intFromBool(layout.tag_align >= layout.payload_align); |
| 9594 | index_type.constNull(), | 10118 | const indices: [3]Builder.Value = |
| 9595 | index_type.constInt(@intFromBool(layout.tag_align >= layout.payload_align), .False), | 10119 | .{ usize_zero, try o.builder.intValue(.i32, payload_index), i32_zero }; |
| 9596 | index_type.constNull(), | 10120 | const len: usize = if (field_size == layout.payload_size) 2 else 3; |
| 9597 | }; | 10121 | const field_ptr = |
| 9598 | const len: c_uint = if (field_size == layout.payload_size) 2 else 3; | 10122 | try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], ""); |
| 9599 | const field_ptr = self.builder.buildInBoundsGEP(llvm_union_ty, result_ptr, &indices, len, ""); | 10123 | try self.store(field_ptr, field_ptr_ty, llvm_payload, .none); |
| 9600 | try self.store(field_ptr, field_ptr_ty, llvm_payload, .NotAtomic); | | |
| 9601 | } | 10124 | } |
| 9602 | { | 10125 | { |
| 9603 | const indices: [2]*llvm.Value = .{ | 10126 | const tag_index = @intFromBool(layout.tag_align < layout.payload_align); |
| 9604 | index_type.constNull(), | 10127 | const indices: [2]Builder.Value = .{ usize_zero, try o.builder.intValue(.i32, tag_index) }; |
| 9605 | index_type.constInt(@intFromBool(layout.tag_align < layout.payload_align), .False), | 10128 | const field_ptr = try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, &indices, ""); |
| 9606 | }; | 10129 | const tag_ty = try o.lowerType(union_obj.tag_ty); |
| 9607 | const field_ptr = self.builder.buildInBoundsGEP(llvm_union_ty, result_ptr, &indices, indices.len, ""); | 10130 | const llvm_tag = try o.builder.intValue(tag_ty, tag_int); |
| 9608 | const tag_llvm_ty = try o.lowerType(union_obj.tag_ty); | 10131 | const tag_alignment = Builder.Alignment.fromByteUnits(union_obj.tag_ty.abiAlignment(mod)); |
| 9609 | const llvm_tag = tag_llvm_ty.constInt(tag_int, .False); | 10132 | _ = try self.wip.store(.normal, llvm_tag, field_ptr, tag_alignment); |
| 9610 | const store_inst = self.builder.buildStore(llvm_tag, field_ptr); | | |
| 9611 | store_inst.setAlignment(union_obj.tag_ty.abiAlignment(mod)); | | |
| 9612 | } | 10133 | } |
| 9613 | | 10134 | |
| 9614 | return result_ptr; | 10135 | return result_ptr; |
| 9615 | } | 10136 | } |
| 9616 | | 10137 | |
| 9617 | fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10138 | fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9618 | const o = self.dg.object; | 10139 | const o = self.dg.object; |
| 9619 | const prefetch = self.air.instructions.items(.data)[inst].prefetch; | 10140 | const prefetch = self.air.instructions.items(.data)[inst].prefetch; |
| 9620 | | 10141 | |
| ... | @@ -9643,10 +10164,10 @@ pub const FuncGen = struct { | ... | @@ -9643,10 +10164,10 @@ pub const FuncGen = struct { |
| 9643 | .powerpcle, | 10164 | .powerpcle, |
| 9644 | .powerpc64, | 10165 | .powerpc64, |
| 9645 | .powerpc64le, | 10166 | .powerpc64le, |
| 9646 | => return null, | 10167 | => return .none, |
| 9647 | .arm, .armeb, .thumb, .thumbeb => { | 10168 | .arm, .armeb, .thumb, .thumbeb => { |
| 9648 | switch (prefetch.rw) { | 10169 | switch (prefetch.rw) { |
| 9649 | .write => return null, | 10170 | .write => return .none, |
| 9650 | else => {}, | 10171 | else => {}, |
| 9651 | } | 10172 | } |
| 9652 | }, | 10173 | }, |
| ... | @@ -9655,58 +10176,64 @@ pub const FuncGen = struct { | ... | @@ -9655,58 +10176,64 @@ pub const FuncGen = struct { |
| 9655 | .data => {}, | 10176 | .data => {}, |
| 9656 | } | 10177 | } |
| 9657 | | 10178 | |
| 9658 | const llvm_ptr_u8 = self.context.pointerType(0); | | |
| 9659 | const llvm_u32 = self.context.intType(32); | | |
| 9660 | | | |
| 9661 | const llvm_fn_name = "llvm.prefetch.p0"; | 10179 | const llvm_fn_name = "llvm.prefetch.p0"; |
| 9662 | const fn_val = o.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: { | 10180 | // declare void @llvm.prefetch(i8*, i32, i32, i32) |
| 9663 | // declare void @llvm.prefetch(i8*, i32, i32, i32) | 10181 | const llvm_fn_ty = try o.builder.fnType(.void, &.{ .ptr, .i32, .i32, .i32 }, .normal); |
| 9664 | const llvm_void = self.context.voidType(); | 10182 | const fn_val = o.llvm_module.getNamedFunction(llvm_fn_name) orelse |
| 9665 | const param_types = [_]*llvm.Type{ | 10183 | o.llvm_module.addFunction(llvm_fn_name, llvm_fn_ty.toLlvm(&o.builder)); |
| 9666 | llvm_ptr_u8, llvm_u32, llvm_u32, llvm_u32, | | |
| 9667 | }; | | |
| 9668 | const fn_type = llvm.functionType(llvm_void, &param_types, param_types.len, .False); | | |
| 9669 | break :blk o.llvm_module.addFunction(llvm_fn_name, fn_type); | | |
| 9670 | }; | | |
| 9671 | | 10184 | |
| 9672 | const ptr = try self.resolveInst(prefetch.ptr); | 10185 | const ptr = try self.resolveInst(prefetch.ptr); |
| 9673 | | 10186 | |
| 9674 | const params = [_]*llvm.Value{ | 10187 | const params = [_]*llvm.Value{ |
| 9675 | ptr, | 10188 | ptr.toLlvm(&self.wip), |
| 9676 | llvm_u32.constInt(@intFromEnum(prefetch.rw), .False), | 10189 | (try o.builder.intConst(.i32, @intFromEnum(prefetch.rw))).toLlvm(&o.builder), |
| 9677 | llvm_u32.constInt(prefetch.locality, .False), | 10190 | (try o.builder.intConst(.i32, prefetch.locality)).toLlvm(&o.builder), |
| 9678 | llvm_u32.constInt(@intFromEnum(prefetch.cache), .False), | 10191 | (try o.builder.intConst(.i32, @intFromEnum(prefetch.cache))).toLlvm(&o.builder), |
| 9679 | }; | 10192 | }; |
| 9680 | _ = self.builder.buildCall(fn_val.globalGetValueType(), fn_val, &params, params.len, .C, .Auto, ""); | 10193 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildCall( |
| 9681 | return null; | 10194 | llvm_fn_ty.toLlvm(&o.builder), |
| | 10195 | fn_val, |
| | 10196 | &params, |
| | 10197 | params.len, |
| | 10198 | .C, |
| | 10199 | .Auto, |
| | 10200 | "", |
| | 10201 | ), &self.wip); |
| | 10202 | return .none; |
| 9682 | } | 10203 | } |
| 9683 | | 10204 | |
| 9684 | fn airAddrSpaceCast(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10205 | fn airAddrSpaceCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9685 | const o = self.dg.object; | 10206 | const o = self.dg.object; |
| 9686 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 10207 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 9687 | const inst_ty = self.typeOfIndex(inst); | 10208 | const inst_ty = self.typeOfIndex(inst); |
| 9688 | const operand = try self.resolveInst(ty_op.operand); | 10209 | const operand = try self.resolveInst(ty_op.operand); |
| 9689 | | 10210 | |
| 9690 | const llvm_dest_ty = try o.lowerType(inst_ty); | 10211 | return self.wip.cast(.addrspacecast, operand, try o.lowerType(inst_ty), ""); |
| 9691 | return self.builder.buildAddrSpaceCast(operand, llvm_dest_ty, ""); | | |
| 9692 | } | 10212 | } |
| 9693 | | 10213 | |
| 9694 | fn amdgcnWorkIntrinsic(self: *FuncGen, dimension: u32, default: u32, comptime basename: []const u8) !?*llvm.Value { | 10214 | fn amdgcnWorkIntrinsic(self: *FuncGen, dimension: u32, default: u32, comptime basename: []const u8) !Builder.Value { |
| 9695 | const llvm_u32 = self.context.intType(32); | 10215 | const o = self.dg.object; |
| 9696 | | | |
| 9697 | const llvm_fn_name = switch (dimension) { | 10216 | const llvm_fn_name = switch (dimension) { |
| 9698 | 0 => basename ++ ".x", | 10217 | 0 => basename ++ ".x", |
| 9699 | 1 => basename ++ ".y", | 10218 | 1 => basename ++ ".y", |
| 9700 | 2 => basename ++ ".z", | 10219 | 2 => basename ++ ".z", |
| 9701 | else => return llvm_u32.constInt(default, .False), | 10220 | else => return o.builder.intValue(.i32, default), |
| 9702 | }; | 10221 | }; |
| 9703 | | 10222 | |
| 9704 | const args: [0]*llvm.Value = .{}; | 10223 | const args: [0]*llvm.Value = .{}; |
| 9705 | const llvm_fn = self.getIntrinsic(llvm_fn_name, &.{}); | 10224 | const llvm_fn = try self.getIntrinsic(llvm_fn_name, &.{}); |
| 9706 | return self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 10225 | return (try self.wip.unimplemented(.i32, "")).finish(self.builder.buildCall( |
| | 10226 | (try o.builder.fnType(.i32, &.{}, .normal)).toLlvm(&o.builder), |
| | 10227 | llvm_fn, |
| | 10228 | &args, |
| | 10229 | args.len, |
| | 10230 | .Fast, |
| | 10231 | .Auto, |
| | 10232 | "", |
| | 10233 | ), &self.wip); |
| 9707 | } | 10234 | } |
| 9708 | | 10235 | |
| 9709 | fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10236 | fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9710 | const o = self.dg.object; | 10237 | const o = self.dg.object; |
| 9711 | const target = o.module.getTarget(); | 10238 | const target = o.module.getTarget(); |
| 9712 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures | 10239 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| ... | @@ -9716,37 +10243,41 @@ pub const FuncGen = struct { | ... | @@ -9716,37 +10243,41 @@ pub const FuncGen = struct { |
| 9716 | return self.amdgcnWorkIntrinsic(dimension, 0, "llvm.amdgcn.workitem.id"); | 10243 | return self.amdgcnWorkIntrinsic(dimension, 0, "llvm.amdgcn.workitem.id"); |
| 9717 | } | 10244 | } |
| 9718 | | 10245 | |
| 9719 | fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10246 | fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9720 | const o = self.dg.object; | 10247 | const o = self.dg.object; |
| 9721 | const target = o.module.getTarget(); | 10248 | const target = o.module.getTarget(); |
| 9722 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures | 10249 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| 9723 | | 10250 | |
| 9724 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 10251 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 9725 | const dimension = pl_op.payload; | 10252 | const dimension = pl_op.payload; |
| 9726 | const llvm_u32 = self.context.intType(32); | 10253 | if (dimension >= 3) return o.builder.intValue(.i32, 1); |
| 9727 | if (dimension >= 3) { | | |
| 9728 | return llvm_u32.constInt(1, .False); | | |
| 9729 | } | | |
| 9730 | | 10254 | |
| 9731 | // Fetch the dispatch pointer, which points to this structure: | 10255 | // Fetch the dispatch pointer, which points to this structure: |
| 9732 | // https://github.com/RadeonOpenCompute/ROCR-Runtime/blob/adae6c61e10d371f7cbc3d0e94ae2c070cab18a4/src/inc/hsa.h#L2913 | 10256 | // https://github.com/RadeonOpenCompute/ROCR-Runtime/blob/adae6c61e10d371f7cbc3d0e94ae2c070cab18a4/src/inc/hsa.h#L2913 |
| 9733 | const llvm_fn = self.getIntrinsic("llvm.amdgcn.dispatch.ptr", &.{}); | 10257 | const llvm_fn = try self.getIntrinsic("llvm.amdgcn.dispatch.ptr", &.{}); |
| 9734 | const args: [0]*llvm.Value = .{}; | 10258 | const args: [0]*llvm.Value = .{}; |
| 9735 | const dispatch_ptr = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, &args, args.len, .Fast, .Auto, ""); | 10259 | const llvm_ret_ty = try o.builder.ptrType(Builder.AddrSpace.amdgpu.constant); |
| 9736 | dispatch_ptr.setAlignment(4); | 10260 | const dispatch_ptr = (try self.wip.unimplemented(llvm_ret_ty, "")).finish(self.builder.buildCall( |
| | 10261 | (try o.builder.fnType(llvm_ret_ty, &.{}, .normal)).toLlvm(&o.builder), |
| | 10262 | llvm_fn, |
| | 10263 | &args, |
| | 10264 | args.len, |
| | 10265 | .Fast, |
| | 10266 | .Auto, |
| | 10267 | "", |
| | 10268 | ), &self.wip); |
| | 10269 | o.addAttrInt(dispatch_ptr.toLlvm(&self.wip), 0, "align", 4); |
| 9737 | | 10270 | |
| 9738 | // Load the work_group_* member from the struct as u16. | 10271 | // Load the work_group_* member from the struct as u16. |
| 9739 | // Just treat the dispatch pointer as an array of u16 to keep things simple. | 10272 | // Just treat the dispatch pointer as an array of u16 to keep things simple. |
| 9740 | const offset = 2 + dimension; | 10273 | const workgroup_size_ptr = try self.wip.gep(.inbounds, .i16, dispatch_ptr, &.{ |
| 9741 | const index = [_]*llvm.Value{llvm_u32.constInt(offset, .False)}; | 10274 | try o.builder.intValue(try o.lowerType(Type.usize), 2 + dimension), |
| 9742 | const llvm_u16 = self.context.intType(16); | 10275 | }, ""); |
| 9743 | const workgroup_size_ptr = self.builder.buildInBoundsGEP(llvm_u16, dispatch_ptr, &index, index.len, ""); | 10276 | const workgroup_size_alignment = comptime Builder.Alignment.fromByteUnits(2); |
| 9744 | const workgroup_size = self.builder.buildLoad(llvm_u16, workgroup_size_ptr, ""); | 10277 | return self.wip.load(.normal, .i16, workgroup_size_ptr, workgroup_size_alignment, ""); |
| 9745 | workgroup_size.setAlignment(2); | | |
| 9746 | return workgroup_size; | | |
| 9747 | } | 10278 | } |
| 9748 | | 10279 | |
| 9749 | fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value { | 10280 | fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9750 | const o = self.dg.object; | 10281 | const o = self.dg.object; |
| 9751 | const target = o.module.getTarget(); | 10282 | const target = o.module.getTarget(); |
| 9752 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures | 10283 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| ... | @@ -9756,65 +10287,82 @@ pub const FuncGen = struct { | ... | @@ -9756,65 +10287,82 @@ pub const FuncGen = struct { |
| 9756 | return self.amdgcnWorkIntrinsic(dimension, 0, "llvm.amdgcn.workgroup.id"); | 10287 | return self.amdgcnWorkIntrinsic(dimension, 0, "llvm.amdgcn.workgroup.id"); |
| 9757 | } | 10288 | } |
| 9758 | | 10289 | |
| 9759 | fn getErrorNameTable(self: *FuncGen) !*llvm.Value { | 10290 | fn getErrorNameTable(self: *FuncGen) Allocator.Error!Builder.Variable.Index { |
| 9760 | const o = self.dg.object; | 10291 | const o = self.dg.object; |
| 9761 | if (o.error_name_table) |table| { | 10292 | const table = o.error_name_table; |
| 9762 | return table; | 10293 | if (table != .none) return table; |
| 9763 | } | | |
| 9764 | | 10294 | |
| 9765 | const mod = o.module; | 10295 | const mod = o.module; |
| 9766 | const slice_ty = Type.slice_const_u8_sentinel_0; | 10296 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| 9767 | const slice_alignment = slice_ty.abiAlignment(mod); | 10297 | const slice_alignment = slice_ty.abiAlignment(mod); |
| 9768 | const llvm_slice_ptr_ty = self.context.pointerType(0); // TODO: Address space | 10298 | const undef_init = try o.builder.undefConst(.ptr); // TODO: Address space |
| 9769 | | 10299 | |
| 9770 | const error_name_table_global = o.llvm_module.addGlobal(llvm_slice_ptr_ty, "__zig_err_name_table"); | 10300 | const name = try o.builder.string("__zig_err_name_table"); |
| 9771 | error_name_table_global.setInitializer(llvm_slice_ptr_ty.getUndef()); | 10301 | const error_name_table_global = o.llvm_module.addGlobal(Builder.Type.ptr.toLlvm(&o.builder), name.toSlice(&o.builder).?); |
| | 10302 | error_name_table_global.setInitializer(undef_init.toLlvm(&o.builder)); |
| 9772 | error_name_table_global.setLinkage(.Private); | 10303 | error_name_table_global.setLinkage(.Private); |
| 9773 | error_name_table_global.setGlobalConstant(.True); | 10304 | error_name_table_global.setGlobalConstant(.True); |
| 9774 | error_name_table_global.setUnnamedAddr(.True); | 10305 | error_name_table_global.setUnnamedAddr(.True); |
| 9775 | error_name_table_global.setAlignment(slice_alignment); | 10306 | error_name_table_global.setAlignment(slice_alignment); |
| 9776 | | 10307 | |
| 9777 | o.error_name_table = error_name_table_global; | 10308 | var global = Builder.Global{ |
| 9778 | return error_name_table_global; | 10309 | .linkage = .private, |
| | 10310 | .unnamed_addr = .unnamed_addr, |
| | 10311 | .type = .ptr, |
| | 10312 | .kind = .{ .variable = @enumFromInt(o.builder.variables.items.len) }, |
| | 10313 | }; |
| | 10314 | var variable = Builder.Variable{ |
| | 10315 | .global = @enumFromInt(o.builder.globals.count()), |
| | 10316 | .mutability = .constant, |
| | 10317 | .init = undef_init, |
| | 10318 | .alignment = Builder.Alignment.fromByteUnits(slice_alignment), |
| | 10319 | }; |
| | 10320 | try o.builder.llvm.globals.append(o.gpa, error_name_table_global); |
| | 10321 | _ = try o.builder.addGlobal(name, global); |
| | 10322 | try o.builder.variables.append(o.gpa, variable); |
| | 10323 | |
| | 10324 | o.error_name_table = global.kind.variable; |
| | 10325 | return global.kind.variable; |
| 9779 | } | 10326 | } |
| 9780 | | 10327 | |
| 9781 | /// Assumes the optional is not pointer-like and payload has bits. | 10328 | /// Assumes the optional is not pointer-like and payload has bits. |
| 9782 | fn optIsNonNull( | 10329 | fn optCmpNull( |
| 9783 | self: *FuncGen, | 10330 | self: *FuncGen, |
| 9784 | opt_llvm_ty: *llvm.Type, | 10331 | cond: Builder.IntegerCondition, |
| 9785 | opt_handle: *llvm.Value, | 10332 | opt_llvm_ty: Builder.Type, |
| | 10333 | opt_handle: Builder.Value, |
| 9786 | is_by_ref: bool, | 10334 | is_by_ref: bool, |
| 9787 | ) *llvm.Value { | 10335 | ) Allocator.Error!Builder.Value { |
| 9788 | const non_null_llvm_ty = self.context.intType(8); | 10336 | const o = self.dg.object; |
| 9789 | const field = b: { | 10337 | const field = b: { |
| 9790 | if (is_by_ref) { | 10338 | if (is_by_ref) { |
| 9791 | const field_ptr = self.builder.buildStructGEP(opt_llvm_ty, opt_handle, 1, ""); | 10339 | const field_ptr = try self.wip.gepStruct(opt_llvm_ty, opt_handle, 1, ""); |
| 9792 | break :b self.builder.buildLoad(non_null_llvm_ty, field_ptr, ""); | 10340 | break :b try self.wip.load(.normal, .i8, field_ptr, .default, ""); |
| 9793 | } | 10341 | } |
| 9794 | break :b self.builder.buildExtractValue(opt_handle, 1, ""); | 10342 | break :b try self.wip.extractValue(opt_handle, &.{1}, ""); |
| 9795 | }; | 10343 | }; |
| 9796 | comptime assert(optional_layout_version == 3); | 10344 | comptime assert(optional_layout_version == 3); |
| 9797 | | 10345 | |
| 9798 | return self.builder.buildICmp(.NE, field, non_null_llvm_ty.constInt(0, .False), ""); | 10346 | return self.wip.icmp(cond, field, try o.builder.intValue(.i8, 0), ""); |
| 9799 | } | 10347 | } |
| 9800 | | 10348 | |
| 9801 | /// Assumes the optional is not pointer-like and payload has bits. | 10349 | /// Assumes the optional is not pointer-like and payload has bits. |
| 9802 | fn optPayloadHandle( | 10350 | fn optPayloadHandle( |
| 9803 | fg: *FuncGen, | 10351 | fg: *FuncGen, |
| 9804 | opt_llvm_ty: *llvm.Type, | 10352 | opt_llvm_ty: Builder.Type, |
| 9805 | opt_handle: *llvm.Value, | 10353 | opt_handle: Builder.Value, |
| 9806 | opt_ty: Type, | 10354 | opt_ty: Type, |
| 9807 | can_elide_load: bool, | 10355 | can_elide_load: bool, |
| 9808 | ) !*llvm.Value { | 10356 | ) !Builder.Value { |
| 9809 | const o = fg.dg.object; | 10357 | const o = fg.dg.object; |
| 9810 | const mod = o.module; | 10358 | const mod = o.module; |
| 9811 | const payload_ty = opt_ty.optionalChild(mod); | 10359 | const payload_ty = opt_ty.optionalChild(mod); |
| 9812 | | 10360 | |
| 9813 | if (isByRef(opt_ty, mod)) { | 10361 | if (isByRef(opt_ty, mod)) { |
| 9814 | // We have a pointer and we need to return a pointer to the first field. | 10362 | // We have a pointer and we need to return a pointer to the first field. |
| 9815 | const payload_ptr = fg.builder.buildStructGEP(opt_llvm_ty, opt_handle, 0, ""); | 10363 | const payload_ptr = try fg.wip.gepStruct(opt_llvm_ty, opt_handle, 0, ""); |
| 9816 | | 10364 | |
| 9817 | const payload_alignment = payload_ty.abiAlignment(mod); | 10365 | const payload_alignment = Builder.Alignment.fromByteUnits(payload_ty.abiAlignment(mod)); |
| 9818 | if (isByRef(payload_ty, mod)) { | 10366 | if (isByRef(payload_ty, mod)) { |
| 9819 | if (can_elide_load) | 10367 | if (can_elide_load) |
| 9820 | return payload_ptr; | 10368 | return payload_ptr; |
| ... | @@ -9822,55 +10370,51 @@ pub const FuncGen = struct { | ... | @@ -9822,55 +10370,51 @@ pub const FuncGen = struct { |
| 9822 | return fg.loadByRef(payload_ptr, payload_ty, payload_alignment, false); | 10370 | return fg.loadByRef(payload_ptr, payload_ty, payload_alignment, false); |
| 9823 | } | 10371 | } |
| 9824 | const payload_llvm_ty = try o.lowerType(payload_ty); | 10372 | const payload_llvm_ty = try o.lowerType(payload_ty); |
| 9825 | const load_inst = fg.builder.buildLoad(payload_llvm_ty, payload_ptr, ""); | 10373 | return fg.wip.load(.normal, payload_llvm_ty, payload_ptr, payload_alignment, ""); |
| 9826 | load_inst.setAlignment(payload_alignment); | | |
| 9827 | return load_inst; | | |
| 9828 | } | 10374 | } |
| 9829 | | 10375 | |
| 9830 | assert(!isByRef(payload_ty, mod)); | 10376 | assert(!isByRef(payload_ty, mod)); |
| 9831 | return fg.builder.buildExtractValue(opt_handle, 0, ""); | 10377 | return fg.wip.extractValue(opt_handle, &.{0}, ""); |
| 9832 | } | 10378 | } |
| 9833 | | 10379 | |
| 9834 | fn buildOptional( | 10380 | fn buildOptional( |
| 9835 | self: *FuncGen, | 10381 | self: *FuncGen, |
| 9836 | optional_ty: Type, | 10382 | optional_ty: Type, |
| 9837 | payload: *llvm.Value, | 10383 | payload: Builder.Value, |
| 9838 | non_null_bit: *llvm.Value, | 10384 | non_null_bit: Builder.Value, |
| 9839 | ) !?*llvm.Value { | 10385 | ) !Builder.Value { |
| 9840 | const o = self.dg.object; | 10386 | const o = self.dg.object; |
| 9841 | const optional_llvm_ty = try o.lowerType(optional_ty); | 10387 | const optional_llvm_ty = try o.lowerType(optional_ty); |
| 9842 | const non_null_field = self.builder.buildZExt(non_null_bit, self.context.intType(8), ""); | 10388 | const non_null_field = try self.wip.cast(.zext, non_null_bit, .i8, ""); |
| 9843 | const mod = o.module; | 10389 | const mod = o.module; |
| 9844 | | 10390 | |
| 9845 | if (isByRef(optional_ty, mod)) { | 10391 | if (isByRef(optional_ty, mod)) { |
| 9846 | const payload_alignment = optional_ty.abiAlignment(mod); | 10392 | const payload_alignment = Builder.Alignment.fromByteUnits(optional_ty.abiAlignment(mod)); |
| 9847 | const alloca_inst = self.buildAlloca(optional_llvm_ty, payload_alignment); | 10393 | const alloca_inst = try self.buildAlloca(optional_llvm_ty, payload_alignment); |
| 9848 | | 10394 | |
| 9849 | { | 10395 | { |
| 9850 | const field_ptr = self.builder.buildStructGEP(optional_llvm_ty, alloca_inst, 0, ""); | 10396 | const field_ptr = try self.wip.gepStruct(optional_llvm_ty, alloca_inst, 0, ""); |
| 9851 | const store_inst = self.builder.buildStore(payload, field_ptr); | 10397 | _ = try self.wip.store(.normal, payload, field_ptr, payload_alignment); |
| 9852 | store_inst.setAlignment(payload_alignment); | | |
| 9853 | } | 10398 | } |
| 9854 | { | 10399 | { |
| 9855 | const field_ptr = self.builder.buildStructGEP(optional_llvm_ty, alloca_inst, 1, ""); | 10400 | const non_null_alignment = comptime Builder.Alignment.fromByteUnits(1); |
| 9856 | const store_inst = self.builder.buildStore(non_null_field, field_ptr); | 10401 | const field_ptr = try self.wip.gepStruct(optional_llvm_ty, alloca_inst, 1, ""); |
| 9857 | store_inst.setAlignment(1); | 10402 | _ = try self.wip.store(.normal, non_null_field, field_ptr, non_null_alignment); |
| 9858 | } | 10403 | } |
| 9859 | | 10404 | |
| 9860 | return alloca_inst; | 10405 | return alloca_inst; |
| 9861 | } | 10406 | } |
| 9862 | | 10407 | |
| 9863 | const partial = self.builder.buildInsertValue(optional_llvm_ty.getUndef(), payload, 0, ""); | 10408 | return self.wip.buildAggregate(optional_llvm_ty, &.{ payload, non_null_field }, ""); |
| 9864 | return self.builder.buildInsertValue(partial, non_null_field, 1, ""); | | |
| 9865 | } | 10409 | } |
| 9866 | | 10410 | |
| 9867 | fn fieldPtr( | 10411 | fn fieldPtr( |
| 9868 | self: *FuncGen, | 10412 | self: *FuncGen, |
| 9869 | inst: Air.Inst.Index, | 10413 | inst: Air.Inst.Index, |
| 9870 | struct_ptr: *llvm.Value, | 10414 | struct_ptr: Builder.Value, |
| 9871 | struct_ptr_ty: Type, | 10415 | struct_ptr_ty: Type, |
| 9872 | field_index: u32, | 10416 | field_index: u32, |
| 9873 | ) !?*llvm.Value { | 10417 | ) !Builder.Value { |
| 9874 | const o = self.dg.object; | 10418 | const o = self.dg.object; |
| 9875 | const mod = o.module; | 10419 | const mod = o.module; |
| 9876 | const struct_ty = struct_ptr_ty.childType(mod); | 10420 | const struct_ty = struct_ptr_ty.childType(mod); |
| ... | @@ -9892,26 +10436,25 @@ pub const FuncGen = struct { | ... | @@ -9892,26 +10436,25 @@ pub const FuncGen = struct { |
| 9892 | // Offset our operand pointer by the correct number of bytes. | 10436 | // Offset our operand pointer by the correct number of bytes. |
| 9893 | const byte_offset = struct_ty.packedStructFieldByteOffset(field_index, mod); | 10437 | const byte_offset = struct_ty.packedStructFieldByteOffset(field_index, mod); |
| 9894 | if (byte_offset == 0) return struct_ptr; | 10438 | if (byte_offset == 0) return struct_ptr; |
| 9895 | const byte_llvm_ty = self.context.intType(8); | 10439 | const usize_ty = try o.lowerType(Type.usize); |
| 9896 | const llvm_usize = try o.lowerType(Type.usize); | 10440 | const llvm_index = try o.builder.intValue(usize_ty, byte_offset); |
| 9897 | const llvm_index = llvm_usize.constInt(byte_offset, .False); | 10441 | return self.wip.gep(.inbounds, .i8, struct_ptr, &.{llvm_index}, ""); |
| 9898 | const indices: [1]*llvm.Value = .{llvm_index}; | | |
| 9899 | return self.builder.buildInBoundsGEP(byte_llvm_ty, struct_ptr, &indices, indices.len, ""); | | |
| 9900 | }, | 10442 | }, |
| 9901 | else => { | 10443 | else => { |
| 9902 | const struct_llvm_ty = try o.lowerPtrElemTy(struct_ty); | 10444 | const struct_llvm_ty = try o.lowerPtrElemTy(struct_ty); |
| 9903 | | 10445 | |
| 9904 | if (llvmField(struct_ty, field_index, mod)) |llvm_field| { | 10446 | if (llvmField(struct_ty, field_index, mod)) |llvm_field| { |
| 9905 | return self.builder.buildStructGEP(struct_llvm_ty, struct_ptr, llvm_field.index, ""); | 10447 | return self.wip.gepStruct(struct_llvm_ty, struct_ptr, llvm_field.index, ""); |
| 9906 | } else { | 10448 | } else { |
| 9907 | // If we found no index then this means this is a zero sized field at the | 10449 | // If we found no index then this means this is a zero sized field at the |
| 9908 | // end of the struct. Treat our struct pointer as an array of two and get | 10450 | // end of the struct. Treat our struct pointer as an array of two and get |
| 9909 | // the index to the element at index `1` to get a pointer to the end of | 10451 | // the index to the element at index `1` to get a pointer to the end of |
| 9910 | // the struct. | 10452 | // the struct. |
| 9911 | const llvm_u32 = self.context.intType(32); | 10453 | const llvm_index = try o.builder.intValue( |
| 9912 | const llvm_index = llvm_u32.constInt(@intFromBool(struct_ty.hasRuntimeBitsIgnoreComptime(mod)), .False); | 10454 | try o.lowerType(Type.usize), |
| 9913 | const indices: [1]*llvm.Value = .{llvm_index}; | 10455 | @intFromBool(struct_ty.hasRuntimeBitsIgnoreComptime(mod)), |
| 9914 | return self.builder.buildInBoundsGEP(struct_llvm_ty, struct_ptr, &indices, indices.len, ""); | 10456 | ); |
| | 10457 | return self.wip.gep(.inbounds, struct_llvm_ty, struct_ptr, &.{llvm_index}, ""); |
| 9915 | } | 10458 | } |
| 9916 | }, | 10459 | }, |
| 9917 | }, | 10460 | }, |
| ... | @@ -9920,126 +10463,128 @@ pub const FuncGen = struct { | ... | @@ -9920,126 +10463,128 @@ pub const FuncGen = struct { |
| 9920 | if (layout.payload_size == 0 or struct_ty.containerLayout(mod) == .Packed) return struct_ptr; | 10463 | if (layout.payload_size == 0 or struct_ty.containerLayout(mod) == .Packed) return struct_ptr; |
| 9921 | const payload_index = @intFromBool(layout.tag_align >= layout.payload_align); | 10464 | const payload_index = @intFromBool(layout.tag_align >= layout.payload_align); |
| 9922 | const union_llvm_ty = try o.lowerType(struct_ty); | 10465 | const union_llvm_ty = try o.lowerType(struct_ty); |
| 9923 | const union_field_ptr = self.builder.buildStructGEP(union_llvm_ty, struct_ptr, payload_index, ""); | 10466 | return self.wip.gepStruct(union_llvm_ty, struct_ptr, payload_index, ""); |
| 9924 | return union_field_ptr; | | |
| 9925 | }, | 10467 | }, |
| 9926 | else => unreachable, | 10468 | else => unreachable, |
| 9927 | } | 10469 | } |
| 9928 | } | 10470 | } |
| 9929 | | 10471 | |
| 9930 | fn getIntrinsic(fg: *FuncGen, name: []const u8, types: []const *llvm.Type) *llvm.Value { | 10472 | fn getIntrinsic( |
| | 10473 | fg: *FuncGen, |
| | 10474 | name: []const u8, |
| | 10475 | types: []const Builder.Type, |
| | 10476 | ) Allocator.Error!*llvm.Value { |
| | 10477 | const o = fg.dg.object; |
| 9931 | const id = llvm.lookupIntrinsicID(name.ptr, name.len); | 10478 | const id = llvm.lookupIntrinsicID(name.ptr, name.len); |
| 9932 | assert(id != 0); | 10479 | assert(id != 0); |
| 9933 | const o = fg.dg.object; | 10480 | const llvm_types = try o.gpa.alloc(*llvm.Type, types.len); |
| 9934 | return o.llvm_module.getIntrinsicDeclaration(id, types.ptr, types.len); | 10481 | defer o.gpa.free(llvm_types); |
| | 10482 | for (llvm_types, types) |*llvm_type, ty| llvm_type.* = ty.toLlvm(&o.builder); |
| | 10483 | return o.llvm_module.getIntrinsicDeclaration(id, llvm_types.ptr, llvm_types.len); |
| 9935 | } | 10484 | } |
| 9936 | | 10485 | |
| 9937 | /// Load a by-ref type by constructing a new alloca and performing a memcpy. | 10486 | /// Load a by-ref type by constructing a new alloca and performing a memcpy. |
| 9938 | fn loadByRef( | 10487 | fn loadByRef( |
| 9939 | fg: *FuncGen, | 10488 | fg: *FuncGen, |
| 9940 | ptr: *llvm.Value, | 10489 | ptr: Builder.Value, |
| 9941 | pointee_type: Type, | 10490 | pointee_type: Type, |
| 9942 | ptr_alignment: u32, | 10491 | ptr_alignment: Builder.Alignment, |
| 9943 | is_volatile: bool, | 10492 | is_volatile: bool, |
| 9944 | ) !*llvm.Value { | 10493 | ) !Builder.Value { |
| 9945 | const o = fg.dg.object; | 10494 | const o = fg.dg.object; |
| 9946 | const mod = o.module; | 10495 | const mod = o.module; |
| 9947 | const pointee_llvm_ty = try o.lowerType(pointee_type); | 10496 | const pointee_llvm_ty = try o.lowerType(pointee_type); |
| 9948 | const result_align = @max(ptr_alignment, pointee_type.abiAlignment(mod)); | 10497 | const result_align = Builder.Alignment.fromByteUnits( |
| 9949 | const result_ptr = fg.buildAlloca(pointee_llvm_ty, result_align); | 10498 | @max(ptr_alignment.toByteUnits() orelse 0, pointee_type.abiAlignment(mod)), |
| 9950 | const llvm_usize = fg.context.intType(Type.usize.intInfo(mod).bits); | 10499 | ); |
| | 10500 | const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align); |
| | 10501 | const usize_ty = try o.lowerType(Type.usize); |
| 9951 | const size_bytes = pointee_type.abiSize(mod); | 10502 | const size_bytes = pointee_type.abiSize(mod); |
| 9952 | _ = fg.builder.buildMemCpy( | 10503 | _ = (try fg.wip.unimplemented(.void, "")).finish(fg.builder.buildMemCpy( |
| 9953 | result_ptr, | 10504 | result_ptr.toLlvm(&fg.wip), |
| 9954 | result_align, | 10505 | @intCast(result_align.toByteUnits() orelse 0), |
| 9955 | ptr, | 10506 | ptr.toLlvm(&fg.wip), |
| 9956 | ptr_alignment, | 10507 | @intCast(ptr_alignment.toByteUnits() orelse 0), |
| 9957 | llvm_usize.constInt(size_bytes, .False), | 10508 | (try o.builder.intConst(usize_ty, size_bytes)).toLlvm(&o.builder), |
| 9958 | is_volatile, | 10509 | is_volatile, |
| 9959 | ); | 10510 | ), &fg.wip); |
| 9960 | return result_ptr; | 10511 | return result_ptr; |
| 9961 | } | 10512 | } |
| 9962 | | 10513 | |
| 9963 | /// This function always performs a copy. For isByRef=true types, it creates a new | 10514 | /// This function always performs a copy. For isByRef=true types, it creates a new |
| 9964 | /// alloca and copies the value into it, then returns the alloca instruction. | 10515 | /// alloca and copies the value into it, then returns the alloca instruction. |
| 9965 | /// For isByRef=false types, it creates a load instruction and returns it. | 10516 | /// For isByRef=false types, it creates a load instruction and returns it. |
| 9966 | fn load(self: *FuncGen, ptr: *llvm.Value, ptr_ty: Type) !?*llvm.Value { | 10517 | fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) !Builder.Value { |
| 9967 | const o = self.dg.object; | 10518 | const o = self.dg.object; |
| 9968 | const mod = o.module; | 10519 | const mod = o.module; |
| 9969 | const info = ptr_ty.ptrInfo(mod); | 10520 | const info = ptr_ty.ptrInfo(mod); |
| 9970 | const elem_ty = info.child.toType(); | 10521 | const elem_ty = info.child.toType(); |
| 9971 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return null; | 10522 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; |
| 9972 | | 10523 | |
| 9973 | const ptr_alignment = @as(u32, @intCast(info.flags.alignment.toByteUnitsOptional() orelse | 10524 | const ptr_alignment = Builder.Alignment.fromByteUnits( |
| 9974 | elem_ty.abiAlignment(mod))); | 10525 | info.flags.alignment.toByteUnitsOptional() orelse elem_ty.abiAlignment(mod), |
| 9975 | const ptr_volatile = llvm.Bool.fromBool(info.flags.is_volatile); | 10526 | ); |
| | 10527 | const ptr_kind: Builder.MemoryAccessKind = switch (info.flags.is_volatile) { |
| | 10528 | false => .normal, |
| | 10529 | true => .@"volatile", |
| | 10530 | }; |
| 9976 | | 10531 | |
| 9977 | assert(info.flags.vector_index != .runtime); | 10532 | assert(info.flags.vector_index != .runtime); |
| 9978 | if (info.flags.vector_index != .none) { | 10533 | if (info.flags.vector_index != .none) { |
| 9979 | const index_u32 = self.context.intType(32).constInt(@intFromEnum(info.flags.vector_index), .False); | 10534 | const index_u32 = try o.builder.intValue(.i32, @intFromEnum(info.flags.vector_index)); |
| 9980 | const vec_elem_ty = try o.lowerType(elem_ty); | 10535 | const vec_elem_ty = try o.lowerType(elem_ty); |
| 9981 | const vec_ty = vec_elem_ty.vectorType(info.packed_offset.host_size); | 10536 | const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty); |
| 9982 | | | |
| 9983 | const loaded_vector = self.builder.buildLoad(vec_ty, ptr, ""); | | |
| 9984 | loaded_vector.setAlignment(ptr_alignment); | | |
| 9985 | loaded_vector.setVolatile(ptr_volatile); | | |
| 9986 | | 10537 | |
| 9987 | return self.builder.buildExtractElement(loaded_vector, index_u32, ""); | 10538 | const loaded_vector = try self.wip.load(ptr_kind, vec_ty, ptr, ptr_alignment, ""); |
| | 10539 | return self.wip.extractElement(loaded_vector, index_u32, ""); |
| 9988 | } | 10540 | } |
| 9989 | | 10541 | |
| 9990 | if (info.packed_offset.host_size == 0) { | 10542 | if (info.packed_offset.host_size == 0) { |
| 9991 | if (isByRef(elem_ty, mod)) { | 10543 | if (isByRef(elem_ty, mod)) { |
| 9992 | return self.loadByRef(ptr, elem_ty, ptr_alignment, info.flags.is_volatile); | 10544 | return self.loadByRef(ptr, elem_ty, ptr_alignment, info.flags.is_volatile); |
| 9993 | } | 10545 | } |
| 9994 | const elem_llvm_ty = try o.lowerType(elem_ty); | 10546 | return self.wip.load(ptr_kind, try o.lowerType(elem_ty), ptr, ptr_alignment, ""); |
| 9995 | const llvm_inst = self.builder.buildLoad(elem_llvm_ty, ptr, ""); | | |
| 9996 | llvm_inst.setAlignment(ptr_alignment); | | |
| 9997 | llvm_inst.setVolatile(ptr_volatile); | | |
| 9998 | return llvm_inst; | | |
| 9999 | } | 10547 | } |
| 10000 | | 10548 | |
| 10001 | const int_elem_ty = self.context.intType(info.packed_offset.host_size * 8); | 10549 | const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8)); |
| 10002 | const containing_int = self.builder.buildLoad(int_elem_ty, ptr, ""); | 10550 | const containing_int = try self.wip.load(ptr_kind, containing_int_ty, ptr, ptr_alignment, ""); |
| 10003 | containing_int.setAlignment(ptr_alignment); | | |
| 10004 | containing_int.setVolatile(ptr_volatile); | | |
| 10005 | | 10551 | |
| 10006 | const elem_bits = @as(c_uint, @intCast(ptr_ty.childType(mod).bitSize(mod))); | 10552 | const elem_bits = ptr_ty.childType(mod).bitSize(mod); |
| 10007 | const shift_amt = containing_int.typeOf().constInt(info.packed_offset.bit_offset, .False); | 10553 | const shift_amt = try o.builder.intValue(containing_int_ty, info.packed_offset.bit_offset); |
| 10008 | const shifted_value = self.builder.buildLShr(containing_int, shift_amt, ""); | 10554 | const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, ""); |
| 10009 | const elem_llvm_ty = try o.lowerType(elem_ty); | 10555 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 10010 | | 10556 | |
| 10011 | if (isByRef(elem_ty, mod)) { | 10557 | if (isByRef(elem_ty, mod)) { |
| 10012 | const result_align = elem_ty.abiAlignment(mod); | 10558 | const result_align = Builder.Alignment.fromByteUnits(elem_ty.abiAlignment(mod)); |
| 10013 | const result_ptr = self.buildAlloca(elem_llvm_ty, result_align); | 10559 | const result_ptr = try self.buildAlloca(elem_llvm_ty, result_align); |
| 10014 | | 10560 | |
| 10015 | const same_size_int = self.context.intType(elem_bits); | 10561 | const same_size_int = try o.builder.intType(@intCast(elem_bits)); |
| 10016 | const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, ""); | 10562 | const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 10017 | const store_inst = self.builder.buildStore(truncated_int, result_ptr); | 10563 | _ = try self.wip.store(.normal, truncated_int, result_ptr, result_align); |
| 10018 | store_inst.setAlignment(result_align); | | |
| 10019 | return result_ptr; | 10564 | return result_ptr; |
| 10020 | } | 10565 | } |
| 10021 | | 10566 | |
| 10022 | if (elem_ty.zigTypeTag(mod) == .Float or elem_ty.zigTypeTag(mod) == .Vector) { | 10567 | if (elem_ty.zigTypeTag(mod) == .Float or elem_ty.zigTypeTag(mod) == .Vector) { |
| 10023 | const same_size_int = self.context.intType(elem_bits); | 10568 | const same_size_int = try o.builder.intType(@intCast(elem_bits)); |
| 10024 | const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, ""); | 10569 | const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 10025 | return self.builder.buildBitCast(truncated_int, elem_llvm_ty, ""); | 10570 | return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, ""); |
| 10026 | } | 10571 | } |
| 10027 | | 10572 | |
| 10028 | if (elem_ty.isPtrAtRuntime(mod)) { | 10573 | if (elem_ty.isPtrAtRuntime(mod)) { |
| 10029 | const same_size_int = self.context.intType(elem_bits); | 10574 | const same_size_int = try o.builder.intType(@intCast(elem_bits)); |
| 10030 | const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, ""); | 10575 | const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 10031 | return self.builder.buildIntToPtr(truncated_int, elem_llvm_ty, ""); | 10576 | return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, ""); |
| 10032 | } | 10577 | } |
| 10033 | | 10578 | |
| 10034 | return self.builder.buildTrunc(shifted_value, elem_llvm_ty, ""); | 10579 | return self.wip.cast(.trunc, shifted_value, elem_llvm_ty, ""); |
| 10035 | } | 10580 | } |
| 10036 | | 10581 | |
| 10037 | fn store( | 10582 | fn store( |
| 10038 | self: *FuncGen, | 10583 | self: *FuncGen, |
| 10039 | ptr: *llvm.Value, | 10584 | ptr: Builder.Value, |
| 10040 | ptr_ty: Type, | 10585 | ptr_ty: Type, |
| 10041 | elem: *llvm.Value, | 10586 | elem: Builder.Value, |
| 10042 | ordering: llvm.AtomicOrdering, | 10587 | ordering: Builder.AtomicOrdering, |
| 10043 | ) !void { | 10588 | ) !void { |
| 10044 | const o = self.dg.object; | 10589 | const o = self.dg.object; |
| 10045 | const mod = o.module; | 10590 | const mod = o.module; |
| ... | @@ -10048,124 +10593,115 @@ pub const FuncGen = struct { | ... | @@ -10048,124 +10593,115 @@ pub const FuncGen = struct { |
| 10048 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | 10593 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 10049 | return; | 10594 | return; |
| 10050 | } | 10595 | } |
| 10051 | const ptr_alignment = ptr_ty.ptrAlignment(mod); | 10596 | const ptr_alignment = Builder.Alignment.fromByteUnits(ptr_ty.ptrAlignment(mod)); |
| 10052 | const ptr_volatile = llvm.Bool.fromBool(info.flags.is_volatile); | 10597 | const ptr_kind: Builder.MemoryAccessKind = switch (info.flags.is_volatile) { |
| | 10598 | false => .normal, |
| | 10599 | true => .@"volatile", |
| | 10600 | }; |
| 10053 | | 10601 | |
| 10054 | assert(info.flags.vector_index != .runtime); | 10602 | assert(info.flags.vector_index != .runtime); |
| 10055 | if (info.flags.vector_index != .none) { | 10603 | if (info.flags.vector_index != .none) { |
| 10056 | const index_u32 = self.context.intType(32).constInt(@intFromEnum(info.flags.vector_index), .False); | 10604 | const index_u32 = try o.builder.intValue(.i32, @intFromEnum(info.flags.vector_index)); |
| 10057 | const vec_elem_ty = try o.lowerType(elem_ty); | 10605 | const vec_elem_ty = try o.lowerType(elem_ty); |
| 10058 | const vec_ty = vec_elem_ty.vectorType(info.packed_offset.host_size); | 10606 | const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty); |
| 10059 | | 10607 | |
| 10060 | const loaded_vector = self.builder.buildLoad(vec_ty, ptr, ""); | 10608 | const loaded_vector = try self.wip.load(ptr_kind, vec_ty, ptr, ptr_alignment, ""); |
| 10061 | loaded_vector.setAlignment(ptr_alignment); | | |
| 10062 | loaded_vector.setVolatile(ptr_volatile); | | |
| 10063 | | 10609 | |
| 10064 | const modified_vector = self.builder.buildInsertElement(loaded_vector, elem, index_u32, ""); | 10610 | const modified_vector = try self.wip.insertElement(loaded_vector, elem, index_u32, ""); |
| 10065 | | 10611 | |
| 10066 | const store_inst = self.builder.buildStore(modified_vector, ptr); | 10612 | assert(ordering == .none); |
| 10067 | assert(ordering == .NotAtomic); | 10613 | _ = try self.wip.store(ptr_kind, modified_vector, ptr, ptr_alignment); |
| 10068 | store_inst.setAlignment(ptr_alignment); | | |
| 10069 | store_inst.setVolatile(ptr_volatile); | | |
| 10070 | return; | 10614 | return; |
| 10071 | } | 10615 | } |
| 10072 | | 10616 | |
| 10073 | if (info.packed_offset.host_size != 0) { | 10617 | if (info.packed_offset.host_size != 0) { |
| 10074 | const int_elem_ty = self.context.intType(info.packed_offset.host_size * 8); | 10618 | const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8)); |
| 10075 | const containing_int = self.builder.buildLoad(int_elem_ty, ptr, ""); | 10619 | assert(ordering == .none); |
| 10076 | assert(ordering == .NotAtomic); | 10620 | const containing_int = |
| 10077 | containing_int.setAlignment(ptr_alignment); | 10621 | try self.wip.load(ptr_kind, containing_int_ty, ptr, ptr_alignment, ""); |
| 10078 | containing_int.setVolatile(ptr_volatile); | 10622 | const elem_bits = ptr_ty.childType(mod).bitSize(mod); |
| 10079 | const elem_bits = @as(c_uint, @intCast(ptr_ty.childType(mod).bitSize(mod))); | 10623 | const shift_amt = try o.builder.intConst(containing_int_ty, info.packed_offset.bit_offset); |
| 10080 | const containing_int_ty = containing_int.typeOf(); | | |
| 10081 | const shift_amt = containing_int_ty.constInt(info.packed_offset.bit_offset, .False); | | |
| 10082 | // Convert to equally-sized integer type in order to perform the bit | 10624 | // Convert to equally-sized integer type in order to perform the bit |
| 10083 | // operations on the value to store | 10625 | // operations on the value to store |
| 10084 | const value_bits_type = self.context.intType(elem_bits); | 10626 | const value_bits_type = try o.builder.intType(@intCast(elem_bits)); |
| 10085 | const value_bits = if (elem_ty.isPtrAtRuntime(mod)) | 10627 | const value_bits = if (elem_ty.isPtrAtRuntime(mod)) |
| 10086 | self.builder.buildPtrToInt(elem, value_bits_type, "") | 10628 | try self.wip.cast(.ptrtoint, elem, value_bits_type, "") |
| 10087 | else | 10629 | else |
| 10088 | self.builder.buildBitCast(elem, value_bits_type, ""); | 10630 | try self.wip.cast(.bitcast, elem, value_bits_type, ""); |
| 10089 | | 10631 | |
| 10090 | var mask_val = value_bits_type.constAllOnes(); | 10632 | var mask_val = try o.builder.intConst(value_bits_type, -1); |
| 10091 | mask_val = mask_val.constZExt(containing_int_ty); | 10633 | mask_val = try o.builder.castConst(.zext, mask_val, containing_int_ty); |
| 10092 | mask_val = mask_val.constShl(shift_amt); | 10634 | mask_val = try o.builder.binConst(.shl, mask_val, shift_amt); |
| 10093 | mask_val = mask_val.constNot(); | 10635 | mask_val = |
| 10094 | | 10636 | try o.builder.binConst(.xor, mask_val, try o.builder.intConst(containing_int_ty, -1)); |
| 10095 | const anded_containing_int = self.builder.buildAnd(containing_int, mask_val, ""); | 10637 | |
| 10096 | const extended_value = self.builder.buildZExt(value_bits, containing_int_ty, ""); | 10638 | const anded_containing_int = |
| 10097 | const shifted_value = self.builder.buildShl(extended_value, shift_amt, ""); | 10639 | try self.wip.bin(.@"and", containing_int, mask_val.toValue(), ""); |
| 10098 | const ored_value = self.builder.buildOr(shifted_value, anded_containing_int, ""); | 10640 | const extended_value = try self.wip.cast(.zext, value_bits, containing_int_ty, ""); |
| 10099 | | 10641 | const shifted_value = try self.wip.bin(.shl, extended_value, shift_amt.toValue(), ""); |
| 10100 | const store_inst = self.builder.buildStore(ored_value, ptr); | 10642 | const ored_value = try self.wip.bin(.@"or", shifted_value, anded_containing_int, ""); |
| 10101 | assert(ordering == .NotAtomic); | 10643 | |
| 10102 | store_inst.setAlignment(ptr_alignment); | 10644 | assert(ordering == .none); |
| 10103 | store_inst.setVolatile(ptr_volatile); | 10645 | _ = try self.wip.store(ptr_kind, ored_value, ptr, ptr_alignment); |
| 10104 | return; | 10646 | return; |
| 10105 | } | 10647 | } |
| 10106 | if (!isByRef(elem_ty, mod)) { | 10648 | if (!isByRef(elem_ty, mod)) { |
| 10107 | const store_inst = self.builder.buildStore(elem, ptr); | 10649 | _ = try self.wip.storeAtomic(ptr_kind, elem, ptr, self.sync_scope, ordering, ptr_alignment); |
| 10108 | store_inst.setOrdering(ordering); | | |
| 10109 | store_inst.setAlignment(ptr_alignment); | | |
| 10110 | store_inst.setVolatile(ptr_volatile); | | |
| 10111 | return; | 10650 | return; |
| 10112 | } | 10651 | } |
| 10113 | assert(ordering == .NotAtomic); | 10652 | assert(ordering == .none); |
| 10114 | const size_bytes = elem_ty.abiSize(mod); | 10653 | const size_bytes = elem_ty.abiSize(mod); |
| 10115 | _ = self.builder.buildMemCpy( | 10654 | _ = (try self.wip.unimplemented(.void, "")).finish(self.builder.buildMemCpy( |
| 10116 | ptr, | 10655 | ptr.toLlvm(&self.wip), |
| 10117 | ptr_alignment, | 10656 | @intCast(ptr_alignment.toByteUnits() orelse 0), |
| 10118 | elem, | 10657 | elem.toLlvm(&self.wip), |
| 10119 | elem_ty.abiAlignment(mod), | 10658 | elem_ty.abiAlignment(mod), |
| 10120 | self.context.intType(Type.usize.intInfo(mod).bits).constInt(size_bytes, .False), | 10659 | (try o.builder.intConst(try o.lowerType(Type.usize), size_bytes)).toLlvm(&o.builder), |
| 10121 | info.flags.is_volatile, | 10660 | info.flags.is_volatile, |
| 10122 | ); | 10661 | ), &self.wip); |
| 10123 | } | 10662 | } |
| 10124 | | 10663 | |
| 10125 | fn valgrindMarkUndef(fg: *FuncGen, ptr: *llvm.Value, len: *llvm.Value) void { | 10664 | fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void { |
| 10126 | const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545; | 10665 | const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545; |
| 10127 | const o = fg.dg.object; | 10666 | const o = fg.dg.object; |
| 10128 | const target = o.module.getTarget(); | 10667 | const usize_ty = try o.lowerType(Type.usize); |
| 10129 | const usize_llvm_ty = fg.context.intType(target.ptrBitWidth()); | 10668 | const zero = try o.builder.intValue(usize_ty, 0); |
| 10130 | const zero = usize_llvm_ty.constInt(0, .False); | 10669 | const req = try o.builder.intValue(usize_ty, VG_USERREQ__MAKE_MEM_UNDEFINED); |
| 10131 | const req = usize_llvm_ty.constInt(VG_USERREQ__MAKE_MEM_UNDEFINED, .False); | 10670 | const ptr_as_usize = try fg.wip.cast(.ptrtoint, ptr, usize_ty, ""); |
| 10132 | const ptr_as_usize = fg.builder.buildPtrToInt(ptr, usize_llvm_ty, ""); | 10671 | _ = try valgrindClientRequest(fg, zero, req, ptr_as_usize, len, zero, zero, zero); |
| 10133 | _ = valgrindClientRequest(fg, zero, req, ptr_as_usize, len, zero, zero, zero); | | |
| 10134 | } | 10672 | } |
| 10135 | | 10673 | |
| 10136 | fn valgrindClientRequest( | 10674 | fn valgrindClientRequest( |
| 10137 | fg: *FuncGen, | 10675 | fg: *FuncGen, |
| 10138 | default_value: *llvm.Value, | 10676 | default_value: Builder.Value, |
| 10139 | request: *llvm.Value, | 10677 | request: Builder.Value, |
| 10140 | a1: *llvm.Value, | 10678 | a1: Builder.Value, |
| 10141 | a2: *llvm.Value, | 10679 | a2: Builder.Value, |
| 10142 | a3: *llvm.Value, | 10680 | a3: Builder.Value, |
| 10143 | a4: *llvm.Value, | 10681 | a4: Builder.Value, |
| 10144 | a5: *llvm.Value, | 10682 | a5: Builder.Value, |
| 10145 | ) *llvm.Value { | 10683 | ) Allocator.Error!Builder.Value { |
| 10146 | const o = fg.dg.object; | 10684 | const o = fg.dg.object; |
| 10147 | const mod = o.module; | 10685 | const mod = o.module; |
| 10148 | const target = mod.getTarget(); | 10686 | const target = mod.getTarget(); |
| 10149 | if (!target_util.hasValgrindSupport(target)) return default_value; | 10687 | if (!target_util.hasValgrindSupport(target)) return default_value; |
| 10150 | | 10688 | |
| 10151 | const usize_llvm_ty = fg.context.intType(target.ptrBitWidth()); | 10689 | const llvm_usize = try o.lowerType(Type.usize); |
| 10152 | const usize_alignment = @as(c_uint, @intCast(Type.usize.abiSize(mod))); | 10690 | const usize_alignment = Builder.Alignment.fromByteUnits(Type.usize.abiAlignment(mod)); |
| 10153 | | 10691 | |
| 10154 | const array_llvm_ty = usize_llvm_ty.arrayType(6); | 10692 | const array_llvm_ty = try o.builder.arrayType(6, llvm_usize); |
| 10155 | const array_ptr = fg.valgrind_client_request_array orelse a: { | 10693 | const array_ptr = if (fg.valgrind_client_request_array == .none) a: { |
| 10156 | const array_ptr = fg.buildAlloca(array_llvm_ty, usize_alignment); | 10694 | const array_ptr = try fg.buildAlloca(array_llvm_ty, usize_alignment); |
| 10157 | fg.valgrind_client_request_array = array_ptr; | 10695 | fg.valgrind_client_request_array = array_ptr; |
| 10158 | break :a array_ptr; | 10696 | break :a array_ptr; |
| 10159 | }; | 10697 | } else fg.valgrind_client_request_array; |
| 10160 | const array_elements = [_]*llvm.Value{ request, a1, a2, a3, a4, a5 }; | 10698 | const array_elements = [_]Builder.Value{ request, a1, a2, a3, a4, a5 }; |
| 10161 | const zero = usize_llvm_ty.constInt(0, .False); | 10699 | const zero = try o.builder.intValue(llvm_usize, 0); |
| 10162 | for (array_elements, 0..) |elem, i| { | 10700 | for (array_elements, 0..) |elem, i| { |
| 10163 | const indexes = [_]*llvm.Value{ | 10701 | const elem_ptr = try fg.wip.gep(.inbounds, array_llvm_ty, array_ptr, &.{ |
| 10164 | zero, usize_llvm_ty.constInt(@as(c_uint, @intCast(i)), .False), | 10702 | zero, try o.builder.intValue(llvm_usize, i), |
| 10165 | }; | 10703 | }, ""); |
| 10166 | const elem_ptr = fg.builder.buildInBoundsGEP(array_llvm_ty, array_ptr, &indexes, indexes.len, ""); | 10704 | _ = try fg.wip.store(.normal, elem, elem_ptr, usize_alignment); |
| 10167 | const store_inst = fg.builder.buildStore(elem, elem_ptr); | | |
| 10168 | store_inst.setAlignment(usize_alignment); | | |
| 10169 | } | 10705 | } |
| 10170 | | 10706 | |
| 10171 | const arch_specific: struct { | 10707 | const arch_specific: struct { |
| ... | @@ -10199,10 +10735,9 @@ pub const FuncGen = struct { | ... | @@ -10199,10 +10735,9 @@ pub const FuncGen = struct { |
| 10199 | else => unreachable, | 10735 | else => unreachable, |
| 10200 | }; | 10736 | }; |
| 10201 | | 10737 | |
| 10202 | const array_ptr_as_usize = fg.builder.buildPtrToInt(array_ptr, usize_llvm_ty, ""); | 10738 | const fn_llvm_ty = (try o.builder.fnType(llvm_usize, &(.{llvm_usize} ** 2), .normal)).toLlvm(&o.builder); |
| 10203 | const args = [_]*llvm.Value{ array_ptr_as_usize, default_value }; | 10739 | const array_ptr_as_usize = try fg.wip.cast(.ptrtoint, array_ptr, llvm_usize, ""); |
| 10204 | const param_types = [_]*llvm.Type{ usize_llvm_ty, usize_llvm_ty }; | 10740 | const args = [_]*llvm.Value{ array_ptr_as_usize.toLlvm(&fg.wip), default_value.toLlvm(&fg.wip) }; |
| 10205 | const fn_llvm_ty = llvm.functionType(usize_llvm_ty, &param_types, args.len, .False); | | |
| 10206 | const asm_fn = llvm.getInlineAsm( | 10741 | const asm_fn = llvm.getInlineAsm( |
| 10207 | fn_llvm_ty, | 10742 | fn_llvm_ty, |
| 10208 | arch_specific.template.ptr, | 10743 | arch_specific.template.ptr, |
| ... | @@ -10215,14 +10750,9 @@ pub const FuncGen = struct { | ... | @@ -10215,14 +10750,9 @@ pub const FuncGen = struct { |
| 10215 | .False, // can throw | 10750 | .False, // can throw |
| 10216 | ); | 10751 | ); |
| 10217 | | 10752 | |
| 10218 | const call = fg.builder.buildCall( | 10753 | const call = (try fg.wip.unimplemented(llvm_usize, "")).finish( |
| 10219 | fn_llvm_ty, | 10754 | fg.builder.buildCall(fn_llvm_ty, asm_fn, &args, args.len, .C, .Auto, ""), |
| 10220 | asm_fn, | 10755 | &fg.wip, |
| 10221 | &args, | | |
| 10222 | args.len, | | |
| 10223 | .C, | | |
| 10224 | .Auto, | | |
| 10225 | "", | | |
| 10226 | ); | 10756 | ); |
| 10227 | return call; | 10757 | return call; |
| 10228 | } | 10758 | } |
| ... | @@ -10432,14 +10962,14 @@ fn initializeLLVMTarget(arch: std.Target.Cpu.Arch) void { | ... | @@ -10432,14 +10962,14 @@ fn initializeLLVMTarget(arch: std.Target.Cpu.Arch) void { |
| 10432 | } | 10962 | } |
| 10433 | } | 10963 | } |
| 10434 | | 10964 | |
| 10435 | fn toLlvmAtomicOrdering(atomic_order: std.builtin.AtomicOrder) llvm.AtomicOrdering { | 10965 | fn toLlvmAtomicOrdering(atomic_order: std.builtin.AtomicOrder) Builder.AtomicOrdering { |
| 10436 | return switch (atomic_order) { | 10966 | return switch (atomic_order) { |
| 10437 | .Unordered => .Unordered, | 10967 | .Unordered => .unordered, |
| 10438 | .Monotonic => .Monotonic, | 10968 | .Monotonic => .monotonic, |
| 10439 | .Acquire => .Acquire, | 10969 | .Acquire => .acquire, |
| 10440 | .Release => .Release, | 10970 | .Release => .release, |
| 10441 | .AcqRel => .AcquireRelease, | 10971 | .AcqRel => .acq_rel, |
| 10442 | .SeqCst => .SequentiallyConsistent, | 10972 | .SeqCst => .seq_cst, |
| 10443 | }; | 10973 | }; |
| 10444 | } | 10974 | } |
| 10445 | | 10975 | |
| ... | @@ -10494,45 +11024,67 @@ fn toLlvmCallConv(cc: std.builtin.CallingConvention, target: std.Target) llvm.Ca | ... | @@ -10494,45 +11024,67 @@ fn toLlvmCallConv(cc: std.builtin.CallingConvention, target: std.Target) llvm.Ca |
| 10494 | } | 11024 | } |
| 10495 | | 11025 | |
| 10496 | /// Convert a zig-address space to an llvm address space. | 11026 | /// Convert a zig-address space to an llvm address space. |
| 10497 | fn toLlvmAddressSpace(address_space: std.builtin.AddressSpace, target: std.Target) c_uint { | 11027 | fn toLlvmAddressSpace(address_space: std.builtin.AddressSpace, target: std.Target) Builder.AddrSpace { |
| | 11028 | for (llvmAddrSpaceInfo(target)) |info| if (info.zig == address_space) return info.llvm; |
| | 11029 | unreachable; |
| | 11030 | } |
| | 11031 | |
| | 11032 | const AddrSpaceInfo = struct { |
| | 11033 | zig: ?std.builtin.AddressSpace, |
| | 11034 | llvm: Builder.AddrSpace, |
| | 11035 | non_integral: bool = false, |
| | 11036 | size: ?u16 = null, |
| | 11037 | abi: ?u16 = null, |
| | 11038 | pref: ?u16 = null, |
| | 11039 | idx: ?u16 = null, |
| | 11040 | force_in_data_layout: bool = false, |
| | 11041 | }; |
| | 11042 | fn llvmAddrSpaceInfo(target: std.Target) []const AddrSpaceInfo { |
| 10498 | return switch (target.cpu.arch) { | 11043 | return switch (target.cpu.arch) { |
| 10499 | .x86, .x86_64 => switch (address_space) { | 11044 | .x86, .x86_64 => &.{ |
| 10500 | .generic => llvm.address_space.default, | 11045 | .{ .zig = .generic, .llvm = .default }, |
| 10501 | .gs => llvm.address_space.x86.gs, | 11046 | .{ .zig = .gs, .llvm = Builder.AddrSpace.x86.gs }, |
| 10502 | .fs => llvm.address_space.x86.fs, | 11047 | .{ .zig = .fs, .llvm = Builder.AddrSpace.x86.fs }, |
| 10503 | .ss => llvm.address_space.x86.ss, | 11048 | .{ .zig = .ss, .llvm = Builder.AddrSpace.x86.ss }, |
| 10504 | else => unreachable, | 11049 | .{ .zig = null, .llvm = Builder.AddrSpace.x86.ptr32_sptr, .size = 32, .abi = 32, .force_in_data_layout = true }, |
| | 11050 | .{ .zig = null, .llvm = Builder.AddrSpace.x86.ptr32_uptr, .size = 32, .abi = 32, .force_in_data_layout = true }, |
| | 11051 | .{ .zig = null, .llvm = Builder.AddrSpace.x86.ptr64, .size = 64, .abi = 64, .force_in_data_layout = true }, |
| 10505 | }, | 11052 | }, |
| 10506 | .nvptx, .nvptx64 => switch (address_space) { | 11053 | .nvptx, .nvptx64 => &.{ |
| 10507 | .generic => llvm.address_space.default, | 11054 | .{ .zig = .generic, .llvm = .default }, |
| 10508 | .global => llvm.address_space.nvptx.global, | 11055 | .{ .zig = .global, .llvm = Builder.AddrSpace.nvptx.global }, |
| 10509 | .constant => llvm.address_space.nvptx.constant, | 11056 | .{ .zig = .constant, .llvm = Builder.AddrSpace.nvptx.constant }, |
| 10510 | .param => llvm.address_space.nvptx.param, | 11057 | .{ .zig = .param, .llvm = Builder.AddrSpace.nvptx.param }, |
| 10511 | .shared => llvm.address_space.nvptx.shared, | 11058 | .{ .zig = .shared, .llvm = Builder.AddrSpace.nvptx.shared }, |
| 10512 | .local => llvm.address_space.nvptx.local, | 11059 | .{ .zig = .local, .llvm = Builder.AddrSpace.nvptx.local }, |
| 10513 | else => unreachable, | | |
| 10514 | }, | 11060 | }, |
| 10515 | .amdgcn => switch (address_space) { | 11061 | .amdgcn => &.{ |
| 10516 | .generic => llvm.address_space.amdgpu.flat, | 11062 | .{ .zig = .generic, .llvm = Builder.AddrSpace.amdgpu.flat, .force_in_data_layout = true }, |
| 10517 | .global => llvm.address_space.amdgpu.global, | 11063 | .{ .zig = .global, .llvm = Builder.AddrSpace.amdgpu.global, .force_in_data_layout = true }, |
| 10518 | .constant => llvm.address_space.amdgpu.constant, | 11064 | .{ .zig = null, .llvm = Builder.AddrSpace.amdgpu.region, .size = 32, .abi = 32 }, |
| 10519 | .shared => llvm.address_space.amdgpu.local, | 11065 | .{ .zig = .shared, .llvm = Builder.AddrSpace.amdgpu.local, .size = 32, .abi = 32 }, |
| 10520 | .local => llvm.address_space.amdgpu.private, | 11066 | .{ .zig = .constant, .llvm = Builder.AddrSpace.amdgpu.constant, .force_in_data_layout = true }, |
| 10521 | else => unreachable, | 11067 | .{ .zig = .local, .llvm = Builder.AddrSpace.amdgpu.private, .size = 32, .abi = 32 }, |
| | 11068 | .{ .zig = null, .llvm = Builder.AddrSpace.amdgpu.constant_32bit, .size = 32, .abi = 32 }, |
| | 11069 | .{ .zig = null, .llvm = Builder.AddrSpace.amdgpu.buffer_fat_pointer, .non_integral = true }, |
| 10522 | }, | 11070 | }, |
| 10523 | .avr => switch (address_space) { | 11071 | .avr => &.{ |
| 10524 | .generic => llvm.address_space.default, | 11072 | .{ .zig = .generic, .llvm = .default, .abi = 8 }, |
| 10525 | .flash => llvm.address_space.avr.flash, | 11073 | .{ .zig = .flash, .llvm = Builder.AddrSpace.avr.flash, .abi = 8 }, |
| 10526 | .flash1 => llvm.address_space.avr.flash1, | 11074 | .{ .zig = .flash1, .llvm = Builder.AddrSpace.avr.flash1, .abi = 8 }, |
| 10527 | .flash2 => llvm.address_space.avr.flash2, | 11075 | .{ .zig = .flash2, .llvm = Builder.AddrSpace.avr.flash2, .abi = 8 }, |
| 10528 | .flash3 => llvm.address_space.avr.flash3, | 11076 | .{ .zig = .flash3, .llvm = Builder.AddrSpace.avr.flash3, .abi = 8 }, |
| 10529 | .flash4 => llvm.address_space.avr.flash4, | 11077 | .{ .zig = .flash4, .llvm = Builder.AddrSpace.avr.flash4, .abi = 8 }, |
| 10530 | .flash5 => llvm.address_space.avr.flash5, | 11078 | .{ .zig = .flash5, .llvm = Builder.AddrSpace.avr.flash5, .abi = 8 }, |
| 10531 | else => unreachable, | | |
| 10532 | }, | 11079 | }, |
| 10533 | else => switch (address_space) { | 11080 | .wasm32, .wasm64 => &.{ |
| 10534 | .generic => llvm.address_space.default, | 11081 | .{ .zig = .generic, .llvm = .default, .force_in_data_layout = true }, |
| 10535 | else => unreachable, | 11082 | .{ .zig = null, .llvm = Builder.AddrSpace.wasm.variable, .non_integral = true }, |
| | 11083 | .{ .zig = null, .llvm = Builder.AddrSpace.wasm.externref, .non_integral = true, .size = 8, .abi = 8 }, |
| | 11084 | .{ .zig = null, .llvm = Builder.AddrSpace.wasm.funcref, .non_integral = true, .size = 8, .abi = 8 }, |
| | 11085 | }, |
| | 11086 | else => &.{ |
| | 11087 | .{ .zig = .generic, .llvm = .default }, |
| 10536 | }, | 11088 | }, |
| 10537 | }; | 11089 | }; |
| 10538 | } | 11090 | } |
| ... | @@ -10541,30 +11093,30 @@ fn toLlvmAddressSpace(address_space: std.builtin.AddressSpace, target: std.Targe | ... | @@ -10541,30 +11093,30 @@ fn toLlvmAddressSpace(address_space: std.builtin.AddressSpace, target: std.Targe |
| 10541 | /// different address, space and then cast back to the generic address space. | 11093 | /// different address, space and then cast back to the generic address space. |
| 10542 | /// For example, on GPUs local variable declarations must be generated into the local address space. | 11094 | /// For example, on GPUs local variable declarations must be generated into the local address space. |
| 10543 | /// This function returns the address space local values should be generated into. | 11095 | /// This function returns the address space local values should be generated into. |
| 10544 | fn llvmAllocaAddressSpace(target: std.Target) c_uint { | 11096 | fn llvmAllocaAddressSpace(target: std.Target) Builder.AddrSpace { |
| 10545 | return switch (target.cpu.arch) { | 11097 | return switch (target.cpu.arch) { |
| 10546 | // On amdgcn, locals should be generated into the private address space. | 11098 | // On amdgcn, locals should be generated into the private address space. |
| 10547 | // To make Zig not impossible to use, these are then converted to addresses in the | 11099 | // To make Zig not impossible to use, these are then converted to addresses in the |
| 10548 | // generic address space and treates as regular pointers. This is the way that HIP also does it. | 11100 | // generic address space and treates as regular pointers. This is the way that HIP also does it. |
| 10549 | .amdgcn => llvm.address_space.amdgpu.private, | 11101 | .amdgcn => Builder.AddrSpace.amdgpu.private, |
| 10550 | else => llvm.address_space.default, | 11102 | else => .default, |
| 10551 | }; | 11103 | }; |
| 10552 | } | 11104 | } |
| 10553 | | 11105 | |
| 10554 | /// On some targets, global values that are in the generic address space must be generated into a | 11106 | /// On some targets, global values that are in the generic address space must be generated into a |
| 10555 | /// different address space, and then cast back to the generic address space. | 11107 | /// different address space, and then cast back to the generic address space. |
| 10556 | fn llvmDefaultGlobalAddressSpace(target: std.Target) c_uint { | 11108 | fn llvmDefaultGlobalAddressSpace(target: std.Target) Builder.AddrSpace { |
| 10557 | return switch (target.cpu.arch) { | 11109 | return switch (target.cpu.arch) { |
| 10558 | // On amdgcn, globals must be explicitly allocated and uploaded so that the program can access | 11110 | // On amdgcn, globals must be explicitly allocated and uploaded so that the program can access |
| 10559 | // them. | 11111 | // them. |
| 10560 | .amdgcn => llvm.address_space.amdgpu.global, | 11112 | .amdgcn => Builder.AddrSpace.amdgpu.global, |
| 10561 | else => llvm.address_space.default, | 11113 | else => .default, |
| 10562 | }; | 11114 | }; |
| 10563 | } | 11115 | } |
| 10564 | | 11116 | |
| 10565 | /// Return the actual address space that a value should be stored in if its a global address space. | 11117 | /// Return the actual address space that a value should be stored in if its a global address space. |
| 10566 | /// When a value is placed in the resulting address space, it needs to be cast back into wanted_address_space. | 11118 | /// When a value is placed in the resulting address space, it needs to be cast back into wanted_address_space. |
| 10567 | fn toLlvmGlobalAddressSpace(wanted_address_space: std.builtin.AddressSpace, target: std.Target) c_uint { | 11119 | fn toLlvmGlobalAddressSpace(wanted_address_space: std.builtin.AddressSpace, target: std.Target) Builder.AddrSpace { |
| 10568 | return switch (wanted_address_space) { | 11120 | return switch (wanted_address_space) { |
| 10569 | .generic => llvmDefaultGlobalAddressSpace(target), | 11121 | .generic => llvmDefaultGlobalAddressSpace(target), |
| 10570 | else => |as| toLlvmAddressSpace(as, target), | 11122 | else => |as| toLlvmAddressSpace(as, target), |
| ... | @@ -10694,28 +11246,20 @@ fn firstParamSRetSystemV(ty: Type, mod: *Module) bool { | ... | @@ -10694,28 +11246,20 @@ fn firstParamSRetSystemV(ty: Type, mod: *Module) bool { |
| 10694 | /// In order to support the C calling convention, some return types need to be lowered | 11246 | /// In order to support the C calling convention, some return types need to be lowered |
| 10695 | /// completely differently in the function prototype to honor the C ABI, and then | 11247 | /// completely differently in the function prototype to honor the C ABI, and then |
| 10696 | /// be effectively bitcasted to the actual return type. | 11248 | /// be effectively bitcasted to the actual return type. |
| 10697 | fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | 11249 | fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 10698 | const mod = o.module; | 11250 | const mod = o.module; |
| 10699 | const return_type = fn_info.return_type.toType(); | 11251 | const return_type = fn_info.return_type.toType(); |
| 10700 | if (!return_type.hasRuntimeBitsIgnoreComptime(mod)) { | 11252 | if (!return_type.hasRuntimeBitsIgnoreComptime(mod)) { |
| 10701 | // If the return type is an error set or an error union, then we make this | 11253 | // If the return type is an error set or an error union, then we make this |
| 10702 | // anyerror return type instead, so that it can be coerced into a function | 11254 | // anyerror return type instead, so that it can be coerced into a function |
| 10703 | // pointer type which has anyerror as the return type. | 11255 | // pointer type which has anyerror as the return type. |
| 10704 | if (return_type.isError(mod)) { | 11256 | return if (return_type.isError(mod)) Builder.Type.err_int else .void; |
| 10705 | return o.lowerType(Type.anyerror); | | |
| 10706 | } else { | | |
| 10707 | return o.context.voidType(); | | |
| 10708 | } | | |
| 10709 | } | 11257 | } |
| 10710 | const target = mod.getTarget(); | 11258 | const target = mod.getTarget(); |
| 10711 | switch (fn_info.cc) { | 11259 | switch (fn_info.cc) { |
| 10712 | .Unspecified, .Inline => { | 11260 | .Unspecified, |
| 10713 | if (isByRef(return_type, mod)) { | 11261 | .Inline, |
| 10714 | return o.context.voidType(); | 11262 | => return if (isByRef(return_type, mod)) .void else o.lowerType(return_type), |
| 10715 | } else { | | |
| 10716 | return o.lowerType(return_type); | | |
| 10717 | } | | |
| 10718 | }, | | |
| 10719 | .C => { | 11263 | .C => { |
| 10720 | switch (target.cpu.arch) { | 11264 | switch (target.cpu.arch) { |
| 10721 | .mips, .mipsel => return o.lowerType(return_type), | 11265 | .mips, .mipsel => return o.lowerType(return_type), |
| ... | @@ -10729,50 +11273,37 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | ... | @@ -10729,50 +11273,37 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { |
| 10729 | } | 11273 | } |
| 10730 | const classes = wasm_c_abi.classifyType(return_type, mod); | 11274 | const classes = wasm_c_abi.classifyType(return_type, mod); |
| 10731 | if (classes[0] == .indirect or classes[0] == .none) { | 11275 | if (classes[0] == .indirect or classes[0] == .none) { |
| 10732 | return o.context.voidType(); | 11276 | return .void; |
| 10733 | } | 11277 | } |
| 10734 | | 11278 | |
| 10735 | assert(classes[0] == .direct and classes[1] == .none); | 11279 | assert(classes[0] == .direct and classes[1] == .none); |
| 10736 | const scalar_type = wasm_c_abi.scalarType(return_type, mod); | 11280 | const scalar_type = wasm_c_abi.scalarType(return_type, mod); |
| 10737 | const abi_size = scalar_type.abiSize(mod); | 11281 | return o.builder.intType(@intCast(scalar_type.abiSize(mod) * 8)); |
| 10738 | return o.context.intType(@as(c_uint, @intCast(abi_size * 8))); | | |
| 10739 | }, | 11282 | }, |
| 10740 | .aarch64, .aarch64_be => { | 11283 | .aarch64, .aarch64_be => { |
| 10741 | switch (aarch64_c_abi.classifyType(return_type, mod)) { | 11284 | switch (aarch64_c_abi.classifyType(return_type, mod)) { |
| 10742 | .memory => return o.context.voidType(), | 11285 | .memory => return .void, |
| 10743 | .float_array => return o.lowerType(return_type), | 11286 | .float_array => return o.lowerType(return_type), |
| 10744 | .byval => return o.lowerType(return_type), | 11287 | .byval => return o.lowerType(return_type), |
| 10745 | .integer => { | 11288 | .integer => return o.builder.intType(@intCast(return_type.bitSize(mod))), |
| 10746 | const bit_size = return_type.bitSize(mod); | 11289 | .double_integer => return o.builder.arrayType(2, .i64), |
| 10747 | return o.context.intType(@as(c_uint, @intCast(bit_size))); | | |
| 10748 | }, | | |
| 10749 | .double_integer => return o.context.intType(64).arrayType(2), | | |
| 10750 | } | 11290 | } |
| 10751 | }, | 11291 | }, |
| 10752 | .arm, .armeb => { | 11292 | .arm, .armeb => { |
| 10753 | switch (arm_c_abi.classifyType(return_type, mod, .ret)) { | 11293 | switch (arm_c_abi.classifyType(return_type, mod, .ret)) { |
| 10754 | .memory, .i64_array => return o.context.voidType(), | 11294 | .memory, .i64_array => return .void, |
| 10755 | .i32_array => |len| if (len == 1) { | 11295 | .i32_array => |len| return if (len == 1) .i32 else .void, |
| 10756 | return o.context.intType(32); | | |
| 10757 | } else { | | |
| 10758 | return o.context.voidType(); | | |
| 10759 | }, | | |
| 10760 | .byval => return o.lowerType(return_type), | 11296 | .byval => return o.lowerType(return_type), |
| 10761 | } | 11297 | } |
| 10762 | }, | 11298 | }, |
| 10763 | .riscv32, .riscv64 => { | 11299 | .riscv32, .riscv64 => { |
| 10764 | switch (riscv_c_abi.classifyType(return_type, mod)) { | 11300 | switch (riscv_c_abi.classifyType(return_type, mod)) { |
| 10765 | .memory => return o.context.voidType(), | 11301 | .memory => return .void, |
| 10766 | .integer => { | 11302 | .integer => { |
| 10767 | const bit_size = return_type.bitSize(mod); | 11303 | return o.builder.intType(@intCast(return_type.bitSize(mod))); |
| 10768 | return o.context.intType(@as(c_uint, @intCast(bit_size))); | | |
| 10769 | }, | 11304 | }, |
| 10770 | .double_integer => { | 11305 | .double_integer => { |
| 10771 | var llvm_types_buffer: [2]*llvm.Type = .{ | 11306 | return o.builder.structType(.normal, &.{ .i64, .i64 }); |
| 10772 | o.context.intType(64), | | |
| 10773 | o.context.intType(64), | | |
| 10774 | }; | | |
| 10775 | return o.context.structType(&llvm_types_buffer, 2, .False); | | |
| 10776 | }, | 11307 | }, |
| 10777 | .byval => return o.lowerType(return_type), | 11308 | .byval => return o.lowerType(return_type), |
| 10778 | } | 11309 | } |
| ... | @@ -10783,18 +11314,12 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | ... | @@ -10783,18 +11314,12 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { |
| 10783 | }, | 11314 | }, |
| 10784 | .Win64 => return lowerWin64FnRetTy(o, fn_info), | 11315 | .Win64 => return lowerWin64FnRetTy(o, fn_info), |
| 10785 | .SysV => return lowerSystemVFnRetTy(o, fn_info), | 11316 | .SysV => return lowerSystemVFnRetTy(o, fn_info), |
| 10786 | .Stdcall => { | 11317 | .Stdcall => return if (isScalar(mod, return_type)) o.lowerType(return_type) else .void, |
| 10787 | if (isScalar(mod, return_type)) { | | |
| 10788 | return o.lowerType(return_type); | | |
| 10789 | } else { | | |
| 10790 | return o.context.voidType(); | | |
| 10791 | } | | |
| 10792 | }, | | |
| 10793 | else => return o.lowerType(return_type), | 11318 | else => return o.lowerType(return_type), |
| 10794 | } | 11319 | } |
| 10795 | } | 11320 | } |
| 10796 | | 11321 | |
| 10797 | fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | 11322 | fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 10798 | const mod = o.module; | 11323 | const mod = o.module; |
| 10799 | const return_type = fn_info.return_type.toType(); | 11324 | const return_type = fn_info.return_type.toType(); |
| 10800 | switch (x86_64_abi.classifyWindows(return_type, mod)) { | 11325 | switch (x86_64_abi.classifyWindows(return_type, mod)) { |
| ... | @@ -10802,53 +11327,48 @@ fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | ... | @@ -10802,53 +11327,48 @@ fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { |
| 10802 | if (isScalar(mod, return_type)) { | 11327 | if (isScalar(mod, return_type)) { |
| 10803 | return o.lowerType(return_type); | 11328 | return o.lowerType(return_type); |
| 10804 | } else { | 11329 | } else { |
| 10805 | const abi_size = return_type.abiSize(mod); | 11330 | return o.builder.intType(@intCast(return_type.abiSize(mod) * 8)); |
| 10806 | return o.context.intType(@as(c_uint, @intCast(abi_size * 8))); | | |
| 10807 | } | 11331 | } |
| 10808 | }, | 11332 | }, |
| 10809 | .win_i128 => return o.context.intType(64).vectorType(2), | 11333 | .win_i128 => return o.builder.vectorType(.normal, 2, .i64), |
| 10810 | .memory => return o.context.voidType(), | 11334 | .memory => return .void, |
| 10811 | .sse => return o.lowerType(return_type), | 11335 | .sse => return o.lowerType(return_type), |
| 10812 | else => unreachable, | 11336 | else => unreachable, |
| 10813 | } | 11337 | } |
| 10814 | } | 11338 | } |
| 10815 | | 11339 | |
| 10816 | fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type { | 11340 | fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 10817 | const mod = o.module; | 11341 | const mod = o.module; |
| 10818 | const return_type = fn_info.return_type.toType(); | 11342 | const return_type = fn_info.return_type.toType(); |
| 10819 | if (isScalar(mod, return_type)) { | 11343 | if (isScalar(mod, return_type)) { |
| 10820 | return o.lowerType(return_type); | 11344 | return o.lowerType(return_type); |
| 10821 | } | 11345 | } |
| 10822 | const classes = x86_64_abi.classifySystemV(return_type, mod, .ret); | 11346 | const classes = x86_64_abi.classifySystemV(return_type, mod, .ret); |
| 10823 | if (classes[0] == .memory) { | 11347 | if (classes[0] == .memory) return .void; |
| 10824 | return o.context.voidType(); | 11348 | var types_index: u32 = 0; |
| 10825 | } | 11349 | var types_buffer: [8]Builder.Type = undefined; |
| 10826 | var llvm_types_buffer: [8]*llvm.Type = undefined; | | |
| 10827 | var llvm_types_index: u32 = 0; | | |
| 10828 | for (classes) |class| { | 11350 | for (classes) |class| { |
| 10829 | switch (class) { | 11351 | switch (class) { |
| 10830 | .integer => { | 11352 | .integer => { |
| 10831 | llvm_types_buffer[llvm_types_index] = o.context.intType(64); | 11353 | types_buffer[types_index] = .i64; |
| 10832 | llvm_types_index += 1; | 11354 | types_index += 1; |
| 10833 | }, | 11355 | }, |
| 10834 | .sse, .sseup => { | 11356 | .sse, .sseup => { |
| 10835 | llvm_types_buffer[llvm_types_index] = o.context.doubleType(); | 11357 | types_buffer[types_index] = .double; |
| 10836 | llvm_types_index += 1; | 11358 | types_index += 1; |
| 10837 | }, | 11359 | }, |
| 10838 | .float => { | 11360 | .float => { |
| 10839 | llvm_types_buffer[llvm_types_index] = o.context.floatType(); | 11361 | types_buffer[types_index] = .float; |
| 10840 | llvm_types_index += 1; | 11362 | types_index += 1; |
| 10841 | }, | 11363 | }, |
| 10842 | .float_combine => { | 11364 | .float_combine => { |
| 10843 | llvm_types_buffer[llvm_types_index] = o.context.floatType().vectorType(2); | 11365 | types_buffer[types_index] = try o.builder.vectorType(.normal, 2, .float); |
| 10844 | llvm_types_index += 1; | 11366 | types_index += 1; |
| 10845 | }, | 11367 | }, |
| 10846 | .x87 => { | 11368 | .x87 => { |
| 10847 | if (llvm_types_index != 0 or classes[2] != .none) { | 11369 | if (types_index != 0 or classes[2] != .none) return .void; |
| 10848 | return o.context.voidType(); | 11370 | types_buffer[types_index] = .x86_fp80; |
| 10849 | } | 11371 | types_index += 1; |
| 10850 | llvm_types_buffer[llvm_types_index] = o.context.x86FP80Type(); | | |
| 10851 | llvm_types_index += 1; | | |
| 10852 | }, | 11372 | }, |
| 10853 | .x87up => continue, | 11373 | .x87up => continue, |
| 10854 | .complex_x87 => { | 11374 | .complex_x87 => { |
| ... | @@ -10860,10 +11380,9 @@ fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type | ... | @@ -10860,10 +11380,9 @@ fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) !*llvm.Type |
| 10860 | } | 11380 | } |
| 10861 | } | 11381 | } |
| 10862 | if (classes[0] == .integer and classes[1] == .none) { | 11382 | if (classes[0] == .integer and classes[1] == .none) { |
| 10863 | const abi_size = return_type.abiSize(mod); | 11383 | return o.builder.intType(@intCast(return_type.abiSize(mod) * 8)); |
| 10864 | return o.context.intType(@as(c_uint, @intCast(abi_size * 8))); | | |
| 10865 | } | 11384 | } |
| 10866 | return o.context.structType(&llvm_types_buffer, llvm_types_index, .False); | 11385 | return o.builder.structType(.normal, types_buffer[0..types_index]); |
| 10867 | } | 11386 | } |
| 10868 | | 11387 | |
| 10869 | const ParamTypeIterator = struct { | 11388 | const ParamTypeIterator = struct { |
| ... | @@ -10871,8 +11390,8 @@ const ParamTypeIterator = struct { | ... | @@ -10871,8 +11390,8 @@ const ParamTypeIterator = struct { |
| 10871 | fn_info: InternPool.Key.FuncType, | 11390 | fn_info: InternPool.Key.FuncType, |
| 10872 | zig_index: u32, | 11391 | zig_index: u32, |
| 10873 | llvm_index: u32, | 11392 | llvm_index: u32, |
| 10874 | llvm_types_len: u32, | 11393 | types_len: u32, |
| 10875 | llvm_types_buffer: [8]*llvm.Type, | 11394 | types_buffer: [8]Builder.Type, |
| 10876 | byval_attr: bool, | 11395 | byval_attr: bool, |
| 10877 | | 11396 | |
| 10878 | const Lowering = union(enum) { | 11397 | const Lowering = union(enum) { |
| ... | @@ -10889,7 +11408,7 @@ const ParamTypeIterator = struct { | ... | @@ -10889,7 +11408,7 @@ const ParamTypeIterator = struct { |
| 10889 | i64_array: u8, | 11408 | i64_array: u8, |
| 10890 | }; | 11409 | }; |
| 10891 | | 11410 | |
| 10892 | pub fn next(it: *ParamTypeIterator) ?Lowering { | 11411 | pub fn next(it: *ParamTypeIterator) Allocator.Error!?Lowering { |
| 10893 | if (it.zig_index >= it.fn_info.param_types.len) return null; | 11412 | if (it.zig_index >= it.fn_info.param_types.len) return null; |
| 10894 | const mod = it.object.module; | 11413 | const mod = it.object.module; |
| 10895 | const ip = &mod.intern_pool; | 11414 | const ip = &mod.intern_pool; |
| ... | @@ -10899,7 +11418,7 @@ const ParamTypeIterator = struct { | ... | @@ -10899,7 +11418,7 @@ const ParamTypeIterator = struct { |
| 10899 | } | 11418 | } |
| 10900 | | 11419 | |
| 10901 | /// `airCall` uses this instead of `next` so that it can take into account variadic functions. | 11420 | /// `airCall` uses this instead of `next` so that it can take into account variadic functions. |
| 10902 | pub fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) ?Lowering { | 11421 | pub fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) Allocator.Error!?Lowering { |
| 10903 | const mod = it.object.module; | 11422 | const mod = it.object.module; |
| 10904 | const ip = &mod.intern_pool; | 11423 | const ip = &mod.intern_pool; |
| 10905 | if (it.zig_index >= it.fn_info.param_types.len) { | 11424 | if (it.zig_index >= it.fn_info.param_types.len) { |
| ... | @@ -10913,7 +11432,7 @@ const ParamTypeIterator = struct { | ... | @@ -10913,7 +11432,7 @@ const ParamTypeIterator = struct { |
| 10913 | } | 11432 | } |
| 10914 | } | 11433 | } |
| 10915 | | 11434 | |
| 10916 | fn nextInner(it: *ParamTypeIterator, ty: Type) ?Lowering { | 11435 | fn nextInner(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering { |
| 10917 | const mod = it.object.module; | 11436 | const mod = it.object.module; |
| 10918 | const target = mod.getTarget(); | 11437 | const target = mod.getTarget(); |
| 10919 | | 11438 | |
| ... | @@ -10968,8 +11487,8 @@ const ParamTypeIterator = struct { | ... | @@ -10968,8 +11487,8 @@ const ParamTypeIterator = struct { |
| 10968 | .float_array => |len| return Lowering{ .float_array = len }, | 11487 | .float_array => |len| return Lowering{ .float_array = len }, |
| 10969 | .byval => return .byval, | 11488 | .byval => return .byval, |
| 10970 | .integer => { | 11489 | .integer => { |
| 10971 | it.llvm_types_len = 1; | 11490 | it.types_len = 1; |
| 10972 | it.llvm_types_buffer[0] = it.object.context.intType(64); | 11491 | it.types_buffer[0] = .i64; |
| 10973 | return .multiple_llvm_types; | 11492 | return .multiple_llvm_types; |
| 10974 | }, | 11493 | }, |
| 10975 | .double_integer => return Lowering{ .i64_array = 2 }, | 11494 | .double_integer => return Lowering{ .i64_array = 2 }, |
| ... | @@ -11063,7 +11582,7 @@ const ParamTypeIterator = struct { | ... | @@ -11063,7 +11582,7 @@ const ParamTypeIterator = struct { |
| 11063 | } | 11582 | } |
| 11064 | } | 11583 | } |
| 11065 | | 11584 | |
| 11066 | fn nextSystemV(it: *ParamTypeIterator, ty: Type) ?Lowering { | 11585 | fn nextSystemV(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering { |
| 11067 | const mod = it.object.module; | 11586 | const mod = it.object.module; |
| 11068 | const classes = x86_64_abi.classifySystemV(ty, mod, .arg); | 11587 | const classes = x86_64_abi.classifySystemV(ty, mod, .arg); |
| 11069 | if (classes[0] == .memory) { | 11588 | if (classes[0] == .memory) { |
| ... | @@ -11077,25 +11596,25 @@ const ParamTypeIterator = struct { | ... | @@ -11077,25 +11596,25 @@ const ParamTypeIterator = struct { |
| 11077 | it.llvm_index += 1; | 11596 | it.llvm_index += 1; |
| 11078 | return .byval; | 11597 | return .byval; |
| 11079 | } | 11598 | } |
| 11080 | var llvm_types_buffer: [8]*llvm.Type = undefined; | 11599 | var types_index: u32 = 0; |
| 11081 | var llvm_types_index: u32 = 0; | 11600 | var types_buffer: [8]Builder.Type = undefined; |
| 11082 | for (classes) |class| { | 11601 | for (classes) |class| { |
| 11083 | switch (class) { | 11602 | switch (class) { |
| 11084 | .integer => { | 11603 | .integer => { |
| 11085 | llvm_types_buffer[llvm_types_index] = it.object.context.intType(64); | 11604 | types_buffer[types_index] = .i64; |
| 11086 | llvm_types_index += 1; | 11605 | types_index += 1; |
| 11087 | }, | 11606 | }, |
| 11088 | .sse, .sseup => { | 11607 | .sse, .sseup => { |
| 11089 | llvm_types_buffer[llvm_types_index] = it.object.context.doubleType(); | 11608 | types_buffer[types_index] = .double; |
| 11090 | llvm_types_index += 1; | 11609 | types_index += 1; |
| 11091 | }, | 11610 | }, |
| 11092 | .float => { | 11611 | .float => { |
| 11093 | llvm_types_buffer[llvm_types_index] = it.object.context.floatType(); | 11612 | types_buffer[types_index] = .float; |
| 11094 | llvm_types_index += 1; | 11613 | types_index += 1; |
| 11095 | }, | 11614 | }, |
| 11096 | .float_combine => { | 11615 | .float_combine => { |
| 11097 | llvm_types_buffer[llvm_types_index] = it.object.context.floatType().vectorType(2); | 11616 | types_buffer[types_index] = try it.object.builder.vectorType(.normal, 2, .float); |
| 11098 | llvm_types_index += 1; | 11617 | types_index += 1; |
| 11099 | }, | 11618 | }, |
| 11100 | .x87 => { | 11619 | .x87 => { |
| 11101 | it.zig_index += 1; | 11620 | it.zig_index += 1; |
| ... | @@ -11117,9 +11636,9 @@ const ParamTypeIterator = struct { | ... | @@ -11117,9 +11636,9 @@ const ParamTypeIterator = struct { |
| 11117 | it.llvm_index += 1; | 11636 | it.llvm_index += 1; |
| 11118 | return .abi_sized_int; | 11637 | return .abi_sized_int; |
| 11119 | } | 11638 | } |
| 11120 | it.llvm_types_buffer = llvm_types_buffer; | 11639 | it.types_len = types_index; |
| 11121 | it.llvm_types_len = llvm_types_index; | 11640 | it.types_buffer = types_buffer; |
| 11122 | it.llvm_index += llvm_types_index; | 11641 | it.llvm_index += types_index; |
| 11123 | it.zig_index += 1; | 11642 | it.zig_index += 1; |
| 11124 | return .multiple_llvm_types; | 11643 | return .multiple_llvm_types; |
| 11125 | } | 11644 | } |
| ... | @@ -11131,8 +11650,8 @@ fn iterateParamTypes(object: *Object, fn_info: InternPool.Key.FuncType) ParamTyp | ... | @@ -11131,8 +11650,8 @@ fn iterateParamTypes(object: *Object, fn_info: InternPool.Key.FuncType) ParamTyp |
| 11131 | .fn_info = fn_info, | 11650 | .fn_info = fn_info, |
| 11132 | .zig_index = 0, | 11651 | .zig_index = 0, |
| 11133 | .llvm_index = 0, | 11652 | .llvm_index = 0, |
| 11134 | .llvm_types_buffer = undefined, | 11653 | .types_len = 0, |
| 11135 | .llvm_types_len = 0, | 11654 | .types_buffer = undefined, |
| 11136 | .byval_attr = false, | 11655 | .byval_attr = false, |
| 11137 | }; | 11656 | }; |
| 11138 | } | 11657 | } |
| ... | @@ -11355,23 +11874,23 @@ const AnnotatedDITypePtr = enum(usize) { | ... | @@ -11355,23 +11874,23 @@ const AnnotatedDITypePtr = enum(usize) { |
| 11355 | fn initFwd(di_type: *llvm.DIType) AnnotatedDITypePtr { | 11874 | fn initFwd(di_type: *llvm.DIType) AnnotatedDITypePtr { |
| 11356 | const addr = @intFromPtr(di_type); | 11875 | const addr = @intFromPtr(di_type); |
| 11357 | assert(@as(u1, @truncate(addr)) == 0); | 11876 | assert(@as(u1, @truncate(addr)) == 0); |
| 11358 | return @as(AnnotatedDITypePtr, @enumFromInt(addr | 1)); | 11877 | return @enumFromInt(addr | 1); |
| 11359 | } | 11878 | } |
| 11360 | | 11879 | |
| 11361 | fn initFull(di_type: *llvm.DIType) AnnotatedDITypePtr { | 11880 | fn initFull(di_type: *llvm.DIType) AnnotatedDITypePtr { |
| 11362 | const addr = @intFromPtr(di_type); | 11881 | const addr = @intFromPtr(di_type); |
| 11363 | return @as(AnnotatedDITypePtr, @enumFromInt(addr)); | 11882 | return @enumFromInt(addr); |
| 11364 | } | 11883 | } |
| 11365 | | 11884 | |
| 11366 | fn init(di_type: *llvm.DIType, resolve: Object.DebugResolveStatus) AnnotatedDITypePtr { | 11885 | fn init(di_type: *llvm.DIType, resolve: Object.DebugResolveStatus) AnnotatedDITypePtr { |
| 11367 | const addr = @intFromPtr(di_type); | 11886 | const addr = @intFromPtr(di_type); |
| 11368 | const bit = @intFromBool(resolve == .fwd); | 11887 | const bit = @intFromBool(resolve == .fwd); |
| 11369 | return @as(AnnotatedDITypePtr, @enumFromInt(addr | bit)); | 11888 | return @enumFromInt(addr | bit); |
| 11370 | } | 11889 | } |
| 11371 | | 11890 | |
| 11372 | fn toDIType(self: AnnotatedDITypePtr) *llvm.DIType { | 11891 | fn toDIType(self: AnnotatedDITypePtr) *llvm.DIType { |
| 11373 | const fixed_addr = @intFromEnum(self) & ~@as(usize, 1); | 11892 | const fixed_addr = @intFromEnum(self) & ~@as(usize, 1); |
| 11374 | return @as(*llvm.DIType, @ptrFromInt(fixed_addr)); | 11893 | return @ptrFromInt(fixed_addr); |
| 11375 | } | 11894 | } |
| 11376 | | 11895 | |
| 11377 | fn isFwdOnly(self: AnnotatedDITypePtr) bool { | 11896 | fn isFwdOnly(self: AnnotatedDITypePtr) bool { |
| ... | @@ -11401,56 +11920,39 @@ fn compilerRtIntBits(bits: u16) u16 { | ... | @@ -11401,56 +11920,39 @@ fn compilerRtIntBits(bits: u16) u16 { |
| 11401 | } | 11920 | } |
| 11402 | | 11921 | |
| 11403 | fn buildAllocaInner( | 11922 | fn buildAllocaInner( |
| 11404 | context: *llvm.Context, | 11923 | wip: *Builder.WipFunction, |
| 11405 | builder: *llvm.Builder, | | |
| 11406 | llvm_func: *llvm.Value, | | |
| 11407 | di_scope_non_null: bool, | 11924 | di_scope_non_null: bool, |
| 11408 | llvm_ty: *llvm.Type, | 11925 | llvm_ty: Builder.Type, |
| 11409 | maybe_alignment: ?c_uint, | 11926 | alignment: Builder.Alignment, |
| 11410 | target: std.Target, | 11927 | target: std.Target, |
| 11411 | ) *llvm.Value { | 11928 | ) Allocator.Error!Builder.Value { |
| 11412 | const address_space = llvmAllocaAddressSpace(target); | 11929 | const address_space = llvmAllocaAddressSpace(target); |
| 11413 | | 11930 | |
| 11414 | const alloca = blk: { | 11931 | const alloca = blk: { |
| 11415 | const prev_block = builder.getInsertBlock(); | 11932 | const prev_cursor = wip.cursor; |
| 11416 | const prev_debug_location = builder.getCurrentDebugLocation2(); | 11933 | const prev_debug_location = wip.llvm.builder.getCurrentDebugLocation2(); |
| 11417 | defer { | 11934 | defer { |
| 11418 | builder.positionBuilderAtEnd(prev_block); | 11935 | wip.cursor = prev_cursor; |
| 11419 | if (di_scope_non_null) { | 11936 | if (wip.cursor.block == .entry) wip.cursor.instruction += 1; |
| 11420 | builder.setCurrentDebugLocation2(prev_debug_location); | 11937 | if (di_scope_non_null) wip.llvm.builder.setCurrentDebugLocation2(prev_debug_location); |
| 11421 | } | | |
| 11422 | } | | |
| 11423 | | | |
| 11424 | const entry_block = llvm_func.getFirstBasicBlock().?; | | |
| 11425 | if (entry_block.getFirstInstruction()) |first_inst| { | | |
| 11426 | builder.positionBuilder(entry_block, first_inst); | | |
| 11427 | } else { | | |
| 11428 | builder.positionBuilderAtEnd(entry_block); | | |
| 11429 | } | 11938 | } |
| 11430 | builder.clearCurrentDebugLocation(); | | |
| 11431 | | 11939 | |
| 11432 | break :blk builder.buildAllocaInAddressSpace(llvm_ty, address_space, ""); | 11940 | wip.cursor = .{ .block = .entry }; |
| | 11941 | wip.llvm.builder.clearCurrentDebugLocation(); |
| | 11942 | break :blk try wip.alloca(.normal, llvm_ty, .none, alignment, address_space, ""); |
| 11433 | }; | 11943 | }; |
| 11434 | | 11944 | |
| 11435 | if (maybe_alignment) |alignment| { | | |
| 11436 | alloca.setAlignment(alignment); | | |
| 11437 | } | | |
| 11438 | | | |
| 11439 | // The pointer returned from this function should have the generic address space, | 11945 | // The pointer returned from this function should have the generic address space, |
| 11440 | // if this isn't the case then cast it to the generic address space. | 11946 | // if this isn't the case then cast it to the generic address space. |
| 11441 | if (address_space != llvm.address_space.default) { | 11947 | return wip.conv(.unneeded, alloca, .ptr, ""); |
| 11442 | return builder.buildAddrSpaceCast(alloca, context.pointerType(llvm.address_space.default), ""); | | |
| 11443 | } | | |
| 11444 | | | |
| 11445 | return alloca; | | |
| 11446 | } | 11948 | } |
| 11447 | | 11949 | |
| 11448 | fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u1 { | 11950 | fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u1 { |
| 11449 | return @intFromBool(Type.anyerror.abiAlignment(mod) > payload_ty.abiAlignment(mod)); | 11951 | return @intFromBool(Type.err_int.abiAlignment(mod) > payload_ty.abiAlignment(mod)); |
| 11450 | } | 11952 | } |
| 11451 | | 11953 | |
| 11452 | fn errUnionErrorOffset(payload_ty: Type, mod: *Module) u1 { | 11954 | fn errUnionErrorOffset(payload_ty: Type, mod: *Module) u1 { |
| 11453 | return @intFromBool(Type.anyerror.abiAlignment(mod) <= payload_ty.abiAlignment(mod)); | 11955 | return @intFromBool(Type.err_int.abiAlignment(mod) <= payload_ty.abiAlignment(mod)); |
| 11454 | } | 11956 | } |
| 11455 | | 11957 | |
| 11456 | /// Returns true for asm constraint (e.g. "=*m", "=r") if it accepts a memory location | 11958 | /// Returns true for asm constraint (e.g. "=*m", "=r") if it accepts a memory location |