authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-12-23 01:30:25+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-12-23 01:30:25+01:00
log603f826779bf0cd7fcd3b6f27a7ee6df8d158d8a
tree04e6477604f864ef556c0a64039bbadcb8c949ba
parent9078cb0197e09de701fbc9d5941358b7910f38b2

stage2: migrate push/pop r/m64 to new lowering mechanism


1 files changed, 11 insertions(+), 23 deletions(-)

src/arch/x86_64/Emit.zig+11-23
......@@ -113,7 +113,8 @@ pub fn emitMir(emit: *Emit) InnerError!void {
113113
114114 .imul_complex => try emit.mirIMulComplex(inst),
115115
116 .push, .pop => try emit.mirPushPop(tag, inst),
116 .push => try emit.mirPushPop(.push, inst),
117 .pop => try emit.mirPushPop(.pop, inst),
117118
118119 .jmp => try emit.mirJmpCall(.jmp_near, inst),
119120 .call => try emit.mirJmpCall(.call_near, inst),
......@@ -198,7 +199,7 @@ fn mirSyscall(emit: *Emit) InnerError!void {
198199 encoder.opcode_2byte(0x0f, 0x05);
199200}
200201
201fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!void {
202fn mirPushPop(emit: *Emit, tag: Tag, inst: Mir.Inst.Index) InnerError!void {
202203 const ops = Mir.Ops.decode(emit.mir.instructions.items(.ops)[inst]);
203204 switch (ops.flags) {
204205 0b00 => {
......@@ -217,25 +218,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v
217218 0b01 => {
218219 // PUSH/POP r/m64
219220 const imm = emit.mir.instructions.items(.data)[inst].imm;
220 const opc: u8 = switch (tag) {
221 .push => 0xff,
222 .pop => 0x8f,
223 else => unreachable,
224 };
225 const modrm_ext: u3 = switch (tag) {
226 .push => 0x6,
227 .pop => 0x0,
228 else => unreachable,
229 };
230 const encoder = try Encoder.init(emit.code, 6);
231 encoder.opcode_1byte(opc);
232 if (math.cast(i8, imm)) |imm_i8| {
233 encoder.modRm_indirectDisp8(modrm_ext, ops.reg1.lowId());
234 encoder.imm8(@intCast(i8, imm_i8));
235 } else |_| {
236 encoder.modRm_indirectDisp32(modrm_ext, ops.reg1.lowId());
237 encoder.imm32(imm);
238 }
221 return lowerToMEnc(tag, RegisterOrMemory.mem(ops.reg1, imm), emit.code);
239222 },
240223 0b10 => {
241224 // PUSH imm32
......@@ -255,7 +238,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v
255238 0b11 => unreachable,
256239 }
257240}
258fn mirPushPopRegsFromCalleePreservedRegs(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!void {
241fn mirPushPopRegsFromCalleePreservedRegs(emit: *Emit, tag: Tag, inst: Mir.Inst.Index) InnerError!void {
259242 const callee_preserved_regs = bits.callee_preserved_regs;
260243 // PUSH/POP reg
261244 const opc: u8 = switch (tag) {
......@@ -532,6 +515,8 @@ const Tag = enum {
532515 lea,
533516 jmp_near,
534517 call_near,
518 push,
519 pop,
535520};
536521
537522const Encoding = enum {
......@@ -568,7 +553,8 @@ inline fn getOpCode(tag: Tag, enc: Encoding) ?u8 {
568553 else => null,
569554 },
570555 .m => return switch (tag) {
571 .jmp_near, .call_near => 0xff,
556 .jmp_near, .call_near, .push => 0xff,
557 .pop => 0x8f,
572558 else => null,
573559 },
574560 .mi => return switch (tag) {
......@@ -629,6 +615,8 @@ inline fn getModRmExt(tag: Tag) ?u3 {
629615 .mov => 0x0,
630616 .jmp_near => 0x4,
631617 .call_near => 0x2,
618 .push => 0x6,
619 .pop => 0x0,
632620 else => null,
633621 };
634622}