ARB prog parse: Fix cut-and-paste error for constant vectors
[mesa.git] / src / mesa / shader / program_parse.y
1 %{
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "main/mtypes.h"
29 #include "main/imports.h"
30 #include "program.h"
31 #include "prog_parameter.h"
32 #include "prog_parameter_layout.h"
33 #include "prog_statevars.h"
34 #include "prog_instruction.h"
35
36 #include "symbol_table.h"
37 #include "program_parser.h"
38
39 extern void *yy_scan_string(char *);
40 extern void yy_delete_buffer(void *);
41
42 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
43 char *name, enum asm_type t, struct YYLTYPE *locp);
44
45 static int add_state_reference(struct gl_program_parameter_list *param_list,
46 const gl_state_index tokens[STATE_LENGTH]);
47
48 static int initialize_symbol_from_state(struct gl_program *prog,
49 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
50
51 static int initialize_symbol_from_param(struct gl_program *prog,
52 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
53
54 static int initialize_symbol_from_const(struct gl_program *prog,
55 struct asm_symbol *param_var, const struct asm_vector *vec);
56
57 static int yyparse(struct asm_parser_state *state);
58
59 static char *make_error_string(const char *fmt, ...);
60
61 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
62 const char *s);
63
64 static int validate_inputs(struct YYLTYPE *locp,
65 struct asm_parser_state *state);
66
67 static void init_dst_reg(struct prog_dst_register *r);
68
69 static void init_src_reg(struct asm_src_register *r);
70
71 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
72 const struct prog_dst_register *dst, const struct asm_src_register *src0,
73 const struct asm_src_register *src1, const struct asm_src_register *src2);
74
75 #ifndef FALSE
76 #define FALSE 0
77 #define TRUE (!FALSE)
78 #endif
79
80 #define YYLLOC_DEFAULT(Current, Rhs, N) \
81 do { \
82 if (YYID(N)) { \
83 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
84 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
85 (Current).position = YYRHSLOC(Rhs, 1).position; \
86 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
87 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
88 } else { \
89 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
90 (Current).last_line = (Current).first_line; \
91 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
92 (Current).last_column = (Current).first_column; \
93 (Current).position = YYRHSLOC(Rhs, 0).position \
94 + (Current).first_column; \
95 } \
96 } while(YYID(0))
97
98 #define YYLEX_PARAM state->scanner
99 %}
100
101 %pure-parser
102 %locations
103 %parse-param { struct asm_parser_state *state }
104 %error-verbose
105 %lex-param { void *scanner }
106
107 %union {
108 struct asm_instruction *inst;
109 struct asm_symbol *sym;
110 struct asm_symbol temp_sym;
111 struct asm_swizzle_mask swiz_mask;
112 struct asm_src_register src_reg;
113 struct prog_dst_register dst_reg;
114 struct prog_instruction temp_inst;
115 char *string;
116 unsigned result;
117 unsigned attrib;
118 int integer;
119 float real;
120 unsigned state[5];
121 int negate;
122 struct asm_vector vector;
123 gl_inst_opcode opcode;
124 }
125
126 %token ARBvp_10 ARBfp_10
127
128 /* Tokens for assembler pseudo-ops */
129 %token <integer> ADDRESS
130 %token ALIAS ATTRIB
131 %token OPTION OUTPUT
132 %token PARAM
133 %token <integer> TEMP
134 %token END
135
136 /* Tokens for instructions */
137 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
138 %token <temp_inst> ARL KIL SWZ
139
140 %token <integer> INTEGER
141 %token <real> REAL
142
143 %token AMBIENT ATTENUATION
144 %token BACK
145 %token CLIP COLOR
146 %token DEPTH DIFFUSE DIRECTION
147 %token EMISSION ENV EYE
148 %token FOG FOGCOORD FRAGMENT FRONT
149 %token HALF
150 %token INVERSE INVTRANS
151 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
152 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
153 %token NORMAL
154 %token OBJECT
155 %token PALETTE PARAMS PLANE POINT POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
156 %token RANGE RESULT ROW
157 %token SCENECOLOR SECONDARY SHININESS SIZE SPECULAR SPOT STATE
158 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
159 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
160 %token VERTEX VTXATTRIB
161 %token WEIGHT
162
163 %token <string> IDENTIFIER
164 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
165 %token DOT_DOT
166 %token DOT
167
168 %type <inst> instruction ALU_instruction TexInstruction
169 %type <inst> ARL_instruction VECTORop_instruction
170 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
171 %type <inst> TRIop_instruction SWZ_instruction SAMPLE_instruction
172 %type <inst> KIL_instruction
173
174 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
175 %type <src_reg> srcReg scalarSrcReg swizzleSrcReg
176 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle extSwizComp
177 %type <swiz_mask> optionalMask
178 %type <integer> extSwizSel
179
180 %type <sym> progParamArray
181 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
182 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
183 %type <sym> addrReg
184 %type <swiz_mask> addrComponent addrWriteMask
185
186 %type <result> resultBinding resultColBinding
187 %type <integer> optFaceType optColorType
188 %type <integer> optResultFaceType optResultColorType
189
190 %type <integer> optTexImageUnitNum texImageUnitNum
191 %type <integer> optTexCoordUnitNum texCoordUnitNum
192 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
193 %type <integer> texImageUnit texTarget
194 %type <integer> vtxAttribNum
195
196 %type <attrib> attribBinding vtxAttribItem fragAttribItem
197
198 %type <temp_sym> paramSingleInit paramSingleItemDecl
199 %type <integer> optArraySize
200
201 %type <state> stateSingleItem stateMultipleItem
202 %type <state> stateMaterialItem
203 %type <state> stateLightItem stateLightModelItem stateLightProdItem
204 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
205 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
206 %type <state> stateTexEnvItem
207
208 %type <state> stateLModProperty
209 %type <state> stateMatrixName optMatrixRows
210
211 %type <integer> stateMatProperty
212 %type <integer> stateLightProperty stateSpotProperty
213 %type <integer> stateLightNumber stateLProdProperty
214 %type <integer> stateTexGenType stateTexGenCoord
215 %type <integer> stateTexEnvProperty
216 %type <integer> stateFogProperty
217 %type <integer> stateClipPlaneNum
218 %type <integer> statePointProperty
219
220 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
221 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
222 %type <integer> stateProgramMatNum
223
224 %type <integer> ambDiffSpecProperty
225
226 %type <state> programSingleItem progEnvParam progLocalParam
227 %type <state> programMultipleItem progEnvParams progLocalParams
228
229 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
230 %type <temp_sym> paramSingleItemUse
231
232 %type <integer> progEnvParamNum progLocalParamNum
233 %type <state> progEnvParamNums progLocalParamNums
234
235 %type <vector> paramConstDecl paramConstUse
236 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
237 %type <real> signedFloatConstant
238 %type <negate> optionalSign
239
240 %{
241 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
242 void *yyscanner);
243 %}
244
245 %%
246
247 program: language optionSequence statementSequence END
248 ;
249
250 language: ARBvp_10
251 {
252 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
253 yyerror(& @1, state, "invalid fragment program header");
254
255 }
256 state->mode = ARB_vertex;
257 }
258 | ARBfp_10
259 {
260 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
261 yyerror(& @1, state, "invalid vertex program header");
262 }
263 state->mode = ARB_fragment;
264 }
265 ;
266
267 optionSequence: optionSequence option
268 |
269 ;
270
271 option: OPTION IDENTIFIER ';'
272 {
273 int valid = 0;
274
275 if (state->mode == ARB_vertex) {
276 valid = _mesa_ARBvp_parse_option(state, $2);
277 } else if (state->mode == ARB_fragment) {
278 valid = _mesa_ARBfp_parse_option(state, $2);
279 }
280
281
282 if (!valid) {
283 yyerror(& @2, state, "invalid option string");
284 YYERROR;
285 }
286 }
287 ;
288
289 statementSequence: statementSequence statement
290 |
291 ;
292
293 statement: instruction ';'
294 {
295 if ($1 != NULL) {
296 if (state->inst_tail == NULL) {
297 state->inst_head = $1;
298 } else {
299 state->inst_tail->next = $1;
300 }
301
302 state->inst_tail = $1;
303 $1->next = NULL;
304
305 state->prog->NumInstructions++;
306 }
307 }
308 | namingStatement ';'
309 ;
310
311 instruction: ALU_instruction
312 {
313 $$ = $1;
314 state->prog->NumAluInstructions++;
315 }
316 | TexInstruction
317 {
318 $$ = $1;
319 state->prog->NumTexInstructions++;
320 }
321 ;
322
323 ALU_instruction: ARL_instruction
324 | VECTORop_instruction
325 | SCALARop_instruction
326 | BINSCop_instruction
327 | BINop_instruction
328 | TRIop_instruction
329 | SWZ_instruction
330 ;
331
332 TexInstruction: SAMPLE_instruction
333 | KIL_instruction
334 ;
335
336 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
337 {
338 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
339 }
340 ;
341
342 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
343 {
344 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
345 $$->Base.SaturateMode = $1.SaturateMode;
346 }
347 ;
348
349 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
350 {
351 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
352 $$->Base.SaturateMode = $1.SaturateMode;
353 }
354 ;
355
356 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
357 {
358 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, NULL);
359 $$->Base.SaturateMode = $1.SaturateMode;
360 }
361 ;
362
363
364 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
365 {
366 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, NULL);
367 $$->Base.SaturateMode = $1.SaturateMode;
368 }
369 ;
370
371 TRIop_instruction: TRI_OP maskedDstReg ','
372 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
373 {
374 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, & $8);
375 $$->Base.SaturateMode = $1.SaturateMode;
376 }
377 ;
378
379 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
380 {
381 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
382 if ($$ != NULL) {
383 $$->Base.SaturateMode = $1.SaturateMode;
384 $$->Base.TexSrcUnit = $6;
385 $$->Base.TexSrcTarget = $8;
386
387 state->prog->TexturesUsed[$6] |= (1U << $8);
388 }
389 }
390 ;
391
392 KIL_instruction: KIL swizzleSrcReg
393 {
394 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
395 state->fragment.UsesKill = 1;
396 }
397 ;
398
399 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
400 {
401 $$ = $2;
402 }
403 ;
404
405 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
406 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
407 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
408 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
409 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
410 ;
411
412 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
413 {
414 /* FIXME: Is this correct? Should the extenedSwizzle be applied
415 * FIXME: to the existing swizzle?
416 */
417 $4.Base.Swizzle = $6.swizzle;
418
419 $$ = asm_instruction_ctor(OPCODE_SWZ, & $2, & $4, NULL, NULL);
420 $$->Base.SaturateMode = $1.SaturateMode;
421 }
422 ;
423
424 scalarSrcReg: optionalSign srcReg scalarSuffix
425 {
426 $$ = $2;
427
428 if ($1) {
429 $$.Base.Negate = ~$$.Base.Negate;
430 }
431
432 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
433 $3.swizzle);
434 }
435 ;
436
437 swizzleSrcReg: optionalSign srcReg swizzleSuffix
438 {
439 $$ = $2;
440
441 if ($1) {
442 $$.Base.Negate = ~$$.Base.Negate;
443 }
444
445 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
446 $3.swizzle);
447 }
448 ;
449
450 maskedDstReg: dstReg optionalMask
451 {
452 $$ = $1;
453 $$.WriteMask = $2.mask;
454
455 if ($$.File == PROGRAM_OUTPUT) {
456 /* Technically speaking, this should check that it is in
457 * vertex program mode. However, PositionInvariant can never be
458 * set in fragment program mode, so it is somewhat irrelevant.
459 */
460 if (state->option.PositionInvariant
461 && ($$.Index == VERT_RESULT_HPOS)) {
462 yyerror(& @1, state, "position-invariant programs cannot "
463 "write position");
464 YYERROR;
465 }
466
467 state->prog->OutputsWritten |= (1U << $$.Index);
468 }
469 }
470 ;
471
472 maskedAddrReg: addrReg addrWriteMask
473 {
474 init_dst_reg(& $$);
475 $$.File = PROGRAM_ADDRESS;
476 $$.Index = 0;
477 $$.WriteMask = $2.mask;
478 }
479 ;
480
481 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
482 {
483 $$.swizzle = MAKE_SWIZZLE4($1.swizzle, $3.swizzle,
484 $5.swizzle, $7.swizzle);
485 $$.mask = ($1.mask) | ($3.mask << 1) | ($5.mask << 2)
486 | ($7.mask << 3);
487 }
488 ;
489
490 extSwizComp: optionalSign extSwizSel
491 {
492 $$.swizzle = $2;
493 $$.mask = ($1) ? 1 : 0;
494 }
495 ;
496
497 extSwizSel: INTEGER
498 {
499 if (($1 != 0) && ($1 != 1)) {
500 yyerror(& @1, state, "invalid extended swizzle selector");
501 YYERROR;
502 }
503
504 $$ = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
505 }
506 | IDENTIFIER
507 {
508 if (strlen($1) > 1) {
509 yyerror(& @1, state, "invalid extended swizzle selector");
510 YYERROR;
511 }
512
513 switch ($1[0]) {
514 case 'x':
515 $$ = SWIZZLE_X;
516 break;
517 case 'y':
518 $$ = SWIZZLE_Y;
519 break;
520 case 'z':
521 $$ = SWIZZLE_Z;
522 break;
523 case 'w':
524 $$ = SWIZZLE_W;
525 break;
526 default:
527 yyerror(& @1, state, "invalid extended swizzle selector");
528 YYERROR;
529 break;
530 }
531 }
532 ;
533
534 srcReg: IDENTIFIER /* temporaryReg | progParamSingle */
535 {
536 struct asm_symbol *const s = (struct asm_symbol *)
537 _mesa_symbol_table_find_symbol(state->st, 0, $1);
538
539 if (s == NULL) {
540 yyerror(& @1, state, "invalid operand variable");
541 YYERROR;
542 } else if ((s->type != at_param) && (s->type != at_temp)
543 && (s->type != at_attrib)) {
544 yyerror(& @1, state, "invalid operand variable");
545 YYERROR;
546 } else if ((s->type == at_param) && s->param_is_array) {
547 yyerror(& @1, state, "non-array access to array PARAM");
548 YYERROR;
549 }
550
551 init_src_reg(& $$);
552 switch (s->type) {
553 case at_temp:
554 $$.Base.File = PROGRAM_TEMPORARY;
555 $$.Base.Index = s->temp_binding;
556 break;
557 case at_param:
558 $$.Base.File = s->param_binding_type;
559 $$.Base.Index = s->param_binding_begin;
560 break;
561 case at_attrib:
562 $$.Base.File = PROGRAM_INPUT;
563 $$.Base.Index = s->attrib_binding;
564 state->prog->InputsRead |= (1U << $$.Base.Index);
565
566 if (!validate_inputs(& @1, state)) {
567 YYERROR;
568 }
569 break;
570
571 default:
572 YYERROR;
573 break;
574 }
575 }
576 | attribBinding
577 {
578 init_src_reg(& $$);
579 $$.Base.File = PROGRAM_INPUT;
580 $$.Base.Index = $1;
581 state->prog->InputsRead |= (1U << $$.Base.Index);
582
583 if (!validate_inputs(& @1, state)) {
584 YYERROR;
585 }
586 }
587 | progParamArray '[' progParamArrayMem ']'
588 {
589 if (! $3.Base.RelAddr
590 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
591 yyerror(& @3, state, "out of bounds array access");
592 YYERROR;
593 }
594
595 init_src_reg(& $$);
596 $$.Base.File = $1->param_binding_type;
597
598 if ($3.Base.RelAddr) {
599 $1->param_accessed_indirectly = 1;
600
601 $$.Base.RelAddr = 1;
602 $$.Base.Index = $3.Base.Index;
603 $$.Symbol = $1;
604 } else {
605 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
606 }
607 }
608 | paramSingleItemUse
609 {
610 init_src_reg(& $$);
611 $$.Base.File = ($1.name != NULL)
612 ? $1.param_binding_type
613 : PROGRAM_CONSTANT;
614 $$.Base.Index = $1.param_binding_begin;
615 }
616 ;
617
618 dstReg: resultBinding
619 {
620 init_dst_reg(& $$);
621 $$.File = PROGRAM_OUTPUT;
622 $$.Index = $1;
623 }
624 | IDENTIFIER /* temporaryReg | vertexResultReg */
625 {
626 struct asm_symbol *const s = (struct asm_symbol *)
627 _mesa_symbol_table_find_symbol(state->st, 0, $1);
628
629 if (s == NULL) {
630 yyerror(& @1, state, "invalid operand variable");
631 YYERROR;
632 } else if ((s->type != at_output) && (s->type != at_temp)) {
633 yyerror(& @1, state, "invalid operand variable");
634 YYERROR;
635 }
636
637 init_dst_reg(& $$);
638 if (s->type == at_temp) {
639 $$.File = PROGRAM_TEMPORARY;
640 $$.Index = s->temp_binding;
641 } else {
642 $$.File = s->param_binding_type;
643 $$.Index = s->param_binding_begin;
644 }
645 }
646 ;
647
648 progParamArray: IDENTIFIER
649 {
650 struct asm_symbol *const s = (struct asm_symbol *)
651 _mesa_symbol_table_find_symbol(state->st, 0, $1);
652
653 if (s == NULL) {
654 yyerror(& @1, state, "invalid operand variable");
655 YYERROR;
656 } else if ((s->type != at_param) || !s->param_is_array) {
657 yyerror(& @1, state, "array access to non-PARAM variable");
658 YYERROR;
659 } else {
660 $$ = s;
661 }
662 }
663 ;
664
665 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
666
667 progParamArrayAbs: INTEGER
668 {
669 init_src_reg(& $$);
670 $$.Base.Index = $1;
671 }
672 ;
673
674 progParamArrayRel: addrReg addrComponent addrRegRelOffset
675 {
676 /* FINISHME: Add support for multiple address registers.
677 */
678 /* FINISHME: Add support for 4-component address registers.
679 */
680 init_src_reg(& $$);
681 $$.Base.RelAddr = 1;
682 $$.Base.Index = $3;
683 }
684 ;
685
686 addrRegRelOffset: { $$ = 0; }
687 | '+' addrRegPosOffset { $$ = $2; }
688 | '-' addrRegNegOffset { $$ = -$2; }
689 ;
690
691 addrRegPosOffset: INTEGER
692 {
693 if (($1 < 0) || ($1 > 63)) {
694 yyerror(& @1, state,
695 "relative address offset too large (positive)");
696 YYERROR;
697 } else {
698 $$ = $1;
699 }
700 }
701 ;
702
703 addrRegNegOffset: INTEGER
704 {
705 if (($1 < 0) || ($1 > 64)) {
706 yyerror(& @1, state,
707 "relative address offset too large (negative)");
708 YYERROR;
709 } else {
710 $$ = $1;
711 }
712 }
713 ;
714
715 addrReg: IDENTIFIER
716 {
717 struct asm_symbol *const s = (struct asm_symbol *)
718 _mesa_symbol_table_find_symbol(state->st, 0, $1);
719
720 if (s == NULL) {
721 yyerror(& @1, state, "invalid array member");
722 YYERROR;
723 } else if (s->type != at_address) {
724 yyerror(& @1, state,
725 "invalid variable for indexed array access");
726 YYERROR;
727 } else {
728 $$ = s;
729 }
730 }
731 ;
732
733 addrComponent: MASK1
734 {
735 if ($1.mask != WRITEMASK_X) {
736 yyerror(& @1, state, "invalid address component selector");
737 YYERROR;
738 } else {
739 $$ = $1;
740 }
741 }
742 ;
743
744 addrWriteMask: MASK1
745 {
746 if ($1.mask != WRITEMASK_X) {
747 yyerror(& @1, state,
748 "address register write mask must be \".x\"");
749 YYERROR;
750 } else {
751 $$ = $1;
752 }
753 }
754 ;
755
756 scalarSuffix: MASK1;
757
758 swizzleSuffix: MASK1
759 | MASK4
760 | SWIZZLE
761 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
762 ;
763
764 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
765 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
766 ;
767
768 namingStatement: ATTRIB_statement
769 | PARAM_statement
770 | TEMP_statement
771 | ADDRESS_statement
772 | OUTPUT_statement
773 | ALIAS_statement
774 ;
775
776 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
777 {
778 struct asm_symbol *const s =
779 declare_variable(state, $2, at_attrib, & @2);
780
781 if (s == NULL) {
782 YYERROR;
783 } else {
784 s->attrib_binding = $4;
785 state->InputsBound |= (1U << s->attrib_binding);
786
787 if (!validate_inputs(& @4, state)) {
788 YYERROR;
789 }
790 }
791 }
792 ;
793
794 attribBinding: VERTEX vtxAttribItem
795 {
796 $$ = $2;
797 }
798 | FRAGMENT fragAttribItem
799 {
800 $$ = $2;
801 }
802 ;
803
804 vtxAttribItem: POSITION
805 {
806 $$ = VERT_ATTRIB_POS;
807 }
808 | WEIGHT vtxOptWeightNum
809 {
810 $$ = VERT_ATTRIB_WEIGHT;
811 }
812 | NORMAL
813 {
814 $$ = VERT_ATTRIB_NORMAL;
815 }
816 | COLOR optColorType
817 {
818 $$ = VERT_ATTRIB_COLOR0 + $2;
819 }
820 | FOGCOORD
821 {
822 $$ = VERT_ATTRIB_FOG;
823 }
824 | TEXCOORD optTexCoordUnitNum
825 {
826 $$ = VERT_ATTRIB_TEX0 + $2;
827 }
828 | MATRIXINDEX '[' vtxWeightNum ']'
829 {
830 YYERROR;
831 }
832 | VTXATTRIB '[' vtxAttribNum ']'
833 {
834 $$ = VERT_ATTRIB_GENERIC0 + $3;
835 }
836 ;
837
838 vtxAttribNum: INTEGER
839 {
840 if ((unsigned) $1 >= state->limits->MaxAttribs) {
841 yyerror(& @1, state, "invalid vertex attribute reference");
842 YYERROR;
843 }
844
845 $$ = $1;
846 }
847 ;
848
849 vtxOptWeightNum: | '[' vtxWeightNum ']';
850 vtxWeightNum: INTEGER;
851
852 fragAttribItem: POSITION
853 {
854 $$ = FRAG_ATTRIB_WPOS;
855 }
856 | COLOR optColorType
857 {
858 $$ = FRAG_ATTRIB_COL0 + $2;
859 }
860 | FOGCOORD
861 {
862 $$ = FRAG_ATTRIB_FOGC;
863 }
864 | TEXCOORD optTexCoordUnitNum
865 {
866 $$ = FRAG_ATTRIB_TEX0 + $2;
867 }
868 ;
869
870 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
871
872 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
873 {
874 struct asm_symbol *const s =
875 declare_variable(state, $2, at_param, & @2);
876
877 if (s == NULL) {
878 YYERROR;
879 } else {
880 s->param_binding_type = $3.param_binding_type;
881 s->param_binding_begin = $3.param_binding_begin;
882 s->param_binding_length = $3.param_binding_length;
883 s->param_is_array = 0;
884 }
885 }
886 ;
887
888 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
889 {
890 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
891 yyerror(& @4, state,
892 "parameter array size and number of bindings must match");
893 YYERROR;
894 } else {
895 struct asm_symbol *const s =
896 declare_variable(state, $2, $6.type, & @2);
897
898 if (s == NULL) {
899 YYERROR;
900 } else {
901 s->param_binding_type = $6.param_binding_type;
902 s->param_binding_begin = $6.param_binding_begin;
903 s->param_binding_length = $6.param_binding_length;
904 s->param_is_array = 1;
905 }
906 }
907 }
908 ;
909
910 optArraySize:
911 {
912 $$ = 0;
913 }
914 | INTEGER
915 {
916 if (($1 < 1) || ((unsigned) $1 >= state->limits->MaxParameters)) {
917 yyerror(& @1, state, "invalid parameter array size");
918 YYERROR;
919 } else {
920 $$ = $1;
921 }
922 }
923 ;
924
925 paramSingleInit: '=' paramSingleItemDecl
926 {
927 $$ = $2;
928 }
929 ;
930
931 paramMultipleInit: '=' '{' paramMultInitList '}'
932 {
933 $$ = $3;
934 }
935 ;
936
937 paramMultInitList: paramMultipleItem
938 | paramMultInitList ',' paramMultipleItem
939 {
940 $1.param_binding_length += $3.param_binding_length;
941 $$ = $1;
942 }
943 ;
944
945 paramSingleItemDecl: stateSingleItem
946 {
947 memset(& $$, 0, sizeof($$));
948 $$.param_binding_begin = ~0;
949 initialize_symbol_from_state(state->prog, & $$, $1);
950 }
951 | programSingleItem
952 {
953 memset(& $$, 0, sizeof($$));
954 $$.param_binding_begin = ~0;
955 initialize_symbol_from_param(state->prog, & $$, $1);
956 }
957 | paramConstDecl
958 {
959 memset(& $$, 0, sizeof($$));
960 $$.param_binding_begin = ~0;
961 initialize_symbol_from_const(state->prog, & $$, & $1);
962 }
963 ;
964
965 paramSingleItemUse: stateSingleItem
966 {
967 memset(& $$, 0, sizeof($$));
968 $$.param_binding_begin = ~0;
969 initialize_symbol_from_state(state->prog, & $$, $1);
970 }
971 | programSingleItem
972 {
973 memset(& $$, 0, sizeof($$));
974 $$.param_binding_begin = ~0;
975 initialize_symbol_from_param(state->prog, & $$, $1);
976 }
977 | paramConstUse
978 {
979 memset(& $$, 0, sizeof($$));
980 $$.param_binding_begin = ~0;
981 initialize_symbol_from_const(state->prog, & $$, & $1);
982 }
983 ;
984
985 paramMultipleItem: stateMultipleItem
986 {
987 memset(& $$, 0, sizeof($$));
988 $$.param_binding_begin = ~0;
989 initialize_symbol_from_state(state->prog, & $$, $1);
990 }
991 | programMultipleItem
992 {
993 memset(& $$, 0, sizeof($$));
994 $$.param_binding_begin = ~0;
995 initialize_symbol_from_param(state->prog, & $$, $1);
996 }
997 | paramConstDecl
998 {
999 memset(& $$, 0, sizeof($$));
1000 $$.param_binding_begin = ~0;
1001 initialize_symbol_from_const(state->prog, & $$, & $1);
1002 }
1003 ;
1004
1005 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1006 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1007 ;
1008
1009 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1010 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1011 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1012 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1013 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1014 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1015 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1016 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1017 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1018 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1019 ;
1020
1021 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1022 {
1023 memset($$, 0, sizeof($$));
1024 $$[0] = STATE_MATERIAL;
1025 $$[1] = $2;
1026 $$[2] = $3;
1027 }
1028 ;
1029
1030 stateMatProperty: ambDiffSpecProperty
1031 {
1032 $$ = $1;
1033 }
1034 | EMISSION
1035 {
1036 $$ = STATE_EMISSION;
1037 }
1038 | SHININESS
1039 {
1040 $$ = STATE_SHININESS;
1041 }
1042 ;
1043
1044 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1045 {
1046 memset($$, 0, sizeof($$));
1047 $$[0] = STATE_LIGHT;
1048 $$[1] = $3;
1049 $$[2] = $5;
1050 }
1051 ;
1052
1053 stateLightProperty: ambDiffSpecProperty
1054 {
1055 $$ = $1;
1056 }
1057 | POSITION
1058 {
1059 $$ = STATE_POSITION;
1060 }
1061 | ATTENUATION
1062 {
1063 $$ = STATE_ATTENUATION;
1064 }
1065 | SPOT stateSpotProperty
1066 {
1067 $$ = $2;
1068 }
1069 | HALF
1070 {
1071 $$ = STATE_HALF_VECTOR;
1072 }
1073 ;
1074
1075 stateSpotProperty: DIRECTION
1076 {
1077 $$ = STATE_SPOT_DIRECTION;
1078 }
1079 ;
1080
1081 stateLightModelItem: LIGHTMODEL stateLModProperty
1082 {
1083 $$[0] = $2[0];
1084 $$[1] = $2[1];
1085 }
1086 ;
1087
1088 stateLModProperty: AMBIENT
1089 {
1090 memset($$, 0, sizeof($$));
1091 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1092 }
1093 | optFaceType SCENECOLOR
1094 {
1095 memset($$, 0, sizeof($$));
1096 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1097 $$[1] = $1;
1098 }
1099 ;
1100
1101 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1102 {
1103 memset($$, 0, sizeof($$));
1104 $$[0] = STATE_LIGHTPROD;
1105 $$[1] = $3;
1106 $$[2] = $5;
1107 $$[3] = $6;
1108 }
1109 ;
1110
1111 stateLProdProperty: ambDiffSpecProperty;
1112
1113 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1114 {
1115 memset($$, 0, sizeof($$));
1116 $$[0] = $3;
1117 $$[1] = $2;
1118 }
1119 ;
1120
1121 stateTexEnvProperty: COLOR
1122 {
1123 $$ = STATE_TEXENV_COLOR;
1124 }
1125 ;
1126
1127 ambDiffSpecProperty: AMBIENT
1128 {
1129 $$ = STATE_AMBIENT;
1130 }
1131 | DIFFUSE
1132 {
1133 $$ = STATE_DIFFUSE;
1134 }
1135 | SPECULAR
1136 {
1137 $$ = STATE_SPECULAR;
1138 }
1139 ;
1140
1141 stateLightNumber: INTEGER
1142 {
1143 if ((unsigned) $1 >= state->MaxLights) {
1144 yyerror(& @1, state, "invalid light selector");
1145 YYERROR;
1146 }
1147
1148 $$ = $1;
1149 }
1150 ;
1151
1152 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1153 {
1154 memset($$, 0, sizeof($$));
1155 $$[0] = STATE_TEXGEN;
1156 $$[1] = $2;
1157 $$[2] = $3 + $4;
1158 }
1159 ;
1160
1161 stateTexGenType: EYE
1162 {
1163 $$ = STATE_TEXGEN_EYE_S;
1164 }
1165 | OBJECT
1166 {
1167 $$ = STATE_TEXGEN_OBJECT_S;
1168 }
1169 ;
1170 stateTexGenCoord: TEXGEN_S
1171 {
1172 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1173 }
1174 | TEXGEN_T
1175 {
1176 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1177 }
1178 | TEXGEN_R
1179 {
1180 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1181 }
1182 | TEXGEN_Q
1183 {
1184 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1185 }
1186 ;
1187
1188 stateFogItem: FOG stateFogProperty
1189 {
1190 memset($$, 0, sizeof($$));
1191 $$[0] = $2;
1192 }
1193 ;
1194
1195 stateFogProperty: COLOR
1196 {
1197 $$ = STATE_FOG_COLOR;
1198 }
1199 | PARAMS
1200 {
1201 $$ = STATE_FOG_PARAMS;
1202 }
1203 ;
1204
1205 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1206 {
1207 memset($$, 0, sizeof($$));
1208 $$[0] = STATE_CLIPPLANE;
1209 $$[1] = $3;
1210 }
1211 ;
1212
1213 stateClipPlaneNum: INTEGER
1214 {
1215 if ((unsigned) $1 >= state->MaxClipPlanes) {
1216 yyerror(& @1, state, "invalid clip plane selector");
1217 YYERROR;
1218 }
1219
1220 $$ = $1;
1221 }
1222 ;
1223
1224 statePointItem: POINT statePointProperty
1225 {
1226 memset($$, 0, sizeof($$));
1227 $$[0] = $2;
1228 }
1229 ;
1230
1231 statePointProperty: SIZE
1232 {
1233 $$ = STATE_POINT_SIZE;
1234 }
1235 | ATTENUATION
1236 {
1237 $$ = STATE_POINT_ATTENUATION;
1238 }
1239 ;
1240
1241 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1242 {
1243 $$[0] = $1[0];
1244 $$[1] = $1[1];
1245 $$[2] = $4;
1246 $$[3] = $4;
1247 $$[4] = $1[2];
1248 }
1249 ;
1250
1251 stateMatrixRows: stateMatrixItem optMatrixRows
1252 {
1253 $$[0] = $1[0];
1254 $$[1] = $1[1];
1255 $$[2] = $2[2];
1256 $$[3] = $2[3];
1257 $$[4] = $1[2];
1258 }
1259 ;
1260
1261 optMatrixRows:
1262 {
1263 $$[2] = 0;
1264 $$[3] = 3;
1265 }
1266 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1267 {
1268 /* It seems logical that the matrix row range specifier would have
1269 * to specify a range or more than one row (i.e., $5 > $3).
1270 * However, the ARB_vertex_program spec says "a program will fail
1271 * to load if <a> is greater than <b>." This means that $3 == $5
1272 * is valid.
1273 */
1274 if ($3 > $5) {
1275 yyerror(& @3, state, "invalid matrix row range");
1276 YYERROR;
1277 }
1278
1279 $$[2] = $3;
1280 $$[3] = $5;
1281 }
1282 ;
1283
1284 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1285 {
1286 $$[0] = $2[0];
1287 $$[1] = $2[1];
1288 $$[2] = $3;
1289 }
1290 ;
1291
1292 stateOptMatModifier:
1293 {
1294 $$ = 0;
1295 }
1296 | stateMatModifier
1297 {
1298 $$ = $1;
1299 }
1300 ;
1301
1302 stateMatModifier: INVERSE
1303 {
1304 $$ = STATE_MATRIX_INVERSE;
1305 }
1306 | TRANSPOSE
1307 {
1308 $$ = STATE_MATRIX_TRANSPOSE;
1309 }
1310 | INVTRANS
1311 {
1312 $$ = STATE_MATRIX_INVTRANS;
1313 }
1314 ;
1315
1316 stateMatrixRowNum: INTEGER
1317 {
1318 if ($1 > 3) {
1319 yyerror(& @1, state, "invalid matrix row reference");
1320 YYERROR;
1321 }
1322
1323 $$ = $1;
1324 }
1325 ;
1326
1327 stateMatrixName: MODELVIEW stateOptModMatNum
1328 {
1329 $$[0] = STATE_MODELVIEW_MATRIX;
1330 $$[1] = $2;
1331 }
1332 | PROJECTION
1333 {
1334 $$[0] = STATE_PROJECTION_MATRIX;
1335 $$[1] = 0;
1336 }
1337 | MVP
1338 {
1339 $$[0] = STATE_MVP_MATRIX;
1340 $$[1] = 0;
1341 }
1342 | TEXTURE optTexCoordUnitNum
1343 {
1344 $$[0] = STATE_TEXTURE_MATRIX;
1345 $$[1] = $2;
1346 }
1347 | PALETTE '[' statePaletteMatNum ']'
1348 {
1349 YYERROR;
1350 }
1351 | MAT_PROGRAM '[' stateProgramMatNum ']'
1352 {
1353 $$[0] = STATE_PROGRAM_MATRIX;
1354 $$[1] = $3;
1355 }
1356 ;
1357
1358 stateOptModMatNum:
1359 {
1360 $$ = 0;
1361 }
1362 | stateModMatNum
1363 {
1364 $$ = $1;
1365 }
1366 ;
1367 stateModMatNum: INTEGER
1368 {
1369 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1370 * zero is valid.
1371 */
1372 if ($1 != 0) {
1373 yyerror(& @1, state, "invalid modelview matrix index");
1374 YYERROR;
1375 }
1376
1377 $$ = $1;
1378 }
1379 ;
1380 statePaletteMatNum: INTEGER
1381 {
1382 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1383 * through here. The error will be generated later.
1384 */
1385 $$ = $1;
1386 }
1387 ;
1388 stateProgramMatNum: INTEGER
1389 {
1390 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1391 yyerror(& @1, state, "invalid program matrix selector");
1392 YYERROR;
1393 }
1394
1395 $$ = $1;
1396 }
1397 ;
1398
1399
1400
1401 programSingleItem: progEnvParam | progLocalParam;
1402
1403 programMultipleItem: progEnvParams | progLocalParams;
1404
1405 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1406 {
1407 memset($$, 0, sizeof($$));
1408 $$[0] = state->state_param_enum;
1409 $$[1] = STATE_ENV;
1410 $$[2] = $4[0];
1411 $$[3] = $4[1];
1412 }
1413 ;
1414
1415 progEnvParamNums: progEnvParamNum
1416 {
1417 $$[0] = $1;
1418 $$[1] = $1;
1419 }
1420 | progEnvParamNum DOT_DOT progEnvParamNum
1421 {
1422 $$[0] = $1;
1423 $$[1] = $3;
1424 }
1425 ;
1426
1427 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1428 {
1429 memset($$, 0, sizeof($$));
1430 $$[0] = state->state_param_enum;
1431 $$[1] = STATE_ENV;
1432 $$[2] = $4;
1433 $$[3] = $4;
1434 }
1435 ;
1436
1437 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1438 {
1439 memset($$, 0, sizeof($$));
1440 $$[0] = state->state_param_enum;
1441 $$[1] = STATE_LOCAL;
1442 $$[2] = $4[0];
1443 $$[3] = $4[1];
1444 }
1445
1446 progLocalParamNums: progLocalParamNum
1447 {
1448 $$[0] = $1;
1449 $$[1] = $1;
1450 }
1451 | progLocalParamNum DOT_DOT progLocalParamNum
1452 {
1453 $$[0] = $1;
1454 $$[1] = $3;
1455 }
1456 ;
1457
1458 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1459 {
1460 memset($$, 0, sizeof($$));
1461 $$[0] = state->state_param_enum;
1462 $$[1] = STATE_LOCAL;
1463 $$[2] = $4;
1464 $$[3] = $4;
1465 }
1466 ;
1467
1468 progEnvParamNum: INTEGER
1469 {
1470 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1471 yyerror(& @1, state, "invalid environment parameter reference");
1472 YYERROR;
1473 }
1474 $$ = $1;
1475 }
1476 ;
1477
1478 progLocalParamNum: INTEGER
1479 {
1480 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1481 yyerror(& @1, state, "invalid local parameter reference");
1482 YYERROR;
1483 }
1484 $$ = $1;
1485 }
1486 ;
1487
1488
1489
1490 paramConstDecl: paramConstScalarDecl | paramConstVector;
1491 paramConstUse: paramConstScalarUse | paramConstVector;
1492
1493 paramConstScalarDecl: signedFloatConstant
1494 {
1495 $$.count = 1;
1496 $$.data[0] = $1;
1497 }
1498 ;
1499
1500 paramConstScalarUse: REAL
1501 {
1502 $$.count = 1;
1503 $$.data[0] = $1;
1504 }
1505 | INTEGER
1506 {
1507 $$.count = 1;
1508 $$.data[0] = (float) $1;
1509 }
1510 ;
1511
1512 paramConstVector: '{' signedFloatConstant '}'
1513 {
1514 $$.count = 1;
1515 $$.data[0] = $2;
1516 $$.data[1] = 0.0f;
1517 $$.data[2] = 0.0f;
1518 $$.data[3] = 0.0f;
1519 }
1520 | '{' signedFloatConstant ',' signedFloatConstant '}'
1521 {
1522 $$.count = 2;
1523 $$.data[0] = $2;
1524 $$.data[1] = $4;
1525 $$.data[2] = 0.0f;
1526 $$.data[3] = 0.0f;
1527 }
1528 | '{' signedFloatConstant ',' signedFloatConstant ','
1529 signedFloatConstant '}'
1530 {
1531 $$.count = 3;
1532 $$.data[0] = $2;
1533 $$.data[1] = $4;
1534 $$.data[2] = $6;
1535 $$.data[3] = 0.0f;
1536 }
1537 | '{' signedFloatConstant ',' signedFloatConstant ','
1538 signedFloatConstant ',' signedFloatConstant '}'
1539 {
1540 $$.count = 4;
1541 $$.data[0] = $2;
1542 $$.data[1] = $4;
1543 $$.data[2] = $6;
1544 $$.data[3] = $8;
1545 }
1546 ;
1547
1548 signedFloatConstant: optionalSign REAL
1549 {
1550 $$ = ($1) ? -$2 : $2;
1551 }
1552 | optionalSign INTEGER
1553 {
1554 $$ = (float)(($1) ? -$2 : $2);
1555 }
1556 ;
1557
1558 optionalSign: '+' { $$ = FALSE; }
1559 | '-' { $$ = TRUE; }
1560 | { $$ = FALSE; }
1561 ;
1562
1563 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1564 ;
1565
1566 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1567 ;
1568
1569 varNameList: varNameList ',' IDENTIFIER
1570 {
1571 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1572 YYERROR;
1573 }
1574 }
1575 | IDENTIFIER
1576 {
1577 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1578 YYERROR;
1579 }
1580 }
1581 ;
1582
1583 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1584 {
1585 struct asm_symbol *const s =
1586 declare_variable(state, $2, at_output, & @2);
1587
1588 if (s == NULL) {
1589 YYERROR;
1590 } else {
1591 s->output_binding = $4;
1592 }
1593 }
1594 ;
1595
1596 resultBinding: RESULT POSITION
1597 {
1598 if (state->mode == ARB_vertex) {
1599 $$ = VERT_RESULT_HPOS;
1600 } else {
1601 yyerror(& @2, state, "invalid program result name");
1602 YYERROR;
1603 }
1604 }
1605 | RESULT FOGCOORD
1606 {
1607 if (state->mode == ARB_vertex) {
1608 $$ = VERT_RESULT_FOGC;
1609 } else {
1610 yyerror(& @2, state, "invalid program result name");
1611 YYERROR;
1612 }
1613 }
1614 | RESULT resultColBinding
1615 {
1616 $$ = $2;
1617 }
1618 | RESULT POINTSIZE
1619 {
1620 if (state->mode == ARB_vertex) {
1621 $$ = VERT_RESULT_PSIZ;
1622 } else {
1623 yyerror(& @2, state, "invalid program result name");
1624 YYERROR;
1625 }
1626 }
1627 | RESULT TEXCOORD optTexCoordUnitNum
1628 {
1629 if (state->mode == ARB_vertex) {
1630 $$ = VERT_RESULT_TEX0 + $3;
1631 } else {
1632 yyerror(& @2, state, "invalid program result name");
1633 YYERROR;
1634 }
1635 }
1636 | RESULT DEPTH
1637 {
1638 if (state->mode == ARB_fragment) {
1639 $$ = FRAG_RESULT_DEPTH;
1640 } else {
1641 yyerror(& @2, state, "invalid program result name");
1642 YYERROR;
1643 }
1644 }
1645 ;
1646
1647 resultColBinding: COLOR optResultFaceType optResultColorType
1648 {
1649 $$ = $2 + $3;
1650 }
1651 ;
1652
1653 optResultFaceType:
1654 {
1655 $$ = (state->mode == ARB_vertex)
1656 ? VERT_RESULT_COL0
1657 : FRAG_RESULT_COLOR;
1658 }
1659 | FRONT
1660 {
1661 if (state->mode == ARB_vertex) {
1662 $$ = VERT_RESULT_COL0;
1663 } else {
1664 yyerror(& @1, state, "invalid program result name");
1665 YYERROR;
1666 }
1667 }
1668 | BACK
1669 {
1670 if (state->mode == ARB_vertex) {
1671 $$ = VERT_RESULT_BFC0;
1672 } else {
1673 yyerror(& @1, state, "invalid program result name");
1674 YYERROR;
1675 }
1676 }
1677 ;
1678
1679 optResultColorType:
1680 {
1681 $$ = 0;
1682 }
1683 | PRIMARY
1684 {
1685 if (state->mode == ARB_vertex) {
1686 $$ = 0;
1687 } else {
1688 yyerror(& @1, state, "invalid program result name");
1689 YYERROR;
1690 }
1691 }
1692 | SECONDARY
1693 {
1694 if (state->mode == ARB_vertex) {
1695 $$ = 1;
1696 } else {
1697 yyerror(& @1, state, "invalid program result name");
1698 YYERROR;
1699 }
1700 }
1701 ;
1702
1703 optFaceType: { $$ = 0; }
1704 | FRONT { $$ = 0; }
1705 | BACK { $$ = 1; }
1706 ;
1707
1708 optColorType: { $$ = 0; }
1709 | PRIMARY { $$ = 0; }
1710 | SECONDARY { $$ = 1; }
1711 ;
1712
1713 optTexCoordUnitNum: { $$ = 0; }
1714 | '[' texCoordUnitNum ']' { $$ = $2; }
1715 ;
1716
1717 optTexImageUnitNum: { $$ = 0; }
1718 | '[' texImageUnitNum ']' { $$ = $2; }
1719 ;
1720
1721 optLegacyTexUnitNum: { $$ = 0; }
1722 | '[' legacyTexUnitNum ']' { $$ = $2; }
1723 ;
1724
1725 texCoordUnitNum: INTEGER
1726 {
1727 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
1728 yyerror(& @1, state, "invalid texture coordinate unit selector");
1729 YYERROR;
1730 }
1731
1732 $$ = $1;
1733 }
1734 ;
1735
1736 texImageUnitNum: INTEGER
1737 {
1738 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
1739 yyerror(& @1, state, "invalid texture image unit selector");
1740 YYERROR;
1741 }
1742
1743 $$ = $1;
1744 }
1745 ;
1746
1747 legacyTexUnitNum: INTEGER
1748 {
1749 if ((unsigned) $1 >= state->MaxTextureUnits) {
1750 yyerror(& @1, state, "invalid texture unit selector");
1751 YYERROR;
1752 }
1753
1754 $$ = $1;
1755 }
1756 ;
1757
1758 ALIAS_statement: ALIAS IDENTIFIER '=' IDENTIFIER
1759 {
1760 struct asm_symbol *exist = (struct asm_symbol *)
1761 _mesa_symbol_table_find_symbol(state->st, 0, $2);
1762 struct asm_symbol *target = (struct asm_symbol *)
1763 _mesa_symbol_table_find_symbol(state->st, 0, $4);
1764
1765
1766 if (exist != NULL) {
1767 yyerror(& @2, state, "redeclared identifier");
1768 YYERROR;
1769 } else if (target == NULL) {
1770 yyerror(& @4, state,
1771 "undefined variable binding in ALIAS statement");
1772 YYERROR;
1773 } else {
1774 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
1775 }
1776 }
1777 ;
1778
1779 %%
1780
1781 struct asm_instruction *
1782 asm_instruction_ctor(gl_inst_opcode op,
1783 const struct prog_dst_register *dst,
1784 const struct asm_src_register *src0,
1785 const struct asm_src_register *src1,
1786 const struct asm_src_register *src2)
1787 {
1788 struct asm_instruction *inst = calloc(1, sizeof(struct asm_instruction));
1789
1790 if (inst) {
1791 _mesa_init_instructions(& inst->Base, 1);
1792 inst->Base.Opcode = op;
1793 inst->Base.DstReg = *dst;
1794
1795 inst->Base.SrcReg[0] = src0->Base;
1796 inst->SrcReg[0] = *src0;
1797
1798 if (src1 != NULL) {
1799 inst->Base.SrcReg[1] = src1->Base;
1800 inst->SrcReg[1] = *src1;
1801 } else {
1802 init_src_reg(& inst->SrcReg[1]);
1803 }
1804
1805 if (src2 != NULL) {
1806 inst->Base.SrcReg[2] = src2->Base;
1807 inst->SrcReg[2] = *src2;
1808 } else {
1809 init_src_reg(& inst->SrcReg[2]);
1810 }
1811 }
1812
1813 return inst;
1814 }
1815
1816
1817 void
1818 init_dst_reg(struct prog_dst_register *r)
1819 {
1820 memset(r, 0, sizeof(*r));
1821 r->File = PROGRAM_UNDEFINED;
1822 r->WriteMask = WRITEMASK_XYZW;
1823 r->CondMask = COND_TR;
1824 r->CondSwizzle = SWIZZLE_NOOP;
1825 }
1826
1827
1828 void
1829 init_src_reg(struct asm_src_register *r)
1830 {
1831 memset(r, 0, sizeof(*r));
1832 r->Base.File = PROGRAM_UNDEFINED;
1833 r->Base.Swizzle = SWIZZLE_NOOP;
1834 r->Symbol = NULL;
1835 }
1836
1837
1838 /**
1839 * Validate the set of inputs used by a program
1840 *
1841 * Validates that legal sets of inputs are used by the program. In this case
1842 * "used" included both reading the input or binding the input to a name using
1843 * the \c ATTRIB command.
1844 *
1845 * \return
1846 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
1847 */
1848 int
1849 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
1850 {
1851 const int inputs = state->prog->InputsRead | state->InputsBound;
1852
1853 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
1854 yyerror(locp, state, "illegal use of generic attribute and name attribute");
1855 return 0;
1856 }
1857
1858 return 1;
1859 }
1860
1861
1862 struct asm_symbol *
1863 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
1864 struct YYLTYPE *locp)
1865 {
1866 struct asm_symbol *s = NULL;
1867 struct asm_symbol *exist = (struct asm_symbol *)
1868 _mesa_symbol_table_find_symbol(state->st, 0, name);
1869
1870
1871 if (exist != NULL) {
1872 yyerror(locp, state, "redeclared identifier");
1873 } else {
1874 s = calloc(1, sizeof(struct asm_symbol));
1875 s->name = name;
1876 s->type = t;
1877
1878 switch (t) {
1879 case at_temp:
1880 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
1881 yyerror(locp, state, "too many temporaries declared");
1882 free(s);
1883 return NULL;
1884 }
1885
1886 s->temp_binding = state->prog->NumTemporaries;
1887 state->prog->NumTemporaries++;
1888 break;
1889
1890 case at_address:
1891 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
1892 yyerror(locp, state, "too many address registers declared");
1893 free(s);
1894 return NULL;
1895 }
1896
1897 /* FINISHME: Add support for multiple address registers.
1898 */
1899 state->prog->NumAddressRegs++;
1900 break;
1901
1902 default:
1903 break;
1904 }
1905
1906 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
1907 }
1908
1909 return s;
1910 }
1911
1912
1913 int add_state_reference(struct gl_program_parameter_list *param_list,
1914 const gl_state_index tokens[STATE_LENGTH])
1915 {
1916 const GLuint size = 4; /* XXX fix */
1917 char *name;
1918 GLint index;
1919
1920 name = _mesa_program_state_string(tokens);
1921 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
1922 size, GL_NONE,
1923 NULL, (gl_state_index *) tokens, 0x0);
1924 param_list->StateFlags |= _mesa_program_state_flags(tokens);
1925
1926 /* free name string here since we duplicated it in add_parameter() */
1927 _mesa_free(name);
1928
1929 return index;
1930 }
1931
1932
1933 int
1934 initialize_symbol_from_state(struct gl_program *prog,
1935 struct asm_symbol *param_var,
1936 const gl_state_index tokens[STATE_LENGTH])
1937 {
1938 int idx = -1;
1939 gl_state_index state_tokens[STATE_LENGTH];
1940
1941
1942 memcpy(state_tokens, tokens, sizeof(state_tokens));
1943
1944 param_var->type = at_param;
1945 param_var->param_binding_type = PROGRAM_STATE_VAR;
1946
1947 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
1948 * unroll it and call add_state_reference() for each row
1949 */
1950 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1951 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1952 state_tokens[0] == STATE_MVP_MATRIX ||
1953 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1954 state_tokens[0] == STATE_PROGRAM_MATRIX)
1955 && (state_tokens[2] != state_tokens[3])) {
1956 int row;
1957 const int first_row = state_tokens[2];
1958 const int last_row = state_tokens[3];
1959
1960 for (row = first_row; row <= last_row; row++) {
1961 state_tokens[2] = state_tokens[3] = row;
1962
1963 idx = add_state_reference(prog->Parameters, state_tokens);
1964 if (param_var->param_binding_begin == ~0U)
1965 param_var->param_binding_begin = idx;
1966 param_var->param_binding_length++;
1967 }
1968 }
1969 else {
1970 idx = add_state_reference(prog->Parameters, state_tokens);
1971 if (param_var->param_binding_begin == ~0U)
1972 param_var->param_binding_begin = idx;
1973 param_var->param_binding_length++;
1974 }
1975
1976 return idx;
1977 }
1978
1979
1980 int
1981 initialize_symbol_from_param(struct gl_program *prog,
1982 struct asm_symbol *param_var,
1983 const gl_state_index tokens[STATE_LENGTH])
1984 {
1985 int idx = -1;
1986 gl_state_index state_tokens[STATE_LENGTH];
1987
1988
1989 memcpy(state_tokens, tokens, sizeof(state_tokens));
1990
1991 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
1992 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
1993 assert((state_tokens[1] == STATE_ENV)
1994 || (state_tokens[1] == STATE_LOCAL));
1995
1996 param_var->type = at_param;
1997 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
1998 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
1999
2000 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2001 * we need to unroll it and call add_state_reference() for each row
2002 */
2003 if (state_tokens[2] != state_tokens[3]) {
2004 int row;
2005 const int first_row = state_tokens[2];
2006 const int last_row = state_tokens[3];
2007
2008 for (row = first_row; row <= last_row; row++) {
2009 state_tokens[2] = state_tokens[3] = row;
2010
2011 idx = add_state_reference(prog->Parameters, state_tokens);
2012 if (param_var->param_binding_begin == ~0U)
2013 param_var->param_binding_begin = idx;
2014 param_var->param_binding_length++;
2015 }
2016 }
2017 else {
2018 idx = add_state_reference(prog->Parameters, state_tokens);
2019 if (param_var->param_binding_begin == ~0U)
2020 param_var->param_binding_begin = idx;
2021 param_var->param_binding_length++;
2022 }
2023
2024 return idx;
2025 }
2026
2027
2028 int
2029 initialize_symbol_from_const(struct gl_program *prog,
2030 struct asm_symbol *param_var,
2031 const struct asm_vector *vec)
2032 {
2033 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2034 NULL, vec->count, GL_NONE, vec->data,
2035 NULL, 0x0);
2036
2037 param_var->type = at_param;
2038 param_var->param_binding_type = PROGRAM_CONSTANT;
2039
2040 if (param_var->param_binding_begin == ~0U)
2041 param_var->param_binding_begin = idx;
2042 param_var->param_binding_length++;
2043
2044 return idx;
2045 }
2046
2047
2048 char *
2049 make_error_string(const char *fmt, ...)
2050 {
2051 int length;
2052 char *str;
2053 va_list args;
2054
2055 va_start(args, fmt);
2056
2057 /* Call vsnprintf once to determine how large the final string is. Call it
2058 * again to do the actual formatting. from the vsnprintf manual page:
2059 *
2060 * Upon successful return, these functions return the number of
2061 * characters printed (not including the trailing '\0' used to end
2062 * output to strings).
2063 */
2064 length = 1 + vsnprintf(NULL, 0, fmt, args);
2065
2066 str = _mesa_malloc(length);
2067 if (str) {
2068 vsnprintf(str, length, fmt, args);
2069 }
2070
2071 va_end(args);
2072
2073 return str;
2074 }
2075
2076
2077 void
2078 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2079 {
2080 char *err_str;
2081
2082
2083 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2084 if (err_str) {
2085 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2086 _mesa_free(err_str);
2087 }
2088
2089 err_str = make_error_string("line %u, char %u: error: %s\n",
2090 locp->first_line, locp->first_column, s);
2091 _mesa_set_program_error(state->ctx, locp->position, err_str);
2092
2093 if (err_str) {
2094 _mesa_free(err_str);
2095 }
2096 }
2097
2098
2099 GLboolean
2100 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2101 GLsizei len, struct asm_parser_state *state)
2102 {
2103 struct gl_program_constants limits;
2104 struct asm_instruction *inst;
2105 unsigned i;
2106 GLubyte *strz;
2107
2108 state->ctx = ctx;
2109 state->prog->Target = target;
2110 state->prog->Parameters = _mesa_new_parameter_list();
2111
2112 /* Make a copy of the program string and force it to be NUL-terminated.
2113 */
2114 strz = (GLubyte *) _mesa_malloc(len + 1);
2115 if (strz == NULL) {
2116 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2117 return GL_FALSE;
2118 }
2119 _mesa_memcpy (strz, str, len);
2120 strz[len] = '\0';
2121
2122 state->prog->String = strz;
2123
2124 state->st = _mesa_symbol_table_ctor();
2125
2126 /* All of these limits should come from ctx.
2127 */
2128 limits.MaxInstructions = 128;
2129 limits.MaxAluInstructions = 128;
2130 limits.MaxTexInstructions = 128;
2131 limits.MaxTexIndirections = 128;
2132 limits.MaxAttribs = 16;
2133 limits.MaxTemps = 128;
2134 limits.MaxAddressRegs = 1;
2135 limits.MaxParameters = 128;
2136 limits.MaxLocalParams = 256;
2137 limits.MaxEnvParams = 128;
2138 limits.MaxNativeInstructions = 128;
2139 limits.MaxNativeAluInstructions = 128;
2140 limits.MaxNativeTexInstructions = 128;
2141 limits.MaxNativeTexIndirections = 128;
2142 limits.MaxNativeAttribs = 16;
2143 limits.MaxNativeTemps = 128;
2144 limits.MaxNativeAddressRegs = 1;
2145 limits.MaxNativeParameters = 128;
2146 limits.MaxUniformComponents = 0;
2147
2148 state->limits = & limits;
2149
2150 state->MaxTextureImageUnits = 16;
2151 state->MaxTextureCoordUnits = 8;
2152 state->MaxTextureUnits = 8;
2153 state->MaxClipPlanes = 6;
2154 state->MaxLights = 8;
2155 state->MaxProgramMatrices = 8;
2156
2157 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2158 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2159
2160 _mesa_set_program_error(ctx, -1, NULL);
2161
2162 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2163 yyparse(state);
2164 _mesa_program_lexer_dtor(state->scanner);
2165
2166
2167 if (ctx->Program.ErrorPos != -1) {
2168 return GL_FALSE;
2169 }
2170
2171 if (! _mesa_layout_parameters(state)) {
2172 struct YYLTYPE loc;
2173
2174 loc.first_line = 0;
2175 loc.first_column = 0;
2176 loc.position = len;
2177
2178 yyerror(& loc, state, "invalid PARAM usage");
2179 return GL_FALSE;
2180 }
2181
2182
2183
2184 /* Add one instruction to store the "END" instruction.
2185 */
2186 state->prog->Instructions =
2187 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2188 inst = state->inst_head;
2189 for (i = 0; i < state->prog->NumInstructions; i++) {
2190 struct asm_instruction *const temp = inst->next;
2191
2192 state->prog->Instructions[i] = inst->Base;
2193 _mesa_free(inst);
2194
2195 inst = temp;
2196 }
2197
2198 /* Finally, tag on an OPCODE_END instruction */
2199 {
2200 const GLuint numInst = state->prog->NumInstructions;
2201 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2202 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2203 }
2204 state->prog->NumInstructions++;
2205
2206 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2207
2208 /*
2209 * Initialize native counts to logical counts. The device driver may
2210 * change them if program is translated into a hardware program.
2211 */
2212 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2213 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2214 state->prog->NumNativeParameters = state->prog->NumParameters;
2215 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2216 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2217
2218 return GL_TRUE;
2219 }