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