1 /*
2 ** Definitions for PPC CPUs.
3 ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
4 */
5 
6 #ifndef _LJ_TARGET_PPC_H
7 #define _LJ_TARGET_PPC_H
8 
9 /* -- Registers IDs ------------------------------------------------------- */
10 
11 #define GPRDEF(_) \
12   _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \
13   _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \
14   _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \
15   _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31)
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 #define VRIDDEF(_)
22 
23 #define RIDENUM(name)	RID_##name,
24 
25 enum {
26   GPRDEF(RIDENUM)		/* General-purpose registers (GPRs). */
27   FPRDEF(RIDENUM)		/* Floating-point registers (FPRs). */
28   RID_MAX,
29   RID_TMP = RID_R0,
30 
31   /* Calling conventions. */
32   RID_RET = RID_R3,
33 #if LJ_LE
34   RID_RETHI = RID_R4,
35   RID_RETLO = RID_R3,
36 #else
37   RID_RETHI = RID_R3,
38   RID_RETLO = RID_R4,
39 #endif
40   RID_FPRET = RID_F1,
41 
42   /* These definitions must match with the *.dasc file(s): */
43   RID_BASE = RID_R14,		/* Interpreter BASE. */
44   RID_LPC = RID_R16,		/* Interpreter PC. */
45   RID_DISPATCH = RID_R17,	/* Interpreter DISPATCH table. */
46   RID_LREG = RID_R18,		/* Interpreter L. */
47   RID_JGL = RID_R31,		/* On-trace: global_State + 32768. */
48 
49   /* Register ranges [min, max) and number of registers. */
50   RID_MIN_GPR = RID_R0,
51   RID_MAX_GPR = RID_R31+1,
52   RID_MIN_FPR = RID_F0,
53   RID_MAX_FPR = RID_F31+1,
54   RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,
55   RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR
56 };
57 
58 #define RID_NUM_KREF		RID_NUM_GPR
59 #define RID_MIN_KREF		RID_R0
60 
61 /* -- Register sets ------------------------------------------------------- */
62 
63 /* Make use of all registers, except TMP, SP, SYS1, SYS2 and JGL. */
64 #define RSET_FIXED \
65   (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\
66    RID2RSET(RID_SYS2)|RID2RSET(RID_JGL))
67 #define RSET_GPR	(RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED)
68 #define RSET_FPR	RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)
69 #define RSET_ALL	(RSET_GPR|RSET_FPR)
70 #define RSET_INIT	RSET_ALL
71 
72 #define RSET_SCRATCH_GPR	(RSET_RANGE(RID_R3, RID_R12+1))
73 #define RSET_SCRATCH_FPR	(RSET_RANGE(RID_F0, RID_F13+1))
74 #define RSET_SCRATCH		(RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)
75 #define REGARG_FIRSTGPR		RID_R3
76 #define REGARG_LASTGPR		RID_R10
77 #define REGARG_NUMGPR		8
78 #define REGARG_FIRSTFPR		RID_F1
79 #define REGARG_LASTFPR		RID_F8
80 #define REGARG_NUMFPR		8
81 
82 /* -- Spill slots --------------------------------------------------------- */
83 
84 /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.
85 **
86 ** SPS_FIXED: Available fixed spill slots in interpreter frame.
87 ** This definition must match with the *.dasc file(s).
88 **
89 ** SPS_FIRST: First spill slot for general use.
90 ** [sp+12] tmplo word \
91 ** [sp+ 8] tmphi word / tmp dword, parameter area for callee
92 ** [sp+ 4] tmpw, LR of callee
93 ** [sp+ 0] stack chain
94 */
95 #define SPS_FIXED	7
96 #define SPS_FIRST	4
97 
98 /* Stack offsets for temporary slots. Used for FP<->int conversions etc. */
99 #define SPOFS_TMPW	4
100 #define SPOFS_TMP	8
101 #define SPOFS_TMPHI	8
102 #define SPOFS_TMPLO	12
103 
104 #define sps_scale(slot)		(4 * (int32_t)(slot))
105 #define sps_align(slot)		(((slot) - SPS_FIXED + 3) & ~3)
106 
107 /* -- Exit state ---------------------------------------------------------- */
108 
109 /* This definition must match with the *.dasc file(s). */
110 typedef struct {
111   lua_Number fpr[RID_NUM_FPR];	/* Floating-point registers. */
112   intptr_t gpr[RID_NUM_GPR];	/* General-purpose registers. */
113   int32_t spill[256];		/* Spill slots. */
114 } ExitState;
115 
116 /* Highest exit + 1 indicates stack check. */
117 #define EXITSTATE_CHECKEXIT	1
118 
119 /* Return the address of a per-trace exit stub. */
exitstub_trace_addr_(uint32_t * p,uint32_t exitno)120 static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno)
121 {
122   while (*p == 0x60000000) p++;  /* Skip PPCI_NOP. */
123   return p + 3 + exitno;
124 }
125 /* Avoid dependence on lj_jit.h if only including lj_target.h. */
126 #define exitstub_trace_addr(T, exitno) \
127   exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno))
128 
129 /* -- Instructions -------------------------------------------------------- */
130 
131 /* Instruction fields. */
132 #define PPCF_CC(cc)	((((cc) & 3) << 16) | (((cc) & 4) << 22))
133 #define PPCF_T(r)	((r) << 21)
134 #define PPCF_A(r)	((r) << 16)
135 #define PPCF_B(r)	((r) << 11)
136 #define PPCF_C(r)	((r) << 6)
137 #define PPCF_MB(n)	((n) << 6)
138 #define PPCF_ME(n)	((n) << 1)
139 #define PPCF_SH(n)	((((n) & 31) << (11+1)) | (((n) & 32) >> (5-1)))
140 #define PPCF_M6(n)	((((n) & 31) << (5+1)) | (((n) & 32) << (11-5)))
141 #define PPCF_Y		0x00200000
142 #define PPCF_DOT	0x00000001
143 
144 typedef enum PPCIns {
145   /* Integer instructions. */
146   PPCI_MR = 0x7c000378,
147   PPCI_NOP = 0x60000000,
148 
149   PPCI_LI = 0x38000000,
150   PPCI_LIS = 0x3c000000,
151 
152   PPCI_ADD = 0x7c000214,
153   PPCI_ADDC = 0x7c000014,
154   PPCI_ADDO = 0x7c000614,
155   PPCI_ADDE = 0x7c000114,
156   PPCI_ADDZE = 0x7c000194,
157   PPCI_ADDME = 0x7c0001d4,
158   PPCI_ADDI = 0x38000000,
159   PPCI_ADDIS = 0x3c000000,
160   PPCI_ADDIC = 0x30000000,
161   PPCI_ADDICDOT = 0x34000000,
162 
163   PPCI_SUBF = 0x7c000050,
164   PPCI_SUBFC = 0x7c000010,
165   PPCI_SUBFO = 0x7c000450,
166   PPCI_SUBFE = 0x7c000110,
167   PPCI_SUBFZE = 0x7c000190,
168   PPCI_SUBFME = 0x7c0001d0,
169   PPCI_SUBFIC = 0x20000000,
170 
171   PPCI_NEG = 0x7c0000d0,
172 
173   PPCI_AND = 0x7c000038,
174   PPCI_ANDC = 0x7c000078,
175   PPCI_NAND = 0x7c0003b8,
176   PPCI_ANDIDOT = 0x70000000,
177   PPCI_ANDISDOT = 0x74000000,
178 
179   PPCI_OR = 0x7c000378,
180   PPCI_NOR = 0x7c0000f8,
181   PPCI_ORI = 0x60000000,
182   PPCI_ORIS = 0x64000000,
183 
184   PPCI_XOR = 0x7c000278,
185   PPCI_EQV = 0x7c000238,
186   PPCI_XORI = 0x68000000,
187   PPCI_XORIS = 0x6c000000,
188 
189   PPCI_CMPW = 0x7c000000,
190   PPCI_CMPLW = 0x7c000040,
191   PPCI_CMPWI = 0x2c000000,
192   PPCI_CMPLWI = 0x28000000,
193 
194   PPCI_MULLW = 0x7c0001d6,
195   PPCI_MULLI = 0x1c000000,
196   PPCI_MULLWO = 0x7c0005d6,
197 
198   PPCI_EXTSB = 0x7c000774,
199   PPCI_EXTSH = 0x7c000734,
200 
201   PPCI_SLW = 0x7c000030,
202   PPCI_SRW = 0x7c000430,
203   PPCI_SRAW = 0x7c000630,
204   PPCI_SRAWI = 0x7c000670,
205 
206   PPCI_RLWNM = 0x5c000000,
207   PPCI_RLWINM = 0x54000000,
208   PPCI_RLWIMI = 0x50000000,
209 
210   PPCI_RLDICL = 0x78000000,
211   PPCI_RLDICR = 0x78000004,
212   PPCI_RLDIC = 0x78000008,
213   PPCI_RLDIMI = 0x7800000c,
214   PPCI_RLDCL = 0x78000010,
215   PPCI_RLDCR = 0x78000012,
216 
217   PPCI_B = 0x48000000,
218   PPCI_BL = 0x48000001,
219   PPCI_BC = 0x40800000,
220   PPCI_BCL = 0x40800001,
221   PPCI_BCTR = 0x4e800420,
222   PPCI_BCTRL = 0x4e800421,
223 
224   PPCI_CRANDC = 0x4c000102,
225   PPCI_CRXOR = 0x4c000182,
226   PPCI_CRAND = 0x4c000202,
227   PPCI_CREQV = 0x4c000242,
228   PPCI_CRORC = 0x4c000342,
229   PPCI_CROR = 0x4c000382,
230 
231   PPCI_MFLR = 0x7c0802a6,
232   PPCI_MTCTR = 0x7c0903a6,
233 
234   PPCI_MCRXR = 0x7c000400,
235 
236   /* Load/store instructions. */
237   PPCI_LWZ = 0x80000000,
238   PPCI_LBZ = 0x88000000,
239   PPCI_STW = 0x90000000,
240   PPCI_STB = 0x98000000,
241   PPCI_LHZ = 0xa0000000,
242   PPCI_LHA = 0xa8000000,
243   PPCI_STH = 0xb0000000,
244 
245   PPCI_STWU = 0x94000000,
246 
247   PPCI_LFS = 0xc0000000,
248   PPCI_LFD = 0xc8000000,
249   PPCI_STFS = 0xd0000000,
250   PPCI_STFD = 0xd8000000,
251 
252   PPCI_LWZX = 0x7c00002e,
253   PPCI_LBZX = 0x7c0000ae,
254   PPCI_STWX = 0x7c00012e,
255   PPCI_STBX = 0x7c0001ae,
256   PPCI_LHZX = 0x7c00022e,
257   PPCI_LHAX = 0x7c0002ae,
258   PPCI_STHX = 0x7c00032e,
259 
260   PPCI_LWBRX = 0x7c00042c,
261   PPCI_STWBRX = 0x7c00052c,
262 
263   PPCI_LFSX = 0x7c00042e,
264   PPCI_LFDX = 0x7c0004ae,
265   PPCI_STFSX = 0x7c00052e,
266   PPCI_STFDX = 0x7c0005ae,
267 
268   /* FP instructions. */
269   PPCI_FMR = 0xfc000090,
270   PPCI_FNEG = 0xfc000050,
271   PPCI_FABS = 0xfc000210,
272 
273   PPCI_FRSP = 0xfc000018,
274   PPCI_FCTIWZ = 0xfc00001e,
275 
276   PPCI_FADD = 0xfc00002a,
277   PPCI_FSUB = 0xfc000028,
278   PPCI_FMUL = 0xfc000032,
279   PPCI_FDIV = 0xfc000024,
280   PPCI_FSQRT = 0xfc00002c,
281 
282   PPCI_FMADD = 0xfc00003a,
283   PPCI_FMSUB = 0xfc000038,
284   PPCI_FNMSUB = 0xfc00003c,
285 
286   PPCI_FCMPU = 0xfc000000,
287   PPCI_FSEL = 0xfc00002e,
288 } PPCIns;
289 
290 typedef enum PPCCC {
291   CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO
292 } PPCCC;
293 
294 #endif
295