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