| ... | @@ -50,32 +50,39 @@ pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features { | ... | @@ -50,32 +50,39 @@ pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features { |
| 50 | /// * The types used, so declarations can be emitted in `flush` | 50 | /// * The types used, so declarations can be emitted in `flush` |
| 51 | /// * The lazy functions used, so definitions can be emitted in `flush` | 51 | /// * The lazy functions used, so definitions can be emitted in `flush` |
| 52 | pub const Mir = struct { | 52 | pub const Mir = struct { |
| | 53 | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. |
| | 54 | fwd_decl: []u8, |
| | 55 | code_header: []u8, |
| | 56 | code: []u8, |
| 53 | /// This map contains all the UAVs we saw generating this function. | 57 | /// This map contains all the UAVs we saw generating this function. |
| 54 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. | 58 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. |
| 55 | /// Key is the value of the UAV; value is the UAV's alignment, or | 59 | /// Key is the value of the UAV; value is the UAV's alignment, or |
| 56 | /// `.none` for natural alignment. The specified alignment is never | 60 | /// `.none` for natural alignment. The specified alignment is never |
| 57 | /// less than the natural alignment. | 61 | /// less than the natural alignment. |
| 58 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), | 62 | need_uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 59 | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. | 63 | ctype_deps: CType.Dependencies, |
| 60 | code_header: []u8, | 64 | /// Key is an enum type for which we need a generated `@tagName` function. |
| 61 | code: []u8, | 65 | need_tag_name_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Index, void), |
| 62 | fwd_decl: []u8, | 66 | /// Key is a function Nav for which we need a generated `zig_never_tail` wrapper. |
| 63 | ctype_pool: CType.Pool, | 67 | need_never_tail_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void), |
| 64 | lazy_fns: LazyFnMap, | 68 | /// Key is a function Nav for which we need a generated `zig_never_inline` wrapper. |
| | 69 | need_never_inline_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void), |
| 65 | | 70 | |
| 66 | pub fn deinit(mir: *Mir, gpa: Allocator) void { | 71 | pub fn deinit(mir: *Mir, gpa: Allocator) void { |
| 67 | mir.uavs.deinit(gpa); | 72 | gpa.free(mir.fwd_decl); |
| 68 | gpa.free(mir.code_header); | 73 | gpa.free(mir.code_header); |
| 69 | gpa.free(mir.code); | 74 | gpa.free(mir.code); |
| 70 | gpa.free(mir.fwd_decl); | 75 | mir.need_uavs.deinit(gpa); |
| 71 | mir.ctype_pool.deinit(gpa); | 76 | mir.ctype_deps.deinit(gpa); |
| 72 | mir.lazy_fns.deinit(gpa); | 77 | mir.need_tag_name_funcs.deinit(gpa); |
| | 78 | mir.need_never_tail_funcs.deinit(gpa); |
| | 79 | mir.need_never_inline_funcs.deinit(gpa); |
| 73 | } | 80 | } |
| 74 | }; | 81 | }; |
| 75 | | 82 | |
| 76 | pub const Error = Writer.Error || std.mem.Allocator.Error || error{AnalysisFail}; | 83 | pub const Error = Writer.Error || Allocator.Error || error{AnalysisFail}; |
| 77 | | 84 | |
| 78 | pub const CType = @import("c/Type.zig"); | 85 | pub const CType = @import("c/type.zig").CType; |
| 79 | | 86 | |
| 80 | pub const CValue = union(enum) { | 87 | pub const CValue = union(enum) { |
| 81 | none: void, | 88 | none: void, |
| ... | @@ -87,8 +94,6 @@ pub const CValue = union(enum) { | ... | @@ -87,8 +94,6 @@ pub const CValue = union(enum) { |
| 87 | constant: Value, | 94 | constant: Value, |
| 88 | /// Index into the parameters | 95 | /// Index into the parameters |
| 89 | arg: usize, | 96 | arg: usize, |
| 90 | /// The array field of a parameter | | |
| 91 | arg_array: usize, | | |
| 92 | /// Index into a tuple's fields | 97 | /// Index into a tuple's fields |
| 93 | field: usize, | 98 | field: usize, |
| 94 | /// By-value | 99 | /// By-value |
| ... | @@ -100,8 +105,6 @@ pub const CValue = union(enum) { | ... | @@ -100,8 +105,6 @@ pub const CValue = union(enum) { |
| 100 | identifier: []const u8, | 105 | identifier: []const u8, |
| 101 | /// Rendered as "payload." followed by as identifier (using fmtIdent) | 106 | /// Rendered as "payload." followed by as identifier (using fmtIdent) |
| 102 | payload_identifier: []const u8, | 107 | payload_identifier: []const u8, |
| 103 | /// Rendered with fmtCTypePoolString | | |
| 104 | ctype_pool_string: CType.Pool.String, | | |
| 105 | | 108 | |
| 106 | fn eql(lhs: CValue, rhs: CValue) bool { | 109 | fn eql(lhs: CValue, rhs: CValue) bool { |
| 107 | return switch (lhs) { | 110 | return switch (lhs) { |
| ... | @@ -122,10 +125,6 @@ pub const CValue = union(enum) { | ... | @@ -122,10 +125,6 @@ pub const CValue = union(enum) { |
| 122 | .arg => |rhs_arg_index| lhs_arg_index == rhs_arg_index, | 125 | .arg => |rhs_arg_index| lhs_arg_index == rhs_arg_index, |
| 123 | else => false, | 126 | else => false, |
| 124 | }, | 127 | }, |
| 125 | .arg_array => |lhs_arg_index| switch (rhs) { | | |
| 126 | .arg_array => |rhs_arg_index| lhs_arg_index == rhs_arg_index, | | |
| 127 | else => false, | | |
| 128 | }, | | |
| 129 | .field => |lhs_field_index| switch (rhs) { | 128 | .field => |lhs_field_index| switch (rhs) { |
| 130 | .field => |rhs_field_index| lhs_field_index == rhs_field_index, | 129 | .field => |rhs_field_index| lhs_field_index == rhs_field_index, |
| 131 | else => false, | 130 | else => false, |
| ... | @@ -150,10 +149,6 @@ pub const CValue = union(enum) { | ... | @@ -150,10 +149,6 @@ pub const CValue = union(enum) { |
| 150 | .payload_identifier => |rhs_id| std.mem.eql(u8, lhs_id, rhs_id), | 149 | .payload_identifier => |rhs_id| std.mem.eql(u8, lhs_id, rhs_id), |
| 151 | else => false, | 150 | else => false, |
| 152 | }, | 151 | }, |
| 153 | .ctype_pool_string => |lhs_str| switch (rhs) { | | |
| 154 | .ctype_pool_string => |rhs_str| lhs_str.index == rhs_str.index, | | |
| 155 | else => false, | | |
| 156 | }, | | |
| 157 | }; | 152 | }; |
| 158 | } | 153 | } |
| 159 | }; | 154 | }; |
| ... | @@ -163,53 +158,24 @@ const BlockData = struct { | ... | @@ -163,53 +158,24 @@ const BlockData = struct { |
| 163 | result: CValue, | 158 | result: CValue, |
| 164 | }; | 159 | }; |
| 165 | | 160 | |
| 166 | pub const CValueMap = std.AutoHashMap(Air.Inst.Ref, CValue); | 161 | const LocalType = struct { |
| 167 | | 162 | type: Type, |
| 168 | pub const LazyFnKey = union(enum) { | 163 | alignment: Alignment, |
| 169 | tag_name: InternPool.Index, | | |
| 170 | never_tail: InternPool.Nav.Index, | | |
| 171 | never_inline: InternPool.Nav.Index, | | |
| 172 | }; | | |
| 173 | pub const LazyFnValue = struct { | | |
| 174 | fn_name: CType.Pool.String, | | |
| 175 | }; | | |
| 176 | pub const LazyFnMap = std.AutoArrayHashMapUnmanaged(LazyFnKey, LazyFnValue); | | |
| 177 | | | |
| 178 | const Local = struct { | | |
| 179 | ctype: CType, | | |
| 180 | flags: packed struct(u32) { | | |
| 181 | alignas: CType.AlignAs, | | |
| 182 | _: u20 = undefined, | | |
| 183 | }, | | |
| 184 | | | |
| 185 | fn getType(local: Local) LocalType { | | |
| 186 | return .{ .ctype = local.ctype, .alignas = local.flags.alignas }; | | |
| 187 | } | | |
| 188 | }; | 164 | }; |
| 189 | | 165 | |
| 190 | const LocalIndex = u16; | 166 | const LocalIndex = u16; |
| 191 | const LocalType = struct { ctype: CType, alignas: CType.AlignAs }; | | |
| 192 | const LocalsList = std.AutoArrayHashMapUnmanaged(LocalIndex, void); | 167 | const LocalsList = std.AutoArrayHashMapUnmanaged(LocalIndex, void); |
| 193 | const LocalsMap = std.AutoArrayHashMapUnmanaged(LocalType, LocalsList); | 168 | const LocalsMap = std.AutoArrayHashMapUnmanaged(LocalType, LocalsList); |
| 194 | | 169 | |
| 195 | const ValueRenderLocation = enum { | 170 | const ValueRenderLocation = enum { |
| 196 | FunctionArgument, | 171 | initializer, |
| 197 | Initializer, | 172 | static_initializer, |
| 198 | StaticInitializer, | 173 | other, |
| 199 | Other, | | |
| 200 | | 174 | |
| 201 | fn isInitializer(loc: ValueRenderLocation) bool { | 175 | fn isInitializer(loc: ValueRenderLocation) bool { |
| 202 | return switch (loc) { | 176 | return switch (loc) { |
| 203 | .Initializer, .StaticInitializer => true, | 177 | .initializer, .static_initializer => true, |
| 204 | else => false, | 178 | .other => false, |
| 205 | }; | | |
| 206 | } | | |
| 207 | | | |
| 208 | fn toCTypeKind(loc: ValueRenderLocation) CType.Kind { | | |
| 209 | return switch (loc) { | | |
| 210 | .FunctionArgument => .parameter, | | |
| 211 | .Initializer, .Other => .complete, | | |
| 212 | .StaticInitializer => .global, | | |
| 213 | }; | 179 | }; |
| 214 | } | 180 | } |
| 215 | }; | 181 | }; |
| ... | @@ -334,16 +300,31 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{ | ... | @@ -334,16 +300,31 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{ |
| 334 | }); | 300 | }); |
| 335 | | 301 | |
| 336 | fn isReservedIdent(ident: []const u8) bool { | 302 | fn isReservedIdent(ident: []const u8) bool { |
| 337 | if (ident.len >= 2 and ident[0] == '_') { // C language | 303 | // C language |
| | 304 | if (ident.len >= 2 and ident[0] == '_') { |
| 338 | switch (ident[1]) { | 305 | switch (ident[1]) { |
| 339 | 'A'...'Z', '_' => return true, | 306 | 'A'...'Z', '_' => return true, |
| 340 | else => return false, | 307 | else => {}, |
| 341 | } | 308 | } |
| 342 | } else if (mem.startsWith(u8, ident, "DUMMYSTRUCTNAME") or | 309 | } |
| | 310 | |
| | 311 | // windows.h |
| | 312 | if (mem.startsWith(u8, ident, "DUMMYSTRUCTNAME") or |
| 343 | mem.startsWith(u8, ident, "DUMMYUNIONNAME")) | 313 | mem.startsWith(u8, ident, "DUMMYUNIONNAME")) |
| 344 | { // windows.h | 314 | { |
| | 315 | return true; |
| | 316 | } |
| | 317 | |
| | 318 | // CType |
| | 319 | if (mem.startsWith(u8, ident, "enum__") or |
| | 320 | mem.startsWith(u8, ident, "bitpack__") or |
| | 321 | mem.startsWith(u8, ident, "aligned__") or |
| | 322 | mem.startsWith(u8, ident, "fn__")) |
| | 323 | { |
| 345 | return true; | 324 | return true; |
| 346 | } else return reserved_idents.has(ident); | 325 | } |
| | 326 | |
| | 327 | return reserved_idents.has(ident); |
| 347 | } | 328 | } |
| 348 | | 329 | |
| 349 | fn formatIdentSolo(ident: []const u8, w: *Writer) Writer.Error!void { | 330 | fn formatIdentSolo(ident: []const u8, w: *Writer) Writer.Error!void { |
| ... | @@ -361,7 +342,7 @@ fn formatIdentOptions(ident: []const u8, w: *Writer, solo: bool) Writer.Error!vo | ... | @@ -361,7 +342,7 @@ fn formatIdentOptions(ident: []const u8, w: *Writer, solo: bool) Writer.Error!vo |
| 361 | for (ident, 0..) |c, i| { | 342 | for (ident, 0..) |c, i| { |
| 362 | switch (c) { | 343 | switch (c) { |
| 363 | 'a'...'z', 'A'...'Z', '_' => try w.writeByte(c), | 344 | 'a'...'z', 'A'...'Z', '_' => try w.writeByte(c), |
| 364 | '.' => try w.writeByte('_'), | 345 | '.', ' ' => try w.writeByte('_'), |
| 365 | '0'...'9' => if (i == 0) { | 346 | '0'...'9' => if (i == 0) { |
| 366 | try w.print("_{x:2}", .{c}); | 347 | try w.print("_{x:2}", .{c}); |
| 367 | } else { | 348 | } else { |
| ... | @@ -380,29 +361,6 @@ pub fn fmtIdentUnsolo(ident: []const u8) std.fmt.Alt([]const u8, formatIdentUnso | ... | @@ -380,29 +361,6 @@ pub fn fmtIdentUnsolo(ident: []const u8) std.fmt.Alt([]const u8, formatIdentUnso |
| 380 | return .{ .data = ident }; | 361 | return .{ .data = ident }; |
| 381 | } | 362 | } |
| 382 | | 363 | |
| 383 | const CTypePoolStringFormatData = struct { | | |
| 384 | ctype_pool_string: CType.Pool.String, | | |
| 385 | ctype_pool: *const CType.Pool, | | |
| 386 | solo: bool, | | |
| 387 | }; | | |
| 388 | fn formatCTypePoolString(data: CTypePoolStringFormatData, w: *Writer) Writer.Error!void { | | |
| 389 | if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice| | | |
| 390 | try formatIdentOptions(slice, w, data.solo) | | |
| 391 | else | | |
| 392 | try w.print("{f}", .{data.ctype_pool_string.fmt(data.ctype_pool)}); | | |
| 393 | } | | |
| 394 | pub fn fmtCTypePoolString( | | |
| 395 | ctype_pool_string: CType.Pool.String, | | |
| 396 | ctype_pool: *const CType.Pool, | | |
| 397 | solo: bool, | | |
| 398 | ) std.fmt.Alt(CTypePoolStringFormatData, formatCTypePoolString) { | | |
| 399 | return .{ .data = .{ | | |
| 400 | .ctype_pool_string = ctype_pool_string, | | |
| 401 | .ctype_pool = ctype_pool, | | |
| 402 | .solo = solo, | | |
| 403 | } }; | | |
| 404 | } | | |
| 405 | | | |
| 406 | // Returns true if `formatIdent` would make any edits to ident. | 364 | // Returns true if `formatIdent` would make any edits to ident. |
| 407 | // This must be kept in sync with `formatIdent`. | 365 | // This must be kept in sync with `formatIdent`. |
| 408 | pub fn isMangledIdent(ident: []const u8, solo: bool) bool { | 366 | pub fn isMangledIdent(ident: []const u8, solo: bool) bool { |
| ... | @@ -417,21 +375,26 @@ pub fn isMangledIdent(ident: []const u8, solo: bool) bool { | ... | @@ -417,21 +375,26 @@ pub fn isMangledIdent(ident: []const u8, solo: bool) bool { |
| 417 | return false; | 375 | return false; |
| 418 | } | 376 | } |
| 419 | | 377 | |
| 420 | /// This data is available when outputting .c code for a `InternPool.Index` | 378 | /// This data is available when rendering C source code for an interned function. |
| 421 | /// that corresponds to `func`. | | |
| 422 | /// It is not available when generating .h file. | | |
| 423 | pub const Function = struct { | 379 | pub const Function = struct { |
| 424 | air: Air, | 380 | air: Air, |
| 425 | liveness: Air.Liveness, | 381 | liveness: Air.Liveness, |
| 426 | value_map: CValueMap, | 382 | value_map: std.AutoHashMap(Air.Inst.Ref, CValue), |
| 427 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .empty, | 383 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .empty, |
| 428 | next_arg_index: u32 = 0, | 384 | next_arg_index: u32 = 0, |
| 429 | next_block_index: u32 = 0, | 385 | next_block_index: u32 = 0, |
| 430 | object: Object, | 386 | dg: DeclGen, |
| 431 | lazy_fns: LazyFnMap, | 387 | code: Writer.Allocating, |
| | 388 | indent_counter: usize, |
| | 389 | /// Key is an enum type for which we need a generated `@tagName` function. |
| | 390 | need_tag_name_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Index, void), |
| | 391 | /// Key is a function Nav for which we need a generated `zig_never_tail` wrapper. |
| | 392 | need_never_tail_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void), |
| | 393 | /// Key is a function Nav for which we need a generated `zig_never_inline` wrapper. |
| | 394 | need_never_inline_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void), |
| 432 | func_index: InternPool.Index, | 395 | func_index: InternPool.Index, |
| 433 | /// All the locals, to be emitted at the top of the function. | 396 | /// All the locals, to be emitted at the top of the function. |
| 434 | locals: std.ArrayList(Local) = .empty, | 397 | locals: std.ArrayList(LocalType) = .empty, |
| 435 | /// Which locals are available for reuse, based on Type. | 398 | /// Which locals are available for reuse, based on Type. |
| 436 | free_locals_map: LocalsMap = .{}, | 399 | free_locals_map: LocalsMap = .{}, |
| 437 | /// Locals which will not be freed by Liveness. This is used after a | 400 | /// Locals which will not be freed by Liveness. This is used after a |
| ... | @@ -445,37 +408,41 @@ pub const Function = struct { | ... | @@ -445,37 +408,41 @@ pub const Function = struct { |
| 445 | /// for the switch cond. Dispatches should set this local to the new cond. | 408 | /// for the switch cond. Dispatches should set this local to the new cond. |
| 446 | loop_switch_conds: std.AutoHashMapUnmanaged(Air.Inst.Index, LocalIndex) = .empty, | 409 | loop_switch_conds: std.AutoHashMapUnmanaged(Air.Inst.Index, LocalIndex) = .empty, |
| 447 | | 410 | |
| | 411 | const indent_width = 1; |
| | 412 | const indent_char = ' '; |
| | 413 | |
| | 414 | fn newline(f: *Function) !void { |
| | 415 | const w = &f.code.writer; |
| | 416 | try w.writeByte('\n'); |
| | 417 | try w.splatByteAll(indent_char, f.indent_counter); |
| | 418 | } |
| | 419 | fn indent(f: *Function) void { |
| | 420 | f.indent_counter += indent_width; |
| | 421 | } |
| | 422 | fn outdent(f: *Function) !void { |
| | 423 | f.indent_counter -= indent_width; |
| | 424 | const written = f.code.written(); |
| | 425 | switch (written[written.len - 1]) { |
| | 426 | indent_char => f.code.shrinkRetainingCapacity(written.len - indent_width), |
| | 427 | '\n' => try f.code.writer.splatByteAll(indent_char, f.indent_counter), |
| | 428 | else => { |
| | 429 | std.debug.print("\"{f}\"\n", .{std.zig.fmtString(written[written.len -| 100..])}); |
| | 430 | unreachable; |
| | 431 | }, |
| | 432 | } |
| | 433 | } |
| | 434 | |
| 448 | fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue { | 435 | fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue { |
| 449 | const gop = try f.value_map.getOrPut(ref); | 436 | const gop = try f.value_map.getOrPut(ref); |
| 450 | if (gop.found_existing) return gop.value_ptr.*; | 437 | if (!gop.found_existing) { |
| 451 | | 438 | const val = try f.air.value(ref, f.dg.pt); |
| 452 | const pt = f.object.dg.pt; | 439 | gop.value_ptr.* = .{ .constant = val.? }; |
| 453 | const zcu = pt.zcu; | 440 | } |
| 454 | const val = (try f.air.value(ref, pt)).?; | 441 | return gop.value_ptr.*; |
| 455 | const ty = f.typeOf(ref); | | |
| 456 | | | |
| 457 | const result: CValue = if (lowersToArray(ty, zcu)) result: { | | |
| 458 | const ch = &f.object.code_header.writer; | | |
| 459 | const decl_c_value = try f.allocLocalValue(.{ | | |
| 460 | .ctype = try f.ctypeFromType(ty, .complete), | | |
| 461 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(zcu)), | | |
| 462 | }); | | |
| 463 | const gpa = f.object.dg.gpa; | | |
| 464 | try f.allocs.put(gpa, decl_c_value.new_local, false); | | |
| 465 | try ch.writeAll("static "); | | |
| 466 | try f.object.dg.renderTypeAndName(ch, ty, decl_c_value, Const, .none, .complete); | | |
| 467 | try ch.writeAll(" = "); | | |
| 468 | try f.object.dg.renderValue(ch, val, .StaticInitializer); | | |
| 469 | try ch.writeAll(";\n "); | | |
| 470 | break :result .{ .local = decl_c_value.new_local }; | | |
| 471 | } else .{ .constant = val }; | | |
| 472 | | | |
| 473 | gop.value_ptr.* = result; | | |
| 474 | return result; | | |
| 475 | } | 442 | } |
| 476 | | 443 | |
| 477 | fn wantSafety(f: *Function) bool { | 444 | fn wantSafety(f: *Function) bool { |
| 478 | return switch (f.object.dg.pt.zcu.optimizeMode()) { | 445 | return switch (f.dg.pt.zcu.optimizeMode()) { |
| 479 | .Debug, .ReleaseSafe => true, | 446 | .Debug, .ReleaseSafe => true, |
| 480 | .ReleaseFast, .ReleaseSmall => false, | 447 | .ReleaseFast, .ReleaseSmall => false, |
| 481 | }; | 448 | }; |
| ... | @@ -485,18 +452,16 @@ pub const Function = struct { | ... | @@ -485,18 +452,16 @@ pub const Function = struct { |
| 485 | /// those which go into `allocs`. This function does not add the resulting local into `allocs`; | 452 | /// those which go into `allocs`. This function does not add the resulting local into `allocs`; |
| 486 | /// that responsibility lies with the caller. | 453 | /// that responsibility lies with the caller. |
| 487 | fn allocLocalValue(f: *Function, local_type: LocalType) !CValue { | 454 | fn allocLocalValue(f: *Function, local_type: LocalType) !CValue { |
| 488 | try f.locals.ensureUnusedCapacity(f.object.dg.gpa, 1); | 455 | try f.locals.ensureUnusedCapacity(f.dg.gpa, 1); |
| 489 | defer f.locals.appendAssumeCapacity(.{ | 456 | const index = f.locals.items.len; |
| 490 | .ctype = local_type.ctype, | 457 | f.locals.appendAssumeCapacity(local_type); |
| 491 | .flags = .{ .alignas = local_type.alignas }, | 458 | return .{ .new_local = @intCast(index) }; |
| 492 | }); | | |
| 493 | return .{ .new_local = @intCast(f.locals.items.len) }; | | |
| 494 | } | 459 | } |
| 495 | | 460 | |
| 496 | fn allocLocal(f: *Function, inst: ?Air.Inst.Index, ty: Type) !CValue { | 461 | fn allocLocal(f: *Function, inst: ?Air.Inst.Index, ty: Type) !CValue { |
| 497 | return f.allocAlignedLocal(inst, .{ | 462 | return f.allocAlignedLocal(inst, .{ |
| 498 | .ctype = try f.ctypeFromType(ty, .complete), | 463 | .type = ty, |
| 499 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(f.object.dg.pt.zcu)), | 464 | .alignment = .none, |
| 500 | }); | 465 | }); |
| 501 | } | 466 | } |
| 502 | | 467 | |
| ... | @@ -524,11 +489,10 @@ pub const Function = struct { | ... | @@ -524,11 +489,10 @@ pub const Function = struct { |
| 524 | .none => unreachable, | 489 | .none => unreachable, |
| 525 | .new_local, .local => |i| try w.print("t{d}", .{i}), | 490 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| 526 | .local_ref => |i| try w.print("&t{d}", .{i}), | 491 | .local_ref => |i| try w.print("&t{d}", .{i}), |
| 527 | .constant => |val| try f.object.dg.renderValue(w, val, location), | 492 | .constant => |val| try f.dg.renderValue(w, val, location), |
| 528 | .arg => |i| try w.print("a{d}", .{i}), | 493 | .arg => |i| try w.print("a{d}", .{i}), |
| 529 | .arg_array => |i| try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }), | 494 | .undef => |ty| try f.dg.renderUndefValue(w, ty, location), |
| 530 | .undef => |ty| try f.object.dg.renderUndefValue(w, ty, location), | 495 | else => try f.dg.writeCValue(w, c_value), |
| 531 | else => try f.object.dg.writeCValue(w, c_value), | | |
| 532 | } | 496 | } |
| 533 | } | 497 | } |
| 534 | | 498 | |
| ... | @@ -537,17 +501,12 @@ pub const Function = struct { | ... | @@ -537,17 +501,12 @@ pub const Function = struct { |
| 537 | .none => unreachable, | 501 | .none => unreachable, |
| 538 | .new_local, .local, .constant => { | 502 | .new_local, .local, .constant => { |
| 539 | try w.writeAll("(*"); | 503 | try w.writeAll("(*"); |
| 540 | try f.writeCValue(w, c_value, .Other); | 504 | try f.writeCValue(w, c_value, .other); |
| 541 | try w.writeByte(')'); | 505 | try w.writeByte(')'); |
| 542 | }, | 506 | }, |
| 543 | .local_ref => |i| try w.print("t{d}", .{i}), | 507 | .local_ref => |i| try w.print("t{d}", .{i}), |
| 544 | .arg => |i| try w.print("(*a{d})", .{i}), | 508 | .arg => |i| try w.print("(*a{d})", .{i}), |
| 545 | .arg_array => |i| { | 509 | else => try f.dg.writeCValueDeref(w, c_value), |
| 546 | try w.writeAll("(*"); | | |
| 547 | try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }); | | |
| 548 | try w.writeByte(')'); | | |
| 549 | }, | | |
| 550 | else => try f.object.dg.writeCValueDeref(w, c_value), | | |
| 551 | } | 510 | } |
| 552 | } | 511 | } |
| 553 | | 512 | |
| ... | @@ -558,119 +517,77 @@ pub const Function = struct { | ... | @@ -558,119 +517,77 @@ pub const Function = struct { |
| 558 | member: CValue, | 517 | member: CValue, |
| 559 | ) Error!void { | 518 | ) Error!void { |
| 560 | switch (c_value) { | 519 | switch (c_value) { |
| 561 | .new_local, .local, .local_ref, .constant, .arg, .arg_array => { | 520 | .new_local, .local, .local_ref, .constant, .arg => { |
| 562 | try f.writeCValue(w, c_value, .Other); | 521 | try f.writeCValue(w, c_value, .other); |
| 563 | try w.writeByte('.'); | 522 | try w.writeByte('.'); |
| 564 | try f.writeCValue(w, member, .Other); | 523 | try f.writeCValue(w, member, .other); |
| 565 | }, | 524 | }, |
| 566 | else => return f.object.dg.writeCValueMember(w, c_value, member), | 525 | else => return f.dg.writeCValueMember(w, c_value, member), |
| 567 | } | 526 | } |
| 568 | } | 527 | } |
| 569 | | 528 | |
| 570 | fn writeCValueDerefMember(f: *Function, w: *Writer, c_value: CValue, member: CValue) !void { | 529 | fn writeCValueDerefMember(f: *Function, w: *Writer, c_value: CValue, member: CValue) !void { |
| 571 | switch (c_value) { | 530 | switch (c_value) { |
| 572 | .new_local, .local, .arg, .arg_array => { | 531 | .new_local, .local, .arg => { |
| 573 | try f.writeCValue(w, c_value, .Other); | 532 | try f.writeCValue(w, c_value, .other); |
| 574 | try w.writeAll("->"); | 533 | try w.writeAll("->"); |
| 575 | }, | 534 | }, |
| 576 | .constant => { | 535 | .constant => { |
| 577 | try w.writeByte('('); | 536 | try w.writeByte('('); |
| 578 | try f.writeCValue(w, c_value, .Other); | 537 | try f.writeCValue(w, c_value, .other); |
| 579 | try w.writeAll(")->"); | 538 | try w.writeAll(")->"); |
| 580 | }, | 539 | }, |
| 581 | .local_ref => { | 540 | .local_ref => { |
| 582 | try f.writeCValueDeref(w, c_value); | 541 | try f.writeCValueDeref(w, c_value); |
| 583 | try w.writeByte('.'); | 542 | try w.writeByte('.'); |
| 584 | }, | 543 | }, |
| 585 | else => return f.object.dg.writeCValueDerefMember(w, c_value, member), | 544 | else => return f.dg.writeCValueDerefMember(w, c_value, member), |
| 586 | } | 545 | } |
| 587 | try f.writeCValue(w, member, .Other); | 546 | try f.writeCValue(w, member, .other); |
| 588 | } | 547 | } |
| 589 | | 548 | |
| 590 | fn fail(f: *Function, comptime format: []const u8, args: anytype) Error { | 549 | fn fail(f: *Function, comptime format: []const u8, args: anytype) Error { |
| 591 | return f.object.dg.fail(format, args); | 550 | return f.dg.fail(format, args); |
| 592 | } | | |
| 593 | | | |
| 594 | fn ctypeFromType(f: *Function, ty: Type, kind: CType.Kind) !CType { | | |
| 595 | return f.object.dg.ctypeFromType(ty, kind); | | |
| 596 | } | | |
| 597 | | | |
| 598 | fn byteSize(f: *Function, ctype: CType) u64 { | | |
| 599 | return f.object.dg.byteSize(ctype); | | |
| 600 | } | | |
| 601 | | | |
| 602 | fn renderType(f: *Function, w: *Writer, ctype: Type) !void { | | |
| 603 | return f.object.dg.renderType(w, ctype); | | |
| 604 | } | 551 | } |
| 605 | | 552 | |
| 606 | fn renderCType(f: *Function, w: *Writer, ctype: CType) !void { | 553 | fn renderType(f: *Function, w: *Writer, ty: Type) !void { |
| 607 | return f.object.dg.renderCType(w, ctype); | 554 | return f.dg.renderType(w, ty); |
| 608 | } | 555 | } |
| 609 | | 556 | |
| 610 | fn renderIntCast(f: *Function, w: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void { | 557 | fn renderIntCast(f: *Function, w: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void { |
| 611 | return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location); | 558 | return f.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location); |
| 612 | } | 559 | } |
| 613 | | 560 | |
| 614 | fn fmtIntLiteralDec(f: *Function, val: Value) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { | 561 | fn fmtIntLiteralDec(f: *Function, val: Value) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { |
| 615 | return f.object.dg.fmtIntLiteralDec(val, .Other); | 562 | return f.dg.fmtIntLiteralDec(val, .other); |
| 616 | } | 563 | } |
| 617 | | 564 | |
| 618 | fn fmtIntLiteralHex(f: *Function, val: Value) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { | 565 | fn fmtIntLiteralHex(f: *Function, val: Value) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { |
| 619 | return f.object.dg.fmtIntLiteralHex(val, .Other); | 566 | return f.dg.fmtIntLiteralHex(val, .other); |
| 620 | } | | |
| 621 | | | |
| 622 | fn getLazyFnName(f: *Function, key: LazyFnKey) ![]const u8 { | | |
| 623 | const gpa = f.object.dg.gpa; | | |
| 624 | const pt = f.object.dg.pt; | | |
| 625 | const zcu = pt.zcu; | | |
| 626 | const ip = &zcu.intern_pool; | | |
| 627 | const ctype_pool = &f.object.dg.ctype_pool; | | |
| 628 | | | |
| 629 | const gop = try f.lazy_fns.getOrPut(gpa, key); | | |
| 630 | if (!gop.found_existing) { | | |
| 631 | errdefer _ = f.lazy_fns.pop(); | | |
| 632 | | | |
| 633 | gop.value_ptr.* = .{ | | |
| 634 | .fn_name = switch (key) { | | |
| 635 | .tag_name, | | |
| 636 | => |enum_ty| try ctype_pool.fmt(gpa, "zig_{s}_{f}__{d}", .{ | | |
| 637 | @tagName(key), | | |
| 638 | fmtIdentUnsolo(ip.loadEnumType(enum_ty).name.toSlice(ip)), | | |
| 639 | @intFromEnum(enum_ty), | | |
| 640 | }), | | |
| 641 | .never_tail, | | |
| 642 | .never_inline, | | |
| 643 | => |owner_nav| try ctype_pool.fmt(gpa, "zig_{s}_{f}__{d}", .{ | | |
| 644 | @tagName(key), | | |
| 645 | fmtIdentUnsolo(ip.getNav(owner_nav).name.toSlice(ip)), | | |
| 646 | @intFromEnum(owner_nav), | | |
| 647 | }), | | |
| 648 | }, | | |
| 649 | }; | | |
| 650 | } | | |
| 651 | return gop.value_ptr.fn_name.toSlice(ctype_pool).?; | | |
| 652 | } | 567 | } |
| 653 | | 568 | |
| 654 | pub fn deinit(f: *Function) void { | 569 | pub fn deinit(f: *Function) void { |
| 655 | const gpa = f.object.dg.gpa; | 570 | const gpa = f.dg.gpa; |
| 656 | f.allocs.deinit(gpa); | 571 | f.allocs.deinit(gpa); |
| 657 | f.locals.deinit(gpa); | 572 | f.locals.deinit(gpa); |
| 658 | deinitFreeLocalsMap(gpa, &f.free_locals_map); | 573 | deinitFreeLocalsMap(gpa, &f.free_locals_map); |
| 659 | f.blocks.deinit(gpa); | 574 | f.blocks.deinit(gpa); |
| 660 | f.value_map.deinit(); | 575 | f.value_map.deinit(); |
| 661 | f.lazy_fns.deinit(gpa); | 576 | f.need_tag_name_funcs.deinit(gpa); |
| | 577 | f.need_never_tail_funcs.deinit(gpa); |
| | 578 | f.need_never_inline_funcs.deinit(gpa); |
| 662 | f.loop_switch_conds.deinit(gpa); | 579 | f.loop_switch_conds.deinit(gpa); |
| 663 | } | 580 | } |
| 664 | | 581 | |
| 665 | fn typeOf(f: *Function, inst: Air.Inst.Ref) Type { | 582 | fn typeOf(f: *Function, inst: Air.Inst.Ref) Type { |
| 666 | return f.air.typeOf(inst, &f.object.dg.pt.zcu.intern_pool); | 583 | return f.air.typeOf(inst, &f.dg.pt.zcu.intern_pool); |
| 667 | } | 584 | } |
| 668 | | 585 | |
| 669 | fn typeOfIndex(f: *Function, inst: Air.Inst.Index) Type { | 586 | fn typeOfIndex(f: *Function, inst: Air.Inst.Index) Type { |
| 670 | return f.air.typeOfIndex(inst, &f.object.dg.pt.zcu.intern_pool); | 587 | return f.air.typeOfIndex(inst, &f.dg.pt.zcu.intern_pool); |
| 671 | } | 588 | } |
| 672 | | 589 | |
| 673 | fn copyCValue(f: *Function, ctype: CType, dst: CValue, src: CValue) !void { | 590 | fn copyCValue(f: *Function, dst: CValue, src: CValue) !void { |
| 674 | switch (dst) { | 591 | switch (dst) { |
| 675 | .new_local, .local => |dst_local_index| switch (src) { | 592 | .new_local, .local => |dst_local_index| switch (src) { |
| 676 | .new_local, .local => |src_local_index| if (dst_local_index == src_local_index) return, | 593 | .new_local, .local => |src_local_index| if (dst_local_index == src_local_index) return, |
| ... | @@ -678,12 +595,12 @@ pub const Function = struct { | ... | @@ -678,12 +595,12 @@ pub const Function = struct { |
| 678 | }, | 595 | }, |
| 679 | else => {}, | 596 | else => {}, |
| 680 | } | 597 | } |
| 681 | const w = &f.object.code.writer; | 598 | const w = &f.code.writer; |
| 682 | const a = try Assignment.start(f, w, ctype); | 599 | try f.writeCValue(w, dst, .other); |
| 683 | try f.writeCValue(w, dst, .Other); | 600 | try w.writeAll(" = "); |
| 684 | try a.assign(f, w); | 601 | try f.writeCValue(w, src, .other); |
| 685 | try f.writeCValue(w, src, .Other); | 602 | try w.writeByte(';'); |
| 686 | try a.end(f, w); | 603 | try f.newline(); |
| 687 | } | 604 | } |
| 688 | | 605 | |
| 689 | fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue { | 606 | fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue { |
| ... | @@ -694,7 +611,7 @@ pub const Function = struct { | ... | @@ -694,7 +611,7 @@ pub const Function = struct { |
| 694 | else => { | 611 | else => { |
| 695 | try freeCValue(f, inst, src); | 612 | try freeCValue(f, inst, src); |
| 696 | const dst = try f.allocLocal(inst, ty); | 613 | const dst = try f.allocLocal(inst, ty); |
| 697 | try f.copyCValue(try f.ctypeFromType(ty, .complete), dst, src); | 614 | try f.copyCValue(dst, src); |
| 698 | return dst; | 615 | return dst; |
| 699 | }, | 616 | }, |
| 700 | } | 617 | } |
| ... | @@ -708,51 +625,17 @@ pub const Function = struct { | ... | @@ -708,51 +625,17 @@ pub const Function = struct { |
| 708 | } | 625 | } |
| 709 | }; | 626 | }; |
| 710 | | 627 | |
| 711 | /// This data is available when outputting .c code for a `Zcu`. | 628 | /// This data is available when rendering *any* C source code (function or otherwise). |
| 712 | /// It is not available when generating .h file. | | |
| 713 | pub const Object = struct { | | |
| 714 | dg: DeclGen, | | |
| 715 | code_header: Writer.Allocating, | | |
| 716 | code: Writer.Allocating, | | |
| 717 | indent_counter: usize, | | |
| 718 | | | |
| 719 | const indent_width = 1; | | |
| 720 | const indent_char = ' '; | | |
| 721 | | | |
| 722 | fn newline(o: *Object) !void { | | |
| 723 | const w = &o.code.writer; | | |
| 724 | try w.writeByte('\n'); | | |
| 725 | try w.splatByteAll(indent_char, o.indent_counter); | | |
| 726 | } | | |
| 727 | fn indent(o: *Object) void { | | |
| 728 | o.indent_counter += indent_width; | | |
| 729 | } | | |
| 730 | fn outdent(o: *Object) !void { | | |
| 731 | o.indent_counter -= indent_width; | | |
| 732 | const written = o.code.written(); | | |
| 733 | switch (written[written.len - 1]) { | | |
| 734 | indent_char => o.code.shrinkRetainingCapacity(written.len - indent_width), | | |
| 735 | '\n' => try o.code.writer.splatByteAll(indent_char, o.indent_counter), | | |
| 736 | else => { | | |
| 737 | std.debug.print("\"{f}\"\n", .{std.zig.fmtString(written[written.len -| 100..])}); | | |
| 738 | unreachable; | | |
| 739 | }, | | |
| 740 | } | | |
| 741 | } | | |
| 742 | }; | | |
| 743 | | | |
| 744 | /// This data is available both when outputting .c code and when outputting an .h file. | | |
| 745 | pub const DeclGen = struct { | 629 | pub const DeclGen = struct { |
| 746 | gpa: Allocator, | 630 | gpa: Allocator, |
| | 631 | arena: Allocator, |
| 747 | pt: Zcu.PerThread, | 632 | pt: Zcu.PerThread, |
| 748 | mod: *Module, | 633 | mod: *Module, |
| 749 | pass: Pass, | 634 | owner_nav: InternPool.Nav.Index.Optional, |
| 750 | is_naked_fn: bool, | 635 | is_naked_fn: bool, |
| 751 | expected_block: ?u32, | 636 | expected_block: ?u32, |
| 752 | fwd_decl: Writer.Allocating, | | |
| 753 | error_msg: ?*Zcu.ErrorMsg, | 637 | error_msg: ?*Zcu.ErrorMsg, |
| 754 | ctype_pool: CType.Pool, | 638 | ctype_deps: CType.Dependencies, |
| 755 | scratch: std.ArrayList(u32), | | |
| 756 | /// This map contains all the UAVs we saw generating this function. | 639 | /// This map contains all the UAVs we saw generating this function. |
| 757 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. | 640 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. |
| 758 | /// Key is the value of the UAV; value is the UAV's alignment, or | 641 | /// Key is the value of the UAV; value is the UAV's alignment, or |
| ... | @@ -760,16 +643,10 @@ pub const DeclGen = struct { | ... | @@ -760,16 +643,10 @@ pub const DeclGen = struct { |
| 760 | /// less than the natural alignment. | 643 | /// less than the natural alignment. |
| 761 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), | 644 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 762 | | 645 | |
| 763 | pub const Pass = union(enum) { | | |
| 764 | nav: InternPool.Nav.Index, | | |
| 765 | uav: InternPool.Index, | | |
| 766 | flush, | | |
| 767 | }; | | |
| 768 | | | |
| 769 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) Error { | 646 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) Error { |
| 770 | @branchHint(.cold); | 647 | @branchHint(.cold); |
| 771 | const zcu = dg.pt.zcu; | 648 | const zcu = dg.pt.zcu; |
| 772 | const src_loc = zcu.navSrcLoc(dg.pass.nav); | 649 | const src_loc = zcu.navSrcLoc(dg.owner_nav.unwrap().?); |
| 773 | dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args); | 650 | dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args); |
| 774 | return error.AnalysisFail; | 651 | return error.AnalysisFail; |
| 775 | } | 652 | } |
| ... | @@ -783,14 +660,13 @@ pub const DeclGen = struct { | ... | @@ -783,14 +660,13 @@ pub const DeclGen = struct { |
| 783 | const pt = dg.pt; | 660 | const pt = dg.pt; |
| 784 | const zcu = pt.zcu; | 661 | const zcu = pt.zcu; |
| 785 | const ip = &zcu.intern_pool; | 662 | const ip = &zcu.intern_pool; |
| 786 | const ctype_pool = &dg.ctype_pool; | | |
| 787 | const uav_val = Value.fromInterned(uav.val); | 663 | const uav_val = Value.fromInterned(uav.val); |
| 788 | const uav_ty = uav_val.typeOf(zcu); | 664 | const uav_ty = uav_val.typeOf(zcu); |
| 789 | | 665 | |
| 790 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. | 666 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 791 | const ptr_ty: Type = .fromInterned(uav.orig_ty); | 667 | const ptr_ty: Type = .fromInterned(uav.orig_ty); |
| 792 | if (ptr_ty.isPtrAtRuntime(zcu) and !uav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) { | 668 | if (ptr_ty.isPtrAtRuntime(zcu) and !uav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) { |
| 793 | return dg.writeCValue(w, .{ .undef = ptr_ty }); | 669 | return dg.renderUndefValue(w, ptr_ty, location); |
| 794 | } | 670 | } |
| 795 | | 671 | |
| 796 | // Chase function values in order to be able to reference the original function. | 672 | // Chase function values in order to be able to reference the original function. |
| ... | @@ -805,14 +681,12 @@ pub const DeclGen = struct { | ... | @@ -805,14 +681,12 @@ pub const DeclGen = struct { |
| 805 | // them). The analysis until now should ensure that the C function | 681 | // them). The analysis until now should ensure that the C function |
| 806 | // pointers are compatible. If they are not, then there is a bug | 682 | // pointers are compatible. If they are not, then there is a bug |
| 807 | // somewhere and we should let the C compiler tell us about it. | 683 | // somewhere and we should let the C compiler tell us about it. |
| 808 | const ptr_ctype = try dg.ctypeFromType(ptr_ty, .complete); | 684 | const elem_ty = ptr_ty.childType(zcu); |
| 809 | const elem_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype; | 685 | const need_cast = elem_ty.toIntern() != uav_ty.toIntern() and |
| 810 | const uav_ctype = try dg.ctypeFromType(uav_ty, .complete); | 686 | elem_ty.zigTypeTag(zcu) != .@"fn" or uav_ty.zigTypeTag(zcu) != .@"fn"; |
| 811 | const need_cast = !elem_ctype.eql(uav_ctype) and | | |
| 812 | (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function); | | |
| 813 | if (need_cast) { | 687 | if (need_cast) { |
| 814 | try w.writeAll("(("); | 688 | try w.writeAll("(("); |
| 815 | try dg.renderCType(w, ptr_ctype); | 689 | try dg.renderType(w, ptr_ty); |
| 816 | try w.writeByte(')'); | 690 | try w.writeByte(')'); |
| 817 | } | 691 | } |
| 818 | try w.writeByte('&'); | 692 | try w.writeByte('&'); |
| ... | @@ -842,11 +716,9 @@ pub const DeclGen = struct { | ... | @@ -842,11 +716,9 @@ pub const DeclGen = struct { |
| 842 | nav_index: InternPool.Nav.Index, | 716 | nav_index: InternPool.Nav.Index, |
| 843 | location: ValueRenderLocation, | 717 | location: ValueRenderLocation, |
| 844 | ) Error!void { | 718 | ) Error!void { |
| 845 | _ = location; | | |
| 846 | const pt = dg.pt; | 719 | const pt = dg.pt; |
| 847 | const zcu = pt.zcu; | 720 | const zcu = pt.zcu; |
| 848 | const ip = &zcu.intern_pool; | 721 | const ip = &zcu.intern_pool; |
| 849 | const ctype_pool = &dg.ctype_pool; | | |
| 850 | | 722 | |
| 851 | // Chase function values in order to be able to reference the original function. | 723 | // Chase function values in order to be able to reference the original function. |
| 852 | const owner_nav = switch (ip.getNav(nav_index).status) { | 724 | const owner_nav = switch (ip.getNav(nav_index).status) { |
| ... | @@ -863,25 +735,23 @@ pub const DeclGen = struct { | ... | @@ -863,25 +735,23 @@ pub const DeclGen = struct { |
| 863 | const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip)); | 735 | const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip)); |
| 864 | const ptr_ty = try pt.navPtrType(owner_nav); | 736 | const ptr_ty = try pt.navPtrType(owner_nav); |
| 865 | if (!nav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) { | 737 | if (!nav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) { |
| 866 | return dg.writeCValue(w, .{ .undef = ptr_ty }); | 738 | return dg.renderUndefValue(w, ptr_ty, location); |
| 867 | } | 739 | } |
| 868 | | 740 | |
| 869 | // We shouldn't cast C function pointers as this is UB (when you call | 741 | // We shouldn't cast C function pointers as this is UB (when you call |
| 870 | // them). The analysis until now should ensure that the C function | 742 | // them). The analysis until now should ensure that the C function |
| 871 | // pointers are compatible. If they are not, then there is a bug | 743 | // pointers are compatible. If they are not, then there is a bug |
| 872 | // somewhere and we should let the C compiler tell us about it. | 744 | // somewhere and we should let the C compiler tell us about it. |
| 873 | const ctype = try dg.ctypeFromType(ptr_ty, .complete); | 745 | const elem_ty = ptr_ty.childType(zcu); |
| 874 | const elem_ctype = ctype.info(ctype_pool).pointer.elem_ctype; | 746 | const need_cast = elem_ty.toIntern() != nav_ty.toIntern() and |
| 875 | const nav_ctype = try dg.ctypeFromType(nav_ty, .complete); | 747 | elem_ty.zigTypeTag(zcu) != .@"fn" or nav_ty.zigTypeTag(zcu) != .@"fn"; |
| 876 | const need_cast = !elem_ctype.eql(nav_ctype) and | | |
| 877 | (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function); | | |
| 878 | if (need_cast) { | 748 | if (need_cast) { |
| 879 | try w.writeAll("(("); | 749 | try w.writeAll("(("); |
| 880 | try dg.renderCType(w, ctype); | 750 | try dg.renderType(w, ptr_ty); |
| 881 | try w.writeByte(')'); | 751 | try w.writeByte(')'); |
| 882 | } | 752 | } |
| 883 | try w.writeByte('&'); | 753 | try w.writeByte('&'); |
| 884 | try dg.renderNavName(w, owner_nav); | 754 | try renderNavName(w, owner_nav, ip); |
| 885 | if (need_cast) try w.writeByte(')'); | 755 | if (need_cast) try w.writeByte(')'); |
| 886 | } | 756 | } |
| 887 | | 757 | |
| ... | @@ -896,11 +766,10 @@ pub const DeclGen = struct { | ... | @@ -896,11 +766,10 @@ pub const DeclGen = struct { |
| 896 | switch (derivation) { | 766 | switch (derivation) { |
| 897 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, | 767 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 898 | .int => |int| { | 768 | .int => |int| { |
| 899 | const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete); | | |
| 900 | const addr_val = try pt.intValue(.usize, int.addr); | 769 | const addr_val = try pt.intValue(.usize, int.addr); |
| 901 | try w.writeByte('('); | 770 | try w.writeByte('('); |
| 902 | try dg.renderCType(w, ptr_ctype); | 771 | try dg.renderType(w, int.ptr_ty); |
| 903 | try w.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)}); | 772 | try w.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .other)}); |
| 904 | }, | 773 | }, |
| 905 | | 774 | |
| 906 | .nav_ptr => |nav| try dg.renderNav(w, nav, location), | 775 | .nav_ptr => |nav| try dg.renderNav(w, nav, location), |
| ... | @@ -915,14 +784,10 @@ pub const DeclGen = struct { | ... | @@ -915,14 +784,10 @@ pub const DeclGen = struct { |
| 915 | .field_ptr => |field| { | 784 | .field_ptr => |field| { |
| 916 | const parent_ptr_ty = try field.parent.ptrType(pt); | 785 | const parent_ptr_ty = try field.parent.ptrType(pt); |
| 917 | | 786 | |
| 918 | // Ensure complete type definition is available before accessing fields. | | |
| 919 | _ = try dg.ctypeFromType(parent_ptr_ty.childType(zcu), .complete); | | |
| 920 | | | |
| 921 | switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, zcu)) { | 787 | switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, zcu)) { |
| 922 | .begin => { | 788 | .begin => { |
| 923 | const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete); | | |
| 924 | try w.writeByte('('); | 789 | try w.writeByte('('); |
| 925 | try dg.renderCType(w, ptr_ctype); | 790 | try dg.renderType(w, field.result_ptr_ty); |
| 926 | try w.writeByte(')'); | 791 | try w.writeByte(')'); |
| 927 | try dg.renderPointer(w, field.parent.*, location); | 792 | try dg.renderPointer(w, field.parent.*, location); |
| 928 | }, | 793 | }, |
| ... | @@ -933,51 +798,40 @@ pub const DeclGen = struct { | ... | @@ -933,51 +798,40 @@ pub const DeclGen = struct { |
| 933 | try dg.writeCValue(w, name); | 798 | try dg.writeCValue(w, name); |
| 934 | }, | 799 | }, |
| 935 | .byte_offset => |byte_offset| { | 800 | .byte_offset => |byte_offset| { |
| 936 | const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete); | | |
| 937 | try w.writeByte('('); | 801 | try w.writeByte('('); |
| 938 | try dg.renderCType(w, ptr_ctype); | 802 | try dg.renderType(w, field.result_ptr_ty); |
| 939 | try w.writeByte(')'); | 803 | try w.writeByte(')'); |
| 940 | const offset_val = try pt.intValue(.usize, byte_offset); | 804 | const offset_val = try pt.intValue(.usize, byte_offset); |
| 941 | try w.writeAll("((char *)"); | 805 | try w.writeAll("((char *)"); |
| 942 | try dg.renderPointer(w, field.parent.*, location); | 806 | try dg.renderPointer(w, field.parent.*, location); |
| 943 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)}); | 807 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .other)}); |
| 944 | }, | 808 | }, |
| 945 | } | 809 | } |
| 946 | }, | 810 | }, |
| 947 | | 811 | |
| 948 | .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) { | 812 | .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) { |
| 949 | // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer. | 813 | // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer. |
| 950 | const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete); | | |
| 951 | try w.writeByte('('); | 814 | try w.writeByte('('); |
| 952 | try dg.renderCType(w, ptr_ctype); | 815 | try dg.renderType(w, elem.result_ptr_ty); |
| 953 | try w.writeByte(')'); | 816 | try w.writeByte(')'); |
| 954 | try dg.renderPointer(w, elem.parent.*, location); | 817 | try dg.renderPointer(w, elem.parent.*, location); |
| 955 | } else { | 818 | } else { |
| 956 | const index_val = try pt.intValue(.usize, elem.elem_idx); | 819 | const index_val = try pt.intValue(.usize, elem.elem_idx); |
| 957 | // We want to do pointer arithmetic on a pointer to the element type. | 820 | try w.writeByte('('); |
| 958 | // We might have a pointer-to-array. In this case, we must cast first. | 821 | // We want to do pointer arithmetic on a pointer to the element type, but the parent |
| 959 | const result_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete); | 822 | // might be a pointer-to-array, in which case we must cast it. |
| 960 | const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete); | 823 | if (elem.result_ptr_ty.toIntern() != (try elem.parent.ptrType(pt)).toIntern()) { |
| 961 | if (result_ctype.eql(parent_ctype)) { | | |
| 962 | // The pointer already has an appropriate type - just do the arithmetic. | | |
| 963 | try w.writeByte('('); | 824 | try w.writeByte('('); |
| 964 | try dg.renderPointer(w, elem.parent.*, location); | 825 | try dg.renderType(w, elem.result_ptr_ty); |
| 965 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)}); | | |
| 966 | } else { | | |
| 967 | // We probably have an array pointer `T (*)[n]`. Cast to an element pointer, | | |
| 968 | // and *then* apply the index. | | |
| 969 | try w.writeAll("(("); | | |
| 970 | try dg.renderCType(w, result_ctype); | | |
| 971 | try w.writeByte(')'); | 826 | try w.writeByte(')'); |
| 972 | try dg.renderPointer(w, elem.parent.*, location); | | |
| 973 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)}); | | |
| 974 | } | 827 | } |
| | 828 | try dg.renderPointer(w, elem.parent.*, location); |
| | 829 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .other)}); |
| 975 | }, | 830 | }, |
| 976 | | 831 | |
| 977 | .offset_and_cast => |oac| { | 832 | .offset_and_cast => |oac| { |
| 978 | const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete); | | |
| 979 | try w.writeByte('('); | 833 | try w.writeByte('('); |
| 980 | try dg.renderCType(w, ptr_ctype); | 834 | try dg.renderType(w, oac.new_ptr_ty); |
| 981 | try w.writeByte(')'); | 835 | try w.writeByte(')'); |
| 982 | if (oac.byte_offset == 0) { | 836 | if (oac.byte_offset == 0) { |
| 983 | try dg.renderPointer(w, oac.parent.*, location); | 837 | try dg.renderPointer(w, oac.parent.*, location); |
| ... | @@ -985,14 +839,40 @@ pub const DeclGen = struct { | ... | @@ -985,14 +839,40 @@ pub const DeclGen = struct { |
| 985 | const offset_val = try pt.intValue(.usize, oac.byte_offset); | 839 | const offset_val = try pt.intValue(.usize, oac.byte_offset); |
| 986 | try w.writeAll("((char *)"); | 840 | try w.writeAll("((char *)"); |
| 987 | try dg.renderPointer(w, oac.parent.*, location); | 841 | try dg.renderPointer(w, oac.parent.*, location); |
| 988 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)}); | 842 | try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .other)}); |
| 989 | } | 843 | } |
| 990 | }, | 844 | }, |
| 991 | } | 845 | } |
| 992 | } | 846 | } |
| 993 | | 847 | |
| 994 | fn renderErrorName(dg: *DeclGen, w: *Writer, err_name: InternPool.NullTerminatedString) !void { | 848 | fn renderValueAsLvalue( |
| 995 | try w.print("zig_error_{f}", .{fmtIdentUnsolo(err_name.toSlice(&dg.pt.zcu.intern_pool))}); | 849 | dg: *DeclGen, |
| | 850 | w: *Writer, |
| | 851 | val: Value, |
| | 852 | ) Error!void { |
| | 853 | const zcu = dg.pt.zcu; |
| | 854 | |
| | 855 | // If the type of `val` lowers to a C struct or union type, then `renderValue` will render |
| | 856 | // it as a compound literal, and compound literals are already lvalues. |
| | 857 | const ty = val.typeOf(zcu); |
| | 858 | const is_aggregate: bool = switch (ty.zigTypeTag(zcu)) { |
| | 859 | .@"struct", .@"union" => switch (ty.containerLayout(zcu)) { |
| | 860 | .auto, .@"extern" => true, |
| | 861 | .@"packed" => false, |
| | 862 | }, |
| | 863 | .array, |
| | 864 | .vector, |
| | 865 | .error_union, |
| | 866 | .optional, |
| | 867 | => true, |
| | 868 | else => false, |
| | 869 | }; |
| | 870 | if (is_aggregate) return renderValue(dg, w, val, .other); |
| | 871 | |
| | 872 | // Otherwise, use a UAV. |
| | 873 | const gop = try dg.uavs.getOrPut(dg.gpa, val.toIntern()); |
| | 874 | if (!gop.found_existing) gop.value_ptr.* = .none; |
| | 875 | try renderUavName(w, val); |
| 996 | } | 876 | } |
| 997 | | 877 | |
| 998 | fn renderValue( | 878 | fn renderValue( |
| ... | @@ -1005,16 +885,13 @@ pub const DeclGen = struct { | ... | @@ -1005,16 +885,13 @@ pub const DeclGen = struct { |
| 1005 | const zcu = pt.zcu; | 885 | const zcu = pt.zcu; |
| 1006 | const ip = &zcu.intern_pool; | 886 | const ip = &zcu.intern_pool; |
| 1007 | const target = &dg.mod.resolved_target.result; | 887 | const target = &dg.mod.resolved_target.result; |
| 1008 | const ctype_pool = &dg.ctype_pool; | | |
| 1009 | | 888 | |
| 1010 | const initializer_type: ValueRenderLocation = switch (location) { | 889 | const initializer_type: ValueRenderLocation = switch (location) { |
| 1011 | .StaticInitializer => .StaticInitializer, | 890 | .static_initializer => .static_initializer, |
| 1012 | else => .Initializer, | 891 | else => .initializer, |
| 1013 | }; | 892 | }; |
| 1014 | | 893 | |
| 1015 | const ty = val.typeOf(zcu); | 894 | const ty = val.typeOf(zcu); |
| 1016 | if (val.isUndef(zcu)) return dg.renderUndefValue(w, ty, location); | | |
| 1017 | const ctype = try dg.ctypeFromType(ty, location.toCTypeKind()); | | |
| 1018 | switch (ip.indexToKey(val.toIntern())) { | 895 | switch (ip.indexToKey(val.toIntern())) { |
| 1019 | // types, not values | 896 | // types, not values |
| 1020 | .int_type, | 897 | .int_type, |
| ... | @@ -1037,7 +914,7 @@ pub const DeclGen = struct { | ... | @@ -1037,7 +914,7 @@ pub const DeclGen = struct { |
| 1037 | .memoized_call, | 914 | .memoized_call, |
| 1038 | => unreachable, | 915 | => unreachable, |
| 1039 | | 916 | |
| 1040 | .undef => unreachable, // handled above | 917 | .undef => try dg.renderUndefValue(w, ty, location), |
| 1041 | .simple_value => |simple_value| switch (simple_value) { | 918 | .simple_value => |simple_value| switch (simple_value) { |
| 1042 | // non-runtime values | 919 | // non-runtime values |
| 1043 | .void => unreachable, | 920 | .void => unreachable, |
| ... | @@ -1053,46 +930,28 @@ pub const DeclGen = struct { | ... | @@ -1053,46 +930,28 @@ pub const DeclGen = struct { |
| 1053 | .enum_literal, | 930 | .enum_literal, |
| 1054 | => unreachable, // non-runtime values | 931 | => unreachable, // non-runtime values |
| 1055 | .int => try w.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}), | 932 | .int => try w.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}), |
| 1056 | .err => |err| try dg.renderErrorName(w, err.name), | 933 | .err => |err| try renderErrorName(w, err.name.toSlice(ip)), |
| 1057 | .error_union => |error_union| switch (ctype.info(ctype_pool)) { | 934 | .error_union => |error_union| { |
| 1058 | .basic => switch (error_union.val) { | 935 | if (!location.isInitializer()) { |
| 1059 | .err_name => |err_name| try dg.renderErrorName(w, err_name), | 936 | try w.writeByte('('); |
| | 937 | try dg.renderType(w, ty); |
| | 938 | try w.writeByte(')'); |
| | 939 | } |
| | 940 | try w.writeAll("{ .error = "); |
| | 941 | switch (error_union.val) { |
| | 942 | .err_name => |err_name| try renderErrorName(w, err_name.toSlice(ip)), |
| 1060 | .payload => try w.writeByte('0'), | 943 | .payload => try w.writeByte('0'), |
| 1061 | }, | 944 | } |
| 1062 | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 945 | if (ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 1063 | .aggregate => |aggregate| { | 946 | try w.writeAll(", .payload = "); |
| 1064 | if (!location.isInitializer()) { | 947 | switch (error_union.val) { |
| 1065 | try w.writeByte('('); | 948 | .err_name => try dg.renderUndefValue(w, ty.errorUnionPayload(zcu), initializer_type), |
| 1066 | try dg.renderCType(w, ctype); | 949 | .payload => |payload| try dg.renderValue(w, .fromInterned(payload), initializer_type), |
| 1067 | try w.writeByte(')'); | | |
| 1068 | } | | |
| 1069 | try w.writeByte('{'); | | |
| 1070 | for (0..aggregate.fields.len) |field_index| { | | |
| 1071 | if (field_index > 0) try w.writeByte(','); | | |
| 1072 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | | |
| 1073 | .@"error" => switch (error_union.val) { | | |
| 1074 | .err_name => |err_name| try dg.renderErrorName(w, err_name), | | |
| 1075 | .payload => try w.writeByte('0'), | | |
| 1076 | }, | | |
| 1077 | .payload => switch (error_union.val) { | | |
| 1078 | .err_name => try dg.renderUndefValue( | | |
| 1079 | w, | | |
| 1080 | ty.errorUnionPayload(zcu), | | |
| 1081 | initializer_type, | | |
| 1082 | ), | | |
| 1083 | .payload => |payload| try dg.renderValue( | | |
| 1084 | w, | | |
| 1085 | Value.fromInterned(payload), | | |
| 1086 | initializer_type, | | |
| 1087 | ), | | |
| 1088 | }, | | |
| 1089 | else => unreachable, | | |
| 1090 | } | | |
| 1091 | } | 950 | } |
| 1092 | try w.writeByte('}'); | 951 | } |
| 1093 | }, | 952 | try w.writeAll(" }"); |
| 1094 | }, | 953 | }, |
| 1095 | .enum_tag => |enum_tag| try dg.renderValue(w, Value.fromInterned(enum_tag.int), location), | 954 | .enum_tag => |enum_tag| try dg.renderValue(w, .fromInterned(enum_tag.int), location), |
| 1096 | .float => { | 955 | .float => { |
| 1097 | const bits = ty.floatBits(target); | 956 | const bits = ty.floatBits(target); |
| 1098 | const f128_val = val.toFloat(f128, zcu); | 957 | const f128_val = val.toFloat(f128, zcu); |
| ... | @@ -1143,7 +1002,7 @@ pub const DeclGen = struct { | ... | @@ -1143,7 +1002,7 @@ pub const DeclGen = struct { |
| 1143 | else | 1002 | else |
| 1144 | unreachable; | 1003 | unreachable; |
| 1145 | | 1004 | |
| 1146 | if (location == .StaticInitializer) { | 1005 | if (location == .static_initializer) { |
| 1147 | if (!std.math.isNan(f128_val) and std.math.isSignalNan(f128_val)) | 1006 | if (!std.math.isNan(f128_val) and std.math.isSignalNan(f128_val)) |
| 1148 | return dg.fail("TODO: C backend: implement nans rendering in static initializers", .{}); | 1007 | return dg.fail("TODO: C backend: implement nans rendering in static initializers", .{}); |
| 1149 | | 1008 | |
| ... | @@ -1154,9 +1013,11 @@ pub const DeclGen = struct { | ... | @@ -1154,9 +1013,11 @@ pub const DeclGen = struct { |
| 1154 | // return dg.fail("Only quiet nans are supported in global variable initializers", .{}); | 1013 | // return dg.fail("Only quiet nans are supported in global variable initializers", .{}); |
| 1155 | } | 1014 | } |
| 1156 | | 1015 | |
| 1157 | try w.writeAll("zig_"); | 1016 | if (location == .static_initializer) { |
| 1158 | try w.writeAll(if (location == .StaticInitializer) "init" else "make"); | 1017 | try w.writeAll("zig_init_special_"); |
| 1159 | try w.writeAll("_special_"); | 1018 | } else { |
| | 1019 | try w.writeAll("zig_make_special_"); |
| | 1020 | } |
| 1160 | try dg.renderTypeForBuiltinFnName(w, ty); | 1021 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1161 | try w.writeByte('('); | 1022 | try w.writeByte('('); |
| 1162 | if (std.math.signbit(f128_val)) try w.writeByte('-'); | 1023 | if (std.math.signbit(f128_val)) try w.writeByte('-'); |
| ... | @@ -1183,105 +1044,85 @@ pub const DeclGen = struct { | ... | @@ -1183,105 +1044,85 @@ pub const DeclGen = struct { |
| 1183 | if (!empty) try w.writeByte(')'); | 1044 | if (!empty) try w.writeByte(')'); |
| 1184 | }, | 1045 | }, |
| 1185 | .slice => |slice| { | 1046 | .slice => |slice| { |
| 1186 | const aggregate = ctype.info(ctype_pool).aggregate; | | |
| 1187 | if (!location.isInitializer()) { | 1047 | if (!location.isInitializer()) { |
| 1188 | try w.writeByte('('); | 1048 | try w.writeByte('('); |
| 1189 | try dg.renderCType(w, ctype); | 1049 | try dg.renderType(w, ty); |
| 1190 | try w.writeByte(')'); | 1050 | try w.writeByte(')'); |
| 1191 | } | 1051 | } |
| 1192 | try w.writeByte('{'); | 1052 | try w.writeByte('{'); |
| 1193 | for (0..aggregate.fields.len) |field_index| { | 1053 | try dg.renderValue(w, .fromInterned(slice.ptr), initializer_type); |
| 1194 | if (field_index > 0) try w.writeByte(','); | 1054 | try w.writeByte(','); |
| 1195 | try dg.renderValue(w, Value.fromInterned( | 1055 | try dg.renderValue(w, .fromInterned(slice.len), initializer_type); |
| 1196 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | | |
| 1197 | .ptr => slice.ptr, | | |
| 1198 | .len => slice.len, | | |
| 1199 | else => unreachable, | | |
| 1200 | }, | | |
| 1201 | ), initializer_type); | | |
| 1202 | } | | |
| 1203 | try w.writeByte('}'); | 1056 | try w.writeByte('}'); |
| 1204 | }, | 1057 | }, |
| 1205 | .ptr => { | 1058 | .ptr => { |
| 1206 | var arena = std.heap.ArenaAllocator.init(zcu.gpa); | 1059 | const derivation = try val.pointerDerivation(dg.arena, pt, null); |
| 1207 | defer arena.deinit(); | 1060 | try w.writeByte('('); |
| 1208 | const derivation = try val.pointerDerivation(arena.allocator(), pt, null); | | |
| 1209 | try dg.renderPointer(w, derivation, location); | 1061 | try dg.renderPointer(w, derivation, location); |
| | 1062 | try w.writeByte(')'); |
| 1210 | }, | 1063 | }, |
| 1211 | .opt => |opt| switch (ctype.info(ctype_pool)) { | 1064 | .opt => |opt| switch (CType.classifyOptional(ty, zcu)) { |
| 1212 | .basic => if (ctype.isBool()) try w.writeAll(switch (opt.val) { | 1065 | .npv_payload => unreachable, // opv optional |
| 1213 | .none => "true", | 1066 | .opv_payload => { |
| 1214 | else => "false", | 1067 | if (!location.isInitializer()) { |
| 1215 | }) else switch (opt.val) { | 1068 | try w.writeByte('('); |
| | 1069 | try dg.renderType(w, ty); |
| | 1070 | try w.writeByte(')'); |
| | 1071 | } |
| | 1072 | try w.writeAll(switch (opt.val) { |
| | 1073 | .none => "{.is_null = true}", |
| | 1074 | else => "{.is_null = false}", |
| | 1075 | }); |
| | 1076 | }, |
| | 1077 | .error_set => switch (opt.val) { |
| 1216 | .none => try w.writeByte('0'), | 1078 | .none => try w.writeByte('0'), |
| 1217 | else => |payload| switch (ip.indexToKey(payload)) { | 1079 | else => |payload_val| try dg.renderValue(w, .fromInterned(payload_val), location), |
| 1218 | .undef => |err_ty| try dg.renderUndefValue( | | |
| 1219 | w, | | |
| 1220 | .fromInterned(err_ty), | | |
| 1221 | location, | | |
| 1222 | ), | | |
| 1223 | .err => |err| try dg.renderErrorName(w, err.name), | | |
| 1224 | else => unreachable, | | |
| 1225 | }, | | |
| 1226 | }, | 1080 | }, |
| 1227 | .pointer => switch (opt.val) { | 1081 | .ptr_like => switch (opt.val) { |
| 1228 | .none => try w.writeAll("NULL"), | 1082 | .none => try w.writeAll("NULL"), |
| 1229 | else => |payload| try dg.renderValue(w, Value.fromInterned(payload), location), | 1083 | else => |payload_val| try dg.renderValue(w, .fromInterned(payload_val), location), |
| 1230 | }, | 1084 | }, |
| 1231 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 1085 | .slice_like => switch (opt.val) { |
| 1232 | .aggregate => |aggregate| { | 1086 | .none => { |
| 1233 | switch (opt.val) { | 1087 | if (!location.isInitializer()) { |
| 1234 | .none => {}, | 1088 | try w.writeByte('('); |
| 1235 | else => |payload| switch (aggregate.fields.at(0, ctype_pool).name.index) { | 1089 | try dg.renderType(w, ty); |
| 1236 | .is_null, .payload => {}, | 1090 | try w.writeByte(')'); |
| 1237 | .ptr, .len => return dg.renderValue( | 1091 | } |
| 1238 | w, | 1092 | try w.writeAll("{NULL,"); |
| 1239 | Value.fromInterned(payload), | 1093 | try dg.renderUndefValue(w, .usize, initializer_type); |
| 1240 | location, | 1094 | try w.writeByte('}'); |
| 1241 | ), | 1095 | }, |
| 1242 | else => unreachable, | 1096 | else => |payload_val| try dg.renderValue(w, .fromInterned(payload_val), location), |
| 1243 | }, | 1097 | }, |
| 1244 | } | 1098 | .@"struct" => { |
| 1245 | if (!location.isInitializer()) { | 1099 | if (!location.isInitializer()) { |
| 1246 | try w.writeByte('('); | 1100 | try w.writeByte('('); |
| 1247 | try dg.renderCType(w, ctype); | 1101 | try dg.renderType(w, ty); |
| 1248 | try w.writeByte(')'); | 1102 | try w.writeByte(')'); |
| 1249 | } | 1103 | } |
| 1250 | try w.writeByte('{'); | 1104 | switch (opt.val) { |
| 1251 | for (0..aggregate.fields.len) |field_index| { | 1105 | .none => { |
| 1252 | if (field_index > 0) try w.writeByte(','); | 1106 | try w.writeAll("{ .is_null = true, .payload = "); |
| 1253 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | 1107 | try dg.renderUndefValue(w, ty.optionalChild(zcu), initializer_type); |
| 1254 | .is_null => try w.writeAll(switch (opt.val) { | 1108 | try w.writeAll(" }"); |
| 1255 | .none => "true", | 1109 | }, |
| 1256 | else => "false", | 1110 | else => |payload_val| { |
| 1257 | }), | 1111 | try w.writeAll("{ .is_null = false, .payload = "); |
| 1258 | .payload => switch (opt.val) { | 1112 | try dg.renderValue(w, .fromInterned(payload_val), initializer_type); |
| 1259 | .none => try dg.renderUndefValue( | 1113 | try w.writeAll(" }"); |
| 1260 | w, | 1114 | }, |
| 1261 | ty.optionalChild(zcu), | | |
| 1262 | initializer_type, | | |
| 1263 | ), | | |
| 1264 | else => |payload| try dg.renderValue( | | |
| 1265 | w, | | |
| 1266 | Value.fromInterned(payload), | | |
| 1267 | initializer_type, | | |
| 1268 | ), | | |
| 1269 | }, | | |
| 1270 | .ptr => try w.writeAll("NULL"), | | |
| 1271 | .len => try dg.renderUndefValue(w, .usize, initializer_type), | | |
| 1272 | else => unreachable, | | |
| 1273 | } | | |
| 1274 | } | 1115 | } |
| 1275 | try w.writeByte('}'); | | |
| 1276 | }, | 1116 | }, |
| 1277 | }, | 1117 | }, |
| 1278 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { | 1118 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { |
| 1279 | .array_type, .vector_type => { | 1119 | .array_type, .vector_type => { |
| 1280 | if (location == .FunctionArgument) { | 1120 | if (!location.isInitializer()) { |
| 1281 | try w.writeByte('('); | 1121 | try w.writeByte('('); |
| 1282 | try dg.renderCType(w, ctype); | 1122 | try dg.renderType(w, ty); |
| 1283 | try w.writeByte(')'); | 1123 | try w.writeByte(')'); |
| 1284 | } | 1124 | } |
| | 1125 | try w.writeByte('{'); |
| 1285 | const ai = ty.arrayInfo(zcu); | 1126 | const ai = ty.arrayInfo(zcu); |
| 1286 | if (ai.elem_type.eql(.u8, zcu)) { | 1127 | if (ai.elem_type.eql(.u8, zcu)) { |
| 1287 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); | 1128 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); |
| ... | @@ -1314,11 +1155,12 @@ pub const DeclGen = struct { | ... | @@ -1314,11 +1155,12 @@ pub const DeclGen = struct { |
| 1314 | } | 1155 | } |
| 1315 | try w.writeByte('}'); | 1156 | try w.writeByte('}'); |
| 1316 | } | 1157 | } |
| | 1158 | try w.writeByte('}'); |
| 1317 | }, | 1159 | }, |
| 1318 | .tuple_type => |tuple| { | 1160 | .tuple_type => |tuple| { |
| 1319 | if (!location.isInitializer()) { | 1161 | if (!location.isInitializer()) { |
| 1320 | try w.writeByte('('); | 1162 | try w.writeByte('('); |
| 1321 | try dg.renderCType(w, ctype); | 1163 | try dg.renderType(w, ty); |
| 1322 | try w.writeByte(')'); | 1164 | try w.writeByte(')'); |
| 1323 | } | 1165 | } |
| 1324 | | 1166 | |
| ... | @@ -1354,7 +1196,7 @@ pub const DeclGen = struct { | ... | @@ -1354,7 +1196,7 @@ pub const DeclGen = struct { |
| 1354 | | 1196 | |
| 1355 | if (!location.isInitializer()) { | 1197 | if (!location.isInitializer()) { |
| 1356 | try w.writeByte('('); | 1198 | try w.writeByte('('); |
| 1357 | try dg.renderCType(w, ctype); | 1199 | try dg.renderType(w, ty); |
| 1358 | try w.writeByte(')'); | 1200 | try w.writeByte(')'); |
| 1359 | } | 1201 | } |
| 1360 | | 1202 | |
| ... | @@ -1385,69 +1227,60 @@ pub const DeclGen = struct { | ... | @@ -1385,69 +1227,60 @@ pub const DeclGen = struct { |
| 1385 | .un => |un| { | 1227 | .un => |un| { |
| 1386 | const loaded_union = ip.loadUnionType(ty.toIntern()); | 1228 | const loaded_union = ip.loadUnionType(ty.toIntern()); |
| 1387 | if (un.tag == .none) { | 1229 | if (un.tag == .none) { |
| 1388 | const backing_ty = try ty.externUnionBackingType(pt); | | |
| 1389 | assert(loaded_union.layout == .@"extern"); | 1230 | assert(loaded_union.layout == .@"extern"); |
| 1390 | if (location == .StaticInitializer) { | 1231 | if (location == .static_initializer) { |
| 1391 | return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{}); | 1232 | return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{}); |
| 1392 | } | 1233 | } |
| 1393 | | 1234 | |
| 1394 | const ptr_ty = try pt.singleConstPtrType(ty); | 1235 | const ptr_ty = try pt.singleConstPtrType(ty); |
| 1395 | try w.writeAll("*(("); | 1236 | try w.writeAll("*("); |
| 1396 | try dg.renderType(w, ptr_ty); | 1237 | try dg.renderType(w, ptr_ty); |
| 1397 | try w.writeAll(")("); | 1238 | try w.writeAll(")&"); |
| 1398 | try dg.renderType(w, backing_ty); | 1239 | // We need an lvalue for '&'. |
| 1399 | try w.writeAll("){"); | 1240 | try dg.renderValueAsLvalue(w, .fromInterned(un.val)); |
| 1400 | try dg.renderValue(w, Value.fromInterned(un.val), location); | | |
| 1401 | try w.writeAll("})"); | | |
| 1402 | } else { | 1241 | } else { |
| 1403 | if (!location.isInitializer()) { | 1242 | if (!location.isInitializer()) { |
| 1404 | try w.writeByte('('); | 1243 | try w.writeByte('('); |
| 1405 | try dg.renderCType(w, ctype); | 1244 | try dg.renderType(w, ty); |
| 1406 | try w.writeByte(')'); | 1245 | try w.writeByte(')'); |
| 1407 | } | 1246 | } |
| | 1247 | if (ty.unionHasAllZeroBitFieldTypes(zcu)) { |
| | 1248 | assert(loaded_union.has_runtime_tag); // otherwise it does not have runtime bits |
| | 1249 | try w.writeAll("{ .tag = "); |
| | 1250 | try dg.renderValue(w, .fromInterned(un.tag), initializer_type); |
| | 1251 | try w.writeAll(" }"); |
| | 1252 | return; |
| | 1253 | } |
| 1408 | | 1254 | |
| 1409 | const field_index = zcu.unionTagFieldIndex(loaded_union, Value.fromInterned(un.tag)).?; | 1255 | if (loaded_union.layout == .auto) try w.writeByte('{'); |
| 1410 | const field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]); | | |
| 1411 | const field_name = ip.loadEnumType(loaded_union.enum_tag_type).field_names.get(ip)[field_index]; | | |
| 1412 | | 1256 | |
| 1413 | const has_tag = loaded_union.has_runtime_tag; | 1257 | if (loaded_union.has_runtime_tag) { |
| 1414 | if (has_tag) try w.writeByte('{'); | 1258 | try w.writeAll(" .tag = "); |
| 1415 | const aggregate = ctype.info(ctype_pool).aggregate; | 1259 | try dg.renderValue(w, .fromInterned(un.tag), initializer_type); |
| 1416 | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { | 1260 | try w.writeAll(", .payload = "); |
| 1417 | if (outer_field_index > 0) try w.writeByte(','); | 1261 | } |
| 1418 | switch (if (has_tag) | 1262 | |
| 1419 | aggregate.fields.at(outer_field_index, ctype_pool).name.index | 1263 | const enum_tag_ty: Type = .fromInterned(loaded_union.enum_tag_type); |
| 1420 | else | 1264 | const active_field_index = enum_tag_ty.enumTagFieldIndex(.fromInterned(un.tag), zcu).?; |
| 1421 | .payload) { | 1265 | const active_field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[active_field_index]); |
| 1422 | .tag => try dg.renderValue( | 1266 | if (active_field_ty.hasRuntimeBits(zcu)) { |
| 1423 | w, | 1267 | const active_field_name = enum_tag_ty.enumFieldName(active_field_index, zcu); |
| 1424 | Value.fromInterned(un.tag), | 1268 | try w.print("{{ .{f} = ", .{fmtIdentSolo(active_field_name.toSlice(ip))}); |
| 1425 | initializer_type, | 1269 | try dg.renderValue(w, .fromInterned(un.val), initializer_type); |
| 1426 | ), | 1270 | try w.writeAll(" }"); |
| 1427 | .payload => { | 1271 | } else { |
| 1428 | try w.writeByte('{'); | 1272 | const first_field_ty: Type = for (loaded_union.field_types.get(ip)) |field_ty_ip| { |
| 1429 | if (field_ty.hasRuntimeBits(zcu)) { | 1273 | const field_ty: Type = .fromInterned(field_ty_ip); |
| 1430 | try w.print(" .{f} = ", .{fmtIdentSolo(field_name.toSlice(ip))}); | 1274 | if (!field_ty.hasRuntimeBits(pt.zcu)) continue; |
| 1431 | try dg.renderValue( | 1275 | break field_ty; |
| 1432 | w, | 1276 | } else unreachable; |
| 1433 | Value.fromInterned(un.val), | 1277 | try w.writeByte('{'); |
| 1434 | initializer_type, | 1278 | try dg.renderUndefValue(w, first_field_ty, initializer_type); |
| 1435 | ); | 1279 | try w.writeByte('}'); |
| 1436 | try w.writeByte(' '); | | |
| 1437 | } else for (0..loaded_union.field_types.len) |inner_field_index| { | | |
| 1438 | const inner_field_ty: Type = .fromInterned( | | |
| 1439 | loaded_union.field_types.get(ip)[inner_field_index], | | |
| 1440 | ); | | |
| 1441 | if (!inner_field_ty.hasRuntimeBits(zcu)) continue; | | |
| 1442 | try dg.renderUndefValue(w, inner_field_ty, initializer_type); | | |
| 1443 | break; | | |
| 1444 | } | | |
| 1445 | try w.writeByte('}'); | | |
| 1446 | }, | | |
| 1447 | else => unreachable, | | |
| 1448 | } | | |
| 1449 | } | 1280 | } |
| 1450 | if (has_tag) try w.writeByte('}'); | 1281 | |
| | 1282 | if (loaded_union.has_runtime_tag) try w.writeByte(' '); |
| | 1283 | if (loaded_union.layout == .auto) try w.writeByte('}'); |
| 1451 | } | 1284 | } |
| 1452 | }, | 1285 | }, |
| 1453 | } | 1286 | } |
| ... | @@ -1463,11 +1296,10 @@ pub const DeclGen = struct { | ... | @@ -1463,11 +1296,10 @@ pub const DeclGen = struct { |
| 1463 | const zcu = pt.zcu; | 1296 | const zcu = pt.zcu; |
| 1464 | const ip = &zcu.intern_pool; | 1297 | const ip = &zcu.intern_pool; |
| 1465 | const target = &dg.mod.resolved_target.result; | 1298 | const target = &dg.mod.resolved_target.result; |
| 1466 | const ctype_pool = &dg.ctype_pool; | | |
| 1467 | | 1299 | |
| 1468 | const initializer_type: ValueRenderLocation = switch (location) { | 1300 | const initializer_type: ValueRenderLocation = switch (location) { |
| 1469 | .StaticInitializer => .StaticInitializer, | 1301 | .static_initializer => .static_initializer, |
| 1470 | else => .Initializer, | 1302 | else => .initializer, |
| 1471 | }; | 1303 | }; |
| 1472 | | 1304 | |
| 1473 | const safety_on = switch (zcu.optimizeMode()) { | 1305 | const safety_on = switch (zcu.optimizeMode()) { |
| ... | @@ -1475,7 +1307,6 @@ pub const DeclGen = struct { | ... | @@ -1475,7 +1307,6 @@ pub const DeclGen = struct { |
| 1475 | .ReleaseFast, .ReleaseSmall => false, | 1307 | .ReleaseFast, .ReleaseSmall => false, |
| 1476 | }; | 1308 | }; |
| 1477 | | 1309 | |
| 1478 | const ctype = try dg.ctypeFromType(ty, location.toCTypeKind()); | | |
| 1479 | switch (ty.toIntern()) { | 1310 | switch (ty.toIntern()) { |
| 1480 | .c_longdouble_type, | 1311 | .c_longdouble_type, |
| 1481 | .f16_type, | 1312 | .f16_type, |
| ... | @@ -1500,76 +1331,109 @@ pub const DeclGen = struct { | ... | @@ -1500,76 +1331,109 @@ pub const DeclGen = struct { |
| 1500 | else => unreachable, | 1331 | else => unreachable, |
| 1501 | } | 1332 | } |
| 1502 | try w.writeAll(", "); | 1333 | try w.writeAll(", "); |
| 1503 | try dg.renderUndefValue(w, repr_ty, .FunctionArgument); | 1334 | try dg.renderUndefValue(w, repr_ty, .other); |
| 1504 | return w.writeByte(')'); | 1335 | return w.writeByte(')'); |
| 1505 | }, | 1336 | }, |
| 1506 | .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"), | 1337 | .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"), |
| 1507 | else => switch (ip.indexToKey(ty.toIntern())) { | 1338 | else => switch (ip.indexToKey(ty.toIntern())) { |
| 1508 | .simple_type, | 1339 | .simple_type, // anyerror, c_char (etc), usize, isize |
| 1509 | .int_type, | 1340 | .int_type, |
| 1510 | .enum_type, | 1341 | .enum_type, |
| 1511 | .error_set_type, | 1342 | .error_set_type, |
| 1512 | .inferred_error_set_type, | 1343 | .inferred_error_set_type, |
| 1513 | => return w.print("{f}", .{ | 1344 | => switch (CType.classifyInt(ty, zcu)) { |
| 1514 | try dg.fmtIntLiteralHex(try pt.undefValue(ty), location), | 1345 | .void => unreachable, // opv |
| 1515 | }), | 1346 | .small => |s| { |
| | 1347 | const int = ty.intInfo(zcu); |
| | 1348 | var buf: [std.math.big.int.calcTwosCompLimbCount(128)]std.math.big.Limb = undefined; |
| | 1349 | var bigint: std.math.big.int.Mutable = .init(&buf, undefPattern(u128)); |
| | 1350 | bigint.truncate(bigint.toConst(), int.signedness, int.bits); |
| | 1351 | const fmt_undef: FormatInt128 = .{ |
| | 1352 | .target = zcu.getTarget(), |
| | 1353 | .int_cty = s, |
| | 1354 | .val = bigint.toConst(), |
| | 1355 | .is_global = location == .static_initializer, |
| | 1356 | .base = 16, |
| | 1357 | .case = .lower, |
| | 1358 | }; |
| | 1359 | try w.print("{f}", .{fmt_undef}); |
| | 1360 | }, |
| | 1361 | .big => |big| { |
| | 1362 | var buf: [std.math.big.int.calcTwosCompLimbCount(128)]std.math.big.Limb = undefined; |
| | 1363 | var limb_bigint: std.math.big.int.Mutable = .init(&buf, undefPattern(u128)); |
| | 1364 | limb_bigint.truncate(limb_bigint.toConst(), .unsigned, big.limb_size.bits()); |
| | 1365 | const fmt_undef_limb: FormatInt128 = .{ |
| | 1366 | .target = zcu.getTarget(), |
| | 1367 | .int_cty = big.limb_size.unsigned(), |
| | 1368 | .val = limb_bigint.toConst(), |
| | 1369 | .is_global = location == .static_initializer, |
| | 1370 | .base = 16, |
| | 1371 | .case = .lower, |
| | 1372 | }; |
| | 1373 | |
| | 1374 | if (!location.isInitializer()) { |
| | 1375 | try w.writeByte('('); |
| | 1376 | try dg.renderType(w, ty); |
| | 1377 | try w.writeByte(')'); |
| | 1378 | } |
| | 1379 | try w.writeAll("{{"); |
| | 1380 | try w.print("{f}", .{fmt_undef_limb}); |
| | 1381 | for (1..big.limbs_len) |_| { |
| | 1382 | try w.print(",{f}", .{fmt_undef_limb}); |
| | 1383 | } |
| | 1384 | try w.writeAll("}}"); |
| | 1385 | }, |
| | 1386 | }, |
| 1516 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { | 1387 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1517 | .one, .many, .c => { | 1388 | .one, .many, .c => { |
| 1518 | try w.writeAll("(("); | 1389 | try w.writeAll("(("); |
| 1519 | try dg.renderCType(w, ctype); | 1390 | try dg.renderType(w, ty); |
| 1520 | return w.print("){f})", .{ | 1391 | try w.writeByte(')'); |
| 1521 | try dg.fmtIntLiteralHex(.undef_usize, .Other), | 1392 | try dg.renderUndefValue(w, .usize, location); |
| 1522 | }); | 1393 | try w.writeByte(')'); |
| 1523 | }, | 1394 | }, |
| 1524 | .slice => { | 1395 | .slice => { |
| 1525 | if (!location.isInitializer()) { | 1396 | if (!location.isInitializer()) { |
| 1526 | try w.writeByte('('); | 1397 | try w.writeByte('('); |
| 1527 | try dg.renderCType(w, ctype); | 1398 | try dg.renderType(w, ty); |
| 1528 | try w.writeByte(')'); | 1399 | try w.writeByte(')'); |
| 1529 | } | 1400 | } |
| 1530 | | 1401 | |
| 1531 | try w.writeAll("{("); | 1402 | try w.writeByte('{'); |
| 1532 | const ptr_ty = ty.slicePtrFieldType(zcu); | 1403 | try dg.renderUndefValue(w, ty.slicePtrFieldType(zcu), initializer_type); |
| 1533 | try dg.renderType(w, ptr_ty); | 1404 | try w.writeByte(','); |
| 1534 | return w.print("){f}, {0f}}}", .{ | 1405 | try dg.renderUndefValue(w, .usize, initializer_type); |
| 1535 | try dg.fmtIntLiteralHex(.undef_usize, .Other), | 1406 | try w.writeByte('}'); |
| 1536 | }); | | |
| 1537 | }, | 1407 | }, |
| 1538 | }, | 1408 | }, |
| 1539 | .opt_type => |child_type| switch (ctype.info(ctype_pool)) { | 1409 | .opt_type => |child_type| switch (CType.classifyOptional(ty, zcu)) { |
| 1540 | .basic, .pointer => try dg.renderUndefValue( | 1410 | .npv_payload => unreachable, // opv optional |
| 1541 | w, | 1411 | |
| 1542 | .fromInterned(if (ctype.isBool()) .bool_type else child_type), | 1412 | .error_set, |
| 1543 | location, | 1413 | .ptr_like, |
| 1544 | ), | 1414 | .slice_like, |
| 1545 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 1415 | => try dg.renderUndefValue(w, .fromInterned(child_type), location), |
| 1546 | .aggregate => |aggregate| { | 1416 | |
| 1547 | switch (aggregate.fields.at(0, ctype_pool).name.index) { | 1417 | .opv_payload => { |
| 1548 | .is_null, .payload => {}, | | |
| 1549 | .ptr, .len => return dg.renderUndefValue( | | |
| 1550 | w, | | |
| 1551 | .fromInterned(child_type), | | |
| 1552 | location, | | |
| 1553 | ), | | |
| 1554 | else => unreachable, | | |
| 1555 | } | | |
| 1556 | if (!location.isInitializer()) { | 1418 | if (!location.isInitializer()) { |
| 1557 | try w.writeByte('('); | 1419 | try w.writeByte('('); |
| 1558 | try dg.renderCType(w, ctype); | 1420 | try dg.renderType(w, ty); |
| 1559 | try w.writeByte(')'); | 1421 | try w.writeByte(')'); |
| 1560 | } | 1422 | } |
| 1561 | try w.writeByte('{'); | 1423 | try w.writeAll(if (safety_on) "{.is_null=0xaa}" else "{.is_null=false}"); |
| 1562 | for (0..aggregate.fields.len) |field_index| { | 1424 | }, |
| 1563 | if (field_index > 0) try w.writeByte(','); | 1425 | |
| 1564 | try dg.renderUndefValue(w, .fromInterned( | 1426 | .@"struct" => { |
| 1565 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | 1427 | if (!location.isInitializer()) { |
| 1566 | .is_null => .bool_type, | 1428 | try w.writeByte('('); |
| 1567 | .payload => child_type, | 1429 | try dg.renderType(w, ty); |
| 1568 | else => unreachable, | 1430 | try w.writeByte(')'); |
| 1569 | }, | | |
| 1570 | ), initializer_type); | | |
| 1571 | } | 1431 | } |
| 1572 | try w.writeByte('}'); | 1432 | try w.writeAll("{ .is_null = "); |
| | 1433 | try dg.renderUndefValue(w, .bool, initializer_type); |
| | 1434 | try w.writeAll(", .payload = "); |
| | 1435 | try dg.renderUndefValue(w, .fromInterned(child_type), initializer_type); |
| | 1436 | try w.writeAll(" }"); |
| 1573 | }, | 1437 | }, |
| 1574 | }, | 1438 | }, |
| 1575 | .struct_type => { | 1439 | .struct_type => { |
| ... | @@ -1578,10 +1442,9 @@ pub const DeclGen = struct { | ... | @@ -1578,10 +1442,9 @@ pub const DeclGen = struct { |
| 1578 | .auto, .@"extern" => { | 1442 | .auto, .@"extern" => { |
| 1579 | if (!location.isInitializer()) { | 1443 | if (!location.isInitializer()) { |
| 1580 | try w.writeByte('('); | 1444 | try w.writeByte('('); |
| 1581 | try dg.renderCType(w, ctype); | 1445 | try dg.renderType(w, ty); |
| 1582 | try w.writeByte(')'); | 1446 | try w.writeByte(')'); |
| 1583 | } | 1447 | } |
| 1584 | | | |
| 1585 | try w.writeByte('{'); | 1448 | try w.writeByte('{'); |
| 1586 | var field_it = loaded_struct.iterateRuntimeOrder(ip); | 1449 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| 1587 | var need_comma = false; | 1450 | var need_comma = false; |
| ... | @@ -1601,7 +1464,7 @@ pub const DeclGen = struct { | ... | @@ -1601,7 +1464,7 @@ pub const DeclGen = struct { |
| 1601 | .tuple_type => |tuple_info| { | 1464 | .tuple_type => |tuple_info| { |
| 1602 | if (!location.isInitializer()) { | 1465 | if (!location.isInitializer()) { |
| 1603 | try w.writeByte('('); | 1466 | try w.writeByte('('); |
| 1604 | try dg.renderCType(w, ctype); | 1467 | try dg.renderType(w, ty); |
| 1605 | try w.writeByte(')'); | 1468 | try w.writeByte(')'); |
| 1606 | } | 1469 | } |
| 1607 | | 1470 | |
| ... | @@ -1624,80 +1487,61 @@ pub const DeclGen = struct { | ... | @@ -1624,80 +1487,61 @@ pub const DeclGen = struct { |
| 1624 | .auto, .@"extern" => { | 1487 | .auto, .@"extern" => { |
| 1625 | if (!location.isInitializer()) { | 1488 | if (!location.isInitializer()) { |
| 1626 | try w.writeByte('('); | 1489 | try w.writeByte('('); |
| 1627 | try dg.renderCType(w, ctype); | 1490 | try dg.renderType(w, ty); |
| 1628 | try w.writeByte(')'); | 1491 | try w.writeByte(')'); |
| 1629 | } | 1492 | } |
| 1630 | | 1493 | |
| 1631 | const has_tag = loaded_union.has_runtime_tag; | 1494 | const first_field_ty: Type = for (loaded_union.field_types.get(ip)) |field_ty_ip| { |
| 1632 | if (has_tag) try w.writeByte('{'); | 1495 | const field_ty: Type = .fromInterned(field_ty_ip); |
| 1633 | const aggregate = ctype.info(ctype_pool).aggregate; | 1496 | if (!field_ty.hasRuntimeBits(pt.zcu)) continue; |
| 1634 | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { | 1497 | break field_ty; |
| 1635 | if (outer_field_index > 0) try w.writeByte(','); | 1498 | } else { |
| 1636 | switch (if (has_tag) | 1499 | assert(loaded_union.has_runtime_tag); // otherwise it does not have runtime bits |
| 1637 | aggregate.fields.at(outer_field_index, ctype_pool).name.index | 1500 | try w.writeAll("{ .tag = "); |
| 1638 | else | 1501 | try dg.renderUndefValue(w, .fromInterned(loaded_union.enum_tag_type), initializer_type); |
| 1639 | .payload) { | 1502 | try w.writeAll(" }"); |
| 1640 | .tag => try dg.renderUndefValue( | 1503 | return; |
| 1641 | w, | 1504 | }; |
| 1642 | .fromInterned(loaded_union.enum_tag_type), | 1505 | |
| 1643 | initializer_type, | 1506 | if (loaded_union.layout == .auto) try w.writeByte('{'); |
| 1644 | ), | 1507 | |
| 1645 | .payload => { | 1508 | if (loaded_union.has_runtime_tag) { |
| 1646 | try w.writeByte('{'); | 1509 | try w.writeAll(" .tag = "); |
| 1647 | for (0..loaded_union.field_types.len) |inner_field_index| { | 1510 | try dg.renderUndefValue(w, .fromInterned(loaded_union.enum_tag_type), initializer_type); |
| 1648 | const inner_field_ty: Type = .fromInterned( | 1511 | try w.writeAll(", .payload = "); |
| 1649 | loaded_union.field_types.get(ip)[inner_field_index], | | |
| 1650 | ); | | |
| 1651 | if (!inner_field_ty.hasRuntimeBits(pt.zcu)) continue; | | |
| 1652 | try dg.renderUndefValue( | | |
| 1653 | w, | | |
| 1654 | inner_field_ty, | | |
| 1655 | initializer_type, | | |
| 1656 | ); | | |
| 1657 | break; | | |
| 1658 | } | | |
| 1659 | try w.writeByte('}'); | | |
| 1660 | }, | | |
| 1661 | else => unreachable, | | |
| 1662 | } | | |
| 1663 | } | 1512 | } |
| 1664 | if (has_tag) try w.writeByte('}'); | 1513 | |
| | 1514 | try w.writeByte('{'); |
| | 1515 | try dg.renderUndefValue(w, first_field_ty, initializer_type); |
| | 1516 | try w.writeByte('}'); |
| | 1517 | |
| | 1518 | if (loaded_union.has_runtime_tag) try w.writeByte(' '); |
| | 1519 | if (loaded_union.layout == .auto) try w.writeByte('}'); |
| 1665 | }, | 1520 | }, |
| 1666 | .@"packed" => return dg.renderUndefValue(w, ty.bitpackBackingInt(zcu), location), | 1521 | .@"packed" => return dg.renderUndefValue(w, ty.bitpackBackingInt(zcu), location), |
| 1667 | } | 1522 | } |
| 1668 | }, | 1523 | }, |
| 1669 | .error_union_type => |error_union_type| switch (ctype.info(ctype_pool)) { | 1524 | .error_union_type => |error_union| { |
| 1670 | .basic => try dg.renderUndefValue( | 1525 | if (!location.isInitializer()) { |
| 1671 | w, | 1526 | try w.writeByte('('); |
| 1672 | .fromInterned(error_union_type.error_set_type), | 1527 | try dg.renderType(w, ty); |
| 1673 | location, | 1528 | try w.writeByte(')'); |
| 1674 | ), | 1529 | } |
| 1675 | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 1530 | try w.writeAll("{ .error = "); |
| 1676 | .aggregate => |aggregate| { | 1531 | try dg.renderUndefValue(w, .fromInterned(error_union.error_set_type), initializer_type); |
| 1677 | if (!location.isInitializer()) { | 1532 | if (Type.fromInterned(error_union.payload_type).hasRuntimeBits(zcu)) { |
| 1678 | try w.writeByte('('); | 1533 | try w.writeAll(", .payload = "); |
| 1679 | try dg.renderCType(w, ctype); | 1534 | try dg.renderUndefValue(w, .fromInterned(error_union.payload_type), initializer_type); |
| 1680 | try w.writeByte(')'); | 1535 | } |
| 1681 | } | 1536 | try w.writeAll(" }"); |
| 1682 | try w.writeByte('{'); | | |
| 1683 | for (0..aggregate.fields.len) |field_index| { | | |
| 1684 | if (field_index > 0) try w.writeByte(','); | | |
| 1685 | try dg.renderUndefValue( | | |
| 1686 | w, | | |
| 1687 | .fromInterned( | | |
| 1688 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | | |
| 1689 | .@"error" => error_union_type.error_set_type, | | |
| 1690 | .payload => error_union_type.payload_type, | | |
| 1691 | else => unreachable, | | |
| 1692 | }, | | |
| 1693 | ), | | |
| 1694 | initializer_type, | | |
| 1695 | ); | | |
| 1696 | } | | |
| 1697 | try w.writeByte('}'); | | |
| 1698 | }, | | |
| 1699 | }, | 1537 | }, |
| 1700 | .array_type, .vector_type => { | 1538 | .array_type, .vector_type => { |
| | 1539 | if (!location.isInitializer()) { |
| | 1540 | try w.writeByte('('); |
| | 1541 | try dg.renderType(w, ty); |
| | 1542 | try w.writeByte(')'); |
| | 1543 | } |
| | 1544 | try w.writeByte('{'); |
| 1701 | const ai = ty.arrayInfo(zcu); | 1545 | const ai = ty.arrayInfo(zcu); |
| 1702 | if (ai.elem_type.eql(.u8, zcu)) { | 1546 | if (ai.elem_type.eql(.u8, zcu)) { |
| 1703 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); | 1547 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); |
| ... | @@ -1708,14 +1552,8 @@ pub const DeclGen = struct { | ... | @@ -1708,14 +1552,8 @@ pub const DeclGen = struct { |
| 1708 | const s_u8: u8 = @intCast(s.toUnsignedInt(zcu)); | 1552 | const s_u8: u8 = @intCast(s.toUnsignedInt(zcu)); |
| 1709 | if (s_u8 != 0) try literal.writeChar(s_u8); | 1553 | if (s_u8 != 0) try literal.writeChar(s_u8); |
| 1710 | } | 1554 | } |
| 1711 | return literal.end(); | 1555 | try literal.end(); |
| 1712 | } else { | 1556 | } else { |
| 1713 | if (!location.isInitializer()) { | | |
| 1714 | try w.writeByte('('); | | |
| 1715 | try dg.renderCType(w, ctype); | | |
| 1716 | try w.writeByte(')'); | | |
| 1717 | } | | |
| 1718 | | | |
| 1719 | try w.writeByte('{'); | 1557 | try w.writeByte('{'); |
| 1720 | var index: u64 = 0; | 1558 | var index: u64 = 0; |
| 1721 | while (index < ai.len) : (index += 1) { | 1559 | while (index < ai.len) : (index += 1) { |
| ... | @@ -1726,8 +1564,9 @@ pub const DeclGen = struct { | ... | @@ -1726,8 +1564,9 @@ pub const DeclGen = struct { |
| 1726 | if (index > 0) try w.writeAll(", "); | 1564 | if (index > 0) try w.writeAll(", "); |
| 1727 | try dg.renderValue(w, s, location); | 1565 | try dg.renderValue(w, s, location); |
| 1728 | } | 1566 | } |
| 1729 | return w.writeByte('}'); | 1567 | try w.writeByte('}'); |
| 1730 | } | 1568 | } |
| | 1569 | try w.writeByte('}'); |
| 1731 | }, | 1570 | }, |
| 1732 | .anyframe_type, | 1571 | .anyframe_type, |
| 1733 | .opaque_type, | 1572 | .opaque_type, |
| ... | @@ -1762,10 +1601,11 @@ pub const DeclGen = struct { | ... | @@ -1762,10 +1601,11 @@ pub const DeclGen = struct { |
| 1762 | w: *Writer, | 1601 | w: *Writer, |
| 1763 | fn_val: Value, | 1602 | fn_val: Value, |
| 1764 | fn_align: InternPool.Alignment, | 1603 | fn_align: InternPool.Alignment, |
| 1765 | kind: CType.Kind, | 1604 | kind: enum { forward_decl, definition }, |
| 1766 | name: union(enum) { | 1605 | name: union(enum) { |
| 1767 | nav: InternPool.Nav.Index, | 1606 | nav: InternPool.Nav.Index, |
| 1768 | fmt_ctype_pool_string: std.fmt.Alt(CTypePoolStringFormatData, formatCTypePoolString), | 1607 | nav_never_tail: InternPool.Nav.Index, |
| | 1608 | nav_never_inline: InternPool.Nav.Index, |
| 1769 | @"export": struct { | 1609 | @"export": struct { |
| 1770 | main_name: InternPool.NullTerminatedString, | 1610 | main_name: InternPool.NullTerminatedString, |
| 1771 | extern_name: InternPool.NullTerminatedString, | 1611 | extern_name: InternPool.NullTerminatedString, |
| ... | @@ -1776,14 +1616,12 @@ pub const DeclGen = struct { | ... | @@ -1776,14 +1616,12 @@ pub const DeclGen = struct { |
| 1776 | const ip = &zcu.intern_pool; | 1616 | const ip = &zcu.intern_pool; |
| 1777 | | 1617 | |
| 1778 | const fn_ty = fn_val.typeOf(zcu); | 1618 | const fn_ty = fn_val.typeOf(zcu); |
| 1779 | const fn_ctype = try dg.ctypeFromType(fn_ty, kind); | | |
| 1780 | | 1619 | |
| 1781 | const fn_info = zcu.typeToFunc(fn_ty).?; | 1620 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 1782 | if (fn_info.cc == .naked) { | 1621 | if (fn_info.cc == .naked) { |
| 1783 | switch (kind) { | 1622 | switch (kind) { |
| 1784 | .forward => try w.writeAll("zig_naked_decl "), | 1623 | .forward_decl => try w.writeAll("zig_naked_decl "), |
| 1785 | .complete => try w.writeAll("zig_naked "), | 1624 | .definition => try w.writeAll("zig_naked "), |
| 1786 | else => unreachable, | | |
| 1787 | } | 1625 | } |
| 1788 | } | 1626 | } |
| 1789 | | 1627 | |
| ... | @@ -1793,45 +1631,63 @@ pub const DeclGen = struct { | ... | @@ -1793,45 +1631,63 @@ pub const DeclGen = struct { |
| 1793 | if (func_analysis.branch_hint == .cold) | 1631 | if (func_analysis.branch_hint == .cold) |
| 1794 | try w.writeAll("zig_cold "); | 1632 | try w.writeAll("zig_cold "); |
| 1795 | | 1633 | |
| 1796 | if (kind == .complete and func_analysis.disable_intrinsics or dg.mod.no_builtin) | 1634 | if (kind == .definition and func_analysis.disable_intrinsics or dg.mod.no_builtin) |
| 1797 | try w.writeAll("zig_no_builtin "); | 1635 | try w.writeAll("zig_no_builtin "); |
| 1798 | } | 1636 | } |
| 1799 | | 1637 | |
| 1800 | if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn "); | 1638 | if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn "); |
| 1801 | | 1639 | |
| 1802 | var trailing = try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, fn_ctype, .suffix, .{}); | 1640 | // While incomplete types are usually an acceptable substitute for "void", this is not true |
| | 1641 | // in function return types, where "void" is the only incomplete type permitted. |
| | 1642 | const actual_return_type: Type = .fromInterned(fn_info.return_type); |
| | 1643 | const effective_return_type: Type = switch (actual_return_type.classify(zcu)) { |
| | 1644 | .no_possible_value => .noreturn, |
| | 1645 | .one_possible_value, .fully_comptime => .void, // no runtime bits |
| | 1646 | .partially_comptime, .runtime => actual_return_type, // yes runtime bits |
| | 1647 | }; |
| 1803 | | 1648 | |
| | 1649 | const ret_cty: CType = try .lower(effective_return_type, &dg.ctype_deps, dg.arena, zcu); |
| | 1650 | try w.print("{f}", .{ret_cty.fmtDeclaratorPrefix(zcu)}); |
| 1804 | if (toCallingConvention(fn_info.cc, zcu)) |call_conv| { | 1651 | if (toCallingConvention(fn_info.cc, zcu)) |call_conv| { |
| 1805 | try w.print("{f}zig_callconv({s})", .{ trailing, call_conv }); | 1652 | try w.print("zig_callconv({s}) ", .{call_conv}); |
| 1806 | trailing = .maybe_space; | | |
| 1807 | } | 1653 | } |
| 1808 | | | |
| 1809 | try w.print("{f}", .{trailing}); | | |
| 1810 | switch (name) { | 1654 | switch (name) { |
| 1811 | .nav => |nav| try dg.renderNavName(w, nav), | 1655 | .nav => |nav| try renderNavName(w, nav, ip), |
| 1812 | .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}), | 1656 | .nav_never_tail => |nav| try w.print("zig_never_tail_{f}__{d}", .{ |
| | 1657 | fmtIdentUnsolo(ip.getNav(nav).name.toSlice(ip)), @intFromEnum(nav), |
| | 1658 | }), |
| | 1659 | .nav_never_inline => |nav| try w.print("zig_never_inline_{f}__{d}", .{ |
| | 1660 | fmtIdentUnsolo(ip.getNav(nav).name.toSlice(ip)), @intFromEnum(nav), |
| | 1661 | }), |
| 1813 | .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), | 1662 | .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), |
| 1814 | } | 1663 | } |
| 1815 | | 1664 | { |
| 1816 | try renderTypeSuffix( | 1665 | try w.writeByte('('); |
| 1817 | dg.pass, | 1666 | var c_param_index: u32 = 0; |
| 1818 | &dg.ctype_pool, | 1667 | for (fn_info.param_types.get(ip)) |param_ty_ip| { |
| 1819 | zcu, | 1668 | const param_ty: Type = .fromInterned(param_ty_ip); |
| 1820 | w, | 1669 | if (!param_ty.hasRuntimeBits(zcu)) continue; |
| 1821 | fn_ctype, | 1670 | if (c_param_index != 0) try w.writeAll(", "); |
| 1822 | .suffix, | 1671 | try dg.renderTypeAndName(w, param_ty, .{ .arg = c_param_index }, .{ |
| 1823 | CQualifiers.init(.{ .@"const" = switch (kind) { | 1672 | .@"const" = kind == .definition, |
| 1824 | .forward => false, | 1673 | }, .none); |
| 1825 | .complete => true, | 1674 | c_param_index += 1; |
| 1826 | else => unreachable, | 1675 | } |
| 1827 | } }), | 1676 | if (fn_info.is_var_args) { |
| 1828 | ); | 1677 | if (c_param_index != 0) try w.writeAll(", "); |
| | 1678 | try w.writeAll("..."); |
| | 1679 | } else if (c_param_index == 0) { |
| | 1680 | try w.writeAll("void"); |
| | 1681 | } |
| | 1682 | try w.writeByte(')'); |
| | 1683 | } |
| | 1684 | try w.print("{f}", .{ret_cty.fmtDeclaratorSuffixIgnoreNonstring(zcu)}); |
| 1829 | | 1685 | |
| 1830 | switch (kind) { | 1686 | switch (kind) { |
| 1831 | .forward => { | 1687 | .forward_decl => { |
| 1832 | if (fn_align.toByteUnits()) |a| try w.print(" zig_align_fn({})", .{a}); | 1688 | if (fn_align.toByteUnits()) |a| try w.print(" zig_align_fn({})", .{a}); |
| 1833 | switch (name) { | 1689 | switch (name) { |
| 1834 | .nav, .fmt_ctype_pool_string => {}, | 1690 | .nav, .nav_never_tail, .nav_never_inline => {}, |
| 1835 | .@"export" => |@"export"| { | 1691 | .@"export" => |@"export"| { |
| 1836 | const extern_name = @"export".extern_name.toSlice(ip); | 1692 | const extern_name = @"export".extern_name.toSlice(ip); |
| 1837 | const is_mangled = isMangledIdent(extern_name, true); | 1693 | const is_mangled = isMangledIdent(extern_name, true); |
| ... | @@ -1855,38 +1711,16 @@ pub const DeclGen = struct { | ... | @@ -1855,38 +1711,16 @@ pub const DeclGen = struct { |
| 1855 | }, | 1711 | }, |
| 1856 | } | 1712 | } |
| 1857 | }, | 1713 | }, |
| 1858 | .complete => {}, | 1714 | .definition => {}, |
| 1859 | else => unreachable, | | |
| 1860 | } | 1715 | } |
| 1861 | } | 1716 | } |
| 1862 | | 1717 | |
| 1863 | fn ctypeFromType(dg: *DeclGen, ty: Type, kind: CType.Kind) !CType { | 1718 | /// Renders the C lowering of the given Zig type to `w`. This renders the type name---to render |
| 1864 | defer std.debug.assert(dg.scratch.items.len == 0); | 1719 | /// a declarator with this type, see instead `renderTypeAndName`. |
| 1865 | return dg.ctype_pool.fromType(dg.gpa, &dg.scratch, ty, dg.pt, dg.mod, kind); | 1720 | fn renderType(dg: *DeclGen, w: *Writer, ty: Type) (Writer.Error || Allocator.Error)!void { |
| 1866 | } | 1721 | const zcu = dg.pt.zcu; |
| 1867 | | 1722 | const cty: CType = try .lower(ty, &dg.ctype_deps, dg.arena, zcu); |
| 1868 | fn byteSize(dg: *DeclGen, ctype: CType) u64 { | 1723 | try w.print("{f}", .{cty.fmtTypeName(zcu)}); |
| 1869 | return ctype.byteSize(&dg.ctype_pool, dg.mod); | | |
| 1870 | } | | |
| 1871 | | | |
| 1872 | /// Renders a type as a single identifier, generating intermediate typedefs | | |
| 1873 | /// if necessary. | | |
| 1874 | /// | | |
| 1875 | /// This is guaranteed to be valid in both typedefs and declarations/definitions. | | |
| 1876 | /// | | |
| 1877 | /// There are three type formats in total that we support rendering: | | |
| 1878 | /// | Function | Example 1 (*u8) | Example 2 ([10]*u8) | | | |
| 1879 | /// |---------------------|-----------------|---------------------| | | |
| 1880 | /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" | | | |
| 1881 | /// | `renderType` | "uint8_t *" | "uint8_t *[10]" | | | |
| 1882 | /// | | |
| 1883 | fn renderType(dg: *DeclGen, w: *Writer, t: Type) Error!void { | | |
| 1884 | try dg.renderCType(w, try dg.ctypeFromType(t, .complete)); | | |
| 1885 | } | | |
| 1886 | | | |
| 1887 | fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) Error!void { | | |
| 1888 | _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); | | |
| 1889 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); | | |
| 1890 | } | 1724 | } |
| 1891 | | 1725 | |
| 1892 | const IntCastContext = union(enum) { | 1726 | const IntCastContext = union(enum) { |
| ... | @@ -1990,7 +1824,7 @@ pub const DeclGen = struct { | ... | @@ -1990,7 +1824,7 @@ pub const DeclGen = struct { |
| 1990 | try w.writeAll("zig_lo_"); | 1824 | try w.writeAll("zig_lo_"); |
| 1991 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); | 1825 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 1992 | try w.writeByte('('); | 1826 | try w.writeByte('('); |
| 1993 | try context.writeValue(dg, w, .FunctionArgument); | 1827 | try context.writeValue(dg, w, .other); |
| 1994 | try w.writeByte(')'); | 1828 | try w.writeByte(')'); |
| 1995 | } else if (dest_bits > 64 and src_bits <= 64) { | 1829 | } else if (dest_bits > 64 and src_bits <= 64) { |
| 1996 | try w.writeAll("zig_make_"); | 1830 | try w.writeAll("zig_make_"); |
| ... | @@ -2001,7 +1835,7 @@ pub const DeclGen = struct { | ... | @@ -2001,7 +1835,7 @@ pub const DeclGen = struct { |
| 2001 | try dg.renderType(w, src_eff_ty); | 1835 | try dg.renderType(w, src_eff_ty); |
| 2002 | try w.writeByte(')'); | 1836 | try w.writeByte(')'); |
| 2003 | } | 1837 | } |
| 2004 | try context.writeValue(dg, w, .FunctionArgument); | 1838 | try context.writeValue(dg, w, .other); |
| 2005 | try w.writeByte(')'); | 1839 | try w.writeByte(')'); |
| 2006 | } else { | 1840 | } else { |
| 2007 | assert(!src_is_ptr); | 1841 | assert(!src_is_ptr); |
| ... | @@ -2010,23 +1844,16 @@ pub const DeclGen = struct { | ... | @@ -2010,23 +1844,16 @@ pub const DeclGen = struct { |
| 2010 | try w.writeAll("(zig_hi_"); | 1844 | try w.writeAll("(zig_hi_"); |
| 2011 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); | 1845 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 2012 | try w.writeByte('('); | 1846 | try w.writeByte('('); |
| 2013 | try context.writeValue(dg, w, .FunctionArgument); | 1847 | try context.writeValue(dg, w, .other); |
| 2014 | try w.writeAll("), zig_lo_"); | 1848 | try w.writeAll("), zig_lo_"); |
| 2015 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); | 1849 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 2016 | try w.writeByte('('); | 1850 | try w.writeByte('('); |
| 2017 | try context.writeValue(dg, w, .FunctionArgument); | 1851 | try context.writeValue(dg, w, .other); |
| 2018 | try w.writeAll("))"); | 1852 | try w.writeAll("))"); |
| 2019 | } | 1853 | } |
| 2020 | } | 1854 | } |
| 2021 | | 1855 | |
| 2022 | /// Renders a type and name in field declaration/definition format. | 1856 | /// Renders to `w` a C declarator whose type is the C lowering of the given Zig type. |
| 2023 | /// | | |
| 2024 | /// There are three type formats in total that we support rendering: | | |
| 2025 | /// | Function | Example 1 (*u8) | Example 2 ([10]*u8) | | | |
| 2026 | /// |---------------------|-----------------|---------------------| | | |
| 2027 | /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" | | | |
| 2028 | /// | `renderType` | "uint8_t *" | "uint8_t *[10]" | | | |
| 2029 | /// | | |
| 2030 | fn renderTypeAndName( | 1857 | fn renderTypeAndName( |
| 2031 | dg: *DeclGen, | 1858 | dg: *DeclGen, |
| 2032 | w: *Writer, | 1859 | w: *Writer, |
| ... | @@ -2034,73 +1861,47 @@ pub const DeclGen = struct { | ... | @@ -2034,73 +1861,47 @@ pub const DeclGen = struct { |
| 2034 | name: CValue, | 1861 | name: CValue, |
| 2035 | qualifiers: CQualifiers, | 1862 | qualifiers: CQualifiers, |
| 2036 | alignment: Alignment, | 1863 | alignment: Alignment, |
| 2037 | kind: CType.Kind, | | |
| 2038 | ) !void { | | |
| 2039 | try dg.renderCTypeAndName( | | |
| 2040 | w, | | |
| 2041 | try dg.ctypeFromType(ty, kind), | | |
| 2042 | name, | | |
| 2043 | qualifiers, | | |
| 2044 | CType.AlignAs.fromAlignment(.{ | | |
| 2045 | .@"align" = alignment, | | |
| 2046 | .abi = ty.abiAlignment(dg.pt.zcu), | | |
| 2047 | }), | | |
| 2048 | ); | | |
| 2049 | } | | |
| 2050 | | | |
| 2051 | fn renderCTypeAndName( | | |
| 2052 | dg: *DeclGen, | | |
| 2053 | w: *Writer, | | |
| 2054 | ctype: CType, | | |
| 2055 | name: CValue, | | |
| 2056 | qualifiers: CQualifiers, | | |
| 2057 | alignas: CType.AlignAs, | | |
| 2058 | ) !void { | 1864 | ) !void { |
| 2059 | const zcu = dg.pt.zcu; | 1865 | const zcu = dg.pt.zcu; |
| 2060 | switch (alignas.abiOrder()) { | 1866 | const ip = &zcu.intern_pool; |
| 2061 | .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}), | 1867 | const cty: CType = try .lower(ty, &dg.ctype_deps, dg.arena, zcu); |
| | 1868 | try w.print("{f}", .{cty.fmtDeclaratorPrefix(zcu)}); |
| | 1869 | if (alignment != .none) switch (alignment.order(ty.abiAlignment(zcu))) { |
| | 1870 | .lt => try w.print("zig_under_align({d}) ", .{alignment.toByteUnits().?}), |
| 2062 | .eq => {}, | 1871 | .eq => {}, |
| 2063 | .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}), | 1872 | .gt => try w.print("zig_align({d}) ", .{alignment.toByteUnits().?}), |
| 2064 | } | 1873 | }; |
| 2065 | | 1874 | if (qualifiers.@"const") try w.writeAll("const "); |
| 2066 | try w.print("{f}", .{ | 1875 | if (qualifiers.@"volatile") try w.writeAll("volatile "); |
| 2067 | try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers), | 1876 | if (qualifiers.restrict) try w.writeAll("restrict "); |
| 2068 | }); | 1877 | switch (name) { |
| 2069 | try dg.writeName(w, name); | | |
| 2070 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, .{}); | | |
| 2071 | if (ctype.isNonString(&dg.ctype_pool)) try w.writeAll(" zig_nonstring"); | | |
| 2072 | } | | |
| 2073 | | | |
| 2074 | fn writeName(dg: *DeclGen, w: *Writer, c_value: CValue) !void { | | |
| 2075 | switch (c_value) { | | |
| 2076 | .new_local, .local => |i| try w.print("t{d}", .{i}), | 1878 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| | 1879 | .arg => |i| try w.print("a{d}", .{i}), |
| 2077 | .constant => |uav| try renderUavName(w, uav), | 1880 | .constant => |uav| try renderUavName(w, uav), |
| 2078 | .nav => |nav| try dg.renderNavName(w, nav), | 1881 | .nav => |nav| try renderNavName(w, nav, ip), |
| 2079 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), | 1882 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), |
| 2080 | else => unreachable, | 1883 | else => unreachable, |
| 2081 | } | 1884 | } |
| | 1885 | try w.print("{f}", .{cty.fmtDeclaratorSuffix(zcu)}); |
| 2082 | } | 1886 | } |
| 2083 | | 1887 | |
| 2084 | fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) Error!void { | 1888 | fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) Error!void { |
| 2085 | switch (c_value) { | 1889 | switch (c_value) { |
| 2086 | .none, .new_local, .local, .local_ref => unreachable, | 1890 | .none, .new_local, .local, .local_ref => unreachable, |
| 2087 | .constant => |uav| try renderUavName(w, uav), | 1891 | .constant => |uav| try renderUavName(w, uav), |
| 2088 | .arg, .arg_array => unreachable, | 1892 | .arg => unreachable, |
| 2089 | .field => |i| try w.print("f{d}", .{i}), | 1893 | .field => |i| try w.print("f{d}", .{i}), |
| 2090 | .nav => |nav| try dg.renderNavName(w, nav), | 1894 | .nav => |nav| try renderNavName(w, nav, &dg.pt.zcu.intern_pool), |
| 2091 | .nav_ref => |nav| { | 1895 | .nav_ref => |nav| { |
| 2092 | try w.writeByte('&'); | 1896 | try w.writeByte('&'); |
| 2093 | try dg.renderNavName(w, nav); | 1897 | try renderNavName(w, nav, &dg.pt.zcu.intern_pool); |
| 2094 | }, | 1898 | }, |
| 2095 | .undef => |ty| try dg.renderUndefValue(w, ty, .Other), | 1899 | .undef => |ty| try dg.renderUndefValue(w, ty, .other), |
| 2096 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), | 1900 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), |
| 2097 | .payload_identifier => |ident| try w.print("{f}.{f}", .{ | 1901 | .payload_identifier => |ident| try w.print("{f}.{f}", .{ |
| 2098 | fmtIdentSolo("payload"), | 1902 | fmtIdentSolo("payload"), |
| 2099 | fmtIdentSolo(ident), | 1903 | fmtIdentSolo(ident), |
| 2100 | }), | 1904 | }), |
| 2101 | .ctype_pool_string => |string| try w.print("{f}", .{ | | |
| 2102 | fmtCTypePoolString(string, &dg.ctype_pool, true), | | |
| 2103 | }), | | |
| 2104 | } | 1905 | } |
| 2105 | } | 1906 | } |
| 2106 | | 1907 | |
| ... | @@ -2112,16 +1913,14 @@ pub const DeclGen = struct { | ... | @@ -2112,16 +1913,14 @@ pub const DeclGen = struct { |
| 2112 | .local_ref, | 1913 | .local_ref, |
| 2113 | .constant, | 1914 | .constant, |
| 2114 | .arg, | 1915 | .arg, |
| 2115 | .arg_array, | | |
| 2116 | .ctype_pool_string, | | |
| 2117 | => unreachable, | 1916 | => unreachable, |
| 2118 | .field => |i| try w.print("f{d}", .{i}), | 1917 | .field => |i| try w.print("f{d}", .{i}), |
| 2119 | .nav => |nav| { | 1918 | .nav => |nav| { |
| 2120 | try w.writeAll("(*"); | 1919 | try w.writeAll("(*"); |
| 2121 | try dg.renderNavName(w, nav); | 1920 | try renderNavName(w, nav, &dg.pt.zcu.intern_pool); |
| 2122 | try w.writeByte(')'); | 1921 | try w.writeByte(')'); |
| 2123 | }, | 1922 | }, |
| 2124 | .nav_ref => |nav| try dg.renderNavName(w, nav), | 1923 | .nav_ref => |nav| try renderNavName(w, nav, &dg.pt.zcu.intern_pool), |
| 2125 | .undef => unreachable, | 1924 | .undef => unreachable, |
| 2126 | .identifier => |ident| try w.print("(*{f})", .{fmtIdentSolo(ident)}), | 1925 | .identifier => |ident| try w.print("(*{f})", .{fmtIdentSolo(ident)}), |
| 2127 | .payload_identifier => |ident| try w.print("(*{f}.{f})", .{ | 1926 | .payload_identifier => |ident| try w.print("(*{f}.{f})", .{ |
| ... | @@ -2157,8 +1956,6 @@ pub const DeclGen = struct { | ... | @@ -2157,8 +1956,6 @@ pub const DeclGen = struct { |
| 2157 | .field, | 1956 | .field, |
| 2158 | .undef, | 1957 | .undef, |
| 2159 | .arg, | 1958 | .arg, |
| 2160 | .arg_array, | | |
| 2161 | .ctype_pool_string, | | |
| 2162 | => unreachable, | 1959 | => unreachable, |
| 2163 | .nav, .identifier, .payload_identifier => { | 1960 | .nav, .identifier, .payload_identifier => { |
| 2164 | try dg.writeCValue(w, c_value); | 1961 | try dg.writeCValue(w, c_value); |
| ... | @@ -2172,101 +1969,36 @@ pub const DeclGen = struct { | ... | @@ -2172,101 +1969,36 @@ pub const DeclGen = struct { |
| 2172 | try dg.writeCValue(w, member); | 1969 | try dg.writeCValue(w, member); |
| 2173 | } | 1970 | } |
| 2174 | | 1971 | |
| 2175 | fn renderFwdDecl( | 1972 | fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void { |
| 2176 | dg: *DeclGen, | | |
| 2177 | nav_index: InternPool.Nav.Index, | | |
| 2178 | flags: packed struct { | | |
| 2179 | is_const: bool, | | |
| 2180 | is_threadlocal: bool, | | |
| 2181 | linkage: std.builtin.GlobalLinkage, | | |
| 2182 | visibility: std.builtin.SymbolVisibility, | | |
| 2183 | }, | | |
| 2184 | ) !void { | | |
| 2185 | const zcu = dg.pt.zcu; | 1973 | const zcu = dg.pt.zcu; |
| 2186 | const ip = &zcu.intern_pool; | 1974 | switch (ty.zigTypeTag(zcu)) { |
| 2187 | const nav = ip.getNav(nav_index); | 1975 | .bool => return w.writeAll("u8"), |
| 2188 | const fwd = &dg.fwd_decl.writer; | 1976 | .float => return w.print("f{d}", .{ty.floatBits(zcu.getTarget())}), |
| 2189 | try fwd.writeAll(switch (flags.linkage) { | 1977 | else => {}, |
| 2190 | .internal => "static ", | | |
| 2191 | .strong, .weak, .link_once => "zig_extern ", | | |
| 2192 | }); | | |
| 2193 | switch (flags.linkage) { | | |
| 2194 | .internal, .strong => {}, | | |
| 2195 | .weak => try fwd.writeAll("zig_weak_linkage "), | | |
| 2196 | .link_once => return dg.fail("TODO: CBE: implement linkonce linkage?", .{}), | | |
| 2197 | } | 1978 | } |
| 2198 | switch (flags.linkage) { | 1979 | if (ty.isPtrAtRuntime(zcu)) { |
| 2199 | .internal => {}, | 1980 | return w.print("p{d}", .{zcu.getTarget().ptrBitWidth()}); |
| 2200 | .strong, .weak, .link_once => try fwd.print("zig_visibility({s}) ", .{@tagName(flags.visibility)}), | | |
| 2201 | } | 1981 | } |
| 2202 | if (flags.is_threadlocal and !dg.mod.single_threaded) try fwd.writeAll("zig_threadlocal "); | 1982 | switch (CType.classifyInt(ty, zcu)) { |
| 2203 | try dg.renderTypeAndName( | 1983 | .void => unreachable, // opv |
| 2204 | fwd, | 1984 | .small => try w.print("{c}{d}", .{ |
| 2205 | .fromInterned(nav.typeOf(ip)), | 1985 | signAbbrev(ty.intInfo(zcu).signedness), |
| 2206 | .{ .nav = nav_index }, | 1986 | ty.abiSize(zcu) * 8, |
| 2207 | CQualifiers.init(.{ .@"const" = flags.is_const }), | | |
| 2208 | nav.getAlignment(), | | |
| 2209 | .complete, | | |
| 2210 | ); | | |
| 2211 | try fwd.writeAll(";\n"); | | |
| 2212 | } | | |
| 2213 | | | |
| 2214 | fn renderNavName(dg: *DeclGen, w: *Writer, nav_index: InternPool.Nav.Index) !void { | | |
| 2215 | const zcu = dg.pt.zcu; | | |
| 2216 | const ip = &zcu.intern_pool; | | |
| 2217 | const nav = ip.getNav(nav_index); | | |
| 2218 | if (nav.getExtern(ip)) |@"extern"| { | | |
| 2219 | try w.print("{f}", .{ | | |
| 2220 | fmtIdentSolo(ip.getNav(@"extern".owner_nav).name.toSlice(ip)), | | |
| 2221 | }); | | |
| 2222 | } else { | | |
| 2223 | // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), | | |
| 2224 | // expand to 3x the length of its input, but let's cut it off at a much shorter limit. | | |
| 2225 | const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip); | | |
| 2226 | try w.print("{f}__{d}", .{ | | |
| 2227 | fmtIdentUnsolo(fqn_slice[0..@min(fqn_slice.len, 100)]), | | |
| 2228 | @intFromEnum(nav_index), | | |
| 2229 | }); | | |
| 2230 | } | | |
| 2231 | } | | |
| 2232 | | | |
| 2233 | fn renderUavName(w: *Writer, uav: Value) !void { | | |
| 2234 | try w.print("__anon_{d}", .{@intFromEnum(uav.toIntern())}); | | |
| 2235 | } | | |
| 2236 | | | |
| 2237 | fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void { | | |
| 2238 | try dg.renderCTypeForBuiltinFnName(w, try dg.ctypeFromType(ty, .complete)); | | |
| 2239 | } | | |
| 2240 | | | |
| 2241 | fn renderCTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ctype: CType) !void { | | |
| 2242 | switch (ctype.info(&dg.ctype_pool)) { | | |
| 2243 | else => |ctype_info| try w.print("{c}{d}", .{ | | |
| 2244 | if (ctype.isBool()) | | |
| 2245 | signAbbrev(.unsigned) | | |
| 2246 | else if (ctype.isInteger()) | | |
| 2247 | signAbbrev(ctype.signedness(dg.mod)) | | |
| 2248 | else if (ctype.isFloat()) | | |
| 2249 | @as(u8, 'f') | | |
| 2250 | else if (ctype_info == .pointer) | | |
| 2251 | @as(u8, 'p') | | |
| 2252 | else | | |
| 2253 | return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for {s} type", .{@tagName(ctype_info)}), | | |
| 2254 | if (ctype.isFloat()) ctype.floatActiveBits(dg.mod) else dg.byteSize(ctype) * 8, | | |
| 2255 | }), | 1987 | }), |
| 2256 | .array => try w.writeAll("big"), | 1988 | .big => try w.writeAll("big"), |
| 2257 | } | 1989 | } |
| 2258 | } | 1990 | } |
| 2259 | | 1991 | |
| 2260 | fn renderBuiltinInfo(dg: *DeclGen, w: *Writer, ty: Type, info: BuiltinInfo) !void { | 1992 | fn renderBuiltinInfo(dg: *DeclGen, w: *Writer, ty: Type, info: BuiltinInfo) !void { |
| 2261 | const ctype = try dg.ctypeFromType(ty, .complete); | 1993 | const pt = dg.pt; |
| 2262 | const is_big = ctype.info(&dg.ctype_pool) == .array; | 1994 | const zcu = pt.zcu; |
| | 1995 | |
| | 1996 | const is_big = lowersToBigInt(ty, zcu); |
| 2263 | switch (info) { | 1997 | switch (info) { |
| 2264 | .none => if (!is_big) return, | 1998 | .none => if (!is_big) return, |
| 2265 | .bits => {}, | 1999 | .bits => {}, |
| 2266 | } | 2000 | } |
| 2267 | | 2001 | |
| 2268 | const pt = dg.pt; | | |
| 2269 | const zcu = pt.zcu; | | |
| 2270 | const int_info: std.builtin.Type.Int = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else .{ | 2002 | const int_info: std.builtin.Type.Int = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else .{ |
| 2271 | .signedness = .unsigned, | 2003 | .signedness = .unsigned, |
| 2272 | .bits = @intCast(ty.bitSize(zcu)), | 2004 | .bits = @intCast(ty.bitSize(zcu)), |
| ... | @@ -2275,7 +2007,7 @@ pub const DeclGen = struct { | ... | @@ -2275,7 +2007,7 @@ pub const DeclGen = struct { |
| 2275 | if (is_big) try w.print(", {}", .{int_info.signedness == .signed}); | 2007 | if (is_big) try w.print(", {}", .{int_info.signedness == .signed}); |
| 2276 | try w.print(", {f}", .{try dg.fmtIntLiteralDec( | 2008 | try w.print(", {f}", .{try dg.fmtIntLiteralDec( |
| 2277 | try pt.intValue(if (is_big) .u16 else .u8, int_info.bits), | 2009 | try pt.intValue(if (is_big) .u16 else .u8, int_info.bits), |
| 2278 | .FunctionArgument, | 2010 | .other, |
| 2279 | )}); | 2011 | )}); |
| 2280 | } | 2012 | } |
| 2281 | | 2013 | |
| ... | @@ -2286,15 +2018,13 @@ pub const DeclGen = struct { | ... | @@ -2286,15 +2018,13 @@ pub const DeclGen = struct { |
| 2286 | base: u8, | 2018 | base: u8, |
| 2287 | case: std.fmt.Case, | 2019 | case: std.fmt.Case, |
| 2288 | ) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { | 2020 | ) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { |
| 2289 | const zcu = dg.pt.zcu; | 2021 | // If there's a bigint type involved, mark a dependency on it. |
| 2290 | const kind = loc.toCTypeKind(); | 2022 | const cty: CType = try .lower(val.typeOf(dg.pt.zcu), &dg.ctype_deps, dg.arena, dg.pt.zcu); |
| 2291 | const ty = val.typeOf(zcu); | | |
| 2292 | return .{ .data = .{ | 2023 | return .{ .data = .{ |
| 2293 | .dg = dg, | 2024 | .dg = dg, |
| 2294 | .int_info = ty.intInfo(zcu), | 2025 | .loc = loc, |
| 2295 | .kind = kind, | | |
| 2296 | .ctype = try dg.ctypeFromType(ty, kind), | | |
| 2297 | .val = val, | 2026 | .val = val, |
| | 2027 | .cty = cty, |
| 2298 | .base = base, | 2028 | .base = base, |
| 2299 | .case = case, | 2029 | .case = case, |
| 2300 | } }; | 2030 | } }; |
| ... | @@ -2317,339 +2047,11 @@ pub const DeclGen = struct { | ... | @@ -2317,339 +2047,11 @@ pub const DeclGen = struct { |
| 2317 | } | 2047 | } |
| 2318 | }; | 2048 | }; |
| 2319 | | 2049 | |
| 2320 | const CTypeFix = enum { prefix, suffix }; | 2050 | const CQualifiers = packed struct { |
| 2321 | const CQualifiers = std.enums.EnumSet(enum { @"const", @"volatile", restrict }); | 2051 | @"const": bool = false, |
| 2322 | const Const = CQualifiers.init(.{ .@"const" = true }); | 2052 | @"volatile": bool = false, |
| 2323 | const RenderCTypeTrailing = enum { | 2053 | restrict: bool = false, |
| 2324 | no_space, | | |
| 2325 | maybe_space, | | |
| 2326 | | | |
| 2327 | pub fn format(self: @This(), w: *Writer) Writer.Error!void { | | |
| 2328 | switch (self) { | | |
| 2329 | .no_space => {}, | | |
| 2330 | .maybe_space => try w.writeByte(' '), | | |
| 2331 | } | | |
| 2332 | } | | |
| 2333 | }; | 2054 | }; |
| 2334 | fn renderAlignedTypeName(w: *Writer, ctype: CType) !void { | | |
| 2335 | try w.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)}); | | |
| 2336 | } | | |
| 2337 | fn renderFwdDeclTypeName( | | |
| 2338 | zcu: *Zcu, | | |
| 2339 | w: *Writer, | | |
| 2340 | ctype: CType, | | |
| 2341 | fwd_decl: CType.Info.FwdDecl, | | |
| 2342 | attributes: []const u8, | | |
| 2343 | ) !void { | | |
| 2344 | const ip = &zcu.intern_pool; | | |
| 2345 | try w.print("{s} {s}", .{ @tagName(fwd_decl.tag), attributes }); | | |
| 2346 | switch (fwd_decl.name) { | | |
| 2347 | .anon => try w.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}), | | |
| 2348 | .index => |index| try w.print("{f}__{d}", .{ | | |
| 2349 | fmtIdentUnsolo(Type.fromInterned(index).containerTypeName(ip).toSlice(&zcu.intern_pool)), | | |
| 2350 | @intFromEnum(index), | | |
| 2351 | }), | | |
| 2352 | } | | |
| 2353 | } | | |
| 2354 | fn renderTypePrefix( | | |
| 2355 | pass: DeclGen.Pass, | | |
| 2356 | ctype_pool: *const CType.Pool, | | |
| 2357 | zcu: *Zcu, | | |
| 2358 | w: *Writer, | | |
| 2359 | ctype: CType, | | |
| 2360 | parent_fix: CTypeFix, | | |
| 2361 | qualifiers: CQualifiers, | | |
| 2362 | ) Writer.Error!RenderCTypeTrailing { | | |
| 2363 | var trailing = RenderCTypeTrailing.maybe_space; | | |
| 2364 | switch (ctype.info(ctype_pool)) { | | |
| 2365 | .basic => |basic_info| try w.writeAll(@tagName(basic_info)), | | |
| 2366 | | | |
| 2367 | .pointer => |pointer_info| { | | |
| 2368 | try w.print("{f}*", .{try renderTypePrefix( | | |
| 2369 | pass, | | |
| 2370 | ctype_pool, | | |
| 2371 | zcu, | | |
| 2372 | w, | | |
| 2373 | pointer_info.elem_ctype, | | |
| 2374 | .prefix, | | |
| 2375 | CQualifiers.init(.{ | | |
| 2376 | .@"const" = pointer_info.@"const", | | |
| 2377 | .@"volatile" = pointer_info.@"volatile", | | |
| 2378 | }), | | |
| 2379 | )}); | | |
| 2380 | trailing = .no_space; | | |
| 2381 | }, | | |
| 2382 | | | |
| 2383 | .aligned => switch (pass) { | | |
| 2384 | .nav => |nav| try w.print("nav__{d}_{d}", .{ | | |
| 2385 | @intFromEnum(nav), @intFromEnum(ctype.index), | | |
| 2386 | }), | | |
| 2387 | .uav => |uav| try w.print("uav__{d}_{d}", .{ | | |
| 2388 | @intFromEnum(uav), @intFromEnum(ctype.index), | | |
| 2389 | }), | | |
| 2390 | .flush => try renderAlignedTypeName(w, ctype), | | |
| 2391 | }, | | |
| 2392 | | | |
| 2393 | .array, .vector => |sequence_info| { | | |
| 2394 | const child_trailing = try renderTypePrefix( | | |
| 2395 | pass, | | |
| 2396 | ctype_pool, | | |
| 2397 | zcu, | | |
| 2398 | w, | | |
| 2399 | sequence_info.elem_ctype, | | |
| 2400 | .suffix, | | |
| 2401 | qualifiers, | | |
| 2402 | ); | | |
| 2403 | switch (parent_fix) { | | |
| 2404 | .prefix => { | | |
| 2405 | try w.print("{f}(", .{child_trailing}); | | |
| 2406 | return .no_space; | | |
| 2407 | }, | | |
| 2408 | .suffix => return child_trailing, | | |
| 2409 | } | | |
| 2410 | }, | | |
| 2411 | | | |
| 2412 | .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) { | | |
| 2413 | .anon => switch (pass) { | | |
| 2414 | .nav => |nav| try w.print("nav__{d}_{d}", .{ | | |
| 2415 | @intFromEnum(nav), @intFromEnum(ctype.index), | | |
| 2416 | }), | | |
| 2417 | .uav => |uav| try w.print("uav__{d}_{d}", .{ | | |
| 2418 | @intFromEnum(uav), @intFromEnum(ctype.index), | | |
| 2419 | }), | | |
| 2420 | .flush => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""), | | |
| 2421 | }, | | |
| 2422 | .index => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""), | | |
| 2423 | }, | | |
| 2424 | | | |
| 2425 | .aggregate => |aggregate_info| switch (aggregate_info.name) { | | |
| 2426 | .anon => { | | |
| 2427 | try w.print("{s} {s}", .{ | | |
| 2428 | @tagName(aggregate_info.tag), | | |
| 2429 | if (aggregate_info.@"packed") "zig_packed(" else "", | | |
| 2430 | }); | | |
| 2431 | try renderFields(zcu, w, ctype_pool, aggregate_info, 1); | | |
| 2432 | if (aggregate_info.@"packed") try w.writeByte(')'); | | |
| 2433 | }, | | |
| 2434 | .fwd_decl => |fwd_decl| return renderTypePrefix( | | |
| 2435 | pass, | | |
| 2436 | ctype_pool, | | |
| 2437 | zcu, | | |
| 2438 | w, | | |
| 2439 | fwd_decl, | | |
| 2440 | parent_fix, | | |
| 2441 | qualifiers, | | |
| 2442 | ), | | |
| 2443 | }, | | |
| 2444 | | | |
| 2445 | .function => |function_info| { | | |
| 2446 | const child_trailing = try renderTypePrefix( | | |
| 2447 | pass, | | |
| 2448 | ctype_pool, | | |
| 2449 | zcu, | | |
| 2450 | w, | | |
| 2451 | function_info.return_ctype, | | |
| 2452 | .suffix, | | |
| 2453 | .{}, | | |
| 2454 | ); | | |
| 2455 | switch (parent_fix) { | | |
| 2456 | .prefix => { | | |
| 2457 | try w.print("{f}(", .{child_trailing}); | | |
| 2458 | return .no_space; | | |
| 2459 | }, | | |
| 2460 | .suffix => return child_trailing, | | |
| 2461 | } | | |
| 2462 | }, | | |
| 2463 | } | | |
| 2464 | var qualifier_it = qualifiers.iterator(); | | |
| 2465 | while (qualifier_it.next()) |qualifier| { | | |
| 2466 | try w.print("{f}{s}", .{ trailing, @tagName(qualifier) }); | | |
| 2467 | trailing = .maybe_space; | | |
| 2468 | } | | |
| 2469 | return trailing; | | |
| 2470 | } | | |
| 2471 | fn renderTypeSuffix( | | |
| 2472 | pass: DeclGen.Pass, | | |
| 2473 | ctype_pool: *const CType.Pool, | | |
| 2474 | zcu: *Zcu, | | |
| 2475 | w: *Writer, | | |
| 2476 | ctype: CType, | | |
| 2477 | parent_fix: CTypeFix, | | |
| 2478 | qualifiers: CQualifiers, | | |
| 2479 | ) Writer.Error!void { | | |
| 2480 | switch (ctype.info(ctype_pool)) { | | |
| 2481 | .basic, .aligned, .fwd_decl, .aggregate => {}, | | |
| 2482 | .pointer => |pointer_info| try renderTypeSuffix( | | |
| 2483 | pass, | | |
| 2484 | ctype_pool, | | |
| 2485 | zcu, | | |
| 2486 | w, | | |
| 2487 | pointer_info.elem_ctype, | | |
| 2488 | .prefix, | | |
| 2489 | .{}, | | |
| 2490 | ), | | |
| 2491 | .array, .vector => |sequence_info| { | | |
| 2492 | switch (parent_fix) { | | |
| 2493 | .prefix => try w.writeByte(')'), | | |
| 2494 | .suffix => {}, | | |
| 2495 | } | | |
| 2496 | | | |
| 2497 | try w.print("[{}]", .{sequence_info.len}); | | |
| 2498 | try renderTypeSuffix(pass, ctype_pool, zcu, w, sequence_info.elem_ctype, .suffix, .{}); | | |
| 2499 | }, | | |
| 2500 | .function => |function_info| { | | |
| 2501 | switch (parent_fix) { | | |
| 2502 | .prefix => try w.writeByte(')'), | | |
| 2503 | .suffix => {}, | | |
| 2504 | } | | |
| 2505 | | | |
| 2506 | try w.writeByte('('); | | |
| 2507 | var need_comma = false; | | |
| 2508 | for (0..function_info.param_ctypes.len) |param_index| { | | |
| 2509 | const param_type = function_info.param_ctypes.at(param_index, ctype_pool); | | |
| 2510 | if (need_comma) try w.writeAll(", "); | | |
| 2511 | need_comma = true; | | |
| 2512 | const trailing = | | |
| 2513 | try renderTypePrefix(pass, ctype_pool, zcu, w, param_type, .suffix, qualifiers); | | |
| 2514 | if (qualifiers.contains(.@"const")) try w.print("{f}a{d}", .{ trailing, param_index }); | | |
| 2515 | try renderTypeSuffix(pass, ctype_pool, zcu, w, param_type, .suffix, .{}); | | |
| 2516 | } | | |
| 2517 | if (function_info.varargs) { | | |
| 2518 | if (need_comma) try w.writeAll(", "); | | |
| 2519 | need_comma = true; | | |
| 2520 | try w.writeAll("..."); | | |
| 2521 | } | | |
| 2522 | if (!need_comma) try w.writeAll("void"); | | |
| 2523 | try w.writeByte(')'); | | |
| 2524 | | | |
| 2525 | try renderTypeSuffix(pass, ctype_pool, zcu, w, function_info.return_ctype, .suffix, .{}); | | |
| 2526 | }, | | |
| 2527 | } | | |
| 2528 | } | | |
| 2529 | fn renderFields( | | |
| 2530 | zcu: *Zcu, | | |
| 2531 | w: *Writer, | | |
| 2532 | ctype_pool: *const CType.Pool, | | |
| 2533 | aggregate_info: CType.Info.Aggregate, | | |
| 2534 | indent: usize, | | |
| 2535 | ) !void { | | |
| 2536 | try w.writeAll("{\n"); | | |
| 2537 | for (0..aggregate_info.fields.len) |field_index| { | | |
| 2538 | const field_info = aggregate_info.fields.at(field_index, ctype_pool); | | |
| 2539 | try w.splatByteAll(' ', indent + 1); | | |
| 2540 | switch (field_info.alignas.abiOrder()) { | | |
| 2541 | .lt => { | | |
| 2542 | std.debug.assert(aggregate_info.@"packed"); | | |
| 2543 | if (field_info.alignas.@"align" != .@"1") try w.print("zig_under_align({}) ", .{ | | |
| 2544 | field_info.alignas.toByteUnits(), | | |
| 2545 | }); | | |
| 2546 | }, | | |
| 2547 | .eq => if (aggregate_info.@"packed" and field_info.alignas.@"align" != .@"1") | | |
| 2548 | try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}), | | |
| 2549 | .gt => { | | |
| 2550 | std.debug.assert(field_info.alignas.@"align" != .@"1"); | | |
| 2551 | try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}); | | |
| 2552 | }, | | |
| 2553 | } | | |
| 2554 | const trailing = try renderTypePrefix( | | |
| 2555 | .flush, | | |
| 2556 | ctype_pool, | | |
| 2557 | zcu, | | |
| 2558 | w, | | |
| 2559 | field_info.ctype, | | |
| 2560 | .suffix, | | |
| 2561 | .{}, | | |
| 2562 | ); | | |
| 2563 | try w.print("{f}{f}", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool, true) }); | | |
| 2564 | try renderTypeSuffix(.flush, ctype_pool, zcu, w, field_info.ctype, .suffix, .{}); | | |
| 2565 | if (field_info.ctype.isNonString(ctype_pool)) try w.writeAll(" zig_nonstring"); | | |
| 2566 | try w.writeAll(";\n"); | | |
| 2567 | } | | |
| 2568 | try w.splatByteAll(' ', indent); | | |
| 2569 | try w.writeByte('}'); | | |
| 2570 | } | | |
| 2571 | | | |
| 2572 | pub fn genTypeDecl( | | |
| 2573 | zcu: *Zcu, | | |
| 2574 | w: *Writer, | | |
| 2575 | global_ctype_pool: *const CType.Pool, | | |
| 2576 | global_ctype: CType, | | |
| 2577 | pass: DeclGen.Pass, | | |
| 2578 | decl_ctype_pool: *const CType.Pool, | | |
| 2579 | decl_ctype: CType, | | |
| 2580 | found_existing: bool, | | |
| 2581 | ) !void { | | |
| 2582 | switch (global_ctype.info(global_ctype_pool)) { | | |
| 2583 | .basic, .pointer, .array, .vector, .function => {}, | | |
| 2584 | .aligned => |aligned_info| { | | |
| 2585 | if (!found_existing) { | | |
| 2586 | std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt)); | | |
| 2587 | try w.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()}); | | |
| 2588 | try w.print("{f}", .{try renderTypePrefix( | | |
| 2589 | .flush, | | |
| 2590 | global_ctype_pool, | | |
| 2591 | zcu, | | |
| 2592 | w, | | |
| 2593 | aligned_info.ctype, | | |
| 2594 | .suffix, | | |
| 2595 | .{}, | | |
| 2596 | )}); | | |
| 2597 | try renderAlignedTypeName(w, global_ctype); | | |
| 2598 | try renderTypeSuffix(.flush, global_ctype_pool, zcu, w, aligned_info.ctype, .suffix, .{}); | | |
| 2599 | try w.writeAll(";\n"); | | |
| 2600 | } | | |
| 2601 | switch (pass) { | | |
| 2602 | .nav, .uav => { | | |
| 2603 | try w.writeAll("typedef "); | | |
| 2604 | _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{}); | | |
| 2605 | try w.writeByte(' '); | | |
| 2606 | _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{}); | | |
| 2607 | try w.writeAll(";\n"); | | |
| 2608 | }, | | |
| 2609 | .flush => {}, | | |
| 2610 | } | | |
| 2611 | }, | | |
| 2612 | .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) { | | |
| 2613 | .anon => switch (pass) { | | |
| 2614 | .nav, .uav => { | | |
| 2615 | try w.writeAll("typedef "); | | |
| 2616 | _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{}); | | |
| 2617 | try w.writeByte(' '); | | |
| 2618 | _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{}); | | |
| 2619 | try w.writeAll(";\n"); | | |
| 2620 | }, | | |
| 2621 | .flush => {}, | | |
| 2622 | }, | | |
| 2623 | .index => |index| if (!found_existing) { | | |
| 2624 | const ip = &zcu.intern_pool; | | |
| 2625 | const ty: Type = .fromInterned(index); | | |
| 2626 | _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{}); | | |
| 2627 | try w.writeByte(';'); | | |
| 2628 | const file_scope = ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip); | | |
| 2629 | if (!zcu.fileByIndex(file_scope).mod.?.strip) try w.print(" /* {f} */", .{ | | |
| 2630 | ty.containerTypeName(ip).fmt(ip), | | |
| 2631 | }); | | |
| 2632 | try w.writeByte('\n'); | | |
| 2633 | }, | | |
| 2634 | }, | | |
| 2635 | .aggregate => |aggregate_info| switch (aggregate_info.name) { | | |
| 2636 | .anon => {}, | | |
| 2637 | .fwd_decl => |fwd_decl| if (!found_existing) { | | |
| 2638 | try renderFwdDeclTypeName( | | |
| 2639 | zcu, | | |
| 2640 | w, | | |
| 2641 | fwd_decl, | | |
| 2642 | fwd_decl.info(global_ctype_pool).fwd_decl, | | |
| 2643 | if (aggregate_info.@"packed") "zig_packed(" else "", | | |
| 2644 | ); | | |
| 2645 | try w.writeByte(' '); | | |
| 2646 | try renderFields(zcu, w, global_ctype_pool, aggregate_info, 0); | | |
| 2647 | if (aggregate_info.@"packed") try w.writeByte(')'); | | |
| 2648 | try w.writeAll(";\n"); | | |
| 2649 | }, | | |
| 2650 | }, | | |
| 2651 | } | | |
| 2652 | } | | |
| 2653 | | 2055 | |
| 2654 | pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { | 2056 | pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { |
| 2655 | for (zcu.global_assembly.values()) |asm_source| { | 2057 | for (zcu.global_assembly.values()) |asm_source| { |
| ... | @@ -2657,200 +2059,128 @@ pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { | ... | @@ -2657,200 +2059,128 @@ pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { |
| 2657 | } | 2059 | } |
| 2658 | } | 2060 | } |
| 2659 | | 2061 | |
| 2660 | pub fn genErrDecls(o: *Object) Error!void { | 2062 | pub fn genErrDecls( |
| 2661 | const pt = o.dg.pt; | 2063 | zcu: *const Zcu, |
| 2662 | const zcu = pt.zcu; | 2064 | w: *Writer, |
| | 2065 | slice_const_u8_sentinel_0_type_name: []const u8, |
| | 2066 | ) Writer.Error!void { |
| 2663 | const ip = &zcu.intern_pool; | 2067 | const ip = &zcu.intern_pool; |
| 2664 | const w = &o.code.writer; | | |
| 2665 | | 2068 | |
| 2666 | var max_name_len: usize = 0; | | |
| 2667 | // do not generate an invalid empty enum when the global error set is empty | | |
| 2668 | const names = ip.global_error_set.getNamesFromMainThread(); | 2069 | const names = ip.global_error_set.getNamesFromMainThread(); |
| | 2070 | // Don't generate an invalid empty enum if the global error set is empty! |
| 2669 | if (names.len > 0) { | 2071 | if (names.len > 0) { |
| 2670 | try w.writeAll("enum {"); | 2072 | try w.writeAll("enum {\n"); |
| 2671 | o.indent(); | | |
| 2672 | try o.newline(); | | |
| 2673 | for (names, 1..) |name_nts, value| { | 2073 | for (names, 1..) |name_nts, value| { |
| 2674 | const name = name_nts.toSlice(ip); | 2074 | try w.writeByte(' '); |
| 2675 | max_name_len = @max(name.len, max_name_len); | 2075 | try renderErrorName(w, name_nts.toSlice(ip)); |
| 2676 | const err_val = try pt.intern(.{ .err = .{ | 2076 | try w.print(" = {d}u,\n", .{value}); |
| 2677 | .ty = .anyerror_type, | | |
| 2678 | .name = name_nts, | | |
| 2679 | } }); | | |
| 2680 | try o.dg.renderValue(w, Value.fromInterned(err_val), .Other); | | |
| 2681 | try w.print(" = {d}u,", .{value}); | | |
| 2682 | try o.newline(); | | |
| 2683 | } | 2077 | } |
| 2684 | try o.outdent(); | 2078 | try w.writeAll("};\n"); |
| 2685 | try w.writeAll("};"); | 2079 | } |
| 2686 | try o.newline(); | | |
| 2687 | } | | |
| 2688 | const array_identifier = "zig_errorName"; | | |
| 2689 | const name_prefix = array_identifier ++ "_"; | | |
| 2690 | const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len); | | |
| 2691 | defer o.dg.gpa.free(name_buf); | | |
| 2692 | | | |
| 2693 | @memcpy(name_buf[0..name_prefix.len], name_prefix); | | |
| 2694 | for (names) |name| { | | |
| 2695 | const name_slice = name.toSlice(ip); | | |
| 2696 | @memcpy(name_buf[name_prefix.len..][0..name_slice.len], name_slice); | | |
| 2697 | const identifier = name_buf[0 .. name_prefix.len + name_slice.len]; | | |
| 2698 | | | |
| 2699 | const name_ty = try pt.arrayType(.{ | | |
| 2700 | .len = name_slice.len, | | |
| 2701 | .child = .u8_type, | | |
| 2702 | .sentinel = .zero_u8, | | |
| 2703 | }); | | |
| 2704 | const name_val = try pt.intern(.{ .aggregate = .{ | | |
| 2705 | .ty = name_ty.toIntern(), | | |
| 2706 | .storage = .{ .bytes = name.toString() }, | | |
| 2707 | } }); | | |
| 2708 | | 2080 | |
| 2709 | try w.writeAll("static "); | 2081 | for (names) |name_nts| { |
| 2710 | try o.dg.renderTypeAndName( | 2082 | const name = name_nts.toSlice(ip); |
| 2711 | w, | 2083 | try w.print( |
| 2712 | name_ty, | 2084 | "static uint8_t const zig_errorName_{f}[] = {f};\n", |
| 2713 | .{ .identifier = identifier }, | 2085 | .{ fmtIdentUnsolo(name), fmtStringLiteral(name, 0) }, |
| 2714 | Const, | | |
| 2715 | .none, | | |
| 2716 | .complete, | | |
| 2717 | ); | 2086 | ); |
| 2718 | try w.writeAll(" = "); | | |
| 2719 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); | | |
| 2720 | try w.writeByte(';'); | | |
| 2721 | try o.newline(); | | |
| 2722 | } | 2087 | } |
| 2723 | | 2088 | |
| 2724 | const name_array_ty = try pt.arrayType(.{ | 2089 | try w.print( |
| 2725 | .len = 1 + names.len, | 2090 | "static {s} const zig_errorName[{d}] = {{", |
| 2726 | .child = .slice_const_u8_sentinel_0_type, | 2091 | .{ slice_const_u8_sentinel_0_type_name, names.len }, |
| 2727 | }); | | |
| 2728 | | | |
| 2729 | try w.writeAll("static "); | | |
| 2730 | try o.dg.renderTypeAndName( | | |
| 2731 | w, | | |
| 2732 | name_array_ty, | | |
| 2733 | .{ .identifier = array_identifier }, | | |
| 2734 | Const, | | |
| 2735 | .none, | | |
| 2736 | .complete, | | |
| 2737 | ); | 2092 | ); |
| 2738 | try w.writeAll(" = {"); | 2093 | if (names.len > 0) try w.writeByte('\n'); |
| 2739 | for (names, 1..) |name_nts, val| { | 2094 | for (names) |name_nts| { |
| 2740 | const name = name_nts.toSlice(ip); | 2095 | const name = name_nts.toSlice(ip); |
| 2741 | if (val > 1) try w.writeAll(", "); | 2096 | try w.print( |
| 2742 | try w.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{ | 2097 | " {{zig_errorName_{f},{d}}},\n", |
| 2743 | fmtIdentUnsolo(name), | 2098 | .{ fmtIdentUnsolo(name), name.len }, |
| 2744 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer), | 2099 | ); |
| | 2100 | } |
| | 2101 | try w.writeAll("};\n"); |
| | 2102 | } |
| | 2103 | |
| | 2104 | pub fn genTagNameFn( |
| | 2105 | zcu: *const Zcu, |
| | 2106 | w: *Writer, |
| | 2107 | slice_const_u8_sentinel_0_type_name: []const u8, |
| | 2108 | enum_ty: Type, |
| | 2109 | enum_type_name: []const u8, |
| | 2110 | ) Writer.Error!void { |
| | 2111 | const ip = &zcu.intern_pool; |
| | 2112 | const loaded_enum = ip.loadEnumType(enum_ty.toIntern()); |
| | 2113 | assert(loaded_enum.field_names.len > 0); |
| | 2114 | if (Type.fromInterned(loaded_enum.int_tag_type).bitSize(zcu) > 64) { |
| | 2115 | @panic("TODO CBE: tagName for enum over 128 bits"); |
| | 2116 | } |
| | 2117 | |
| | 2118 | try w.print("static {s} zig_tagName_{f}__{d}({s} tag) {{\n", .{ |
| | 2119 | slice_const_u8_sentinel_0_type_name, |
| | 2120 | fmtIdentUnsolo(loaded_enum.name.toSlice(ip)), |
| | 2121 | @intFromEnum(enum_ty.toIntern()), |
| | 2122 | enum_type_name, |
| | 2123 | }); |
| | 2124 | for (loaded_enum.field_names.get(ip), 0..) |field_name, field_index| { |
| | 2125 | try w.print(" static uint8_t const name{d}[] = {f};\n", .{ |
| | 2126 | field_index, fmtStringLiteral(field_name.toSlice(ip), 0), |
| | 2127 | }); |
| | 2128 | } |
| | 2129 | |
| | 2130 | try w.writeAll(" switch (tag) {\n"); |
| | 2131 | const field_values = loaded_enum.field_values.get(ip); |
| | 2132 | for (loaded_enum.field_names.get(ip), 0..) |field_name, field_index| { |
| | 2133 | const field_int: u64 = int: { |
| | 2134 | if (field_values.len == 0) break :int field_index; |
| | 2135 | const field_val: Value = .fromInterned(field_values[field_index]); |
| | 2136 | break :int field_val.toUnsignedInt(zcu); |
| | 2137 | }; |
| | 2138 | try w.print(" case {d}: return ({s}){{name{d},{d}}};\n", .{ |
| | 2139 | field_int, |
| | 2140 | slice_const_u8_sentinel_0_type_name, |
| | 2141 | field_index, |
| | 2142 | field_name.toSlice(ip).len, |
| 2745 | }); | 2143 | }); |
| 2746 | } | 2144 | } |
| 2747 | try w.writeAll("};"); | 2145 | try w.writeAll( |
| 2748 | try o.newline(); | 2146 | \\ } |
| | 2147 | \\ zig_unreachable(); |
| | 2148 | \\} |
| | 2149 | \\ |
| | 2150 | ); |
| 2749 | } | 2151 | } |
| 2750 | | 2152 | |
| 2751 | pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFnMap.Entry) Error!void { | 2153 | pub fn genLazyCallModifierFn( |
| 2752 | const pt = o.dg.pt; | 2154 | dg: *DeclGen, |
| 2753 | const zcu = pt.zcu; | 2155 | fn_nav: InternPool.Nav.Index, |
| | 2156 | kind: enum { never_tail, never_inline }, |
| | 2157 | w: *Writer, |
| | 2158 | ) Error!void { |
| | 2159 | const zcu = dg.pt.zcu; |
| 2754 | const ip = &zcu.intern_pool; | 2160 | const ip = &zcu.intern_pool; |
| 2755 | const ctype_pool = &o.dg.ctype_pool; | | |
| 2756 | const w = &o.code.writer; | | |
| 2757 | const key = lazy_fn.key_ptr.*; | | |
| 2758 | const val = lazy_fn.value_ptr; | | |
| 2759 | switch (key) { | | |
| 2760 | .tag_name => |enum_ty_ip| { | | |
| 2761 | const enum_ty: Type = .fromInterned(enum_ty_ip); | | |
| 2762 | const name_slice_ty: Type = .slice_const_u8_sentinel_0; | | |
| 2763 | | | |
| 2764 | try w.writeAll("static "); | | |
| 2765 | try o.dg.renderType(w, name_slice_ty); | | |
| 2766 | try w.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)}); | | |
| 2767 | try o.dg.renderTypeAndName(w, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete); | | |
| 2768 | try w.writeAll(") {"); | | |
| 2769 | o.indent(); | | |
| 2770 | try o.newline(); | | |
| 2771 | try w.writeAll("switch (tag) {"); | | |
| 2772 | o.indent(); | | |
| 2773 | try o.newline(); | | |
| 2774 | const tag_names = enum_ty.enumFields(zcu); | | |
| 2775 | for (0..tag_names.len) |tag_index| { | | |
| 2776 | const tag_name = tag_names.get(ip)[tag_index]; | | |
| 2777 | const tag_name_len = tag_name.length(ip); | | |
| 2778 | const tag_val = try pt.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | | |
| 2779 | | | |
| 2780 | const name_ty = try pt.arrayType(.{ | | |
| 2781 | .len = tag_name_len, | | |
| 2782 | .child = .u8_type, | | |
| 2783 | .sentinel = .zero_u8, | | |
| 2784 | }); | | |
| 2785 | const name_val = try pt.intern(.{ .aggregate = .{ | | |
| 2786 | .ty = name_ty.toIntern(), | | |
| 2787 | .storage = .{ .bytes = tag_name.toString() }, | | |
| 2788 | } }); | | |
| 2789 | | 2161 | |
| 2790 | try w.print("case {f}: {{", .{ | 2162 | const fn_val = zcu.navValue(fn_nav); |
| 2791 | try o.dg.fmtIntLiteralDec(tag_val.intFromEnum(zcu), .Other), | | |
| 2792 | }); | | |
| 2793 | o.indent(); | | |
| 2794 | try o.newline(); | | |
| 2795 | try w.writeAll("static "); | | |
| 2796 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); | | |
| 2797 | try w.writeAll(" = "); | | |
| 2798 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); | | |
| 2799 | try w.writeByte(';'); | | |
| 2800 | try o.newline(); | | |
| 2801 | try w.writeAll("return ("); | | |
| 2802 | try o.dg.renderType(w, name_slice_ty); | | |
| 2803 | try w.print("){{{f}, {f}}};", .{ | | |
| 2804 | fmtIdentUnsolo("name"), | | |
| 2805 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, tag_name_len), .Other), | | |
| 2806 | }); | | |
| 2807 | try o.newline(); | | |
| 2808 | try o.outdent(); | | |
| 2809 | try w.writeByte('}'); | | |
| 2810 | try o.newline(); | | |
| 2811 | } | | |
| 2812 | try o.outdent(); | | |
| 2813 | try w.writeByte('}'); | | |
| 2814 | try o.newline(); | | |
| 2815 | try airUnreach(o); | | |
| 2816 | try o.outdent(); | | |
| 2817 | try w.writeByte('}'); | | |
| 2818 | try o.newline(); | | |
| 2819 | }, | | |
| 2820 | .never_tail, .never_inline => |fn_nav_index| { | | |
| 2821 | const fn_val = zcu.navValue(fn_nav_index); | | |
| 2822 | const fn_ctype = try o.dg.ctypeFromType(fn_val.typeOf(zcu), .complete); | | |
| 2823 | const fn_info = fn_ctype.info(ctype_pool).function; | | |
| 2824 | const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool, true); | | |
| 2825 | | | |
| 2826 | const fwd = &o.dg.fwd_decl.writer; | | |
| 2827 | try fwd.print("static zig_{s} ", .{@tagName(key)}); | | |
| 2828 | try o.dg.renderFunctionSignature(fwd, fn_val, ip.getNav(fn_nav_index).getAlignment(), .forward, .{ | | |
| 2829 | .fmt_ctype_pool_string = fn_name, | | |
| 2830 | }); | | |
| 2831 | try fwd.writeAll(";\n"); | | |
| 2832 | | 2163 | |
| 2833 | try w.print("zig_{s} ", .{@tagName(key)}); | 2164 | try w.print("static zig_{t} ", .{kind}); |
| 2834 | try o.dg.renderFunctionSignature(w, fn_val, .none, .complete, .{ | 2165 | try dg.renderFunctionSignature(w, fn_val, .none, .definition, switch (kind) { |
| 2835 | .fmt_ctype_pool_string = fn_name, | 2166 | .never_tail => .{ .nav_never_tail = fn_nav }, |
| 2836 | }); | 2167 | .never_inline => .{ .nav_never_inline = fn_nav }, |
| 2837 | try w.writeAll(" {"); | 2168 | }); |
| 2838 | o.indent(); | 2169 | try w.writeAll(" {\n return "); |
| 2839 | try o.newline(); | 2170 | try renderNavName(w, fn_nav, ip); |
| 2840 | try w.writeAll("return "); | 2171 | try w.writeByte('('); |
| 2841 | try o.dg.renderNavName(w, fn_nav_index); | 2172 | { |
| 2842 | try w.writeByte('('); | 2173 | const func_type = ip.indexToKey(fn_val.typeOf(zcu).toIntern()).func_type; |
| 2843 | for (0..fn_info.param_ctypes.len) |arg| { | 2174 | var c_param_index: u32 = 0; |
| 2844 | if (arg > 0) try w.writeAll(", "); | 2175 | for (func_type.param_types.get(ip)) |param_ty_ip| { |
| 2845 | try w.print("a{d}", .{arg}); | 2176 | const param_ty: Type = .fromInterned(param_ty_ip); |
| 2846 | } | 2177 | if (!param_ty.hasRuntimeBits(zcu)) continue; |
| 2847 | try w.writeAll(");"); | 2178 | if (c_param_index != 0) try w.writeAll(", "); |
| 2848 | try o.newline(); | 2179 | try w.print("a{d}", .{c_param_index}); |
| 2849 | try o.outdent(); | 2180 | c_param_index += 1; |
| 2850 | try w.writeByte('}'); | 2181 | } |
| 2851 | try o.newline(); | | |
| 2852 | }, | | |
| 2853 | } | 2182 | } |
| | 2183 | try w.writeAll(");\n}\n"); |
| 2854 | } | 2184 | } |
| 2855 | | 2185 | |
| 2856 | pub fn generate( | 2186 | pub fn generate( |
| ... | @@ -2869,110 +2199,109 @@ pub fn generate( | ... | @@ -2869,110 +2199,109 @@ pub fn generate( |
| 2869 | | 2199 | |
| 2870 | const func = zcu.funcInfo(func_index); | 2200 | const func = zcu.funcInfo(func_index); |
| 2871 | | 2201 | |
| | 2202 | var arena: std.heap.ArenaAllocator = .init(gpa); |
| | 2203 | defer arena.deinit(); |
| | 2204 | |
| 2872 | var function: Function = .{ | 2205 | var function: Function = .{ |
| 2873 | .value_map = .init(gpa), | 2206 | .value_map = .init(gpa), |
| 2874 | .air = air.*, | 2207 | .air = air.*, |
| 2875 | .liveness = liveness.*.?, | 2208 | .liveness = liveness.*.?, |
| 2876 | .func_index = func_index, | 2209 | .func_index = func_index, |
| 2877 | .object = .{ | 2210 | .dg = .{ |
| 2878 | .dg = .{ | 2211 | .gpa = gpa, |
| 2879 | .gpa = gpa, | 2212 | .arena = arena.allocator(), |
| 2880 | .pt = pt, | 2213 | .pt = pt, |
| 2881 | .mod = zcu.navFileScope(func.owner_nav).mod.?, | 2214 | .mod = zcu.navFileScope(func.owner_nav).mod.?, |
| 2882 | .error_msg = null, | 2215 | .error_msg = null, |
| 2883 | .pass = .{ .nav = func.owner_nav }, | 2216 | .owner_nav = func.owner_nav.toOptional(), |
| 2884 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, | 2217 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, |
| 2885 | .expected_block = null, | 2218 | .expected_block = null, |
| 2886 | .fwd_decl = .init(gpa), | 2219 | .ctype_deps = .empty, |
| 2887 | .ctype_pool = .empty, | 2220 | .uavs = .empty, |
| 2888 | .scratch = .empty, | | |
| 2889 | .uavs = .empty, | | |
| 2890 | }, | | |
| 2891 | .code_header = .init(gpa), | | |
| 2892 | .code = .init(gpa), | | |
| 2893 | .indent_counter = 0, | | |
| 2894 | }, | 2221 | }, |
| 2895 | .lazy_fns = .empty, | 2222 | .code = .init(gpa), |
| | 2223 | .indent_counter = 0, |
| | 2224 | .need_tag_name_funcs = .empty, |
| | 2225 | .need_never_tail_funcs = .empty, |
| | 2226 | .need_never_inline_funcs = .empty, |
| 2896 | }; | 2227 | }; |
| 2897 | defer { | 2228 | defer { |
| 2898 | function.object.code_header.deinit(); | 2229 | function.code.deinit(); |
| 2899 | function.object.code.deinit(); | 2230 | function.dg.ctype_deps.deinit(gpa); |
| 2900 | function.object.dg.fwd_decl.deinit(); | 2231 | function.dg.uavs.deinit(gpa); |
| 2901 | function.object.dg.ctype_pool.deinit(gpa); | | |
| 2902 | function.object.dg.scratch.deinit(gpa); | | |
| 2903 | function.object.dg.uavs.deinit(gpa); | | |
| 2904 | function.deinit(); | 2232 | function.deinit(); |
| 2905 | } | 2233 | } |
| 2906 | try function.object.dg.ctype_pool.init(gpa); | | |
| 2907 | | 2234 | |
| 2908 | genFunc(&function) catch |err| switch (err) { | 2235 | var fwd_decl: Writer.Allocating = .init(gpa); |
| 2909 | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.object.dg.error_msg.?), | 2236 | defer fwd_decl.deinit(); |
| 2910 | error.OutOfMemory => return error.OutOfMemory, | 2237 | |
| | 2238 | var code_header: Writer.Allocating = .init(gpa); |
| | 2239 | defer code_header.deinit(); |
| | 2240 | |
| | 2241 | genFunc(&function, &fwd_decl.writer, &code_header.writer) catch |err| switch (err) { |
| | 2242 | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.dg.error_msg.?), |
| 2911 | error.WriteFailed => return error.OutOfMemory, | 2243 | error.WriteFailed => return error.OutOfMemory, |
| | 2244 | error.OutOfMemory => |e| return e, |
| 2912 | }; | 2245 | }; |
| 2913 | | 2246 | |
| 2914 | var mir: Mir = .{ | 2247 | var mir: Mir = .{ |
| 2915 | .uavs = .empty, | | |
| 2916 | .code = &.{}, | | |
| 2917 | .code_header = &.{}, | | |
| 2918 | .fwd_decl = &.{}, | 2248 | .fwd_decl = &.{}, |
| 2919 | .ctype_pool = .empty, | 2249 | .code_header = &.{}, |
| 2920 | .lazy_fns = .empty, | 2250 | .code = &.{}, |
| | 2251 | .ctype_deps = function.dg.ctype_deps.move(), |
| | 2252 | .need_uavs = function.dg.uavs.move(), |
| | 2253 | .need_tag_name_funcs = function.need_tag_name_funcs.move(), |
| | 2254 | .need_never_tail_funcs = function.need_never_tail_funcs.move(), |
| | 2255 | .need_never_inline_funcs = function.need_never_inline_funcs.move(), |
| 2921 | }; | 2256 | }; |
| 2922 | errdefer mir.deinit(gpa); | 2257 | errdefer mir.deinit(gpa); |
| 2923 | mir.uavs = function.object.dg.uavs.move(); | 2258 | mir.fwd_decl = try fwd_decl.toOwnedSlice(); |
| 2924 | mir.code_header = try function.object.code_header.toOwnedSlice(); | 2259 | mir.code_header = try code_header.toOwnedSlice(); |
| 2925 | mir.code = try function.object.code.toOwnedSlice(); | 2260 | mir.code = try function.code.toOwnedSlice(); |
| 2926 | mir.fwd_decl = try function.object.dg.fwd_decl.toOwnedSlice(); | | |
| 2927 | mir.ctype_pool = function.object.dg.ctype_pool.move(); | | |
| 2928 | mir.lazy_fns = function.lazy_fns.move(); | | |
| 2929 | return mir; | 2261 | return mir; |
| 2930 | } | 2262 | } |
| 2931 | | 2263 | |
| 2932 | pub fn genFunc(f: *Function) Error!void { | 2264 | pub fn genFunc(f: *Function, fwd_decl_writer: *Writer, header_writer: *Writer) Error!void { |
| 2933 | const tracy = trace(@src()); | 2265 | const tracy = trace(@src()); |
| 2934 | defer tracy.end(); | 2266 | defer tracy.end(); |
| 2935 | | 2267 | |
| 2936 | const o = &f.object; | 2268 | const zcu = f.dg.pt.zcu; |
| 2937 | const zcu = o.dg.pt.zcu; | | |
| 2938 | const ip = &zcu.intern_pool; | 2269 | const ip = &zcu.intern_pool; |
| 2939 | const gpa = o.dg.gpa; | 2270 | const gpa = f.dg.gpa; |
| 2940 | const nav_index = o.dg.pass.nav; | 2271 | const nav_index = f.dg.owner_nav.unwrap().?; |
| 2941 | const nav_val = zcu.navValue(nav_index); | 2272 | const nav_val = zcu.navValue(nav_index); |
| 2942 | const nav = ip.getNav(nav_index); | 2273 | const nav = ip.getNav(nav_index); |
| 2943 | | 2274 | |
| 2944 | const fwd = &o.dg.fwd_decl.writer; | 2275 | try fwd_decl_writer.writeAll("static "); |
| 2945 | try fwd.writeAll("static "); | 2276 | try f.dg.renderFunctionSignature( |
| 2946 | try o.dg.renderFunctionSignature( | 2277 | fwd_decl_writer, |
| 2947 | fwd, | | |
| 2948 | nav_val, | 2278 | nav_val, |
| 2949 | nav.status.fully_resolved.alignment, | 2279 | nav.status.fully_resolved.alignment, |
| 2950 | .forward, | 2280 | .forward_decl, |
| 2951 | .{ .nav = nav_index }, | 2281 | .{ .nav = nav_index }, |
| 2952 | ); | 2282 | ); |
| 2953 | try fwd.writeAll(";\n"); | 2283 | try fwd_decl_writer.writeAll(";\n"); |
| 2954 | | 2284 | |
| 2955 | const ch = &o.code_header.writer; | | |
| 2956 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| | 2285 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| |
| 2957 | try ch.print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); | 2286 | try header_writer.print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); |
| 2958 | try o.dg.renderFunctionSignature( | 2287 | try f.dg.renderFunctionSignature( |
| 2959 | ch, | 2288 | header_writer, |
| 2960 | nav_val, | 2289 | nav_val, |
| 2961 | .none, | 2290 | .none, |
| 2962 | .complete, | 2291 | .definition, |
| 2963 | .{ .nav = nav_index }, | 2292 | .{ .nav = nav_index }, |
| 2964 | ); | 2293 | ); |
| 2965 | try ch.writeAll(" {\n "); | 2294 | try header_writer.writeAll(" {\n "); |
| 2966 | | 2295 | |
| 2967 | f.free_locals_map.clearRetainingCapacity(); | 2296 | f.free_locals_map.clearRetainingCapacity(); |
| 2968 | | 2297 | |
| 2969 | const main_body = f.air.getMainBody(); | 2298 | const main_body = f.air.getMainBody(); |
| 2970 | o.indent(); | 2299 | f.indent(); |
| 2971 | try genBodyResolveState(f, undefined, &.{}, main_body, true); | 2300 | try genBodyResolveState(f, undefined, &.{}, main_body, true); |
| 2972 | try o.outdent(); | 2301 | try f.outdent(); |
| 2973 | try o.code.writer.writeByte('}'); | 2302 | try f.code.writer.writeByte('}'); |
| 2974 | try o.newline(); | 2303 | try f.newline(); |
| 2975 | if (o.dg.expected_block) |_| | 2304 | if (f.dg.expected_block) |_| |
| 2976 | return f.fail("runtime code not allowed in naked function", .{}); | 2305 | return f.fail("runtime code not allowed in naked function", .{}); |
| 2977 | | 2306 | |
| 2978 | // Take advantage of the free_locals map to bucket locals per type. All | 2307 | // Take advantage of the free_locals map to bucket locals per type. All |
| ... | @@ -2986,155 +2315,204 @@ pub fn genFunc(f: *Function) Error!void { | ... | @@ -2986,155 +2315,204 @@ pub fn genFunc(f: *Function) Error!void { |
| 2986 | if (!should_emit) continue; | 2315 | if (!should_emit) continue; |
| 2987 | const local = f.locals.items[local_index]; | 2316 | const local = f.locals.items[local_index]; |
| 2988 | log.debug("inserting local {d} into free_locals", .{local_index}); | 2317 | log.debug("inserting local {d} into free_locals", .{local_index}); |
| 2989 | const gop = try free_locals.getOrPut(gpa, local.getType()); | 2318 | const gop = try free_locals.getOrPut(gpa, local); |
| 2990 | if (!gop.found_existing) gop.value_ptr.* = .{}; | 2319 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 2991 | try gop.value_ptr.putNoClobber(gpa, local_index, {}); | 2320 | try gop.value_ptr.putNoClobber(gpa, local_index, {}); |
| 2992 | } | 2321 | } |
| 2993 | | 2322 | |
| 2994 | const SortContext = struct { | 2323 | const SortContext = struct { |
| | 2324 | zcu: *const Zcu, |
| 2995 | keys: []const LocalType, | 2325 | keys: []const LocalType, |
| 2996 | | 2326 | |
| 2997 | pub fn lessThan(ctx: @This(), lhs_index: usize, rhs_index: usize) bool { | 2327 | pub fn lessThan(ctx: @This(), lhs_index: usize, rhs_index: usize) bool { |
| 2998 | const lhs_ty = ctx.keys[lhs_index]; | 2328 | const lhs = ctx.keys[lhs_index]; |
| 2999 | const rhs_ty = ctx.keys[rhs_index]; | 2329 | const rhs = ctx.keys[rhs_index]; |
| 3000 | return lhs_ty.alignas.order(rhs_ty.alignas).compare(.gt); | 2330 | const lhs_align = switch (lhs.alignment) { |
| | 2331 | .none => lhs.type.abiAlignment(ctx.zcu), |
| | 2332 | else => |a| a, |
| | 2333 | }; |
| | 2334 | const rhs_align = switch (rhs.alignment) { |
| | 2335 | .none => rhs.type.abiAlignment(ctx.zcu), |
| | 2336 | else => |a| a, |
| | 2337 | }; |
| | 2338 | return Alignment.compareStrict(lhs_align, .gt, rhs_align); |
| 3001 | } | 2339 | } |
| 3002 | }; | 2340 | }; |
| 3003 | free_locals.sort(SortContext{ .keys = free_locals.keys() }); | 2341 | free_locals.sort(SortContext{ |
| | 2342 | .zcu = zcu, |
| | 2343 | .keys = free_locals.keys(), |
| | 2344 | }); |
| 3004 | | 2345 | |
| 3005 | for (free_locals.values()) |list| { | 2346 | for (free_locals.values()) |list| { |
| 3006 | for (list.keys()) |local_index| { | 2347 | for (list.keys()) |local_index| { |
| 3007 | const local = f.locals.items[local_index]; | 2348 | const local = f.locals.items[local_index]; |
| 3008 | try o.dg.renderCTypeAndName(ch, local.ctype, .{ .local = local_index }, .{}, local.flags.alignas); | 2349 | try f.dg.renderTypeAndName(header_writer, local.type, .{ .local = local_index }, .{}, local.alignment); |
| 3009 | try ch.writeAll(";\n "); | 2350 | try header_writer.writeAll(";\n "); |
| 3010 | } | 2351 | } |
| 3011 | } | 2352 | } |
| 3012 | } | 2353 | } |
| 3013 | | 2354 | |
| 3014 | pub fn genDecl(o: *Object) Error!void { | 2355 | pub fn genDecl(dg: *DeclGen, w: *Writer) Error!void { |
| 3015 | const tracy = trace(@src()); | 2356 | const tracy = trace(@src()); |
| 3016 | defer tracy.end(); | 2357 | defer tracy.end(); |
| 3017 | | 2358 | |
| 3018 | const pt = o.dg.pt; | 2359 | const pt = dg.pt; |
| 3019 | const zcu = pt.zcu; | 2360 | const zcu = pt.zcu; |
| 3020 | const ip = &zcu.intern_pool; | 2361 | const ip = &zcu.intern_pool; |
| 3021 | const nav = ip.getNav(o.dg.pass.nav); | 2362 | const nav = ip.getNav(dg.owner_nav.unwrap().?); |
| 3022 | const nav_ty: Type = .fromInterned(nav.typeOf(ip)); | 2363 | const nav_ty: Type = .fromInterned(nav.typeOf(ip)); |
| 3023 | | 2364 | |
| 3024 | if (!nav_ty.hasRuntimeBits(zcu)) return; | 2365 | const is_const: bool, const is_threadlocal: bool, const init_val: Value = switch (ip.indexToKey(nav.status.fully_resolved.val)) { |
| 3025 | switch (ip.indexToKey(nav.status.fully_resolved.val)) { | 2366 | else => .{ true, false, .fromInterned(nav.status.fully_resolved.val) }, |
| 3026 | .@"extern" => |@"extern"| { | 2367 | .variable => |v| .{ false, v.is_threadlocal, .fromInterned(v.init) }, |
| 3027 | if (!ip.isFunctionType(nav_ty.toIntern())) return o.dg.renderFwdDecl(o.dg.pass.nav, .{ | 2368 | .@"extern" => return, |
| 3028 | .is_const = @"extern".is_const, | 2369 | }; |
| 3029 | .is_threadlocal = @"extern".is_threadlocal, | | |
| 3030 | .linkage = @"extern".linkage, | | |
| 3031 | .visibility = @"extern".visibility, | | |
| 3032 | }); | | |
| 3033 | | 2370 | |
| 3034 | const fwd = &o.dg.fwd_decl.writer; | 2371 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| { |
| 3035 | try fwd.writeAll("zig_extern "); | 2372 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); |
| 3036 | try o.dg.renderFunctionSignature( | | |
| 3037 | fwd, | | |
| 3038 | Value.fromInterned(nav.status.fully_resolved.val), | | |
| 3039 | nav.status.fully_resolved.alignment, | | |
| 3040 | .forward, | | |
| 3041 | .{ .@"export" = .{ | | |
| 3042 | .main_name = nav.name, | | |
| 3043 | .extern_name = nav.name, | | |
| 3044 | } }, | | |
| 3045 | ); | | |
| 3046 | try fwd.writeAll(";\n"); | | |
| 3047 | }, | | |
| 3048 | .variable => |variable| { | | |
| 3049 | try o.dg.renderFwdDecl(o.dg.pass.nav, .{ | | |
| 3050 | .is_const = false, | | |
| 3051 | .is_threadlocal = variable.is_threadlocal, | | |
| 3052 | .linkage = .internal, | | |
| 3053 | .visibility = .default, | | |
| 3054 | }); | | |
| 3055 | const w = &o.code.writer; | | |
| 3056 | if (variable.is_threadlocal and !o.dg.mod.single_threaded) try w.writeAll("zig_threadlocal "); | | |
| 3057 | if (nav.status.fully_resolved.@"linksection".toSlice(&zcu.intern_pool)) |s| | | |
| 3058 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); | | |
| 3059 | try o.dg.renderTypeAndName( | | |
| 3060 | w, | | |
| 3061 | nav_ty, | | |
| 3062 | .{ .nav = o.dg.pass.nav }, | | |
| 3063 | .{}, | | |
| 3064 | nav.status.fully_resolved.alignment, | | |
| 3065 | .complete, | | |
| 3066 | ); | | |
| 3067 | try w.writeAll(" = "); | | |
| 3068 | try o.dg.renderValue(w, Value.fromInterned(variable.init), .StaticInitializer); | | |
| 3069 | try w.writeByte(';'); | | |
| 3070 | try o.newline(); | | |
| 3071 | }, | | |
| 3072 | else => try genDeclValue( | | |
| 3073 | o, | | |
| 3074 | Value.fromInterned(nav.status.fully_resolved.val), | | |
| 3075 | .{ .nav = o.dg.pass.nav }, | | |
| 3076 | nav.status.fully_resolved.alignment, | | |
| 3077 | nav.status.fully_resolved.@"linksection", | | |
| 3078 | ), | | |
| 3079 | } | 2373 | } |
| | 2374 | |
| | 2375 | // We don't bother underaligning---it's unnecessary and hurts compatibility. |
| | 2376 | const a = nav.status.fully_resolved.alignment; |
| | 2377 | if (a != .none and a.compareStrict(.gt, nav_ty.abiAlignment(zcu))) { |
| | 2378 | try w.print("zig_align({d}) ", .{a.toByteUnits().?}); |
| | 2379 | } |
| | 2380 | |
| | 2381 | try genDeclValue(dg, w, .{ |
| | 2382 | .name = .{ .nav = dg.owner_nav.unwrap().? }, |
| | 2383 | .@"const" = is_const, |
| | 2384 | .@"threadlocal" = is_threadlocal, |
| | 2385 | .init_val = init_val, |
| | 2386 | }); |
| 3080 | } | 2387 | } |
| | 2388 | pub fn genDeclFwd(dg: *DeclGen, w: *Writer) Error!void { |
| | 2389 | const tracy = trace(@src()); |
| | 2390 | defer tracy.end(); |
| 3081 | | 2391 | |
| 3082 | pub fn genDeclValue( | 2392 | const pt = dg.pt; |
| 3083 | o: *Object, | 2393 | const zcu = pt.zcu; |
| 3084 | val: Value, | 2394 | const ip = &zcu.intern_pool; |
| 3085 | decl_c_value: CValue, | 2395 | const nav = ip.getNav(dg.owner_nav.unwrap().?); |
| 3086 | alignment: Alignment, | 2396 | const nav_ty: Type = .fromInterned(nav.typeOf(ip)); |
| 3087 | @"linksection": InternPool.OptionalNullTerminatedString, | | |
| 3088 | ) Error!void { | | |
| 3089 | const zcu = o.dg.pt.zcu; | | |
| 3090 | const ty = val.typeOf(zcu); | | |
| 3091 | | 2397 | |
| 3092 | const fwd = &o.dg.fwd_decl.writer; | 2398 | const is_const: bool, const is_threadlocal: bool, const init_val: Value = switch (ip.indexToKey(nav.status.fully_resolved.val)) { |
| 3093 | try fwd.writeAll("static "); | 2399 | else => .{ true, false, .fromInterned(nav.status.fully_resolved.val) }, |
| 3094 | try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete); | 2400 | .variable => |v| .{ false, v.is_threadlocal, .fromInterned(v.init) }, |
| 3095 | try fwd.writeAll(";\n"); | | |
| 3096 | | 2401 | |
| 3097 | const w = &o.code.writer; | 2402 | .@"extern" => |@"extern"| switch (nav_ty.zigTypeTag(zcu)) { |
| 3098 | if (@"linksection".toSlice(&zcu.intern_pool)) |s| | 2403 | .@"fn" => { |
| 3099 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); | 2404 | try w.writeAll("zig_extern "); |
| 3100 | try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete); | 2405 | try dg.renderFunctionSignature( |
| | 2406 | w, |
| | 2407 | Value.fromInterned(nav.status.fully_resolved.val), |
| | 2408 | nav.status.fully_resolved.alignment, |
| | 2409 | .forward_decl, |
| | 2410 | .{ .@"export" = .{ |
| | 2411 | .main_name = nav.name, |
| | 2412 | .extern_name = nav.name, |
| | 2413 | } }, |
| | 2414 | ); |
| | 2415 | try w.writeAll(";\n"); |
| | 2416 | return; |
| | 2417 | }, |
| | 2418 | else => { |
| | 2419 | switch (@"extern".linkage) { |
| | 2420 | .internal => try w.writeAll("static "), |
| | 2421 | .strong => try w.print("zig_extern zig_visibility({t}) ", .{@"extern".visibility}), |
| | 2422 | .weak => try w.print("zig_extern zig_weak_linkage zig_visibility({t}) ", .{@"extern".visibility}), |
| | 2423 | .link_once => return dg.fail("TODO: CBE: implement linkonce linkage?", .{}), |
| | 2424 | } |
| | 2425 | if (@"extern".is_threadlocal and !dg.mod.single_threaded) { |
| | 2426 | try w.writeAll("zig_threadlocal "); |
| | 2427 | } |
| | 2428 | try dg.renderTypeAndName( |
| | 2429 | w, |
| | 2430 | .fromInterned(nav.typeOf(ip)), |
| | 2431 | .{ .nav = dg.owner_nav.unwrap().? }, |
| | 2432 | .{ .@"const" = @"extern".is_const }, |
| | 2433 | nav.getAlignment(), |
| | 2434 | ); |
| | 2435 | try w.writeAll(";\n"); |
| | 2436 | return; |
| | 2437 | }, |
| | 2438 | }, |
| | 2439 | }; |
| | 2440 | |
| | 2441 | // We don't bother underaligning---it's unnecessary and hurts compatibility. |
| | 2442 | const a = nav.status.fully_resolved.alignment; |
| | 2443 | if (a != .none and a.compareStrict(.gt, nav_ty.abiAlignment(zcu))) { |
| | 2444 | try w.print("zig_align({d}) ", .{a.toByteUnits().?}); |
| | 2445 | } |
| | 2446 | |
| | 2447 | try genDeclValueFwd(dg, w, .{ |
| | 2448 | .name = .{ .nav = dg.owner_nav.unwrap().? }, |
| | 2449 | .@"const" = is_const, |
| | 2450 | .@"threadlocal" = is_threadlocal, |
| | 2451 | .init_val = init_val, |
| | 2452 | }); |
| | 2453 | } |
| | 2454 | pub fn genDeclValue(dg: *DeclGen, w: *Writer, options: struct { |
| | 2455 | name: CValue, |
| | 2456 | @"const": bool, |
| | 2457 | @"threadlocal": bool, |
| | 2458 | init_val: Value, |
| | 2459 | }) Error!void { |
| | 2460 | const zcu = dg.pt.zcu; |
| | 2461 | const ty = options.init_val.typeOf(zcu); |
| | 2462 | if (options.@"threadlocal" and !dg.mod.single_threaded) { |
| | 2463 | try w.writeAll("zig_threadlocal "); |
| | 2464 | } |
| | 2465 | try dg.renderTypeAndName(w, ty, options.name, .{ .@"const" = options.@"const" }, .none); |
| 3101 | try w.writeAll(" = "); | 2466 | try w.writeAll(" = "); |
| 3102 | try o.dg.renderValue(w, val, .StaticInitializer); | 2467 | try dg.renderValue(w, options.init_val, .static_initializer); |
| 3103 | try w.writeByte(';'); | 2468 | try w.writeAll(";\n"); |
| 3104 | try o.newline(); | 2469 | } |
| | 2470 | pub fn genDeclValueFwd(dg: *DeclGen, w: *Writer, options: struct { |
| | 2471 | name: CValue, |
| | 2472 | @"const": bool, |
| | 2473 | @"threadlocal": bool, |
| | 2474 | init_val: Value, |
| | 2475 | }) Error!void { |
| | 2476 | const zcu = dg.pt.zcu; |
| | 2477 | const ty = options.init_val.typeOf(zcu); |
| | 2478 | try w.writeAll("static "); |
| | 2479 | if (options.@"threadlocal" and !dg.mod.single_threaded) { |
| | 2480 | try w.writeAll("zig_threadlocal "); |
| | 2481 | } |
| | 2482 | try dg.renderTypeAndName(w, ty, options.name, .{ .@"const" = options.@"const" }, .none); |
| | 2483 | try w.writeAll(";\n"); |
| 3105 | } | 2484 | } |
| 3106 | | 2485 | |
| 3107 | pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const Zcu.Export.Index) !void { | 2486 | pub fn genExports(dg: *DeclGen, w: *Writer, exported: Zcu.Exported, export_indices: []const Zcu.Export.Index) !void { |
| 3108 | const zcu = dg.pt.zcu; | 2487 | const zcu = dg.pt.zcu; |
| 3109 | const ip = &zcu.intern_pool; | 2488 | const ip = &zcu.intern_pool; |
| 3110 | const fwd = &dg.fwd_decl.writer; | | |
| 3111 | | 2489 | |
| 3112 | const main_name = export_indices[0].ptr(zcu).opts.name; | 2490 | const main_name = export_indices[0].ptr(zcu).opts.name; |
| 3113 | try fwd.writeAll("#define "); | 2491 | try w.writeAll("#define "); |
| 3114 | switch (exported) { | 2492 | switch (exported) { |
| 3115 | .nav => |nav| try dg.renderNavName(fwd, nav), | 2493 | .nav => |nav| try renderNavName(w, nav, ip), |
| 3116 | .uav => |uav| try DeclGen.renderUavName(fwd, Value.fromInterned(uav)), | 2494 | .uav => |uav| try renderUavName(w, Value.fromInterned(uav)), |
| 3117 | } | 2495 | } |
| 3118 | try fwd.writeByte(' '); | 2496 | try w.writeByte(' '); |
| 3119 | try fwd.print("{f}", .{fmtIdentSolo(main_name.toSlice(ip))}); | 2497 | try w.print("{f}", .{fmtIdentSolo(main_name.toSlice(ip))}); |
| 3120 | try fwd.writeByte('\n'); | 2498 | try w.writeByte('\n'); |
| 3121 | | 2499 | |
| 3122 | const exported_val = exported.getValue(zcu); | 2500 | const exported_val = exported.getValue(zcu); |
| 3123 | if (ip.isFunctionType(exported_val.typeOf(zcu).toIntern())) return for (export_indices) |export_index| { | 2501 | if (ip.isFunctionType(exported_val.typeOf(zcu).toIntern())) return for (export_indices) |export_index| { |
| 3124 | const @"export" = export_index.ptr(zcu); | 2502 | const @"export" = export_index.ptr(zcu); |
| 3125 | try fwd.writeAll("zig_extern "); | 2503 | try w.writeAll("zig_extern "); |
| 3126 | if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage_fn "); | 2504 | if (@"export".opts.linkage == .weak) try w.writeAll("zig_weak_linkage_fn "); |
| 3127 | try dg.renderFunctionSignature( | 2505 | try dg.renderFunctionSignature( |
| 3128 | fwd, | 2506 | w, |
| 3129 | exported.getValue(zcu), | 2507 | exported.getValue(zcu), |
| 3130 | exported.getAlign(zcu), | 2508 | exported.getAlign(zcu), |
| 3131 | .forward, | 2509 | .forward_decl, |
| 3132 | .{ .@"export" = .{ | 2510 | .{ .@"export" = .{ |
| 3133 | .main_name = main_name, | 2511 | .main_name = main_name, |
| 3134 | .extern_name = @"export".opts.name, | 2512 | .extern_name = @"export".opts.name, |
| 3135 | } }, | 2513 | } }, |
| 3136 | ); | 2514 | ); |
| 3137 | try fwd.writeAll(";\n"); | 2515 | try w.writeAll(";\n"); |
| 3138 | }; | 2516 | }; |
| 3139 | const is_const = switch (ip.indexToKey(exported_val.toIntern())) { | 2517 | const is_const = switch (ip.indexToKey(exported_val.toIntern())) { |
| 3140 | .func => unreachable, | 2518 | .func => unreachable, |
| ... | @@ -3144,39 +2522,38 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const | ... | @@ -3144,39 +2522,38 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const |
| 3144 | }; | 2522 | }; |
| 3145 | for (export_indices) |export_index| { | 2523 | for (export_indices) |export_index| { |
| 3146 | const @"export" = export_index.ptr(zcu); | 2524 | const @"export" = export_index.ptr(zcu); |
| 3147 | try fwd.writeAll("zig_extern "); | 2525 | try w.writeAll("zig_extern "); |
| 3148 | if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage "); | 2526 | if (@"export".opts.linkage == .weak) try w.writeAll("zig_weak_linkage "); |
| 3149 | if (@"export".opts.section.toSlice(ip)) |s| try fwd.print("zig_linksection({f}) ", .{ | 2527 | if (@"export".opts.section.toSlice(ip)) |s| try w.print("zig_linksection({f}) ", .{ |
| 3150 | fmtStringLiteral(s, null), | 2528 | fmtStringLiteral(s, null), |
| 3151 | }); | 2529 | }); |
| 3152 | const extern_name = @"export".opts.name.toSlice(ip); | 2530 | const extern_name = @"export".opts.name.toSlice(ip); |
| 3153 | const is_mangled = isMangledIdent(extern_name, true); | 2531 | const is_mangled = isMangledIdent(extern_name, true); |
| 3154 | const is_export = @"export".opts.name != main_name; | 2532 | const is_export = @"export".opts.name != main_name; |
| 3155 | try dg.renderTypeAndName( | 2533 | try dg.renderTypeAndName( |
| 3156 | fwd, | 2534 | w, |
| 3157 | exported.getValue(zcu).typeOf(zcu), | 2535 | exported.getValue(zcu).typeOf(zcu), |
| 3158 | .{ .identifier = extern_name }, | 2536 | .{ .identifier = extern_name }, |
| 3159 | CQualifiers.init(.{ .@"const" = is_const }), | 2537 | .{ .@"const" = is_const }, |
| 3160 | exported.getAlign(zcu), | 2538 | exported.getAlign(zcu), |
| 3161 | .complete, | | |
| 3162 | ); | 2539 | ); |
| 3163 | if (is_mangled and is_export) { | 2540 | if (is_mangled and is_export) { |
| 3164 | try fwd.print(" zig_mangled_export({f}, {f}, {f})", .{ | 2541 | try w.print(" zig_mangled_export({f}, {f}, {f})", .{ |
| 3165 | fmtIdentSolo(extern_name), | 2542 | fmtIdentSolo(extern_name), |
| 3166 | fmtStringLiteral(extern_name, null), | 2543 | fmtStringLiteral(extern_name, null), |
| 3167 | fmtStringLiteral(main_name.toSlice(ip), null), | 2544 | fmtStringLiteral(main_name.toSlice(ip), null), |
| 3168 | }); | 2545 | }); |
| 3169 | } else if (is_mangled) { | 2546 | } else if (is_mangled) { |
| 3170 | try fwd.print(" zig_mangled({f}, {f})", .{ | 2547 | try w.print(" zig_mangled({f}, {f})", .{ |
| 3171 | fmtIdentSolo(extern_name), fmtStringLiteral(extern_name, null), | 2548 | fmtIdentSolo(extern_name), fmtStringLiteral(extern_name, null), |
| 3172 | }); | 2549 | }); |
| 3173 | } else if (is_export) { | 2550 | } else if (is_export) { |
| 3174 | try fwd.print(" zig_export({f}, {f})", .{ | 2551 | try w.print(" zig_export({f}, {f})", .{ |
| 3175 | fmtStringLiteral(main_name.toSlice(ip), null), | 2552 | fmtStringLiteral(main_name.toSlice(ip), null), |
| 3176 | fmtStringLiteral(extern_name, null), | 2553 | fmtStringLiteral(extern_name, null), |
| 3177 | }); | 2554 | }); |
| 3178 | } | 2555 | } |
| 3179 | try fwd.writeAll(";\n"); | 2556 | try w.writeAll(";\n"); |
| 3180 | } | 2557 | } |
| 3181 | } | 2558 | } |
| 3182 | | 2559 | |
| ... | @@ -3185,15 +2562,15 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const | ... | @@ -3185,15 +2562,15 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const |
| 3185 | /// have been added to `free_locals_map`. For a version of this function that restores this state, | 2562 | /// have been added to `free_locals_map`. For a version of this function that restores this state, |
| 3186 | /// see `genBodyResolveState`. | 2563 | /// see `genBodyResolveState`. |
| 3187 | fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { | 2564 | fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3188 | const w = &f.object.code.writer; | 2565 | const w = &f.code.writer; |
| 3189 | if (body.len == 0) { | 2566 | if (body.len == 0) { |
| 3190 | try w.writeAll("{}"); | 2567 | try w.writeAll("{}"); |
| 3191 | } else { | 2568 | } else { |
| 3192 | try w.writeByte('{'); | 2569 | try w.writeByte('{'); |
| 3193 | f.object.indent(); | 2570 | f.indent(); |
| 3194 | try f.object.newline(); | 2571 | try f.newline(); |
| 3195 | try genBodyInner(f, body); | 2572 | try genBodyInner(f, body); |
| 3196 | try f.object.outdent(); | 2573 | try f.outdent(); |
| 3197 | try w.writeByte('}'); | 2574 | try w.writeByte('}'); |
| 3198 | } | 2575 | } |
| 3199 | } | 2576 | } |
| ... | @@ -3207,13 +2584,13 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { | ... | @@ -3207,13 +2584,13 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3207 | fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []const Air.Inst.Index, body: []const Air.Inst.Index, inner: bool) Error!void { | 2584 | fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []const Air.Inst.Index, body: []const Air.Inst.Index, inner: bool) Error!void { |
| 3208 | if (body.len == 0) { | 2585 | if (body.len == 0) { |
| 3209 | // Don't go to the expense of cloning everything! | 2586 | // Don't go to the expense of cloning everything! |
| 3210 | if (!inner) try f.object.code.writer.writeAll("{}"); | 2587 | if (!inner) try f.code.writer.writeAll("{}"); |
| 3211 | return; | 2588 | return; |
| 3212 | } | 2589 | } |
| 3213 | | 2590 | |
| 3214 | // TODO: we can probably avoid the copies in some other common cases too. | 2591 | // TODO: we can probably avoid the copies in some other common cases too. |
| 3215 | | 2592 | |
| 3216 | const gpa = f.object.dg.gpa; | 2593 | const gpa = f.dg.gpa; |
| 3217 | | 2594 | |
| 3218 | // Save the original value_map and free_locals_map so that we can restore them after the body. | 2595 | // Save the original value_map and free_locals_map so that we can restore them after the body. |
| 3219 | var old_value_map = try f.value_map.clone(); | 2596 | var old_value_map = try f.value_map.clone(); |
| ... | @@ -3254,13 +2631,13 @@ fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []con | ... | @@ -3254,13 +2631,13 @@ fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []con |
| 3254 | } | 2631 | } |
| 3255 | | 2632 | |
| 3256 | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { | 2633 | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3257 | const zcu = f.object.dg.pt.zcu; | 2634 | const zcu = f.dg.pt.zcu; |
| 3258 | const ip = &zcu.intern_pool; | 2635 | const ip = &zcu.intern_pool; |
| 3259 | const air_tags = f.air.instructions.items(.tag); | 2636 | const air_tags = f.air.instructions.items(.tag); |
| 3260 | const air_datas = f.air.instructions.items(.data); | 2637 | const air_datas = f.air.instructions.items(.data); |
| 3261 | | 2638 | |
| 3262 | for (body) |inst| { | 2639 | for (body) |inst| { |
| 3263 | if (f.object.dg.expected_block) |_| | 2640 | if (f.dg.expected_block) |_| |
| 3264 | return f.fail("runtime code not allowed in naked function", .{}); | 2641 | return f.fail("runtime code not allowed in naked function", .{}); |
| 3265 | if (f.liveness.isUnused(inst) and !f.air.mustLower(inst, ip)) | 2642 | if (f.liveness.isUnused(inst) and !f.air.mustLower(inst, ip)) |
| 3266 | continue; | 2643 | continue; |
| ... | @@ -3529,8 +2906,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { | ... | @@ -3529,8 +2906,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3529 | .ret => return airRet(f, inst, false), | 2906 | .ret => return airRet(f, inst, false), |
| 3530 | .ret_safe => return airRet(f, inst, false), // TODO | 2907 | .ret_safe => return airRet(f, inst, false), // TODO |
| 3531 | .ret_load => return airRet(f, inst, true), | 2908 | .ret_load => return airRet(f, inst, true), |
| 3532 | .trap => return airTrap(f, &f.object.code.writer), | 2909 | .trap => return airTrap(f), |
| 3533 | .unreach => return airUnreach(&f.object), | 2910 | .unreach => return airUnreach(f), |
| 3534 | | 2911 | |
| 3535 | // Instructions which may be `noreturn`. | 2912 | // Instructions which may be `noreturn`. |
| 3536 | .block => res: { | 2913 | .block => res: { |
| ... | @@ -3573,21 +2950,21 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ | ... | @@ -3573,21 +2950,21 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 3573 | const operand = try f.resolveInst(ty_op.operand); | 2950 | const operand = try f.resolveInst(ty_op.operand); |
| 3574 | try reap(f, inst, &.{ty_op.operand}); | 2951 | try reap(f, inst, &.{ty_op.operand}); |
| 3575 | | 2952 | |
| 3576 | const w = &f.object.code.writer; | 2953 | const w = &f.code.writer; |
| 3577 | const local = try f.allocLocal(inst, inst_ty); | 2954 | const local = try f.allocLocal(inst, inst_ty); |
| 3578 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 2955 | try f.writeCValue(w, local, .other); |
| 3579 | try f.writeCValue(w, local, .Other); | 2956 | try w.writeAll(" = "); |
| 3580 | try a.assign(f, w); | | |
| 3581 | if (is_ptr) { | 2957 | if (is_ptr) { |
| 3582 | try w.writeByte('&'); | 2958 | try w.writeByte('&'); |
| 3583 | try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name }); | 2959 | try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name }); |
| 3584 | } else try f.writeCValueMember(w, operand, .{ .identifier = field_name }); | 2960 | } else try f.writeCValueMember(w, operand, .{ .identifier = field_name }); |
| 3585 | try a.end(f, w); | 2961 | try w.writeByte(';'); |
| | 2962 | try f.newline(); |
| 3586 | return local; | 2963 | return local; |
| 3587 | } | 2964 | } |
| 3588 | | 2965 | |
| 3589 | fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | 2966 | fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3590 | const zcu = f.object.dg.pt.zcu; | 2967 | const zcu = f.dg.pt.zcu; |
| 3591 | const inst_ty = f.typeOfIndex(inst); | 2968 | const inst_ty = f.typeOfIndex(inst); |
| 3592 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 2969 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3593 | assert(inst_ty.hasRuntimeBits(zcu)); | 2970 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | @@ -3596,21 +2973,24 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3596,21 +2973,24 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3596 | const index = try f.resolveInst(bin_op.rhs); | 2973 | const index = try f.resolveInst(bin_op.rhs); |
| 3597 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 2974 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3598 | | 2975 | |
| 3599 | const w = &f.object.code.writer; | 2976 | const w = &f.code.writer; |
| 3600 | const local = try f.allocLocal(inst, inst_ty); | 2977 | const local = try f.allocLocal(inst, inst_ty); |
| 3601 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 2978 | try f.writeCValue(w, local, .other); |
| 3602 | try f.writeCValue(w, local, .Other); | 2979 | try w.writeAll(" = "); |
| 3603 | try a.assign(f, w); | 2980 | switch (f.typeOf(bin_op.lhs).ptrSize(zcu)) { |
| 3604 | try f.writeCValue(w, ptr, .Other); | 2981 | .one => try f.writeCValueDerefMember(w, ptr, .{ .identifier = "array" }), |
| | 2982 | .many, .c => try f.writeCValue(w, ptr, .other), |
| | 2983 | .slice => unreachable, |
| | 2984 | } |
| 3605 | try w.writeByte('['); | 2985 | try w.writeByte('['); |
| 3606 | try f.writeCValue(w, index, .Other); | 2986 | try f.writeCValue(w, index, .other); |
| 3607 | try w.writeByte(']'); | 2987 | try w.writeAll("];"); |
| 3608 | try a.end(f, w); | 2988 | try f.newline(); |
| 3609 | return local; | 2989 | return local; |
| 3610 | } | 2990 | } |
| 3611 | | 2991 | |
| 3612 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 2992 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3613 | const pt = f.object.dg.pt; | 2993 | const pt = f.dg.pt; |
| 3614 | const zcu = pt.zcu; | 2994 | const zcu = pt.zcu; |
| 3615 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 2995 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3616 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 2996 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -3623,28 +3003,26 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3623,28 +3003,26 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3623 | const index = try f.resolveInst(bin_op.rhs); | 3003 | const index = try f.resolveInst(bin_op.rhs); |
| 3624 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3004 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3625 | | 3005 | |
| 3626 | const w = &f.object.code.writer; | 3006 | const w = &f.code.writer; |
| 3627 | const local = try f.allocLocal(inst, inst_ty); | 3007 | const local = try f.allocLocal(inst, inst_ty); |
| 3628 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 3008 | try f.writeCValue(w, local, .other); |
| 3629 | try f.writeCValue(w, local, .Other); | 3009 | try w.writeAll(" = "); |
| 3630 | try a.assign(f, w); | | |
| 3631 | try w.writeByte('('); | | |
| 3632 | try f.renderType(w, inst_ty); | | |
| 3633 | try w.writeByte(')'); | | |
| 3634 | try w.writeByte('&'); | 3010 | try w.writeByte('&'); |
| 3635 | if (ptr_ty.ptrSize(zcu) == .one) { | 3011 | if (ptr_ty.ptrSize(zcu) == .one) { |
| 3636 | // It's a pointer to an array, so we need to de-reference. | 3012 | // `*[n]T` was turned into a pointer to `struct { T array[n]; }` |
| 3637 | try f.writeCValueDeref(w, ptr); | 3013 | try f.writeCValueDerefMember(w, ptr, .{ .identifier = "array" }); |
| 3638 | } else try f.writeCValue(w, ptr, .Other); | 3014 | } else { |
| | 3015 | try f.writeCValue(w, ptr, .other); |
| | 3016 | } |
| 3639 | try w.writeByte('['); | 3017 | try w.writeByte('['); |
| 3640 | try f.writeCValue(w, index, .Other); | 3018 | try f.writeCValue(w, index, .other); |
| 3641 | try w.writeByte(']'); | 3019 | try w.writeAll("];"); |
| 3642 | try a.end(f, w); | 3020 | try f.newline(); |
| 3643 | return local; | 3021 | return local; |
| 3644 | } | 3022 | } |
| 3645 | | 3023 | |
| 3646 | fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | 3024 | fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3647 | const zcu = f.object.dg.pt.zcu; | 3025 | const zcu = f.dg.pt.zcu; |
| 3648 | const inst_ty = f.typeOfIndex(inst); | 3026 | const inst_ty = f.typeOfIndex(inst); |
| 3649 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3027 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3650 | assert(inst_ty.hasRuntimeBits(zcu)); | 3028 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | @@ -3653,21 +3031,20 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3653,21 +3031,20 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3653 | const index = try f.resolveInst(bin_op.rhs); | 3031 | const index = try f.resolveInst(bin_op.rhs); |
| 3654 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3032 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3655 | | 3033 | |
| 3656 | const w = &f.object.code.writer; | 3034 | const w = &f.code.writer; |
| 3657 | const local = try f.allocLocal(inst, inst_ty); | 3035 | const local = try f.allocLocal(inst, inst_ty); |
| 3658 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 3036 | try f.writeCValue(w, local, .other); |
| 3659 | try f.writeCValue(w, local, .Other); | 3037 | try w.writeAll(" = "); |
| 3660 | try a.assign(f, w); | | |
| 3661 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); | 3038 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); |
| 3662 | try w.writeByte('['); | 3039 | try w.writeByte('['); |
| 3663 | try f.writeCValue(w, index, .Other); | 3040 | try f.writeCValue(w, index, .other); |
| 3664 | try w.writeByte(']'); | 3041 | try w.writeAll("];"); |
| 3665 | try a.end(f, w); | 3042 | try f.newline(); |
| 3666 | return local; | 3043 | return local; |
| 3667 | } | 3044 | } |
| 3668 | | 3045 | |
| 3669 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 3046 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3670 | const pt = f.object.dg.pt; | 3047 | const pt = f.dg.pt; |
| 3671 | const zcu = pt.zcu; | 3048 | const zcu = pt.zcu; |
| 3672 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 3049 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3673 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3050 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -3681,22 +3058,21 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3681,22 +3058,21 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3681 | const index = try f.resolveInst(bin_op.rhs); | 3058 | const index = try f.resolveInst(bin_op.rhs); |
| 3682 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3059 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3683 | | 3060 | |
| 3684 | const w = &f.object.code.writer; | 3061 | const w = &f.code.writer; |
| 3685 | const local = try f.allocLocal(inst, inst_ty); | 3062 | const local = try f.allocLocal(inst, inst_ty); |
| 3686 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 3063 | try f.writeCValue(w, local, .other); |
| 3687 | try f.writeCValue(w, local, .Other); | 3064 | try w.writeAll(" = "); |
| 3688 | try a.assign(f, w); | | |
| 3689 | try w.writeByte('&'); | 3065 | try w.writeByte('&'); |
| 3690 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); | 3066 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); |
| 3691 | try w.writeByte('['); | 3067 | try w.writeByte('['); |
| 3692 | try f.writeCValue(w, index, .Other); | 3068 | try f.writeCValue(w, index, .other); |
| 3693 | try w.writeByte(']'); | 3069 | try w.writeAll("];"); |
| 3694 | try a.end(f, w); | 3070 | try f.newline(); |
| 3695 | return local; | 3071 | return local; |
| 3696 | } | 3072 | } |
| 3697 | | 3073 | |
| 3698 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | 3074 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3699 | const zcu = f.object.dg.pt.zcu; | 3075 | const zcu = f.dg.pt.zcu; |
| 3700 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3076 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3701 | const inst_ty = f.typeOfIndex(inst); | 3077 | const inst_ty = f.typeOfIndex(inst); |
| 3702 | assert(inst_ty.hasRuntimeBits(zcu)); | 3078 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | @@ -3705,32 +3081,28 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3705,32 +3081,28 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3705 | const index = try f.resolveInst(bin_op.rhs); | 3081 | const index = try f.resolveInst(bin_op.rhs); |
| 3706 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3082 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3707 | | 3083 | |
| 3708 | const w = &f.object.code.writer; | 3084 | const w = &f.code.writer; |
| 3709 | const local = try f.allocLocal(inst, inst_ty); | 3085 | const local = try f.allocLocal(inst, inst_ty); |
| 3710 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 3086 | try f.writeCValue(w, local, .other); |
| 3711 | try f.writeCValue(w, local, .Other); | 3087 | try w.writeAll(" = "); |
| 3712 | try a.assign(f, w); | 3088 | try f.writeCValueMember(w, array, .{ .identifier = "array" }); |
| 3713 | try f.writeCValue(w, array, .Other); | | |
| 3714 | try w.writeByte('['); | 3089 | try w.writeByte('['); |
| 3715 | try f.writeCValue(w, index, .Other); | 3090 | try f.writeCValue(w, index, .other); |
| 3716 | try w.writeByte(']'); | 3091 | try w.writeAll("];"); |
| 3717 | try a.end(f, w); | 3092 | try f.newline(); |
| 3718 | return local; | 3093 | return local; |
| 3719 | } | 3094 | } |
| 3720 | | 3095 | |
| 3721 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { | 3096 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3722 | const pt = f.object.dg.pt; | 3097 | const pt = f.dg.pt; |
| 3723 | const zcu = pt.zcu; | 3098 | const zcu = pt.zcu; |
| 3724 | const inst_ty = f.typeOfIndex(inst); | 3099 | const inst_ty = f.typeOfIndex(inst); |
| 3725 | const elem_ty = inst_ty.childType(zcu); | 3100 | const elem_ty = inst_ty.childType(zcu); |
| 3726 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; | 3101 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; |
| 3727 | | 3102 | |
| 3728 | const local = try f.allocLocalValue(.{ | 3103 | const local = try f.allocLocalValue(.{ |
| 3729 | .ctype = try f.ctypeFromType(elem_ty, .complete), | 3104 | .type = elem_ty, |
| 3730 | .alignas = CType.AlignAs.fromAlignment(.{ | 3105 | .alignment = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3731 | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, | | |
| 3732 | .abi = elem_ty.abiAlignment(zcu), | | |
| 3733 | }), | | |
| 3734 | }); | 3106 | }); |
| 3735 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); | 3107 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3736 | try f.allocs.put(zcu.gpa, local.new_local, true); | 3108 | try f.allocs.put(zcu.gpa, local.new_local, true); |
| ... | @@ -3741,11 +3113,11 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3741,11 +3113,11 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3741 | // For packed aggregates, we zero-initialize to try and work around a design flaw | 3113 | // For packed aggregates, we zero-initialize to try and work around a design flaw |
| 3742 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` | 3114 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` |
| 3743 | // for details. | 3115 | // for details. |
| 3744 | const w = &f.object.code.writer; | 3116 | const w = &f.code.writer; |
| 3745 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); | 3117 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); |
| 3746 | try f.renderType(w, elem_ty); | 3118 | try f.renderType(w, elem_ty); |
| 3747 | try w.writeAll("));"); | 3119 | try w.writeAll("));"); |
| 3748 | try f.object.newline(); | 3120 | try f.newline(); |
| 3749 | }, | 3121 | }, |
| 3750 | .auto, .@"extern" => {}, | 3122 | .auto, .@"extern" => {}, |
| 3751 | }, | 3123 | }, |
| ... | @@ -3756,18 +3128,15 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3756,18 +3128,15 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3756 | } | 3128 | } |
| 3757 | | 3129 | |
| 3758 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 3130 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3759 | const pt = f.object.dg.pt; | 3131 | const pt = f.dg.pt; |
| 3760 | const zcu = pt.zcu; | 3132 | const zcu = pt.zcu; |
| 3761 | const inst_ty = f.typeOfIndex(inst); | 3133 | const inst_ty = f.typeOfIndex(inst); |
| 3762 | const elem_ty = inst_ty.childType(zcu); | 3134 | const elem_ty = inst_ty.childType(zcu); |
| 3763 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; | 3135 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; |
| 3764 | | 3136 | |
| 3765 | const local = try f.allocLocalValue(.{ | 3137 | const local = try f.allocLocalValue(.{ |
| 3766 | .ctype = try f.ctypeFromType(elem_ty, .complete), | 3138 | .type = elem_ty, |
| 3767 | .alignas = CType.AlignAs.fromAlignment(.{ | 3139 | .alignment = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3768 | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, | | |
| 3769 | .abi = elem_ty.abiAlignment(zcu), | | |
| 3770 | }), | | |
| 3771 | }); | 3140 | }); |
| 3772 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); | 3141 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3773 | try f.allocs.put(zcu.gpa, local.new_local, true); | 3142 | try f.allocs.put(zcu.gpa, local.new_local, true); |
| ... | @@ -3778,11 +3147,11 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3778,11 +3147,11 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3778 | // For packed aggregates, we zero-initialize to try and work around a design flaw | 3147 | // For packed aggregates, we zero-initialize to try and work around a design flaw |
| 3779 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` | 3148 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` |
| 3780 | // for details. | 3149 | // for details. |
| 3781 | const w = &f.object.code.writer; | 3150 | const w = &f.code.writer; |
| 3782 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); | 3151 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); |
| 3783 | try f.renderType(w, elem_ty); | 3152 | try f.renderType(w, elem_ty); |
| 3784 | try w.writeAll("));"); | 3153 | try w.writeAll("));"); |
| 3785 | try f.object.newline(); | 3154 | try f.newline(); |
| 3786 | }, | 3155 | }, |
| 3787 | .auto, .@"extern" => {}, | 3156 | .auto, .@"extern" => {}, |
| 3788 | }, | 3157 | }, |
| ... | @@ -3793,24 +3162,18 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3793,24 +3162,18 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3793 | } | 3162 | } |
| 3794 | | 3163 | |
| 3795 | fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { | 3164 | fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3796 | const inst_ty = f.typeOfIndex(inst); | | |
| 3797 | const inst_ctype = try f.ctypeFromType(inst_ty, .parameter); | | |
| 3798 | | | |
| 3799 | const i = f.next_arg_index; | 3165 | const i = f.next_arg_index; |
| 3800 | f.next_arg_index += 1; | 3166 | f.next_arg_index += 1; |
| 3801 | const result: CValue = if (inst_ctype.eql(try f.ctypeFromType(inst_ty, .complete))) | 3167 | const result: CValue = .{ .arg = i }; |
| 3802 | .{ .arg = i } | | |
| 3803 | else | | |
| 3804 | .{ .arg_array = i }; | | |
| 3805 | | 3168 | |
| 3806 | if (f.liveness.isUnused(inst)) { | 3169 | if (f.liveness.isUnused(inst)) { |
| 3807 | const w = &f.object.code.writer; | 3170 | const w = &f.code.writer; |
| 3808 | try w.writeByte('('); | 3171 | try w.writeByte('('); |
| 3809 | try f.renderType(w, .void); | 3172 | try f.renderType(w, .void); |
| 3810 | try w.writeByte(')'); | 3173 | try w.writeByte(')'); |
| 3811 | try f.writeCValue(w, result, .Other); | 3174 | try f.writeCValue(w, result, .other); |
| 3812 | try w.writeByte(';'); | 3175 | try w.writeByte(';'); |
| 3813 | try f.object.newline(); | 3176 | try f.newline(); |
| 3814 | return .none; | 3177 | return .none; |
| 3815 | } | 3178 | } |
| 3816 | | 3179 | |
| ... | @@ -3818,7 +3181,7 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3818,7 +3181,7 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3818 | } | 3181 | } |
| 3819 | | 3182 | |
| 3820 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | 3183 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3821 | const pt = f.object.dg.pt; | 3184 | const pt = f.dg.pt; |
| 3822 | const zcu = pt.zcu; | 3185 | const zcu = pt.zcu; |
| 3823 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 3186 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3824 | | 3187 | |
| ... | @@ -3841,94 +3204,69 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3841,94 +3204,69 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3841 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) | 3204 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) |
| 3842 | else | 3205 | else |
| 3843 | true; | 3206 | true; |
| 3844 | const is_array = lowersToArray(src_ty, zcu); | | |
| 3845 | const need_memcpy = !is_aligned or is_array; | | |
| 3846 | | 3207 | |
| 3847 | const w = &f.object.code.writer; | 3208 | const w = &f.code.writer; |
| 3848 | const local = try f.allocLocal(inst, src_ty); | 3209 | const local = try f.allocLocal(inst, src_ty); |
| 3849 | const v = try Vectorize.start(f, inst, w, ptr_ty); | 3210 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 3850 | | 3211 | |
| 3851 | if (need_memcpy) { | 3212 | if (!is_aligned) { |
| 3852 | try w.writeAll("memcpy("); | 3213 | try w.writeAll("memcpy(&"); |
| 3853 | if (!is_array) try w.writeByte('&'); | 3214 | try f.writeCValue(w, local, .other); |
| 3854 | try f.writeCValue(w, local, .Other); | | |
| 3855 | try v.elem(f, w); | 3215 | try v.elem(f, w); |
| 3856 | try w.writeAll(", (const char *)"); | 3216 | try w.writeAll(", (const char *)"); |
| 3857 | try f.writeCValue(w, operand, .Other); | 3217 | try f.writeCValue(w, operand, .other); |
| 3858 | try v.elem(f, w); | 3218 | try v.elem(f, w); |
| 3859 | try w.writeAll(", sizeof("); | 3219 | try w.writeAll(", sizeof("); |
| 3860 | try f.renderType(w, src_ty); | 3220 | try f.renderType(w, src_ty); |
| 3861 | try w.writeAll("))"); | 3221 | try w.writeAll("))"); |
| 3862 | } else { | 3222 | } else { |
| 3863 | try f.writeCValue(w, local, .Other); | 3223 | try f.writeCValue(w, local, .other); |
| 3864 | try v.elem(f, w); | 3224 | try v.elem(f, w); |
| 3865 | try w.writeAll(" = "); | 3225 | try w.writeAll(" = "); |
| 3866 | try f.writeCValueDeref(w, operand); | 3226 | try f.writeCValueDeref(w, operand); |
| 3867 | try v.elem(f, w); | 3227 | try v.elem(f, w); |
| 3868 | } | 3228 | } |
| 3869 | try w.writeByte(';'); | 3229 | try w.writeByte(';'); |
| 3870 | try f.object.newline(); | 3230 | try f.newline(); |
| 3871 | try v.end(f, inst, w); | 3231 | try v.end(f, inst, w); |
| 3872 | | 3232 | |
| 3873 | return local; | 3233 | return local; |
| 3874 | } | 3234 | } |
| 3875 | | 3235 | |
| 3876 | fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { | 3236 | fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 3877 | const pt = f.object.dg.pt; | 3237 | const pt = f.dg.pt; |
| 3878 | const zcu = pt.zcu; | 3238 | const zcu = pt.zcu; |
| 3879 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 3239 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3880 | const w = &f.object.code.writer; | 3240 | const w = &f.code.writer; |
| 3881 | const op_inst = un_op.toIndex(); | 3241 | const op_inst = un_op.toIndex(); |
| 3882 | const op_ty = f.typeOf(un_op); | 3242 | const op_ty = f.typeOf(un_op); |
| 3883 | const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty; | 3243 | const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty; |
| 3884 | const ret_ctype = try f.ctypeFromType(ret_ty, .parameter); | | |
| 3885 | | 3244 | |
| 3886 | if (op_inst != null and f.air.instructions.items(.tag)[@intFromEnum(op_inst.?)] == .call_always_tail) { | 3245 | if (op_inst != null and f.air.instructions.items(.tag)[@intFromEnum(op_inst.?)] == .call_always_tail) { |
| 3887 | try reap(f, inst, &.{un_op}); | 3246 | try reap(f, inst, &.{un_op}); |
| 3888 | _ = try airCall(f, op_inst.?, .always_tail); | 3247 | _ = try airCall(f, op_inst.?, .always_tail); |
| 3889 | } else if (ret_ctype.index != .void) { | 3248 | } else if (ret_ty.hasRuntimeBits(zcu)) { |
| 3890 | const operand = try f.resolveInst(un_op); | 3249 | const operand = try f.resolveInst(un_op); |
| 3891 | try reap(f, inst, &.{un_op}); | 3250 | try reap(f, inst, &.{un_op}); |
| 3892 | var deref = is_ptr; | | |
| 3893 | const is_array = lowersToArray(ret_ty, zcu); | | |
| 3894 | const ret_val = if (is_array) ret_val: { | | |
| 3895 | const array_local = try f.allocAlignedLocal(inst, .{ | | |
| 3896 | .ctype = ret_ctype, | | |
| 3897 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)), | | |
| 3898 | }); | | |
| 3899 | try w.writeAll("memcpy("); | | |
| 3900 | try f.writeCValueMember(w, array_local, .{ .identifier = "array" }); | | |
| 3901 | try w.writeAll(", "); | | |
| 3902 | if (deref) | | |
| 3903 | try f.writeCValueDeref(w, operand) | | |
| 3904 | else | | |
| 3905 | try f.writeCValue(w, operand, .FunctionArgument); | | |
| 3906 | deref = false; | | |
| 3907 | try w.writeAll(", sizeof("); | | |
| 3908 | try f.renderType(w, ret_ty); | | |
| 3909 | try w.writeAll("));"); | | |
| 3910 | try f.object.newline(); | | |
| 3911 | break :ret_val array_local; | | |
| 3912 | } else operand; | | |
| 3913 | | 3251 | |
| 3914 | try w.writeAll("return "); | 3252 | try w.writeAll("return "); |
| 3915 | if (deref) | 3253 | if (is_ptr) { |
| 3916 | try f.writeCValueDeref(w, ret_val) | 3254 | try f.writeCValueDeref(w, operand); |
| 3917 | else | 3255 | } else switch (operand) { |
| 3918 | try f.writeCValue(w, ret_val, .Other); | 3256 | // Instead of 'return &local', emit 'return undefined'. |
| 3919 | try w.writeAll(";\n"); | 3257 | .local_ref => try f.dg.renderUndefValue(w, ret_ty, .other), |
| 3920 | if (is_array) { | 3258 | else => try f.writeCValue(w, operand, .other), |
| 3921 | try freeLocal(f, inst, ret_val.new_local, null); | | |
| 3922 | } | 3259 | } |
| | 3260 | try w.writeAll(";\n"); |
| 3923 | } else { | 3261 | } else { |
| 3924 | try reap(f, inst, &.{un_op}); | 3262 | try reap(f, inst, &.{un_op}); |
| 3925 | // Not even allowed to return void in a naked function. | 3263 | // Not even allowed to return void in a naked function. |
| 3926 | if (!f.object.dg.is_naked_fn) try w.writeAll("return;\n"); | 3264 | if (!f.dg.is_naked_fn) try w.writeAll("return;\n"); |
| 3927 | } | 3265 | } |
| 3928 | } | 3266 | } |
| 3929 | | 3267 | |
| 3930 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { | 3268 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3931 | const pt = f.object.dg.pt; | 3269 | const pt = f.dg.pt; |
| 3932 | const zcu = pt.zcu; | 3270 | const zcu = pt.zcu; |
| 3933 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 3271 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3934 | | 3272 | |
| ... | @@ -3940,23 +3278,23 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3940,23 +3278,23 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3940 | const operand_ty = f.typeOf(ty_op.operand); | 3278 | const operand_ty = f.typeOf(ty_op.operand); |
| 3941 | const scalar_ty = operand_ty.scalarType(zcu); | 3279 | const scalar_ty = operand_ty.scalarType(zcu); |
| 3942 | | 3280 | |
| 3943 | if (f.object.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand); | 3281 | if (f.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand); |
| 3944 | | 3282 | |
| 3945 | const w = &f.object.code.writer; | 3283 | const w = &f.code.writer; |
| 3946 | const local = try f.allocLocal(inst, inst_ty); | 3284 | const local = try f.allocLocal(inst, inst_ty); |
| 3947 | const v = try Vectorize.start(f, inst, w, operand_ty); | 3285 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 3948 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); | 3286 | try f.writeCValue(w, local, .other); |
| 3949 | try f.writeCValue(w, local, .Other); | | |
| 3950 | try v.elem(f, w); | 3287 | try v.elem(f, w); |
| 3951 | try a.assign(f, w); | 3288 | try w.writeAll(" = "); |
| 3952 | try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .Other); | 3289 | try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .other); |
| 3953 | try a.end(f, w); | 3290 | try w.writeByte(';'); |
| | 3291 | try f.newline(); |
| 3954 | try v.end(f, inst, w); | 3292 | try v.end(f, inst, w); |
| 3955 | return local; | 3293 | return local; |
| 3956 | } | 3294 | } |
| 3957 | | 3295 | |
| 3958 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | 3296 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3959 | const pt = f.object.dg.pt; | 3297 | const pt = f.dg.pt; |
| 3960 | const zcu = pt.zcu; | 3298 | const zcu = pt.zcu; |
| 3961 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 3299 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3962 | | 3300 | |
| ... | @@ -3978,13 +3316,12 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3978,13 +3316,12 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3978 | const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits); | 3316 | const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits); |
| 3979 | if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand); | 3317 | if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand); |
| 3980 | | 3318 | |
| 3981 | const w = &f.object.code.writer; | 3319 | const w = &f.code.writer; |
| 3982 | const local = try f.allocLocal(inst, inst_ty); | 3320 | const local = try f.allocLocal(inst, inst_ty); |
| 3983 | const v = try Vectorize.start(f, inst, w, operand_ty); | 3321 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 3984 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete)); | 3322 | try f.writeCValue(w, local, .other); |
| 3985 | try f.writeCValue(w, local, .Other); | | |
| 3986 | try v.elem(f, w); | 3323 | try v.elem(f, w); |
| 3987 | try a.assign(f, w); | 3324 | try w.writeAll(" = "); |
| 3988 | if (need_cast) { | 3325 | if (need_cast) { |
| 3989 | try w.writeByte('('); | 3326 | try w.writeByte('('); |
| 3990 | try f.renderType(w, inst_scalar_ty); | 3327 | try f.renderType(w, inst_scalar_ty); |
| ... | @@ -3992,18 +3329,18 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3992,18 +3329,18 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3992 | } | 3329 | } |
| 3993 | if (need_lo) { | 3330 | if (need_lo) { |
| 3994 | try w.writeAll("zig_lo_"); | 3331 | try w.writeAll("zig_lo_"); |
| 3995 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 3332 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3996 | try w.writeByte('('); | 3333 | try w.writeByte('('); |
| 3997 | } | 3334 | } |
| 3998 | if (!need_mask) { | 3335 | if (!need_mask) { |
| 3999 | try f.writeCValue(w, operand, .Other); | 3336 | try f.writeCValue(w, operand, .other); |
| 4000 | try v.elem(f, w); | 3337 | try v.elem(f, w); |
| 4001 | } else switch (dest_int_info.signedness) { | 3338 | } else switch (dest_int_info.signedness) { |
| 4002 | .unsigned => { | 3339 | .unsigned => { |
| 4003 | try w.writeAll("zig_and_"); | 3340 | try w.writeAll("zig_and_"); |
| 4004 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 3341 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4005 | try w.writeByte('('); | 3342 | try w.writeByte('('); |
| 4006 | try f.writeCValue(w, operand, .FunctionArgument); | 3343 | try f.writeCValue(w, operand, .other); |
| 4007 | try v.elem(f, w); | 3344 | try v.elem(f, w); |
| 4008 | try w.print(", {f})", .{ | 3345 | try w.print(", {f})", .{ |
| 4009 | try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), | 3346 | try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), |
| ... | @@ -4015,7 +3352,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4015,7 +3352,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4015 | const shift_val = try pt.intValue(.u8, c_bits - dest_bits); | 3352 | const shift_val = try pt.intValue(.u8, c_bits - dest_bits); |
| 4016 | | 3353 | |
| 4017 | try w.writeAll("zig_shr_"); | 3354 | try w.writeAll("zig_shr_"); |
| 4018 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 3355 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4019 | if (c_bits == 128) { | 3356 | if (c_bits == 128) { |
| 4020 | try w.print("(zig_bitCast_i{d}(", .{c_bits}); | 3357 | try w.print("(zig_bitCast_i{d}(", .{c_bits}); |
| 4021 | } else { | 3358 | } else { |
| ... | @@ -4027,7 +3364,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4027,7 +3364,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4027 | } else { | 3364 | } else { |
| 4028 | try w.print("(uint{d}_t)", .{c_bits}); | 3365 | try w.print("(uint{d}_t)", .{c_bits}); |
| 4029 | } | 3366 | } |
| 4030 | try f.writeCValue(w, operand, .FunctionArgument); | 3367 | try f.writeCValue(w, operand, .other); |
| 4031 | try v.elem(f, w); | 3368 | try v.elem(f, w); |
| 4032 | if (c_bits == 128) try w.writeByte(')'); | 3369 | if (c_bits == 128) try w.writeByte(')'); |
| 4033 | try w.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)}); | 3370 | try w.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)}); |
| ... | @@ -4036,13 +3373,14 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4036,13 +3373,14 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4036 | }, | 3373 | }, |
| 4037 | } | 3374 | } |
| 4038 | if (need_lo) try w.writeByte(')'); | 3375 | if (need_lo) try w.writeByte(')'); |
| 4039 | try a.end(f, w); | 3376 | try w.writeByte(';'); |
| | 3377 | try f.newline(); |
| 4040 | try v.end(f, inst, w); | 3378 | try v.end(f, inst, w); |
| 4041 | return local; | 3379 | return local; |
| 4042 | } | 3380 | } |
| 4043 | | 3381 | |
| 4044 | fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | 3382 | fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4045 | const pt = f.object.dg.pt; | 3383 | const pt = f.dg.pt; |
| 4046 | const zcu = pt.zcu; | 3384 | const zcu = pt.zcu; |
| 4047 | // *a = b; | 3385 | // *a = b; |
| 4048 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3386 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| ... | @@ -4060,7 +3398,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -4060,7 +3398,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4060 | | 3398 | |
| 4061 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndef(zcu) else false; | 3399 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndef(zcu) else false; |
| 4062 | | 3400 | |
| 4063 | const w = &f.object.code.writer; | 3401 | const w = &f.code.writer; |
| 4064 | if (val_is_undef) { | 3402 | if (val_is_undef) { |
| 4065 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3403 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4066 | if (safety and ptr_info.packed_offset.host_size == 0) { | 3404 | if (safety and ptr_info.packed_offset.host_size == 0) { |
| ... | @@ -4080,11 +3418,11 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -4080,11 +3418,11 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4080 | }, | 3418 | }, |
| 4081 | }; | 3419 | }; |
| 4082 | try w.writeAll("memset("); | 3420 | try w.writeAll("memset("); |
| 4083 | try f.writeCValue(w, ptr_val, .FunctionArgument); | 3421 | try f.writeCValue(w, ptr_val, .other); |
| 4084 | try w.print(", {s}, sizeof(", .{byte_str}); | 3422 | try w.print(", {s}, sizeof(", .{byte_str}); |
| 4085 | try f.renderType(w, .fromInterned(ptr_info.child)); | 3423 | try f.renderType(w, .fromInterned(ptr_info.child)); |
| 4086 | try w.writeAll("));"); | 3424 | try w.writeAll("));"); |
| 4087 | try f.object.newline(); | 3425 | try f.newline(); |
| 4088 | } | 3426 | } |
| 4089 | return .none; | 3427 | return .none; |
| 4090 | } | 3428 | } |
| ... | @@ -4093,46 +3431,29 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -4093,46 +3431,29 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4093 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) | 3431 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) |
| 4094 | else | 3432 | else |
| 4095 | true; | 3433 | true; |
| 4096 | const is_array = lowersToArray(.fromInterned(ptr_info.child), zcu); | | |
| 4097 | const need_memcpy = !is_aligned or is_array; | | |
| 4098 | | 3434 | |
| 4099 | const src_val = try f.resolveInst(bin_op.rhs); | 3435 | const src_val = try f.resolveInst(bin_op.rhs); |
| 4100 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3436 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4101 | | 3437 | |
| 4102 | const src_scalar_ctype = try f.ctypeFromType(src_ty.scalarType(zcu), .complete); | 3438 | if (!is_aligned) { |
| 4103 | if (need_memcpy) { | | |
| 4104 | // For this memcpy to safely work we need the rhs to have the same | 3439 | // For this memcpy to safely work we need the rhs to have the same |
| 4105 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). | 3440 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| 4106 | assert(src_ty.eql(.fromInterned(ptr_info.child), zcu)); | 3441 | assert(src_ty.eql(.fromInterned(ptr_info.child), zcu)); |
| 4107 | | 3442 | |
| 4108 | // If the source is a constant, writeCValue will emit a brace initialization | | |
| 4109 | // so work around this by initializing into new local. | | |
| 4110 | // TODO this should be done by manually initializing elements of the dest array | | |
| 4111 | const array_src = if (src_val == .constant) blk: { | | |
| 4112 | const new_local = try f.allocLocal(inst, src_ty); | | |
| 4113 | try f.writeCValue(w, new_local, .Other); | | |
| 4114 | try w.writeAll(" = "); | | |
| 4115 | try f.writeCValue(w, src_val, .Other); | | |
| 4116 | try w.writeByte(';'); | | |
| 4117 | try f.object.newline(); | | |
| 4118 | | | |
| 4119 | break :blk new_local; | | |
| 4120 | } else src_val; | | |
| 4121 | | | |
| 4122 | const v = try Vectorize.start(f, inst, w, ptr_ty); | 3443 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 4123 | try w.writeAll("memcpy((char *)"); | 3444 | try w.writeAll("memcpy((char *)"); |
| 4124 | try f.writeCValue(w, ptr_val, .FunctionArgument); | 3445 | try f.writeCValue(w, ptr_val, .other); |
| 4125 | try v.elem(f, w); | 3446 | try v.elem(f, w); |
| 4126 | try w.writeAll(", "); | 3447 | try w.writeAll(", &"); |
| 4127 | if (!is_array) try w.writeByte('&'); | 3448 | switch (src_val) { |
| 4128 | try f.writeCValue(w, array_src, .FunctionArgument); | 3449 | .constant => |val| try f.dg.renderValueAsLvalue(w, val), |
| | 3450 | else => try f.writeCValue(w, src_val, .other), |
| | 3451 | } |
| 4129 | try v.elem(f, w); | 3452 | try v.elem(f, w); |
| 4130 | try w.writeAll(", sizeof("); | 3453 | try w.writeAll(", sizeof("); |
| 4131 | try f.renderType(w, src_ty); | 3454 | try f.renderType(w, src_ty); |
| 4132 | try w.writeAll("))"); | 3455 | try w.writeAll("));"); |
| 4133 | try f.freeCValue(inst, array_src); | 3456 | try f.newline(); |
| 4134 | try w.writeByte(';'); | | |
| 4135 | try f.object.newline(); | | |
| 4136 | try v.end(f, inst, w); | 3457 | try v.end(f, inst, w); |
| 4137 | } else { | 3458 | } else { |
| 4138 | switch (ptr_val) { | 3459 | switch (ptr_val) { |
| ... | @@ -4144,20 +3465,20 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -4144,20 +3465,20 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4144 | else => {}, | 3465 | else => {}, |
| 4145 | } | 3466 | } |
| 4146 | const v = try Vectorize.start(f, inst, w, ptr_ty); | 3467 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 4147 | const a = try Assignment.start(f, w, src_scalar_ctype); | | |
| 4148 | try f.writeCValueDeref(w, ptr_val); | 3468 | try f.writeCValueDeref(w, ptr_val); |
| 4149 | try v.elem(f, w); | 3469 | try v.elem(f, w); |
| 4150 | try a.assign(f, w); | 3470 | try w.writeAll(" = "); |
| 4151 | try f.writeCValue(w, src_val, .Other); | 3471 | try f.writeCValue(w, src_val, .other); |
| 4152 | try v.elem(f, w); | 3472 | try v.elem(f, w); |
| 4153 | try a.end(f, w); | 3473 | try w.writeByte(';'); |
| | 3474 | try f.newline(); |
| 4154 | try v.end(f, inst, w); | 3475 | try v.end(f, inst, w); |
| 4155 | } | 3476 | } |
| 4156 | return .none; | 3477 | return .none; |
| 4157 | } | 3478 | } |
| 4158 | | 3479 | |
| 4159 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { | 3480 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { |
| 4160 | const pt = f.object.dg.pt; | 3481 | const pt = f.dg.pt; |
| 4161 | const zcu = pt.zcu; | 3482 | const zcu = pt.zcu; |
| 4162 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 3483 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4163 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3484 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -4170,7 +3491,7 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: | ... | @@ -4170,7 +3491,7 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4170 | const operand_ty = f.typeOf(bin_op.lhs); | 3491 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4171 | const scalar_ty = operand_ty.scalarType(zcu); | 3492 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4172 | | 3493 | |
| 4173 | const w = &f.object.code.writer; | 3494 | const w = &f.code.writer; |
| 4174 | const local = try f.allocLocal(inst, inst_ty); | 3495 | const local = try f.allocLocal(inst, inst_ty); |
| 4175 | const v = try Vectorize.start(f, inst, w, operand_ty); | 3496 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4176 | try f.writeCValueMember(w, local, .{ .field = 1 }); | 3497 | try f.writeCValueMember(w, local, .{ .field = 1 }); |
| ... | @@ -4178,26 +3499,26 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: | ... | @@ -4178,26 +3499,26 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4178 | try w.writeAll(" = zig_"); | 3499 | try w.writeAll(" = zig_"); |
| 4179 | try w.writeAll(operation); | 3500 | try w.writeAll(operation); |
| 4180 | try w.writeAll("o_"); | 3501 | try w.writeAll("o_"); |
| 4181 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 3502 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4182 | try w.writeAll("(&"); | 3503 | try w.writeAll("(&"); |
| 4183 | try f.writeCValueMember(w, local, .{ .field = 0 }); | 3504 | try f.writeCValueMember(w, local, .{ .field = 0 }); |
| 4184 | try v.elem(f, w); | 3505 | try v.elem(f, w); |
| 4185 | try w.writeAll(", "); | 3506 | try w.writeAll(", "); |
| 4186 | try f.writeCValue(w, lhs, .FunctionArgument); | 3507 | try f.writeCValue(w, lhs, .other); |
| 4187 | try v.elem(f, w); | 3508 | try v.elem(f, w); |
| 4188 | try w.writeAll(", "); | 3509 | try w.writeAll(", "); |
| 4189 | try f.writeCValue(w, rhs, .FunctionArgument); | 3510 | try f.writeCValue(w, rhs, .other); |
| 4190 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); | 3511 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); |
| 4191 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); | 3512 | try f.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 4192 | try w.writeAll(");"); | 3513 | try w.writeAll(");"); |
| 4193 | try f.object.newline(); | 3514 | try f.newline(); |
| 4194 | try v.end(f, inst, w); | 3515 | try v.end(f, inst, w); |
| 4195 | | 3516 | |
| 4196 | return local; | 3517 | return local; |
| 4197 | } | 3518 | } |
| 4198 | | 3519 | |
| 4199 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { | 3520 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4200 | const pt = f.object.dg.pt; | 3521 | const pt = f.dg.pt; |
| 4201 | const zcu = pt.zcu; | 3522 | const zcu = pt.zcu; |
| 4202 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 3523 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4203 | const operand_ty = f.typeOf(ty_op.operand); | 3524 | const operand_ty = f.typeOf(ty_op.operand); |
| ... | @@ -4209,17 +3530,17 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4209,17 +3530,17 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4209 | | 3530 | |
| 4210 | const inst_ty = f.typeOfIndex(inst); | 3531 | const inst_ty = f.typeOfIndex(inst); |
| 4211 | | 3532 | |
| 4212 | const w = &f.object.code.writer; | 3533 | const w = &f.code.writer; |
| 4213 | const local = try f.allocLocal(inst, inst_ty); | 3534 | const local = try f.allocLocal(inst, inst_ty); |
| 4214 | const v = try Vectorize.start(f, inst, w, operand_ty); | 3535 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4215 | try f.writeCValue(w, local, .Other); | 3536 | try f.writeCValue(w, local, .other); |
| 4216 | try v.elem(f, w); | 3537 | try v.elem(f, w); |
| 4217 | try w.writeAll(" = "); | 3538 | try w.writeAll(" = "); |
| 4218 | try w.writeByte('!'); | 3539 | try w.writeByte('!'); |
| 4219 | try f.writeCValue(w, op, .Other); | 3540 | try f.writeCValue(w, op, .other); |
| 4220 | try v.elem(f, w); | 3541 | try v.elem(f, w); |
| 4221 | try w.writeByte(';'); | 3542 | try w.writeByte(';'); |
| 4222 | try f.object.newline(); | 3543 | try f.newline(); |
| 4223 | try v.end(f, inst, w); | 3544 | try v.end(f, inst, w); |
| 4224 | | 3545 | |
| 4225 | return local; | 3546 | return local; |
| ... | @@ -4232,7 +3553,7 @@ fn airBinOp( | ... | @@ -4232,7 +3553,7 @@ fn airBinOp( |
| 4232 | operation: []const u8, | 3553 | operation: []const u8, |
| 4233 | info: BuiltinInfo, | 3554 | info: BuiltinInfo, |
| 4234 | ) !CValue { | 3555 | ) !CValue { |
| 4235 | const pt = f.object.dg.pt; | 3556 | const pt = f.dg.pt; |
| 4236 | const zcu = pt.zcu; | 3557 | const zcu = pt.zcu; |
| 4237 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3558 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4238 | const operand_ty = f.typeOf(bin_op.lhs); | 3559 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | @@ -4246,21 +3567,21 @@ fn airBinOp( | ... | @@ -4246,21 +3567,21 @@ fn airBinOp( |
| 4246 | | 3567 | |
| 4247 | const inst_ty = f.typeOfIndex(inst); | 3568 | const inst_ty = f.typeOfIndex(inst); |
| 4248 | | 3569 | |
| 4249 | const w = &f.object.code.writer; | 3570 | const w = &f.code.writer; |
| 4250 | const local = try f.allocLocal(inst, inst_ty); | 3571 | const local = try f.allocLocal(inst, inst_ty); |
| 4251 | const v = try Vectorize.start(f, inst, w, operand_ty); | 3572 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4252 | try f.writeCValue(w, local, .Other); | 3573 | try f.writeCValue(w, local, .other); |
| 4253 | try v.elem(f, w); | 3574 | try v.elem(f, w); |
| 4254 | try w.writeAll(" = "); | 3575 | try w.writeAll(" = "); |
| 4255 | try f.writeCValue(w, lhs, .Other); | 3576 | try f.writeCValue(w, lhs, .other); |
| 4256 | try v.elem(f, w); | 3577 | try v.elem(f, w); |
| 4257 | try w.writeByte(' '); | 3578 | try w.writeByte(' '); |
| 4258 | try w.writeAll(operator); | 3579 | try w.writeAll(operator); |
| 4259 | try w.writeByte(' '); | 3580 | try w.writeByte(' '); |
| 4260 | try f.writeCValue(w, rhs, .Other); | 3581 | try f.writeCValue(w, rhs, .other); |
| 4261 | try v.elem(f, w); | 3582 | try v.elem(f, w); |
| 4262 | try w.writeByte(';'); | 3583 | try w.writeByte(';'); |
| 4263 | try f.object.newline(); | 3584 | try f.newline(); |
| 4264 | try v.end(f, inst, w); | 3585 | try v.end(f, inst, w); |
| 4265 | | 3586 | |
| 4266 | return local; | 3587 | return local; |
| ... | @@ -4272,7 +3593,7 @@ fn airCmpOp( | ... | @@ -4272,7 +3593,7 @@ fn airCmpOp( |
| 4272 | data: anytype, | 3593 | data: anytype, |
| 4273 | operator: std.math.CompareOperator, | 3594 | operator: std.math.CompareOperator, |
| 4274 | ) !CValue { | 3595 | ) !CValue { |
| 4275 | const pt = f.object.dg.pt; | 3596 | const pt = f.dg.pt; |
| 4276 | const zcu = pt.zcu; | 3597 | const zcu = pt.zcu; |
| 4277 | const lhs_ty = f.typeOf(data.lhs); | 3598 | const lhs_ty = f.typeOf(data.lhs); |
| 4278 | const scalar_ty = lhs_ty.scalarType(zcu); | 3599 | const scalar_ty = lhs_ty.scalarType(zcu); |
| ... | @@ -4297,26 +3618,26 @@ fn airCmpOp( | ... | @@ -4297,26 +3618,26 @@ fn airCmpOp( |
| 4297 | | 3618 | |
| 4298 | const rhs_ty = f.typeOf(data.rhs); | 3619 | const rhs_ty = f.typeOf(data.rhs); |
| 4299 | const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu); | 3620 | const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu); |
| 4300 | const w = &f.object.code.writer; | 3621 | const w = &f.code.writer; |
| 4301 | const local = try f.allocLocal(inst, inst_ty); | 3622 | const local = try f.allocLocal(inst, inst_ty); |
| 4302 | const v = try Vectorize.start(f, inst, w, lhs_ty); | 3623 | const v = try Vectorize.start(f, inst, w, lhs_ty); |
| 4303 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); | 3624 | try f.writeCValue(w, local, .other); |
| 4304 | try f.writeCValue(w, local, .Other); | | |
| 4305 | try v.elem(f, w); | 3625 | try v.elem(f, w); |
| 4306 | try a.assign(f, w); | 3626 | try w.writeAll(" = "); |
| 4307 | if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) { | 3627 | if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) { |
| 4308 | .lt, .neq, .gt => "false", | 3628 | .lt, .neq, .gt => "false", |
| 4309 | .lte, .eq, .gte => "true", | 3629 | .lte, .eq, .gte => "true", |
| 4310 | }) else { | 3630 | }) else { |
| 4311 | if (need_cast) try w.writeAll("(void*)"); | 3631 | if (need_cast) try w.writeAll("(void*)"); |
| 4312 | try f.writeCValue(w, lhs, .Other); | 3632 | try f.writeCValue(w, lhs, .other); |
| 4313 | try v.elem(f, w); | 3633 | try v.elem(f, w); |
| 4314 | try w.writeAll(compareOperatorC(operator)); | 3634 | try w.writeAll(compareOperatorC(operator)); |
| 4315 | if (need_cast) try w.writeAll("(void*)"); | 3635 | if (need_cast) try w.writeAll("(void*)"); |
| 4316 | try f.writeCValue(w, rhs, .Other); | 3636 | try f.writeCValue(w, rhs, .other); |
| 4317 | try v.elem(f, w); | 3637 | try v.elem(f, w); |
| 4318 | } | 3638 | } |
| 4319 | try a.end(f, w); | 3639 | try w.writeByte(';'); |
| | 3640 | try f.newline(); |
| 4320 | try v.end(f, inst, w); | 3641 | try v.end(f, inst, w); |
| 4321 | | 3642 | |
| 4322 | return local; | 3643 | return local; |
| ... | @@ -4327,9 +3648,8 @@ fn airEquality( | ... | @@ -4327,9 +3648,8 @@ fn airEquality( |
| 4327 | inst: Air.Inst.Index, | 3648 | inst: Air.Inst.Index, |
| 4328 | operator: std.math.CompareOperator, | 3649 | operator: std.math.CompareOperator, |
| 4329 | ) !CValue { | 3650 | ) !CValue { |
| 4330 | const pt = f.object.dg.pt; | 3651 | const pt = f.dg.pt; |
| 4331 | const zcu = pt.zcu; | 3652 | const zcu = pt.zcu; |
| 4332 | const ctype_pool = &f.object.dg.ctype_pool; | | |
| 4333 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3653 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4334 | | 3654 | |
| 4335 | const operand_ty = f.typeOf(bin_op.lhs); | 3655 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | @@ -4350,54 +3670,64 @@ fn airEquality( | ... | @@ -4350,54 +3670,64 @@ fn airEquality( |
| 4350 | const rhs = try f.resolveInst(bin_op.rhs); | 3670 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4351 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3671 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4352 | | 3672 | |
| 4353 | const w = &f.object.code.writer; | 3673 | if (lhs.eql(rhs)) { |
| | 3674 | // Avoid emitting a tautological comparison. |
| | 3675 | return .{ .constant = .makeBool(switch (operator) { |
| | 3676 | .eq, .lte, .gte => true, |
| | 3677 | .neq, .lt, .gt => false, |
| | 3678 | }) }; |
| | 3679 | } |
| | 3680 | |
| | 3681 | const w = &f.code.writer; |
| 4354 | const local = try f.allocLocal(inst, .bool); | 3682 | const local = try f.allocLocal(inst, .bool); |
| 4355 | const a = try Assignment.start(f, w, .bool); | 3683 | try f.writeCValue(w, local, .other); |
| 4356 | try f.writeCValue(w, local, .Other); | 3684 | try w.writeAll(" = "); |
| 4357 | try a.assign(f, w); | | |
| 4358 | | 3685 | |
| 4359 | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); | 3686 | switch (operand_ty.zigTypeTag(zcu)) { |
| 4360 | if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) { | 3687 | .optional => switch (CType.classifyOptional(operand_ty, zcu)) { |
| 4361 | .lt, .lte, .gte, .gt => unreachable, | 3688 | .npv_payload => unreachable, // opv optional |
| 4362 | .neq => "false", | 3689 | |
| 4363 | .eq => "true", | 3690 | .error_set, .ptr_like => {}, |
| 4364 | }) else switch (operand_ctype.info(ctype_pool)) { | 3691 | |
| 4365 | .basic, .pointer => { | 3692 | .slice_like => unreachable, // equality is not defined on slices |
| 4366 | try f.writeCValue(w, lhs, .Other); | 3693 | |
| 4367 | try w.writeAll(compareOperatorC(operator)); | 3694 | .opv_payload => { |
| 4368 | try f.writeCValue(w, rhs, .Other); | 3695 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 4369 | }, | 3696 | try w.writeAll(compareOperatorC(operator)); |
| 4370 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 3697 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 4371 | .aggregate => |aggregate| if (aggregate.fields.len == 2 and | 3698 | try w.writeByte(';'); |
| 4372 | (aggregate.fields.at(0, ctype_pool).name.index == .is_null or | 3699 | try f.newline(); |
| 4373 | aggregate.fields.at(1, ctype_pool).name.index == .is_null)) | 3700 | return local; |
| 4374 | { | 3701 | }, |
| 4375 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); | 3702 | |
| 4376 | try w.writeAll(" || "); | 3703 | .@"struct" => { |
| 4377 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); | 3704 | // `lhs.is_null || rhs.is_null ? lhs.is_null == rhs.is_null : lhs.payload == rhs.payload` |
| 4378 | try w.writeAll(" ? "); | 3705 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 4379 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); | 3706 | try w.writeAll(" || "); |
| 4380 | try w.writeAll(compareOperatorC(operator)); | 3707 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 4381 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); | 3708 | try w.writeAll(" ? "); |
| 4382 | try w.writeAll(" : "); | 3709 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 4383 | try f.writeCValueMember(w, lhs, .{ .identifier = "payload" }); | 3710 | try w.writeAll(compareOperatorC(operator)); |
| 4384 | try w.writeAll(compareOperatorC(operator)); | 3711 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 4385 | try f.writeCValueMember(w, rhs, .{ .identifier = "payload" }); | 3712 | try w.writeAll(" : "); |
| 4386 | } else for (0..aggregate.fields.len) |field_index| { | 3713 | try f.writeCValueMember(w, lhs, .{ .identifier = "payload" }); |
| 4387 | if (field_index > 0) try w.writeAll(switch (operator) { | 3714 | try w.writeAll(compareOperatorC(operator)); |
| 4388 | .lt, .lte, .gte, .gt => unreachable, | 3715 | try f.writeCValueMember(w, rhs, .{ .identifier = "payload" }); |
| 4389 | .eq => " && ", | 3716 | try w.writeByte(';'); |
| 4390 | .neq => " || ", | 3717 | try f.newline(); |
| 4391 | }); | 3718 | return local; |
| 4392 | const field_name: CValue = .{ | 3719 | }, |
| 4393 | .ctype_pool_string = aggregate.fields.at(field_index, ctype_pool).name, | | |
| 4394 | }; | | |
| 4395 | try f.writeCValueMember(w, lhs, field_name); | | |
| 4396 | try w.writeAll(compareOperatorC(operator)); | | |
| 4397 | try f.writeCValueMember(w, rhs, field_name); | | |
| 4398 | }, | 3720 | }, |
| | 3721 | .bool, .int, .pointer, .@"enum", .error_set => {}, |
| | 3722 | .@"struct", .@"union" => assert(operand_ty.containerLayout(zcu) == .@"packed"), |
| | 3723 | else => unreachable, |
| 4399 | } | 3724 | } |
| 4400 | try a.end(f, w); | 3725 | |
| | 3726 | try f.writeCValue(w, lhs, .other); |
| | 3727 | try w.writeAll(compareOperatorC(operator)); |
| | 3728 | try f.writeCValue(w, rhs, .other); |
| | 3729 | try w.writeByte(';'); |
| | 3730 | try f.newline(); |
| 4401 | | 3731 | |
| 4402 | return local; | 3732 | return local; |
| 4403 | } | 3733 | } |
| ... | @@ -4408,18 +3738,18 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4408,18 +3738,18 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4408 | const operand = try f.resolveInst(un_op); | 3738 | const operand = try f.resolveInst(un_op); |
| 4409 | try reap(f, inst, &.{un_op}); | 3739 | try reap(f, inst, &.{un_op}); |
| 4410 | | 3740 | |
| 4411 | const w = &f.object.code.writer; | 3741 | const w = &f.code.writer; |
| 4412 | const local = try f.allocLocal(inst, .bool); | 3742 | const local = try f.allocLocal(inst, .bool); |
| 4413 | try f.writeCValue(w, local, .Other); | 3743 | try f.writeCValue(w, local, .other); |
| 4414 | try w.writeAll(" = "); | 3744 | try w.writeAll(" = "); |
| 4415 | try f.writeCValue(w, operand, .Other); | 3745 | try f.writeCValue(w, operand, .other); |
| 4416 | try w.print(" < sizeof({f}) / sizeof(*{0f});", .{fmtIdentSolo("zig_errorName")}); | 3746 | try w.print(" < sizeof({f}) / sizeof(*{0f});", .{fmtIdentSolo("zig_errorName")}); |
| 4417 | try f.object.newline(); | 3747 | try f.newline(); |
| 4418 | return local; | 3748 | return local; |
| 4419 | } | 3749 | } |
| 4420 | | 3750 | |
| 4421 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { | 3751 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4422 | const pt = f.object.dg.pt; | 3752 | const pt = f.dg.pt; |
| 4423 | const zcu = pt.zcu; | 3753 | const zcu = pt.zcu; |
| 4424 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 3754 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4425 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3755 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -4432,38 +3762,34 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { | ... | @@ -4432,38 +3762,34 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4432 | const inst_scalar_ty = inst_ty.scalarType(zcu); | 3762 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4433 | const elem_ty = inst_scalar_ty.indexableElem(zcu); | 3763 | const elem_ty = inst_scalar_ty.indexableElem(zcu); |
| 4434 | assert(elem_ty.hasRuntimeBits(zcu)); | 3764 | assert(elem_ty.hasRuntimeBits(zcu)); |
| 4435 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); | | |
| 4436 | | 3765 | |
| 4437 | const local = try f.allocLocal(inst, inst_ty); | 3766 | const local = try f.allocLocal(inst, inst_ty); |
| 4438 | const w = &f.object.code.writer; | 3767 | const w = &f.code.writer; |
| 4439 | const v = try Vectorize.start(f, inst, w, inst_ty); | 3768 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 4440 | const a = try Assignment.start(f, w, inst_scalar_ctype); | 3769 | try f.writeCValue(w, local, .other); |
| 4441 | try f.writeCValue(w, local, .Other); | | |
| 4442 | try v.elem(f, w); | 3770 | try v.elem(f, w); |
| 4443 | try a.assign(f, w); | 3771 | try w.writeAll(" = "); |
| 4444 | // We must convert to and from integer types to prevent UB if the operation | 3772 | // We must convert to and from integer types to prevent UB if the operation |
| 4445 | // results in a NULL pointer, or if LHS is NULL. The operation is only UB | 3773 | // results in a NULL pointer, or if LHS is NULL. The operation is only UB |
| 4446 | // if the result is NULL and then dereferenced. | 3774 | // if the result is NULL and then dereferenced. |
| 4447 | try w.writeByte('('); | 3775 | try w.writeByte('('); |
| 4448 | try f.renderCType(w, inst_scalar_ctype); | 3776 | try f.renderType(w, inst_scalar_ty); |
| 4449 | try w.writeAll(")(((uintptr_t)"); | 3777 | try w.writeAll(")(((uintptr_t)"); |
| 4450 | try f.writeCValue(w, lhs, .Other); | 3778 | try f.writeCValue(w, lhs, .other); |
| 4451 | try v.elem(f, w); | 3779 | try v.elem(f, w); |
| 4452 | try w.writeAll(") "); | 3780 | try w.print(") {c} (", .{operator}); |
| 4453 | try w.writeByte(operator); | 3781 | try f.writeCValue(w, rhs, .other); |
| 4454 | try w.writeAll(" ("); | | |
| 4455 | try f.writeCValue(w, rhs, .Other); | | |
| 4456 | try v.elem(f, w); | 3782 | try v.elem(f, w); |
| 4457 | try w.writeAll("*sizeof("); | 3783 | try w.writeAll("*sizeof("); |
| 4458 | try f.renderType(w, elem_ty); | 3784 | try f.renderType(w, elem_ty); |
| 4459 | try w.writeAll(")))"); | 3785 | try w.writeAll(")));"); |
| 4460 | try a.end(f, w); | 3786 | try f.newline(); |
| 4461 | try v.end(f, inst, w); | 3787 | try v.end(f, inst, w); |
| 4462 | return local; | 3788 | return local; |
| 4463 | } | 3789 | } |
| 4464 | | 3790 | |
| 4465 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { | 3791 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { |
| 4466 | const pt = f.object.dg.pt; | 3792 | const pt = f.dg.pt; |
| 4467 | const zcu = pt.zcu; | 3793 | const zcu = pt.zcu; |
| 4468 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 3794 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4469 | | 3795 | |
| ... | @@ -4477,36 +3803,34 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons | ... | @@ -4477,36 +3803,34 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 4477 | const rhs = try f.resolveInst(bin_op.rhs); | 3803 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4478 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3804 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4479 | | 3805 | |
| 4480 | const w = &f.object.code.writer; | 3806 | const w = &f.code.writer; |
| 4481 | const local = try f.allocLocal(inst, inst_ty); | 3807 | const local = try f.allocLocal(inst, inst_ty); |
| 4482 | const v = try Vectorize.start(f, inst, w, inst_ty); | 3808 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 4483 | try f.writeCValue(w, local, .Other); | 3809 | try f.writeCValue(w, local, .other); |
| 4484 | try v.elem(f, w); | 3810 | try v.elem(f, w); |
| 4485 | // (lhs <> rhs) ? lhs : rhs | 3811 | // (lhs <> rhs) ? lhs : rhs |
| 4486 | try w.writeAll(" = ("); | 3812 | try w.writeAll(" = ("); |
| 4487 | try f.writeCValue(w, lhs, .Other); | 3813 | try f.writeCValue(w, lhs, .other); |
| 4488 | try v.elem(f, w); | 3814 | try v.elem(f, w); |
| 4489 | try w.writeByte(' '); | 3815 | try w.writeByte(' '); |
| 4490 | try w.writeByte(operator); | 3816 | try w.writeByte(operator); |
| 4491 | try w.writeByte(' '); | 3817 | try w.writeByte(' '); |
| 4492 | try f.writeCValue(w, rhs, .Other); | 3818 | try f.writeCValue(w, rhs, .other); |
| 4493 | try v.elem(f, w); | 3819 | try v.elem(f, w); |
| 4494 | try w.writeAll(") ? "); | 3820 | try w.writeAll(") ? "); |
| 4495 | try f.writeCValue(w, lhs, .Other); | 3821 | try f.writeCValue(w, lhs, .other); |
| 4496 | try v.elem(f, w); | 3822 | try v.elem(f, w); |
| 4497 | try w.writeAll(" : "); | 3823 | try w.writeAll(" : "); |
| 4498 | try f.writeCValue(w, rhs, .Other); | 3824 | try f.writeCValue(w, rhs, .other); |
| 4499 | try v.elem(f, w); | 3825 | try v.elem(f, w); |
| 4500 | try w.writeByte(';'); | 3826 | try w.writeByte(';'); |
| 4501 | try f.object.newline(); | 3827 | try f.newline(); |
| 4502 | try v.end(f, inst, w); | 3828 | try v.end(f, inst, w); |
| 4503 | | 3829 | |
| 4504 | return local; | 3830 | return local; |
| 4505 | } | 3831 | } |
| 4506 | | 3832 | |
| 4507 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { | 3833 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4508 | const pt = f.object.dg.pt; | | |
| 4509 | const zcu = pt.zcu; | | |
| 4510 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 3834 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4511 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3835 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4512 | | 3836 | |
| ... | @@ -4515,24 +3839,22 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4515,24 +3839,22 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4515 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 3839 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4516 | | 3840 | |
| 4517 | const inst_ty = f.typeOfIndex(inst); | 3841 | const inst_ty = f.typeOfIndex(inst); |
| 4518 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); | | |
| 4519 | | 3842 | |
| 4520 | const w = &f.object.code.writer; | 3843 | const w = &f.code.writer; |
| 4521 | const local = try f.allocLocal(inst, inst_ty); | 3844 | const local = try f.allocLocal(inst, inst_ty); |
| 4522 | { | 3845 | |
| 4523 | const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete)); | 3846 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 4524 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); | 3847 | try w.writeAll(" = "); |
| 4525 | try a.assign(f, w); | 3848 | try f.writeCValue(w, ptr, .other); |
| 4526 | try f.writeCValue(w, ptr, .Other); | 3849 | try w.writeByte(';'); |
| 4527 | try a.end(f, w); | 3850 | try f.newline(); |
| 4528 | } | 3851 | |
| 4529 | { | 3852 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); |
| 4530 | const a = try Assignment.start(f, w, .usize); | 3853 | try w.writeAll(" = "); |
| 4531 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); | 3854 | try f.writeCValue(w, len, .other); |
| 4532 | try a.assign(f, w); | 3855 | try w.writeByte(';'); |
| 4533 | try f.writeCValue(w, len, .Other); | 3856 | try f.newline(); |
| 4534 | try a.end(f, w); | 3857 | |
| 4535 | } | | |
| 4536 | return local; | 3858 | return local; |
| 4537 | } | 3859 | } |
| 4538 | | 3860 | |
| ... | @@ -4541,14 +3863,14 @@ fn airCall( | ... | @@ -4541,14 +3863,14 @@ fn airCall( |
| 4541 | inst: Air.Inst.Index, | 3863 | inst: Air.Inst.Index, |
| 4542 | modifier: std.builtin.CallModifier, | 3864 | modifier: std.builtin.CallModifier, |
| 4543 | ) !CValue { | 3865 | ) !CValue { |
| 4544 | const pt = f.object.dg.pt; | 3866 | const pt = f.dg.pt; |
| 4545 | const zcu = pt.zcu; | 3867 | const zcu = pt.zcu; |
| 4546 | const ip = &zcu.intern_pool; | 3868 | const ip = &zcu.intern_pool; |
| 4547 | // Not even allowed to call panic in a naked function. | 3869 | // Not even allowed to call panic in a naked function. |
| 4548 | if (f.object.dg.is_naked_fn) return .none; | 3870 | if (f.dg.is_naked_fn) return .none; |
| 4549 | | 3871 | |
| 4550 | const gpa = f.object.dg.gpa; | 3872 | const gpa = f.dg.gpa; |
| 4551 | const w = &f.object.code.writer; | 3873 | const w = &f.code.writer; |
| 4552 | | 3874 | |
| 4553 | const call = f.air.unwrapCall(inst); | 3875 | const call = f.air.unwrapCall(inst); |
| 4554 | const args = call.args; | 3876 | const args = call.args; |
| ... | @@ -4557,27 +3879,11 @@ fn airCall( | ... | @@ -4557,27 +3879,11 @@ fn airCall( |
| 4557 | defer gpa.free(resolved_args); | 3879 | defer gpa.free(resolved_args); |
| 4558 | for (resolved_args, args) |*resolved_arg, arg| { | 3880 | for (resolved_args, args) |*resolved_arg, arg| { |
| 4559 | const arg_ty = f.typeOf(arg); | 3881 | const arg_ty = f.typeOf(arg); |
| 4560 | const arg_ctype = try f.ctypeFromType(arg_ty, .parameter); | 3882 | if (!arg_ty.hasRuntimeBits(zcu)) { |
| 4561 | if (arg_ctype.index == .void) { | | |
| 4562 | resolved_arg.* = .none; | 3883 | resolved_arg.* = .none; |
| 4563 | continue; | 3884 | continue; |
| 4564 | } | 3885 | } |
| 4565 | resolved_arg.* = try f.resolveInst(arg); | 3886 | resolved_arg.* = try f.resolveInst(arg); |
| 4566 | if (!arg_ctype.eql(try f.ctypeFromType(arg_ty, .complete))) { | | |
| 4567 | const array_local = try f.allocAlignedLocal(inst, .{ | | |
| 4568 | .ctype = arg_ctype, | | |
| 4569 | .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)), | | |
| 4570 | }); | | |
| 4571 | try w.writeAll("memcpy("); | | |
| 4572 | try f.writeCValueMember(w, array_local, .{ .identifier = "array" }); | | |
| 4573 | try w.writeAll(", "); | | |
| 4574 | try f.writeCValue(w, resolved_arg.*, .FunctionArgument); | | |
| 4575 | try w.writeAll(", sizeof("); | | |
| 4576 | try f.renderCType(w, arg_ctype); | | |
| 4577 | try w.writeAll("));"); | | |
| 4578 | try f.object.newline(); | | |
| 4579 | resolved_arg.* = array_local; | | |
| 4580 | } | | |
| 4581 | } | 3887 | } |
| 4582 | | 3888 | |
| 4583 | const callee = try f.resolveInst(call.callee); | 3889 | const callee = try f.resolveInst(call.callee); |
| ... | @@ -4596,28 +3902,22 @@ fn airCall( | ... | @@ -4596,28 +3902,22 @@ fn airCall( |
| 4596 | }; | 3902 | }; |
| 4597 | const fn_info = zcu.typeToFunc(if (callee_is_ptr) callee_ty.childType(zcu) else callee_ty).?; | 3903 | const fn_info = zcu.typeToFunc(if (callee_is_ptr) callee_ty.childType(zcu) else callee_ty).?; |
| 4598 | const ret_ty: Type = .fromInterned(fn_info.return_type); | 3904 | const ret_ty: Type = .fromInterned(fn_info.return_type); |
| 4599 | const ret_ctype: CType = if (ret_ty.isNoReturn(zcu)) | | |
| 4600 | .void | | |
| 4601 | else | | |
| 4602 | try f.ctypeFromType(ret_ty, .parameter); | | |
| 4603 | | 3905 | |
| 4604 | const result_local = result: { | 3906 | const result_local = result: { |
| 4605 | if (modifier == .always_tail) { | 3907 | if (modifier == .always_tail) { |
| 4606 | try w.writeAll("zig_always_tail return "); | 3908 | try w.writeAll("zig_always_tail return "); |
| 4607 | break :result .none; | 3909 | break :result .none; |
| 4608 | } else if (ret_ctype.index == .void) { | 3910 | } else if (!ret_ty.hasRuntimeBits(zcu)) { |
| 4609 | break :result .none; | 3911 | break :result .none; |
| 4610 | } else if (f.liveness.isUnused(inst)) { | 3912 | } else if (f.liveness.isUnused(inst)) { |
| 4611 | try w.writeByte('('); | 3913 | try w.writeAll("(void)"); |
| 4612 | try f.renderCType(w, .void); | | |
| 4613 | try w.writeByte(')'); | | |
| 4614 | break :result .none; | 3914 | break :result .none; |
| 4615 | } else { | 3915 | } else { |
| 4616 | const local = try f.allocAlignedLocal(inst, .{ | 3916 | const local = try f.allocAlignedLocal(inst, .{ |
| 4617 | .ctype = ret_ctype, | 3917 | .type = ret_ty, |
| 4618 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)), | 3918 | .alignment = .none, |
| 4619 | }); | 3919 | }); |
| 4620 | try f.writeCValue(w, local, .Other); | 3920 | try f.writeCValue(w, local, .other); |
| 4621 | try w.writeAll(" = "); | 3921 | try w.writeAll(" = "); |
| 4622 | break :result local; | 3922 | break :result local; |
| 4623 | } | 3923 | } |
| ... | @@ -4644,8 +3944,19 @@ fn airCall( | ... | @@ -4644,8 +3944,19 @@ fn airCall( |
| 4644 | if (!callee_is_ptr) try w.writeByte('&'); | 3944 | if (!callee_is_ptr) try w.writeByte('&'); |
| 4645 | } | 3945 | } |
| 4646 | switch (modifier) { | 3946 | switch (modifier) { |
| 4647 | .auto, .always_tail => try f.object.dg.renderNavName(w, fn_nav), | 3947 | .auto, .always_tail => try renderNavName(w, fn_nav, ip), |
| 4648 | inline .never_tail, .never_inline => |m| try w.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))), | 3948 | .never_tail => { |
| | 3949 | try f.need_never_tail_funcs.put(gpa, fn_nav, {}); |
| | 3950 | try w.print("zig_never_tail_{f}__{d}", .{ |
| | 3951 | fmtIdentUnsolo(ip.getNav(fn_nav).name.toSlice(ip)), @intFromEnum(fn_nav), |
| | 3952 | }); |
| | 3953 | }, |
| | 3954 | .never_inline => { |
| | 3955 | try f.need_never_inline_funcs.put(gpa, fn_nav, {}); |
| | 3956 | try w.print("zig_never_inline_{f}__{d}", .{ |
| | 3957 | fmtIdentUnsolo(ip.getNav(fn_nav).name.toSlice(ip)), @intFromEnum(fn_nav), |
| | 3958 | }); |
| | 3959 | }, |
| 4649 | else => unreachable, | 3960 | else => unreachable, |
| 4650 | } | 3961 | } |
| 4651 | if (need_cast) try w.writeByte(')'); | 3962 | if (need_cast) try w.writeByte(')'); |
| ... | @@ -4658,7 +3969,7 @@ fn airCall( | ... | @@ -4658,7 +3969,7 @@ fn airCall( |
| 4658 | else => unreachable, | 3969 | else => unreachable, |
| 4659 | } | 3970 | } |
| 4660 | // Fall back to function pointer call. | 3971 | // Fall back to function pointer call. |
| 4661 | try f.writeCValue(w, callee, .Other); | 3972 | try f.writeCValue(w, callee, .other); |
| 4662 | } | 3973 | } |
| 4663 | | 3974 | |
| 4664 | try w.writeByte('('); | 3975 | try w.writeByte('('); |
| ... | @@ -4667,38 +3978,20 @@ fn airCall( | ... | @@ -4667,38 +3978,20 @@ fn airCall( |
| 4667 | if (resolved_arg == .none) continue; | 3978 | if (resolved_arg == .none) continue; |
| 4668 | if (need_comma) try w.writeAll(", "); | 3979 | if (need_comma) try w.writeAll(", "); |
| 4669 | need_comma = true; | 3980 | need_comma = true; |
| 4670 | try f.writeCValue(w, resolved_arg, .FunctionArgument); | 3981 | try f.writeCValue(w, resolved_arg, .other); |
| 4671 | try f.freeCValue(inst, resolved_arg); | | |
| 4672 | } | 3982 | } |
| 4673 | try w.writeAll(");"); | 3983 | try w.writeAll(");"); |
| 4674 | switch (modifier) { | 3984 | switch (modifier) { |
| 4675 | .always_tail => try w.writeByte('\n'), | 3985 | .always_tail => try w.writeByte('\n'), |
| 4676 | else => try f.object.newline(), | 3986 | else => try f.newline(), |
| 4677 | } | 3987 | } |
| 4678 | | 3988 | |
| 4679 | const result = result: { | 3989 | return result_local; |
| 4680 | if (result_local == .none or !lowersToArray(ret_ty, zcu)) | | |
| 4681 | break :result result_local; | | |
| 4682 | | | |
| 4683 | const array_local = try f.allocLocal(inst, ret_ty); | | |
| 4684 | try w.writeAll("memcpy("); | | |
| 4685 | try f.writeCValue(w, array_local, .FunctionArgument); | | |
| 4686 | try w.writeAll(", "); | | |
| 4687 | try f.writeCValueMember(w, result_local, .{ .identifier = "array" }); | | |
| 4688 | try w.writeAll(", sizeof("); | | |
| 4689 | try f.renderType(w, ret_ty); | | |
| 4690 | try w.writeAll("));"); | | |
| 4691 | try f.object.newline(); | | |
| 4692 | try freeLocal(f, inst, result_local.new_local, null); | | |
| 4693 | break :result array_local; | | |
| 4694 | }; | | |
| 4695 | | | |
| 4696 | return result; | | |
| 4697 | } | 3990 | } |
| 4698 | | 3991 | |
| 4699 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { | 3992 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4700 | const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; | 3993 | const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 4701 | const w = &f.object.code.writer; | 3994 | const w = &f.code.writer; |
| 4702 | // TODO re-evaluate whether to emit these or not. If we naively emit | 3995 | // TODO re-evaluate whether to emit these or not. If we naively emit |
| 4703 | // these directives, the output file will report bogus line numbers because | 3996 | // these directives, the output file will report bogus line numbers because |
| 4704 | // every newline after the #line directive adds one to the line. | 3997 | // every newline after the #line directive adds one to the line. |
| ... | @@ -4707,32 +4000,32 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4707,32 +4000,32 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4707 | // newlines until the next dbg_stmt occurs. | 4000 | // newlines until the next dbg_stmt occurs. |
| 4708 | // Perhaps an additional compilation option is in order? | 4001 | // Perhaps an additional compilation option is in order? |
| 4709 | //try w.print("#line {d}", .{dbg_stmt.line + 1}); | 4002 | //try w.print("#line {d}", .{dbg_stmt.line + 1}); |
| 4710 | //try f.object.newline(); | 4003 | //try f.newline(); |
| 4711 | try w.print("/* file:{d}:{d} */", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); | 4004 | try w.print("/* file:{d}:{d} */", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); |
| 4712 | try f.object.newline(); | 4005 | try f.newline(); |
| 4713 | return .none; | 4006 | return .none; |
| 4714 | } | 4007 | } |
| 4715 | | 4008 | |
| 4716 | fn airDbgEmptyStmt(f: *Function, _: Air.Inst.Index) !CValue { | 4009 | fn airDbgEmptyStmt(f: *Function, _: Air.Inst.Index) !CValue { |
| 4717 | try f.object.code.writer.writeAll("(void)0;"); | 4010 | try f.code.writer.writeAll("(void)0;"); |
| 4718 | try f.object.newline(); | 4011 | try f.newline(); |
| 4719 | return .none; | 4012 | return .none; |
| 4720 | } | 4013 | } |
| 4721 | | 4014 | |
| 4722 | fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { | 4015 | fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4723 | const pt = f.object.dg.pt; | 4016 | const pt = f.dg.pt; |
| 4724 | const zcu = pt.zcu; | 4017 | const zcu = pt.zcu; |
| 4725 | const ip = &zcu.intern_pool; | 4018 | const ip = &zcu.intern_pool; |
| 4726 | const block = f.air.unwrapDbgBlock(inst); | 4019 | const block = f.air.unwrapDbgBlock(inst); |
| 4727 | const owner_nav = ip.getNav(zcu.funcInfo(block.func).owner_nav); | 4020 | const owner_nav = ip.getNav(zcu.funcInfo(block.func).owner_nav); |
| 4728 | const w = &f.object.code.writer; | 4021 | const w = &f.code.writer; |
| 4729 | try w.print("/* inline:{f} */", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); | 4022 | try w.print("/* inline:{f} */", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); |
| 4730 | try f.object.newline(); | 4023 | try f.newline(); |
| 4731 | return lowerBlock(f, inst, block.body); | 4024 | return lowerBlock(f, inst, block.body); |
| 4732 | } | 4025 | } |
| 4733 | | 4026 | |
| 4734 | fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { | 4027 | fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4735 | const pt = f.object.dg.pt; | 4028 | const pt = f.dg.pt; |
| 4736 | const zcu = pt.zcu; | 4029 | const zcu = pt.zcu; |
| 4737 | const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)]; | 4030 | const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4738 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 4031 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| ... | @@ -4741,9 +4034,9 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4741,9 +4034,9 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4741 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); | 4034 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); |
| 4742 | | 4035 | |
| 4743 | try reap(f, inst, &.{pl_op.operand}); | 4036 | try reap(f, inst, &.{pl_op.operand}); |
| 4744 | const w = &f.object.code.writer; | 4037 | const w = &f.code.writer; |
| 4745 | try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) }); | 4038 | try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) }); |
| 4746 | try f.object.newline(); | 4039 | try f.newline(); |
| 4747 | return .none; | 4040 | return .none; |
| 4748 | } | 4041 | } |
| 4749 | | 4042 | |
| ... | @@ -4753,13 +4046,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4753,13 +4046,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4753 | } | 4046 | } |
| 4754 | | 4047 | |
| 4755 | fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) !CValue { | 4048 | fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) !CValue { |
| 4756 | const pt = f.object.dg.pt; | 4049 | const pt = f.dg.pt; |
| 4757 | const zcu = pt.zcu; | 4050 | const zcu = pt.zcu; |
| 4758 | const liveness_block = f.liveness.getBlock(inst); | 4051 | const liveness_block = f.liveness.getBlock(inst); |
| 4759 | | 4052 | |
| 4760 | const block_id = f.next_block_index; | 4053 | const block_id = f.next_block_index; |
| 4761 | f.next_block_index += 1; | 4054 | f.next_block_index += 1; |
| 4762 | const w = &f.object.code.writer; | 4055 | const w = &f.code.writer; |
| 4763 | | 4056 | |
| 4764 | const inst_ty = f.typeOfIndex(inst); | 4057 | const inst_ty = f.typeOfIndex(inst); |
| 4765 | const result = if (inst_ty.hasRuntimeBits(zcu) and !f.liveness.isUnused(inst)) | 4058 | const result = if (inst_ty.hasRuntimeBits(zcu) and !f.liveness.isUnused(inst)) |
| ... | @@ -4767,7 +4060,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -4767,7 +4060,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4767 | else | 4060 | else |
| 4768 | .none; | 4061 | .none; |
| 4769 | | 4062 | |
| 4770 | try f.blocks.putNoClobber(f.object.dg.gpa, inst, .{ | 4063 | try f.blocks.putNoClobber(f.dg.gpa, inst, .{ |
| 4771 | .block_id = block_id, | 4064 | .block_id = block_id, |
| 4772 | .result = result, | 4065 | .result = result, |
| 4773 | }); | 4066 | }); |
| ... | @@ -4782,23 +4075,23 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) | ... | @@ -4782,23 +4075,23 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4782 | } | 4075 | } |
| 4783 | | 4076 | |
| 4784 | // noreturn blocks have no `br` instructions reaching them, so we don't want a label | 4077 | // noreturn blocks have no `br` instructions reaching them, so we don't want a label |
| 4785 | if (f.object.dg.is_naked_fn) { | 4078 | if (f.dg.is_naked_fn) { |
| 4786 | if (f.object.dg.expected_block) |expected_block| { | 4079 | if (f.dg.expected_block) |expected_block| { |
| 4787 | if (block_id != expected_block) | 4080 | if (block_id != expected_block) |
| 4788 | return f.fail("runtime code not allowed in naked function", .{}); | 4081 | return f.fail("runtime code not allowed in naked function", .{}); |
| 4789 | f.object.dg.expected_block = null; | 4082 | f.dg.expected_block = null; |
| 4790 | } | 4083 | } |
| 4791 | } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) { | 4084 | } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) { |
| 4792 | // label must be followed by an expression, include an empty one. | 4085 | // label must be followed by an expression, include an empty one. |
| 4793 | try w.print("\nzig_block_{d}:;", .{block_id}); | 4086 | try w.print("\nzig_block_{d}:;", .{block_id}); |
| 4794 | try f.object.newline(); | 4087 | try f.newline(); |
| 4795 | } | 4088 | } |
| 4796 | | 4089 | |
| 4797 | return result; | 4090 | return result; |
| 4798 | } | 4091 | } |
| 4799 | | 4092 | |
| 4800 | fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { | 4093 | fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4801 | const pt = f.object.dg.pt; | 4094 | const pt = f.dg.pt; |
| 4802 | const unwrapped_try = f.air.unwrapTry(inst); | 4095 | const unwrapped_try = f.air.unwrapTry(inst); |
| 4803 | const body = unwrapped_try.else_body; | 4096 | const body = unwrapped_try.else_body; |
| 4804 | const err_union_ty = f.air.typeOf(unwrapped_try.error_union, &pt.zcu.intern_pool); | 4097 | const err_union_ty = f.air.typeOf(unwrapped_try.error_union, &pt.zcu.intern_pool); |
| ... | @@ -4806,7 +4099,7 @@ fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4806,7 +4099,7 @@ fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4806 | } | 4099 | } |
| 4807 | | 4100 | |
| 4808 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 4101 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4809 | const pt = f.object.dg.pt; | 4102 | const pt = f.dg.pt; |
| 4810 | const unwrapped_try = f.air.unwrapTryPtr(inst); | 4103 | const unwrapped_try = f.air.unwrapTryPtr(inst); |
| 4811 | const body = unwrapped_try.else_body; | 4104 | const body = unwrapped_try.else_body; |
| 4812 | const err_union_ty = f.air.typeOf(unwrapped_try.error_union_ptr, &pt.zcu.intern_pool).childType(pt.zcu); | 4105 | const err_union_ty = f.air.typeOf(unwrapped_try.error_union_ptr, &pt.zcu.intern_pool).childType(pt.zcu); |
| ... | @@ -4821,46 +4114,38 @@ fn lowerTry( | ... | @@ -4821,46 +4114,38 @@ fn lowerTry( |
| 4821 | err_union_ty: Type, | 4114 | err_union_ty: Type, |
| 4822 | is_ptr: bool, | 4115 | is_ptr: bool, |
| 4823 | ) !CValue { | 4116 | ) !CValue { |
| 4824 | const pt = f.object.dg.pt; | 4117 | const pt = f.dg.pt; |
| 4825 | const zcu = pt.zcu; | 4118 | const zcu = pt.zcu; |
| 4826 | const err_union = try f.resolveInst(operand); | 4119 | const err_union = try f.resolveInst(operand); |
| 4827 | const inst_ty = f.typeOfIndex(inst); | 4120 | const inst_ty = f.typeOfIndex(inst); |
| 4828 | const liveness_condbr = f.liveness.getCondBr(inst); | 4121 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4829 | const w = &f.object.code.writer; | 4122 | const w = &f.code.writer; |
| 4830 | const payload_ty = err_union_ty.errorUnionPayload(zcu); | 4123 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 4831 | const payload_has_bits = payload_ty.hasRuntimeBits(zcu); | | |
| 4832 | | 4124 | |
| 4833 | if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { | 4125 | try w.writeAll("if ("); |
| 4834 | try w.writeAll("if ("); | | |
| 4835 | if (!payload_has_bits) { | | |
| 4836 | if (is_ptr) | | |
| 4837 | try f.writeCValueDeref(w, err_union) | | |
| 4838 | else | | |
| 4839 | try f.writeCValue(w, err_union, .Other); | | |
| 4840 | } else { | | |
| 4841 | // Reap the operand so that it can be reused inside genBody. | | |
| 4842 | // Remember we must avoid calling reap() twice for the same operand | | |
| 4843 | // in this function. | | |
| 4844 | try reap(f, inst, &.{operand}); | | |
| 4845 | if (is_ptr) | | |
| 4846 | try f.writeCValueDerefMember(w, err_union, .{ .identifier = "error" }) | | |
| 4847 | else | | |
| 4848 | try f.writeCValueMember(w, err_union, .{ .identifier = "error" }); | | |
| 4849 | } | | |
| 4850 | try w.writeAll(") "); | | |
| 4851 | | 4126 | |
| 4852 | try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false); | 4127 | // Reap the operand so that it can be reused inside genBody. |
| 4853 | try f.object.newline(); | 4128 | // Remember we must avoid calling reap() twice for the same operand |
| 4854 | if (f.object.dg.expected_block) |_| | 4129 | // in this function. |
| 4855 | return f.fail("runtime code not allowed in naked function", .{}); | 4130 | try reap(f, inst, &.{operand}); |
| 4856 | } | 4131 | if (is_ptr) |
| | 4132 | try f.writeCValueDerefMember(w, err_union, .{ .identifier = "error" }) |
| | 4133 | else |
| | 4134 | try f.writeCValueMember(w, err_union, .{ .identifier = "error" }); |
| | 4135 | |
| | 4136 | try w.writeAll(") "); |
| | 4137 | |
| | 4138 | try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false); |
| | 4139 | try f.newline(); |
| | 4140 | if (f.dg.expected_block) |_| |
| | 4141 | return f.fail("runtime code not allowed in naked function", .{}); |
| 4857 | | 4142 | |
| 4858 | // Now we have the "then branch" (in terms of the liveness data); process any deaths. | 4143 | // Now we have the "then branch" (in terms of the liveness data); process any deaths. |
| 4859 | for (liveness_condbr.then_deaths) |death| { | 4144 | for (liveness_condbr.then_deaths) |death| { |
| 4860 | try die(f, inst, death.toRef()); | 4145 | try die(f, inst, death.toRef()); |
| 4861 | } | 4146 | } |
| 4862 | | 4147 | |
| 4863 | if (!payload_has_bits) { | 4148 | if (!payload_ty.hasRuntimeBits(zcu)) { |
| 4864 | if (!is_ptr) { | 4149 | if (!is_ptr) { |
| 4865 | return .none; | 4150 | return .none; |
| 4866 | } else { | 4151 | } else { |
| ... | @@ -4873,14 +4158,14 @@ fn lowerTry( | ... | @@ -4873,14 +4158,14 @@ fn lowerTry( |
| 4873 | if (f.liveness.isUnused(inst)) return .none; | 4158 | if (f.liveness.isUnused(inst)) return .none; |
| 4874 | | 4159 | |
| 4875 | const local = try f.allocLocal(inst, inst_ty); | 4160 | const local = try f.allocLocal(inst, inst_ty); |
| 4876 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 4161 | try f.writeCValue(w, local, .other); |
| 4877 | try f.writeCValue(w, local, .Other); | 4162 | try w.writeAll(" = "); |
| 4878 | try a.assign(f, w); | | |
| 4879 | if (is_ptr) { | 4163 | if (is_ptr) { |
| 4880 | try w.writeByte('&'); | 4164 | try w.writeByte('&'); |
| 4881 | try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" }); | 4165 | try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" }); |
| 4882 | } else try f.writeCValueMember(w, err_union, .{ .identifier = "payload" }); | 4166 | } else try f.writeCValueMember(w, err_union, .{ .identifier = "payload" }); |
| 4883 | try a.end(f, w); | 4167 | try w.writeByte(';'); |
| | 4168 | try f.newline(); |
| 4884 | return local; | 4169 | return local; |
| 4885 | } | 4170 | } |
| 4886 | | 4171 | |
| ... | @@ -4888,25 +4173,24 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { | ... | @@ -4888,25 +4173,24 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { |
| 4888 | const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br; | 4173 | const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4889 | const block = f.blocks.get(branch.block_inst).?; | 4174 | const block = f.blocks.get(branch.block_inst).?; |
| 4890 | const result = block.result; | 4175 | const result = block.result; |
| 4891 | const w = &f.object.code.writer; | 4176 | const w = &f.code.writer; |
| 4892 | | 4177 | |
| 4893 | if (f.object.dg.is_naked_fn) { | 4178 | if (f.dg.is_naked_fn) { |
| 4894 | if (result != .none) return f.fail("runtime code not allowed in naked function", .{}); | 4179 | if (result != .none) return f.fail("runtime code not allowed in naked function", .{}); |
| 4895 | f.object.dg.expected_block = block.block_id; | 4180 | f.dg.expected_block = block.block_id; |
| 4896 | return; | 4181 | return; |
| 4897 | } | 4182 | } |
| 4898 | | 4183 | |
| 4899 | // If result is .none then the value of the block is unused. | 4184 | // If result is .none then the value of the block is unused. |
| 4900 | if (result != .none) { | 4185 | if (result != .none) { |
| 4901 | const operand_ty = f.typeOf(branch.operand); | | |
| 4902 | const operand = try f.resolveInst(branch.operand); | 4186 | const operand = try f.resolveInst(branch.operand); |
| 4903 | try reap(f, inst, &.{branch.operand}); | 4187 | try reap(f, inst, &.{branch.operand}); |
| 4904 | | 4188 | |
| 4905 | const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete)); | 4189 | try f.writeCValue(w, result, .other); |
| 4906 | try f.writeCValue(w, result, .Other); | 4190 | try w.writeAll(" = "); |
| 4907 | try a.assign(f, w); | 4191 | try f.writeCValue(w, operand, .other); |
| 4908 | try f.writeCValue(w, operand, .Other); | 4192 | try w.writeByte(';'); |
| 4909 | try a.end(f, w); | 4193 | try f.newline(); |
| 4910 | } | 4194 | } |
| 4911 | | 4195 | |
| 4912 | try w.print("goto zig_block_{d};\n", .{block.block_id}); | 4196 | try w.print("goto zig_block_{d};\n", .{block.block_id}); |
| ... | @@ -4914,14 +4198,14 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { | ... | @@ -4914,14 +4198,14 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { |
| 4914 | | 4198 | |
| 4915 | fn airRepeat(f: *Function, inst: Air.Inst.Index) !void { | 4199 | fn airRepeat(f: *Function, inst: Air.Inst.Index) !void { |
| 4916 | const repeat = f.air.instructions.items(.data)[@intFromEnum(inst)].repeat; | 4200 | const repeat = f.air.instructions.items(.data)[@intFromEnum(inst)].repeat; |
| 4917 | try f.object.code.writer.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)}); | 4201 | try f.code.writer.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)}); |
| 4918 | } | 4202 | } |
| 4919 | | 4203 | |
| 4920 | fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { | 4204 | fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { |
| 4921 | const pt = f.object.dg.pt; | 4205 | const pt = f.dg.pt; |
| 4922 | const zcu = pt.zcu; | 4206 | const zcu = pt.zcu; |
| 4923 | const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br; | 4207 | const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4924 | const w = &f.object.code.writer; | 4208 | const w = &f.code.writer; |
| 4925 | | 4209 | |
| 4926 | if (try f.air.value(br.operand, pt)) |cond_val| { | 4210 | if (try f.air.value(br.operand, pt)) |cond_val| { |
| 4927 | // Comptime-known dispatch. Iterate the cases to find the correct | 4211 | // Comptime-known dispatch. Iterate the cases to find the correct |
| ... | @@ -4950,11 +4234,11 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { | ... | @@ -4950,11 +4234,11 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { |
| 4950 | // Runtime-known dispatch. Set the switch condition, and branch back. | 4234 | // Runtime-known dispatch. Set the switch condition, and branch back. |
| 4951 | const cond = try f.resolveInst(br.operand); | 4235 | const cond = try f.resolveInst(br.operand); |
| 4952 | const cond_local = f.loop_switch_conds.get(br.block_inst).?; | 4236 | const cond_local = f.loop_switch_conds.get(br.block_inst).?; |
| 4953 | try f.writeCValue(w, .{ .local = cond_local }, .Other); | 4237 | try f.writeCValue(w, .{ .local = cond_local }, .other); |
| 4954 | try w.writeAll(" = "); | 4238 | try w.writeAll(" = "); |
| 4955 | try f.writeCValue(w, cond, .Other); | 4239 | try f.writeCValue(w, cond, .other); |
| 4956 | try w.writeByte(';'); | 4240 | try w.writeByte(';'); |
| 4957 | try f.object.newline(); | 4241 | try f.newline(); |
| 4958 | try w.print("goto zig_switch_{d}_loop;\n", .{@intFromEnum(br.block_inst)}); | 4242 | try w.print("goto zig_switch_{d}_loop;\n", .{@intFromEnum(br.block_inst)}); |
| 4959 | } | 4243 | } |
| 4960 | | 4244 | |
| ... | @@ -4971,11 +4255,10 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4971,11 +4255,10 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4971 | } | 4255 | } |
| 4972 | | 4256 | |
| 4973 | fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CValue { | 4257 | fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CValue { |
| 4974 | const pt = f.object.dg.pt; | 4258 | const pt = f.dg.pt; |
| 4975 | const zcu = pt.zcu; | 4259 | const zcu = pt.zcu; |
| 4976 | const target = &f.object.dg.mod.resolved_target.result; | 4260 | const target = &f.dg.mod.resolved_target.result; |
| 4977 | const ctype_pool = &f.object.dg.ctype_pool; | 4261 | const w = &f.code.writer; |
| 4978 | const w = &f.object.code.writer; | | |
| 4979 | | 4262 | |
| 4980 | if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) { | 4263 | if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) { |
| 4981 | const src_info = dest_ty.intInfo(zcu); | 4264 | const src_info = dest_ty.intInfo(zcu); |
| ... | @@ -4986,26 +4269,16 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal | ... | @@ -4986,26 +4269,16 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 4986 | | 4269 | |
| 4987 | if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) { | 4270 | if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) { |
| 4988 | const local = try f.allocLocal(null, dest_ty); | 4271 | const local = try f.allocLocal(null, dest_ty); |
| 4989 | try f.writeCValue(w, local, .Other); | 4272 | try f.writeCValue(w, local, .other); |
| 4990 | try w.writeAll(" = ("); | 4273 | try w.writeAll(" = ("); |
| 4991 | try f.renderType(w, dest_ty); | 4274 | try f.renderType(w, dest_ty); |
| 4992 | try w.writeByte(')'); | 4275 | try w.writeByte(')'); |
| 4993 | try f.writeCValue(w, operand, .Other); | 4276 | try f.writeCValue(w, operand, .other); |
| 4994 | try w.writeByte(';'); | 4277 | try w.writeByte(';'); |
| 4995 | try f.object.newline(); | 4278 | try f.newline(); |
| 4996 | return local; | 4279 | return local; |
| 4997 | } | 4280 | } |
| 4998 | | 4281 | |
| 4999 | const operand_lval = if (operand == .constant) blk: { | | |
| 5000 | const operand_local = try f.allocLocal(null, operand_ty); | | |
| 5001 | try f.writeCValue(w, operand_local, .Other); | | |
| 5002 | try w.writeAll(" = "); | | |
| 5003 | try f.writeCValue(w, operand, .Other); | | |
| 5004 | try w.writeByte(';'); | | |
| 5005 | try f.object.newline(); | | |
| 5006 | break :blk operand_local; | | |
| 5007 | } else operand; | | |
| 5008 | | | |
| 5009 | const local = try f.allocLocal(null, dest_ty); | 4282 | const local = try f.allocLocal(null, dest_ty); |
| 5010 | // On big-endian targets, copying ABI integers with padding bits is awkward, because the padding bits are at the low bytes of the value. | 4283 | // On big-endian targets, copying ABI integers with padding bits is awkward, because the padding bits are at the low bytes of the value. |
| 5011 | // We need to offset the source or destination pointer appropriately and copy the right number of bytes. | 4284 | // We need to offset the source or destination pointer appropriately and copy the right number of bytes. |
| ... | @@ -5013,141 +4286,134 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal | ... | @@ -5013,141 +4286,134 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 5013 | // e.g. [10]u8 -> u80. We need to offset the destination so that we copy to the least significant bits of the integer. | 4286 | // e.g. [10]u8 -> u80. We need to offset the destination so that we copy to the least significant bits of the integer. |
| 5014 | const offset = dest_ty.abiSize(zcu) - operand_ty.abiSize(zcu); | 4287 | const offset = dest_ty.abiSize(zcu) - operand_ty.abiSize(zcu); |
| 5015 | try w.writeAll("memcpy((char *)&"); | 4288 | try w.writeAll("memcpy((char *)&"); |
| 5016 | try f.writeCValue(w, local, .Other); | 4289 | try f.writeCValue(w, local, .other); |
| 5017 | try w.print(" + {d}, &", .{offset}); | 4290 | try w.print(" + {d}, &", .{offset}); |
| 5018 | try f.writeCValue(w, operand_lval, .Other); | 4291 | switch (operand) { |
| | 4292 | .constant => |val| try f.dg.renderValueAsLvalue(w, val), |
| | 4293 | else => try f.writeCValue(w, operand, .other), |
| | 4294 | } |
| 5019 | try w.print(", {d});", .{operand_ty.abiSize(zcu)}); | 4295 | try w.print(", {d});", .{operand_ty.abiSize(zcu)}); |
| 5020 | } else if (target.cpu.arch.endian() == .big and operand_ty.isAbiInt(zcu) and !dest_ty.isAbiInt(zcu)) { | 4296 | } else if (target.cpu.arch.endian() == .big and operand_ty.isAbiInt(zcu) and !dest_ty.isAbiInt(zcu)) { |
| 5021 | // e.g. u80 -> [10]u8. We need to offset the source so that we copy from the least significant bits of the integer. | 4297 | // e.g. u80 -> [10]u8. We need to offset the source so that we copy from the least significant bits of the integer. |
| 5022 | const offset = operand_ty.abiSize(zcu) - dest_ty.abiSize(zcu); | 4298 | const offset = operand_ty.abiSize(zcu) - dest_ty.abiSize(zcu); |
| 5023 | try w.writeAll("memcpy(&"); | 4299 | try w.writeAll("memcpy(&"); |
| 5024 | try f.writeCValue(w, local, .Other); | 4300 | try f.writeCValue(w, local, .other); |
| 5025 | try w.writeAll(", (const char *)&"); | 4301 | try w.writeAll(", (const char *)&"); |
| 5026 | try f.writeCValue(w, operand_lval, .Other); | 4302 | switch (operand) { |
| | 4303 | .constant => |val| try f.dg.renderValueAsLvalue(w, val), |
| | 4304 | else => try f.writeCValue(w, operand, .other), |
| | 4305 | } |
| 5027 | try w.print(" + {d}, {d});", .{ offset, dest_ty.abiSize(zcu) }); | 4306 | try w.print(" + {d}, {d});", .{ offset, dest_ty.abiSize(zcu) }); |
| 5028 | } else { | 4307 | } else { |
| 5029 | try w.writeAll("memcpy(&"); | 4308 | try w.writeAll("memcpy(&"); |
| 5030 | try f.writeCValue(w, local, .Other); | 4309 | try f.writeCValue(w, local, .other); |
| 5031 | try w.writeAll(", &"); | 4310 | try w.writeAll(", &"); |
| 5032 | try f.writeCValue(w, operand_lval, .Other); | 4311 | switch (operand) { |
| | 4312 | .constant => |val| try f.dg.renderValueAsLvalue(w, val), |
| | 4313 | else => try f.writeCValue(w, operand, .other), |
| | 4314 | } |
| 5033 | try w.print(", {d});", .{@min(dest_ty.abiSize(zcu), operand_ty.abiSize(zcu))}); | 4315 | try w.print(", {d});", .{@min(dest_ty.abiSize(zcu), operand_ty.abiSize(zcu))}); |
| 5034 | } | 4316 | } |
| 5035 | | 4317 | |
| 5036 | try f.object.newline(); | 4318 | try f.newline(); |
| 5037 | | 4319 | |
| 5038 | // Ensure padding bits have the expected value. | 4320 | // Ensure padding bits have the expected value. |
| 5039 | if (dest_ty.isAbiInt(zcu)) { | 4321 | if (dest_ty.isAbiInt(zcu)) { |
| 5040 | const dest_ctype = try f.ctypeFromType(dest_ty, .complete); | 4322 | switch (CType.classifyInt(dest_ty, zcu)) { |
| 5041 | const dest_info = dest_ty.intInfo(zcu); | 4323 | .void => unreachable, // opv |
| 5042 | var bits: u16 = dest_info.bits; | 4324 | .small => { |
| 5043 | var wrap_ctype: ?CType = null; | 4325 | try f.writeCValue(w, local, .other); |
| 5044 | var need_bitcasts = false; | 4326 | try w.writeAll(" = zig_wrap_"); |
| 5045 | | 4327 | try f.dg.renderTypeForBuiltinFnName(w, dest_ty); |
| 5046 | try f.writeCValue(w, local, .Other); | 4328 | try w.writeByte('('); |
| 5047 | switch (dest_ctype.info(ctype_pool)) { | 4329 | try f.writeCValue(w, local, .other); |
| 5048 | else => {}, | 4330 | try f.dg.renderBuiltinInfo(w, dest_ty, .bits); |
| 5049 | .array => |array_info| { | 4331 | try w.writeAll(");"); |
| 5050 | try w.print("[{d}]", .{switch (target.cpu.arch.endian()) { | 4332 | try f.newline(); |
| 5051 | .little => array_info.len - 1, | | |
| 5052 | .big => 0, | | |
| 5053 | }}); | | |
| 5054 | wrap_ctype = array_info.elem_ctype.toSignedness(dest_info.signedness); | | |
| 5055 | need_bitcasts = wrap_ctype.?.index == .zig_i128; | | |
| 5056 | bits -= 1; | | |
| 5057 | bits %= @as(u16, @intCast(f.byteSize(array_info.elem_ctype) * 8)); | | |
| 5058 | bits += 1; | | |
| 5059 | }, | 4333 | }, |
| 5060 | } | 4334 | .big => |big| { |
| 5061 | try w.writeAll(" = "); | 4335 | const dest_info = dest_ty.intInfo(zcu); |
| 5062 | if (need_bitcasts) { | 4336 | const padding_index: u16 = switch (target.cpu.arch.endian()) { |
| 5063 | try w.writeAll("zig_bitCast_"); | 4337 | .little => big.limbs_len - 1, |
| 5064 | try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?.toUnsigned()); | | |
| 5065 | try w.writeByte('('); | | |
| 5066 | } | | |
| 5067 | try w.writeAll("zig_wrap_"); | | |
| 5068 | const info_ty = try pt.intType(dest_info.signedness, bits); | | |
| 5069 | if (wrap_ctype) |ctype| | | |
| 5070 | try f.object.dg.renderCTypeForBuiltinFnName(w, ctype) | | |
| 5071 | else | | |
| 5072 | try f.object.dg.renderTypeForBuiltinFnName(w, info_ty); | | |
| 5073 | try w.writeByte('('); | | |
| 5074 | if (need_bitcasts) { | | |
| 5075 | try w.writeAll("zig_bitCast_"); | | |
| 5076 | try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?); | | |
| 5077 | try w.writeByte('('); | | |
| 5078 | } | | |
| 5079 | try f.writeCValue(w, local, .Other); | | |
| 5080 | switch (dest_ctype.info(ctype_pool)) { | | |
| 5081 | else => {}, | | |
| 5082 | .array => |array_info| try w.print("[{d}]", .{ | | |
| 5083 | switch (target.cpu.arch.endian()) { | | |
| 5084 | .little => array_info.len - 1, | | |
| 5085 | .big => 0, | 4338 | .big => 0, |
| 5086 | }, | 4339 | }; |
| 5087 | }), | 4340 | const wrap_bits = ((dest_info.bits - 1) % big.limb_size.bits()) + 1; |
| | 4341 | if (big.limb_size != .@"128" or dest_info.signedness == .unsigned) { |
| | 4342 | try f.writeCValueMember(w, local, .{ .identifier = "limbs" }); |
| | 4343 | try w.print("[{d}] = zig_wrap_{c}{d}(", .{ |
| | 4344 | padding_index, |
| | 4345 | signAbbrev(dest_info.signedness), |
| | 4346 | big.limb_size.bits(), |
| | 4347 | }); |
| | 4348 | try f.writeCValueMember(w, local, .{ .identifier = "limbs" }); |
| | 4349 | try w.print("[{d}], {d});", .{ padding_index, wrap_bits }); |
| | 4350 | } else { |
| | 4351 | try f.writeCValueMember(w, local, .{ .identifier = "limbs" }); |
| | 4352 | try w.print("[{d}] = zig_bitCast_u128(zig_wrap_i128(zig_bitCast_i128(", .{ |
| | 4353 | padding_index, |
| | 4354 | }); |
| | 4355 | try f.writeCValueMember(w, local, .{ .identifier = "limbs" }); |
| | 4356 | try w.print("[{d}]), {d}));", .{ padding_index, wrap_bits }); |
| | 4357 | try f.newline(); |
| | 4358 | } |
| | 4359 | }, |
| 5088 | } | 4360 | } |
| 5089 | if (need_bitcasts) try w.writeByte(')'); | | |
| 5090 | try f.object.dg.renderBuiltinInfo(w, info_ty, .bits); | | |
| 5091 | if (need_bitcasts) try w.writeByte(')'); | | |
| 5092 | try w.writeAll(");"); | | |
| 5093 | try f.object.newline(); | | |
| 5094 | } | 4361 | } |
| 5095 | | 4362 | |
| 5096 | try f.freeCValue(null, operand_lval); | | |
| 5097 | return local; | 4363 | return local; |
| 5098 | } | 4364 | } |
| 5099 | | 4365 | |
| 5100 | fn airTrap(f: *Function, w: *Writer) !void { | 4366 | fn airTrap(f: *Function) !void { |
| 5101 | // Not even allowed to call trap in a naked function. | 4367 | // Not even allowed to call trap in a naked function. |
| 5102 | if (f.object.dg.is_naked_fn) return; | 4368 | if (f.dg.is_naked_fn) return; |
| 5103 | try w.writeAll("zig_trap();\n"); | 4369 | try f.code.writer.writeAll("zig_trap();\n"); |
| 5104 | } | 4370 | } |
| 5105 | | 4371 | |
| 5106 | fn airBreakpoint(f: *Function) !CValue { | 4372 | fn airBreakpoint(f: *Function) !CValue { |
| 5107 | const w = &f.object.code.writer; | 4373 | const w = &f.code.writer; |
| 5108 | try w.writeAll("zig_breakpoint();"); | 4374 | try w.writeAll("zig_breakpoint();"); |
| 5109 | try f.object.newline(); | 4375 | try f.newline(); |
| 5110 | return .none; | 4376 | return .none; |
| 5111 | } | 4377 | } |
| 5112 | | 4378 | |
| 5113 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { | 4379 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5114 | const w = &f.object.code.writer; | 4380 | const w = &f.code.writer; |
| 5115 | const local = try f.allocLocal(inst, .usize); | 4381 | const local = try f.allocLocal(inst, .usize); |
| 5116 | try f.writeCValue(w, local, .Other); | 4382 | try f.writeCValue(w, local, .other); |
| 5117 | try w.writeAll(" = ("); | 4383 | try w.writeAll(" = ("); |
| 5118 | try f.renderType(w, .usize); | 4384 | try f.renderType(w, .usize); |
| 5119 | try w.writeAll(")zig_return_address();"); | 4385 | try w.writeAll(")zig_return_address();"); |
| 5120 | try f.object.newline(); | 4386 | try f.newline(); |
| 5121 | return local; | 4387 | return local; |
| 5122 | } | 4388 | } |
| 5123 | | 4389 | |
| 5124 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { | 4390 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5125 | const w = &f.object.code.writer; | 4391 | const w = &f.code.writer; |
| 5126 | const local = try f.allocLocal(inst, .usize); | 4392 | const local = try f.allocLocal(inst, .usize); |
| 5127 | try f.writeCValue(w, local, .Other); | 4393 | try f.writeCValue(w, local, .other); |
| 5128 | try w.writeAll(" = ("); | 4394 | try w.writeAll(" = ("); |
| 5129 | try f.renderType(w, .usize); | 4395 | try f.renderType(w, .usize); |
| 5130 | try w.writeAll(")zig_frame_address();"); | 4396 | try w.writeAll(")zig_frame_address();"); |
| 5131 | try f.object.newline(); | 4397 | try f.newline(); |
| 5132 | return local; | 4398 | return local; |
| 5133 | } | 4399 | } |
| 5134 | | 4400 | |
| 5135 | fn airUnreach(o: *Object) !void { | 4401 | fn airUnreach(f: *Function) !void { |
| 5136 | // Not even allowed to call unreachable in a naked function. | 4402 | // Not even allowed to call unreachable in a naked function. |
| 5137 | if (o.dg.is_naked_fn) return; | 4403 | if (f.dg.is_naked_fn) return; |
| 5138 | try o.code.writer.writeAll("zig_unreachable();\n"); | 4404 | try f.code.writer.writeAll("zig_unreachable();\n"); |
| 5139 | } | 4405 | } |
| 5140 | | 4406 | |
| 5141 | fn airLoop(f: *Function, inst: Air.Inst.Index) !void { | 4407 | fn airLoop(f: *Function, inst: Air.Inst.Index) !void { |
| 5142 | const block = f.air.unwrapBlock(inst); | 4408 | const block = f.air.unwrapBlock(inst); |
| 5143 | const w = &f.object.code.writer; | 4409 | const w = &f.code.writer; |
| 5144 | | 4410 | |
| 5145 | // `repeat` instructions matching this loop will branch to | 4411 | // `repeat` instructions matching this loop will branch to |
| 5146 | // this label. Since we need a label for arbitrary `repeat` | 4412 | // this label. Since we need a label for arbitrary `repeat` |
| 5147 | // anyway, there's actually no need to use a "real" looping | 4413 | // anyway, there's actually no need to use a "real" looping |
| 5148 | // construct at all! | 4414 | // construct at all! |
| 5149 | try w.print("zig_loop_{d}:", .{@intFromEnum(inst)}); | 4415 | try w.print("zig_loop_{d}:", .{@intFromEnum(inst)}); |
| 5150 | try f.object.newline(); | 4416 | try f.newline(); |
| 5151 | try genBodyInner(f, block.body); // no need to restore state, we're noreturn | 4417 | try genBodyInner(f, block.body); // no need to restore state, we're noreturn |
| 5152 | } | 4418 | } |
| 5153 | | 4419 | |
| ... | @@ -5158,15 +4424,15 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void { | ... | @@ -5158,15 +4424,15 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void { |
| 5158 | const then_body = cond_br.then_body; | 4424 | const then_body = cond_br.then_body; |
| 5159 | const else_body = cond_br.else_body; | 4425 | const else_body = cond_br.else_body; |
| 5160 | const liveness_condbr = f.liveness.getCondBr(inst); | 4426 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 5161 | const w = &f.object.code.writer; | 4427 | const w = &f.code.writer; |
| 5162 | | 4428 | |
| 5163 | try w.writeAll("if ("); | 4429 | try w.writeAll("if ("); |
| 5164 | try f.writeCValue(w, cond, .Other); | 4430 | try f.writeCValue(w, cond, .other); |
| 5165 | try w.writeAll(") "); | 4431 | try w.writeAll(") "); |
| 5166 | | 4432 | |
| 5167 | try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false); | 4433 | try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false); |
| 5168 | try f.object.newline(); | 4434 | try f.newline(); |
| 5169 | if (else_body.len > 0) if (f.object.dg.expected_block) |_| | 4435 | if (else_body.len > 0) if (f.dg.expected_block) |_| |
| 5170 | return f.fail("runtime code not allowed in naked function", .{}); | 4436 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5171 | | 4437 | |
| 5172 | // We don't need to use `genBodyResolveState` for the else block, because this instruction is | 4438 | // We don't need to use `genBodyResolveState` for the else block, because this instruction is |
| ... | @@ -5184,23 +4450,23 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void { | ... | @@ -5184,23 +4450,23 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void { |
| 5184 | } | 4450 | } |
| 5185 | | 4451 | |
| 5186 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void { | 4452 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void { |
| 5187 | const pt = f.object.dg.pt; | 4453 | const pt = f.dg.pt; |
| 5188 | const zcu = pt.zcu; | 4454 | const zcu = pt.zcu; |
| 5189 | const gpa = f.object.dg.gpa; | 4455 | const gpa = f.dg.gpa; |
| 5190 | const switch_br = f.air.unwrapSwitch(inst); | 4456 | const switch_br = f.air.unwrapSwitch(inst); |
| 5191 | const init_condition = try f.resolveInst(switch_br.operand); | 4457 | const init_condition = try f.resolveInst(switch_br.operand); |
| 5192 | try reap(f, inst, &.{switch_br.operand}); | 4458 | try reap(f, inst, &.{switch_br.operand}); |
| 5193 | const condition_ty = f.typeOf(switch_br.operand); | 4459 | const condition_ty = f.typeOf(switch_br.operand); |
| 5194 | const w = &f.object.code.writer; | 4460 | const w = &f.code.writer; |
| 5195 | | 4461 | |
| 5196 | // For dispatches, we will create a local alloc to contain the condition value. | 4462 | // For dispatches, we will create a local alloc to contain the condition value. |
| 5197 | // This may not result in optimal codegen for switch loops, but it minimizes the | 4463 | // This may not result in optimal codegen for switch loops, but it minimizes the |
| 5198 | // amount of C code we generate, which is probably more desirable here (and is simpler). | 4464 | // amount of C code we generate, which is probably more desirable here (and is simpler). |
| 5199 | const condition = if (is_dispatch_loop) cond: { | 4465 | const condition = if (is_dispatch_loop) cond: { |
| 5200 | const new_local = try f.allocLocal(inst, condition_ty); | 4466 | const new_local = try f.allocLocal(inst, condition_ty); |
| 5201 | try f.copyCValue(try f.ctypeFromType(condition_ty, .complete), new_local, init_condition); | 4467 | try f.copyCValue(new_local, init_condition); |
| 5202 | try w.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)}); | 4468 | try w.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)}); |
| 5203 | try f.object.newline(); | 4469 | try f.newline(); |
| 5204 | try f.loop_switch_conds.put(gpa, inst, new_local.new_local); | 4470 | try f.loop_switch_conds.put(gpa, inst, new_local.new_local); |
| 5205 | break :cond new_local; | 4471 | break :cond new_local; |
| 5206 | } else init_condition; | 4472 | } else init_condition; |
| ... | @@ -5222,9 +4488,9 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5222,9 +4488,9 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5222 | try f.renderType(w, lowered_condition_ty); | 4488 | try f.renderType(w, lowered_condition_ty); |
| 5223 | try w.writeByte(')'); | 4489 | try w.writeByte(')'); |
| 5224 | } | 4490 | } |
| 5225 | try f.writeCValue(w, condition, .Other); | 4491 | try f.writeCValue(w, condition, .other); |
| 5226 | try w.writeAll(") {"); | 4492 | try w.writeAll(") {"); |
| 5227 | f.object.indent(); | 4493 | f.indent(); |
| 5228 | | 4494 | |
| 5229 | const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1); | 4495 | const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1); |
| 5230 | defer gpa.free(liveness.deaths); | 4496 | defer gpa.free(liveness.deaths); |
| ... | @@ -5237,7 +4503,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5237,7 +4503,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5237 | continue; | 4503 | continue; |
| 5238 | } | 4504 | } |
| 5239 | for (case.items) |item| { | 4505 | for (case.items) |item| { |
| 5240 | try f.object.newline(); | 4506 | try f.newline(); |
| 5241 | try w.writeAll("case "); | 4507 | try w.writeAll("case "); |
| 5242 | const item_value = try f.air.value(item, pt); | 4508 | const item_value = try f.air.value(item, pt); |
| 5243 | // If `item_value` is a pointer with a known integer address, print the address | 4509 | // If `item_value` is a pointer with a known integer address, print the address |
| ... | @@ -5254,28 +4520,28 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5254,28 +4520,28 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5254 | try f.renderType(w, .usize); | 4520 | try f.renderType(w, .usize); |
| 5255 | try w.writeByte(')'); | 4521 | try w.writeByte(')'); |
| 5256 | } | 4522 | } |
| 5257 | try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other); | 4523 | try f.dg.renderValue(w, (try f.air.value(item, pt)).?, .other); |
| 5258 | } | 4524 | } |
| 5259 | try w.writeByte(':'); | 4525 | try w.writeByte(':'); |
| 5260 | } | 4526 | } |
| 5261 | try w.writeAll(" {"); | 4527 | try w.writeAll(" {"); |
| 5262 | f.object.indent(); | 4528 | f.indent(); |
| 5263 | try f.object.newline(); | 4529 | try f.newline(); |
| 5264 | if (is_dispatch_loop) { | 4530 | if (is_dispatch_loop) { |
| 5265 | try w.print("zig_switch_{d}_dispatch_{d}:;", .{ @intFromEnum(inst), case.idx }); | 4531 | try w.print("zig_switch_{d}_dispatch_{d}:;", .{ @intFromEnum(inst), case.idx }); |
| 5266 | try f.object.newline(); | 4532 | try f.newline(); |
| 5267 | } | 4533 | } |
| 5268 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); | 4534 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5269 | try f.object.outdent(); | 4535 | try f.outdent(); |
| 5270 | try w.writeByte('}'); | 4536 | try w.writeByte('}'); |
| 5271 | if (f.object.dg.expected_block) |_| | 4537 | if (f.dg.expected_block) |_| |
| 5272 | return f.fail("runtime code not allowed in naked function", .{}); | 4538 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5273 | | 4539 | |
| 5274 | // The case body must be noreturn so we don't need to insert a break. | 4540 | // The case body must be noreturn so we don't need to insert a break. |
| 5275 | } | 4541 | } |
| 5276 | | 4542 | |
| 5277 | const else_body = it.elseBody(); | 4543 | const else_body = it.elseBody(); |
| 5278 | try f.object.newline(); | 4544 | try f.newline(); |
| 5279 | | 4545 | |
| 5280 | try w.writeAll("default: "); | 4546 | try w.writeAll("default: "); |
| 5281 | if (any_range_cases) { | 4547 | if (any_range_cases) { |
| ... | @@ -5288,33 +4554,33 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5288,33 +4554,33 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5288 | try w.writeAll("if ("); | 4554 | try w.writeAll("if ("); |
| 5289 | for (case.items, 0..) |item, item_i| { | 4555 | for (case.items, 0..) |item, item_i| { |
| 5290 | if (item_i != 0) try w.writeAll(" || "); | 4556 | if (item_i != 0) try w.writeAll(" || "); |
| 5291 | try f.writeCValue(w, condition, .Other); | 4557 | try f.writeCValue(w, condition, .other); |
| 5292 | try w.writeAll(" == "); | 4558 | try w.writeAll(" == "); |
| 5293 | try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other); | 4559 | try f.dg.renderValue(w, (try f.air.value(item, pt)).?, .other); |
| 5294 | } | 4560 | } |
| 5295 | for (case.ranges, 0..) |range, range_i| { | 4561 | for (case.ranges, 0..) |range, range_i| { |
| 5296 | if (case.items.len != 0 or range_i != 0) try w.writeAll(" || "); | 4562 | if (case.items.len != 0 or range_i != 0) try w.writeAll(" || "); |
| 5297 | // "(x >= lower && x <= upper)" | 4563 | // "(x >= lower && x <= upper)" |
| 5298 | try w.writeByte('('); | 4564 | try w.writeByte('('); |
| 5299 | try f.writeCValue(w, condition, .Other); | 4565 | try f.writeCValue(w, condition, .other); |
| 5300 | try w.writeAll(" >= "); | 4566 | try w.writeAll(" >= "); |
| 5301 | try f.object.dg.renderValue(w, (try f.air.value(range[0], pt)).?, .Other); | 4567 | try f.dg.renderValue(w, (try f.air.value(range[0], pt)).?, .other); |
| 5302 | try w.writeAll(" && "); | 4568 | try w.writeAll(" && "); |
| 5303 | try f.writeCValue(w, condition, .Other); | 4569 | try f.writeCValue(w, condition, .other); |
| 5304 | try w.writeAll(" <= "); | 4570 | try w.writeAll(" <= "); |
| 5305 | try f.object.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .Other); | 4571 | try f.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .other); |
| 5306 | try w.writeByte(')'); | 4572 | try w.writeByte(')'); |
| 5307 | } | 4573 | } |
| 5308 | try w.writeAll(") {"); | 4574 | try w.writeAll(") {"); |
| 5309 | f.object.indent(); | 4575 | f.indent(); |
| 5310 | try f.object.newline(); | 4576 | try f.newline(); |
| 5311 | if (is_dispatch_loop) { | 4577 | if (is_dispatch_loop) { |
| 5312 | try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx }); | 4578 | try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx }); |
| 5313 | } | 4579 | } |
| 5314 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); | 4580 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5315 | try f.object.outdent(); | 4581 | try f.outdent(); |
| 5316 | try w.writeByte('}'); | 4582 | try w.writeByte('}'); |
| 5317 | if (f.object.dg.expected_block) |_| | 4583 | if (f.dg.expected_block) |_| |
| 5318 | return f.fail("runtime code not allowed in naked function", .{}); | 4584 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5319 | } | 4585 | } |
| 5320 | } | 4586 | } |
| ... | @@ -5328,16 +4594,16 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void | ... | @@ -5328,16 +4594,16 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5328 | try die(f, inst, death.toRef()); | 4594 | try die(f, inst, death.toRef()); |
| 5329 | } | 4595 | } |
| 5330 | try genBody(f, else_body); | 4596 | try genBody(f, else_body); |
| 5331 | if (f.object.dg.expected_block) |_| | 4597 | if (f.dg.expected_block) |_| |
| 5332 | return f.fail("runtime code not allowed in naked function", .{}); | 4598 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5333 | } else try airUnreach(&f.object); | 4599 | } else try airUnreach(f); |
| 5334 | try f.object.newline(); | 4600 | try f.newline(); |
| 5335 | try f.object.outdent(); | 4601 | try f.outdent(); |
| 5336 | try w.writeAll("}\n"); | 4602 | try w.writeAll("}\n"); |
| 5337 | } | 4603 | } |
| 5338 | | 4604 | |
| 5339 | fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool { | 4605 | fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool { |
| 5340 | const dg = f.object.dg; | 4606 | const dg = f.dg; |
| 5341 | const target = &dg.mod.resolved_target.result; | 4607 | const target = &dg.mod.resolved_target.result; |
| 5342 | return switch (constraint[0]) { | 4608 | return switch (constraint[0]) { |
| 5343 | '{' => true, | 4609 | '{' => true, |
| ... | @@ -5357,28 +4623,28 @@ fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool | ... | @@ -5357,28 +4623,28 @@ fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool |
| 5357 | } | 4623 | } |
| 5358 | | 4624 | |
| 5359 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | 4625 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5360 | const pt = f.object.dg.pt; | 4626 | const pt = f.dg.pt; |
| 5361 | const zcu = pt.zcu; | 4627 | const zcu = pt.zcu; |
| 5362 | const unwrapped_asm = f.air.unwrapAsm(inst); | 4628 | const unwrapped_asm = f.air.unwrapAsm(inst); |
| 5363 | const is_volatile = unwrapped_asm.is_volatile; | 4629 | const is_volatile = unwrapped_asm.is_volatile; |
| 5364 | const gpa = f.object.dg.gpa; | 4630 | const gpa = f.dg.gpa; |
| 5365 | const outputs = unwrapped_asm.outputs; | 4631 | const outputs = unwrapped_asm.outputs; |
| 5366 | const inputs = unwrapped_asm.inputs; | 4632 | const inputs = unwrapped_asm.inputs; |
| 5367 | | 4633 | |
| 5368 | const result = result: { | 4634 | const result = result: { |
| 5369 | const w = &f.object.code.writer; | 4635 | const w = &f.code.writer; |
| 5370 | const inst_ty = f.typeOfIndex(inst); | 4636 | const inst_ty = f.typeOfIndex(inst); |
| 5371 | const inst_local = if (inst_ty.hasRuntimeBits(zcu)) local: { | 4637 | const inst_local = if (inst_ty.hasRuntimeBits(zcu)) local: { |
| 5372 | const inst_local = try f.allocLocalValue(.{ | 4638 | const inst_local = try f.allocLocalValue(.{ |
| 5373 | .ctype = try f.ctypeFromType(inst_ty, .complete), | 4639 | .type = inst_ty, |
| 5374 | .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)), | 4640 | .alignment = .none, |
| 5375 | }); | 4641 | }); |
| 5376 | if (f.wantSafety()) { | 4642 | if (f.wantSafety()) { |
| 5377 | try f.writeCValue(w, inst_local, .Other); | 4643 | try f.writeCValue(w, inst_local, .other); |
| 5378 | try w.writeAll(" = "); | 4644 | try w.writeAll(" = "); |
| 5379 | try f.writeCValue(w, .{ .undef = inst_ty }, .Other); | 4645 | try f.writeCValue(w, .{ .undef = inst_ty }, .other); |
| 5380 | try w.writeByte(';'); | 4646 | try w.writeByte(';'); |
| 5381 | try f.object.newline(); | 4647 | try f.newline(); |
| 5382 | } | 4648 | } |
| 5383 | break :local inst_local; | 4649 | break :local inst_local; |
| 5384 | } else .none; | 4650 | } else .none; |
| ... | @@ -5399,20 +4665,20 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5399,20 +4665,20 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5399 | const output_ty = if (output.operand == .none) inst_ty else f.typeOf(output.operand).childType(zcu); | 4665 | const output_ty = if (output.operand == .none) inst_ty else f.typeOf(output.operand).childType(zcu); |
| 5400 | try w.writeAll("register "); | 4666 | try w.writeAll("register "); |
| 5401 | const output_local = try f.allocLocalValue(.{ | 4667 | const output_local = try f.allocLocalValue(.{ |
| 5402 | .ctype = try f.ctypeFromType(output_ty, .complete), | 4668 | .type = output_ty, |
| 5403 | .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)), | 4669 | .alignment = .none, |
| 5404 | }); | 4670 | }); |
| 5405 | try f.allocs.put(gpa, output_local.new_local, false); | 4671 | try f.allocs.put(gpa, output_local.new_local, false); |
| 5406 | try f.object.dg.renderTypeAndName(w, output_ty, output_local, .{}, .none, .complete); | 4672 | try f.dg.renderTypeAndName(w, output_ty, output_local, .{}, .none); |
| 5407 | try w.writeAll(" __asm(\""); | 4673 | try w.writeAll(" __asm(\""); |
| 5408 | try w.writeAll(constraint["={".len .. constraint.len - "}".len]); | 4674 | try w.writeAll(constraint["={".len .. constraint.len - "}".len]); |
| 5409 | try w.writeAll("\")"); | 4675 | try w.writeAll("\")"); |
| 5410 | if (f.wantSafety()) { | 4676 | if (f.wantSafety()) { |
| 5411 | try w.writeAll(" = "); | 4677 | try w.writeAll(" = "); |
| 5412 | try f.writeCValue(w, .{ .undef = output_ty }, .Other); | 4678 | try f.writeCValue(w, .{ .undef = output_ty }, .other); |
| 5413 | } | 4679 | } |
| 5414 | try w.writeByte(';'); | 4680 | try w.writeByte(';'); |
| 5415 | try f.object.newline(); | 4681 | try f.newline(); |
| 5416 | } | 4682 | } |
| 5417 | } | 4683 | } |
| 5418 | | 4684 | |
| ... | @@ -5432,29 +4698,29 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5432,29 +4698,29 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5432 | const input_ty = f.typeOf(input.operand); | 4698 | const input_ty = f.typeOf(input.operand); |
| 5433 | if (is_reg) try w.writeAll("register "); | 4699 | if (is_reg) try w.writeAll("register "); |
| 5434 | const input_local = try f.allocLocalValue(.{ | 4700 | const input_local = try f.allocLocalValue(.{ |
| 5435 | .ctype = try f.ctypeFromType(input_ty, .complete), | 4701 | .type = input_ty, |
| 5436 | .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)), | 4702 | .alignment = .none, |
| 5437 | }); | 4703 | }); |
| 5438 | try f.allocs.put(gpa, input_local.new_local, false); | 4704 | try f.allocs.put(gpa, input_local.new_local, false); |
| 5439 | // Do not render the declaration as `const` qualified if we're generating an | 4705 | // Do not render the declaration as `const` qualified if we're generating an |
| 5440 | // explicit `register` local, as GCC will ignore the constraint completely. | 4706 | // explicit `register` local, as GCC will ignore the constraint completely. |
| 5441 | try f.object.dg.renderTypeAndName(w, input_ty, input_local, if (is_reg) .{} else Const, .none, .complete); | 4707 | try f.dg.renderTypeAndName(w, input_ty, input_local, .{ .@"const" = is_reg }, .none); |
| 5442 | if (is_reg) { | 4708 | if (is_reg) { |
| 5443 | try w.writeAll(" __asm(\""); | 4709 | try w.writeAll(" __asm(\""); |
| 5444 | try w.writeAll(constraint["{".len .. constraint.len - "}".len]); | 4710 | try w.writeAll(constraint["{".len .. constraint.len - "}".len]); |
| 5445 | try w.writeAll("\")"); | 4711 | try w.writeAll("\")"); |
| 5446 | } | 4712 | } |
| 5447 | try w.writeAll(" = "); | 4713 | try w.writeAll(" = "); |
| 5448 | try f.writeCValue(w, input_val, .Other); | 4714 | try f.writeCValue(w, input_val, .other); |
| 5449 | try w.writeByte(';'); | 4715 | try w.writeByte(';'); |
| 5450 | try f.object.newline(); | 4716 | try f.newline(); |
| 5451 | } | 4717 | } |
| 5452 | } | 4718 | } |
| 5453 | | 4719 | |
| 5454 | { | 4720 | { |
| 5455 | const asm_source = unwrapped_asm.source; | 4721 | const asm_source = unwrapped_asm.source; |
| 5456 | | 4722 | |
| 5457 | var stack = std.heap.stackFallback(256, f.object.dg.gpa); | 4723 | var stack = std.heap.stackFallback(256, f.dg.gpa); |
| 5458 | const allocator = stack.get(); | 4724 | const allocator = stack.get(); |
| 5459 | const fixed_asm_source = try allocator.alloc(u8, asm_source.len); | 4725 | const fixed_asm_source = try allocator.alloc(u8, asm_source.len); |
| 5460 | defer allocator.free(fixed_asm_source); | 4726 | defer allocator.free(fixed_asm_source); |
| ... | @@ -5520,10 +4786,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5520,10 +4786,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5520 | const is_reg = constraint[1] == '{'; | 4786 | const is_reg = constraint[1] == '{'; |
| 5521 | try w.print("{f}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)}); | 4787 | try w.print("{f}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)}); |
| 5522 | if (is_reg) { | 4788 | if (is_reg) { |
| 5523 | try f.writeCValue(w, .{ .local = locals_index }, .Other); | 4789 | try f.writeCValue(w, .{ .local = locals_index }, .other); |
| 5524 | locals_index += 1; | 4790 | locals_index += 1; |
| 5525 | } else if (output.operand == .none) { | 4791 | } else if (output.operand == .none) { |
| 5526 | try f.writeCValue(w, inst_local, .FunctionArgument); | 4792 | try f.writeCValue(w, inst_local, .other); |
| 5527 | } else { | 4793 | } else { |
| 5528 | try f.writeCValueDeref(w, try f.resolveInst(output.operand)); | 4794 | try f.writeCValueDeref(w, try f.resolveInst(output.operand)); |
| 5529 | } | 4795 | } |
| ... | @@ -5547,7 +4813,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5547,7 +4813,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5547 | const input_local_idx = locals_index; | 4813 | const input_local_idx = locals_index; |
| 5548 | locals_index += 1; | 4814 | locals_index += 1; |
| 5549 | break :local .{ .local = input_local_idx }; | 4815 | break :local .{ .local = input_local_idx }; |
| 5550 | } else input_val, .Other); | 4816 | } else input_val, .other); |
| 5551 | try w.writeByte(')'); | 4817 | try w.writeByte(')'); |
| 5552 | } | 4818 | } |
| 5553 | try w.writeByte(':'); | 4819 | try w.writeByte(':'); |
| ... | @@ -5567,7 +4833,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5567,7 +4833,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5567 | const field_name = clobbers_ty.structFieldName(field_index, zcu).toSlice(ip).?; | 4833 | const field_name = clobbers_ty.structFieldName(field_index, zcu).toSlice(ip).?; |
| 5568 | assert(field_name.len != 0); | 4834 | assert(field_name.len != 0); |
| 5569 | | 4835 | |
| 5570 | const target = &f.object.dg.mod.resolved_target.result; | 4836 | const target = &f.dg.mod.resolved_target.result; |
| 5571 | var c_name_buf: [16]u8 = undefined; | 4837 | var c_name_buf: [16]u8 = undefined; |
| 5572 | const name = | 4838 | const name = |
| 5573 | if ((target.cpu.arch.isMIPS() or target.cpu.arch == .alpha) and field_name[0] == 'r') name: { | 4839 | if ((target.cpu.arch.isMIPS() or target.cpu.arch == .alpha) and field_name[0] == 'r') name: { |
| ... | @@ -5594,7 +4860,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5594,7 +4860,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5594 | } | 4860 | } |
| 5595 | w.undo(1); // erase the last comma | 4861 | w.undo(1); // erase the last comma |
| 5596 | try w.writeAll(");"); | 4862 | try w.writeAll(");"); |
| 5597 | try f.object.newline(); | 4863 | try f.newline(); |
| 5598 | | 4864 | |
| 5599 | locals_index = locals_begin; | 4865 | locals_index = locals_begin; |
| 5600 | it = unwrapped_asm.iterateOutputs(); | 4866 | it = unwrapped_asm.iterateOutputs(); |
| ... | @@ -5608,10 +4874,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5608,10 +4874,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5608 | else | 4874 | else |
| 5609 | try f.resolveInst(output.operand)); | 4875 | try f.resolveInst(output.operand)); |
| 5610 | try w.writeAll(" = "); | 4876 | try w.writeAll(" = "); |
| 5611 | try f.writeCValue(w, .{ .local = locals_index }, .Other); | 4877 | try f.writeCValue(w, .{ .local = locals_index }, .other); |
| 5612 | locals_index += 1; | 4878 | locals_index += 1; |
| 5613 | try w.writeByte(';'); | 4879 | try w.writeByte(';'); |
| 5614 | try f.object.newline(); | 4880 | try f.newline(); |
| 5615 | } | 4881 | } |
| 5616 | } | 4882 | } |
| 5617 | | 4883 | |
| ... | @@ -5633,147 +4899,145 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5633,147 +4899,145 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5633 | fn airIsNull( | 4899 | fn airIsNull( |
| 5634 | f: *Function, | 4900 | f: *Function, |
| 5635 | inst: Air.Inst.Index, | 4901 | inst: Air.Inst.Index, |
| 5636 | operator: std.math.CompareOperator, | 4902 | operator: enum { eq, neq }, |
| 5637 | is_ptr: bool, | 4903 | is_ptr: bool, |
| 5638 | ) !CValue { | 4904 | ) !CValue { |
| 5639 | const pt = f.object.dg.pt; | 4905 | const pt = f.dg.pt; |
| 5640 | const zcu = pt.zcu; | 4906 | const zcu = pt.zcu; |
| 5641 | const ctype_pool = &f.object.dg.ctype_pool; | | |
| 5642 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 4907 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5643 | | 4908 | |
| 5644 | const w = &f.object.code.writer; | 4909 | const w = &f.code.writer; |
| 5645 | const operand = try f.resolveInst(un_op); | 4910 | const operand = try f.resolveInst(un_op); |
| 5646 | try reap(f, inst, &.{un_op}); | 4911 | try reap(f, inst, &.{un_op}); |
| 5647 | | 4912 | |
| 5648 | const local = try f.allocLocal(inst, .bool); | 4913 | const local = try f.allocLocal(inst, .bool); |
| 5649 | const a = try Assignment.start(f, w, .bool); | 4914 | try f.writeCValue(w, local, .other); |
| 5650 | try f.writeCValue(w, local, .Other); | 4915 | try w.writeAll(" = "); |
| 5651 | try a.assign(f, w); | | |
| 5652 | | 4916 | |
| 5653 | const operand_ty = f.typeOf(un_op); | 4917 | const operand_ty = f.typeOf(un_op); |
| 5654 | const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; | 4918 | const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5655 | const opt_ctype = try f.ctypeFromType(optional_ty, .complete); | 4919 | |
| 5656 | const rhs = switch (opt_ctype.info(ctype_pool)) { | 4920 | const pre: []const u8, const maybe_field: ?[]const u8, const post: []const u8 = switch (operator) { |
| 5657 | .basic, .pointer => rhs: { | 4921 | // zig fmt: off |
| 5658 | if (is_ptr) | 4922 | .eq => switch (CType.classifyOptional(optional_ty, zcu)) { |
| 5659 | try f.writeCValueDeref(w, operand) | 4923 | .npv_payload => unreachable, // opv optional |
| 5660 | else | 4924 | .error_set => .{ "", null, " == 0" }, |
| 5661 | try f.writeCValue(w, operand, .Other); | 4925 | .ptr_like => .{ "", null, " == NULL" }, |
| 5662 | break :rhs if (opt_ctype.isBool()) | 4926 | .slice_like => .{ "", "ptr", " == NULL" }, |
| 5663 | "true" | 4927 | .opv_payload => .{ "", "is_null", "" }, |
| 5664 | else if (opt_ctype.isInteger()) | 4928 | .@"struct" => .{ "", "is_null", "" }, |
| 5665 | "0" | | |
| 5666 | else | | |
| 5667 | "NULL"; | | |
| 5668 | }, | 4929 | }, |
| 5669 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 4930 | .neq => switch (CType.classifyOptional(optional_ty, zcu)) { |
| 5670 | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { | 4931 | .npv_payload => unreachable, // opv optional |
| 5671 | .is_null, .payload => rhs: { | 4932 | .error_set => .{ "", null, " != 0" }, |
| 5672 | if (is_ptr) | 4933 | .ptr_like => .{ "", null, " != NULL" }, |
| 5673 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }) | 4934 | .slice_like => .{ "", "ptr", " != NULL" }, |
| 5674 | else | 4935 | .opv_payload => .{ "!", "is_null", "" }, |
| 5675 | try f.writeCValueMember(w, operand, .{ .identifier = "is_null" }); | 4936 | .@"struct" => .{ "!", "is_null", "" }, |
| 5676 | break :rhs "true"; | | |
| 5677 | }, | | |
| 5678 | .ptr, .len => rhs: { | | |
| 5679 | if (is_ptr) | | |
| 5680 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "ptr" }) | | |
| 5681 | else | | |
| 5682 | try f.writeCValueMember(w, operand, .{ .identifier = "ptr" }); | | |
| 5683 | break :rhs "NULL"; | | |
| 5684 | }, | | |
| 5685 | else => unreachable, | | |
| 5686 | }, | 4937 | }, |
| | 4938 | // zig fmt: on |
| 5687 | }; | 4939 | }; |
| 5688 | try w.writeAll(compareOperatorC(operator)); | 4940 | |
| 5689 | try w.writeAll(rhs); | 4941 | try w.writeAll(pre); |
| 5690 | try a.end(f, w); | 4942 | if (maybe_field) |field| { |
| | 4943 | if (is_ptr) { |
| | 4944 | try f.writeCValueDerefMember(w, operand, .{ .identifier = field }); |
| | 4945 | } else { |
| | 4946 | try f.writeCValueMember(w, operand, .{ .identifier = field }); |
| | 4947 | } |
| | 4948 | } else { |
| | 4949 | if (is_ptr) { |
| | 4950 | try f.writeCValueDeref(w, operand); |
| | 4951 | } else { |
| | 4952 | try f.writeCValue(w, operand, .other); |
| | 4953 | } |
| | 4954 | } |
| | 4955 | try w.writeAll(post); |
| | 4956 | |
| | 4957 | try w.writeByte(';'); |
| | 4958 | try f.newline(); |
| 5691 | return local; | 4959 | return local; |
| 5692 | } | 4960 | } |
| 5693 | | 4961 | |
| 5694 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { | 4962 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 5695 | const pt = f.object.dg.pt; | 4963 | const pt = f.dg.pt; |
| 5696 | const zcu = pt.zcu; | 4964 | const zcu = pt.zcu; |
| 5697 | const ctype_pool = &f.object.dg.ctype_pool; | | |
| 5698 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 4965 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5699 | | 4966 | |
| 5700 | const inst_ty = f.typeOfIndex(inst); | 4967 | const inst_ty = f.typeOfIndex(inst); |
| 5701 | const operand_ty = f.typeOf(ty_op.operand); | 4968 | const operand_ty = f.typeOf(ty_op.operand); |
| 5702 | const opt_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; | 4969 | const opt_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5703 | const opt_ctype = try f.ctypeFromType(opt_ty, .complete); | | |
| 5704 | if (opt_ctype.isBool()) return if (is_ptr) .{ .undef = inst_ty } else .none; | | |
| 5705 | | 4970 | |
| 5706 | const operand = try f.resolveInst(ty_op.operand); | 4971 | const operand = try f.resolveInst(ty_op.operand); |
| 5707 | switch (opt_ctype.info(ctype_pool)) { | 4972 | |
| 5708 | .basic, .pointer => return f.moveCValue(inst, inst_ty, operand), | 4973 | switch (CType.classifyOptional(opt_ty, zcu)) { |
| 5709 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 4974 | .npv_payload => unreachable, // opv optional |
| 5710 | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { | 4975 | |
| 5711 | .is_null, .payload => { | 4976 | .opv_payload => return if (is_ptr) .{ .undef = inst_ty } else .none, |
| 5712 | const w = &f.object.code.writer; | 4977 | |
| 5713 | const local = try f.allocLocal(inst, inst_ty); | 4978 | .error_set, |
| 5714 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 4979 | .ptr_like, |
| 5715 | try f.writeCValue(w, local, .Other); | 4980 | .slice_like, |
| 5716 | try a.assign(f, w); | 4981 | => return f.moveCValue(inst, inst_ty, operand), |
| 5717 | if (is_ptr) { | 4982 | |
| 5718 | try w.writeByte('&'); | 4983 | .@"struct" => { |
| 5719 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); | 4984 | const w = &f.code.writer; |
| 5720 | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); | 4985 | const local = try f.allocLocal(inst, inst_ty); |
| 5721 | try a.end(f, w); | 4986 | try f.writeCValue(w, local, .other); |
| 5722 | return local; | 4987 | try w.writeAll(" = "); |
| 5723 | }, | 4988 | if (is_ptr) { |
| 5724 | .ptr, .len => return f.moveCValue(inst, inst_ty, operand), | 4989 | try w.writeByte('&'); |
| 5725 | else => unreachable, | 4990 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| | 4991 | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); |
| | 4992 | try w.writeByte(';'); |
| | 4993 | try f.newline(); |
| | 4994 | return local; |
| 5726 | }, | 4995 | }, |
| 5727 | } | 4996 | } |
| 5728 | } | 4997 | } |
| 5729 | | 4998 | |
| 5730 | fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | 4999 | fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5731 | const pt = f.object.dg.pt; | 5000 | const pt = f.dg.pt; |
| 5732 | const zcu = pt.zcu; | 5001 | const zcu = pt.zcu; |
| 5733 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5002 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5734 | const w = &f.object.code.writer; | 5003 | const w = &f.code.writer; |
| 5735 | const operand = try f.resolveInst(ty_op.operand); | 5004 | const operand = try f.resolveInst(ty_op.operand); |
| 5736 | try reap(f, inst, &.{ty_op.operand}); | 5005 | try reap(f, inst, &.{ty_op.operand}); |
| 5737 | const operand_ty = f.typeOf(ty_op.operand); | 5006 | const operand_ty = f.typeOf(ty_op.operand); |
| | 5007 | const opt_ty = operand_ty.childType(zcu); |
| 5738 | | 5008 | |
| 5739 | const inst_ty = f.typeOfIndex(inst); | 5009 | const inst_ty = f.typeOfIndex(inst); |
| 5740 | const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete); | 5010 | |
| 5741 | switch (opt_ctype.info(&f.object.dg.ctype_pool)) { | 5011 | switch (CType.classifyOptional(opt_ty, zcu)) { |
| 5742 | .basic => { | 5012 | .npv_payload => unreachable, // opv optional |
| 5743 | const a = try Assignment.start(f, w, opt_ctype); | 5013 | |
| 5744 | try f.writeCValueDeref(w, operand); | 5014 | .opv_payload => { |
| 5745 | try a.assign(f, w); | 5015 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); |
| 5746 | try f.object.dg.renderValue(w, Value.false, .Other); | 5016 | try w.writeAll(" = "); |
| 5747 | try a.end(f, w); | 5017 | try f.dg.renderValue(w, .false, .other); |
| 5748 | return .none; | 5018 | try w.writeByte(';'); |
| 5749 | }, | 5019 | try f.newline(); |
| 5750 | .pointer => { | 5020 | return .{ .undef = inst_ty }; |
| 5751 | if (f.liveness.isUnused(inst)) return .none; | | |
| 5752 | const local = try f.allocLocal(inst, inst_ty); | | |
| 5753 | const a = try Assignment.start(f, w, opt_ctype); | | |
| 5754 | try f.writeCValue(w, local, .Other); | | |
| 5755 | try a.assign(f, w); | | |
| 5756 | try f.writeCValue(w, operand, .Other); | | |
| 5757 | try a.end(f, w); | | |
| 5758 | return local; | | |
| 5759 | }, | 5021 | }, |
| 5760 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 5022 | |
| 5761 | .aggregate => { | 5023 | .error_set, |
| 5762 | { | 5024 | .ptr_like, |
| 5763 | const a = try Assignment.start(f, w, opt_ctype); | 5025 | .slice_like, |
| 5764 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); | 5026 | => return f.moveCValue(inst, inst_ty, operand), |
| 5765 | try a.assign(f, w); | 5027 | |
| 5766 | try f.object.dg.renderValue(w, Value.false, .Other); | 5028 | .@"struct" => { |
| 5767 | try a.end(f, w); | 5029 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); |
| 5768 | } | 5030 | try w.writeAll(" = "); |
| | 5031 | try f.dg.renderValue(w, .false, .other); |
| | 5032 | try w.writeByte(';'); |
| | 5033 | try f.newline(); |
| 5769 | if (f.liveness.isUnused(inst)) return .none; | 5034 | if (f.liveness.isUnused(inst)) return .none; |
| 5770 | const local = try f.allocLocal(inst, inst_ty); | 5035 | const local = try f.allocLocal(inst, inst_ty); |
| 5771 | const a = try Assignment.start(f, w, opt_ctype); | 5036 | try f.writeCValue(w, local, .other); |
| 5772 | try f.writeCValue(w, local, .Other); | 5037 | try w.writeAll(" = &"); |
| 5773 | try a.assign(f, w); | | |
| 5774 | try w.writeByte('&'); | | |
| 5775 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); | 5038 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 5776 | try a.end(f, w); | 5039 | try w.writeByte(';'); |
| | 5040 | try f.newline(); |
| 5777 | return local; | 5041 | return local; |
| 5778 | }, | 5042 | }, |
| 5779 | } | 5043 | } |
| ... | @@ -5817,18 +5081,20 @@ fn fieldLocation( | ... | @@ -5817,18 +5081,20 @@ fn fieldLocation( |
| 5817 | .union_type => { | 5081 | .union_type => { |
| 5818 | const loaded_union = ip.loadUnionType(container_ty.toIntern()); | 5082 | const loaded_union = ip.loadUnionType(container_ty.toIntern()); |
| 5819 | switch (loaded_union.layout) { | 5083 | switch (loaded_union.layout) { |
| 5820 | .auto, .@"extern" => { | 5084 | .auto => { |
| 5821 | const field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]); | 5085 | const field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| 5822 | if (!field_ty.hasRuntimeBits(zcu)) | 5086 | if (!field_ty.hasRuntimeBits(zcu)) { |
| 5823 | return if (loaded_union.has_runtime_tag and !container_ty.unionHasAllZeroBitFieldTypes(zcu)) | 5087 | if (container_ty.unionHasAllZeroBitFieldTypes(zcu)) return .begin; |
| 5824 | .{ .field = .{ .identifier = "payload" } } | 5088 | return .{ .field = .{ .identifier = "payload" } }; |
| 5825 | else | 5089 | } |
| 5826 | .begin; | | |
| 5827 | const field_name = ip.loadEnumType(loaded_union.enum_tag_type).field_names.get(ip)[field_index]; | 5090 | const field_name = ip.loadEnumType(loaded_union.enum_tag_type).field_names.get(ip)[field_index]; |
| 5828 | return .{ .field = if (loaded_union.has_runtime_tag) | 5091 | return .{ .field = .{ .payload_identifier = field_name.toSlice(ip) } }; |
| 5829 | .{ .payload_identifier = field_name.toSlice(ip) } | 5092 | }, |
| 5830 | else | 5093 | .@"extern" => { |
| 5831 | .{ .identifier = field_name.toSlice(ip) } }; | 5094 | const field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| | 5095 | if (!field_ty.hasRuntimeBits(zcu)) return .begin; |
| | 5096 | const field_name = ip.loadEnumType(loaded_union.enum_tag_type).field_names.get(ip)[field_index]; |
| | 5097 | return .{ .field = .{ .identifier = field_name.toSlice(ip) } }; |
| 5832 | }, | 5098 | }, |
| 5833 | .@"packed" => return .begin, | 5099 | .@"packed" => return .begin, |
| 5834 | } | 5100 | } |
| ... | @@ -5865,7 +5131,7 @@ fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue | ... | @@ -5865,7 +5131,7 @@ fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue |
| 5865 | } | 5131 | } |
| 5866 | | 5132 | |
| 5867 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 5133 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5868 | const pt = f.object.dg.pt; | 5134 | const pt = f.dg.pt; |
| 5869 | const zcu = pt.zcu; | 5135 | const zcu = pt.zcu; |
| 5870 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 5136 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5871 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; | 5137 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| ... | @@ -5877,26 +5143,26 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5877,26 +5143,26 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5877 | const field_ptr_val = try f.resolveInst(extra.field_ptr); | 5143 | const field_ptr_val = try f.resolveInst(extra.field_ptr); |
| 5878 | try reap(f, inst, &.{extra.field_ptr}); | 5144 | try reap(f, inst, &.{extra.field_ptr}); |
| 5879 | | 5145 | |
| 5880 | const w = &f.object.code.writer; | 5146 | const w = &f.code.writer; |
| 5881 | const local = try f.allocLocal(inst, container_ptr_ty); | 5147 | const local = try f.allocLocal(inst, container_ptr_ty); |
| 5882 | try f.writeCValue(w, local, .Other); | 5148 | try f.writeCValue(w, local, .other); |
| 5883 | try w.writeAll(" = ("); | 5149 | try w.writeAll(" = ("); |
| 5884 | try f.renderType(w, container_ptr_ty); | 5150 | try f.renderType(w, container_ptr_ty); |
| 5885 | try w.writeByte(')'); | 5151 | try w.writeByte(')'); |
| 5886 | | 5152 | |
| 5887 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, zcu)) { | 5153 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, zcu)) { |
| 5888 | .begin => try f.writeCValue(w, field_ptr_val, .Other), | 5154 | .begin => try f.writeCValue(w, field_ptr_val, .other), |
| 5889 | .field => |field| { | 5155 | .field => |field| { |
| 5890 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8); | 5156 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8); |
| 5891 | | 5157 | |
| 5892 | try w.writeAll("(("); | 5158 | try w.writeAll("(("); |
| 5893 | try f.renderType(w, u8_ptr_ty); | 5159 | try f.renderType(w, u8_ptr_ty); |
| 5894 | try w.writeByte(')'); | 5160 | try w.writeByte(')'); |
| 5895 | try f.writeCValue(w, field_ptr_val, .Other); | 5161 | try f.writeCValue(w, field_ptr_val, .other); |
| 5896 | try w.writeAll(" - offsetof("); | 5162 | try w.writeAll(" - offsetof("); |
| 5897 | try f.renderType(w, container_ty); | 5163 | try f.renderType(w, container_ty); |
| 5898 | try w.writeAll(", "); | 5164 | try w.writeAll(", "); |
| 5899 | try f.writeCValue(w, field, .Other); | 5165 | try f.writeCValue(w, field, .other); |
| 5900 | try w.writeAll("))"); | 5166 | try w.writeAll("))"); |
| 5901 | }, | 5167 | }, |
| 5902 | .byte_offset => |byte_offset| { | 5168 | .byte_offset => |byte_offset| { |
| ... | @@ -5905,7 +5171,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5905,7 +5171,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5905 | try w.writeAll("(("); | 5171 | try w.writeAll("(("); |
| 5906 | try f.renderType(w, u8_ptr_ty); | 5172 | try f.renderType(w, u8_ptr_ty); |
| 5907 | try w.writeByte(')'); | 5173 | try w.writeByte(')'); |
| 5908 | try f.writeCValue(w, field_ptr_val, .Other); | 5174 | try f.writeCValue(w, field_ptr_val, .other); |
| 5909 | try w.print(" - {f})", .{ | 5175 | try w.print(" - {f})", .{ |
| 5910 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), | 5176 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), |
| 5911 | }); | 5177 | }); |
| ... | @@ -5913,7 +5179,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5913,7 +5179,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5913 | } | 5179 | } |
| 5914 | | 5180 | |
| 5915 | try w.writeByte(';'); | 5181 | try w.writeByte(';'); |
| 5916 | try f.object.newline(); | 5182 | try f.newline(); |
| 5917 | return local; | 5183 | return local; |
| 5918 | } | 5184 | } |
| 5919 | | 5185 | |
| ... | @@ -5924,23 +5190,19 @@ fn fieldPtr( | ... | @@ -5924,23 +5190,19 @@ fn fieldPtr( |
| 5924 | container_ptr_val: CValue, | 5190 | container_ptr_val: CValue, |
| 5925 | field_index: u32, | 5191 | field_index: u32, |
| 5926 | ) !CValue { | 5192 | ) !CValue { |
| 5927 | const pt = f.object.dg.pt; | 5193 | const pt = f.dg.pt; |
| 5928 | const zcu = pt.zcu; | 5194 | const zcu = pt.zcu; |
| 5929 | const container_ty = container_ptr_ty.childType(zcu); | | |
| 5930 | const field_ptr_ty = f.typeOfIndex(inst); | 5195 | const field_ptr_ty = f.typeOfIndex(inst); |
| 5931 | | 5196 | |
| 5932 | // Ensure complete type definition is visible before accessing fields. | 5197 | const w = &f.code.writer; |
| 5933 | _ = try f.ctypeFromType(container_ty, .complete); | | |
| 5934 | | | |
| 5935 | const w = &f.object.code.writer; | | |
| 5936 | const local = try f.allocLocal(inst, field_ptr_ty); | 5198 | const local = try f.allocLocal(inst, field_ptr_ty); |
| 5937 | try f.writeCValue(w, local, .Other); | 5199 | try f.writeCValue(w, local, .other); |
| 5938 | try w.writeAll(" = ("); | 5200 | try w.writeAll(" = ("); |
| 5939 | try f.renderType(w, field_ptr_ty); | 5201 | try f.renderType(w, field_ptr_ty); |
| 5940 | try w.writeByte(')'); | 5202 | try w.writeByte(')'); |
| 5941 | | 5203 | |
| 5942 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, zcu)) { | 5204 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, zcu)) { |
| 5943 | .begin => try f.writeCValue(w, container_ptr_val, .Other), | 5205 | .begin => try f.writeCValue(w, container_ptr_val, .other), |
| 5944 | .field => |field| { | 5206 | .field => |field| { |
| 5945 | try w.writeByte('&'); | 5207 | try w.writeByte('&'); |
| 5946 | try f.writeCValueDerefMember(w, container_ptr_val, field); | 5208 | try f.writeCValueDerefMember(w, container_ptr_val, field); |
| ... | @@ -5951,7 +5213,7 @@ fn fieldPtr( | ... | @@ -5951,7 +5213,7 @@ fn fieldPtr( |
| 5951 | try w.writeAll("(("); | 5213 | try w.writeAll("(("); |
| 5952 | try f.renderType(w, u8_ptr_ty); | 5214 | try f.renderType(w, u8_ptr_ty); |
| 5953 | try w.writeByte(')'); | 5215 | try w.writeByte(')'); |
| 5954 | try f.writeCValue(w, container_ptr_val, .Other); | 5216 | try f.writeCValue(w, container_ptr_val, .other); |
| 5955 | try w.print(" + {f})", .{ | 5217 | try w.print(" + {f})", .{ |
| 5956 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), | 5218 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), |
| 5957 | }); | 5219 | }); |
| ... | @@ -5959,12 +5221,12 @@ fn fieldPtr( | ... | @@ -5959,12 +5221,12 @@ fn fieldPtr( |
| 5959 | } | 5221 | } |
| 5960 | | 5222 | |
| 5961 | try w.writeByte(';'); | 5223 | try w.writeByte(';'); |
| 5962 | try f.object.newline(); | 5224 | try f.newline(); |
| 5963 | return local; | 5225 | return local; |
| 5964 | } | 5226 | } |
| 5965 | | 5227 | |
| 5966 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | 5228 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5967 | const pt = f.object.dg.pt; | 5229 | const pt = f.dg.pt; |
| 5968 | const zcu = pt.zcu; | 5230 | const zcu = pt.zcu; |
| 5969 | const ip = &zcu.intern_pool; | 5231 | const ip = &zcu.intern_pool; |
| 5970 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 5232 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | @@ -5976,10 +5238,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5976,10 +5238,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5976 | const struct_byval = try f.resolveInst(extra.struct_operand); | 5238 | const struct_byval = try f.resolveInst(extra.struct_operand); |
| 5977 | try reap(f, inst, &.{extra.struct_operand}); | 5239 | try reap(f, inst, &.{extra.struct_operand}); |
| 5978 | const struct_ty = f.typeOf(extra.struct_operand); | 5240 | const struct_ty = f.typeOf(extra.struct_operand); |
| 5979 | const w = &f.object.code.writer; | 5241 | const w = &f.code.writer; |
| 5980 | | | |
| 5981 | // Ensure complete type definition is visible before accessing fields. | | |
| 5982 | _ = try f.ctypeFromType(struct_ty, .complete); | | |
| 5983 | | 5242 | |
| 5984 | assert(struct_ty.containerLayout(zcu) != .@"packed"); // `Air.Legalize.Feature.expand_packed_struct_field_val` handles this case | 5243 | assert(struct_ty.containerLayout(zcu) != .@"packed"); // `Air.Legalize.Feature.expand_packed_struct_field_val` handles this case |
| 5985 | const field_name: CValue = switch (ip.indexToKey(struct_ty.toIntern())) { | 5244 | const field_name: CValue = switch (ip.indexToKey(struct_ty.toIntern())) { |
| ... | @@ -5988,29 +5247,25 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5988,29 +5247,25 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5988 | const union_type = ip.loadUnionType(struct_ty.toIntern()); | 5247 | const union_type = ip.loadUnionType(struct_ty.toIntern()); |
| 5989 | const enum_tag_ty: Type = .fromInterned(union_type.enum_tag_type); | 5248 | const enum_tag_ty: Type = .fromInterned(union_type.enum_tag_type); |
| 5990 | const field_name_str = enum_tag_ty.enumFieldName(extra.field_index, zcu).toSlice(ip); | 5249 | const field_name_str = enum_tag_ty.enumFieldName(extra.field_index, zcu).toSlice(ip); |
| 5991 | if (union_type.has_runtime_tag) { | 5250 | break :name .{ .payload_identifier = field_name_str }; |
| 5992 | break :name .{ .payload_identifier = field_name_str }; | | |
| 5993 | } else { | | |
| 5994 | break :name .{ .identifier = field_name_str }; | | |
| 5995 | } | | |
| 5996 | }, | 5251 | }, |
| 5997 | .tuple_type => .{ .field = extra.field_index }, | 5252 | .tuple_type => .{ .field = extra.field_index }, |
| 5998 | else => unreachable, | 5253 | else => unreachable, |
| 5999 | }; | 5254 | }; |
| 6000 | | 5255 | |
| 6001 | const local = try f.allocLocal(inst, inst_ty); | 5256 | const local = try f.allocLocal(inst, inst_ty); |
| 6002 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 5257 | try f.writeCValue(w, local, .other); |
| 6003 | try f.writeCValue(w, local, .Other); | 5258 | try w.writeAll(" = "); |
| 6004 | try a.assign(f, w); | | |
| 6005 | try f.writeCValueMember(w, struct_byval, field_name); | 5259 | try f.writeCValueMember(w, struct_byval, field_name); |
| 6006 | try a.end(f, w); | 5260 | try w.writeByte(';'); |
| | 5261 | try f.newline(); |
| 6007 | return local; | 5262 | return local; |
| 6008 | } | 5263 | } |
| 6009 | | 5264 | |
| 6010 | /// *(E!T) -> E | 5265 | /// *(E!T) -> E |
| 6011 | /// Note that the result is never a pointer. | 5266 | /// Note that the result is never a pointer. |
| 6012 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | 5267 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6013 | const pt = f.object.dg.pt; | 5268 | const pt = f.dg.pt; |
| 6014 | const zcu = pt.zcu; | 5269 | const zcu = pt.zcu; |
| 6015 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5270 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6016 | | 5271 | |
| ... | @@ -6020,37 +5275,23 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6020,37 +5275,23 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6020 | try reap(f, inst, &.{ty_op.operand}); | 5275 | try reap(f, inst, &.{ty_op.operand}); |
| 6021 | | 5276 | |
| 6022 | const operand_is_ptr = operand_ty.zigTypeTag(zcu) == .pointer; | 5277 | const operand_is_ptr = operand_ty.zigTypeTag(zcu) == .pointer; |
| 6023 | const error_union_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty; | | |
| 6024 | const error_ty = error_union_ty.errorUnionSet(zcu); | | |
| 6025 | const payload_ty = error_union_ty.errorUnionPayload(zcu); | | |
| 6026 | const local = try f.allocLocal(inst, inst_ty); | 5278 | const local = try f.allocLocal(inst, inst_ty); |
| 6027 | | 5279 | |
| 6028 | if (!payload_ty.hasRuntimeBits(zcu) and operand == .local and operand.local == local.new_local) { | 5280 | const w = &f.code.writer; |
| 6029 | // The store will be 'x = x'; elide it. | 5281 | try f.writeCValue(w, local, .other); |
| 6030 | return local; | | |
| 6031 | } | | |
| 6032 | | | |
| 6033 | const w = &f.object.code.writer; | | |
| 6034 | try f.writeCValue(w, local, .Other); | | |
| 6035 | try w.writeAll(" = "); | 5282 | try w.writeAll(" = "); |
| 6036 | | 5283 | |
| 6037 | if (!payload_ty.hasRuntimeBits(zcu)) | 5284 | if (operand_is_ptr) |
| 6038 | try f.writeCValue(w, operand, .Other) | | |
| 6039 | else if (error_ty.errorSetIsEmpty(zcu)) | | |
| 6040 | try w.print("{f}", .{ | | |
| 6041 | try f.fmtIntLiteralDec(try pt.intValue(try pt.errorIntType(), 0)), | | |
| 6042 | }) | | |
| 6043 | else if (operand_is_ptr) | | |
| 6044 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) | 5285 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) |
| 6045 | else | 5286 | else |
| 6046 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); | 5287 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); |
| 6047 | try w.writeByte(';'); | 5288 | try w.writeByte(';'); |
| 6048 | try f.object.newline(); | 5289 | try f.newline(); |
| 6049 | return local; | 5290 | return local; |
| 6050 | } | 5291 | } |
| 6051 | | 5292 | |
| 6052 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { | 5293 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 6053 | const pt = f.object.dg.pt; | 5294 | const pt = f.dg.pt; |
| 6054 | const zcu = pt.zcu; | 5295 | const zcu = pt.zcu; |
| 6055 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5296 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6056 | | 5297 | |
| ... | @@ -6060,154 +5301,124 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu | ... | @@ -6060,154 +5301,124 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 6060 | const operand_ty = f.typeOf(ty_op.operand); | 5301 | const operand_ty = f.typeOf(ty_op.operand); |
| 6061 | const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; | 5302 | const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 6062 | | 5303 | |
| 6063 | const w = &f.object.code.writer; | 5304 | const w = &f.code.writer; |
| 6064 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { | 5305 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 6065 | if (!is_ptr) return .none; | 5306 | assert(is_ptr); // opv bug in sema |
| 6066 | | | |
| 6067 | const local = try f.allocLocal(inst, inst_ty); | 5307 | const local = try f.allocLocal(inst, inst_ty); |
| 6068 | try f.writeCValue(w, local, .Other); | 5308 | try f.writeCValue(w, local, .other); |
| 6069 | try w.writeAll(" = ("); | 5309 | try w.writeAll(" = ("); |
| 6070 | try f.renderType(w, inst_ty); | 5310 | try f.renderType(w, inst_ty); |
| 6071 | try w.writeByte(')'); | 5311 | try w.writeByte(')'); |
| 6072 | try f.writeCValue(w, operand, .Other); | 5312 | try f.writeCValue(w, operand, .other); |
| 6073 | try w.writeByte(';'); | 5313 | try w.writeByte(';'); |
| 6074 | try f.object.newline(); | 5314 | try f.newline(); |
| 6075 | return local; | 5315 | return local; |
| 6076 | } | 5316 | } |
| 6077 | | 5317 | |
| 6078 | const local = try f.allocLocal(inst, inst_ty); | 5318 | const local = try f.allocLocal(inst, inst_ty); |
| 6079 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 5319 | try f.writeCValue(w, local, .other); |
| 6080 | try f.writeCValue(w, local, .Other); | 5320 | try w.writeAll(" = "); |
| 6081 | try a.assign(f, w); | | |
| 6082 | if (is_ptr) { | 5321 | if (is_ptr) { |
| 6083 | try w.writeByte('&'); | 5322 | try w.writeByte('&'); |
| 6084 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); | 5323 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 6085 | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); | 5324 | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); |
| 6086 | try a.end(f, w); | 5325 | try w.writeByte(';'); |
| | 5326 | try f.newline(); |
| 6087 | return local; | 5327 | return local; |
| 6088 | } | 5328 | } |
| 6089 | | 5329 | |
| 6090 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { | 5330 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6091 | const ctype_pool = &f.object.dg.ctype_pool; | 5331 | const zcu = f.dg.pt.zcu; |
| 6092 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5332 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6093 | | 5333 | |
| 6094 | const inst_ty = f.typeOfIndex(inst); | 5334 | const inst_ty = f.typeOfIndex(inst); |
| 6095 | const inst_ctype = try f.ctypeFromType(inst_ty, .complete); | | |
| 6096 | if (inst_ctype.isBool()) return .{ .constant = Value.true }; | | |
| 6097 | | 5335 | |
| 6098 | const operand = try f.resolveInst(ty_op.operand); | 5336 | const operand = try f.resolveInst(ty_op.operand); |
| 6099 | switch (inst_ctype.info(ctype_pool)) { | 5337 | |
| 6100 | .basic, .pointer => return f.moveCValue(inst, inst_ty, operand), | 5338 | switch (CType.classifyOptional(inst_ty, zcu)) { |
| 6101 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 5339 | .npv_payload => unreachable, // opv optional |
| 6102 | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { | 5340 | |
| 6103 | .is_null, .payload => { | 5341 | .opv_payload => unreachable, // opv bug in Sema |
| 6104 | const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete); | 5342 | |
| 6105 | const w = &f.object.code.writer; | 5343 | .error_set, |
| 6106 | const local = try f.allocLocal(inst, inst_ty); | 5344 | .ptr_like, |
| 6107 | { | 5345 | .slice_like, |
| 6108 | const a = try Assignment.start(f, w, .bool); | 5346 | => return f.moveCValue(inst, inst_ty, operand), |
| 6109 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); | 5347 | |
| 6110 | try a.assign(f, w); | 5348 | .@"struct" => { |
| 6111 | try w.writeAll("false"); | 5349 | const w = &f.code.writer; |
| 6112 | try a.end(f, w); | 5350 | const local = try f.allocLocal(inst, inst_ty); |
| 6113 | } | 5351 | |
| 6114 | { | 5352 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 6115 | const a = try Assignment.start(f, w, operand_ctype); | 5353 | try w.writeAll(" = false;"); |
| 6116 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); | 5354 | try f.newline(); |
| 6117 | try a.assign(f, w); | 5355 | |
| 6118 | try f.writeCValue(w, operand, .Other); | 5356 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6119 | try a.end(f, w); | 5357 | try w.writeAll(" = "); |
| 6120 | } | 5358 | try f.writeCValue(w, operand, .other); |
| 6121 | return local; | 5359 | try w.writeByte(';'); |
| 6122 | }, | 5360 | try f.newline(); |
| 6123 | .ptr, .len => return f.moveCValue(inst, inst_ty, operand), | 5361 | |
| 6124 | else => unreachable, | 5362 | return local; |
| 6125 | }, | 5363 | }, |
| 6126 | } | 5364 | } |
| 6127 | } | 5365 | } |
| 6128 | | 5366 | |
| 6129 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | 5367 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6130 | const pt = f.object.dg.pt; | 5368 | const pt = f.dg.pt; |
| 6131 | const zcu = pt.zcu; | 5369 | const zcu = pt.zcu; |
| 6132 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5370 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6133 | | 5371 | |
| 6134 | const inst_ty = f.typeOfIndex(inst); | 5372 | const inst_ty = f.typeOfIndex(inst); |
| 6135 | const payload_ty = inst_ty.errorUnionPayload(zcu); | 5373 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 6136 | const repr_is_err = !payload_ty.hasRuntimeBits(zcu); | | |
| 6137 | const err_ty = inst_ty.errorUnionSet(zcu); | | |
| 6138 | const err = try f.resolveInst(ty_op.operand); | 5374 | const err = try f.resolveInst(ty_op.operand); |
| 6139 | try reap(f, inst, &.{ty_op.operand}); | 5375 | try reap(f, inst, &.{ty_op.operand}); |
| 6140 | | 5376 | |
| 6141 | const w = &f.object.code.writer; | 5377 | const w = &f.code.writer; |
| 6142 | const local = try f.allocLocal(inst, inst_ty); | 5378 | const local = try f.allocLocal(inst, inst_ty); |
| 6143 | | 5379 | |
| 6144 | if (repr_is_err and err == .local and err.local == local.new_local) { | 5380 | if (payload_ty.hasRuntimeBits(zcu)) { |
| 6145 | // The store will be 'x = x'; elide it. | | |
| 6146 | return local; | | |
| 6147 | } | | |
| 6148 | | | |
| 6149 | if (!repr_is_err) { | | |
| 6150 | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); | | |
| 6151 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); | 5381 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6152 | try a.assign(f, w); | 5382 | try w.writeAll(" = "); |
| 6153 | try f.object.dg.renderUndefValue(w, payload_ty, .Other); | 5383 | try f.dg.renderUndefValue(w, payload_ty, .other); |
| 6154 | try a.end(f, w); | 5384 | try w.writeByte(';'); |
| 6155 | } | 5385 | try f.newline(); |
| 6156 | { | | |
| 6157 | const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete)); | | |
| 6158 | if (repr_is_err) | | |
| 6159 | try f.writeCValue(w, local, .Other) | | |
| 6160 | else | | |
| 6161 | try f.writeCValueMember(w, local, .{ .identifier = "error" }); | | |
| 6162 | try a.assign(f, w); | | |
| 6163 | try f.writeCValue(w, err, .Other); | | |
| 6164 | try a.end(f, w); | | |
| 6165 | } | 5386 | } |
| | 5387 | |
| | 5388 | try f.writeCValueMember(w, local, .{ .identifier = "error" }); |
| | 5389 | try w.writeAll(" = "); |
| | 5390 | try f.writeCValue(w, err, .other); |
| | 5391 | try w.writeByte(';'); |
| | 5392 | try f.newline(); |
| | 5393 | |
| 6166 | return local; | 5394 | return local; |
| 6167 | } | 5395 | } |
| 6168 | | 5396 | |
| 6169 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | 5397 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6170 | const pt = f.object.dg.pt; | 5398 | const pt = f.dg.pt; |
| 6171 | const zcu = pt.zcu; | 5399 | const w = &f.code.writer; |
| 6172 | const w = &f.object.code.writer; | | |
| 6173 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5400 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6174 | const inst_ty = f.typeOfIndex(inst); | 5401 | const inst_ty = f.typeOfIndex(inst); |
| 6175 | const operand = try f.resolveInst(ty_op.operand); | 5402 | const operand = try f.resolveInst(ty_op.operand); |
| 6176 | const operand_ty = f.typeOf(ty_op.operand); | | |
| 6177 | const error_union_ty = operand_ty.childType(zcu); | | |
| 6178 | | 5403 | |
| 6179 | const payload_ty = error_union_ty.errorUnionPayload(zcu); | | |
| 6180 | const err_int_ty = try pt.errorIntType(); | 5404 | const err_int_ty = try pt.errorIntType(); |
| 6181 | const no_err = try pt.intValue(err_int_ty, 0); | 5405 | const no_err = try pt.intValue(err_int_ty, 0); |
| 6182 | try reap(f, inst, &.{ty_op.operand}); | 5406 | try reap(f, inst, &.{ty_op.operand}); |
| 6183 | | 5407 | |
| 6184 | // First, set the non-error value. | 5408 | // First, set the non-error value. |
| 6185 | if (!payload_ty.hasRuntimeBits(zcu)) { | 5409 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }); |
| 6186 | const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete)); | 5410 | try w.print(" = {f};", .{try f.fmtIntLiteralDec(no_err)}); |
| 6187 | try f.writeCValueDeref(w, operand); | 5411 | try f.newline(); |
| 6188 | try a.assign(f, w); | | |
| 6189 | try w.print("{f}", .{try f.fmtIntLiteralDec(no_err)}); | | |
| 6190 | try a.end(f, w); | | |
| 6191 | return .none; | | |
| 6192 | } | | |
| 6193 | { | | |
| 6194 | const a = try Assignment.start(f, w, try f.ctypeFromType(err_int_ty, .complete)); | | |
| 6195 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }); | | |
| 6196 | try a.assign(f, w); | | |
| 6197 | try w.print("{f}", .{try f.fmtIntLiteralDec(no_err)}); | | |
| 6198 | try a.end(f, w); | | |
| 6199 | } | | |
| 6200 | | 5412 | |
| 6201 | // Then return the payload pointer (only if it is used) | 5413 | // Then return the payload pointer (only if it is used) |
| 6202 | if (f.liveness.isUnused(inst)) return .none; | 5414 | if (f.liveness.isUnused(inst)) return .none; |
| 6203 | | 5415 | |
| 6204 | const local = try f.allocLocal(inst, inst_ty); | 5416 | const local = try f.allocLocal(inst, inst_ty); |
| 6205 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 5417 | try f.writeCValue(w, local, .other); |
| 6206 | try f.writeCValue(w, local, .Other); | 5418 | try w.writeAll(" = &"); |
| 6207 | try a.assign(f, w); | | |
| 6208 | try w.writeByte('&'); | | |
| 6209 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); | 5419 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 6210 | try a.end(f, w); | 5420 | try w.writeByte(';'); |
| | 5421 | try f.newline(); |
| 6211 | return local; | 5422 | return local; |
| 6212 | } | 5423 | } |
| 6213 | | 5424 | |
| ... | @@ -6227,7 +5438,7 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6227,7 +5438,7 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6227 | } | 5438 | } |
| 6228 | | 5439 | |
| 6229 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { | 5440 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6230 | const pt = f.object.dg.pt; | 5441 | const pt = f.dg.pt; |
| 6231 | const zcu = pt.zcu; | 5442 | const zcu = pt.zcu; |
| 6232 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5443 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6233 | | 5444 | |
| ... | @@ -6235,120 +5446,88 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6235,120 +5446,88 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6235 | const payload_ty = inst_ty.errorUnionPayload(zcu); | 5446 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 6236 | const payload = try f.resolveInst(ty_op.operand); | 5447 | const payload = try f.resolveInst(ty_op.operand); |
| 6237 | assert(payload_ty.hasRuntimeBits(zcu)); | 5448 | assert(payload_ty.hasRuntimeBits(zcu)); |
| 6238 | const err_ty = inst_ty.errorUnionSet(zcu); | | |
| 6239 | try reap(f, inst, &.{ty_op.operand}); | 5449 | try reap(f, inst, &.{ty_op.operand}); |
| 6240 | | 5450 | |
| 6241 | const w = &f.object.code.writer; | 5451 | const w = &f.code.writer; |
| 6242 | const local = try f.allocLocal(inst, inst_ty); | 5452 | const local = try f.allocLocal(inst, inst_ty); |
| 6243 | { | 5453 | |
| 6244 | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); | 5454 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6245 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); | 5455 | try w.writeAll(" = "); |
| 6246 | try a.assign(f, w); | 5456 | try f.writeCValue(w, payload, .other); |
| 6247 | try f.writeCValue(w, payload, .Other); | 5457 | try w.writeByte(';'); |
| 6248 | try a.end(f, w); | 5458 | try f.newline(); |
| 6249 | } | 5459 | |
| 6250 | { | 5460 | try f.writeCValueMember(w, local, .{ .identifier = "error" }); |
| 6251 | const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete)); | 5461 | try w.writeAll(" = "); |
| 6252 | try f.writeCValueMember(w, local, .{ .identifier = "error" }); | 5462 | try f.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .other); |
| 6253 | try a.assign(f, w); | 5463 | try w.writeByte(';'); |
| 6254 | try f.object.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .Other); | 5464 | try f.newline(); |
| 6255 | try a.end(f, w); | 5465 | |
| 6256 | } | | |
| 6257 | return local; | 5466 | return local; |
| 6258 | } | 5467 | } |
| 6259 | | 5468 | |
| 6260 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { | 5469 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 6261 | const pt = f.object.dg.pt; | 5470 | const pt = f.dg.pt; |
| 6262 | const zcu = pt.zcu; | | |
| 6263 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 5471 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6264 | | 5472 | |
| 6265 | const w = &f.object.code.writer; | 5473 | const w = &f.code.writer; |
| 6266 | const operand = try f.resolveInst(un_op); | 5474 | const operand = try f.resolveInst(un_op); |
| 6267 | try reap(f, inst, &.{un_op}); | 5475 | try reap(f, inst, &.{un_op}); |
| 6268 | const operand_ty = f.typeOf(un_op); | | |
| 6269 | const local = try f.allocLocal(inst, .bool); | 5476 | const local = try f.allocLocal(inst, .bool); |
| 6270 | const err_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; | | |
| 6271 | const payload_ty = err_union_ty.errorUnionPayload(zcu); | | |
| 6272 | const error_ty = err_union_ty.errorUnionSet(zcu); | | |
| 6273 | | 5477 | |
| 6274 | const a = try Assignment.start(f, w, .bool); | 5478 | try f.writeCValue(w, local, .other); |
| 6275 | try f.writeCValue(w, local, .Other); | 5479 | try w.writeAll(" = "); |
| 6276 | try a.assign(f, w); | | |
| 6277 | const err_int_ty = try pt.errorIntType(); | 5480 | const err_int_ty = try pt.errorIntType(); |
| 6278 | if (!error_ty.errorSetIsEmpty(zcu)) | 5481 | if (is_ptr) |
| 6279 | if (payload_ty.hasRuntimeBits(zcu)) | 5482 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) |
| 6280 | if (is_ptr) | | |
| 6281 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) | | |
| 6282 | else | | |
| 6283 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }) | | |
| 6284 | else | | |
| 6285 | try f.writeCValue(w, operand, .Other) | | |
| 6286 | else | 5483 | else |
| 6287 | try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other); | 5484 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); |
| 6288 | try w.writeByte(' '); | 5485 | try w.print(" {s} ", .{operator}); |
| 6289 | try w.writeAll(operator); | 5486 | try f.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .other); |
| 6290 | try w.writeByte(' '); | 5487 | try w.writeByte(';'); |
| 6291 | try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other); | 5488 | try f.newline(); |
| 6292 | try a.end(f, w); | | |
| 6293 | return local; | 5489 | return local; |
| 6294 | } | 5490 | } |
| 6295 | | 5491 | |
| 6296 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { | 5492 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6297 | const pt = f.object.dg.pt; | 5493 | const pt = f.dg.pt; |
| 6298 | const zcu = pt.zcu; | 5494 | const zcu = pt.zcu; |
| 6299 | const ctype_pool = &f.object.dg.ctype_pool; | | |
| 6300 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5495 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6301 | | 5496 | |
| 6302 | const operand = try f.resolveInst(ty_op.operand); | 5497 | const operand = try f.resolveInst(ty_op.operand); |
| 6303 | try reap(f, inst, &.{ty_op.operand}); | 5498 | try reap(f, inst, &.{ty_op.operand}); |
| 6304 | const inst_ty = f.typeOfIndex(inst); | 5499 | const inst_ty = f.typeOfIndex(inst); |
| 6305 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); | 5500 | const w = &f.code.writer; |
| 6306 | const w = &f.object.code.writer; | | |
| 6307 | const local = try f.allocLocal(inst, inst_ty); | 5501 | const local = try f.allocLocal(inst, inst_ty); |
| 6308 | const operand_ty = f.typeOf(ty_op.operand); | 5502 | const operand_ty = f.typeOf(ty_op.operand); |
| 6309 | const array_ty = operand_ty.childType(zcu); | 5503 | const array_ty = operand_ty.childType(zcu); |
| 6310 | | 5504 | |
| 6311 | { | 5505 | // We have a `*[n]T`, which was turned into to a pointer to `struct { T array[n]; }`. |
| 6312 | const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete)); | 5506 | // Ideally we would want to use 'operand->array' to convert to a `T *` (we get a `T []` |
| 6313 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); | 5507 | // which decays to a pointer), but if the element type is zero-bit or the array length is |
| 6314 | try a.assign(f, w); | 5508 | // zero, there will not be an `array` member (the array type lowers to `void`). We cannot |
| 6315 | if (operand == .undef) { | 5509 | // check the type layout here because it may not be resolved, so in this instance, we must |
| 6316 | try f.writeCValue(w, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other); | 5510 | // use a pointer cast. |
| 6317 | } else { | 5511 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 6318 | const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete); | 5512 | try w.writeAll(" = ("); |
| 6319 | const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype; | 5513 | try f.dg.renderType(w, inst_ty.slicePtrFieldType(zcu)); |
| 6320 | const elem_ty = array_ty.childType(zcu); | 5514 | try w.writeByte(')'); |
| 6321 | const elem_ctype = try f.ctypeFromType(elem_ty, .complete); | 5515 | try f.writeCValue(w, operand, .other); |
| 6322 | if (!ptr_child_ctype.eql(elem_ctype)) { | 5516 | try w.writeByte(';'); |
| 6323 | try w.writeByte('('); | 5517 | try f.newline(); |
| 6324 | try f.renderCType(w, ptr_ctype); | 5518 | |
| 6325 | try w.writeByte(')'); | 5519 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); |
| 6326 | } | 5520 | try w.print(" = {f}", .{ |
| 6327 | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); | 5521 | try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))), |
| 6328 | const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype; | 5522 | }); |
| 6329 | if (operand_child_ctype.info(ctype_pool) == .array) { | 5523 | try w.writeByte(';'); |
| 6330 | try w.writeByte('&'); | 5524 | try f.newline(); |
| 6331 | try f.writeCValueDeref(w, operand); | | |
| 6332 | try w.print("[{f}]", .{try f.fmtIntLiteralDec(.zero_usize)}); | | |
| 6333 | } else try f.writeCValue(w, operand, .Other); | | |
| 6334 | } | | |
| 6335 | try a.end(f, w); | | |
| 6336 | } | | |
| 6337 | { | | |
| 6338 | const a = try Assignment.start(f, w, .usize); | | |
| 6339 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); | | |
| 6340 | try a.assign(f, w); | | |
| 6341 | try w.print("{f}", .{ | | |
| 6342 | try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))), | | |
| 6343 | }); | | |
| 6344 | try a.end(f, w); | | |
| 6345 | } | | |
| 6346 | | 5525 | |
| 6347 | return local; | 5526 | return local; |
| 6348 | } | 5527 | } |
| 6349 | | 5528 | |
| 6350 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | 5529 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6351 | const pt = f.object.dg.pt; | 5530 | const pt = f.dg.pt; |
| 6352 | const zcu = pt.zcu; | 5531 | const zcu = pt.zcu; |
| 6353 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 5532 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6354 | | 5533 | |
| ... | @@ -6358,7 +5537,7 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6358,7 +5537,7 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6358 | try reap(f, inst, &.{ty_op.operand}); | 5537 | try reap(f, inst, &.{ty_op.operand}); |
| 6359 | const operand_ty = f.typeOf(ty_op.operand); | 5538 | const operand_ty = f.typeOf(ty_op.operand); |
| 6360 | const scalar_ty = operand_ty.scalarType(zcu); | 5539 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6361 | const target = &f.object.dg.mod.resolved_target.result; | 5540 | const target = &f.dg.mod.resolved_target.result; |
| 6362 | const operation = if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isRuntimeFloat()) | 5541 | const operation = if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isRuntimeFloat()) |
| 6363 | if (inst_scalar_ty.floatBits(target) < scalar_ty.floatBits(target)) "trunc" else "extend" | 5542 | if (inst_scalar_ty.floatBits(target) < scalar_ty.floatBits(target)) "trunc" else "extend" |
| 6364 | else if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) | 5543 | else if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) |
| ... | @@ -6368,16 +5547,15 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6368,16 +5547,15 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6368 | else | 5547 | else |
| 6369 | unreachable; | 5548 | unreachable; |
| 6370 | | 5549 | |
| 6371 | const w = &f.object.code.writer; | 5550 | const w = &f.code.writer; |
| 6372 | const local = try f.allocLocal(inst, inst_ty); | 5551 | const local = try f.allocLocal(inst, inst_ty); |
| 6373 | const v = try Vectorize.start(f, inst, w, operand_ty); | 5552 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6374 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); | 5553 | try f.writeCValue(w, local, .other); |
| 6375 | try f.writeCValue(w, local, .Other); | | |
| 6376 | try v.elem(f, w); | 5554 | try v.elem(f, w); |
| 6377 | try a.assign(f, w); | 5555 | try w.writeAll(" = "); |
| 6378 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { | 5556 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { |
| 6379 | try w.writeAll("zig_wrap_"); | 5557 | try w.writeAll("zig_wrap_"); |
| 6380 | try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty); | 5558 | try f.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty); |
| 6381 | try w.writeByte('('); | 5559 | try w.writeByte('('); |
| 6382 | } | 5560 | } |
| 6383 | try w.writeAll("zig_"); | 5561 | try w.writeAll("zig_"); |
| ... | @@ -6385,14 +5563,15 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6385,14 +5563,15 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6385 | try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target)); | 5563 | try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target)); |
| 6386 | try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target)); | 5564 | try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target)); |
| 6387 | try w.writeByte('('); | 5565 | try w.writeByte('('); |
| 6388 | try f.writeCValue(w, operand, .FunctionArgument); | 5566 | try f.writeCValue(w, operand, .other); |
| 6389 | try v.elem(f, w); | 5567 | try v.elem(f, w); |
| 6390 | try w.writeByte(')'); | 5568 | try w.writeByte(')'); |
| 6391 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { | 5569 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { |
| 6392 | try f.object.dg.renderBuiltinInfo(w, inst_scalar_ty, .bits); | 5570 | try f.dg.renderBuiltinInfo(w, inst_scalar_ty, .bits); |
| 6393 | try w.writeByte(')'); | 5571 | try w.writeByte(')'); |
| 6394 | } | 5572 | } |
| 6395 | try a.end(f, w); | 5573 | try w.writeByte(';'); |
| | 5574 | try f.newline(); |
| 6396 | try v.end(f, inst, w); | 5575 | try v.end(f, inst, w); |
| 6397 | | 5576 | |
| 6398 | return local; | 5577 | return local; |
| ... | @@ -6405,7 +5584,7 @@ fn airUnBuiltinCall( | ... | @@ -6405,7 +5584,7 @@ fn airUnBuiltinCall( |
| 6405 | operation: []const u8, | 5584 | operation: []const u8, |
| 6406 | info: BuiltinInfo, | 5585 | info: BuiltinInfo, |
| 6407 | ) !CValue { | 5586 | ) !CValue { |
| 6408 | const pt = f.object.dg.pt; | 5587 | const pt = f.dg.pt; |
| 6409 | const zcu = pt.zcu; | 5588 | const zcu = pt.zcu; |
| 6410 | | 5589 | |
| 6411 | const operand = try f.resolveInst(operand_ref); | 5590 | const operand = try f.resolveInst(operand_ref); |
| ... | @@ -6415,30 +5594,32 @@ fn airUnBuiltinCall( | ... | @@ -6415,30 +5594,32 @@ fn airUnBuiltinCall( |
| 6415 | const operand_ty = f.typeOf(operand_ref); | 5594 | const operand_ty = f.typeOf(operand_ref); |
| 6416 | const scalar_ty = operand_ty.scalarType(zcu); | 5595 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6417 | | 5596 | |
| 6418 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); | 5597 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 6419 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; | 5598 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6420 | | 5599 | |
| 6421 | const w = &f.object.code.writer; | 5600 | const w = &f.code.writer; |
| 6422 | const local = try f.allocLocal(inst, inst_ty); | 5601 | const local = try f.allocLocal(inst, inst_ty); |
| 6423 | const v = try Vectorize.start(f, inst, w, operand_ty); | 5602 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6424 | if (!ref_ret) { | 5603 | if (!ref_ret) { |
| 6425 | try f.writeCValue(w, local, .Other); | 5604 | try f.writeCValue(w, local, .other); |
| 6426 | try v.elem(f, w); | 5605 | try v.elem(f, w); |
| 6427 | try w.writeAll(" = "); | 5606 | try w.writeAll(" = "); |
| 6428 | } | 5607 | } |
| 6429 | try w.print("zig_{s}_", .{operation}); | 5608 | try w.print("zig_{s}_", .{operation}); |
| 6430 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 5609 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 6431 | try w.writeByte('('); | 5610 | try w.writeByte('('); |
| 6432 | if (ref_ret) { | 5611 | if (ref_ret) { |
| 6433 | try f.writeCValue(w, local, .FunctionArgument); | 5612 | try w.writeByte('&'); |
| | 5613 | try f.writeCValue(w, local, .other); |
| 6434 | try v.elem(f, w); | 5614 | try v.elem(f, w); |
| 6435 | try w.writeAll(", "); | 5615 | try w.writeAll(", "); |
| 6436 | } | 5616 | } |
| 6437 | try f.writeCValue(w, operand, .FunctionArgument); | 5617 | if (ref_arg) try w.writeByte('&'); |
| | 5618 | try f.writeCValue(w, operand, .other); |
| 6438 | try v.elem(f, w); | 5619 | try v.elem(f, w); |
| 6439 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); | 5620 | try f.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 6440 | try w.writeAll(");"); | 5621 | try w.writeAll(");"); |
| 6441 | try f.object.newline(); | 5622 | try f.newline(); |
| 6442 | try v.end(f, inst, w); | 5623 | try v.end(f, inst, w); |
| 6443 | | 5624 | |
| 6444 | return local; | 5625 | return local; |
| ... | @@ -6450,13 +5631,12 @@ fn airBinBuiltinCall( | ... | @@ -6450,13 +5631,12 @@ fn airBinBuiltinCall( |
| 6450 | operation: []const u8, | 5631 | operation: []const u8, |
| 6451 | info: BuiltinInfo, | 5632 | info: BuiltinInfo, |
| 6452 | ) !CValue { | 5633 | ) !CValue { |
| 6453 | const pt = f.object.dg.pt; | 5634 | const pt = f.dg.pt; |
| 6454 | const zcu = pt.zcu; | 5635 | const zcu = pt.zcu; |
| 6455 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 5636 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6456 | | 5637 | |
| 6457 | const operand_ty = f.typeOf(bin_op.lhs); | 5638 | const operand_ty = f.typeOf(bin_op.lhs); |
| 6458 | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); | 5639 | const is_big = lowersToBigInt(operand_ty, zcu); |
| 6459 | const is_big = operand_ctype.info(&f.object.dg.ctype_pool) == .array; | | |
| 6460 | | 5640 | |
| 6461 | const lhs = try f.resolveInst(bin_op.lhs); | 5641 | const lhs = try f.resolveInst(bin_op.lhs); |
| 6462 | const rhs = try f.resolveInst(bin_op.rhs); | 5642 | const rhs = try f.resolveInst(bin_op.rhs); |
| ... | @@ -6466,32 +5646,35 @@ fn airBinBuiltinCall( | ... | @@ -6466,32 +5646,35 @@ fn airBinBuiltinCall( |
| 6466 | const inst_scalar_ty = inst_ty.scalarType(zcu); | 5646 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6467 | const scalar_ty = operand_ty.scalarType(zcu); | 5647 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6468 | | 5648 | |
| 6469 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); | 5649 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 6470 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; | 5650 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6471 | | 5651 | |
| 6472 | const w = &f.object.code.writer; | 5652 | const w = &f.code.writer; |
| 6473 | const local = try f.allocLocal(inst, inst_ty); | 5653 | const local = try f.allocLocal(inst, inst_ty); |
| 6474 | if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 5654 | if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6475 | const v = try Vectorize.start(f, inst, w, operand_ty); | 5655 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6476 | if (!ref_ret) { | 5656 | if (!ref_ret) { |
| 6477 | try f.writeCValue(w, local, .Other); | 5657 | try f.writeCValue(w, local, .other); |
| 6478 | try v.elem(f, w); | 5658 | try v.elem(f, w); |
| 6479 | try w.writeAll(" = "); | 5659 | try w.writeAll(" = "); |
| 6480 | } | 5660 | } |
| 6481 | try w.print("zig_{s}_", .{operation}); | 5661 | try w.print("zig_{s}_", .{operation}); |
| 6482 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 5662 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 6483 | try w.writeByte('('); | 5663 | try w.writeByte('('); |
| 6484 | if (ref_ret) { | 5664 | if (ref_ret) { |
| 6485 | try f.writeCValue(w, local, .FunctionArgument); | 5665 | try w.writeByte('&'); |
| | 5666 | try f.writeCValue(w, local, .other); |
| 6486 | try v.elem(f, w); | 5667 | try v.elem(f, w); |
| 6487 | try w.writeAll(", "); | 5668 | try w.writeAll(", "); |
| 6488 | } | 5669 | } |
| 6489 | try f.writeCValue(w, lhs, .FunctionArgument); | 5670 | if (ref_arg) try w.writeByte('&'); |
| | 5671 | try f.writeCValue(w, lhs, .other); |
| 6490 | try v.elem(f, w); | 5672 | try v.elem(f, w); |
| 6491 | try w.writeAll(", "); | 5673 | try w.writeAll(", "); |
| 6492 | try f.writeCValue(w, rhs, .FunctionArgument); | 5674 | if (ref_arg) try w.writeByte('&'); |
| | 5675 | try f.writeCValue(w, rhs, .other); |
| 6493 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); | 5676 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); |
| 6494 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); | 5677 | try f.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 6495 | try w.writeAll(");\n"); | 5678 | try w.writeAll(");\n"); |
| 6496 | try v.end(f, inst, w); | 5679 | try v.end(f, inst, w); |
| 6497 | | 5680 | |
| ... | @@ -6506,7 +5689,7 @@ fn airCmpBuiltinCall( | ... | @@ -6506,7 +5689,7 @@ fn airCmpBuiltinCall( |
| 6506 | operation: enum { cmp, operator }, | 5689 | operation: enum { cmp, operator }, |
| 6507 | info: BuiltinInfo, | 5690 | info: BuiltinInfo, |
| 6508 | ) !CValue { | 5691 | ) !CValue { |
| 6509 | const pt = f.object.dg.pt; | 5692 | const pt = f.dg.pt; |
| 6510 | const zcu = pt.zcu; | 5693 | const zcu = pt.zcu; |
| 6511 | const lhs = try f.resolveInst(data.lhs); | 5694 | const lhs = try f.resolveInst(data.lhs); |
| 6512 | const rhs = try f.resolveInst(data.rhs); | 5695 | const rhs = try f.resolveInst(data.rhs); |
| ... | @@ -6517,14 +5700,14 @@ fn airCmpBuiltinCall( | ... | @@ -6517,14 +5700,14 @@ fn airCmpBuiltinCall( |
| 6517 | const operand_ty = f.typeOf(data.lhs); | 5700 | const operand_ty = f.typeOf(data.lhs); |
| 6518 | const scalar_ty = operand_ty.scalarType(zcu); | 5701 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6519 | | 5702 | |
| 6520 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); | 5703 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 6521 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; | 5704 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6522 | | 5705 | |
| 6523 | const w = &f.object.code.writer; | 5706 | const w = &f.code.writer; |
| 6524 | const local = try f.allocLocal(inst, inst_ty); | 5707 | const local = try f.allocLocal(inst, inst_ty); |
| 6525 | const v = try Vectorize.start(f, inst, w, operand_ty); | 5708 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6526 | if (!ref_ret) { | 5709 | if (!ref_ret) { |
| 6527 | try f.writeCValue(w, local, .Other); | 5710 | try f.writeCValue(w, local, .other); |
| 6528 | try v.elem(f, w); | 5711 | try v.elem(f, w); |
| 6529 | try w.writeAll(" = "); | 5712 | try w.writeAll(" = "); |
| 6530 | } | 5713 | } |
| ... | @@ -6532,33 +5715,36 @@ fn airCmpBuiltinCall( | ... | @@ -6532,33 +5715,36 @@ fn airCmpBuiltinCall( |
| 6532 | else => @tagName(operation), | 5715 | else => @tagName(operation), |
| 6533 | .operator => compareOperatorAbbrev(operator), | 5716 | .operator => compareOperatorAbbrev(operator), |
| 6534 | }}); | 5717 | }}); |
| 6535 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 5718 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 6536 | try w.writeByte('('); | 5719 | try w.writeByte('('); |
| 6537 | if (ref_ret) { | 5720 | if (ref_ret) { |
| 6538 | try f.writeCValue(w, local, .FunctionArgument); | 5721 | try w.writeByte('&'); |
| | 5722 | try f.writeCValue(w, local, .other); |
| 6539 | try v.elem(f, w); | 5723 | try v.elem(f, w); |
| 6540 | try w.writeAll(", "); | 5724 | try w.writeAll(", "); |
| 6541 | } | 5725 | } |
| 6542 | try f.writeCValue(w, lhs, .FunctionArgument); | 5726 | if (ref_arg) try w.writeByte('&'); |
| | 5727 | try f.writeCValue(w, lhs, .other); |
| 6543 | try v.elem(f, w); | 5728 | try v.elem(f, w); |
| 6544 | try w.writeAll(", "); | 5729 | try w.writeAll(", "); |
| 6545 | try f.writeCValue(w, rhs, .FunctionArgument); | 5730 | if (ref_arg) try w.writeByte('&'); |
| | 5731 | try f.writeCValue(w, rhs, .other); |
| 6546 | try v.elem(f, w); | 5732 | try v.elem(f, w); |
| 6547 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); | 5733 | try f.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 6548 | try w.writeByte(')'); | 5734 | try w.writeByte(')'); |
| 6549 | if (!ref_ret) try w.print("{s}{f}", .{ | 5735 | if (!ref_ret) try w.print("{s}{f}", .{ |
| 6550 | compareOperatorC(operator), | 5736 | compareOperatorC(operator), |
| 6551 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), | 5737 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), |
| 6552 | }); | 5738 | }); |
| 6553 | try w.writeByte(';'); | 5739 | try w.writeByte(';'); |
| 6554 | try f.object.newline(); | 5740 | try f.newline(); |
| 6555 | try v.end(f, inst, w); | 5741 | try v.end(f, inst, w); |
| 6556 | | 5742 | |
| 6557 | return local; | 5743 | return local; |
| 6558 | } | 5744 | } |
| 6559 | | 5745 | |
| 6560 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { | 5746 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 6561 | const pt = f.object.dg.pt; | 5747 | const pt = f.dg.pt; |
| 6562 | const zcu = pt.zcu; | 5748 | const zcu = pt.zcu; |
| 6563 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 5749 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6564 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; | 5750 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| ... | @@ -6568,9 +5754,8 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue | ... | @@ -6568,9 +5754,8 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6568 | const new_value = try f.resolveInst(extra.new_value); | 5754 | const new_value = try f.resolveInst(extra.new_value); |
| 6569 | const ptr_ty = f.typeOf(extra.ptr); | 5755 | const ptr_ty = f.typeOf(extra.ptr); |
| 6570 | const ty = ptr_ty.childType(zcu); | 5756 | const ty = ptr_ty.childType(zcu); |
| 6571 | const ctype = try f.ctypeFromType(ty, .complete); | | |
| 6572 | | 5757 | |
| 6573 | const w = &f.object.code.writer; | 5758 | const w = &f.code.writer; |
| 6574 | const new_value_mat = try Materialize.start(f, inst, ty, new_value); | 5759 | const new_value_mat = try Materialize.start(f, inst, ty, new_value); |
| 6575 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); | 5760 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6576 | | 5761 | |
| ... | @@ -6581,13 +5766,11 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue | ... | @@ -6581,13 +5766,11 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6581 | | 5766 | |
| 6582 | const local = try f.allocLocal(inst, inst_ty); | 5767 | const local = try f.allocLocal(inst, inst_ty); |
| 6583 | if (inst_ty.isPtrLikeOptional(zcu)) { | 5768 | if (inst_ty.isPtrLikeOptional(zcu)) { |
| 6584 | { | 5769 | try f.writeCValue(w, local, .other); |
| 6585 | const a = try Assignment.start(f, w, ctype); | 5770 | try w.writeAll(" = "); |
| 6586 | try f.writeCValue(w, local, .Other); | 5771 | try f.writeCValue(w, expected_value, .other); |
| 6587 | try a.assign(f, w); | 5772 | try w.writeByte(';'); |
| 6588 | try f.writeCValue(w, expected_value, .Other); | 5773 | try f.newline(); |
| 6589 | try a.end(f, w); | | |
| 6590 | } | | |
| 6591 | | 5774 | |
| 6592 | try w.writeAll("if ("); | 5775 | try w.writeAll("if ("); |
| 6593 | try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); | 5776 | try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| ... | @@ -6595,9 +5778,9 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue | ... | @@ -6595,9 +5778,9 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6595 | try w.writeByte(')'); | 5778 | try w.writeByte(')'); |
| 6596 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); | 5779 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6597 | try w.writeAll(" *)"); | 5780 | try w.writeAll(" *)"); |
| 6598 | try f.writeCValue(w, ptr, .Other); | 5781 | try f.writeCValue(w, ptr, .other); |
| 6599 | try w.writeAll(", "); | 5782 | try w.writeAll(", "); |
| 6600 | try f.writeCValue(w, local, .FunctionArgument); | 5783 | try f.writeCValue(w, local, .other); |
| 6601 | try w.writeAll(", "); | 5784 | try w.writeAll(", "); |
| 6602 | try new_value_mat.mat(f, w); | 5785 | try new_value_mat.mat(f, w); |
| 6603 | try w.writeAll(", "); | 5786 | try w.writeAll(", "); |
| ... | @@ -6605,56 +5788,49 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue | ... | @@ -6605,56 +5788,49 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6605 | try w.writeAll(", "); | 5788 | try w.writeAll(", "); |
| 6606 | try writeMemoryOrder(w, extra.failureOrder()); | 5789 | try writeMemoryOrder(w, extra.failureOrder()); |
| 6607 | try w.writeAll(", "); | 5790 | try w.writeAll(", "); |
| 6608 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); | 5791 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6609 | try w.writeAll(", "); | 5792 | try w.writeAll(", "); |
| 6610 | try f.renderType(w, repr_ty); | 5793 | try f.renderType(w, repr_ty); |
| 6611 | try w.writeByte(')'); | 5794 | try w.writeByte(')'); |
| 6612 | try w.writeAll(") {"); | 5795 | try w.writeAll(") {"); |
| 6613 | f.object.indent(); | 5796 | f.indent(); |
| 6614 | try f.object.newline(); | 5797 | try f.newline(); |
| 6615 | { | 5798 | |
| 6616 | const a = try Assignment.start(f, w, ctype); | 5799 | try f.writeCValue(w, local, .other); |
| 6617 | try f.writeCValue(w, local, .Other); | 5800 | try w.writeAll(" = NULL;"); |
| 6618 | try a.assign(f, w); | 5801 | try f.newline(); |
| 6619 | try w.writeAll("NULL"); | 5802 | |
| 6620 | try a.end(f, w); | 5803 | try f.outdent(); |
| 6621 | } | | |
| 6622 | try f.object.outdent(); | | |
| 6623 | try w.writeByte('}'); | 5804 | try w.writeByte('}'); |
| 6624 | try f.object.newline(); | 5805 | try f.newline(); |
| 6625 | } else { | 5806 | } else { |
| 6626 | { | 5807 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6627 | const a = try Assignment.start(f, w, ctype); | 5808 | try w.writeAll(" = "); |
| 6628 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); | 5809 | try f.writeCValue(w, expected_value, .other); |
| 6629 | try a.assign(f, w); | 5810 | try w.writeByte(';'); |
| 6630 | try f.writeCValue(w, expected_value, .Other); | 5811 | try f.newline(); |
| 6631 | try a.end(f, w); | 5812 | |
| 6632 | } | 5813 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 6633 | { | 5814 | try w.print(" = zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 6634 | const a = try Assignment.start(f, w, .bool); | 5815 | try f.renderType(w, ty); |
| 6635 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); | 5816 | try w.writeByte(')'); |
| 6636 | try a.assign(f, w); | 5817 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6637 | try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); | 5818 | try w.writeAll(" *)"); |
| 6638 | try f.renderType(w, ty); | 5819 | try f.writeCValue(w, ptr, .other); |
| 6639 | try w.writeByte(')'); | 5820 | try w.writeAll(", "); |
| 6640 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); | 5821 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6641 | try w.writeAll(" *)"); | 5822 | try w.writeAll(", "); |
| 6642 | try f.writeCValue(w, ptr, .Other); | 5823 | try new_value_mat.mat(f, w); |
| 6643 | try w.writeAll(", "); | 5824 | try w.writeAll(", "); |
| 6644 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); | 5825 | try writeMemoryOrder(w, extra.successOrder()); |
| 6645 | try w.writeAll(", "); | 5826 | try w.writeAll(", "); |
| 6646 | try new_value_mat.mat(f, w); | 5827 | try writeMemoryOrder(w, extra.failureOrder()); |
| 6647 | try w.writeAll(", "); | 5828 | try w.writeAll(", "); |
| 6648 | try writeMemoryOrder(w, extra.successOrder()); | 5829 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6649 | try w.writeAll(", "); | 5830 | try w.writeAll(", "); |
| 6650 | try writeMemoryOrder(w, extra.failureOrder()); | 5831 | try f.renderType(w, repr_ty); |
| 6651 | try w.writeAll(", "); | 5832 | try w.writeAll(");"); |
| 6652 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); | 5833 | try f.newline(); |
| 6653 | try w.writeAll(", "); | | |
| 6654 | try f.renderType(w, repr_ty); | | |
| 6655 | try w.writeByte(')'); | | |
| 6656 | try a.end(f, w); | | |
| 6657 | } | | |
| 6658 | } | 5834 | } |
| 6659 | try new_value_mat.end(f, inst); | 5835 | try new_value_mat.end(f, inst); |
| 6660 | | 5836 | |
| ... | @@ -6667,7 +5843,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue | ... | @@ -6667,7 +5843,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6667 | } | 5843 | } |
| 6668 | | 5844 | |
| 6669 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | 5845 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6670 | const pt = f.object.dg.pt; | 5846 | const pt = f.dg.pt; |
| 6671 | const zcu = pt.zcu; | 5847 | const zcu = pt.zcu; |
| 6672 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 5848 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6673 | const extra = f.air.extraData(Air.AtomicRmw, pl_op.payload).data; | 5849 | const extra = f.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| ... | @@ -6677,7 +5853,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6677,7 +5853,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6677 | const ptr = try f.resolveInst(pl_op.operand); | 5853 | const ptr = try f.resolveInst(pl_op.operand); |
| 6678 | const operand = try f.resolveInst(extra.operand); | 5854 | const operand = try f.resolveInst(extra.operand); |
| 6679 | | 5855 | |
| 6680 | const w = &f.object.code.writer; | 5856 | const w = &f.code.writer; |
| 6681 | const operand_mat = try Materialize.start(f, inst, ty, operand); | 5857 | const operand_mat = try Materialize.start(f, inst, ty, operand); |
| 6682 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); | 5858 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); |
| 6683 | | 5859 | |
| ... | @@ -6690,7 +5866,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6690,7 +5866,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6690 | try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); | 5866 | try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); |
| 6691 | if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128"); | 5867 | if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128"); |
| 6692 | try w.writeByte('('); | 5868 | try w.writeByte('('); |
| 6693 | try f.writeCValue(w, local, .Other); | 5869 | try f.writeCValue(w, local, .other); |
| 6694 | try w.writeAll(", ("); | 5870 | try w.writeAll(", ("); |
| 6695 | const use_atomic = switch (extra.op()) { | 5871 | const use_atomic = switch (extra.op()) { |
| 6696 | else => true, | 5872 | else => true, |
| ... | @@ -6702,17 +5878,17 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6702,17 +5878,17 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6702 | if (use_atomic) try w.writeByte(')'); | 5878 | if (use_atomic) try w.writeByte(')'); |
| 6703 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); | 5879 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6704 | try w.writeAll(" *)"); | 5880 | try w.writeAll(" *)"); |
| 6705 | try f.writeCValue(w, ptr, .Other); | 5881 | try f.writeCValue(w, ptr, .other); |
| 6706 | try w.writeAll(", "); | 5882 | try w.writeAll(", "); |
| 6707 | try operand_mat.mat(f, w); | 5883 | try operand_mat.mat(f, w); |
| 6708 | try w.writeAll(", "); | 5884 | try w.writeAll(", "); |
| 6709 | try writeMemoryOrder(w, extra.ordering()); | 5885 | try writeMemoryOrder(w, extra.ordering()); |
| 6710 | try w.writeAll(", "); | 5886 | try w.writeAll(", "); |
| 6711 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); | 5887 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6712 | try w.writeAll(", "); | 5888 | try w.writeAll(", "); |
| 6713 | try f.renderType(w, repr_ty); | 5889 | try f.renderType(w, repr_ty); |
| 6714 | try w.writeAll(");"); | 5890 | try w.writeAll(");"); |
| 6715 | try f.object.newline(); | 5891 | try f.newline(); |
| 6716 | try operand_mat.end(f, inst); | 5892 | try operand_mat.end(f, inst); |
| 6717 | | 5893 | |
| 6718 | if (f.liveness.isUnused(inst)) { | 5894 | if (f.liveness.isUnused(inst)) { |
| ... | @@ -6724,7 +5900,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6724,7 +5900,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6724 | } | 5900 | } |
| 6725 | | 5901 | |
| 6726 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { | 5902 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6727 | const pt = f.object.dg.pt; | 5903 | const pt = f.dg.pt; |
| 6728 | const zcu = pt.zcu; | 5904 | const zcu = pt.zcu; |
| 6729 | const atomic_load = f.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; | 5905 | const atomic_load = f.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 6730 | const ptr = try f.resolveInst(atomic_load.ptr); | 5906 | const ptr = try f.resolveInst(atomic_load.ptr); |
| ... | @@ -6738,31 +5914,31 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6738,31 +5914,31 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6738 | ty; | 5914 | ty; |
| 6739 | | 5915 | |
| 6740 | const inst_ty = f.typeOfIndex(inst); | 5916 | const inst_ty = f.typeOfIndex(inst); |
| 6741 | const w = &f.object.code.writer; | 5917 | const w = &f.code.writer; |
| 6742 | const local = try f.allocLocal(inst, inst_ty); | 5918 | const local = try f.allocLocal(inst, inst_ty); |
| 6743 | | 5919 | |
| 6744 | try w.writeAll("zig_atomic_load("); | 5920 | try w.writeAll("zig_atomic_load("); |
| 6745 | try f.writeCValue(w, local, .Other); | 5921 | try f.writeCValue(w, local, .other); |
| 6746 | try w.writeAll(", (zig_atomic("); | 5922 | try w.writeAll(", (zig_atomic("); |
| 6747 | try f.renderType(w, ty); | 5923 | try f.renderType(w, ty); |
| 6748 | try w.writeByte(')'); | 5924 | try w.writeByte(')'); |
| 6749 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); | 5925 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6750 | try w.writeAll(" *)"); | 5926 | try w.writeAll(" *)"); |
| 6751 | try f.writeCValue(w, ptr, .Other); | 5927 | try f.writeCValue(w, ptr, .other); |
| 6752 | try w.writeAll(", "); | 5928 | try w.writeAll(", "); |
| 6753 | try writeMemoryOrder(w, atomic_load.order); | 5929 | try writeMemoryOrder(w, atomic_load.order); |
| 6754 | try w.writeAll(", "); | 5930 | try w.writeAll(", "); |
| 6755 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); | 5931 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6756 | try w.writeAll(", "); | 5932 | try w.writeAll(", "); |
| 6757 | try f.renderType(w, repr_ty); | 5933 | try f.renderType(w, repr_ty); |
| 6758 | try w.writeAll(");"); | 5934 | try w.writeAll(");"); |
| 6759 | try f.object.newline(); | 5935 | try f.newline(); |
| 6760 | | 5936 | |
| 6761 | return local; | 5937 | return local; |
| 6762 | } | 5938 | } |
| 6763 | | 5939 | |
| 6764 | fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { | 5940 | fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { |
| 6765 | const pt = f.object.dg.pt; | 5941 | const pt = f.dg.pt; |
| 6766 | const zcu = pt.zcu; | 5942 | const zcu = pt.zcu; |
| 6767 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 5943 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6768 | const ptr_ty = f.typeOf(bin_op.lhs); | 5944 | const ptr_ty = f.typeOf(bin_op.lhs); |
| ... | @@ -6770,7 +5946,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa | ... | @@ -6770,7 +5946,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6770 | const ptr = try f.resolveInst(bin_op.lhs); | 5946 | const ptr = try f.resolveInst(bin_op.lhs); |
| 6771 | const element = try f.resolveInst(bin_op.rhs); | 5947 | const element = try f.resolveInst(bin_op.rhs); |
| 6772 | | 5948 | |
| 6773 | const w = &f.object.code.writer; | 5949 | const w = &f.code.writer; |
| 6774 | const element_mat = try Materialize.start(f, inst, ty, element); | 5950 | const element_mat = try Materialize.start(f, inst, ty, element); |
| 6775 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 5951 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6776 | | 5952 | |
| ... | @@ -6784,32 +5960,22 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa | ... | @@ -6784,32 +5960,22 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6784 | try w.writeByte(')'); | 5960 | try w.writeByte(')'); |
| 6785 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); | 5961 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6786 | try w.writeAll(" *)"); | 5962 | try w.writeAll(" *)"); |
| 6787 | try f.writeCValue(w, ptr, .Other); | 5963 | try f.writeCValue(w, ptr, .other); |
| 6788 | try w.writeAll(", "); | 5964 | try w.writeAll(", "); |
| 6789 | try element_mat.mat(f, w); | 5965 | try element_mat.mat(f, w); |
| 6790 | try w.print(", {s}, ", .{order}); | 5966 | try w.print(", {s}, ", .{order}); |
| 6791 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); | 5967 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6792 | try w.writeAll(", "); | 5968 | try w.writeAll(", "); |
| 6793 | try f.renderType(w, repr_ty); | 5969 | try f.renderType(w, repr_ty); |
| 6794 | try w.writeAll(");"); | 5970 | try w.writeAll(");"); |
| 6795 | try f.object.newline(); | 5971 | try f.newline(); |
| 6796 | try element_mat.end(f, inst); | 5972 | try element_mat.end(f, inst); |
| 6797 | | 5973 | |
| 6798 | return .none; | 5974 | return .none; |
| 6799 | } | 5975 | } |
| 6800 | | 5976 | |
| 6801 | fn writeSliceOrPtr(f: *Function, w: *Writer, ptr: CValue, ptr_ty: Type) !void { | | |
| 6802 | const pt = f.object.dg.pt; | | |
| 6803 | const zcu = pt.zcu; | | |
| 6804 | if (ptr_ty.isSlice(zcu)) { | | |
| 6805 | try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" }); | | |
| 6806 | } else { | | |
| 6807 | try f.writeCValue(w, ptr, .FunctionArgument); | | |
| 6808 | } | | |
| 6809 | } | | |
| 6810 | | | |
| 6811 | fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | 5977 | fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6812 | const pt = f.object.dg.pt; | 5978 | const pt = f.dg.pt; |
| 6813 | const zcu = pt.zcu; | 5979 | const zcu = pt.zcu; |
| 6814 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 5980 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6815 | const dest_ty = f.typeOf(bin_op.lhs); | 5981 | const dest_ty = f.typeOf(bin_op.lhs); |
| ... | @@ -6818,7 +5984,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -6818,7 +5984,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6818 | const elem_ty = f.typeOf(bin_op.rhs); | 5984 | const elem_ty = f.typeOf(bin_op.rhs); |
| 6819 | const elem_abi_size = elem_ty.abiSize(zcu); | 5985 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 6820 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false; | 5986 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false; |
| 6821 | const w = &f.object.code.writer; | 5987 | const w = &f.code.writer; |
| 6822 | | 5988 | |
| 6823 | if (val_is_undef) { | 5989 | if (val_is_undef) { |
| 6824 | if (!safety) { | 5990 | if (!safety) { |
| ... | @@ -6832,153 +5998,128 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { | ... | @@ -6832,153 +5998,128 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6832 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); | 5998 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); |
| 6833 | try w.writeAll(", 0xaa, "); | 5999 | try w.writeAll(", 0xaa, "); |
| 6834 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); | 6000 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 6835 | if (elem_abi_size > 1) { | | |
| 6836 | try w.print(" * {d}", .{elem_abi_size}); | | |
| 6837 | } | | |
| 6838 | try w.writeAll(");"); | | |
| 6839 | try f.object.newline(); | | |
| 6840 | }, | 6001 | }, |
| 6841 | .one => { | 6002 | .one => { |
| 6842 | const array_ty = dest_ty.childType(zcu); | 6003 | try f.writeCValue(w, dest_slice, .other); |
| 6843 | const len = array_ty.arrayLen(zcu) * elem_abi_size; | 6004 | try w.print(", 0xaa, {d}", .{dest_ty.childType(zcu).arrayLen(zcu)}); |
| 6844 | | | |
| 6845 | try f.writeCValue(w, dest_slice, .FunctionArgument); | | |
| 6846 | try w.print(", 0xaa, {d});", .{len}); | | |
| 6847 | try f.object.newline(); | | |
| 6848 | }, | 6005 | }, |
| 6849 | .many, .c => unreachable, | 6006 | .many, .c => unreachable, |
| 6850 | } | 6007 | } |
| | 6008 | if (elem_abi_size > 0) try w.print(" * {d}", .{elem_abi_size}); |
| | 6009 | try w.writeAll(");"); |
| | 6010 | try f.newline(); |
| 6851 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 6011 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6852 | return .none; | 6012 | return .none; |
| 6853 | } | 6013 | } |
| 6854 | | 6014 | |
| 6855 | if (elem_abi_size > 1 or dest_ty.isVolatilePtr(zcu)) { | 6015 | if (elem_abi_size == 1 and !dest_ty.isVolatilePtr(zcu)) { |
| 6856 | // For the assignment in this loop, the array pointer needs to get | 6016 | const bitcasted = try bitcast(f, .u8, value, elem_ty); |
| 6857 | // casted to a regular pointer, otherwise an error like this occurs: | 6017 | try w.writeAll("memset("); |
| 6858 | // error: array type 'uint32_t[20]' (aka 'unsigned int[20]') is not assignable | | |
| 6859 | const elem_ptr_ty = try pt.ptrType(.{ | | |
| 6860 | .child = elem_ty.toIntern(), | | |
| 6861 | .flags = .{ | | |
| 6862 | .size = .c, | | |
| 6863 | }, | | |
| 6864 | }); | | |
| 6865 | | | |
| 6866 | const index = try f.allocLocal(inst, .usize); | | |
| 6867 | | | |
| 6868 | try w.writeAll("for ("); | | |
| 6869 | try f.writeCValue(w, index, .Other); | | |
| 6870 | try w.writeAll(" = "); | | |
| 6871 | try f.object.dg.renderValue(w, .zero_usize, .Other); | | |
| 6872 | try w.writeAll("; "); | | |
| 6873 | try f.writeCValue(w, index, .Other); | | |
| 6874 | try w.writeAll(" != "); | | |
| 6875 | switch (dest_ty.ptrSize(zcu)) { | 6018 | switch (dest_ty.ptrSize(zcu)) { |
| 6876 | .slice => { | 6019 | .slice => { |
| | 6020 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); |
| | 6021 | try w.writeAll(", "); |
| | 6022 | try f.writeCValue(w, bitcasted, .other); |
| | 6023 | try w.writeAll(", "); |
| 6877 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); | 6024 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 6878 | }, | 6025 | }, |
| 6879 | .one => { | 6026 | .one => { |
| 6880 | const array_ty = dest_ty.childType(zcu); | 6027 | try f.writeCValue(w, dest_slice, .other); |
| 6881 | try w.print("{d}", .{array_ty.arrayLen(zcu)}); | 6028 | try w.writeAll(", "); |
| | 6029 | try f.writeCValue(w, bitcasted, .other); |
| | 6030 | try w.print(", {d}", .{dest_ty.childType(zcu).arrayLen(zcu)}); |
| 6882 | }, | 6031 | }, |
| 6883 | .many, .c => unreachable, | 6032 | .many, .c => unreachable, |
| 6884 | } | 6033 | } |
| 6885 | try w.writeAll("; ++"); | 6034 | try w.writeAll(");"); |
| 6886 | try f.writeCValue(w, index, .Other); | 6035 | try f.newline(); |
| 6887 | try w.writeAll(") "); | 6036 | try f.freeCValue(inst, bitcasted); |
| 6888 | | | |
| 6889 | const a = try Assignment.start(f, w, try f.ctypeFromType(elem_ty, .complete)); | | |
| 6890 | try w.writeAll("(("); | | |
| 6891 | try f.renderType(w, elem_ptr_ty); | | |
| 6892 | try w.writeByte(')'); | | |
| 6893 | try writeSliceOrPtr(f, w, dest_slice, dest_ty); | | |
| 6894 | try w.writeAll(")["); | | |
| 6895 | try f.writeCValue(w, index, .Other); | | |
| 6896 | try w.writeByte(']'); | | |
| 6897 | try a.assign(f, w); | | |
| 6898 | try f.writeCValue(w, value, .Other); | | |
| 6899 | try a.end(f, w); | | |
| 6900 | | | |
| 6901 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 6037 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6902 | try freeLocal(f, inst, index.new_local, null); | | |
| 6903 | | | |
| 6904 | return .none; | 6038 | return .none; |
| 6905 | } | 6039 | } |
| 6906 | | 6040 | |
| 6907 | const bitcasted = try bitcast(f, .u8, value, elem_ty); | 6041 | // Fallback path: use a `for` loop. |
| | 6042 | |
| | 6043 | const index = try f.allocLocal(inst, .usize); |
| 6908 | | 6044 | |
| 6909 | try w.writeAll("memset("); | 6045 | try w.writeAll("for ("); |
| | 6046 | try f.writeCValue(w, index, .other); |
| | 6047 | try w.writeAll(" = "); |
| | 6048 | try f.dg.renderValue(w, .zero_usize, .other); |
| | 6049 | try w.writeAll("; "); |
| | 6050 | try f.writeCValue(w, index, .other); |
| | 6051 | try w.writeAll(" != "); |
| 6910 | switch (dest_ty.ptrSize(zcu)) { | 6052 | switch (dest_ty.ptrSize(zcu)) { |
| 6911 | .slice => { | 6053 | .slice => try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }), |
| 6912 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); | 6054 | .one => try w.print("{d}", .{dest_ty.childType(zcu).arrayLen(zcu)}), |
| 6913 | try w.writeAll(", "); | 6055 | .many, .c => unreachable, |
| 6914 | try f.writeCValue(w, bitcasted, .FunctionArgument); | 6056 | } |
| 6915 | try w.writeAll(", "); | 6057 | try w.writeAll("; ++"); |
| 6916 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); | 6058 | try f.writeCValue(w, index, .other); |
| 6917 | try w.writeAll(");"); | 6059 | try w.writeAll(") "); |
| 6918 | try f.object.newline(); | | |
| 6919 | }, | | |
| 6920 | .one => { | | |
| 6921 | const array_ty = dest_ty.childType(zcu); | | |
| 6922 | const len = array_ty.arrayLen(zcu) * elem_abi_size; | | |
| 6923 | | 6060 | |
| 6924 | try f.writeCValue(w, dest_slice, .FunctionArgument); | 6061 | switch (dest_ty.ptrSize(zcu)) { |
| 6925 | try w.writeAll(", "); | 6062 | .slice => try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }), |
| 6926 | try f.writeCValue(w, bitcasted, .FunctionArgument); | 6063 | .one => try f.writeCValueDerefMember(w, dest_slice, .{ .identifier = "array" }), |
| 6927 | try w.print(", {d});", .{len}); | | |
| 6928 | try f.object.newline(); | | |
| 6929 | }, | | |
| 6930 | .many, .c => unreachable, | 6064 | .many, .c => unreachable, |
| 6931 | } | 6065 | } |
| 6932 | try f.freeCValue(inst, bitcasted); | 6066 | try w.writeByte('['); |
| | 6067 | try f.writeCValue(w, index, .other); |
| | 6068 | try w.writeAll("] = "); |
| | 6069 | try f.writeCValue(w, value, .other); |
| | 6070 | try w.writeByte(';'); |
| | 6071 | try f.newline(); |
| | 6072 | |
| 6933 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 6073 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 6074 | try freeLocal(f, inst, index.new_local, null); |
| | 6075 | |
| 6934 | return .none; | 6076 | return .none; |
| 6935 | } | 6077 | } |
| 6936 | | 6078 | |
| 6937 | fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CValue { | 6079 | fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CValue { |
| 6938 | const pt = f.object.dg.pt; | 6080 | const pt = f.dg.pt; |
| 6939 | const zcu = pt.zcu; | 6081 | const zcu = pt.zcu; |
| 6940 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6082 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6941 | const dest_ptr = try f.resolveInst(bin_op.lhs); | 6083 | const dest_ptr = try f.resolveInst(bin_op.lhs); |
| 6942 | const src_ptr = try f.resolveInst(bin_op.rhs); | 6084 | const src_ptr = try f.resolveInst(bin_op.rhs); |
| 6943 | const dest_ty = f.typeOf(bin_op.lhs); | 6085 | const dest_ty = f.typeOf(bin_op.lhs); |
| 6944 | const src_ty = f.typeOf(bin_op.rhs); | 6086 | const src_ty = f.typeOf(bin_op.rhs); |
| 6945 | const w = &f.object.code.writer; | 6087 | const w = &f.code.writer; |
| 6946 | | 6088 | |
| 6947 | if (dest_ty.ptrSize(zcu) != .one) { | 6089 | if (dest_ty.ptrSize(zcu) != .one) { |
| 6948 | try w.writeAll("if ("); | 6090 | try w.writeAll("if ("); |
| 6949 | try writeArrayLen(f, dest_ptr, dest_ty); | 6091 | try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }); |
| 6950 | try w.writeAll(" != 0) "); | 6092 | try w.writeAll(" != 0) "); |
| 6951 | } | 6093 | } |
| 6952 | try w.writeAll(function_paren); | 6094 | try w.writeAll(function_paren); |
| 6953 | try writeSliceOrPtr(f, w, dest_ptr, dest_ty); | 6095 | switch (dest_ty.ptrSize(zcu)) { |
| | 6096 | .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "ptr" }), |
| | 6097 | .one => try f.writeCValueDerefMember(w, dest_ptr, .{ .identifier = "array" }), |
| | 6098 | .many, .c => unreachable, |
| | 6099 | } |
| 6954 | try w.writeAll(", "); | 6100 | try w.writeAll(", "); |
| 6955 | try writeSliceOrPtr(f, w, src_ptr, src_ty); | 6101 | switch (src_ty.ptrSize(zcu)) { |
| | 6102 | .slice => try f.writeCValueMember(w, src_ptr, .{ .identifier = "ptr" }), |
| | 6103 | .one => try f.writeCValueDerefMember(w, src_ptr, .{ .identifier = "array" }), |
| | 6104 | .many, .c => try f.writeCValue(w, src_ptr, .other), |
| | 6105 | } |
| 6956 | try w.writeAll(", "); | 6106 | try w.writeAll(", "); |
| 6957 | try writeArrayLen(f, dest_ptr, dest_ty); | 6107 | switch (dest_ty.ptrSize(zcu)) { |
| | 6108 | .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }), |
| | 6109 | .one => try w.print("{d}", .{dest_ty.childType(zcu).arrayLen(zcu)}), |
| | 6110 | .many, .c => unreachable, |
| | 6111 | } |
| 6958 | try w.writeAll(" * sizeof("); | 6112 | try w.writeAll(" * sizeof("); |
| 6959 | try f.renderType(w, dest_ty.indexableElem(zcu)); | 6113 | try f.renderType(w, dest_ty.indexableElem(zcu)); |
| 6960 | try w.writeAll("));"); | 6114 | try w.writeAll("));"); |
| 6961 | try f.object.newline(); | 6115 | try f.newline(); |
| 6962 | | 6116 | |
| 6963 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); | 6117 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6964 | return .none; | 6118 | return .none; |
| 6965 | } | 6119 | } |
| 6966 | | 6120 | |
| 6967 | fn writeArrayLen(f: *Function, dest_ptr: CValue, dest_ty: Type) !void { | | |
| 6968 | const pt = f.object.dg.pt; | | |
| 6969 | const zcu = pt.zcu; | | |
| 6970 | const w = &f.object.code.writer; | | |
| 6971 | switch (dest_ty.ptrSize(zcu)) { | | |
| 6972 | .one => try w.print("{f}", .{ | | |
| 6973 | try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), | | |
| 6974 | }), | | |
| 6975 | .many, .c => unreachable, | | |
| 6976 | .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }), | | |
| 6977 | } | | |
| 6978 | } | | |
| 6979 | | | |
| 6980 | fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | 6121 | fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6981 | const pt = f.object.dg.pt; | 6122 | const pt = f.dg.pt; |
| 6982 | const zcu = pt.zcu; | 6123 | const zcu = pt.zcu; |
| 6983 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; | 6124 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6984 | const union_ptr = try f.resolveInst(bin_op.lhs); | 6125 | const union_ptr = try f.resolveInst(bin_op.lhs); |
| ... | @@ -6988,19 +6129,18 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -6988,19 +6129,18 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6988 | const union_ty = f.typeOf(bin_op.lhs).childType(zcu); | 6129 | const union_ty = f.typeOf(bin_op.lhs).childType(zcu); |
| 6989 | const layout = union_ty.unionGetLayout(zcu); | 6130 | const layout = union_ty.unionGetLayout(zcu); |
| 6990 | if (layout.tag_size == 0) return .none; | 6131 | if (layout.tag_size == 0) return .none; |
| 6991 | const tag_ty = union_ty.unionTagTypeRuntime(zcu).?; | | |
| 6992 | | 6132 | |
| 6993 | const w = &f.object.code.writer; | 6133 | const w = &f.code.writer; |
| 6994 | const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete)); | | |
| 6995 | try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" }); | 6134 | try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" }); |
| 6996 | try a.assign(f, w); | 6135 | try w.writeAll(" = "); |
| 6997 | try f.writeCValue(w, new_tag, .Other); | 6136 | try f.writeCValue(w, new_tag, .other); |
| 6998 | try a.end(f, w); | 6137 | try w.writeByte(';'); |
| | 6138 | try f.newline(); |
| 6999 | return .none; | 6139 | return .none; |
| 7000 | } | 6140 | } |
| 7001 | | 6141 | |
| 7002 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | 6142 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7003 | const pt = f.object.dg.pt; | 6143 | const pt = f.dg.pt; |
| 7004 | const zcu = pt.zcu; | 6144 | const zcu = pt.zcu; |
| 7005 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6145 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7006 | | 6146 | |
| ... | @@ -7012,17 +6152,20 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7012,17 +6152,20 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7012 | if (layout.tag_size == 0) return .none; | 6152 | if (layout.tag_size == 0) return .none; |
| 7013 | | 6153 | |
| 7014 | const inst_ty = f.typeOfIndex(inst); | 6154 | const inst_ty = f.typeOfIndex(inst); |
| 7015 | const w = &f.object.code.writer; | 6155 | const w = &f.code.writer; |
| 7016 | const local = try f.allocLocal(inst, inst_ty); | 6156 | const local = try f.allocLocal(inst, inst_ty); |
| 7017 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); | 6157 | try f.writeCValue(w, local, .other); |
| 7018 | try f.writeCValue(w, local, .Other); | 6158 | try w.writeAll(" = "); |
| 7019 | try a.assign(f, w); | | |
| 7020 | try f.writeCValueMember(w, operand, .{ .identifier = "tag" }); | 6159 | try f.writeCValueMember(w, operand, .{ .identifier = "tag" }); |
| 7021 | try a.end(f, w); | 6160 | try w.writeByte(';'); |
| | 6161 | try f.newline(); |
| 7022 | return local; | 6162 | return local; |
| 7023 | } | 6163 | } |
| 7024 | | 6164 | |
| 7025 | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { | 6165 | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| | 6166 | const zcu = f.dg.pt.zcu; |
| | 6167 | const ip = &zcu.intern_pool; |
| | 6168 | const gpa = zcu.comp.gpa; |
| 7026 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 6169 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7027 | | 6170 | |
| 7028 | const inst_ty = f.typeOfIndex(inst); | 6171 | const inst_ty = f.typeOfIndex(inst); |
| ... | @@ -7030,15 +6173,17 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7030,15 +6173,17 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7030 | const operand = try f.resolveInst(un_op); | 6173 | const operand = try f.resolveInst(un_op); |
| 7031 | try reap(f, inst, &.{un_op}); | 6174 | try reap(f, inst, &.{un_op}); |
| 7032 | | 6175 | |
| 7033 | const w = &f.object.code.writer; | 6176 | const w = &f.code.writer; |
| 7034 | const local = try f.allocLocal(inst, inst_ty); | 6177 | const local = try f.allocLocal(inst, inst_ty); |
| 7035 | try f.writeCValue(w, local, .Other); | 6178 | try f.writeCValue(w, local, .other); |
| 7036 | try w.print(" = {s}(", .{ | 6179 | try f.need_tag_name_funcs.put(gpa, enum_ty.toIntern(), {}); |
| 7037 | try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }), | 6180 | try w.print(" = zig_tagName_{f}__{d}(", .{ |
| | 6181 | fmtIdentUnsolo(enum_ty.containerTypeName(ip).toSlice(ip)), |
| | 6182 | @intFromEnum(enum_ty.toIntern()), |
| 7038 | }); | 6183 | }); |
| 7039 | try f.writeCValue(w, operand, .Other); | 6184 | try f.writeCValue(w, operand, .other); |
| 7040 | try w.writeAll(");"); | 6185 | try w.writeAll(");"); |
| 7041 | try f.object.newline(); | 6186 | try f.newline(); |
| 7042 | | 6187 | |
| 7043 | return local; | 6188 | return local; |
| 7044 | } | 6189 | } |
| ... | @@ -7046,40 +6191,37 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7046,40 +6191,37 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7046 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { | 6191 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7047 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; | 6192 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7048 | | 6193 | |
| 7049 | const w = &f.object.code.writer; | 6194 | const w = &f.code.writer; |
| 7050 | const inst_ty = f.typeOfIndex(inst); | 6195 | const inst_ty = f.typeOfIndex(inst); |
| 7051 | const operand = try f.resolveInst(un_op); | 6196 | const operand = try f.resolveInst(un_op); |
| 7052 | try reap(f, inst, &.{un_op}); | 6197 | try reap(f, inst, &.{un_op}); |
| 7053 | const local = try f.allocLocal(inst, inst_ty); | 6198 | const local = try f.allocLocal(inst, inst_ty); |
| 7054 | try f.writeCValue(w, local, .Other); | 6199 | try f.writeCValue(w, local, .other); |
| 7055 | | 6200 | |
| 7056 | try w.writeAll(" = zig_errorName["); | 6201 | try w.writeAll(" = zig_errorName["); |
| 7057 | try f.writeCValue(w, operand, .Other); | 6202 | try f.writeCValue(w, operand, .other); |
| 7058 | try w.writeAll(" - 1];"); | 6203 | try w.writeAll(" - 1];"); |
| 7059 | try f.object.newline(); | 6204 | try f.newline(); |
| 7060 | return local; | 6205 | return local; |
| 7061 | } | 6206 | } |
| 7062 | | 6207 | |
| 7063 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { | 6208 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7064 | const pt = f.object.dg.pt; | | |
| 7065 | const zcu = pt.zcu; | | |
| 7066 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; | 6209 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7067 | | 6210 | |
| 7068 | const operand = try f.resolveInst(ty_op.operand); | 6211 | const operand = try f.resolveInst(ty_op.operand); |
| 7069 | try reap(f, inst, &.{ty_op.operand}); | 6212 | try reap(f, inst, &.{ty_op.operand}); |
| 7070 | | 6213 | |
| 7071 | const inst_ty = f.typeOfIndex(inst); | 6214 | const inst_ty = f.typeOfIndex(inst); |
| 7072 | const inst_scalar_ty = inst_ty.scalarType(zcu); | | |
| 7073 | | 6215 | |
| 7074 | const w = &f.object.code.writer; | 6216 | const w = &f.code.writer; |
| 7075 | const local = try f.allocLocal(inst, inst_ty); | 6217 | const local = try f.allocLocal(inst, inst_ty); |
| 7076 | const v = try Vectorize.start(f, inst, w, inst_ty); | 6218 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7077 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete)); | 6219 | try f.writeCValue(w, local, .other); |
| 7078 | try f.writeCValue(w, local, .Other); | | |
| 7079 | try v.elem(f, w); | 6220 | try v.elem(f, w); |
| 7080 | try a.assign(f, w); | 6221 | try w.writeAll(" = "); |
| 7081 | try f.writeCValue(w, operand, .Other); | 6222 | try f.writeCValue(w, operand, .other); |
| 7082 | try a.end(f, w); | 6223 | try w.writeByte(';'); |
| | 6224 | try f.newline(); |
| 7083 | try v.end(f, inst, w); | 6225 | try v.end(f, inst, w); |
| 7084 | | 6226 | |
| 7085 | return local; | 6227 | return local; |
| ... | @@ -7096,29 +6238,29 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7096,29 +6238,29 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7096 | | 6238 | |
| 7097 | const inst_ty = f.typeOfIndex(inst); | 6239 | const inst_ty = f.typeOfIndex(inst); |
| 7098 | | 6240 | |
| 7099 | const w = &f.object.code.writer; | 6241 | const w = &f.code.writer; |
| 7100 | const local = try f.allocLocal(inst, inst_ty); | 6242 | const local = try f.allocLocal(inst, inst_ty); |
| 7101 | const v = try Vectorize.start(f, inst, w, inst_ty); | 6243 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7102 | try f.writeCValue(w, local, .Other); | 6244 | try f.writeCValue(w, local, .other); |
| 7103 | try v.elem(f, w); | 6245 | try v.elem(f, w); |
| 7104 | try w.writeAll(" = "); | 6246 | try w.writeAll(" = "); |
| 7105 | try f.writeCValue(w, pred, .Other); | 6247 | try f.writeCValue(w, pred, .other); |
| 7106 | try v.elem(f, w); | 6248 | try v.elem(f, w); |
| 7107 | try w.writeAll(" ? "); | 6249 | try w.writeAll(" ? "); |
| 7108 | try f.writeCValue(w, lhs, .Other); | 6250 | try f.writeCValue(w, lhs, .other); |
| 7109 | try v.elem(f, w); | 6251 | try v.elem(f, w); |
| 7110 | try w.writeAll(" : "); | 6252 | try w.writeAll(" : "); |
| 7111 | try f.writeCValue(w, rhs, .Other); | 6253 | try f.writeCValue(w, rhs, .other); |
| 7112 | try v.elem(f, w); | 6254 | try v.elem(f, w); |
| 7113 | try w.writeByte(';'); | 6255 | try w.writeByte(';'); |
| 7114 | try f.object.newline(); | 6256 | try f.newline(); |
| 7115 | try v.end(f, inst, w); | 6257 | try v.end(f, inst, w); |
| 7116 | | 6258 | |
| 7117 | return local; | 6259 | return local; |
| 7118 | } | 6260 | } |
| 7119 | | 6261 | |
| 7120 | fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { | 6262 | fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7121 | const pt = f.object.dg.pt; | 6263 | const pt = f.dg.pt; |
| 7122 | const zcu = pt.zcu; | 6264 | const zcu = pt.zcu; |
| 7123 | | 6265 | |
| 7124 | const unwrapped = f.air.unwrapShuffleOne(zcu, inst); | 6266 | const unwrapped = f.air.unwrapShuffleOne(zcu, inst); |
| ... | @@ -7126,22 +6268,22 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7126,22 +6268,22 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7126 | const operand = try f.resolveInst(unwrapped.operand); | 6268 | const operand = try f.resolveInst(unwrapped.operand); |
| 7127 | const inst_ty = unwrapped.result_ty; | 6269 | const inst_ty = unwrapped.result_ty; |
| 7128 | | 6270 | |
| 7129 | const w = &f.object.code.writer; | 6271 | const w = &f.code.writer; |
| 7130 | const local = try f.allocLocal(inst, inst_ty); | 6272 | const local = try f.allocLocal(inst, inst_ty); |
| 7131 | try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand | 6273 | try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand |
| 7132 | for (mask, 0..) |mask_elem, out_idx| { | 6274 | for (mask, 0..) |mask_elem, out_idx| { |
| 7133 | try f.writeCValue(w, local, .Other); | 6275 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7134 | try w.writeByte('['); | 6276 | try w.writeByte('['); |
| 7135 | try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other); | 6277 | try f.dg.renderValue(w, try pt.intValue(.usize, out_idx), .other); |
| 7136 | try w.writeAll("] = "); | 6278 | try w.writeAll("] = "); |
| 7137 | switch (mask_elem.unwrap()) { | 6279 | switch (mask_elem.unwrap()) { |
| 7138 | .elem => |src_idx| { | 6280 | .elem => |src_idx| { |
| 7139 | try f.writeCValue(w, operand, .Other); | 6281 | try f.writeCValueMember(w, operand, .{ .identifier = "array" }); |
| 7140 | try w.writeByte('['); | 6282 | try w.writeByte('['); |
| 7141 | try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other); | 6283 | try f.dg.renderValue(w, try pt.intValue(.usize, src_idx), .other); |
| 7142 | try w.writeByte(']'); | 6284 | try w.writeByte(']'); |
| 7143 | }, | 6285 | }, |
| 7144 | .value => |val| try f.object.dg.renderValue(w, .fromInterned(val), .Other), | 6286 | .value => |val| try f.dg.renderValue(w, .fromInterned(val), .other), |
| 7145 | } | 6287 | } |
| 7146 | try w.writeAll(";\n"); | 6288 | try w.writeAll(";\n"); |
| 7147 | } | 6289 | } |
| ... | @@ -7150,7 +6292,7 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7150,7 +6292,7 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7150 | } | 6292 | } |
| 7151 | | 6293 | |
| 7152 | fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { | 6294 | fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7153 | const pt = f.object.dg.pt; | 6295 | const pt = f.dg.pt; |
| 7154 | const zcu = pt.zcu; | 6296 | const zcu = pt.zcu; |
| 7155 | | 6297 | |
| 7156 | const unwrapped = f.air.unwrapShuffleTwo(zcu, inst); | 6298 | const unwrapped = f.air.unwrapShuffleTwo(zcu, inst); |
| ... | @@ -7160,38 +6302,38 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7160,38 +6302,38 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7160 | const inst_ty = unwrapped.result_ty; | 6302 | const inst_ty = unwrapped.result_ty; |
| 7161 | const elem_ty = inst_ty.childType(zcu); | 6303 | const elem_ty = inst_ty.childType(zcu); |
| 7162 | | 6304 | |
| 7163 | const w = &f.object.code.writer; | 6305 | const w = &f.code.writer; |
| 7164 | const local = try f.allocLocal(inst, inst_ty); | 6306 | const local = try f.allocLocal(inst, inst_ty); |
| 7165 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands | 6307 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands |
| 7166 | for (mask, 0..) |mask_elem, out_idx| { | 6308 | for (mask, 0..) |mask_elem, out_idx| { |
| 7167 | try f.writeCValue(w, local, .Other); | 6309 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7168 | try w.writeByte('['); | 6310 | try w.writeByte('['); |
| 7169 | try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other); | 6311 | try f.dg.renderValue(w, try pt.intValue(.usize, out_idx), .other); |
| 7170 | try w.writeAll("] = "); | 6312 | try w.writeAll("] = "); |
| 7171 | switch (mask_elem.unwrap()) { | 6313 | switch (mask_elem.unwrap()) { |
| 7172 | .a_elem => |src_idx| { | 6314 | .a_elem => |src_idx| { |
| 7173 | try f.writeCValue(w, operand_a, .Other); | 6315 | try f.writeCValueMember(w, operand_a, .{ .identifier = "array" }); |
| 7174 | try w.writeByte('['); | 6316 | try w.writeByte('['); |
| 7175 | try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other); | 6317 | try f.dg.renderValue(w, try pt.intValue(.usize, src_idx), .other); |
| 7176 | try w.writeByte(']'); | 6318 | try w.writeByte(']'); |
| 7177 | }, | 6319 | }, |
| 7178 | .b_elem => |src_idx| { | 6320 | .b_elem => |src_idx| { |
| 7179 | try f.writeCValue(w, operand_b, .Other); | 6321 | try f.writeCValueMember(w, operand_b, .{ .identifier = "array" }); |
| 7180 | try w.writeByte('['); | 6322 | try w.writeByte('['); |
| 7181 | try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other); | 6323 | try f.dg.renderValue(w, try pt.intValue(.usize, src_idx), .other); |
| 7182 | try w.writeByte(']'); | 6324 | try w.writeByte(']'); |
| 7183 | }, | 6325 | }, |
| 7184 | .undef => try f.object.dg.renderUndefValue(w, elem_ty, .Other), | 6326 | .undef => try f.dg.renderUndefValue(w, elem_ty, .other), |
| 7185 | } | 6327 | } |
| 7186 | try w.writeByte(';'); | 6328 | try w.writeByte(';'); |
| 7187 | try f.object.newline(); | 6329 | try f.newline(); |
| 7188 | } | 6330 | } |
| 7189 | | 6331 | |
| 7190 | return local; | 6332 | return local; |
| 7191 | } | 6333 | } |
| 7192 | | 6334 | |
| 7193 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | 6335 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7194 | const pt = f.object.dg.pt; | 6336 | const pt = f.dg.pt; |
| 7195 | const zcu = pt.zcu; | 6337 | const zcu = pt.zcu; |
| 7196 | const reduce = f.air.instructions.items(.data)[@intFromEnum(inst)].reduce; | 6338 | const reduce = f.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 7197 | | 6339 | |
| ... | @@ -7199,7 +6341,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7199,7 +6341,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7199 | const operand = try f.resolveInst(reduce.operand); | 6341 | const operand = try f.resolveInst(reduce.operand); |
| 7200 | try reap(f, inst, &.{reduce.operand}); | 6342 | try reap(f, inst, &.{reduce.operand}); |
| 7201 | const operand_ty = f.typeOf(reduce.operand); | 6343 | const operand_ty = f.typeOf(reduce.operand); |
| 7202 | const w = &f.object.code.writer; | 6344 | const w = &f.code.writer; |
| 7203 | | 6345 | |
| 7204 | const use_operator = scalar_ty.bitSize(zcu) <= 64; | 6346 | const use_operator = scalar_ty.bitSize(zcu) <= 64; |
| 7205 | const op: union(enum) { | 6347 | const op: union(enum) { |
| ... | @@ -7246,10 +6388,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7246,10 +6388,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7246 | // } | 6388 | // } |
| 7247 | | 6389 | |
| 7248 | const accum = try f.allocLocal(inst, scalar_ty); | 6390 | const accum = try f.allocLocal(inst, scalar_ty); |
| 7249 | try f.writeCValue(w, accum, .Other); | 6391 | try f.writeCValue(w, accum, .other); |
| 7250 | try w.writeAll(" = "); | 6392 | try w.writeAll(" = "); |
| 7251 | | 6393 | |
| 7252 | try f.object.dg.renderValue(w, switch (reduce.operation) { | 6394 | try f.dg.renderValue(w, switch (reduce.operation) { |
| 7253 | .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) { | 6395 | .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) { |
| 7254 | .bool => Value.false, | 6396 | .bool => Value.false, |
| 7255 | .int => try pt.intValue(scalar_ty, 0), | 6397 | .int => try pt.intValue(scalar_ty, 0), |
| ... | @@ -7285,58 +6427,58 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7285,58 +6427,58 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7285 | .float => try pt.floatValue(scalar_ty, std.math.nan(f128)), | 6427 | .float => try pt.floatValue(scalar_ty, std.math.nan(f128)), |
| 7286 | else => unreachable, | 6428 | else => unreachable, |
| 7287 | }, | 6429 | }, |
| 7288 | }, .Other); | 6430 | }, .other); |
| 7289 | try w.writeByte(';'); | 6431 | try w.writeByte(';'); |
| 7290 | try f.object.newline(); | 6432 | try f.newline(); |
| 7291 | | 6433 | |
| 7292 | const v = try Vectorize.start(f, inst, w, operand_ty); | 6434 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 7293 | try f.writeCValue(w, accum, .Other); | 6435 | try f.writeCValue(w, accum, .other); |
| 7294 | switch (op) { | 6436 | switch (op) { |
| 7295 | .builtin => |func| { | 6437 | .builtin => |func| { |
| 7296 | try w.print(" = zig_{s}_", .{func.operation}); | 6438 | try w.print(" = zig_{s}_", .{func.operation}); |
| 7297 | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); | 6439 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 7298 | try w.writeByte('('); | 6440 | try w.writeByte('('); |
| 7299 | try f.writeCValue(w, accum, .FunctionArgument); | 6441 | try f.writeCValue(w, accum, .other); |
| 7300 | try w.writeAll(", "); | 6442 | try w.writeAll(", "); |
| 7301 | try f.writeCValue(w, operand, .Other); | 6443 | try f.writeCValue(w, operand, .other); |
| 7302 | try v.elem(f, w); | 6444 | try v.elem(f, w); |
| 7303 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, func.info); | 6445 | try f.dg.renderBuiltinInfo(w, scalar_ty, func.info); |
| 7304 | try w.writeByte(')'); | 6446 | try w.writeByte(')'); |
| 7305 | }, | 6447 | }, |
| 7306 | .infix => |ass| { | 6448 | .infix => |ass| { |
| 7307 | try w.writeAll(ass); | 6449 | try w.writeAll(ass); |
| 7308 | try f.writeCValue(w, operand, .Other); | 6450 | try f.writeCValue(w, operand, .other); |
| 7309 | try v.elem(f, w); | 6451 | try v.elem(f, w); |
| 7310 | }, | 6452 | }, |
| 7311 | .ternary => |cmp| { | 6453 | .ternary => |cmp| { |
| 7312 | try w.writeAll(" = "); | 6454 | try w.writeAll(" = "); |
| 7313 | try f.writeCValue(w, accum, .Other); | 6455 | try f.writeCValue(w, accum, .other); |
| 7314 | try w.writeAll(cmp); | 6456 | try w.writeAll(cmp); |
| 7315 | try f.writeCValue(w, operand, .Other); | 6457 | try f.writeCValue(w, operand, .other); |
| 7316 | try v.elem(f, w); | 6458 | try v.elem(f, w); |
| 7317 | try w.writeAll(" ? "); | 6459 | try w.writeAll(" ? "); |
| 7318 | try f.writeCValue(w, accum, .Other); | 6460 | try f.writeCValue(w, accum, .other); |
| 7319 | try w.writeAll(" : "); | 6461 | try w.writeAll(" : "); |
| 7320 | try f.writeCValue(w, operand, .Other); | 6462 | try f.writeCValue(w, operand, .other); |
| 7321 | try v.elem(f, w); | 6463 | try v.elem(f, w); |
| 7322 | }, | 6464 | }, |
| 7323 | } | 6465 | } |
| 7324 | try w.writeByte(';'); | 6466 | try w.writeByte(';'); |
| 7325 | try f.object.newline(); | 6467 | try f.newline(); |
| 7326 | try v.end(f, inst, w); | 6468 | try v.end(f, inst, w); |
| 7327 | | 6469 | |
| 7328 | return accum; | 6470 | return accum; |
| 7329 | } | 6471 | } |
| 7330 | | 6472 | |
| 7331 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | 6473 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7332 | const pt = f.object.dg.pt; | 6474 | const pt = f.dg.pt; |
| 7333 | const zcu = pt.zcu; | 6475 | const zcu = pt.zcu; |
| 7334 | const ip = &zcu.intern_pool; | 6476 | const ip = &zcu.intern_pool; |
| 7335 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6477 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7336 | const inst_ty = f.typeOfIndex(inst); | 6478 | const inst_ty = f.typeOfIndex(inst); |
| 7337 | const len: usize = @intCast(inst_ty.arrayLen(zcu)); | 6479 | const len: usize = @intCast(inst_ty.arrayLen(zcu)); |
| 7338 | const elements: []const Air.Inst.Ref = @ptrCast(f.air.extra.items[ty_pl.payload..][0..len]); | 6480 | const elements: []const Air.Inst.Ref = @ptrCast(f.air.extra.items[ty_pl.payload..][0..len]); |
| 7339 | const gpa = f.object.dg.gpa; | 6481 | const gpa = f.dg.gpa; |
| 7340 | const resolved_elements = try gpa.alloc(CValue, elements.len); | 6482 | const resolved_elements = try gpa.alloc(CValue, elements.len); |
| 7341 | defer gpa.free(resolved_elements); | 6483 | defer gpa.free(resolved_elements); |
| 7342 | for (resolved_elements, elements) |*resolved_element, element| { | 6484 | for (resolved_elements, elements) |*resolved_element, element| { |
| ... | @@ -7349,28 +6491,23 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7349,28 +6491,23 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7349 | } | 6491 | } |
| 7350 | } | 6492 | } |
| 7351 | | 6493 | |
| 7352 | const w = &f.object.code.writer; | 6494 | const w = &f.code.writer; |
| 7353 | const local = try f.allocLocal(inst, inst_ty); | 6495 | const local = try f.allocLocal(inst, inst_ty); |
| 7354 | switch (ip.indexToKey(inst_ty.toIntern())) { | 6496 | switch (ip.indexToKey(inst_ty.toIntern())) { |
| 7355 | inline .array_type, .vector_type => |info, tag| { | 6497 | inline .array_type, .vector_type => |info, tag| { |
| 7356 | const a: Assignment = .{ | | |
| 7357 | .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete), | | |
| 7358 | }; | | |
| 7359 | for (resolved_elements, 0..) |element, i| { | 6498 | for (resolved_elements, 0..) |element, i| { |
| 7360 | try a.restart(f, w); | 6499 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7361 | try f.writeCValue(w, local, .Other); | 6500 | try w.print("[{d}] = ", .{i}); |
| 7362 | try w.print("[{d}]", .{i}); | 6501 | try f.writeCValue(w, element, .other); |
| 7363 | try a.assign(f, w); | 6502 | try w.writeByte(';'); |
| 7364 | try f.writeCValue(w, element, .Other); | 6503 | try f.newline(); |
| 7365 | try a.end(f, w); | | |
| 7366 | } | 6504 | } |
| 7367 | if (tag == .array_type and info.sentinel != .none) { | 6505 | if (tag == .array_type and info.sentinel != .none) { |
| 7368 | try a.restart(f, w); | 6506 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7369 | try f.writeCValue(w, local, .Other); | 6507 | try w.print("[{d}] = ", .{info.len}); |
| 7370 | try w.print("[{d}]", .{info.len}); | 6508 | try f.dg.renderValue(w, Value.fromInterned(info.sentinel), .other); |
| 7371 | try a.assign(f, w); | 6509 | try w.writeByte(';'); |
| 7372 | try f.object.dg.renderValue(w, Value.fromInterned(info.sentinel), .Other); | 6510 | try f.newline(); |
| 7373 | try a.end(f, w); | | |
| 7374 | } | 6511 | } |
| 7375 | }, | 6512 | }, |
| 7376 | .struct_type => { | 6513 | .struct_type => { |
| ... | @@ -7382,11 +6519,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7382,11 +6519,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7382 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); | 6519 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 7383 | if (!field_ty.hasRuntimeBits(zcu)) continue; | 6520 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 7384 | | 6521 | |
| 7385 | const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete)); | | |
| 7386 | try f.writeCValueMember(w, local, .{ .identifier = loaded_struct.field_names.get(ip)[field_index].toSlice(ip) }); | 6522 | try f.writeCValueMember(w, local, .{ .identifier = loaded_struct.field_names.get(ip)[field_index].toSlice(ip) }); |
| 7387 | try a.assign(f, w); | 6523 | try w.writeAll(" = "); |
| 7388 | try f.writeCValue(w, resolved_elements[field_index], .Other); | 6524 | try f.writeCValue(w, resolved_elements[field_index], .other); |
| 7389 | try a.end(f, w); | 6525 | try w.writeByte(';'); |
| | 6526 | try f.newline(); |
| 7390 | } | 6527 | } |
| 7391 | }, | 6528 | }, |
| 7392 | .@"packed" => unreachable, // `Air.Legalize.Feature.expand_packed_struct_init` handles this case | 6529 | .@"packed" => unreachable, // `Air.Legalize.Feature.expand_packed_struct_init` handles this case |
| ... | @@ -7397,11 +6534,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7397,11 +6534,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7397 | const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]); | 6534 | const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]); |
| 7398 | if (!field_ty.hasRuntimeBits(zcu)) continue; | 6535 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 7399 | | 6536 | |
| 7400 | const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete)); | | |
| 7401 | try f.writeCValueMember(w, local, .{ .field = field_index }); | 6537 | try f.writeCValueMember(w, local, .{ .field = field_index }); |
| 7402 | try a.assign(f, w); | 6538 | try w.writeAll(" = "); |
| 7403 | try f.writeCValue(w, resolved_elements[field_index], .Other); | 6539 | try f.writeCValue(w, resolved_elements[field_index], .other); |
| 7404 | try a.end(f, w); | 6540 | try w.writeByte(';'); |
| | 6541 | try f.newline(); |
| 7405 | }, | 6542 | }, |
| 7406 | else => unreachable, | 6543 | else => unreachable, |
| 7407 | } | 6544 | } |
| ... | @@ -7410,46 +6547,52 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7410,46 +6547,52 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7410 | } | 6547 | } |
| 7411 | | 6548 | |
| 7412 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { | 6549 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7413 | const pt = f.object.dg.pt; | 6550 | const pt = f.dg.pt; |
| 7414 | const zcu = pt.zcu; | 6551 | const zcu = pt.zcu; |
| 7415 | const ip = &zcu.intern_pool; | 6552 | const ip = &zcu.intern_pool; |
| 7416 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 6553 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7417 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; | 6554 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| | 6555 | const field_index = extra.field_index; |
| 7418 | | 6556 | |
| 7419 | const union_ty = f.typeOfIndex(inst); | 6557 | const union_ty = f.typeOfIndex(inst); |
| 7420 | const loaded_union = ip.loadUnionType(union_ty.toIntern()); | 6558 | const loaded_union = ip.loadUnionType(union_ty.toIntern()); |
| 7421 | const field_name = ip.loadEnumType(loaded_union.enum_tag_type).field_names.get(ip)[extra.field_index]; | 6559 | const loaded_enum = ip.loadEnumType(loaded_union.enum_tag_type); |
| 7422 | const payload_ty = f.typeOf(extra.init); | 6560 | |
| 7423 | const payload = try f.resolveInst(extra.init); | 6561 | const payload = try f.resolveInst(extra.init); |
| 7424 | try reap(f, inst, &.{extra.init}); | 6562 | try reap(f, inst, &.{extra.init}); |
| 7425 | | 6563 | |
| 7426 | const w = &f.object.code.writer; | 6564 | const w = &f.code.writer; |
| 7427 | if (loaded_union.layout == .@"packed") return f.moveCValue(inst, union_ty, payload); | 6565 | if (loaded_union.layout == .@"packed") return f.moveCValue(inst, union_ty, payload); |
| 7428 | | 6566 | |
| 7429 | const local = try f.allocLocal(inst, union_ty); | 6567 | const local = try f.allocLocal(inst, union_ty); |
| 7430 | | 6568 | |
| 7431 | const field: CValue = if (union_ty.unionTagTypeRuntime(zcu)) |tag_ty| field: { | 6569 | if (loaded_union.has_runtime_tag) { |
| 7432 | assert(union_ty.unionGetLayout(zcu).tag_size != 0); | | |
| 7433 | const field_index = tag_ty.enumFieldIndex(field_name, zcu).?; | | |
| 7434 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | | |
| 7435 | const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete)); | | |
| 7436 | try f.writeCValueMember(w, local, .{ .identifier = "tag" }); | 6570 | try f.writeCValueMember(w, local, .{ .identifier = "tag" }); |
| 7437 | try a.assign(f, w); | 6571 | if (loaded_enum.field_values.len == 0) { |
| 7438 | try w.print("{f}", .{try f.fmtIntLiteralDec(tag_val.intFromEnum(zcu))}); | 6572 | // auto-numbered |
| 7439 | try a.end(f, w); | 6573 | try w.print(" = {d};", .{field_index}); |
| 7440 | break :field .{ .payload_identifier = field_name.toSlice(ip) }; | 6574 | } else { |
| 7441 | } else .{ .identifier = field_name.toSlice(ip) }; | 6575 | const tag_int_val: Value = .fromInterned(loaded_enum.field_values.get(ip)[field_index]); |
| 7442 | | 6576 | try w.print(" = {f};", .{try f.fmtIntLiteralDec(tag_int_val)}); |
| 7443 | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); | 6577 | } |
| 7444 | try f.writeCValueMember(w, local, field); | 6578 | try f.newline(); |
| 7445 | try a.assign(f, w); | 6579 | } |
| 7446 | try f.writeCValue(w, payload, .Other); | 6580 | |
| 7447 | try a.end(f, w); | 6581 | const field_name_slice = loaded_enum.field_names.get(ip)[field_index].toSlice(ip); |
| | 6582 | switch (loaded_union.layout) { |
| | 6583 | .auto => try f.writeCValueMember(w, local, .{ .payload_identifier = field_name_slice }), |
| | 6584 | .@"extern" => try f.writeCValueMember(w, local, .{ .identifier = field_name_slice }), |
| | 6585 | .@"packed" => unreachable, |
| | 6586 | } |
| | 6587 | try w.writeAll(" = "); |
| | 6588 | try f.writeCValue(w, payload, .other); |
| | 6589 | try w.writeByte(';'); |
| | 6590 | try f.newline(); |
| 7448 | return local; | 6591 | return local; |
| 7449 | } | 6592 | } |
| 7450 | | 6593 | |
| 7451 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { | 6594 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7452 | const pt = f.object.dg.pt; | 6595 | const pt = f.dg.pt; |
| 7453 | const zcu = pt.zcu; | 6596 | const zcu = pt.zcu; |
| 7454 | const prefetch = f.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; | 6597 | const prefetch = f.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 7455 | | 6598 | |
| ... | @@ -7457,16 +6600,16 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7457,16 +6600,16 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7457 | const ptr = try f.resolveInst(prefetch.ptr); | 6600 | const ptr = try f.resolveInst(prefetch.ptr); |
| 7458 | try reap(f, inst, &.{prefetch.ptr}); | 6601 | try reap(f, inst, &.{prefetch.ptr}); |
| 7459 | | 6602 | |
| 7460 | const w = &f.object.code.writer; | 6603 | const w = &f.code.writer; |
| 7461 | switch (prefetch.cache) { | 6604 | switch (prefetch.cache) { |
| 7462 | .data => { | 6605 | .data => { |
| 7463 | try w.writeAll("zig_prefetch("); | 6606 | try w.writeAll("zig_prefetch("); |
| 7464 | if (ptr_ty.isSlice(zcu)) | 6607 | if (ptr_ty.isSlice(zcu)) |
| 7465 | try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" }) | 6608 | try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" }) |
| 7466 | else | 6609 | else |
| 7467 | try f.writeCValue(w, ptr, .FunctionArgument); | 6610 | try f.writeCValue(w, ptr, .other); |
| 7468 | try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality }); | 6611 | try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality }); |
| 7469 | try f.object.newline(); | 6612 | try f.newline(); |
| 7470 | }, | 6613 | }, |
| 7471 | // The available prefetch intrinsics do not accept a cache argument; only | 6614 | // The available prefetch intrinsics do not accept a cache argument; only |
| 7472 | // address, rw, and locality. | 6615 | // address, rw, and locality. |
| ... | @@ -7479,14 +6622,14 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7479,14 +6622,14 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7479 | fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { | 6622 | fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7480 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 6623 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7481 | | 6624 | |
| 7482 | const w = &f.object.code.writer; | 6625 | const w = &f.code.writer; |
| 7483 | const inst_ty = f.typeOfIndex(inst); | 6626 | const inst_ty = f.typeOfIndex(inst); |
| 7484 | const local = try f.allocLocal(inst, inst_ty); | 6627 | const local = try f.allocLocal(inst, inst_ty); |
| 7485 | try f.writeCValue(w, local, .Other); | 6628 | try f.writeCValue(w, local, .other); |
| 7486 | | 6629 | |
| 7487 | try w.writeAll(" = "); | 6630 | try w.writeAll(" = "); |
| 7488 | try w.print("zig_wasm_memory_size({d});", .{pl_op.payload}); | 6631 | try w.print("zig_wasm_memory_size({d});", .{pl_op.payload}); |
| 7489 | try f.object.newline(); | 6632 | try f.newline(); |
| 7490 | | 6633 | |
| 7491 | return local; | 6634 | return local; |
| 7492 | } | 6635 | } |
| ... | @@ -7494,23 +6637,23 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7494,23 +6637,23 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7494 | fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { | 6637 | fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7495 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 6638 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7496 | | 6639 | |
| 7497 | const w = &f.object.code.writer; | 6640 | const w = &f.code.writer; |
| 7498 | const inst_ty = f.typeOfIndex(inst); | 6641 | const inst_ty = f.typeOfIndex(inst); |
| 7499 | const operand = try f.resolveInst(pl_op.operand); | 6642 | const operand = try f.resolveInst(pl_op.operand); |
| 7500 | try reap(f, inst, &.{pl_op.operand}); | 6643 | try reap(f, inst, &.{pl_op.operand}); |
| 7501 | const local = try f.allocLocal(inst, inst_ty); | 6644 | const local = try f.allocLocal(inst, inst_ty); |
| 7502 | try f.writeCValue(w, local, .Other); | 6645 | try f.writeCValue(w, local, .other); |
| 7503 | | 6646 | |
| 7504 | try w.writeAll(" = "); | 6647 | try w.writeAll(" = "); |
| 7505 | try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); | 6648 | try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); |
| 7506 | try f.writeCValue(w, operand, .FunctionArgument); | 6649 | try f.writeCValue(w, operand, .other); |
| 7507 | try w.writeAll(");"); | 6650 | try w.writeAll(");"); |
| 7508 | try f.object.newline(); | 6651 | try f.newline(); |
| 7509 | return local; | 6652 | return local; |
| 7510 | } | 6653 | } |
| 7511 | | 6654 | |
| 7512 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { | 6655 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7513 | const pt = f.object.dg.pt; | 6656 | const pt = f.dg.pt; |
| 7514 | const zcu = pt.zcu; | 6657 | const zcu = pt.zcu; |
| 7515 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; | 6658 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7516 | const bin_op = f.air.extraData(Air.Bin, pl_op.payload).data; | 6659 | const bin_op = f.air.extraData(Air.Bin, pl_op.payload).data; |
| ... | @@ -7523,24 +6666,24 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7523,24 +6666,24 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7523 | const inst_ty = f.typeOfIndex(inst); | 6666 | const inst_ty = f.typeOfIndex(inst); |
| 7524 | const inst_scalar_ty = inst_ty.scalarType(zcu); | 6667 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 7525 | | 6668 | |
| 7526 | const w = &f.object.code.writer; | 6669 | const w = &f.code.writer; |
| 7527 | const local = try f.allocLocal(inst, inst_ty); | 6670 | const local = try f.allocLocal(inst, inst_ty); |
| 7528 | const v = try Vectorize.start(f, inst, w, inst_ty); | 6671 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7529 | try f.writeCValue(w, local, .Other); | 6672 | try f.writeCValue(w, local, .other); |
| 7530 | try v.elem(f, w); | 6673 | try v.elem(f, w); |
| 7531 | try w.writeAll(" = zig_fma_"); | 6674 | try w.writeAll(" = zig_fma_"); |
| 7532 | try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty); | 6675 | try f.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty); |
| 7533 | try w.writeByte('('); | 6676 | try w.writeByte('('); |
| 7534 | try f.writeCValue(w, mulend1, .FunctionArgument); | 6677 | try f.writeCValue(w, mulend1, .other); |
| 7535 | try v.elem(f, w); | 6678 | try v.elem(f, w); |
| 7536 | try w.writeAll(", "); | 6679 | try w.writeAll(", "); |
| 7537 | try f.writeCValue(w, mulend2, .FunctionArgument); | 6680 | try f.writeCValue(w, mulend2, .other); |
| 7538 | try v.elem(f, w); | 6681 | try v.elem(f, w); |
| 7539 | try w.writeAll(", "); | 6682 | try w.writeAll(", "); |
| 7540 | try f.writeCValue(w, addend, .FunctionArgument); | 6683 | try f.writeCValue(w, addend, .other); |
| 7541 | try v.elem(f, w); | 6684 | try v.elem(f, w); |
| 7542 | try w.writeAll(");"); | 6685 | try w.writeAll(");"); |
| 7543 | try f.object.newline(); | 6686 | try f.newline(); |
| 7544 | try v.end(f, inst, w); | 6687 | try v.end(f, inst, w); |
| 7545 | | 6688 | |
| 7546 | return local; | 6689 | return local; |
| ... | @@ -7548,34 +6691,33 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7548,34 +6691,33 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7548 | | 6691 | |
| 7549 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 6692 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7550 | const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; | 6693 | const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; |
| 7551 | const w = &f.object.code.writer; | 6694 | const w = &f.code.writer; |
| 7552 | const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); | 6695 | const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); |
| 7553 | try f.writeCValue(w, local, .Other); | 6696 | try f.writeCValue(w, local, .other); |
| 7554 | try w.writeAll(" = "); | 6697 | try w.writeAll(" = "); |
| 7555 | try f.object.dg.renderNav(w, ty_nav.nav, .Other); | 6698 | try f.dg.renderNav(w, ty_nav.nav, .other); |
| 7556 | try w.writeByte(';'); | 6699 | try w.writeByte(';'); |
| 7557 | try f.object.newline(); | 6700 | try f.newline(); |
| 7558 | return local; | 6701 | return local; |
| 7559 | } | 6702 | } |
| 7560 | | 6703 | |
| 7561 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { | 6704 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7562 | const pt = f.object.dg.pt; | 6705 | const pt = f.dg.pt; |
| 7563 | const zcu = pt.zcu; | 6706 | const zcu = pt.zcu; |
| 7564 | const inst_ty = f.typeOfIndex(inst); | 6707 | const inst_ty = f.typeOfIndex(inst); |
| 7565 | const function_ty = zcu.navValue(f.object.dg.pass.nav).typeOf(zcu); | | |
| 7566 | const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function; | | |
| 7567 | assert(function_info.varargs); | | |
| 7568 | | 6708 | |
| 7569 | const w = &f.object.code.writer; | 6709 | assert(Value.fromInterned(f.func_index).typeOf(zcu).fnIsVarArgs(zcu)); |
| | 6710 | |
| | 6711 | const w = &f.code.writer; |
| 7570 | const local = try f.allocLocal(inst, inst_ty); | 6712 | const local = try f.allocLocal(inst, inst_ty); |
| 7571 | try w.writeAll("va_start(*(va_list *)&"); | 6713 | try w.writeAll("va_start(*(va_list *)&"); |
| 7572 | try f.writeCValue(w, local, .Other); | 6714 | try f.writeCValue(w, local, .other); |
| 7573 | if (function_info.param_ctypes.len > 0) { | 6715 | if (f.next_arg_index > 0) { |
| 7574 | try w.writeAll(", "); | 6716 | try w.writeAll(", "); |
| 7575 | try f.writeCValue(w, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument); | 6717 | try f.writeCValue(w, .{ .arg = f.next_arg_index - 1 }, .other); |
| 7576 | } | 6718 | } |
| 7577 | try w.writeAll(");"); | 6719 | try w.writeAll(");"); |
| 7578 | try f.object.newline(); | 6720 | try f.newline(); |
| 7579 | return local; | 6721 | return local; |
| 7580 | } | 6722 | } |
| 7581 | | 6723 | |
| ... | @@ -7586,15 +6728,15 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7586,15 +6728,15 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7586 | const va_list = try f.resolveInst(ty_op.operand); | 6728 | const va_list = try f.resolveInst(ty_op.operand); |
| 7587 | try reap(f, inst, &.{ty_op.operand}); | 6729 | try reap(f, inst, &.{ty_op.operand}); |
| 7588 | | 6730 | |
| 7589 | const w = &f.object.code.writer; | 6731 | const w = &f.code.writer; |
| 7590 | const local = try f.allocLocal(inst, inst_ty); | 6732 | const local = try f.allocLocal(inst, inst_ty); |
| 7591 | try f.writeCValue(w, local, .Other); | 6733 | try f.writeCValue(w, local, .other); |
| 7592 | try w.writeAll(" = va_arg(*(va_list *)"); | 6734 | try w.writeAll(" = va_arg(*(va_list *)"); |
| 7593 | try f.writeCValue(w, va_list, .Other); | 6735 | try f.writeCValue(w, va_list, .other); |
| 7594 | try w.writeAll(", "); | 6736 | try w.writeAll(", "); |
| 7595 | try f.renderType(w, ty_op.ty.toType()); | 6737 | try f.renderType(w, ty_op.ty.toType()); |
| 7596 | try w.writeAll(");"); | 6738 | try w.writeAll(");"); |
| 7597 | try f.object.newline(); | 6739 | try f.newline(); |
| 7598 | return local; | 6740 | return local; |
| 7599 | } | 6741 | } |
| 7600 | | 6742 | |
| ... | @@ -7604,11 +6746,11 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7604,11 +6746,11 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7604 | const va_list = try f.resolveInst(un_op); | 6746 | const va_list = try f.resolveInst(un_op); |
| 7605 | try reap(f, inst, &.{un_op}); | 6747 | try reap(f, inst, &.{un_op}); |
| 7606 | | 6748 | |
| 7607 | const w = &f.object.code.writer; | 6749 | const w = &f.code.writer; |
| 7608 | try w.writeAll("va_end(*(va_list *)"); | 6750 | try w.writeAll("va_end(*(va_list *)"); |
| 7609 | try f.writeCValue(w, va_list, .Other); | 6751 | try f.writeCValue(w, va_list, .other); |
| 7610 | try w.writeAll(");"); | 6752 | try w.writeAll(");"); |
| 7611 | try f.object.newline(); | 6753 | try f.newline(); |
| 7612 | return .none; | 6754 | return .none; |
| 7613 | } | 6755 | } |
| 7614 | | 6756 | |
| ... | @@ -7619,14 +6761,14 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7619,14 +6761,14 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7619 | const va_list = try f.resolveInst(ty_op.operand); | 6761 | const va_list = try f.resolveInst(ty_op.operand); |
| 7620 | try reap(f, inst, &.{ty_op.operand}); | 6762 | try reap(f, inst, &.{ty_op.operand}); |
| 7621 | | 6763 | |
| 7622 | const w = &f.object.code.writer; | 6764 | const w = &f.code.writer; |
| 7623 | const local = try f.allocLocal(inst, inst_ty); | 6765 | const local = try f.allocLocal(inst, inst_ty); |
| 7624 | try w.writeAll("va_copy(*(va_list *)&"); | 6766 | try w.writeAll("va_copy(*(va_list *)&"); |
| 7625 | try f.writeCValue(w, local, .Other); | 6767 | try f.writeCValue(w, local, .other); |
| 7626 | try w.writeAll(", *(va_list *)"); | 6768 | try w.writeAll(", *(va_list *)"); |
| 7627 | try f.writeCValue(w, va_list, .Other); | 6769 | try f.writeCValue(w, va_list, .other); |
| 7628 | try w.writeAll(");"); | 6770 | try w.writeAll(");"); |
| 7629 | try f.object.newline(); | 6771 | try f.newline(); |
| 7630 | return local; | 6772 | return local; |
| 7631 | } | 6773 | } |
| 7632 | | 6774 | |
| ... | @@ -7943,103 +7085,193 @@ fn undefPattern(comptime IntType: type) IntType { | ... | @@ -7943,103 +7085,193 @@ fn undefPattern(comptime IntType: type) IntType { |
| 7943 | | 7085 | |
| 7944 | const FormatIntLiteralContext = struct { | 7086 | const FormatIntLiteralContext = struct { |
| 7945 | dg: *DeclGen, | 7087 | dg: *DeclGen, |
| 7946 | int_info: InternPool.Key.IntType, | 7088 | loc: ValueRenderLocation, |
| 7947 | kind: CType.Kind, | | |
| 7948 | ctype: CType, | | |
| 7949 | val: Value, | 7089 | val: Value, |
| | 7090 | cty: CType, |
| 7950 | base: u8, | 7091 | base: u8, |
| 7951 | case: std.fmt.Case, | 7092 | case: std.fmt.Case, |
| 7952 | }; | 7093 | }; |
| 7953 | fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void { | 7094 | fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void { |
| 7954 | const pt = data.dg.pt; | 7095 | const dg = data.dg; |
| 7955 | const zcu = pt.zcu; | 7096 | const zcu = dg.pt.zcu; |
| 7956 | const target = &data.dg.mod.resolved_target.result; | 7097 | const target = &dg.mod.resolved_target.result; |
| 7957 | const ctype_pool = &data.dg.ctype_pool; | 7098 | |
| 7958 | | 7099 | const val = data.val; |
| 7959 | const ExpectedContents = struct { | 7100 | const ty = val.typeOf(zcu); |
| 7960 | const base = 10; | 7101 | |
| 7961 | const bits = 128; | 7102 | assert(!val.isUndef(zcu)); |
| 7962 | const limbs_count = BigInt.calcTwosCompLimbCount(bits); | 7103 | |
| 7963 | | 7104 | var space: Value.BigIntSpace = undefined; |
| 7964 | undef_limbs: [limbs_count]BigIntLimb, | 7105 | const val_bigint = val.toBigInt(&space, zcu); |
| 7965 | wrap_limbs: [limbs_count]BigIntLimb, | 7106 | |
| 7966 | to_string_buf: [bits]u8, | 7107 | switch (CType.classifyInt(ty, zcu)) { |
| 7967 | to_string_limbs: [BigInt.calcToStringLimbsBufferLen(limbs_count, base)]BigIntLimb, | 7108 | .void => unreachable, // opv |
| 7968 | }; | 7109 | .small => |int_cty| return FormatInt128.format(.{ |
| 7969 | var stack align(@alignOf(ExpectedContents)) = | 7110 | .target = zcu.getTarget(), |
| 7970 | std.heap.stackFallback(@sizeOf(ExpectedContents), data.dg.gpa); | 7111 | .int_cty = int_cty, |
| 7971 | const allocator = stack.get(); | 7112 | .val = val_bigint, |
| 7972 | | 7113 | .is_global = data.loc == .static_initializer, |
| 7973 | var undef_limbs: []BigIntLimb = &.{}; | 7114 | .base = data.base, |
| 7974 | defer allocator.free(undef_limbs); | 7115 | .case = data.case, |
| 7975 | | 7116 | }, w), |
| 7976 | var int_buf: Value.BigIntSpace = undefined; | 7117 | .big => |big| { |
| 7977 | const int = if (data.val.isUndef(zcu)) blk: { | 7118 | if (!data.loc.isInitializer()) { |
| 7978 | undef_limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)) catch return error.WriteFailed; | 7119 | // Use `CType.fmtTypeName` directly to avoid the possibility of `error.OutOfMemory`. |
| 7979 | @memset(undef_limbs, undefPattern(BigIntLimb)); | 7120 | try w.print("({f})", .{data.cty.fmtTypeName(zcu)}); |
| 7980 | | 7121 | } |
| 7981 | var undef_int = BigInt.Mutable{ | 7122 | |
| 7982 | .limbs = undef_limbs, | 7123 | try w.writeAll("{{"); |
| 7983 | .len = undef_limbs.len, | 7124 | |
| 7984 | .positive = true, | 7125 | var limb_buf: [std.math.big.int.calcTwosCompLimbCount(65535)]std.math.big.Limb = undefined; |
| 7985 | }; | 7126 | for (0..big.limbs_len) |limb_index| { |
| 7986 | undef_int.truncate(undef_int.toConst(), data.int_info.signedness, data.int_info.bits); | 7127 | if (limb_index != 0) try w.writeAll(", "); |
| 7987 | break :blk undef_int.toConst(); | 7128 | const limb_bit_offset: u64 = switch (target.cpu.arch.endian()) { |
| 7988 | } else data.val.toBigInt(&int_buf, zcu); | 7129 | .little => limb_index * big.limb_size.bits(), |
| 7989 | assert(int.fitsInTwosComp(data.int_info.signedness, data.int_info.bits)); | 7130 | .big => (big.limbs_len - limb_index - 1) * big.limb_size.bits(), |
| 7990 | | 7131 | }; |
| 7991 | const c_bits: usize = @intCast(data.ctype.byteSize(ctype_pool, data.dg.mod) * 8); | 7132 | var limb_bigint: std.math.big.int.Mutable = .{ |
| 7992 | var one_limbs: [BigInt.calcLimbLen(1)]BigIntLimb = undefined; | 7133 | .limbs = &limb_buf, |
| 7993 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); | 7134 | .len = undefined, |
| 7994 | | 7135 | .positive = undefined, |
| 7995 | var wrap = BigInt.Mutable{ | 7136 | }; |
| 7996 | .limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits)) catch return error.WriteFailed, | 7137 | limb_bigint.shiftRight(val_bigint, limb_bit_offset); |
| 7997 | .len = undefined, | 7138 | limb_bigint.truncate(limb_bigint.toConst(), .unsigned, big.limb_size.bits()); |
| 7998 | .positive = undefined, | 7139 | try FormatInt128.format(.{ |
| 7999 | }; | 7140 | .target = zcu.getTarget(), |
| 8000 | defer allocator.free(wrap.limbs); | 7141 | .int_cty = big.limb_size.unsigned(), |
| 8001 | | 7142 | .val = limb_bigint.toConst(), |
| 8002 | const c_limb_info: struct { | 7143 | .is_global = data.loc == .static_initializer, |
| 8003 | ctype: CType, | 7144 | .base = data.base, |
| 8004 | count: usize, | 7145 | .case = data.case, |
| 8005 | endian: std.builtin.Endian, | 7146 | }, w); |
| 8006 | homogeneous: bool, | 7147 | } |
| 8007 | } = switch (data.ctype.info(ctype_pool)) { | 7148 | |
| 8008 | .basic => |basic_info| switch (basic_info) { | 7149 | try w.writeAll("}}"); |
| 8009 | else => .{ | 7150 | }, |
| 8010 | .ctype = .void, | 7151 | } |
| 8011 | .count = 1, | 7152 | } |
| 8012 | .endian = .little, | 7153 | const FormatInt128 = struct { |
| 8013 | .homogeneous = true, | 7154 | target: *const std.Target, |
| | 7155 | int_cty: CType.Int, |
| | 7156 | val: std.math.big.int.Const, |
| | 7157 | is_global: bool, |
| | 7158 | base: u8, |
| | 7159 | case: std.fmt.Case, |
| | 7160 | pub fn format(data: FormatInt128, w: *Writer) Writer.Error!void { |
| | 7161 | const target = data.target; |
| | 7162 | |
| | 7163 | const val = data.val; |
| | 7164 | const is_global = data.is_global; |
| | 7165 | const base = data.base; |
| | 7166 | const case = data.case; |
| | 7167 | |
| | 7168 | switch (data.int_cty) { |
| | 7169 | .uint8_t, |
| | 7170 | .uint16_t, |
| | 7171 | .uint32_t, |
| | 7172 | .uint64_t, |
| | 7173 | .@"unsigned short", |
| | 7174 | .@"unsigned int", |
| | 7175 | .@"unsigned long", |
| | 7176 | .@"unsigned long long", |
| | 7177 | .uintptr_t, |
| | 7178 | => |t| try w.print("{f}", .{ |
| | 7179 | fmtUnsignedIntLiteralSmall(target, t, val.toInt(u64) catch unreachable, is_global, base, case), |
| | 7180 | }), |
| | 7181 | |
| | 7182 | .int8_t, |
| | 7183 | .int16_t, |
| | 7184 | .int32_t, |
| | 7185 | .int64_t, |
| | 7186 | .char, |
| | 7187 | .@"signed short", |
| | 7188 | .@"signed int", |
| | 7189 | .@"signed long", |
| | 7190 | .@"signed long long", |
| | 7191 | .intptr_t, |
| | 7192 | => |t| try w.print("{f}", .{ |
| | 7193 | fmtSignedIntLiteralSmall(target, t, val.toInt(i64) catch unreachable, is_global, base, case), |
| | 7194 | }), |
| | 7195 | |
| | 7196 | .zig_u128 => { |
| | 7197 | const raw = val.toInt(u128) catch unreachable; |
| | 7198 | const lo: u64 = @truncate(raw); |
| | 7199 | const hi: u64 = @intCast(raw >> 64); |
| | 7200 | const macro_name: []const u8 = if (is_global) "zig_init_u128" else "zig_make_u128"; |
| | 7201 | try w.print("{s}({f}, {f})", .{ |
| | 7202 | macro_name, |
| | 7203 | fmtUnsignedIntLiteralSmall(target, .uint64_t, hi, is_global, base, case), |
| | 7204 | fmtUnsignedIntLiteralSmall(target, .uint64_t, lo, is_global, base, case), |
| | 7205 | }); |
| 8014 | }, | 7206 | }, |
| 8015 | .zig_u128, .zig_i128 => .{ | 7207 | |
| 8016 | .ctype = .u64, | 7208 | .zig_i128 => { |
| 8017 | .count = 2, | 7209 | const raw = val.toInt(i128) catch unreachable; |
| 8018 | .endian = .big, | 7210 | const lo: u64 = @truncate(@as(u128, @bitCast(raw))); |
| 8019 | .homogeneous = false, | 7211 | const hi: i64 = @intCast(raw >> 64); |
| | 7212 | const macro_name: []const u8 = if (is_global) "zig_init_i128" else "zig_make_i128"; |
| | 7213 | try w.print("{s}({f}, {f})", .{ |
| | 7214 | macro_name, |
| | 7215 | fmtSignedIntLiteralSmall(target, .int64_t, hi, is_global, base, case), |
| | 7216 | fmtUnsignedIntLiteralSmall(target, .uint64_t, lo, is_global, base, case), |
| | 7217 | }); |
| 8020 | }, | 7218 | }, |
| 8021 | }, | 7219 | } |
| 8022 | .array => |array_info| .{ | 7220 | } |
| 8023 | .ctype = array_info.elem_ctype, | 7221 | }; |
| 8024 | .count = @intCast(array_info.len), | 7222 | fn fmtUnsignedIntLiteralSmall( |
| 8025 | .endian = target.cpu.arch.endian(), | 7223 | target: *const std.Target, |
| 8026 | .homogeneous = true, | 7224 | int_cty: CType.Int, |
| 8027 | }, | 7225 | val: u64, |
| 8028 | else => unreachable, | 7226 | is_global: bool, |
| | 7227 | base: u8, |
| | 7228 | case: std.fmt.Case, |
| | 7229 | ) FormatUnsignedIntLiteralSmall { |
| | 7230 | return .{ |
| | 7231 | .target = target, |
| | 7232 | .int_cty = int_cty, |
| | 7233 | .val = val, |
| | 7234 | .is_global = is_global, |
| | 7235 | .base = base, |
| | 7236 | .case = case, |
| 8029 | }; | 7237 | }; |
| 8030 | if (c_limb_info.count == 1) { | 7238 | } |
| 8031 | if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or | 7239 | fn fmtSignedIntLiteralSmall( |
| 8032 | data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits)) | 7240 | target: *const std.Target, |
| 8033 | return w.print("{s}_{s}", .{ | 7241 | int_cty: CType.Int, |
| 8034 | data.ctype.getStandardDefineAbbrev() orelse return w.print("zig_{s}Int_{c}{d}", .{ | 7242 | val: i64, |
| 8035 | if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits, | 7243 | is_global: bool, |
| 8036 | }), | 7244 | base: u8, |
| 8037 | if (int.positive) "MAX" else "MIN", | 7245 | case: std.fmt.Case, |
| 8038 | }); | 7246 | ) FormatSignedIntLiteralSmall { |
| 8039 | | 7247 | return .{ |
| 8040 | if (!int.positive) try w.writeByte('-'); | 7248 | .target = target, |
| 8041 | try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool); | 7249 | .int_cty = int_cty, |
| | 7250 | .val = val, |
| | 7251 | .is_global = is_global, |
| | 7252 | .base = base, |
| | 7253 | .case = case, |
| | 7254 | }; |
| | 7255 | } |
| 8042 | | 7256 | |
| | 7257 | const FormatSignedIntLiteralSmall = struct { |
| | 7258 | target: *const std.Target, |
| | 7259 | int_cty: CType.Int, |
| | 7260 | val: i64, |
| | 7261 | is_global: bool, |
| | 7262 | base: u8, |
| | 7263 | case: std.fmt.Case, |
| | 7264 | pub fn format(data: FormatSignedIntLiteralSmall, w: *Writer) Writer.Error!void { |
| | 7265 | const bits = data.int_cty.bits(data.target); |
| | 7266 | const max_int: i64 = @bitCast((@as(u64, 1) << @intCast(bits - 1)) - 1); |
| | 7267 | const min_int: i64 = @bitCast(@as(u64, 1) << @intCast(bits - 1)); |
| | 7268 | if (data.val == max_int) { |
| | 7269 | return w.print("{s}_MAX", .{minMaxMacroPrefix(data.int_cty)}); |
| | 7270 | } else if (data.val == min_int) { |
| | 7271 | return w.print("{s}_MIN", .{minMaxMacroPrefix(data.int_cty)}); |
| | 7272 | } |
| | 7273 | if (data.val < 0) try w.writeByte('-'); |
| | 7274 | try w.writeAll(intLiteralPrefix(data.int_cty, data.is_global)); |
| 8043 | switch (data.base) { | 7275 | switch (data.base) { |
| 8044 | 2 => try w.writeAll("0b"), | 7276 | 2 => try w.writeAll("0b"), |
| 8045 | 8 => try w.writeByte('0'), | 7277 | 8 => try w.writeByte('0'), |
| ... | @@ -8047,68 +7279,131 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void | ... | @@ -8047,68 +7279,131 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void |
| 8047 | 16 => try w.writeAll("0x"), | 7279 | 16 => try w.writeAll("0x"), |
| 8048 | else => unreachable, | 7280 | else => unreachable, |
| 8049 | } | 7281 | } |
| 8050 | const string = int.abs().toStringAlloc(allocator, data.base, data.case) catch | 7282 | // This `@abs` is safe thanks to the `min_int` case above. |
| 8051 | return error.WriteFailed; | 7283 | try w.printInt(@abs(data.val), data.base, data.case, .{}); |
| 8052 | defer allocator.free(string); | 7284 | try w.writeAll(intLiteralSuffix(data.int_cty)); |
| 8053 | try w.writeAll(string); | 7285 | } |
| 8054 | } else { | 7286 | }; |
| 8055 | try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool); | 7287 | const FormatUnsignedIntLiteralSmall = struct { |
| 8056 | wrap.truncate(int, .unsigned, c_bits); | 7288 | target: *const std.Target, |
| 8057 | @memset(wrap.limbs[wrap.len..], 0); | 7289 | int_cty: CType.Int, |
| 8058 | wrap.len = wrap.limbs.len; | 7290 | val: u64, |
| 8059 | const limbs_per_c_limb = @divExact(wrap.len, c_limb_info.count); | 7291 | is_global: bool, |
| 8060 | | 7292 | base: u8, |
| 8061 | var c_limb_int_info: std.builtin.Type.Int = .{ | 7293 | case: std.fmt.Case, |
| 8062 | .signedness = undefined, | 7294 | pub fn format(data: FormatUnsignedIntLiteralSmall, w: *Writer) Writer.Error!void { |
| 8063 | .bits = @intCast(@divExact(c_bits, c_limb_info.count)), | 7295 | const bits = data.int_cty.bits(data.target); |
| 8064 | }; | 7296 | const max_int: u64 = @as(u64, std.math.maxInt(u64)) >> @intCast(64 - bits); |
| 8065 | var c_limb_ctype: CType = undefined; | 7297 | if (data.val == max_int) { |
| 8066 | | 7298 | return w.print("{s}_MAX", .{minMaxMacroPrefix(data.int_cty)}); |
| 8067 | var limb_offset: usize = 0; | 7299 | } |
| 8068 | const most_significant_limb_i = wrap.len - limbs_per_c_limb; | 7300 | try w.writeAll(intLiteralPrefix(data.int_cty, data.is_global)); |
| 8069 | while (limb_offset < wrap.len) : (limb_offset += limbs_per_c_limb) { | 7301 | switch (data.base) { |
| 8070 | const limb_i = switch (c_limb_info.endian) { | 7302 | 2 => try w.writeAll("0b"), |
| 8071 | .little => limb_offset, | 7303 | 8 => try w.writeByte('0'), |
| 8072 | .big => most_significant_limb_i - limb_offset, | 7304 | 10 => {}, |
| 8073 | }; | 7305 | 16 => try w.writeAll("0x"), |
| 8074 | var c_limb_mut = BigInt.Mutable{ | 7306 | else => unreachable, |
| 8075 | .limbs = wrap.limbs[limb_i..][0..limbs_per_c_limb], | | |
| 8076 | .len = undefined, | | |
| 8077 | .positive = true, | | |
| 8078 | }; | | |
| 8079 | c_limb_mut.normalize(limbs_per_c_limb); | | |
| 8080 | | | |
| 8081 | if (limb_i == most_significant_limb_i and | | |
| 8082 | !c_limb_info.homogeneous and data.int_info.signedness == .signed) | | |
| 8083 | { | | |
| 8084 | // most significant limb is actually signed | | |
| 8085 | c_limb_int_info.signedness = .signed; | | |
| 8086 | c_limb_ctype = c_limb_info.ctype.toSigned(); | | |
| 8087 | | | |
| 8088 | c_limb_mut.truncate( | | |
| 8089 | c_limb_mut.toConst(), | | |
| 8090 | .signed, | | |
| 8091 | data.int_info.bits - limb_i * @bitSizeOf(BigIntLimb), | | |
| 8092 | ); | | |
| 8093 | } else { | | |
| 8094 | c_limb_int_info.signedness = .unsigned; | | |
| 8095 | c_limb_ctype = c_limb_info.ctype; | | |
| 8096 | } | | |
| 8097 | | | |
| 8098 | if (limb_offset > 0) try w.writeAll(", "); | | |
| 8099 | try formatIntLiteral(.{ | | |
| 8100 | .dg = data.dg, | | |
| 8101 | .int_info = c_limb_int_info, | | |
| 8102 | .kind = data.kind, | | |
| 8103 | .ctype = c_limb_ctype, | | |
| 8104 | .val = pt.intValue_big(.comptime_int, c_limb_mut.toConst()) catch | | |
| 8105 | return error.WriteFailed, | | |
| 8106 | .base = data.base, | | |
| 8107 | .case = data.case, | | |
| 8108 | }, w); | | |
| 8109 | } | 7307 | } |
| | 7308 | try w.printInt(data.val, data.base, data.case, .{}); |
| | 7309 | try w.writeAll(intLiteralSuffix(data.int_cty)); |
| 8110 | } | 7310 | } |
| 8111 | try data.ctype.renderLiteralSuffix(w, ctype_pool); | 7311 | }; |
| | 7312 | fn minMaxMacroPrefix(int_cty: CType.Int) []const u8 { |
| | 7313 | return switch (int_cty) { |
| | 7314 | // zig fmt: off |
| | 7315 | .char => "CHAR", |
| | 7316 | |
| | 7317 | .@"unsigned short" => "USHRT", |
| | 7318 | .@"unsigned int" => "UINT", |
| | 7319 | .@"unsigned long" => "ULONG", |
| | 7320 | .@"unsigned long long" => "ULLONG", |
| | 7321 | |
| | 7322 | .@"signed short" => "SHRT", |
| | 7323 | .@"signed int" => "INT", |
| | 7324 | .@"signed long" => "LONG", |
| | 7325 | .@"signed long long" => "LLONG", |
| | 7326 | |
| | 7327 | .uint8_t => "UINT8", |
| | 7328 | .uint16_t => "UINT16", |
| | 7329 | .uint32_t => "UINT32", |
| | 7330 | .uint64_t => "UINT64", |
| | 7331 | .zig_u128 => unreachable, |
| | 7332 | |
| | 7333 | .int8_t => "INT8", |
| | 7334 | .int16_t => "INT16", |
| | 7335 | .int32_t => "INT32", |
| | 7336 | .int64_t => "INT64", |
| | 7337 | .zig_i128 => unreachable, |
| | 7338 | |
| | 7339 | .uintptr_t => "UINTPTR", |
| | 7340 | .intptr_t => "INTPTR", |
| | 7341 | // zig fmt: on |
| | 7342 | }; |
| | 7343 | } |
| | 7344 | fn intLiteralPrefix(cty: CType.Int, is_global: bool) []const u8 { |
| | 7345 | return switch (cty) { |
| | 7346 | // zig fmt: off |
| | 7347 | .char => if (is_global) "" else "(char)", |
| | 7348 | |
| | 7349 | .@"unsigned short" => if (is_global) "" else "(unsigned short)", |
| | 7350 | .@"unsigned int" => "", |
| | 7351 | .@"unsigned long" => "", |
| | 7352 | .@"unsigned long long" => "", |
| | 7353 | |
| | 7354 | .@"signed short" => if (is_global) "" else "(signed short)", |
| | 7355 | .@"signed int" => "", |
| | 7356 | .@"signed long" => "", |
| | 7357 | .@"signed long long" => "", |
| | 7358 | |
| | 7359 | .uint8_t => "UINT8_C(", |
| | 7360 | .uint16_t => "UINT16_C(", |
| | 7361 | .uint32_t => "UINT32_C(", |
| | 7362 | .uint64_t => "UINT64_C(", |
| | 7363 | .zig_u128 => unreachable, |
| | 7364 | |
| | 7365 | .int8_t => "INT8_C(", |
| | 7366 | .int16_t => "INT16_C(", |
| | 7367 | .int32_t => "INT32_C(", |
| | 7368 | .int64_t => "INT64_C(", |
| | 7369 | .zig_i128 => unreachable, |
| | 7370 | |
| | 7371 | .uintptr_t => if (is_global) "" else "(uintptr_t)", |
| | 7372 | .intptr_t => if (is_global) "" else "(intptr_t)", |
| | 7373 | // zig fmt: on |
| | 7374 | }; |
| | 7375 | } |
| | 7376 | fn intLiteralSuffix(cty: CType.Int) []const u8 { |
| | 7377 | return switch (cty) { |
| | 7378 | // zig fmt: off |
| | 7379 | .char => "", |
| | 7380 | |
| | 7381 | .@"unsigned short" => "u", |
| | 7382 | .@"unsigned int" => "u", |
| | 7383 | .@"unsigned long" => "ul", |
| | 7384 | .@"unsigned long long" => "ull", |
| | 7385 | |
| | 7386 | .@"signed short" => "", |
| | 7387 | .@"signed int" => "", |
| | 7388 | .@"signed long" => "l", |
| | 7389 | .@"signed long long" => "ll", |
| | 7390 | |
| | 7391 | .uint8_t => ")", |
| | 7392 | .uint16_t => ")", |
| | 7393 | .uint32_t => ")", |
| | 7394 | .uint64_t => ")", |
| | 7395 | .zig_u128 => unreachable, |
| | 7396 | |
| | 7397 | .int8_t => ")", |
| | 7398 | .int16_t => ")", |
| | 7399 | .int32_t => ")", |
| | 7400 | .int64_t => ")", |
| | 7401 | .zig_i128 => unreachable, |
| | 7402 | |
| | 7403 | .uintptr_t => "ul", |
| | 7404 | .intptr_t => "", |
| | 7405 | // zig fmt: on |
| | 7406 | }; |
| 8112 | } | 7407 | } |
| 8113 | | 7408 | |
| 8114 | const Materialize = struct { | 7409 | const Materialize = struct { |
| ... | @@ -8123,7 +7418,7 @@ const Materialize = struct { | ... | @@ -8123,7 +7418,7 @@ const Materialize = struct { |
| 8123 | } | 7418 | } |
| 8124 | | 7419 | |
| 8125 | pub fn mat(self: Materialize, f: *Function, w: *Writer) !void { | 7420 | pub fn mat(self: Materialize, f: *Function, w: *Writer) !void { |
| 8126 | try f.writeCValue(w, self.local, .Other); | 7421 | try f.writeCValue(w, self.local, .other); |
| 8127 | } | 7422 | } |
| 8128 | | 7423 | |
| 8129 | pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void { | 7424 | pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void { |
| ... | @@ -8131,95 +7426,52 @@ const Materialize = struct { | ... | @@ -8131,95 +7426,52 @@ const Materialize = struct { |
| 8131 | } | 7426 | } |
| 8132 | }; | 7427 | }; |
| 8133 | | 7428 | |
| 8134 | const Assignment = struct { | | |
| 8135 | ctype: CType, | | |
| 8136 | | | |
| 8137 | pub fn start(f: *Function, w: *Writer, ctype: CType) !Assignment { | | |
| 8138 | const self: Assignment = .{ .ctype = ctype }; | | |
| 8139 | try self.restart(f, w); | | |
| 8140 | return self; | | |
| 8141 | } | | |
| 8142 | | | |
| 8143 | pub fn restart(self: Assignment, f: *Function, w: *Writer) !void { | | |
| 8144 | switch (self.strategy(f)) { | | |
| 8145 | .assign => {}, | | |
| 8146 | .memcpy => try w.writeAll("memcpy("), | | |
| 8147 | } | | |
| 8148 | } | | |
| 8149 | | | |
| 8150 | pub fn assign(self: Assignment, f: *Function, w: *Writer) !void { | | |
| 8151 | switch (self.strategy(f)) { | | |
| 8152 | .assign => try w.writeAll(" = "), | | |
| 8153 | .memcpy => try w.writeAll(", "), | | |
| 8154 | } | | |
| 8155 | } | | |
| 8156 | | | |
| 8157 | pub fn end(self: Assignment, f: *Function, w: *Writer) !void { | | |
| 8158 | switch (self.strategy(f)) { | | |
| 8159 | .assign => {}, | | |
| 8160 | .memcpy => { | | |
| 8161 | try w.writeAll(", sizeof("); | | |
| 8162 | try f.renderCType(w, self.ctype); | | |
| 8163 | try w.writeAll("))"); | | |
| 8164 | }, | | |
| 8165 | } | | |
| 8166 | try w.writeByte(';'); | | |
| 8167 | try f.object.newline(); | | |
| 8168 | } | | |
| 8169 | | | |
| 8170 | fn strategy(self: Assignment, f: *Function) enum { assign, memcpy } { | | |
| 8171 | return switch (self.ctype.info(&f.object.dg.ctype_pool)) { | | |
| 8172 | else => .assign, | | |
| 8173 | .array, .vector => .memcpy, | | |
| 8174 | }; | | |
| 8175 | } | | |
| 8176 | }; | | |
| 8177 | | | |
| 8178 | const Vectorize = struct { | 7429 | const Vectorize = struct { |
| 8179 | index: CValue = .none, | 7430 | index: CValue = .none, |
| 8180 | | 7431 | |
| 8181 | pub fn start(f: *Function, inst: Air.Inst.Index, w: *Writer, ty: Type) !Vectorize { | 7432 | pub fn start(f: *Function, inst: Air.Inst.Index, w: *Writer, ty: Type) !Vectorize { |
| 8182 | const pt = f.object.dg.pt; | 7433 | const pt = f.dg.pt; |
| 8183 | const zcu = pt.zcu; | 7434 | const zcu = pt.zcu; |
| 8184 | return if (ty.zigTypeTag(zcu) == .vector) index: { | 7435 | switch (ty.zigTypeTag(zcu)) { |
| 8185 | const local = try f.allocLocal(inst, .usize); | 7436 | else => return .{ .index = .none }, |
| 8186 | | 7437 | .vector => { |
| 8187 | try w.writeAll("for ("); | 7438 | const local = try f.allocLocal(inst, .usize); |
| 8188 | try f.writeCValue(w, local, .Other); | 7439 | try w.writeAll("for ("); |
| 8189 | try w.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)}); | 7440 | try f.writeCValue(w, local, .other); |
| 8190 | try f.writeCValue(w, local, .Other); | 7441 | try w.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)}); |
| 8191 | try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); | 7442 | try f.writeCValue(w, local, .other); |
| 8192 | try f.writeCValue(w, local, .Other); | 7443 | try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 8193 | try w.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)}); | 7444 | try f.writeCValue(w, local, .other); |
| 8194 | f.object.indent(); | 7445 | try w.print(" += {f}) {{", .{try f.fmtIntLiteralDec(.one_usize)}); |
| 8195 | try f.object.newline(); | 7446 | f.indent(); |
| 8196 | | 7447 | try f.newline(); |
| 8197 | break :index .{ .index = local }; | 7448 | return .{ .index = local }; |
| 8198 | } else .{}; | 7449 | }, |
| | 7450 | } |
| 8199 | } | 7451 | } |
| 8200 | | 7452 | |
| 8201 | pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void { | 7453 | pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void { |
| 8202 | if (self.index != .none) { | 7454 | if (self.index != .none) { |
| 8203 | try w.writeByte('['); | 7455 | try w.writeAll(".array["); |
| 8204 | try f.writeCValue(w, self.index, .Other); | 7456 | try f.writeCValue(w, self.index, .other); |
| 8205 | try w.writeByte(']'); | 7457 | try w.writeByte(']'); |
| 8206 | } | 7458 | } |
| 8207 | } | 7459 | } |
| 8208 | | 7460 | |
| 8209 | pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void { | 7461 | pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void { |
| 8210 | if (self.index != .none) { | 7462 | if (self.index != .none) { |
| 8211 | try f.object.outdent(); | 7463 | try f.outdent(); |
| 8212 | try w.writeByte('}'); | 7464 | try w.writeByte('}'); |
| 8213 | try f.object.newline(); | 7465 | try f.newline(); |
| 8214 | try freeLocal(f, inst, self.index.new_local, null); | 7466 | try freeLocal(f, inst, self.index.new_local, null); |
| 8215 | } | 7467 | } |
| 8216 | } | 7468 | } |
| 8217 | }; | 7469 | }; |
| 8218 | | 7470 | |
| 8219 | fn lowersToArray(ty: Type, zcu: *Zcu) bool { | 7471 | fn lowersToBigInt(ty: Type, zcu: *const Zcu) bool { |
| 8220 | return switch (ty.zigTypeTag(zcu)) { | 7472 | return switch (ty.zigTypeTag(zcu)) { |
| 8221 | .array, .vector => return true, | 7473 | .int, .@"enum", .@"struct", .@"union" => CType.classifyInt(ty, zcu) == .big, |
| 8222 | else => return ty.isAbiInt(zcu) and toCIntBits(@as(u32, @intCast(ty.bitSize(zcu)))) == null, | 7474 | else => false, |
| 8223 | }; | 7475 | }; |
| 8224 | } | 7476 | } |
| 8225 | | 7477 | |
| ... | @@ -8245,8 +7497,8 @@ fn die(f: *Function, inst: Air.Inst.Index, ref: Air.Inst.Ref) !void { | ... | @@ -8245,8 +7497,8 @@ fn die(f: *Function, inst: Air.Inst.Index, ref: Air.Inst.Ref) !void { |
| 8245 | } | 7497 | } |
| 8246 | | 7498 | |
| 8247 | fn freeLocal(f: *Function, inst: ?Air.Inst.Index, local_index: LocalIndex, ref_inst: ?Air.Inst.Index) !void { | 7499 | fn freeLocal(f: *Function, inst: ?Air.Inst.Index, local_index: LocalIndex, ref_inst: ?Air.Inst.Index) !void { |
| 8248 | const gpa = f.object.dg.gpa; | 7500 | const gpa = f.dg.gpa; |
| 8249 | const local = &f.locals.items[local_index]; | 7501 | const local = f.locals.items[local_index]; |
| 8250 | if (inst) |i| { | 7502 | if (inst) |i| { |
| 8251 | if (ref_inst) |operand| { | 7503 | if (ref_inst) |operand| { |
| 8252 | log.debug("%{d}: freeing t{d} (operand %{d})", .{ @intFromEnum(i), local_index, operand }); | 7504 | log.debug("%{d}: freeing t{d} (operand %{d})", .{ @intFromEnum(i), local_index, operand }); |
| ... | @@ -8260,7 +7512,7 @@ fn freeLocal(f: *Function, inst: ?Air.Inst.Index, local_index: LocalIndex, ref_i | ... | @@ -8260,7 +7512,7 @@ fn freeLocal(f: *Function, inst: ?Air.Inst.Index, local_index: LocalIndex, ref_i |
| 8260 | log.debug("freeing t{d}", .{local_index}); | 7512 | log.debug("freeing t{d}", .{local_index}); |
| 8261 | } | 7513 | } |
| 8262 | } | 7514 | } |
| 8263 | const gop = try f.free_locals_map.getOrPut(gpa, local.getType()); | 7515 | const gop = try f.free_locals_map.getOrPut(gpa, local); |
| 8264 | if (!gop.found_existing) gop.value_ptr.* = .{}; | 7516 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 8265 | if (std.debug.runtime_safety) { | 7517 | if (std.debug.runtime_safety) { |
| 8266 | // If this trips, an unfreeable allocation was attempted to be freed. | 7518 | // If this trips, an unfreeable allocation was attempted to be freed. |
| ... | @@ -8317,3 +7569,28 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { | ... | @@ -8317,3 +7569,28 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { |
| 8317 | } | 7569 | } |
| 8318 | map.deinit(gpa); | 7570 | map.deinit(gpa); |
| 8319 | } | 7571 | } |
| | 7572 | |
| | 7573 | fn renderErrorName(w: *Writer, err_name: []const u8) Writer.Error!void { |
| | 7574 | try w.print("zig_error_{f}", .{fmtIdentUnsolo(err_name)}); |
| | 7575 | } |
| | 7576 | |
| | 7577 | fn renderNavName(w: *Writer, nav_index: InternPool.Nav.Index, ip: *const InternPool) !void { |
| | 7578 | const nav = ip.getNav(nav_index); |
| | 7579 | if (nav.getExtern(ip)) |@"extern"| { |
| | 7580 | try w.print("{f}", .{ |
| | 7581 | fmtIdentSolo(ip.getNav(@"extern".owner_nav).name.toSlice(ip)), |
| | 7582 | }); |
| | 7583 | } else { |
| | 7584 | // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), |
| | 7585 | // expand to 3x the length of its input, but let's cut it off at a much shorter limit. |
| | 7586 | const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip); |
| | 7587 | try w.print("{f}__{d}", .{ |
| | 7588 | fmtIdentUnsolo(fqn_slice[0..@min(fqn_slice.len, 100)]), |
| | 7589 | @intFromEnum(nav_index), |
| | 7590 | }); |
| | 7591 | } |
| | 7592 | } |
| | 7593 | |
| | 7594 | fn renderUavName(w: *Writer, uav: Value) !void { |
| | 7595 | try w.print("__anon_{d}", .{@intFromEnum(uav.toIntern())}); |
| | 7596 | } |