authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-12 21:01:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-15 19:06:39-07:00
log69645e28171829a3c53ee5359e08562bddd174f7
treef967874ad60b0142c770c223d13ef9e483ee4ce9
parent0e50a0c1e53a062e7c68ceeb1cfab9597a9caa23

stage2: implement `@sizeOf`


3 files changed, 22 insertions(+), 1 deletions(-)

src/AstGen.zig+7-1
......@@ -1359,6 +1359,7 @@ fn blockExprStmts(
13591359 .int_to_enum,
13601360 .enum_to_int,
13611361 .type_info,
1362 .size_of,
13621363 => break :b false,
13631364
13641365 // ZIR instructions that are always either `noreturn` or `void`.
......@@ -4342,6 +4343,12 @@ fn builtinCall(
43424343 return rvalue(gz, scope, rl, result, node);
43434344 },
43444345
4346 .size_of => {
4347 const operand = try typeExpr(gz, scope, params[0]);
4348 const result = try gz.addUnNode(.size_of, operand, node);
4349 return rvalue(gz, scope, rl, result, node);
4350 },
4351
43454352 .add_with_overflow,
43464353 .align_cast,
43474354 .align_of,
......@@ -4396,7 +4403,6 @@ fn builtinCall(
43964403 .shl_with_overflow,
43974404 .shr_exact,
43984405 .shuffle,
4399 .size_of,
44004406 .splat,
44014407 .reduce,
44024408 .src,
src/Sema.zig+11
......@@ -264,6 +264,7 @@ pub fn analyzeBody(
264264 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
265265 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
266266 .type_info => try sema.zirTypeInfo(block, inst),
267 .size_of => try sema.zirSizeOf(block, inst),
267268 .typeof => try sema.zirTypeof(block, inst),
268269 .typeof_elem => try sema.zirTypeofElem(block, inst),
269270 .typeof_peer => try sema.zirTypeofPeer(block, inst),
......@@ -4347,6 +4348,16 @@ fn zirCmp(
43474348 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);
43484349}
43494350
4351fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
4352 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4353 const src = inst_data.src();
4354 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
4355 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
4356 const target = sema.mod.getTarget();
4357 const abi_size = operand_ty.abiSize(target);
4358 return sema.mod.constIntUnsigned(sema.arena, src, Type.initTag(.comptime_int), abi_size);
4359}
4360
43504361fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
43514362 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
43524363 const src = inst_data.src();
src/zir.zig+4
......@@ -695,6 +695,8 @@ pub const Inst = struct {
695695 enum_to_int,
696696 /// Implements the `@typeInfo` builtin. Uses `un_node`.
697697 type_info,
698 /// Implements the `@sizeOf` builtin. Uses `un_node`.
699 size_of,
698700
699701 /// Returns whether the instruction is one of the control flow "noreturn" types.
700702 /// Function calls do not count.
......@@ -861,6 +863,7 @@ pub const Inst = struct {
861863 .int_to_enum,
862864 .enum_to_int,
863865 .type_info,
866 .size_of,
864867 => false,
865868
866869 .@"break",
......@@ -1670,6 +1673,7 @@ const Writer = struct {
16701673 .struct_init_empty,
16711674 .enum_to_int,
16721675 .type_info,
1676 .size_of,
16731677 => try self.writeUnNode(stream, inst),
16741678
16751679 .ref,