1%{
2/*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdarg.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "main/errors.h"
31#include "main/mtypes.h"
32#include "main/imports.h"
33#include "program/program.h"
34#include "program/prog_parameter.h"
35#include "program/prog_parameter_layout.h"
36#include "program/prog_statevars.h"
37#include "program/prog_instruction.h"
38
39#include "program/symbol_table.h"
40#include "program/program_parser.h"
41
42#include "util/u_math.h"
43
44extern void *yy_scan_string(char *);
45extern void yy_delete_buffer(void *);
46
47static struct asm_symbol *declare_variable(struct asm_parser_state *state,
48    char *name, enum asm_type t, struct YYLTYPE *locp);
49
50static int add_state_reference(struct gl_program_parameter_list *param_list,
51    const gl_state_index16 tokens[STATE_LENGTH]);
52
53static int initialize_symbol_from_state(struct gl_program *prog,
54    struct asm_symbol *param_var, const gl_state_index16 tokens[STATE_LENGTH]);
55
56static int initialize_symbol_from_param(struct gl_program *prog,
57    struct asm_symbol *param_var, const gl_state_index16 tokens[STATE_LENGTH]);
58
59static int initialize_symbol_from_const(struct gl_program *prog,
60    struct asm_symbol *param_var, const struct asm_vector *vec,
61    GLboolean allowSwizzle);
62
63static int yyparse(struct asm_parser_state *state);
64
65static char *make_error_string(const char *fmt, ...);
66
67static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
68    const char *s);
69
70static int validate_inputs(struct YYLTYPE *locp,
71    struct asm_parser_state *state);
72
73static void init_dst_reg(struct prog_dst_register *r);
74
75static void set_dst_reg(struct prog_dst_register *r,
76                        gl_register_file file, GLint index);
77
78static void init_src_reg(struct asm_src_register *r);
79
80static void set_src_reg(struct asm_src_register *r,
81                        gl_register_file file, GLint index);
82
83static void set_src_reg_swz(struct asm_src_register *r,
84                            gl_register_file file, GLint index, GLuint swizzle);
85
86static void asm_instruction_set_operands(struct asm_instruction *inst,
87    const struct prog_dst_register *dst, const struct asm_src_register *src0,
88    const struct asm_src_register *src1, const struct asm_src_register *src2);
89
90static struct asm_instruction *asm_instruction_ctor(enum prog_opcode op,
91    const struct prog_dst_register *dst, const struct asm_src_register *src0,
92    const struct asm_src_register *src1, const struct asm_src_register *src2);
93
94static struct asm_instruction *asm_instruction_copy_ctor(
95    const struct prog_instruction *base, const struct prog_dst_register *dst,
96    const struct asm_src_register *src0, const struct asm_src_register *src1,
97    const struct asm_src_register *src2);
98
99#ifndef FALSE
100#define FALSE 0
101#define TRUE (!FALSE)
102#endif
103
104#define YYLLOC_DEFAULT(Current, Rhs, N)					\
105   do {									\
106      if (N) {							\
107	 (Current).first_line = YYRHSLOC(Rhs, 1).first_line;		\
108	 (Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
109	 (Current).position = YYRHSLOC(Rhs, 1).position;		\
110	 (Current).last_line = YYRHSLOC(Rhs, N).last_line;		\
111	 (Current).last_column = YYRHSLOC(Rhs, N).last_column;		\
112      } else {								\
113	 (Current).first_line = YYRHSLOC(Rhs, 0).last_line;		\
114	 (Current).last_line = (Current).first_line;			\
115	 (Current).first_column = YYRHSLOC(Rhs, 0).last_column;		\
116	 (Current).last_column = (Current).first_column;		\
117	 (Current).position = YYRHSLOC(Rhs, 0).position			\
118	    + (Current).first_column;					\
119      }									\
120   } while(0)
121%}
122
123%pure-parser
124%locations
125%lex-param   { struct asm_parser_state *state }
126%parse-param { struct asm_parser_state *state }
127%error-verbose
128
129%union {
130   struct asm_instruction *inst;
131   struct asm_symbol *sym;
132   struct asm_symbol temp_sym;
133   struct asm_swizzle_mask swiz_mask;
134   struct asm_src_register src_reg;
135   struct prog_dst_register dst_reg;
136   struct prog_instruction temp_inst;
137   char *string;
138   unsigned result;
139   unsigned attrib;
140   int integer;
141   float real;
142   gl_state_index16 state[STATE_LENGTH];
143   int negate;
144   struct asm_vector vector;
145   enum prog_opcode opcode;
146
147   struct {
148      unsigned swz;
149      unsigned rgba_valid:1;
150      unsigned xyzw_valid:1;
151      unsigned negate:1;
152   } ext_swizzle;
153}
154
155%token ARBvp_10 ARBfp_10
156
157/* Tokens for assembler pseudo-ops */
158%token <integer> ADDRESS
159%token ALIAS ATTRIB
160%token OPTION OUTPUT
161%token PARAM
162%token <integer> TEMP
163%token END
164
165 /* Tokens for instructions */
166%token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
167%token <temp_inst> ARL KIL SWZ TXD_OP
168
169%token <integer> INTEGER
170%token <real> REAL
171
172%token AMBIENT ATTENUATION
173%token BACK
174%token CLIP COLOR
175%token DEPTH DIFFUSE DIRECTION
176%token EMISSION ENV EYE
177%token FOG FOGCOORD FRAGMENT FRONT
178%token HALF
179%token INVERSE INVTRANS
180%token LIGHT LIGHTMODEL LIGHTPROD LOCAL
181%token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
182%token NORMAL
183%token OBJECT
184%token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
185%token RANGE RESULT ROW
186%token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
187%token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
188%token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
189%token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
190%token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
191%token VERTEX VTXATTRIB
192
193%token <string> IDENTIFIER USED_IDENTIFIER
194%type <string> string
195%token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
196%token DOT_DOT
197%token DOT
198
199%type <inst> instruction ALU_instruction TexInstruction
200%type <inst> ARL_instruction VECTORop_instruction
201%type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
202%type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
203%type <inst> KIL_instruction
204
205%type <dst_reg> dstReg maskedDstReg maskedAddrReg
206%type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
207%type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
208%type <ext_swizzle> extSwizComp extSwizSel
209%type <swiz_mask> optionalMask
210
211%type <sym> progParamArray
212%type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
213%type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
214%type <sym> addrReg
215%type <swiz_mask> addrComponent addrWriteMask
216
217%type <result> resultBinding resultColBinding
218%type <integer> optFaceType optColorType
219%type <integer> optResultFaceType optResultColorType
220
221%type <integer> optTexImageUnitNum texImageUnitNum
222%type <integer> optTexCoordUnitNum texCoordUnitNum
223%type <integer> optLegacyTexUnitNum legacyTexUnitNum
224%type <integer> texImageUnit texTarget
225%type <integer> vtxAttribNum
226
227%type <attrib> attribBinding vtxAttribItem fragAttribItem
228
229%type <temp_sym> paramSingleInit paramSingleItemDecl
230%type <integer> optArraySize
231
232%type <state> stateSingleItem stateMultipleItem
233%type <state> stateMaterialItem
234%type <state> stateLightItem stateLightModelItem stateLightProdItem
235%type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
236%type <state> stateMatrixItem stateMatrixRow stateMatrixRows
237%type <state> stateTexEnvItem stateDepthItem
238
239%type <state> stateLModProperty
240%type <state> stateMatrixName optMatrixRows
241
242%type <integer> stateMatProperty
243%type <integer> stateLightProperty stateSpotProperty
244%type <integer> stateLightNumber stateLProdProperty
245%type <integer> stateTexGenType stateTexGenCoord
246%type <integer> stateTexEnvProperty
247%type <integer> stateFogProperty
248%type <integer> stateClipPlaneNum
249%type <integer> statePointProperty
250
251%type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
252%type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
253%type <integer> stateProgramMatNum
254
255%type <integer> ambDiffSpecProperty
256
257%type <state> programSingleItem progEnvParam progLocalParam
258%type <state> programMultipleItem progEnvParams progLocalParams
259
260%type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
261%type <temp_sym> paramSingleItemUse
262
263%type <integer> progEnvParamNum progLocalParamNum
264%type <state> progEnvParamNums progLocalParamNums
265
266%type <vector> paramConstDecl paramConstUse
267%type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
268%type <real> signedFloatConstant
269%type <negate> optionalSign
270
271%{
272extern int
273_mesa_program_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
274                        void *yyscanner);
275
276static int
277yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
278      struct asm_parser_state *state)
279{
280   return _mesa_program_lexer_lex(yylval_param, yylloc_param, state->scanner);
281}
282%}
283
284%%
285
286program: language optionSequence statementSequence END
287	;
288
289language: ARBvp_10
290	{
291	   if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
292	      yyerror(& @1, state, "invalid fragment program header");
293
294	   }
295	   state->mode = ARB_vertex;
296	}
297	| ARBfp_10
298	{
299	   if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
300	      yyerror(& @1, state, "invalid vertex program header");
301	   }
302	   state->mode = ARB_fragment;
303
304	   state->option.TexRect =
305	      (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
306	}
307	;
308
309optionSequence: optionSequence option
310	|
311	;
312
313option: OPTION string ';'
314	{
315	   int valid = 0;
316
317	   if (state->mode == ARB_vertex) {
318	      valid = _mesa_ARBvp_parse_option(state, $2);
319	   } else if (state->mode == ARB_fragment) {
320	      valid = _mesa_ARBfp_parse_option(state, $2);
321	   }
322
323
324	   free($2);
325
326	   if (!valid) {
327	      const char *const err_str = (state->mode == ARB_vertex)
328		 ? "invalid ARB vertex program option"
329		 : "invalid ARB fragment program option";
330
331	      yyerror(& @2, state, err_str);
332	      YYERROR;
333	   }
334	}
335	;
336
337statementSequence: statementSequence statement
338	|
339	;
340
341statement: instruction ';'
342	{
343	   if ($1 != NULL) {
344	      if (state->inst_tail == NULL) {
345		 state->inst_head = $1;
346	      } else {
347		 state->inst_tail->next = $1;
348	      }
349
350	      state->inst_tail = $1;
351	      $1->next = NULL;
352
353              state->prog->arb.NumInstructions++;
354	   }
355	}
356	| namingStatement ';'
357	;
358
359instruction: ALU_instruction
360	{
361	   $$ = $1;
362           state->prog->arb.NumAluInstructions++;
363	}
364	| TexInstruction
365	{
366	   $$ = $1;
367           state->prog->arb.NumTexInstructions++;
368	}
369	;
370
371ALU_instruction: ARL_instruction
372	| VECTORop_instruction
373	| SCALARop_instruction
374	| BINSCop_instruction
375	| BINop_instruction
376	| TRIop_instruction
377	| SWZ_instruction
378	;
379
380TexInstruction: SAMPLE_instruction
381	| KIL_instruction
382	| TXD_instruction
383	;
384
385ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
386	{
387	   $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
388	}
389	;
390
391VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
392	{
393	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
394	}
395	;
396
397SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
398	{
399	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
400	}
401	;
402
403BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
404	{
405	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
406	}
407	;
408
409
410BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
411	{
412	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
413	}
414	;
415
416TRIop_instruction: TRI_OP maskedDstReg ','
417                   swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
418	{
419	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
420	}
421	;
422
423SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
424	{
425	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
426	   if ($$ != NULL) {
427	      const GLbitfield tex_mask = (1U << $6);
428	      GLbitfield shadow_tex = 0;
429	      GLbitfield target_mask = 0;
430
431
432	      $$->Base.TexSrcUnit = $6;
433
434	      if ($8 < 0) {
435		 shadow_tex = tex_mask;
436
437		 $$->Base.TexSrcTarget = -$8;
438		 $$->Base.TexShadow = 1;
439	      } else {
440		 $$->Base.TexSrcTarget = $8;
441	      }
442
443	      target_mask = (1U << $$->Base.TexSrcTarget);
444
445	      /* If this texture unit was previously accessed and that access
446	       * had a different texture target, generate an error.
447	       *
448	       * If this texture unit was previously accessed and that access
449	       * had a different shadow mode, generate an error.
450	       */
451	      if ((state->prog->TexturesUsed[$6] != 0)
452		  && ((state->prog->TexturesUsed[$6] != target_mask)
453		      || ((state->prog->ShadowSamplers & tex_mask)
454			  != shadow_tex))) {
455		 yyerror(& @8, state,
456			 "multiple targets used on one texture image unit");
457		 YYERROR;
458	      }
459
460
461	      state->prog->TexturesUsed[$6] |= target_mask;
462	      state->prog->ShadowSamplers |= shadow_tex;
463	   }
464	}
465	;
466
467KIL_instruction: KIL swizzleSrcReg
468	{
469	   $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
470	   state->fragment.UsesKill = 1;
471	}
472	;
473
474TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
475	{
476	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
477	   if ($$ != NULL) {
478	      const GLbitfield tex_mask = (1U << $10);
479	      GLbitfield shadow_tex = 0;
480	      GLbitfield target_mask = 0;
481
482
483	      $$->Base.TexSrcUnit = $10;
484
485	      if ($12 < 0) {
486		 shadow_tex = tex_mask;
487
488		 $$->Base.TexSrcTarget = -$12;
489		 $$->Base.TexShadow = 1;
490	      } else {
491		 $$->Base.TexSrcTarget = $12;
492	      }
493
494	      target_mask = (1U << $$->Base.TexSrcTarget);
495
496	      /* If this texture unit was previously accessed and that access
497	       * had a different texture target, generate an error.
498	       *
499	       * If this texture unit was previously accessed and that access
500	       * had a different shadow mode, generate an error.
501	       */
502	      if ((state->prog->TexturesUsed[$10] != 0)
503		  && ((state->prog->TexturesUsed[$10] != target_mask)
504		      || ((state->prog->ShadowSamplers & tex_mask)
505			  != shadow_tex))) {
506		 yyerror(& @12, state,
507			 "multiple targets used on one texture image unit");
508		 YYERROR;
509	      }
510
511
512	      state->prog->TexturesUsed[$10] |= target_mask;
513	      state->prog->ShadowSamplers |= shadow_tex;
514	   }
515	}
516	;
517
518texImageUnit: TEXTURE_UNIT optTexImageUnitNum
519	{
520	   $$ = $2;
521	}
522	;
523
524texTarget: TEX_1D  { $$ = TEXTURE_1D_INDEX; }
525	| TEX_2D   { $$ = TEXTURE_2D_INDEX; }
526	| TEX_3D   { $$ = TEXTURE_3D_INDEX; }
527	| TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
528	| TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
529	| TEX_SHADOW1D   { $$ = -TEXTURE_1D_INDEX; }
530	| TEX_SHADOW2D   { $$ = -TEXTURE_2D_INDEX; }
531	| TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
532	| TEX_ARRAY1D         { $$ = TEXTURE_1D_ARRAY_INDEX; }
533	| TEX_ARRAY2D         { $$ = TEXTURE_2D_ARRAY_INDEX; }
534	| TEX_ARRAYSHADOW1D   { $$ = -TEXTURE_1D_ARRAY_INDEX; }
535	| TEX_ARRAYSHADOW2D   { $$ = -TEXTURE_2D_ARRAY_INDEX; }
536	;
537
538SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
539	{
540	   /* FIXME: Is this correct?  Should the extenedSwizzle be applied
541	    * FIXME: to the existing swizzle?
542	    */
543	   $4.Base.Swizzle = $6.swizzle;
544	   $4.Base.Negate = $6.mask;
545
546	   $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
547	}
548	;
549
550scalarSrcReg: optionalSign scalarUse
551	{
552	   $$ = $2;
553
554	   if ($1) {
555	      $$.Base.Negate = ~$$.Base.Negate;
556	   }
557	}
558	;
559
560scalarUse:  srcReg scalarSuffix
561	{
562	   $$ = $1;
563
564	   $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
565						    $2.swizzle);
566	}
567	;
568
569swizzleSrcReg: optionalSign srcReg swizzleSuffix
570	{
571	   $$ = $2;
572
573	   if ($1) {
574	      $$.Base.Negate = ~$$.Base.Negate;
575	   }
576
577	   $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
578						    $3.swizzle);
579	}
580	;
581
582maskedDstReg: dstReg optionalMask
583	{
584	   $$ = $1;
585	   $$.WriteMask = $2.mask;
586
587	   if ($$.File == PROGRAM_OUTPUT) {
588	      /* Technically speaking, this should check that it is in
589	       * vertex program mode.  However, PositionInvariant can never be
590	       * set in fragment program mode, so it is somewhat irrelevant.
591	       */
592	      if (state->option.PositionInvariant
593	       && ($$.Index == VARYING_SLOT_POS)) {
594		 yyerror(& @1, state, "position-invariant programs cannot "
595			 "write position");
596		 YYERROR;
597	      }
598
599              state->prog->info.outputs_written |= BITFIELD64_BIT($$.Index);
600	   }
601	}
602	;
603
604maskedAddrReg: addrReg addrWriteMask
605	{
606	   set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
607	   $$.WriteMask = $2.mask;
608	}
609	;
610
611extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
612	{
613	   const unsigned xyzw_valid =
614	      ($1.xyzw_valid << 0)
615	      | ($3.xyzw_valid << 1)
616	      | ($5.xyzw_valid << 2)
617	      | ($7.xyzw_valid << 3);
618	   const unsigned rgba_valid =
619	      ($1.rgba_valid << 0)
620	      | ($3.rgba_valid << 1)
621	      | ($5.rgba_valid << 2)
622	      | ($7.rgba_valid << 3);
623
624	   /* All of the swizzle components have to be valid in either RGBA
625	    * or XYZW.  Note that 0 and 1 are valid in both, so both masks
626	    * can have some bits set.
627	    *
628	    * We somewhat deviate from the spec here.  It would be really hard
629	    * to figure out which component is the error, and there probably
630	    * isn't a lot of benefit.
631	    */
632	   if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
633	      yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
634		      "components");
635	      YYERROR;
636	   }
637
638	   $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
639	   $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
640	      | ($7.negate << 3);
641	}
642	;
643
644extSwizComp: optionalSign extSwizSel
645	{
646	   $$ = $2;
647	   $$.negate = ($1) ? 1 : 0;
648	}
649	;
650
651extSwizSel: INTEGER
652	{
653	   if (($1 != 0) && ($1 != 1)) {
654	      yyerror(& @1, state, "invalid extended swizzle selector");
655	      YYERROR;
656	   }
657
658	   $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
659           $$.negate = 0;
660
661	   /* 0 and 1 are valid for both RGBA swizzle names and XYZW
662	    * swizzle names.
663	    */
664	   $$.xyzw_valid = 1;
665	   $$.rgba_valid = 1;
666	}
667	| string
668	{
669	   char s;
670
671	   if (strlen($1) > 1) {
672	      yyerror(& @1, state, "invalid extended swizzle selector");
673	      YYERROR;
674	   }
675
676	   s = $1[0];
677	   free($1);
678
679           $$.rgba_valid = 0;
680           $$.xyzw_valid = 0;
681           $$.negate = 0;
682
683	   switch (s) {
684	   case 'x':
685	      $$.swz = SWIZZLE_X;
686	      $$.xyzw_valid = 1;
687	      break;
688	   case 'y':
689	      $$.swz = SWIZZLE_Y;
690	      $$.xyzw_valid = 1;
691	      break;
692	   case 'z':
693	      $$.swz = SWIZZLE_Z;
694	      $$.xyzw_valid = 1;
695	      break;
696	   case 'w':
697	      $$.swz = SWIZZLE_W;
698	      $$.xyzw_valid = 1;
699	      break;
700
701	   case 'r':
702	      $$.swz = SWIZZLE_X;
703	      $$.rgba_valid = 1;
704	      break;
705	   case 'g':
706	      $$.swz = SWIZZLE_Y;
707	      $$.rgba_valid = 1;
708	      break;
709	   case 'b':
710	      $$.swz = SWIZZLE_Z;
711	      $$.rgba_valid = 1;
712	      break;
713	   case 'a':
714	      $$.swz = SWIZZLE_W;
715	      $$.rgba_valid = 1;
716	      break;
717
718	   default:
719	      yyerror(& @1, state, "invalid extended swizzle selector");
720	      YYERROR;
721	      break;
722	   }
723	}
724	;
725
726srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
727	{
728	   struct asm_symbol *const s = (struct asm_symbol *)
729              _mesa_symbol_table_find_symbol(state->st, $1);
730
731	   free($1);
732
733	   if (s == NULL) {
734	      yyerror(& @1, state, "invalid operand variable");
735	      YYERROR;
736	   } else if ((s->type != at_param) && (s->type != at_temp)
737		      && (s->type != at_attrib)) {
738	      yyerror(& @1, state, "invalid operand variable");
739	      YYERROR;
740	   } else if ((s->type == at_param) && s->param_is_array) {
741	      yyerror(& @1, state, "non-array access to array PARAM");
742	      YYERROR;
743	   }
744
745	   init_src_reg(& $$);
746	   switch (s->type) {
747	   case at_temp:
748	      set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
749	      break;
750	   case at_param:
751              set_src_reg_swz(& $$, s->param_binding_type,
752                              s->param_binding_begin,
753                              s->param_binding_swizzle);
754	      break;
755	   case at_attrib:
756	      set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
757              state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index);
758
759	      if (!validate_inputs(& @1, state)) {
760		 YYERROR;
761	      }
762	      break;
763
764	   default:
765	      YYERROR;
766	      break;
767	   }
768	}
769	| attribBinding
770	{
771	   set_src_reg(& $$, PROGRAM_INPUT, $1);
772           state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index);
773
774	   if (!validate_inputs(& @1, state)) {
775	      YYERROR;
776	   }
777	}
778	| progParamArray '[' progParamArrayMem ']'
779	{
780	   if (! $3.Base.RelAddr
781	       && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
782	      yyerror(& @3, state, "out of bounds array access");
783	      YYERROR;
784	   }
785
786	   init_src_reg(& $$);
787	   $$.Base.File = $1->param_binding_type;
788
789	   if ($3.Base.RelAddr) {
790              state->prog->arb.IndirectRegisterFiles |= (1 << $$.Base.File);
791	      $1->param_accessed_indirectly = 1;
792
793	      $$.Base.RelAddr = 1;
794	      $$.Base.Index = $3.Base.Index;
795	      $$.Symbol = $1;
796	   } else {
797	      $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
798	   }
799	}
800	| paramSingleItemUse
801	{
802           gl_register_file file = ($1.name != NULL)
803	      ? $1.param_binding_type
804	      : PROGRAM_CONSTANT;
805           set_src_reg_swz(& $$, file, $1.param_binding_begin,
806                           $1.param_binding_swizzle);
807	}
808	;
809
810dstReg: resultBinding
811	{
812	   set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
813	}
814	| USED_IDENTIFIER /* temporaryReg | vertexResultReg */
815	{
816	   struct asm_symbol *const s = (struct asm_symbol *)
817              _mesa_symbol_table_find_symbol(state->st, $1);
818
819	   free($1);
820
821	   if (s == NULL) {
822	      yyerror(& @1, state, "invalid operand variable");
823	      YYERROR;
824	   } else if ((s->type != at_output) && (s->type != at_temp)) {
825	      yyerror(& @1, state, "invalid operand variable");
826	      YYERROR;
827	   }
828
829	   switch (s->type) {
830	   case at_temp:
831	      set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
832	      break;
833	   case at_output:
834	      set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
835	      break;
836	   default:
837	      set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
838	      break;
839	   }
840	}
841	;
842
843progParamArray: USED_IDENTIFIER
844	{
845	   struct asm_symbol *const s = (struct asm_symbol *)
846              _mesa_symbol_table_find_symbol(state->st, $1);
847
848	   free($1);
849
850	   if (s == NULL) {
851	      yyerror(& @1, state, "invalid operand variable");
852	      YYERROR;
853	   } else if ((s->type != at_param) || !s->param_is_array) {
854	      yyerror(& @1, state, "array access to non-PARAM variable");
855	      YYERROR;
856	   } else {
857	      $$ = s;
858	   }
859	}
860	;
861
862progParamArrayMem: progParamArrayAbs | progParamArrayRel;
863
864progParamArrayAbs: INTEGER
865	{
866	   init_src_reg(& $$);
867	   $$.Base.Index = $1;
868	}
869	;
870
871progParamArrayRel: addrReg addrComponent addrRegRelOffset
872	{
873	   /* FINISHME: Add support for multiple address registers.
874	    */
875	   /* FINISHME: Add support for 4-component address registers.
876	    */
877	   init_src_reg(& $$);
878	   $$.Base.RelAddr = 1;
879	   $$.Base.Index = $3;
880	}
881	;
882
883addrRegRelOffset:              { $$ = 0; }
884	| '+' addrRegPosOffset { $$ = $2; }
885	| '-' addrRegNegOffset { $$ = -$2; }
886	;
887
888addrRegPosOffset: INTEGER
889	{
890	   if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
891              char s[100];
892              _mesa_snprintf(s, sizeof(s),
893                             "relative address offset too large (%d)", $1);
894	      yyerror(& @1, state, s);
895	      YYERROR;
896	   } else {
897	      $$ = $1;
898	   }
899	}
900	;
901
902addrRegNegOffset: INTEGER
903	{
904	   if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
905              char s[100];
906              _mesa_snprintf(s, sizeof(s),
907                             "relative address offset too large (%d)", $1);
908	      yyerror(& @1, state, s);
909	      YYERROR;
910	   } else {
911	      $$ = $1;
912	   }
913	}
914	;
915
916addrReg: USED_IDENTIFIER
917	{
918	   struct asm_symbol *const s = (struct asm_symbol *)
919              _mesa_symbol_table_find_symbol(state->st, $1);
920
921	   free($1);
922
923	   if (s == NULL) {
924	      yyerror(& @1, state, "invalid array member");
925	      YYERROR;
926	   } else if (s->type != at_address) {
927	      yyerror(& @1, state,
928		      "invalid variable for indexed array access");
929	      YYERROR;
930	   } else {
931	      $$ = s;
932	   }
933	}
934	;
935
936addrComponent: MASK1
937	{
938	   if ($1.mask != WRITEMASK_X) {
939	      yyerror(& @1, state, "invalid address component selector");
940	      YYERROR;
941	   } else {
942	      $$ = $1;
943	   }
944	}
945	;
946
947addrWriteMask: MASK1
948	{
949	   if ($1.mask != WRITEMASK_X) {
950	      yyerror(& @1, state,
951		      "address register write mask must be \".x\"");
952	      YYERROR;
953	   } else {
954	      $$ = $1;
955	   }
956	}
957	;
958
959scalarSuffix: MASK1;
960
961swizzleSuffix: MASK1
962	| MASK4
963	| SWIZZLE
964	|              { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
965	;
966
967optionalMask: MASK4 | MASK3 | MASK2 | MASK1
968	|              { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
969	;
970
971namingStatement: ATTRIB_statement
972	| PARAM_statement
973	| TEMP_statement
974	| ADDRESS_statement
975	| OUTPUT_statement
976	| ALIAS_statement
977	;
978
979ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
980	{
981	   struct asm_symbol *const s =
982	      declare_variable(state, $2, at_attrib, & @2);
983
984	   if (s == NULL) {
985	      free($2);
986	      YYERROR;
987	   } else {
988	      s->attrib_binding = $4;
989	      state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
990
991	      if (!validate_inputs(& @4, state)) {
992		 YYERROR;
993	      }
994	   }
995	}
996	;
997
998attribBinding: VERTEX vtxAttribItem
999	{
1000	   $$ = $2;
1001	}
1002	| FRAGMENT fragAttribItem
1003	{
1004	   $$ = $2;
1005	}
1006	;
1007
1008vtxAttribItem: POSITION
1009	{
1010	   $$ = VERT_ATTRIB_POS;
1011	}
1012	| NORMAL
1013	{
1014	   $$ = VERT_ATTRIB_NORMAL;
1015	}
1016	| COLOR optColorType
1017	{
1018	   $$ = VERT_ATTRIB_COLOR0 + $2;
1019	}
1020	| FOGCOORD
1021	{
1022	   $$ = VERT_ATTRIB_FOG;
1023	}
1024	| TEXCOORD optTexCoordUnitNum
1025	{
1026	   $$ = VERT_ATTRIB_TEX0 + $2;
1027	}
1028	| MATRIXINDEX '[' vtxWeightNum ']'
1029	{
1030	   yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1031	   YYERROR;
1032	}
1033	| VTXATTRIB '[' vtxAttribNum ']'
1034	{
1035	   $$ = VERT_ATTRIB_GENERIC0 + $3;
1036	}
1037	;
1038
1039vtxAttribNum: INTEGER
1040	{
1041	   if ((unsigned) $1 >= state->limits->MaxAttribs) {
1042	      yyerror(& @1, state, "invalid vertex attribute reference");
1043	      YYERROR;
1044	   }
1045
1046	   $$ = $1;
1047	}
1048	;
1049
1050vtxWeightNum: INTEGER;
1051
1052fragAttribItem: POSITION
1053	{
1054	   $$ = VARYING_SLOT_POS;
1055	}
1056	| COLOR optColorType
1057	{
1058	   $$ = VARYING_SLOT_COL0 + $2;
1059	}
1060	| FOGCOORD
1061	{
1062	   $$ = VARYING_SLOT_FOGC;
1063	}
1064	| TEXCOORD optTexCoordUnitNum
1065	{
1066	   $$ = VARYING_SLOT_TEX0 + $2;
1067	}
1068	;
1069
1070PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1071
1072PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1073	{
1074	   struct asm_symbol *const s =
1075	      declare_variable(state, $2, at_param, & @2);
1076
1077	   if (s == NULL) {
1078	      free($2);
1079	      YYERROR;
1080	   } else {
1081	      s->param_binding_type = $3.param_binding_type;
1082	      s->param_binding_begin = $3.param_binding_begin;
1083	      s->param_binding_length = $3.param_binding_length;
1084              s->param_binding_swizzle = $3.param_binding_swizzle;
1085	      s->param_is_array = 0;
1086	   }
1087	}
1088	;
1089
1090PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1091	{
1092	   if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1093	      free($2);
1094	      yyerror(& @4, state,
1095		      "parameter array size and number of bindings must match");
1096	      YYERROR;
1097	   } else {
1098	      struct asm_symbol *const s =
1099		 declare_variable(state, $2, $6.type, & @2);
1100
1101	      if (s == NULL) {
1102		 free($2);
1103		 YYERROR;
1104	      } else {
1105		 s->param_binding_type = $6.param_binding_type;
1106		 s->param_binding_begin = $6.param_binding_begin;
1107		 s->param_binding_length = $6.param_binding_length;
1108                 s->param_binding_swizzle = SWIZZLE_XYZW;
1109		 s->param_is_array = 1;
1110	      }
1111	   }
1112	}
1113	;
1114
1115optArraySize:
1116	{
1117	   $$ = 0;
1118	}
1119	| INTEGER
1120        {
1121	   if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1122              char msg[100];
1123              _mesa_snprintf(msg, sizeof(msg),
1124                             "invalid parameter array size (size=%d max=%u)",
1125                             $1, state->limits->MaxParameters);
1126	      yyerror(& @1, state, msg);
1127	      YYERROR;
1128	   } else {
1129	      $$ = $1;
1130	   }
1131	}
1132	;
1133
1134paramSingleInit: '=' paramSingleItemDecl
1135	{
1136	   $$ = $2;
1137	}
1138	;
1139
1140paramMultipleInit: '=' '{' paramMultInitList '}'
1141	{
1142	   $$ = $3;
1143	}
1144	;
1145
1146paramMultInitList: paramMultipleItem
1147	| paramMultInitList ',' paramMultipleItem
1148	{
1149	   $1.param_binding_length += $3.param_binding_length;
1150	   $$ = $1;
1151	}
1152	;
1153
1154paramSingleItemDecl: stateSingleItem
1155	{
1156	   memset(& $$, 0, sizeof($$));
1157	   $$.param_binding_begin = ~0;
1158	   initialize_symbol_from_state(state->prog, & $$, $1);
1159	}
1160	| programSingleItem
1161	{
1162	   memset(& $$, 0, sizeof($$));
1163	   $$.param_binding_begin = ~0;
1164	   initialize_symbol_from_param(state->prog, & $$, $1);
1165	}
1166	| paramConstDecl
1167	{
1168	   memset(& $$, 0, sizeof($$));
1169	   $$.param_binding_begin = ~0;
1170	   initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1171	}
1172	;
1173
1174paramSingleItemUse: stateSingleItem
1175	{
1176	   memset(& $$, 0, sizeof($$));
1177	   $$.param_binding_begin = ~0;
1178	   initialize_symbol_from_state(state->prog, & $$, $1);
1179	}
1180	| programSingleItem
1181	{
1182	   memset(& $$, 0, sizeof($$));
1183	   $$.param_binding_begin = ~0;
1184	   initialize_symbol_from_param(state->prog, & $$, $1);
1185	}
1186	| paramConstUse
1187	{
1188	   memset(& $$, 0, sizeof($$));
1189	   $$.param_binding_begin = ~0;
1190	   initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1191	}
1192	;
1193
1194paramMultipleItem: stateMultipleItem
1195	{
1196	   memset(& $$, 0, sizeof($$));
1197	   $$.param_binding_begin = ~0;
1198	   initialize_symbol_from_state(state->prog, & $$, $1);
1199	}
1200	| programMultipleItem
1201	{
1202	   memset(& $$, 0, sizeof($$));
1203	   $$.param_binding_begin = ~0;
1204	   initialize_symbol_from_param(state->prog, & $$, $1);
1205	}
1206	| paramConstDecl
1207	{
1208	   memset(& $$, 0, sizeof($$));
1209	   $$.param_binding_begin = ~0;
1210	   initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1211	}
1212	;
1213
1214stateMultipleItem: stateSingleItem        { memcpy($$, $1, sizeof($$)); }
1215	| STATE stateMatrixRows           { memcpy($$, $2, sizeof($$)); }
1216	;
1217
1218stateSingleItem: STATE stateMaterialItem  { memcpy($$, $2, sizeof($$)); }
1219	| STATE stateLightItem            { memcpy($$, $2, sizeof($$)); }
1220	| STATE stateLightModelItem       { memcpy($$, $2, sizeof($$)); }
1221	| STATE stateLightProdItem        { memcpy($$, $2, sizeof($$)); }
1222	| STATE stateTexGenItem           { memcpy($$, $2, sizeof($$)); }
1223	| STATE stateTexEnvItem           { memcpy($$, $2, sizeof($$)); }
1224	| STATE stateFogItem              { memcpy($$, $2, sizeof($$)); }
1225	| STATE stateClipPlaneItem        { memcpy($$, $2, sizeof($$)); }
1226	| STATE statePointItem            { memcpy($$, $2, sizeof($$)); }
1227	| STATE stateMatrixRow            { memcpy($$, $2, sizeof($$)); }
1228	| STATE stateDepthItem            { memcpy($$, $2, sizeof($$)); }
1229	;
1230
1231stateMaterialItem: MATERIAL optFaceType stateMatProperty
1232	{
1233	   memset($$, 0, sizeof($$));
1234	   $$[0] = STATE_MATERIAL;
1235	   $$[1] = $2;
1236	   $$[2] = $3;
1237	}
1238	;
1239
1240stateMatProperty: ambDiffSpecProperty
1241	{
1242	   $$ = $1;
1243	}
1244	| EMISSION
1245	{
1246	   $$ = STATE_EMISSION;
1247	}
1248	| SHININESS
1249	{
1250	   $$ = STATE_SHININESS;
1251	}
1252	;
1253
1254stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1255	{
1256	   memset($$, 0, sizeof($$));
1257	   $$[0] = STATE_LIGHT;
1258	   $$[1] = $3;
1259	   $$[2] = $5;
1260	}
1261	;
1262
1263stateLightProperty: ambDiffSpecProperty
1264	{
1265	   $$ = $1;
1266	}
1267	| POSITION
1268	{
1269	   $$ = STATE_POSITION;
1270	}
1271	| ATTENUATION
1272	{
1273	   if (!state->ctx->Extensions.EXT_point_parameters) {
1274	      yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1275	      YYERROR;
1276	   }
1277
1278	   $$ = STATE_ATTENUATION;
1279	}
1280	| SPOT stateSpotProperty
1281	{
1282	   $$ = $2;
1283	}
1284	| HALF
1285	{
1286	   $$ = STATE_HALF_VECTOR;
1287	}
1288	;
1289
1290stateSpotProperty: DIRECTION
1291	{
1292	   $$ = STATE_SPOT_DIRECTION;
1293	}
1294	;
1295
1296stateLightModelItem: LIGHTMODEL stateLModProperty
1297	{
1298	   $$[0] = $2[0];
1299	   $$[1] = $2[1];
1300	}
1301	;
1302
1303stateLModProperty: AMBIENT
1304	{
1305	   memset($$, 0, sizeof($$));
1306	   $$[0] = STATE_LIGHTMODEL_AMBIENT;
1307	}
1308	| optFaceType SCENECOLOR
1309	{
1310	   memset($$, 0, sizeof($$));
1311	   $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1312	   $$[1] = $1;
1313	}
1314	;
1315
1316stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1317	{
1318	   memset($$, 0, sizeof($$));
1319	   $$[0] = STATE_LIGHTPROD;
1320	   $$[1] = $3;
1321	   $$[2] = $5;
1322	   $$[3] = $6;
1323	}
1324	;
1325
1326stateLProdProperty: ambDiffSpecProperty;
1327
1328stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1329	{
1330	   memset($$, 0, sizeof($$));
1331	   $$[0] = $3;
1332	   $$[1] = $2;
1333	}
1334	;
1335
1336stateTexEnvProperty: COLOR
1337	{
1338	   $$ = STATE_TEXENV_COLOR;
1339	}
1340	;
1341
1342ambDiffSpecProperty: AMBIENT
1343	{
1344	   $$ = STATE_AMBIENT;
1345	}
1346	| DIFFUSE
1347	{
1348	   $$ = STATE_DIFFUSE;
1349	}
1350	| SPECULAR
1351	{
1352	   $$ = STATE_SPECULAR;
1353	}
1354	;
1355
1356stateLightNumber: INTEGER
1357	{
1358	   if ((unsigned) $1 >= state->MaxLights) {
1359	      yyerror(& @1, state, "invalid light selector");
1360	      YYERROR;
1361	   }
1362
1363	   $$ = $1;
1364	}
1365	;
1366
1367stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1368	{
1369	   memset($$, 0, sizeof($$));
1370	   $$[0] = STATE_TEXGEN;
1371	   $$[1] = $2;
1372	   $$[2] = $3 + $4;
1373	}
1374	;
1375
1376stateTexGenType: EYE
1377	{
1378	   $$ = STATE_TEXGEN_EYE_S;
1379	}
1380	| OBJECT
1381	{
1382	   $$ = STATE_TEXGEN_OBJECT_S;
1383	}
1384	;
1385stateTexGenCoord: TEXGEN_S
1386	{
1387	   $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1388	}
1389	| TEXGEN_T
1390	{
1391	   $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1392	}
1393	| TEXGEN_R
1394	{
1395	   $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1396	}
1397	| TEXGEN_Q
1398	{
1399	   $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1400	}
1401	;
1402
1403stateFogItem: FOG stateFogProperty
1404	{
1405	   memset($$, 0, sizeof($$));
1406	   $$[0] = $2;
1407	}
1408	;
1409
1410stateFogProperty: COLOR
1411	{
1412	   $$ = STATE_FOG_COLOR;
1413	}
1414	| PARAMS
1415	{
1416	   $$ = STATE_FOG_PARAMS;
1417	}
1418	;
1419
1420stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1421	{
1422	   memset($$, 0, sizeof($$));
1423	   $$[0] = STATE_CLIPPLANE;
1424	   $$[1] = $3;
1425	}
1426	;
1427
1428stateClipPlaneNum: INTEGER
1429	{
1430	   if ((unsigned) $1 >= state->MaxClipPlanes) {
1431	      yyerror(& @1, state, "invalid clip plane selector");
1432	      YYERROR;
1433	   }
1434
1435	   $$ = $1;
1436	}
1437	;
1438
1439statePointItem: POINT_TOK statePointProperty
1440	{
1441	   memset($$, 0, sizeof($$));
1442	   $$[0] = $2;
1443	}
1444	;
1445
1446statePointProperty: SIZE_TOK
1447	{
1448	   $$ = STATE_POINT_SIZE;
1449	}
1450	| ATTENUATION
1451	{
1452	   $$ = STATE_POINT_ATTENUATION;
1453	}
1454	;
1455
1456stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1457	{
1458	   $$[0] = $1[0];
1459	   $$[1] = $1[1];
1460	   $$[2] = $4;
1461	   $$[3] = $4;
1462	   $$[4] = $1[2];
1463	}
1464	;
1465
1466stateMatrixRows: stateMatrixItem optMatrixRows
1467	{
1468	   $$[0] = $1[0];
1469	   $$[1] = $1[1];
1470	   $$[2] = $2[2];
1471	   $$[3] = $2[3];
1472	   $$[4] = $1[2];
1473	}
1474	;
1475
1476optMatrixRows:
1477	{
1478	   $$[2] = 0;
1479	   $$[3] = 3;
1480	}
1481	| ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1482	{
1483	   /* It seems logical that the matrix row range specifier would have
1484	    * to specify a range or more than one row (i.e., $5 > $3).
1485	    * However, the ARB_vertex_program spec says "a program will fail
1486	    * to load if <a> is greater than <b>."  This means that $3 == $5
1487	    * is valid.
1488	    */
1489	   if ($3 > $5) {
1490	      yyerror(& @3, state, "invalid matrix row range");
1491	      YYERROR;
1492	   }
1493
1494	   $$[2] = $3;
1495	   $$[3] = $5;
1496	}
1497	;
1498
1499stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1500	{
1501	   $$[0] = $2[0];
1502	   $$[1] = $2[1];
1503	   $$[2] = $3;
1504	}
1505	;
1506
1507stateOptMatModifier:
1508	{
1509	   $$ = 0;
1510	}
1511	| stateMatModifier
1512	{
1513	   $$ = $1;
1514	}
1515	;
1516
1517stateMatModifier: INVERSE
1518	{
1519	   $$ = STATE_MATRIX_INVERSE;
1520	}
1521	| TRANSPOSE
1522	{
1523	   $$ = STATE_MATRIX_TRANSPOSE;
1524	}
1525	| INVTRANS
1526	{
1527	   $$ = STATE_MATRIX_INVTRANS;
1528	}
1529	;
1530
1531stateMatrixRowNum: INTEGER
1532	{
1533	   if ($1 > 3) {
1534	      yyerror(& @1, state, "invalid matrix row reference");
1535	      YYERROR;
1536	   }
1537
1538	   $$ = $1;
1539	}
1540	;
1541
1542stateMatrixName: MODELVIEW stateOptModMatNum
1543	{
1544	   $$[0] = STATE_MODELVIEW_MATRIX;
1545	   $$[1] = $2;
1546	}
1547	| PROJECTION
1548	{
1549	   $$[0] = STATE_PROJECTION_MATRIX;
1550	   $$[1] = 0;
1551	}
1552	| MVP
1553	{
1554	   $$[0] = STATE_MVP_MATRIX;
1555	   $$[1] = 0;
1556	}
1557	| TEXTURE optTexCoordUnitNum
1558	{
1559	   $$[0] = STATE_TEXTURE_MATRIX;
1560	   $$[1] = $2;
1561	}
1562	| PALETTE '[' statePaletteMatNum ']'
1563	{
1564	   yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1565	   YYERROR;
1566	}
1567	| MAT_PROGRAM '[' stateProgramMatNum ']'
1568	{
1569	   $$[0] = STATE_PROGRAM_MATRIX;
1570	   $$[1] = $3;
1571	}
1572	;
1573
1574stateOptModMatNum:
1575	{
1576	   $$ = 0;
1577	}
1578	| '[' stateModMatNum ']'
1579	{
1580	   $$ = $2;
1581	}
1582	;
1583stateModMatNum: INTEGER
1584	{
1585	   /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1586	    * zero is valid.
1587	    */
1588	   if ($1 != 0) {
1589	      yyerror(& @1, state, "invalid modelview matrix index");
1590	      YYERROR;
1591	   }
1592
1593	   $$ = $1;
1594	}
1595	;
1596statePaletteMatNum: INTEGER
1597	{
1598	   /* Since GL_ARB_matrix_palette isn't supported, just let any value
1599	    * through here.  The error will be generated later.
1600	    */
1601	   $$ = $1;
1602	}
1603	;
1604stateProgramMatNum: INTEGER
1605	{
1606	   if ((unsigned) $1 >= state->MaxProgramMatrices) {
1607	      yyerror(& @1, state, "invalid program matrix selector");
1608	      YYERROR;
1609	   }
1610
1611	   $$ = $1;
1612	}
1613	;
1614
1615stateDepthItem: DEPTH RANGE
1616	{
1617	   memset($$, 0, sizeof($$));
1618	   $$[0] = STATE_DEPTH_RANGE;
1619	}
1620	;
1621
1622
1623programSingleItem: progEnvParam | progLocalParam;
1624
1625programMultipleItem: progEnvParams | progLocalParams;
1626
1627progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1628	{
1629	   memset($$, 0, sizeof($$));
1630	   $$[0] = state->state_param_enum;
1631	   $$[1] = STATE_ENV;
1632	   $$[2] = $4[0];
1633	   $$[3] = $4[1];
1634	}
1635	;
1636
1637progEnvParamNums: progEnvParamNum
1638	{
1639	   $$[0] = $1;
1640	   $$[1] = $1;
1641	}
1642	| progEnvParamNum DOT_DOT progEnvParamNum
1643	{
1644	   $$[0] = $1;
1645	   $$[1] = $3;
1646	}
1647	;
1648
1649progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1650	{
1651	   memset($$, 0, sizeof($$));
1652	   $$[0] = state->state_param_enum;
1653	   $$[1] = STATE_ENV;
1654	   $$[2] = $4;
1655	   $$[3] = $4;
1656	}
1657	;
1658
1659progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1660	{
1661	   memset($$, 0, sizeof($$));
1662	   $$[0] = state->state_param_enum;
1663	   $$[1] = STATE_LOCAL;
1664	   $$[2] = $4[0];
1665	   $$[3] = $4[1];
1666	}
1667
1668progLocalParamNums: progLocalParamNum
1669	{
1670	   $$[0] = $1;
1671	   $$[1] = $1;
1672	}
1673	| progLocalParamNum DOT_DOT progLocalParamNum
1674	{
1675	   $$[0] = $1;
1676	   $$[1] = $3;
1677	}
1678	;
1679
1680progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1681	{
1682	   memset($$, 0, sizeof($$));
1683	   $$[0] = state->state_param_enum;
1684	   $$[1] = STATE_LOCAL;
1685	   $$[2] = $4;
1686	   $$[3] = $4;
1687	}
1688	;
1689
1690progEnvParamNum: INTEGER
1691	{
1692	   if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1693	      yyerror(& @1, state, "invalid environment parameter reference");
1694	      YYERROR;
1695	   }
1696	   $$ = $1;
1697	}
1698	;
1699
1700progLocalParamNum: INTEGER
1701	{
1702	   if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1703	      yyerror(& @1, state, "invalid local parameter reference");
1704	      YYERROR;
1705	   }
1706	   $$ = $1;
1707	}
1708	;
1709
1710
1711
1712paramConstDecl: paramConstScalarDecl | paramConstVector;
1713paramConstUse: paramConstScalarUse | paramConstVector;
1714
1715paramConstScalarDecl: signedFloatConstant
1716	{
1717	   $$.count = 4;
1718	   $$.data[0].f = $1;
1719	   $$.data[1].f = $1;
1720	   $$.data[2].f = $1;
1721	   $$.data[3].f = $1;
1722	}
1723	;
1724
1725paramConstScalarUse: REAL
1726	{
1727	   $$.count = 1;
1728	   $$.data[0].f = $1;
1729	   $$.data[1].f = $1;
1730	   $$.data[2].f = $1;
1731	   $$.data[3].f = $1;
1732	}
1733	| INTEGER
1734	{
1735	   $$.count = 1;
1736	   $$.data[0].f = (float) $1;
1737	   $$.data[1].f = (float) $1;
1738	   $$.data[2].f = (float) $1;
1739	   $$.data[3].f = (float) $1;
1740	}
1741	;
1742
1743paramConstVector: '{' signedFloatConstant '}'
1744	{
1745	   $$.count = 4;
1746	   $$.data[0].f = $2;
1747	   $$.data[1].f = 0.0f;
1748	   $$.data[2].f = 0.0f;
1749	   $$.data[3].f = 1.0f;
1750	}
1751	| '{' signedFloatConstant ',' signedFloatConstant '}'
1752	{
1753	   $$.count = 4;
1754	   $$.data[0].f = $2;
1755	   $$.data[1].f = $4;
1756	   $$.data[2].f = 0.0f;
1757	   $$.data[3].f = 1.0f;
1758	}
1759	| '{' signedFloatConstant ',' signedFloatConstant ','
1760              signedFloatConstant '}'
1761	{
1762	   $$.count = 4;
1763	   $$.data[0].f = $2;
1764	   $$.data[1].f = $4;
1765	   $$.data[2].f = $6;
1766	   $$.data[3].f = 1.0f;
1767	}
1768	| '{' signedFloatConstant ',' signedFloatConstant ','
1769              signedFloatConstant ',' signedFloatConstant '}'
1770	{
1771	   $$.count = 4;
1772	   $$.data[0].f = $2;
1773	   $$.data[1].f = $4;
1774	   $$.data[2].f = $6;
1775	   $$.data[3].f = $8;
1776	}
1777	;
1778
1779signedFloatConstant: optionalSign REAL
1780	{
1781	   $$ = ($1) ? -$2 : $2;
1782	}
1783	| optionalSign INTEGER
1784	{
1785	   $$ = (float)(($1) ? -$2 : $2);
1786	}
1787	;
1788
1789optionalSign: '+'        { $$ = FALSE; }
1790	| '-'            { $$ = TRUE;  }
1791	|                { $$ = FALSE; }
1792	;
1793
1794TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1795	;
1796
1797ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1798	;
1799
1800varNameList: varNameList ',' IDENTIFIER
1801	{
1802	   if (!declare_variable(state, $3, $<integer>0, & @3)) {
1803	      free($3);
1804	      YYERROR;
1805	   }
1806	}
1807	| IDENTIFIER
1808	{
1809	   if (!declare_variable(state, $1, $<integer>0, & @1)) {
1810	      free($1);
1811	      YYERROR;
1812	   }
1813	}
1814	;
1815
1816OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1817	{
1818	   struct asm_symbol *const s =
1819	      declare_variable(state, $2, at_output, & @2);
1820
1821	   if (s == NULL) {
1822	      free($2);
1823	      YYERROR;
1824	   } else {
1825	      s->output_binding = $4;
1826	   }
1827	}
1828	;
1829
1830resultBinding: RESULT POSITION
1831	{
1832	   if (state->mode == ARB_vertex) {
1833	      $$ = VARYING_SLOT_POS;
1834	   } else {
1835	      yyerror(& @2, state, "invalid program result name");
1836	      YYERROR;
1837	   }
1838	}
1839	| RESULT FOGCOORD
1840	{
1841	   if (state->mode == ARB_vertex) {
1842	      $$ = VARYING_SLOT_FOGC;
1843	   } else {
1844	      yyerror(& @2, state, "invalid program result name");
1845	      YYERROR;
1846	   }
1847	}
1848	| RESULT resultColBinding
1849	{
1850	   $$ = $2;
1851	}
1852	| RESULT POINTSIZE
1853	{
1854	   if (state->mode == ARB_vertex) {
1855	      $$ = VARYING_SLOT_PSIZ;
1856	   } else {
1857	      yyerror(& @2, state, "invalid program result name");
1858	      YYERROR;
1859	   }
1860	}
1861	| RESULT TEXCOORD optTexCoordUnitNum
1862	{
1863	   if (state->mode == ARB_vertex) {
1864	      $$ = VARYING_SLOT_TEX0 + $3;
1865	   } else {
1866	      yyerror(& @2, state, "invalid program result name");
1867	      YYERROR;
1868	   }
1869	}
1870	| RESULT DEPTH
1871	{
1872	   if (state->mode == ARB_fragment) {
1873	      $$ = FRAG_RESULT_DEPTH;
1874	   } else {
1875	      yyerror(& @2, state, "invalid program result name");
1876	      YYERROR;
1877	   }
1878	}
1879	;
1880
1881resultColBinding: COLOR optResultFaceType optResultColorType
1882	{
1883	   $$ = $2 + $3;
1884	}
1885	;
1886
1887optResultFaceType:
1888	{
1889	   if (state->mode == ARB_vertex) {
1890	      $$ = VARYING_SLOT_COL0;
1891	   } else {
1892	      if (state->option.DrawBuffers)
1893		 $$ = FRAG_RESULT_DATA0;
1894	      else
1895		 $$ = FRAG_RESULT_COLOR;
1896	   }
1897	}
1898	| '[' INTEGER ']'
1899	{
1900	   if (state->mode == ARB_vertex) {
1901	      yyerror(& @1, state, "invalid program result name");
1902	      YYERROR;
1903	   } else {
1904	      if (!state->option.DrawBuffers) {
1905		 /* From the ARB_draw_buffers spec (same text exists
1906		  * for ATI_draw_buffers):
1907		  *
1908		  *     If this option is not specified, a fragment
1909		  *     program that attempts to bind
1910		  *     "result.color[n]" will fail to load, and only
1911		  *     "result.color" will be allowed.
1912		  */
1913		 yyerror(& @1, state,
1914			 "result.color[] used without "
1915			 "`OPTION ARB_draw_buffers' or "
1916			 "`OPTION ATI_draw_buffers'");
1917		 YYERROR;
1918	      } else if ($2 >= state->MaxDrawBuffers) {
1919		 yyerror(& @1, state,
1920			 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
1921		 YYERROR;
1922	      }
1923	      $$ = FRAG_RESULT_DATA0 + $2;
1924	   }
1925	}
1926	| FRONT
1927	{
1928	   if (state->mode == ARB_vertex) {
1929	      $$ = VARYING_SLOT_COL0;
1930	   } else {
1931	      yyerror(& @1, state, "invalid program result name");
1932	      YYERROR;
1933	   }
1934	}
1935	| BACK
1936	{
1937	   if (state->mode == ARB_vertex) {
1938	      $$ = VARYING_SLOT_BFC0;
1939	   } else {
1940	      yyerror(& @1, state, "invalid program result name");
1941	      YYERROR;
1942	   }
1943	}
1944	;
1945
1946optResultColorType:
1947	{
1948	   $$ = 0;
1949	}
1950	| PRIMARY
1951	{
1952	   if (state->mode == ARB_vertex) {
1953	      $$ = 0;
1954	   } else {
1955	      yyerror(& @1, state, "invalid program result name");
1956	      YYERROR;
1957	   }
1958	}
1959	| SECONDARY
1960	{
1961	   if (state->mode == ARB_vertex) {
1962	      $$ = 1;
1963	   } else {
1964	      yyerror(& @1, state, "invalid program result name");
1965	      YYERROR;
1966	   }
1967	}
1968	;
1969
1970optFaceType:    { $$ = 0; }
1971	| FRONT	{ $$ = 0; }
1972	| BACK  { $$ = 1; }
1973	;
1974
1975optColorType:       { $$ = 0; }
1976	| PRIMARY   { $$ = 0; }
1977	| SECONDARY { $$ = 1; }
1978	;
1979
1980optTexCoordUnitNum:                { $$ = 0; }
1981	| '[' texCoordUnitNum ']'  { $$ = $2; }
1982	;
1983
1984optTexImageUnitNum:                { $$ = 0; }
1985	| '[' texImageUnitNum ']'  { $$ = $2; }
1986	;
1987
1988optLegacyTexUnitNum:               { $$ = 0; }
1989	| '[' legacyTexUnitNum ']' { $$ = $2; }
1990	;
1991
1992texCoordUnitNum: INTEGER
1993	{
1994	   if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
1995	      yyerror(& @1, state, "invalid texture coordinate unit selector");
1996	      YYERROR;
1997	   }
1998
1999	   $$ = $1;
2000	}
2001	;
2002
2003texImageUnitNum: INTEGER
2004	{
2005	   if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2006	      yyerror(& @1, state, "invalid texture image unit selector");
2007	      YYERROR;
2008	   }
2009
2010	   $$ = $1;
2011	}
2012	;
2013
2014legacyTexUnitNum: INTEGER
2015	{
2016	   if ((unsigned) $1 >= state->MaxTextureUnits) {
2017	      yyerror(& @1, state, "invalid texture unit selector");
2018	      YYERROR;
2019	   }
2020
2021	   $$ = $1;
2022	}
2023	;
2024
2025ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2026	{
2027	   struct asm_symbol *exist = (struct asm_symbol *)
2028              _mesa_symbol_table_find_symbol(state->st, $2);
2029	   struct asm_symbol *target = (struct asm_symbol *)
2030              _mesa_symbol_table_find_symbol(state->st, $4);
2031
2032	   free($4);
2033
2034	   if (exist != NULL) {
2035	      char m[1000];
2036	      _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2037	      free($2);
2038	      yyerror(& @2, state, m);
2039	      YYERROR;
2040	   } else if (target == NULL) {
2041	      free($2);
2042	      yyerror(& @4, state,
2043		      "undefined variable binding in ALIAS statement");
2044	      YYERROR;
2045	   } else {
2046              _mesa_symbol_table_add_symbol(state->st, $2, target);
2047	   }
2048	}
2049	;
2050
2051string: IDENTIFIER
2052	| USED_IDENTIFIER
2053	;
2054
2055%%
2056
2057void
2058asm_instruction_set_operands(struct asm_instruction *inst,
2059			     const struct prog_dst_register *dst,
2060			     const struct asm_src_register *src0,
2061			     const struct asm_src_register *src1,
2062			     const struct asm_src_register *src2)
2063{
2064   /* In the core ARB extensions only the KIL instruction doesn't have a
2065    * destination register.
2066    */
2067   if (dst == NULL) {
2068      init_dst_reg(& inst->Base.DstReg);
2069   } else {
2070      inst->Base.DstReg = *dst;
2071   }
2072
2073   if (src0 != NULL) {
2074      inst->Base.SrcReg[0] = src0->Base;
2075      inst->SrcReg[0] = *src0;
2076   } else {
2077      init_src_reg(& inst->SrcReg[0]);
2078   }
2079
2080   if (src1 != NULL) {
2081      inst->Base.SrcReg[1] = src1->Base;
2082      inst->SrcReg[1] = *src1;
2083   } else {
2084      init_src_reg(& inst->SrcReg[1]);
2085   }
2086
2087   if (src2 != NULL) {
2088      inst->Base.SrcReg[2] = src2->Base;
2089      inst->SrcReg[2] = *src2;
2090   } else {
2091      init_src_reg(& inst->SrcReg[2]);
2092   }
2093}
2094
2095
2096struct asm_instruction *
2097asm_instruction_ctor(enum prog_opcode op,
2098		     const struct prog_dst_register *dst,
2099		     const struct asm_src_register *src0,
2100		     const struct asm_src_register *src1,
2101		     const struct asm_src_register *src2)
2102{
2103   struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2104
2105   if (inst) {
2106      _mesa_init_instructions(& inst->Base, 1);
2107      inst->Base.Opcode = op;
2108
2109      asm_instruction_set_operands(inst, dst, src0, src1, src2);
2110   }
2111
2112   return inst;
2113}
2114
2115
2116struct asm_instruction *
2117asm_instruction_copy_ctor(const struct prog_instruction *base,
2118			  const struct prog_dst_register *dst,
2119			  const struct asm_src_register *src0,
2120			  const struct asm_src_register *src1,
2121			  const struct asm_src_register *src2)
2122{
2123   struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2124
2125   if (inst) {
2126      _mesa_init_instructions(& inst->Base, 1);
2127      inst->Base.Opcode = base->Opcode;
2128      inst->Base.Saturate = base->Saturate;
2129
2130      asm_instruction_set_operands(inst, dst, src0, src1, src2);
2131   }
2132
2133   return inst;
2134}
2135
2136
2137void
2138init_dst_reg(struct prog_dst_register *r)
2139{
2140   memset(r, 0, sizeof(*r));
2141   r->File = PROGRAM_UNDEFINED;
2142   r->WriteMask = WRITEMASK_XYZW;
2143}
2144
2145
2146/** Like init_dst_reg() but set the File and Index fields. */
2147void
2148set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2149{
2150   const GLint maxIndex = 1 << INST_INDEX_BITS;
2151   const GLint minIndex = 0;
2152   assert(index >= minIndex);
2153   (void) minIndex;
2154   assert(index <= maxIndex);
2155   (void) maxIndex;
2156   assert(file == PROGRAM_TEMPORARY ||
2157	  file == PROGRAM_ADDRESS ||
2158	  file == PROGRAM_OUTPUT);
2159   memset(r, 0, sizeof(*r));
2160   r->File = file;
2161   r->Index = index;
2162   r->WriteMask = WRITEMASK_XYZW;
2163}
2164
2165
2166void
2167init_src_reg(struct asm_src_register *r)
2168{
2169   memset(r, 0, sizeof(*r));
2170   r->Base.File = PROGRAM_UNDEFINED;
2171   r->Base.Swizzle = SWIZZLE_NOOP;
2172   r->Symbol = NULL;
2173}
2174
2175
2176/** Like init_src_reg() but set the File and Index fields.
2177 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2178 */
2179void
2180set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2181{
2182   set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2183}
2184
2185
2186void
2187set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2188                GLuint swizzle)
2189{
2190   const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2191   const GLint minIndex = -(1 << INST_INDEX_BITS);
2192   assert(file < PROGRAM_FILE_MAX);
2193   assert(index >= minIndex);
2194   (void) minIndex;
2195   assert(index <= maxIndex);
2196   (void) maxIndex;
2197   memset(r, 0, sizeof(*r));
2198   r->Base.File = file;
2199   r->Base.Index = index;
2200   r->Base.Swizzle = swizzle;
2201   r->Symbol = NULL;
2202}
2203
2204
2205/**
2206 * Validate the set of inputs used by a program
2207 *
2208 * Validates that legal sets of inputs are used by the program.  In this case
2209 * "used" included both reading the input or binding the input to a name using
2210 * the \c ATTRIB command.
2211 *
2212 * \return
2213 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2214 */
2215int
2216validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2217{
2218   const GLbitfield64 inputs = state->prog->info.inputs_read | state->InputsBound;
2219   GLbitfield ff_inputs = 0;
2220
2221   /* Since Mesa internal attribute indices are different from
2222    * how NV_vertex_program defines attribute aliasing, we have to construct
2223    * a separate usage mask based on how the aliasing is defined.
2224    *
2225    * Note that attribute aliasing is optional if NV_vertex_program is
2226    * unsupported.
2227    */
2228   if (inputs & VERT_BIT_POS)
2229      ff_inputs |= 1 << 0;
2230   if (inputs & VERT_BIT_NORMAL)
2231      ff_inputs |= 1 << 2;
2232   if (inputs & VERT_BIT_COLOR0)
2233      ff_inputs |= 1 << 3;
2234   if (inputs & VERT_BIT_COLOR1)
2235      ff_inputs |= 1 << 4;
2236   if (inputs & VERT_BIT_FOG)
2237      ff_inputs |= 1 << 5;
2238
2239   ff_inputs |= ((inputs & VERT_BIT_TEX_ALL) >> VERT_ATTRIB_TEX0) << 8;
2240
2241   if ((ff_inputs & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2242      yyerror(locp, state, "illegal use of generic attribute and name attribute");
2243      return 0;
2244   }
2245
2246   return 1;
2247}
2248
2249
2250struct asm_symbol *
2251declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2252		 struct YYLTYPE *locp)
2253{
2254   struct asm_symbol *s = NULL;
2255   struct asm_symbol *exist = (struct asm_symbol *)
2256      _mesa_symbol_table_find_symbol(state->st, name);
2257
2258
2259   if (exist != NULL) {
2260      yyerror(locp, state, "redeclared identifier");
2261   } else {
2262      s = calloc(1, sizeof(struct asm_symbol));
2263      s->name = name;
2264      s->type = t;
2265
2266      switch (t) {
2267      case at_temp:
2268         if (state->prog->arb.NumTemporaries >= state->limits->MaxTemps) {
2269	    yyerror(locp, state, "too many temporaries declared");
2270	    free(s);
2271	    return NULL;
2272	 }
2273
2274         s->temp_binding = state->prog->arb.NumTemporaries;
2275         state->prog->arb.NumTemporaries++;
2276	 break;
2277
2278      case at_address:
2279         if (state->prog->arb.NumAddressRegs >=
2280             state->limits->MaxAddressRegs) {
2281	    yyerror(locp, state, "too many address registers declared");
2282	    free(s);
2283	    return NULL;
2284	 }
2285
2286	 /* FINISHME: Add support for multiple address registers.
2287	  */
2288         state->prog->arb.NumAddressRegs++;
2289	 break;
2290
2291      default:
2292	 break;
2293      }
2294
2295      _mesa_symbol_table_add_symbol(state->st, s->name, s);
2296      s->next = state->sym;
2297      state->sym = s;
2298   }
2299
2300   return s;
2301}
2302
2303
2304int add_state_reference(struct gl_program_parameter_list *param_list,
2305			const gl_state_index16 tokens[STATE_LENGTH])
2306{
2307   const GLuint size = 4; /* XXX fix */
2308   char *name;
2309   GLint index;
2310
2311   name = _mesa_program_state_string(tokens);
2312   index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2313                               size, GL_NONE, NULL, tokens, true);
2314   param_list->StateFlags |= _mesa_program_state_flags(tokens);
2315
2316   /* free name string here since we duplicated it in add_parameter() */
2317   free(name);
2318
2319   return index;
2320}
2321
2322
2323int
2324initialize_symbol_from_state(struct gl_program *prog,
2325			     struct asm_symbol *param_var,
2326			     const gl_state_index16 tokens[STATE_LENGTH])
2327{
2328   int idx = -1;
2329   gl_state_index16 state_tokens[STATE_LENGTH];
2330
2331
2332   memcpy(state_tokens, tokens, sizeof(state_tokens));
2333
2334   param_var->type = at_param;
2335   param_var->param_binding_type = PROGRAM_STATE_VAR;
2336
2337   /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2338    * unroll it and call add_state_reference() for each row
2339    */
2340   if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2341	state_tokens[0] == STATE_PROJECTION_MATRIX ||
2342	state_tokens[0] == STATE_MVP_MATRIX ||
2343	state_tokens[0] == STATE_TEXTURE_MATRIX ||
2344	state_tokens[0] == STATE_PROGRAM_MATRIX)
2345       && (state_tokens[2] != state_tokens[3])) {
2346      int row;
2347      const int first_row = state_tokens[2];
2348      const int last_row = state_tokens[3];
2349
2350      for (row = first_row; row <= last_row; row++) {
2351	 state_tokens[2] = state_tokens[3] = row;
2352
2353	 idx = add_state_reference(prog->Parameters, state_tokens);
2354	 if (param_var->param_binding_begin == ~0U) {
2355	    param_var->param_binding_begin = idx;
2356            param_var->param_binding_swizzle = SWIZZLE_XYZW;
2357         }
2358
2359	 param_var->param_binding_length++;
2360      }
2361   }
2362   else {
2363      idx = add_state_reference(prog->Parameters, state_tokens);
2364      if (param_var->param_binding_begin == ~0U) {
2365	 param_var->param_binding_begin = idx;
2366         param_var->param_binding_swizzle = SWIZZLE_XYZW;
2367      }
2368      param_var->param_binding_length++;
2369   }
2370
2371   return idx;
2372}
2373
2374
2375int
2376initialize_symbol_from_param(struct gl_program *prog,
2377			     struct asm_symbol *param_var,
2378			     const gl_state_index16 tokens[STATE_LENGTH])
2379{
2380   int idx = -1;
2381   gl_state_index16 state_tokens[STATE_LENGTH];
2382
2383
2384   memcpy(state_tokens, tokens, sizeof(state_tokens));
2385
2386   assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2387	  || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2388   assert((state_tokens[1] == STATE_ENV)
2389	  || (state_tokens[1] == STATE_LOCAL));
2390
2391   /*
2392    * The param type is STATE_VAR.  The program parameter entry will
2393    * effectively be a pointer into the LOCAL or ENV parameter array.
2394    */
2395   param_var->type = at_param;
2396   param_var->param_binding_type = PROGRAM_STATE_VAR;
2397
2398   /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2399    * we need to unroll it and call add_state_reference() for each row
2400    */
2401   if (state_tokens[2] != state_tokens[3]) {
2402      int row;
2403      const int first_row = state_tokens[2];
2404      const int last_row = state_tokens[3];
2405
2406      for (row = first_row; row <= last_row; row++) {
2407	 state_tokens[2] = state_tokens[3] = row;
2408
2409	 idx = add_state_reference(prog->Parameters, state_tokens);
2410	 if (param_var->param_binding_begin == ~0U) {
2411	    param_var->param_binding_begin = idx;
2412            param_var->param_binding_swizzle = SWIZZLE_XYZW;
2413         }
2414	 param_var->param_binding_length++;
2415      }
2416   }
2417   else {
2418      idx = add_state_reference(prog->Parameters, state_tokens);
2419      if (param_var->param_binding_begin == ~0U) {
2420	 param_var->param_binding_begin = idx;
2421         param_var->param_binding_swizzle = SWIZZLE_XYZW;
2422      }
2423      param_var->param_binding_length++;
2424   }
2425
2426   return idx;
2427}
2428
2429
2430/**
2431 * Put a float/vector constant/literal into the parameter list.
2432 * \param param_var  returns info about the parameter/constant's location,
2433 *                   binding, type, etc.
2434 * \param vec  the vector/constant to add
2435 * \param allowSwizzle  if true, try to consolidate constants which only differ
2436 *                      by a swizzle.  We don't want to do this when building
2437 *                      arrays of constants that may be indexed indirectly.
2438 * \return index of the constant in the parameter list.
2439 */
2440int
2441initialize_symbol_from_const(struct gl_program *prog,
2442			     struct asm_symbol *param_var,
2443			     const struct asm_vector *vec,
2444                             GLboolean allowSwizzle)
2445{
2446   unsigned swizzle;
2447   const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2448                                              vec->data, vec->count,
2449                                              allowSwizzle ? &swizzle : NULL);
2450
2451   param_var->type = at_param;
2452   param_var->param_binding_type = PROGRAM_CONSTANT;
2453
2454   if (param_var->param_binding_begin == ~0U) {
2455      param_var->param_binding_begin = idx;
2456      param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2457   }
2458   param_var->param_binding_length++;
2459
2460   return idx;
2461}
2462
2463
2464char *
2465make_error_string(const char *fmt, ...)
2466{
2467   int length;
2468   char *str;
2469   va_list args;
2470
2471
2472   /* Call vsnprintf once to determine how large the final string is.  Call it
2473    * again to do the actual formatting.  from the vsnprintf manual page:
2474    *
2475    *    Upon successful return, these functions return the number of
2476    *    characters printed  (not including the trailing '\0' used to end
2477    *    output to strings).
2478    */
2479   va_start(args, fmt);
2480   length = 1 + vsnprintf(NULL, 0, fmt, args);
2481   va_end(args);
2482
2483   str = malloc(length);
2484   if (str) {
2485      va_start(args, fmt);
2486      vsnprintf(str, length, fmt, args);
2487      va_end(args);
2488   }
2489
2490   return str;
2491}
2492
2493
2494void
2495yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2496{
2497   char *err_str;
2498
2499
2500   err_str = make_error_string("glProgramStringARB(%s)\n", s);
2501   if (err_str) {
2502      _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2503      free(err_str);
2504   }
2505
2506   err_str = make_error_string("line %u, char %u: error: %s\n",
2507			       locp->first_line, locp->first_column, s);
2508   _mesa_set_program_error(state->ctx, locp->position, err_str);
2509
2510   if (err_str) {
2511      free(err_str);
2512   }
2513}
2514
2515
2516GLboolean
2517_mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2518			GLsizei len, struct asm_parser_state *state)
2519{
2520   struct asm_instruction *inst;
2521   unsigned i;
2522   GLubyte *strz;
2523   GLboolean result = GL_FALSE;
2524   void *temp;
2525   struct asm_symbol *sym;
2526
2527   state->ctx = ctx;
2528   state->prog->Target = target;
2529   state->prog->Parameters = _mesa_new_parameter_list();
2530
2531   /* Make a copy of the program string and force it to be NUL-terminated.
2532    */
2533   strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
2534   if (strz == NULL) {
2535      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2536      return GL_FALSE;
2537   }
2538   memcpy (strz, str, len);
2539   strz[len] = '\0';
2540
2541   state->prog->String = strz;
2542
2543   state->st = _mesa_symbol_table_ctor();
2544
2545   state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2546      ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2547      : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2548
2549   state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2550   state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2551   state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2552   state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2553   state->MaxLights = ctx->Const.MaxLights;
2554   state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2555   state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2556
2557   state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2558      ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2559
2560   _mesa_set_program_error(ctx, -1, NULL);
2561
2562   _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2563   yyparse(state);
2564   _mesa_program_lexer_dtor(state->scanner);
2565
2566
2567   if (ctx->Program.ErrorPos != -1) {
2568      goto error;
2569   }
2570
2571   if (! _mesa_layout_parameters(state)) {
2572      struct YYLTYPE loc;
2573
2574      loc.first_line = 0;
2575      loc.first_column = 0;
2576      loc.position = len;
2577
2578      yyerror(& loc, state, "invalid PARAM usage");
2579      goto error;
2580   }
2581
2582
2583
2584   /* Add one instruction to store the "END" instruction.
2585    */
2586   state->prog->arb.Instructions =
2587      rzalloc_array(state->mem_ctx, struct prog_instruction,
2588                    state->prog->arb.NumInstructions + 1);
2589
2590   if (state->prog->arb.Instructions == NULL) {
2591      goto error;
2592   }
2593
2594   inst = state->inst_head;
2595   for (i = 0; i < state->prog->arb.NumInstructions; i++) {
2596      struct asm_instruction *const temp = inst->next;
2597
2598      state->prog->arb.Instructions[i] = inst->Base;
2599      inst = temp;
2600   }
2601
2602   /* Finally, tag on an OPCODE_END instruction */
2603   {
2604      const GLuint numInst = state->prog->arb.NumInstructions;
2605      _mesa_init_instructions(state->prog->arb.Instructions + numInst, 1);
2606      state->prog->arb.Instructions[numInst].Opcode = OPCODE_END;
2607   }
2608   state->prog->arb.NumInstructions++;
2609
2610   state->prog->arb.NumParameters = state->prog->Parameters->NumParameters;
2611   state->prog->arb.NumAttributes =
2612      util_bitcount64(state->prog->info.inputs_read);
2613
2614   /*
2615    * Initialize native counts to logical counts.  The device driver may
2616    * change them if program is translated into a hardware program.
2617    */
2618   state->prog->arb.NumNativeInstructions = state->prog->arb.NumInstructions;
2619   state->prog->arb.NumNativeTemporaries = state->prog->arb.NumTemporaries;
2620   state->prog->arb.NumNativeParameters = state->prog->arb.NumParameters;
2621   state->prog->arb.NumNativeAttributes = state->prog->arb.NumAttributes;
2622   state->prog->arb.NumNativeAddressRegs = state->prog->arb.NumAddressRegs;
2623
2624   result = GL_TRUE;
2625
2626error:
2627   for (inst = state->inst_head; inst != NULL; inst = temp) {
2628      temp = inst->next;
2629      free(inst);
2630   }
2631
2632   state->inst_head = NULL;
2633   state->inst_tail = NULL;
2634
2635   for (sym = state->sym; sym != NULL; sym = temp) {
2636      temp = sym->next;
2637
2638      free((void *) sym->name);
2639      free(sym);
2640   }
2641   state->sym = NULL;
2642
2643   _mesa_symbol_table_dtor(state->st);
2644   state->st = NULL;
2645
2646   return result;
2647}
2648