xref: /php-src/ext/opcache/jit/ir/dynasm/dasm_mips.h (revision 2ab1c3d5)
1 /*
2 ** DynASM MIPS encoding engine.
3 ** Copyright (C) 2005-2023 Mike Pall. All rights reserved.
4 ** Released under the MIT license. See dynasm.lua for full copyright notice.
5 */
6 
7 #include <stddef.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include <stdlib.h>
11 
12 #define DASM_ARCH		"mips"
13 
14 #ifndef DASM_EXTERN
15 #define DASM_EXTERN(a,b,c,d)	0
16 #endif
17 
18 /* Action definitions. */
19 enum {
20   DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT,
21   /* The following actions need a buffer position. */
22   DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
23   /* The following actions also have an argument. */
24   DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, DASM_IMMS,
25   DASM__MAX
26 };
27 
28 /* Maximum number of section buffer positions for a single dasm_put() call. */
29 #define DASM_MAXSECPOS		25
30 
31 /* DynASM encoder status codes. Action list offset or number are or'ed in. */
32 #define DASM_S_OK		0x00000000
33 #define DASM_S_NOMEM		0x01000000
34 #define DASM_S_PHASE		0x02000000
35 #define DASM_S_MATCH_SEC	0x03000000
36 #define DASM_S_RANGE_I		0x11000000
37 #define DASM_S_RANGE_SEC	0x12000000
38 #define DASM_S_RANGE_LG		0x13000000
39 #define DASM_S_RANGE_PC		0x14000000
40 #define DASM_S_RANGE_REL	0x15000000
41 #define DASM_S_UNDEF_LG		0x21000000
42 #define DASM_S_UNDEF_PC		0x22000000
43 
44 /* Macros to convert positions (8 bit section + 24 bit index). */
45 #define DASM_POS2IDX(pos)	((pos)&0x00ffffff)
46 #define DASM_POS2BIAS(pos)	((pos)&0xff000000)
47 #define DASM_SEC2POS(sec)	((sec)<<24)
48 #define DASM_POS2SEC(pos)	((pos)>>24)
49 #define DASM_POS2PTR(D, pos)	(D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
50 
51 /* Action list type. */
52 typedef const unsigned int *dasm_ActList;
53 
54 /* Per-section structure. */
55 typedef struct dasm_Section {
56   int *rbuf;		/* Biased buffer pointer (negative section bias). */
57   int *buf;		/* True buffer pointer. */
58   size_t bsize;		/* Buffer size in bytes. */
59   int pos;		/* Biased buffer position. */
60   int epos;		/* End of biased buffer position - max single put. */
61   int ofs;		/* Byte offset into section. */
62 } dasm_Section;
63 
64 /* Core structure holding the DynASM encoding state. */
65 struct dasm_State {
66   size_t psize;			/* Allocated size of this structure. */
67   dasm_ActList actionlist;	/* Current actionlist pointer. */
68   int *lglabels;		/* Local/global chain/pos ptrs. */
69   size_t lgsize;
70   int *pclabels;		/* PC label chains/pos ptrs. */
71   size_t pcsize;
72   void **globals;		/* Array of globals. */
73   dasm_Section *section;	/* Pointer to active section. */
74   size_t codesize;		/* Total size of all code sections. */
75   int maxsection;		/* 0 <= sectionidx < maxsection. */
76   int status;			/* Status code. */
77   dasm_Section sections[1];	/* All sections. Alloc-extended. */
78 };
79 
80 /* The size of the core structure depends on the max. number of sections. */
81 #define DASM_PSZ(ms)	(sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
82 
83 
84 /* Initialize DynASM state. */
dasm_init(Dst_DECL,int maxsection)85 void dasm_init(Dst_DECL, int maxsection)
86 {
87   dasm_State *D;
88   size_t psz = 0;
89   Dst_REF = NULL;
90   DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
91   D = Dst_REF;
92   D->psize = psz;
93   D->lglabels = NULL;
94   D->lgsize = 0;
95   D->pclabels = NULL;
96   D->pcsize = 0;
97   D->globals = NULL;
98   D->maxsection = maxsection;
99   memset((void *)D->sections, 0, maxsection * sizeof(dasm_Section));
100 }
101 
102 /* Free DynASM state. */
dasm_free(Dst_DECL)103 void dasm_free(Dst_DECL)
104 {
105   dasm_State *D = Dst_REF;
106   int i;
107   for (i = 0; i < D->maxsection; i++)
108     if (D->sections[i].buf)
109       DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
110   if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
111   if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
112   DASM_M_FREE(Dst, D, D->psize);
113 }
114 
115 /* Setup global label array. Must be called before dasm_setup(). */
dasm_setupglobal(Dst_DECL,void ** gl,unsigned int maxgl)116 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
117 {
118   dasm_State *D = Dst_REF;
119   D->globals = gl;
120   DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
121 }
122 
123 /* Grow PC label array. Can be called after dasm_setup(), too. */
dasm_growpc(Dst_DECL,unsigned int maxpc)124 void dasm_growpc(Dst_DECL, unsigned int maxpc)
125 {
126   dasm_State *D = Dst_REF;
127   size_t osz = D->pcsize;
128   DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
129   memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
130 }
131 
132 /* Setup encoder. */
dasm_setup(Dst_DECL,const void * actionlist)133 void dasm_setup(Dst_DECL, const void *actionlist)
134 {
135   dasm_State *D = Dst_REF;
136   int i;
137   D->actionlist = (dasm_ActList)actionlist;
138   D->status = DASM_S_OK;
139   D->section = &D->sections[0];
140   memset((void *)D->lglabels, 0, D->lgsize);
141   if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
142   for (i = 0; i < D->maxsection; i++) {
143     D->sections[i].pos = DASM_SEC2POS(i);
144     D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos;
145     D->sections[i].ofs = 0;
146   }
147 }
148 
149 
150 #ifdef DASM_CHECKS
151 #define CK(x, st) \
152   do { if (!(x)) { \
153     D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
154 #define CKPL(kind, st) \
155   do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
156     D->status = DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
157 #else
158 #define CK(x, st)	((void)0)
159 #define CKPL(kind, st)	((void)0)
160 #endif
161 
162 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
dasm_put(Dst_DECL,int start,...)163 void dasm_put(Dst_DECL, int start, ...)
164 {
165   va_list ap;
166   dasm_State *D = Dst_REF;
167   dasm_ActList p = D->actionlist + start;
168   dasm_Section *sec = D->section;
169   int pos = sec->pos, ofs = sec->ofs;
170   int *b;
171 
172   if (pos >= sec->epos) {
173     DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
174       sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
175     sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
176     sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
177   }
178 
179   b = sec->rbuf;
180   b[pos++] = start;
181 
182   va_start(ap, start);
183   while (1) {
184     unsigned int ins = *p++;
185     unsigned int action = (ins >> 16) - 0xff00;
186     if (action >= DASM__MAX) {
187       ofs += 4;
188     } else {
189       int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
190       switch (action) {
191       case DASM_STOP: goto stop;
192       case DASM_SECTION:
193 	n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
194 	D->section = &D->sections[n]; goto stop;
195       case DASM_ESC: p++; ofs += 4; break;
196       case DASM_REL_EXT: break;
197       case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
198       case DASM_REL_LG:
199 	n = (ins & 2047) - 10; pl = D->lglabels + n;
200 	/* Bkwd rel or global. */
201 	if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
202 	pl += 10; n = *pl;
203 	if (n < 0) n = 0;  /* Start new chain for fwd rel if label exists. */
204 	goto linkrel;
205       case DASM_REL_PC:
206 	pl = D->pclabels + n; CKPL(pc, PC);
207       putrel:
208 	n = *pl;
209 	if (n < 0) {  /* Label exists. Get label pos and store it. */
210 	  b[pos] = -n;
211 	} else {
212       linkrel:
213 	  b[pos] = n;  /* Else link to rel chain, anchored at label. */
214 	  *pl = pos;
215 	}
216 	pos++;
217 	break;
218       case DASM_LABEL_LG:
219 	pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
220       case DASM_LABEL_PC:
221 	pl = D->pclabels + n; CKPL(pc, PC);
222       putlabel:
223 	n = *pl;  /* n > 0: Collapse rel chain and replace with label pos. */
224 	while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
225 	}
226 	*pl = -pos;  /* Label exists now. */
227 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
228 	break;
229       case DASM_IMM: case DASM_IMMS:
230 #ifdef DASM_CHECKS
231 	CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
232 #endif
233 	n >>= ((ins>>10)&31);
234 #ifdef DASM_CHECKS
235 	if (ins & 0x8000)
236 	  CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
237 	else
238 	  CK((n>>((ins>>5)&31)) == 0, RANGE_I);
239 #endif
240 	b[pos++] = n;
241 	break;
242       }
243     }
244   }
245 stop:
246   va_end(ap);
247   sec->pos = pos;
248   sec->ofs = ofs;
249 }
250 #undef CK
251 
252 /* Pass 2: Link sections, shrink aligns, fix label offsets. */
dasm_link(Dst_DECL,size_t * szp)253 int dasm_link(Dst_DECL, size_t *szp)
254 {
255   dasm_State *D = Dst_REF;
256   int secnum;
257   int ofs = 0;
258 
259 #ifdef DASM_CHECKS
260   *szp = 0;
261   if (D->status != DASM_S_OK) return D->status;
262   {
263     int pc;
264     for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
265       if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
266   }
267 #endif
268 
269   { /* Handle globals not defined in this translation unit. */
270     int idx;
271     for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
272       int n = D->lglabels[idx];
273       /* Undefined label: Collapse rel chain and replace with marker (< 0). */
274       while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
275     }
276   }
277 
278   /* Combine all code sections. No support for data sections (yet). */
279   for (secnum = 0; secnum < D->maxsection; secnum++) {
280     dasm_Section *sec = D->sections + secnum;
281     int *b = sec->rbuf;
282     int pos = DASM_SEC2POS(secnum);
283     int lastpos = sec->pos;
284 
285     while (pos != lastpos) {
286       dasm_ActList p = D->actionlist + b[pos++];
287       while (1) {
288 	unsigned int ins = *p++;
289 	unsigned int action = (ins >> 16) - 0xff00;
290 	switch (action) {
291 	case DASM_STOP: case DASM_SECTION: goto stop;
292 	case DASM_ESC: p++; break;
293 	case DASM_REL_EXT: break;
294 	case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
295 	case DASM_REL_LG: case DASM_REL_PC: pos++; break;
296 	case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
297 	case DASM_IMM: case DASM_IMMS: pos++; break;
298 	}
299       }
300       stop: (void)0;
301     }
302     ofs += sec->ofs;  /* Next section starts right after current section. */
303   }
304 
305   D->codesize = ofs;  /* Total size of all code sections */
306   *szp = ofs;
307   return DASM_S_OK;
308 }
309 
310 #ifdef DASM_CHECKS
311 #define CK(x, st) \
312   do { if (!(x)) return DASM_S_##st|(int)(p-D->actionlist-1); } while (0)
313 #else
314 #define CK(x, st)	((void)0)
315 #endif
316 
317 /* Pass 3: Encode sections. */
dasm_encode(Dst_DECL,void * buffer)318 int dasm_encode(Dst_DECL, void *buffer)
319 {
320   dasm_State *D = Dst_REF;
321   char *base = (char *)buffer;
322   unsigned int *cp = (unsigned int *)buffer;
323   int secnum;
324 
325   /* Encode all code sections. No support for data sections (yet). */
326   for (secnum = 0; secnum < D->maxsection; secnum++) {
327     dasm_Section *sec = D->sections + secnum;
328     int *b = sec->buf;
329     int *endb = sec->rbuf + sec->pos;
330 
331     while (b != endb) {
332       dasm_ActList p = D->actionlist + *b++;
333       while (1) {
334 	unsigned int ins = *p++;
335 	unsigned int action = (ins >> 16) - 0xff00;
336 	int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
337 	switch (action) {
338 	case DASM_STOP: case DASM_SECTION: goto stop;
339 	case DASM_ESC: *cp++ = *p++; break;
340 	case DASM_REL_EXT:
341 	  n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1);
342 	  goto patchrel;
343 	case DASM_ALIGN:
344 	  ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000;
345 	  break;
346 	case DASM_REL_LG:
347 	  if (n < 0) {
348 	    n = (int)((ptrdiff_t)D->globals[-n-10] - (ptrdiff_t)cp);
349 	    goto patchrel;
350 	  }
351 	  /* fallthrough */
352 	case DASM_REL_PC:
353 	  CK(n >= 0, UNDEF_PC);
354 	  n = *DASM_POS2PTR(D, n);
355 	  if (ins & 2048)
356 	    n = (n + (int)(size_t)base) & 0x0fffffff;
357 	  else
358 	    n = n - (int)((char *)cp - base);
359 	patchrel: {
360 	  unsigned int e = 16 + ((ins >> 12) & 15);
361 	  CK((n & 3) == 0 &&
362 	     ((n + ((ins & 2048) ? 0 : (1<<(e+1)))) >> (e+2)) == 0, RANGE_REL);
363 	  cp[-1] |= ((n>>2) & ((1<<e)-1));
364 	  }
365 	  break;
366 	case DASM_LABEL_LG:
367 	  ins &= 2047; if (ins >= 20) D->globals[ins-20] = (void *)(base + n);
368 	  break;
369 	case DASM_LABEL_PC: break;
370 	case DASM_IMMS:
371 	  cp[-1] |= ((n>>3) & 4); n &= 0x1f;
372 	  /* fallthrough */
373 	case DASM_IMM:
374 	  cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
375 	  break;
376 	default: *cp++ = ins; break;
377 	}
378       }
379       stop: (void)0;
380     }
381   }
382 
383   if (base + D->codesize != (char *)cp)  /* Check for phase errors. */
384     return DASM_S_PHASE;
385   return DASM_S_OK;
386 }
387 #undef CK
388 
389 /* Get PC label offset. */
dasm_getpclabel(Dst_DECL,unsigned int pc)390 int dasm_getpclabel(Dst_DECL, unsigned int pc)
391 {
392   dasm_State *D = Dst_REF;
393   if (pc*sizeof(int) < D->pcsize) {
394     int pos = D->pclabels[pc];
395     if (pos < 0) return *DASM_POS2PTR(D, -pos);
396     if (pos > 0) return -1;  /* Undefined. */
397   }
398   return -2;  /* Unused or out of range. */
399 }
400 
401 #ifdef DASM_CHECKS
402 /* Optional sanity checker to call between isolated encoding steps. */
dasm_checkstep(Dst_DECL,int secmatch)403 int dasm_checkstep(Dst_DECL, int secmatch)
404 {
405   dasm_State *D = Dst_REF;
406   if (D->status == DASM_S_OK) {
407     int i;
408     for (i = 1; i <= 9; i++) {
409       if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
410       D->lglabels[i] = 0;
411     }
412   }
413   if (D->status == DASM_S_OK && secmatch >= 0 &&
414       D->section != &D->sections[secmatch])
415     D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
416   return D->status;
417 }
418 #endif
419 
420