dasm_x86.h 17 KB

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