1 /* libs/pixelflinger/codeflinger/MIPSAssembler.h 2 ** 3 ** Copyright 2012, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #ifndef ANDROID_MIPSASSEMBLER_H 19 #define ANDROID_MIPSASSEMBLER_H 20 21 #include <stdint.h> 22 #include <sys/types.h> 23 24 #include "tinyutils/smartpointer.h" 25 #include "utils/KeyedVector.h" 26 #include "utils/Vector.h" 27 28 #include "ARMAssemblerInterface.h" 29 #include "CodeCache.h" 30 31 namespace android { 32 33 class MIPSAssembler; // forward reference 34 35 // this class mimics ARMAssembler interface 36 // intent is to translate each ARM instruction to 1 or more MIPS instr 37 // implementation calls MIPSAssembler class to generate mips code 38 class ArmToMipsAssembler : public ARMAssemblerInterface 39 { 40 public: 41 ArmToMipsAssembler(const sp<Assembly>& assembly, 42 char *abuf = 0, int linesz = 0, int instr_count = 0); 43 virtual ~ArmToMipsAssembler(); 44 45 uint32_t* base() const; 46 uint32_t* pc() const; 47 void disassemble(const char* name); 48 49 virtual void reset(); 50 51 virtual int generate(const char* name); 52 virtual int getCodegenArch(); 53 54 virtual void prolog(); 55 virtual void epilog(uint32_t touched); 56 virtual void comment(const char* string); 57 58 59 // ----------------------------------------------------------------------- 60 // shifters and addressing modes 61 // ----------------------------------------------------------------------- 62 63 // shifters... 64 virtual bool isValidImmediate(uint32_t immed); 65 virtual int buildImmediate(uint32_t i, uint32_t& rot, uint32_t& imm); 66 67 virtual uint32_t imm(uint32_t immediate); 68 virtual uint32_t reg_imm(int Rm, int type, uint32_t shift); 69 virtual uint32_t reg_rrx(int Rm); 70 virtual uint32_t reg_reg(int Rm, int type, int Rs); 71 72 // addressing modes... 73 // LDR(B)/STR(B)/PLD 74 // (immediate and Rm can be negative, which indicates U=0) 75 virtual uint32_t immed12_pre(int32_t immed12, int W=0); 76 virtual uint32_t immed12_post(int32_t immed12); 77 virtual uint32_t reg_scale_pre(int Rm, int type=0, uint32_t shift=0, int W=0); 78 virtual uint32_t reg_scale_post(int Rm, int type=0, uint32_t shift=0); 79 80 // LDRH/LDRSB/LDRSH/STRH 81 // (immediate and Rm can be negative, which indicates U=0) 82 virtual uint32_t immed8_pre(int32_t immed8, int W=0); 83 virtual uint32_t immed8_post(int32_t immed8); 84 virtual uint32_t reg_pre(int Rm, int W=0); 85 virtual uint32_t reg_post(int Rm); 86 87 88 89 90 virtual void dataProcessing(int opcode, int cc, int s, 91 int Rd, int Rn, 92 uint32_t Op2); 93 virtual void MLA(int cc, int s, 94 int Rd, int Rm, int Rs, int Rn); 95 virtual void MUL(int cc, int s, 96 int Rd, int Rm, int Rs); 97 virtual void UMULL(int cc, int s, 98 int RdLo, int RdHi, int Rm, int Rs); 99 virtual void UMUAL(int cc, int s, 100 int RdLo, int RdHi, int Rm, int Rs); 101 virtual void SMULL(int cc, int s, 102 int RdLo, int RdHi, int Rm, int Rs); 103 virtual void SMUAL(int cc, int s, 104 int RdLo, int RdHi, int Rm, int Rs); 105 106 virtual void B(int cc, uint32_t* pc); 107 virtual void BL(int cc, uint32_t* pc); 108 virtual void BX(int cc, int Rn); 109 virtual void label(const char* theLabel); 110 virtual void B(int cc, const char* label); 111 virtual void BL(int cc, const char* label); 112 113 virtual uint32_t* pcForLabel(const char* label); 114 115 virtual void LDR (int cc, int Rd, 116 int Rn, uint32_t offset = 0); 117 virtual void LDRB(int cc, int Rd, 118 int Rn, uint32_t offset = 0); 119 virtual void STR (int cc, int Rd, 120 int Rn, uint32_t offset = 0); 121 virtual void STRB(int cc, int Rd, 122 int Rn, uint32_t offset = 0); 123 virtual void LDRH (int cc, int Rd, 124 int Rn, uint32_t offset = 0); 125 virtual void LDRSB(int cc, int Rd, 126 int Rn, uint32_t offset = 0); 127 virtual void LDRSH(int cc, int Rd, 128 int Rn, uint32_t offset = 0); 129 virtual void STRH (int cc, int Rd, 130 int Rn, uint32_t offset = 0); 131 132 virtual void LDM(int cc, int dir, 133 int Rn, int W, uint32_t reg_list); 134 virtual void STM(int cc, int dir, 135 int Rn, int W, uint32_t reg_list); 136 137 virtual void SWP(int cc, int Rn, int Rd, int Rm); 138 virtual void SWPB(int cc, int Rn, int Rd, int Rm); 139 virtual void SWI(int cc, uint32_t comment); 140 141 virtual void PLD(int Rn, uint32_t offset); 142 virtual void CLZ(int cc, int Rd, int Rm); 143 virtual void QADD(int cc, int Rd, int Rm, int Rn); 144 virtual void QDADD(int cc, int Rd, int Rm, int Rn); 145 virtual void QSUB(int cc, int Rd, int Rm, int Rn); 146 virtual void QDSUB(int cc, int Rd, int Rm, int Rn); 147 virtual void SMUL(int cc, int xy, 148 int Rd, int Rm, int Rs); 149 virtual void SMULW(int cc, int y, 150 int Rd, int Rm, int Rs); 151 virtual void SMLA(int cc, int xy, 152 int Rd, int Rm, int Rs, int Rn); 153 virtual void SMLAL(int cc, int xy, 154 int RdHi, int RdLo, int Rs, int Rm); 155 virtual void SMLAW(int cc, int y, 156 int Rd, int Rm, int Rs, int Rn); 157 158 // byte/half word extract... 159 virtual void UXTB16(int cc, int Rd, int Rm, int rotate); 160 161 // bit manipulation... 162 virtual void UBFX(int cc, int Rd, int Rn, int lsb, int width); 163 164 // this is some crap to share is MIPSAssembler class for debug 165 char * mArmDisassemblyBuffer; 166 int mArmLineLength; 167 int mArmInstrCount; 168 169 int mInum; // current arm instuction number (0..n) 170 uint32_t** mArmPC; // array: PC for 1st mips instr of 171 // each translated ARM instr 172 173 174 private: 175 ArmToMipsAssembler(const ArmToMipsAssembler& rhs); 176 ArmToMipsAssembler& operator = (const ArmToMipsAssembler& rhs); 177 178 void init_conditional_labels(void); 179 180 void protectConditionalOperands(int Rd); 181 182 // reg__tmp set to MIPS AT, reg 1 183 int dataProcAdrModes(int op, int& source, bool sign = false, int reg_tmp = 1); 184 185 sp<Assembly> mAssembly; 186 MIPSAssembler* mMips; 187 188 189 enum misc_constants_t { 190 ARM_MAX_INSTUCTIONS = 512 // based on ASSEMBLY_SCRATCH_SIZE 191 }; 192 193 enum { 194 SRC_REG = 0, 195 SRC_IMM, 196 SRC_ERROR = -1 197 }; 198 199 enum addr_modes { 200 // start above the range of legal mips reg #'s (0-31) 201 AMODE_REG = 0x20, 202 AMODE_IMM, AMODE_REG_IMM, // for data processing 203 AMODE_IMM_12_PRE, AMODE_IMM_12_POST, // for load/store 204 AMODE_REG_SCALE_PRE, AMODE_IMM_8_PRE, 205 AMODE_IMM_8_POST, AMODE_REG_PRE, 206 AMODE_UNSUPPORTED 207 }; 208 209 struct addr_mode_t { // address modes for current ARM instruction 210 int reg; 211 int stype; 212 uint32_t value; 213 bool writeback; // writeback the adr reg after modification 214 } amode; 215 216 enum cond_types { 217 CMP_COND = 1, 218 SBIT_COND 219 }; 220 221 struct cond_mode_t { // conditional-execution info for current ARM instruction 222 cond_types type; 223 int r1; 224 int r2; 225 int labelnum; 226 char label[100][10]; 227 } cond; 228 229 }; 230 231 232 233 234 // ---------------------------------------------------------------------------- 235 // ---------------------------------------------------------------------------- 236 // ---------------------------------------------------------------------------- 237 238 // This is the basic MIPS assembler, which just creates the opcodes in memory. 239 // All the more complicated work is done in ArmToMipsAssember above. 240 241 class MIPSAssembler 242 { 243 public: 244 MIPSAssembler(const sp<Assembly>& assembly, ArmToMipsAssembler *parent); 245 MIPSAssembler(void* assembly); 246 virtual ~MIPSAssembler(); 247 248 virtual uint32_t* base() const; 249 virtual uint32_t* pc() const; 250 virtual void reset(); 251 252 virtual void disassemble(const char* name); 253 254 virtual void prolog(); 255 virtual void epilog(uint32_t touched); 256 virtual int generate(const char* name); 257 virtual void comment(const char* string); 258 virtual void label(const char* string); 259 260 // valid only after generate() has been called 261 virtual uint32_t* pcForLabel(const char* label); 262 263 264 // ------------------------------------------------------------------------ 265 // MIPSAssemblerInterface... 266 // ------------------------------------------------------------------------ 267 268 #if 0 269 #pragma mark - 270 #pragma mark Arithmetic... 271 #endif 272 273 void ADDU(int Rd, int Rs, int Rt); 274 void ADDIU(int Rt, int Rs, int16_t imm); 275 void SUBU(int Rd, int Rs, int Rt); 276 void SUBIU(int Rt, int Rs, int16_t imm); 277 void NEGU(int Rd, int Rs); 278 void MUL(int Rd, int Rs, int Rt); 279 void MULT(int Rs, int Rt); // dest is hi,lo 280 void MULTU(int Rs, int Rt); // dest is hi,lo 281 void MADD(int Rs, int Rt); // hi,lo = hi,lo + Rs * Rt 282 void MADDU(int Rs, int Rt); // hi,lo = hi,lo + Rs * Rt 283 void MSUB(int Rs, int Rt); // hi,lo = hi,lo - Rs * Rt 284 void MSUBU(int Rs, int Rt); // hi,lo = hi,lo - Rs * Rt 285 void SEB(int Rd, int Rt); // sign-extend byte (mips32r2) 286 void SEH(int Rd, int Rt); // sign-extend half-word (mips32r2) 287 288 289 #if 0 290 #pragma mark - 291 #pragma mark Comparisons... 292 #endif 293 294 void SLT(int Rd, int Rs, int Rt); 295 void SLTI(int Rt, int Rs, int16_t imm); 296 void SLTU(int Rd, int Rs, int Rt); 297 void SLTIU(int Rt, int Rs, int16_t imm); 298 299 300 #if 0 301 #pragma mark - 302 #pragma mark Logical... 303 #endif 304 305 void AND(int Rd, int Rs, int Rt); 306 void ANDI(int Rd, int Rs, uint16_t imm); 307 void OR(int Rd, int Rs, int Rt); 308 void ORI(int Rt, int Rs, uint16_t imm); 309 void NOR(int Rd, int Rs, int Rt); 310 void NOT(int Rd, int Rs); 311 void XOR(int Rd, int Rs, int Rt); 312 void XORI(int Rt, int Rs, uint16_t imm); 313 314 void SLL(int Rd, int Rt, int shft); 315 void SLLV(int Rd, int Rt, int Rs); 316 void SRL(int Rd, int Rt, int shft); 317 void SRLV(int Rd, int Rt, int Rs); 318 void SRA(int Rd, int Rt, int shft); 319 void SRAV(int Rd, int Rt, int Rs); 320 void ROTR(int Rd, int Rt, int shft); // mips32r2 321 void ROTRV(int Rd, int Rt, int Rs); // mips32r2 322 void RORsyn(int Rd, int Rs, int Rt); // synthetic: d = s rotated by t 323 void RORIsyn(int Rd, int Rt, int rot); // synthetic: d = s rotated by immed 324 325 void CLO(int Rd, int Rs); 326 void CLZ(int Rd, int Rs); 327 void WSBH(int Rd, int Rt); 328 329 330 #if 0 331 #pragma mark - 332 #pragma mark Load/store... 333 #endif 334 335 void LW(int Rt, int Rbase, int16_t offset); 336 void SW(int Rt, int Rbase, int16_t offset); 337 void LB(int Rt, int Rbase, int16_t offset); 338 void LBU(int Rt, int Rbase, int16_t offset); 339 void SB(int Rt, int Rbase, int16_t offset); 340 void LH(int Rt, int Rbase, int16_t offset); 341 void LHU(int Rt, int Rbase, int16_t offset); 342 void SH(int Rt, int Rbase, int16_t offset); 343 void LUI(int Rt, int16_t offset); 344 345 #if 0 346 #pragma mark - 347 #pragma mark Register moves... 348 #endif 349 350 void MOVE(int Rd, int Rs); 351 void MOVN(int Rd, int Rs, int Rt); 352 void MOVZ(int Rd, int Rs, int Rt); 353 void MFHI(int Rd); 354 void MFLO(int Rd); 355 void MTHI(int Rs); 356 void MTLO(int Rs); 357 358 #if 0 359 #pragma mark - 360 #pragma mark Branch... 361 #endif 362 363 void B(const char* label); 364 void BEQ(int Rs, int Rt, const char* label); 365 void BNE(int Rs, int Rt, const char* label); 366 void BGEZ(int Rs, const char* label); 367 void BGTZ(int Rs, const char* label); 368 void BLEZ(int Rs, const char* label); 369 void BLTZ(int Rs, const char* label); 370 void JR(int Rs); 371 372 373 #if 0 374 #pragma mark - 375 #pragma mark Synthesized Branch... 376 #endif 377 378 // synthetic variants of above (using slt & friends) 379 void BEQZ(int Rs, const char* label); 380 void BNEZ(int Rs, const char* label); 381 void BGE(int Rs, int Rt, const char* label); 382 void BGEU(int Rs, int Rt, const char* label); 383 void BGT(int Rs, int Rt, const char* label); 384 void BGTU(int Rs, int Rt, const char* label); 385 void BLE(int Rs, int Rt, const char* label); 386 void BLEU(int Rs, int Rt, const char* label); 387 void BLT(int Rs, int Rt, const char* label); 388 void BLTU(int Rs, int Rt, const char* label); 389 390 #if 0 391 #pragma mark - 392 #pragma mark Misc... 393 #endif 394 395 void NOP(void); 396 void NOP2(void); 397 void UNIMPL(void); 398 399 400 401 402 403 protected: 404 virtual void string_detab(char *s); 405 virtual void string_pad(char *s, int padded_len); 406 407 ArmToMipsAssembler *mParent; 408 sp<Assembly> mAssembly; 409 uint32_t* mBase; 410 uint32_t* mPC; 411 uint32_t* mPrologPC; 412 int64_t mDuration; 413 414 struct branch_target_t { branch_target_tbranch_target_t415 inline branch_target_t() : label(0), pc(0) { } branch_target_tbranch_target_t416 inline branch_target_t(const char* l, uint32_t* p) 417 : label(l), pc(p) { } 418 const char* label; 419 uint32_t* pc; 420 }; 421 422 Vector<branch_target_t> mBranchTargets; 423 KeyedVector< const char*, uint32_t* > mLabels; 424 KeyedVector< uint32_t*, const char* > mLabelsInverseMapping; 425 KeyedVector< uint32_t*, const char* > mComments; 426 427 428 429 430 // opcode field of all instructions 431 enum opcode_field { 432 spec_op, regimm_op, j_op, jal_op, // 00 433 beq_op, bne_op, blez_op, bgtz_op, 434 addi_op, addiu_op, slti_op, sltiu_op, // 08 435 andi_op, ori_op, xori_op, lui_op, 436 cop0_op, cop1_op, cop2_op, cop1x_op, // 10 437 beql_op, bnel_op, blezl_op, bgtzl_op, 438 daddi_op, daddiu_op, ldl_op, ldr_op, // 18 439 spec2_op, jalx_op, mdmx_op, spec3_op, 440 lb_op, lh_op, lwl_op, lw_op, // 20 441 lbu_op, lhu_op, lwr_op, lwu_op, 442 sb_op, sh_op, swl_op, sw_op, // 28 443 sdl_op, sdr_op, swr_op, cache_op, 444 ll_op, lwc1_op, lwc2_op, pref_op, // 30 445 lld_op, ldc1_op, ldc2_op, ld_op, 446 sc_op, swc1_op, swc2_op, rsrv_3b_op, // 38 447 scd_op, sdc1_op, sdc2_op, sd_op 448 }; 449 450 451 // func field for special opcode 452 enum func_spec_op { 453 sll_fn, movc_fn, srl_fn, sra_fn, // 00 454 sllv_fn, pmon_fn, srlv_fn, srav_fn, 455 jr_fn, jalr_fn, movz_fn, movn_fn, // 08 456 syscall_fn, break_fn, spim_fn, sync_fn, 457 mfhi_fn, mthi_fn, mflo_fn, mtlo_fn, // 10 458 dsllv_fn, rsrv_spec_2, dsrlv_fn, dsrav_fn, 459 mult_fn, multu_fn, div_fn, divu_fn, // 18 460 dmult_fn, dmultu_fn, ddiv_fn, ddivu_fn, 461 add_fn, addu_fn, sub_fn, subu_fn, // 20 462 and_fn, or_fn, xor_fn, nor_fn, 463 rsrv_spec_3, rsrv_spec_4, slt_fn, sltu_fn, // 28 464 dadd_fn, daddu_fn, dsub_fn, dsubu_fn, 465 tge_fn, tgeu_fn, tlt_fn, tltu_fn, // 30 466 teq_fn, rsrv_spec_5, tne_fn, rsrv_spec_6, 467 dsll_fn, rsrv_spec_7, dsrl_fn, dsra_fn, // 38 468 dsll32_fn, rsrv_spec_8, dsrl32_fn, dsra32_fn 469 }; 470 471 // func field for spec2 opcode 472 enum func_spec2_op { 473 madd_fn, maddu_fn, mul_fn, rsrv_spec2_3, 474 msub_fn, msubu_fn, 475 clz_fn = 0x20, clo_fn, 476 dclz_fn = 0x24, dclo_fn, 477 sdbbp_fn = 0x3f 478 }; 479 480 // func field for spec3 opcode 481 enum func_spec3_op { 482 ext_fn, dextm_fn, dextu_fn, dext_fn, 483 ins_fn, dinsm_fn, dinsu_fn, dins_fn, 484 bshfl_fn = 0x20, 485 dbshfl_fn = 0x24, 486 rdhwr_fn = 0x3b 487 }; 488 489 // sa field for spec3 opcodes, with BSHFL function 490 enum func_spec3_bshfl { 491 wsbh_fn = 0x02, 492 seb_fn = 0x10, 493 seh_fn = 0x18 494 }; 495 496 // rt field of regimm opcodes. 497 enum regimm_fn { 498 bltz_fn, bgez_fn, bltzl_fn, bgezl_fn, 499 rsrv_ri_fn4, rsrv_ri_fn5, rsrv_ri_fn6, rsrv_ri_fn7, 500 tgei_fn, tgeiu_fn, tlti_fn, tltiu_fn, 501 teqi_fn, rsrv_ri_fn_0d, tnei_fn, rsrv_ri_fn0f, 502 bltzal_fn, bgezal_fn, bltzall_fn, bgezall_fn, 503 bposge32_fn= 0x1c, 504 synci_fn = 0x1f 505 }; 506 507 508 // func field for mad opcodes (MIPS IV). 509 enum mad_func { 510 madd_fp_op = 0x08, msub_fp_op = 0x0a, 511 nmadd_fp_op = 0x0c, nmsub_fp_op = 0x0e 512 }; 513 514 515 enum mips_inst_shifts { 516 OP_SHF = 26, 517 JTARGET_SHF = 0, 518 RS_SHF = 21, 519 RT_SHF = 16, 520 RD_SHF = 11, 521 RE_SHF = 6, 522 SA_SHF = RE_SHF, // synonym 523 IMM_SHF = 0, 524 FUNC_SHF = 0, 525 526 // mask values 527 MSK_16 = 0xffff, 528 529 530 CACHEOP_SHF = 18, 531 CACHESEL_SHF = 16, 532 }; 533 }; 534 535 enum mips_regnames { 536 R_zero = 0, 537 R_at, R_v0, R_v1, R_a0, R_a1, R_a2, R_a3, 538 #if __mips_isa_rev < 6 539 R_t0, R_t1, R_t2, R_t3, R_t4, R_t5, R_t6, R_t7, 540 #else 541 R_a4, R_a5, R_a6, R_a7, R_t0, R_t1, R_t2, R_t3, 542 #endif 543 R_s0, R_s1, R_s2, R_s3, R_s4, R_s5, R_s6, R_s7, 544 R_t8, R_t9, R_k0, R_k1, R_gp, R_sp, R_s8, R_ra, 545 R_lr = R_s8, 546 547 // arm regs 0-15 are mips regs 2-17 (meaning s0 & s1 are used) 548 R_at2 = R_s2, // R_at2 = 18 = s2 549 R_cmp = R_s3, // R_cmp = 19 = s3 550 R_cmp2 = R_s4 // R_cmp2 = 20 = s4 551 }; 552 553 554 555 }; // namespace android 556 557 #endif //ANDROID_MIPSASSEMBLER_H 558