lj_target_mips.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. ** Definitions for MIPS CPUs.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_TARGET_MIPS_H
  6. #define _LJ_TARGET_MIPS_H
  7. /* -- Registers IDs ------------------------------------------------------- */
  8. #define GPRDEF(_) \
  9. _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \
  10. _(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15) \
  11. _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \
  12. _(R24) _(R25) _(SYS1) _(SYS2) _(R28) _(SP) _(R30) _(RA)
  13. #if LJ_SOFTFP
  14. #define FPRDEF(_)
  15. #else
  16. #define FPRDEF(_) \
  17. _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \
  18. _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \
  19. _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \
  20. _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31)
  21. #endif
  22. #define VRIDDEF(_)
  23. #define RIDENUM(name) RID_##name,
  24. enum {
  25. GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */
  26. FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */
  27. RID_MAX,
  28. RID_ZERO = RID_R0,
  29. RID_TMP = RID_RA,
  30. RID_GP = RID_R28,
  31. /* Calling conventions. */
  32. RID_RET = RID_R2,
  33. #if LJ_LE
  34. RID_RETHI = RID_R3,
  35. RID_RETLO = RID_R2,
  36. #else
  37. RID_RETHI = RID_R2,
  38. RID_RETLO = RID_R3,
  39. #endif
  40. #if LJ_SOFTFP
  41. RID_FPRET = RID_R2,
  42. #else
  43. RID_FPRET = RID_F0,
  44. #endif
  45. RID_CFUNCADDR = RID_R25,
  46. /* These definitions must match with the *.dasc file(s): */
  47. RID_BASE = RID_R16, /* Interpreter BASE. */
  48. RID_LPC = RID_R18, /* Interpreter PC. */
  49. RID_DISPATCH = RID_R19, /* Interpreter DISPATCH table. */
  50. RID_LREG = RID_R20, /* Interpreter L. */
  51. RID_JGL = RID_R30, /* On-trace: global_State + 32768. */
  52. /* Register ranges [min, max) and number of registers. */
  53. RID_MIN_GPR = RID_R0,
  54. RID_MAX_GPR = RID_RA+1,
  55. RID_MIN_FPR = RID_MAX_GPR,
  56. #if LJ_SOFTFP
  57. RID_MAX_FPR = RID_MIN_FPR,
  58. #else
  59. RID_MAX_FPR = RID_F31+1,
  60. #endif
  61. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,
  62. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR /* Only even regs are used. */
  63. };
  64. #define RID_NUM_KREF RID_NUM_GPR
  65. #define RID_MIN_KREF RID_R0
  66. /* -- Register sets ------------------------------------------------------- */
  67. /* Make use of all registers, except ZERO, TMP, SP, SYS1, SYS2, JGL and GP. */
  68. #define RSET_FIXED \
  69. (RID2RSET(RID_ZERO)|RID2RSET(RID_TMP)|RID2RSET(RID_SP)|\
  70. RID2RSET(RID_SYS1)|RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)|RID2RSET(RID_GP))
  71. #define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED)
  72. #if LJ_SOFTFP
  73. #define RSET_FPR 0
  74. #else
  75. #if LJ_32
  76. #define RSET_FPR \
  77. (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\
  78. RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\
  79. RID2RSET(RID_F16)|RID2RSET(RID_F18)|RID2RSET(RID_F20)|RID2RSET(RID_F22)|\
  80. RID2RSET(RID_F24)|RID2RSET(RID_F26)|RID2RSET(RID_F28)|RID2RSET(RID_F30))
  81. #else
  82. #define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)
  83. #endif
  84. #endif
  85. #define RSET_ALL (RSET_GPR|RSET_FPR)
  86. #define RSET_INIT RSET_ALL
  87. #define RSET_SCRATCH_GPR \
  88. (RSET_RANGE(RID_R1, RID_R15+1)|\
  89. RID2RSET(RID_R24)|RID2RSET(RID_R25))
  90. #if LJ_SOFTFP
  91. #define RSET_SCRATCH_FPR 0
  92. #else
  93. #if LJ_32
  94. #define RSET_SCRATCH_FPR \
  95. (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\
  96. RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\
  97. RID2RSET(RID_F16)|RID2RSET(RID_F18))
  98. #else
  99. #define RSET_SCRATCH_FPR RSET_RANGE(RID_F0, RID_F24)
  100. #endif
  101. #endif
  102. #define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)
  103. #define REGARG_FIRSTGPR RID_R4
  104. #if LJ_32
  105. #define REGARG_LASTGPR RID_R7
  106. #define REGARG_NUMGPR 4
  107. #else
  108. #define REGARG_LASTGPR RID_R11
  109. #define REGARG_NUMGPR 8
  110. #endif
  111. #if LJ_ABI_SOFTFP
  112. #define REGARG_FIRSTFPR 0
  113. #define REGARG_LASTFPR 0
  114. #define REGARG_NUMFPR 0
  115. #else
  116. #define REGARG_FIRSTFPR RID_F12
  117. #if LJ_32
  118. #define REGARG_LASTFPR RID_F14
  119. #define REGARG_NUMFPR 2
  120. #else
  121. #define REGARG_LASTFPR RID_F19
  122. #define REGARG_NUMFPR 8
  123. #endif
  124. #endif
  125. /* -- Spill slots --------------------------------------------------------- */
  126. /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.
  127. **
  128. ** SPS_FIXED: Available fixed spill slots in interpreter frame.
  129. ** This definition must match with the *.dasc file(s).
  130. **
  131. ** SPS_FIRST: First spill slot for general use.
  132. */
  133. #if LJ_32
  134. #define SPS_FIXED 5
  135. #else
  136. #define SPS_FIXED 4
  137. #endif
  138. #define SPS_FIRST 4
  139. #define SPOFS_TMP 0
  140. #define sps_scale(slot) (4 * (int32_t)(slot))
  141. #define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1)
  142. /* -- Exit state ---------------------------------------------------------- */
  143. /* This definition must match with the *.dasc file(s). */
  144. typedef struct {
  145. #if !LJ_SOFTFP
  146. lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */
  147. #endif
  148. intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */
  149. int32_t spill[256]; /* Spill slots. */
  150. } ExitState;
  151. /* Highest exit + 1 indicates stack check. */
  152. #define EXITSTATE_CHECKEXIT 1
  153. /* Return the address of a per-trace exit stub. */
  154. static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p)
  155. {
  156. while (*p == 0x00000000) p++; /* Skip MIPSI_NOP. */
  157. return p;
  158. }
  159. /* Avoid dependence on lj_jit.h if only including lj_target.h. */
  160. #define exitstub_trace_addr(T, exitno) \
  161. exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode))
  162. /* -- Instructions -------------------------------------------------------- */
  163. /* Instruction fields. */
  164. #define MIPSF_S(r) ((r) << 21)
  165. #define MIPSF_T(r) ((r) << 16)
  166. #define MIPSF_D(r) ((r) << 11)
  167. #define MIPSF_R(r) ((r) << 21)
  168. #define MIPSF_H(r) ((r) << 16)
  169. #define MIPSF_G(r) ((r) << 11)
  170. #define MIPSF_F(r) ((r) << 6)
  171. #define MIPSF_A(n) ((n) << 6)
  172. #define MIPSF_M(n) ((n) << 11)
  173. #define MIPSF_L(n) ((n) << 6)
  174. typedef enum MIPSIns {
  175. MIPSI_D = 0x38,
  176. MIPSI_DV = 0x10,
  177. MIPSI_D32 = 0x3c,
  178. /* Integer instructions. */
  179. MIPSI_MOVE = 0x00000025,
  180. MIPSI_NOP = 0x00000000,
  181. MIPSI_LI = 0x24000000,
  182. MIPSI_LU = 0x34000000,
  183. MIPSI_LUI = 0x3c000000,
  184. MIPSI_AND = 0x00000024,
  185. MIPSI_ANDI = 0x30000000,
  186. MIPSI_OR = 0x00000025,
  187. MIPSI_ORI = 0x34000000,
  188. MIPSI_XOR = 0x00000026,
  189. MIPSI_XORI = 0x38000000,
  190. MIPSI_NOR = 0x00000027,
  191. MIPSI_SLT = 0x0000002a,
  192. MIPSI_SLTU = 0x0000002b,
  193. MIPSI_SLTI = 0x28000000,
  194. MIPSI_SLTIU = 0x2c000000,
  195. MIPSI_ADDU = 0x00000021,
  196. MIPSI_ADDIU = 0x24000000,
  197. MIPSI_SUB = 0x00000022,
  198. MIPSI_SUBU = 0x00000023,
  199. #if !LJ_TARGET_MIPSR6
  200. MIPSI_MUL = 0x70000002,
  201. MIPSI_DIV = 0x0000001a,
  202. MIPSI_DIVU = 0x0000001b,
  203. MIPSI_MOVZ = 0x0000000a,
  204. MIPSI_MOVN = 0x0000000b,
  205. MIPSI_MFHI = 0x00000010,
  206. MIPSI_MFLO = 0x00000012,
  207. MIPSI_MULT = 0x00000018,
  208. #else
  209. MIPSI_MUL = 0x00000098,
  210. MIPSI_MUH = 0x000000d8,
  211. MIPSI_DIV = 0x0000009a,
  212. MIPSI_DIVU = 0x0000009b,
  213. MIPSI_SELEQZ = 0x00000035,
  214. MIPSI_SELNEZ = 0x00000037,
  215. #endif
  216. MIPSI_SLL = 0x00000000,
  217. MIPSI_SRL = 0x00000002,
  218. MIPSI_SRA = 0x00000003,
  219. MIPSI_ROTR = 0x00200002, /* MIPSXXR2 */
  220. MIPSI_DROTR = 0x0020003a,
  221. MIPSI_DROTR32 = 0x0020003e,
  222. MIPSI_SLLV = 0x00000004,
  223. MIPSI_SRLV = 0x00000006,
  224. MIPSI_SRAV = 0x00000007,
  225. MIPSI_ROTRV = 0x00000046, /* MIPSXXR2 */
  226. MIPSI_DROTRV = 0x00000056,
  227. MIPSI_INS = 0x7c000004, /* MIPSXXR2 */
  228. MIPSI_SEB = 0x7c000420, /* MIPSXXR2 */
  229. MIPSI_SEH = 0x7c000620, /* MIPSXXR2 */
  230. MIPSI_WSBH = 0x7c0000a0, /* MIPSXXR2 */
  231. MIPSI_DSBH = 0x7c0000a4,
  232. MIPSI_B = 0x10000000,
  233. MIPSI_J = 0x08000000,
  234. MIPSI_JAL = 0x0c000000,
  235. #if !LJ_TARGET_MIPSR6
  236. MIPSI_JALX = 0x74000000,
  237. MIPSI_JR = 0x00000008,
  238. #else
  239. MIPSI_JR = 0x00000009,
  240. MIPSI_BALC = 0xe8000000,
  241. #endif
  242. MIPSI_JALR = 0x0000f809,
  243. MIPSI_BEQ = 0x10000000,
  244. MIPSI_BNE = 0x14000000,
  245. MIPSI_BLEZ = 0x18000000,
  246. MIPSI_BGTZ = 0x1c000000,
  247. MIPSI_BLTZ = 0x04000000,
  248. MIPSI_BGEZ = 0x04010000,
  249. /* Load/store instructions. */
  250. MIPSI_LW = 0x8c000000,
  251. MIPSI_LD = 0xdc000000,
  252. MIPSI_SW = 0xac000000,
  253. MIPSI_SD = 0xfc000000,
  254. MIPSI_LB = 0x80000000,
  255. MIPSI_SB = 0xa0000000,
  256. MIPSI_LH = 0x84000000,
  257. MIPSI_SH = 0xa4000000,
  258. MIPSI_LBU = 0x90000000,
  259. MIPSI_LHU = 0x94000000,
  260. MIPSI_LWC1 = 0xc4000000,
  261. MIPSI_SWC1 = 0xe4000000,
  262. MIPSI_LDC1 = 0xd4000000,
  263. MIPSI_SDC1 = 0xf4000000,
  264. /* MIPS64 instructions. */
  265. MIPSI_DADD = 0x0000002c,
  266. MIPSI_DADDU = 0x0000002d,
  267. MIPSI_DADDIU = 0x64000000,
  268. MIPSI_DSUB = 0x0000002e,
  269. MIPSI_DSUBU = 0x0000002f,
  270. #if !LJ_TARGET_MIPSR6
  271. MIPSI_DDIV = 0x0000001e,
  272. MIPSI_DDIVU = 0x0000001f,
  273. MIPSI_DMULT = 0x0000001c,
  274. MIPSI_DMULTU = 0x0000001d,
  275. #else
  276. MIPSI_DDIV = 0x0000009e,
  277. MIPSI_DMOD = 0x000000de,
  278. MIPSI_DDIVU = 0x0000009f,
  279. MIPSI_DMODU = 0x000000df,
  280. MIPSI_DMUL = 0x0000009c,
  281. MIPSI_DMUH = 0x000000dc,
  282. #endif
  283. MIPSI_DSLL = 0x00000038,
  284. MIPSI_DSRL = 0x0000003a,
  285. MIPSI_DSLLV = 0x00000014,
  286. MIPSI_DSRLV = 0x00000016,
  287. MIPSI_DSRA = 0x0000003b,
  288. MIPSI_DSRAV = 0x00000017,
  289. MIPSI_DSRA32 = 0x0000003f,
  290. MIPSI_DSLL32 = 0x0000003c,
  291. MIPSI_DSRL32 = 0x0000003e,
  292. MIPSI_DSHD = 0x7c000164,
  293. MIPSI_AADDU = LJ_32 ? MIPSI_ADDU : MIPSI_DADDU,
  294. MIPSI_AADDIU = LJ_32 ? MIPSI_ADDIU : MIPSI_DADDIU,
  295. MIPSI_ASUBU = LJ_32 ? MIPSI_SUBU : MIPSI_DSUBU,
  296. MIPSI_AL = LJ_32 ? MIPSI_LW : MIPSI_LD,
  297. MIPSI_AS = LJ_32 ? MIPSI_SW : MIPSI_SD,
  298. #if LJ_TARGET_MIPSR6
  299. MIPSI_LSA = 0x00000005,
  300. MIPSI_DLSA = 0x00000015,
  301. MIPSI_ALSA = LJ_32 ? MIPSI_LSA : MIPSI_DLSA,
  302. #endif
  303. /* Extract/insert instructions. */
  304. MIPSI_DEXTM = 0x7c000001,
  305. MIPSI_DEXTU = 0x7c000002,
  306. MIPSI_DEXT = 0x7c000003,
  307. MIPSI_DINSM = 0x7c000005,
  308. MIPSI_DINSU = 0x7c000006,
  309. MIPSI_DINS = 0x7c000007,
  310. MIPSI_FLOOR_D = 0x4620000b,
  311. /* FP instructions. */
  312. MIPSI_MOV_S = 0x46000006,
  313. MIPSI_MOV_D = 0x46200006,
  314. #if !LJ_TARGET_MIPSR6
  315. MIPSI_MOVT_D = 0x46210011,
  316. MIPSI_MOVF_D = 0x46200011,
  317. #else
  318. MIPSI_MIN_D = 0x4620001C,
  319. MIPSI_MAX_D = 0x4620001E,
  320. MIPSI_SEL_D = 0x46200010,
  321. #endif
  322. MIPSI_ABS_D = 0x46200005,
  323. MIPSI_NEG_D = 0x46200007,
  324. MIPSI_ADD_D = 0x46200000,
  325. MIPSI_SUB_D = 0x46200001,
  326. MIPSI_MUL_D = 0x46200002,
  327. MIPSI_DIV_D = 0x46200003,
  328. MIPSI_SQRT_D = 0x46200004,
  329. MIPSI_ADD_S = 0x46000000,
  330. MIPSI_SUB_S = 0x46000001,
  331. MIPSI_CVT_D_S = 0x46000021,
  332. MIPSI_CVT_W_S = 0x46000024,
  333. MIPSI_CVT_S_D = 0x46200020,
  334. MIPSI_CVT_W_D = 0x46200024,
  335. MIPSI_CVT_S_W = 0x46800020,
  336. MIPSI_CVT_D_W = 0x46800021,
  337. MIPSI_CVT_S_L = 0x46a00020,
  338. MIPSI_CVT_D_L = 0x46a00021,
  339. MIPSI_TRUNC_W_S = 0x4600000d,
  340. MIPSI_TRUNC_W_D = 0x4620000d,
  341. MIPSI_TRUNC_L_S = 0x46000009,
  342. MIPSI_TRUNC_L_D = 0x46200009,
  343. MIPSI_FLOOR_W_S = 0x4600000f,
  344. MIPSI_FLOOR_W_D = 0x4620000f,
  345. MIPSI_MFC1 = 0x44000000,
  346. MIPSI_MTC1 = 0x44800000,
  347. MIPSI_DMTC1 = 0x44a00000,
  348. MIPSI_DMFC1 = 0x44200000,
  349. #if !LJ_TARGET_MIPSR6
  350. MIPSI_BC1F = 0x45000000,
  351. MIPSI_BC1T = 0x45010000,
  352. MIPSI_C_EQ_D = 0x46200032,
  353. MIPSI_C_OLT_S = 0x46000034,
  354. MIPSI_C_OLT_D = 0x46200034,
  355. MIPSI_C_ULT_D = 0x46200035,
  356. MIPSI_C_OLE_D = 0x46200036,
  357. MIPSI_C_ULE_D = 0x46200037,
  358. #else
  359. MIPSI_BC1EQZ = 0x45200000,
  360. MIPSI_BC1NEZ = 0x45a00000,
  361. MIPSI_CMP_EQ_D = 0x46a00002,
  362. MIPSI_CMP_LT_S = 0x46800004,
  363. MIPSI_CMP_LT_D = 0x46a00004,
  364. #endif
  365. } MIPSIns;
  366. #endif