| 1 | //===-- SparcInstPrinter.cpp - Convert Sparc MCInst to assembly syntax --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class prints an Sparc MCInst to a .s file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | /* Capstone Disassembly Engine */ |
| 15 | /* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */ |
| 16 | |
| 17 | #ifdef CAPSTONE_HAS_SPARC |
| 18 | |
| 19 | #ifdef _MSC_VER |
| 20 | #define _CRT_SECURE_NO_WARNINGS |
| 21 | #endif |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <limits.h> |
| 27 | |
| 28 | #include "SparcInstPrinter.h" |
| 29 | #include "../../MCInst.h" |
| 30 | #include "../../utils.h" |
| 31 | #include "../../SStream.h" |
| 32 | #include "../../MCRegisterInfo.h" |
| 33 | #include "../../MathExtras.h" |
| 34 | #include "SparcMapping.h" |
| 35 | |
| 36 | #include "Sparc.h" |
| 37 | |
| 38 | static const char *getRegisterName(unsigned RegNo); |
| 39 | static void printInstruction(MCInst *MI, SStream *O, const MCRegisterInfo *MRI); |
| 40 | static void printMemOperand(MCInst *MI, int opNum, SStream *O, const char *Modifier); |
| 41 | static void printOperand(MCInst *MI, int opNum, SStream *O); |
| 42 | |
| 43 | static void Sparc_add_hint(MCInst *MI, unsigned int hint) |
| 44 | { |
| 45 | if (MI->csh->detail) { |
| 46 | MI->flat_insn->detail->sparc.hint = hint; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void Sparc_add_reg(MCInst *MI, unsigned int reg) |
| 51 | { |
| 52 | if (MI->csh->detail) { |
| 53 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG; |
| 54 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg; |
| 55 | MI->flat_insn->detail->sparc.op_count++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void set_mem_access(MCInst *MI, bool status) |
| 60 | { |
| 61 | if (MI->csh->detail != CS_OPT_ON) |
| 62 | return; |
| 63 | |
| 64 | MI->csh->doing_mem = status; |
| 65 | |
| 66 | if (status) { |
| 67 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_MEM; |
| 68 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base = SPARC_REG_INVALID; |
| 69 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.disp = 0; |
| 70 | } else { |
| 71 | // done, create the next operand slot |
| 72 | MI->flat_insn->detail->sparc.op_count++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void Sparc_post_printer(csh ud, cs_insn *insn, char *insn_asm, MCInst *mci) |
| 77 | { |
| 78 | if (((cs_struct *)ud)->detail != CS_OPT_ON) |
| 79 | return; |
| 80 | |
| 81 | // fix up some instructions |
| 82 | if (insn->id == SPARC_INS_CASX) { |
| 83 | // first op is actually a memop, not regop |
| 84 | insn->detail->sparc.operands[0].type = SPARC_OP_MEM; |
| 85 | insn->detail->sparc.operands[0].mem.base = (uint8_t)insn->detail->sparc.operands[0].reg; |
| 86 | insn->detail->sparc.operands[0].mem.disp = 0; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void printRegName(SStream *OS, unsigned RegNo) |
| 91 | { |
| 92 | SStream_concat0(OS, "%"); |
| 93 | SStream_concat0(OS, getRegisterName(RegNo)); |
| 94 | } |
| 95 | |
| 96 | #define GET_INSTRINFO_ENUM |
| 97 | #include "SparcGenInstrInfo.inc" |
| 98 | |
| 99 | #define GET_REGINFO_ENUM |
| 100 | #include "SparcGenRegisterInfo.inc" |
| 101 | |
| 102 | static bool printSparcAliasInstr(MCInst *MI, SStream *O) |
| 103 | { |
| 104 | switch (MCInst_getOpcode(MI)) { |
| 105 | default: return false; |
| 106 | case SP_JMPLrr: |
| 107 | case SP_JMPLri: |
| 108 | if (MCInst_getNumOperands(MI) != 3) |
| 109 | return false; |
| 110 | if (!MCOperand_isReg(MCInst_getOperand(MI, 0))) |
| 111 | return false; |
| 112 | |
| 113 | switch (MCOperand_getReg(MCInst_getOperand(MI, 0))) { |
| 114 | default: return false; |
| 115 | case SP_G0: // jmp $addr | ret | retl |
| 116 | if (MCOperand_isImm(MCInst_getOperand(MI, 2)) && |
| 117 | MCOperand_getImm(MCInst_getOperand(MI, 2)) == 8) { |
| 118 | switch(MCOperand_getReg(MCInst_getOperand(MI, 1))) { |
| 119 | default: break; |
| 120 | case SP_I7: SStream_concat0(O, "ret"); MCInst_setOpcodePub(MI, SPARC_INS_RET); return true; |
| 121 | case SP_O7: SStream_concat0(O, "retl"); MCInst_setOpcodePub(MI, SPARC_INS_RETL); return true; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | SStream_concat0(O, "jmp\t"); |
| 126 | MCInst_setOpcodePub(MI, SPARC_INS_JMP); |
| 127 | printMemOperand(MI, 1, O, NULL); |
| 128 | return true; |
| 129 | case SP_O7: // call $addr |
| 130 | SStream_concat0(O, "call "); |
| 131 | MCInst_setOpcodePub(MI, SPARC_INS_CALL); |
| 132 | printMemOperand(MI, 1, O, NULL); |
| 133 | return true; |
| 134 | } |
| 135 | case SP_V9FCMPS: |
| 136 | case SP_V9FCMPD: |
| 137 | case SP_V9FCMPQ: |
| 138 | case SP_V9FCMPES: |
| 139 | case SP_V9FCMPED: |
| 140 | case SP_V9FCMPEQ: |
| 141 | if (MI->csh->mode & CS_MODE_V9 || (MCInst_getNumOperands(MI) != 3) || |
| 142 | (!MCOperand_isReg(MCInst_getOperand(MI, 0))) || |
| 143 | (MCOperand_getReg(MCInst_getOperand(MI, 0)) != SP_FCC0)) |
| 144 | return false; |
| 145 | // if V8, skip printing %fcc0. |
| 146 | switch(MCInst_getOpcode(MI)) { |
| 147 | default: |
| 148 | case SP_V9FCMPS: SStream_concat0(O, "fcmps\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPS); break; |
| 149 | case SP_V9FCMPD: SStream_concat0(O, "fcmpd\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPD); break; |
| 150 | case SP_V9FCMPQ: SStream_concat0(O, "fcmpq\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPQ); break; |
| 151 | case SP_V9FCMPES: SStream_concat0(O, "fcmpes\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPES); break; |
| 152 | case SP_V9FCMPED: SStream_concat0(O, "fcmped\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPED); break; |
| 153 | case SP_V9FCMPEQ: SStream_concat0(O, "fcmpeq\t"); MCInst_setOpcodePub(MI, SPARC_INS_FCMPEQ); break; |
| 154 | } |
| 155 | printOperand(MI, 1, O); |
| 156 | SStream_concat0(O, ", "); |
| 157 | printOperand(MI, 2, O); |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void printOperand(MCInst *MI, int opNum, SStream *O) |
| 163 | { |
| 164 | int64_t Imm; |
| 165 | unsigned reg; |
| 166 | MCOperand *MO = MCInst_getOperand(MI, opNum); |
| 167 | |
| 168 | if (MCOperand_isReg(MO)) { |
| 169 | reg = MCOperand_getReg(MO); |
| 170 | printRegName(O, reg); |
| 171 | reg = Sparc_map_register(reg); |
| 172 | |
| 173 | if (MI->csh->detail) { |
| 174 | if (MI->csh->doing_mem) { |
| 175 | if (MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base) |
| 176 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.index = (uint8_t)reg; |
| 177 | else |
| 178 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.base = (uint8_t)reg; |
| 179 | } else { |
| 180 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG; |
| 181 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg; |
| 182 | MI->flat_insn->detail->sparc.op_count++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (MCOperand_isImm(MO)) { |
| 190 | Imm = (int)MCOperand_getImm(MO); |
| 191 | |
| 192 | // Conditional branches displacements needs to be signextended to be |
| 193 | // able to jump backwards. |
| 194 | // |
| 195 | // Displacements are measured as the number of instructions forward or |
| 196 | // backward, so they need to be multiplied by 4 |
| 197 | switch (MI->Opcode) { |
| 198 | case SP_CALL: |
| 199 | // Imm = SignExtend32(Imm, 30); |
| 200 | Imm += MI->address; |
| 201 | break; |
| 202 | |
| 203 | // Branch on integer condition with prediction (BPcc) |
| 204 | // Branch on floating point condition with prediction (FBPfcc) |
| 205 | case SP_BPICC: |
| 206 | case SP_BPICCA: |
| 207 | case SP_BPICCANT: |
| 208 | case SP_BPICCNT: |
| 209 | case SP_BPXCC: |
| 210 | case SP_BPXCCA: |
| 211 | case SP_BPXCCANT: |
| 212 | case SP_BPXCCNT: |
| 213 | case SP_BPFCC: |
| 214 | case SP_BPFCCA: |
| 215 | case SP_BPFCCANT: |
| 216 | case SP_BPFCCNT: |
| 217 | Imm = SignExtend32(Imm, 19); |
| 218 | Imm = MI->address + Imm * 4; |
| 219 | break; |
| 220 | |
| 221 | // Branch on integer condition (Bicc) |
| 222 | // Branch on floating point condition (FBfcc) |
| 223 | case SP_BA: |
| 224 | case SP_BCOND: |
| 225 | case SP_BCONDA: |
| 226 | case SP_FBCOND: |
| 227 | case SP_FBCONDA: |
| 228 | Imm = SignExtend32(Imm, 22); |
| 229 | Imm = MI->address + Imm * 4; |
| 230 | break; |
| 231 | |
| 232 | // Branch on integer register with prediction (BPr) |
| 233 | case SP_BPGEZapn: |
| 234 | case SP_BPGEZapt: |
| 235 | case SP_BPGEZnapn: |
| 236 | case SP_BPGEZnapt: |
| 237 | case SP_BPGZapn: |
| 238 | case SP_BPGZapt: |
| 239 | case SP_BPGZnapn: |
| 240 | case SP_BPGZnapt: |
| 241 | case SP_BPLEZapn: |
| 242 | case SP_BPLEZapt: |
| 243 | case SP_BPLEZnapn: |
| 244 | case SP_BPLEZnapt: |
| 245 | case SP_BPLZapn: |
| 246 | case SP_BPLZapt: |
| 247 | case SP_BPLZnapn: |
| 248 | case SP_BPLZnapt: |
| 249 | case SP_BPNZapn: |
| 250 | case SP_BPNZapt: |
| 251 | case SP_BPNZnapn: |
| 252 | case SP_BPNZnapt: |
| 253 | case SP_BPZapn: |
| 254 | case SP_BPZapt: |
| 255 | case SP_BPZnapn: |
| 256 | case SP_BPZnapt: |
| 257 | Imm = SignExtend32(Imm, 16); |
| 258 | Imm = MI->address + Imm * 4; |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | printInt64(O, Imm); |
| 263 | |
| 264 | if (MI->csh->detail) { |
| 265 | if (MI->csh->doing_mem) { |
| 266 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].mem.disp = Imm; |
| 267 | } else { |
| 268 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_IMM; |
| 269 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].imm = Imm; |
| 270 | MI->flat_insn->detail->sparc.op_count++; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | static void printMemOperand(MCInst *MI, int opNum, SStream *O, const char *Modifier) |
| 279 | { |
| 280 | MCOperand *MO; |
| 281 | |
| 282 | set_mem_access(MI, true); |
| 283 | printOperand(MI, opNum, O); |
| 284 | |
| 285 | // If this is an ADD operand, emit it like normal operands. |
| 286 | if (Modifier && !strcmp(Modifier, "arith")) { |
| 287 | SStream_concat0(O, ", "); |
| 288 | printOperand(MI, opNum + 1, O); |
| 289 | set_mem_access(MI, false); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | MO = MCInst_getOperand(MI, opNum + 1); |
| 294 | |
| 295 | if (MCOperand_isReg(MO) && (MCOperand_getReg(MO) == SP_G0)) { |
| 296 | set_mem_access(MI, false); |
| 297 | return; // don't print "+%g0" |
| 298 | } |
| 299 | |
| 300 | if (MCOperand_isImm(MO) && (MCOperand_getImm(MO) == 0)) { |
| 301 | set_mem_access(MI, false); |
| 302 | return; // don't print "+0" |
| 303 | } |
| 304 | |
| 305 | SStream_concat0(O, "+"); // qq |
| 306 | |
| 307 | printOperand(MI, opNum + 1, O); |
| 308 | set_mem_access(MI, false); |
| 309 | } |
| 310 | |
| 311 | static void printCCOperand(MCInst *MI, int opNum, SStream *O) |
| 312 | { |
| 313 | int CC = (int)MCOperand_getImm(MCInst_getOperand(MI, opNum)) + 256; |
| 314 | |
| 315 | switch (MCInst_getOpcode(MI)) { |
| 316 | default: break; |
| 317 | case SP_FBCOND: |
| 318 | case SP_FBCONDA: |
| 319 | case SP_BPFCC: |
| 320 | case SP_BPFCCA: |
| 321 | case SP_BPFCCNT: |
| 322 | case SP_BPFCCANT: |
| 323 | case SP_MOVFCCrr: case SP_V9MOVFCCrr: |
| 324 | case SP_MOVFCCri: case SP_V9MOVFCCri: |
| 325 | case SP_FMOVS_FCC: case SP_V9FMOVS_FCC: |
| 326 | case SP_FMOVD_FCC: case SP_V9FMOVD_FCC: |
| 327 | case SP_FMOVQ_FCC: case SP_V9FMOVQ_FCC: |
| 328 | // Make sure CC is a fp conditional flag. |
| 329 | CC = (CC < 16+256) ? (CC + 16) : CC; |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | SStream_concat0(O, SPARCCondCodeToString((sparc_cc)CC)); |
| 334 | |
| 335 | if (MI->csh->detail) |
| 336 | MI->flat_insn->detail->sparc.cc = (sparc_cc)CC; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | static bool printGetPCX(MCInst *MI, unsigned opNum, SStream *O) |
| 341 | { |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | #define PRINT_ALIAS_INSTR |
| 347 | #include "SparcGenAsmWriter.inc" |
| 348 | |
| 349 | void Sparc_printInst(MCInst *MI, SStream *O, void *Info) |
| 350 | { |
| 351 | char *mnem, *p; |
| 352 | char instr[64]; // Sparc has no instruction this long |
| 353 | |
| 354 | mnem = printAliasInstr(MI, O, Info); |
| 355 | if (mnem) { |
| 356 | // fixup instruction id due to the change in alias instruction |
| 357 | unsigned cpy_len = sizeof(instr) - 1 < strlen(mnem) ? sizeof(instr) - 1 : strlen(mnem); |
| 358 | memcpy(instr, mnem, cpy_len); |
| 359 | instr[cpy_len] = '\0'; |
| 360 | // does this contains hint with a coma? |
| 361 | p = strchr(instr, ','); |
| 362 | if (p) |
| 363 | *p = '\0'; // now instr only has instruction mnemonic |
| 364 | MCInst_setOpcodePub(MI, Sparc_map_insn(instr)); |
| 365 | switch(MCInst_getOpcode(MI)) { |
| 366 | case SP_BCOND: |
| 367 | case SP_BCONDA: |
| 368 | case SP_BPICCANT: |
| 369 | case SP_BPICCNT: |
| 370 | case SP_BPXCCANT: |
| 371 | case SP_BPXCCNT: |
| 372 | case SP_TXCCri: |
| 373 | case SP_TXCCrr: |
| 374 | if (MI->csh->detail) { |
| 375 | // skip 'b', 't' |
| 376 | MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 1); |
| 377 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 378 | } |
| 379 | break; |
| 380 | case SP_BPFCCANT: |
| 381 | case SP_BPFCCNT: |
| 382 | if (MI->csh->detail) { |
| 383 | // skip 'fb' |
| 384 | MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 2); |
| 385 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 386 | } |
| 387 | break; |
| 388 | case SP_FMOVD_ICC: |
| 389 | case SP_FMOVD_XCC: |
| 390 | case SP_FMOVQ_ICC: |
| 391 | case SP_FMOVQ_XCC: |
| 392 | case SP_FMOVS_ICC: |
| 393 | case SP_FMOVS_XCC: |
| 394 | if (MI->csh->detail) { |
| 395 | // skip 'fmovd', 'fmovq', 'fmovs' |
| 396 | MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 5); |
| 397 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 398 | } |
| 399 | break; |
| 400 | case SP_MOVICCri: |
| 401 | case SP_MOVICCrr: |
| 402 | case SP_MOVXCCri: |
| 403 | case SP_MOVXCCrr: |
| 404 | if (MI->csh->detail) { |
| 405 | // skip 'mov' |
| 406 | MI->flat_insn->detail->sparc.cc = Sparc_map_ICC(instr + 3); |
| 407 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 408 | } |
| 409 | break; |
| 410 | case SP_V9FMOVD_FCC: |
| 411 | case SP_V9FMOVQ_FCC: |
| 412 | case SP_V9FMOVS_FCC: |
| 413 | if (MI->csh->detail) { |
| 414 | // skip 'fmovd', 'fmovq', 'fmovs' |
| 415 | MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 5); |
| 416 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 417 | } |
| 418 | break; |
| 419 | case SP_V9MOVFCCri: |
| 420 | case SP_V9MOVFCCrr: |
| 421 | if (MI->csh->detail) { |
| 422 | // skip 'mov' |
| 423 | MI->flat_insn->detail->sparc.cc = Sparc_map_FCC(instr + 3); |
| 424 | MI->flat_insn->detail->sparc.hint = Sparc_map_hint(mnem); |
| 425 | } |
| 426 | break; |
| 427 | default: |
| 428 | break; |
| 429 | } |
| 430 | cs_mem_free(mnem); |
| 431 | } else { |
| 432 | if (!printSparcAliasInstr(MI, O)) |
| 433 | printInstruction(MI, O, NULL); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void Sparc_addReg(MCInst *MI, int reg) |
| 438 | { |
| 439 | if (MI->csh->detail) { |
| 440 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].type = SPARC_OP_REG; |
| 441 | MI->flat_insn->detail->sparc.operands[MI->flat_insn->detail->sparc.op_count].reg = reg; |
| 442 | MI->flat_insn->detail->sparc.op_count++; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | #endif |