| author | |
| committer | |
| log | 83735207850bd526807a39771432be0bc7410414 |
| tree | de0b8dc27f0824f14134dbece33a088bbffa4001 |
| parent | a0775fdaa1e7427dcd6be9b19726d4996344e9fa |
| parent | 60af42705d62417c73a13481e60b0861423e77fe |
| signature |
stage2: fixes for error union semantics14 files changed, 1553 insertions(+), 727 deletions(-)
lib/std/debug.zig+1-1| ... | ... | @@ -1798,7 +1798,7 @@ fn resetSegfaultHandler() void { |
| 1798 | 1798 | .mask = os.empty_sigset, |
| 1799 | 1799 | .flags = 0, |
| 1800 | 1800 | }; |
| 1801 | // do nothing if an error happens to avoid a double-panic | |
| 1801 | // To avoid a double-panic, do nothing if an error happens here. | |
| 1802 | 1802 | updateSegfaultHandler(&act) catch {}; |
| 1803 | 1803 | } |
| 1804 | 1804 |
src/Sema.zig+87-16| ... | ... | @@ -5899,12 +5899,22 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 5899 | 5899 | if (val.isUndef()) { |
| 5900 | 5900 | return sema.addConstUndef(result_ty); |
| 5901 | 5901 | } |
| 5902 | const payload = try sema.arena.create(Value.Payload.U64); | |
| 5903 | payload.* = .{ | |
| 5904 | .base = .{ .tag = .int_u64 }, | |
| 5905 | .data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value, | |
| 5906 | }; | |
| 5907 | return sema.addConstant(result_ty, Value.initPayload(&payload.base)); | |
| 5902 | switch (val.tag()) { | |
| 5903 | .@"error" => { | |
| 5904 | const payload = try sema.arena.create(Value.Payload.U64); | |
| 5905 | payload.* = .{ | |
| 5906 | .base = .{ .tag = .int_u64 }, | |
| 5907 | .data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value, | |
| 5908 | }; | |
| 5909 | return sema.addConstant(result_ty, Value.initPayload(&payload.base)); | |
| 5910 | }, | |
| 5911 | ||
| 5912 | // This is not a valid combination with the type `anyerror`. | |
| 5913 | .the_only_possible_value => unreachable, | |
| 5914 | ||
| 5915 | // Assume it's already encoded as an integer. | |
| 5916 | else => return sema.addConstant(result_ty, val), | |
| 5917 | } | |
| 5908 | 5918 | } |
| 5909 | 5919 | |
| 5910 | 5920 | try sema.requireRuntimeBlock(block, src); |
| ... | ... | @@ -6261,19 +6271,24 @@ fn zirErrUnionPayload( |
| 6261 | 6271 | }); |
| 6262 | 6272 | } |
| 6263 | 6273 | |
| 6274 | const result_ty = operand_ty.errorUnionPayload(); | |
| 6264 | 6275 | if (try sema.resolveDefinedValue(block, src, operand)) |val| { |
| 6265 | 6276 | if (val.getError()) |name| { |
| 6266 | 6277 | return sema.fail(block, src, "caught unexpected error '{s}'", .{name}); |
| 6267 | 6278 | } |
| 6268 | 6279 | const data = val.castTag(.eu_payload).?.data; |
| 6269 | const result_ty = operand_ty.errorUnionPayload(); | |
| 6270 | 6280 | return sema.addConstant(result_ty, data); |
| 6271 | 6281 | } |
| 6282 | ||
| 6272 | 6283 | try sema.requireRuntimeBlock(block, src); |
| 6273 | if (safety_check and block.wantSafety()) { | |
| 6284 | ||
| 6285 | // If the error set has no fields then no safety check is needed. | |
| 6286 | if (safety_check and block.wantSafety() and | |
| 6287 | operand_ty.errorUnionSet().errorSetCardinality() != .zero) | |
| 6288 | { | |
| 6274 | 6289 | try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err); |
| 6275 | 6290 | } |
| 6276 | const result_ty = operand_ty.errorUnionPayload(); | |
| 6291 | ||
| 6277 | 6292 | return block.addTyOp(.unwrap_errunion_payload, result_ty, operand); |
| 6278 | 6293 | } |
| 6279 | 6294 | |
| ... | ... | @@ -6311,7 +6326,8 @@ fn analyzeErrUnionPayloadPtr( |
| 6311 | 6326 | }); |
| 6312 | 6327 | } |
| 6313 | 6328 | |
| 6314 | const payload_ty = operand_ty.elemType().errorUnionPayload(); | |
| 6329 | const err_union_ty = operand_ty.elemType(); | |
| 6330 | const payload_ty = err_union_ty.errorUnionPayload(); | |
| 6315 | 6331 | const operand_pointer_ty = try Type.ptr(sema.arena, sema.mod, .{ |
| 6316 | 6332 | .pointee_type = payload_ty, |
| 6317 | 6333 | .mutable = !operand_ty.isConstPtr(), |
| ... | ... | @@ -6351,9 +6367,14 @@ fn analyzeErrUnionPayloadPtr( |
| 6351 | 6367 | } |
| 6352 | 6368 | |
| 6353 | 6369 | try sema.requireRuntimeBlock(block, src); |
| 6354 | if (safety_check and block.wantSafety()) { | |
| 6370 | ||
| 6371 | // If the error set has no fields then no safety check is needed. | |
| 6372 | if (safety_check and block.wantSafety() and | |
| 6373 | err_union_ty.errorUnionSet().errorSetCardinality() != .zero) | |
| 6374 | { | |
| 6355 | 6375 | try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr); |
| 6356 | 6376 | } |
| 6377 | ||
| 6357 | 6378 | const air_tag: Air.Inst.Tag = if (initializing) |
| 6358 | 6379 | .errunion_payload_ptr_set |
| 6359 | 6380 | else |
| ... | ... | @@ -20929,6 +20950,11 @@ fn analyzeLoad( |
| 20929 | 20950 | .Pointer => ptr_ty.childType(), |
| 20930 | 20951 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(sema.mod)}), |
| 20931 | 20952 | }; |
| 20953 | ||
| 20954 | if (try sema.typeHasOnePossibleValue(block, src, elem_ty)) |opv| { | |
| 20955 | return sema.addConstant(elem_ty, opv); | |
| 20956 | } | |
| 20957 | ||
| 20932 | 20958 | if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| { |
| 20933 | 20959 | if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| { |
| 20934 | 20960 | return sema.addConstant(elem_ty, elem_val); |
| ... | ... | @@ -23295,16 +23321,11 @@ pub fn typeHasOnePossibleValue( |
| 23295 | 23321 | .const_slice, |
| 23296 | 23322 | .mut_slice, |
| 23297 | 23323 | .anyopaque, |
| 23298 | .optional, | |
| 23299 | 23324 | .optional_single_mut_pointer, |
| 23300 | 23325 | .optional_single_const_pointer, |
| 23301 | 23326 | .enum_literal, |
| 23302 | 23327 | .anyerror_void_error_union, |
| 23303 | .error_union, | |
| 23304 | .error_set, | |
| 23305 | .error_set_single, | |
| 23306 | 23328 | .error_set_inferred, |
| 23307 | .error_set_merged, | |
| 23308 | 23329 | .@"opaque", |
| 23309 | 23330 | .var_args_param, |
| 23310 | 23331 | .manyptr_u8, |
| ... | ... | @@ -23333,6 +23354,56 @@ pub fn typeHasOnePossibleValue( |
| 23333 | 23354 | .bound_fn, |
| 23334 | 23355 | => return null, |
| 23335 | 23356 | |
| 23357 | .optional => { | |
| 23358 | var buf: Type.Payload.ElemType = undefined; | |
| 23359 | const child_ty = ty.optionalChild(&buf); | |
| 23360 | if (child_ty.isNoReturn()) { | |
| 23361 | return Value.@"null"; | |
| 23362 | } else { | |
| 23363 | return null; | |
| 23364 | } | |
| 23365 | }, | |
| 23366 | ||
| 23367 | .error_union => { | |
| 23368 | const error_ty = ty.errorUnionSet(); | |
| 23369 | switch (error_ty.errorSetCardinality()) { | |
| 23370 | .zero => { | |
| 23371 | const payload_ty = ty.errorUnionPayload(); | |
| 23372 | if (try typeHasOnePossibleValue(sema, block, src, payload_ty)) |payload_val| { | |
| 23373 | return try Value.Tag.eu_payload.create(sema.arena, payload_val); | |
| 23374 | } else { | |
| 23375 | return null; | |
| 23376 | } | |
| 23377 | }, | |
| 23378 | .one => { | |
| 23379 | if (ty.errorUnionPayload().isNoReturn()) { | |
| 23380 | const error_val = (try typeHasOnePossibleValue(sema, block, src, error_ty)).?; | |
| 23381 | return error_val; | |
| 23382 | } else { | |
| 23383 | return null; | |
| 23384 | } | |
| 23385 | }, | |
| 23386 | .many => return null, | |
| 23387 | } | |
| 23388 | }, | |
| 23389 | ||
| 23390 | .error_set_single => { | |
| 23391 | const name = ty.castTag(.error_set_single).?.data; | |
| 23392 | return try Value.Tag.@"error".create(sema.arena, .{ .name = name }); | |
| 23393 | }, | |
| 23394 | .error_set => { | |
| 23395 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 23396 | const names = err_set_obj.names.keys(); | |
| 23397 | if (names.len > 1) return null; | |
| 23398 | return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] }); | |
| 23399 | }, | |
| 23400 | .error_set_merged => { | |
| 23401 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 23402 | const names = name_map.keys(); | |
| 23403 | if (names.len > 1) return null; | |
| 23404 | return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] }); | |
| 23405 | }, | |
| 23406 | ||
| 23336 | 23407 | .@"struct" => { |
| 23337 | 23408 | const resolved_ty = try sema.resolveTypeFields(block, src, ty); |
| 23338 | 23409 | const s = resolved_ty.castTag(.@"struct").?.data; |
src/arch/aarch64/CodeGen.zig+69-41| ... | ... | @@ -3,6 +3,7 @@ const builtin = @import("builtin"); |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | const math = std.math; |
| 5 | 5 | const assert = std.debug.assert; |
| 6 | const codegen = @import("../../codegen.zig"); | |
| 6 | 7 | const Air = @import("../../Air.zig"); |
| 7 | 8 | const Mir = @import("Mir.zig"); |
| 8 | 9 | const Emit = @import("Emit.zig"); |
| ... | ... | @@ -22,12 +23,14 @@ const leb128 = std.leb; |
| 22 | 23 | const log = std.log.scoped(.codegen); |
| 23 | 24 | const build_options = @import("build_options"); |
| 24 | 25 | |
| 25 | const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError; | |
| 26 | const FnResult = @import("../../codegen.zig").FnResult; | |
| 27 | const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; | |
| 26 | const GenerateSymbolError = codegen.GenerateSymbolError; | |
| 27 | const FnResult = codegen.FnResult; | |
| 28 | const DebugInfoOutput = codegen.DebugInfoOutput; | |
| 28 | 29 | |
| 29 | 30 | const bits = @import("bits.zig"); |
| 30 | 31 | const abi = @import("abi.zig"); |
| 32 | const errUnionPayloadOffset = codegen.errUnionPayloadOffset; | |
| 33 | const errUnionErrorOffset = codegen.errUnionErrorOffset; | |
| 31 | 34 | const RegisterManager = abi.RegisterManager; |
| 32 | 35 | const RegisterLock = RegisterManager.RegisterLock; |
| 33 | 36 | const Register = bits.Register; |
| ... | ... | @@ -3272,7 +3275,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 3272 | 3275 | |
| 3273 | 3276 | fn ret(self: *Self, mcv: MCValue) !void { |
| 3274 | 3277 | const ret_ty = self.fn_type.fnReturnType(); |
| 3275 | try self.setRegOrMem(ret_ty, self.ret_mcv, mcv); | |
| 3278 | switch (self.ret_mcv) { | |
| 3279 | .immediate => { | |
| 3280 | assert(ret_ty.isError()); | |
| 3281 | }, | |
| 3282 | else => { | |
| 3283 | try self.setRegOrMem(ret_ty, self.ret_mcv, mcv); | |
| 3284 | }, | |
| 3285 | } | |
| 3276 | 3286 | // Just add space for an instruction, patch this later |
| 3277 | 3287 | const index = try self.addInst(.{ |
| 3278 | 3288 | .tag = .nop, |
| ... | ... | @@ -3601,30 +3611,39 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| 3601 | 3611 | const error_type = ty.errorUnionSet(); |
| 3602 | 3612 | const payload_type = ty.errorUnionPayload(); |
| 3603 | 3613 | |
| 3604 | if (!error_type.hasRuntimeBits()) { | |
| 3614 | if (error_type.errorSetCardinality() == .zero) { | |
| 3605 | 3615 | return MCValue{ .immediate = 0 }; // always false |
| 3606 | } else if (!payload_type.hasRuntimeBits()) { | |
| 3607 | if (error_type.abiSize(self.target.*) <= 8) { | |
| 3608 | const reg_mcv: MCValue = switch (operand) { | |
| 3609 | .register => operand, | |
| 3610 | else => .{ .register = try self.copyToTmpRegister(error_type, operand) }, | |
| 3611 | }; | |
| 3616 | } | |
| 3612 | 3617 | |
| 3618 | const err_off = errUnionErrorOffset(payload_type, self.target.*); | |
| 3619 | switch (operand) { | |
| 3620 | .stack_offset => |off| { | |
| 3621 | const offset = off - @intCast(u32, err_off); | |
| 3622 | const tmp_reg = try self.copyToTmpRegister(Type.anyerror, .{ .stack_offset = offset }); | |
| 3613 | 3623 | _ = try self.addInst(.{ |
| 3614 | 3624 | .tag = .cmp_immediate, |
| 3615 | 3625 | .data = .{ .r_imm12_sh = .{ |
| 3616 | .rn = reg_mcv.register, | |
| 3626 | .rn = tmp_reg, | |
| 3617 | 3627 | .imm12 = 0, |
| 3618 | 3628 | } }, |
| 3619 | 3629 | }); |
| 3620 | ||
| 3621 | return MCValue{ .compare_flags_unsigned = .gt }; | |
| 3622 | } else { | |
| 3623 | return self.fail("TODO isErr for errors with size > 8", .{}); | |
| 3624 | } | |
| 3625 | } else { | |
| 3626 | return self.fail("TODO isErr for non-empty payloads", .{}); | |
| 3630 | }, | |
| 3631 | .register => |reg| { | |
| 3632 | if (err_off > 0 or payload_type.hasRuntimeBitsIgnoreComptime()) { | |
| 3633 | return self.fail("TODO implement isErr for register operand with payload bits", .{}); | |
| 3634 | } | |
| 3635 | _ = try self.addInst(.{ | |
| 3636 | .tag = .cmp_immediate, | |
| 3637 | .data = .{ .r_imm12_sh = .{ | |
| 3638 | .rn = reg, | |
| 3639 | .imm12 = 0, | |
| 3640 | } }, | |
| 3641 | }); | |
| 3642 | }, | |
| 3643 | else => return self.fail("TODO implement isErr for {}", .{operand}), | |
| 3627 | 3644 | } |
| 3645 | ||
| 3646 | return MCValue{ .compare_flags_unsigned = .gt }; | |
| 3628 | 3647 | } |
| 3629 | 3648 | |
| 3630 | 3649 | fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| ... | ... | @@ -4483,7 +4502,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 4483 | 4502 | const ref_int = @enumToInt(inst); |
| 4484 | 4503 | if (ref_int < Air.Inst.Ref.typed_value_map.len) { |
| 4485 | 4504 | const tv = Air.Inst.Ref.typed_value_map[ref_int]; |
| 4486 | if (!tv.ty.hasRuntimeBits()) { | |
| 4505 | if (!tv.ty.hasRuntimeBitsIgnoreComptime() and !tv.ty.isError()) { | |
| 4487 | 4506 | return MCValue{ .none = {} }; |
| 4488 | 4507 | } |
| 4489 | 4508 | return self.genTypedValue(tv); |
| ... | ... | @@ -4491,7 +4510,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 4491 | 4510 | |
| 4492 | 4511 | // If the type has no codegen bits, no need to store it. |
| 4493 | 4512 | const inst_ty = self.air.typeOf(inst); |
| 4494 | if (!inst_ty.hasRuntimeBits()) | |
| 4513 | if (!inst_ty.hasRuntimeBitsIgnoreComptime() and !inst_ty.isError()) | |
| 4495 | 4514 | return MCValue{ .none = {} }; |
| 4496 | 4515 | |
| 4497 | 4516 | const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); |
| ... | ... | @@ -4674,32 +4693,38 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4674 | 4693 | } |
| 4675 | 4694 | }, |
| 4676 | 4695 | .ErrorSet => { |
| 4677 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 4678 | const module = self.bin_file.options.module.?; | |
| 4679 | const global_error_set = module.global_error_set; | |
| 4680 | const error_index = global_error_set.get(err_name).?; | |
| 4681 | return MCValue{ .immediate = error_index }; | |
| 4696 | switch (typed_value.val.tag()) { | |
| 4697 | .@"error" => { | |
| 4698 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 4699 | const module = self.bin_file.options.module.?; | |
| 4700 | const global_error_set = module.global_error_set; | |
| 4701 | const error_index = global_error_set.get(err_name).?; | |
| 4702 | return MCValue{ .immediate = error_index }; | |
| 4703 | }, | |
| 4704 | else => { | |
| 4705 | // In this case we are rendering an error union which has a 0 bits payload. | |
| 4706 | return MCValue{ .immediate = 0 }; | |
| 4707 | }, | |
| 4708 | } | |
| 4682 | 4709 | }, |
| 4683 | 4710 | .ErrorUnion => { |
| 4684 | 4711 | const error_type = typed_value.ty.errorUnionSet(); |
| 4685 | 4712 | const payload_type = typed_value.ty.errorUnionPayload(); |
| 4686 | 4713 | |
| 4687 | if (typed_value.val.castTag(.eu_payload)) |pl| { | |
| 4688 | if (!payload_type.hasRuntimeBits()) { | |
| 4689 | // We use the error type directly as the type. | |
| 4690 | return MCValue{ .immediate = 0 }; | |
| 4691 | } | |
| 4714 | if (error_type.errorSetCardinality() == .zero) { | |
| 4715 | const payload_val = typed_value.val.castTag(.eu_payload).?.data; | |
| 4716 | return self.genTypedValue(.{ .ty = payload_type, .val = payload_val }); | |
| 4717 | } | |
| 4692 | 4718 | |
| 4693 | _ = pl; | |
| 4694 | return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty.fmtDebug()}); | |
| 4695 | } else { | |
| 4696 | if (!payload_type.hasRuntimeBits()) { | |
| 4697 | // We use the error type directly as the type. | |
| 4698 | return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val }); | |
| 4699 | } | |
| 4719 | const is_pl = typed_value.val.errorUnionIsPayload(); | |
| 4700 | 4720 | |
| 4701 | return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty.fmtDebug()}); | |
| 4721 | if (!payload_type.hasRuntimeBitsIgnoreComptime()) { | |
| 4722 | // We use the error type directly as the type. | |
| 4723 | const err_val = if (!is_pl) typed_value.val else Value.initTag(.zero); | |
| 4724 | return self.genTypedValue(.{ .ty = error_type, .val = err_val }); | |
| 4702 | 4725 | } |
| 4726 | ||
| 4727 | return self.lowerUnnamedConst(typed_value); | |
| 4703 | 4728 | }, |
| 4704 | 4729 | .Struct => { |
| 4705 | 4730 | return self.lowerUnnamedConst(typed_value); |
| ... | ... | @@ -4796,13 +4821,16 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 4796 | 4821 | |
| 4797 | 4822 | if (ret_ty.zigTypeTag() == .NoReturn) { |
| 4798 | 4823 | result.return_value = .{ .unreach = {} }; |
| 4799 | } else if (!ret_ty.hasRuntimeBits()) { | |
| 4824 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError()) { | |
| 4800 | 4825 | result.return_value = .{ .none = {} }; |
| 4801 | 4826 | } else switch (cc) { |
| 4802 | 4827 | .Naked => unreachable, |
| 4803 | 4828 | .Unspecified, .C => { |
| 4804 | 4829 | const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*)); |
| 4805 | if (ret_ty_size <= 8) { | |
| 4830 | if (ret_ty_size == 0) { | |
| 4831 | assert(ret_ty.isError()); | |
| 4832 | result.return_value = .{ .immediate = 0 }; | |
| 4833 | } else if (ret_ty_size <= 8) { | |
| 4806 | 4834 | result.return_value = .{ .register = registerAlias(c_abi_int_return_regs[0], ret_ty_size) }; |
| 4807 | 4835 | } else { |
| 4808 | 4836 | return self.fail("TODO support more return types for ARM backend", .{}); |
src/arch/arm/CodeGen.zig+64-37| ... | ... | @@ -3,6 +3,7 @@ const builtin = @import("builtin"); |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | const math = std.math; |
| 5 | 5 | const assert = std.debug.assert; |
| 6 | const codegen = @import("../../codegen.zig"); | |
| 6 | 7 | const Air = @import("../../Air.zig"); |
| 7 | 8 | const Mir = @import("Mir.zig"); |
| 8 | 9 | const Emit = @import("Emit.zig"); |
| ... | ... | @@ -22,12 +23,14 @@ const leb128 = std.leb; |
| 22 | 23 | const log = std.log.scoped(.codegen); |
| 23 | 24 | const build_options = @import("build_options"); |
| 24 | 25 | |
| 25 | const FnResult = @import("../../codegen.zig").FnResult; | |
| 26 | const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError; | |
| 27 | const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; | |
| 26 | const FnResult = codegen.FnResult; | |
| 27 | const GenerateSymbolError = codegen.GenerateSymbolError; | |
| 28 | const DebugInfoOutput = codegen.DebugInfoOutput; | |
| 28 | 29 | |
| 29 | 30 | const bits = @import("bits.zig"); |
| 30 | 31 | const abi = @import("abi.zig"); |
| 32 | const errUnionPayloadOffset = codegen.errUnionPayloadOffset; | |
| 33 | const errUnionErrorOffset = codegen.errUnionErrorOffset; | |
| 31 | 34 | const RegisterManager = abi.RegisterManager; |
| 32 | 35 | const RegisterLock = RegisterManager.RegisterLock; |
| 33 | 36 | const Register = bits.Register; |
| ... | ... | @@ -1763,19 +1766,26 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 1763 | 1766 | |
| 1764 | 1767 | /// Given an error union, returns the error |
| 1765 | 1768 | fn errUnionErr(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue { |
| 1769 | const err_ty = error_union_ty.errorUnionSet(); | |
| 1766 | 1770 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 1767 | if (!payload_ty.hasRuntimeBits()) return error_union_mcv; | |
| 1771 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1772 | return MCValue{ .immediate = 0 }; | |
| 1773 | } | |
| 1774 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1775 | return error_union_mcv; | |
| 1776 | } | |
| 1768 | 1777 | |
| 1778 | const err_offset = @intCast(u32, errUnionErrorOffset(payload_ty, self.target.*)); | |
| 1769 | 1779 | switch (error_union_mcv) { |
| 1770 | 1780 | .register => return self.fail("TODO errUnionErr for registers", .{}), |
| 1771 | 1781 | .stack_argument_offset => |off| { |
| 1772 | return MCValue{ .stack_argument_offset = off }; | |
| 1782 | return MCValue{ .stack_argument_offset = off - err_offset }; | |
| 1773 | 1783 | }, |
| 1774 | 1784 | .stack_offset => |off| { |
| 1775 | return MCValue{ .stack_offset = off }; | |
| 1785 | return MCValue{ .stack_offset = off - err_offset }; | |
| 1776 | 1786 | }, |
| 1777 | 1787 | .memory => |addr| { |
| 1778 | return MCValue{ .memory = addr }; | |
| 1788 | return MCValue{ .memory = addr + err_offset }; | |
| 1779 | 1789 | }, |
| 1780 | 1790 | else => unreachable, // invalid MCValue for an error union |
| 1781 | 1791 | } |
| ... | ... | @@ -1793,24 +1803,26 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1793 | 1803 | |
| 1794 | 1804 | /// Given an error union, returns the payload |
| 1795 | 1805 | fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue { |
| 1806 | const err_ty = error_union_ty.errorUnionSet(); | |
| 1796 | 1807 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 1797 | if (!payload_ty.hasRuntimeBits()) return MCValue.none; | |
| 1798 | ||
| 1799 | const error_ty = error_union_ty.errorUnionSet(); | |
| 1800 | const error_size = @intCast(u32, error_ty.abiSize(self.target.*)); | |
| 1801 | const eu_align = @intCast(u32, error_union_ty.abiAlignment(self.target.*)); | |
| 1802 | const offset = std.mem.alignForwardGeneric(u32, error_size, eu_align); | |
| 1808 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1809 | return error_union_mcv; | |
| 1810 | } | |
| 1811 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1812 | return MCValue.none; | |
| 1813 | } | |
| 1803 | 1814 | |
| 1815 | const payload_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target.*)); | |
| 1804 | 1816 | switch (error_union_mcv) { |
| 1805 | 1817 | .register => return self.fail("TODO errUnionPayload for registers", .{}), |
| 1806 | 1818 | .stack_argument_offset => |off| { |
| 1807 | return MCValue{ .stack_argument_offset = off - offset }; | |
| 1819 | return MCValue{ .stack_argument_offset = off - payload_offset }; | |
| 1808 | 1820 | }, |
| 1809 | 1821 | .stack_offset => |off| { |
| 1810 | return MCValue{ .stack_offset = off - offset }; | |
| 1822 | return MCValue{ .stack_offset = off - payload_offset }; | |
| 1811 | 1823 | }, |
| 1812 | 1824 | .memory => |addr| { |
| 1813 | return MCValue{ .memory = addr - offset }; | |
| 1825 | return MCValue{ .memory = addr + payload_offset }; | |
| 1814 | 1826 | }, |
| 1815 | 1827 | else => unreachable, // invalid MCValue for an error union |
| 1816 | 1828 | } |
| ... | ... | @@ -3478,6 +3490,9 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 3478 | 3490 | |
| 3479 | 3491 | switch (self.ret_mcv) { |
| 3480 | 3492 | .none => {}, |
| 3493 | .immediate => { | |
| 3494 | assert(ret_ty.isError()); | |
| 3495 | }, | |
| 3481 | 3496 | .register => |reg| { |
| 3482 | 3497 | // Return result by value |
| 3483 | 3498 | try self.genSetReg(ret_ty, reg, operand); |
| ... | ... | @@ -3867,7 +3882,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| 3867 | 3882 | const error_type = ty.errorUnionSet(); |
| 3868 | 3883 | const error_int_type = Type.initTag(.u16); |
| 3869 | 3884 | |
| 3870 | if (!error_type.hasRuntimeBits()) { | |
| 3885 | if (error_type.errorSetCardinality() == .zero) { | |
| 3871 | 3886 | return MCValue{ .immediate = 0 }; // always false |
| 3872 | 3887 | } |
| 3873 | 3888 | |
| ... | ... | @@ -4975,7 +4990,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 4975 | 4990 | const ref_int = @enumToInt(inst); |
| 4976 | 4991 | if (ref_int < Air.Inst.Ref.typed_value_map.len) { |
| 4977 | 4992 | const tv = Air.Inst.Ref.typed_value_map[ref_int]; |
| 4978 | if (!tv.ty.hasRuntimeBits()) { | |
| 4993 | if (!tv.ty.hasRuntimeBitsIgnoreComptime() and !tv.ty.isError()) { | |
| 4979 | 4994 | return MCValue{ .none = {} }; |
| 4980 | 4995 | } |
| 4981 | 4996 | return self.genTypedValue(tv); |
| ... | ... | @@ -4983,7 +4998,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 4983 | 4998 | |
| 4984 | 4999 | // If the type has no codegen bits, no need to store it. |
| 4985 | 5000 | const inst_ty = self.air.typeOf(inst); |
| 4986 | if (!inst_ty.hasRuntimeBits()) | |
| 5001 | if (!inst_ty.hasRuntimeBitsIgnoreComptime() and !inst_ty.isError()) | |
| 4987 | 5002 | return MCValue{ .none = {} }; |
| 4988 | 5003 | |
| 4989 | 5004 | const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); |
| ... | ... | @@ -5147,26 +5162,35 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 5147 | 5162 | } |
| 5148 | 5163 | }, |
| 5149 | 5164 | .ErrorSet => { |
| 5150 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 5151 | const module = self.bin_file.options.module.?; | |
| 5152 | const global_error_set = module.global_error_set; | |
| 5153 | const error_index = global_error_set.get(err_name).?; | |
| 5154 | return MCValue{ .immediate = error_index }; | |
| 5165 | switch (typed_value.val.tag()) { | |
| 5166 | .@"error" => { | |
| 5167 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 5168 | const module = self.bin_file.options.module.?; | |
| 5169 | const global_error_set = module.global_error_set; | |
| 5170 | const error_index = global_error_set.get(err_name).?; | |
| 5171 | return MCValue{ .immediate = error_index }; | |
| 5172 | }, | |
| 5173 | else => { | |
| 5174 | // In this case we are rendering an error union which has a 0 bits payload. | |
| 5175 | return MCValue{ .immediate = 0 }; | |
| 5176 | }, | |
| 5177 | } | |
| 5155 | 5178 | }, |
| 5156 | 5179 | .ErrorUnion => { |
| 5157 | 5180 | const error_type = typed_value.ty.errorUnionSet(); |
| 5158 | 5181 | const payload_type = typed_value.ty.errorUnionPayload(); |
| 5159 | 5182 | |
| 5160 | if (typed_value.val.castTag(.eu_payload)) |_| { | |
| 5161 | if (!payload_type.hasRuntimeBits()) { | |
| 5162 | // We use the error type directly as the type. | |
| 5163 | return MCValue{ .immediate = 0 }; | |
| 5164 | } | |
| 5165 | } else { | |
| 5166 | if (!payload_type.hasRuntimeBits()) { | |
| 5167 | // We use the error type directly as the type. | |
| 5168 | return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val }); | |
| 5169 | } | |
| 5183 | if (error_type.errorSetCardinality() == .zero) { | |
| 5184 | const payload_val = typed_value.val.castTag(.eu_payload).?.data; | |
| 5185 | return self.genTypedValue(.{ .ty = payload_type, .val = payload_val }); | |
| 5186 | } | |
| 5187 | ||
| 5188 | const is_pl = typed_value.val.errorUnionIsPayload(); | |
| 5189 | ||
| 5190 | if (!payload_type.hasRuntimeBitsIgnoreComptime()) { | |
| 5191 | // We use the error type directly as the type. | |
| 5192 | const err_val = if (!is_pl) typed_value.val else Value.initTag(.zero); | |
| 5193 | return self.genTypedValue(.{ .ty = error_type, .val = err_val }); | |
| 5170 | 5194 | } |
| 5171 | 5195 | }, |
| 5172 | 5196 | |
| ... | ... | @@ -5231,7 +5255,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 5231 | 5255 | |
| 5232 | 5256 | if (ret_ty.zigTypeTag() == .NoReturn) { |
| 5233 | 5257 | result.return_value = .{ .unreach = {} }; |
| 5234 | } else if (!ret_ty.hasRuntimeBits()) { | |
| 5258 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 5235 | 5259 | result.return_value = .{ .none = {} }; |
| 5236 | 5260 | } else { |
| 5237 | 5261 | const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*)); |
| ... | ... | @@ -5278,11 +5302,14 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 5278 | 5302 | .Unspecified => { |
| 5279 | 5303 | if (ret_ty.zigTypeTag() == .NoReturn) { |
| 5280 | 5304 | result.return_value = .{ .unreach = {} }; |
| 5281 | } else if (!ret_ty.hasRuntimeBits()) { | |
| 5305 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError()) { | |
| 5282 | 5306 | result.return_value = .{ .none = {} }; |
| 5283 | 5307 | } else { |
| 5284 | 5308 | const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*)); |
| 5285 | if (ret_ty_size <= 4) { | |
| 5309 | if (ret_ty_size == 0) { | |
| 5310 | assert(ret_ty.isError()); | |
| 5311 | result.return_value = .{ .immediate = 0 }; | |
| 5312 | } else if (ret_ty_size <= 4) { | |
| 5286 | 5313 | result.return_value = .{ .register = .r0 }; |
| 5287 | 5314 | } else { |
| 5288 | 5315 | // The result is returned by reference, not by |
src/arch/wasm/CodeGen.zig+110-59| ... | ... | @@ -22,6 +22,8 @@ const Liveness = @import("../../Liveness.zig"); |
| 22 | 22 | const Mir = @import("Mir.zig"); |
| 23 | 23 | const Emit = @import("Emit.zig"); |
| 24 | 24 | const abi = @import("abi.zig"); |
| 25 | const errUnionPayloadOffset = codegen.errUnionPayloadOffset; | |
| 26 | const errUnionErrorOffset = codegen.errUnionErrorOffset; | |
| 25 | 27 | |
| 26 | 28 | /// Wasm Value, created when generating an instruction |
| 27 | 29 | const WValue = union(enum) { |
| ... | ... | @@ -636,7 +638,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 636 | 638 | // means we must generate it from a constant. |
| 637 | 639 | const val = self.air.value(ref).?; |
| 638 | 640 | const ty = self.air.typeOf(ref); |
| 639 | if (!ty.hasRuntimeBitsIgnoreComptime() and !ty.isInt()) { | |
| 641 | if (!ty.hasRuntimeBitsIgnoreComptime() and !ty.isInt() and !ty.isError()) { | |
| 640 | 642 | gop.value_ptr.* = WValue{ .none = {} }; |
| 641 | 643 | return gop.value_ptr.*; |
| 642 | 644 | } |
| ... | ... | @@ -804,6 +806,8 @@ fn genFunctype(gpa: Allocator, fn_info: Type.Payload.Function.Data, target: std. |
| 804 | 806 | } else { |
| 805 | 807 | try returns.append(typeToValtype(fn_info.return_type, target)); |
| 806 | 808 | } |
| 809 | } else if (fn_info.return_type.isError()) { | |
| 810 | try returns.append(.i32); | |
| 807 | 811 | } |
| 808 | 812 | |
| 809 | 813 | // param types |
| ... | ... | @@ -1373,13 +1377,18 @@ fn isByRef(ty: Type, target: std.Target) bool { |
| 1373 | 1377 | .Int => return ty.intInfo(target).bits > 64, |
| 1374 | 1378 | .Float => return ty.floatBits(target) > 64, |
| 1375 | 1379 | .ErrorUnion => { |
| 1376 | const has_tag = ty.errorUnionSet().hasRuntimeBitsIgnoreComptime(); | |
| 1377 | const has_pl = ty.errorUnionPayload().hasRuntimeBitsIgnoreComptime(); | |
| 1378 | if (!has_tag or !has_pl) return false; | |
| 1379 | return ty.hasRuntimeBitsIgnoreComptime(); | |
| 1380 | const err_ty = ty.errorUnionSet(); | |
| 1381 | const pl_ty = ty.errorUnionPayload(); | |
| 1382 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1383 | return isByRef(pl_ty, target); | |
| 1384 | } | |
| 1385 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1386 | return false; | |
| 1387 | } | |
| 1388 | return true; | |
| 1380 | 1389 | }, |
| 1381 | 1390 | .Optional => { |
| 1382 | if (ty.isPtrLikeOptional()) return false; | |
| 1391 | if (ty.optionalReprIsPayload()) return false; | |
| 1383 | 1392 | var buf: Type.Payload.ElemType = undefined; |
| 1384 | 1393 | return ty.optionalChild(&buf).hasRuntimeBitsIgnoreComptime(); |
| 1385 | 1394 | }, |
| ... | ... | @@ -1624,13 +1633,14 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1624 | 1633 | fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1625 | 1634 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1626 | 1635 | const operand = try self.resolveInst(un_op); |
| 1627 | const ret_ty = self.decl.ty.fnReturnType(); | |
| 1636 | const fn_info = self.decl.ty.fnInfo(); | |
| 1637 | const ret_ty = fn_info.return_type; | |
| 1628 | 1638 | |
| 1629 | 1639 | // result must be stored in the stack and we return a pointer |
| 1630 | 1640 | // to the stack instead |
| 1631 | 1641 | if (self.return_value != .none) { |
| 1632 | try self.store(self.return_value, operand, self.decl.ty.fnReturnType(), 0); | |
| 1633 | } else if (self.decl.ty.fnInfo().cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1642 | try self.store(self.return_value, operand, ret_ty, 0); | |
| 1643 | } else if (fn_info.cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1634 | 1644 | switch (ret_ty.zigTypeTag()) { |
| 1635 | 1645 | // Aggregate types can be lowered as a singular value |
| 1636 | 1646 | .Struct, .Union => { |
| ... | ... | @@ -1650,7 +1660,11 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1650 | 1660 | else => try self.emitWValue(operand), |
| 1651 | 1661 | } |
| 1652 | 1662 | } else { |
| 1653 | try self.emitWValue(operand); | |
| 1663 | if (!ret_ty.hasRuntimeBitsIgnoreComptime() and ret_ty.isError()) { | |
| 1664 | try self.addImm32(0); | |
| 1665 | } else { | |
| 1666 | try self.emitWValue(operand); | |
| 1667 | } | |
| 1654 | 1668 | } |
| 1655 | 1669 | try self.restoreStackPointer(); |
| 1656 | 1670 | try self.addTag(.@"return"); |
| ... | ... | @@ -1675,7 +1689,13 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1675 | 1689 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1676 | 1690 | const operand = try self.resolveInst(un_op); |
| 1677 | 1691 | const ret_ty = self.air.typeOf(un_op).childType(); |
| 1678 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) return WValue.none; | |
| 1692 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1693 | if (ret_ty.isError()) { | |
| 1694 | try self.addImm32(0); | |
| 1695 | } else { | |
| 1696 | return WValue.none; | |
| 1697 | } | |
| 1698 | } | |
| 1679 | 1699 | |
| 1680 | 1700 | if (!firstParamSRet(self.decl.ty.fnInfo(), self.target)) { |
| 1681 | 1701 | const result = try self.load(operand, ret_ty, 0); |
| ... | ... | @@ -1723,8 +1743,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 1723 | 1743 | |
| 1724 | 1744 | const sret = if (first_param_sret) blk: { |
| 1725 | 1745 | const sret_local = try self.allocStack(ret_ty); |
| 1726 | const ptr_offset = try self.buildPointerOffset(sret_local, 0, .new); | |
| 1727 | try self.emitWValue(ptr_offset); | |
| 1746 | try self.lowerToStack(sret_local); | |
| 1728 | 1747 | break :blk sret_local; |
| 1729 | 1748 | } else WValue{ .none = {} }; |
| 1730 | 1749 | |
| ... | ... | @@ -1754,7 +1773,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 1754 | 1773 | try self.addLabel(.call_indirect, fn_type_index); |
| 1755 | 1774 | } |
| 1756 | 1775 | |
| 1757 | if (self.liveness.isUnused(inst) or !ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1776 | if (self.liveness.isUnused(inst) or (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError())) { | |
| 1758 | 1777 | return WValue.none; |
| 1759 | 1778 | } else if (ret_ty.isNoReturn()) { |
| 1760 | 1779 | try self.addTag(.@"unreachable"); |
| ... | ... | @@ -1796,8 +1815,11 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1796 | 1815 | .ErrorUnion => { |
| 1797 | 1816 | const err_ty = ty.errorUnionSet(); |
| 1798 | 1817 | const pl_ty = ty.errorUnionPayload(); |
| 1818 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1819 | return self.store(lhs, rhs, pl_ty, 0); | |
| 1820 | } | |
| 1799 | 1821 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1800 | return self.store(lhs, rhs, err_ty, 0); | |
| 1822 | return self.store(lhs, rhs, Type.anyerror, 0); | |
| 1801 | 1823 | } |
| 1802 | 1824 | |
| 1803 | 1825 | const len = @intCast(u32, ty.abiSize(self.target)); |
| ... | ... | @@ -1812,6 +1834,9 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 1812 | 1834 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1813 | 1835 | return self.store(lhs, rhs, Type.u8, 0); |
| 1814 | 1836 | } |
| 1837 | if (pl_ty.zigTypeTag() == .ErrorSet) { | |
| 1838 | return self.store(lhs, rhs, Type.anyerror, 0); | |
| 1839 | } | |
| 1815 | 1840 | |
| 1816 | 1841 | const len = @intCast(u32, ty.abiSize(self.target)); |
| 1817 | 1842 | return self.memcpy(lhs, rhs, .{ .imm32 = len }); |
| ... | ... | @@ -2178,7 +2203,7 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV |
| 2178 | 2203 | const parent_ptr = try self.lowerParentPtr(payload_ptr.container_ptr, payload_ptr.container_ty); |
| 2179 | 2204 | var buf: Type.Payload.ElemType = undefined; |
| 2180 | 2205 | const payload_ty = payload_ptr.container_ty.optionalChild(&buf); |
| 2181 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.isPtrLikeOptional()) { | |
| 2206 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.optionalReprIsPayload()) { | |
| 2182 | 2207 | return parent_ptr; |
| 2183 | 2208 | } |
| 2184 | 2209 | |
| ... | ... | @@ -2256,6 +2281,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2256 | 2281 | const target = self.target; |
| 2257 | 2282 | |
| 2258 | 2283 | switch (ty.zigTypeTag()) { |
| 2284 | .Void => return WValue{ .none = {} }, | |
| 2259 | 2285 | .Int => { |
| 2260 | 2286 | const int_info = ty.intInfo(self.target); |
| 2261 | 2287 | switch (int_info.signedness) { |
| ... | ... | @@ -2324,11 +2350,15 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2324 | 2350 | }, |
| 2325 | 2351 | .ErrorUnion => { |
| 2326 | 2352 | const error_type = ty.errorUnionSet(); |
| 2353 | if (error_type.errorSetCardinality() == .zero) { | |
| 2354 | const pl_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef); | |
| 2355 | return self.lowerConstant(pl_val, ty.errorUnionPayload()); | |
| 2356 | } | |
| 2327 | 2357 | const is_pl = val.errorUnionIsPayload(); |
| 2328 | 2358 | const err_val = if (!is_pl) val else Value.initTag(.zero); |
| 2329 | 2359 | return self.lowerConstant(err_val, error_type); |
| 2330 | 2360 | }, |
| 2331 | .Optional => if (ty.isPtrLikeOptional()) { | |
| 2361 | .Optional => if (ty.optionalReprIsPayload()) { | |
| 2332 | 2362 | var buf: Type.Payload.ElemType = undefined; |
| 2333 | 2363 | const pl_ty = ty.optionalChild(&buf); |
| 2334 | 2364 | if (val.castTag(.opt_payload)) |payload| { |
| ... | ... | @@ -2367,7 +2397,7 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2367 | 2397 | .Optional => { |
| 2368 | 2398 | var buf: Type.Payload.ElemType = undefined; |
| 2369 | 2399 | const pl_ty = ty.optionalChild(&buf); |
| 2370 | if (ty.isPtrLikeOptional()) { | |
| 2400 | if (ty.optionalReprIsPayload()) { | |
| 2371 | 2401 | return self.emitUndefined(pl_ty); |
| 2372 | 2402 | } |
| 2373 | 2403 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| ... | ... | @@ -2517,7 +2547,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner |
| 2517 | 2547 | } |
| 2518 | 2548 | |
| 2519 | 2549 | fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 2520 | if (ty.zigTypeTag() == .Optional and !ty.isPtrLikeOptional()) { | |
| 2550 | if (ty.zigTypeTag() == .Optional and !ty.optionalReprIsPayload()) { | |
| 2521 | 2551 | var buf: Type.Payload.ElemType = undefined; |
| 2522 | 2552 | const payload_ty = ty.optionalChild(&buf); |
| 2523 | 2553 | if (payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| ... | ... | @@ -2889,15 +2919,22 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2889 | 2919 | fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue { |
| 2890 | 2920 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 2891 | 2921 | const operand = try self.resolveInst(un_op); |
| 2892 | const err_ty = self.air.typeOf(un_op); | |
| 2893 | const pl_ty = err_ty.errorUnionPayload(); | |
| 2922 | const err_union_ty = self.air.typeOf(un_op); | |
| 2923 | const pl_ty = err_union_ty.errorUnionPayload(); | |
| 2924 | ||
| 2925 | if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 2926 | switch (opcode) { | |
| 2927 | .i32_ne => return WValue{ .imm32 = 0 }, | |
| 2928 | .i32_eq => return WValue{ .imm32 = 1 }, | |
| 2929 | else => unreachable, | |
| 2930 | } | |
| 2931 | } | |
| 2894 | 2932 | |
| 2895 | // load the error tag value | |
| 2896 | 2933 | try self.emitWValue(operand); |
| 2897 | 2934 | if (pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2898 | 2935 | try self.addMemArg(.i32_load16_u, .{ |
| 2899 | .offset = operand.offset(), | |
| 2900 | .alignment = err_ty.errorUnionSet().abiAlignment(self.target), | |
| 2936 | .offset = operand.offset() + @intCast(u32, errUnionErrorOffset(pl_ty, self.target)), | |
| 2937 | .alignment = Type.anyerror.abiAlignment(self.target), | |
| 2901 | 2938 | }); |
| 2902 | 2939 | } |
| 2903 | 2940 | |
| ... | ... | @@ -2905,7 +2942,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W |
| 2905 | 2942 | try self.addImm32(0); |
| 2906 | 2943 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2907 | 2944 | |
| 2908 | const is_err_tmp = try self.allocLocal(Type.initTag(.i32)); // result is always an i32 | |
| 2945 | const is_err_tmp = try self.allocLocal(Type.i32); | |
| 2909 | 2946 | try self.addLabel(.local_set, is_err_tmp.local); |
| 2910 | 2947 | return is_err_tmp; |
| 2911 | 2948 | } |
| ... | ... | @@ -2917,14 +2954,18 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) |
| 2917 | 2954 | const op_ty = self.air.typeOf(ty_op.operand); |
| 2918 | 2955 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 2919 | 2956 | const payload_ty = err_ty.errorUnionPayload(); |
| 2957 | ||
| 2958 | if (err_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 2959 | return operand; | |
| 2960 | } | |
| 2961 | ||
| 2920 | 2962 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} }; |
| 2921 | const err_align = err_ty.abiAlignment(self.target); | |
| 2922 | const set_size = err_ty.errorUnionSet().abiSize(self.target); | |
| 2923 | const offset = mem.alignForwardGeneric(u64, set_size, err_align); | |
| 2963 | ||
| 2964 | const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)); | |
| 2924 | 2965 | if (op_is_ptr or isByRef(payload_ty, self.target)) { |
| 2925 | return self.buildPointerOffset(operand, offset, .new); | |
| 2966 | return self.buildPointerOffset(operand, pl_offset, .new); | |
| 2926 | 2967 | } |
| 2927 | return self.load(operand, payload_ty, @intCast(u32, offset)); | |
| 2968 | return self.load(operand, payload_ty, pl_offset); | |
| 2928 | 2969 | } |
| 2929 | 2970 | |
| 2930 | 2971 | fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!WValue { |
| ... | ... | @@ -2935,11 +2976,16 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In |
| 2935 | 2976 | const op_ty = self.air.typeOf(ty_op.operand); |
| 2936 | 2977 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 2937 | 2978 | const payload_ty = err_ty.errorUnionPayload(); |
| 2979 | ||
| 2980 | if (err_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 2981 | return WValue{ .imm32 = 0 }; | |
| 2982 | } | |
| 2983 | ||
| 2938 | 2984 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2939 | 2985 | return operand; |
| 2940 | 2986 | } |
| 2941 | 2987 | |
| 2942 | return self.load(operand, err_ty.errorUnionSet(), 0); | |
| 2988 | return self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target))); | |
| 2943 | 2989 | } |
| 2944 | 2990 | |
| 2945 | 2991 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| ... | ... | @@ -2947,22 +2993,26 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2947 | 2993 | |
| 2948 | 2994 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2949 | 2995 | const operand = try self.resolveInst(ty_op.operand); |
| 2996 | const err_ty = self.air.typeOfIndex(inst); | |
| 2950 | 2997 | |
| 2951 | const op_ty = self.air.typeOf(ty_op.operand); | |
| 2952 | if (!op_ty.hasRuntimeBitsIgnoreComptime()) return operand; | |
| 2953 | const err_union_ty = self.air.getRefType(ty_op.ty); | |
| 2954 | const err_align = err_union_ty.abiAlignment(self.target); | |
| 2955 | const set_size = err_union_ty.errorUnionSet().abiSize(self.target); | |
| 2956 | const offset = mem.alignForwardGeneric(u64, set_size, err_align); | |
| 2998 | if (err_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 2999 | return operand; | |
| 3000 | } | |
| 2957 | 3001 | |
| 2958 | const err_union = try self.allocStack(err_union_ty); | |
| 2959 | const payload_ptr = try self.buildPointerOffset(err_union, offset, .new); | |
| 2960 | try self.store(payload_ptr, operand, op_ty, 0); | |
| 3002 | const pl_ty = self.air.typeOf(ty_op.operand); | |
| 3003 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 3004 | return operand; | |
| 3005 | } | |
| 3006 | ||
| 3007 | const err_union = try self.allocStack(err_ty); | |
| 3008 | const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new); | |
| 3009 | try self.store(payload_ptr, operand, pl_ty, 0); | |
| 2961 | 3010 | |
| 2962 | 3011 | // ensure we also write '0' to the error part, so any present stack value gets overwritten by it. |
| 2963 | 3012 | try self.emitWValue(err_union); |
| 2964 | 3013 | try self.addImm32(0); |
| 2965 | try self.addMemArg(.i32_store16, .{ .offset = err_union.offset(), .alignment = 2 }); | |
| 3014 | const err_val_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target)); | |
| 3015 | try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 }); | |
| 2966 | 3016 | |
| 2967 | 3017 | return err_union; |
| 2968 | 3018 | } |
| ... | ... | @@ -2973,17 +3023,18 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2973 | 3023 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2974 | 3024 | const operand = try self.resolveInst(ty_op.operand); |
| 2975 | 3025 | const err_ty = self.air.getRefType(ty_op.ty); |
| 3026 | const pl_ty = err_ty.errorUnionPayload(); | |
| 2976 | 3027 | |
| 2977 | if (!err_ty.errorUnionPayload().hasRuntimeBitsIgnoreComptime()) return operand; | |
| 3028 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 3029 | return operand; | |
| 3030 | } | |
| 2978 | 3031 | |
| 2979 | 3032 | const err_union = try self.allocStack(err_ty); |
| 2980 | try self.store(err_union, operand, err_ty.errorUnionSet(), 0); | |
| 3033 | // store error value | |
| 3034 | try self.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(pl_ty, self.target))); | |
| 2981 | 3035 | |
| 2982 | 3036 | // write 'undefined' to the payload |
| 2983 | const err_align = err_ty.abiAlignment(self.target); | |
| 2984 | const set_size = err_ty.errorUnionSet().abiSize(self.target); | |
| 2985 | const offset = mem.alignForwardGeneric(u64, set_size, err_align); | |
| 2986 | const payload_ptr = try self.buildPointerOffset(err_union, offset, .new); | |
| 3037 | const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new); | |
| 2987 | 3038 | const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target)); |
| 2988 | 3039 | try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa }); |
| 2989 | 3040 | |
| ... | ... | @@ -3074,7 +3125,7 @@ fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: en |
| 3074 | 3125 | |
| 3075 | 3126 | fn isNull(self: *Self, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue { |
| 3076 | 3127 | try self.emitWValue(operand); |
| 3077 | if (!optional_ty.isPtrLikeOptional()) { | |
| 3128 | if (!optional_ty.optionalReprIsPayload()) { | |
| 3078 | 3129 | var buf: Type.Payload.ElemType = undefined; |
| 3079 | 3130 | const payload_ty = optional_ty.optionalChild(&buf); |
| 3080 | 3131 | // When payload is zero-bits, we can treat operand as a value, rather than |
| ... | ... | @@ -3100,7 +3151,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3100 | 3151 | const opt_ty = self.air.typeOf(ty_op.operand); |
| 3101 | 3152 | const payload_ty = self.air.typeOfIndex(inst); |
| 3102 | 3153 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} }; |
| 3103 | if (opt_ty.isPtrLikeOptional()) return operand; | |
| 3154 | if (opt_ty.optionalReprIsPayload()) return operand; | |
| 3104 | 3155 | |
| 3105 | 3156 | const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target); |
| 3106 | 3157 | |
| ... | ... | @@ -3120,7 +3171,7 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3120 | 3171 | |
| 3121 | 3172 | var buf: Type.Payload.ElemType = undefined; |
| 3122 | 3173 | const payload_ty = opt_ty.optionalChild(&buf); |
| 3123 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.isPtrLikeOptional()) { | |
| 3174 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) { | |
| 3124 | 3175 | return operand; |
| 3125 | 3176 | } |
| 3126 | 3177 | |
| ... | ... | @@ -3138,7 +3189,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 3138 | 3189 | return self.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()}); |
| 3139 | 3190 | } |
| 3140 | 3191 | |
| 3141 | if (opt_ty.isPtrLikeOptional()) { | |
| 3192 | if (opt_ty.optionalReprIsPayload()) { | |
| 3142 | 3193 | return operand; |
| 3143 | 3194 | } |
| 3144 | 3195 | |
| ... | ... | @@ -3169,7 +3220,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3169 | 3220 | |
| 3170 | 3221 | const operand = try self.resolveInst(ty_op.operand); |
| 3171 | 3222 | const op_ty = self.air.typeOfIndex(inst); |
| 3172 | if (op_ty.isPtrLikeOptional()) { | |
| 3223 | if (op_ty.optionalReprIsPayload()) { | |
| 3173 | 3224 | return operand; |
| 3174 | 3225 | } |
| 3175 | 3226 | const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) catch { |
| ... | ... | @@ -3927,12 +3978,16 @@ fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3927 | 3978 | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3928 | 3979 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3929 | 3980 | const err_set_ty = self.air.typeOf(ty_op.operand).childType(); |
| 3930 | const err_ty = err_set_ty.errorUnionSet(); | |
| 3931 | 3981 | const payload_ty = err_set_ty.errorUnionPayload(); |
| 3932 | 3982 | const operand = try self.resolveInst(ty_op.operand); |
| 3933 | 3983 | |
| 3934 | 3984 | // set error-tag to '0' to annotate error union is non-error |
| 3935 | try self.store(operand, .{ .imm32 = 0 }, err_ty, 0); | |
| 3985 | try self.store( | |
| 3986 | operand, | |
| 3987 | .{ .imm32 = 0 }, | |
| 3988 | Type.anyerror, | |
| 3989 | @intCast(u32, errUnionErrorOffset(payload_ty, self.target)), | |
| 3990 | ); | |
| 3936 | 3991 | |
| 3937 | 3992 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3938 | 3993 | |
| ... | ... | @@ -3940,11 +3995,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 3940 | 3995 | return operand; |
| 3941 | 3996 | } |
| 3942 | 3997 | |
| 3943 | const err_align = err_set_ty.abiAlignment(self.target); | |
| 3944 | const set_size = err_ty.abiSize(self.target); | |
| 3945 | const offset = mem.alignForwardGeneric(u64, set_size, err_align); | |
| 3946 | ||
| 3947 | return self.buildPointerOffset(operand, @intCast(u32, offset), .new); | |
| 3998 | return self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new); | |
| 3948 | 3999 | } |
| 3949 | 4000 | |
| 3950 | 4001 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
src/arch/x86_64/CodeGen.zig+161-81| ... | ... | @@ -2,6 +2,7 @@ const std = @import("std"); |
| 2 | 2 | const build_options = @import("build_options"); |
| 3 | 3 | const builtin = @import("builtin"); |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | const codegen = @import("../../codegen.zig"); | |
| 5 | 6 | const leb128 = std.leb; |
| 6 | 7 | const link = @import("../../link.zig"); |
| 7 | 8 | const log = std.log.scoped(.codegen); |
| ... | ... | @@ -12,11 +13,11 @@ const trace = @import("../../tracy.zig").trace; |
| 12 | 13 | const Air = @import("../../Air.zig"); |
| 13 | 14 | const Allocator = mem.Allocator; |
| 14 | 15 | const Compilation = @import("../../Compilation.zig"); |
| 15 | const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; | |
| 16 | const DebugInfoOutput = codegen.DebugInfoOutput; | |
| 16 | 17 | const DW = std.dwarf; |
| 17 | 18 | const ErrorMsg = Module.ErrorMsg; |
| 18 | const FnResult = @import("../../codegen.zig").FnResult; | |
| 19 | const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError; | |
| 19 | const FnResult = codegen.FnResult; | |
| 20 | const GenerateSymbolError = codegen.GenerateSymbolError; | |
| 20 | 21 | const Emit = @import("Emit.zig"); |
| 21 | 22 | const Liveness = @import("../../Liveness.zig"); |
| 22 | 23 | const Mir = @import("Mir.zig"); |
| ... | ... | @@ -28,6 +29,8 @@ const Value = @import("../../value.zig").Value; |
| 28 | 29 | |
| 29 | 30 | const bits = @import("bits.zig"); |
| 30 | 31 | const abi = @import("abi.zig"); |
| 32 | const errUnionPayloadOffset = codegen.errUnionPayloadOffset; | |
| 33 | const errUnionErrorOffset = codegen.errUnionErrorOffset; | |
| 31 | 34 | |
| 32 | 35 | const callee_preserved_regs = abi.callee_preserved_regs; |
| 33 | 36 | const caller_preserved_regs = abi.caller_preserved_regs; |
| ... | ... | @@ -854,7 +857,7 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 854 | 857 | const ptr_ty = self.air.typeOfIndex(inst); |
| 855 | 858 | const elem_ty = ptr_ty.elemType(); |
| 856 | 859 | |
| 857 | if (!elem_ty.hasRuntimeBits()) { | |
| 860 | if (!elem_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 858 | 861 | return self.allocMem(inst, @sizeOf(usize), @alignOf(usize)); |
| 859 | 862 | } |
| 860 | 863 | |
| ... | ... | @@ -1786,21 +1789,34 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1786 | 1789 | const err_ty = err_union_ty.errorUnionSet(); |
| 1787 | 1790 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 1788 | 1791 | const operand = try self.resolveInst(ty_op.operand); |
| 1789 | const operand_lock: ?RegisterLock = switch (operand) { | |
| 1790 | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), | |
| 1791 | else => null, | |
| 1792 | }; | |
| 1793 | defer if (operand_lock) |lock| self.register_manager.unlockReg(lock); | |
| 1794 | 1792 | |
| 1795 | 1793 | const result: MCValue = result: { |
| 1796 | if (!payload_ty.hasRuntimeBits()) break :result operand; | |
| 1794 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1795 | break :result MCValue{ .immediate = 0 }; | |
| 1796 | } | |
| 1797 | ||
| 1798 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1799 | break :result operand; | |
| 1800 | } | |
| 1801 | ||
| 1802 | const err_off = errUnionErrorOffset(payload_ty, self.target.*); | |
| 1797 | 1803 | switch (operand) { |
| 1798 | 1804 | .stack_offset => |off| { |
| 1799 | break :result MCValue{ .stack_offset = off }; | |
| 1805 | const offset = off - @intCast(i32, err_off); | |
| 1806 | break :result MCValue{ .stack_offset = offset }; | |
| 1800 | 1807 | }, |
| 1801 | .register => { | |
| 1808 | .register => |reg| { | |
| 1802 | 1809 | // TODO reuse operand |
| 1803 | break :result try self.copyToRegisterWithInstTracking(inst, err_ty, operand); | |
| 1810 | const lock = self.register_manager.lockRegAssumeUnused(reg); | |
| 1811 | defer self.register_manager.unlockReg(lock); | |
| 1812 | const result = try self.copyToRegisterWithInstTracking(inst, err_union_ty, operand); | |
| 1813 | if (err_off > 0) { | |
| 1814 | const shift = @intCast(u6, err_off * 8); | |
| 1815 | try self.genShiftBinOpMir(.shr, err_union_ty, result.register, .{ .immediate = shift }); | |
| 1816 | } else { | |
| 1817 | try self.truncateRegister(Type.anyerror, result.register); | |
| 1818 | } | |
| 1819 | break :result result; | |
| 1804 | 1820 | }, |
| 1805 | 1821 | else => return self.fail("TODO implement unwrap_err_err for {}", .{operand}), |
| 1806 | 1822 | } |
| ... | ... | @@ -1815,32 +1831,37 @@ fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 1815 | 1831 | } |
| 1816 | 1832 | const err_union_ty = self.air.typeOf(ty_op.operand); |
| 1817 | 1833 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 1834 | const err_ty = err_union_ty.errorUnionSet(); | |
| 1835 | const operand = try self.resolveInst(ty_op.operand); | |
| 1836 | ||
| 1818 | 1837 | const result: MCValue = result: { |
| 1819 | if (!payload_ty.hasRuntimeBits()) break :result MCValue.none; | |
| 1838 | if (err_ty.errorSetCardinality() == .zero) { | |
| 1839 | // TODO check if we can reuse | |
| 1840 | break :result operand; | |
| 1841 | } | |
| 1820 | 1842 | |
| 1821 | const operand = try self.resolveInst(ty_op.operand); | |
| 1822 | const operand_lock: ?RegisterLock = switch (operand) { | |
| 1823 | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), | |
| 1824 | else => null, | |
| 1825 | }; | |
| 1826 | defer if (operand_lock) |lock| self.register_manager.unlockReg(lock); | |
| 1843 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1844 | break :result MCValue.none; | |
| 1845 | } | |
| 1827 | 1846 | |
| 1828 | const abi_align = err_union_ty.abiAlignment(self.target.*); | |
| 1829 | const err_ty = err_union_ty.errorUnionSet(); | |
| 1830 | const err_abi_size = mem.alignForwardGeneric(u32, @intCast(u32, err_ty.abiSize(self.target.*)), abi_align); | |
| 1847 | const payload_off = errUnionPayloadOffset(payload_ty, self.target.*); | |
| 1831 | 1848 | switch (operand) { |
| 1832 | 1849 | .stack_offset => |off| { |
| 1833 | const offset = off - @intCast(i32, err_abi_size); | |
| 1850 | const offset = off - @intCast(i32, payload_off); | |
| 1834 | 1851 | break :result MCValue{ .stack_offset = offset }; |
| 1835 | 1852 | }, |
| 1836 | .register => { | |
| 1853 | .register => |reg| { | |
| 1837 | 1854 | // TODO reuse operand |
| 1838 | const shift = @intCast(u6, err_abi_size * @sizeOf(usize)); | |
| 1855 | const lock = self.register_manager.lockRegAssumeUnused(reg); | |
| 1856 | defer self.register_manager.unlockReg(lock); | |
| 1839 | 1857 | const result = try self.copyToRegisterWithInstTracking(inst, err_union_ty, operand); |
| 1840 | try self.genShiftBinOpMir(.shr, Type.usize, result.register, .{ .immediate = shift }); | |
| 1841 | break :result MCValue{ | |
| 1842 | .register = registerAlias(result.register, @intCast(u32, payload_ty.abiSize(self.target.*))), | |
| 1843 | }; | |
| 1858 | if (payload_off > 0) { | |
| 1859 | const shift = @intCast(u6, payload_off * 8); | |
| 1860 | try self.genShiftBinOpMir(.shr, err_union_ty, result.register, .{ .immediate = shift }); | |
| 1861 | } else { | |
| 1862 | try self.truncateRegister(payload_ty, result.register); | |
| 1863 | } | |
| 1864 | break :result result; | |
| 1844 | 1865 | }, |
| 1845 | 1866 | else => return self.fail("TODO implement unwrap_err_payload for {}", .{operand}), |
| 1846 | 1867 | } |
| ... | ... | @@ -1935,24 +1956,37 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 1935 | 1956 | /// T to E!T |
| 1936 | 1957 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 1937 | 1958 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1959 | ||
| 1938 | 1960 | if (self.liveness.isUnused(inst)) { |
| 1939 | 1961 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| 1940 | 1962 | } |
| 1963 | ||
| 1941 | 1964 | const error_union_ty = self.air.getRefType(ty_op.ty); |
| 1942 | 1965 | const error_ty = error_union_ty.errorUnionSet(); |
| 1943 | 1966 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 1944 | 1967 | const operand = try self.resolveInst(ty_op.operand); |
| 1945 | assert(payload_ty.hasRuntimeBits()); | |
| 1946 | 1968 | |
| 1947 | const abi_size = @intCast(u32, error_union_ty.abiSize(self.target.*)); | |
| 1948 | const abi_align = error_union_ty.abiAlignment(self.target.*); | |
| 1949 | const err_abi_size = @intCast(u32, error_ty.abiSize(self.target.*)); | |
| 1950 | const stack_offset = @intCast(i32, try self.allocMem(inst, abi_size, abi_align)); | |
| 1951 | const offset = mem.alignForwardGeneric(u32, err_abi_size, abi_align); | |
| 1952 | try self.genSetStack(error_ty, stack_offset, .{ .immediate = 0 }, .{}); | |
| 1953 | try self.genSetStack(payload_ty, stack_offset - @intCast(i32, offset), operand, .{}); | |
| 1969 | const result: MCValue = result: { | |
| 1970 | if (error_ty.errorSetCardinality() == .zero) { | |
| 1971 | break :result operand; | |
| 1972 | } | |
| 1973 | ||
| 1974 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1975 | break :result operand; | |
| 1976 | } | |
| 1977 | ||
| 1978 | const abi_size = @intCast(u32, error_union_ty.abiSize(self.target.*)); | |
| 1979 | const abi_align = error_union_ty.abiAlignment(self.target.*); | |
| 1980 | const stack_offset = @intCast(i32, try self.allocMem(inst, abi_size, abi_align)); | |
| 1981 | const payload_off = errUnionPayloadOffset(payload_ty, self.target.*); | |
| 1982 | const err_off = errUnionErrorOffset(payload_ty, self.target.*); | |
| 1983 | try self.genSetStack(payload_ty, stack_offset - @intCast(i32, payload_off), operand, .{}); | |
| 1984 | try self.genSetStack(Type.anyerror, stack_offset - @intCast(i32, err_off), .{ .immediate = 0 }, .{}); | |
| 1985 | ||
| 1986 | break :result MCValue{ .stack_offset = stack_offset }; | |
| 1987 | }; | |
| 1954 | 1988 | |
| 1955 | return self.finishAir(inst, .{ .stack_offset = stack_offset }, .{ ty_op.operand, .none, .none }); | |
| 1989 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); | |
| 1956 | 1990 | } |
| 1957 | 1991 | |
| 1958 | 1992 | /// E to E!T |
| ... | ... | @@ -1962,19 +1996,22 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1962 | 1996 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| 1963 | 1997 | } |
| 1964 | 1998 | const error_union_ty = self.air.getRefType(ty_op.ty); |
| 1965 | const error_ty = error_union_ty.errorUnionSet(); | |
| 1966 | 1999 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 1967 | const err = try self.resolveInst(ty_op.operand); | |
| 2000 | const operand = try self.resolveInst(ty_op.operand); | |
| 2001 | ||
| 1968 | 2002 | const result: MCValue = result: { |
| 1969 | if (!payload_ty.hasRuntimeBits()) break :result err; | |
| 2003 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 2004 | break :result operand; | |
| 2005 | } | |
| 1970 | 2006 | |
| 1971 | 2007 | const abi_size = @intCast(u32, error_union_ty.abiSize(self.target.*)); |
| 1972 | 2008 | const abi_align = error_union_ty.abiAlignment(self.target.*); |
| 1973 | const err_abi_size = @intCast(u32, error_ty.abiSize(self.target.*)); | |
| 1974 | 2009 | const stack_offset = @intCast(i32, try self.allocMem(inst, abi_size, abi_align)); |
| 1975 | const offset = mem.alignForwardGeneric(u32, err_abi_size, abi_align); | |
| 1976 | try self.genSetStack(error_ty, stack_offset, err, .{}); | |
| 1977 | try self.genSetStack(payload_ty, stack_offset - @intCast(i32, offset), .undef, .{}); | |
| 2010 | const payload_off = errUnionPayloadOffset(payload_ty, self.target.*); | |
| 2011 | const err_off = errUnionErrorOffset(payload_ty, self.target.*); | |
| 2012 | try self.genSetStack(Type.anyerror, stack_offset - @intCast(i32, err_off), operand, .{}); | |
| 2013 | try self.genSetStack(payload_ty, stack_offset - @intCast(i32, payload_off), .undef, .{}); | |
| 2014 | ||
| 1978 | 2015 | break :result MCValue{ .stack_offset = stack_offset }; |
| 1979 | 2016 | }; |
| 1980 | 2017 | |
| ... | ... | @@ -2535,7 +2572,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 2535 | 2572 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2536 | 2573 | const elem_ty = self.air.typeOfIndex(inst); |
| 2537 | 2574 | const result: MCValue = result: { |
| 2538 | if (!elem_ty.hasRuntimeBits()) | |
| 2575 | if (!elem_ty.hasRuntimeBitsIgnoreComptime()) | |
| 2539 | 2576 | break :result MCValue.none; |
| 2540 | 2577 | |
| 2541 | 2578 | const ptr = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -4102,6 +4139,9 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4102 | 4139 | const operand = try self.resolveInst(un_op); |
| 4103 | 4140 | const ret_ty = self.fn_type.fnReturnType(); |
| 4104 | 4141 | switch (self.ret_mcv) { |
| 4142 | .immediate => { | |
| 4143 | assert(ret_ty.isError()); | |
| 4144 | }, | |
| 4105 | 4145 | .stack_offset => { |
| 4106 | 4146 | const reg = try self.copyToTmpRegister(Type.usize, self.ret_mcv); |
| 4107 | 4147 | const reg_lock = self.register_manager.lockRegAssumeUnused(reg); |
| ... | ... | @@ -4134,6 +4174,9 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4134 | 4174 | const ptr_ty = self.air.typeOf(un_op); |
| 4135 | 4175 | const elem_ty = ptr_ty.elemType(); |
| 4136 | 4176 | switch (self.ret_mcv) { |
| 4177 | .immediate => { | |
| 4178 | assert(elem_ty.isError()); | |
| 4179 | }, | |
| 4137 | 4180 | .stack_offset => { |
| 4138 | 4181 | const reg = try self.copyToTmpRegister(Type.usize, self.ret_mcv); |
| 4139 | 4182 | const reg_lock = self.register_manager.lockRegAssumeUnused(reg); |
| ... | ... | @@ -4377,7 +4420,6 @@ fn genVarDbgInfo( |
| 4377 | 4420 | fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { |
| 4378 | 4421 | switch (self.debug_output) { |
| 4379 | 4422 | .dwarf => |dw| { |
| 4380 | assert(ty.hasRuntimeBits()); | |
| 4381 | 4423 | const dbg_info = &dw.dbg_info; |
| 4382 | 4424 | const index = dbg_info.items.len; |
| 4383 | 4425 | try dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4 |
| ... | ... | @@ -4604,7 +4646,7 @@ fn isNull(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCValu |
| 4604 | 4646 | const cmp_ty: Type = if (!ty.isPtrLikeOptional()) blk: { |
| 4605 | 4647 | var buf: Type.Payload.ElemType = undefined; |
| 4606 | 4648 | const payload_ty = ty.optionalChild(&buf); |
| 4607 | break :blk if (payload_ty.hasRuntimeBits()) Type.bool else ty; | |
| 4649 | break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime()) Type.bool else ty; | |
| 4608 | 4650 | } else ty; |
| 4609 | 4651 | |
| 4610 | 4652 | try self.genBinOpMir(.cmp, cmp_ty, operand, MCValue{ .immediate = 0 }); |
| ... | ... | @@ -4620,25 +4662,36 @@ fn isNonNull(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCV |
| 4620 | 4662 | |
| 4621 | 4663 | fn isErr(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCValue { |
| 4622 | 4664 | const err_type = ty.errorUnionSet(); |
| 4623 | const payload_type = ty.errorUnionPayload(); | |
| 4624 | if (!err_type.hasRuntimeBits()) { | |
| 4665 | ||
| 4666 | if (err_type.errorSetCardinality() == .zero) { | |
| 4625 | 4667 | return MCValue{ .immediate = 0 }; // always false |
| 4626 | 4668 | } |
| 4627 | 4669 | |
| 4628 | 4670 | try self.spillCompareFlagsIfOccupied(); |
| 4629 | 4671 | self.compare_flags_inst = inst; |
| 4630 | 4672 | |
| 4631 | if (!payload_type.hasRuntimeBits()) { | |
| 4632 | if (err_type.abiSize(self.target.*) <= 8) { | |
| 4633 | try self.genBinOpMir(.cmp, err_type, operand, MCValue{ .immediate = 0 }); | |
| 4634 | return MCValue{ .compare_flags_unsigned = .gt }; | |
| 4635 | } else { | |
| 4636 | return self.fail("TODO isErr for errors with size larger than register size", .{}); | |
| 4637 | } | |
| 4638 | } else { | |
| 4639 | try self.genBinOpMir(.cmp, err_type, operand, MCValue{ .immediate = 0 }); | |
| 4640 | return MCValue{ .compare_flags_unsigned = .gt }; | |
| 4673 | const err_off = errUnionErrorOffset(ty.errorUnionPayload(), self.target.*); | |
| 4674 | switch (operand) { | |
| 4675 | .stack_offset => |off| { | |
| 4676 | const offset = off - @intCast(i32, err_off); | |
| 4677 | try self.genBinOpMir(.cmp, Type.anyerror, .{ .stack_offset = offset }, .{ .immediate = 0 }); | |
| 4678 | }, | |
| 4679 | .register => |reg| { | |
| 4680 | const maybe_lock = self.register_manager.lockReg(reg); | |
| 4681 | defer if (maybe_lock) |lock| self.register_manager.unlockReg(lock); | |
| 4682 | const tmp_reg = try self.copyToTmpRegister(ty, operand); | |
| 4683 | if (err_off > 0) { | |
| 4684 | const shift = @intCast(u6, err_off * 8); | |
| 4685 | try self.genShiftBinOpMir(.shr, ty, tmp_reg, .{ .immediate = shift }); | |
| 4686 | } else { | |
| 4687 | try self.truncateRegister(Type.anyerror, tmp_reg); | |
| 4688 | } | |
| 4689 | try self.genBinOpMir(.cmp, Type.anyerror, .{ .register = tmp_reg }, .{ .immediate = 0 }); | |
| 4690 | }, | |
| 4691 | else => return self.fail("TODO implement isErr for {}", .{operand}), | |
| 4641 | 4692 | } |
| 4693 | ||
| 4694 | return MCValue{ .compare_flags_unsigned = .gt }; | |
| 4642 | 4695 | } |
| 4643 | 4696 | |
| 4644 | 4697 | fn isNonErr(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCValue { |
| ... | ... | @@ -5461,6 +5514,21 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl |
| 5461 | 5514 | .immediate => |x_big| { |
| 5462 | 5515 | const base_reg = opts.dest_stack_base orelse .rbp; |
| 5463 | 5516 | switch (abi_size) { |
| 5517 | 0 => { | |
| 5518 | assert(ty.isError()); | |
| 5519 | const payload = try self.addExtra(Mir.ImmPair{ | |
| 5520 | .dest_off = @bitCast(u32, -stack_offset), | |
| 5521 | .operand = @truncate(u32, x_big), | |
| 5522 | }); | |
| 5523 | _ = try self.addInst(.{ | |
| 5524 | .tag = .mov_mem_imm, | |
| 5525 | .ops = Mir.Inst.Ops.encode(.{ | |
| 5526 | .reg1 = base_reg, | |
| 5527 | .flags = 0b00, | |
| 5528 | }), | |
| 5529 | .data = .{ .payload = payload }, | |
| 5530 | }); | |
| 5531 | }, | |
| 5464 | 5532 | 1, 2, 4 => { |
| 5465 | 5533 | const payload = try self.addExtra(Mir.ImmPair{ |
| 5466 | 5534 | .dest_off = @bitCast(u32, -stack_offset), |
| ... | ... | @@ -6643,7 +6711,7 @@ pub fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 6643 | 6711 | const ref_int = @enumToInt(inst); |
| 6644 | 6712 | if (ref_int < Air.Inst.Ref.typed_value_map.len) { |
| 6645 | 6713 | const tv = Air.Inst.Ref.typed_value_map[ref_int]; |
| 6646 | if (!tv.ty.hasRuntimeBits()) { | |
| 6714 | if (!tv.ty.hasRuntimeBitsIgnoreComptime() and !tv.ty.isError()) { | |
| 6647 | 6715 | return MCValue{ .none = {} }; |
| 6648 | 6716 | } |
| 6649 | 6717 | return self.genTypedValue(tv); |
| ... | ... | @@ -6651,7 +6719,7 @@ pub fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 6651 | 6719 | |
| 6652 | 6720 | // If the type has no codegen bits, no need to store it. |
| 6653 | 6721 | const inst_ty = self.air.typeOf(inst); |
| 6654 | if (!inst_ty.hasRuntimeBits()) | |
| 6722 | if (!inst_ty.hasRuntimeBitsIgnoreComptime() and !inst_ty.isError()) | |
| 6655 | 6723 | return MCValue{ .none = {} }; |
| 6656 | 6724 | |
| 6657 | 6725 | const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len); |
| ... | ... | @@ -6780,6 +6848,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 6780 | 6848 | const target = self.target.*; |
| 6781 | 6849 | |
| 6782 | 6850 | switch (typed_value.ty.zigTypeTag()) { |
| 6851 | .Void => return MCValue{ .none = {} }, | |
| 6783 | 6852 | .Pointer => switch (typed_value.ty.ptrSize()) { |
| 6784 | 6853 | .Slice => {}, |
| 6785 | 6854 | else => { |
| ... | ... | @@ -6841,26 +6910,35 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 6841 | 6910 | } |
| 6842 | 6911 | }, |
| 6843 | 6912 | .ErrorSet => { |
| 6844 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 6845 | const module = self.bin_file.options.module.?; | |
| 6846 | const global_error_set = module.global_error_set; | |
| 6847 | const error_index = global_error_set.get(err_name).?; | |
| 6848 | return MCValue{ .immediate = error_index }; | |
| 6913 | switch (typed_value.val.tag()) { | |
| 6914 | .@"error" => { | |
| 6915 | const err_name = typed_value.val.castTag(.@"error").?.data.name; | |
| 6916 | const module = self.bin_file.options.module.?; | |
| 6917 | const global_error_set = module.global_error_set; | |
| 6918 | const error_index = global_error_set.get(err_name).?; | |
| 6919 | return MCValue{ .immediate = error_index }; | |
| 6920 | }, | |
| 6921 | else => { | |
| 6922 | // In this case we are rendering an error union which has a 0 bits payload. | |
| 6923 | return MCValue{ .immediate = 0 }; | |
| 6924 | }, | |
| 6925 | } | |
| 6849 | 6926 | }, |
| 6850 | 6927 | .ErrorUnion => { |
| 6851 | 6928 | const error_type = typed_value.ty.errorUnionSet(); |
| 6852 | 6929 | const payload_type = typed_value.ty.errorUnionPayload(); |
| 6853 | 6930 | |
| 6854 | if (typed_value.val.castTag(.eu_payload)) |_| { | |
| 6855 | if (!payload_type.hasRuntimeBits()) { | |
| 6856 | // We use the error type directly as the type. | |
| 6857 | return MCValue{ .immediate = 0 }; | |
| 6858 | } | |
| 6859 | } else { | |
| 6860 | if (!payload_type.hasRuntimeBits()) { | |
| 6861 | // We use the error type directly as the type. | |
| 6862 | return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val }); | |
| 6863 | } | |
| 6931 | if (error_type.errorSetCardinality() == .zero) { | |
| 6932 | const payload_val = typed_value.val.castTag(.eu_payload).?.data; | |
| 6933 | return self.genTypedValue(.{ .ty = payload_type, .val = payload_val }); | |
| 6934 | } | |
| 6935 | ||
| 6936 | const is_pl = typed_value.val.errorUnionIsPayload(); | |
| 6937 | ||
| 6938 | if (!payload_type.hasRuntimeBitsIgnoreComptime()) { | |
| 6939 | // We use the error type directly as the type. | |
| 6940 | const err_val = if (!is_pl) typed_value.val else Value.initTag(.zero); | |
| 6941 | return self.genTypedValue(.{ .ty = error_type, .val = err_val }); | |
| 6864 | 6942 | } |
| 6865 | 6943 | }, |
| 6866 | 6944 | |
| ... | ... | @@ -6868,7 +6946,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 6868 | 6946 | .ComptimeFloat => unreachable, |
| 6869 | 6947 | .Type => unreachable, |
| 6870 | 6948 | .EnumLiteral => unreachable, |
| 6871 | .Void => unreachable, | |
| 6872 | 6949 | .NoReturn => unreachable, |
| 6873 | 6950 | .Undefined => unreachable, |
| 6874 | 6951 | .Null => unreachable, |
| ... | ... | @@ -6922,11 +6999,14 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6922 | 6999 | // Return values |
| 6923 | 7000 | if (ret_ty.zigTypeTag() == .NoReturn) { |
| 6924 | 7001 | result.return_value = .{ .unreach = {} }; |
| 6925 | } else if (!ret_ty.hasRuntimeBits()) { | |
| 7002 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError()) { | |
| 6926 | 7003 | result.return_value = .{ .none = {} }; |
| 6927 | 7004 | } else { |
| 6928 | 7005 | const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*)); |
| 6929 | if (ret_ty_size <= 8) { | |
| 7006 | if (ret_ty_size == 0) { | |
| 7007 | assert(ret_ty.isError()); | |
| 7008 | result.return_value = .{ .immediate = 0 }; | |
| 7009 | } else if (ret_ty_size <= 8) { | |
| 6930 | 7010 | const aliased_reg = registerAlias(c_abi_int_return_regs[0], ret_ty_size); |
| 6931 | 7011 | result.return_value = .{ .register = aliased_reg }; |
| 6932 | 7012 | } else { |
src/codegen.zig+67-10| ... | ... | @@ -442,7 +442,10 @@ pub fn generateSymbol( |
| 442 | 442 | .Int => { |
| 443 | 443 | const info = typed_value.ty.intInfo(target); |
| 444 | 444 | if (info.bits <= 8) { |
| 445 | const x = @intCast(u8, typed_value.val.toUnsignedInt(target)); | |
| 445 | const x: u8 = switch (info.signedness) { | |
| 446 | .unsigned => @intCast(u8, typed_value.val.toUnsignedInt(target)), | |
| 447 | .signed => @bitCast(u8, @intCast(i8, typed_value.val.toSignedInt())), | |
| 448 | }; | |
| 446 | 449 | try code.append(x); |
| 447 | 450 | return Result{ .appended = {} }; |
| 448 | 451 | } |
| ... | ... | @@ -654,7 +657,7 @@ pub fn generateSymbol( |
| 654 | 657 | return Result{ .appended = {} }; |
| 655 | 658 | } |
| 656 | 659 | |
| 657 | if (typed_value.ty.isPtrLikeOptional()) { | |
| 660 | if (typed_value.ty.optionalReprIsPayload()) { | |
| 658 | 661 | if (typed_value.val.castTag(.opt_payload)) |payload| { |
| 659 | 662 | switch (try generateSymbol(bin_file, src_loc, .{ |
| 660 | 663 | .ty = payload_type, |
| ... | ... | @@ -702,16 +705,50 @@ pub fn generateSymbol( |
| 702 | 705 | .ErrorUnion => { |
| 703 | 706 | const error_ty = typed_value.ty.errorUnionSet(); |
| 704 | 707 | const payload_ty = typed_value.ty.errorUnionPayload(); |
| 708 | ||
| 709 | if (error_ty.errorSetCardinality() == .zero) { | |
| 710 | const payload_val = typed_value.val.castTag(.eu_payload).?.data; | |
| 711 | return generateSymbol(bin_file, src_loc, .{ | |
| 712 | .ty = payload_ty, | |
| 713 | .val = payload_val, | |
| 714 | }, code, debug_output, reloc_info); | |
| 715 | } | |
| 716 | ||
| 705 | 717 | const is_payload = typed_value.val.errorUnionIsPayload(); |
| 706 | 718 | |
| 719 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 720 | const err_val = if (is_payload) Value.initTag(.zero) else typed_value.val; | |
| 721 | return generateSymbol(bin_file, src_loc, .{ | |
| 722 | .ty = error_ty, | |
| 723 | .val = err_val, | |
| 724 | }, code, debug_output, reloc_info); | |
| 725 | } | |
| 726 | ||
| 727 | const payload_align = payload_ty.abiAlignment(target); | |
| 728 | const error_align = Type.anyerror.abiAlignment(target); | |
| 707 | 729 | const abi_align = typed_value.ty.abiAlignment(target); |
| 708 | 730 | |
| 731 | // error value first when its type is larger than the error union's payload | |
| 732 | if (error_align > payload_align) { | |
| 733 | switch (try generateSymbol(bin_file, src_loc, .{ | |
| 734 | .ty = error_ty, | |
| 735 | .val = if (is_payload) Value.initTag(.zero) else typed_value.val, | |
| 736 | }, code, debug_output, reloc_info)) { | |
| 737 | .appended => {}, | |
| 738 | .externally_managed => |external_slice| { | |
| 739 | code.appendSliceAssumeCapacity(external_slice); | |
| 740 | }, | |
| 741 | .fail => |em| return Result{ .fail = em }, | |
| 742 | } | |
| 743 | } | |
| 744 | ||
| 745 | // emit payload part of the error union | |
| 709 | 746 | { |
| 710 | const error_val = if (!is_payload) typed_value.val else Value.initTag(.zero); | |
| 711 | 747 | const begin = code.items.len; |
| 748 | const payload_val = if (typed_value.val.castTag(.eu_payload)) |val| val.data else Value.initTag(.undef); | |
| 712 | 749 | switch (try generateSymbol(bin_file, src_loc, .{ |
| 713 | .ty = error_ty, | |
| 714 | .val = error_val, | |
| 750 | .ty = payload_ty, | |
| 751 | .val = payload_val, | |
| 715 | 752 | }, code, debug_output, reloc_info)) { |
| 716 | 753 | .appended => {}, |
| 717 | 754 | .externally_managed => |external_slice| { |
| ... | ... | @@ -728,12 +765,12 @@ pub fn generateSymbol( |
| 728 | 765 | } |
| 729 | 766 | } |
| 730 | 767 | |
| 731 | if (payload_ty.hasRuntimeBits()) { | |
| 768 | // Payload size is larger than error set, so emit our error set last | |
| 769 | if (error_align <= payload_align) { | |
| 732 | 770 | const begin = code.items.len; |
| 733 | const payload_val = if (typed_value.val.castTag(.eu_payload)) |val| val.data else Value.initTag(.undef); | |
| 734 | 771 | switch (try generateSymbol(bin_file, src_loc, .{ |
| 735 | .ty = payload_ty, | |
| 736 | .val = payload_val, | |
| 772 | .ty = error_ty, | |
| 773 | .val = if (is_payload) Value.initTag(.zero) else typed_value.val, | |
| 737 | 774 | }, code, debug_output, reloc_info)) { |
| 738 | 775 | .appended => {}, |
| 739 | 776 | .externally_managed => |external_slice| { |
| ... | ... | @@ -760,7 +797,7 @@ pub fn generateSymbol( |
| 760 | 797 | try code.writer().writeInt(u32, kv.value, endian); |
| 761 | 798 | }, |
| 762 | 799 | else => { |
| 763 | try code.writer().writeByteNTimes(0, @intCast(usize, typed_value.ty.abiSize(target))); | |
| 800 | try code.writer().writeByteNTimes(0, @intCast(usize, Type.anyerror.abiSize(target))); | |
| 764 | 801 | }, |
| 765 | 802 | } |
| 766 | 803 | return Result{ .appended = {} }; |
| ... | ... | @@ -853,3 +890,23 @@ fn lowerDeclRef( |
| 853 | 890 | |
| 854 | 891 | return Result{ .appended = {} }; |
| 855 | 892 | } |
| 893 | ||
| 894 | pub fn errUnionPayloadOffset(payload_ty: Type, target: std.Target) u64 { | |
| 895 | const payload_align = payload_ty.abiAlignment(target); | |
| 896 | const error_align = Type.anyerror.abiAlignment(target); | |
| 897 | if (payload_align >= error_align) { | |
| 898 | return 0; | |
| 899 | } else { | |
| 900 | return mem.alignForwardGeneric(u64, Type.anyerror.abiSize(target), payload_align); | |
| 901 | } | |
| 902 | } | |
| 903 | ||
| 904 | pub fn errUnionErrorOffset(payload_ty: Type, target: std.Target) u64 { | |
| 905 | const payload_align = payload_ty.abiAlignment(target); | |
| 906 | const error_align = Type.anyerror.abiAlignment(target); | |
| 907 | if (payload_align >= error_align) { | |
| 908 | return mem.alignForwardGeneric(u64, payload_ty.abiSize(target), error_align); | |
| 909 | } else { | |
| 910 | return 0; | |
| 911 | } | |
| 912 | } |
src/codegen/c.zig+177-85| ... | ... | @@ -711,21 +711,24 @@ pub const DeclGen = struct { |
| 711 | 711 | .Bool => return writer.print("{}", .{val.toBool()}), |
| 712 | 712 | .Optional => { |
| 713 | 713 | var opt_buf: Type.Payload.ElemType = undefined; |
| 714 | const payload_type = ty.optionalChild(&opt_buf); | |
| 715 | if (ty.isPtrLikeOptional()) { | |
| 716 | return dg.renderValue(writer, payload_type, val, location); | |
| 717 | } | |
| 718 | if (payload_type.abiSize(target) == 0) { | |
| 714 | const payload_ty = ty.optionalChild(&opt_buf); | |
| 715 | ||
| 716 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 719 | 717 | const is_null = val.castTag(.opt_payload) == null; |
| 720 | 718 | return writer.print("{}", .{is_null}); |
| 721 | 719 | } |
| 720 | ||
| 721 | if (ty.optionalReprIsPayload()) { | |
| 722 | return dg.renderValue(writer, payload_ty, val, location); | |
| 723 | } | |
| 724 | ||
| 722 | 725 | try writer.writeByte('('); |
| 723 | 726 | try dg.renderTypecast(writer, ty); |
| 724 | 727 | try writer.writeAll("){"); |
| 725 | 728 | if (val.castTag(.opt_payload)) |pl| { |
| 726 | 729 | const payload_val = pl.data; |
| 727 | 730 | try writer.writeAll(" .is_null = false, .payload = "); |
| 728 | try dg.renderValue(writer, payload_type, payload_val, location); | |
| 731 | try dg.renderValue(writer, payload_ty, payload_val, location); | |
| 729 | 732 | try writer.writeAll(" }"); |
| 730 | 733 | } else { |
| 731 | 734 | try writer.writeAll(" .is_null = true }"); |
| ... | ... | @@ -749,6 +752,12 @@ pub const DeclGen = struct { |
| 749 | 752 | const error_type = ty.errorUnionSet(); |
| 750 | 753 | const payload_type = ty.errorUnionPayload(); |
| 751 | 754 | |
| 755 | if (error_type.errorSetCardinality() == .zero) { | |
| 756 | // We use the payload directly as the type. | |
| 757 | const payload_val = val.castTag(.eu_payload).?.data; | |
| 758 | return dg.renderValue(writer, payload_type, payload_val, location); | |
| 759 | } | |
| 760 | ||
| 752 | 761 | if (!payload_type.hasRuntimeBits()) { |
| 753 | 762 | // We use the error type directly as the type. |
| 754 | 763 | const err_val = if (val.errorUnionIsPayload()) Value.initTag(.zero) else val; |
| ... | ... | @@ -894,10 +903,12 @@ pub const DeclGen = struct { |
| 894 | 903 | try w.writeAll("ZIG_COLD "); |
| 895 | 904 | } |
| 896 | 905 | } |
| 897 | const return_ty = dg.decl.ty.fnReturnType(); | |
| 898 | if (return_ty.hasRuntimeBits()) { | |
| 899 | try dg.renderType(w, return_ty); | |
| 900 | } else if (return_ty.zigTypeTag() == .NoReturn) { | |
| 906 | const fn_info = dg.decl.ty.fnInfo(); | |
| 907 | if (fn_info.return_type.hasRuntimeBits()) { | |
| 908 | try dg.renderType(w, fn_info.return_type); | |
| 909 | } else if (fn_info.return_type.isError()) { | |
| 910 | try dg.renderType(w, Type.anyerror); | |
| 911 | } else if (fn_info.return_type.zigTypeTag() == .NoReturn) { | |
| 901 | 912 | try w.writeAll("zig_noreturn void"); |
| 902 | 913 | } else { |
| 903 | 914 | try w.writeAll("void"); |
| ... | ... | @@ -905,22 +916,19 @@ pub const DeclGen = struct { |
| 905 | 916 | try w.writeAll(" "); |
| 906 | 917 | try dg.renderDeclName(w, dg.decl_index); |
| 907 | 918 | try w.writeAll("("); |
| 908 | const param_len = dg.decl.ty.fnParamLen(); | |
| 909 | 919 | |
| 910 | var index: usize = 0; | |
| 911 | 920 | var params_written: usize = 0; |
| 912 | while (index < param_len) : (index += 1) { | |
| 913 | const param_type = dg.decl.ty.fnParamType(index); | |
| 921 | for (fn_info.param_types) |param_type, index| { | |
| 914 | 922 | if (!param_type.hasRuntimeBitsIgnoreComptime()) continue; |
| 915 | 923 | if (params_written > 0) { |
| 916 | 924 | try w.writeAll(", "); |
| 917 | 925 | } |
| 918 | 926 | const name = CValue{ .arg = index }; |
| 919 | try dg.renderTypeAndName(w, dg.decl.ty.fnParamType(index), name, .Mut, 0); | |
| 927 | try dg.renderTypeAndName(w, param_type, name, .Mut, 0); | |
| 920 | 928 | params_written += 1; |
| 921 | 929 | } |
| 922 | 930 | |
| 923 | if (dg.decl.ty.fnIsVarArgs()) { | |
| 931 | if (fn_info.is_var_args) { | |
| 924 | 932 | if (params_written != 0) try w.writeAll(", "); |
| 925 | 933 | try w.writeAll("..."); |
| 926 | 934 | } else if (params_written == 0) { |
| ... | ... | @@ -1156,26 +1164,36 @@ pub const DeclGen = struct { |
| 1156 | 1164 | } |
| 1157 | 1165 | |
| 1158 | 1166 | fn renderErrorUnionTypedef(dg: *DeclGen, t: Type) error{ OutOfMemory, AnalysisFail }![]const u8 { |
| 1159 | const child_type = t.errorUnionPayload(); | |
| 1160 | const err_set_type = t.errorUnionSet(); | |
| 1167 | const payload_ty = t.errorUnionPayload(); | |
| 1168 | const error_ty = t.errorUnionSet(); | |
| 1161 | 1169 | |
| 1162 | 1170 | var buffer = std.ArrayList(u8).init(dg.typedefs.allocator); |
| 1163 | 1171 | defer buffer.deinit(); |
| 1164 | 1172 | const bw = buffer.writer(); |
| 1165 | 1173 | |
| 1166 | try bw.writeAll("typedef struct { "); | |
| 1167 | 1174 | const payload_name = CValue{ .bytes = "payload" }; |
| 1168 | try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, 0); | |
| 1169 | try bw.writeAll("; uint16_t error; } "); | |
| 1175 | const target = dg.module.getTarget(); | |
| 1176 | const payload_align = payload_ty.abiAlignment(target); | |
| 1177 | const error_align = Type.anyerror.abiAlignment(target); | |
| 1178 | if (error_align > payload_align) { | |
| 1179 | try bw.writeAll("typedef struct { "); | |
| 1180 | try dg.renderTypeAndName(bw, payload_ty, payload_name, .Mut, 0); | |
| 1181 | try bw.writeAll("; uint16_t error; } "); | |
| 1182 | } else { | |
| 1183 | try bw.writeAll("typedef struct { uint16_t error; "); | |
| 1184 | try dg.renderTypeAndName(bw, payload_ty, payload_name, .Mut, 0); | |
| 1185 | try bw.writeAll("; } "); | |
| 1186 | } | |
| 1187 | ||
| 1170 | 1188 | const name_index = buffer.items.len; |
| 1171 | if (err_set_type.castTag(.error_set_inferred)) |inf_err_set_payload| { | |
| 1189 | if (error_ty.castTag(.error_set_inferred)) |inf_err_set_payload| { | |
| 1172 | 1190 | const func = inf_err_set_payload.data.func; |
| 1173 | 1191 | try bw.writeAll("zig_E_"); |
| 1174 | 1192 | try dg.renderDeclName(bw, func.owner_decl); |
| 1175 | 1193 | try bw.writeAll(";\n"); |
| 1176 | 1194 | } else { |
| 1177 | 1195 | try bw.print("zig_E_{s}_{s};\n", .{ |
| 1178 | typeToCIdentifier(err_set_type, dg.module), typeToCIdentifier(child_type, dg.module), | |
| 1196 | typeToCIdentifier(error_ty, dg.module), typeToCIdentifier(payload_ty, dg.module), | |
| 1179 | 1197 | }); |
| 1180 | 1198 | } |
| 1181 | 1199 | |
| ... | ... | @@ -1345,12 +1363,12 @@ pub const DeclGen = struct { |
| 1345 | 1363 | var opt_buf: Type.Payload.ElemType = undefined; |
| 1346 | 1364 | const child_type = t.optionalChild(&opt_buf); |
| 1347 | 1365 | |
| 1348 | if (t.isPtrLikeOptional()) { | |
| 1349 | return dg.renderType(w, child_type); | |
| 1366 | if (!child_type.hasRuntimeBitsIgnoreComptime()) { | |
| 1367 | return w.writeAll("bool"); | |
| 1350 | 1368 | } |
| 1351 | 1369 | |
| 1352 | if (child_type.abiSize(target) == 0) { | |
| 1353 | return w.writeAll("bool"); | |
| 1370 | if (t.optionalReprIsPayload()) { | |
| 1371 | return dg.renderType(w, child_type); | |
| 1354 | 1372 | } |
| 1355 | 1373 | |
| 1356 | 1374 | const name = dg.getTypedefName(t) orelse |
| ... | ... | @@ -1359,12 +1377,19 @@ pub const DeclGen = struct { |
| 1359 | 1377 | return w.writeAll(name); |
| 1360 | 1378 | }, |
| 1361 | 1379 | .ErrorSet => { |
| 1362 | comptime assert(Type.initTag(.anyerror).abiSize(builtin.target) == 2); | |
| 1380 | comptime assert(Type.anyerror.abiSize(builtin.target) == 2); | |
| 1363 | 1381 | return w.writeAll("uint16_t"); |
| 1364 | 1382 | }, |
| 1365 | 1383 | .ErrorUnion => { |
| 1366 | if (t.errorUnionPayload().abiSize(target) == 0) { | |
| 1367 | return dg.renderType(w, t.errorUnionSet()); | |
| 1384 | const error_ty = t.errorUnionSet(); | |
| 1385 | const payload_ty = t.errorUnionPayload(); | |
| 1386 | ||
| 1387 | if (error_ty.errorSetCardinality() == .zero) { | |
| 1388 | return dg.renderType(w, payload_ty); | |
| 1389 | } | |
| 1390 | ||
| 1391 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 1392 | return dg.renderType(w, Type.anyerror); | |
| 1368 | 1393 | } |
| 1369 | 1394 | |
| 1370 | 1395 | const name = dg.getTypedefName(t) orelse |
| ... | ... | @@ -1794,8 +1819,9 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 1794 | 1819 | .not => try airNot (f, inst), |
| 1795 | 1820 | |
| 1796 | 1821 | .optional_payload => try airOptionalPayload(f, inst), |
| 1797 | .optional_payload_ptr => try airOptionalPayload(f, inst), | |
| 1822 | .optional_payload_ptr => try airOptionalPayloadPtr(f, inst), | |
| 1798 | 1823 | .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst), |
| 1824 | .wrap_optional => try airWrapOptional(f, inst), | |
| 1799 | 1825 | |
| 1800 | 1826 | .is_err => try airIsErr(f, inst, false, "!="), |
| 1801 | 1827 | .is_non_err => try airIsErr(f, inst, false, "=="), |
| ... | ... | @@ -1824,7 +1850,6 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 1824 | 1850 | .cond_br => try airCondBr(f, inst), |
| 1825 | 1851 | .br => try airBr(f, inst), |
| 1826 | 1852 | .switch_br => try airSwitchBr(f, inst), |
| 1827 | .wrap_optional => try airWrapOptional(f, inst), | |
| 1828 | 1853 | .struct_field_ptr => try airStructFieldPtr(f, inst), |
| 1829 | 1854 | .array_to_slice => try airArrayToSlice(f, inst), |
| 1830 | 1855 | .cmpxchg_weak => try airCmpxchg(f, inst, "weak"), |
| ... | ... | @@ -1901,8 +1926,8 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 1901 | 1926 | .array_elem_val => try airArrayElemVal(f, inst), |
| 1902 | 1927 | |
| 1903 | 1928 | .unwrap_errunion_payload => try airUnwrapErrUnionPay(f, inst, ""), |
| 1904 | .unwrap_errunion_err => try airUnwrapErrUnionErr(f, inst), | |
| 1905 | 1929 | .unwrap_errunion_payload_ptr => try airUnwrapErrUnionPay(f, inst, "&"), |
| 1930 | .unwrap_errunion_err => try airUnwrapErrUnionErr(f, inst), | |
| 1906 | 1931 | .unwrap_errunion_err_ptr => try airUnwrapErrUnionErr(f, inst), |
| 1907 | 1932 | .wrap_errunion_payload => try airWrapErrUnionPay(f, inst), |
| 1908 | 1933 | .wrap_errunion_err => try airWrapErrUnionErr(f, inst), |
| ... | ... | @@ -2120,11 +2145,14 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2120 | 2145 | fn airRet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2121 | 2146 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 2122 | 2147 | const writer = f.object.writer(); |
| 2123 | if (f.air.typeOf(un_op).isFnOrHasRuntimeBitsIgnoreComptime()) { | |
| 2148 | const ret_ty = f.air.typeOf(un_op); | |
| 2149 | if (ret_ty.isFnOrHasRuntimeBitsIgnoreComptime()) { | |
| 2124 | 2150 | const operand = try f.resolveInst(un_op); |
| 2125 | 2151 | try writer.writeAll("return "); |
| 2126 | 2152 | try f.writeCValue(writer, operand); |
| 2127 | 2153 | try writer.writeAll(";\n"); |
| 2154 | } else if (ret_ty.isError()) { | |
| 2155 | try writer.writeAll("return 0;"); | |
| 2128 | 2156 | } else { |
| 2129 | 2157 | try writer.writeAll("return;\n"); |
| 2130 | 2158 | } |
| ... | ... | @@ -2136,13 +2164,16 @@ fn airRetLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2136 | 2164 | const writer = f.object.writer(); |
| 2137 | 2165 | const ptr_ty = f.air.typeOf(un_op); |
| 2138 | 2166 | const ret_ty = ptr_ty.childType(); |
| 2139 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime()) { | |
| 2167 | if (ret_ty.isFnOrHasRuntimeBitsIgnoreComptime()) { | |
| 2168 | const ptr = try f.resolveInst(un_op); | |
| 2169 | try writer.writeAll("return *"); | |
| 2170 | try f.writeCValue(writer, ptr); | |
| 2171 | try writer.writeAll(";\n"); | |
| 2172 | } else if (ret_ty.isError()) { | |
| 2173 | try writer.writeAll("return 0;\n"); | |
| 2174 | } else { | |
| 2140 | 2175 | try writer.writeAll("return;\n"); |
| 2141 | 2176 | } |
| 2142 | const ptr = try f.resolveInst(un_op); | |
| 2143 | try writer.writeAll("return *"); | |
| 2144 | try f.writeCValue(writer, ptr); | |
| 2145 | try writer.writeAll(";\n"); | |
| 2146 | 2177 | return CValue.none; |
| 2147 | 2178 | } |
| 2148 | 2179 | |
| ... | ... | @@ -2713,19 +2744,20 @@ fn airCall( |
| 2713 | 2744 | .Pointer => callee_ty.childType(), |
| 2714 | 2745 | else => unreachable, |
| 2715 | 2746 | }; |
| 2716 | const ret_ty = fn_ty.fnReturnType(); | |
| 2717 | const unused_result = f.liveness.isUnused(inst); | |
| 2718 | 2747 | const writer = f.object.writer(); |
| 2719 | 2748 | |
| 2720 | var result_local: CValue = .none; | |
| 2721 | if (unused_result) { | |
| 2722 | if (ret_ty.hasRuntimeBits()) { | |
| 2723 | try writer.print("(void)", .{}); | |
| 2749 | const result_local: CValue = r: { | |
| 2750 | if (f.liveness.isUnused(inst)) { | |
| 2751 | if (loweredFnRetTyHasBits(fn_ty)) { | |
| 2752 | try writer.print("(void)", .{}); | |
| 2753 | } | |
| 2754 | break :r .none; | |
| 2755 | } else { | |
| 2756 | const local = try f.allocLocal(fn_ty.fnReturnType(), .Const); | |
| 2757 | try writer.writeAll(" = "); | |
| 2758 | break :r local; | |
| 2724 | 2759 | } |
| 2725 | } else { | |
| 2726 | result_local = try f.allocLocal(ret_ty, .Const); | |
| 2727 | try writer.writeAll(" = "); | |
| 2728 | } | |
| 2760 | }; | |
| 2729 | 2761 | |
| 2730 | 2762 | callee: { |
| 2731 | 2763 | known: { |
| ... | ... | @@ -3116,7 +3148,6 @@ fn airIsNull( |
| 3116 | 3148 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 3117 | 3149 | const writer = f.object.writer(); |
| 3118 | 3150 | const operand = try f.resolveInst(un_op); |
| 3119 | const target = f.object.dg.module.getTarget(); | |
| 3120 | 3151 | |
| 3121 | 3152 | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 3122 | 3153 | try writer.writeAll(" = ("); |
| ... | ... | @@ -3124,16 +3155,18 @@ fn airIsNull( |
| 3124 | 3155 | |
| 3125 | 3156 | const ty = f.air.typeOf(un_op); |
| 3126 | 3157 | var opt_buf: Type.Payload.ElemType = undefined; |
| 3127 | const payload_type = if (ty.zigTypeTag() == .Pointer) | |
| 3158 | const payload_ty = if (ty.zigTypeTag() == .Pointer) | |
| 3128 | 3159 | ty.childType().optionalChild(&opt_buf) |
| 3129 | 3160 | else |
| 3130 | 3161 | ty.optionalChild(&opt_buf); |
| 3131 | 3162 | |
| 3132 | if (ty.isPtrLikeOptional()) { | |
| 3163 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 3164 | try writer.print("){s} {s} true;\n", .{ deref_suffix, operator }); | |
| 3165 | } else if (ty.isPtrLikeOptional()) { | |
| 3133 | 3166 | // operand is a regular pointer, test `operand !=/== NULL` |
| 3134 | 3167 | try writer.print("){s} {s} NULL;\n", .{ deref_suffix, operator }); |
| 3135 | } else if (payload_type.abiSize(target) == 0) { | |
| 3136 | try writer.print("){s} {s} true;\n", .{ deref_suffix, operator }); | |
| 3168 | } else if (payload_ty.zigTypeTag() == .ErrorSet) { | |
| 3169 | try writer.print("){s} {s} 0;\n", .{ deref_suffix, operator }); | |
| 3137 | 3170 | } else { |
| 3138 | 3171 | try writer.print("){s}.is_null {s} true;\n", .{ deref_suffix, operator }); |
| 3139 | 3172 | } |
| ... | ... | @@ -3141,34 +3174,58 @@ fn airIsNull( |
| 3141 | 3174 | } |
| 3142 | 3175 | |
| 3143 | 3176 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3144 | if (f.liveness.isUnused(inst)) | |
| 3177 | if (f.liveness.isUnused(inst)) return CValue.none; | |
| 3178 | ||
| 3179 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; | |
| 3180 | const writer = f.object.writer(); | |
| 3181 | const operand = try f.resolveInst(ty_op.operand); | |
| 3182 | const opt_ty = f.air.typeOf(ty_op.operand); | |
| 3183 | ||
| 3184 | var buf: Type.Payload.ElemType = undefined; | |
| 3185 | const payload_ty = opt_ty.optionalChild(&buf); | |
| 3186 | ||
| 3187 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 3145 | 3188 | return CValue.none; |
| 3189 | } | |
| 3190 | ||
| 3191 | if (opt_ty.optionalReprIsPayload()) { | |
| 3192 | return operand; | |
| 3193 | } | |
| 3194 | ||
| 3195 | const inst_ty = f.air.typeOfIndex(inst); | |
| 3196 | const local = try f.allocLocal(inst_ty, .Const); | |
| 3197 | try writer.writeAll(" = ("); | |
| 3198 | try f.writeCValue(writer, operand); | |
| 3199 | try writer.writeAll(").payload;\n"); | |
| 3200 | return local; | |
| 3201 | } | |
| 3202 | ||
| 3203 | fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { | |
| 3204 | if (f.liveness.isUnused(inst)) return CValue.none; | |
| 3146 | 3205 | |
| 3147 | 3206 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 3148 | 3207 | const writer = f.object.writer(); |
| 3149 | 3208 | const operand = try f.resolveInst(ty_op.operand); |
| 3150 | const operand_ty = f.air.typeOf(ty_op.operand); | |
| 3209 | const ptr_ty = f.air.typeOf(ty_op.operand); | |
| 3210 | const opt_ty = ptr_ty.childType(); | |
| 3211 | var buf: Type.Payload.ElemType = undefined; | |
| 3212 | const payload_ty = opt_ty.optionalChild(&buf); | |
| 3151 | 3213 | |
| 3152 | const opt_ty = if (operand_ty.zigTypeTag() == .Pointer) | |
| 3153 | operand_ty.elemType() | |
| 3154 | else | |
| 3155 | operand_ty; | |
| 3214 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 3215 | return operand; | |
| 3216 | } | |
| 3156 | 3217 | |
| 3157 | if (opt_ty.isPtrLikeOptional()) { | |
| 3218 | if (opt_ty.optionalReprIsPayload()) { | |
| 3158 | 3219 | // the operand is just a regular pointer, no need to do anything special. |
| 3159 | 3220 | // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C |
| 3160 | 3221 | return operand; |
| 3161 | 3222 | } |
| 3162 | 3223 | |
| 3163 | 3224 | const inst_ty = f.air.typeOfIndex(inst); |
| 3164 | const maybe_deref = if (operand_ty.zigTypeTag() == .Pointer) "->" else "."; | |
| 3165 | const maybe_addrof = if (inst_ty.zigTypeTag() == .Pointer) "&" else ""; | |
| 3166 | ||
| 3167 | 3225 | const local = try f.allocLocal(inst_ty, .Const); |
| 3168 | try writer.print(" = {s}(", .{maybe_addrof}); | |
| 3226 | try writer.writeAll(" = &("); | |
| 3169 | 3227 | try f.writeCValue(writer, operand); |
| 3170 | ||
| 3171 | try writer.print("){s}payload;\n", .{maybe_deref}); | |
| 3228 | try writer.writeAll(")->payload;\n"); | |
| 3172 | 3229 | return local; |
| 3173 | 3230 | } |
| 3174 | 3231 | |
| ... | ... | @@ -3180,7 +3237,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3180 | 3237 | |
| 3181 | 3238 | const opt_ty = operand_ty.elemType(); |
| 3182 | 3239 | |
| 3183 | if (opt_ty.isPtrLikeOptional()) { | |
| 3240 | if (opt_ty.optionalReprIsPayload()) { | |
| 3184 | 3241 | // The payload and the optional are the same value. |
| 3185 | 3242 | // Setting to non-null will be done when the payload is set. |
| 3186 | 3243 | return operand; |
| ... | ... | @@ -3307,7 +3364,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3307 | 3364 | return local; |
| 3308 | 3365 | } |
| 3309 | 3366 | |
| 3310 | // *(E!T) -> E NOT *E | |
| 3367 | /// *(E!T) -> E | |
| 3368 | /// Note that the result is never a pointer. | |
| 3311 | 3369 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3312 | 3370 | if (f.liveness.isUnused(inst)) |
| 3313 | 3371 | return CValue.none; |
| ... | ... | @@ -3319,7 +3377,11 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3319 | 3377 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 3320 | 3378 | |
| 3321 | 3379 | if (operand_ty.zigTypeTag() == .Pointer) { |
| 3322 | if (!operand_ty.childType().errorUnionPayload().hasRuntimeBits()) { | |
| 3380 | const err_union_ty = operand_ty.childType(); | |
| 3381 | if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 3382 | return CValue{ .bytes = "0" }; | |
| 3383 | } | |
| 3384 | if (!err_union_ty.errorUnionPayload().hasRuntimeBits()) { | |
| 3323 | 3385 | return operand; |
| 3324 | 3386 | } |
| 3325 | 3387 | const local = try f.allocLocal(inst_ty, .Const); |
| ... | ... | @@ -3328,6 +3390,9 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3328 | 3390 | try writer.writeAll(";\n"); |
| 3329 | 3391 | return local; |
| 3330 | 3392 | } |
| 3393 | if (operand_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 3394 | return CValue{ .bytes = "0" }; | |
| 3395 | } | |
| 3331 | 3396 | if (!operand_ty.errorUnionPayload().hasRuntimeBits()) { |
| 3332 | 3397 | return operand; |
| 3333 | 3398 | } |
| ... | ... | @@ -3343,7 +3408,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3343 | 3408 | return local; |
| 3344 | 3409 | } |
| 3345 | 3410 | |
| 3346 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []const u8) !CValue { | |
| 3411 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: [*:0]const u8) !CValue { | |
| 3347 | 3412 | if (f.liveness.isUnused(inst)) |
| 3348 | 3413 | return CValue.none; |
| 3349 | 3414 | |
| ... | ... | @@ -3351,17 +3416,19 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []cons |
| 3351 | 3416 | const writer = f.object.writer(); |
| 3352 | 3417 | const operand = try f.resolveInst(ty_op.operand); |
| 3353 | 3418 | const operand_ty = f.air.typeOf(ty_op.operand); |
| 3419 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; | |
| 3420 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | |
| 3421 | ||
| 3422 | if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 3423 | return operand; | |
| 3424 | } | |
| 3354 | 3425 | |
| 3355 | const error_union_ty = if (operand_ty.zigTypeTag() == .Pointer) | |
| 3356 | operand_ty.childType() | |
| 3357 | else | |
| 3358 | operand_ty; | |
| 3359 | 3426 | if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) { |
| 3360 | 3427 | return CValue.none; |
| 3361 | 3428 | } |
| 3362 | 3429 | |
| 3363 | 3430 | const inst_ty = f.air.typeOfIndex(inst); |
| 3364 | const maybe_deref = if (operand_ty.zigTypeTag() == .Pointer) "->" else "."; | |
| 3431 | const maybe_deref = if (operand_is_ptr) "->" else "."; | |
| 3365 | 3432 | |
| 3366 | 3433 | const local = try f.allocLocal(inst_ty, .Const); |
| 3367 | 3434 | try writer.print(" = {s}(", .{maybe_addrof}); |
| ... | ... | @@ -3380,8 +3447,7 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3380 | 3447 | const operand = try f.resolveInst(ty_op.operand); |
| 3381 | 3448 | |
| 3382 | 3449 | const inst_ty = f.air.typeOfIndex(inst); |
| 3383 | if (inst_ty.isPtrLikeOptional()) { | |
| 3384 | // the operand is just a regular pointer, no need to do anything special. | |
| 3450 | if (inst_ty.optionalReprIsPayload()) { | |
| 3385 | 3451 | return operand; |
| 3386 | 3452 | } |
| 3387 | 3453 | |
| ... | ... | @@ -3421,6 +3487,11 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3421 | 3487 | const error_ty = error_union_ty.errorUnionSet(); |
| 3422 | 3488 | const payload_ty = error_union_ty.errorUnionPayload(); |
| 3423 | 3489 | |
| 3490 | if (error_ty.errorSetCardinality() == .zero) { | |
| 3491 | // TODO: write undefined bytes through the pointer here | |
| 3492 | return operand; | |
| 3493 | } | |
| 3494 | ||
| 3424 | 3495 | // First, set the non-error value. |
| 3425 | 3496 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3426 | 3497 | try f.writeCValueDeref(writer, operand); |
| ... | ... | @@ -3464,6 +3535,9 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3464 | 3535 | const operand = try f.resolveInst(ty_op.operand); |
| 3465 | 3536 | |
| 3466 | 3537 | const inst_ty = f.air.typeOfIndex(inst); |
| 3538 | if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 3539 | return operand; | |
| 3540 | } | |
| 3467 | 3541 | const local = try f.allocLocal(inst_ty, .Const); |
| 3468 | 3542 | try writer.writeAll(" = { .error = 0, .payload = "); |
| 3469 | 3543 | try f.writeCValue(writer, operand); |
| ... | ... | @@ -3486,16 +3560,23 @@ fn airIsErr( |
| 3486 | 3560 | const operand_ty = f.air.typeOf(un_op); |
| 3487 | 3561 | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 3488 | 3562 | const payload_ty = operand_ty.errorUnionPayload(); |
| 3563 | const error_ty = operand_ty.errorUnionSet(); | |
| 3564 | ||
| 3489 | 3565 | try writer.writeAll(" = "); |
| 3490 | if (is_ptr) { | |
| 3491 | try f.writeCValueDeref(writer, operand); | |
| 3566 | ||
| 3567 | if (error_ty.errorSetCardinality() == .zero) { | |
| 3568 | try writer.print("0 {s} 0;\n", .{op_str}); | |
| 3492 | 3569 | } else { |
| 3493 | try f.writeCValue(writer, operand); | |
| 3494 | } | |
| 3495 | if (payload_ty.hasRuntimeBits()) { | |
| 3496 | try writer.writeAll(".error"); | |
| 3570 | if (is_ptr) { | |
| 3571 | try f.writeCValueDeref(writer, operand); | |
| 3572 | } else { | |
| 3573 | try f.writeCValue(writer, operand); | |
| 3574 | } | |
| 3575 | if (payload_ty.hasRuntimeBits()) { | |
| 3576 | try writer.writeAll(".error"); | |
| 3577 | } | |
| 3578 | try writer.print(" {s} 0;\n", .{op_str}); | |
| 3497 | 3579 | } |
| 3498 | try writer.print(" {s} 0;\n", .{op_str}); | |
| 3499 | 3580 | return local; |
| 3500 | 3581 | } |
| 3501 | 3582 | |
| ... | ... | @@ -4129,3 +4210,14 @@ fn intMin(ty: Type, target: std.Target, buf: []u8) []const u8 { |
| 4129 | 4210 | }, |
| 4130 | 4211 | } |
| 4131 | 4212 | } |
| 4213 | ||
| 4214 | fn loweredFnRetTyHasBits(fn_ty: Type) bool { | |
| 4215 | const ret_ty = fn_ty.fnReturnType(); | |
| 4216 | if (ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 4217 | return true; | |
| 4218 | } | |
| 4219 | if (ret_ty.isError()) { | |
| 4220 | return true; | |
| 4221 | } | |
| 4222 | return false; | |
| 4223 | } |
src/codegen/llvm.zig+396-301| ... | ... | @@ -745,7 +745,7 @@ pub const Object = struct { |
| 745 | 745 | const param = llvm_func.getParam(llvm_arg_i); |
| 746 | 746 | llvm_arg_i += 1; |
| 747 | 747 | |
| 748 | const param_llvm_ty = try dg.llvmType(param_ty); | |
| 748 | const param_llvm_ty = try dg.lowerType(param_ty); | |
| 749 | 749 | const abi_size = @intCast(c_uint, param_ty.abiSize(target)); |
| 750 | 750 | const int_llvm_ty = dg.context.intType(abi_size * 8); |
| 751 | 751 | const int_ptr_llvm_ty = int_llvm_ty.pointerType(0); |
| ... | ... | @@ -775,7 +775,7 @@ pub const Object = struct { |
| 775 | 775 | .Struct => { |
| 776 | 776 | const fields = param_ty.structFields().values(); |
| 777 | 777 | if (is_by_ref) { |
| 778 | const param_llvm_ty = try dg.llvmType(param_ty); | |
| 778 | const param_llvm_ty = try dg.lowerType(param_ty); | |
| 779 | 779 | const arg_ptr = buildAllocaInner(builder, llvm_func, false, param_llvm_ty); |
| 780 | 780 | arg_ptr.setAlignment(param_ty.abiAlignment(target)); |
| 781 | 781 | |
| ... | ... | @@ -1390,7 +1390,7 @@ pub const Object = struct { |
| 1390 | 1390 | gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_ty); |
| 1391 | 1391 | return di_ty; |
| 1392 | 1392 | } |
| 1393 | if (ty.isPtrLikeOptional()) { | |
| 1393 | if (ty.optionalReprIsPayload()) { | |
| 1394 | 1394 | const ptr_di_ty = try o.lowerDebugType(child_ty, resolve); |
| 1395 | 1395 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1396 | 1396 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(ptr_di_ty), .{ .mod = o.module }); |
| ... | ... | @@ -1470,10 +1470,25 @@ pub const Object = struct { |
| 1470 | 1470 | return full_di_ty; |
| 1471 | 1471 | }, |
| 1472 | 1472 | .ErrorUnion => { |
| 1473 | const err_set_ty = ty.errorUnionSet(); | |
| 1474 | 1473 | const payload_ty = ty.errorUnionPayload(); |
| 1474 | switch (ty.errorUnionSet().errorSetCardinality()) { | |
| 1475 | .zero => { | |
| 1476 | const payload_di_ty = try o.lowerDebugType(payload_ty, .full); | |
| 1477 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. | |
| 1478 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(payload_di_ty), .{ .mod = o.module }); | |
| 1479 | return payload_di_ty; | |
| 1480 | }, | |
| 1481 | .one => { | |
| 1482 | if (payload_ty.isNoReturn()) { | |
| 1483 | const di_type = dib.createBasicType("void", 0, DW.ATE.signed); | |
| 1484 | gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type); | |
| 1485 | return di_type; | |
| 1486 | } | |
| 1487 | }, | |
| 1488 | .many => {}, | |
| 1489 | } | |
| 1475 | 1490 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1476 | const err_set_di_ty = try o.lowerDebugType(err_set_ty, .full); | |
| 1491 | const err_set_di_ty = try o.lowerDebugType(Type.anyerror, .full); | |
| 1477 | 1492 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1478 | 1493 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(err_set_di_ty), .{ .mod = o.module }); |
| 1479 | 1494 | return err_set_di_ty; |
| ... | ... | @@ -1496,56 +1511,51 @@ pub const Object = struct { |
| 1496 | 1511 | break :blk fwd_decl; |
| 1497 | 1512 | }; |
| 1498 | 1513 | |
| 1499 | const err_set_size = err_set_ty.abiSize(target); | |
| 1500 | const err_set_align = err_set_ty.abiAlignment(target); | |
| 1514 | const error_size = Type.anyerror.abiSize(target); | |
| 1515 | const error_align = Type.anyerror.abiAlignment(target); | |
| 1501 | 1516 | const payload_size = payload_ty.abiSize(target); |
| 1502 | 1517 | const payload_align = payload_ty.abiAlignment(target); |
| 1503 | 1518 | |
| 1504 | var offset: u64 = 0; | |
| 1505 | offset += err_set_size; | |
| 1506 | offset = std.mem.alignForwardGeneric(u64, offset, payload_align); | |
| 1507 | const payload_offset = offset; | |
| 1508 | ||
| 1509 | var len: u8 = 2; | |
| 1510 | var fields: [3]*llvm.DIType = .{ | |
| 1511 | dib.createMemberType( | |
| 1512 | fwd_decl.toScope(), | |
| 1513 | "tag", | |
| 1514 | di_file, | |
| 1515 | line, | |
| 1516 | err_set_size * 8, // size in bits | |
| 1517 | err_set_align * 8, // align in bits | |
| 1518 | 0, // offset in bits | |
| 1519 | 0, // flags | |
| 1520 | try o.lowerDebugType(err_set_ty, .full), | |
| 1521 | ), | |
| 1522 | dib.createMemberType( | |
| 1523 | fwd_decl.toScope(), | |
| 1524 | "value", | |
| 1525 | di_file, | |
| 1526 | line, | |
| 1527 | payload_size * 8, // size in bits | |
| 1528 | payload_align * 8, // align in bits | |
| 1529 | payload_offset * 8, // offset in bits | |
| 1530 | 0, // flags | |
| 1531 | try o.lowerDebugType(payload_ty, .full), | |
| 1532 | ), | |
| 1533 | undefined, | |
| 1534 | }; | |
| 1535 | ||
| 1536 | const error_size = Type.anyerror.abiSize(target); | |
| 1537 | if (payload_align > error_size) { | |
| 1538 | fields[2] = fields[1]; | |
| 1539 | const pad_len = @intCast(u32, payload_align - error_size); | |
| 1540 | fields[1] = dib.createArrayType( | |
| 1541 | pad_len * 8, | |
| 1542 | 8, | |
| 1543 | try o.lowerDebugType(Type.u8, .full), | |
| 1544 | @intCast(c_int, pad_len), | |
| 1545 | ); | |
| 1546 | len += 1; | |
| 1519 | var error_index: u32 = undefined; | |
| 1520 | var payload_index: u32 = undefined; | |
| 1521 | var error_offset: u64 = undefined; | |
| 1522 | var payload_offset: u64 = undefined; | |
| 1523 | if (error_align > payload_align) { | |
| 1524 | error_index = 0; | |
| 1525 | payload_index = 1; | |
| 1526 | error_offset = 0; | |
| 1527 | payload_offset = std.mem.alignForwardGeneric(u64, error_size, payload_align); | |
| 1528 | } else { | |
| 1529 | payload_index = 0; | |
| 1530 | error_index = 1; | |
| 1531 | payload_offset = 0; | |
| 1532 | error_offset = std.mem.alignForwardGeneric(u64, payload_size, error_align); | |
| 1547 | 1533 | } |
| 1548 | 1534 | |
| 1535 | var fields: [2]*llvm.DIType = undefined; | |
| 1536 | fields[error_index] = dib.createMemberType( | |
| 1537 | fwd_decl.toScope(), | |
| 1538 | "tag", | |
| 1539 | di_file, | |
| 1540 | line, | |
| 1541 | error_size * 8, // size in bits | |
| 1542 | error_align * 8, // align in bits | |
| 1543 | error_offset * 8, // offset in bits | |
| 1544 | 0, // flags | |
| 1545 | try o.lowerDebugType(Type.anyerror, .full), | |
| 1546 | ); | |
| 1547 | fields[payload_index] = dib.createMemberType( | |
| 1548 | fwd_decl.toScope(), | |
| 1549 | "value", | |
| 1550 | di_file, | |
| 1551 | line, | |
| 1552 | payload_size * 8, // size in bits | |
| 1553 | payload_align * 8, // align in bits | |
| 1554 | payload_offset * 8, // offset in bits | |
| 1555 | 0, // flags | |
| 1556 | try o.lowerDebugType(payload_ty, .full), | |
| 1557 | ); | |
| 1558 | ||
| 1549 | 1559 | const full_di_ty = dib.createStructType( |
| 1550 | 1560 | compile_unit_scope, |
| 1551 | 1561 | name.ptr, |
| ... | ... | @@ -1556,7 +1566,7 @@ pub const Object = struct { |
| 1556 | 1566 | 0, // flags |
| 1557 | 1567 | null, // derived from |
| 1558 | 1568 | &fields, |
| 1559 | len, | |
| 1569 | fields.len, | |
| 1560 | 1570 | 0, // run time lang |
| 1561 | 1571 | null, // vtable holder |
| 1562 | 1572 | "", // unique id |
| ... | ... | @@ -2094,7 +2104,7 @@ pub const DeclGen = struct { |
| 2094 | 2104 | break :init_val decl.val; |
| 2095 | 2105 | }; |
| 2096 | 2106 | if (init_val.tag() != .unreachable_value) { |
| 2097 | const llvm_init = try dg.genTypedValue(.{ .ty = decl.ty, .val = init_val }); | |
| 2107 | const llvm_init = try dg.lowerValue(.{ .ty = decl.ty, .val = init_val }); | |
| 2098 | 2108 | if (global.globalGetValueType() == llvm_init.typeOf()) { |
| 2099 | 2109 | global.setInitializer(llvm_init); |
| 2100 | 2110 | } else { |
| ... | ... | @@ -2165,7 +2175,7 @@ pub const DeclGen = struct { |
| 2165 | 2175 | const target = dg.module.getTarget(); |
| 2166 | 2176 | const sret = firstParamSRet(fn_info, target); |
| 2167 | 2177 | |
| 2168 | const fn_type = try dg.llvmType(zig_fn_type); | |
| 2178 | const fn_type = try dg.lowerType(zig_fn_type); | |
| 2169 | 2179 | |
| 2170 | 2180 | const fqn = try decl.getFullyQualifiedName(dg.module); |
| 2171 | 2181 | defer dg.gpa.free(fqn); |
| ... | ... | @@ -2192,7 +2202,7 @@ pub const DeclGen = struct { |
| 2192 | 2202 | dg.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0 |
| 2193 | 2203 | dg.addArgAttr(llvm_fn, 0, "noalias"); |
| 2194 | 2204 | |
| 2195 | const raw_llvm_ret_ty = try dg.llvmType(fn_info.return_type); | |
| 2205 | const raw_llvm_ret_ty = try dg.lowerType(fn_info.return_type); | |
| 2196 | 2206 | llvm_fn.addSretAttr(0, raw_llvm_ret_ty); |
| 2197 | 2207 | } |
| 2198 | 2208 | |
| ... | ... | @@ -2285,7 +2295,7 @@ pub const DeclGen = struct { |
| 2285 | 2295 | const fqn = try decl.getFullyQualifiedName(dg.module); |
| 2286 | 2296 | defer dg.gpa.free(fqn); |
| 2287 | 2297 | |
| 2288 | const llvm_type = try dg.llvmType(decl.ty); | |
| 2298 | const llvm_type = try dg.lowerType(decl.ty); | |
| 2289 | 2299 | const llvm_addrspace = dg.llvmAddressSpace(decl.@"addrspace"); |
| 2290 | 2300 | const llvm_global = dg.object.llvm_module.addGlobalInAddressSpace(llvm_type, fqn, llvm_addrspace); |
| 2291 | 2301 | gop.value_ptr.* = llvm_global; |
| ... | ... | @@ -2339,15 +2349,15 @@ pub const DeclGen = struct { |
| 2339 | 2349 | } |
| 2340 | 2350 | |
| 2341 | 2351 | fn isUnnamedType(dg: *DeclGen, ty: Type, val: *const llvm.Value) bool { |
| 2342 | // Once `llvmType` succeeds, successive calls to it with the same Zig type | |
| 2343 | // are guaranteed to succeed. So if a call to `llvmType` fails here it means | |
| 2352 | // Once `lowerType` succeeds, successive calls to it with the same Zig type | |
| 2353 | // are guaranteed to succeed. So if a call to `lowerType` fails here it means | |
| 2344 | 2354 | // it is the first time lowering the type, which means the value can't possible |
| 2345 | 2355 | // have that type. |
| 2346 | const llvm_ty = dg.llvmType(ty) catch return true; | |
| 2356 | const llvm_ty = dg.lowerType(ty) catch return true; | |
| 2347 | 2357 | return val.typeOf() != llvm_ty; |
| 2348 | 2358 | } |
| 2349 | 2359 | |
| 2350 | fn llvmType(dg: *DeclGen, t: Type) Allocator.Error!*const llvm.Type { | |
| 2360 | fn lowerType(dg: *DeclGen, t: Type) Allocator.Error!*const llvm.Type { | |
| 2351 | 2361 | const gpa = dg.gpa; |
| 2352 | 2362 | const target = dg.module.getTarget(); |
| 2353 | 2363 | switch (t.zigTypeTag()) { |
| ... | ... | @@ -2379,8 +2389,8 @@ pub const DeclGen = struct { |
| 2379 | 2389 | const ptr_type = t.slicePtrFieldType(&buf); |
| 2380 | 2390 | |
| 2381 | 2391 | const fields: [2]*const llvm.Type = .{ |
| 2382 | try dg.llvmType(ptr_type), | |
| 2383 | try dg.llvmType(Type.usize), | |
| 2392 | try dg.lowerType(ptr_type), | |
| 2393 | try dg.lowerType(Type.usize), | |
| 2384 | 2394 | }; |
| 2385 | 2395 | return dg.context.structType(&fields, fields.len, .False); |
| 2386 | 2396 | } |
| ... | ... | @@ -2396,7 +2406,7 @@ pub const DeclGen = struct { |
| 2396 | 2406 | else => elem_ty.hasRuntimeBitsIgnoreComptime(), |
| 2397 | 2407 | }; |
| 2398 | 2408 | const llvm_elem_ty = if (lower_elem_ty) |
| 2399 | try dg.llvmType(elem_ty) | |
| 2409 | try dg.lowerType(elem_ty) | |
| 2400 | 2410 | else |
| 2401 | 2411 | dg.context.intType(8); |
| 2402 | 2412 | return llvm_elem_ty.pointerType(llvm_addrspace); |
| ... | ... | @@ -2424,12 +2434,12 @@ pub const DeclGen = struct { |
| 2424 | 2434 | .Array => { |
| 2425 | 2435 | const elem_ty = t.childType(); |
| 2426 | 2436 | assert(elem_ty.onePossibleValue() == null); |
| 2427 | const elem_llvm_ty = try dg.llvmType(elem_ty); | |
| 2437 | const elem_llvm_ty = try dg.lowerType(elem_ty); | |
| 2428 | 2438 | const total_len = t.arrayLen() + @boolToInt(t.sentinel() != null); |
| 2429 | 2439 | return elem_llvm_ty.arrayType(@intCast(c_uint, total_len)); |
| 2430 | 2440 | }, |
| 2431 | 2441 | .Vector => { |
| 2432 | const elem_type = try dg.llvmType(t.childType()); | |
| 2442 | const elem_type = try dg.lowerType(t.childType()); | |
| 2433 | 2443 | return elem_type.vectorType(t.vectorLen()); |
| 2434 | 2444 | }, |
| 2435 | 2445 | .Optional => { |
| ... | ... | @@ -2438,8 +2448,8 @@ pub const DeclGen = struct { |
| 2438 | 2448 | if (!child_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2439 | 2449 | return dg.context.intType(1); |
| 2440 | 2450 | } |
| 2441 | const payload_llvm_ty = try dg.llvmType(child_ty); | |
| 2442 | if (t.isPtrLikeOptional()) { | |
| 2451 | const payload_llvm_ty = try dg.lowerType(child_ty); | |
| 2452 | if (t.optionalReprIsPayload()) { | |
| 2443 | 2453 | return payload_llvm_ty; |
| 2444 | 2454 | } |
| 2445 | 2455 | |
| ... | ... | @@ -2449,28 +2459,33 @@ pub const DeclGen = struct { |
| 2449 | 2459 | return dg.context.structType(&fields, fields.len, .False); |
| 2450 | 2460 | }, |
| 2451 | 2461 | .ErrorUnion => { |
| 2452 | const error_type = t.errorUnionSet(); | |
| 2453 | const payload_type = t.errorUnionPayload(); | |
| 2454 | const llvm_error_type = try dg.llvmType(error_type); | |
| 2455 | if (!payload_type.hasRuntimeBitsIgnoreComptime()) { | |
| 2456 | return llvm_error_type; | |
| 2462 | const payload_ty = t.errorUnionPayload(); | |
| 2463 | switch (t.errorUnionSet().errorSetCardinality()) { | |
| 2464 | .zero => return dg.lowerType(payload_ty), | |
| 2465 | .one => { | |
| 2466 | if (payload_ty.isNoReturn()) { | |
| 2467 | return dg.context.voidType(); | |
| 2468 | } | |
| 2469 | }, | |
| 2470 | .many => {}, | |
| 2471 | } | |
| 2472 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 2473 | return try dg.lowerType(Type.anyerror); | |
| 2457 | 2474 | } |
| 2458 | const llvm_payload_type = try dg.llvmType(payload_type); | |
| 2475 | const llvm_error_type = try dg.lowerType(Type.anyerror); | |
| 2476 | const llvm_payload_type = try dg.lowerType(payload_ty); | |
| 2459 | 2477 | |
| 2460 | const payload_align = payload_type.abiAlignment(target); | |
| 2461 | const error_size = error_type.abiSize(target); | |
| 2462 | if (payload_align > error_size) { | |
| 2463 | const pad_type = dg.context.intType(8).arrayType(@intCast(u32, payload_align - error_size)); | |
| 2464 | const fields: [3]*const llvm.Type = .{ llvm_error_type, pad_type, llvm_payload_type }; | |
| 2478 | const payload_align = payload_ty.abiAlignment(target); | |
| 2479 | const error_align = Type.anyerror.abiAlignment(target); | |
| 2480 | if (error_align > payload_align) { | |
| 2481 | const fields: [2]*const llvm.Type = .{ llvm_error_type, llvm_payload_type }; | |
| 2465 | 2482 | return dg.context.structType(&fields, fields.len, .False); |
| 2466 | 2483 | } else { |
| 2467 | const fields: [2]*const llvm.Type = .{ llvm_error_type, llvm_payload_type }; | |
| 2484 | const fields: [2]*const llvm.Type = .{ llvm_payload_type, llvm_error_type }; | |
| 2468 | 2485 | return dg.context.structType(&fields, fields.len, .False); |
| 2469 | 2486 | } |
| 2470 | 2487 | }, |
| 2471 | .ErrorSet => { | |
| 2472 | return dg.context.intType(16); | |
| 2473 | }, | |
| 2488 | .ErrorSet => return dg.context.intType(16), | |
| 2474 | 2489 | .Struct => { |
| 2475 | 2490 | const gop = try dg.object.type_map.getOrPutContext(gpa, t, .{ .mod = dg.module }); |
| 2476 | 2491 | if (gop.found_existing) return gop.value_ptr.*; |
| ... | ... | @@ -2507,7 +2522,7 @@ pub const DeclGen = struct { |
| 2507 | 2522 | const llvm_array_ty = dg.context.intType(8).arrayType(@intCast(c_uint, padding_len)); |
| 2508 | 2523 | try llvm_field_types.append(gpa, llvm_array_ty); |
| 2509 | 2524 | } |
| 2510 | const field_llvm_ty = try dg.llvmType(field_ty); | |
| 2525 | const field_llvm_ty = try dg.lowerType(field_ty); | |
| 2511 | 2526 | try llvm_field_types.append(gpa, field_llvm_ty); |
| 2512 | 2527 | |
| 2513 | 2528 | offset += field_ty.abiSize(target); |
| ... | ... | @@ -2536,7 +2551,7 @@ pub const DeclGen = struct { |
| 2536 | 2551 | if (struct_obj.layout == .Packed) { |
| 2537 | 2552 | var buf: Type.Payload.Bits = undefined; |
| 2538 | 2553 | const int_ty = struct_obj.packedIntegerType(target, &buf); |
| 2539 | const int_llvm_ty = try dg.llvmType(int_ty); | |
| 2554 | const int_llvm_ty = try dg.lowerType(int_ty); | |
| 2540 | 2555 | gop.value_ptr.* = int_llvm_ty; |
| 2541 | 2556 | return int_llvm_ty; |
| 2542 | 2557 | } |
| ... | ... | @@ -2571,7 +2586,7 @@ pub const DeclGen = struct { |
| 2571 | 2586 | const llvm_array_ty = dg.context.intType(8).arrayType(@intCast(c_uint, padding_len)); |
| 2572 | 2587 | try llvm_field_types.append(gpa, llvm_array_ty); |
| 2573 | 2588 | } |
| 2574 | const field_llvm_ty = try dg.llvmType(field.ty); | |
| 2589 | const field_llvm_ty = try dg.lowerType(field.ty); | |
| 2575 | 2590 | try llvm_field_types.append(gpa, field_llvm_ty); |
| 2576 | 2591 | |
| 2577 | 2592 | offset += field.ty.abiSize(target); |
| ... | ... | @@ -2606,7 +2621,7 @@ pub const DeclGen = struct { |
| 2606 | 2621 | const union_obj = t.cast(Type.Payload.Union).?.data; |
| 2607 | 2622 | |
| 2608 | 2623 | if (layout.payload_size == 0) { |
| 2609 | const enum_tag_llvm_ty = try dg.llvmType(union_obj.tag_ty); | |
| 2624 | const enum_tag_llvm_ty = try dg.lowerType(union_obj.tag_ty); | |
| 2610 | 2625 | gop.value_ptr.* = enum_tag_llvm_ty; |
| 2611 | 2626 | return enum_tag_llvm_ty; |
| 2612 | 2627 | } |
| ... | ... | @@ -2618,7 +2633,7 @@ pub const DeclGen = struct { |
| 2618 | 2633 | gop.value_ptr.* = llvm_union_ty; // must be done before any recursive calls |
| 2619 | 2634 | |
| 2620 | 2635 | const aligned_field = union_obj.fields.values()[layout.most_aligned_field]; |
| 2621 | const llvm_aligned_field_ty = try dg.llvmType(aligned_field.ty); | |
| 2636 | const llvm_aligned_field_ty = try dg.lowerType(aligned_field.ty); | |
| 2622 | 2637 | |
| 2623 | 2638 | const llvm_payload_ty = t: { |
| 2624 | 2639 | if (layout.most_aligned_field_size == layout.payload_size) { |
| ... | ... | @@ -2637,7 +2652,7 @@ pub const DeclGen = struct { |
| 2637 | 2652 | llvm_union_ty.structSetBody(&llvm_fields, llvm_fields.len, .False); |
| 2638 | 2653 | return llvm_union_ty; |
| 2639 | 2654 | } |
| 2640 | const enum_tag_llvm_ty = try dg.llvmType(union_obj.tag_ty); | |
| 2655 | const enum_tag_llvm_ty = try dg.lowerType(union_obj.tag_ty); | |
| 2641 | 2656 | |
| 2642 | 2657 | // Put the tag before or after the payload depending on which one's |
| 2643 | 2658 | // alignment is greater. |
| ... | ... | @@ -2659,7 +2674,7 @@ pub const DeclGen = struct { |
| 2659 | 2674 | llvm_union_ty.structSetBody(&llvm_fields, llvm_fields_len, .False); |
| 2660 | 2675 | return llvm_union_ty; |
| 2661 | 2676 | }, |
| 2662 | .Fn => return llvmTypeFn(dg, t), | |
| 2677 | .Fn => return lowerTypeFn(dg, t), | |
| 2663 | 2678 | .ComptimeInt => unreachable, |
| 2664 | 2679 | .ComptimeFloat => unreachable, |
| 2665 | 2680 | .Type => unreachable, |
| ... | ... | @@ -2674,7 +2689,7 @@ pub const DeclGen = struct { |
| 2674 | 2689 | } |
| 2675 | 2690 | } |
| 2676 | 2691 | |
| 2677 | fn llvmTypeFn(dg: *DeclGen, fn_ty: Type) Allocator.Error!*const llvm.Type { | |
| 2692 | fn lowerTypeFn(dg: *DeclGen, fn_ty: Type) Allocator.Error!*const llvm.Type { | |
| 2678 | 2693 | const target = dg.module.getTarget(); |
| 2679 | 2694 | const fn_info = fn_ty.fnInfo(); |
| 2680 | 2695 | const llvm_ret_ty = try lowerFnRetTy(dg, fn_info); |
| ... | ... | @@ -2683,7 +2698,7 @@ pub const DeclGen = struct { |
| 2683 | 2698 | defer llvm_params.deinit(); |
| 2684 | 2699 | |
| 2685 | 2700 | if (firstParamSRet(fn_info, target)) { |
| 2686 | const llvm_sret_ty = try dg.llvmType(fn_info.return_type); | |
| 2701 | const llvm_sret_ty = try dg.lowerType(fn_info.return_type); | |
| 2687 | 2702 | try llvm_params.append(llvm_sret_ty.pointerType(0)); |
| 2688 | 2703 | } |
| 2689 | 2704 | |
| ... | ... | @@ -2695,7 +2710,7 @@ pub const DeclGen = struct { |
| 2695 | 2710 | .data = dg.object.getStackTraceType(), |
| 2696 | 2711 | }; |
| 2697 | 2712 | const ptr_ty = Type.initPayload(&ptr_ty_payload.base); |
| 2698 | try llvm_params.append(try dg.llvmType(ptr_ty)); | |
| 2713 | try llvm_params.append(try dg.lowerType(ptr_ty)); | |
| 2699 | 2714 | } |
| 2700 | 2715 | |
| 2701 | 2716 | var it = iterateParamTypes(dg, fn_info); |
| ... | ... | @@ -2703,11 +2718,11 @@ pub const DeclGen = struct { |
| 2703 | 2718 | .no_bits => continue, |
| 2704 | 2719 | .byval => { |
| 2705 | 2720 | const param_ty = fn_info.param_types[it.zig_index - 1]; |
| 2706 | try llvm_params.append(try dg.llvmType(param_ty)); | |
| 2721 | try llvm_params.append(try dg.lowerType(param_ty)); | |
| 2707 | 2722 | }, |
| 2708 | 2723 | .byref => { |
| 2709 | 2724 | const param_ty = fn_info.param_types[it.zig_index - 1]; |
| 2710 | const raw_llvm_ty = try dg.llvmType(param_ty); | |
| 2725 | const raw_llvm_ty = try dg.lowerType(param_ty); | |
| 2711 | 2726 | try llvm_params.append(raw_llvm_ty.pointerType(0)); |
| 2712 | 2727 | }, |
| 2713 | 2728 | .abi_sized_int => { |
| ... | ... | @@ -2749,7 +2764,7 @@ pub const DeclGen = struct { |
| 2749 | 2764 | // one field; in this case keep the type information |
| 2750 | 2765 | // to avoid the potentially costly ptrtoint/bitcast. |
| 2751 | 2766 | if (bits_used == 0 and field_abi_bits == int_bits) { |
| 2752 | const llvm_field_ty = try dg.llvmType(field.ty); | |
| 2767 | const llvm_field_ty = try dg.lowerType(field.ty); | |
| 2753 | 2768 | llvm_params.appendAssumeCapacity(llvm_field_ty); |
| 2754 | 2769 | field_i += 1; |
| 2755 | 2770 | if (field_i >= fields.len) { |
| ... | ... | @@ -2787,16 +2802,16 @@ pub const DeclGen = struct { |
| 2787 | 2802 | ); |
| 2788 | 2803 | } |
| 2789 | 2804 | |
| 2790 | fn genTypedValue(dg: *DeclGen, tv: TypedValue) Error!*const llvm.Value { | |
| 2805 | fn lowerValue(dg: *DeclGen, tv: TypedValue) Error!*const llvm.Value { | |
| 2791 | 2806 | if (tv.val.isUndef()) { |
| 2792 | const llvm_type = try dg.llvmType(tv.ty); | |
| 2807 | const llvm_type = try dg.lowerType(tv.ty); | |
| 2793 | 2808 | return llvm_type.getUndef(); |
| 2794 | 2809 | } |
| 2795 | 2810 | const target = dg.module.getTarget(); |
| 2796 | 2811 | |
| 2797 | 2812 | switch (tv.ty.zigTypeTag()) { |
| 2798 | 2813 | .Bool => { |
| 2799 | const llvm_type = try dg.llvmType(tv.ty); | |
| 2814 | const llvm_type = try dg.lowerType(tv.ty); | |
| 2800 | 2815 | return if (tv.val.toBool()) llvm_type.constAllOnes() else llvm_type.constNull(); |
| 2801 | 2816 | }, |
| 2802 | 2817 | // TODO this duplicates code with Pointer but they should share the handling |
| ... | ... | @@ -2857,7 +2872,7 @@ pub const DeclGen = struct { |
| 2857 | 2872 | return unsigned_val; |
| 2858 | 2873 | }, |
| 2859 | 2874 | .Float => { |
| 2860 | const llvm_ty = try dg.llvmType(tv.ty); | |
| 2875 | const llvm_ty = try dg.lowerType(tv.ty); | |
| 2861 | 2876 | switch (tv.ty.floatBits(target)) { |
| 2862 | 2877 | 16, 32, 64 => return llvm_ty.constReal(tv.val.toFloat(f64)), |
| 2863 | 2878 | 80 => { |
| ... | ... | @@ -2894,7 +2909,7 @@ pub const DeclGen = struct { |
| 2894 | 2909 | const decl = dg.module.declPtr(decl_index); |
| 2895 | 2910 | dg.module.markDeclAlive(decl); |
| 2896 | 2911 | const val = try dg.resolveGlobalDecl(decl_index); |
| 2897 | const llvm_var_type = try dg.llvmType(tv.ty); | |
| 2912 | const llvm_var_type = try dg.lowerType(tv.ty); | |
| 2898 | 2913 | const llvm_addrspace = dg.llvmAddressSpace(decl.@"addrspace"); |
| 2899 | 2914 | const llvm_type = llvm_var_type.pointerType(llvm_addrspace); |
| 2900 | 2915 | return val.constBitCast(llvm_type); |
| ... | ... | @@ -2903,11 +2918,11 @@ pub const DeclGen = struct { |
| 2903 | 2918 | const slice = tv.val.castTag(.slice).?.data; |
| 2904 | 2919 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 2905 | 2920 | const fields: [2]*const llvm.Value = .{ |
| 2906 | try dg.genTypedValue(.{ | |
| 2921 | try dg.lowerValue(.{ | |
| 2907 | 2922 | .ty = tv.ty.slicePtrFieldType(&buf), |
| 2908 | 2923 | .val = slice.ptr, |
| 2909 | 2924 | }), |
| 2910 | try dg.genTypedValue(.{ | |
| 2925 | try dg.lowerValue(.{ | |
| 2911 | 2926 | .ty = Type.usize, |
| 2912 | 2927 | .val = slice.len, |
| 2913 | 2928 | }), |
| ... | ... | @@ -2915,15 +2930,15 @@ pub const DeclGen = struct { |
| 2915 | 2930 | return dg.context.constStruct(&fields, fields.len, .False); |
| 2916 | 2931 | }, |
| 2917 | 2932 | .int_u64, .one, .int_big_positive => { |
| 2918 | const llvm_usize = try dg.llvmType(Type.usize); | |
| 2933 | const llvm_usize = try dg.lowerType(Type.usize); | |
| 2919 | 2934 | const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(target), .False); |
| 2920 | return llvm_int.constIntToPtr(try dg.llvmType(tv.ty)); | |
| 2935 | return llvm_int.constIntToPtr(try dg.lowerType(tv.ty)); | |
| 2921 | 2936 | }, |
| 2922 | 2937 | .field_ptr, .opt_payload_ptr, .eu_payload_ptr, .elem_ptr => { |
| 2923 | 2938 | return dg.lowerParentPtr(tv.val, tv.ty.childType()); |
| 2924 | 2939 | }, |
| 2925 | 2940 | .null_value, .zero => { |
| 2926 | const llvm_type = try dg.llvmType(tv.ty); | |
| 2941 | const llvm_type = try dg.lowerType(tv.ty); | |
| 2927 | 2942 | return llvm_type.constNull(); |
| 2928 | 2943 | }, |
| 2929 | 2944 | else => |tag| return dg.todo("implement const of pointer type '{}' ({})", .{ |
| ... | ... | @@ -2978,7 +2993,7 @@ pub const DeclGen = struct { |
| 2978 | 2993 | defer gpa.free(llvm_elems); |
| 2979 | 2994 | var need_unnamed = false; |
| 2980 | 2995 | for (elem_vals[0..len]) |elem_val, i| { |
| 2981 | llvm_elems[i] = try dg.genTypedValue(.{ .ty = elem_ty, .val = elem_val }); | |
| 2996 | llvm_elems[i] = try dg.lowerValue(.{ .ty = elem_ty, .val = elem_val }); | |
| 2982 | 2997 | need_unnamed = need_unnamed or dg.isUnnamedType(elem_ty, llvm_elems[i]); |
| 2983 | 2998 | } |
| 2984 | 2999 | if (need_unnamed) { |
| ... | ... | @@ -2988,7 +3003,7 @@ pub const DeclGen = struct { |
| 2988 | 3003 | .True, |
| 2989 | 3004 | ); |
| 2990 | 3005 | } else { |
| 2991 | const llvm_elem_ty = try dg.llvmType(elem_ty); | |
| 3006 | const llvm_elem_ty = try dg.lowerType(elem_ty); | |
| 2992 | 3007 | return llvm_elem_ty.constArray( |
| 2993 | 3008 | llvm_elems.ptr, |
| 2994 | 3009 | @intCast(c_uint, llvm_elems.len), |
| ... | ... | @@ -3008,13 +3023,13 @@ pub const DeclGen = struct { |
| 3008 | 3023 | var need_unnamed = false; |
| 3009 | 3024 | if (len != 0) { |
| 3010 | 3025 | for (llvm_elems[0..len]) |*elem| { |
| 3011 | elem.* = try dg.genTypedValue(.{ .ty = elem_ty, .val = val }); | |
| 3026 | elem.* = try dg.lowerValue(.{ .ty = elem_ty, .val = val }); | |
| 3012 | 3027 | } |
| 3013 | 3028 | need_unnamed = need_unnamed or dg.isUnnamedType(elem_ty, llvm_elems[0]); |
| 3014 | 3029 | } |
| 3015 | 3030 | |
| 3016 | 3031 | if (sentinel) |sent| { |
| 3017 | llvm_elems[len] = try dg.genTypedValue(.{ .ty = elem_ty, .val = sent }); | |
| 3032 | llvm_elems[len] = try dg.lowerValue(.{ .ty = elem_ty, .val = sent }); | |
| 3018 | 3033 | need_unnamed = need_unnamed or dg.isUnnamedType(elem_ty, llvm_elems[len]); |
| 3019 | 3034 | } |
| 3020 | 3035 | |
| ... | ... | @@ -3025,7 +3040,7 @@ pub const DeclGen = struct { |
| 3025 | 3040 | .True, |
| 3026 | 3041 | ); |
| 3027 | 3042 | } else { |
| 3028 | const llvm_elem_ty = try dg.llvmType(elem_ty); | |
| 3043 | const llvm_elem_ty = try dg.lowerType(elem_ty); | |
| 3029 | 3044 | return llvm_elem_ty.constArray( |
| 3030 | 3045 | llvm_elems.ptr, |
| 3031 | 3046 | @intCast(c_uint, llvm_elems.len), |
| ... | ... | @@ -3035,13 +3050,13 @@ pub const DeclGen = struct { |
| 3035 | 3050 | .empty_array_sentinel => { |
| 3036 | 3051 | const elem_ty = tv.ty.elemType(); |
| 3037 | 3052 | const sent_val = tv.ty.sentinel().?; |
| 3038 | const sentinel = try dg.genTypedValue(.{ .ty = elem_ty, .val = sent_val }); | |
| 3053 | const sentinel = try dg.lowerValue(.{ .ty = elem_ty, .val = sent_val }); | |
| 3039 | 3054 | const llvm_elems: [1]*const llvm.Value = .{sentinel}; |
| 3040 | 3055 | const need_unnamed = dg.isUnnamedType(elem_ty, llvm_elems[0]); |
| 3041 | 3056 | if (need_unnamed) { |
| 3042 | 3057 | return dg.context.constStruct(&llvm_elems, llvm_elems.len, .True); |
| 3043 | 3058 | } else { |
| 3044 | const llvm_elem_ty = try dg.llvmType(elem_ty); | |
| 3059 | const llvm_elem_ty = try dg.lowerType(elem_ty); | |
| 3045 | 3060 | return llvm_elem_ty.constArray(&llvm_elems, llvm_elems.len); |
| 3046 | 3061 | } |
| 3047 | 3062 | }, |
| ... | ... | @@ -3056,19 +3071,19 @@ pub const DeclGen = struct { |
| 3056 | 3071 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3057 | 3072 | return non_null_bit; |
| 3058 | 3073 | } |
| 3059 | if (tv.ty.isPtrLikeOptional()) { | |
| 3074 | if (tv.ty.optionalReprIsPayload()) { | |
| 3060 | 3075 | if (tv.val.castTag(.opt_payload)) |payload| { |
| 3061 | return dg.genTypedValue(.{ .ty = payload_ty, .val = payload.data }); | |
| 3076 | return dg.lowerValue(.{ .ty = payload_ty, .val = payload.data }); | |
| 3062 | 3077 | } else if (is_pl) { |
| 3063 | return dg.genTypedValue(.{ .ty = payload_ty, .val = tv.val }); | |
| 3078 | return dg.lowerValue(.{ .ty = payload_ty, .val = tv.val }); | |
| 3064 | 3079 | } else { |
| 3065 | const llvm_ty = try dg.llvmType(tv.ty); | |
| 3080 | const llvm_ty = try dg.lowerType(tv.ty); | |
| 3066 | 3081 | return llvm_ty.constNull(); |
| 3067 | 3082 | } |
| 3068 | 3083 | } |
| 3069 | 3084 | assert(payload_ty.zigTypeTag() != .Fn); |
| 3070 | 3085 | const fields: [2]*const llvm.Value = .{ |
| 3071 | try dg.genTypedValue(.{ | |
| 3086 | try dg.lowerValue(.{ | |
| 3072 | 3087 | .ty = payload_ty, |
| 3073 | 3088 | .val = if (tv.val.castTag(.opt_payload)) |pl| pl.data else Value.initTag(.undef), |
| 3074 | 3089 | }), |
| ... | ... | @@ -3087,7 +3102,7 @@ pub const DeclGen = struct { |
| 3087 | 3102 | return dg.resolveLlvmFunction(fn_decl_index); |
| 3088 | 3103 | }, |
| 3089 | 3104 | .ErrorSet => { |
| 3090 | const llvm_ty = try dg.llvmType(tv.ty); | |
| 3105 | const llvm_ty = try dg.lowerType(Type.anyerror); | |
| 3091 | 3106 | switch (tv.val.tag()) { |
| 3092 | 3107 | .@"error" => { |
| 3093 | 3108 | const err_name = tv.val.castTag(.@"error").?.data.name; |
| ... | ... | @@ -3101,40 +3116,39 @@ pub const DeclGen = struct { |
| 3101 | 3116 | } |
| 3102 | 3117 | }, |
| 3103 | 3118 | .ErrorUnion => { |
| 3104 | const error_type = tv.ty.errorUnionSet(); | |
| 3105 | 3119 | const payload_type = tv.ty.errorUnionPayload(); |
| 3120 | if (tv.ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 3121 | const payload_val = tv.val.castTag(.eu_payload).?.data; | |
| 3122 | return dg.lowerValue(.{ .ty = payload_type, .val = payload_val }); | |
| 3123 | } | |
| 3106 | 3124 | const is_pl = tv.val.errorUnionIsPayload(); |
| 3107 | 3125 | |
| 3108 | 3126 | if (!payload_type.hasRuntimeBitsIgnoreComptime()) { |
| 3109 | 3127 | // We use the error type directly as the type. |
| 3110 | 3128 | const err_val = if (!is_pl) tv.val else Value.initTag(.zero); |
| 3111 | return dg.genTypedValue(.{ .ty = error_type, .val = err_val }); | |
| 3129 | return dg.lowerValue(.{ .ty = Type.anyerror, .val = err_val }); | |
| 3112 | 3130 | } |
| 3113 | var len: u8 = 2; | |
| 3114 | var fields: [3]*const llvm.Value = .{ | |
| 3115 | try dg.genTypedValue(.{ | |
| 3116 | .ty = error_type, | |
| 3117 | .val = if (is_pl) Value.initTag(.zero) else tv.val, | |
| 3118 | }), | |
| 3119 | try dg.genTypedValue(.{ | |
| 3120 | .ty = payload_type, | |
| 3121 | .val = if (tv.val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef), | |
| 3122 | }), | |
| 3123 | undefined, | |
| 3124 | }; | |
| 3125 | 3131 | |
| 3126 | 3132 | const payload_align = payload_type.abiAlignment(target); |
| 3127 | const error_size = error_type.abiSize(target); | |
| 3128 | if (payload_align > error_size) { | |
| 3129 | fields[2] = fields[1]; | |
| 3130 | const pad_type = dg.context.intType(8).arrayType(@intCast(u32, payload_align - error_size)); | |
| 3131 | fields[1] = pad_type.getUndef(); | |
| 3132 | len += 1; | |
| 3133 | const error_align = Type.anyerror.abiAlignment(target); | |
| 3134 | const llvm_error_value = try dg.lowerValue(.{ | |
| 3135 | .ty = Type.anyerror, | |
| 3136 | .val = if (is_pl) Value.initTag(.zero) else tv.val, | |
| 3137 | }); | |
| 3138 | const llvm_payload_value = try dg.lowerValue(.{ | |
| 3139 | .ty = payload_type, | |
| 3140 | .val = if (tv.val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef), | |
| 3141 | }); | |
| 3142 | if (error_align > payload_align) { | |
| 3143 | const fields: [2]*const llvm.Value = .{ llvm_error_value, llvm_payload_value }; | |
| 3144 | return dg.context.constStruct(&fields, fields.len, .False); | |
| 3145 | } else { | |
| 3146 | const fields: [2]*const llvm.Value = .{ llvm_payload_value, llvm_error_value }; | |
| 3147 | return dg.context.constStruct(&fields, fields.len, .False); | |
| 3133 | 3148 | } |
| 3134 | return dg.context.constStruct(&fields, len, .False); | |
| 3135 | 3149 | }, |
| 3136 | 3150 | .Struct => { |
| 3137 | const llvm_struct_ty = try dg.llvmType(tv.ty); | |
| 3151 | const llvm_struct_ty = try dg.lowerType(tv.ty); | |
| 3138 | 3152 | const field_vals = tv.val.castTag(.aggregate).?.data; |
| 3139 | 3153 | const gpa = dg.gpa; |
| 3140 | 3154 | |
| ... | ... | @@ -3167,7 +3181,7 @@ pub const DeclGen = struct { |
| 3167 | 3181 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); |
| 3168 | 3182 | } |
| 3169 | 3183 | |
| 3170 | const field_llvm_val = try dg.genTypedValue(.{ | |
| 3184 | const field_llvm_val = try dg.lowerValue(.{ | |
| 3171 | 3185 | .ty = field_ty, |
| 3172 | 3186 | .val = field_vals[i], |
| 3173 | 3187 | }); |
| ... | ... | @@ -3215,7 +3229,7 @@ pub const DeclGen = struct { |
| 3215 | 3229 | const field = fields[i]; |
| 3216 | 3230 | if (!field.ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 3217 | 3231 | |
| 3218 | const non_int_val = try dg.genTypedValue(.{ | |
| 3232 | const non_int_val = try dg.lowerValue(.{ | |
| 3219 | 3233 | .ty = field.ty, |
| 3220 | 3234 | .val = field_val, |
| 3221 | 3235 | }); |
| ... | ... | @@ -3259,7 +3273,7 @@ pub const DeclGen = struct { |
| 3259 | 3273 | llvm_fields.appendAssumeCapacity(llvm_array_ty.getUndef()); |
| 3260 | 3274 | } |
| 3261 | 3275 | |
| 3262 | const field_llvm_val = try dg.genTypedValue(.{ | |
| 3276 | const field_llvm_val = try dg.lowerValue(.{ | |
| 3263 | 3277 | .ty = field.ty, |
| 3264 | 3278 | .val = field_vals[i], |
| 3265 | 3279 | }); |
| ... | ... | @@ -3294,13 +3308,13 @@ pub const DeclGen = struct { |
| 3294 | 3308 | } |
| 3295 | 3309 | }, |
| 3296 | 3310 | .Union => { |
| 3297 | const llvm_union_ty = try dg.llvmType(tv.ty); | |
| 3311 | const llvm_union_ty = try dg.lowerType(tv.ty); | |
| 3298 | 3312 | const tag_and_val = tv.val.castTag(.@"union").?.data; |
| 3299 | 3313 | |
| 3300 | 3314 | const layout = tv.ty.unionGetLayout(target); |
| 3301 | 3315 | |
| 3302 | 3316 | if (layout.payload_size == 0) { |
| 3303 | return genTypedValue(dg, .{ | |
| 3317 | return lowerValue(dg, .{ | |
| 3304 | 3318 | .ty = tv.ty.unionTagType().?, |
| 3305 | 3319 | .val = tag_and_val.tag, |
| 3306 | 3320 | }); |
| ... | ... | @@ -3314,7 +3328,7 @@ pub const DeclGen = struct { |
| 3314 | 3328 | const padding_len = @intCast(c_uint, layout.payload_size); |
| 3315 | 3329 | break :p dg.context.intType(8).arrayType(padding_len).getUndef(); |
| 3316 | 3330 | } |
| 3317 | const field = try genTypedValue(dg, .{ .ty = field_ty, .val = tag_and_val.val }); | |
| 3331 | const field = try lowerValue(dg, .{ .ty = field_ty, .val = tag_and_val.val }); | |
| 3318 | 3332 | const field_size = field_ty.abiSize(target); |
| 3319 | 3333 | if (field_size == layout.payload_size) { |
| 3320 | 3334 | break :p field; |
| ... | ... | @@ -3340,7 +3354,7 @@ pub const DeclGen = struct { |
| 3340 | 3354 | return llvm_union_ty.constNamedStruct(&fields, fields.len); |
| 3341 | 3355 | } |
| 3342 | 3356 | } |
| 3343 | const llvm_tag_value = try genTypedValue(dg, .{ | |
| 3357 | const llvm_tag_value = try lowerValue(dg, .{ | |
| 3344 | 3358 | .ty = tv.ty.unionTagType().?, |
| 3345 | 3359 | .val = tag_and_val.tag, |
| 3346 | 3360 | }); |
| ... | ... | @@ -3377,7 +3391,7 @@ pub const DeclGen = struct { |
| 3377 | 3391 | .data = bytes[i], |
| 3378 | 3392 | }; |
| 3379 | 3393 | |
| 3380 | elem.* = try dg.genTypedValue(.{ | |
| 3394 | elem.* = try dg.lowerValue(.{ | |
| 3381 | 3395 | .ty = elem_ty, |
| 3382 | 3396 | .val = Value.initPayload(&byte_payload.base), |
| 3383 | 3397 | }); |
| ... | ... | @@ -3397,7 +3411,7 @@ pub const DeclGen = struct { |
| 3397 | 3411 | const llvm_elems = try dg.gpa.alloc(*const llvm.Value, vector_len); |
| 3398 | 3412 | defer dg.gpa.free(llvm_elems); |
| 3399 | 3413 | for (llvm_elems) |*elem, i| { |
| 3400 | elem.* = try dg.genTypedValue(.{ .ty = elem_ty, .val = elem_vals[i] }); | |
| 3414 | elem.* = try dg.lowerValue(.{ .ty = elem_ty, .val = elem_vals[i] }); | |
| 3401 | 3415 | } |
| 3402 | 3416 | return llvm.constVector( |
| 3403 | 3417 | llvm_elems.ptr, |
| ... | ... | @@ -3412,7 +3426,7 @@ pub const DeclGen = struct { |
| 3412 | 3426 | const llvm_elems = try dg.gpa.alloc(*const llvm.Value, len); |
| 3413 | 3427 | defer dg.gpa.free(llvm_elems); |
| 3414 | 3428 | for (llvm_elems) |*elem| { |
| 3415 | elem.* = try dg.genTypedValue(.{ .ty = elem_ty, .val = val }); | |
| 3429 | elem.* = try dg.lowerValue(.{ .ty = elem_ty, .val = val }); | |
| 3416 | 3430 | } |
| 3417 | 3431 | return llvm.constVector( |
| 3418 | 3432 | llvm_elems.ptr, |
| ... | ... | @@ -3462,7 +3476,7 @@ pub const DeclGen = struct { |
| 3462 | 3476 | if (ptr_child_ty.eql(decl.ty, dg.module)) { |
| 3463 | 3477 | return llvm_ptr; |
| 3464 | 3478 | } else { |
| 3465 | return llvm_ptr.constBitCast((try dg.llvmType(ptr_child_ty)).pointerType(0)); | |
| 3479 | return llvm_ptr.constBitCast((try dg.lowerType(ptr_child_ty)).pointerType(0)); | |
| 3466 | 3480 | } |
| 3467 | 3481 | } |
| 3468 | 3482 | |
| ... | ... | @@ -3484,15 +3498,15 @@ pub const DeclGen = struct { |
| 3484 | 3498 | }, |
| 3485 | 3499 | .int_i64 => { |
| 3486 | 3500 | const int = ptr_val.castTag(.int_i64).?.data; |
| 3487 | const llvm_usize = try dg.llvmType(Type.usize); | |
| 3501 | const llvm_usize = try dg.lowerType(Type.usize); | |
| 3488 | 3502 | const llvm_int = llvm_usize.constInt(@bitCast(u64, int), .False); |
| 3489 | return llvm_int.constIntToPtr((try dg.llvmType(ptr_child_ty)).pointerType(0)); | |
| 3503 | return llvm_int.constIntToPtr((try dg.lowerType(ptr_child_ty)).pointerType(0)); | |
| 3490 | 3504 | }, |
| 3491 | 3505 | .int_u64 => { |
| 3492 | 3506 | const int = ptr_val.castTag(.int_u64).?.data; |
| 3493 | const llvm_usize = try dg.llvmType(Type.usize); | |
| 3507 | const llvm_usize = try dg.lowerType(Type.usize); | |
| 3494 | 3508 | const llvm_int = llvm_usize.constInt(int, .False); |
| 3495 | return llvm_int.constIntToPtr((try dg.llvmType(ptr_child_ty)).pointerType(0)); | |
| 3509 | return llvm_int.constIntToPtr((try dg.lowerType(ptr_child_ty)).pointerType(0)); | |
| 3496 | 3510 | }, |
| 3497 | 3511 | .field_ptr => blk: { |
| 3498 | 3512 | const field_ptr = ptr_val.castTag(.field_ptr).?.data; |
| ... | ... | @@ -3541,7 +3555,7 @@ pub const DeclGen = struct { |
| 3541 | 3555 | const parent_llvm_ptr = try dg.lowerParentPtr(elem_ptr.array_ptr, elem_ptr.elem_ty); |
| 3542 | 3556 | bitcast_needed = !elem_ptr.elem_ty.eql(ptr_child_ty, dg.module); |
| 3543 | 3557 | |
| 3544 | const llvm_usize = try dg.llvmType(Type.usize); | |
| 3558 | const llvm_usize = try dg.lowerType(Type.usize); | |
| 3545 | 3559 | const indices: [1]*const llvm.Value = .{ |
| 3546 | 3560 | llvm_usize.constInt(elem_ptr.index, .False), |
| 3547 | 3561 | }; |
| ... | ... | @@ -3555,7 +3569,9 @@ pub const DeclGen = struct { |
| 3555 | 3569 | const payload_ty = opt_payload_ptr.container_ty.optionalChild(&buf); |
| 3556 | 3570 | bitcast_needed = !payload_ty.eql(ptr_child_ty, dg.module); |
| 3557 | 3571 | |
| 3558 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.isPtrLikeOptional()) { | |
| 3572 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or | |
| 3573 | payload_ty.optionalReprIsPayload()) | |
| 3574 | { | |
| 3559 | 3575 | // In this case, we represent pointer to optional the same as pointer |
| 3560 | 3576 | // to the payload. |
| 3561 | 3577 | break :blk parent_llvm_ptr; |
| ... | ... | @@ -3592,7 +3608,7 @@ pub const DeclGen = struct { |
| 3592 | 3608 | else => unreachable, |
| 3593 | 3609 | }; |
| 3594 | 3610 | if (bitcast_needed) { |
| 3595 | return llvm_ptr.constBitCast((try dg.llvmType(ptr_child_ty)).pointerType(0)); | |
| 3611 | return llvm_ptr.constBitCast((try dg.lowerType(ptr_child_ty)).pointerType(0)); | |
| 3596 | 3612 | } else { |
| 3597 | 3613 | return llvm_ptr; |
| 3598 | 3614 | } |
| ... | ... | @@ -3611,11 +3627,11 @@ pub const DeclGen = struct { |
| 3611 | 3627 | .data = tv.val.sliceLen(self.module), |
| 3612 | 3628 | }; |
| 3613 | 3629 | const fields: [2]*const llvm.Value = .{ |
| 3614 | try self.genTypedValue(.{ | |
| 3630 | try self.lowerValue(.{ | |
| 3615 | 3631 | .ty = ptr_ty, |
| 3616 | 3632 | .val = tv.val, |
| 3617 | 3633 | }), |
| 3618 | try self.genTypedValue(.{ | |
| 3634 | try self.lowerValue(.{ | |
| 3619 | 3635 | .ty = Type.usize, |
| 3620 | 3636 | .val = Value.initPayload(&slice_len.base), |
| 3621 | 3637 | }), |
| ... | ... | @@ -3647,7 +3663,7 @@ pub const DeclGen = struct { |
| 3647 | 3663 | else |
| 3648 | 3664 | try self.resolveGlobalDecl(decl_index); |
| 3649 | 3665 | |
| 3650 | const llvm_type = try self.llvmType(tv.ty); | |
| 3666 | const llvm_type = try self.lowerType(tv.ty); | |
| 3651 | 3667 | if (tv.ty.zigTypeTag() == .Int) { |
| 3652 | 3668 | return llvm_val.constPtrToInt(llvm_type); |
| 3653 | 3669 | } else { |
| ... | ... | @@ -3662,8 +3678,8 @@ pub const DeclGen = struct { |
| 3662 | 3678 | // The value cannot be undefined, because we use the `nonnull` annotation |
| 3663 | 3679 | // for non-optional pointers. We also need to respect the alignment, even though |
| 3664 | 3680 | // the address will never be dereferenced. |
| 3665 | const llvm_usize = try dg.llvmType(Type.usize); | |
| 3666 | const llvm_ptr_ty = try dg.llvmType(ptr_ty); | |
| 3681 | const llvm_usize = try dg.lowerType(Type.usize); | |
| 3682 | const llvm_ptr_ty = try dg.lowerType(ptr_ty); | |
| 3667 | 3683 | if (alignment != 0) { |
| 3668 | 3684 | return llvm_usize.constInt(alignment, .False).constIntToPtr(llvm_ptr_ty); |
| 3669 | 3685 | } |
| ... | ... | @@ -3842,7 +3858,7 @@ pub const FuncGen = struct { |
| 3842 | 3858 | |
| 3843 | 3859 | const val = self.air.value(inst).?; |
| 3844 | 3860 | const ty = self.air.typeOf(inst); |
| 3845 | const llvm_val = try self.dg.genTypedValue(.{ .ty = ty, .val = val }); | |
| 3861 | const llvm_val = try self.dg.lowerValue(.{ .ty = ty, .val = val }); | |
| 3846 | 3862 | if (!isByRef(ty)) { |
| 3847 | 3863 | gop.value_ptr.* = llvm_val; |
| 3848 | 3864 | return llvm_val; |
| ... | ... | @@ -3860,7 +3876,7 @@ pub const FuncGen = struct { |
| 3860 | 3876 | // Because of LLVM limitations for lowering certain types such as unions, |
| 3861 | 3877 | // the type of global constants might not match the type it is supposed to |
| 3862 | 3878 | // be, and so we must bitcast the pointer at the usage sites. |
| 3863 | const wanted_llvm_ty = try self.dg.llvmType(ty); | |
| 3879 | const wanted_llvm_ty = try self.dg.lowerType(ty); | |
| 3864 | 3880 | const wanted_llvm_ptr_ty = wanted_llvm_ty.pointerType(0); |
| 3865 | 3881 | const casted_ptr = global.constBitCast(wanted_llvm_ptr_ty); |
| 3866 | 3882 | gop.value_ptr.* = casted_ptr; |
| ... | ... | @@ -4084,7 +4100,7 @@ pub const FuncGen = struct { |
| 4084 | 4100 | defer llvm_args.deinit(); |
| 4085 | 4101 | |
| 4086 | 4102 | const ret_ptr = if (!sret) null else blk: { |
| 4087 | const llvm_ret_ty = try self.dg.llvmType(return_type); | |
| 4103 | const llvm_ret_ty = try self.dg.lowerType(return_type); | |
| 4088 | 4104 | const ret_ptr = self.buildAlloca(llvm_ret_ty); |
| 4089 | 4105 | ret_ptr.setAlignment(return_type.abiAlignment(target)); |
| 4090 | 4106 | try llvm_args.append(ret_ptr); |
| ... | ... | @@ -4116,7 +4132,7 @@ pub const FuncGen = struct { |
| 4116 | 4132 | // which is always lowered to an LLVM type of `*i8`. |
| 4117 | 4133 | // 2. The argument is a global which does act as a pointer, however |
| 4118 | 4134 | // a bitcast is needed in order for the LLVM types to match. |
| 4119 | const llvm_param_ty = try self.dg.llvmType(param_ty); | |
| 4135 | const llvm_param_ty = try self.dg.lowerType(param_ty); | |
| 4120 | 4136 | const casted_ptr = self.builder.buildBitCast(llvm_arg, llvm_param_ty, ""); |
| 4121 | 4137 | try llvm_args.append(casted_ptr); |
| 4122 | 4138 | } else { |
| ... | ... | @@ -4163,7 +4179,7 @@ pub const FuncGen = struct { |
| 4163 | 4179 | ); |
| 4164 | 4180 | const int_ptr = self.buildAlloca(int_llvm_ty); |
| 4165 | 4181 | int_ptr.setAlignment(alignment); |
| 4166 | const param_llvm_ty = try self.dg.llvmType(param_ty); | |
| 4182 | const param_llvm_ty = try self.dg.lowerType(param_ty); | |
| 4167 | 4183 | const casted_ptr = self.builder.buildBitCast(int_ptr, param_llvm_ty.pointerType(0), ""); |
| 4168 | 4184 | const store_inst = self.builder.buildStore(llvm_arg, casted_ptr); |
| 4169 | 4185 | store_inst.setAlignment(alignment); |
| ... | ... | @@ -4274,7 +4290,7 @@ pub const FuncGen = struct { |
| 4274 | 4290 | return null; |
| 4275 | 4291 | } |
| 4276 | 4292 | |
| 4277 | const llvm_ret_ty = try self.dg.llvmType(return_type); | |
| 4293 | const llvm_ret_ty = try self.dg.lowerType(return_type); | |
| 4278 | 4294 | |
| 4279 | 4295 | if (ret_ptr) |rp| { |
| 4280 | 4296 | call.setCallSret(llvm_ret_ty); |
| ... | ... | @@ -4338,11 +4354,19 @@ pub const FuncGen = struct { |
| 4338 | 4354 | _ = self.builder.buildRetVoid(); |
| 4339 | 4355 | return null; |
| 4340 | 4356 | } |
| 4357 | const fn_info = self.dg.decl.ty.fnInfo(); | |
| 4341 | 4358 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4342 | _ = self.builder.buildRetVoid(); | |
| 4359 | if (fn_info.return_type.isError()) { | |
| 4360 | // Functions with an empty error set are emitted with an error code | |
| 4361 | // return type and return zero so they can be function pointers coerced | |
| 4362 | // to functions that return anyerror. | |
| 4363 | const err_int = try self.dg.lowerType(Type.anyerror); | |
| 4364 | _ = self.builder.buildRet(err_int.constInt(0, .False)); | |
| 4365 | } else { | |
| 4366 | _ = self.builder.buildRetVoid(); | |
| 4367 | } | |
| 4343 | 4368 | return null; |
| 4344 | 4369 | } |
| 4345 | const fn_info = self.dg.decl.ty.fnInfo(); | |
| 4346 | 4370 | const abi_ret_ty = try lowerFnRetTy(self.dg, fn_info); |
| 4347 | 4371 | const operand = try self.resolveInst(un_op); |
| 4348 | 4372 | const llvm_ret_ty = operand.typeOf(); |
| ... | ... | @@ -4369,15 +4393,27 @@ pub const FuncGen = struct { |
| 4369 | 4393 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 4370 | 4394 | const ptr_ty = self.air.typeOf(un_op); |
| 4371 | 4395 | const ret_ty = ptr_ty.childType(); |
| 4372 | if (!ret_ty.hasRuntimeBitsIgnoreComptime() or self.ret_ptr != null) { | |
| 4396 | const fn_info = self.dg.decl.ty.fnInfo(); | |
| 4397 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { | |
| 4398 | if (fn_info.return_type.isError()) { | |
| 4399 | // Functions with an empty error set are emitted with an error code | |
| 4400 | // return type and return zero so they can be function pointers coerced | |
| 4401 | // to functions that return anyerror. | |
| 4402 | const err_int = try self.dg.lowerType(Type.anyerror); | |
| 4403 | _ = self.builder.buildRet(err_int.constInt(0, .False)); | |
| 4404 | } else { | |
| 4405 | _ = self.builder.buildRetVoid(); | |
| 4406 | } | |
| 4407 | return null; | |
| 4408 | } | |
| 4409 | if (self.ret_ptr != null) { | |
| 4373 | 4410 | _ = self.builder.buildRetVoid(); |
| 4374 | 4411 | return null; |
| 4375 | 4412 | } |
| 4376 | 4413 | const ptr = try self.resolveInst(un_op); |
| 4377 | 4414 | const target = self.dg.module.getTarget(); |
| 4378 | const fn_info = self.dg.decl.ty.fnInfo(); | |
| 4379 | 4415 | const abi_ret_ty = try lowerFnRetTy(self.dg, fn_info); |
| 4380 | const llvm_ret_ty = try self.dg.llvmType(ret_ty); | |
| 4416 | const llvm_ret_ty = try self.dg.lowerType(ret_ty); | |
| 4381 | 4417 | const casted_ptr = if (abi_ret_ty == llvm_ret_ty) ptr else p: { |
| 4382 | 4418 | const ptr_abi_ty = abi_ret_ty.pointerType(0); |
| 4383 | 4419 | break :p self.builder.buildBitCast(ptr, ptr_abi_ty, ""); |
| ... | ... | @@ -4439,7 +4475,9 @@ pub const FuncGen = struct { |
| 4439 | 4475 | .Int, .Bool, .Pointer, .ErrorSet => scalar_ty, |
| 4440 | 4476 | .Optional => blk: { |
| 4441 | 4477 | const payload_ty = operand_ty.optionalChild(&opt_buffer); |
| 4442 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or operand_ty.isPtrLikeOptional()) { | |
| 4478 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or | |
| 4479 | operand_ty.optionalReprIsPayload()) | |
| 4480 | { | |
| 4443 | 4481 | break :blk operand_ty; |
| 4444 | 4482 | } |
| 4445 | 4483 | // We need to emit instructions to check for equality/inequality |
| ... | ... | @@ -4556,7 +4594,7 @@ pub const FuncGen = struct { |
| 4556 | 4594 | const is_body = inst_ty.zigTypeTag() == .Fn; |
| 4557 | 4595 | if (!is_body and !inst_ty.hasRuntimeBitsIgnoreComptime()) return null; |
| 4558 | 4596 | |
| 4559 | const raw_llvm_ty = try self.dg.llvmType(inst_ty); | |
| 4597 | const raw_llvm_ty = try self.dg.lowerType(inst_ty); | |
| 4560 | 4598 | |
| 4561 | 4599 | const llvm_ty = ty: { |
| 4562 | 4600 | // If the zig tag type is a function, this represents an actual function body; not |
| ... | ... | @@ -4696,9 +4734,9 @@ pub const FuncGen = struct { |
| 4696 | 4734 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 4697 | 4735 | const operand_ty = self.air.typeOf(ty_op.operand); |
| 4698 | 4736 | const array_ty = operand_ty.childType(); |
| 4699 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 4737 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 4700 | 4738 | const len = llvm_usize.constInt(array_ty.arrayLen(), .False); |
| 4701 | const slice_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 4739 | const slice_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 4702 | 4740 | if (!array_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4703 | 4741 | return self.builder.buildInsertValue(slice_llvm_ty.getUndef(), len, 1, ""); |
| 4704 | 4742 | } |
| ... | ... | @@ -4723,7 +4761,7 @@ pub const FuncGen = struct { |
| 4723 | 4761 | |
| 4724 | 4762 | const dest_ty = self.air.typeOfIndex(inst); |
| 4725 | 4763 | const dest_scalar_ty = dest_ty.scalarType(); |
| 4726 | const dest_llvm_ty = try self.dg.llvmType(dest_ty); | |
| 4764 | const dest_llvm_ty = try self.dg.lowerType(dest_ty); | |
| 4727 | 4765 | const target = self.dg.module.getTarget(); |
| 4728 | 4766 | |
| 4729 | 4767 | if (intrinsicsAllowed(dest_scalar_ty, target)) { |
| ... | ... | @@ -4774,7 +4812,7 @@ pub const FuncGen = struct { |
| 4774 | 4812 | |
| 4775 | 4813 | const dest_ty = self.air.typeOfIndex(inst); |
| 4776 | 4814 | const dest_scalar_ty = dest_ty.scalarType(); |
| 4777 | const dest_llvm_ty = try self.dg.llvmType(dest_ty); | |
| 4815 | const dest_llvm_ty = try self.dg.lowerType(dest_ty); | |
| 4778 | 4816 | |
| 4779 | 4817 | if (intrinsicsAllowed(operand_scalar_ty, target)) { |
| 4780 | 4818 | // TODO set fast math flag |
| ... | ... | @@ -4801,7 +4839,7 @@ pub const FuncGen = struct { |
| 4801 | 4839 | compiler_rt_dest_abbrev, |
| 4802 | 4840 | }) catch unreachable; |
| 4803 | 4841 | |
| 4804 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 4842 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 4805 | 4843 | const param_types = [1]*const llvm.Type{operand_llvm_ty}; |
| 4806 | 4844 | const libc_fn = self.getLibcFunction(fn_name, &param_types, libc_ret_ty); |
| 4807 | 4845 | const params = [1]*const llvm.Value{operand}; |
| ... | ... | @@ -4962,7 +5000,7 @@ pub const FuncGen = struct { |
| 4962 | 5000 | const containing_int = struct_llvm_val; |
| 4963 | 5001 | const shift_amt = containing_int.typeOf().constInt(bit_offset, .False); |
| 4964 | 5002 | const shifted_value = self.builder.buildLShr(containing_int, shift_amt, ""); |
| 4965 | const elem_llvm_ty = try self.dg.llvmType(field_ty); | |
| 5003 | const elem_llvm_ty = try self.dg.lowerType(field_ty); | |
| 4966 | 5004 | if (field_ty.zigTypeTag() == .Float) { |
| 4967 | 5005 | const elem_bits = @intCast(c_uint, field_ty.bitSize(target)); |
| 4968 | 5006 | const same_size_int = self.context.intType(elem_bits); |
| ... | ... | @@ -4994,7 +5032,7 @@ pub const FuncGen = struct { |
| 4994 | 5032 | return self.load(field_ptr, field_ptr_ty); |
| 4995 | 5033 | }, |
| 4996 | 5034 | .Union => { |
| 4997 | const llvm_field_ty = try self.dg.llvmType(field_ty); | |
| 5035 | const llvm_field_ty = try self.dg.lowerType(field_ty); | |
| 4998 | 5036 | const layout = struct_ty.unionGetLayout(target); |
| 4999 | 5037 | const payload_index = @boolToInt(layout.tag_align >= layout.payload_align); |
| 5000 | 5038 | const union_field_ptr = self.builder.buildStructGEP(struct_llvm_val, payload_index, ""); |
| ... | ... | @@ -5021,7 +5059,7 @@ pub const FuncGen = struct { |
| 5021 | 5059 | const struct_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 5022 | 5060 | const field_offset = struct_ty.structFieldOffset(extra.field_index, target); |
| 5023 | 5061 | |
| 5024 | const res_ty = try self.dg.llvmType(self.air.getRefType(ty_pl.ty)); | |
| 5062 | const res_ty = try self.dg.lowerType(self.air.getRefType(ty_pl.ty)); | |
| 5025 | 5063 | if (field_offset == 0) { |
| 5026 | 5064 | return self.builder.buildBitCast(field_ptr, res_ty, ""); |
| 5027 | 5065 | } |
| ... | ... | @@ -5351,7 +5389,7 @@ pub const FuncGen = struct { |
| 5351 | 5389 | } |
| 5352 | 5390 | |
| 5353 | 5391 | const ret_ty = self.air.typeOfIndex(inst); |
| 5354 | const ret_llvm_ty = try self.dg.llvmType(ret_ty); | |
| 5392 | const ret_llvm_ty = try self.dg.lowerType(ret_ty); | |
| 5355 | 5393 | const llvm_fn_ty = llvm.functionType( |
| 5356 | 5394 | ret_llvm_ty, |
| 5357 | 5395 | llvm_param_types.ptr, |
| ... | ... | @@ -5392,8 +5430,8 @@ pub const FuncGen = struct { |
| 5392 | 5430 | const operand = try self.resolveInst(un_op); |
| 5393 | 5431 | const operand_ty = self.air.typeOf(un_op); |
| 5394 | 5432 | const optional_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; |
| 5395 | if (optional_ty.isPtrLikeOptional()) { | |
| 5396 | const optional_llvm_ty = try self.dg.llvmType(optional_ty); | |
| 5433 | if (optional_ty.optionalReprIsPayload()) { | |
| 5434 | const optional_llvm_ty = try self.dg.lowerType(optional_ty); | |
| 5397 | 5435 | const loaded = if (operand_is_ptr) self.builder.buildLoad(operand, "") else operand; |
| 5398 | 5436 | return self.builder.buildICmp(pred, loaded, optional_llvm_ty.constNull(), ""); |
| 5399 | 5437 | } |
| ... | ... | @@ -5430,21 +5468,33 @@ pub const FuncGen = struct { |
| 5430 | 5468 | const operand = try self.resolveInst(un_op); |
| 5431 | 5469 | const err_union_ty = self.air.typeOf(un_op); |
| 5432 | 5470 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 5433 | const err_set_ty = try self.dg.llvmType(Type.initTag(.anyerror)); | |
| 5471 | const err_set_ty = try self.dg.lowerType(Type.initTag(.anyerror)); | |
| 5434 | 5472 | const zero = err_set_ty.constNull(); |
| 5435 | 5473 | |
| 5474 | if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 5475 | const llvm_i1 = self.context.intType(1); | |
| 5476 | switch (op) { | |
| 5477 | .EQ => return llvm_i1.constInt(1, .False), // 0 == 0 | |
| 5478 | .NE => return llvm_i1.constInt(0, .False), // 0 != 0 | |
| 5479 | else => unreachable, | |
| 5480 | } | |
| 5481 | } | |
| 5482 | ||
| 5436 | 5483 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5437 | 5484 | const loaded = if (operand_is_ptr) self.builder.buildLoad(operand, "") else operand; |
| 5438 | 5485 | return self.builder.buildICmp(op, loaded, zero, ""); |
| 5439 | 5486 | } |
| 5440 | 5487 | |
| 5488 | const target = self.dg.module.getTarget(); | |
| 5489 | const err_field_index = errUnionErrorOffset(payload_ty, target); | |
| 5490 | ||
| 5441 | 5491 | if (operand_is_ptr or isByRef(err_union_ty)) { |
| 5442 | const err_field_ptr = self.builder.buildStructGEP(operand, 0, ""); | |
| 5492 | const err_field_ptr = self.builder.buildStructGEP(operand, err_field_index, ""); | |
| 5443 | 5493 | const loaded = self.builder.buildLoad(err_field_ptr, ""); |
| 5444 | 5494 | return self.builder.buildICmp(op, loaded, zero, ""); |
| 5445 | 5495 | } |
| 5446 | 5496 | |
| 5447 | const loaded = self.builder.buildExtractValue(operand, 0, ""); | |
| 5497 | const loaded = self.builder.buildExtractValue(operand, err_field_index, ""); | |
| 5448 | 5498 | return self.builder.buildICmp(op, loaded, zero, ""); |
| 5449 | 5499 | } |
| 5450 | 5500 | |
| ... | ... | @@ -5462,10 +5512,10 @@ pub const FuncGen = struct { |
| 5462 | 5512 | // a pointer to a zero-bit value. |
| 5463 | 5513 | |
| 5464 | 5514 | // TODO once we update to LLVM 14 this bitcast won't be necessary. |
| 5465 | const res_ptr_ty = try self.dg.llvmType(result_ty); | |
| 5515 | const res_ptr_ty = try self.dg.lowerType(result_ty); | |
| 5466 | 5516 | return self.builder.buildBitCast(operand, res_ptr_ty, ""); |
| 5467 | 5517 | } |
| 5468 | if (optional_ty.isPtrLikeOptional()) { | |
| 5518 | if (optional_ty.optionalReprIsPayload()) { | |
| 5469 | 5519 | // The payload and the optional are the same value. |
| 5470 | 5520 | return operand; |
| 5471 | 5521 | } |
| ... | ... | @@ -5490,10 +5540,10 @@ pub const FuncGen = struct { |
| 5490 | 5540 | _ = self.builder.buildStore(non_null_bit, operand); |
| 5491 | 5541 | |
| 5492 | 5542 | // TODO once we update to LLVM 14 this bitcast won't be necessary. |
| 5493 | const res_ptr_ty = try self.dg.llvmType(result_ty); | |
| 5543 | const res_ptr_ty = try self.dg.lowerType(result_ty); | |
| 5494 | 5544 | return self.builder.buildBitCast(operand, res_ptr_ty, ""); |
| 5495 | 5545 | } |
| 5496 | if (optional_ty.isPtrLikeOptional()) { | |
| 5546 | if (optional_ty.optionalReprIsPayload()) { | |
| 5497 | 5547 | // The payload and the optional are the same value. |
| 5498 | 5548 | // Setting to non-null will be done when the payload is set. |
| 5499 | 5549 | return operand; |
| ... | ... | @@ -5527,7 +5577,7 @@ pub const FuncGen = struct { |
| 5527 | 5577 | const payload_ty = self.air.typeOfIndex(inst); |
| 5528 | 5578 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return null; |
| 5529 | 5579 | |
| 5530 | if (optional_ty.isPtrLikeOptional()) { | |
| 5580 | if (optional_ty.optionalReprIsPayload()) { | |
| 5531 | 5581 | // Payload value is the same as the optional value. |
| 5532 | 5582 | return operand; |
| 5533 | 5583 | } |
| ... | ... | @@ -5544,17 +5594,23 @@ pub const FuncGen = struct { |
| 5544 | 5594 | |
| 5545 | 5595 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5546 | 5596 | const operand = try self.resolveInst(ty_op.operand); |
| 5547 | const result_ty = self.air.getRefType(ty_op.ty); | |
| 5597 | const operand_ty = self.air.typeOf(ty_op.operand); | |
| 5598 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | |
| 5599 | // If the error set has no fields, then the payload and the error | |
| 5600 | // union are the same value. | |
| 5601 | if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 5602 | return operand; | |
| 5603 | } | |
| 5604 | const result_ty = self.air.typeOfIndex(inst); | |
| 5548 | 5605 | const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty; |
| 5549 | ||
| 5550 | 5606 | const target = self.dg.module.getTarget(); |
| 5551 | const offset: u8 = if (payload_ty.abiAlignment(target) > Type.anyerror.abiSize(target)) 2 else 1; | |
| 5607 | const offset = errUnionPayloadOffset(payload_ty, target); | |
| 5552 | 5608 | |
| 5553 | 5609 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5554 | 5610 | if (!operand_is_ptr) return null; |
| 5555 | 5611 | |
| 5556 | 5612 | // TODO once we update to LLVM 14 this bitcast won't be necessary. |
| 5557 | const res_ptr_ty = try self.dg.llvmType(result_ty); | |
| 5613 | const res_ptr_ty = try self.dg.lowerType(result_ty); | |
| 5558 | 5614 | return self.builder.buildBitCast(operand, res_ptr_ty, ""); |
| 5559 | 5615 | } |
| 5560 | 5616 | if (operand_is_ptr or isByRef(payload_ty)) { |
| ... | ... | @@ -5574,54 +5630,69 @@ pub const FuncGen = struct { |
| 5574 | 5630 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5575 | 5631 | const operand = try self.resolveInst(ty_op.operand); |
| 5576 | 5632 | const operand_ty = self.air.typeOf(ty_op.operand); |
| 5577 | const err_set_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | |
| 5633 | const err_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; | |
| 5634 | if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 5635 | const err_llvm_ty = try self.dg.lowerType(Type.anyerror); | |
| 5636 | if (operand_is_ptr) { | |
| 5637 | return self.builder.buildBitCast(operand, err_llvm_ty.pointerType(0), ""); | |
| 5638 | } else { | |
| 5639 | return err_llvm_ty.constInt(0, .False); | |
| 5640 | } | |
| 5641 | } | |
| 5578 | 5642 | |
| 5579 | const payload_ty = err_set_ty.errorUnionPayload(); | |
| 5643 | const payload_ty = err_union_ty.errorUnionPayload(); | |
| 5580 | 5644 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5581 | 5645 | if (!operand_is_ptr) return operand; |
| 5582 | 5646 | return self.builder.buildLoad(operand, ""); |
| 5583 | 5647 | } |
| 5584 | 5648 | |
| 5585 | if (operand_is_ptr or isByRef(err_set_ty)) { | |
| 5586 | const err_field_ptr = self.builder.buildStructGEP(operand, 0, ""); | |
| 5649 | const target = self.dg.module.getTarget(); | |
| 5650 | const offset = errUnionErrorOffset(payload_ty, target); | |
| 5651 | ||
| 5652 | if (operand_is_ptr or isByRef(err_union_ty)) { | |
| 5653 | const err_field_ptr = self.builder.buildStructGEP(operand, offset, ""); | |
| 5587 | 5654 | return self.builder.buildLoad(err_field_ptr, ""); |
| 5588 | 5655 | } |
| 5589 | 5656 | |
| 5590 | return self.builder.buildExtractValue(operand, 0, ""); | |
| 5657 | return self.builder.buildExtractValue(operand, offset, ""); | |
| 5591 | 5658 | } |
| 5592 | 5659 | |
| 5593 | 5660 | fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value { |
| 5594 | 5661 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5595 | 5662 | const operand = try self.resolveInst(ty_op.operand); |
| 5596 | const error_set_ty = self.air.typeOf(ty_op.operand).childType(); | |
| 5663 | const error_union_ty = self.air.typeOf(ty_op.operand).childType(); | |
| 5597 | 5664 | |
| 5598 | const error_ty = error_set_ty.errorUnionSet(); | |
| 5599 | const payload_ty = error_set_ty.errorUnionPayload(); | |
| 5600 | const non_error_val = try self.dg.genTypedValue(.{ .ty = error_ty, .val = Value.zero }); | |
| 5665 | if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 5666 | // TODO: write undefined bytes through the pointer here | |
| 5667 | return operand; | |
| 5668 | } | |
| 5669 | const payload_ty = error_union_ty.errorUnionPayload(); | |
| 5670 | const non_error_val = try self.dg.lowerValue(.{ .ty = Type.anyerror, .val = Value.zero }); | |
| 5601 | 5671 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5602 | // We have a pointer to a i1. We need to set it to 1 and then return the same pointer. | |
| 5603 | 5672 | _ = self.builder.buildStore(non_error_val, operand); |
| 5604 | 5673 | return operand; |
| 5605 | 5674 | } |
| 5606 | 5675 | const index_type = self.context.intType(32); |
| 5676 | const target = self.dg.module.getTarget(); | |
| 5607 | 5677 | { |
| 5678 | const error_offset = errUnionErrorOffset(payload_ty, target); | |
| 5608 | 5679 | // First set the non-error value. |
| 5609 | 5680 | const indices: [2]*const llvm.Value = .{ |
| 5610 | 5681 | index_type.constNull(), // dereference the pointer |
| 5611 | index_type.constNull(), // first field is the payload | |
| 5682 | index_type.constInt(error_offset, .False), | |
| 5612 | 5683 | }; |
| 5613 | 5684 | const non_null_ptr = self.builder.buildInBoundsGEP(operand, &indices, indices.len, ""); |
| 5614 | _ = self.builder.buildStore(non_error_val, non_null_ptr); | |
| 5685 | const store_inst = self.builder.buildStore(non_error_val, non_null_ptr); | |
| 5686 | store_inst.setAlignment(Type.anyerror.abiAlignment(target)); | |
| 5615 | 5687 | } |
| 5616 | 5688 | // Then return the payload pointer (only if it is used). |
| 5617 | 5689 | if (self.liveness.isUnused(inst)) |
| 5618 | 5690 | return null; |
| 5619 | 5691 | |
| 5620 | const target = self.dg.module.getTarget(); | |
| 5621 | const payload_offset: u8 = if (payload_ty.abiAlignment(target) > Type.anyerror.abiSize(target)) 2 else 1; | |
| 5692 | const payload_offset = errUnionPayloadOffset(payload_ty, target); | |
| 5622 | 5693 | const indices: [2]*const llvm.Value = .{ |
| 5623 | 5694 | index_type.constNull(), // dereference the pointer |
| 5624 | index_type.constInt(payload_offset, .False), // second field is the payload | |
| 5695 | index_type.constInt(payload_offset, .False), | |
| 5625 | 5696 | }; |
| 5626 | 5697 | return self.builder.buildInBoundsGEP(operand, &indices, indices.len, ""); |
| 5627 | 5698 | } |
| ... | ... | @@ -5646,8 +5717,10 @@ pub const FuncGen = struct { |
| 5646 | 5717 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return non_null_bit; |
| 5647 | 5718 | const operand = try self.resolveInst(ty_op.operand); |
| 5648 | 5719 | const optional_ty = self.air.typeOfIndex(inst); |
| 5649 | if (optional_ty.isPtrLikeOptional()) return operand; | |
| 5650 | const llvm_optional_ty = try self.dg.llvmType(optional_ty); | |
| 5720 | if (optional_ty.optionalReprIsPayload()) { | |
| 5721 | return operand; | |
| 5722 | } | |
| 5723 | const llvm_optional_ty = try self.dg.lowerType(optional_ty); | |
| 5651 | 5724 | if (isByRef(optional_ty)) { |
| 5652 | 5725 | const optional_ptr = self.buildAlloca(llvm_optional_ty); |
| 5653 | 5726 | const payload_ptr = self.builder.buildStructGEP(optional_ptr, 0, ""); |
| ... | ... | @@ -5669,21 +5742,26 @@ pub const FuncGen = struct { |
| 5669 | 5742 | if (self.liveness.isUnused(inst)) return null; |
| 5670 | 5743 | |
| 5671 | 5744 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 5672 | const payload_ty = self.air.typeOf(ty_op.operand); | |
| 5745 | const inst_ty = self.air.typeOfIndex(inst); | |
| 5673 | 5746 | const operand = try self.resolveInst(ty_op.operand); |
| 5747 | if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) { | |
| 5748 | return operand; | |
| 5749 | } | |
| 5750 | const payload_ty = self.air.typeOf(ty_op.operand); | |
| 5674 | 5751 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5675 | 5752 | return operand; |
| 5676 | 5753 | } |
| 5677 | const inst_ty = self.air.typeOfIndex(inst); | |
| 5678 | const ok_err_code = self.context.intType(16).constNull(); | |
| 5679 | const err_un_llvm_ty = try self.dg.llvmType(inst_ty); | |
| 5754 | const ok_err_code = (try self.dg.lowerType(Type.anyerror)).constNull(); | |
| 5755 | const err_un_llvm_ty = try self.dg.lowerType(inst_ty); | |
| 5680 | 5756 | |
| 5681 | 5757 | const target = self.dg.module.getTarget(); |
| 5682 | const payload_offset: u8 = if (payload_ty.abiAlignment(target) > Type.anyerror.abiSize(target)) 2 else 1; | |
| 5758 | const payload_offset = errUnionPayloadOffset(payload_ty, target); | |
| 5759 | const error_offset = errUnionErrorOffset(payload_ty, target); | |
| 5683 | 5760 | if (isByRef(inst_ty)) { |
| 5684 | 5761 | const result_ptr = self.buildAlloca(err_un_llvm_ty); |
| 5685 | const err_ptr = self.builder.buildStructGEP(result_ptr, 0, ""); | |
| 5686 | _ = self.builder.buildStore(ok_err_code, err_ptr); | |
| 5762 | const err_ptr = self.builder.buildStructGEP(result_ptr, error_offset, ""); | |
| 5763 | const store_inst = self.builder.buildStore(ok_err_code, err_ptr); | |
| 5764 | store_inst.setAlignment(Type.anyerror.abiAlignment(target)); | |
| 5687 | 5765 | const payload_ptr = self.builder.buildStructGEP(result_ptr, payload_offset, ""); |
| 5688 | 5766 | var ptr_ty_payload: Type.Payload.ElemType = .{ |
| 5689 | 5767 | .base = .{ .tag = .single_mut_pointer }, |
| ... | ... | @@ -5694,7 +5772,7 @@ pub const FuncGen = struct { |
| 5694 | 5772 | return result_ptr; |
| 5695 | 5773 | } |
| 5696 | 5774 | |
| 5697 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), ok_err_code, 0, ""); | |
| 5775 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), ok_err_code, error_offset, ""); | |
| 5698 | 5776 | return self.builder.buildInsertValue(partial, operand, payload_offset, ""); |
| 5699 | 5777 | } |
| 5700 | 5778 | |
| ... | ... | @@ -5708,14 +5786,16 @@ pub const FuncGen = struct { |
| 5708 | 5786 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 5709 | 5787 | return operand; |
| 5710 | 5788 | } |
| 5711 | const err_un_llvm_ty = try self.dg.llvmType(err_un_ty); | |
| 5789 | const err_un_llvm_ty = try self.dg.lowerType(err_un_ty); | |
| 5712 | 5790 | |
| 5713 | 5791 | const target = self.dg.module.getTarget(); |
| 5714 | const payload_offset: u8 = if (payload_ty.abiAlignment(target) > Type.anyerror.abiSize(target)) 2 else 1; | |
| 5792 | const payload_offset = errUnionPayloadOffset(payload_ty, target); | |
| 5793 | const error_offset = errUnionErrorOffset(payload_ty, target); | |
| 5715 | 5794 | if (isByRef(err_un_ty)) { |
| 5716 | 5795 | const result_ptr = self.buildAlloca(err_un_llvm_ty); |
| 5717 | const err_ptr = self.builder.buildStructGEP(result_ptr, 0, ""); | |
| 5718 | _ = self.builder.buildStore(operand, err_ptr); | |
| 5796 | const err_ptr = self.builder.buildStructGEP(result_ptr, error_offset, ""); | |
| 5797 | const store_inst = self.builder.buildStore(operand, err_ptr); | |
| 5798 | store_inst.setAlignment(Type.anyerror.abiAlignment(target)); | |
| 5719 | 5799 | const payload_ptr = self.builder.buildStructGEP(result_ptr, payload_offset, ""); |
| 5720 | 5800 | var ptr_ty_payload: Type.Payload.ElemType = .{ |
| 5721 | 5801 | .base = .{ .tag = .single_mut_pointer }, |
| ... | ... | @@ -5728,7 +5808,7 @@ pub const FuncGen = struct { |
| 5728 | 5808 | return result_ptr; |
| 5729 | 5809 | } |
| 5730 | 5810 | |
| 5731 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), operand, 0, ""); | |
| 5811 | const partial = self.builder.buildInsertValue(err_un_llvm_ty.getUndef(), operand, error_offset, ""); | |
| 5732 | 5812 | // TODO set payload bytes to undef |
| 5733 | 5813 | return partial; |
| 5734 | 5814 | } |
| ... | ... | @@ -5791,7 +5871,7 @@ pub const FuncGen = struct { |
| 5791 | 5871 | const ptr = try self.resolveInst(bin_op.lhs); |
| 5792 | 5872 | const len = try self.resolveInst(bin_op.rhs); |
| 5793 | 5873 | const inst_ty = self.air.typeOfIndex(inst); |
| 5794 | const llvm_slice_ty = try self.dg.llvmType(inst_ty); | |
| 5874 | const llvm_slice_ty = try self.dg.lowerType(inst_ty); | |
| 5795 | 5875 | |
| 5796 | 5876 | // In case of slicing a global, the result type looks something like `{ i8*, i64 }` |
| 5797 | 5877 | // but `ptr` is pointing to the global directly. If it's an array, we would want to |
| ... | ... | @@ -5799,7 +5879,7 @@ pub const FuncGen = struct { |
| 5799 | 5879 | // This prevents an assertion failure. |
| 5800 | 5880 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 5801 | 5881 | const ptr_ty = inst_ty.slicePtrFieldType(&buf); |
| 5802 | const ptr_llvm_ty = try self.dg.llvmType(ptr_ty); | |
| 5882 | const ptr_llvm_ty = try self.dg.lowerType(ptr_ty); | |
| 5803 | 5883 | const casted_ptr = self.builder.buildBitCast(ptr, ptr_llvm_ty, ""); |
| 5804 | 5884 | const partial = self.builder.buildInsertValue(llvm_slice_ty.getUndef(), casted_ptr, 0, ""); |
| 5805 | 5885 | return self.builder.buildInsertValue(partial, len, 1, ""); |
| ... | ... | @@ -5965,7 +6045,7 @@ pub const FuncGen = struct { |
| 5965 | 6045 | // const d = @divTrunc(a, b); |
| 5966 | 6046 | // const r = @rem(a, b); |
| 5967 | 6047 | // return if (r == 0) d else d - ((a < 0) ^ (b < 0)); |
| 5968 | const result_llvm_ty = try self.dg.llvmType(inst_ty); | |
| 6048 | const result_llvm_ty = try self.dg.lowerType(inst_ty); | |
| 5969 | 6049 | const zero = result_llvm_ty.constNull(); |
| 5970 | 6050 | const div_trunc = self.builder.buildSDiv(lhs, rhs, ""); |
| 5971 | 6051 | const rem = self.builder.buildSRem(lhs, rhs, ""); |
| ... | ... | @@ -6015,7 +6095,7 @@ pub const FuncGen = struct { |
| 6015 | 6095 | const lhs = try self.resolveInst(bin_op.lhs); |
| 6016 | 6096 | const rhs = try self.resolveInst(bin_op.rhs); |
| 6017 | 6097 | const inst_ty = self.air.typeOfIndex(inst); |
| 6018 | const inst_llvm_ty = try self.dg.llvmType(inst_ty); | |
| 6098 | const inst_llvm_ty = try self.dg.lowerType(inst_ty); | |
| 6019 | 6099 | const scalar_ty = inst_ty.scalarType(); |
| 6020 | 6100 | |
| 6021 | 6101 | if (scalar_ty.isRuntimeFloat()) { |
| ... | ... | @@ -6099,8 +6179,8 @@ pub const FuncGen = struct { |
| 6099 | 6179 | |
| 6100 | 6180 | const intrinsic_name = if (scalar_ty.isSignedInt()) signed_intrinsic else unsigned_intrinsic; |
| 6101 | 6181 | |
| 6102 | const llvm_lhs_ty = try self.dg.llvmType(lhs_ty); | |
| 6103 | const llvm_dest_ty = try self.dg.llvmType(dest_ty); | |
| 6182 | const llvm_lhs_ty = try self.dg.lowerType(lhs_ty); | |
| 6183 | const llvm_dest_ty = try self.dg.lowerType(dest_ty); | |
| 6104 | 6184 | |
| 6105 | 6185 | const tg = self.dg.module.getTarget(); |
| 6106 | 6186 | |
| ... | ... | @@ -6208,7 +6288,7 @@ pub const FuncGen = struct { |
| 6208 | 6288 | ) !*const llvm.Value { |
| 6209 | 6289 | const target = self.dg.module.getTarget(); |
| 6210 | 6290 | const scalar_ty = ty.scalarType(); |
| 6211 | const scalar_llvm_ty = try self.dg.llvmType(scalar_ty); | |
| 6291 | const scalar_llvm_ty = try self.dg.lowerType(scalar_ty); | |
| 6212 | 6292 | |
| 6213 | 6293 | if (intrinsicsAllowed(scalar_ty, target)) { |
| 6214 | 6294 | const llvm_predicate: llvm.RealPredicate = switch (pred) { |
| ... | ... | @@ -6308,8 +6388,8 @@ pub const FuncGen = struct { |
| 6308 | 6388 | ) !*const llvm.Value { |
| 6309 | 6389 | const target = self.dg.module.getTarget(); |
| 6310 | 6390 | const scalar_ty = ty.scalarType(); |
| 6311 | const llvm_ty = try self.dg.llvmType(ty); | |
| 6312 | const scalar_llvm_ty = try self.dg.llvmType(scalar_ty); | |
| 6391 | const llvm_ty = try self.dg.lowerType(ty); | |
| 6392 | const scalar_llvm_ty = try self.dg.lowerType(scalar_ty); | |
| 6313 | 6393 | |
| 6314 | 6394 | const intrinsics_allowed = op != .tan and intrinsicsAllowed(scalar_ty, target); |
| 6315 | 6395 | var fn_name_buf: [64]u8 = undefined; |
| ... | ... | @@ -6403,12 +6483,12 @@ pub const FuncGen = struct { |
| 6403 | 6483 | const rhs_scalar_ty = rhs_ty.scalarType(); |
| 6404 | 6484 | |
| 6405 | 6485 | const dest_ty = self.air.typeOfIndex(inst); |
| 6406 | const llvm_dest_ty = try self.dg.llvmType(dest_ty); | |
| 6486 | const llvm_dest_ty = try self.dg.lowerType(dest_ty); | |
| 6407 | 6487 | |
| 6408 | 6488 | const tg = self.dg.module.getTarget(); |
| 6409 | 6489 | |
| 6410 | 6490 | const casted_rhs = if (rhs_scalar_ty.bitSize(tg) < lhs_scalar_ty.bitSize(tg)) |
| 6411 | self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_ty), "") | |
| 6491 | self.builder.buildZExt(rhs, try self.dg.lowerType(lhs_ty), "") | |
| 6412 | 6492 | else |
| 6413 | 6493 | rhs; |
| 6414 | 6494 | |
| ... | ... | @@ -6468,7 +6548,7 @@ pub const FuncGen = struct { |
| 6468 | 6548 | const tg = self.dg.module.getTarget(); |
| 6469 | 6549 | |
| 6470 | 6550 | const casted_rhs = if (rhs_scalar_ty.bitSize(tg) < lhs_scalar_ty.bitSize(tg)) |
| 6471 | self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_ty), "") | |
| 6551 | self.builder.buildZExt(rhs, try self.dg.lowerType(lhs_ty), "") | |
| 6472 | 6552 | else |
| 6473 | 6553 | rhs; |
| 6474 | 6554 | if (lhs_scalar_ty.isSignedInt()) return self.builder.buildNSWShl(lhs, casted_rhs, ""); |
| ... | ... | @@ -6491,7 +6571,7 @@ pub const FuncGen = struct { |
| 6491 | 6571 | const tg = self.dg.module.getTarget(); |
| 6492 | 6572 | |
| 6493 | 6573 | const casted_rhs = if (rhs_scalar_ty.bitSize(tg) < lhs_scalar_ty.bitSize(tg)) |
| 6494 | self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_type), "") | |
| 6574 | self.builder.buildZExt(rhs, try self.dg.lowerType(lhs_type), "") | |
| 6495 | 6575 | else |
| 6496 | 6576 | rhs; |
| 6497 | 6577 | return self.builder.buildShl(lhs, casted_rhs, ""); |
| ... | ... | @@ -6513,7 +6593,7 @@ pub const FuncGen = struct { |
| 6513 | 6593 | const tg = self.dg.module.getTarget(); |
| 6514 | 6594 | |
| 6515 | 6595 | const casted_rhs = if (rhs_scalar_ty.bitSize(tg) < lhs_scalar_ty.bitSize(tg)) |
| 6516 | self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_ty), "") | |
| 6596 | self.builder.buildZExt(rhs, try self.dg.lowerType(lhs_ty), "") | |
| 6517 | 6597 | else |
| 6518 | 6598 | rhs; |
| 6519 | 6599 | if (lhs_scalar_ty.isSignedInt()) return self.builder.buildSShlSat(lhs, casted_rhs, ""); |
| ... | ... | @@ -6536,7 +6616,7 @@ pub const FuncGen = struct { |
| 6536 | 6616 | const tg = self.dg.module.getTarget(); |
| 6537 | 6617 | |
| 6538 | 6618 | const casted_rhs = if (rhs_scalar_ty.bitSize(tg) < lhs_scalar_ty.bitSize(tg)) |
| 6539 | self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_ty), "") | |
| 6619 | self.builder.buildZExt(rhs, try self.dg.lowerType(lhs_ty), "") | |
| 6540 | 6620 | else |
| 6541 | 6621 | rhs; |
| 6542 | 6622 | const is_signed_int = lhs_scalar_ty.isSignedInt(); |
| ... | ... | @@ -6564,7 +6644,7 @@ pub const FuncGen = struct { |
| 6564 | 6644 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6565 | 6645 | const dest_ty = self.air.typeOfIndex(inst); |
| 6566 | 6646 | const dest_info = dest_ty.intInfo(target); |
| 6567 | const dest_llvm_ty = try self.dg.llvmType(dest_ty); | |
| 6647 | const dest_llvm_ty = try self.dg.lowerType(dest_ty); | |
| 6568 | 6648 | const operand = try self.resolveInst(ty_op.operand); |
| 6569 | 6649 | const operand_ty = self.air.typeOf(ty_op.operand); |
| 6570 | 6650 | const operand_info = operand_ty.intInfo(target); |
| ... | ... | @@ -6586,7 +6666,7 @@ pub const FuncGen = struct { |
| 6586 | 6666 | |
| 6587 | 6667 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6588 | 6668 | const operand = try self.resolveInst(ty_op.operand); |
| 6589 | const dest_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 6669 | const dest_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 6590 | 6670 | return self.builder.buildTrunc(operand, dest_llvm_ty, ""); |
| 6591 | 6671 | } |
| 6592 | 6672 | |
| ... | ... | @@ -6604,7 +6684,7 @@ pub const FuncGen = struct { |
| 6604 | 6684 | if (!backendSupportsF80(target) and (src_bits == 80 or dest_bits == 80)) { |
| 6605 | 6685 | return softF80TruncOrExt(self, operand, src_bits, dest_bits); |
| 6606 | 6686 | } |
| 6607 | const dest_llvm_ty = try self.dg.llvmType(dest_ty); | |
| 6687 | const dest_llvm_ty = try self.dg.lowerType(dest_ty); | |
| 6608 | 6688 | return self.builder.buildFPTrunc(operand, dest_llvm_ty, ""); |
| 6609 | 6689 | } |
| 6610 | 6690 | |
| ... | ... | @@ -6622,7 +6702,7 @@ pub const FuncGen = struct { |
| 6622 | 6702 | if (!backendSupportsF80(target) and (src_bits == 80 or dest_bits == 80)) { |
| 6623 | 6703 | return softF80TruncOrExt(self, operand, src_bits, dest_bits); |
| 6624 | 6704 | } |
| 6625 | const dest_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 6705 | const dest_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 6626 | 6706 | return self.builder.buildFPExt(operand, dest_llvm_ty, ""); |
| 6627 | 6707 | } |
| 6628 | 6708 | |
| ... | ... | @@ -6632,7 +6712,7 @@ pub const FuncGen = struct { |
| 6632 | 6712 | |
| 6633 | 6713 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 6634 | 6714 | const operand = try self.resolveInst(un_op); |
| 6635 | const dest_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 6715 | const dest_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 6636 | 6716 | return self.builder.buildPtrToInt(operand, dest_llvm_ty, ""); |
| 6637 | 6717 | } |
| 6638 | 6718 | |
| ... | ... | @@ -6640,12 +6720,12 @@ pub const FuncGen = struct { |
| 6640 | 6720 | if (self.liveness.isUnused(inst)) return null; |
| 6641 | 6721 | |
| 6642 | 6722 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 6643 | const operand = try self.resolveInst(ty_op.operand); | |
| 6644 | 6723 | const operand_ty = self.air.typeOf(ty_op.operand); |
| 6645 | 6724 | const inst_ty = self.air.typeOfIndex(inst); |
| 6725 | const operand = try self.resolveInst(ty_op.operand); | |
| 6646 | 6726 | const operand_is_ref = isByRef(operand_ty); |
| 6647 | 6727 | const result_is_ref = isByRef(inst_ty); |
| 6648 | const llvm_dest_ty = try self.dg.llvmType(inst_ty); | |
| 6728 | const llvm_dest_ty = try self.dg.lowerType(inst_ty); | |
| 6649 | 6729 | const target = self.dg.module.getTarget(); |
| 6650 | 6730 | |
| 6651 | 6731 | if (operand_is_ref and result_is_ref) { |
| ... | ... | @@ -6665,14 +6745,14 @@ pub const FuncGen = struct { |
| 6665 | 6745 | const array_ptr = self.buildAlloca(llvm_dest_ty); |
| 6666 | 6746 | const bitcast_ok = elem_ty.bitSize(target) == elem_ty.abiSize(target) * 8; |
| 6667 | 6747 | if (bitcast_ok) { |
| 6668 | const llvm_vector_ty = try self.dg.llvmType(operand_ty); | |
| 6748 | const llvm_vector_ty = try self.dg.lowerType(operand_ty); | |
| 6669 | 6749 | const casted_ptr = self.builder.buildBitCast(array_ptr, llvm_vector_ty.pointerType(0), ""); |
| 6670 | 6750 | const llvm_store = self.builder.buildStore(operand, casted_ptr); |
| 6671 | 6751 | llvm_store.setAlignment(inst_ty.abiAlignment(target)); |
| 6672 | 6752 | } else { |
| 6673 | 6753 | // If the ABI size of the element type is not evenly divisible by size in bits; |
| 6674 | 6754 | // a simple bitcast will not work, and we fall back to extractelement. |
| 6675 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 6755 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 6676 | 6756 | const llvm_u32 = self.context.intType(32); |
| 6677 | 6757 | const zero = llvm_usize.constNull(); |
| 6678 | 6758 | const vector_len = operand_ty.arrayLen(); |
| ... | ... | @@ -6689,7 +6769,7 @@ pub const FuncGen = struct { |
| 6689 | 6769 | return array_ptr; |
| 6690 | 6770 | } else if (operand_ty.zigTypeTag() == .Array and inst_ty.zigTypeTag() == .Vector) { |
| 6691 | 6771 | const elem_ty = operand_ty.childType(); |
| 6692 | const llvm_vector_ty = try self.dg.llvmType(inst_ty); | |
| 6772 | const llvm_vector_ty = try self.dg.lowerType(inst_ty); | |
| 6693 | 6773 | if (!operand_is_ref) { |
| 6694 | 6774 | return self.dg.todo("implement bitcast non-ref array to vector", .{}); |
| 6695 | 6775 | } |
| ... | ... | @@ -6706,7 +6786,7 @@ pub const FuncGen = struct { |
| 6706 | 6786 | } else { |
| 6707 | 6787 | // If the ABI size of the element type is not evenly divisible by size in bits; |
| 6708 | 6788 | // a simple bitcast will not work, and we fall back to extractelement. |
| 6709 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 6789 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 6710 | 6790 | const llvm_u32 = self.context.intType(32); |
| 6711 | 6791 | const zero = llvm_usize.constNull(); |
| 6712 | 6792 | const vector_len = operand_ty.arrayLen(); |
| ... | ... | @@ -6738,7 +6818,7 @@ pub const FuncGen = struct { |
| 6738 | 6818 | const alignment = @maximum(operand_ty.abiAlignment(target), inst_ty.abiAlignment(target)); |
| 6739 | 6819 | const result_ptr = self.buildAlloca(llvm_dest_ty); |
| 6740 | 6820 | result_ptr.setAlignment(alignment); |
| 6741 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 6821 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 6742 | 6822 | const casted_ptr = self.builder.buildBitCast(result_ptr, operand_llvm_ty.pointerType(0), ""); |
| 6743 | 6823 | const store_inst = self.builder.buildStore(operand, casted_ptr); |
| 6744 | 6824 | store_inst.setAlignment(alignment); |
| ... | ... | @@ -6752,7 +6832,7 @@ pub const FuncGen = struct { |
| 6752 | 6832 | const alignment = @maximum(operand_ty.abiAlignment(target), inst_ty.abiAlignment(target)); |
| 6753 | 6833 | const result_ptr = self.buildAlloca(llvm_dest_ty); |
| 6754 | 6834 | result_ptr.setAlignment(alignment); |
| 6755 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 6835 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 6756 | 6836 | const casted_ptr = self.builder.buildBitCast(result_ptr, operand_llvm_ty.pointerType(0), ""); |
| 6757 | 6837 | const store_inst = self.builder.buildStore(operand, casted_ptr); |
| 6758 | 6838 | store_inst.setAlignment(alignment); |
| ... | ... | @@ -6826,7 +6906,7 @@ pub const FuncGen = struct { |
| 6826 | 6906 | const pointee_type = ptr_ty.childType(); |
| 6827 | 6907 | if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime()) return self.dg.lowerPtrToVoid(ptr_ty); |
| 6828 | 6908 | |
| 6829 | const pointee_llvm_ty = try self.dg.llvmType(pointee_type); | |
| 6909 | const pointee_llvm_ty = try self.dg.lowerType(pointee_type); | |
| 6830 | 6910 | const alloca_inst = self.buildAlloca(pointee_llvm_ty); |
| 6831 | 6911 | const target = self.dg.module.getTarget(); |
| 6832 | 6912 | const alignment = ptr_ty.ptrAlignment(target); |
| ... | ... | @@ -6840,7 +6920,7 @@ pub const FuncGen = struct { |
| 6840 | 6920 | const ret_ty = ptr_ty.childType(); |
| 6841 | 6921 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime()) return self.dg.lowerPtrToVoid(ptr_ty); |
| 6842 | 6922 | if (self.ret_ptr) |ret_ptr| return ret_ptr; |
| 6843 | const ret_llvm_ty = try self.dg.llvmType(ret_ty); | |
| 6923 | const ret_llvm_ty = try self.dg.lowerType(ret_ty); | |
| 6844 | 6924 | const target = self.dg.module.getTarget(); |
| 6845 | 6925 | const alloca_inst = self.buildAlloca(ret_llvm_ty); |
| 6846 | 6926 | alloca_inst.setAlignment(ptr_ty.ptrAlignment(target)); |
| ... | ... | @@ -6871,7 +6951,7 @@ pub const FuncGen = struct { |
| 6871 | 6951 | const dest_ptr_u8 = self.builder.buildBitCast(dest_ptr, ptr_u8_llvm_ty, ""); |
| 6872 | 6952 | const fill_char = u8_llvm_ty.constInt(0xaa, .False); |
| 6873 | 6953 | const dest_ptr_align = ptr_ty.ptrAlignment(target); |
| 6874 | const usize_llvm_ty = try self.dg.llvmType(Type.usize); | |
| 6954 | const usize_llvm_ty = try self.dg.lowerType(Type.usize); | |
| 6875 | 6955 | const len = usize_llvm_ty.constInt(operand_size, .False); |
| 6876 | 6956 | _ = self.builder.buildMemSet(dest_ptr_u8, fill_char, len, dest_ptr_align, ptr_ty.isVolatilePtr()); |
| 6877 | 6957 | if (self.dg.module.comp.bin_file.options.valgrind) { |
| ... | ... | @@ -6908,7 +6988,7 @@ pub const FuncGen = struct { |
| 6908 | 6988 | const llvm_fn = self.getIntrinsic("llvm.returnaddress", &.{}); |
| 6909 | 6989 | const params = [_]*const llvm.Value{llvm_i32.constNull()}; |
| 6910 | 6990 | const ptr_val = self.builder.buildCall(llvm_fn, &params, params.len, .Fast, .Auto, ""); |
| 6911 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 6991 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 6912 | 6992 | return self.builder.buildPtrToInt(ptr_val, llvm_usize, ""); |
| 6913 | 6993 | } |
| 6914 | 6994 | |
| ... | ... | @@ -6926,7 +7006,7 @@ pub const FuncGen = struct { |
| 6926 | 7006 | |
| 6927 | 7007 | const params = [_]*const llvm.Value{llvm_i32.constNull()}; |
| 6928 | 7008 | const ptr_val = self.builder.buildCall(llvm_fn, &params, params.len, .Fast, .Auto, ""); |
| 6929 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 7009 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 6930 | 7010 | return self.builder.buildPtrToInt(ptr_val, llvm_usize, ""); |
| 6931 | 7011 | } |
| 6932 | 7012 | |
| ... | ... | @@ -6971,15 +7051,15 @@ pub const FuncGen = struct { |
| 6971 | 7051 | |
| 6972 | 7052 | var payload = self.builder.buildExtractValue(result, 0, ""); |
| 6973 | 7053 | if (opt_abi_ty != null) { |
| 6974 | payload = self.builder.buildTrunc(payload, try self.dg.llvmType(operand_ty), ""); | |
| 7054 | payload = self.builder.buildTrunc(payload, try self.dg.lowerType(operand_ty), ""); | |
| 6975 | 7055 | } |
| 6976 | 7056 | const success_bit = self.builder.buildExtractValue(result, 1, ""); |
| 6977 | 7057 | |
| 6978 | if (optional_ty.isPtrLikeOptional()) { | |
| 7058 | if (optional_ty.optionalReprIsPayload()) { | |
| 6979 | 7059 | return self.builder.buildSelect(success_bit, payload.typeOf().constNull(), payload, ""); |
| 6980 | 7060 | } |
| 6981 | 7061 | |
| 6982 | const optional_llvm_ty = try self.dg.llvmType(optional_ty); | |
| 7062 | const optional_llvm_ty = try self.dg.lowerType(optional_ty); | |
| 6983 | 7063 | const non_null_bit = self.builder.buildNot(success_bit, ""); |
| 6984 | 7064 | const partial = self.builder.buildInsertValue(optional_llvm_ty.getUndef(), payload, 0, ""); |
| 6985 | 7065 | return self.builder.buildInsertValue(partial, non_null_bit, 1, ""); |
| ... | ... | @@ -7015,7 +7095,7 @@ pub const FuncGen = struct { |
| 7015 | 7095 | ordering, |
| 7016 | 7096 | single_threaded, |
| 7017 | 7097 | ); |
| 7018 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 7098 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 7019 | 7099 | if (is_float) { |
| 7020 | 7100 | return self.builder.buildBitCast(uncasted_result, operand_llvm_ty, ""); |
| 7021 | 7101 | } else { |
| ... | ... | @@ -7028,7 +7108,7 @@ pub const FuncGen = struct { |
| 7028 | 7108 | } |
| 7029 | 7109 | |
| 7030 | 7110 | // It's a pointer but we need to treat it as an int. |
| 7031 | const usize_llvm_ty = try self.dg.llvmType(Type.usize); | |
| 7111 | const usize_llvm_ty = try self.dg.lowerType(Type.usize); | |
| 7032 | 7112 | const casted_ptr = self.builder.buildBitCast(ptr, usize_llvm_ty.pointerType(0), ""); |
| 7033 | 7113 | const casted_operand = self.builder.buildPtrToInt(operand, usize_llvm_ty, ""); |
| 7034 | 7114 | const uncasted_result = self.builder.buildAtomicRmw( |
| ... | ... | @@ -7038,7 +7118,7 @@ pub const FuncGen = struct { |
| 7038 | 7118 | ordering, |
| 7039 | 7119 | single_threaded, |
| 7040 | 7120 | ); |
| 7041 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 7121 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 7042 | 7122 | return self.builder.buildIntToPtr(uncasted_result, operand_llvm_ty, ""); |
| 7043 | 7123 | } |
| 7044 | 7124 | |
| ... | ... | @@ -7057,7 +7137,7 @@ pub const FuncGen = struct { |
| 7057 | 7137 | const casted_ptr = self.builder.buildBitCast(ptr, abi_ty.pointerType(0), ""); |
| 7058 | 7138 | const load_inst = (try self.load(casted_ptr, ptr_ty)).?; |
| 7059 | 7139 | load_inst.setOrdering(ordering); |
| 7060 | return self.builder.buildTrunc(load_inst, try self.dg.llvmType(operand_ty), ""); | |
| 7140 | return self.builder.buildTrunc(load_inst, try self.dg.lowerType(operand_ty), ""); | |
| 7061 | 7141 | } |
| 7062 | 7142 | const load_inst = (try self.load(ptr, ptr_ty)).?; |
| 7063 | 7143 | load_inst.setOrdering(ordering); |
| ... | ... | @@ -7198,13 +7278,13 @@ pub const FuncGen = struct { |
| 7198 | 7278 | const operand = try self.resolveInst(ty_op.operand); |
| 7199 | 7279 | |
| 7200 | 7280 | const llvm_i1 = self.context.intType(1); |
| 7201 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 7281 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 7202 | 7282 | const fn_val = self.getIntrinsic(llvm_fn_name, &.{operand_llvm_ty}); |
| 7203 | 7283 | |
| 7204 | 7284 | const params = [_]*const llvm.Value{ operand, llvm_i1.constNull() }; |
| 7205 | 7285 | const wrong_size_result = self.builder.buildCall(fn_val, &params, params.len, .C, .Auto, ""); |
| 7206 | 7286 | const result_ty = self.air.typeOfIndex(inst); |
| 7207 | const result_llvm_ty = try self.dg.llvmType(result_ty); | |
| 7287 | const result_llvm_ty = try self.dg.lowerType(result_ty); | |
| 7208 | 7288 | |
| 7209 | 7289 | const target = self.dg.module.getTarget(); |
| 7210 | 7290 | const bits = operand_ty.intInfo(target).bits; |
| ... | ... | @@ -7226,12 +7306,12 @@ pub const FuncGen = struct { |
| 7226 | 7306 | const operand = try self.resolveInst(ty_op.operand); |
| 7227 | 7307 | |
| 7228 | 7308 | const params = [_]*const llvm.Value{operand}; |
| 7229 | const operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 7309 | const operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 7230 | 7310 | const fn_val = self.getIntrinsic(llvm_fn_name, &.{operand_llvm_ty}); |
| 7231 | 7311 | |
| 7232 | 7312 | const wrong_size_result = self.builder.buildCall(fn_val, &params, params.len, .C, .Auto, ""); |
| 7233 | 7313 | const result_ty = self.air.typeOfIndex(inst); |
| 7234 | const result_llvm_ty = try self.dg.llvmType(result_ty); | |
| 7314 | const result_llvm_ty = try self.dg.lowerType(result_ty); | |
| 7235 | 7315 | |
| 7236 | 7316 | const target = self.dg.module.getTarget(); |
| 7237 | 7317 | const bits = operand_ty.intInfo(target).bits; |
| ... | ... | @@ -7255,7 +7335,7 @@ pub const FuncGen = struct { |
| 7255 | 7335 | assert(bits % 8 == 0); |
| 7256 | 7336 | |
| 7257 | 7337 | var operand = try self.resolveInst(ty_op.operand); |
| 7258 | var operand_llvm_ty = try self.dg.llvmType(operand_ty); | |
| 7338 | var operand_llvm_ty = try self.dg.lowerType(operand_ty); | |
| 7259 | 7339 | |
| 7260 | 7340 | if (bits % 16 == 8) { |
| 7261 | 7341 | // If not an even byte-multiple, we need zero-extend + shift-left 1 byte |
| ... | ... | @@ -7289,7 +7369,7 @@ pub const FuncGen = struct { |
| 7289 | 7369 | const wrong_size_result = self.builder.buildCall(fn_val, &params, params.len, .C, .Auto, ""); |
| 7290 | 7370 | |
| 7291 | 7371 | const result_ty = self.air.typeOfIndex(inst); |
| 7292 | const result_llvm_ty = try self.dg.llvmType(result_ty); | |
| 7372 | const result_llvm_ty = try self.dg.lowerType(result_ty); | |
| 7293 | 7373 | const result_bits = result_ty.intInfo(target).bits; |
| 7294 | 7374 | if (bits > result_bits) { |
| 7295 | 7375 | return self.builder.buildTrunc(wrong_size_result, result_llvm_ty, ""); |
| ... | ... | @@ -7332,14 +7412,14 @@ pub const FuncGen = struct { |
| 7332 | 7412 | } |
| 7333 | 7413 | |
| 7334 | 7414 | const slice_ty = Type.initTag(.const_slice_u8_sentinel_0); |
| 7335 | const llvm_ret_ty = try self.dg.llvmType(slice_ty); | |
| 7336 | const usize_llvm_ty = try self.dg.llvmType(Type.usize); | |
| 7415 | const llvm_ret_ty = try self.dg.lowerType(slice_ty); | |
| 7416 | const usize_llvm_ty = try self.dg.lowerType(Type.usize); | |
| 7337 | 7417 | const target = self.dg.module.getTarget(); |
| 7338 | 7418 | const slice_alignment = slice_ty.abiAlignment(target); |
| 7339 | 7419 | |
| 7340 | 7420 | var int_tag_type_buffer: Type.Payload.Bits = undefined; |
| 7341 | 7421 | const int_tag_ty = enum_ty.intTagType(&int_tag_type_buffer); |
| 7342 | const param_types = [_]*const llvm.Type{try self.dg.llvmType(int_tag_ty)}; | |
| 7422 | const param_types = [_]*const llvm.Type{try self.dg.lowerType(int_tag_ty)}; | |
| 7343 | 7423 | |
| 7344 | 7424 | const fn_type = llvm.functionType(llvm_ret_ty, &param_types, param_types.len, .False); |
| 7345 | 7425 | const fn_val = self.dg.object.llvm_module.addFunction(llvm_fn_name, fn_type); |
| ... | ... | @@ -7396,7 +7476,7 @@ pub const FuncGen = struct { |
| 7396 | 7476 | .base = .{ .tag = .enum_field_index }, |
| 7397 | 7477 | .data = @intCast(u32, field_index), |
| 7398 | 7478 | }; |
| 7399 | break :int try self.dg.genTypedValue(.{ | |
| 7479 | break :int try self.dg.lowerValue(.{ | |
| 7400 | 7480 | .ty = enum_ty, |
| 7401 | 7481 | .val = Value.initPayload(&tag_val_payload.base), |
| 7402 | 7482 | }); |
| ... | ... | @@ -7421,8 +7501,8 @@ pub const FuncGen = struct { |
| 7421 | 7501 | |
| 7422 | 7502 | // Function signature: fn (anyerror) bool |
| 7423 | 7503 | |
| 7424 | const ret_llvm_ty = try self.dg.llvmType(Type.bool); | |
| 7425 | const anyerror_llvm_ty = try self.dg.llvmType(Type.anyerror); | |
| 7504 | const ret_llvm_ty = try self.dg.lowerType(Type.bool); | |
| 7505 | const anyerror_llvm_ty = try self.dg.lowerType(Type.anyerror); | |
| 7426 | 7506 | const param_types = [_]*const llvm.Type{anyerror_llvm_ty}; |
| 7427 | 7507 | |
| 7428 | 7508 | const fn_type = llvm.functionType(ret_llvm_ty, &param_types, param_types.len, .False); |
| ... | ... | @@ -7531,7 +7611,7 @@ pub const FuncGen = struct { |
| 7531 | 7611 | .Add => switch (scalar_ty.zigTypeTag()) { |
| 7532 | 7612 | .Int => return self.builder.buildAddReduce(operand), |
| 7533 | 7613 | .Float => { |
| 7534 | const scalar_llvm_ty = try self.dg.llvmType(scalar_ty); | |
| 7614 | const scalar_llvm_ty = try self.dg.lowerType(scalar_ty); | |
| 7535 | 7615 | const neutral_value = scalar_llvm_ty.constReal(-0.0); |
| 7536 | 7616 | return self.builder.buildFPAddReduce(neutral_value, operand); |
| 7537 | 7617 | }, |
| ... | ... | @@ -7540,7 +7620,7 @@ pub const FuncGen = struct { |
| 7540 | 7620 | .Mul => switch (scalar_ty.zigTypeTag()) { |
| 7541 | 7621 | .Int => return self.builder.buildMulReduce(operand), |
| 7542 | 7622 | .Float => { |
| 7543 | const scalar_llvm_ty = try self.dg.llvmType(scalar_ty); | |
| 7623 | const scalar_llvm_ty = try self.dg.lowerType(scalar_ty); | |
| 7544 | 7624 | const neutral_value = scalar_llvm_ty.constReal(1.0); |
| 7545 | 7625 | return self.builder.buildFPMulReduce(neutral_value, operand); |
| 7546 | 7626 | }, |
| ... | ... | @@ -7556,7 +7636,7 @@ pub const FuncGen = struct { |
| 7556 | 7636 | const result_ty = self.air.typeOfIndex(inst); |
| 7557 | 7637 | const len = @intCast(usize, result_ty.arrayLen()); |
| 7558 | 7638 | const elements = @ptrCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]); |
| 7559 | const llvm_result_ty = try self.dg.llvmType(result_ty); | |
| 7639 | const llvm_result_ty = try self.dg.lowerType(result_ty); | |
| 7560 | 7640 | const target = self.dg.module.getTarget(); |
| 7561 | 7641 | |
| 7562 | 7642 | switch (result_ty.zigTypeTag()) { |
| ... | ... | @@ -7644,7 +7724,7 @@ pub const FuncGen = struct { |
| 7644 | 7724 | .Array => { |
| 7645 | 7725 | assert(isByRef(result_ty)); |
| 7646 | 7726 | |
| 7647 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 7727 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 7648 | 7728 | const alloca_inst = self.buildAlloca(llvm_result_ty); |
| 7649 | 7729 | alloca_inst.setAlignment(result_ty.abiAlignment(target)); |
| 7650 | 7730 | |
| ... | ... | @@ -7679,7 +7759,7 @@ pub const FuncGen = struct { |
| 7679 | 7759 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 7680 | 7760 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 7681 | 7761 | const union_ty = self.air.typeOfIndex(inst); |
| 7682 | const union_llvm_ty = try self.dg.llvmType(union_ty); | |
| 7762 | const union_llvm_ty = try self.dg.lowerType(union_ty); | |
| 7683 | 7763 | const target = self.dg.module.getTarget(); |
| 7684 | 7764 | const layout = union_ty.unionGetLayout(target); |
| 7685 | 7765 | if (layout.payload_size == 0) { |
| ... | ... | @@ -7699,8 +7779,8 @@ pub const FuncGen = struct { |
| 7699 | 7779 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 7700 | 7780 | assert(union_obj.haveFieldTypes()); |
| 7701 | 7781 | const field = union_obj.fields.values()[extra.field_index]; |
| 7702 | const field_llvm_ty = try self.dg.llvmType(field.ty); | |
| 7703 | const tag_llvm_ty = try self.dg.llvmType(union_obj.tag_ty); | |
| 7782 | const field_llvm_ty = try self.dg.lowerType(field.ty); | |
| 7783 | const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty); | |
| 7704 | 7784 | const field_size = field.ty.abiSize(target); |
| 7705 | 7785 | const field_align = field.normalAlignment(target); |
| 7706 | 7786 | |
| ... | ... | @@ -7936,7 +8016,7 @@ pub const FuncGen = struct { |
| 7936 | 8016 | |
| 7937 | 8017 | const slice_ty = Type.initTag(.const_slice_u8_sentinel_0); |
| 7938 | 8018 | const slice_alignment = slice_ty.abiAlignment(self.dg.module.getTarget()); |
| 7939 | const llvm_slice_ty = try self.dg.llvmType(slice_ty); | |
| 8019 | const llvm_slice_ty = try self.dg.lowerType(slice_ty); | |
| 7940 | 8020 | const llvm_slice_ptr_ty = llvm_slice_ty.pointerType(0); // TODO: Address space |
| 7941 | 8021 | |
| 7942 | 8022 | const error_name_table_global = self.dg.object.llvm_module.addGlobal(llvm_slice_ptr_ty, "__zig_err_name_table"); |
| ... | ... | @@ -8000,7 +8080,7 @@ pub const FuncGen = struct { |
| 8000 | 8080 | // out the relevant bits when accessing the pointee. |
| 8001 | 8081 | // Here we perform a bitcast because we want to use the host_size |
| 8002 | 8082 | // as the llvm pointer element type. |
| 8003 | const result_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 8083 | const result_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 8004 | 8084 | // TODO this can be removed if we change host_size to be bits instead |
| 8005 | 8085 | // of bytes. |
| 8006 | 8086 | return self.builder.buildBitCast(struct_ptr, result_llvm_ty, ""); |
| ... | ... | @@ -8015,7 +8095,7 @@ pub const FuncGen = struct { |
| 8015 | 8095 | // end of the struct. Treat our struct pointer as an array of two and get |
| 8016 | 8096 | // the index to the element at index `1` to get a pointer to the end of |
| 8017 | 8097 | // the struct. |
| 8018 | const llvm_usize = try self.dg.llvmType(Type.usize); | |
| 8098 | const llvm_usize = try self.dg.lowerType(Type.usize); | |
| 8019 | 8099 | const llvm_index = llvm_usize.constInt(1, .False); |
| 8020 | 8100 | const indices: [1]*const llvm.Value = .{llvm_index}; |
| 8021 | 8101 | return self.builder.buildInBoundsGEP(struct_ptr, &indices, indices.len, ""); |
| ... | ... | @@ -8036,7 +8116,7 @@ pub const FuncGen = struct { |
| 8036 | 8116 | ) !?*const llvm.Value { |
| 8037 | 8117 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 8038 | 8118 | const field = &union_obj.fields.values()[field_index]; |
| 8039 | const result_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst)); | |
| 8119 | const result_llvm_ty = try self.dg.lowerType(self.air.typeOfIndex(inst)); | |
| 8040 | 8120 | if (!field.ty.hasRuntimeBitsIgnoreComptime()) { |
| 8041 | 8121 | return null; |
| 8042 | 8122 | } |
| ... | ... | @@ -8075,7 +8155,7 @@ pub const FuncGen = struct { |
| 8075 | 8155 | const ptr_volatile = llvm.Bool.fromBool(ptr_ty.isVolatilePtr()); |
| 8076 | 8156 | if (info.host_size == 0) { |
| 8077 | 8157 | if (isByRef(info.pointee_type)) { |
| 8078 | const elem_llvm_ty = try self.dg.llvmType(info.pointee_type); | |
| 8158 | const elem_llvm_ty = try self.dg.lowerType(info.pointee_type); | |
| 8079 | 8159 | const result_align = info.pointee_type.abiAlignment(target); |
| 8080 | 8160 | const max_align = @maximum(result_align, ptr_alignment); |
| 8081 | 8161 | const result_ptr = self.buildAlloca(elem_llvm_ty); |
| ... | ... | @@ -8108,7 +8188,7 @@ pub const FuncGen = struct { |
| 8108 | 8188 | const elem_bits = @intCast(c_uint, ptr_ty.elemType().bitSize(target)); |
| 8109 | 8189 | const shift_amt = containing_int.typeOf().constInt(info.bit_offset, .False); |
| 8110 | 8190 | const shifted_value = self.builder.buildLShr(containing_int, shift_amt, ""); |
| 8111 | const elem_llvm_ty = try self.dg.llvmType(info.pointee_type); | |
| 8191 | const elem_llvm_ty = try self.dg.lowerType(info.pointee_type); | |
| 8112 | 8192 | |
| 8113 | 8193 | if (isByRef(info.pointee_type)) { |
| 8114 | 8194 | const result_align = info.pointee_type.abiAlignment(target); |
| ... | ... | @@ -8546,7 +8626,14 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool |
| 8546 | 8626 | /// be effectively bitcasted to the actual return type. |
| 8547 | 8627 | fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm.Type { |
| 8548 | 8628 | if (!fn_info.return_type.hasRuntimeBitsIgnoreComptime()) { |
| 8549 | return dg.context.voidType(); | |
| 8629 | // If the return type is an error set or an error union, then we make this | |
| 8630 | // anyerror return type instead, so that it can be coerced into a function | |
| 8631 | // pointer type which has anyerror as the return type. | |
| 8632 | if (fn_info.return_type.isError()) { | |
| 8633 | return dg.lowerType(Type.anyerror); | |
| 8634 | } else { | |
| 8635 | return dg.context.voidType(); | |
| 8636 | } | |
| 8550 | 8637 | } |
| 8551 | 8638 | const target = dg.module.getTarget(); |
| 8552 | 8639 | switch (fn_info.cc) { |
| ... | ... | @@ -8554,7 +8641,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm. |
| 8554 | 8641 | if (isByRef(fn_info.return_type)) { |
| 8555 | 8642 | return dg.context.voidType(); |
| 8556 | 8643 | } else { |
| 8557 | return dg.llvmType(fn_info.return_type); | |
| 8644 | return dg.lowerType(fn_info.return_type); | |
| 8558 | 8645 | } |
| 8559 | 8646 | }, |
| 8560 | 8647 | .C => { |
| ... | ... | @@ -8575,24 +8662,24 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm. |
| 8575 | 8662 | else => false, |
| 8576 | 8663 | }; |
| 8577 | 8664 | switch (target.cpu.arch) { |
| 8578 | .mips, .mipsel => return dg.llvmType(fn_info.return_type), | |
| 8665 | .mips, .mipsel => return dg.lowerType(fn_info.return_type), | |
| 8579 | 8666 | .x86_64 => switch (target.os.tag) { |
| 8580 | 8667 | .windows => switch (x86_64_abi.classifyWindows(fn_info.return_type, target)) { |
| 8581 | 8668 | .integer => { |
| 8582 | 8669 | if (is_scalar) { |
| 8583 | return dg.llvmType(fn_info.return_type); | |
| 8670 | return dg.lowerType(fn_info.return_type); | |
| 8584 | 8671 | } else { |
| 8585 | 8672 | const abi_size = fn_info.return_type.abiSize(target); |
| 8586 | 8673 | return dg.context.intType(@intCast(c_uint, abi_size * 8)); |
| 8587 | 8674 | } |
| 8588 | 8675 | }, |
| 8589 | 8676 | .memory => return dg.context.voidType(), |
| 8590 | .sse => return dg.llvmType(fn_info.return_type), | |
| 8677 | .sse => return dg.lowerType(fn_info.return_type), | |
| 8591 | 8678 | else => unreachable, |
| 8592 | 8679 | }, |
| 8593 | 8680 | else => { |
| 8594 | 8681 | if (is_scalar) { |
| 8595 | return dg.llvmType(fn_info.return_type); | |
| 8682 | return dg.lowerType(fn_info.return_type); | |
| 8596 | 8683 | } |
| 8597 | 8684 | const classes = x86_64_abi.classifySystemV(fn_info.return_type, target); |
| 8598 | 8685 | if (classes[0] == .memory) { |
| ... | ... | @@ -8633,10 +8720,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm. |
| 8633 | 8720 | }, |
| 8634 | 8721 | }, |
| 8635 | 8722 | // TODO investigate C ABI for other architectures |
| 8636 | else => return dg.llvmType(fn_info.return_type), | |
| 8723 | else => return dg.lowerType(fn_info.return_type), | |
| 8637 | 8724 | } |
| 8638 | 8725 | }, |
| 8639 | else => return dg.llvmType(fn_info.return_type), | |
| 8726 | else => return dg.lowerType(fn_info.return_type), | |
| 8640 | 8727 | } |
| 8641 | 8728 | } |
| 8642 | 8729 | |
| ... | ... | @@ -8991,3 +9078,11 @@ fn buildAllocaInner( |
| 8991 | 9078 | |
| 8992 | 9079 | return builder.buildAlloca(llvm_ty, ""); |
| 8993 | 9080 | } |
| 9081 | ||
| 9082 | fn errUnionPayloadOffset(payload_ty: Type, target: std.Target) u1 { | |
| 9083 | return @boolToInt(Type.anyerror.abiAlignment(target) > payload_ty.abiAlignment(target)); | |
| 9084 | } | |
| 9085 | ||
| 9086 | fn errUnionErrorOffset(payload_ty: Type, target: std.Target) u1 { | |
| 9087 | return @boolToInt(Type.anyerror.abiAlignment(target) <= payload_ty.abiAlignment(target)); | |
| 9088 | } |
src/link/Dwarf.zig+6-4| ... | ... | @@ -498,9 +498,11 @@ pub const DeclState = struct { |
| 498 | 498 | .ErrorUnion => { |
| 499 | 499 | const error_ty = ty.errorUnionSet(); |
| 500 | 500 | const payload_ty = ty.errorUnionPayload(); |
| 501 | const payload_align = payload_ty.abiAlignment(target); | |
| 502 | const error_align = Type.anyerror.abiAlignment(target); | |
| 501 | 503 | const abi_size = ty.abiSize(target); |
| 502 | const abi_align = ty.abiAlignment(target); | |
| 503 | const payload_off = mem.alignForwardGeneric(u64, error_ty.abiSize(target), abi_align); | |
| 504 | const payload_off = if (error_align >= payload_align) Type.anyerror.abiSize(target) else 0; | |
| 505 | const error_off = if (error_align >= payload_align) 0 else payload_ty.abiSize(target); | |
| 504 | 506 | |
| 505 | 507 | // DW.AT.structure_type |
| 506 | 508 | try dbg_info_buffer.append(@enumToInt(AbbrevKind.struct_type)); |
| ... | ... | @@ -534,7 +536,7 @@ pub const DeclState = struct { |
| 534 | 536 | try dbg_info_buffer.resize(index + 4); |
| 535 | 537 | try self.addTypeReloc(atom, error_ty, @intCast(u32, index), null); |
| 536 | 538 | // DW.AT.data_member_location, DW.FORM.sdata |
| 537 | try dbg_info_buffer.append(0); | |
| 539 | try leb128.writeULEB128(dbg_info_buffer.writer(), error_off); | |
| 538 | 540 | |
| 539 | 541 | // DW.AT.structure_type delimit children |
| 540 | 542 | try dbg_info_buffer.append(0); |
| ... | ... | @@ -2293,7 +2295,7 @@ fn addDbgInfoErrorSet( |
| 2293 | 2295 | // DW.AT.enumeration_type |
| 2294 | 2296 | try dbg_info_buffer.append(@enumToInt(AbbrevKind.enum_type)); |
| 2295 | 2297 | // DW.AT.byte_size, DW.FORM.sdata |
| 2296 | const abi_size = ty.abiSize(target); | |
| 2298 | const abi_size = Type.anyerror.abiSize(target); | |
| 2297 | 2299 | try leb128.writeULEB128(dbg_info_buffer.writer(), abi_size); |
| 2298 | 2300 | // DW.AT.name, DW.FORM.string |
| 2299 | 2301 | const name = try ty.nameAllocArena(arena, module); |
src/type.zig+298-60| ... | ... | @@ -2317,10 +2317,7 @@ pub const Type = extern union { |
| 2317 | 2317 | .const_slice_u8_sentinel_0, |
| 2318 | 2318 | .array_u8_sentinel_0, |
| 2319 | 2319 | .anyerror_void_error_union, |
| 2320 | .error_set, | |
| 2321 | .error_set_single, | |
| 2322 | 2320 | .error_set_inferred, |
| 2323 | .error_set_merged, | |
| 2324 | 2321 | .manyptr_u8, |
| 2325 | 2322 | .manyptr_const_u8, |
| 2326 | 2323 | .manyptr_const_u8_sentinel_0, |
| ... | ... | @@ -2361,12 +2358,23 @@ pub const Type = extern union { |
| 2361 | 2358 | .fn_void_no_args, |
| 2362 | 2359 | .fn_naked_noreturn_no_args, |
| 2363 | 2360 | .fn_ccc_void_no_args, |
| 2361 | .error_set_single, | |
| 2364 | 2362 | => return false, |
| 2365 | 2363 | |
| 2364 | .error_set => { | |
| 2365 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 2366 | const names = err_set_obj.names.keys(); | |
| 2367 | return names.len > 1; | |
| 2368 | }, | |
| 2369 | .error_set_merged => { | |
| 2370 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 2371 | const names = name_map.keys(); | |
| 2372 | return names.len > 1; | |
| 2373 | }, | |
| 2374 | ||
| 2366 | 2375 | // These types have more than one possible value, so the result is the same as |
| 2367 | 2376 | // asking whether they are comptime-only types. |
| 2368 | 2377 | .anyframe_T, |
| 2369 | .optional, | |
| 2370 | 2378 | .optional_single_mut_pointer, |
| 2371 | 2379 | .optional_single_const_pointer, |
| 2372 | 2380 | .single_const_pointer, |
| ... | ... | @@ -2388,6 +2396,41 @@ pub const Type = extern union { |
| 2388 | 2396 | } |
| 2389 | 2397 | }, |
| 2390 | 2398 | |
| 2399 | .optional => { | |
| 2400 | var buf: Payload.ElemType = undefined; | |
| 2401 | const child_ty = ty.optionalChild(&buf); | |
| 2402 | if (child_ty.isNoReturn()) { | |
| 2403 | // Then the optional is comptime-known to be null. | |
| 2404 | return false; | |
| 2405 | } | |
| 2406 | if (ignore_comptime_only) { | |
| 2407 | return true; | |
| 2408 | } else if (sema_kit) |sk| { | |
| 2409 | return !(try sk.sema.typeRequiresComptime(sk.block, sk.src, child_ty)); | |
| 2410 | } else { | |
| 2411 | return !comptimeOnly(child_ty); | |
| 2412 | } | |
| 2413 | }, | |
| 2414 | ||
| 2415 | .error_union => { | |
| 2416 | // This code needs to be kept in sync with the equivalent switch prong | |
| 2417 | // in abiSizeAdvanced. | |
| 2418 | const data = ty.castTag(.error_union).?.data; | |
| 2419 | switch (data.error_set.errorSetCardinality()) { | |
| 2420 | .zero => return hasRuntimeBitsAdvanced(data.payload, ignore_comptime_only, sema_kit), | |
| 2421 | .one => return !data.payload.isNoReturn(), | |
| 2422 | .many => { | |
| 2423 | if (ignore_comptime_only) { | |
| 2424 | return true; | |
| 2425 | } else if (sema_kit) |sk| { | |
| 2426 | return !(try sk.sema.typeRequiresComptime(sk.block, sk.src, ty)); | |
| 2427 | } else { | |
| 2428 | return !comptimeOnly(ty); | |
| 2429 | } | |
| 2430 | }, | |
| 2431 | } | |
| 2432 | }, | |
| 2433 | ||
| 2391 | 2434 | .@"struct" => { |
| 2392 | 2435 | const struct_obj = ty.castTag(.@"struct").?.data; |
| 2393 | 2436 | if (sema_kit) |sk| { |
| ... | ... | @@ -2467,12 +2510,6 @@ pub const Type = extern union { |
| 2467 | 2510 | |
| 2468 | 2511 | .int_signed, .int_unsigned => return ty.cast(Payload.Bits).?.data != 0, |
| 2469 | 2512 | |
| 2470 | .error_union => { | |
| 2471 | const payload = ty.castTag(.error_union).?.data; | |
| 2472 | return (try payload.error_set.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) or | |
| 2473 | (try payload.payload.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)); | |
| 2474 | }, | |
| 2475 | ||
| 2476 | 2513 | .tuple, .anon_struct => { |
| 2477 | 2514 | const tuple = ty.tupleFields(); |
| 2478 | 2515 | for (tuple.types) |field_ty, i| { |
| ... | ... | @@ -2647,13 +2684,22 @@ pub const Type = extern union { |
| 2647 | 2684 | }; |
| 2648 | 2685 | } |
| 2649 | 2686 | |
| 2650 | pub fn isNoReturn(self: Type) bool { | |
| 2651 | const definitely_correct_result = | |
| 2652 | self.tag_if_small_enough != .bound_fn and | |
| 2653 | self.zigTypeTag() == .NoReturn; | |
| 2654 | const fast_result = self.tag_if_small_enough == Tag.noreturn; | |
| 2655 | assert(fast_result == definitely_correct_result); | |
| 2656 | return fast_result; | |
| 2687 | /// TODO add enums with no fields here | |
| 2688 | pub fn isNoReturn(ty: Type) bool { | |
| 2689 | switch (ty.tag()) { | |
| 2690 | .noreturn => return true, | |
| 2691 | .error_set => { | |
| 2692 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 2693 | const names = err_set_obj.names.keys(); | |
| 2694 | return names.len == 0; | |
| 2695 | }, | |
| 2696 | .error_set_merged => { | |
| 2697 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 2698 | const names = name_map.keys(); | |
| 2699 | return names.len == 0; | |
| 2700 | }, | |
| 2701 | else => return false, | |
| 2702 | } | |
| 2657 | 2703 | } |
| 2658 | 2704 | |
| 2659 | 2705 | /// Returns 0 if the pointer is naturally aligned and the element type is 0-bit. |
| ... | ... | @@ -2852,13 +2898,30 @@ pub const Type = extern union { |
| 2852 | 2898 | else => unreachable, |
| 2853 | 2899 | }, |
| 2854 | 2900 | |
| 2855 | .error_set, | |
| 2856 | .error_set_single, | |
| 2901 | // TODO revisit this when we have the concept of the error tag type | |
| 2857 | 2902 | .anyerror_void_error_union, |
| 2858 | 2903 | .anyerror, |
| 2859 | 2904 | .error_set_inferred, |
| 2860 | .error_set_merged, | |
| 2861 | => return AbiAlignmentAdvanced{ .scalar = 2 }, // TODO revisit this when we have the concept of the error tag type | |
| 2905 | => return AbiAlignmentAdvanced{ .scalar = 2 }, | |
| 2906 | ||
| 2907 | .error_set => { | |
| 2908 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 2909 | const names = err_set_obj.names.keys(); | |
| 2910 | if (names.len <= 1) { | |
| 2911 | return AbiAlignmentAdvanced{ .scalar = 0 }; | |
| 2912 | } else { | |
| 2913 | return AbiAlignmentAdvanced{ .scalar = 2 }; | |
| 2914 | } | |
| 2915 | }, | |
| 2916 | .error_set_merged => { | |
| 2917 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 2918 | const names = name_map.keys(); | |
| 2919 | if (names.len <= 1) { | |
| 2920 | return AbiAlignmentAdvanced{ .scalar = 0 }; | |
| 2921 | } else { | |
| 2922 | return AbiAlignmentAdvanced{ .scalar = 2 }; | |
| 2923 | } | |
| 2924 | }, | |
| 2862 | 2925 | |
| 2863 | 2926 | .array, .array_sentinel => return ty.elemType().abiAlignmentAdvanced(target, strat), |
| 2864 | 2927 | |
| ... | ... | @@ -2881,8 +2944,16 @@ pub const Type = extern union { |
| 2881 | 2944 | var buf: Payload.ElemType = undefined; |
| 2882 | 2945 | const child_type = ty.optionalChild(&buf); |
| 2883 | 2946 | |
| 2884 | if (child_type.zigTypeTag() == .Pointer and !child_type.isCPtr()) { | |
| 2885 | return AbiAlignmentAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) }; | |
| 2947 | switch (child_type.zigTypeTag()) { | |
| 2948 | .Pointer => return AbiAlignmentAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) }, | |
| 2949 | .ErrorSet => switch (child_type.errorSetCardinality()) { | |
| 2950 | // `?error{}` is comptime-known to be null. | |
| 2951 | .zero => return AbiAlignmentAdvanced{ .scalar = 0 }, | |
| 2952 | .one => return AbiAlignmentAdvanced{ .scalar = 1 }, | |
| 2953 | .many => return abiAlignmentAdvanced(Type.anyerror, target, strat), | |
| 2954 | }, | |
| 2955 | .NoReturn => return AbiAlignmentAdvanced{ .scalar = 0 }, | |
| 2956 | else => {}, | |
| 2886 | 2957 | } |
| 2887 | 2958 | |
| 2888 | 2959 | switch (strat) { |
| ... | ... | @@ -2900,31 +2971,35 @@ pub const Type = extern union { |
| 2900 | 2971 | }, |
| 2901 | 2972 | |
| 2902 | 2973 | .error_union => { |
| 2974 | // This code needs to be kept in sync with the equivalent switch prong | |
| 2975 | // in abiSizeAdvanced. | |
| 2903 | 2976 | const data = ty.castTag(.error_union).?.data; |
| 2977 | switch (data.error_set.errorSetCardinality()) { | |
| 2978 | .zero => return abiAlignmentAdvanced(data.payload, target, strat), | |
| 2979 | .one => { | |
| 2980 | if (data.payload.isNoReturn()) { | |
| 2981 | return AbiAlignmentAdvanced{ .scalar = 0 }; | |
| 2982 | } | |
| 2983 | }, | |
| 2984 | .many => {}, | |
| 2985 | } | |
| 2986 | const code_align = abiAlignment(Type.anyerror, target); | |
| 2904 | 2987 | switch (strat) { |
| 2905 | 2988 | .eager, .sema_kit => { |
| 2906 | if (!(try data.error_set.hasRuntimeBitsAdvanced(false, sema_kit))) { | |
| 2907 | return data.payload.abiAlignmentAdvanced(target, strat); | |
| 2908 | } else if (!(try data.payload.hasRuntimeBitsAdvanced(false, sema_kit))) { | |
| 2909 | return data.error_set.abiAlignmentAdvanced(target, strat); | |
| 2989 | if (!(try data.payload.hasRuntimeBitsAdvanced(false, sema_kit))) { | |
| 2990 | return AbiAlignmentAdvanced{ .scalar = code_align }; | |
| 2910 | 2991 | } |
| 2911 | 2992 | return AbiAlignmentAdvanced{ .scalar = @maximum( |
| 2993 | code_align, | |
| 2912 | 2994 | (try data.payload.abiAlignmentAdvanced(target, strat)).scalar, |
| 2913 | (try data.error_set.abiAlignmentAdvanced(target, strat)).scalar, | |
| 2914 | 2995 | ) }; |
| 2915 | 2996 | }, |
| 2916 | 2997 | .lazy => |arena| { |
| 2917 | 2998 | switch (try data.payload.abiAlignmentAdvanced(target, strat)) { |
| 2918 | 2999 | .scalar => |payload_align| { |
| 2919 | if (payload_align == 0) { | |
| 2920 | return data.error_set.abiAlignmentAdvanced(target, strat); | |
| 2921 | } | |
| 2922 | switch (try data.error_set.abiAlignmentAdvanced(target, strat)) { | |
| 2923 | .scalar => |err_set_align| { | |
| 2924 | return AbiAlignmentAdvanced{ .scalar = @maximum(payload_align, err_set_align) }; | |
| 2925 | }, | |
| 2926 | .val => {}, | |
| 2927 | } | |
| 3000 | return AbiAlignmentAdvanced{ | |
| 3001 | .scalar = @maximum(code_align, payload_align), | |
| 3002 | }; | |
| 2928 | 3003 | }, |
| 2929 | 3004 | .val => {}, |
| 2930 | 3005 | } |
| ... | ... | @@ -3018,6 +3093,7 @@ pub const Type = extern union { |
| 3018 | 3093 | .@"undefined", |
| 3019 | 3094 | .enum_literal, |
| 3020 | 3095 | .type_info, |
| 3096 | .error_set_single, | |
| 3021 | 3097 | => return AbiAlignmentAdvanced{ .scalar = 0 }, |
| 3022 | 3098 | |
| 3023 | 3099 | .noreturn, |
| ... | ... | @@ -3136,6 +3212,7 @@ pub const Type = extern union { |
| 3136 | 3212 | .empty_struct_literal, |
| 3137 | 3213 | .empty_struct, |
| 3138 | 3214 | .void, |
| 3215 | .error_set_single, | |
| 3139 | 3216 | => return AbiSizeAdvanced{ .scalar = 0 }, |
| 3140 | 3217 | |
| 3141 | 3218 | .@"struct", .tuple, .anon_struct => switch (ty.containerLayout()) { |
| ... | ... | @@ -3291,14 +3368,30 @@ pub const Type = extern union { |
| 3291 | 3368 | }, |
| 3292 | 3369 | |
| 3293 | 3370 | // TODO revisit this when we have the concept of the error tag type |
| 3294 | .error_set, | |
| 3295 | .error_set_single, | |
| 3296 | 3371 | .anyerror_void_error_union, |
| 3297 | 3372 | .anyerror, |
| 3298 | 3373 | .error_set_inferred, |
| 3299 | .error_set_merged, | |
| 3300 | 3374 | => return AbiSizeAdvanced{ .scalar = 2 }, |
| 3301 | 3375 | |
| 3376 | .error_set => { | |
| 3377 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 3378 | const names = err_set_obj.names.keys(); | |
| 3379 | if (names.len <= 1) { | |
| 3380 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 3381 | } else { | |
| 3382 | return AbiSizeAdvanced{ .scalar = 2 }; | |
| 3383 | } | |
| 3384 | }, | |
| 3385 | .error_set_merged => { | |
| 3386 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 3387 | const names = name_map.keys(); | |
| 3388 | if (names.len <= 1) { | |
| 3389 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 3390 | } else { | |
| 3391 | return AbiSizeAdvanced{ .scalar = 2 }; | |
| 3392 | } | |
| 3393 | }, | |
| 3394 | ||
| 3302 | 3395 | .i16, .u16 => return AbiSizeAdvanced{ .scalar = intAbiSize(16, target) }, |
| 3303 | 3396 | .i32, .u32 => return AbiSizeAdvanced{ .scalar = intAbiSize(32, target) }, |
| 3304 | 3397 | .i64, .u64 => return AbiSizeAdvanced{ .scalar = intAbiSize(64, target) }, |
| ... | ... | @@ -3312,37 +3405,81 @@ pub const Type = extern union { |
| 3312 | 3405 | .optional => { |
| 3313 | 3406 | var buf: Payload.ElemType = undefined; |
| 3314 | 3407 | const child_type = ty.optionalChild(&buf); |
| 3408 | ||
| 3409 | if (child_type.isNoReturn()) { | |
| 3410 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 3411 | } | |
| 3412 | ||
| 3315 | 3413 | if (!child_type.hasRuntimeBits()) return AbiSizeAdvanced{ .scalar = 1 }; |
| 3316 | 3414 | |
| 3317 | if (child_type.zigTypeTag() == .Pointer and !child_type.isCPtr() and !child_type.isSlice()) | |
| 3318 | return AbiSizeAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) }; | |
| 3415 | switch (child_type.zigTypeTag()) { | |
| 3416 | .Pointer => { | |
| 3417 | const ptr_info = child_type.ptrInfo().data; | |
| 3418 | const has_null = switch (ptr_info.size) { | |
| 3419 | .Slice, .C => true, | |
| 3420 | else => ptr_info.@"allowzero", | |
| 3421 | }; | |
| 3422 | if (!has_null) { | |
| 3423 | const ptr_size_bytes = @divExact(target.cpu.arch.ptrBitWidth(), 8); | |
| 3424 | return AbiSizeAdvanced{ .scalar = ptr_size_bytes }; | |
| 3425 | } | |
| 3426 | }, | |
| 3427 | .ErrorSet => return abiSizeAdvanced(Type.anyerror, target, strat), | |
| 3428 | else => {}, | |
| 3429 | } | |
| 3319 | 3430 | |
| 3320 | 3431 | // Optional types are represented as a struct with the child type as the first |
| 3321 | 3432 | // field and a boolean as the second. Since the child type's abi alignment is |
| 3322 | 3433 | // guaranteed to be >= that of bool's (1 byte) the added size is exactly equal |
| 3323 | 3434 | // to the child type's ABI alignment. |
| 3324 | return AbiSizeAdvanced{ .scalar = child_type.abiAlignment(target) + child_type.abiSize(target) }; | |
| 3435 | return AbiSizeAdvanced{ | |
| 3436 | .scalar = child_type.abiAlignment(target) + child_type.abiSize(target), | |
| 3437 | }; | |
| 3325 | 3438 | }, |
| 3326 | 3439 | |
| 3327 | 3440 | .error_union => { |
| 3441 | // This code needs to be kept in sync with the equivalent switch prong | |
| 3442 | // in abiAlignmentAdvanced. | |
| 3328 | 3443 | const data = ty.castTag(.error_union).?.data; |
| 3329 | if (!data.error_set.hasRuntimeBits() and !data.payload.hasRuntimeBits()) { | |
| 3330 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 3331 | } else if (!data.error_set.hasRuntimeBits()) { | |
| 3332 | return AbiSizeAdvanced{ .scalar = data.payload.abiSize(target) }; | |
| 3333 | } else if (!data.payload.hasRuntimeBits()) { | |
| 3334 | return AbiSizeAdvanced{ .scalar = data.error_set.abiSize(target) }; | |
| 3444 | // Here we need to care whether or not the error set is *empty* or whether | |
| 3445 | // it only has *one possible value*. In the former case, it means there | |
| 3446 | // cannot possibly be an error, meaning the ABI size is equivalent to the | |
| 3447 | // payload ABI size. In the latter case, we need to account for the "tag" | |
| 3448 | // because even if both the payload type and the error set type of an | |
| 3449 | // error union have no runtime bits, an error union still has | |
| 3450 | // 1 bit of data which is whether or not the value is an error. | |
| 3451 | // Zig still uses the error code encoding at runtime, even when only 1 bit | |
| 3452 | // would suffice. This prevents coercions from needing to branch. | |
| 3453 | switch (data.error_set.errorSetCardinality()) { | |
| 3454 | .zero => return abiSizeAdvanced(data.payload, target, strat), | |
| 3455 | .one => { | |
| 3456 | if (data.payload.isNoReturn()) { | |
| 3457 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 3458 | } | |
| 3459 | }, | |
| 3460 | .many => {}, | |
| 3461 | } | |
| 3462 | const code_size = abiSize(Type.anyerror, target); | |
| 3463 | if (!data.payload.hasRuntimeBits()) { | |
| 3464 | // Same as anyerror. | |
| 3465 | return AbiSizeAdvanced{ .scalar = code_size }; | |
| 3335 | 3466 | } |
| 3336 | const code_align = abiAlignment(data.error_set, target); | |
| 3467 | const code_align = abiAlignment(Type.anyerror, target); | |
| 3337 | 3468 | const payload_align = abiAlignment(data.payload, target); |
| 3338 | const big_align = @maximum(code_align, payload_align); | |
| 3339 | 3469 | const payload_size = abiSize(data.payload, target); |
| 3340 | 3470 | |
| 3341 | 3471 | var size: u64 = 0; |
| 3342 | size += abiSize(data.error_set, target); | |
| 3343 | size = std.mem.alignForwardGeneric(u64, size, payload_align); | |
| 3344 | size += payload_size; | |
| 3345 | size = std.mem.alignForwardGeneric(u64, size, big_align); | |
| 3472 | if (code_align > payload_align) { | |
| 3473 | size += code_size; | |
| 3474 | size = std.mem.alignForwardGeneric(u64, size, payload_align); | |
| 3475 | size += payload_size; | |
| 3476 | size = std.mem.alignForwardGeneric(u64, size, code_align); | |
| 3477 | } else { | |
| 3478 | size += payload_size; | |
| 3479 | size = std.mem.alignForwardGeneric(u64, size, code_align); | |
| 3480 | size += code_size; | |
| 3481 | size = std.mem.alignForwardGeneric(u64, size, payload_align); | |
| 3482 | } | |
| 3346 | 3483 | return AbiSizeAdvanced{ .scalar = size }; |
| 3347 | 3484 | }, |
| 3348 | 3485 | } |
| ... | ... | @@ -3832,8 +3969,39 @@ pub const Type = extern union { |
| 3832 | 3969 | return ty.ptrInfo().data.@"allowzero"; |
| 3833 | 3970 | } |
| 3834 | 3971 | |
| 3972 | /// See also `isPtrLikeOptional`. | |
| 3973 | pub fn optionalReprIsPayload(ty: Type) bool { | |
| 3974 | switch (ty.tag()) { | |
| 3975 | .optional_single_const_pointer, | |
| 3976 | .optional_single_mut_pointer, | |
| 3977 | .c_const_pointer, | |
| 3978 | .c_mut_pointer, | |
| 3979 | => return true, | |
| 3980 | ||
| 3981 | .optional => { | |
| 3982 | const child_ty = ty.castTag(.optional).?.data; | |
| 3983 | switch (child_ty.zigTypeTag()) { | |
| 3984 | .Pointer => { | |
| 3985 | const info = child_ty.ptrInfo().data; | |
| 3986 | switch (info.size) { | |
| 3987 | .Slice, .C => return false, | |
| 3988 | .Many, .One => return !info.@"allowzero", | |
| 3989 | } | |
| 3990 | }, | |
| 3991 | .ErrorSet => return true, | |
| 3992 | else => return false, | |
| 3993 | } | |
| 3994 | }, | |
| 3995 | ||
| 3996 | .pointer => return ty.castTag(.pointer).?.data.size == .C, | |
| 3997 | ||
| 3998 | else => return false, | |
| 3999 | } | |
| 4000 | } | |
| 4001 | ||
| 3835 | 4002 | /// Returns true if the type is optional and would be lowered to a single pointer |
| 3836 | 4003 | /// address value, using 0 for null. Note that this returns true for C pointers. |
| 4004 | /// See also `hasOptionalRepr`. | |
| 3837 | 4005 | pub fn isPtrLikeOptional(self: Type) bool { |
| 3838 | 4006 | switch (self.tag()) { |
| 3839 | 4007 | .optional_single_const_pointer, |
| ... | ... | @@ -4166,6 +4334,35 @@ pub const Type = extern union { |
| 4166 | 4334 | }; |
| 4167 | 4335 | } |
| 4168 | 4336 | |
| 4337 | const ErrorSetCardinality = enum { zero, one, many }; | |
| 4338 | ||
| 4339 | pub fn errorSetCardinality(ty: Type) ErrorSetCardinality { | |
| 4340 | switch (ty.tag()) { | |
| 4341 | .anyerror => return .many, | |
| 4342 | .error_set_inferred => return .many, | |
| 4343 | .error_set_single => return .one, | |
| 4344 | .error_set => { | |
| 4345 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 4346 | const names = err_set_obj.names.keys(); | |
| 4347 | switch (names.len) { | |
| 4348 | 0 => return .zero, | |
| 4349 | 1 => return .one, | |
| 4350 | else => return .many, | |
| 4351 | } | |
| 4352 | }, | |
| 4353 | .error_set_merged => { | |
| 4354 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 4355 | const names = name_map.keys(); | |
| 4356 | switch (names.len) { | |
| 4357 | 0 => return .zero, | |
| 4358 | 1 => return .one, | |
| 4359 | else => return .many, | |
| 4360 | } | |
| 4361 | }, | |
| 4362 | else => unreachable, | |
| 4363 | } | |
| 4364 | } | |
| 4365 | ||
| 4169 | 4366 | /// Returns true if it is an error set that includes anyerror, false otherwise. |
| 4170 | 4367 | /// Note that the result may be a false negative if the type did not get error set |
| 4171 | 4368 | /// resolution prior to this call. |
| ... | ... | @@ -4658,16 +4855,11 @@ pub const Type = extern union { |
| 4658 | 4855 | .const_slice, |
| 4659 | 4856 | .mut_slice, |
| 4660 | 4857 | .anyopaque, |
| 4661 | .optional, | |
| 4662 | 4858 | .optional_single_mut_pointer, |
| 4663 | 4859 | .optional_single_const_pointer, |
| 4664 | 4860 | .enum_literal, |
| 4665 | 4861 | .anyerror_void_error_union, |
| 4666 | .error_union, | |
| 4667 | .error_set, | |
| 4668 | .error_set_single, | |
| 4669 | 4862 | .error_set_inferred, |
| 4670 | .error_set_merged, | |
| 4671 | 4863 | .@"opaque", |
| 4672 | 4864 | .var_args_param, |
| 4673 | 4865 | .manyptr_u8, |
| ... | ... | @@ -4696,6 +4888,52 @@ pub const Type = extern union { |
| 4696 | 4888 | .bound_fn, |
| 4697 | 4889 | => return null, |
| 4698 | 4890 | |
| 4891 | .optional => { | |
| 4892 | var buf: Payload.ElemType = undefined; | |
| 4893 | const child_ty = ty.optionalChild(&buf); | |
| 4894 | if (child_ty.isNoReturn()) { | |
| 4895 | return Value.@"null"; | |
| 4896 | } else { | |
| 4897 | return null; | |
| 4898 | } | |
| 4899 | }, | |
| 4900 | ||
| 4901 | .error_union => { | |
| 4902 | const error_ty = ty.errorUnionSet(); | |
| 4903 | switch (error_ty.errorSetCardinality()) { | |
| 4904 | .zero => { | |
| 4905 | const payload_ty = ty.errorUnionPayload(); | |
| 4906 | if (onePossibleValue(payload_ty)) |payload_val| { | |
| 4907 | _ = payload_val; | |
| 4908 | return Value.initTag(.the_only_possible_value); | |
| 4909 | } else { | |
| 4910 | return null; | |
| 4911 | } | |
| 4912 | }, | |
| 4913 | .one => { | |
| 4914 | if (ty.errorUnionPayload().isNoReturn()) { | |
| 4915 | const error_val = onePossibleValue(error_ty).?; | |
| 4916 | return error_val; | |
| 4917 | } else { | |
| 4918 | return null; | |
| 4919 | } | |
| 4920 | }, | |
| 4921 | .many => return null, | |
| 4922 | } | |
| 4923 | }, | |
| 4924 | ||
| 4925 | .error_set_single => return Value.initTag(.the_only_possible_value), | |
| 4926 | .error_set => { | |
| 4927 | const err_set_obj = ty.castTag(.error_set).?.data; | |
| 4928 | if (err_set_obj.names.count() > 1) return null; | |
| 4929 | return Value.initTag(.the_only_possible_value); | |
| 4930 | }, | |
| 4931 | .error_set_merged => { | |
| 4932 | const name_map = ty.castTag(.error_set_merged).?.data; | |
| 4933 | if (name_map.count() > 1) return null; | |
| 4934 | return Value.initTag(.the_only_possible_value); | |
| 4935 | }, | |
| 4936 | ||
| 4699 | 4937 | .@"struct" => { |
| 4700 | 4938 | const s = ty.castTag(.@"struct").?.data; |
| 4701 | 4939 | assert(s.haveFieldTypes()); |
test/behavior/error.zig+97-5| ... | ... | @@ -121,7 +121,7 @@ test "debug info for optional error set" { |
| 121 | 121 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 122 | 122 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 123 | 123 | |
| 124 | const SomeError = error{Hello}; | |
| 124 | const SomeError = error{ Hello, Hello2 }; | |
| 125 | 125 | var a_local_variable: ?SomeError = null; |
| 126 | 126 | _ = a_local_variable; |
| 127 | 127 | } |
| ... | ... | @@ -148,18 +148,46 @@ test "implicit cast to optional to error union to return result loc" { |
| 148 | 148 | //comptime S.entry(); TODO |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | test "error: fn returning empty error set can be passed as fn returning any error" { | |
| 151 | test "fn returning empty error set can be passed as fn returning any error" { | |
| 152 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | |
| 153 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 154 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 155 | ||
| 152 | 156 | entry(); |
| 153 | 157 | comptime entry(); |
| 154 | 158 | } |
| 155 | 159 | |
| 160 | test "fn returning empty error set can be passed as fn returning any error - pointer" { | |
| 161 | if (builtin.zig_backend == .stage1) return error.SkipZigTest; | |
| 162 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | |
| 163 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 164 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 165 | ||
| 166 | entryPtr(); | |
| 167 | comptime entryPtr(); | |
| 168 | } | |
| 169 | ||
| 156 | 170 | fn entry() void { |
| 157 | 171 | foo2(bar2); |
| 158 | 172 | } |
| 159 | 173 | |
| 174 | fn entryPtr() void { | |
| 175 | var ptr = &bar2; | |
| 176 | fooPtr(ptr); | |
| 177 | } | |
| 178 | ||
| 160 | 179 | fn foo2(f: fn () anyerror!void) void { |
| 161 | 180 | const x = f(); |
| 162 | x catch {}; | |
| 181 | x catch { | |
| 182 | @panic("fail"); | |
| 183 | }; | |
| 184 | } | |
| 185 | ||
| 186 | fn fooPtr(f: *const fn () anyerror!void) void { | |
| 187 | const x = f(); | |
| 188 | x catch { | |
| 189 | @panic("fail"); | |
| 190 | }; | |
| 163 | 191 | } |
| 164 | 192 | |
| 165 | 193 | fn bar2() (error{}!void) {} |
| ... | ... | @@ -239,7 +267,10 @@ fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void { |
| 239 | 267 | } |
| 240 | 268 | |
| 241 | 269 | test "comptime err to int of error set with only 1 possible value" { |
| 242 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | |
| 270 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 271 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | |
| 272 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 273 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 243 | 274 | |
| 244 | 275 | testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A)); |
| 245 | 276 | comptime testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A)); |
| ... | ... | @@ -409,9 +440,11 @@ test "return function call to error set from error union function" { |
| 409 | 440 | } |
| 410 | 441 | |
| 411 | 442 | test "optional error set is the same size as error set" { |
| 412 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | |
| 443 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 444 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 413 | 445 | |
| 414 | 446 | comptime try expect(@sizeOf(?anyerror) == @sizeOf(anyerror)); |
| 447 | comptime try expect(@alignOf(?anyerror) == @alignOf(anyerror)); | |
| 415 | 448 | const S = struct { |
| 416 | 449 | fn returnsOptErrSet() ?anyerror { |
| 417 | 450 | return null; |
| ... | ... | @@ -421,6 +454,65 @@ test "optional error set is the same size as error set" { |
| 421 | 454 | comptime try expect(S.returnsOptErrSet() == null); |
| 422 | 455 | } |
| 423 | 456 | |
| 457 | test "optional error set with only one error is the same size as bool" { | |
| 458 | if (builtin.zig_backend == .stage1) return error.SkipZigTest; | |
| 459 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 460 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 461 | ||
| 462 | const E = error{only}; | |
| 463 | comptime try expect(@sizeOf(?E) == @sizeOf(bool)); | |
| 464 | comptime try expect(@alignOf(?E) == @alignOf(bool)); | |
| 465 | const S = struct { | |
| 466 | fn gimmeNull() ?E { | |
| 467 | return null; | |
| 468 | } | |
| 469 | fn gimmeErr() ?E { | |
| 470 | return error.only; | |
| 471 | } | |
| 472 | }; | |
| 473 | try expect(S.gimmeNull() == null); | |
| 474 | try expect(error.only == S.gimmeErr().?); | |
| 475 | comptime try expect(S.gimmeNull() == null); | |
| 476 | comptime try expect(error.only == S.gimmeErr().?); | |
| 477 | } | |
| 478 | ||
| 479 | test "optional empty error set" { | |
| 480 | if (builtin.zig_backend == .stage1) return error.SkipZigTest; | |
| 481 | ||
| 482 | comptime try expect(@sizeOf(error{}!void) == @sizeOf(void)); | |
| 483 | comptime try expect(@alignOf(error{}!void) == @alignOf(void)); | |
| 484 | ||
| 485 | var x: ?error{} = undefined; | |
| 486 | if (x != null) { | |
| 487 | @compileError("test failed"); | |
| 488 | } | |
| 489 | } | |
| 490 | ||
| 491 | test "empty error set plus zero-bit payload" { | |
| 492 | if (builtin.zig_backend == .stage1) return error.SkipZigTest; | |
| 493 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 494 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 495 | ||
| 496 | comptime try expect(@sizeOf(error{}!void) == @sizeOf(void)); | |
| 497 | comptime try expect(@alignOf(error{}!void) == @alignOf(void)); | |
| 498 | ||
| 499 | var x: error{}!void = undefined; | |
| 500 | if (x) |payload| { | |
| 501 | if (payload != {}) { | |
| 502 | @compileError("test failed"); | |
| 503 | } | |
| 504 | } else |_| { | |
| 505 | @compileError("test failed"); | |
| 506 | } | |
| 507 | const S = struct { | |
| 508 | fn empty() error{}!void {} | |
| 509 | fn inferred() !void { | |
| 510 | return empty(); | |
| 511 | } | |
| 512 | }; | |
| 513 | try S.inferred(); | |
| 514 | } | |
| 515 | ||
| 424 | 516 | test "nested catch" { |
| 425 | 517 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 426 | 518 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
test/behavior/eval.zig-27| ... | ... | @@ -425,7 +425,6 @@ test "f64 at compile time is lossy" { |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | test { |
| 428 | if (builtin.zig_backend != .stage1 and builtin.os.tag == .macos) return error.SkipZigTest; | |
| 429 | 428 | comptime try expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192); |
| 430 | 429 | } |
| 431 | 430 | |
| ... | ... | @@ -573,28 +572,6 @@ test "inlined loop has array literal with elided runtime scope on first iteratio |
| 573 | 572 | } |
| 574 | 573 | } |
| 575 | 574 | |
| 576 | test "call method on bound fn referring to var instance" { | |
| 577 | if (builtin.zig_backend != .stage1) { | |
| 578 | // Let's delay solving this one; I want to try to eliminate bound functions from | |
| 579 | // the language. | |
| 580 | return error.SkipZigTest; // TODO | |
| 581 | } | |
| 582 | ||
| 583 | try expect(bound_fn() == 1237); | |
| 584 | } | |
| 585 | ||
| 586 | const SimpleStruct = struct { | |
| 587 | field: i32, | |
| 588 | ||
| 589 | fn method(self: *const SimpleStruct) i32 { | |
| 590 | return self.field + 3; | |
| 591 | } | |
| 592 | }; | |
| 593 | ||
| 594 | var simple_struct = SimpleStruct{ .field = 1234 }; | |
| 595 | ||
| 596 | const bound_fn = simple_struct.method; | |
| 597 | ||
| 598 | 575 | test "ptr to local array argument at comptime" { |
| 599 | 576 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 600 | 577 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| ... | ... | @@ -669,8 +646,6 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type { |
| 669 | 646 | } |
| 670 | 647 | |
| 671 | 648 | test "comptime function with mutable pointer is not memoized" { |
| 672 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | |
| 673 | ||
| 674 | 649 | comptime { |
| 675 | 650 | var x: i32 = 1; |
| 676 | 651 | const ptr = &x; |
| ... | ... | @@ -685,8 +660,6 @@ fn increment(value: *i32) void { |
| 685 | 660 | } |
| 686 | 661 | |
| 687 | 662 | test "const ptr to comptime mutable data is not memoized" { |
| 688 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | |
| 689 | ||
| 690 | 663 | comptime { |
| 691 | 664 | var foo = SingleFieldStruct{ .x = 1 }; |
| 692 | 665 | try expect(foo.read_x() == 1); |
test/cases/compile_errors/call method on bound fn referring to var instance.zig	 created+20| ... | ... | @@ -0,0 +1,20 @@ |
| 1 | export fn entry() void { | |
| 2 | bad(bound_fn() == 1237); | |
| 3 | } | |
| 4 | const SimpleStruct = struct { | |
| 5 | field: i32, | |
| 6 | ||
| 7 | fn method(self: *const SimpleStruct) i32 { | |
| 8 | return self.field + 3; | |
| 9 | } | |
| 10 | }; | |
| 11 | var simple_struct = SimpleStruct{ .field = 1234 }; | |
| 12 | const bound_fn = simple_struct.method; | |
| 13 | fn bad(ok: bool) void { | |
| 14 | _ = ok; | |
| 15 | } | |
| 16 | // error | |
| 17 | // target=native | |
| 18 | // backend=stage2 | |
| 19 | // | |
| 20 | // :12:18: error: unable to resolve comptime value |