dasm_mips.h 12 KB

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