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