authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-05-24 10:01:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-24 20:49:29-04:00
log07472fb453ced756d248fc03493b83a2221ea1a3
treea7f101f29a4df262ea5b8bc7d5b29be34776b62e
parentdb0c30446a8522538ab6088f9a28da951143e5e5

[Stage2/Codegen] Properly handle arch in genCall


1 files changed, 28 insertions(+), 27 deletions(-)

src-self-hosted/codegen.zig+28-27
......@@ -196,35 +196,36 @@ const Function = struct {
196196 }
197197
198198 fn genCall(self: *Function, inst: *ir.Inst.Call) !MCValue {
199 if (inst.args.func.cast(ir.Inst.Constant)) |func_inst| {
200 if (inst.args.args.len != 0) {
201 return self.fail(inst.base.src, "TODO implement call with more than 0 parameters", .{});
202 }
199 switch (self.target.cpu.arch) {
200 .x86_64, .i386 => {
201 if (inst.args.func.cast(ir.Inst.Constant)) |func_inst| {
202 if (inst.args.args.len != 0) {
203 return self.fail(inst.base.src, "TODO implement call with more than 0 parameters", .{});
204 }
203205
204 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
205 const func = func_val.func;
206 const got = &self.bin_file.program_headers.items[self.bin_file.phdr_got_index.?];
207 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
208 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
209 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.offset_table_index * ptr_bytes);
210 // ff 14 25 xx xx xx xx call [addr]
211 try self.code.resize(self.code.items.len + 7);
212 self.code.items[self.code.items.len - 7 ..][0..3].* = [3]u8{ 0xff, 0x14, 0x25 };
213 mem.writeIntLittle(u32, self.code.items[self.code.items.len - 4 ..][0..4], got_addr);
214 const return_type = func.fn_type.fnReturnType();
215 switch (return_type.zigTypeTag()) {
216 .Void => return MCValue{ .none = {} },
217 .NoReturn => return MCValue{ .unreach = {} },
218 else => return self.fail(inst.base.src, "TODO implement fn call with non-void return value", .{}),
206 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
207 const func = func_val.func;
208 const got = &self.bin_file.program_headers.items[self.bin_file.phdr_got_index.?];
209 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
210 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
211 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.offset_table_index * ptr_bytes);
212 // ff 14 25 xx xx xx xx call [addr]
213 try self.code.resize(self.code.items.len + 7);
214 self.code.items[self.code.items.len - 7 ..][0..3].* = [3]u8{ 0xff, 0x14, 0x25 };
215 mem.writeIntLittle(u32, self.code.items[self.code.items.len - 4 ..][0..4], got_addr);
216 const return_type = func.fn_type.fnReturnType();
217 switch (return_type.zigTypeTag()) {
218 .Void => return MCValue{ .none = {} },
219 .NoReturn => return MCValue{ .unreach = {} },
220 else => return self.fail(inst.base.src, "TODO implement fn call with non-void return value", .{}),
221 }
222 } else {
223 return self.fail(inst.base.src, "TODO implement calling weird function values", .{});
224 }
225 } else {
226 return self.fail(inst.base.src, "TODO implement calling runtime known function pointer", .{});
219227 }
220 } else {
221 return self.fail(inst.base.src, "TODO implement calling weird function values", .{});
222 }
223 } else {
224 return self.fail(inst.base.src, "TODO implement calling runtime known function pointer", .{});
225 }
226
227 switch (self.target.cpu.arch) {
228 },
228229 else => return self.fail(inst.base.src, "TODO implement call for {}", .{self.target.cpu.arch}),
229230 }
230231 }