authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-30 15:55:05-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-30 15:55:05-04:00
logf559ea95b1c37fd6ede8fff6ffb2d74d5c2abc4e
tree6d9714e54fa2223aa6842d8d496d5f3dadd360e8
parent0a42602418dcaf08f13b4220b6c216356f87cbfc
parent7377dce368090e3c49a15d8996cc812adadd3d43
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12686 from Vexu/stage2-fixes

Stage2 fixes

18 files changed, 528 insertions(+), 201 deletions(-)

src/AstGen.zig+22-11
......@@ -226,6 +226,8 @@ pub const ResultLoc = union(enum) {
226226 ref,
227227 /// The expression will be coerced into this type, but it will be evaluated as an rvalue.
228228 ty: Zir.Inst.Ref,
229 /// Same as `ty` but for shift operands.
230 ty_shift_operand: Zir.Inst.Ref,
229231 /// Same as `ty` but it is guaranteed that Sema will additionally perform the coercion,
230232 /// so no `as` instruction needs to be emitted.
231233 coerced_ty: Zir.Inst.Ref,
......@@ -259,7 +261,7 @@ pub const ResultLoc = union(enum) {
259261 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {
260262 switch (rl) {
261263 // In this branch there will not be any store_to_block_ptr instructions.
262 .none, .ty, .coerced_ty, .ref => return .{
264 .none, .ty, .ty_shift_operand, .coerced_ty, .ref => return .{
263265 .tag = .break_operand,
264266 .elide_store_to_block_ptr_instructions = false,
265267 },
......@@ -302,6 +304,14 @@ pub const ResultLoc = union(enum) {
302304 else => rl,
303305 };
304306 }
307
308 fn zirTag(rl: ResultLoc) Zir.Inst.Tag {
309 return switch (rl) {
310 .ty => .as_node,
311 .ty_shift_operand => .as_shift_operand,
312 else => unreachable,
313 };
314 }
305315};
306316
307317pub const align_rl: ResultLoc = .{ .ty = .u29_type };
......@@ -1385,7 +1395,7 @@ fn arrayInitExpr(
13851395 const tag: Zir.Inst.Tag = if (types.array != .none) .array_init else .array_init_anon;
13861396 return arrayInitExprInner(gz, scope, node, array_init.ast.elements, types.array, types.elem, tag);
13871397 },
1388 .ty, .coerced_ty => {
1398 .ty, .ty_shift_operand, .coerced_ty => {
13891399 const tag: Zir.Inst.Tag = if (types.array != .none) .array_init else .array_init_anon;
13901400 const result = try arrayInitExprInner(gz, scope, node, array_init.ast.elements, types.array, types.elem, tag);
13911401 return rvalue(gz, rl, result, node);
......@@ -1631,7 +1641,7 @@ fn structInitExpr(
16311641 return structInitExprRlNone(gz, scope, node, struct_init, .none, .struct_init_anon);
16321642 }
16331643 },
1634 .ty, .coerced_ty => |ty_inst| {
1644 .ty, .ty_shift_operand, .coerced_ty => |ty_inst| {
16351645 if (struct_init.ast.type_expr == 0) {
16361646 const result = try structInitExprRlNone(gz, scope, node, struct_init, ty_inst, .struct_init_anon);
16371647 return rvalue(gz, rl, result, node);
......@@ -2327,6 +2337,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
23272337 .anyframe_type,
23282338 .as,
23292339 .as_node,
2340 .as_shift_operand,
23302341 .bit_and,
23312342 .bitcast,
23322343 .bit_or,
......@@ -2497,7 +2508,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
24972508 .field_parent_ptr,
24982509 .maximum,
24992510 .minimum,
2500 .builtin_async_call,
25012511 .c_import,
25022512 .@"resume",
25032513 .@"await",
......@@ -7278,7 +7288,7 @@ fn as(
72787288) InnerError!Zir.Inst.Ref {
72797289 const dest_type = try typeExpr(gz, scope, lhs);
72807290 switch (rl) {
7281 .none, .discard, .ref, .ty, .coerced_ty => {
7291 .none, .discard, .ref, .ty, .ty_shift_operand, .coerced_ty => {
72827292 const result = try reachableExpr(gz, scope, .{ .ty = dest_type }, rhs, node);
72837293 return rvalue(gz, rl, result, node);
72847294 },
......@@ -7959,7 +7969,8 @@ fn builtinCall(
79597969 return rvalue(gz, rl, result, node);
79607970 },
79617971 .async_call => {
7962 const result = try gz.addPlNode(.builtin_async_call, node, Zir.Inst.AsyncCall{
7972 const result = try gz.addExtendedPayload(.builtin_async_call, Zir.Inst.AsyncCall{
7973 .node = gz.nodeIndexToRelative(node),
79637974 .frame_buffer = try expr(gz, scope, .none, params[0]),
79647975 .result_ptr = try expr(gz, scope, .none, params[1]),
79657976 .fn_ptr = try expr(gz, scope, .none, params[2]),
......@@ -8178,7 +8189,7 @@ fn shiftOp(
81788189) InnerError!Zir.Inst.Ref {
81798190 const lhs = try expr(gz, scope, .none, lhs_node);
81808191 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);
8181 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, rhs_node);
8192 const rhs = try expr(gz, scope, .{ .ty_shift_operand = log2_int_type }, rhs_node);
81828193 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
81838194 .lhs = lhs,
81848195 .rhs = rhs,
......@@ -9409,7 +9420,7 @@ fn rvalue(
94099420 }
94109421 return indexToRef(gop.value_ptr.*);
94119422 },
9412 .ty => |ty_inst| {
9423 .ty, .ty_shift_operand => |ty_inst| {
94139424 // Quickly eliminate some common, unnecessary type coercion.
94149425 const as_ty = @as(u64, @enumToInt(Zir.Inst.Ref.type_type)) << 32;
94159426 const as_comptime_int = @as(u64, @enumToInt(Zir.Inst.Ref.comptime_int_type)) << 32;
......@@ -9470,7 +9481,7 @@ fn rvalue(
94709481 => return result, // type of result is already correct
94719482
94729483 // Need an explicit type coercion instruction.
9473 else => return gz.addPlNode(.as_node, src_node, Zir.Inst.As{
9484 else => return gz.addPlNode(rl.zirTag(), src_node, Zir.Inst.As{
94749485 .dest_type = ty_inst,
94759486 .operand = result,
94769487 }),
......@@ -10350,7 +10361,7 @@ const GenZir = struct {
1035010361 // we emit ZIR for the block break instructions to have the result values,
1035110362 // and then rvalue() on that to pass the value to the result location.
1035210363 switch (parent_rl) {
10353 .ty, .coerced_ty => |ty_inst| {
10364 .ty, .ty_shift_operand, .coerced_ty => |ty_inst| {
1035410365 gz.rl_ty_inst = ty_inst;
1035510366 gz.break_result_loc = parent_rl;
1035610367 },
......@@ -11506,7 +11517,7 @@ const GenZir = struct {
1150611517 fn addRet(gz: *GenZir, rl: ResultLoc, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {
1150711518 switch (rl) {
1150811519 .ptr => |ret_ptr| _ = try gz.addUnNode(.ret_load, ret_ptr, node),
11509 .ty => _ = try gz.addUnNode(.ret_node, operand, node),
11520 .ty, .ty_shift_operand => _ = try gz.addUnNode(.ret_node, operand, node),
1151011521 else => unreachable,
1151111522 }
1151211523 }
src/Autodoc.zig+1-1
......@@ -1888,7 +1888,7 @@ fn walkInstruction(
18881888 .expr = .{ .typeInfo = operand_index },
18891889 };
18901890 },
1891 .as_node => {
1891 .as_node, .as_shift_operand => {
18921892 const pl_node = data[inst_index].pl_node;
18931893 const extra = file.zir.extraData(Zir.Inst.As, pl_node.payload_index);
18941894 const dest_type_walk = try self.walkRef(
src/Compilation.zig+19-17
......@@ -4766,6 +4766,24 @@ pub fn dump_argv(argv: []const []const u8) void {
47664766 std.debug.print("{s}\n", .{argv[argv.len - 1]});
47674767}
47684768
4769pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
4770 const use_stage1 = build_options.have_stage1 and comp.bin_file.options.use_stage1;
4771 if (use_stage1) return .stage1;
4772 if (build_options.have_llvm and comp.bin_file.options.use_llvm) return .stage2_llvm;
4773 const target = comp.bin_file.options.target;
4774 if (target.ofmt == .c) return .stage2_c;
4775 return switch (target.cpu.arch) {
4776 .wasm32, .wasm64 => std.builtin.CompilerBackend.stage2_wasm,
4777 .arm, .armeb, .thumb, .thumbeb => .stage2_arm,
4778 .x86_64 => .stage2_x86_64,
4779 .i386 => .stage2_x86,
4780 .aarch64, .aarch64_be, .aarch64_32 => .stage2_aarch64,
4781 .riscv64 => .stage2_riscv64,
4782 .sparc64 => .stage2_sparc64,
4783 else => .other,
4784 };
4785}
4786
47694787pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
47704788 const tracy_trace = trace(@src());
47714789 defer tracy_trace.end();
......@@ -4775,23 +4793,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca
47754793
47764794 const target = comp.getTarget();
47774795 const generic_arch_name = target.cpu.arch.genericName();
4778 const use_stage1 = build_options.have_stage1 and comp.bin_file.options.use_stage1;
4779
4780 const zig_backend: std.builtin.CompilerBackend = blk: {
4781 if (use_stage1) break :blk .stage1;
4782 if (build_options.have_llvm and comp.bin_file.options.use_llvm) break :blk .stage2_llvm;
4783 if (target.ofmt == .c) break :blk .stage2_c;
4784 break :blk switch (target.cpu.arch) {
4785 .wasm32, .wasm64 => std.builtin.CompilerBackend.stage2_wasm,
4786 .arm, .armeb, .thumb, .thumbeb => .stage2_arm,
4787 .x86_64 => .stage2_x86_64,
4788 .i386 => .stage2_x86,
4789 .aarch64, .aarch64_be, .aarch64_32 => .stage2_aarch64,
4790 .riscv64 => .stage2_riscv64,
4791 .sparc64 => .stage2_sparc64,
4792 else => .other,
4793 };
4794 };
4796 const zig_backend = comp.getZigBackend();
47954797
47964798 @setEvalBranchQuota(4000);
47974799 try buffer.writer().print(
src/Sema.zig+134-39
......@@ -712,6 +712,7 @@ fn analyzeBodyInner(
712712 .vector_type => try sema.zirVectorType(block, inst),
713713 .as => try sema.zirAs(block, inst),
714714 .as_node => try sema.zirAsNode(block, inst),
715 .as_shift_operand => try sema.zirAsShiftOperand(block, inst),
715716 .bit_and => try sema.zirBitwise(block, inst, .bit_and),
716717 .bit_not => try sema.zirBitNot(block, inst),
717718 .bit_or => try sema.zirBitwise(block, inst, .bit_or),
......@@ -848,7 +849,6 @@ fn analyzeBodyInner(
848849 .mul_add => try sema.zirMulAdd(block, inst),
849850 .builtin_call => try sema.zirBuiltinCall(block, inst),
850851 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),
851 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
852852 .@"resume" => try sema.zirResume(block, inst),
853853 .@"await" => try sema.zirAwait(block, inst),
854854 .array_base_ptr => try sema.zirArrayBasePtr(block, inst),
......@@ -956,6 +956,7 @@ fn analyzeBodyInner(
956956 .error_to_int => try sema.zirErrorToInt( block, extended),
957957 .int_to_error => try sema.zirIntToError( block, extended),
958958 .reify => try sema.zirReify( block, extended, inst),
959 .builtin_async_call => try sema.zirBuiltinAsyncCall( block, extended),
959960 // zig fmt: on
960961 .fence => {
961962 try sema.zirFence(block, extended);
......@@ -6152,9 +6153,30 @@ fn analyzeCall(
61526153 if (ensure_result_used) {
61536154 try sema.ensureResultUsed(block, result, call_src);
61546155 }
6156 if (call_tag == .call_always_tail) {
6157 return sema.handleTailCall(block, call_src, func_ty, result);
6158 }
61556159 return result;
61566160}
61576161
6162fn handleTailCall(sema: *Sema, block: *Block, call_src: LazySrcLoc, func_ty: Type, result: Air.Inst.Ref) !Air.Inst.Ref {
6163 const target = sema.mod.getTarget();
6164 const backend = sema.mod.comp.getZigBackend();
6165 if (!target_util.supportsTailCall(target, backend)) {
6166 return sema.fail(block, call_src, "unable to perform tail call: compiler backend '{s}' does not support tail calls on target architecture '{s}' with the selected CPU feature flags", .{
6167 @tagName(backend), @tagName(target.cpu.arch),
6168 });
6169 }
6170 const func_decl = sema.mod.declPtr(sema.owner_func.?.owner_decl);
6171 if (!func_ty.eql(func_decl.ty, sema.mod)) {
6172 return sema.fail(block, call_src, "unable to perform tail call: type of function being called '{}' does not match type of calling function '{}'", .{
6173 func_ty.fmt(sema.mod), func_decl.ty.fmt(sema.mod),
6174 });
6175 }
6176 _ = try block.addUnOp(.ret, result);
6177 return Air.Inst.Ref.unreachable_value;
6178}
6179
61586180fn analyzeInlineCallArg(
61596181 sema: *Sema,
61606182 arg_block: *Block,
......@@ -6670,7 +6692,8 @@ fn instantiateGenericCall(
66706692 try sema.requireFunctionBlock(block, call_src);
66716693
66726694 const comptime_args = callee.comptime_args.?;
6673 const new_fn_info = mod.declPtr(callee.owner_decl).ty.fnInfo();
6695 const func_ty = mod.declPtr(callee.owner_decl).ty;
6696 const new_fn_info = func_ty.fnInfo();
66746697 const runtime_args_len = @intCast(u32, new_fn_info.param_types.len);
66756698 const runtime_args = try sema.arena.alloc(Air.Inst.Ref, runtime_args_len);
66766699 {
......@@ -6717,7 +6740,7 @@ fn instantiateGenericCall(
67176740
67186741 try sema.air_extra.ensureUnusedCapacity(sema.gpa, @typeInfo(Air.Call).Struct.fields.len +
67196742 runtime_args_len);
6720 const func_inst = try block.addInst(.{
6743 const result = try block.addInst(.{
67216744 .tag = call_tag,
67226745 .data = .{ .pl_op = .{
67236746 .operand = callee_inst,
......@@ -6729,9 +6752,12 @@ fn instantiateGenericCall(
67296752 sema.appendRefsAssumeCapacity(runtime_args);
67306753
67316754 if (ensure_result_used) {
6732 try sema.ensureResultUsed(block, func_inst, call_src);
6755 try sema.ensureResultUsed(block, result, call_src);
67336756 }
6734 return func_inst;
6757 if (call_tag == .call_always_tail) {
6758 return sema.handleTailCall(block, call_src, func_ty, result);
6759 }
6760 return result;
67356761}
67366762
67376763fn emitDbgInline(
......@@ -8177,8 +8203,22 @@ fn zirParam(
81778203 .is_comptime = comptime_syntax,
81788204 .name = param_name,
81798205 });
8180 const result = try sema.addConstant(param_ty, Value.initTag(.generic_poison));
8181 try sema.inst_map.putNoClobber(sema.gpa, inst, result);
8206
8207 if (is_comptime) {
8208 // If this is a comptime parameter we can add a constant generic_poison
8209 // since this is also a generic parameter.
8210 const result = try sema.addConstant(param_ty, Value.initTag(.generic_poison));
8211 try sema.inst_map.putNoClobber(sema.gpa, inst, result);
8212 } else {
8213 // Otherwise we need a dummy runtime instruction.
8214 const result_index = @intCast(Air.Inst.Index, sema.air_instructions.len);
8215 try sema.air_instructions.append(sema.gpa, .{
8216 .tag = .alloc,
8217 .data = .{ .ty = param_ty },
8218 });
8219 const result = Air.indexToRef(result_index);
8220 try sema.inst_map.putNoClobber(sema.gpa, inst, result);
8221 }
81828222}
81838223
81848224fn zirParamAnytype(
......@@ -8225,7 +8265,7 @@ fn zirAs(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst
82258265 defer tracy.end();
82268266
82278267 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
8228 return sema.analyzeAs(block, sema.src, bin_inst.lhs, bin_inst.rhs);
8268 return sema.analyzeAs(block, sema.src, bin_inst.lhs, bin_inst.rhs, false);
82298269}
82308270
82318271fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -8235,7 +8275,17 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
82358275 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82368276 const src = inst_data.src();
82378277 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
8238 return sema.analyzeAs(block, src, extra.dest_type, extra.operand);
8278 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);
8279}
8280
8281fn zirAsShiftOperand(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8282 const tracy = trace(@src());
8283 defer tracy.end();
8284
8285 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8286 const src = inst_data.src();
8287 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
8288 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, true);
82398289}
82408290
82418291fn analyzeAs(
......@@ -8244,6 +8294,7 @@ fn analyzeAs(
82448294 src: LazySrcLoc,
82458295 zir_dest_type: Zir.Inst.Ref,
82468296 zir_operand: Zir.Inst.Ref,
8297 no_cast_to_comptime_int: bool,
82478298) CompileError!Air.Inst.Ref {
82488299 const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
82498300 sema.code.instructions.items(.tag)[ptr_index] == .ret_type
......@@ -8255,7 +8306,7 @@ fn analyzeAs(
82558306 if (dest_ty.zigTypeTag() == .NoReturn) {
82568307 return sema.fail(block, src, "cannot cast to noreturn", .{});
82578308 }
8258 return sema.coerceExtra(block, dest_ty, operand, src, true, is_ret) catch |err| switch (err) {
8309 return sema.coerceExtra(block, dest_ty, operand, src, .{ .is_ret = is_ret, .no_cast_to_comptime_int = no_cast_to_comptime_int }) catch |err| switch (err) {
82598310 error.NotCoercible => unreachable,
82608311 else => |e| return e,
82618312 };
......@@ -10459,7 +10510,12 @@ fn zirShl(
1045910510
1046010511 const runtime_src = if (maybe_lhs_val) |lhs_val| rs: {
1046110512 if (lhs_val.isUndef()) return sema.addConstUndef(lhs_ty);
10462 const rhs_val = maybe_rhs_val orelse break :rs rhs_src;
10513 const rhs_val = maybe_rhs_val orelse {
10514 if (scalar_ty.zigTypeTag() == .ComptimeInt) {
10515 return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be a comptime known", .{});
10516 }
10517 break :rs rhs_src;
10518 };
1046310519
1046410520 const val = switch (air_tag) {
1046510521 .shl_exact => val: {
......@@ -10583,7 +10639,10 @@ fn zirShr(
1058310639 const target = sema.mod.getTarget();
1058410640 const scalar_ty = lhs_ty.scalarType();
1058510641
10586 const runtime_src = if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| rs: {
10642 const maybe_lhs_val = try sema.resolveMaybeUndefVal(block, lhs_src, lhs);
10643 const maybe_rhs_val = try sema.resolveMaybeUndefVal(block, rhs_src, rhs);
10644
10645 const runtime_src = if (maybe_rhs_val) |rhs_val| rs: {
1058710646 if (rhs_val.isUndef()) {
1058810647 return sema.addConstUndef(lhs_ty);
1058910648 }
......@@ -10615,7 +10674,7 @@ fn zirShr(
1061510674 });
1061610675 }
1061710676 }
10618 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
10677 if (maybe_lhs_val) |lhs_val| {
1061910678 if (lhs_val.isUndef()) {
1062010679 return sema.addConstUndef(lhs_ty);
1062110680 }
......@@ -10633,6 +10692,10 @@ fn zirShr(
1063310692 }
1063410693 } else rhs_src;
1063510694
10695 if (maybe_rhs_val == null and scalar_ty.zigTypeTag() == .ComptimeInt) {
10696 return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be a comptime known", .{});
10697 }
10698
1063610699 try sema.requireRuntimeBlock(block, src, runtime_src);
1063710700 const result = try block.addBinOp(air_tag, lhs, rhs);
1063810701 if (block.wantSafety()) {
......@@ -15353,7 +15416,7 @@ fn analyzeRet(
1535315416 if (sema.fn_ret_ty.zigTypeTag() == .ErrorUnion) {
1535415417 try sema.addToInferredErrorSet(uncasted_operand);
1535515418 }
15356 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, true, true) catch |err| switch (err) {
15419 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, .{ .is_ret = true }) catch |err| switch (err) {
1535715420 error.NotCoercible => unreachable,
1535815421 else => |e| return e,
1535915422 };
......@@ -19262,7 +19325,7 @@ fn resolveCallOptions(
1926219325 return wanted_modifier;
1926319326 },
1926419327 // These can be upgraded to comptime. nosuspend bit can be safely ignored.
19265 .always_tail, .always_inline, .compile_time => {
19328 .always_inline, .compile_time => {
1926619329 _ = (try sema.resolveDefinedValue(block, func_src, func)) orelse {
1926719330 return sema.fail(block, func_src, "modifier '{s}' requires a comptime-known function", .{@tagName(wanted_modifier)});
1926819331 };
......@@ -19272,6 +19335,12 @@ fn resolveCallOptions(
1927219335 }
1927319336 return wanted_modifier;
1927419337 },
19338 .always_tail => {
19339 if (is_comptime) {
19340 return .compile_time;
19341 }
19342 return wanted_modifier;
19343 },
1927519344 .async_kw => {
1927619345 if (is_nosuspend) {
1927719346 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used inside nosuspend block", .{});
......@@ -19614,9 +19683,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1961419683 });
1961519684}
1961619685
19617fn zirBuiltinAsyncCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
19618 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
19619 const src = inst_data.src();
19686fn zirBuiltinAsyncCall(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
19687 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
19688 const src = LazySrcLoc.nodeOffset(extra.node);
1962019689 return sema.failWithUseOfAsync(block, src);
1962119690}
1962219691
......@@ -21871,6 +21940,7 @@ fn unionFieldPtr(
2187121940 .mutable = union_ptr_ty.ptrIsMutable(),
2187221941 .@"addrspace" = union_ptr_ty.ptrAddressSpace(),
2187321942 });
21943 const enum_field_index = @intCast(u32, union_obj.tag_ty.enumFieldIndex(field_name).?);
2187421944
2187521945 if (initializing and field.ty.zigTypeTag() == .NoReturn) {
2187621946 const msg = msg: {
......@@ -21892,11 +21962,10 @@ fn unionFieldPtr(
2189221962 if (union_val.isUndef()) {
2189321963 return sema.failWithUseOfUndef(block, src);
2189421964 }
21895 const enum_field_index = union_obj.tag_ty.enumFieldIndex(field_name).?;
2189621965 const tag_and_val = union_val.castTag(.@"union").?.data;
2189721966 var field_tag_buf: Value.Payload.U32 = .{
2189821967 .base = .{ .tag = .enum_field_index },
21899 .data = @intCast(u32, enum_field_index),
21968 .data = enum_field_index,
2190021969 };
2190121970 const field_tag = Value.initPayload(&field_tag_buf.base);
2190221971 const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty, sema.mod);
......@@ -21928,7 +21997,7 @@ fn unionFieldPtr(
2192821997 if (!initializing and union_obj.layout == .Auto and block.wantSafety() and
2192921998 union_ty.unionTagTypeSafety() != null and union_obj.fields.count() > 1)
2193021999 {
21931 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
22000 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
2193222001 const wanted_tag = try sema.addConstant(union_obj.tag_ty, wanted_tag_val);
2193322002 // TODO would it be better if get_union_tag supported pointers to unions?
2193422003 const union_val = try block.addTyOp(.load, union_ty, union_ptr);
......@@ -21958,15 +22027,15 @@ fn unionFieldVal(
2195822027 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
2195922028 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_name_src);
2196022029 const field = union_obj.fields.values()[field_index];
22030 const enum_field_index = @intCast(u32, union_obj.tag_ty.enumFieldIndex(field_name).?);
2196122031
2196222032 if (try sema.resolveMaybeUndefVal(block, src, union_byval)) |union_val| {
2196322033 if (union_val.isUndef()) return sema.addConstUndef(field.ty);
2196422034
2196522035 const tag_and_val = union_val.castTag(.@"union").?.data;
21966 const enum_field_index = union_obj.tag_ty.enumFieldIndex(field_name).?;
2196722036 var field_tag_buf: Value.Payload.U32 = .{
2196822037 .base = .{ .tag = .enum_field_index },
21969 .data = @intCast(u32, enum_field_index),
22038 .data = enum_field_index,
2197022039 };
2197122040 const field_tag = Value.initPayload(&field_tag_buf.base);
2197222041 const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty, sema.mod);
......@@ -22002,7 +22071,7 @@ fn unionFieldVal(
2200222071 if (union_obj.layout == .Auto and block.wantSafety() and
2200322072 union_ty.unionTagTypeSafety() != null and union_obj.fields.count() > 1)
2200422073 {
22005 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
22074 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
2200622075 const wanted_tag = try sema.addConstant(union_obj.tag_ty, wanted_tag_val);
2200722076 const active_tag = try block.addTyOp(.get_union_tag, union_obj.tag_ty, union_byval);
2200822077 const ok = try block.addBinOp(.cmp_eq, active_tag, wanted_tag);
......@@ -22504,7 +22573,7 @@ fn coerce(
2250422573 inst: Air.Inst.Ref,
2250522574 inst_src: LazySrcLoc,
2250622575) CompileError!Air.Inst.Ref {
22507 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, true, false) catch |err| switch (err) {
22576 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, .{}) catch |err| switch (err) {
2250822577 error.NotCoercible => unreachable,
2250922578 else => |e| return e,
2251022579 };
......@@ -22516,14 +22585,22 @@ const CoersionError = CompileError || error{
2251622585 NotCoercible,
2251722586};
2251822587
22588const CoerceOpts = struct {
22589 /// Should coerceExtra emit error messages.
22590 report_err: bool = true,
22591 /// Ignored if `report_err == false`.
22592 is_ret: bool = false,
22593 /// Should coercion to comptime_int ermit an error message.
22594 no_cast_to_comptime_int: bool = false,
22595};
22596
2251922597fn coerceExtra(
2252022598 sema: *Sema,
2252122599 block: *Block,
2252222600 dest_ty_unresolved: Type,
2252322601 inst: Air.Inst.Ref,
2252422602 inst_src: LazySrcLoc,
22525 report_err: bool,
22526 is_ret: bool,
22603 opts: CoerceOpts,
2252722604) CoersionError!Air.Inst.Ref {
2252822605 switch (dest_ty_unresolved.tag()) {
2252922606 .var_args_param => return sema.coerceVarArgParam(block, inst, inst_src),
......@@ -22575,7 +22652,7 @@ fn coerceExtra(
2257522652
2257622653 // T to ?T
2257722654 const child_type = try dest_ty.optionalChildAlloc(sema.arena);
22578 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, false, is_ret) catch |err| switch (err) {
22655 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2257922656 error.NotCoercible => {
2258022657 if (in_memory_result == .no_match) {
2258122658 // Try to give more useful notes
......@@ -22691,7 +22768,7 @@ fn coerceExtra(
2269122768 return sema.addConstant(dest_ty, Value.@"null");
2269222769 },
2269322770 .ComptimeInt => {
22694 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, false, is_ret) catch |err| switch (err) {
22771 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2269522772 error.NotCoercible => break :pointer,
2269622773 else => |e| return e,
2269722774 };
......@@ -22702,7 +22779,7 @@ fn coerceExtra(
2270222779 .signed => Type.isize,
2270322780 .unsigned => Type.usize,
2270422781 };
22705 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, false, is_ret) catch |err| switch (err) {
22782 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2270622783 error.NotCoercible => {
2270722784 // Try to give more useful notes
2270822785 in_memory_result = try sema.coerceInMemoryAllowed(block, ptr_size_ty, inst_ty, false, target, dest_ty_src, inst_src);
......@@ -22828,7 +22905,13 @@ fn coerceExtra(
2282822905 },
2282922906 .Int, .ComptimeInt => switch (inst_ty.zigTypeTag()) {
2283022907 .Float, .ComptimeFloat => float: {
22831 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse break :float;
22908 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
22909 if (dest_ty.zigTypeTag() == .ComptimeInt) {
22910 if (!opts.report_err) return error.NotCoercible;
22911 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime known");
22912 }
22913 break :float;
22914 };
2283222915
2283322916 if (val.floatHasFraction()) {
2283422917 return sema.fail(
......@@ -22845,11 +22928,16 @@ fn coerceExtra(
2284522928 if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
2284622929 // comptime known integer to other number
2284722930 if (!(try sema.intFitsInType(block, inst_src, val, dest_ty, null))) {
22848 if (!report_err) return error.NotCoercible;
22931 if (!opts.report_err) return error.NotCoercible;
2284922932 return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(sema.mod), val.fmtValue(inst_ty, sema.mod) });
2285022933 }
2285122934 return try sema.addConstant(dest_ty, val);
2285222935 }
22936 if (dest_ty.zigTypeTag() == .ComptimeInt) {
22937 if (!opts.report_err) return error.NotCoercible;
22938 if (opts.no_cast_to_comptime_int) return inst;
22939 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime known");
22940 }
2285322941
2285422942 // integer widening
2285522943 const dst_info = dest_ty.intInfo(target);
......@@ -22886,6 +22974,7 @@ fn coerceExtra(
2288622974 }
2288722975 return try sema.addConstant(dest_ty, result_val);
2288822976 } else if (dest_ty.zigTypeTag() == .ComptimeFloat) {
22977 if (!opts.report_err) return error.NotCoercible;
2288922978 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime known");
2289022979 }
2289122980
......@@ -22898,7 +22987,13 @@ fn coerceExtra(
2289822987 }
2289922988 },
2290022989 .Int, .ComptimeInt => int: {
22901 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse break :int;
22990 const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
22991 if (dest_ty.zigTypeTag() == .ComptimeFloat) {
22992 if (!opts.report_err) return error.NotCoercible;
22993 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime known");
22994 }
22995 break :int;
22996 };
2290222997 const result_val = try val.intToFloat(sema.arena, inst_ty, dest_ty, target);
2290322998 // TODO implement this compile error
2290422999 //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);
......@@ -23050,9 +23145,9 @@ fn coerceExtra(
2305023145 return sema.addConstUndef(dest_ty);
2305123146 }
2305223147
23053 if (!report_err) return error.NotCoercible;
23148 if (!opts.report_err) return error.NotCoercible;
2305423149
23055 if (is_ret and dest_ty.zigTypeTag() == .NoReturn) {
23150 if (opts.is_ret and dest_ty.zigTypeTag() == .NoReturn) {
2305623151 const msg = msg: {
2305723152 const msg = try sema.errMsg(block, inst_src, "function declared 'noreturn' returns", .{});
2305823153 errdefer msg.destroy(sema.gpa);
......@@ -23089,7 +23184,7 @@ fn coerceExtra(
2308923184 try in_memory_result.report(sema, block, inst_src, msg);
2309023185
2309123186 // Add notes about function return type
23092 if (is_ret and sema.mod.test_functions.get(sema.func.?.owner_decl) == null) {
23187 if (opts.is_ret and sema.mod.test_functions.get(sema.func.?.owner_decl) == null) {
2309323188 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = 0 };
2309423189 const src_decl = sema.mod.declPtr(sema.func.?.owner_decl);
2309523190 if (inst_ty.isError() and !dest_ty.isError()) {
......@@ -24050,7 +24145,7 @@ fn storePtr2(
2405024145 // https://github.com/ziglang/zig/issues/11154
2405124146 if (sema.obtainBitCastedVectorPtr(ptr)) |vector_ptr| {
2405224147 const vector_ty = sema.typeOf(vector_ptr).childType();
24053 const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
24148 const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
2405424149 error.NotCoercible => unreachable,
2405524150 else => |e| return e,
2405624151 };
......@@ -24058,7 +24153,7 @@ fn storePtr2(
2405824153 return;
2405924154 }
2406024155
24061 const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
24156 const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
2406224157 error.NotCoercible => unreachable,
2406324158 else => |e| return e,
2406424159 };
......@@ -26793,7 +26888,7 @@ fn wrapErrorUnionPayload(
2679326888 inst_src: LazySrcLoc,
2679426889) !Air.Inst.Ref {
2679526890 const dest_payload_ty = dest_ty.errorUnionPayload();
26796 const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, false, false);
26891 const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, .{ .report_err = false });
2679726892 if (try sema.resolveMaybeUndefVal(block, inst_src, coerced)) |val| {
2679826893 return sema.addConstant(dest_ty, try Value.Tag.eu_payload.create(sema.arena, val));
2679926894 }
src/Zir.zig+9-6
......@@ -242,6 +242,8 @@ pub const Inst = struct {
242242 /// Type coercion to the function's return type.
243243 /// Uses the `pl_node` field. Payload is `As`. AST node could be many things.
244244 as_node,
245 /// Same as `as_node` but ignores runtime to comptime int error.
246 as_shift_operand,
245247 /// Bitwise AND. `&`
246248 bit_and,
247249 /// Reinterpret the memory representation of a value as a different type.
......@@ -942,9 +944,6 @@ pub const Inst = struct {
942944 /// Implements the `@maximum` builtin.
943945 /// Uses the `pl_node` union field with payload `Bin`
944946 maximum,
945 /// Implements the `@asyncCall` builtin.
946 /// Uses the `pl_node` union field with payload `AsyncCall`.
947 builtin_async_call,
948947 /// Implements the `@cImport` builtin.
949948 /// Uses the `pl_node` union field with payload `Block`.
950949 c_import,
......@@ -1029,6 +1028,7 @@ pub const Inst = struct {
10291028 .anyframe_type,
10301029 .as,
10311030 .as_node,
1031 .as_shift_operand,
10321032 .bit_and,
10331033 .bitcast,
10341034 .bit_or,
......@@ -1231,7 +1231,6 @@ pub const Inst = struct {
12311231 .memcpy,
12321232 .memset,
12331233 .minimum,
1234 .builtin_async_call,
12351234 .c_import,
12361235 .@"resume",
12371236 .@"await",
......@@ -1339,6 +1338,7 @@ pub const Inst = struct {
13391338 .anyframe_type,
13401339 .as,
13411340 .as_node,
1341 .as_shift_operand,
13421342 .bit_and,
13431343 .bitcast,
13441344 .bit_or,
......@@ -1513,7 +1513,6 @@ pub const Inst = struct {
15131513 .field_parent_ptr,
15141514 .maximum,
15151515 .minimum,
1516 .builtin_async_call,
15171516 .c_import,
15181517 .@"resume",
15191518 .@"await",
......@@ -1577,6 +1576,7 @@ pub const Inst = struct {
15771576 .anyframe_type = .un_node,
15781577 .as = .bin,
15791578 .as_node = .pl_node,
1579 .as_shift_operand = .pl_node,
15801580 .bit_and = .pl_node,
15811581 .bitcast = .pl_node,
15821582 .bit_not = .un_node,
......@@ -1801,7 +1801,6 @@ pub const Inst = struct {
18011801 .memcpy = .pl_node,
18021802 .memset = .pl_node,
18031803 .minimum = .pl_node,
1804 .builtin_async_call = .pl_node,
18051804 .c_import = .pl_node,
18061805
18071806 .alloc = .un_node,
......@@ -1972,6 +1971,9 @@ pub const Inst = struct {
19721971 /// `operand` is payload index to `UnNode`.
19731972 /// `small` contains `NameStrategy
19741973 reify,
1974 /// Implements the `@asyncCall` builtin.
1975 /// `operand` is payload index to `AsyncCall`.
1976 builtin_async_call,
19751977
19761978 pub const InstData = struct {
19771979 opcode: Extended,
......@@ -3454,6 +3456,7 @@ pub const Inst = struct {
34543456 };
34553457
34563458 pub const AsyncCall = struct {
3459 node: i32,
34573460 frame_buffer: Ref,
34583461 result_ptr: Ref,
34593462 fn_ptr: Ref,
src/codegen/llvm.zig+127-4
......@@ -177,6 +177,116 @@ pub fn targetTriple(allocator: Allocator, target: std.Target) ![:0]u8 {
177177 return llvm_triple.toOwnedSliceSentinel(0);
178178}
179179
180pub fn targetOs(os_tag: std.Target.Os.Tag) llvm.OSType {
181 return switch (os_tag) {
182 .freestanding, .other, .opencl, .glsl450, .vulkan, .plan9 => .UnknownOS,
183 .windows, .uefi => .Win32,
184 .ananas => .Ananas,
185 .cloudabi => .CloudABI,
186 .dragonfly => .DragonFly,
187 .freebsd => .FreeBSD,
188 .fuchsia => .Fuchsia,
189 .ios => .IOS,
190 .kfreebsd => .KFreeBSD,
191 .linux => .Linux,
192 .lv2 => .Lv2,
193 .macos => .MacOSX,
194 .netbsd => .NetBSD,
195 .openbsd => .OpenBSD,
196 .solaris => .Solaris,
197 .zos => .ZOS,
198 .haiku => .Haiku,
199 .minix => .Minix,
200 .rtems => .RTEMS,
201 .nacl => .NaCl,
202 .aix => .AIX,
203 .cuda => .CUDA,
204 .nvcl => .NVCL,
205 .amdhsa => .AMDHSA,
206 .ps4 => .PS4,
207 .elfiamcu => .ELFIAMCU,
208 .tvos => .TvOS,
209 .watchos => .WatchOS,
210 .mesa3d => .Mesa3D,
211 .contiki => .Contiki,
212 .amdpal => .AMDPAL,
213 .hermit => .HermitCore,
214 .hurd => .Hurd,
215 .wasi => .WASI,
216 .emscripten => .Emscripten,
217 };
218}
219
220pub fn targetArch(arch_tag: std.Target.Cpu.Arch) llvm.ArchType {
221 return switch (arch_tag) {
222 .arm => .arm,
223 .armeb => .armeb,
224 .aarch64 => .aarch64,
225 .aarch64_be => .aarch64_be,
226 .aarch64_32 => .aarch64_32,
227 .arc => .arc,
228 .avr => .avr,
229 .bpfel => .bpfel,
230 .bpfeb => .bpfeb,
231 .csky => .csky,
232 .hexagon => .hexagon,
233 .m68k => .m68k,
234 .mips => .mips,
235 .mipsel => .mipsel,
236 .mips64 => .mips64,
237 .mips64el => .mips64el,
238 .msp430 => .msp430,
239 .powerpc => .ppc,
240 .powerpcle => .ppcle,
241 .powerpc64 => .ppc64,
242 .powerpc64le => .ppc64le,
243 .r600 => .r600,
244 .amdgcn => .amdgcn,
245 .riscv32 => .riscv32,
246 .riscv64 => .riscv64,
247 .sparc => .sparc,
248 .sparc64 => .sparcv9, // In LLVM, sparc64 == sparcv9.
249 .sparcel => .sparcel,
250 .s390x => .systemz,
251 .tce => .tce,
252 .tcele => .tcele,
253 .thumb => .thumb,
254 .thumbeb => .thumbeb,
255 .i386 => .x86,
256 .x86_64 => .x86_64,
257 .xcore => .xcore,
258 .nvptx => .nvptx,
259 .nvptx64 => .nvptx64,
260 .le32 => .le32,
261 .le64 => .le64,
262 .amdil => .amdil,
263 .amdil64 => .amdil64,
264 .hsail => .hsail,
265 .hsail64 => .hsail64,
266 .spir => .spir,
267 .spir64 => .spir64,
268 .kalimba => .kalimba,
269 .shave => .shave,
270 .lanai => .lanai,
271 .wasm32 => .wasm32,
272 .wasm64 => .wasm64,
273 .renderscript32 => .renderscript32,
274 .renderscript64 => .renderscript64,
275 .ve => .ve,
276 .spu_2, .spirv32, .spirv64 => .UnknownArch,
277 };
278}
279
280pub fn supportsTailCall(target: std.Target) bool {
281 switch (target.cpu.arch) {
282 .wasm32, .wasm64 => return std.Target.wasm.featureSetHas(target.cpu.features, .tail_call),
283 // Although these ISAs support tail calls, LLVM does not support tail calls on them.
284 .mips, .mipsel, .mips64, .mips64el => return false,
285 .powerpc, .powerpcle, .powerpc64, .powerpc64le => return false,
286 else => return true,
287 }
288}
289
180290pub const Object = struct {
181291 gpa: Allocator,
182292 module: *Module,
......@@ -4522,7 +4632,7 @@ pub const FuncGen = struct {
45224632 "",
45234633 );
45244634
4525 if (return_type.isNoReturn()) {
4635 if (return_type.isNoReturn() and attr != .AlwaysTail) {
45264636 _ = self.builder.buildUnreachable();
45274637 return null;
45284638 }
......@@ -8527,12 +8637,26 @@ pub const FuncGen = struct {
85278637 const union_llvm_ty = try self.dg.lowerType(union_ty);
85288638 const target = self.dg.module.getTarget();
85298639 const layout = union_ty.unionGetLayout(target);
8640 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
8641 const tag_int = blk: {
8642 const tag_ty = union_ty.unionTagTypeHypothetical();
8643 const union_field_name = union_obj.fields.keys()[extra.field_index];
8644 const enum_field_index = tag_ty.enumFieldIndex(union_field_name).?;
8645 var tag_val_payload: Value.Payload.U32 = .{
8646 .base = .{ .tag = .enum_field_index },
8647 .data = @intCast(u32, enum_field_index),
8648 };
8649 const tag_val = Value.initPayload(&tag_val_payload.base);
8650 var int_payload: Value.Payload.U64 = undefined;
8651 const tag_int_val = tag_val.enumToInt(tag_ty, &int_payload);
8652 break :blk tag_int_val.toUnsignedInt(target);
8653 };
85308654 if (layout.payload_size == 0) {
85318655 if (layout.tag_size == 0) {
85328656 return null;
85338657 }
85348658 assert(!isByRef(union_ty));
8535 return union_llvm_ty.constInt(extra.field_index, .False);
8659 return union_llvm_ty.constInt(tag_int, .False);
85368660 }
85378661 assert(isByRef(union_ty));
85388662 // The llvm type of the alloca will the the named LLVM union type, which will not
......@@ -8541,7 +8665,6 @@ pub const FuncGen = struct {
85418665 // then set the fields appropriately.
85428666 const result_ptr = self.buildAlloca(union_llvm_ty);
85438667 const llvm_payload = try self.resolveInst(extra.init);
8544 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
85458668 assert(union_obj.haveFieldTypes());
85468669 const field = union_obj.fields.values()[extra.field_index];
85478670 const field_llvm_ty = try self.dg.lowerType(field.ty);
......@@ -8625,7 +8748,7 @@ pub const FuncGen = struct {
86258748 };
86268749 const field_ptr = self.builder.buildInBoundsGEP(casted_ptr, &indices, indices.len, "");
86278750 const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
8628 const llvm_tag = tag_llvm_ty.constInt(extra.field_index, .False);
8751 const llvm_tag = tag_llvm_ty.constInt(tag_int, .False);
86298752 const store_inst = self.builder.buildStore(llvm_tag, field_ptr);
86308753 store_inst.setAlignment(union_obj.tag_ty.abiAlignment(target));
86318754 }
src/link.zig+4-3
......@@ -931,9 +931,10 @@ pub const File = struct {
931931 std.debug.print("\n", .{});
932932 }
933933
934 const llvm = @import("codegen/llvm/bindings.zig");
935 const os_type = @import("target.zig").osToLLVM(base.options.target.os.tag);
936 const bad = llvm.WriteArchive(full_out_path_z, object_files.items.ptr, object_files.items.len, os_type);
934 const llvm_bindings = @import("codegen/llvm/bindings.zig");
935 const llvm = @import("codegen/llvm.zig");
936 const os_tag = llvm.targetOs(base.options.target.os.tag);
937 const bad = llvm_bindings.WriteArchive(full_out_path_z, object_files.items.ptr, object_files.items.len, os_tag);
937938 if (bad) return error.UnableToWriteArchive;
938939
939940 if (!base.options.disable_lld_caching) {
src/mingw.zig+4-4
......@@ -6,7 +6,6 @@ const assert = std.debug.assert;
66const log = std.log.scoped(.mingw);
77
88const builtin = @import("builtin");
9const target_util = @import("target.zig");
109const Compilation = @import("Compilation.zig");
1110const build_options = @import("build_options");
1211const Cache = @import("Cache.zig");
......@@ -404,11 +403,12 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
404403 });
405404 errdefer comp.gpa.free(lib_final_path);
406405
407 const llvm = @import("codegen/llvm/bindings.zig");
408 const arch_type = target_util.archToLLVM(target.cpu.arch);
406 const llvm_bindings = @import("codegen/llvm/bindings.zig");
407 const llvm = @import("codegen/llvm.zig");
408 const arch_tag = llvm.targetArch(target.cpu.arch);
409409 const def_final_path_z = try arena.dupeZ(u8, def_final_path);
410410 const lib_final_path_z = try arena.dupeZ(u8, lib_final_path);
411 if (llvm.WriteImportLibrary(def_final_path_z.ptr, arch_type, lib_final_path_z.ptr, true)) {
411 if (llvm_bindings.WriteImportLibrary(def_final_path_z.ptr, arch_tag, lib_final_path_z.ptr, true)) {
412412 // TODO surface a proper error here
413413 log.err("unable to turn {s}.def into {s}.lib", .{ lib_name, lib_name });
414414 return error.WritingImportLibFailed;
src/print_zir.zig+5-6
......@@ -283,7 +283,6 @@ const Writer = struct {
283283 .mul_add => try self.writeMulAdd(stream, inst),
284284 .field_parent_ptr => try self.writeFieldParentPtr(stream, inst),
285285 .builtin_call => try self.writeBuiltinCall(stream, inst),
286 .builtin_async_call => try self.writeBuiltinAsyncCall(stream, inst),
287286
288287 .struct_init_anon,
289288 .struct_init_anon_ref,
......@@ -397,7 +396,7 @@ const Writer = struct {
397396 .field_val_named,
398397 => try self.writePlNodeFieldNamed(stream, inst),
399398
400 .as_node => try self.writeAs(stream, inst),
399 .as_node, .as_shift_operand => try self.writeAs(stream, inst),
401400
402401 .repeat,
403402 .repeat_inline,
......@@ -531,6 +530,7 @@ const Writer = struct {
531530 try stream.writeAll(") ");
532531 try self.writeSrc(stream, src);
533532 },
533 .builtin_async_call => try self.writeBuiltinAsyncCall(stream, extended),
534534 }
535535 }
536536
......@@ -814,9 +814,8 @@ const Writer = struct {
814814 try self.writeSrc(stream, inst_data.src());
815815 }
816816
817 fn writeBuiltinAsyncCall(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
818 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
819 const extra = self.code.extraData(Zir.Inst.AsyncCall, inst_data.payload_index).data;
817 fn writeBuiltinAsyncCall(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
818 const extra = self.code.extraData(Zir.Inst.AsyncCall, extended.operand).data;
820819 try self.writeInstRef(stream, extra.frame_buffer);
821820 try stream.writeAll(", ");
822821 try self.writeInstRef(stream, extra.result_ptr);
......@@ -825,7 +824,7 @@ const Writer = struct {
825824 try stream.writeAll(", ");
826825 try self.writeInstRef(stream, extra.args);
827826 try stream.writeAll(") ");
828 try self.writeSrc(stream, inst_data.src());
827 try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
829828 }
830829
831830 fn writeParam(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
src/target.zig+7-101
......@@ -1,5 +1,4 @@
11const std = @import("std");
2const llvm = @import("codegen/llvm/bindings.zig");
32const Type = @import("type.zig").Type;
43
54pub const ArchOsAbi = struct {
......@@ -317,106 +316,6 @@ pub fn supportsReturnAddress(target: std.Target) bool {
317316 };
318317}
319318
320pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType {
321 return switch (os_tag) {
322 .freestanding, .other, .opencl, .glsl450, .vulkan, .plan9 => .UnknownOS,
323 .windows, .uefi => .Win32,
324 .ananas => .Ananas,
325 .cloudabi => .CloudABI,
326 .dragonfly => .DragonFly,
327 .freebsd => .FreeBSD,
328 .fuchsia => .Fuchsia,
329 .ios => .IOS,
330 .kfreebsd => .KFreeBSD,
331 .linux => .Linux,
332 .lv2 => .Lv2,
333 .macos => .MacOSX,
334 .netbsd => .NetBSD,
335 .openbsd => .OpenBSD,
336 .solaris => .Solaris,
337 .zos => .ZOS,
338 .haiku => .Haiku,
339 .minix => .Minix,
340 .rtems => .RTEMS,
341 .nacl => .NaCl,
342 .aix => .AIX,
343 .cuda => .CUDA,
344 .nvcl => .NVCL,
345 .amdhsa => .AMDHSA,
346 .ps4 => .PS4,
347 .elfiamcu => .ELFIAMCU,
348 .tvos => .TvOS,
349 .watchos => .WatchOS,
350 .mesa3d => .Mesa3D,
351 .contiki => .Contiki,
352 .amdpal => .AMDPAL,
353 .hermit => .HermitCore,
354 .hurd => .Hurd,
355 .wasi => .WASI,
356 .emscripten => .Emscripten,
357 };
358}
359
360pub fn archToLLVM(arch_tag: std.Target.Cpu.Arch) llvm.ArchType {
361 return switch (arch_tag) {
362 .arm => .arm,
363 .armeb => .armeb,
364 .aarch64 => .aarch64,
365 .aarch64_be => .aarch64_be,
366 .aarch64_32 => .aarch64_32,
367 .arc => .arc,
368 .avr => .avr,
369 .bpfel => .bpfel,
370 .bpfeb => .bpfeb,
371 .csky => .csky,
372 .hexagon => .hexagon,
373 .m68k => .m68k,
374 .mips => .mips,
375 .mipsel => .mipsel,
376 .mips64 => .mips64,
377 .mips64el => .mips64el,
378 .msp430 => .msp430,
379 .powerpc => .ppc,
380 .powerpcle => .ppcle,
381 .powerpc64 => .ppc64,
382 .powerpc64le => .ppc64le,
383 .r600 => .r600,
384 .amdgcn => .amdgcn,
385 .riscv32 => .riscv32,
386 .riscv64 => .riscv64,
387 .sparc => .sparc,
388 .sparc64 => .sparcv9, // In LLVM, sparc64 == sparcv9.
389 .sparcel => .sparcel,
390 .s390x => .systemz,
391 .tce => .tce,
392 .tcele => .tcele,
393 .thumb => .thumb,
394 .thumbeb => .thumbeb,
395 .i386 => .x86,
396 .x86_64 => .x86_64,
397 .xcore => .xcore,
398 .nvptx => .nvptx,
399 .nvptx64 => .nvptx64,
400 .le32 => .le32,
401 .le64 => .le64,
402 .amdil => .amdil,
403 .amdil64 => .amdil64,
404 .hsail => .hsail,
405 .hsail64 => .hsail64,
406 .spir => .spir,
407 .spir64 => .spir64,
408 .kalimba => .kalimba,
409 .shave => .shave,
410 .lanai => .lanai,
411 .wasm32 => .wasm32,
412 .wasm64 => .wasm64,
413 .renderscript32 => .renderscript32,
414 .renderscript64 => .renderscript64,
415 .ve => .ve,
416 .spu_2, .spirv32, .spirv64 => .UnknownArch,
417 };
418}
419
420319fn eqlIgnoreCase(ignore_case: bool, a: []const u8, b: []const u8) bool {
421320 if (ignore_case) {
422321 return std.ascii.eqlIgnoreCase(a, b);
......@@ -770,3 +669,10 @@ pub fn supportsFunctionAlignment(target: std.Target) bool {
770669 else => true,
771670 };
772671}
672
673pub fn supportsTailCall(target: std.Target, backend: std.builtin.CompilerBackend) bool {
674 switch (backend) {
675 .stage1, .stage2_llvm => return @import("codegen/llvm.zig").supportsTailCall(target),
676 else => return false,
677 }
678}
test/behavior/call.zig+68
......@@ -261,3 +261,71 @@ test "arguments to comptime parameters generated in comptime blocks" {
261261 };
262262 S.foo(S.fortyTwo());
263263}
264
265test "forced tail call" {
266 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
267 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
268 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
269 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
270 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
271 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
272
273 if (builtin.zig_backend == .stage2_llvm) {
274 // Only attempt this test on targets we know have tail call support in LLVM.
275 if (builtin.cpu.arch != .x86_64 and builtin.cpu.arch != .aarch64) {
276 return error.SkipZigTest;
277 }
278 }
279
280 const S = struct {
281 fn fibonacciTailInternal(n: u16, a: u16, b: u16) u16 {
282 if (n == 0) return a;
283 if (n == 1) return b;
284 return @call(
285 .{ .modifier = .always_tail },
286 fibonacciTailInternal,
287 .{ n - 1, b, a + b },
288 );
289 }
290
291 fn fibonacciTail(n: u16) u16 {
292 return fibonacciTailInternal(n, 0, 1);
293 }
294 };
295 try expect(S.fibonacciTail(10) == 55);
296}
297
298test "inline call preserves tail call" {
299 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
300 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
301 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
302 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
303 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
304 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
305
306 if (builtin.zig_backend == .stage2_llvm) {
307 // Only attempt this test on targets we know have tail call support in LLVM.
308 if (builtin.cpu.arch != .x86_64 and builtin.cpu.arch != .aarch64) {
309 return error.SkipZigTest;
310 }
311 }
312
313 const max = std.math.maxInt(u16);
314 const S = struct {
315 var a: u16 = 0;
316 fn foo() void {
317 return bar();
318 }
319
320 inline fn bar() void {
321 if (a == max) return;
322 // Stack overflow if not tail called
323 var buf: [max]u16 = undefined;
324 buf[a] = a;
325 a += 1;
326 return @call(.{ .modifier = .always_tail }, foo, .{});
327 }
328 };
329 S.foo();
330 try expect(S.a == std.math.maxInt(u16));
331}
test/behavior/union.zig+28
......@@ -1324,3 +1324,31 @@ test "union and enum field order doesn't match" {
13241324 x = .b;
13251325 try expect(x == .b);
13261326}
1327
1328test "@unionInit uses tag value instead of field index" {
1329 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1330 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1331 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1332 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1333 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1334
1335 const E = enum(u8) {
1336 b = 255,
1337 a = 3,
1338 };
1339 const U = union(E) {
1340 a: usize,
1341 b: isize,
1342 };
1343 var i: isize = -1;
1344 var u = @unionInit(U, "b", i);
1345 {
1346 var a = u.b;
1347 try expect(a == i);
1348 }
1349 {
1350 var a = &u.b;
1351 try expect(a.* == i);
1352 }
1353 try expect(@enumToInt(u) == 255);
1354}
test/cases/compile_errors/invalid_tail_call.zig created+12
......@@ -0,0 +1,12 @@
1fn myFn(_: usize) void {
2 return;
3}
4pub export fn entry() void {
5 @call(.{ .modifier = .always_tail }, myFn, .{0});
6}
7
8// error
9// backend=llvm
10// target=native
11//
12// :5:5: error: unable to perform tail call: type of function being called 'fn(usize) void' does not match type of calling function 'fn() callconv(.C) void'
test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig created+16
......@@ -0,0 +1,16 @@
1export fn entry() void {
2 const llamas1 = makeLlamas(5);
3 const llamas2 = makeLlamas(5);
4 _ = llamas1;
5 _ = llamas2;
6}
7
8fn makeLlamas(count: usize) [count]u8 {
9 _ = count;
10}
11
12// error
13// target=native
14//
15// :8:30: error: unable to resolve comptime value
16// :8:30: note: array length must be comptime known
test/cases/compile_errors/runtime_to_comptime_num.zig created+31
......@@ -0,0 +1,31 @@
1pub export fn entry() void {
2 var a: u32 = 0;
3 _ = @as(comptime_int, a);
4}
5pub export fn entry2() void{
6 var a: u32 = 0;
7 _ = @as(comptime_float, a);
8}
9pub export fn entry3() void{
10 comptime var aa: comptime_float = 0.0;
11 var a: f32 = 4;
12 aa = a;
13}
14pub export fn entry4() void{
15 comptime var aa: comptime_int = 0.0;
16 var a: f32 = 4;
17 aa = a;
18}
19
20// error
21// backend=stage2
22// target=native
23//
24// :3:27: error: unable to resolve comptime value
25// :3:27: note: value being casted to 'comptime_int' must be comptime known
26// :7:29: error: unable to resolve comptime value
27// :7:29: note: value being casted to 'comptime_float' must be comptime known
28// :12:10: error: unable to resolve comptime value
29// :12:10: note: value being casted to 'comptime_float' must be comptime known
30// :17:10: error: unable to resolve comptime value
31// :17:10: note: value being casted to 'comptime_int' must be comptime known
test/cases/compile_errors/shifting_without_int_type_or_comptime_known.zig created+23
......@@ -0,0 +1,23 @@
1export fn entry(x: u8) u8 {
2 return 0x11 << x;
3}
4export fn entry1(x: u8) u8 {
5 return 0x11 >> x;
6}
7export fn entry2() void {
8 var x: u5 = 1;
9 _ = @shlExact(12345, x);
10}
11export fn entry3() void {
12 var x: u5 = 1;
13 _ = @shrExact(12345, x);
14}
15
16// error
17// backend=stage2
18// target=native
19//
20// :2:17: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
21// :5:17: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
22// :9:9: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
23// :13:9: error: LHS of shift must be a fixed-width integer type, or RHS must be a comptime known
test/cases/compile_errors/stage1/obj/shifting_without_int_type_or_comptime_known.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry(x: u8) u8 {
2 return 0x11 << x;
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:17: error: LHS of shift must be a fixed-width integer type, or RHS must be compile-time known
test/cases/taill_call_noreturn.zig created+18
......@@ -0,0 +1,18 @@
1const std = @import("std");
2const builtin = std.builtin;
3pub fn foo(message: []const u8, stack_trace: ?*builtin.StackTrace) noreturn {
4 @call(.{ .modifier = .always_tail }, bar, .{ message, stack_trace });
5}
6pub fn bar(message: []const u8, stack_trace: ?*builtin.StackTrace) noreturn {
7 _ = message;
8 _ = stack_trace;
9 std.process.exit(0);
10}
11
12pub fn main() void {
13 foo("foo", null);
14}
15
16// run
17// backend=llvm
18// target=x86_64-linux,x86_64-macos,aarch64-linux,aarch64-macos