xref: /php-src/ext/opcache/jit/ir/dynasm/dasm_x86.h (revision 2ab1c3d5)
1 /*
2 ** DynASM x86 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		"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. */
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   Dst_REF = NULL;
92   DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
93   D = Dst_REF;
94   D->psize = psz;
95   D->lglabels = NULL;
96   D->lgsize = 0;
97   D->pclabels = NULL;
98   D->pcsize = 0;
99   D->globals = NULL;
100   D->maxsection = maxsection;
101   memset((void *)D->sections, 0, maxsection * sizeof(dasm_Section));
102 }
103 
104 /* Free DynASM state. */
dasm_free(Dst_DECL)105 void dasm_free(Dst_DECL)
106 {
107   dasm_State *D = Dst_REF;
108   int i;
109   for (i = 0; i < D->maxsection; i++)
110     if (D->sections[i].buf)
111       DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
112   if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
113   if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
114   DASM_M_FREE(Dst, D, D->psize);
115 }
116 
117 /* Setup global label array. Must be called before dasm_setup(). */
dasm_setupglobal(Dst_DECL,void ** gl,unsigned int maxgl)118 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
119 {
120   dasm_State *D = Dst_REF;
121   D->globals = gl;
122   DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
123 }
124 
125 /* Grow PC label array. Can be called after dasm_setup(), too. */
dasm_growpc(Dst_DECL,unsigned int maxpc)126 void dasm_growpc(Dst_DECL, unsigned int maxpc)
127 {
128   dasm_State *D = Dst_REF;
129   size_t osz = D->pcsize;
130   DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
131   memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
132 }
133 
134 /* Setup encoder. */
dasm_setup(Dst_DECL,const void * actionlist)135 void dasm_setup(Dst_DECL, const void *actionlist)
136 {
137   dasm_State *D = Dst_REF;
138   int i;
139   D->actionlist = (dasm_ActList)actionlist;
140   D->status = DASM_S_OK;
141   D->section = &D->sections[0];
142   memset((void *)D->lglabels, 0, D->lgsize);
143   if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
144   for (i = 0; i < D->maxsection; i++) {
145     D->sections[i].pos = DASM_SEC2POS(i);
146     D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, D->sections[i].pos);
147     D->sections[i].ofs = 0;
148   }
149 }
150 
151 
152 #ifdef DASM_CHECKS
153 #define CK(x, st) \
154   do { if (!(x)) { \
155     D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
156 #define CKPL(kind, st) \
157   do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
158     D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
159 #else
160 #define CK(x, st)	((void)0)
161 #define CKPL(kind, st)	((void)0)
162 #endif
163 
164 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
dasm_put(Dst_DECL,int start,...)165 void dasm_put(Dst_DECL, int start, ...)
166 {
167   va_list ap;
168   dasm_State *D = Dst_REF;
169   dasm_ActList p = D->actionlist + start;
170   dasm_Section *sec = D->section;
171   int pos = sec->pos, ofs = sec->ofs, mrm = -1;
172   int *b;
173 
174   if (pos >= sec->epos) {
175     DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
176       sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
177     sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
178     sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
179   }
180 
181   b = sec->rbuf;
182   b[pos++] = start;
183 
184   va_start(ap, start);
185   while (1) {
186     int action = *p++;
187     while (action < DASM_DISP) {
188       ofs++;
189       action = *p++;
190     }
191     if (action <= DASM_REL_A) {
192       int n = va_arg(ap, int);
193       b[pos++] = n;
194       switch (action) {
195       case DASM_DISP:
196 	if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
197 	/* fallthrough */
198       case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) goto ob; /* fallthrough */
199       case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
200       case DASM_IMM_D: ofs += 4; break;
201       case DASM_IMM_S: CK((((unsigned)n+128)&-256) == 0, RANGE_I); goto ob;
202       case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break;
203       case DASM_IMM_WB: if ((((unsigned)n+128)&-256) == 0) goto ob; /* fallthrough */
204       case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
205       case DASM_SPACE: p++; ofs += n; break;
206       case DASM_SETLABEL: b[pos-2] = -0x40000000; break;  /* Neg. label ofs. */
207       case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
208 	if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
209 	if (*p < 0x20 && (n&7) == 4) ofs++;
210 	switch ((*p++ >> 3) & 3) {
211 	case 3: n |= b[pos-3]; /* fallthrough */
212 	case 2: n |= b[pos-2]; /* fallthrough */
213 	case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
214 	}
215 	continue;
216       }
217       mrm = -1;
218     } else {
219       int *pl, n;
220       switch (action) {
221       case DASM_REL_LG:
222       case DASM_IMM_LG:
223 	n = *p++; pl = D->lglabels + n;
224 	/* Bkwd rel or global. */
225 	if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
226 	pl -= 246; n = *pl;
227 	if (n < 0) n = 0;  /* Start new chain for fwd rel if label exists. */
228 	goto linkrel;
229       case DASM_REL_PC:
230       case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
231       putrel:
232 	n = *pl;
233 	if (n < 0) {  /* Label exists. Get label pos and store it. */
234 	  b[pos] = -n;
235 	} else {
236       linkrel:
237 	  b[pos] = n;  /* Else link to rel chain, anchored at label. */
238 	  *pl = pos;
239 	}
240 	pos++;
241 	ofs += 4;  /* Maximum offset needed. */
242 	if (action == DASM_REL_LG || action == DASM_REL_PC) {
243 	  b[pos++] = ofs;  /* Store pass1 offset estimate. */
244 	} else if (sizeof(ptrdiff_t) == 8) {
245 	  ofs += 4;
246 	}
247 	break;
248       case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel;
249       case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
250       putlabel:
251 	n = *pl;  /* n > 0: Collapse rel chain and replace with label pos. */
252 	while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; }
253 	*pl = -pos;  /* Label exists now. */
254 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
255 	break;
256       case DASM_ALIGN:
257 	ofs += *p++;  /* Maximum alignment needed (arg is 2**n-1). */
258 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
259 	break;
260       case DASM_EXTERN: p += 2; ofs += 4; break;
261       case DASM_ESC: p++; ofs++; break;
262       case DASM_MARK: mrm = p[-2]; break;
263       case DASM_SECTION:
264 	n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n];
265       case DASM_STOP: goto stop;
266       }
267     }
268   }
269 stop:
270   va_end(ap);
271   sec->pos = pos;
272   sec->ofs = ofs;
273 }
274 #undef CK
275 
276 /* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */
dasm_link(Dst_DECL,size_t * szp)277 int dasm_link(Dst_DECL, size_t *szp)
278 {
279   dasm_State *D = Dst_REF;
280   int secnum;
281   int ofs = 0;
282 
283 #ifdef DASM_CHECKS
284   *szp = 0;
285   if (D->status != DASM_S_OK) return D->status;
286   {
287     int pc;
288     for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
289       if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
290   }
291 #endif
292 
293   { /* Handle globals not defined in this translation unit. */
294     int idx;
295     for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
296       int n = D->lglabels[idx];
297       /* Undefined label: Collapse rel chain and replace with marker (< 0). */
298       while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
299     }
300   }
301 
302   /* Combine all code sections. No support for data sections (yet). */
303   for (secnum = 0; secnum < D->maxsection; secnum++) {
304     dasm_Section *sec = D->sections + secnum;
305     int *b = sec->rbuf;
306     int pos = DASM_SEC2POS(secnum);
307     int lastpos = sec->pos;
308 
309     while (pos != lastpos) {
310       dasm_ActList p = D->actionlist + b[pos++];
311       int op = 0;
312       while (1) {
313 	int action = *p++;
314 	while (action < DASM_DISP) {
315 	  op = action;
316 	  action = *p++;
317 	}
318 	switch (action) {
319 	case DASM_REL_LG: p++;
320 	  /* fallthrough */
321 	case DASM_REL_PC: {
322 	  int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
323 	  if (shrink) {  /* Shrinkable branch opcode? */
324 	    int lofs, lpos = b[pos];
325 	    if (lpos < 0) goto noshrink;  /* Ext global? */
326 	    lofs = *DASM_POS2PTR(D, lpos);
327 	    if (lpos > pos) {  /* Fwd label: add cumulative section offsets. */
328 	      int i;
329 	      for (i = secnum; i < DASM_POS2SEC(lpos); i++)
330 		lofs += D->sections[i].ofs;
331 	    } else {
332 	      lofs -= ofs;  /* Bkwd label: unfix offset. */
333 	    }
334 	    lofs -= b[pos+1];  /* Short branch ok? */
335 	    if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink;  /* Yes. */
336 	    else { noshrink: shrink = 0; }  /* No, cannot shrink op. */
337 	  }
338 	  b[pos+1] = shrink;
339 	  pos += 2;
340 	  break;
341 	}
342 	  /* fallthrough */
343 	case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
344 	  /* fallthrough */
345 	case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
346 	case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
347 	case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break;
348 	case DASM_LABEL_LG: p++;
349 	  /* fallthrough */
350 	case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
351 	case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
352 	case DASM_EXTERN: p += 2; break;
353 	case DASM_ESC: op = *p++; break;
354 	case DASM_MARK: break;
355 	case DASM_SECTION: case DASM_STOP: goto stop;
356 	}
357       }
358       stop: (void)0;
359     }
360     ofs += sec->ofs;  /* Next section starts right after current section. */
361   }
362 
363   D->codesize = ofs;  /* Total size of all code sections */
364   *szp = ofs;
365   return DASM_S_OK;
366 }
367 
368 #define dasmb(x)	*cp++ = (unsigned char)(x)
369 #ifndef DASM_ALIGNED_WRITES
370 typedef IR_SET_ALIGNED(1, unsigned short unaligned_short);
371 typedef IR_SET_ALIGNED(1, unsigned int unaligned_int);
372 typedef IR_SET_ALIGNED(1, unsigned long long unaligned_long_long);
373 #define dasmw(x) \
374   do { *((unaligned_short *)cp) = (unsigned short)(x); cp+=2; } while (0)
375 #define dasmd(x) \
376   do { *((unaligned_int *)cp) = (unsigned int)(x); cp+=4; } while (0)
377 #define dasmq(x) \
378   do { *((unaligned_long_long *)cp) = (unsigned long long)(x); cp+=8; } while (0)
379 #else
380 #define dasmw(x)	do { dasmb(x); dasmb((x)>>8); } while (0)
381 #define dasmd(x)	do { dasmw(x); dasmw((x)>>16); } while (0)
382 #define dasmq(x)	do { dasmd(x); dasmd((x)>>32); } while (0)
383 #endif
dasma_(unsigned char * cp,ptrdiff_t x)384 static unsigned char *dasma_(unsigned char *cp, ptrdiff_t x)
385 {
386   if (sizeof(ptrdiff_t) == 8)
387     dasmq((unsigned long long)x);
388   else
389     dasmd((unsigned int)x);
390   return cp;
391 }
392 #define dasma(x)	(cp = dasma_(cp, (x)))
393 
394 /* Pass 3: Encode sections. */
dasm_encode(Dst_DECL,void * buffer)395 int dasm_encode(Dst_DECL, void *buffer)
396 {
397   dasm_State *D = Dst_REF;
398   unsigned char *base = (unsigned char *)buffer;
399   unsigned char *cp = base;
400   int secnum;
401 
402   /* Encode all code sections. No support for data sections (yet). */
403   for (secnum = 0; secnum < D->maxsection; secnum++) {
404     dasm_Section *sec = D->sections + secnum;
405     int *b = sec->buf;
406     int *endb = DASM_PTR_ADD(sec->rbuf, sec->pos);
407 
408     while (b != endb) {
409       dasm_ActList p = D->actionlist + *b++;
410       unsigned char *mark = NULL;
411       while (1) {
412 	int n;
413 	int action = *p++;
414 	while (action < DASM_DISP) {
415 	  *cp++ = action;
416 	  action = *p++;
417 	}
418 	if (action >= DASM_ALIGN) {
419 	  switch (action) {
420 	  case DASM_ALIGN:
421 	    b++;
422 	    n = *p++;
423 	    while (((cp-base) & n)) *cp++ = 0x90; /* nop */
424 	    continue;
425 	  case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd;
426 	  case DASM_MARK: mark = cp; continue;
427 	  case DASM_ESC: action = *p++; *cp++ = action; continue;
428 	  case DASM_SECTION: case DASM_STOP: goto stop;
429 	  }
430 	}
431 	n = *b++;
432 	switch (action) {
433 	case DASM_DISP: if (!mark) mark = cp; {
434 	  unsigned char *mm = mark;
435 	  if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL;
436 	  if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7;
437 	    if (mrm != 5) { mm[-1] -= 0x80; break; } }
438 	  if ((((unsigned)n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40;
439 	}
440 	  /* fallthrough */
441 	case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break;
442 	case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) {
443 	    db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb;
444 	  } else mark = NULL;
445 	  /* fallthrough */
446 	case DASM_IMM_D: wd: dasmd(n); break;
447 	case DASM_IMM_WB: if ((((unsigned)n+128)&-256) == 0) goto db; else mark = NULL;
448 	  /* fallthrough */
449 	case DASM_IMM_W: dasmw(n); break;
450 	case DASM_VREG: {
451 	  int t = *p++;
452 	  unsigned char *ex = cp - (t&7);
453 	  if ((n & 8) && t < 0xa0) {
454 	    if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
455 	    n &= 7;
456 	  } else if (n & 0x10) {
457 	    if (*ex & 0x80) {
458 	      *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
459 	    }
460 	    while (++ex < cp) ex[-1] = *ex;
461 	    if (mark) mark--;
462 	    cp--;
463 	    n &= 7;
464 	  }
465 	  if (t >= 0xc0) n <<= 4;
466 	  else if (t >= 0x40) n <<= 3;
467 	  else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
468 	  cp[-1] ^= n;
469 	  break;
470 	}
471 	case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
472 	  b++; n = (int)(ptrdiff_t)D->globals[-n-10];
473 	  /* fallthrough */
474 	case DASM_REL_A: rel_a:
475 	  n -= (unsigned int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */
476 	case DASM_REL_PC: rel_pc: {
477 	  int shrink = *b++;
478 	  int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; }
479 	  n = *pb - ((int)(cp-base) + 4-shrink);
480 	  if (shrink == 0) goto wd;
481 	  if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb;
482 	  goto wb;
483 	}
484 	case DASM_IMM_LG:
485 	  p++;
486 	  if (n < 0) { dasma((ptrdiff_t)D->globals[-n-10]); break; }
487 	  /* fallthrough */
488 	case DASM_IMM_PC: {
489 	  int *pb = DASM_POS2PTR(D, n);
490 	  dasma(*pb < 0 ? (ptrdiff_t)pb[1] : (*pb + (ptrdiff_t)base));
491 	  break;
492 	}
493 	case DASM_LABEL_LG: {
494 	  int idx = *p++;
495 	  if (idx >= 10)
496 	    D->globals[idx-10] = (void *)(base + (*p == DASM_SETLABEL ? *b : n));
497 	  break;
498 	}
499 	case DASM_LABEL_PC: case DASM_SETLABEL: break;
500 	case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; }
501 	}
502       }
503       stop: (void)0;
504     }
505   }
506 
507   if (base + D->codesize != cp)  /* Check for phase errors. */
508     return DASM_S_PHASE;
509   return DASM_S_OK;
510 }
511 
512 /* Get PC label offset. */
dasm_getpclabel(Dst_DECL,unsigned int pc)513 int dasm_getpclabel(Dst_DECL, unsigned int pc)
514 {
515   dasm_State *D = Dst_REF;
516   if (pc*sizeof(int) < D->pcsize) {
517     int pos = D->pclabels[pc];
518     if (pos < 0) return *DASM_POS2PTR(D, -pos);
519     if (pos > 0) return -1;  /* Undefined. */
520   }
521   return -2;  /* Unused or out of range. */
522 }
523 
524 #ifdef DASM_CHECKS
525 /* Optional sanity checker to call between isolated encoding steps. */
dasm_checkstep(Dst_DECL,int secmatch)526 int dasm_checkstep(Dst_DECL, int secmatch)
527 {
528   dasm_State *D = Dst_REF;
529   if (D->status == DASM_S_OK) {
530     int i;
531     for (i = 1; i <= 9; i++) {
532       if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; }
533       D->lglabels[i] = 0;
534     }
535   }
536   if (D->status == DASM_S_OK && secmatch >= 0 &&
537       D->section != &D->sections[secmatch])
538     D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
539   return D->status;
540 }
541 #endif
542 
543