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