summaryrefslogtreecommitdiff
path: root/src/mesa/shader/program_parse.y
blob: 9703e8e670f599b7b4cae91b79097435f8a06d90 (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
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
%{
/*
 * Copyright © 2009 Intel Corporation
 *
 * 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, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main/mtypes.h"
#include "main/imports.h"
#include "program.h"
#include "prog_parameter.h"
#include "prog_parameter_layout.h"
#include "prog_statevars.h"
#include "prog_instruction.h"

#include "symbol_table.h"
#include "program_parser.h"

extern void *yy_scan_string(char *);
extern void yy_delete_buffer(void *);

static struct asm_symbol *declare_variable(struct asm_parser_state *state,
    char *name, enum asm_type t, struct YYLTYPE *locp);

static int add_state_reference(struct gl_program_parameter_list *param_list,
    const gl_state_index tokens[STATE_LENGTH]);

static int initialize_symbol_from_state(struct gl_program *prog,
    struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);

static int initialize_symbol_from_param(struct gl_program *prog,
    struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);

static int initialize_symbol_from_const(struct gl_program *prog,
    struct asm_symbol *param_var, const struct asm_vector *vec);

static int yyparse(struct asm_parser_state *state);

static char *make_error_string(const char *fmt, ...);

static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
    const char *s);

static int validate_inputs(struct YYLTYPE *locp,
    struct asm_parser_state *state);

static void init_dst_reg(struct prog_dst_register *r);

static void init_src_reg(struct asm_src_register *r);

static void asm_instruction_set_operands(struct asm_instruction *inst,
    const struct prog_dst_register *dst, const struct asm_src_register *src0,
    const struct asm_src_register *src1, const struct asm_src_register *src2);

static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
    const struct prog_dst_register *dst, const struct asm_src_register *src0,
    const struct asm_src_register *src1, const struct asm_src_register *src2);

static struct asm_instruction *asm_instruction_copy_ctor(
    const struct prog_instruction *base, const struct prog_dst_register *dst,
    const struct asm_src_register *src0, const struct asm_src_register *src1,
    const struct asm_src_register *src2);

#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif

#define YYLLOC_DEFAULT(Current, Rhs, N)					\
   do {									\
      if (YYID(N)) {							\
	 (Current).first_line = YYRHSLOC(Rhs, 1).first_line;		\
	 (Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
	 (Current).position = YYRHSLOC(Rhs, 1).position;		\
	 (Current).last_line = YYRHSLOC(Rhs, N).last_line;		\
	 (Current).last_column = YYRHSLOC(Rhs, N).last_column;		\
      } else {								\
	 (Current).first_line = YYRHSLOC(Rhs, 0).last_line;		\
	 (Current).last_line = (Current).first_line;			\
	 (Current).first_column = YYRHSLOC(Rhs, 0).last_column;		\
	 (Current).last_column = (Current).first_column;		\
	 (Current).position = YYRHSLOC(Rhs, 0).position			\
	    + (Current).first_column;					\
      }									\
   } while(YYID(0))

#define YYLEX_PARAM state->scanner
%}

%pure-parser
%locations
%parse-param { struct asm_parser_state *state }
%error-verbose
%lex-param { void *scanner }

%union {
   struct asm_instruction *inst;
   struct asm_symbol *sym;
   struct asm_symbol temp_sym;
   struct asm_swizzle_mask swiz_mask;
   struct asm_src_register src_reg;
   struct prog_dst_register dst_reg;
   struct prog_instruction temp_inst;
   char *string;
   unsigned result;
   unsigned attrib;
   int integer;
   float real;
   gl_state_index state[STATE_LENGTH];
   int negate;
   struct asm_vector vector;
   gl_inst_opcode opcode;

   struct {
      unsigned swz;
      unsigned rgba_valid:1;
      unsigned xyzw_valid:1;
      unsigned negate:1;
   } ext_swizzle;
}

%token ARBvp_10 ARBfp_10

/* Tokens for assembler pseudo-ops */
%token <integer> ADDRESS
%token ALIAS ATTRIB
%token OPTION OUTPUT
%token PARAM
%token <integer> TEMP
%token END

 /* Tokens for instructions */
%token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
%token <temp_inst> ARL KIL SWZ TXD_OP

%token <integer> INTEGER
%token <real> REAL

%token AMBIENT ATTENUATION
%token BACK
%token CLIP COLOR
%token DEPTH DIFFUSE DIRECTION
%token EMISSION ENV EYE
%token FOG FOGCOORD FRAGMENT FRONT
%token HALF
%token INVERSE INVTRANS
%token LIGHT LIGHTMODEL LIGHTPROD LOCAL
%token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
%token NORMAL
%token OBJECT
%token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
%token RANGE RESULT ROW
%token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
%token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
%token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
%token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
%token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D 
%token VERTEX VTXATTRIB
%token WEIGHT

%token <string> IDENTIFIER USED_IDENTIFIER
%type <string> string
%token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
%token DOT_DOT
%token DOT

%type <inst> instruction ALU_instruction TexInstruction
%type <inst> ARL_instruction VECTORop_instruction
%type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
%type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
%type <inst> KIL_instruction

%type <dst_reg> dstReg maskedDstReg maskedAddrReg
%type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
%type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
%type <ext_swizzle> extSwizComp extSwizSel
%type <swiz_mask> optionalMask

%type <sym> progParamArray
%type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
%type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
%type <sym> addrReg
%type <swiz_mask> addrComponent addrWriteMask

%type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask

%type <result> resultBinding resultColBinding
%type <integer> optFaceType optColorType
%type <integer> optResultFaceType optResultColorType

%type <integer> optTexImageUnitNum texImageUnitNum
%type <integer> optTexCoordUnitNum texCoordUnitNum
%type <integer> optLegacyTexUnitNum legacyTexUnitNum
%type <integer> texImageUnit texTarget
%type <integer> vtxAttribNum

%type <attrib> attribBinding vtxAttribItem fragAttribItem

%type <temp_sym> paramSingleInit paramSingleItemDecl
%type <integer> optArraySize

%type <state> stateSingleItem stateMultipleItem
%type <state> stateMaterialItem
%type <state> stateLightItem stateLightModelItem stateLightProdItem
%type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
%type <state> stateMatrixItem stateMatrixRow stateMatrixRows
%type <state> stateTexEnvItem stateDepthItem

%type <state> stateLModProperty
%type <state> stateMatrixName optMatrixRows

%type <integer> stateMatProperty
%type <integer> stateLightProperty stateSpotProperty
%type <integer> stateLightNumber stateLProdProperty
%type <integer> stateTexGenType stateTexGenCoord
%type <integer> stateTexEnvProperty
%type <integer> stateFogProperty
%type <integer> stateClipPlaneNum
%type <integer> statePointProperty

%type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
%type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum 
%type <integer> stateProgramMatNum

%type <integer> ambDiffSpecProperty

%type <state> programSingleItem progEnvParam progLocalParam
%type <state> programMultipleItem progEnvParams progLocalParams

%type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
%type <temp_sym> paramSingleItemUse

%type <integer> progEnvParamNum progLocalParamNum
%type <state> progEnvParamNums progLocalParamNums

%type <vector> paramConstDecl paramConstUse
%type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
%type <real> signedFloatConstant
%type <negate> optionalSign

%{
extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
    void *yyscanner);
%}

%%

program: language optionSequence statementSequence END
	;

language: ARBvp_10
	{
	   if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
	      yyerror(& @1, state, "invalid fragment program header");

	   }
	   state->mode = ARB_vertex;
	}
	| ARBfp_10
	{
	   if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
	      yyerror(& @1, state, "invalid vertex program header");
	   }
	   state->mode = ARB_fragment;

	   state->option.TexRect =
	      (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
	}
	;

optionSequence: optionSequence option
	|
	;

option: OPTION string ';'
	{
	   int valid = 0;

	   if (state->mode == ARB_vertex) {
	      valid = _mesa_ARBvp_parse_option(state, $2);
	   } else if (state->mode == ARB_fragment) {
	      valid = _mesa_ARBfp_parse_option(state, $2);
	   }


	   if (!valid) {
	      const char *const err_str = (state->mode == ARB_vertex)
		 ? "invalid ARB vertex program option"
		 : "invalid ARB fragment program option";

	      yyerror(& @2, state, err_str);
	      YYERROR;
	   }
	}
	;

statementSequence: statementSequence statement
	|
	;

statement: instruction ';'
	{
	   if ($1 != NULL) {
	      if (state->inst_tail == NULL) {
		 state->inst_head = $1;
	      } else {
		 state->inst_tail->next = $1;
	      }

	      state->inst_tail = $1;
	      $1->next = NULL;

	      state->prog->NumInstructions++;
	   }
	}
	| namingStatement ';'
	;

instruction: ALU_instruction
	{
	   $$ = $1;
	   state->prog->NumAluInstructions++;
	}
	| TexInstruction
	{
	   $$ = $1;
	   state->prog->NumTexInstructions++;
	}
	;

ALU_instruction: ARL_instruction
	| VECTORop_instruction
	| SCALARop_instruction
	| BINSCop_instruction
	| BINop_instruction
	| TRIop_instruction
	| SWZ_instruction
	;

TexInstruction: SAMPLE_instruction
	| KIL_instruction
	| TXD_instruction
	;

ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
	{
	   $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
	}
	;

VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
	}
	;

SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
	}
	;

BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
	}
	;


BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
	}
	;

TRIop_instruction: TRI_OP maskedDstReg ','
                   swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
	}
	;

SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
	   if ($$ != NULL) {
	      const GLbitfield tex_mask = (1U << $6);
	      GLbitfield shadow_tex = 0;
	      GLbitfield target_mask = 0;


	      $$->Base.TexSrcUnit = $6;

	      if ($8 < 0) {
		 shadow_tex = tex_mask;

		 $$->Base.TexSrcTarget = -$8;
		 $$->Base.TexShadow = 1;
	      } else {
		 $$->Base.TexSrcTarget = $8;
	      }

	      target_mask = (1U << $$->Base.TexSrcTarget);

	      /* If this texture unit was previously accessed and that access
	       * had a different texture target, generate an error.
	       *
	       * If this texture unit was previously accessed and that access
	       * had a different shadow mode, generate an error.
	       */
	      if ((state->prog->TexturesUsed[$6] != 0)
		  && ((state->prog->TexturesUsed[$6] != target_mask)
		      || ((state->prog->ShadowSamplers & tex_mask)
			  != shadow_tex))) {
		 yyerror(& @8, state,
			 "multiple targets used on one texture image unit");
		 YYERROR;
	      }


	      state->prog->TexturesUsed[$6] |= target_mask;
	      state->prog->ShadowSamplers |= shadow_tex;
	   }
	}
	;

KIL_instruction: KIL swizzleSrcReg
	{
	   $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
	   state->fragment.UsesKill = 1;
	}
	| KIL ccTest
	{
	   $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
	   $$->Base.DstReg.CondMask = $2.CondMask;
	   $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
	   $$->Base.DstReg.CondSrc = $2.CondSrc;
	   state->fragment.UsesKill = 1;
	}
	;

TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
	{
	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
	   if ($$ != NULL) {
	      const GLbitfield tex_mask = (1U << $10);
	      GLbitfield shadow_tex = 0;
	      GLbitfield target_mask = 0;


	      $$->Base.TexSrcUnit = $10;

	      if ($12 < 0) {
		 shadow_tex = tex_mask;

		 $$->Base.TexSrcTarget = -$12;
		 $$->Base.TexShadow = 1;
	      } else {
		 $$->Base.TexSrcTarget = $12;
	      }

	      target_mask = (1U << $$->Base.TexSrcTarget);

	      /* If this texture unit was previously accessed and that access
	       * had a different texture target, generate an error.
	       *
	       * If this texture unit was previously accessed and that access
	       * had a different shadow mode, generate an error.
	       */
	      if ((state->prog->TexturesUsed[$10] != 0)
		  && ((state->prog->TexturesUsed[$10] != target_mask)
		      || ((state->prog->ShadowSamplers & tex_mask)
			  != shadow_tex))) {
		 yyerror(& @12, state,
			 "multiple targets used on one texture image unit");
		 YYERROR;
	      }


	      state->prog->TexturesUsed[$10] |= target_mask;
	      state->prog->ShadowSamplers |= shadow_tex;
	   }
	}
	;

texImageUnit: TEXTURE_UNIT optTexImageUnitNum
	{
	   $$ = $2;
	}
	;

texTarget: TEX_1D  { $$ = TEXTURE_1D_INDEX; }
	| TEX_2D   { $$ = TEXTURE_2D_INDEX; }
	| TEX_3D   { $$ = TEXTURE_3D_INDEX; }
	| TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
	| TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
	| TEX_SHADOW1D   { $$ = -TEXTURE_1D_INDEX; }
	| TEX_SHADOW2D   { $$ = -TEXTURE_2D_INDEX; }
	| TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
	| TEX_ARRAY1D         { $$ = TEXTURE_1D_ARRAY_INDEX; }
	| TEX_ARRAY2D         { $$ = TEXTURE_2D_ARRAY_INDEX; }
	| TEX_ARRAYSHADOW1D   { $$ = -TEXTURE_1D_ARRAY_INDEX; }
	| TEX_ARRAYSHADOW2D   { $$ = -TEXTURE_2D_ARRAY_INDEX; }
	;

SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
	{
	   /* FIXME: Is this correct?  Should the extenedSwizzle be applied
	    * FIXME: to the existing swizzle?
	    */
	   $4.Base.Swizzle = $6.swizzle;
	   $4.Base.Negate = $6.mask;

	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
	}
	;

scalarSrcReg: optionalSign scalarUse
	{
	   $$ = $2;

	   if ($1) {
	      $$.Base.Negate = ~$$.Base.Negate;
	   }
	}
	| optionalSign '|' scalarUse '|'
	{
	   $$ = $3;

	   if (!state->option.NV_fragment) {
	      yyerror(& @2, state, "unexpected character '|'");
	      YYERROR;
	   }

	   if ($1) {
	      $$.Base.Negate = ~$$.Base.Negate;
	   }

	   $$.Base.Abs = 1;
	}
	;

scalarUse:  srcReg scalarSuffix
	{
	   $$ = $1;

	   $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
						    $2.swizzle);
	}
	| paramConstScalarUse
	{
	   struct asm_symbol temp_sym;

	   if (!state->option.NV_fragment) {
	      yyerror(& @1, state, "expected scalar suffix");
	      YYERROR;
	   }

	   memset(& temp_sym, 0, sizeof(temp_sym));
	   temp_sym.param_binding_begin = ~0;
	   initialize_symbol_from_const(state->prog, & temp_sym, & $1);

	   init_src_reg(& $$);
	   $$.Base.File = PROGRAM_CONSTANT;
	   $$.Base.Index = temp_sym.param_binding_begin;
	}
	;

swizzleSrcReg: optionalSign srcReg swizzleSuffix
	{
	   $$ = $2;

	   if ($1) {
	      $$.Base.Negate = ~$$.Base.Negate;
	   }

	   $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
						    $3.swizzle);
	}
	| optionalSign '|' srcReg swizzleSuffix '|'
	{
	   $$ = $3;

	   if (!state->option.NV_fragment) {
	      yyerror(& @2, state, "unexpected character '|'");
	      YYERROR;
	   }

	   if ($1) {
	      $$.Base.Negate = ~$$.Base.Negate;
	   }

	   $$.Base.Abs = 1;
	   $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
						    $4.swizzle);
	}

	;

maskedDstReg: dstReg optionalMask optionalCcMask
	{
	   $$ = $1;
	   $$.WriteMask = $2.mask;
	   $$.CondMask = $3.CondMask;
	   $$.CondSwizzle = $3.CondSwizzle;
	   $$.CondSrc = $3.CondSrc;

	   if ($$.File == PROGRAM_OUTPUT) {
	      /* Technically speaking, this should check that it is in
	       * vertex program mode.  However, PositionInvariant can never be
	       * set in fragment program mode, so it is somewhat irrelevant.
	       */
	      if (state->option.PositionInvariant
	       && ($$.Index == VERT_RESULT_HPOS)) {
		 yyerror(& @1, state, "position-invariant programs cannot "
			 "write position");
		 YYERROR;
	      }

	      state->prog->OutputsWritten |= (1U << $$.Index);
	   }
	}
	;

maskedAddrReg: addrReg addrWriteMask
	{
	   init_dst_reg(& $$);
	   $$.File = PROGRAM_ADDRESS;
	   $$.Index = 0;
	   $$.WriteMask = $2.mask;
	}
	;

extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
	{
	   const unsigned xyzw_valid =
	      ($1.xyzw_valid << 0)
	      | ($3.xyzw_valid << 1)
	      | ($5.xyzw_valid << 2)
	      | ($7.xyzw_valid << 3);
	   const unsigned rgba_valid =
	      ($1.rgba_valid << 0)
	      | ($3.rgba_valid << 1)
	      | ($5.rgba_valid << 2)
	      | ($7.rgba_valid << 3);

	   /* All of the swizzle components have to be valid in either RGBA
	    * or XYZW.  Note that 0 and 1 are valid in both, so both masks
	    * can have some bits set.
	    *
	    * We somewhat deviate from the spec here.  It would be really hard
	    * to figure out which component is the error, and there probably
	    * isn't a lot of benefit.
	    */
	   if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
	      yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
		      "components");
	      YYERROR;
	   }

	   $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
	   $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
	      | ($7.negate << 3);
	}
	;

extSwizComp: optionalSign extSwizSel
	{
	   $$ = $2;
	   $$.negate = ($1) ? 1 : 0;
	}
	;

extSwizSel: INTEGER
	{
	   if (($1 != 0) && ($1 != 1)) {
	      yyerror(& @1, state, "invalid extended swizzle selector");
	      YYERROR;
	   }

	   $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;

	   /* 0 and 1 are valid for both RGBA swizzle names and XYZW
	    * swizzle names.
	    */
	   $$.xyzw_valid = 1;
	   $$.rgba_valid = 1;
	}
	| string
	{
	   if (strlen($1) > 1) {
	      yyerror(& @1, state, "invalid extended swizzle selector");
	      YYERROR;
	   }

	   switch ($1[0]) {
	   case 'x':
	      $$.swz = SWIZZLE_X;
	      $$.xyzw_valid = 1;
	      break;
	   case 'y':
	      $$.swz = SWIZZLE_Y;
	      $$.xyzw_valid = 1;
	      break;
	   case 'z':
	      $$.swz = SWIZZLE_Z;
	      $$.xyzw_valid = 1;
	      break;
	   case 'w':
	      $$.swz = SWIZZLE_W;
	      $$.xyzw_valid = 1;
	      break;

	   case 'r':
	      $$.swz = SWIZZLE_X;
	      $$.rgba_valid = 1;
	      break;
	   case 'g':
	      $$.swz = SWIZZLE_Y;
	      $$.rgba_valid = 1;
	      break;
	   case 'b':
	      $$.swz = SWIZZLE_Z;
	      $$.rgba_valid = 1;
	      break;
	   case 'a':
	      $$.swz = SWIZZLE_W;
	      $$.rgba_valid = 1;
	      break;

	   default:
	      yyerror(& @1, state, "invalid extended swizzle selector");
	      YYERROR;
	      break;
	   }
	}
	;

srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
	{
	   struct asm_symbol *const s = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $1);

	   if (s == NULL) {
	      yyerror(& @1, state, "invalid operand variable");
	      YYERROR;
	   } else if ((s->type != at_param) && (s->type != at_temp)
		      && (s->type != at_attrib)) {
	      yyerror(& @1, state, "invalid operand variable");
	      YYERROR;
	   } else if ((s->type == at_param) && s->param_is_array) {
	      yyerror(& @1, state, "non-array access to array PARAM");
	      YYERROR;
	   }

	   init_src_reg(& $$);
	   switch (s->type) {
	   case at_temp:
	      $$.Base.File = PROGRAM_TEMPORARY;
	      $$.Base.Index = s->temp_binding;
	      break;
	   case at_param:
	      $$.Base.File = s->param_binding_type;
	      $$.Base.Index = s->param_binding_begin;
	      break;
	   case at_attrib:
	      $$.Base.File = PROGRAM_INPUT;
	      $$.Base.Index = s->attrib_binding;
	      state->prog->InputsRead |= (1U << $$.Base.Index);

	      if (!validate_inputs(& @1, state)) {
		 YYERROR;
	      }
	      break;

	   default:
	      YYERROR;
	      break;
	   }
	}
	| attribBinding
	{
	   init_src_reg(& $$);
	   $$.Base.File = PROGRAM_INPUT;
	   $$.Base.Index = $1;
	   state->prog->InputsRead |= (1U << $$.Base.Index);

	   if (!validate_inputs(& @1, state)) {
	      YYERROR;
	   }
	}
	| progParamArray '[' progParamArrayMem ']'
	{
	   if (! $3.Base.RelAddr
	       && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
	      yyerror(& @3, state, "out of bounds array access");
	      YYERROR;
	   }

	   init_src_reg(& $$);
	   $$.Base.File = $1->param_binding_type;

	   if ($3.Base.RelAddr) {
	      $1->param_accessed_indirectly = 1;

	      $$.Base.RelAddr = 1;
	      $$.Base.Index = $3.Base.Index;
	      $$.Symbol = $1;
	   } else {
	      $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
	   }
	}
	| paramSingleItemUse
	{
	   init_src_reg(& $$);
	   $$.Base.File = ($1.name != NULL) 
	      ? $1.param_binding_type
	      : PROGRAM_CONSTANT;
	   $$.Base.Index = $1.param_binding_begin;
	}
	;

dstReg: resultBinding
	{
	   init_dst_reg(& $$);
	   $$.File = PROGRAM_OUTPUT;
	   $$.Index = $1;
	}
	| USED_IDENTIFIER /* temporaryReg | vertexResultReg */
	{
	   struct asm_symbol *const s = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $1);

	   if (s == NULL) {
	      yyerror(& @1, state, "invalid operand variable");
	      YYERROR;
	   } else if ((s->type != at_output) && (s->type != at_temp)) {
	      yyerror(& @1, state, "invalid operand variable");
	      YYERROR;
	   }

	   init_dst_reg(& $$);
	   switch (s->type) {
	   case at_temp:
	      $$.File = PROGRAM_TEMPORARY;
	      $$.Index = s->temp_binding;
	      break;
	   case at_output:
	      $$.File = PROGRAM_OUTPUT;
	      $$.Index = s->output_binding;
	      break;
	   default:
	      $$.File = s->param_binding_type;
	      $$.Index = s->param_binding_begin;
	      break;
	   }
	}
	;

progParamArray: USED_IDENTIFIER
	{
	   struct asm_symbol *const s = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $1);

	   if (s == NULL) {
	      yyerror(& @1, state, "invalid operand variable");
	      YYERROR;
	   } else if ((s->type != at_param) || !s->param_is_array) {
	      yyerror(& @1, state, "array access to non-PARAM variable");
	      YYERROR;
	   } else {
	      $$ = s;
	   }
	}
	;

progParamArrayMem: progParamArrayAbs | progParamArrayRel;

progParamArrayAbs: INTEGER
	{
	   init_src_reg(& $$);
	   $$.Base.Index = $1;
	}
	;

progParamArrayRel: addrReg addrComponent addrRegRelOffset
	{
	   /* FINISHME: Add support for multiple address registers.
	    */
	   /* FINISHME: Add support for 4-component address registers.
	    */
	   init_src_reg(& $$);
	   $$.Base.RelAddr = 1;
	   $$.Base.Index = $3;
	}
	;

addrRegRelOffset:              { $$ = 0; }
	| '+' addrRegPosOffset { $$ = $2; }
	| '-' addrRegNegOffset { $$ = -$2; }
	;

addrRegPosOffset: INTEGER
	{
	   if (($1 < 0) || ($1 > 63)) {
              char s[100];
              _mesa_snprintf(s, sizeof(s),
                             "relative address offset too large (%d)", $1);
	      yyerror(& @1, state, s);
	      YYERROR;
	   } else {
	      $$ = $1;
	   }
	}
	;

addrRegNegOffset: INTEGER
	{
	   if (($1 < 0) || ($1 > 64)) {
              char s[100];
              _mesa_snprintf(s, sizeof(s),
                             "relative address offset too large (%d)", $1);
	      yyerror(& @1, state, s);
	      YYERROR;
	   } else {
	      $$ = $1;
	   }
	}
	;

addrReg: USED_IDENTIFIER
	{
	   struct asm_symbol *const s = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $1);

	   if (s == NULL) {
	      yyerror(& @1, state, "invalid array member");
	      YYERROR;
	   } else if (s->type != at_address) {
	      yyerror(& @1, state,
		      "invalid variable for indexed array access");
	      YYERROR;
	   } else {
	      $$ = s;
	   }
	}
	;

addrComponent: MASK1
	{
	   if ($1.mask != WRITEMASK_X) {
	      yyerror(& @1, state, "invalid address component selector");
	      YYERROR;
	   } else {
	      $$ = $1;
	   }
	}
	;

addrWriteMask: MASK1
	{
	   if ($1.mask != WRITEMASK_X) {
	      yyerror(& @1, state,
		      "address register write mask must be \".x\"");
	      YYERROR;
	   } else {
	      $$ = $1;
	   }
	}
	;

scalarSuffix: MASK1;

swizzleSuffix: MASK1
	| MASK4
	| SWIZZLE
	|              { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
	;

optionalMask: MASK4 | MASK3 | MASK2 | MASK1 
	|              { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
	;

optionalCcMask: '(' ccTest ')'
	{
	   $$ = $2;
	}
	| '(' ccTest2 ')'
	{
	   $$ = $2;
	}
	|
	{
	   $$.CondMask = COND_TR;
	   $$.CondSwizzle = SWIZZLE_NOOP;
	   $$.CondSrc = 0;
	}
	;

ccTest: ccMaskRule swizzleSuffix
	{
	   $$ = $1;
	   $$.CondSwizzle = $2.swizzle;
	}
	;

ccTest2: ccMaskRule2 swizzleSuffix
	{
	   $$ = $1;
	   $$.CondSwizzle = $2.swizzle;
	}
	;

ccMaskRule: IDENTIFIER
	{
	   const int cond = _mesa_parse_cc($1);
	   if ((cond == 0) || ($1[2] != '\0')) {
	      char *const err_str =
		 make_error_string("invalid condition code \"%s\"", $1);

	      yyerror(& @1, state, (err_str != NULL)
		      ? err_str : "invalid condition code");

	      if (err_str != NULL) {
		 _mesa_free(err_str);
	      }

	      YYERROR;
	   }

	   $$.CondMask = cond;
	   $$.CondSwizzle = SWIZZLE_NOOP;
	   $$.CondSrc = 0;
	}
	;

ccMaskRule2: USED_IDENTIFIER
	{
	   const int cond = _mesa_parse_cc($1);
	   if ((cond == 0) || ($1[2] != '\0')) {
	      char *const err_str =
		 make_error_string("invalid condition code \"%s\"", $1);

	      yyerror(& @1, state, (err_str != NULL)
		      ? err_str : "invalid condition code");

	      if (err_str != NULL) {
		 _mesa_free(err_str);
	      }

	      YYERROR;
	   }

	   $$.CondMask = cond;
	   $$.CondSwizzle = SWIZZLE_NOOP;
	   $$.CondSrc = 0;
	}
	;

namingStatement: ATTRIB_statement
	| PARAM_statement
	| TEMP_statement
	| ADDRESS_statement
	| OUTPUT_statement
	| ALIAS_statement
	;

ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
	{
	   struct asm_symbol *const s =
	      declare_variable(state, $2, at_attrib, & @2);

	   if (s == NULL) {
	      YYERROR;
	   } else {
	      s->attrib_binding = $4;
	      state->InputsBound |= (1U << s->attrib_binding);

	      if (!validate_inputs(& @4, state)) {
		 YYERROR;
	      }
	   }
	}
	;

attribBinding: VERTEX vtxAttribItem
	{
	   $$ = $2;
	}
	| FRAGMENT fragAttribItem
	{
	   $$ = $2;
	}
	;

vtxAttribItem: POSITION
	{
	   $$ = VERT_ATTRIB_POS;
	}
	| WEIGHT vtxOptWeightNum
	{
	   $$ = VERT_ATTRIB_WEIGHT;
	}
	| NORMAL
	{
	   $$ = VERT_ATTRIB_NORMAL;
	}
	| COLOR optColorType
	{
	   if (!state->ctx->Extensions.EXT_secondary_color) {
	      yyerror(& @2, state, "GL_EXT_secondary_color not supported");
	      YYERROR;
	   }

	   $$ = VERT_ATTRIB_COLOR0 + $2;
	}
	| FOGCOORD
	{
	   if (!state->ctx->Extensions.EXT_fog_coord) {
	      yyerror(& @1, state, "GL_EXT_fog_coord not supported");
	      YYERROR;
	   }

	   $$ = VERT_ATTRIB_FOG;
	}
	| TEXCOORD optTexCoordUnitNum
	{
	   $$ = VERT_ATTRIB_TEX0 + $2;
	}
	| MATRIXINDEX '[' vtxWeightNum ']'
	{
	   yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
	   YYERROR;
	}
	| VTXATTRIB '[' vtxAttribNum ']'
	{
	   $$ = VERT_ATTRIB_GENERIC0 + $3;
	}
	;

vtxAttribNum: INTEGER
	{
	   if ((unsigned) $1 >= state->limits->MaxAttribs) {
	      yyerror(& @1, state, "invalid vertex attribute reference");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

vtxOptWeightNum:  | '[' vtxWeightNum ']';
vtxWeightNum: INTEGER;

fragAttribItem: POSITION
	{
	   $$ = FRAG_ATTRIB_WPOS;
	}
	| COLOR optColorType
	{
	   $$ = FRAG_ATTRIB_COL0 + $2;
	}
	| FOGCOORD
	{
	   $$ = FRAG_ATTRIB_FOGC;
	}
	| TEXCOORD optTexCoordUnitNum
	{
	   $$ = FRAG_ATTRIB_TEX0 + $2;
	}
	;

PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;

PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
	{
	   struct asm_symbol *const s =
	      declare_variable(state, $2, at_param, & @2);

	   if (s == NULL) {
	      YYERROR;
	   } else {
	      s->param_binding_type = $3.param_binding_type;
	      s->param_binding_begin = $3.param_binding_begin;
	      s->param_binding_length = $3.param_binding_length;
	      s->param_is_array = 0;
	   }
	}
	;

PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
	{
	   if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
	      yyerror(& @4, state, 
		      "parameter array size and number of bindings must match");
	      YYERROR;
	   } else {
	      struct asm_symbol *const s =
		 declare_variable(state, $2, $6.type, & @2);

	      if (s == NULL) {
		 YYERROR;
	      } else {
		 s->param_binding_type = $6.param_binding_type;
		 s->param_binding_begin = $6.param_binding_begin;
		 s->param_binding_length = $6.param_binding_length;
		 s->param_is_array = 1;
	      }
	   }
	}
	;

optArraySize:
	{
	   $$ = 0;
	}
	| INTEGER
        {
	   if (($1 < 1) || ((unsigned) $1 >= state->limits->MaxParameters)) {
	      yyerror(& @1, state, "invalid parameter array size");
	      YYERROR;
	   } else {
	      $$ = $1;
	   }
	}
	;

paramSingleInit: '=' paramSingleItemDecl
	{
	   $$ = $2;
	}
	;

paramMultipleInit: '=' '{' paramMultInitList '}'
	{
	   $$ = $3;
	}
	;

paramMultInitList: paramMultipleItem
	| paramMultInitList ',' paramMultipleItem
	{
	   $1.param_binding_length += $3.param_binding_length;
	   $$ = $1;
	}
	;

paramSingleItemDecl: stateSingleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_state(state->prog, & $$, $1);
	}
	| programSingleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_param(state->prog, & $$, $1);
	}
	| paramConstDecl
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_const(state->prog, & $$, & $1);
	}
	;

paramSingleItemUse: stateSingleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_state(state->prog, & $$, $1);
	}
	| programSingleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_param(state->prog, & $$, $1);
	}
	| paramConstUse
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_const(state->prog, & $$, & $1);
	}
	;

paramMultipleItem: stateMultipleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_state(state->prog, & $$, $1);
	}
	| programMultipleItem
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_param(state->prog, & $$, $1);
	}
	| paramConstDecl
	{
	   memset(& $$, 0, sizeof($$));
	   $$.param_binding_begin = ~0;
	   initialize_symbol_from_const(state->prog, & $$, & $1);
	}
	;

stateMultipleItem: stateSingleItem        { memcpy($$, $1, sizeof($$)); }
	| STATE stateMatrixRows           { memcpy($$, $2, sizeof($$)); }
	;

stateSingleItem: STATE stateMaterialItem  { memcpy($$, $2, sizeof($$)); }
	| STATE stateLightItem            { memcpy($$, $2, sizeof($$)); }
	| STATE stateLightModelItem       { memcpy($$, $2, sizeof($$)); }
	| STATE stateLightProdItem        { memcpy($$, $2, sizeof($$)); }
	| STATE stateTexGenItem           { memcpy($$, $2, sizeof($$)); }
	| STATE stateTexEnvItem           { memcpy($$, $2, sizeof($$)); }
	| STATE stateFogItem              { memcpy($$, $2, sizeof($$)); }
	| STATE stateClipPlaneItem        { memcpy($$, $2, sizeof($$)); }
	| STATE statePointItem            { memcpy($$, $2, sizeof($$)); }
	| STATE stateMatrixRow            { memcpy($$, $2, sizeof($$)); }
	| STATE stateDepthItem            { memcpy($$, $2, sizeof($$)); }
	;

stateMaterialItem: MATERIAL optFaceType stateMatProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_MATERIAL;
	   $$[1] = $2;
	   $$[2] = $3;
	}
	;

stateMatProperty: ambDiffSpecProperty
	{
	   $$ = $1;
	}
	| EMISSION
	{
	   $$ = STATE_EMISSION;
	}
	| SHININESS
	{
	   $$ = STATE_SHININESS;
	}
	;

stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_LIGHT;
	   $$[1] = $3;
	   $$[2] = $5;
	}
	;

stateLightProperty: ambDiffSpecProperty
	{
	   $$ = $1;
	}
	| POSITION
	{
	   $$ = STATE_POSITION;
	}
	| ATTENUATION
	{
	   if (!state->ctx->Extensions.EXT_point_parameters) {
	      yyerror(& @1, state, "GL_ARB_point_parameters not supported");
	      YYERROR;
	   }

	   $$ = STATE_ATTENUATION;
	}
	| SPOT stateSpotProperty
	{
	   $$ = $2;
	}
	| HALF
	{
	   $$ = STATE_HALF_VECTOR;
	}
	;

stateSpotProperty: DIRECTION
	{
	   $$ = STATE_SPOT_DIRECTION;
	}
	;

stateLightModelItem: LIGHTMODEL stateLModProperty
	{
	   $$[0] = $2[0];
	   $$[1] = $2[1];
	}
	;

stateLModProperty: AMBIENT
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_LIGHTMODEL_AMBIENT;
	}
	| optFaceType SCENECOLOR
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
	   $$[1] = $1;
	}
	;

stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_LIGHTPROD;
	   $$[1] = $3;
	   $$[2] = $5;
	   $$[3] = $6;
	}
	;

stateLProdProperty: ambDiffSpecProperty;

stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = $3;
	   $$[1] = $2;
	}
	;

stateTexEnvProperty: COLOR
	{
	   $$ = STATE_TEXENV_COLOR;
	}
	;

ambDiffSpecProperty: AMBIENT
	{
	   $$ = STATE_AMBIENT;
	}
	| DIFFUSE
	{
	   $$ = STATE_DIFFUSE;
	}
	| SPECULAR
	{
	   $$ = STATE_SPECULAR;
	}
	;

stateLightNumber: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxLights) {
	      yyerror(& @1, state, "invalid light selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_TEXGEN;
	   $$[1] = $2;
	   $$[2] = $3 + $4;
	}
	;

stateTexGenType: EYE
	{
	   $$ = STATE_TEXGEN_EYE_S;
	}
	| OBJECT
	{
	   $$ = STATE_TEXGEN_OBJECT_S;
	}
	;
stateTexGenCoord: TEXGEN_S
	{
	   $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
	}
	| TEXGEN_T
	{
	   $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
	}
	| TEXGEN_R
	{
	   $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
	}
	| TEXGEN_Q
	{
	   $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
	}
	;

stateFogItem: FOG stateFogProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = $2;
	}
	;

stateFogProperty: COLOR
	{
	   $$ = STATE_FOG_COLOR;
	}
	| PARAMS
	{
	   $$ = STATE_FOG_PARAMS;
	}
	;

stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_CLIPPLANE;
	   $$[1] = $3;
	}
	;

stateClipPlaneNum: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxClipPlanes) {
	      yyerror(& @1, state, "invalid clip plane selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

statePointItem: POINT_TOK statePointProperty
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = $2;
	}
	;

statePointProperty: SIZE_TOK
	{
	   $$ = STATE_POINT_SIZE;
	}
	| ATTENUATION
	{
	   $$ = STATE_POINT_ATTENUATION;
	}
	;

stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
	{
	   $$[0] = $1[0];
	   $$[1] = $1[1];
	   $$[2] = $4;
	   $$[3] = $4;
	   $$[4] = $1[2];
	}
	;

stateMatrixRows: stateMatrixItem optMatrixRows
	{
	   $$[0] = $1[0];
	   $$[1] = $1[1];
	   $$[2] = $2[2];
	   $$[3] = $2[3];
	   $$[4] = $1[2];
	}
	;

optMatrixRows:
	{
	   $$[2] = 0;
	   $$[3] = 3;
	}
	| ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
	{
	   /* It seems logical that the matrix row range specifier would have
	    * to specify a range or more than one row (i.e., $5 > $3).
	    * However, the ARB_vertex_program spec says "a program will fail
	    * to load if <a> is greater than <b>."  This means that $3 == $5
	    * is valid.
	    */
	   if ($3 > $5) {
	      yyerror(& @3, state, "invalid matrix row range");
	      YYERROR;
	   }

	   $$[2] = $3;
	   $$[3] = $5;
	}
	;

stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
	{
	   $$[0] = $2[0];
	   $$[1] = $2[1];
	   $$[2] = $3;
	}
	;

stateOptMatModifier: 
	{
	   $$ = 0;
	}
	| stateMatModifier
	{
	   $$ = $1;
	}
	;

stateMatModifier: INVERSE 
	{
	   $$ = STATE_MATRIX_INVERSE;
	}
	| TRANSPOSE 
	{
	   $$ = STATE_MATRIX_TRANSPOSE;
	}
	| INVTRANS
	{
	   $$ = STATE_MATRIX_INVTRANS;
	}
	;

stateMatrixRowNum: INTEGER
	{
	   if ($1 > 3) {
	      yyerror(& @1, state, "invalid matrix row reference");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

stateMatrixName: MODELVIEW stateOptModMatNum
	{
	   $$[0] = STATE_MODELVIEW_MATRIX;
	   $$[1] = $2;
	}
	| PROJECTION
	{
	   $$[0] = STATE_PROJECTION_MATRIX;
	   $$[1] = 0;
	}
	| MVP
	{
	   $$[0] = STATE_MVP_MATRIX;
	   $$[1] = 0;
	}
	| TEXTURE optTexCoordUnitNum
	{
	   $$[0] = STATE_TEXTURE_MATRIX;
	   $$[1] = $2;
	}
	| PALETTE '[' statePaletteMatNum ']'
	{
	   yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
	   YYERROR;
	}
	| MAT_PROGRAM '[' stateProgramMatNum ']'
	{
	   $$[0] = STATE_PROGRAM_MATRIX;
	   $$[1] = $3;
	}
	;

stateOptModMatNum:
	{
	   $$ = 0;
	}
	| '[' stateModMatNum ']'
	{
	   $$ = $2;
	}
	;
stateModMatNum: INTEGER
	{
	   /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
	    * zero is valid.
	    */
	   if ($1 != 0) {
	      yyerror(& @1, state, "invalid modelview matrix index");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;
statePaletteMatNum: INTEGER
	{
	   /* Since GL_ARB_matrix_palette isn't supported, just let any value
	    * through here.  The error will be generated later.
	    */
	   $$ = $1;
	}
	;
stateProgramMatNum: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxProgramMatrices) {
	      yyerror(& @1, state, "invalid program matrix selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

stateDepthItem: DEPTH RANGE
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = STATE_DEPTH_RANGE;
	}
	;


programSingleItem: progEnvParam | progLocalParam;

programMultipleItem: progEnvParams | progLocalParams;

progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = state->state_param_enum;
	   $$[1] = STATE_ENV;
	   $$[2] = $4[0];
	   $$[3] = $4[1];
	}
	;

progEnvParamNums: progEnvParamNum
	{
	   $$[0] = $1;
	   $$[1] = $1;
	}
	| progEnvParamNum DOT_DOT progEnvParamNum
	{
	   $$[0] = $1;
	   $$[1] = $3;
	}
	;

progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = state->state_param_enum;
	   $$[1] = STATE_ENV;
	   $$[2] = $4;
	   $$[3] = $4;
	}
	;

progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = state->state_param_enum;
	   $$[1] = STATE_LOCAL;
	   $$[2] = $4[0];
	   $$[3] = $4[1];
	}

progLocalParamNums: progLocalParamNum
	{
	   $$[0] = $1;
	   $$[1] = $1;
	}
	| progLocalParamNum DOT_DOT progLocalParamNum
	{
	   $$[0] = $1;
	   $$[1] = $3;
	}
	;

progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
	{
	   memset($$, 0, sizeof($$));
	   $$[0] = state->state_param_enum;
	   $$[1] = STATE_LOCAL;
	   $$[2] = $4;
	   $$[3] = $4;
	}
	;

progEnvParamNum: INTEGER
	{
	   if ((unsigned) $1 >= state->limits->MaxEnvParams) {
	      yyerror(& @1, state, "invalid environment parameter reference");
	      YYERROR;
	   }
	   $$ = $1;
	}
	;

progLocalParamNum: INTEGER
	{
	   if ((unsigned) $1 >= state->limits->MaxLocalParams) {
	      yyerror(& @1, state, "invalid local parameter reference");
	      YYERROR;
	   }
	   $$ = $1;
	}
	;



paramConstDecl: paramConstScalarDecl | paramConstVector;
paramConstUse: paramConstScalarUse | paramConstVector;

paramConstScalarDecl: signedFloatConstant
	{
	   $$.count = 4;
	   $$.data[0] = $1;
	   $$.data[1] = $1;
	   $$.data[2] = $1;
	   $$.data[3] = $1;
	}
	;

paramConstScalarUse: REAL
	{
	   $$.count = 1;
	   $$.data[0] = $1;
	   $$.data[1] = $1;
	   $$.data[2] = $1;
	   $$.data[3] = $1;
	}
	| INTEGER
	{
	   $$.count = 1;
	   $$.data[0] = (float) $1;
	   $$.data[1] = (float) $1;
	   $$.data[2] = (float) $1;
	   $$.data[3] = (float) $1;
	}
	;

paramConstVector: '{' signedFloatConstant '}'
	{
	   $$.count = 4;
	   $$.data[0] = $2;
	   $$.data[1] = 0.0f;
	   $$.data[2] = 0.0f;
	   $$.data[3] = 1.0f;
	}
	| '{' signedFloatConstant ',' signedFloatConstant '}'
	{
	   $$.count = 4;
	   $$.data[0] = $2;
	   $$.data[1] = $4;
	   $$.data[2] = 0.0f;
	   $$.data[3] = 1.0f;
	}
	| '{' signedFloatConstant ',' signedFloatConstant ','
              signedFloatConstant '}'
	{
	   $$.count = 4;
	   $$.data[0] = $2;
	   $$.data[1] = $4;
	   $$.data[2] = $6;
	   $$.data[3] = 1.0f;
	}
	| '{' signedFloatConstant ',' signedFloatConstant ','
              signedFloatConstant ',' signedFloatConstant '}'
	{
	   $$.count = 4;
	   $$.data[0] = $2;
	   $$.data[1] = $4;
	   $$.data[2] = $6;
	   $$.data[3] = $8;
	}
	;

signedFloatConstant: optionalSign REAL
	{
	   $$ = ($1) ? -$2 : $2;
	}
	| optionalSign INTEGER
	{
	   $$ = (float)(($1) ? -$2 : $2);
	}
	;

optionalSign: '+'        { $$ = FALSE; }
	| '-'            { $$ = TRUE;  }
	|                { $$ = FALSE; }
	;

TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
	;

optVarSize: string
	{
	   /* NV_fragment_program_option defines the size qualifiers in a
	    * fairly broken way.  "SHORT" or "LONG" can optionally be used
	    * before TEMP or OUTPUT.  However, neither is a reserved word!
	    * This means that we have to parse it as an identifier, then check
	    * to make sure it's one of the valid values.  *sigh*
	    *
	    * In addition, the grammar in the extension spec does *not* allow
	    * the size specifier to be optional, but all known implementations
	    * do.
	    */
	   if (!state->option.NV_fragment) {
	      yyerror(& @1, state, "unexpected IDENTIFIER");
	      YYERROR;
	   }

	   if (strcmp("SHORT", $1) == 0) {
	   } else if (strcmp("LONG", $1) == 0) {
	   } else {
	      char *const err_str =
		 make_error_string("invalid storage size specifier \"%s\"",
				   $1);

	      yyerror(& @1, state, (err_str != NULL)
		      ? err_str : "invalid storage size specifier");

	      if (err_str != NULL) {
		 _mesa_free(err_str);
	      }

	      YYERROR;
	   }
	}
	|
	{
	}
	;

ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
	;

varNameList: varNameList ',' IDENTIFIER
	{
	   if (!declare_variable(state, $3, $<integer>0, & @3)) {
	      YYERROR;
	   }
	}
	| IDENTIFIER
	{
	   if (!declare_variable(state, $1, $<integer>0, & @1)) {
	      YYERROR;
	   }
	}
	;

OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
	{
	   struct asm_symbol *const s =
	      declare_variable(state, $3, at_output, & @3);

	   if (s == NULL) {
	      YYERROR;
	   } else {
	      s->output_binding = $5;
	   }
	}
	;

resultBinding: RESULT POSITION
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_HPOS;
	   } else {
	      yyerror(& @2, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| RESULT FOGCOORD
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_FOGC;
	   } else {
	      yyerror(& @2, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| RESULT resultColBinding
	{
	   $$ = $2;
	}
	| RESULT POINTSIZE
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_PSIZ;
	   } else {
	      yyerror(& @2, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| RESULT TEXCOORD optTexCoordUnitNum
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_TEX0 + $3;
	   } else {
	      yyerror(& @2, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| RESULT DEPTH
	{
	   if (state->mode == ARB_fragment) {
	      $$ = FRAG_RESULT_DEPTH;
	   } else {
	      yyerror(& @2, state, "invalid program result name");
	      YYERROR;
	   }
	}
	;

resultColBinding: COLOR optResultFaceType optResultColorType
	{
	   $$ = $2 + $3;
	}
	;

optResultFaceType:
	{
	   $$ = (state->mode == ARB_vertex)
	      ? VERT_RESULT_COL0
	      : FRAG_RESULT_COLOR;
	}
	| FRONT
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_COL0;
	   } else {
	      yyerror(& @1, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| BACK
	{
	   if (state->mode == ARB_vertex) {
	      $$ = VERT_RESULT_BFC0;
	   } else {
	      yyerror(& @1, state, "invalid program result name");
	      YYERROR;
	   }
	}
	;

optResultColorType:
	{
	   $$ = 0; 
	}
	| PRIMARY
	{
	   if (state->mode == ARB_vertex) {
	      $$ = 0;
	   } else {
	      yyerror(& @1, state, "invalid program result name");
	      YYERROR;
	   }
	}
	| SECONDARY
	{
	   if (state->mode == ARB_vertex) {
	      $$ = 1;
	   } else {
	      yyerror(& @1, state, "invalid program result name");
	      YYERROR;
	   }
	}
	;

optFaceType:    { $$ = 0; }
	| FRONT	{ $$ = 0; }
	| BACK  { $$ = 1; }
	;

optColorType:       { $$ = 0; }
	| PRIMARY   { $$ = 0; }
	| SECONDARY { $$ = 1; }
	;

optTexCoordUnitNum:                { $$ = 0; }
	| '[' texCoordUnitNum ']'  { $$ = $2; }
	;

optTexImageUnitNum:                { $$ = 0; }
	| '[' texImageUnitNum ']'  { $$ = $2; }
	;

optLegacyTexUnitNum:               { $$ = 0; }
	| '[' legacyTexUnitNum ']' { $$ = $2; }
	;

texCoordUnitNum: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
	      yyerror(& @1, state, "invalid texture coordinate unit selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

texImageUnitNum: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxTextureImageUnits) {
	      yyerror(& @1, state, "invalid texture image unit selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

legacyTexUnitNum: INTEGER
	{
	   if ((unsigned) $1 >= state->MaxTextureUnits) {
	      yyerror(& @1, state, "invalid texture unit selector");
	      YYERROR;
	   }

	   $$ = $1;
	}
	;

ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
	{
	   struct asm_symbol *exist = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $2);
	   struct asm_symbol *target = (struct asm_symbol *)
	      _mesa_symbol_table_find_symbol(state->st, 0, $4);


	   if (exist != NULL) {
	      yyerror(& @2, state, "redeclared identifier");
	      YYERROR;
	   } else if (target == NULL) {
	      yyerror(& @4, state,
		      "undefined variable binding in ALIAS statement");
	      YYERROR;
	   } else {
	      _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
	   }
	}
	;

string: IDENTIFIER
	| USED_IDENTIFIER
	;

%%

void
asm_instruction_set_operands(struct asm_instruction *inst,
			     const struct prog_dst_register *dst,
			     const struct asm_src_register *src0,
			     const struct asm_src_register *src1,
			     const struct asm_src_register *src2)
{
   /* In the core ARB extensions only the KIL instruction doesn't have a
    * destination register.
    */
   if (dst == NULL) {
      init_dst_reg(& inst->Base.DstReg);
   } else {
      inst->Base.DstReg = *dst;
   }

   /* The only instruction that doesn't have any source registers is the
    * condition-code based KIL instruction added by NV_fragment_program_option.
    */
   if (src0 != NULL) {
      inst->Base.SrcReg[0] = src0->Base;
      inst->SrcReg[0] = *src0;
   } else {
      init_src_reg(& inst->SrcReg[0]);
   }

   if (src1 != NULL) {
      inst->Base.SrcReg[1] = src1->Base;
      inst->SrcReg[1] = *src1;
   } else {
      init_src_reg(& inst->SrcReg[1]);
   }

   if (src2 != NULL) {
      inst->Base.SrcReg[2] = src2->Base;
      inst->SrcReg[2] = *src2;
   } else {
      init_src_reg(& inst->SrcReg[2]);
   }
}


struct asm_instruction *
asm_instruction_ctor(gl_inst_opcode op,
		     const struct prog_dst_register *dst,
		     const struct asm_src_register *src0,
		     const struct asm_src_register *src1,
		     const struct asm_src_register *src2)
{
   struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);

   if (inst) {
      _mesa_init_instructions(& inst->Base, 1);
      inst->Base.Opcode = op;

      asm_instruction_set_operands(inst, dst, src0, src1, src2);
   }

   return inst;
}


struct asm_instruction *
asm_instruction_copy_ctor(const struct prog_instruction *base,
			  const struct prog_dst_register *dst,
			  const struct asm_src_register *src0,
			  const struct asm_src_register *src1,
			  const struct asm_src_register *src2)
{
   struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);

   if (inst) {
      _mesa_init_instructions(& inst->Base, 1);
      inst->Base.Opcode = base->Opcode;
      inst->Base.CondUpdate = base->CondUpdate;
      inst->Base.CondDst = base->CondDst;
      inst->Base.SaturateMode = base->SaturateMode;
      inst->Base.Precision = base->Precision;

      asm_instruction_set_operands(inst, dst, src0, src1, src2);
   }

   return inst;
}


void
init_dst_reg(struct prog_dst_register *r)
{
   memset(r, 0, sizeof(*r));
   r->File = PROGRAM_UNDEFINED;
   r->WriteMask = WRITEMASK_XYZW;
   r->CondMask = COND_TR;
   r->CondSwizzle = SWIZZLE_NOOP;
}


void
init_src_reg(struct asm_src_register *r)
{
   memset(r, 0, sizeof(*r));
   r->Base.File = PROGRAM_UNDEFINED;
   r->Base.Swizzle = SWIZZLE_NOOP;
   r->Symbol = NULL;
}


/**
 * Validate the set of inputs used by a program
 *
 * Validates that legal sets of inputs are used by the program.  In this case
 * "used" included both reading the input or binding the input to a name using
 * the \c ATTRIB command.
 *
 * \return
 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
 */
int
validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
{
   const int inputs = state->prog->InputsRead | state->InputsBound;

   if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
      yyerror(locp, state, "illegal use of generic attribute and name attribute");
      return 0;
   }

   return 1;
}


struct asm_symbol *
declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
		 struct YYLTYPE *locp)
{
   struct asm_symbol *s = NULL;
   struct asm_symbol *exist = (struct asm_symbol *)
      _mesa_symbol_table_find_symbol(state->st, 0, name);


   if (exist != NULL) {
      yyerror(locp, state, "redeclared identifier");
   } else {
      s = calloc(1, sizeof(struct asm_symbol));
      s->name = name;
      s->type = t;

      switch (t) {
      case at_temp:
	 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
	    yyerror(locp, state, "too many temporaries declared");
	    free(s);
	    return NULL;
	 }

	 s->temp_binding = state->prog->NumTemporaries;
	 state->prog->NumTemporaries++;
	 break;

      case at_address:
	 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
	    yyerror(locp, state, "too many address registers declared");
	    free(s);
	    return NULL;
	 }

	 /* FINISHME: Add support for multiple address registers.
	  */
	 state->prog->NumAddressRegs++;
	 break;

      default:
	 break;
      }

      _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
      s->next = state->sym;
      state->sym = s;
   }

   return s;
}


int add_state_reference(struct gl_program_parameter_list *param_list,
			const gl_state_index tokens[STATE_LENGTH])
{
   const GLuint size = 4; /* XXX fix */
   char *name;
   GLint index;

   name = _mesa_program_state_string(tokens);
   index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
                               size, GL_NONE, NULL, tokens, 0x0);
   param_list->StateFlags |= _mesa_program_state_flags(tokens);

   /* free name string here since we duplicated it in add_parameter() */
   _mesa_free(name);

   return index;
}


int
initialize_symbol_from_state(struct gl_program *prog,
			     struct asm_symbol *param_var, 
			     const gl_state_index tokens[STATE_LENGTH])
{
   int idx = -1;
   gl_state_index state_tokens[STATE_LENGTH];


   memcpy(state_tokens, tokens, sizeof(state_tokens));

   param_var->type = at_param;
   param_var->param_binding_type = PROGRAM_STATE_VAR;

   /* If we are adding a STATE_MATRIX that has multiple rows, we need to
    * unroll it and call add_state_reference() for each row
    */
   if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
	state_tokens[0] == STATE_PROJECTION_MATRIX ||
	state_tokens[0] == STATE_MVP_MATRIX ||
	state_tokens[0] == STATE_TEXTURE_MATRIX ||
	state_tokens[0] == STATE_PROGRAM_MATRIX)
       && (state_tokens[2] != state_tokens[3])) {
      int row;
      const int first_row = state_tokens[2];
      const int last_row = state_tokens[3];

      for (row = first_row; row <= last_row; row++) {
	 state_tokens[2] = state_tokens[3] = row;

	 idx = add_state_reference(prog->Parameters, state_tokens);
	 if (param_var->param_binding_begin == ~0U)
	    param_var->param_binding_begin = idx;
	 param_var->param_binding_length++;
      }
   }
   else {
      idx = add_state_reference(prog->Parameters, state_tokens);
      if (param_var->param_binding_begin == ~0U)
	 param_var->param_binding_begin = idx;
      param_var->param_binding_length++;
   }

   return idx;
}


int
initialize_symbol_from_param(struct gl_program *prog,
			     struct asm_symbol *param_var, 
			     const gl_state_index tokens[STATE_LENGTH])
{
   int idx = -1;
   gl_state_index state_tokens[STATE_LENGTH];


   memcpy(state_tokens, tokens, sizeof(state_tokens));

   assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
	  || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
   assert((state_tokens[1] == STATE_ENV)
	  || (state_tokens[1] == STATE_LOCAL));

   param_var->type = at_param;
   param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
     ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;

   /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
    * we need to unroll it and call add_state_reference() for each row
    */
   if (state_tokens[2] != state_tokens[3]) {
      int row;
      const int first_row = state_tokens[2];
      const int last_row = state_tokens[3];

      for (row = first_row; row <= last_row; row++) {
	 state_tokens[2] = state_tokens[3] = row;

	 idx = add_state_reference(prog->Parameters, state_tokens);
	 if (param_var->param_binding_begin == ~0U)
	    param_var->param_binding_begin = idx;
	 param_var->param_binding_length++;
      }
   }
   else {
      idx = add_state_reference(prog->Parameters, state_tokens);
      if (param_var->param_binding_begin == ~0U)
	 param_var->param_binding_begin = idx;
      param_var->param_binding_length++;
   }

   return idx;
}


int
initialize_symbol_from_const(struct gl_program *prog,
			     struct asm_symbol *param_var, 
			     const struct asm_vector *vec)
{
   const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
				       NULL, vec->count, GL_NONE, vec->data,
				       NULL, 0x0);

   param_var->type = at_param;
   param_var->param_binding_type = PROGRAM_CONSTANT;

   if (param_var->param_binding_begin == ~0U)
      param_var->param_binding_begin = idx;
   param_var->param_binding_length++;

   return idx;
}


char *
make_error_string(const char *fmt, ...)
{
   int length;
   char *str;
   va_list args;

   va_start(args, fmt);

   /* Call vsnprintf once to determine how large the final string is.  Call it
    * again to do the actual formatting.  from the vsnprintf manual page:
    *
    *    Upon successful return, these functions return the number of
    *    characters printed  (not including the trailing '\0' used to end
    *    output to strings).
    */
   length = 1 + vsnprintf(NULL, 0, fmt, args);

   str = _mesa_malloc(length);
   if (str) {
      vsnprintf(str, length, fmt, args);
   }

   va_end(args);

   return str;
}


void
yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
{
   char *err_str;


   err_str = make_error_string("glProgramStringARB(%s)\n", s);
   if (err_str) {
      _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
      _mesa_free(err_str);
   }

   err_str = make_error_string("line %u, char %u: error: %s\n",
			       locp->first_line, locp->first_column, s);
   _mesa_set_program_error(state->ctx, locp->position, err_str);

   if (err_str) {
      _mesa_free(err_str);
   }
}


GLboolean
_mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
			GLsizei len, struct asm_parser_state *state)
{
   struct asm_instruction *inst;
   unsigned i;
   GLubyte *strz;
   GLboolean result = GL_FALSE;
   void *temp;
   struct asm_symbol *sym;

   state->ctx = ctx;
   state->prog->Target = target;
   state->prog->Parameters = _mesa_new_parameter_list();

   /* Make a copy of the program string and force it to be NUL-terminated.
    */
   strz = (GLubyte *) _mesa_malloc(len + 1);
   if (strz == NULL) {
      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
      return GL_FALSE;
   }
   _mesa_memcpy (strz, str, len);
   strz[len] = '\0';

   state->prog->String = strz;

   state->st = _mesa_symbol_table_ctor();

   state->limits = (target == GL_VERTEX_PROGRAM_ARB)
      ? & ctx->Const.VertexProgram
      : & ctx->Const.FragmentProgram;

   state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
   state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
   state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
   state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
   state->MaxLights = ctx->Const.MaxLights;
   state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;

   state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
      ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;

   _mesa_set_program_error(ctx, -1, NULL);

   _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
   yyparse(state);
   _mesa_program_lexer_dtor(state->scanner);


   if (ctx->Program.ErrorPos != -1) {
      goto error;
   }

   if (! _mesa_layout_parameters(state)) {
      struct YYLTYPE loc;

      loc.first_line = 0;
      loc.first_column = 0;
      loc.position = len;

      yyerror(& loc, state, "invalid PARAM usage");
      goto error;
   }


   
   /* Add one instruction to store the "END" instruction.
    */
   state->prog->Instructions =
      _mesa_alloc_instructions(state->prog->NumInstructions + 1);
   inst = state->inst_head;
   for (i = 0; i < state->prog->NumInstructions; i++) {
      struct asm_instruction *const temp = inst->next;

      state->prog->Instructions[i] = inst->Base;
      inst = temp;
   }

   /* Finally, tag on an OPCODE_END instruction */
   {
      const GLuint numInst = state->prog->NumInstructions;
      _mesa_init_instructions(state->prog->Instructions + numInst, 1);
      state->prog->Instructions[numInst].Opcode = OPCODE_END;
   }
   state->prog->NumInstructions++;

   state->prog->NumParameters = state->prog->Parameters->NumParameters;
   state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);

   /*
    * Initialize native counts to logical counts.  The device driver may
    * change them if program is translated into a hardware program.
    */
   state->prog->NumNativeInstructions = state->prog->NumInstructions;
   state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
   state->prog->NumNativeParameters = state->prog->NumParameters;
   state->prog->NumNativeAttributes = state->prog->NumAttributes;
   state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;

   result = GL_TRUE;

error:
   for (inst = state->inst_head; inst != NULL; inst = temp) {
      temp = inst->next;
      _mesa_free(inst);
   }

   state->inst_head = NULL;
   state->inst_tail = NULL;

   for (sym = state->sym; sym != NULL; sym = temp) {
      temp = sym->next;

      _mesa_free((void *) sym->name);
      _mesa_free(sym);
   }
   state->sym = NULL;

   _mesa_symbol_table_dtor(state->st);
   state->st = NULL;

   return result;
}