xref: /PHP-8.2/ext/opcache/jit/dynasm/dasm_x86.h (revision 28f6a2ba)
1 /*
2 ** DynASM x86 encoding engine.
3 ** Copyright (C) 2005-2021 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		"x86"
13 
14 #ifndef DASM_EXTERN
15 #define DASM_EXTERN(a,b,c,d)	0
16 #endif
17 
18 /* Action definitions. DASM_STOP must be 255. */
19 enum {
20   DASM_DISP = 233,
21   DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB,
22   DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC,
23   DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN,
24   DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP
25 };
26 
27 /* Maximum number of section buffer positions for a single dasm_put() call. */
28 #define DASM_MAXSECPOS		25
29 
30 /* DynASM encoder status codes. Action list offset or number are or'ed in. */
31 #define DASM_S_OK		0x00000000
32 #define DASM_S_NOMEM		0x01000000
33 #define DASM_S_PHASE		0x02000000
34 #define DASM_S_MATCH_SEC	0x03000000
35 #define DASM_S_RANGE_I		0x11000000
36 #define DASM_S_RANGE_SEC	0x12000000
37 #define DASM_S_RANGE_LG		0x13000000
38 #define DASM_S_RANGE_PC		0x14000000
39 #define DASM_S_RANGE_VREG	0x15000000
40 #define DASM_S_UNDEF_L		0x21000000
41 #define DASM_S_UNDEF_PC		0x22000000
42 
43 /* Macros to convert positions (8 bit section + 24 bit index). */
44 #define DASM_POS2IDX(pos)	((pos)&0x00ffffff)
45 #define DASM_POS2BIAS(pos)	((pos)&0xff000000)
46 #define DASM_SEC2POS(sec)	((sec)<<24)
47 #define DASM_POS2SEC(pos)	((pos)>>24)
48 #define DASM_POS2PTR(D, pos)	(D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
49 
50 /* Action list type. */
51 typedef const unsigned char *dasm_ActList;
52 
53 /* Per-section structure. */
54 typedef struct dasm_Section {
55   int *rbuf;		/* Biased buffer pointer (negative section bias). */
56   int *buf;		/* True buffer pointer. */
57   size_t bsize;		/* Buffer size in bytes. */
58   int pos;		/* Biased buffer position. */
59   int epos;		/* End of biased buffer position - max single put. */
60   int ofs;		/* Byte offset into section. */
61 } dasm_Section;
62 
63 /* Core structure holding the DynASM encoding state. */
64 struct dasm_State {
65   size_t psize;			/* Allocated size of this structure. */
66   dasm_ActList actionlist;	/* Current actionlist pointer. */
67   int *lglabels;		/* Local/global chain/pos ptrs. */
68   size_t lgsize;
69   int *pclabels;		/* PC label chains/pos ptrs. */
70   size_t pcsize;
71   void **globals;		/* Array of globals (bias -10). */
72   dasm_Section *section;	/* Pointer to active section. */
73   size_t codesize;		/* Total size of all code sections. */
74   int maxsection;		/* 0 <= sectionidx < maxsection. */
75   int status;			/* Status code. */
76   dasm_Section sections[1];	/* All sections. Alloc-extended. */
77 };
78 
79 /* The size of the core structure depends on the max. number of sections. */
80 #define DASM_PSZ(ms)	(sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
81 
82 /* Perform potentially overflowing pointer operations in a way that avoids UB. */
83 #define DASM_PTR_SUB(p1, off) ((void *) ((uintptr_t) (p1) - sizeof(*p1) * (uintptr_t) (off)))
84 #define DASM_PTR_ADD(p1, off) ((void *) ((uintptr_t) (p1) + sizeof(*p1) * (uintptr_t) (off)))
85 
86 /* Initialize DynASM state. */
dasm_init(Dst_DECL,int maxsection)87 void dasm_init(Dst_DECL, int maxsection)
88 {
89   dasm_State *D;
90   size_t psz = 0;
91   int i;
92   Dst_REF = NULL;
93   DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
94   D = Dst_REF;
95   D->psize = psz;
96   D->lglabels = NULL;
97   D->lgsize = 0;
98   D->pclabels = NULL;
99   D->pcsize = 0;
100   D->globals = NULL;
101   D->maxsection = maxsection;
102   for (i = 0; i < maxsection; i++) {
103     D->sections[i].buf = NULL;  /* Need this for pass3. */
104     D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i));
105     D->sections[i].bsize = 0;
106     D->sections[i].epos = 0;  /* Wrong, but is recalculated after resize. */
107   }
108 }
109 
110 /* Free DynASM state. */
dasm_free(Dst_DECL)111 void dasm_free(Dst_DECL)
112 {
113   dasm_State *D = Dst_REF;
114   int i;
115   for (i = 0; i < D->maxsection; i++)
116     if (D->sections[i].buf)
117       DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
118   if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
119   if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
120   DASM_M_FREE(Dst, D, D->psize);
121 }
122 
123 /* Setup global label array. Must be called before dasm_setup(). */
dasm_setupglobal(Dst_DECL,void ** gl,unsigned int maxgl)124 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
125 {
126   dasm_State *D = Dst_REF;
127   D->globals = gl - 10;  /* Negative bias to compensate for locals. */
128   DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
129 }
130 
131 /* Grow PC label array. Can be called after dasm_setup(), too. */
dasm_growpc(Dst_DECL,unsigned int maxpc)132 void dasm_growpc(Dst_DECL, unsigned int maxpc)
133 {
134   dasm_State *D = Dst_REF;
135   size_t osz = D->pcsize;
136   DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
137   memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
138 }
139 
140 /* Setup encoder. */
dasm_setup(Dst_DECL,const void * actionlist)141 void dasm_setup(Dst_DECL, const void *actionlist)
142 {
143   dasm_State *D = Dst_REF;
144   int i;
145   D->actionlist = (dasm_ActList)actionlist;
146   D->status = DASM_S_OK;
147   D->section = &D->sections[0];
148   memset((void *)D->lglabels, 0, D->lgsize);
149   if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
150   for (i = 0; i < D->maxsection; i++) {
151     D->sections[i].pos = DASM_SEC2POS(i);
152     D->sections[i].ofs = 0;
153   }
154 }
155 
156 
157 #ifdef DASM_CHECKS
158 #define CK(x, st) \
159   do { if (!(x)) { \
160     D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
161 #define CKPL(kind, st) \
162   do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
163     D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
164 #else
165 #define CK(x, st)	((void)0)
166 #define CKPL(kind, st)	((void)0)
167 #endif
168 
169 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
dasm_put(Dst_DECL,int start,...)170 void dasm_put(Dst_DECL, int start, ...)
171 {
172   va_list ap;
173   dasm_State *D = Dst_REF;
174   dasm_ActList p = D->actionlist + start;
175   dasm_Section *sec = D->section;
176   int pos = sec->pos, ofs = sec->ofs, mrm = -1;
177   int *b;
178 
179   if (pos >= sec->epos) {
180     DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
181       sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
182     sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
183     sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
184   }
185 
186   b = sec->rbuf;
187   b[pos++] = start;
188 
189   va_start(ap, start);
190   while (1) {
191     int action = *p++;
192     if (action < DASM_DISP) {
193       ofs++;
194     } else if (action <= DASM_REL_A) {
195       int n = va_arg(ap, int);
196       b[pos++] = n;
197       switch (action) {
198       case DASM_DISP:
199 	if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
200 	/* fallthrough */
201       case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) goto ob; /* fallthrough */
202       case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
203       case DASM_IMM_D: ofs += 4; break;
204       case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob;
205       case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break;
206       case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */
207       case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
208       case DASM_SPACE: p++; ofs += n; break;
209       case DASM_SETLABEL: b[pos-2] = -0x40000000; break;  /* Neg. label ofs. */
210       case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
211 	if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
212 	if (*p < 0x20 && (n&7) == 4) ofs++;
213 	switch ((*p++ >> 3) & 3) {
214 	case 3: n |= b[pos-3]; /* fallthrough */
215 	case 2: n |= b[pos-2]; /* fallthrough */
216 	case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
217 	}
218 	continue;
219       }
220       mrm = -1;
221     } else {
222       int *pl, n;
223       switch (action) {
224       case DASM_REL_LG:
225       case DASM_IMM_LG:
226 	n = *p++; pl = D->lglabels + n;
227 	/* Bkwd rel or global. */
228 	if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
229 	pl -= 246; n = *pl;
230 	if (n < 0) n = 0;  /* Start new chain for fwd rel if label exists. */
231 	goto linkrel;
232       case DASM_REL_PC:
233       case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
234       putrel:
235 	n = *pl;
236 	if (n < 0) {  /* Label exists. Get label pos and store it. */
237 	  b[pos] = -n;
238 	} else {
239       linkrel:
240 	  b[pos] = n;  /* Else link to rel chain, anchored at label. */
241 	  *pl = pos;
242 	}
243 	pos++;
244 	ofs += 4;  /* Maximum offset needed. */
245 	if (action == DASM_REL_LG || action == DASM_REL_PC) {
246 	  b[pos++] = ofs;  /* Store pass1 offset estimate. */
247 	} else if (sizeof(ptrdiff_t) == 8) {
248 	  ofs += 4;
249 	}
250 	break;
251       case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel;
252       case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
253       putlabel:
254 	n = *pl;  /* n > 0: Collapse rel chain and replace with label pos. */
255 	while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; }
256 	*pl = -pos;  /* Label exists now. */
257 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
258 	break;
259       case DASM_ALIGN:
260 	ofs += *p++;  /* Maximum alignment needed (arg is 2**n-1). */
261 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
262 	break;
263       case DASM_EXTERN: p += 2; ofs += 4; break;
264       case DASM_ESC: p++; ofs++; break;
265       case DASM_MARK: mrm = p[-2]; break;
266       case DASM_SECTION:
267 	n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n];
268       case DASM_STOP: goto stop;
269       }
270     }
271   }
272 stop:
273   va_end(ap);
274   sec->pos = pos;
275   sec->ofs = ofs;
276 }
277 #undef CK
278 
279 /* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */
dasm_link(Dst_DECL,size_t * szp)280 int dasm_link(Dst_DECL, size_t *szp)
281 {
282   dasm_State *D = Dst_REF;
283   int secnum;
284   int ofs = 0;
285 
286 #ifdef DASM_CHECKS
287   *szp = 0;
288   if (D->status != DASM_S_OK) return D->status;
289   {
290     int pc;
291     for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
292       if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
293   }
294 #endif
295 
296   { /* Handle globals not defined in this translation unit. */
297     int idx;
298     for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
299       int n = D->lglabels[idx];
300       /* Undefined label: Collapse rel chain and replace with marker (< 0). */
301       while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
302     }
303   }
304 
305   /* Combine all code sections. No support for data sections (yet). */
306   for (secnum = 0; secnum < D->maxsection; secnum++) {
307     dasm_Section *sec = D->sections + secnum;
308     int *b = sec->rbuf;
309     int pos = DASM_SEC2POS(secnum);
310     int lastpos = sec->pos;
311 
312     while (pos != lastpos) {
313       dasm_ActList p = D->actionlist + b[pos++];
314       int op = 0;
315       while (1) {
316 	int action = *p++;
317 	switch (action) {
318 	case DASM_REL_LG: p++;
319 	  /* fallthrough */
320 	case DASM_REL_PC: {
321 	  int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
322 	  if (shrink) {  /* Shrinkable branch opcode? */
323 	    int lofs, lpos = b[pos];
324 	    if (lpos < 0) goto noshrink;  /* Ext global? */
325 	    lofs = *DASM_POS2PTR(D, lpos);
326 	    if (lpos > pos) {  /* Fwd label: add cumulative section offsets. */
327 	      int i;
328 	      for (i = secnum; i < DASM_POS2SEC(lpos); i++)
329 		lofs += D->sections[i].ofs;
330 	    } else {
331 	      lofs -= ofs;  /* Bkwd label: unfix offset. */
332 	    }
333 	    lofs -= b[pos+1];  /* Short branch ok? */
334 	    if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink;  /* Yes. */
335 	    else { noshrink: shrink = 0; }  /* No, cannot shrink op. */
336 	  }
337 	  b[pos+1] = shrink;
338 	  pos += 2;
339 	  break;
340 	}
341 	  /* fallthrough */
342 	case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
343 	case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
344 	case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
345 	case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break;
346 	case DASM_LABEL_LG: p++;
347 	  /* fallthrough */
348 	case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
349 	case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
350 	case DASM_EXTERN: p += 2; break;
351 	case DASM_ESC: op = *p++; break;
352 	case DASM_MARK: break;
353 	case DASM_SECTION: case DASM_STOP: goto stop;
354 	default: op = action; break;
355 	}
356       }
357       stop: (void)0;
358     }
359     ofs += sec->ofs;  /* Next section starts right after current section. */
360   }
361 
362   D->codesize = ofs;  /* Total size of all code sections */
363   *szp = ofs;
364   return DASM_S_OK;
365 }
366 
367 #define dasmb(x)	*cp++ = (unsigned char)(x)
368 #ifndef DASM_ALIGNED_WRITES
369 typedef ZEND_SET_ALIGNED(1, unsigned short unaligned_short);
370 typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_int);
371 typedef ZEND_SET_ALIGNED(1, unsigned long long unaligned_long_long);
372 #define dasmw(x) \
373   do { *((unaligned_short *)cp) = (unsigned short)(x); cp+=2; } while (0)
374 #define dasmd(x) \
375   do { *((unaligned_int *)cp) = (unsigned int)(x); cp+=4; } while (0)
376 #define dasmq(x) \
377   do { *((unaligned_long_long *)cp) = (unsigned long long)(x); cp+=8; } while (0)
378 #else
379 #define dasmw(x)	do { dasmb(x); dasmb((x)>>8); } while (0)
380 #define dasmd(x)	do { dasmw(x); dasmw((x)>>16); } while (0)
381 #define dasmq(x)	do { dasmd(x); dasmd((x)>>32); } while (0)
382 #endif
dasma_(unsigned char * cp,ptrdiff_t x)383 static unsigned char *dasma_(unsigned char *cp, ptrdiff_t x)
384 {
385   if (sizeof(ptrdiff_t) == 8)
386     dasmq((unsigned long long)x);
387   else
388     dasmd((unsigned int)x);
389   return cp;
390 }
391 #define dasma(x)	(cp = dasma_(cp, (x)))
392 
393 /* Pass 3: Encode sections. */
dasm_encode(Dst_DECL,void * buffer)394 int dasm_encode(Dst_DECL, void *buffer)
395 {
396   dasm_State *D = Dst_REF;
397   unsigned char *base = (unsigned char *)buffer;
398   unsigned char *cp = base;
399   int secnum;
400 
401   /* Encode all code sections. No support for data sections (yet). */
402   for (secnum = 0; secnum < D->maxsection; secnum++) {
403     dasm_Section *sec = D->sections + secnum;
404     int *b = sec->buf;
405     int *endb = DASM_PTR_ADD(sec->rbuf, sec->pos);
406 
407     while (b != endb) {
408       dasm_ActList p = D->actionlist + *b++;
409       unsigned char *mark = NULL;
410       while (1) {
411 	int action = *p++;
412 	int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0;
413 	switch (action) {
414 	case DASM_DISP: if (!mark) mark = cp; {
415 	  unsigned char *mm = mark;
416 	  if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL;
417 	  if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7;
418 	    if (mrm != 5) { mm[-1] -= 0x80; break; } }
419 	  if ((((unsigned)n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40;
420 	}
421 	  /* fallthrough */
422 	case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break;
423 	case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) {
424 	    db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb;
425 	  } else mark = NULL;
426 	  /* fallthrough */
427 	case DASM_IMM_D: wd: dasmd(n); break;
428 	case DASM_IMM_WB: if ((((unsigned)n+128)&-256) == 0) goto db; else mark = NULL;
429 	  /* fallthrough */
430 	case DASM_IMM_W: dasmw(n); break;
431 	case DASM_VREG: {
432 	  int t = *p++;
433 	  unsigned char *ex = cp - (t&7);
434 	  if ((n & 8) && t < 0xa0) {
435 	    if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
436 	    n &= 7;
437 	  } else if (n & 0x10) {
438 	    if (*ex & 0x80) {
439 	      *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
440 	    }
441 	    while (++ex < cp) ex[-1] = *ex;
442 	    if (mark) mark--;
443 	    cp--;
444 	    n &= 7;
445 	  }
446 	  if (t >= 0xc0) n <<= 4;
447 	  else if (t >= 0x40) n <<= 3;
448 	  else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
449 	  cp[-1] ^= n;
450 	  break;
451 	}
452 	case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
453 	  b++; n = (int)(ptrdiff_t)D->globals[-n];
454 	  /* fallthrough */
455 	case DASM_REL_A: rel_a:
456 	  n -= (unsigned int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */
457 	case DASM_REL_PC: rel_pc: {
458 	  int shrink = *b++;
459 	  int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; }
460 	  n = *pb - ((int)(cp-base) + 4-shrink);
461 	  if (shrink == 0) goto wd;
462 	  if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb;
463 	  goto wb;
464 	}
465 	case DASM_IMM_LG:
466 	  p++;
467 	  if (n < 0) { dasma((ptrdiff_t)D->globals[-n]); break; }
468 	  /* fallthrough */
469 	case DASM_IMM_PC: {
470 	  int *pb = DASM_POS2PTR(D, n);
471 	  dasma(*pb < 0 ? (ptrdiff_t)pb[1] : (*pb + (ptrdiff_t)base));
472 	  break;
473 	}
474 	case DASM_LABEL_LG: {
475 	  int idx = *p++;
476 	  if (idx >= 10)
477 	    D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n));
478 	  break;
479 	}
480 	case DASM_LABEL_PC: case DASM_SETLABEL: break;
481 	case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; }
482 	case DASM_ALIGN:
483 	  n = *p++;
484 	  while (((cp-base) & n)) *cp++ = 0x90; /* nop */
485 	  break;
486 	case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd;
487 	case DASM_MARK: mark = cp; break;
488 	case DASM_ESC: action = *p++;
489 	  /* fallthrough */
490 	default: *cp++ = action; break;
491 	case DASM_SECTION: case DASM_STOP: goto stop;
492 	}
493       }
494       stop: (void)0;
495     }
496   }
497 
498   if (base + D->codesize != cp)  /* Check for phase errors. */
499     return DASM_S_PHASE;
500   return DASM_S_OK;
501 }
502 
503 /* Get PC label offset. */
dasm_getpclabel(Dst_DECL,unsigned int pc)504 int dasm_getpclabel(Dst_DECL, unsigned int pc)
505 {
506   dasm_State *D = Dst_REF;
507   if (pc*sizeof(int) < D->pcsize) {
508     int pos = D->pclabels[pc];
509     if (pos < 0) return *DASM_POS2PTR(D, -pos);
510     if (pos > 0) return -1;  /* Undefined. */
511   }
512   return -2;  /* Unused or out of range. */
513 }
514 
515 #ifdef DASM_CHECKS
516 /* Optional sanity checker to call between isolated encoding steps. */
dasm_checkstep(Dst_DECL,int secmatch)517 int dasm_checkstep(Dst_DECL, int secmatch)
518 {
519   dasm_State *D = Dst_REF;
520   if (D->status == DASM_S_OK) {
521     int i;
522     for (i = 1; i <= 9; i++) {
523       if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; }
524       D->lglabels[i] = 0;
525     }
526   }
527   if (D->status == DASM_S_OK && secmatch >= 0 &&
528       D->section != &D->sections[secmatch])
529     D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
530   return D->status;
531 }
532 #endif
533 
534