| ... | @@ -1,36 +1,103 @@ | ... | @@ -1,36 +1,103 @@ |
| 1 | const std = @import("std"); | 1 | // Compilation |
| 2 | const Allocator = std.mem.Allocator; | 2 | pt: Zcu.PerThread, |
| 3 | const Target = std.Target; | 3 | zcu: *Zcu, |
| 4 | const Signedness = std.lang.Signedness; | 4 | gpa: Allocator, |
| 5 | const assert = std.debug.assert; | 5 | arena: Allocator, |
| 6 | const log = std.log.scoped(.codegen); | 6 | air: Air, |
| | 7 | liveness: Air.Liveness, |
| | 8 | owner_nav: InternPool.Nav.Index, |
| | 9 | base_line: u32, |
| 7 | | 10 | |
| 8 | const builtin = @import("builtin"); | 11 | // Module-level output (accumulated across the nav's codegen) |
| 9 | const link = @import("../../link.zig"); | 12 | next_result_id: Word = 1, |
| 10 | const codegen = @import("../../codegen.zig"); | 13 | decls: std.ArrayList(Decl) = .empty, |
| 11 | const Zcu = @import("../../Zcu.zig"); | 14 | decl_deps: std.ArrayList(Decl.Index) = .empty, |
| 12 | const Type = @import("../../Type.zig"); | 15 | nav_link: std.AutoHashMapUnmanaged(InternPool.Nav.Index, Decl.Index) = .empty, |
| 13 | const Value = @import("../../Value.zig"); | 16 | uav_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, spec.StorageClass }, Decl.Index) = .empty, |
| 14 | const Air = @import("../../Air.zig"); | 17 | entry_points: std.array_hash_map.Auto(Id, EntryPoint) = .empty, |
| 15 | const InternPool = @import("../../InternPool.zig"); | 18 | error_buffer: ?Decl.Index = null, |
| 16 | const Section = @import("Section.zig"); | 19 | struct_types: std.array_hash_map.Custom(StructType, Id, StructType.HashContext, true) = .empty, |
| 17 | const Assembler = @import("Assembler.zig"); | 20 | builtins: std.AutoHashMapUnmanaged(struct { spec.BuiltIn, spec.StorageClass }, Decl.Index) = .empty, |
| 18 | const Mir = @import("Mir.zig"); | 21 | sections: struct { |
| | 22 | // Module layout, according to SPIR-V Spec section 2.4, "Logical Layout of a Module". |
| | 23 | extended_instruction_set: Section = .{}, |
| | 24 | memory_model: Section = .{}, |
| | 25 | execution_modes: Section = .{}, |
| | 26 | debug_strings: Section = .{}, |
| | 27 | debug_names: Section = .{}, |
| | 28 | annotations: Section = .{}, |
| | 29 | globals: Section = .{}, |
| | 30 | functions: Section = .{}, |
| | 31 | } = .{}, |
| | 32 | |
| | 33 | // Per-function state (reset between top-level genNav calls) |
| | 34 | prologue: Section = .{}, |
| | 35 | body: Section = .{}, |
| | 36 | args: std.ArrayList(Id) = .empty, |
| | 37 | next_arg_index: u32 = 0, |
| | 38 | block_stack: std.ArrayList(*Block) = .empty, |
| | 39 | block_label: Id = .none, |
| | 40 | /// Whether the current block has been terminated by a terminator |
| | 41 | /// instruction (e.g. OpKill from inline assembly). When true, no further |
| | 42 | /// branch instructions should be emitted for the current block. |
| | 43 | block_terminated: bool = false, |
| | 44 | block_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, |
| | 45 | inst_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, |
| | 46 | tracked_allocas: std.AutoHashMapUnmanaged(Id, ?Id) = .empty, |
| | 47 | loop_switches: std.AutoHashMapUnmanaged(Air.Inst.Index, LoopSwitch) = .empty, |
| | 48 | id_scratch: std.ArrayList(Id) = .empty, |
| 19 | | 49 | |
| 20 | const spec = @import("spec.zig"); | 50 | const big_int_bits = @bitSizeOf(u32); |
| 21 | const Opcode = spec.Opcode; | | |
| 22 | const Word = spec.Word; | | |
| 23 | const Id = spec.Id; | | |
| 24 | const IdRange = spec.IdRange; | | |
| 25 | const StorageClass = spec.StorageClass; | | |
| 26 | | 51 | |
| 27 | const Module = @import("Module.zig"); | 52 | /// Data can be lowered into in two basic representations: indirect, which is when |
| 28 | const Decl = Module.Decl; | 53 | /// a type is stored in memory, and direct, which is how a type is stored when its |
| 29 | const Repr = Module.Repr; | 54 | /// a direct SPIR-V value. |
| 30 | const InternMap = Module.InternMap; | 55 | pub const Repr = enum { |
| 31 | const PtrTypeMap = Module.PtrTypeMap; | 56 | /// A SPIR-V value as it would be used in operations. |
| | 57 | direct, |
| | 58 | /// A SPIR-V value as it is stored in memory. |
| | 59 | indirect, |
| | 60 | }; |
| 32 | | 61 | |
| 33 | const CodeGen = @This(); | 62 | /// A function or global, tracked here so the linker can order globals and build |
| | 63 | /// per-entry-point interface lists. |
| | 64 | pub const Decl = struct { |
| | 65 | pub const Index = enum(u32) { _ }; |
| | 66 | pub const Kind = enum { func, global, invocation_global }; |
| | 67 | |
| | 68 | kind: Kind, |
| | 69 | /// Result-id of the associated OpFunction / OpVariable / InvocationGlobal. |
| | 70 | result_id: Id, |
| | 71 | /// Range into `decl_deps` for this decl's dependencies. |
| | 72 | begin_dep: usize = 0, |
| | 73 | end_dep: usize = 0, |
| | 74 | /// Whether an extern-function stub has been emitted. |
| | 75 | has_extern_stub: bool = false, |
| | 76 | }; |
| | 77 | |
| | 78 | pub const EntryPoint = struct { |
| | 79 | decl_index: Decl.Index, |
| | 80 | name: []const u8, |
| | 81 | cc: std.builtin.CallingConvention, |
| | 82 | }; |
| | 83 | |
| | 84 | const StructType = struct { |
| | 85 | fields: []const Id, |
| | 86 | ip_index: InternPool.Index, |
| | 87 | |
| | 88 | const HashContext = struct { |
| | 89 | pub fn hash(_: @This(), ty: StructType) u32 { |
| | 90 | var hasher = std.hash.Wyhash.init(0); |
| | 91 | hasher.update(std.mem.sliceAsBytes(ty.fields)); |
| | 92 | hasher.update(std.mem.asBytes(&ty.ip_index)); |
| | 93 | return @truncate(hasher.final()); |
| | 94 | } |
| | 95 | |
| | 96 | pub fn eql(_: @This(), a: StructType, b: StructType, _: usize) bool { |
| | 97 | return a.ip_index == b.ip_index and std.mem.eql(Id, a.fields, b.fields); |
| | 98 | } |
| | 99 | }; |
| | 100 | }; |
| 34 | | 101 | |
| 35 | pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { | 102 | pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { |
| 36 | return comptime &.initMany(&.{ | 103 | return comptime &.initMany(&.{ |
| ... | @@ -43,60 +110,40 @@ pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { | ... | @@ -43,60 +110,40 @@ pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { |
| 43 | }); | 110 | }); |
| 44 | } | 111 | } |
| 45 | | 112 | |
| 46 | pub const zig_call_abi_ver = 3; | | |
| 47 | | | |
| 48 | const LoopSwitch = struct { cond_var: Id, continue_label: Id }; | 113 | const LoopSwitch = struct { cond_var: Id, continue_label: Id }; |
| 49 | | 114 | |
| 50 | /// This type indicates the way that a block is terminated. The | 115 | /// Pointer-typed AIR refs should resolve through `resolvePtr` to handle the |
| 51 | /// state of a particular block is used to track how a jump from | 116 | /// `tracked_allocas` case explicitly at every use site. |
| 52 | /// inside the block must reach the outside. | 117 | const Ptr = union(enum) { |
| | 118 | id: Id, |
| | 119 | /// Function-local pointer whose value lives in `tracked_allocas` rather |
| | 120 | /// than a real OpVariable. `slot` is the current pointee value. |
| | 121 | tracked: struct { id: Id, slot: *?Id }, |
| | 122 | }; |
| | 123 | |
| | 124 | /// Tracks how control flow leaves a Zig `block` under SPIR-V's structured |
| | 125 | /// control flow rules. |
| 53 | const Block = union(enum) { | 126 | const Block = union(enum) { |
| 54 | const Incoming = struct { | 127 | const Incoming = struct { |
| 55 | src_label: Id, | 128 | src_label: Id, |
| 56 | /// Instruction that returns an u32 value of the | 129 | /// Block index (u32) that control flow should jump to next. |
| 57 | /// `Air.Inst.Index` that control flow should jump to. | | |
| 58 | next_block: Id, | 130 | next_block: Id, |
| 59 | }; | 131 | }; |
| 60 | | 132 | |
| 61 | const SelectionMerge = struct { | 133 | const SelectionMerge = struct { |
| 62 | /// Incoming block from the `then` label. | | |
| 63 | /// Note that the incoming block from the `else` label is | | |
| 64 | /// either given by the next element in the stack. | | |
| 65 | incoming: Incoming, | 134 | incoming: Incoming, |
| 66 | /// The label id of the cond_br's merge block. | 135 | /// Label of the cond_br's merge block (undefined for top-of-stack). |
| 67 | /// For the top-most element in the stack, this | | |
| 68 | /// value is undefined. | | |
| 69 | merge_block: Id, | 136 | merge_block: Id, |
| 70 | }; | 137 | }; |
| 71 | | 138 | |
| 72 | /// For a `selection` type block, we cannot use early exits, and we | 139 | /// Selection blocks can't use early exits. Closing requires a "merge ladder" |
| 73 | /// must generate a 'merge ladder' of OpSelection instructions. To that end, | 140 | /// of nested OpSelectionMerge instructions, one per pending merge. |
| 74 | /// we keep a stack of the merges that still must be closed at the end of | | |
| 75 | /// a block. | | |
| 76 | /// | | |
| 77 | /// This entire structure basically just resembles a tree like | | |
| 78 | /// a x | | |
| 79 | /// \ / | | |
| 80 | /// b o merge | | |
| 81 | /// \ / | | |
| 82 | /// c o merge | | |
| 83 | /// \ / | | |
| 84 | /// o merge | | |
| 85 | /// / | | |
| 86 | /// o jump to next block | | |
| 87 | selection: struct { | 141 | selection: struct { |
| 88 | /// In order to know which merges we still need to do, we need to keep | | |
| 89 | /// a stack of those. | | |
| 90 | merge_stack: std.ArrayList(SelectionMerge) = .empty, | 142 | merge_stack: std.ArrayList(SelectionMerge) = .empty, |
| 91 | }, | 143 | }, |
| 92 | /// For a `loop` type block, we can early-exit the block by | 144 | /// Loop blocks early-exit by jumping to the loop merge label. |
| 93 | /// jumping to the loop exit node, and we don't need to generate | | |
| 94 | /// an entire stack of merges. | | |
| 95 | loop: struct { | 145 | loop: struct { |
| 96 | /// The next block to jump to can be determined from any number | | |
| 97 | /// of conditions that jump to the loop exit. | | |
| 98 | merges: std.ArrayList(Incoming) = .empty, | 146 | merges: std.ArrayList(Incoming) = .empty, |
| 99 | /// The label id of the loop's merge block. | | |
| 100 | merge_block: Id, | 147 | merge_block: Id, |
| 101 | }, | 148 | }, |
| 102 | | 149 | |
| ... | @@ -109,39 +156,36 @@ const Block = union(enum) { | ... | @@ -109,39 +156,36 @@ const Block = union(enum) { |
| 109 | } | 156 | } |
| 110 | }; | 157 | }; |
| 111 | | 158 | |
| 112 | pt: Zcu.PerThread, | | |
| 113 | air: Air, | | |
| 114 | liveness: Air.Liveness, | | |
| 115 | owner_nav: InternPool.Nav.Index, | | |
| 116 | module: *Module, | | |
| 117 | block_stack: std.ArrayList(*Block) = .empty, | | |
| 118 | block_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, | | |
| 119 | base_line: u32, | | |
| 120 | block_label: Id = .none, | | |
| 121 | /// Whether the current block has been terminated by a terminator | | |
| 122 | /// instruction (e.g. OpKill from inline assembly). When true, no further | | |
| 123 | /// branch instructions should be emitted for the current block. | | |
| 124 | block_terminated: bool = false, | | |
| 125 | next_arg_index: u32 = 0, | | |
| 126 | args: std.ArrayList(Id) = .empty, | | |
| 127 | virtual_allocas: std.AutoHashMapUnmanaged(Id, ?Id) = .empty, | | |
| 128 | inst_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, | | |
| 129 | loop_switches: std.AutoHashMapUnmanaged(Air.Inst.Index, LoopSwitch) = .empty, | | |
| 130 | id_scratch: std.ArrayList(Id) = .empty, | | |
| 131 | prologue: Section = .{}, | | |
| 132 | body: Section = .{}, | | |
| 133 | | | |
| 134 | pub fn deinit(cg: *CodeGen) void { | 159 | pub fn deinit(cg: *CodeGen) void { |
| 135 | const gpa = cg.module.gpa; | 160 | const gpa = cg.gpa; |
| 136 | cg.block_stack.deinit(gpa); | 161 | cg.block_stack.deinit(gpa); |
| 137 | cg.block_results.deinit(gpa); | 162 | cg.block_results.deinit(gpa); |
| 138 | cg.args.deinit(gpa); | 163 | cg.args.deinit(gpa); |
| 139 | cg.virtual_allocas.deinit(gpa); | 164 | cg.tracked_allocas.deinit(gpa); |
| 140 | cg.inst_results.deinit(gpa); | 165 | cg.inst_results.deinit(gpa); |
| 141 | cg.loop_switches.deinit(gpa); | 166 | cg.loop_switches.deinit(gpa); |
| 142 | cg.id_scratch.deinit(gpa); | 167 | cg.id_scratch.deinit(gpa); |
| 143 | cg.prologue.deinit(gpa); | 168 | cg.prologue.deinit(gpa); |
| 144 | cg.body.deinit(gpa); | 169 | cg.body.deinit(gpa); |
| | 170 | |
| | 171 | cg.nav_link.deinit(gpa); |
| | 172 | cg.uav_link.deinit(gpa); |
| | 173 | |
| | 174 | cg.sections.extended_instruction_set.deinit(gpa); |
| | 175 | cg.sections.memory_model.deinit(gpa); |
| | 176 | cg.sections.execution_modes.deinit(gpa); |
| | 177 | cg.sections.debug_strings.deinit(gpa); |
| | 178 | cg.sections.debug_names.deinit(gpa); |
| | 179 | cg.sections.annotations.deinit(gpa); |
| | 180 | cg.sections.globals.deinit(gpa); |
| | 181 | cg.sections.functions.deinit(gpa); |
| | 182 | |
| | 183 | cg.struct_types.deinit(gpa); |
| | 184 | cg.builtins.deinit(gpa); |
| | 185 | |
| | 186 | cg.decls.deinit(gpa); |
| | 187 | cg.decl_deps.deinit(gpa); |
| | 188 | cg.entry_points.deinit(gpa); |
| 145 | } | 189 | } |
| 146 | | 190 | |
| 147 | pub fn generate( | 191 | pub fn generate( |
| ... | @@ -157,19 +201,15 @@ pub fn generate( | ... | @@ -157,19 +201,15 @@ pub fn generate( |
| 157 | | 201 | |
| 158 | var arena = std.heap.ArenaAllocator.init(gpa); | 202 | var arena = std.heap.ArenaAllocator.init(gpa); |
| 159 | defer arena.deinit(); | 203 | defer arena.deinit(); |
| 160 | var module: Module = .{ | | |
| 161 | .gpa = gpa, | | |
| 162 | .arena = arena.allocator(), | | |
| 163 | .zcu = zcu, | | |
| 164 | }; | | |
| 165 | defer module.deinit(); | | |
| 166 | | 204 | |
| 167 | var cg: CodeGen = .{ | 205 | var cg: CodeGen = .{ |
| 168 | .pt = pt, | 206 | .pt = pt, |
| | 207 | .gpa = gpa, |
| | 208 | .arena = arena.allocator(), |
| | 209 | .zcu = zcu, |
| 169 | .air = air.*, | 210 | .air = air.*, |
| 170 | .liveness = liveness.*.?, | 211 | .liveness = liveness.*.?, |
| 171 | .owner_nav = nav, | 212 | .owner_nav = nav, |
| 172 | .module = &module, | | |
| 173 | .base_line = zcu.navSrcLine(nav), | 213 | .base_line = zcu.navSrcLine(nav), |
| 174 | }; | 214 | }; |
| 175 | defer cg.deinit(); | 215 | defer cg.deinit(); |
| ... | @@ -191,19 +231,15 @@ pub fn generateNav( | ... | @@ -191,19 +231,15 @@ pub fn generateNav( |
| 191 | | 231 | |
| 192 | var arena = std.heap.ArenaAllocator.init(gpa); | 232 | var arena = std.heap.ArenaAllocator.init(gpa); |
| 193 | defer arena.deinit(); | 233 | defer arena.deinit(); |
| 194 | var module: Module = .{ | | |
| 195 | .gpa = gpa, | | |
| 196 | .arena = arena.allocator(), | | |
| 197 | .zcu = zcu, | | |
| 198 | }; | | |
| 199 | defer module.deinit(); | | |
| 200 | | 234 | |
| 201 | var cg: CodeGen = .{ | 235 | var cg: CodeGen = .{ |
| 202 | .pt = pt, | 236 | .pt = pt, |
| | 237 | .gpa = gpa, |
| | 238 | .arena = arena.allocator(), |
| | 239 | .zcu = zcu, |
| 203 | .air = undefined, | 240 | .air = undefined, |
| 204 | .liveness = undefined, | 241 | .liveness = undefined, |
| 205 | .owner_nav = nav_index, | 242 | .owner_nav = nav_index, |
| 206 | .module = &module, | | |
| 207 | .base_line = zcu.navSrcLine(nav_index), | 243 | .base_line = zcu.navSrcLine(nav_index), |
| 208 | }; | 244 | }; |
| 209 | defer cg.deinit(); | 245 | defer cg.deinit(); |
| ... | @@ -217,11 +253,9 @@ pub fn generateNav( | ... | @@ -217,11 +253,9 @@ pub fn generateNav( |
| 217 | } | 253 | } |
| 218 | | 254 | |
| 219 | fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | 255 | fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 220 | const module = cg.module; | 256 | const owner_entry = cg.nav_link.get(cg.owner_nav); |
| 221 | | | |
| 222 | const owner_entry = module.nav_link.get(cg.owner_nav); | | |
| 223 | const owner_decl_index = owner_entry orelse return .{ | 257 | const owner_decl_index = owner_entry orelse return .{ |
| 224 | .id_bound = module.next_result_id, | 258 | .id_bound = cg.next_result_id, |
| 225 | .owner_nav = cg.owner_nav, | 259 | .owner_nav = cg.owner_nav, |
| 226 | .kind = .func, | 260 | .kind = .func, |
| 227 | .decl_result_id = .none, | 261 | .decl_result_id = .none, |
| ... | @@ -239,14 +273,14 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -239,14 +273,14 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 239 | .entry_points = &.{}, | 273 | .entry_points = &.{}, |
| 240 | }; | 274 | }; |
| 241 | | 275 | |
| 242 | const owner_decl = module.declPtr(owner_decl_index); | 276 | const owner_decl = cg.declPtr(owner_decl_index); |
| 243 | | 277 | |
| 244 | var nav_refs: std.ArrayList(Mir.NavRef) = .empty; | 278 | var nav_refs: std.ArrayList(Mir.NavRef) = .empty; |
| 245 | defer nav_refs.deinit(gpa); | 279 | defer nav_refs.deinit(gpa); |
| 246 | var nav_it = module.nav_link.iterator(); | 280 | var nav_it = cg.nav_link.iterator(); |
| 247 | while (nav_it.next()) |entry| { | 281 | while (nav_it.next()) |entry| { |
| 248 | if (entry.key_ptr.* == cg.owner_nav) continue; | 282 | if (entry.key_ptr.* == cg.owner_nav) continue; |
| 249 | const decl = module.declPtr(entry.value_ptr.*); | 283 | const decl = cg.declPtr(entry.value_ptr.*); |
| 250 | try nav_refs.append(gpa, .{ | 284 | try nav_refs.append(gpa, .{ |
| 251 | .local_id = decl.result_id, | 285 | .local_id = decl.result_id, |
| 252 | .nav = entry.key_ptr.*, | 286 | .nav = entry.key_ptr.*, |
| ... | @@ -256,9 +290,9 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -256,9 +290,9 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 256 | | 290 | |
| 257 | var uav_refs: std.ArrayList(Mir.UavRef) = .empty; | 291 | var uav_refs: std.ArrayList(Mir.UavRef) = .empty; |
| 258 | defer uav_refs.deinit(gpa); | 292 | defer uav_refs.deinit(gpa); |
| 259 | var uav_it = module.uav_link.iterator(); | 293 | var uav_it = cg.uav_link.iterator(); |
| 260 | while (uav_it.next()) |entry| { | 294 | while (uav_it.next()) |entry| { |
| 261 | const decl = module.declPtr(entry.value_ptr.*); | 295 | const decl = cg.declPtr(entry.value_ptr.*); |
| 262 | try uav_refs.append(gpa, .{ | 296 | try uav_refs.append(gpa, .{ |
| 263 | .local_id = decl.result_id, | 297 | .local_id = decl.result_id, |
| 264 | .val = entry.key_ptr.*[0], | 298 | .val = entry.key_ptr.*[0], |
| ... | @@ -272,9 +306,9 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -272,9 +306,9 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 272 | var internal_globals: std.ArrayList(Id) = .empty; | 306 | var internal_globals: std.ArrayList(Id) = .empty; |
| 273 | defer internal_globals.deinit(gpa); | 307 | defer internal_globals.deinit(gpa); |
| 274 | | 308 | |
| 275 | const deps = module.decl_deps.items[owner_decl.begin_dep..owner_decl.end_dep]; | 309 | const deps = cg.decl_deps.items[owner_decl.begin_dep..owner_decl.end_dep]; |
| 276 | for (deps) |dep_index| { | 310 | for (deps) |dep_index| { |
| 277 | const dep_decl = module.declPtr(dep_index); | 311 | const dep_decl = cg.declPtr(dep_index); |
| 278 | var found = false; | 312 | var found = false; |
| 279 | nav_it.index = 0; | 313 | nav_it.index = 0; |
| 280 | while (nav_it.next()) |entry| { | 314 | while (nav_it.next()) |entry| { |
| ... | @@ -294,10 +328,10 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -294,10 +328,10 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 294 | | 328 | |
| 295 | var ep_list: std.ArrayList(Mir.EntryPoint) = .empty; | 329 | var ep_list: std.ArrayList(Mir.EntryPoint) = .empty; |
| 296 | defer ep_list.deinit(gpa); | 330 | defer ep_list.deinit(gpa); |
| 297 | var ep_it = module.entry_points.iterator(); | 331 | var ep_it = cg.entry_points.iterator(); |
| 298 | while (ep_it.next()) |entry| { | 332 | while (ep_it.next()) |entry| { |
| 299 | const ep = entry.value_ptr; | 333 | const ep = entry.value_ptr; |
| 300 | const ep_decl = module.declPtr(ep.decl_index); | 334 | const ep_decl = cg.declPtr(ep.decl_index); |
| 301 | try ep_list.append(gpa, .{ | 335 | try ep_list.append(gpa, .{ |
| 302 | .local_id = ep_decl.result_id, | 336 | .local_id = ep_decl.result_id, |
| 303 | .name = try gpa.dupe(u8, ep.name), | 337 | .name = try gpa.dupe(u8, ep.name), |
| ... | @@ -306,17 +340,17 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -306,17 +340,17 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 306 | } | 340 | } |
| 307 | | 341 | |
| 308 | return .{ | 342 | return .{ |
| 309 | .id_bound = module.next_result_id, | 343 | .id_bound = cg.next_result_id, |
| 310 | .owner_nav = cg.owner_nav, | 344 | .owner_nav = cg.owner_nav, |
| 311 | .kind = owner_decl.kind, | 345 | .kind = owner_decl.kind, |
| 312 | .decl_result_id = owner_decl.result_id, | 346 | .decl_result_id = owner_decl.result_id, |
| 313 | .extended_instruction_set = try module.sections.extended_instruction_set.instructions.toOwnedSlice(gpa), | 347 | .extended_instruction_set = try cg.sections.extended_instruction_set.instructions.toOwnedSlice(gpa), |
| 314 | .globals = try module.sections.globals.instructions.toOwnedSlice(gpa), | 348 | .globals = try cg.sections.globals.instructions.toOwnedSlice(gpa), |
| 315 | .functions = try module.sections.functions.instructions.toOwnedSlice(gpa), | 349 | .functions = try cg.sections.functions.instructions.toOwnedSlice(gpa), |
| 316 | .annotations = try module.sections.annotations.instructions.toOwnedSlice(gpa), | 350 | .annotations = try cg.sections.annotations.instructions.toOwnedSlice(gpa), |
| 317 | .debug_names = try module.sections.debug_names.instructions.toOwnedSlice(gpa), | 351 | .debug_names = try cg.sections.debug_names.instructions.toOwnedSlice(gpa), |
| 318 | .debug_strings = try module.sections.debug_strings.instructions.toOwnedSlice(gpa), | 352 | .debug_strings = try cg.sections.debug_strings.instructions.toOwnedSlice(gpa), |
| 319 | .execution_modes = try module.sections.execution_modes.instructions.toOwnedSlice(gpa), | 353 | .execution_modes = try cg.sections.execution_modes.instructions.toOwnedSlice(gpa), |
| 320 | .nav_refs = try nav_refs.toOwnedSlice(gpa), | 354 | .nav_refs = try nav_refs.toOwnedSlice(gpa), |
| 321 | .uav_refs = try uav_refs.toOwnedSlice(gpa), | 355 | .uav_refs = try uav_refs.toOwnedSlice(gpa), |
| 322 | .decl_deps = try decl_deps.toOwnedSlice(gpa), | 356 | .decl_deps = try decl_deps.toOwnedSlice(gpa), |
| ... | @@ -325,11 +359,365 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { | ... | @@ -325,11 +359,365 @@ fn serializeToMir(cg: *CodeGen, gpa: Allocator) codegen.Error!Mir { |
| 325 | }; | 359 | }; |
| 326 | } | 360 | } |
| 327 | | 361 | |
| | 362 | fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type { |
| | 363 | const zcu = cg.zcu; |
| | 364 | return cg.air.typeOf(inst, &zcu.intern_pool); |
| | 365 | } |
| | 366 | |
| | 367 | fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type { |
| | 368 | const zcu = cg.zcu; |
| | 369 | return cg.air.typeOfIndex(inst, &zcu.intern_pool); |
| | 370 | } |
| | 371 | |
| | 372 | /// Does not generate the nav. |
| | 373 | pub fn resolveNav(cg: *CodeGen, ip: *InternPool, nav_index: InternPool.Nav.Index) !Decl.Index { |
| | 374 | const entry = try cg.nav_link.getOrPut(cg.gpa, nav_index); |
| | 375 | if (!entry.found_existing) { |
| | 376 | const nav = ip.getNav(nav_index); |
| | 377 | // TODO: Extern fn? |
| | 378 | const kind: Decl.Kind = if (ip.isFunctionType(nav.resolved.?.type)) |
| | 379 | .func |
| | 380 | else switch (nav.resolved.?.@"addrspace") { |
| | 381 | .generic => .invocation_global, |
| | 382 | else => .global, |
| | 383 | }; |
| | 384 | entry.value_ptr.* = try cg.allocDecl(kind); |
| | 385 | } |
| | 386 | |
| | 387 | return entry.value_ptr.*; |
| | 388 | } |
| | 389 | |
| | 390 | pub fn allocIds(cg: *CodeGen, n: u32) spec.IdRange { |
| | 391 | defer cg.next_result_id += n; |
| | 392 | return .{ .base = cg.next_result_id, .len = n }; |
| | 393 | } |
| | 394 | |
| | 395 | pub fn allocId(cg: *CodeGen) Id { |
| | 396 | return cg.allocIds(1).at(0); |
| | 397 | } |
| | 398 | |
| | 399 | pub fn idBound(cg: *const CodeGen) Word { |
| | 400 | return cg.next_result_id; |
| | 401 | } |
| | 402 | |
| | 403 | pub fn addEntryPointDeps( |
| | 404 | cg: *CodeGen, |
| | 405 | decl_index: Decl.Index, |
| | 406 | seen: *std.bit_set.Dynamic, |
| | 407 | interface: *std.array_list.Managed(Id), |
| | 408 | ) !void { |
| | 409 | const decl = cg.declPtr(decl_index); |
| | 410 | const deps = cg.decl_deps.items[decl.begin_dep..decl.end_dep]; |
| | 411 | |
| | 412 | if (seen.isSet(@intFromEnum(decl_index))) { |
| | 413 | return; |
| | 414 | } |
| | 415 | |
| | 416 | seen.set(@intFromEnum(decl_index)); |
| | 417 | |
| | 418 | if (decl.kind == .global) { |
| | 419 | try interface.append(decl.result_id); |
| | 420 | } |
| | 421 | |
| | 422 | for (deps) |dep| { |
| | 423 | try cg.addEntryPointDeps(dep, seen, interface); |
| | 424 | } |
| | 425 | } |
| | 426 | |
| | 427 | pub fn importInstructionSet(cg: *CodeGen, set: spec.InstructionSet) !Id { |
| | 428 | assert(set != .core); |
| | 429 | const result_id = cg.allocId(); |
| | 430 | try cg.sections.extended_instruction_set.emit(cg.gpa, .OpExtInstImport, .{ |
| | 431 | .id_result = result_id, |
| | 432 | .name = @tagName(set), |
| | 433 | }); |
| | 434 | return result_id; |
| | 435 | } |
| | 436 | |
| | 437 | pub fn boolType(cg: *CodeGen) !Id { |
| | 438 | const result_id = cg.allocId(); |
| | 439 | try cg.sections.globals.emit(cg.gpa, .OpTypeBool, .{ |
| | 440 | .id_result = result_id, |
| | 441 | }); |
| | 442 | return result_id; |
| | 443 | } |
| | 444 | |
| | 445 | pub fn voidType(cg: *CodeGen) !Id { |
| | 446 | const result_id = cg.allocId(); |
| | 447 | try cg.sections.globals.emit(cg.gpa, .OpTypeVoid, .{ |
| | 448 | .id_result = result_id, |
| | 449 | }); |
| | 450 | try cg.debugName(result_id, "void"); |
| | 451 | return result_id; |
| | 452 | } |
| | 453 | |
| | 454 | pub fn opaqueType(cg: *CodeGen, name: []const u8) !Id { |
| | 455 | const result_id = cg.allocId(); |
| | 456 | try cg.sections.globals.emit(cg.gpa, .OpTypeOpaque, .{ |
| | 457 | .id_result = result_id, |
| | 458 | .literal_string = name, |
| | 459 | }); |
| | 460 | try cg.debugName(result_id, name); |
| | 461 | return result_id; |
| | 462 | } |
| | 463 | |
| | 464 | pub fn backingIntBits(cg: *const CodeGen, bits: u16) struct { u16, bool } { |
| | 465 | assert(bits != 0); |
| | 466 | const target = cg.zcu.getTarget(); |
| | 467 | const ints = [_]struct { bits: u16, enabled: bool }{ |
| | 468 | .{ .bits = 8, .enabled = target.cpu.has(.spirv, .int8) }, |
| | 469 | .{ .bits = 16, .enabled = target.cpu.has(.spirv, .int16) }, |
| | 470 | .{ .bits = 32, .enabled = true }, |
| | 471 | .{ .bits = 64, .enabled = target.cpu.has(.spirv, .int64) or target.cpu.arch == .spirv64 }, |
| | 472 | }; |
| | 473 | |
| | 474 | for (ints) |int| { |
| | 475 | if (bits <= int.bits and int.enabled) return .{ int.bits, false }; |
| | 476 | } |
| | 477 | |
| | 478 | return .{ std.mem.alignForward(u16, bits, big_int_bits), true }; |
| | 479 | } |
| | 480 | |
| | 481 | pub fn intType(cg: *CodeGen, signedness: std.lang.Signedness, bits: u16) !Id { |
| | 482 | assert(bits > 0); |
| | 483 | |
| | 484 | const target = cg.zcu.getTarget(); |
| | 485 | const actual_signedness = switch (target.os.tag) { |
| | 486 | // Kernel only supports unsigned ints. |
| | 487 | .opencl, .amdhsa => .unsigned, |
| | 488 | else => signedness, |
| | 489 | }; |
| | 490 | const backing_bits, const big_int = cg.backingIntBits(bits); |
| | 491 | if (big_int) { |
| | 492 | const u32_ty = try cg.intType(.unsigned, 32); |
| | 493 | const len_id = cg.allocId(); |
| | 494 | try cg.sections.globals.emit(cg.gpa, .OpConstant, .{ |
| | 495 | .id_result_type = u32_ty, |
| | 496 | .id_result = len_id, |
| | 497 | .value = .{ .uint32 = backing_bits / big_int_bits }, |
| | 498 | }); |
| | 499 | return cg.arrayType(len_id, u32_ty); |
| | 500 | } |
| | 501 | |
| | 502 | const result_id = cg.allocId(); |
| | 503 | try cg.sections.globals.emit(cg.gpa, .OpTypeInt, .{ |
| | 504 | .id_result = result_id, |
| | 505 | .width = backing_bits, |
| | 506 | .signedness = switch (actual_signedness) { |
| | 507 | .signed => 1, |
| | 508 | .unsigned => 0, |
| | 509 | }, |
| | 510 | }); |
| | 511 | switch (actual_signedness) { |
| | 512 | .signed => try cg.debugNameFmt(result_id, "i{}", .{backing_bits}), |
| | 513 | .unsigned => try cg.debugNameFmt(result_id, "u{}", .{backing_bits}), |
| | 514 | } |
| | 515 | return result_id; |
| | 516 | } |
| | 517 | |
| | 518 | pub fn floatType(cg: *CodeGen, bits: u16) !Id { |
| | 519 | assert(bits > 0); |
| | 520 | const result_id = cg.allocId(); |
| | 521 | try cg.sections.globals.emit(cg.gpa, .OpTypeFloat, .{ |
| | 522 | .id_result = result_id, |
| | 523 | .width = bits, |
| | 524 | }); |
| | 525 | try cg.debugNameFmt(result_id, "f{}", .{bits}); |
| | 526 | return result_id; |
| | 527 | } |
| | 528 | |
| | 529 | pub fn vectorType(cg: *CodeGen, len: u32, child_ty_id: Id) !Id { |
| | 530 | const result_id = cg.allocId(); |
| | 531 | try cg.sections.globals.emit(cg.gpa, .OpTypeVector, .{ |
| | 532 | .id_result = result_id, |
| | 533 | .component_type = child_ty_id, |
| | 534 | .component_count = len, |
| | 535 | }); |
| | 536 | return result_id; |
| | 537 | } |
| | 538 | |
| | 539 | pub fn arrayType(cg: *CodeGen, len_id: Id, child_ty_id: Id) !Id { |
| | 540 | const result_id = cg.allocId(); |
| | 541 | try cg.sections.globals.emit(cg.gpa, .OpTypeArray, .{ |
| | 542 | .id_result = result_id, |
| | 543 | .element_type = child_ty_id, |
| | 544 | .length = len_id, |
| | 545 | }); |
| | 546 | return result_id; |
| | 547 | } |
| | 548 | |
| | 549 | pub fn ptrType(cg: *CodeGen, child_ty_id: Id, storage_class: spec.StorageClass) !Id { |
| | 550 | const result_id = cg.allocId(); |
| | 551 | try cg.sections.globals.emit(cg.gpa, .OpTypePointer, .{ |
| | 552 | .id_result = result_id, |
| | 553 | .storage_class = storage_class, |
| | 554 | .type = child_ty_id, |
| | 555 | }); |
| | 556 | return result_id; |
| | 557 | } |
| | 558 | |
| | 559 | pub fn structType( |
| | 560 | cg: *CodeGen, |
| | 561 | types: []const Id, |
| | 562 | maybe_names: ?[]const []const u8, |
| | 563 | ip_index: InternPool.Index, |
| | 564 | ) !Id { |
| | 565 | const actual_ip_index = if (cg.zcu.comp.config.root_strip) .none else ip_index; |
| | 566 | |
| | 567 | if (cg.struct_types.get(.{ .fields = types, .ip_index = actual_ip_index })) |id| return id; |
| | 568 | const result_id = cg.allocId(); |
| | 569 | const types_dup = try cg.arena.dupe(Id, types); |
| | 570 | try cg.sections.globals.emit(cg.gpa, .OpTypeStruct, .{ |
| | 571 | .id_result = result_id, |
| | 572 | .id_ref = types_dup, |
| | 573 | }); |
| | 574 | |
| | 575 | if (maybe_names) |names| { |
| | 576 | assert(names.len == types.len); |
| | 577 | for (names, 0..) |name, i| { |
| | 578 | try cg.memberDebugName(result_id, @intCast(i), name); |
| | 579 | } |
| | 580 | } |
| | 581 | |
| | 582 | try cg.struct_types.put( |
| | 583 | cg.gpa, |
| | 584 | .{ .fields = types_dup, .ip_index = actual_ip_index }, |
| | 585 | result_id, |
| | 586 | ); |
| | 587 | return result_id; |
| | 588 | } |
| | 589 | |
| | 590 | pub fn functionType(cg: *CodeGen, return_ty_id: Id, param_type_ids: []const Id) !Id { |
| | 591 | const result_id = cg.allocId(); |
| | 592 | try cg.sections.globals.emit(cg.gpa, .OpTypeFunction, .{ |
| | 593 | .id_result = result_id, |
| | 594 | .return_type = return_ty_id, |
| | 595 | .id_ref_2 = param_type_ids, |
| | 596 | }); |
| | 597 | return result_id; |
| | 598 | } |
| | 599 | |
| | 600 | pub fn constUndef(cg: *CodeGen, ty_id: Id) !Id { |
| | 601 | const result_id = cg.allocId(); |
| | 602 | try cg.sections.globals.emit(cg.gpa, .OpUndef, .{ |
| | 603 | .id_result_type = ty_id, |
| | 604 | .id_result = result_id, |
| | 605 | }); |
| | 606 | return result_id; |
| | 607 | } |
| | 608 | |
| | 609 | pub fn constNull(cg: *CodeGen, ty_id: Id) !Id { |
| | 610 | const result_id = cg.allocId(); |
| | 611 | try cg.sections.globals.emit(cg.gpa, .OpConstantNull, .{ |
| | 612 | .id_result_type = ty_id, |
| | 613 | .id_result = result_id, |
| | 614 | }); |
| | 615 | return result_id; |
| | 616 | } |
| | 617 | |
| | 618 | pub fn decorate( |
| | 619 | cg: *CodeGen, |
| | 620 | target: Id, |
| | 621 | decoration: spec.Decoration.Extended, |
| | 622 | ) !void { |
| | 623 | try cg.sections.annotations.emit(cg.gpa, .OpDecorate, .{ |
| | 624 | .target = target, |
| | 625 | .decoration = decoration, |
| | 626 | }); |
| | 627 | } |
| | 628 | |
| | 629 | pub fn decorateMember( |
| | 630 | cg: *CodeGen, |
| | 631 | structure_type: Id, |
| | 632 | member: u32, |
| | 633 | decoration: spec.Decoration.Extended, |
| | 634 | ) !void { |
| | 635 | try cg.sections.annotations.emit(cg.gpa, .OpMemberDecorate, .{ |
| | 636 | .structure_type = structure_type, |
| | 637 | .member = member, |
| | 638 | .decoration = decoration, |
| | 639 | }); |
| | 640 | } |
| | 641 | |
| | 642 | pub fn allocDecl(cg: *CodeGen, kind: Decl.Kind) !Decl.Index { |
| | 643 | try cg.decls.append(cg.gpa, .{ |
| | 644 | .kind = kind, |
| | 645 | .result_id = cg.allocId(), |
| | 646 | }); |
| | 647 | |
| | 648 | return @as(Decl.Index, @enumFromInt(@as(u32, @intCast(cg.decls.items.len - 1)))); |
| | 649 | } |
| | 650 | |
| | 651 | pub fn declPtr(cg: *CodeGen, index: Decl.Index) *Decl { |
| | 652 | return &cg.decls.items[@intFromEnum(index)]; |
| | 653 | } |
| | 654 | |
| | 655 | pub fn debugName(cg: *CodeGen, target: Id, name: []const u8) !void { |
| | 656 | if (cg.zcu.comp.config.root_strip) return; |
| | 657 | try cg.sections.debug_names.emit(cg.gpa, .OpName, .{ |
| | 658 | .target = target, |
| | 659 | .name = name, |
| | 660 | }); |
| | 661 | } |
| | 662 | |
| | 663 | pub fn debugNameFmt(cg: *CodeGen, target: Id, comptime fmt: []const u8, args: anytype) !void { |
| | 664 | if (cg.zcu.comp.config.root_strip) return; |
| | 665 | const name = try std.fmt.allocPrint(cg.gpa, fmt, args); |
| | 666 | defer cg.gpa.free(name); |
| | 667 | try cg.debugName(target, name); |
| | 668 | } |
| | 669 | |
| | 670 | pub fn memberDebugName(cg: *CodeGen, target: Id, member: u32, name: []const u8) !void { |
| | 671 | if (cg.zcu.comp.config.root_strip) return; |
| | 672 | try cg.sections.debug_names.emit(cg.gpa, .OpMemberName, .{ |
| | 673 | .type = target, |
| | 674 | .member = member, |
| | 675 | .name = name, |
| | 676 | }); |
| | 677 | } |
| | 678 | |
| | 679 | pub fn storageClass(cg: *const CodeGen, as: std.lang.AddressSpace) spec.StorageClass { |
| | 680 | const target = cg.zcu.getTarget(); |
| | 681 | return switch (as) { |
| | 682 | .generic => .function, |
| | 683 | .global => switch (target.os.tag) { |
| | 684 | .opencl, .amdhsa => .cross_workgroup, |
| | 685 | else => .storage_buffer, |
| | 686 | }, |
| | 687 | .push_constant => .push_constant, |
| | 688 | .output => .output, |
| | 689 | .uniform => .uniform, |
| | 690 | .storage_buffer => .storage_buffer, |
| | 691 | .physical_storage_buffer => .physical_storage_buffer, |
| | 692 | .constant => .uniform_constant, |
| | 693 | .shared => .workgroup, |
| | 694 | .local => .function, |
| | 695 | .input => .input, |
| | 696 | .gs, |
| | 697 | .fs, |
| | 698 | .ss, |
| | 699 | .far, |
| | 700 | .param, |
| | 701 | .flash, |
| | 702 | .flash1, |
| | 703 | .flash2, |
| | 704 | .flash3, |
| | 705 | .flash4, |
| | 706 | .flash5, |
| | 707 | .cog, |
| | 708 | .lut, |
| | 709 | .hub, |
| | 710 | .externref, |
| | 711 | .funcref, |
| | 712 | => unreachable, |
| | 713 | }; |
| | 714 | } |
| | 715 | |
| 328 | const Error = error{ AlreadyReported, OutOfMemory }; | 716 | const Error = error{ AlreadyReported, OutOfMemory }; |
| 329 | | 717 | |
| 330 | pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | 718 | pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 331 | const gpa = cg.module.gpa; | 719 | const gpa = cg.gpa; |
| 332 | const zcu = cg.module.zcu; | 720 | const zcu = cg.zcu; |
| 333 | const ip = &zcu.intern_pool; | 721 | const ip = &zcu.intern_pool; |
| 334 | const target = zcu.getTarget(); | 722 | const target = zcu.getTarget(); |
| 335 | | 723 | |
| ... | @@ -337,19 +725,22 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -337,19 +725,22 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 337 | const val = zcu.navValue(cg.owner_nav); | 725 | const val = zcu.navValue(cg.owner_nav); |
| 338 | const ty = val.typeOf(zcu); | 726 | const ty = val.typeOf(zcu); |
| 339 | | 727 | |
| 340 | if (!do_codegen and !ty.hasRuntimeBits(zcu)) return; | 728 | if (!do_codegen and !ty.hasRuntimeBits(zcu)) { |
| | 729 | const child_ty = if (ty.zigTypeTag(zcu) == .pointer) ty.childType(zcu) else ty; |
| | 730 | if (child_ty.zigTypeTag(zcu) != .spirv) return; |
| | 731 | } |
| 341 | | 732 | |
| 342 | const spv_decl_index = try cg.module.resolveNav(ip, cg.owner_nav); | 733 | const spv_decl_index = try cg.resolveNav(ip, cg.owner_nav); |
| 343 | const decl = cg.module.declPtr(spv_decl_index); | 734 | const decl = cg.declPtr(spv_decl_index); |
| 344 | const result_id = decl.result_id; | 735 | const result_id = decl.result_id; |
| 345 | decl.begin_dep = cg.module.decl_deps.items.len; | 736 | decl.begin_dep = cg.decl_deps.items.len; |
| 346 | | 737 | |
| 347 | switch (decl.kind) { | 738 | switch (decl.kind) { |
| 348 | .func => { | 739 | .func => { |
| 349 | if (nav.resolved.?.is_extern_decl) { | 740 | if (nav.resolved.?.is_extern_decl) { |
| 350 | _ = try cg.resolveType(ty, .direct); | 741 | _ = try cg.resolveType(ty, .direct); |
| 351 | try emitExternFnStub(cg, nav, decl, ty); | 742 | try emitExternFnStub(cg, nav, decl, ty); |
| 352 | decl.end_dep = cg.module.decl_deps.items.len; | 743 | decl.end_dep = cg.decl_deps.items.len; |
| 353 | return; | 744 | return; |
| 354 | } | 745 | } |
| 355 | | 746 | |
| ... | @@ -357,7 +748,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -357,7 +748,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 357 | const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); | 748 | const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); |
| 358 | const is_test = zcu.test_functions.contains(cg.owner_nav); | 749 | const is_test = zcu.test_functions.contains(cg.owner_nav); |
| 359 | | 750 | |
| 360 | const func_result_id = if (is_test) cg.module.allocId() else result_id; | 751 | const func_result_id = if (is_test) cg.allocId() else result_id; |
| 361 | const prototype_ty_id = try cg.resolveType(ty, .direct); | 752 | const prototype_ty_id = try cg.resolveType(ty, .direct); |
| 362 | try cg.prologue.emit(gpa, .OpFunction, .{ | 753 | try cg.prologue.emit(gpa, .OpFunction, .{ |
| 363 | .id_result_type = return_ty_id, | 754 | .id_result_type = return_ty_id, |
| ... | @@ -368,14 +759,13 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -368,14 +759,13 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 368 | .function_control = .{}, | 759 | .function_control = .{}, |
| 369 | }); | 760 | }); |
| 370 | | 761 | |
| 371 | comptime assert(zig_call_abi_ver == 3); | | |
| 372 | try cg.args.ensureUnusedCapacity(gpa, fn_info.param_types.len); | 762 | try cg.args.ensureUnusedCapacity(gpa, fn_info.param_types.len); |
| 373 | for (fn_info.param_types.get(ip)) |param_ty_index| { | 763 | for (fn_info.param_types.get(ip)) |param_ty_index| { |
| 374 | const param_ty: Type = .fromInterned(param_ty_index); | 764 | const param_ty: Type = .fromInterned(param_ty_index); |
| 375 | if (!param_ty.hasRuntimeBits(zcu)) continue; | 765 | if (!param_ty.hasRuntimeBits(zcu)) continue; |
| 376 | | 766 | |
| 377 | const param_type_id = try cg.resolveType(param_ty, .direct); | 767 | const param_type_id = try cg.resolveType(param_ty, .direct); |
| 378 | const arg_result_id = cg.module.allocId(); | 768 | const arg_result_id = cg.allocId(); |
| 379 | try cg.prologue.emit(gpa, .OpFunctionParameter, .{ | 769 | try cg.prologue.emit(gpa, .OpFunctionParameter, .{ |
| 380 | .id_result_type = param_type_id, | 770 | .id_result_type = param_type_id, |
| 381 | .id_result = arg_result_id, | 771 | .id_result = arg_result_id, |
| ... | @@ -384,7 +774,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -384,7 +774,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 384 | } | 774 | } |
| 385 | | 775 | |
| 386 | // TODO: This could probably be done in a better way... | 776 | // TODO: This could probably be done in a better way... |
| 387 | const root_block_id = cg.module.allocId(); | 777 | const root_block_id = cg.allocId(); |
| 388 | | 778 | |
| 389 | // The root block of a function declaration should appear before OpVariable instructions, | 779 | // The root block of a function declaration should appear before OpVariable instructions, |
| 390 | // so it is generated into the function's prologue. | 780 | // so it is generated into the function's prologue. |
| ... | @@ -400,26 +790,26 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -400,26 +790,26 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 400 | try cg.body.emit(gpa, .OpUnreachable, {}); | 790 | try cg.body.emit(gpa, .OpUnreachable, {}); |
| 401 | try cg.body.emit(gpa, .OpFunctionEnd, {}); | 791 | try cg.body.emit(gpa, .OpFunctionEnd, {}); |
| 402 | // Append the actual code into the functions section. | 792 | // Append the actual code into the functions section. |
| 403 | try cg.module.sections.functions.append(gpa, cg.prologue); | 793 | try cg.sections.functions.append(gpa, cg.prologue); |
| 404 | try cg.module.sections.functions.append(gpa, cg.body); | 794 | try cg.sections.functions.append(gpa, cg.body); |
| 405 | | 795 | |
| 406 | // Temporarily generate a test kernel declaration if this is a test function. | 796 | // Temporarily generate a test kernel declaration if this is a test function. |
| 407 | if (is_test) { | 797 | if (is_test) { |
| 408 | try cg.generateTestEntryPoint(nav.fqn.toSlice(ip), spv_decl_index, func_result_id); | 798 | try cg.generateTestEntryPoint(nav.fqn.toSlice(ip), spv_decl_index, func_result_id); |
| 409 | } | 799 | } |
| 410 | | 800 | |
| 411 | try cg.module.debugName(func_result_id, nav.fqn.toSlice(ip)); | 801 | try cg.debugName(func_result_id, nav.fqn.toSlice(ip)); |
| 412 | }, | 802 | }, |
| 413 | .global => { | 803 | .global => { |
| 414 | const key = ip.indexToKey(val.toIntern()).@"extern"; | 804 | const key = ip.indexToKey(val.toIntern()).@"extern"; |
| 415 | | 805 | |
| 416 | const storage_class = cg.module.storageClass(nav.resolved.?.@"addrspace"); | 806 | const storage_class = cg.storageClass(nav.resolved.?.@"addrspace"); |
| 417 | assert(storage_class != .generic); // These should be instance globals | 807 | assert(storage_class != .generic); // These should be instance globals |
| 418 | | 808 | |
| 419 | const ty_id = try cg.resolveType(ty, .indirect); | 809 | const ty_id = try cg.resolveType(ty, .indirect); |
| 420 | const ptr_ty_id = try cg.module.ptrType(ty_id, storage_class); | 810 | const ptr_ty_id = try cg.ptrType(ty_id, storage_class); |
| 421 | | 811 | |
| 422 | try cg.module.sections.globals.emit(gpa, .OpVariable, .{ | 812 | try cg.sections.globals.emit(gpa, .OpVariable, .{ |
| 423 | .id_result_type = ptr_ty_id, | 813 | .id_result_type = ptr_ty_id, |
| 424 | .id_result = result_id, | 814 | .id_result = result_id, |
| 425 | .storage_class = storage_class, | 815 | .storage_class = storage_class, |
| ... | @@ -430,11 +820,11 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -430,11 +820,11 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 430 | switch (storage_class) { | 820 | switch (storage_class) { |
| 431 | .uniform, .push_constant, .storage_buffer, .physical_storage_buffer => { | 821 | .uniform, .push_constant, .storage_buffer, .physical_storage_buffer => { |
| 432 | if (ty.zigTypeTag(zcu) == .@"struct" and storage_class != .physical_storage_buffer) { | 822 | if (ty.zigTypeTag(zcu) == .@"struct" and storage_class != .physical_storage_buffer) { |
| 433 | try cg.module.decorate(ty_id, .block); | 823 | try cg.decorate(ty_id, .block); |
| 434 | } | 824 | } |
| 435 | | 825 | |
| 436 | if (ty.hasRuntimeBits(zcu)) { | 826 | if (ty.hasRuntimeBits(zcu)) { |
| 437 | try cg.module.decorate(ptr_ty_id, .{ | 827 | try cg.decorate(ptr_ty_id, .{ |
| 438 | .array_stride = .{ .array_stride = @intCast(ty.abiSize(zcu)) }, | 828 | .array_stride = .{ .array_stride = @intCast(ty.abiSize(zcu)) }, |
| 439 | }); | 829 | }); |
| 440 | try cg.decorateLayout(ty, ty_id); | 830 | try cg.decorateLayout(ty, ty_id); |
| ... | @@ -448,23 +838,23 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -448,23 +838,23 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 448 | if (storage_class != .output and storage_class != .input and storage_class != .uniform_constant) { | 838 | if (storage_class != .output and storage_class != .input and storage_class != .uniform_constant) { |
| 449 | return cg.fail("storage class must be one of (output, input, uniform_constant) but is {s}", .{@tagName(storage_class)}); | 839 | return cg.fail("storage class must be one of (output, input, uniform_constant) but is {s}", .{@tagName(storage_class)}); |
| 450 | } | 840 | } |
| 451 | try cg.module.decorate(result_id, .{ | 841 | try cg.decorate(result_id, .{ |
| 452 | .location = .{ .location = location }, | 842 | .location = .{ .location = location }, |
| 453 | }); | 843 | }); |
| 454 | }, | 844 | }, |
| 455 | .flat => |location| { | 845 | .flat => |location| { |
| 456 | try cg.module.decorate(result_id, .{ .location = .{ .location = location } }); | 846 | try cg.decorate(result_id, .{ .location = .{ .location = location } }); |
| 457 | try cg.module.decorate(result_id, .flat); | 847 | try cg.decorate(result_id, .flat); |
| 458 | }, | 848 | }, |
| 459 | .descriptor => |descriptor| { | 849 | .descriptor => |descriptor| { |
| 460 | if (storage_class != .storage_buffer and storage_class != .uniform and storage_class != .uniform_constant) { | 850 | if (storage_class != .storage_buffer and storage_class != .uniform and storage_class != .uniform_constant) { |
| 461 | return cg.fail("storage class must be one of (storage_buffer, uniform, uniform_constant) but is {s}", .{@tagName(storage_class)}); | 851 | return cg.fail("storage class must be one of (storage_buffer, uniform, uniform_constant) but is {s}", .{@tagName(storage_class)}); |
| 462 | } | 852 | } |
| 463 | try cg.module.decorate(result_id, .{ | 853 | try cg.decorate(result_id, .{ |
| 464 | .binding = .{ .binding_point = descriptor.binding }, | 854 | .binding = .{ .binding_point = descriptor.binding }, |
| 465 | }); | 855 | }); |
| 466 | | 856 | |
| 467 | try cg.module.decorate(result_id, .{ | 857 | try cg.decorate(result_id, .{ |
| 468 | .descriptor_set = .{ .descriptor_set = descriptor.set }, | 858 | .descriptor_set = .{ .descriptor_set = descriptor.set }, |
| 469 | }); | 859 | }); |
| 470 | }, | 860 | }, |
| ... | @@ -474,10 +864,10 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -474,10 +864,10 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 474 | } | 864 | } |
| 475 | | 865 | |
| 476 | if (std.meta.stringToEnum(spec.BuiltIn, nav.fqn.toSlice(ip))) |built_in| { | 866 | if (std.meta.stringToEnum(spec.BuiltIn, nav.fqn.toSlice(ip))) |built_in| { |
| 477 | try cg.module.decorate(result_id, .{ .built_in = .{ .built_in = built_in } }); | 867 | try cg.decorate(result_id, .{ .built_in = .{ .built_in = built_in } }); |
| 478 | } | 868 | } |
| 479 | | 869 | |
| 480 | try cg.module.debugName(result_id, nav.fqn.toSlice(ip)); | 870 | try cg.debugName(result_id, nav.fqn.toSlice(ip)); |
| 481 | }, | 871 | }, |
| 482 | .invocation_global => { | 872 | .invocation_global => { |
| 483 | // `@extern()` produces an invocation_global whose value is a | 873 | // `@extern()` produces an invocation_global whose value is a |
| ... | @@ -488,18 +878,18 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -488,18 +878,18 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 488 | if (ptr_key.base_addr != .nav or ptr_key.byte_offset != 0) break :alias; | 878 | if (ptr_key.base_addr != .nav or ptr_key.byte_offset != 0) break :alias; |
| 489 | const underlying_nav = ip.getNav(ptr_key.base_addr.nav); | 879 | const underlying_nav = ip.getNav(ptr_key.base_addr.nav); |
| 490 | if (!underlying_nav.resolved.?.is_extern_decl) break :alias; | 880 | if (!underlying_nav.resolved.?.is_extern_decl) break :alias; |
| 491 | cg.module.declPtr(spv_decl_index).end_dep = cg.module.decl_deps.items.len; | 881 | cg.declPtr(spv_decl_index).end_dep = cg.decl_deps.items.len; |
| 492 | return; | 882 | return; |
| 493 | } | 883 | } |
| 494 | | 884 | |
| 495 | const ty_id = try cg.resolveType(ty, .indirect); | 885 | const ty_id = try cg.resolveType(ty, .indirect); |
| 496 | const ptr_ty_id = try cg.module.ptrType(ty_id, .function); | 886 | const ptr_ty_id = try cg.ptrType(ty_id, .function); |
| 497 | | 887 | |
| 498 | // TODO: Combine with resolveAnonDecl? | 888 | // TODO: Combine with resolveAnonDecl? |
| 499 | const void_ty_id = try cg.resolveType(.void, .direct); | 889 | const void_ty_id = try cg.resolveType(.void, .direct); |
| 500 | const initializer_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); | 890 | const initializer_proto_ty_id = try cg.functionType(void_ty_id, &.{}); |
| 501 | | 891 | |
| 502 | const initializer_id = cg.module.allocId(); | 892 | const initializer_id = cg.allocId(); |
| 503 | try cg.prologue.emit(gpa, .OpFunction, .{ | 893 | try cg.prologue.emit(gpa, .OpFunction, .{ |
| 504 | .id_result_type = try cg.resolveType(.void, .direct), | 894 | .id_result_type = try cg.resolveType(.void, .direct), |
| 505 | .id_result = initializer_id, | 895 | .id_result = initializer_id, |
| ... | @@ -507,7 +897,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -507,7 +897,7 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 507 | .function_type = initializer_proto_ty_id, | 897 | .function_type = initializer_proto_ty_id, |
| 508 | }); | 898 | }); |
| 509 | | 899 | |
| 510 | const root_block_id = cg.module.allocId(); | 900 | const root_block_id = cg.allocId(); |
| 511 | try cg.prologue.emit(gpa, .OpLabel, .{ | 901 | try cg.prologue.emit(gpa, .OpLabel, .{ |
| 512 | .id_result = root_block_id, | 902 | .id_result = root_block_id, |
| 513 | }); | 903 | }); |
| ... | @@ -521,33 +911,33 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { | ... | @@ -521,33 +911,33 @@ pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { |
| 521 | | 911 | |
| 522 | try cg.body.emit(gpa, .OpReturn, {}); | 912 | try cg.body.emit(gpa, .OpReturn, {}); |
| 523 | try cg.body.emit(gpa, .OpFunctionEnd, {}); | 913 | try cg.body.emit(gpa, .OpFunctionEnd, {}); |
| 524 | try cg.module.sections.functions.append(gpa, cg.prologue); | 914 | try cg.sections.functions.append(gpa, cg.prologue); |
| 525 | try cg.module.sections.functions.append(gpa, cg.body); | 915 | try cg.sections.functions.append(gpa, cg.body); |
| 526 | | 916 | |
| 527 | try cg.module.debugNameFmt(initializer_id, "initializer of {f}", .{nav.fqn.fmt(ip)}); | 917 | try cg.debugNameFmt(initializer_id, "initializer of {f}", .{nav.fqn.fmt(ip)}); |
| 528 | try cg.module.debugName(result_id, nav.fqn.toSlice(ip)); | 918 | try cg.debugName(result_id, nav.fqn.toSlice(ip)); |
| 529 | | 919 | |
| 530 | try cg.module.sections.globals.emit(gpa, .OpExtInst, .{ | 920 | try cg.sections.globals.emit(gpa, .OpExtInst, .{ |
| 531 | .id_result_type = ptr_ty_id, | 921 | .id_result_type = ptr_ty_id, |
| 532 | .id_result = result_id, | 922 | .id_result = result_id, |
| 533 | .set = try cg.module.importInstructionSet(.zig), | 923 | .set = try cg.importInstructionSet(.zig), |
| 534 | .instruction = .{ .inst = @intFromEnum(spec.Zig.InvocationGlobal) }, | 924 | .instruction = .{ .inst = @intFromEnum(spec.Zig.InvocationGlobal) }, |
| 535 | .id_ref_4 = &.{initializer_id}, | 925 | .id_ref_4 = &.{initializer_id}, |
| 536 | }); | 926 | }); |
| 537 | }, | 927 | }, |
| 538 | } | 928 | } |
| 539 | | 929 | |
| 540 | cg.module.declPtr(spv_decl_index).end_dep = cg.module.decl_deps.items.len; | 930 | cg.declPtr(spv_decl_index).end_dep = cg.decl_deps.items.len; |
| 541 | } | 931 | } |
| 542 | | 932 | |
| 543 | fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { | 933 | fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { |
| 544 | const zcu = cg.module.zcu; | 934 | const zcu = cg.zcu; |
| 545 | const ip = &zcu.intern_pool; | 935 | const ip = &zcu.intern_pool; |
| 546 | switch (ty.zigTypeTag(zcu)) { | 936 | switch (ty.zigTypeTag(zcu)) { |
| 547 | .array => { | 937 | .array => { |
| 548 | const elem_ty = ty.childType(zcu); | 938 | const elem_ty = ty.childType(zcu); |
| 549 | if (!elem_ty.hasRuntimeBits(zcu)) return; | 939 | if (!elem_ty.hasRuntimeBits(zcu)) return; |
| 550 | try cg.module.decorate(ty_id, .{ | 940 | try cg.decorate(ty_id, .{ |
| 551 | .array_stride = .{ .array_stride = @intCast(elem_ty.abiSize(zcu)) }, | 941 | .array_stride = .{ .array_stride = @intCast(elem_ty.abiSize(zcu)) }, |
| 552 | }); | 942 | }); |
| 553 | try cg.decorateLayout(elem_ty, try cg.resolveType(elem_ty, .indirect)); | 943 | try cg.decorateLayout(elem_ty, try cg.resolveType(elem_ty, .indirect)); |
| ... | @@ -556,7 +946,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { | ... | @@ -556,7 +946,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { |
| 556 | const elem_ty = ty.childType(zcu); | 946 | const elem_ty = ty.childType(zcu); |
| 557 | try cg.decorateLayout(elem_ty, try cg.resolveType(elem_ty, .indirect)); | 947 | try cg.decorateLayout(elem_ty, try cg.resolveType(elem_ty, .indirect)); |
| 558 | if (cg.isSpvVector(ty)) return; | 948 | if (cg.isSpvVector(ty)) return; |
| 559 | try cg.module.decorate(ty_id, .{ | 949 | try cg.decorate(ty_id, .{ |
| 560 | .array_stride = .{ .array_stride = @intCast(elem_ty.abiSize(zcu)) }, | 950 | .array_stride = .{ .array_stride = @intCast(elem_ty.abiSize(zcu)) }, |
| 561 | }); | 951 | }); |
| 562 | }, | 952 | }, |
| ... | @@ -570,7 +960,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { | ... | @@ -570,7 +960,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { |
| 570 | const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]); | 960 | const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 571 | if (!field_ty.hasRuntimeBits(zcu)) continue; | 961 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 572 | const offset: u32 = @intCast(ty.structFieldOffset(field_index, zcu)); | 962 | const offset: u32 = @intCast(ty.structFieldOffset(field_index, zcu)); |
| 573 | try cg.module.decorateMember(ty_id, member, .{ .offset = .{ .byte_offset = offset } }); | 963 | try cg.decorateMember(ty_id, member, .{ .offset = .{ .byte_offset = offset } }); |
| 574 | try cg.decorateLayout(field_ty, try cg.resolveType(field_ty, .indirect)); | 964 | try cg.decorateLayout(field_ty, try cg.resolveType(field_ty, .indirect)); |
| 575 | member += 1; | 965 | member += 1; |
| 576 | } | 966 | } |
| ... | @@ -598,13 +988,13 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { | ... | @@ -598,13 +988,13 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { |
| 598 | const u8_id = try cg.resolveType(.u8, .direct); | 988 | const u8_id = try cg.resolveType(.u8, .direct); |
| 599 | if (layout.payload_padding_size != 0) { | 989 | if (layout.payload_padding_size != 0) { |
| 600 | const len_id = try cg.constInt(.u32, layout.payload_padding_size); | 990 | const len_id = try cg.constInt(.u32, layout.payload_padding_size); |
| 601 | const arr_id = try cg.module.arrayType(len_id, u8_id); | 991 | const arr_id = try cg.arrayType(len_id, u8_id); |
| 602 | try cg.module.decorate(arr_id, .{ .array_stride = .{ .array_stride = 1 } }); | 992 | try cg.decorate(arr_id, .{ .array_stride = .{ .array_stride = 1 } }); |
| 603 | } | 993 | } |
| 604 | if (layout.padding_size != 0) { | 994 | if (layout.padding_size != 0) { |
| 605 | const len_id = try cg.constInt(.u32, layout.padding_size); | 995 | const len_id = try cg.constInt(.u32, layout.padding_size); |
| 606 | const arr_id = try cg.module.arrayType(len_id, u8_id); | 996 | const arr_id = try cg.arrayType(len_id, u8_id); |
| 607 | try cg.module.decorate(arr_id, .{ .array_stride = .{ .array_stride = 1 } }); | 997 | try cg.decorate(arr_id, .{ .array_stride = .{ .array_stride = 1 } }); |
| 608 | } | 998 | } |
| 609 | }, | 999 | }, |
| 610 | .optional => { | 1000 | .optional => { |
| ... | @@ -621,7 +1011,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { | ... | @@ -621,7 +1011,7 @@ fn decorateLayout(cg: *CodeGen, ty: Type, ty_id: spec.Id) Error!void { |
| 621 | | 1011 | |
| 622 | pub fn fail(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { | 1012 | pub fn fail(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { |
| 623 | @branchHint(.cold); | 1013 | @branchHint(.cold); |
| 624 | return cg.module.zcu.codegenFail(cg.owner_nav, format, args); | 1014 | return cg.zcu.codegenFail(cg.owner_nav, format, args); |
| 625 | } | 1015 | } |
| 626 | | 1016 | |
| 627 | pub fn todo(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { | 1017 | pub fn todo(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { |
| ... | @@ -631,17 +1021,17 @@ pub fn todo(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { | ... | @@ -631,17 +1021,17 @@ pub fn todo(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { |
| 631 | /// This imports the "default" extended instruction set for the target | 1021 | /// This imports the "default" extended instruction set for the target |
| 632 | /// For OpenCL, OpenCL.std.100. For Vulkan and OpenGL, GLSL.std.450. | 1022 | /// For OpenCL, OpenCL.std.100. For Vulkan and OpenGL, GLSL.std.450. |
| 633 | fn importExtendedSet(cg: *CodeGen) !Id { | 1023 | fn importExtendedSet(cg: *CodeGen) !Id { |
| 634 | const target = cg.module.zcu.getTarget(); | 1024 | const target = cg.zcu.getTarget(); |
| 635 | return switch (target.os.tag) { | 1025 | return switch (target.os.tag) { |
| 636 | .opencl, .amdhsa => try cg.module.importInstructionSet(.@"OpenCL.std"), | 1026 | .opencl, .amdhsa => try cg.importInstructionSet(.@"OpenCL.std"), |
| 637 | .vulkan, .opengl => try cg.module.importInstructionSet(.@"GLSL.std.450"), | 1027 | .vulkan, .opengl => try cg.importInstructionSet(.@"GLSL.std.450"), |
| 638 | else => unreachable, | 1028 | else => unreachable, |
| 639 | }; | 1029 | }; |
| 640 | } | 1030 | } |
| 641 | | 1031 | |
| 642 | /// Fetch the result-id for a previously generated instruction or constant. | 1032 | /// Fetch the result-id for a previously generated instruction or constant. |
| 643 | fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { | 1033 | fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { |
| 644 | const zcu = cg.module.zcu; | 1034 | const zcu = cg.zcu; |
| 645 | const ip = &zcu.intern_pool; | 1035 | const ip = &zcu.intern_pool; |
| 646 | if (inst.toInterned()) |val_ip_index| { | 1036 | if (inst.toInterned()) |val_ip_index| { |
| 647 | const ty = cg.typeOf(inst); | 1037 | const ty = cg.typeOf(inst); |
| ... | @@ -652,9 +1042,9 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { | ... | @@ -652,9 +1042,9 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { |
| 652 | .func => |func| func.owner_nav, | 1042 | .func => |func| func.owner_nav, |
| 653 | else => unreachable, | 1043 | else => unreachable, |
| 654 | }; | 1044 | }; |
| 655 | const spv_decl_index = try cg.module.resolveNav(ip, fn_nav); | 1045 | const spv_decl_index = try cg.resolveNav(ip, fn_nav); |
| 656 | try cg.module.decl_deps.append(cg.module.gpa, spv_decl_index); | 1046 | try cg.decl_deps.append(cg.gpa, spv_decl_index); |
| 657 | const decl = cg.module.declPtr(spv_decl_index); | 1047 | const decl = cg.declPtr(spv_decl_index); |
| 658 | if (val_key == .@"extern") { | 1048 | if (val_key == .@"extern") { |
| 659 | const nav = ip.getNav(fn_nav); | 1049 | const nav = ip.getNav(fn_nav); |
| 660 | const nav_ty: Type = .fromInterned(nav.resolved.?.type); | 1050 | const nav_ty: Type = .fromInterned(nav.resolved.?.type); |
| ... | @@ -670,22 +1060,22 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { | ... | @@ -670,22 +1060,22 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { |
| 670 | } | 1060 | } |
| 671 | | 1061 | |
| 672 | fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { | 1062 | fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { |
| 673 | const gpa = cg.module.gpa; | 1063 | const gpa = cg.gpa; |
| 674 | | 1064 | |
| 675 | // TODO: This cannot be a function at this point, but it should probably be handled anyway. | 1065 | // TODO: This cannot be a function at this point, but it should probably be handled anyway. |
| 676 | | 1066 | |
| 677 | const zcu = cg.module.zcu; | 1067 | const zcu = cg.zcu; |
| 678 | const ty: Type = .fromInterned(zcu.intern_pool.typeOf(val)); | 1068 | const ty: Type = .fromInterned(zcu.intern_pool.typeOf(val)); |
| 679 | const ty_id = try cg.resolveType(ty, .indirect); | 1069 | const ty_id = try cg.resolveType(ty, .indirect); |
| 680 | | 1070 | |
| 681 | const spv_decl_index = blk: { | 1071 | const spv_decl_index = blk: { |
| 682 | const entry = try cg.module.uav_link.getOrPut(gpa, .{ val, .function }); | 1072 | const entry = try cg.uav_link.getOrPut(gpa, .{ val, .function }); |
| 683 | if (entry.found_existing) { | 1073 | if (entry.found_existing) { |
| 684 | try cg.addFunctionDep(entry.value_ptr.*, .function); | 1074 | try cg.addFunctionDep(entry.value_ptr.*, .function); |
| 685 | return cg.module.declPtr(entry.value_ptr.*).result_id; | 1075 | return cg.declPtr(entry.value_ptr.*).result_id; |
| 686 | } | 1076 | } |
| 687 | | 1077 | |
| 688 | const spv_decl_index = try cg.module.allocDecl(.invocation_global); | 1078 | const spv_decl_index = try cg.allocDecl(.invocation_global); |
| 689 | try cg.addFunctionDep(spv_decl_index, .function); | 1079 | try cg.addFunctionDep(spv_decl_index, .function); |
| 690 | entry.value_ptr.* = spv_decl_index; | 1080 | entry.value_ptr.* = spv_decl_index; |
| 691 | break :blk spv_decl_index; | 1081 | break :blk spv_decl_index; |
| ... | @@ -697,7 +1087,7 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { | ... | @@ -697,7 +1087,7 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { |
| 697 | // constant lowering of this value will need to be deferred to an initializer similar to | 1087 | // constant lowering of this value will need to be deferred to an initializer similar to |
| 698 | // other globals. | 1088 | // other globals. |
| 699 | | 1089 | |
| 700 | const result_id = cg.module.declPtr(spv_decl_index).result_id; | 1090 | const result_id = cg.declPtr(spv_decl_index).result_id; |
| 701 | | 1091 | |
| 702 | { | 1092 | { |
| 703 | // Save the current state so that we can temporarily generate into a different function. | 1093 | // Save the current state so that we can temporarily generate into a different function. |
| ... | @@ -719,16 +1109,16 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { | ... | @@ -719,16 +1109,16 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { |
| 719 | } | 1109 | } |
| 720 | | 1110 | |
| 721 | const void_ty_id = try cg.resolveType(.void, .direct); | 1111 | const void_ty_id = try cg.resolveType(.void, .direct); |
| 722 | const initializer_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); | 1112 | const initializer_proto_ty_id = try cg.functionType(void_ty_id, &.{}); |
| 723 | | 1113 | |
| 724 | const initializer_id = cg.module.allocId(); | 1114 | const initializer_id = cg.allocId(); |
| 725 | try cg.prologue.emit(gpa, .OpFunction, .{ | 1115 | try cg.prologue.emit(gpa, .OpFunction, .{ |
| 726 | .id_result_type = try cg.resolveType(.void, .direct), | 1116 | .id_result_type = try cg.resolveType(.void, .direct), |
| 727 | .id_result = initializer_id, | 1117 | .id_result = initializer_id, |
| 728 | .function_control = .{}, | 1118 | .function_control = .{}, |
| 729 | .function_type = initializer_proto_ty_id, | 1119 | .function_type = initializer_proto_ty_id, |
| 730 | }); | 1120 | }); |
| 731 | const root_block_id = cg.module.allocId(); | 1121 | const root_block_id = cg.allocId(); |
| 732 | try cg.prologue.emit(gpa, .OpLabel, .{ | 1122 | try cg.prologue.emit(gpa, .OpLabel, .{ |
| 733 | .id_result = root_block_id, | 1123 | .id_result = root_block_id, |
| 734 | }); | 1124 | }); |
| ... | @@ -743,16 +1133,16 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { | ... | @@ -743,16 +1133,16 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { |
| 743 | try cg.body.emit(gpa, .OpReturn, {}); | 1133 | try cg.body.emit(gpa, .OpReturn, {}); |
| 744 | try cg.body.emit(gpa, .OpFunctionEnd, {}); | 1134 | try cg.body.emit(gpa, .OpFunctionEnd, {}); |
| 745 | | 1135 | |
| 746 | try cg.module.sections.functions.append(gpa, cg.prologue); | 1136 | try cg.sections.functions.append(gpa, cg.prologue); |
| 747 | try cg.module.sections.functions.append(gpa, cg.body); | 1137 | try cg.sections.functions.append(gpa, cg.body); |
| 748 | | 1138 | |
| 749 | try cg.module.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); | 1139 | try cg.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); |
| 750 | | 1140 | |
| 751 | const fn_decl_ptr_ty_id = try cg.module.ptrType(ty_id, .function); | 1141 | const fn_decl_ptr_ty_id = try cg.ptrType(ty_id, .function); |
| 752 | try cg.module.sections.globals.emit(gpa, .OpExtInst, .{ | 1142 | try cg.sections.globals.emit(gpa, .OpExtInst, .{ |
| 753 | .id_result_type = fn_decl_ptr_ty_id, | 1143 | .id_result_type = fn_decl_ptr_ty_id, |
| 754 | .id_result = result_id, | 1144 | .id_result = result_id, |
| 755 | .set = try cg.module.importInstructionSet(.zig), | 1145 | .set = try cg.importInstructionSet(.zig), |
| 756 | .instruction = .{ .inst = @intFromEnum(spec.Zig.InvocationGlobal) }, | 1146 | .instruction = .{ .inst = @intFromEnum(spec.Zig.InvocationGlobal) }, |
| 757 | .id_ref_4 = &.{initializer_id}, | 1147 | .id_ref_4 = &.{initializer_id}, |
| 758 | }); | 1148 | }); |
| ... | @@ -761,15 +1151,21 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { | ... | @@ -761,15 +1151,21 @@ fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { |
| 761 | return result_id; | 1151 | return result_id; |
| 762 | } | 1152 | } |
| 763 | | 1153 | |
| 764 | fn addFunctionDep(cg: *CodeGen, decl_index: Module.Decl.Index, storage_class: StorageClass) !void { | 1154 | fn resolvePtr(cg: *CodeGen, ref: Air.Inst.Ref) !Ptr { |
| 765 | const gpa = cg.module.gpa; | 1155 | const id = try cg.resolve(ref); |
| 766 | const target = cg.module.zcu.getTarget(); | 1156 | if (cg.tracked_allocas.getPtr(id)) |slot| return .{ .tracked = .{ .id = id, .slot = slot } }; |
| | 1157 | return .{ .id = id }; |
| | 1158 | } |
| | 1159 | |
| | 1160 | fn addFunctionDep(cg: *CodeGen, decl_index: Decl.Index, storage_class: StorageClass) !void { |
| | 1161 | const gpa = cg.gpa; |
| | 1162 | const target = cg.zcu.getTarget(); |
| 767 | if (target.cpu.has(.spirv, .v1_4)) { | 1163 | if (target.cpu.has(.spirv, .v1_4)) { |
| 768 | try cg.module.decl_deps.append(gpa, decl_index); | 1164 | try cg.decl_deps.append(gpa, decl_index); |
| 769 | } else { | 1165 | } else { |
| 770 | // Before version 1.4, the interface’s storage classes are limited to the Input and Output | 1166 | // Before version 1.4, the interface’s storage classes are limited to the Input and Output |
| 771 | if (storage_class == .input or storage_class == .output) { | 1167 | if (storage_class == .input or storage_class == .output) { |
| 772 | try cg.module.decl_deps.append(gpa, decl_index); | 1168 | try cg.decl_deps.append(gpa, decl_index); |
| 773 | } | 1169 | } |
| 774 | } | 1170 | } |
| 775 | } | 1171 | } |
| ... | @@ -779,25 +1175,11 @@ fn addFunctionDep(cg: *CodeGen, decl_index: Module.Decl.Index, storage_class: St | ... | @@ -779,25 +1175,11 @@ fn addFunctionDep(cg: *CodeGen, decl_index: Module.Decl.Index, storage_class: St |
| 779 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to | 1175 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to |
| 780 | /// keep track of the previous block. | 1176 | /// keep track of the previous block. |
| 781 | fn beginSpvBlock(cg: *CodeGen, label: Id) !void { | 1177 | fn beginSpvBlock(cg: *CodeGen, label: Id) !void { |
| 782 | try cg.body.emit(cg.module.gpa, .OpLabel, .{ .id_result = label }); | 1178 | try cg.body.emit(cg.gpa, .OpLabel, .{ .id_result = label }); |
| 783 | cg.block_label = label; | 1179 | cg.block_label = label; |
| 784 | cg.block_terminated = false; | 1180 | cg.block_terminated = false; |
| 785 | } | 1181 | } |
| 786 | | 1182 | |
| 787 | /// Return the amount of bits in the largest supported integer type. This is either 32 (always supported), or 64 (if | | |
| 788 | /// the Int64 capability is enabled). | | |
| 789 | /// Note: The extension SPV_INTEL_arbitrary_precision_integers allows any integer size (at least up to 32 bits). | | |
| 790 | /// In theory that could also be used, but since the spec says that it only guarantees support up to 32-bit ints there | | |
| 791 | /// is no way of knowing whether those are actually supported. | | |
| 792 | /// TODO: Maybe this should be cached? | | |
| 793 | fn largestSupportedIntBits(cg: *CodeGen) u16 { | | |
| 794 | const target = cg.module.zcu.getTarget(); | | |
| 795 | if (target.cpu.has(.spirv, .int64) or target.cpu.arch == .spirv64) { | | |
| 796 | return 64; | | |
| 797 | } | | |
| 798 | return 32; | | |
| 799 | } | | |
| 800 | | | |
| 801 | const ArithmeticTypeInfo = struct { | 1183 | const ArithmeticTypeInfo = struct { |
| 802 | const Class = enum { | 1184 | const Class = enum { |
| 803 | bool, | 1185 | bool, |
| ... | @@ -835,8 +1217,8 @@ const ArithmeticTypeInfo = struct { | ... | @@ -835,8 +1217,8 @@ const ArithmeticTypeInfo = struct { |
| 835 | }; | 1217 | }; |
| 836 | | 1218 | |
| 837 | fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { | 1219 | fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { |
| 838 | const zcu = cg.module.zcu; | 1220 | const zcu = cg.zcu; |
| 839 | const target = cg.module.zcu.getTarget(); | 1221 | const target = cg.zcu.getTarget(); |
| 840 | var scalar_ty = ty.scalarType(zcu); | 1222 | var scalar_ty = ty.scalarType(zcu); |
| 841 | if (scalar_ty.zigTypeTag(zcu) == .@"enum") { | 1223 | if (scalar_ty.zigTypeTag(zcu) == .@"enum") { |
| 842 | scalar_ty = scalar_ty.intTagType(zcu); | 1224 | scalar_ty = scalar_ty.intTagType(zcu); |
| ... | @@ -845,7 +1227,7 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { | ... | @@ -845,7 +1227,7 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { |
| 845 | return switch (scalar_ty.zigTypeTag(zcu)) { | 1227 | return switch (scalar_ty.zigTypeTag(zcu)) { |
| 846 | .bool => .{ | 1228 | .bool => .{ |
| 847 | .bits = 1, // Doesn't matter for this class. | 1229 | .bits = 1, // Doesn't matter for this class. |
| 848 | .backing_bits = cg.module.backingIntBits(1).@"0", | 1230 | .backing_bits = cg.backingIntBits(1).@"0", |
| 849 | .vector_len = vector_len, | 1231 | .vector_len = vector_len, |
| 850 | .signedness = .unsigned, // Technically, but doesn't matter for this class. | 1232 | .signedness = .unsigned, // Technically, but doesn't matter for this class. |
| 851 | .class = .bool, | 1233 | .class = .bool, |
| ... | @@ -860,7 +1242,7 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { | ... | @@ -860,7 +1242,7 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { |
| 860 | .int => blk: { | 1242 | .int => blk: { |
| 861 | const int_info = scalar_ty.intInfo(zcu); | 1243 | const int_info = scalar_ty.intInfo(zcu); |
| 862 | // TODO: Maybe it's useful to also return this value. | 1244 | // TODO: Maybe it's useful to also return this value. |
| 863 | const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); | 1245 | const backing_bits, const big_int = cg.backingIntBits(int_info.bits); |
| 864 | break :blk .{ | 1246 | break :blk .{ |
| 865 | .bits = int_info.bits, | 1247 | .bits = int_info.bits, |
| 866 | .backing_bits = backing_bits, | 1248 | .backing_bits = backing_bits, |
| ... | @@ -880,8 +1262,8 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { | ... | @@ -880,8 +1262,8 @@ fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { |
| 880 | | 1262 | |
| 881 | /// Checks whether the type can be directly translated to SPIR-V vectors | 1263 | /// Checks whether the type can be directly translated to SPIR-V vectors |
| 882 | fn isSpvVector(cg: *CodeGen, ty: Type) bool { | 1264 | fn isSpvVector(cg: *CodeGen, ty: Type) bool { |
| 883 | const zcu = cg.module.zcu; | 1265 | const zcu = cg.zcu; |
| 884 | const target = cg.module.zcu.getTarget(); | 1266 | const target = cg.zcu.getTarget(); |
| 885 | if (ty.zigTypeTag(zcu) != .vector) return false; | 1267 | if (ty.zigTypeTag(zcu) != .vector) return false; |
| 886 | | 1268 | |
| 887 | // TODO: This check must be expanded for types that can be represented | 1269 | // TODO: This check must be expanded for types that can be represented |
| ... | @@ -909,23 +1291,34 @@ fn isSpvVector(cg: *CodeGen, ty: Type) bool { | ... | @@ -909,23 +1291,34 @@ fn isSpvVector(cg: *CodeGen, ty: Type) bool { |
| 909 | | 1291 | |
| 910 | /// Emits a bool constant in a particular representation. | 1292 | /// Emits a bool constant in a particular representation. |
| 911 | fn constBool(cg: *CodeGen, value: bool, repr: Repr) !Id { | 1293 | fn constBool(cg: *CodeGen, value: bool, repr: Repr) !Id { |
| 912 | return switch (repr) { | 1294 | switch (repr) { |
| 913 | .indirect => cg.constInt(.u1, @intFromBool(value)), | 1295 | .indirect => return cg.constInt(.u1, @intFromBool(value)), |
| 914 | .direct => cg.module.constBool(value), | 1296 | .direct => { |
| 915 | }; | 1297 | const result_ty_id = try cg.boolType(); |
| | 1298 | const result_id = cg.allocId(); |
| | 1299 | switch (value) { |
| | 1300 | inline else => |value_ct| try cg.sections.globals.emit( |
| | 1301 | cg.gpa, |
| | 1302 | if (value_ct) .OpConstantTrue else .OpConstantFalse, |
| | 1303 | .{ .id_result_type = result_ty_id, .id_result = result_id }, |
| | 1304 | ), |
| | 1305 | } |
| | 1306 | return result_id; |
| | 1307 | }, |
| | 1308 | } |
| 916 | } | 1309 | } |
| 917 | | 1310 | |
| 918 | /// Emits an integer constant. | 1311 | /// Emits an integer constant. |
| 919 | /// This function, unlike Module.constInt, takes care to bitcast | 1312 | /// This function, unlike cg.constInt, takes care to bitcast |
| 920 | /// the value to an unsigned int first for Kernels. | 1313 | /// the value to an unsigned int first for Kernels. |
| 921 | fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { | 1314 | fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { |
| 922 | const gpa = cg.module.gpa; | 1315 | const gpa = cg.gpa; |
| 923 | const zcu = cg.module.zcu; | 1316 | const zcu = cg.zcu; |
| 924 | const target = cg.module.zcu.getTarget(); | 1317 | const target = cg.zcu.getTarget(); |
| 925 | const scalar_ty = ty.scalarType(zcu); | 1318 | const scalar_ty = ty.scalarType(zcu); |
| 926 | const int_info = scalar_ty.intInfo(zcu); | 1319 | const int_info = scalar_ty.intInfo(zcu); |
| 927 | // Use backing bits so that negatives are sign extended | 1320 | // Use backing bits so that negatives are sign extended |
| 928 | const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); | 1321 | const backing_bits, const big_int = cg.backingIntBits(int_info.bits); |
| 929 | assert(backing_bits != 0); // u0 is comptime | 1322 | assert(backing_bits != 0); // u0 is comptime |
| 930 | | 1323 | |
| 931 | const result_ty_id = try cg.resolveType(scalar_ty, .indirect); | 1324 | const result_ty_id = try cg.resolveType(scalar_ty, .indirect); |
| ... | @@ -939,7 +1332,7 @@ fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { | ... | @@ -939,7 +1332,7 @@ fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { |
| 939 | .signed => @bitCast(@as(i64, @intCast(value))), | 1332 | .signed => @bitCast(@as(i64, @intCast(value))), |
| 940 | .unsigned => @as(u64, @intCast(value)), | 1333 | .unsigned => @as(u64, @intCast(value)), |
| 941 | }; | 1334 | }; |
| 942 | const n_limbs = backing_bits / Module.big_int_bits; | 1335 | const n_limbs = backing_bits / big_int_bits; |
| 943 | const fill: u32 = if (signedness == .signed and value < 0) 0xFFFFFFFF else 0; | 1336 | const fill: u32 = if (signedness == .signed and value < 0) 0xFFFFFFFF else 0; |
| 944 | const scratch_top = cg.id_scratch.items.len; | 1337 | const scratch_top = cg.id_scratch.items.len; |
| 945 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 1338 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| ... | @@ -979,61 +1372,45 @@ fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { | ... | @@ -979,61 +1372,45 @@ fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { |
| 979 | }, | 1372 | }, |
| 980 | }; | 1373 | }; |
| 981 | | 1374 | |
| 982 | const result_id = try cg.module.constant(result_ty_id, final_value); | 1375 | const result_id = cg.allocId(); |
| | 1376 | try cg.sections.globals.emit(cg.gpa, .OpConstant, .{ |
| | 1377 | .id_result_type = result_ty_id, |
| | 1378 | .id_result = result_id, |
| | 1379 | .value = final_value, |
| | 1380 | }); |
| 983 | | 1381 | |
| 984 | if (!ty.isVector(zcu)) return result_id; | 1382 | if (!ty.isVector(zcu)) return result_id; |
| 985 | return cg.constructCompositeSplat(ty, result_id); | 1383 | return cg.constructCompositeSplat(ty, result_id); |
| 986 | } | 1384 | } |
| 987 | | 1385 | |
| 988 | fn constIntBig(cg: *CodeGen, ty: Type, val: Value) !Id { | | |
| 989 | const gpa = cg.module.gpa; | | |
| 990 | const zcu = cg.module.zcu; | | |
| 991 | const int_info = ty.intInfo(zcu); | | |
| 992 | const backing_bits, _ = cg.module.backingIntBits(int_info.bits); | | |
| 993 | const n_limbs = backing_bits / Module.big_int_bits; | | |
| 994 | const result_ty_id = try cg.resolveType(ty, .indirect); | | |
| 995 | | | |
| 996 | var bigint_space: Value.BigIntSpace = undefined; | | |
| 997 | const bigint = val.toBigInt(&bigint_space, zcu); | | |
| 998 | | | |
| 999 | const limb_values = try gpa.alloc(u32, n_limbs); | | |
| 1000 | defer gpa.free(limb_values); | | |
| 1001 | | | |
| 1002 | const bytes = std.mem.sliceAsBytes(limb_values); | | |
| 1003 | bigint.writeTwosComplement(bytes, .little); | | |
| 1004 | if (builtin.cpu.arch.endian() == .big) { | | |
| 1005 | for (limb_values) |*limb| limb.* = @byteSwap(limb.*); | | |
| 1006 | } | | |
| 1007 | | | |
| 1008 | const scratch_top = cg.id_scratch.items.len; | | |
| 1009 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | | |
| 1010 | const constituents = try cg.id_scratch.addManyAsSlice(gpa, n_limbs); | | |
| 1011 | for (constituents, 0..) |*c, i| { | | |
| 1012 | c.* = try cg.constInt(.u32, limb_values[i]); | | |
| 1013 | } | | |
| 1014 | return cg.constructComposite(result_ty_id, constituents); | | |
| 1015 | } | | |
| 1016 | | | |
| 1017 | /// Construct a composite value from its constituents. | 1386 | /// Construct a composite value from its constituents. |
| 1018 | /// In logical addressing mode (Vulkan/OpenGL), OpCompositeConstruct cannot accept | 1387 | /// In logical addressing mode (Vulkan/OpenGL), OpCompositeConstruct cannot accept |
| 1019 | /// pointer operands, so for struct types we use alloc, store for each field and load instead. | 1388 | /// pointer operands, so for struct types we use alloc, store for each field and load instead. |
| 1020 | pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const Id) !Id { | 1389 | pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const Id) !Id { |
| 1021 | const gpa = cg.module.gpa; | 1390 | const gpa = cg.gpa; |
| 1022 | | 1391 | |
| 1023 | if (cg.module.structFields(result_ty_id)) |fields| { | 1392 | const maybe_fields: ?[]const Id = for (cg.struct_types.keys(), cg.struct_types.values()) |key, val| { |
| | 1393 | if (val == result_ty_id) break key.fields; |
| | 1394 | } else null; |
| | 1395 | if (maybe_fields) |fields| { |
| 1024 | assert(fields.len == constituents.len); | 1396 | assert(fields.len == constituents.len); |
| 1025 | const u32_ty_id = try cg.module.intType(.unsigned, 32); | 1397 | const u32_ty_id = try cg.intType(.unsigned, 32); |
| 1026 | const var_id = try cg.alloc(result_ty_id, null); | 1398 | const var_id = try cg.alloc(result_ty_id, null); |
| 1027 | for (fields, constituents, 0..) |field_ty_id, constituent, i| { | 1399 | for (fields, constituents, 0..) |field_ty_id, constituent, i| { |
| 1028 | const field_ptr_ty_id = try cg.module.ptrType(field_ty_id, .function); | 1400 | const field_ptr_ty_id = try cg.ptrType(field_ty_id, .function); |
| 1029 | const index_id = try cg.module.constant(u32_ty_id, .{ .uint32 = @intCast(i) }); | 1401 | const index_id = cg.allocId(); |
| | 1402 | try cg.sections.globals.emit(gpa, .OpConstant, .{ |
| | 1403 | .id_result_type = u32_ty_id, |
| | 1404 | .id_result = index_id, |
| | 1405 | .value = .{ .uint32 = @intCast(i) }, |
| | 1406 | }); |
| 1030 | const field_ptr = try cg.accessChainId(field_ptr_ty_id, var_id, &.{index_id}); | 1407 | const field_ptr = try cg.accessChainId(field_ptr_ty_id, var_id, &.{index_id}); |
| 1031 | try cg.body.emit(gpa, .OpStore, .{ | 1408 | try cg.body.emit(gpa, .OpStore, .{ |
| 1032 | .pointer = field_ptr, | 1409 | .pointer = field_ptr, |
| 1033 | .object = constituent, | 1410 | .object = constituent, |
| 1034 | }); | 1411 | }); |
| 1035 | } | 1412 | } |
| 1036 | const result_id = cg.module.allocId(); | 1413 | const result_id = cg.allocId(); |
| 1037 | try cg.body.emit(gpa, .OpLoad, .{ | 1414 | try cg.body.emit(gpa, .OpLoad, .{ |
| 1038 | .id_result_type = result_ty_id, | 1415 | .id_result_type = result_ty_id, |
| 1039 | .id_result = result_id, | 1416 | .id_result = result_id, |
| ... | @@ -1042,7 +1419,7 @@ pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const | ... | @@ -1042,7 +1419,7 @@ pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const |
| 1042 | return result_id; | 1419 | return result_id; |
| 1043 | } | 1420 | } |
| 1044 | | 1421 | |
| 1045 | const result_id = cg.module.allocId(); | 1422 | const result_id = cg.allocId(); |
| 1046 | try cg.body.emit(gpa, .OpCompositeConstruct, .{ | 1423 | try cg.body.emit(gpa, .OpCompositeConstruct, .{ |
| 1047 | .id_result_type = result_ty_id, | 1424 | .id_result_type = result_ty_id, |
| 1048 | .id_result = result_id, | 1425 | .id_result = result_id, |
| ... | @@ -1054,8 +1431,8 @@ pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const | ... | @@ -1054,8 +1431,8 @@ pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const |
| 1054 | /// Construct a composite at runtime with all lanes set to the same value. | 1431 | /// Construct a composite at runtime with all lanes set to the same value. |
| 1055 | /// ty must be an aggregate type. | 1432 | /// ty must be an aggregate type. |
| 1056 | fn constructCompositeSplat(cg: *CodeGen, ty: Type, constituent: Id) !Id { | 1433 | fn constructCompositeSplat(cg: *CodeGen, ty: Type, constituent: Id) !Id { |
| 1057 | const gpa = cg.module.gpa; | 1434 | const gpa = cg.gpa; |
| 1058 | const zcu = cg.module.zcu; | 1435 | const zcu = cg.zcu; |
| 1059 | const n: usize = @intCast(ty.arrayLen(zcu)); | 1436 | const n: usize = @intCast(ty.arrayLen(zcu)); |
| 1060 | | 1437 | |
| 1061 | const scratch_top = cg.id_scratch.items.len; | 1438 | const scratch_top = cg.id_scratch.items.len; |
| ... | @@ -1075,24 +1452,17 @@ fn constructCompositeSplat(cg: *CodeGen, ty: Type, constituent: Id) !Id { | ... | @@ -1075,24 +1452,17 @@ fn constructCompositeSplat(cg: *CodeGen, ty: Type, constituent: Id) !Id { |
| 1075 | // | 1452 | // |
| 1076 | /// This function should only be called during function code generation. | 1453 | /// This function should only be called during function code generation. |
| 1077 | fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | 1454 | fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1078 | const gpa = cg.module.gpa; | 1455 | const gpa = cg.gpa; |
| 1079 | | | |
| 1080 | // Note: Using intern_map can only be used with constants that DO NOT generate any runtime code!! | | |
| 1081 | // Ideally that should be all constants in the future, or it should be cleaned up somehow. For | | |
| 1082 | // now, only use the intern_map on case-by-case basis by breaking to :cache. | | |
| 1083 | if (cg.module.intern_map.get(.{ val.toIntern(), repr })) |id| { | | |
| 1084 | return id; | | |
| 1085 | } | | |
| 1086 | | 1456 | |
| 1087 | const pt = cg.pt; | 1457 | const pt = cg.pt; |
| 1088 | const zcu = cg.module.zcu; | 1458 | const zcu = cg.zcu; |
| 1089 | const target = cg.module.zcu.getTarget(); | 1459 | const target = cg.zcu.getTarget(); |
| 1090 | const result_ty_id = try cg.resolveType(ty, repr); | 1460 | const result_ty_id = try cg.resolveType(ty, repr); |
| 1091 | const ip = &zcu.intern_pool; | 1461 | const ip = &zcu.intern_pool; |
| 1092 | | 1462 | |
| 1093 | log.debug("lowering constant: ty = {f}, val = {f}, key = {s}", .{ ty.fmt(pt), val.fmtValue(pt), @tagName(ip.indexToKey(val.toIntern())) }); | 1463 | log.debug("lowering constant: ty = {f}, val = {f}, key = {s}", .{ ty.fmt(pt), val.fmtValue(pt), @tagName(ip.indexToKey(val.toIntern())) }); |
| 1094 | if (val.isUndef(zcu)) { | 1464 | if (val.isUndef(zcu)) { |
| 1095 | return cg.module.constUndef(result_ty_id); | 1465 | return cg.constUndef(result_ty_id); |
| 1096 | } | 1466 | } |
| 1097 | | 1467 | |
| 1098 | const cacheable_id = cache: { | 1468 | const cacheable_id = cache: { |
| ... | @@ -1133,9 +1503,25 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | ... | @@ -1133,9 +1503,25 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1133 | }, | 1503 | }, |
| 1134 | .int => { | 1504 | .int => { |
| 1135 | const int_info = ty.intInfo(zcu); | 1505 | const int_info = ty.intInfo(zcu); |
| 1136 | _, const is_big_int = cg.module.backingIntBits(int_info.bits); | 1506 | const backing_bits, const is_big_int = cg.backingIntBits(int_info.bits); |
| 1137 | if (is_big_int) { | 1507 | if (is_big_int) { |
| 1138 | break :cache try cg.constIntBig(ty, val); | 1508 | const n_limbs = backing_bits / big_int_bits; |
| | 1509 | const big_result_ty_id = try cg.resolveType(ty, .indirect); |
| | 1510 | var bigint_space: Value.BigIntSpace = undefined; |
| | 1511 | const bigint = val.toBigInt(&bigint_space, zcu); |
| | 1512 | const limb_values = try gpa.alloc(u32, n_limbs); |
| | 1513 | defer gpa.free(limb_values); |
| | 1514 | bigint.writeTwosComplement(std.mem.sliceAsBytes(limb_values), .little); |
| | 1515 | if (builtin.cpu.arch.endian() == .big) { |
| | 1516 | for (limb_values) |*limb| limb.* = @byteSwap(limb.*); |
| | 1517 | } |
| | 1518 | const scratch_top = cg.id_scratch.items.len; |
| | 1519 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| | 1520 | const constituents = try cg.id_scratch.addManyAsSlice(gpa, n_limbs); |
| | 1521 | for (constituents, 0..) |*c, i| { |
| | 1522 | c.* = try cg.constInt(.u32, limb_values[i]); |
| | 1523 | } |
| | 1524 | break :cache try cg.constructComposite(big_result_ty_id, constituents); |
| 1139 | } | 1525 | } |
| 1140 | if (ty.isSignedInt(zcu)) { | 1526 | if (ty.isSignedInt(zcu)) { |
| 1141 | break :cache try cg.constInt(ty, val.toSignedInt(zcu)); | 1527 | break :cache try cg.constInt(ty, val.toSignedInt(zcu)); |
| ... | @@ -1151,7 +1537,13 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | ... | @@ -1151,7 +1537,13 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1151 | 80, 128 => unreachable, // TODO | 1537 | 80, 128 => unreachable, // TODO |
| 1152 | else => unreachable, | 1538 | else => unreachable, |
| 1153 | }; | 1539 | }; |
| 1154 | break :cache try cg.module.constant(result_ty_id, lit); | 1540 | const lit_id = cg.allocId(); |
| | 1541 | try cg.sections.globals.emit(gpa, .OpConstant, .{ |
| | 1542 | .id_result_type = result_ty_id, |
| | 1543 | .id_result = lit_id, |
| | 1544 | .value = lit, |
| | 1545 | }); |
| | 1546 | break :cache lit_id; |
| 1155 | }, | 1547 | }, |
| 1156 | .err => |err| { | 1548 | .err => |err| { |
| 1157 | const value = try pt.getErrorValue(err.name); | 1549 | const value = try pt.getErrorValue(err.name); |
| ... | @@ -1218,7 +1610,7 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | ... | @@ -1218,7 +1610,7 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1218 | if (maybe_payload_val) |payload_val| { | 1610 | if (maybe_payload_val) |payload_val| { |
| 1219 | return try cg.constant(payload_ty, payload_val, .indirect); | 1611 | return try cg.constant(payload_ty, payload_val, .indirect); |
| 1220 | } else { | 1612 | } else { |
| 1221 | break :cache try cg.module.constNull(result_ty_id); | 1613 | break :cache try cg.constNull(result_ty_id); |
| 1222 | } | 1614 | } |
| 1223 | } | 1615 | } |
| 1224 | | 1616 | |
| ... | @@ -1229,7 +1621,7 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | ... | @@ -1229,7 +1621,7 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1229 | const payload_id = if (maybe_payload_val) |payload_val| | 1621 | const payload_id = if (maybe_payload_val) |payload_val| |
| 1230 | try cg.constant(payload_ty, payload_val, .indirect) | 1622 | try cg.constant(payload_ty, payload_val, .indirect) |
| 1231 | else | 1623 | else |
| 1232 | try cg.module.constUndef(try cg.resolveType(payload_ty, .indirect)); | 1624 | try cg.constUndef(try cg.resolveType(payload_ty, .indirect)); |
| 1233 | | 1625 | |
| 1234 | const comp_ty_id = try cg.resolveType(ty, .direct); | 1626 | const comp_ty_id = try cg.resolveType(ty, .direct); |
| 1235 | return try cg.constructComposite(comp_ty_id, &.{ payload_id, has_pl_id }); | 1627 | return try cg.constructComposite(comp_ty_id, &.{ payload_id, has_pl_id }); |
| ... | @@ -1339,21 +1731,18 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { | ... | @@ -1339,21 +1731,18 @@ fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { |
| 1339 | .memoized_call => unreachable, | 1731 | .memoized_call => unreachable, |
| 1340 | } | 1732 | } |
| 1341 | }; | 1733 | }; |
| 1342 | | | |
| 1343 | try cg.module.intern_map.putNoClobber(gpa, .{ val.toIntern(), repr }, cacheable_id); | | |
| 1344 | | | |
| 1345 | return cacheable_id; | 1734 | return cacheable_id; |
| 1346 | } | 1735 | } |
| 1347 | | 1736 | |
| 1348 | fn constantPtr(cg: *CodeGen, ptr_val: Value) !Id { | 1737 | fn constantPtr(cg: *CodeGen, ptr_val: Value) !Id { |
| 1349 | const pt = cg.pt; | 1738 | const pt = cg.pt; |
| 1350 | const zcu = cg.module.zcu; | 1739 | const zcu = cg.zcu; |
| 1351 | const gpa = cg.module.gpa; | 1740 | const gpa = cg.gpa; |
| 1352 | | 1741 | |
| 1353 | if (ptr_val.isUndef(zcu)) { | 1742 | if (ptr_val.isUndef(zcu)) { |
| 1354 | const result_ty = ptr_val.typeOf(zcu); | 1743 | const result_ty = ptr_val.typeOf(zcu); |
| 1355 | const result_ty_id = try cg.resolveType(result_ty, .direct); | 1744 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 1356 | return cg.module.constUndef(result_ty_id); | 1745 | return cg.constUndef(result_ty_id); |
| 1357 | } | 1746 | } |
| 1358 | | 1747 | |
| 1359 | var arena = std.heap.ArenaAllocator.init(gpa); | 1748 | var arena = std.heap.ArenaAllocator.init(gpa); |
| ... | @@ -1364,9 +1753,9 @@ fn constantPtr(cg: *CodeGen, ptr_val: Value) !Id { | ... | @@ -1364,9 +1753,9 @@ fn constantPtr(cg: *CodeGen, ptr_val: Value) !Id { |
| 1364 | } | 1753 | } |
| 1365 | | 1754 | |
| 1366 | fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { | 1755 | fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { |
| 1367 | const gpa = cg.module.gpa; | 1756 | const gpa = cg.gpa; |
| 1368 | const pt = cg.pt; | 1757 | const pt = cg.pt; |
| 1369 | const zcu = cg.module.zcu; | 1758 | const zcu = cg.zcu; |
| 1370 | const target = zcu.getTarget(); | 1759 | const target = zcu.getTarget(); |
| 1371 | switch (derivation) { | 1760 | switch (derivation) { |
| 1372 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, | 1761 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| ... | @@ -1383,7 +1772,7 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { | ... | @@ -1383,7 +1772,7 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { |
| 1383 | // TODO: This can probably be an OpSpecConstantOp Bitcast, but | 1772 | // TODO: This can probably be an OpSpecConstantOp Bitcast, but |
| 1384 | // that is not implemented by Mesa yet. Therefore, just generate it | 1773 | // that is not implemented by Mesa yet. Therefore, just generate it |
| 1385 | // as a runtime operation. | 1774 | // as a runtime operation. |
| 1386 | const result_ptr_id = cg.module.allocId(); | 1775 | const result_ptr_id = cg.allocId(); |
| 1387 | const value_id = try cg.constInt(.usize, int.addr); | 1776 | const value_id = try cg.constInt(.usize, int.addr); |
| 1388 | try cg.body.emit(gpa, .OpConvertUToPtr, .{ | 1777 | try cg.body.emit(gpa, .OpConvertUToPtr, .{ |
| 1389 | .id_result_type = result_ty_id, | 1778 | .id_result_type = result_ty_id, |
| ... | @@ -1392,13 +1781,87 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { | ... | @@ -1392,13 +1781,87 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { |
| 1392 | }); | 1781 | }); |
| 1393 | return result_ptr_id; | 1782 | return result_ptr_id; |
| 1394 | }, | 1783 | }, |
| 1395 | .nav_ptr => |nav| { | 1784 | .nav_ptr => |nav_index| { |
| 1396 | const result_ptr_ty = try pt.navPtrType(nav); | 1785 | const ip = &zcu.intern_pool; |
| 1397 | return cg.constantNavRef(result_ptr_ty, nav); | 1786 | const result_ptr_ty = try pt.navPtrType(nav_index); |
| | 1787 | const ty_id = try cg.resolveType(result_ptr_ty, .direct); |
| | 1788 | const nav = ip.getNav(nav_index); |
| | 1789 | const nav_ty: Type = .fromInterned(nav.resolved.?.type); |
| | 1790 | |
| | 1791 | switch (nav.resolved.?.value) { |
| | 1792 | .none => {}, |
| | 1793 | else => |value| switch (ip.indexToKey(value)) { |
| | 1794 | // TODO: Properly lower function pointers; for now substitute undef. |
| | 1795 | .func => return try cg.constUndef(ty_id), |
| | 1796 | .@"extern" => if (ip.isFunctionType(nav_ty.toIntern())) { |
| | 1797 | const spv_decl_index = try cg.resolveNav(ip, nav_index); |
| | 1798 | const decl = cg.declPtr(spv_decl_index); |
| | 1799 | try emitExternFnStub(cg, nav, decl, nav_ty); |
| | 1800 | return decl.result_id; |
| | 1801 | }, |
| | 1802 | else => {}, |
| | 1803 | }, |
| | 1804 | } |
| | 1805 | |
| | 1806 | if (!nav_ty.hasRuntimeBits(zcu) and nav_ty.zigTypeTag(zcu) != .spirv) { |
| | 1807 | return cg.constUndef(ty_id); |
| | 1808 | } |
| | 1809 | |
| | 1810 | const spv_decl_index = try cg.resolveNav(ip, nav_index); |
| | 1811 | const spv_decl = cg.declPtr(spv_decl_index); |
| | 1812 | assert(spv_decl.kind != .func); |
| | 1813 | const storage_class = cg.storageClass(nav.resolved.?.@"addrspace"); |
| | 1814 | try cg.addFunctionDep(spv_decl_index, storage_class); |
| | 1815 | |
| | 1816 | const nav_ty_id = try cg.resolveType(nav_ty, .indirect); |
| | 1817 | const decl_ptr_ty_id = try cg.ptrType(nav_ty_id, storage_class); |
| | 1818 | if (decl_ptr_ty_id == ty_id) return spv_decl.result_id; |
| | 1819 | switch (target.os.tag) { |
| | 1820 | .vulkan, .opengl => return spv_decl.result_id, |
| | 1821 | else => {}, |
| | 1822 | } |
| | 1823 | const casted_ptr_id = cg.allocId(); |
| | 1824 | try cg.body.emit(gpa, .OpBitcast, .{ |
| | 1825 | .id_result_type = ty_id, |
| | 1826 | .id_result = casted_ptr_id, |
| | 1827 | .operand = spv_decl.result_id, |
| | 1828 | }); |
| | 1829 | return casted_ptr_id; |
| 1398 | }, | 1830 | }, |
| 1399 | .uav_ptr => |uav| { | 1831 | .uav_ptr => |uav| { |
| | 1832 | const ip = &zcu.intern_pool; |
| 1400 | const result_ptr_ty: Type = .fromInterned(uav.orig_ty); | 1833 | const result_ptr_ty: Type = .fromInterned(uav.orig_ty); |
| 1401 | return cg.constantUavRef(result_ptr_ty, uav); | 1834 | const ty_id = try cg.resolveType(result_ptr_ty, .direct); |
| | 1835 | const uav_ty: Type = .fromInterned(ip.typeOf(uav.val)); |
| | 1836 | |
| | 1837 | switch (ip.indexToKey(uav.val)) { |
| | 1838 | .func => unreachable, // TODO |
| | 1839 | .@"extern" => assert(!ip.isFunctionType(uav_ty.toIntern())), |
| | 1840 | else => {}, |
| | 1841 | } |
| | 1842 | |
| | 1843 | if (!uav_ty.hasRuntimeBits(zcu) and uav_ty.zigTypeTag(zcu) != .spirv) { |
| | 1844 | return cg.constUndef(ty_id); |
| | 1845 | } |
| | 1846 | |
| | 1847 | // Uav refs are always generic. |
| | 1848 | assert(result_ptr_ty.ptrAddressSpace(zcu) == .generic); |
| | 1849 | const uav_ty_id = try cg.resolveType(uav_ty, .indirect); |
| | 1850 | const decl_ptr_ty_id = try cg.ptrType(uav_ty_id, .function); |
| | 1851 | const ptr_id = try cg.resolveUav(uav.val); |
| | 1852 | |
| | 1853 | if (decl_ptr_ty_id == ty_id) return ptr_id; |
| | 1854 | switch (target.os.tag) { |
| | 1855 | .vulkan, .opengl => return ptr_id, |
| | 1856 | else => {}, |
| | 1857 | } |
| | 1858 | const casted_ptr_id = cg.allocId(); |
| | 1859 | try cg.body.emit(gpa, .OpBitcast, .{ |
| | 1860 | .id_result_type = ty_id, |
| | 1861 | .id_result = casted_ptr_id, |
| | 1862 | .operand = ptr_id, |
| | 1863 | }); |
| | 1864 | return casted_ptr_id; |
| 1402 | }, | 1865 | }, |
| 1403 | .eu_payload_ptr => @panic("TODO"), | 1866 | .eu_payload_ptr => @panic("TODO"), |
| 1404 | .opt_payload_ptr => @panic("TODO"), | 1867 | .opt_payload_ptr => @panic("TODO"), |
| ... | @@ -1450,7 +1913,7 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { | ... | @@ -1450,7 +1913,7 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { |
| 1450 | return cg.accessChainId(result_ty_id, parent_ptr_id, ids); | 1913 | return cg.accessChainId(result_ty_id, parent_ptr_id, ids); |
| 1451 | } | 1914 | } |
| 1452 | if (target.os.tag == .opencl) { | 1915 | if (target.os.tag == .opencl) { |
| 1453 | const result_ptr_id = cg.module.allocId(); | 1916 | const result_ptr_id = cg.allocId(); |
| 1454 | try cg.body.emit(gpa, .OpBitcast, .{ | 1917 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 1455 | .id_result_type = result_ty_id, | 1918 | .id_result_type = result_ty_id, |
| 1456 | .id_result = result_ptr_id, | 1919 | .id_result = result_ptr_id, |
| ... | @@ -1468,59 +1931,15 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { | ... | @@ -1468,59 +1931,15 @@ fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { |
| 1468 | } | 1931 | } |
| 1469 | } | 1932 | } |
| 1470 | | 1933 | |
| 1471 | fn constantUavRef( | | |
| 1472 | cg: *CodeGen, | | |
| 1473 | ty: Type, | | |
| 1474 | uav: InternPool.Key.Ptr.BaseAddr.Uav, | | |
| 1475 | ) !Id { | | |
| 1476 | // TODO: Merge this function with constantDeclRef. | | |
| 1477 | | | |
| 1478 | const zcu = cg.module.zcu; | | |
| 1479 | const ip = &zcu.intern_pool; | | |
| 1480 | const ty_id = try cg.resolveType(ty, .direct); | | |
| 1481 | const uav_ty: Type = .fromInterned(ip.typeOf(uav.val)); | | |
| 1482 | | | |
| 1483 | switch (ip.indexToKey(uav.val)) { | | |
| 1484 | .func => unreachable, // TODO | | |
| 1485 | .@"extern" => assert(!ip.isFunctionType(uav_ty.toIntern())), | | |
| 1486 | else => {}, | | |
| 1487 | } | | |
| 1488 | | | |
| 1489 | // const is_fn_body = decl_ty.zigTypeTag(zcu) == .@"fn"; | | |
| 1490 | if (!uav_ty.hasRuntimeBits(zcu)) { | | |
| 1491 | // Pointer to nothing - return undefined | | |
| 1492 | return cg.module.constUndef(ty_id); | | |
| 1493 | } | | |
| 1494 | | | |
| 1495 | // Uav refs are always generic. | | |
| 1496 | assert(ty.ptrAddressSpace(zcu) == .generic); | | |
| 1497 | const uav_ty_id = try cg.resolveType(uav_ty, .indirect); | | |
| 1498 | const decl_ptr_ty_id = try cg.module.ptrType(uav_ty_id, .function); | | |
| 1499 | const ptr_id = try cg.resolveUav(uav.val); | | |
| 1500 | | | |
| 1501 | if (decl_ptr_ty_id != ty_id) { | | |
| 1502 | // Differing pointer types, insert a cast. | | |
| 1503 | const casted_ptr_id = cg.module.allocId(); | | |
| 1504 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | | |
| 1505 | .id_result_type = ty_id, | | |
| 1506 | .id_result = casted_ptr_id, | | |
| 1507 | .operand = ptr_id, | | |
| 1508 | }); | | |
| 1509 | return casted_ptr_id; | | |
| 1510 | } else { | | |
| 1511 | return ptr_id; | | |
| 1512 | } | | |
| 1513 | } | | |
| 1514 | | | |
| 1515 | /// Emit a stub OpFunction/OpFunctionEnd + Import linkage decoration for an | 1934 | /// Emit a stub OpFunction/OpFunctionEnd + Import linkage decoration for an |
| 1516 | /// extern function so the module is structurally valid. The stub will be | 1935 | /// extern function so the module is structurally valid. The stub will be |
| 1517 | /// replaced by the real definition at link time. | 1936 | /// replaced by the real definition at link time. |
| 1518 | fn emitExternFnStub(cg: *CodeGen, nav: InternPool.Nav, decl: *Module.Decl, fn_ty: Type) !void { | 1937 | fn emitExternFnStub(cg: *CodeGen, nav: InternPool.Nav, decl: *Decl, fn_ty: Type) !void { |
| 1519 | if (decl.has_extern_stub) return; | 1938 | if (decl.has_extern_stub) return; |
| 1520 | decl.has_extern_stub = true; | 1939 | decl.has_extern_stub = true; |
| 1521 | | 1940 | |
| 1522 | const gpa = cg.module.gpa; | 1941 | const gpa = cg.gpa; |
| 1523 | const zcu = cg.module.zcu; | 1942 | const zcu = cg.zcu; |
| 1524 | const ip = &zcu.intern_pool; | 1943 | const ip = &zcu.intern_pool; |
| 1525 | const fn_info = zcu.typeToFunc(fn_ty).?; | 1944 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 1526 | const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); | 1945 | const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); |
| ... | @@ -1540,81 +1959,25 @@ fn emitExternFnStub(cg: *CodeGen, nav: InternPool.Nav, decl: *Module.Decl, fn_ty | ... | @@ -1540,81 +1959,25 @@ fn emitExternFnStub(cg: *CodeGen, nav: InternPool.Nav, decl: *Module.Decl, fn_ty |
| 1540 | const param_type_id = try cg.resolveType(param_ty, .direct); | 1959 | const param_type_id = try cg.resolveType(param_ty, .direct); |
| 1541 | try stub.emit(gpa, .OpFunctionParameter, .{ | 1960 | try stub.emit(gpa, .OpFunctionParameter, .{ |
| 1542 | .id_result_type = param_type_id, | 1961 | .id_result_type = param_type_id, |
| 1543 | .id_result = cg.module.allocId(), | 1962 | .id_result = cg.allocId(), |
| 1544 | }); | 1963 | }); |
| 1545 | } | 1964 | } |
| 1546 | try stub.emit(gpa, .OpFunctionEnd, {}); | 1965 | try stub.emit(gpa, .OpFunctionEnd, {}); |
| 1547 | try cg.module.sections.functions.append(gpa, stub); | 1966 | try cg.sections.functions.append(gpa, stub); |
| 1548 | | 1967 | |
| 1549 | const extern_name = nav.getExtern(ip).?.name.toSlice(ip); | 1968 | const extern_name = nav.getExtern(ip).?.name.toSlice(ip); |
| 1550 | try cg.module.sections.annotations.emit(gpa, .OpDecorate, .{ | 1969 | try cg.sections.annotations.emit(gpa, .OpDecorate, .{ |
| 1551 | .target = decl.result_id, | 1970 | .target = decl.result_id, |
| 1552 | .decoration = .{ .linkage_attributes = .{ | 1971 | .decoration = .{ .linkage_attributes = .{ |
| 1553 | .name = extern_name, | 1972 | .name = extern_name, |
| 1554 | .linkage_type = .import, | 1973 | .linkage_type = .import, |
| 1555 | } }, | 1974 | } }, |
| 1556 | }); | 1975 | }); |
| 1557 | try cg.module.debugName(decl.result_id, extern_name); | 1976 | try cg.debugName(decl.result_id, extern_name); |
| 1558 | } | 1977 | } |
| 1559 | | 1978 | |
| 1560 | fn constantNavRef(cg: *CodeGen, ty: Type, nav_index: InternPool.Nav.Index) !Id { | | |
| 1561 | const zcu = cg.module.zcu; | | |
| 1562 | const ip = &zcu.intern_pool; | | |
| 1563 | const ty_id = try cg.resolveType(ty, .direct); | | |
| 1564 | const nav = ip.getNav(nav_index); | | |
| 1565 | const nav_ty: Type = .fromInterned(nav.resolved.?.type); | | |
| 1566 | | | |
| 1567 | switch (nav.resolved.?.value) { | | |
| 1568 | .none => {}, // this is not a function or extern | | |
| 1569 | else => |value| switch (ip.indexToKey(value)) { | | |
| 1570 | .func => { | | |
| 1571 | // TODO: Properly lower function pointers. For now we are going to hack around it and | | |
| 1572 | // just generate an empty pointer. Function pointers are represented by a pointer to usize. | | |
| 1573 | return try cg.module.constUndef(ty_id); | | |
| 1574 | }, | | |
| 1575 | .@"extern" => if (ip.isFunctionType(nav_ty.toIntern())) { | | |
| 1576 | const spv_decl_index = try cg.module.resolveNav(ip, nav_index); | | |
| 1577 | const decl = cg.module.declPtr(spv_decl_index); | | |
| 1578 | try emitExternFnStub(cg, nav, decl, nav_ty); | | |
| 1579 | return decl.result_id; | | |
| 1580 | }, | | |
| 1581 | else => {}, | | |
| 1582 | }, | | |
| 1583 | } | | |
| 1584 | | | |
| 1585 | if (!nav_ty.hasRuntimeBits(zcu)) { | | |
| 1586 | // Pointer to nothing - return undefined. | | |
| 1587 | return cg.module.constUndef(ty_id); | | |
| 1588 | } | | |
| 1589 | | | |
| 1590 | const spv_decl_index = try cg.module.resolveNav(ip, nav_index); | | |
| 1591 | const spv_decl = cg.module.declPtr(spv_decl_index); | | |
| 1592 | const spv_decl_result_id = spv_decl.result_id; | | |
| 1593 | assert(spv_decl.kind != .func); | | |
| 1594 | | | |
| 1595 | const storage_class = cg.module.storageClass(nav.resolved.?.@"addrspace"); | | |
| 1596 | try cg.addFunctionDep(spv_decl_index, storage_class); | | |
| 1597 | | | |
| 1598 | const nav_ty_id = try cg.resolveType(nav_ty, .indirect); | | |
| 1599 | const decl_ptr_ty_id = try cg.module.ptrType(nav_ty_id, storage_class); | | |
| 1600 | | | |
| 1601 | if (decl_ptr_ty_id != ty_id) { | | |
| 1602 | // Differing pointer types, insert a cast. | | |
| 1603 | const casted_ptr_id = cg.module.allocId(); | | |
| 1604 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | | |
| 1605 | .id_result_type = ty_id, | | |
| 1606 | .id_result = casted_ptr_id, | | |
| 1607 | .operand = spv_decl_result_id, | | |
| 1608 | }); | | |
| 1609 | return casted_ptr_id; | | |
| 1610 | } | | |
| 1611 | | | |
| 1612 | return spv_decl_result_id; | | |
| 1613 | } | | |
| 1614 | | | |
| 1615 | // Turn a Zig type's name into a cache reference. | | |
| 1616 | fn resolveTypeName(cg: *CodeGen, ty: Type) ![]const u8 { | 1979 | fn resolveTypeName(cg: *CodeGen, ty: Type) ![]const u8 { |
| 1617 | const gpa = cg.module.gpa; | 1980 | const gpa = cg.gpa; |
| 1618 | var aw: std.Io.Writer.Allocating = .init(gpa); | 1981 | var aw: std.Io.Writer.Allocating = .init(gpa); |
| 1619 | defer aw.deinit(); | 1982 | defer aw.deinit(); |
| 1620 | ty.print(&aw.writer, cg.pt, null) catch |err| switch (err) { | 1983 | ty.print(&aw.writer, cg.pt, null) catch |err| switch (err) { |
| ... | @@ -1641,67 +2004,8 @@ fn resolveTypeName(cg: *CodeGen, ty: Type) ![]const u8 { | ... | @@ -1641,67 +2004,8 @@ fn resolveTypeName(cg: *CodeGen, ty: Type) ![]const u8 { |
| 1641 | /// padding: [padding_size]u8, | 2004 | /// padding: [padding_size]u8, |
| 1642 | /// } | 2005 | /// } |
| 1643 | /// If any of the fields' size is 0, it will be omitted. | 2006 | /// If any of the fields' size is 0, it will be omitted. |
| 1644 | fn resolveUnionType(cg: *CodeGen, ty: Type) !Id { | | |
| 1645 | const gpa = cg.module.gpa; | | |
| 1646 | const zcu = cg.module.zcu; | | |
| 1647 | const union_obj = zcu.typeToUnion(ty).?; | | |
| 1648 | | | |
| 1649 | if (union_obj.layout == .@"packed") { | | |
| 1650 | return try cg.module.intType(.unsigned, @intCast(ty.bitSize(zcu))); | | |
| 1651 | } | | |
| 1652 | | | |
| 1653 | const layout = cg.unionLayout(ty); | | |
| 1654 | if (!layout.has_payload) { | | |
| 1655 | // No payload, so represent this as just the tag type. | | |
| 1656 | return try cg.resolveType(.fromInterned(union_obj.enum_tag_type), .indirect); | | |
| 1657 | } | | |
| 1658 | | | |
| 1659 | var member_types: [4]Id = undefined; | | |
| 1660 | var member_names: [4][]const u8 = undefined; | | |
| 1661 | | | |
| 1662 | const u8_ty_id = try cg.resolveType(.u8, .direct); | | |
| 1663 | | | |
| 1664 | if (layout.tag_size != 0) { | | |
| 1665 | const tag_ty_id = try cg.resolveType(.fromInterned(union_obj.enum_tag_type), .indirect); | | |
| 1666 | member_types[layout.tag_index] = tag_ty_id; | | |
| 1667 | member_names[layout.tag_index] = "(tag)"; | | |
| 1668 | } | | |
| 1669 | | | |
| 1670 | if (layout.payload_size != 0) { | | |
| 1671 | const payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); | | |
| 1672 | member_types[layout.payload_index] = payload_ty_id; | | |
| 1673 | member_names[layout.payload_index] = "(payload)"; | | |
| 1674 | } | | |
| 1675 | | | |
| 1676 | if (layout.payload_padding_size != 0) { | | |
| 1677 | const len_id = try cg.constInt(.u32, layout.payload_padding_size); | | |
| 1678 | const payload_padding_ty_id = try cg.module.arrayType(len_id, u8_ty_id); | | |
| 1679 | member_types[layout.payload_padding_index] = payload_padding_ty_id; | | |
| 1680 | member_names[layout.payload_padding_index] = "(payload padding)"; | | |
| 1681 | } | | |
| 1682 | | | |
| 1683 | if (layout.padding_size != 0) { | | |
| 1684 | const len_id = try cg.constInt(.u32, layout.padding_size); | | |
| 1685 | const padding_ty_id = try cg.module.arrayType(len_id, u8_ty_id); | | |
| 1686 | member_types[layout.padding_index] = padding_ty_id; | | |
| 1687 | member_names[layout.padding_index] = "(padding)"; | | |
| 1688 | } | | |
| 1689 | | | |
| 1690 | const result_id = try cg.module.structType( | | |
| 1691 | member_types[0..layout.total_fields], | | |
| 1692 | member_names[0..layout.total_fields], | | |
| 1693 | .none, | | |
| 1694 | ); | | |
| 1695 | | | |
| 1696 | const type_name = try cg.resolveTypeName(ty); | | |
| 1697 | defer gpa.free(type_name); | | |
| 1698 | try cg.module.debugName(result_id, type_name); | | |
| 1699 | | | |
| 1700 | return result_id; | | |
| 1701 | } | | |
| 1702 | | | |
| 1703 | fn resolveFnReturnType(cg: *CodeGen, ret_ty: Type) !Id { | 2007 | fn resolveFnReturnType(cg: *CodeGen, ret_ty: Type) !Id { |
| 1704 | const zcu = cg.module.zcu; | 2008 | const zcu = cg.zcu; |
| 1705 | if (!ret_ty.hasRuntimeBits(zcu)) { | 2009 | if (!ret_ty.hasRuntimeBits(zcu)) { |
| 1706 | // If the return type is an error set or an error union, then we make this | 2010 | // If the return type is an error set or an error union, then we make this |
| 1707 | // anyerror return type instead, so that it can be coerced into a function | 2011 | // anyerror return type instead, so that it can be coerced into a function |
| ... | @@ -1717,38 +2021,38 @@ fn resolveFnReturnType(cg: *CodeGen, ret_ty: Type) !Id { | ... | @@ -1717,38 +2021,38 @@ fn resolveFnReturnType(cg: *CodeGen, ret_ty: Type) !Id { |
| 1717 | } | 2021 | } |
| 1718 | | 2022 | |
| 1719 | fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | 2023 | fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1720 | const gpa = cg.module.gpa; | 2024 | const gpa = cg.gpa; |
| 1721 | const pt = cg.pt; | 2025 | const pt = cg.pt; |
| 1722 | const zcu = cg.module.zcu; | 2026 | const zcu = cg.zcu; |
| 1723 | const ip = &zcu.intern_pool; | 2027 | const ip = &zcu.intern_pool; |
| 1724 | const target = cg.module.zcu.getTarget(); | 2028 | const target = cg.zcu.getTarget(); |
| 1725 | | 2029 | |
| 1726 | log.debug("resolveType: ty = {f}", .{ty.fmt(pt)}); | 2030 | log.debug("resolveType: ty = {f}", .{ty.fmt(pt)}); |
| 1727 | | 2031 | |
| 1728 | switch (ty.zigTypeTag(zcu)) { | 2032 | switch (ty.zigTypeTag(zcu)) { |
| 1729 | .noreturn => { | 2033 | .noreturn => { |
| 1730 | assert(repr == .direct); | 2034 | assert(repr == .direct); |
| 1731 | return try cg.module.voidType(); | 2035 | return try cg.voidType(); |
| 1732 | }, | 2036 | }, |
| 1733 | .void => switch (repr) { | 2037 | .void => switch (repr) { |
| 1734 | .direct => return try cg.module.voidType(), | 2038 | .direct => return try cg.voidType(), |
| 1735 | .indirect => { | 2039 | .indirect => { |
| 1736 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); | 2040 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); |
| 1737 | return try cg.module.opaqueType("void"); | 2041 | return try cg.opaqueType("void"); |
| 1738 | }, | 2042 | }, |
| 1739 | }, | 2043 | }, |
| 1740 | .bool => switch (repr) { | 2044 | .bool => switch (repr) { |
| 1741 | .direct => return try cg.module.boolType(), | 2045 | .direct => return try cg.boolType(), |
| 1742 | .indirect => return try cg.resolveType(.u1, .indirect), | 2046 | .indirect => return try cg.resolveType(.u1, .indirect), |
| 1743 | }, | 2047 | }, |
| 1744 | .int => { | 2048 | .int => { |
| 1745 | if (ty.toIntern() == .u0_type) { | 2049 | if (ty.toIntern() == .u0_type) { |
| 1746 | assert(repr == .indirect); | 2050 | assert(repr == .indirect); |
| 1747 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); | 2051 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); |
| 1748 | return try cg.module.opaqueType("u0"); | 2052 | return try cg.opaqueType("u0"); |
| 1749 | } | 2053 | } |
| 1750 | const int_info = ty.intInfo(zcu); | 2054 | const int_info = ty.intInfo(zcu); |
| 1751 | return try cg.module.intType(int_info.signedness, int_info.bits); | 2055 | return try cg.intType(int_info.signedness, int_info.bits); |
| 1752 | }, | 2056 | }, |
| 1753 | .@"enum" => return try cg.resolveType(ty.intTagType(zcu), repr), | 2057 | .@"enum" => return try cg.resolveType(ty.intTagType(zcu), repr), |
| 1754 | .float => { | 2058 | .float => { |
| ... | @@ -1767,7 +2071,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1767,7 +2071,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1767 | ); | 2071 | ); |
| 1768 | } | 2072 | } |
| 1769 | | 2073 | |
| 1770 | return try cg.module.floatType(bits); | 2074 | return try cg.floatType(bits); |
| 1771 | }, | 2075 | }, |
| 1772 | .array => { | 2076 | .array => { |
| 1773 | const elem_ty = ty.childType(zcu); | 2077 | const elem_ty = ty.childType(zcu); |
| ... | @@ -1779,7 +2083,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1779,7 +2083,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1779 | if (!elem_ty.hasRuntimeBits(zcu)) { | 2083 | if (!elem_ty.hasRuntimeBits(zcu)) { |
| 1780 | assert(repr == .indirect); | 2084 | assert(repr == .indirect); |
| 1781 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); | 2085 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); |
| 1782 | return try cg.module.opaqueType("zero-sized-array"); | 2086 | return try cg.opaqueType("zero-sized-array"); |
| 1783 | } else if (total_len == 0) { | 2087 | } else if (total_len == 0) { |
| 1784 | // The size of the array would be 0, but that is not allowed in SPIR-V. | 2088 | // The size of the array would be 0, but that is not allowed in SPIR-V. |
| 1785 | // This path can be reached for example when there is a slicing of a pointer | 2089 | // This path can be reached for example when there is a slicing of a pointer |
| ... | @@ -1790,25 +2094,24 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1790,25 +2094,24 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1790 | // generate an array of 1 element instead, so that ptr_elem_ptr instructions | 2094 | // generate an array of 1 element instead, so that ptr_elem_ptr instructions |
| 1791 | // can be lowered to ptrAccessChain instead of manually performing the math. | 2095 | // can be lowered to ptrAccessChain instead of manually performing the math. |
| 1792 | const len_id = try cg.constInt(.u32, 1); | 2096 | const len_id = try cg.constInt(.u32, 1); |
| 1793 | return try cg.module.arrayType(len_id, elem_ty_id); | 2097 | return try cg.arrayType(len_id, elem_ty_id); |
| 1794 | } else { | 2098 | } else { |
| 1795 | const total_len_id = try cg.constInt(.u32, total_len); | 2099 | const total_len_id = try cg.constInt(.u32, total_len); |
| 1796 | return try cg.module.arrayType(total_len_id, elem_ty_id); | 2100 | return try cg.arrayType(total_len_id, elem_ty_id); |
| 1797 | } | 2101 | } |
| 1798 | }, | 2102 | }, |
| 1799 | .vector => { | 2103 | .vector => { |
| 1800 | const elem_ty = ty.childType(zcu); | 2104 | const elem_ty = ty.childType(zcu); |
| 1801 | const elem_ty_id = try cg.resolveType(elem_ty, repr); | 2105 | const elem_ty_id = try cg.resolveType(elem_ty, repr); |
| 1802 | const len = ty.vectorLen(zcu); | 2106 | const len = ty.vectorLen(zcu); |
| 1803 | if (cg.isSpvVector(ty)) return try cg.module.vectorType(len, elem_ty_id); | 2107 | if (cg.isSpvVector(ty)) return try cg.vectorType(len, elem_ty_id); |
| 1804 | const len_id = try cg.constInt(.u32, len); | 2108 | const len_id = try cg.constInt(.u32, len); |
| 1805 | return try cg.module.arrayType(len_id, elem_ty_id); | 2109 | return try cg.arrayType(len_id, elem_ty_id); |
| 1806 | }, | 2110 | }, |
| 1807 | .@"fn" => switch (repr) { | 2111 | .@"fn" => switch (repr) { |
| 1808 | .direct => { | 2112 | .direct => { |
| 1809 | const fn_info = zcu.typeToFunc(ty).?; | 2113 | const fn_info = zcu.typeToFunc(ty).?; |
| 1810 | | 2114 | |
| 1811 | comptime assert(zig_call_abi_ver == 3); | | |
| 1812 | assert(!fn_info.is_var_args); | 2115 | assert(!fn_info.is_var_args); |
| 1813 | switch (fn_info.cc) { | 2116 | switch (fn_info.cc) { |
| 1814 | .auto, | 2117 | .auto, |
| ... | @@ -1837,7 +2140,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1837,7 +2140,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1837 | param_index += 1; | 2140 | param_index += 1; |
| 1838 | } | 2141 | } |
| 1839 | | 2142 | |
| 1840 | return try cg.module.functionType(return_ty_id, param_ty_ids[0..param_index]); | 2143 | return try cg.functionType(return_ty_id, param_ty_ids[0..param_index]); |
| 1841 | }, | 2144 | }, |
| 1842 | .indirect => { | 2145 | .indirect => { |
| 1843 | // TODO: Represent function pointers properly. | 2146 | // TODO: Represent function pointers properly. |
| ... | @@ -1860,15 +2163,15 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1860,15 +2163,15 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1860 | }, | 2163 | }, |
| 1861 | }; | 2164 | }; |
| 1862 | const child_ty_id = try cg.resolveType(child_ty, .indirect); | 2165 | const child_ty_id = try cg.resolveType(child_ty, .indirect); |
| 1863 | const storage_class = cg.module.storageClass(ptr_info.flags.address_space); | 2166 | const storage_class = cg.storageClass(ptr_info.flags.address_space); |
| 1864 | const ptr_ty_id = try cg.module.ptrType(child_ty_id, storage_class); | 2167 | const ptr_ty_id = try cg.ptrType(child_ty_id, storage_class); |
| 1865 | | 2168 | |
| 1866 | if (ptr_info.flags.size != .slice) { | 2169 | if (ptr_info.flags.size != .slice) { |
| 1867 | return ptr_ty_id; | 2170 | return ptr_ty_id; |
| 1868 | } | 2171 | } |
| 1869 | | 2172 | |
| 1870 | const size_ty_id = try cg.resolveType(.usize, .direct); | 2173 | const size_ty_id = try cg.resolveType(.usize, .direct); |
| 1871 | return try cg.module.structType( | 2174 | return try cg.structType( |
| 1872 | &.{ ptr_ty_id, size_ty_id }, | 2175 | &.{ ptr_ty_id, size_ty_id }, |
| 1873 | &.{ "ptr", "len" }, | 2176 | &.{ "ptr", "len" }, |
| 1874 | .none, | 2177 | .none, |
| ... | @@ -1889,14 +2192,14 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1889,14 +2192,14 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1889 | member_index += 1; | 2192 | member_index += 1; |
| 1890 | } | 2193 | } |
| 1891 | | 2194 | |
| 1892 | const result_id = try cg.module.structType( | 2195 | const result_id = try cg.structType( |
| 1893 | member_types[0..member_index], | 2196 | member_types[0..member_index], |
| 1894 | null, | 2197 | null, |
| 1895 | .none, | 2198 | .none, |
| 1896 | ); | 2199 | ); |
| 1897 | const type_name = try cg.resolveTypeName(ty); | 2200 | const type_name = try cg.resolveTypeName(ty); |
| 1898 | defer gpa.free(type_name); | 2201 | defer gpa.free(type_name); |
| 1899 | try cg.module.debugName(result_id, type_name); | 2202 | try cg.debugName(result_id, type_name); |
| 1900 | return result_id; | 2203 | return result_id; |
| 1901 | }, | 2204 | }, |
| 1902 | .struct_type => ip.loadStructType(ty.toIntern()), | 2205 | .struct_type => ip.loadStructType(ty.toIntern()), |
| ... | @@ -1923,7 +2226,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1923,7 +2226,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1923 | try member_names.append(field_name.toSlice(ip)); | 2226 | try member_names.append(field_name.toSlice(ip)); |
| 1924 | } | 2227 | } |
| 1925 | | 2228 | |
| 1926 | const result_id = try cg.module.structType( | 2229 | const result_id = try cg.structType( |
| 1927 | member_types.items, | 2230 | member_types.items, |
| 1928 | member_names.items, | 2231 | member_names.items, |
| 1929 | ty.toIntern(), | 2232 | ty.toIntern(), |
| ... | @@ -1931,7 +2234,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1931,7 +2234,7 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1931 | | 2234 | |
| 1932 | const type_name = try cg.resolveTypeName(ty); | 2235 | const type_name = try cg.resolveTypeName(ty); |
| 1933 | defer gpa.free(type_name); | 2236 | defer gpa.free(type_name); |
| 1934 | try cg.module.debugName(result_id, type_name); | 2237 | try cg.debugName(result_id, type_name); |
| 1935 | | 2238 | |
| 1936 | return result_id; | 2239 | return result_id; |
| 1937 | }, | 2240 | }, |
| ... | @@ -1952,13 +2255,52 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1952,13 +2255,52 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1952 | | 2255 | |
| 1953 | const bool_ty_id = try cg.resolveType(.bool, .indirect); | 2256 | const bool_ty_id = try cg.resolveType(.bool, .indirect); |
| 1954 | | 2257 | |
| 1955 | return try cg.module.structType( | 2258 | return try cg.structType( |
| 1956 | &.{ payload_ty_id, bool_ty_id }, | 2259 | &.{ payload_ty_id, bool_ty_id }, |
| 1957 | &.{ "payload", "valid" }, | 2260 | &.{ "payload", "valid" }, |
| 1958 | .none, | 2261 | .none, |
| 1959 | ); | 2262 | ); |
| 1960 | }, | 2263 | }, |
| 1961 | .@"union" => return try cg.resolveUnionType(ty), | 2264 | .@"union" => { |
| | 2265 | const union_obj = zcu.typeToUnion(ty).?; |
| | 2266 | if (union_obj.layout == .@"packed") { |
| | 2267 | return try cg.intType(.unsigned, @intCast(ty.bitSize(zcu))); |
| | 2268 | } |
| | 2269 | const layout = cg.unionLayout(ty); |
| | 2270 | if (!layout.has_payload) { |
| | 2271 | return try cg.resolveType(.fromInterned(union_obj.enum_tag_type), .indirect); |
| | 2272 | } |
| | 2273 | var member_types: [4]Id = undefined; |
| | 2274 | var member_names: [4][]const u8 = undefined; |
| | 2275 | const u8_ty_id = try cg.resolveType(.u8, .direct); |
| | 2276 | if (layout.tag_size != 0) { |
| | 2277 | member_types[layout.tag_index] = try cg.resolveType(.fromInterned(union_obj.enum_tag_type), .indirect); |
| | 2278 | member_names[layout.tag_index] = "(tag)"; |
| | 2279 | } |
| | 2280 | if (layout.payload_size != 0) { |
| | 2281 | member_types[layout.payload_index] = try cg.resolveType(layout.payload_ty, .indirect); |
| | 2282 | member_names[layout.payload_index] = "(payload)"; |
| | 2283 | } |
| | 2284 | if (layout.payload_padding_size != 0) { |
| | 2285 | const len_id = try cg.constInt(.u32, layout.payload_padding_size); |
| | 2286 | member_types[layout.payload_padding_index] = try cg.arrayType(len_id, u8_ty_id); |
| | 2287 | member_names[layout.payload_padding_index] = "(payload padding)"; |
| | 2288 | } |
| | 2289 | if (layout.padding_size != 0) { |
| | 2290 | const len_id = try cg.constInt(.u32, layout.padding_size); |
| | 2291 | member_types[layout.padding_index] = try cg.arrayType(len_id, u8_ty_id); |
| | 2292 | member_names[layout.padding_index] = "(padding)"; |
| | 2293 | } |
| | 2294 | const result_id = try cg.structType( |
| | 2295 | member_types[0..layout.total_fields], |
| | 2296 | member_names[0..layout.total_fields], |
| | 2297 | .none, |
| | 2298 | ); |
| | 2299 | const type_name = try cg.resolveTypeName(ty); |
| | 2300 | defer gpa.free(type_name); |
| | 2301 | try cg.debugName(result_id, type_name); |
| | 2302 | return result_id; |
| | 2303 | }, |
| 1962 | .error_set => { | 2304 | .error_set => { |
| 1963 | const err_int_ty = try pt.errorIntType(); | 2305 | const err_int_ty = try pt.errorIntType(); |
| 1964 | return try cg.resolveType(err_int_ty, repr); | 2306 | return try cg.resolveType(err_int_ty, repr); |
| ... | @@ -1989,46 +2331,43 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -1989,46 +2331,43 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 1989 | // TODO: ABI padding? | 2331 | // TODO: ABI padding? |
| 1990 | } | 2332 | } |
| 1991 | | 2333 | |
| 1992 | return try cg.module.structType(&member_types, &member_names, .none); | 2334 | return try cg.structType(&member_types, &member_names, .none); |
| 1993 | }, | 2335 | }, |
| 1994 | .@"opaque" => { | 2336 | .@"opaque" => { |
| 1995 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); | 2337 | if (target.os.tag != .opencl) return cg.fail("cannot generate opaque type", .{}); |
| 1996 | const type_name = try cg.resolveTypeName(ty); | 2338 | const type_name = try cg.resolveTypeName(ty); |
| 1997 | defer gpa.free(type_name); | 2339 | defer gpa.free(type_name); |
| 1998 | return try cg.module.opaqueType(type_name); | 2340 | return try cg.opaqueType(type_name); |
| 1999 | }, | 2341 | }, |
| 2000 | .spirv => { | 2342 | .spirv => { |
| 2001 | const ip_index = ty.toIntern(); | 2343 | const spirv_type = ip.loadSpirvType(ty.toIntern()); |
| 2002 | const spirv_type = ip.loadSpirvType(ip_index); | 2344 | const result_id = cg.allocId(); |
| 2003 | switch (spirv_type.flags.tag) { | 2345 | switch (spirv_type.flags.tag) { |
| 2004 | .sampler => return try cg.module.samplerType(ip_index), | 2346 | .sampler => try cg.sections.globals.emit(gpa, .OpTypeSampler, .{ .id_result = result_id }), |
| 2005 | .image => { | 2347 | .image => { |
| 2006 | const sampled_type_id = blk: { | 2348 | const sampled_type_id = try cg.resolveType(.fromInterned(spirv_type.ty), .direct); |
| 2007 | if (spirv_type.ty == .none) break :blk try cg.module.intType(.unsigned, 32); | 2349 | try cg.sections.globals.emit(gpa, .OpTypeImage, .{ |
| 2008 | break :blk try cg.resolveType(Type.fromInterned(spirv_type.ty), .direct); | 2350 | .id_result = result_id, |
| 2009 | }; | 2351 | .sampled_type = sampled_type_id, |
| 2010 | return try cg.module.imageType( | 2352 | .dim = switch (spirv_type.flags.dim) { |
| 2011 | ip_index, | | |
| 2012 | sampled_type_id, | | |
| 2013 | switch (spirv_type.flags.dim) { | | |
| 2014 | .@"1d" => .@"1d", | 2353 | .@"1d" => .@"1d", |
| 2015 | .@"2d" => .@"2d", | 2354 | .@"2d" => .@"2d", |
| 2016 | .@"3d" => .@"3d", | 2355 | .@"3d" => .@"3d", |
| 2017 | .cube => .cube, | 2356 | .cube => .cube, |
| 2018 | }, | 2357 | }, |
| 2019 | switch (spirv_type.flags.depth) { | 2358 | .depth = switch (spirv_type.flags.depth) { |
| 2020 | .not_depth => 0, | 2359 | .not_depth => 0, |
| 2021 | .depth => 1, | 2360 | .depth => 1, |
| 2022 | .unknown => 2, | 2361 | .unknown => 2, |
| 2023 | }, | 2362 | }, |
| 2024 | @intFromBool(spirv_type.flags.is_arrayed), | 2363 | .arrayed = @intFromBool(spirv_type.flags.is_arrayed), |
| 2025 | @intFromBool(spirv_type.flags.is_multisampled), | 2364 | .ms = @intFromBool(spirv_type.flags.is_multisampled), |
| 2026 | switch (spirv_type.flags.usage) { | 2365 | .sampled = switch (spirv_type.flags.usage) { |
| 2027 | .unknown => 1, | 2366 | .unknown => 0, |
| 2028 | .sampled => 1, | 2367 | .sampled => 1, |
| 2029 | .storage => 2, | 2368 | .storage => 2, |
| 2030 | }, | 2369 | }, |
| 2031 | switch (spirv_type.flags.format) { | 2370 | .image_format = switch (spirv_type.flags.format) { |
| 2032 | .unknown => .unknown, | 2371 | .unknown => .unknown, |
| 2033 | .rgba32f => .rgba32f, | 2372 | .rgba32f => .rgba32f, |
| 2034 | .rgba32i => .rgba32i, | 2373 | .rgba32i => .rgba32i, |
| ... | @@ -2044,31 +2383,36 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { | ... | @@ -2044,31 +2383,36 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { |
| 2044 | .r32i => .r32i, | 2383 | .r32i => .r32i, |
| 2045 | .r32u => .r32ui, | 2384 | .r32u => .r32ui, |
| 2046 | }, | 2385 | }, |
| 2047 | switch (spirv_type.flags.access) { | 2386 | .access_qualifier = switch (spirv_type.flags.access) { |
| 2048 | .unknown => null, | 2387 | .unknown => null, |
| 2049 | .read_only => .read_only, | 2388 | .read_only => .read_only, |
| 2050 | .write_only => .write_only, | 2389 | .write_only => .write_only, |
| 2051 | .read_write => .read_write, | 2390 | .read_write => .read_write, |
| 2052 | }, | 2391 | }, |
| 2053 | ); | 2392 | }); |
| 2054 | }, | 2393 | }, |
| 2055 | .sampled_image => { | 2394 | .sampled_image => { |
| 2056 | const image_ty_id = try cg.resolveType(.fromInterned(spirv_type.ty), .indirect); | 2395 | const image_ty_id = try cg.resolveType(.fromInterned(spirv_type.ty), .indirect); |
| 2057 | return try cg.module.sampledImageType(ip_index, image_ty_id); | 2396 | try cg.sections.globals.emit(gpa, .OpTypeSampledImage, .{ |
| | 2397 | .id_result = result_id, |
| | 2398 | .image_type = image_ty_id, |
| | 2399 | }); |
| 2058 | }, | 2400 | }, |
| 2059 | .runtime_array => { | 2401 | .runtime_array => { |
| 2060 | const elem_ty: Type = .fromInterned(spirv_type.ty); | 2402 | const elem_ty: Type = .fromInterned(spirv_type.ty); |
| 2061 | const elem_ty_id = try cg.resolveType(elem_ty, .indirect); | 2403 | const elem_ty_id = try cg.resolveType(elem_ty, .indirect); |
| 2062 | const result_id = try cg.module.runtimeArrayType(ip_index, elem_ty_id); | 2404 | try cg.sections.globals.emit(gpa, .OpTypeRuntimeArray, .{ |
| 2063 | | 2405 | .id_result = result_id, |
| | 2406 | .element_type = elem_ty_id, |
| | 2407 | }); |
| 2064 | if (elem_ty.hasRuntimeBits(zcu)) { | 2408 | if (elem_ty.hasRuntimeBits(zcu)) { |
| 2065 | try cg.module.decorate(result_id, .{ .array_stride = .{ | 2409 | try cg.decorate(result_id, .{ .array_stride = .{ |
| 2066 | .array_stride = @intCast(elem_ty.abiSize(zcu)), | 2410 | .array_stride = @intCast(elem_ty.abiSize(zcu)), |
| 2067 | } }); | 2411 | } }); |
| 2068 | } | 2412 | } |
| 2069 | return result_id; | | |
| 2070 | }, | 2413 | }, |
| 2071 | } | 2414 | } |
| | 2415 | return result_id; |
| 2072 | }, | 2416 | }, |
| 2073 | | 2417 | |
| 2074 | .null, | 2418 | .null, |
| ... | @@ -2099,7 +2443,7 @@ const ErrorUnionLayout = struct { | ... | @@ -2099,7 +2443,7 @@ const ErrorUnionLayout = struct { |
| 2099 | }; | 2443 | }; |
| 2100 | | 2444 | |
| 2101 | fn errorUnionLayout(cg: *CodeGen, payload_ty: Type) ErrorUnionLayout { | 2445 | fn errorUnionLayout(cg: *CodeGen, payload_ty: Type) ErrorUnionLayout { |
| 2102 | const zcu = cg.module.zcu; | 2446 | const zcu = cg.zcu; |
| 2103 | | 2447 | |
| 2104 | const error_align = Type.abiAlignment(.anyerror, zcu); | 2448 | const error_align = Type.abiAlignment(.anyerror, zcu); |
| 2105 | const payload_align = payload_ty.abiAlignment(zcu); | 2449 | const payload_align = payload_ty.abiAlignment(zcu); |
| ... | @@ -2130,7 +2474,7 @@ const UnionLayout = struct { | ... | @@ -2130,7 +2474,7 @@ const UnionLayout = struct { |
| 2130 | }; | 2474 | }; |
| 2131 | | 2475 | |
| 2132 | fn unionLayout(cg: *CodeGen, ty: Type) UnionLayout { | 2476 | fn unionLayout(cg: *CodeGen, ty: Type) UnionLayout { |
| 2133 | const zcu = cg.module.zcu; | 2477 | const zcu = cg.zcu; |
| 2134 | const ip = &zcu.intern_pool; | 2478 | const ip = &zcu.intern_pool; |
| 2135 | const layout = ty.unionGetLayout(zcu); | 2479 | const layout = ty.unionGetLayout(zcu); |
| 2136 | const union_obj = zcu.typeToUnion(ty).?; | 2480 | const union_obj = zcu.typeToUnion(ty).?; |
| ... | @@ -2220,8 +2564,8 @@ const Temporary = struct { | ... | @@ -2220,8 +2564,8 @@ const Temporary = struct { |
| 2220 | } | 2564 | } |
| 2221 | | 2565 | |
| 2222 | fn materialize(temp: Temporary, cg: *CodeGen) !Id { | 2566 | fn materialize(temp: Temporary, cg: *CodeGen) !Id { |
| 2223 | const gpa = cg.module.gpa; | 2567 | const gpa = cg.gpa; |
| 2224 | const zcu = cg.module.zcu; | 2568 | const zcu = cg.zcu; |
| 2225 | switch (temp.value) { | 2569 | switch (temp.value) { |
| 2226 | .singleton => |id| return id, | 2570 | .singleton => |id| return id, |
| 2227 | .exploded_vector => |range| { | 2571 | .exploded_vector => |range| { |
| ... | @@ -2255,7 +2599,7 @@ const Temporary = struct { | ... | @@ -2255,7 +2599,7 @@ const Temporary = struct { |
| 2255 | /// 'Explode' a temporary into separate elements. This turns a vector | 2599 | /// 'Explode' a temporary into separate elements. This turns a vector |
| 2256 | /// into a bag of elements. | 2600 | /// into a bag of elements. |
| 2257 | fn explode(temp: Temporary, cg: *CodeGen) !IdRange { | 2601 | fn explode(temp: Temporary, cg: *CodeGen) !IdRange { |
| 2258 | const zcu = cg.module.zcu; | 2602 | const zcu = cg.zcu; |
| 2259 | | 2603 | |
| 2260 | // If the value is a scalar, then this is a no-op. | 2604 | // If the value is a scalar, then this is a no-op. |
| 2261 | if (!temp.ty.isVector(zcu)) { | 2605 | if (!temp.ty.isVector(zcu)) { |
| ... | @@ -2267,7 +2611,7 @@ const Temporary = struct { | ... | @@ -2267,7 +2611,7 @@ const Temporary = struct { |
| 2267 | | 2611 | |
| 2268 | const ty_id = try cg.resolveType(temp.ty.scalarType(zcu), .direct); | 2612 | const ty_id = try cg.resolveType(temp.ty.scalarType(zcu), .direct); |
| 2269 | const n = temp.ty.vectorLen(zcu); | 2613 | const n = temp.ty.vectorLen(zcu); |
| 2270 | const results = cg.module.allocIds(n); | 2614 | const results = cg.allocIds(n); |
| 2271 | | 2615 | |
| 2272 | const id = switch (temp.value) { | 2616 | const id = switch (temp.value) { |
| 2273 | .singleton => |id| id, | 2617 | .singleton => |id| id, |
| ... | @@ -2276,7 +2620,7 @@ const Temporary = struct { | ... | @@ -2276,7 +2620,7 @@ const Temporary = struct { |
| 2276 | | 2620 | |
| 2277 | for (0..n) |i| { | 2621 | for (0..n) |i| { |
| 2278 | const indexes = [_]u32{@intCast(i)}; | 2622 | const indexes = [_]u32{@intCast(i)}; |
| 2279 | try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ | 2623 | try cg.body.emit(cg.gpa, .OpCompositeExtract, .{ |
| 2280 | .id_result_type = ty_id, | 2624 | .id_result_type = ty_id, |
| 2281 | .id_result = results.at(i), | 2625 | .id_result = results.at(i), |
| 2282 | .composite = id, | 2626 | .composite = id, |
| ... | @@ -2296,12 +2640,12 @@ const CompositeInt = struct { | ... | @@ -2296,12 +2640,12 @@ const CompositeInt = struct { |
| 2296 | info: ArithmeticTypeInfo, | 2640 | info: ArithmeticTypeInfo, |
| 2297 | | 2641 | |
| 2298 | fn init(cg: *CodeGen, composite_id: Id, info: ArithmeticTypeInfo) !CompositeInt { | 2642 | fn init(cg: *CodeGen, composite_id: Id, info: ArithmeticTypeInfo) !CompositeInt { |
| 2299 | const n_limbs: u16 = info.backing_bits / Module.big_int_bits; | 2643 | const n_limbs: u16 = info.backing_bits / big_int_bits; |
| 2300 | const gpa = cg.module.gpa; | 2644 | const gpa = cg.gpa; |
| 2301 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 2645 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 2302 | const limbs = try cg.id_scratch.addManyAsSlice(gpa, n_limbs); | 2646 | const limbs = try cg.id_scratch.addManyAsSlice(gpa, n_limbs); |
| 2303 | for (limbs, 0..) |*limb, i| { | 2647 | for (limbs, 0..) |*limb, i| { |
| 2304 | const result_id = cg.module.allocId(); | 2648 | const result_id = cg.allocId(); |
| 2305 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 2649 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2306 | .id_result_type = u32_ty_id, | 2650 | .id_result_type = u32_ty_id, |
| 2307 | .id_result = result_id, | 2651 | .id_result = result_id, |
| ... | @@ -2323,8 +2667,8 @@ const CompositeInt = struct { | ... | @@ -2323,8 +2667,8 @@ const CompositeInt = struct { |
| 2323 | } | 2667 | } |
| 2324 | | 2668 | |
| 2325 | fn zero(cg: *CodeGen, info: ArithmeticTypeInfo) !CompositeInt { | 2669 | fn zero(cg: *CodeGen, info: ArithmeticTypeInfo) !CompositeInt { |
| 2326 | const n_limbs: u16 = info.backing_bits / Module.big_int_bits; | 2670 | const n_limbs: u16 = info.backing_bits / big_int_bits; |
| 2327 | const limbs = try cg.id_scratch.addManyAsSlice(cg.module.gpa, n_limbs); | 2671 | const limbs = try cg.id_scratch.addManyAsSlice(cg.gpa, n_limbs); |
| 2328 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); | 2672 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); |
| 2329 | for (limbs) |*limb| limb.* = zero_id; | 2673 | for (limbs) |*limb| limb.* = zero_id; |
| 2330 | return .{ .cg = cg, .limbs = limbs, .n_limbs = n_limbs, .info = info }; | 2674 | return .{ .cg = cg, .limbs = limbs, .n_limbs = n_limbs, .info = info }; |
| ... | @@ -2337,9 +2681,9 @@ const CompositeInt = struct { | ... | @@ -2337,9 +2681,9 @@ const CompositeInt = struct { |
| 2337 | | 2681 | |
| 2338 | fn limbBinOp(ci: CompositeInt, opcode: Opcode, lhs: Id, rhs: Id) !Id { | 2682 | fn limbBinOp(ci: CompositeInt, opcode: Opcode, lhs: Id, rhs: Id) !Id { |
| 2339 | const cg = ci.cg; | 2683 | const cg = ci.cg; |
| 2340 | const gpa = cg.module.gpa; | 2684 | const gpa = cg.gpa; |
| 2341 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 2685 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 2342 | const result_id = cg.module.allocId(); | 2686 | const result_id = cg.allocId(); |
| 2343 | try cg.body.emitRaw(gpa, opcode, 4); | 2687 | try cg.body.emitRaw(gpa, opcode, 4); |
| 2344 | cg.body.writeOperand(Id, u32_ty_id); | 2688 | cg.body.writeOperand(Id, u32_ty_id); |
| 2345 | cg.body.writeOperand(Id, result_id); | 2689 | cg.body.writeOperand(Id, result_id); |
| ... | @@ -2350,9 +2694,9 @@ const CompositeInt = struct { | ... | @@ -2350,9 +2694,9 @@ const CompositeInt = struct { |
| 2350 | | 2694 | |
| 2351 | fn limbUnOp(ci: CompositeInt, opcode: Opcode, operand: Id) !Id { | 2695 | fn limbUnOp(ci: CompositeInt, opcode: Opcode, operand: Id) !Id { |
| 2352 | const cg = ci.cg; | 2696 | const cg = ci.cg; |
| 2353 | const gpa = cg.module.gpa; | 2697 | const gpa = cg.gpa; |
| 2354 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 2698 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 2355 | const result_id = cg.module.allocId(); | 2699 | const result_id = cg.allocId(); |
| 2356 | try cg.body.emitRaw(gpa, opcode, 3); | 2700 | try cg.body.emitRaw(gpa, opcode, 3); |
| 2357 | cg.body.writeOperand(Id, u32_ty_id); | 2701 | cg.body.writeOperand(Id, u32_ty_id); |
| 2358 | cg.body.writeOperand(Id, result_id); | 2702 | cg.body.writeOperand(Id, result_id); |
| ... | @@ -2362,7 +2706,7 @@ const CompositeInt = struct { | ... | @@ -2362,7 +2706,7 @@ const CompositeInt = struct { |
| 2362 | | 2706 | |
| 2363 | fn bitwiseOp(ci: CompositeInt, other: CompositeInt, opcode: Opcode) !CompositeInt { | 2707 | fn bitwiseOp(ci: CompositeInt, other: CompositeInt, opcode: Opcode) !CompositeInt { |
| 2364 | const cg = ci.cg; | 2708 | const cg = ci.cg; |
| 2365 | const gpa = cg.module.gpa; | 2709 | const gpa = cg.gpa; |
| 2366 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); | 2710 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); |
| 2367 | for (result_limbs, 0..) |*r, i| { | 2711 | for (result_limbs, 0..) |*r, i| { |
| 2368 | r.* = try ci.limbBinOp(opcode, ci.limbs[i], other.limbs[i]); | 2712 | r.* = try ci.limbBinOp(opcode, ci.limbs[i], other.limbs[i]); |
| ... | @@ -2372,7 +2716,7 @@ const CompositeInt = struct { | ... | @@ -2372,7 +2716,7 @@ const CompositeInt = struct { |
| 2372 | | 2716 | |
| 2373 | fn bitwiseNot(ci: CompositeInt) !CompositeInt { | 2717 | fn bitwiseNot(ci: CompositeInt) !CompositeInt { |
| 2374 | const cg = ci.cg; | 2718 | const cg = ci.cg; |
| 2375 | const gpa = cg.module.gpa; | 2719 | const gpa = cg.gpa; |
| 2376 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); | 2720 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); |
| 2377 | for (result_limbs, 0..) |*r, i| { | 2721 | for (result_limbs, 0..) |*r, i| { |
| 2378 | r.* = try ci.limbUnOp(.OpNot, ci.limbs[i]); | 2722 | r.* = try ci.limbUnOp(.OpNot, ci.limbs[i]); |
| ... | @@ -2382,41 +2726,45 @@ const CompositeInt = struct { | ... | @@ -2382,41 +2726,45 @@ const CompositeInt = struct { |
| 2382 | | 2726 | |
| 2383 | fn cmp(ci: CompositeInt, other: CompositeInt, op: std.math.CompareOperator) !Id { | 2727 | fn cmp(ci: CompositeInt, other: CompositeInt, op: std.math.CompareOperator) !Id { |
| 2384 | const cg = ci.cg; | 2728 | const cg = ci.cg; |
| 2385 | const gpa = cg.module.gpa; | 2729 | const gpa = cg.gpa; |
| 2386 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 2730 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 2387 | | 2731 | |
| 2388 | switch (op) { | 2732 | switch (op) { |
| 2389 | .eq, .neq => { | 2733 | .eq, .neq => { |
| 2390 | var result = blk: { | 2734 | var result = blk: { |
| 2391 | const r = cg.module.allocId(); | 2735 | const r = cg.allocId(); |
| 2392 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 2736 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2393 | cg.body.writeOperand(Id, bool_ty_id); | 2737 | .id_result_type = bool_ty_id, |
| 2394 | cg.body.writeOperand(Id, r); | 2738 | .id_result = r, |
| 2395 | cg.body.writeOperand(Id, ci.limbs[0]); | 2739 | .operand_1 = ci.limbs[0], |
| 2396 | cg.body.writeOperand(Id, other.limbs[0]); | 2740 | .operand_2 = other.limbs[0], |
| | 2741 | }); |
| 2397 | break :blk r; | 2742 | break :blk r; |
| 2398 | }; | 2743 | }; |
| 2399 | for (1..ci.n_limbs) |i| { | 2744 | for (1..ci.n_limbs) |i| { |
| 2400 | const limb_eq = cg.module.allocId(); | 2745 | const limb_eq = cg.allocId(); |
| 2401 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 2746 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2402 | cg.body.writeOperand(Id, bool_ty_id); | 2747 | .id_result_type = bool_ty_id, |
| 2403 | cg.body.writeOperand(Id, limb_eq); | 2748 | .id_result = limb_eq, |
| 2404 | cg.body.writeOperand(Id, ci.limbs[i]); | 2749 | .operand_1 = ci.limbs[i], |
| 2405 | cg.body.writeOperand(Id, other.limbs[i]); | 2750 | .operand_2 = other.limbs[i], |
| 2406 | const combined = cg.module.allocId(); | 2751 | }); |
| 2407 | try cg.body.emitRaw(gpa, .OpLogicalAnd, 4); | 2752 | const combined = cg.allocId(); |
| 2408 | cg.body.writeOperand(Id, bool_ty_id); | 2753 | try cg.body.emit(gpa, .OpLogicalAnd, .{ |
| 2409 | cg.body.writeOperand(Id, combined); | 2754 | .id_result_type = bool_ty_id, |
| 2410 | cg.body.writeOperand(Id, result); | 2755 | .id_result = combined, |
| 2411 | cg.body.writeOperand(Id, limb_eq); | 2756 | .operand_1 = result, |
| | 2757 | .operand_2 = limb_eq, |
| | 2758 | }); |
| 2412 | result = combined; | 2759 | result = combined; |
| 2413 | } | 2760 | } |
| 2414 | if (op == .neq) { | 2761 | if (op == .neq) { |
| 2415 | const negated = cg.module.allocId(); | 2762 | const negated = cg.allocId(); |
| 2416 | try cg.body.emitRaw(gpa, .OpLogicalNot, 3); | 2763 | try cg.body.emit(gpa, .OpLogicalNot, .{ |
| 2417 | cg.body.writeOperand(Id, bool_ty_id); | 2764 | .id_result_type = bool_ty_id, |
| 2418 | cg.body.writeOperand(Id, negated); | 2765 | .id_result = negated, |
| 2419 | cg.body.writeOperand(Id, result); | 2766 | .operand = result, |
| | 2767 | }); |
| 2420 | result = negated; | 2768 | result = negated; |
| 2421 | } | 2769 | } |
| 2422 | return result; | 2770 | return result; |
| ... | @@ -2429,12 +2777,13 @@ const CompositeInt = struct { | ... | @@ -2429,12 +2777,13 @@ const CompositeInt = struct { |
| 2429 | for (0..ci.n_limbs) |i| { | 2777 | for (0..ci.n_limbs) |i| { |
| 2430 | const l = ci.limbs[i]; | 2778 | const l = ci.limbs[i]; |
| 2431 | const r = other.limbs[i]; | 2779 | const r = other.limbs[i]; |
| 2432 | const limb_ne = cg.module.allocId(); | 2780 | const limb_ne = cg.allocId(); |
| 2433 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 2781 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 2434 | cg.body.writeOperand(Id, bool_ty_id); | 2782 | .id_result_type = bool_ty_id, |
| 2435 | cg.body.writeOperand(Id, limb_ne); | 2783 | .id_result = limb_ne, |
| 2436 | cg.body.writeOperand(Id, l); | 2784 | .operand_1 = l, |
| 2437 | cg.body.writeOperand(Id, r); | 2785 | .operand_2 = r, |
| | 2786 | }); |
| 2438 | | 2787 | |
| 2439 | const is_top = (i == ci.n_limbs - 1); | 2788 | const is_top = (i == ci.n_limbs - 1); |
| 2440 | const use_signed = is_top and ci.info.signedness == .signed; | 2789 | const use_signed = is_top and ci.info.signedness == .signed; |
| ... | @@ -2442,13 +2791,13 @@ const CompositeInt = struct { | ... | @@ -2442,13 +2791,13 @@ const CompositeInt = struct { |
| 2442 | var cmp_r = r; | 2791 | var cmp_r = r; |
| 2443 | if (use_signed) { | 2792 | if (use_signed) { |
| 2444 | const i32_ty_id = try cg.resolveType(.i32, .direct); | 2793 | const i32_ty_id = try cg.resolveType(.i32, .direct); |
| 2445 | const sl = cg.module.allocId(); | 2794 | const sl = cg.allocId(); |
| 2446 | try cg.body.emit(gpa, .OpBitcast, .{ | 2795 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2447 | .id_result_type = i32_ty_id, | 2796 | .id_result_type = i32_ty_id, |
| 2448 | .id_result = sl, | 2797 | .id_result = sl, |
| 2449 | .operand = l, | 2798 | .operand = l, |
| 2450 | }); | 2799 | }); |
| 2451 | const sr = cg.module.allocId(); | 2800 | const sr = cg.allocId(); |
| 2452 | try cg.body.emit(gpa, .OpBitcast, .{ | 2801 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2453 | .id_result_type = i32_ty_id, | 2802 | .id_result_type = i32_ty_id, |
| 2454 | .id_result = sr, | 2803 | .id_result = sr, |
| ... | @@ -2463,14 +2812,14 @@ const CompositeInt = struct { | ... | @@ -2463,14 +2812,14 @@ const CompositeInt = struct { |
| 2463 | else | 2812 | else |
| 2464 | (if (use_signed) .OpSGreaterThan else .OpUGreaterThan); | 2813 | (if (use_signed) .OpSGreaterThan else .OpUGreaterThan); |
| 2465 | | 2814 | |
| 2466 | const limb_cmp = cg.module.allocId(); | 2815 | const limb_cmp = cg.allocId(); |
| 2467 | try cg.body.emitRaw(gpa, cmp_opcode, 4); | 2816 | try cg.body.emitRaw(gpa, cmp_opcode, 4); |
| 2468 | cg.body.writeOperand(Id, bool_ty_id); | 2817 | cg.body.writeOperand(Id, bool_ty_id); |
| 2469 | cg.body.writeOperand(Id, limb_cmp); | 2818 | cg.body.writeOperand(Id, limb_cmp); |
| 2470 | cg.body.writeOperand(Id, cmp_l); | 2819 | cg.body.writeOperand(Id, cmp_l); |
| 2471 | cg.body.writeOperand(Id, cmp_r); | 2820 | cg.body.writeOperand(Id, cmp_r); |
| 2472 | | 2821 | |
| 2473 | const selected = cg.module.allocId(); | 2822 | const selected = cg.allocId(); |
| 2474 | try cg.body.emit(gpa, .OpSelect, .{ | 2823 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2475 | .id_result_type = bool_ty_id, | 2824 | .id_result_type = bool_ty_id, |
| 2476 | .id_result = selected, | 2825 | .id_result = selected, |
| ... | @@ -2487,9 +2836,9 @@ const CompositeInt = struct { | ... | @@ -2487,9 +2836,9 @@ const CompositeInt = struct { |
| 2487 | | 2836 | |
| 2488 | fn addSub(ci: CompositeInt, other: CompositeInt, comptime is_add: bool) !CompositeInt { | 2837 | fn addSub(ci: CompositeInt, other: CompositeInt, comptime is_add: bool) !CompositeInt { |
| 2489 | const cg = ci.cg; | 2838 | const cg = ci.cg; |
| 2490 | const gpa = cg.module.gpa; | 2839 | const gpa = cg.gpa; |
| 2491 | const pt = cg.pt; | 2840 | const pt = cg.pt; |
| 2492 | const zcu = cg.module.zcu; | 2841 | const zcu = cg.zcu; |
| 2493 | const ip = &zcu.intern_pool; | 2842 | const ip = &zcu.intern_pool; |
| 2494 | const comp = zcu.comp; | 2843 | const comp = zcu.comp; |
| 2495 | const io = comp.io; | 2844 | const io = comp.io; |
| ... | @@ -2508,21 +2857,21 @@ const CompositeInt = struct { | ... | @@ -2508,21 +2857,21 @@ const CompositeInt = struct { |
| 2508 | const opcode: Opcode = if (is_add) .OpIAddCarry else .OpISubBorrow; | 2857 | const opcode: Opcode = if (is_add) .OpIAddCarry else .OpISubBorrow; |
| 2509 | | 2858 | |
| 2510 | for (0..ci.n_limbs) |i| { | 2859 | for (0..ci.n_limbs) |i| { |
| 2511 | const op1 = cg.module.allocId(); | 2860 | const op1 = cg.allocId(); |
| 2512 | try cg.body.emitRaw(gpa, opcode, 4); | 2861 | try cg.body.emitRaw(gpa, opcode, 4); |
| 2513 | cg.body.writeOperand(Id, carry_struct_ty_id); | 2862 | cg.body.writeOperand(Id, carry_struct_ty_id); |
| 2514 | cg.body.writeOperand(Id, op1); | 2863 | cg.body.writeOperand(Id, op1); |
| 2515 | cg.body.writeOperand(Id, ci.limbs[i]); | 2864 | cg.body.writeOperand(Id, ci.limbs[i]); |
| 2516 | cg.body.writeOperand(Id, other.limbs[i]); | 2865 | cg.body.writeOperand(Id, other.limbs[i]); |
| 2517 | | 2866 | |
| 2518 | const sum1 = cg.module.allocId(); | 2867 | const sum1 = cg.allocId(); |
| 2519 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 2868 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2520 | .id_result_type = u32_ty_id, | 2869 | .id_result_type = u32_ty_id, |
| 2521 | .id_result = sum1, | 2870 | .id_result = sum1, |
| 2522 | .composite = op1, | 2871 | .composite = op1, |
| 2523 | .indexes = &.{0}, | 2872 | .indexes = &.{0}, |
| 2524 | }); | 2873 | }); |
| 2525 | const carry1 = cg.module.allocId(); | 2874 | const carry1 = cg.allocId(); |
| 2526 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 2875 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2527 | .id_result_type = u32_ty_id, | 2876 | .id_result_type = u32_ty_id, |
| 2528 | .id_result = carry1, | 2877 | .id_result = carry1, |
| ... | @@ -2530,21 +2879,21 @@ const CompositeInt = struct { | ... | @@ -2530,21 +2879,21 @@ const CompositeInt = struct { |
| 2530 | .indexes = &.{1}, | 2879 | .indexes = &.{1}, |
| 2531 | }); | 2880 | }); |
| 2532 | | 2881 | |
| 2533 | const op2 = cg.module.allocId(); | 2882 | const op2 = cg.allocId(); |
| 2534 | try cg.body.emitRaw(gpa, opcode, 4); | 2883 | try cg.body.emitRaw(gpa, opcode, 4); |
| 2535 | cg.body.writeOperand(Id, carry_struct_ty_id); | 2884 | cg.body.writeOperand(Id, carry_struct_ty_id); |
| 2536 | cg.body.writeOperand(Id, op2); | 2885 | cg.body.writeOperand(Id, op2); |
| 2537 | cg.body.writeOperand(Id, sum1); | 2886 | cg.body.writeOperand(Id, sum1); |
| 2538 | cg.body.writeOperand(Id, carry_id); | 2887 | cg.body.writeOperand(Id, carry_id); |
| 2539 | | 2888 | |
| 2540 | result_limbs[i] = cg.module.allocId(); | 2889 | result_limbs[i] = cg.allocId(); |
| 2541 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 2890 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2542 | .id_result_type = u32_ty_id, | 2891 | .id_result_type = u32_ty_id, |
| 2543 | .id_result = result_limbs[i], | 2892 | .id_result = result_limbs[i], |
| 2544 | .composite = op2, | 2893 | .composite = op2, |
| 2545 | .indexes = &.{0}, | 2894 | .indexes = &.{0}, |
| 2546 | }); | 2895 | }); |
| 2547 | const carry2 = cg.module.allocId(); | 2896 | const carry2 = cg.allocId(); |
| 2548 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 2897 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2549 | .id_result_type = u32_ty_id, | 2898 | .id_result_type = u32_ty_id, |
| 2550 | .id_result = carry2, | 2899 | .id_result = carry2, |
| ... | @@ -2560,7 +2909,7 @@ const CompositeInt = struct { | ... | @@ -2560,7 +2909,7 @@ const CompositeInt = struct { |
| 2560 | | 2909 | |
| 2561 | fn shl(ci: CompositeInt, shift_amt_id: Id) !CompositeInt { | 2910 | fn shl(ci: CompositeInt, shift_amt_id: Id) !CompositeInt { |
| 2562 | const cg = ci.cg; | 2911 | const cg = ci.cg; |
| 2563 | const gpa = cg.module.gpa; | 2912 | const gpa = cg.gpa; |
| 2564 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 2913 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 2565 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 2914 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 2566 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); | 2915 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); |
| ... | @@ -2572,12 +2921,13 @@ const CompositeInt = struct { | ... | @@ -2572,12 +2921,13 @@ const CompositeInt = struct { |
| 2572 | const frac = try ci.limbBinOp(.OpBitwiseAnd, shift_amt_id, thirty_one_id); | 2921 | const frac = try ci.limbBinOp(.OpBitwiseAnd, shift_amt_id, thirty_one_id); |
| 2573 | const comp_frac = try ci.limbBinOp(.OpISub, thirty_two_id, frac); | 2922 | const comp_frac = try ci.limbBinOp(.OpISub, thirty_two_id, frac); |
| 2574 | const frac_is_zero = blk: { | 2923 | const frac_is_zero = blk: { |
| 2575 | const r = cg.module.allocId(); | 2924 | const r = cg.allocId(); |
| 2576 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 2925 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2577 | cg.body.writeOperand(Id, bool_ty_id); | 2926 | .id_result_type = bool_ty_id, |
| 2578 | cg.body.writeOperand(Id, r); | 2927 | .id_result = r, |
| 2579 | cg.body.writeOperand(Id, frac); | 2928 | .operand_1 = frac, |
| 2580 | cg.body.writeOperand(Id, zero_id); | 2929 | .operand_2 = zero_id, |
| | 2930 | }); |
| 2581 | break :blk r; | 2931 | break :blk r; |
| 2582 | }; | 2932 | }; |
| 2583 | | 2933 | |
| ... | @@ -2593,17 +2943,18 @@ const CompositeInt = struct { | ... | @@ -2593,17 +2943,18 @@ const CompositeInt = struct { |
| 2593 | const j_plus_whole = try ci.limbBinOp(.OpIAdd, j_id, whole); | 2943 | const j_plus_whole = try ci.limbBinOp(.OpIAdd, j_id, whole); |
| 2594 | | 2944 | |
| 2595 | const is_main = blk: { | 2945 | const is_main = blk: { |
| 2596 | const r = cg.module.allocId(); | 2946 | const r = cg.allocId(); |
| 2597 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 2947 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2598 | cg.body.writeOperand(Id, bool_ty_id); | 2948 | .id_result_type = bool_ty_id, |
| 2599 | cg.body.writeOperand(Id, r); | 2949 | .id_result = r, |
| 2600 | cg.body.writeOperand(Id, j_plus_whole); | 2950 | .operand_1 = j_plus_whole, |
| 2601 | cg.body.writeOperand(Id, i_id); | 2951 | .operand_2 = i_id, |
| | 2952 | }); |
| 2602 | break :blk r; | 2953 | break :blk r; |
| 2603 | }; | 2954 | }; |
| 2604 | const shifted = try ci.limbBinOp(.OpShiftLeftLogical, ci.limbs[j], frac); | 2955 | const shifted = try ci.limbBinOp(.OpShiftLeftLogical, ci.limbs[j], frac); |
| 2605 | main_val = blk: { | 2956 | main_val = blk: { |
| 2606 | const r = cg.module.allocId(); | 2957 | const r = cg.allocId(); |
| 2607 | try cg.body.emit(gpa, .OpSelect, .{ | 2958 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2608 | .id_result_type = u32_ty_id, | 2959 | .id_result_type = u32_ty_id, |
| 2609 | .id_result = r, | 2960 | .id_result = r, |
| ... | @@ -2617,17 +2968,18 @@ const CompositeInt = struct { | ... | @@ -2617,17 +2968,18 @@ const CompositeInt = struct { |
| 2617 | const one_id = try cg.constInt(.u32, @as(u32, 1)); | 2968 | const one_id = try cg.constInt(.u32, @as(u32, 1)); |
| 2618 | const j_plus_whole_plus_1 = try ci.limbBinOp(.OpIAdd, j_plus_whole, one_id); | 2969 | const j_plus_whole_plus_1 = try ci.limbBinOp(.OpIAdd, j_plus_whole, one_id); |
| 2619 | const is_carry = blk: { | 2970 | const is_carry = blk: { |
| 2620 | const r = cg.module.allocId(); | 2971 | const r = cg.allocId(); |
| 2621 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 2972 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2622 | cg.body.writeOperand(Id, bool_ty_id); | 2973 | .id_result_type = bool_ty_id, |
| 2623 | cg.body.writeOperand(Id, r); | 2974 | .id_result = r, |
| 2624 | cg.body.writeOperand(Id, j_plus_whole_plus_1); | 2975 | .operand_1 = j_plus_whole_plus_1, |
| 2625 | cg.body.writeOperand(Id, i_id); | 2976 | .operand_2 = i_id, |
| | 2977 | }); |
| 2626 | break :blk r; | 2978 | break :blk r; |
| 2627 | }; | 2979 | }; |
| 2628 | const carry_shifted = try ci.limbBinOp(.OpShiftRightLogical, ci.limbs[j], comp_frac); | 2980 | const carry_shifted = try ci.limbBinOp(.OpShiftRightLogical, ci.limbs[j], comp_frac); |
| 2629 | const guarded_carry = blk: { | 2981 | const guarded_carry = blk: { |
| 2630 | const r = cg.module.allocId(); | 2982 | const r = cg.allocId(); |
| 2631 | try cg.body.emit(gpa, .OpSelect, .{ | 2983 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2632 | .id_result_type = u32_ty_id, | 2984 | .id_result_type = u32_ty_id, |
| 2633 | .id_result = r, | 2985 | .id_result = r, |
| ... | @@ -2638,7 +2990,7 @@ const CompositeInt = struct { | ... | @@ -2638,7 +2990,7 @@ const CompositeInt = struct { |
| 2638 | break :blk r; | 2990 | break :blk r; |
| 2639 | }; | 2991 | }; |
| 2640 | carry_val = blk: { | 2992 | carry_val = blk: { |
| 2641 | const r = cg.module.allocId(); | 2993 | const r = cg.allocId(); |
| 2642 | try cg.body.emit(gpa, .OpSelect, .{ | 2994 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2643 | .id_result_type = u32_ty_id, | 2995 | .id_result_type = u32_ty_id, |
| 2644 | .id_result = r, | 2996 | .id_result = r, |
| ... | @@ -2658,7 +3010,7 @@ const CompositeInt = struct { | ... | @@ -2658,7 +3010,7 @@ const CompositeInt = struct { |
| 2658 | | 3010 | |
| 2659 | fn shr(ci: CompositeInt, shift_amt_id: Id, comptime is_arithmetic: bool) !CompositeInt { | 3011 | fn shr(ci: CompositeInt, shift_amt_id: Id, comptime is_arithmetic: bool) !CompositeInt { |
| 2660 | const cg = ci.cg; | 3012 | const cg = ci.cg; |
| 2661 | const gpa = cg.module.gpa; | 3013 | const gpa = cg.gpa; |
| 2662 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 3014 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 2663 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 3015 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 2664 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); | 3016 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); |
| ... | @@ -2670,31 +3022,33 @@ const CompositeInt = struct { | ... | @@ -2670,31 +3022,33 @@ const CompositeInt = struct { |
| 2670 | const frac = try ci.limbBinOp(.OpBitwiseAnd, shift_amt_id, thirty_one_id); | 3022 | const frac = try ci.limbBinOp(.OpBitwiseAnd, shift_amt_id, thirty_one_id); |
| 2671 | const comp_frac = try ci.limbBinOp(.OpISub, thirty_two_id, frac); | 3023 | const comp_frac = try ci.limbBinOp(.OpISub, thirty_two_id, frac); |
| 2672 | const frac_is_zero = blk: { | 3024 | const frac_is_zero = blk: { |
| 2673 | const r = cg.module.allocId(); | 3025 | const r = cg.allocId(); |
| 2674 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 3026 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2675 | cg.body.writeOperand(Id, bool_ty_id); | 3027 | .id_result_type = bool_ty_id, |
| 2676 | cg.body.writeOperand(Id, r); | 3028 | .id_result = r, |
| 2677 | cg.body.writeOperand(Id, frac); | 3029 | .operand_1 = frac, |
| 2678 | cg.body.writeOperand(Id, zero_id); | 3030 | .operand_2 = zero_id, |
| | 3031 | }); |
| 2679 | break :blk r; | 3032 | break :blk r; |
| 2680 | }; | 3033 | }; |
| 2681 | | 3034 | |
| 2682 | const fill_id = if (is_arithmetic) blk: { | 3035 | const fill_id = if (is_arithmetic) blk: { |
| 2683 | const i32_ty_id = try cg.resolveType(.i32, .direct); | 3036 | const i32_ty_id = try cg.resolveType(.i32, .direct); |
| 2684 | const msb_signed = cg.module.allocId(); | 3037 | const msb_signed = cg.allocId(); |
| 2685 | try cg.body.emit(gpa, .OpBitcast, .{ | 3038 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2686 | .id_result_type = i32_ty_id, | 3039 | .id_result_type = i32_ty_id, |
| 2687 | .id_result = msb_signed, | 3040 | .id_result = msb_signed, |
| 2688 | .operand = ci.limbs[ci.n_limbs - 1], | 3041 | .operand = ci.limbs[ci.n_limbs - 1], |
| 2689 | }); | 3042 | }); |
| 2690 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); | 3043 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); |
| 2691 | const sign_ext = cg.module.allocId(); | 3044 | const sign_ext = cg.allocId(); |
| 2692 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 3045 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 2693 | cg.body.writeOperand(Id, i32_ty_id); | 3046 | .id_result_type = i32_ty_id, |
| 2694 | cg.body.writeOperand(Id, sign_ext); | 3047 | .id_result = sign_ext, |
| 2695 | cg.body.writeOperand(Id, msb_signed); | 3048 | .base = msb_signed, |
| 2696 | cg.body.writeOperand(Id, shift31); | 3049 | .shift = shift31, |
| 2697 | const back = cg.module.allocId(); | 3050 | }); |
| | 3051 | const back = cg.allocId(); |
| 2698 | try cg.body.emit(gpa, .OpBitcast, .{ | 3052 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2699 | .id_result_type = u32_ty_id, | 3053 | .id_result_type = u32_ty_id, |
| 2700 | .id_result = back, | 3054 | .id_result = back, |
| ... | @@ -2707,7 +3061,7 @@ const CompositeInt = struct { | ... | @@ -2707,7 +3061,7 @@ const CompositeInt = struct { |
| 2707 | | 3061 | |
| 2708 | const arith_carry_init = if (is_arithmetic) blk: { | 3062 | const arith_carry_init = if (is_arithmetic) blk: { |
| 2709 | const shifted_fill = try ci.limbBinOp(.OpShiftLeftLogical, fill_id, comp_frac); | 3063 | const shifted_fill = try ci.limbBinOp(.OpShiftLeftLogical, fill_id, comp_frac); |
| 2710 | const guarded = cg.module.allocId(); | 3064 | const guarded = cg.allocId(); |
| 2711 | try cg.body.emit(gpa, .OpSelect, .{ | 3065 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2712 | .id_result_type = u32_ty_id, | 3066 | .id_result_type = u32_ty_id, |
| 2713 | .id_result = guarded, | 3067 | .id_result = guarded, |
| ... | @@ -2727,17 +3081,18 @@ const CompositeInt = struct { | ... | @@ -2727,17 +3081,18 @@ const CompositeInt = struct { |
| 2727 | const j_id = try cg.constInt(.u32, @as(u32, @intCast(j))); | 3081 | const j_id = try cg.constInt(.u32, @as(u32, @intCast(j))); |
| 2728 | const i_plus_whole = try ci.limbBinOp(.OpIAdd, i_id, whole); | 3082 | const i_plus_whole = try ci.limbBinOp(.OpIAdd, i_id, whole); |
| 2729 | const is_main = blk: { | 3083 | const is_main = blk: { |
| 2730 | const r = cg.module.allocId(); | 3084 | const r = cg.allocId(); |
| 2731 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 3085 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2732 | cg.body.writeOperand(Id, bool_ty_id); | 3086 | .id_result_type = bool_ty_id, |
| 2733 | cg.body.writeOperand(Id, r); | 3087 | .id_result = r, |
| 2734 | cg.body.writeOperand(Id, j_id); | 3088 | .operand_1 = j_id, |
| 2735 | cg.body.writeOperand(Id, i_plus_whole); | 3089 | .operand_2 = i_plus_whole, |
| | 3090 | }); |
| 2736 | break :blk r; | 3091 | break :blk r; |
| 2737 | }; | 3092 | }; |
| 2738 | const shifted = try ci.limbBinOp(.OpShiftRightLogical, ci.limbs[j], frac); | 3093 | const shifted = try ci.limbBinOp(.OpShiftRightLogical, ci.limbs[j], frac); |
| 2739 | main_val = blk: { | 3094 | main_val = blk: { |
| 2740 | const r = cg.module.allocId(); | 3095 | const r = cg.allocId(); |
| 2741 | try cg.body.emit(gpa, .OpSelect, .{ | 3096 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2742 | .id_result_type = u32_ty_id, | 3097 | .id_result_type = u32_ty_id, |
| 2743 | .id_result = r, | 3098 | .id_result = r, |
| ... | @@ -2751,17 +3106,18 @@ const CompositeInt = struct { | ... | @@ -2751,17 +3106,18 @@ const CompositeInt = struct { |
| 2751 | const one_id = try cg.constInt(.u32, @as(u32, 1)); | 3106 | const one_id = try cg.constInt(.u32, @as(u32, 1)); |
| 2752 | const i_plus_whole_plus_1 = try ci.limbBinOp(.OpIAdd, i_plus_whole, one_id); | 3107 | const i_plus_whole_plus_1 = try ci.limbBinOp(.OpIAdd, i_plus_whole, one_id); |
| 2753 | const is_carry = blk: { | 3108 | const is_carry = blk: { |
| 2754 | const r = cg.module.allocId(); | 3109 | const r = cg.allocId(); |
| 2755 | try cg.body.emitRaw(gpa, .OpIEqual, 4); | 3110 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 2756 | cg.body.writeOperand(Id, bool_ty_id); | 3111 | .id_result_type = bool_ty_id, |
| 2757 | cg.body.writeOperand(Id, r); | 3112 | .id_result = r, |
| 2758 | cg.body.writeOperand(Id, j_id); | 3113 | .operand_1 = j_id, |
| 2759 | cg.body.writeOperand(Id, i_plus_whole_plus_1); | 3114 | .operand_2 = i_plus_whole_plus_1, |
| | 3115 | }); |
| 2760 | break :blk r; | 3116 | break :blk r; |
| 2761 | }; | 3117 | }; |
| 2762 | const carry_shifted = try ci.limbBinOp(.OpShiftLeftLogical, ci.limbs[j], comp_frac); | 3118 | const carry_shifted = try ci.limbBinOp(.OpShiftLeftLogical, ci.limbs[j], comp_frac); |
| 2763 | const guarded_carry = blk: { | 3119 | const guarded_carry = blk: { |
| 2764 | const r = cg.module.allocId(); | 3120 | const r = cg.allocId(); |
| 2765 | try cg.body.emit(gpa, .OpSelect, .{ | 3121 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2766 | .id_result_type = u32_ty_id, | 3122 | .id_result_type = u32_ty_id, |
| 2767 | .id_result = r, | 3123 | .id_result = r, |
| ... | @@ -2772,7 +3128,7 @@ const CompositeInt = struct { | ... | @@ -2772,7 +3128,7 @@ const CompositeInt = struct { |
| 2772 | break :blk r; | 3128 | break :blk r; |
| 2773 | }; | 3129 | }; |
| 2774 | carry_val = blk: { | 3130 | carry_val = blk: { |
| 2775 | const r = cg.module.allocId(); | 3131 | const r = cg.allocId(); |
| 2776 | try cg.body.emit(gpa, .OpSelect, .{ | 3132 | try cg.body.emit(gpa, .OpSelect, .{ |
| 2777 | .id_result_type = u32_ty_id, | 3133 | .id_result_type = u32_ty_id, |
| 2778 | .id_result = r, | 3134 | .id_result = r, |
| ... | @@ -2792,9 +3148,9 @@ const CompositeInt = struct { | ... | @@ -2792,9 +3148,9 @@ const CompositeInt = struct { |
| 2792 | | 3148 | |
| 2793 | fn mul(ci: CompositeInt, other: CompositeInt, comptime wide: bool) ![]Id { | 3149 | fn mul(ci: CompositeInt, other: CompositeInt, comptime wide: bool) ![]Id { |
| 2794 | const cg = ci.cg; | 3150 | const cg = ci.cg; |
| 2795 | const gpa = cg.module.gpa; | 3151 | const gpa = cg.gpa; |
| 2796 | const pt = cg.pt; | 3152 | const pt = cg.pt; |
| 2797 | const zcu = cg.module.zcu; | 3153 | const zcu = cg.zcu; |
| 2798 | const ip = &zcu.intern_pool; | 3154 | const ip = &zcu.intern_pool; |
| 2799 | const comp = zcu.comp; | 3155 | const comp = zcu.comp; |
| 2800 | const io = comp.io; | 3156 | const io = comp.io; |
| ... | @@ -2825,15 +3181,16 @@ const CompositeInt = struct { | ... | @@ -2825,15 +3181,16 @@ const CompositeInt = struct { |
| 2825 | var hi: Id = undefined; | 3181 | var hi: Id = undefined; |
| 2826 | switch (target.os.tag) { | 3182 | switch (target.os.tag) { |
| 2827 | .opencl => { | 3183 | .opencl => { |
| 2828 | lo = cg.module.allocId(); | 3184 | lo = cg.allocId(); |
| 2829 | try cg.body.emitRaw(gpa, .OpIMul, 4); | 3185 | try cg.body.emit(gpa, .OpIMul, .{ |
| 2830 | cg.body.writeOperand(Id, u32_ty_id); | 3186 | .id_result_type = u32_ty_id, |
| 2831 | cg.body.writeOperand(Id, lo); | 3187 | .id_result = lo, |
| 2832 | cg.body.writeOperand(Id, ci.limbs[i]); | 3188 | .operand_1 = ci.limbs[i], |
| 2833 | cg.body.writeOperand(Id, other.limbs[j]); | 3189 | .operand_2 = other.limbs[j], |
| | 3190 | }); |
| 2834 | | 3191 | |
| 2835 | const set = try cg.importExtendedSet(); | 3192 | const set = try cg.importExtendedSet(); |
| 2836 | hi = cg.module.allocId(); | 3193 | hi = cg.allocId(); |
| 2837 | try cg.body.emit(gpa, .OpExtInst, .{ | 3194 | try cg.body.emit(gpa, .OpExtInst, .{ |
| 2838 | .id_result_type = u32_ty_id, | 3195 | .id_result_type = u32_ty_id, |
| 2839 | .id_result = hi, | 3196 | .id_result = hi, |
| ... | @@ -2843,21 +3200,22 @@ const CompositeInt = struct { | ... | @@ -2843,21 +3200,22 @@ const CompositeInt = struct { |
| 2843 | }); | 3200 | }); |
| 2844 | }, | 3201 | }, |
| 2845 | else => { | 3202 | else => { |
| 2846 | const mul_result = cg.module.allocId(); | 3203 | const mul_result = cg.allocId(); |
| 2847 | try cg.body.emitRaw(gpa, .OpUMulExtended, 4); | 3204 | try cg.body.emit(gpa, .OpUMulExtended, .{ |
| 2848 | cg.body.writeOperand(Id, pair_struct_ty_id); | 3205 | .id_result_type = pair_struct_ty_id, |
| 2849 | cg.body.writeOperand(Id, mul_result); | 3206 | .id_result = mul_result, |
| 2850 | cg.body.writeOperand(Id, ci.limbs[i]); | 3207 | .operand_1 = ci.limbs[i], |
| 2851 | cg.body.writeOperand(Id, other.limbs[j]); | 3208 | .operand_2 = other.limbs[j], |
| 2852 | | 3209 | }); |
| 2853 | lo = cg.module.allocId(); | 3210 | |
| | 3211 | lo = cg.allocId(); |
| 2854 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3212 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2855 | .id_result_type = u32_ty_id, | 3213 | .id_result_type = u32_ty_id, |
| 2856 | .id_result = lo, | 3214 | .id_result = lo, |
| 2857 | .composite = mul_result, | 3215 | .composite = mul_result, |
| 2858 | .indexes = &.{0}, | 3216 | .indexes = &.{0}, |
| 2859 | }); | 3217 | }); |
| 2860 | hi = cg.module.allocId(); | 3218 | hi = cg.allocId(); |
| 2861 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3219 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2862 | .id_result_type = u32_ty_id, | 3220 | .id_result_type = u32_ty_id, |
| 2863 | .id_result = hi, | 3221 | .id_result = hi, |
| ... | @@ -2867,21 +3225,22 @@ const CompositeInt = struct { | ... | @@ -2867,21 +3225,22 @@ const CompositeInt = struct { |
| 2867 | }, | 3225 | }, |
| 2868 | } | 3226 | } |
| 2869 | | 3227 | |
| 2870 | const add1 = cg.module.allocId(); | 3228 | const add1 = cg.allocId(); |
| 2871 | try cg.body.emitRaw(gpa, .OpIAddCarry, 4); | 3229 | try cg.body.emit(gpa, .OpIAddCarry, .{ |
| 2872 | cg.body.writeOperand(Id, pair_struct_ty_id); | 3230 | .id_result_type = pair_struct_ty_id, |
| 2873 | cg.body.writeOperand(Id, add1); | 3231 | .id_result = add1, |
| 2874 | cg.body.writeOperand(Id, result_limbs[k]); | 3232 | .operand_1 = result_limbs[k], |
| 2875 | cg.body.writeOperand(Id, lo); | 3233 | .operand_2 = lo, |
| | 3234 | }); |
| 2876 | | 3235 | |
| 2877 | const sum1 = cg.module.allocId(); | 3236 | const sum1 = cg.allocId(); |
| 2878 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3237 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2879 | .id_result_type = u32_ty_id, | 3238 | .id_result_type = u32_ty_id, |
| 2880 | .id_result = sum1, | 3239 | .id_result = sum1, |
| 2881 | .composite = add1, | 3240 | .composite = add1, |
| 2882 | .indexes = &.{0}, | 3241 | .indexes = &.{0}, |
| 2883 | }); | 3242 | }); |
| 2884 | const c1 = cg.module.allocId(); | 3243 | const c1 = cg.allocId(); |
| 2885 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3244 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2886 | .id_result_type = u32_ty_id, | 3245 | .id_result_type = u32_ty_id, |
| 2887 | .id_result = c1, | 3246 | .id_result = c1, |
| ... | @@ -2889,21 +3248,22 @@ const CompositeInt = struct { | ... | @@ -2889,21 +3248,22 @@ const CompositeInt = struct { |
| 2889 | .indexes = &.{1}, | 3248 | .indexes = &.{1}, |
| 2890 | }); | 3249 | }); |
| 2891 | | 3250 | |
| 2892 | const add2 = cg.module.allocId(); | 3251 | const add2 = cg.allocId(); |
| 2893 | try cg.body.emitRaw(gpa, .OpIAddCarry, 4); | 3252 | try cg.body.emit(gpa, .OpIAddCarry, .{ |
| 2894 | cg.body.writeOperand(Id, pair_struct_ty_id); | 3253 | .id_result_type = pair_struct_ty_id, |
| 2895 | cg.body.writeOperand(Id, add2); | 3254 | .id_result = add2, |
| 2896 | cg.body.writeOperand(Id, sum1); | 3255 | .operand_1 = sum1, |
| 2897 | cg.body.writeOperand(Id, carry_id); | 3256 | .operand_2 = carry_id, |
| | 3257 | }); |
| 2898 | | 3258 | |
| 2899 | result_limbs[k] = cg.module.allocId(); | 3259 | result_limbs[k] = cg.allocId(); |
| 2900 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3260 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2901 | .id_result_type = u32_ty_id, | 3261 | .id_result_type = u32_ty_id, |
| 2902 | .id_result = result_limbs[k], | 3262 | .id_result = result_limbs[k], |
| 2903 | .composite = add2, | 3263 | .composite = add2, |
| 2904 | .indexes = &.{0}, | 3264 | .indexes = &.{0}, |
| 2905 | }); | 3265 | }); |
| 2906 | const c2 = cg.module.allocId(); | 3266 | const c2 = cg.allocId(); |
| 2907 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 3267 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 2908 | .id_result_type = u32_ty_id, | 3268 | .id_result_type = u32_ty_id, |
| 2909 | .id_result = c2, | 3269 | .id_result = c2, |
| ... | @@ -2925,8 +3285,8 @@ const CompositeInt = struct { | ... | @@ -2925,8 +3285,8 @@ const CompositeInt = struct { |
| 2925 | fn normalize(ci: CompositeInt) !CompositeInt { | 3285 | fn normalize(ci: CompositeInt) !CompositeInt { |
| 2926 | if (ci.info.bits == ci.info.backing_bits) return ci; | 3286 | if (ci.info.bits == ci.info.backing_bits) return ci; |
| 2927 | const cg = ci.cg; | 3287 | const cg = ci.cg; |
| 2928 | const gpa = cg.module.gpa; | 3288 | const gpa = cg.gpa; |
| 2929 | const top_bits: u16 = ci.info.bits % Module.big_int_bits; | 3289 | const top_bits: u16 = ci.info.bits % big_int_bits; |
| 2930 | assert(top_bits != 0); | 3290 | assert(top_bits != 0); |
| 2931 | | 3291 | |
| 2932 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); | 3292 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, ci.n_limbs); |
| ... | @@ -2947,25 +3307,27 @@ const CompositeInt = struct { | ... | @@ -2947,25 +3307,27 @@ const CompositeInt = struct { |
| 2947 | const shift_amt: u32 = 32 - top_bits; | 3307 | const shift_amt: u32 = 32 - top_bits; |
| 2948 | const shift_id = try cg.constInt(.u32, shift_amt); | 3308 | const shift_id = try cg.constInt(.u32, shift_amt); |
| 2949 | | 3309 | |
| 2950 | const as_signed = cg.module.allocId(); | 3310 | const as_signed = cg.allocId(); |
| 2951 | try cg.body.emit(gpa, .OpBitcast, .{ | 3311 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2952 | .id_result_type = i32_ty_id, | 3312 | .id_result_type = i32_ty_id, |
| 2953 | .id_result = as_signed, | 3313 | .id_result = as_signed, |
| 2954 | .operand = top_limb, | 3314 | .operand = top_limb, |
| 2955 | }); | 3315 | }); |
| 2956 | const shifted_left = cg.module.allocId(); | 3316 | const shifted_left = cg.allocId(); |
| 2957 | try cg.body.emitRaw(gpa, .OpShiftLeftLogical, 4); | 3317 | try cg.body.emit(gpa, .OpShiftLeftLogical, .{ |
| 2958 | cg.body.writeOperand(Id, i32_ty_id); | 3318 | .id_result_type = i32_ty_id, |
| 2959 | cg.body.writeOperand(Id, shifted_left); | 3319 | .id_result = shifted_left, |
| 2960 | cg.body.writeOperand(Id, as_signed); | 3320 | .base = as_signed, |
| 2961 | cg.body.writeOperand(Id, shift_id); | 3321 | .shift = shift_id, |
| 2962 | const shifted_right = cg.module.allocId(); | 3322 | }); |
| 2963 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 3323 | const shifted_right = cg.allocId(); |
| 2964 | cg.body.writeOperand(Id, i32_ty_id); | 3324 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 2965 | cg.body.writeOperand(Id, shifted_right); | 3325 | .id_result_type = i32_ty_id, |
| 2966 | cg.body.writeOperand(Id, shifted_left); | 3326 | .id_result = shifted_right, |
| 2967 | cg.body.writeOperand(Id, shift_id); | 3327 | .base = shifted_left, |
| 2968 | const back = cg.module.allocId(); | 3328 | .shift = shift_id, |
| | 3329 | }); |
| | 3330 | const back = cg.allocId(); |
| 2969 | try cg.body.emit(gpa, .OpBitcast, .{ | 3331 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 2970 | .id_result_type = u32_ty_id, | 3332 | .id_result_type = u32_ty_id, |
| 2971 | .id_result = back, | 3333 | .id_result = back, |
| ... | @@ -3000,7 +3362,7 @@ const Vectorization = union(enum) { | ... | @@ -3000,7 +3362,7 @@ const Vectorization = union(enum) { |
| 3000 | | 3362 | |
| 3001 | /// Derive a vectorization from a particular type | 3363 | /// Derive a vectorization from a particular type |
| 3002 | fn fromType(ty: Type, cg: *CodeGen) Vectorization { | 3364 | fn fromType(ty: Type, cg: *CodeGen) Vectorization { |
| 3003 | const zcu = cg.module.zcu; | 3365 | const zcu = cg.zcu; |
| 3004 | if (!ty.isVector(zcu)) return .scalar; | 3366 | if (!ty.isVector(zcu)) return .scalar; |
| 3005 | return .{ .unrolled = ty.vectorLen(zcu) }; | 3367 | return .{ .unrolled = ty.vectorLen(zcu) }; |
| 3006 | } | 3368 | } |
| ... | @@ -3034,7 +3396,7 @@ const Vectorization = union(enum) { | ... | @@ -3034,7 +3396,7 @@ const Vectorization = union(enum) { |
| 3034 | /// `ty` may be a scalar or vector, it doesn't matter. | 3396 | /// `ty` may be a scalar or vector, it doesn't matter. |
| 3035 | fn resultType(vec: Vectorization, cg: *CodeGen, ty: Type) !Type { | 3397 | fn resultType(vec: Vectorization, cg: *CodeGen, ty: Type) !Type { |
| 3036 | const pt = cg.pt; | 3398 | const pt = cg.pt; |
| 3037 | const zcu = cg.module.zcu; | 3399 | const zcu = cg.zcu; |
| 3038 | const scalar_ty = ty.scalarType(zcu); | 3400 | const scalar_ty = ty.scalarType(zcu); |
| 3039 | return switch (vec) { | 3401 | return switch (vec) { |
| 3040 | .scalar => scalar_ty, | 3402 | .scalar => scalar_ty, |
| ... | @@ -3046,7 +3408,7 @@ const Vectorization = union(enum) { | ... | @@ -3046,7 +3408,7 @@ const Vectorization = union(enum) { |
| 3046 | /// this setup, and returns a new type that holds the relevant information on how to access | 3408 | /// this setup, and returns a new type that holds the relevant information on how to access |
| 3047 | /// elements of the input. | 3409 | /// elements of the input. |
| 3048 | fn prepare(vec: Vectorization, cg: *CodeGen, tmp: Temporary) !PreparedOperand { | 3410 | fn prepare(vec: Vectorization, cg: *CodeGen, tmp: Temporary) !PreparedOperand { |
| 3049 | const zcu = cg.module.zcu; | 3411 | const zcu = cg.zcu; |
| 3050 | const is_vector = tmp.ty.isVector(zcu); | 3412 | const is_vector = tmp.ty.isVector(zcu); |
| 3051 | const value: PreparedOperand.Value = switch (tmp.value) { | 3413 | const value: PreparedOperand.Value = switch (tmp.value) { |
| 3052 | .singleton => |id| switch (vec) { | 3414 | .singleton => |id| switch (vec) { |
| ... | @@ -3146,26 +3508,28 @@ fn vectorization(cg: *CodeGen, args: anytype) Vectorization { | ... | @@ -3146,26 +3508,28 @@ fn vectorization(cg: *CodeGen, args: anytype) Vectorization { |
| 3146 | /// This function builds an OpSConvert of OpUConvert depending on the | 3508 | /// This function builds an OpSConvert of OpUConvert depending on the |
| 3147 | /// signedness of the types. | 3509 | /// signedness of the types. |
| 3148 | fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { | 3510 | fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { |
| 3149 | const zcu = cg.module.zcu; | 3511 | const zcu = cg.zcu; |
| 3150 | | | |
| 3151 | const dst_ty_id = try cg.resolveType(dst_ty.scalarType(zcu), .direct); | | |
| 3152 | const src_ty_id = try cg.resolveType(src.ty.scalarType(zcu), .direct); | | |
| 3153 | | 3512 | |
| 3154 | const v = cg.vectorization(.{ dst_ty, src }); | 3513 | const v = cg.vectorization(.{ dst_ty, src }); |
| 3155 | const result_ty = try v.resultType(cg, dst_ty); | 3514 | const result_ty = try v.resultType(cg, dst_ty); |
| 3156 | | 3515 | |
| 3157 | // We can directly compare integers, because those type-IDs are cached. | 3516 | const dst_scalar = dst_ty.scalarType(zcu); |
| 3158 | if (dst_ty_id == src_ty_id) { | 3517 | const src_scalar = src.ty.scalarType(zcu); |
| 3159 | // Nothing to do, type-pun to the right value. | 3518 | if (dst_scalar.toIntern() == src_scalar.toIntern()) { |
| 3160 | // Note, Caller guarantees that the types fit (or caller will normalize after), | | |
| 3161 | // so we don't have to normalize here. | | |
| 3162 | // Note, dst_ty may be a scalar type even if we expect a vector, so we have to | | |
| 3163 | // convert to the right type here. | | |
| 3164 | return src.pun(result_ty); | 3519 | return src.pun(result_ty); |
| 3165 | } | 3520 | } |
| | 3521 | if (dst_scalar.isInt(zcu) and src_scalar.isInt(zcu)) { |
| | 3522 | const dst_info = dst_scalar.intInfo(zcu); |
| | 3523 | const src_info = src_scalar.intInfo(zcu); |
| | 3524 | if (cg.backingIntBits(dst_info.bits).@"0" == cg.backingIntBits(src_info.bits).@"0" and |
| | 3525 | dst_info.signedness == src_info.signedness) |
| | 3526 | { |
| | 3527 | return src.pun(result_ty); |
| | 3528 | } |
| | 3529 | } |
| 3166 | | 3530 | |
| 3167 | const ops = v.components(); | 3531 | const ops = v.components(); |
| 3168 | const results = cg.module.allocIds(ops); | 3532 | const results = cg.allocIds(ops); |
| 3169 | | 3533 | |
| 3170 | const op_result_ty = dst_ty.scalarType(zcu); | 3534 | const op_result_ty = dst_ty.scalarType(zcu); |
| 3171 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 3535 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| ... | @@ -3179,7 +3543,7 @@ fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { | ... | @@ -3179,7 +3543,7 @@ fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { |
| 3179 | const op_src = try v.prepare(cg, src); | 3543 | const op_src = try v.prepare(cg, src); |
| 3180 | | 3544 | |
| 3181 | for (0..ops) |i| { | 3545 | for (0..ops) |i| { |
| 3182 | try cg.body.emitRaw(cg.module.gpa, opcode, 3); | 3546 | try cg.body.emitRaw(cg.gpa, opcode, 3); |
| 3183 | cg.body.writeOperand(Id, op_result_ty_id); | 3547 | cg.body.writeOperand(Id, op_result_ty_id); |
| 3184 | cg.body.writeOperand(Id, results.at(i)); | 3548 | cg.body.writeOperand(Id, results.at(i)); |
| 3185 | cg.body.writeOperand(Id, op_src.at(i)); | 3549 | cg.body.writeOperand(Id, op_src.at(i)); |
| ... | @@ -3188,51 +3552,12 @@ fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { | ... | @@ -3188,51 +3552,12 @@ fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { |
| 3188 | return v.finalize(result_ty, results); | 3552 | return v.finalize(result_ty, results); |
| 3189 | } | 3553 | } |
| 3190 | | 3554 | |
| 3191 | fn buildFma(cg: *CodeGen, a: Temporary, b: Temporary, c: Temporary) !Temporary { | | |
| 3192 | const zcu = cg.module.zcu; | | |
| 3193 | const target = cg.module.zcu.getTarget(); | | |
| 3194 | | | |
| 3195 | const v = cg.vectorization(.{ a, b, c }); | | |
| 3196 | const ops = v.components(); | | |
| 3197 | const results = cg.module.allocIds(ops); | | |
| 3198 | | | |
| 3199 | const op_result_ty = a.ty.scalarType(zcu); | | |
| 3200 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | | |
| 3201 | const result_ty = try v.resultType(cg, a.ty); | | |
| 3202 | | | |
| 3203 | const op_a = try v.prepare(cg, a); | | |
| 3204 | const op_b = try v.prepare(cg, b); | | |
| 3205 | const op_c = try v.prepare(cg, c); | | |
| 3206 | | | |
| 3207 | const set = try cg.importExtendedSet(); | | |
| 3208 | const opcode: u32 = switch (target.os.tag) { | | |
| 3209 | .opencl => @intFromEnum(spec.OpenClOpcode.fma), | | |
| 3210 | // NOTE: Vulkan's FMA instruction does *NOT* produce the right values! | | |
| 3211 | // its precision guarantees do NOT match zigs and it does NOT match OpenCLs! | | |
| 3212 | // it needs to be emulated! | | |
| 3213 | .vulkan, .opengl => @intFromEnum(spec.GlslOpcode.Fma), | | |
| 3214 | else => unreachable, | | |
| 3215 | }; | | |
| 3216 | | | |
| 3217 | for (0..ops) |i| { | | |
| 3218 | try cg.body.emit(cg.module.gpa, .OpExtInst, .{ | | |
| 3219 | .id_result_type = op_result_ty_id, | | |
| 3220 | .id_result = results.at(i), | | |
| 3221 | .set = set, | | |
| 3222 | .instruction = .{ .inst = opcode }, | | |
| 3223 | .id_ref_4 = &.{ op_a.at(i), op_b.at(i), op_c.at(i) }, | | |
| 3224 | }); | | |
| 3225 | } | | |
| 3226 | | | |
| 3227 | return v.finalize(result_ty, results); | | |
| 3228 | } | | |
| 3229 | | | |
| 3230 | fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporary) !Temporary { | 3555 | fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporary) !Temporary { |
| 3231 | const zcu = cg.module.zcu; | 3556 | const zcu = cg.zcu; |
| 3232 | | 3557 | |
| 3233 | const v = cg.vectorization(.{ condition, lhs, rhs }); | 3558 | const v = cg.vectorization(.{ condition, lhs, rhs }); |
| 3234 | const ops = v.components(); | 3559 | const ops = v.components(); |
| 3235 | const results = cg.module.allocIds(ops); | 3560 | const results = cg.allocIds(ops); |
| 3236 | | 3561 | |
| 3237 | const op_result_ty = lhs.ty.scalarType(zcu); | 3562 | const op_result_ty = lhs.ty.scalarType(zcu); |
| 3238 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 3563 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| ... | @@ -3245,7 +3570,7 @@ fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporar | ... | @@ -3245,7 +3570,7 @@ fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporar |
| 3245 | const object_2 = try v.prepare(cg, rhs); | 3570 | const object_2 = try v.prepare(cg, rhs); |
| 3246 | | 3571 | |
| 3247 | for (0..ops) |i| { | 3572 | for (0..ops) |i| { |
| 3248 | try cg.body.emit(cg.module.gpa, .OpSelect, .{ | 3573 | try cg.body.emit(cg.gpa, .OpSelect, .{ |
| 3249 | .id_result_type = op_result_ty_id, | 3574 | .id_result_type = op_result_ty_id, |
| 3250 | .id_result = results.at(i), | 3575 | .id_result = results.at(i), |
| 3251 | .condition = cond.at(i), | 3576 | .condition = cond.at(i), |
| ... | @@ -3260,7 +3585,7 @@ fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporar | ... | @@ -3260,7 +3585,7 @@ fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporar |
| 3260 | fn buildCmp(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { | 3585 | fn buildCmp(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { |
| 3261 | const v = cg.vectorization(.{ lhs, rhs }); | 3586 | const v = cg.vectorization(.{ lhs, rhs }); |
| 3262 | const ops = v.components(); | 3587 | const ops = v.components(); |
| 3263 | const results = cg.module.allocIds(ops); | 3588 | const results = cg.allocIds(ops); |
| 3264 | | 3589 | |
| 3265 | const op_result_ty: Type = .bool; | 3590 | const op_result_ty: Type = .bool; |
| 3266 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 3591 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| ... | @@ -3270,7 +3595,7 @@ fn buildCmp(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Tempo | ... | @@ -3270,7 +3595,7 @@ fn buildCmp(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Tempo |
| 3270 | const op_rhs = try v.prepare(cg, rhs); | 3595 | const op_rhs = try v.prepare(cg, rhs); |
| 3271 | | 3596 | |
| 3272 | for (0..ops) |i| { | 3597 | for (0..ops) |i| { |
| 3273 | try cg.body.emitRaw(cg.module.gpa, opcode, 4); | 3598 | try cg.body.emitRaw(cg.gpa, opcode, 4); |
| 3274 | cg.body.writeOperand(Id, op_result_ty_id); | 3599 | cg.body.writeOperand(Id, op_result_ty_id); |
| 3275 | cg.body.writeOperand(Id, results.at(i)); | 3600 | cg.body.writeOperand(Id, results.at(i)); |
| 3276 | cg.body.writeOperand(Id, op_lhs.at(i)); | 3601 | cg.body.writeOperand(Id, op_lhs.at(i)); |
| ... | @@ -3351,11 +3676,11 @@ const UnaryOp = enum { | ... | @@ -3351,11 +3676,11 @@ const UnaryOp = enum { |
| 3351 | }; | 3676 | }; |
| 3352 | | 3677 | |
| 3353 | fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { | 3678 | fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { |
| 3354 | const zcu = cg.module.zcu; | 3679 | const zcu = cg.zcu; |
| 3355 | const target = cg.module.zcu.getTarget(); | 3680 | const target = cg.zcu.getTarget(); |
| 3356 | const v = cg.vectorization(.{operand}); | 3681 | const v = cg.vectorization(.{operand}); |
| 3357 | const ops = v.components(); | 3682 | const ops = v.components(); |
| 3358 | const results = cg.module.allocIds(ops); | 3683 | const results = cg.allocIds(ops); |
| 3359 | const op_result_ty = operand.ty.scalarType(zcu); | 3684 | const op_result_ty = operand.ty.scalarType(zcu); |
| 3360 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 3685 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| 3361 | const result_ty = try v.resultType(cg, operand.ty); | 3686 | const result_ty = try v.resultType(cg, operand.ty); |
| ... | @@ -3364,7 +3689,7 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { | ... | @@ -3364,7 +3689,7 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { |
| 3364 | if (op.extInstOpcode(target)) |opcode| { | 3689 | if (op.extInstOpcode(target)) |opcode| { |
| 3365 | const set = try cg.importExtendedSet(); | 3690 | const set = try cg.importExtendedSet(); |
| 3366 | for (0..ops) |i| { | 3691 | for (0..ops) |i| { |
| 3367 | try cg.body.emit(cg.module.gpa, .OpExtInst, .{ | 3692 | try cg.body.emit(cg.gpa, .OpExtInst, .{ |
| 3368 | .id_result_type = op_result_ty_id, | 3693 | .id_result_type = op_result_ty_id, |
| 3369 | .id_result = results.at(i), | 3694 | .id_result = results.at(i), |
| 3370 | .set = set, | 3695 | .set = set, |
| ... | @@ -3384,7 +3709,7 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { | ... | @@ -3384,7 +3709,7 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { |
| 3384 | ), | 3709 | ), |
| 3385 | }; | 3710 | }; |
| 3386 | for (0..ops) |i| { | 3711 | for (0..ops) |i| { |
| 3387 | try cg.body.emitRaw(cg.module.gpa, opcode, 3); | 3712 | try cg.body.emitRaw(cg.gpa, opcode, 3); |
| 3388 | cg.body.writeOperand(Id, op_result_ty_id); | 3713 | cg.body.writeOperand(Id, op_result_ty_id); |
| 3389 | cg.body.writeOperand(Id, results.at(i)); | 3714 | cg.body.writeOperand(Id, results.at(i)); |
| 3390 | cg.body.writeOperand(Id, op_operand.at(i)); | 3715 | cg.body.writeOperand(Id, op_operand.at(i)); |
| ... | @@ -3395,11 +3720,11 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { | ... | @@ -3395,11 +3720,11 @@ fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { |
| 3395 | } | 3720 | } |
| 3396 | | 3721 | |
| 3397 | fn buildBinary(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { | 3722 | fn buildBinary(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { |
| 3398 | const zcu = cg.module.zcu; | 3723 | const zcu = cg.zcu; |
| 3399 | | 3724 | |
| 3400 | const v = cg.vectorization(.{ lhs, rhs }); | 3725 | const v = cg.vectorization(.{ lhs, rhs }); |
| 3401 | const ops = v.components(); | 3726 | const ops = v.components(); |
| 3402 | const results = cg.module.allocIds(ops); | 3727 | const results = cg.allocIds(ops); |
| 3403 | | 3728 | |
| 3404 | const op_result_ty = lhs.ty.scalarType(zcu); | 3729 | const op_result_ty = lhs.ty.scalarType(zcu); |
| 3405 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 3730 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| ... | @@ -3409,7 +3734,7 @@ fn buildBinary(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Te | ... | @@ -3409,7 +3734,7 @@ fn buildBinary(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Te |
| 3409 | const op_rhs = try v.prepare(cg, rhs); | 3734 | const op_rhs = try v.prepare(cg, rhs); |
| 3410 | | 3735 | |
| 3411 | for (0..ops) |i| { | 3736 | for (0..ops) |i| { |
| 3412 | try cg.body.emitRaw(cg.module.gpa, opcode, 4); | 3737 | try cg.body.emitRaw(cg.gpa, opcode, 4); |
| 3413 | cg.body.writeOperand(Id, op_result_ty_id); | 3738 | cg.body.writeOperand(Id, op_result_ty_id); |
| 3414 | cg.body.writeOperand(Id, results.at(i)); | 3739 | cg.body.writeOperand(Id, results.at(i)); |
| 3415 | cg.body.writeOperand(Id, op_lhs.at(i)); | 3740 | cg.body.writeOperand(Id, op_lhs.at(i)); |
| ... | @@ -3428,11 +3753,11 @@ fn buildWideMul( | ... | @@ -3428,11 +3753,11 @@ fn buildWideMul( |
| 3428 | rhs: Temporary, | 3753 | rhs: Temporary, |
| 3429 | ) !struct { Temporary, Temporary } { | 3754 | ) !struct { Temporary, Temporary } { |
| 3430 | const pt = cg.pt; | 3755 | const pt = cg.pt; |
| 3431 | const zcu = cg.module.zcu; | 3756 | const zcu = cg.zcu; |
| 3432 | const comp = zcu.comp; | 3757 | const comp = zcu.comp; |
| 3433 | const gpa = comp.gpa; | 3758 | const gpa = comp.gpa; |
| 3434 | const io = comp.io; | 3759 | const io = comp.io; |
| 3435 | const target = cg.module.zcu.getTarget(); | 3760 | const target = cg.zcu.getTarget(); |
| 3436 | const ip = &zcu.intern_pool; | 3761 | const ip = &zcu.intern_pool; |
| 3437 | | 3762 | |
| 3438 | const v = lhs.vectorization(cg).unify(rhs.vectorization(cg)); | 3763 | const v = lhs.vectorization(cg).unify(rhs.vectorization(cg)); |
| ... | @@ -3444,8 +3769,8 @@ fn buildWideMul( | ... | @@ -3444,8 +3769,8 @@ fn buildWideMul( |
| 3444 | const lhs_op = try v.prepare(cg, lhs); | 3769 | const lhs_op = try v.prepare(cg, lhs); |
| 3445 | const rhs_op = try v.prepare(cg, rhs); | 3770 | const rhs_op = try v.prepare(cg, rhs); |
| 3446 | | 3771 | |
| 3447 | const value_results = cg.module.allocIds(ops); | 3772 | const value_results = cg.allocIds(ops); |
| 3448 | const overflow_results = cg.module.allocIds(ops); | 3773 | const overflow_results = cg.allocIds(ops); |
| 3449 | | 3774 | |
| 3450 | switch (target.os.tag) { | 3775 | switch (target.os.tag) { |
| 3451 | .opencl => { | 3776 | .opencl => { |
| ... | @@ -3490,7 +3815,7 @@ fn buildWideMul( | ... | @@ -3490,7 +3815,7 @@ fn buildWideMul( |
| 3490 | }; | 3815 | }; |
| 3491 | | 3816 | |
| 3492 | for (0..ops) |i| { | 3817 | for (0..ops) |i| { |
| 3493 | const op_result = cg.module.allocId(); | 3818 | const op_result = cg.allocId(); |
| 3494 | | 3819 | |
| 3495 | try cg.body.emitRaw(gpa, opcode, 4); | 3820 | try cg.body.emitRaw(gpa, opcode, 4); |
| 3496 | cg.body.writeOperand(Id, op_result_ty_id); | 3821 | cg.body.writeOperand(Id, op_result_ty_id); |
| ... | @@ -3550,12 +3875,12 @@ fn buildWideMul( | ... | @@ -3550,12 +3875,12 @@ fn buildWideMul( |
| 3550 | fn generateTestEntryPoint( | 3875 | fn generateTestEntryPoint( |
| 3551 | cg: *CodeGen, | 3876 | cg: *CodeGen, |
| 3552 | name: []const u8, | 3877 | name: []const u8, |
| 3553 | spv_decl_index: Module.Decl.Index, | 3878 | spv_decl_index: Decl.Index, |
| 3554 | test_id: Id, | 3879 | test_id: Id, |
| 3555 | ) !void { | 3880 | ) !void { |
| 3556 | const gpa = cg.module.gpa; | 3881 | const gpa = cg.gpa; |
| 3557 | const zcu = cg.module.zcu; | 3882 | const zcu = cg.zcu; |
| 3558 | const target = cg.module.zcu.getTarget(); | 3883 | const target = cg.zcu.getTarget(); |
| 3559 | | 3884 | |
| 3560 | const anyerror_ty_id = try cg.resolveType(.anyerror, .direct); | 3885 | const anyerror_ty_id = try cg.resolveType(.anyerror, .direct); |
| 3561 | const ptr_anyerror_ty = try cg.pt.ptrType(.{ | 3886 | const ptr_anyerror_ty = try cg.pt.ptrType(.{ |
| ... | @@ -3564,15 +3889,15 @@ fn generateTestEntryPoint( | ... | @@ -3564,15 +3889,15 @@ fn generateTestEntryPoint( |
| 3564 | }); | 3889 | }); |
| 3565 | const ptr_anyerror_ty_id = try cg.resolveType(ptr_anyerror_ty, .direct); | 3890 | const ptr_anyerror_ty_id = try cg.resolveType(ptr_anyerror_ty, .direct); |
| 3566 | | 3891 | |
| 3567 | const kernel_id = cg.module.declPtr(spv_decl_index).result_id; | 3892 | const kernel_id = cg.declPtr(spv_decl_index).result_id; |
| 3568 | | 3893 | |
| 3569 | const section = &cg.module.sections.functions; | 3894 | const section = &cg.sections.functions; |
| 3570 | | 3895 | |
| 3571 | const p_error_id = cg.module.allocId(); | 3896 | const p_error_id = cg.allocId(); |
| 3572 | switch (target.os.tag) { | 3897 | switch (target.os.tag) { |
| 3573 | .opencl, .amdhsa => { | 3898 | .opencl, .amdhsa => { |
| 3574 | const void_ty_id = try cg.resolveType(.void, .direct); | 3899 | const void_ty_id = try cg.resolveType(.void, .direct); |
| 3575 | const kernel_proto_ty_id = try cg.module.functionType(void_ty_id, &.{ptr_anyerror_ty_id}); | 3900 | const kernel_proto_ty_id = try cg.functionType(void_ty_id, &.{ptr_anyerror_ty_id}); |
| 3576 | | 3901 | |
| 3577 | try section.emit(gpa, .OpFunction, .{ | 3902 | try section.emit(gpa, .OpFunction, .{ |
| 3578 | .id_result_type = try cg.resolveType(.void, .direct), | 3903 | .id_result_type = try cg.resolveType(.void, .direct), |
| ... | @@ -3587,43 +3912,43 @@ fn generateTestEntryPoint( | ... | @@ -3587,43 +3912,43 @@ fn generateTestEntryPoint( |
| 3587 | }); | 3912 | }); |
| 3588 | | 3913 | |
| 3589 | try section.emit(gpa, .OpLabel, .{ | 3914 | try section.emit(gpa, .OpLabel, .{ |
| 3590 | .id_result = cg.module.allocId(), | 3915 | .id_result = cg.allocId(), |
| 3591 | }); | 3916 | }); |
| 3592 | }, | 3917 | }, |
| 3593 | .vulkan, .opengl => { | 3918 | .vulkan, .opengl => { |
| 3594 | if (cg.module.error_buffer == null) { | 3919 | if (cg.error_buffer == null) { |
| 3595 | const spv_err_decl_index = try cg.module.allocDecl(.global); | 3920 | const spv_err_decl_index = try cg.allocDecl(.global); |
| 3596 | const err_buf_result_id = cg.module.declPtr(spv_err_decl_index).result_id; | 3921 | const err_buf_result_id = cg.declPtr(spv_err_decl_index).result_id; |
| 3597 | | 3922 | |
| 3598 | const buffer_struct_ty_id = cg.module.allocId(); | 3923 | const buffer_struct_ty_id = cg.allocId(); |
| 3599 | try cg.module.sections.globals.emit(gpa, .OpTypeStruct, .{ | 3924 | try cg.sections.globals.emit(gpa, .OpTypeStruct, .{ |
| 3600 | .id_result = buffer_struct_ty_id, | 3925 | .id_result = buffer_struct_ty_id, |
| 3601 | .id_ref = &.{anyerror_ty_id}, | 3926 | .id_ref = &.{anyerror_ty_id}, |
| 3602 | }); | 3927 | }); |
| 3603 | try cg.module.memberDebugName(buffer_struct_ty_id, 0, "error_out"); | 3928 | try cg.memberDebugName(buffer_struct_ty_id, 0, "error_out"); |
| 3604 | try cg.module.decorate(buffer_struct_ty_id, .block); | 3929 | try cg.decorate(buffer_struct_ty_id, .block); |
| 3605 | try cg.module.decorateMember(buffer_struct_ty_id, 0, .{ .offset = .{ .byte_offset = 0 } }); | 3930 | try cg.decorateMember(buffer_struct_ty_id, 0, .{ .offset = .{ .byte_offset = 0 } }); |
| 3606 | | 3931 | |
| 3607 | const ptr_buffer_struct_ty_id = cg.module.allocId(); | 3932 | const ptr_buffer_struct_ty_id = cg.allocId(); |
| 3608 | try cg.module.sections.globals.emit(gpa, .OpTypePointer, .{ | 3933 | try cg.sections.globals.emit(gpa, .OpTypePointer, .{ |
| 3609 | .id_result = ptr_buffer_struct_ty_id, | 3934 | .id_result = ptr_buffer_struct_ty_id, |
| 3610 | .storage_class = cg.module.storageClass(.global), | 3935 | .storage_class = cg.storageClass(.global), |
| 3611 | .type = buffer_struct_ty_id, | 3936 | .type = buffer_struct_ty_id, |
| 3612 | }); | 3937 | }); |
| 3613 | | 3938 | |
| 3614 | try cg.module.sections.globals.emit(gpa, .OpVariable, .{ | 3939 | try cg.sections.globals.emit(gpa, .OpVariable, .{ |
| 3615 | .id_result_type = ptr_buffer_struct_ty_id, | 3940 | .id_result_type = ptr_buffer_struct_ty_id, |
| 3616 | .id_result = err_buf_result_id, | 3941 | .id_result = err_buf_result_id, |
| 3617 | .storage_class = cg.module.storageClass(.global), | 3942 | .storage_class = cg.storageClass(.global), |
| 3618 | }); | 3943 | }); |
| 3619 | try cg.module.decorate(err_buf_result_id, .{ .descriptor_set = .{ .descriptor_set = 0 } }); | 3944 | try cg.decorate(err_buf_result_id, .{ .descriptor_set = .{ .descriptor_set = 0 } }); |
| 3620 | try cg.module.decorate(err_buf_result_id, .{ .binding = .{ .binding_point = 0 } }); | 3945 | try cg.decorate(err_buf_result_id, .{ .binding = .{ .binding_point = 0 } }); |
| 3621 | | 3946 | |
| 3622 | cg.module.error_buffer = spv_err_decl_index; | 3947 | cg.error_buffer = spv_err_decl_index; |
| 3623 | } | 3948 | } |
| 3624 | | 3949 | |
| 3625 | const void_ty_id = try cg.resolveType(.void, .direct); | 3950 | const void_ty_id = try cg.resolveType(.void, .direct); |
| 3626 | const kernel_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); | 3951 | const kernel_proto_ty_id = try cg.functionType(void_ty_id, &.{}); |
| 3627 | try section.emit(gpa, .OpFunction, .{ | 3952 | try section.emit(gpa, .OpFunction, .{ |
| 3628 | .id_result_type = try cg.resolveType(.void, .direct), | 3953 | .id_result_type = try cg.resolveType(.void, .direct), |
| 3629 | .id_result = kernel_id, | 3954 | .id_result = kernel_id, |
| ... | @@ -3631,12 +3956,12 @@ fn generateTestEntryPoint( | ... | @@ -3631,12 +3956,12 @@ fn generateTestEntryPoint( |
| 3631 | .function_type = kernel_proto_ty_id, | 3956 | .function_type = kernel_proto_ty_id, |
| 3632 | }); | 3957 | }); |
| 3633 | try section.emit(gpa, .OpLabel, .{ | 3958 | try section.emit(gpa, .OpLabel, .{ |
| 3634 | .id_result = cg.module.allocId(), | 3959 | .id_result = cg.allocId(), |
| 3635 | }); | 3960 | }); |
| 3636 | | 3961 | |
| 3637 | const spv_err_decl_index = cg.module.error_buffer.?; | 3962 | const spv_err_decl_index = cg.error_buffer.?; |
| 3638 | const buffer_id = cg.module.declPtr(spv_err_decl_index).result_id; | 3963 | const buffer_id = cg.declPtr(spv_err_decl_index).result_id; |
| 3639 | try cg.module.decl_deps.append(gpa, spv_err_decl_index); | 3964 | try cg.decl_deps.append(gpa, spv_err_decl_index); |
| 3640 | | 3965 | |
| 3641 | const zero_id = try cg.constInt(.u32, 0); | 3966 | const zero_id = try cg.constInt(.u32, 0); |
| 3642 | try section.emit(gpa, .OpInBoundsAccessChain, .{ | 3967 | try section.emit(gpa, .OpInBoundsAccessChain, .{ |
| ... | @@ -3649,7 +3974,7 @@ fn generateTestEntryPoint( | ... | @@ -3649,7 +3974,7 @@ fn generateTestEntryPoint( |
| 3649 | else => unreachable, | 3974 | else => unreachable, |
| 3650 | } | 3975 | } |
| 3651 | | 3976 | |
| 3652 | const error_id = cg.module.allocId(); | 3977 | const error_id = cg.allocId(); |
| 3653 | try section.emit(gpa, .OpFunctionCall, .{ | 3978 | try section.emit(gpa, .OpFunctionCall, .{ |
| 3654 | .id_result_type = anyerror_ty_id, | 3979 | .id_result_type = anyerror_ty_id, |
| 3655 | .id_result = error_id, | 3980 | .id_result = error_id, |
| ... | @@ -3668,9 +3993,14 @@ fn generateTestEntryPoint( | ... | @@ -3668,9 +3993,14 @@ fn generateTestEntryPoint( |
| 3668 | | 3993 | |
| 3669 | // Just generate a quick other name because the intel runtime crashes when the entry- | 3994 | // Just generate a quick other name because the intel runtime crashes when the entry- |
| 3670 | // point name is the same as a different OpName. | 3995 | // point name is the same as a different OpName. |
| 3671 | const test_name = try std.fmt.allocPrint(cg.module.arena, "test {s}", .{name}); | 3996 | const test_name = try std.fmt.allocPrint(cg.arena, "test {s}", .{name}); |
| 3672 | | 3997 | |
| 3673 | try cg.module.declareEntryPoint(spv_decl_index, test_name, .{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }); | 3998 | const ep_gop = try cg.entry_points.getOrPut(cg.gpa, cg.declPtr(spv_decl_index).result_id); |
| | 3999 | ep_gop.value_ptr.* = .{ |
| | 4000 | .decl_index = spv_decl_index, |
| | 4001 | .name = test_name, |
| | 4002 | .cc = .{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }, |
| | 4003 | }; |
| 3674 | } | 4004 | } |
| 3675 | | 4005 | |
| 3676 | fn intFromBool(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary { | 4006 | fn intFromBool(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary { |
| ... | @@ -3688,7 +4018,7 @@ fn intFromBool(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary { | ... | @@ -3688,7 +4018,7 @@ fn intFromBool(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary { |
| 3688 | /// This converts the argument type from resolveType(ty, .indirect) to resolveType(ty, .direct). | 4018 | /// This converts the argument type from resolveType(ty, .indirect) to resolveType(ty, .direct). |
| 3689 | fn convertToDirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { | 4019 | fn convertToDirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { |
| 3690 | const pt = cg.pt; | 4020 | const pt = cg.pt; |
| 3691 | const zcu = cg.module.zcu; | 4021 | const zcu = cg.zcu; |
| 3692 | switch (ty.scalarType(zcu).zigTypeTag(zcu)) { | 4022 | switch (ty.scalarType(zcu).zigTypeTag(zcu)) { |
| 3693 | .bool => { | 4023 | .bool => { |
| 3694 | const false_id = try cg.constBool(false, .indirect); | 4024 | const false_id = try cg.constBool(false, .indirect); |
| ... | @@ -3714,7 +4044,7 @@ fn convertToDirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { | ... | @@ -3714,7 +4044,7 @@ fn convertToDirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { |
| 3714 | /// Convert representation from direct (in 'register) to direct (in memory) | 4044 | /// Convert representation from direct (in 'register) to direct (in memory) |
| 3715 | /// This converts the argument type from resolveType(ty, .direct) to resolveType(ty, .indirect). | 4045 | /// This converts the argument type from resolveType(ty, .direct) to resolveType(ty, .indirect). |
| 3716 | fn convertToIndirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { | 4046 | fn convertToIndirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { |
| 3717 | const zcu = cg.module.zcu; | 4047 | const zcu = cg.zcu; |
| 3718 | switch (ty.scalarType(zcu).zigTypeTag(zcu)) { | 4048 | switch (ty.scalarType(zcu).zigTypeTag(zcu)) { |
| 3719 | .bool => { | 4049 | .bool => { |
| 3720 | const result = try cg.intFromBool(.init(ty, operand_id), .u1); | 4050 | const result = try cg.intFromBool(.init(ty, operand_id), .u1); |
| ... | @@ -3726,9 +4056,9 @@ fn convertToIndirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { | ... | @@ -3726,9 +4056,9 @@ fn convertToIndirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { |
| 3726 | | 4056 | |
| 3727 | fn extractField(cg: *CodeGen, result_ty: Type, object: Id, field: u32) !Id { | 4057 | fn extractField(cg: *CodeGen, result_ty: Type, object: Id, field: u32) !Id { |
| 3728 | const result_ty_id = try cg.resolveType(result_ty, .indirect); | 4058 | const result_ty_id = try cg.resolveType(result_ty, .indirect); |
| 3729 | const result_id = cg.module.allocId(); | 4059 | const result_id = cg.allocId(); |
| 3730 | const indexes = [_]u32{field}; | 4060 | const indexes = [_]u32{field}; |
| 3731 | try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ | 4061 | try cg.body.emit(cg.gpa, .OpCompositeExtract, .{ |
| 3732 | .id_result_type = result_ty_id, | 4062 | .id_result_type = result_ty_id, |
| 3733 | .id_result = result_id, | 4063 | .id_result = result_id, |
| 3734 | .composite = object, | 4064 | .composite = object, |
| ... | @@ -3740,9 +4070,9 @@ fn extractField(cg: *CodeGen, result_ty: Type, object: Id, field: u32) !Id { | ... | @@ -3740,9 +4070,9 @@ fn extractField(cg: *CodeGen, result_ty: Type, object: Id, field: u32) !Id { |
| 3740 | | 4070 | |
| 3741 | fn extractVectorComponent(cg: *CodeGen, result_ty: Type, vector_id: Id, field: u32) !Id { | 4071 | fn extractVectorComponent(cg: *CodeGen, result_ty: Type, vector_id: Id, field: u32) !Id { |
| 3742 | const result_ty_id = try cg.resolveType(result_ty, .direct); | 4072 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 3743 | const result_id = cg.module.allocId(); | 4073 | const result_id = cg.allocId(); |
| 3744 | const indexes = [_]u32{field}; | 4074 | const indexes = [_]u32{field}; |
| 3745 | try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ | 4075 | try cg.body.emit(cg.gpa, .OpCompositeExtract, .{ |
| 3746 | .id_result_type = result_ty_id, | 4076 | .id_result_type = result_ty_id, |
| 3747 | .id_result = result_id, | 4077 | .id_result = result_id, |
| 3748 | .composite = vector_id, | 4078 | .composite = vector_id, |
| ... | @@ -3757,15 +4087,15 @@ const MemoryOptions = struct { | ... | @@ -3757,15 +4087,15 @@ const MemoryOptions = struct { |
| 3757 | }; | 4087 | }; |
| 3758 | | 4088 | |
| 3759 | fn load(cg: *CodeGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { | 4089 | fn load(cg: *CodeGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { |
| 3760 | const zcu = cg.module.zcu; | 4090 | const zcu = cg.zcu; |
| 3761 | const alignment: u32 = @intCast(value_ty.abiAlignment(zcu).toByteUnits().?); | 4091 | const alignment: u32 = @intCast(value_ty.abiAlignment(zcu).toByteUnits().?); |
| 3762 | const indirect_value_ty_id = try cg.resolveType(value_ty, .indirect); | 4092 | const indirect_value_ty_id = try cg.resolveType(value_ty, .indirect); |
| 3763 | const result_id = cg.module.allocId(); | 4093 | const result_id = cg.allocId(); |
| 3764 | const access: spec.MemoryAccess.Extended = .{ | 4094 | const access: spec.MemoryAccess.Extended = .{ |
| 3765 | .@"volatile" = options.is_volatile, | 4095 | .@"volatile" = options.is_volatile, |
| 3766 | .aligned = .{ .literal_integer = alignment }, | 4096 | .aligned = .{ .literal_integer = alignment }, |
| 3767 | }; | 4097 | }; |
| 3768 | try cg.body.emit(cg.module.gpa, .OpLoad, .{ | 4098 | try cg.body.emit(cg.gpa, .OpLoad, .{ |
| 3769 | .id_result_type = indirect_value_ty_id, | 4099 | .id_result_type = indirect_value_ty_id, |
| 3770 | .id_result = result_id, | 4100 | .id_result = result_id, |
| 3771 | .pointer = ptr_id, | 4101 | .pointer = ptr_id, |
| ... | @@ -3777,7 +4107,7 @@ fn load(cg: *CodeGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { | ... | @@ -3777,7 +4107,7 @@ fn load(cg: *CodeGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { |
| 3777 | fn store(cg: *CodeGen, value_ty: Type, ptr_id: Id, value_id: Id, options: MemoryOptions) !void { | 4107 | fn store(cg: *CodeGen, value_ty: Type, ptr_id: Id, value_id: Id, options: MemoryOptions) !void { |
| 3778 | const indirect_value_id = try cg.convertToIndirect(value_ty, value_id); | 4108 | const indirect_value_id = try cg.convertToIndirect(value_ty, value_id); |
| 3779 | const access: spec.MemoryAccess.Extended = .{ .@"volatile" = options.is_volatile }; | 4109 | const access: spec.MemoryAccess.Extended = .{ .@"volatile" = options.is_volatile }; |
| 3780 | try cg.body.emit(cg.module.gpa, .OpStore, .{ | 4110 | try cg.body.emit(cg.gpa, .OpStore, .{ |
| 3781 | .pointer = ptr_id, | 4111 | .pointer = ptr_id, |
| 3782 | .object = indirect_value_id, | 4112 | .object = indirect_value_id, |
| 3783 | .memory_access = access, | 4113 | .memory_access = access, |
| ... | @@ -3791,8 +4121,8 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) !void { | ... | @@ -3791,8 +4121,8 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) !void { |
| 3791 | } | 4121 | } |
| 3792 | | 4122 | |
| 3793 | fn genInst(cg: *CodeGen, inst: Air.Inst.Index) Error!void { | 4123 | fn genInst(cg: *CodeGen, inst: Air.Inst.Index) Error!void { |
| 3794 | const gpa = cg.module.gpa; | 4124 | const gpa = cg.gpa; |
| 3795 | const zcu = cg.module.zcu; | 4125 | const zcu = cg.zcu; |
| 3796 | const ip = &zcu.intern_pool; | 4126 | const ip = &zcu.intern_pool; |
| 3797 | if (cg.liveness.isUnused(inst) and !cg.air.mustLower(inst, ip)) | 4127 | if (cg.liveness.isUnused(inst) and !cg.air.mustLower(inst, ip)) |
| 3798 | return; | 4128 | return; |
| ... | @@ -4028,7 +4358,7 @@ fn airBitwiseOp(cg: *CodeGen, inst: Air.Inst.Index, op: BitwiseOp) !?Id { | ... | @@ -4028,7 +4358,7 @@ fn airBitwiseOp(cg: *CodeGen, inst: Air.Inst.Index, op: BitwiseOp) !?Id { |
| 4028 | } | 4358 | } |
| 4029 | | 4359 | |
| 4030 | fn airShift(cg: *CodeGen, inst: Air.Inst.Index, unsigned: Opcode, signed: Opcode) !?Id { | 4360 | fn airShift(cg: *CodeGen, inst: Air.Inst.Index, unsigned: Opcode, signed: Opcode) !?Id { |
| 4031 | const zcu = cg.module.zcu; | 4361 | const zcu = cg.zcu; |
| 4032 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 4362 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4033 | | 4363 | |
| 4034 | if (cg.typeOf(bin_op.lhs).isVector(zcu) and !cg.typeOf(bin_op.rhs).isVector(zcu)) { | 4364 | if (cg.typeOf(bin_op.lhs).isVector(zcu) and !cg.typeOf(bin_op.rhs).isVector(zcu)) { |
| ... | @@ -4048,8 +4378,8 @@ fn airShift(cg: *CodeGen, inst: Air.Inst.Index, unsigned: Opcode, signed: Opcode | ... | @@ -4048,8 +4378,8 @@ fn airShift(cg: *CodeGen, inst: Air.Inst.Index, unsigned: Opcode, signed: Opcode |
| 4048 | .composite_integer => blk: { | 4378 | .composite_integer => blk: { |
| 4049 | const shift_id = try shift.materialize(cg); | 4379 | const shift_id = try shift.materialize(cg); |
| 4050 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 4380 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 4051 | const result_id = cg.module.allocId(); | 4381 | const result_id = cg.allocId(); |
| 4052 | try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ | 4382 | try cg.body.emit(cg.gpa, .OpCompositeExtract, .{ |
| 4053 | .id_result_type = u32_ty_id, | 4383 | .id_result_type = u32_ty_id, |
| 4054 | .id_result = result_id, | 4384 | .id_result = result_id, |
| 4055 | .composite = shift_id, | 4385 | .composite = shift_id, |
| ... | @@ -4156,13 +4486,13 @@ fn airMinMax(cg: *CodeGen, inst: Air.Inst.Index, op: MinMax) !?Id { | ... | @@ -4156,13 +4486,13 @@ fn airMinMax(cg: *CodeGen, inst: Air.Inst.Index, op: MinMax) !?Id { |
| 4156 | } | 4486 | } |
| 4157 | | 4487 | |
| 4158 | fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { | 4488 | fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { |
| 4159 | const zcu = cg.module.zcu; | 4489 | const zcu = cg.zcu; |
| 4160 | const target = zcu.getTarget(); | 4490 | const target = zcu.getTarget(); |
| 4161 | const info = cg.arithmeticTypeInfo(lhs.ty); | 4491 | const info = cg.arithmeticTypeInfo(lhs.ty); |
| 4162 | | 4492 | |
| 4163 | const v = cg.vectorization(.{ lhs, rhs }); | 4493 | const v = cg.vectorization(.{ lhs, rhs }); |
| 4164 | const ops = v.components(); | 4494 | const ops = v.components(); |
| 4165 | const results = cg.module.allocIds(ops); | 4495 | const results = cg.allocIds(ops); |
| 4166 | | 4496 | |
| 4167 | const op_result_ty = lhs.ty.scalarType(zcu); | 4497 | const op_result_ty = lhs.ty.scalarType(zcu); |
| 4168 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); | 4498 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| ... | @@ -4174,7 +4504,7 @@ fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { | ... | @@ -4174,7 +4504,7 @@ fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { |
| 4174 | const set = try cg.importExtendedSet(); | 4504 | const set = try cg.importExtendedSet(); |
| 4175 | const opcode = op.extInstOpcode(target, info); | 4505 | const opcode = op.extInstOpcode(target, info); |
| 4176 | for (0..ops) |i| { | 4506 | for (0..ops) |i| { |
| 4177 | try cg.body.emit(cg.module.gpa, .OpExtInst, .{ | 4507 | try cg.body.emit(cg.gpa, .OpExtInst, .{ |
| 4178 | .id_result_type = op_result_ty_id, | 4508 | .id_result_type = op_result_ty_id, |
| 4179 | .id_result = results.at(i), | 4509 | .id_result = results.at(i), |
| 4180 | .set = set, | 4510 | .set = set, |
| ... | @@ -4195,7 +4525,7 @@ fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { | ... | @@ -4195,7 +4525,7 @@ fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { |
| 4195 | /// All other values are returned unmodified (this makes strange integer | 4525 | /// All other values are returned unmodified (this makes strange integer |
| 4196 | /// wrapping easier to use in generic operations). | 4526 | /// wrapping easier to use in generic operations). |
| 4197 | fn normalize(cg: *CodeGen, value: Temporary, info: ArithmeticTypeInfo) !Temporary { | 4527 | fn normalize(cg: *CodeGen, value: Temporary, info: ArithmeticTypeInfo) !Temporary { |
| 4198 | const zcu = cg.module.zcu; | 4528 | const zcu = cg.zcu; |
| 4199 | const ty = value.ty; | 4529 | const ty = value.ty; |
| 4200 | switch (info.class) { | 4530 | switch (info.class) { |
| 4201 | .integer, .bool, .float => return value, | 4531 | .integer, .bool, .float => return value, |
| ... | @@ -4341,29 +4671,24 @@ fn airArithOp( | ... | @@ -4341,29 +4671,24 @@ fn airArithOp( |
| 4341 | } | 4671 | } |
| 4342 | | 4672 | |
| 4343 | fn airAbs(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 4673 | fn airAbs(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| | 4674 | const zcu = cg.zcu; |
| | 4675 | const target = zcu.getTarget(); |
| 4344 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 4676 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4345 | const operand = try cg.temporary(ty_op.operand); | 4677 | const value = try cg.temporary(ty_op.operand); |
| 4346 | // Note: operand_ty may be signed, while ty is always unsigned! | 4678 | // Note: operand_ty may be signed, while ty is always unsigned. |
| 4347 | const result_ty = cg.typeOfIndex(inst); | 4679 | const result_ty = cg.typeOfIndex(inst); |
| 4348 | const result = try cg.abs(result_ty, operand); | | |
| 4349 | return try result.materialize(cg); | | |
| 4350 | } | | |
| 4351 | | | |
| 4352 | fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { | | |
| 4353 | const zcu = cg.module.zcu; | | |
| 4354 | const target = cg.module.zcu.getTarget(); | | |
| 4355 | const operand_info = cg.arithmeticTypeInfo(value.ty); | 4680 | const operand_info = cg.arithmeticTypeInfo(value.ty); |
| 4356 | switch (operand_info.class) { | 4681 | const result: Temporary = switch (operand_info.class) { |
| 4357 | .float => return try cg.buildUnary(.f_abs, value), | 4682 | .float => try cg.buildUnary(.f_abs, value), |
| 4358 | .integer, .strange_integer => { | 4683 | .integer, .strange_integer => abs: { |
| 4359 | var abs_value = try cg.buildUnary(.i_abs, value); | 4684 | var abs_value = try cg.buildUnary(.i_abs, value); |
| 4360 | switch (target.os.tag) { | 4685 | switch (target.os.tag) { |
| 4361 | .vulkan, .opengl => { | 4686 | .vulkan, .opengl => { |
| 4362 | if (value.ty.intInfo(zcu).signedness == .signed) { | 4687 | if (value.ty.intInfo(zcu).signedness == .signed) { |
| 4363 | const abs_id = try abs_value.materialize(cg); | 4688 | const abs_id = try abs_value.materialize(cg); |
| 4364 | const dst_ty_id = try cg.resolveType(result_ty, .direct); | 4689 | const dst_ty_id = try cg.resolveType(result_ty, .direct); |
| 4365 | const cast_id = cg.module.allocId(); | 4690 | const cast_id = cg.allocId(); |
| 4366 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | 4691 | try cg.body.emit(cg.gpa, .OpBitcast, .{ |
| 4367 | .id_result_type = dst_ty_id, | 4692 | .id_result_type = dst_ty_id, |
| 4368 | .id_result = cast_id, | 4693 | .id_result = cast_id, |
| 4369 | .operand = abs_id, | 4694 | .operand = abs_id, |
| ... | @@ -4373,9 +4698,9 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { | ... | @@ -4373,9 +4698,9 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { |
| 4373 | }, | 4698 | }, |
| 4374 | else => {}, | 4699 | else => {}, |
| 4375 | } | 4700 | } |
| 4376 | return try cg.normalize(abs_value, cg.arithmeticTypeInfo(result_ty)); | 4701 | break :abs try cg.normalize(abs_value, cg.arithmeticTypeInfo(result_ty)); |
| 4377 | }, | 4702 | }, |
| 4378 | .composite_integer => { | 4703 | .composite_integer => abs: { |
| 4379 | const val_id = try value.materialize(cg); | 4704 | const val_id = try value.materialize(cg); |
| 4380 | const scratch_top = cg.id_scratch.items.len; | 4705 | const scratch_top = cg.id_scratch.items.len; |
| 4381 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 4706 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| ... | @@ -4385,10 +4710,10 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { | ... | @@ -4385,10 +4710,10 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { |
| 4385 | const ci_neg = try ci_z.addSub(ci, false); | 4710 | const ci_neg = try ci_z.addSub(ci, false); |
| 4386 | const result_info = cg.arithmeticTypeInfo(result_ty); | 4711 | const result_info = cg.arithmeticTypeInfo(result_ty); |
| 4387 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 4712 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 4388 | const result_limbs = try cg.id_scratch.addManyAsSlice(cg.module.gpa, ci.n_limbs); | 4713 | const result_limbs = try cg.id_scratch.addManyAsSlice(cg.gpa, ci.n_limbs); |
| 4389 | for (0..ci.n_limbs) |i| { | 4714 | for (0..ci.n_limbs) |i| { |
| 4390 | result_limbs[i] = cg.module.allocId(); | 4715 | result_limbs[i] = cg.allocId(); |
| 4391 | try cg.body.emit(cg.module.gpa, .OpSelect, .{ | 4716 | try cg.body.emit(cg.gpa, .OpSelect, .{ |
| 4392 | .id_result_type = u32_ty_id, | 4717 | .id_result_type = u32_ty_id, |
| 4393 | .id_result = result_limbs[i], | 4718 | .id_result = result_limbs[i], |
| 4394 | .condition = is_neg, | 4719 | .condition = is_neg, |
| ... | @@ -4398,10 +4723,11 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { | ... | @@ -4398,10 +4723,11 @@ fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { |
| 4398 | } | 4723 | } |
| 4399 | const ci_result = CompositeInt.fromLimbs(cg, result_limbs, result_info); | 4724 | const ci_result = CompositeInt.fromLimbs(cg, result_limbs, result_info); |
| 4400 | const normalized = try ci_result.normalize(); | 4725 | const normalized = try ci_result.normalize(); |
| 4401 | return .init(result_ty, try normalized.materialize(result_ty)); | 4726 | break :abs .init(result_ty, try normalized.materialize(result_ty)); |
| 4402 | }, | 4727 | }, |
| 4403 | .bool => unreachable, | 4728 | .bool => unreachable, |
| 4404 | } | 4729 | }; |
| | 4730 | return try result.materialize(cg); |
| 4405 | } | 4731 | } |
| 4406 | | 4732 | |
| 4407 | fn airAddSubOverflow( | 4733 | fn airAddSubOverflow( |
| ... | @@ -4457,32 +4783,36 @@ fn airAddSubOverflow( | ... | @@ -4457,32 +4783,36 @@ fn airAddSubOverflow( |
| 4457 | const res_neg = try ci_res2.cmp(ci_z, .lt); | 4783 | const res_neg = try ci_res2.cmp(ci_z, .lt); |
| 4458 | | 4784 | |
| 4459 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 4785 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 4460 | const signs_match = cg.module.allocId(); | 4786 | const signs_match = cg.allocId(); |
| 4461 | try cg.body.emitRaw(cg.module.gpa, .OpLogicalEqual, 4); | 4787 | try cg.body.emit(cg.gpa, .OpLogicalEqual, .{ |
| 4462 | cg.body.writeOperand(Id, bool_ty_id); | 4788 | .id_result_type = bool_ty_id, |
| 4463 | cg.body.writeOperand(Id, signs_match); | 4789 | .id_result = signs_match, |
| 4464 | cg.body.writeOperand(Id, lhs_neg); | 4790 | .operand_1 = lhs_neg, |
| 4465 | cg.body.writeOperand(Id, rhs_neg); | 4791 | .operand_2 = rhs_neg, |
| 4466 | const res_sign_diff = cg.module.allocId(); | 4792 | }); |
| 4467 | try cg.body.emitRaw(cg.module.gpa, .OpLogicalNotEqual, 4); | 4793 | const res_sign_diff = cg.allocId(); |
| 4468 | cg.body.writeOperand(Id, bool_ty_id); | 4794 | try cg.body.emit(cg.gpa, .OpLogicalNotEqual, .{ |
| 4469 | cg.body.writeOperand(Id, res_sign_diff); | 4795 | .id_result_type = bool_ty_id, |
| 4470 | cg.body.writeOperand(Id, lhs_neg); | 4796 | .id_result = res_sign_diff, |
| 4471 | cg.body.writeOperand(Id, res_neg); | 4797 | .operand_1 = lhs_neg, |
| | 4798 | .operand_2 = res_neg, |
| | 4799 | }); |
| 4472 | const ov_cond = if (add == .OpIAdd) signs_match else blk2: { | 4800 | const ov_cond = if (add == .OpIAdd) signs_match else blk2: { |
| 4473 | const not_match = cg.module.allocId(); | 4801 | const not_match = cg.allocId(); |
| 4474 | try cg.body.emitRaw(cg.module.gpa, .OpLogicalNot, 3); | 4802 | try cg.body.emit(cg.gpa, .OpLogicalNot, .{ |
| 4475 | cg.body.writeOperand(Id, bool_ty_id); | 4803 | .id_result_type = bool_ty_id, |
| 4476 | cg.body.writeOperand(Id, not_match); | 4804 | .id_result = not_match, |
| 4477 | cg.body.writeOperand(Id, signs_match); | 4805 | .operand = signs_match, |
| | 4806 | }); |
| 4478 | break :blk2 not_match; | 4807 | break :blk2 not_match; |
| 4479 | }; | 4808 | }; |
| 4480 | const ov_result = cg.module.allocId(); | 4809 | const ov_result = cg.allocId(); |
| 4481 | try cg.body.emitRaw(cg.module.gpa, .OpLogicalAnd, 4); | 4810 | try cg.body.emit(cg.gpa, .OpLogicalAnd, .{ |
| 4482 | cg.body.writeOperand(Id, bool_ty_id); | 4811 | .id_result_type = bool_ty_id, |
| 4483 | cg.body.writeOperand(Id, ov_result); | 4812 | .id_result = ov_result, |
| 4484 | cg.body.writeOperand(Id, ov_cond); | 4813 | .operand_1 = ov_cond, |
| 4485 | cg.body.writeOperand(Id, res_sign_diff); | 4814 | .operand_2 = res_sign_diff, |
| | 4815 | }); |
| 4486 | break :blk ov_result; | 4816 | break :blk ov_result; |
| 4487 | }, | 4817 | }, |
| 4488 | }; | 4818 | }; |
| ... | @@ -4531,7 +4861,7 @@ fn airAddSubOverflow( | ... | @@ -4531,7 +4861,7 @@ fn airAddSubOverflow( |
| 4531 | | 4861 | |
| 4532 | fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 4862 | fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4533 | const pt = cg.pt; | 4863 | const pt = cg.pt; |
| 4534 | const gpa = cg.module.gpa; | 4864 | const gpa = cg.gpa; |
| 4535 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 4865 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4536 | const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; | 4866 | const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4537 | const lhs = try cg.temporary(extra.lhs); | 4867 | const lhs = try cg.temporary(extra.lhs); |
| ... | @@ -4559,32 +4889,35 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4559,32 +4889,35 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4559 | | 4889 | |
| 4560 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 4890 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 4561 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 4891 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 4562 | const n: usize = info.backing_bits / Module.big_int_bits; | 4892 | const n: usize = info.backing_bits / big_int_bits; |
| 4563 | | 4893 | |
| 4564 | const ov_bool = switch (info.signedness) { | 4894 | const ov_bool = switch (info.signedness) { |
| 4565 | .unsigned => blk: { | 4895 | .unsigned => blk: { |
| 4566 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); | 4896 | const zero_id = try cg.constInt(.u32, @as(u32, 0)); |
| 4567 | var any_nonzero = cg.module.allocId(); | 4897 | var any_nonzero = cg.allocId(); |
| 4568 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 4898 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 4569 | cg.body.writeOperand(Id, bool_ty_id); | 4899 | .id_result_type = bool_ty_id, |
| 4570 | cg.body.writeOperand(Id, any_nonzero); | 4900 | .id_result = any_nonzero, |
| 4571 | cg.body.writeOperand(Id, high_limbs[0]); | 4901 | .operand_1 = high_limbs[0], |
| 4572 | cg.body.writeOperand(Id, zero_id); | 4902 | .operand_2 = zero_id, |
| | 4903 | }); |
| 4573 | | 4904 | |
| 4574 | for (1..n) |i| { | 4905 | for (1..n) |i| { |
| 4575 | const limb_nz = cg.module.allocId(); | 4906 | const limb_nz = cg.allocId(); |
| 4576 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 4907 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 4577 | cg.body.writeOperand(Id, bool_ty_id); | 4908 | .id_result_type = bool_ty_id, |
| 4578 | cg.body.writeOperand(Id, limb_nz); | 4909 | .id_result = limb_nz, |
| 4579 | cg.body.writeOperand(Id, high_limbs[i]); | 4910 | .operand_1 = high_limbs[i], |
| 4580 | cg.body.writeOperand(Id, zero_id); | 4911 | .operand_2 = zero_id, |
| 4581 | | 4912 | }); |
| 4582 | const combined = cg.module.allocId(); | 4913 | |
| 4583 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 4914 | const combined = cg.allocId(); |
| 4584 | cg.body.writeOperand(Id, bool_ty_id); | 4915 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 4585 | cg.body.writeOperand(Id, combined); | 4916 | .id_result_type = bool_ty_id, |
| 4586 | cg.body.writeOperand(Id, any_nonzero); | 4917 | .id_result = combined, |
| 4587 | cg.body.writeOperand(Id, limb_nz); | 4918 | .operand_1 = any_nonzero, |
| | 4919 | .operand_2 = limb_nz, |
| | 4920 | }); |
| 4588 | any_nonzero = combined; | 4921 | any_nonzero = combined; |
| 4589 | } | 4922 | } |
| 4590 | | 4923 | |
| ... | @@ -4595,92 +4928,99 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4595,92 +4928,99 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4595 | const top_limb = ci_res.limbs[n - 1]; | 4928 | const top_limb = ci_res.limbs[n - 1]; |
| 4596 | const i32_ty_id = try cg.resolveType(.i32, .direct); | 4929 | const i32_ty_id = try cg.resolveType(.i32, .direct); |
| 4597 | | 4930 | |
| 4598 | const top_bits: u16 = if (info.bits % Module.big_int_bits == 0) | 4931 | const top_bits: u16 = if (info.bits % big_int_bits == 0) |
| 4599 | Module.big_int_bits | 4932 | big_int_bits |
| 4600 | else | 4933 | else |
| 4601 | info.bits % Module.big_int_bits; | 4934 | info.bits % big_int_bits; |
| 4602 | | 4935 | |
| 4603 | const shift_amt: u32 = top_bits - 1; | 4936 | const shift_amt: u32 = top_bits - 1; |
| 4604 | const shift_id = try cg.constInt(.u32, shift_amt); | 4937 | const shift_id = try cg.constInt(.u32, shift_amt); |
| 4605 | | 4938 | |
| 4606 | const as_signed = cg.module.allocId(); | 4939 | const as_signed = cg.allocId(); |
| 4607 | try cg.body.emit(gpa, .OpBitcast, .{ | 4940 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 4608 | .id_result_type = i32_ty_id, | 4941 | .id_result_type = i32_ty_id, |
| 4609 | .id_result = as_signed, | 4942 | .id_result = as_signed, |
| 4610 | .operand = top_limb, | 4943 | .operand = top_limb, |
| 4611 | }); | 4944 | }); |
| 4612 | const sign_ext = cg.module.allocId(); | 4945 | const sign_ext = cg.allocId(); |
| 4613 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 4946 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 4614 | cg.body.writeOperand(Id, i32_ty_id); | 4947 | .id_result_type = i32_ty_id, |
| 4615 | cg.body.writeOperand(Id, sign_ext); | 4948 | .id_result = sign_ext, |
| 4616 | cg.body.writeOperand(Id, as_signed); | 4949 | .base = as_signed, |
| 4617 | cg.body.writeOperand(Id, shift_id); | 4950 | .shift = shift_id, |
| 4618 | const expected = cg.module.allocId(); | 4951 | }); |
| | 4952 | const expected = cg.allocId(); |
| 4619 | try cg.body.emit(gpa, .OpBitcast, .{ | 4953 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 4620 | .id_result_type = u32_ty_id, | 4954 | .id_result_type = u32_ty_id, |
| 4621 | .id_result = expected, | 4955 | .id_result = expected, |
| 4622 | .operand = sign_ext, | 4956 | .operand = sign_ext, |
| 4623 | }); | 4957 | }); |
| 4624 | | 4958 | |
| 4625 | var any_mismatch = cg.module.allocId(); | 4959 | var any_mismatch = cg.allocId(); |
| 4626 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 4960 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 4627 | cg.body.writeOperand(Id, bool_ty_id); | 4961 | .id_result_type = bool_ty_id, |
| 4628 | cg.body.writeOperand(Id, any_mismatch); | 4962 | .id_result = any_mismatch, |
| 4629 | cg.body.writeOperand(Id, high_limbs[0]); | 4963 | .operand_1 = high_limbs[0], |
| 4630 | cg.body.writeOperand(Id, expected); | 4964 | .operand_2 = expected, |
| | 4965 | }); |
| 4631 | | 4966 | |
| 4632 | for (1..n) |i| { | 4967 | for (1..n) |i| { |
| 4633 | const limb_ne = cg.module.allocId(); | 4968 | const limb_ne = cg.allocId(); |
| 4634 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 4969 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 4635 | cg.body.writeOperand(Id, bool_ty_id); | 4970 | .id_result_type = bool_ty_id, |
| 4636 | cg.body.writeOperand(Id, limb_ne); | 4971 | .id_result = limb_ne, |
| 4637 | cg.body.writeOperand(Id, high_limbs[i]); | 4972 | .operand_1 = high_limbs[i], |
| 4638 | cg.body.writeOperand(Id, expected); | 4973 | .operand_2 = expected, |
| 4639 | | 4974 | }); |
| 4640 | const combined = cg.module.allocId(); | 4975 | |
| 4641 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 4976 | const combined = cg.allocId(); |
| 4642 | cg.body.writeOperand(Id, bool_ty_id); | 4977 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 4643 | cg.body.writeOperand(Id, combined); | 4978 | .id_result_type = bool_ty_id, |
| 4644 | cg.body.writeOperand(Id, any_mismatch); | 4979 | .id_result = combined, |
| 4645 | cg.body.writeOperand(Id, limb_ne); | 4980 | .operand_1 = any_mismatch, |
| | 4981 | .operand_2 = limb_ne, |
| | 4982 | }); |
| 4646 | any_mismatch = combined; | 4983 | any_mismatch = combined; |
| 4647 | } | 4984 | } |
| 4648 | | 4985 | |
| 4649 | if (info.bits != info.backing_bits) { | 4986 | if (info.bits != info.backing_bits) { |
| 4650 | const top_bits_s: u16 = info.bits % Module.big_int_bits; | 4987 | const top_bits_s: u16 = info.bits % big_int_bits; |
| 4651 | const s_shift_id = try cg.constInt(.u32, top_bits_s - 1); | 4988 | const s_shift_id = try cg.constInt(.u32, top_bits_s - 1); |
| 4652 | | 4989 | |
| 4653 | const top_as_signed = cg.module.allocId(); | 4990 | const top_as_signed = cg.allocId(); |
| 4654 | try cg.body.emit(gpa, .OpBitcast, .{ | 4991 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 4655 | .id_result_type = i32_ty_id, | 4992 | .id_result_type = i32_ty_id, |
| 4656 | .id_result = top_as_signed, | 4993 | .id_result = top_as_signed, |
| 4657 | .operand = top_limb, | 4994 | .operand = top_limb, |
| 4658 | }); | 4995 | }); |
| 4659 | const top_sign_ext = cg.module.allocId(); | 4996 | const top_sign_ext = cg.allocId(); |
| 4660 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 4997 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 4661 | cg.body.writeOperand(Id, i32_ty_id); | 4998 | .id_result_type = i32_ty_id, |
| 4662 | cg.body.writeOperand(Id, top_sign_ext); | 4999 | .id_result = top_sign_ext, |
| 4663 | cg.body.writeOperand(Id, top_as_signed); | 5000 | .base = top_as_signed, |
| 4664 | cg.body.writeOperand(Id, s_shift_id); | 5001 | .shift = s_shift_id, |
| 4665 | const top_expected = cg.module.allocId(); | 5002 | }); |
| | 5003 | const top_expected = cg.allocId(); |
| 4666 | try cg.body.emit(gpa, .OpBitcast, .{ | 5004 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 4667 | .id_result_type = u32_ty_id, | 5005 | .id_result_type = u32_ty_id, |
| 4668 | .id_result = top_expected, | 5006 | .id_result = top_expected, |
| 4669 | .operand = top_sign_ext, | 5007 | .operand = top_sign_ext, |
| 4670 | }); | 5008 | }); |
| 4671 | const top_mismatch = cg.module.allocId(); | 5009 | const top_mismatch = cg.allocId(); |
| 4672 | try cg.body.emitRaw(gpa, .OpINotEqual, 4); | 5010 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 4673 | cg.body.writeOperand(Id, bool_ty_id); | 5011 | .id_result_type = bool_ty_id, |
| 4674 | cg.body.writeOperand(Id, top_mismatch); | 5012 | .id_result = top_mismatch, |
| 4675 | cg.body.writeOperand(Id, top_limb); | 5013 | .operand_1 = top_limb, |
| 4676 | cg.body.writeOperand(Id, top_expected); | 5014 | .operand_2 = top_expected, |
| 4677 | | 5015 | }); |
| 4678 | const combined = cg.module.allocId(); | 5016 | |
| 4679 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 5017 | const combined = cg.allocId(); |
| 4680 | cg.body.writeOperand(Id, bool_ty_id); | 5018 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 4681 | cg.body.writeOperand(Id, combined); | 5019 | .id_result_type = bool_ty_id, |
| 4682 | cg.body.writeOperand(Id, any_mismatch); | 5020 | .id_result = combined, |
| 4683 | cg.body.writeOperand(Id, top_mismatch); | 5021 | .operand_1 = any_mismatch, |
| | 5022 | .operand_2 = top_mismatch, |
| | 5023 | }); |
| 4684 | any_mismatch = combined; | 5024 | any_mismatch = combined; |
| 4685 | } | 5025 | } |
| 4686 | | 5026 | |
| ... | @@ -4702,7 +5042,8 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4702,7 +5042,8 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4702 | // - Additionally, if info.bits != 32, we'll have to check the high bits | 5042 | // - Additionally, if info.bits != 32, we'll have to check the high bits |
| 4703 | // of the result too. | 5043 | // of the result too. |
| 4704 | | 5044 | |
| 4705 | const largest_int_bits = cg.largestSupportedIntBits(); | 5045 | const target = cg.zcu.getTarget(); |
| | 5046 | const largest_int_bits: u16 = if (target.cpu.has(.spirv, .int64) or target.cpu.arch == .spirv64) 64 else 32; |
| 4706 | // If non-null, the number of bits that the multiplication should be performed in. If | 5047 | // If non-null, the number of bits that the multiplication should be performed in. If |
| 4707 | // null, we have to use wide multiplication. | 5048 | // null, we have to use wide multiplication. |
| 4708 | const maybe_op_ty_bits: ?u16 = switch (info.bits) { | 5049 | const maybe_op_ty_bits: ?u16 = switch (info.bits) { |
| ... | @@ -4846,7 +5187,7 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4846,7 +5187,7 @@ fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4846 | } | 5187 | } |
| 4847 | | 5188 | |
| 4848 | fn airShlOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 5189 | fn airShlOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4849 | const zcu = cg.module.zcu; | 5190 | const zcu = cg.zcu; |
| 4850 | | 5191 | |
| 4851 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 5192 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4852 | const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; | 5193 | const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -4898,14 +5239,48 @@ fn airMulAdd(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4898,14 +5239,48 @@ fn airMulAdd(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4898 | const info = cg.arithmeticTypeInfo(result_ty); | 5239 | const info = cg.arithmeticTypeInfo(result_ty); |
| 4899 | assert(info.class == .float); // .mul_add is only emitted for floats | 5240 | assert(info.class == .float); // .mul_add is only emitted for floats |
| 4900 | | 5241 | |
| 4901 | const result = try cg.buildFma(a, b, c); | 5242 | const zcu = cg.zcu; |
| | 5243 | const target = zcu.getTarget(); |
| | 5244 | |
| | 5245 | const v = cg.vectorization(.{ a, b, c }); |
| | 5246 | const ops = v.components(); |
| | 5247 | const results = cg.allocIds(ops); |
| | 5248 | |
| | 5249 | const op_result_ty = a.ty.scalarType(zcu); |
| | 5250 | const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); |
| | 5251 | const result_temp_ty = try v.resultType(cg, a.ty); |
| | 5252 | |
| | 5253 | const op_a = try v.prepare(cg, a); |
| | 5254 | const op_b = try v.prepare(cg, b); |
| | 5255 | const op_c = try v.prepare(cg, c); |
| | 5256 | |
| | 5257 | const set = try cg.importExtendedSet(); |
| | 5258 | const opcode: u32 = switch (target.os.tag) { |
| | 5259 | .opencl => @intFromEnum(spec.OpenClOpcode.fma), |
| | 5260 | // NOTE: Vulkan's FMA does not meet Zig's nor OpenCL's precision guarantees and needs |
| | 5261 | // to be emulated. |
| | 5262 | .vulkan, .opengl => @intFromEnum(spec.GlslOpcode.Fma), |
| | 5263 | else => unreachable, |
| | 5264 | }; |
| | 5265 | |
| | 5266 | for (0..ops) |i| { |
| | 5267 | try cg.body.emit(cg.gpa, .OpExtInst, .{ |
| | 5268 | .id_result_type = op_result_ty_id, |
| | 5269 | .id_result = results.at(i), |
| | 5270 | .set = set, |
| | 5271 | .instruction = .{ .inst = opcode }, |
| | 5272 | .id_ref_4 = &.{ op_a.at(i), op_b.at(i), op_c.at(i) }, |
| | 5273 | }); |
| | 5274 | } |
| | 5275 | |
| | 5276 | const result = v.finalize(result_temp_ty, results); |
| 4902 | return try result.materialize(cg); | 5277 | return try result.materialize(cg); |
| 4903 | } | 5278 | } |
| 4904 | | 5279 | |
| 4905 | fn airClzCtz(cg: *CodeGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { | 5280 | fn airClzCtz(cg: *CodeGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { |
| 4906 | if (cg.liveness.isUnused(inst)) return null; | 5281 | if (cg.liveness.isUnused(inst)) return null; |
| 4907 | | 5282 | |
| 4908 | const zcu = cg.module.zcu; | 5283 | const zcu = cg.zcu; |
| 4909 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5284 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4910 | const operand = try cg.temporary(ty_op.operand); | 5285 | const operand = try cg.temporary(ty_op.operand); |
| 4911 | | 5286 | |
| ... | @@ -4948,7 +5323,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -4948,7 +5323,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4948 | } | 5323 | } |
| 4949 | | 5324 | |
| 4950 | fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 5325 | fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 4951 | const zcu = cg.module.zcu; | 5326 | const zcu = cg.zcu; |
| 4952 | const reduce = cg.air.instructions.items(.data)[@intFromEnum(inst)].reduce; | 5327 | const reduce = cg.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 4953 | const operand = try cg.resolve(reduce.operand); | 5328 | const operand = try cg.resolve(reduce.operand); |
| 4954 | const operand_ty = cg.typeOf(reduce.operand); | 5329 | const operand_ty = cg.typeOf(reduce.operand); |
| ... | @@ -5016,7 +5391,7 @@ fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5016,7 +5391,7 @@ fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5016 | } | 5391 | } |
| 5017 | | 5392 | |
| 5018 | fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 5393 | fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5019 | const zcu = cg.module.zcu; | 5394 | const zcu = cg.zcu; |
| 5020 | const gpa = zcu.gpa; | 5395 | const gpa = zcu.gpa; |
| 5021 | | 5396 | |
| 5022 | const unwrapped = cg.air.unwrapShuffleOne(zcu, inst); | 5397 | const unwrapped = cg.air.unwrapShuffleOne(zcu, inst); |
| ... | @@ -5041,7 +5416,7 @@ fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5041,7 +5416,7 @@ fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5041 | } | 5416 | } |
| 5042 | | 5417 | |
| 5043 | fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 5418 | fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5044 | const zcu = cg.module.zcu; | 5419 | const zcu = cg.zcu; |
| 5045 | const gpa = zcu.gpa; | 5420 | const gpa = zcu.gpa; |
| 5046 | | 5421 | |
| 5047 | const unwrapped = cg.air.unwrapShuffleTwo(zcu, inst); | 5422 | const unwrapped = cg.air.unwrapShuffleTwo(zcu, inst); |
| ... | @@ -5060,7 +5435,7 @@ fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5060,7 +5435,7 @@ fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5060 | id.* = switch (mask_elem.unwrap()) { | 5435 | id.* = switch (mask_elem.unwrap()) { |
| 5061 | .a_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_a, idx), | 5436 | .a_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_a, idx), |
| 5062 | .b_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_b, idx), | 5437 | .b_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_b, idx), |
| 5063 | .undef => try cg.module.constUndef(elem_ty_id), | 5438 | .undef => try cg.constUndef(elem_ty_id), |
| 5064 | }; | 5439 | }; |
| 5065 | } | 5440 | } |
| 5066 | | 5441 | |
| ... | @@ -5074,8 +5449,8 @@ fn accessChainId( | ... | @@ -5074,8 +5449,8 @@ fn accessChainId( |
| 5074 | base: Id, | 5449 | base: Id, |
| 5075 | indices: []const Id, | 5450 | indices: []const Id, |
| 5076 | ) !Id { | 5451 | ) !Id { |
| 5077 | const result_id = cg.module.allocId(); | 5452 | const result_id = cg.allocId(); |
| 5078 | try cg.body.emit(cg.module.gpa, .OpInBoundsAccessChain, .{ | 5453 | try cg.body.emit(cg.gpa, .OpInBoundsAccessChain, .{ |
| 5079 | .id_result_type = result_ty_id, | 5454 | .id_result_type = result_ty_id, |
| 5080 | .id_result = result_id, | 5455 | .id_result = result_id, |
| 5081 | .base = base, | 5456 | .base = base, |
| ... | @@ -5094,7 +5469,7 @@ fn accessChain( | ... | @@ -5094,7 +5469,7 @@ fn accessChain( |
| 5094 | base: Id, | 5469 | base: Id, |
| 5095 | indices: []const u32, | 5470 | indices: []const u32, |
| 5096 | ) !Id { | 5471 | ) !Id { |
| 5097 | const gpa = cg.module.gpa; | 5472 | const gpa = cg.gpa; |
| 5098 | const scratch_top = cg.id_scratch.items.len; | 5473 | const scratch_top = cg.id_scratch.items.len; |
| 5099 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 5474 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| 5100 | const ids = try cg.id_scratch.addManyAsSlice(gpa, indices.len); | 5475 | const ids = try cg.id_scratch.addManyAsSlice(gpa, indices.len); |
| ... | @@ -5111,8 +5486,8 @@ fn ptrAccessChain( | ... | @@ -5111,8 +5486,8 @@ fn ptrAccessChain( |
| 5111 | element: Id, | 5486 | element: Id, |
| 5112 | indices: []const u32, | 5487 | indices: []const u32, |
| 5113 | ) !Id { | 5488 | ) !Id { |
| 5114 | const gpa = cg.module.gpa; | 5489 | const gpa = cg.gpa; |
| 5115 | const target = cg.module.zcu.getTarget(); | 5490 | const target = cg.zcu.getTarget(); |
| 5116 | | 5491 | |
| 5117 | const scratch_top = cg.id_scratch.items.len; | 5492 | const scratch_top = cg.id_scratch.items.len; |
| 5118 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 5493 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| ... | @@ -5121,7 +5496,7 @@ fn ptrAccessChain( | ... | @@ -5121,7 +5496,7 @@ fn ptrAccessChain( |
| 5121 | id.* = try cg.constInt(.u32, index); | 5496 | id.* = try cg.constInt(.u32, index); |
| 5122 | } | 5497 | } |
| 5123 | | 5498 | |
| 5124 | const result_id = cg.module.allocId(); | 5499 | const result_id = cg.allocId(); |
| 5125 | switch (target.os.tag) { | 5500 | switch (target.os.tag) { |
| 5126 | .opencl, .amdhsa => { | 5501 | .opencl, .amdhsa => { |
| 5127 | try cg.body.emit(gpa, .OpInBoundsPtrAccessChain, .{ | 5502 | try cg.body.emit(gpa, .OpInBoundsPtrAccessChain, .{ |
| ... | @@ -5147,7 +5522,7 @@ fn ptrAccessChain( | ... | @@ -5147,7 +5522,7 @@ fn ptrAccessChain( |
| 5147 | } | 5522 | } |
| 5148 | | 5523 | |
| 5149 | fn ptrAdd(cg: *CodeGen, result_ty: Type, ptr_ty: Type, ptr_id: Id, offset_id: Id) !Id { | 5524 | fn ptrAdd(cg: *CodeGen, result_ty: Type, ptr_ty: Type, ptr_id: Id, offset_id: Id) !Id { |
| 5150 | const zcu = cg.module.zcu; | 5525 | const zcu = cg.zcu; |
| 5151 | const result_ty_id = try cg.resolveType(result_ty, .direct); | 5526 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 5152 | | 5527 | |
| 5153 | switch (ptr_ty.ptrSize(zcu)) { | 5528 | switch (ptr_ty.ptrSize(zcu)) { |
| ... | @@ -5188,8 +5563,8 @@ fn airPtrSub(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5188,8 +5563,8 @@ fn airPtrSub(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5188 | const offset_ty_id = try cg.resolveType(offset_ty, .direct); | 5563 | const offset_ty_id = try cg.resolveType(offset_ty, .direct); |
| 5189 | const result_ty = cg.typeOfIndex(inst); | 5564 | const result_ty = cg.typeOfIndex(inst); |
| 5190 | | 5565 | |
| 5191 | const negative_offset_id = cg.module.allocId(); | 5566 | const negative_offset_id = cg.allocId(); |
| 5192 | try cg.body.emit(cg.module.gpa, .OpSNegate, .{ | 5567 | try cg.body.emit(cg.gpa, .OpSNegate, .{ |
| 5193 | .id_result_type = offset_ty_id, | 5568 | .id_result_type = offset_ty_id, |
| 5194 | .id_result = negative_offset_id, | 5569 | .id_result = negative_offset_id, |
| 5195 | .operand = offset_id, | 5570 | .operand = offset_id, |
| ... | @@ -5203,9 +5578,9 @@ fn cmp( | ... | @@ -5203,9 +5578,9 @@ fn cmp( |
| 5203 | lhs: Temporary, | 5578 | lhs: Temporary, |
| 5204 | rhs: Temporary, | 5579 | rhs: Temporary, |
| 5205 | ) !Temporary { | 5580 | ) !Temporary { |
| 5206 | const gpa = cg.module.gpa; | 5581 | const gpa = cg.gpa; |
| 5207 | const pt = cg.pt; | 5582 | const pt = cg.pt; |
| 5208 | const zcu = cg.module.zcu; | 5583 | const zcu = cg.zcu; |
| 5209 | const scalar_ty = lhs.ty.scalarType(zcu); | 5584 | const scalar_ty = lhs.ty.scalarType(zcu); |
| 5210 | const is_vector = lhs.ty.isVector(zcu); | 5585 | const is_vector = lhs.ty.isVector(zcu); |
| 5211 | | 5586 | |
| ... | @@ -5234,14 +5609,14 @@ fn cmp( | ... | @@ -5234,14 +5609,14 @@ fn cmp( |
| 5234 | | 5609 | |
| 5235 | const usize_ty_id = try cg.resolveType(.usize, .direct); | 5610 | const usize_ty_id = try cg.resolveType(.usize, .direct); |
| 5236 | | 5611 | |
| 5237 | const lhs_int_id = cg.module.allocId(); | 5612 | const lhs_int_id = cg.allocId(); |
| 5238 | try cg.body.emit(gpa, .OpConvertPtrToU, .{ | 5613 | try cg.body.emit(gpa, .OpConvertPtrToU, .{ |
| 5239 | .id_result_type = usize_ty_id, | 5614 | .id_result_type = usize_ty_id, |
| 5240 | .id_result = lhs_int_id, | 5615 | .id_result = lhs_int_id, |
| 5241 | .pointer = try lhs.materialize(cg), | 5616 | .pointer = try lhs.materialize(cg), |
| 5242 | }); | 5617 | }); |
| 5243 | | 5618 | |
| 5244 | const rhs_int_id = cg.module.allocId(); | 5619 | const rhs_int_id = cg.allocId(); |
| 5245 | try cg.body.emit(gpa, .OpConvertPtrToU, .{ | 5620 | try cg.body.emit(gpa, .OpConvertPtrToU, .{ |
| 5246 | .id_result_type = usize_ty_id, | 5621 | .id_result_type = usize_ty_id, |
| 5247 | .id_result = rhs_int_id, | 5622 | .id_result = rhs_int_id, |
| ... | @@ -5410,14 +5785,29 @@ fn bitCast( | ... | @@ -5410,14 +5785,29 @@ fn bitCast( |
| 5410 | src_ty: Type, | 5785 | src_ty: Type, |
| 5411 | src_id: Id, | 5786 | src_id: Id, |
| 5412 | ) !Id { | 5787 | ) !Id { |
| 5413 | const gpa = cg.module.gpa; | 5788 | const gpa = cg.gpa; |
| 5414 | const zcu = cg.module.zcu; | 5789 | const zcu = cg.zcu; |
| 5415 | const target = zcu.getTarget(); | 5790 | const target = zcu.getTarget(); |
| 5416 | const src_ty_id = try cg.resolveType(src_ty, .direct); | | |
| 5417 | const dst_ty_id = try cg.resolveType(dst_ty, .direct); | | |
| 5418 | | 5791 | |
| | 5792 | if (src_ty.toIntern() == dst_ty.toIntern()) return src_id; |
| | 5793 | if (src_ty.isPtrAtRuntime(zcu) and dst_ty.isPtrAtRuntime(zcu)) switch (target.os.tag) { |
| | 5794 | .vulkan, .opengl => if (src_ty.ptrAddressSpace(zcu) != .physical_storage_buffer) return src_id, |
| | 5795 | else => {}, |
| | 5796 | }; |
| | 5797 | |
| | 5798 | const dst_ty_id = try cg.resolveType(dst_ty, .direct); |
| 5419 | const result_id = blk: { | 5799 | const result_id = blk: { |
| 5420 | if (src_ty_id == dst_ty_id) break :blk src_id; | 5800 | // Big-int ↔ big-int bitcast: the indirect representation is an array, |
| | 5801 | // which OpBitcast cannot operate on. The arrays are bitwise identical |
| | 5802 | // apart from the top limb's padding; the normalize pass below fixes |
| | 5803 | // the padding. |
| | 5804 | if (src_ty.isInt(zcu) and dst_ty.isInt(zcu)) { |
| | 5805 | const src_info = src_ty.intInfo(zcu); |
| | 5806 | const dst_info = dst_ty.intInfo(zcu); |
| | 5807 | const src_backing, const src_big = cg.backingIntBits(src_info.bits); |
| | 5808 | const dst_backing, const dst_big = cg.backingIntBits(dst_info.bits); |
| | 5809 | if (src_backing == dst_backing and src_big and dst_big) break :blk src_id; |
| | 5810 | } |
| 5421 | | 5811 | |
| 5422 | // TODO: Some more cases are missing here | 5812 | // TODO: Some more cases are missing here |
| 5423 | // See fn bitCast in llvm.zig | 5813 | // See fn bitCast in llvm.zig |
| ... | @@ -5432,7 +5822,7 @@ fn bitCast( | ... | @@ -5432,7 +5822,7 @@ fn bitCast( |
| 5432 | } | 5822 | } |
| 5433 | } | 5823 | } |
| 5434 | | 5824 | |
| 5435 | const result_id = cg.module.allocId(); | 5825 | const result_id = cg.allocId(); |
| 5436 | try cg.body.emit(gpa, .OpConvertUToPtr, .{ | 5826 | try cg.body.emit(gpa, .OpConvertUToPtr, .{ |
| 5437 | .id_result_type = dst_ty_id, | 5827 | .id_result_type = dst_ty_id, |
| 5438 | .id_result = result_id, | 5828 | .id_result = result_id, |
| ... | @@ -5446,7 +5836,7 @@ fn bitCast( | ... | @@ -5446,7 +5836,7 @@ fn bitCast( |
| 5446 | // otherwise use a temporary and perform a pointer cast. | 5836 | // otherwise use a temporary and perform a pointer cast. |
| 5447 | const can_bitcast = (src_ty.isNumeric(zcu) and dst_ty.isNumeric(zcu)) or (src_ty.isPtrAtRuntime(zcu) and dst_ty.isPtrAtRuntime(zcu)); | 5837 | const can_bitcast = (src_ty.isNumeric(zcu) and dst_ty.isNumeric(zcu)) or (src_ty.isPtrAtRuntime(zcu) and dst_ty.isPtrAtRuntime(zcu)); |
| 5448 | if (can_bitcast) { | 5838 | if (can_bitcast) { |
| 5449 | const result_id = cg.module.allocId(); | 5839 | const result_id = cg.allocId(); |
| 5450 | try cg.body.emit(gpa, .OpBitcast, .{ | 5840 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5451 | .id_result_type = dst_ty_id, | 5841 | .id_result_type = dst_ty_id, |
| 5452 | .id_result = result_id, | 5842 | .id_result = result_id, |
| ... | @@ -5456,12 +5846,24 @@ fn bitCast( | ... | @@ -5456,12 +5846,24 @@ fn bitCast( |
| 5456 | break :blk result_id; | 5846 | break :blk result_id; |
| 5457 | } | 5847 | } |
| 5458 | | 5848 | |
| 5459 | const dst_ptr_ty_id = try cg.module.ptrType(dst_ty_id, .function); | 5849 | switch (target.os.tag) { |
| | 5850 | .vulkan, .opengl => { |
| | 5851 | // Logical addressing forbids OpBitcast on pointers. Allocate |
| | 5852 | // the temp with dst_ty so the load reads through a slot of the right type. |
| | 5853 | const dst_ty_indirect_id = try cg.resolveType(dst_ty, .indirect); |
| | 5854 | const tmp_id = try cg.alloc(dst_ty_indirect_id, null); |
| | 5855 | try cg.store(dst_ty, tmp_id, src_id, .{}); |
| | 5856 | break :blk try cg.load(dst_ty, tmp_id, .{}); |
| | 5857 | }, |
| | 5858 | else => {}, |
| | 5859 | } |
| | 5860 | |
| | 5861 | const dst_ptr_ty_id = try cg.ptrType(dst_ty_id, .function); |
| 5460 | | 5862 | |
| 5461 | const src_ty_indirect_id = try cg.resolveType(src_ty, .indirect); | 5863 | const src_ty_indirect_id = try cg.resolveType(src_ty, .indirect); |
| 5462 | const tmp_id = try cg.alloc(src_ty_indirect_id, null); | 5864 | const tmp_id = try cg.alloc(src_ty_indirect_id, null); |
| 5463 | try cg.store(src_ty, tmp_id, src_id, .{}); | 5865 | try cg.store(src_ty, tmp_id, src_id, .{}); |
| 5464 | const casted_ptr_id = cg.module.allocId(); | 5866 | const casted_ptr_id = cg.allocId(); |
| 5465 | try cg.body.emit(gpa, .OpBitcast, .{ | 5867 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5466 | .id_result_type = dst_ptr_ty_id, | 5868 | .id_result_type = dst_ptr_ty_id, |
| 5467 | .id_result = casted_ptr_id, | 5869 | .id_result = casted_ptr_id, |
| ... | @@ -5492,8 +5894,13 @@ fn airBitCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5492,8 +5894,13 @@ fn airBitCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5492 | const result = try cg.intFromBool(operand, .u1); | 5894 | const result = try cg.intFromBool(operand, .u1); |
| 5493 | return try result.materialize(cg); | 5895 | return try result.materialize(cg); |
| 5494 | } | 5896 | } |
| | 5897 | if (operand_ty.zigTypeTag(cg.zcu) == .pointer) { |
| | 5898 | switch (try cg.resolvePtr(ty_op.operand)) { |
| | 5899 | .tracked => |t| return t.id, // TODO |
| | 5900 | .id => |operand_id| return try cg.bitCast(result_ty, operand_ty, operand_id), |
| | 5901 | } |
| | 5902 | } |
| 5495 | const operand_id = try cg.resolve(ty_op.operand); | 5903 | const operand_id = try cg.resolve(ty_op.operand); |
| 5496 | if (cg.virtual_allocas.contains(operand_id)) return operand_id; | | |
| 5497 | return try cg.bitCast(result_ty, operand_ty, operand_id); | 5904 | return try cg.bitCast(result_ty, operand_ty, operand_id); |
| 5498 | } | 5905 | } |
| 5499 | | 5906 | |
| ... | @@ -5509,19 +5916,19 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5509,19 +5916,19 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5509 | const dst_composite = dst_info.class == .composite_integer; | 5916 | const dst_composite = dst_info.class == .composite_integer; |
| 5510 | | 5917 | |
| 5511 | if (src_composite or dst_composite) { | 5918 | if (src_composite or dst_composite) { |
| 5512 | const gpa = cg.module.gpa; | 5919 | const gpa = cg.gpa; |
| 5513 | const scratch_top = cg.id_scratch.items.len; | 5920 | const scratch_top = cg.id_scratch.items.len; |
| 5514 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 5921 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| 5515 | | 5922 | |
| 5516 | if (src_composite and dst_composite) { | 5923 | if (src_composite and dst_composite) { |
| 5517 | const src_id = try src.materialize(cg); | 5924 | const src_id = try src.materialize(cg); |
| 5518 | const src_n: u16 = src_info.backing_bits / Module.big_int_bits; | 5925 | const src_n: u16 = src_info.backing_bits / big_int_bits; |
| 5519 | const dst_n: u16 = dst_info.backing_bits / Module.big_int_bits; | 5926 | const dst_n: u16 = dst_info.backing_bits / big_int_bits; |
| 5520 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, dst_n); | 5927 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, dst_n); |
| 5521 | const min_n = @min(src_n, dst_n); | 5928 | const min_n = @min(src_n, dst_n); |
| 5522 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 5929 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 5523 | for (0..min_n) |i| { | 5930 | for (0..min_n) |i| { |
| 5524 | result_limbs[i] = cg.module.allocId(); | 5931 | result_limbs[i] = cg.allocId(); |
| 5525 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 5932 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 5526 | .id_result_type = u32_ty_id, | 5933 | .id_result_type = u32_ty_id, |
| 5527 | .id_result = result_limbs[i], | 5934 | .id_result = result_limbs[i], |
| ... | @@ -5533,20 +5940,21 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5533,20 +5940,21 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5533 | const fill = if (src_info.signedness == .signed) blk: { | 5940 | const fill = if (src_info.signedness == .signed) blk: { |
| 5534 | const i32_ty_id = try cg.resolveType(.i32, .direct); | 5941 | const i32_ty_id = try cg.resolveType(.i32, .direct); |
| 5535 | const msb = result_limbs[src_n - 1]; | 5942 | const msb = result_limbs[src_n - 1]; |
| 5536 | const msb_signed = cg.module.allocId(); | 5943 | const msb_signed = cg.allocId(); |
| 5537 | try cg.body.emit(gpa, .OpBitcast, .{ | 5944 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5538 | .id_result_type = i32_ty_id, | 5945 | .id_result_type = i32_ty_id, |
| 5539 | .id_result = msb_signed, | 5946 | .id_result = msb_signed, |
| 5540 | .operand = msb, | 5947 | .operand = msb, |
| 5541 | }); | 5948 | }); |
| 5542 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); | 5949 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); |
| 5543 | const sign_ext = cg.module.allocId(); | 5950 | const sign_ext = cg.allocId(); |
| 5544 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 5951 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 5545 | cg.body.writeOperand(Id, i32_ty_id); | 5952 | .id_result_type = i32_ty_id, |
| 5546 | cg.body.writeOperand(Id, sign_ext); | 5953 | .id_result = sign_ext, |
| 5547 | cg.body.writeOperand(Id, msb_signed); | 5954 | .base = msb_signed, |
| 5548 | cg.body.writeOperand(Id, shift31); | 5955 | .shift = shift31, |
| 5549 | const back = cg.module.allocId(); | 5956 | }); |
| | 5957 | const back = cg.allocId(); |
| 5550 | try cg.body.emit(gpa, .OpBitcast, .{ | 5958 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5551 | .id_result_type = u32_ty_id, | 5959 | .id_result_type = u32_ty_id, |
| 5552 | .id_result = back, | 5960 | .id_result = back, |
| ... | @@ -5565,7 +5973,7 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5565,7 +5973,7 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5565 | const src_id = try src.materialize(cg); | 5973 | const src_id = try src.materialize(cg); |
| 5566 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 5974 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 5567 | if (dst_info.backing_bits <= 32) { | 5975 | if (dst_info.backing_bits <= 32) { |
| 5568 | const limb0 = cg.module.allocId(); | 5976 | const limb0 = cg.allocId(); |
| 5569 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 5977 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 5570 | .id_result_type = u32_ty_id, | 5978 | .id_result_type = u32_ty_id, |
| 5571 | .id_result = limb0, | 5979 | .id_result = limb0, |
| ... | @@ -5580,14 +5988,14 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5580,14 +5988,14 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5580 | converted; | 5988 | converted; |
| 5581 | return try result.materialize(cg); | 5989 | return try result.materialize(cg); |
| 5582 | } else { | 5990 | } else { |
| 5583 | const limb0 = cg.module.allocId(); | 5991 | const limb0 = cg.allocId(); |
| 5584 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 5992 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 5585 | .id_result_type = u32_ty_id, | 5993 | .id_result_type = u32_ty_id, |
| 5586 | .id_result = limb0, | 5994 | .id_result = limb0, |
| 5587 | .composite = src_id, | 5995 | .composite = src_id, |
| 5588 | .indexes = &.{@as(u32, 0)}, | 5996 | .indexes = &.{@as(u32, 0)}, |
| 5589 | }); | 5997 | }); |
| 5590 | const limb1 = cg.module.allocId(); | 5998 | const limb1 = cg.allocId(); |
| 5591 | try cg.body.emit(gpa, .OpCompositeExtract, .{ | 5999 | try cg.body.emit(gpa, .OpCompositeExtract, .{ |
| 5592 | .id_result_type = u32_ty_id, | 6000 | .id_result_type = u32_ty_id, |
| 5593 | .id_result = limb1, | 6001 | .id_result = limb1, |
| ... | @@ -5595,29 +6003,33 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5595,29 +6003,33 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5595 | .indexes = &.{@as(u32, 1)}, | 6003 | .indexes = &.{@as(u32, 1)}, |
| 5596 | }); | 6004 | }); |
| 5597 | const u64_ty_id = try cg.resolveType(.u64, .direct); | 6005 | const u64_ty_id = try cg.resolveType(.u64, .direct); |
| 5598 | const lo = cg.module.allocId(); | 6006 | const lo = cg.allocId(); |
| 5599 | try cg.body.emitRaw(gpa, .OpUConvert, 3); | 6007 | try cg.body.emit(gpa, .OpUConvert, .{ |
| 5600 | cg.body.writeOperand(Id, u64_ty_id); | 6008 | .id_result_type = u64_ty_id, |
| 5601 | cg.body.writeOperand(Id, lo); | 6009 | .id_result = lo, |
| 5602 | cg.body.writeOperand(Id, limb0); | 6010 | .unsigned_value = limb0, |
| 5603 | const hi = cg.module.allocId(); | 6011 | }); |
| 5604 | try cg.body.emitRaw(gpa, .OpUConvert, 3); | 6012 | const hi = cg.allocId(); |
| 5605 | cg.body.writeOperand(Id, u64_ty_id); | 6013 | try cg.body.emit(gpa, .OpUConvert, .{ |
| 5606 | cg.body.writeOperand(Id, hi); | 6014 | .id_result_type = u64_ty_id, |
| 5607 | cg.body.writeOperand(Id, limb1); | 6015 | .id_result = hi, |
| | 6016 | .unsigned_value = limb1, |
| | 6017 | }); |
| 5608 | const shift32 = try cg.constInt(.u64, @as(u64, 32)); | 6018 | const shift32 = try cg.constInt(.u64, @as(u64, 32)); |
| 5609 | const hi_shifted = cg.module.allocId(); | 6019 | const hi_shifted = cg.allocId(); |
| 5610 | try cg.body.emitRaw(gpa, .OpShiftLeftLogical, 4); | 6020 | try cg.body.emit(gpa, .OpShiftLeftLogical, .{ |
| 5611 | cg.body.writeOperand(Id, u64_ty_id); | 6021 | .id_result_type = u64_ty_id, |
| 5612 | cg.body.writeOperand(Id, hi_shifted); | 6022 | .id_result = hi_shifted, |
| 5613 | cg.body.writeOperand(Id, hi); | 6023 | .base = hi, |
| 5614 | cg.body.writeOperand(Id, shift32); | 6024 | .shift = shift32, |
| 5615 | const combined = cg.module.allocId(); | 6025 | }); |
| 5616 | try cg.body.emitRaw(gpa, .OpBitwiseOr, 4); | 6026 | const combined = cg.allocId(); |
| 5617 | cg.body.writeOperand(Id, u64_ty_id); | 6027 | try cg.body.emit(gpa, .OpBitwiseOr, .{ |
| 5618 | cg.body.writeOperand(Id, combined); | 6028 | .id_result_type = u64_ty_id, |
| 5619 | cg.body.writeOperand(Id, lo); | 6029 | .id_result = combined, |
| 5620 | cg.body.writeOperand(Id, hi_shifted); | 6030 | .operand_1 = lo, |
| | 6031 | .operand_2 = hi_shifted, |
| | 6032 | }); |
| 5621 | const tmp: Temporary = .init(.u64, combined); | 6033 | const tmp: Temporary = .init(.u64, combined); |
| 5622 | const converted = try cg.buildConvert(dst_ty, tmp); | 6034 | const converted = try cg.buildConvert(dst_ty, tmp); |
| 5623 | const result = if (dst_info.bits < src_info.bits) | 6035 | const result = if (dst_info.bits < src_info.bits) |
| ... | @@ -5627,7 +6039,7 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5627,7 +6039,7 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5627 | return try result.materialize(cg); | 6039 | return try result.materialize(cg); |
| 5628 | } | 6040 | } |
| 5629 | } else { | 6041 | } else { |
| 5630 | const dst_n: u16 = dst_info.backing_bits / Module.big_int_bits; | 6042 | const dst_n: u16 = dst_info.backing_bits / big_int_bits; |
| 5631 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, dst_n); | 6043 | const result_limbs = try cg.id_scratch.addManyAsSlice(gpa, dst_n); |
| 5632 | const u32_ty_id = try cg.resolveType(.u32, .direct); | 6044 | const u32_ty_id = try cg.resolveType(.u32, .direct); |
| 5633 | | 6045 | |
| ... | @@ -5637,44 +6049,48 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5637,44 +6049,48 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5637 | } else { | 6049 | } else { |
| 5638 | const src_as_u64 = try cg.buildConvert(.u64, src); | 6050 | const src_as_u64 = try cg.buildConvert(.u64, src); |
| 5639 | const src_id = try src_as_u64.materialize(cg); | 6051 | const src_id = try src_as_u64.materialize(cg); |
| 5640 | result_limbs[0] = cg.module.allocId(); | 6052 | result_limbs[0] = cg.allocId(); |
| 5641 | try cg.body.emitRaw(gpa, .OpUConvert, 3); | 6053 | try cg.body.emit(gpa, .OpUConvert, .{ |
| 5642 | cg.body.writeOperand(Id, u32_ty_id); | 6054 | .id_result_type = u32_ty_id, |
| 5643 | cg.body.writeOperand(Id, result_limbs[0]); | 6055 | .id_result = result_limbs[0], |
| 5644 | cg.body.writeOperand(Id, src_id); | 6056 | .unsigned_value = src_id, |
| | 6057 | }); |
| 5645 | const u64_ty_id = try cg.resolveType(.u64, .direct); | 6058 | const u64_ty_id = try cg.resolveType(.u64, .direct); |
| 5646 | const shift32 = try cg.constInt(.u64, @as(u64, 32)); | 6059 | const shift32 = try cg.constInt(.u64, @as(u64, 32)); |
| 5647 | const hi = cg.module.allocId(); | 6060 | const hi = cg.allocId(); |
| 5648 | try cg.body.emitRaw(gpa, .OpShiftRightLogical, 4); | 6061 | try cg.body.emit(gpa, .OpShiftRightLogical, .{ |
| 5649 | cg.body.writeOperand(Id, u64_ty_id); | 6062 | .id_result_type = u64_ty_id, |
| 5650 | cg.body.writeOperand(Id, hi); | 6063 | .id_result = hi, |
| 5651 | cg.body.writeOperand(Id, src_id); | 6064 | .base = src_id, |
| 5652 | cg.body.writeOperand(Id, shift32); | 6065 | .shift = shift32, |
| 5653 | result_limbs[1] = cg.module.allocId(); | 6066 | }); |
| 5654 | try cg.body.emitRaw(gpa, .OpUConvert, 3); | 6067 | result_limbs[1] = cg.allocId(); |
| 5655 | cg.body.writeOperand(Id, u32_ty_id); | 6068 | try cg.body.emit(gpa, .OpUConvert, .{ |
| 5656 | cg.body.writeOperand(Id, result_limbs[1]); | 6069 | .id_result_type = u32_ty_id, |
| 5657 | cg.body.writeOperand(Id, hi); | 6070 | .id_result = result_limbs[1], |
| | 6071 | .unsigned_value = hi, |
| | 6072 | }); |
| 5658 | } | 6073 | } |
| 5659 | // Sign/zero-extend remaining limbs. | 6074 | // Sign/zero-extend remaining limbs. |
| 5660 | const fill_start: u16 = if (src_info.backing_bits <= 32) 1 else 2; | 6075 | const fill_start: u16 = if (src_info.backing_bits <= 32) 1 else 2; |
| 5661 | const fill = if (src_info.signedness == .signed) blk: { | 6076 | const fill = if (src_info.signedness == .signed) blk: { |
| 5662 | const i32_ty_id = try cg.resolveType(.i32, .direct); | 6077 | const i32_ty_id = try cg.resolveType(.i32, .direct); |
| 5663 | const msb = result_limbs[fill_start - 1]; | 6078 | const msb = result_limbs[fill_start - 1]; |
| 5664 | const msb_signed = cg.module.allocId(); | 6079 | const msb_signed = cg.allocId(); |
| 5665 | try cg.body.emit(gpa, .OpBitcast, .{ | 6080 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5666 | .id_result_type = i32_ty_id, | 6081 | .id_result_type = i32_ty_id, |
| 5667 | .id_result = msb_signed, | 6082 | .id_result = msb_signed, |
| 5668 | .operand = msb, | 6083 | .operand = msb, |
| 5669 | }); | 6084 | }); |
| 5670 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); | 6085 | const shift31 = try cg.constInt(.i32, @as(i32, 31)); |
| 5671 | const sign_ext = cg.module.allocId(); | 6086 | const sign_ext = cg.allocId(); |
| 5672 | try cg.body.emitRaw(gpa, .OpShiftRightArithmetic, 4); | 6087 | try cg.body.emit(gpa, .OpShiftRightArithmetic, .{ |
| 5673 | cg.body.writeOperand(Id, i32_ty_id); | 6088 | .id_result_type = i32_ty_id, |
| 5674 | cg.body.writeOperand(Id, sign_ext); | 6089 | .id_result = sign_ext, |
| 5675 | cg.body.writeOperand(Id, msb_signed); | 6090 | .base = msb_signed, |
| 5676 | cg.body.writeOperand(Id, shift31); | 6091 | .shift = shift31, |
| 5677 | const back = cg.module.allocId(); | 6092 | }); |
| | 6093 | const back = cg.allocId(); |
| 5678 | try cg.body.emit(gpa, .OpBitcast, .{ | 6094 | try cg.body.emit(gpa, .OpBitcast, .{ |
| 5679 | .id_result_type = u32_ty_id, | 6095 | .id_result_type = u32_ty_id, |
| 5680 | .id_result = back, | 6096 | .id_result = back, |
| ... | @@ -5715,8 +6131,8 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5715,8 +6131,8 @@ fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5715 | | 6131 | |
| 5716 | fn intFromPtr(cg: *CodeGen, operand_id: Id) !Id { | 6132 | fn intFromPtr(cg: *CodeGen, operand_id: Id) !Id { |
| 5717 | const result_type_id = try cg.resolveType(.usize, .direct); | 6133 | const result_type_id = try cg.resolveType(.usize, .direct); |
| 5718 | const result_id = cg.module.allocId(); | 6134 | const result_id = cg.allocId(); |
| 5719 | try cg.body.emit(cg.module.gpa, .OpConvertPtrToU, .{ | 6135 | try cg.body.emit(cg.gpa, .OpConvertPtrToU, .{ |
| 5720 | .id_result_type = result_type_id, | 6136 | .id_result_type = result_type_id, |
| 5721 | .id_result = result_id, | 6137 | .id_result = result_id, |
| 5722 | .pointer = operand_id, | 6138 | .pointer = operand_id, |
| ... | @@ -5725,17 +6141,13 @@ fn intFromPtr(cg: *CodeGen, operand_id: Id) !Id { | ... | @@ -5725,17 +6141,13 @@ fn intFromPtr(cg: *CodeGen, operand_id: Id) !Id { |
| 5725 | } | 6141 | } |
| 5726 | | 6142 | |
| 5727 | fn airFloatFromInt(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6143 | fn airFloatFromInt(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| | 6144 | const gpa = cg.gpa; |
| 5728 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6145 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5729 | const operand_ty = cg.typeOf(ty_op.operand); | 6146 | const operand_ty = cg.typeOf(ty_op.operand); |
| 5730 | const operand_id = try cg.resolve(ty_op.operand); | 6147 | const operand_id = try cg.resolve(ty_op.operand); |
| 5731 | const result_ty = cg.typeOfIndex(inst); | 6148 | const result_ty = cg.typeOfIndex(inst); |
| 5732 | return try cg.floatFromInt(result_ty, operand_ty, operand_id); | | |
| 5733 | } | | |
| 5734 | | | |
| 5735 | fn floatFromInt(cg: *CodeGen, result_ty: Type, operand_ty: Type, operand_id: Id) !Id { | | |
| 5736 | const gpa = cg.module.gpa; | | |
| 5737 | const operand_info = cg.arithmeticTypeInfo(operand_ty); | 6149 | const operand_info = cg.arithmeticTypeInfo(operand_ty); |
| 5738 | const result_id = cg.module.allocId(); | 6150 | const result_id = cg.allocId(); |
| 5739 | const result_ty_id = try cg.resolveType(result_ty, .direct); | 6151 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 5740 | switch (operand_info.signedness) { | 6152 | switch (operand_info.signedness) { |
| 5741 | .signed => try cg.body.emit(gpa, .OpConvertSToF, .{ | 6153 | .signed => try cg.body.emit(gpa, .OpConvertSToF, .{ |
| ... | @@ -5753,17 +6165,13 @@ fn floatFromInt(cg: *CodeGen, result_ty: Type, operand_ty: Type, operand_id: Id) | ... | @@ -5753,17 +6165,13 @@ fn floatFromInt(cg: *CodeGen, result_ty: Type, operand_ty: Type, operand_id: Id) |
| 5753 | } | 6165 | } |
| 5754 | | 6166 | |
| 5755 | fn airIntFromFloat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6167 | fn airIntFromFloat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| | 6168 | const gpa = cg.gpa; |
| 5756 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6169 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5757 | const operand_id = try cg.resolve(ty_op.operand); | 6170 | const operand_id = try cg.resolve(ty_op.operand); |
| 5758 | const result_ty = cg.typeOfIndex(inst); | 6171 | const result_ty = cg.typeOfIndex(inst); |
| 5759 | return try cg.intFromFloat(result_ty, operand_id); | | |
| 5760 | } | | |
| 5761 | | | |
| 5762 | fn intFromFloat(cg: *CodeGen, result_ty: Type, operand_id: Id) !Id { | | |
| 5763 | const gpa = cg.module.gpa; | | |
| 5764 | const result_info = cg.arithmeticTypeInfo(result_ty); | 6172 | const result_info = cg.arithmeticTypeInfo(result_ty); |
| 5765 | const result_ty_id = try cg.resolveType(result_ty, .direct); | 6173 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 5766 | const result_id = cg.module.allocId(); | 6174 | const result_id = cg.allocId(); |
| 5767 | switch (result_info.signedness) { | 6175 | switch (result_info.signedness) { |
| 5768 | .signed => try cg.body.emit(gpa, .OpConvertFToS, .{ | 6176 | .signed => try cg.body.emit(gpa, .OpConvertFToS, .{ |
| 5769 | .id_result_type = result_ty_id, | 6177 | .id_result_type = result_ty_id, |
| ... | @@ -5815,7 +6223,7 @@ fn airNot(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5815,7 +6223,7 @@ fn airNot(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5815 | } | 6223 | } |
| 5816 | | 6224 | |
| 5817 | fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6225 | fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5818 | const zcu = cg.module.zcu; | 6226 | const zcu = cg.zcu; |
| 5819 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6227 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5820 | const array_ptr_ty = cg.typeOf(ty_op.operand); | 6228 | const array_ptr_ty = cg.typeOf(ty_op.operand); |
| 5821 | const array_ty = array_ptr_ty.childType(zcu); | 6229 | const array_ty = array_ptr_ty.childType(zcu); |
| ... | @@ -5849,11 +6257,11 @@ fn airSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5849,11 +6257,11 @@ fn airSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5849 | } | 6257 | } |
| 5850 | | 6258 | |
| 5851 | fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6259 | fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5852 | const gpa = cg.module.gpa; | 6260 | const gpa = cg.gpa; |
| 5853 | const pt = cg.pt; | 6261 | const pt = cg.pt; |
| 5854 | const zcu = cg.module.zcu; | 6262 | const zcu = cg.zcu; |
| 5855 | const ip = &zcu.intern_pool; | 6263 | const ip = &zcu.intern_pool; |
| 5856 | const target = cg.module.zcu.getTarget(); | 6264 | const target = cg.zcu.getTarget(); |
| 5857 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6265 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5858 | const result_ty = cg.typeOfIndex(inst); | 6266 | const result_ty = cg.typeOfIndex(inst); |
| 5859 | const len: usize = @intCast(result_ty.arrayLen(zcu)); | 6267 | const len: usize = @intCast(result_ty.arrayLen(zcu)); |
| ... | @@ -5978,23 +6386,8 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -5978,23 +6386,8 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 5978 | } | 6386 | } |
| 5979 | } | 6387 | } |
| 5980 | | 6388 | |
| 5981 | fn sliceOrArrayLen(cg: *CodeGen, operand_id: Id, ty: Type) !Id { | | |
| 5982 | const zcu = cg.module.zcu; | | |
| 5983 | switch (ty.ptrSize(zcu)) { | | |
| 5984 | .slice => return cg.extractField(.usize, operand_id, 1), | | |
| 5985 | .one => { | | |
| 5986 | const array_ty = ty.childType(zcu); | | |
| 5987 | const elem_ty = array_ty.childType(zcu); | | |
| 5988 | const abi_size = elem_ty.abiSize(zcu); | | |
| 5989 | const size = array_ty.arrayLenIncludingSentinel(zcu) * abi_size; | | |
| 5990 | return try cg.constInt(.usize, size); | | |
| 5991 | }, | | |
| 5992 | .many, .c => unreachable, | | |
| 5993 | } | | |
| 5994 | } | | |
| 5995 | | | |
| 5996 | fn sliceOrArrayPtr(cg: *CodeGen, operand_id: Id, ty: Type) !Id { | 6389 | fn sliceOrArrayPtr(cg: *CodeGen, operand_id: Id, ty: Type) !Id { |
| 5997 | const zcu = cg.module.zcu; | 6390 | const zcu = cg.zcu; |
| 5998 | if (ty.isSlice(zcu)) { | 6391 | if (ty.isSlice(zcu)) { |
| 5999 | const ptr_ty = ty.slicePtrFieldType(zcu); | 6392 | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 6000 | return cg.extractField(ptr_ty, operand_id, 0); | 6393 | return cg.extractField(ptr_ty, operand_id, 0); |
| ... | @@ -6010,8 +6403,17 @@ fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -6010,8 +6403,17 @@ fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6010 | const src_ty = cg.typeOf(bin_op.rhs); | 6403 | const src_ty = cg.typeOf(bin_op.rhs); |
| 6011 | const dest_ptr = try cg.sliceOrArrayPtr(dest_slice, dest_ty); | 6404 | const dest_ptr = try cg.sliceOrArrayPtr(dest_slice, dest_ty); |
| 6012 | const src_ptr = try cg.sliceOrArrayPtr(src_slice, src_ty); | 6405 | const src_ptr = try cg.sliceOrArrayPtr(src_slice, src_ty); |
| 6013 | const len = try cg.sliceOrArrayLen(dest_slice, dest_ty); | 6406 | const len = switch (dest_ty.ptrSize(cg.zcu)) { |
| 6014 | try cg.body.emit(cg.module.gpa, .OpCopyMemorySized, .{ | 6407 | .slice => try cg.extractField(.usize, dest_slice, 1), |
| | 6408 | .one => len: { |
| | 6409 | const array_ty = dest_ty.childType(cg.zcu); |
| | 6410 | const elem_ty = array_ty.childType(cg.zcu); |
| | 6411 | const size = array_ty.arrayLenIncludingSentinel(cg.zcu) * elem_ty.abiSize(cg.zcu); |
| | 6412 | break :len try cg.constInt(.usize, size); |
| | 6413 | }, |
| | 6414 | .many, .c => unreachable, |
| | 6415 | }; |
| | 6416 | try cg.body.emit(cg.gpa, .OpCopyMemorySized, .{ |
| 6015 | .target = dest_ptr, | 6417 | .target = dest_ptr, |
| 6016 | .source = src_ptr, | 6418 | .source = src_ptr, |
| 6017 | .size = len, | 6419 | .size = len, |
| ... | @@ -6031,12 +6433,12 @@ fn airSliceField(cg: *CodeGen, inst: Air.Inst.Index, field: u32) !?Id { | ... | @@ -6031,12 +6433,12 @@ fn airSliceField(cg: *CodeGen, inst: Air.Inst.Index, field: u32) !?Id { |
| 6031 | } | 6433 | } |
| 6032 | | 6434 | |
| 6033 | fn airSpirvRuntimeArrayLen(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6435 | fn airSpirvRuntimeArrayLen(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6034 | const gpa = cg.module.gpa; | 6436 | const gpa = cg.gpa; |
| 6035 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6437 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6036 | const extra = cg.air.extraData(Air.StructField, ty_pl.payload).data; | 6438 | const extra = cg.air.extraData(Air.StructField, ty_pl.payload).data; |
| 6037 | const struct_ptr_id = try cg.resolve(extra.struct_operand); | 6439 | const struct_ptr_id = try cg.resolve(extra.struct_operand); |
| 6038 | const u32_ty_id = try cg.module.intType(.unsigned, 32); | 6440 | const u32_ty_id = try cg.intType(.unsigned, 32); |
| 6039 | const result_id = cg.module.allocId(); | 6441 | const result_id = cg.allocId(); |
| 6040 | try cg.body.emit(gpa, .OpArrayLength, .{ | 6442 | try cg.body.emit(gpa, .OpArrayLength, .{ |
| 6041 | .id_result_type = u32_ty_id, | 6443 | .id_result_type = u32_ty_id, |
| 6042 | .id_result = result_id, | 6444 | .id_result = result_id, |
| ... | @@ -6047,7 +6449,7 @@ fn airSpirvRuntimeArrayLen(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6047,7 +6449,7 @@ fn airSpirvRuntimeArrayLen(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6047 | } | 6449 | } |
| 6048 | | 6450 | |
| 6049 | fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6451 | fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6050 | const zcu = cg.module.zcu; | 6452 | const zcu = cg.zcu; |
| 6051 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6453 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6052 | const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; | 6454 | const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; |
| 6053 | const slice_ty = cg.typeOf(bin_op.lhs); | 6455 | const slice_ty = cg.typeOf(bin_op.lhs); |
| ... | @@ -6064,7 +6466,7 @@ fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6064,7 +6466,7 @@ fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6064 | } | 6466 | } |
| 6065 | | 6467 | |
| 6066 | fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6468 | fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6067 | const zcu = cg.module.zcu; | 6469 | const zcu = cg.zcu; |
| 6068 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6470 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6069 | const slice_ty = cg.typeOf(bin_op.lhs); | 6471 | const slice_ty = cg.typeOf(bin_op.lhs); |
| 6070 | if (!slice_ty.isVolatilePtr(zcu) and cg.liveness.isUnused(inst)) return null; | 6472 | if (!slice_ty.isVolatilePtr(zcu) and cg.liveness.isUnused(inst)) return null; |
| ... | @@ -6081,11 +6483,11 @@ fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6081,11 +6483,11 @@ fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6081 | } | 6483 | } |
| 6082 | | 6484 | |
| 6083 | fn ptrElemPtr(cg: *CodeGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { | 6485 | fn ptrElemPtr(cg: *CodeGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { |
| 6084 | const zcu = cg.module.zcu; | 6486 | const zcu = cg.zcu; |
| 6085 | // Construct new pointer type for the resulting pointer | 6487 | // Construct new pointer type for the resulting pointer |
| 6086 | const elem_ty = ptr_ty.indexableElem(zcu); | 6488 | const elem_ty = ptr_ty.indexableElem(zcu); |
| 6087 | const elem_ty_id = try cg.resolveType(elem_ty, .indirect); | 6489 | const elem_ty_id = try cg.resolveType(elem_ty, .indirect); |
| 6088 | const elem_ptr_ty_id = try cg.module.ptrType(elem_ty_id, cg.module.storageClass(ptr_ty.ptrAddressSpace(zcu))); | 6490 | const elem_ptr_ty_id = try cg.ptrType(elem_ty_id, cg.storageClass(ptr_ty.ptrAddressSpace(zcu))); |
| 6089 | if (ptr_ty.isSinglePointer(zcu)) { | 6491 | if (ptr_ty.isSinglePointer(zcu)) { |
| 6090 | // Pointer-to-array. In this case, the resulting pointer is not of the same type | 6492 | // Pointer-to-array. In this case, the resulting pointer is not of the same type |
| 6091 | // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain. | 6493 | // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain. |
| ... | @@ -6097,7 +6499,7 @@ fn ptrElemPtr(cg: *CodeGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { | ... | @@ -6097,7 +6499,7 @@ fn ptrElemPtr(cg: *CodeGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { |
| 6097 | } | 6499 | } |
| 6098 | | 6500 | |
| 6099 | fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6501 | fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6100 | const zcu = cg.module.zcu; | 6502 | const zcu = cg.zcu; |
| 6101 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6503 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6102 | const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; | 6504 | const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; |
| 6103 | const src_ptr_ty = cg.typeOf(bin_op.lhs); | 6505 | const src_ptr_ty = cg.typeOf(bin_op.lhs); |
| ... | @@ -6111,8 +6513,8 @@ fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6111,8 +6513,8 @@ fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6111 | } | 6513 | } |
| 6112 | | 6514 | |
| 6113 | fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6515 | fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6114 | const gpa = cg.module.gpa; | 6516 | const gpa = cg.gpa; |
| 6115 | const zcu = cg.module.zcu; | 6517 | const zcu = cg.zcu; |
| 6116 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6518 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6117 | const array_ty = cg.typeOf(bin_op.lhs); | 6519 | const array_ty = cg.typeOf(bin_op.lhs); |
| 6118 | const elem_ty = array_ty.childType(zcu); | 6520 | const elem_ty = array_ty.childType(zcu); |
| ... | @@ -6127,10 +6529,10 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6127,10 +6529,10 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6127 | const elem_repr: Repr = if (is_vector) .direct else .indirect; | 6529 | const elem_repr: Repr = if (is_vector) .direct else .indirect; |
| 6128 | const array_ty_id = try cg.resolveType(array_ty, .direct); | 6530 | const array_ty_id = try cg.resolveType(array_ty, .direct); |
| 6129 | const elem_ty_id = try cg.resolveType(elem_ty, elem_repr); | 6531 | const elem_ty_id = try cg.resolveType(elem_ty, elem_repr); |
| 6130 | const ptr_array_ty_id = try cg.module.ptrType(array_ty_id, .function); | 6532 | const ptr_array_ty_id = try cg.ptrType(array_ty_id, .function); |
| 6131 | const ptr_elem_ty_id = try cg.module.ptrType(elem_ty_id, .function); | 6533 | const ptr_elem_ty_id = try cg.ptrType(elem_ty_id, .function); |
| 6132 | | 6534 | |
| 6133 | const tmp_id = cg.module.allocId(); | 6535 | const tmp_id = cg.allocId(); |
| 6134 | try cg.prologue.emit(gpa, .OpVariable, .{ | 6536 | try cg.prologue.emit(gpa, .OpVariable, .{ |
| 6135 | .id_result_type = ptr_array_ty_id, | 6537 | .id_result_type = ptr_array_ty_id, |
| 6136 | .id_result = tmp_id, | 6538 | .id_result = tmp_id, |
| ... | @@ -6144,7 +6546,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6144,7 +6546,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6144 | | 6546 | |
| 6145 | const elem_ptr_id = try cg.accessChainId(ptr_elem_ty_id, tmp_id, &.{index_id}); | 6547 | const elem_ptr_id = try cg.accessChainId(ptr_elem_ty_id, tmp_id, &.{index_id}); |
| 6146 | | 6548 | |
| 6147 | const result_id = cg.module.allocId(); | 6549 | const result_id = cg.allocId(); |
| 6148 | try cg.body.emit(gpa, .OpLoad, .{ | 6550 | try cg.body.emit(gpa, .OpLoad, .{ |
| 6149 | .id_result_type = try cg.resolveType(elem_ty, elem_repr), | 6551 | .id_result_type = try cg.resolveType(elem_ty, elem_repr), |
| 6150 | .id_result = result_id, | 6552 | .id_result = result_id, |
| ... | @@ -6163,7 +6565,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6163,7 +6565,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6163 | } | 6565 | } |
| 6164 | | 6566 | |
| 6165 | fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6567 | fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6166 | const zcu = cg.module.zcu; | 6568 | const zcu = cg.zcu; |
| 6167 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6569 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6168 | const ptr_ty = cg.typeOf(bin_op.lhs); | 6570 | const ptr_ty = cg.typeOf(bin_op.lhs); |
| 6169 | const elem_ty = cg.typeOfIndex(inst); | 6571 | const elem_ty = cg.typeOfIndex(inst); |
| ... | @@ -6174,7 +6576,7 @@ fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6174,7 +6576,7 @@ fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6174 | } | 6576 | } |
| 6175 | | 6577 | |
| 6176 | fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !void { | 6578 | fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6177 | const zcu = cg.module.zcu; | 6579 | const zcu = cg.zcu; |
| 6178 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6580 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6179 | const un_ptr_ty = cg.typeOf(bin_op.lhs); | 6581 | const un_ptr_ty = cg.typeOf(bin_op.lhs); |
| 6180 | const un_ty = un_ptr_ty.childType(zcu); | 6582 | const un_ty = un_ptr_ty.childType(zcu); |
| ... | @@ -6184,7 +6586,7 @@ fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -6184,7 +6586,7 @@ fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6184 | | 6586 | |
| 6185 | const tag_ty = un_ty.unionTagTypeRuntime(zcu).?; | 6587 | const tag_ty = un_ty.unionTagTypeRuntime(zcu).?; |
| 6186 | const tag_ty_id = try cg.resolveType(tag_ty, .indirect); | 6588 | const tag_ty_id = try cg.resolveType(tag_ty, .indirect); |
| 6187 | const tag_ptr_ty_id = try cg.module.ptrType(tag_ty_id, cg.module.storageClass(un_ptr_ty.ptrAddressSpace(zcu))); | 6589 | const tag_ptr_ty_id = try cg.ptrType(tag_ty_id, cg.storageClass(un_ptr_ty.ptrAddressSpace(zcu))); |
| 6188 | | 6590 | |
| 6189 | const union_ptr_id = try cg.resolve(bin_op.lhs); | 6591 | const union_ptr_id = try cg.resolve(bin_op.lhs); |
| 6190 | const new_tag_id = try cg.resolve(bin_op.rhs); | 6592 | const new_tag_id = try cg.resolve(bin_op.rhs); |
| ... | @@ -6201,7 +6603,7 @@ fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6201,7 +6603,7 @@ fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6201 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6603 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6202 | const un_ty = cg.typeOf(ty_op.operand); | 6604 | const un_ty = cg.typeOf(ty_op.operand); |
| 6203 | | 6605 | |
| 6204 | const zcu = cg.module.zcu; | 6606 | const zcu = cg.zcu; |
| 6205 | const layout = cg.unionLayout(un_ty); | 6607 | const layout = cg.unionLayout(un_ty); |
| 6206 | if (layout.tag_size == 0) return null; | 6608 | if (layout.tag_size == 0) return null; |
| 6207 | | 6609 | |
| ... | @@ -6225,7 +6627,7 @@ fn unionInit( | ... | @@ -6225,7 +6627,7 @@ fn unionInit( |
| 6225 | // Note: The result here is not cached, because it generates runtime code. | 6627 | // Note: The result here is not cached, because it generates runtime code. |
| 6226 | | 6628 | |
| 6227 | const pt = cg.pt; | 6629 | const pt = cg.pt; |
| 6228 | const zcu = cg.module.zcu; | 6630 | const zcu = cg.zcu; |
| 6229 | const ip = &zcu.intern_pool; | 6631 | const ip = &zcu.intern_pool; |
| 6230 | const union_ty = zcu.typeToUnion(ty).?; | 6632 | const union_ty = zcu.typeToUnion(ty).?; |
| 6231 | const tag_ty: Type = .fromInterned(union_ty.enum_tag_type); | 6633 | const tag_ty: Type = .fromInterned(union_ty.enum_tag_type); |
| ... | @@ -6250,7 +6652,7 @@ fn unionInit( | ... | @@ -6250,7 +6652,7 @@ fn unionInit( |
| 6250 | | 6652 | |
| 6251 | if (layout.tag_size != 0) { | 6653 | if (layout.tag_size != 0) { |
| 6252 | const tag_ty_id = try cg.resolveType(tag_ty, .indirect); | 6654 | const tag_ty_id = try cg.resolveType(tag_ty, .indirect); |
| 6253 | const tag_ptr_ty_id = try cg.module.ptrType(tag_ty_id, .function); | 6655 | const tag_ptr_ty_id = try cg.ptrType(tag_ty_id, .function); |
| 6254 | const ptr_id = try cg.accessChain(tag_ptr_ty_id, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); | 6656 | const ptr_id = try cg.accessChain(tag_ptr_ty_id, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); |
| 6255 | const tag_id = try cg.constInt(tag_ty, tag_int); | 6657 | const tag_id = try cg.constInt(tag_ty, tag_int); |
| 6256 | try cg.store(tag_ty, ptr_id, tag_id, .{}); | 6658 | try cg.store(tag_ty, ptr_id, tag_id, .{}); |
| ... | @@ -6258,13 +6660,13 @@ fn unionInit( | ... | @@ -6258,13 +6660,13 @@ fn unionInit( |
| 6258 | | 6660 | |
| 6259 | if (payload_ty.hasRuntimeBits(zcu)) { | 6661 | if (payload_ty.hasRuntimeBits(zcu)) { |
| 6260 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); | 6662 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); |
| 6261 | const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, .function); | 6663 | const pl_ptr_ty_id = try cg.ptrType(layout_payload_ty_id, .function); |
| 6262 | const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); | 6664 | const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); |
| 6263 | const active_pl_ptr_id = if (!layout.payload_ty.eql(payload_ty)) blk: { | 6665 | const active_pl_ptr_id = if (!layout.payload_ty.eql(payload_ty)) blk: { |
| 6264 | const payload_ty_id = try cg.resolveType(payload_ty, .indirect); | 6666 | const payload_ty_id = try cg.resolveType(payload_ty, .indirect); |
| 6265 | const active_pl_ptr_ty_id = try cg.module.ptrType(payload_ty_id, .function); | 6667 | const active_pl_ptr_ty_id = try cg.ptrType(payload_ty_id, .function); |
| 6266 | const active_pl_ptr_id = cg.module.allocId(); | 6668 | const active_pl_ptr_id = cg.allocId(); |
| 6267 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | 6669 | try cg.body.emit(cg.gpa, .OpBitcast, .{ |
| 6268 | .id_result_type = active_pl_ptr_ty_id, | 6670 | .id_result_type = active_pl_ptr_ty_id, |
| 6269 | .id_result = active_pl_ptr_id, | 6671 | .id_result = active_pl_ptr_id, |
| 6270 | .operand = pl_ptr_id, | 6672 | .operand = pl_ptr_id, |
| ... | @@ -6284,7 +6686,7 @@ fn unionInit( | ... | @@ -6284,7 +6686,7 @@ fn unionInit( |
| 6284 | } | 6686 | } |
| 6285 | | 6687 | |
| 6286 | fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6688 | fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6287 | const zcu = cg.module.zcu; | 6689 | const zcu = cg.zcu; |
| 6288 | const ip = &zcu.intern_pool; | 6690 | const ip = &zcu.intern_pool; |
| 6289 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6691 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6290 | const extra = cg.air.extraData(Air.UnionInit, ty_pl.payload).data; | 6692 | const extra = cg.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| ... | @@ -6301,7 +6703,7 @@ fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6301,7 +6703,7 @@ fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6301 | | 6703 | |
| 6302 | fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6704 | fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6303 | const pt = cg.pt; | 6705 | const pt = cg.pt; |
| 6304 | const zcu = cg.module.zcu; | 6706 | const zcu = cg.zcu; |
| 6305 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6707 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6306 | const struct_field = cg.air.extraData(Air.StructField, ty_pl.payload).data; | 6708 | const struct_field = cg.air.extraData(Air.StructField, ty_pl.payload).data; |
| 6307 | | 6709 | |
| ... | @@ -6316,7 +6718,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6316,7 +6718,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6316 | .@"struct" => switch (object_ty.containerLayout(zcu)) { | 6718 | .@"struct" => switch (object_ty.containerLayout(zcu)) { |
| 6317 | .@"packed" => { | 6719 | .@"packed" => { |
| 6318 | const struct_ty = zcu.typeToPackedStruct(object_ty).?; | 6720 | const struct_ty = zcu.typeToPackedStruct(object_ty).?; |
| 6319 | const struct_backing_int_bits = cg.module.backingIntBits(@intCast(object_ty.bitSize(zcu))).@"0"; | 6721 | const struct_backing_int_bits = cg.backingIntBits(@intCast(object_ty.bitSize(zcu))).@"0"; |
| 6320 | const bit_offset = zcu.structPackedFieldBitOffset(struct_ty, field_index); | 6722 | const bit_offset = zcu.structPackedFieldBitOffset(struct_ty, field_index); |
| 6321 | // We use the same int type the packed struct is backed by, because even though it would | 6723 | // We use the same int type the packed struct is backed by, because even though it would |
| 6322 | // be valid SPIR-V to use an smaller type like u16, some implementations like PoCL will complain. | 6724 | // be valid SPIR-V to use an smaller type like u16, some implementations like PoCL will complain. |
| ... | @@ -6329,7 +6731,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6329,7 +6731,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6329 | const mask_id = try cg.constInt(object_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); | 6731 | const mask_id = try cg.constInt(object_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); |
| 6330 | const masked = try cg.buildBinary(.OpBitwiseAnd, shift, .{ .ty = object_ty, .value = .{ .singleton = mask_id } }); | 6732 | const masked = try cg.buildBinary(.OpBitwiseAnd, shift, .{ .ty = object_ty, .value = .{ .singleton = mask_id } }); |
| 6331 | const result_id = blk: { | 6733 | const result_id = blk: { |
| 6332 | if (cg.module.backingIntBits(field_bit_size).@"0" == struct_backing_int_bits) | 6734 | if (cg.backingIntBits(field_bit_size).@"0" == struct_backing_int_bits) |
| 6333 | break :blk try cg.bitCast(field_int_ty, object_ty, try masked.materialize(cg)); | 6735 | break :blk try cg.bitCast(field_int_ty, object_ty, try masked.materialize(cg)); |
| 6334 | const trunc = try cg.buildConvert(field_int_ty, masked); | 6736 | const trunc = try cg.buildConvert(field_int_ty, masked); |
| 6335 | break :blk try trunc.materialize(cg); | 6737 | break :blk try trunc.materialize(cg); |
| ... | @@ -6353,7 +6755,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6353,7 +6755,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6353 | .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } }, | 6755 | .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } }, |
| 6354 | ); | 6756 | ); |
| 6355 | const result_id = blk: { | 6757 | const result_id = blk: { |
| 6356 | if (cg.module.backingIntBits(field_bit_size).@"0" == cg.module.backingIntBits(@intCast(backing_int_ty.bitSize(zcu))).@"0") | 6758 | if (cg.backingIntBits(field_bit_size).@"0" == cg.backingIntBits(@intCast(backing_int_ty.bitSize(zcu))).@"0") |
| 6357 | break :blk try cg.bitCast(int_ty, backing_int_ty, try masked.materialize(cg)); | 6759 | break :blk try cg.bitCast(int_ty, backing_int_ty, try masked.materialize(cg)); |
| 6358 | const trunc = try cg.buildConvert(int_ty, masked); | 6760 | const trunc = try cg.buildConvert(int_ty, masked); |
| 6359 | break :blk try trunc.materialize(cg); | 6761 | break :blk try trunc.materialize(cg); |
| ... | @@ -6372,13 +6774,13 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6372,13 +6774,13 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6372 | try cg.store(object_ty, tmp_id, object_id, .{}); | 6774 | try cg.store(object_ty, tmp_id, object_id, .{}); |
| 6373 | | 6775 | |
| 6374 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); | 6776 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); |
| 6375 | const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, .function); | 6777 | const pl_ptr_ty_id = try cg.ptrType(layout_payload_ty_id, .function); |
| 6376 | const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); | 6778 | const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); |
| 6377 | | 6779 | |
| 6378 | const field_ty_id = try cg.resolveType(field_ty, .indirect); | 6780 | const field_ty_id = try cg.resolveType(field_ty, .indirect); |
| 6379 | const active_pl_ptr_ty_id = try cg.module.ptrType(field_ty_id, .function); | 6781 | const active_pl_ptr_ty_id = try cg.ptrType(field_ty_id, .function); |
| 6380 | const active_pl_ptr_id = cg.module.allocId(); | 6782 | const active_pl_ptr_id = cg.allocId(); |
| 6381 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | 6783 | try cg.body.emit(cg.gpa, .OpBitcast, .{ |
| 6382 | .id_result_type = active_pl_ptr_ty_id, | 6784 | .id_result_type = active_pl_ptr_ty_id, |
| 6383 | .id_result = active_pl_ptr_id, | 6785 | .id_result = active_pl_ptr_id, |
| 6384 | .operand = pl_ptr_id, | 6786 | .operand = pl_ptr_id, |
| ... | @@ -6391,7 +6793,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6391,7 +6793,7 @@ fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6391 | } | 6793 | } |
| 6392 | | 6794 | |
| 6393 | fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6795 | fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6394 | const zcu = cg.module.zcu; | 6796 | const zcu = cg.zcu; |
| 6395 | const target = zcu.getTarget(); | 6797 | const target = zcu.getTarget(); |
| 6396 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6798 | const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6397 | const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; | 6799 | const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| ... | @@ -6424,8 +6826,8 @@ fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6424,8 +6826,8 @@ fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6424 | } | 6826 | } |
| 6425 | } | 6827 | } |
| 6426 | | 6828 | |
| 6427 | const base_ptr = cg.module.allocId(); | 6829 | const base_ptr = cg.allocId(); |
| 6428 | try cg.body.emit(cg.module.gpa, .OpConvertUToPtr, .{ | 6830 | try cg.body.emit(cg.gpa, .OpConvertUToPtr, .{ |
| 6429 | .id_result_type = result_ty_id, | 6831 | .id_result_type = result_ty_id, |
| 6430 | .id_result = base_ptr, | 6832 | .id_result = base_ptr, |
| 6431 | .integer_value = base_ptr_int, | 6833 | .integer_value = base_ptr_int, |
| ... | @@ -6443,7 +6845,7 @@ fn structFieldPtr( | ... | @@ -6443,7 +6845,7 @@ fn structFieldPtr( |
| 6443 | ) !Id { | 6845 | ) !Id { |
| 6444 | const result_ty_id = try cg.resolveType(result_ptr_ty, .direct); | 6846 | const result_ty_id = try cg.resolveType(result_ptr_ty, .direct); |
| 6445 | | 6847 | |
| 6446 | const zcu = cg.module.zcu; | 6848 | const zcu = cg.zcu; |
| 6447 | const object_ty = object_ptr_ty.childType(zcu); | 6849 | const object_ty = object_ptr_ty.childType(zcu); |
| 6448 | switch (object_ty.zigTypeTag(zcu)) { | 6850 | switch (object_ty.zigTypeTag(zcu)) { |
| 6449 | .pointer => { | 6851 | .pointer => { |
| ... | @@ -6455,8 +6857,8 @@ fn structFieldPtr( | ... | @@ -6455,8 +6857,8 @@ fn structFieldPtr( |
| 6455 | const byte_offset = codegen.fieldOffset(object_ptr_ty, result_ptr_ty, field_index, zcu); | 6857 | const byte_offset = codegen.fieldOffset(object_ptr_ty, result_ptr_ty, field_index, zcu); |
| 6456 | if (byte_offset == 0) return object_ptr; | 6858 | if (byte_offset == 0) return object_ptr; |
| 6457 | const usize_ty_id = try cg.resolveType(.usize, .direct); | 6859 | const usize_ty_id = try cg.resolveType(.usize, .direct); |
| 6458 | const base_int = cg.module.allocId(); | 6860 | const base_int = cg.allocId(); |
| 6459 | try cg.body.emit(cg.module.gpa, .OpConvertPtrToU, .{ | 6861 | try cg.body.emit(cg.gpa, .OpConvertPtrToU, .{ |
| 6460 | .id_result_type = usize_ty_id, | 6862 | .id_result_type = usize_ty_id, |
| 6461 | .id_result = base_int, | 6863 | .id_result = base_int, |
| 6462 | .pointer = object_ptr, | 6864 | .pointer = object_ptr, |
| ... | @@ -6464,8 +6866,8 @@ fn structFieldPtr( | ... | @@ -6464,8 +6866,8 @@ fn structFieldPtr( |
| 6464 | const offset_id = try cg.constInt(.usize, byte_offset); | 6866 | const offset_id = try cg.constInt(.usize, byte_offset); |
| 6465 | const adjusted = try cg.buildBinary(.OpIAdd, .{ .ty = .usize, .value = .{ .singleton = base_int } }, .{ .ty = .usize, .value = .{ .singleton = offset_id } }); | 6867 | const adjusted = try cg.buildBinary(.OpIAdd, .{ .ty = .usize, .value = .{ .singleton = base_int } }, .{ .ty = .usize, .value = .{ .singleton = offset_id } }); |
| 6466 | const adjusted_id = try adjusted.materialize(cg); | 6868 | const adjusted_id = try adjusted.materialize(cg); |
| 6467 | const result_id = cg.module.allocId(); | 6869 | const result_id = cg.allocId(); |
| 6468 | try cg.body.emit(cg.module.gpa, .OpConvertUToPtr, .{ | 6870 | try cg.body.emit(cg.gpa, .OpConvertUToPtr, .{ |
| 6469 | .id_result_type = result_ty_id, | 6871 | .id_result_type = result_ty_id, |
| 6470 | .id_result = result_id, | 6872 | .id_result = result_id, |
| 6471 | .integer_value = adjusted_id, | 6873 | .integer_value = adjusted_id, |
| ... | @@ -6483,19 +6885,19 @@ fn structFieldPtr( | ... | @@ -6483,19 +6885,19 @@ fn structFieldPtr( |
| 6483 | if (!layout.has_payload) { | 6885 | if (!layout.has_payload) { |
| 6484 | // Asked to get a pointer to a zero-sized field. Just lower this | 6886 | // Asked to get a pointer to a zero-sized field. Just lower this |
| 6485 | // to undefined, there is no reason to make it be a valid pointer. | 6887 | // to undefined, there is no reason to make it be a valid pointer. |
| 6486 | return try cg.module.constUndef(result_ty_id); | 6888 | return try cg.constUndef(result_ty_id); |
| 6487 | } | 6889 | } |
| 6488 | | 6890 | |
| 6489 | const storage_class = cg.module.storageClass(object_ptr_ty.ptrAddressSpace(zcu)); | 6891 | const storage_class = cg.storageClass(object_ptr_ty.ptrAddressSpace(zcu)); |
| 6490 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); | 6892 | const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); |
| 6491 | const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, storage_class); | 6893 | const pl_ptr_ty_id = try cg.ptrType(layout_payload_ty_id, storage_class); |
| 6492 | const pl_ptr_id = blk: { | 6894 | const pl_ptr_id = blk: { |
| 6493 | if (object_ty.containerLayout(zcu) == .@"packed") break :blk object_ptr; | 6895 | if (object_ty.containerLayout(zcu) == .@"packed") break :blk object_ptr; |
| 6494 | break :blk try cg.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index}); | 6896 | break :blk try cg.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index}); |
| 6495 | }; | 6897 | }; |
| 6496 | | 6898 | |
| 6497 | const active_pl_ptr_id = cg.module.allocId(); | 6899 | const active_pl_ptr_id = cg.allocId(); |
| 6498 | try cg.body.emit(cg.module.gpa, .OpBitcast, .{ | 6900 | try cg.body.emit(cg.gpa, .OpBitcast, .{ |
| 6499 | .id_result_type = result_ty_id, | 6901 | .id_result_type = result_ty_id, |
| 6500 | .id_result = active_pl_ptr_id, | 6902 | .id_result = active_pl_ptr_id, |
| 6501 | .operand = pl_ptr_id, | 6903 | .operand = pl_ptr_id, |
| ... | @@ -6525,9 +6927,9 @@ fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, field_index: u32) | ... | @@ -6525,9 +6927,9 @@ fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, field_index: u32) |
| 6525 | } | 6927 | } |
| 6526 | | 6928 | |
| 6527 | fn alloc(cg: *CodeGen, ty_id: Id, initializer: ?Id) !Id { | 6929 | fn alloc(cg: *CodeGen, ty_id: Id, initializer: ?Id) !Id { |
| 6528 | const ptr_ty_id = try cg.module.ptrType(ty_id, .function); | 6930 | const ptr_ty_id = try cg.ptrType(ty_id, .function); |
| 6529 | const result_id = cg.module.allocId(); | 6931 | const result_id = cg.allocId(); |
| 6530 | try cg.prologue.emit(cg.module.gpa, .OpVariable, .{ | 6932 | try cg.prologue.emit(cg.gpa, .OpVariable, .{ |
| 6531 | .id_result_type = ptr_ty_id, | 6933 | .id_result_type = ptr_ty_id, |
| 6532 | .id_result = result_id, | 6934 | .id_result = result_id, |
| 6533 | .storage_class = .function, | 6935 | .storage_class = .function, |
| ... | @@ -6537,7 +6939,7 @@ fn alloc(cg: *CodeGen, ty_id: Id, initializer: ?Id) !Id { | ... | @@ -6537,7 +6939,7 @@ fn alloc(cg: *CodeGen, ty_id: Id, initializer: ?Id) !Id { |
| 6537 | } | 6939 | } |
| 6538 | | 6940 | |
| 6539 | fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 6941 | fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6540 | const zcu = cg.module.zcu; | 6942 | const zcu = cg.zcu; |
| 6541 | const target = zcu.getTarget(); | 6943 | const target = zcu.getTarget(); |
| 6542 | const ptr_ty = cg.typeOfIndex(inst); | 6944 | const ptr_ty = cg.typeOfIndex(inst); |
| 6543 | const child_ty = ptr_ty.childType(zcu); | 6945 | const child_ty = ptr_ty.childType(zcu); |
| ... | @@ -6546,9 +6948,9 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6546,9 +6948,9 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6546 | .vulkan, .opengl => { | 6948 | .vulkan, .opengl => { |
| 6547 | if (child_ty.zigTypeTag(zcu) == .pointer and !child_ty.isSlice(zcu)) { | 6949 | if (child_ty.zigTypeTag(zcu) == .pointer and !child_ty.isSlice(zcu)) { |
| 6548 | const as = child_ty.ptrAddressSpace(zcu); | 6950 | const as = child_ty.ptrAddressSpace(zcu); |
| 6549 | if (cg.module.storageClass(as) == .function) { | 6951 | if (cg.storageClass(as) == .function) { |
| 6550 | const result_id = cg.module.allocId(); | 6952 | const result_id = cg.allocId(); |
| 6551 | try cg.virtual_allocas.put(cg.module.gpa, result_id, null); | 6953 | try cg.tracked_allocas.put(cg.gpa, result_id, null); |
| 6552 | return result_id; | 6954 | return result_id; |
| 6553 | } | 6955 | } |
| 6554 | } | 6956 | } |
| ... | @@ -6561,7 +6963,7 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6561,7 +6963,7 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6561 | const result_id = try cg.alloc(child_ty_id, null); | 6963 | const result_id = try cg.alloc(child_ty_id, null); |
| 6562 | if (ptr_align != child_ty.abiAlignment(zcu)) { | 6964 | if (ptr_align != child_ty.abiAlignment(zcu)) { |
| 6563 | if (target.os.tag != .opencl) return cg.fail("cannot apply alignment to variables", .{}); | 6965 | if (target.os.tag != .opencl) return cg.fail("cannot apply alignment to variables", .{}); |
| 6564 | try cg.module.decorate(result_id, .{ | 6966 | try cg.decorate(result_id, .{ |
| 6565 | .alignment = .{ .alignment = @intCast(ptr_align.toByteUnits().?) }, | 6967 | .alignment = .{ .alignment = @intCast(ptr_align.toByteUnits().?) }, |
| 6566 | }); | 6968 | }); |
| 6567 | } | 6969 | } |
| ... | @@ -6578,9 +6980,9 @@ fn airArg(cg: *CodeGen) Id { | ... | @@ -6578,9 +6980,9 @@ fn airArg(cg: *CodeGen) Id { |
| 6578 | /// inside the merge block of the block. | 6980 | /// inside the merge block of the block. |
| 6579 | /// This function should only be called with structured control flow generation. | 6981 | /// This function should only be called with structured control flow generation. |
| 6580 | fn structuredNextBlock(cg: *CodeGen, incoming: []const Block.Incoming) !Id { | 6982 | fn structuredNextBlock(cg: *CodeGen, incoming: []const Block.Incoming) !Id { |
| 6581 | const result_id = cg.module.allocId(); | 6983 | const result_id = cg.allocId(); |
| 6582 | const block_id_ty_id = try cg.resolveType(.u32, .direct); | 6984 | const block_id_ty_id = try cg.resolveType(.u32, .direct); |
| 6583 | try cg.body.emitRaw(cg.module.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... | 6985 | try cg.body.emitRaw(cg.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... |
| 6584 | cg.body.writeOperand(Id, block_id_ty_id); | 6986 | cg.body.writeOperand(Id, block_id_ty_id); |
| 6585 | cg.body.writeOperand(Id, result_id); | 6987 | cg.body.writeOperand(Id, result_id); |
| 6586 | | 6988 | |
| ... | @@ -6597,11 +6999,11 @@ fn structuredNextBlock(cg: *CodeGen, incoming: []const Block.Incoming) !Id { | ... | @@ -6597,11 +6999,11 @@ fn structuredNextBlock(cg: *CodeGen, incoming: []const Block.Incoming) !Id { |
| 6597 | fn structuredBreak(cg: *CodeGen, target_block: Id) !void { | 6999 | fn structuredBreak(cg: *CodeGen, target_block: Id) !void { |
| 6598 | if (cg.block_terminated) return; | 7000 | if (cg.block_terminated) return; |
| 6599 | | 7001 | |
| 6600 | const gpa = cg.module.gpa; | 7002 | const gpa = cg.gpa; |
| 6601 | const sblock = cg.block_stack.getLast().?; | 7003 | const sblock = cg.block_stack.getLast().?; |
| 6602 | const merge_block = switch (sblock.*) { | 7004 | const merge_block = switch (sblock.*) { |
| 6603 | .selection => |*merge| blk: { | 7005 | .selection => |*merge| blk: { |
| 6604 | const merge_label = cg.module.allocId(); | 7006 | const merge_label = cg.allocId(); |
| 6605 | try merge.merge_stack.append(gpa, .{ | 7007 | try merge.merge_stack.append(gpa, .{ |
| 6606 | .incoming = .{ | 7008 | .incoming = .{ |
| 6607 | .src_label = cg.block_label, | 7009 | .src_label = cg.block_label, |
| ... | @@ -6641,7 +7043,7 @@ fn genStructuredBody( | ... | @@ -6641,7 +7043,7 @@ fn genStructuredBody( |
| 6641 | }, | 7043 | }, |
| 6642 | body: []const Air.Inst.Index, | 7044 | body: []const Air.Inst.Index, |
| 6643 | ) !Id { | 7045 | ) !Id { |
| 6644 | const gpa = cg.module.gpa; | 7046 | const gpa = cg.gpa; |
| 6645 | | 7047 | |
| 6646 | var sblock: Block = switch (block_merge_type) { | 7048 | var sblock: Block = switch (block_merge_type) { |
| 6647 | .loop => |merge| .{ .loop = .{ | 7049 | .loop => |merge| .{ .loop = .{ |
| ... | @@ -6679,9 +7081,9 @@ fn genStructuredBody( | ... | @@ -6679,9 +7081,9 @@ fn genStructuredBody( |
| 6679 | | 7081 | |
| 6680 | // Make sure that we are still in a block when exiting the function. | 7082 | // Make sure that we are still in a block when exiting the function. |
| 6681 | // TODO: Can we get rid of that? | 7083 | // TODO: Can we get rid of that? |
| 6682 | try cg.beginSpvBlock(cg.module.allocId()); | 7084 | try cg.beginSpvBlock(cg.allocId()); |
| 6683 | const block_id_ty_id = try cg.resolveType(.u32, .direct); | 7085 | const block_id_ty_id = try cg.resolveType(.u32, .direct); |
| 6684 | return try cg.module.constUndef(block_id_ty_id); | 7086 | return try cg.constUndef(block_id_ty_id); |
| 6685 | } | 7087 | } |
| 6686 | | 7088 | |
| 6687 | // The top-most merge actually only has a single source, the | 7089 | // The top-most merge actually only has a single source, the |
| ... | @@ -6735,8 +7137,8 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -6735,8 +7137,8 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 6735 | // of the block, then a label, and then generate the rest of the current | 7137 | // of the block, then a label, and then generate the rest of the current |
| 6736 | // ir.Block in a different SPIR-V block. | 7138 | // ir.Block in a different SPIR-V block. |
| 6737 | | 7139 | |
| 6738 | const gpa = cg.module.gpa; | 7140 | const gpa = cg.gpa; |
| 6739 | const zcu = cg.module.zcu; | 7141 | const zcu = cg.zcu; |
| 6740 | const ty = cg.typeOfIndex(inst); | 7142 | const ty = cg.typeOfIndex(inst); |
| 6741 | const have_block_result = ty.hasRuntimeBits(zcu); | 7143 | const have_block_result = ty.hasRuntimeBits(zcu); |
| 6742 | | 7144 | |
| ... | @@ -6756,7 +7158,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -6756,7 +7158,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 6756 | | 7158 | |
| 6757 | // Check if the target of the branch was this current block. | 7159 | // Check if the target of the branch was this current block. |
| 6758 | const this_block = try cg.constInt(.u32, @intFromEnum(inst)); | 7160 | const this_block = try cg.constInt(.u32, @intFromEnum(inst)); |
| 6759 | const jump_to_this_block_id = cg.module.allocId(); | 7161 | const jump_to_this_block_id = cg.allocId(); |
| 6760 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 7162 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 6761 | try cg.body.emit(gpa, .OpIEqual, .{ | 7163 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 6762 | .id_result_type = bool_ty_id, | 7164 | .id_result_type = bool_ty_id, |
| ... | @@ -6776,8 +7178,8 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -6776,8 +7178,8 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 6776 | .selection => |*merge| { | 7178 | .selection => |*merge| { |
| 6777 | // To jump out of a selection block, push a new entry onto its merge stack and | 7179 | // To jump out of a selection block, push a new entry onto its merge stack and |
| 6778 | // generate a conditional branch to there and to the instructions following this block. | 7180 | // generate a conditional branch to there and to the instructions following this block. |
| 6779 | const merge_label = cg.module.allocId(); | 7181 | const merge_label = cg.allocId(); |
| 6780 | const then_label = cg.module.allocId(); | 7182 | const then_label = cg.allocId(); |
| 6781 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 7183 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 6782 | .merge_block = merge_label, | 7184 | .merge_block = merge_label, |
| 6783 | .selection_control = .{}, | 7185 | .selection_control = .{}, |
| ... | @@ -6800,7 +7202,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -6800,7 +7202,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 6800 | .loop => |*merge| { | 7202 | .loop => |*merge| { |
| 6801 | // To jump out of a loop block, generate a conditional that exits the block | 7203 | // To jump out of a loop block, generate a conditional that exits the block |
| 6802 | // to the loop merge if the target ID is not the one of this block. | 7204 | // to the loop merge if the target ID is not the one of this block. |
| 6803 | const continue_label = cg.module.allocId(); | 7205 | const continue_label = cg.allocId(); |
| 6804 | try cg.body.emit(gpa, .OpBranchConditional, .{ | 7206 | try cg.body.emit(gpa, .OpBranchConditional, .{ |
| 6805 | .condition = jump_to_this_block_id, | 7207 | .condition = jump_to_this_block_id, |
| 6806 | .true_label = continue_label, | 7208 | .true_label = continue_label, |
| ... | @@ -6823,7 +7225,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -6823,7 +7225,7 @@ fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 6823 | } | 7225 | } |
| 6824 | | 7226 | |
| 6825 | fn airBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7227 | fn airBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6826 | const zcu = cg.module.zcu; | 7228 | const zcu = cg.zcu; |
| 6827 | const br = cg.air.instructions.items(.data)[@intFromEnum(inst)].br; | 7229 | const br = cg.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 6828 | const operand_ty = cg.typeOf(br.operand); | 7230 | const operand_ty = cg.typeOf(br.operand); |
| 6829 | | 7231 | |
| ... | @@ -6838,16 +7240,16 @@ fn airBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -6838,16 +7240,16 @@ fn airBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6838 | } | 7240 | } |
| 6839 | | 7241 | |
| 6840 | fn airCondBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7242 | fn airCondBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6841 | const gpa = cg.module.gpa; | 7243 | const gpa = cg.gpa; |
| 6842 | const cond_br = cg.air.unwrapCondBr(inst); | 7244 | const cond_br = cg.air.unwrapCondBr(inst); |
| 6843 | const then_body = cond_br.then_body; | 7245 | const then_body = cond_br.then_body; |
| 6844 | const else_body = cond_br.else_body; | 7246 | const else_body = cond_br.else_body; |
| 6845 | const condition_id = try cg.resolve(cond_br.condition); | 7247 | const condition_id = try cg.resolve(cond_br.condition); |
| 6846 | | 7248 | |
| 6847 | const then_label = cg.module.allocId(); | 7249 | const then_label = cg.allocId(); |
| 6848 | const else_label = cg.module.allocId(); | 7250 | const else_label = cg.allocId(); |
| 6849 | | 7251 | |
| 6850 | const merge_label = cg.module.allocId(); | 7252 | const merge_label = cg.allocId(); |
| 6851 | | 7253 | |
| 6852 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 7254 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 6853 | .merge_block = merge_label, | 7255 | .merge_block = merge_label, |
| ... | @@ -6888,14 +7290,14 @@ fn airCondBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -6888,14 +7290,14 @@ fn airCondBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6888 | } | 7290 | } |
| 6889 | | 7291 | |
| 6890 | fn airLoop(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7292 | fn airLoop(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6891 | const gpa = cg.module.gpa; | 7293 | const gpa = cg.gpa; |
| 6892 | const block = cg.air.unwrapBlock(inst); | 7294 | const block = cg.air.unwrapBlock(inst); |
| 6893 | | 7295 | |
| 6894 | const body_label = cg.module.allocId(); | 7296 | const body_label = cg.allocId(); |
| 6895 | | 7297 | |
| 6896 | const header_label = cg.module.allocId(); | 7298 | const header_label = cg.allocId(); |
| 6897 | const merge_label = cg.module.allocId(); | 7299 | const merge_label = cg.allocId(); |
| 6898 | const continue_label = cg.module.allocId(); | 7300 | const continue_label = cg.allocId(); |
| 6899 | | 7301 | |
| 6900 | // The back-edge must point to the loop header, so generate a separate block for the | 7302 | // The back-edge must point to the loop header, so generate a separate block for the |
| 6901 | // loop header so that we don't accidentally include some instructions from there | 7303 | // loop header so that we don't accidentally include some instructions from there |
| ... | @@ -6927,18 +7329,19 @@ fn airLoop(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -6927,18 +7329,19 @@ fn airLoop(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6927 | } | 7329 | } |
| 6928 | | 7330 | |
| 6929 | fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7331 | fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6930 | const zcu = cg.module.zcu; | 7332 | const zcu = cg.zcu; |
| 6931 | const pt = cg.pt; | 7333 | const pt = cg.pt; |
| 6932 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7334 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6933 | | 7335 | const ptr_ty = cg.typeOf(ty_op.operand); |
| 6934 | const ptr_info = cg.typeOf(ty_op.operand).ptrInfo(zcu); | 7336 | const ptr_info = ptr_ty.ptrInfo(zcu); |
| 6935 | | | |
| 6936 | const elem_ty = cg.typeOfIndex(inst); | 7337 | const elem_ty = cg.typeOfIndex(inst); |
| 6937 | const operand_ptr_id = try cg.resolve(ty_op.operand); | 7338 | const ptr = try cg.resolvePtr(ty_op.operand); |
| 6938 | | | |
| 6939 | assert(ptr_info.child == elem_ty.toIntern()); | 7339 | assert(ptr_info.child == elem_ty.toIntern()); |
| 6940 | | 7340 | |
| 6941 | if (cg.virtual_allocas.get(operand_ptr_id)) |stored| return stored.?; | 7341 | const operand_ptr_id = switch (ptr) { |
| | 7342 | .tracked => |t| return t.slot.*.?, |
| | 7343 | .id => |id| id, |
| | 7344 | }; |
| 6942 | | 7345 | |
| 6943 | if (ptr_info.packed_offset.host_size != 0 and | 7346 | if (ptr_info.packed_offset.host_size != 0 and |
| 6944 | ptr_info.flags.vector_index == .none) | 7347 | ptr_info.flags.vector_index == .none) |
| ... | @@ -6955,7 +7358,7 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6955,7 +7358,7 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6955 | break :blk try shifted.materialize(cg); | 7358 | break :blk try shifted.materialize(cg); |
| 6956 | } else host_val; | 7359 | } else host_val; |
| 6957 | const result_id = blk: { | 7360 | const result_id = blk: { |
| 6958 | if (cg.module.backingIntBits(elem_bit_size).@"0" == cg.module.backingIntBits(host_bits).@"0") | 7361 | if (cg.backingIntBits(elem_bit_size).@"0" == cg.backingIntBits(host_bits).@"0") |
| 6959 | break :blk try cg.bitCast(field_int_ty, host_int_ty, narrowed); | 7362 | break :blk try cg.bitCast(field_int_ty, host_int_ty, narrowed); |
| 6960 | const trunc = try cg.buildConvert(field_int_ty, .{ .ty = host_int_ty, .value = .{ .singleton = narrowed } }); | 7363 | const trunc = try cg.buildConvert(field_int_ty, .{ .ty = host_int_ty, .value = .{ .singleton = narrowed } }); |
| 6961 | break :blk try trunc.materialize(cg); | 7364 | break :blk try trunc.materialize(cg); |
| ... | @@ -6968,9 +7371,9 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6968,9 +7371,9 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6968 | const ptr_id = switch (ptr_info.flags.vector_index) { | 7371 | const ptr_id = switch (ptr_info.flags.vector_index) { |
| 6969 | .none => operand_ptr_id, | 7372 | .none => operand_ptr_id, |
| 6970 | else => |index| ptr_id: { | 7373 | else => |index| ptr_id: { |
| 6971 | const elem_ptr_ty_id = try cg.module.ptrType( | 7374 | const elem_ptr_ty_id = try cg.ptrType( |
| 6972 | try cg.resolveType(elem_ty, .indirect), | 7375 | try cg.resolveType(elem_ty, .indirect), |
| 6973 | cg.module.storageClass(ptr_info.flags.address_space), | 7376 | cg.storageClass(ptr_info.flags.address_space), |
| 6974 | ); | 7377 | ); |
| 6975 | break :ptr_id try cg.accessChain(elem_ptr_ty_id, operand_ptr_id, &.{@intFromEnum(index)}); | 7378 | break :ptr_id try cg.accessChain(elem_ptr_ty_id, operand_ptr_id, &.{@intFromEnum(index)}); |
| 6976 | }, | 7379 | }, |
| ... | @@ -6979,18 +7382,20 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -6979,18 +7382,20 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 6979 | } | 7382 | } |
| 6980 | | 7383 | |
| 6981 | fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7384 | fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 6982 | const zcu = cg.module.zcu; | 7385 | const zcu = cg.zcu; |
| 6983 | const pt = cg.pt; | 7386 | const pt = cg.pt; |
| 6984 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 7387 | const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6985 | const ptr_info = cg.typeOf(bin_op.lhs).ptrInfo(zcu); | 7388 | const ptr_ty = cg.typeOf(bin_op.lhs); |
| | 7389 | const ptr_info = ptr_ty.ptrInfo(zcu); |
| 6986 | const elem_ty: Type = .fromInterned(ptr_info.child); | 7390 | const elem_ty: Type = .fromInterned(ptr_info.child); |
| 6987 | const operand_ptr_id = try cg.resolve(bin_op.lhs); | | |
| 6988 | const value_id = try cg.resolve(bin_op.rhs); | 7391 | const value_id = try cg.resolve(bin_op.rhs); |
| 6989 | | 7392 | const operand_ptr_id = switch (try cg.resolvePtr(bin_op.lhs)) { |
| 6990 | if (cg.virtual_allocas.getPtr(operand_ptr_id)) |slot| { | 7393 | .tracked => |t| { |
| 6991 | slot.* = value_id; | 7394 | t.slot.* = value_id; |
| 6992 | return; | 7395 | return; |
| 6993 | } | 7396 | }, |
| | 7397 | .id => |id| id, |
| | 7398 | }; |
| 6994 | | 7399 | |
| 6995 | if (ptr_info.packed_offset.host_size != 0 and | 7400 | if (ptr_info.packed_offset.host_size != 0 and |
| 6996 | ptr_info.flags.vector_index == .none) | 7401 | ptr_info.flags.vector_index == .none) |
| ... | @@ -7013,7 +7418,7 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7013,7 +7418,7 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7013 | } | 7418 | } |
| 7014 | | 7419 | |
| 7015 | const extended = blk: { | 7420 | const extended = blk: { |
| 7016 | if (cg.module.backingIntBits(elem_bit_size).@"0" == cg.module.backingIntBits(host_bits).@"0") | 7421 | if (cg.backingIntBits(elem_bit_size).@"0" == cg.backingIntBits(host_bits).@"0") |
| 7017 | break :blk try cg.bitCast(host_int_ty, field_int_ty, value_as_int); | 7422 | break :blk try cg.bitCast(host_int_ty, field_int_ty, value_as_int); |
| 7018 | const conv = try cg.buildConvert(host_int_ty, .{ .ty = field_int_ty, .value = .{ .singleton = value_as_int } }); | 7423 | const conv = try cg.buildConvert(host_int_ty, .{ .ty = field_int_ty, .value = .{ .singleton = value_as_int } }); |
| 7019 | break :blk try conv.materialize(cg); | 7424 | break :blk try conv.materialize(cg); |
| ... | @@ -7037,9 +7442,9 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7037,9 +7442,9 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7037 | const ptr_id = switch (ptr_info.flags.vector_index) { | 7442 | const ptr_id = switch (ptr_info.flags.vector_index) { |
| 7038 | .none => operand_ptr_id, | 7443 | .none => operand_ptr_id, |
| 7039 | else => |index| ptr_id: { | 7444 | else => |index| ptr_id: { |
| 7040 | const elem_ptr_ty_id = try cg.module.ptrType( | 7445 | const elem_ptr_ty_id = try cg.ptrType( |
| 7041 | try cg.resolveType(elem_ty, .indirect), | 7446 | try cg.resolveType(elem_ty, .indirect), |
| 7042 | cg.module.storageClass(ptr_info.flags.address_space), | 7447 | cg.storageClass(ptr_info.flags.address_space), |
| 7043 | ); | 7448 | ); |
| 7044 | break :ptr_id try cg.accessChain(elem_ptr_ty_id, operand_ptr_id, &.{@intFromEnum(index)}); | 7449 | break :ptr_id try cg.accessChain(elem_ptr_ty_id, operand_ptr_id, &.{@intFromEnum(index)}); |
| 7045 | }, | 7450 | }, |
| ... | @@ -7049,8 +7454,8 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7049,8 +7454,8 @@ fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7049 | } | 7454 | } |
| 7050 | | 7455 | |
| 7051 | fn airRet(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7456 | fn airRet(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7052 | const gpa = cg.module.gpa; | 7457 | const gpa = cg.gpa; |
| 7053 | const zcu = cg.module.zcu; | 7458 | const zcu = cg.zcu; |
| 7054 | const operand = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 7459 | const operand = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7055 | const ret_ty = cg.typeOf(operand); | 7460 | const ret_ty = cg.typeOf(operand); |
| 7056 | if (!ret_ty.hasRuntimeBits(zcu)) { | 7461 | if (!ret_ty.hasRuntimeBits(zcu)) { |
| ... | @@ -7071,8 +7476,8 @@ fn airRet(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7071,8 +7476,8 @@ fn airRet(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7071 | } | 7476 | } |
| 7072 | | 7477 | |
| 7073 | fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7478 | fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7074 | const gpa = cg.module.gpa; | 7479 | const gpa = cg.gpa; |
| 7075 | const zcu = cg.module.zcu; | 7480 | const zcu = cg.zcu; |
| 7076 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 7481 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7077 | const ptr_ty = cg.typeOf(un_op); | 7482 | const ptr_ty = cg.typeOf(un_op); |
| 7078 | const ret_ty = ptr_ty.childType(zcu); | 7483 | const ret_ty = ptr_ty.childType(zcu); |
| ... | @@ -7090,16 +7495,18 @@ fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7090,16 +7495,18 @@ fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7090 | } | 7495 | } |
| 7091 | } | 7496 | } |
| 7092 | | 7497 | |
| 7093 | const ptr = try cg.resolve(un_op); | 7498 | const value = switch (try cg.resolvePtr(un_op)) { |
| 7094 | const value = try cg.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); | 7499 | .tracked => |t| t.slot.*.?, |
| | 7500 | .id => |ptr| try cg.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }), |
| | 7501 | }; |
| 7095 | try cg.body.emit(gpa, .OpReturnValue, .{ | 7502 | try cg.body.emit(gpa, .OpReturnValue, .{ |
| 7096 | .value = value, | 7503 | .value = value, |
| 7097 | }); | 7504 | }); |
| 7098 | } | 7505 | } |
| 7099 | | 7506 | |
| 7100 | fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7507 | fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7101 | const gpa = cg.module.gpa; | 7508 | const gpa = cg.gpa; |
| 7102 | const zcu = cg.module.zcu; | 7509 | const zcu = cg.zcu; |
| 7103 | const unwrapped_try = cg.air.unwrapTry(inst); | 7510 | const unwrapped_try = cg.air.unwrapTry(inst); |
| 7104 | const body = unwrapped_try.else_body; | 7511 | const body = unwrapped_try.else_body; |
| 7105 | | 7512 | |
| ... | @@ -7118,7 +7525,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7118,7 +7525,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7118 | err_union_id; | 7525 | err_union_id; |
| 7119 | | 7526 | |
| 7120 | const zero_id = try cg.constInt(.anyerror, 0); | 7527 | const zero_id = try cg.constInt(.anyerror, 0); |
| 7121 | const is_err_id = cg.module.allocId(); | 7528 | const is_err_id = cg.allocId(); |
| 7122 | try cg.body.emit(gpa, .OpINotEqual, .{ | 7529 | try cg.body.emit(gpa, .OpINotEqual, .{ |
| 7123 | .id_result_type = bool_ty_id, | 7530 | .id_result_type = bool_ty_id, |
| 7124 | .id_result = is_err_id, | 7531 | .id_result = is_err_id, |
| ... | @@ -7130,8 +7537,8 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7130,8 +7537,8 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7130 | // with the current body. | 7537 | // with the current body. |
| 7131 | // Just generate a new block here, then generate a new block inline for the remainder of the body. | 7538 | // Just generate a new block here, then generate a new block inline for the remainder of the body. |
| 7132 | | 7539 | |
| 7133 | const err_block = cg.module.allocId(); | 7540 | const err_block = cg.allocId(); |
| 7134 | const ok_block = cg.module.allocId(); | 7541 | const ok_block = cg.allocId(); |
| 7135 | | 7542 | |
| 7136 | // According to AIR documentation, this block is guaranteed | 7543 | // According to AIR documentation, this block is guaranteed |
| 7137 | // to not break and end in a return instruction. Thus, | 7544 | // to not break and end in a return instruction. Thus, |
| ... | @@ -7162,7 +7569,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7162,7 +7569,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7162 | } | 7569 | } |
| 7163 | | 7570 | |
| 7164 | fn airErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7571 | fn airErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7165 | const zcu = cg.module.zcu; | 7572 | const zcu = cg.zcu; |
| 7166 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7573 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7167 | const operand_id = try cg.resolve(ty_op.operand); | 7574 | const operand_id = try cg.resolve(ty_op.operand); |
| 7168 | const err_union_ty = cg.typeOf(ty_op.operand); | 7575 | const err_union_ty = cg.typeOf(ty_op.operand); |
| ... | @@ -7170,7 +7577,7 @@ fn airErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7170,7 +7577,7 @@ fn airErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7170 | | 7577 | |
| 7171 | if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { | 7578 | if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { |
| 7172 | // No error possible, so just return undefined. | 7579 | // No error possible, so just return undefined. |
| 7173 | return try cg.module.constUndef(err_ty_id); | 7580 | return try cg.constUndef(err_ty_id); |
| 7174 | } | 7581 | } |
| 7175 | | 7582 | |
| 7176 | const payload_ty = err_union_ty.errorUnionPayload(zcu); | 7583 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| ... | @@ -7198,7 +7605,7 @@ fn airErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7198,7 +7605,7 @@ fn airErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7198 | } | 7605 | } |
| 7199 | | 7606 | |
| 7200 | fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7607 | fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7201 | const zcu = cg.module.zcu; | 7608 | const zcu = cg.zcu; |
| 7202 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7609 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7203 | const err_union_ty = cg.typeOfIndex(inst); | 7610 | const err_union_ty = cg.typeOfIndex(inst); |
| 7204 | const payload_ty = err_union_ty.errorUnionPayload(zcu); | 7611 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| ... | @@ -7213,7 +7620,7 @@ fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7213,7 +7620,7 @@ fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7213 | | 7620 | |
| 7214 | var members: [2]Id = undefined; | 7621 | var members: [2]Id = undefined; |
| 7215 | members[eu_layout.errorFieldIndex()] = operand_id; | 7622 | members[eu_layout.errorFieldIndex()] = operand_id; |
| 7216 | members[eu_layout.payloadFieldIndex()] = try cg.module.constUndef(payload_ty_id); | 7623 | members[eu_layout.payloadFieldIndex()] = try cg.constUndef(payload_ty_id); |
| 7217 | | 7624 | |
| 7218 | var types: [2]Type = undefined; | 7625 | var types: [2]Type = undefined; |
| 7219 | types[eu_layout.errorFieldIndex()] = .anyerror; | 7626 | types[eu_layout.errorFieldIndex()] = .anyerror; |
| ... | @@ -7247,7 +7654,7 @@ fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7247,7 +7654,7 @@ fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7247 | } | 7654 | } |
| 7248 | | 7655 | |
| 7249 | fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { is_null, is_non_null }) !?Id { | 7656 | fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { is_null, is_non_null }) !?Id { |
| 7250 | const zcu = cg.module.zcu; | 7657 | const zcu = cg.zcu; |
| 7251 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 7658 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7252 | const operand_id = try cg.resolve(un_op); | 7659 | const operand_id = try cg.resolve(un_op); |
| 7253 | const operand_ty = cg.typeOf(un_op); | 7660 | const operand_ty = cg.typeOf(un_op); |
| ... | @@ -7274,7 +7681,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { | ... | @@ -7274,7 +7681,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { |
| 7274 | loaded_id; | 7681 | loaded_id; |
| 7275 | | 7682 | |
| 7276 | const ptr_ty_id = try cg.resolveType(ptr_ty, .direct); | 7683 | const ptr_ty_id = try cg.resolveType(ptr_ty, .direct); |
| 7277 | const null_id = try cg.module.constNull(ptr_ty_id); | 7684 | const null_id = try cg.constNull(ptr_ty_id); |
| 7278 | const null_tmp: Temporary = .init(ptr_ty, null_id); | 7685 | const null_tmp: Temporary = .init(ptr_ty, null_id); |
| 7279 | const ptr: Temporary = .init(ptr_ty, ptr_id); | 7686 | const ptr: Temporary = .init(ptr_ty, ptr_id); |
| 7280 | | 7687 | |
| ... | @@ -7289,9 +7696,9 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { | ... | @@ -7289,9 +7696,9 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { |
| 7289 | const is_non_null_id = blk: { | 7696 | const is_non_null_id = blk: { |
| 7290 | if (is_pointer) { | 7697 | if (is_pointer) { |
| 7291 | if (payload_ty.hasRuntimeBits(zcu)) { | 7698 | if (payload_ty.hasRuntimeBits(zcu)) { |
| 7292 | const storage_class = cg.module.storageClass(operand_ty.ptrAddressSpace(zcu)); | 7699 | const storage_class = cg.storageClass(operand_ty.ptrAddressSpace(zcu)); |
| 7293 | const bool_indirect_ty_id = try cg.resolveType(.bool, .indirect); | 7700 | const bool_indirect_ty_id = try cg.resolveType(.bool, .indirect); |
| 7294 | const bool_ptr_ty_id = try cg.module.ptrType(bool_indirect_ty_id, storage_class); | 7701 | const bool_ptr_ty_id = try cg.ptrType(bool_indirect_ty_id, storage_class); |
| 7295 | const tag_ptr_id = try cg.accessChain(bool_ptr_ty_id, operand_id, &.{1}); | 7702 | const tag_ptr_id = try cg.accessChain(bool_ptr_ty_id, operand_id, &.{1}); |
| 7296 | break :blk try cg.load(.bool, tag_ptr_id, .{}); | 7703 | break :blk try cg.load(.bool, tag_ptr_id, .{}); |
| 7297 | } | 7704 | } |
| ... | @@ -7311,8 +7718,8 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { | ... | @@ -7311,8 +7718,8 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { |
| 7311 | return switch (pred) { | 7718 | return switch (pred) { |
| 7312 | .is_null => blk: { | 7719 | .is_null => blk: { |
| 7313 | // Invert condition | 7720 | // Invert condition |
| 7314 | const result_id = cg.module.allocId(); | 7721 | const result_id = cg.allocId(); |
| 7315 | try cg.body.emit(cg.module.gpa, .OpLogicalNot, .{ | 7722 | try cg.body.emit(cg.gpa, .OpLogicalNot, .{ |
| 7316 | .id_result_type = bool_ty_id, | 7723 | .id_result_type = bool_ty_id, |
| 7317 | .id_result = result_id, | 7724 | .id_result = result_id, |
| 7318 | .operand = is_non_null_id, | 7725 | .operand = is_non_null_id, |
| ... | @@ -7324,7 +7731,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { | ... | @@ -7324,7 +7731,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { |
| 7324 | } | 7731 | } |
| 7325 | | 7732 | |
| 7326 | fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err }) !?Id { | 7733 | fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err }) !?Id { |
| 7327 | const zcu = cg.module.zcu; | 7734 | const zcu = cg.zcu; |
| 7328 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 7735 | const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7329 | const operand_id = try cg.resolve(un_op); | 7736 | const operand_id = try cg.resolve(un_op); |
| 7330 | const err_union_ty = cg.typeOf(un_op); | 7737 | const err_union_ty = cg.typeOf(un_op); |
| ... | @@ -7342,10 +7749,10 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err | ... | @@ -7342,10 +7749,10 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err |
| 7342 | else | 7749 | else |
| 7343 | try cg.extractField(.anyerror, operand_id, eu_layout.errorFieldIndex()); | 7750 | try cg.extractField(.anyerror, operand_id, eu_layout.errorFieldIndex()); |
| 7344 | | 7751 | |
| 7345 | const result_id = cg.module.allocId(); | 7752 | const result_id = cg.allocId(); |
| 7346 | switch (pred) { | 7753 | switch (pred) { |
| 7347 | inline else => |pred_ct| try cg.body.emit( | 7754 | inline else => |pred_ct| try cg.body.emit( |
| 7348 | cg.module.gpa, | 7755 | cg.gpa, |
| 7349 | switch (pred_ct) { | 7756 | switch (pred_ct) { |
| 7350 | .is_err => .OpINotEqual, | 7757 | .is_err => .OpINotEqual, |
| 7351 | .is_non_err => .OpIEqual, | 7758 | .is_non_err => .OpIEqual, |
| ... | @@ -7362,7 +7769,7 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err | ... | @@ -7362,7 +7769,7 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err |
| 7362 | } | 7769 | } |
| 7363 | | 7770 | |
| 7364 | fn airUnwrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7771 | fn airUnwrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7365 | const zcu = cg.module.zcu; | 7772 | const zcu = cg.zcu; |
| 7366 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7773 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7367 | const operand_id = try cg.resolve(ty_op.operand); | 7774 | const operand_id = try cg.resolve(ty_op.operand); |
| 7368 | const optional_ty = cg.typeOf(ty_op.operand); | 7775 | const optional_ty = cg.typeOf(ty_op.operand); |
| ... | @@ -7378,7 +7785,7 @@ fn airUnwrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7378,7 +7785,7 @@ fn airUnwrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7378 | } | 7785 | } |
| 7379 | | 7786 | |
| 7380 | fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7787 | fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7381 | const zcu = cg.module.zcu; | 7788 | const zcu = cg.zcu; |
| 7382 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7789 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7383 | const operand_id = try cg.resolve(ty_op.operand); | 7790 | const operand_id = try cg.resolve(ty_op.operand); |
| 7384 | const operand_ty = cg.typeOf(ty_op.operand); | 7791 | const operand_ty = cg.typeOf(ty_op.operand); |
| ... | @@ -7402,7 +7809,7 @@ fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7402,7 +7809,7 @@ fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7402 | } | 7809 | } |
| 7403 | | 7810 | |
| 7404 | fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 7811 | fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7405 | const zcu = cg.module.zcu; | 7812 | const zcu = cg.zcu; |
| 7406 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 7813 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7407 | const payload_ty = cg.typeOf(ty_op.operand); | 7814 | const payload_ty = cg.typeOf(ty_op.operand); |
| 7408 | | 7815 | |
| ... | @@ -7422,9 +7829,9 @@ fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7422,9 +7829,9 @@ fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7422 | } | 7829 | } |
| 7423 | | 7830 | |
| 7424 | fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | 7831 | fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7425 | const gpa = cg.module.gpa; | 7832 | const gpa = cg.gpa; |
| 7426 | const zcu = cg.module.zcu; | 7833 | const zcu = cg.zcu; |
| 7427 | const target = cg.module.zcu.getTarget(); | 7834 | const target = cg.zcu.getTarget(); |
| 7428 | const switch_br = cg.air.unwrapSwitch(inst); | 7835 | const switch_br = cg.air.unwrapSwitch(inst); |
| 7429 | const cond_ty = cg.typeOf(switch_br.operand); | 7836 | const cond_ty = cg.typeOf(switch_br.operand); |
| 7430 | const cond = try cg.resolve(switch_br.operand); | 7837 | const cond = try cg.resolve(switch_br.operand); |
| ... | @@ -7434,14 +7841,14 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7434,14 +7841,14 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7434 | .bool, .error_set => 1, | 7841 | .bool, .error_set => 1, |
| 7435 | .int => blk: { | 7842 | .int => blk: { |
| 7436 | const bits = cond_ty.intInfo(zcu).bits; | 7843 | const bits = cond_ty.intInfo(zcu).bits; |
| 7437 | const backing_bits, const big_int = cg.module.backingIntBits(bits); | 7844 | const backing_bits, const big_int = cg.backingIntBits(bits); |
| 7438 | if (big_int) return cg.todo("implement composite int switch", .{}); | 7845 | if (big_int) return cg.todo("implement composite int switch", .{}); |
| 7439 | break :blk if (backing_bits <= 32) 1 else 2; | 7846 | break :blk if (backing_bits <= 32) 1 else 2; |
| 7440 | }, | 7847 | }, |
| 7441 | .@"enum" => blk: { | 7848 | .@"enum" => blk: { |
| 7442 | const int_ty = cond_ty.intTagType(zcu); | 7849 | const int_ty = cond_ty.intTagType(zcu); |
| 7443 | const int_info = int_ty.intInfo(zcu); | 7850 | const int_info = int_ty.intInfo(zcu); |
| 7444 | const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); | 7851 | const backing_bits, const big_int = cg.backingIntBits(int_info.bits); |
| 7445 | if (big_int) return cg.todo("implement composite int switch", .{}); | 7852 | if (big_int) return cg.todo("implement composite int switch", .{}); |
| 7446 | break :blk if (backing_bits <= 32) 1 else 2; | 7853 | break :blk if (backing_bits <= 32) 1 else 2; |
| 7447 | }, | 7854 | }, |
| ... | @@ -7470,12 +7877,12 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7470,12 +7877,12 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7470 | } | 7877 | } |
| 7471 | | 7878 | |
| 7472 | // First, pre-allocate the labels for the cases. | 7879 | // First, pre-allocate the labels for the cases. |
| 7473 | const case_labels = cg.module.allocIds(num_cases); | 7880 | const case_labels = cg.allocIds(num_cases); |
| 7474 | // We always need the default case - if zig has none, we will generate unreachable there. | 7881 | // We always need the default case - if zig has none, we will generate unreachable there. |
| 7475 | const default_label = cg.module.allocId(); | 7882 | const default_label = cg.allocId(); |
| 7476 | const switch_default = if (last_range_case != null) cg.module.allocId() else default_label; | 7883 | const switch_default = if (last_range_case != null) cg.allocId() else default_label; |
| 7477 | | 7884 | |
| 7478 | const merge_label = cg.module.allocId(); | 7885 | const merge_label = cg.allocId(); |
| 7479 | | 7886 | |
| 7480 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 7887 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 7481 | .merge_block = merge_label, | 7888 | .merge_block = merge_label, |
| ... | @@ -7540,12 +7947,13 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7540,12 +7947,13 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7540 | const item_tmp: Temporary = try cg.temporary(item); | 7947 | const item_tmp: Temporary = try cg.temporary(item); |
| 7541 | const eq = try (try cg.cmp(.eq, cond_tmp, item_tmp)).materialize(cg); | 7948 | const eq = try (try cg.cmp(.eq, cond_tmp, item_tmp)).materialize(cg); |
| 7542 | case_cond = if (case_cond) |prev| blk: { | 7949 | case_cond = if (case_cond) |prev| blk: { |
| 7543 | const combined = cg.module.allocId(); | 7950 | const combined = cg.allocId(); |
| 7544 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 7951 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 7545 | cg.body.writeOperand(Id, bool_ty_id); | 7952 | .id_result_type = bool_ty_id, |
| 7546 | cg.body.writeOperand(Id, combined); | 7953 | .id_result = combined, |
| 7547 | cg.body.writeOperand(Id, prev); | 7954 | .operand_1 = prev, |
| 7548 | cg.body.writeOperand(Id, eq); | 7955 | .operand_2 = eq, |
| | 7956 | }); |
| 7549 | break :blk combined; | 7957 | break :blk combined; |
| 7550 | } else eq; | 7958 | } else eq; |
| 7551 | } | 7959 | } |
| ... | @@ -7555,26 +7963,28 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7555,26 +7963,28 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7555 | const hi_tmp: Temporary = try cg.temporary(range[1]); | 7963 | const hi_tmp: Temporary = try cg.temporary(range[1]); |
| 7556 | const ge = try (try cg.cmp(.gte, cond_tmp, lo_tmp)).materialize(cg); | 7964 | const ge = try (try cg.cmp(.gte, cond_tmp, lo_tmp)).materialize(cg); |
| 7557 | const le = try (try cg.cmp(.lte, cond_tmp, hi_tmp)).materialize(cg); | 7965 | const le = try (try cg.cmp(.lte, cond_tmp, hi_tmp)).materialize(cg); |
| 7558 | const in_range = cg.module.allocId(); | 7966 | const in_range = cg.allocId(); |
| 7559 | try cg.body.emitRaw(gpa, .OpLogicalAnd, 4); | 7967 | try cg.body.emit(gpa, .OpLogicalAnd, .{ |
| 7560 | cg.body.writeOperand(Id, bool_ty_id); | 7968 | .id_result_type = bool_ty_id, |
| 7561 | cg.body.writeOperand(Id, in_range); | 7969 | .id_result = in_range, |
| 7562 | cg.body.writeOperand(Id, ge); | 7970 | .operand_1 = ge, |
| 7563 | cg.body.writeOperand(Id, le); | 7971 | .operand_2 = le, |
| | 7972 | }); |
| 7564 | case_cond = if (case_cond) |prev| blk: { | 7973 | case_cond = if (case_cond) |prev| blk: { |
| 7565 | const combined = cg.module.allocId(); | 7974 | const combined = cg.allocId(); |
| 7566 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 7975 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 7567 | cg.body.writeOperand(Id, bool_ty_id); | 7976 | .id_result_type = bool_ty_id, |
| 7568 | cg.body.writeOperand(Id, combined); | 7977 | .id_result = combined, |
| 7569 | cg.body.writeOperand(Id, prev); | 7978 | .operand_1 = prev, |
| 7570 | cg.body.writeOperand(Id, in_range); | 7979 | .operand_2 = in_range, |
| | 7980 | }); |
| 7571 | break :blk combined; | 7981 | break :blk combined; |
| 7572 | } else in_range; | 7982 | } else in_range; |
| 7573 | } | 7983 | } |
| 7574 | | 7984 | |
| 7575 | const case_label = case_labels.at(case.idx); | 7985 | const case_label = case_labels.at(case.idx); |
| 7576 | const is_last = case.idx == last_range_case.?; | 7986 | const is_last = case.idx == last_range_case.?; |
| 7577 | const next_check = if (is_last) default_label else cg.module.allocId(); | 7987 | const next_check = if (is_last) default_label else cg.allocId(); |
| 7578 | | 7988 | |
| 7579 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 7989 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 7580 | .merge_block = next_check, | 7990 | .merge_block = next_check, |
| ... | @@ -7633,9 +8043,9 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7633,9 +8043,9 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7633 | } | 8043 | } |
| 7634 | | 8044 | |
| 7635 | fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | 8045 | fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7636 | const gpa = cg.module.gpa; | 8046 | const gpa = cg.gpa; |
| 7637 | const zcu = cg.module.zcu; | 8047 | const zcu = cg.zcu; |
| 7638 | const target = cg.module.zcu.getTarget(); | 8048 | const target = cg.zcu.getTarget(); |
| 7639 | const switch_br = cg.air.unwrapSwitch(inst); | 8049 | const switch_br = cg.air.unwrapSwitch(inst); |
| 7640 | const cond_ty = cg.typeOf(switch_br.operand); | 8050 | const cond_ty = cg.typeOf(switch_br.operand); |
| 7641 | const initial_cond = try cg.resolve(switch_br.operand); | 8051 | const initial_cond = try cg.resolve(switch_br.operand); |
| ... | @@ -7645,14 +8055,14 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7645,14 +8055,14 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7645 | .bool, .error_set => 1, | 8055 | .bool, .error_set => 1, |
| 7646 | .int => blk: { | 8056 | .int => blk: { |
| 7647 | const bits = cond_ty.intInfo(zcu).bits; | 8057 | const bits = cond_ty.intInfo(zcu).bits; |
| 7648 | const backing_bits, const big_int = cg.module.backingIntBits(bits); | 8058 | const backing_bits, const big_int = cg.backingIntBits(bits); |
| 7649 | if (big_int) return cg.todo("implement composite int loop switch", .{}); | 8059 | if (big_int) return cg.todo("implement composite int loop switch", .{}); |
| 7650 | break :blk if (backing_bits <= 32) 1 else 2; | 8060 | break :blk if (backing_bits <= 32) 1 else 2; |
| 7651 | }, | 8061 | }, |
| 7652 | .@"enum" => blk: { | 8062 | .@"enum" => blk: { |
| 7653 | const int_ty = cond_ty.intTagType(zcu); | 8063 | const int_ty = cond_ty.intTagType(zcu); |
| 7654 | const int_info = int_ty.intInfo(zcu); | 8064 | const int_info = int_ty.intInfo(zcu); |
| 7655 | const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); | 8065 | const backing_bits, const big_int = cg.backingIntBits(int_info.bits); |
| 7656 | if (big_int) return cg.todo("implement composite int loop switch", .{}); | 8066 | if (big_int) return cg.todo("implement composite int loop switch", .{}); |
| 7657 | break :blk if (backing_bits <= 32) 1 else 2; | 8067 | break :blk if (backing_bits <= 32) 1 else 2; |
| 7658 | }, | 8068 | }, |
| ... | @@ -7682,15 +8092,15 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7682,15 +8092,15 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7682 | } | 8092 | } |
| 7683 | } | 8093 | } |
| 7684 | | 8094 | |
| 7685 | const case_labels = cg.module.allocIds(num_cases); | 8095 | const case_labels = cg.allocIds(num_cases); |
| 7686 | const default_label = cg.module.allocId(); | 8096 | const default_label = cg.allocId(); |
| 7687 | const switch_default = if (last_range_case != null) cg.module.allocId() else default_label; | 8097 | const switch_default = if (last_range_case != null) cg.allocId() else default_label; |
| 7688 | | 8098 | |
| 7689 | const header_label = cg.module.allocId(); | 8099 | const header_label = cg.allocId(); |
| 7690 | const loop_merge = cg.module.allocId(); | 8100 | const loop_merge = cg.allocId(); |
| 7691 | const continue_label = cg.module.allocId(); | 8101 | const continue_label = cg.allocId(); |
| 7692 | const switch_merge = cg.module.allocId(); | 8102 | const switch_merge = cg.allocId(); |
| 7693 | const body_label = cg.module.allocId(); | 8103 | const body_label = cg.allocId(); |
| 7694 | | 8104 | |
| 7695 | // switch_dispatch signals "continue the loop" by using this sentinel as the | 8105 | // switch_dispatch signals "continue the loop" by using this sentinel as the |
| 7696 | // next_block in structuredBreak. at switch_merge, a phi + comparison distinguishes | 8106 | // next_block in structuredBreak. at switch_merge, a phi + comparison distinguishes |
| ... | @@ -7772,12 +8182,13 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7772,12 +8182,13 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7772 | const item_tmp: Temporary = try cg.temporary(item); | 8182 | const item_tmp: Temporary = try cg.temporary(item); |
| 7773 | const eq = try (try cg.cmp(.eq, cond_tmp, item_tmp)).materialize(cg); | 8183 | const eq = try (try cg.cmp(.eq, cond_tmp, item_tmp)).materialize(cg); |
| 7774 | case_cond = if (case_cond) |prev| blk: { | 8184 | case_cond = if (case_cond) |prev| blk: { |
| 7775 | const combined = cg.module.allocId(); | 8185 | const combined = cg.allocId(); |
| 7776 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 8186 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 7777 | cg.body.writeOperand(Id, bool_ty_id); | 8187 | .id_result_type = bool_ty_id, |
| 7778 | cg.body.writeOperand(Id, combined); | 8188 | .id_result = combined, |
| 7779 | cg.body.writeOperand(Id, prev); | 8189 | .operand_1 = prev, |
| 7780 | cg.body.writeOperand(Id, eq); | 8190 | .operand_2 = eq, |
| | 8191 | }); |
| 7781 | break :blk combined; | 8192 | break :blk combined; |
| 7782 | } else eq; | 8193 | } else eq; |
| 7783 | } | 8194 | } |
| ... | @@ -7787,26 +8198,28 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7787,26 +8198,28 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7787 | const hi_tmp: Temporary = try cg.temporary(range[1]); | 8198 | const hi_tmp: Temporary = try cg.temporary(range[1]); |
| 7788 | const ge = try (try cg.cmp(.gte, cond_tmp, lo_tmp)).materialize(cg); | 8199 | const ge = try (try cg.cmp(.gte, cond_tmp, lo_tmp)).materialize(cg); |
| 7789 | const le = try (try cg.cmp(.lte, cond_tmp, hi_tmp)).materialize(cg); | 8200 | const le = try (try cg.cmp(.lte, cond_tmp, hi_tmp)).materialize(cg); |
| 7790 | const in_range = cg.module.allocId(); | 8201 | const in_range = cg.allocId(); |
| 7791 | try cg.body.emitRaw(gpa, .OpLogicalAnd, 4); | 8202 | try cg.body.emit(gpa, .OpLogicalAnd, .{ |
| 7792 | cg.body.writeOperand(Id, bool_ty_id); | 8203 | .id_result_type = bool_ty_id, |
| 7793 | cg.body.writeOperand(Id, in_range); | 8204 | .id_result = in_range, |
| 7794 | cg.body.writeOperand(Id, ge); | 8205 | .operand_1 = ge, |
| 7795 | cg.body.writeOperand(Id, le); | 8206 | .operand_2 = le, |
| | 8207 | }); |
| 7796 | case_cond = if (case_cond) |prev| blk: { | 8208 | case_cond = if (case_cond) |prev| blk: { |
| 7797 | const combined = cg.module.allocId(); | 8209 | const combined = cg.allocId(); |
| 7798 | try cg.body.emitRaw(gpa, .OpLogicalOr, 4); | 8210 | try cg.body.emit(gpa, .OpLogicalOr, .{ |
| 7799 | cg.body.writeOperand(Id, bool_ty_id); | 8211 | .id_result_type = bool_ty_id, |
| 7800 | cg.body.writeOperand(Id, combined); | 8212 | .id_result = combined, |
| 7801 | cg.body.writeOperand(Id, prev); | 8213 | .operand_1 = prev, |
| 7802 | cg.body.writeOperand(Id, in_range); | 8214 | .operand_2 = in_range, |
| | 8215 | }); |
| 7803 | break :blk combined; | 8216 | break :blk combined; |
| 7804 | } else in_range; | 8217 | } else in_range; |
| 7805 | } | 8218 | } |
| 7806 | | 8219 | |
| 7807 | const case_label = case_labels.at(case.idx); | 8220 | const case_label = case_labels.at(case.idx); |
| 7808 | const is_last = case.idx == last_range_case.?; | 8221 | const is_last = case.idx == last_range_case.?; |
| 7809 | const next_check = if (is_last) default_label else cg.module.allocId(); | 8222 | const next_check = if (is_last) default_label else cg.allocId(); |
| 7810 | | 8223 | |
| 7811 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 8224 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 7812 | .merge_block = next_check, | 8225 | .merge_block = next_check, |
| ... | @@ -7860,7 +8273,7 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7860,7 +8273,7 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7860 | try cg.beginSpvBlock(switch_merge); | 8273 | try cg.beginSpvBlock(switch_merge); |
| 7861 | const next_block = try cg.structuredNextBlock(incoming_structured_blocks.items); | 8274 | const next_block = try cg.structuredNextBlock(incoming_structured_blocks.items); |
| 7862 | | 8275 | |
| 7863 | const is_dispatch = cg.module.allocId(); | 8276 | const is_dispatch = cg.allocId(); |
| 7864 | const bool_ty_id = try cg.resolveType(.bool, .direct); | 8277 | const bool_ty_id = try cg.resolveType(.bool, .direct); |
| 7865 | try cg.body.emit(gpa, .OpIEqual, .{ | 8278 | try cg.body.emit(gpa, .OpIEqual, .{ |
| 7866 | .id_result_type = bool_ty_id, | 8279 | .id_result_type = bool_ty_id, |
| ... | @@ -7869,12 +8282,12 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7869,12 +8282,12 @@ fn airLoopSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7869 | .operand_2 = dispatch_sentinel, | 8282 | .operand_2 = dispatch_sentinel, |
| 7870 | }); | 8283 | }); |
| 7871 | | 8284 | |
| 7872 | const dispatch_check_merge = cg.module.allocId(); | 8285 | const dispatch_check_merge = cg.allocId(); |
| 7873 | try cg.body.emit(gpa, .OpSelectionMerge, .{ | 8286 | try cg.body.emit(gpa, .OpSelectionMerge, .{ |
| 7874 | .merge_block = dispatch_check_merge, | 8287 | .merge_block = dispatch_check_merge, |
| 7875 | .selection_control = .{}, | 8288 | .selection_control = .{}, |
| 7876 | }); | 8289 | }); |
| 7877 | const exit_block = cg.module.allocId(); | 8290 | const exit_block = cg.allocId(); |
| 7878 | try cg.body.emit(gpa, .OpBranchConditional, .{ | 8291 | try cg.body.emit(gpa, .OpBranchConditional, .{ |
| 7879 | .condition = is_dispatch, | 8292 | .condition = is_dispatch, |
| 7880 | .true_label = dispatch_check_merge, | 8293 | .true_label = dispatch_check_merge, |
| ... | @@ -7906,25 +8319,30 @@ fn airSwitchDispatch(cg: *CodeGen, inst: Air.Inst.Index) !void { | ... | @@ -7906,25 +8319,30 @@ fn airSwitchDispatch(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7906 | } | 8319 | } |
| 7907 | | 8320 | |
| 7908 | fn airUnreach(cg: *CodeGen) !void { | 8321 | fn airUnreach(cg: *CodeGen) !void { |
| 7909 | try cg.body.emit(cg.module.gpa, .OpUnreachable, {}); | 8322 | try cg.body.emit(cg.gpa, .OpUnreachable, {}); |
| 7910 | } | 8323 | } |
| 7911 | | 8324 | |
| 7912 | fn airDbgStmt(cg: *CodeGen, inst: Air.Inst.Index) !void { | 8325 | fn airDbgStmt(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7913 | const zcu = cg.module.zcu; | 8326 | const zcu = cg.zcu; |
| 7914 | const dbg_stmt = cg.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; | 8327 | const dbg_stmt = cg.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 7915 | const path = zcu.navFileScope(cg.owner_nav).sub_file_path; | 8328 | const path = zcu.navFileScope(cg.owner_nav).sub_file_path; |
| 7916 | | 8329 | |
| 7917 | if (zcu.comp.config.root_strip) return; | 8330 | if (zcu.comp.config.root_strip) return; |
| 7918 | | 8331 | |
| 7919 | try cg.body.emit(cg.module.gpa, .OpLine, .{ | 8332 | const path_id = cg.allocId(); |
| 7920 | .file = try cg.module.debugString(path), | 8333 | try cg.sections.debug_strings.emit(cg.gpa, .OpString, .{ |
| | 8334 | .id_result = path_id, |
| | 8335 | .string = path, |
| | 8336 | }); |
| | 8337 | try cg.body.emit(cg.gpa, .OpLine, .{ |
| | 8338 | .file = path_id, |
| 7921 | .line = cg.base_line + dbg_stmt.line + 1, | 8339 | .line = cg.base_line + dbg_stmt.line + 1, |
| 7922 | .column = dbg_stmt.column + 1, | 8340 | .column = dbg_stmt.column + 1, |
| 7923 | }); | 8341 | }); |
| 7924 | } | 8342 | } |
| 7925 | | 8343 | |
| 7926 | fn airDbgInlineBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 8344 | fn airDbgInlineBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7927 | const zcu = cg.module.zcu; | 8345 | const zcu = cg.zcu; |
| 7928 | const block = cg.air.unwrapDbgBlock(inst); | 8346 | const block = cg.air.unwrapDbgBlock(inst); |
| 7929 | const old_base_line = cg.base_line; | 8347 | const old_base_line = cg.base_line; |
| 7930 | defer cg.base_line = old_base_line; | 8348 | defer cg.base_line = old_base_line; |
| ... | @@ -7934,15 +8352,17 @@ fn airDbgInlineBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7934,15 +8352,17 @@ fn airDbgInlineBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7934 | | 8352 | |
| 7935 | fn airDbgVar(cg: *CodeGen, inst: Air.Inst.Index) !void { | 8353 | fn airDbgVar(cg: *CodeGen, inst: Air.Inst.Index) !void { |
| 7936 | const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 8354 | const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7937 | const target_id = try cg.resolve(pl_op.operand); | 8355 | const target_id = switch (try cg.resolvePtr(pl_op.operand)) { |
| 7938 | if (cg.virtual_allocas.contains(target_id)) return; | 8356 | .tracked => return, |
| | 8357 | .id => |id| id, |
| | 8358 | }; |
| 7939 | const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); | 8359 | const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); |
| 7940 | try cg.module.debugName(target_id, name.toSlice(cg.air)); | 8360 | try cg.debugName(target_id, name.toSlice(cg.air)); |
| 7941 | } | 8361 | } |
| 7942 | | 8362 | |
| 7943 | fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | 8363 | fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7944 | const gpa = cg.module.gpa; | 8364 | const gpa = cg.gpa; |
| 7945 | const zcu = cg.module.zcu; | 8365 | const zcu = cg.zcu; |
| 7946 | const unwrapped_asm = cg.air.unwrapAsm(inst); | 8366 | const unwrapped_asm = cg.air.unwrapAsm(inst); |
| 7947 | | 8367 | |
| 7948 | const is_volatile = unwrapped_asm.is_volatile; | 8368 | const is_volatile = unwrapped_asm.is_volatile; |
| ... | @@ -7974,7 +8394,6 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -7974,7 +8394,6 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 7974 | return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{}); | 8394 | return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{}); |
| 7975 | }); | 8395 | }); |
| 7976 | | 8396 | |
| 7977 | // TODO: This entire function should be handled a bit better... | | |
| 7978 | const ip = &zcu.intern_pool; | 8397 | const ip = &zcu.intern_pool; |
| 7979 | switch (ip.indexToKey(val.toIntern())) { | 8398 | switch (ip.indexToKey(val.toIntern())) { |
| 7980 | .int_type, | 8399 | .int_type, |
| ... | @@ -8080,8 +8499,8 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -8080,8 +8499,8 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 8080 | fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) !?Id { | 8499 | fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) !?Id { |
| 8081 | _ = modifier; | 8500 | _ = modifier; |
| 8082 | | 8501 | |
| 8083 | const gpa = cg.module.gpa; | 8502 | const gpa = cg.gpa; |
| 8084 | const zcu = cg.module.zcu; | 8503 | const zcu = cg.zcu; |
| 8085 | const air_call = cg.air.unwrapCall(inst); | 8504 | const air_call = cg.air.unwrapCall(inst); |
| 8086 | const args = air_call.args; | 8505 | const args = air_call.args; |
| 8087 | const callee_ty = cg.typeOf(air_call.callee); | 8506 | const callee_ty = cg.typeOf(air_call.callee); |
| ... | @@ -8094,11 +8513,9 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) | ... | @@ -8094,11 +8513,9 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) |
| 8094 | const return_type = fn_info.return_type; | 8513 | const return_type = fn_info.return_type; |
| 8095 | | 8514 | |
| 8096 | const result_type_id = try cg.resolveFnReturnType(.fromInterned(return_type)); | 8515 | const result_type_id = try cg.resolveFnReturnType(.fromInterned(return_type)); |
| 8097 | const result_id = cg.module.allocId(); | 8516 | const result_id = cg.allocId(); |
| 8098 | const callee_id = try cg.resolve(air_call.callee); | 8517 | const callee_id = try cg.resolve(air_call.callee); |
| 8099 | | 8518 | |
| 8100 | comptime assert(zig_call_abi_ver == 3); | | |
| 8101 | | | |
| 8102 | const scratch_top = cg.id_scratch.items.len; | 8519 | const scratch_top = cg.id_scratch.items.len; |
| 8103 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); | 8520 | defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); |
| 8104 | const params = try cg.id_scratch.addManyAsSlice(gpa, args.len); | 8521 | const params = try cg.id_scratch.addManyAsSlice(gpa, args.len); |
| ... | @@ -8112,7 +8529,8 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) | ... | @@ -8112,7 +8529,8 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier) |
| 8112 | if (!arg_ty.hasRuntimeBits(zcu)) continue; | 8529 | if (!arg_ty.hasRuntimeBits(zcu)) continue; |
| 8113 | | 8530 | |
| 8114 | if (arg_ty.zigTypeTag(zcu) == .pointer and !arg_ty.isSlice(zcu) and | 8531 | if (arg_ty.zigTypeTag(zcu) == .pointer and !arg_ty.isSlice(zcu) and |
| 8115 | !arg_ty.childType(zcu).hasRuntimeBits(zcu)) | 8532 | !arg_ty.childType(zcu).hasRuntimeBits(zcu) and |
| | 8533 | cg.storageClass(arg_ty.ptrAddressSpace(zcu)) == .function) |
| 8116 | { | 8534 | { |
| 8117 | // in logical addressing, pointer arguments to function calls | 8535 | // in logical addressing, pointer arguments to function calls |
| 8118 | // must be memory object declarations (OpVariable). for pointers to | 8536 | // must be memory object declarations (OpVariable). for pointers to |
| ... | @@ -8147,15 +8565,26 @@ fn builtin3D( | ... | @@ -8147,15 +8565,26 @@ fn builtin3D( |
| 8147 | dimension: u32, | 8565 | dimension: u32, |
| 8148 | out_of_range_value: anytype, | 8566 | out_of_range_value: anytype, |
| 8149 | ) !Id { | 8567 | ) !Id { |
| 8150 | const gpa = cg.module.gpa; | 8568 | const gpa = cg.gpa; |
| 8151 | if (dimension >= 3) return try cg.constInt(result_ty, out_of_range_value); | 8569 | if (dimension >= 3) return try cg.constInt(result_ty, out_of_range_value); |
| 8152 | const u32_ty_id = try cg.module.intType(.unsigned, 32); | 8570 | const u32_ty_id = try cg.intType(.unsigned, 32); |
| 8153 | const vec_ty_id = try cg.module.vectorType(3, u32_ty_id); | 8571 | const vec_ty_id = try cg.vectorType(3, u32_ty_id); |
| 8154 | const ptr_ty_id = try cg.module.ptrType(vec_ty_id, .input); | 8572 | const ptr_ty_id = try cg.ptrType(vec_ty_id, .input); |
| 8155 | const spv_decl_index = try cg.module.builtin(ptr_ty_id, built_in, .input); | 8573 | const builtins_gop = try cg.builtins.getOrPut(gpa, .{ built_in, .input }); |
| 8156 | try cg.module.decl_deps.append(gpa, spv_decl_index); | 8574 | if (!builtins_gop.found_existing) { |
| 8157 | const ptr_id = cg.module.declPtr(spv_decl_index).result_id; | 8575 | builtins_gop.value_ptr.* = try cg.allocDecl(.global); |
| 8158 | const vec_id = cg.module.allocId(); | 8576 | const decl = cg.declPtr(builtins_gop.value_ptr.*); |
| | 8577 | try cg.sections.globals.emit(gpa, .OpVariable, .{ |
| | 8578 | .id_result_type = ptr_ty_id, |
| | 8579 | .id_result = decl.result_id, |
| | 8580 | .storage_class = .input, |
| | 8581 | }); |
| | 8582 | try cg.decorate(decl.result_id, .{ .built_in = .{ .built_in = built_in } }); |
| | 8583 | } |
| | 8584 | const spv_decl_index = builtins_gop.value_ptr.*; |
| | 8585 | try cg.decl_deps.append(gpa, spv_decl_index); |
| | 8586 | const ptr_id = cg.declPtr(spv_decl_index).result_id; |
| | 8587 | const vec_id = cg.allocId(); |
| 8159 | try cg.body.emit(gpa, .OpLoad, .{ | 8588 | try cg.body.emit(gpa, .OpLoad, .{ |
| 8160 | .id_result_type = vec_ty_id, | 8589 | .id_result_type = vec_ty_id, |
| 8161 | .id_result = vec_id, | 8590 | .id_result = vec_id, |
| ... | @@ -8186,12 +8615,30 @@ fn airWorkGroupId(cg: *CodeGen, inst: Air.Inst.Index) !?Id { | ... | @@ -8186,12 +8615,30 @@ fn airWorkGroupId(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 8186 | return try cg.builtin3D(.u32, .workgroup_id, dimension, 0); | 8615 | return try cg.builtin3D(.u32, .workgroup_id, dimension, 0); |
| 8187 | } | 8616 | } |
| 8188 | | 8617 | |
| 8189 | fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type { | 8618 | const std = @import("std"); |
| 8190 | const zcu = cg.module.zcu; | 8619 | const Allocator = std.mem.Allocator; |
| 8191 | return cg.air.typeOf(inst, &zcu.intern_pool); | 8620 | const Target = std.Target; |
| 8192 | } | 8621 | const Signedness = std.lang.Signedness; |
| | 8622 | const assert = std.debug.assert; |
| | 8623 | const log = std.log.scoped(.codegen); |
| 8193 | | 8624 | |
| 8194 | fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type { | 8625 | const builtin = @import("builtin"); |
| 8195 | const zcu = cg.module.zcu; | 8626 | const link = @import("../../link.zig"); |
| 8196 | return cg.air.typeOfIndex(inst, &zcu.intern_pool); | 8627 | const codegen = @import("../../codegen.zig"); |
| 8197 | } | 8628 | const Zcu = @import("../../Zcu.zig"); |
| | 8629 | const Type = @import("../../Type.zig"); |
| | 8630 | const Value = @import("../../Value.zig"); |
| | 8631 | const Air = @import("../../Air.zig"); |
| | 8632 | const InternPool = @import("../../InternPool.zig"); |
| | 8633 | const Section = @import("Section.zig"); |
| | 8634 | const Assembler = @import("Assembler.zig"); |
| | 8635 | const Mir = @import("Mir.zig"); |
| | 8636 | |
| | 8637 | const spec = @import("spec.zig"); |
| | 8638 | const Opcode = spec.Opcode; |
| | 8639 | const Word = spec.Word; |
| | 8640 | const Id = spec.Id; |
| | 8641 | const IdRange = spec.IdRange; |
| | 8642 | const StorageClass = spec.StorageClass; |
| | 8643 | |
| | 8644 | const CodeGen = @This(); |