2 * (C) Copyright IBM Corporation 2008
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Real-time assembly generation interface for Cell B.E. SPEs.
29 * \author Ian Romanick <idr@us.ibm.com>
32 #include "pipe/p_compiler.h"
33 #include "util/u_memory.h"
34 #include "rtasm_ppc_spe.h"
38 * SPE instruction types
40 * There are 6 primary instruction encodings used on the Cell's SPEs. Each of
41 * the following unions encodes one type.
44 * If, at some point, we start generating SPE code from a little-endian host
45 * these unions will not work.
49 * Encode one output register with two input registers
63 * Encode one output register with three input registers
78 * Encode one output register with one input reg. and a 7-bit signed immed
92 * Encode one output register with one input reg. and an 8-bit signed immed
106 * Encode one output register with one input reg. and a 10-bit signed immed
108 union spe_inst_RI10
{
120 * Encode one output register with a 16-bit signed immediate
122 union spe_inst_RI16
{
133 * Encode one output register with a 18-bit signed immediate
135 union spe_inst_RI18
{
146 static void emit_RR(struct spe_function
*p
, unsigned op
, unsigned rT
,
147 unsigned rA
, unsigned rB
)
149 union spe_inst_RR inst
;
159 static void emit_RRR(struct spe_function
*p
, unsigned op
, unsigned rT
,
160 unsigned rA
, unsigned rB
, unsigned rC
)
162 union spe_inst_RRR inst
;
173 static void emit_RI7(struct spe_function
*p
, unsigned op
, unsigned rT
,
174 unsigned rA
, int imm
)
176 union spe_inst_RI7 inst
;
187 static void emit_RI8(struct spe_function
*p
, unsigned op
, unsigned rT
,
188 unsigned rA
, int imm
)
190 union spe_inst_RI8 inst
;
201 static void emit_RI10(struct spe_function
*p
, unsigned op
, unsigned rT
,
202 unsigned rA
, int imm
)
204 union spe_inst_RI10 inst
;
214 static void emit_RI16(struct spe_function
*p
, unsigned op
, unsigned rT
,
217 union spe_inst_RI16 inst
;
226 static void emit_RI18(struct spe_function
*p
, unsigned op
, unsigned rT
,
229 union spe_inst_RI18 inst
;
240 #define EMIT_(_name, _op) \
241 void _name (struct spe_function *p, unsigned rT) \
243 emit_RR(p, _op, rT, 0, 0); \
246 #define EMIT_R(_name, _op) \
247 void _name (struct spe_function *p, unsigned rT, unsigned rA) \
249 emit_RR(p, _op, rT, rA, 0); \
252 #define EMIT_RR(_name, _op) \
253 void _name (struct spe_function *p, unsigned rT, unsigned rA, unsigned rB) \
255 emit_RR(p, _op, rT, rA, rB); \
258 #define EMIT_RRR(_name, _op) \
259 void _name (struct spe_function *p, unsigned rT, unsigned rA, unsigned rB, unsigned rC) \
261 emit_RRR(p, _op, rT, rA, rB, rC); \
264 #define EMIT_RI7(_name, _op) \
265 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
267 emit_RI7(p, _op, rT, rA, imm); \
270 #define EMIT_RI8(_name, _op, bias) \
271 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
273 emit_RI8(p, _op, rT, rA, bias - imm); \
276 #define EMIT_RI10(_name, _op) \
277 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
279 emit_RI10(p, _op, rT, rA, imm); \
282 #define EMIT_RI16(_name, _op) \
283 void _name (struct spe_function *p, unsigned rT, int imm) \
285 emit_RI16(p, _op, rT, imm); \
288 #define EMIT_RI18(_name, _op) \
289 void _name (struct spe_function *p, unsigned rT, int imm) \
291 emit_RI18(p, _op, rT, imm); \
294 #define EMIT_I16(_name, _op) \
295 void _name (struct spe_function *p, int imm) \
297 emit_RI16(p, _op, 0, imm); \
300 #include "rtasm_ppc_spe.h"
304 * Initialize an spe_function.
305 * \param code_size size of instruction buffer to allocate, in bytes.
307 void spe_init_func(struct spe_function
*p
, unsigned code_size
)
309 p
->store
= align_malloc(code_size
, 16);
312 /* Conservatively treat R0 - R2 and R80 - R127 as non-volatile.
315 p
->regs
[1] = (1U << (80 - 64)) - 1;
319 void spe_release_func(struct spe_function
*p
)
321 if (p
->store
!= NULL
) {
322 align_free(p
->store
);
330 * Alloate a SPE register.
331 * \return register index or -1 if none left.
333 int spe_allocate_available_register(struct spe_function
*p
)
336 for (i
= 0; i
< SPE_NUM_REGS
; i
++) {
337 const uint64_t mask
= (1ULL << (i
% 64));
338 const unsigned idx
= i
/ 64;
340 if ((p
->regs
[idx
] & mask
) != 0) {
341 p
->regs
[idx
] &= ~mask
;
351 * Mark the given SPE register as "allocated".
353 int spe_allocate_register(struct spe_function
*p
, int reg
)
355 const unsigned idx
= reg
/ 64;
356 const unsigned bit
= reg
% 64;
358 assert(reg
< SPE_NUM_REGS
);
359 assert((p
->regs
[idx
] & (1ULL << bit
)) != 0);
361 p
->regs
[idx
] &= ~(1ULL << bit
);
367 * Mark the given SPE register as "unallocated".
369 void spe_release_register(struct spe_function
*p
, int reg
)
371 const unsigned idx
= reg
/ 64;
372 const unsigned bit
= reg
% 64;
374 assert(reg
< SPE_NUM_REGS
);
375 assert((p
->regs
[idx
] & (1ULL << bit
)) == 0);
377 p
->regs
[idx
] |= (1ULL << bit
);
382 * For branch instructions:
383 * \param d if 1, disable interupts if branch is taken
384 * \param e if 1, enable interupts if branch is taken
385 * If d and e are both zero, don't change interupt status (right?)
388 /** Branch Indirect to address in rA */
389 void spe_bi(struct spe_function
*p
, unsigned rA
, int d
, int e
)
391 emit_RI7(p
, 0x1a8, 0, rA
, (d
<< 5) | (e
<< 4));
394 /** Interupt Return */
395 void spe_iret(struct spe_function
*p
, unsigned rA
, int d
, int e
)
397 emit_RI7(p
, 0x1aa, 0, rA
, (d
<< 5) | (e
<< 4));
400 /** Branch indirect and set link on external data */
401 void spe_bisled(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
404 emit_RI7(p
, 0x1ab, rT
, rA
, (d
<< 5) | (e
<< 4));
407 /** Branch indirect and set link. Save PC in rT, jump to rA. */
408 void spe_bisl(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
411 emit_RI7(p
, 0x1a9, rT
, rA
, (d
<< 5) | (e
<< 4));
414 /** Branch indirect if zero word. If rT.word[0]==0, jump to rA. */
415 void spe_biz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
417 emit_RI7(p
, 0x128, rT
, rA
, (d
<< 5) | (e
<< 4));
420 /** Branch indirect if non-zero word. If rT.word[0]!=0, jump to rA. */
421 void spe_binz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
423 emit_RI7(p
, 0x129, rT
, rA
, (d
<< 5) | (e
<< 4));
426 /** Branch indirect if zero halfword. If rT.halfword[1]==0, jump to rA. */
427 void spe_bihz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
429 emit_RI7(p
, 0x12a, rT
, rA
, (d
<< 5) | (e
<< 4));
432 /** Branch indirect if non-zero halfword. If rT.halfword[1]!=0, jump to rA. */
433 void spe_bihnz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
435 emit_RI7(p
, 0x12b, rT
, rA
, (d
<< 5) | (e
<< 4));
439 /* Hint-for-branch instructions
448 /* Control instructions
452 EMIT_RR (spe_stopd
, 0x140);
453 EMIT_ (spe_lnop
, 0x001);
454 EMIT_ (spe_nop
, 0x201);
456 EMIT_ (spe_dsync
, 0x003);
457 EMIT_R (spe_mfspr
, 0x00c);
458 EMIT_R (spe_mtspr
, 0x10c);
461 #endif /* GALLIUM_CELL */