3 * Copyright © 2009 Intel Corporation
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:
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
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.
28 #include "main/mtypes.h"
29 #include "main/imports.h"
30 #include "program/program.h"
31 #include "program/prog_parameter.h"
32 #include "program/prog_parameter_layout.h"
33 #include "program/prog_statevars.h"
34 #include "program/prog_instruction.h"
36 #include "program/symbol_table.h"
37 #include "program/program_parser.h"
39 extern void *yy_scan_string(char *);
40 extern void yy_delete_buffer(void *);
42 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
43 char *name, enum asm_type t, struct YYLTYPE *locp);
45 static int add_state_reference(struct gl_program_parameter_list *param_list,
46 const gl_state_index tokens[STATE_LENGTH]);
48 static int initialize_symbol_from_state(struct gl_program *prog,
49 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
51 static int initialize_symbol_from_param(struct gl_program *prog,
52 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
54 static int initialize_symbol_from_const(struct gl_program *prog,
55 struct asm_symbol *param_var, const struct asm_vector *vec,
56 GLboolean allowSwizzle);
58 static int yyparse(struct asm_parser_state *state);
60 static char *make_error_string(const char *fmt, ...);
62 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
65 static int validate_inputs(struct YYLTYPE *locp,
66 struct asm_parser_state *state);
68 static void init_dst_reg(struct prog_dst_register *r);
70 static void set_dst_reg(struct prog_dst_register *r,
71 gl_register_file file, GLint index);
73 static void init_src_reg(struct asm_src_register *r);
75 static void set_src_reg(struct asm_src_register *r,
76 gl_register_file file, GLint index);
78 static void set_src_reg_swz(struct asm_src_register *r,
79 gl_register_file file, GLint index, GLuint swizzle);
81 static void asm_instruction_set_operands(struct asm_instruction *inst,
82 const struct prog_dst_register *dst, const struct asm_src_register *src0,
83 const struct asm_src_register *src1, const struct asm_src_register *src2);
85 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
86 const struct prog_dst_register *dst, const struct asm_src_register *src0,
87 const struct asm_src_register *src1, const struct asm_src_register *src2);
89 static struct asm_instruction *asm_instruction_copy_ctor(
90 const struct prog_instruction *base, const struct prog_dst_register *dst,
91 const struct asm_src_register *src0, const struct asm_src_register *src1,
92 const struct asm_src_register *src2);
99 #define YYLLOC_DEFAULT(Current, Rhs, N) \
102 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
103 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
104 (Current).position = YYRHSLOC(Rhs, 1).position; \
105 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
106 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
108 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
109 (Current).last_line = (Current).first_line; \
110 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
111 (Current).last_column = (Current).first_column; \
112 (Current).position = YYRHSLOC(Rhs, 0).position \
113 + (Current).first_column; \
120 %lex-param { struct asm_parser_state *state }
121 %parse-param { struct asm_parser_state *state }
125 struct asm_instruction *inst;
126 struct asm_symbol *sym;
127 struct asm_symbol temp_sym;
128 struct asm_swizzle_mask swiz_mask;
129 struct asm_src_register src_reg;
130 struct prog_dst_register dst_reg;
131 struct prog_instruction temp_inst;
137 gl_state_index state[STATE_LENGTH];
139 struct asm_vector vector;
140 gl_inst_opcode opcode;
144 unsigned rgba_valid:1;
145 unsigned xyzw_valid:1;
150 %token ARBvp_10 ARBfp_10
152 /* Tokens for assembler pseudo-ops */
153 %token <integer> ADDRESS
157 %token <integer> TEMP
160 /* Tokens for instructions */
161 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
162 %token <temp_inst> ARL KIL SWZ TXD_OP
164 %token <integer> INTEGER
167 %token AMBIENT ATTENUATION
170 %token DEPTH DIFFUSE DIRECTION
171 %token EMISSION ENV EYE
172 %token FOG FOGCOORD FRAGMENT FRONT
174 %token INVERSE INVTRANS
175 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
176 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
179 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
180 %token RANGE RESULT ROW
181 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
182 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
183 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
184 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
185 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
186 %token VERTEX VTXATTRIB
189 %token <string> IDENTIFIER USED_IDENTIFIER
190 %type <string> string
191 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
195 %type <inst> instruction ALU_instruction TexInstruction
196 %type <inst> ARL_instruction VECTORop_instruction
197 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
198 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
199 %type <inst> KIL_instruction
201 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
202 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
203 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
204 %type <ext_swizzle> extSwizComp extSwizSel
205 %type <swiz_mask> optionalMask
207 %type <sym> progParamArray
208 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
209 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
211 %type <swiz_mask> addrComponent addrWriteMask
213 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
215 %type <result> resultBinding resultColBinding
216 %type <integer> optFaceType optColorType
217 %type <integer> optResultFaceType optResultColorType
219 %type <integer> optTexImageUnitNum texImageUnitNum
220 %type <integer> optTexCoordUnitNum texCoordUnitNum
221 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
222 %type <integer> texImageUnit texTarget
223 %type <integer> vtxAttribNum
225 %type <attrib> attribBinding vtxAttribItem fragAttribItem
227 %type <temp_sym> paramSingleInit paramSingleItemDecl
228 %type <integer> optArraySize
230 %type <state> stateSingleItem stateMultipleItem
231 %type <state> stateMaterialItem
232 %type <state> stateLightItem stateLightModelItem stateLightProdItem
233 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
234 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
235 %type <state> stateTexEnvItem stateDepthItem
237 %type <state> stateLModProperty
238 %type <state> stateMatrixName optMatrixRows
240 %type <integer> stateMatProperty
241 %type <integer> stateLightProperty stateSpotProperty
242 %type <integer> stateLightNumber stateLProdProperty
243 %type <integer> stateTexGenType stateTexGenCoord
244 %type <integer> stateTexEnvProperty
245 %type <integer> stateFogProperty
246 %type <integer> stateClipPlaneNum
247 %type <integer> statePointProperty
249 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
250 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
251 %type <integer> stateProgramMatNum
253 %type <integer> ambDiffSpecProperty
255 %type <state> programSingleItem progEnvParam progLocalParam
256 %type <state> programMultipleItem progEnvParams progLocalParams
258 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
259 %type <temp_sym> paramSingleItemUse
261 %type <integer> progEnvParamNum progLocalParamNum
262 %type <state> progEnvParamNums progLocalParamNums
264 %type <vector> paramConstDecl paramConstUse
265 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
266 %type <real> signedFloatConstant
267 %type <negate> optionalSign
271 _mesa_program_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
275 yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
276 struct asm_parser_state *state)
278 return _mesa_program_lexer_lex(yylval_param, yylloc_param, state->scanner);
284 program: language optionSequence statementSequence END
289 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
290 yyerror(& @1, state, "invalid fragment program header");
293 state->mode = ARB_vertex;
297 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
298 yyerror(& @1, state, "invalid vertex program header");
300 state->mode = ARB_fragment;
302 state->option.TexRect =
303 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
307 optionSequence: optionSequence option
311 option: OPTION string ';'
315 if (state->mode == ARB_vertex) {
316 valid = _mesa_ARBvp_parse_option(state, $2);
317 } else if (state->mode == ARB_fragment) {
318 valid = _mesa_ARBfp_parse_option(state, $2);
325 const char *const err_str = (state->mode == ARB_vertex)
326 ? "invalid ARB vertex program option"
327 : "invalid ARB fragment program option";
329 yyerror(& @2, state, err_str);
335 statementSequence: statementSequence statement
339 statement: instruction ';'
342 if (state->inst_tail == NULL) {
343 state->inst_head = $1;
345 state->inst_tail->next = $1;
348 state->inst_tail = $1;
351 state->prog->NumInstructions++;
354 | namingStatement ';'
357 instruction: ALU_instruction
360 state->prog->NumAluInstructions++;
365 state->prog->NumTexInstructions++;
369 ALU_instruction: ARL_instruction
370 | VECTORop_instruction
371 | SCALARop_instruction
372 | BINSCop_instruction
378 TexInstruction: SAMPLE_instruction
383 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
385 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
389 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
391 if ($1.Opcode == OPCODE_DDY)
392 state->fragment.UsesDFdy = 1;
393 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
397 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
399 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
403 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
405 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
410 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
412 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
416 TRIop_instruction: TRI_OP maskedDstReg ','
417 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
419 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
423 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
425 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
427 const GLbitfield tex_mask = (1U << $6);
428 GLbitfield shadow_tex = 0;
429 GLbitfield target_mask = 0;
432 $$->Base.TexSrcUnit = $6;
435 shadow_tex = tex_mask;
437 $$->Base.TexSrcTarget = -$8;
438 $$->Base.TexShadow = 1;
440 $$->Base.TexSrcTarget = $8;
443 target_mask = (1U << $$->Base.TexSrcTarget);
445 /* If this texture unit was previously accessed and that access
446 * had a different texture target, generate an error.
448 * If this texture unit was previously accessed and that access
449 * had a different shadow mode, generate an error.
451 if ((state->prog->TexturesUsed[$6] != 0)
452 && ((state->prog->TexturesUsed[$6] != target_mask)
453 || ((state->prog->ShadowSamplers & tex_mask)
456 "multiple targets used on one texture image unit");
461 state->prog->TexturesUsed[$6] |= target_mask;
462 state->prog->ShadowSamplers |= shadow_tex;
467 KIL_instruction: KIL swizzleSrcReg
469 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
470 state->fragment.UsesKill = 1;
474 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
475 $$->Base.DstReg.CondMask = $2.CondMask;
476 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
477 state->fragment.UsesKill = 1;
481 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
483 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
485 const GLbitfield tex_mask = (1U << $10);
486 GLbitfield shadow_tex = 0;
487 GLbitfield target_mask = 0;
490 $$->Base.TexSrcUnit = $10;
493 shadow_tex = tex_mask;
495 $$->Base.TexSrcTarget = -$12;
496 $$->Base.TexShadow = 1;
498 $$->Base.TexSrcTarget = $12;
501 target_mask = (1U << $$->Base.TexSrcTarget);
503 /* If this texture unit was previously accessed and that access
504 * had a different texture target, generate an error.
506 * If this texture unit was previously accessed and that access
507 * had a different shadow mode, generate an error.
509 if ((state->prog->TexturesUsed[$10] != 0)
510 && ((state->prog->TexturesUsed[$10] != target_mask)
511 || ((state->prog->ShadowSamplers & tex_mask)
513 yyerror(& @12, state,
514 "multiple targets used on one texture image unit");
519 state->prog->TexturesUsed[$10] |= target_mask;
520 state->prog->ShadowSamplers |= shadow_tex;
525 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
531 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
532 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
533 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
534 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
535 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
536 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
537 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
538 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
539 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
540 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
541 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
542 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
545 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
547 /* FIXME: Is this correct? Should the extenedSwizzle be applied
548 * FIXME: to the existing swizzle?
550 $4.Base.Swizzle = $6.swizzle;
551 $4.Base.Negate = $6.mask;
553 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
557 scalarSrcReg: optionalSign scalarUse
562 $$.Base.Negate = ~$$.Base.Negate;
565 | optionalSign '|' scalarUse '|'
569 if (!state->option.NV_fragment) {
570 yyerror(& @2, state, "unexpected character '|'");
575 $$.Base.Negate = ~$$.Base.Negate;
582 scalarUse: srcReg scalarSuffix
586 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
589 | paramConstScalarUse
591 struct asm_symbol temp_sym;
593 if (!state->option.NV_fragment) {
594 yyerror(& @1, state, "expected scalar suffix");
598 memset(& temp_sym, 0, sizeof(temp_sym));
599 temp_sym.param_binding_begin = ~0;
600 initialize_symbol_from_const(state->prog, & temp_sym, & $1, GL_TRUE);
602 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
603 temp_sym.param_binding_begin,
604 temp_sym.param_binding_swizzle);
608 swizzleSrcReg: optionalSign srcReg swizzleSuffix
613 $$.Base.Negate = ~$$.Base.Negate;
616 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
619 | optionalSign '|' srcReg swizzleSuffix '|'
623 if (!state->option.NV_fragment) {
624 yyerror(& @2, state, "unexpected character '|'");
629 $$.Base.Negate = ~$$.Base.Negate;
633 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
639 maskedDstReg: dstReg optionalMask optionalCcMask
642 $$.WriteMask = $2.mask;
643 $$.CondMask = $3.CondMask;
644 $$.CondSwizzle = $3.CondSwizzle;
646 if ($$.File == PROGRAM_OUTPUT) {
647 /* Technically speaking, this should check that it is in
648 * vertex program mode. However, PositionInvariant can never be
649 * set in fragment program mode, so it is somewhat irrelevant.
651 if (state->option.PositionInvariant
652 && ($$.Index == VARYING_SLOT_POS)) {
653 yyerror(& @1, state, "position-invariant programs cannot "
658 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
663 maskedAddrReg: addrReg addrWriteMask
665 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
666 $$.WriteMask = $2.mask;
670 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
672 const unsigned xyzw_valid =
674 | ($3.xyzw_valid << 1)
675 | ($5.xyzw_valid << 2)
676 | ($7.xyzw_valid << 3);
677 const unsigned rgba_valid =
679 | ($3.rgba_valid << 1)
680 | ($5.rgba_valid << 2)
681 | ($7.rgba_valid << 3);
683 /* All of the swizzle components have to be valid in either RGBA
684 * or XYZW. Note that 0 and 1 are valid in both, so both masks
685 * can have some bits set.
687 * We somewhat deviate from the spec here. It would be really hard
688 * to figure out which component is the error, and there probably
689 * isn't a lot of benefit.
691 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
692 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
697 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
698 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
703 extSwizComp: optionalSign extSwizSel
706 $$.negate = ($1) ? 1 : 0;
712 if (($1 != 0) && ($1 != 1)) {
713 yyerror(& @1, state, "invalid extended swizzle selector");
717 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
720 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
730 if (strlen($1) > 1) {
731 yyerror(& @1, state, "invalid extended swizzle selector");
778 yyerror(& @1, state, "invalid extended swizzle selector");
785 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
787 struct asm_symbol *const s = (struct asm_symbol *)
788 _mesa_symbol_table_find_symbol(state->st, 0, $1);
793 yyerror(& @1, state, "invalid operand variable");
795 } else if ((s->type != at_param) && (s->type != at_temp)
796 && (s->type != at_attrib)) {
797 yyerror(& @1, state, "invalid operand variable");
799 } else if ((s->type == at_param) && s->param_is_array) {
800 yyerror(& @1, state, "non-array access to array PARAM");
807 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
810 set_src_reg_swz(& $$, s->param_binding_type,
811 s->param_binding_begin,
812 s->param_binding_swizzle);
815 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
816 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
818 if (!validate_inputs(& @1, state)) {
830 set_src_reg(& $$, PROGRAM_INPUT, $1);
831 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
833 if (!validate_inputs(& @1, state)) {
837 | progParamArray '[' progParamArrayMem ']'
839 if (! $3.Base.RelAddr
840 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
841 yyerror(& @3, state, "out of bounds array access");
846 $$.Base.File = $1->param_binding_type;
848 if ($3.Base.RelAddr) {
849 state->prog->IndirectRegisterFiles |= (1 << $$.Base.File);
850 $1->param_accessed_indirectly = 1;
853 $$.Base.Index = $3.Base.Index;
856 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
861 gl_register_file file = ($1.name != NULL)
862 ? $1.param_binding_type
864 set_src_reg_swz(& $$, file, $1.param_binding_begin,
865 $1.param_binding_swizzle);
869 dstReg: resultBinding
871 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
873 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
875 struct asm_symbol *const s = (struct asm_symbol *)
876 _mesa_symbol_table_find_symbol(state->st, 0, $1);
881 yyerror(& @1, state, "invalid operand variable");
883 } else if ((s->type != at_output) && (s->type != at_temp)) {
884 yyerror(& @1, state, "invalid operand variable");
890 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
893 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
896 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
902 progParamArray: USED_IDENTIFIER
904 struct asm_symbol *const s = (struct asm_symbol *)
905 _mesa_symbol_table_find_symbol(state->st, 0, $1);
910 yyerror(& @1, state, "invalid operand variable");
912 } else if ((s->type != at_param) || !s->param_is_array) {
913 yyerror(& @1, state, "array access to non-PARAM variable");
921 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
923 progParamArrayAbs: INTEGER
930 progParamArrayRel: addrReg addrComponent addrRegRelOffset
932 /* FINISHME: Add support for multiple address registers.
934 /* FINISHME: Add support for 4-component address registers.
942 addrRegRelOffset: { $$ = 0; }
943 | '+' addrRegPosOffset { $$ = $2; }
944 | '-' addrRegNegOffset { $$ = -$2; }
947 addrRegPosOffset: INTEGER
949 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
951 _mesa_snprintf(s, sizeof(s),
952 "relative address offset too large (%d)", $1);
953 yyerror(& @1, state, s);
961 addrRegNegOffset: INTEGER
963 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
965 _mesa_snprintf(s, sizeof(s),
966 "relative address offset too large (%d)", $1);
967 yyerror(& @1, state, s);
975 addrReg: USED_IDENTIFIER
977 struct asm_symbol *const s = (struct asm_symbol *)
978 _mesa_symbol_table_find_symbol(state->st, 0, $1);
983 yyerror(& @1, state, "invalid array member");
985 } else if (s->type != at_address) {
987 "invalid variable for indexed array access");
997 if ($1.mask != WRITEMASK_X) {
998 yyerror(& @1, state, "invalid address component selector");
1006 addrWriteMask: MASK1
1008 if ($1.mask != WRITEMASK_X) {
1009 yyerror(& @1, state,
1010 "address register write mask must be \".x\"");
1018 scalarSuffix: MASK1;
1020 swizzleSuffix: MASK1
1023 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1026 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1027 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1030 optionalCcMask: '(' ccTest ')'
1040 $$.CondMask = COND_TR;
1041 $$.CondSwizzle = SWIZZLE_NOOP;
1045 ccTest: ccMaskRule swizzleSuffix
1048 $$.CondSwizzle = $2.swizzle;
1052 ccTest2: ccMaskRule2 swizzleSuffix
1055 $$.CondSwizzle = $2.swizzle;
1059 ccMaskRule: IDENTIFIER
1061 const int cond = _mesa_parse_cc($1);
1062 if ((cond == 0) || ($1[2] != '\0')) {
1063 char *const err_str =
1064 make_error_string("invalid condition code \"%s\"", $1);
1066 yyerror(& @1, state, (err_str != NULL)
1067 ? err_str : "invalid condition code");
1069 if (err_str != NULL) {
1077 $$.CondSwizzle = SWIZZLE_NOOP;
1081 ccMaskRule2: USED_IDENTIFIER
1083 const int cond = _mesa_parse_cc($1);
1084 if ((cond == 0) || ($1[2] != '\0')) {
1085 char *const err_str =
1086 make_error_string("invalid condition code \"%s\"", $1);
1088 yyerror(& @1, state, (err_str != NULL)
1089 ? err_str : "invalid condition code");
1091 if (err_str != NULL) {
1099 $$.CondSwizzle = SWIZZLE_NOOP;
1103 namingStatement: ATTRIB_statement
1111 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1113 struct asm_symbol *const s =
1114 declare_variable(state, $2, at_attrib, & @2);
1120 s->attrib_binding = $4;
1121 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
1123 if (!validate_inputs(& @4, state)) {
1130 attribBinding: VERTEX vtxAttribItem
1134 | FRAGMENT fragAttribItem
1140 vtxAttribItem: POSITION
1142 $$ = VERT_ATTRIB_POS;
1144 | WEIGHT vtxOptWeightNum
1146 $$ = VERT_ATTRIB_WEIGHT;
1150 $$ = VERT_ATTRIB_NORMAL;
1152 | COLOR optColorType
1154 $$ = VERT_ATTRIB_COLOR0 + $2;
1158 $$ = VERT_ATTRIB_FOG;
1160 | TEXCOORD optTexCoordUnitNum
1162 $$ = VERT_ATTRIB_TEX0 + $2;
1164 | MATRIXINDEX '[' vtxWeightNum ']'
1166 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1169 | VTXATTRIB '[' vtxAttribNum ']'
1171 $$ = VERT_ATTRIB_GENERIC0 + $3;
1175 vtxAttribNum: INTEGER
1177 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1178 yyerror(& @1, state, "invalid vertex attribute reference");
1186 vtxOptWeightNum: | '[' vtxWeightNum ']';
1187 vtxWeightNum: INTEGER;
1189 fragAttribItem: POSITION
1191 $$ = VARYING_SLOT_POS;
1193 | COLOR optColorType
1195 $$ = VARYING_SLOT_COL0 + $2;
1199 $$ = VARYING_SLOT_FOGC;
1201 | TEXCOORD optTexCoordUnitNum
1203 $$ = VARYING_SLOT_TEX0 + $2;
1207 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1209 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1211 struct asm_symbol *const s =
1212 declare_variable(state, $2, at_param, & @2);
1218 s->param_binding_type = $3.param_binding_type;
1219 s->param_binding_begin = $3.param_binding_begin;
1220 s->param_binding_length = $3.param_binding_length;
1221 s->param_binding_swizzle = $3.param_binding_swizzle;
1222 s->param_is_array = 0;
1227 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1229 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1231 yyerror(& @4, state,
1232 "parameter array size and number of bindings must match");
1235 struct asm_symbol *const s =
1236 declare_variable(state, $2, $6.type, & @2);
1242 s->param_binding_type = $6.param_binding_type;
1243 s->param_binding_begin = $6.param_binding_begin;
1244 s->param_binding_length = $6.param_binding_length;
1245 s->param_binding_swizzle = SWIZZLE_XYZW;
1246 s->param_is_array = 1;
1258 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1260 _mesa_snprintf(msg, sizeof(msg),
1261 "invalid parameter array size (size=%d max=%u)",
1262 $1, state->limits->MaxParameters);
1263 yyerror(& @1, state, msg);
1271 paramSingleInit: '=' paramSingleItemDecl
1277 paramMultipleInit: '=' '{' paramMultInitList '}'
1283 paramMultInitList: paramMultipleItem
1284 | paramMultInitList ',' paramMultipleItem
1286 $1.param_binding_length += $3.param_binding_length;
1291 paramSingleItemDecl: stateSingleItem
1293 memset(& $$, 0, sizeof($$));
1294 $$.param_binding_begin = ~0;
1295 initialize_symbol_from_state(state->prog, & $$, $1);
1299 memset(& $$, 0, sizeof($$));
1300 $$.param_binding_begin = ~0;
1301 initialize_symbol_from_param(state->prog, & $$, $1);
1305 memset(& $$, 0, sizeof($$));
1306 $$.param_binding_begin = ~0;
1307 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1311 paramSingleItemUse: stateSingleItem
1313 memset(& $$, 0, sizeof($$));
1314 $$.param_binding_begin = ~0;
1315 initialize_symbol_from_state(state->prog, & $$, $1);
1319 memset(& $$, 0, sizeof($$));
1320 $$.param_binding_begin = ~0;
1321 initialize_symbol_from_param(state->prog, & $$, $1);
1325 memset(& $$, 0, sizeof($$));
1326 $$.param_binding_begin = ~0;
1327 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1331 paramMultipleItem: stateMultipleItem
1333 memset(& $$, 0, sizeof($$));
1334 $$.param_binding_begin = ~0;
1335 initialize_symbol_from_state(state->prog, & $$, $1);
1337 | programMultipleItem
1339 memset(& $$, 0, sizeof($$));
1340 $$.param_binding_begin = ~0;
1341 initialize_symbol_from_param(state->prog, & $$, $1);
1345 memset(& $$, 0, sizeof($$));
1346 $$.param_binding_begin = ~0;
1347 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1351 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1352 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1355 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1356 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1357 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1358 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1359 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1360 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1361 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1362 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1363 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1364 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1365 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1368 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1370 memset($$, 0, sizeof($$));
1371 $$[0] = STATE_MATERIAL;
1377 stateMatProperty: ambDiffSpecProperty
1383 $$ = STATE_EMISSION;
1387 $$ = STATE_SHININESS;
1391 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1393 memset($$, 0, sizeof($$));
1394 $$[0] = STATE_LIGHT;
1400 stateLightProperty: ambDiffSpecProperty
1406 $$ = STATE_POSITION;
1410 if (!state->ctx->Extensions.EXT_point_parameters) {
1411 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1415 $$ = STATE_ATTENUATION;
1417 | SPOT stateSpotProperty
1423 $$ = STATE_HALF_VECTOR;
1427 stateSpotProperty: DIRECTION
1429 $$ = STATE_SPOT_DIRECTION;
1433 stateLightModelItem: LIGHTMODEL stateLModProperty
1440 stateLModProperty: AMBIENT
1442 memset($$, 0, sizeof($$));
1443 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1445 | optFaceType SCENECOLOR
1447 memset($$, 0, sizeof($$));
1448 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1453 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1455 memset($$, 0, sizeof($$));
1456 $$[0] = STATE_LIGHTPROD;
1463 stateLProdProperty: ambDiffSpecProperty;
1465 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1467 memset($$, 0, sizeof($$));
1473 stateTexEnvProperty: COLOR
1475 $$ = STATE_TEXENV_COLOR;
1479 ambDiffSpecProperty: AMBIENT
1489 $$ = STATE_SPECULAR;
1493 stateLightNumber: INTEGER
1495 if ((unsigned) $1 >= state->MaxLights) {
1496 yyerror(& @1, state, "invalid light selector");
1504 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1506 memset($$, 0, sizeof($$));
1507 $$[0] = STATE_TEXGEN;
1513 stateTexGenType: EYE
1515 $$ = STATE_TEXGEN_EYE_S;
1519 $$ = STATE_TEXGEN_OBJECT_S;
1522 stateTexGenCoord: TEXGEN_S
1524 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1528 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1532 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1536 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1540 stateFogItem: FOG stateFogProperty
1542 memset($$, 0, sizeof($$));
1547 stateFogProperty: COLOR
1549 $$ = STATE_FOG_COLOR;
1553 $$ = STATE_FOG_PARAMS;
1557 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1559 memset($$, 0, sizeof($$));
1560 $$[0] = STATE_CLIPPLANE;
1565 stateClipPlaneNum: INTEGER
1567 if ((unsigned) $1 >= state->MaxClipPlanes) {
1568 yyerror(& @1, state, "invalid clip plane selector");
1576 statePointItem: POINT_TOK statePointProperty
1578 memset($$, 0, sizeof($$));
1583 statePointProperty: SIZE_TOK
1585 $$ = STATE_POINT_SIZE;
1589 $$ = STATE_POINT_ATTENUATION;
1593 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1603 stateMatrixRows: stateMatrixItem optMatrixRows
1618 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1620 /* It seems logical that the matrix row range specifier would have
1621 * to specify a range or more than one row (i.e., $5 > $3).
1622 * However, the ARB_vertex_program spec says "a program will fail
1623 * to load if <a> is greater than <b>." This means that $3 == $5
1627 yyerror(& @3, state, "invalid matrix row range");
1636 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1644 stateOptMatModifier:
1654 stateMatModifier: INVERSE
1656 $$ = STATE_MATRIX_INVERSE;
1660 $$ = STATE_MATRIX_TRANSPOSE;
1664 $$ = STATE_MATRIX_INVTRANS;
1668 stateMatrixRowNum: INTEGER
1671 yyerror(& @1, state, "invalid matrix row reference");
1679 stateMatrixName: MODELVIEW stateOptModMatNum
1681 $$[0] = STATE_MODELVIEW_MATRIX;
1686 $$[0] = STATE_PROJECTION_MATRIX;
1691 $$[0] = STATE_MVP_MATRIX;
1694 | TEXTURE optTexCoordUnitNum
1696 $$[0] = STATE_TEXTURE_MATRIX;
1699 | PALETTE '[' statePaletteMatNum ']'
1701 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1704 | MAT_PROGRAM '[' stateProgramMatNum ']'
1706 $$[0] = STATE_PROGRAM_MATRIX;
1715 | '[' stateModMatNum ']'
1720 stateModMatNum: INTEGER
1722 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1726 yyerror(& @1, state, "invalid modelview matrix index");
1733 statePaletteMatNum: INTEGER
1735 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1736 * through here. The error will be generated later.
1741 stateProgramMatNum: INTEGER
1743 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1744 yyerror(& @1, state, "invalid program matrix selector");
1752 stateDepthItem: DEPTH RANGE
1754 memset($$, 0, sizeof($$));
1755 $$[0] = STATE_DEPTH_RANGE;
1760 programSingleItem: progEnvParam | progLocalParam;
1762 programMultipleItem: progEnvParams | progLocalParams;
1764 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1766 memset($$, 0, sizeof($$));
1767 $$[0] = state->state_param_enum;
1774 progEnvParamNums: progEnvParamNum
1779 | progEnvParamNum DOT_DOT progEnvParamNum
1786 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1788 memset($$, 0, sizeof($$));
1789 $$[0] = state->state_param_enum;
1796 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1798 memset($$, 0, sizeof($$));
1799 $$[0] = state->state_param_enum;
1800 $$[1] = STATE_LOCAL;
1805 progLocalParamNums: progLocalParamNum
1810 | progLocalParamNum DOT_DOT progLocalParamNum
1817 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1819 memset($$, 0, sizeof($$));
1820 $$[0] = state->state_param_enum;
1821 $$[1] = STATE_LOCAL;
1827 progEnvParamNum: INTEGER
1829 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1830 yyerror(& @1, state, "invalid environment parameter reference");
1837 progLocalParamNum: INTEGER
1839 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1840 yyerror(& @1, state, "invalid local parameter reference");
1849 paramConstDecl: paramConstScalarDecl | paramConstVector;
1850 paramConstUse: paramConstScalarUse | paramConstVector;
1852 paramConstScalarDecl: signedFloatConstant
1862 paramConstScalarUse: REAL
1873 $$.data[0].f = (float) $1;
1874 $$.data[1].f = (float) $1;
1875 $$.data[2].f = (float) $1;
1876 $$.data[3].f = (float) $1;
1880 paramConstVector: '{' signedFloatConstant '}'
1884 $$.data[1].f = 0.0f;
1885 $$.data[2].f = 0.0f;
1886 $$.data[3].f = 1.0f;
1888 | '{' signedFloatConstant ',' signedFloatConstant '}'
1893 $$.data[2].f = 0.0f;
1894 $$.data[3].f = 1.0f;
1896 | '{' signedFloatConstant ',' signedFloatConstant ','
1897 signedFloatConstant '}'
1903 $$.data[3].f = 1.0f;
1905 | '{' signedFloatConstant ',' signedFloatConstant ','
1906 signedFloatConstant ',' signedFloatConstant '}'
1916 signedFloatConstant: optionalSign REAL
1918 $$ = ($1) ? -$2 : $2;
1920 | optionalSign INTEGER
1922 $$ = (float)(($1) ? -$2 : $2);
1926 optionalSign: '+' { $$ = FALSE; }
1927 | '-' { $$ = TRUE; }
1931 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1936 /* NV_fragment_program_option defines the size qualifiers in a
1937 * fairly broken way. "SHORT" or "LONG" can optionally be used
1938 * before TEMP or OUTPUT. However, neither is a reserved word!
1939 * This means that we have to parse it as an identifier, then check
1940 * to make sure it's one of the valid values. *sigh*
1942 * In addition, the grammar in the extension spec does *not* allow
1943 * the size specifier to be optional, but all known implementations
1946 if (!state->option.NV_fragment) {
1947 yyerror(& @1, state, "unexpected IDENTIFIER");
1951 if (strcmp("SHORT", $1) == 0) {
1952 } else if (strcmp("LONG", $1) == 0) {
1954 char *const err_str =
1955 make_error_string("invalid storage size specifier \"%s\"",
1958 yyerror(& @1, state, (err_str != NULL)
1959 ? err_str : "invalid storage size specifier");
1961 if (err_str != NULL) {
1973 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1976 varNameList: varNameList ',' IDENTIFIER
1978 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1985 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1992 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1994 struct asm_symbol *const s =
1995 declare_variable(state, $3, at_output, & @3);
2001 s->output_binding = $5;
2006 resultBinding: RESULT POSITION
2008 if (state->mode == ARB_vertex) {
2009 $$ = VARYING_SLOT_POS;
2011 yyerror(& @2, state, "invalid program result name");
2017 if (state->mode == ARB_vertex) {
2018 $$ = VARYING_SLOT_FOGC;
2020 yyerror(& @2, state, "invalid program result name");
2024 | RESULT resultColBinding
2030 if (state->mode == ARB_vertex) {
2031 $$ = VARYING_SLOT_PSIZ;
2033 yyerror(& @2, state, "invalid program result name");
2037 | RESULT TEXCOORD optTexCoordUnitNum
2039 if (state->mode == ARB_vertex) {
2040 $$ = VARYING_SLOT_TEX0 + $3;
2042 yyerror(& @2, state, "invalid program result name");
2048 if (state->mode == ARB_fragment) {
2049 $$ = FRAG_RESULT_DEPTH;
2051 yyerror(& @2, state, "invalid program result name");
2057 resultColBinding: COLOR optResultFaceType optResultColorType
2065 if (state->mode == ARB_vertex) {
2066 $$ = VARYING_SLOT_COL0;
2068 if (state->option.DrawBuffers)
2069 $$ = FRAG_RESULT_DATA0;
2071 $$ = FRAG_RESULT_COLOR;
2076 if (state->mode == ARB_vertex) {
2077 yyerror(& @1, state, "invalid program result name");
2080 if (!state->option.DrawBuffers) {
2081 /* From the ARB_draw_buffers spec (same text exists
2082 * for ATI_draw_buffers):
2084 * If this option is not specified, a fragment
2085 * program that attempts to bind
2086 * "result.color[n]" will fail to load, and only
2087 * "result.color" will be allowed.
2089 yyerror(& @1, state,
2090 "result.color[] used without "
2091 "`OPTION ARB_draw_buffers' or "
2092 "`OPTION ATI_draw_buffers'");
2094 } else if ($2 >= state->MaxDrawBuffers) {
2095 yyerror(& @1, state,
2096 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
2099 $$ = FRAG_RESULT_DATA0 + $2;
2104 if (state->mode == ARB_vertex) {
2105 $$ = VARYING_SLOT_COL0;
2107 yyerror(& @1, state, "invalid program result name");
2113 if (state->mode == ARB_vertex) {
2114 $$ = VARYING_SLOT_BFC0;
2116 yyerror(& @1, state, "invalid program result name");
2128 if (state->mode == ARB_vertex) {
2131 yyerror(& @1, state, "invalid program result name");
2137 if (state->mode == ARB_vertex) {
2140 yyerror(& @1, state, "invalid program result name");
2146 optFaceType: { $$ = 0; }
2151 optColorType: { $$ = 0; }
2152 | PRIMARY { $$ = 0; }
2153 | SECONDARY { $$ = 1; }
2156 optTexCoordUnitNum: { $$ = 0; }
2157 | '[' texCoordUnitNum ']' { $$ = $2; }
2160 optTexImageUnitNum: { $$ = 0; }
2161 | '[' texImageUnitNum ']' { $$ = $2; }
2164 optLegacyTexUnitNum: { $$ = 0; }
2165 | '[' legacyTexUnitNum ']' { $$ = $2; }
2168 texCoordUnitNum: INTEGER
2170 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2171 yyerror(& @1, state, "invalid texture coordinate unit selector");
2179 texImageUnitNum: INTEGER
2181 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2182 yyerror(& @1, state, "invalid texture image unit selector");
2190 legacyTexUnitNum: INTEGER
2192 if ((unsigned) $1 >= state->MaxTextureUnits) {
2193 yyerror(& @1, state, "invalid texture unit selector");
2201 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2203 struct asm_symbol *exist = (struct asm_symbol *)
2204 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2205 struct asm_symbol *target = (struct asm_symbol *)
2206 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2210 if (exist != NULL) {
2212 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2214 yyerror(& @2, state, m);
2216 } else if (target == NULL) {
2218 yyerror(& @4, state,
2219 "undefined variable binding in ALIAS statement");
2222 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2234 asm_instruction_set_operands(struct asm_instruction *inst,
2235 const struct prog_dst_register *dst,
2236 const struct asm_src_register *src0,
2237 const struct asm_src_register *src1,
2238 const struct asm_src_register *src2)
2240 /* In the core ARB extensions only the KIL instruction doesn't have a
2241 * destination register.
2244 init_dst_reg(& inst->Base.DstReg);
2246 inst->Base.DstReg = *dst;
2249 /* The only instruction that doesn't have any source registers is the
2250 * condition-code based KIL instruction added by NV_fragment_program_option.
2253 inst->Base.SrcReg[0] = src0->Base;
2254 inst->SrcReg[0] = *src0;
2256 init_src_reg(& inst->SrcReg[0]);
2260 inst->Base.SrcReg[1] = src1->Base;
2261 inst->SrcReg[1] = *src1;
2263 init_src_reg(& inst->SrcReg[1]);
2267 inst->Base.SrcReg[2] = src2->Base;
2268 inst->SrcReg[2] = *src2;
2270 init_src_reg(& inst->SrcReg[2]);
2275 struct asm_instruction *
2276 asm_instruction_ctor(gl_inst_opcode op,
2277 const struct prog_dst_register *dst,
2278 const struct asm_src_register *src0,
2279 const struct asm_src_register *src1,
2280 const struct asm_src_register *src2)
2282 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2285 _mesa_init_instructions(& inst->Base, 1);
2286 inst->Base.Opcode = op;
2288 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2295 struct asm_instruction *
2296 asm_instruction_copy_ctor(const struct prog_instruction *base,
2297 const struct prog_dst_register *dst,
2298 const struct asm_src_register *src0,
2299 const struct asm_src_register *src1,
2300 const struct asm_src_register *src2)
2302 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2305 _mesa_init_instructions(& inst->Base, 1);
2306 inst->Base.Opcode = base->Opcode;
2307 inst->Base.CondUpdate = base->CondUpdate;
2308 inst->Base.CondDst = base->CondDst;
2309 inst->Base.SaturateMode = base->SaturateMode;
2310 inst->Base.Precision = base->Precision;
2312 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2320 init_dst_reg(struct prog_dst_register *r)
2322 memset(r, 0, sizeof(*r));
2323 r->File = PROGRAM_UNDEFINED;
2324 r->WriteMask = WRITEMASK_XYZW;
2325 r->CondMask = COND_TR;
2326 r->CondSwizzle = SWIZZLE_NOOP;
2330 /** Like init_dst_reg() but set the File and Index fields. */
2332 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2334 const GLint maxIndex = 1 << INST_INDEX_BITS;
2335 const GLint minIndex = 0;
2336 assert(index >= minIndex);
2338 assert(index <= maxIndex);
2340 assert(file == PROGRAM_TEMPORARY ||
2341 file == PROGRAM_ADDRESS ||
2342 file == PROGRAM_OUTPUT);
2343 memset(r, 0, sizeof(*r));
2346 r->WriteMask = WRITEMASK_XYZW;
2347 r->CondMask = COND_TR;
2348 r->CondSwizzle = SWIZZLE_NOOP;
2353 init_src_reg(struct asm_src_register *r)
2355 memset(r, 0, sizeof(*r));
2356 r->Base.File = PROGRAM_UNDEFINED;
2357 r->Base.Swizzle = SWIZZLE_NOOP;
2362 /** Like init_src_reg() but set the File and Index fields.
2363 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2366 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2368 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2373 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2376 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2377 const GLint minIndex = -(1 << INST_INDEX_BITS);
2378 assert(file < PROGRAM_FILE_MAX);
2379 assert(index >= minIndex);
2381 assert(index <= maxIndex);
2383 memset(r, 0, sizeof(*r));
2384 r->Base.File = file;
2385 r->Base.Index = index;
2386 r->Base.Swizzle = swizzle;
2392 * Validate the set of inputs used by a program
2394 * Validates that legal sets of inputs are used by the program. In this case
2395 * "used" included both reading the input or binding the input to a name using
2396 * the \c ATTRIB command.
2399 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2402 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2404 const GLbitfield64 inputs = state->prog->InputsRead | state->InputsBound;
2406 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2407 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2416 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2417 struct YYLTYPE *locp)
2419 struct asm_symbol *s = NULL;
2420 struct asm_symbol *exist = (struct asm_symbol *)
2421 _mesa_symbol_table_find_symbol(state->st, 0, name);
2424 if (exist != NULL) {
2425 yyerror(locp, state, "redeclared identifier");
2427 s = calloc(1, sizeof(struct asm_symbol));
2433 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2434 yyerror(locp, state, "too many temporaries declared");
2439 s->temp_binding = state->prog->NumTemporaries;
2440 state->prog->NumTemporaries++;
2444 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2445 yyerror(locp, state, "too many address registers declared");
2450 /* FINISHME: Add support for multiple address registers.
2452 state->prog->NumAddressRegs++;
2459 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2460 s->next = state->sym;
2468 int add_state_reference(struct gl_program_parameter_list *param_list,
2469 const gl_state_index tokens[STATE_LENGTH])
2471 const GLuint size = 4; /* XXX fix */
2475 name = _mesa_program_state_string(tokens);
2476 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2477 size, GL_NONE, NULL, tokens);
2478 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2480 /* free name string here since we duplicated it in add_parameter() */
2488 initialize_symbol_from_state(struct gl_program *prog,
2489 struct asm_symbol *param_var,
2490 const gl_state_index tokens[STATE_LENGTH])
2493 gl_state_index state_tokens[STATE_LENGTH];
2496 memcpy(state_tokens, tokens, sizeof(state_tokens));
2498 param_var->type = at_param;
2499 param_var->param_binding_type = PROGRAM_STATE_VAR;
2501 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2502 * unroll it and call add_state_reference() for each row
2504 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2505 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2506 state_tokens[0] == STATE_MVP_MATRIX ||
2507 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2508 state_tokens[0] == STATE_PROGRAM_MATRIX)
2509 && (state_tokens[2] != state_tokens[3])) {
2511 const int first_row = state_tokens[2];
2512 const int last_row = state_tokens[3];
2514 for (row = first_row; row <= last_row; row++) {
2515 state_tokens[2] = state_tokens[3] = row;
2517 idx = add_state_reference(prog->Parameters, state_tokens);
2518 if (param_var->param_binding_begin == ~0U) {
2519 param_var->param_binding_begin = idx;
2520 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2523 param_var->param_binding_length++;
2527 idx = add_state_reference(prog->Parameters, state_tokens);
2528 if (param_var->param_binding_begin == ~0U) {
2529 param_var->param_binding_begin = idx;
2530 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2532 param_var->param_binding_length++;
2540 initialize_symbol_from_param(struct gl_program *prog,
2541 struct asm_symbol *param_var,
2542 const gl_state_index tokens[STATE_LENGTH])
2545 gl_state_index state_tokens[STATE_LENGTH];
2548 memcpy(state_tokens, tokens, sizeof(state_tokens));
2550 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2551 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2552 assert((state_tokens[1] == STATE_ENV)
2553 || (state_tokens[1] == STATE_LOCAL));
2556 * The param type is STATE_VAR. The program parameter entry will
2557 * effectively be a pointer into the LOCAL or ENV parameter array.
2559 param_var->type = at_param;
2560 param_var->param_binding_type = PROGRAM_STATE_VAR;
2562 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2563 * we need to unroll it and call add_state_reference() for each row
2565 if (state_tokens[2] != state_tokens[3]) {
2567 const int first_row = state_tokens[2];
2568 const int last_row = state_tokens[3];
2570 for (row = first_row; row <= last_row; row++) {
2571 state_tokens[2] = state_tokens[3] = row;
2573 idx = add_state_reference(prog->Parameters, state_tokens);
2574 if (param_var->param_binding_begin == ~0U) {
2575 param_var->param_binding_begin = idx;
2576 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2578 param_var->param_binding_length++;
2582 idx = add_state_reference(prog->Parameters, state_tokens);
2583 if (param_var->param_binding_begin == ~0U) {
2584 param_var->param_binding_begin = idx;
2585 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2587 param_var->param_binding_length++;
2595 * Put a float/vector constant/literal into the parameter list.
2596 * \param param_var returns info about the parameter/constant's location,
2597 * binding, type, etc.
2598 * \param vec the vector/constant to add
2599 * \param allowSwizzle if true, try to consolidate constants which only differ
2600 * by a swizzle. We don't want to do this when building
2601 * arrays of constants that may be indexed indirectly.
2602 * \return index of the constant in the parameter list.
2605 initialize_symbol_from_const(struct gl_program *prog,
2606 struct asm_symbol *param_var,
2607 const struct asm_vector *vec,
2608 GLboolean allowSwizzle)
2611 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2612 vec->data, vec->count,
2613 allowSwizzle ? &swizzle : NULL);
2615 param_var->type = at_param;
2616 param_var->param_binding_type = PROGRAM_CONSTANT;
2618 if (param_var->param_binding_begin == ~0U) {
2619 param_var->param_binding_begin = idx;
2620 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2622 param_var->param_binding_length++;
2629 make_error_string(const char *fmt, ...)
2636 /* Call vsnprintf once to determine how large the final string is. Call it
2637 * again to do the actual formatting. from the vsnprintf manual page:
2639 * Upon successful return, these functions return the number of
2640 * characters printed (not including the trailing '\0' used to end
2641 * output to strings).
2643 va_start(args, fmt);
2644 length = 1 + vsnprintf(NULL, 0, fmt, args);
2647 str = malloc(length);
2649 va_start(args, fmt);
2650 vsnprintf(str, length, fmt, args);
2659 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2664 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2666 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2670 err_str = make_error_string("line %u, char %u: error: %s\n",
2671 locp->first_line, locp->first_column, s);
2672 _mesa_set_program_error(state->ctx, locp->position, err_str);
2681 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2682 GLsizei len, struct asm_parser_state *state)
2684 struct asm_instruction *inst;
2687 GLboolean result = GL_FALSE;
2689 struct asm_symbol *sym;
2692 state->prog->Target = target;
2693 state->prog->Parameters = _mesa_new_parameter_list();
2695 /* Make a copy of the program string and force it to be NUL-terminated.
2697 strz = (GLubyte *) malloc(len + 1);
2699 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2702 memcpy (strz, str, len);
2705 state->prog->String = strz;
2707 state->st = _mesa_symbol_table_ctor();
2709 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2710 ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2711 : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2713 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2714 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2715 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2716 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2717 state->MaxLights = ctx->Const.MaxLights;
2718 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2719 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2721 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2722 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2724 _mesa_set_program_error(ctx, -1, NULL);
2726 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2728 _mesa_program_lexer_dtor(state->scanner);
2731 if (ctx->Program.ErrorPos != -1) {
2735 if (! _mesa_layout_parameters(state)) {
2739 loc.first_column = 0;
2742 yyerror(& loc, state, "invalid PARAM usage");
2748 /* Add one instruction to store the "END" instruction.
2750 state->prog->Instructions =
2751 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2753 if (state->prog->Instructions == NULL) {
2757 inst = state->inst_head;
2758 for (i = 0; i < state->prog->NumInstructions; i++) {
2759 struct asm_instruction *const temp = inst->next;
2761 state->prog->Instructions[i] = inst->Base;
2765 /* Finally, tag on an OPCODE_END instruction */
2767 const GLuint numInst = state->prog->NumInstructions;
2768 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2769 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2771 state->prog->NumInstructions++;
2773 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2774 state->prog->NumAttributes = _mesa_bitcount_64(state->prog->InputsRead);
2777 * Initialize native counts to logical counts. The device driver may
2778 * change them if program is translated into a hardware program.
2780 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2781 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2782 state->prog->NumNativeParameters = state->prog->NumParameters;
2783 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2784 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2789 for (inst = state->inst_head; inst != NULL; inst = temp) {
2794 state->inst_head = NULL;
2795 state->inst_tail = NULL;
2797 for (sym = state->sym; sym != NULL; sym = temp) {
2800 free((void *) sym->name);
2805 _mesa_symbol_table_dtor(state->st);