| ... | @@ -30,10 +30,9 @@ const BigInt = std.math.big.int; | ... | @@ -30,10 +30,9 @@ const BigInt = std.math.big.int; |
| 30 | | 30 | |
| 31 | pub const CValue = union(enum) { | 31 | pub const CValue = union(enum) { |
| 32 | none: void, | 32 | none: void, |
| 33 | /// Index into local_names | 33 | local: LocalIndex, |
| 34 | local: usize, | 34 | /// Address of a local. |
| 35 | /// Index into local_names, but take the address. | 35 | local_ref: LocalIndex, |
| 36 | local_ref: usize, | | |
| 37 | /// A constant instruction, to be rendered inline. | 36 | /// A constant instruction, to be rendered inline. |
| 38 | constant: Air.Inst.Ref, | 37 | constant: Air.Inst.Ref, |
| 39 | /// Index into the parameters | 38 | /// Index into the parameters |
| ... | @@ -50,8 +49,6 @@ pub const CValue = union(enum) { | ... | @@ -50,8 +49,6 @@ pub const CValue = union(enum) { |
| 50 | /// Render these bytes literally. | 49 | /// Render these bytes literally. |
| 51 | /// TODO make this a [*:0]const u8 to save memory | 50 | /// TODO make this a [*:0]const u8 to save memory |
| 52 | bytes: []const u8, | 51 | bytes: []const u8, |
| 53 | /// Index of an instruction that should later be rendered inline. | | |
| 54 | inline_index: Air.Inst.Index, | | |
| 55 | }; | 52 | }; |
| 56 | | 53 | |
| 57 | const BlockData = struct { | 54 | const BlockData = struct { |
| ... | @@ -72,6 +69,19 @@ pub const TypedefMap = std.ArrayHashMap( | ... | @@ -72,6 +69,19 @@ pub const TypedefMap = std.ArrayHashMap( |
| 72 | true, | 69 | true, |
| 73 | ); | 70 | ); |
| 74 | | 71 | |
| | 72 | const LoopDepth = u16; |
| | 73 | const Local = struct { |
| | 74 | ty: Type, |
| | 75 | alignment: u32, |
| | 76 | /// How many loops the last definition was nested in. |
| | 77 | loop_depth: LoopDepth, |
| | 78 | }; |
| | 79 | |
| | 80 | const LocalIndex = u16; |
| | 81 | const LocalsList = std.ArrayListUnmanaged(LocalIndex); |
| | 82 | const LocalsMap = std.ArrayHashMapUnmanaged(Type, LocalsList, Type.HashContext32, true); |
| | 83 | const LocalsStack = std.ArrayListUnmanaged(LocalsMap); |
| | 84 | |
| 75 | const FormatTypeAsCIdentContext = struct { | 85 | const FormatTypeAsCIdentContext = struct { |
| 76 | ty: Type, | 86 | ty: Type, |
| 77 | mod: *Module, | 87 | mod: *Module, |
| ... | @@ -81,7 +91,6 @@ const ValueRenderLocation = enum { | ... | @@ -81,7 +91,6 @@ const ValueRenderLocation = enum { |
| 81 | FunctionArgument, | 91 | FunctionArgument, |
| 82 | Initializer, | 92 | Initializer, |
| 83 | Other, | 93 | Other, |
| 84 | condition, | | |
| 85 | }; | 94 | }; |
| 86 | | 95 | |
| 87 | const BuiltinInfo = enum { | 96 | const BuiltinInfo = enum { |
| ... | @@ -254,10 +263,29 @@ pub const Function = struct { | ... | @@ -254,10 +263,29 @@ pub const Function = struct { |
| 254 | value_map: CValueMap, | 263 | value_map: CValueMap, |
| 255 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .{}, | 264 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .{}, |
| 256 | next_arg_index: usize = 0, | 265 | next_arg_index: usize = 0, |
| 257 | next_local_index: usize = 0, | | |
| 258 | next_block_index: usize = 0, | 266 | next_block_index: usize = 0, |
| 259 | object: Object, | 267 | object: Object, |
| 260 | func: *Module.Fn, | 268 | func: *Module.Fn, |
| | 269 | /// All the locals, to be emitted at the top of the function. |
| | 270 | locals: std.ArrayListUnmanaged(Local) = .{}, |
| | 271 | /// Which locals are available for reuse, based on Type. |
| | 272 | /// Only locals in the last stack entry are available for reuse, |
| | 273 | /// other entries will become available on loop exit. |
| | 274 | free_locals_stack: LocalsStack = .{}, |
| | 275 | free_locals_clone_depth: LoopDepth = 0, |
| | 276 | /// Locals which will not be freed by Liveness. This is used after a |
| | 277 | /// Function body is lowered in order to make `free_locals_stack` have |
| | 278 | /// 100% of the locals within so that it can be used to render the block |
| | 279 | /// of variable declarations at the top of a function, sorted descending |
| | 280 | /// by type alignment. |
| | 281 | /// The value is whether the alloc is static or not. |
| | 282 | allocs: std.AutoArrayHashMapUnmanaged(LocalIndex, bool) = .{}, |
| | 283 | /// Needed for memory used by the keys of free_locals_stack entries. |
| | 284 | arena: std.heap.ArenaAllocator, |
| | 285 | |
| | 286 | fn tyHashCtx(f: Function) Type.HashContext32 { |
| | 287 | return .{ .mod = f.object.dg.module }; |
| | 288 | } |
| 261 | | 289 | |
| 262 | fn resolveInst(f: *Function, inst: Air.Inst.Ref) !CValue { | 290 | fn resolveInst(f: *Function, inst: Air.Inst.Ref) !CValue { |
| 263 | const gop = try f.value_map.getOrPut(inst); | 291 | const gop = try f.value_map.getOrPut(inst); |
| ... | @@ -268,9 +296,12 @@ pub const Function = struct { | ... | @@ -268,9 +296,12 @@ pub const Function = struct { |
| 268 | | 296 | |
| 269 | const result = if (lowersToArray(ty, f.object.dg.module.getTarget())) result: { | 297 | const result = if (lowersToArray(ty, f.object.dg.module.getTarget())) result: { |
| 270 | const writer = f.object.code_header.writer(); | 298 | const writer = f.object.code_header.writer(); |
| 271 | const decl_c_value = f.allocLocalValue(); | 299 | const alignment = 0; |
| | 300 | const decl_c_value = try f.allocLocalValue(ty, alignment); |
| | 301 | const gpa = f.object.dg.gpa; |
| | 302 | try f.allocs.put(gpa, decl_c_value.local, true); |
| 272 | try writer.writeAll("static "); | 303 | try writer.writeAll("static "); |
| 273 | try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, .Const, 0, .Complete); | 304 | try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, .Const, alignment, .Complete); |
| 274 | try writer.writeAll(" = "); | 305 | try writer.writeAll(" = "); |
| 275 | try f.object.dg.renderValue(writer, ty, val, .Initializer); | 306 | try f.object.dg.renderValue(writer, ty, val, .Initializer); |
| 276 | try writer.writeAll(";\n "); | 307 | try writer.writeAll(";\n "); |
| ... | @@ -281,19 +312,6 @@ pub const Function = struct { | ... | @@ -281,19 +312,6 @@ pub const Function = struct { |
| 281 | return result; | 312 | return result; |
| 282 | } | 313 | } |
| 283 | | 314 | |
| 284 | fn resolveInstNoInline(f: *Function, inst: Air.Inst.Ref) !CValue { | | |
| 285 | const operand = try f.resolveInst(inst); | | |
| 286 | if (operand != .inline_index) return operand; | | |
| 287 | | | |
| 288 | const inst_ty = f.air.typeOf(inst); | | |
| 289 | const writer = f.object.writer(); | | |
| 290 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 291 | try writer.writeAll(" = "); | | |
| 292 | try f.writeCValueInline(operand.inline_index); | | |
| 293 | try writer.writeAll(";\n"); | | |
| 294 | return local; | | |
| 295 | } | | |
| 296 | | | |
| 297 | fn wantSafety(f: *Function) bool { | 315 | fn wantSafety(f: *Function) bool { |
| 298 | return switch (f.object.dg.module.optimizeMode()) { | 316 | return switch (f.object.dg.module.optimizeMode()) { |
| 299 | .Debug, .ReleaseSafe => true, | 317 | .Debug, .ReleaseSafe => true, |
| ... | @@ -301,27 +319,43 @@ pub const Function = struct { | ... | @@ -301,27 +319,43 @@ pub const Function = struct { |
| 301 | }; | 319 | }; |
| 302 | } | 320 | } |
| 303 | | 321 | |
| 304 | fn allocLocalValue(f: *Function) CValue { | 322 | fn getFreeLocals(f: *Function) *LocalsMap { |
| 305 | const result = f.next_local_index; | 323 | return &f.free_locals_stack.items[f.free_locals_stack.items.len - 1]; |
| 306 | f.next_local_index += 1; | 324 | } |
| 307 | return .{ .local = result }; | 325 | |
| | 326 | /// Skips the reuse logic. |
| | 327 | fn allocLocalValue(f: *Function, ty: Type, alignment: u32) !CValue { |
| | 328 | const gpa = f.object.dg.gpa; |
| | 329 | try f.locals.append(gpa, .{ |
| | 330 | .ty = ty, |
| | 331 | .alignment = alignment, |
| | 332 | .loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1), |
| | 333 | }); |
| | 334 | return CValue{ .local = @intCast(LocalIndex, f.locals.items.len - 1) }; |
| 308 | } | 335 | } |
| 309 | | 336 | |
| 310 | fn allocLocal(f: *Function, ty: Type, mutability: Mutability) !CValue { | 337 | fn allocLocal(f: *Function, inst: Air.Inst.Index, ty: Type) !CValue { |
| 311 | return f.allocAlignedLocal(ty, mutability, 0); | 338 | const result = try f.allocAlignedLocal(ty, .Mut, 0); |
| | 339 | log.debug("%{d}: allocating t{d}", .{ inst, result.local }); |
| | 340 | return result; |
| 312 | } | 341 | } |
| 313 | | 342 | |
| | 343 | /// Only allocates the local; does not print anything. |
| 314 | fn allocAlignedLocal(f: *Function, ty: Type, mutability: Mutability, alignment: u32) !CValue { | 344 | fn allocAlignedLocal(f: *Function, ty: Type, mutability: Mutability, alignment: u32) !CValue { |
| 315 | const local_value = f.allocLocalValue(); | 345 | _ = mutability; |
| 316 | try f.object.dg.renderTypeAndName( | 346 | |
| 317 | f.object.writer(), | 347 | if (f.getFreeLocals().getPtrContext(ty, f.tyHashCtx())) |locals_list| { |
| 318 | ty, | 348 | for (locals_list.items) |local_index, i| { |
| 319 | local_value, | 349 | const local = &f.locals.items[local_index]; |
| 320 | mutability, | 350 | if (local.alignment >= alignment) { |
| 321 | alignment, | 351 | local.loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1); |
| 322 | .Complete, | 352 | _ = locals_list.swapRemove(i); |
| 323 | ); | 353 | return CValue{ .local = local_index }; |
| 324 | return local_value; | 354 | } |
| | 355 | } |
| | 356 | } |
| | 357 | |
| | 358 | return try f.allocLocalValue(ty, alignment); |
| 325 | } | 359 | } |
| 326 | | 360 | |
| 327 | fn writeCValue(f: *Function, w: anytype, c_value: CValue, location: ValueRenderLocation) !void { | 361 | fn writeCValue(f: *Function, w: anytype, c_value: CValue, location: ValueRenderLocation) !void { |
| ... | @@ -329,95 +363,10 @@ pub const Function = struct { | ... | @@ -329,95 +363,10 @@ pub const Function = struct { |
| 329 | .constant => |inst| { | 363 | .constant => |inst| { |
| 330 | const ty = f.air.typeOf(inst); | 364 | const ty = f.air.typeOf(inst); |
| 331 | const val = f.air.value(inst).?; | 365 | const val = f.air.value(inst).?; |
| 332 | try f.object.dg.renderValue(w, ty, val, location); | 366 | return f.object.dg.renderValue(w, ty, val, location); |
| 333 | }, | | |
| 334 | .undef => |ty| try f.object.dg.renderValue(w, ty, Value.undef, location), | | |
| 335 | .inline_index => |node| { | | |
| 336 | if (location != .condition) try w.writeByte('('); | | |
| 337 | try f.writeCValueInline(node); | | |
| 338 | if (location != .condition) try w.writeByte(')'); | | |
| 339 | }, | | |
| 340 | else => try f.object.dg.writeCValue(w, c_value), | | |
| 341 | } | | |
| 342 | } | | |
| 343 | | | |
| 344 | const E = error{ OutOfMemory, AnalysisFail }; | | |
| 345 | | | |
| 346 | fn writeCValueInline(f: *Function, inst: Air.Inst.Index) E!void { | | |
| 347 | switch (f.air.instructions.items(.tag)[inst]) { | | |
| 348 | // zig fmt: off | | |
| 349 | // TODO use a different strategy for add, sub, mul, div | | |
| 350 | // that communicates to the optimizer that wrapping is UB. | | |
| 351 | .add => try airBinOp(f, inst, "+", "add", .None), | | |
| 352 | .sub => try airBinOp(f, inst, "-", "sub", .None), | | |
| 353 | .mul => try airBinOp(f, inst, "*", "mul", .None), | | |
| 354 | | | |
| 355 | .div_float => try airBinBuiltinCall(f, inst, "div", .None), | | |
| 356 | | | |
| 357 | .div_trunc, .div_exact => try airBinOp(f, inst, "/", "div_trunc", .None), | | |
| 358 | .rem => { | | |
| 359 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | | |
| 360 | const lhs_ty = f.air.typeOf(bin_op.lhs); | | |
| 361 | // For binary operations @TypeOf(lhs)==@TypeOf(rhs), | | |
| 362 | // so we only check one. | | |
| 363 | if (lhs_ty.isInt()) | | |
| 364 | try airBinOp(f, inst, "%", "rem", .None) | | |
| 365 | else | | |
| 366 | try airBinFloatOp(f, inst, "fmod"); | | |
| 367 | }, | 367 | }, |
| 368 | .div_floor => try airBinBuiltinCall(f, inst, "div_floor", .None), | 368 | .undef => |ty| return f.object.dg.renderValue(w, ty, Value.undef, location), |
| 369 | .mod => try airBinBuiltinCall(f, inst, "mod", .None), | 369 | else => return f.object.dg.writeCValue(w, c_value), |
| 370 | | | |
| 371 | .addwrap => try airBinBuiltinCall(f, inst, "addw", .Bits), | | |
| 372 | .subwrap => try airBinBuiltinCall(f, inst, "subw", .Bits), | | |
| 373 | .mulwrap => try airBinBuiltinCall(f, inst, "mulw", .Bits), | | |
| 374 | | | |
| 375 | .add_sat => try airBinBuiltinCall(f, inst, "adds", .Bits), | | |
| 376 | .sub_sat => try airBinBuiltinCall(f, inst, "subs", .Bits), | | |
| 377 | .mul_sat => try airBinBuiltinCall(f, inst, "muls", .Bits), | | |
| 378 | .shl_sat => try airBinBuiltinCall(f, inst, "shls", .Bits), | | |
| 379 | | | |
| 380 | .min => try airMinMax(f, inst, '<', "fmin"), | | |
| 381 | .max => try airMinMax(f, inst, '>', "fmax"), | | |
| 382 | | | |
| 383 | .cmp_gt => try airCmpOp(f, inst, ">", "gt"), | | |
| 384 | .cmp_gte => try airCmpOp(f, inst, ">=", "ge"), | | |
| 385 | .cmp_lt => try airCmpOp(f, inst, "<", "lt"), | | |
| 386 | .cmp_lte => try airCmpOp(f, inst, "<=", "le"), | | |
| 387 | | | |
| 388 | .cmp_eq => try airEquality(f, inst, "((", "==", "eq"), | | |
| 389 | .cmp_neq => try airEquality(f, inst, "!((", "!=", "ne"), | | |
| 390 | | | |
| 391 | .bool_and, .bit_and => try airBinOp(f, inst, "&", "and", .None), | | |
| 392 | .bool_or, .bit_or => try airBinOp(f, inst, "|", "or", .None), | | |
| 393 | .xor => try airBinOp(f, inst, "^", "xor", .None), | | |
| 394 | .shr, .shr_exact => try airBinBuiltinCall(f, inst, "shr", .None), | | |
| 395 | .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits), | | |
| 396 | .shl_exact => try airBinOp(f, inst, "<<", "shl", .None), | | |
| 397 | .not => try airNot (f, inst), | | |
| 398 | | | |
| 399 | .is_err => try airIsErr(f, inst, false, "!="), | | |
| 400 | .is_non_err => try airIsErr(f, inst, false, "=="), | | |
| 401 | .is_err_ptr => try airIsErr(f, inst, true, "!="), | | |
| 402 | .is_non_err_ptr => try airIsErr(f, inst, true, "=="), | | |
| 403 | | | |
| 404 | .is_null => try airIsNull(f, inst, "==", false), | | |
| 405 | .is_non_null => try airIsNull(f, inst, "!=", false), | | |
| 406 | .is_null_ptr => try airIsNull(f, inst, "==", true), | | |
| 407 | .is_non_null_ptr => try airIsNull(f, inst, "!=", true), | | |
| 408 | | | |
| 409 | .get_union_tag => try airGetUnionTag(f, inst), | | |
| 410 | .clz => try airUnBuiltinCall(f, inst, "clz", .Bits), | | |
| 411 | .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits), | | |
| 412 | .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits), | | |
| 413 | .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits), | | |
| 414 | .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits), | | |
| 415 | .tag_name => try airTagName(f, inst), | | |
| 416 | .error_name => try airErrorName(f, inst), | | |
| 417 | | | |
| 418 | .ptrtoint => try airPtrToInt(f, inst), | | |
| 419 | else => unreachable, | | |
| 420 | // zig fmt: on | | |
| 421 | } | 370 | } |
| 422 | } | 371 | } |
| 423 | | 372 | |
| ... | @@ -443,7 +392,6 @@ pub const Function = struct { | ... | @@ -443,7 +392,6 @@ pub const Function = struct { |
| 443 | try w.writeByte('.'); | 392 | try w.writeByte('.'); |
| 444 | return f.writeCValue(w, member, .Other); | 393 | return f.writeCValue(w, member, .Other); |
| 445 | }, | 394 | }, |
| 446 | .inline_index => unreachable, // Use resolveInstNoInline | | |
| 447 | else => return f.object.dg.writeCValueMember(w, c_value, member), | 395 | else => return f.object.dg.writeCValueMember(w, c_value, member), |
| 448 | } | 396 | } |
| 449 | } | 397 | } |
| ... | @@ -477,6 +425,24 @@ pub const Function = struct { | ... | @@ -477,6 +425,24 @@ pub const Function = struct { |
| 477 | fn fmtIntLiteral(f: *Function, ty: Type, val: Value) !std.fmt.Formatter(formatIntLiteral) { | 425 | fn fmtIntLiteral(f: *Function, ty: Type, val: Value) !std.fmt.Formatter(formatIntLiteral) { |
| 478 | return f.object.dg.fmtIntLiteral(ty, val); | 426 | return f.object.dg.fmtIntLiteral(ty, val); |
| 479 | } | 427 | } |
| | 428 | |
| | 429 | pub fn deinit(f: *Function, gpa: mem.Allocator) void { |
| | 430 | f.allocs.deinit(gpa); |
| | 431 | f.locals.deinit(gpa); |
| | 432 | for (f.free_locals_stack.items) |*free_locals| { |
| | 433 | deinitFreeLocalsMap(gpa, free_locals); |
| | 434 | } |
| | 435 | f.free_locals_stack.deinit(gpa); |
| | 436 | f.blocks.deinit(gpa); |
| | 437 | f.value_map.deinit(); |
| | 438 | f.object.code.deinit(); |
| | 439 | for (f.object.dg.typedefs.values()) |typedef| { |
| | 440 | gpa.free(typedef.rendered); |
| | 441 | } |
| | 442 | f.object.dg.typedefs.deinit(); |
| | 443 | f.object.dg.fwd_decl.deinit(); |
| | 444 | f.arena.deinit(); |
| | 445 | } |
| 480 | }; | 446 | }; |
| 481 | | 447 | |
| 482 | /// This data is available when outputting .c code for a `Module`. | 448 | /// This data is available when outputting .c code for a `Module`. |
| ... | @@ -2267,7 +2233,7 @@ pub const DeclGen = struct { | ... | @@ -2267,7 +2233,7 @@ pub const DeclGen = struct { |
| 2267 | | 2233 | |
| 2268 | fn writeCValue(dg: *DeclGen, w: anytype, c_value: CValue) !void { | 2234 | fn writeCValue(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 2269 | switch (c_value) { | 2235 | switch (c_value) { |
| 2270 | .none, .inline_index => unreachable, | 2236 | .none => unreachable, |
| 2271 | .local => |i| return w.print("t{d}", .{i}), | 2237 | .local => |i| return w.print("t{d}", .{i}), |
| 2272 | .local_ref => |i| return w.print("&t{d}", .{i}), | 2238 | .local_ref => |i| return w.print("&t{d}", .{i}), |
| 2273 | .constant => unreachable, | 2239 | .constant => unreachable, |
| ... | @@ -2286,7 +2252,7 @@ pub const DeclGen = struct { | ... | @@ -2286,7 +2252,7 @@ pub const DeclGen = struct { |
| 2286 | | 2252 | |
| 2287 | fn writeCValueDeref(dg: *DeclGen, w: anytype, c_value: CValue) !void { | 2253 | fn writeCValueDeref(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 2288 | switch (c_value) { | 2254 | switch (c_value) { |
| 2289 | .none, .inline_index => unreachable, | 2255 | .none => unreachable, |
| 2290 | .local => |i| return w.print("(*t{d})", .{i}), | 2256 | .local => |i| return w.print("(*t{d})", .{i}), |
| 2291 | .local_ref => |i| return w.print("t{d}", .{i}), | 2257 | .local_ref => |i| return w.print("t{d}", .{i}), |
| 2292 | .constant => unreachable, | 2258 | .constant => unreachable, |
| ... | @@ -2316,7 +2282,7 @@ pub const DeclGen = struct { | ... | @@ -2316,7 +2282,7 @@ pub const DeclGen = struct { |
| 2316 | | 2282 | |
| 2317 | fn writeCValueDerefMember(dg: *DeclGen, writer: anytype, c_value: CValue, member: CValue) !void { | 2283 | fn writeCValueDerefMember(dg: *DeclGen, writer: anytype, c_value: CValue, member: CValue) !void { |
| 2318 | switch (c_value) { | 2284 | switch (c_value) { |
| 2319 | .none, .constant, .field, .undef, .inline_index => unreachable, | 2285 | .none, .constant, .field, .undef => unreachable, |
| 2320 | .local, .arg, .decl, .identifier, .bytes => { | 2286 | .local, .arg, .decl, .identifier, .bytes => { |
| 2321 | try dg.writeCValue(writer, c_value); | 2287 | try dg.writeCValue(writer, c_value); |
| 2322 | try writer.writeAll("->"); | 2288 | try writer.writeAll("->"); |
| ... | @@ -2502,12 +2468,13 @@ pub fn genFunc(f: *Function) !void { | ... | @@ -2502,12 +2468,13 @@ pub fn genFunc(f: *Function) !void { |
| 2502 | defer tracy.end(); | 2468 | defer tracy.end(); |
| 2503 | | 2469 | |
| 2504 | const o = &f.object; | 2470 | const o = &f.object; |
| | 2471 | const gpa = o.dg.gpa; |
| 2505 | const tv: TypedValue = .{ | 2472 | const tv: TypedValue = .{ |
| 2506 | .ty = o.dg.decl.ty, | 2473 | .ty = o.dg.decl.ty, |
| 2507 | .val = o.dg.decl.val, | 2474 | .val = o.dg.decl.val, |
| 2508 | }; | 2475 | }; |
| 2509 | | 2476 | |
| 2510 | o.code_header = std.ArrayList(u8).init(f.object.dg.gpa); | 2477 | o.code_header = std.ArrayList(u8).init(gpa); |
| 2511 | defer o.code_header.deinit(); | 2478 | defer o.code_header.deinit(); |
| 2512 | | 2479 | |
| 2513 | const is_global = o.dg.declIsGlobal(tv); | 2480 | const is_global = o.dg.declIsGlobal(tv); |
| ... | @@ -2529,11 +2496,59 @@ pub fn genFunc(f: *Function) !void { | ... | @@ -2529,11 +2496,59 @@ pub fn genFunc(f: *Function) !void { |
| 2529 | o.code_header.appendSliceAssumeCapacity("{\n "); | 2496 | o.code_header.appendSliceAssumeCapacity("{\n "); |
| 2530 | const empty_header_len = o.code_header.items.len; | 2497 | const empty_header_len = o.code_header.items.len; |
| 2531 | | 2498 | |
| | 2499 | f.free_locals_stack.clearRetainingCapacity(); |
| | 2500 | try f.free_locals_stack.append(gpa, .{}); |
| | 2501 | |
| 2532 | const main_body = f.air.getMainBody(); | 2502 | const main_body = f.air.getMainBody(); |
| 2533 | try genBody(f, main_body); | 2503 | try genBody(f, main_body); |
| 2534 | | 2504 | |
| 2535 | try o.indent_writer.insertNewline(); | 2505 | try o.indent_writer.insertNewline(); |
| 2536 | | 2506 | |
| | 2507 | // Take advantage of the free_locals map to bucket locals per type. All |
| | 2508 | // locals corresponding to AIR instructions should be in there due to |
| | 2509 | // Liveness analysis, however, locals from alloc instructions will be |
| | 2510 | // missing. These are added now to complete the map. Then we can sort by |
| | 2511 | // alignment, descending. |
| | 2512 | const free_locals = f.getFreeLocals(); |
| | 2513 | const values = f.allocs.values(); |
| | 2514 | for (f.allocs.keys()) |local_index, i| { |
| | 2515 | if (values[i]) continue; // static |
| | 2516 | const local = f.locals.items[local_index]; |
| | 2517 | log.debug("inserting local {d} into free_locals", .{local_index}); |
| | 2518 | const gop = try free_locals.getOrPutContext(gpa, local.ty, f.tyHashCtx()); |
| | 2519 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| | 2520 | try gop.value_ptr.append(gpa, local_index); |
| | 2521 | } |
| | 2522 | |
| | 2523 | const SortContext = struct { |
| | 2524 | target: std.Target, |
| | 2525 | keys: []const Type, |
| | 2526 | |
| | 2527 | pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool { |
| | 2528 | const a_ty = ctx.keys[a_index]; |
| | 2529 | const b_ty = ctx.keys[b_index]; |
| | 2530 | return b_ty.abiAlignment(ctx.target) < a_ty.abiAlignment(ctx.target); |
| | 2531 | } |
| | 2532 | }; |
| | 2533 | const target = o.dg.module.getTarget(); |
| | 2534 | free_locals.sort(SortContext{ .target = target, .keys = free_locals.keys() }); |
| | 2535 | |
| | 2536 | const w = o.code_header.writer(); |
| | 2537 | for (free_locals.values()) |list| { |
| | 2538 | for (list.items) |local_index| { |
| | 2539 | const local = f.locals.items[local_index]; |
| | 2540 | try o.dg.renderTypeAndName( |
| | 2541 | w, |
| | 2542 | local.ty, |
| | 2543 | .{ .local = local_index }, |
| | 2544 | .Mut, |
| | 2545 | local.alignment, |
| | 2546 | .Complete, |
| | 2547 | ); |
| | 2548 | try w.writeAll(";\n "); |
| | 2549 | } |
| | 2550 | } |
| | 2551 | |
| 2537 | // If we have a header to insert, append the body to the header | 2552 | // If we have a header to insert, append the body to the header |
| 2538 | // and then return the result, freeing the body. | 2553 | // and then return the result, freeing the body. |
| 2539 | if (o.code_header.items.len > empty_header_len) { | 2554 | if (o.code_header.items.len > empty_header_len) { |
| ... | @@ -2655,41 +2670,51 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2655,41 +2670,51 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2655 | .ptr_add => try airPtrAddSub(f, inst, '+'), | 2670 | .ptr_add => try airPtrAddSub(f, inst, '+'), |
| 2656 | .ptr_sub => try airPtrAddSub(f, inst, '-'), | 2671 | .ptr_sub => try airPtrAddSub(f, inst, '-'), |
| 2657 | | 2672 | |
| 2658 | .add => CValue{ .inline_index = inst }, | 2673 | // TODO use a different strategy for add, sub, mul, div |
| 2659 | .sub => CValue{ .inline_index = inst }, | 2674 | // that communicates to the optimizer that wrapping is UB. |
| 2660 | .mul => CValue{ .inline_index = inst }, | 2675 | .add => try airBinOp(f, inst, "+", "add", .None), |
| | 2676 | .sub => try airBinOp(f, inst, "-", "sub", .None), |
| | 2677 | .mul => try airBinOp(f, inst, "*", "mul", .None), |
| 2661 | | 2678 | |
| 2662 | .neg => try airFloatNeg(f, inst), | 2679 | .neg => try airFloatNeg(f, inst), |
| 2663 | .div_float => CValue{ .inline_index = inst }, | 2680 | .div_float => try airBinBuiltinCall(f, inst, "div", .None), |
| 2664 | | 2681 | |
| 2665 | .div_trunc, .div_exact => CValue{ .inline_index = inst }, | 2682 | .div_trunc, .div_exact => try airBinOp(f, inst, "/", "div_trunc", .None), |
| 2666 | .rem => CValue{ .inline_index = inst }, | 2683 | .rem => blk: { |
| 2667 | .div_floor => CValue{ .inline_index = inst }, | 2684 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 2668 | .mod => CValue{ .inline_index = inst }, | 2685 | const lhs_ty = f.air.typeOf(bin_op.lhs); |
| 2669 | | 2686 | // For binary operations @TypeOf(lhs)==@TypeOf(rhs), |
| 2670 | .addwrap => CValue{ .inline_index = inst }, | 2687 | // so we only check one. |
| 2671 | .subwrap => CValue{ .inline_index = inst }, | 2688 | break :blk if (lhs_ty.isInt()) |
| 2672 | .mulwrap => CValue{ .inline_index = inst }, | 2689 | try airBinOp(f, inst, "%", "rem", .None) |
| 2673 | | 2690 | else |
| 2674 | .add_sat => CValue{ .inline_index = inst }, | 2691 | try airBinFloatOp(f, inst, "fmod"); |
| 2675 | .sub_sat => CValue{ .inline_index = inst }, | 2692 | }, |
| 2676 | .mul_sat => CValue{ .inline_index = inst }, | 2693 | .div_floor => try airBinBuiltinCall(f, inst, "div_floor", .None), |
| 2677 | .shl_sat => CValue{ .inline_index = inst }, | 2694 | .mod => try airBinBuiltinCall(f, inst, "mod", .None), |
| 2678 | | 2695 | |
| 2679 | .sqrt, | 2696 | .addwrap => try airBinBuiltinCall(f, inst, "addw", .Bits), |
| 2680 | .sin, | 2697 | .subwrap => try airBinBuiltinCall(f, inst, "subw", .Bits), |
| 2681 | .cos, | 2698 | .mulwrap => try airBinBuiltinCall(f, inst, "mulw", .Bits), |
| 2682 | .tan, | 2699 | |
| 2683 | .exp, | 2700 | .add_sat => try airBinBuiltinCall(f, inst, "adds", .Bits), |
| 2684 | .exp2, | 2701 | .sub_sat => try airBinBuiltinCall(f, inst, "subs", .Bits), |
| 2685 | .log, | 2702 | .mul_sat => try airBinBuiltinCall(f, inst, "muls", .Bits), |
| 2686 | .log2, | 2703 | .shl_sat => try airBinBuiltinCall(f, inst, "shls", .Bits), |
| 2687 | .log10, | 2704 | |
| 2688 | .fabs, | 2705 | .sqrt => try airUnFloatOp(f, inst, "sqrt"), |
| 2689 | .floor, | 2706 | .sin => try airUnFloatOp(f, inst, "sin"), |
| 2690 | .ceil, | 2707 | .cos => try airUnFloatOp(f, inst, "cos"), |
| 2691 | .round, | 2708 | .tan => try airUnFloatOp(f, inst, "tan"), |
| 2692 | => |tag| try airUnFloatOp(f, inst, @tagName(tag)), | 2709 | .exp => try airUnFloatOp(f, inst, "exp"), |
| | 2710 | .exp2 => try airUnFloatOp(f, inst, "exp2"), |
| | 2711 | .log => try airUnFloatOp(f, inst, "log"), |
| | 2712 | .log2 => try airUnFloatOp(f, inst, "log2"), |
| | 2713 | .log10 => try airUnFloatOp(f, inst, "log10"), |
| | 2714 | .fabs => try airUnFloatOp(f, inst, "fabs"), |
| | 2715 | .floor => try airUnFloatOp(f, inst, "floor"), |
| | 2716 | .ceil => try airUnFloatOp(f, inst, "ceil"), |
| | 2717 | .round => try airUnFloatOp(f, inst, "round"), |
| 2693 | .trunc_float => try airUnFloatOp(f, inst, "trunc"), | 2718 | .trunc_float => try airUnFloatOp(f, inst, "trunc"), |
| 2694 | | 2719 | |
| 2695 | .mul_add => try airMulAdd(f, inst), | 2720 | .mul_add => try airMulAdd(f, inst), |
| ... | @@ -2699,45 +2724,45 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2699,45 +2724,45 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2699 | .mul_with_overflow => try airOverflow(f, inst, "mul", .Bits), | 2724 | .mul_with_overflow => try airOverflow(f, inst, "mul", .Bits), |
| 2700 | .shl_with_overflow => try airOverflow(f, inst, "shl", .Bits), | 2725 | .shl_with_overflow => try airOverflow(f, inst, "shl", .Bits), |
| 2701 | | 2726 | |
| 2702 | .min => CValue{ .inline_index = inst }, | 2727 | .min => try airMinMax(f, inst, '<', "fmin"), |
| 2703 | .max => CValue{ .inline_index = inst }, | 2728 | .max => try airMinMax(f, inst, '>', "fmax"), |
| 2704 | | 2729 | |
| 2705 | .slice => try airSlice(f, inst), | 2730 | .slice => try airSlice(f, inst), |
| 2706 | | 2731 | |
| 2707 | .cmp_gt => CValue{ .inline_index = inst }, | 2732 | .cmp_gt => try airCmpOp(f, inst, ">", "gt"), |
| 2708 | .cmp_gte => CValue{ .inline_index = inst }, | 2733 | .cmp_gte => try airCmpOp(f, inst, ">=", "ge"), |
| 2709 | .cmp_lt => CValue{ .inline_index = inst }, | 2734 | .cmp_lt => try airCmpOp(f, inst, "<", "lt"), |
| 2710 | .cmp_lte => CValue{ .inline_index = inst }, | 2735 | .cmp_lte => try airCmpOp(f, inst, "<=", "le"), |
| 2711 | | 2736 | |
| 2712 | .cmp_eq => CValue{ .inline_index = inst }, | 2737 | .cmp_eq => try airEquality(f, inst, "((", "==", "eq"), |
| 2713 | .cmp_neq => CValue{ .inline_index = inst }, | 2738 | .cmp_neq => try airEquality(f, inst, "!((", "!=", "ne"), |
| 2714 | | 2739 | |
| 2715 | .cmp_vector => return f.fail("TODO: C backend: implement cmp_vector", .{}), | 2740 | .cmp_vector => return f.fail("TODO: C backend: implement cmp_vector", .{}), |
| 2716 | .cmp_lt_errors_len => try airCmpLtErrorsLen(f, inst), | 2741 | .cmp_lt_errors_len => try airCmpLtErrorsLen(f, inst), |
| 2717 | | 2742 | |
| 2718 | // bool_and and bool_or are non-short-circuit operations | 2743 | // bool_and and bool_or are non-short-circuit operations |
| 2719 | .bool_and, .bit_and => CValue{ .inline_index = inst }, | 2744 | .bool_and, .bit_and => try airBinOp(f, inst, "&", "and", .None), |
| 2720 | .bool_or, .bit_or => CValue{ .inline_index = inst }, | 2745 | .bool_or, .bit_or => try airBinOp(f, inst, "|", "or", .None), |
| 2721 | .xor => CValue{ .inline_index = inst }, | 2746 | .xor => try airBinOp(f, inst, "^", "xor", .None), |
| 2722 | .shr, .shr_exact => CValue{ .inline_index = inst }, | 2747 | .shr, .shr_exact => try airBinBuiltinCall(f, inst, "shr", .None), |
| 2723 | .shl, => CValue{ .inline_index = inst }, | 2748 | .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits), |
| 2724 | .shl_exact => CValue{ .inline_index = inst }, | 2749 | .shl_exact => try airBinOp(f, inst, "<<", "shl", .None), |
| 2725 | .not => CValue{ .inline_index = inst }, | 2750 | .not => try airNot (f, inst), |
| 2726 | | 2751 | |
| 2727 | .optional_payload => try airOptionalPayload(f, inst), | 2752 | .optional_payload => try airOptionalPayload(f, inst), |
| 2728 | .optional_payload_ptr => try airOptionalPayloadPtr(f, inst), | 2753 | .optional_payload_ptr => try airOptionalPayloadPtr(f, inst), |
| 2729 | .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst), | 2754 | .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst), |
| 2730 | .wrap_optional => try airWrapOptional(f, inst), | 2755 | .wrap_optional => try airWrapOptional(f, inst), |
| 2731 | | 2756 | |
| 2732 | .is_err => CValue{ .inline_index = inst }, | 2757 | .is_err => try airIsErr(f, inst, false, "!="), |
| 2733 | .is_non_err => CValue{ .inline_index = inst }, | 2758 | .is_non_err => try airIsErr(f, inst, false, "=="), |
| 2734 | .is_err_ptr => CValue{ .inline_index = inst }, | 2759 | .is_err_ptr => try airIsErr(f, inst, true, "!="), |
| 2735 | .is_non_err_ptr => CValue{ .inline_index = inst }, | 2760 | .is_non_err_ptr => try airIsErr(f, inst, true, "=="), |
| 2736 | | 2761 | |
| 2737 | .is_null => CValue{ .inline_index = inst }, | 2762 | .is_null => try airIsNull(f, inst, "==", false), |
| 2738 | .is_non_null => CValue{ .inline_index = inst }, | 2763 | .is_non_null => try airIsNull(f, inst, "!=", false), |
| 2739 | .is_null_ptr => CValue{ .inline_index = inst }, | 2764 | .is_null_ptr => try airIsNull(f, inst, "==", true), |
| 2740 | .is_non_null_ptr => CValue{ .inline_index = inst }, | 2765 | .is_non_null_ptr => try airIsNull(f, inst, "!=", true), |
| 2741 | | 2766 | |
| 2742 | .alloc => try airAlloc(f, inst), | 2767 | .alloc => try airAlloc(f, inst), |
| 2743 | .ret_ptr => try airRetPtr(f, inst), | 2768 | .ret_ptr => try airRetPtr(f, inst), |
| ... | @@ -2765,14 +2790,14 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2765,14 +2790,14 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2765 | .memset => try airMemset(f, inst), | 2790 | .memset => try airMemset(f, inst), |
| 2766 | .memcpy => try airMemcpy(f, inst), | 2791 | .memcpy => try airMemcpy(f, inst), |
| 2767 | .set_union_tag => try airSetUnionTag(f, inst), | 2792 | .set_union_tag => try airSetUnionTag(f, inst), |
| 2768 | .get_union_tag => CValue{ .inline_index = inst }, | 2793 | .get_union_tag => try airGetUnionTag(f, inst), |
| 2769 | .clz => CValue{ .inline_index = inst }, | 2794 | .clz => try airUnBuiltinCall(f, inst, "clz", .Bits), |
| 2770 | .ctz => CValue{ .inline_index = inst }, | 2795 | .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits), |
| 2771 | .popcount => CValue{ .inline_index = inst }, | 2796 | .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits), |
| 2772 | .byte_swap => CValue{ .inline_index = inst }, | 2797 | .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits), |
| 2773 | .bit_reverse => CValue{ .inline_index = inst }, | 2798 | .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits), |
| 2774 | .tag_name => CValue{ .inline_index = inst }, | 2799 | .tag_name => try airTagName(f, inst), |
| 2775 | .error_name => CValue{ .inline_index = inst }, | 2800 | .error_name => try airErrorName(f, inst), |
| 2776 | .splat => try airSplat(f, inst), | 2801 | .splat => try airSplat(f, inst), |
| 2777 | .select => try airSelect(f, inst), | 2802 | .select => try airSelect(f, inst), |
| 2778 | .shuffle => try airShuffle(f, inst), | 2803 | .shuffle => try airShuffle(f, inst), |
| ... | @@ -2808,7 +2833,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2808,7 +2833,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2808 | .fpext, | 2833 | .fpext, |
| 2809 | => try airFloatCast(f, inst), | 2834 | => try airFloatCast(f, inst), |
| 2810 | | 2835 | |
| 2811 | .ptrtoint => CValue{ .inline_index = inst }, | 2836 | .ptrtoint => try airPtrToInt(f, inst), |
| 2812 | | 2837 | |
| 2813 | .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)), | 2838 | .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)), |
| 2814 | .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)), | 2839 | .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)), |
| ... | @@ -2877,6 +2902,9 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2877,6 +2902,9 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2877 | .error_set_has_value => return f.fail("TODO: C backend: implement error_set_has_value", .{}), | 2902 | .error_set_has_value => return f.fail("TODO: C backend: implement error_set_has_value", .{}), |
| 2878 | // zig fmt: on | 2903 | // zig fmt: on |
| 2879 | }; | 2904 | }; |
| | 2905 | if (result_value == .local) { |
| | 2906 | log.debug("map %{d} to t{d}", .{ inst, result_value.local }); |
| | 2907 | } |
| 2880 | switch (result_value) { | 2908 | switch (result_value) { |
| 2881 | .none => {}, | 2909 | .none => {}, |
| 2882 | else => try f.value_map.putNoClobber(Air.indexToRef(inst), result_value), | 2910 | else => try f.value_map.putNoClobber(Air.indexToRef(inst), result_value), |
| ... | @@ -2885,13 +2913,19 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, | ... | @@ -2885,13 +2913,19 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 2885 | } | 2913 | } |
| 2886 | | 2914 | |
| 2887 | fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: []const u8) !CValue { | 2915 | fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: []const u8) !CValue { |
| 2888 | if (f.liveness.isUnused(inst)) return CValue.none; | 2916 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 2917 | |
| | 2918 | if (f.liveness.isUnused(inst)) { |
| | 2919 | try reap(f, inst, &.{ty_op.operand}); |
| | 2920 | return CValue.none; |
| | 2921 | } |
| 2889 | | 2922 | |
| 2890 | const inst_ty = f.air.typeOfIndex(inst); | 2923 | const inst_ty = f.air.typeOfIndex(inst); |
| 2891 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 2924 | const operand = try f.resolveInst(ty_op.operand); |
| 2892 | const operand = try f.resolveInstNoInline(ty_op.operand); | 2925 | try reap(f, inst, &.{ty_op.operand}); |
| 2893 | const writer = f.object.writer(); | 2926 | const writer = f.object.writer(); |
| 2894 | const local = try f.allocLocal(inst_ty, .Const); | 2927 | const local = try f.allocLocal(inst, inst_ty); |
| | 2928 | try f.writeCValue(writer, local, .Other); |
| 2895 | try writer.writeAll(" = "); | 2929 | try writer.writeAll(" = "); |
| 2896 | if (is_ptr) { | 2930 | if (is_ptr) { |
| 2897 | try writer.writeByte('&'); | 2931 | try writer.writeByte('&'); |
| ... | @@ -2906,22 +2940,29 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -2906,22 +2940,29 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2906 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 2940 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 2907 | const ptr_ty = f.air.typeOf(bin_op.lhs); | 2941 | const ptr_ty = f.air.typeOf(bin_op.lhs); |
| 2908 | if ((!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst)) or | 2942 | if ((!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst)) or |
| 2909 | !inst_ty.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 2943 | !inst_ty.hasRuntimeBitsIgnoreComptime()) |
| | 2944 | { |
| | 2945 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 2946 | return CValue.none; |
| | 2947 | } |
| 2910 | | 2948 | |
| 2911 | const ptr = try f.resolveInst(bin_op.lhs); | 2949 | const ptr = try f.resolveInst(bin_op.lhs); |
| 2912 | const index = try f.resolveInst(bin_op.rhs); | 2950 | const index = try f.resolveInst(bin_op.rhs); |
| | 2951 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 2913 | | 2952 | |
| 2914 | const target = f.object.dg.module.getTarget(); | 2953 | const target = f.object.dg.module.getTarget(); |
| 2915 | const is_array = lowersToArray(inst_ty, target); | 2954 | const is_array = lowersToArray(inst_ty, target); |
| 2916 | | 2955 | |
| 2917 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | 2956 | const local = try f.allocLocal(inst, inst_ty); |
| 2918 | const writer = f.object.writer(); | 2957 | const writer = f.object.writer(); |
| 2919 | if (is_array) { | 2958 | if (is_array) { |
| 2920 | try writer.writeAll(";\n"); | | |
| 2921 | try writer.writeAll("memcpy("); | 2959 | try writer.writeAll("memcpy("); |
| 2922 | try f.writeCValue(writer, local, .FunctionArgument); | 2960 | try f.writeCValue(writer, local, .FunctionArgument); |
| 2923 | try writer.writeAll(", "); | 2961 | try writer.writeAll(", "); |
| 2924 | } else try writer.writeAll(" = "); | 2962 | } else { |
| | 2963 | try f.writeCValue(writer, local, .Other); |
| | 2964 | try writer.writeAll(" = "); |
| | 2965 | } |
| 2925 | try f.writeCValue(writer, ptr, .Other); | 2966 | try f.writeCValue(writer, ptr, .Other); |
| 2926 | try writer.writeByte('['); | 2967 | try writer.writeByte('['); |
| 2927 | try f.writeCValue(writer, index, .Other); | 2968 | try f.writeCValue(writer, index, .Other); |
| ... | @@ -2936,19 +2977,28 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -2936,19 +2977,28 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2936 | } | 2977 | } |
| 2937 | | 2978 | |
| 2938 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 2979 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2939 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 2940 | | | |
| 2941 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 2980 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 2942 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 2981 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| | 2982 | |
| | 2983 | if (f.liveness.isUnused(inst)) { |
| | 2984 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 2985 | return CValue.none; |
| | 2986 | } |
| | 2987 | |
| 2943 | const ptr_ty = f.air.typeOf(bin_op.lhs); | 2988 | const ptr_ty = f.air.typeOf(bin_op.lhs); |
| 2944 | const child_ty = ptr_ty.childType(); | 2989 | const child_ty = ptr_ty.childType(); |
| 2945 | | 2990 | |
| 2946 | const ptr = try f.resolveInst(bin_op.lhs); | 2991 | const ptr = try f.resolveInst(bin_op.lhs); |
| 2947 | if (!child_ty.hasRuntimeBitsIgnoreComptime()) return ptr; | 2992 | if (!child_ty.hasRuntimeBitsIgnoreComptime()) { |
| | 2993 | if (f.liveness.operandDies(inst, 1)) try die(f, inst, bin_op.rhs); |
| | 2994 | return ptr; |
| | 2995 | } |
| 2948 | const index = try f.resolveInst(bin_op.rhs); | 2996 | const index = try f.resolveInst(bin_op.rhs); |
| | 2997 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 2949 | | 2998 | |
| 2950 | const writer = f.object.writer(); | 2999 | const writer = f.object.writer(); |
| 2951 | const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); | 3000 | const local = try f.allocLocal(inst, f.air.typeOfIndex(inst)); |
| | 3001 | try f.writeCValue(writer, local, .Other); |
| 2952 | try writer.writeAll(" = &("); | 3002 | try writer.writeAll(" = &("); |
| 2953 | if (ptr_ty.ptrSize() == .One) { | 3003 | if (ptr_ty.ptrSize() == .One) { |
| 2954 | // It's a pointer to an array, so we need to de-reference. | 3004 | // It's a pointer to an array, so we need to de-reference. |
| ... | @@ -2967,22 +3017,29 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -2967,22 +3017,29 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2967 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3017 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 2968 | const slice_ty = f.air.typeOf(bin_op.lhs); | 3018 | const slice_ty = f.air.typeOf(bin_op.lhs); |
| 2969 | if ((!slice_ty.isVolatilePtr() and f.liveness.isUnused(inst)) or | 3019 | if ((!slice_ty.isVolatilePtr() and f.liveness.isUnused(inst)) or |
| 2970 | !inst_ty.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 3020 | !inst_ty.hasRuntimeBitsIgnoreComptime()) |
| | 3021 | { |
| | 3022 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3023 | return CValue.none; |
| | 3024 | } |
| 2971 | | 3025 | |
| 2972 | const slice = try f.resolveInst(bin_op.lhs); | 3026 | const slice = try f.resolveInst(bin_op.lhs); |
| 2973 | const index = try f.resolveInst(bin_op.rhs); | 3027 | const index = try f.resolveInst(bin_op.rhs); |
| | 3028 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 2974 | | 3029 | |
| 2975 | const target = f.object.dg.module.getTarget(); | 3030 | const target = f.object.dg.module.getTarget(); |
| 2976 | const is_array = lowersToArray(inst_ty, target); | 3031 | const is_array = lowersToArray(inst_ty, target); |
| 2977 | | 3032 | |
| 2978 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | 3033 | const local = try f.allocLocal(inst, inst_ty); |
| 2979 | const writer = f.object.writer(); | 3034 | const writer = f.object.writer(); |
| 2980 | if (is_array) { | 3035 | if (is_array) { |
| 2981 | try writer.writeAll(";\n"); | | |
| 2982 | try writer.writeAll("memcpy("); | 3036 | try writer.writeAll("memcpy("); |
| 2983 | try f.writeCValue(writer, local, .FunctionArgument); | 3037 | try f.writeCValue(writer, local, .FunctionArgument); |
| 2984 | try writer.writeAll(", "); | 3038 | try writer.writeAll(", "); |
| 2985 | } else try writer.writeAll(" = "); | 3039 | } else { |
| | 3040 | try f.writeCValue(writer, local, .Other); |
| | 3041 | try writer.writeAll(" = "); |
| | 3042 | } |
| 2986 | try f.writeCValue(writer, slice, .Other); | 3043 | try f.writeCValue(writer, slice, .Other); |
| 2987 | try writer.writeAll(".ptr["); | 3044 | try writer.writeAll(".ptr["); |
| 2988 | try f.writeCValue(writer, index, .Other); | 3045 | try f.writeCValue(writer, index, .Other); |
| ... | @@ -2997,23 +3054,28 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -2997,23 +3054,28 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2997 | } | 3054 | } |
| 2998 | | 3055 | |
| 2999 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 3056 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3000 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 3001 | | | |
| 3002 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 3057 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3003 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3058 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3004 | | 3059 | |
| | 3060 | if (f.liveness.isUnused(inst)) { |
| | 3061 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3062 | return CValue.none; |
| | 3063 | } |
| | 3064 | |
| 3005 | const slice_ty = f.air.typeOf(bin_op.lhs); | 3065 | const slice_ty = f.air.typeOf(bin_op.lhs); |
| 3006 | const child_ty = slice_ty.elemType2(); | 3066 | const child_ty = slice_ty.elemType2(); |
| 3007 | const slice = try f.resolveInst(bin_op.lhs); | 3067 | const slice = try f.resolveInst(bin_op.lhs); |
| | 3068 | const index = try f.resolveInst(bin_op.rhs); |
| | 3069 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3008 | | 3070 | |
| 3009 | const writer = f.object.writer(); | 3071 | const writer = f.object.writer(); |
| 3010 | const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); | 3072 | const local = try f.allocLocal(inst, f.air.typeOfIndex(inst)); |
| | 3073 | try f.writeCValue(writer, local, .Other); |
| 3011 | try writer.writeAll(" = "); | 3074 | try writer.writeAll(" = "); |
| 3012 | if (child_ty.hasRuntimeBitsIgnoreComptime()) try writer.writeByte('&'); | 3075 | if (child_ty.hasRuntimeBitsIgnoreComptime()) try writer.writeByte('&'); |
| 3013 | try f.writeCValue(writer, slice, .Other); | 3076 | try f.writeCValue(writer, slice, .Other); |
| 3014 | try writer.writeAll(".ptr"); | 3077 | try writer.writeAll(".ptr"); |
| 3015 | if (child_ty.hasRuntimeBitsIgnoreComptime()) { | 3078 | if (child_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3016 | const index = try f.resolveInst(bin_op.rhs); | | |
| 3017 | try writer.writeByte('['); | 3079 | try writer.writeByte('['); |
| 3018 | try f.writeCValue(writer, index, .Other); | 3080 | try f.writeCValue(writer, index, .Other); |
| 3019 | try writer.writeByte(']'); | 3081 | try writer.writeByte(']'); |
| ... | @@ -3023,24 +3085,30 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3023,24 +3085,30 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3023 | } | 3085 | } |
| 3024 | | 3086 | |
| 3025 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | 3087 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| | 3088 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3026 | const inst_ty = f.air.typeOfIndex(inst); | 3089 | const inst_ty = f.air.typeOfIndex(inst); |
| 3027 | if (f.liveness.isUnused(inst) or !inst_ty.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 3090 | if (f.liveness.isUnused(inst) or !inst_ty.hasRuntimeBitsIgnoreComptime()) { |
| | 3091 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3092 | return CValue.none; |
| | 3093 | } |
| 3028 | | 3094 | |
| 3029 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | | |
| 3030 | const array = try f.resolveInst(bin_op.lhs); | 3095 | const array = try f.resolveInst(bin_op.lhs); |
| 3031 | const index = try f.resolveInst(bin_op.rhs); | 3096 | const index = try f.resolveInst(bin_op.rhs); |
| | 3097 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3032 | | 3098 | |
| 3033 | const target = f.object.dg.module.getTarget(); | 3099 | const target = f.object.dg.module.getTarget(); |
| 3034 | const is_array = lowersToArray(inst_ty, target); | 3100 | const is_array = lowersToArray(inst_ty, target); |
| 3035 | | 3101 | |
| 3036 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | 3102 | const local = try f.allocLocal(inst, inst_ty); |
| 3037 | const writer = f.object.writer(); | 3103 | const writer = f.object.writer(); |
| 3038 | if (is_array) { | 3104 | if (is_array) { |
| 3039 | try writer.writeAll(";\n"); | | |
| 3040 | try writer.writeAll("memcpy("); | 3105 | try writer.writeAll("memcpy("); |
| 3041 | try f.writeCValue(writer, local, .FunctionArgument); | 3106 | try f.writeCValue(writer, local, .FunctionArgument); |
| 3042 | try writer.writeAll(", "); | 3107 | try writer.writeAll(", "); |
| 3043 | } else try writer.writeAll(" = "); | 3108 | } else { |
| | 3109 | try f.writeCValue(writer, local, .Other); |
| | 3110 | try writer.writeAll(" = "); |
| | 3111 | } |
| 3044 | try f.writeCValue(writer, array, .Other); | 3112 | try f.writeCValue(writer, array, .Other); |
| 3045 | try writer.writeByte('['); | 3113 | try writer.writeByte('['); |
| 3046 | try f.writeCValue(writer, index, .Other); | 3114 | try f.writeCValue(writer, index, .Other); |
| ... | @@ -3055,36 +3123,36 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3055,36 +3123,36 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3055 | } | 3123 | } |
| 3056 | | 3124 | |
| 3057 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { | 3125 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3058 | const writer = f.object.writer(); | | |
| 3059 | const inst_ty = f.air.typeOfIndex(inst); | 3126 | const inst_ty = f.air.typeOfIndex(inst); |
| 3060 | | 3127 | |
| 3061 | const elem_type = inst_ty.elemType(); | 3128 | const elem_type = inst_ty.elemType(); |
| 3062 | const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut; | | |
| 3063 | if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime()) { | 3129 | if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime()) { |
| 3064 | return CValue{ .undef = inst_ty }; | 3130 | return CValue{ .undef = inst_ty }; |
| 3065 | } | 3131 | } |
| 3066 | | 3132 | |
| | 3133 | const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut; |
| 3067 | const target = f.object.dg.module.getTarget(); | 3134 | const target = f.object.dg.module.getTarget(); |
| 3068 | // First line: the variable used as data storage. | | |
| 3069 | const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target)); | 3135 | const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target)); |
| 3070 | try writer.writeAll(";\n"); | 3136 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local }); |
| 3071 | | 3137 | const gpa = f.object.dg.module.gpa; |
| | 3138 | try f.allocs.put(gpa, local.local, false); |
| 3072 | return CValue{ .local_ref = local.local }; | 3139 | return CValue{ .local_ref = local.local }; |
| 3073 | } | 3140 | } |
| 3074 | | 3141 | |
| 3075 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 3142 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3076 | const writer = f.object.writer(); | | |
| 3077 | const inst_ty = f.air.typeOfIndex(inst); | 3143 | const inst_ty = f.air.typeOfIndex(inst); |
| 3078 | | 3144 | |
| 3079 | const elem_ty = inst_ty.elemType(); | 3145 | const elem_ty = inst_ty.elemType(); |
| 3080 | if (!elem_ty.hasRuntimeBitsIgnoreComptime()) { | 3146 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime()) { |
| 3081 | return CValue{ .undef = inst_ty }; | 3147 | return CValue{ .undef = inst_ty }; |
| 3082 | } | 3148 | } |
| 3083 | | 3149 | |
| 3084 | // First line: the variable used as data storage. | 3150 | const mutability: Mutability = if (inst_ty.isConstPtr()) .Const else .Mut; |
| 3085 | const local = try f.allocLocal(elem_ty, .Mut); | 3151 | const target = f.object.dg.module.getTarget(); |
| 3086 | try writer.writeAll(";\n"); | 3152 | const local = try f.allocAlignedLocal(elem_ty, mutability, inst_ty.ptrAlignment(target)); |
| 3087 | | 3153 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local }); |
| | 3154 | const gpa = f.object.dg.module.gpa; |
| | 3155 | try f.allocs.put(gpa, local.local, false); |
| 3088 | return CValue{ .local_ref = local.local }; | 3156 | return CValue{ .local_ref = local.local }; |
| 3089 | } | 3157 | } |
| 3090 | | 3158 | |
| ... | @@ -3100,21 +3168,25 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3100,21 +3168,25 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3100 | const src_ty = ptr_info.pointee_type; | 3168 | const src_ty = ptr_info.pointee_type; |
| 3101 | | 3169 | |
| 3102 | if (!src_ty.hasRuntimeBitsIgnoreComptime() or | 3170 | if (!src_ty.hasRuntimeBitsIgnoreComptime() or |
| 3103 | !ptr_info.@"volatile" and f.liveness.isUnused(inst)) | 3171 | (!ptr_info.@"volatile" and f.liveness.isUnused(inst))) |
| | 3172 | { |
| | 3173 | try reap(f, inst, &.{ty_op.operand}); |
| 3104 | return CValue.none; | 3174 | return CValue.none; |
| | 3175 | } |
| | 3176 | |
| | 3177 | const operand = try f.resolveInst(ty_op.operand); |
| | 3178 | |
| | 3179 | try reap(f, inst, &.{ty_op.operand}); |
| 3105 | | 3180 | |
| 3106 | const target = f.object.dg.module.getTarget(); | 3181 | const target = f.object.dg.module.getTarget(); |
| 3107 | const is_aligned = ptr_info.@"align" == 0 or ptr_info.@"align" >= src_ty.abiAlignment(target); | 3182 | const is_aligned = ptr_info.@"align" == 0 or ptr_info.@"align" >= src_ty.abiAlignment(target); |
| 3108 | const is_array = lowersToArray(src_ty, target); | 3183 | const is_array = lowersToArray(src_ty, target); |
| 3109 | const need_memcpy = !is_aligned or is_array; | 3184 | const need_memcpy = !is_aligned or is_array; |
| 3110 | const operand = try f.resolveInst(ty_op.operand); | | |
| 3111 | const writer = f.object.writer(); | 3185 | const writer = f.object.writer(); |
| 3112 | | 3186 | |
| 3113 | // We need to initialize arrays and unaligned loads with a memcpy so they must be mutable. | 3187 | const local = try f.allocLocal(inst, src_ty); |
| 3114 | const local = try f.allocLocal(src_ty, if (need_memcpy) .Mut else .Const); | | |
| 3115 | | 3188 | |
| 3116 | if (need_memcpy) { | 3189 | if (need_memcpy) { |
| 3117 | try writer.writeAll(";\n"); | | |
| 3118 | try writer.writeAll("memcpy("); | 3190 | try writer.writeAll("memcpy("); |
| 3119 | if (!is_array) try writer.writeByte('&'); | 3191 | if (!is_array) try writer.writeByte('&'); |
| 3120 | try f.writeCValue(writer, local, .FunctionArgument); | 3192 | try f.writeCValue(writer, local, .FunctionArgument); |
| ... | @@ -3148,6 +3220,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3148,6 +3220,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3148 | }; | 3220 | }; |
| 3149 | const field_ty = Type.initPayload(&field_pl.base); | 3221 | const field_ty = Type.initPayload(&field_pl.base); |
| 3150 | | 3222 | |
| | 3223 | try f.writeCValue(writer, local, .Other); |
| 3151 | try writer.writeAll(" = ("); | 3224 | try writer.writeAll(" = ("); |
| 3152 | try f.renderTypecast(writer, src_ty); | 3225 | try f.renderTypecast(writer, src_ty); |
| 3153 | try writer.writeAll(")zig_wrap_"); | 3226 | try writer.writeAll(")zig_wrap_"); |
| ... | @@ -3162,6 +3235,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3162,6 +3235,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3162 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .Bits); | 3235 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .Bits); |
| 3163 | try writer.writeByte(')'); | 3236 | try writer.writeByte(')'); |
| 3164 | } else { | 3237 | } else { |
| | 3238 | try f.writeCValue(writer, local, .Other); |
| 3165 | try writer.writeAll(" = "); | 3239 | try writer.writeAll(" = "); |
| 3166 | try f.writeCValueDeref(writer, operand); | 3240 | try f.writeCValueDeref(writer, operand); |
| 3167 | } | 3241 | } |
| ... | @@ -3181,9 +3255,10 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { | ... | @@ -3181,9 +3255,10 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3181 | if (lowered_ret_ty.hasRuntimeBitsIgnoreComptime()) { | 3255 | if (lowered_ret_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3182 | var deref = is_ptr; | 3256 | var deref = is_ptr; |
| 3183 | const operand = try f.resolveInst(un_op); | 3257 | const operand = try f.resolveInst(un_op); |
| 3184 | const ret_val = if (lowersToArray(ret_ty, target)) ret_val: { | 3258 | try reap(f, inst, &.{un_op}); |
| 3185 | const array_local = try f.allocLocal(lowered_ret_ty, .Mut); | 3259 | const is_array = lowersToArray(ret_ty, target); |
| 3186 | try writer.writeAll(";\n"); | 3260 | const ret_val = if (is_array) ret_val: { |
| | 3261 | const array_local = try f.allocLocal(inst, try lowered_ret_ty.copy(f.arena.allocator())); |
| 3187 | try writer.writeAll("memcpy("); | 3262 | try writer.writeAll("memcpy("); |
| 3188 | try f.writeCValueMember(writer, array_local, .{ .field = 0 }); | 3263 | try f.writeCValueMember(writer, array_local, .{ .field = 0 }); |
| 3189 | try writer.writeAll(", "); | 3264 | try writer.writeAll(", "); |
| ... | @@ -3204,23 +3279,33 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { | ... | @@ -3204,23 +3279,33 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3204 | else | 3279 | else |
| 3205 | try f.writeCValue(writer, ret_val, .Other); | 3280 | try f.writeCValue(writer, ret_val, .Other); |
| 3206 | try writer.writeAll(";\n"); | 3281 | try writer.writeAll(";\n"); |
| 3207 | } else if (f.object.dg.decl.ty.fnCallingConvention() != .Naked) { | 3282 | if (is_array) { |
| 3208 | // Not even allowed to return void in a naked function. | 3283 | try freeLocal(f, inst, ret_val.local, 0); |
| 3209 | try writer.writeAll("return;\n"); | 3284 | } |
| | 3285 | } else { |
| | 3286 | try reap(f, inst, &.{un_op}); |
| | 3287 | if (f.object.dg.decl.ty.fnCallingConvention() != .Naked) { |
| | 3288 | // Not even allowed to return void in a naked function. |
| | 3289 | try writer.writeAll("return;\n"); |
| | 3290 | } |
| 3210 | } | 3291 | } |
| 3211 | return CValue.none; | 3292 | return CValue.none; |
| 3212 | } | 3293 | } |
| 3213 | | 3294 | |
| 3214 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { | 3295 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3215 | if (f.liveness.isUnused(inst)) | 3296 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 3297 | |
| | 3298 | if (f.liveness.isUnused(inst)) { |
| | 3299 | try reap(f, inst, &.{ty_op.operand}); |
| 3216 | return CValue.none; | 3300 | return CValue.none; |
| | 3301 | } |
| 3217 | | 3302 | |
| 3218 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 3219 | const operand = try f.resolveInst(ty_op.operand); | 3303 | const operand = try f.resolveInst(ty_op.operand); |
| 3220 | | 3304 | try reap(f, inst, &.{ty_op.operand}); |
| 3221 | const writer = f.object.writer(); | 3305 | const writer = f.object.writer(); |
| 3222 | const inst_ty = f.air.typeOfIndex(inst); | 3306 | const inst_ty = f.air.typeOfIndex(inst); |
| 3223 | const local = try f.allocLocal(inst_ty, .Const); | 3307 | const local = try f.allocLocal(inst, inst_ty); |
| | 3308 | try f.writeCValue(writer, local, .Other); |
| 3224 | try writer.writeAll(" = ("); | 3309 | try writer.writeAll(" = ("); |
| 3225 | try f.renderTypecast(writer, inst_ty); | 3310 | try f.renderTypecast(writer, inst_ty); |
| 3226 | try writer.writeByte(')'); | 3311 | try writer.writeByte(')'); |
| ... | @@ -3230,17 +3315,22 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3230,17 +3315,22 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3230 | } | 3315 | } |
| 3231 | | 3316 | |
| 3232 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | 3317 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3233 | if (f.liveness.isUnused(inst)) return CValue.none; | 3318 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 3319 | if (f.liveness.isUnused(inst)) { |
| | 3320 | try reap(f, inst, &.{ty_op.operand}); |
| | 3321 | return CValue.none; |
| | 3322 | } |
| 3234 | | 3323 | |
| | 3324 | const operand = try f.resolveInst(ty_op.operand); |
| | 3325 | try reap(f, inst, &.{ty_op.operand}); |
| 3235 | const inst_ty = f.air.typeOfIndex(inst); | 3326 | const inst_ty = f.air.typeOfIndex(inst); |
| 3236 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 3237 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 3238 | const writer = f.object.writer(); | 3327 | const writer = f.object.writer(); |
| 3239 | const operand = try f.resolveInst(ty_op.operand); | 3328 | const local = try f.allocLocal(inst, inst_ty); |
| 3240 | const target = f.object.dg.module.getTarget(); | 3329 | const target = f.object.dg.module.getTarget(); |
| 3241 | const dest_int_info = inst_ty.intInfo(target); | 3330 | const dest_int_info = inst_ty.intInfo(target); |
| 3242 | const dest_bits = dest_int_info.bits; | 3331 | const dest_bits = dest_int_info.bits; |
| 3243 | | 3332 | |
| | 3333 | try f.writeCValue(writer, local, .Other); |
| 3244 | try writer.writeAll(" = ("); | 3334 | try writer.writeAll(" = ("); |
| 3245 | try f.renderTypecast(writer, inst_ty); | 3335 | try f.renderTypecast(writer, inst_ty); |
| 3246 | try writer.writeByte(')'); | 3336 | try writer.writeByte(')'); |
| ... | @@ -3282,20 +3372,24 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3282,20 +3372,24 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3282 | } | 3372 | } |
| 3283 | | 3373 | |
| 3284 | fn airBoolToInt(f: *Function, inst: Air.Inst.Index) !CValue { | 3374 | fn airBoolToInt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3285 | if (f.liveness.isUnused(inst)) | | |
| 3286 | return CValue.none; | | |
| 3287 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 3375 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 3376 | if (f.liveness.isUnused(inst)) { |
| | 3377 | try reap(f, inst, &.{un_op}); |
| | 3378 | return CValue.none; |
| | 3379 | } |
| | 3380 | const operand = try f.resolveInst(un_op); |
| | 3381 | try reap(f, inst, &.{un_op}); |
| 3288 | const writer = f.object.writer(); | 3382 | const writer = f.object.writer(); |
| 3289 | const inst_ty = f.air.typeOfIndex(inst); | 3383 | const inst_ty = f.air.typeOfIndex(inst); |
| 3290 | const operand = try f.resolveInst(un_op); | 3384 | const local = try f.allocLocal(inst, inst_ty); |
| 3291 | const local = try f.allocLocal(inst_ty, .Const); | 3385 | try f.writeCValue(writer, local, .Other); |
| 3292 | try writer.writeAll(" = "); | 3386 | try writer.writeAll(" = "); |
| 3293 | try f.writeCValue(writer, operand, .Other); | 3387 | try f.writeCValue(writer, operand, .Other); |
| 3294 | try writer.writeAll(";\n"); | 3388 | try writer.writeAll(";\n"); |
| 3295 | return local; | 3389 | return local; |
| 3296 | } | 3390 | } |
| 3297 | | 3391 | |
| 3298 | fn airStoreUndefined(f: *Function, lhs_child_ty: Type, dest_ptr: CValue) !CValue { | 3392 | fn storeUndefined(f: *Function, lhs_child_ty: Type, dest_ptr: CValue) !CValue { |
| 3299 | if (f.wantSafety()) { | 3393 | if (f.wantSafety()) { |
| 3300 | const writer = f.object.writer(); | 3394 | const writer = f.object.writer(); |
| 3301 | try writer.writeAll("memset("); | 3395 | try writer.writeAll("memset("); |
| ... | @@ -3311,18 +3405,23 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3311,18 +3405,23 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3311 | // *a = b; | 3405 | // *a = b; |
| 3312 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3406 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3313 | const ptr_info = f.air.typeOf(bin_op.lhs).ptrInfo().data; | 3407 | const ptr_info = f.air.typeOf(bin_op.lhs).ptrInfo().data; |
| 3314 | if (!ptr_info.pointee_type.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 3408 | if (!ptr_info.pointee_type.hasRuntimeBitsIgnoreComptime()) { |
| | 3409 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3410 | return CValue.none; |
| | 3411 | } |
| 3315 | | 3412 | |
| 3316 | const ptr_val = try f.resolveInst(bin_op.lhs); | 3413 | const ptr_val = try f.resolveInst(bin_op.lhs); |
| 3317 | const src_ty = f.air.typeOf(bin_op.rhs); | 3414 | const src_ty = f.air.typeOf(bin_op.rhs); |
| 3318 | const src_val = try f.resolveInst(bin_op.rhs); | 3415 | const src_val = try f.resolveInst(bin_op.rhs); |
| 3319 | | 3416 | |
| | 3417 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3418 | |
| 3320 | // TODO Sema should emit a different instruction when the store should | 3419 | // TODO Sema should emit a different instruction when the store should |
| 3321 | // possibly do the safety 0xaa bytes for undefined. | 3420 | // possibly do the safety 0xaa bytes for undefined. |
| 3322 | const src_val_is_undefined = | 3421 | const src_val_is_undefined = |
| 3323 | if (f.air.value(bin_op.rhs)) |v| v.isUndefDeep() else false; | 3422 | if (f.air.value(bin_op.rhs)) |v| v.isUndefDeep() else false; |
| 3324 | if (src_val_is_undefined) | 3423 | if (src_val_is_undefined) |
| 3325 | return try airStoreUndefined(f, ptr_info.pointee_type, ptr_val); | 3424 | return try storeUndefined(f, ptr_info.pointee_type, ptr_val); |
| 3326 | | 3425 | |
| 3327 | const target = f.object.dg.module.getTarget(); | 3426 | const target = f.object.dg.module.getTarget(); |
| 3328 | const is_aligned = ptr_info.@"align" == 0 or | 3427 | const is_aligned = ptr_info.@"align" == 0 or |
| ... | @@ -3340,7 +3439,8 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3340,7 +3439,8 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3340 | // so work around this by initializing into new local. | 3439 | // so work around this by initializing into new local. |
| 3341 | // TODO this should be done by manually initializing elements of the dest array | 3440 | // TODO this should be done by manually initializing elements of the dest array |
| 3342 | const array_src = if (src_val == .constant) blk: { | 3441 | const array_src = if (src_val == .constant) blk: { |
| 3343 | const new_local = try f.allocLocal(src_ty, .Const); | 3442 | const new_local = try f.allocLocal(inst, src_ty); |
| | 3443 | try f.writeCValue(writer, new_local, .Other); |
| 3344 | try writer.writeAll(" = "); | 3444 | try writer.writeAll(" = "); |
| 3345 | try f.writeCValue(writer, src_val, .Initializer); | 3445 | try f.writeCValue(writer, src_val, .Initializer); |
| 3346 | try writer.writeAll(";\n"); | 3446 | try writer.writeAll(";\n"); |
| ... | @@ -3356,6 +3456,9 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3356,6 +3456,9 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3356 | try writer.writeAll(", sizeof("); | 3456 | try writer.writeAll(", sizeof("); |
| 3357 | try f.renderTypecast(writer, src_ty); | 3457 | try f.renderTypecast(writer, src_ty); |
| 3358 | try writer.writeAll("))"); | 3458 | try writer.writeAll("))"); |
| | 3459 | if (src_val == .constant) { |
| | 3460 | try freeLocal(f, inst, array_src.local, 0); |
| | 3461 | } |
| 3359 | } else if (ptr_info.host_size != 0) { | 3462 | } else if (ptr_info.host_size != 0) { |
| 3360 | const host_bits = ptr_info.host_size * 8; | 3463 | const host_bits = ptr_info.host_size * 8; |
| 3361 | var host_pl = Type.Payload.Bits{ .base = .{ .tag = .int_unsigned }, .data = host_bits }; | 3464 | var host_pl = Type.Payload.Bits{ .base = .{ .tag = .int_unsigned }, .data = host_bits }; |
| ... | @@ -3421,22 +3524,24 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3421,22 +3524,24 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3421 | } | 3524 | } |
| 3422 | | 3525 | |
| 3423 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { | 3526 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { |
| 3424 | if (f.liveness.isUnused(inst)) | | |
| 3425 | return CValue.none; | | |
| 3426 | | | |
| 3427 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 3527 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3428 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3528 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3429 | | 3529 | |
| | 3530 | if (f.liveness.isUnused(inst)) { |
| | 3531 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3532 | return CValue.none; |
| | 3533 | } |
| | 3534 | |
| 3430 | const lhs = try f.resolveInst(bin_op.lhs); | 3535 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3431 | const rhs = try f.resolveInst(bin_op.rhs); | 3536 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 3537 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3432 | | 3538 | |
| 3433 | const inst_ty = f.air.typeOfIndex(inst); | 3539 | const inst_ty = f.air.typeOfIndex(inst); |
| 3434 | const vector_ty = f.air.typeOf(bin_op.lhs); | 3540 | const vector_ty = f.air.typeOf(bin_op.lhs); |
| 3435 | const scalar_ty = vector_ty.scalarType(); | 3541 | const scalar_ty = vector_ty.scalarType(); |
| 3436 | const w = f.object.writer(); | 3542 | const w = f.object.writer(); |
| 3437 | | 3543 | |
| 3438 | const local = try f.allocLocal(inst_ty, .Mut); | 3544 | const local = try f.allocLocal(inst, inst_ty); |
| 3439 | try w.writeAll(";\n"); | | |
| 3440 | | 3545 | |
| 3441 | switch (vector_ty.zigTypeTag()) { | 3546 | switch (vector_ty.zigTypeTag()) { |
| 3442 | .Vector => { | 3547 | .Vector => { |
| ... | @@ -3471,21 +3576,27 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: | ... | @@ -3471,21 +3576,27 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 3471 | return local; | 3576 | return local; |
| 3472 | } | 3577 | } |
| 3473 | | 3578 | |
| 3474 | fn airNot(f: *Function, inst: Air.Inst.Index) !void { | 3579 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3475 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 3580 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 3581 | |
| | 3582 | if (f.liveness.isUnused(inst)) { |
| | 3583 | try reap(f, inst, &.{ty_op.operand}); |
| | 3584 | return CValue.none; |
| | 3585 | } |
| | 3586 | |
| 3476 | const op = try f.resolveInst(ty_op.operand); | 3587 | const op = try f.resolveInst(ty_op.operand); |
| | 3588 | try reap(f, inst, &.{ty_op.operand}); |
| 3477 | | 3589 | |
| 3478 | const writer = f.object.writer(); | 3590 | const writer = f.object.writer(); |
| 3479 | const inst_ty = f.air.typeOfIndex(inst); | 3591 | const inst_ty = f.air.typeOfIndex(inst); |
| 3480 | | 3592 | const local = try f.allocLocal(inst, inst_ty); |
| 3481 | const target = f.object.dg.module.getTarget(); | 3593 | try f.writeCValue(writer, local, .Other); |
| 3482 | if (inst_ty.bitSize(target) > 64) {} | 3594 | try writer.writeAll(" = "); |
| 3483 | | | |
| 3484 | try writer.writeByte('('); | | |
| 3485 | try f.renderTypecast(writer, inst_ty); | | |
| 3486 | try writer.writeByte(')'); | | |
| 3487 | try writer.writeByte(if (inst_ty.tag() == .bool) '!' else '~'); | 3595 | try writer.writeByte(if (inst_ty.tag() == .bool) '!' else '~'); |
| 3488 | try f.writeCValue(writer, op, .Other); | 3596 | try f.writeCValue(writer, op, .Other); |
| | 3597 | try writer.writeAll(";\n"); |
| | 3598 | |
| | 3599 | return local; |
| 3489 | } | 3600 | } |
| 3490 | | 3601 | |
| 3491 | fn airBinOp( | 3602 | fn airBinOp( |
| ... | @@ -3494,54 +3605,68 @@ fn airBinOp( | ... | @@ -3494,54 +3605,68 @@ fn airBinOp( |
| 3494 | operator: []const u8, | 3605 | operator: []const u8, |
| 3495 | operation: []const u8, | 3606 | operation: []const u8, |
| 3496 | info: BuiltinInfo, | 3607 | info: BuiltinInfo, |
| 3497 | ) !void { | 3608 | ) !CValue { |
| 3498 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3609 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3499 | | | |
| 3500 | const operand_ty = f.air.typeOf(bin_op.lhs); | 3610 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3501 | const target = f.object.dg.module.getTarget(); | 3611 | const target = f.object.dg.module.getTarget(); |
| 3502 | if ((operand_ty.isInt() and operand_ty.bitSize(target) > 64) or operand_ty.isRuntimeFloat()) | 3612 | if ((operand_ty.isInt() and operand_ty.bitSize(target) > 64) or operand_ty.isRuntimeFloat()) |
| 3503 | return airBinBuiltinCall(f, inst, operation, info); | 3613 | return try airBinBuiltinCall(f, inst, operation, info); |
| 3504 | | 3614 | |
| 3505 | const inst_ty = f.air.typeOfIndex(inst); | | |
| 3506 | const lhs = try f.resolveInst(bin_op.lhs); | 3615 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3507 | const rhs = try f.resolveInst(bin_op.rhs); | 3616 | const rhs = try f.resolveInst(bin_op.rhs); |
| 3508 | | 3617 | |
| | 3618 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3619 | |
| | 3620 | if (f.liveness.isUnused(inst)) return CValue.none; |
| | 3621 | |
| | 3622 | const inst_ty = f.air.typeOfIndex(inst); |
| | 3623 | |
| 3509 | const writer = f.object.writer(); | 3624 | const writer = f.object.writer(); |
| 3510 | try writer.writeByte('('); | 3625 | const local = try f.allocLocal(inst, inst_ty); |
| 3511 | try f.renderTypecast(writer, inst_ty); | 3626 | try f.writeCValue(writer, local, .Other); |
| 3512 | try writer.writeAll(")("); | 3627 | try writer.writeAll(" = "); |
| 3513 | try f.writeCValue(writer, lhs, .Other); | 3628 | try f.writeCValue(writer, lhs, .Other); |
| 3514 | try writer.writeByte(' '); | 3629 | try writer.writeByte(' '); |
| 3515 | try writer.writeAll(operator); | 3630 | try writer.writeAll(operator); |
| 3516 | try writer.writeByte(' '); | 3631 | try writer.writeByte(' '); |
| 3517 | try f.writeCValue(writer, rhs, .Other); | 3632 | try f.writeCValue(writer, rhs, .Other); |
| 3518 | try writer.writeByte(')'); | 3633 | try writer.writeAll(";\n"); |
| | 3634 | |
| | 3635 | return local; |
| 3519 | } | 3636 | } |
| 3520 | | 3637 | |
| 3521 | fn airCmpOp( | 3638 | fn airCmpOp(f: *Function, inst: Air.Inst.Index, operator: []const u8, operation: []const u8) !CValue { |
| 3522 | f: *Function, | | |
| 3523 | inst: Air.Inst.Index, | | |
| 3524 | operator: []const u8, | | |
| 3525 | operation: []const u8, | | |
| 3526 | ) !void { | | |
| 3527 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3639 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3528 | | 3640 | |
| | 3641 | if (f.liveness.isUnused(inst)) { |
| | 3642 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3643 | return CValue.none; |
| | 3644 | } |
| | 3645 | |
| 3529 | const operand_ty = f.air.typeOf(bin_op.lhs); | 3646 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3530 | const target = f.object.dg.module.getTarget(); | 3647 | const target = f.object.dg.module.getTarget(); |
| 3531 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) | 3648 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) |
| 3532 | return airCmpBuiltinCall(f, inst, operator, "cmp"); | 3649 | return try cmpBuiltinCall(f, inst, operator, "cmp"); |
| 3533 | if (operand_ty.isRuntimeFloat()) | 3650 | if (operand_ty.isRuntimeFloat()) |
| 3534 | return airCmpBuiltinCall(f, inst, operator, operation); | 3651 | return try cmpBuiltinCall(f, inst, operator, operation); |
| 3535 | | 3652 | |
| | 3653 | const inst_ty = f.air.typeOfIndex(inst); |
| 3536 | const lhs = try f.resolveInst(bin_op.lhs); | 3654 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3537 | const rhs = try f.resolveInst(bin_op.rhs); | 3655 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 3656 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3538 | | 3657 | |
| 3539 | const writer = f.object.writer(); | 3658 | const writer = f.object.writer(); |
| | 3659 | const local = try f.allocLocal(inst, inst_ty); |
| | 3660 | try f.writeCValue(writer, local, .Other); |
| | 3661 | try writer.writeAll(" = "); |
| 3540 | try f.writeCValue(writer, lhs, .Other); | 3662 | try f.writeCValue(writer, lhs, .Other); |
| 3541 | try writer.writeByte(' '); | 3663 | try writer.writeByte(' '); |
| 3542 | try writer.writeAll(operator); | 3664 | try writer.writeAll(operator); |
| 3543 | try writer.writeByte(' '); | 3665 | try writer.writeByte(' '); |
| 3544 | try f.writeCValue(writer, rhs, .Other); | 3666 | try f.writeCValue(writer, rhs, .Other); |
| | 3667 | try writer.writeAll(";\n"); |
| | 3668 | |
| | 3669 | return local; |
| 3545 | } | 3670 | } |
| 3546 | | 3671 | |
| 3547 | fn airEquality( | 3672 | fn airEquality( |
| ... | @@ -3550,20 +3675,31 @@ fn airEquality( | ... | @@ -3550,20 +3675,31 @@ fn airEquality( |
| 3550 | negate_prefix: []const u8, | 3675 | negate_prefix: []const u8, |
| 3551 | operator: []const u8, | 3676 | operator: []const u8, |
| 3552 | operation: []const u8, | 3677 | operation: []const u8, |
| 3553 | ) !void { | 3678 | ) !CValue { |
| 3554 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3679 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3555 | | 3680 | |
| | 3681 | if (f.liveness.isUnused(inst)) { |
| | 3682 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3683 | return CValue.none; |
| | 3684 | } |
| | 3685 | |
| 3556 | const operand_ty = f.air.typeOf(bin_op.lhs); | 3686 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3557 | const target = f.object.dg.module.getTarget(); | 3687 | const target = f.object.dg.module.getTarget(); |
| 3558 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) | 3688 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) |
| 3559 | return airCmpBuiltinCall(f, inst, operator, "cmp"); | 3689 | return try cmpBuiltinCall(f, inst, operator, "cmp"); |
| 3560 | if (operand_ty.isRuntimeFloat()) | 3690 | if (operand_ty.isRuntimeFloat()) |
| 3561 | return airCmpBuiltinCall(f, inst, operator, operation); | 3691 | return try cmpBuiltinCall(f, inst, operator, operation); |
| 3562 | | 3692 | |
| 3563 | const lhs = try f.resolveInst(bin_op.lhs); | 3693 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3564 | const rhs = try f.resolveInst(bin_op.rhs); | 3694 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 3695 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3565 | | 3696 | |
| 3566 | const writer = f.object.writer(); | 3697 | const writer = f.object.writer(); |
| | 3698 | const inst_ty = f.air.typeOfIndex(inst); |
| | 3699 | const local = try f.allocLocal(inst, inst_ty); |
| | 3700 | try f.writeCValue(writer, local, .Other); |
| | 3701 | try writer.writeAll(" = "); |
| | 3702 | |
| 3567 | if (operand_ty.zigTypeTag() == .Optional and !operand_ty.isPtrLikeOptional()) { | 3703 | if (operand_ty.zigTypeTag() == .Optional and !operand_ty.isPtrLikeOptional()) { |
| 3568 | // (A && B) || (C && (A == B)) | 3704 | // (A && B) || (C && (A == B)) |
| 3569 | // A = lhs.is_null ; B = rhs.is_null ; C = rhs.payload == lhs.payload | 3705 | // A = lhs.is_null ; B = rhs.is_null ; C = rhs.payload == lhs.payload |
| ... | @@ -3580,8 +3716,9 @@ fn airEquality( | ... | @@ -3580,8 +3716,9 @@ fn airEquality( |
| 3580 | try f.writeCValue(writer, lhs, .Other); | 3716 | try f.writeCValue(writer, lhs, .Other); |
| 3581 | try writer.writeAll(".is_null == "); | 3717 | try writer.writeAll(".is_null == "); |
| 3582 | try f.writeCValue(writer, rhs, .Other); | 3718 | try f.writeCValue(writer, rhs, .Other); |
| 3583 | try writer.writeAll(".is_null))"); | 3719 | try writer.writeAll(".is_null));\n"); |
| 3584 | return; | 3720 | |
| | 3721 | return local; |
| 3585 | } | 3722 | } |
| 3586 | | 3723 | |
| 3587 | try f.writeCValue(writer, lhs, .Other); | 3724 | try f.writeCValue(writer, lhs, .Other); |
| ... | @@ -3589,17 +3726,26 @@ fn airEquality( | ... | @@ -3589,17 +3726,26 @@ fn airEquality( |
| 3589 | try writer.writeAll(operator); | 3726 | try writer.writeAll(operator); |
| 3590 | try writer.writeByte(' '); | 3727 | try writer.writeByte(' '); |
| 3591 | try f.writeCValue(writer, rhs, .Other); | 3728 | try f.writeCValue(writer, rhs, .Other); |
| | 3729 | try writer.writeAll(";\n"); |
| | 3730 | |
| | 3731 | return local; |
| 3592 | } | 3732 | } |
| 3593 | | 3733 | |
| 3594 | fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { | 3734 | fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3595 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 3596 | | | |
| 3597 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 3735 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 3736 | |
| | 3737 | if (f.liveness.isUnused(inst)) { |
| | 3738 | try reap(f, inst, &.{un_op}); |
| | 3739 | return CValue.none; |
| | 3740 | } |
| | 3741 | |
| 3598 | const inst_ty = f.air.typeOfIndex(inst); | 3742 | const inst_ty = f.air.typeOfIndex(inst); |
| 3599 | const operand = try f.resolveInst(un_op); | 3743 | const operand = try f.resolveInst(un_op); |
| | 3744 | try reap(f, inst, &.{un_op}); |
| 3600 | | 3745 | |
| 3601 | const writer = f.object.writer(); | 3746 | const writer = f.object.writer(); |
| 3602 | const local = try f.allocLocal(inst_ty, .Const); | 3747 | const local = try f.allocLocal(inst, inst_ty); |
| | 3748 | try f.writeCValue(writer, local, .Other); |
| 3603 | try writer.writeAll(" = "); | 3749 | try writer.writeAll(" = "); |
| 3604 | try f.writeCValue(writer, operand, .Other); | 3750 | try f.writeCValue(writer, operand, .Other); |
| 3605 | try writer.print(" < sizeof({ }) / sizeof(*{0 });\n", .{fmtIdent("zig_errorName")}); | 3751 | try writer.print(" < sizeof({ }) / sizeof(*{0 });\n", .{fmtIdent("zig_errorName")}); |
| ... | @@ -3607,16 +3753,18 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3607,16 +3753,18 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3607 | } | 3753 | } |
| 3608 | | 3754 | |
| 3609 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { | 3755 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 3610 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 3611 | | | |
| 3612 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 3756 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3613 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3757 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| | 3758 | if (f.liveness.isUnused(inst)) { |
| | 3759 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3760 | return CValue.none; |
| | 3761 | } |
| | 3762 | |
| 3614 | const lhs = try f.resolveInst(bin_op.lhs); | 3763 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3615 | const rhs = try f.resolveInst(bin_op.rhs); | 3764 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 3765 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3616 | | 3766 | |
| 3617 | const writer = f.object.writer(); | | |
| 3618 | const inst_ty = f.air.typeOfIndex(inst); | 3767 | const inst_ty = f.air.typeOfIndex(inst); |
| 3619 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 3620 | const elem_ty = switch (inst_ty.ptrSize()) { | 3768 | const elem_ty = switch (inst_ty.ptrSize()) { |
| 3621 | .One => blk: { | 3769 | .One => blk: { |
| 3622 | const array_ty = inst_ty.childType(); | 3770 | const array_ty = inst_ty.childType(); |
| ... | @@ -3625,8 +3773,12 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { | ... | @@ -3625,8 +3773,12 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 3625 | else => inst_ty.childType(), | 3773 | else => inst_ty.childType(), |
| 3626 | }; | 3774 | }; |
| 3627 | | 3775 | |
| 3628 | // We must convert to and from integer types to prevent UB if the operation results in a NULL pointer, | 3776 | // We must convert to and from integer types to prevent UB if the operation |
| 3629 | // or if LHS is NULL. The operation is only UB if the result is NULL and then dereferenced. | 3777 | // results in a NULL pointer, or if LHS is NULL. The operation is only UB |
| | 3778 | // if the result is NULL and then dereferenced. |
| | 3779 | const local = try f.allocLocal(inst, inst_ty); |
| | 3780 | const writer = f.object.writer(); |
| | 3781 | try f.writeCValue(writer, local, .Other); |
| 3630 | try writer.writeAll(" = ("); | 3782 | try writer.writeAll(" = ("); |
| 3631 | try f.renderTypecast(writer, inst_ty); | 3783 | try f.renderTypecast(writer, inst_ty); |
| 3632 | try writer.writeAll(")(((uintptr_t)"); | 3784 | try writer.writeAll(")(((uintptr_t)"); |
| ... | @@ -3642,23 +3794,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { | ... | @@ -3642,23 +3794,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 3642 | return local; | 3794 | return local; |
| 3643 | } | 3795 | } |
| 3644 | | 3796 | |
| 3645 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !void { | 3797 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { |
| 3646 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 3798 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3647 | | 3799 | |
| | 3800 | if (f.liveness.isUnused(inst)) { |
| | 3801 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3802 | return CValue.none; |
| | 3803 | } |
| | 3804 | |
| 3648 | const inst_ty = f.air.typeOfIndex(inst); | 3805 | const inst_ty = f.air.typeOfIndex(inst); |
| 3649 | const target = f.object.dg.module.getTarget(); | 3806 | const target = f.object.dg.module.getTarget(); |
| 3650 | if (inst_ty.isInt() and inst_ty.bitSize(target) > 64) | 3807 | if (inst_ty.isInt() and inst_ty.bitSize(target) > 64) |
| 3651 | return airBinBuiltinCall(f, inst, operation[1..], .None); | 3808 | return try airBinBuiltinCall(f, inst, operation[1..], .None); |
| 3652 | if (inst_ty.isRuntimeFloat()) | 3809 | if (inst_ty.isRuntimeFloat()) |
| 3653 | return airBinFloatOp(f, inst, operation); | 3810 | return try airBinFloatOp(f, inst, operation); |
| 3654 | | 3811 | |
| 3655 | const lhs = try f.resolveInst(bin_op.lhs); | 3812 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3656 | const rhs = try f.resolveInst(bin_op.rhs); | 3813 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 3814 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3657 | | 3815 | |
| 3658 | const writer = f.object.writer(); | 3816 | const writer = f.object.writer(); |
| 3659 | | 3817 | const local = try f.allocLocal(inst, inst_ty); |
| | 3818 | try f.writeCValue(writer, local, .Other); |
| 3660 | // (lhs <> rhs) ? lhs : rhs | 3819 | // (lhs <> rhs) ? lhs : rhs |
| 3661 | try writer.writeAll("("); | 3820 | try writer.writeAll(" = ("); |
| 3662 | try f.writeCValue(writer, lhs, .Other); | 3821 | try f.writeCValue(writer, lhs, .Other); |
| 3663 | try writer.writeByte(' '); | 3822 | try writer.writeByte(' '); |
| 3664 | try writer.writeByte(operator); | 3823 | try writer.writeByte(operator); |
| ... | @@ -3668,28 +3827,38 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons | ... | @@ -3668,28 +3827,38 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 3668 | try f.writeCValue(writer, lhs, .Other); | 3827 | try f.writeCValue(writer, lhs, .Other); |
| 3669 | try writer.writeAll(" : "); | 3828 | try writer.writeAll(" : "); |
| 3670 | try f.writeCValue(writer, rhs, .Other); | 3829 | try f.writeCValue(writer, rhs, .Other); |
| | 3830 | try writer.writeAll(";\n"); |
| | 3831 | |
| | 3832 | return local; |
| 3671 | } | 3833 | } |
| 3672 | | 3834 | |
| 3673 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { | 3835 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3674 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 3675 | | | |
| 3676 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 3836 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3677 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; | 3837 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| | 3838 | |
| | 3839 | if (f.liveness.isUnused(inst)) { |
| | 3840 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 3841 | return CValue.none; |
| | 3842 | } |
| | 3843 | |
| 3678 | const ptr = try f.resolveInst(bin_op.lhs); | 3844 | const ptr = try f.resolveInst(bin_op.lhs); |
| 3679 | const len = try f.resolveInst(bin_op.rhs); | 3845 | const len = try f.resolveInst(bin_op.rhs); |
| | 3846 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3680 | | 3847 | |
| 3681 | const writer = f.object.writer(); | 3848 | const writer = f.object.writer(); |
| 3682 | const inst_ty = f.air.typeOfIndex(inst); | 3849 | const inst_ty = f.air.typeOfIndex(inst); |
| 3683 | const local = try f.allocLocal(inst_ty, .Const); | 3850 | const local = try f.allocLocal(inst, inst_ty); |
| 3684 | | 3851 | try f.writeCValue(writer, local, .Other); |
| 3685 | try writer.writeAll(" = {("); | 3852 | try writer.writeAll(".ptr = ("); |
| 3686 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; | 3853 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 3687 | try f.renderTypecast(writer, inst_ty.slicePtrFieldType(&buf)); | 3854 | try f.renderTypecast(writer, inst_ty.slicePtrFieldType(&buf)); |
| 3688 | try writer.writeByte(')'); | 3855 | try writer.writeByte(')'); |
| 3689 | try f.writeCValue(writer, ptr, .Other); | 3856 | try f.writeCValue(writer, ptr, .Other); |
| 3690 | try writer.writeAll(", "); | 3857 | try writer.writeAll("; "); |
| | 3858 | try f.writeCValue(writer, local, .Other); |
| | 3859 | try writer.writeAll(".len = "); |
| 3691 | try f.writeCValue(writer, len, .Initializer); | 3860 | try f.writeCValue(writer, len, .Initializer); |
| 3692 | try writer.writeAll("};\n"); | 3861 | try writer.writeAll(";\n"); |
| 3693 | | 3862 | |
| 3694 | return local; | 3863 | return local; |
| 3695 | } | 3864 | } |
| ... | @@ -3701,6 +3870,7 @@ fn airCall( | ... | @@ -3701,6 +3870,7 @@ fn airCall( |
| 3701 | ) !CValue { | 3870 | ) !CValue { |
| 3702 | // Not even allowed to call panic in a naked function. | 3871 | // Not even allowed to call panic in a naked function. |
| 3703 | if (f.object.dg.decl.ty.fnCallingConvention() == .Naked) return .none; | 3872 | if (f.object.dg.decl.ty.fnCallingConvention() == .Naked) return .none; |
| | 3873 | const gpa = f.object.dg.gpa; |
| 3704 | | 3874 | |
| 3705 | switch (modifier) { | 3875 | switch (modifier) { |
| 3706 | .auto => {}, | 3876 | .auto => {}, |
| ... | @@ -3712,6 +3882,21 @@ fn airCall( | ... | @@ -3712,6 +3882,21 @@ fn airCall( |
| 3712 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; | 3882 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 3713 | const extra = f.air.extraData(Air.Call, pl_op.payload); | 3883 | const extra = f.air.extraData(Air.Call, pl_op.payload); |
| 3714 | const args = @ptrCast([]const Air.Inst.Ref, f.air.extra[extra.end..][0..extra.data.args_len]); | 3884 | const args = @ptrCast([]const Air.Inst.Ref, f.air.extra[extra.end..][0..extra.data.args_len]); |
| | 3885 | |
| | 3886 | const resolved_args = try gpa.alloc(CValue, args.len); |
| | 3887 | defer gpa.free(resolved_args); |
| | 3888 | for (args) |arg, i| { |
| | 3889 | resolved_args[i] = try f.resolveInst(arg); |
| | 3890 | } |
| | 3891 | |
| | 3892 | const callee = try f.resolveInst(pl_op.operand); |
| | 3893 | |
| | 3894 | { |
| | 3895 | var bt = iterateBigTomb(f, inst); |
| | 3896 | try bt.feed(pl_op.operand); |
| | 3897 | for (args) |arg| try bt.feed(arg); |
| | 3898 | } |
| | 3899 | |
| 3715 | const callee_ty = f.air.typeOf(pl_op.operand); | 3900 | const callee_ty = f.air.typeOf(pl_op.operand); |
| 3716 | const fn_ty = switch (callee_ty.zigTypeTag()) { | 3901 | const fn_ty = switch (callee_ty.zigTypeTag()) { |
| 3717 | .Fn => callee_ty, | 3902 | .Fn => callee_ty, |
| ... | @@ -3733,7 +3918,8 @@ fn airCall( | ... | @@ -3733,7 +3918,8 @@ fn airCall( |
| 3733 | try writer.writeByte(')'); | 3918 | try writer.writeByte(')'); |
| 3734 | break :r .none; | 3919 | break :r .none; |
| 3735 | } else r: { | 3920 | } else r: { |
| 3736 | const local = try f.allocLocal(lowered_ret_ty, .Const); | 3921 | const local = try f.allocLocal(inst, try lowered_ret_ty.copy(f.arena.allocator())); |
| | 3922 | try f.writeCValue(writer, local, .Other); |
| 3737 | try writer.writeAll(" = "); | 3923 | try writer.writeAll(" = "); |
| 3738 | break :r local; | 3924 | break :r local; |
| 3739 | }; | 3925 | }; |
| ... | @@ -3759,13 +3945,12 @@ fn airCall( | ... | @@ -3759,13 +3945,12 @@ fn airCall( |
| 3759 | break :callee; | 3945 | break :callee; |
| 3760 | } | 3946 | } |
| 3761 | // Fall back to function pointer call. | 3947 | // Fall back to function pointer call. |
| 3762 | const callee = try f.resolveInst(pl_op.operand); | | |
| 3763 | try f.writeCValue(writer, callee, .Other); | 3948 | try f.writeCValue(writer, callee, .Other); |
| 3764 | } | 3949 | } |
| 3765 | | 3950 | |
| 3766 | try writer.writeByte('('); | 3951 | try writer.writeByte('('); |
| 3767 | var args_written: usize = 0; | 3952 | var args_written: usize = 0; |
| 3768 | for (args) |arg| { | 3953 | for (args) |arg, arg_i| { |
| 3769 | const ty = f.air.typeOf(arg); | 3954 | const ty = f.air.typeOf(arg); |
| 3770 | if (!ty.hasRuntimeBitsIgnoreComptime()) continue; | 3955 | if (!ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 3771 | if (args_written != 0) { | 3956 | if (args_written != 0) { |
| ... | @@ -3780,23 +3965,28 @@ fn airCall( | ... | @@ -3780,23 +3965,28 @@ fn airCall( |
| 3780 | if (ty.isVolatilePtr()) try writer.writeAll(" volatile"); | 3965 | if (ty.isVolatilePtr()) try writer.writeAll(" volatile"); |
| 3781 | try writer.writeAll(" *)"); | 3966 | try writer.writeAll(" *)"); |
| 3782 | } | 3967 | } |
| 3783 | try f.writeCValue(writer, try f.resolveInst(arg), .FunctionArgument); | 3968 | try f.writeCValue(writer, resolved_args[arg_i], .FunctionArgument); |
| 3784 | args_written += 1; | 3969 | args_written += 1; |
| 3785 | } | 3970 | } |
| 3786 | try writer.writeAll(");\n"); | 3971 | try writer.writeAll(");\n"); |
| 3787 | | 3972 | |
| 3788 | if (result_local == .none or !lowersToArray(ret_ty, target)) return result_local; | 3973 | const result = r: { |
| | 3974 | if (result_local == .none or !lowersToArray(ret_ty, target)) |
| | 3975 | break :r result_local; |
| 3789 | | 3976 | |
| 3790 | const array_local = try f.allocLocal(ret_ty, .Mut); | 3977 | const array_local = try f.allocLocal(inst, ret_ty); |
| 3791 | try writer.writeAll(";\n"); | 3978 | try writer.writeAll("memcpy("); |
| 3792 | try writer.writeAll("memcpy("); | 3979 | try f.writeCValue(writer, array_local, .FunctionArgument); |
| 3793 | try f.writeCValue(writer, array_local, .FunctionArgument); | 3980 | try writer.writeAll(", "); |
| 3794 | try writer.writeAll(", "); | 3981 | try f.writeCValueMember(writer, result_local, .{ .field = 0 }); |
| 3795 | try f.writeCValueMember(writer, result_local, .{ .field = 0 }); | 3982 | try writer.writeAll(", sizeof("); |
| 3796 | try writer.writeAll(", sizeof("); | 3983 | try f.renderTypecast(writer, ret_ty); |
| 3797 | try f.renderTypecast(writer, ret_ty); | 3984 | try writer.writeAll("));\n"); |
| 3798 | try writer.writeAll("));\n"); | 3985 | try freeLocal(f, inst, result_local.local, 0); |
| 3799 | return array_local; | 3986 | break :r array_local; |
| | 3987 | }; |
| | 3988 | |
| | 3989 | return result; |
| 3800 | } | 3990 | } |
| 3801 | | 3991 | |
| 3802 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { | 3992 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | @@ -3828,6 +4018,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3828,6 +4018,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3828 | const name = f.air.nullTerminatedString(pl_op.payload); | 4018 | const name = f.air.nullTerminatedString(pl_op.payload); |
| 3829 | const operand = try f.resolveInst(pl_op.operand); | 4019 | const operand = try f.resolveInst(pl_op.operand); |
| 3830 | _ = operand; | 4020 | _ = operand; |
| | 4021 | try reap(f, inst, &.{pl_op.operand}); |
| 3831 | const writer = f.object.writer(); | 4022 | const writer = f.object.writer(); |
| 3832 | try writer.print("/* var:{s} */\n", .{name}); | 4023 | try writer.print("/* var:{s} */\n", .{name}); |
| 3833 | return CValue.none; | 4024 | return CValue.none; |
| ... | @@ -3843,12 +4034,10 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3843,12 +4034,10 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3843 | const writer = f.object.writer(); | 4034 | const writer = f.object.writer(); |
| 3844 | | 4035 | |
| 3845 | const inst_ty = f.air.typeOfIndex(inst); | 4036 | const inst_ty = f.air.typeOfIndex(inst); |
| 3846 | const result = if (inst_ty.tag() != .void and !f.liveness.isUnused(inst)) blk: { | 4037 | const result = if (inst_ty.tag() != .void and !f.liveness.isUnused(inst)) |
| 3847 | // allocate a location for the result | 4038 | try f.allocLocal(inst, inst_ty) |
| 3848 | const local = try f.allocLocal(inst_ty, .Mut); | 4039 | else |
| 3849 | try writer.writeAll(";\n"); | 4040 | CValue{ .none = {} }; |
| 3850 | break :blk local; | | |
| 3851 | } else CValue{ .none = {} }; | | |
| 3852 | | 4041 | |
| 3853 | try f.blocks.putNoClobber(f.object.dg.gpa, inst, .{ | 4042 | try f.blocks.putNoClobber(f.object.dg.gpa, inst, .{ |
| 3854 | .block_id = block_id, | 4043 | .block_id = block_id, |
| ... | @@ -3864,32 +4053,30 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3864,32 +4053,30 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3864 | | 4053 | |
| 3865 | fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { | 4054 | fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3866 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; | 4055 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 3867 | const err_union = try f.resolveInst(pl_op.operand); | | |
| 3868 | const extra = f.air.extraData(Air.Try, pl_op.payload); | 4056 | const extra = f.air.extraData(Air.Try, pl_op.payload); |
| 3869 | const body = f.air.extra[extra.end..][0..extra.data.body_len]; | 4057 | const body = f.air.extra[extra.end..][0..extra.data.body_len]; |
| 3870 | const err_union_ty = f.air.typeOf(pl_op.operand); | 4058 | const err_union_ty = f.air.typeOf(pl_op.operand); |
| 3871 | const result_ty = f.air.typeOfIndex(inst); | 4059 | return lowerTry(f, inst, pl_op.operand, body, err_union_ty, false); |
| 3872 | return lowerTry(f, err_union, body, err_union_ty, false, result_ty); | | |
| 3873 | } | 4060 | } |
| 3874 | | 4061 | |
| 3875 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 4062 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3876 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 4063 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3877 | const extra = f.air.extraData(Air.TryPtr, ty_pl.payload); | 4064 | const extra = f.air.extraData(Air.TryPtr, ty_pl.payload); |
| 3878 | const err_union_ptr = try f.resolveInst(extra.data.ptr); | | |
| 3879 | const body = f.air.extra[extra.end..][0..extra.data.body_len]; | 4065 | const body = f.air.extra[extra.end..][0..extra.data.body_len]; |
| 3880 | const err_union_ty = f.air.typeOf(extra.data.ptr).childType(); | 4066 | const err_union_ty = f.air.typeOf(extra.data.ptr).childType(); |
| 3881 | const result_ty = f.air.typeOfIndex(inst); | 4067 | return lowerTry(f, inst, extra.data.ptr, body, err_union_ty, true); |
| 3882 | return lowerTry(f, err_union_ptr, body, err_union_ty, true, result_ty); | | |
| 3883 | } | 4068 | } |
| 3884 | | 4069 | |
| 3885 | fn lowerTry( | 4070 | fn lowerTry( |
| 3886 | f: *Function, | 4071 | f: *Function, |
| 3887 | err_union: CValue, | 4072 | inst: Air.Inst.Index, |
| | 4073 | operand: Air.Inst.Ref, |
| 3888 | body: []const Air.Inst.Index, | 4074 | body: []const Air.Inst.Index, |
| 3889 | err_union_ty: Type, | 4075 | err_union_ty: Type, |
| 3890 | operand_is_ptr: bool, | 4076 | operand_is_ptr: bool, |
| 3891 | result_ty: Type, | | |
| 3892 | ) !CValue { | 4077 | ) !CValue { |
| | 4078 | const err_union = try f.resolveInst(operand); |
| | 4079 | const result_ty = f.air.typeOfIndex(inst); |
| 3893 | const writer = f.object.writer(); | 4080 | const writer = f.object.writer(); |
| 3894 | const payload_ty = err_union_ty.errorUnionPayload(); | 4081 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 3895 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(); | 4082 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(); |
| ... | @@ -3902,6 +4089,10 @@ fn lowerTry( | ... | @@ -3902,6 +4089,10 @@ fn lowerTry( |
| 3902 | else | 4089 | else |
| 3903 | try f.writeCValue(writer, err_union, .Other); | 4090 | try f.writeCValue(writer, err_union, .Other); |
| 3904 | } else { | 4091 | } else { |
| | 4092 | // Reap the operand so that it can be reused inside genBody. |
| | 4093 | // Remember we must avoid calling reap() twice for the same operand |
| | 4094 | // in this function. |
| | 4095 | try reap(f, inst, &.{operand}); |
| 3905 | if (operand_is_ptr or isByRef(err_union_ty)) | 4096 | if (operand_is_ptr or isByRef(err_union_ty)) |
| 3906 | try f.writeCValueDerefMember(writer, err_union, .{ .identifier = "error" }) | 4097 | try f.writeCValueDerefMember(writer, err_union, .{ .identifier = "error" }) |
| 3907 | else | 4098 | else |
| ... | @@ -3921,11 +4112,16 @@ fn lowerTry( | ... | @@ -3921,11 +4112,16 @@ fn lowerTry( |
| 3921 | } | 4112 | } |
| 3922 | } | 4113 | } |
| 3923 | | 4114 | |
| | 4115 | try reap(f, inst, &.{operand}); |
| | 4116 | |
| | 4117 | if (f.liveness.isUnused(inst)) { |
| | 4118 | return CValue.none; |
| | 4119 | } |
| | 4120 | |
| 3924 | const target = f.object.dg.module.getTarget(); | 4121 | const target = f.object.dg.module.getTarget(); |
| 3925 | const is_array = lowersToArray(payload_ty, target); | 4122 | const is_array = lowersToArray(payload_ty, target); |
| 3926 | const local = try f.allocLocal(result_ty, if (is_array) .Mut else .Const); | 4123 | const local = try f.allocLocal(inst, result_ty); |
| 3927 | if (is_array) { | 4124 | if (is_array) { |
| 3928 | try writer.writeAll(";\n"); | | |
| 3929 | try writer.writeAll("memcpy("); | 4125 | try writer.writeAll("memcpy("); |
| 3930 | try f.writeCValue(writer, local, .FunctionArgument); | 4126 | try f.writeCValue(writer, local, .FunctionArgument); |
| 3931 | try writer.writeAll(", "); | 4127 | try writer.writeAll(", "); |
| ... | @@ -3934,6 +4130,7 @@ fn lowerTry( | ... | @@ -3934,6 +4130,7 @@ fn lowerTry( |
| 3934 | try f.renderTypecast(writer, payload_ty); | 4130 | try f.renderTypecast(writer, payload_ty); |
| 3935 | try writer.writeAll("));\n"); | 4131 | try writer.writeAll("));\n"); |
| 3936 | } else { | 4132 | } else { |
| | 4133 | try f.writeCValue(writer, local, .Other); |
| 3937 | try writer.writeAll(" = "); | 4134 | try writer.writeAll(" = "); |
| 3938 | if (operand_is_ptr or isByRef(payload_ty)) { | 4135 | if (operand_is_ptr or isByRef(payload_ty)) { |
| 3939 | try writer.writeByte('&'); | 4136 | try writer.writeByte('&'); |
| ... | @@ -3953,6 +4150,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3953,6 +4150,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3953 | // If result is .none then the value of the block is unused. | 4150 | // If result is .none then the value of the block is unused. |
| 3954 | if (result != .none) { | 4151 | if (result != .none) { |
| 3955 | const operand = try f.resolveInst(branch.operand); | 4152 | const operand = try f.resolveInst(branch.operand); |
| | 4153 | try reap(f, inst, &.{branch.operand}); |
| 3956 | | 4154 | |
| 3957 | const operand_ty = f.air.typeOf(branch.operand); | 4155 | const operand_ty = f.air.typeOf(branch.operand); |
| 3958 | const target = f.object.dg.module.getTarget(); | 4156 | const target = f.object.dg.module.getTarget(); |
| ... | @@ -3977,40 +4175,50 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -3977,40 +4175,50 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3977 | } | 4175 | } |
| 3978 | | 4176 | |
| 3979 | fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { | 4177 | fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3980 | const src_ty = f.air.typeOfIndex(inst); | 4178 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 4179 | const dest_ty = f.air.typeOfIndex(inst); |
| 3981 | // No IgnoreComptime until Sema stops giving us garbage Air. | 4180 | // No IgnoreComptime until Sema stops giving us garbage Air. |
| 3982 | // https://github.com/ziglang/zig/issues/13410 | 4181 | // https://github.com/ziglang/zig/issues/13410 |
| 3983 | if (f.liveness.isUnused(inst) or !src_ty.hasRuntimeBits()) return CValue.none; | 4182 | if (f.liveness.isUnused(inst) or !dest_ty.hasRuntimeBits()) { |
| | 4183 | try reap(f, inst, &.{ty_op.operand}); |
| | 4184 | return CValue.none; |
| | 4185 | } |
| 3984 | | 4186 | |
| 3985 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 4187 | const operand = try f.resolveInst(ty_op.operand); |
| 3986 | const operand = try f.resolveInstNoInline(ty_op.operand); | 4188 | try reap(f, inst, &.{ty_op.operand}); |
| 3987 | const dest_ty = f.air.typeOf(ty_op.operand); | 4189 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 3988 | const target = f.object.dg.module.getTarget(); | 4190 | const target = f.object.dg.module.getTarget(); |
| | 4191 | const writer = f.object.writer(); |
| | 4192 | |
| | 4193 | const local = try f.allocLocal(inst, dest_ty); |
| 3989 | | 4194 | |
| 3990 | if (dest_ty.isAbiInt() and src_ty.isAbiInt()) { | 4195 | if (operand_ty.isAbiInt() and dest_ty.isAbiInt()) { |
| 3991 | const src_info = src_ty.intInfo(target); | 4196 | const src_info = dest_ty.intInfo(target); |
| 3992 | const dest_info = dest_ty.intInfo(target); | 4197 | const dest_info = operand_ty.intInfo(target); |
| 3993 | if (std.meta.eql(src_info, dest_info)) { | 4198 | if (src_info.signedness == dest_info.signedness and |
| 3994 | return operand; | 4199 | src_info.bits == dest_info.bits) |
| | 4200 | { |
| | 4201 | try f.writeCValue(writer, local, .Other); |
| | 4202 | try writer.writeAll(" = "); |
| | 4203 | try f.writeCValue(writer, operand, .Other); |
| | 4204 | try writer.writeAll(";\n"); |
| | 4205 | return local; |
| 3995 | } | 4206 | } |
| 3996 | } | 4207 | } |
| 3997 | | 4208 | |
| 3998 | const writer = f.object.writer(); | 4209 | if (dest_ty.isPtrAtRuntime() and operand_ty.isPtrAtRuntime()) { |
| 3999 | if (src_ty.isPtrAtRuntime() and dest_ty.isPtrAtRuntime()) { | 4210 | try f.writeCValue(writer, local, .Other); |
| 4000 | const local = try f.allocLocal(src_ty, .Const); | | |
| 4001 | try writer.writeAll(" = ("); | 4211 | try writer.writeAll(" = ("); |
| 4002 | try f.renderTypecast(writer, src_ty); | 4212 | try f.renderTypecast(writer, dest_ty); |
| 4003 | try writer.writeByte(')'); | 4213 | try writer.writeByte(')'); |
| 4004 | try f.writeCValue(writer, operand, .Other); | 4214 | try f.writeCValue(writer, operand, .Other); |
| 4005 | try writer.writeAll(";\n"); | 4215 | try writer.writeAll(";\n"); |
| 4006 | return local; | 4216 | return local; |
| 4007 | } | 4217 | } |
| 4008 | | 4218 | |
| 4009 | const local = try f.allocLocal(src_ty, .Mut); | | |
| 4010 | try writer.writeAll(";\n"); | | |
| 4011 | | | |
| 4012 | const operand_lval = if (operand == .constant) blk: { | 4219 | const operand_lval = if (operand == .constant) blk: { |
| 4013 | const operand_local = try f.allocLocal(dest_ty, .Const); | 4220 | const operand_local = try f.allocLocal(inst, operand_ty); |
| | 4221 | try f.writeCValue(writer, operand_local, .Other); |
| 4014 | try writer.writeAll(" = "); | 4222 | try writer.writeAll(" = "); |
| 4015 | try f.writeCValue(writer, operand, .Initializer); | 4223 | try f.writeCValue(writer, operand, .Initializer); |
| 4016 | try writer.writeAll(";\n"); | 4224 | try writer.writeAll(";\n"); |
| ... | @@ -4022,20 +4230,24 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4022,20 +4230,24 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4022 | try writer.writeAll(", &"); | 4230 | try writer.writeAll(", &"); |
| 4023 | try f.writeCValue(writer, operand_lval, .Other); | 4231 | try f.writeCValue(writer, operand_lval, .Other); |
| 4024 | try writer.writeAll(", sizeof("); | 4232 | try writer.writeAll(", sizeof("); |
| 4025 | try f.renderTypecast(writer, src_ty); | 4233 | try f.renderTypecast(writer, dest_ty); |
| 4026 | try writer.writeAll("));\n"); | 4234 | try writer.writeAll("));\n"); |
| 4027 | | 4235 | |
| 4028 | // Ensure padding bits have the expected value. | 4236 | // Ensure padding bits have the expected value. |
| 4029 | if (src_ty.isAbiInt()) { | 4237 | if (dest_ty.isAbiInt()) { |
| 4030 | try f.writeCValue(writer, local, .Other); | 4238 | try f.writeCValue(writer, local, .Other); |
| 4031 | try writer.writeAll(" = zig_wrap_"); | 4239 | try writer.writeAll(" = zig_wrap_"); |
| 4032 | try f.object.dg.renderTypeForBuiltinFnName(writer, src_ty); | 4240 | try f.object.dg.renderTypeForBuiltinFnName(writer, dest_ty); |
| 4033 | try writer.writeByte('('); | 4241 | try writer.writeByte('('); |
| 4034 | try f.writeCValue(writer, local, .Other); | 4242 | try f.writeCValue(writer, local, .Other); |
| 4035 | try f.object.dg.renderBuiltinInfo(writer, src_ty, .Bits); | 4243 | try f.object.dg.renderBuiltinInfo(writer, dest_ty, .Bits); |
| 4036 | try writer.writeAll(");\n"); | 4244 | try writer.writeAll(");\n"); |
| 4037 | } | 4245 | } |
| 4038 | | 4246 | |
| | 4247 | if (operand == .constant) { |
| | 4248 | try freeLocal(f, inst, operand_lval.local, 0); |
| | 4249 | } |
| | 4250 | |
| 4039 | return local; | 4251 | return local; |
| 4040 | } | 4252 | } |
| 4041 | | 4253 | |
| ... | @@ -4047,7 +4259,8 @@ fn airBreakpoint(writer: anytype) !CValue { | ... | @@ -4047,7 +4259,8 @@ fn airBreakpoint(writer: anytype) !CValue { |
| 4047 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { | 4259 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4048 | if (f.liveness.isUnused(inst)) return CValue.none; | 4260 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4049 | const writer = f.object.writer(); | 4261 | const writer = f.object.writer(); |
| 4050 | const local = try f.allocLocal(Type.usize, .Const); | 4262 | const local = try f.allocLocal(inst, Type.usize); |
| | 4263 | try f.writeCValue(writer, local, .Other); |
| 4051 | try writer.writeAll(" = ("); | 4264 | try writer.writeAll(" = ("); |
| 4052 | try f.renderTypecast(writer, Type.usize); | 4265 | try f.renderTypecast(writer, Type.usize); |
| 4053 | try writer.writeAll(")zig_return_address();\n"); | 4266 | try writer.writeAll(")zig_return_address();\n"); |
| ... | @@ -4057,7 +4270,8 @@ fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4057,7 +4270,8 @@ fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4057 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { | 4270 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4058 | if (f.liveness.isUnused(inst)) return CValue.none; | 4271 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4059 | const writer = f.object.writer(); | 4272 | const writer = f.object.writer(); |
| 4060 | const local = try f.allocLocal(Type.usize, .Const); | 4273 | const local = try f.allocLocal(inst, Type.usize); |
| | 4274 | try f.writeCValue(writer, local, .Other); |
| 4061 | try writer.writeAll(" = ("); | 4275 | try writer.writeAll(" = ("); |
| 4062 | try f.renderTypecast(writer, Type.usize); | 4276 | try f.renderTypecast(writer, Type.usize); |
| 4063 | try writer.writeAll(")zig_frame_address();\n"); | 4277 | try writer.writeAll(")zig_frame_address();\n"); |
| ... | @@ -4088,27 +4302,79 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4088,27 +4302,79 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4088 | const loop = f.air.extraData(Air.Block, ty_pl.payload); | 4302 | const loop = f.air.extraData(Air.Block, ty_pl.payload); |
| 4089 | const body = f.air.extra[loop.end..][0..loop.data.body_len]; | 4303 | const body = f.air.extra[loop.end..][0..loop.data.body_len]; |
| 4090 | const writer = f.object.writer(); | 4304 | const writer = f.object.writer(); |
| 4091 | try writer.writeAll("while ("); | 4305 | |
| 4092 | try f.object.dg.renderValue(writer, Type.bool, Value.true, .condition); | 4306 | const gpa = f.object.dg.gpa; |
| 4093 | try writer.writeAll(") "); | 4307 | try f.free_locals_stack.insert(gpa, f.free_locals_stack.items.len - 1, .{}); |
| | 4308 | |
| | 4309 | try writer.writeAll("for (;;) "); |
| 4094 | try genBody(f, body); | 4310 | try genBody(f, body); |
| 4095 | try writer.writeByte('\n'); | 4311 | try writer.writeByte('\n'); |
| | 4312 | |
| | 4313 | var old_free_locals = f.free_locals_stack.pop(); |
| | 4314 | defer deinitFreeLocalsMap(gpa, &old_free_locals); |
| | 4315 | const new_free_locals = f.getFreeLocals(); |
| | 4316 | var it = new_free_locals.iterator(); |
| | 4317 | while (it.next()) |entry| { |
| | 4318 | const gop = try old_free_locals.getOrPutContext(gpa, entry.key_ptr.*, f.tyHashCtx()); |
| | 4319 | if (gop.found_existing) { |
| | 4320 | try gop.value_ptr.appendSlice(gpa, entry.value_ptr.items); |
| | 4321 | } else { |
| | 4322 | gop.value_ptr.* = entry.value_ptr.*; |
| | 4323 | entry.value_ptr.* = .{}; |
| | 4324 | } |
| | 4325 | } |
| | 4326 | deinitFreeLocalsMap(gpa, new_free_locals); |
| | 4327 | new_free_locals.* = old_free_locals.move(); |
| | 4328 | |
| 4096 | return CValue.none; | 4329 | return CValue.none; |
| 4097 | } | 4330 | } |
| 4098 | | 4331 | |
| 4099 | fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { | 4332 | fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4100 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; | 4333 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 4101 | const cond = try f.resolveInst(pl_op.operand); | 4334 | const cond = try f.resolveInst(pl_op.operand); |
| | 4335 | try reap(f, inst, &.{pl_op.operand}); |
| 4102 | const extra = f.air.extraData(Air.CondBr, pl_op.payload); | 4336 | const extra = f.air.extraData(Air.CondBr, pl_op.payload); |
| 4103 | const then_body = f.air.extra[extra.end..][0..extra.data.then_body_len]; | 4337 | const then_body = f.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 4104 | const else_body = f.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; | 4338 | const else_body = f.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| | 4339 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4105 | const writer = f.object.writer(); | 4340 | const writer = f.object.writer(); |
| 4106 | | 4341 | |
| | 4342 | // Keep using the original for the then branch; use a clone of the value |
| | 4343 | // map for the else branch. |
| | 4344 | const gpa = f.object.dg.gpa; |
| | 4345 | var cloned_map = try f.value_map.clone(); |
| | 4346 | defer cloned_map.deinit(); |
| | 4347 | var cloned_frees = try cloneFreeLocalsMap(gpa, f.getFreeLocals()); |
| | 4348 | defer deinitFreeLocalsMap(gpa, &cloned_frees); |
| | 4349 | |
| | 4350 | // Remember how many locals there were before entering the then branch so |
| | 4351 | // that we can notice and use them in the else branch. Any new locals must |
| | 4352 | // necessarily be free already after the then branch is complete. |
| | 4353 | const pre_locals_len = @intCast(LocalIndex, f.locals.items.len); |
| | 4354 | const pre_clone_depth = f.free_locals_clone_depth; |
| | 4355 | f.free_locals_clone_depth = @intCast(LoopDepth, f.free_locals_stack.items.len); |
| | 4356 | |
| | 4357 | for (liveness_condbr.then_deaths) |operand| { |
| | 4358 | try die(f, inst, Air.indexToRef(operand)); |
| | 4359 | } |
| | 4360 | |
| 4107 | try writer.writeAll("if ("); | 4361 | try writer.writeAll("if ("); |
| 4108 | try f.writeCValue(writer, cond, .condition); | 4362 | try f.writeCValue(writer, cond, .Other); |
| 4109 | try writer.writeAll(") "); | 4363 | try writer.writeAll(") "); |
| 4110 | try genBody(f, then_body); | 4364 | try genBody(f, then_body); |
| 4111 | try writer.writeAll(" else "); | 4365 | try writer.writeAll(" else "); |
| | 4366 | f.value_map.deinit(); |
| | 4367 | f.value_map = cloned_map.move(); |
| | 4368 | const free_locals = f.getFreeLocals(); |
| | 4369 | deinitFreeLocalsMap(gpa, free_locals); |
| | 4370 | free_locals.* = cloned_frees.move(); |
| | 4371 | f.free_locals_clone_depth = pre_clone_depth; |
| | 4372 | for (liveness_condbr.else_deaths) |operand| { |
| | 4373 | try die(f, inst, Air.indexToRef(operand)); |
| | 4374 | } |
| | 4375 | |
| | 4376 | try noticeBranchFrees(f, pre_locals_len, inst); |
| | 4377 | |
| 4112 | try genBody(f, else_body); | 4378 | try genBody(f, else_body); |
| 4113 | try f.object.indent_writer.insertNewline(); | 4379 | try f.object.indent_writer.insertNewline(); |
| 4114 | | 4380 | |
| ... | @@ -4118,6 +4384,7 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4118,6 +4384,7 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4118 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { | 4384 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4119 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; | 4385 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 4120 | const condition = try f.resolveInst(pl_op.operand); | 4386 | const condition = try f.resolveInst(pl_op.operand); |
| | 4387 | try reap(f, inst, &.{pl_op.operand}); |
| 4121 | const condition_ty = f.air.typeOf(pl_op.operand); | 4388 | const condition_ty = f.air.typeOf(pl_op.operand); |
| 4122 | const switch_br = f.air.extraData(Air.SwitchBr, pl_op.payload); | 4389 | const switch_br = f.air.extraData(Air.SwitchBr, pl_op.payload); |
| 4123 | const writer = f.object.writer(); | 4390 | const writer = f.object.writer(); |
| ... | @@ -4136,6 +4403,15 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4136,6 +4403,15 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4136 | try writer.writeAll(") {"); | 4403 | try writer.writeAll(") {"); |
| 4137 | f.object.indent_writer.pushIndent(); | 4404 | f.object.indent_writer.pushIndent(); |
| 4138 | | 4405 | |
| | 4406 | const gpa = f.object.dg.gpa; |
| | 4407 | const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.data.cases_len + 1); |
| | 4408 | defer gpa.free(liveness.deaths); |
| | 4409 | |
| | 4410 | // On the final iteration we do not clone the map. This ensures that |
| | 4411 | // lowering proceeds after the switch_br taking into account the |
| | 4412 | // mutations to the liveness information. |
| | 4413 | const last_case_i = switch_br.data.cases_len - @boolToInt(switch_br.data.else_body_len == 0); |
| | 4414 | |
| 4139 | var extra_index: usize = switch_br.end; | 4415 | var extra_index: usize = switch_br.end; |
| 4140 | var case_i: u32 = 0; | 4416 | var case_i: u32 = 0; |
| 4141 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { | 4417 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| ... | @@ -4155,14 +4431,61 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4155,14 +4431,61 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4155 | try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?, .Other); | 4431 | try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?, .Other); |
| 4156 | try writer.writeAll(": "); | 4432 | try writer.writeAll(": "); |
| 4157 | } | 4433 | } |
| | 4434 | |
| | 4435 | if (case_i != last_case_i) { |
| | 4436 | const old_value_map = f.value_map; |
| | 4437 | f.value_map = try old_value_map.clone(); |
| | 4438 | var free_locals = f.getFreeLocals(); |
| | 4439 | const old_free_locals = free_locals.*; |
| | 4440 | free_locals.* = try cloneFreeLocalsMap(gpa, free_locals); |
| | 4441 | |
| | 4442 | // Remember how many locals there were before entering each branch so that |
| | 4443 | // we can notice and use them in subsequent branches. Any new locals must |
| | 4444 | // necessarily be free already after the previous branch is complete. |
| | 4445 | const pre_locals_len = @intCast(LocalIndex, f.locals.items.len); |
| | 4446 | const pre_clone_depth = f.free_locals_clone_depth; |
| | 4447 | f.free_locals_clone_depth = @intCast(LoopDepth, f.free_locals_stack.items.len); |
| | 4448 | |
| | 4449 | { |
| | 4450 | defer { |
| | 4451 | f.free_locals_clone_depth = pre_clone_depth; |
| | 4452 | f.value_map.deinit(); |
| | 4453 | free_locals = f.getFreeLocals(); |
| | 4454 | deinitFreeLocalsMap(gpa, free_locals); |
| | 4455 | f.value_map = old_value_map; |
| | 4456 | free_locals.* = old_free_locals; |
| | 4457 | } |
| | 4458 | |
| | 4459 | for (liveness.deaths[case_i]) |operand| { |
| | 4460 | try die(f, inst, Air.indexToRef(operand)); |
| | 4461 | } |
| | 4462 | |
| | 4463 | try genBody(f, case_body); |
| | 4464 | } |
| | 4465 | |
| | 4466 | try noticeBranchFrees(f, pre_locals_len, inst); |
| | 4467 | } else { |
| | 4468 | for (liveness.deaths[case_i]) |operand| { |
| | 4469 | try die(f, inst, Air.indexToRef(operand)); |
| | 4470 | } |
| | 4471 | try genBody(f, case_body); |
| | 4472 | } |
| | 4473 | |
| 4158 | // The case body must be noreturn so we don't need to insert a break. | 4474 | // The case body must be noreturn so we don't need to insert a break. |
| 4159 | try genBody(f, case_body); | 4475 | |
| 4160 | } | 4476 | } |
| 4161 | | 4477 | |
| 4162 | const else_body = f.air.extra[extra_index..][0..switch_br.data.else_body_len]; | 4478 | const else_body = f.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 4163 | try f.object.indent_writer.insertNewline(); | 4479 | try f.object.indent_writer.insertNewline(); |
| 4164 | try writer.writeAll("default: "); | 4480 | if (else_body.len > 0) { |
| 4165 | try genBody(f, else_body); | 4481 | for (liveness.deaths[liveness.deaths.len - 1]) |operand| { |
| | 4482 | try die(f, inst, Air.indexToRef(operand)); |
| | 4483 | } |
| | 4484 | try writer.writeAll("default: "); |
| | 4485 | try genBody(f, else_body); |
| | 4486 | } else { |
| | 4487 | try writer.writeAll("default: zig_unreachable();"); |
| | 4488 | } |
| 4166 | try f.object.indent_writer.insertNewline(); | 4489 | try f.object.indent_writer.insertNewline(); |
| 4167 | | 4490 | |
| 4168 | f.object.indent_writer.popIndent(); | 4491 | f.object.indent_writer.popIndent(); |
| ... | @@ -4189,235 +4512,268 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4189,235 +4512,268 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4189 | const inputs = @ptrCast([]const Air.Inst.Ref, f.air.extra[extra_i..][0..extra.data.inputs_len]); | 4512 | const inputs = @ptrCast([]const Air.Inst.Ref, f.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 4190 | extra_i += inputs.len; | 4513 | extra_i += inputs.len; |
| 4191 | | 4514 | |
| 4192 | if (!is_volatile and f.liveness.isUnused(inst)) return CValue.none; | 4515 | const result: CValue = r: { |
| 4193 | | 4516 | if (!is_volatile and f.liveness.isUnused(inst)) break :r CValue.none; |
| 4194 | const writer = f.object.writer(); | | |
| 4195 | const inst_ty = f.air.typeOfIndex(inst); | | |
| 4196 | const local = if (inst_ty.hasRuntimeBitsIgnoreComptime()) local: { | | |
| 4197 | const local = try f.allocLocal(inst_ty, .Mut); | | |
| 4198 | if (f.wantSafety()) { | | |
| 4199 | try writer.writeAll(" = "); | | |
| 4200 | try f.writeCValue(writer, .{ .undef = inst_ty }, .Initializer); | | |
| 4201 | } | | |
| 4202 | try writer.writeAll(";\n"); | | |
| 4203 | break :local local; | | |
| 4204 | } else .none; | | |
| 4205 | | 4517 | |
| 4206 | const locals_begin = f.next_local_index; | 4518 | const writer = f.object.writer(); |
| 4207 | const constraints_extra_begin = extra_i; | 4519 | const inst_ty = f.air.typeOfIndex(inst); |
| 4208 | for (outputs) |output| { | 4520 | const local = if (inst_ty.hasRuntimeBitsIgnoreComptime()) local: { |
| 4209 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); | 4521 | const local = try f.allocLocal(inst, inst_ty); |
| 4210 | const constraint = std.mem.sliceTo(extra_bytes, 0); | | |
| 4211 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); | | |
| 4212 | // This equation accounts for the fact that even if we have exactly 4 bytes | | |
| 4213 | // for the string, we still use the next u32 for the null terminator. | | |
| 4214 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; | | |
| 4215 | | | |
| 4216 | if (constraint.len < 2 or constraint[0] != '=' or | | |
| 4217 | (constraint[1] == '{' and constraint[constraint.len - 1] != '}')) | | |
| 4218 | { | | |
| 4219 | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); | | |
| 4220 | } | | |
| 4221 | | | |
| 4222 | const is_reg = constraint[1] == '{'; | | |
| 4223 | if (is_reg) { | | |
| 4224 | const output_ty = if (output == .none) inst_ty else f.air.typeOf(output).childType(); | | |
| 4225 | try writer.writeAll("register "); | | |
| 4226 | _ = try f.allocLocal(output_ty, .Mut); | | |
| 4227 | try writer.writeAll(" __asm(\""); | | |
| 4228 | try writer.writeAll(constraint["={".len .. constraint.len - "}".len]); | | |
| 4229 | try writer.writeAll("\")"); | | |
| 4230 | if (f.wantSafety()) { | 4522 | if (f.wantSafety()) { |
| | 4523 | try f.writeCValue(writer, local, .Other); |
| 4231 | try writer.writeAll(" = "); | 4524 | try writer.writeAll(" = "); |
| 4232 | try f.writeCValue(writer, .{ .undef = output_ty }, .Initializer); | 4525 | try f.writeCValue(writer, .{ .undef = inst_ty }, .Initializer); |
| | 4526 | try writer.writeAll(";\n"); |
| | 4527 | } |
| | 4528 | break :local local; |
| | 4529 | } else .none; |
| | 4530 | |
| | 4531 | const locals_begin = @intCast(LocalIndex, f.locals.items.len); |
| | 4532 | const constraints_extra_begin = extra_i; |
| | 4533 | for (outputs) |output| { |
| | 4534 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| | 4535 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| | 4536 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| | 4537 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| | 4538 | // for the string, we still use the next u32 for the null terminator. |
| | 4539 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| | 4540 | |
| | 4541 | if (constraint.len < 2 or constraint[0] != '=' or |
| | 4542 | (constraint[1] == '{' and constraint[constraint.len - 1] != '}')) |
| | 4543 | { |
| | 4544 | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); |
| 4233 | } | 4545 | } |
| 4234 | try writer.writeAll(";\n"); | | |
| 4235 | } | | |
| 4236 | } | | |
| 4237 | for (inputs) |input| { | | |
| 4238 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); | | |
| 4239 | const constraint = std.mem.sliceTo(extra_bytes, 0); | | |
| 4240 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); | | |
| 4241 | // This equation accounts for the fact that even if we have exactly 4 bytes | | |
| 4242 | // for the string, we still use the next u32 for the null terminator. | | |
| 4243 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; | | |
| 4244 | | | |
| 4245 | if (constraint.len < 1 or std.mem.indexOfScalar(u8, "=+&%", constraint[0]) != null or | | |
| 4246 | (constraint[0] == '{' and constraint[constraint.len - 1] != '}')) | | |
| 4247 | { | | |
| 4248 | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); | | |
| 4249 | } | | |
| 4250 | | 4546 | |
| 4251 | const is_reg = constraint[0] == '{'; | 4547 | const is_reg = constraint[1] == '{'; |
| 4252 | const input_val = try f.resolveInst(input); | | |
| 4253 | if (asmInputNeedsLocal(constraint, input_val)) { | | |
| 4254 | const input_ty = f.air.typeOf(input); | | |
| 4255 | if (is_reg) try writer.writeAll("register "); | | |
| 4256 | _ = try f.allocLocal(input_ty, .Const); | | |
| 4257 | if (is_reg) { | 4548 | if (is_reg) { |
| | 4549 | const output_ty = if (output == .none) inst_ty else f.air.typeOf(output).childType(); |
| | 4550 | try writer.writeAll("register "); |
| | 4551 | const alignment = 0; |
| | 4552 | const local_value = try f.allocLocalValue(output_ty, alignment); |
| | 4553 | try f.object.dg.renderTypeAndName( |
| | 4554 | writer, |
| | 4555 | output_ty, |
| | 4556 | local_value, |
| | 4557 | .Mut, |
| | 4558 | alignment, |
| | 4559 | .Complete, |
| | 4560 | ); |
| 4258 | try writer.writeAll(" __asm(\""); | 4561 | try writer.writeAll(" __asm(\""); |
| 4259 | try writer.writeAll(constraint["{".len .. constraint.len - "}".len]); | 4562 | try writer.writeAll(constraint["={".len .. constraint.len - "}".len]); |
| 4260 | try writer.writeAll("\")"); | 4563 | try writer.writeAll("\")"); |
| | 4564 | if (f.wantSafety()) { |
| | 4565 | try writer.writeAll(" = "); |
| | 4566 | try f.writeCValue(writer, .{ .undef = output_ty }, .Initializer); |
| | 4567 | } |
| | 4568 | try writer.writeAll(";\n"); |
| 4261 | } | 4569 | } |
| 4262 | try writer.writeAll(" = "); | | |
| 4263 | try f.writeCValue(writer, input_val, .Initializer); | | |
| 4264 | try writer.writeAll(";\n"); | | |
| 4265 | } | 4570 | } |
| 4266 | } | 4571 | for (inputs) |input| { |
| 4267 | { | 4572 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 4268 | var clobber_i: u32 = 0; | 4573 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 4269 | while (clobber_i < clobbers_len) : (clobber_i += 1) { | 4574 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4270 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(f.air.extra[extra_i..]), 0); | | |
| 4271 | // This equation accounts for the fact that even if we have exactly 4 bytes | 4575 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4272 | // for the string, we still use the next u32 for the null terminator. | 4576 | // for the string, we still use the next u32 for the null terminator. |
| 4273 | extra_i += clobber.len / 4 + 1; | 4577 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 4274 | } | | |
| 4275 | } | | |
| 4276 | { | | |
| 4277 | const asm_source = mem.sliceAsBytes(f.air.extra[extra_i..])[0..extra.data.source_len]; | | |
| 4278 | | 4578 | |
| 4279 | var stack = std.heap.stackFallback(256, f.object.dg.gpa); | 4579 | if (constraint.len < 1 or std.mem.indexOfScalar(u8, "=+&%", constraint[0]) != null or |
| 4280 | const allocator = stack.get(); | 4580 | (constraint[0] == '{' and constraint[constraint.len - 1] != '}')) |
| 4281 | const fixed_asm_source = try allocator.alloc(u8, asm_source.len); | 4581 | { |
| 4282 | defer allocator.free(fixed_asm_source); | 4582 | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); |
| | 4583 | } |
| 4283 | | 4584 | |
| 4284 | var src_i: usize = 0; | 4585 | const is_reg = constraint[0] == '{'; |
| 4285 | var dst_i: usize = 0; | 4586 | const input_val = try f.resolveInst(input); |
| 4286 | while (true) { | 4587 | if (asmInputNeedsLocal(constraint, input_val)) { |
| 4287 | const literal = mem.sliceTo(asm_source[src_i..], '%'); | 4588 | const input_ty = f.air.typeOf(input); |
| 4288 | src_i += literal.len; | 4589 | if (is_reg) try writer.writeAll("register "); |
| | 4590 | const alignment = 0; |
| | 4591 | const local_value = try f.allocLocalValue(input_ty, alignment); |
| | 4592 | try f.object.dg.renderTypeAndName( |
| | 4593 | writer, |
| | 4594 | input_ty, |
| | 4595 | local_value, |
| | 4596 | .Const, |
| | 4597 | alignment, |
| | 4598 | .Complete, |
| | 4599 | ); |
| | 4600 | if (is_reg) { |
| | 4601 | try writer.writeAll(" __asm(\""); |
| | 4602 | try writer.writeAll(constraint["{".len .. constraint.len - "}".len]); |
| | 4603 | try writer.writeAll("\")"); |
| | 4604 | } |
| | 4605 | try writer.writeAll(" = "); |
| | 4606 | try f.writeCValue(writer, input_val, .Initializer); |
| | 4607 | try writer.writeAll(";\n"); |
| | 4608 | } |
| | 4609 | } |
| | 4610 | { |
| | 4611 | var clobber_i: u32 = 0; |
| | 4612 | while (clobber_i < clobbers_len) : (clobber_i += 1) { |
| | 4613 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(f.air.extra[extra_i..]), 0); |
| | 4614 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| | 4615 | // for the string, we still use the next u32 for the null terminator. |
| | 4616 | extra_i += clobber.len / 4 + 1; |
| | 4617 | } |
| | 4618 | } |
| | 4619 | |
| | 4620 | { |
| | 4621 | const asm_source = mem.sliceAsBytes(f.air.extra[extra_i..])[0..extra.data.source_len]; |
| 4289 | | 4622 | |
| 4290 | mem.copy(u8, fixed_asm_source[dst_i..], literal); | 4623 | var stack = std.heap.stackFallback(256, f.object.dg.gpa); |
| 4291 | dst_i += literal.len; | 4624 | const allocator = stack.get(); |
| | 4625 | const fixed_asm_source = try allocator.alloc(u8, asm_source.len); |
| | 4626 | defer allocator.free(fixed_asm_source); |
| 4292 | | 4627 | |
| 4293 | if (src_i >= asm_source.len) break; | 4628 | var src_i: usize = 0; |
| | 4629 | var dst_i: usize = 0; |
| | 4630 | while (true) { |
| | 4631 | const literal = mem.sliceTo(asm_source[src_i..], '%'); |
| | 4632 | src_i += literal.len; |
| 4294 | | 4633 | |
| 4295 | src_i += 1; | 4634 | mem.copy(u8, fixed_asm_source[dst_i..], literal); |
| 4296 | if (src_i >= asm_source.len) | 4635 | dst_i += literal.len; |
| 4297 | return f.fail("CBE: invalid inline asm string '{s}'", .{asm_source}); | | |
| 4298 | | 4636 | |
| 4299 | fixed_asm_source[dst_i] = '%'; | 4637 | if (src_i >= asm_source.len) break; |
| 4300 | dst_i += 1; | | |
| 4301 | | 4638 | |
| 4302 | if (asm_source[src_i] != '[') { | | |
| 4303 | // This also handles %% | | |
| 4304 | fixed_asm_source[dst_i] = asm_source[src_i]; | | |
| 4305 | src_i += 1; | 4639 | src_i += 1; |
| | 4640 | if (src_i >= asm_source.len) |
| | 4641 | return f.fail("CBE: invalid inline asm string '{s}'", .{asm_source}); |
| | 4642 | |
| | 4643 | fixed_asm_source[dst_i] = '%'; |
| 4306 | dst_i += 1; | 4644 | dst_i += 1; |
| 4307 | continue; | | |
| 4308 | } | | |
| 4309 | | 4645 | |
| 4310 | const desc = mem.sliceTo(asm_source[src_i..], ']'); | 4646 | if (asm_source[src_i] != '[') { |
| 4311 | if (mem.indexOfScalar(u8, desc, ':')) |colon| { | 4647 | // This also handles %% |
| 4312 | const name = desc[0..colon]; | 4648 | fixed_asm_source[dst_i] = asm_source[src_i]; |
| 4313 | const modifier = desc[colon + 1 ..]; | 4649 | src_i += 1; |
| | 4650 | dst_i += 1; |
| | 4651 | continue; |
| | 4652 | } |
| 4314 | | 4653 | |
| 4315 | mem.copy(u8, fixed_asm_source[dst_i..], modifier); | 4654 | const desc = mem.sliceTo(asm_source[src_i..], ']'); |
| 4316 | dst_i += modifier.len; | 4655 | if (mem.indexOfScalar(u8, desc, ':')) |colon| { |
| 4317 | mem.copy(u8, fixed_asm_source[dst_i..], name); | 4656 | const name = desc[0..colon]; |
| 4318 | dst_i += name.len; | 4657 | const modifier = desc[colon + 1 ..]; |
| 4319 | | 4658 | |
| 4320 | src_i += desc.len; | 4659 | mem.copy(u8, fixed_asm_source[dst_i..], modifier); |
| 4321 | if (src_i >= asm_source.len) | 4660 | dst_i += modifier.len; |
| 4322 | return f.fail("CBE: invalid inline asm string '{s}'", .{asm_source}); | 4661 | mem.copy(u8, fixed_asm_source[dst_i..], name); |
| | 4662 | dst_i += name.len; |
| | 4663 | |
| | 4664 | src_i += desc.len; |
| | 4665 | if (src_i >= asm_source.len) |
| | 4666 | return f.fail("CBE: invalid inline asm string '{s}'", .{asm_source}); |
| | 4667 | } |
| 4323 | } | 4668 | } |
| | 4669 | |
| | 4670 | try writer.writeAll("__asm"); |
| | 4671 | if (is_volatile) try writer.writeAll(" volatile"); |
| | 4672 | try writer.print("({s}", .{fmtStringLiteral(fixed_asm_source[0..dst_i])}); |
| 4324 | } | 4673 | } |
| 4325 | | 4674 | |
| 4326 | try writer.writeAll("__asm"); | 4675 | extra_i = constraints_extra_begin; |
| 4327 | if (is_volatile) try writer.writeAll(" volatile"); | 4676 | var locals_index = locals_begin; |
| 4328 | try writer.print("({s}", .{fmtStringLiteral(fixed_asm_source[0..dst_i])}); | 4677 | try writer.writeByte(':'); |
| 4329 | } | 4678 | for (outputs) |output, index| { |
| 4330 | | 4679 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 4331 | extra_i = constraints_extra_begin; | 4680 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 4332 | var locals_index = locals_begin; | 4681 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4333 | try writer.writeByte(':'); | 4682 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4334 | for (outputs) |output, index| { | 4683 | // for the string, we still use the next u32 for the null terminator. |
| 4335 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); | 4684 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 4336 | const constraint = std.mem.sliceTo(extra_bytes, 0); | 4685 | |
| 4337 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); | 4686 | if (index > 0) try writer.writeByte(','); |
| 4338 | // This equation accounts for the fact that even if we have exactly 4 bytes | 4687 | try writer.writeByte(' '); |
| 4339 | // for the string, we still use the next u32 for the null terminator. | 4688 | if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name}); |
| 4340 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; | 4689 | const is_reg = constraint[1] == '{'; |
| 4341 | | 4690 | try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint)}); |
| 4342 | if (index > 0) try writer.writeByte(','); | 4691 | if (is_reg) { |
| 4343 | try writer.writeByte(' '); | 4692 | try f.writeCValue(writer, .{ .local = locals_index }, .Other); |
| 4344 | if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name}); | 4693 | locals_index += 1; |
| 4345 | const is_reg = constraint[1] == '{'; | 4694 | } else if (output == .none) { |
| 4346 | try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint)}); | 4695 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4347 | if (is_reg) { | 4696 | } else { |
| 4348 | try f.writeCValue(writer, .{ .local = locals_index }, .Other); | 4697 | try f.writeCValueDeref(writer, try f.resolveInst(output)); |
| 4349 | locals_index += 1; | 4698 | } |
| 4350 | } else if (output == .none) { | 4699 | try writer.writeByte(')'); |
| 4351 | try f.writeCValue(writer, local, .FunctionArgument); | | |
| 4352 | } else { | | |
| 4353 | try f.writeCValueDeref(writer, try f.resolveInst(output)); | | |
| 4354 | } | 4700 | } |
| 4355 | try writer.writeByte(')'); | 4701 | try writer.writeByte(':'); |
| 4356 | } | 4702 | for (inputs) |input, index| { |
| 4357 | try writer.writeByte(':'); | 4703 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 4358 | for (inputs) |input, index| { | 4704 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 4359 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); | 4705 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4360 | const constraint = std.mem.sliceTo(extra_bytes, 0); | | |
| 4361 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); | | |
| 4362 | // This equation accounts for the fact that even if we have exactly 4 bytes | | |
| 4363 | // for the string, we still use the next u32 for the null terminator. | | |
| 4364 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; | | |
| 4365 | | | |
| 4366 | if (index > 0) try writer.writeByte(','); | | |
| 4367 | try writer.writeByte(' '); | | |
| 4368 | if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name}); | | |
| 4369 | | | |
| 4370 | const is_reg = constraint[0] == '{'; | | |
| 4371 | const input_val = try f.resolveInst(input); | | |
| 4372 | try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "r" else constraint)}); | | |
| 4373 | try f.writeCValue(writer, if (asmInputNeedsLocal(constraint, input_val)) local: { | | |
| 4374 | const input_local = CValue{ .local = locals_index }; | | |
| 4375 | locals_index += 1; | | |
| 4376 | break :local input_local; | | |
| 4377 | } else input_val, .Other); | | |
| 4378 | try writer.writeByte(')'); | | |
| 4379 | } | | |
| 4380 | try writer.writeByte(':'); | | |
| 4381 | { | | |
| 4382 | var clobber_i: u32 = 0; | | |
| 4383 | while (clobber_i < clobbers_len) : (clobber_i += 1) { | | |
| 4384 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(f.air.extra[extra_i..]), 0); | | |
| 4385 | // This equation accounts for the fact that even if we have exactly 4 bytes | 4706 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4386 | // for the string, we still use the next u32 for the null terminator. | 4707 | // for the string, we still use the next u32 for the null terminator. |
| 4387 | extra_i += clobber.len / 4 + 1; | 4708 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| | 4709 | |
| | 4710 | if (index > 0) try writer.writeByte(','); |
| | 4711 | try writer.writeByte(' '); |
| | 4712 | if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name}); |
| | 4713 | |
| | 4714 | const is_reg = constraint[0] == '{'; |
| | 4715 | const input_val = try f.resolveInst(input); |
| | 4716 | try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "r" else constraint)}); |
| | 4717 | try f.writeCValue(writer, if (asmInputNeedsLocal(constraint, input_val)) local: { |
| | 4718 | const input_local = CValue{ .local = locals_index }; |
| | 4719 | locals_index += 1; |
| | 4720 | break :local input_local; |
| | 4721 | } else input_val, .Other); |
| | 4722 | try writer.writeByte(')'); |
| | 4723 | } |
| | 4724 | try writer.writeByte(':'); |
| | 4725 | { |
| | 4726 | var clobber_i: u32 = 0; |
| | 4727 | while (clobber_i < clobbers_len) : (clobber_i += 1) { |
| | 4728 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(f.air.extra[extra_i..]), 0); |
| | 4729 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| | 4730 | // for the string, we still use the next u32 for the null terminator. |
| | 4731 | extra_i += clobber.len / 4 + 1; |
| 4388 | | 4732 | |
| 4389 | if (clobber.len == 0) continue; | 4733 | if (clobber.len == 0) continue; |
| 4390 | | 4734 | |
| 4391 | if (clobber_i > 0) try writer.writeByte(','); | 4735 | if (clobber_i > 0) try writer.writeByte(','); |
| 4392 | try writer.print(" {s}", .{fmtStringLiteral(clobber)}); | 4736 | try writer.print(" {s}", .{fmtStringLiteral(clobber)}); |
| | 4737 | } |
| 4393 | } | 4738 | } |
| 4394 | } | 4739 | try writer.writeAll(");\n"); |
| 4395 | try writer.writeAll(");\n"); | | |
| 4396 | | 4740 | |
| 4397 | extra_i = constraints_extra_begin; | 4741 | extra_i = constraints_extra_begin; |
| 4398 | locals_index = locals_begin; | 4742 | locals_index = locals_begin; |
| 4399 | for (outputs) |output| { | 4743 | for (outputs) |output| { |
| 4400 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); | 4744 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 4401 | const constraint = std.mem.sliceTo(extra_bytes, 0); | 4745 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 4402 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); | 4746 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4403 | // This equation accounts for the fact that even if we have exactly 4 bytes | 4747 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4404 | // for the string, we still use the next u32 for the null terminator. | 4748 | // for the string, we still use the next u32 for the null terminator. |
| 4405 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; | 4749 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 4406 | | 4750 | |
| 4407 | const is_reg = constraint[1] == '{'; | 4751 | const is_reg = constraint[1] == '{'; |
| 4408 | if (is_reg) { | 4752 | if (is_reg) { |
| 4409 | try f.writeCValueDeref(writer, if (output == .none) | 4753 | try f.writeCValueDeref(writer, if (output == .none) |
| 4410 | CValue{ .local_ref = local.local } | 4754 | CValue{ .local_ref = local.local } |
| 4411 | else | 4755 | else |
| 4412 | try f.resolveInst(output)); | 4756 | try f.resolveInst(output)); |
| 4413 | try writer.writeAll(" = "); | 4757 | try writer.writeAll(" = "); |
| 4414 | try f.writeCValue(writer, .{ .local = locals_index }, .Other); | 4758 | try f.writeCValue(writer, .{ .local = locals_index }, .Other); |
| 4415 | locals_index += 1; | 4759 | locals_index += 1; |
| 4416 | try writer.writeAll(";\n"); | 4760 | try writer.writeAll(";\n"); |
| | 4761 | } |
| 4417 | } | 4762 | } |
| | 4763 | |
| | 4764 | break :r local; |
| | 4765 | }; |
| | 4766 | |
| | 4767 | var bt = iterateBigTomb(f, inst); |
| | 4768 | for (outputs) |output| { |
| | 4769 | if (output == .none) continue; |
| | 4770 | try bt.feed(output); |
| | 4771 | } |
| | 4772 | for (inputs) |input| { |
| | 4773 | try bt.feed(input); |
| 4418 | } | 4774 | } |
| 4419 | | 4775 | |
| 4420 | return local; | 4776 | return result; |
| 4421 | } | 4777 | } |
| 4422 | | 4778 | |
| 4423 | fn airIsNull( | 4779 | fn airIsNull( |
| ... | @@ -4425,12 +4781,26 @@ fn airIsNull( | ... | @@ -4425,12 +4781,26 @@ fn airIsNull( |
| 4425 | inst: Air.Inst.Index, | 4781 | inst: Air.Inst.Index, |
| 4426 | operator: []const u8, | 4782 | operator: []const u8, |
| 4427 | is_ptr: bool, | 4783 | is_ptr: bool, |
| 4428 | ) !void { | 4784 | ) !CValue { |
| 4429 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 4785 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 4786 | |
| | 4787 | if (f.liveness.isUnused(inst)) { |
| | 4788 | try reap(f, inst, &.{un_op}); |
| | 4789 | return CValue.none; |
| | 4790 | } |
| | 4791 | |
| 4430 | const writer = f.object.writer(); | 4792 | const writer = f.object.writer(); |
| 4431 | const operand = try f.resolveInst(un_op); | 4793 | const operand = try f.resolveInst(un_op); |
| | 4794 | try reap(f, inst, &.{un_op}); |
| 4432 | | 4795 | |
| 4433 | try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other); | 4796 | const local = try f.allocLocal(inst, Type.bool); |
| | 4797 | try f.writeCValue(writer, local, .Other); |
| | 4798 | try writer.writeAll(" = "); |
| | 4799 | if (is_ptr) { |
| | 4800 | try f.writeCValueDeref(writer, operand); |
| | 4801 | } else { |
| | 4802 | try f.writeCValue(writer, operand, .Other); |
| | 4803 | } |
| 4434 | | 4804 | |
| 4435 | const operand_ty = f.air.typeOf(un_op); | 4805 | const operand_ty = f.air.typeOf(un_op); |
| 4436 | const optional_ty = if (is_ptr) operand_ty.childType() else operand_ty; | 4806 | const optional_ty = if (is_ptr) operand_ty.childType() else operand_ty; |
| ... | @@ -4457,33 +4827,52 @@ fn airIsNull( | ... | @@ -4457,33 +4827,52 @@ fn airIsNull( |
| 4457 | try writer.writeAll(operator); | 4827 | try writer.writeAll(operator); |
| 4458 | try writer.writeByte(' '); | 4828 | try writer.writeByte(' '); |
| 4459 | try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other); | 4829 | try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other); |
| | 4830 | try writer.writeAll(";\n"); |
| | 4831 | return local; |
| 4460 | } | 4832 | } |
| 4461 | | 4833 | |
| 4462 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { | 4834 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4463 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 4464 | | | |
| 4465 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 4835 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 4836 | |
| | 4837 | if (f.liveness.isUnused(inst)) { |
| | 4838 | try reap(f, inst, &.{ty_op.operand}); |
| | 4839 | return CValue.none; |
| | 4840 | } |
| | 4841 | |
| 4466 | const operand = try f.resolveInst(ty_op.operand); | 4842 | const operand = try f.resolveInst(ty_op.operand); |
| | 4843 | try reap(f, inst, &.{ty_op.operand}); |
| 4467 | const opt_ty = f.air.typeOf(ty_op.operand); | 4844 | const opt_ty = f.air.typeOf(ty_op.operand); |
| 4468 | | 4845 | |
| 4469 | var buf: Type.Payload.ElemType = undefined; | 4846 | var buf: Type.Payload.ElemType = undefined; |
| 4470 | const payload_ty = opt_ty.optionalChild(&buf); | 4847 | const payload_ty = opt_ty.optionalChild(&buf); |
| 4471 | | 4848 | |
| 4472 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 4849 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4473 | if (opt_ty.optionalReprIsPayload()) return operand; | 4850 | return CValue.none; |
| | 4851 | } |
| 4474 | | 4852 | |
| 4475 | const inst_ty = f.air.typeOfIndex(inst); | 4853 | const inst_ty = f.air.typeOfIndex(inst); |
| | 4854 | const local = try f.allocLocal(inst, inst_ty); |
| | 4855 | const writer = f.object.writer(); |
| | 4856 | |
| | 4857 | if (opt_ty.optionalReprIsPayload()) { |
| | 4858 | try f.writeCValue(writer, local, .Other); |
| | 4859 | try writer.writeAll(" = "); |
| | 4860 | try f.writeCValue(writer, operand, .Other); |
| | 4861 | try writer.writeAll(";\n"); |
| | 4862 | return local; |
| | 4863 | } |
| | 4864 | |
| 4476 | const target = f.object.dg.module.getTarget(); | 4865 | const target = f.object.dg.module.getTarget(); |
| 4477 | const is_array = lowersToArray(inst_ty, target); | 4866 | const is_array = lowersToArray(inst_ty, target); |
| 4478 | | 4867 | |
| 4479 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | | |
| 4480 | const writer = f.object.writer(); | | |
| 4481 | if (is_array) { | 4868 | if (is_array) { |
| 4482 | try writer.writeAll(";\n"); | | |
| 4483 | try writer.writeAll("memcpy("); | 4869 | try writer.writeAll("memcpy("); |
| 4484 | try f.writeCValue(writer, local, .FunctionArgument); | 4870 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4485 | try writer.writeAll(", "); | 4871 | try writer.writeAll(", "); |
| 4486 | } else try writer.writeAll(" = "); | 4872 | } else { |
| | 4873 | try f.writeCValue(writer, local, .Other); |
| | 4874 | try writer.writeAll(" = "); |
| | 4875 | } |
| 4487 | try f.writeCValueMember(writer, operand, .{ .identifier = "payload" }); | 4876 | try f.writeCValueMember(writer, operand, .{ .identifier = "payload" }); |
| 4488 | if (is_array) { | 4877 | if (is_array) { |
| 4489 | try writer.writeAll(", sizeof("); | 4878 | try writer.writeAll(", sizeof("); |
| ... | @@ -4495,11 +4884,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4495,11 +4884,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4495 | } | 4884 | } |
| 4496 | | 4885 | |
| 4497 | fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 4886 | fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4498 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 4499 | | | |
| 4500 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 4887 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 4888 | |
| | 4889 | if (f.liveness.isUnused(inst)) { |
| | 4890 | try reap(f, inst, &.{ty_op.operand}); |
| | 4891 | return CValue.none; |
| | 4892 | } |
| | 4893 | |
| 4501 | const writer = f.object.writer(); | 4894 | const writer = f.object.writer(); |
| 4502 | const operand = try f.resolveInst(ty_op.operand); | 4895 | const operand = try f.resolveInst(ty_op.operand); |
| | 4896 | try reap(f, inst, &.{ty_op.operand}); |
| 4503 | const ptr_ty = f.air.typeOf(ty_op.operand); | 4897 | const ptr_ty = f.air.typeOf(ty_op.operand); |
| 4504 | const opt_ty = ptr_ty.childType(); | 4898 | const opt_ty = ptr_ty.childType(); |
| 4505 | const inst_ty = f.air.typeOfIndex(inst); | 4899 | const inst_ty = f.air.typeOfIndex(inst); |
| ... | @@ -4508,15 +4902,18 @@ fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4508,15 +4902,18 @@ fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4508 | return CValue{ .undef = inst_ty }; | 4902 | return CValue{ .undef = inst_ty }; |
| 4509 | } | 4903 | } |
| 4510 | | 4904 | |
| | 4905 | const local = try f.allocLocal(inst, inst_ty); |
| | 4906 | try f.writeCValue(writer, local, .Other); |
| | 4907 | |
| 4511 | if (opt_ty.optionalReprIsPayload()) { | 4908 | if (opt_ty.optionalReprIsPayload()) { |
| 4512 | // the operand is just a regular pointer, no need to do anything special. | 4909 | // the operand is just a regular pointer, no need to do anything special. |
| 4513 | // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C | 4910 | // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C |
| 4514 | return operand; | 4911 | try writer.writeAll(" = "); |
| | 4912 | try f.writeCValue(writer, operand, .Other); |
| | 4913 | } else { |
| | 4914 | try writer.writeAll(" = &"); |
| | 4915 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" }); |
| 4515 | } | 4916 | } |
| 4516 | | | |
| 4517 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 4518 | try writer.writeAll(" = &"); | | |
| 4519 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" }); | | |
| 4520 | try writer.writeAll(";\n"); | 4917 | try writer.writeAll(";\n"); |
| 4521 | return local; | 4918 | return local; |
| 4522 | } | 4919 | } |
| ... | @@ -4525,62 +4922,88 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4525,62 +4922,88 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4525 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 4922 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 4526 | const writer = f.object.writer(); | 4923 | const writer = f.object.writer(); |
| 4527 | const operand = try f.resolveInst(ty_op.operand); | 4924 | const operand = try f.resolveInst(ty_op.operand); |
| | 4925 | try reap(f, inst, &.{ty_op.operand}); |
| 4528 | const operand_ty = f.air.typeOf(ty_op.operand); | 4926 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 4529 | | 4927 | |
| 4530 | const opt_ty = operand_ty.elemType(); | 4928 | const opt_ty = operand_ty.elemType(); |
| 4531 | | 4929 | |
| | 4930 | const inst_ty = f.air.typeOfIndex(inst); |
| | 4931 | |
| 4532 | if (opt_ty.optionalReprIsPayload()) { | 4932 | if (opt_ty.optionalReprIsPayload()) { |
| | 4933 | if (f.liveness.isUnused(inst)) { |
| | 4934 | return CValue.none; |
| | 4935 | } |
| | 4936 | const local = try f.allocLocal(inst, inst_ty); |
| 4533 | // The payload and the optional are the same value. | 4937 | // The payload and the optional are the same value. |
| 4534 | // Setting to non-null will be done when the payload is set. | 4938 | // Setting to non-null will be done when the payload is set. |
| 4535 | return operand; | 4939 | try f.writeCValue(writer, local, .Other); |
| 4536 | } | 4940 | try writer.writeAll(" = "); |
| 4537 | | 4941 | try f.writeCValue(writer, operand, .Other); |
| 4538 | try f.writeCValueDeref(writer, operand); | 4942 | try writer.writeAll(";\n"); |
| 4539 | try writer.writeAll(".is_null = "); | 4943 | return local; |
| 4540 | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer); | 4944 | } else { |
| 4541 | try writer.writeAll(";\n"); | 4945 | try f.writeCValueDeref(writer, operand); |
| | 4946 | try writer.writeAll(".is_null = "); |
| | 4947 | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer); |
| | 4948 | try writer.writeAll(";\n"); |
| 4542 | | 4949 | |
| 4543 | const inst_ty = f.air.typeOfIndex(inst); | 4950 | if (f.liveness.isUnused(inst)) { |
| 4544 | const local = try f.allocLocal(inst_ty, .Const); | 4951 | return CValue.none; |
| 4545 | try writer.writeAll(" = &"); | 4952 | } |
| 4546 | try f.writeCValueDeref(writer, operand); | | |
| 4547 | | 4953 | |
| 4548 | try writer.writeAll(".payload;\n"); | 4954 | const local = try f.allocLocal(inst, inst_ty); |
| 4549 | return local; | 4955 | try f.writeCValue(writer, local, .Other); |
| | 4956 | try writer.writeAll(" = &"); |
| | 4957 | try f.writeCValueDeref(writer, operand); |
| | 4958 | try writer.writeAll(".payload;\n"); |
| | 4959 | return local; |
| | 4960 | } |
| 4550 | } | 4961 | } |
| 4551 | | 4962 | |
| 4552 | fn airStructFieldPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 4963 | fn airStructFieldPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4553 | if (f.liveness.isUnused(inst)) | 4964 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| | 4965 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| | 4966 | |
| | 4967 | if (f.liveness.isUnused(inst)) { |
| | 4968 | try reap(f, inst, &.{extra.struct_operand}); |
| 4554 | // TODO this @as is needed because of a stage1 bug | 4969 | // TODO this @as is needed because of a stage1 bug |
| 4555 | return @as(CValue, CValue.none); | 4970 | return @as(CValue, CValue.none); |
| | 4971 | } |
| 4556 | | 4972 | |
| 4557 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | | |
| 4558 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; | | |
| 4559 | const struct_ptr = try f.resolveInst(extra.struct_operand); | 4973 | const struct_ptr = try f.resolveInst(extra.struct_operand); |
| | 4974 | try reap(f, inst, &.{extra.struct_operand}); |
| 4560 | const struct_ptr_ty = f.air.typeOf(extra.struct_operand); | 4975 | const struct_ptr_ty = f.air.typeOf(extra.struct_operand); |
| 4561 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, extra.field_index); | 4976 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, extra.field_index); |
| 4562 | } | 4977 | } |
| 4563 | | 4978 | |
| 4564 | fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue { | 4979 | fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue { |
| 4565 | if (f.liveness.isUnused(inst)) | 4980 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 4981 | |
| | 4982 | if (f.liveness.isUnused(inst)) { |
| | 4983 | try reap(f, inst, &.{ty_op.operand}); |
| 4566 | // TODO this @as is needed because of a stage1 bug | 4984 | // TODO this @as is needed because of a stage1 bug |
| 4567 | return @as(CValue, CValue.none); | 4985 | return @as(CValue, CValue.none); |
| | 4986 | } |
| 4568 | | 4987 | |
| 4569 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 4570 | const struct_ptr = try f.resolveInst(ty_op.operand); | 4988 | const struct_ptr = try f.resolveInst(ty_op.operand); |
| | 4989 | try reap(f, inst, &.{ty_op.operand}); |
| 4571 | const struct_ptr_ty = f.air.typeOf(ty_op.operand); | 4990 | const struct_ptr_ty = f.air.typeOf(ty_op.operand); |
| 4572 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, index); | 4991 | return structFieldPtr(f, inst, struct_ptr_ty, struct_ptr, index); |
| 4573 | } | 4992 | } |
| 4574 | | 4993 | |
| 4575 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 4994 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4576 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 4577 | | | |
| 4578 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 4995 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 4579 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; | 4996 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 4580 | | 4997 | |
| | 4998 | if (f.liveness.isUnused(inst)) { |
| | 4999 | try reap(f, inst, &.{extra.field_ptr}); |
| | 5000 | return CValue.none; |
| | 5001 | } |
| | 5002 | |
| 4581 | const struct_ptr_ty = f.air.typeOfIndex(inst); | 5003 | const struct_ptr_ty = f.air.typeOfIndex(inst); |
| 4582 | const field_ptr_ty = f.air.typeOf(extra.field_ptr); | 5004 | const field_ptr_ty = f.air.typeOf(extra.field_ptr); |
| 4583 | const field_ptr_val = try f.resolveInst(extra.field_ptr); | 5005 | const field_ptr_val = try f.resolveInst(extra.field_ptr); |
| | 5006 | try reap(f, inst, &.{extra.field_ptr}); |
| 4584 | | 5007 | |
| 4585 | const target = f.object.dg.module.getTarget(); | 5008 | const target = f.object.dg.module.getTarget(); |
| 4586 | const struct_ty = struct_ptr_ty.childType(); | 5009 | const struct_ty = struct_ptr_ty.childType(); |
| ... | @@ -4597,7 +5020,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4597,7 +5020,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4597 | const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base); | 5020 | const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base); |
| 4598 | | 5021 | |
| 4599 | const writer = f.object.writer(); | 5022 | const writer = f.object.writer(); |
| 4600 | const local = try f.allocLocal(struct_ptr_ty, .Const); | 5023 | const local = try f.allocLocal(inst, struct_ptr_ty); |
| | 5024 | try f.writeCValue(writer, local, .Other); |
| 4601 | try writer.writeAll(" = ("); | 5025 | try writer.writeAll(" = ("); |
| 4602 | try f.renderTypecast(writer, struct_ptr_ty); | 5026 | try f.renderTypecast(writer, struct_ptr_ty); |
| 4603 | try writer.writeAll(")&(("); | 5027 | try writer.writeAll(")&(("); |
| ... | @@ -4618,7 +5042,8 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc | ... | @@ -4618,7 +5042,8 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc |
| 4618 | // Ensure complete type definition is visible before accessing fields. | 5042 | // Ensure complete type definition is visible before accessing fields. |
| 4619 | try f.renderType(std.io.null_writer, struct_ty); | 5043 | try f.renderType(std.io.null_writer, struct_ty); |
| 4620 | | 5044 | |
| 4621 | const local = try f.allocLocal(field_ptr_ty, .Const); | 5045 | const local = try f.allocLocal(inst, field_ptr_ty); |
| | 5046 | try f.writeCValue(writer, local, .Other); |
| 4622 | try writer.writeAll(" = ("); | 5047 | try writer.writeAll(" = ("); |
| 4623 | try f.renderTypecast(writer, field_ptr_ty); | 5048 | try f.renderTypecast(writer, field_ptr_ty); |
| 4624 | try writer.writeByte(')'); | 5049 | try writer.writeByte(')'); |
| ... | @@ -4706,16 +5131,23 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc | ... | @@ -4706,16 +5131,23 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc |
| 4706 | } | 5131 | } |
| 4707 | | 5132 | |
| 4708 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | 5133 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4709 | if (f.liveness.isUnused(inst)) | 5134 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| | 5135 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| | 5136 | |
| | 5137 | if (f.liveness.isUnused(inst)) { |
| | 5138 | try reap(f, inst, &.{extra.struct_operand}); |
| 4710 | return CValue.none; | 5139 | return CValue.none; |
| | 5140 | } |
| 4711 | | 5141 | |
| 4712 | const inst_ty = f.air.typeOfIndex(inst); | 5142 | const inst_ty = f.air.typeOfIndex(inst); |
| 4713 | if (!inst_ty.hasRuntimeBitsIgnoreComptime()) return CValue.none; | 5143 | if (!inst_ty.hasRuntimeBitsIgnoreComptime()) { |
| | 5144 | try reap(f, inst, &.{extra.struct_operand}); |
| | 5145 | return CValue.none; |
| | 5146 | } |
| 4714 | | 5147 | |
| 4715 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | | |
| 4716 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; | | |
| 4717 | const target = f.object.dg.module.getTarget(); | 5148 | const target = f.object.dg.module.getTarget(); |
| 4718 | const struct_byval = try f.resolveInst(extra.struct_operand); | 5149 | const struct_byval = try f.resolveInst(extra.struct_operand); |
| | 5150 | try reap(f, inst, &.{extra.struct_operand}); |
| 4719 | const struct_ty = f.air.typeOf(extra.struct_operand); | 5151 | const struct_ty = f.air.typeOf(extra.struct_operand); |
| 4720 | const writer = f.object.writer(); | 5152 | const writer = f.object.writer(); |
| 4721 | | 5153 | |
| ... | @@ -4759,7 +5191,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4759,7 +5191,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4759 | }; | 5191 | }; |
| 4760 | const field_int_ty = Type.initPayload(&field_int_pl.base); | 5192 | const field_int_ty = Type.initPayload(&field_int_pl.base); |
| 4761 | | 5193 | |
| 4762 | const temp_local = try f.allocLocal(field_int_ty, .Const); | 5194 | const temp_local = try f.allocLocal(inst, try field_int_ty.copy(f.arena.allocator())); |
| | 5195 | try f.writeCValue(writer, temp_local, .Other); |
| 4763 | try writer.writeAll(" = zig_wrap_"); | 5196 | try writer.writeAll(" = zig_wrap_"); |
| 4764 | try f.object.dg.renderTypeForBuiltinFnName(writer, field_int_ty); | 5197 | try f.object.dg.renderTypeForBuiltinFnName(writer, field_int_ty); |
| 4765 | try writer.writeAll("(("); | 5198 | try writer.writeAll("(("); |
| ... | @@ -4775,8 +5208,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4775,8 +5208,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4775 | try writer.writeAll(");\n"); | 5208 | try writer.writeAll(");\n"); |
| 4776 | if (inst_ty.eql(field_int_ty, f.object.dg.module)) return temp_local; | 5209 | if (inst_ty.eql(field_int_ty, f.object.dg.module)) return temp_local; |
| 4777 | | 5210 | |
| 4778 | const local = try f.allocLocal(inst_ty, .Mut); | 5211 | const local = try f.allocLocal(inst, inst_ty); |
| 4779 | try writer.writeAll(";\n"); | | |
| 4780 | try writer.writeAll("memcpy("); | 5212 | try writer.writeAll("memcpy("); |
| 4781 | try f.writeCValue(writer, .{ .local_ref = local.local }, .FunctionArgument); | 5213 | try f.writeCValue(writer, .{ .local_ref = local.local }, .FunctionArgument); |
| 4782 | try writer.writeAll(", "); | 5214 | try writer.writeAll(", "); |
| ... | @@ -4784,20 +5216,21 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4784,20 +5216,21 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4784 | try writer.writeAll(", sizeof("); | 5216 | try writer.writeAll(", sizeof("); |
| 4785 | try f.renderTypecast(writer, inst_ty); | 5217 | try f.renderTypecast(writer, inst_ty); |
| 4786 | try writer.writeAll("));\n"); | 5218 | try writer.writeAll("));\n"); |
| | 5219 | try freeLocal(f, inst, temp_local.local, 0); |
| 4787 | return local; | 5220 | return local; |
| 4788 | }, | 5221 | }, |
| 4789 | }, | 5222 | }, |
| 4790 | .@"union", .union_safety_tagged, .union_tagged => if (struct_ty.containerLayout() == .Packed) { | 5223 | .@"union", .union_safety_tagged, .union_tagged => if (struct_ty.containerLayout() == .Packed) { |
| 4791 | const operand_lval = if (struct_byval == .constant) blk: { | 5224 | const operand_lval = if (struct_byval == .constant) blk: { |
| 4792 | const operand_local = try f.allocLocal(struct_ty, .Const); | 5225 | const operand_local = try f.allocLocal(inst, struct_ty); |
| | 5226 | try f.writeCValue(writer, operand_local, .Other); |
| 4793 | try writer.writeAll(" = "); | 5227 | try writer.writeAll(" = "); |
| 4794 | try f.writeCValue(writer, struct_byval, .Initializer); | 5228 | try f.writeCValue(writer, struct_byval, .Initializer); |
| 4795 | try writer.writeAll(";\n"); | 5229 | try writer.writeAll(";\n"); |
| 4796 | break :blk operand_local; | 5230 | break :blk operand_local; |
| 4797 | } else struct_byval; | 5231 | } else struct_byval; |
| 4798 | | 5232 | |
| 4799 | const local = try f.allocLocal(inst_ty, .Mut); | 5233 | const local = try f.allocLocal(inst, inst_ty); |
| 4800 | try writer.writeAll(";\n"); | | |
| 4801 | try writer.writeAll("memcpy(&"); | 5234 | try writer.writeAll("memcpy(&"); |
| 4802 | try f.writeCValue(writer, local, .FunctionArgument); | 5235 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4803 | try writer.writeAll(", &"); | 5236 | try writer.writeAll(", &"); |
| ... | @@ -4805,6 +5238,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4805,6 +5238,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4805 | try writer.writeAll(", sizeof("); | 5238 | try writer.writeAll(", sizeof("); |
| 4806 | try f.renderTypecast(writer, inst_ty); | 5239 | try f.renderTypecast(writer, inst_ty); |
| 4807 | try writer.writeAll("));\n"); | 5240 | try writer.writeAll("));\n"); |
| | 5241 | |
| | 5242 | if (struct_byval == .constant) { |
| | 5243 | try freeLocal(f, inst, operand_lval.local, 0); |
| | 5244 | } |
| | 5245 | |
| 4808 | return local; | 5246 | return local; |
| 4809 | } else .{ | 5247 | } else .{ |
| 4810 | .identifier = struct_ty.unionFields().keys()[extra.field_index], | 5248 | .identifier = struct_ty.unionFields().keys()[extra.field_index], |
| ... | @@ -4822,13 +5260,15 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4822,13 +5260,15 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4822 | }; | 5260 | }; |
| 4823 | | 5261 | |
| 4824 | const is_array = lowersToArray(inst_ty, target); | 5262 | const is_array = lowersToArray(inst_ty, target); |
| 4825 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | 5263 | const local = try f.allocLocal(inst, inst_ty); |
| 4826 | if (is_array) { | 5264 | if (is_array) { |
| 4827 | try writer.writeAll(";\n"); | | |
| 4828 | try writer.writeAll("memcpy("); | 5265 | try writer.writeAll("memcpy("); |
| 4829 | try f.writeCValue(writer, local, .FunctionArgument); | 5266 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4830 | try writer.writeAll(", "); | 5267 | try writer.writeAll(", "); |
| 4831 | } else try writer.writeAll(" = "); | 5268 | } else { |
| | 5269 | try f.writeCValue(writer, local, .Other); |
| | 5270 | try writer.writeAll(" = "); |
| | 5271 | } |
| 4832 | if (extra_name != .none) { | 5272 | if (extra_name != .none) { |
| 4833 | try f.writeCValueMember(writer, struct_byval, extra_name); | 5273 | try f.writeCValueMember(writer, struct_byval, extra_name); |
| 4834 | try writer.writeByte('.'); | 5274 | try writer.writeByte('.'); |
| ... | @@ -4846,40 +5286,53 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4846,40 +5286,53 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4846 | /// *(E!T) -> E | 5286 | /// *(E!T) -> E |
| 4847 | /// Note that the result is never a pointer. | 5287 | /// Note that the result is never a pointer. |
| 4848 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | 5288 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4849 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 4850 | | | |
| 4851 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 5289 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5290 | |
| | 5291 | if (f.liveness.isUnused(inst)) { |
| | 5292 | try reap(f, inst, &.{ty_op.operand}); |
| | 5293 | return CValue.none; |
| | 5294 | } |
| | 5295 | |
| 4852 | const inst_ty = f.air.typeOfIndex(inst); | 5296 | const inst_ty = f.air.typeOfIndex(inst); |
| 4853 | const operand = try f.resolveInst(ty_op.operand); | 5297 | const operand = try f.resolveInst(ty_op.operand); |
| 4854 | const operand_ty = f.air.typeOf(ty_op.operand); | 5298 | const operand_ty = f.air.typeOf(ty_op.operand); |
| | 5299 | try reap(f, inst, &.{ty_op.operand}); |
| 4855 | | 5300 | |
| 4856 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; | 5301 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; |
| 4857 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | 5302 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; |
| 4858 | const error_ty = error_union_ty.errorUnionSet(); | 5303 | const error_ty = error_union_ty.errorUnionSet(); |
| 4859 | const payload_ty = error_union_ty.errorUnionPayload(); | 5304 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 4860 | if (!payload_ty.hasRuntimeBits()) return operand; | 5305 | const local = try f.allocLocal(inst, inst_ty); |
| 4861 | | | |
| 4862 | const writer = f.object.writer(); | 5306 | const writer = f.object.writer(); |
| 4863 | const local = try f.allocLocal(inst_ty, .Const); | 5307 | try f.writeCValue(writer, local, .Other); |
| 4864 | try writer.writeAll(" = "); | 5308 | try writer.writeAll(" = "); |
| 4865 | if (!error_ty.errorSetIsEmpty()) | 5309 | |
| 4866 | if (operand_is_ptr) | 5310 | if (!payload_ty.hasRuntimeBits()) { |
| 4867 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) | 5311 | try f.writeCValue(writer, operand, .Other); |
| | 5312 | } else { |
| | 5313 | if (!error_ty.errorSetIsEmpty()) |
| | 5314 | if (operand_is_ptr) |
| | 5315 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| | 5316 | else |
| | 5317 | try f.writeCValueMember(writer, operand, .{ .identifier = "error" }) |
| 4868 | else | 5318 | else |
| 4869 | try f.writeCValueMember(writer, operand, .{ .identifier = "error" }) | 5319 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Initializer); |
| 4870 | else | 5320 | } |
| 4871 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Initializer); | | |
| 4872 | try writer.writeAll(";\n"); | 5321 | try writer.writeAll(";\n"); |
| 4873 | return local; | 5322 | return local; |
| 4874 | } | 5323 | } |
| 4875 | | 5324 | |
| 4876 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { | 5325 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 4877 | if (f.liveness.isUnused(inst)) | 5326 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5327 | |
| | 5328 | if (f.liveness.isUnused(inst)) { |
| | 5329 | try reap(f, inst, &.{ty_op.operand}); |
| 4878 | return CValue.none; | 5330 | return CValue.none; |
| | 5331 | } |
| 4879 | | 5332 | |
| 4880 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 4881 | const inst_ty = f.air.typeOfIndex(inst); | 5333 | const inst_ty = f.air.typeOfIndex(inst); |
| 4882 | const operand = try f.resolveInst(ty_op.operand); | 5334 | const operand = try f.resolveInst(ty_op.operand); |
| | 5335 | try reap(f, inst, &.{ty_op.operand}); |
| 4883 | const operand_ty = f.air.typeOf(ty_op.operand); | 5336 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 4884 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; | 5337 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; |
| 4885 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | 5338 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; |
| ... | @@ -4887,8 +5340,9 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu | ... | @@ -4887,8 +5340,9 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 4887 | if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) { | 5340 | if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) { |
| 4888 | if (!is_ptr) return CValue.none; | 5341 | if (!is_ptr) return CValue.none; |
| 4889 | | 5342 | |
| 4890 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 4891 | const w = f.object.writer(); | 5343 | const w = f.object.writer(); |
| | 5344 | const local = try f.allocLocal(inst, inst_ty); |
| | 5345 | try f.writeCValue(w, local, .Other); |
| 4892 | try w.writeAll(" = ("); | 5346 | try w.writeAll(" = ("); |
| 4893 | try f.renderTypecast(w, inst_ty); | 5347 | try f.renderTypecast(w, inst_ty); |
| 4894 | try w.writeByte(')'); | 5348 | try w.writeByte(')'); |
| ... | @@ -4898,7 +5352,8 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu | ... | @@ -4898,7 +5352,8 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 4898 | } | 5352 | } |
| 4899 | | 5353 | |
| 4900 | const writer = f.object.writer(); | 5354 | const writer = f.object.writer(); |
| 4901 | const local = try f.allocLocal(inst_ty, .Const); | 5355 | const local = try f.allocLocal(inst, inst_ty); |
| | 5356 | try f.writeCValue(writer, local, .Other); |
| 4902 | try writer.writeAll(" = "); | 5357 | try writer.writeAll(" = "); |
| 4903 | if (is_ptr) try writer.writeByte('&'); | 5358 | if (is_ptr) try writer.writeByte('&'); |
| 4904 | if (operand_is_ptr) | 5359 | if (operand_is_ptr) |
| ... | @@ -4910,24 +5365,40 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu | ... | @@ -4910,24 +5365,40 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 4910 | } | 5365 | } |
| 4911 | | 5366 | |
| 4912 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { | 5367 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4913 | if (f.liveness.isUnused(inst)) return CValue.none; | 5368 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5369 | |
| | 5370 | if (f.liveness.isUnused(inst)) { |
| | 5371 | try reap(f, inst, &.{ty_op.operand}); |
| | 5372 | return CValue.none; |
| | 5373 | } |
| 4914 | | 5374 | |
| 4915 | const inst_ty = f.air.typeOfIndex(inst); | 5375 | const inst_ty = f.air.typeOfIndex(inst); |
| 4916 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 4917 | const payload = try f.resolveInst(ty_op.operand); | 5376 | const payload = try f.resolveInst(ty_op.operand); |
| 4918 | if (inst_ty.optionalReprIsPayload()) return payload; | 5377 | try reap(f, inst, &.{ty_op.operand}); |
| | 5378 | const writer = f.object.writer(); |
| | 5379 | |
| | 5380 | if (inst_ty.optionalReprIsPayload()) { |
| | 5381 | const local = try f.allocLocal(inst, inst_ty); |
| | 5382 | try f.writeCValue(writer, local, .Other); |
| | 5383 | try writer.writeAll(" = "); |
| | 5384 | try f.writeCValue(writer, payload, .Other); |
| | 5385 | try writer.writeAll(";\n"); |
| | 5386 | return local; |
| | 5387 | } |
| 4919 | | 5388 | |
| 4920 | const payload_ty = f.air.typeOf(ty_op.operand); | 5389 | const payload_ty = f.air.typeOf(ty_op.operand); |
| 4921 | const target = f.object.dg.module.getTarget(); | 5390 | const target = f.object.dg.module.getTarget(); |
| 4922 | const is_array = lowersToArray(payload_ty, target); | 5391 | const is_array = lowersToArray(payload_ty, target); |
| 4923 | | 5392 | |
| 4924 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | 5393 | const local = try f.allocLocal(inst, inst_ty); |
| 4925 | const writer = f.object.writer(); | 5394 | if (!is_array) { |
| 4926 | try writer.writeAll(" = { .payload = "); | 5395 | try f.writeCValue(writer, local, .Other); |
| 4927 | try f.writeCValue(writer, if (is_array) CValue{ .undef = payload_ty } else payload, .Initializer); | 5396 | try writer.writeAll(".payload = "); |
| 4928 | try writer.writeAll(", .is_null = "); | 5397 | try f.writeCValue(writer, payload, .Other); |
| 4929 | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer); | 5398 | try writer.writeAll("; "); |
| 4930 | try writer.writeAll(" };\n"); | 5399 | } |
| | 5400 | try f.writeCValue(writer, local, .Other); |
| | 5401 | try writer.writeAll(".is_null = false;\n"); |
| 4931 | if (is_array) { | 5402 | if (is_array) { |
| 4932 | try writer.writeAll("memcpy("); | 5403 | try writer.writeAll("memcpy("); |
| 4933 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); | 5404 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); |
| ... | @@ -4941,21 +5412,35 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4941,21 +5412,35 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4941 | } | 5412 | } |
| 4942 | | 5413 | |
| 4943 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { | 5414 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4944 | if (f.liveness.isUnused(inst)) return CValue.none; | 5415 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5416 | if (f.liveness.isUnused(inst)) { |
| | 5417 | try reap(f, inst, &.{ty_op.operand}); |
| | 5418 | return CValue.none; |
| | 5419 | } |
| 4945 | | 5420 | |
| 4946 | const writer = f.object.writer(); | 5421 | const writer = f.object.writer(); |
| 4947 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 4948 | const operand = try f.resolveInst(ty_op.operand); | 5422 | const operand = try f.resolveInst(ty_op.operand); |
| | 5423 | try reap(f, inst, &.{ty_op.operand}); |
| 4949 | const error_union_ty = f.air.typeOfIndex(inst); | 5424 | const error_union_ty = f.air.typeOfIndex(inst); |
| 4950 | const payload_ty = error_union_ty.errorUnionPayload(); | 5425 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 4951 | if (!payload_ty.hasRuntimeBits()) return operand; | 5426 | const local = try f.allocLocal(inst, error_union_ty); |
| 4952 | | 5427 | |
| 4953 | const local = try f.allocLocal(error_union_ty, .Const); | 5428 | if (!payload_ty.hasRuntimeBits()) { |
| 4954 | try writer.writeAll(" = { .payload = "); | 5429 | try f.writeCValue(writer, local, .Other); |
| 4955 | try f.writeCValue(writer, .{ .undef = payload_ty }, .Initializer); | 5430 | try writer.writeAll(" = "); |
| 4956 | try writer.writeAll(", .error = "); | 5431 | try f.writeCValue(writer, operand, .Other); |
| 4957 | try f.writeCValue(writer, operand, .Initializer); | 5432 | try writer.writeAll(";\n"); |
| 4958 | try writer.writeAll(" };\n"); | 5433 | return local; |
| | 5434 | } |
| | 5435 | |
| | 5436 | { |
| | 5437 | // TODO: set the payload to undefined |
| | 5438 | //try f.writeCValue(writer, local, .Other); |
| | 5439 | } |
| | 5440 | try f.writeCValue(writer, local, .Other); |
| | 5441 | try writer.writeAll(".error = "); |
| | 5442 | try f.writeCValue(writer, operand, .Other); |
| | 5443 | try writer.writeAll(";\n"); |
| 4959 | return local; | 5444 | return local; |
| 4960 | } | 5445 | } |
| 4961 | | 5446 | |
| ... | @@ -4977,6 +5462,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4977,6 +5462,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4977 | | 5462 | |
| 4978 | return operand; | 5463 | return operand; |
| 4979 | } | 5464 | } |
| | 5465 | try reap(f, inst, &.{ty_op.operand}); |
| 4980 | try f.writeCValueDeref(writer, operand); | 5466 | try f.writeCValueDeref(writer, operand); |
| 4981 | try writer.writeAll(".error = "); | 5467 | try writer.writeAll(".error = "); |
| 4982 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other); | 5468 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other); |
| ... | @@ -4985,7 +5471,8 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4985,7 +5471,8 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4985 | // Then return the payload pointer (only if it is used) | 5471 | // Then return the payload pointer (only if it is used) |
| 4986 | if (f.liveness.isUnused(inst)) return CValue.none; | 5472 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4987 | | 5473 | |
| 4988 | const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); | 5474 | const local = try f.allocLocal(inst, f.air.typeOfIndex(inst)); |
| | 5475 | try f.writeCValue(writer, local, .Other); |
| 4989 | try writer.writeAll(" = &("); | 5476 | try writer.writeAll(" = &("); |
| 4990 | try f.writeCValueDeref(writer, operand); | 5477 | try f.writeCValueDeref(writer, operand); |
| 4991 | try writer.writeAll(").payload;\n"); | 5478 | try writer.writeAll(").payload;\n"); |
| ... | @@ -5008,24 +5495,30 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5008,24 +5495,30 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5008 | } | 5495 | } |
| 5009 | | 5496 | |
| 5010 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { | 5497 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5011 | if (f.liveness.isUnused(inst)) return CValue.none; | 5498 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5499 | if (f.liveness.isUnused(inst)) { |
| | 5500 | try reap(f, inst, &.{ty_op.operand}); |
| | 5501 | return CValue.none; |
| | 5502 | } |
| 5012 | | 5503 | |
| 5013 | const inst_ty = f.air.typeOfIndex(inst); | 5504 | const inst_ty = f.air.typeOfIndex(inst); |
| 5014 | const error_ty = inst_ty.errorUnionSet(); | | |
| 5015 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 5016 | const payload_ty = inst_ty.errorUnionPayload(); | 5505 | const payload_ty = inst_ty.errorUnionPayload(); |
| 5017 | const payload = try f.resolveInst(ty_op.operand); | 5506 | const payload = try f.resolveInst(ty_op.operand); |
| | 5507 | try reap(f, inst, &.{ty_op.operand}); |
| 5018 | | 5508 | |
| 5019 | const target = f.object.dg.module.getTarget(); | 5509 | const target = f.object.dg.module.getTarget(); |
| 5020 | const is_array = lowersToArray(payload_ty, target); | 5510 | const is_array = lowersToArray(payload_ty, target); |
| 5021 | | 5511 | |
| 5022 | const local = try f.allocLocal(inst_ty, if (is_array) .Mut else .Const); | | |
| 5023 | const writer = f.object.writer(); | 5512 | const writer = f.object.writer(); |
| 5024 | try writer.writeAll(" = { .payload = "); | 5513 | const local = try f.allocLocal(inst, inst_ty); |
| 5025 | try f.writeCValue(writer, if (is_array) CValue{ .undef = payload_ty } else payload, .Initializer); | 5514 | if (!is_array) { |
| 5026 | try writer.writeAll(", .error = "); | 5515 | try f.writeCValue(writer, local, .Other); |
| 5027 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Initializer); | 5516 | try writer.writeAll(".payload = "); |
| 5028 | try writer.writeAll(" };\n"); | 5517 | try f.writeCValue(writer, payload, .Other); |
| | 5518 | try writer.writeAll("; "); |
| | 5519 | } |
| | 5520 | try f.writeCValue(writer, local, .Other); |
| | 5521 | try writer.writeAll(".error = 0;\n"); |
| 5029 | if (is_array) { | 5522 | if (is_array) { |
| 5030 | try writer.writeAll("memcpy("); | 5523 | try writer.writeAll("memcpy("); |
| 5031 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); | 5524 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); |
| ... | @@ -5038,15 +5531,26 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5038,15 +5531,26 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5038 | return local; | 5531 | return local; |
| 5039 | } | 5532 | } |
| 5040 | | 5533 | |
| 5041 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !void { | 5534 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 5042 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 5535 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 5536 | |
| | 5537 | if (f.liveness.isUnused(inst)) { |
| | 5538 | try reap(f, inst, &.{un_op}); |
| | 5539 | return CValue.none; |
| | 5540 | } |
| | 5541 | |
| 5043 | const writer = f.object.writer(); | 5542 | const writer = f.object.writer(); |
| 5044 | const operand = try f.resolveInst(un_op); | 5543 | const operand = try f.resolveInst(un_op); |
| | 5544 | try reap(f, inst, &.{un_op}); |
| 5045 | const operand_ty = f.air.typeOf(un_op); | 5545 | const operand_ty = f.air.typeOf(un_op); |
| | 5546 | const local = try f.allocLocal(inst, Type.bool); |
| 5046 | const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty; | 5547 | const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty; |
| 5047 | const payload_ty = err_union_ty.errorUnionPayload(); | 5548 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 5048 | const error_ty = err_union_ty.errorUnionSet(); | 5549 | const error_ty = err_union_ty.errorUnionSet(); |
| 5049 | | 5550 | |
| | 5551 | try f.writeCValue(writer, local, .Other); |
| | 5552 | try writer.writeAll(" = "); |
| | 5553 | |
| 5050 | if (!error_ty.errorSetIsEmpty()) | 5554 | if (!error_ty.errorSetIsEmpty()) |
| 5051 | if (payload_ty.hasRuntimeBits()) | 5555 | if (payload_ty.hasRuntimeBits()) |
| 5052 | if (is_ptr) | 5556 | if (is_ptr) |
| ... | @@ -5061,20 +5565,27 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const | ... | @@ -5061,20 +5565,27 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 5061 | try writer.writeAll(operator); | 5565 | try writer.writeAll(operator); |
| 5062 | try writer.writeByte(' '); | 5566 | try writer.writeByte(' '); |
| 5063 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other); | 5567 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other); |
| | 5568 | try writer.writeAll(";\n"); |
| | 5569 | return local; |
| 5064 | } | 5570 | } |
| 5065 | | 5571 | |
| 5066 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { | 5572 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5067 | if (f.liveness.isUnused(inst)) | 5573 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5574 | |
| | 5575 | if (f.liveness.isUnused(inst)) { |
| | 5576 | try reap(f, inst, &.{ty_op.operand}); |
| 5068 | return CValue.none; | 5577 | return CValue.none; |
| | 5578 | } |
| 5069 | | 5579 | |
| | 5580 | const operand = try f.resolveInst(ty_op.operand); |
| | 5581 | try reap(f, inst, &.{ty_op.operand}); |
| 5070 | const inst_ty = f.air.typeOfIndex(inst); | 5582 | const inst_ty = f.air.typeOfIndex(inst); |
| 5071 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 5072 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 5073 | const writer = f.object.writer(); | 5583 | const writer = f.object.writer(); |
| 5074 | const operand = try f.resolveInst(ty_op.operand); | 5584 | const local = try f.allocLocal(inst, inst_ty); |
| | 5585 | try f.writeCValue(writer, local, .Other); |
| 5075 | const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen(); | 5586 | const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen(); |
| 5076 | | 5587 | |
| 5077 | try writer.writeAll(" = { .ptr = "); | 5588 | try writer.writeAll(".ptr = "); |
| 5078 | if (operand == .undef) { | 5589 | if (operand == .undef) { |
| 5079 | // Unfortunately, C does not support any equivalent to | 5590 | // Unfortunately, C does not support any equivalent to |
| 5080 | // &(*(void *)p)[0], although LLVM does via GetElementPtr | 5591 | // &(*(void *)p)[0], although LLVM does via GetElementPtr |
| ... | @@ -5088,16 +5599,23 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5088,16 +5599,23 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5088 | | 5599 | |
| 5089 | var len_pl: Value.Payload.U64 = .{ .base = .{ .tag = .int_u64 }, .data = array_len }; | 5600 | var len_pl: Value.Payload.U64 = .{ .base = .{ .tag = .int_u64 }, .data = array_len }; |
| 5090 | const len_val = Value.initPayload(&len_pl.base); | 5601 | const len_val = Value.initPayload(&len_pl.base); |
| 5091 | try writer.print(", .len = {} }};\n", .{try f.fmtIntLiteral(Type.usize, len_val)}); | 5602 | try writer.writeAll("; "); |
| | 5603 | try f.writeCValue(writer, local, .Other); |
| | 5604 | try writer.print(".len = {};\n", .{try f.fmtIntLiteral(Type.usize, len_val)}); |
| 5092 | return local; | 5605 | return local; |
| 5093 | } | 5606 | } |
| 5094 | | 5607 | |
| 5095 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | 5608 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5096 | if (f.liveness.isUnused(inst)) return CValue.none; | 5609 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 5610 | |
| | 5611 | if (f.liveness.isUnused(inst)) { |
| | 5612 | try reap(f, inst, &.{ty_op.operand}); |
| | 5613 | return CValue.none; |
| | 5614 | } |
| 5097 | | 5615 | |
| 5098 | const inst_ty = f.air.typeOfIndex(inst); | 5616 | const inst_ty = f.air.typeOfIndex(inst); |
| 5099 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 5100 | const operand = try f.resolveInst(ty_op.operand); | 5617 | const operand = try f.resolveInst(ty_op.operand); |
| | 5618 | try reap(f, inst, &.{ty_op.operand}); |
| 5101 | const operand_ty = f.air.typeOf(ty_op.operand); | 5619 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 5102 | const target = f.object.dg.module.getTarget(); | 5620 | const target = f.object.dg.module.getTarget(); |
| 5103 | const operation = if (inst_ty.isRuntimeFloat() and operand_ty.isRuntimeFloat()) | 5621 | const operation = if (inst_ty.isRuntimeFloat() and operand_ty.isRuntimeFloat()) |
| ... | @@ -5109,8 +5627,9 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5109,8 +5627,9 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5109 | else | 5627 | else |
| 5110 | unreachable; | 5628 | unreachable; |
| 5111 | | 5629 | |
| 5112 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 5113 | const writer = f.object.writer(); | 5630 | const writer = f.object.writer(); |
| | 5631 | const local = try f.allocLocal(inst, inst_ty); |
| | 5632 | try f.writeCValue(writer, local, .Other); |
| 5114 | | 5633 | |
| 5115 | try writer.writeAll(" = "); | 5634 | try writer.writeAll(" = "); |
| 5116 | if (inst_ty.isInt() and operand_ty.isRuntimeFloat()) { | 5635 | if (inst_ty.isInt() and operand_ty.isRuntimeFloat()) { |
| ... | @@ -5134,16 +5653,27 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5134,16 +5653,27 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5134 | return local; | 5653 | return local; |
| 5135 | } | 5654 | } |
| 5136 | | 5655 | |
| 5137 | fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !void { | 5656 | fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5138 | const inst_ty = f.air.typeOfIndex(inst); | | |
| 5139 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 5657 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 5140 | const writer = f.object.writer(); | 5658 | |
| | 5659 | if (f.liveness.isUnused(inst)) { |
| | 5660 | try reap(f, inst, &.{un_op}); |
| | 5661 | return CValue.none; |
| | 5662 | } |
| | 5663 | |
| 5141 | const operand = try f.resolveInst(un_op); | 5664 | const operand = try f.resolveInst(un_op); |
| | 5665 | try reap(f, inst, &.{un_op}); |
| | 5666 | const inst_ty = f.air.typeOfIndex(inst); |
| | 5667 | const writer = f.object.writer(); |
| | 5668 | const local = try f.allocLocal(inst, inst_ty); |
| | 5669 | try f.writeCValue(writer, local, .Other); |
| 5142 | | 5670 | |
| 5143 | try writer.writeAll("("); | 5671 | try writer.writeAll(" = ("); |
| 5144 | try f.renderTypecast(writer, inst_ty); | 5672 | try f.renderTypecast(writer, inst_ty); |
| 5145 | try writer.writeByte(')'); | 5673 | try writer.writeByte(')'); |
| 5146 | try f.writeCValue(writer, operand, .Other); | 5674 | try f.writeCValue(writer, operand, .Other); |
| | 5675 | try writer.writeAll(";\n"); |
| | 5676 | return local; |
| 5147 | } | 5677 | } |
| 5148 | | 5678 | |
| 5149 | fn airUnBuiltinCall( | 5679 | fn airUnBuiltinCall( |
| ... | @@ -5151,19 +5681,31 @@ fn airUnBuiltinCall( | ... | @@ -5151,19 +5681,31 @@ fn airUnBuiltinCall( |
| 5151 | inst: Air.Inst.Index, | 5681 | inst: Air.Inst.Index, |
| 5152 | operation: []const u8, | 5682 | operation: []const u8, |
| 5153 | info: BuiltinInfo, | 5683 | info: BuiltinInfo, |
| 5154 | ) !void { | 5684 | ) !CValue { |
| 5155 | const operand = f.air.instructions.items(.data)[inst].ty_op.operand; | 5685 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 5156 | const operand_ty = f.air.typeOf(operand); | 5686 | |
| | 5687 | if (f.liveness.isUnused(inst)) { |
| | 5688 | try reap(f, inst, &.{ty_op.operand}); |
| | 5689 | return CValue.none; |
| | 5690 | } |
| | 5691 | |
| | 5692 | const operand = try f.resolveInst(ty_op.operand); |
| | 5693 | try reap(f, inst, &.{ty_op.operand}); |
| | 5694 | const inst_ty = f.air.typeOfIndex(inst); |
| | 5695 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 5157 | | 5696 | |
| 5158 | const writer = f.object.writer(); | 5697 | const writer = f.object.writer(); |
| 5159 | try writer.writeAll("zig_"); | 5698 | const local = try f.allocLocal(inst, inst_ty); |
| | 5699 | try f.writeCValue(writer, local, .Other); |
| | 5700 | try writer.writeAll(" = zig_"); |
| 5160 | try writer.writeAll(operation); | 5701 | try writer.writeAll(operation); |
| 5161 | try writer.writeByte('_'); | 5702 | try writer.writeByte('_'); |
| 5162 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); | 5703 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| 5163 | try writer.writeByte('('); | 5704 | try writer.writeByte('('); |
| 5164 | try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument); | 5705 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 5165 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); | 5706 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); |
| 5166 | try writer.writeAll(")"); | 5707 | try writer.writeAll(");\n"); |
| | 5708 | return local; |
| 5167 | } | 5709 | } |
| 5168 | | 5710 | |
| 5169 | fn airBinBuiltinCall( | 5711 | fn airBinBuiltinCall( |
| ... | @@ -5171,99 +5713,131 @@ fn airBinBuiltinCall( | ... | @@ -5171,99 +5713,131 @@ fn airBinBuiltinCall( |
| 5171 | inst: Air.Inst.Index, | 5713 | inst: Air.Inst.Index, |
| 5172 | operation: []const u8, | 5714 | operation: []const u8, |
| 5173 | info: BuiltinInfo, | 5715 | info: BuiltinInfo, |
| 5174 | ) !void { | 5716 | ) !CValue { |
| 5175 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 5717 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| | 5718 | |
| | 5719 | if (f.liveness.isUnused(inst)) { |
| | 5720 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 5721 | return CValue.none; |
| | 5722 | } |
| | 5723 | |
| | 5724 | const lhs = try f.resolveInst(bin_op.lhs); |
| | 5725 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 5726 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 5727 | |
| | 5728 | const inst_ty = f.air.typeOfIndex(inst); |
| 5176 | const operand_ty = f.air.typeOf(bin_op.lhs); | 5729 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 5177 | | 5730 | |
| 5178 | const writer = f.object.writer(); | 5731 | const writer = f.object.writer(); |
| 5179 | try writer.writeAll("zig_"); | 5732 | const local = try f.allocLocal(inst, inst_ty); |
| | 5733 | try f.writeCValue(writer, local, .Other); |
| | 5734 | try writer.writeAll(" = zig_"); |
| 5180 | try writer.writeAll(operation); | 5735 | try writer.writeAll(operation); |
| 5181 | try writer.writeByte('_'); | 5736 | try writer.writeByte('_'); |
| 5182 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); | 5737 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| 5183 | try writer.writeByte('('); | 5738 | try writer.writeByte('('); |
| 5184 | try f.writeCValue(writer, try f.resolveInst(bin_op.lhs), .FunctionArgument); | 5739 | try f.writeCValue(writer, lhs, .FunctionArgument); |
| 5185 | try writer.writeAll(", "); | 5740 | try writer.writeAll(", "); |
| 5186 | try f.writeCValue(writer, try f.resolveInst(bin_op.rhs), .FunctionArgument); | 5741 | try f.writeCValue(writer, rhs, .FunctionArgument); |
| 5187 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); | 5742 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); |
| 5188 | try writer.writeAll(")"); | 5743 | try writer.writeAll(");\n"); |
| | 5744 | return local; |
| 5189 | } | 5745 | } |
| 5190 | | 5746 | |
| 5191 | fn airCmpBuiltinCall( | 5747 | fn cmpBuiltinCall( |
| 5192 | f: *Function, | 5748 | f: *Function, |
| 5193 | inst: Air.Inst.Index, | 5749 | inst: Air.Inst.Index, |
| 5194 | operator: []const u8, | 5750 | operator: []const u8, |
| 5195 | operation: []const u8, | 5751 | operation: []const u8, |
| 5196 | ) !void { | 5752 | ) !CValue { |
| | 5753 | const inst_ty = f.air.typeOfIndex(inst); |
| 5197 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 5754 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 5198 | const operand_ty = f.air.typeOf(bin_op.lhs); | 5755 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 5199 | | 5756 | |
| | 5757 | const lhs = try f.resolveInst(bin_op.lhs); |
| | 5758 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 5759 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 5760 | |
| 5200 | const writer = f.object.writer(); | 5761 | const writer = f.object.writer(); |
| 5201 | try writer.writeAll("zig_"); | 5762 | const local = try f.allocLocal(inst, inst_ty); |
| | 5763 | try f.writeCValue(writer, local, .Other); |
| | 5764 | try writer.writeAll(" = zig_"); |
| 5202 | try writer.writeAll(operation); | 5765 | try writer.writeAll(operation); |
| 5203 | try writer.writeByte('_'); | 5766 | try writer.writeByte('_'); |
| 5204 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); | 5767 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| 5205 | try writer.writeByte('('); | 5768 | try writer.writeByte('('); |
| 5206 | try f.writeCValue(writer, try f.resolveInst(bin_op.lhs), .FunctionArgument); | 5769 | try f.writeCValue(writer, lhs, .FunctionArgument); |
| 5207 | try writer.writeAll(", "); | 5770 | try writer.writeAll(", "); |
| 5208 | try f.writeCValue(writer, try f.resolveInst(bin_op.rhs), .FunctionArgument); | 5771 | try f.writeCValue(writer, rhs, .FunctionArgument); |
| 5209 | try writer.print(") {s} {}", .{ operator, try f.fmtIntLiteral(Type.initTag(.i32), Value.zero) }); | 5772 | try writer.print(") {s} {};\n", .{ operator, try f.fmtIntLiteral(Type.initTag(.i32), Value.zero) }); |
| | 5773 | return local; |
| 5210 | } | 5774 | } |
| 5211 | | 5775 | |
| 5212 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { | 5776 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 5213 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 5777 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 5214 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; | 5778 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 5215 | const inst_ty = f.air.typeOfIndex(inst); | 5779 | const inst_ty = f.air.typeOfIndex(inst); |
| 5216 | const is_struct = !inst_ty.isPtrLikeOptional(); | | |
| 5217 | const ptr_ty = f.air.typeOf(extra.ptr); | | |
| 5218 | const ptr = try f.resolveInst(extra.ptr); | 5780 | const ptr = try f.resolveInst(extra.ptr); |
| 5219 | const expected_value = try f.resolveInst(extra.expected_value); | 5781 | const expected_value = try f.resolveInst(extra.expected_value); |
| 5220 | const new_value = try f.resolveInst(extra.new_value); | 5782 | const new_value = try f.resolveInst(extra.new_value); |
| | 5783 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 5221 | const writer = f.object.writer(); | 5784 | const writer = f.object.writer(); |
| 5222 | | 5785 | const ptr_ty = f.air.typeOf(extra.ptr); |
| 5223 | const local = try f.allocLocal(inst_ty, .Mut); | 5786 | const local = try f.allocLocal(inst, inst_ty); |
| 5224 | try writer.writeAll(" = "); | 5787 | if (inst_ty.isPtrLikeOptional()) { |
| 5225 | if (is_struct) try writer.writeAll("{ .payload = "); | | |
| 5226 | try f.writeCValue(writer, expected_value, .Initializer); | | |
| 5227 | if (is_struct) { | | |
| 5228 | try writer.writeAll(", .is_null = "); | | |
| 5229 | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer); | | |
| 5230 | try writer.writeAll(" }"); | | |
| 5231 | } | | |
| 5232 | try writer.writeAll(";\n"); | | |
| 5233 | | | |
| 5234 | if (is_struct) { | | |
| 5235 | try f.writeCValue(writer, local, .Other); | 5788 | try f.writeCValue(writer, local, .Other); |
| 5236 | try writer.writeAll(".is_null = "); | 5789 | try writer.writeAll(" = "); |
| 5237 | } else { | 5790 | try f.writeCValue(writer, expected_value, .Initializer); |
| | 5791 | try writer.writeAll(";\n"); |
| 5238 | try writer.writeAll("if ("); | 5792 | try writer.writeAll("if ("); |
| 5239 | } | 5793 | try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 5240 | try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); | 5794 | try f.renderTypecast(writer, ptr_ty.elemType()); |
| 5241 | try f.renderTypecast(writer, ptr_ty.elemType()); | 5795 | try writer.writeByte(')'); |
| 5242 | try writer.writeByte(')'); | 5796 | if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile"); |
| 5243 | if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile"); | 5797 | try writer.writeAll(" *)"); |
| 5244 | try writer.writeAll(" *)"); | 5798 | try f.writeCValue(writer, ptr, .Other); |
| 5245 | try f.writeCValue(writer, ptr, .Other); | 5799 | try writer.writeAll(", "); |
| 5246 | try writer.writeAll(", "); | | |
| 5247 | if (is_struct) | | |
| 5248 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }) | | |
| 5249 | else | | |
| 5250 | try f.writeCValue(writer, local, .FunctionArgument); | 5800 | try f.writeCValue(writer, local, .FunctionArgument); |
| 5251 | try writer.writeAll(", "); | 5801 | try writer.writeAll(", "); |
| 5252 | try f.writeCValue(writer, new_value, .FunctionArgument); | 5802 | try f.writeCValue(writer, new_value, .FunctionArgument); |
| 5253 | try writer.writeAll(", "); | 5803 | try writer.writeAll(", "); |
| 5254 | try writeMemoryOrder(writer, extra.successOrder()); | 5804 | try writeMemoryOrder(writer, extra.successOrder()); |
| 5255 | try writer.writeAll(", "); | 5805 | try writer.writeAll(", "); |
| 5256 | try writeMemoryOrder(writer, extra.failureOrder()); | 5806 | try writeMemoryOrder(writer, extra.failureOrder()); |
| 5257 | try writer.writeByte(')'); | 5807 | try writer.writeByte(')'); |
| 5258 | if (is_struct) { | | |
| 5259 | try writer.writeAll(";\n"); | | |
| 5260 | } else { | | |
| 5261 | try writer.writeAll(") {\n"); | 5808 | try writer.writeAll(") {\n"); |
| 5262 | f.object.indent_writer.pushIndent(); | 5809 | f.object.indent_writer.pushIndent(); |
| 5263 | try f.writeCValue(writer, local, .Other); | 5810 | try f.writeCValue(writer, local, .Other); |
| 5264 | try writer.writeAll(" = NULL;\n"); | 5811 | try writer.writeAll(" = NULL;\n"); |
| 5265 | f.object.indent_writer.popIndent(); | 5812 | f.object.indent_writer.popIndent(); |
| 5266 | try writer.writeAll("}\n"); | 5813 | try writer.writeAll("}\n"); |
| | 5814 | } else { |
| | 5815 | try f.writeCValue(writer, local, .Other); |
| | 5816 | try writer.writeAll(".payload = "); |
| | 5817 | try f.writeCValue(writer, expected_value, .Other); |
| | 5818 | try writer.writeAll(";\n"); |
| | 5819 | try f.writeCValue(writer, local, .Other); |
| | 5820 | try writer.print(".is_null = zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| | 5821 | try f.renderTypecast(writer, ptr_ty.elemType()); |
| | 5822 | try writer.writeByte(')'); |
| | 5823 | if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile"); |
| | 5824 | try writer.writeAll(" *)"); |
| | 5825 | try f.writeCValue(writer, ptr, .Other); |
| | 5826 | try writer.writeAll(", "); |
| | 5827 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); |
| | 5828 | try writer.writeAll(", "); |
| | 5829 | try f.writeCValue(writer, new_value, .FunctionArgument); |
| | 5830 | try writer.writeAll(", "); |
| | 5831 | try writeMemoryOrder(writer, extra.successOrder()); |
| | 5832 | try writer.writeAll(", "); |
| | 5833 | try writeMemoryOrder(writer, extra.failureOrder()); |
| | 5834 | try writer.writeByte(')'); |
| | 5835 | try writer.writeAll(";\n"); |
| | 5836 | } |
| | 5837 | |
| | 5838 | if (f.liveness.isUnused(inst)) { |
| | 5839 | try freeLocal(f, inst, local.local, 0); |
| | 5840 | return CValue.none; |
| 5267 | } | 5841 | } |
| 5268 | | 5842 | |
| 5269 | return local; | 5843 | return local; |
| ... | @@ -5276,8 +5850,10 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5276,8 +5850,10 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5276 | const ptr_ty = f.air.typeOf(pl_op.operand); | 5850 | const ptr_ty = f.air.typeOf(pl_op.operand); |
| 5277 | const ptr = try f.resolveInst(pl_op.operand); | 5851 | const ptr = try f.resolveInst(pl_op.operand); |
| 5278 | const operand = try f.resolveInst(extra.operand); | 5852 | const operand = try f.resolveInst(extra.operand); |
| 5279 | const local = try f.allocLocal(inst_ty, .Const); | 5853 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); |
| 5280 | const writer = f.object.writer(); | 5854 | const writer = f.object.writer(); |
| | 5855 | const local = try f.allocLocal(inst, inst_ty); |
| | 5856 | try f.writeCValue(writer, local, .Other); |
| 5281 | | 5857 | |
| 5282 | try writer.print(" = zig_atomicrmw_{s}((", .{toAtomicRmwSuffix(extra.op())}); | 5858 | try writer.print(" = zig_atomicrmw_{s}((", .{toAtomicRmwSuffix(extra.op())}); |
| 5283 | switch (extra.op()) { | 5859 | switch (extra.op()) { |
| ... | @@ -5300,19 +5876,27 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5300,19 +5876,27 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5300 | try writeMemoryOrder(writer, extra.ordering()); | 5876 | try writeMemoryOrder(writer, extra.ordering()); |
| 5301 | try writer.writeAll(");\n"); | 5877 | try writer.writeAll(");\n"); |
| 5302 | | 5878 | |
| | 5879 | if (f.liveness.isUnused(inst)) { |
| | 5880 | try freeLocal(f, inst, local.local, 0); |
| | 5881 | return CValue.none; |
| | 5882 | } |
| | 5883 | |
| 5303 | return local; | 5884 | return local; |
| 5304 | } | 5885 | } |
| 5305 | | 5886 | |
| 5306 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { | 5887 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5307 | const atomic_load = f.air.instructions.items(.data)[inst].atomic_load; | 5888 | const atomic_load = f.air.instructions.items(.data)[inst].atomic_load; |
| 5308 | const ptr = try f.resolveInst(atomic_load.ptr); | 5889 | const ptr = try f.resolveInst(atomic_load.ptr); |
| | 5890 | try reap(f, inst, &.{atomic_load.ptr}); |
| 5309 | const ptr_ty = f.air.typeOf(atomic_load.ptr); | 5891 | const ptr_ty = f.air.typeOf(atomic_load.ptr); |
| 5310 | if (!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst)) | 5892 | if (!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst)) { |
| 5311 | return CValue.none; | 5893 | return CValue.none; |
| | 5894 | } |
| 5312 | | 5895 | |
| 5313 | const inst_ty = f.air.typeOfIndex(inst); | 5896 | const inst_ty = f.air.typeOfIndex(inst); |
| 5314 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 5315 | const writer = f.object.writer(); | 5897 | const writer = f.object.writer(); |
| | 5898 | const local = try f.allocLocal(inst, inst_ty); |
| | 5899 | try f.writeCValue(writer, local, .Other); |
| 5316 | | 5900 | |
| 5317 | try writer.writeAll(" = zig_atomic_load((zig_atomic("); | 5901 | try writer.writeAll(" = zig_atomic_load((zig_atomic("); |
| 5318 | try f.renderTypecast(writer, ptr_ty.elemType()); | 5902 | try f.renderTypecast(writer, ptr_ty.elemType()); |
| ... | @@ -5332,6 +5916,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa | ... | @@ -5332,6 +5916,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 5332 | const ptr_ty = f.air.typeOf(bin_op.lhs); | 5916 | const ptr_ty = f.air.typeOf(bin_op.lhs); |
| 5333 | const ptr = try f.resolveInst(bin_op.lhs); | 5917 | const ptr = try f.resolveInst(bin_op.lhs); |
| 5334 | const element = try f.resolveInst(bin_op.rhs); | 5918 | const element = try f.resolveInst(bin_op.rhs); |
| | 5919 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 5335 | const writer = f.object.writer(); | 5920 | const writer = f.object.writer(); |
| 5336 | | 5921 | |
| 5337 | try writer.writeAll("zig_atomic_store((zig_atomic("); | 5922 | try writer.writeAll("zig_atomic_store((zig_atomic("); |
| ... | @@ -5354,6 +5939,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5354,6 +5939,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5354 | const dest_ptr = try f.resolveInst(pl_op.operand); | 5939 | const dest_ptr = try f.resolveInst(pl_op.operand); |
| 5355 | const value = try f.resolveInst(extra.lhs); | 5940 | const value = try f.resolveInst(extra.lhs); |
| 5356 | const len = try f.resolveInst(extra.rhs); | 5941 | const len = try f.resolveInst(extra.rhs); |
| | 5942 | try reap(f, inst, &.{ pl_op.operand, extra.lhs, extra.rhs }); |
| 5357 | | 5943 | |
| 5358 | const writer = f.object.writer(); | 5944 | const writer = f.object.writer(); |
| 5359 | if (dest_ty.isVolatilePtr()) { | 5945 | if (dest_ty.isVolatilePtr()) { |
| ... | @@ -5362,7 +5948,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5362,7 +5948,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5362 | const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base); | 5948 | const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base); |
| 5363 | | 5949 | |
| 5364 | try writer.writeAll("for ("); | 5950 | try writer.writeAll("for ("); |
| 5365 | const index = try f.allocLocal(Type.usize, .Mut); | 5951 | const index = try f.allocLocal(inst, Type.usize); |
| | 5952 | try f.writeCValue(writer, index, .Other); |
| 5366 | try writer.writeAll(" = "); | 5953 | try writer.writeAll(" = "); |
| 5367 | try f.object.dg.renderValue(writer, Type.usize, Value.zero, .Initializer); | 5954 | try f.object.dg.renderValue(writer, Type.usize, Value.zero, .Initializer); |
| 5368 | try writer.writeAll("; "); | 5955 | try writer.writeAll("; "); |
| ... | @@ -5383,6 +5970,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5383,6 +5970,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5383 | try f.writeCValue(writer, value, .FunctionArgument); | 5970 | try f.writeCValue(writer, value, .FunctionArgument); |
| 5384 | try writer.writeAll(";\n"); | 5971 | try writer.writeAll(";\n"); |
| 5385 | | 5972 | |
| | 5973 | try freeLocal(f, inst, index.local, 0); |
| | 5974 | |
| 5386 | return CValue.none; | 5975 | return CValue.none; |
| 5387 | } | 5976 | } |
| 5388 | | 5977 | |
| ... | @@ -5403,6 +5992,7 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5403,6 +5992,7 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5403 | const dest_ptr = try f.resolveInst(pl_op.operand); | 5992 | const dest_ptr = try f.resolveInst(pl_op.operand); |
| 5404 | const src_ptr = try f.resolveInst(extra.lhs); | 5993 | const src_ptr = try f.resolveInst(extra.lhs); |
| 5405 | const len = try f.resolveInst(extra.rhs); | 5994 | const len = try f.resolveInst(extra.rhs); |
| | 5995 | try reap(f, inst, &.{ pl_op.operand, extra.lhs, extra.rhs }); |
| 5406 | const writer = f.object.writer(); | 5996 | const writer = f.object.writer(); |
| 5407 | | 5997 | |
| 5408 | try writer.writeAll("memcpy("); | 5998 | try writer.writeAll("memcpy("); |
| ... | @@ -5420,6 +6010,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5420,6 +6010,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5420 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 6010 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 5421 | const union_ptr = try f.resolveInst(bin_op.lhs); | 6011 | const union_ptr = try f.resolveInst(bin_op.lhs); |
| 5422 | const new_tag = try f.resolveInst(bin_op.rhs); | 6012 | const new_tag = try f.resolveInst(bin_op.rhs); |
| | 6013 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 5423 | const writer = f.object.writer(); | 6014 | const writer = f.object.writer(); |
| 5424 | | 6015 | |
| 5425 | const union_ty = f.air.typeOf(bin_op.lhs).childType(); | 6016 | const union_ty = f.air.typeOf(bin_op.lhs).childType(); |
| ... | @@ -5436,53 +6027,94 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5436,53 +6027,94 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5436 | return CValue.none; | 6027 | return CValue.none; |
| 5437 | } | 6028 | } |
| 5438 | | 6029 | |
| 5439 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void { | 6030 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5440 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | 6031 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 5441 | const un_ty = f.air.typeOf(ty_op.operand); | 6032 | |
| 5442 | const writer = f.object.writer(); | 6033 | if (f.liveness.isUnused(inst)) { |
| | 6034 | try reap(f, inst, &.{ty_op.operand}); |
| | 6035 | return CValue.none; |
| | 6036 | } |
| | 6037 | |
| 5443 | const operand = try f.resolveInst(ty_op.operand); | 6038 | const operand = try f.resolveInst(ty_op.operand); |
| | 6039 | try reap(f, inst, &.{ty_op.operand}); |
| | 6040 | |
| | 6041 | const un_ty = f.air.typeOf(ty_op.operand); |
| 5444 | | 6042 | |
| 5445 | const target = f.object.dg.module.getTarget(); | 6043 | const target = f.object.dg.module.getTarget(); |
| 5446 | const layout = un_ty.unionGetLayout(target); | 6044 | const layout = un_ty.unionGetLayout(target); |
| 5447 | assert(layout.tag_size != 0); | 6045 | if (layout.tag_size == 0) return CValue.none; |
| 5448 | | 6046 | |
| | 6047 | const inst_ty = f.air.typeOfIndex(inst); |
| | 6048 | const writer = f.object.writer(); |
| | 6049 | const local = try f.allocLocal(inst, inst_ty); |
| | 6050 | try f.writeCValue(writer, local, .Other); |
| | 6051 | |
| | 6052 | try writer.writeAll(" = "); |
| 5449 | try f.writeCValue(writer, operand, .Other); | 6053 | try f.writeCValue(writer, operand, .Other); |
| 5450 | try writer.writeAll(".tag"); | 6054 | try writer.writeAll(".tag;\n"); |
| | 6055 | return local; |
| 5451 | } | 6056 | } |
| 5452 | | 6057 | |
| 5453 | fn airTagName(f: *Function, inst: Air.Inst.Index) !void { | 6058 | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5454 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 6059 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 6060 | |
| | 6061 | if (f.liveness.isUnused(inst)) { |
| | 6062 | try reap(f, inst, &.{un_op}); |
| | 6063 | return CValue.none; |
| | 6064 | } |
| | 6065 | |
| | 6066 | const inst_ty = f.air.typeOfIndex(inst); |
| 5455 | const enum_ty = f.air.typeOf(un_op); | 6067 | const enum_ty = f.air.typeOf(un_op); |
| 5456 | const operand = try f.resolveInst(un_op); | 6068 | const operand = try f.resolveInst(un_op); |
| | 6069 | try reap(f, inst, &.{un_op}); |
| 5457 | | 6070 | |
| 5458 | const writer = f.object.writer(); | 6071 | const writer = f.object.writer(); |
| 5459 | try writer.print("{s}(", .{try f.object.dg.getTagNameFn(enum_ty)}); | 6072 | const local = try f.allocLocal(inst, inst_ty); |
| | 6073 | try f.writeCValue(writer, local, .Other); |
| | 6074 | try writer.print(" = {s}(", .{try f.object.dg.getTagNameFn(enum_ty)}); |
| 5460 | try f.writeCValue(writer, operand, .Other); | 6075 | try f.writeCValue(writer, operand, .Other); |
| 5461 | try writer.writeAll(")"); | 6076 | try writer.writeAll(");\n"); |
| | 6077 | |
| | 6078 | return local; |
| 5462 | } | 6079 | } |
| 5463 | | 6080 | |
| 5464 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !void { | 6081 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5465 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 6082 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 6083 | |
| | 6084 | if (f.liveness.isUnused(inst)) { |
| | 6085 | try reap(f, inst, &.{un_op}); |
| | 6086 | return CValue.none; |
| | 6087 | } |
| | 6088 | |
| 5466 | const writer = f.object.writer(); | 6089 | const writer = f.object.writer(); |
| | 6090 | const inst_ty = f.air.typeOfIndex(inst); |
| 5467 | const operand = try f.resolveInst(un_op); | 6091 | const operand = try f.resolveInst(un_op); |
| | 6092 | try reap(f, inst, &.{un_op}); |
| | 6093 | const local = try f.allocLocal(inst, inst_ty); |
| | 6094 | try f.writeCValue(writer, local, .Other); |
| 5468 | | 6095 | |
| 5469 | try writer.writeAll("zig_errorName["); | 6096 | try writer.writeAll(" = zig_errorName["); |
| 5470 | try f.writeCValue(writer, operand, .Other); | 6097 | try f.writeCValue(writer, operand, .Other); |
| 5471 | try writer.writeAll("]"); | 6098 | try writer.writeAll("];\n"); |
| | 6099 | return local; |
| 5472 | } | 6100 | } |
| 5473 | | 6101 | |
| 5474 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { | 6102 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5475 | if (f.liveness.isUnused(inst)) return CValue.none; | 6103 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| | 6104 | if (f.liveness.isUnused(inst)) { |
| | 6105 | try reap(f, inst, &.{ty_op.operand}); |
| | 6106 | return CValue.none; |
| | 6107 | } |
| 5476 | | 6108 | |
| 5477 | const inst_ty = f.air.typeOfIndex(inst); | 6109 | const inst_ty = f.air.typeOfIndex(inst); |
| 5478 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | | |
| 5479 | const operand = try f.resolveInst(ty_op.operand); | 6110 | const operand = try f.resolveInst(ty_op.operand); |
| | 6111 | try reap(f, inst, &.{ty_op.operand}); |
| 5480 | const writer = f.object.writer(); | 6112 | const writer = f.object.writer(); |
| 5481 | const local = try f.allocLocal(inst_ty, .Const); | 6113 | const local = try f.allocLocal(inst, inst_ty); |
| | 6114 | try f.writeCValue(writer, local, .Other); |
| 5482 | try writer.writeAll(" = "); | 6115 | try writer.writeAll(" = "); |
| 5483 | | 6116 | |
| 5484 | _ = operand; | 6117 | _ = operand; |
| 5485 | _ = local; | | |
| 5486 | return f.fail("TODO: C backend: implement airSplat", .{}); | 6118 | return f.fail("TODO: C backend: implement airSplat", .{}); |
| 5487 | } | 6119 | } |
| 5488 | | 6120 | |
| ... | @@ -5499,12 +6131,17 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5499,12 +6131,17 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5499 | } | 6131 | } |
| 5500 | | 6132 | |
| 5501 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | 6133 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5502 | if (f.liveness.isUnused(inst)) return CValue.none; | 6134 | const reduce = f.air.instructions.items(.data)[inst].reduce; |
| | 6135 | |
| | 6136 | if (f.liveness.isUnused(inst)) { |
| | 6137 | try reap(f, inst, &.{reduce.operand}); |
| | 6138 | return CValue.none; |
| | 6139 | } |
| 5503 | | 6140 | |
| 5504 | const target = f.object.dg.module.getTarget(); | 6141 | const target = f.object.dg.module.getTarget(); |
| 5505 | const scalar_ty = f.air.typeOfIndex(inst); | 6142 | const scalar_ty = f.air.typeOfIndex(inst); |
| 5506 | const reduce = f.air.instructions.items(.data)[inst].reduce; | | |
| 5507 | const operand = try f.resolveInst(reduce.operand); | 6143 | const operand = try f.resolveInst(reduce.operand); |
| | 6144 | try reap(f, inst, &.{reduce.operand}); |
| 5508 | const operand_ty = f.air.typeOf(reduce.operand); | 6145 | const operand_ty = f.air.typeOf(reduce.operand); |
| 5509 | const vector_len = operand_ty.vectorLen(); | 6146 | const vector_len = operand_ty.vectorLen(); |
| 5510 | const writer = f.object.writer(); | 6147 | const writer = f.object.writer(); |
| ... | @@ -5581,10 +6218,12 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5581,10 +6218,12 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5581 | // } | 6218 | // } |
| 5582 | // break :reduce accum; | 6219 | // break :reduce accum; |
| 5583 | // } | 6220 | // } |
| 5584 | const it = try f.allocLocal(Type.usize, .Mut); | 6221 | const it = try f.allocLocal(inst, Type.usize); |
| | 6222 | try f.writeCValue(writer, it, .Other); |
| 5585 | try writer.writeAll(" = 0;\n"); | 6223 | try writer.writeAll(" = 0;\n"); |
| 5586 | | 6224 | |
| 5587 | const accum = try f.allocLocal(scalar_ty, .Mut); | 6225 | const accum = try f.allocLocal(inst, scalar_ty); |
| | 6226 | try f.writeCValue(writer, accum, .Other); |
| 5588 | try writer.writeAll(" = "); | 6227 | try writer.writeAll(" = "); |
| 5589 | | 6228 | |
| 5590 | const init_val = switch (reduce.operation) { | 6229 | const init_val = switch (reduce.operation) { |
| ... | @@ -5604,7 +6243,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5604,7 +6243,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5604 | try writer.writeAll(init_val); | 6243 | try writer.writeAll(init_val); |
| 5605 | try writer.writeAll(";"); | 6244 | try writer.writeAll(";"); |
| 5606 | try f.object.indent_writer.insertNewline(); | 6245 | try f.object.indent_writer.insertNewline(); |
| 5607 | try writer.writeAll("for(;"); | 6246 | try writer.writeAll("for (;"); |
| 5608 | try f.writeCValue(writer, it, .Other); | 6247 | try f.writeCValue(writer, it, .Other); |
| 5609 | try writer.print("<{d};++", .{vector_len}); | 6248 | try writer.print("<{d};++", .{vector_len}); |
| 5610 | try f.writeCValue(writer, it, .Other); | 6249 | try f.writeCValue(writer, it, .Other); |
| ... | @@ -5647,44 +6286,57 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5647,44 +6286,57 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5647 | | 6286 | |
| 5648 | try writer.writeAll(";\n"); | 6287 | try writer.writeAll(";\n"); |
| 5649 | | 6288 | |
| | 6289 | try freeLocal(f, inst, it.local, 0); |
| | 6290 | |
| 5650 | return accum; | 6291 | return accum; |
| 5651 | } | 6292 | } |
| 5652 | | 6293 | |
| 5653 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | 6294 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5654 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 5655 | | | |
| 5656 | const inst_ty = f.air.typeOfIndex(inst); | | |
| 5657 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 6295 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| | 6296 | const inst_ty = f.air.typeOfIndex(inst); |
| 5658 | const len = @intCast(usize, inst_ty.arrayLen()); | 6297 | const len = @intCast(usize, inst_ty.arrayLen()); |
| 5659 | const elements = @ptrCast([]const Air.Inst.Ref, f.air.extra[ty_pl.payload..][0..len]); | 6298 | const elements = @ptrCast([]const Air.Inst.Ref, f.air.extra[ty_pl.payload..][0..len]); |
| | 6299 | const gpa = f.object.dg.gpa; |
| | 6300 | const resolved_elements = try gpa.alloc(CValue, elements.len); |
| | 6301 | defer gpa.free(resolved_elements); |
| | 6302 | for (elements) |element, i| { |
| | 6303 | resolved_elements[i] = try f.resolveInst(element); |
| | 6304 | } |
| | 6305 | { |
| | 6306 | var bt = iterateBigTomb(f, inst); |
| | 6307 | for (elements) |element| { |
| | 6308 | try bt.feed(element); |
| | 6309 | } |
| | 6310 | } |
| | 6311 | |
| | 6312 | if (f.liveness.isUnused(inst)) return CValue.none; |
| | 6313 | |
| 5660 | const target = f.object.dg.module.getTarget(); | 6314 | const target = f.object.dg.module.getTarget(); |
| 5661 | const mutability: Mutability = for (elements) |element| { | | |
| 5662 | if (lowersToArray(f.air.typeOf(element), target)) break .Mut; | | |
| 5663 | } else .Const; | | |
| 5664 | | 6315 | |
| 5665 | const writer = f.object.writer(); | 6316 | const writer = f.object.writer(); |
| 5666 | const local = try f.allocLocal(inst_ty, mutability); | 6317 | const local = try f.allocLocal(inst, inst_ty); |
| 5667 | try writer.writeAll(" = "); | | |
| 5668 | switch (inst_ty.zigTypeTag()) { | 6318 | switch (inst_ty.zigTypeTag()) { |
| 5669 | .Array, .Vector => { | 6319 | .Array, .Vector => { |
| 5670 | const elem_ty = inst_ty.childType(); | 6320 | const elem_ty = inst_ty.childType(); |
| 5671 | try writer.writeByte('{'); | 6321 | for (resolved_elements) |element, i| { |
| 5672 | var empty = true; | 6322 | try f.writeCValue(writer, local, .Other); |
| 5673 | for (elements) |element| { | 6323 | try writer.print("[{d}] = ", .{i}); |
| 5674 | if (!empty) try writer.writeAll(", "); | 6324 | try f.writeCValue(writer, element, .Other); |
| 5675 | try f.writeCValue(writer, try f.resolveInst(element), .Initializer); | 6325 | try writer.writeAll(";\n"); |
| 5676 | empty = false; | | |
| 5677 | } | 6326 | } |
| 5678 | if (inst_ty.sentinel()) |sentinel| { | 6327 | if (inst_ty.sentinel()) |sentinel| { |
| 5679 | if (!empty) try writer.writeAll(", "); | 6328 | try f.writeCValue(writer, local, .Other); |
| 5680 | try f.object.dg.renderValue(writer, elem_ty, sentinel, .Initializer); | 6329 | try writer.print("[{d}] = ", .{resolved_elements.len}); |
| 5681 | empty = false; | 6330 | try f.object.dg.renderValue(writer, elem_ty, sentinel, .Other); |
| | 6331 | try writer.writeAll(";\n"); |
| 5682 | } | 6332 | } |
| 5683 | if (empty) try writer.print("{}", .{try f.fmtIntLiteral(Type.u8, Value.zero)}); | | |
| 5684 | try writer.writeAll("};\n"); | | |
| 5685 | }, | 6333 | }, |
| 5686 | .Struct => switch (inst_ty.containerLayout()) { | 6334 | .Struct => switch (inst_ty.containerLayout()) { |
| 5687 | .Auto, .Extern => { | 6335 | .Auto, .Extern => { |
| | 6336 | try f.writeCValue(writer, local, .Other); |
| | 6337 | try writer.writeAll(" = ("); |
| | 6338 | try f.renderTypecast(writer, inst_ty); |
| | 6339 | try writer.writeAll(")"); |
| 5688 | try writer.writeByte('{'); | 6340 | try writer.writeByte('{'); |
| 5689 | var empty = true; | 6341 | var empty = true; |
| 5690 | for (elements) |element, index| { | 6342 | for (elements) |element, index| { |
| ... | @@ -5698,7 +6350,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5698,7 +6350,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5698 | const element_ty = f.air.typeOf(element); | 6350 | const element_ty = f.air.typeOf(element); |
| 5699 | try f.writeCValue(writer, switch (element_ty.zigTypeTag()) { | 6351 | try f.writeCValue(writer, switch (element_ty.zigTypeTag()) { |
| 5700 | .Array => CValue{ .undef = element_ty }, | 6352 | .Array => CValue{ .undef = element_ty }, |
| 5701 | else => try f.resolveInst(element), | 6353 | else => resolved_elements[index], |
| 5702 | }, .Initializer); | 6354 | }, .Initializer); |
| 5703 | empty = false; | 6355 | empty = false; |
| 5704 | } | 6356 | } |
| ... | @@ -5721,7 +6373,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5721,7 +6373,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5721 | try writer.writeAll("memcpy("); | 6373 | try writer.writeAll("memcpy("); |
| 5722 | try f.writeCValueMember(writer, local, field_name); | 6374 | try f.writeCValueMember(writer, local, field_name); |
| 5723 | try writer.writeAll(", "); | 6375 | try writer.writeAll(", "); |
| 5724 | try f.writeCValue(writer, try f.resolveInst(element), .FunctionArgument); | 6376 | try f.writeCValue(writer, resolved_elements[index], .FunctionArgument); |
| 5725 | try writer.writeAll(", sizeof("); | 6377 | try writer.writeAll(", sizeof("); |
| 5726 | try f.renderTypecast(writer, element_ty); | 6378 | try f.renderTypecast(writer, element_ty); |
| 5727 | try writer.writeAll("));\n"); | 6379 | try writer.writeAll("));\n"); |
| ... | @@ -5730,6 +6382,10 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5730,6 +6382,10 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5730 | } | 6382 | } |
| 5731 | }, | 6383 | }, |
| 5732 | .Packed => { | 6384 | .Packed => { |
| | 6385 | try f.writeCValue(writer, local, .Other); |
| | 6386 | try writer.writeAll(" = ("); |
| | 6387 | try f.renderTypecast(writer, inst_ty); |
| | 6388 | try writer.writeAll(")"); |
| 5733 | const int_info = inst_ty.intInfo(target); | 6389 | const int_info = inst_ty.intInfo(target); |
| 5734 | | 6390 | |
| 5735 | var bit_offset_ty_pl = Type.Payload.Bits{ | 6391 | var bit_offset_ty_pl = Type.Payload.Bits{ |
| ... | @@ -5754,7 +6410,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5754,7 +6410,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5754 | empty = false; | 6410 | empty = false; |
| 5755 | } | 6411 | } |
| 5756 | empty = true; | 6412 | empty = true; |
| 5757 | for (elements) |element, index| { | 6413 | for (resolved_elements) |element, index| { |
| 5758 | const field_ty = inst_ty.structFieldType(index); | 6414 | const field_ty = inst_ty.structFieldType(index); |
| 5759 | if (!field_ty.hasRuntimeBitsIgnoreComptime()) continue; | 6415 | if (!field_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 5760 | | 6416 | |
| ... | @@ -5772,7 +6428,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5772,7 +6428,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5772 | }); | 6428 | }); |
| 5773 | try writer.writeByte(')'); | 6429 | try writer.writeByte(')'); |
| 5774 | } | 6430 | } |
| 5775 | try f.writeCValue(writer, try f.resolveInst(element), .Other); | 6431 | try f.writeCValue(writer, element, .Other); |
| 5776 | try writer.writeAll(", "); | 6432 | try writer.writeAll(", "); |
| 5777 | try f.object.dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); | 6433 | try f.object.dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); |
| 5778 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .Bits); | 6434 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .Bits); |
| ... | @@ -5793,26 +6449,31 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5793,26 +6449,31 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5793 | } | 6449 | } |
| 5794 | | 6450 | |
| 5795 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { | 6451 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5796 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 5797 | | | |
| 5798 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; | 6452 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 5799 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; | 6453 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| | 6454 | |
| | 6455 | if (f.liveness.isUnused(inst)) { |
| | 6456 | try reap(f, inst, &.{extra.init}); |
| | 6457 | return CValue.none; |
| | 6458 | } |
| | 6459 | |
| 5800 | const union_ty = f.air.typeOfIndex(inst); | 6460 | const union_ty = f.air.typeOfIndex(inst); |
| 5801 | const target = f.object.dg.module.getTarget(); | 6461 | const target = f.object.dg.module.getTarget(); |
| 5802 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; | 6462 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 5803 | const field_name = union_obj.fields.keys()[extra.field_index]; | 6463 | const field_name = union_obj.fields.keys()[extra.field_index]; |
| 5804 | const payload = try f.resolveInst(extra.init); | 6464 | const payload = try f.resolveInst(extra.init); |
| | 6465 | try reap(f, inst, &.{extra.init}); |
| 5805 | | 6466 | |
| 5806 | const writer = f.object.writer(); | 6467 | const writer = f.object.writer(); |
| 5807 | const local = try f.allocLocal(union_ty, .Const); | 6468 | const local = try f.allocLocal(inst, union_ty); |
| 5808 | if (union_obj.layout == .Packed) { | 6469 | if (union_obj.layout == .Packed) { |
| | 6470 | try f.writeCValue(writer, local, .Other); |
| 5809 | try writer.writeAll(" = "); | 6471 | try writer.writeAll(" = "); |
| 5810 | try f.writeCValue(writer, payload, .Initializer); | 6472 | try f.writeCValue(writer, payload, .Initializer); |
| 5811 | try writer.writeAll(";\n"); | 6473 | try writer.writeAll(";\n"); |
| 5812 | return local; | 6474 | return local; |
| 5813 | } | 6475 | } |
| 5814 | | 6476 | |
| 5815 | try writer.writeAll(" = {"); | | |
| 5816 | if (union_ty.unionTagTypeSafety()) |tag_ty| { | 6477 | if (union_ty.unionTagTypeSafety()) |tag_ty| { |
| 5817 | const layout = union_ty.unionGetLayout(target); | 6478 | const layout = union_ty.unionGetLayout(target); |
| 5818 | if (layout.tag_size != 0) { | 6479 | if (layout.tag_size != 0) { |
| ... | @@ -5827,16 +6488,20 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5827,16 +6488,20 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5827 | var int_pl: Value.Payload.U64 = undefined; | 6488 | var int_pl: Value.Payload.U64 = undefined; |
| 5828 | const int_val = tag_val.enumToInt(tag_ty, &int_pl); | 6489 | const int_val = tag_val.enumToInt(tag_ty, &int_pl); |
| 5829 | | 6490 | |
| 5830 | try writer.print(".tag = {}, ", .{try f.fmtIntLiteral(tag_ty, int_val)}); | 6491 | try f.writeCValue(writer, local, .Other); |
| | 6492 | try writer.print(".tag = {}; ", .{try f.fmtIntLiteral(tag_ty, int_val)}); |
| 5831 | } | 6493 | } |
| 5832 | try writer.writeAll(".payload = {"); | 6494 | try f.writeCValue(writer, local, .Other); |
| | 6495 | try writer.print(".payload.{ } = ", .{fmtIdent(field_name)}); |
| | 6496 | try f.writeCValue(writer, payload, .Other); |
| | 6497 | try writer.writeAll(";\n"); |
| | 6498 | return local; |
| 5833 | } | 6499 | } |
| 5834 | | 6500 | |
| | 6501 | try f.writeCValue(writer, local, .Other); |
| 5835 | try writer.print(".{ } = ", .{fmtIdent(field_name)}); | 6502 | try writer.print(".{ } = ", .{fmtIdent(field_name)}); |
| 5836 | try f.writeCValue(writer, payload, .Initializer); | 6503 | try f.writeCValue(writer, payload, .Other); |
| 5837 | | 6504 | try writer.writeAll(";\n"); |
| 5838 | if (union_ty.unionTagTypeSafety()) |_| try writer.writeByte('}'); | | |
| 5839 | try writer.writeAll("};\n"); | | |
| 5840 | | 6505 | |
| 5841 | return local; | 6506 | return local; |
| 5842 | } | 6507 | } |
| ... | @@ -5851,6 +6516,7 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5851,6 +6516,7 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5851 | .instruction => return CValue.none, | 6516 | .instruction => return CValue.none, |
| 5852 | } | 6517 | } |
| 5853 | const ptr = try f.resolveInst(prefetch.ptr); | 6518 | const ptr = try f.resolveInst(prefetch.ptr); |
| | 6519 | try reap(f, inst, &.{prefetch.ptr}); |
| 5854 | const writer = f.object.writer(); | 6520 | const writer = f.object.writer(); |
| 5855 | try writer.writeAll("zig_prefetch("); | 6521 | try writer.writeAll("zig_prefetch("); |
| 5856 | try f.writeCValue(writer, ptr, .FunctionArgument); | 6522 | try f.writeCValue(writer, ptr, .FunctionArgument); |
| ... | @@ -5867,7 +6533,8 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5867,7 +6533,8 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5867 | | 6533 | |
| 5868 | const writer = f.object.writer(); | 6534 | const writer = f.object.writer(); |
| 5869 | const inst_ty = f.air.typeOfIndex(inst); | 6535 | const inst_ty = f.air.typeOfIndex(inst); |
| 5870 | const local = try f.allocLocal(inst_ty, .Const); | 6536 | const local = try f.allocLocal(inst, inst_ty); |
| | 6537 | try f.writeCValue(writer, local, .Other); |
| 5871 | | 6538 | |
| 5872 | try writer.writeAll(" = "); | 6539 | try writer.writeAll(" = "); |
| 5873 | try writer.print("zig_wasm_memory_size({d});\n", .{pl_op.payload}); | 6540 | try writer.print("zig_wasm_memory_size({d});\n", .{pl_op.payload}); |
| ... | @@ -5881,7 +6548,9 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5881,7 +6548,9 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5881 | const writer = f.object.writer(); | 6548 | const writer = f.object.writer(); |
| 5882 | const inst_ty = f.air.typeOfIndex(inst); | 6549 | const inst_ty = f.air.typeOfIndex(inst); |
| 5883 | const operand = try f.resolveInst(pl_op.operand); | 6550 | const operand = try f.resolveInst(pl_op.operand); |
| 5884 | const local = try f.allocLocal(inst_ty, .Const); | 6551 | try reap(f, inst, &.{pl_op.operand}); |
| | 6552 | const local = try f.allocLocal(inst, inst_ty); |
| | 6553 | try f.writeCValue(writer, local, .Other); |
| 5885 | | 6554 | |
| 5886 | try writer.writeAll(" = "); | 6555 | try writer.writeAll(" = "); |
| 5887 | try writer.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); | 6556 | try writer.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); |
| ... | @@ -5891,15 +6560,20 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5891,15 +6560,20 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5891 | } | 6560 | } |
| 5892 | | 6561 | |
| 5893 | fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { | 6562 | fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5894 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 5895 | | | |
| 5896 | const inst_ty = f.air.typeOfIndex(inst); | 6563 | const inst_ty = f.air.typeOfIndex(inst); |
| 5897 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 6564 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 6565 | if (f.liveness.isUnused(inst)) { |
| | 6566 | try reap(f, inst, &.{un_op}); |
| | 6567 | return CValue.none; |
| | 6568 | } |
| | 6569 | |
| 5898 | const operand = try f.resolveInst(un_op); | 6570 | const operand = try f.resolveInst(un_op); |
| | 6571 | try reap(f, inst, &.{un_op}); |
| 5899 | const operand_ty = f.air.typeOf(un_op); | 6572 | const operand_ty = f.air.typeOf(un_op); |
| 5900 | | 6573 | |
| 5901 | const local = try f.allocLocal(inst_ty, .Const); | | |
| 5902 | const writer = f.object.writer(); | 6574 | const writer = f.object.writer(); |
| | 6575 | const local = try f.allocLocal(inst, inst_ty); |
| | 6576 | try f.writeCValue(writer, local, .Other); |
| 5903 | try writer.writeAll(" = zig_neg_"); | 6577 | try writer.writeAll(" = zig_neg_"); |
| 5904 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); | 6578 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| 5905 | try writer.writeByte('('); | 6579 | try writer.writeByte('('); |
| ... | @@ -5909,12 +6583,17 @@ fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -5909,12 +6583,17 @@ fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5909 | } | 6583 | } |
| 5910 | | 6584 | |
| 5911 | fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CValue { | 6585 | fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CValue { |
| 5912 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 5913 | const un_op = f.air.instructions.items(.data)[inst].un_op; | 6586 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| | 6587 | if (f.liveness.isUnused(inst)) { |
| | 6588 | try reap(f, inst, &.{un_op}); |
| | 6589 | return CValue.none; |
| | 6590 | } |
| | 6591 | const operand = try f.resolveInst(un_op); |
| | 6592 | try reap(f, inst, &.{un_op}); |
| 5914 | const writer = f.object.writer(); | 6593 | const writer = f.object.writer(); |
| 5915 | const inst_ty = f.air.typeOfIndex(inst); | 6594 | const inst_ty = f.air.typeOfIndex(inst); |
| 5916 | const operand = try f.resolveInst(un_op); | 6595 | const local = try f.allocLocal(inst, inst_ty); |
| 5917 | const local = try f.allocLocal(inst_ty, .Const); | 6596 | try f.writeCValue(writer, local, .Other); |
| 5918 | try writer.writeAll(" = zig_libc_name_"); | 6597 | try writer.writeAll(" = zig_libc_name_"); |
| 5919 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); | 6598 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 5920 | try writer.writeByte('('); | 6599 | try writer.writeByte('('); |
| ... | @@ -5925,18 +6604,21 @@ fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVal | ... | @@ -5925,18 +6604,21 @@ fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVal |
| 5925 | return local; | 6604 | return local; |
| 5926 | } | 6605 | } |
| 5927 | | 6606 | |
| 5928 | fn airBinFloatOp( | 6607 | fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CValue { |
| 5929 | f: *Function, | | |
| 5930 | inst: Air.Inst.Index, | | |
| 5931 | operation: []const u8, | | |
| 5932 | ) !void { | | |
| 5933 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; | 6608 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 5934 | const writer = f.object.writer(); | 6609 | if (f.liveness.isUnused(inst)) { |
| 5935 | const inst_ty = f.air.typeOfIndex(inst); | 6610 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| | 6611 | return CValue.none; |
| | 6612 | } |
| 5936 | const lhs = try f.resolveInst(bin_op.lhs); | 6613 | const lhs = try f.resolveInst(bin_op.lhs); |
| 5937 | const rhs = try f.resolveInst(bin_op.rhs); | 6614 | const rhs = try f.resolveInst(bin_op.rhs); |
| | 6615 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 5938 | | 6616 | |
| 5939 | try writer.writeAll("zig_libc_name_"); | 6617 | const writer = f.object.writer(); |
| | 6618 | const inst_ty = f.air.typeOfIndex(inst); |
| | 6619 | const local = try f.allocLocal(inst, inst_ty); |
| | 6620 | try f.writeCValue(writer, local, .Other); |
| | 6621 | try writer.writeAll(" = zig_libc_name_"); |
| 5940 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); | 6622 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 5941 | try writer.writeByte('('); | 6623 | try writer.writeByte('('); |
| 5942 | try writer.writeAll(operation); | 6624 | try writer.writeAll(operation); |
| ... | @@ -5944,19 +6626,25 @@ fn airBinFloatOp( | ... | @@ -5944,19 +6626,25 @@ fn airBinFloatOp( |
| 5944 | try f.writeCValue(writer, lhs, .FunctionArgument); | 6626 | try f.writeCValue(writer, lhs, .FunctionArgument); |
| 5945 | try writer.writeAll(", "); | 6627 | try writer.writeAll(", "); |
| 5946 | try f.writeCValue(writer, rhs, .FunctionArgument); | 6628 | try f.writeCValue(writer, rhs, .FunctionArgument); |
| 5947 | try writer.writeAll(")"); | 6629 | try writer.writeAll(");\n"); |
| | 6630 | return local; |
| 5948 | } | 6631 | } |
| 5949 | | 6632 | |
| 5950 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { | 6633 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5951 | if (f.liveness.isUnused(inst)) return CValue.none; | | |
| 5952 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; | 6634 | const pl_op = f.air.instructions.items(.data)[inst].pl_op; |
| 5953 | const extra = f.air.extraData(Air.Bin, pl_op.payload).data; | 6635 | const bin_op = f.air.extraData(Air.Bin, pl_op.payload).data; |
| | 6636 | if (f.liveness.isUnused(inst)) { |
| | 6637 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs, pl_op.operand }); |
| | 6638 | return CValue.none; |
| | 6639 | } |
| 5954 | const inst_ty = f.air.typeOfIndex(inst); | 6640 | const inst_ty = f.air.typeOfIndex(inst); |
| 5955 | const mulend1 = try f.resolveInst(extra.lhs); | 6641 | const mulend1 = try f.resolveInst(bin_op.lhs); |
| 5956 | const mulend2 = try f.resolveInst(extra.rhs); | 6642 | const mulend2 = try f.resolveInst(bin_op.rhs); |
| 5957 | const addend = try f.resolveInst(pl_op.operand); | 6643 | const addend = try f.resolveInst(pl_op.operand); |
| | 6644 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs, pl_op.operand }); |
| 5958 | const writer = f.object.writer(); | 6645 | const writer = f.object.writer(); |
| 5959 | const local = try f.allocLocal(inst_ty, .Const); | 6646 | const local = try f.allocLocal(inst, inst_ty); |
| | 6647 | try f.writeCValue(writer, local, .Other); |
| 5960 | try writer.writeAll(" = zig_libc_name_"); | 6648 | try writer.writeAll(" = zig_libc_name_"); |
| 5961 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); | 6649 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 5962 | try writer.writeAll("(fma)("); | 6650 | try writer.writeAll("(fma)("); |
| ... | @@ -6335,3 +7023,105 @@ fn loweredArrayInfo(ty: Type, target: std.Target) ?Type.ArrayInfo { | ... | @@ -6335,3 +7023,105 @@ fn loweredArrayInfo(ty: Type, target: std.Target) ?Type.ArrayInfo { |
| 6335 | }, | 7023 | }, |
| 6336 | } | 7024 | } |
| 6337 | } | 7025 | } |
| | 7026 | |
| | 7027 | fn reap(f: *Function, inst: Air.Inst.Index, operands: []const Air.Inst.Ref) !void { |
| | 7028 | assert(operands.len <= Liveness.bpi - 1); |
| | 7029 | var tomb_bits = f.liveness.getTombBits(inst); |
| | 7030 | for (operands) |operand| { |
| | 7031 | const dies = @truncate(u1, tomb_bits) != 0; |
| | 7032 | tomb_bits >>= 1; |
| | 7033 | if (!dies) continue; |
| | 7034 | try die(f, inst, operand); |
| | 7035 | } |
| | 7036 | } |
| | 7037 | |
| | 7038 | fn die(f: *Function, inst: Air.Inst.Index, ref: Air.Inst.Ref) !void { |
| | 7039 | const ref_inst = Air.refToIndex(ref) orelse return; |
| | 7040 | if (f.air.instructions.items(.tag)[ref_inst] == .constant) return; |
| | 7041 | const c_value = (f.value_map.fetchRemove(ref) orelse return).value; |
| | 7042 | const local_index = switch (c_value) { |
| | 7043 | .local => |l| l, |
| | 7044 | else => return, |
| | 7045 | }; |
| | 7046 | try freeLocal(f, inst, local_index, ref_inst); |
| | 7047 | } |
| | 7048 | |
| | 7049 | fn freeLocal(f: *Function, inst: Air.Inst.Index, local_index: LocalIndex, ref_inst: Air.Inst.Index) !void { |
| | 7050 | const gpa = f.object.dg.gpa; |
| | 7051 | const local = &f.locals.items[local_index]; |
| | 7052 | log.debug("%{d}: freeing t{d} (operand %{d})", .{ inst, local_index, ref_inst }); |
| | 7053 | if (local.loop_depth < f.free_locals_clone_depth) return; |
| | 7054 | const gop = try f.free_locals_stack.items[local.loop_depth].getOrPutContext( |
| | 7055 | gpa, |
| | 7056 | local.ty, |
| | 7057 | f.tyHashCtx(), |
| | 7058 | ); |
| | 7059 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| | 7060 | if (std.debug.runtime_safety) { |
| | 7061 | // If this trips, it means a local is being inserted into the |
| | 7062 | // free_locals map while it already exists in the map, which is not |
| | 7063 | // allowed. |
| | 7064 | assert(mem.indexOfScalar(LocalIndex, gop.value_ptr.items, local_index) == null); |
| | 7065 | // If this trips, an unfreeable allocation was attempted to be freed. |
| | 7066 | assert(!f.allocs.contains(local_index)); |
| | 7067 | } |
| | 7068 | try gop.value_ptr.append(gpa, local_index); |
| | 7069 | } |
| | 7070 | |
| | 7071 | const BigTomb = struct { |
| | 7072 | f: *Function, |
| | 7073 | inst: Air.Inst.Index, |
| | 7074 | lbt: Liveness.BigTomb, |
| | 7075 | |
| | 7076 | fn feed(bt: *BigTomb, op_ref: Air.Inst.Ref) !void { |
| | 7077 | const dies = bt.lbt.feed(); |
| | 7078 | if (!dies) return; |
| | 7079 | try die(bt.f, bt.inst, op_ref); |
| | 7080 | } |
| | 7081 | }; |
| | 7082 | |
| | 7083 | fn iterateBigTomb(f: *Function, inst: Air.Inst.Index) BigTomb { |
| | 7084 | return .{ |
| | 7085 | .f = f, |
| | 7086 | .inst = inst, |
| | 7087 | .lbt = f.liveness.iterateBigTomb(inst), |
| | 7088 | }; |
| | 7089 | } |
| | 7090 | |
| | 7091 | /// A naive clone of this map would create copies of the ArrayList which is |
| | 7092 | /// stored in the values. This function additionally clones the values. |
| | 7093 | fn cloneFreeLocalsMap(gpa: mem.Allocator, map: *LocalsMap) !LocalsMap { |
| | 7094 | var cloned = try map.clone(gpa); |
| | 7095 | const values = cloned.values(); |
| | 7096 | var i: usize = 0; |
| | 7097 | errdefer { |
| | 7098 | cloned.deinit(gpa); |
| | 7099 | while (i > 0) { |
| | 7100 | i -= 1; |
| | 7101 | values[i].deinit(gpa); |
| | 7102 | } |
| | 7103 | } |
| | 7104 | while (i < values.len) : (i += 1) { |
| | 7105 | values[i] = try values[i].clone(gpa); |
| | 7106 | } |
| | 7107 | return cloned; |
| | 7108 | } |
| | 7109 | |
| | 7110 | fn deinitFreeLocalsMap(gpa: mem.Allocator, map: *LocalsMap) void { |
| | 7111 | for (map.values()) |*value| { |
| | 7112 | value.deinit(gpa); |
| | 7113 | } |
| | 7114 | map.deinit(gpa); |
| | 7115 | } |
| | 7116 | |
| | 7117 | fn noticeBranchFrees(f: *Function, pre_locals_len: LocalIndex, inst: Air.Inst.Index) !void { |
| | 7118 | for (f.locals.items[pre_locals_len..]) |*local, local_offset| { |
| | 7119 | const local_index = pre_locals_len + @intCast(LocalIndex, local_offset); |
| | 7120 | if (f.allocs.contains(local_index)) continue; // allocs are not freeable |
| | 7121 | |
| | 7122 | // free more deeply nested locals from other branches at current depth |
| | 7123 | assert(local.loop_depth >= f.free_locals_stack.items.len - 1); |
| | 7124 | local.loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1); |
| | 7125 | try freeLocal(f, inst, local_index, 0); |
| | 7126 | } |
| | 7127 | } |