summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/cell/ppu/cell_gen_fp.c
blob: 58a8b5d0b0f6bea44dbd667f80c3fde6e8342ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
/**************************************************************************
 * 
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * Copyright 2009 VMware, Inc.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/



/**
 * Generate SPU fragment program/shader code.
 *
 * Note that we generate SOA-style code here.  So each TGSI instruction
 * operates on four pixels (and is translated into four SPU instructions,
 * generally speaking).
 *
 * \author Brian Paul
 */

#include <math.h>
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_shader_tokens.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
#include "tgsi/tgsi_exec.h"
#include "tgsi/tgsi_dump.h"
#include "rtasm/rtasm_ppc_spe.h"
#include "util/u_memory.h"
#include "cell_context.h"
#include "cell_gen_fp.h"


#define MAX_TEMPS 16
#define MAX_IMMED  8

#define CHAN_X  0
#define CHAN_Y  1
#define CHAN_Z  2
#define CHAN_W  3

/**
 * Context needed during code generation.
 */
struct codegen
{
   struct cell_context *cell;
   int inputs_reg;      /**< 1st function parameter */
   int outputs_reg;     /**< 2nd function parameter */
   int constants_reg;   /**< 3rd function parameter */
   int temp_regs[MAX_TEMPS][4]; /**< maps TGSI temps to SPE registers */
   int imm_regs[MAX_IMMED][4];  /**< maps TGSI immediates to SPE registers */

   int num_imm;  /**< number of immediates */

   int one_reg;         /**< register containing {1.0, 1.0, 1.0, 1.0} */

   int addr_reg;        /**< address register, integer values */

   /** Per-instruction temps / intermediate temps */
   int num_itemps;
   int itemps[12];

   /** Current IF/ELSE/ENDIF nesting level */
   int if_nesting;
   /** Current BGNLOOP/ENDLOOP nesting level */
   int loop_nesting;
   /** Location of start of current loop */
   int loop_start;

   /** Index of if/conditional mask register */
   int cond_mask_reg;
   /** Index of loop mask register */
   int loop_mask_reg;

   /** Index of master execution mask register */
   int exec_mask_reg;

   /** KIL mask: indicates which fragments have been killed */
   int kill_mask_reg;

   int frame_size;  /**< Stack frame size, in words */

   struct spe_function *f;
   boolean error;
};


/**
 * Allocate an intermediate temporary register.
 */
static int
get_itemp(struct codegen *gen)
{
   int t = spe_allocate_available_register(gen->f);
   assert(gen->num_itemps < Elements(gen->itemps));
   gen->itemps[gen->num_itemps++] = t;
   return t;
}

/**
 * Free all intermediate temporary registers.  To be called after each
 * instruction has been emitted.
 */
static void
free_itemps(struct codegen *gen)
{
   int i;
   for (i = 0; i < gen->num_itemps; i++) {
      spe_release_register(gen->f, gen->itemps[i]);
   }
   gen->num_itemps = 0;
}


/**
 * Return index of an SPE register containing {1.0, 1.0, 1.0, 1.0}.
 * The register is allocated and initialized upon the first call.
 */
static int
get_const_one_reg(struct codegen *gen)
{
   if (gen->one_reg <= 0) {
      gen->one_reg = spe_allocate_available_register(gen->f);

      spe_indent(gen->f, 4);
      spe_comment(gen->f, -4, "init constant reg = 1.0:");

      /* one = {1.0, 1.0, 1.0, 1.0} */
      spe_load_float(gen->f, gen->one_reg, 1.0f);

      spe_indent(gen->f, -4);
   }

   return gen->one_reg;
}


/**
 * Return index of the address register.
 * Used for indirect register loads/stores.
 */
static int
get_address_reg(struct codegen *gen)
{
   if (gen->addr_reg <= 0) {
      gen->addr_reg = spe_allocate_available_register(gen->f);

      spe_indent(gen->f, 4);
      spe_comment(gen->f, -4, "init address reg = 0:");

      /* init addr = {0, 0, 0, 0} */
      spe_zero(gen->f, gen->addr_reg);

      spe_indent(gen->f, -4);
   }

   return gen->addr_reg;
}


/**
 * Return index of the master execution mask.
 * The register is allocated an initialized upon the first call.
 *
 * The master execution mask controls which pixels in a quad are
 * modified, according to surrounding conditionals, loops, etc.
 */
static int
get_exec_mask_reg(struct codegen *gen)
{
   if (gen->exec_mask_reg <= 0) {
      gen->exec_mask_reg = spe_allocate_available_register(gen->f);

      /* XXX this may not be needed */
      spe_comment(gen->f, 0*-4, "initialize master execution mask = ~0");
      spe_load_int(gen->f, gen->exec_mask_reg, ~0);
   }

   return gen->exec_mask_reg;
}


/** Return index of the conditional (if/else) execution mask register */
static int
get_cond_mask_reg(struct codegen *gen)
{
   if (gen->cond_mask_reg <= 0) {
      gen->cond_mask_reg = spe_allocate_available_register(gen->f);
   }

   return gen->cond_mask_reg;
}


/** Return index of the loop execution mask register */
static int
get_loop_mask_reg(struct codegen *gen)
{
   if (gen->loop_mask_reg <= 0) {
      gen->loop_mask_reg = spe_allocate_available_register(gen->f);
   }

   return gen->loop_mask_reg;
}



static boolean
is_register_src(struct codegen *gen, int channel,
                const struct tgsi_full_src_register *src)
{
   int swizzle = tgsi_util_get_full_src_register_extswizzle(src, channel);
   int sign_op = tgsi_util_get_full_src_register_sign_mode(src, channel);

   if (swizzle > TGSI_SWIZZLE_W || sign_op != TGSI_UTIL_SIGN_KEEP) {
      return FALSE;
   }
   if (src->SrcRegister.File == TGSI_FILE_TEMPORARY ||
       src->SrcRegister.File == TGSI_FILE_IMMEDIATE) {
      return TRUE;
   }
   return FALSE;
}

  
static boolean
is_memory_dst(struct codegen *gen, int channel,
              const struct tgsi_full_dst_register *dst)
{
   if (dst->DstRegister.File == TGSI_FILE_OUTPUT) {
      return TRUE;
   }
   else {
      return FALSE;
   }
}

  
/**
 * Return the index of the SPU temporary containing the named TGSI
 * source register.  If the TGSI register is a TGSI_FILE_TEMPORARY we
 * just return the corresponding SPE register.  If the TGIS register
 * is TGSI_FILE_INPUT/CONSTANT/IMMEDIATE we allocate a new SPE register
 * and emit an SPE load instruction.
 */
static int
get_src_reg(struct codegen *gen,
            int channel,
            const struct tgsi_full_src_register *src)
{
   int reg = -1;
   int swizzle = tgsi_util_get_full_src_register_extswizzle(src, channel);
   boolean reg_is_itemp = FALSE;
   uint sign_op;

   assert(swizzle >= TGSI_SWIZZLE_X);
   assert(swizzle <= TGSI_EXTSWIZZLE_ONE);

   if (swizzle == TGSI_EXTSWIZZLE_ONE) {
      /* Load const one float and early out */
      reg = get_const_one_reg(gen);
   }
   else if (swizzle == TGSI_EXTSWIZZLE_ZERO) {
      /* Load const zero float and early out */
      reg = get_itemp(gen);
      spe_xor(gen->f, reg, reg, reg);
   }
   else {
      int index = src->SrcRegister.Index;

      assert(swizzle < 4);

      if (src->SrcRegister.Indirect) {
         /* XXX unfinished */
      }

      switch (src->SrcRegister.File) {
      case TGSI_FILE_TEMPORARY:
         reg = gen->temp_regs[index][swizzle];
         break;
      case TGSI_FILE_INPUT:
         {
            /* offset is measured in quadwords, not bytes */
            int offset = index * 4 + swizzle;
            reg = get_itemp(gen);
            reg_is_itemp = TRUE;
            /* Load:  reg = memory[(machine_reg) + offset] */
            spe_lqd(gen->f, reg, gen->inputs_reg, offset * 16);
         }
         break;
      case TGSI_FILE_IMMEDIATE:
         reg = gen->imm_regs[index][swizzle];
         break;
      case TGSI_FILE_CONSTANT:
         {
            /* offset is measured in quadwords, not bytes */
            int offset = index * 4 + swizzle;
            reg = get_itemp(gen);
            reg_is_itemp = TRUE;
            /* Load:  reg = memory[(machine_reg) + offset] */
            spe_lqd(gen->f, reg, gen->constants_reg, offset * 16);
         }
         break;
      default:
         assert(0);
      }
   }

   /*
    * Handle absolute value, negate or set-negative of src register.
    */
   sign_op = tgsi_util_get_full_src_register_sign_mode(src, channel);
   if (sign_op != TGSI_UTIL_SIGN_KEEP) {
      /*
       * All sign ops are done by manipulating bit 31, the IEEE float sign bit.
       */
      const int bit31mask_reg = get_itemp(gen);
      int result_reg;

      if (reg_is_itemp) {
         /* re-use 'reg' for the result */
         result_reg = reg;
      }
      else {
         /* alloc a new reg for the result */
         result_reg = get_itemp(gen);
      }

      /* mask with bit 31 set, the rest cleared */
      spe_load_uint(gen->f, bit31mask_reg, (1 << 31));

      if (sign_op == TGSI_UTIL_SIGN_CLEAR) {
         spe_andc(gen->f, result_reg, reg, bit31mask_reg);
      }
      else if (sign_op == TGSI_UTIL_SIGN_SET) {
         spe_and(gen->f, result_reg, reg, bit31mask_reg);
      }
      else {
         assert(sign_op == TGSI_UTIL_SIGN_TOGGLE);
         spe_xor(gen->f, result_reg, reg, bit31mask_reg);
      }

      reg = result_reg;
   }

   return reg;
}


/**
 * Return the index of an SPE register to use for the given TGSI register.
 * If the TGSI register is TGSI_FILE_TEMPORARAY, the index of the
 * corresponding SPE register is returned.  If the TGSI register is
 * TGSI_FILE_OUTPUT we allocate an intermediate temporary register.
 * See store_dest_reg() below...
 */
static int
get_dst_reg(struct codegen *gen,
            int channel,
            const struct tgsi_full_dst_register *dest)
{
   int reg = -1;

   switch (dest->DstRegister.File) {
   case TGSI_FILE_TEMPORARY:
      if (gen->if_nesting > 0 || gen->loop_nesting > 0)
         reg = get_itemp(gen);
      else
         reg = gen->temp_regs[dest->DstRegister.Index][channel];
      break;
   case TGSI_FILE_OUTPUT:
      reg = get_itemp(gen);
      break;
   default:
      assert(0);
   }

   return reg;
}


/**
 * When a TGSI instruction is writing to an output register, this
 * function emits the SPE store instruction to store the value_reg.
 * \param value_reg  the SPE register containing the value to store.
 *                   This would have been returned by get_dst_reg().
 */
static void
store_dest_reg(struct codegen *gen,
               int value_reg, int channel,
               const struct tgsi_full_dst_register *dest)
{
   /*
    * XXX need to implement dst reg clamping/saturation
    */
#if 0
   switch (inst->Instruction.Saturate) {
   case TGSI_SAT_NONE:
      break;
   case TGSI_SAT_ZERO_ONE:
      break;
   case TGSI_SAT_MINUS_PLUS_ONE:
      break;
   default:
      assert( 0 );
   }
#endif

   switch (dest->DstRegister.File) {
   case TGSI_FILE_TEMPORARY:
      if (gen->if_nesting > 0 || gen->loop_nesting > 0) {
         int d_reg = gen->temp_regs[dest->DstRegister.Index][channel];
         int exec_reg = get_exec_mask_reg(gen);
         /* Mix d with new value according to exec mask:
          * d[i] = mask_reg[i] ? value_reg : d_reg
          */
         spe_selb(gen->f, d_reg, d_reg, value_reg, exec_reg);
      }
      else {
         /* we're not inside a condition or loop: do nothing special */

      }
      break;
   case TGSI_FILE_OUTPUT:
      {
         /* offset is measured in quadwords, not bytes */
         int offset = dest->DstRegister.Index * 4 + channel;
         if (gen->if_nesting > 0 || gen->loop_nesting > 0) {
            int exec_reg = get_exec_mask_reg(gen);
            int curval_reg = get_itemp(gen);
            /* First read the current value from memory:
             * Load:  curval = memory[(machine_reg) + offset]
             */
            spe_lqd(gen->f, curval_reg, gen->outputs_reg, offset * 16);
            /* Mix curval with newvalue according to exec mask:
             * d[i] = mask_reg[i] ? value_reg : d_reg
             */
            spe_selb(gen->f, curval_reg, curval_reg, value_reg, exec_reg);
            /* Store: memory[(machine_reg) + offset] = curval */
            spe_stqd(gen->f, curval_reg, gen->outputs_reg, offset * 16);
         }
         else {
            /* Store: memory[(machine_reg) + offset] = reg */
            spe_stqd(gen->f, value_reg, gen->outputs_reg, offset * 16);
         }
      }
      break;
   default:
      assert(0);
   }
}



static void
emit_prologue(struct codegen *gen)
{
   gen->frame_size = 1024; /* XXX temporary, should be dynamic */

   spe_comment(gen->f, 0, "Function prologue:");

   /* save $lr on stack     # stqd $lr,16($sp) */
   spe_stqd(gen->f, SPE_REG_RA, SPE_REG_SP, 16);

   if (gen->frame_size >= 512) {
      /* offset is too large for ai instruction */
      int offset_reg = spe_allocate_available_register(gen->f);
      int sp_reg = spe_allocate_available_register(gen->f);
      /* offset = -framesize */
      spe_load_int(gen->f, offset_reg, -gen->frame_size);
      /* sp = $sp */
      spe_move(gen->f, sp_reg, SPE_REG_SP);
      /* $sp = $sp + offset_reg */
      spe_a(gen->f, SPE_REG_SP, SPE_REG_SP, offset_reg);
      /* save $sp in stack frame */
      spe_stqd(gen->f, sp_reg, SPE_REG_SP, 0);
      /* clean up */
      spe_release_register(gen->f, offset_reg);
      spe_release_register(gen->f, sp_reg);
   }
   else {
      /* save stack pointer    # stqd $sp,-frameSize($sp) */
      spe_stqd(gen->f, SPE_REG_SP, SPE_REG_SP, -gen->frame_size);

      /* adjust stack pointer  # ai $sp,$sp,-frameSize */
      spe_ai(gen->f, SPE_REG_SP, SPE_REG_SP, -gen->frame_size);
   }
}


static void
emit_epilogue(struct codegen *gen)
{
   const int return_reg = 3;

   spe_comment(gen->f, 0, "Function epilogue:");

   spe_comment(gen->f, 0, "return the killed mask");
   if (gen->kill_mask_reg > 0) {
      /* shader called KIL, return the "alive" mask */
      spe_move(gen->f, return_reg, gen->kill_mask_reg);
   }
   else {
      /* return {0,0,0,0} */
      spe_load_uint(gen->f, return_reg, 0);
   }

   spe_comment(gen->f, 0, "restore stack and return");
   if (gen->frame_size >= 512) {
      /* offset is too large for ai instruction */
      int offset_reg = spe_allocate_available_register(gen->f);
      /* offset = framesize */
      spe_load_int(gen->f, offset_reg, gen->frame_size);
      /* $sp = $sp + offset */
      spe_a(gen->f, SPE_REG_SP, SPE_REG_SP, offset_reg);
      /* clean up */
      spe_release_register(gen->f, offset_reg);
   }
   else {
      /* restore stack pointer    # ai $sp,$sp,frameSize */
      spe_ai(gen->f, SPE_REG_SP, SPE_REG_SP, gen->frame_size);
   }

   /* restore $lr              # lqd $lr,16($sp) */
   spe_lqd(gen->f, SPE_REG_RA, SPE_REG_SP, 16);

   /* return from function call */
   spe_bi(gen->f, SPE_REG_RA, 0, 0);
}


#define FOR_EACH_ENABLED_CHANNEL(inst, ch) \
   for (ch = 0; ch < 4; ch++) \
      if (inst->FullDstRegisters[0].DstRegister.WriteMask & (1 << ch))


static boolean
emit_ARL(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch = 0, src_reg, addr_reg;

   src_reg = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
   addr_reg = get_address_reg(gen);

   /* convert float to int */
   spe_cflts(gen->f, addr_reg, src_reg, 0);

   free_itemps(gen);

   return TRUE;
}


static boolean
emit_MOV(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, src_reg[4], dst_reg[4];

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      src_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      dst_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      if (is_register_src(gen, ch, &inst->FullSrcRegisters[0]) &&
          is_memory_dst(gen, ch, &inst->FullDstRegisters[0])) {
         /* special-case: register to memory store */
         store_dest_reg(gen, src_reg[ch], ch, &inst->FullDstRegisters[0]);
      }
      else {
         spe_move(gen->f, dst_reg[ch], src_reg[ch]);
         store_dest_reg(gen, dst_reg[ch], ch, &inst->FullDstRegisters[0]);
      }
   }

   free_itemps(gen);

   return TRUE;
}

/**
 * Emit binary operation
 */
static boolean
emit_binop(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], s2_reg[4], d_reg[4];

   /* Loop over Red/Green/Blue/Alpha channels, fetch src operands */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      s2_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   /* Loop over Red/Green/Blue/Alpha channels, do the op, store results */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      /* Emit actual SPE instruction: d = s1 + s2 */
      switch (inst->Instruction.Opcode) {
      case TGSI_OPCODE_ADD:
         spe_fa(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         break;
      case TGSI_OPCODE_SUB:
         spe_fs(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         break;
      case TGSI_OPCODE_MUL:
         spe_fm(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         break;
      default:
         ;
      }
   }

   /* Store the result (a no-op for TGSI_FILE_TEMPORARY dests) */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   /* Free any intermediate temps we allocated */
   free_itemps(gen);

   return TRUE;
}


/**
 * Emit multiply add.  See emit_ADD for comments.
 */
static boolean
emit_MAD(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], s2_reg[4], s3_reg[4], d_reg[4];

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      s2_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      s3_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[2]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fma(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch], s3_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }
   free_itemps(gen);
   return TRUE;
}


/**
 * Emit linear interpolate.  See emit_ADD for comments.
 */
static boolean
emit_LERP(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], s2_reg[4], s3_reg[4], d_reg[4], tmp_reg[4];

   /* setup/get src/dst/temp regs */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      s2_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      s3_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[2]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      tmp_reg[ch] = get_itemp(gen);
   }

   /* d = s3 + s1(s2 - s3) */
   /* do all subtracts, then all fma, then all stores to better pipeline */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fs(gen->f, tmp_reg[ch], s2_reg[ch], s3_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fma(gen->f, d_reg[ch], tmp_reg[ch], s1_reg[ch], s3_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }
   free_itemps(gen);
   return TRUE;
}



/**
 * Emit reciprocal or recip sqrt.
 */
static boolean
emit_RCP_RSQ(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], d_reg[4], tmp_reg[4];

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      tmp_reg[ch] = get_itemp(gen);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      if (inst->Instruction.Opcode == TGSI_OPCODE_RCP) {
         /* tmp = 1/s1 */
         spe_frest(gen->f, tmp_reg[ch], s1_reg[ch]);
      }
      else {
         /* tmp = 1/sqrt(s1) */
         spe_frsqest(gen->f, tmp_reg[ch], s1_reg[ch]);
      }
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      /* d = float_interp(s1, tmp) */
      spe_fi(gen->f, d_reg[ch], s1_reg[ch], tmp_reg[ch]);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit absolute value.  See emit_ADD for comments.
 */
static boolean
emit_ABS(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], d_reg[4];
   const int bit31mask_reg = get_itemp(gen);

   /* mask with bit 31 set, the rest cleared */  
   spe_load_uint(gen->f, bit31mask_reg, (1 << 31));

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   /* d = sign bit cleared in s1 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_andc(gen->f, d_reg[ch], s1_reg[ch], bit31mask_reg);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}

/**
 * Emit 3 component dot product.  See emit_ADD for comments.
 */
static boolean
emit_DP3(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch;
   int s1x_reg, s1y_reg, s1z_reg;
   int s2x_reg, s2y_reg, s2z_reg;
   int t0_reg = get_itemp(gen), t1_reg = get_itemp(gen);

   s1x_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   s2x_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[1]);
   s1y_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   s2y_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[1]);
   s1z_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);
   s2z_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[1]);

   /* t0 = x0 * x1 */
   spe_fm(gen->f, t0_reg, s1x_reg, s2x_reg);

   /* t1 = y0 * y1 */
   spe_fm(gen->f, t1_reg, s1y_reg, s2y_reg);

   /* t0 = z0 * z1 + t0 */
   spe_fma(gen->f, t0_reg, s1z_reg, s2z_reg, t0_reg);

   /* t0 = t0 + t1 */
   spe_fa(gen->f, t0_reg, t0_reg, t1_reg);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      spe_move(gen->f, d_reg, t0_reg);
      store_dest_reg(gen, d_reg, ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}

/**
 * Emit 4 component dot product.  See emit_ADD for comments.
 */
static boolean
emit_DP4(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch;
   int s0x_reg, s0y_reg, s0z_reg, s0w_reg;
   int s1x_reg, s1y_reg, s1z_reg, s1w_reg;
   int t0_reg = get_itemp(gen), t1_reg = get_itemp(gen);

   s0x_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   s1x_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[1]);
   s0y_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   s1y_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[1]);
   s0z_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);
   s1z_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[1]);
   s0w_reg = get_src_reg(gen, CHAN_W, &inst->FullSrcRegisters[0]);
   s1w_reg = get_src_reg(gen, CHAN_W, &inst->FullSrcRegisters[1]);

   /* t0 = x0 * x1 */
   spe_fm(gen->f, t0_reg, s0x_reg, s1x_reg);

   /* t1 = y0 * y1 */
   spe_fm(gen->f, t1_reg, s0y_reg, s1y_reg);

   /* t0 = z0 * z1 + t0 */
   spe_fma(gen->f, t0_reg, s0z_reg, s1z_reg, t0_reg);

   /* t1 = w0 * w1 + t1 */
   spe_fma(gen->f, t1_reg, s0w_reg, s1w_reg, t1_reg);

   /* t0 = t0 + t1 */
   spe_fa(gen->f, t0_reg, t0_reg, t1_reg);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      spe_move(gen->f, d_reg, t0_reg);
      store_dest_reg(gen, d_reg, ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}

/**
 * Emit homogeneous dot product.  See emit_ADD for comments.
 */
static boolean
emit_DPH(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   /* XXX rewrite this function to look more like DP3/DP4 */
   int ch;
   int s1_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   int s2_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[1]);
   int tmp_reg = get_itemp(gen);

   /* t = x0 * x1 */
   spe_fm(gen->f, tmp_reg, s1_reg, s2_reg);

   s1_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[1]);
   /* t = y0 * y1 + t */
   spe_fma(gen->f, tmp_reg, s1_reg, s2_reg, tmp_reg);

   s1_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[1]);
   /* t = z0 * z1 + t */
   spe_fma(gen->f, tmp_reg, s1_reg, s2_reg, tmp_reg);

   s2_reg = get_src_reg(gen, CHAN_W, &inst->FullSrcRegisters[1]);
   /* t = w1 + t */
   spe_fa(gen->f, tmp_reg, s2_reg, tmp_reg);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      spe_move(gen->f, d_reg, tmp_reg);
      store_dest_reg(gen, tmp_reg, ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}

/**
 * Emit 3-component vector normalize.
 */
static boolean
emit_NRM3(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch;
   int src_reg[3];
   int t0_reg = get_itemp(gen), t1_reg = get_itemp(gen);

   src_reg[0] = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   src_reg[1] = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   src_reg[2] = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);

   /* t0 = x * x */
   spe_fm(gen->f, t0_reg, src_reg[0], src_reg[0]);

   /* t1 = y * y */
   spe_fm(gen->f, t1_reg, src_reg[1], src_reg[1]);

   /* t0 = z * z + t0 */
   spe_fma(gen->f, t0_reg, src_reg[2], src_reg[2], t0_reg);

   /* t0 = t0 + t1 */
   spe_fa(gen->f, t0_reg, t0_reg, t1_reg);

   /* t1 = 1.0 / sqrt(t0) */
   spe_frsqest(gen->f, t1_reg, t0_reg);
   spe_fi(gen->f, t1_reg, t0_reg, t1_reg);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      /* dst = src[ch] * t1 */
      spe_fm(gen->f, d_reg, src_reg[ch], t1_reg);
      store_dest_reg(gen, d_reg, ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit cross product.  See emit_ADD for comments.
 */
static boolean
emit_XPD(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int s1_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);
   int s2_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[1]);
   int tmp_reg = get_itemp(gen);

   /* t = z0 * y1 */
   spe_fm(gen->f, tmp_reg, s1_reg, s2_reg);

   s1_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[1]);
   /* t = y0 * z1 - t */
   spe_fms(gen->f, tmp_reg, s1_reg, s2_reg, tmp_reg);

   if (inst->FullDstRegisters[0].DstRegister.WriteMask & (1 << CHAN_X)) {
      store_dest_reg(gen, tmp_reg, CHAN_X, &inst->FullDstRegisters[0]);
   }

   s1_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[1]);
   /* t = x0 * z1 */
   spe_fm(gen->f, tmp_reg, s1_reg, s2_reg);

   s1_reg = get_src_reg(gen, CHAN_Z, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[1]);
   /* t = z0 * x1 - t */
   spe_fms(gen->f, tmp_reg, s1_reg, s2_reg, tmp_reg);

   if (inst->FullDstRegisters[0].DstRegister.WriteMask & (1 << CHAN_Y)) {
      store_dest_reg(gen, tmp_reg, CHAN_Y, &inst->FullDstRegisters[0]);
   }

   s1_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[1]);
   /* t = y0 * x1 */
   spe_fm(gen->f, tmp_reg, s1_reg, s2_reg);

   s1_reg = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[0]);
   s2_reg = get_src_reg(gen, CHAN_Y, &inst->FullSrcRegisters[1]);
   /* t = x0 * y1 - t */
   spe_fms(gen->f, tmp_reg, s1_reg, s2_reg, tmp_reg);

   if (inst->FullDstRegisters[0].DstRegister.WriteMask & (1 << CHAN_Z)) {
      store_dest_reg(gen, tmp_reg, CHAN_Z, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit inequality instruction.
 * Note that the SPE fcgt instruction produces 0x0 and 0xffffffff as
 * the result but OpenGL/TGSI needs 0.0 and 1.0 results.
 * We can easily convert 0x0/0xffffffff to 0.0/1.0 with a bitwise AND.
 */
static boolean
emit_inequality(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], s2_reg[4], d_reg[4], one_reg;
   bool complement = FALSE;

   one_reg = get_const_one_reg(gen);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      s2_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      switch (inst->Instruction.Opcode) {
      case TGSI_OPCODE_SGT:
         spe_fcgt(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         break;
      case TGSI_OPCODE_SLT:
         spe_fcgt(gen->f, d_reg[ch], s2_reg[ch], s1_reg[ch]);
         break;
      case TGSI_OPCODE_SGE:
         spe_fcgt(gen->f, d_reg[ch], s2_reg[ch], s1_reg[ch]);
         complement = TRUE;
         break;
      case TGSI_OPCODE_SLE:
         spe_fcgt(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         complement = TRUE;
         break;
      case TGSI_OPCODE_SEQ:
         spe_fceq(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         break;
      case TGSI_OPCODE_SNE:
         spe_fceq(gen->f, d_reg[ch], s1_reg[ch], s2_reg[ch]);
         complement = TRUE;
         break;
      default:
         assert(0);
      }
   }

   /* convert d from 0x0/0xffffffff to 0.0/1.0 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      /* d = d & one_reg */
      if (complement)
         spe_andc(gen->f, d_reg[ch], one_reg, d_reg[ch]);
      else
         spe_and(gen->f, d_reg[ch], one_reg, d_reg[ch]);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit compare.
 */
static boolean
emit_CMP(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch;

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int s1_reg = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      int s2_reg = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      int s3_reg = get_src_reg(gen, ch, &inst->FullSrcRegisters[2]);
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      int zero_reg = get_itemp(gen);
   
      spe_zero(gen->f, zero_reg);

      /* d = (s1 < 0) ? s2 : s3 */
      spe_fcgt(gen->f, d_reg, zero_reg, s1_reg);
      spe_selb(gen->f, d_reg, s3_reg, s2_reg, d_reg);

      store_dest_reg(gen, d_reg, ch, &inst->FullDstRegisters[0]);
      free_itemps(gen);
   }

   return TRUE;
}

/**
 * Emit trunc.  
 * Convert float to signed int
 * Convert signed int to float
 */
static boolean
emit_TRUNC(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], d_reg[4];

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   /* Convert float to int */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_cflts(gen->f, d_reg[ch], s1_reg[ch], 0);
   }

   /* Convert int to float */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_csflt(gen->f, d_reg[ch], d_reg[ch], 0);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit floor.  
 * If negative int subtract one
 * Convert float to signed int
 * Convert signed int to float
 */
static boolean
emit_FLR(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], d_reg[4], tmp_reg[4], zero_reg, one_reg;

   zero_reg = get_itemp(gen);
   spe_zero(gen->f, zero_reg);
   one_reg = get_const_one_reg(gen);
   
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      tmp_reg[ch] = get_itemp(gen);
   }

   /* If negative, subtract 1.0 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fcgt(gen->f, tmp_reg[ch], zero_reg, s1_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_selb(gen->f, tmp_reg[ch], zero_reg, one_reg, tmp_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fs(gen->f, tmp_reg[ch], s1_reg[ch], tmp_reg[ch]);
   }

   /* Convert float to int */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_cflts(gen->f, tmp_reg[ch], tmp_reg[ch], 0);
   }

   /* Convert int to float */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_csflt(gen->f, d_reg[ch], tmp_reg[ch], 0);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Compute frac = Input - FLR(Input)
 */
static boolean
emit_FRC(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s1_reg[4], d_reg[4], tmp_reg[4], zero_reg, one_reg;

   zero_reg = get_itemp(gen);
   spe_zero(gen->f, zero_reg);
   one_reg = get_const_one_reg(gen);

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      tmp_reg[ch] = get_itemp(gen);
   }

   /* If negative, subtract 1.0 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fcgt(gen->f, tmp_reg[ch], zero_reg, s1_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_selb(gen->f, tmp_reg[ch], zero_reg, one_reg, tmp_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fs(gen->f, tmp_reg[ch], s1_reg[ch], tmp_reg[ch]);
   }

   /* Convert float to int */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_cflts(gen->f, tmp_reg[ch], tmp_reg[ch], 0);
   }

   /* Convert int to float */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_csflt(gen->f, tmp_reg[ch], tmp_reg[ch], 0);
   }

   /* d = s1 - FLR(s1) */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_fs(gen->f, d_reg[ch], s1_reg[ch], tmp_reg[ch]);
   }

   /* store result */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


#if 0
static void
print_functions(struct cell_context *cell)
{
   struct cell_spu_function_info *funcs = &cell->spu_functions;
   uint i;
   for (i = 0; i < funcs->num; i++) {
      printf("SPU func %u: %s at %u\n",
             i, funcs->names[i], funcs->addrs[i]);
   }
}
#endif


static uint
lookup_function(struct cell_context *cell, const char *funcname)
{
   const struct cell_spu_function_info *funcs = &cell->spu_functions;
   uint i, addr = 0;
   for (i = 0; i < funcs->num; i++) {
      if (strcmp(funcs->names[i], funcname) == 0) {
         addr = funcs->addrs[i];
      }
   }
   assert(addr && "spu function not found");
   return addr / 4;  /* discard 2 least significant bits */
}


/**
 * Emit code to call a SPU function.
 * Used to implement instructions like SIN/COS/POW/TEX/etc.
 * If scalar, only the X components of the src regs are used, and the
 * result is replicated across the dest register's XYZW components.
 */
static boolean
emit_function_call(struct codegen *gen,
                   const struct tgsi_full_instruction *inst,
                   char *funcname, uint num_args, boolean scalar)
{
   const uint addr = lookup_function(gen->cell, funcname);
   char comment[100];
   int s_regs[3];
   int func_called = FALSE;
   uint a, ch;
   int retval_reg = -1;

   assert(num_args <= 3);

   snprintf(comment, sizeof(comment), "CALL %s:", funcname);
   spe_comment(gen->f, -4, comment);

   if (scalar) {
      for (a = 0; a < num_args; a++) {
         s_regs[a] = get_src_reg(gen, CHAN_X, &inst->FullSrcRegisters[a]);
      }
      /* we'll call the function, put the return value in this register,
       * then replicate it across all write-enabled components in d_reg.
       */
      retval_reg = spe_allocate_available_register(gen->f);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int d_reg;
      ubyte usedRegs[SPE_NUM_REGS];
      uint i, numUsed;

      if (!scalar) {
         for (a = 0; a < num_args; a++) {
            s_regs[a] = get_src_reg(gen, ch, &inst->FullSrcRegisters[a]);
         }
      }

      d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);

      if (!scalar || !func_called) {
         /* for a scalar function, we'll really only call the function once */

         numUsed = spe_get_registers_used(gen->f, usedRegs);
         assert(numUsed < gen->frame_size / 16 - 2);

         /* save registers to stack */
         for (i = 0; i < numUsed; i++) {
            uint reg = usedRegs[i];
            int offset = 2 + i;
            spe_stqd(gen->f, reg, SPE_REG_SP, 16 * offset);
         }

         /* setup function arguments */
         for (a = 0; a < num_args; a++) {
            spe_move(gen->f, 3 + a, s_regs[a]);
         }

         /* branch to function, save return addr */
         spe_brasl(gen->f, SPE_REG_RA, addr);

         /* save function's return value */
         if (scalar)
            spe_move(gen->f, retval_reg, 3);
         else
            spe_move(gen->f, d_reg, 3);

         /* restore registers from stack */
         for (i = 0; i < numUsed; i++) {
            uint reg = usedRegs[i];
            if (reg != d_reg && reg != retval_reg) {
               int offset = 2 + i;
               spe_lqd(gen->f, reg, SPE_REG_SP, 16 * offset);
            }
         }

         func_called = TRUE;
      }

      if (scalar) {
         spe_move(gen->f, d_reg, retval_reg);
      }

      store_dest_reg(gen, d_reg, ch, &inst->FullDstRegisters[0]);
      free_itemps(gen);
   }

   if (scalar) {
      spe_release_register(gen->f, retval_reg);
   }

   return TRUE;
}


static boolean
emit_TEX(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   const uint target = inst->InstructionExtTexture.Texture;
   const uint unit = inst->FullSrcRegisters[1].SrcRegister.Index;
   uint addr;
   int ch;
   int coord_regs[4], d_regs[4];

   switch (target) {
   case TGSI_TEXTURE_1D:
   case TGSI_TEXTURE_2D:
      addr = lookup_function(gen->cell, "spu_tex_2d");
      break;
   case TGSI_TEXTURE_3D:
      addr = lookup_function(gen->cell, "spu_tex_3d");
      break;
   case TGSI_TEXTURE_CUBE:
      addr = lookup_function(gen->cell, "spu_tex_cube");
      break;
   default:
      ASSERT(0 && "unsupported texture target");
      return FALSE;
   }

   assert(inst->FullSrcRegisters[1].SrcRegister.File == TGSI_FILE_SAMPLER);

   spe_comment(gen->f, -4, "CALL tex:");

   /* get src/dst reg info */
   for (ch = 0; ch < 4; ch++) {
      coord_regs[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      d_regs[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
   }

   {
      ubyte usedRegs[SPE_NUM_REGS];
      uint i, numUsed;

      numUsed = spe_get_registers_used(gen->f, usedRegs);
      assert(numUsed < gen->frame_size / 16 - 2);

      /* save registers to stack */
      for (i = 0; i < numUsed; i++) {
         uint reg = usedRegs[i];
         int offset = 2 + i;
         spe_stqd(gen->f, reg, SPE_REG_SP, 16 * offset);
      }

      /* setup function arguments (XXX depends on target) */
      for (i = 0; i < 4; i++) {
         spe_move(gen->f, 3 + i, coord_regs[i]);
      }
      spe_load_uint(gen->f, 7, unit); /* sampler unit */

      /* branch to function, save return addr */
      spe_brasl(gen->f, SPE_REG_RA, addr);

      /* save function's return values (four pixel's colors) */
      for (i = 0; i < 4; i++) {
         spe_move(gen->f, d_regs[i], 3 + i);
      }

      /* restore registers from stack */
      for (i = 0; i < numUsed; i++) {
         uint reg = usedRegs[i];
         if (reg != d_regs[0] &&
             reg != d_regs[1] &&
             reg != d_regs[2] &&
             reg != d_regs[3]) {
            int offset = 2 + i;
            spe_lqd(gen->f, reg, SPE_REG_SP, 16 * offset);
         }
      }
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_regs[ch], ch, &inst->FullDstRegisters[0]);
      free_itemps(gen);
   }

   return TRUE;
}


/**
 * KILL if any of src reg values are less than zero.
 */
static boolean
emit_KIL(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch;
   int s_regs[4], kil_reg = -1, cmp_reg, zero_reg;

   spe_comment(gen->f, -4, "CALL kil:");

   /* zero = {0,0,0,0} */
   zero_reg = get_itemp(gen);
   spe_zero(gen->f, zero_reg);

   cmp_reg = get_itemp(gen);

   /* get src regs */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s_regs[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
   }

   /* test if any src regs are < 0 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      if (kil_reg >= 0) {
         /* cmp = 0 > src ? : ~0 : 0 */
         spe_fcgt(gen->f, cmp_reg, zero_reg, s_regs[ch]);
         /* kil = kil | cmp */
         spe_or(gen->f, kil_reg, kil_reg, cmp_reg);
      }
      else {
         kil_reg = get_itemp(gen);
         /* kil = 0 > src ? : ~0 : 0 */
         spe_fcgt(gen->f, kil_reg, zero_reg, s_regs[ch]);
      }
   }

   if (gen->if_nesting || gen->loop_nesting) {
      /* may have been a conditional kil */
      spe_and(gen->f, kil_reg, kil_reg, gen->exec_mask_reg);
   }

   /* allocate the kill mask reg if needed */
   if (gen->kill_mask_reg <= 0) {
      gen->kill_mask_reg = spe_allocate_available_register(gen->f);
      spe_move(gen->f, gen->kill_mask_reg, kil_reg);
   }
   else {
      spe_or(gen->f, gen->kill_mask_reg, gen->kill_mask_reg, kil_reg);
   }

   free_itemps(gen);

   return TRUE;
}



/**
 * Emit min or max.
 */
static boolean
emit_MIN_MAX(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int ch, s0_reg[4], s1_reg[4], d_reg[4], tmp_reg[4];

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      s0_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      s1_reg[ch] = get_src_reg(gen, ch, &inst->FullSrcRegisters[1]);
      d_reg[ch] = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);
      tmp_reg[ch] = get_itemp(gen);         
   }

   /* d = (s0 > s1) ? s0 : s1 */
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      if (inst->Instruction.Opcode == TGSI_OPCODE_MAX)
         spe_fcgt(gen->f, tmp_reg[ch], s0_reg[ch], s1_reg[ch]);
      else
         spe_fcgt(gen->f, tmp_reg[ch], s1_reg[ch], s0_reg[ch]);
   }
   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      spe_selb(gen->f, d_reg[ch], s1_reg[ch], s0_reg[ch], tmp_reg[ch]);
   }

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      store_dest_reg(gen, d_reg[ch], ch, &inst->FullDstRegisters[0]);
   }

   free_itemps(gen);
   return TRUE;
}


/**
 * Emit code to update the execution mask.
 * This needs to be done whenever the execution status of a conditional
 * or loop is changed.
 */
static void
emit_update_exec_mask(struct codegen *gen)
{
   const int exec_reg = get_exec_mask_reg(gen);
   const int cond_reg = gen->cond_mask_reg;
   const int loop_reg = gen->loop_mask_reg;

   spe_comment(gen->f, 0, "Update master execution mask");

   if (gen->if_nesting > 0 && gen->loop_nesting > 0) {
      /* exec_mask = cond_mask & loop_mask */
      assert(cond_reg > 0);
      assert(loop_reg > 0);
      spe_and(gen->f, exec_reg, cond_reg, loop_reg);
   }
   else if (gen->if_nesting > 0) {
      assert(cond_reg > 0);
      spe_move(gen->f, exec_reg, cond_reg);
   }
   else if (gen->loop_nesting > 0) {
      assert(loop_reg > 0);
      spe_move(gen->f, exec_reg, loop_reg);
   }
   else {
      spe_load_int(gen->f, exec_reg, ~0x0);
   }
}


static boolean
emit_IF(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   const int channel = 0;
   int cond_reg;

   cond_reg = get_cond_mask_reg(gen);

   /* XXX push cond exec mask */

   spe_comment(gen->f,  0, "init conditional exec mask = ~0:");
   spe_load_int(gen->f, cond_reg, ~0);

   /* update conditional execution mask with the predicate register */
   int tmp_reg = get_itemp(gen);
   int s1_reg = get_src_reg(gen, channel, &inst->FullSrcRegisters[0]);

   /* tmp = (s1_reg == 0) */
   spe_ceqi(gen->f, tmp_reg, s1_reg, 0);
   /* tmp = !tmp */
   spe_complement(gen->f, tmp_reg, tmp_reg);
   /* cond_mask = cond_mask & tmp */
   spe_and(gen->f, cond_reg, cond_reg, tmp_reg);

   gen->if_nesting++;

   /* update the master execution mask */
   emit_update_exec_mask(gen);

   free_itemps(gen);

   return TRUE;
}


static boolean
emit_ELSE(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   const int cond_reg = get_cond_mask_reg(gen);

   spe_comment(gen->f, 0, "cond exec mask = !cond exec mask");
   spe_complement(gen->f, cond_reg, cond_reg);
   emit_update_exec_mask(gen);

   return TRUE;
}


static boolean
emit_ENDIF(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   /* XXX todo: pop cond exec mask */

   gen->if_nesting--;

   emit_update_exec_mask(gen);

   return TRUE;
}


static boolean
emit_BGNLOOP(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   int exec_reg, loop_reg;

   exec_reg = get_exec_mask_reg(gen);
   loop_reg = get_loop_mask_reg(gen);

   /* XXX push loop_exec mask */

   spe_comment(gen->f,  0*-4, "initialize loop exec mask = ~0");
   spe_load_int(gen->f, loop_reg, ~0x0);

   gen->loop_nesting++;
   gen->loop_start = spe_code_size(gen->f);  /* in bytes */

   return TRUE;
}


static boolean
emit_ENDLOOP(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   const int loop_reg = get_loop_mask_reg(gen);
   const int tmp_reg = get_itemp(gen);
   int offset;

   /* tmp_reg = exec[0] | exec[1] | exec[2] | exec[3] */
   spe_orx(gen->f, tmp_reg, loop_reg);

   offset = gen->loop_start - spe_code_size(gen->f); /* in bytes */

   /* branch back to top of loop if tmp_reg != 0 */
   spe_brnz(gen->f, tmp_reg, offset / 4);

   /* XXX pop loop_exec mask */

   gen->loop_nesting--;

   emit_update_exec_mask(gen);

   return TRUE;
}


static boolean
emit_BRK(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   const int exec_reg = get_exec_mask_reg(gen);
   const int loop_reg = get_loop_mask_reg(gen);

   assert(gen->loop_nesting > 0);

   spe_comment(gen->f, 0, "loop exec mask &= ~master exec mask");
   spe_andc(gen->f, loop_reg, loop_reg, exec_reg);

   emit_update_exec_mask(gen);

   return TRUE;
}


static boolean
emit_CONT(struct codegen *gen, const struct tgsi_full_instruction *inst)
{
   assert(gen->loop_nesting > 0);

   return TRUE;
}


static boolean
emit_DDX_DDY(struct codegen *gen, const struct tgsi_full_instruction *inst,
             boolean ddx)
{
   int ch;

   FOR_EACH_ENABLED_CHANNEL(inst, ch) {
      int s_reg = get_src_reg(gen, ch, &inst->FullSrcRegisters[0]);
      int d_reg = get_dst_reg(gen, ch, &inst->FullDstRegisters[0]);

      int t1_reg = get_itemp(gen);
      int t2_reg = get_itemp(gen);

      spe_splat_word(gen->f, t1_reg, s_reg, 0); /* upper-left pixel */
      if (ddx) {
         spe_splat_word(gen->f, t2_reg, s_reg, 1); /* upper-right pixel */
      }
      else {
         spe_splat_word(gen->f, t2_reg, s_reg, 2); /* lower-left pixel */
      }
      spe_fs(gen->f, d_reg, t2_reg, t1_reg);

      free_itemps(gen);
   }

   return TRUE;
}




/**
 * Emit END instruction.
 * We just return from the shader function at this point.
 *
 * Note that there may be more code after this that would be
 * called by TGSI_OPCODE_CALL.
 */
static boolean
emit_END(struct codegen *gen)
{
   emit_epilogue(gen);
   return TRUE;
}


/**
 * Emit code for the given instruction.  Just a big switch stmt.
 */
static boolean
emit_instruction(struct codegen *gen,
                 const struct tgsi_full_instruction *inst)
{
   switch (inst->Instruction.Opcode) {
   case TGSI_OPCODE_ARL:
      return emit_ARL(gen, inst);
   case TGSI_OPCODE_MOV:
   case TGSI_OPCODE_SWZ:
      return emit_MOV(gen, inst);
   case TGSI_OPCODE_ADD:
   case TGSI_OPCODE_SUB:
   case TGSI_OPCODE_MUL:
      return emit_binop(gen, inst);
   case TGSI_OPCODE_MAD:
      return emit_MAD(gen, inst);
   case TGSI_OPCODE_LERP:
      return emit_LERP(gen, inst);
   case TGSI_OPCODE_DP3:
      return emit_DP3(gen, inst);
   case TGSI_OPCODE_DP4:
      return emit_DP4(gen, inst);
   case TGSI_OPCODE_DPH:
      return emit_DPH(gen, inst);
   case TGSI_OPCODE_NRM:
      return emit_NRM3(gen, inst);
   case TGSI_OPCODE_XPD:
      return emit_XPD(gen, inst);
   case TGSI_OPCODE_RCP:
   case TGSI_OPCODE_RSQ:
      return emit_RCP_RSQ(gen, inst);
   case TGSI_OPCODE_ABS:
      return emit_ABS(gen, inst);
   case TGSI_OPCODE_SGT:
   case TGSI_OPCODE_SLT:
   case TGSI_OPCODE_SGE:
   case TGSI_OPCODE_SLE:
   case TGSI_OPCODE_SEQ:
   case TGSI_OPCODE_SNE:
      return emit_inequality(gen, inst);
   case TGSI_OPCODE_CMP:
      return emit_CMP(gen, inst);
   case TGSI_OPCODE_MIN:
   case TGSI_OPCODE_MAX:
      return emit_MIN_MAX(gen, inst);
   case TGSI_OPCODE_TRUNC:
      return emit_TRUNC(gen, inst);
   case TGSI_OPCODE_FLR:
      return emit_FLR(gen, inst);
   case TGSI_OPCODE_FRC:
      return emit_FRC(gen, inst);
   case TGSI_OPCODE_END:
      return emit_END(gen);

   case TGSI_OPCODE_COS:
      return emit_function_call(gen, inst, "spu_cos", 1, TRUE);
   case TGSI_OPCODE_SIN:
      return emit_function_call(gen, inst, "spu_sin", 1, TRUE);
   case TGSI_OPCODE_POW:
      return emit_function_call(gen, inst, "spu_pow", 2, TRUE);
   case TGSI_OPCODE_EXPBASE2:
      return emit_function_call(gen, inst, "spu_exp2", 1, TRUE);
   case TGSI_OPCODE_LOGBASE2:
      return emit_function_call(gen, inst, "spu_log2", 1, TRUE);
   case TGSI_OPCODE_TEX:
      /* fall-through for now */
   case TGSI_OPCODE_TXD:
      /* fall-through for now */
   case TGSI_OPCODE_TXB:
      /* fall-through for now */
   case TGSI_OPCODE_TXL:
      /* fall-through for now */
   case TGSI_OPCODE_TXP:
      return emit_TEX(gen, inst);
   case TGSI_OPCODE_KIL:
      return emit_KIL(gen, inst);

   case TGSI_OPCODE_IF:
      return emit_IF(gen, inst);
   case TGSI_OPCODE_ELSE:
      return emit_ELSE(gen, inst);
   case TGSI_OPCODE_ENDIF:
      return emit_ENDIF(gen, inst);

   case TGSI_OPCODE_BGNLOOP:
      return emit_BGNLOOP(gen, inst);
   case TGSI_OPCODE_ENDLOOP:
      return emit_ENDLOOP(gen, inst);
   case TGSI_OPCODE_BRK:
      return emit_BRK(gen, inst);
   case TGSI_OPCODE_CONT:
      return emit_CONT(gen, inst);

   case TGSI_OPCODE_DDX:
      return emit_DDX_DDY(gen, inst, TRUE);
   case TGSI_OPCODE_DDY:
      return emit_DDX_DDY(gen, inst, FALSE);

   /* XXX lots more cases to do... */

   default:
      fprintf(stderr, "Cell: unimplemented TGSI instruction %d!\n",
              inst->Instruction.Opcode);
      return FALSE;
   }

   return TRUE;
}



/**
 * Emit code for a TGSI immediate value (vector of four floats).
 * This involves register allocation and initialization.
 * XXX the initialization should be done by a "prepare" stage, not
 * per quad execution!
 */
static boolean
emit_immediate(struct codegen *gen, const struct tgsi_full_immediate *immed)
{
   int ch;

   assert(gen->num_imm < MAX_TEMPS);

   for (ch = 0; ch < 4; ch++) {
      float val = immed->u[ch].Float;

      if (ch > 0 && val == immed->u[ch - 1].Float) {
         /* re-use previous register */
         gen->imm_regs[gen->num_imm][ch] = gen->imm_regs[gen->num_imm][ch - 1];
      }
      else {
         char str[100];
         int reg = spe_allocate_available_register(gen->f);

         if (reg < 0)
            return FALSE;

         sprintf(str, "init $%d = %f", reg, val);
         spe_comment(gen->f, 0, str);

         /* update immediate map */
         gen->imm_regs[gen->num_imm][ch] = reg;

         /* emit initializer instruction */
         spe_load_float(gen->f, reg, val);
      }
   }

   gen->num_imm++;

   return TRUE;
}



/**
 * Emit "code" for a TGSI declaration.
 * We only care about TGSI TEMPORARY register declarations at this time.
 * For each TGSI TEMPORARY we allocate four SPE registers.
 */
static boolean
emit_declaration(struct cell_context *cell,
                 struct codegen *gen, const struct tgsi_full_declaration *decl)
{
   int i, ch;

   switch (decl->Declaration.File) {
   case TGSI_FILE_TEMPORARY:
      for (i = decl->DeclarationRange.First;
           i <= decl->DeclarationRange.Last;
           i++) {
         assert(i < MAX_TEMPS);
         for (ch = 0; ch < 4; ch++) {
            gen->temp_regs[i][ch] = spe_allocate_available_register(gen->f);
            if (gen->temp_regs[i][ch] < 0)
               return FALSE; /* out of regs */
         }

         /* XXX if we run out of SPE registers, we need to spill
          * to SPU memory.  someday...
          */

         {
            char buf[100];
            sprintf(buf, "TGSI temp[%d] maps to SPU regs [$%d $%d $%d $%d]", i,
                    gen->temp_regs[i][0], gen->temp_regs[i][1],
                    gen->temp_regs[i][2], gen->temp_regs[i][3]);
            spe_comment(gen->f, 0, buf);
         }
      }
      break;
   default:
      ; /* ignore */
   }

   return TRUE;
}



/**
 * Translate TGSI shader code to SPE instructions.  This is done when
 * the state tracker gives us a new shader (via pipe->create_fs_state()).
 *
 * \param cell    the rendering context (in)
 * \param tokens  the TGSI shader (in)
 * \param f       the generated function (out)
 */
boolean
cell_gen_fragment_program(struct cell_context *cell,
                          const struct tgsi_token *tokens,
                          struct spe_function *f)
{
   struct tgsi_parse_context parse;
   struct codegen gen;
   uint ic = 0;

   memset(&gen, 0, sizeof(gen));
   gen.cell = cell;
   gen.f = f;

   /* For SPE function calls: reg $3 = first param, $4 = second param, etc. */
   gen.inputs_reg = 3;     /* pointer to inputs array */
   gen.outputs_reg = 4;    /* pointer to outputs array */
   gen.constants_reg = 5;  /* pointer to constants array */

   spe_init_func(f, SPU_MAX_FRAGMENT_PROGRAM_INSTS * SPE_INST_SIZE);
   spe_allocate_register(f, gen.inputs_reg);
   spe_allocate_register(f, gen.outputs_reg);
   spe_allocate_register(f, gen.constants_reg);

   if (cell->debug_flags & CELL_DEBUG_ASM) {
      spe_print_code(f, TRUE);
      spe_indent(f, 2*8);
      printf("Begin %s\n", __FUNCTION__);
      tgsi_dump(tokens, 0);
   }

   tgsi_parse_init(&parse, tokens);

   emit_prologue(&gen);

   while (!tgsi_parse_end_of_tokens(&parse) && !gen.error) {
      tgsi_parse_token(&parse);

      switch (parse.FullToken.Token.Type) {
      case TGSI_TOKEN_TYPE_IMMEDIATE:
         if (f->print) {
            _debug_printf("    # ");
            tgsi_dump_immediate(&parse.FullToken.FullImmediate);
         }
         if (!emit_immediate(&gen, &parse.FullToken.FullImmediate))
            gen.error = TRUE;
         break;

      case TGSI_TOKEN_TYPE_DECLARATION:
         if (f->print) {
            _debug_printf("    # ");
            tgsi_dump_declaration(&parse.FullToken.FullDeclaration);
         }
         if (!emit_declaration(cell, &gen, &parse.FullToken.FullDeclaration))
            gen.error = TRUE;
         break;

      case TGSI_TOKEN_TYPE_INSTRUCTION:
         if (f->print) {
            _debug_printf("    # ");
            ic++;
            tgsi_dump_instruction(&parse.FullToken.FullInstruction, ic);
         }
         if (!emit_instruction(&gen, &parse.FullToken.FullInstruction))
            gen.error = TRUE;
         break;

      default:
         assert(0);
      }
   }

   if (gen.error) {
      /* terminate the SPE code */
      return emit_END(&gen);
   }

   if (cell->debug_flags & CELL_DEBUG_ASM) {
      printf("cell_gen_fragment_program nr instructions: %d\n", f->num_inst);
      printf("End %s\n", __FUNCTION__);
   }

   tgsi_parse_free( &parse );

   return !gen.error;
}