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