authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2021-01-11 10:30:15-07:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2021-02-10 20:22:18-07:00
log7644e9a752ebe50c4372fae8ea0e8eaecb211508
tree423ded389db49148c77ff5a3b36bb7dd4f0cccc7
parent1c15091bc8d83b9464082afbdb62ecd9c9176cd6
signaturelock-open Commit is signed but in an unrecognized format.

stage2: switch from inline fn to callconv(.Inline)


4 files changed, 38 insertions(+), 42 deletions(-)

src/Module.zig+19-16
......@@ -1087,14 +1087,23 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
10871087 if (fn_proto.getSectionExpr()) |sect_expr| {
10881088 return self.failNode(&fn_type_scope.base, sect_expr, "TODO implement function section expression", .{});
10891089 }
1090 if (fn_proto.getCallconvExpr()) |callconv_expr| {
1091 return self.failNode(
1092 &fn_type_scope.base,
1093 callconv_expr,
1094 "TODO implement function calling convention expression",
1095 .{},
1096 );
1097 }
1090
1091 const enum_literal_type = try astgen.addZIRInstConst(self, &fn_type_scope.base, fn_src, .{
1092 .ty = Type.initTag(.type),
1093 .val = Value.initTag(.enum_literal_type),
1094 });
1095 const enum_literal_type_rl: astgen.ResultLoc = .{ .ty = enum_literal_type };
1096 const cc = if (fn_proto.getCallconvExpr()) |callconv_expr|
1097 try astgen.expr(self, &fn_type_scope.base, enum_literal_type_rl, callconv_expr)
1098 else
1099 try astgen.addZIRInstConst(self, &fn_type_scope.base, fn_src, .{
1100 .ty = Type.initTag(.enum_literal),
1101 .val = try Value.Tag.enum_literal.create(
1102 &fn_type_scope_arena.allocator,
1103 try fn_type_scope_arena.allocator.dupe(u8, "Unspecified"),
1104 ),
1105 });
1106
10981107 const return_type_expr = switch (fn_proto.return_type) {
10991108 .Explicit => |node| node,
11001109 .InferErrorSet => |node| return self.failNode(&fn_type_scope.base, node, "TODO implement inferred error sets", .{}),
......@@ -1105,6 +1114,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11051114 const fn_type_inst = try astgen.addZIRInst(self, &fn_type_scope.base, fn_src, zir.Inst.FnType, .{
11061115 .return_type = return_type_inst,
11071116 .param_types = param_types,
1117 .cc = cc,
11081118 }, .{});
11091119
11101120 if (std.builtin.mode == .Debug and self.comp.verbose_ir) {
......@@ -1230,14 +1240,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12301240 };
12311241 };
12321242
1233 const is_inline = blk: {
1234 if (fn_proto.getExternExportInlineToken()) |maybe_inline_token| {
1235 if (tree.token_ids[maybe_inline_token] == .Keyword_inline) {
1236 break :blk true;
1237 }
1238 }
1239 break :blk false;
1240 };
1243 const is_inline = fn_type.fnCallingConvention() == .Inline;
12411244 const anal_state = ([2]Fn.Analysis{ .queued, .inline_only })[@boolToInt(is_inline)];
12421245
12431246 new_func.* = .{
src/type.zig+3-1
......@@ -552,7 +552,9 @@ pub const Type = extern union {
552552 if (i != 0) try out_stream.writeAll(", ");
553553 try param_type.format("", .{}, out_stream);
554554 }
555 try out_stream.writeAll(") ");
555 try out_stream.writeAll(") callconv(.");
556 try out_stream.writeAll(@tagName(payload.cc));
557 try out_stream.writeAll(")");
556558 ty = payload.return_type;
557559 continue;
558560 },
src/zir.zig+3-6
......@@ -863,9 +863,7 @@ pub const Inst = struct {
863863 fn_type: *Inst,
864864 body: Body,
865865 },
866 kw_args: struct {
867 is_inline: bool = false,
868 },
866 kw_args: struct {},
869867 };
870868
871869 pub const FnType = struct {
......@@ -875,10 +873,9 @@ pub const Inst = struct {
875873 positionals: struct {
876874 param_types: []*Inst,
877875 return_type: *Inst,
876 cc: *Inst,
878877 },
879 kw_args: struct {
880 cc: std.builtin.CallingConvention = .Unspecified,
881 },
878 kw_args: struct {},
882879 };
883880
884881 pub const IntType = struct {
src/zir_sema.zig+13-19
......@@ -980,18 +980,8 @@ fn zirCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
980980
981981 const b = try mod.requireFunctionBlock(scope, inst.base.src);
982982 const is_comptime_call = b.is_comptime or inst.kw_args.modifier == .compile_time;
983 const is_inline_call = is_comptime_call or inst.kw_args.modifier == .always_inline or blk: {
984 // This logic will get simplified by
985 // https://github.com/ziglang/zig/issues/6429
986 if (try mod.resolveDefinedValue(scope, func)) |func_val| {
987 const module_fn = switch (func_val.tag()) {
988 .function => func_val.castTag(.function).?.data,
989 else => break :blk false,
990 };
991 break :blk module_fn.state == .inline_only;
992 }
993 break :blk false;
994 };
983 const is_inline_call = is_comptime_call or inst.kw_args.modifier == .always_inline or
984 func.ty.fnCallingConvention() == .Inline;
995985 if (is_inline_call) {
996986 const func_val = try mod.resolveConstValue(scope, func);
997987 const module_fn = switch (func_val.tag()) {
......@@ -1075,7 +1065,7 @@ fn zirFn(mod: *Module, scope: *Scope, fn_inst: *zir.Inst.Fn) InnerError!*Inst {
10751065 const fn_type = try resolveType(mod, scope, fn_inst.positionals.fn_type);
10761066 const new_func = try scope.arena().create(Module.Fn);
10771067 new_func.* = .{
1078 .state = if (fn_inst.kw_args.is_inline) .inline_only else .queued,
1068 .state = if (fn_type.fnCallingConvention() == .Inline) .inline_only else .queued,
10791069 .zir = fn_inst.positionals.body,
10801070 .body = undefined,
10811071 .owner_decl = scope.ownerDecl().?,
......@@ -1305,22 +1295,26 @@ fn zirFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*
13051295 const tracy = trace(@src());
13061296 defer tracy.end();
13071297 const return_type = try resolveType(mod, scope, fntype.positionals.return_type);
1298 const cc_tv = try resolveInstConst(mod, scope, fntype.positionals.cc);
1299 const cc_str = cc_tv.val.castTag(.enum_literal).?.data;
1300 const cc = std.meta.stringToEnum(std.builtin.CallingConvention, cc_str) orelse
1301 return mod.fail(scope, fntype.positionals.cc.src, "Unknown calling convention {s}", .{cc_str});
13081302
13091303 // Hot path for some common function types.
13101304 if (fntype.positionals.param_types.len == 0) {
1311 if (return_type.zigTypeTag() == .NoReturn and fntype.kw_args.cc == .Unspecified) {
1305 if (return_type.zigTypeTag() == .NoReturn and cc == .Unspecified) {
13121306 return mod.constType(scope, fntype.base.src, Type.initTag(.fn_noreturn_no_args));
13131307 }
13141308
1315 if (return_type.zigTypeTag() == .Void and fntype.kw_args.cc == .Unspecified) {
1309 if (return_type.zigTypeTag() == .Void and cc == .Unspecified) {
13161310 return mod.constType(scope, fntype.base.src, Type.initTag(.fn_void_no_args));
13171311 }
13181312
1319 if (return_type.zigTypeTag() == .NoReturn and fntype.kw_args.cc == .Naked) {
1313 if (return_type.zigTypeTag() == .NoReturn and cc == .Naked) {
13201314 return mod.constType(scope, fntype.base.src, Type.initTag(.fn_naked_noreturn_no_args));
13211315 }
13221316
1323 if (return_type.zigTypeTag() == .Void and fntype.kw_args.cc == .C) {
1317 if (return_type.zigTypeTag() == .Void and cc == .C) {
13241318 return mod.constType(scope, fntype.base.src, Type.initTag(.fn_ccc_void_no_args));
13251319 }
13261320 }
......@@ -1337,9 +1331,9 @@ fn zirFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*
13371331 }
13381332
13391333 const fn_ty = try Type.Tag.function.create(arena, .{
1340 .cc = fntype.kw_args.cc,
1341 .return_type = return_type,
13421334 .param_types = param_types,
1335 .return_type = return_type,
1336 .cc = cc,
13431337 });
13441338 return mod.constType(scope, fntype.base.src, fn_ty);
13451339}