MIPS64Assembler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* libs/pixelflinger/codeflinger/MIPS64Assembler.h
  2. **
  3. ** Copyright 2015, 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. #ifndef ANDROID_MIPS64ASSEMBLER_H
  18. #define ANDROID_MIPS64ASSEMBLER_H
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. #include "utils/KeyedVector.h"
  22. #include "utils/Vector.h"
  23. #include "tinyutils/smartpointer.h"
  24. #include "ARMAssemblerInterface.h"
  25. #include "MIPSAssembler.h"
  26. #include "CodeCache.h"
  27. namespace android {
  28. class MIPS64Assembler; // forward reference
  29. // this class mimics ARMAssembler interface
  30. // intent is to translate each ARM instruction to 1 or more MIPS instr
  31. // implementation calls MIPS64Assembler class to generate mips code
  32. class ArmToMips64Assembler : public ARMAssemblerInterface
  33. {
  34. public:
  35. ArmToMips64Assembler(const sp<Assembly>& assembly,
  36. char *abuf = 0, int linesz = 0, int instr_count = 0);
  37. ArmToMips64Assembler(void* assembly);
  38. virtual ~ArmToMips64Assembler();
  39. uint32_t* base() const;
  40. uint32_t* pc() const;
  41. void disassemble(const char* name);
  42. virtual void reset();
  43. virtual int generate(const char* name);
  44. virtual int getCodegenArch();
  45. virtual void prolog();
  46. virtual void epilog(uint32_t touched);
  47. virtual void comment(const char* string);
  48. // for testing purposes
  49. void fix_branches();
  50. void set_condition(int mode, int R1, int R2);
  51. // -----------------------------------------------------------------------
  52. // shifters and addressing modes
  53. // -----------------------------------------------------------------------
  54. // shifters...
  55. virtual bool isValidImmediate(uint32_t immed);
  56. virtual int buildImmediate(uint32_t i, uint32_t& rot, uint32_t& imm);
  57. virtual uint32_t imm(uint32_t immediate);
  58. virtual uint32_t reg_imm(int Rm, int type, uint32_t shift);
  59. virtual uint32_t reg_rrx(int Rm);
  60. virtual uint32_t reg_reg(int Rm, int type, int Rs);
  61. // addressing modes...
  62. // LDR(B)/STR(B)/PLD
  63. // (immediate and Rm can be negative, which indicates U=0)
  64. virtual uint32_t immed12_pre(int32_t immed12, int W=0);
  65. virtual uint32_t immed12_post(int32_t immed12);
  66. virtual uint32_t reg_scale_pre(int Rm, int type=0, uint32_t shift=0, int W=0);
  67. virtual uint32_t reg_scale_post(int Rm, int type=0, uint32_t shift=0);
  68. // LDRH/LDRSB/LDRSH/STRH
  69. // (immediate and Rm can be negative, which indicates U=0)
  70. virtual uint32_t immed8_pre(int32_t immed8, int W=0);
  71. virtual uint32_t immed8_post(int32_t immed8);
  72. virtual uint32_t reg_pre(int Rm, int W=0);
  73. virtual uint32_t reg_post(int Rm);
  74. virtual void dataProcessing(int opcode, int cc, int s,
  75. int Rd, int Rn,
  76. uint32_t Op2);
  77. virtual void MLA(int cc, int s,
  78. int Rd, int Rm, int Rs, int Rn);
  79. virtual void MUL(int cc, int s,
  80. int Rd, int Rm, int Rs);
  81. virtual void UMULL(int cc, int s,
  82. int RdLo, int RdHi, int Rm, int Rs);
  83. virtual void UMUAL(int cc, int s,
  84. int RdLo, int RdHi, int Rm, int Rs);
  85. virtual void SMULL(int cc, int s,
  86. int RdLo, int RdHi, int Rm, int Rs);
  87. virtual void SMUAL(int cc, int s,
  88. int RdLo, int RdHi, int Rm, int Rs);
  89. virtual void B(int cc, uint32_t* pc);
  90. virtual void BL(int cc, uint32_t* pc);
  91. virtual void BX(int cc, int Rn);
  92. virtual void label(const char* theLabel);
  93. virtual void B(int cc, const char* label);
  94. virtual void BL(int cc, const char* label);
  95. virtual uint32_t* pcForLabel(const char* label);
  96. virtual void LDR (int cc, int Rd,
  97. int Rn, uint32_t offset = 0);
  98. virtual void LDRB(int cc, int Rd,
  99. int Rn, uint32_t offset = 0);
  100. virtual void STR (int cc, int Rd,
  101. int Rn, uint32_t offset = 0);
  102. virtual void STRB(int cc, int Rd,
  103. int Rn, uint32_t offset = 0);
  104. virtual void LDRH (int cc, int Rd,
  105. int Rn, uint32_t offset = 0);
  106. virtual void LDRSB(int cc, int Rd,
  107. int Rn, uint32_t offset = 0);
  108. virtual void LDRSH(int cc, int Rd,
  109. int Rn, uint32_t offset = 0);
  110. virtual void STRH (int cc, int Rd,
  111. int Rn, uint32_t offset = 0);
  112. virtual void LDM(int cc, int dir,
  113. int Rn, int W, uint32_t reg_list);
  114. virtual void STM(int cc, int dir,
  115. int Rn, int W, uint32_t reg_list);
  116. virtual void SWP(int cc, int Rn, int Rd, int Rm);
  117. virtual void SWPB(int cc, int Rn, int Rd, int Rm);
  118. virtual void SWI(int cc, uint32_t comment);
  119. virtual void PLD(int Rn, uint32_t offset);
  120. virtual void CLZ(int cc, int Rd, int Rm);
  121. virtual void QADD(int cc, int Rd, int Rm, int Rn);
  122. virtual void QDADD(int cc, int Rd, int Rm, int Rn);
  123. virtual void QSUB(int cc, int Rd, int Rm, int Rn);
  124. virtual void QDSUB(int cc, int Rd, int Rm, int Rn);
  125. virtual void SMUL(int cc, int xy,
  126. int Rd, int Rm, int Rs);
  127. virtual void SMULW(int cc, int y,
  128. int Rd, int Rm, int Rs);
  129. virtual void SMLA(int cc, int xy,
  130. int Rd, int Rm, int Rs, int Rn);
  131. virtual void SMLAL(int cc, int xy,
  132. int RdHi, int RdLo, int Rs, int Rm);
  133. virtual void SMLAW(int cc, int y,
  134. int Rd, int Rm, int Rs, int Rn);
  135. // byte/half word extract...
  136. virtual void UXTB16(int cc, int Rd, int Rm, int rotate);
  137. // bit manipulation...
  138. virtual void UBFX(int cc, int Rd, int Rn, int lsb, int width);
  139. // Address loading/storing/manipulation
  140. virtual void ADDR_LDR(int cc, int Rd, int Rn, uint32_t offset = __immed12_pre(0));
  141. virtual void ADDR_STR(int cc, int Rd, int Rn, uint32_t offset = __immed12_pre(0));
  142. virtual void ADDR_ADD(int cc, int s, int Rd, int Rn, uint32_t Op2);
  143. virtual void ADDR_SUB(int cc, int s, int Rd, int Rn, uint32_t Op2);
  144. // this is some crap to share is MIPS64Assembler class for debug
  145. char * mArmDisassemblyBuffer;
  146. int mArmLineLength;
  147. int mArmInstrCount;
  148. int mInum; // current arm instuction number (0..n)
  149. uint32_t** mArmPC; // array: PC for 1st mips instr of
  150. // each translated ARM instr
  151. private:
  152. ArmToMips64Assembler(const ArmToMips64Assembler& rhs);
  153. ArmToMips64Assembler& operator = (const ArmToMips64Assembler& rhs);
  154. void init_conditional_labels(void);
  155. void protectConditionalOperands(int Rd);
  156. // reg__tmp set to MIPS AT, reg 1
  157. int dataProcAdrModes(int op, int& source, bool sign = false, int reg_tmp = 1);
  158. sp<Assembly> mAssembly;
  159. MIPS64Assembler* mMips;
  160. enum misc_constants_t {
  161. ARM_MAX_INSTUCTIONS = 512 // based on ASSEMBLY_SCRATCH_SIZE
  162. };
  163. enum {
  164. SRC_REG = 0,
  165. SRC_IMM,
  166. SRC_ERROR = -1
  167. };
  168. enum addr_modes {
  169. // start above the range of legal mips reg #'s (0-31)
  170. AMODE_REG = 0x20,
  171. AMODE_IMM, AMODE_REG_IMM, // for data processing
  172. AMODE_IMM_12_PRE, AMODE_IMM_12_POST, // for load/store
  173. AMODE_REG_SCALE_PRE, AMODE_IMM_8_PRE,
  174. AMODE_IMM_8_POST, AMODE_REG_PRE,
  175. AMODE_UNSUPPORTED
  176. };
  177. struct addr_mode_t { // address modes for current ARM instruction
  178. int reg;
  179. int stype;
  180. uint32_t value;
  181. bool writeback; // writeback the adr reg after modification
  182. } amode;
  183. enum cond_types {
  184. CMP_COND = 1,
  185. SBIT_COND
  186. };
  187. struct cond_mode_t { // conditional-execution info for current ARM instruction
  188. cond_types type;
  189. int r1;
  190. int r2;
  191. int labelnum;
  192. char label[100][10];
  193. } cond;
  194. };
  195. // ----------------------------------------------------------------------------
  196. // ----------------------------------------------------------------------------
  197. // ----------------------------------------------------------------------------
  198. // This is the basic MIPS64 assembler, which just creates the opcodes in memory.
  199. // All the more complicated work is done in ArmToMips64Assember above.
  200. // Inherits MIPSAssembler class, and overrides only MIPS64r6 specific stuff
  201. class MIPS64Assembler : public MIPSAssembler
  202. {
  203. public:
  204. MIPS64Assembler(const sp<Assembly>& assembly, ArmToMips64Assembler *parent);
  205. MIPS64Assembler(void* assembly, ArmToMips64Assembler *parent);
  206. virtual ~MIPS64Assembler();
  207. virtual void reset();
  208. virtual void disassemble(const char* name);
  209. void fix_branches();
  210. // ------------------------------------------------------------------------
  211. // MIPS64AssemblerInterface...
  212. // ------------------------------------------------------------------------
  213. #if 0
  214. #pragma mark -
  215. #pragma mark Arithmetic...
  216. #endif
  217. void DADDU(int Rd, int Rs, int Rt);
  218. void DADDIU(int Rt, int Rs, int16_t imm);
  219. void DSUBU(int Rd, int Rs, int Rt);
  220. void DSUBIU(int Rt, int Rs, int16_t imm);
  221. virtual void MUL(int Rd, int Rs, int Rt);
  222. void MUH(int Rd, int Rs, int Rt);
  223. #if 0
  224. #pragma mark -
  225. #pragma mark Logical...
  226. #endif
  227. virtual void CLO(int Rd, int Rs);
  228. virtual void CLZ(int Rd, int Rs);
  229. #if 0
  230. #pragma mark -
  231. #pragma mark Load/store...
  232. #endif
  233. void LD(int Rt, int Rbase, int16_t offset);
  234. void SD(int Rt, int Rbase, int16_t offset);
  235. virtual void LUI(int Rt, int16_t offset);
  236. #if 0
  237. #pragma mark -
  238. #pragma mark Branch...
  239. #endif
  240. void JR(int Rs);
  241. protected:
  242. ArmToMips64Assembler *mParent;
  243. // opcode field of all instructions
  244. enum opcode_field {
  245. spec_op, regimm_op, j_op, jal_op, // 0x00 - 0x03
  246. beq_op, bne_op, pop06_op, pop07_op, // 0x04 - 0x07
  247. pop10_op, addiu_op, slti_op, sltiu_op, // 0x08 - 0x0b
  248. andi_op, ori_op, xori_op, aui_op, // 0x0c - 0x0f
  249. cop0_op, cop1_op, cop2_op, rsrv_opc_0, // 0x10 - 0x13
  250. rsrv_opc_1, rsrv_opc_2, pop26_op, pop27_op, // 0x14 - 0x17
  251. pop30_op, daddiu_op, rsrv_opc_3, rsrv_opc_4, // 0x18 - 0x1b
  252. rsrv_opc_5, daui_op, msa_op, spec3_op, // 0x1c - 0x1f
  253. lb_op, lh_op, rsrv_opc_6, lw_op, // 0x20 - 0x23
  254. lbu_op, lhu_op, rsrv_opc_7, lwu_op, // 0x24 - 0x27
  255. sb_op, sh_op, rsrv_opc_8, sw_op, // 0x28 - 0x2b
  256. rsrv_opc_9, rsrv_opc_10, rsrv_opc_11, rsrv_opc_12, // 0x2c - 0x2f
  257. rsrv_opc_13, lwc1_op, bc_op, rsrv_opc_14, // 0x2c - 0x2f
  258. rsrv_opc_15, ldc1_op, pop66_op, ld_op, // 0x30 - 0x33
  259. rsrv_opc_16, swc1_op, balc_op, pcrel_op, // 0x34 - 0x37
  260. rsrv_opc_17, sdc1_op, pop76_op, sd_op // 0x38 - 0x3b
  261. };
  262. // func field for special opcode
  263. enum func_spec_op {
  264. sll_fn, rsrv_spec_0, srl_fn, sra_fn,
  265. sllv_fn, lsa_fn, srlv_fn, srav_fn,
  266. rsrv_spec_1, jalr_fn, rsrv_spec_2, rsrv_spec_3,
  267. syscall_fn, break_fn, sdbbp_fn, sync_fn,
  268. clz_fn, clo_fn, dclz_fn, dclo_fn,
  269. dsllv_fn, dlsa_fn, dsrlv_fn, dsrav_fn,
  270. sop30_fn, sop31_fn, sop32_fn, sop33_fn,
  271. sop34_fn, sop35_fn, sop36_fn, sop37_fn,
  272. add_fn, addu_fn, sub_fn, subu_fn,
  273. and_fn, or_fn, xor_fn, nor_fn,
  274. rsrv_spec_4, rsrv_spec_5, slt_fn, sltu_fn,
  275. dadd_fn, daddu_fn, dsub_fn, dsubu_fn,
  276. tge_fn, tgeu_fn, tlt_fn, tltu_fn,
  277. teq_fn, seleqz_fn, tne_fn, selnez_fn,
  278. dsll_fn, rsrv_spec_6, dsrl_fn, dsra_fn,
  279. dsll32_fn, rsrv_spec_7, dsrl32_fn, dsra32_fn
  280. };
  281. // func field for spec3 opcode
  282. enum func_spec3_op {
  283. ext_fn, dextm_fn, dextu_fn, dext_fn,
  284. ins_fn, dinsm_fn, dinsu_fn, dins_fn,
  285. cachee_fn = 0x1b, sbe_fn, she_fn, sce_fn, swe_fn,
  286. bshfl_fn, prefe_fn = 0x23, dbshfl_fn, cache_fn, sc_fn, scd_fn,
  287. lbue_fn, lhue_fn, lbe_fn = 0x2c, lhe_fn, lle_fn, lwe_fn,
  288. pref_fn = 0x35, ll_fn, lld_fn, rdhwr_fn = 0x3b
  289. };
  290. // sa field for spec3 opcodes, with BSHFL function
  291. enum func_spec3_bshfl {
  292. bitswap_fn,
  293. wsbh_fn = 0x02,
  294. dshd_fn = 0x05,
  295. seb_fn = 0x10,
  296. seh_fn = 0x18
  297. };
  298. // rt field of regimm opcodes.
  299. enum regimm_fn {
  300. bltz_fn, bgez_fn,
  301. dahi_fn = 0x6,
  302. nal_fn = 0x10, bal_fn, bltzall_fn, bgezall_fn,
  303. sigrie_fn = 0x17,
  304. dati_fn = 0x1e, synci_fn
  305. };
  306. enum muldiv_fn {
  307. mul_fn = 0x02, muh_fn
  308. };
  309. enum mips_inst_shifts {
  310. OP_SHF = 26,
  311. JTARGET_SHF = 0,
  312. RS_SHF = 21,
  313. RT_SHF = 16,
  314. RD_SHF = 11,
  315. RE_SHF = 6,
  316. SA_SHF = RE_SHF, // synonym
  317. IMM_SHF = 0,
  318. FUNC_SHF = 0,
  319. // mask values
  320. MSK_16 = 0xffff,
  321. CACHEOP_SHF = 18,
  322. CACHESEL_SHF = 16,
  323. };
  324. };
  325. }; // namespace android
  326. #endif //ANDROID_MIPS64ASSEMBLER_H