dasm_arm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ** DynASM ARM 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 "arm"
  11. #ifndef DASM_EXTERN
  12. #define DASM_EXTERN(a,b,c,d) 0
  13. #endif
  14. /* Action definitions. */
  15. enum {
  16. DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT,
  17. /* The following actions need a buffer position. */
  18. DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
  19. /* The following actions also have an argument. */
  20. DASM_REL_PC, DASM_LABEL_PC,
  21. DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, DASM_IMMV8,
  22. DASM__MAX
  23. };
  24. /* Maximum number of section buffer positions for a single dasm_put() call. */
  25. #define DASM_MAXSECPOS 25
  26. /* DynASM encoder status codes. Action list offset or number are or'ed in. */
  27. #define DASM_S_OK 0x00000000
  28. #define DASM_S_NOMEM 0x01000000
  29. #define DASM_S_PHASE 0x02000000
  30. #define DASM_S_MATCH_SEC 0x03000000
  31. #define DASM_S_RANGE_I 0x11000000
  32. #define DASM_S_RANGE_SEC 0x12000000
  33. #define DASM_S_RANGE_LG 0x13000000
  34. #define DASM_S_RANGE_PC 0x14000000
  35. #define DASM_S_RANGE_REL 0x15000000
  36. #define DASM_S_UNDEF_LG 0x21000000
  37. #define DASM_S_UNDEF_PC 0x22000000
  38. /* Macros to convert positions (8 bit section + 24 bit index). */
  39. #define DASM_POS2IDX(pos) ((pos)&0x00ffffff)
  40. #define DASM_POS2BIAS(pos) ((pos)&0xff000000)
  41. #define DASM_SEC2POS(sec) ((sec)<<24)
  42. #define DASM_POS2SEC(pos) ((pos)>>24)
  43. #define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
  44. /* Action list type. */
  45. typedef const unsigned int *dasm_ActList;
  46. /* Per-section structure. */
  47. typedef struct dasm_Section {
  48. int *rbuf; /* Biased buffer pointer (negative section bias). */
  49. int *buf; /* True buffer pointer. */
  50. size_t bsize; /* Buffer size in bytes. */
  51. int pos; /* Biased buffer position. */
  52. int epos; /* End of biased buffer position - max single put. */
  53. int ofs; /* Byte offset into section. */
  54. } dasm_Section;
  55. /* Core structure holding the DynASM encoding state. */
  56. struct dasm_State {
  57. size_t psize; /* Allocated size of this structure. */
  58. dasm_ActList actionlist; /* Current actionlist pointer. */
  59. int *lglabels; /* Local/global chain/pos ptrs. */
  60. size_t lgsize;
  61. int *pclabels; /* PC label chains/pos ptrs. */
  62. size_t pcsize;
  63. void **globals; /* Array of globals. */
  64. dasm_Section *section; /* Pointer to active section. */
  65. size_t codesize; /* Total size of all code sections. */
  66. int maxsection; /* 0 <= sectionidx < maxsection. */
  67. int status; /* Status code. */
  68. dasm_Section sections[1]; /* All sections. Alloc-extended. */
  69. };
  70. /* The size of the core structure depends on the max. number of sections. */
  71. #define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
  72. /* Initialize DynASM state. */
  73. void dasm_init(Dst_DECL, int maxsection)
  74. {
  75. dasm_State *D;
  76. size_t psz = 0;
  77. Dst_REF = NULL;
  78. DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
  79. D = Dst_REF;
  80. D->psize = psz;
  81. D->lglabels = NULL;
  82. D->lgsize = 0;
  83. D->pclabels = NULL;
  84. D->pcsize = 0;
  85. D->globals = NULL;
  86. D->maxsection = maxsection;
  87. memset((void *)D->sections, 0, maxsection * sizeof(dasm_Section));
  88. }
  89. /* Free DynASM state. */
  90. void dasm_free(Dst_DECL)
  91. {
  92. dasm_State *D = Dst_REF;
  93. int i;
  94. for (i = 0; i < D->maxsection; i++)
  95. if (D->sections[i].buf)
  96. DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
  97. if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
  98. if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
  99. DASM_M_FREE(Dst, D, D->psize);
  100. }
  101. /* Setup global label array. Must be called before dasm_setup(). */
  102. void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
  103. {
  104. dasm_State *D = Dst_REF;
  105. D->globals = gl;
  106. DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
  107. }
  108. /* Grow PC label array. Can be called after dasm_setup(), too. */
  109. void dasm_growpc(Dst_DECL, unsigned int maxpc)
  110. {
  111. dasm_State *D = Dst_REF;
  112. size_t osz = D->pcsize;
  113. DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
  114. memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
  115. }
  116. /* Setup encoder. */
  117. void dasm_setup(Dst_DECL, const void *actionlist)
  118. {
  119. dasm_State *D = Dst_REF;
  120. int i;
  121. D->actionlist = (dasm_ActList)actionlist;
  122. D->status = DASM_S_OK;
  123. D->section = &D->sections[0];
  124. memset((void *)D->lglabels, 0, D->lgsize);
  125. if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
  126. for (i = 0; i < D->maxsection; i++) {
  127. D->sections[i].pos = DASM_SEC2POS(i);
  128. D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos;
  129. D->sections[i].ofs = 0;
  130. }
  131. }
  132. #ifdef DASM_CHECKS
  133. #define CK(x, st) \
  134. do { if (!(x)) { \
  135. D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
  136. #define CKPL(kind, st) \
  137. do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
  138. D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
  139. #else
  140. #define CK(x, st) ((void)0)
  141. #define CKPL(kind, st) ((void)0)
  142. #endif
  143. static int dasm_imm12(unsigned int n)
  144. {
  145. int i;
  146. for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30))
  147. if (n <= 255) return (int)(n + (i << 8));
  148. return -1;
  149. }
  150. /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
  151. void dasm_put(Dst_DECL, int start, ...)
  152. {
  153. va_list ap;
  154. dasm_State *D = Dst_REF;
  155. dasm_ActList p = D->actionlist + start;
  156. dasm_Section *sec = D->section;
  157. int pos = sec->pos, ofs = sec->ofs;
  158. int *b;
  159. if (pos >= sec->epos) {
  160. DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
  161. sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
  162. sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
  163. sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
  164. }
  165. b = sec->rbuf;
  166. b[pos++] = start;
  167. va_start(ap, start);
  168. while (1) {
  169. unsigned int ins = *p++;
  170. unsigned int action = (ins >> 16);
  171. if (action >= DASM__MAX) {
  172. ofs += 4;
  173. } else {
  174. int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
  175. switch (action) {
  176. case DASM_STOP: goto stop;
  177. case DASM_SECTION:
  178. n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
  179. D->section = &D->sections[n]; goto stop;
  180. case DASM_ESC: p++; ofs += 4; break;
  181. case DASM_REL_EXT: break;
  182. case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
  183. case DASM_REL_LG:
  184. n = (ins & 2047) - 10; pl = D->lglabels + n;
  185. /* Bkwd rel or global. */
  186. if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
  187. pl += 10; n = *pl;
  188. if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
  189. goto linkrel;
  190. case DASM_REL_PC:
  191. pl = D->pclabels + n; CKPL(pc, PC);
  192. putrel:
  193. n = *pl;
  194. if (n < 0) { /* Label exists. Get label pos and store it. */
  195. b[pos] = -n;
  196. } else {
  197. linkrel:
  198. b[pos] = n; /* Else link to rel chain, anchored at label. */
  199. *pl = pos;
  200. }
  201. pos++;
  202. break;
  203. case DASM_LABEL_LG:
  204. pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
  205. case DASM_LABEL_PC:
  206. pl = D->pclabels + n; CKPL(pc, PC);
  207. putlabel:
  208. n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
  209. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
  210. }
  211. *pl = -pos; /* Label exists now. */
  212. b[pos++] = ofs; /* Store pass1 offset estimate. */
  213. break;
  214. case DASM_IMM:
  215. case DASM_IMM16:
  216. #ifdef DASM_CHECKS
  217. CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
  218. if ((ins & 0x8000))
  219. CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
  220. else
  221. CK((n>>((ins>>5)&31)) == 0, RANGE_I);
  222. #endif
  223. b[pos++] = n;
  224. break;
  225. case DASM_IMMV8:
  226. CK((n & 3) == 0, RANGE_I);
  227. n >>= 2;
  228. /* fallthrough */
  229. case DASM_IMML8:
  230. case DASM_IMML12:
  231. CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) :
  232. (((-n)>>((ins>>5)&31)) == 0), RANGE_I);
  233. b[pos++] = n;
  234. break;
  235. case DASM_IMM12:
  236. CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
  237. b[pos++] = n;
  238. break;
  239. }
  240. }
  241. }
  242. stop:
  243. va_end(ap);
  244. sec->pos = pos;
  245. sec->ofs = ofs;
  246. }
  247. #undef CK
  248. /* Pass 2: Link sections, shrink aligns, fix label offsets. */
  249. int dasm_link(Dst_DECL, size_t *szp)
  250. {
  251. dasm_State *D = Dst_REF;
  252. int secnum;
  253. int ofs = 0;
  254. #ifdef DASM_CHECKS
  255. *szp = 0;
  256. if (D->status != DASM_S_OK) return D->status;
  257. {
  258. int pc;
  259. for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
  260. if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
  261. }
  262. #endif
  263. { /* Handle globals not defined in this translation unit. */
  264. int idx;
  265. for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
  266. int n = D->lglabels[idx];
  267. /* Undefined label: Collapse rel chain and replace with marker (< 0). */
  268. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
  269. }
  270. }
  271. /* Combine all code sections. No support for data sections (yet). */
  272. for (secnum = 0; secnum < D->maxsection; secnum++) {
  273. dasm_Section *sec = D->sections + secnum;
  274. int *b = sec->rbuf;
  275. int pos = DASM_SEC2POS(secnum);
  276. int lastpos = sec->pos;
  277. while (pos != lastpos) {
  278. dasm_ActList p = D->actionlist + b[pos++];
  279. while (1) {
  280. unsigned int ins = *p++;
  281. unsigned int action = (ins >> 16);
  282. switch (action) {
  283. case DASM_STOP: case DASM_SECTION: goto stop;
  284. case DASM_ESC: p++; break;
  285. case DASM_REL_EXT: break;
  286. case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
  287. case DASM_REL_LG: case DASM_REL_PC: pos++; break;
  288. case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
  289. case DASM_IMM: case DASM_IMM12: case DASM_IMM16:
  290. case DASM_IMML8: case DASM_IMML12: case DASM_IMMV8: pos++; break;
  291. }
  292. }
  293. stop: (void)0;
  294. }
  295. ofs += sec->ofs; /* Next section starts right after current section. */
  296. }
  297. D->codesize = ofs; /* Total size of all code sections */
  298. *szp = ofs;
  299. return DASM_S_OK;
  300. }
  301. #ifdef DASM_CHECKS
  302. #define CK(x, st) \
  303. do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
  304. #else
  305. #define CK(x, st) ((void)0)
  306. #endif
  307. /* Pass 3: Encode sections. */
  308. int dasm_encode(Dst_DECL, void *buffer)
  309. {
  310. dasm_State *D = Dst_REF;
  311. char *base = (char *)buffer;
  312. unsigned int *cp = (unsigned int *)buffer;
  313. int secnum;
  314. /* Encode all code sections. No support for data sections (yet). */
  315. for (secnum = 0; secnum < D->maxsection; secnum++) {
  316. dasm_Section *sec = D->sections + secnum;
  317. int *b = sec->buf;
  318. int *endb = sec->rbuf + sec->pos;
  319. while (b != endb) {
  320. dasm_ActList p = D->actionlist + *b++;
  321. while (1) {
  322. unsigned int ins = *p++;
  323. unsigned int action = (ins >> 16);
  324. int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
  325. switch (action) {
  326. case DASM_STOP: case DASM_SECTION: goto stop;
  327. case DASM_ESC: *cp++ = *p++; break;
  328. case DASM_REL_EXT:
  329. n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
  330. goto patchrel;
  331. case DASM_ALIGN:
  332. ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000;
  333. break;
  334. case DASM_REL_LG:
  335. if (n < 0) {
  336. n = (int)((ptrdiff_t)D->globals[-n-10] - (ptrdiff_t)cp - 4);
  337. goto patchrel;
  338. }
  339. /* fallthrough */
  340. case DASM_REL_PC:
  341. CK(n >= 0, UNDEF_PC);
  342. n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4;
  343. patchrel:
  344. if ((ins & 0x800) == 0) {
  345. CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL);
  346. cp[-1] |= ((n >> 2) & 0x00ffffff);
  347. } else if ((ins & 0x1000)) {
  348. CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL);
  349. goto patchimml8;
  350. } else if ((ins & 0x2000) == 0) {
  351. CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL);
  352. goto patchimml;
  353. } else {
  354. CK((n & 3) == 0 && -1020 <= n && n <= 1020, RANGE_REL);
  355. n >>= 2;
  356. goto patchimml;
  357. }
  358. break;
  359. case DASM_LABEL_LG:
  360. ins &= 2047; if (ins >= 20) D->globals[ins-20] = (void *)(base + n);
  361. break;
  362. case DASM_LABEL_PC: break;
  363. case DASM_IMM:
  364. cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31);
  365. break;
  366. case DASM_IMM12:
  367. cp[-1] |= dasm_imm12((unsigned int)n);
  368. break;
  369. case DASM_IMM16:
  370. cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff);
  371. break;
  372. case DASM_IMML8: patchimml8:
  373. cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) :
  374. ((-n & 0x0f) | ((-n & 0xf0) << 4));
  375. break;
  376. case DASM_IMML12: case DASM_IMMV8: patchimml:
  377. cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n);
  378. break;
  379. default: *cp++ = ins; break;
  380. }
  381. }
  382. stop: (void)0;
  383. }
  384. }
  385. if (base + D->codesize != (char *)cp) /* Check for phase errors. */
  386. return DASM_S_PHASE;
  387. return DASM_S_OK;
  388. }
  389. #undef CK
  390. /* Get PC label offset. */
  391. int dasm_getpclabel(Dst_DECL, unsigned int pc)
  392. {
  393. dasm_State *D = Dst_REF;
  394. if (pc*sizeof(int) < D->pcsize) {
  395. int pos = D->pclabels[pc];
  396. if (pos < 0) return *DASM_POS2PTR(D, -pos);
  397. if (pos > 0) return -1; /* Undefined. */
  398. }
  399. return -2; /* Unused or out of range. */
  400. }
  401. #ifdef DASM_CHECKS
  402. /* Optional sanity checker to call between isolated encoding steps. */
  403. int dasm_checkstep(Dst_DECL, int secmatch)
  404. {
  405. dasm_State *D = Dst_REF;
  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|(D->section-D->sections);
  416. return D->status;
  417. }
  418. #endif