php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
dasm_mips.h
Go to the documentation of this file.
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. */
19enum {
21 /* The following actions need a buffer position. */
23 /* The following actions also have an argument. */
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. */
52typedef const unsigned int *dasm_ActList;
53
54/* Per-section structure. */
55typedef 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. */
63
64/* Core structure holding the DynASM encoding state. */
65struct 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. */
85void dasm_init(Dst_DECL, int maxsection)
86{
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. */
104{
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(). */
116void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
117{
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. */
124void dasm_growpc(Dst_DECL, unsigned int maxpc)
125{
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. */
133void dasm_setup(Dst_DECL, const void *actionlist)
134{
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. */
163void dasm_put(Dst_DECL, int start, ...)
164{
165 va_list ap;
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 }
245stop:
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. */
253int dasm_link(Dst_DECL, size_t *szp)
254{
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. */
319{
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. */
390int dasm_getpclabel(Dst_DECL, unsigned int pc)
391{
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. */
403int dasm_checkstep(Dst_DECL, int secmatch)
404{
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
zend_long ptrdiff_t
#define DASM_MAXSECPOS
Definition dasm_arm.h:30
#define DASM_S_MATCH_SEC
Definition dasm_arm.h:36
const unsigned int * dasm_ActList
Definition dasm_arm.h:53
#define DASM_S_UNDEF_PC
Definition dasm_arm.h:43
@ DASM_LABEL_PC
Definition dasm_arm.h:24
@ DASM_REL_EXT
Definition dasm_arm.h:20
@ DASM_LABEL_LG
Definition dasm_arm.h:22
@ DASM_IMM
Definition dasm_arm.h:25
@ DASM__MAX
Definition dasm_arm.h:26
@ DASM_SECTION
Definition dasm_arm.h:20
@ DASM_ALIGN
Definition dasm_arm.h:22
@ DASM_REL_PC
Definition dasm_arm.h:24
@ DASM_REL_LG
Definition dasm_arm.h:22
@ DASM_STOP
Definition dasm_arm.h:20
@ DASM_ESC
Definition dasm_arm.h:20
#define DASM_S_OK
Definition dasm_arm.h:33
#define DASM_S_UNDEF_LG
Definition dasm_arm.h:42
#define DASM_S_PHASE
Definition dasm_arm.h:35
void dasm_put(Dst_DECL, int start,...)
Definition dasm_mips.h:163
void dasm_growpc(Dst_DECL, unsigned int maxpc)
Definition dasm_mips.h:124
int dasm_encode(Dst_DECL, void *buffer)
Definition dasm_mips.h:318
void dasm_free(Dst_DECL)
Definition dasm_mips.h:103
void dasm_init(Dst_DECL, int maxsection)
Definition dasm_mips.h:85
#define DASM_POS2PTR(D, pos)
Definition dasm_mips.h:49
#define DASM_POS2BIAS(pos)
Definition dasm_mips.h:46
@ DASM_IMMS
Definition dasm_mips.h:24
#define CKPL(kind, st)
Definition dasm_mips.h:159
#define DASM_EXTERN(a, b, c, d)
Definition dasm_mips.h:15
#define DASM_PSZ(ms)
Definition dasm_mips.h:81
void dasm_setup(Dst_DECL, const void *actionlist)
Definition dasm_mips.h:133
int dasm_getpclabel(Dst_DECL, unsigned int pc)
Definition dasm_mips.h:390
#define CK(x, st)
Definition dasm_mips.h:158
int dasm_link(Dst_DECL, size_t *szp)
Definition dasm_mips.h:253
#define DASM_SEC2POS(sec)
Definition dasm_mips.h:47
void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
Definition dasm_mips.h:116
#define dasm_checkstep(a, b)
Definition dasm_proto.h:79
#define DASM_M_FREE(ctx, p, sz)
Definition dasm_proto.h:43
#define Dst_REF
Definition dasm_proto.h:21
#define Dst_DECL
Definition dasm_proto.h:17
#define DASM_M_GROW(ctx, t, p, sz, need)
Definition dasm_proto.h:29
struct dasm_Section dasm_Section
const unsigned char * dasm_ActList
Definition dasm_x86.h:51
zend_long n
Definition ffi.c:4979
memset(ptr, 0, type->size)
buf start
Definition ffi.c:4687
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define NULL
Definition gdcache.h:45
unsigned const char * pos
Definition php_ffi.h:52
p
Definition session.c:1105
#define D(d)
Definition minilua.c:2260
Definition file.h:177
int * buf
Definition dasm_arm.h:58
int * rbuf
Definition dasm_arm.h:57
size_t bsize
Definition dasm_arm.h:59
size_t pcsize
Definition dasm_arm.h:72
int * pclabels
Definition dasm_arm.h:71
dasm_Section sections[1]
Definition dasm_arm.h:78
size_t codesize
Definition dasm_arm.h:75
dasm_ActList actionlist
Definition dasm_arm.h:68
int status
Definition dasm_arm.h:77
size_t lgsize
Definition dasm_arm.h:70
void ** globals
Definition dasm_arm.h:73
size_t psize
Definition dasm_arm.h:67
int * lglabels
Definition dasm_arm.h:69
dasm_Section * section
Definition dasm_arm.h:74
int maxsection
Definition dasm_arm.h:76
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)