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
;
154 p
->store
[p
->num_inst
++] = inst
.bits
;
155 assert(p
->num_inst
<= p
->max_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
;
168 p
->store
[p
->num_inst
++] = inst
.bits
;
169 assert(p
->num_inst
<= p
->max_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
;
181 p
->store
[p
->num_inst
++] = inst
.bits
;
182 assert(p
->num_inst
<= p
->max_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
;
195 p
->store
[p
->num_inst
++] = inst
.bits
;
196 assert(p
->num_inst
<= p
->max_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
;
209 p
->store
[p
->num_inst
++] = inst
.bits
;
210 assert(p
->num_inst
<= p
->max_inst
);
214 static void emit_RI16(struct spe_function
*p
, unsigned op
, unsigned rT
,
217 union spe_inst_RI16 inst
;
221 p
->store
[p
->num_inst
++] = inst
.bits
;
222 assert(p
->num_inst
<= p
->max_inst
);
226 static void emit_RI18(struct spe_function
*p
, unsigned op
, unsigned rT
,
229 union spe_inst_RI18 inst
;
233 p
->store
[p
->num_inst
++] = inst
.bits
;
234 assert(p
->num_inst
<= p
->max_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);
311 p
->max_inst
= code_size
/ SPE_INST_SIZE
;
313 /* Conservatively treat R0 - R2 and R80 - R127 as non-volatile.
316 p
->regs
[1] = (1U << (80 - 64)) - 1;
320 void spe_release_func(struct spe_function
*p
)
322 assert(p
->num_inst
<= p
->max_inst
);
323 if (p
->store
!= NULL
) {
324 align_free(p
->store
);
331 * Alloate a SPE register.
332 * \return register index or -1 if none left.
334 int spe_allocate_available_register(struct spe_function
*p
)
337 for (i
= 0; i
< SPE_NUM_REGS
; i
++) {
338 const uint64_t mask
= (1ULL << (i
% 64));
339 const unsigned idx
= i
/ 64;
342 if ((p
->regs
[idx
] & mask
) != 0) {
343 p
->regs
[idx
] &= ~mask
;
353 * Mark the given SPE register as "allocated".
355 int spe_allocate_register(struct spe_function
*p
, int reg
)
357 const unsigned idx
= reg
/ 64;
358 const unsigned bit
= reg
% 64;
360 assert(reg
< SPE_NUM_REGS
);
361 assert((p
->regs
[idx
] & (1ULL << bit
)) != 0);
363 p
->regs
[idx
] &= ~(1ULL << bit
);
369 * Mark the given SPE register as "unallocated".
371 void spe_release_register(struct spe_function
*p
, int reg
)
373 const unsigned idx
= reg
/ 64;
374 const unsigned bit
= reg
% 64;
378 assert(reg
< SPE_NUM_REGS
);
379 assert((p
->regs
[idx
] & (1ULL << bit
)) == 0);
381 p
->regs
[idx
] |= (1ULL << bit
);
386 * For branch instructions:
387 * \param d if 1, disable interupts if branch is taken
388 * \param e if 1, enable interupts if branch is taken
389 * If d and e are both zero, don't change interupt status (right?)
392 /** Branch Indirect to address in rA */
393 void spe_bi(struct spe_function
*p
, unsigned rA
, int d
, int e
)
395 emit_RI7(p
, 0x1a8, 0, rA
, (d
<< 5) | (e
<< 4));
398 /** Interupt Return */
399 void spe_iret(struct spe_function
*p
, unsigned rA
, int d
, int e
)
401 emit_RI7(p
, 0x1aa, 0, rA
, (d
<< 5) | (e
<< 4));
404 /** Branch indirect and set link on external data */
405 void spe_bisled(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
408 emit_RI7(p
, 0x1ab, rT
, rA
, (d
<< 5) | (e
<< 4));
411 /** Branch indirect and set link. Save PC in rT, jump to rA. */
412 void spe_bisl(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
,
415 emit_RI7(p
, 0x1a9, rT
, rA
, (d
<< 5) | (e
<< 4));
418 /** Branch indirect if zero word. If rT.word[0]==0, jump to rA. */
419 void spe_biz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
421 emit_RI7(p
, 0x128, rT
, rA
, (d
<< 5) | (e
<< 4));
424 /** Branch indirect if non-zero word. If rT.word[0]!=0, jump to rA. */
425 void spe_binz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
427 emit_RI7(p
, 0x129, rT
, rA
, (d
<< 5) | (e
<< 4));
430 /** Branch indirect if zero halfword. If rT.halfword[1]==0, jump to rA. */
431 void spe_bihz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
433 emit_RI7(p
, 0x12a, rT
, rA
, (d
<< 5) | (e
<< 4));
436 /** Branch indirect if non-zero halfword. If rT.halfword[1]!=0, jump to rA. */
437 void spe_bihnz(struct spe_function
*p
, unsigned rT
, unsigned rA
, int d
, int e
)
439 emit_RI7(p
, 0x12b, rT
, rA
, (d
<< 5) | (e
<< 4));
443 /* Hint-for-branch instructions
452 /* Control instructions
456 EMIT_RR (spe_stopd
, 0x140);
457 EMIT_ (spe_lnop
, 0x001);
458 EMIT_ (spe_nop
, 0x201);
460 EMIT_ (spe_dsync
, 0x003);
461 EMIT_R (spe_mfspr
, 0x00c);
462 EMIT_R (spe_mtspr
, 0x10c);
467 ** Helper / "macro" instructions.
468 ** Use somewhat verbose names as a reminder that these aren't native
474 spe_load_float(struct spe_function
*p
, unsigned rT
, float x
)
481 spe_ilhu(p
, rT
, bits
.u
>> 16);
482 spe_iohl(p
, rT
, bits
.u
& 0xffff);
487 spe_load_int(struct spe_function
*p
, unsigned rT
, int i
)
489 spe_ilhu(p
, rT
, i
>> 16);
490 spe_iohl(p
, rT
, i
& 0xffff);
495 spe_complement(struct spe_function
*p
, unsigned rT
)
497 spe_nor(p
, rT
, rT
, rT
);
502 spe_move(struct spe_function
*p
, unsigned rT
, unsigned rA
)
504 spe_ori(p
, rT
, rA
, 0);
509 spe_zero(struct spe_function
*p
, unsigned rT
)
511 spe_xor(p
, rT
, rT
, rT
);
515 #endif /* GALLIUM_CELL */