authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-01 14:03:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-01 14:03:32-07:00
log18e42661dc9b8199311ee086b24ef5c85cf4708f
tree9a9a32ea7b4e58e733cd5548a2be5d2102385c66
parent543bee0adf2d3b036654fa0983c16ff7023f504c

Sema: eliminate use of resolveAlreadyCoercedInt


3 files changed, 16 insertions(+), 31 deletions(-)

src/AstGen.zig+6-5
......@@ -290,6 +290,7 @@ pub const ResultLoc = union(enum) {
290290};
291291
292292pub const align_rl: ResultLoc = .{ .ty = .u16_type };
293pub const coerced_align_rl: ResultLoc = .{ .coerced_ty = .u16_type };
293294pub const bool_rl: ResultLoc = .{ .ty = .bool_type };
294295pub const type_rl: ResultLoc = .{ .ty = .type_type };
295296pub const coerced_type_rl: ResultLoc = .{ .coerced_ty = .type_type };
......@@ -2971,7 +2972,7 @@ fn ptrType(
29712972 trailing_count += 1;
29722973 }
29732974 if (ptr_info.ast.align_node != 0) {
2974 align_ref = try expr(gz, scope, align_rl, ptr_info.ast.align_node);
2975 align_ref = try expr(gz, scope, coerced_align_rl, ptr_info.ast.align_node);
29752976 trailing_count += 1;
29762977 }
29772978 if (ptr_info.ast.addrspace_node != 0) {
......@@ -2980,8 +2981,8 @@ fn ptrType(
29802981 }
29812982 if (ptr_info.ast.bit_range_start != 0) {
29822983 assert(ptr_info.ast.bit_range_end != 0);
2983 bit_start_ref = try expr(gz, scope, .none, ptr_info.ast.bit_range_start);
2984 bit_end_ref = try expr(gz, scope, .none, ptr_info.ast.bit_range_end);
2984 bit_start_ref = try expr(gz, scope, .{ .coerced_ty = .u16_type }, ptr_info.ast.bit_range_start);
2985 bit_end_ref = try expr(gz, scope, .{ .coerced_ty = .u16_type }, ptr_info.ast.bit_range_end);
29852986 trailing_count += 2;
29862987 }
29872988
......@@ -7122,7 +7123,7 @@ fn builtinCall(
71227123 .error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
71237124 .int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u16_type }, params[0], .int_to_error),
71247125 .compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
7125 .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u32_type }, params[0], .set_eval_branch_quota),
7126 .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type }, params[0], .set_eval_branch_quota),
71267127 .enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
71277128 .bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
71287129 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
......@@ -7437,7 +7438,7 @@ fn builtinCall(
74377438 },
74387439 .Vector => {
74397440 const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{
7440 .lhs = try comptimeExpr(gz, scope, .{ .ty = .u32_type }, params[0]),
7441 .lhs = try comptimeExpr(gz, scope, .{ .coerced_ty = .u32_type }, params[0]),
74417442 .rhs = try typeExpr(gz, scope, params[1]),
74427443 });
74437444 return rvalue(gz, rl, result, node);
src/Sema.zig+9-26
......@@ -1464,26 +1464,6 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
14641464 return error.AnalysisFail;
14651465}
14661466
1467/// Appropriate to call when the coercion has already been done by result
1468/// location semantics. Asserts the value fits in the provided `Int` type.
1469/// Only supports `Int` types 64 bits or less.
1470/// TODO don't ever call this since we're migrating towards ResultLoc.coerced_ty.
1471fn resolveAlreadyCoercedInt(
1472 sema: *Sema,
1473 block: *Block,
1474 src: LazySrcLoc,
1475 zir_ref: Zir.Inst.Ref,
1476 comptime Int: type,
1477) !Int {
1478 comptime assert(@typeInfo(Int).Int.bits <= 64);
1479 const air_inst = sema.resolveInst(zir_ref);
1480 const val = try sema.resolveConstValue(block, src, air_inst);
1481 switch (@typeInfo(Int).Int.signedness) {
1482 .signed => return @intCast(Int, val.toSignedInt()),
1483 .unsigned => return @intCast(Int, val.toUnsignedInt()),
1484 }
1485}
1486
14871467fn resolveAlign(
14881468 sema: *Sema,
14891469 block: *Block,
......@@ -3380,7 +3360,7 @@ fn storeToInferredAllocComptime(
33803360fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
33813361 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
33823362 const src = inst_data.src();
3383 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);
3363 const quota = @intCast(u32, try sema.resolveInt(block, src, inst_data.operand, Type.u32));
33843364 if (sema.branch_quota < quota)
33853365 sema.branch_quota = quota;
33863366}
......@@ -5080,11 +5060,11 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
50805060 const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
50815061 const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
50825062 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
5083 const len = try sema.resolveAlreadyCoercedInt(block, len_src, extra.lhs, u32);
5063 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.u32);
50845064 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
50855065 try sema.checkVectorElemType(block, elem_type_src, elem_type);
50865066 const vector_type = try Type.Tag.vector.create(sema.arena, .{
5087 .len = len,
5067 .len = @intCast(u32, len),
50885068 .elem_type = elem_type,
50895069 });
50905070 return sema.addType(vector_type);
......@@ -11341,7 +11321,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1134111321 const abi_align = if (inst_data.flags.has_align) blk: {
1134211322 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1134311323 extra_i += 1;
11344 break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u32);
11324 const abi_align = try sema.resolveInt(block, .unneeded, ref, Type.u32);
11325 break :blk @intCast(u32, abi_align);
1134511326 } else 0;
1134611327
1134711328 const address_space = if (inst_data.flags.has_addrspace) blk: {
......@@ -11353,13 +11334,15 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1135311334 const bit_offset = if (inst_data.flags.has_bit_range) blk: {
1135411335 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1135511336 extra_i += 1;
11356 break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16);
11337 const bit_offset = try sema.resolveInt(block, .unneeded, ref, Type.u16);
11338 break :blk @intCast(u16, bit_offset);
1135711339 } else 0;
1135811340
1135911341 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
1136011342 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1136111343 extra_i += 1;
11362 break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16);
11344 const host_size = try sema.resolveInt(block, .unneeded, ref, Type.u16);
11345 break :blk @intCast(u16, host_size);
1136311346 } else 0;
1136411347
1136511348 if (host_size != 0 and bit_offset >= host_size * 8) {
src/type.zig+1
......@@ -5063,6 +5063,7 @@ pub const Type = extern union {
50635063 };
50645064
50655065 pub const @"u8" = initTag(.u8);
5066 pub const @"u16" = initTag(.u16);
50665067 pub const @"u32" = initTag(.u32);
50675068 pub const @"u64" = initTag(.u64);
50685069