aboutsummaryrefslogtreecommitdiff
path: root/dynasm
diff options
context:
space:
mode:
authorMike Pall <mike>2010-08-26 17:27:17 +0200
committerMike Pall <mike>2010-08-26 17:27:17 +0200
commit4aa8757aac08fcd340649d9850f7bf88d5911f98 (patch)
treec000f9b1c03493659ae86b411c04ac7f151cbb4d /dynasm
parent4f47d31fefbe8a690fbf85e8bf2b74598e72a183 (diff)
downloadluajit-4aa8757aac08fcd340649d9850f7bf88d5911f98.tar.gz
luajit-4aa8757aac08fcd340649d9850f7bf88d5911f98.tar.bz2
luajit-4aa8757aac08fcd340649d9850f7bf88d5911f98.zip
PPC: Add DynASM PowerPC encoding engine.
Diffstat (limited to 'dynasm')
-rw-r--r--dynasm/dasm_ppc.h408
1 files changed, 408 insertions, 0 deletions
diff --git a/dynasm/dasm_ppc.h b/dynasm/dasm_ppc.h
new file mode 100644
index 00000000..85aeccd8
--- /dev/null
+++ b/dynasm/dasm_ppc.h
@@ -0,0 +1,408 @@
1/*
2** DynASM PPC encoding engine.
3** Copyright (C) 2005-2010 Mike Pall. All rights reserved.
4** Released under the MIT/X 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. */
19enum {
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,
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. */
52typedef const unsigned int *dasm_ActList;
53
54/* Per-section structure. */
55typedef 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. */
65struct 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 (bias -10). */
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. */
85void dasm_init(Dst_DECL, int maxsection)
86{
87 dasm_State *D;
88 size_t psz = 0;
89 int i;
90 Dst_REF = NULL;
91 DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
92 D = Dst_REF;
93 D->psize = psz;
94 D->lglabels = NULL;
95 D->lgsize = 0;
96 D->pclabels = NULL;
97 D->pcsize = 0;
98 D->globals = NULL;
99 D->maxsection = maxsection;
100 for (i = 0; i < maxsection; i++) {
101 D->sections[i].buf = NULL; /* Need this for pass3. */
102 D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i);
103 D->sections[i].bsize = 0;
104 D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */
105 }
106}
107
108/* Free DynASM state. */
109void dasm_free(Dst_DECL)
110{
111 dasm_State *D = Dst_REF;
112 int i;
113 for (i = 0; i < D->maxsection; i++)
114 if (D->sections[i].buf)
115 DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
116 if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
117 if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
118 DASM_M_FREE(Dst, D, D->psize);
119}
120
121/* Setup global label array. Must be called before dasm_setup(). */
122void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
123{
124 dasm_State *D = Dst_REF;
125 D->globals = gl - 10; /* Negative bias to compensate for locals. */
126 DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
127}
128
129/* Grow PC label array. Can be called after dasm_setup(), too. */
130void dasm_growpc(Dst_DECL, unsigned int maxpc)
131{
132 dasm_State *D = Dst_REF;
133 size_t osz = D->pcsize;
134 DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
135 memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
136}
137
138/* Setup encoder. */
139void dasm_setup(Dst_DECL, const void *actionlist)
140{
141 dasm_State *D = Dst_REF;
142 int i;
143 D->actionlist = (dasm_ActList)actionlist;
144 D->status = DASM_S_OK;
145 D->section = &D->sections[0];
146 memset((void *)D->lglabels, 0, D->lgsize);
147 if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
148 for (i = 0; i < D->maxsection; i++) {
149 D->sections[i].pos = DASM_SEC2POS(i);
150 D->sections[i].ofs = 0;
151 }
152}
153
154
155#ifdef DASM_CHECKS
156#define CK(x, st) \
157 do { if (!(x)) { \
158 D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
159#define CKPL(kind, st) \
160 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
161 D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
162#else
163#define CK(x, st) ((void)0)
164#define CKPL(kind, st) ((void)0)
165#endif
166
167/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
168void dasm_put(Dst_DECL, int start, ...)
169{
170 va_list ap;
171 dasm_State *D = Dst_REF;
172 dasm_ActList p = D->actionlist + start;
173 dasm_Section *sec = D->section;
174 int pos = sec->pos, ofs = sec->ofs;
175 int *b;
176
177 if (pos >= sec->epos) {
178 DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
179 sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
180 sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
181 sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
182 }
183
184 b = sec->rbuf;
185 b[pos++] = start;
186
187 va_start(ap, start);
188 while (1) {
189 unsigned int ins = *p++;
190 unsigned int action = (ins >> 16);
191 if (action >= DASM__MAX) {
192 ofs += 4;
193 } else {
194 int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
195 switch (action) {
196 case DASM_STOP: goto stop;
197 case DASM_SECTION:
198 n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
199 D->section = &D->sections[n]; goto stop;
200 case DASM_ESC: p++; ofs += 4; break;
201 case DASM_REL_EXT: break;
202 case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
203 case DASM_REL_LG:
204 n = (ins & 2047) - 10; pl = D->lglabels + n;
205 if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */
206 pl += 10; n = *pl;
207 if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
208 goto linkrel;
209 case DASM_REL_PC:
210 pl = D->pclabels + n; CKPL(pc, PC);
211 putrel:
212 n = *pl;
213 if (n < 0) { /* Label exists. Get label pos and store it. */
214 b[pos] = -n;
215 } else {
216 linkrel:
217 b[pos] = n; /* Else link to rel chain, anchored at label. */
218 *pl = pos;
219 }
220 pos++;
221 break;
222 case DASM_LABEL_LG:
223 pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
224 case DASM_LABEL_PC:
225 pl = D->pclabels + n; CKPL(pc, PC);
226 putlabel:
227 n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
228 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
229 }
230 *pl = -pos; /* Label exists now. */
231 b[pos++] = ofs; /* Store pass1 offset estimate. */
232 break;
233 case DASM_IMM:
234#ifdef DASM_CHECKS
235 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
236 if (ins & 0x8000)
237 CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
238 else
239 CK((n>>((ins>>5)&31)) == 0, RANGE_I);
240#endif
241 b[pos++] = n;
242 break;
243 }
244 }
245 }
246stop:
247 va_end(ap);
248 sec->pos = pos;
249 sec->ofs = ofs;
250}
251#undef CK
252
253/* Pass 2: Link sections, shrink aligns, fix label offsets. */
254int dasm_link(Dst_DECL, size_t *szp)
255{
256 dasm_State *D = Dst_REF;
257 int secnum;
258 int ofs = 0;
259
260#ifdef DASM_CHECKS
261 *szp = 0;
262 if (D->status != DASM_S_OK) return D->status;
263 {
264 int pc;
265 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
266 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
267 }
268#endif
269
270 { /* Handle globals not defined in this translation unit. */
271 int idx;
272 for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) {
273 int n = D->lglabels[idx];
274 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
275 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
276 }
277 }
278
279 /* Combine all code sections. No support for data sections (yet). */
280 for (secnum = 0; secnum < D->maxsection; secnum++) {
281 dasm_Section *sec = D->sections + secnum;
282 int *b = sec->rbuf;
283 int pos = DASM_SEC2POS(secnum);
284 int lastpos = sec->pos;
285
286 while (pos != lastpos) {
287 dasm_ActList p = D->actionlist + b[pos++];
288 while (1) {
289 unsigned int ins = *p++;
290 unsigned int action = (ins >> 16);
291 switch (action) {
292 case DASM_STOP: case DASM_SECTION: goto stop;
293 case DASM_ESC: p++; break;
294 case DASM_REL_EXT: break;
295 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
296 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
297 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
298 case DASM_IMM: pos++; break;
299 }
300 }
301 stop: (void)0;
302 }
303 ofs += sec->ofs; /* Next section starts right after current section. */
304 }
305
306 D->codesize = ofs; /* Total size of all code sections */
307 *szp = ofs;
308 return DASM_S_OK;
309}
310
311#ifdef DASM_CHECKS
312#define CK(x, st) \
313 do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
314#else
315#define CK(x, st) ((void)0)
316#endif
317
318/* Pass 3: Encode sections. */
319int dasm_encode(Dst_DECL, void *buffer)
320{
321 dasm_State *D = Dst_REF;
322 char *base = (char *)buffer;
323 unsigned int *cp = (unsigned int *)buffer;
324 int secnum;
325
326 /* Encode all code sections. No support for data sections (yet). */
327 for (secnum = 0; secnum < D->maxsection; secnum++) {
328 dasm_Section *sec = D->sections + secnum;
329 int *b = sec->buf;
330 int *endb = sec->rbuf + sec->pos;
331
332 while (b != endb) {
333 dasm_ActList p = D->actionlist + *b++;
334 while (1) {
335 unsigned int ins = *p++;
336 unsigned int action = (ins >> 16);
337 int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
338 switch (action) {
339 case DASM_STOP: case DASM_SECTION: goto stop;
340 case DASM_ESC: *cp++ = *p++; break;
341 case DASM_REL_EXT:
342 n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1);
343 goto patchrel;
344 case DASM_ALIGN:
345 ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000;
346 break;
347 case DASM_REL_LG:
348 CK(n >= 0, UNDEF_LG);
349 case DASM_REL_PC:
350 CK(n >= 0, UNDEF_PC);
351 n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base);
352 patchrel:
353 CK((n & 3) == 0 &&
354 (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >>
355 ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL);
356 cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc));
357 break;
358 case DASM_LABEL_LG:
359 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
360 break;
361 case DASM_LABEL_PC: break;
362 case DASM_IMM:
363 cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31);
364 break;
365 default: *cp++ = ins; break;
366 }
367 }
368 stop: (void)0;
369 }
370 }
371
372 if (base + D->codesize != (char *)cp) /* Check for phase errors. */
373 return DASM_S_PHASE;
374 return DASM_S_OK;
375}
376#undef CK
377
378/* Get PC label offset. */
379int dasm_getpclabel(Dst_DECL, unsigned int pc)
380{
381 dasm_State *D = Dst_REF;
382 if (pc*sizeof(int) < D->pcsize) {
383 int pos = D->pclabels[pc];
384 if (pos < 0) return *DASM_POS2PTR(D, -pos);
385 if (pos > 0) return -1; /* Undefined. */
386 }
387 return -2; /* Unused or out of range. */
388}
389
390#ifdef DASM_CHECKS
391/* Optional sanity checker to call between isolated encoding steps. */
392int dasm_checkstep(Dst_DECL, int secmatch)
393{
394 dasm_State *D = Dst_REF;
395 if (D->status == DASM_S_OK) {
396 int i;
397 for (i = 1; i <= 9; i++) {
398 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
399 D->lglabels[i] = 0;
400 }
401 }
402 if (D->status == DASM_S_OK && secmatch >= 0 &&
403 D->section != &D->sections[secmatch])
404 D->status = DASM_S_MATCH_SEC|(D->section-D->sections);
405 return D->status;
406}
407#endif
408