2 ** DynASM MIPS 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.
12 #define DASM_ARCH "mips"
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_IMMS
,
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. */
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
)
90 DASM_M_GROW(Dst
, struct dasm_State
, Dst_REF
, psz
, DASM_PSZ(maxsection
));
98 D
->maxsection
= maxsection
;
99 memset((void *)D
->sections
, 0, maxsection
* sizeof(dasm_Section
));
102 /* Free DynASM state. */
103 void dasm_free(Dst_DECL
)
105 dasm_State
*D
= Dst_REF
;
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
);
115 /* Setup global label array. Must be called before dasm_setup(). */
116 void dasm_setupglobal(Dst_DECL
, void **gl
, unsigned int maxgl
)
118 dasm_State
*D
= Dst_REF
;
120 DASM_M_GROW(Dst
, int, D
->lglabels
, D
->lgsize
, (10+maxgl
)*sizeof(int));
123 /* Grow PC label array. Can be called after dasm_setup(), too. */
124 void dasm_growpc(Dst_DECL
, unsigned int maxpc
)
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
);
133 void dasm_setup(Dst_DECL
, const void *actionlist
)
135 dasm_State
*D
= Dst_REF
;
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;
153 D->status = DASM_S_##st|(int)(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|(int)(p-D->actionlist-1); return; } } while (0)
158 #define CK(x, st) ((void)0)
159 #define CKPL(kind, st) ((void)0)
162 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
163 void dasm_put(Dst_DECL
, int start
, ...)
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
;
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
);
184 unsigned int ins
= *p
++;
185 unsigned int action
= (ins
>> 16) - 0xff00;
186 if (action
>= DASM__MAX
) {
189 int *pl
, n
= action
>= DASM_REL_PC
? va_arg(ap
, int) : 0;
191 case DASM_STOP
: goto stop
;
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;
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
; }
203 if (n
< 0) n
= 0; /* Start new chain for fwd rel if label exists. */
206 pl
= D
->pclabels
+ n
; CKPL(pc
, PC
);
209 if (n
< 0) { /* Label exists. Get label pos and store it. */
213 b
[pos
] = n
; /* Else link to rel chain, anchored at label. */
219 pl
= D
->lglabels
+ (ins
& 2047) - 10; CKPL(lg
, LG
); goto putlabel
;
221 pl
= D
->pclabels
+ n
; CKPL(pc
, PC
);
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
;
226 *pl
= -pos
; /* Label exists now. */
227 b
[pos
++] = ofs
; /* Store pass1 offset estimate. */
229 case DASM_IMM
: case DASM_IMMS
:
231 CK((n
& ((1<<((ins
>>10)&31))-1)) == 0, RANGE_I
);
233 n
>>= ((ins
>>10)&31);
236 CK(((n
+ (1<<(((ins
>>5)&31)-1)))>>((ins
>>5)&31)) == 0, RANGE_I
);
238 CK((n
>>((ins
>>5)&31)) == 0, RANGE_I
);
252 /* Pass 2: Link sections, shrink aligns, fix label offsets. */
253 int dasm_link(Dst_DECL
, size_t *szp
)
255 dasm_State
*D
= Dst_REF
;
261 if (D
->status
!= DASM_S_OK
) return D
->status
;
264 for (pc
= 0; pc
*sizeof(int) < D
->pcsize
; pc
++)
265 if (D
->pclabels
[pc
] > 0) return DASM_S_UNDEF_PC
|pc
;
269 { /* Handle globals not defined in this translation unit. */
271 for (idx
= 10; idx
*sizeof(int) < D
->lgsize
; idx
++) {
272 int n
= D
->lglabels
[idx
];
273 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
274 while (n
> 0) { int *pb
= DASM_POS2PTR(D
, n
); n
= *pb
; *pb
= -idx
; }
278 /* Combine all code sections. No support for data sections (yet). */
279 for (secnum
= 0; secnum
< D
->maxsection
; secnum
++) {
280 dasm_Section
*sec
= D
->sections
+ secnum
;
282 int pos
= DASM_SEC2POS(secnum
);
283 int lastpos
= sec
->pos
;
285 while (pos
!= lastpos
) {
286 dasm_ActList p
= D
->actionlist
+ b
[pos
++];
288 unsigned int ins
= *p
++;
289 unsigned int action
= (ins
>> 16) - 0xff00;
291 case DASM_STOP
: case DASM_SECTION
: goto stop
;
292 case DASM_ESC
: p
++; break;
293 case DASM_REL_EXT
: break;
294 case DASM_ALIGN
: ofs
-= (b
[pos
++] + ofs
) & (ins
& 255); break;
295 case DASM_REL_LG
: case DASM_REL_PC
: pos
++; break;
296 case DASM_LABEL_LG
: case DASM_LABEL_PC
: b
[pos
++] += ofs
; break;
297 case DASM_IMM
: case DASM_IMMS
: pos
++; break;
302 ofs
+= sec
->ofs
; /* Next section starts right after current section. */
305 D
->codesize
= ofs
; /* Total size of all code sections */
312 do { if (!(x)) return DASM_S_##st|(int)(p-D->actionlist-1); } while (0)
314 #define CK(x, st) ((void)0)
317 /* Pass 3: Encode sections. */
318 int dasm_encode(Dst_DECL
, void *buffer
)
320 dasm_State
*D
= Dst_REF
;
321 char *base
= (char *)buffer
;
322 unsigned int *cp
= (unsigned int *)buffer
;
325 /* Encode all code sections. No support for data sections (yet). */
326 for (secnum
= 0; secnum
< D
->maxsection
; secnum
++) {
327 dasm_Section
*sec
= D
->sections
+ secnum
;
329 int *endb
= sec
->rbuf
+ sec
->pos
;
332 dasm_ActList p
= D
->actionlist
+ *b
++;
334 unsigned int ins
= *p
++;
335 unsigned int action
= (ins
>> 16) - 0xff00;
336 int n
= (action
>= DASM_ALIGN
&& action
< DASM__MAX
) ? *b
++ : 0;
338 case DASM_STOP
: case DASM_SECTION
: goto stop
;
339 case DASM_ESC
: *cp
++ = *p
++; break;
341 n
= DASM_EXTERN(Dst
, (unsigned char *)cp
, (ins
& 2047), 1);
344 ins
&= 255; while ((((char *)cp
- base
) & ins
)) *cp
++ = 0x60000000;
348 n
= (int)((ptrdiff_t)D
->globals
[-n
-10] - (ptrdiff_t)cp
);
353 CK(n
>= 0, UNDEF_PC
);
354 n
= *DASM_POS2PTR(D
, n
);
356 n
= (n
+ (int)(size_t)base
) & 0x0fffffff;
358 n
= n
- (int)((char *)cp
- base
);
360 unsigned int e
= 16 + ((ins
>> 12) & 15);
362 ((n
+ ((ins
& 2048) ? 0 : (1<<(e
+1)))) >> (e
+2)) == 0, RANGE_REL
);
363 cp
[-1] |= ((n
>>2) & ((1<<e
)-1));
367 ins
&= 2047; if (ins
>= 20) D
->globals
[ins
-20] = (void *)(base
+ n
);
369 case DASM_LABEL_PC
: break;
371 cp
[-1] |= ((n
>>3) & 4); n
&= 0x1f;
374 cp
[-1] |= (n
& ((1<<((ins
>>5)&31))-1)) << (ins
&31);
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
|(int)(D
->section
-D
->sections
);