dasm_x86.h 16 KB

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