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; \
117 #define YYLEX_PARAM state->scanner
122 %parse-param { struct asm_parser_state *state }
124 %lex-param { void *scanner }
127 struct asm_instruction *inst;
128 struct asm_symbol *sym;
129 struct asm_symbol temp_sym;
130 struct asm_swizzle_mask swiz_mask;
131 struct asm_src_register src_reg;
132 struct prog_dst_register dst_reg;
133 struct prog_instruction temp_inst;
139 gl_state_index state[STATE_LENGTH];
141 struct asm_vector vector;
142 gl_inst_opcode opcode;
146 unsigned rgba_valid:1;
147 unsigned xyzw_valid:1;
152 %token ARBvp_10 ARBfp_10
154 /* Tokens for assembler pseudo-ops */
155 %token <integer> ADDRESS
159 %token <integer> TEMP
162 /* Tokens for instructions */
163 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
164 %token <temp_inst> ARL KIL SWZ TXD_OP
166 %token <integer> INTEGER
169 %token AMBIENT ATTENUATION
172 %token DEPTH DIFFUSE DIRECTION
173 %token EMISSION ENV EYE
174 %token FOG FOGCOORD FRAGMENT FRONT
176 %token INVERSE INVTRANS
177 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
178 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
181 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
182 %token RANGE RESULT ROW
183 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
184 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
185 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
186 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
187 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
188 %token VERTEX VTXATTRIB
191 %token <string> IDENTIFIER USED_IDENTIFIER
192 %type <string> string
193 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
197 %type <inst> instruction ALU_instruction TexInstruction
198 %type <inst> ARL_instruction VECTORop_instruction
199 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
200 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
201 %type <inst> KIL_instruction
203 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
204 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
205 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
206 %type <ext_swizzle> extSwizComp extSwizSel
207 %type <swiz_mask> optionalMask
209 %type <sym> progParamArray
210 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
211 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
213 %type <swiz_mask> addrComponent addrWriteMask
215 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
217 %type <result> resultBinding resultColBinding
218 %type <integer> optFaceType optColorType
219 %type <integer> optResultFaceType optResultColorType
221 %type <integer> optTexImageUnitNum texImageUnitNum
222 %type <integer> optTexCoordUnitNum texCoordUnitNum
223 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
224 %type <integer> texImageUnit texTarget
225 %type <integer> vtxAttribNum
227 %type <attrib> attribBinding vtxAttribItem fragAttribItem
229 %type <temp_sym> paramSingleInit paramSingleItemDecl
230 %type <integer> optArraySize
232 %type <state> stateSingleItem stateMultipleItem
233 %type <state> stateMaterialItem
234 %type <state> stateLightItem stateLightModelItem stateLightProdItem
235 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
236 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
237 %type <state> stateTexEnvItem stateDepthItem
239 %type <state> stateLModProperty
240 %type <state> stateMatrixName optMatrixRows
242 %type <integer> stateMatProperty
243 %type <integer> stateLightProperty stateSpotProperty
244 %type <integer> stateLightNumber stateLProdProperty
245 %type <integer> stateTexGenType stateTexGenCoord
246 %type <integer> stateTexEnvProperty
247 %type <integer> stateFogProperty
248 %type <integer> stateClipPlaneNum
249 %type <integer> statePointProperty
251 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
252 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
253 %type <integer> stateProgramMatNum
255 %type <integer> ambDiffSpecProperty
257 %type <state> programSingleItem progEnvParam progLocalParam
258 %type <state> programMultipleItem progEnvParams progLocalParams
260 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
261 %type <temp_sym> paramSingleItemUse
263 %type <integer> progEnvParamNum progLocalParamNum
264 %type <state> progEnvParamNums progLocalParamNums
266 %type <vector> paramConstDecl paramConstUse
267 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
268 %type <real> signedFloatConstant
269 %type <negate> optionalSign
272 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
278 program: language optionSequence statementSequence END
283 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
284 yyerror(& @1, state, "invalid fragment program header");
287 state->mode = ARB_vertex;
291 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
292 yyerror(& @1, state, "invalid vertex program header");
294 state->mode = ARB_fragment;
296 state->option.TexRect =
297 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
301 optionSequence: optionSequence option
305 option: OPTION string ';'
309 if (state->mode == ARB_vertex) {
310 valid = _mesa_ARBvp_parse_option(state, $2);
311 } else if (state->mode == ARB_fragment) {
312 valid = _mesa_ARBfp_parse_option(state, $2);
319 const char *const err_str = (state->mode == ARB_vertex)
320 ? "invalid ARB vertex program option"
321 : "invalid ARB fragment program option";
323 yyerror(& @2, state, err_str);
329 statementSequence: statementSequence statement
333 statement: instruction ';'
336 if (state->inst_tail == NULL) {
337 state->inst_head = $1;
339 state->inst_tail->next = $1;
342 state->inst_tail = $1;
345 state->prog->NumInstructions++;
348 | namingStatement ';'
351 instruction: ALU_instruction
354 state->prog->NumAluInstructions++;
359 state->prog->NumTexInstructions++;
363 ALU_instruction: ARL_instruction
364 | VECTORop_instruction
365 | SCALARop_instruction
366 | BINSCop_instruction
372 TexInstruction: SAMPLE_instruction
377 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
379 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
383 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
385 if ($1.Opcode == OPCODE_DDY)
386 state->fragment.UsesDFdy = 1;
387 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
391 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
393 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
397 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
399 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
404 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
406 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
410 TRIop_instruction: TRI_OP maskedDstReg ','
411 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
413 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
417 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
419 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
421 const GLbitfield tex_mask = (1U << $6);
422 GLbitfield shadow_tex = 0;
423 GLbitfield target_mask = 0;
426 $$->Base.TexSrcUnit = $6;
429 shadow_tex = tex_mask;
431 $$->Base.TexSrcTarget = -$8;
432 $$->Base.TexShadow = 1;
434 $$->Base.TexSrcTarget = $8;
437 target_mask = (1U << $$->Base.TexSrcTarget);
439 /* If this texture unit was previously accessed and that access
440 * had a different texture target, generate an error.
442 * If this texture unit was previously accessed and that access
443 * had a different shadow mode, generate an error.
445 if ((state->prog->TexturesUsed[$6] != 0)
446 && ((state->prog->TexturesUsed[$6] != target_mask)
447 || ((state->prog->ShadowSamplers & tex_mask)
450 "multiple targets used on one texture image unit");
455 state->prog->TexturesUsed[$6] |= target_mask;
456 state->prog->ShadowSamplers |= shadow_tex;
461 KIL_instruction: KIL swizzleSrcReg
463 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
464 state->fragment.UsesKill = 1;
468 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
469 $$->Base.DstReg.CondMask = $2.CondMask;
470 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
471 state->fragment.UsesKill = 1;
475 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
477 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
479 const GLbitfield tex_mask = (1U << $10);
480 GLbitfield shadow_tex = 0;
481 GLbitfield target_mask = 0;
484 $$->Base.TexSrcUnit = $10;
487 shadow_tex = tex_mask;
489 $$->Base.TexSrcTarget = -$12;
490 $$->Base.TexShadow = 1;
492 $$->Base.TexSrcTarget = $12;
495 target_mask = (1U << $$->Base.TexSrcTarget);
497 /* If this texture unit was previously accessed and that access
498 * had a different texture target, generate an error.
500 * If this texture unit was previously accessed and that access
501 * had a different shadow mode, generate an error.
503 if ((state->prog->TexturesUsed[$10] != 0)
504 && ((state->prog->TexturesUsed[$10] != target_mask)
505 || ((state->prog->ShadowSamplers & tex_mask)
507 yyerror(& @12, state,
508 "multiple targets used on one texture image unit");
513 state->prog->TexturesUsed[$10] |= target_mask;
514 state->prog->ShadowSamplers |= shadow_tex;
519 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
525 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
526 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
527 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
528 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
529 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
530 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
531 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
532 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
533 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
534 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
535 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
536 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
539 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
541 /* FIXME: Is this correct? Should the extenedSwizzle be applied
542 * FIXME: to the existing swizzle?
544 $4.Base.Swizzle = $6.swizzle;
545 $4.Base.Negate = $6.mask;
547 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
551 scalarSrcReg: optionalSign scalarUse
556 $$.Base.Negate = ~$$.Base.Negate;
559 | optionalSign '|' scalarUse '|'
563 if (!state->option.NV_fragment) {
564 yyerror(& @2, state, "unexpected character '|'");
569 $$.Base.Negate = ~$$.Base.Negate;
576 scalarUse: srcReg scalarSuffix
580 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
583 | paramConstScalarUse
585 struct asm_symbol temp_sym;
587 if (!state->option.NV_fragment) {
588 yyerror(& @1, state, "expected scalar suffix");
592 memset(& temp_sym, 0, sizeof(temp_sym));
593 temp_sym.param_binding_begin = ~0;
594 initialize_symbol_from_const(state->prog, & temp_sym, & $1, GL_TRUE);
596 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
597 temp_sym.param_binding_begin,
598 temp_sym.param_binding_swizzle);
602 swizzleSrcReg: optionalSign srcReg swizzleSuffix
607 $$.Base.Negate = ~$$.Base.Negate;
610 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
613 | optionalSign '|' srcReg swizzleSuffix '|'
617 if (!state->option.NV_fragment) {
618 yyerror(& @2, state, "unexpected character '|'");
623 $$.Base.Negate = ~$$.Base.Negate;
627 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
633 maskedDstReg: dstReg optionalMask optionalCcMask
636 $$.WriteMask = $2.mask;
637 $$.CondMask = $3.CondMask;
638 $$.CondSwizzle = $3.CondSwizzle;
640 if ($$.File == PROGRAM_OUTPUT) {
641 /* Technically speaking, this should check that it is in
642 * vertex program mode. However, PositionInvariant can never be
643 * set in fragment program mode, so it is somewhat irrelevant.
645 if (state->option.PositionInvariant
646 && ($$.Index == VARYING_SLOT_POS)) {
647 yyerror(& @1, state, "position-invariant programs cannot "
652 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
657 maskedAddrReg: addrReg addrWriteMask
659 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
660 $$.WriteMask = $2.mask;
664 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
666 const unsigned xyzw_valid =
668 | ($3.xyzw_valid << 1)
669 | ($5.xyzw_valid << 2)
670 | ($7.xyzw_valid << 3);
671 const unsigned rgba_valid =
673 | ($3.rgba_valid << 1)
674 | ($5.rgba_valid << 2)
675 | ($7.rgba_valid << 3);
677 /* All of the swizzle components have to be valid in either RGBA
678 * or XYZW. Note that 0 and 1 are valid in both, so both masks
679 * can have some bits set.
681 * We somewhat deviate from the spec here. It would be really hard
682 * to figure out which component is the error, and there probably
683 * isn't a lot of benefit.
685 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
686 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
691 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
692 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
697 extSwizComp: optionalSign extSwizSel
700 $$.negate = ($1) ? 1 : 0;
706 if (($1 != 0) && ($1 != 1)) {
707 yyerror(& @1, state, "invalid extended swizzle selector");
711 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
713 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
723 if (strlen($1) > 1) {
724 yyerror(& @1, state, "invalid extended swizzle selector");
767 yyerror(& @1, state, "invalid extended swizzle selector");
774 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
776 struct asm_symbol *const s = (struct asm_symbol *)
777 _mesa_symbol_table_find_symbol(state->st, 0, $1);
782 yyerror(& @1, state, "invalid operand variable");
784 } else if ((s->type != at_param) && (s->type != at_temp)
785 && (s->type != at_attrib)) {
786 yyerror(& @1, state, "invalid operand variable");
788 } else if ((s->type == at_param) && s->param_is_array) {
789 yyerror(& @1, state, "non-array access to array PARAM");
796 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
799 set_src_reg_swz(& $$, s->param_binding_type,
800 s->param_binding_begin,
801 s->param_binding_swizzle);
804 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
805 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
807 if (!validate_inputs(& @1, state)) {
819 set_src_reg(& $$, PROGRAM_INPUT, $1);
820 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
822 if (!validate_inputs(& @1, state)) {
826 | progParamArray '[' progParamArrayMem ']'
828 if (! $3.Base.RelAddr
829 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
830 yyerror(& @3, state, "out of bounds array access");
835 $$.Base.File = $1->param_binding_type;
837 if ($3.Base.RelAddr) {
838 state->prog->IndirectRegisterFiles |= (1 << $$.Base.File);
839 $1->param_accessed_indirectly = 1;
842 $$.Base.Index = $3.Base.Index;
845 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
850 gl_register_file file = ($1.name != NULL)
851 ? $1.param_binding_type
853 set_src_reg_swz(& $$, file, $1.param_binding_begin,
854 $1.param_binding_swizzle);
858 dstReg: resultBinding
860 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
862 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
864 struct asm_symbol *const s = (struct asm_symbol *)
865 _mesa_symbol_table_find_symbol(state->st, 0, $1);
870 yyerror(& @1, state, "invalid operand variable");
872 } else if ((s->type != at_output) && (s->type != at_temp)) {
873 yyerror(& @1, state, "invalid operand variable");
879 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
882 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
885 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
891 progParamArray: USED_IDENTIFIER
893 struct asm_symbol *const s = (struct asm_symbol *)
894 _mesa_symbol_table_find_symbol(state->st, 0, $1);
899 yyerror(& @1, state, "invalid operand variable");
901 } else if ((s->type != at_param) || !s->param_is_array) {
902 yyerror(& @1, state, "array access to non-PARAM variable");
910 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
912 progParamArrayAbs: INTEGER
919 progParamArrayRel: addrReg addrComponent addrRegRelOffset
921 /* FINISHME: Add support for multiple address registers.
923 /* FINISHME: Add support for 4-component address registers.
931 addrRegRelOffset: { $$ = 0; }
932 | '+' addrRegPosOffset { $$ = $2; }
933 | '-' addrRegNegOffset { $$ = -$2; }
936 addrRegPosOffset: INTEGER
938 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
940 _mesa_snprintf(s, sizeof(s),
941 "relative address offset too large (%d)", $1);
942 yyerror(& @1, state, s);
950 addrRegNegOffset: INTEGER
952 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
954 _mesa_snprintf(s, sizeof(s),
955 "relative address offset too large (%d)", $1);
956 yyerror(& @1, state, s);
964 addrReg: USED_IDENTIFIER
966 struct asm_symbol *const s = (struct asm_symbol *)
967 _mesa_symbol_table_find_symbol(state->st, 0, $1);
972 yyerror(& @1, state, "invalid array member");
974 } else if (s->type != at_address) {
976 "invalid variable for indexed array access");
986 if ($1.mask != WRITEMASK_X) {
987 yyerror(& @1, state, "invalid address component selector");
997 if ($1.mask != WRITEMASK_X) {
999 "address register write mask must be \".x\"");
1007 scalarSuffix: MASK1;
1009 swizzleSuffix: MASK1
1012 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1015 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1016 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1019 optionalCcMask: '(' ccTest ')'
1029 $$.CondMask = COND_TR;
1030 $$.CondSwizzle = SWIZZLE_NOOP;
1034 ccTest: ccMaskRule swizzleSuffix
1037 $$.CondSwizzle = $2.swizzle;
1041 ccTest2: ccMaskRule2 swizzleSuffix
1044 $$.CondSwizzle = $2.swizzle;
1048 ccMaskRule: IDENTIFIER
1050 const int cond = _mesa_parse_cc($1);
1051 if ((cond == 0) || ($1[2] != '\0')) {
1052 char *const err_str =
1053 make_error_string("invalid condition code \"%s\"", $1);
1055 yyerror(& @1, state, (err_str != NULL)
1056 ? err_str : "invalid condition code");
1058 if (err_str != NULL) {
1066 $$.CondSwizzle = SWIZZLE_NOOP;
1070 ccMaskRule2: USED_IDENTIFIER
1072 const int cond = _mesa_parse_cc($1);
1073 if ((cond == 0) || ($1[2] != '\0')) {
1074 char *const err_str =
1075 make_error_string("invalid condition code \"%s\"", $1);
1077 yyerror(& @1, state, (err_str != NULL)
1078 ? err_str : "invalid condition code");
1080 if (err_str != NULL) {
1088 $$.CondSwizzle = SWIZZLE_NOOP;
1092 namingStatement: ATTRIB_statement
1100 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1102 struct asm_symbol *const s =
1103 declare_variable(state, $2, at_attrib, & @2);
1109 s->attrib_binding = $4;
1110 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
1112 if (!validate_inputs(& @4, state)) {
1119 attribBinding: VERTEX vtxAttribItem
1123 | FRAGMENT fragAttribItem
1129 vtxAttribItem: POSITION
1131 $$ = VERT_ATTRIB_POS;
1133 | WEIGHT vtxOptWeightNum
1135 $$ = VERT_ATTRIB_WEIGHT;
1139 $$ = VERT_ATTRIB_NORMAL;
1141 | COLOR optColorType
1143 if (!state->ctx->Extensions.EXT_secondary_color) {
1144 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1148 $$ = VERT_ATTRIB_COLOR0 + $2;
1152 if (!state->ctx->Extensions.EXT_fog_coord) {
1153 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1157 $$ = VERT_ATTRIB_FOG;
1159 | TEXCOORD optTexCoordUnitNum
1161 $$ = VERT_ATTRIB_TEX0 + $2;
1163 | MATRIXINDEX '[' vtxWeightNum ']'
1165 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1168 | VTXATTRIB '[' vtxAttribNum ']'
1170 $$ = VERT_ATTRIB_GENERIC0 + $3;
1174 vtxAttribNum: INTEGER
1176 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1177 yyerror(& @1, state, "invalid vertex attribute reference");
1185 vtxOptWeightNum: | '[' vtxWeightNum ']';
1186 vtxWeightNum: INTEGER;
1188 fragAttribItem: POSITION
1190 $$ = FRAG_ATTRIB_WPOS;
1192 | COLOR optColorType
1194 $$ = FRAG_ATTRIB_COL0 + $2;
1198 $$ = FRAG_ATTRIB_FOGC;
1200 | TEXCOORD optTexCoordUnitNum
1202 $$ = FRAG_ATTRIB_TEX0 + $2;
1206 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1208 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1210 struct asm_symbol *const s =
1211 declare_variable(state, $2, at_param, & @2);
1217 s->param_binding_type = $3.param_binding_type;
1218 s->param_binding_begin = $3.param_binding_begin;
1219 s->param_binding_length = $3.param_binding_length;
1220 s->param_binding_swizzle = $3.param_binding_swizzle;
1221 s->param_is_array = 0;
1226 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1228 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1230 yyerror(& @4, state,
1231 "parameter array size and number of bindings must match");
1234 struct asm_symbol *const s =
1235 declare_variable(state, $2, $6.type, & @2);
1241 s->param_binding_type = $6.param_binding_type;
1242 s->param_binding_begin = $6.param_binding_begin;
1243 s->param_binding_length = $6.param_binding_length;
1244 s->param_binding_swizzle = SWIZZLE_XYZW;
1245 s->param_is_array = 1;
1257 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1259 _mesa_snprintf(msg, sizeof(msg),
1260 "invalid parameter array size (size=%d max=%u)",
1261 $1, state->limits->MaxParameters);
1262 yyerror(& @1, state, msg);
1270 paramSingleInit: '=' paramSingleItemDecl
1276 paramMultipleInit: '=' '{' paramMultInitList '}'
1282 paramMultInitList: paramMultipleItem
1283 | paramMultInitList ',' paramMultipleItem
1285 $1.param_binding_length += $3.param_binding_length;
1290 paramSingleItemDecl: stateSingleItem
1292 memset(& $$, 0, sizeof($$));
1293 $$.param_binding_begin = ~0;
1294 initialize_symbol_from_state(state->prog, & $$, $1);
1298 memset(& $$, 0, sizeof($$));
1299 $$.param_binding_begin = ~0;
1300 initialize_symbol_from_param(state->prog, & $$, $1);
1304 memset(& $$, 0, sizeof($$));
1305 $$.param_binding_begin = ~0;
1306 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1310 paramSingleItemUse: stateSingleItem
1312 memset(& $$, 0, sizeof($$));
1313 $$.param_binding_begin = ~0;
1314 initialize_symbol_from_state(state->prog, & $$, $1);
1318 memset(& $$, 0, sizeof($$));
1319 $$.param_binding_begin = ~0;
1320 initialize_symbol_from_param(state->prog, & $$, $1);
1324 memset(& $$, 0, sizeof($$));
1325 $$.param_binding_begin = ~0;
1326 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1330 paramMultipleItem: stateMultipleItem
1332 memset(& $$, 0, sizeof($$));
1333 $$.param_binding_begin = ~0;
1334 initialize_symbol_from_state(state->prog, & $$, $1);
1336 | programMultipleItem
1338 memset(& $$, 0, sizeof($$));
1339 $$.param_binding_begin = ~0;
1340 initialize_symbol_from_param(state->prog, & $$, $1);
1344 memset(& $$, 0, sizeof($$));
1345 $$.param_binding_begin = ~0;
1346 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1350 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1351 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1354 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1355 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1356 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1357 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1358 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1359 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1360 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1361 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1362 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1363 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1364 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1367 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1369 memset($$, 0, sizeof($$));
1370 $$[0] = STATE_MATERIAL;
1376 stateMatProperty: ambDiffSpecProperty
1382 $$ = STATE_EMISSION;
1386 $$ = STATE_SHININESS;
1390 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1392 memset($$, 0, sizeof($$));
1393 $$[0] = STATE_LIGHT;
1399 stateLightProperty: ambDiffSpecProperty
1405 $$ = STATE_POSITION;
1409 if (!state->ctx->Extensions.EXT_point_parameters) {
1410 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1414 $$ = STATE_ATTENUATION;
1416 | SPOT stateSpotProperty
1422 $$ = STATE_HALF_VECTOR;
1426 stateSpotProperty: DIRECTION
1428 $$ = STATE_SPOT_DIRECTION;
1432 stateLightModelItem: LIGHTMODEL stateLModProperty
1439 stateLModProperty: AMBIENT
1441 memset($$, 0, sizeof($$));
1442 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1444 | optFaceType SCENECOLOR
1446 memset($$, 0, sizeof($$));
1447 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1452 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1454 memset($$, 0, sizeof($$));
1455 $$[0] = STATE_LIGHTPROD;
1462 stateLProdProperty: ambDiffSpecProperty;
1464 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1466 memset($$, 0, sizeof($$));
1472 stateTexEnvProperty: COLOR
1474 $$ = STATE_TEXENV_COLOR;
1478 ambDiffSpecProperty: AMBIENT
1488 $$ = STATE_SPECULAR;
1492 stateLightNumber: INTEGER
1494 if ((unsigned) $1 >= state->MaxLights) {
1495 yyerror(& @1, state, "invalid light selector");
1503 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1505 memset($$, 0, sizeof($$));
1506 $$[0] = STATE_TEXGEN;
1512 stateTexGenType: EYE
1514 $$ = STATE_TEXGEN_EYE_S;
1518 $$ = STATE_TEXGEN_OBJECT_S;
1521 stateTexGenCoord: TEXGEN_S
1523 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1527 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1531 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1535 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1539 stateFogItem: FOG stateFogProperty
1541 memset($$, 0, sizeof($$));
1546 stateFogProperty: COLOR
1548 $$ = STATE_FOG_COLOR;
1552 $$ = STATE_FOG_PARAMS;
1556 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1558 memset($$, 0, sizeof($$));
1559 $$[0] = STATE_CLIPPLANE;
1564 stateClipPlaneNum: INTEGER
1566 if ((unsigned) $1 >= state->MaxClipPlanes) {
1567 yyerror(& @1, state, "invalid clip plane selector");
1575 statePointItem: POINT_TOK statePointProperty
1577 memset($$, 0, sizeof($$));
1582 statePointProperty: SIZE_TOK
1584 $$ = STATE_POINT_SIZE;
1588 $$ = STATE_POINT_ATTENUATION;
1592 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1602 stateMatrixRows: stateMatrixItem optMatrixRows
1617 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1619 /* It seems logical that the matrix row range specifier would have
1620 * to specify a range or more than one row (i.e., $5 > $3).
1621 * However, the ARB_vertex_program spec says "a program will fail
1622 * to load if <a> is greater than <b>." This means that $3 == $5
1626 yyerror(& @3, state, "invalid matrix row range");
1635 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1643 stateOptMatModifier:
1653 stateMatModifier: INVERSE
1655 $$ = STATE_MATRIX_INVERSE;
1659 $$ = STATE_MATRIX_TRANSPOSE;
1663 $$ = STATE_MATRIX_INVTRANS;
1667 stateMatrixRowNum: INTEGER
1670 yyerror(& @1, state, "invalid matrix row reference");
1678 stateMatrixName: MODELVIEW stateOptModMatNum
1680 $$[0] = STATE_MODELVIEW_MATRIX;
1685 $$[0] = STATE_PROJECTION_MATRIX;
1690 $$[0] = STATE_MVP_MATRIX;
1693 | TEXTURE optTexCoordUnitNum
1695 $$[0] = STATE_TEXTURE_MATRIX;
1698 | PALETTE '[' statePaletteMatNum ']'
1700 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1703 | MAT_PROGRAM '[' stateProgramMatNum ']'
1705 $$[0] = STATE_PROGRAM_MATRIX;
1714 | '[' stateModMatNum ']'
1719 stateModMatNum: INTEGER
1721 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1725 yyerror(& @1, state, "invalid modelview matrix index");
1732 statePaletteMatNum: INTEGER
1734 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1735 * through here. The error will be generated later.
1740 stateProgramMatNum: INTEGER
1742 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1743 yyerror(& @1, state, "invalid program matrix selector");
1751 stateDepthItem: DEPTH RANGE
1753 memset($$, 0, sizeof($$));
1754 $$[0] = STATE_DEPTH_RANGE;
1759 programSingleItem: progEnvParam | progLocalParam;
1761 programMultipleItem: progEnvParams | progLocalParams;
1763 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1765 memset($$, 0, sizeof($$));
1766 $$[0] = state->state_param_enum;
1773 progEnvParamNums: progEnvParamNum
1778 | progEnvParamNum DOT_DOT progEnvParamNum
1785 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1787 memset($$, 0, sizeof($$));
1788 $$[0] = state->state_param_enum;
1795 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1797 memset($$, 0, sizeof($$));
1798 $$[0] = state->state_param_enum;
1799 $$[1] = STATE_LOCAL;
1804 progLocalParamNums: progLocalParamNum
1809 | progLocalParamNum DOT_DOT progLocalParamNum
1816 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1818 memset($$, 0, sizeof($$));
1819 $$[0] = state->state_param_enum;
1820 $$[1] = STATE_LOCAL;
1826 progEnvParamNum: INTEGER
1828 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1829 yyerror(& @1, state, "invalid environment parameter reference");
1836 progLocalParamNum: INTEGER
1838 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1839 yyerror(& @1, state, "invalid local parameter reference");
1848 paramConstDecl: paramConstScalarDecl | paramConstVector;
1849 paramConstUse: paramConstScalarUse | paramConstVector;
1851 paramConstScalarDecl: signedFloatConstant
1861 paramConstScalarUse: REAL
1872 $$.data[0].f = (float) $1;
1873 $$.data[1].f = (float) $1;
1874 $$.data[2].f = (float) $1;
1875 $$.data[3].f = (float) $1;
1879 paramConstVector: '{' signedFloatConstant '}'
1883 $$.data[1].f = 0.0f;
1884 $$.data[2].f = 0.0f;
1885 $$.data[3].f = 1.0f;
1887 | '{' signedFloatConstant ',' signedFloatConstant '}'
1892 $$.data[2].f = 0.0f;
1893 $$.data[3].f = 1.0f;
1895 | '{' signedFloatConstant ',' signedFloatConstant ','
1896 signedFloatConstant '}'
1902 $$.data[3].f = 1.0f;
1904 | '{' signedFloatConstant ',' signedFloatConstant ','
1905 signedFloatConstant ',' signedFloatConstant '}'
1915 signedFloatConstant: optionalSign REAL
1917 $$ = ($1) ? -$2 : $2;
1919 | optionalSign INTEGER
1921 $$ = (float)(($1) ? -$2 : $2);
1925 optionalSign: '+' { $$ = FALSE; }
1926 | '-' { $$ = TRUE; }
1930 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1935 /* NV_fragment_program_option defines the size qualifiers in a
1936 * fairly broken way. "SHORT" or "LONG" can optionally be used
1937 * before TEMP or OUTPUT. However, neither is a reserved word!
1938 * This means that we have to parse it as an identifier, then check
1939 * to make sure it's one of the valid values. *sigh*
1941 * In addition, the grammar in the extension spec does *not* allow
1942 * the size specifier to be optional, but all known implementations
1945 if (!state->option.NV_fragment) {
1946 yyerror(& @1, state, "unexpected IDENTIFIER");
1950 if (strcmp("SHORT", $1) == 0) {
1951 } else if (strcmp("LONG", $1) == 0) {
1953 char *const err_str =
1954 make_error_string("invalid storage size specifier \"%s\"",
1957 yyerror(& @1, state, (err_str != NULL)
1958 ? err_str : "invalid storage size specifier");
1960 if (err_str != NULL) {
1972 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1975 varNameList: varNameList ',' IDENTIFIER
1977 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1984 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1991 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1993 struct asm_symbol *const s =
1994 declare_variable(state, $3, at_output, & @3);
2000 s->output_binding = $5;
2005 resultBinding: RESULT POSITION
2007 if (state->mode == ARB_vertex) {
2008 $$ = VARYING_SLOT_POS;
2010 yyerror(& @2, state, "invalid program result name");
2016 if (state->mode == ARB_vertex) {
2017 $$ = VARYING_SLOT_FOGC;
2019 yyerror(& @2, state, "invalid program result name");
2023 | RESULT resultColBinding
2029 if (state->mode == ARB_vertex) {
2030 $$ = VARYING_SLOT_PSIZ;
2032 yyerror(& @2, state, "invalid program result name");
2036 | RESULT TEXCOORD optTexCoordUnitNum
2038 if (state->mode == ARB_vertex) {
2039 $$ = VARYING_SLOT_TEX0 + $3;
2041 yyerror(& @2, state, "invalid program result name");
2047 if (state->mode == ARB_fragment) {
2048 $$ = FRAG_RESULT_DEPTH;
2050 yyerror(& @2, state, "invalid program result name");
2056 resultColBinding: COLOR optResultFaceType optResultColorType
2064 if (state->mode == ARB_vertex) {
2065 $$ = VARYING_SLOT_COL0;
2067 if (state->option.DrawBuffers)
2068 $$ = FRAG_RESULT_DATA0;
2070 $$ = FRAG_RESULT_COLOR;
2075 if (state->mode == ARB_vertex) {
2076 yyerror(& @1, state, "invalid program result name");
2079 if (!state->option.DrawBuffers) {
2080 /* From the ARB_draw_buffers spec (same text exists
2081 * for ATI_draw_buffers):
2083 * If this option is not specified, a fragment
2084 * program that attempts to bind
2085 * "result.color[n]" will fail to load, and only
2086 * "result.color" will be allowed.
2088 yyerror(& @1, state,
2089 "result.color[] used without "
2090 "`OPTION ARB_draw_buffers' or "
2091 "`OPTION ATI_draw_buffers'");
2093 } else if ($2 >= state->MaxDrawBuffers) {
2094 yyerror(& @1, state,
2095 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
2098 $$ = FRAG_RESULT_DATA0 + $2;
2103 if (state->mode == ARB_vertex) {
2104 $$ = VARYING_SLOT_COL0;
2106 yyerror(& @1, state, "invalid program result name");
2112 if (state->mode == ARB_vertex) {
2113 $$ = VARYING_SLOT_BFC0;
2115 yyerror(& @1, state, "invalid program result name");
2127 if (state->mode == ARB_vertex) {
2130 yyerror(& @1, state, "invalid program result name");
2136 if (state->mode == ARB_vertex) {
2139 yyerror(& @1, state, "invalid program result name");
2145 optFaceType: { $$ = 0; }
2150 optColorType: { $$ = 0; }
2151 | PRIMARY { $$ = 0; }
2152 | SECONDARY { $$ = 1; }
2155 optTexCoordUnitNum: { $$ = 0; }
2156 | '[' texCoordUnitNum ']' { $$ = $2; }
2159 optTexImageUnitNum: { $$ = 0; }
2160 | '[' texImageUnitNum ']' { $$ = $2; }
2163 optLegacyTexUnitNum: { $$ = 0; }
2164 | '[' legacyTexUnitNum ']' { $$ = $2; }
2167 texCoordUnitNum: INTEGER
2169 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2170 yyerror(& @1, state, "invalid texture coordinate unit selector");
2178 texImageUnitNum: INTEGER
2180 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2181 yyerror(& @1, state, "invalid texture image unit selector");
2189 legacyTexUnitNum: INTEGER
2191 if ((unsigned) $1 >= state->MaxTextureUnits) {
2192 yyerror(& @1, state, "invalid texture unit selector");
2200 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2202 struct asm_symbol *exist = (struct asm_symbol *)
2203 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2204 struct asm_symbol *target = (struct asm_symbol *)
2205 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2209 if (exist != NULL) {
2211 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2213 yyerror(& @2, state, m);
2215 } else if (target == NULL) {
2217 yyerror(& @4, state,
2218 "undefined variable binding in ALIAS statement");
2221 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2233 asm_instruction_set_operands(struct asm_instruction *inst,
2234 const struct prog_dst_register *dst,
2235 const struct asm_src_register *src0,
2236 const struct asm_src_register *src1,
2237 const struct asm_src_register *src2)
2239 /* In the core ARB extensions only the KIL instruction doesn't have a
2240 * destination register.
2243 init_dst_reg(& inst->Base.DstReg);
2245 inst->Base.DstReg = *dst;
2248 /* The only instruction that doesn't have any source registers is the
2249 * condition-code based KIL instruction added by NV_fragment_program_option.
2252 inst->Base.SrcReg[0] = src0->Base;
2253 inst->SrcReg[0] = *src0;
2255 init_src_reg(& inst->SrcReg[0]);
2259 inst->Base.SrcReg[1] = src1->Base;
2260 inst->SrcReg[1] = *src1;
2262 init_src_reg(& inst->SrcReg[1]);
2266 inst->Base.SrcReg[2] = src2->Base;
2267 inst->SrcReg[2] = *src2;
2269 init_src_reg(& inst->SrcReg[2]);
2274 struct asm_instruction *
2275 asm_instruction_ctor(gl_inst_opcode op,
2276 const struct prog_dst_register *dst,
2277 const struct asm_src_register *src0,
2278 const struct asm_src_register *src1,
2279 const struct asm_src_register *src2)
2281 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2284 _mesa_init_instructions(& inst->Base, 1);
2285 inst->Base.Opcode = op;
2287 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2294 struct asm_instruction *
2295 asm_instruction_copy_ctor(const struct prog_instruction *base,
2296 const struct prog_dst_register *dst,
2297 const struct asm_src_register *src0,
2298 const struct asm_src_register *src1,
2299 const struct asm_src_register *src2)
2301 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2304 _mesa_init_instructions(& inst->Base, 1);
2305 inst->Base.Opcode = base->Opcode;
2306 inst->Base.CondUpdate = base->CondUpdate;
2307 inst->Base.CondDst = base->CondDst;
2308 inst->Base.SaturateMode = base->SaturateMode;
2309 inst->Base.Precision = base->Precision;
2311 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2319 init_dst_reg(struct prog_dst_register *r)
2321 memset(r, 0, sizeof(*r));
2322 r->File = PROGRAM_UNDEFINED;
2323 r->WriteMask = WRITEMASK_XYZW;
2324 r->CondMask = COND_TR;
2325 r->CondSwizzle = SWIZZLE_NOOP;
2329 /** Like init_dst_reg() but set the File and Index fields. */
2331 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2333 const GLint maxIndex = 1 << INST_INDEX_BITS;
2334 const GLint minIndex = 0;
2335 ASSERT(index >= minIndex);
2337 ASSERT(index <= maxIndex);
2339 ASSERT(file == PROGRAM_TEMPORARY ||
2340 file == PROGRAM_ADDRESS ||
2341 file == PROGRAM_OUTPUT);
2342 memset(r, 0, sizeof(*r));
2345 r->WriteMask = WRITEMASK_XYZW;
2346 r->CondMask = COND_TR;
2347 r->CondSwizzle = SWIZZLE_NOOP;
2352 init_src_reg(struct asm_src_register *r)
2354 memset(r, 0, sizeof(*r));
2355 r->Base.File = PROGRAM_UNDEFINED;
2356 r->Base.Swizzle = SWIZZLE_NOOP;
2361 /** Like init_src_reg() but set the File and Index fields.
2362 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2365 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2367 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2372 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2375 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2376 const GLint minIndex = -(1 << INST_INDEX_BITS);
2377 ASSERT(file < PROGRAM_FILE_MAX);
2378 ASSERT(index >= minIndex);
2380 ASSERT(index <= maxIndex);
2382 memset(r, 0, sizeof(*r));
2383 r->Base.File = file;
2384 r->Base.Index = index;
2385 r->Base.Swizzle = swizzle;
2391 * Validate the set of inputs used by a program
2393 * Validates that legal sets of inputs are used by the program. In this case
2394 * "used" included both reading the input or binding the input to a name using
2395 * the \c ATTRIB command.
2398 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2401 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2403 const GLbitfield64 inputs = state->prog->InputsRead | state->InputsBound;
2405 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2406 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2415 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2416 struct YYLTYPE *locp)
2418 struct asm_symbol *s = NULL;
2419 struct asm_symbol *exist = (struct asm_symbol *)
2420 _mesa_symbol_table_find_symbol(state->st, 0, name);
2423 if (exist != NULL) {
2424 yyerror(locp, state, "redeclared identifier");
2426 s = calloc(1, sizeof(struct asm_symbol));
2432 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2433 yyerror(locp, state, "too many temporaries declared");
2438 s->temp_binding = state->prog->NumTemporaries;
2439 state->prog->NumTemporaries++;
2443 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2444 yyerror(locp, state, "too many address registers declared");
2449 /* FINISHME: Add support for multiple address registers.
2451 state->prog->NumAddressRegs++;
2458 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2459 s->next = state->sym;
2467 int add_state_reference(struct gl_program_parameter_list *param_list,
2468 const gl_state_index tokens[STATE_LENGTH])
2470 const GLuint size = 4; /* XXX fix */
2474 name = _mesa_program_state_string(tokens);
2475 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2476 size, GL_NONE, NULL, tokens);
2477 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2479 /* free name string here since we duplicated it in add_parameter() */
2487 initialize_symbol_from_state(struct gl_program *prog,
2488 struct asm_symbol *param_var,
2489 const gl_state_index tokens[STATE_LENGTH])
2492 gl_state_index state_tokens[STATE_LENGTH];
2495 memcpy(state_tokens, tokens, sizeof(state_tokens));
2497 param_var->type = at_param;
2498 param_var->param_binding_type = PROGRAM_STATE_VAR;
2500 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2501 * unroll it and call add_state_reference() for each row
2503 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2504 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2505 state_tokens[0] == STATE_MVP_MATRIX ||
2506 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2507 state_tokens[0] == STATE_PROGRAM_MATRIX)
2508 && (state_tokens[2] != state_tokens[3])) {
2510 const int first_row = state_tokens[2];
2511 const int last_row = state_tokens[3];
2513 for (row = first_row; row <= last_row; row++) {
2514 state_tokens[2] = state_tokens[3] = row;
2516 idx = add_state_reference(prog->Parameters, state_tokens);
2517 if (param_var->param_binding_begin == ~0U) {
2518 param_var->param_binding_begin = idx;
2519 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2522 param_var->param_binding_length++;
2526 idx = add_state_reference(prog->Parameters, state_tokens);
2527 if (param_var->param_binding_begin == ~0U) {
2528 param_var->param_binding_begin = idx;
2529 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2531 param_var->param_binding_length++;
2539 initialize_symbol_from_param(struct gl_program *prog,
2540 struct asm_symbol *param_var,
2541 const gl_state_index tokens[STATE_LENGTH])
2544 gl_state_index state_tokens[STATE_LENGTH];
2547 memcpy(state_tokens, tokens, sizeof(state_tokens));
2549 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2550 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2551 assert((state_tokens[1] == STATE_ENV)
2552 || (state_tokens[1] == STATE_LOCAL));
2555 * The param type is STATE_VAR. The program parameter entry will
2556 * effectively be a pointer into the LOCAL or ENV parameter array.
2558 param_var->type = at_param;
2559 param_var->param_binding_type = PROGRAM_STATE_VAR;
2561 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2562 * we need to unroll it and call add_state_reference() for each row
2564 if (state_tokens[2] != state_tokens[3]) {
2566 const int first_row = state_tokens[2];
2567 const int last_row = state_tokens[3];
2569 for (row = first_row; row <= last_row; row++) {
2570 state_tokens[2] = state_tokens[3] = row;
2572 idx = add_state_reference(prog->Parameters, state_tokens);
2573 if (param_var->param_binding_begin == ~0U) {
2574 param_var->param_binding_begin = idx;
2575 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2577 param_var->param_binding_length++;
2581 idx = add_state_reference(prog->Parameters, state_tokens);
2582 if (param_var->param_binding_begin == ~0U) {
2583 param_var->param_binding_begin = idx;
2584 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2586 param_var->param_binding_length++;
2594 * Put a float/vector constant/literal into the parameter list.
2595 * \param param_var returns info about the parameter/constant's location,
2596 * binding, type, etc.
2597 * \param vec the vector/constant to add
2598 * \param allowSwizzle if true, try to consolidate constants which only differ
2599 * by a swizzle. We don't want to do this when building
2600 * arrays of constants that may be indexed indirectly.
2601 * \return index of the constant in the parameter list.
2604 initialize_symbol_from_const(struct gl_program *prog,
2605 struct asm_symbol *param_var,
2606 const struct asm_vector *vec,
2607 GLboolean allowSwizzle)
2610 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2611 vec->data, vec->count,
2612 allowSwizzle ? &swizzle : NULL);
2614 param_var->type = at_param;
2615 param_var->param_binding_type = PROGRAM_CONSTANT;
2617 if (param_var->param_binding_begin == ~0U) {
2618 param_var->param_binding_begin = idx;
2619 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2621 param_var->param_binding_length++;
2628 make_error_string(const char *fmt, ...)
2635 /* Call vsnprintf once to determine how large the final string is. Call it
2636 * again to do the actual formatting. from the vsnprintf manual page:
2638 * Upon successful return, these functions return the number of
2639 * characters printed (not including the trailing '\0' used to end
2640 * output to strings).
2642 va_start(args, fmt);
2643 length = 1 + vsnprintf(NULL, 0, fmt, args);
2646 str = malloc(length);
2648 va_start(args, fmt);
2649 vsnprintf(str, length, fmt, args);
2658 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2663 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2665 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2669 err_str = make_error_string("line %u, char %u: error: %s\n",
2670 locp->first_line, locp->first_column, s);
2671 _mesa_set_program_error(state->ctx, locp->position, err_str);
2680 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2681 GLsizei len, struct asm_parser_state *state)
2683 struct asm_instruction *inst;
2686 GLboolean result = GL_FALSE;
2688 struct asm_symbol *sym;
2691 state->prog->Target = target;
2692 state->prog->Parameters = _mesa_new_parameter_list();
2694 /* Make a copy of the program string and force it to be NUL-terminated.
2696 strz = (GLubyte *) malloc(len + 1);
2698 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2701 memcpy (strz, str, len);
2704 state->prog->String = strz;
2706 state->st = _mesa_symbol_table_ctor();
2708 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2709 ? & ctx->Const.VertexProgram
2710 : & ctx->Const.FragmentProgram;
2712 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2713 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2714 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2715 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2716 state->MaxLights = ctx->Const.MaxLights;
2717 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2718 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2720 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2721 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2723 _mesa_set_program_error(ctx, -1, NULL);
2725 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2727 _mesa_program_lexer_dtor(state->scanner);
2730 if (ctx->Program.ErrorPos != -1) {
2734 if (! _mesa_layout_parameters(state)) {
2738 loc.first_column = 0;
2741 yyerror(& loc, state, "invalid PARAM usage");
2747 /* Add one instruction to store the "END" instruction.
2749 state->prog->Instructions =
2750 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2751 inst = state->inst_head;
2752 for (i = 0; i < state->prog->NumInstructions; i++) {
2753 struct asm_instruction *const temp = inst->next;
2755 state->prog->Instructions[i] = inst->Base;
2759 /* Finally, tag on an OPCODE_END instruction */
2761 const GLuint numInst = state->prog->NumInstructions;
2762 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2763 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2765 state->prog->NumInstructions++;
2767 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2768 state->prog->NumAttributes = _mesa_bitcount_64(state->prog->InputsRead);
2771 * Initialize native counts to logical counts. The device driver may
2772 * change them if program is translated into a hardware program.
2774 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2775 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2776 state->prog->NumNativeParameters = state->prog->NumParameters;
2777 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2778 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2783 for (inst = state->inst_head; inst != NULL; inst = temp) {
2788 state->inst_head = NULL;
2789 state->inst_tail = NULL;
2791 for (sym = state->sym; sym != NULL; sym = temp) {
2794 free((void *) sym->name);
2799 _mesa_symbol_table_dtor(state->st);