authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-08 23:16:50+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-13 14:51:23-08:00
log5487dd13ea23ad7e547995b9a088ba37bfe17737
treed1bc46aed5d4db149d32cb4fca0406f86f1f1790
parenta021c7b1b2428ecda85e79e281d43fa1c92f8c94

stage2: lay the groundwork in prep for extern fn

This commit lays the groundwork in preparation for implementing handling of extern functions in various backends.

6 files changed, 74 insertions(+), 6 deletions(-)

src/Module.zig+43-2
......@@ -22,6 +22,7 @@ const ast = std.zig.ast;
2222const trace = @import("tracy.zig").trace;
2323const astgen = @import("astgen.zig");
2424const zir_sema = @import("zir_sema.zig");
25const target_util = @import("target.zig");
2526
2627const default_eval_branch_quota = 1000;
2728
......@@ -1109,8 +1110,48 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11091110 if (fn_proto.getVarArgsToken()) |var_args_token| {
11101111 return self.failTok(&fn_type_scope.base, var_args_token, "TODO implement var args", .{});
11111112 }
1112 if (fn_proto.getLibName()) |lib_name| {
1113 return self.failNode(&fn_type_scope.base, lib_name, "TODO implement function library name", .{});
1113 if (fn_proto.getLibName()) |lib_name| blk: {
1114 const lib_name_str = mem.trim(u8, tree.tokenSlice(lib_name.firstToken()), "\""); // TODO: call identifierTokenString
1115 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name_str});
1116 const target = self.comp.getTarget();
1117 if (target_util.is_libc_lib_name(target, lib_name_str)) {
1118 if (!self.comp.bin_file.options.link_libc) {
1119 return self.failNode(
1120 &fn_type_scope.base,
1121 lib_name,
1122 "dependency on libc must be explicitly specified in the build command",
1123 .{},
1124 );
1125 }
1126 break :blk;
1127 }
1128 if (target_util.is_libcpp_lib_name(target, lib_name_str)) {
1129 if (!self.comp.bin_file.options.link_libcpp) {
1130 return self.failNode(
1131 &fn_type_scope.base,
1132 lib_name,
1133 "dependency on libc++ must be explicitly specified in the build command",
1134 .{},
1135 );
1136 }
1137 break :blk;
1138 }
1139 if (!target.isWasm() and !self.comp.bin_file.options.pic) {
1140 return self.failNode(
1141 &fn_type_scope.base,
1142 lib_name,
1143 "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by `-l{s}` or `-fPIC`.",
1144 .{ lib_name, lib_name },
1145 );
1146 }
1147 self.comp.stage1AddLinkLib(lib_name_str) catch |err| {
1148 return self.failNode(
1149 &fn_type_scope.base,
1150 lib_name,
1151 "unable to add link lib '{s}': {s}",
1152 .{ lib_name, @errorName(err) },
1153 );
1154 };
11141155 }
11151156 if (fn_proto.getAlignExpr()) |align_expr| {
11161157 return self.failNode(&fn_type_scope.base, align_expr, "TODO implement function align expression", .{});
src/codegen.zig+12
......@@ -1602,6 +1602,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
16021602 try self.code.ensureCapacity(self.code.items.len + 7);
16031603 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });
16041604 mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), got_addr);
1605 } else if (func_value.castTag(.extern_fn)) |_| {
1606 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
16051607 } else {
16061608 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
16071609 }
......@@ -1628,6 +1630,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
16281630
16291631 try self.genSetReg(inst.base.src, .ra, .{ .memory = got_addr });
16301632 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.jalr(.ra, 0, .ra).toU32());
1633 } else if (func_value.castTag(.extern_fn)) |_| {
1634 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
16311635 } else {
16321636 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
16331637 }
......@@ -1672,6 +1676,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
16721676 else => return self.fail(inst.base.src, "TODO implement fn call with non-void return value", .{}),
16731677 }
16741678 }
1679 } else if (func_value.castTag(.extern_fn)) |_| {
1680 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
16751681 } else {
16761682 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
16771683 }
......@@ -1733,6 +1739,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17331739 writeInt(u32, try self.code.addManyAsArray(4), Instruction.mov(.al, .lr, Instruction.Operand.reg(.pc, Instruction.Operand.Shift.none)).toU32());
17341740 writeInt(u32, try self.code.addManyAsArray(4), Instruction.bx(.al, .lr).toU32());
17351741 }
1742 } else if (func_value.castTag(.extern_fn)) |_| {
1743 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
17361744 } else {
17371745 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
17381746 }
......@@ -1787,6 +1795,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17871795 try self.genSetReg(inst.base.src, .x30, .{ .memory = got_addr });
17881796
17891797 writeInt(u32, try self.code.addManyAsArray(4), Instruction.blr(.x30).toU32());
1798 } else if (func_value.castTag(.extern_fn)) |_| {
1799 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
17901800 } else {
17911801 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
17921802 }
......@@ -1849,6 +1859,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
18491859 },
18501860 else => unreachable, // unsupported architecture on MachO
18511861 }
1862 } else if (func_value.castTag(.extern_fn)) |_| {
1863 return self.fail(inst.base.src, "TODO implement calling extern functions", .{});
18521864 } else {
18531865 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
18541866 }
src/link/Coff.zig+5-1
......@@ -662,10 +662,14 @@ pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {
662662 if (build_options.have_llvm)
663663 if (self.llvm_ir_module) |llvm_ir_module| return try llvm_ir_module.updateDecl(module, decl);
664664
665 const typed_value = decl.typed_value.most_recent.typed_value;
666 if (typed_value.val.tag() == .extern_fn) {
667 return; // TODO Should we do more when front-end analyzed extern decl?
668 }
669
665670 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
666671 defer code_buffer.deinit();
667672
668 const typed_value = decl.typed_value.most_recent.typed_value;
669673 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .none);
670674 const code = switch (res) {
671675 .externally_managed => |x| x,
src/link/Elf.zig+5-1
......@@ -2157,6 +2157,11 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
21572157 if (build_options.have_llvm)
21582158 if (self.llvm_ir_module) |llvm_ir_module| return try llvm_ir_module.updateDecl(module, decl);
21592159
2160 const typed_value = decl.typed_value.most_recent.typed_value;
2161 if (typed_value.val.tag() == .extern_fn) {
2162 return; // TODO Should we do more when front-end analyzed extern decl?
2163 }
2164
21602165 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
21612166 defer code_buffer.deinit();
21622167
......@@ -2175,7 +2180,6 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
21752180 dbg_info_type_relocs.deinit(self.base.allocator);
21762181 }
21772182
2178 const typed_value = decl.typed_value.most_recent.typed_value;
21792183 const is_fn: bool = switch (typed_value.ty.zigTypeTag()) {
21802184 .Fn => true,
21812185 else => false,
src/link/MachO.zig+5-1
......@@ -1118,6 +1118,11 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
11181118 const tracy = trace(@src());
11191119 defer tracy.end();
11201120
1121 const typed_value = decl.typed_value.most_recent.typed_value;
1122 if (typed_value.val.tag() == .extern_fn) {
1123 return; // TODO Should we do more when front-end analyzed extern decl?
1124 }
1125
11211126 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
11221127 defer code_buffer.deinit();
11231128
......@@ -1134,7 +1139,6 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
11341139 }
11351140 }
11361141
1137 const typed_value = decl.typed_value.most_recent.typed_value;
11381142 const res = if (debug_buffers) |*dbg|
11391143 try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .{
11401144 .dwarf = .{
src/link/Wasm.zig+4-1
......@@ -100,8 +100,11 @@ pub fn deinit(self: *Wasm) void {
100100// Generate code for the Decl, storing it in memory to be later written to
101101// the file on flush().
102102pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void {
103 if (decl.typed_value.most_recent.typed_value.ty.zigTypeTag() != .Fn)
103 const typed_value = decl.typed_value.most_recent.typed_value;
104 if (typed_value.ty.zigTypeTag() != .Fn)
104105 return error.TODOImplementNonFnDeclsForWasm;
106 if (typed_value.val.tag() == .extern_fn)
107 return error.TODOImplementExternFnDeclsForWasm;
105108
106109 if (decl.fn_link.wasm) |*fn_data| {
107110 fn_data.functype.items.len = 0;