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"
31 #include "prog_parameter.h"
32 #include "prog_parameter_layout.h"
33 #include "prog_statevars.h"
34 #include "prog_instruction.h"
36 #include "symbol_table.h"
37 #include "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);
57 static int yyparse(struct asm_parser_state *state);
59 static char *make_error_string(const char *fmt, ...);
61 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
64 static int validate_inputs(struct YYLTYPE *locp,
65 struct asm_parser_state *state);
67 static void init_dst_reg(struct prog_dst_register *r);
69 static void init_src_reg(struct asm_src_register *r);
71 static void asm_instruction_set_operands(struct asm_instruction *inst,
72 const struct prog_dst_register *dst, const struct asm_src_register *src0,
73 const struct asm_src_register *src1, const struct asm_src_register *src2);
75 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
76 const struct prog_dst_register *dst, const struct asm_src_register *src0,
77 const struct asm_src_register *src1, const struct asm_src_register *src2);
79 static struct asm_instruction *asm_instruction_copy_ctor(
80 const struct prog_instruction *base, const struct prog_dst_register *dst,
81 const struct asm_src_register *src0, const struct asm_src_register *src1,
82 const struct asm_src_register *src2);
89 #define YYLLOC_DEFAULT(Current, Rhs, N) \
92 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
93 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
94 (Current).position = YYRHSLOC(Rhs, 1).position; \
95 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
96 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
98 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
99 (Current).last_line = (Current).first_line; \
100 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
101 (Current).last_column = (Current).first_column; \
102 (Current).position = YYRHSLOC(Rhs, 0).position \
103 + (Current).first_column; \
107 #define YYLEX_PARAM state->scanner
112 %parse-param { struct asm_parser_state *state }
114 %lex-param { void *scanner }
117 struct asm_instruction *inst;
118 struct asm_symbol *sym;
119 struct asm_symbol temp_sym;
120 struct asm_swizzle_mask swiz_mask;
121 struct asm_src_register src_reg;
122 struct prog_dst_register dst_reg;
123 struct prog_instruction temp_inst;
129 gl_state_index state[STATE_LENGTH];
131 struct asm_vector vector;
132 gl_inst_opcode opcode;
136 unsigned rgba_valid:1;
137 unsigned xyzw_valid:1;
142 %token ARBvp_10 ARBfp_10
144 /* Tokens for assembler pseudo-ops */
145 %token <integer> ADDRESS
149 %token <integer> TEMP
152 /* Tokens for instructions */
153 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
154 %token <temp_inst> ARL KIL SWZ TXD_OP
156 %token <integer> INTEGER
159 %token AMBIENT ATTENUATION
162 %token DEPTH DIFFUSE DIRECTION
163 %token EMISSION ENV EYE
164 %token FOG FOGCOORD FRAGMENT FRONT
166 %token INVERSE INVTRANS
167 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
168 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
171 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
172 %token RANGE RESULT ROW
173 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
174 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
175 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
176 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
177 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
178 %token VERTEX VTXATTRIB
181 %token <string> IDENTIFIER USED_IDENTIFIER
182 %type <string> string
183 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
187 %type <inst> instruction ALU_instruction TexInstruction
188 %type <inst> ARL_instruction VECTORop_instruction
189 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
190 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
191 %type <inst> KIL_instruction
193 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
194 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
195 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
196 %type <ext_swizzle> extSwizComp extSwizSel
197 %type <swiz_mask> optionalMask
199 %type <sym> progParamArray
200 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
201 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
203 %type <swiz_mask> addrComponent addrWriteMask
205 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
207 %type <result> resultBinding resultColBinding
208 %type <integer> optFaceType optColorType
209 %type <integer> optResultFaceType optResultColorType
211 %type <integer> optTexImageUnitNum texImageUnitNum
212 %type <integer> optTexCoordUnitNum texCoordUnitNum
213 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
214 %type <integer> texImageUnit texTarget
215 %type <integer> vtxAttribNum
217 %type <attrib> attribBinding vtxAttribItem fragAttribItem
219 %type <temp_sym> paramSingleInit paramSingleItemDecl
220 %type <integer> optArraySize
222 %type <state> stateSingleItem stateMultipleItem
223 %type <state> stateMaterialItem
224 %type <state> stateLightItem stateLightModelItem stateLightProdItem
225 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
226 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
227 %type <state> stateTexEnvItem stateDepthItem
229 %type <state> stateLModProperty
230 %type <state> stateMatrixName optMatrixRows
232 %type <integer> stateMatProperty
233 %type <integer> stateLightProperty stateSpotProperty
234 %type <integer> stateLightNumber stateLProdProperty
235 %type <integer> stateTexGenType stateTexGenCoord
236 %type <integer> stateTexEnvProperty
237 %type <integer> stateFogProperty
238 %type <integer> stateClipPlaneNum
239 %type <integer> statePointProperty
241 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
242 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
243 %type <integer> stateProgramMatNum
245 %type <integer> ambDiffSpecProperty
247 %type <state> programSingleItem progEnvParam progLocalParam
248 %type <state> programMultipleItem progEnvParams progLocalParams
250 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
251 %type <temp_sym> paramSingleItemUse
253 %type <integer> progEnvParamNum progLocalParamNum
254 %type <state> progEnvParamNums progLocalParamNums
256 %type <vector> paramConstDecl paramConstUse
257 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
258 %type <real> signedFloatConstant
259 %type <negate> optionalSign
262 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
268 program: language optionSequence statementSequence END
273 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
274 yyerror(& @1, state, "invalid fragment program header");
277 state->mode = ARB_vertex;
281 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
282 yyerror(& @1, state, "invalid vertex program header");
284 state->mode = ARB_fragment;
286 state->option.TexRect =
287 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
291 optionSequence: optionSequence option
295 option: OPTION string ';'
299 if (state->mode == ARB_vertex) {
300 valid = _mesa_ARBvp_parse_option(state, $2);
301 } else if (state->mode == ARB_fragment) {
302 valid = _mesa_ARBfp_parse_option(state, $2);
307 const char *const err_str = (state->mode == ARB_vertex)
308 ? "invalid ARB vertex program option"
309 : "invalid ARB fragment program option";
311 yyerror(& @2, state, err_str);
317 statementSequence: statementSequence statement
321 statement: instruction ';'
324 if (state->inst_tail == NULL) {
325 state->inst_head = $1;
327 state->inst_tail->next = $1;
330 state->inst_tail = $1;
333 state->prog->NumInstructions++;
336 | namingStatement ';'
339 instruction: ALU_instruction
342 state->prog->NumAluInstructions++;
347 state->prog->NumTexInstructions++;
351 ALU_instruction: ARL_instruction
352 | VECTORop_instruction
353 | SCALARop_instruction
354 | BINSCop_instruction
360 TexInstruction: SAMPLE_instruction
365 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
367 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
371 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
373 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
377 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
379 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
383 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
385 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
390 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
392 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
396 TRIop_instruction: TRI_OP maskedDstReg ','
397 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
399 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
403 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
405 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
407 const GLbitfield tex_mask = (1U << $6);
408 GLbitfield shadow_tex = 0;
409 GLbitfield target_mask = 0;
412 $$->Base.TexSrcUnit = $6;
415 shadow_tex = tex_mask;
417 $$->Base.TexSrcTarget = -$8;
418 $$->Base.TexShadow = 1;
420 $$->Base.TexSrcTarget = $8;
423 target_mask = (1U << $$->Base.TexSrcTarget);
425 /* If this texture unit was previously accessed and that access
426 * had a different texture target, generate an error.
428 * If this texture unit was previously accessed and that access
429 * had a different shadow mode, generate an error.
431 if ((state->prog->TexturesUsed[$6] != 0)
432 && ((state->prog->TexturesUsed[$6] != target_mask)
433 || ((state->prog->ShadowSamplers & tex_mask)
436 "multiple targets used on one texture image unit");
441 state->prog->TexturesUsed[$6] |= target_mask;
442 state->prog->ShadowSamplers |= shadow_tex;
447 KIL_instruction: KIL swizzleSrcReg
449 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
450 state->fragment.UsesKill = 1;
454 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
455 $$->Base.DstReg.CondMask = $2.CondMask;
456 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
457 $$->Base.DstReg.CondSrc = $2.CondSrc;
458 state->fragment.UsesKill = 1;
462 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
464 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
466 const GLbitfield tex_mask = (1U << $10);
467 GLbitfield shadow_tex = 0;
468 GLbitfield target_mask = 0;
471 $$->Base.TexSrcUnit = $10;
474 shadow_tex = tex_mask;
476 $$->Base.TexSrcTarget = -$12;
477 $$->Base.TexShadow = 1;
479 $$->Base.TexSrcTarget = $12;
482 target_mask = (1U << $$->Base.TexSrcTarget);
484 /* If this texture unit was previously accessed and that access
485 * had a different texture target, generate an error.
487 * If this texture unit was previously accessed and that access
488 * had a different shadow mode, generate an error.
490 if ((state->prog->TexturesUsed[$10] != 0)
491 && ((state->prog->TexturesUsed[$10] != target_mask)
492 || ((state->prog->ShadowSamplers & tex_mask)
494 yyerror(& @12, state,
495 "multiple targets used on one texture image unit");
500 state->prog->TexturesUsed[$10] |= target_mask;
501 state->prog->ShadowSamplers |= shadow_tex;
506 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
512 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
513 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
514 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
515 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
516 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
517 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
518 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
519 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
520 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
521 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
522 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
523 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
526 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
528 /* FIXME: Is this correct? Should the extenedSwizzle be applied
529 * FIXME: to the existing swizzle?
531 $4.Base.Swizzle = $6.swizzle;
532 $4.Base.Negate = $6.mask;
534 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
538 scalarSrcReg: optionalSign scalarUse
543 $$.Base.Negate = ~$$.Base.Negate;
546 | optionalSign '|' scalarUse '|'
550 if (!state->option.NV_fragment) {
551 yyerror(& @2, state, "unexpected character '|'");
556 $$.Base.Negate = ~$$.Base.Negate;
563 scalarUse: srcReg scalarSuffix
567 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
570 | paramConstScalarUse
572 struct asm_symbol temp_sym;
574 if (!state->option.NV_fragment) {
575 yyerror(& @1, state, "expected scalar suffix");
579 memset(& temp_sym, 0, sizeof(temp_sym));
580 temp_sym.param_binding_begin = ~0;
581 initialize_symbol_from_const(state->prog, & temp_sym, & $1);
584 $$.Base.File = PROGRAM_CONSTANT;
585 $$.Base.Index = temp_sym.param_binding_begin;
589 swizzleSrcReg: optionalSign srcReg swizzleSuffix
594 $$.Base.Negate = ~$$.Base.Negate;
597 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
600 | optionalSign '|' srcReg swizzleSuffix '|'
604 if (!state->option.NV_fragment) {
605 yyerror(& @2, state, "unexpected character '|'");
610 $$.Base.Negate = ~$$.Base.Negate;
614 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
620 maskedDstReg: dstReg optionalMask optionalCcMask
623 $$.WriteMask = $2.mask;
624 $$.CondMask = $3.CondMask;
625 $$.CondSwizzle = $3.CondSwizzle;
626 $$.CondSrc = $3.CondSrc;
628 if ($$.File == PROGRAM_OUTPUT) {
629 /* Technically speaking, this should check that it is in
630 * vertex program mode. However, PositionInvariant can never be
631 * set in fragment program mode, so it is somewhat irrelevant.
633 if (state->option.PositionInvariant
634 && ($$.Index == VERT_RESULT_HPOS)) {
635 yyerror(& @1, state, "position-invariant programs cannot "
640 state->prog->OutputsWritten |= (1U << $$.Index);
645 maskedAddrReg: addrReg addrWriteMask
648 $$.File = PROGRAM_ADDRESS;
650 $$.WriteMask = $2.mask;
654 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
656 const unsigned xyzw_valid =
658 | ($3.xyzw_valid << 1)
659 | ($5.xyzw_valid << 2)
660 | ($7.xyzw_valid << 3);
661 const unsigned rgba_valid =
663 | ($3.rgba_valid << 1)
664 | ($5.rgba_valid << 2)
665 | ($7.rgba_valid << 3);
667 /* All of the swizzle components have to be valid in either RGBA
668 * or XYZW. Note that 0 and 1 are valid in both, so both masks
669 * can have some bits set.
671 * We somewhat deviate from the spec here. It would be really hard
672 * to figure out which component is the error, and there probably
673 * isn't a lot of benefit.
675 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
676 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
681 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
682 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
687 extSwizComp: optionalSign extSwizSel
690 $$.negate = ($1) ? 1 : 0;
696 if (($1 != 0) && ($1 != 1)) {
697 yyerror(& @1, state, "invalid extended swizzle selector");
701 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
703 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
711 if (strlen($1) > 1) {
712 yyerror(& @1, state, "invalid extended swizzle selector");
752 yyerror(& @1, state, "invalid extended swizzle selector");
759 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
761 struct asm_symbol *const s = (struct asm_symbol *)
762 _mesa_symbol_table_find_symbol(state->st, 0, $1);
765 yyerror(& @1, state, "invalid operand variable");
767 } else if ((s->type != at_param) && (s->type != at_temp)
768 && (s->type != at_attrib)) {
769 yyerror(& @1, state, "invalid operand variable");
771 } else if ((s->type == at_param) && s->param_is_array) {
772 yyerror(& @1, state, "non-array access to array PARAM");
779 $$.Base.File = PROGRAM_TEMPORARY;
780 $$.Base.Index = s->temp_binding;
783 $$.Base.File = s->param_binding_type;
784 $$.Base.Index = s->param_binding_begin;
787 $$.Base.File = PROGRAM_INPUT;
788 $$.Base.Index = s->attrib_binding;
789 state->prog->InputsRead |= (1U << $$.Base.Index);
791 if (!validate_inputs(& @1, state)) {
804 $$.Base.File = PROGRAM_INPUT;
806 state->prog->InputsRead |= (1U << $$.Base.Index);
808 if (!validate_inputs(& @1, state)) {
812 | progParamArray '[' progParamArrayMem ']'
814 if (! $3.Base.RelAddr
815 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
816 yyerror(& @3, state, "out of bounds array access");
821 $$.Base.File = $1->param_binding_type;
823 if ($3.Base.RelAddr) {
824 $1->param_accessed_indirectly = 1;
827 $$.Base.Index = $3.Base.Index;
830 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
836 $$.Base.File = ($1.name != NULL)
837 ? $1.param_binding_type
839 $$.Base.Index = $1.param_binding_begin;
843 dstReg: resultBinding
846 $$.File = PROGRAM_OUTPUT;
849 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
851 struct asm_symbol *const s = (struct asm_symbol *)
852 _mesa_symbol_table_find_symbol(state->st, 0, $1);
855 yyerror(& @1, state, "invalid operand variable");
857 } else if ((s->type != at_output) && (s->type != at_temp)) {
858 yyerror(& @1, state, "invalid operand variable");
865 $$.File = PROGRAM_TEMPORARY;
866 $$.Index = s->temp_binding;
869 $$.File = PROGRAM_OUTPUT;
870 $$.Index = s->output_binding;
873 $$.File = s->param_binding_type;
874 $$.Index = s->param_binding_begin;
880 progParamArray: USED_IDENTIFIER
882 struct asm_symbol *const s = (struct asm_symbol *)
883 _mesa_symbol_table_find_symbol(state->st, 0, $1);
886 yyerror(& @1, state, "invalid operand variable");
888 } else if ((s->type != at_param) || !s->param_is_array) {
889 yyerror(& @1, state, "array access to non-PARAM variable");
897 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
899 progParamArrayAbs: INTEGER
906 progParamArrayRel: addrReg addrComponent addrRegRelOffset
908 /* FINISHME: Add support for multiple address registers.
910 /* FINISHME: Add support for 4-component address registers.
918 addrRegRelOffset: { $$ = 0; }
919 | '+' addrRegPosOffset { $$ = $2; }
920 | '-' addrRegNegOffset { $$ = -$2; }
923 addrRegPosOffset: INTEGER
925 if (($1 < 0) || ($1 > 63)) {
927 _mesa_snprintf(s, sizeof(s),
928 "relative address offset too large (%d)", $1);
929 yyerror(& @1, state, s);
937 addrRegNegOffset: INTEGER
939 if (($1 < 0) || ($1 > 64)) {
941 _mesa_snprintf(s, sizeof(s),
942 "relative address offset too large (%d)", $1);
943 yyerror(& @1, state, s);
951 addrReg: USED_IDENTIFIER
953 struct asm_symbol *const s = (struct asm_symbol *)
954 _mesa_symbol_table_find_symbol(state->st, 0, $1);
957 yyerror(& @1, state, "invalid array member");
959 } else if (s->type != at_address) {
961 "invalid variable for indexed array access");
971 if ($1.mask != WRITEMASK_X) {
972 yyerror(& @1, state, "invalid address component selector");
982 if ($1.mask != WRITEMASK_X) {
984 "address register write mask must be \".x\"");
997 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1000 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1001 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1004 optionalCcMask: '(' ccTest ')'
1014 $$.CondMask = COND_TR;
1015 $$.CondSwizzle = SWIZZLE_NOOP;
1020 ccTest: ccMaskRule swizzleSuffix
1023 $$.CondSwizzle = $2.swizzle;
1027 ccTest2: ccMaskRule2 swizzleSuffix
1030 $$.CondSwizzle = $2.swizzle;
1034 ccMaskRule: IDENTIFIER
1036 const int cond = _mesa_parse_cc($1);
1037 if ((cond == 0) || ($1[2] != '\0')) {
1038 char *const err_str =
1039 make_error_string("invalid condition code \"%s\"", $1);
1041 yyerror(& @1, state, (err_str != NULL)
1042 ? err_str : "invalid condition code");
1044 if (err_str != NULL) {
1045 _mesa_free(err_str);
1052 $$.CondSwizzle = SWIZZLE_NOOP;
1057 ccMaskRule2: USED_IDENTIFIER
1059 const int cond = _mesa_parse_cc($1);
1060 if ((cond == 0) || ($1[2] != '\0')) {
1061 char *const err_str =
1062 make_error_string("invalid condition code \"%s\"", $1);
1064 yyerror(& @1, state, (err_str != NULL)
1065 ? err_str : "invalid condition code");
1067 if (err_str != NULL) {
1068 _mesa_free(err_str);
1075 $$.CondSwizzle = SWIZZLE_NOOP;
1080 namingStatement: ATTRIB_statement
1088 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1090 struct asm_symbol *const s =
1091 declare_variable(state, $2, at_attrib, & @2);
1096 s->attrib_binding = $4;
1097 state->InputsBound |= (1U << s->attrib_binding);
1099 if (!validate_inputs(& @4, state)) {
1106 attribBinding: VERTEX vtxAttribItem
1110 | FRAGMENT fragAttribItem
1116 vtxAttribItem: POSITION
1118 $$ = VERT_ATTRIB_POS;
1120 | WEIGHT vtxOptWeightNum
1122 $$ = VERT_ATTRIB_WEIGHT;
1126 $$ = VERT_ATTRIB_NORMAL;
1128 | COLOR optColorType
1130 if (!state->ctx->Extensions.EXT_secondary_color) {
1131 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1135 $$ = VERT_ATTRIB_COLOR0 + $2;
1139 if (!state->ctx->Extensions.EXT_fog_coord) {
1140 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1144 $$ = VERT_ATTRIB_FOG;
1146 | TEXCOORD optTexCoordUnitNum
1148 $$ = VERT_ATTRIB_TEX0 + $2;
1150 | MATRIXINDEX '[' vtxWeightNum ']'
1152 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1155 | VTXATTRIB '[' vtxAttribNum ']'
1157 $$ = VERT_ATTRIB_GENERIC0 + $3;
1161 vtxAttribNum: INTEGER
1163 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1164 yyerror(& @1, state, "invalid vertex attribute reference");
1172 vtxOptWeightNum: | '[' vtxWeightNum ']';
1173 vtxWeightNum: INTEGER;
1175 fragAttribItem: POSITION
1177 $$ = FRAG_ATTRIB_WPOS;
1179 | COLOR optColorType
1181 $$ = FRAG_ATTRIB_COL0 + $2;
1185 $$ = FRAG_ATTRIB_FOGC;
1187 | TEXCOORD optTexCoordUnitNum
1189 $$ = FRAG_ATTRIB_TEX0 + $2;
1193 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1195 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1197 struct asm_symbol *const s =
1198 declare_variable(state, $2, at_param, & @2);
1203 s->param_binding_type = $3.param_binding_type;
1204 s->param_binding_begin = $3.param_binding_begin;
1205 s->param_binding_length = $3.param_binding_length;
1206 s->param_is_array = 0;
1211 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1213 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1214 yyerror(& @4, state,
1215 "parameter array size and number of bindings must match");
1218 struct asm_symbol *const s =
1219 declare_variable(state, $2, $6.type, & @2);
1224 s->param_binding_type = $6.param_binding_type;
1225 s->param_binding_begin = $6.param_binding_begin;
1226 s->param_binding_length = $6.param_binding_length;
1227 s->param_is_array = 1;
1239 if (($1 < 1) || ((unsigned) $1 >= state->limits->MaxParameters)) {
1240 yyerror(& @1, state, "invalid parameter array size");
1248 paramSingleInit: '=' paramSingleItemDecl
1254 paramMultipleInit: '=' '{' paramMultInitList '}'
1260 paramMultInitList: paramMultipleItem
1261 | paramMultInitList ',' paramMultipleItem
1263 $1.param_binding_length += $3.param_binding_length;
1268 paramSingleItemDecl: stateSingleItem
1270 memset(& $$, 0, sizeof($$));
1271 $$.param_binding_begin = ~0;
1272 initialize_symbol_from_state(state->prog, & $$, $1);
1276 memset(& $$, 0, sizeof($$));
1277 $$.param_binding_begin = ~0;
1278 initialize_symbol_from_param(state->prog, & $$, $1);
1282 memset(& $$, 0, sizeof($$));
1283 $$.param_binding_begin = ~0;
1284 initialize_symbol_from_const(state->prog, & $$, & $1);
1288 paramSingleItemUse: stateSingleItem
1290 memset(& $$, 0, sizeof($$));
1291 $$.param_binding_begin = ~0;
1292 initialize_symbol_from_state(state->prog, & $$, $1);
1296 memset(& $$, 0, sizeof($$));
1297 $$.param_binding_begin = ~0;
1298 initialize_symbol_from_param(state->prog, & $$, $1);
1302 memset(& $$, 0, sizeof($$));
1303 $$.param_binding_begin = ~0;
1304 initialize_symbol_from_const(state->prog, & $$, & $1);
1308 paramMultipleItem: stateMultipleItem
1310 memset(& $$, 0, sizeof($$));
1311 $$.param_binding_begin = ~0;
1312 initialize_symbol_from_state(state->prog, & $$, $1);
1314 | programMultipleItem
1316 memset(& $$, 0, sizeof($$));
1317 $$.param_binding_begin = ~0;
1318 initialize_symbol_from_param(state->prog, & $$, $1);
1322 memset(& $$, 0, sizeof($$));
1323 $$.param_binding_begin = ~0;
1324 initialize_symbol_from_const(state->prog, & $$, & $1);
1328 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1329 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1332 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1333 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1334 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1335 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1336 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1337 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1338 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1339 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1340 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1341 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1342 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1345 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1347 memset($$, 0, sizeof($$));
1348 $$[0] = STATE_MATERIAL;
1354 stateMatProperty: ambDiffSpecProperty
1360 $$ = STATE_EMISSION;
1364 $$ = STATE_SHININESS;
1368 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1370 memset($$, 0, sizeof($$));
1371 $$[0] = STATE_LIGHT;
1377 stateLightProperty: ambDiffSpecProperty
1383 $$ = STATE_POSITION;
1387 if (!state->ctx->Extensions.EXT_point_parameters) {
1388 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1392 $$ = STATE_ATTENUATION;
1394 | SPOT stateSpotProperty
1400 $$ = STATE_HALF_VECTOR;
1404 stateSpotProperty: DIRECTION
1406 $$ = STATE_SPOT_DIRECTION;
1410 stateLightModelItem: LIGHTMODEL stateLModProperty
1417 stateLModProperty: AMBIENT
1419 memset($$, 0, sizeof($$));
1420 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1422 | optFaceType SCENECOLOR
1424 memset($$, 0, sizeof($$));
1425 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1430 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1432 memset($$, 0, sizeof($$));
1433 $$[0] = STATE_LIGHTPROD;
1440 stateLProdProperty: ambDiffSpecProperty;
1442 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1444 memset($$, 0, sizeof($$));
1450 stateTexEnvProperty: COLOR
1452 $$ = STATE_TEXENV_COLOR;
1456 ambDiffSpecProperty: AMBIENT
1466 $$ = STATE_SPECULAR;
1470 stateLightNumber: INTEGER
1472 if ((unsigned) $1 >= state->MaxLights) {
1473 yyerror(& @1, state, "invalid light selector");
1481 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1483 memset($$, 0, sizeof($$));
1484 $$[0] = STATE_TEXGEN;
1490 stateTexGenType: EYE
1492 $$ = STATE_TEXGEN_EYE_S;
1496 $$ = STATE_TEXGEN_OBJECT_S;
1499 stateTexGenCoord: TEXGEN_S
1501 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1505 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1509 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1513 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1517 stateFogItem: FOG stateFogProperty
1519 memset($$, 0, sizeof($$));
1524 stateFogProperty: COLOR
1526 $$ = STATE_FOG_COLOR;
1530 $$ = STATE_FOG_PARAMS;
1534 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1536 memset($$, 0, sizeof($$));
1537 $$[0] = STATE_CLIPPLANE;
1542 stateClipPlaneNum: INTEGER
1544 if ((unsigned) $1 >= state->MaxClipPlanes) {
1545 yyerror(& @1, state, "invalid clip plane selector");
1553 statePointItem: POINT_TOK statePointProperty
1555 memset($$, 0, sizeof($$));
1560 statePointProperty: SIZE_TOK
1562 $$ = STATE_POINT_SIZE;
1566 $$ = STATE_POINT_ATTENUATION;
1570 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1580 stateMatrixRows: stateMatrixItem optMatrixRows
1595 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1597 /* It seems logical that the matrix row range specifier would have
1598 * to specify a range or more than one row (i.e., $5 > $3).
1599 * However, the ARB_vertex_program spec says "a program will fail
1600 * to load if <a> is greater than <b>." This means that $3 == $5
1604 yyerror(& @3, state, "invalid matrix row range");
1613 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1621 stateOptMatModifier:
1631 stateMatModifier: INVERSE
1633 $$ = STATE_MATRIX_INVERSE;
1637 $$ = STATE_MATRIX_TRANSPOSE;
1641 $$ = STATE_MATRIX_INVTRANS;
1645 stateMatrixRowNum: INTEGER
1648 yyerror(& @1, state, "invalid matrix row reference");
1656 stateMatrixName: MODELVIEW stateOptModMatNum
1658 $$[0] = STATE_MODELVIEW_MATRIX;
1663 $$[0] = STATE_PROJECTION_MATRIX;
1668 $$[0] = STATE_MVP_MATRIX;
1671 | TEXTURE optTexCoordUnitNum
1673 $$[0] = STATE_TEXTURE_MATRIX;
1676 | PALETTE '[' statePaletteMatNum ']'
1678 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1681 | MAT_PROGRAM '[' stateProgramMatNum ']'
1683 $$[0] = STATE_PROGRAM_MATRIX;
1692 | '[' stateModMatNum ']'
1697 stateModMatNum: INTEGER
1699 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1703 yyerror(& @1, state, "invalid modelview matrix index");
1710 statePaletteMatNum: INTEGER
1712 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1713 * through here. The error will be generated later.
1718 stateProgramMatNum: INTEGER
1720 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1721 yyerror(& @1, state, "invalid program matrix selector");
1729 stateDepthItem: DEPTH RANGE
1731 memset($$, 0, sizeof($$));
1732 $$[0] = STATE_DEPTH_RANGE;
1737 programSingleItem: progEnvParam | progLocalParam;
1739 programMultipleItem: progEnvParams | progLocalParams;
1741 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1743 memset($$, 0, sizeof($$));
1744 $$[0] = state->state_param_enum;
1751 progEnvParamNums: progEnvParamNum
1756 | progEnvParamNum DOT_DOT progEnvParamNum
1763 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1765 memset($$, 0, sizeof($$));
1766 $$[0] = state->state_param_enum;
1773 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1775 memset($$, 0, sizeof($$));
1776 $$[0] = state->state_param_enum;
1777 $$[1] = STATE_LOCAL;
1782 progLocalParamNums: progLocalParamNum
1787 | progLocalParamNum DOT_DOT progLocalParamNum
1794 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1796 memset($$, 0, sizeof($$));
1797 $$[0] = state->state_param_enum;
1798 $$[1] = STATE_LOCAL;
1804 progEnvParamNum: INTEGER
1806 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1807 yyerror(& @1, state, "invalid environment parameter reference");
1814 progLocalParamNum: INTEGER
1816 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1817 yyerror(& @1, state, "invalid local parameter reference");
1826 paramConstDecl: paramConstScalarDecl | paramConstVector;
1827 paramConstUse: paramConstScalarUse | paramConstVector;
1829 paramConstScalarDecl: signedFloatConstant
1839 paramConstScalarUse: REAL
1850 $$.data[0] = (float) $1;
1851 $$.data[1] = (float) $1;
1852 $$.data[2] = (float) $1;
1853 $$.data[3] = (float) $1;
1857 paramConstVector: '{' signedFloatConstant '}'
1865 | '{' signedFloatConstant ',' signedFloatConstant '}'
1873 | '{' signedFloatConstant ',' signedFloatConstant ','
1874 signedFloatConstant '}'
1882 | '{' signedFloatConstant ',' signedFloatConstant ','
1883 signedFloatConstant ',' signedFloatConstant '}'
1893 signedFloatConstant: optionalSign REAL
1895 $$ = ($1) ? -$2 : $2;
1897 | optionalSign INTEGER
1899 $$ = (float)(($1) ? -$2 : $2);
1903 optionalSign: '+' { $$ = FALSE; }
1904 | '-' { $$ = TRUE; }
1908 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1913 /* NV_fragment_program_option defines the size qualifiers in a
1914 * fairly broken way. "SHORT" or "LONG" can optionally be used
1915 * before TEMP or OUTPUT. However, neither is a reserved word!
1916 * This means that we have to parse it as an identifier, then check
1917 * to make sure it's one of the valid values. *sigh*
1919 * In addition, the grammar in the extension spec does *not* allow
1920 * the size specifier to be optional, but all known implementations
1923 if (!state->option.NV_fragment) {
1924 yyerror(& @1, state, "unexpected IDENTIFIER");
1928 if (strcmp("SHORT", $1) == 0) {
1929 } else if (strcmp("LONG", $1) == 0) {
1931 char *const err_str =
1932 make_error_string("invalid storage size specifier \"%s\"",
1935 yyerror(& @1, state, (err_str != NULL)
1936 ? err_str : "invalid storage size specifier");
1938 if (err_str != NULL) {
1939 _mesa_free(err_str);
1950 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1953 varNameList: varNameList ',' IDENTIFIER
1955 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1961 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1967 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1969 struct asm_symbol *const s =
1970 declare_variable(state, $3, at_output, & @3);
1975 s->output_binding = $5;
1980 resultBinding: RESULT POSITION
1982 if (state->mode == ARB_vertex) {
1983 $$ = VERT_RESULT_HPOS;
1985 yyerror(& @2, state, "invalid program result name");
1991 if (state->mode == ARB_vertex) {
1992 $$ = VERT_RESULT_FOGC;
1994 yyerror(& @2, state, "invalid program result name");
1998 | RESULT resultColBinding
2004 if (state->mode == ARB_vertex) {
2005 $$ = VERT_RESULT_PSIZ;
2007 yyerror(& @2, state, "invalid program result name");
2011 | RESULT TEXCOORD optTexCoordUnitNum
2013 if (state->mode == ARB_vertex) {
2014 $$ = VERT_RESULT_TEX0 + $3;
2016 yyerror(& @2, state, "invalid program result name");
2022 if (state->mode == ARB_fragment) {
2023 $$ = FRAG_RESULT_DEPTH;
2025 yyerror(& @2, state, "invalid program result name");
2031 resultColBinding: COLOR optResultFaceType optResultColorType
2039 $$ = (state->mode == ARB_vertex)
2041 : FRAG_RESULT_COLOR;
2045 if (state->mode == ARB_vertex) {
2046 $$ = VERT_RESULT_COL0;
2048 yyerror(& @1, state, "invalid program result name");
2054 if (state->mode == ARB_vertex) {
2055 $$ = VERT_RESULT_BFC0;
2057 yyerror(& @1, state, "invalid program result name");
2069 if (state->mode == ARB_vertex) {
2072 yyerror(& @1, state, "invalid program result name");
2078 if (state->mode == ARB_vertex) {
2081 yyerror(& @1, state, "invalid program result name");
2087 optFaceType: { $$ = 0; }
2092 optColorType: { $$ = 0; }
2093 | PRIMARY { $$ = 0; }
2094 | SECONDARY { $$ = 1; }
2097 optTexCoordUnitNum: { $$ = 0; }
2098 | '[' texCoordUnitNum ']' { $$ = $2; }
2101 optTexImageUnitNum: { $$ = 0; }
2102 | '[' texImageUnitNum ']' { $$ = $2; }
2105 optLegacyTexUnitNum: { $$ = 0; }
2106 | '[' legacyTexUnitNum ']' { $$ = $2; }
2109 texCoordUnitNum: INTEGER
2111 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2112 yyerror(& @1, state, "invalid texture coordinate unit selector");
2120 texImageUnitNum: INTEGER
2122 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2123 yyerror(& @1, state, "invalid texture image unit selector");
2131 legacyTexUnitNum: INTEGER
2133 if ((unsigned) $1 >= state->MaxTextureUnits) {
2134 yyerror(& @1, state, "invalid texture unit selector");
2142 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2144 struct asm_symbol *exist = (struct asm_symbol *)
2145 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2146 struct asm_symbol *target = (struct asm_symbol *)
2147 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2150 if (exist != NULL) {
2151 yyerror(& @2, state, "redeclared identifier");
2153 } else if (target == NULL) {
2154 yyerror(& @4, state,
2155 "undefined variable binding in ALIAS statement");
2158 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2170 asm_instruction_set_operands(struct asm_instruction *inst,
2171 const struct prog_dst_register *dst,
2172 const struct asm_src_register *src0,
2173 const struct asm_src_register *src1,
2174 const struct asm_src_register *src2)
2176 /* In the core ARB extensions only the KIL instruction doesn't have a
2177 * destination register.
2180 init_dst_reg(& inst->Base.DstReg);
2182 inst->Base.DstReg = *dst;
2185 /* The only instruction that doesn't have any source registers is the
2186 * condition-code based KIL instruction added by NV_fragment_program_option.
2189 inst->Base.SrcReg[0] = src0->Base;
2190 inst->SrcReg[0] = *src0;
2192 init_src_reg(& inst->SrcReg[0]);
2196 inst->Base.SrcReg[1] = src1->Base;
2197 inst->SrcReg[1] = *src1;
2199 init_src_reg(& inst->SrcReg[1]);
2203 inst->Base.SrcReg[2] = src2->Base;
2204 inst->SrcReg[2] = *src2;
2206 init_src_reg(& inst->SrcReg[2]);
2211 struct asm_instruction *
2212 asm_instruction_ctor(gl_inst_opcode op,
2213 const struct prog_dst_register *dst,
2214 const struct asm_src_register *src0,
2215 const struct asm_src_register *src1,
2216 const struct asm_src_register *src2)
2218 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2221 _mesa_init_instructions(& inst->Base, 1);
2222 inst->Base.Opcode = op;
2224 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2231 struct asm_instruction *
2232 asm_instruction_copy_ctor(const struct prog_instruction *base,
2233 const struct prog_dst_register *dst,
2234 const struct asm_src_register *src0,
2235 const struct asm_src_register *src1,
2236 const struct asm_src_register *src2)
2238 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2241 _mesa_init_instructions(& inst->Base, 1);
2242 inst->Base.Opcode = base->Opcode;
2243 inst->Base.CondUpdate = base->CondUpdate;
2244 inst->Base.CondDst = base->CondDst;
2245 inst->Base.SaturateMode = base->SaturateMode;
2246 inst->Base.Precision = base->Precision;
2248 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2256 init_dst_reg(struct prog_dst_register *r)
2258 memset(r, 0, sizeof(*r));
2259 r->File = PROGRAM_UNDEFINED;
2260 r->WriteMask = WRITEMASK_XYZW;
2261 r->CondMask = COND_TR;
2262 r->CondSwizzle = SWIZZLE_NOOP;
2267 init_src_reg(struct asm_src_register *r)
2269 memset(r, 0, sizeof(*r));
2270 r->Base.File = PROGRAM_UNDEFINED;
2271 r->Base.Swizzle = SWIZZLE_NOOP;
2277 * Validate the set of inputs used by a program
2279 * Validates that legal sets of inputs are used by the program. In this case
2280 * "used" included both reading the input or binding the input to a name using
2281 * the \c ATTRIB command.
2284 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2287 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2289 const int inputs = state->prog->InputsRead | state->InputsBound;
2291 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
2292 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2301 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2302 struct YYLTYPE *locp)
2304 struct asm_symbol *s = NULL;
2305 struct asm_symbol *exist = (struct asm_symbol *)
2306 _mesa_symbol_table_find_symbol(state->st, 0, name);
2309 if (exist != NULL) {
2310 yyerror(locp, state, "redeclared identifier");
2312 s = calloc(1, sizeof(struct asm_symbol));
2318 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2319 yyerror(locp, state, "too many temporaries declared");
2324 s->temp_binding = state->prog->NumTemporaries;
2325 state->prog->NumTemporaries++;
2329 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2330 yyerror(locp, state, "too many address registers declared");
2335 /* FINISHME: Add support for multiple address registers.
2337 state->prog->NumAddressRegs++;
2344 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2345 s->next = state->sym;
2353 int add_state_reference(struct gl_program_parameter_list *param_list,
2354 const gl_state_index tokens[STATE_LENGTH])
2356 const GLuint size = 4; /* XXX fix */
2360 name = _mesa_program_state_string(tokens);
2361 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2362 size, GL_NONE, NULL, tokens, 0x0);
2363 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2365 /* free name string here since we duplicated it in add_parameter() */
2373 initialize_symbol_from_state(struct gl_program *prog,
2374 struct asm_symbol *param_var,
2375 const gl_state_index tokens[STATE_LENGTH])
2378 gl_state_index state_tokens[STATE_LENGTH];
2381 memcpy(state_tokens, tokens, sizeof(state_tokens));
2383 param_var->type = at_param;
2384 param_var->param_binding_type = PROGRAM_STATE_VAR;
2386 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2387 * unroll it and call add_state_reference() for each row
2389 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2390 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2391 state_tokens[0] == STATE_MVP_MATRIX ||
2392 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2393 state_tokens[0] == STATE_PROGRAM_MATRIX)
2394 && (state_tokens[2] != state_tokens[3])) {
2396 const int first_row = state_tokens[2];
2397 const int last_row = state_tokens[3];
2399 for (row = first_row; row <= last_row; row++) {
2400 state_tokens[2] = state_tokens[3] = row;
2402 idx = add_state_reference(prog->Parameters, state_tokens);
2403 if (param_var->param_binding_begin == ~0U)
2404 param_var->param_binding_begin = idx;
2405 param_var->param_binding_length++;
2409 idx = add_state_reference(prog->Parameters, state_tokens);
2410 if (param_var->param_binding_begin == ~0U)
2411 param_var->param_binding_begin = idx;
2412 param_var->param_binding_length++;
2420 initialize_symbol_from_param(struct gl_program *prog,
2421 struct asm_symbol *param_var,
2422 const gl_state_index tokens[STATE_LENGTH])
2425 gl_state_index state_tokens[STATE_LENGTH];
2428 memcpy(state_tokens, tokens, sizeof(state_tokens));
2430 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2431 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2432 assert((state_tokens[1] == STATE_ENV)
2433 || (state_tokens[1] == STATE_LOCAL));
2435 param_var->type = at_param;
2436 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2437 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
2439 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2440 * we need to unroll it and call add_state_reference() for each row
2442 if (state_tokens[2] != state_tokens[3]) {
2444 const int first_row = state_tokens[2];
2445 const int last_row = state_tokens[3];
2447 for (row = first_row; row <= last_row; row++) {
2448 state_tokens[2] = state_tokens[3] = row;
2450 idx = add_state_reference(prog->Parameters, state_tokens);
2451 if (param_var->param_binding_begin == ~0U)
2452 param_var->param_binding_begin = idx;
2453 param_var->param_binding_length++;
2457 idx = add_state_reference(prog->Parameters, state_tokens);
2458 if (param_var->param_binding_begin == ~0U)
2459 param_var->param_binding_begin = idx;
2460 param_var->param_binding_length++;
2468 initialize_symbol_from_const(struct gl_program *prog,
2469 struct asm_symbol *param_var,
2470 const struct asm_vector *vec)
2472 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2473 NULL, vec->count, GL_NONE, vec->data,
2476 param_var->type = at_param;
2477 param_var->param_binding_type = PROGRAM_CONSTANT;
2479 if (param_var->param_binding_begin == ~0U)
2480 param_var->param_binding_begin = idx;
2481 param_var->param_binding_length++;
2488 make_error_string(const char *fmt, ...)
2494 va_start(args, fmt);
2496 /* Call vsnprintf once to determine how large the final string is. Call it
2497 * again to do the actual formatting. from the vsnprintf manual page:
2499 * Upon successful return, these functions return the number of
2500 * characters printed (not including the trailing '\0' used to end
2501 * output to strings).
2503 length = 1 + vsnprintf(NULL, 0, fmt, args);
2505 str = _mesa_malloc(length);
2507 vsnprintf(str, length, fmt, args);
2517 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2522 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2524 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2525 _mesa_free(err_str);
2528 err_str = make_error_string("line %u, char %u: error: %s\n",
2529 locp->first_line, locp->first_column, s);
2530 _mesa_set_program_error(state->ctx, locp->position, err_str);
2533 _mesa_free(err_str);
2539 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2540 GLsizei len, struct asm_parser_state *state)
2542 struct asm_instruction *inst;
2545 GLboolean result = GL_FALSE;
2547 struct asm_symbol *sym;
2550 state->prog->Target = target;
2551 state->prog->Parameters = _mesa_new_parameter_list();
2553 /* Make a copy of the program string and force it to be NUL-terminated.
2555 strz = (GLubyte *) _mesa_malloc(len + 1);
2557 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2560 _mesa_memcpy (strz, str, len);
2563 state->prog->String = strz;
2565 state->st = _mesa_symbol_table_ctor();
2567 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2568 ? & ctx->Const.VertexProgram
2569 : & ctx->Const.FragmentProgram;
2571 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2572 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2573 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2574 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2575 state->MaxLights = ctx->Const.MaxLights;
2576 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2578 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2579 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2581 _mesa_set_program_error(ctx, -1, NULL);
2583 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2585 _mesa_program_lexer_dtor(state->scanner);
2588 if (ctx->Program.ErrorPos != -1) {
2592 if (! _mesa_layout_parameters(state)) {
2596 loc.first_column = 0;
2599 yyerror(& loc, state, "invalid PARAM usage");
2605 /* Add one instruction to store the "END" instruction.
2607 state->prog->Instructions =
2608 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2609 inst = state->inst_head;
2610 for (i = 0; i < state->prog->NumInstructions; i++) {
2611 struct asm_instruction *const temp = inst->next;
2613 state->prog->Instructions[i] = inst->Base;
2617 /* Finally, tag on an OPCODE_END instruction */
2619 const GLuint numInst = state->prog->NumInstructions;
2620 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2621 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2623 state->prog->NumInstructions++;
2625 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2626 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
2629 * Initialize native counts to logical counts. The device driver may
2630 * change them if program is translated into a hardware program.
2632 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2633 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2634 state->prog->NumNativeParameters = state->prog->NumParameters;
2635 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2636 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2641 for (inst = state->inst_head; inst != NULL; inst = temp) {
2646 state->inst_head = NULL;
2647 state->inst_tail = NULL;
2649 for (sym = state->sym; sym != NULL; sym = temp) {
2652 _mesa_free((void *) sym->name);
2657 _mesa_symbol_table_dtor(state->st);