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