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.
28 * For details, see /opt/cell/sdk/docs/arch/SPU_ISA_v1.2_27Jan2007_pub.pdf
30 * \author Ian Romanick <idr@us.ibm.com>
34 #ifndef RTASM_PPC_SPE_H
35 #define RTASM_PPC_SPE_H
37 /** 4 bytes per instruction */
38 #define SPE_INST_SIZE 4
40 /** number of general-purpose SIMD registers */
41 #define SPE_NUM_REGS 128
43 /** Return Address register (aka $lr / Link Register) */
46 /** Stack Pointer register (aka $sp) */
52 uint32_t *store
; /**< instruction buffer */
57 * The "set count" reflects the number of nested register sets
58 * are allowed. In the unlikely case that we exceed the set count,
59 * register allocation will start to be confused, which is critical
60 * enough that we check for it.
62 unsigned char set_count
;
65 * Flags for used and unused registers. Each byte corresponds to a
66 * register; a 0 in that byte means that the register is available.
67 * A value of 1 means that the register was allocated in the current
68 * register set. Any other value N means that the register was allocated
69 * N register sets ago.
72 * spe_allocate_register, spe_allocate_available_register,
73 * spe_allocate_register_set, spe_release_register_set, spe_release_register,
75 unsigned char regs
[SPE_NUM_REGS
];
77 boolean print
; /**< print/dump instructions as they're emitted? */
78 int indent
; /**< number of spaces to indent */
82 extern void spe_init_func(struct spe_function
*p
, unsigned code_size
);
83 extern void spe_release_func(struct spe_function
*p
);
84 extern unsigned spe_code_size(const struct spe_function
*p
);
86 extern int spe_allocate_available_register(struct spe_function
*p
);
87 extern int spe_allocate_register(struct spe_function
*p
, int reg
);
88 extern void spe_release_register(struct spe_function
*p
, int reg
);
89 extern void spe_allocate_register_set(struct spe_function
*p
);
90 extern void spe_release_register_set(struct spe_function
*p
);
93 spe_get_registers_used(const struct spe_function
*p
, ubyte used
[]);
95 extern void spe_print_code(struct spe_function
*p
, boolean enable
);
96 extern void spe_indent(struct spe_function
*p
, int spaces
);
97 extern void spe_comment(struct spe_function
*p
, int rel_indent
, const char *s
);
100 #endif /* RTASM_PPC_SPE_H */
103 #define EMIT_(name, _op) \
104 extern void _name (struct spe_function *p, unsigned rT)
105 #define EMIT_R(_name, _op) \
106 extern void _name (struct spe_function *p, unsigned rT, unsigned rA)
107 #define EMIT_RR(_name, _op) \
108 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
110 #define EMIT_RRR(_name, _op) \
111 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
112 unsigned rB, unsigned rC)
113 #define EMIT_RI7(_name, _op) \
114 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
116 #define EMIT_RI8(_name, _op, bias) \
117 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
119 #define EMIT_RI10(_name, _op) \
120 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
122 #define EMIT_RI10s(_name, _op) \
123 extern void _name (struct spe_function *p, unsigned rT, unsigned rA, \
125 #define EMIT_RI16(_name, _op) \
126 extern void _name (struct spe_function *p, unsigned rT, int imm)
127 #define EMIT_RI18(_name, _op) \
128 extern void _name (struct spe_function *p, unsigned rT, int imm)
129 #define EMIT_I16(_name, _op) \
130 extern void _name (struct spe_function *p, int imm)
131 #define UNDEF_EMIT_MACROS
135 /* Memory load / store instructions
137 EMIT_RR (spe_lqx
, 0x1c4);
138 EMIT_RI16(spe_lqa
, 0x061);
139 EMIT_RI16(spe_lqr
, 0x067);
140 EMIT_RR (spe_stqx
, 0x144);
141 EMIT_RI16(spe_stqa
, 0x041);
142 EMIT_RI16(spe_stqr
, 0x047);
143 EMIT_RI7 (spe_cbd
, 0x1f4);
144 EMIT_RR (spe_cbx
, 0x1d4);
145 EMIT_RI7 (spe_chd
, 0x1f5);
146 EMIT_RI7 (spe_chx
, 0x1d5);
147 EMIT_RI7 (spe_cwd
, 0x1f6);
148 EMIT_RI7 (spe_cwx
, 0x1d6);
149 EMIT_RI7 (spe_cdd
, 0x1f7);
150 EMIT_RI7 (spe_cdx
, 0x1d7);
153 /* Constant formation instructions
155 EMIT_RI16(spe_ilh
, 0x083);
156 EMIT_RI16(spe_ilhu
, 0x082);
157 EMIT_RI16(spe_il
, 0x081);
158 EMIT_RI18(spe_ila
, 0x021);
159 EMIT_RI16(spe_iohl
, 0x0c1);
160 EMIT_RI16(spe_fsmbi
, 0x065);
164 /* Integer and logical instructions
166 EMIT_RR (spe_ah
, 0x0c8);
167 EMIT_RI10(spe_ahi
, 0x01d);
168 EMIT_RR (spe_a
, 0x0c0);
169 EMIT_RI10s(spe_ai
, 0x01c);
170 EMIT_RR (spe_sfh
, 0x048);
171 EMIT_RI10(spe_sfhi
, 0x00d);
172 EMIT_RR (spe_sf
, 0x040);
173 EMIT_RI10(spe_sfi
, 0x00c);
174 EMIT_RR (spe_addx
, 0x340);
175 EMIT_RR (spe_cg
, 0x0c2);
176 EMIT_RR (spe_cgx
, 0x342);
177 EMIT_RR (spe_sfx
, 0x341);
178 EMIT_RR (spe_bg
, 0x042);
179 EMIT_RR (spe_bgx
, 0x343);
180 EMIT_RR (spe_mpy
, 0x3c4);
181 EMIT_RR (spe_mpyu
, 0x3cc);
182 EMIT_RI10(spe_mpyi
, 0x074);
183 EMIT_RI10(spe_mpyui
, 0x075);
184 EMIT_RRR (spe_mpya
, 0x00c);
185 EMIT_RR (spe_mpyh
, 0x3c5);
186 EMIT_RR (spe_mpys
, 0x3c7);
187 EMIT_RR (spe_mpyhh
, 0x3c6);
188 EMIT_RR (spe_mpyhha
, 0x346);
189 EMIT_RR (spe_mpyhhu
, 0x3ce);
190 EMIT_RR (spe_mpyhhau
, 0x34e);
191 EMIT_R (spe_clz
, 0x2a5);
192 EMIT_R (spe_cntb
, 0x2b4);
193 EMIT_R (spe_fsmb
, 0x1b6);
194 EMIT_R (spe_fsmh
, 0x1b5);
195 EMIT_R (spe_fsm
, 0x1b4);
196 EMIT_R (spe_gbb
, 0x1b2);
197 EMIT_R (spe_gbh
, 0x1b1);
198 EMIT_R (spe_gb
, 0x1b0);
199 EMIT_RR (spe_avgb
, 0x0d3);
200 EMIT_RR (spe_absdb
, 0x053);
201 EMIT_RR (spe_sumb
, 0x253);
202 EMIT_R (spe_xsbh
, 0x2b6);
203 EMIT_R (spe_xshw
, 0x2ae);
204 EMIT_R (spe_xswd
, 0x2a6);
205 EMIT_RR (spe_and
, 0x0c1);
206 EMIT_RR (spe_andc
, 0x2c1);
207 EMIT_RI10s(spe_andbi
, 0x016);
208 EMIT_RI10s(spe_andhi
, 0x015);
209 EMIT_RI10s(spe_andi
, 0x014);
210 EMIT_RR (spe_or
, 0x041);
211 EMIT_RR (spe_orc
, 0x2c9);
212 EMIT_RI10s(spe_orbi
, 0x006);
213 EMIT_RI10s(spe_orhi
, 0x005);
214 EMIT_RI10s(spe_ori
, 0x004);
215 EMIT_R (spe_orx
, 0x1f0);
216 EMIT_RR (spe_xor
, 0x241);
217 EMIT_RI10s(spe_xorbi
, 0x026);
218 EMIT_RI10s(spe_xorhi
, 0x025);
219 EMIT_RI10s(spe_xori
, 0x024);
220 EMIT_RR (spe_nand
, 0x0c9);
221 EMIT_RR (spe_nor
, 0x049);
222 EMIT_RR (spe_eqv
, 0x249);
223 EMIT_RRR (spe_selb
, 0x008);
224 EMIT_RRR (spe_shufb
, 0x00b);
227 /* Shift and rotate instructions
229 EMIT_RR (spe_shlh
, 0x05f);
230 EMIT_RI7 (spe_shlhi
, 0x07f);
231 EMIT_RR (spe_shl
, 0x05b);
232 EMIT_RI7 (spe_shli
, 0x07b);
233 EMIT_RR (spe_shlqbi
, 0x1db);
234 EMIT_RI7 (spe_shlqbii
, 0x1fb);
235 EMIT_RR (spe_shlqby
, 0x1df);
236 EMIT_RI7 (spe_shlqbyi
, 0x1ff);
237 EMIT_RR (spe_shlqbybi
, 0x1cf);
238 EMIT_RR (spe_roth
, 0x05c);
239 EMIT_RI7 (spe_rothi
, 0x07c);
240 EMIT_RR (spe_rot
, 0x058);
241 EMIT_RI7 (spe_roti
, 0x078);
242 EMIT_RR (spe_rotqby
, 0x1dc);
243 EMIT_RI7 (spe_rotqbyi
, 0x1fc);
244 EMIT_RR (spe_rotqbybi
, 0x1cc);
245 EMIT_RR (spe_rotqbi
, 0x1d8);
246 EMIT_RI7 (spe_rotqbii
, 0x1f8);
247 EMIT_RR (spe_rothm
, 0x05d);
248 EMIT_RI7 (spe_rothmi
, 0x07d);
249 EMIT_RR (spe_rotm
, 0x059);
250 EMIT_RI7 (spe_rotmi
, 0x079);
251 EMIT_RR (spe_rotqmby
, 0x1dd);
252 EMIT_RI7 (spe_rotqmbyi
, 0x1fd);
253 EMIT_RR (spe_rotqmbybi
, 0x1cd);
254 EMIT_RR (spe_rotqmbi
, 0x1c9);
255 EMIT_RI7 (spe_rotqmbii
, 0x1f9);
256 EMIT_RR (spe_rotmah
, 0x05e);
257 EMIT_RI7 (spe_rotmahi
, 0x07e);
258 EMIT_RR (spe_rotma
, 0x05a);
259 EMIT_RI7 (spe_rotmai
, 0x07a);
262 /* Compare, branch, and halt instructions
264 EMIT_RR (spe_heq
, 0x3d8);
265 EMIT_RI10(spe_heqi
, 0x07f);
266 EMIT_RR (spe_hgt
, 0x258);
267 EMIT_RI10(spe_hgti
, 0x04f);
268 EMIT_RR (spe_hlgt
, 0x2d8);
269 EMIT_RI10(spe_hlgti
, 0x05f);
270 EMIT_RR (spe_ceqb
, 0x3d0);
271 EMIT_RI10(spe_ceqbi
, 0x07e);
272 EMIT_RR (spe_ceqh
, 0x3c8);
273 EMIT_RI10(spe_ceqhi
, 0x07d);
274 EMIT_RR (spe_ceq
, 0x3c0);
275 EMIT_RI10(spe_ceqi
, 0x07c);
276 EMIT_RR (spe_cgtb
, 0x250);
277 EMIT_RI10(spe_cgtbi
, 0x04e);
278 EMIT_RR (spe_cgth
, 0x248);
279 EMIT_RI10(spe_cgthi
, 0x04d);
280 EMIT_RR (spe_cgt
, 0x240);
281 EMIT_RI10(spe_cgti
, 0x04c);
282 EMIT_RR (spe_clgtb
, 0x2d0);
283 EMIT_RI10(spe_clgtbi
, 0x05e);
284 EMIT_RR (spe_clgth
, 0x2c8);
285 EMIT_RI10(spe_clgthi
, 0x05d);
286 EMIT_RR (spe_clgt
, 0x2c0);
287 EMIT_RI10(spe_clgti
, 0x05c);
288 EMIT_I16 (spe_br
, 0x064);
289 EMIT_I16 (spe_bra
, 0x060);
290 EMIT_RI16(spe_brsl
, 0x066);
291 EMIT_RI16(spe_brasl
, 0x062);
292 EMIT_RI16(spe_brnz
, 0x042);
293 EMIT_RI16(spe_brz
, 0x040);
294 EMIT_RI16(spe_brhnz
, 0x046);
295 EMIT_RI16(spe_brhz
, 0x044);
298 spe_lqd(struct spe_function
*p
, unsigned rT
, unsigned rA
, int offset
);
301 spe_stqd(struct spe_function
*p
, unsigned rT
, unsigned rA
, int offset
);
303 extern void spe_bi(struct spe_function
*p
, unsigned rA
, int d
, int e
);
304 extern void spe_iret(struct spe_function
*p
, unsigned rA
, int d
, int e
);
305 extern void spe_bisled(struct spe_function
*p
, unsigned rT
, unsigned rA
,
307 extern void spe_bisl(struct spe_function
*p
, unsigned rT
, unsigned rA
,
309 extern void spe_biz(struct spe_function
*p
, unsigned rT
, unsigned rA
,
311 extern void spe_binz(struct spe_function
*p
, unsigned rT
, unsigned rA
,
313 extern void spe_bihz(struct spe_function
*p
, unsigned rT
, unsigned rA
,
315 extern void spe_bihnz(struct spe_function
*p
, unsigned rT
, unsigned rA
,
319 /** Load/splat immediate float into rT. */
321 spe_load_float(struct spe_function
*p
, unsigned rT
, float x
);
323 /** Load/splat immediate int into rT. */
325 spe_load_int(struct spe_function
*p
, unsigned rT
, int i
);
327 /** Load/splat immediate unsigned int into rT. */
329 spe_load_uint(struct spe_function
*p
, unsigned rT
, unsigned int ui
);
331 /** And immediate value into rT. */
333 spe_and_uint(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned int ui
);
335 /** Xor immediate value into rT. */
337 spe_xor_uint(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned int ui
);
339 /** Compare equal with immediate value. */
341 spe_compare_equal_uint(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned int ui
);
343 /** Compare greater with immediate value. */
345 spe_compare_greater_uint(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned int ui
);
347 /** Replicate word 0 of rA across rT. */
349 spe_splat(struct spe_function
*p
, unsigned rT
, unsigned rA
);
351 /** rT = complement_all_bits(rA). */
353 spe_complement(struct spe_function
*p
, unsigned rT
, unsigned rA
);
357 spe_move(struct spe_function
*p
, unsigned rT
, unsigned rA
);
359 /** rT = {0,0,0,0}. */
361 spe_zero(struct spe_function
*p
, unsigned rT
);
363 /** rT = splat(rA, word) */
365 spe_splat_word(struct spe_function
*p
, unsigned rT
, unsigned rA
, int word
);
367 /** rT = float min(rA, rB) */
369 spe_float_min(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned rB
);
371 /** rT = float max(rA, rB) */
373 spe_float_max(struct spe_function
*p
, unsigned rT
, unsigned rA
, unsigned rB
);
376 /* Floating-point instructions
378 EMIT_RR (spe_fa
, 0x2c4);
379 EMIT_RR (spe_dfa
, 0x2cc);
380 EMIT_RR (spe_fs
, 0x2c5);
381 EMIT_RR (spe_dfs
, 0x2cd);
382 EMIT_RR (spe_fm
, 0x2c6);
383 EMIT_RR (spe_dfm
, 0x2ce);
384 EMIT_RRR (spe_fma
, 0x00e);
385 EMIT_RR (spe_dfma
, 0x35c);
386 EMIT_RRR (spe_fnms
, 0x00d);
387 EMIT_RR (spe_dfnms
, 0x35e);
388 EMIT_RRR (spe_fms
, 0x00f);
389 EMIT_RR (spe_dfms
, 0x35d);
390 EMIT_RR (spe_dfnma
, 0x35f);
391 EMIT_R (spe_frest
, 0x1b8);
392 EMIT_R (spe_frsqest
, 0x1b9);
393 EMIT_RR (spe_fi
, 0x3d4);
394 EMIT_RI8 (spe_csflt
, 0x1da, 155);
395 EMIT_RI8 (spe_cflts
, 0x1d8, 173);
396 EMIT_RI8 (spe_cuflt
, 0x1db, 155);
397 EMIT_RI8 (spe_cfltu
, 0x1d9, 173);
398 EMIT_R (spe_frds
, 0x3b9);
399 EMIT_R (spe_fesd
, 0x3b8);
400 EMIT_RR (spe_dfceq
, 0x3c3);
401 EMIT_RR (spe_dfcmeq
, 0x3cb);
402 EMIT_RR (spe_dfcgt
, 0x2c3);
403 EMIT_RR (spe_dfcmgt
, 0x2cb);
404 EMIT_RI7 (spe_dftsv
, 0x3bf);
405 EMIT_RR (spe_fceq
, 0x3c2);
406 EMIT_RR (spe_fcmeq
, 0x3ca);
407 EMIT_RR (spe_fcgt
, 0x2c2);
408 EMIT_RR (spe_fcmgt
, 0x2ca);
409 EMIT_R (spe_fscrwr
, 0x3ba);
410 EMIT_ (spe_fscrrd
, 0x398);
413 /* Channel instructions
415 EMIT_R (spe_rdch
, 0x00d);
416 EMIT_R (spe_rdchcnt
, 0x00f);
417 EMIT_R (spe_wrch
, 0x10d);
420 #ifdef UNDEF_EMIT_MACROS
432 #undef UNDEF_EMIT_MACROS