| ... | ... | @@ -50,32 +50,39 @@ pub fn legalizeFeatures(_: *const std.Target) ?*const Air.Legalize.Features { |
| 50 | 50 | /// * The types used, so declarations can be emitted in `flush` |
| 51 | 51 | /// * The lazy functions used, so definitions can be emitted in `flush` |
| 52 | 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 | 57 | /// This map contains all the UAVs we saw generating this function. |
| 54 | 58 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. |
| 55 | 59 | /// Key is the value of the UAV; value is the UAV's alignment, or |
| 56 | 60 | /// `.none` for natural alignment. The specified alignment is never |
| 57 | 61 | /// less than the natural alignment. |
| 58 | | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 59 | | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. |
| 60 | | code_header: []u8, |
| 61 | | code: []u8, |
| 62 | | fwd_decl: []u8, |
| 63 | | ctype_pool: CType.Pool, |
| 64 | | lazy_fns: LazyFnMap, |
| 62 | need_uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 63 | ctype_deps: CType.Dependencies, |
| 64 | /// Key is an enum type for which we need a generated `@tagName` function. |
| 65 | need_tag_name_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Index, void), |
| 66 | /// Key is a function Nav for which we need a generated `zig_never_tail` wrapper. |
| 67 | need_never_tail_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void), |
| 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 | 71 | pub fn deinit(mir: *Mir, gpa: Allocator) void { |
| 67 | | mir.uavs.deinit(gpa); |
| 72 | gpa.free(mir.fwd_decl); |
| 68 | 73 | gpa.free(mir.code_header); |
| 69 | 74 | gpa.free(mir.code); |
| 70 | | gpa.free(mir.fwd_decl); |
| 71 | | mir.ctype_pool.deinit(gpa); |
| 72 | | mir.lazy_fns.deinit(gpa); |
| 75 | mir.need_uavs.deinit(gpa); |
| 76 | mir.ctype_deps.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 | 87 | pub const CValue = union(enum) { |
| 81 | 88 | none: void, |
| ... | ... | @@ -87,8 +94,6 @@ pub const CValue = union(enum) { |
| 87 | 94 | constant: Value, |
| 88 | 95 | /// Index into the parameters |
| 89 | 96 | arg: usize, |
| 90 | | /// The array field of a parameter |
| 91 | | arg_array: usize, |
| 92 | 97 | /// Index into a tuple's fields |
| 93 | 98 | field: usize, |
| 94 | 99 | /// By-value |
| ... | ... | @@ -100,8 +105,6 @@ pub const CValue = union(enum) { |
| 100 | 105 | identifier: []const u8, |
| 101 | 106 | /// Rendered as "payload." followed by as identifier (using fmtIdent) |
| 102 | 107 | payload_identifier: []const u8, |
| 103 | | /// Rendered with fmtCTypePoolString |
| 104 | | ctype_pool_string: CType.Pool.String, |
| 105 | 108 | |
| 106 | 109 | fn eql(lhs: CValue, rhs: CValue) bool { |
| 107 | 110 | return switch (lhs) { |
| ... | ... | @@ -122,10 +125,6 @@ pub const CValue = union(enum) { |
| 122 | 125 | .arg => |rhs_arg_index| lhs_arg_index == rhs_arg_index, |
| 123 | 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 | 128 | .field => |lhs_field_index| switch (rhs) { |
| 130 | 129 | .field => |rhs_field_index| lhs_field_index == rhs_field_index, |
| 131 | 130 | else => false, |
| ... | ... | @@ -150,10 +149,6 @@ pub const CValue = union(enum) { |
| 150 | 149 | .payload_identifier => |rhs_id| std.mem.eql(u8, lhs_id, rhs_id), |
| 151 | 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 | 158 | result: CValue, |
| 164 | 159 | }; |
| 165 | 160 | |
| 166 | | pub const CValueMap = std.AutoHashMap(Air.Inst.Ref, CValue); |
| 167 | | |
| 168 | | pub const LazyFnKey = union(enum) { |
| 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 | | } |
| 161 | const LocalType = struct { |
| 162 | type: Type, |
| 163 | alignment: Alignment, |
| 188 | 164 | }; |
| 189 | 165 | |
| 190 | 166 | const LocalIndex = u16; |
| 191 | | const LocalType = struct { ctype: CType, alignas: CType.AlignAs }; |
| 192 | 167 | const LocalsList = std.AutoArrayHashMapUnmanaged(LocalIndex, void); |
| 193 | 168 | const LocalsMap = std.AutoArrayHashMapUnmanaged(LocalType, LocalsList); |
| 194 | 169 | |
| 195 | 170 | const ValueRenderLocation = enum { |
| 196 | | FunctionArgument, |
| 197 | | Initializer, |
| 198 | | StaticInitializer, |
| 199 | | Other, |
| 171 | initializer, |
| 172 | static_initializer, |
| 173 | other, |
| 200 | 174 | |
| 201 | 175 | fn isInitializer(loc: ValueRenderLocation) bool { |
| 202 | 176 | return switch (loc) { |
| 203 | | .Initializer, .StaticInitializer => true, |
| 204 | | else => 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, |
| 177 | .initializer, .static_initializer => true, |
| 178 | .other => false, |
| 213 | 179 | }; |
| 214 | 180 | } |
| 215 | 181 | }; |
| ... | ... | @@ -334,16 +300,31 @@ const reserved_idents = std.StaticStringMap(void).initComptime(.{ |
| 334 | 300 | }); |
| 335 | 301 | |
| 336 | 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 | 305 | switch (ident[1]) { |
| 339 | 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 | 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 | 324 | return true; |
| 346 | | } else return reserved_idents.has(ident); |
| 325 | } |
| 326 | |
| 327 | return reserved_idents.has(ident); |
| 347 | 328 | } |
| 348 | 329 | |
| 349 | 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 | 342 | for (ident, 0..) |c, i| { |
| 362 | 343 | switch (c) { |
| 363 | 344 | 'a'...'z', 'A'...'Z', '_' => try w.writeByte(c), |
| 364 | | '.' => try w.writeByte('_'), |
| 345 | '.', ' ' => try w.writeByte('_'), |
| 365 | 346 | '0'...'9' => if (i == 0) { |
| 366 | 347 | try w.print("_{x:2}", .{c}); |
| 367 | 348 | } else { |
| ... | ... | @@ -380,29 +361,6 @@ pub fn fmtIdentUnsolo(ident: []const u8) std.fmt.Alt([]const u8, formatIdentUnso |
| 380 | 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 | 364 | // Returns true if `formatIdent` would make any edits to ident. |
| 407 | 365 | // This must be kept in sync with `formatIdent`. |
| 408 | 366 | pub fn isMangledIdent(ident: []const u8, solo: bool) bool { |
| ... | ... | @@ -417,21 +375,26 @@ pub fn isMangledIdent(ident: []const u8, solo: bool) bool { |
| 417 | 375 | return false; |
| 418 | 376 | } |
| 419 | 377 | |
| 420 | | /// This data is available when outputting .c code for a `InternPool.Index` |
| 421 | | /// that corresponds to `func`. |
| 422 | | /// It is not available when generating .h file. |
| 378 | /// This data is available when rendering C source code for an interned function. |
| 423 | 379 | pub const Function = struct { |
| 424 | 380 | air: Air, |
| 425 | 381 | liveness: Air.Liveness, |
| 426 | | value_map: CValueMap, |
| 382 | value_map: std.AutoHashMap(Air.Inst.Ref, CValue), |
| 427 | 383 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .empty, |
| 428 | 384 | next_arg_index: u32 = 0, |
| 429 | 385 | next_block_index: u32 = 0, |
| 430 | | object: Object, |
| 431 | | lazy_fns: LazyFnMap, |
| 386 | dg: DeclGen, |
| 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 | 395 | func_index: InternPool.Index, |
| 433 | 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 | 398 | /// Which locals are available for reuse, based on Type. |
| 436 | 399 | free_locals_map: LocalsMap = .{}, |
| 437 | 400 | /// Locals which will not be freed by Liveness. This is used after a |
| ... | ... | @@ -445,37 +408,41 @@ pub const Function = struct { |
| 445 | 408 | /// for the switch cond. Dispatches should set this local to the new cond. |
| 446 | 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 | 435 | fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue { |
| 449 | 436 | const gop = try f.value_map.getOrPut(ref); |
| 450 | | if (gop.found_existing) return gop.value_ptr.*; |
| 451 | | |
| 452 | | const pt = f.object.dg.pt; |
| 453 | | const zcu = pt.zcu; |
| 454 | | const val = (try f.air.value(ref, pt)).?; |
| 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; |
| 437 | if (!gop.found_existing) { |
| 438 | const val = try f.air.value(ref, f.dg.pt); |
| 439 | gop.value_ptr.* = .{ .constant = val.? }; |
| 440 | } |
| 441 | return gop.value_ptr.*; |
| 475 | 442 | } |
| 476 | 443 | |
| 477 | 444 | fn wantSafety(f: *Function) bool { |
| 478 | | return switch (f.object.dg.pt.zcu.optimizeMode()) { |
| 445 | return switch (f.dg.pt.zcu.optimizeMode()) { |
| 479 | 446 | .Debug, .ReleaseSafe => true, |
| 480 | 447 | .ReleaseFast, .ReleaseSmall => false, |
| 481 | 448 | }; |
| ... | ... | @@ -485,18 +452,16 @@ pub const Function = struct { |
| 485 | 452 | /// those which go into `allocs`. This function does not add the resulting local into `allocs`; |
| 486 | 453 | /// that responsibility lies with the caller. |
| 487 | 454 | fn allocLocalValue(f: *Function, local_type: LocalType) !CValue { |
| 488 | | try f.locals.ensureUnusedCapacity(f.object.dg.gpa, 1); |
| 489 | | defer f.locals.appendAssumeCapacity(.{ |
| 490 | | .ctype = local_type.ctype, |
| 491 | | .flags = .{ .alignas = local_type.alignas }, |
| 492 | | }); |
| 493 | | return .{ .new_local = @intCast(f.locals.items.len) }; |
| 455 | try f.locals.ensureUnusedCapacity(f.dg.gpa, 1); |
| 456 | const index = f.locals.items.len; |
| 457 | f.locals.appendAssumeCapacity(local_type); |
| 458 | return .{ .new_local = @intCast(index) }; |
| 494 | 459 | } |
| 495 | 460 | |
| 496 | 461 | fn allocLocal(f: *Function, inst: ?Air.Inst.Index, ty: Type) !CValue { |
| 497 | 462 | return f.allocAlignedLocal(inst, .{ |
| 498 | | .ctype = try f.ctypeFromType(ty, .complete), |
| 499 | | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(f.object.dg.pt.zcu)), |
| 463 | .type = ty, |
| 464 | .alignment = .none, |
| 500 | 465 | }); |
| 501 | 466 | } |
| 502 | 467 | |
| ... | ... | @@ -524,11 +489,10 @@ pub const Function = struct { |
| 524 | 489 | .none => unreachable, |
| 525 | 490 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| 526 | 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 | 493 | .arg => |i| try w.print("a{d}", .{i}), |
| 529 | | .arg_array => |i| try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }), |
| 530 | | .undef => |ty| try f.object.dg.renderUndefValue(w, ty, location), |
| 531 | | else => try f.object.dg.writeCValue(w, c_value), |
| 494 | .undef => |ty| try f.dg.renderUndefValue(w, ty, location), |
| 495 | else => try f.dg.writeCValue(w, c_value), |
| 532 | 496 | } |
| 533 | 497 | } |
| 534 | 498 | |
| ... | ... | @@ -537,17 +501,12 @@ pub const Function = struct { |
| 537 | 501 | .none => unreachable, |
| 538 | 502 | .new_local, .local, .constant => { |
| 539 | 503 | try w.writeAll("(*"); |
| 540 | | try f.writeCValue(w, c_value, .Other); |
| 504 | try f.writeCValue(w, c_value, .other); |
| 541 | 505 | try w.writeByte(')'); |
| 542 | 506 | }, |
| 543 | 507 | .local_ref => |i| try w.print("t{d}", .{i}), |
| 544 | 508 | .arg => |i| try w.print("(*a{d})", .{i}), |
| 545 | | .arg_array => |i| { |
| 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), |
| 509 | else => try f.dg.writeCValueDeref(w, c_value), |
| 551 | 510 | } |
| 552 | 511 | } |
| 553 | 512 | |
| ... | ... | @@ -558,119 +517,77 @@ pub const Function = struct { |
| 558 | 517 | member: CValue, |
| 559 | 518 | ) Error!void { |
| 560 | 519 | switch (c_value) { |
| 561 | | .new_local, .local, .local_ref, .constant, .arg, .arg_array => { |
| 562 | | try f.writeCValue(w, c_value, .Other); |
| 520 | .new_local, .local, .local_ref, .constant, .arg => { |
| 521 | try f.writeCValue(w, c_value, .other); |
| 563 | 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 | 529 | fn writeCValueDerefMember(f: *Function, w: *Writer, c_value: CValue, member: CValue) !void { |
| 571 | 530 | switch (c_value) { |
| 572 | | .new_local, .local, .arg, .arg_array => { |
| 573 | | try f.writeCValue(w, c_value, .Other); |
| 531 | .new_local, .local, .arg => { |
| 532 | try f.writeCValue(w, c_value, .other); |
| 574 | 533 | try w.writeAll("->"); |
| 575 | 534 | }, |
| 576 | 535 | .constant => { |
| 577 | 536 | try w.writeByte('('); |
| 578 | | try f.writeCValue(w, c_value, .Other); |
| 537 | try f.writeCValue(w, c_value, .other); |
| 579 | 538 | try w.writeAll(")->"); |
| 580 | 539 | }, |
| 581 | 540 | .local_ref => { |
| 582 | 541 | try f.writeCValueDeref(w, c_value); |
| 583 | 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 | 549 | fn fail(f: *Function, comptime format: []const u8, args: anytype) Error { |
| 591 | | return f.object.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); |
| 550 | return f.dg.fail(format, args); |
| 604 | 551 | } |
| 605 | 552 | |
| 606 | | fn renderCType(f: *Function, w: *Writer, ctype: CType) !void { |
| 607 | | return f.object.dg.renderCType(w, ctype); |
| 553 | fn renderType(f: *Function, w: *Writer, ty: Type) !void { |
| 554 | return f.dg.renderType(w, ty); |
| 608 | 555 | } |
| 609 | 556 | |
| 610 | 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 | 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 | 565 | fn fmtIntLiteralHex(f: *Function, val: Value) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { |
| 619 | | return f.object.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).?; |
| 566 | return f.dg.fmtIntLiteralHex(val, .other); |
| 652 | 567 | } |
| 653 | 568 | |
| 654 | 569 | pub fn deinit(f: *Function) void { |
| 655 | | const gpa = f.object.dg.gpa; |
| 570 | const gpa = f.dg.gpa; |
| 656 | 571 | f.allocs.deinit(gpa); |
| 657 | 572 | f.locals.deinit(gpa); |
| 658 | 573 | deinitFreeLocalsMap(gpa, &f.free_locals_map); |
| 659 | 574 | f.blocks.deinit(gpa); |
| 660 | 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 | 579 | f.loop_switch_conds.deinit(gpa); |
| 663 | 580 | } |
| 664 | 581 | |
| 665 | 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 | 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 | 591 | switch (dst) { |
| 675 | 592 | .new_local, .local => |dst_local_index| switch (src) { |
| 676 | 593 | .new_local, .local => |src_local_index| if (dst_local_index == src_local_index) return, |
| ... | ... | @@ -678,12 +595,12 @@ pub const Function = struct { |
| 678 | 595 | }, |
| 679 | 596 | else => {}, |
| 680 | 597 | } |
| 681 | | const w = &f.object.code.writer; |
| 682 | | const a = try Assignment.start(f, w, ctype); |
| 683 | | try f.writeCValue(w, dst, .Other); |
| 684 | | try a.assign(f, w); |
| 685 | | try f.writeCValue(w, src, .Other); |
| 686 | | try a.end(f, w); |
| 598 | const w = &f.code.writer; |
| 599 | try f.writeCValue(w, dst, .other); |
| 600 | try w.writeAll(" = "); |
| 601 | try f.writeCValue(w, src, .other); |
| 602 | try w.writeByte(';'); |
| 603 | try f.newline(); |
| 687 | 604 | } |
| 688 | 605 | |
| 689 | 606 | fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue { |
| ... | ... | @@ -694,7 +611,7 @@ pub const Function = struct { |
| 694 | 611 | else => { |
| 695 | 612 | try freeCValue(f, inst, src); |
| 696 | 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 | 615 | return dst; |
| 699 | 616 | }, |
| 700 | 617 | } |
| ... | ... | @@ -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`. |
| 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. |
| 628 | /// This data is available when rendering *any* C source code (function or otherwise). |
| 745 | 629 | pub const DeclGen = struct { |
| 746 | 630 | gpa: Allocator, |
| 631 | arena: Allocator, |
| 747 | 632 | pt: Zcu.PerThread, |
| 748 | 633 | mod: *Module, |
| 749 | | pass: Pass, |
| 634 | owner_nav: InternPool.Nav.Index.Optional, |
| 750 | 635 | is_naked_fn: bool, |
| 751 | 636 | expected_block: ?u32, |
| 752 | | fwd_decl: Writer.Allocating, |
| 753 | 637 | error_msg: ?*Zcu.ErrorMsg, |
| 754 | | ctype_pool: CType.Pool, |
| 755 | | scratch: std.ArrayList(u32), |
| 638 | ctype_deps: CType.Dependencies, |
| 756 | 639 | /// This map contains all the UAVs we saw generating this function. |
| 757 | 640 | /// `link.C` will merge them into its `uavs`/`aligned_uavs` fields. |
| 758 | 641 | /// Key is the value of the UAV; value is the UAV's alignment, or |
| ... | ... | @@ -760,16 +643,10 @@ pub const DeclGen = struct { |
| 760 | 643 | /// less than the natural alignment. |
| 761 | 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 | 646 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) Error { |
| 770 | 647 | @branchHint(.cold); |
| 771 | 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 | 650 | dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args); |
| 774 | 651 | return error.AnalysisFail; |
| 775 | 652 | } |
| ... | ... | @@ -783,14 +660,13 @@ pub const DeclGen = struct { |
| 783 | 660 | const pt = dg.pt; |
| 784 | 661 | const zcu = pt.zcu; |
| 785 | 662 | const ip = &zcu.intern_pool; |
| 786 | | const ctype_pool = &dg.ctype_pool; |
| 787 | 663 | const uav_val = Value.fromInterned(uav.val); |
| 788 | 664 | const uav_ty = uav_val.typeOf(zcu); |
| 789 | 665 | |
| 790 | 666 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 791 | 667 | const ptr_ty: Type = .fromInterned(uav.orig_ty); |
| 792 | 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 | 672 | // Chase function values in order to be able to reference the original function. |
| ... | ... | @@ -805,14 +681,12 @@ pub const DeclGen = struct { |
| 805 | 681 | // them). The analysis until now should ensure that the C function |
| 806 | 682 | // pointers are compatible. If they are not, then there is a bug |
| 807 | 683 | // somewhere and we should let the C compiler tell us about it. |
| 808 | | const ptr_ctype = try dg.ctypeFromType(ptr_ty, .complete); |
| 809 | | const elem_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype; |
| 810 | | const uav_ctype = try dg.ctypeFromType(uav_ty, .complete); |
| 811 | | const need_cast = !elem_ctype.eql(uav_ctype) and |
| 812 | | (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function); |
| 684 | const elem_ty = ptr_ty.childType(zcu); |
| 685 | const need_cast = elem_ty.toIntern() != uav_ty.toIntern() and |
| 686 | elem_ty.zigTypeTag(zcu) != .@"fn" or uav_ty.zigTypeTag(zcu) != .@"fn"; |
| 813 | 687 | if (need_cast) { |
| 814 | 688 | try w.writeAll("(("); |
| 815 | | try dg.renderCType(w, ptr_ctype); |
| 689 | try dg.renderType(w, ptr_ty); |
| 816 | 690 | try w.writeByte(')'); |
| 817 | 691 | } |
| 818 | 692 | try w.writeByte('&'); |
| ... | ... | @@ -842,11 +716,9 @@ pub const DeclGen = struct { |
| 842 | 716 | nav_index: InternPool.Nav.Index, |
| 843 | 717 | location: ValueRenderLocation, |
| 844 | 718 | ) Error!void { |
| 845 | | _ = location; |
| 846 | 719 | const pt = dg.pt; |
| 847 | 720 | const zcu = pt.zcu; |
| 848 | 721 | const ip = &zcu.intern_pool; |
| 849 | | const ctype_pool = &dg.ctype_pool; |
| 850 | 722 | |
| 851 | 723 | // Chase function values in order to be able to reference the original function. |
| 852 | 724 | const owner_nav = switch (ip.getNav(nav_index).status) { |
| ... | ... | @@ -863,25 +735,23 @@ pub const DeclGen = struct { |
| 863 | 735 | const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip)); |
| 864 | 736 | const ptr_ty = try pt.navPtrType(owner_nav); |
| 865 | 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 | 741 | // We shouldn't cast C function pointers as this is UB (when you call |
| 870 | 742 | // them). The analysis until now should ensure that the C function |
| 871 | 743 | // pointers are compatible. If they are not, then there is a bug |
| 872 | 744 | // somewhere and we should let the C compiler tell us about it. |
| 873 | | const ctype = try dg.ctypeFromType(ptr_ty, .complete); |
| 874 | | const elem_ctype = ctype.info(ctype_pool).pointer.elem_ctype; |
| 875 | | const nav_ctype = try dg.ctypeFromType(nav_ty, .complete); |
| 876 | | const need_cast = !elem_ctype.eql(nav_ctype) and |
| 877 | | (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function); |
| 745 | const elem_ty = ptr_ty.childType(zcu); |
| 746 | const need_cast = elem_ty.toIntern() != nav_ty.toIntern() and |
| 747 | elem_ty.zigTypeTag(zcu) != .@"fn" or nav_ty.zigTypeTag(zcu) != .@"fn"; |
| 878 | 748 | if (need_cast) { |
| 879 | 749 | try w.writeAll("(("); |
| 880 | | try dg.renderCType(w, ctype); |
| 750 | try dg.renderType(w, ptr_ty); |
| 881 | 751 | try w.writeByte(')'); |
| 882 | 752 | } |
| 883 | 753 | try w.writeByte('&'); |
| 884 | | try dg.renderNavName(w, owner_nav); |
| 754 | try renderNavName(w, owner_nav, ip); |
| 885 | 755 | if (need_cast) try w.writeByte(')'); |
| 886 | 756 | } |
| 887 | 757 | |
| ... | ... | @@ -896,11 +766,10 @@ pub const DeclGen = struct { |
| 896 | 766 | switch (derivation) { |
| 897 | 767 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 898 | 768 | .int => |int| { |
| 899 | | const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete); |
| 900 | 769 | const addr_val = try pt.intValue(.usize, int.addr); |
| 901 | 770 | try w.writeByte('('); |
| 902 | | try dg.renderCType(w, ptr_ctype); |
| 903 | | try w.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)}); |
| 771 | try dg.renderType(w, int.ptr_ty); |
| 772 | try w.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .other)}); |
| 904 | 773 | }, |
| 905 | 774 | |
| 906 | 775 | .nav_ptr => |nav| try dg.renderNav(w, nav, location), |
| ... | ... | @@ -915,14 +784,10 @@ pub const DeclGen = struct { |
| 915 | 784 | .field_ptr => |field| { |
| 916 | 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 | 787 | switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, zcu)) { |
| 922 | 788 | .begin => { |
| 923 | | const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete); |
| 924 | 789 | try w.writeByte('('); |
| 925 | | try dg.renderCType(w, ptr_ctype); |
| 790 | try dg.renderType(w, field.result_ptr_ty); |
| 926 | 791 | try w.writeByte(')'); |
| 927 | 792 | try dg.renderPointer(w, field.parent.*, location); |
| 928 | 793 | }, |
| ... | ... | @@ -933,51 +798,40 @@ pub const DeclGen = struct { |
| 933 | 798 | try dg.writeCValue(w, name); |
| 934 | 799 | }, |
| 935 | 800 | .byte_offset => |byte_offset| { |
| 936 | | const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete); |
| 937 | 801 | try w.writeByte('('); |
| 938 | | try dg.renderCType(w, ptr_ctype); |
| 802 | try dg.renderType(w, field.result_ptr_ty); |
| 939 | 803 | try w.writeByte(')'); |
| 940 | 804 | const offset_val = try pt.intValue(.usize, byte_offset); |
| 941 | 805 | try w.writeAll("((char *)"); |
| 942 | 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 | 812 | .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) { |
| 949 | 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 | 814 | try w.writeByte('('); |
| 952 | | try dg.renderCType(w, ptr_ctype); |
| 815 | try dg.renderType(w, elem.result_ptr_ty); |
| 953 | 816 | try w.writeByte(')'); |
| 954 | 817 | try dg.renderPointer(w, elem.parent.*, location); |
| 955 | 818 | } else { |
| 956 | 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. |
| 958 | | // We might have a pointer-to-array. In this case, we must cast first. |
| 959 | | const result_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete); |
| 960 | | const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete); |
| 961 | | if (result_ctype.eql(parent_ctype)) { |
| 962 | | // The pointer already has an appropriate type - just do the arithmetic. |
| 820 | try w.writeByte('('); |
| 821 | // We want to do pointer arithmetic on a pointer to the element type, but the parent |
| 822 | // might be a pointer-to-array, in which case we must cast it. |
| 823 | if (elem.result_ptr_ty.toIntern() != (try elem.parent.ptrType(pt)).toIntern()) { |
| 963 | 824 | try w.writeByte('('); |
| 964 | | try dg.renderPointer(w, elem.parent.*, location); |
| 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); |
| 825 | try dg.renderType(w, elem.result_ptr_ty); |
| 971 | 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 | 832 | .offset_and_cast => |oac| { |
| 978 | | const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete); |
| 979 | 833 | try w.writeByte('('); |
| 980 | | try dg.renderCType(w, ptr_ctype); |
| 834 | try dg.renderType(w, oac.new_ptr_ty); |
| 981 | 835 | try w.writeByte(')'); |
| 982 | 836 | if (oac.byte_offset == 0) { |
| 983 | 837 | try dg.renderPointer(w, oac.parent.*, location); |
| ... | ... | @@ -985,14 +839,40 @@ pub const DeclGen = struct { |
| 985 | 839 | const offset_val = try pt.intValue(.usize, oac.byte_offset); |
| 986 | 840 | try w.writeAll("((char *)"); |
| 987 | 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 { |
| 995 | | try w.print("zig_error_{f}", .{fmtIdentUnsolo(err_name.toSlice(&dg.pt.zcu.intern_pool))}); |
| 848 | fn renderValueAsLvalue( |
| 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 | 878 | fn renderValue( |
| ... | ... | @@ -1005,16 +885,13 @@ pub const DeclGen = struct { |
| 1005 | 885 | const zcu = pt.zcu; |
| 1006 | 886 | const ip = &zcu.intern_pool; |
| 1007 | 887 | const target = &dg.mod.resolved_target.result; |
| 1008 | | const ctype_pool = &dg.ctype_pool; |
| 1009 | 888 | |
| 1010 | 889 | const initializer_type: ValueRenderLocation = switch (location) { |
| 1011 | | .StaticInitializer => .StaticInitializer, |
| 1012 | | else => .Initializer, |
| 890 | .static_initializer => .static_initializer, |
| 891 | else => .initializer, |
| 1013 | 892 | }; |
| 1014 | 893 | |
| 1015 | 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 | 895 | switch (ip.indexToKey(val.toIntern())) { |
| 1019 | 896 | // types, not values |
| 1020 | 897 | .int_type, |
| ... | ... | @@ -1037,7 +914,7 @@ pub const DeclGen = struct { |
| 1037 | 914 | .memoized_call, |
| 1038 | 915 | => unreachable, |
| 1039 | 916 | |
| 1040 | | .undef => unreachable, // handled above |
| 917 | .undef => try dg.renderUndefValue(w, ty, location), |
| 1041 | 918 | .simple_value => |simple_value| switch (simple_value) { |
| 1042 | 919 | // non-runtime values |
| 1043 | 920 | .void => unreachable, |
| ... | ... | @@ -1053,46 +930,28 @@ pub const DeclGen = struct { |
| 1053 | 930 | .enum_literal, |
| 1054 | 931 | => unreachable, // non-runtime values |
| 1055 | 932 | .int => try w.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}), |
| 1056 | | .err => |err| try dg.renderErrorName(w, err.name), |
| 1057 | | .error_union => |error_union| switch (ctype.info(ctype_pool)) { |
| 1058 | | .basic => switch (error_union.val) { |
| 1059 | | .err_name => |err_name| try dg.renderErrorName(w, err_name), |
| 933 | .err => |err| try renderErrorName(w, err.name.toSlice(ip)), |
| 934 | .error_union => |error_union| { |
| 935 | if (!location.isInitializer()) { |
| 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 | 943 | .payload => try w.writeByte('0'), |
| 1061 | | }, |
| 1062 | | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1063 | | .aggregate => |aggregate| { |
| 1064 | | if (!location.isInitializer()) { |
| 1065 | | try w.writeByte('('); |
| 1066 | | try dg.renderCType(w, ctype); |
| 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 | | } |
| 944 | } |
| 945 | if (ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 946 | try w.writeAll(", .payload = "); |
| 947 | switch (error_union.val) { |
| 948 | .err_name => try dg.renderUndefValue(w, ty.errorUnionPayload(zcu), initializer_type), |
| 949 | .payload => |payload| try dg.renderValue(w, .fromInterned(payload), initializer_type), |
| 1091 | 950 | } |
| 1092 | | try w.writeByte('}'); |
| 1093 | | }, |
| 951 | } |
| 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 | 955 | .float => { |
| 1097 | 956 | const bits = ty.floatBits(target); |
| 1098 | 957 | const f128_val = val.toFloat(f128, zcu); |
| ... | ... | @@ -1143,7 +1002,7 @@ pub const DeclGen = struct { |
| 1143 | 1002 | else |
| 1144 | 1003 | unreachable; |
| 1145 | 1004 | |
| 1146 | | if (location == .StaticInitializer) { |
| 1005 | if (location == .static_initializer) { |
| 1147 | 1006 | if (!std.math.isNan(f128_val) and std.math.isSignalNan(f128_val)) |
| 1148 | 1007 | return dg.fail("TODO: C backend: implement nans rendering in static initializers", .{}); |
| 1149 | 1008 | |
| ... | ... | @@ -1154,9 +1013,11 @@ pub const DeclGen = struct { |
| 1154 | 1013 | // return dg.fail("Only quiet nans are supported in global variable initializers", .{}); |
| 1155 | 1014 | } |
| 1156 | 1015 | |
| 1157 | | try w.writeAll("zig_"); |
| 1158 | | try w.writeAll(if (location == .StaticInitializer) "init" else "make"); |
| 1159 | | try w.writeAll("_special_"); |
| 1016 | if (location == .static_initializer) { |
| 1017 | try w.writeAll("zig_init_special_"); |
| 1018 | } else { |
| 1019 | try w.writeAll("zig_make_special_"); |
| 1020 | } |
| 1160 | 1021 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1161 | 1022 | try w.writeByte('('); |
| 1162 | 1023 | if (std.math.signbit(f128_val)) try w.writeByte('-'); |
| ... | ... | @@ -1183,105 +1044,85 @@ pub const DeclGen = struct { |
| 1183 | 1044 | if (!empty) try w.writeByte(')'); |
| 1184 | 1045 | }, |
| 1185 | 1046 | .slice => |slice| { |
| 1186 | | const aggregate = ctype.info(ctype_pool).aggregate; |
| 1187 | 1047 | if (!location.isInitializer()) { |
| 1188 | 1048 | try w.writeByte('('); |
| 1189 | | try dg.renderCType(w, ctype); |
| 1049 | try dg.renderType(w, ty); |
| 1190 | 1050 | try w.writeByte(')'); |
| 1191 | 1051 | } |
| 1192 | 1052 | try w.writeByte('{'); |
| 1193 | | for (0..aggregate.fields.len) |field_index| { |
| 1194 | | if (field_index > 0) try w.writeByte(','); |
| 1195 | | try dg.renderValue(w, Value.fromInterned( |
| 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 | | } |
| 1053 | try dg.renderValue(w, .fromInterned(slice.ptr), initializer_type); |
| 1054 | try w.writeByte(','); |
| 1055 | try dg.renderValue(w, .fromInterned(slice.len), initializer_type); |
| 1203 | 1056 | try w.writeByte('}'); |
| 1204 | 1057 | }, |
| 1205 | 1058 | .ptr => { |
| 1206 | | var arena = std.heap.ArenaAllocator.init(zcu.gpa); |
| 1207 | | defer arena.deinit(); |
| 1208 | | const derivation = try val.pointerDerivation(arena.allocator(), pt, null); |
| 1059 | const derivation = try val.pointerDerivation(dg.arena, pt, null); |
| 1060 | try w.writeByte('('); |
| 1209 | 1061 | try dg.renderPointer(w, derivation, location); |
| 1062 | try w.writeByte(')'); |
| 1210 | 1063 | }, |
| 1211 | | .opt => |opt| switch (ctype.info(ctype_pool)) { |
| 1212 | | .basic => if (ctype.isBool()) try w.writeAll(switch (opt.val) { |
| 1213 | | .none => "true", |
| 1214 | | else => "false", |
| 1215 | | }) else switch (opt.val) { |
| 1064 | .opt => |opt| switch (CType.classifyOptional(ty, zcu)) { |
| 1065 | .npv_payload => unreachable, // opv optional |
| 1066 | .opv_payload => { |
| 1067 | if (!location.isInitializer()) { |
| 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 | 1078 | .none => try w.writeByte('0'), |
| 1217 | | else => |payload| switch (ip.indexToKey(payload)) { |
| 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 | | }, |
| 1079 | else => |payload_val| try dg.renderValue(w, .fromInterned(payload_val), location), |
| 1226 | 1080 | }, |
| 1227 | | .pointer => switch (opt.val) { |
| 1081 | .ptr_like => switch (opt.val) { |
| 1228 | 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, |
| 1232 | | .aggregate => |aggregate| { |
| 1233 | | switch (opt.val) { |
| 1234 | | .none => {}, |
| 1235 | | else => |payload| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 1236 | | .is_null, .payload => {}, |
| 1237 | | .ptr, .len => return dg.renderValue( |
| 1238 | | w, |
| 1239 | | Value.fromInterned(payload), |
| 1240 | | location, |
| 1241 | | ), |
| 1242 | | else => unreachable, |
| 1243 | | }, |
| 1244 | | } |
| 1085 | .slice_like => switch (opt.val) { |
| 1086 | .none => { |
| 1087 | if (!location.isInitializer()) { |
| 1088 | try w.writeByte('('); |
| 1089 | try dg.renderType(w, ty); |
| 1090 | try w.writeByte(')'); |
| 1091 | } |
| 1092 | try w.writeAll("{NULL,"); |
| 1093 | try dg.renderUndefValue(w, .usize, initializer_type); |
| 1094 | try w.writeByte('}'); |
| 1095 | }, |
| 1096 | else => |payload_val| try dg.renderValue(w, .fromInterned(payload_val), location), |
| 1097 | }, |
| 1098 | .@"struct" => { |
| 1245 | 1099 | if (!location.isInitializer()) { |
| 1246 | 1100 | try w.writeByte('('); |
| 1247 | | try dg.renderCType(w, ctype); |
| 1101 | try dg.renderType(w, ty); |
| 1248 | 1102 | try w.writeByte(')'); |
| 1249 | 1103 | } |
| 1250 | | try w.writeByte('{'); |
| 1251 | | for (0..aggregate.fields.len) |field_index| { |
| 1252 | | if (field_index > 0) try w.writeByte(','); |
| 1253 | | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { |
| 1254 | | .is_null => try w.writeAll(switch (opt.val) { |
| 1255 | | .none => "true", |
| 1256 | | else => "false", |
| 1257 | | }), |
| 1258 | | .payload => switch (opt.val) { |
| 1259 | | .none => try dg.renderUndefValue( |
| 1260 | | w, |
| 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 | | } |
| 1104 | switch (opt.val) { |
| 1105 | .none => { |
| 1106 | try w.writeAll("{ .is_null = true, .payload = "); |
| 1107 | try dg.renderUndefValue(w, ty.optionalChild(zcu), initializer_type); |
| 1108 | try w.writeAll(" }"); |
| 1109 | }, |
| 1110 | else => |payload_val| { |
| 1111 | try w.writeAll("{ .is_null = false, .payload = "); |
| 1112 | try dg.renderValue(w, .fromInterned(payload_val), initializer_type); |
| 1113 | try w.writeAll(" }"); |
| 1114 | }, |
| 1274 | 1115 | } |
| 1275 | | try w.writeByte('}'); |
| 1276 | 1116 | }, |
| 1277 | 1117 | }, |
| 1278 | 1118 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { |
| 1279 | 1119 | .array_type, .vector_type => { |
| 1280 | | if (location == .FunctionArgument) { |
| 1120 | if (!location.isInitializer()) { |
| 1281 | 1121 | try w.writeByte('('); |
| 1282 | | try dg.renderCType(w, ctype); |
| 1122 | try dg.renderType(w, ty); |
| 1283 | 1123 | try w.writeByte(')'); |
| 1284 | 1124 | } |
| 1125 | try w.writeByte('{'); |
| 1285 | 1126 | const ai = ty.arrayInfo(zcu); |
| 1286 | 1127 | if (ai.elem_type.eql(.u8, zcu)) { |
| 1287 | 1128 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); |
| ... | ... | @@ -1314,11 +1155,12 @@ pub const DeclGen = struct { |
| 1314 | 1155 | } |
| 1315 | 1156 | try w.writeByte('}'); |
| 1316 | 1157 | } |
| 1158 | try w.writeByte('}'); |
| 1317 | 1159 | }, |
| 1318 | 1160 | .tuple_type => |tuple| { |
| 1319 | 1161 | if (!location.isInitializer()) { |
| 1320 | 1162 | try w.writeByte('('); |
| 1321 | | try dg.renderCType(w, ctype); |
| 1163 | try dg.renderType(w, ty); |
| 1322 | 1164 | try w.writeByte(')'); |
| 1323 | 1165 | } |
| 1324 | 1166 | |
| ... | ... | @@ -1354,7 +1196,7 @@ pub const DeclGen = struct { |
| 1354 | 1196 | |
| 1355 | 1197 | if (!location.isInitializer()) { |
| 1356 | 1198 | try w.writeByte('('); |
| 1357 | | try dg.renderCType(w, ctype); |
| 1199 | try dg.renderType(w, ty); |
| 1358 | 1200 | try w.writeByte(')'); |
| 1359 | 1201 | } |
| 1360 | 1202 | |
| ... | ... | @@ -1385,69 +1227,60 @@ pub const DeclGen = struct { |
| 1385 | 1227 | .un => |un| { |
| 1386 | 1228 | const loaded_union = ip.loadUnionType(ty.toIntern()); |
| 1387 | 1229 | if (un.tag == .none) { |
| 1388 | | const backing_ty = try ty.externUnionBackingType(pt); |
| 1389 | 1230 | assert(loaded_union.layout == .@"extern"); |
| 1390 | | if (location == .StaticInitializer) { |
| 1231 | if (location == .static_initializer) { |
| 1391 | 1232 | return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{}); |
| 1392 | 1233 | } |
| 1393 | 1234 | |
| 1394 | 1235 | const ptr_ty = try pt.singleConstPtrType(ty); |
| 1395 | | try w.writeAll("*(("); |
| 1236 | try w.writeAll("*("); |
| 1396 | 1237 | try dg.renderType(w, ptr_ty); |
| 1397 | | try w.writeAll(")("); |
| 1398 | | try dg.renderType(w, backing_ty); |
| 1399 | | try w.writeAll("){"); |
| 1400 | | try dg.renderValue(w, Value.fromInterned(un.val), location); |
| 1401 | | try w.writeAll("})"); |
| 1238 | try w.writeAll(")&"); |
| 1239 | // We need an lvalue for '&'. |
| 1240 | try dg.renderValueAsLvalue(w, .fromInterned(un.val)); |
| 1402 | 1241 | } else { |
| 1403 | 1242 | if (!location.isInitializer()) { |
| 1404 | 1243 | try w.writeByte('('); |
| 1405 | | try dg.renderCType(w, ctype); |
| 1244 | try dg.renderType(w, ty); |
| 1406 | 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)).?; |
| 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]; |
| 1255 | if (loaded_union.layout == .auto) try w.writeByte('{'); |
| 1412 | 1256 | |
| 1413 | | const has_tag = loaded_union.has_runtime_tag; |
| 1414 | | if (has_tag) try w.writeByte('{'); |
| 1415 | | const aggregate = ctype.info(ctype_pool).aggregate; |
| 1416 | | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { |
| 1417 | | if (outer_field_index > 0) try w.writeByte(','); |
| 1418 | | switch (if (has_tag) |
| 1419 | | aggregate.fields.at(outer_field_index, ctype_pool).name.index |
| 1420 | | else |
| 1421 | | .payload) { |
| 1422 | | .tag => try dg.renderValue( |
| 1423 | | w, |
| 1424 | | Value.fromInterned(un.tag), |
| 1425 | | initializer_type, |
| 1426 | | ), |
| 1427 | | .payload => { |
| 1428 | | try w.writeByte('{'); |
| 1429 | | if (field_ty.hasRuntimeBits(zcu)) { |
| 1430 | | try w.print(" .{f} = ", .{fmtIdentSolo(field_name.toSlice(ip))}); |
| 1431 | | try dg.renderValue( |
| 1432 | | w, |
| 1433 | | Value.fromInterned(un.val), |
| 1434 | | initializer_type, |
| 1435 | | ); |
| 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 | | } |
| 1257 | if (loaded_union.has_runtime_tag) { |
| 1258 | try w.writeAll(" .tag = "); |
| 1259 | try dg.renderValue(w, .fromInterned(un.tag), initializer_type); |
| 1260 | try w.writeAll(", .payload = "); |
| 1261 | } |
| 1262 | |
| 1263 | const enum_tag_ty: Type = .fromInterned(loaded_union.enum_tag_type); |
| 1264 | const active_field_index = enum_tag_ty.enumTagFieldIndex(.fromInterned(un.tag), zcu).?; |
| 1265 | const active_field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[active_field_index]); |
| 1266 | if (active_field_ty.hasRuntimeBits(zcu)) { |
| 1267 | const active_field_name = enum_tag_ty.enumFieldName(active_field_index, zcu); |
| 1268 | try w.print("{{ .{f} = ", .{fmtIdentSolo(active_field_name.toSlice(ip))}); |
| 1269 | try dg.renderValue(w, .fromInterned(un.val), initializer_type); |
| 1270 | try w.writeAll(" }"); |
| 1271 | } else { |
| 1272 | const first_field_ty: Type = for (loaded_union.field_types.get(ip)) |field_ty_ip| { |
| 1273 | const field_ty: Type = .fromInterned(field_ty_ip); |
| 1274 | if (!field_ty.hasRuntimeBits(pt.zcu)) continue; |
| 1275 | break field_ty; |
| 1276 | } else unreachable; |
| 1277 | try w.writeByte('{'); |
| 1278 | try dg.renderUndefValue(w, first_field_ty, initializer_type); |
| 1279 | try w.writeByte('}'); |
| 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 | 1296 | const zcu = pt.zcu; |
| 1464 | 1297 | const ip = &zcu.intern_pool; |
| 1465 | 1298 | const target = &dg.mod.resolved_target.result; |
| 1466 | | const ctype_pool = &dg.ctype_pool; |
| 1467 | 1299 | |
| 1468 | 1300 | const initializer_type: ValueRenderLocation = switch (location) { |
| 1469 | | .StaticInitializer => .StaticInitializer, |
| 1470 | | else => .Initializer, |
| 1301 | .static_initializer => .static_initializer, |
| 1302 | else => .initializer, |
| 1471 | 1303 | }; |
| 1472 | 1304 | |
| 1473 | 1305 | const safety_on = switch (zcu.optimizeMode()) { |
| ... | ... | @@ -1475,7 +1307,6 @@ pub const DeclGen = struct { |
| 1475 | 1307 | .ReleaseFast, .ReleaseSmall => false, |
| 1476 | 1308 | }; |
| 1477 | 1309 | |
| 1478 | | const ctype = try dg.ctypeFromType(ty, location.toCTypeKind()); |
| 1479 | 1310 | switch (ty.toIntern()) { |
| 1480 | 1311 | .c_longdouble_type, |
| 1481 | 1312 | .f16_type, |
| ... | ... | @@ -1500,76 +1331,109 @@ pub const DeclGen = struct { |
| 1500 | 1331 | else => unreachable, |
| 1501 | 1332 | } |
| 1502 | 1333 | try w.writeAll(", "); |
| 1503 | | try dg.renderUndefValue(w, repr_ty, .FunctionArgument); |
| 1334 | try dg.renderUndefValue(w, repr_ty, .other); |
| 1504 | 1335 | return w.writeByte(')'); |
| 1505 | 1336 | }, |
| 1506 | 1337 | .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"), |
| 1507 | 1338 | else => switch (ip.indexToKey(ty.toIntern())) { |
| 1508 | | .simple_type, |
| 1339 | .simple_type, // anyerror, c_char (etc), usize, isize |
| 1509 | 1340 | .int_type, |
| 1510 | 1341 | .enum_type, |
| 1511 | 1342 | .error_set_type, |
| 1512 | 1343 | .inferred_error_set_type, |
| 1513 | | => return w.print("{f}", .{ |
| 1514 | | try dg.fmtIntLiteralHex(try pt.undefValue(ty), location), |
| 1515 | | }), |
| 1344 | => switch (CType.classifyInt(ty, zcu)) { |
| 1345 | .void => unreachable, // opv |
| 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 | 1387 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1517 | 1388 | .one, .many, .c => { |
| 1518 | 1389 | try w.writeAll("(("); |
| 1519 | | try dg.renderCType(w, ctype); |
| 1520 | | return w.print("){f})", .{ |
| 1521 | | try dg.fmtIntLiteralHex(.undef_usize, .Other), |
| 1522 | | }); |
| 1390 | try dg.renderType(w, ty); |
| 1391 | try w.writeByte(')'); |
| 1392 | try dg.renderUndefValue(w, .usize, location); |
| 1393 | try w.writeByte(')'); |
| 1523 | 1394 | }, |
| 1524 | 1395 | .slice => { |
| 1525 | 1396 | if (!location.isInitializer()) { |
| 1526 | 1397 | try w.writeByte('('); |
| 1527 | | try dg.renderCType(w, ctype); |
| 1398 | try dg.renderType(w, ty); |
| 1528 | 1399 | try w.writeByte(')'); |
| 1529 | 1400 | } |
| 1530 | 1401 | |
| 1531 | | try w.writeAll("{("); |
| 1532 | | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 1533 | | try dg.renderType(w, ptr_ty); |
| 1534 | | return w.print("){f}, {0f}}}", .{ |
| 1535 | | try dg.fmtIntLiteralHex(.undef_usize, .Other), |
| 1536 | | }); |
| 1402 | try w.writeByte('{'); |
| 1403 | try dg.renderUndefValue(w, ty.slicePtrFieldType(zcu), initializer_type); |
| 1404 | try w.writeByte(','); |
| 1405 | try dg.renderUndefValue(w, .usize, initializer_type); |
| 1406 | try w.writeByte('}'); |
| 1537 | 1407 | }, |
| 1538 | 1408 | }, |
| 1539 | | .opt_type => |child_type| switch (ctype.info(ctype_pool)) { |
| 1540 | | .basic, .pointer => try dg.renderUndefValue( |
| 1541 | | w, |
| 1542 | | .fromInterned(if (ctype.isBool()) .bool_type else child_type), |
| 1543 | | location, |
| 1544 | | ), |
| 1545 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1546 | | .aggregate => |aggregate| { |
| 1547 | | switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 1548 | | .is_null, .payload => {}, |
| 1549 | | .ptr, .len => return dg.renderUndefValue( |
| 1550 | | w, |
| 1551 | | .fromInterned(child_type), |
| 1552 | | location, |
| 1553 | | ), |
| 1554 | | else => unreachable, |
| 1555 | | } |
| 1409 | .opt_type => |child_type| switch (CType.classifyOptional(ty, zcu)) { |
| 1410 | .npv_payload => unreachable, // opv optional |
| 1411 | |
| 1412 | .error_set, |
| 1413 | .ptr_like, |
| 1414 | .slice_like, |
| 1415 | => try dg.renderUndefValue(w, .fromInterned(child_type), location), |
| 1416 | |
| 1417 | .opv_payload => { |
| 1556 | 1418 | if (!location.isInitializer()) { |
| 1557 | 1419 | try w.writeByte('('); |
| 1558 | | try dg.renderCType(w, ctype); |
| 1420 | try dg.renderType(w, ty); |
| 1559 | 1421 | try w.writeByte(')'); |
| 1560 | 1422 | } |
| 1561 | | try w.writeByte('{'); |
| 1562 | | for (0..aggregate.fields.len) |field_index| { |
| 1563 | | if (field_index > 0) try w.writeByte(','); |
| 1564 | | try dg.renderUndefValue(w, .fromInterned( |
| 1565 | | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { |
| 1566 | | .is_null => .bool_type, |
| 1567 | | .payload => child_type, |
| 1568 | | else => unreachable, |
| 1569 | | }, |
| 1570 | | ), initializer_type); |
| 1423 | try w.writeAll(if (safety_on) "{.is_null=0xaa}" else "{.is_null=false}"); |
| 1424 | }, |
| 1425 | |
| 1426 | .@"struct" => { |
| 1427 | if (!location.isInitializer()) { |
| 1428 | try w.writeByte('('); |
| 1429 | try dg.renderType(w, ty); |
| 1430 | try w.writeByte(')'); |
| 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 | 1439 | .struct_type => { |
| ... | ... | @@ -1578,10 +1442,9 @@ pub const DeclGen = struct { |
| 1578 | 1442 | .auto, .@"extern" => { |
| 1579 | 1443 | if (!location.isInitializer()) { |
| 1580 | 1444 | try w.writeByte('('); |
| 1581 | | try dg.renderCType(w, ctype); |
| 1445 | try dg.renderType(w, ty); |
| 1582 | 1446 | try w.writeByte(')'); |
| 1583 | 1447 | } |
| 1584 | | |
| 1585 | 1448 | try w.writeByte('{'); |
| 1586 | 1449 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| 1587 | 1450 | var need_comma = false; |
| ... | ... | @@ -1601,7 +1464,7 @@ pub const DeclGen = struct { |
| 1601 | 1464 | .tuple_type => |tuple_info| { |
| 1602 | 1465 | if (!location.isInitializer()) { |
| 1603 | 1466 | try w.writeByte('('); |
| 1604 | | try dg.renderCType(w, ctype); |
| 1467 | try dg.renderType(w, ty); |
| 1605 | 1468 | try w.writeByte(')'); |
| 1606 | 1469 | } |
| 1607 | 1470 | |
| ... | ... | @@ -1624,80 +1487,61 @@ pub const DeclGen = struct { |
| 1624 | 1487 | .auto, .@"extern" => { |
| 1625 | 1488 | if (!location.isInitializer()) { |
| 1626 | 1489 | try w.writeByte('('); |
| 1627 | | try dg.renderCType(w, ctype); |
| 1490 | try dg.renderType(w, ty); |
| 1628 | 1491 | try w.writeByte(')'); |
| 1629 | 1492 | } |
| 1630 | 1493 | |
| 1631 | | const has_tag = loaded_union.has_runtime_tag; |
| 1632 | | if (has_tag) try w.writeByte('{'); |
| 1633 | | const aggregate = ctype.info(ctype_pool).aggregate; |
| 1634 | | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { |
| 1635 | | if (outer_field_index > 0) try w.writeByte(','); |
| 1636 | | switch (if (has_tag) |
| 1637 | | aggregate.fields.at(outer_field_index, ctype_pool).name.index |
| 1638 | | else |
| 1639 | | .payload) { |
| 1640 | | .tag => try dg.renderUndefValue( |
| 1641 | | w, |
| 1642 | | .fromInterned(loaded_union.enum_tag_type), |
| 1643 | | initializer_type, |
| 1644 | | ), |
| 1645 | | .payload => { |
| 1646 | | try w.writeByte('{'); |
| 1647 | | for (0..loaded_union.field_types.len) |inner_field_index| { |
| 1648 | | const inner_field_ty: Type = .fromInterned( |
| 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 | | } |
| 1494 | const first_field_ty: Type = for (loaded_union.field_types.get(ip)) |field_ty_ip| { |
| 1495 | const field_ty: Type = .fromInterned(field_ty_ip); |
| 1496 | if (!field_ty.hasRuntimeBits(pt.zcu)) continue; |
| 1497 | break field_ty; |
| 1498 | } else { |
| 1499 | assert(loaded_union.has_runtime_tag); // otherwise it does not have runtime bits |
| 1500 | try w.writeAll("{ .tag = "); |
| 1501 | try dg.renderUndefValue(w, .fromInterned(loaded_union.enum_tag_type), initializer_type); |
| 1502 | try w.writeAll(" }"); |
| 1503 | return; |
| 1504 | }; |
| 1505 | |
| 1506 | if (loaded_union.layout == .auto) try w.writeByte('{'); |
| 1507 | |
| 1508 | if (loaded_union.has_runtime_tag) { |
| 1509 | try w.writeAll(" .tag = "); |
| 1510 | try dg.renderUndefValue(w, .fromInterned(loaded_union.enum_tag_type), initializer_type); |
| 1511 | try w.writeAll(", .payload = "); |
| 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 | 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)) { |
| 1670 | | .basic => try dg.renderUndefValue( |
| 1671 | | w, |
| 1672 | | .fromInterned(error_union_type.error_set_type), |
| 1673 | | location, |
| 1674 | | ), |
| 1675 | | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1676 | | .aggregate => |aggregate| { |
| 1677 | | if (!location.isInitializer()) { |
| 1678 | | try w.writeByte('('); |
| 1679 | | try dg.renderCType(w, ctype); |
| 1680 | | try w.writeByte(')'); |
| 1681 | | } |
| 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 | | }, |
| 1524 | .error_union_type => |error_union| { |
| 1525 | if (!location.isInitializer()) { |
| 1526 | try w.writeByte('('); |
| 1527 | try dg.renderType(w, ty); |
| 1528 | try w.writeByte(')'); |
| 1529 | } |
| 1530 | try w.writeAll("{ .error = "); |
| 1531 | try dg.renderUndefValue(w, .fromInterned(error_union.error_set_type), initializer_type); |
| 1532 | if (Type.fromInterned(error_union.payload_type).hasRuntimeBits(zcu)) { |
| 1533 | try w.writeAll(", .payload = "); |
| 1534 | try dg.renderUndefValue(w, .fromInterned(error_union.payload_type), initializer_type); |
| 1535 | } |
| 1536 | try w.writeAll(" }"); |
| 1699 | 1537 | }, |
| 1700 | 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 | 1545 | const ai = ty.arrayInfo(zcu); |
| 1702 | 1546 | if (ai.elem_type.eql(.u8, zcu)) { |
| 1703 | 1547 | var literal: StringLiteral = .init(w, @intCast(ty.arrayLenIncludingSentinel(zcu))); |
| ... | ... | @@ -1708,14 +1552,8 @@ pub const DeclGen = struct { |
| 1708 | 1552 | const s_u8: u8 = @intCast(s.toUnsignedInt(zcu)); |
| 1709 | 1553 | if (s_u8 != 0) try literal.writeChar(s_u8); |
| 1710 | 1554 | } |
| 1711 | | return literal.end(); |
| 1555 | try literal.end(); |
| 1712 | 1556 | } else { |
| 1713 | | if (!location.isInitializer()) { |
| 1714 | | try w.writeByte('('); |
| 1715 | | try dg.renderCType(w, ctype); |
| 1716 | | try w.writeByte(')'); |
| 1717 | | } |
| 1718 | | |
| 1719 | 1557 | try w.writeByte('{'); |
| 1720 | 1558 | var index: u64 = 0; |
| 1721 | 1559 | while (index < ai.len) : (index += 1) { |
| ... | ... | @@ -1726,8 +1564,9 @@ pub const DeclGen = struct { |
| 1726 | 1564 | if (index > 0) try w.writeAll(", "); |
| 1727 | 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 | 1571 | .anyframe_type, |
| 1733 | 1572 | .opaque_type, |
| ... | ... | @@ -1762,10 +1601,11 @@ pub const DeclGen = struct { |
| 1762 | 1601 | w: *Writer, |
| 1763 | 1602 | fn_val: Value, |
| 1764 | 1603 | fn_align: InternPool.Alignment, |
| 1765 | | kind: CType.Kind, |
| 1604 | kind: enum { forward_decl, definition }, |
| 1766 | 1605 | name: union(enum) { |
| 1767 | 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 | 1609 | @"export": struct { |
| 1770 | 1610 | main_name: InternPool.NullTerminatedString, |
| 1771 | 1611 | extern_name: InternPool.NullTerminatedString, |
| ... | ... | @@ -1776,14 +1616,12 @@ pub const DeclGen = struct { |
| 1776 | 1616 | const ip = &zcu.intern_pool; |
| 1777 | 1617 | |
| 1778 | 1618 | const fn_ty = fn_val.typeOf(zcu); |
| 1779 | | const fn_ctype = try dg.ctypeFromType(fn_ty, kind); |
| 1780 | 1619 | |
| 1781 | 1620 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 1782 | 1621 | if (fn_info.cc == .naked) { |
| 1783 | 1622 | switch (kind) { |
| 1784 | | .forward => try w.writeAll("zig_naked_decl "), |
| 1785 | | .complete => try w.writeAll("zig_naked "), |
| 1786 | | else => unreachable, |
| 1623 | .forward_decl => try w.writeAll("zig_naked_decl "), |
| 1624 | .definition => try w.writeAll("zig_naked "), |
| 1787 | 1625 | } |
| 1788 | 1626 | } |
| 1789 | 1627 | |
| ... | ... | @@ -1793,45 +1631,63 @@ pub const DeclGen = struct { |
| 1793 | 1631 | if (func_analysis.branch_hint == .cold) |
| 1794 | 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 | 1635 | try w.writeAll("zig_no_builtin "); |
| 1798 | 1636 | } |
| 1799 | 1637 | |
| 1800 | 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 | 1651 | if (toCallingConvention(fn_info.cc, zcu)) |call_conv| { |
| 1805 | | try w.print("{f}zig_callconv({s})", .{ trailing, call_conv }); |
| 1806 | | trailing = .maybe_space; |
| 1652 | try w.print("zig_callconv({s}) ", .{call_conv}); |
| 1807 | 1653 | } |
| 1808 | | |
| 1809 | | try w.print("{f}", .{trailing}); |
| 1810 | 1654 | switch (name) { |
| 1811 | | .nav => |nav| try dg.renderNavName(w, nav), |
| 1812 | | .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}), |
| 1655 | .nav => |nav| try renderNavName(w, nav, ip), |
| 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 | 1662 | .@"export" => |@"export"| try w.print("{f}", .{fmtIdentSolo(@"export".extern_name.toSlice(ip))}), |
| 1814 | 1663 | } |
| 1815 | | |
| 1816 | | try renderTypeSuffix( |
| 1817 | | dg.pass, |
| 1818 | | &dg.ctype_pool, |
| 1819 | | zcu, |
| 1820 | | w, |
| 1821 | | fn_ctype, |
| 1822 | | .suffix, |
| 1823 | | CQualifiers.init(.{ .@"const" = switch (kind) { |
| 1824 | | .forward => false, |
| 1825 | | .complete => true, |
| 1826 | | else => unreachable, |
| 1827 | | } }), |
| 1828 | | ); |
| 1664 | { |
| 1665 | try w.writeByte('('); |
| 1666 | var c_param_index: u32 = 0; |
| 1667 | for (fn_info.param_types.get(ip)) |param_ty_ip| { |
| 1668 | const param_ty: Type = .fromInterned(param_ty_ip); |
| 1669 | if (!param_ty.hasRuntimeBits(zcu)) continue; |
| 1670 | if (c_param_index != 0) try w.writeAll(", "); |
| 1671 | try dg.renderTypeAndName(w, param_ty, .{ .arg = c_param_index }, .{ |
| 1672 | .@"const" = kind == .definition, |
| 1673 | }, .none); |
| 1674 | c_param_index += 1; |
| 1675 | } |
| 1676 | if (fn_info.is_var_args) { |
| 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 | 1686 | switch (kind) { |
| 1831 | | .forward => { |
| 1687 | .forward_decl => { |
| 1832 | 1688 | if (fn_align.toByteUnits()) |a| try w.print(" zig_align_fn({})", .{a}); |
| 1833 | 1689 | switch (name) { |
| 1834 | | .nav, .fmt_ctype_pool_string => {}, |
| 1690 | .nav, .nav_never_tail, .nav_never_inline => {}, |
| 1835 | 1691 | .@"export" => |@"export"| { |
| 1836 | 1692 | const extern_name = @"export".extern_name.toSlice(ip); |
| 1837 | 1693 | const is_mangled = isMangledIdent(extern_name, true); |
| ... | ... | @@ -1855,38 +1711,16 @@ pub const DeclGen = struct { |
| 1855 | 1711 | }, |
| 1856 | 1712 | } |
| 1857 | 1713 | }, |
| 1858 | | .complete => {}, |
| 1859 | | else => unreachable, |
| 1714 | .definition => {}, |
| 1860 | 1715 | } |
| 1861 | 1716 | } |
| 1862 | 1717 | |
| 1863 | | fn ctypeFromType(dg: *DeclGen, ty: Type, kind: CType.Kind) !CType { |
| 1864 | | defer std.debug.assert(dg.scratch.items.len == 0); |
| 1865 | | return dg.ctype_pool.fromType(dg.gpa, &dg.scratch, ty, dg.pt, dg.mod, kind); |
| 1866 | | } |
| 1867 | | |
| 1868 | | fn byteSize(dg: *DeclGen, ctype: CType) u64 { |
| 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, .{}); |
| 1718 | /// Renders the C lowering of the given Zig type to `w`. This renders the type name---to render |
| 1719 | /// a declarator with this type, see instead `renderTypeAndName`. |
| 1720 | fn renderType(dg: *DeclGen, w: *Writer, ty: Type) (Writer.Error || Allocator.Error)!void { |
| 1721 | const zcu = dg.pt.zcu; |
| 1722 | const cty: CType = try .lower(ty, &dg.ctype_deps, dg.arena, zcu); |
| 1723 | try w.print("{f}", .{cty.fmtTypeName(zcu)}); |
| 1890 | 1724 | } |
| 1891 | 1725 | |
| 1892 | 1726 | const IntCastContext = union(enum) { |
| ... | ... | @@ -1990,7 +1824,7 @@ pub const DeclGen = struct { |
| 1990 | 1824 | try w.writeAll("zig_lo_"); |
| 1991 | 1825 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 1992 | 1826 | try w.writeByte('('); |
| 1993 | | try context.writeValue(dg, w, .FunctionArgument); |
| 1827 | try context.writeValue(dg, w, .other); |
| 1994 | 1828 | try w.writeByte(')'); |
| 1995 | 1829 | } else if (dest_bits > 64 and src_bits <= 64) { |
| 1996 | 1830 | try w.writeAll("zig_make_"); |
| ... | ... | @@ -2001,7 +1835,7 @@ pub const DeclGen = struct { |
| 2001 | 1835 | try dg.renderType(w, src_eff_ty); |
| 2002 | 1836 | try w.writeByte(')'); |
| 2003 | 1837 | } |
| 2004 | | try context.writeValue(dg, w, .FunctionArgument); |
| 1838 | try context.writeValue(dg, w, .other); |
| 2005 | 1839 | try w.writeByte(')'); |
| 2006 | 1840 | } else { |
| 2007 | 1841 | assert(!src_is_ptr); |
| ... | ... | @@ -2010,23 +1844,16 @@ pub const DeclGen = struct { |
| 2010 | 1844 | try w.writeAll("(zig_hi_"); |
| 2011 | 1845 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 2012 | 1846 | try w.writeByte('('); |
| 2013 | | try context.writeValue(dg, w, .FunctionArgument); |
| 1847 | try context.writeValue(dg, w, .other); |
| 2014 | 1848 | try w.writeAll("), zig_lo_"); |
| 2015 | 1849 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 2016 | 1850 | try w.writeByte('('); |
| 2017 | | try context.writeValue(dg, w, .FunctionArgument); |
| 1851 | try context.writeValue(dg, w, .other); |
| 2018 | 1852 | try w.writeAll("))"); |
| 2019 | 1853 | } |
| 2020 | 1854 | } |
| 2021 | 1855 | |
| 2022 | | /// Renders a type and name in field declaration/definition format. |
| 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 | | /// |
| 1856 | /// Renders to `w` a C declarator whose type is the C lowering of the given Zig type. |
| 2030 | 1857 | fn renderTypeAndName( |
| 2031 | 1858 | dg: *DeclGen, |
| 2032 | 1859 | w: *Writer, |
| ... | ... | @@ -2034,73 +1861,47 @@ pub const DeclGen = struct { |
| 2034 | 1861 | name: CValue, |
| 2035 | 1862 | qualifiers: CQualifiers, |
| 2036 | 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 | 1864 | ) !void { |
| 2059 | 1865 | const zcu = dg.pt.zcu; |
| 2060 | | switch (alignas.abiOrder()) { |
| 2061 | | .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}), |
| 1866 | const ip = &zcu.intern_pool; |
| 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 | 1871 | .eq => {}, |
| 2063 | | .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}), |
| 2064 | | } |
| 2065 | | |
| 2066 | | try w.print("{f}", .{ |
| 2067 | | try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers), |
| 2068 | | }); |
| 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) { |
| 1872 | .gt => try w.print("zig_align({d}) ", .{alignment.toByteUnits().?}), |
| 1873 | }; |
| 1874 | if (qualifiers.@"const") try w.writeAll("const "); |
| 1875 | if (qualifiers.@"volatile") try w.writeAll("volatile "); |
| 1876 | if (qualifiers.restrict) try w.writeAll("restrict "); |
| 1877 | switch (name) { |
| 2076 | 1878 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| 1879 | .arg => |i| try w.print("a{d}", .{i}), |
| 2077 | 1880 | .constant => |uav| try renderUavName(w, uav), |
| 2078 | | .nav => |nav| try dg.renderNavName(w, nav), |
| 1881 | .nav => |nav| try renderNavName(w, nav, ip), |
| 2079 | 1882 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), |
| 2080 | 1883 | else => unreachable, |
| 2081 | 1884 | } |
| 1885 | try w.print("{f}", .{cty.fmtDeclaratorSuffix(zcu)}); |
| 2082 | 1886 | } |
| 2083 | 1887 | |
| 2084 | 1888 | fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) Error!void { |
| 2085 | 1889 | switch (c_value) { |
| 2086 | 1890 | .none, .new_local, .local, .local_ref => unreachable, |
| 2087 | 1891 | .constant => |uav| try renderUavName(w, uav), |
| 2088 | | .arg, .arg_array => unreachable, |
| 1892 | .arg => unreachable, |
| 2089 | 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 | 1895 | .nav_ref => |nav| { |
| 2092 | 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 | 1900 | .identifier => |ident| try w.print("{f}", .{fmtIdentSolo(ident)}), |
| 2097 | 1901 | .payload_identifier => |ident| try w.print("{f}.{f}", .{ |
| 2098 | 1902 | fmtIdentSolo("payload"), |
| 2099 | 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 | 1913 | .local_ref, |
| 2113 | 1914 | .constant, |
| 2114 | 1915 | .arg, |
| 2115 | | .arg_array, |
| 2116 | | .ctype_pool_string, |
| 2117 | 1916 | => unreachable, |
| 2118 | 1917 | .field => |i| try w.print("f{d}", .{i}), |
| 2119 | 1918 | .nav => |nav| { |
| 2120 | 1919 | try w.writeAll("(*"); |
| 2121 | | try dg.renderNavName(w, nav); |
| 1920 | try renderNavName(w, nav, &dg.pt.zcu.intern_pool); |
| 2122 | 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 | 1924 | .undef => unreachable, |
| 2126 | 1925 | .identifier => |ident| try w.print("(*{f})", .{fmtIdentSolo(ident)}), |
| 2127 | 1926 | .payload_identifier => |ident| try w.print("(*{f}.{f})", .{ |
| ... | ... | @@ -2157,8 +1956,6 @@ pub const DeclGen = struct { |
| 2157 | 1956 | .field, |
| 2158 | 1957 | .undef, |
| 2159 | 1958 | .arg, |
| 2160 | | .arg_array, |
| 2161 | | .ctype_pool_string, |
| 2162 | 1959 | => unreachable, |
| 2163 | 1960 | .nav, .identifier, .payload_identifier => { |
| 2164 | 1961 | try dg.writeCValue(w, c_value); |
| ... | ... | @@ -2172,101 +1969,36 @@ pub const DeclGen = struct { |
| 2172 | 1969 | try dg.writeCValue(w, member); |
| 2173 | 1970 | } |
| 2174 | 1971 | |
| 2175 | | fn renderFwdDecl( |
| 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 { |
| 1972 | fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void { |
| 2185 | 1973 | const zcu = dg.pt.zcu; |
| 2186 | | const ip = &zcu.intern_pool; |
| 2187 | | const nav = ip.getNav(nav_index); |
| 2188 | | const fwd = &dg.fwd_decl.writer; |
| 2189 | | try fwd.writeAll(switch (flags.linkage) { |
| 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?", .{}), |
| 1974 | switch (ty.zigTypeTag(zcu)) { |
| 1975 | .bool => return w.writeAll("u8"), |
| 1976 | .float => return w.print("f{d}", .{ty.floatBits(zcu.getTarget())}), |
| 1977 | else => {}, |
| 2197 | 1978 | } |
| 2198 | | switch (flags.linkage) { |
| 2199 | | .internal => {}, |
| 2200 | | .strong, .weak, .link_once => try fwd.print("zig_visibility({s}) ", .{@tagName(flags.visibility)}), |
| 1979 | if (ty.isPtrAtRuntime(zcu)) { |
| 1980 | return w.print("p{d}", .{zcu.getTarget().ptrBitWidth()}); |
| 2201 | 1981 | } |
| 2202 | | if (flags.is_threadlocal and !dg.mod.single_threaded) try fwd.writeAll("zig_threadlocal "); |
| 2203 | | try dg.renderTypeAndName( |
| 2204 | | fwd, |
| 2205 | | .fromInterned(nav.typeOf(ip)), |
| 2206 | | .{ .nav = nav_index }, |
| 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, |
| 1982 | switch (CType.classifyInt(ty, zcu)) { |
| 1983 | .void => unreachable, // opv |
| 1984 | .small => try w.print("{c}{d}", .{ |
| 1985 | signAbbrev(ty.intInfo(zcu).signedness), |
| 1986 | ty.abiSize(zcu) * 8, |
| 2255 | 1987 | }), |
| 2256 | | .array => try w.writeAll("big"), |
| 1988 | .big => try w.writeAll("big"), |
| 2257 | 1989 | } |
| 2258 | 1990 | } |
| 2259 | 1991 | |
| 2260 | 1992 | fn renderBuiltinInfo(dg: *DeclGen, w: *Writer, ty: Type, info: BuiltinInfo) !void { |
| 2261 | | const ctype = try dg.ctypeFromType(ty, .complete); |
| 2262 | | const is_big = ctype.info(&dg.ctype_pool) == .array; |
| 1993 | const pt = dg.pt; |
| 1994 | const zcu = pt.zcu; |
| 1995 | |
| 1996 | const is_big = lowersToBigInt(ty, zcu); |
| 2263 | 1997 | switch (info) { |
| 2264 | 1998 | .none => if (!is_big) return, |
| 2265 | 1999 | .bits => {}, |
| 2266 | 2000 | } |
| 2267 | 2001 | |
| 2268 | | const pt = dg.pt; |
| 2269 | | const zcu = pt.zcu; |
| 2270 | 2002 | const int_info: std.builtin.Type.Int = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else .{ |
| 2271 | 2003 | .signedness = .unsigned, |
| 2272 | 2004 | .bits = @intCast(ty.bitSize(zcu)), |
| ... | ... | @@ -2275,7 +2007,7 @@ pub const DeclGen = struct { |
| 2275 | 2007 | if (is_big) try w.print(", {}", .{int_info.signedness == .signed}); |
| 2276 | 2008 | try w.print(", {f}", .{try dg.fmtIntLiteralDec( |
| 2277 | 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 | 2018 | base: u8, |
| 2287 | 2019 | case: std.fmt.Case, |
| 2288 | 2020 | ) !std.fmt.Alt(FormatIntLiteralContext, formatIntLiteral) { |
| 2289 | | const zcu = dg.pt.zcu; |
| 2290 | | const kind = loc.toCTypeKind(); |
| 2291 | | const ty = val.typeOf(zcu); |
| 2021 | // If there's a bigint type involved, mark a dependency on it. |
| 2022 | const cty: CType = try .lower(val.typeOf(dg.pt.zcu), &dg.ctype_deps, dg.arena, dg.pt.zcu); |
| 2292 | 2023 | return .{ .data = .{ |
| 2293 | 2024 | .dg = dg, |
| 2294 | | .int_info = ty.intInfo(zcu), |
| 2295 | | .kind = kind, |
| 2296 | | .ctype = try dg.ctypeFromType(ty, kind), |
| 2025 | .loc = loc, |
| 2297 | 2026 | .val = val, |
| 2027 | .cty = cty, |
| 2298 | 2028 | .base = base, |
| 2299 | 2029 | .case = case, |
| 2300 | 2030 | } }; |
| ... | ... | @@ -2317,339 +2047,11 @@ pub const DeclGen = struct { |
| 2317 | 2047 | } |
| 2318 | 2048 | }; |
| 2319 | 2049 | |
| 2320 | | const CTypeFix = enum { prefix, suffix }; |
| 2321 | | const CQualifiers = std.enums.EnumSet(enum { @"const", @"volatile", restrict }); |
| 2322 | | const Const = CQualifiers.init(.{ .@"const" = true }); |
| 2323 | | const RenderCTypeTrailing = enum { |
| 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 | | } |
| 2050 | const CQualifiers = packed struct { |
| 2051 | @"const": bool = false, |
| 2052 | @"volatile": bool = false, |
| 2053 | restrict: bool = false, |
| 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 | 2056 | pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { |
| 2655 | 2057 | for (zcu.global_assembly.values()) |asm_source| { |
| ... | ... | @@ -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 { |
| 2661 | | const pt = o.dg.pt; |
| 2662 | | const zcu = pt.zcu; |
| 2062 | pub fn genErrDecls( |
| 2063 | zcu: *const Zcu, |
| 2064 | w: *Writer, |
| 2065 | slice_const_u8_sentinel_0_type_name: []const u8, |
| 2066 | ) Writer.Error!void { |
| 2663 | 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 | 2069 | const names = ip.global_error_set.getNamesFromMainThread(); |
| 2070 | // Don't generate an invalid empty enum if the global error set is empty! |
| 2669 | 2071 | if (names.len > 0) { |
| 2670 | | try w.writeAll("enum {"); |
| 2671 | | o.indent(); |
| 2672 | | try o.newline(); |
| 2072 | try w.writeAll("enum {\n"); |
| 2673 | 2073 | for (names, 1..) |name_nts, value| { |
| 2674 | | const name = name_nts.toSlice(ip); |
| 2675 | | max_name_len = @max(name.len, max_name_len); |
| 2676 | | const err_val = try pt.intern(.{ .err = .{ |
| 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(); |
| 2074 | try w.writeByte(' '); |
| 2075 | try renderErrorName(w, name_nts.toSlice(ip)); |
| 2076 | try w.print(" = {d}u,\n", .{value}); |
| 2683 | 2077 | } |
| 2684 | | try o.outdent(); |
| 2685 | | try w.writeAll("};"); |
| 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 | | } }); |
| 2078 | try w.writeAll("};\n"); |
| 2079 | } |
| 2708 | 2080 | |
| 2709 | | try w.writeAll("static "); |
| 2710 | | try o.dg.renderTypeAndName( |
| 2711 | | w, |
| 2712 | | name_ty, |
| 2713 | | .{ .identifier = identifier }, |
| 2714 | | Const, |
| 2715 | | .none, |
| 2716 | | .complete, |
| 2081 | for (names) |name_nts| { |
| 2082 | const name = name_nts.toSlice(ip); |
| 2083 | try w.print( |
| 2084 | "static uint8_t const zig_errorName_{f}[] = {f};\n", |
| 2085 | .{ fmtIdentUnsolo(name), fmtStringLiteral(name, 0) }, |
| 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(.{ |
| 2725 | | .len = 1 + names.len, |
| 2726 | | .child = .slice_const_u8_sentinel_0_type, |
| 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, |
| 2089 | try w.print( |
| 2090 | "static {s} const zig_errorName[{d}] = {{", |
| 2091 | .{ slice_const_u8_sentinel_0_type_name, names.len }, |
| 2737 | 2092 | ); |
| 2738 | | try w.writeAll(" = {"); |
| 2739 | | for (names, 1..) |name_nts, val| { |
| 2093 | if (names.len > 0) try w.writeByte('\n'); |
| 2094 | for (names) |name_nts| { |
| 2740 | 2095 | const name = name_nts.toSlice(ip); |
| 2741 | | if (val > 1) try w.writeAll(", "); |
| 2742 | | try w.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{ |
| 2743 | | fmtIdentUnsolo(name), |
| 2744 | | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer), |
| 2096 | try w.print( |
| 2097 | " {{zig_errorName_{f},{d}}},\n", |
| 2098 | .{ fmtIdentUnsolo(name), name.len }, |
| 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("};"); |
| 2748 | | try o.newline(); |
| 2145 | try w.writeAll( |
| 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 { |
| 2752 | | const pt = o.dg.pt; |
| 2753 | | const zcu = pt.zcu; |
| 2153 | pub fn genLazyCallModifierFn( |
| 2154 | dg: *DeclGen, |
| 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 | 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}: {{", .{ |
| 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"); |
| 2162 | const fn_val = zcu.navValue(fn_nav); |
| 2832 | 2163 | |
| 2833 | | try w.print("zig_{s} ", .{@tagName(key)}); |
| 2834 | | try o.dg.renderFunctionSignature(w, fn_val, .none, .complete, .{ |
| 2835 | | .fmt_ctype_pool_string = fn_name, |
| 2836 | | }); |
| 2837 | | try w.writeAll(" {"); |
| 2838 | | o.indent(); |
| 2839 | | try o.newline(); |
| 2840 | | try w.writeAll("return "); |
| 2841 | | try o.dg.renderNavName(w, fn_nav_index); |
| 2842 | | try w.writeByte('('); |
| 2843 | | for (0..fn_info.param_ctypes.len) |arg| { |
| 2844 | | if (arg > 0) try w.writeAll(", "); |
| 2845 | | try w.print("a{d}", .{arg}); |
| 2846 | | } |
| 2847 | | try w.writeAll(");"); |
| 2848 | | try o.newline(); |
| 2849 | | try o.outdent(); |
| 2850 | | try w.writeByte('}'); |
| 2851 | | try o.newline(); |
| 2852 | | }, |
| 2164 | try w.print("static zig_{t} ", .{kind}); |
| 2165 | try dg.renderFunctionSignature(w, fn_val, .none, .definition, switch (kind) { |
| 2166 | .never_tail => .{ .nav_never_tail = fn_nav }, |
| 2167 | .never_inline => .{ .nav_never_inline = fn_nav }, |
| 2168 | }); |
| 2169 | try w.writeAll(" {\n return "); |
| 2170 | try renderNavName(w, fn_nav, ip); |
| 2171 | try w.writeByte('('); |
| 2172 | { |
| 2173 | const func_type = ip.indexToKey(fn_val.typeOf(zcu).toIntern()).func_type; |
| 2174 | var c_param_index: u32 = 0; |
| 2175 | for (func_type.param_types.get(ip)) |param_ty_ip| { |
| 2176 | const param_ty: Type = .fromInterned(param_ty_ip); |
| 2177 | if (!param_ty.hasRuntimeBits(zcu)) continue; |
| 2178 | if (c_param_index != 0) try w.writeAll(", "); |
| 2179 | try w.print("a{d}", .{c_param_index}); |
| 2180 | c_param_index += 1; |
| 2181 | } |
| 2853 | 2182 | } |
| 2183 | try w.writeAll(");\n}\n"); |
| 2854 | 2184 | } |
| 2855 | 2185 | |
| 2856 | 2186 | pub fn generate( |
| ... | ... | @@ -2869,110 +2199,109 @@ pub fn generate( |
| 2869 | 2199 | |
| 2870 | 2200 | const func = zcu.funcInfo(func_index); |
| 2871 | 2201 | |
| 2202 | var arena: std.heap.ArenaAllocator = .init(gpa); |
| 2203 | defer arena.deinit(); |
| 2204 | |
| 2872 | 2205 | var function: Function = .{ |
| 2873 | 2206 | .value_map = .init(gpa), |
| 2874 | 2207 | .air = air.*, |
| 2875 | 2208 | .liveness = liveness.*.?, |
| 2876 | 2209 | .func_index = func_index, |
| 2877 | | .object = .{ |
| 2878 | | .dg = .{ |
| 2879 | | .gpa = gpa, |
| 2880 | | .pt = pt, |
| 2881 | | .mod = zcu.navFileScope(func.owner_nav).mod.?, |
| 2882 | | .error_msg = null, |
| 2883 | | .pass = .{ .nav = func.owner_nav }, |
| 2884 | | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, |
| 2885 | | .expected_block = null, |
| 2886 | | .fwd_decl = .init(gpa), |
| 2887 | | .ctype_pool = .empty, |
| 2888 | | .scratch = .empty, |
| 2889 | | .uavs = .empty, |
| 2890 | | }, |
| 2891 | | .code_header = .init(gpa), |
| 2892 | | .code = .init(gpa), |
| 2893 | | .indent_counter = 0, |
| 2210 | .dg = .{ |
| 2211 | .gpa = gpa, |
| 2212 | .arena = arena.allocator(), |
| 2213 | .pt = pt, |
| 2214 | .mod = zcu.navFileScope(func.owner_nav).mod.?, |
| 2215 | .error_msg = null, |
| 2216 | .owner_nav = func.owner_nav.toOptional(), |
| 2217 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, |
| 2218 | .expected_block = null, |
| 2219 | .ctype_deps = .empty, |
| 2220 | .uavs = .empty, |
| 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 | 2228 | defer { |
| 2898 | | function.object.code_header.deinit(); |
| 2899 | | function.object.code.deinit(); |
| 2900 | | function.object.dg.fwd_decl.deinit(); |
| 2901 | | function.object.dg.ctype_pool.deinit(gpa); |
| 2902 | | function.object.dg.scratch.deinit(gpa); |
| 2903 | | function.object.dg.uavs.deinit(gpa); |
| 2229 | function.code.deinit(); |
| 2230 | function.dg.ctype_deps.deinit(gpa); |
| 2231 | function.dg.uavs.deinit(gpa); |
| 2904 | 2232 | function.deinit(); |
| 2905 | 2233 | } |
| 2906 | | try function.object.dg.ctype_pool.init(gpa); |
| 2907 | 2234 | |
| 2908 | | genFunc(&function) catch |err| switch (err) { |
| 2909 | | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.object.dg.error_msg.?), |
| 2910 | | error.OutOfMemory => return error.OutOfMemory, |
| 2235 | var fwd_decl: Writer.Allocating = .init(gpa); |
| 2236 | defer fwd_decl.deinit(); |
| 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 | 2243 | error.WriteFailed => return error.OutOfMemory, |
| 2244 | error.OutOfMemory => |e| return e, |
| 2912 | 2245 | }; |
| 2913 | 2246 | |
| 2914 | 2247 | var mir: Mir = .{ |
| 2915 | | .uavs = .empty, |
| 2916 | | .code = &.{}, |
| 2917 | | .code_header = &.{}, |
| 2918 | 2248 | .fwd_decl = &.{}, |
| 2919 | | .ctype_pool = .empty, |
| 2920 | | .lazy_fns = .empty, |
| 2249 | .code_header = &.{}, |
| 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 | 2257 | errdefer mir.deinit(gpa); |
| 2923 | | mir.uavs = function.object.dg.uavs.move(); |
| 2924 | | mir.code_header = try function.object.code_header.toOwnedSlice(); |
| 2925 | | mir.code = try function.object.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(); |
| 2258 | mir.fwd_decl = try fwd_decl.toOwnedSlice(); |
| 2259 | mir.code_header = try code_header.toOwnedSlice(); |
| 2260 | mir.code = try function.code.toOwnedSlice(); |
| 2929 | 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 | 2265 | const tracy = trace(@src()); |
| 2934 | 2266 | defer tracy.end(); |
| 2935 | 2267 | |
| 2936 | | const o = &f.object; |
| 2937 | | const zcu = o.dg.pt.zcu; |
| 2268 | const zcu = f.dg.pt.zcu; |
| 2938 | 2269 | const ip = &zcu.intern_pool; |
| 2939 | | const gpa = o.dg.gpa; |
| 2940 | | const nav_index = o.dg.pass.nav; |
| 2270 | const gpa = f.dg.gpa; |
| 2271 | const nav_index = f.dg.owner_nav.unwrap().?; |
| 2941 | 2272 | const nav_val = zcu.navValue(nav_index); |
| 2942 | 2273 | const nav = ip.getNav(nav_index); |
| 2943 | 2274 | |
| 2944 | | const fwd = &o.dg.fwd_decl.writer; |
| 2945 | | try fwd.writeAll("static "); |
| 2946 | | try o.dg.renderFunctionSignature( |
| 2947 | | fwd, |
| 2275 | try fwd_decl_writer.writeAll("static "); |
| 2276 | try f.dg.renderFunctionSignature( |
| 2277 | fwd_decl_writer, |
| 2948 | 2278 | nav_val, |
| 2949 | 2279 | nav.status.fully_resolved.alignment, |
| 2950 | | .forward, |
| 2280 | .forward_decl, |
| 2951 | 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 | 2285 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| |
| 2957 | | try ch.print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); |
| 2958 | | try o.dg.renderFunctionSignature( |
| 2959 | | ch, |
| 2286 | try header_writer.print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); |
| 2287 | try f.dg.renderFunctionSignature( |
| 2288 | header_writer, |
| 2960 | 2289 | nav_val, |
| 2961 | 2290 | .none, |
| 2962 | | .complete, |
| 2291 | .definition, |
| 2963 | 2292 | .{ .nav = nav_index }, |
| 2964 | 2293 | ); |
| 2965 | | try ch.writeAll(" {\n "); |
| 2294 | try header_writer.writeAll(" {\n "); |
| 2966 | 2295 | |
| 2967 | 2296 | f.free_locals_map.clearRetainingCapacity(); |
| 2968 | 2297 | |
| 2969 | 2298 | const main_body = f.air.getMainBody(); |
| 2970 | | o.indent(); |
| 2299 | f.indent(); |
| 2971 | 2300 | try genBodyResolveState(f, undefined, &.{}, main_body, true); |
| 2972 | | try o.outdent(); |
| 2973 | | try o.code.writer.writeByte('}'); |
| 2974 | | try o.newline(); |
| 2975 | | if (o.dg.expected_block) |_| |
| 2301 | try f.outdent(); |
| 2302 | try f.code.writer.writeByte('}'); |
| 2303 | try f.newline(); |
| 2304 | if (f.dg.expected_block) |_| |
| 2976 | 2305 | return f.fail("runtime code not allowed in naked function", .{}); |
| 2977 | 2306 | |
| 2978 | 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 | 2315 | if (!should_emit) continue; |
| 2987 | 2316 | const local = f.locals.items[local_index]; |
| 2988 | 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 | 2319 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 2991 | 2320 | try gop.value_ptr.putNoClobber(gpa, local_index, {}); |
| 2992 | 2321 | } |
| 2993 | 2322 | |
| 2994 | 2323 | const SortContext = struct { |
| 2324 | zcu: *const Zcu, |
| 2995 | 2325 | keys: []const LocalType, |
| 2996 | 2326 | |
| 2997 | 2327 | pub fn lessThan(ctx: @This(), lhs_index: usize, rhs_index: usize) bool { |
| 2998 | | const lhs_ty = ctx.keys[lhs_index]; |
| 2999 | | const rhs_ty = ctx.keys[rhs_index]; |
| 3000 | | return lhs_ty.alignas.order(rhs_ty.alignas).compare(.gt); |
| 2328 | const lhs = ctx.keys[lhs_index]; |
| 2329 | const rhs = ctx.keys[rhs_index]; |
| 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 | 2346 | for (free_locals.values()) |list| { |
| 3006 | 2347 | for (list.keys()) |local_index| { |
| 3007 | 2348 | const local = f.locals.items[local_index]; |
| 3008 | | try o.dg.renderCTypeAndName(ch, local.ctype, .{ .local = local_index }, .{}, local.flags.alignas); |
| 3009 | | try ch.writeAll(";\n "); |
| 2349 | try f.dg.renderTypeAndName(header_writer, local.type, .{ .local = local_index }, .{}, local.alignment); |
| 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 | 2356 | const tracy = trace(@src()); |
| 3016 | 2357 | defer tracy.end(); |
| 3017 | 2358 | |
| 3018 | | const pt = o.dg.pt; |
| 2359 | const pt = dg.pt; |
| 3019 | 2360 | const zcu = pt.zcu; |
| 3020 | 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 | 2363 | const nav_ty: Type = .fromInterned(nav.typeOf(ip)); |
| 3023 | 2364 | |
| 3024 | | if (!nav_ty.hasRuntimeBits(zcu)) return; |
| 3025 | | switch (ip.indexToKey(nav.status.fully_resolved.val)) { |
| 3026 | | .@"extern" => |@"extern"| { |
| 3027 | | if (!ip.isFunctionType(nav_ty.toIntern())) return o.dg.renderFwdDecl(o.dg.pass.nav, .{ |
| 3028 | | .is_const = @"extern".is_const, |
| 3029 | | .is_threadlocal = @"extern".is_threadlocal, |
| 3030 | | .linkage = @"extern".linkage, |
| 3031 | | .visibility = @"extern".visibility, |
| 3032 | | }); |
| 2365 | const is_const: bool, const is_threadlocal: bool, const init_val: Value = switch (ip.indexToKey(nav.status.fully_resolved.val)) { |
| 2366 | else => .{ true, false, .fromInterned(nav.status.fully_resolved.val) }, |
| 2367 | .variable => |v| .{ false, v.is_threadlocal, .fromInterned(v.init) }, |
| 2368 | .@"extern" => return, |
| 2369 | }; |
| 3033 | 2370 | |
| 3034 | | const fwd = &o.dg.fwd_decl.writer; |
| 3035 | | try fwd.writeAll("zig_extern "); |
| 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 | | ), |
| 2371 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| { |
| 2372 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); |
| 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( |
| 3083 | | o: *Object, |
| 3084 | | val: Value, |
| 3085 | | decl_c_value: CValue, |
| 3086 | | alignment: Alignment, |
| 3087 | | @"linksection": InternPool.OptionalNullTerminatedString, |
| 3088 | | ) Error!void { |
| 3089 | | const zcu = o.dg.pt.zcu; |
| 3090 | | const ty = val.typeOf(zcu); |
| 2392 | const pt = dg.pt; |
| 2393 | const zcu = pt.zcu; |
| 2394 | const ip = &zcu.intern_pool; |
| 2395 | const nav = ip.getNav(dg.owner_nav.unwrap().?); |
| 2396 | const nav_ty: Type = .fromInterned(nav.typeOf(ip)); |
| 3091 | 2397 | |
| 3092 | | const fwd = &o.dg.fwd_decl.writer; |
| 3093 | | try fwd.writeAll("static "); |
| 3094 | | try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete); |
| 3095 | | try fwd.writeAll(";\n"); |
| 2398 | const is_const: bool, const is_threadlocal: bool, const init_val: Value = switch (ip.indexToKey(nav.status.fully_resolved.val)) { |
| 2399 | else => .{ true, false, .fromInterned(nav.status.fully_resolved.val) }, |
| 2400 | .variable => |v| .{ false, v.is_threadlocal, .fromInterned(v.init) }, |
| 3096 | 2401 | |
| 3097 | | const w = &o.code.writer; |
| 3098 | | if (@"linksection".toSlice(&zcu.intern_pool)) |s| |
| 3099 | | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); |
| 3100 | | try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete); |
| 2402 | .@"extern" => |@"extern"| switch (nav_ty.zigTypeTag(zcu)) { |
| 2403 | .@"fn" => { |
| 2404 | try w.writeAll("zig_extern "); |
| 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 | 2466 | try w.writeAll(" = "); |
| 3102 | | try o.dg.renderValue(w, val, .StaticInitializer); |
| 3103 | | try w.writeByte(';'); |
| 3104 | | try o.newline(); |
| 2467 | try dg.renderValue(w, options.init_val, .static_initializer); |
| 2468 | try w.writeAll(";\n"); |
| 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 | 2487 | const zcu = dg.pt.zcu; |
| 3109 | 2488 | const ip = &zcu.intern_pool; |
| 3110 | | const fwd = &dg.fwd_decl.writer; |
| 3111 | 2489 | |
| 3112 | 2490 | const main_name = export_indices[0].ptr(zcu).opts.name; |
| 3113 | | try fwd.writeAll("#define "); |
| 2491 | try w.writeAll("#define "); |
| 3114 | 2492 | switch (exported) { |
| 3115 | | .nav => |nav| try dg.renderNavName(fwd, nav), |
| 3116 | | .uav => |uav| try DeclGen.renderUavName(fwd, Value.fromInterned(uav)), |
| 2493 | .nav => |nav| try renderNavName(w, nav, ip), |
| 2494 | .uav => |uav| try renderUavName(w, Value.fromInterned(uav)), |
| 3117 | 2495 | } |
| 3118 | | try fwd.writeByte(' '); |
| 3119 | | try fwd.print("{f}", .{fmtIdentSolo(main_name.toSlice(ip))}); |
| 3120 | | try fwd.writeByte('\n'); |
| 2496 | try w.writeByte(' '); |
| 2497 | try w.print("{f}", .{fmtIdentSolo(main_name.toSlice(ip))}); |
| 2498 | try w.writeByte('\n'); |
| 3121 | 2499 | |
| 3122 | 2500 | const exported_val = exported.getValue(zcu); |
| 3123 | 2501 | if (ip.isFunctionType(exported_val.typeOf(zcu).toIntern())) return for (export_indices) |export_index| { |
| 3124 | 2502 | const @"export" = export_index.ptr(zcu); |
| 3125 | | try fwd.writeAll("zig_extern "); |
| 3126 | | if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage_fn "); |
| 2503 | try w.writeAll("zig_extern "); |
| 2504 | if (@"export".opts.linkage == .weak) try w.writeAll("zig_weak_linkage_fn "); |
| 3127 | 2505 | try dg.renderFunctionSignature( |
| 3128 | | fwd, |
| 2506 | w, |
| 3129 | 2507 | exported.getValue(zcu), |
| 3130 | 2508 | exported.getAlign(zcu), |
| 3131 | | .forward, |
| 2509 | .forward_decl, |
| 3132 | 2510 | .{ .@"export" = .{ |
| 3133 | 2511 | .main_name = main_name, |
| 3134 | 2512 | .extern_name = @"export".opts.name, |
| 3135 | 2513 | } }, |
| 3136 | 2514 | ); |
| 3137 | | try fwd.writeAll(";\n"); |
| 2515 | try w.writeAll(";\n"); |
| 3138 | 2516 | }; |
| 3139 | 2517 | const is_const = switch (ip.indexToKey(exported_val.toIntern())) { |
| 3140 | 2518 | .func => unreachable, |
| ... | ... | @@ -3144,39 +2522,38 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const |
| 3144 | 2522 | }; |
| 3145 | 2523 | for (export_indices) |export_index| { |
| 3146 | 2524 | const @"export" = export_index.ptr(zcu); |
| 3147 | | try fwd.writeAll("zig_extern "); |
| 3148 | | if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage "); |
| 3149 | | if (@"export".opts.section.toSlice(ip)) |s| try fwd.print("zig_linksection({f}) ", .{ |
| 2525 | try w.writeAll("zig_extern "); |
| 2526 | if (@"export".opts.linkage == .weak) try w.writeAll("zig_weak_linkage "); |
| 2527 | if (@"export".opts.section.toSlice(ip)) |s| try w.print("zig_linksection({f}) ", .{ |
| 3150 | 2528 | fmtStringLiteral(s, null), |
| 3151 | 2529 | }); |
| 3152 | 2530 | const extern_name = @"export".opts.name.toSlice(ip); |
| 3153 | 2531 | const is_mangled = isMangledIdent(extern_name, true); |
| 3154 | 2532 | const is_export = @"export".opts.name != main_name; |
| 3155 | 2533 | try dg.renderTypeAndName( |
| 3156 | | fwd, |
| 2534 | w, |
| 3157 | 2535 | exported.getValue(zcu).typeOf(zcu), |
| 3158 | 2536 | .{ .identifier = extern_name }, |
| 3159 | | CQualifiers.init(.{ .@"const" = is_const }), |
| 2537 | .{ .@"const" = is_const }, |
| 3160 | 2538 | exported.getAlign(zcu), |
| 3161 | | .complete, |
| 3162 | 2539 | ); |
| 3163 | 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 | 2542 | fmtIdentSolo(extern_name), |
| 3166 | 2543 | fmtStringLiteral(extern_name, null), |
| 3167 | 2544 | fmtStringLiteral(main_name.toSlice(ip), null), |
| 3168 | 2545 | }); |
| 3169 | 2546 | } else if (is_mangled) { |
| 3170 | | try fwd.print(" zig_mangled({f}, {f})", .{ |
| 2547 | try w.print(" zig_mangled({f}, {f})", .{ |
| 3171 | 2548 | fmtIdentSolo(extern_name), fmtStringLiteral(extern_name, null), |
| 3172 | 2549 | }); |
| 3173 | 2550 | } else if (is_export) { |
| 3174 | | try fwd.print(" zig_export({f}, {f})", .{ |
| 2551 | try w.print(" zig_export({f}, {f})", .{ |
| 3175 | 2552 | fmtStringLiteral(main_name.toSlice(ip), null), |
| 3176 | 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 | 2562 | /// have been added to `free_locals_map`. For a version of this function that restores this state, |
| 3186 | 2563 | /// see `genBodyResolveState`. |
| 3187 | 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 | 2566 | if (body.len == 0) { |
| 3190 | 2567 | try w.writeAll("{}"); |
| 3191 | 2568 | } else { |
| 3192 | 2569 | try w.writeByte('{'); |
| 3193 | | f.object.indent(); |
| 3194 | | try f.object.newline(); |
| 2570 | f.indent(); |
| 2571 | try f.newline(); |
| 3195 | 2572 | try genBodyInner(f, body); |
| 3196 | | try f.object.outdent(); |
| 2573 | try f.outdent(); |
| 3197 | 2574 | try w.writeByte('}'); |
| 3198 | 2575 | } |
| 3199 | 2576 | } |
| ... | ... | @@ -3207,13 +2584,13 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3207 | 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 | 2585 | if (body.len == 0) { |
| 3209 | 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 | 2588 | return; |
| 3212 | 2589 | } |
| 3213 | 2590 | |
| 3214 | 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 | 2595 | // Save the original value_map and free_locals_map so that we can restore them after the body. |
| 3219 | 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 | 2631 | } |
| 3255 | 2632 | |
| 3256 | 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 | 2635 | const ip = &zcu.intern_pool; |
| 3259 | 2636 | const air_tags = f.air.instructions.items(.tag); |
| 3260 | 2637 | const air_datas = f.air.instructions.items(.data); |
| 3261 | 2638 | |
| 3262 | 2639 | for (body) |inst| { |
| 3263 | | if (f.object.dg.expected_block) |_| |
| 2640 | if (f.dg.expected_block) |_| |
| 3264 | 2641 | return f.fail("runtime code not allowed in naked function", .{}); |
| 3265 | 2642 | if (f.liveness.isUnused(inst) and !f.air.mustLower(inst, ip)) |
| 3266 | 2643 | continue; |
| ... | ... | @@ -3529,8 +2906,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3529 | 2906 | .ret => return airRet(f, inst, false), |
| 3530 | 2907 | .ret_safe => return airRet(f, inst, false), // TODO |
| 3531 | 2908 | .ret_load => return airRet(f, inst, true), |
| 3532 | | .trap => return airTrap(f, &f.object.code.writer), |
| 3533 | | .unreach => return airUnreach(&f.object), |
| 2909 | .trap => return airTrap(f), |
| 2910 | .unreach => return airUnreach(f), |
| 3534 | 2911 | |
| 3535 | 2912 | // Instructions which may be `noreturn`. |
| 3536 | 2913 | .block => res: { |
| ... | ... | @@ -3573,21 +2950,21 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 3573 | 2950 | const operand = try f.resolveInst(ty_op.operand); |
| 3574 | 2951 | try reap(f, inst, &.{ty_op.operand}); |
| 3575 | 2952 | |
| 3576 | | const w = &f.object.code.writer; |
| 2953 | const w = &f.code.writer; |
| 3577 | 2954 | const local = try f.allocLocal(inst, inst_ty); |
| 3578 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3579 | | try f.writeCValue(w, local, .Other); |
| 3580 | | try a.assign(f, w); |
| 2955 | try f.writeCValue(w, local, .other); |
| 2956 | try w.writeAll(" = "); |
| 3581 | 2957 | if (is_ptr) { |
| 3582 | 2958 | try w.writeByte('&'); |
| 3583 | 2959 | try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name }); |
| 3584 | 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 | 2963 | return local; |
| 3587 | 2964 | } |
| 3588 | 2965 | |
| 3589 | 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 | 2968 | const inst_ty = f.typeOfIndex(inst); |
| 3592 | 2969 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3593 | 2970 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | ... | @@ -3596,21 +2973,24 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3596 | 2973 | const index = try f.resolveInst(bin_op.rhs); |
| 3597 | 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 | 2977 | const local = try f.allocLocal(inst, inst_ty); |
| 3601 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3602 | | try f.writeCValue(w, local, .Other); |
| 3603 | | try a.assign(f, w); |
| 3604 | | try f.writeCValue(w, ptr, .Other); |
| 2978 | try f.writeCValue(w, local, .other); |
| 2979 | try w.writeAll(" = "); |
| 2980 | switch (f.typeOf(bin_op.lhs).ptrSize(zcu)) { |
| 2981 | .one => try f.writeCValueDerefMember(w, ptr, .{ .identifier = "array" }), |
| 2982 | .many, .c => try f.writeCValue(w, ptr, .other), |
| 2983 | .slice => unreachable, |
| 2984 | } |
| 3605 | 2985 | try w.writeByte('['); |
| 3606 | | try f.writeCValue(w, index, .Other); |
| 3607 | | try w.writeByte(']'); |
| 3608 | | try a.end(f, w); |
| 2986 | try f.writeCValue(w, index, .other); |
| 2987 | try w.writeAll("];"); |
| 2988 | try f.newline(); |
| 3609 | 2989 | return local; |
| 3610 | 2990 | } |
| 3611 | 2991 | |
| 3612 | 2992 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3613 | | const pt = f.object.dg.pt; |
| 2993 | const pt = f.dg.pt; |
| 3614 | 2994 | const zcu = pt.zcu; |
| 3615 | 2995 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3616 | 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 | 3003 | const index = try f.resolveInst(bin_op.rhs); |
| 3624 | 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 | 3007 | const local = try f.allocLocal(inst, inst_ty); |
| 3628 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3629 | | try f.writeCValue(w, local, .Other); |
| 3630 | | try a.assign(f, w); |
| 3631 | | try w.writeByte('('); |
| 3632 | | try f.renderType(w, inst_ty); |
| 3633 | | try w.writeByte(')'); |
| 3008 | try f.writeCValue(w, local, .other); |
| 3009 | try w.writeAll(" = "); |
| 3634 | 3010 | try w.writeByte('&'); |
| 3635 | 3011 | if (ptr_ty.ptrSize(zcu) == .one) { |
| 3636 | | // It's a pointer to an array, so we need to de-reference. |
| 3637 | | try f.writeCValueDeref(w, ptr); |
| 3638 | | } else try f.writeCValue(w, ptr, .Other); |
| 3012 | // `*[n]T` was turned into a pointer to `struct { T array[n]; }` |
| 3013 | try f.writeCValueDerefMember(w, ptr, .{ .identifier = "array" }); |
| 3014 | } else { |
| 3015 | try f.writeCValue(w, ptr, .other); |
| 3016 | } |
| 3639 | 3017 | try w.writeByte('['); |
| 3640 | | try f.writeCValue(w, index, .Other); |
| 3641 | | try w.writeByte(']'); |
| 3642 | | try a.end(f, w); |
| 3018 | try f.writeCValue(w, index, .other); |
| 3019 | try w.writeAll("];"); |
| 3020 | try f.newline(); |
| 3643 | 3021 | return local; |
| 3644 | 3022 | } |
| 3645 | 3023 | |
| 3646 | 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 | 3026 | const inst_ty = f.typeOfIndex(inst); |
| 3649 | 3027 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3650 | 3028 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | ... | @@ -3653,21 +3031,20 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3653 | 3031 | const index = try f.resolveInst(bin_op.rhs); |
| 3654 | 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 | 3035 | const local = try f.allocLocal(inst, inst_ty); |
| 3658 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3659 | | try f.writeCValue(w, local, .Other); |
| 3660 | | try a.assign(f, w); |
| 3036 | try f.writeCValue(w, local, .other); |
| 3037 | try w.writeAll(" = "); |
| 3661 | 3038 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); |
| 3662 | 3039 | try w.writeByte('['); |
| 3663 | | try f.writeCValue(w, index, .Other); |
| 3664 | | try w.writeByte(']'); |
| 3665 | | try a.end(f, w); |
| 3040 | try f.writeCValue(w, index, .other); |
| 3041 | try w.writeAll("];"); |
| 3042 | try f.newline(); |
| 3666 | 3043 | return local; |
| 3667 | 3044 | } |
| 3668 | 3045 | |
| 3669 | 3046 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3670 | | const pt = f.object.dg.pt; |
| 3047 | const pt = f.dg.pt; |
| 3671 | 3048 | const zcu = pt.zcu; |
| 3672 | 3049 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3673 | 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 | 3058 | const index = try f.resolveInst(bin_op.rhs); |
| 3682 | 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 | 3062 | const local = try f.allocLocal(inst, inst_ty); |
| 3686 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3687 | | try f.writeCValue(w, local, .Other); |
| 3688 | | try a.assign(f, w); |
| 3063 | try f.writeCValue(w, local, .other); |
| 3064 | try w.writeAll(" = "); |
| 3689 | 3065 | try w.writeByte('&'); |
| 3690 | 3066 | try f.writeCValueMember(w, slice, .{ .identifier = "ptr" }); |
| 3691 | 3067 | try w.writeByte('['); |
| 3692 | | try f.writeCValue(w, index, .Other); |
| 3693 | | try w.writeByte(']'); |
| 3694 | | try a.end(f, w); |
| 3068 | try f.writeCValue(w, index, .other); |
| 3069 | try w.writeAll("];"); |
| 3070 | try f.newline(); |
| 3695 | 3071 | return local; |
| 3696 | 3072 | } |
| 3697 | 3073 | |
| 3698 | 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 | 3076 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3701 | 3077 | const inst_ty = f.typeOfIndex(inst); |
| 3702 | 3078 | assert(inst_ty.hasRuntimeBits(zcu)); |
| ... | ... | @@ -3705,32 +3081,28 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3705 | 3081 | const index = try f.resolveInst(bin_op.rhs); |
| 3706 | 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 | 3085 | const local = try f.allocLocal(inst, inst_ty); |
| 3710 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3711 | | try f.writeCValue(w, local, .Other); |
| 3712 | | try a.assign(f, w); |
| 3713 | | try f.writeCValue(w, array, .Other); |
| 3086 | try f.writeCValue(w, local, .other); |
| 3087 | try w.writeAll(" = "); |
| 3088 | try f.writeCValueMember(w, array, .{ .identifier = "array" }); |
| 3714 | 3089 | try w.writeByte('['); |
| 3715 | | try f.writeCValue(w, index, .Other); |
| 3716 | | try w.writeByte(']'); |
| 3717 | | try a.end(f, w); |
| 3090 | try f.writeCValue(w, index, .other); |
| 3091 | try w.writeAll("];"); |
| 3092 | try f.newline(); |
| 3718 | 3093 | return local; |
| 3719 | 3094 | } |
| 3720 | 3095 | |
| 3721 | 3096 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3722 | | const pt = f.object.dg.pt; |
| 3097 | const pt = f.dg.pt; |
| 3723 | 3098 | const zcu = pt.zcu; |
| 3724 | 3099 | const inst_ty = f.typeOfIndex(inst); |
| 3725 | 3100 | const elem_ty = inst_ty.childType(zcu); |
| 3726 | 3101 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; |
| 3727 | 3102 | |
| 3728 | 3103 | const local = try f.allocLocalValue(.{ |
| 3729 | | .ctype = try f.ctypeFromType(elem_ty, .complete), |
| 3730 | | .alignas = CType.AlignAs.fromAlignment(.{ |
| 3731 | | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3732 | | .abi = elem_ty.abiAlignment(zcu), |
| 3733 | | }), |
| 3104 | .type = elem_ty, |
| 3105 | .alignment = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3734 | 3106 | }); |
| 3735 | 3107 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3736 | 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 | 3113 | // For packed aggregates, we zero-initialize to try and work around a design flaw |
| 3742 | 3114 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` |
| 3743 | 3115 | // for details. |
| 3744 | | const w = &f.object.code.writer; |
| 3116 | const w = &f.code.writer; |
| 3745 | 3117 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); |
| 3746 | 3118 | try f.renderType(w, elem_ty); |
| 3747 | 3119 | try w.writeAll("));"); |
| 3748 | | try f.object.newline(); |
| 3120 | try f.newline(); |
| 3749 | 3121 | }, |
| 3750 | 3122 | .auto, .@"extern" => {}, |
| 3751 | 3123 | }, |
| ... | ... | @@ -3756,18 +3128,15 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3756 | 3128 | } |
| 3757 | 3129 | |
| 3758 | 3130 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3759 | | const pt = f.object.dg.pt; |
| 3131 | const pt = f.dg.pt; |
| 3760 | 3132 | const zcu = pt.zcu; |
| 3761 | 3133 | const inst_ty = f.typeOfIndex(inst); |
| 3762 | 3134 | const elem_ty = inst_ty.childType(zcu); |
| 3763 | 3135 | if (!elem_ty.hasRuntimeBits(zcu)) return .{ .undef = inst_ty }; |
| 3764 | 3136 | |
| 3765 | 3137 | const local = try f.allocLocalValue(.{ |
| 3766 | | .ctype = try f.ctypeFromType(elem_ty, .complete), |
| 3767 | | .alignas = CType.AlignAs.fromAlignment(.{ |
| 3768 | | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3769 | | .abi = elem_ty.abiAlignment(zcu), |
| 3770 | | }), |
| 3138 | .type = elem_ty, |
| 3139 | .alignment = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3771 | 3140 | }); |
| 3772 | 3141 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3773 | 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 | 3147 | // For packed aggregates, we zero-initialize to try and work around a design flaw |
| 3779 | 3148 | // related to how `packed`, `undefined`, and RLS interact. See comment in `airStore` |
| 3780 | 3149 | // for details. |
| 3781 | | const w = &f.object.code.writer; |
| 3150 | const w = &f.code.writer; |
| 3782 | 3151 | try w.print("memset(&t{d}, 0x00, sizeof(", .{local.new_local}); |
| 3783 | 3152 | try f.renderType(w, elem_ty); |
| 3784 | 3153 | try w.writeAll("));"); |
| 3785 | | try f.object.newline(); |
| 3154 | try f.newline(); |
| 3786 | 3155 | }, |
| 3787 | 3156 | .auto, .@"extern" => {}, |
| 3788 | 3157 | }, |
| ... | ... | @@ -3793,24 +3162,18 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3793 | 3162 | } |
| 3794 | 3163 | |
| 3795 | 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 | 3165 | const i = f.next_arg_index; |
| 3800 | 3166 | f.next_arg_index += 1; |
| 3801 | | const result: CValue = if (inst_ctype.eql(try f.ctypeFromType(inst_ty, .complete))) |
| 3802 | | .{ .arg = i } |
| 3803 | | else |
| 3804 | | .{ .arg_array = i }; |
| 3167 | const result: CValue = .{ .arg = i }; |
| 3805 | 3168 | |
| 3806 | 3169 | if (f.liveness.isUnused(inst)) { |
| 3807 | | const w = &f.object.code.writer; |
| 3170 | const w = &f.code.writer; |
| 3808 | 3171 | try w.writeByte('('); |
| 3809 | 3172 | try f.renderType(w, .void); |
| 3810 | 3173 | try w.writeByte(')'); |
| 3811 | | try f.writeCValue(w, result, .Other); |
| 3174 | try f.writeCValue(w, result, .other); |
| 3812 | 3175 | try w.writeByte(';'); |
| 3813 | | try f.object.newline(); |
| 3176 | try f.newline(); |
| 3814 | 3177 | return .none; |
| 3815 | 3178 | } |
| 3816 | 3179 | |
| ... | ... | @@ -3818,7 +3181,7 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3818 | 3181 | } |
| 3819 | 3182 | |
| 3820 | 3183 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3821 | | const pt = f.object.dg.pt; |
| 3184 | const pt = f.dg.pt; |
| 3822 | 3185 | const zcu = pt.zcu; |
| 3823 | 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 | 3204 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) |
| 3842 | 3205 | else |
| 3843 | 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 | 3209 | const local = try f.allocLocal(inst, src_ty); |
| 3849 | 3210 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 3850 | 3211 | |
| 3851 | | if (need_memcpy) { |
| 3852 | | try w.writeAll("memcpy("); |
| 3853 | | if (!is_array) try w.writeByte('&'); |
| 3854 | | try f.writeCValue(w, local, .Other); |
| 3212 | if (!is_aligned) { |
| 3213 | try w.writeAll("memcpy(&"); |
| 3214 | try f.writeCValue(w, local, .other); |
| 3855 | 3215 | try v.elem(f, w); |
| 3856 | 3216 | try w.writeAll(", (const char *)"); |
| 3857 | | try f.writeCValue(w, operand, .Other); |
| 3217 | try f.writeCValue(w, operand, .other); |
| 3858 | 3218 | try v.elem(f, w); |
| 3859 | 3219 | try w.writeAll(", sizeof("); |
| 3860 | 3220 | try f.renderType(w, src_ty); |
| 3861 | 3221 | try w.writeAll("))"); |
| 3862 | 3222 | } else { |
| 3863 | | try f.writeCValue(w, local, .Other); |
| 3223 | try f.writeCValue(w, local, .other); |
| 3864 | 3224 | try v.elem(f, w); |
| 3865 | 3225 | try w.writeAll(" = "); |
| 3866 | 3226 | try f.writeCValueDeref(w, operand); |
| 3867 | 3227 | try v.elem(f, w); |
| 3868 | 3228 | } |
| 3869 | 3229 | try w.writeByte(';'); |
| 3870 | | try f.object.newline(); |
| 3230 | try f.newline(); |
| 3871 | 3231 | try v.end(f, inst, w); |
| 3872 | 3232 | |
| 3873 | 3233 | return local; |
| 3874 | 3234 | } |
| 3875 | 3235 | |
| 3876 | 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 | 3238 | const zcu = pt.zcu; |
| 3879 | 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 | 3241 | const op_inst = un_op.toIndex(); |
| 3882 | 3242 | const op_ty = f.typeOf(un_op); |
| 3883 | 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 | 3245 | if (op_inst != null and f.air.instructions.items(.tag)[@intFromEnum(op_inst.?)] == .call_always_tail) { |
| 3887 | 3246 | try reap(f, inst, &.{un_op}); |
| 3888 | 3247 | _ = try airCall(f, op_inst.?, .always_tail); |
| 3889 | | } else if (ret_ctype.index != .void) { |
| 3248 | } else if (ret_ty.hasRuntimeBits(zcu)) { |
| 3890 | 3249 | const operand = try f.resolveInst(un_op); |
| 3891 | 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 | 3252 | try w.writeAll("return "); |
| 3915 | | if (deref) |
| 3916 | | try f.writeCValueDeref(w, ret_val) |
| 3917 | | else |
| 3918 | | try f.writeCValue(w, ret_val, .Other); |
| 3919 | | try w.writeAll(";\n"); |
| 3920 | | if (is_array) { |
| 3921 | | try freeLocal(f, inst, ret_val.new_local, null); |
| 3253 | if (is_ptr) { |
| 3254 | try f.writeCValueDeref(w, operand); |
| 3255 | } else switch (operand) { |
| 3256 | // Instead of 'return &local', emit 'return undefined'. |
| 3257 | .local_ref => try f.dg.renderUndefValue(w, ret_ty, .other), |
| 3258 | else => try f.writeCValue(w, operand, .other), |
| 3922 | 3259 | } |
| 3260 | try w.writeAll(";\n"); |
| 3923 | 3261 | } else { |
| 3924 | 3262 | try reap(f, inst, &.{un_op}); |
| 3925 | 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 | 3268 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3931 | | const pt = f.object.dg.pt; |
| 3269 | const pt = f.dg.pt; |
| 3932 | 3270 | const zcu = pt.zcu; |
| 3933 | 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 | 3278 | const operand_ty = f.typeOf(ty_op.operand); |
| 3941 | 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 | 3284 | const local = try f.allocLocal(inst, inst_ty); |
| 3947 | 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)); |
| 3949 | | try f.writeCValue(w, local, .Other); |
| 3286 | try f.writeCValue(w, local, .other); |
| 3950 | 3287 | try v.elem(f, w); |
| 3951 | | try a.assign(f, w); |
| 3952 | | try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .Other); |
| 3953 | | try a.end(f, w); |
| 3288 | try w.writeAll(" = "); |
| 3289 | try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .other); |
| 3290 | try w.writeByte(';'); |
| 3291 | try f.newline(); |
| 3954 | 3292 | try v.end(f, inst, w); |
| 3955 | 3293 | return local; |
| 3956 | 3294 | } |
| 3957 | 3295 | |
| 3958 | 3296 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3959 | | const pt = f.object.dg.pt; |
| 3297 | const pt = f.dg.pt; |
| 3960 | 3298 | const zcu = pt.zcu; |
| 3961 | 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 | 3316 | const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits); |
| 3979 | 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 | 3320 | const local = try f.allocLocal(inst, inst_ty); |
| 3983 | 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)); |
| 3985 | | try f.writeCValue(w, local, .Other); |
| 3322 | try f.writeCValue(w, local, .other); |
| 3986 | 3323 | try v.elem(f, w); |
| 3987 | | try a.assign(f, w); |
| 3324 | try w.writeAll(" = "); |
| 3988 | 3325 | if (need_cast) { |
| 3989 | 3326 | try w.writeByte('('); |
| 3990 | 3327 | try f.renderType(w, inst_scalar_ty); |
| ... | ... | @@ -3992,18 +3329,18 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3992 | 3329 | } |
| 3993 | 3330 | if (need_lo) { |
| 3994 | 3331 | try w.writeAll("zig_lo_"); |
| 3995 | | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3332 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3996 | 3333 | try w.writeByte('('); |
| 3997 | 3334 | } |
| 3998 | 3335 | if (!need_mask) { |
| 3999 | | try f.writeCValue(w, operand, .Other); |
| 3336 | try f.writeCValue(w, operand, .other); |
| 4000 | 3337 | try v.elem(f, w); |
| 4001 | 3338 | } else switch (dest_int_info.signedness) { |
| 4002 | 3339 | .unsigned => { |
| 4003 | 3340 | try w.writeAll("zig_and_"); |
| 4004 | | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3341 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4005 | 3342 | try w.writeByte('('); |
| 4006 | | try f.writeCValue(w, operand, .FunctionArgument); |
| 3343 | try f.writeCValue(w, operand, .other); |
| 4007 | 3344 | try v.elem(f, w); |
| 4008 | 3345 | try w.print(", {f})", .{ |
| 4009 | 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 | 3352 | const shift_val = try pt.intValue(.u8, c_bits - dest_bits); |
| 4016 | 3353 | |
| 4017 | 3354 | try w.writeAll("zig_shr_"); |
| 4018 | | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3355 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4019 | 3356 | if (c_bits == 128) { |
| 4020 | 3357 | try w.print("(zig_bitCast_i{d}(", .{c_bits}); |
| 4021 | 3358 | } else { |
| ... | ... | @@ -4027,7 +3364,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4027 | 3364 | } else { |
| 4028 | 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 | 3368 | try v.elem(f, w); |
| 4032 | 3369 | if (c_bits == 128) try w.writeByte(')'); |
| 4033 | 3370 | try w.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)}); |
| ... | ... | @@ -4036,13 +3373,14 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4036 | 3373 | }, |
| 4037 | 3374 | } |
| 4038 | 3375 | if (need_lo) try w.writeByte(')'); |
| 4039 | | try a.end(f, w); |
| 3376 | try w.writeByte(';'); |
| 3377 | try f.newline(); |
| 4040 | 3378 | try v.end(f, inst, w); |
| 4041 | 3379 | return local; |
| 4042 | 3380 | } |
| 4043 | 3381 | |
| 4044 | 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 | 3384 | const zcu = pt.zcu; |
| 4047 | 3385 | // *a = b; |
| 4048 | 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 | 3398 | |
| 4061 | 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 | 3402 | if (val_is_undef) { |
| 4065 | 3403 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4066 | 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 | 3418 | }, |
| 4081 | 3419 | }; |
| 4082 | 3420 | try w.writeAll("memset("); |
| 4083 | | try f.writeCValue(w, ptr_val, .FunctionArgument); |
| 3421 | try f.writeCValue(w, ptr_val, .other); |
| 4084 | 3422 | try w.print(", {s}, sizeof(", .{byte_str}); |
| 4085 | 3423 | try f.renderType(w, .fromInterned(ptr_info.child)); |
| 4086 | 3424 | try w.writeAll("));"); |
| 4087 | | try f.object.newline(); |
| 3425 | try f.newline(); |
| 4088 | 3426 | } |
| 4089 | 3427 | return .none; |
| 4090 | 3428 | } |
| ... | ... | @@ -4093,46 +3431,29 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4093 | 3431 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) |
| 4094 | 3432 | else |
| 4095 | 3433 | true; |
| 4096 | | const is_array = lowersToArray(.fromInterned(ptr_info.child), zcu); |
| 4097 | | const need_memcpy = !is_aligned or is_array; |
| 4098 | 3434 | |
| 4099 | 3435 | const src_val = try f.resolveInst(bin_op.rhs); |
| 4100 | 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); |
| 4103 | | if (need_memcpy) { |
| 3438 | if (!is_aligned) { |
| 4104 | 3439 | // For this memcpy to safely work we need the rhs to have the same |
| 4105 | 3440 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| 4106 | 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 | 3443 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 4123 | 3444 | try w.writeAll("memcpy((char *)"); |
| 4124 | | try f.writeCValue(w, ptr_val, .FunctionArgument); |
| 3445 | try f.writeCValue(w, ptr_val, .other); |
| 4125 | 3446 | try v.elem(f, w); |
| 4126 | | try w.writeAll(", "); |
| 4127 | | if (!is_array) try w.writeByte('&'); |
| 4128 | | try f.writeCValue(w, array_src, .FunctionArgument); |
| 3447 | try w.writeAll(", &"); |
| 3448 | switch (src_val) { |
| 3449 | .constant => |val| try f.dg.renderValueAsLvalue(w, val), |
| 3450 | else => try f.writeCValue(w, src_val, .other), |
| 3451 | } |
| 4129 | 3452 | try v.elem(f, w); |
| 4130 | 3453 | try w.writeAll(", sizeof("); |
| 4131 | 3454 | try f.renderType(w, src_ty); |
| 4132 | | try w.writeAll("))"); |
| 4133 | | try f.freeCValue(inst, array_src); |
| 4134 | | try w.writeByte(';'); |
| 4135 | | try f.object.newline(); |
| 3455 | try w.writeAll("));"); |
| 3456 | try f.newline(); |
| 4136 | 3457 | try v.end(f, inst, w); |
| 4137 | 3458 | } else { |
| 4138 | 3459 | switch (ptr_val) { |
| ... | ... | @@ -4144,20 +3465,20 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4144 | 3465 | else => {}, |
| 4145 | 3466 | } |
| 4146 | 3467 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 4147 | | const a = try Assignment.start(f, w, src_scalar_ctype); |
| 4148 | 3468 | try f.writeCValueDeref(w, ptr_val); |
| 4149 | 3469 | try v.elem(f, w); |
| 4150 | | try a.assign(f, w); |
| 4151 | | try f.writeCValue(w, src_val, .Other); |
| 3470 | try w.writeAll(" = "); |
| 3471 | try f.writeCValue(w, src_val, .other); |
| 4152 | 3472 | try v.elem(f, w); |
| 4153 | | try a.end(f, w); |
| 3473 | try w.writeByte(';'); |
| 3474 | try f.newline(); |
| 4154 | 3475 | try v.end(f, inst, w); |
| 4155 | 3476 | } |
| 4156 | 3477 | return .none; |
| 4157 | 3478 | } |
| 4158 | 3479 | |
| 4159 | 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 | 3482 | const zcu = pt.zcu; |
| 4162 | 3483 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4163 | 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 | 3491 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4171 | 3492 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4172 | 3493 | |
| 4173 | | const w = &f.object.code.writer; |
| 3494 | const w = &f.code.writer; |
| 4174 | 3495 | const local = try f.allocLocal(inst, inst_ty); |
| 4175 | 3496 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4176 | 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 | 3499 | try w.writeAll(" = zig_"); |
| 4179 | 3500 | try w.writeAll(operation); |
| 4180 | 3501 | try w.writeAll("o_"); |
| 4181 | | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 3502 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 4182 | 3503 | try w.writeAll("(&"); |
| 4183 | 3504 | try f.writeCValueMember(w, local, .{ .field = 0 }); |
| 4184 | 3505 | try v.elem(f, w); |
| 4185 | 3506 | try w.writeAll(", "); |
| 4186 | | try f.writeCValue(w, lhs, .FunctionArgument); |
| 3507 | try f.writeCValue(w, lhs, .other); |
| 4187 | 3508 | try v.elem(f, w); |
| 4188 | 3509 | try w.writeAll(", "); |
| 4189 | | try f.writeCValue(w, rhs, .FunctionArgument); |
| 3510 | try f.writeCValue(w, rhs, .other); |
| 4190 | 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 | 3513 | try w.writeAll(");"); |
| 4193 | | try f.object.newline(); |
| 3514 | try f.newline(); |
| 4194 | 3515 | try v.end(f, inst, w); |
| 4195 | 3516 | |
| 4196 | 3517 | return local; |
| 4197 | 3518 | } |
| 4198 | 3519 | |
| 4199 | 3520 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4200 | | const pt = f.object.dg.pt; |
| 3521 | const pt = f.dg.pt; |
| 4201 | 3522 | const zcu = pt.zcu; |
| 4202 | 3523 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4203 | 3524 | const operand_ty = f.typeOf(ty_op.operand); |
| ... | ... | @@ -4209,17 +3530,17 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4209 | 3530 | |
| 4210 | 3531 | const inst_ty = f.typeOfIndex(inst); |
| 4211 | 3532 | |
| 4212 | | const w = &f.object.code.writer; |
| 3533 | const w = &f.code.writer; |
| 4213 | 3534 | const local = try f.allocLocal(inst, inst_ty); |
| 4214 | 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 | 3537 | try v.elem(f, w); |
| 4217 | 3538 | try w.writeAll(" = "); |
| 4218 | 3539 | try w.writeByte('!'); |
| 4219 | | try f.writeCValue(w, op, .Other); |
| 3540 | try f.writeCValue(w, op, .other); |
| 4220 | 3541 | try v.elem(f, w); |
| 4221 | 3542 | try w.writeByte(';'); |
| 4222 | | try f.object.newline(); |
| 3543 | try f.newline(); |
| 4223 | 3544 | try v.end(f, inst, w); |
| 4224 | 3545 | |
| 4225 | 3546 | return local; |
| ... | ... | @@ -4232,7 +3553,7 @@ fn airBinOp( |
| 4232 | 3553 | operation: []const u8, |
| 4233 | 3554 | info: BuiltinInfo, |
| 4234 | 3555 | ) !CValue { |
| 4235 | | const pt = f.object.dg.pt; |
| 3556 | const pt = f.dg.pt; |
| 4236 | 3557 | const zcu = pt.zcu; |
| 4237 | 3558 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4238 | 3559 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | ... | @@ -4246,21 +3567,21 @@ fn airBinOp( |
| 4246 | 3567 | |
| 4247 | 3568 | const inst_ty = f.typeOfIndex(inst); |
| 4248 | 3569 | |
| 4249 | | const w = &f.object.code.writer; |
| 3570 | const w = &f.code.writer; |
| 4250 | 3571 | const local = try f.allocLocal(inst, inst_ty); |
| 4251 | 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 | 3574 | try v.elem(f, w); |
| 4254 | 3575 | try w.writeAll(" = "); |
| 4255 | | try f.writeCValue(w, lhs, .Other); |
| 3576 | try f.writeCValue(w, lhs, .other); |
| 4256 | 3577 | try v.elem(f, w); |
| 4257 | 3578 | try w.writeByte(' '); |
| 4258 | 3579 | try w.writeAll(operator); |
| 4259 | 3580 | try w.writeByte(' '); |
| 4260 | | try f.writeCValue(w, rhs, .Other); |
| 3581 | try f.writeCValue(w, rhs, .other); |
| 4261 | 3582 | try v.elem(f, w); |
| 4262 | 3583 | try w.writeByte(';'); |
| 4263 | | try f.object.newline(); |
| 3584 | try f.newline(); |
| 4264 | 3585 | try v.end(f, inst, w); |
| 4265 | 3586 | |
| 4266 | 3587 | return local; |
| ... | ... | @@ -4272,7 +3593,7 @@ fn airCmpOp( |
| 4272 | 3593 | data: anytype, |
| 4273 | 3594 | operator: std.math.CompareOperator, |
| 4274 | 3595 | ) !CValue { |
| 4275 | | const pt = f.object.dg.pt; |
| 3596 | const pt = f.dg.pt; |
| 4276 | 3597 | const zcu = pt.zcu; |
| 4277 | 3598 | const lhs_ty = f.typeOf(data.lhs); |
| 4278 | 3599 | const scalar_ty = lhs_ty.scalarType(zcu); |
| ... | ... | @@ -4297,26 +3618,26 @@ fn airCmpOp( |
| 4297 | 3618 | |
| 4298 | 3619 | const rhs_ty = f.typeOf(data.rhs); |
| 4299 | 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 | 3622 | const local = try f.allocLocal(inst, inst_ty); |
| 4302 | 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)); |
| 4304 | | try f.writeCValue(w, local, .Other); |
| 3624 | try f.writeCValue(w, local, .other); |
| 4305 | 3625 | try v.elem(f, w); |
| 4306 | | try a.assign(f, w); |
| 3626 | try w.writeAll(" = "); |
| 4307 | 3627 | if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) { |
| 4308 | 3628 | .lt, .neq, .gt => "false", |
| 4309 | 3629 | .lte, .eq, .gte => "true", |
| 4310 | 3630 | }) else { |
| 4311 | 3631 | if (need_cast) try w.writeAll("(void*)"); |
| 4312 | | try f.writeCValue(w, lhs, .Other); |
| 3632 | try f.writeCValue(w, lhs, .other); |
| 4313 | 3633 | try v.elem(f, w); |
| 4314 | 3634 | try w.writeAll(compareOperatorC(operator)); |
| 4315 | 3635 | if (need_cast) try w.writeAll("(void*)"); |
| 4316 | | try f.writeCValue(w, rhs, .Other); |
| 3636 | try f.writeCValue(w, rhs, .other); |
| 4317 | 3637 | try v.elem(f, w); |
| 4318 | 3638 | } |
| 4319 | | try a.end(f, w); |
| 3639 | try w.writeByte(';'); |
| 3640 | try f.newline(); |
| 4320 | 3641 | try v.end(f, inst, w); |
| 4321 | 3642 | |
| 4322 | 3643 | return local; |
| ... | ... | @@ -4327,9 +3648,8 @@ fn airEquality( |
| 4327 | 3648 | inst: Air.Inst.Index, |
| 4328 | 3649 | operator: std.math.CompareOperator, |
| 4329 | 3650 | ) !CValue { |
| 4330 | | const pt = f.object.dg.pt; |
| 3651 | const pt = f.dg.pt; |
| 4331 | 3652 | const zcu = pt.zcu; |
| 4332 | | const ctype_pool = &f.object.dg.ctype_pool; |
| 4333 | 3653 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4334 | 3654 | |
| 4335 | 3655 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | ... | @@ -4350,54 +3670,64 @@ fn airEquality( |
| 4350 | 3670 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4351 | 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 | 3682 | const local = try f.allocLocal(inst, .bool); |
| 4355 | | const a = try Assignment.start(f, w, .bool); |
| 4356 | | try f.writeCValue(w, local, .Other); |
| 4357 | | try a.assign(f, w); |
| 3683 | try f.writeCValue(w, local, .other); |
| 3684 | try w.writeAll(" = "); |
| 4358 | 3685 | |
| 4359 | | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); |
| 4360 | | if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) { |
| 4361 | | .lt, .lte, .gte, .gt => unreachable, |
| 4362 | | .neq => "false", |
| 4363 | | .eq => "true", |
| 4364 | | }) else switch (operand_ctype.info(ctype_pool)) { |
| 4365 | | .basic, .pointer => { |
| 4366 | | try f.writeCValue(w, lhs, .Other); |
| 4367 | | try w.writeAll(compareOperatorC(operator)); |
| 4368 | | try f.writeCValue(w, rhs, .Other); |
| 4369 | | }, |
| 4370 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 4371 | | .aggregate => |aggregate| if (aggregate.fields.len == 2 and |
| 4372 | | (aggregate.fields.at(0, ctype_pool).name.index == .is_null or |
| 4373 | | aggregate.fields.at(1, ctype_pool).name.index == .is_null)) |
| 4374 | | { |
| 4375 | | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 4376 | | try w.writeAll(" || "); |
| 4377 | | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 4378 | | try w.writeAll(" ? "); |
| 4379 | | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 4380 | | try w.writeAll(compareOperatorC(operator)); |
| 4381 | | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 4382 | | try w.writeAll(" : "); |
| 4383 | | try f.writeCValueMember(w, lhs, .{ .identifier = "payload" }); |
| 4384 | | try w.writeAll(compareOperatorC(operator)); |
| 4385 | | try f.writeCValueMember(w, rhs, .{ .identifier = "payload" }); |
| 4386 | | } else for (0..aggregate.fields.len) |field_index| { |
| 4387 | | if (field_index > 0) try w.writeAll(switch (operator) { |
| 4388 | | .lt, .lte, .gte, .gt => unreachable, |
| 4389 | | .eq => " && ", |
| 4390 | | .neq => " || ", |
| 4391 | | }); |
| 4392 | | const field_name: CValue = .{ |
| 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); |
| 3686 | switch (operand_ty.zigTypeTag(zcu)) { |
| 3687 | .optional => switch (CType.classifyOptional(operand_ty, zcu)) { |
| 3688 | .npv_payload => unreachable, // opv optional |
| 3689 | |
| 3690 | .error_set, .ptr_like => {}, |
| 3691 | |
| 3692 | .slice_like => unreachable, // equality is not defined on slices |
| 3693 | |
| 3694 | .opv_payload => { |
| 3695 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 3696 | try w.writeAll(compareOperatorC(operator)); |
| 3697 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 3698 | try w.writeByte(';'); |
| 3699 | try f.newline(); |
| 3700 | return local; |
| 3701 | }, |
| 3702 | |
| 3703 | .@"struct" => { |
| 3704 | // `lhs.is_null || rhs.is_null ? lhs.is_null == rhs.is_null : lhs.payload == rhs.payload` |
| 3705 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 3706 | try w.writeAll(" || "); |
| 3707 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 3708 | try w.writeAll(" ? "); |
| 3709 | try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" }); |
| 3710 | try w.writeAll(compareOperatorC(operator)); |
| 3711 | try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" }); |
| 3712 | try w.writeAll(" : "); |
| 3713 | try f.writeCValueMember(w, lhs, .{ .identifier = "payload" }); |
| 3714 | try w.writeAll(compareOperatorC(operator)); |
| 3715 | try f.writeCValueMember(w, rhs, .{ .identifier = "payload" }); |
| 3716 | try w.writeByte(';'); |
| 3717 | try f.newline(); |
| 3718 | return local; |
| 3719 | }, |
| 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 | 3732 | return local; |
| 4403 | 3733 | } |
| ... | ... | @@ -4408,18 +3738,18 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4408 | 3738 | const operand = try f.resolveInst(un_op); |
| 4409 | 3739 | try reap(f, inst, &.{un_op}); |
| 4410 | 3740 | |
| 4411 | | const w = &f.object.code.writer; |
| 3741 | const w = &f.code.writer; |
| 4412 | 3742 | const local = try f.allocLocal(inst, .bool); |
| 4413 | | try f.writeCValue(w, local, .Other); |
| 3743 | try f.writeCValue(w, local, .other); |
| 4414 | 3744 | try w.writeAll(" = "); |
| 4415 | | try f.writeCValue(w, operand, .Other); |
| 3745 | try f.writeCValue(w, operand, .other); |
| 4416 | 3746 | try w.print(" < sizeof({f}) / sizeof(*{0f});", .{fmtIdentSolo("zig_errorName")}); |
| 4417 | | try f.object.newline(); |
| 3747 | try f.newline(); |
| 4418 | 3748 | return local; |
| 4419 | 3749 | } |
| 4420 | 3750 | |
| 4421 | 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 | 3753 | const zcu = pt.zcu; |
| 4424 | 3754 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4425 | 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 | 3762 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4433 | 3763 | const elem_ty = inst_scalar_ty.indexableElem(zcu); |
| 4434 | 3764 | assert(elem_ty.hasRuntimeBits(zcu)); |
| 4435 | | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 4436 | 3765 | |
| 4437 | 3766 | const local = try f.allocLocal(inst, inst_ty); |
| 4438 | | const w = &f.object.code.writer; |
| 3767 | const w = &f.code.writer; |
| 4439 | 3768 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 4440 | | const a = try Assignment.start(f, w, inst_scalar_ctype); |
| 4441 | | try f.writeCValue(w, local, .Other); |
| 3769 | try f.writeCValue(w, local, .other); |
| 4442 | 3770 | try v.elem(f, w); |
| 4443 | | try a.assign(f, w); |
| 3771 | try w.writeAll(" = "); |
| 4444 | 3772 | // We must convert to and from integer types to prevent UB if the operation |
| 4445 | 3773 | // results in a NULL pointer, or if LHS is NULL. The operation is only UB |
| 4446 | 3774 | // if the result is NULL and then dereferenced. |
| 4447 | 3775 | try w.writeByte('('); |
| 4448 | | try f.renderCType(w, inst_scalar_ctype); |
| 3776 | try f.renderType(w, inst_scalar_ty); |
| 4449 | 3777 | try w.writeAll(")(((uintptr_t)"); |
| 4450 | | try f.writeCValue(w, lhs, .Other); |
| 3778 | try f.writeCValue(w, lhs, .other); |
| 4451 | 3779 | try v.elem(f, w); |
| 4452 | | try w.writeAll(") "); |
| 4453 | | try w.writeByte(operator); |
| 4454 | | try w.writeAll(" ("); |
| 4455 | | try f.writeCValue(w, rhs, .Other); |
| 3780 | try w.print(") {c} (", .{operator}); |
| 3781 | try f.writeCValue(w, rhs, .other); |
| 4456 | 3782 | try v.elem(f, w); |
| 4457 | 3783 | try w.writeAll("*sizeof("); |
| 4458 | 3784 | try f.renderType(w, elem_ty); |
| 4459 | | try w.writeAll(")))"); |
| 4460 | | try a.end(f, w); |
| 3785 | try w.writeAll(")));"); |
| 3786 | try f.newline(); |
| 4461 | 3787 | try v.end(f, inst, w); |
| 4462 | 3788 | return local; |
| 4463 | 3789 | } |
| 4464 | 3790 | |
| 4465 | 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 | 3793 | const zcu = pt.zcu; |
| 4468 | 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 | 3803 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4478 | 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 | 3807 | const local = try f.allocLocal(inst, inst_ty); |
| 4482 | 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 | 3810 | try v.elem(f, w); |
| 4485 | 3811 | // (lhs <> rhs) ? lhs : rhs |
| 4486 | 3812 | try w.writeAll(" = ("); |
| 4487 | | try f.writeCValue(w, lhs, .Other); |
| 3813 | try f.writeCValue(w, lhs, .other); |
| 4488 | 3814 | try v.elem(f, w); |
| 4489 | 3815 | try w.writeByte(' '); |
| 4490 | 3816 | try w.writeByte(operator); |
| 4491 | 3817 | try w.writeByte(' '); |
| 4492 | | try f.writeCValue(w, rhs, .Other); |
| 3818 | try f.writeCValue(w, rhs, .other); |
| 4493 | 3819 | try v.elem(f, w); |
| 4494 | 3820 | try w.writeAll(") ? "); |
| 4495 | | try f.writeCValue(w, lhs, .Other); |
| 3821 | try f.writeCValue(w, lhs, .other); |
| 4496 | 3822 | try v.elem(f, w); |
| 4497 | 3823 | try w.writeAll(" : "); |
| 4498 | | try f.writeCValue(w, rhs, .Other); |
| 3824 | try f.writeCValue(w, rhs, .other); |
| 4499 | 3825 | try v.elem(f, w); |
| 4500 | 3826 | try w.writeByte(';'); |
| 4501 | | try f.object.newline(); |
| 3827 | try f.newline(); |
| 4502 | 3828 | try v.end(f, inst, w); |
| 4503 | 3829 | |
| 4504 | 3830 | return local; |
| 4505 | 3831 | } |
| 4506 | 3832 | |
| 4507 | 3833 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4508 | | const pt = f.object.dg.pt; |
| 4509 | | const zcu = pt.zcu; |
| 4510 | 3834 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4511 | 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 | 3839 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4516 | 3840 | |
| 4517 | 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 | 3844 | const local = try f.allocLocal(inst, inst_ty); |
| 4522 | | { |
| 4523 | | const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete)); |
| 4524 | | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 4525 | | try a.assign(f, w); |
| 4526 | | try f.writeCValue(w, ptr, .Other); |
| 4527 | | try a.end(f, w); |
| 4528 | | } |
| 4529 | | { |
| 4530 | | const a = try Assignment.start(f, w, .usize); |
| 4531 | | try f.writeCValueMember(w, local, .{ .identifier = "len" }); |
| 4532 | | try a.assign(f, w); |
| 4533 | | try f.writeCValue(w, len, .Other); |
| 4534 | | try a.end(f, w); |
| 4535 | | } |
| 3845 | |
| 3846 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 3847 | try w.writeAll(" = "); |
| 3848 | try f.writeCValue(w, ptr, .other); |
| 3849 | try w.writeByte(';'); |
| 3850 | try f.newline(); |
| 3851 | |
| 3852 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); |
| 3853 | try w.writeAll(" = "); |
| 3854 | try f.writeCValue(w, len, .other); |
| 3855 | try w.writeByte(';'); |
| 3856 | try f.newline(); |
| 3857 | |
| 4536 | 3858 | return local; |
| 4537 | 3859 | } |
| 4538 | 3860 | |
| ... | ... | @@ -4541,14 +3863,14 @@ fn airCall( |
| 4541 | 3863 | inst: Air.Inst.Index, |
| 4542 | 3864 | modifier: std.builtin.CallModifier, |
| 4543 | 3865 | ) !CValue { |
| 4544 | | const pt = f.object.dg.pt; |
| 3866 | const pt = f.dg.pt; |
| 4545 | 3867 | const zcu = pt.zcu; |
| 4546 | 3868 | const ip = &zcu.intern_pool; |
| 4547 | 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; |
| 4551 | | const w = &f.object.code.writer; |
| 3872 | const gpa = f.dg.gpa; |
| 3873 | const w = &f.code.writer; |
| 4552 | 3874 | |
| 4553 | 3875 | const call = f.air.unwrapCall(inst); |
| 4554 | 3876 | const args = call.args; |
| ... | ... | @@ -4557,27 +3879,11 @@ fn airCall( |
| 4557 | 3879 | defer gpa.free(resolved_args); |
| 4558 | 3880 | for (resolved_args, args) |*resolved_arg, arg| { |
| 4559 | 3881 | const arg_ty = f.typeOf(arg); |
| 4560 | | const arg_ctype = try f.ctypeFromType(arg_ty, .parameter); |
| 4561 | | if (arg_ctype.index == .void) { |
| 3882 | if (!arg_ty.hasRuntimeBits(zcu)) { |
| 4562 | 3883 | resolved_arg.* = .none; |
| 4563 | 3884 | continue; |
| 4564 | 3885 | } |
| 4565 | 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 | 3889 | const callee = try f.resolveInst(call.callee); |
| ... | ... | @@ -4596,28 +3902,22 @@ fn airCall( |
| 4596 | 3902 | }; |
| 4597 | 3903 | const fn_info = zcu.typeToFunc(if (callee_is_ptr) callee_ty.childType(zcu) else callee_ty).?; |
| 4598 | 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 | 3906 | const result_local = result: { |
| 4605 | 3907 | if (modifier == .always_tail) { |
| 4606 | 3908 | try w.writeAll("zig_always_tail return "); |
| 4607 | 3909 | break :result .none; |
| 4608 | | } else if (ret_ctype.index == .void) { |
| 3910 | } else if (!ret_ty.hasRuntimeBits(zcu)) { |
| 4609 | 3911 | break :result .none; |
| 4610 | 3912 | } else if (f.liveness.isUnused(inst)) { |
| 4611 | | try w.writeByte('('); |
| 4612 | | try f.renderCType(w, .void); |
| 4613 | | try w.writeByte(')'); |
| 3913 | try w.writeAll("(void)"); |
| 4614 | 3914 | break :result .none; |
| 4615 | 3915 | } else { |
| 4616 | 3916 | const local = try f.allocAlignedLocal(inst, .{ |
| 4617 | | .ctype = ret_ctype, |
| 4618 | | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)), |
| 3917 | .type = ret_ty, |
| 3918 | .alignment = .none, |
| 4619 | 3919 | }); |
| 4620 | | try f.writeCValue(w, local, .Other); |
| 3920 | try f.writeCValue(w, local, .other); |
| 4621 | 3921 | try w.writeAll(" = "); |
| 4622 | 3922 | break :result local; |
| 4623 | 3923 | } |
| ... | ... | @@ -4644,8 +3944,19 @@ fn airCall( |
| 4644 | 3944 | if (!callee_is_ptr) try w.writeByte('&'); |
| 4645 | 3945 | } |
| 4646 | 3946 | switch (modifier) { |
| 4647 | | .auto, .always_tail => try f.object.dg.renderNavName(w, fn_nav), |
| 4648 | | inline .never_tail, .never_inline => |m| try w.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))), |
| 3947 | .auto, .always_tail => try renderNavName(w, fn_nav, ip), |
| 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 | 3960 | else => unreachable, |
| 4650 | 3961 | } |
| 4651 | 3962 | if (need_cast) try w.writeByte(')'); |
| ... | ... | @@ -4658,7 +3969,7 @@ fn airCall( |
| 4658 | 3969 | else => unreachable, |
| 4659 | 3970 | } |
| 4660 | 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 | 3975 | try w.writeByte('('); |
| ... | ... | @@ -4667,38 +3978,20 @@ fn airCall( |
| 4667 | 3978 | if (resolved_arg == .none) continue; |
| 4668 | 3979 | if (need_comma) try w.writeAll(", "); |
| 4669 | 3980 | need_comma = true; |
| 4670 | | try f.writeCValue(w, resolved_arg, .FunctionArgument); |
| 4671 | | try f.freeCValue(inst, resolved_arg); |
| 3981 | try f.writeCValue(w, resolved_arg, .other); |
| 4672 | 3982 | } |
| 4673 | 3983 | try w.writeAll(");"); |
| 4674 | 3984 | switch (modifier) { |
| 4675 | 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: { |
| 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; |
| 3989 | return result_local; |
| 4697 | 3990 | } |
| 4698 | 3991 | |
| 4699 | 3992 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4700 | 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 | 3995 | // TODO re-evaluate whether to emit these or not. If we naively emit |
| 4703 | 3996 | // these directives, the output file will report bogus line numbers because |
| 4704 | 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 | 4000 | // newlines until the next dbg_stmt occurs. |
| 4708 | 4001 | // Perhaps an additional compilation option is in order? |
| 4709 | 4002 | //try w.print("#line {d}", .{dbg_stmt.line + 1}); |
| 4710 | | //try f.object.newline(); |
| 4003 | //try f.newline(); |
| 4711 | 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 | 4006 | return .none; |
| 4714 | 4007 | } |
| 4715 | 4008 | |
| 4716 | 4009 | fn airDbgEmptyStmt(f: *Function, _: Air.Inst.Index) !CValue { |
| 4717 | | try f.object.code.writer.writeAll("(void)0;"); |
| 4718 | | try f.object.newline(); |
| 4010 | try f.code.writer.writeAll("(void)0;"); |
| 4011 | try f.newline(); |
| 4719 | 4012 | return .none; |
| 4720 | 4013 | } |
| 4721 | 4014 | |
| 4722 | 4015 | fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4723 | | const pt = f.object.dg.pt; |
| 4016 | const pt = f.dg.pt; |
| 4724 | 4017 | const zcu = pt.zcu; |
| 4725 | 4018 | const ip = &zcu.intern_pool; |
| 4726 | 4019 | const block = f.air.unwrapDbgBlock(inst); |
| 4727 | 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 | 4022 | try w.print("/* inline:{f} */", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); |
| 4730 | | try f.object.newline(); |
| 4023 | try f.newline(); |
| 4731 | 4024 | return lowerBlock(f, inst, block.body); |
| 4732 | 4025 | } |
| 4733 | 4026 | |
| 4734 | 4027 | fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4735 | | const pt = f.object.dg.pt; |
| 4028 | const pt = f.dg.pt; |
| 4736 | 4029 | const zcu = pt.zcu; |
| 4737 | 4030 | const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4738 | 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 | 4034 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); |
| 4742 | 4035 | |
| 4743 | 4036 | try reap(f, inst, &.{pl_op.operand}); |
| 4744 | | const w = &f.object.code.writer; |
| 4037 | const w = &f.code.writer; |
| 4745 | 4038 | try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) }); |
| 4746 | | try f.object.newline(); |
| 4039 | try f.newline(); |
| 4747 | 4040 | return .none; |
| 4748 | 4041 | } |
| 4749 | 4042 | |
| ... | ... | @@ -4753,13 +4046,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4753 | 4046 | } |
| 4754 | 4047 | |
| 4755 | 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 | 4050 | const zcu = pt.zcu; |
| 4758 | 4051 | const liveness_block = f.liveness.getBlock(inst); |
| 4759 | 4052 | |
| 4760 | 4053 | const block_id = f.next_block_index; |
| 4761 | 4054 | f.next_block_index += 1; |
| 4762 | | const w = &f.object.code.writer; |
| 4055 | const w = &f.code.writer; |
| 4763 | 4056 | |
| 4764 | 4057 | const inst_ty = f.typeOfIndex(inst); |
| 4765 | 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 | 4060 | else |
| 4768 | 4061 | .none; |
| 4769 | 4062 | |
| 4770 | | try f.blocks.putNoClobber(f.object.dg.gpa, inst, .{ |
| 4063 | try f.blocks.putNoClobber(f.dg.gpa, inst, .{ |
| 4771 | 4064 | .block_id = block_id, |
| 4772 | 4065 | .result = result, |
| 4773 | 4066 | }); |
| ... | ... | @@ -4782,23 +4075,23 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4782 | 4075 | } |
| 4783 | 4076 | |
| 4784 | 4077 | // noreturn blocks have no `br` instructions reaching them, so we don't want a label |
| 4785 | | if (f.object.dg.is_naked_fn) { |
| 4786 | | if (f.object.dg.expected_block) |expected_block| { |
| 4078 | if (f.dg.is_naked_fn) { |
| 4079 | if (f.dg.expected_block) |expected_block| { |
| 4787 | 4080 | if (block_id != expected_block) |
| 4788 | 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 | 4084 | } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) { |
| 4792 | 4085 | // label must be followed by an expression, include an empty one. |
| 4793 | 4086 | try w.print("\nzig_block_{d}:;", .{block_id}); |
| 4794 | | try f.object.newline(); |
| 4087 | try f.newline(); |
| 4795 | 4088 | } |
| 4796 | 4089 | |
| 4797 | 4090 | return result; |
| 4798 | 4091 | } |
| 4799 | 4092 | |
| 4800 | 4093 | fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4801 | | const pt = f.object.dg.pt; |
| 4094 | const pt = f.dg.pt; |
| 4802 | 4095 | const unwrapped_try = f.air.unwrapTry(inst); |
| 4803 | 4096 | const body = unwrapped_try.else_body; |
| 4804 | 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 | 4099 | } |
| 4807 | 4100 | |
| 4808 | 4101 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4809 | | const pt = f.object.dg.pt; |
| 4102 | const pt = f.dg.pt; |
| 4810 | 4103 | const unwrapped_try = f.air.unwrapTryPtr(inst); |
| 4811 | 4104 | const body = unwrapped_try.else_body; |
| 4812 | 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 | 4114 | err_union_ty: Type, |
| 4822 | 4115 | is_ptr: bool, |
| 4823 | 4116 | ) !CValue { |
| 4824 | | const pt = f.object.dg.pt; |
| 4117 | const pt = f.dg.pt; |
| 4825 | 4118 | const zcu = pt.zcu; |
| 4826 | 4119 | const err_union = try f.resolveInst(operand); |
| 4827 | 4120 | const inst_ty = f.typeOfIndex(inst); |
| 4828 | 4121 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4829 | | const w = &f.object.code.writer; |
| 4122 | const w = &f.code.writer; |
| 4830 | 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)) { |
| 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(") "); |
| 4125 | try w.writeAll("if ("); |
| 4851 | 4126 | |
| 4852 | | try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false); |
| 4853 | | try f.object.newline(); |
| 4854 | | if (f.object.dg.expected_block) |_| |
| 4855 | | return f.fail("runtime code not allowed in naked function", .{}); |
| 4856 | | } |
| 4127 | // Reap the operand so that it can be reused inside genBody. |
| 4128 | // Remember we must avoid calling reap() twice for the same operand |
| 4129 | // in this function. |
| 4130 | try reap(f, inst, &.{operand}); |
| 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 | 4143 | // Now we have the "then branch" (in terms of the liveness data); process any deaths. |
| 4859 | 4144 | for (liveness_condbr.then_deaths) |death| { |
| 4860 | 4145 | try die(f, inst, death.toRef()); |
| 4861 | 4146 | } |
| 4862 | 4147 | |
| 4863 | | if (!payload_has_bits) { |
| 4148 | if (!payload_ty.hasRuntimeBits(zcu)) { |
| 4864 | 4149 | if (!is_ptr) { |
| 4865 | 4150 | return .none; |
| 4866 | 4151 | } else { |
| ... | ... | @@ -4873,14 +4158,14 @@ fn lowerTry( |
| 4873 | 4158 | if (f.liveness.isUnused(inst)) return .none; |
| 4874 | 4159 | |
| 4875 | 4160 | const local = try f.allocLocal(inst, inst_ty); |
| 4876 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 4877 | | try f.writeCValue(w, local, .Other); |
| 4878 | | try a.assign(f, w); |
| 4161 | try f.writeCValue(w, local, .other); |
| 4162 | try w.writeAll(" = "); |
| 4879 | 4163 | if (is_ptr) { |
| 4880 | 4164 | try w.writeByte('&'); |
| 4881 | 4165 | try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" }); |
| 4882 | 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 | 4169 | return local; |
| 4885 | 4170 | } |
| 4886 | 4171 | |
| ... | ... | @@ -4888,25 +4173,24 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { |
| 4888 | 4173 | const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4889 | 4174 | const block = f.blocks.get(branch.block_inst).?; |
| 4890 | 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 | 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 | 4181 | return; |
| 4897 | 4182 | } |
| 4898 | 4183 | |
| 4899 | 4184 | // If result is .none then the value of the block is unused. |
| 4900 | 4185 | if (result != .none) { |
| 4901 | | const operand_ty = f.typeOf(branch.operand); |
| 4902 | 4186 | const operand = try f.resolveInst(branch.operand); |
| 4903 | 4187 | try reap(f, inst, &.{branch.operand}); |
| 4904 | 4188 | |
| 4905 | | const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete)); |
| 4906 | | try f.writeCValue(w, result, .Other); |
| 4907 | | try a.assign(f, w); |
| 4908 | | try f.writeCValue(w, operand, .Other); |
| 4909 | | try a.end(f, w); |
| 4189 | try f.writeCValue(w, result, .other); |
| 4190 | try w.writeAll(" = "); |
| 4191 | try f.writeCValue(w, operand, .other); |
| 4192 | try w.writeByte(';'); |
| 4193 | try f.newline(); |
| 4910 | 4194 | } |
| 4911 | 4195 | |
| 4912 | 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 | 4198 | |
| 4915 | 4199 | fn airRepeat(f: *Function, inst: Air.Inst.Index) !void { |
| 4916 | 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 | 4204 | fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { |
| 4921 | | const pt = f.object.dg.pt; |
| 4205 | const pt = f.dg.pt; |
| 4922 | 4206 | const zcu = pt.zcu; |
| 4923 | 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 | 4210 | if (try f.air.value(br.operand, pt)) |cond_val| { |
| 4927 | 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 | 4234 | // Runtime-known dispatch. Set the switch condition, and branch back. |
| 4951 | 4235 | const cond = try f.resolveInst(br.operand); |
| 4952 | 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 | 4238 | try w.writeAll(" = "); |
| 4955 | | try f.writeCValue(w, cond, .Other); |
| 4239 | try f.writeCValue(w, cond, .other); |
| 4956 | 4240 | try w.writeByte(';'); |
| 4957 | | try f.object.newline(); |
| 4241 | try f.newline(); |
| 4958 | 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 | 4255 | } |
| 4972 | 4256 | |
| 4973 | 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 | 4259 | const zcu = pt.zcu; |
| 4976 | | const target = &f.object.dg.mod.resolved_target.result; |
| 4977 | | const ctype_pool = &f.object.dg.ctype_pool; |
| 4978 | | const w = &f.object.code.writer; |
| 4260 | const target = &f.dg.mod.resolved_target.result; |
| 4261 | const w = &f.code.writer; |
| 4979 | 4262 | |
| 4980 | 4263 | if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) { |
| 4981 | 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 | 4269 | |
| 4987 | 4270 | if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) { |
| 4988 | 4271 | const local = try f.allocLocal(null, dest_ty); |
| 4989 | | try f.writeCValue(w, local, .Other); |
| 4272 | try f.writeCValue(w, local, .other); |
| 4990 | 4273 | try w.writeAll(" = ("); |
| 4991 | 4274 | try f.renderType(w, dest_ty); |
| 4992 | 4275 | try w.writeByte(')'); |
| 4993 | | try f.writeCValue(w, operand, .Other); |
| 4276 | try f.writeCValue(w, operand, .other); |
| 4994 | 4277 | try w.writeByte(';'); |
| 4995 | | try f.object.newline(); |
| 4278 | try f.newline(); |
| 4996 | 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 | 4282 | const local = try f.allocLocal(null, dest_ty); |
| 5010 | 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 | 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 | 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 | 4287 | const offset = dest_ty.abiSize(zcu) - operand_ty.abiSize(zcu); |
| 5015 | 4288 | try w.writeAll("memcpy((char *)&"); |
| 5016 | | try f.writeCValue(w, local, .Other); |
| 4289 | try f.writeCValue(w, local, .other); |
| 5017 | 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 | 4295 | try w.print(", {d});", .{operand_ty.abiSize(zcu)}); |
| 5020 | 4296 | } else if (target.cpu.arch.endian() == .big and operand_ty.isAbiInt(zcu) and !dest_ty.isAbiInt(zcu)) { |
| 5021 | 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 | 4298 | const offset = operand_ty.abiSize(zcu) - dest_ty.abiSize(zcu); |
| 5023 | 4299 | try w.writeAll("memcpy(&"); |
| 5024 | | try f.writeCValue(w, local, .Other); |
| 4300 | try f.writeCValue(w, local, .other); |
| 5025 | 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 | 4306 | try w.print(" + {d}, {d});", .{ offset, dest_ty.abiSize(zcu) }); |
| 5028 | 4307 | } else { |
| 5029 | 4308 | try w.writeAll("memcpy(&"); |
| 5030 | | try f.writeCValue(w, local, .Other); |
| 4309 | try f.writeCValue(w, local, .other); |
| 5031 | 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 | 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 | 4320 | // Ensure padding bits have the expected value. |
| 5039 | 4321 | if (dest_ty.isAbiInt(zcu)) { |
| 5040 | | const dest_ctype = try f.ctypeFromType(dest_ty, .complete); |
| 5041 | | const dest_info = dest_ty.intInfo(zcu); |
| 5042 | | var bits: u16 = dest_info.bits; |
| 5043 | | var wrap_ctype: ?CType = null; |
| 5044 | | var need_bitcasts = false; |
| 5045 | | |
| 5046 | | try f.writeCValue(w, local, .Other); |
| 5047 | | switch (dest_ctype.info(ctype_pool)) { |
| 5048 | | else => {}, |
| 5049 | | .array => |array_info| { |
| 5050 | | try w.print("[{d}]", .{switch (target.cpu.arch.endian()) { |
| 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; |
| 4322 | switch (CType.classifyInt(dest_ty, zcu)) { |
| 4323 | .void => unreachable, // opv |
| 4324 | .small => { |
| 4325 | try f.writeCValue(w, local, .other); |
| 4326 | try w.writeAll(" = zig_wrap_"); |
| 4327 | try f.dg.renderTypeForBuiltinFnName(w, dest_ty); |
| 4328 | try w.writeByte('('); |
| 4329 | try f.writeCValue(w, local, .other); |
| 4330 | try f.dg.renderBuiltinInfo(w, dest_ty, .bits); |
| 4331 | try w.writeAll(");"); |
| 4332 | try f.newline(); |
| 5059 | 4333 | }, |
| 5060 | | } |
| 5061 | | try w.writeAll(" = "); |
| 5062 | | if (need_bitcasts) { |
| 5063 | | try w.writeAll("zig_bitCast_"); |
| 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, |
| 4334 | .big => |big| { |
| 4335 | const dest_info = dest_ty.intInfo(zcu); |
| 4336 | const padding_index: u16 = switch (target.cpu.arch.endian()) { |
| 4337 | .little => big.limbs_len - 1, |
| 5085 | 4338 | .big => 0, |
| 5086 | | }, |
| 5087 | | }), |
| 4339 | }; |
| 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 | 4363 | return local; |
| 5098 | 4364 | } |
| 5099 | 4365 | |
| 5100 | | fn airTrap(f: *Function, w: *Writer) !void { |
| 4366 | fn airTrap(f: *Function) !void { |
| 5101 | 4367 | // Not even allowed to call trap in a naked function. |
| 5102 | | if (f.object.dg.is_naked_fn) return; |
| 5103 | | try w.writeAll("zig_trap();\n"); |
| 4368 | if (f.dg.is_naked_fn) return; |
| 4369 | try f.code.writer.writeAll("zig_trap();\n"); |
| 5104 | 4370 | } |
| 5105 | 4371 | |
| 5106 | 4372 | fn airBreakpoint(f: *Function) !CValue { |
| 5107 | | const w = &f.object.code.writer; |
| 4373 | const w = &f.code.writer; |
| 5108 | 4374 | try w.writeAll("zig_breakpoint();"); |
| 5109 | | try f.object.newline(); |
| 4375 | try f.newline(); |
| 5110 | 4376 | return .none; |
| 5111 | 4377 | } |
| 5112 | 4378 | |
| 5113 | 4379 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5114 | | const w = &f.object.code.writer; |
| 4380 | const w = &f.code.writer; |
| 5115 | 4381 | const local = try f.allocLocal(inst, .usize); |
| 5116 | | try f.writeCValue(w, local, .Other); |
| 4382 | try f.writeCValue(w, local, .other); |
| 5117 | 4383 | try w.writeAll(" = ("); |
| 5118 | 4384 | try f.renderType(w, .usize); |
| 5119 | 4385 | try w.writeAll(")zig_return_address();"); |
| 5120 | | try f.object.newline(); |
| 4386 | try f.newline(); |
| 5121 | 4387 | return local; |
| 5122 | 4388 | } |
| 5123 | 4389 | |
| 5124 | 4390 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5125 | | const w = &f.object.code.writer; |
| 4391 | const w = &f.code.writer; |
| 5126 | 4392 | const local = try f.allocLocal(inst, .usize); |
| 5127 | | try f.writeCValue(w, local, .Other); |
| 4393 | try f.writeCValue(w, local, .other); |
| 5128 | 4394 | try w.writeAll(" = ("); |
| 5129 | 4395 | try f.renderType(w, .usize); |
| 5130 | 4396 | try w.writeAll(")zig_frame_address();"); |
| 5131 | | try f.object.newline(); |
| 4397 | try f.newline(); |
| 5132 | 4398 | return local; |
| 5133 | 4399 | } |
| 5134 | 4400 | |
| 5135 | | fn airUnreach(o: *Object) !void { |
| 4401 | fn airUnreach(f: *Function) !void { |
| 5136 | 4402 | // Not even allowed to call unreachable in a naked function. |
| 5137 | | if (o.dg.is_naked_fn) return; |
| 5138 | | try o.code.writer.writeAll("zig_unreachable();\n"); |
| 4403 | if (f.dg.is_naked_fn) return; |
| 4404 | try f.code.writer.writeAll("zig_unreachable();\n"); |
| 5139 | 4405 | } |
| 5140 | 4406 | |
| 5141 | 4407 | fn airLoop(f: *Function, inst: Air.Inst.Index) !void { |
| 5142 | 4408 | const block = f.air.unwrapBlock(inst); |
| 5143 | | const w = &f.object.code.writer; |
| 4409 | const w = &f.code.writer; |
| 5144 | 4410 | |
| 5145 | 4411 | // `repeat` instructions matching this loop will branch to |
| 5146 | 4412 | // this label. Since we need a label for arbitrary `repeat` |
| 5147 | 4413 | // anyway, there's actually no need to use a "real" looping |
| 5148 | 4414 | // construct at all! |
| 5149 | 4415 | try w.print("zig_loop_{d}:", .{@intFromEnum(inst)}); |
| 5150 | | try f.object.newline(); |
| 4416 | try f.newline(); |
| 5151 | 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 | 4424 | const then_body = cond_br.then_body; |
| 5159 | 4425 | const else_body = cond_br.else_body; |
| 5160 | 4426 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 5161 | | const w = &f.object.code.writer; |
| 4427 | const w = &f.code.writer; |
| 5162 | 4428 | |
| 5163 | 4429 | try w.writeAll("if ("); |
| 5164 | | try f.writeCValue(w, cond, .Other); |
| 4430 | try f.writeCValue(w, cond, .other); |
| 5165 | 4431 | try w.writeAll(") "); |
| 5166 | 4432 | |
| 5167 | 4433 | try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false); |
| 5168 | | try f.object.newline(); |
| 5169 | | if (else_body.len > 0) if (f.object.dg.expected_block) |_| |
| 4434 | try f.newline(); |
| 4435 | if (else_body.len > 0) if (f.dg.expected_block) |_| |
| 5170 | 4436 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5171 | 4437 | |
| 5172 | 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 | 4450 | } |
| 5185 | 4451 | |
| 5186 | 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 | 4454 | const zcu = pt.zcu; |
| 5189 | | const gpa = f.object.dg.gpa; |
| 4455 | const gpa = f.dg.gpa; |
| 5190 | 4456 | const switch_br = f.air.unwrapSwitch(inst); |
| 5191 | 4457 | const init_condition = try f.resolveInst(switch_br.operand); |
| 5192 | 4458 | try reap(f, inst, &.{switch_br.operand}); |
| 5193 | 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 | 4462 | // For dispatches, we will create a local alloc to contain the condition value. |
| 5197 | 4463 | // This may not result in optimal codegen for switch loops, but it minimizes the |
| 5198 | 4464 | // amount of C code we generate, which is probably more desirable here (and is simpler). |
| 5199 | 4465 | const condition = if (is_dispatch_loop) cond: { |
| 5200 | 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 | 4468 | try w.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)}); |
| 5203 | | try f.object.newline(); |
| 4469 | try f.newline(); |
| 5204 | 4470 | try f.loop_switch_conds.put(gpa, inst, new_local.new_local); |
| 5205 | 4471 | break :cond new_local; |
| 5206 | 4472 | } else init_condition; |
| ... | ... | @@ -5222,9 +4488,9 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5222 | 4488 | try f.renderType(w, lowered_condition_ty); |
| 5223 | 4489 | try w.writeByte(')'); |
| 5224 | 4490 | } |
| 5225 | | try f.writeCValue(w, condition, .Other); |
| 4491 | try f.writeCValue(w, condition, .other); |
| 5226 | 4492 | try w.writeAll(") {"); |
| 5227 | | f.object.indent(); |
| 4493 | f.indent(); |
| 5228 | 4494 | |
| 5229 | 4495 | const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1); |
| 5230 | 4496 | defer gpa.free(liveness.deaths); |
| ... | ... | @@ -5237,7 +4503,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5237 | 4503 | continue; |
| 5238 | 4504 | } |
| 5239 | 4505 | for (case.items) |item| { |
| 5240 | | try f.object.newline(); |
| 4506 | try f.newline(); |
| 5241 | 4507 | try w.writeAll("case "); |
| 5242 | 4508 | const item_value = try f.air.value(item, pt); |
| 5243 | 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 | 4520 | try f.renderType(w, .usize); |
| 5255 | 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 | 4525 | try w.writeByte(':'); |
| 5260 | 4526 | } |
| 5261 | 4527 | try w.writeAll(" {"); |
| 5262 | | f.object.indent(); |
| 5263 | | try f.object.newline(); |
| 4528 | f.indent(); |
| 4529 | try f.newline(); |
| 5264 | 4530 | if (is_dispatch_loop) { |
| 5265 | 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 | 4534 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5269 | | try f.object.outdent(); |
| 4535 | try f.outdent(); |
| 5270 | 4536 | try w.writeByte('}'); |
| 5271 | | if (f.object.dg.expected_block) |_| |
| 4537 | if (f.dg.expected_block) |_| |
| 5272 | 4538 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5273 | 4539 | |
| 5274 | 4540 | // The case body must be noreturn so we don't need to insert a break. |
| 5275 | 4541 | } |
| 5276 | 4542 | |
| 5277 | 4543 | const else_body = it.elseBody(); |
| 5278 | | try f.object.newline(); |
| 4544 | try f.newline(); |
| 5279 | 4545 | |
| 5280 | 4546 | try w.writeAll("default: "); |
| 5281 | 4547 | if (any_range_cases) { |
| ... | ... | @@ -5288,33 +4554,33 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5288 | 4554 | try w.writeAll("if ("); |
| 5289 | 4555 | for (case.items, 0..) |item, item_i| { |
| 5290 | 4556 | if (item_i != 0) try w.writeAll(" || "); |
| 5291 | | try f.writeCValue(w, condition, .Other); |
| 4557 | try f.writeCValue(w, condition, .other); |
| 5292 | 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 | 4561 | for (case.ranges, 0..) |range, range_i| { |
| 5296 | 4562 | if (case.items.len != 0 or range_i != 0) try w.writeAll(" || "); |
| 5297 | 4563 | // "(x >= lower && x <= upper)" |
| 5298 | 4564 | try w.writeByte('('); |
| 5299 | | try f.writeCValue(w, condition, .Other); |
| 4565 | try f.writeCValue(w, condition, .other); |
| 5300 | 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 | 4568 | try w.writeAll(" && "); |
| 5303 | | try f.writeCValue(w, condition, .Other); |
| 4569 | try f.writeCValue(w, condition, .other); |
| 5304 | 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 | 4572 | try w.writeByte(')'); |
| 5307 | 4573 | } |
| 5308 | 4574 | try w.writeAll(") {"); |
| 5309 | | f.object.indent(); |
| 5310 | | try f.object.newline(); |
| 4575 | f.indent(); |
| 4576 | try f.newline(); |
| 5311 | 4577 | if (is_dispatch_loop) { |
| 5312 | 4578 | try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx }); |
| 5313 | 4579 | } |
| 5314 | 4580 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5315 | | try f.object.outdent(); |
| 4581 | try f.outdent(); |
| 5316 | 4582 | try w.writeByte('}'); |
| 5317 | | if (f.object.dg.expected_block) |_| |
| 4583 | if (f.dg.expected_block) |_| |
| 5318 | 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 | 4594 | try die(f, inst, death.toRef()); |
| 5329 | 4595 | } |
| 5330 | 4596 | try genBody(f, else_body); |
| 5331 | | if (f.object.dg.expected_block) |_| |
| 4597 | if (f.dg.expected_block) |_| |
| 5332 | 4598 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5333 | | } else try airUnreach(&f.object); |
| 5334 | | try f.object.newline(); |
| 5335 | | try f.object.outdent(); |
| 4599 | } else try airUnreach(f); |
| 4600 | try f.newline(); |
| 4601 | try f.outdent(); |
| 5336 | 4602 | try w.writeAll("}\n"); |
| 5337 | 4603 | } |
| 5338 | 4604 | |
| 5339 | 4605 | fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool { |
| 5340 | | const dg = f.object.dg; |
| 4606 | const dg = f.dg; |
| 5341 | 4607 | const target = &dg.mod.resolved_target.result; |
| 5342 | 4608 | return switch (constraint[0]) { |
| 5343 | 4609 | '{' => true, |
| ... | ... | @@ -5357,28 +4623,28 @@ fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool |
| 5357 | 4623 | } |
| 5358 | 4624 | |
| 5359 | 4625 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5360 | | const pt = f.object.dg.pt; |
| 4626 | const pt = f.dg.pt; |
| 5361 | 4627 | const zcu = pt.zcu; |
| 5362 | 4628 | const unwrapped_asm = f.air.unwrapAsm(inst); |
| 5363 | 4629 | const is_volatile = unwrapped_asm.is_volatile; |
| 5364 | | const gpa = f.object.dg.gpa; |
| 4630 | const gpa = f.dg.gpa; |
| 5365 | 4631 | const outputs = unwrapped_asm.outputs; |
| 5366 | 4632 | const inputs = unwrapped_asm.inputs; |
| 5367 | 4633 | |
| 5368 | 4634 | const result = result: { |
| 5369 | | const w = &f.object.code.writer; |
| 4635 | const w = &f.code.writer; |
| 5370 | 4636 | const inst_ty = f.typeOfIndex(inst); |
| 5371 | 4637 | const inst_local = if (inst_ty.hasRuntimeBits(zcu)) local: { |
| 5372 | 4638 | const inst_local = try f.allocLocalValue(.{ |
| 5373 | | .ctype = try f.ctypeFromType(inst_ty, .complete), |
| 5374 | | .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)), |
| 4639 | .type = inst_ty, |
| 4640 | .alignment = .none, |
| 5375 | 4641 | }); |
| 5376 | 4642 | if (f.wantSafety()) { |
| 5377 | | try f.writeCValue(w, inst_local, .Other); |
| 4643 | try f.writeCValue(w, inst_local, .other); |
| 5378 | 4644 | try w.writeAll(" = "); |
| 5379 | | try f.writeCValue(w, .{ .undef = inst_ty }, .Other); |
| 4645 | try f.writeCValue(w, .{ .undef = inst_ty }, .other); |
| 5380 | 4646 | try w.writeByte(';'); |
| 5381 | | try f.object.newline(); |
| 4647 | try f.newline(); |
| 5382 | 4648 | } |
| 5383 | 4649 | break :local inst_local; |
| 5384 | 4650 | } else .none; |
| ... | ... | @@ -5399,20 +4665,20 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5399 | 4665 | const output_ty = if (output.operand == .none) inst_ty else f.typeOf(output.operand).childType(zcu); |
| 5400 | 4666 | try w.writeAll("register "); |
| 5401 | 4667 | const output_local = try f.allocLocalValue(.{ |
| 5402 | | .ctype = try f.ctypeFromType(output_ty, .complete), |
| 5403 | | .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)), |
| 4668 | .type = output_ty, |
| 4669 | .alignment = .none, |
| 5404 | 4670 | }); |
| 5405 | 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 | 4673 | try w.writeAll(" __asm(\""); |
| 5408 | 4674 | try w.writeAll(constraint["={".len .. constraint.len - "}".len]); |
| 5409 | 4675 | try w.writeAll("\")"); |
| 5410 | 4676 | if (f.wantSafety()) { |
| 5411 | 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 | 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 | 4698 | const input_ty = f.typeOf(input.operand); |
| 5433 | 4699 | if (is_reg) try w.writeAll("register "); |
| 5434 | 4700 | const input_local = try f.allocLocalValue(.{ |
| 5435 | | .ctype = try f.ctypeFromType(input_ty, .complete), |
| 5436 | | .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)), |
| 4701 | .type = input_ty, |
| 4702 | .alignment = .none, |
| 5437 | 4703 | }); |
| 5438 | 4704 | try f.allocs.put(gpa, input_local.new_local, false); |
| 5439 | 4705 | // Do not render the declaration as `const` qualified if we're generating an |
| 5440 | 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 | 4708 | if (is_reg) { |
| 5443 | 4709 | try w.writeAll(" __asm(\""); |
| 5444 | 4710 | try w.writeAll(constraint["{".len .. constraint.len - "}".len]); |
| 5445 | 4711 | try w.writeAll("\")"); |
| 5446 | 4712 | } |
| 5447 | 4713 | try w.writeAll(" = "); |
| 5448 | | try f.writeCValue(w, input_val, .Other); |
| 4714 | try f.writeCValue(w, input_val, .other); |
| 5449 | 4715 | try w.writeByte(';'); |
| 5450 | | try f.object.newline(); |
| 4716 | try f.newline(); |
| 5451 | 4717 | } |
| 5452 | 4718 | } |
| 5453 | 4719 | |
| 5454 | 4720 | { |
| 5455 | 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 | 4724 | const allocator = stack.get(); |
| 5459 | 4725 | const fixed_asm_source = try allocator.alloc(u8, asm_source.len); |
| 5460 | 4726 | defer allocator.free(fixed_asm_source); |
| ... | ... | @@ -5520,10 +4786,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5520 | 4786 | const is_reg = constraint[1] == '{'; |
| 5521 | 4787 | try w.print("{f}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)}); |
| 5522 | 4788 | if (is_reg) { |
| 5523 | | try f.writeCValue(w, .{ .local = locals_index }, .Other); |
| 4789 | try f.writeCValue(w, .{ .local = locals_index }, .other); |
| 5524 | 4790 | locals_index += 1; |
| 5525 | 4791 | } else if (output.operand == .none) { |
| 5526 | | try f.writeCValue(w, inst_local, .FunctionArgument); |
| 4792 | try f.writeCValue(w, inst_local, .other); |
| 5527 | 4793 | } else { |
| 5528 | 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 | 4813 | const input_local_idx = locals_index; |
| 5548 | 4814 | locals_index += 1; |
| 5549 | 4815 | break :local .{ .local = input_local_idx }; |
| 5550 | | } else input_val, .Other); |
| 4816 | } else input_val, .other); |
| 5551 | 4817 | try w.writeByte(')'); |
| 5552 | 4818 | } |
| 5553 | 4819 | try w.writeByte(':'); |
| ... | ... | @@ -5567,7 +4833,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5567 | 4833 | const field_name = clobbers_ty.structFieldName(field_index, zcu).toSlice(ip).?; |
| 5568 | 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 | 4837 | var c_name_buf: [16]u8 = undefined; |
| 5572 | 4838 | const name = |
| 5573 | 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 | 4860 | } |
| 5595 | 4861 | w.undo(1); // erase the last comma |
| 5596 | 4862 | try w.writeAll(");"); |
| 5597 | | try f.object.newline(); |
| 4863 | try f.newline(); |
| 5598 | 4864 | |
| 5599 | 4865 | locals_index = locals_begin; |
| 5600 | 4866 | it = unwrapped_asm.iterateOutputs(); |
| ... | ... | @@ -5608,10 +4874,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5608 | 4874 | else |
| 5609 | 4875 | try f.resolveInst(output.operand)); |
| 5610 | 4876 | try w.writeAll(" = "); |
| 5611 | | try f.writeCValue(w, .{ .local = locals_index }, .Other); |
| 4877 | try f.writeCValue(w, .{ .local = locals_index }, .other); |
| 5612 | 4878 | locals_index += 1; |
| 5613 | 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 | 4899 | fn airIsNull( |
| 5634 | 4900 | f: *Function, |
| 5635 | 4901 | inst: Air.Inst.Index, |
| 5636 | | operator: std.math.CompareOperator, |
| 4902 | operator: enum { eq, neq }, |
| 5637 | 4903 | is_ptr: bool, |
| 5638 | 4904 | ) !CValue { |
| 5639 | | const pt = f.object.dg.pt; |
| 4905 | const pt = f.dg.pt; |
| 5640 | 4906 | const zcu = pt.zcu; |
| 5641 | | const ctype_pool = &f.object.dg.ctype_pool; |
| 5642 | 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 | 4910 | const operand = try f.resolveInst(un_op); |
| 5646 | 4911 | try reap(f, inst, &.{un_op}); |
| 5647 | 4912 | |
| 5648 | 4913 | const local = try f.allocLocal(inst, .bool); |
| 5649 | | const a = try Assignment.start(f, w, .bool); |
| 5650 | | try f.writeCValue(w, local, .Other); |
| 5651 | | try a.assign(f, w); |
| 4914 | try f.writeCValue(w, local, .other); |
| 4915 | try w.writeAll(" = "); |
| 5652 | 4916 | |
| 5653 | 4917 | const operand_ty = f.typeOf(un_op); |
| 5654 | 4918 | const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5655 | | const opt_ctype = try f.ctypeFromType(optional_ty, .complete); |
| 5656 | | const rhs = switch (opt_ctype.info(ctype_pool)) { |
| 5657 | | .basic, .pointer => rhs: { |
| 5658 | | if (is_ptr) |
| 5659 | | try f.writeCValueDeref(w, operand) |
| 5660 | | else |
| 5661 | | try f.writeCValue(w, operand, .Other); |
| 5662 | | break :rhs if (opt_ctype.isBool()) |
| 5663 | | "true" |
| 5664 | | else if (opt_ctype.isInteger()) |
| 5665 | | "0" |
| 5666 | | else |
| 5667 | | "NULL"; |
| 4919 | |
| 4920 | const pre: []const u8, const maybe_field: ?[]const u8, const post: []const u8 = switch (operator) { |
| 4921 | // zig fmt: off |
| 4922 | .eq => switch (CType.classifyOptional(optional_ty, zcu)) { |
| 4923 | .npv_payload => unreachable, // opv optional |
| 4924 | .error_set => .{ "", null, " == 0" }, |
| 4925 | .ptr_like => .{ "", null, " == NULL" }, |
| 4926 | .slice_like => .{ "", "ptr", " == NULL" }, |
| 4927 | .opv_payload => .{ "", "is_null", "" }, |
| 4928 | .@"struct" => .{ "", "is_null", "" }, |
| 5668 | 4929 | }, |
| 5669 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 5670 | | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 5671 | | .is_null, .payload => rhs: { |
| 5672 | | if (is_ptr) |
| 5673 | | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }) |
| 5674 | | else |
| 5675 | | try f.writeCValueMember(w, operand, .{ .identifier = "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, |
| 4930 | .neq => switch (CType.classifyOptional(optional_ty, zcu)) { |
| 4931 | .npv_payload => unreachable, // opv optional |
| 4932 | .error_set => .{ "", null, " != 0" }, |
| 4933 | .ptr_like => .{ "", null, " != NULL" }, |
| 4934 | .slice_like => .{ "", "ptr", " != NULL" }, |
| 4935 | .opv_payload => .{ "!", "is_null", "" }, |
| 4936 | .@"struct" => .{ "!", "is_null", "" }, |
| 5686 | 4937 | }, |
| 4938 | // zig fmt: on |
| 5687 | 4939 | }; |
| 5688 | | try w.writeAll(compareOperatorC(operator)); |
| 5689 | | try w.writeAll(rhs); |
| 5690 | | try a.end(f, w); |
| 4940 | |
| 4941 | try w.writeAll(pre); |
| 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 | 4959 | return local; |
| 5692 | 4960 | } |
| 5693 | 4961 | |
| 5694 | 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 | 4964 | const zcu = pt.zcu; |
| 5697 | | const ctype_pool = &f.object.dg.ctype_pool; |
| 5698 | 4965 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5699 | 4966 | |
| 5700 | 4967 | const inst_ty = f.typeOfIndex(inst); |
| 5701 | 4968 | const operand_ty = f.typeOf(ty_op.operand); |
| 5702 | 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 | 4971 | const operand = try f.resolveInst(ty_op.operand); |
| 5707 | | switch (opt_ctype.info(ctype_pool)) { |
| 5708 | | .basic, .pointer => return f.moveCValue(inst, inst_ty, operand), |
| 5709 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 5710 | | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 5711 | | .is_null, .payload => { |
| 5712 | | const w = &f.object.code.writer; |
| 5713 | | const local = try f.allocLocal(inst, inst_ty); |
| 5714 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 5715 | | try f.writeCValue(w, local, .Other); |
| 5716 | | try a.assign(f, w); |
| 5717 | | if (is_ptr) { |
| 5718 | | try w.writeByte('&'); |
| 5719 | | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 5720 | | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); |
| 5721 | | try a.end(f, w); |
| 5722 | | return local; |
| 5723 | | }, |
| 5724 | | .ptr, .len => return f.moveCValue(inst, inst_ty, operand), |
| 5725 | | else => unreachable, |
| 4972 | |
| 4973 | switch (CType.classifyOptional(opt_ty, zcu)) { |
| 4974 | .npv_payload => unreachable, // opv optional |
| 4975 | |
| 4976 | .opv_payload => return if (is_ptr) .{ .undef = inst_ty } else .none, |
| 4977 | |
| 4978 | .error_set, |
| 4979 | .ptr_like, |
| 4980 | .slice_like, |
| 4981 | => return f.moveCValue(inst, inst_ty, operand), |
| 4982 | |
| 4983 | .@"struct" => { |
| 4984 | const w = &f.code.writer; |
| 4985 | const local = try f.allocLocal(inst, inst_ty); |
| 4986 | try f.writeCValue(w, local, .other); |
| 4987 | try w.writeAll(" = "); |
| 4988 | if (is_ptr) { |
| 4989 | try w.writeByte('&'); |
| 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 | 4999 | fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5731 | | const pt = f.object.dg.pt; |
| 5000 | const pt = f.dg.pt; |
| 5732 | 5001 | const zcu = pt.zcu; |
| 5733 | 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 | 5004 | const operand = try f.resolveInst(ty_op.operand); |
| 5736 | 5005 | try reap(f, inst, &.{ty_op.operand}); |
| 5737 | 5006 | const operand_ty = f.typeOf(ty_op.operand); |
| 5007 | const opt_ty = operand_ty.childType(zcu); |
| 5738 | 5008 | |
| 5739 | 5009 | const inst_ty = f.typeOfIndex(inst); |
| 5740 | | const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete); |
| 5741 | | switch (opt_ctype.info(&f.object.dg.ctype_pool)) { |
| 5742 | | .basic => { |
| 5743 | | const a = try Assignment.start(f, w, opt_ctype); |
| 5744 | | try f.writeCValueDeref(w, operand); |
| 5745 | | try a.assign(f, w); |
| 5746 | | try f.object.dg.renderValue(w, Value.false, .Other); |
| 5747 | | try a.end(f, w); |
| 5748 | | return .none; |
| 5749 | | }, |
| 5750 | | .pointer => { |
| 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; |
| 5010 | |
| 5011 | switch (CType.classifyOptional(opt_ty, zcu)) { |
| 5012 | .npv_payload => unreachable, // opv optional |
| 5013 | |
| 5014 | .opv_payload => { |
| 5015 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); |
| 5016 | try w.writeAll(" = "); |
| 5017 | try f.dg.renderValue(w, .false, .other); |
| 5018 | try w.writeByte(';'); |
| 5019 | try f.newline(); |
| 5020 | return .{ .undef = inst_ty }; |
| 5759 | 5021 | }, |
| 5760 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 5761 | | .aggregate => { |
| 5762 | | { |
| 5763 | | const a = try Assignment.start(f, w, opt_ctype); |
| 5764 | | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); |
| 5765 | | try a.assign(f, w); |
| 5766 | | try f.object.dg.renderValue(w, Value.false, .Other); |
| 5767 | | try a.end(f, w); |
| 5768 | | } |
| 5022 | |
| 5023 | .error_set, |
| 5024 | .ptr_like, |
| 5025 | .slice_like, |
| 5026 | => return f.moveCValue(inst, inst_ty, operand), |
| 5027 | |
| 5028 | .@"struct" => { |
| 5029 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" }); |
| 5030 | try w.writeAll(" = "); |
| 5031 | try f.dg.renderValue(w, .false, .other); |
| 5032 | try w.writeByte(';'); |
| 5033 | try f.newline(); |
| 5769 | 5034 | if (f.liveness.isUnused(inst)) return .none; |
| 5770 | 5035 | const local = try f.allocLocal(inst, inst_ty); |
| 5771 | | const a = try Assignment.start(f, w, opt_ctype); |
| 5772 | | try f.writeCValue(w, local, .Other); |
| 5773 | | try a.assign(f, w); |
| 5774 | | try w.writeByte('&'); |
| 5036 | try f.writeCValue(w, local, .other); |
| 5037 | try w.writeAll(" = &"); |
| 5775 | 5038 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 5776 | | try a.end(f, w); |
| 5039 | try w.writeByte(';'); |
| 5040 | try f.newline(); |
| 5777 | 5041 | return local; |
| 5778 | 5042 | }, |
| 5779 | 5043 | } |
| ... | ... | @@ -5817,18 +5081,20 @@ fn fieldLocation( |
| 5817 | 5081 | .union_type => { |
| 5818 | 5082 | const loaded_union = ip.loadUnionType(container_ty.toIntern()); |
| 5819 | 5083 | switch (loaded_union.layout) { |
| 5820 | | .auto, .@"extern" => { |
| 5084 | .auto => { |
| 5821 | 5085 | const field_ty: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| 5822 | | if (!field_ty.hasRuntimeBits(zcu)) |
| 5823 | | return if (loaded_union.has_runtime_tag and !container_ty.unionHasAllZeroBitFieldTypes(zcu)) |
| 5824 | | .{ .field = .{ .identifier = "payload" } } |
| 5825 | | else |
| 5826 | | .begin; |
| 5086 | if (!field_ty.hasRuntimeBits(zcu)) { |
| 5087 | if (container_ty.unionHasAllZeroBitFieldTypes(zcu)) return .begin; |
| 5088 | return .{ .field = .{ .identifier = "payload" } }; |
| 5089 | } |
| 5827 | 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) |
| 5829 | | .{ .payload_identifier = field_name.toSlice(ip) } |
| 5830 | | else |
| 5831 | | .{ .identifier = field_name.toSlice(ip) } }; |
| 5091 | return .{ .field = .{ .payload_identifier = field_name.toSlice(ip) } }; |
| 5092 | }, |
| 5093 | .@"extern" => { |
| 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 | 5099 | .@"packed" => return .begin, |
| 5834 | 5100 | } |
| ... | ... | @@ -5865,7 +5131,7 @@ fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue |
| 5865 | 5131 | } |
| 5866 | 5132 | |
| 5867 | 5133 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5868 | | const pt = f.object.dg.pt; |
| 5134 | const pt = f.dg.pt; |
| 5869 | 5135 | const zcu = pt.zcu; |
| 5870 | 5136 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5871 | 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 | 5143 | const field_ptr_val = try f.resolveInst(extra.field_ptr); |
| 5878 | 5144 | try reap(f, inst, &.{extra.field_ptr}); |
| 5879 | 5145 | |
| 5880 | | const w = &f.object.code.writer; |
| 5146 | const w = &f.code.writer; |
| 5881 | 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 | 5149 | try w.writeAll(" = ("); |
| 5884 | 5150 | try f.renderType(w, container_ptr_ty); |
| 5885 | 5151 | try w.writeByte(')'); |
| 5886 | 5152 | |
| 5887 | 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 | 5155 | .field => |field| { |
| 5890 | 5156 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8); |
| 5891 | 5157 | |
| 5892 | 5158 | try w.writeAll("(("); |
| 5893 | 5159 | try f.renderType(w, u8_ptr_ty); |
| 5894 | 5160 | try w.writeByte(')'); |
| 5895 | | try f.writeCValue(w, field_ptr_val, .Other); |
| 5161 | try f.writeCValue(w, field_ptr_val, .other); |
| 5896 | 5162 | try w.writeAll(" - offsetof("); |
| 5897 | 5163 | try f.renderType(w, container_ty); |
| 5898 | 5164 | try w.writeAll(", "); |
| 5899 | | try f.writeCValue(w, field, .Other); |
| 5165 | try f.writeCValue(w, field, .other); |
| 5900 | 5166 | try w.writeAll("))"); |
| 5901 | 5167 | }, |
| 5902 | 5168 | .byte_offset => |byte_offset| { |
| ... | ... | @@ -5905,7 +5171,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5905 | 5171 | try w.writeAll("(("); |
| 5906 | 5172 | try f.renderType(w, u8_ptr_ty); |
| 5907 | 5173 | try w.writeByte(')'); |
| 5908 | | try f.writeCValue(w, field_ptr_val, .Other); |
| 5174 | try f.writeCValue(w, field_ptr_val, .other); |
| 5909 | 5175 | try w.print(" - {f})", .{ |
| 5910 | 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 | 5179 | } |
| 5914 | 5180 | |
| 5915 | 5181 | try w.writeByte(';'); |
| 5916 | | try f.object.newline(); |
| 5182 | try f.newline(); |
| 5917 | 5183 | return local; |
| 5918 | 5184 | } |
| 5919 | 5185 | |
| ... | ... | @@ -5924,23 +5190,19 @@ fn fieldPtr( |
| 5924 | 5190 | container_ptr_val: CValue, |
| 5925 | 5191 | field_index: u32, |
| 5926 | 5192 | ) !CValue { |
| 5927 | | const pt = f.object.dg.pt; |
| 5193 | const pt = f.dg.pt; |
| 5928 | 5194 | const zcu = pt.zcu; |
| 5929 | | const container_ty = container_ptr_ty.childType(zcu); |
| 5930 | 5195 | const field_ptr_ty = f.typeOfIndex(inst); |
| 5931 | 5196 | |
| 5932 | | // Ensure complete type definition is visible before accessing fields. |
| 5933 | | _ = try f.ctypeFromType(container_ty, .complete); |
| 5934 | | |
| 5935 | | const w = &f.object.code.writer; |
| 5197 | const w = &f.code.writer; |
| 5936 | 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 | 5200 | try w.writeAll(" = ("); |
| 5939 | 5201 | try f.renderType(w, field_ptr_ty); |
| 5940 | 5202 | try w.writeByte(')'); |
| 5941 | 5203 | |
| 5942 | 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 | 5206 | .field => |field| { |
| 5945 | 5207 | try w.writeByte('&'); |
| 5946 | 5208 | try f.writeCValueDerefMember(w, container_ptr_val, field); |
| ... | ... | @@ -5951,7 +5213,7 @@ fn fieldPtr( |
| 5951 | 5213 | try w.writeAll("(("); |
| 5952 | 5214 | try f.renderType(w, u8_ptr_ty); |
| 5953 | 5215 | try w.writeByte(')'); |
| 5954 | | try f.writeCValue(w, container_ptr_val, .Other); |
| 5216 | try f.writeCValue(w, container_ptr_val, .other); |
| 5955 | 5217 | try w.print(" + {f})", .{ |
| 5956 | 5218 | try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)), |
| 5957 | 5219 | }); |
| ... | ... | @@ -5959,12 +5221,12 @@ fn fieldPtr( |
| 5959 | 5221 | } |
| 5960 | 5222 | |
| 5961 | 5223 | try w.writeByte(';'); |
| 5962 | | try f.object.newline(); |
| 5224 | try f.newline(); |
| 5963 | 5225 | return local; |
| 5964 | 5226 | } |
| 5965 | 5227 | |
| 5966 | 5228 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5967 | | const pt = f.object.dg.pt; |
| 5229 | const pt = f.dg.pt; |
| 5968 | 5230 | const zcu = pt.zcu; |
| 5969 | 5231 | const ip = &zcu.intern_pool; |
| 5970 | 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 | 5238 | const struct_byval = try f.resolveInst(extra.struct_operand); |
| 5977 | 5239 | try reap(f, inst, &.{extra.struct_operand}); |
| 5978 | 5240 | const struct_ty = f.typeOf(extra.struct_operand); |
| 5979 | | const w = &f.object.code.writer; |
| 5980 | | |
| 5981 | | // Ensure complete type definition is visible before accessing fields. |
| 5982 | | _ = try f.ctypeFromType(struct_ty, .complete); |
| 5241 | const w = &f.code.writer; |
| 5983 | 5242 | |
| 5984 | 5243 | assert(struct_ty.containerLayout(zcu) != .@"packed"); // `Air.Legalize.Feature.expand_packed_struct_field_val` handles this case |
| 5985 | 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 | 5247 | const union_type = ip.loadUnionType(struct_ty.toIntern()); |
| 5989 | 5248 | const enum_tag_ty: Type = .fromInterned(union_type.enum_tag_type); |
| 5990 | 5249 | const field_name_str = enum_tag_ty.enumFieldName(extra.field_index, zcu).toSlice(ip); |
| 5991 | | if (union_type.has_runtime_tag) { |
| 5992 | | break :name .{ .payload_identifier = field_name_str }; |
| 5993 | | } else { |
| 5994 | | break :name .{ .identifier = field_name_str }; |
| 5995 | | } |
| 5250 | break :name .{ .payload_identifier = field_name_str }; |
| 5996 | 5251 | }, |
| 5997 | 5252 | .tuple_type => .{ .field = extra.field_index }, |
| 5998 | 5253 | else => unreachable, |
| 5999 | 5254 | }; |
| 6000 | 5255 | |
| 6001 | 5256 | const local = try f.allocLocal(inst, inst_ty); |
| 6002 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 6003 | | try f.writeCValue(w, local, .Other); |
| 6004 | | try a.assign(f, w); |
| 5257 | try f.writeCValue(w, local, .other); |
| 5258 | try w.writeAll(" = "); |
| 6005 | 5259 | try f.writeCValueMember(w, struct_byval, field_name); |
| 6006 | | try a.end(f, w); |
| 5260 | try w.writeByte(';'); |
| 5261 | try f.newline(); |
| 6007 | 5262 | return local; |
| 6008 | 5263 | } |
| 6009 | 5264 | |
| 6010 | 5265 | /// *(E!T) -> E |
| 6011 | 5266 | /// Note that the result is never a pointer. |
| 6012 | 5267 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6013 | | const pt = f.object.dg.pt; |
| 5268 | const pt = f.dg.pt; |
| 6014 | 5269 | const zcu = pt.zcu; |
| 6015 | 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 | 5275 | try reap(f, inst, &.{ty_op.operand}); |
| 6021 | 5276 | |
| 6022 | 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 | 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) { |
| 6029 | | // The store will be 'x = x'; elide it. |
| 6030 | | return local; |
| 6031 | | } |
| 6032 | | |
| 6033 | | const w = &f.object.code.writer; |
| 6034 | | try f.writeCValue(w, local, .Other); |
| 5280 | const w = &f.code.writer; |
| 5281 | try f.writeCValue(w, local, .other); |
| 6035 | 5282 | try w.writeAll(" = "); |
| 6036 | 5283 | |
| 6037 | | if (!payload_ty.hasRuntimeBits(zcu)) |
| 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) |
| 5284 | if (operand_is_ptr) |
| 6044 | 5285 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) |
| 6045 | 5286 | else |
| 6046 | 5287 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); |
| 6047 | 5288 | try w.writeByte(';'); |
| 6048 | | try f.object.newline(); |
| 5289 | try f.newline(); |
| 6049 | 5290 | return local; |
| 6050 | 5291 | } |
| 6051 | 5292 | |
| 6052 | 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 | 5295 | const zcu = pt.zcu; |
| 6055 | 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 | 5301 | const operand_ty = f.typeOf(ty_op.operand); |
| 6061 | 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 | 5305 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 6065 | | if (!is_ptr) return .none; |
| 6066 | | |
| 5306 | assert(is_ptr); // opv bug in sema |
| 6067 | 5307 | const local = try f.allocLocal(inst, inst_ty); |
| 6068 | | try f.writeCValue(w, local, .Other); |
| 5308 | try f.writeCValue(w, local, .other); |
| 6069 | 5309 | try w.writeAll(" = ("); |
| 6070 | 5310 | try f.renderType(w, inst_ty); |
| 6071 | 5311 | try w.writeByte(')'); |
| 6072 | | try f.writeCValue(w, operand, .Other); |
| 5312 | try f.writeCValue(w, operand, .other); |
| 6073 | 5313 | try w.writeByte(';'); |
| 6074 | | try f.object.newline(); |
| 5314 | try f.newline(); |
| 6075 | 5315 | return local; |
| 6076 | 5316 | } |
| 6077 | 5317 | |
| 6078 | 5318 | const local = try f.allocLocal(inst, inst_ty); |
| 6079 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 6080 | | try f.writeCValue(w, local, .Other); |
| 6081 | | try a.assign(f, w); |
| 5319 | try f.writeCValue(w, local, .other); |
| 5320 | try w.writeAll(" = "); |
| 6082 | 5321 | if (is_ptr) { |
| 6083 | 5322 | try w.writeByte('&'); |
| 6084 | 5323 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 6085 | 5324 | } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" }); |
| 6086 | | try a.end(f, w); |
| 5325 | try w.writeByte(';'); |
| 5326 | try f.newline(); |
| 6087 | 5327 | return local; |
| 6088 | 5328 | } |
| 6089 | 5329 | |
| 6090 | 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 | 5332 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6093 | 5333 | |
| 6094 | 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 | 5336 | const operand = try f.resolveInst(ty_op.operand); |
| 6099 | | switch (inst_ctype.info(ctype_pool)) { |
| 6100 | | .basic, .pointer => return f.moveCValue(inst, inst_ty, operand), |
| 6101 | | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 6102 | | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 6103 | | .is_null, .payload => { |
| 6104 | | const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete); |
| 6105 | | const w = &f.object.code.writer; |
| 6106 | | const local = try f.allocLocal(inst, inst_ty); |
| 6107 | | { |
| 6108 | | const a = try Assignment.start(f, w, .bool); |
| 6109 | | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 6110 | | try a.assign(f, w); |
| 6111 | | try w.writeAll("false"); |
| 6112 | | try a.end(f, w); |
| 6113 | | } |
| 6114 | | { |
| 6115 | | const a = try Assignment.start(f, w, operand_ctype); |
| 6116 | | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6117 | | try a.assign(f, w); |
| 6118 | | try f.writeCValue(w, operand, .Other); |
| 6119 | | try a.end(f, w); |
| 6120 | | } |
| 6121 | | return local; |
| 6122 | | }, |
| 6123 | | .ptr, .len => return f.moveCValue(inst, inst_ty, operand), |
| 6124 | | else => unreachable, |
| 5337 | |
| 5338 | switch (CType.classifyOptional(inst_ty, zcu)) { |
| 5339 | .npv_payload => unreachable, // opv optional |
| 5340 | |
| 5341 | .opv_payload => unreachable, // opv bug in Sema |
| 5342 | |
| 5343 | .error_set, |
| 5344 | .ptr_like, |
| 5345 | .slice_like, |
| 5346 | => return f.moveCValue(inst, inst_ty, operand), |
| 5347 | |
| 5348 | .@"struct" => { |
| 5349 | const w = &f.code.writer; |
| 5350 | const local = try f.allocLocal(inst, inst_ty); |
| 5351 | |
| 5352 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 5353 | try w.writeAll(" = false;"); |
| 5354 | try f.newline(); |
| 5355 | |
| 5356 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 5357 | try w.writeAll(" = "); |
| 5358 | try f.writeCValue(w, operand, .other); |
| 5359 | try w.writeByte(';'); |
| 5360 | try f.newline(); |
| 5361 | |
| 5362 | return local; |
| 6125 | 5363 | }, |
| 6126 | 5364 | } |
| 6127 | 5365 | } |
| 6128 | 5366 | |
| 6129 | 5367 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6130 | | const pt = f.object.dg.pt; |
| 5368 | const pt = f.dg.pt; |
| 6131 | 5369 | const zcu = pt.zcu; |
| 6132 | 5370 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6133 | 5371 | |
| 6134 | 5372 | const inst_ty = f.typeOfIndex(inst); |
| 6135 | 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 | 5374 | const err = try f.resolveInst(ty_op.operand); |
| 6139 | 5375 | try reap(f, inst, &.{ty_op.operand}); |
| 6140 | 5376 | |
| 6141 | | const w = &f.object.code.writer; |
| 5377 | const w = &f.code.writer; |
| 6142 | 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) { |
| 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)); |
| 5380 | if (payload_ty.hasRuntimeBits(zcu)) { |
| 6151 | 5381 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6152 | | try a.assign(f, w); |
| 6153 | | try f.object.dg.renderUndefValue(w, payload_ty, .Other); |
| 6154 | | try a.end(f, w); |
| 6155 | | } |
| 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); |
| 5382 | try w.writeAll(" = "); |
| 5383 | try f.dg.renderUndefValue(w, payload_ty, .other); |
| 5384 | try w.writeByte(';'); |
| 5385 | try f.newline(); |
| 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 | 5394 | return local; |
| 6167 | 5395 | } |
| 6168 | 5396 | |
| 6169 | 5397 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6170 | | const pt = f.object.dg.pt; |
| 6171 | | const zcu = pt.zcu; |
| 6172 | | const w = &f.object.code.writer; |
| 5398 | const pt = f.dg.pt; |
| 5399 | const w = &f.code.writer; |
| 6173 | 5400 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6174 | 5401 | const inst_ty = f.typeOfIndex(inst); |
| 6175 | 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 | 5404 | const err_int_ty = try pt.errorIntType(); |
| 6181 | 5405 | const no_err = try pt.intValue(err_int_ty, 0); |
| 6182 | 5406 | try reap(f, inst, &.{ty_op.operand}); |
| 6183 | 5407 | |
| 6184 | 5408 | // First, set the non-error value. |
| 6185 | | if (!payload_ty.hasRuntimeBits(zcu)) { |
| 6186 | | const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete)); |
| 6187 | | try f.writeCValueDeref(w, operand); |
| 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 | | } |
| 5409 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }); |
| 5410 | try w.print(" = {f};", .{try f.fmtIntLiteralDec(no_err)}); |
| 5411 | try f.newline(); |
| 6200 | 5412 | |
| 6201 | 5413 | // Then return the payload pointer (only if it is used) |
| 6202 | 5414 | if (f.liveness.isUnused(inst)) return .none; |
| 6203 | 5415 | |
| 6204 | 5416 | const local = try f.allocLocal(inst, inst_ty); |
| 6205 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 6206 | | try f.writeCValue(w, local, .Other); |
| 6207 | | try a.assign(f, w); |
| 6208 | | try w.writeByte('&'); |
| 5417 | try f.writeCValue(w, local, .other); |
| 5418 | try w.writeAll(" = &"); |
| 6209 | 5419 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" }); |
| 6210 | | try a.end(f, w); |
| 5420 | try w.writeByte(';'); |
| 5421 | try f.newline(); |
| 6211 | 5422 | return local; |
| 6212 | 5423 | } |
| 6213 | 5424 | |
| ... | ... | @@ -6227,7 +5438,7 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6227 | 5438 | } |
| 6228 | 5439 | |
| 6229 | 5440 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6230 | | const pt = f.object.dg.pt; |
| 5441 | const pt = f.dg.pt; |
| 6231 | 5442 | const zcu = pt.zcu; |
| 6232 | 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 | 5446 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 6236 | 5447 | const payload = try f.resolveInst(ty_op.operand); |
| 6237 | 5448 | assert(payload_ty.hasRuntimeBits(zcu)); |
| 6238 | | const err_ty = inst_ty.errorUnionSet(zcu); |
| 6239 | 5449 | try reap(f, inst, &.{ty_op.operand}); |
| 6240 | 5450 | |
| 6241 | | const w = &f.object.code.writer; |
| 5451 | const w = &f.code.writer; |
| 6242 | 5452 | const local = try f.allocLocal(inst, inst_ty); |
| 6243 | | { |
| 6244 | | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); |
| 6245 | | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6246 | | try a.assign(f, w); |
| 6247 | | try f.writeCValue(w, payload, .Other); |
| 6248 | | try a.end(f, w); |
| 6249 | | } |
| 6250 | | { |
| 6251 | | const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete)); |
| 6252 | | try f.writeCValueMember(w, local, .{ .identifier = "error" }); |
| 6253 | | try a.assign(f, w); |
| 6254 | | try f.object.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .Other); |
| 6255 | | try a.end(f, w); |
| 6256 | | } |
| 5453 | |
| 5454 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 5455 | try w.writeAll(" = "); |
| 5456 | try f.writeCValue(w, payload, .other); |
| 5457 | try w.writeByte(';'); |
| 5458 | try f.newline(); |
| 5459 | |
| 5460 | try f.writeCValueMember(w, local, .{ .identifier = "error" }); |
| 5461 | try w.writeAll(" = "); |
| 5462 | try f.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .other); |
| 5463 | try w.writeByte(';'); |
| 5464 | try f.newline(); |
| 5465 | |
| 6257 | 5466 | return local; |
| 6258 | 5467 | } |
| 6259 | 5468 | |
| 6260 | 5469 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 6261 | | const pt = f.object.dg.pt; |
| 6262 | | const zcu = pt.zcu; |
| 5470 | const pt = f.dg.pt; |
| 6263 | 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 | 5474 | const operand = try f.resolveInst(un_op); |
| 6267 | 5475 | try reap(f, inst, &.{un_op}); |
| 6268 | | const operand_ty = f.typeOf(un_op); |
| 6269 | 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); |
| 6275 | | try f.writeCValue(w, local, .Other); |
| 6276 | | try a.assign(f, w); |
| 5478 | try f.writeCValue(w, local, .other); |
| 5479 | try w.writeAll(" = "); |
| 6277 | 5480 | const err_int_ty = try pt.errorIntType(); |
| 6278 | | if (!error_ty.errorSetIsEmpty(zcu)) |
| 6279 | | if (payload_ty.hasRuntimeBits(zcu)) |
| 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) |
| 5481 | if (is_ptr) |
| 5482 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) |
| 6286 | 5483 | else |
| 6287 | | try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other); |
| 6288 | | try w.writeByte(' '); |
| 6289 | | try w.writeAll(operator); |
| 6290 | | try w.writeByte(' '); |
| 6291 | | try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other); |
| 6292 | | try a.end(f, w); |
| 5484 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); |
| 5485 | try w.print(" {s} ", .{operator}); |
| 5486 | try f.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .other); |
| 5487 | try w.writeByte(';'); |
| 5488 | try f.newline(); |
| 6293 | 5489 | return local; |
| 6294 | 5490 | } |
| 6295 | 5491 | |
| 6296 | 5492 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6297 | | const pt = f.object.dg.pt; |
| 5493 | const pt = f.dg.pt; |
| 6298 | 5494 | const zcu = pt.zcu; |
| 6299 | | const ctype_pool = &f.object.dg.ctype_pool; |
| 6300 | 5495 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6301 | 5496 | |
| 6302 | 5497 | const operand = try f.resolveInst(ty_op.operand); |
| 6303 | 5498 | try reap(f, inst, &.{ty_op.operand}); |
| 6304 | 5499 | const inst_ty = f.typeOfIndex(inst); |
| 6305 | | const ptr_ty = inst_ty.slicePtrFieldType(zcu); |
| 6306 | | const w = &f.object.code.writer; |
| 5500 | const w = &f.code.writer; |
| 6307 | 5501 | const local = try f.allocLocal(inst, inst_ty); |
| 6308 | 5502 | const operand_ty = f.typeOf(ty_op.operand); |
| 6309 | 5503 | const array_ty = operand_ty.childType(zcu); |
| 6310 | 5504 | |
| 6311 | | { |
| 6312 | | const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete)); |
| 6313 | | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 6314 | | try a.assign(f, w); |
| 6315 | | if (operand == .undef) { |
| 6316 | | try f.writeCValue(w, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other); |
| 6317 | | } else { |
| 6318 | | const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete); |
| 6319 | | const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype; |
| 6320 | | const elem_ty = array_ty.childType(zcu); |
| 6321 | | const elem_ctype = try f.ctypeFromType(elem_ty, .complete); |
| 6322 | | if (!ptr_child_ctype.eql(elem_ctype)) { |
| 6323 | | try w.writeByte('('); |
| 6324 | | try f.renderCType(w, ptr_ctype); |
| 6325 | | try w.writeByte(')'); |
| 6326 | | } |
| 6327 | | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); |
| 6328 | | const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype; |
| 6329 | | if (operand_child_ctype.info(ctype_pool) == .array) { |
| 6330 | | try w.writeByte('&'); |
| 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 | | } |
| 5505 | // We have a `*[n]T`, which was turned into to a pointer to `struct { T array[n]; }`. |
| 5506 | // Ideally we would want to use 'operand->array' to convert to a `T *` (we get a `T []` |
| 5507 | // which decays to a pointer), but if the element type is zero-bit or the array length is |
| 5508 | // zero, there will not be an `array` member (the array type lowers to `void`). We cannot |
| 5509 | // check the type layout here because it may not be resolved, so in this instance, we must |
| 5510 | // use a pointer cast. |
| 5511 | try f.writeCValueMember(w, local, .{ .identifier = "ptr" }); |
| 5512 | try w.writeAll(" = ("); |
| 5513 | try f.dg.renderType(w, inst_ty.slicePtrFieldType(zcu)); |
| 5514 | try w.writeByte(')'); |
| 5515 | try f.writeCValue(w, operand, .other); |
| 5516 | try w.writeByte(';'); |
| 5517 | try f.newline(); |
| 5518 | |
| 5519 | try f.writeCValueMember(w, local, .{ .identifier = "len" }); |
| 5520 | try w.print(" = {f}", .{ |
| 5521 | try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))), |
| 5522 | }); |
| 5523 | try w.writeByte(';'); |
| 5524 | try f.newline(); |
| 6346 | 5525 | |
| 6347 | 5526 | return local; |
| 6348 | 5527 | } |
| 6349 | 5528 | |
| 6350 | 5529 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6351 | | const pt = f.object.dg.pt; |
| 5530 | const pt = f.dg.pt; |
| 6352 | 5531 | const zcu = pt.zcu; |
| 6353 | 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 | 5537 | try reap(f, inst, &.{ty_op.operand}); |
| 6359 | 5538 | const operand_ty = f.typeOf(ty_op.operand); |
| 6360 | 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 | 5541 | const operation = if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isRuntimeFloat()) |
| 6363 | 5542 | if (inst_scalar_ty.floatBits(target) < scalar_ty.floatBits(target)) "trunc" else "extend" |
| 6364 | 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 | 5547 | else |
| 6369 | 5548 | unreachable; |
| 6370 | 5549 | |
| 6371 | | const w = &f.object.code.writer; |
| 5550 | const w = &f.code.writer; |
| 6372 | 5551 | const local = try f.allocLocal(inst, inst_ty); |
| 6373 | 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)); |
| 6375 | | try f.writeCValue(w, local, .Other); |
| 5553 | try f.writeCValue(w, local, .other); |
| 6376 | 5554 | try v.elem(f, w); |
| 6377 | | try a.assign(f, w); |
| 5555 | try w.writeAll(" = "); |
| 6378 | 5556 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { |
| 6379 | 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 | 5559 | try w.writeByte('('); |
| 6382 | 5560 | } |
| 6383 | 5561 | try w.writeAll("zig_"); |
| ... | ... | @@ -6385,14 +5563,15 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6385 | 5563 | try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target)); |
| 6386 | 5564 | try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target)); |
| 6387 | 5565 | try w.writeByte('('); |
| 6388 | | try f.writeCValue(w, operand, .FunctionArgument); |
| 5566 | try f.writeCValue(w, operand, .other); |
| 6389 | 5567 | try v.elem(f, w); |
| 6390 | 5568 | try w.writeByte(')'); |
| 6391 | 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 | 5571 | try w.writeByte(')'); |
| 6394 | 5572 | } |
| 6395 | | try a.end(f, w); |
| 5573 | try w.writeByte(';'); |
| 5574 | try f.newline(); |
| 6396 | 5575 | try v.end(f, inst, w); |
| 6397 | 5576 | |
| 6398 | 5577 | return local; |
| ... | ... | @@ -6405,7 +5584,7 @@ fn airUnBuiltinCall( |
| 6405 | 5584 | operation: []const u8, |
| 6406 | 5585 | info: BuiltinInfo, |
| 6407 | 5586 | ) !CValue { |
| 6408 | | const pt = f.object.dg.pt; |
| 5587 | const pt = f.dg.pt; |
| 6409 | 5588 | const zcu = pt.zcu; |
| 6410 | 5589 | |
| 6411 | 5590 | const operand = try f.resolveInst(operand_ref); |
| ... | ... | @@ -6415,30 +5594,32 @@ fn airUnBuiltinCall( |
| 6415 | 5594 | const operand_ty = f.typeOf(operand_ref); |
| 6416 | 5595 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6417 | 5596 | |
| 6418 | | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6419 | | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 5597 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 5598 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6420 | 5599 | |
| 6421 | | const w = &f.object.code.writer; |
| 5600 | const w = &f.code.writer; |
| 6422 | 5601 | const local = try f.allocLocal(inst, inst_ty); |
| 6423 | 5602 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6424 | 5603 | if (!ref_ret) { |
| 6425 | | try f.writeCValue(w, local, .Other); |
| 5604 | try f.writeCValue(w, local, .other); |
| 6426 | 5605 | try v.elem(f, w); |
| 6427 | 5606 | try w.writeAll(" = "); |
| 6428 | 5607 | } |
| 6429 | 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 | 5610 | try w.writeByte('('); |
| 6432 | 5611 | if (ref_ret) { |
| 6433 | | try f.writeCValue(w, local, .FunctionArgument); |
| 5612 | try w.writeByte('&'); |
| 5613 | try f.writeCValue(w, local, .other); |
| 6434 | 5614 | try v.elem(f, w); |
| 6435 | 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 | 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 | 5621 | try w.writeAll(");"); |
| 6441 | | try f.object.newline(); |
| 5622 | try f.newline(); |
| 6442 | 5623 | try v.end(f, inst, w); |
| 6443 | 5624 | |
| 6444 | 5625 | return local; |
| ... | ... | @@ -6450,13 +5631,12 @@ fn airBinBuiltinCall( |
| 6450 | 5631 | operation: []const u8, |
| 6451 | 5632 | info: BuiltinInfo, |
| 6452 | 5633 | ) !CValue { |
| 6453 | | const pt = f.object.dg.pt; |
| 5634 | const pt = f.dg.pt; |
| 6454 | 5635 | const zcu = pt.zcu; |
| 6455 | 5636 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6456 | 5637 | |
| 6457 | 5638 | const operand_ty = f.typeOf(bin_op.lhs); |
| 6458 | | const operand_ctype = try f.ctypeFromType(operand_ty, .complete); |
| 6459 | | const is_big = operand_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 5639 | const is_big = lowersToBigInt(operand_ty, zcu); |
| 6460 | 5640 | |
| 6461 | 5641 | const lhs = try f.resolveInst(bin_op.lhs); |
| 6462 | 5642 | const rhs = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6466,32 +5646,35 @@ fn airBinBuiltinCall( |
| 6466 | 5646 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6467 | 5647 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6468 | 5648 | |
| 6469 | | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6470 | | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 5649 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 5650 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6471 | 5651 | |
| 6472 | | const w = &f.object.code.writer; |
| 5652 | const w = &f.code.writer; |
| 6473 | 5653 | const local = try f.allocLocal(inst, inst_ty); |
| 6474 | 5654 | if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6475 | 5655 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6476 | 5656 | if (!ref_ret) { |
| 6477 | | try f.writeCValue(w, local, .Other); |
| 5657 | try f.writeCValue(w, local, .other); |
| 6478 | 5658 | try v.elem(f, w); |
| 6479 | 5659 | try w.writeAll(" = "); |
| 6480 | 5660 | } |
| 6481 | 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 | 5663 | try w.writeByte('('); |
| 6484 | 5664 | if (ref_ret) { |
| 6485 | | try f.writeCValue(w, local, .FunctionArgument); |
| 5665 | try w.writeByte('&'); |
| 5666 | try f.writeCValue(w, local, .other); |
| 6486 | 5667 | try v.elem(f, w); |
| 6487 | 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 | 5672 | try v.elem(f, w); |
| 6491 | 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 | 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 | 5678 | try w.writeAll(");\n"); |
| 6496 | 5679 | try v.end(f, inst, w); |
| 6497 | 5680 | |
| ... | ... | @@ -6506,7 +5689,7 @@ fn airCmpBuiltinCall( |
| 6506 | 5689 | operation: enum { cmp, operator }, |
| 6507 | 5690 | info: BuiltinInfo, |
| 6508 | 5691 | ) !CValue { |
| 6509 | | const pt = f.object.dg.pt; |
| 5692 | const pt = f.dg.pt; |
| 6510 | 5693 | const zcu = pt.zcu; |
| 6511 | 5694 | const lhs = try f.resolveInst(data.lhs); |
| 6512 | 5695 | const rhs = try f.resolveInst(data.rhs); |
| ... | ... | @@ -6517,14 +5700,14 @@ fn airCmpBuiltinCall( |
| 6517 | 5700 | const operand_ty = f.typeOf(data.lhs); |
| 6518 | 5701 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6519 | 5702 | |
| 6520 | | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6521 | | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 5703 | const ref_ret = lowersToBigInt(inst_scalar_ty, zcu); |
| 5704 | const ref_arg = lowersToBigInt(scalar_ty, zcu); |
| 6522 | 5705 | |
| 6523 | | const w = &f.object.code.writer; |
| 5706 | const w = &f.code.writer; |
| 6524 | 5707 | const local = try f.allocLocal(inst, inst_ty); |
| 6525 | 5708 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6526 | 5709 | if (!ref_ret) { |
| 6527 | | try f.writeCValue(w, local, .Other); |
| 5710 | try f.writeCValue(w, local, .other); |
| 6528 | 5711 | try v.elem(f, w); |
| 6529 | 5712 | try w.writeAll(" = "); |
| 6530 | 5713 | } |
| ... | ... | @@ -6532,33 +5715,36 @@ fn airCmpBuiltinCall( |
| 6532 | 5715 | else => @tagName(operation), |
| 6533 | 5716 | .operator => compareOperatorAbbrev(operator), |
| 6534 | 5717 | }}); |
| 6535 | | try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 5718 | try f.dg.renderTypeForBuiltinFnName(w, scalar_ty); |
| 6536 | 5719 | try w.writeByte('('); |
| 6537 | 5720 | if (ref_ret) { |
| 6538 | | try f.writeCValue(w, local, .FunctionArgument); |
| 5721 | try w.writeByte('&'); |
| 5722 | try f.writeCValue(w, local, .other); |
| 6539 | 5723 | try v.elem(f, w); |
| 6540 | 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 | 5728 | try v.elem(f, w); |
| 6544 | 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 | 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 | 5734 | try w.writeByte(')'); |
| 6549 | 5735 | if (!ref_ret) try w.print("{s}{f}", .{ |
| 6550 | 5736 | compareOperatorC(operator), |
| 6551 | 5737 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), |
| 6552 | 5738 | }); |
| 6553 | 5739 | try w.writeByte(';'); |
| 6554 | | try f.object.newline(); |
| 5740 | try f.newline(); |
| 6555 | 5741 | try v.end(f, inst, w); |
| 6556 | 5742 | |
| 6557 | 5743 | return local; |
| 6558 | 5744 | } |
| 6559 | 5745 | |
| 6560 | 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 | 5748 | const zcu = pt.zcu; |
| 6563 | 5749 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6564 | 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 | 5754 | const new_value = try f.resolveInst(extra.new_value); |
| 6569 | 5755 | const ptr_ty = f.typeOf(extra.ptr); |
| 6570 | 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 | 5759 | const new_value_mat = try Materialize.start(f, inst, ty, new_value); |
| 6575 | 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 | 5766 | |
| 6582 | 5767 | const local = try f.allocLocal(inst, inst_ty); |
| 6583 | 5768 | if (inst_ty.isPtrLikeOptional(zcu)) { |
| 6584 | | { |
| 6585 | | const a = try Assignment.start(f, w, ctype); |
| 6586 | | try f.writeCValue(w, local, .Other); |
| 6587 | | try a.assign(f, w); |
| 6588 | | try f.writeCValue(w, expected_value, .Other); |
| 6589 | | try a.end(f, w); |
| 6590 | | } |
| 5769 | try f.writeCValue(w, local, .other); |
| 5770 | try w.writeAll(" = "); |
| 5771 | try f.writeCValue(w, expected_value, .other); |
| 5772 | try w.writeByte(';'); |
| 5773 | try f.newline(); |
| 6591 | 5774 | |
| 6592 | 5775 | try w.writeAll("if ("); |
| 6593 | 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 | 5778 | try w.writeByte(')'); |
| 6596 | 5779 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6597 | 5780 | try w.writeAll(" *)"); |
| 6598 | | try f.writeCValue(w, ptr, .Other); |
| 5781 | try f.writeCValue(w, ptr, .other); |
| 6599 | 5782 | try w.writeAll(", "); |
| 6600 | | try f.writeCValue(w, local, .FunctionArgument); |
| 5783 | try f.writeCValue(w, local, .other); |
| 6601 | 5784 | try w.writeAll(", "); |
| 6602 | 5785 | try new_value_mat.mat(f, w); |
| 6603 | 5786 | try w.writeAll(", "); |
| ... | ... | @@ -6605,56 +5788,49 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6605 | 5788 | try w.writeAll(", "); |
| 6606 | 5789 | try writeMemoryOrder(w, extra.failureOrder()); |
| 6607 | 5790 | try w.writeAll(", "); |
| 6608 | | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 5791 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6609 | 5792 | try w.writeAll(", "); |
| 6610 | 5793 | try f.renderType(w, repr_ty); |
| 6611 | 5794 | try w.writeByte(')'); |
| 6612 | 5795 | try w.writeAll(") {"); |
| 6613 | | f.object.indent(); |
| 6614 | | try f.object.newline(); |
| 6615 | | { |
| 6616 | | const a = try Assignment.start(f, w, ctype); |
| 6617 | | try f.writeCValue(w, local, .Other); |
| 6618 | | try a.assign(f, w); |
| 6619 | | try w.writeAll("NULL"); |
| 6620 | | try a.end(f, w); |
| 6621 | | } |
| 6622 | | try f.object.outdent(); |
| 5796 | f.indent(); |
| 5797 | try f.newline(); |
| 5798 | |
| 5799 | try f.writeCValue(w, local, .other); |
| 5800 | try w.writeAll(" = NULL;"); |
| 5801 | try f.newline(); |
| 5802 | |
| 5803 | try f.outdent(); |
| 6623 | 5804 | try w.writeByte('}'); |
| 6624 | | try f.object.newline(); |
| 5805 | try f.newline(); |
| 6625 | 5806 | } else { |
| 6626 | | { |
| 6627 | | const a = try Assignment.start(f, w, ctype); |
| 6628 | | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6629 | | try a.assign(f, w); |
| 6630 | | try f.writeCValue(w, expected_value, .Other); |
| 6631 | | try a.end(f, w); |
| 6632 | | } |
| 6633 | | { |
| 6634 | | const a = try Assignment.start(f, w, .bool); |
| 6635 | | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 6636 | | try a.assign(f, w); |
| 6637 | | try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 6638 | | try f.renderType(w, ty); |
| 6639 | | try w.writeByte(')'); |
| 6640 | | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6641 | | try w.writeAll(" *)"); |
| 6642 | | try f.writeCValue(w, ptr, .Other); |
| 6643 | | try w.writeAll(", "); |
| 6644 | | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 6645 | | try w.writeAll(", "); |
| 6646 | | try new_value_mat.mat(f, w); |
| 6647 | | try w.writeAll(", "); |
| 6648 | | try writeMemoryOrder(w, extra.successOrder()); |
| 6649 | | try w.writeAll(", "); |
| 6650 | | try writeMemoryOrder(w, extra.failureOrder()); |
| 6651 | | try w.writeAll(", "); |
| 6652 | | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 6653 | | try w.writeAll(", "); |
| 6654 | | try f.renderType(w, repr_ty); |
| 6655 | | try w.writeByte(')'); |
| 6656 | | try a.end(f, w); |
| 6657 | | } |
| 5807 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 5808 | try w.writeAll(" = "); |
| 5809 | try f.writeCValue(w, expected_value, .other); |
| 5810 | try w.writeByte(';'); |
| 5811 | try f.newline(); |
| 5812 | |
| 5813 | try f.writeCValueMember(w, local, .{ .identifier = "is_null" }); |
| 5814 | try w.print(" = zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 5815 | try f.renderType(w, ty); |
| 5816 | try w.writeByte(')'); |
| 5817 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 5818 | try w.writeAll(" *)"); |
| 5819 | try f.writeCValue(w, ptr, .other); |
| 5820 | try w.writeAll(", "); |
| 5821 | try f.writeCValueMember(w, local, .{ .identifier = "payload" }); |
| 5822 | try w.writeAll(", "); |
| 5823 | try new_value_mat.mat(f, w); |
| 5824 | try w.writeAll(", "); |
| 5825 | try writeMemoryOrder(w, extra.successOrder()); |
| 5826 | try w.writeAll(", "); |
| 5827 | try writeMemoryOrder(w, extra.failureOrder()); |
| 5828 | try w.writeAll(", "); |
| 5829 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 5830 | try w.writeAll(", "); |
| 5831 | try f.renderType(w, repr_ty); |
| 5832 | try w.writeAll(");"); |
| 5833 | try f.newline(); |
| 6658 | 5834 | } |
| 6659 | 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 | 5843 | } |
| 6668 | 5844 | |
| 6669 | 5845 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6670 | | const pt = f.object.dg.pt; |
| 5846 | const pt = f.dg.pt; |
| 6671 | 5847 | const zcu = pt.zcu; |
| 6672 | 5848 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6673 | 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 | 5853 | const ptr = try f.resolveInst(pl_op.operand); |
| 6678 | 5854 | const operand = try f.resolveInst(extra.operand); |
| 6679 | 5855 | |
| 6680 | | const w = &f.object.code.writer; |
| 5856 | const w = &f.code.writer; |
| 6681 | 5857 | const operand_mat = try Materialize.start(f, inst, ty, operand); |
| 6682 | 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 | 5866 | try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); |
| 6691 | 5867 | if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128"); |
| 6692 | 5868 | try w.writeByte('('); |
| 6693 | | try f.writeCValue(w, local, .Other); |
| 5869 | try f.writeCValue(w, local, .other); |
| 6694 | 5870 | try w.writeAll(", ("); |
| 6695 | 5871 | const use_atomic = switch (extra.op()) { |
| 6696 | 5872 | else => true, |
| ... | ... | @@ -6702,17 +5878,17 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6702 | 5878 | if (use_atomic) try w.writeByte(')'); |
| 6703 | 5879 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6704 | 5880 | try w.writeAll(" *)"); |
| 6705 | | try f.writeCValue(w, ptr, .Other); |
| 5881 | try f.writeCValue(w, ptr, .other); |
| 6706 | 5882 | try w.writeAll(", "); |
| 6707 | 5883 | try operand_mat.mat(f, w); |
| 6708 | 5884 | try w.writeAll(", "); |
| 6709 | 5885 | try writeMemoryOrder(w, extra.ordering()); |
| 6710 | 5886 | try w.writeAll(", "); |
| 6711 | | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 5887 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6712 | 5888 | try w.writeAll(", "); |
| 6713 | 5889 | try f.renderType(w, repr_ty); |
| 6714 | 5890 | try w.writeAll(");"); |
| 6715 | | try f.object.newline(); |
| 5891 | try f.newline(); |
| 6716 | 5892 | try operand_mat.end(f, inst); |
| 6717 | 5893 | |
| 6718 | 5894 | if (f.liveness.isUnused(inst)) { |
| ... | ... | @@ -6724,7 +5900,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6724 | 5900 | } |
| 6725 | 5901 | |
| 6726 | 5902 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6727 | | const pt = f.object.dg.pt; |
| 5903 | const pt = f.dg.pt; |
| 6728 | 5904 | const zcu = pt.zcu; |
| 6729 | 5905 | const atomic_load = f.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 6730 | 5906 | const ptr = try f.resolveInst(atomic_load.ptr); |
| ... | ... | @@ -6738,31 +5914,31 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6738 | 5914 | ty; |
| 6739 | 5915 | |
| 6740 | 5916 | const inst_ty = f.typeOfIndex(inst); |
| 6741 | | const w = &f.object.code.writer; |
| 5917 | const w = &f.code.writer; |
| 6742 | 5918 | const local = try f.allocLocal(inst, inst_ty); |
| 6743 | 5919 | |
| 6744 | 5920 | try w.writeAll("zig_atomic_load("); |
| 6745 | | try f.writeCValue(w, local, .Other); |
| 5921 | try f.writeCValue(w, local, .other); |
| 6746 | 5922 | try w.writeAll(", (zig_atomic("); |
| 6747 | 5923 | try f.renderType(w, ty); |
| 6748 | 5924 | try w.writeByte(')'); |
| 6749 | 5925 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6750 | 5926 | try w.writeAll(" *)"); |
| 6751 | | try f.writeCValue(w, ptr, .Other); |
| 5927 | try f.writeCValue(w, ptr, .other); |
| 6752 | 5928 | try w.writeAll(", "); |
| 6753 | 5929 | try writeMemoryOrder(w, atomic_load.order); |
| 6754 | 5930 | try w.writeAll(", "); |
| 6755 | | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 5931 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6756 | 5932 | try w.writeAll(", "); |
| 6757 | 5933 | try f.renderType(w, repr_ty); |
| 6758 | 5934 | try w.writeAll(");"); |
| 6759 | | try f.object.newline(); |
| 5935 | try f.newline(); |
| 6760 | 5936 | |
| 6761 | 5937 | return local; |
| 6762 | 5938 | } |
| 6763 | 5939 | |
| 6764 | 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 | 5942 | const zcu = pt.zcu; |
| 6767 | 5943 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6768 | 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 | 5946 | const ptr = try f.resolveInst(bin_op.lhs); |
| 6771 | 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 | 5950 | const element_mat = try Materialize.start(f, inst, ty, element); |
| 6775 | 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 | 5960 | try w.writeByte(')'); |
| 6785 | 5961 | if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile"); |
| 6786 | 5962 | try w.writeAll(" *)"); |
| 6787 | | try f.writeCValue(w, ptr, .Other); |
| 5963 | try f.writeCValue(w, ptr, .other); |
| 6788 | 5964 | try w.writeAll(", "); |
| 6789 | 5965 | try element_mat.mat(f, w); |
| 6790 | 5966 | try w.print(", {s}, ", .{order}); |
| 6791 | | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 5967 | try f.dg.renderTypeForBuiltinFnName(w, ty); |
| 6792 | 5968 | try w.writeAll(", "); |
| 6793 | 5969 | try f.renderType(w, repr_ty); |
| 6794 | 5970 | try w.writeAll(");"); |
| 6795 | | try f.object.newline(); |
| 5971 | try f.newline(); |
| 6796 | 5972 | try element_mat.end(f, inst); |
| 6797 | 5973 | |
| 6798 | 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 | 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 | 5979 | const zcu = pt.zcu; |
| 6814 | 5980 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6815 | 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 | 5984 | const elem_ty = f.typeOf(bin_op.rhs); |
| 6819 | 5985 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 6820 | 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 | 5989 | if (val_is_undef) { |
| 6824 | 5990 | if (!safety) { |
| ... | ... | @@ -6832,153 +5998,128 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6832 | 5998 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); |
| 6833 | 5999 | try w.writeAll(", 0xaa, "); |
| 6834 | 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 | 6002 | .one => { |
| 6842 | | const array_ty = dest_ty.childType(zcu); |
| 6843 | | const len = array_ty.arrayLen(zcu) * elem_abi_size; |
| 6844 | | |
| 6845 | | try f.writeCValue(w, dest_slice, .FunctionArgument); |
| 6846 | | try w.print(", 0xaa, {d});", .{len}); |
| 6847 | | try f.object.newline(); |
| 6003 | try f.writeCValue(w, dest_slice, .other); |
| 6004 | try w.print(", 0xaa, {d}", .{dest_ty.childType(zcu).arrayLen(zcu)}); |
| 6848 | 6005 | }, |
| 6849 | 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 | 6011 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6852 | 6012 | return .none; |
| 6853 | 6013 | } |
| 6854 | 6014 | |
| 6855 | | if (elem_abi_size > 1 or dest_ty.isVolatilePtr(zcu)) { |
| 6856 | | // For the assignment in this loop, the array pointer needs to get |
| 6857 | | // casted to a regular pointer, otherwise an error like this occurs: |
| 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(" != "); |
| 6015 | if (elem_abi_size == 1 and !dest_ty.isVolatilePtr(zcu)) { |
| 6016 | const bitcasted = try bitcast(f, .u8, value, elem_ty); |
| 6017 | try w.writeAll("memset("); |
| 6875 | 6018 | switch (dest_ty.ptrSize(zcu)) { |
| 6876 | 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 | 6024 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 6878 | 6025 | }, |
| 6879 | 6026 | .one => { |
| 6880 | | const array_ty = dest_ty.childType(zcu); |
| 6881 | | try w.print("{d}", .{array_ty.arrayLen(zcu)}); |
| 6027 | try f.writeCValue(w, dest_slice, .other); |
| 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 | 6032 | .many, .c => unreachable, |
| 6884 | 6033 | } |
| 6885 | | try w.writeAll("; ++"); |
| 6886 | | try f.writeCValue(w, index, .Other); |
| 6887 | | try w.writeAll(") "); |
| 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 | | |
| 6034 | try w.writeAll(");"); |
| 6035 | try f.newline(); |
| 6036 | try f.freeCValue(inst, bitcasted); |
| 6901 | 6037 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6902 | | try freeLocal(f, inst, index.new_local, null); |
| 6903 | | |
| 6904 | 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 | 6052 | switch (dest_ty.ptrSize(zcu)) { |
| 6911 | | .slice => { |
| 6912 | | try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }); |
| 6913 | | try w.writeAll(", "); |
| 6914 | | try f.writeCValue(w, bitcasted, .FunctionArgument); |
| 6915 | | try w.writeAll(", "); |
| 6916 | | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 6917 | | 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; |
| 6053 | .slice => try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }), |
| 6054 | .one => try w.print("{d}", .{dest_ty.childType(zcu).arrayLen(zcu)}), |
| 6055 | .many, .c => unreachable, |
| 6056 | } |
| 6057 | try w.writeAll("; ++"); |
| 6058 | try f.writeCValue(w, index, .other); |
| 6059 | try w.writeAll(") "); |
| 6923 | 6060 | |
| 6924 | | try f.writeCValue(w, dest_slice, .FunctionArgument); |
| 6925 | | try w.writeAll(", "); |
| 6926 | | try f.writeCValue(w, bitcasted, .FunctionArgument); |
| 6927 | | try w.print(", {d});", .{len}); |
| 6928 | | try f.object.newline(); |
| 6929 | | }, |
| 6061 | switch (dest_ty.ptrSize(zcu)) { |
| 6062 | .slice => try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" }), |
| 6063 | .one => try f.writeCValueDerefMember(w, dest_slice, .{ .identifier = "array" }), |
| 6930 | 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 | 6073 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6074 | try freeLocal(f, inst, index.new_local, null); |
| 6075 | |
| 6934 | 6076 | return .none; |
| 6935 | 6077 | } |
| 6936 | 6078 | |
| 6937 | 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 | 6081 | const zcu = pt.zcu; |
| 6940 | 6082 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6941 | 6083 | const dest_ptr = try f.resolveInst(bin_op.lhs); |
| 6942 | 6084 | const src_ptr = try f.resolveInst(bin_op.rhs); |
| 6943 | 6085 | const dest_ty = f.typeOf(bin_op.lhs); |
| 6944 | 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 | 6089 | if (dest_ty.ptrSize(zcu) != .one) { |
| 6948 | 6090 | try w.writeAll("if ("); |
| 6949 | | try writeArrayLen(f, dest_ptr, dest_ty); |
| 6091 | try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }); |
| 6950 | 6092 | try w.writeAll(" != 0) "); |
| 6951 | 6093 | } |
| 6952 | 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 | 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 | 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 | 6112 | try w.writeAll(" * sizeof("); |
| 6959 | 6113 | try f.renderType(w, dest_ty.indexableElem(zcu)); |
| 6960 | 6114 | try w.writeAll("));"); |
| 6961 | | try f.object.newline(); |
| 6115 | try f.newline(); |
| 6962 | 6116 | |
| 6963 | 6117 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6964 | 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 | 6121 | fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6981 | | const pt = f.object.dg.pt; |
| 6122 | const pt = f.dg.pt; |
| 6982 | 6123 | const zcu = pt.zcu; |
| 6983 | 6124 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6984 | 6125 | const union_ptr = try f.resolveInst(bin_op.lhs); |
| ... | ... | @@ -6988,19 +6129,18 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6988 | 6129 | const union_ty = f.typeOf(bin_op.lhs).childType(zcu); |
| 6989 | 6130 | const layout = union_ty.unionGetLayout(zcu); |
| 6990 | 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; |
| 6994 | | const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete)); |
| 6133 | const w = &f.code.writer; |
| 6995 | 6134 | try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" }); |
| 6996 | | try a.assign(f, w); |
| 6997 | | try f.writeCValue(w, new_tag, .Other); |
| 6998 | | try a.end(f, w); |
| 6135 | try w.writeAll(" = "); |
| 6136 | try f.writeCValue(w, new_tag, .other); |
| 6137 | try w.writeByte(';'); |
| 6138 | try f.newline(); |
| 6999 | 6139 | return .none; |
| 7000 | 6140 | } |
| 7001 | 6141 | |
| 7002 | 6142 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7003 | | const pt = f.object.dg.pt; |
| 6143 | const pt = f.dg.pt; |
| 7004 | 6144 | const zcu = pt.zcu; |
| 7005 | 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 | 6152 | if (layout.tag_size == 0) return .none; |
| 7013 | 6153 | |
| 7014 | 6154 | const inst_ty = f.typeOfIndex(inst); |
| 7015 | | const w = &f.object.code.writer; |
| 6155 | const w = &f.code.writer; |
| 7016 | 6156 | const local = try f.allocLocal(inst, inst_ty); |
| 7017 | | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 7018 | | try f.writeCValue(w, local, .Other); |
| 7019 | | try a.assign(f, w); |
| 6157 | try f.writeCValue(w, local, .other); |
| 6158 | try w.writeAll(" = "); |
| 7020 | 6159 | try f.writeCValueMember(w, operand, .{ .identifier = "tag" }); |
| 7021 | | try a.end(f, w); |
| 6160 | try w.writeByte(';'); |
| 6161 | try f.newline(); |
| 7022 | 6162 | return local; |
| 7023 | 6163 | } |
| 7024 | 6164 | |
| 7025 | 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 | 6169 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7027 | 6170 | |
| 7028 | 6171 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -7030,15 +6173,17 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7030 | 6173 | const operand = try f.resolveInst(un_op); |
| 7031 | 6174 | try reap(f, inst, &.{un_op}); |
| 7032 | 6175 | |
| 7033 | | const w = &f.object.code.writer; |
| 6176 | const w = &f.code.writer; |
| 7034 | 6177 | const local = try f.allocLocal(inst, inst_ty); |
| 7035 | | try f.writeCValue(w, local, .Other); |
| 7036 | | try w.print(" = {s}(", .{ |
| 7037 | | try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }), |
| 6178 | try f.writeCValue(w, local, .other); |
| 6179 | try f.need_tag_name_funcs.put(gpa, 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 | 6185 | try w.writeAll(");"); |
| 7041 | | try f.object.newline(); |
| 6186 | try f.newline(); |
| 7042 | 6187 | |
| 7043 | 6188 | return local; |
| 7044 | 6189 | } |
| ... | ... | @@ -7046,40 +6191,37 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7046 | 6191 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7047 | 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 | 6195 | const inst_ty = f.typeOfIndex(inst); |
| 7051 | 6196 | const operand = try f.resolveInst(un_op); |
| 7052 | 6197 | try reap(f, inst, &.{un_op}); |
| 7053 | 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 | 6201 | try w.writeAll(" = zig_errorName["); |
| 7057 | | try f.writeCValue(w, operand, .Other); |
| 6202 | try f.writeCValue(w, operand, .other); |
| 7058 | 6203 | try w.writeAll(" - 1];"); |
| 7059 | | try f.object.newline(); |
| 6204 | try f.newline(); |
| 7060 | 6205 | return local; |
| 7061 | 6206 | } |
| 7062 | 6207 | |
| 7063 | 6208 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7064 | | const pt = f.object.dg.pt; |
| 7065 | | const zcu = pt.zcu; |
| 7066 | 6209 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7067 | 6210 | |
| 7068 | 6211 | const operand = try f.resolveInst(ty_op.operand); |
| 7069 | 6212 | try reap(f, inst, &.{ty_op.operand}); |
| 7070 | 6213 | |
| 7071 | 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 | 6217 | const local = try f.allocLocal(inst, inst_ty); |
| 7076 | 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)); |
| 7078 | | try f.writeCValue(w, local, .Other); |
| 6219 | try f.writeCValue(w, local, .other); |
| 7079 | 6220 | try v.elem(f, w); |
| 7080 | | try a.assign(f, w); |
| 7081 | | try f.writeCValue(w, operand, .Other); |
| 7082 | | try a.end(f, w); |
| 6221 | try w.writeAll(" = "); |
| 6222 | try f.writeCValue(w, operand, .other); |
| 6223 | try w.writeByte(';'); |
| 6224 | try f.newline(); |
| 7083 | 6225 | try v.end(f, inst, w); |
| 7084 | 6226 | |
| 7085 | 6227 | return local; |
| ... | ... | @@ -7096,29 +6238,29 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7096 | 6238 | |
| 7097 | 6239 | const inst_ty = f.typeOfIndex(inst); |
| 7098 | 6240 | |
| 7099 | | const w = &f.object.code.writer; |
| 6241 | const w = &f.code.writer; |
| 7100 | 6242 | const local = try f.allocLocal(inst, inst_ty); |
| 7101 | 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 | 6245 | try v.elem(f, w); |
| 7104 | 6246 | try w.writeAll(" = "); |
| 7105 | | try f.writeCValue(w, pred, .Other); |
| 6247 | try f.writeCValue(w, pred, .other); |
| 7106 | 6248 | try v.elem(f, w); |
| 7107 | 6249 | try w.writeAll(" ? "); |
| 7108 | | try f.writeCValue(w, lhs, .Other); |
| 6250 | try f.writeCValue(w, lhs, .other); |
| 7109 | 6251 | try v.elem(f, w); |
| 7110 | 6252 | try w.writeAll(" : "); |
| 7111 | | try f.writeCValue(w, rhs, .Other); |
| 6253 | try f.writeCValue(w, rhs, .other); |
| 7112 | 6254 | try v.elem(f, w); |
| 7113 | 6255 | try w.writeByte(';'); |
| 7114 | | try f.object.newline(); |
| 6256 | try f.newline(); |
| 7115 | 6257 | try v.end(f, inst, w); |
| 7116 | 6258 | |
| 7117 | 6259 | return local; |
| 7118 | 6260 | } |
| 7119 | 6261 | |
| 7120 | 6262 | fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7121 | | const pt = f.object.dg.pt; |
| 6263 | const pt = f.dg.pt; |
| 7122 | 6264 | const zcu = pt.zcu; |
| 7123 | 6265 | |
| 7124 | 6266 | const unwrapped = f.air.unwrapShuffleOne(zcu, inst); |
| ... | ... | @@ -7126,22 +6268,22 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7126 | 6268 | const operand = try f.resolveInst(unwrapped.operand); |
| 7127 | 6269 | const inst_ty = unwrapped.result_ty; |
| 7128 | 6270 | |
| 7129 | | const w = &f.object.code.writer; |
| 6271 | const w = &f.code.writer; |
| 7130 | 6272 | const local = try f.allocLocal(inst, inst_ty); |
| 7131 | 6273 | try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand |
| 7132 | 6274 | for (mask, 0..) |mask_elem, out_idx| { |
| 7133 | | try f.writeCValue(w, local, .Other); |
| 6275 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7134 | 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 | 6278 | try w.writeAll("] = "); |
| 7137 | 6279 | switch (mask_elem.unwrap()) { |
| 7138 | 6280 | .elem => |src_idx| { |
| 7139 | | try f.writeCValue(w, operand, .Other); |
| 6281 | try f.writeCValueMember(w, operand, .{ .identifier = "array" }); |
| 7140 | 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 | 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 | 6288 | try w.writeAll(";\n"); |
| 7147 | 6289 | } |
| ... | ... | @@ -7150,7 +6292,7 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7150 | 6292 | } |
| 7151 | 6293 | |
| 7152 | 6294 | fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7153 | | const pt = f.object.dg.pt; |
| 6295 | const pt = f.dg.pt; |
| 7154 | 6296 | const zcu = pt.zcu; |
| 7155 | 6297 | |
| 7156 | 6298 | const unwrapped = f.air.unwrapShuffleTwo(zcu, inst); |
| ... | ... | @@ -7160,38 +6302,38 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7160 | 6302 | const inst_ty = unwrapped.result_ty; |
| 7161 | 6303 | const elem_ty = inst_ty.childType(zcu); |
| 7162 | 6304 | |
| 7163 | | const w = &f.object.code.writer; |
| 6305 | const w = &f.code.writer; |
| 7164 | 6306 | const local = try f.allocLocal(inst, inst_ty); |
| 7165 | 6307 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands |
| 7166 | 6308 | for (mask, 0..) |mask_elem, out_idx| { |
| 7167 | | try f.writeCValue(w, local, .Other); |
| 6309 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 7168 | 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 | 6312 | try w.writeAll("] = "); |
| 7171 | 6313 | switch (mask_elem.unwrap()) { |
| 7172 | 6314 | .a_elem => |src_idx| { |
| 7173 | | try f.writeCValue(w, operand_a, .Other); |
| 6315 | try f.writeCValueMember(w, operand_a, .{ .identifier = "array" }); |
| 7174 | 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 | 6318 | try w.writeByte(']'); |
| 7177 | 6319 | }, |
| 7178 | 6320 | .b_elem => |src_idx| { |
| 7179 | | try f.writeCValue(w, operand_b, .Other); |
| 6321 | try f.writeCValueMember(w, operand_b, .{ .identifier = "array" }); |
| 7180 | 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 | 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 | 6328 | try w.writeByte(';'); |
| 7187 | | try f.object.newline(); |
| 6329 | try f.newline(); |
| 7188 | 6330 | } |
| 7189 | 6331 | |
| 7190 | 6332 | return local; |
| 7191 | 6333 | } |
| 7192 | 6334 | |
| 7193 | 6335 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7194 | | const pt = f.object.dg.pt; |
| 6336 | const pt = f.dg.pt; |
| 7195 | 6337 | const zcu = pt.zcu; |
| 7196 | 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 | 6341 | const operand = try f.resolveInst(reduce.operand); |
| 7200 | 6342 | try reap(f, inst, &.{reduce.operand}); |
| 7201 | 6343 | const operand_ty = f.typeOf(reduce.operand); |
| 7202 | | const w = &f.object.code.writer; |
| 6344 | const w = &f.code.writer; |
| 7203 | 6345 | |
| 7204 | 6346 | const use_operator = scalar_ty.bitSize(zcu) <= 64; |
| 7205 | 6347 | const op: union(enum) { |
| ... | ... | @@ -7246,10 +6388,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7246 | 6388 | // } |
| 7247 | 6389 | |
| 7248 | 6390 | const accum = try f.allocLocal(inst, scalar_ty); |
| 7249 | | try f.writeCValue(w, accum, .Other); |
| 6391 | try f.writeCValue(w, accum, .other); |
| 7250 | 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 | 6395 | .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) { |
| 7254 | 6396 | .bool => Value.false, |
| 7255 | 6397 | .int => try pt.intValue(scalar_ty, 0), |
| ... | ... | @@ -7285,58 +6427,58 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7285 | 6427 | .float => try pt.floatValue(scalar_ty, std.math.nan(f128)), |
| 7286 | 6428 | else => unreachable, |
| 7287 | 6429 | }, |
| 7288 | | }, .Other); |
| 6430 | }, .other); |
| 7289 | 6431 | try w.writeByte(';'); |
| 7290 | | try f.object.newline(); |
| 6432 | try f.newline(); |
| 7291 | 6433 | |
| 7292 | 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 | 6436 | switch (op) { |
| 7295 | 6437 | .builtin => |func| { |
| 7296 | 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 | 6440 | try w.writeByte('('); |
| 7299 | | try f.writeCValue(w, accum, .FunctionArgument); |
| 6441 | try f.writeCValue(w, accum, .other); |
| 7300 | 6442 | try w.writeAll(", "); |
| 7301 | | try f.writeCValue(w, operand, .Other); |
| 6443 | try f.writeCValue(w, operand, .other); |
| 7302 | 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 | 6446 | try w.writeByte(')'); |
| 7305 | 6447 | }, |
| 7306 | 6448 | .infix => |ass| { |
| 7307 | 6449 | try w.writeAll(ass); |
| 7308 | | try f.writeCValue(w, operand, .Other); |
| 6450 | try f.writeCValue(w, operand, .other); |
| 7309 | 6451 | try v.elem(f, w); |
| 7310 | 6452 | }, |
| 7311 | 6453 | .ternary => |cmp| { |
| 7312 | 6454 | try w.writeAll(" = "); |
| 7313 | | try f.writeCValue(w, accum, .Other); |
| 6455 | try f.writeCValue(w, accum, .other); |
| 7314 | 6456 | try w.writeAll(cmp); |
| 7315 | | try f.writeCValue(w, operand, .Other); |
| 6457 | try f.writeCValue(w, operand, .other); |
| 7316 | 6458 | try v.elem(f, w); |
| 7317 | 6459 | try w.writeAll(" ? "); |
| 7318 | | try f.writeCValue(w, accum, .Other); |
| 6460 | try f.writeCValue(w, accum, .other); |
| 7319 | 6461 | try w.writeAll(" : "); |
| 7320 | | try f.writeCValue(w, operand, .Other); |
| 6462 | try f.writeCValue(w, operand, .other); |
| 7321 | 6463 | try v.elem(f, w); |
| 7322 | 6464 | }, |
| 7323 | 6465 | } |
| 7324 | 6466 | try w.writeByte(';'); |
| 7325 | | try f.object.newline(); |
| 6467 | try f.newline(); |
| 7326 | 6468 | try v.end(f, inst, w); |
| 7327 | 6469 | |
| 7328 | 6470 | return accum; |
| 7329 | 6471 | } |
| 7330 | 6472 | |
| 7331 | 6473 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7332 | | const pt = f.object.dg.pt; |
| 6474 | const pt = f.dg.pt; |
| 7333 | 6475 | const zcu = pt.zcu; |
| 7334 | 6476 | const ip = &zcu.intern_pool; |
| 7335 | 6477 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7336 | 6478 | const inst_ty = f.typeOfIndex(inst); |
| 7337 | 6479 | const len: usize = @intCast(inst_ty.arrayLen(zcu)); |
| 7338 | 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 | 6482 | const resolved_elements = try gpa.alloc(CValue, elements.len); |
| 7341 | 6483 | defer gpa.free(resolved_elements); |
| 7342 | 6484 | for (resolved_elements, elements) |*resolved_element, element| { |
| ... | ... | @@ -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 | 6495 | const local = try f.allocLocal(inst, inst_ty); |
| 7354 | 6496 | switch (ip.indexToKey(inst_ty.toIntern())) { |
| 7355 | 6497 | inline .array_type, .vector_type => |info, tag| { |
| 7356 | | const a: Assignment = .{ |
| 7357 | | .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete), |
| 7358 | | }; |
| 7359 | 6498 | for (resolved_elements, 0..) |element, i| { |
| 7360 | | try a.restart(f, w); |
| 7361 | | try f.writeCValue(w, local, .Other); |
| 7362 | | try w.print("[{d}]", .{i}); |
| 7363 | | try a.assign(f, w); |
| 7364 | | try f.writeCValue(w, element, .Other); |
| 7365 | | try a.end(f, w); |
| 6499 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 6500 | try w.print("[{d}] = ", .{i}); |
| 6501 | try f.writeCValue(w, element, .other); |
| 6502 | try w.writeByte(';'); |
| 6503 | try f.newline(); |
| 7366 | 6504 | } |
| 7367 | 6505 | if (tag == .array_type and info.sentinel != .none) { |
| 7368 | | try a.restart(f, w); |
| 7369 | | try f.writeCValue(w, local, .Other); |
| 7370 | | try w.print("[{d}]", .{info.len}); |
| 7371 | | try a.assign(f, w); |
| 7372 | | try f.object.dg.renderValue(w, Value.fromInterned(info.sentinel), .Other); |
| 7373 | | try a.end(f, w); |
| 6506 | try f.writeCValueMember(w, local, .{ .identifier = "array" }); |
| 6507 | try w.print("[{d}] = ", .{info.len}); |
| 6508 | try f.dg.renderValue(w, Value.fromInterned(info.sentinel), .other); |
| 6509 | try w.writeByte(';'); |
| 6510 | try f.newline(); |
| 7374 | 6511 | } |
| 7375 | 6512 | }, |
| 7376 | 6513 | .struct_type => { |
| ... | ... | @@ -7382,11 +6519,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7382 | 6519 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 7383 | 6520 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 7384 | 6521 | |
| 7385 | | const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete)); |
| 7386 | 6522 | try f.writeCValueMember(w, local, .{ .identifier = loaded_struct.field_names.get(ip)[field_index].toSlice(ip) }); |
| 7387 | | try a.assign(f, w); |
| 7388 | | try f.writeCValue(w, resolved_elements[field_index], .Other); |
| 7389 | | try a.end(f, w); |
| 6523 | try w.writeAll(" = "); |
| 6524 | try f.writeCValue(w, resolved_elements[field_index], .other); |
| 6525 | try w.writeByte(';'); |
| 6526 | try f.newline(); |
| 7390 | 6527 | } |
| 7391 | 6528 | }, |
| 7392 | 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 | 6534 | const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]); |
| 7398 | 6535 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 7399 | 6536 | |
| 7400 | | const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete)); |
| 7401 | 6537 | try f.writeCValueMember(w, local, .{ .field = field_index }); |
| 7402 | | try a.assign(f, w); |
| 7403 | | try f.writeCValue(w, resolved_elements[field_index], .Other); |
| 7404 | | try a.end(f, w); |
| 6538 | try w.writeAll(" = "); |
| 6539 | try f.writeCValue(w, resolved_elements[field_index], .other); |
| 6540 | try w.writeByte(';'); |
| 6541 | try f.newline(); |
| 7405 | 6542 | }, |
| 7406 | 6543 | else => unreachable, |
| 7407 | 6544 | } |
| ... | ... | @@ -7410,46 +6547,52 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7410 | 6547 | } |
| 7411 | 6548 | |
| 7412 | 6549 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7413 | | const pt = f.object.dg.pt; |
| 6550 | const pt = f.dg.pt; |
| 7414 | 6551 | const zcu = pt.zcu; |
| 7415 | 6552 | const ip = &zcu.intern_pool; |
| 7416 | 6553 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7417 | 6554 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 6555 | const field_index = extra.field_index; |
| 7418 | 6556 | |
| 7419 | 6557 | const union_ty = f.typeOfIndex(inst); |
| 7420 | 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]; |
| 7422 | | const payload_ty = f.typeOf(extra.init); |
| 6559 | const loaded_enum = ip.loadEnumType(loaded_union.enum_tag_type); |
| 6560 | |
| 7423 | 6561 | const payload = try f.resolveInst(extra.init); |
| 7424 | 6562 | try reap(f, inst, &.{extra.init}); |
| 7425 | 6563 | |
| 7426 | | const w = &f.object.code.writer; |
| 6564 | const w = &f.code.writer; |
| 7427 | 6565 | if (loaded_union.layout == .@"packed") return f.moveCValue(inst, union_ty, payload); |
| 7428 | 6566 | |
| 7429 | 6567 | const local = try f.allocLocal(inst, union_ty); |
| 7430 | 6568 | |
| 7431 | | const field: CValue = if (union_ty.unionTagTypeRuntime(zcu)) |tag_ty| field: { |
| 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)); |
| 6569 | if (loaded_union.has_runtime_tag) { |
| 7436 | 6570 | try f.writeCValueMember(w, local, .{ .identifier = "tag" }); |
| 7437 | | try a.assign(f, w); |
| 7438 | | try w.print("{f}", .{try f.fmtIntLiteralDec(tag_val.intFromEnum(zcu))}); |
| 7439 | | try a.end(f, w); |
| 7440 | | break :field .{ .payload_identifier = field_name.toSlice(ip) }; |
| 7441 | | } else .{ .identifier = field_name.toSlice(ip) }; |
| 7442 | | |
| 7443 | | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); |
| 7444 | | try f.writeCValueMember(w, local, field); |
| 7445 | | try a.assign(f, w); |
| 7446 | | try f.writeCValue(w, payload, .Other); |
| 7447 | | try a.end(f, w); |
| 6571 | if (loaded_enum.field_values.len == 0) { |
| 6572 | // auto-numbered |
| 6573 | try w.print(" = {d};", .{field_index}); |
| 6574 | } else { |
| 6575 | const tag_int_val: Value = .fromInterned(loaded_enum.field_values.get(ip)[field_index]); |
| 6576 | try w.print(" = {f};", .{try f.fmtIntLiteralDec(tag_int_val)}); |
| 6577 | } |
| 6578 | try f.newline(); |
| 6579 | } |
| 6580 | |
| 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 | 6591 | return local; |
| 7449 | 6592 | } |
| 7450 | 6593 | |
| 7451 | 6594 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7452 | | const pt = f.object.dg.pt; |
| 6595 | const pt = f.dg.pt; |
| 7453 | 6596 | const zcu = pt.zcu; |
| 7454 | 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 | 6600 | const ptr = try f.resolveInst(prefetch.ptr); |
| 7458 | 6601 | try reap(f, inst, &.{prefetch.ptr}); |
| 7459 | 6602 | |
| 7460 | | const w = &f.object.code.writer; |
| 6603 | const w = &f.code.writer; |
| 7461 | 6604 | switch (prefetch.cache) { |
| 7462 | 6605 | .data => { |
| 7463 | 6606 | try w.writeAll("zig_prefetch("); |
| 7464 | 6607 | if (ptr_ty.isSlice(zcu)) |
| 7465 | 6608 | try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" }) |
| 7466 | 6609 | else |
| 7467 | | try f.writeCValue(w, ptr, .FunctionArgument); |
| 6610 | try f.writeCValue(w, ptr, .other); |
| 7468 | 6611 | try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality }); |
| 7469 | | try f.object.newline(); |
| 6612 | try f.newline(); |
| 7470 | 6613 | }, |
| 7471 | 6614 | // The available prefetch intrinsics do not accept a cache argument; only |
| 7472 | 6615 | // address, rw, and locality. |
| ... | ... | @@ -7479,14 +6622,14 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7479 | 6622 | fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7480 | 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 | 6626 | const inst_ty = f.typeOfIndex(inst); |
| 7484 | 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 | 6630 | try w.writeAll(" = "); |
| 7488 | 6631 | try w.print("zig_wasm_memory_size({d});", .{pl_op.payload}); |
| 7489 | | try f.object.newline(); |
| 6632 | try f.newline(); |
| 7490 | 6633 | |
| 7491 | 6634 | return local; |
| 7492 | 6635 | } |
| ... | ... | @@ -7494,23 +6637,23 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7494 | 6637 | fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7495 | 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 | 6641 | const inst_ty = f.typeOfIndex(inst); |
| 7499 | 6642 | const operand = try f.resolveInst(pl_op.operand); |
| 7500 | 6643 | try reap(f, inst, &.{pl_op.operand}); |
| 7501 | 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 | 6647 | try w.writeAll(" = "); |
| 7505 | 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 | 6650 | try w.writeAll(");"); |
| 7508 | | try f.object.newline(); |
| 6651 | try f.newline(); |
| 7509 | 6652 | return local; |
| 7510 | 6653 | } |
| 7511 | 6654 | |
| 7512 | 6655 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7513 | | const pt = f.object.dg.pt; |
| 6656 | const pt = f.dg.pt; |
| 7514 | 6657 | const zcu = pt.zcu; |
| 7515 | 6658 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7516 | 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 | 6666 | const inst_ty = f.typeOfIndex(inst); |
| 7524 | 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 | 6670 | const local = try f.allocLocal(inst, inst_ty); |
| 7528 | 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 | 6673 | try v.elem(f, w); |
| 7531 | 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 | 6676 | try w.writeByte('('); |
| 7534 | | try f.writeCValue(w, mulend1, .FunctionArgument); |
| 6677 | try f.writeCValue(w, mulend1, .other); |
| 7535 | 6678 | try v.elem(f, w); |
| 7536 | 6679 | try w.writeAll(", "); |
| 7537 | | try f.writeCValue(w, mulend2, .FunctionArgument); |
| 6680 | try f.writeCValue(w, mulend2, .other); |
| 7538 | 6681 | try v.elem(f, w); |
| 7539 | 6682 | try w.writeAll(", "); |
| 7540 | | try f.writeCValue(w, addend, .FunctionArgument); |
| 6683 | try f.writeCValue(w, addend, .other); |
| 7541 | 6684 | try v.elem(f, w); |
| 7542 | 6685 | try w.writeAll(");"); |
| 7543 | | try f.object.newline(); |
| 6686 | try f.newline(); |
| 7544 | 6687 | try v.end(f, inst, w); |
| 7545 | 6688 | |
| 7546 | 6689 | return local; |
| ... | ... | @@ -7548,34 +6691,33 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7548 | 6691 | |
| 7549 | 6692 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7550 | 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 | 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 | 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 | 6699 | try w.writeByte(';'); |
| 7557 | | try f.object.newline(); |
| 6700 | try f.newline(); |
| 7558 | 6701 | return local; |
| 7559 | 6702 | } |
| 7560 | 6703 | |
| 7561 | 6704 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7562 | | const pt = f.object.dg.pt; |
| 6705 | const pt = f.dg.pt; |
| 7563 | 6706 | const zcu = pt.zcu; |
| 7564 | 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 | 6712 | const local = try f.allocLocal(inst, inst_ty); |
| 7571 | 6713 | try w.writeAll("va_start(*(va_list *)&"); |
| 7572 | | try f.writeCValue(w, local, .Other); |
| 7573 | | if (function_info.param_ctypes.len > 0) { |
| 6714 | try f.writeCValue(w, local, .other); |
| 6715 | if (f.next_arg_index > 0) { |
| 7574 | 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 | 6719 | try w.writeAll(");"); |
| 7578 | | try f.object.newline(); |
| 6720 | try f.newline(); |
| 7579 | 6721 | return local; |
| 7580 | 6722 | } |
| 7581 | 6723 | |
| ... | ... | @@ -7586,15 +6728,15 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7586 | 6728 | const va_list = try f.resolveInst(ty_op.operand); |
| 7587 | 6729 | try reap(f, inst, &.{ty_op.operand}); |
| 7588 | 6730 | |
| 7589 | | const w = &f.object.code.writer; |
| 6731 | const w = &f.code.writer; |
| 7590 | 6732 | const local = try f.allocLocal(inst, inst_ty); |
| 7591 | | try f.writeCValue(w, local, .Other); |
| 6733 | try f.writeCValue(w, local, .other); |
| 7592 | 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 | 6736 | try w.writeAll(", "); |
| 7595 | 6737 | try f.renderType(w, ty_op.ty.toType()); |
| 7596 | 6738 | try w.writeAll(");"); |
| 7597 | | try f.object.newline(); |
| 6739 | try f.newline(); |
| 7598 | 6740 | return local; |
| 7599 | 6741 | } |
| 7600 | 6742 | |
| ... | ... | @@ -7604,11 +6746,11 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7604 | 6746 | const va_list = try f.resolveInst(un_op); |
| 7605 | 6747 | try reap(f, inst, &.{un_op}); |
| 7606 | 6748 | |
| 7607 | | const w = &f.object.code.writer; |
| 6749 | const w = &f.code.writer; |
| 7608 | 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 | 6752 | try w.writeAll(");"); |
| 7611 | | try f.object.newline(); |
| 6753 | try f.newline(); |
| 7612 | 6754 | return .none; |
| 7613 | 6755 | } |
| 7614 | 6756 | |
| ... | ... | @@ -7619,14 +6761,14 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7619 | 6761 | const va_list = try f.resolveInst(ty_op.operand); |
| 7620 | 6762 | try reap(f, inst, &.{ty_op.operand}); |
| 7621 | 6763 | |
| 7622 | | const w = &f.object.code.writer; |
| 6764 | const w = &f.code.writer; |
| 7623 | 6765 | const local = try f.allocLocal(inst, inst_ty); |
| 7624 | 6766 | try w.writeAll("va_copy(*(va_list *)&"); |
| 7625 | | try f.writeCValue(w, local, .Other); |
| 6767 | try f.writeCValue(w, local, .other); |
| 7626 | 6768 | try w.writeAll(", *(va_list *)"); |
| 7627 | | try f.writeCValue(w, va_list, .Other); |
| 6769 | try f.writeCValue(w, va_list, .other); |
| 7628 | 6770 | try w.writeAll(");"); |
| 7629 | | try f.object.newline(); |
| 6771 | try f.newline(); |
| 7630 | 6772 | return local; |
| 7631 | 6773 | } |
| 7632 | 6774 | |
| ... | ... | @@ -7943,103 +7085,193 @@ fn undefPattern(comptime IntType: type) IntType { |
| 7943 | 7085 | |
| 7944 | 7086 | const FormatIntLiteralContext = struct { |
| 7945 | 7087 | dg: *DeclGen, |
| 7946 | | int_info: InternPool.Key.IntType, |
| 7947 | | kind: CType.Kind, |
| 7948 | | ctype: CType, |
| 7088 | loc: ValueRenderLocation, |
| 7949 | 7089 | val: Value, |
| 7090 | cty: CType, |
| 7950 | 7091 | base: u8, |
| 7951 | 7092 | case: std.fmt.Case, |
| 7952 | 7093 | }; |
| 7953 | 7094 | fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void { |
| 7954 | | const pt = data.dg.pt; |
| 7955 | | const zcu = pt.zcu; |
| 7956 | | const target = &data.dg.mod.resolved_target.result; |
| 7957 | | const ctype_pool = &data.dg.ctype_pool; |
| 7958 | | |
| 7959 | | const ExpectedContents = struct { |
| 7960 | | const base = 10; |
| 7961 | | const bits = 128; |
| 7962 | | const limbs_count = BigInt.calcTwosCompLimbCount(bits); |
| 7963 | | |
| 7964 | | undef_limbs: [limbs_count]BigIntLimb, |
| 7965 | | wrap_limbs: [limbs_count]BigIntLimb, |
| 7966 | | to_string_buf: [bits]u8, |
| 7967 | | to_string_limbs: [BigInt.calcToStringLimbsBufferLen(limbs_count, base)]BigIntLimb, |
| 7968 | | }; |
| 7969 | | var stack align(@alignOf(ExpectedContents)) = |
| 7970 | | std.heap.stackFallback(@sizeOf(ExpectedContents), data.dg.gpa); |
| 7971 | | const allocator = stack.get(); |
| 7972 | | |
| 7973 | | var undef_limbs: []BigIntLimb = &.{}; |
| 7974 | | defer allocator.free(undef_limbs); |
| 7975 | | |
| 7976 | | var int_buf: Value.BigIntSpace = undefined; |
| 7977 | | const int = if (data.val.isUndef(zcu)) blk: { |
| 7978 | | undef_limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)) catch return error.WriteFailed; |
| 7979 | | @memset(undef_limbs, undefPattern(BigIntLimb)); |
| 7980 | | |
| 7981 | | var undef_int = BigInt.Mutable{ |
| 7982 | | .limbs = undef_limbs, |
| 7983 | | .len = undef_limbs.len, |
| 7984 | | .positive = true, |
| 7985 | | }; |
| 7986 | | undef_int.truncate(undef_int.toConst(), data.int_info.signedness, data.int_info.bits); |
| 7987 | | break :blk undef_int.toConst(); |
| 7988 | | } else data.val.toBigInt(&int_buf, zcu); |
| 7989 | | assert(int.fitsInTwosComp(data.int_info.signedness, data.int_info.bits)); |
| 7990 | | |
| 7991 | | const c_bits: usize = @intCast(data.ctype.byteSize(ctype_pool, data.dg.mod) * 8); |
| 7992 | | var one_limbs: [BigInt.calcLimbLen(1)]BigIntLimb = undefined; |
| 7993 | | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); |
| 7994 | | |
| 7995 | | var wrap = BigInt.Mutable{ |
| 7996 | | .limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits)) catch return error.WriteFailed, |
| 7997 | | .len = undefined, |
| 7998 | | .positive = undefined, |
| 7999 | | }; |
| 8000 | | defer allocator.free(wrap.limbs); |
| 8001 | | |
| 8002 | | const c_limb_info: struct { |
| 8003 | | ctype: CType, |
| 8004 | | count: usize, |
| 8005 | | endian: std.builtin.Endian, |
| 8006 | | homogeneous: bool, |
| 8007 | | } = switch (data.ctype.info(ctype_pool)) { |
| 8008 | | .basic => |basic_info| switch (basic_info) { |
| 8009 | | else => .{ |
| 8010 | | .ctype = .void, |
| 8011 | | .count = 1, |
| 8012 | | .endian = .little, |
| 8013 | | .homogeneous = true, |
| 7095 | const dg = data.dg; |
| 7096 | const zcu = dg.pt.zcu; |
| 7097 | const target = &dg.mod.resolved_target.result; |
| 7098 | |
| 7099 | const val = data.val; |
| 7100 | const ty = val.typeOf(zcu); |
| 7101 | |
| 7102 | assert(!val.isUndef(zcu)); |
| 7103 | |
| 7104 | var space: Value.BigIntSpace = undefined; |
| 7105 | const val_bigint = val.toBigInt(&space, zcu); |
| 7106 | |
| 7107 | switch (CType.classifyInt(ty, zcu)) { |
| 7108 | .void => unreachable, // opv |
| 7109 | .small => |int_cty| return FormatInt128.format(.{ |
| 7110 | .target = zcu.getTarget(), |
| 7111 | .int_cty = int_cty, |
| 7112 | .val = val_bigint, |
| 7113 | .is_global = data.loc == .static_initializer, |
| 7114 | .base = data.base, |
| 7115 | .case = data.case, |
| 7116 | }, w), |
| 7117 | .big => |big| { |
| 7118 | if (!data.loc.isInitializer()) { |
| 7119 | // Use `CType.fmtTypeName` directly to avoid the possibility of `error.OutOfMemory`. |
| 7120 | try w.print("({f})", .{data.cty.fmtTypeName(zcu)}); |
| 7121 | } |
| 7122 | |
| 7123 | try w.writeAll("{{"); |
| 7124 | |
| 7125 | var limb_buf: [std.math.big.int.calcTwosCompLimbCount(65535)]std.math.big.Limb = undefined; |
| 7126 | for (0..big.limbs_len) |limb_index| { |
| 7127 | if (limb_index != 0) try w.writeAll(", "); |
| 7128 | const limb_bit_offset: u64 = switch (target.cpu.arch.endian()) { |
| 7129 | .little => limb_index * big.limb_size.bits(), |
| 7130 | .big => (big.limbs_len - limb_index - 1) * big.limb_size.bits(), |
| 7131 | }; |
| 7132 | var limb_bigint: std.math.big.int.Mutable = .{ |
| 7133 | .limbs = &limb_buf, |
| 7134 | .len = undefined, |
| 7135 | .positive = undefined, |
| 7136 | }; |
| 7137 | limb_bigint.shiftRight(val_bigint, limb_bit_offset); |
| 7138 | limb_bigint.truncate(limb_bigint.toConst(), .unsigned, big.limb_size.bits()); |
| 7139 | try FormatInt128.format(.{ |
| 7140 | .target = zcu.getTarget(), |
| 7141 | .int_cty = big.limb_size.unsigned(), |
| 7142 | .val = limb_bigint.toConst(), |
| 7143 | .is_global = data.loc == .static_initializer, |
| 7144 | .base = data.base, |
| 7145 | .case = data.case, |
| 7146 | }, w); |
| 7147 | } |
| 7148 | |
| 7149 | try w.writeAll("}}"); |
| 7150 | }, |
| 7151 | } |
| 7152 | } |
| 7153 | const FormatInt128 = struct { |
| 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 => .{ |
| 8016 | | .ctype = .u64, |
| 8017 | | .count = 2, |
| 8018 | | .endian = .big, |
| 8019 | | .homogeneous = false, |
| 7207 | |
| 7208 | .zig_i128 => { |
| 7209 | const raw = val.toInt(i128) catch unreachable; |
| 7210 | const lo: u64 = @truncate(@as(u128, @bitCast(raw))); |
| 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 | | }, |
| 8022 | | .array => |array_info| .{ |
| 8023 | | .ctype = array_info.elem_ctype, |
| 8024 | | .count = @intCast(array_info.len), |
| 8025 | | .endian = target.cpu.arch.endian(), |
| 8026 | | .homogeneous = true, |
| 8027 | | }, |
| 8028 | | else => unreachable, |
| 7219 | } |
| 7220 | } |
| 7221 | }; |
| 7222 | fn fmtUnsignedIntLiteralSmall( |
| 7223 | target: *const std.Target, |
| 7224 | int_cty: CType.Int, |
| 7225 | val: u64, |
| 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) { |
| 8031 | | if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or |
| 8032 | | data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits)) |
| 8033 | | return w.print("{s}_{s}", .{ |
| 8034 | | data.ctype.getStandardDefineAbbrev() orelse return w.print("zig_{s}Int_{c}{d}", .{ |
| 8035 | | if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits, |
| 8036 | | }), |
| 8037 | | if (int.positive) "MAX" else "MIN", |
| 8038 | | }); |
| 8039 | | |
| 8040 | | if (!int.positive) try w.writeByte('-'); |
| 8041 | | try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool); |
| 7238 | } |
| 7239 | fn fmtSignedIntLiteralSmall( |
| 7240 | target: *const std.Target, |
| 7241 | int_cty: CType.Int, |
| 7242 | val: i64, |
| 7243 | is_global: bool, |
| 7244 | base: u8, |
| 7245 | case: std.fmt.Case, |
| 7246 | ) FormatSignedIntLiteralSmall { |
| 7247 | return .{ |
| 7248 | .target = target, |
| 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 | 7275 | switch (data.base) { |
| 8044 | 7276 | 2 => try w.writeAll("0b"), |
| 8045 | 7277 | 8 => try w.writeByte('0'), |
| ... | ... | @@ -8047,68 +7279,131 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *Writer) Writer.Error!void |
| 8047 | 7279 | 16 => try w.writeAll("0x"), |
| 8048 | 7280 | else => unreachable, |
| 8049 | 7281 | } |
| 8050 | | const string = int.abs().toStringAlloc(allocator, data.base, data.case) catch |
| 8051 | | return error.WriteFailed; |
| 8052 | | defer allocator.free(string); |
| 8053 | | try w.writeAll(string); |
| 8054 | | } else { |
| 8055 | | try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool); |
| 8056 | | wrap.truncate(int, .unsigned, c_bits); |
| 8057 | | @memset(wrap.limbs[wrap.len..], 0); |
| 8058 | | wrap.len = wrap.limbs.len; |
| 8059 | | const limbs_per_c_limb = @divExact(wrap.len, c_limb_info.count); |
| 8060 | | |
| 8061 | | var c_limb_int_info: std.builtin.Type.Int = .{ |
| 8062 | | .signedness = undefined, |
| 8063 | | .bits = @intCast(@divExact(c_bits, c_limb_info.count)), |
| 8064 | | }; |
| 8065 | | var c_limb_ctype: CType = undefined; |
| 8066 | | |
| 8067 | | var limb_offset: usize = 0; |
| 8068 | | const most_significant_limb_i = wrap.len - limbs_per_c_limb; |
| 8069 | | while (limb_offset < wrap.len) : (limb_offset += limbs_per_c_limb) { |
| 8070 | | const limb_i = switch (c_limb_info.endian) { |
| 8071 | | .little => limb_offset, |
| 8072 | | .big => most_significant_limb_i - limb_offset, |
| 8073 | | }; |
| 8074 | | var c_limb_mut = BigInt.Mutable{ |
| 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); |
| 7282 | // This `@abs` is safe thanks to the `min_int` case above. |
| 7283 | try w.printInt(@abs(data.val), data.base, data.case, .{}); |
| 7284 | try w.writeAll(intLiteralSuffix(data.int_cty)); |
| 7285 | } |
| 7286 | }; |
| 7287 | const FormatUnsignedIntLiteralSmall = struct { |
| 7288 | target: *const std.Target, |
| 7289 | int_cty: CType.Int, |
| 7290 | val: u64, |
| 7291 | is_global: bool, |
| 7292 | base: u8, |
| 7293 | case: std.fmt.Case, |
| 7294 | pub fn format(data: FormatUnsignedIntLiteralSmall, w: *Writer) Writer.Error!void { |
| 7295 | const bits = data.int_cty.bits(data.target); |
| 7296 | const max_int: u64 = @as(u64, std.math.maxInt(u64)) >> @intCast(64 - bits); |
| 7297 | if (data.val == max_int) { |
| 7298 | return w.print("{s}_MAX", .{minMaxMacroPrefix(data.int_cty)}); |
| 7299 | } |
| 7300 | try w.writeAll(intLiteralPrefix(data.int_cty, data.is_global)); |
| 7301 | switch (data.base) { |
| 7302 | 2 => try w.writeAll("0b"), |
| 7303 | 8 => try w.writeByte('0'), |
| 7304 | 10 => {}, |
| 7305 | 16 => try w.writeAll("0x"), |
| 7306 | else => unreachable, |
| 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 | 7409 | const Materialize = struct { |
| ... | ... | @@ -8123,7 +7418,7 @@ const Materialize = struct { |
| 8123 | 7418 | } |
| 8124 | 7419 | |
| 8125 | 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 | 7424 | pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -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 | 7429 | const Vectorize = struct { |
| 8179 | 7430 | index: CValue = .none, |
| 8180 | 7431 | |
| 8181 | 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 | 7434 | const zcu = pt.zcu; |
| 8184 | | return if (ty.zigTypeTag(zcu) == .vector) index: { |
| 8185 | | const local = try f.allocLocal(inst, .usize); |
| 8186 | | |
| 8187 | | try w.writeAll("for ("); |
| 8188 | | try f.writeCValue(w, local, .Other); |
| 8189 | | try w.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)}); |
| 8190 | | try f.writeCValue(w, local, .Other); |
| 8191 | | try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 8192 | | try f.writeCValue(w, local, .Other); |
| 8193 | | try w.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)}); |
| 8194 | | f.object.indent(); |
| 8195 | | try f.object.newline(); |
| 8196 | | |
| 8197 | | break :index .{ .index = local }; |
| 8198 | | } else .{}; |
| 7435 | switch (ty.zigTypeTag(zcu)) { |
| 7436 | else => return .{ .index = .none }, |
| 7437 | .vector => { |
| 7438 | const local = try f.allocLocal(inst, .usize); |
| 7439 | try w.writeAll("for ("); |
| 7440 | try f.writeCValue(w, local, .other); |
| 7441 | try w.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)}); |
| 7442 | try f.writeCValue(w, local, .other); |
| 7443 | try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 7444 | try f.writeCValue(w, local, .other); |
| 7445 | try w.print(" += {f}) {{", .{try f.fmtIntLiteralDec(.one_usize)}); |
| 7446 | f.indent(); |
| 7447 | try f.newline(); |
| 7448 | return .{ .index = local }; |
| 7449 | }, |
| 7450 | } |
| 8199 | 7451 | } |
| 8200 | 7452 | |
| 8201 | 7453 | pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void { |
| 8202 | 7454 | if (self.index != .none) { |
| 8203 | | try w.writeByte('['); |
| 8204 | | try f.writeCValue(w, self.index, .Other); |
| 7455 | try w.writeAll(".array["); |
| 7456 | try f.writeCValue(w, self.index, .other); |
| 8205 | 7457 | try w.writeByte(']'); |
| 8206 | 7458 | } |
| 8207 | 7459 | } |
| 8208 | 7460 | |
| 8209 | 7461 | pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void { |
| 8210 | 7462 | if (self.index != .none) { |
| 8211 | | try f.object.outdent(); |
| 7463 | try f.outdent(); |
| 8212 | 7464 | try w.writeByte('}'); |
| 8213 | | try f.object.newline(); |
| 7465 | try f.newline(); |
| 8214 | 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 | 7472 | return switch (ty.zigTypeTag(zcu)) { |
| 8221 | | .array, .vector => return true, |
| 8222 | | else => return ty.isAbiInt(zcu) and toCIntBits(@as(u32, @intCast(ty.bitSize(zcu)))) == null, |
| 7473 | .int, .@"enum", .@"struct", .@"union" => CType.classifyInt(ty, zcu) == .big, |
| 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 | 7497 | } |
| 8246 | 7498 | |
| 8247 | 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; |
| 8249 | | const local = &f.locals.items[local_index]; |
| 7500 | const gpa = f.dg.gpa; |
| 7501 | const local = f.locals.items[local_index]; |
| 8250 | 7502 | if (inst) |i| { |
| 8251 | 7503 | if (ref_inst) |operand| { |
| 8252 | 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 | 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 | 7516 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 8265 | 7517 | if (std.debug.runtime_safety) { |
| 8266 | 7518 | // If this trips, an unfreeable allocation was attempted to be freed. |
| ... | ... | @@ -8317,3 +7569,28 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { |
| 8317 | 7569 | } |
| 8318 | 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 | } |