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