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 {...@@ -113,7 +113,8 @@ pub fn emitMir(emit: *Emit) InnerError!void {
113113
114 .imul_complex => try emit.mirIMulComplex(inst),114 .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
118 .jmp => try emit.mirJmpCall(.jmp_near, inst),119 .jmp => try emit.mirJmpCall(.jmp_near, inst),
119 .call => try emit.mirJmpCall(.call_near, inst),120 .call => try emit.mirJmpCall(.call_near, inst),
...@@ -198,7 +199,7 @@ fn mirSyscall(emit: *Emit) InnerError!void {...@@ -198,7 +199,7 @@ fn mirSyscall(emit: *Emit) InnerError!void {
198 encoder.opcode_2byte(0x0f, 0x05);199 encoder.opcode_2byte(0x0f, 0x05);
199}200}
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 {
202 const ops = Mir.Ops.decode(emit.mir.instructions.items(.ops)[inst]);203 const ops = Mir.Ops.decode(emit.mir.instructions.items(.ops)[inst]);
203 switch (ops.flags) {204 switch (ops.flags) {
204 0b00 => {205 0b00 => {
...@@ -217,25 +218,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v...@@ -217,25 +218,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v
217 0b01 => {218 0b01 => {
218 // PUSH/POP r/m64219 // PUSH/POP r/m64
219 const imm = emit.mir.instructions.items(.data)[inst].imm;220 const imm = emit.mir.instructions.items(.data)[inst].imm;
220 const opc: u8 = switch (tag) {221 return lowerToMEnc(tag, RegisterOrMemory.mem(ops.reg1, imm), emit.code);
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 }
239 },222 },
240 0b10 => {223 0b10 => {
241 // PUSH imm32224 // PUSH imm32
...@@ -255,7 +238,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v...@@ -255,7 +238,7 @@ fn mirPushPop(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) InnerError!v
255 0b11 => unreachable,238 0b11 => unreachable,
256 }239 }
257}240}
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 {
259 const callee_preserved_regs = bits.callee_preserved_regs;242 const callee_preserved_regs = bits.callee_preserved_regs;
260 // PUSH/POP reg243 // PUSH/POP reg
261 const opc: u8 = switch (tag) {244 const opc: u8 = switch (tag) {
...@@ -532,6 +515,8 @@ const Tag = enum {...@@ -532,6 +515,8 @@ const Tag = enum {
532 lea,515 lea,
533 jmp_near,516 jmp_near,
534 call_near,517 call_near,
518 push,
519 pop,
535};520};
536521
537const Encoding = enum {522const Encoding = enum {
...@@ -568,7 +553,8 @@ inline fn getOpCode(tag: Tag, enc: Encoding) ?u8 {...@@ -568,7 +553,8 @@ inline fn getOpCode(tag: Tag, enc: Encoding) ?u8 {
568 else => null,553 else => null,
569 },554 },
570 .m => return switch (tag) {555 .m => return switch (tag) {
571 .jmp_near, .call_near => 0xff,556 .jmp_near, .call_near, .push => 0xff,
557 .pop => 0x8f,
572 else => null,558 else => null,
573 },559 },
574 .mi => return switch (tag) {560 .mi => return switch (tag) {
...@@ -629,6 +615,8 @@ inline fn getModRmExt(tag: Tag) ?u3 {...@@ -629,6 +615,8 @@ inline fn getModRmExt(tag: Tag) ?u3 {
629 .mov => 0x0,615 .mov => 0x0,
630 .jmp_near => 0x4,616 .jmp_near => 0x4,
631 .call_near => 0x2,617 .call_near => 0x2,
618 .push => 0x6,
619 .pop => 0x0,
632 else => null,620 else => null,
633 };621 };
634}622}