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(...@@ -1359,6 +1359,7 @@ fn blockExprStmts(
1359 .int_to_enum,1359 .int_to_enum,
1360 .enum_to_int,1360 .enum_to_int,
1361 .type_info,1361 .type_info,
1362 .size_of,
1362 => break :b false,1363 => break :b false,
13631364
1364 // ZIR instructions that are always either `noreturn` or `void`.1365 // ZIR instructions that are always either `noreturn` or `void`.
...@@ -4342,6 +4343,12 @@ fn builtinCall(...@@ -4342,6 +4343,12 @@ fn builtinCall(
4342 return rvalue(gz, scope, rl, result, node);4343 return rvalue(gz, scope, rl, result, node);
4343 },4344 },
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
4345 .add_with_overflow,4352 .add_with_overflow,
4346 .align_cast,4353 .align_cast,
4347 .align_of,4354 .align_of,
...@@ -4396,7 +4403,6 @@ fn builtinCall(...@@ -4396,7 +4403,6 @@ fn builtinCall(
4396 .shl_with_overflow,4403 .shl_with_overflow,
4397 .shr_exact,4404 .shr_exact,
4398 .shuffle,4405 .shuffle,
4399 .size_of,
4400 .splat,4406 .splat,
4401 .reduce,4407 .reduce,
4402 .src,4408 .src,
src/Sema.zig+11
...@@ -264,6 +264,7 @@ pub fn analyzeBody(...@@ -264,6 +264,7 @@ pub fn analyzeBody(
264 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),264 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
265 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),265 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
266 .type_info => try sema.zirTypeInfo(block, inst),266 .type_info => try sema.zirTypeInfo(block, inst),
267 .size_of => try sema.zirSizeOf(block, inst),
267 .typeof => try sema.zirTypeof(block, inst),268 .typeof => try sema.zirTypeof(block, inst),
268 .typeof_elem => try sema.zirTypeofElem(block, inst),269 .typeof_elem => try sema.zirTypeofElem(block, inst),
269 .typeof_peer => try sema.zirTypeofPeer(block, inst),270 .typeof_peer => try sema.zirTypeofPeer(block, inst),
...@@ -4347,6 +4348,16 @@ fn zirCmp(...@@ -4347,6 +4348,16 @@ fn zirCmp(
4347 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);4348 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);
4348}4349}
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
4350fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {4361fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
4351 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4362 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4352 const src = inst_data.src();4363 const src = inst_data.src();
src/zir.zig+4
...@@ -695,6 +695,8 @@ pub const Inst = struct {...@@ -695,6 +695,8 @@ pub const Inst = struct {
695 enum_to_int,695 enum_to_int,
696 /// Implements the `@typeInfo` builtin. Uses `un_node`.696 /// Implements the `@typeInfo` builtin. Uses `un_node`.
697 type_info,697 type_info,
698 /// Implements the `@sizeOf` builtin. Uses `un_node`.
699 size_of,
698700
699 /// Returns whether the instruction is one of the control flow "noreturn" types.701 /// Returns whether the instruction is one of the control flow "noreturn" types.
700 /// Function calls do not count.702 /// Function calls do not count.
...@@ -861,6 +863,7 @@ pub const Inst = struct {...@@ -861,6 +863,7 @@ pub const Inst = struct {
861 .int_to_enum,863 .int_to_enum,
862 .enum_to_int,864 .enum_to_int,
863 .type_info,865 .type_info,
866 .size_of,
864 => false,867 => false,
865868
866 .@"break",869 .@"break",
...@@ -1670,6 +1673,7 @@ const Writer = struct {...@@ -1670,6 +1673,7 @@ const Writer = struct {
1670 .struct_init_empty,1673 .struct_init_empty,
1671 .enum_to_int,1674 .enum_to_int,
1672 .type_info,1675 .type_info,
1676 .size_of,
1673 => try self.writeUnNode(stream, inst),1677 => try self.writeUnNode(stream, inst),
16741678
1675 .ref,1679 .ref,