dasm_ppc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. ** DynASM PPC/PPC64 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 "ppc"
  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, DASM_IMMSH,
  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);
  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. /* Bkwd rel or global. */
  183. if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
  184. pl += 10; n = *pl;
  185. if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
  186. goto linkrel;
  187. case DASM_REL_PC:
  188. pl = D->pclabels + n; CKPL(pc, PC);
  189. putrel:
  190. n = *pl;
  191. if (n < 0) { /* Label exists. Get label pos and store it. */
  192. b[pos] = -n;
  193. } else {
  194. linkrel:
  195. b[pos] = n; /* Else link to rel chain, anchored at label. */
  196. *pl = pos;
  197. }
  198. pos++;
  199. break;
  200. case DASM_LABEL_LG:
  201. pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
  202. case DASM_LABEL_PC:
  203. pl = D->pclabels + n; CKPL(pc, PC);
  204. putlabel:
  205. n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
  206. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
  207. }
  208. *pl = -pos; /* Label exists now. */
  209. b[pos++] = ofs; /* Store pass1 offset estimate. */
  210. break;
  211. case DASM_IMM:
  212. #ifdef DASM_CHECKS
  213. CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
  214. #endif
  215. n >>= ((ins>>10)&31);
  216. #ifdef DASM_CHECKS
  217. if (ins & 0x8000)
  218. CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
  219. else
  220. CK((n>>((ins>>5)&31)) == 0, RANGE_I);
  221. #endif
  222. b[pos++] = n;
  223. break;
  224. case DASM_IMMSH:
  225. CK((n >> 6) == 0, RANGE_I);
  226. b[pos++] = n;
  227. break;
  228. }
  229. }
  230. }
  231. stop:
  232. va_end(ap);
  233. sec->pos = pos;
  234. sec->ofs = ofs;
  235. }
  236. #undef CK
  237. /* Pass 2: Link sections, shrink aligns, fix label offsets. */
  238. int dasm_link(Dst_DECL, size_t *szp)
  239. {
  240. dasm_State *D = Dst_REF;
  241. int secnum;
  242. int ofs = 0;
  243. #ifdef DASM_CHECKS
  244. *szp = 0;
  245. if (D->status != DASM_S_OK) return D->status;
  246. {
  247. int pc;
  248. for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
  249. if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
  250. }
  251. #endif
  252. { /* Handle globals not defined in this translation unit. */
  253. int idx;
  254. for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
  255. int n = D->lglabels[idx];
  256. /* Undefined label: Collapse rel chain and replace with marker (< 0). */
  257. while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
  258. }
  259. }
  260. /* Combine all code sections. No support for data sections (yet). */
  261. for (secnum = 0; secnum < D->maxsection; secnum++) {
  262. dasm_Section *sec = D->sections + secnum;
  263. int *b = sec->rbuf;
  264. int pos = DASM_SEC2POS(secnum);
  265. int lastpos = sec->pos;
  266. while (pos != lastpos) {
  267. dasm_ActList p = D->actionlist + b[pos++];
  268. while (1) {
  269. unsigned int ins = *p++;
  270. unsigned int action = (ins >> 16);
  271. switch (action) {
  272. case DASM_STOP: case DASM_SECTION: goto stop;
  273. case DASM_ESC: p++; break;
  274. case DASM_REL_EXT: break;
  275. case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
  276. case DASM_REL_LG: case DASM_REL_PC: pos++; break;
  277. case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
  278. case DASM_IMM: case DASM_IMMSH: pos++; break;
  279. }
  280. }
  281. stop: (void)0;
  282. }
  283. ofs += sec->ofs; /* Next section starts right after current section. */
  284. }
  285. D->codesize = ofs; /* Total size of all code sections */
  286. *szp = ofs;
  287. return DASM_S_OK;
  288. }
  289. #ifdef DASM_CHECKS
  290. #define CK(x, st) \
  291. do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
  292. #else
  293. #define CK(x, st) ((void)0)
  294. #endif
  295. /* Pass 3: Encode sections. */
  296. int dasm_encode(Dst_DECL, void *buffer)
  297. {
  298. dasm_State *D = Dst_REF;
  299. char *base = (char *)buffer;
  300. unsigned int *cp = (unsigned int *)buffer;
  301. int secnum;
  302. /* Encode all code sections. No support for data sections (yet). */
  303. for (secnum = 0; secnum < D->maxsection; secnum++) {
  304. dasm_Section *sec = D->sections + secnum;
  305. int *b = sec->buf;
  306. int *endb = sec->rbuf + sec->pos;
  307. while (b != endb) {
  308. dasm_ActList p = D->actionlist + *b++;
  309. while (1) {
  310. unsigned int ins = *p++;
  311. unsigned int action = (ins >> 16);
  312. int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
  313. switch (action) {
  314. case DASM_STOP: case DASM_SECTION: goto stop;
  315. case DASM_ESC: *cp++ = *p++; break;
  316. case DASM_REL_EXT:
  317. n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4;
  318. goto patchrel;
  319. case DASM_ALIGN:
  320. ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000;
  321. break;
  322. case DASM_REL_LG:
  323. if (n < 0) {
  324. n = (int)((ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp);
  325. goto patchrel;
  326. }
  327. /* fallthrough */
  328. case DASM_REL_PC:
  329. CK(n >= 0, UNDEF_PC);
  330. n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base);
  331. patchrel:
  332. CK((n & 3) == 0 &&
  333. (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >>
  334. ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL);
  335. cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc));
  336. break;
  337. case DASM_LABEL_LG:
  338. ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
  339. break;
  340. case DASM_LABEL_PC: break;
  341. case DASM_IMM:
  342. cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
  343. break;
  344. case DASM_IMMSH:
  345. cp[-1] |= (ins & 1) ? ((n&31)<<11)|((n&32)>>4) : ((n&31)<<6)|(n&32);
  346. break;
  347. default: *cp++ = ins; break;
  348. }
  349. }
  350. stop: (void)0;
  351. }
  352. }
  353. if (base + D->codesize != (char *)cp) /* Check for phase errors. */
  354. return DASM_S_PHASE;
  355. return DASM_S_OK;
  356. }
  357. #undef CK
  358. /* Get PC label offset. */
  359. int dasm_getpclabel(Dst_DECL, unsigned int pc)
  360. {
  361. dasm_State *D = Dst_REF;
  362. if (pc*sizeof(int) < D->pcsize) {
  363. int pos = D->pclabels[pc];
  364. if (pos < 0) return *DASM_POS2PTR(D, -pos);
  365. if (pos > 0) return -1; /* Undefined. */
  366. }
  367. return -2; /* Unused or out of range. */
  368. }
  369. #ifdef DASM_CHECKS
  370. /* Optional sanity checker to call between isolated encoding steps. */
  371. int dasm_checkstep(Dst_DECL, int secmatch)
  372. {
  373. dasm_State *D = Dst_REF;
  374. if (D->status == DASM_S_OK) {
  375. int i;
  376. for (i = 1; i <= 9; i++) {
  377. if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
  378. D->lglabels[i] = 0;
  379. }
  380. }
  381. if (D->status == DASM_S_OK && secmatch >= 0 &&
  382. D->section != &D->sections[secmatch])
  383. D->status = DASM_S_MATCH_SEC|(D->section-D->sections);
  384. return D->status;
  385. }
  386. #endif