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