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 set_dst_reg(struct prog_dst_register *r,
70 gl_register_file file, GLint index);
72 static void init_src_reg(struct asm_src_register *r);
74 static void set_src_reg(struct asm_src_register *r,
75 gl_register_file file, GLint index);
77 static void asm_instruction_set_operands(struct asm_instruction *inst,
78 const struct prog_dst_register *dst, const struct asm_src_register *src0,
79 const struct asm_src_register *src1, const struct asm_src_register *src2);
81 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
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_copy_ctor(
86 const struct prog_instruction *base, const struct prog_dst_register *dst,
87 const struct asm_src_register *src0, const struct asm_src_register *src1,
88 const struct asm_src_register *src2);
95 #define YYLLOC_DEFAULT(Current, Rhs, N) \
98 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
99 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
100 (Current).position = YYRHSLOC(Rhs, 1).position; \
101 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
102 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
104 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
105 (Current).last_line = (Current).first_line; \
106 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
107 (Current).last_column = (Current).first_column; \
108 (Current).position = YYRHSLOC(Rhs, 0).position \
109 + (Current).first_column; \
113 #define YYLEX_PARAM state->scanner
118 %parse-param { struct asm_parser_state *state }
120 %lex-param { void *scanner }
123 struct asm_instruction *inst;
124 struct asm_symbol *sym;
125 struct asm_symbol temp_sym;
126 struct asm_swizzle_mask swiz_mask;
127 struct asm_src_register src_reg;
128 struct prog_dst_register dst_reg;
129 struct prog_instruction temp_inst;
135 gl_state_index state[STATE_LENGTH];
137 struct asm_vector vector;
138 gl_inst_opcode opcode;
142 unsigned rgba_valid:1;
143 unsigned xyzw_valid:1;
148 %token ARBvp_10 ARBfp_10
150 /* Tokens for assembler pseudo-ops */
151 %token <integer> ADDRESS
155 %token <integer> TEMP
158 /* Tokens for instructions */
159 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
160 %token <temp_inst> ARL KIL SWZ TXD_OP
162 %token <integer> INTEGER
165 %token AMBIENT ATTENUATION
168 %token DEPTH DIFFUSE DIRECTION
169 %token EMISSION ENV EYE
170 %token FOG FOGCOORD FRAGMENT FRONT
172 %token INVERSE INVTRANS
173 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
174 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
177 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
178 %token RANGE RESULT ROW
179 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
180 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
181 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
182 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
183 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
184 %token VERTEX VTXATTRIB
187 %token <string> IDENTIFIER USED_IDENTIFIER
188 %type <string> string
189 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
193 %type <inst> instruction ALU_instruction TexInstruction
194 %type <inst> ARL_instruction VECTORop_instruction
195 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
196 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
197 %type <inst> KIL_instruction
199 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
200 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
201 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
202 %type <ext_swizzle> extSwizComp extSwizSel
203 %type <swiz_mask> optionalMask
205 %type <sym> progParamArray
206 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
207 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
209 %type <swiz_mask> addrComponent addrWriteMask
211 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
213 %type <result> resultBinding resultColBinding
214 %type <integer> optFaceType optColorType
215 %type <integer> optResultFaceType optResultColorType
217 %type <integer> optTexImageUnitNum texImageUnitNum
218 %type <integer> optTexCoordUnitNum texCoordUnitNum
219 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
220 %type <integer> texImageUnit texTarget
221 %type <integer> vtxAttribNum
223 %type <attrib> attribBinding vtxAttribItem fragAttribItem
225 %type <temp_sym> paramSingleInit paramSingleItemDecl
226 %type <integer> optArraySize
228 %type <state> stateSingleItem stateMultipleItem
229 %type <state> stateMaterialItem
230 %type <state> stateLightItem stateLightModelItem stateLightProdItem
231 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
232 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
233 %type <state> stateTexEnvItem stateDepthItem
235 %type <state> stateLModProperty
236 %type <state> stateMatrixName optMatrixRows
238 %type <integer> stateMatProperty
239 %type <integer> stateLightProperty stateSpotProperty
240 %type <integer> stateLightNumber stateLProdProperty
241 %type <integer> stateTexGenType stateTexGenCoord
242 %type <integer> stateTexEnvProperty
243 %type <integer> stateFogProperty
244 %type <integer> stateClipPlaneNum
245 %type <integer> statePointProperty
247 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
248 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
249 %type <integer> stateProgramMatNum
251 %type <integer> ambDiffSpecProperty
253 %type <state> programSingleItem progEnvParam progLocalParam
254 %type <state> programMultipleItem progEnvParams progLocalParams
256 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
257 %type <temp_sym> paramSingleItemUse
259 %type <integer> progEnvParamNum progLocalParamNum
260 %type <state> progEnvParamNums progLocalParamNums
262 %type <vector> paramConstDecl paramConstUse
263 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
264 %type <real> signedFloatConstant
265 %type <negate> optionalSign
268 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
274 program: language optionSequence statementSequence END
279 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
280 yyerror(& @1, state, "invalid fragment program header");
283 state->mode = ARB_vertex;
287 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
288 yyerror(& @1, state, "invalid vertex program header");
290 state->mode = ARB_fragment;
292 state->option.TexRect =
293 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
297 optionSequence: optionSequence option
301 option: OPTION string ';'
305 if (state->mode == ARB_vertex) {
306 valid = _mesa_ARBvp_parse_option(state, $2);
307 } else if (state->mode == ARB_fragment) {
308 valid = _mesa_ARBfp_parse_option(state, $2);
315 const char *const err_str = (state->mode == ARB_vertex)
316 ? "invalid ARB vertex program option"
317 : "invalid ARB fragment program option";
319 yyerror(& @2, state, err_str);
325 statementSequence: statementSequence statement
329 statement: instruction ';'
332 if (state->inst_tail == NULL) {
333 state->inst_head = $1;
335 state->inst_tail->next = $1;
338 state->inst_tail = $1;
341 state->prog->NumInstructions++;
344 | namingStatement ';'
347 instruction: ALU_instruction
350 state->prog->NumAluInstructions++;
355 state->prog->NumTexInstructions++;
359 ALU_instruction: ARL_instruction
360 | VECTORop_instruction
361 | SCALARop_instruction
362 | BINSCop_instruction
368 TexInstruction: SAMPLE_instruction
373 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
375 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
379 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
381 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
385 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
387 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
391 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
393 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
398 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
400 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
404 TRIop_instruction: TRI_OP maskedDstReg ','
405 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
407 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
411 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
413 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
415 const GLbitfield tex_mask = (1U << $6);
416 GLbitfield shadow_tex = 0;
417 GLbitfield target_mask = 0;
420 $$->Base.TexSrcUnit = $6;
423 shadow_tex = tex_mask;
425 $$->Base.TexSrcTarget = -$8;
426 $$->Base.TexShadow = 1;
428 $$->Base.TexSrcTarget = $8;
431 target_mask = (1U << $$->Base.TexSrcTarget);
433 /* If this texture unit was previously accessed and that access
434 * had a different texture target, generate an error.
436 * If this texture unit was previously accessed and that access
437 * had a different shadow mode, generate an error.
439 if ((state->prog->TexturesUsed[$6] != 0)
440 && ((state->prog->TexturesUsed[$6] != target_mask)
441 || ((state->prog->ShadowSamplers & tex_mask)
444 "multiple targets used on one texture image unit");
449 state->prog->TexturesUsed[$6] |= target_mask;
450 state->prog->ShadowSamplers |= shadow_tex;
455 KIL_instruction: KIL swizzleSrcReg
457 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
458 state->fragment.UsesKill = 1;
462 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
463 $$->Base.DstReg.CondMask = $2.CondMask;
464 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
465 $$->Base.DstReg.CondSrc = $2.CondSrc;
466 state->fragment.UsesKill = 1;
470 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
472 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
474 const GLbitfield tex_mask = (1U << $10);
475 GLbitfield shadow_tex = 0;
476 GLbitfield target_mask = 0;
479 $$->Base.TexSrcUnit = $10;
482 shadow_tex = tex_mask;
484 $$->Base.TexSrcTarget = -$12;
485 $$->Base.TexShadow = 1;
487 $$->Base.TexSrcTarget = $12;
490 target_mask = (1U << $$->Base.TexSrcTarget);
492 /* If this texture unit was previously accessed and that access
493 * had a different texture target, generate an error.
495 * If this texture unit was previously accessed and that access
496 * had a different shadow mode, generate an error.
498 if ((state->prog->TexturesUsed[$10] != 0)
499 && ((state->prog->TexturesUsed[$10] != target_mask)
500 || ((state->prog->ShadowSamplers & tex_mask)
502 yyerror(& @12, state,
503 "multiple targets used on one texture image unit");
508 state->prog->TexturesUsed[$10] |= target_mask;
509 state->prog->ShadowSamplers |= shadow_tex;
514 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
520 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
521 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
522 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
523 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
524 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
525 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
526 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
527 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
528 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
529 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
530 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
531 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
534 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
536 /* FIXME: Is this correct? Should the extenedSwizzle be applied
537 * FIXME: to the existing swizzle?
539 $4.Base.Swizzle = $6.swizzle;
540 $4.Base.Negate = $6.mask;
542 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
546 scalarSrcReg: optionalSign scalarUse
551 $$.Base.Negate = ~$$.Base.Negate;
554 | optionalSign '|' scalarUse '|'
558 if (!state->option.NV_fragment) {
559 yyerror(& @2, state, "unexpected character '|'");
564 $$.Base.Negate = ~$$.Base.Negate;
571 scalarUse: srcReg scalarSuffix
575 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
578 | paramConstScalarUse
580 struct asm_symbol temp_sym;
582 if (!state->option.NV_fragment) {
583 yyerror(& @1, state, "expected scalar suffix");
587 memset(& temp_sym, 0, sizeof(temp_sym));
588 temp_sym.param_binding_begin = ~0;
589 initialize_symbol_from_const(state->prog, & temp_sym, & $1);
591 set_src_reg(& $$, PROGRAM_CONSTANT, temp_sym.param_binding_begin);
595 swizzleSrcReg: optionalSign srcReg swizzleSuffix
600 $$.Base.Negate = ~$$.Base.Negate;
603 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
606 | optionalSign '|' srcReg swizzleSuffix '|'
610 if (!state->option.NV_fragment) {
611 yyerror(& @2, state, "unexpected character '|'");
616 $$.Base.Negate = ~$$.Base.Negate;
620 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
626 maskedDstReg: dstReg optionalMask optionalCcMask
629 $$.WriteMask = $2.mask;
630 $$.CondMask = $3.CondMask;
631 $$.CondSwizzle = $3.CondSwizzle;
632 $$.CondSrc = $3.CondSrc;
634 if ($$.File == PROGRAM_OUTPUT) {
635 /* Technically speaking, this should check that it is in
636 * vertex program mode. However, PositionInvariant can never be
637 * set in fragment program mode, so it is somewhat irrelevant.
639 if (state->option.PositionInvariant
640 && ($$.Index == VERT_RESULT_HPOS)) {
641 yyerror(& @1, state, "position-invariant programs cannot "
646 state->prog->OutputsWritten |= (1U << $$.Index);
651 maskedAddrReg: addrReg addrWriteMask
653 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
654 $$.WriteMask = $2.mask;
658 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
660 const unsigned xyzw_valid =
662 | ($3.xyzw_valid << 1)
663 | ($5.xyzw_valid << 2)
664 | ($7.xyzw_valid << 3);
665 const unsigned rgba_valid =
667 | ($3.rgba_valid << 1)
668 | ($5.rgba_valid << 2)
669 | ($7.rgba_valid << 3);
671 /* All of the swizzle components have to be valid in either RGBA
672 * or XYZW. Note that 0 and 1 are valid in both, so both masks
673 * can have some bits set.
675 * We somewhat deviate from the spec here. It would be really hard
676 * to figure out which component is the error, and there probably
677 * isn't a lot of benefit.
679 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
680 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
685 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
686 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
691 extSwizComp: optionalSign extSwizSel
694 $$.negate = ($1) ? 1 : 0;
700 if (($1 != 0) && ($1 != 1)) {
701 yyerror(& @1, state, "invalid extended swizzle selector");
705 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
707 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
717 if (strlen($1) > 1) {
718 yyerror(& @1, state, "invalid extended swizzle selector");
761 yyerror(& @1, state, "invalid extended swizzle selector");
768 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
770 struct asm_symbol *const s = (struct asm_symbol *)
771 _mesa_symbol_table_find_symbol(state->st, 0, $1);
776 yyerror(& @1, state, "invalid operand variable");
778 } else if ((s->type != at_param) && (s->type != at_temp)
779 && (s->type != at_attrib)) {
780 yyerror(& @1, state, "invalid operand variable");
782 } else if ((s->type == at_param) && s->param_is_array) {
783 yyerror(& @1, state, "non-array access to array PARAM");
790 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
793 set_src_reg(& $$, s->param_binding_type, s->param_binding_begin);
796 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
797 state->prog->InputsRead |= (1U << $$.Base.Index);
799 if (!validate_inputs(& @1, state)) {
811 set_src_reg(& $$, PROGRAM_INPUT, $1);
812 state->prog->InputsRead |= (1U << $$.Base.Index);
814 if (!validate_inputs(& @1, state)) {
818 | progParamArray '[' progParamArrayMem ']'
820 if (! $3.Base.RelAddr
821 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
822 yyerror(& @3, state, "out of bounds array access");
827 $$.Base.File = $1->param_binding_type;
829 if ($3.Base.RelAddr) {
830 $1->param_accessed_indirectly = 1;
833 $$.Base.Index = $3.Base.Index;
836 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
841 gl_register_file file = ($1.name != NULL)
842 ? $1.param_binding_type
844 set_src_reg(& $$, file, $1.param_binding_begin);
848 dstReg: resultBinding
850 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
852 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
854 struct asm_symbol *const s = (struct asm_symbol *)
855 _mesa_symbol_table_find_symbol(state->st, 0, $1);
860 yyerror(& @1, state, "invalid operand variable");
862 } else if ((s->type != at_output) && (s->type != at_temp)) {
863 yyerror(& @1, state, "invalid operand variable");
869 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
872 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
875 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
881 progParamArray: USED_IDENTIFIER
883 struct asm_symbol *const s = (struct asm_symbol *)
884 _mesa_symbol_table_find_symbol(state->st, 0, $1);
889 yyerror(& @1, state, "invalid operand variable");
891 } else if ((s->type != at_param) || !s->param_is_array) {
892 yyerror(& @1, state, "array access to non-PARAM variable");
900 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
902 progParamArrayAbs: INTEGER
909 progParamArrayRel: addrReg addrComponent addrRegRelOffset
911 /* FINISHME: Add support for multiple address registers.
913 /* FINISHME: Add support for 4-component address registers.
921 addrRegRelOffset: { $$ = 0; }
922 | '+' addrRegPosOffset { $$ = $2; }
923 | '-' addrRegNegOffset { $$ = -$2; }
926 addrRegPosOffset: INTEGER
928 if (($1 < 0) || ($1 > 63)) {
930 _mesa_snprintf(s, sizeof(s),
931 "relative address offset too large (%d)", $1);
932 yyerror(& @1, state, s);
940 addrRegNegOffset: INTEGER
942 if (($1 < 0) || ($1 > 64)) {
944 _mesa_snprintf(s, sizeof(s),
945 "relative address offset too large (%d)", $1);
946 yyerror(& @1, state, s);
954 addrReg: USED_IDENTIFIER
956 struct asm_symbol *const s = (struct asm_symbol *)
957 _mesa_symbol_table_find_symbol(state->st, 0, $1);
962 yyerror(& @1, state, "invalid array member");
964 } else if (s->type != at_address) {
966 "invalid variable for indexed array access");
976 if ($1.mask != WRITEMASK_X) {
977 yyerror(& @1, state, "invalid address component selector");
987 if ($1.mask != WRITEMASK_X) {
989 "address register write mask must be \".x\"");
1002 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1005 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1006 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1009 optionalCcMask: '(' ccTest ')'
1019 $$.CondMask = COND_TR;
1020 $$.CondSwizzle = SWIZZLE_NOOP;
1025 ccTest: ccMaskRule swizzleSuffix
1028 $$.CondSwizzle = $2.swizzle;
1032 ccTest2: ccMaskRule2 swizzleSuffix
1035 $$.CondSwizzle = $2.swizzle;
1039 ccMaskRule: IDENTIFIER
1041 const int cond = _mesa_parse_cc($1);
1042 if ((cond == 0) || ($1[2] != '\0')) {
1043 char *const err_str =
1044 make_error_string("invalid condition code \"%s\"", $1);
1046 yyerror(& @1, state, (err_str != NULL)
1047 ? err_str : "invalid condition code");
1049 if (err_str != NULL) {
1050 _mesa_free(err_str);
1057 $$.CondSwizzle = SWIZZLE_NOOP;
1062 ccMaskRule2: USED_IDENTIFIER
1064 const int cond = _mesa_parse_cc($1);
1065 if ((cond == 0) || ($1[2] != '\0')) {
1066 char *const err_str =
1067 make_error_string("invalid condition code \"%s\"", $1);
1069 yyerror(& @1, state, (err_str != NULL)
1070 ? err_str : "invalid condition code");
1072 if (err_str != NULL) {
1073 _mesa_free(err_str);
1080 $$.CondSwizzle = SWIZZLE_NOOP;
1085 namingStatement: ATTRIB_statement
1093 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1095 struct asm_symbol *const s =
1096 declare_variable(state, $2, at_attrib, & @2);
1102 s->attrib_binding = $4;
1103 state->InputsBound |= (1U << s->attrib_binding);
1105 if (!validate_inputs(& @4, state)) {
1112 attribBinding: VERTEX vtxAttribItem
1116 | FRAGMENT fragAttribItem
1122 vtxAttribItem: POSITION
1124 $$ = VERT_ATTRIB_POS;
1126 | WEIGHT vtxOptWeightNum
1128 $$ = VERT_ATTRIB_WEIGHT;
1132 $$ = VERT_ATTRIB_NORMAL;
1134 | COLOR optColorType
1136 if (!state->ctx->Extensions.EXT_secondary_color) {
1137 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1141 $$ = VERT_ATTRIB_COLOR0 + $2;
1145 if (!state->ctx->Extensions.EXT_fog_coord) {
1146 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1150 $$ = VERT_ATTRIB_FOG;
1152 | TEXCOORD optTexCoordUnitNum
1154 $$ = VERT_ATTRIB_TEX0 + $2;
1156 | MATRIXINDEX '[' vtxWeightNum ']'
1158 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1161 | VTXATTRIB '[' vtxAttribNum ']'
1163 $$ = VERT_ATTRIB_GENERIC0 + $3;
1167 vtxAttribNum: INTEGER
1169 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1170 yyerror(& @1, state, "invalid vertex attribute reference");
1178 vtxOptWeightNum: | '[' vtxWeightNum ']';
1179 vtxWeightNum: INTEGER;
1181 fragAttribItem: POSITION
1183 $$ = FRAG_ATTRIB_WPOS;
1185 | COLOR optColorType
1187 $$ = FRAG_ATTRIB_COL0 + $2;
1191 $$ = FRAG_ATTRIB_FOGC;
1193 | TEXCOORD optTexCoordUnitNum
1195 $$ = FRAG_ATTRIB_TEX0 + $2;
1199 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1201 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1203 struct asm_symbol *const s =
1204 declare_variable(state, $2, at_param, & @2);
1210 s->param_binding_type = $3.param_binding_type;
1211 s->param_binding_begin = $3.param_binding_begin;
1212 s->param_binding_length = $3.param_binding_length;
1213 s->param_is_array = 0;
1218 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1220 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1222 yyerror(& @4, state,
1223 "parameter array size and number of bindings must match");
1226 struct asm_symbol *const s =
1227 declare_variable(state, $2, $6.type, & @2);
1233 s->param_binding_type = $6.param_binding_type;
1234 s->param_binding_begin = $6.param_binding_begin;
1235 s->param_binding_length = $6.param_binding_length;
1236 s->param_is_array = 1;
1248 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1249 yyerror(& @1, state, "invalid parameter array size");
1257 paramSingleInit: '=' paramSingleItemDecl
1263 paramMultipleInit: '=' '{' paramMultInitList '}'
1269 paramMultInitList: paramMultipleItem
1270 | paramMultInitList ',' paramMultipleItem
1272 $1.param_binding_length += $3.param_binding_length;
1277 paramSingleItemDecl: stateSingleItem
1279 memset(& $$, 0, sizeof($$));
1280 $$.param_binding_begin = ~0;
1281 initialize_symbol_from_state(state->prog, & $$, $1);
1285 memset(& $$, 0, sizeof($$));
1286 $$.param_binding_begin = ~0;
1287 initialize_symbol_from_param(state->prog, & $$, $1);
1291 memset(& $$, 0, sizeof($$));
1292 $$.param_binding_begin = ~0;
1293 initialize_symbol_from_const(state->prog, & $$, & $1);
1297 paramSingleItemUse: stateSingleItem
1299 memset(& $$, 0, sizeof($$));
1300 $$.param_binding_begin = ~0;
1301 initialize_symbol_from_state(state->prog, & $$, $1);
1305 memset(& $$, 0, sizeof($$));
1306 $$.param_binding_begin = ~0;
1307 initialize_symbol_from_param(state->prog, & $$, $1);
1311 memset(& $$, 0, sizeof($$));
1312 $$.param_binding_begin = ~0;
1313 initialize_symbol_from_const(state->prog, & $$, & $1);
1317 paramMultipleItem: stateMultipleItem
1319 memset(& $$, 0, sizeof($$));
1320 $$.param_binding_begin = ~0;
1321 initialize_symbol_from_state(state->prog, & $$, $1);
1323 | programMultipleItem
1325 memset(& $$, 0, sizeof($$));
1326 $$.param_binding_begin = ~0;
1327 initialize_symbol_from_param(state->prog, & $$, $1);
1331 memset(& $$, 0, sizeof($$));
1332 $$.param_binding_begin = ~0;
1333 initialize_symbol_from_const(state->prog, & $$, & $1);
1337 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1338 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1341 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1342 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1343 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1344 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1345 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1346 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1347 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1348 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1349 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1350 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1351 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1354 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1356 memset($$, 0, sizeof($$));
1357 $$[0] = STATE_MATERIAL;
1363 stateMatProperty: ambDiffSpecProperty
1369 $$ = STATE_EMISSION;
1373 $$ = STATE_SHININESS;
1377 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1379 memset($$, 0, sizeof($$));
1380 $$[0] = STATE_LIGHT;
1386 stateLightProperty: ambDiffSpecProperty
1392 $$ = STATE_POSITION;
1396 if (!state->ctx->Extensions.EXT_point_parameters) {
1397 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1401 $$ = STATE_ATTENUATION;
1403 | SPOT stateSpotProperty
1409 $$ = STATE_HALF_VECTOR;
1413 stateSpotProperty: DIRECTION
1415 $$ = STATE_SPOT_DIRECTION;
1419 stateLightModelItem: LIGHTMODEL stateLModProperty
1426 stateLModProperty: AMBIENT
1428 memset($$, 0, sizeof($$));
1429 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1431 | optFaceType SCENECOLOR
1433 memset($$, 0, sizeof($$));
1434 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1439 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1441 memset($$, 0, sizeof($$));
1442 $$[0] = STATE_LIGHTPROD;
1449 stateLProdProperty: ambDiffSpecProperty;
1451 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1453 memset($$, 0, sizeof($$));
1459 stateTexEnvProperty: COLOR
1461 $$ = STATE_TEXENV_COLOR;
1465 ambDiffSpecProperty: AMBIENT
1475 $$ = STATE_SPECULAR;
1479 stateLightNumber: INTEGER
1481 if ((unsigned) $1 >= state->MaxLights) {
1482 yyerror(& @1, state, "invalid light selector");
1490 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1492 memset($$, 0, sizeof($$));
1493 $$[0] = STATE_TEXGEN;
1499 stateTexGenType: EYE
1501 $$ = STATE_TEXGEN_EYE_S;
1505 $$ = STATE_TEXGEN_OBJECT_S;
1508 stateTexGenCoord: TEXGEN_S
1510 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1514 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1518 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1522 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1526 stateFogItem: FOG stateFogProperty
1528 memset($$, 0, sizeof($$));
1533 stateFogProperty: COLOR
1535 $$ = STATE_FOG_COLOR;
1539 $$ = STATE_FOG_PARAMS;
1543 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1545 memset($$, 0, sizeof($$));
1546 $$[0] = STATE_CLIPPLANE;
1551 stateClipPlaneNum: INTEGER
1553 if ((unsigned) $1 >= state->MaxClipPlanes) {
1554 yyerror(& @1, state, "invalid clip plane selector");
1562 statePointItem: POINT_TOK statePointProperty
1564 memset($$, 0, sizeof($$));
1569 statePointProperty: SIZE_TOK
1571 $$ = STATE_POINT_SIZE;
1575 $$ = STATE_POINT_ATTENUATION;
1579 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1589 stateMatrixRows: stateMatrixItem optMatrixRows
1604 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1606 /* It seems logical that the matrix row range specifier would have
1607 * to specify a range or more than one row (i.e., $5 > $3).
1608 * However, the ARB_vertex_program spec says "a program will fail
1609 * to load if <a> is greater than <b>." This means that $3 == $5
1613 yyerror(& @3, state, "invalid matrix row range");
1622 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1630 stateOptMatModifier:
1640 stateMatModifier: INVERSE
1642 $$ = STATE_MATRIX_INVERSE;
1646 $$ = STATE_MATRIX_TRANSPOSE;
1650 $$ = STATE_MATRIX_INVTRANS;
1654 stateMatrixRowNum: INTEGER
1657 yyerror(& @1, state, "invalid matrix row reference");
1665 stateMatrixName: MODELVIEW stateOptModMatNum
1667 $$[0] = STATE_MODELVIEW_MATRIX;
1672 $$[0] = STATE_PROJECTION_MATRIX;
1677 $$[0] = STATE_MVP_MATRIX;
1680 | TEXTURE optTexCoordUnitNum
1682 $$[0] = STATE_TEXTURE_MATRIX;
1685 | PALETTE '[' statePaletteMatNum ']'
1687 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1690 | MAT_PROGRAM '[' stateProgramMatNum ']'
1692 $$[0] = STATE_PROGRAM_MATRIX;
1701 | '[' stateModMatNum ']'
1706 stateModMatNum: INTEGER
1708 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1712 yyerror(& @1, state, "invalid modelview matrix index");
1719 statePaletteMatNum: INTEGER
1721 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1722 * through here. The error will be generated later.
1727 stateProgramMatNum: INTEGER
1729 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1730 yyerror(& @1, state, "invalid program matrix selector");
1738 stateDepthItem: DEPTH RANGE
1740 memset($$, 0, sizeof($$));
1741 $$[0] = STATE_DEPTH_RANGE;
1746 programSingleItem: progEnvParam | progLocalParam;
1748 programMultipleItem: progEnvParams | progLocalParams;
1750 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1752 memset($$, 0, sizeof($$));
1753 $$[0] = state->state_param_enum;
1760 progEnvParamNums: progEnvParamNum
1765 | progEnvParamNum DOT_DOT progEnvParamNum
1772 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1774 memset($$, 0, sizeof($$));
1775 $$[0] = state->state_param_enum;
1782 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1784 memset($$, 0, sizeof($$));
1785 $$[0] = state->state_param_enum;
1786 $$[1] = STATE_LOCAL;
1791 progLocalParamNums: progLocalParamNum
1796 | progLocalParamNum DOT_DOT progLocalParamNum
1803 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1805 memset($$, 0, sizeof($$));
1806 $$[0] = state->state_param_enum;
1807 $$[1] = STATE_LOCAL;
1813 progEnvParamNum: INTEGER
1815 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1816 yyerror(& @1, state, "invalid environment parameter reference");
1823 progLocalParamNum: INTEGER
1825 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1826 yyerror(& @1, state, "invalid local parameter reference");
1835 paramConstDecl: paramConstScalarDecl | paramConstVector;
1836 paramConstUse: paramConstScalarUse | paramConstVector;
1838 paramConstScalarDecl: signedFloatConstant
1848 paramConstScalarUse: REAL
1859 $$.data[0] = (float) $1;
1860 $$.data[1] = (float) $1;
1861 $$.data[2] = (float) $1;
1862 $$.data[3] = (float) $1;
1866 paramConstVector: '{' signedFloatConstant '}'
1874 | '{' signedFloatConstant ',' signedFloatConstant '}'
1882 | '{' signedFloatConstant ',' signedFloatConstant ','
1883 signedFloatConstant '}'
1891 | '{' signedFloatConstant ',' signedFloatConstant ','
1892 signedFloatConstant ',' signedFloatConstant '}'
1902 signedFloatConstant: optionalSign REAL
1904 $$ = ($1) ? -$2 : $2;
1906 | optionalSign INTEGER
1908 $$ = (float)(($1) ? -$2 : $2);
1912 optionalSign: '+' { $$ = FALSE; }
1913 | '-' { $$ = TRUE; }
1917 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1922 /* NV_fragment_program_option defines the size qualifiers in a
1923 * fairly broken way. "SHORT" or "LONG" can optionally be used
1924 * before TEMP or OUTPUT. However, neither is a reserved word!
1925 * This means that we have to parse it as an identifier, then check
1926 * to make sure it's one of the valid values. *sigh*
1928 * In addition, the grammar in the extension spec does *not* allow
1929 * the size specifier to be optional, but all known implementations
1932 if (!state->option.NV_fragment) {
1933 yyerror(& @1, state, "unexpected IDENTIFIER");
1937 if (strcmp("SHORT", $1) == 0) {
1938 } else if (strcmp("LONG", $1) == 0) {
1940 char *const err_str =
1941 make_error_string("invalid storage size specifier \"%s\"",
1944 yyerror(& @1, state, (err_str != NULL)
1945 ? err_str : "invalid storage size specifier");
1947 if (err_str != NULL) {
1948 _mesa_free(err_str);
1959 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1962 varNameList: varNameList ',' IDENTIFIER
1964 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1971 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1978 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1980 struct asm_symbol *const s =
1981 declare_variable(state, $3, at_output, & @3);
1987 s->output_binding = $5;
1992 resultBinding: RESULT POSITION
1994 if (state->mode == ARB_vertex) {
1995 $$ = VERT_RESULT_HPOS;
1997 yyerror(& @2, state, "invalid program result name");
2003 if (state->mode == ARB_vertex) {
2004 $$ = VERT_RESULT_FOGC;
2006 yyerror(& @2, state, "invalid program result name");
2010 | RESULT resultColBinding
2016 if (state->mode == ARB_vertex) {
2017 $$ = VERT_RESULT_PSIZ;
2019 yyerror(& @2, state, "invalid program result name");
2023 | RESULT TEXCOORD optTexCoordUnitNum
2025 if (state->mode == ARB_vertex) {
2026 $$ = VERT_RESULT_TEX0 + $3;
2028 yyerror(& @2, state, "invalid program result name");
2034 if (state->mode == ARB_fragment) {
2035 $$ = FRAG_RESULT_DEPTH;
2037 yyerror(& @2, state, "invalid program result name");
2043 resultColBinding: COLOR optResultFaceType optResultColorType
2051 $$ = (state->mode == ARB_vertex)
2053 : FRAG_RESULT_COLOR;
2057 if (state->mode == ARB_vertex) {
2058 $$ = VERT_RESULT_COL0;
2060 yyerror(& @1, state, "invalid program result name");
2066 if (state->mode == ARB_vertex) {
2067 $$ = VERT_RESULT_BFC0;
2069 yyerror(& @1, state, "invalid program result name");
2081 if (state->mode == ARB_vertex) {
2084 yyerror(& @1, state, "invalid program result name");
2090 if (state->mode == ARB_vertex) {
2093 yyerror(& @1, state, "invalid program result name");
2099 optFaceType: { $$ = 0; }
2104 optColorType: { $$ = 0; }
2105 | PRIMARY { $$ = 0; }
2106 | SECONDARY { $$ = 1; }
2109 optTexCoordUnitNum: { $$ = 0; }
2110 | '[' texCoordUnitNum ']' { $$ = $2; }
2113 optTexImageUnitNum: { $$ = 0; }
2114 | '[' texImageUnitNum ']' { $$ = $2; }
2117 optLegacyTexUnitNum: { $$ = 0; }
2118 | '[' legacyTexUnitNum ']' { $$ = $2; }
2121 texCoordUnitNum: INTEGER
2123 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2124 yyerror(& @1, state, "invalid texture coordinate unit selector");
2132 texImageUnitNum: INTEGER
2134 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2135 yyerror(& @1, state, "invalid texture image unit selector");
2143 legacyTexUnitNum: INTEGER
2145 if ((unsigned) $1 >= state->MaxTextureUnits) {
2146 yyerror(& @1, state, "invalid texture unit selector");
2154 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2156 struct asm_symbol *exist = (struct asm_symbol *)
2157 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2158 struct asm_symbol *target = (struct asm_symbol *)
2159 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2163 if (exist != NULL) {
2165 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2167 yyerror(& @2, state, m);
2169 } else if (target == NULL) {
2171 yyerror(& @4, state,
2172 "undefined variable binding in ALIAS statement");
2175 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2187 asm_instruction_set_operands(struct asm_instruction *inst,
2188 const struct prog_dst_register *dst,
2189 const struct asm_src_register *src0,
2190 const struct asm_src_register *src1,
2191 const struct asm_src_register *src2)
2193 /* In the core ARB extensions only the KIL instruction doesn't have a
2194 * destination register.
2197 init_dst_reg(& inst->Base.DstReg);
2199 inst->Base.DstReg = *dst;
2202 /* The only instruction that doesn't have any source registers is the
2203 * condition-code based KIL instruction added by NV_fragment_program_option.
2206 inst->Base.SrcReg[0] = src0->Base;
2207 inst->SrcReg[0] = *src0;
2209 init_src_reg(& inst->SrcReg[0]);
2213 inst->Base.SrcReg[1] = src1->Base;
2214 inst->SrcReg[1] = *src1;
2216 init_src_reg(& inst->SrcReg[1]);
2220 inst->Base.SrcReg[2] = src2->Base;
2221 inst->SrcReg[2] = *src2;
2223 init_src_reg(& inst->SrcReg[2]);
2228 struct asm_instruction *
2229 asm_instruction_ctor(gl_inst_opcode op,
2230 const struct prog_dst_register *dst,
2231 const struct asm_src_register *src0,
2232 const struct asm_src_register *src1,
2233 const struct asm_src_register *src2)
2235 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2238 _mesa_init_instructions(& inst->Base, 1);
2239 inst->Base.Opcode = op;
2241 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2248 struct asm_instruction *
2249 asm_instruction_copy_ctor(const struct prog_instruction *base,
2250 const struct prog_dst_register *dst,
2251 const struct asm_src_register *src0,
2252 const struct asm_src_register *src1,
2253 const struct asm_src_register *src2)
2255 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2258 _mesa_init_instructions(& inst->Base, 1);
2259 inst->Base.Opcode = base->Opcode;
2260 inst->Base.CondUpdate = base->CondUpdate;
2261 inst->Base.CondDst = base->CondDst;
2262 inst->Base.SaturateMode = base->SaturateMode;
2263 inst->Base.Precision = base->Precision;
2265 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2273 init_dst_reg(struct prog_dst_register *r)
2275 memset(r, 0, sizeof(*r));
2276 r->File = PROGRAM_UNDEFINED;
2277 r->WriteMask = WRITEMASK_XYZW;
2278 r->CondMask = COND_TR;
2279 r->CondSwizzle = SWIZZLE_NOOP;
2283 /** Like init_dst_reg() but set the File and Index fields. */
2285 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2287 const GLint maxIndex = 1 << INST_INDEX_BITS;
2288 const GLint minIndex = 0;
2289 ASSERT(index >= minIndex);
2290 ASSERT(index <= maxIndex);
2291 ASSERT(file == PROGRAM_TEMPORARY ||
2292 file == PROGRAM_ADDRESS ||
2293 file == PROGRAM_OUTPUT);
2294 memset(r, 0, sizeof(*r));
2297 r->WriteMask = WRITEMASK_XYZW;
2298 r->CondMask = COND_TR;
2299 r->CondSwizzle = SWIZZLE_NOOP;
2304 init_src_reg(struct asm_src_register *r)
2306 memset(r, 0, sizeof(*r));
2307 r->Base.File = PROGRAM_UNDEFINED;
2308 r->Base.Swizzle = SWIZZLE_NOOP;
2313 /** Like init_src_reg() but set the File and Index fields. */
2315 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2317 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2318 const GLint minIndex = -(1 << INST_INDEX_BITS);
2319 ASSERT(index >= minIndex);
2320 ASSERT(index <= maxIndex);
2321 ASSERT(file < PROGRAM_FILE_MAX);
2322 memset(r, 0, sizeof(*r));
2323 r->Base.File = file;
2324 r->Base.Index = index;
2325 r->Base.Swizzle = SWIZZLE_NOOP;
2331 * Validate the set of inputs used by a program
2333 * Validates that legal sets of inputs are used by the program. In this case
2334 * "used" included both reading the input or binding the input to a name using
2335 * the \c ATTRIB command.
2338 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2341 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2343 const int inputs = state->prog->InputsRead | state->InputsBound;
2345 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
2346 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2355 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2356 struct YYLTYPE *locp)
2358 struct asm_symbol *s = NULL;
2359 struct asm_symbol *exist = (struct asm_symbol *)
2360 _mesa_symbol_table_find_symbol(state->st, 0, name);
2363 if (exist != NULL) {
2364 yyerror(locp, state, "redeclared identifier");
2366 s = calloc(1, sizeof(struct asm_symbol));
2372 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2373 yyerror(locp, state, "too many temporaries declared");
2378 s->temp_binding = state->prog->NumTemporaries;
2379 state->prog->NumTemporaries++;
2383 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2384 yyerror(locp, state, "too many address registers declared");
2389 /* FINISHME: Add support for multiple address registers.
2391 state->prog->NumAddressRegs++;
2398 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2399 s->next = state->sym;
2407 int add_state_reference(struct gl_program_parameter_list *param_list,
2408 const gl_state_index tokens[STATE_LENGTH])
2410 const GLuint size = 4; /* XXX fix */
2414 name = _mesa_program_state_string(tokens);
2415 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2416 size, GL_NONE, NULL, tokens, 0x0);
2417 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2419 /* free name string here since we duplicated it in add_parameter() */
2427 initialize_symbol_from_state(struct gl_program *prog,
2428 struct asm_symbol *param_var,
2429 const gl_state_index tokens[STATE_LENGTH])
2432 gl_state_index state_tokens[STATE_LENGTH];
2435 memcpy(state_tokens, tokens, sizeof(state_tokens));
2437 param_var->type = at_param;
2438 param_var->param_binding_type = PROGRAM_STATE_VAR;
2440 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2441 * unroll it and call add_state_reference() for each row
2443 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2444 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2445 state_tokens[0] == STATE_MVP_MATRIX ||
2446 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2447 state_tokens[0] == STATE_PROGRAM_MATRIX)
2448 && (state_tokens[2] != state_tokens[3])) {
2450 const int first_row = state_tokens[2];
2451 const int last_row = state_tokens[3];
2453 for (row = first_row; row <= last_row; row++) {
2454 state_tokens[2] = state_tokens[3] = row;
2456 idx = add_state_reference(prog->Parameters, state_tokens);
2457 if (param_var->param_binding_begin == ~0U)
2458 param_var->param_binding_begin = idx;
2459 param_var->param_binding_length++;
2463 idx = add_state_reference(prog->Parameters, state_tokens);
2464 if (param_var->param_binding_begin == ~0U)
2465 param_var->param_binding_begin = idx;
2466 param_var->param_binding_length++;
2474 initialize_symbol_from_param(struct gl_program *prog,
2475 struct asm_symbol *param_var,
2476 const gl_state_index tokens[STATE_LENGTH])
2479 gl_state_index state_tokens[STATE_LENGTH];
2482 memcpy(state_tokens, tokens, sizeof(state_tokens));
2484 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2485 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2486 assert((state_tokens[1] == STATE_ENV)
2487 || (state_tokens[1] == STATE_LOCAL));
2489 param_var->type = at_param;
2490 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2491 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
2493 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2494 * we need to unroll it and call add_state_reference() for each row
2496 if (state_tokens[2] != state_tokens[3]) {
2498 const int first_row = state_tokens[2];
2499 const int last_row = state_tokens[3];
2501 for (row = first_row; row <= last_row; row++) {
2502 state_tokens[2] = state_tokens[3] = row;
2504 idx = add_state_reference(prog->Parameters, state_tokens);
2505 if (param_var->param_binding_begin == ~0U)
2506 param_var->param_binding_begin = idx;
2507 param_var->param_binding_length++;
2511 idx = add_state_reference(prog->Parameters, state_tokens);
2512 if (param_var->param_binding_begin == ~0U)
2513 param_var->param_binding_begin = idx;
2514 param_var->param_binding_length++;
2522 initialize_symbol_from_const(struct gl_program *prog,
2523 struct asm_symbol *param_var,
2524 const struct asm_vector *vec)
2526 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2527 NULL, vec->count, GL_NONE, vec->data,
2530 param_var->type = at_param;
2531 param_var->param_binding_type = PROGRAM_CONSTANT;
2533 if (param_var->param_binding_begin == ~0U)
2534 param_var->param_binding_begin = idx;
2535 param_var->param_binding_length++;
2542 make_error_string(const char *fmt, ...)
2548 va_start(args, fmt);
2550 /* Call vsnprintf once to determine how large the final string is. Call it
2551 * again to do the actual formatting. from the vsnprintf manual page:
2553 * Upon successful return, these functions return the number of
2554 * characters printed (not including the trailing '\0' used to end
2555 * output to strings).
2557 length = 1 + vsnprintf(NULL, 0, fmt, args);
2559 str = _mesa_malloc(length);
2561 vsnprintf(str, length, fmt, args);
2571 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2576 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2578 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2579 _mesa_free(err_str);
2582 err_str = make_error_string("line %u, char %u: error: %s\n",
2583 locp->first_line, locp->first_column, s);
2584 _mesa_set_program_error(state->ctx, locp->position, err_str);
2587 _mesa_free(err_str);
2593 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2594 GLsizei len, struct asm_parser_state *state)
2596 struct asm_instruction *inst;
2599 GLboolean result = GL_FALSE;
2601 struct asm_symbol *sym;
2604 state->prog->Target = target;
2605 state->prog->Parameters = _mesa_new_parameter_list();
2607 /* Make a copy of the program string and force it to be NUL-terminated.
2609 strz = (GLubyte *) _mesa_malloc(len + 1);
2611 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2614 _mesa_memcpy (strz, str, len);
2617 state->prog->String = strz;
2619 state->st = _mesa_symbol_table_ctor();
2621 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2622 ? & ctx->Const.VertexProgram
2623 : & ctx->Const.FragmentProgram;
2625 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2626 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2627 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2628 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2629 state->MaxLights = ctx->Const.MaxLights;
2630 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2632 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2633 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2635 _mesa_set_program_error(ctx, -1, NULL);
2637 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2639 _mesa_program_lexer_dtor(state->scanner);
2642 if (ctx->Program.ErrorPos != -1) {
2646 if (! _mesa_layout_parameters(state)) {
2650 loc.first_column = 0;
2653 yyerror(& loc, state, "invalid PARAM usage");
2659 /* Add one instruction to store the "END" instruction.
2661 state->prog->Instructions =
2662 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2663 inst = state->inst_head;
2664 for (i = 0; i < state->prog->NumInstructions; i++) {
2665 struct asm_instruction *const temp = inst->next;
2667 state->prog->Instructions[i] = inst->Base;
2671 /* Finally, tag on an OPCODE_END instruction */
2673 const GLuint numInst = state->prog->NumInstructions;
2674 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2675 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2677 state->prog->NumInstructions++;
2679 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2680 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
2683 * Initialize native counts to logical counts. The device driver may
2684 * change them if program is translated into a hardware program.
2686 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2687 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2688 state->prog->NumNativeParameters = state->prog->NumParameters;
2689 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2690 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2695 for (inst = state->inst_head; inst != NULL; inst = temp) {
2700 state->inst_head = NULL;
2701 state->inst_tail = NULL;
2703 for (sym = state->sym; sym != NULL; sym = temp) {
2706 _mesa_free((void *) sym->name);
2711 _mesa_symbol_table_dtor(state->st);