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