From 8bc759e094cda907acc64c50b86ceb9d3a87213f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Nov 2022 20:56:26 -0700 Subject: [PATCH] compiler: free up two ZIR tags --- src/AstGen.zig | 7 +++---- src/Autodoc.zig | 2 -- src/Sema.zig | 30 +++++++++++++++--------------- src/Zir.zig | 18 ++++++------------ src/codegen/llvm.zig | 5 ++--- src/print_zir.zig | 4 ++-- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 8c15bc8574..7439ee5927 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2700,8 +2700,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As .rem, .shl_exact, .shr_exact, - .bit_offset_of, - .offset_of, .splat, .reduce, .shuffle, @@ -9011,11 +9009,12 @@ fn offsetOf( node: Ast.Node.Index, lhs_node: Ast.Node.Index, rhs_node: Ast.Node.Index, - tag: Zir.Inst.Tag, + tag: Zir.Inst.Extended, ) InnerError!Zir.Inst.Ref { const type_inst = try typeExpr(gz, scope, lhs_node); const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, rhs_node); - const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{ + const result = try gz.addExtendedPayload(tag, Zir.Inst.BinNode{ + .node = gz.nodeIndexToRelative(node), .lhs = type_inst, .rhs = field_name, }); diff --git a/src/Autodoc.zig b/src/Autodoc.zig index 1b9988c0c3..5a06433442 100644 --- a/src/Autodoc.zig +++ b/src/Autodoc.zig @@ -1542,8 +1542,6 @@ fn walkInstruction( .bitcast, .vector_type, // @check - .bit_offset_of, - .offset_of, .splat, .reduce, .min, diff --git a/src/Sema.zig b/src/Sema.zig index a60c24f442..3e4c695a1a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1046,8 +1046,6 @@ fn analyzeBodyInner( .has_field => try sema.zirHasField(block, inst), .byte_swap => try sema.zirByteSwap(block, inst), .bit_reverse => try sema.zirBitReverse(block, inst), - .bit_offset_of => try sema.zirBitOffsetOf(block, inst), - .offset_of => try sema.zirOffsetOf(block, inst), .splat => try sema.zirSplat(block, inst), .reduce => try sema.zirReduce(block, inst), .shuffle => try sema.zirShuffle(block, inst), @@ -1179,6 +1177,8 @@ fn analyzeBodyInner( .work_group_size => try sema.zirWorkItem( block, extended, extended.opcode), .work_group_id => try sema.zirWorkItem( block, extended, extended.opcode), .in_comptime => try sema.zirInComptime( block), + .bit_offset_of => try sema.zirBitOffsetOf( block, extended), + .offset_of => try sema.zirOffsetOf( block, extended), // zig fmt: on .fence => { @@ -21743,24 +21743,24 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! } } -fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { - const offset = try sema.bitOffsetOf(block, inst); +fn zirBitOffsetOf(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { + const offset = try sema.bitOffsetOf(block, extended); return sema.addIntUnsigned(Type.comptime_int, offset); } -fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { - const offset = try sema.bitOffsetOf(block, inst); +fn zirOffsetOf(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { + const offset = try sema.bitOffsetOf(block, extended); // TODO reminder to make this a compile error for packed structs return sema.addIntUnsigned(Type.comptime_int, offset / 8); } -fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 { - const inst_data = sema.code.instructions.items(.data)[inst].pl_node; - const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node }; +fn bitOffsetOf(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!u64 { + const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; + + const src: LazySrcLoc = .{ .node_offset_bin_op = extra.node }; sema.src = src; - const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node }; - const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; - const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; + const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = extra.node }; + const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = extra.node }; const ty = try sema.resolveType(block, lhs_src, extra.lhs); const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, "name of field must be comptime-known"); @@ -33840,9 +33840,9 @@ fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void { } } -// In case of querying the ABI alignment of this struct, we will ask -// for hasRuntimeBits() of each field, so we need "requires comptime" -// to be known already before this function returns. +/// In case of querying the ABI alignment of this struct, we will ask +/// for hasRuntimeBits() of each field, so we need "requires comptime" +/// to be known already before this function returns. pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool { const mod = sema.mod; diff --git a/src/Zir.zig b/src/Zir.zig index eb2e9bdb33..43e94fd7ce 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -876,12 +876,6 @@ pub const Inst = struct { /// Implements the `@bitReverse` builtin. Uses the `un_node` union field. bit_reverse, - /// Implements the `@bitOffsetOf` builtin. - /// Uses the `pl_node` union field with payload `Bin`. - bit_offset_of, - /// Implements the `@offsetOf` builtin. - /// Uses the `pl_node` union field with payload `Bin`. - offset_of, /// Implements the `@splat` builtin. /// Uses the `pl_node` union field with payload `Bin`. splat, @@ -1200,8 +1194,6 @@ pub const Inst = struct { .rem, .shl_exact, .shr_exact, - .bit_offset_of, - .offset_of, .splat, .reduce, .shuffle, @@ -1484,8 +1476,6 @@ pub const Inst = struct { .rem, .shl_exact, .shr_exact, - .bit_offset_of, - .offset_of, .splat, .reduce, .shuffle, @@ -1759,8 +1749,6 @@ pub const Inst = struct { .shr = .pl_node, .shr_exact = .pl_node, - .bit_offset_of = .pl_node, - .offset_of = .pl_node, .splat = .pl_node, .reduce = .pl_node, .shuffle = .pl_node, @@ -2009,6 +1997,12 @@ pub const Inst = struct { /// with a specific value. For instance, this is used for the capture of an `errdefer`. /// This should never appear in a body. value_placeholder, + /// Implements the `@bitOffsetOf` builtin. + /// `operand` is payload index to `BinNode`. + bit_offset_of, + /// Implements the `@offsetOf` builtin. + /// `operand` is payload index to `BinNode`. + offset_of, pub const InstData = struct { opcode: Extended, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index f07875aec2..f6ef790e04 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -4940,8 +4940,7 @@ pub const FuncGen = struct { const frame_alloca = fg.builder.buildArrayAlloca(llvm_i8, frame_size, ""); frame_alloca.setAlignment(target.ptrBitWidth() / 4); const frame_llvm_ty = try o.lowerAsyncFrameHeader(fn_info.return_type.toType()); - const llvm_ptr_ty = fg.context.pointerType(0); - const frame_ptr = fg.builder.buildBitCast(frame_alloca, llvm_ptr_ty, ""); + const frame_ptr = fg.builder.buildBitCast(frame_alloca, o.context.pointerType(0), ""); const l = asyncFrameLayout(); const fn_ptr_ptr = fg.builder.buildStructGEP(frame_llvm_ty, frame_ptr, l.fn_ptr, ""); _ = fg.builder.buildStore(callee, fn_ptr_ptr); @@ -4954,7 +4953,7 @@ pub const FuncGen = struct { const awaiter_ptr = fg.builder.buildStructGEP(frame_llvm_ty, frame_ptr, l.awaiter, ""); _ = fg.builder.buildStore(zero, awaiter_ptr); - var llvm_args = std.ArrayList(*llvm.Value).init(fg.gpa); + var llvm_args = std.ArrayList(*llvm.Value).init(o.gpa); defer llvm_args.deinit(); try addCallArgs(fg, args, &llvm_args, fn_info); diff --git a/src/print_zir.zig b/src/print_zir.zig index 070a91ca15..c856cd3bc0 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -335,8 +335,6 @@ const Writer = struct { .div_trunc, .mod, .rem, - .bit_offset_of, - .offset_of, .splat, .reduce, .bitcast, @@ -526,6 +524,8 @@ const Writer = struct { .wasm_memory_grow, .prefetch, .c_va_arg, + .bit_offset_of, + .offset_of, => { const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data; const src = LazySrcLoc.nodeOffset(inst_data.node);