| ... | ... | @@ -1,478 +0,0 @@ |
| 1 | | //! This file contains the functionality for print x86_64 MIR in a debug way, interleaved with AIR |
| 2 | | |
| 3 | | const Print = @This(); |
| 4 | | |
| 5 | | const std = @import("std"); |
| 6 | | const assert = std.debug.assert; |
| 7 | | const bits = @import("bits.zig"); |
| 8 | | const abi = @import("abi.zig"); |
| 9 | | const leb128 = std.leb; |
| 10 | | const link = @import("../../link.zig"); |
| 11 | | const log = std.log.scoped(.codegen); |
| 12 | | const math = std.math; |
| 13 | | const mem = std.mem; |
| 14 | | |
| 15 | | const Air = @import("../../Air.zig"); |
| 16 | | const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; |
| 17 | | const DW = std.dwarf; |
| 18 | | const Encoder = bits.Encoder; |
| 19 | | const ErrorMsg = Module.ErrorMsg; |
| 20 | | const MCValue = @import("CodeGen.zig").MCValue; |
| 21 | | const Mir = @import("Mir.zig"); |
| 22 | | const Module = @import("../../Module.zig"); |
| 23 | | const Instruction = bits.Instruction; |
| 24 | | const Register = bits.Register; |
| 25 | | const Type = @import("../../type.zig").Type; |
| 26 | | const fmtIntSizeBin = std.fmt.fmtIntSizeBin; |
| 27 | | |
| 28 | | mir: Mir, |
| 29 | | bin_file: *link.File, |
| 30 | | |
| 31 | | pub fn printMir(print: *const Print, w: anytype, mir_to_air_map: std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index), air: Air) !void { |
| 32 | | const instruction_bytes = print.mir.instructions.len * |
| 33 | | // Here we don't use @sizeOf(Mir.Inst.Data) because it would include |
| 34 | | // the debug safety tag but we want to measure release size. |
| 35 | | (@sizeOf(Mir.Inst.Tag) + 2 + 8); |
| 36 | | const extra_bytes = print.mir.extra.len * @sizeOf(u32); |
| 37 | | const total_bytes = @sizeOf(Mir) + instruction_bytes + extra_bytes; |
| 38 | | |
| 39 | | // zig fmt: off |
| 40 | | std.debug.print( |
| 41 | | \\# Total MIR bytes: {} |
| 42 | | \\# MIR Instructions: {d} ({}) |
| 43 | | \\# MIR Extra Data: {d} ({}) |
| 44 | | \\ |
| 45 | | , .{ |
| 46 | | fmtIntSizeBin(total_bytes), |
| 47 | | print.mir.instructions.len, fmtIntSizeBin(instruction_bytes), |
| 48 | | print.mir.extra.len, fmtIntSizeBin(extra_bytes), |
| 49 | | }); |
| 50 | | // zig fmt: on |
| 51 | | const mir_tags = print.mir.instructions.items(.tag); |
| 52 | | |
| 53 | | for (mir_tags) |tag, index| { |
| 54 | | const inst = @intCast(u32, index); |
| 55 | | if (mir_to_air_map.get(inst)) |air_index| { |
| 56 | | try w.print("air index %{} ({}) for following mir inst(s)\n", .{ air_index, air.instructions.items(.tag)[air_index] }); |
| 57 | | } |
| 58 | | try w.writeAll(" "); |
| 59 | | switch (tag) { |
| 60 | | .adc => try print.mirArith(.adc, inst, w), |
| 61 | | .add => try print.mirArith(.add, inst, w), |
| 62 | | .sub => try print.mirArith(.sub, inst, w), |
| 63 | | .xor => try print.mirArith(.xor, inst, w), |
| 64 | | .@"and" => try print.mirArith(.@"and", inst, w), |
| 65 | | .@"or" => try print.mirArith(.@"or", inst, w), |
| 66 | | .sbb => try print.mirArith(.sbb, inst, w), |
| 67 | | .cmp => try print.mirArith(.cmp, inst, w), |
| 68 | | .mov => try print.mirArith(.mov, inst, w), |
| 69 | | |
| 70 | | .adc_mem_imm => try print.mirArithMemImm(.adc, inst, w), |
| 71 | | .add_mem_imm => try print.mirArithMemImm(.add, inst, w), |
| 72 | | .sub_mem_imm => try print.mirArithMemImm(.sub, inst, w), |
| 73 | | .xor_mem_imm => try print.mirArithMemImm(.xor, inst, w), |
| 74 | | .and_mem_imm => try print.mirArithMemImm(.@"and", inst, w), |
| 75 | | .or_mem_imm => try print.mirArithMemImm(.@"or", inst, w), |
| 76 | | .sbb_mem_imm => try print.mirArithMemImm(.sbb, inst, w), |
| 77 | | .cmp_mem_imm => try print.mirArithMemImm(.cmp, inst, w), |
| 78 | | .mov_mem_imm => try print.mirArithMemImm(.mov, inst, w), |
| 79 | | |
| 80 | | .adc_scale_src => try print.mirArithScaleSrc(.adc, inst, w), |
| 81 | | .add_scale_src => try print.mirArithScaleSrc(.add, inst, w), |
| 82 | | .sub_scale_src => try print.mirArithScaleSrc(.sub, inst, w), |
| 83 | | .xor_scale_src => try print.mirArithScaleSrc(.xor, inst, w), |
| 84 | | .and_scale_src => try print.mirArithScaleSrc(.@"and", inst, w), |
| 85 | | .or_scale_src => try print.mirArithScaleSrc(.@"or", inst, w), |
| 86 | | .sbb_scale_src => try print.mirArithScaleSrc(.sbb, inst, w), |
| 87 | | .cmp_scale_src => try print.mirArithScaleSrc(.cmp, inst, w), |
| 88 | | .mov_scale_src => try print.mirArithScaleSrc(.mov, inst, w), |
| 89 | | |
| 90 | | .adc_scale_dst => try print.mirArithScaleDst(.adc, inst, w), |
| 91 | | .add_scale_dst => try print.mirArithScaleDst(.add, inst, w), |
| 92 | | .sub_scale_dst => try print.mirArithScaleDst(.sub, inst, w), |
| 93 | | .xor_scale_dst => try print.mirArithScaleDst(.xor, inst, w), |
| 94 | | .and_scale_dst => try print.mirArithScaleDst(.@"and", inst, w), |
| 95 | | .or_scale_dst => try print.mirArithScaleDst(.@"or", inst, w), |
| 96 | | .sbb_scale_dst => try print.mirArithScaleDst(.sbb, inst, w), |
| 97 | | .cmp_scale_dst => try print.mirArithScaleDst(.cmp, inst, w), |
| 98 | | .mov_scale_dst => try print.mirArithScaleDst(.mov, inst, w), |
| 99 | | |
| 100 | | .adc_scale_imm => try print.mirArithScaleImm(.adc, inst, w), |
| 101 | | .add_scale_imm => try print.mirArithScaleImm(.add, inst, w), |
| 102 | | .sub_scale_imm => try print.mirArithScaleImm(.sub, inst, w), |
| 103 | | .xor_scale_imm => try print.mirArithScaleImm(.xor, inst, w), |
| 104 | | .and_scale_imm => try print.mirArithScaleImm(.@"and", inst, w), |
| 105 | | .or_scale_imm => try print.mirArithScaleImm(.@"or", inst, w), |
| 106 | | .sbb_scale_imm => try print.mirArithScaleImm(.sbb, inst, w), |
| 107 | | .cmp_scale_imm => try print.mirArithScaleImm(.cmp, inst, w), |
| 108 | | .mov_scale_imm => try print.mirArithScaleImm(.mov, inst, w), |
| 109 | | |
| 110 | | .adc_mem_index_imm => try print.mirArithMemIndexImm(.adc, inst, w), |
| 111 | | .add_mem_index_imm => try print.mirArithMemIndexImm(.add, inst, w), |
| 112 | | .sub_mem_index_imm => try print.mirArithMemIndexImm(.sub, inst, w), |
| 113 | | .xor_mem_index_imm => try print.mirArithMemIndexImm(.xor, inst, w), |
| 114 | | .and_mem_index_imm => try print.mirArithMemIndexImm(.@"and", inst, w), |
| 115 | | .or_mem_index_imm => try print.mirArithMemIndexImm(.@"or", inst, w), |
| 116 | | .sbb_mem_index_imm => try print.mirArithMemIndexImm(.sbb, inst, w), |
| 117 | | .cmp_mem_index_imm => try print.mirArithMemIndexImm(.cmp, inst, w), |
| 118 | | .mov_mem_index_imm => try print.mirArithMemIndexImm(.mov, inst, w), |
| 119 | | |
| 120 | | .movabs => try print.mirMovabs(inst, w), |
| 121 | | |
| 122 | | .lea => try print.mirLea(inst, w), |
| 123 | | .lea_pie => try print.mirLeaPie(inst, w), |
| 124 | | |
| 125 | | .imul_complex => try print.mirIMulComplex(inst, w), |
| 126 | | |
| 127 | | .push => try print.mirPushPop(.push, inst, w), |
| 128 | | .pop => try print.mirPushPop(.pop, inst, w), |
| 129 | | |
| 130 | | .jmp => try print.mirJmpCall(.jmp, inst, w), |
| 131 | | .call => try print.mirJmpCall(.call, inst, w), |
| 132 | | |
| 133 | | .cond_jmp_greater_less => try print.mirCondJmp(.cond_jmp_greater_less, inst, w), |
| 134 | | .cond_jmp_above_below => try print.mirCondJmp(.cond_jmp_above_below, inst, w), |
| 135 | | .cond_jmp_eq_ne => try print.mirCondJmp(.cond_jmp_eq_ne, inst, w), |
| 136 | | |
| 137 | | .cond_set_byte_greater_less => try print.mirCondSetByte(.cond_set_byte_greater_less, inst, w), |
| 138 | | .cond_set_byte_above_below => try print.mirCondSetByte(.cond_set_byte_above_below, inst, w), |
| 139 | | .cond_set_byte_eq_ne => try print.mirCondSetByte(.cond_set_byte_eq_ne, inst, w), |
| 140 | | |
| 141 | | .@"test" => try print.mirTest(inst, w), |
| 142 | | |
| 143 | | .brk => try w.writeAll("brk\n"), |
| 144 | | .ret => try w.writeAll("ret\n"), |
| 145 | | .nop => try w.writeAll("nop\n"), |
| 146 | | .syscall => try w.writeAll("syscall\n"), |
| 147 | | |
| 148 | | .call_extern => try print.mirCallExtern(inst, w), |
| 149 | | |
| 150 | | .dbg_line, .dbg_prologue_end, .dbg_epilogue_begin => try w.print("{s}\n", .{@tagName(tag)}), |
| 151 | | |
| 152 | | .push_regs_from_callee_preserved_regs => try print.mirPushPopRegsFromCalleePreservedRegs(.push, inst, w), |
| 153 | | .pop_regs_from_callee_preserved_regs => try print.mirPushPopRegsFromCalleePreservedRegs(.pop, inst, w), |
| 154 | | |
| 155 | | else => { |
| 156 | | try w.print("TODO emit asm for {s}\n", .{@tagName(tag)}); |
| 157 | | }, |
| 158 | | } |
| 159 | | } |
| 160 | | } |
| 161 | | |
| 162 | | fn mirPushPop(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 163 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 164 | | switch (ops.flags) { |
| 165 | | 0b00 => { |
| 166 | | // PUSH/POP reg |
| 167 | | try w.print("{s} {s}", .{ @tagName(tag), @tagName(ops.reg1) }); |
| 168 | | }, |
| 169 | | 0b01 => { |
| 170 | | // PUSH/POP r/m64 |
| 171 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 172 | | try w.print("{s} [{s} + {d}]", .{ @tagName(tag), @tagName(ops.reg1), imm }); |
| 173 | | }, |
| 174 | | 0b10 => { |
| 175 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 176 | | // PUSH imm32 |
| 177 | | assert(tag == .push); |
| 178 | | try w.print("{s} {d}", .{ @tagName(tag), imm }); |
| 179 | | }, |
| 180 | | 0b11 => unreachable, |
| 181 | | } |
| 182 | | try w.writeByte('\n'); |
| 183 | | } |
| 184 | | fn mirPushPopRegsFromCalleePreservedRegs(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 185 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 186 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 187 | | const data = print.mir.extraData(Mir.RegsToPushOrPop, payload).data; |
| 188 | | const regs = data.regs; |
| 189 | | var disp: u32 = data.disp + 8; |
| 190 | | if (regs == 0) return w.writeAll("no regs from callee_preserved_regs\n"); |
| 191 | | var printed_first_reg = false; |
| 192 | | for (abi.callee_preserved_regs) |reg, i| { |
| 193 | | if ((regs >> @intCast(u5, i)) & 1 == 0) continue; |
| 194 | | if (printed_first_reg) try w.writeAll(" "); |
| 195 | | printed_first_reg = true; |
| 196 | | if (tag == .push) { |
| 197 | | try w.print("mov qword ptr [{s} + {d}], {s}", .{ |
| 198 | | @tagName(ops.reg1), |
| 199 | | @bitCast(u32, -@intCast(i32, disp)), |
| 200 | | @tagName(reg.to64()), |
| 201 | | }); |
| 202 | | } else { |
| 203 | | try w.print("mov {s}, qword ptr [{s} + {d}]", .{ |
| 204 | | @tagName(reg.to64()), |
| 205 | | @tagName(ops.reg1), |
| 206 | | @bitCast(u32, -@intCast(i32, disp)), |
| 207 | | }); |
| 208 | | } |
| 209 | | disp += 8; |
| 210 | | try w.writeByte('\n'); |
| 211 | | } |
| 212 | | } |
| 213 | | |
| 214 | | fn mirJmpCall(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 215 | | try w.print("{s} ", .{@tagName(tag)}); |
| 216 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 217 | | const flag = @truncate(u1, ops.flags); |
| 218 | | if (flag == 0) { |
| 219 | | return w.writeAll("TODO target\n"); |
| 220 | | } |
| 221 | | if (ops.reg1 == .none) { |
| 222 | | // JMP/CALL [imm] |
| 223 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 224 | | try w.print("[{x}]\n", .{imm}); |
| 225 | | return; |
| 226 | | } |
| 227 | | // JMP/CALL reg |
| 228 | | try w.print("{s}\n", .{@tagName(ops.reg1)}); |
| 229 | | } |
| 230 | | |
| 231 | | fn mirCondJmp(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 232 | | _ = print; |
| 233 | | _ = tag; |
| 234 | | _ = inst; |
| 235 | | try w.writeAll("TODO print mirCondJmp\n"); |
| 236 | | } |
| 237 | | |
| 238 | | fn mirCondSetByte(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 239 | | _ = tag; |
| 240 | | _ = inst; |
| 241 | | _ = print; |
| 242 | | try w.writeAll("TODO print mirCondSetByte\n"); |
| 243 | | } |
| 244 | | |
| 245 | | fn mirTest(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 246 | | _ = print; |
| 247 | | _ = inst; |
| 248 | | try w.writeAll("TODO print mirTest\n"); |
| 249 | | } |
| 250 | | |
| 251 | | fn mirArith(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 252 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 253 | | try w.writeAll(@tagName(tag)); |
| 254 | | try w.writeByte(' '); |
| 255 | | switch (ops.flags) { |
| 256 | | 0b00 => { |
| 257 | | if (ops.reg2 == .none) { |
| 258 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 259 | | try w.print("{s}, {d}", .{ @tagName(ops.reg1), imm }); |
| 260 | | } else try w.print("{s}, {s}", .{ @tagName(ops.reg1), @tagName(ops.reg2) }); |
| 261 | | }, |
| 262 | | 0b01 => { |
| 263 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 264 | | if (ops.reg2 == .none) { |
| 265 | | try w.print("{s}, ", .{@tagName(ops.reg1)}); |
| 266 | | switch (ops.reg1.size()) { |
| 267 | | 8 => try w.print("byte ptr ", .{}), |
| 268 | | 16 => try w.print("word ptr ", .{}), |
| 269 | | 32 => try w.print("dword ptr ", .{}), |
| 270 | | 64 => try w.print("qword ptr ", .{}), |
| 271 | | else => unreachable, |
| 272 | | } |
| 273 | | try w.print("[ds:{d}]", .{imm}); |
| 274 | | } else { |
| 275 | | try w.print("{s}, ", .{@tagName(ops.reg1)}); |
| 276 | | switch (ops.reg1.size()) { |
| 277 | | 8 => try w.print("byte ptr ", .{}), |
| 278 | | 16 => try w.print("word ptr ", .{}), |
| 279 | | 32 => try w.print("dword ptr ", .{}), |
| 280 | | 64 => try w.print("qword ptr ", .{}), |
| 281 | | else => unreachable, |
| 282 | | } |
| 283 | | try w.print("[{s} + {d}]", .{ @tagName(ops.reg2), imm }); |
| 284 | | } |
| 285 | | }, |
| 286 | | 0b10 => { |
| 287 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 288 | | if (ops.reg2 == .none) { |
| 289 | | try w.writeAll("unused variant"); |
| 290 | | } else { |
| 291 | | switch (ops.reg2.size()) { |
| 292 | | 8 => try w.print("byte ptr ", .{}), |
| 293 | | 16 => try w.print("word ptr ", .{}), |
| 294 | | 32 => try w.print("dword ptr ", .{}), |
| 295 | | 64 => try w.print("qword ptr ", .{}), |
| 296 | | else => unreachable, |
| 297 | | } |
| 298 | | try w.print("[{s} + {d}], {s}", .{ @tagName(ops.reg1), imm, @tagName(ops.reg2) }); |
| 299 | | } |
| 300 | | }, |
| 301 | | 0b11 => { |
| 302 | | try w.writeAll("unused variant"); |
| 303 | | }, |
| 304 | | } |
| 305 | | try w.writeByte('\n'); |
| 306 | | } |
| 307 | | |
| 308 | | fn mirArithMemImm(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 309 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 310 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 311 | | const imm_pair = print.mir.extraData(Mir.ImmPair, payload).data; |
| 312 | | try w.print("{s} ", .{@tagName(tag)}); |
| 313 | | switch (ops.flags) { |
| 314 | | 0b00 => try w.print("byte ptr ", .{}), |
| 315 | | 0b01 => try w.print("word ptr ", .{}), |
| 316 | | 0b10 => try w.print("dword ptr ", .{}), |
| 317 | | 0b11 => try w.print("qword ptr ", .{}), |
| 318 | | } |
| 319 | | try w.print("[{s} + {d}], {d}\n", .{ @tagName(ops.reg1), imm_pair.dest_off, imm_pair.operand }); |
| 320 | | } |
| 321 | | |
| 322 | | fn mirArithScaleSrc(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 323 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 324 | | const scale = ops.flags; |
| 325 | | // OP reg1, [reg2 + scale*rcx + imm32] |
| 326 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 327 | | try w.print("{s} {s}, [{s} + {d}*rcx + {d}]\n", .{ @tagName(tag), @tagName(ops.reg1), @tagName(ops.reg2), scale, imm }); |
| 328 | | } |
| 329 | | |
| 330 | | fn mirArithScaleDst(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 331 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 332 | | const scale = ops.flags; |
| 333 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 334 | | |
| 335 | | if (ops.reg2 == .none) { |
| 336 | | // OP [reg1 + scale*rax + 0], imm32 |
| 337 | | try w.print("{s} [{s} + {d}*rax + 0], {d}\n", .{ @tagName(tag), @tagName(ops.reg1), scale, imm }); |
| 338 | | } |
| 339 | | |
| 340 | | // OP [reg1 + scale*rax + imm32], reg2 |
| 341 | | try w.print("{s} [{s} + {d}*rax + {d}], {s}\n", .{ @tagName(tag), @tagName(ops.reg1), scale, imm, @tagName(ops.reg2) }); |
| 342 | | } |
| 343 | | |
| 344 | | fn mirArithScaleImm(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 345 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 346 | | const scale = ops.flags; |
| 347 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 348 | | const imm_pair = print.mir.extraData(Mir.ImmPair, payload).data; |
| 349 | | try w.print("{s} [{s} + {d}*rax + {d}], {d}\n", .{ @tagName(tag), @tagName(ops.reg1), scale, imm_pair.dest_off, imm_pair.operand }); |
| 350 | | } |
| 351 | | |
| 352 | | fn mirArithMemIndexImm(print: *const Print, tag: Mir.Inst.Tag, inst: Mir.Inst.Index, w: anytype) !void { |
| 353 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 354 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 355 | | const imm_pair = print.mir.extraData(Mir.ImmPair, payload).data; |
| 356 | | try w.print("{s} ", .{@tagName(tag)}); |
| 357 | | switch (ops.flags) { |
| 358 | | 0b00 => try w.print("byte ptr ", .{}), |
| 359 | | 0b01 => try w.print("word ptr ", .{}), |
| 360 | | 0b10 => try w.print("dword ptr ", .{}), |
| 361 | | 0b11 => try w.print("qword ptr ", .{}), |
| 362 | | } |
| 363 | | try w.print("[{s} + 1*rax + {d}], {d}\n", .{ @tagName(ops.reg1), imm_pair.dest_off, imm_pair.operand }); |
| 364 | | } |
| 365 | | |
| 366 | | fn mirMovabs(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 367 | | const tag = print.mir.instructions.items(.tag)[inst]; |
| 368 | | assert(tag == .movabs); |
| 369 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 370 | | |
| 371 | | const is_64 = ops.reg1.size() == 64; |
| 372 | | const imm: i128 = if (is_64) blk: { |
| 373 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 374 | | const imm64 = print.mir.extraData(Mir.Imm64, payload).data; |
| 375 | | break :blk imm64.decode(); |
| 376 | | } else print.mir.instructions.items(.data)[inst].imm; |
| 377 | | if (ops.flags == 0b00) { |
| 378 | | // movabs reg, imm64 |
| 379 | | try w.print("movabs {s}, {d}\n", .{ @tagName(ops.reg1), imm }); |
| 380 | | } |
| 381 | | if (ops.reg1 == .none) { |
| 382 | | try w.writeAll("movabs moffs64, rax\n"); |
| 383 | | } else { |
| 384 | | // movabs rax, moffs64 |
| 385 | | try w.writeAll("movabs rax, moffs64\n"); |
| 386 | | } |
| 387 | | } |
| 388 | | |
| 389 | | fn mirIMulComplex(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 390 | | const tag = print.mir.instructions.items(.tag)[inst]; |
| 391 | | assert(tag == .imul_complex); |
| 392 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 393 | | switch (ops.flags) { |
| 394 | | 0b00 => { |
| 395 | | try w.print("imul {s}, {s}\n", .{ @tagName(ops.reg1), @tagName(ops.reg2) }); |
| 396 | | }, |
| 397 | | 0b10 => { |
| 398 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 399 | | try w.print("imul {s}, {s}, {d}\n", .{ @tagName(ops.reg1), @tagName(ops.reg2), imm }); |
| 400 | | }, |
| 401 | | else => return w.writeAll("TODO implement imul\n"), |
| 402 | | } |
| 403 | | } |
| 404 | | |
| 405 | | fn mirLea(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 406 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 407 | | try w.writeAll("lea "); |
| 408 | | switch (ops.flags) { |
| 409 | | 0b00 => { |
| 410 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 411 | | try w.print("{s} [", .{@tagName(ops.reg1)}); |
| 412 | | if (ops.reg2 != .none) { |
| 413 | | try w.print("{s} + ", .{@tagName(ops.reg2)}); |
| 414 | | } else { |
| 415 | | try w.print("ds:", .{}); |
| 416 | | } |
| 417 | | try w.print("{d}]", .{imm}); |
| 418 | | }, |
| 419 | | 0b01 => { |
| 420 | | try w.print("{s}, ", .{@tagName(ops.reg1)}); |
| 421 | | switch (ops.reg1.size()) { |
| 422 | | 8 => try w.print("byte ptr ", .{}), |
| 423 | | 16 => try w.print("word ptr ", .{}), |
| 424 | | 32 => try w.print("dword ptr ", .{}), |
| 425 | | 64 => try w.print("qword ptr ", .{}), |
| 426 | | else => unreachable, |
| 427 | | } |
| 428 | | try w.print("[rip + 0x0] ", .{}); |
| 429 | | const payload = print.mir.instructions.items(.data)[inst].payload; |
| 430 | | const imm = print.mir.extraData(Mir.Imm64, payload).data.decode(); |
| 431 | | try w.print("target@{x}", .{imm}); |
| 432 | | }, |
| 433 | | 0b10 => { |
| 434 | | const imm = print.mir.instructions.items(.data)[inst].imm; |
| 435 | | try w.print("{s}, ", .{@tagName(ops.reg1)}); |
| 436 | | switch (ops.reg1.size()) { |
| 437 | | 8 => try w.print("byte ptr ", .{}), |
| 438 | | 16 => try w.print("word ptr ", .{}), |
| 439 | | 32 => try w.print("dword ptr ", .{}), |
| 440 | | 64 => try w.print("qword ptr ", .{}), |
| 441 | | else => unreachable, |
| 442 | | } |
| 443 | | try w.print("[rbp + rcx + {d}]", .{imm}); |
| 444 | | }, |
| 445 | | 0b11 => { |
| 446 | | try w.writeAll("unused variant"); |
| 447 | | }, |
| 448 | | } |
| 449 | | try w.writeAll("\n"); |
| 450 | | } |
| 451 | | |
| 452 | | fn mirLeaPie(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 453 | | const ops = Mir.Ops.decode(print.mir.instructions.items(.ops)[inst]); |
| 454 | | const load_reloc = print.mir.instructions.items(.data)[inst].load_reloc; |
| 455 | | try w.print("lea {s}, ", .{@tagName(ops.reg1)}); |
| 456 | | switch (ops.reg1.size()) { |
| 457 | | 8 => try w.print("byte ptr ", .{}), |
| 458 | | 16 => try w.print("word ptr ", .{}), |
| 459 | | 32 => try w.print("dword ptr ", .{}), |
| 460 | | 64 => try w.print("qword ptr ", .{}), |
| 461 | | else => unreachable, |
| 462 | | } |
| 463 | | try w.print("[rip + 0x0] ", .{}); |
| 464 | | if (print.bin_file.cast(link.File.MachO)) |macho_file| { |
| 465 | | const target = macho_file.locals.items[load_reloc.sym_index]; |
| 466 | | const target_name = macho_file.getString(target.n_strx); |
| 467 | | try w.print("target@{s}", .{target_name}); |
| 468 | | } else { |
| 469 | | try w.print("TODO lea PIE for other backends", .{}); |
| 470 | | } |
| 471 | | return w.writeByte('\n'); |
| 472 | | } |
| 473 | | |
| 474 | | fn mirCallExtern(print: *const Print, inst: Mir.Inst.Index, w: anytype) !void { |
| 475 | | _ = print; |
| 476 | | _ = inst; |
| 477 | | return w.writeAll("TODO call_extern"); |
| 478 | | } |