dasm_arm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ** DynASM ARM encoding engine.
  3. ** Copyright (C) 2005-2017 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 (bias -10). */
  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. int i;
  78. Dst_REF = NULL;
  79. DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
  80. D = Dst_REF;
  81. D->psize = psz;
  82. D->lglabels = NULL;
  83. D->lgsize = 0;
  84. D->pclabels = NULL;
  85. D->pcsize = 0;
  86. D->globals = NULL;
  87. D->maxsection = maxsection;
  88. for (i = 0; i < maxsection; i++) {
  89. D->sections[i].buf = NULL; /* Need this for pass3. */
  90. D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i);
  91. D->sections[i].bsize = 0;
  92. D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */
  93. }
  94. }
  95. /* Free DynASM state. */
  96. void dasm_free(Dst_DECL)
  97. {
  98. dasm_State *D = Dst_REF;
  99. int i;
  100. for (i = 0; i < D->maxsection; i++)
  101. if (D->sections[i].buf)
  102. DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
  103. if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
  104. if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
  105. DASM_M_FREE(Dst, D, D->psize);
  106. }
  107. /* Setup global label array. Must be called before dasm_setup(). */
  108. void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
  109. {
  110. dasm_State *D = Dst_REF;
  111. D->globals = gl - 10; /* Negative bias to compensate for locals. */
  112. DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
  113. }
  114. /* Grow PC label array. Can be called after dasm_setup(), too. */
  115. void dasm_growpc(Dst_DECL, unsigned int maxpc)
  116. {
  117. dasm_State *D = Dst_REF;
  118. size_t osz = D->pcsize;
  119. DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
  120. memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
  121. }
  122. /* Setup encoder. */
  123. void dasm_setup(Dst_DECL, const void *actionlist)
  124. {
  125. dasm_State *D = Dst_REF;
  126. int i;
  127. D->actionlist = (dasm_ActList)actionlist;
  128. D->status = DASM_S_OK;
  129. D->section = &D->sections[0];
  130. memset((void *)D->lglabels, 0, D->lgsize);
  131. if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
  132. for (i = 0; i < D->maxsection; i++) {
  133. D->sections[i].pos = DASM_SEC2POS(i);
  134. D->sections[i].ofs = 0;
  135. }
  136. }
  137. #ifdef DASM_CHECKS
  138. #define CK(x, st) \
  139. do { if (!(x)) { \
  140. D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
  141. #define CKPL(kind, st) \
  142. do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
  143. D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
  144. #else
  145. #define CK(x, st) ((void)0)
  146. #define CKPL(kind, st) ((void)0)
  147. #endif
  148. static int dasm_imm12(unsigned int n)
  149. {
  150. int i;
  151. for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30))
  152. if (n <= 255) return (int)(n + (i << 8));
  153. return -1;
  154. }
  155. /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
  156. void dasm_put(Dst_DECL, int start, ...)
  157. {
  158. va_list ap;
  159. dasm_State *D = Dst_REF;
  160. dasm_ActList p = D->actionlist + start;
  161. dasm_Section *sec = D->section;
  162. int pos = sec->pos, ofs = sec->ofs;
  163. int *b;
  164. if (pos >= sec->epos) {
  165. DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
  166. sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
  167. sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
  168. sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
  169. }
  170. b = sec->rbuf;
  171. b[pos++] = start;
  172. va_start(ap, start);
  173. while (1) {
  174. unsigned int ins = *p++;
  175. unsigned int action = (ins >> 16);
  176. if (action >= DASM__MAX) {
  177. ofs += 4;
  178. } else {
  179. int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
  180. switch (action) {
  181. case DASM_STOP: goto stop;
  182. case DASM_SECTION:
  183. n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
  184. D->section = &D->sections[n]; goto stop;
  185. case DASM_ESC: p++; ofs += 4; break;
  186. case DASM_REL_EXT: break;
  187. case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
  188. case DASM_REL_LG:
  189. n = (ins & 2047) - 10; pl = D->lglabels + n;
  190. /* Bkwd rel or global. */
  191. if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
  192. pl += 10; n = *pl;
  193. if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
  194. goto linkrel;
  195. case DASM_REL_PC:
  196. pl = D->pclabels + n; CKPL(pc, PC);
  197. putrel:
  198. n = *pl;
  199. if (n < 0) { /* Label exists. Get label pos and store it. */
  200. b[pos] = -n;
  201. } else {
  202. linkrel:
  203. b[pos] = n; /* Else link to rel chain, anchored at label. */
  204. *pl = pos;
  205. }
  206. pos++;
  207. break;
  208. case DASM_LABEL_LG:
  209. pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
  210. case DASM_LABEL_PC:
  211. pl = D->pclabels + n; CKPL(pc, PC);
  212. putlabel:
  213. n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
  214. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
  215. }
  216. *pl = -pos; /* Label exists now. */
  217. b[pos++] = ofs; /* Store pass1 offset estimate. */
  218. break;
  219. case DASM_IMM:
  220. case DASM_IMM16:
  221. #ifdef DASM_CHECKS
  222. CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
  223. if ((ins & 0x8000))
  224. CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
  225. else
  226. CK((n>>((ins>>5)&31)) == 0, RANGE_I);
  227. #endif
  228. b[pos++] = n;
  229. break;
  230. case DASM_IMMV8:
  231. CK((n & 3) == 0, RANGE_I);
  232. n >>= 2;
  233. case DASM_IMML8:
  234. case DASM_IMML12:
  235. CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) :
  236. (((-n)>>((ins>>5)&31)) == 0), RANGE_I);
  237. b[pos++] = n;
  238. break;
  239. case DASM_IMM12:
  240. CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
  241. b[pos++] = n;
  242. break;
  243. }
  244. }
  245. }
  246. stop:
  247. va_end(ap);
  248. sec->pos = pos;
  249. sec->ofs = ofs;
  250. }
  251. #undef CK
  252. /* Pass 2: Link sections, shrink aligns, fix label offsets. */
  253. int dasm_link(Dst_DECL, size_t *szp)
  254. {
  255. dasm_State *D = Dst_REF;
  256. int secnum;
  257. int ofs = 0;
  258. #ifdef DASM_CHECKS
  259. *szp = 0;
  260. if (D->status != DASM_S_OK) return D->status;
  261. {
  262. int pc;
  263. for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
  264. if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
  265. }
  266. #endif
  267. { /* Handle globals not defined in this translation unit. */
  268. int idx;
  269. for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) {
  270. int n = D->lglabels[idx];
  271. /* Undefined label: Collapse rel chain and replace with marker (< 0). */
  272. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
  273. }
  274. }
  275. /* Combine all code sections. No support for data sections (yet). */
  276. for (secnum = 0; secnum < D->maxsection; secnum++) {
  277. dasm_Section *sec = D->sections + secnum;
  278. int *b = sec->rbuf;
  279. int pos = DASM_SEC2POS(secnum);
  280. int lastpos = sec->pos;
  281. while (pos != lastpos) {
  282. dasm_ActList p = D->actionlist + b[pos++];
  283. while (1) {
  284. unsigned int ins = *p++;
  285. unsigned int action = (ins >> 16);
  286. switch (action) {
  287. case DASM_STOP: case DASM_SECTION: goto stop;
  288. case DASM_ESC: p++; break;
  289. case DASM_REL_EXT: break;
  290. case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
  291. case DASM_REL_LG: case DASM_REL_PC: pos++; break;
  292. case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
  293. case DASM_IMM: case DASM_IMM12: case DASM_IMM16:
  294. case DASM_IMML8: case DASM_IMML12: case DASM_IMMV8: pos++; break;
  295. }
  296. }
  297. stop: (void)0;
  298. }
  299. ofs += sec->ofs; /* Next section starts right after current section. */
  300. }
  301. D->codesize = ofs; /* Total size of all code sections */
  302. *szp = ofs;
  303. return DASM_S_OK;
  304. }
  305. #ifdef DASM_CHECKS
  306. #define CK(x, st) \
  307. do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
  308. #else
  309. #define CK(x, st) ((void)0)
  310. #endif
  311. /* Pass 3: Encode sections. */
  312. int dasm_encode(Dst_DECL, void *buffer)
  313. {
  314. dasm_State *D = Dst_REF;
  315. char *base = (char *)buffer;
  316. unsigned int *cp = (unsigned int *)buffer;
  317. int secnum;
  318. /* Encode all code sections. No support for data sections (yet). */
  319. for (secnum = 0; secnum < D->maxsection; secnum++) {
  320. dasm_Section *sec = D->sections + secnum;
  321. int *b = sec->buf;
  322. int *endb = sec->rbuf + sec->pos;
  323. while (b != endb) {
  324. dasm_ActList p = D->actionlist + *b++;
  325. while (1) {
  326. unsigned int ins = *p++;
  327. unsigned int action = (ins >> 16);
  328. int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
  329. switch (action) {
  330. case DASM_STOP: case DASM_SECTION: goto stop;
  331. case DASM_ESC: *cp++ = *p++; break;
  332. case DASM_REL_EXT:
  333. n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
  334. goto patchrel;
  335. case DASM_ALIGN:
  336. ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000;
  337. break;
  338. case DASM_REL_LG:
  339. CK(n >= 0, UNDEF_LG);
  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-10] = (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