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>
35 #include "pipe/p_compiler.h"
36 #include "util/u_memory.h"
37 #include "rtasm_ppc_spe.h"
42 * SPE instruction types
44 * There are 6 primary instruction encodings used on the Cell's SPEs. Each of
45 * the following unions encodes one type.
48 * If, at some point, we start generating SPE code from a little-endian host
49 * these unions will not work.
53 * Encode one output register with two input registers
67 * Encode one output register with three input registers
82 * Encode one output register with one input reg. and a 7-bit signed immed
96 * Encode one output register with one input reg. and an 8-bit signed immed
110 * Encode one output register with one input reg. and a 10-bit signed immed
112 union spe_inst_RI10
{
124 * Encode one output register with a 16-bit signed immediate
126 union spe_inst_RI16
{
137 * Encode one output register with a 18-bit signed immediate
139 union spe_inst_RI18
{
151 indent(const struct spe_function
*p
)
154 for (i
= 0; i
< p
->indent
; i
++) {
161 rem_prefix(const char *longname
)
167 static void emit_RR(struct spe_function
*p
, unsigned op
, unsigned rT
,
168 unsigned rA
, unsigned rB
, const char *name
)
170 union spe_inst_RR inst
;
175 p
->store
[p
->num_inst
++] = inst
.bits
;
176 assert(p
->num_inst
<= p
->max_inst
);
179 printf("%s\tr%d, r%d, r%d\n", rem_prefix(name
), rT
, rA
, rB
);
184 static void emit_RRR(struct spe_function
*p
, unsigned op
, unsigned rT
,
185 unsigned rA
, unsigned rB
, unsigned rC
, const char *name
)
187 union spe_inst_RRR inst
;
193 p
->store
[p
->num_inst
++] = inst
.bits
;
194 assert(p
->num_inst
<= p
->max_inst
);
197 printf("%s\tr%d, r%d, r%d, r%d\n", rem_prefix(name
), rT
, rA
, rB
, rB
);
202 static void emit_RI7(struct spe_function
*p
, unsigned op
, unsigned rT
,
203 unsigned rA
, int imm
, const char *name
)
205 union spe_inst_RI7 inst
;
210 p
->store
[p
->num_inst
++] = inst
.bits
;
211 assert(p
->num_inst
<= p
->max_inst
);
214 printf("%s\tr%d, r%d, 0x%x\n", rem_prefix(name
), rT
, rA
, imm
);
220 static void emit_RI8(struct spe_function
*p
, unsigned op
, unsigned rT
,
221 unsigned rA
, int imm
, const char *name
)
223 union spe_inst_RI8 inst
;
228 p
->store
[p
->num_inst
++] = inst
.bits
;
229 assert(p
->num_inst
<= p
->max_inst
);
232 printf("%s\tr%d, r%d, 0x%x\n", rem_prefix(name
), rT
, rA
, imm
);
238 static void emit_RI10(struct spe_function
*p
, unsigned op
, unsigned rT
,
239 unsigned rA
, int imm
, const char *name
)
241 union spe_inst_RI10 inst
;
246 p
->store
[p
->num_inst
++] = inst
.bits
;
247 assert(p
->num_inst
<= p
->max_inst
);
250 printf("%s\tr%d, r%d, 0x%x\n", rem_prefix(name
), rT
, rA
, imm
);
255 static void emit_RI16(struct spe_function
*p
, unsigned op
, unsigned rT
,
256 int imm
, const char *name
)
258 union spe_inst_RI16 inst
;
262 p
->store
[p
->num_inst
++] = inst
.bits
;
263 assert(p
->num_inst
<= p
->max_inst
);
266 printf("%s\tr%d, 0x%x\n", rem_prefix(name
), rT
, imm
);
271 static void emit_RI18(struct spe_function
*p
, unsigned op
, unsigned rT
,
272 int imm
, const char *name
)
274 union spe_inst_RI18 inst
;
278 p
->store
[p
->num_inst
++] = inst
.bits
;
279 assert(p
->num_inst
<= p
->max_inst
);
282 printf("%s\tr%d, 0x%x\n", rem_prefix(name
), rT
, imm
);
289 #define EMIT_(_name, _op) \
290 void _name (struct spe_function *p, unsigned rT) \
292 emit_RR(p, _op, rT, 0, 0, __FUNCTION__); \
295 #define EMIT_R(_name, _op) \
296 void _name (struct spe_function *p, unsigned rT, unsigned rA) \
298 emit_RR(p, _op, rT, rA, 0, __FUNCTION__); \
301 #define EMIT_RR(_name, _op) \
302 void _name (struct spe_function *p, unsigned rT, unsigned rA, unsigned rB) \
304 emit_RR(p, _op, rT, rA, rB, __FUNCTION__); \
307 #define EMIT_RRR(_name, _op) \
308 void _name (struct spe_function *p, unsigned rT, unsigned rA, unsigned rB, unsigned rC) \
310 emit_RRR(p, _op, rT, rA, rB, rC, __FUNCTION__); \
313 #define EMIT_RI7(_name, _op) \
314 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
316 emit_RI7(p, _op, rT, rA, imm, __FUNCTION__); \
319 #define EMIT_RI8(_name, _op, bias) \
320 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
322 emit_RI8(p, _op, rT, rA, bias - imm, __FUNCTION__); \
325 #define EMIT_RI10(_name, _op) \
326 void _name (struct spe_function *p, unsigned rT, unsigned rA, int imm) \
328 emit_RI10(p, _op, rT, rA, imm, __FUNCTION__); \
331 #define EMIT_RI16(_name, _op) \
332 void _name (struct spe_function *p, unsigned rT, int imm) \
334 emit_RI16(p, _op, rT, imm, __FUNCTION__); \
337 #define EMIT_RI18(_name, _op) \
338 void _name (struct spe_function *p, unsigned rT, int imm) \
340 emit_RI18(p, _op, rT, imm, __FUNCTION__); \
343 #define EMIT_I16(_name, _op) \
344 void _name (struct spe_function *p, int imm) \
346 emit_RI16(p, _op, 0, imm, __FUNCTION__); \
349 #include "rtasm_ppc_spe.h"
353 * Initialize an spe_function.
354 * \param code_size size of instruction buffer to allocate, in bytes.
356 void spe_init_func(struct spe_function
*p
, unsigned code_size
)
358 p
->store
= align_malloc(code_size
, 16);
360 p
->max_inst
= code_size
/ SPE_INST_SIZE
;
362 /* Conservatively treat R0 - R2 and R80 - R127 as non-volatile.
365 p
->regs
[1] = (1U << (80 - 64)) - 1;
372 void spe_release_func(struct spe_function
*p
)
374 assert(p
->num_inst
<= p
->max_inst
);
375 if (p
->store
!= NULL
) {
376 align_free(p
->store
);
383 * Alloate a SPE register.
384 * \return register index or -1 if none left.
386 int spe_allocate_available_register(struct spe_function
*p
)
389 for (i
= 0; i
< SPE_NUM_REGS
; i
++) {
390 const uint64_t mask
= (1ULL << (i
% 64));
391 const unsigned idx
= i
/ 64;
394 if ((p
->regs
[idx
] & mask
) != 0) {
395 p
->regs
[idx
] &= ~mask
;
405 * Mark the given SPE register as "allocated".
407 int spe_allocate_register(struct spe_function
*p
, int reg
)
409 const unsigned idx
= reg
/ 64;
410 const unsigned bit
= reg
% 64;
412 assert(reg
< SPE_NUM_REGS
);
413 assert((p
->regs
[idx
] & (1ULL << bit
)) != 0);
415 p
->regs
[idx
] &= ~(1ULL << bit
);
421 * Mark the given SPE register as "unallocated".
423 void spe_release_register(struct spe_function
*p
, int reg
)
425 const unsigned idx
= reg
/ 64;
426 const unsigned bit
= reg
% 64;
430 assert(reg
< SPE_NUM_REGS
);
431 assert((p
->regs
[idx
] & (1ULL << bit
)) == 0);
433 p
->regs
[idx
] |= (1ULL << bit
);
438 spe_print_code(struct spe_function
*p
, boolean enable
)
445 spe_indent(struct spe_function
*p
, int spaces
)
452 spe_comment(struct spe_function
*p
, int rel_indent
, const char *s
)
455 p
->indent
+= rel_indent
;
457 p
->indent
-= rel_indent
;
464 * For branch instructions:
465 * \param d if 1, disable interupts if branch is taken
466 * \param e if 1, enable interupts if branch is taken
467 * If d and e are both zero, don't change interupt status (right?)
470 /** Branch Indirect to address in rA */
471 void spe_bi(struct spe_function
*p
, unsigned rA
, int d
, int e
)
473 emit_RI7(p
, 0x1a8, 0, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
476 /** Interupt Return */
477 void spe_iret(struct spe_function
*p
, unsigned rA
, int d
, int e
)
479 emit_RI7(p
, 0x1aa, 0, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
482 /** Branch indirect and set link on external data */
483 void spe_bisled(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
486 emit_RI7(p
, 0x1ab, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
489 /** Branch indirect and set link. Save PC in rT, jump to rA. */
490 void spe_bisl(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
493 emit_RI7(p
, 0x1a9, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
496 /** Branch indirect if zero word. If rT.word[0]==0, jump to rA. */
497 void spe_biz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
499 emit_RI7(p
, 0x128, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
502 /** Branch indirect if non-zero word. If rT.word[0]!=0, jump to rA. */
503 void spe_binz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
505 emit_RI7(p
, 0x129, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
508 /** Branch indirect if zero halfword. If rT.halfword[1]==0, jump to rA. */
509 void spe_bihz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
511 emit_RI7(p
, 0x12a, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
514 /** Branch indirect if non-zero halfword. If rT.halfword[1]!=0, jump to rA. */
515 void spe_bihnz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
517 emit_RI7(p
, 0x12b, rT
, rA
, (d
<< 5) | (e
<< 4), __FUNCTION__
);
521 /* Hint-for-branch instructions
530 /* Control instructions
534 EMIT_RR (spe_stopd
, 0x140);
535 EMIT_ (spe_lnop
, 0x001);
536 EMIT_ (spe_nop
, 0x201);
538 EMIT_ (spe_dsync
, 0x003);
539 EMIT_R (spe_mfspr
, 0x00c);
540 EMIT_R (spe_mtspr
, 0x10c);
545 ** Helper / "macro" instructions.
546 ** Use somewhat verbose names as a reminder that these aren't native
552 spe_load_float(struct spe_function
*p
, unsigned rT
, float x
)
557 else if (x
== 0.5f
) {
558 spe_ilhu(p
, rT
, 0x3f00);
560 else if (x
== 1.0f
) {
561 spe_ilhu(p
, rT
, 0x3f80);
563 else if (x
== -1.0f
) {
564 spe_ilhu(p
, rT
, 0xbf80);
572 spe_ilhu(p
, rT
, bits
.u
>> 16);
573 spe_iohl(p
, rT
, bits
.u
& 0xffff);
579 spe_load_int(struct spe_function
*p
, unsigned rT
, int i
)
581 if (-32768 <= i
&& i
<= 32767) {
585 spe_ilhu(p
, rT
, i
>> 16);
587 spe_iohl(p
, rT
, i
& 0xffff);
593 spe_splat(struct spe_function
*p
, unsigned rT
, unsigned rA
)
595 spe_ila(p
, rT
, 66051);
596 spe_shufb(p
, rT
, rA
, rA
, rT
);
601 spe_complement(struct spe_function
*p
, unsigned rT
)
603 spe_nor(p
, rT
, rT
, rT
);
608 spe_move(struct spe_function
*p
, unsigned rT
, unsigned rA
)
610 spe_ori(p
, rT
, rA
, 0);
615 spe_zero(struct spe_function
*p
, unsigned rT
)
617 spe_xor(p
, rT
, rT
, rT
);
622 spe_splat_word(struct spe_function
*p
, unsigned rT
, unsigned rA
, int word
)
629 spe_ila(p
, tmp1
, 66051);
630 spe_shufb(p
, rT
, rA
, rA
, tmp1
);
633 /* XXX review this, we may not need the rotqbyi instruction */
635 int tmp2
= spe_allocate_available_register(p
);
637 spe_ila(p
, tmp1
, 66051);
638 spe_rotqbyi(p
, tmp2
, rA
, 4 * word
);
639 spe_shufb(p
, rT
, tmp2
, tmp2
, tmp1
);
641 spe_release_register(p
, tmp2
);
646 #endif /* GALLIUM_CELL */