2 ** DynASM PPC/PPC64 encoding engine.
3 ** Copyright (C) 2005-2016 Mike Pall. All rights reserved.
4 ** Released under the MIT license. See dynasm.lua for full copyright notice.
12 #define DASM_ARCH "ppc"
15 #define DASM_EXTERN(a,b,c,d) 0
18 /* Action definitions. */
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
,
28 /* Maximum number of section buffer positions for a single dasm_put() call. */
29 #define DASM_MAXSECPOS 25
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
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))
51 /* Action list type. */
52 typedef const unsigned int *dasm_ActList
;
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. */
64 /* Core structure holding the DynASM encoding 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. */
70 int *pclabels
; /* PC label chains/pos ptrs. */
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. */
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))
84 /* Initialize DynASM state. */
85 void dasm_init(Dst_DECL
, int maxsection
)
91 DASM_M_GROW(Dst
, struct dasm_State
, Dst_REF
, psz
, DASM_PSZ(maxsection
));
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. */
108 /* Free DynASM state. */
109 void dasm_free(Dst_DECL
)
111 dasm_State
*D
= Dst_REF
;
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
);
121 /* Setup global label array. Must be called before dasm_setup(). */
122 void dasm_setupglobal(Dst_DECL
, void **gl
, unsigned int maxgl
)
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));
129 /* Grow PC label array. Can be called after dasm_setup(), too. */
130 void dasm_growpc(Dst_DECL
, unsigned int maxpc
)
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
);
139 void dasm_setup(Dst_DECL
, const void *actionlist
)
141 dasm_State
*D
= Dst_REF
;
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;
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)
163 #define CK(x, st) ((void)0)
164 #define CKPL(kind, st) ((void)0)
167 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
168 void dasm_put(Dst_DECL
, int start
, ...)
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
;
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
);
189 unsigned int ins
= *p
++;
190 unsigned int action
= (ins
>> 16);
191 if (action
>= DASM__MAX
) {
194 int *pl
, n
= action
>= DASM_REL_PC
? va_arg(ap
, int) : 0;
196 case DASM_STOP
: goto stop
;
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;
204 n
= (ins
& 2047) - 10; pl
= D
->lglabels
+ n
;
205 /* Bkwd rel or global. */
206 if (n
>= 0) { CK(n
>=10||*pl
<0, RANGE_LG
); CKPL(lg
, LG
); goto putrel
; }
208 if (n
< 0) n
= 0; /* Start new chain for fwd rel if label exists. */
211 pl
= D
->pclabels
+ n
; CKPL(pc
, PC
);
214 if (n
< 0) { /* Label exists. Get label pos and store it. */
218 b
[pos
] = n
; /* Else link to rel chain, anchored at label. */
224 pl
= D
->lglabels
+ (ins
& 2047) - 10; CKPL(lg
, LG
); goto putlabel
;
226 pl
= D
->pclabels
+ n
; CKPL(pc
, PC
);
228 n
= *pl
; /* n > 0: Collapse rel chain and replace with label pos. */
229 while (n
> 0) { int *pb
= DASM_POS2PTR(D
, n
); n
= *pb
; *pb
= pos
;
231 *pl
= -pos
; /* Label exists now. */
232 b
[pos
++] = ofs
; /* Store pass1 offset estimate. */
236 CK((n
& ((1<<((ins
>>10)&31))-1)) == 0, RANGE_I
);
238 n
>>= ((ins
>>10)&31);
241 CK(((n
+ (1<<(((ins
>>5)&31)-1)))>>((ins
>>5)&31)) == 0, RANGE_I
);
243 CK((n
>>((ins
>>5)&31)) == 0, RANGE_I
);
248 CK((n
>> 6) == 0, RANGE_I
);
261 /* Pass 2: Link sections, shrink aligns, fix label offsets. */
262 int dasm_link(Dst_DECL
, size_t *szp
)
264 dasm_State
*D
= Dst_REF
;
270 if (D
->status
!= DASM_S_OK
) return D
->status
;
273 for (pc
= 0; pc
*sizeof(int) < D
->pcsize
; pc
++)
274 if (D
->pclabels
[pc
] > 0) return DASM_S_UNDEF_PC
|pc
;
278 { /* Handle globals not defined in this translation unit. */
280 for (idx
= 20; idx
*sizeof(int) < D
->lgsize
; idx
++) {
281 int n
= D
->lglabels
[idx
];
282 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
283 while (n
> 0) { int *pb
= DASM_POS2PTR(D
, n
); n
= *pb
; *pb
= -idx
; }
287 /* Combine all code sections. No support for data sections (yet). */
288 for (secnum
= 0; secnum
< D
->maxsection
; secnum
++) {
289 dasm_Section
*sec
= D
->sections
+ secnum
;
291 int pos
= DASM_SEC2POS(secnum
);
292 int lastpos
= sec
->pos
;
294 while (pos
!= lastpos
) {
295 dasm_ActList p
= D
->actionlist
+ b
[pos
++];
297 unsigned int ins
= *p
++;
298 unsigned int action
= (ins
>> 16);
300 case DASM_STOP
: case DASM_SECTION
: goto stop
;
301 case DASM_ESC
: p
++; break;
302 case DASM_REL_EXT
: break;
303 case DASM_ALIGN
: ofs
-= (b
[pos
++] + ofs
) & (ins
& 255); break;
304 case DASM_REL_LG
: case DASM_REL_PC
: pos
++; break;
305 case DASM_LABEL_LG
: case DASM_LABEL_PC
: b
[pos
++] += ofs
; break;
306 case DASM_IMM
: case DASM_IMMSH
: pos
++; break;
311 ofs
+= sec
->ofs
; /* Next section starts right after current section. */
314 D
->codesize
= ofs
; /* Total size of all code sections */
321 do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
323 #define CK(x, st) ((void)0)
326 /* Pass 3: Encode sections. */
327 int dasm_encode(Dst_DECL
, void *buffer
)
329 dasm_State
*D
= Dst_REF
;
330 char *base
= (char *)buffer
;
331 unsigned int *cp
= (unsigned int *)buffer
;
334 /* Encode all code sections. No support for data sections (yet). */
335 for (secnum
= 0; secnum
< D
->maxsection
; secnum
++) {
336 dasm_Section
*sec
= D
->sections
+ secnum
;
338 int *endb
= sec
->rbuf
+ sec
->pos
;
341 dasm_ActList p
= D
->actionlist
+ *b
++;
343 unsigned int ins
= *p
++;
344 unsigned int action
= (ins
>> 16);
345 int n
= (action
>= DASM_ALIGN
&& action
< DASM__MAX
) ? *b
++ : 0;
347 case DASM_STOP
: case DASM_SECTION
: goto stop
;
348 case DASM_ESC
: *cp
++ = *p
++; break;
350 n
= DASM_EXTERN(Dst
, (unsigned char *)cp
, (ins
& 2047), 1) - 4;
353 ins
&= 255; while ((((char *)cp
- base
) & ins
)) *cp
++ = 0x60000000;
356 CK(n
>= 0, UNDEF_LG
);
358 CK(n
>= 0, UNDEF_PC
);
359 n
= *DASM_POS2PTR(D
, n
) - (int)((char *)cp
- base
);
362 (((n
+4) + ((ins
& 2048) ? 0x00008000 : 0x02000000)) >>
363 ((ins
& 2048) ? 16 : 26)) == 0, RANGE_REL
);
364 cp
[-1] |= ((n
+4) & ((ins
& 2048) ? 0x0000fffc: 0x03fffffc));
367 ins
&= 2047; if (ins
>= 20) D
->globals
[ins
-10] = (void *)(base
+ n
);
369 case DASM_LABEL_PC
: break;
371 cp
[-1] |= (n
& ((1<<((ins
>>5)&31))-1)) << (ins
&31);
374 cp
[-1] |= (ins
& 1) ? ((n
&31)<<11)|((n
&32)>>4) : ((n
&31)<<6)|(n
&32);
376 default: *cp
++ = ins
; break;
383 if (base
+ D
->codesize
!= (char *)cp
) /* Check for phase errors. */
389 /* Get PC label offset. */
390 int dasm_getpclabel(Dst_DECL
, unsigned int pc
)
392 dasm_State
*D
= Dst_REF
;
393 if (pc
*sizeof(int) < D
->pcsize
) {
394 int pos
= D
->pclabels
[pc
];
395 if (pos
< 0) return *DASM_POS2PTR(D
, -pos
);
396 if (pos
> 0) return -1; /* Undefined. */
398 return -2; /* Unused or out of range. */
402 /* Optional sanity checker to call between isolated encoding steps. */
403 int dasm_checkstep(Dst_DECL
, int secmatch
)
405 dasm_State
*D
= Dst_REF
;
406 if (D
->status
== DASM_S_OK
) {
408 for (i
= 1; i
<= 9; i
++) {
409 if (D
->lglabels
[i
] > 0) { D
->status
= DASM_S_UNDEF_LG
|i
; break; }
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
);