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