4a94ed0460d6be4d2f26017e06e2e37fc72b051c
1 /**************************************************************************
3 * Copyright (C) 2008 Tungsten Graphics, Inc. All Rights Reserved.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 **************************************************************************/
25 * PPC code generation.
26 * For reference, see http://www.power.org/resources/reading/PowerISA_V2.05.pdf
27 * ABI info: http://www.cs.utsa.edu/~whaley/teach/cs6463FHPO/LEC/lec12_ho.pdf
30 * http://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF778525699600719DF2
31 * http://www.ibm.com/developerworks/eserver/library/es-archguide-v2.html
32 * http://www.freescale.com/files/product/doc/MPCFPE32B.pdf
39 #include "util/u_memory.h"
40 #include "pipe/p_debug.h"
41 #include "rtasm_ppc.h"
45 ppc_init_func(struct ppc_function
*p
, unsigned max_inst
)
49 p
->store
= align_malloc(max_inst
* PPC_INST_SIZE
, 16);
51 p
->max_inst
= max_inst
;
55 /* only allow using gp registers 7..12 for now */
57 for (i
= 7; i
< 13; i
++)
58 p
->reg_used
|= (1 << i
);
63 ppc_release_func(struct ppc_function
*p
)
65 assert(p
->num_inst
<= p
->max_inst
);
66 if (p
->store
!= NULL
) {
73 void (*ppc_get_func(struct ppc_function
*p
))(void)
77 if (DISASSEM
&& p
->store
)
78 debug_printf("disassemble %p %p\n", p
->store
, p
->csr
);
80 if (p
->store
== p
->error_overflow
)
81 return (void (*)(void)) NULL
;
84 return (void (*)(void)) p
->store
;
89 ppc_dump_func(const struct ppc_function
*p
)
92 for (i
= 0; i
< p
->num_inst
; i
++) {
93 debug_printf("%3u: 0x%08x\n", i
, p
->store
[i
]);
99 * Allocate a general purpose register.
100 * \return register index or -1 if none left.
103 ppc_allocate_register(struct ppc_function
*p
)
106 for (i
= 0; i
< PPC_NUM_REGS
; i
++) {
107 const uint64_t mask
= 1 << i
;
108 if ((p
->reg_used
& mask
) != 0) {
109 p
->reg_used
&= ~mask
;
118 * Mark the given general purpose register as "unallocated".
121 ppc_release_register(struct ppc_function
*p
, int reg
)
123 assert(reg
< PPC_NUM_REGS
);
124 assert((p
->reg_used
& (1 << reg
)) == 0);
125 p
->reg_used
|= (1 << reg
);
130 * Allocate a floating point register.
131 * \return register index or -1 if none left.
134 ppc_allocate_fp_register(struct ppc_function
*p
)
137 for (i
= 0; i
< PPC_NUM_FP_REGS
; i
++) {
138 const uint64_t mask
= 1 << i
;
139 if ((p
->fp_used
& mask
) != 0) {
149 * Mark the given floating point register as "unallocated".
152 ppc_release_fp_register(struct ppc_function
*p
, int reg
)
154 assert(reg
< PPC_NUM_FP_REGS
);
155 assert((p
->fp_used
& (1 << reg
)) == 0);
156 p
->fp_used
|= (1 << reg
);
161 * Allocate a vector register.
162 * \return register index or -1 if none left.
165 ppc_allocate_vec_register(struct ppc_function
*p
)
168 for (i
= 0; i
< PPC_NUM_VEC_REGS
; i
++) {
169 const uint64_t mask
= 1 << i
;
170 if ((p
->vec_used
& mask
) != 0) {
171 p
->vec_used
&= ~mask
;
180 * Mark the given vector register as "unallocated".
183 ppc_release_vec_register(struct ppc_function
*p
, int reg
)
185 assert(reg
< PPC_NUM_VEC_REGS
);
186 assert((p
->vec_used
& (1 << reg
)) == 0);
187 p
->vec_used
|= (1 << reg
);
204 emit_vx(struct ppc_function
*p
, uint op2
, uint vD
, uint vA
, uint vB
)
212 p
->store
[p
->num_inst
++] = inst
.bits
;
213 assert(p
->num_inst
<= p
->max_inst
);
230 emit_vxr(struct ppc_function
*p
, uint op2
, uint vD
, uint vA
, uint vB
)
239 p
->store
[p
->num_inst
++] = inst
.bits
;
240 assert(p
->num_inst
<= p
->max_inst
);
257 emit_va(struct ppc_function
*p
, uint op2
, uint vD
, uint vA
, uint vB
, uint vC
)
266 p
->store
[p
->num_inst
++] = inst
.bits
;
267 assert(p
->num_inst
<= p
->max_inst
);
282 emit_i(struct ppc_function
*p
, uint op
, uint li
, uint aa
, uint lk
)
289 p
->store
[p
->num_inst
++] = inst
.bits
;
290 assert(p
->num_inst
<= p
->max_inst
);
308 emit_xl(struct ppc_function
*p
, uint op
, uint bo
, uint bi
, uint bh
,
315 inst
.inst
.unused
= 0x0;
319 p
->store
[p
->num_inst
++] = inst
.bits
;
320 assert(p
->num_inst
<= p
->max_inst
);
324 dump_xl(const char *name
, uint inst
)
329 debug_printf("%s = 0x%08x\n", name
, inst
);
330 debug_printf(" op: %d 0x%x\n", i
.inst
.op
, i
.inst
.op
);
331 debug_printf(" bo: %d 0x%x\n", i
.inst
.bo
, i
.inst
.bo
);
332 debug_printf(" bi: %d 0x%x\n", i
.inst
.bi
, i
.inst
.bi
);
333 debug_printf(" unused: %d 0x%x\n", i
.inst
.unused
, i
.inst
.unused
);
334 debug_printf(" bh: %d 0x%x\n", i
.inst
.bh
, i
.inst
.bh
);
335 debug_printf(" op2: %d 0x%x\n", i
.inst
.op2
, i
.inst
.op2
);
336 debug_printf(" lk: %d 0x%x\n", i
.inst
.lk
, i
.inst
.lk
);
353 emit_x(struct ppc_function
*p
, uint op
, uint vrs
, uint ra
, uint rb
, uint op2
)
361 inst
.inst
.unused
= 0x0;
362 p
->store
[p
->num_inst
++] = inst
.bits
;
363 assert(p
->num_inst
<= p
->max_inst
);
378 emit_d(struct ppc_function
*p
, uint op
, uint rt
, uint ra
, int si
)
381 assert(si
>= -32768);
386 inst
.inst
.si
= (unsigned) (si
& 0xffff);
387 p
->store
[p
->num_inst
++] = inst
.bits
;
388 assert(p
->num_inst
<= p
->max_inst
);
406 emit_a(struct ppc_function
*p
, uint op
, uint frt
, uint fra
, uint frb
, uint op2
,
414 inst
.inst
.unused
= 0x0;
417 p
->store
[p
->num_inst
++] = inst
.bits
;
418 assert(p
->num_inst
<= p
->max_inst
);
436 emit_xo(struct ppc_function
*p
, uint op
, uint rt
, uint ra
, uint rb
, uint oe
,
447 p
->store
[p
->num_inst
++] = inst
.bits
;
448 assert(p
->num_inst
<= p
->max_inst
);
456 ** float vector arithmetic
459 /** vector float add */
461 ppc_vaddfp(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
463 emit_vx(p
, 10, vD
, vA
, vB
);
466 /** vector float substract */
468 ppc_vsubfp(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
470 emit_vx(p
, 74, vD
, vA
, vB
);
473 /** vector float min */
475 ppc_vminfp(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
477 emit_vx(p
, 1098, vD
, vA
, vB
);
480 /** vector float max */
482 ppc_vmaxfp(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
484 emit_vx(p
, 1034, vD
, vA
, vB
);
487 /** vector float mult add: vD = vA * vB + vC */
489 ppc_vmaddfp(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
, uint vC
)
491 emit_va(p
, 46, vD
, vA
, vC
, vB
); /* note arg order */
494 /** vector float compare greater than */
496 ppc_vcmpgtfpx(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
498 emit_vxr(p
, 710, vD
, vA
, vB
);
501 /** vector float compare greater than or equal to */
503 ppc_vcmpgefpx(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
505 emit_vxr(p
, 454, vD
, vA
, vB
);
508 /** vector float compare equal */
510 ppc_vcmpeqfpx(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
512 emit_vxr(p
, 198, vD
, vA
, vB
);
515 /** vector float 2^x */
517 ppc_vexptefp(struct ppc_function
*p
, uint vD
, uint vB
)
519 emit_vx(p
, 394, vD
, 0, vB
);
522 /** vector float log2(x) */
524 ppc_vlogefp(struct ppc_function
*p
, uint vD
, uint vB
)
526 emit_vx(p
, 458, vD
, 0, vB
);
529 /** vector float reciprocol */
531 ppc_vrefp(struct ppc_function
*p
, uint vD
, uint vB
)
533 emit_vx(p
, 266, vD
, 0, vB
);
536 /** vector float reciprocol sqrt estimate */
538 ppc_vrsqrtefp(struct ppc_function
*p
, uint vD
, uint vB
)
540 emit_vx(p
, 330, vD
, 0, vB
);
543 /** vector float round to negative infinity */
545 ppc_vrfim(struct ppc_function
*p
, uint vD
, uint vB
)
547 emit_vx(p
, 714, vD
, 0, vB
);
550 /** vector float round to positive infinity */
552 ppc_vrfip(struct ppc_function
*p
, uint vD
, uint vB
)
554 emit_vx(p
, 650, vD
, 0, vB
);
557 /** vector float round to nearest int */
559 ppc_vrfin(struct ppc_function
*p
, uint vD
, uint vB
)
561 emit_vx(p
, 522, vD
, 0, vB
);
564 /** vector float round to int toward zero */
566 ppc_vrfiz(struct ppc_function
*p
, uint vD
, uint vB
)
568 emit_vx(p
, 586, vD
, 0, vB
);
571 /** vector store: store vR at mem[vA+vB] */
573 ppc_stvx(struct ppc_function
*p
, uint vR
, uint vA
, uint vB
)
575 emit_x(p
, 31, vR
, vA
, vB
, 231);
578 /** vector load: vR = mem[vA+vB] */
580 ppc_lvx(struct ppc_function
*p
, uint vR
, uint vA
, uint vB
)
582 emit_x(p
, 31, vR
, vA
, vB
, 103);
588 ** vector bitwise operations
593 ppc_vand(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
595 emit_vx(p
, 1028, vD
, vA
, vB
);
598 /** vector and complement */
600 ppc_vandc(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
602 emit_vx(p
, 1092, vD
, vA
, vB
);
607 ppc_vor(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
609 emit_vx(p
, 1156, vD
, vA
, vB
);
614 ppc_vnor(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
616 emit_vx(p
, 1284, vD
, vA
, vB
);
621 ppc_vxor(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
623 emit_vx(p
, 1220, vD
, vA
, vB
);
626 /** Pseudo-instruction: vector move */
628 ppc_vecmove(struct ppc_function
*p
, uint vD
, uint vA
)
630 ppc_vor(p
, vD
, vA
, vA
);
636 ** Vector shuffle / select / splat / etc
639 /** vector permute */
641 ppc_vperm(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
, uint vC
)
643 emit_va(p
, 43, vD
, vA
, vB
, vC
);
648 ppc_vsel(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
, uint vC
)
650 emit_va(p
, 42, vD
, vA
, vB
, vC
);
653 /** vector splat byte */
655 ppc_vspltb(struct ppc_function
*p
, uint vD
, uint vB
, uint imm
)
657 emit_vx(p
, 42, vD
, imm
, vB
);
660 /** vector splat half word */
662 ppc_vsplthw(struct ppc_function
*p
, uint vD
, uint vB
, uint imm
)
664 emit_vx(p
, 588, vD
, imm
, vB
);
667 /** vector splat word */
669 ppc_vspltw(struct ppc_function
*p
, uint vD
, uint vB
, uint imm
)
671 emit_vx(p
, 652, vD
, imm
, vB
);
674 /** vector splat signed immediate word */
676 ppc_vspltisw(struct ppc_function
*p
, uint vD
, int imm
)
680 emit_vx(p
, 908, vD
, imm
, 0);
683 /** vector shift left word: vD[word] = vA[word] << (vB[word] & 0x1f) */
685 ppc_vslw(struct ppc_function
*p
, uint vD
, uint vA
, uint vB
)
687 emit_vx(p
, 388, vD
, vA
, vB
);
694 ** integer arithmetic
699 ppc_addi(struct ppc_function
*p
, uint rt
, uint ra
, int imm
)
701 emit_d(p
, 14, rt
, ra
, imm
);
704 /** rt = ra + (imm << 16) */
706 ppc_addis(struct ppc_function
*p
, uint rt
, uint ra
, int imm
)
708 emit_d(p
, 15, rt
, ra
, imm
);
713 ppc_add(struct ppc_function
*p
, uint rt
, uint ra
, uint rb
)
715 emit_xo(p
, 31, rt
, ra
, rb
, 0, 266, 0);
718 /** rt = ra AND ra */
720 ppc_and(struct ppc_function
*p
, uint rt
, uint ra
, uint rb
)
722 emit_x(p
, 31, ra
, rt
, rb
, 28); /* note argument order */
725 /** rt = ra AND imm */
727 ppc_andi(struct ppc_function
*p
, uint rt
, uint ra
, int imm
)
729 emit_d(p
, 28, ra
, rt
, imm
); /* note argument order */
734 ppc_or(struct ppc_function
*p
, uint rt
, uint ra
, uint rb
)
736 emit_x(p
, 31, ra
, rt
, rb
, 444); /* note argument order */
739 /** rt = ra OR imm */
741 ppc_ori(struct ppc_function
*p
, uint rt
, uint ra
, int imm
)
743 emit_d(p
, 24, ra
, rt
, imm
); /* note argument order */
746 /** rt = ra XOR ra */
748 ppc_xor(struct ppc_function
*p
, uint rt
, uint ra
, uint rb
)
750 emit_x(p
, 31, ra
, rt
, rb
, 316); /* note argument order */
753 /** rt = ra XOR imm */
755 ppc_xori(struct ppc_function
*p
, uint rt
, uint ra
, int imm
)
757 emit_d(p
, 26, ra
, rt
, imm
); /* note argument order */
760 /** pseudo instruction: move: rt = ra */
762 ppc_mr(struct ppc_function
*p
, uint rt
, uint ra
)
764 ppc_or(p
, rt
, ra
, ra
);
767 /** pseudo instruction: load immediate: rt = imm */
769 ppc_li(struct ppc_function
*p
, uint rt
, int imm
)
771 ppc_addi(p
, rt
, 0, imm
);
774 /** rt = imm << 16 */
776 ppc_lis(struct ppc_function
*p
, uint rt
, int imm
)
778 ppc_addis(p
, rt
, 0, imm
);
783 ppc_load_int(struct ppc_function
*p
, uint rt
, int imm
)
785 ppc_lis(p
, rt
, (imm
>> 16)); /* rt = imm >> 16 */
786 ppc_ori(p
, rt
, rt
, (imm
& 0xffff)); /* rt = rt | (imm & 0xffff) */
793 ** integer load/store
796 /** store rs at memory[(ra)+d],
797 * then update ra = (ra)+d
800 ppc_stwu(struct ppc_function
*p
, uint rs
, uint ra
, int d
)
802 emit_d(p
, 37, rs
, ra
, d
);
805 /** store rs at memory[(ra)+d] */
807 ppc_stw(struct ppc_function
*p
, uint rs
, uint ra
, int d
)
809 emit_d(p
, 36, rs
, ra
, d
);
812 /** Load rt = mem[(ra)+d]; then zero set high 32 bits to zero. */
814 ppc_lwz(struct ppc_function
*p
, uint rt
, uint ra
, int d
)
816 emit_d(p
, 32, rt
, ra
, d
);
822 ** Float (non-vector) arithmetic
825 /** add: frt = fra + frb */
827 ppc_fadd(struct ppc_function
*p
, uint frt
, uint fra
, uint frb
)
829 emit_a(p
, 63, frt
, fra
, frb
, 21, 0);
832 /** sub: frt = fra - frb */
834 ppc_fsub(struct ppc_function
*p
, uint frt
, uint fra
, uint frb
)
836 emit_a(p
, 63, frt
, fra
, frb
, 20, 0);
839 /** convert to int: rt = (int) ra */
841 ppc_fctiwz(struct ppc_function
*p
, uint rt
, uint fra
)
843 emit_x(p
, 63, rt
, 0, fra
, 15);
846 /** store frs at mem[(ra)+offset] */
848 ppc_stfs(struct ppc_function
*p
, uint frs
, uint ra
, int offset
)
850 emit_d(p
, 52, frs
, ra
, offset
);
853 /** store frs at mem[(ra)+(rb)] */
855 ppc_stfiwx(struct ppc_function
*p
, uint frs
, uint ra
, uint rb
)
857 emit_x(p
, 31, frs
, ra
, rb
, 983);
860 /** load frt = mem[(ra)+offset] */
862 ppc_lfs(struct ppc_function
*p
, uint frt
, uint ra
, int offset
)
864 emit_d(p
, 48, frt
, ra
, offset
);
872 ** branch instructions
875 /** BLR: Branch to link register (p. 35) */
877 ppc_blr(struct ppc_function
*p
)
879 emit_i(p
, 18, 0, 0, 1);
882 /** Branch Conditional to Link Register (p. 36) */
884 ppc_bclr(struct ppc_function
*p
, uint condOp
, uint branchHint
, uint condReg
)
886 emit_xl(p
, 19, condOp
, condReg
, branchHint
, 16, 0);
889 /** Pseudo instruction: return from subroutine */
891 ppc_return(struct ppc_function
*p
)
893 ppc_bclr(p
, BRANCH_COND_ALWAYS
, BRANCH_HINT_SUB_RETURN
, 0);