authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-07 16:05:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-07 16:06:25-07:00
log6ac204714236eaeaea5dc66afddbe158966b2532
tree3e0a8b35ba2aa6ec803861cfc9a6b0d8fe80e49f
parent47531b7d9389c45af3e46b623235792f14a40ff2

stage2: implement extern functions


4 files changed, 57 insertions(+), 20 deletions(-)

src/AstGen.zig+8-1
......@@ -1081,6 +1081,7 @@ fn fnProtoExpr(
10811081 .is_var_args = is_var_args,
10821082 .is_inferred_error = false,
10831083 .is_test = false,
1084 .is_extern = false,
10841085 });
10851086 return rvalue(gz, scope, rl, result, fn_proto.ast.proto_node);
10861087}
......@@ -2807,6 +2808,7 @@ fn fnDecl(
28072808 .is_var_args = is_var_args,
28082809 .is_inferred_error = false,
28092810 .is_test = false,
2811 .is_extern = true,
28102812 });
28112813 } else func: {
28122814 if (is_var_args) {
......@@ -2883,6 +2885,7 @@ fn fnDecl(
28832885 .is_var_args = is_var_args,
28842886 .is_inferred_error = is_inferred_error,
28852887 .is_test = false,
2888 .is_extern = false,
28862889 });
28872890 };
28882891
......@@ -3223,6 +3226,7 @@ fn testDecl(
32233226 .is_var_args = false,
32243227 .is_inferred_error = true,
32253228 .is_test = true,
3229 .is_extern = false,
32263230 });
32273231
32283232 _ = try decl_block.addBreak(.break_inline, block_inst, func_inst);
......@@ -7973,6 +7977,7 @@ const GenZir = struct {
79737977 is_var_args: bool,
79747978 is_inferred_error: bool,
79757979 is_test: bool,
7980 is_extern: bool,
79767981 }) !Zir.Inst.Ref {
79777982 assert(args.src_node != 0);
79787983 assert(args.ret_ty != .none);
......@@ -8010,7 +8015,8 @@ const GenZir = struct {
80108015 }
80118016
80128017 if (args.cc != .none or args.lib_name != 0 or
8013 args.is_var_args or args.is_test or args.align_inst != .none)
8018 args.is_var_args or args.is_test or args.align_inst != .none or
8019 args.is_extern)
80148020 {
80158021 try astgen.extra.ensureUnusedCapacity(
80168022 gpa,
......@@ -8051,6 +8057,7 @@ const GenZir = struct {
80518057 .has_cc = args.cc != .none,
80528058 .has_align = args.align_inst != .none,
80538059 .is_test = args.is_test,
8060 .is_extern = args.is_extern,
80548061 }),
80558062 .operand = payload_index,
80568063 } },
src/Module.zig+29-14
......@@ -282,13 +282,10 @@ pub const Decl = struct {
282282
283283 pub fn destroy(decl: *Decl, module: *Module) void {
284284 const gpa = module.gpa;
285 log.debug("destroy Decl {*} ({s})", .{ decl, decl.name });
285 log.debug("destroy {*} ({s})", .{ decl, decl.name });
286286 decl.clearName(gpa);
287287 if (decl.has_tv) {
288 if (decl.getFunction()) |func| {
289 func.deinit(gpa);
290 gpa.destroy(func);
291 } else if (decl.val.getTypeNamespace()) |namespace| {
288 if (decl.val.getTypeNamespace()) |namespace| {
292289 if (namespace.getDecl() == decl) {
293290 namespace.clearDecls(module);
294291 }
......@@ -470,7 +467,6 @@ pub const Decl = struct {
470467 /// otherwise null.
471468 pub fn getFunction(decl: *Decl) ?*Fn {
472469 if (!decl.has_tv) return null;
473 if (decl.ty.zigTypeTag() != .Fn) return null;
474470 const func = (decl.val.castTag(.function) orelse return null).data;
475471 if (func.owner_decl != decl) return null;
476472 return func;
......@@ -2960,17 +2956,26 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
29602956 decl.clearValues(gpa);
29612957 }
29622958
2963 const new_variable = try decl_arena.allocator.create(Var);
2964 new_variable.* = .{
2965 .owner_decl = decl,
2966 .init = try decl_tv.val.copy(&decl_arena.allocator),
2967 .is_extern = is_extern,
2968 .is_mutable = is_mutable,
2969 .is_threadlocal = is_threadlocal,
2959 const copied_val = try decl_tv.val.copy(&decl_arena.allocator);
2960 const is_extern_fn = copied_val.tag() == .extern_fn;
2961
2962 // TODO: also avoid allocating this Var structure if `!is_mutable`.
2963 // I think this will require adjusting Sema to copy the value or something
2964 // like that; otherwise it causes use of undefined value when freeing resources.
2965 const decl_val: Value = if (is_extern_fn) copied_val else blk: {
2966 const new_variable = try decl_arena.allocator.create(Var);
2967 new_variable.* = .{
2968 .owner_decl = decl,
2969 .init = copied_val,
2970 .is_extern = is_extern,
2971 .is_mutable = is_mutable,
2972 .is_threadlocal = is_threadlocal,
2973 };
2974 break :blk try Value.Tag.variable.create(&decl_arena.allocator, new_variable);
29702975 };
29712976
29722977 decl.ty = try decl_tv.ty.copy(&decl_arena.allocator);
2973 decl.val = try Value.Tag.variable.create(&decl_arena.allocator, new_variable);
2978 decl.val = decl_val;
29742979 decl.align_val = try align_val.copy(&decl_arena.allocator);
29752980 decl.linksection_val = try linksection_val.copy(&decl_arena.allocator);
29762981 decl.has_tv = true;
......@@ -2984,6 +2989,16 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
29842989 // The scope needs to have the decl in it.
29852990 try mod.analyzeExport(&block_scope.base, export_src, mem.spanZ(decl.name), decl);
29862991 }
2992
2993 if (decl.val.tag() == .extern_fn) {
2994 try mod.comp.bin_file.allocateDeclIndexes(decl);
2995 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl });
2996
2997 if (type_changed and mod.emit_h != null) {
2998 try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl });
2999 }
3000 }
3001
29873002 return type_changed;
29883003 }
29893004}
src/Sema.zig+14-4
......@@ -2819,6 +2819,7 @@ fn zirFunc(
28192819 Value.initTag(.null_value),
28202820 false,
28212821 inferred_error_set,
2822 false,
28222823 src_locs,
28232824 null,
28242825 );
......@@ -2835,6 +2836,7 @@ fn funcCommon(
28352836 align_val: Value,
28362837 var_args: bool,
28372838 inferred_error_set: bool,
2839 is_extern: bool,
28382840 src_locs: Zir.Inst.Func.SrcLocs,
28392841 opt_lib_name: ?[]const u8,
28402842) InnerError!*Inst {
......@@ -2885,10 +2887,6 @@ fn funcCommon(
28852887 });
28862888 };
28872889
2888 if (body_inst == 0) {
2889 return mod.constType(sema.arena, src, fn_ty);
2890 }
2891
28922890 if (opt_lib_name) |lib_name| blk: {
28932891 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };
28942892 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
......@@ -2930,6 +2928,17 @@ fn funcCommon(
29302928 }
29312929 }
29322930
2931 if (is_extern) {
2932 return sema.mod.constInst(sema.arena, src, .{
2933 .ty = fn_ty,
2934 .val = try Value.Tag.extern_fn.create(sema.arena, sema.owner_decl),
2935 });
2936 }
2937
2938 if (body_inst == 0) {
2939 return mod.constType(sema.arena, src, fn_ty);
2940 }
2941
29332942 const is_inline = fn_ty.fnCallingConvention() == .Inline;
29342943 const anal_state: Module.Fn.Analysis = if (is_inline) .inline_only else .queued;
29352944
......@@ -5795,6 +5804,7 @@ fn zirFuncExtended(
57955804 align_val,
57965805 small.is_var_args,
57975806 small.is_inferred_error,
5807 small.is_extern,
57985808 src_locs,
57995809 lib_name,
58005810 );
src/Zir.zig+6-1
......@@ -2217,7 +2217,8 @@ pub const Inst = struct {
22172217 has_cc: bool,
22182218 has_align: bool,
22192219 is_test: bool,
2220 _: u10 = undefined,
2220 is_extern: bool,
2221 _: u9 = undefined,
22212222 };
22222223 };
22232224
......@@ -4103,6 +4104,7 @@ const Writer = struct {
41034104 extra.data.return_type,
41044105 inferred_error_set,
41054106 false,
4107 false,
41064108 .none,
41074109 .none,
41084110 body,
......@@ -4150,6 +4152,7 @@ const Writer = struct {
41504152 extra.data.return_type,
41514153 small.is_inferred_error,
41524154 small.is_var_args,
4155 small.is_extern,
41534156 cc,
41544157 align_inst,
41554158 body,
......@@ -4232,6 +4235,7 @@ const Writer = struct {
42324235 ret_ty: Inst.Ref,
42334236 inferred_error_set: bool,
42344237 var_args: bool,
4238 is_extern: bool,
42354239 cc: Inst.Ref,
42364240 align_inst: Inst.Ref,
42374241 body: []const Inst.Index,
......@@ -4248,6 +4252,7 @@ const Writer = struct {
42484252 try self.writeOptionalInstRef(stream, ", cc=", cc);
42494253 try self.writeOptionalInstRef(stream, ", align=", align_inst);
42504254 try self.writeFlag(stream, ", vargs", var_args);
4255 try self.writeFlag(stream, ", extern", is_extern);
42514256 try self.writeFlag(stream, ", inferror", inferred_error_set);
42524257
42534258 if (body.len == 0) {