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);
313 const char *const err_str = (state->mode == ARB_vertex)
314 ? "invalid ARB vertex program option"
315 : "invalid ARB fragment program option";
317 yyerror(& @2, state, err_str);
323 statementSequence: statementSequence statement
327 statement: instruction ';'
330 if (state->inst_tail == NULL) {
331 state->inst_head = $1;
333 state->inst_tail->next = $1;
336 state->inst_tail = $1;
339 state->prog->NumInstructions++;
342 | namingStatement ';'
345 instruction: ALU_instruction
348 state->prog->NumAluInstructions++;
353 state->prog->NumTexInstructions++;
357 ALU_instruction: ARL_instruction
358 | VECTORop_instruction
359 | SCALARop_instruction
360 | BINSCop_instruction
366 TexInstruction: SAMPLE_instruction
371 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
373 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
377 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
379 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
383 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
385 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
389 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
391 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
396 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
398 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
402 TRIop_instruction: TRI_OP maskedDstReg ','
403 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
405 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
409 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
411 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
413 const GLbitfield tex_mask = (1U << $6);
414 GLbitfield shadow_tex = 0;
415 GLbitfield target_mask = 0;
418 $$->Base.TexSrcUnit = $6;
421 shadow_tex = tex_mask;
423 $$->Base.TexSrcTarget = -$8;
424 $$->Base.TexShadow = 1;
426 $$->Base.TexSrcTarget = $8;
429 target_mask = (1U << $$->Base.TexSrcTarget);
431 /* If this texture unit was previously accessed and that access
432 * had a different texture target, generate an error.
434 * If this texture unit was previously accessed and that access
435 * had a different shadow mode, generate an error.
437 if ((state->prog->TexturesUsed[$6] != 0)
438 && ((state->prog->TexturesUsed[$6] != target_mask)
439 || ((state->prog->ShadowSamplers & tex_mask)
442 "multiple targets used on one texture image unit");
447 state->prog->TexturesUsed[$6] |= target_mask;
448 state->prog->ShadowSamplers |= shadow_tex;
453 KIL_instruction: KIL swizzleSrcReg
455 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
456 state->fragment.UsesKill = 1;
460 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
461 $$->Base.DstReg.CondMask = $2.CondMask;
462 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
463 $$->Base.DstReg.CondSrc = $2.CondSrc;
464 state->fragment.UsesKill = 1;
468 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
470 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
472 const GLbitfield tex_mask = (1U << $10);
473 GLbitfield shadow_tex = 0;
474 GLbitfield target_mask = 0;
477 $$->Base.TexSrcUnit = $10;
480 shadow_tex = tex_mask;
482 $$->Base.TexSrcTarget = -$12;
483 $$->Base.TexShadow = 1;
485 $$->Base.TexSrcTarget = $12;
488 target_mask = (1U << $$->Base.TexSrcTarget);
490 /* If this texture unit was previously accessed and that access
491 * had a different texture target, generate an error.
493 * If this texture unit was previously accessed and that access
494 * had a different shadow mode, generate an error.
496 if ((state->prog->TexturesUsed[$10] != 0)
497 && ((state->prog->TexturesUsed[$10] != target_mask)
498 || ((state->prog->ShadowSamplers & tex_mask)
500 yyerror(& @12, state,
501 "multiple targets used on one texture image unit");
506 state->prog->TexturesUsed[$10] |= target_mask;
507 state->prog->ShadowSamplers |= shadow_tex;
512 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
518 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
519 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
520 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
521 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
522 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
523 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
524 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
525 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
526 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
527 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
528 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
529 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
532 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
534 /* FIXME: Is this correct? Should the extenedSwizzle be applied
535 * FIXME: to the existing swizzle?
537 $4.Base.Swizzle = $6.swizzle;
538 $4.Base.Negate = $6.mask;
540 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
544 scalarSrcReg: optionalSign scalarUse
549 $$.Base.Negate = ~$$.Base.Negate;
552 | optionalSign '|' scalarUse '|'
556 if (!state->option.NV_fragment) {
557 yyerror(& @2, state, "unexpected character '|'");
562 $$.Base.Negate = ~$$.Base.Negate;
569 scalarUse: srcReg scalarSuffix
573 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
576 | paramConstScalarUse
578 struct asm_symbol temp_sym;
580 if (!state->option.NV_fragment) {
581 yyerror(& @1, state, "expected scalar suffix");
585 memset(& temp_sym, 0, sizeof(temp_sym));
586 temp_sym.param_binding_begin = ~0;
587 initialize_symbol_from_const(state->prog, & temp_sym, & $1);
589 set_src_reg(& $$, PROGRAM_CONSTANT, temp_sym.param_binding_begin);
593 swizzleSrcReg: optionalSign srcReg swizzleSuffix
598 $$.Base.Negate = ~$$.Base.Negate;
601 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
604 | optionalSign '|' srcReg swizzleSuffix '|'
608 if (!state->option.NV_fragment) {
609 yyerror(& @2, state, "unexpected character '|'");
614 $$.Base.Negate = ~$$.Base.Negate;
618 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
624 maskedDstReg: dstReg optionalMask optionalCcMask
627 $$.WriteMask = $2.mask;
628 $$.CondMask = $3.CondMask;
629 $$.CondSwizzle = $3.CondSwizzle;
630 $$.CondSrc = $3.CondSrc;
632 if ($$.File == PROGRAM_OUTPUT) {
633 /* Technically speaking, this should check that it is in
634 * vertex program mode. However, PositionInvariant can never be
635 * set in fragment program mode, so it is somewhat irrelevant.
637 if (state->option.PositionInvariant
638 && ($$.Index == VERT_RESULT_HPOS)) {
639 yyerror(& @1, state, "position-invariant programs cannot "
644 state->prog->OutputsWritten |= (1U << $$.Index);
649 maskedAddrReg: addrReg addrWriteMask
651 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
652 $$.WriteMask = $2.mask;
656 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
658 const unsigned xyzw_valid =
660 | ($3.xyzw_valid << 1)
661 | ($5.xyzw_valid << 2)
662 | ($7.xyzw_valid << 3);
663 const unsigned rgba_valid =
665 | ($3.rgba_valid << 1)
666 | ($5.rgba_valid << 2)
667 | ($7.rgba_valid << 3);
669 /* All of the swizzle components have to be valid in either RGBA
670 * or XYZW. Note that 0 and 1 are valid in both, so both masks
671 * can have some bits set.
673 * We somewhat deviate from the spec here. It would be really hard
674 * to figure out which component is the error, and there probably
675 * isn't a lot of benefit.
677 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
678 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
683 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
684 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
689 extSwizComp: optionalSign extSwizSel
692 $$.negate = ($1) ? 1 : 0;
698 if (($1 != 0) && ($1 != 1)) {
699 yyerror(& @1, state, "invalid extended swizzle selector");
703 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
705 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
713 if (strlen($1) > 1) {
714 yyerror(& @1, state, "invalid extended swizzle selector");
754 yyerror(& @1, state, "invalid extended swizzle selector");
761 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
763 struct asm_symbol *const s = (struct asm_symbol *)
764 _mesa_symbol_table_find_symbol(state->st, 0, $1);
767 yyerror(& @1, state, "invalid operand variable");
769 } else if ((s->type != at_param) && (s->type != at_temp)
770 && (s->type != at_attrib)) {
771 yyerror(& @1, state, "invalid operand variable");
773 } else if ((s->type == at_param) && s->param_is_array) {
774 yyerror(& @1, state, "non-array access to array PARAM");
781 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
784 set_src_reg(& $$, s->param_binding_type, s->param_binding_begin);
787 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
788 state->prog->InputsRead |= (1U << $$.Base.Index);
790 if (!validate_inputs(& @1, state)) {
802 set_src_reg(& $$, PROGRAM_INPUT, $1);
803 state->prog->InputsRead |= (1U << $$.Base.Index);
805 if (!validate_inputs(& @1, state)) {
809 | progParamArray '[' progParamArrayMem ']'
811 if (! $3.Base.RelAddr
812 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
813 yyerror(& @3, state, "out of bounds array access");
818 $$.Base.File = $1->param_binding_type;
820 if ($3.Base.RelAddr) {
821 $1->param_accessed_indirectly = 1;
824 $$.Base.Index = $3.Base.Index;
827 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
832 gl_register_file file = ($1.name != NULL)
833 ? $1.param_binding_type
835 set_src_reg(& $$, file, $1.param_binding_begin);
839 dstReg: resultBinding
841 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
843 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
845 struct asm_symbol *const s = (struct asm_symbol *)
846 _mesa_symbol_table_find_symbol(state->st, 0, $1);
849 yyerror(& @1, state, "invalid operand variable");
851 } else if ((s->type != at_output) && (s->type != at_temp)) {
852 yyerror(& @1, state, "invalid operand variable");
858 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
861 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
864 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
870 progParamArray: USED_IDENTIFIER
872 struct asm_symbol *const s = (struct asm_symbol *)
873 _mesa_symbol_table_find_symbol(state->st, 0, $1);
876 yyerror(& @1, state, "invalid operand variable");
878 } else if ((s->type != at_param) || !s->param_is_array) {
879 yyerror(& @1, state, "array access to non-PARAM variable");
887 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
889 progParamArrayAbs: INTEGER
896 progParamArrayRel: addrReg addrComponent addrRegRelOffset
898 /* FINISHME: Add support for multiple address registers.
900 /* FINISHME: Add support for 4-component address registers.
908 addrRegRelOffset: { $$ = 0; }
909 | '+' addrRegPosOffset { $$ = $2; }
910 | '-' addrRegNegOffset { $$ = -$2; }
913 addrRegPosOffset: INTEGER
915 if (($1 < 0) || ($1 > 63)) {
917 _mesa_snprintf(s, sizeof(s),
918 "relative address offset too large (%d)", $1);
919 yyerror(& @1, state, s);
927 addrRegNegOffset: INTEGER
929 if (($1 < 0) || ($1 > 64)) {
931 _mesa_snprintf(s, sizeof(s),
932 "relative address offset too large (%d)", $1);
933 yyerror(& @1, state, s);
941 addrReg: USED_IDENTIFIER
943 struct asm_symbol *const s = (struct asm_symbol *)
944 _mesa_symbol_table_find_symbol(state->st, 0, $1);
947 yyerror(& @1, state, "invalid array member");
949 } else if (s->type != at_address) {
951 "invalid variable for indexed array access");
961 if ($1.mask != WRITEMASK_X) {
962 yyerror(& @1, state, "invalid address component selector");
972 if ($1.mask != WRITEMASK_X) {
974 "address register write mask must be \".x\"");
987 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
990 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
991 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
994 optionalCcMask: '(' ccTest ')'
1004 $$.CondMask = COND_TR;
1005 $$.CondSwizzle = SWIZZLE_NOOP;
1010 ccTest: ccMaskRule swizzleSuffix
1013 $$.CondSwizzle = $2.swizzle;
1017 ccTest2: ccMaskRule2 swizzleSuffix
1020 $$.CondSwizzle = $2.swizzle;
1024 ccMaskRule: IDENTIFIER
1026 const int cond = _mesa_parse_cc($1);
1027 if ((cond == 0) || ($1[2] != '\0')) {
1028 char *const err_str =
1029 make_error_string("invalid condition code \"%s\"", $1);
1031 yyerror(& @1, state, (err_str != NULL)
1032 ? err_str : "invalid condition code");
1034 if (err_str != NULL) {
1035 _mesa_free(err_str);
1042 $$.CondSwizzle = SWIZZLE_NOOP;
1047 ccMaskRule2: USED_IDENTIFIER
1049 const int cond = _mesa_parse_cc($1);
1050 if ((cond == 0) || ($1[2] != '\0')) {
1051 char *const err_str =
1052 make_error_string("invalid condition code \"%s\"", $1);
1054 yyerror(& @1, state, (err_str != NULL)
1055 ? err_str : "invalid condition code");
1057 if (err_str != NULL) {
1058 _mesa_free(err_str);
1065 $$.CondSwizzle = SWIZZLE_NOOP;
1070 namingStatement: ATTRIB_statement
1078 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1080 struct asm_symbol *const s =
1081 declare_variable(state, $2, at_attrib, & @2);
1086 s->attrib_binding = $4;
1087 state->InputsBound |= (1U << s->attrib_binding);
1089 if (!validate_inputs(& @4, state)) {
1096 attribBinding: VERTEX vtxAttribItem
1100 | FRAGMENT fragAttribItem
1106 vtxAttribItem: POSITION
1108 $$ = VERT_ATTRIB_POS;
1110 | WEIGHT vtxOptWeightNum
1112 $$ = VERT_ATTRIB_WEIGHT;
1116 $$ = VERT_ATTRIB_NORMAL;
1118 | COLOR optColorType
1120 if (!state->ctx->Extensions.EXT_secondary_color) {
1121 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1125 $$ = VERT_ATTRIB_COLOR0 + $2;
1129 if (!state->ctx->Extensions.EXT_fog_coord) {
1130 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1134 $$ = VERT_ATTRIB_FOG;
1136 | TEXCOORD optTexCoordUnitNum
1138 $$ = VERT_ATTRIB_TEX0 + $2;
1140 | MATRIXINDEX '[' vtxWeightNum ']'
1142 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1145 | VTXATTRIB '[' vtxAttribNum ']'
1147 $$ = VERT_ATTRIB_GENERIC0 + $3;
1151 vtxAttribNum: INTEGER
1153 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1154 yyerror(& @1, state, "invalid vertex attribute reference");
1162 vtxOptWeightNum: | '[' vtxWeightNum ']';
1163 vtxWeightNum: INTEGER;
1165 fragAttribItem: POSITION
1167 $$ = FRAG_ATTRIB_WPOS;
1169 | COLOR optColorType
1171 $$ = FRAG_ATTRIB_COL0 + $2;
1175 $$ = FRAG_ATTRIB_FOGC;
1177 | TEXCOORD optTexCoordUnitNum
1179 $$ = FRAG_ATTRIB_TEX0 + $2;
1183 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1185 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1187 struct asm_symbol *const s =
1188 declare_variable(state, $2, at_param, & @2);
1193 s->param_binding_type = $3.param_binding_type;
1194 s->param_binding_begin = $3.param_binding_begin;
1195 s->param_binding_length = $3.param_binding_length;
1196 s->param_is_array = 0;
1201 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1203 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1204 yyerror(& @4, state,
1205 "parameter array size and number of bindings must match");
1208 struct asm_symbol *const s =
1209 declare_variable(state, $2, $6.type, & @2);
1214 s->param_binding_type = $6.param_binding_type;
1215 s->param_binding_begin = $6.param_binding_begin;
1216 s->param_binding_length = $6.param_binding_length;
1217 s->param_is_array = 1;
1229 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1230 yyerror(& @1, state, "invalid parameter array size");
1238 paramSingleInit: '=' paramSingleItemDecl
1244 paramMultipleInit: '=' '{' paramMultInitList '}'
1250 paramMultInitList: paramMultipleItem
1251 | paramMultInitList ',' paramMultipleItem
1253 $1.param_binding_length += $3.param_binding_length;
1258 paramSingleItemDecl: stateSingleItem
1260 memset(& $$, 0, sizeof($$));
1261 $$.param_binding_begin = ~0;
1262 initialize_symbol_from_state(state->prog, & $$, $1);
1266 memset(& $$, 0, sizeof($$));
1267 $$.param_binding_begin = ~0;
1268 initialize_symbol_from_param(state->prog, & $$, $1);
1272 memset(& $$, 0, sizeof($$));
1273 $$.param_binding_begin = ~0;
1274 initialize_symbol_from_const(state->prog, & $$, & $1);
1278 paramSingleItemUse: stateSingleItem
1280 memset(& $$, 0, sizeof($$));
1281 $$.param_binding_begin = ~0;
1282 initialize_symbol_from_state(state->prog, & $$, $1);
1286 memset(& $$, 0, sizeof($$));
1287 $$.param_binding_begin = ~0;
1288 initialize_symbol_from_param(state->prog, & $$, $1);
1292 memset(& $$, 0, sizeof($$));
1293 $$.param_binding_begin = ~0;
1294 initialize_symbol_from_const(state->prog, & $$, & $1);
1298 paramMultipleItem: stateMultipleItem
1300 memset(& $$, 0, sizeof($$));
1301 $$.param_binding_begin = ~0;
1302 initialize_symbol_from_state(state->prog, & $$, $1);
1304 | programMultipleItem
1306 memset(& $$, 0, sizeof($$));
1307 $$.param_binding_begin = ~0;
1308 initialize_symbol_from_param(state->prog, & $$, $1);
1312 memset(& $$, 0, sizeof($$));
1313 $$.param_binding_begin = ~0;
1314 initialize_symbol_from_const(state->prog, & $$, & $1);
1318 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1319 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1322 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1323 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1324 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1325 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1326 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1327 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1328 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1329 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1330 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1331 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1332 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1335 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1337 memset($$, 0, sizeof($$));
1338 $$[0] = STATE_MATERIAL;
1344 stateMatProperty: ambDiffSpecProperty
1350 $$ = STATE_EMISSION;
1354 $$ = STATE_SHININESS;
1358 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1360 memset($$, 0, sizeof($$));
1361 $$[0] = STATE_LIGHT;
1367 stateLightProperty: ambDiffSpecProperty
1373 $$ = STATE_POSITION;
1377 if (!state->ctx->Extensions.EXT_point_parameters) {
1378 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1382 $$ = STATE_ATTENUATION;
1384 | SPOT stateSpotProperty
1390 $$ = STATE_HALF_VECTOR;
1394 stateSpotProperty: DIRECTION
1396 $$ = STATE_SPOT_DIRECTION;
1400 stateLightModelItem: LIGHTMODEL stateLModProperty
1407 stateLModProperty: AMBIENT
1409 memset($$, 0, sizeof($$));
1410 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1412 | optFaceType SCENECOLOR
1414 memset($$, 0, sizeof($$));
1415 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1420 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1422 memset($$, 0, sizeof($$));
1423 $$[0] = STATE_LIGHTPROD;
1430 stateLProdProperty: ambDiffSpecProperty;
1432 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1434 memset($$, 0, sizeof($$));
1440 stateTexEnvProperty: COLOR
1442 $$ = STATE_TEXENV_COLOR;
1446 ambDiffSpecProperty: AMBIENT
1456 $$ = STATE_SPECULAR;
1460 stateLightNumber: INTEGER
1462 if ((unsigned) $1 >= state->MaxLights) {
1463 yyerror(& @1, state, "invalid light selector");
1471 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1473 memset($$, 0, sizeof($$));
1474 $$[0] = STATE_TEXGEN;
1480 stateTexGenType: EYE
1482 $$ = STATE_TEXGEN_EYE_S;
1486 $$ = STATE_TEXGEN_OBJECT_S;
1489 stateTexGenCoord: TEXGEN_S
1491 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1495 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1499 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1503 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1507 stateFogItem: FOG stateFogProperty
1509 memset($$, 0, sizeof($$));
1514 stateFogProperty: COLOR
1516 $$ = STATE_FOG_COLOR;
1520 $$ = STATE_FOG_PARAMS;
1524 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1526 memset($$, 0, sizeof($$));
1527 $$[0] = STATE_CLIPPLANE;
1532 stateClipPlaneNum: INTEGER
1534 if ((unsigned) $1 >= state->MaxClipPlanes) {
1535 yyerror(& @1, state, "invalid clip plane selector");
1543 statePointItem: POINT_TOK statePointProperty
1545 memset($$, 0, sizeof($$));
1550 statePointProperty: SIZE_TOK
1552 $$ = STATE_POINT_SIZE;
1556 $$ = STATE_POINT_ATTENUATION;
1560 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1570 stateMatrixRows: stateMatrixItem optMatrixRows
1585 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1587 /* It seems logical that the matrix row range specifier would have
1588 * to specify a range or more than one row (i.e., $5 > $3).
1589 * However, the ARB_vertex_program spec says "a program will fail
1590 * to load if <a> is greater than <b>." This means that $3 == $5
1594 yyerror(& @3, state, "invalid matrix row range");
1603 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1611 stateOptMatModifier:
1621 stateMatModifier: INVERSE
1623 $$ = STATE_MATRIX_INVERSE;
1627 $$ = STATE_MATRIX_TRANSPOSE;
1631 $$ = STATE_MATRIX_INVTRANS;
1635 stateMatrixRowNum: INTEGER
1638 yyerror(& @1, state, "invalid matrix row reference");
1646 stateMatrixName: MODELVIEW stateOptModMatNum
1648 $$[0] = STATE_MODELVIEW_MATRIX;
1653 $$[0] = STATE_PROJECTION_MATRIX;
1658 $$[0] = STATE_MVP_MATRIX;
1661 | TEXTURE optTexCoordUnitNum
1663 $$[0] = STATE_TEXTURE_MATRIX;
1666 | PALETTE '[' statePaletteMatNum ']'
1668 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1671 | MAT_PROGRAM '[' stateProgramMatNum ']'
1673 $$[0] = STATE_PROGRAM_MATRIX;
1682 | '[' stateModMatNum ']'
1687 stateModMatNum: INTEGER
1689 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1693 yyerror(& @1, state, "invalid modelview matrix index");
1700 statePaletteMatNum: INTEGER
1702 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1703 * through here. The error will be generated later.
1708 stateProgramMatNum: INTEGER
1710 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1711 yyerror(& @1, state, "invalid program matrix selector");
1719 stateDepthItem: DEPTH RANGE
1721 memset($$, 0, sizeof($$));
1722 $$[0] = STATE_DEPTH_RANGE;
1727 programSingleItem: progEnvParam | progLocalParam;
1729 programMultipleItem: progEnvParams | progLocalParams;
1731 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1733 memset($$, 0, sizeof($$));
1734 $$[0] = state->state_param_enum;
1741 progEnvParamNums: progEnvParamNum
1746 | progEnvParamNum DOT_DOT progEnvParamNum
1753 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1755 memset($$, 0, sizeof($$));
1756 $$[0] = state->state_param_enum;
1763 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1765 memset($$, 0, sizeof($$));
1766 $$[0] = state->state_param_enum;
1767 $$[1] = STATE_LOCAL;
1772 progLocalParamNums: progLocalParamNum
1777 | progLocalParamNum DOT_DOT progLocalParamNum
1784 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1786 memset($$, 0, sizeof($$));
1787 $$[0] = state->state_param_enum;
1788 $$[1] = STATE_LOCAL;
1794 progEnvParamNum: INTEGER
1796 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1797 yyerror(& @1, state, "invalid environment parameter reference");
1804 progLocalParamNum: INTEGER
1806 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1807 yyerror(& @1, state, "invalid local parameter reference");
1816 paramConstDecl: paramConstScalarDecl | paramConstVector;
1817 paramConstUse: paramConstScalarUse | paramConstVector;
1819 paramConstScalarDecl: signedFloatConstant
1829 paramConstScalarUse: REAL
1840 $$.data[0] = (float) $1;
1841 $$.data[1] = (float) $1;
1842 $$.data[2] = (float) $1;
1843 $$.data[3] = (float) $1;
1847 paramConstVector: '{' signedFloatConstant '}'
1855 | '{' signedFloatConstant ',' signedFloatConstant '}'
1863 | '{' signedFloatConstant ',' signedFloatConstant ','
1864 signedFloatConstant '}'
1872 | '{' signedFloatConstant ',' signedFloatConstant ','
1873 signedFloatConstant ',' signedFloatConstant '}'
1883 signedFloatConstant: optionalSign REAL
1885 $$ = ($1) ? -$2 : $2;
1887 | optionalSign INTEGER
1889 $$ = (float)(($1) ? -$2 : $2);
1893 optionalSign: '+' { $$ = FALSE; }
1894 | '-' { $$ = TRUE; }
1898 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1903 /* NV_fragment_program_option defines the size qualifiers in a
1904 * fairly broken way. "SHORT" or "LONG" can optionally be used
1905 * before TEMP or OUTPUT. However, neither is a reserved word!
1906 * This means that we have to parse it as an identifier, then check
1907 * to make sure it's one of the valid values. *sigh*
1909 * In addition, the grammar in the extension spec does *not* allow
1910 * the size specifier to be optional, but all known implementations
1913 if (!state->option.NV_fragment) {
1914 yyerror(& @1, state, "unexpected IDENTIFIER");
1918 if (strcmp("SHORT", $1) == 0) {
1919 } else if (strcmp("LONG", $1) == 0) {
1921 char *const err_str =
1922 make_error_string("invalid storage size specifier \"%s\"",
1925 yyerror(& @1, state, (err_str != NULL)
1926 ? err_str : "invalid storage size specifier");
1928 if (err_str != NULL) {
1929 _mesa_free(err_str);
1940 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1943 varNameList: varNameList ',' IDENTIFIER
1945 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1951 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1957 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1959 struct asm_symbol *const s =
1960 declare_variable(state, $3, at_output, & @3);
1965 s->output_binding = $5;
1970 resultBinding: RESULT POSITION
1972 if (state->mode == ARB_vertex) {
1973 $$ = VERT_RESULT_HPOS;
1975 yyerror(& @2, state, "invalid program result name");
1981 if (state->mode == ARB_vertex) {
1982 $$ = VERT_RESULT_FOGC;
1984 yyerror(& @2, state, "invalid program result name");
1988 | RESULT resultColBinding
1994 if (state->mode == ARB_vertex) {
1995 $$ = VERT_RESULT_PSIZ;
1997 yyerror(& @2, state, "invalid program result name");
2001 | RESULT TEXCOORD optTexCoordUnitNum
2003 if (state->mode == ARB_vertex) {
2004 $$ = VERT_RESULT_TEX0 + $3;
2006 yyerror(& @2, state, "invalid program result name");
2012 if (state->mode == ARB_fragment) {
2013 $$ = FRAG_RESULT_DEPTH;
2015 yyerror(& @2, state, "invalid program result name");
2021 resultColBinding: COLOR optResultFaceType optResultColorType
2029 $$ = (state->mode == ARB_vertex)
2031 : FRAG_RESULT_COLOR;
2035 if (state->mode == ARB_vertex) {
2036 $$ = VERT_RESULT_COL0;
2038 yyerror(& @1, state, "invalid program result name");
2044 if (state->mode == ARB_vertex) {
2045 $$ = VERT_RESULT_BFC0;
2047 yyerror(& @1, state, "invalid program result name");
2059 if (state->mode == ARB_vertex) {
2062 yyerror(& @1, state, "invalid program result name");
2068 if (state->mode == ARB_vertex) {
2071 yyerror(& @1, state, "invalid program result name");
2077 optFaceType: { $$ = 0; }
2082 optColorType: { $$ = 0; }
2083 | PRIMARY { $$ = 0; }
2084 | SECONDARY { $$ = 1; }
2087 optTexCoordUnitNum: { $$ = 0; }
2088 | '[' texCoordUnitNum ']' { $$ = $2; }
2091 optTexImageUnitNum: { $$ = 0; }
2092 | '[' texImageUnitNum ']' { $$ = $2; }
2095 optLegacyTexUnitNum: { $$ = 0; }
2096 | '[' legacyTexUnitNum ']' { $$ = $2; }
2099 texCoordUnitNum: INTEGER
2101 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2102 yyerror(& @1, state, "invalid texture coordinate unit selector");
2110 texImageUnitNum: INTEGER
2112 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2113 yyerror(& @1, state, "invalid texture image unit selector");
2121 legacyTexUnitNum: INTEGER
2123 if ((unsigned) $1 >= state->MaxTextureUnits) {
2124 yyerror(& @1, state, "invalid texture unit selector");
2132 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2134 struct asm_symbol *exist = (struct asm_symbol *)
2135 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2136 struct asm_symbol *target = (struct asm_symbol *)
2137 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2139 if (exist != NULL) {
2141 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2142 yyerror(& @2, state, m);
2144 } else if (target == NULL) {
2145 yyerror(& @4, state,
2146 "undefined variable binding in ALIAS statement");
2149 _mesa_symbol_table_add_symbol(state->st, 0, strdup($2), target);
2161 asm_instruction_set_operands(struct asm_instruction *inst,
2162 const struct prog_dst_register *dst,
2163 const struct asm_src_register *src0,
2164 const struct asm_src_register *src1,
2165 const struct asm_src_register *src2)
2167 /* In the core ARB extensions only the KIL instruction doesn't have a
2168 * destination register.
2171 init_dst_reg(& inst->Base.DstReg);
2173 inst->Base.DstReg = *dst;
2176 /* The only instruction that doesn't have any source registers is the
2177 * condition-code based KIL instruction added by NV_fragment_program_option.
2180 inst->Base.SrcReg[0] = src0->Base;
2181 inst->SrcReg[0] = *src0;
2183 init_src_reg(& inst->SrcReg[0]);
2187 inst->Base.SrcReg[1] = src1->Base;
2188 inst->SrcReg[1] = *src1;
2190 init_src_reg(& inst->SrcReg[1]);
2194 inst->Base.SrcReg[2] = src2->Base;
2195 inst->SrcReg[2] = *src2;
2197 init_src_reg(& inst->SrcReg[2]);
2202 struct asm_instruction *
2203 asm_instruction_ctor(gl_inst_opcode op,
2204 const struct prog_dst_register *dst,
2205 const struct asm_src_register *src0,
2206 const struct asm_src_register *src1,
2207 const struct asm_src_register *src2)
2209 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2212 _mesa_init_instructions(& inst->Base, 1);
2213 inst->Base.Opcode = op;
2215 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2222 struct asm_instruction *
2223 asm_instruction_copy_ctor(const struct prog_instruction *base,
2224 const struct prog_dst_register *dst,
2225 const struct asm_src_register *src0,
2226 const struct asm_src_register *src1,
2227 const struct asm_src_register *src2)
2229 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2232 _mesa_init_instructions(& inst->Base, 1);
2233 inst->Base.Opcode = base->Opcode;
2234 inst->Base.CondUpdate = base->CondUpdate;
2235 inst->Base.CondDst = base->CondDst;
2236 inst->Base.SaturateMode = base->SaturateMode;
2237 inst->Base.Precision = base->Precision;
2239 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2247 init_dst_reg(struct prog_dst_register *r)
2249 memset(r, 0, sizeof(*r));
2250 r->File = PROGRAM_UNDEFINED;
2251 r->WriteMask = WRITEMASK_XYZW;
2252 r->CondMask = COND_TR;
2253 r->CondSwizzle = SWIZZLE_NOOP;
2257 /** Like init_dst_reg() but set the File and Index fields. */
2259 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2261 const GLint maxIndex = 1 << INST_INDEX_BITS;
2262 const GLint minIndex = 0;
2263 ASSERT(index >= minIndex);
2264 ASSERT(index <= maxIndex);
2265 ASSERT(file == PROGRAM_TEMPORARY ||
2266 file == PROGRAM_ADDRESS ||
2267 file == PROGRAM_OUTPUT);
2268 memset(r, 0, sizeof(*r));
2271 r->WriteMask = WRITEMASK_XYZW;
2272 r->CondMask = COND_TR;
2273 r->CondSwizzle = SWIZZLE_NOOP;
2278 init_src_reg(struct asm_src_register *r)
2280 memset(r, 0, sizeof(*r));
2281 r->Base.File = PROGRAM_UNDEFINED;
2282 r->Base.Swizzle = SWIZZLE_NOOP;
2287 /** Like init_src_reg() but set the File and Index fields. */
2289 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2291 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2292 const GLint minIndex = -(1 << INST_INDEX_BITS);
2293 ASSERT(index >= minIndex);
2294 ASSERT(index <= maxIndex);
2295 ASSERT(file < PROGRAM_FILE_MAX);
2296 memset(r, 0, sizeof(*r));
2297 r->Base.File = file;
2298 r->Base.Index = index;
2299 r->Base.Swizzle = SWIZZLE_NOOP;
2305 * Validate the set of inputs used by a program
2307 * Validates that legal sets of inputs are used by the program. In this case
2308 * "used" included both reading the input or binding the input to a name using
2309 * the \c ATTRIB command.
2312 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2315 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2317 const int inputs = state->prog->InputsRead | state->InputsBound;
2319 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
2320 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2329 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2330 struct YYLTYPE *locp)
2332 struct asm_symbol *s = NULL;
2333 struct asm_symbol *exist = (struct asm_symbol *)
2334 _mesa_symbol_table_find_symbol(state->st, 0, name);
2337 if (exist != NULL) {
2338 yyerror(locp, state, "redeclared identifier");
2340 const size_t name_len = strlen(name);
2342 s = calloc(1, sizeof(struct asm_symbol) + name_len + 1);
2343 s->name = (char *)(s + 1);
2346 memcpy((char *) s->name, name, name_len + 1);
2350 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2351 yyerror(locp, state, "too many temporaries declared");
2356 s->temp_binding = state->prog->NumTemporaries;
2357 state->prog->NumTemporaries++;
2361 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2362 yyerror(locp, state, "too many address registers declared");
2367 /* FINISHME: Add support for multiple address registers.
2369 state->prog->NumAddressRegs++;
2376 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2377 s->next = state->sym;
2385 int add_state_reference(struct gl_program_parameter_list *param_list,
2386 const gl_state_index tokens[STATE_LENGTH])
2388 const GLuint size = 4; /* XXX fix */
2392 name = _mesa_program_state_string(tokens);
2393 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2394 size, GL_NONE, NULL, tokens, 0x0);
2395 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2397 /* free name string here since we duplicated it in add_parameter() */
2405 initialize_symbol_from_state(struct gl_program *prog,
2406 struct asm_symbol *param_var,
2407 const gl_state_index tokens[STATE_LENGTH])
2410 gl_state_index state_tokens[STATE_LENGTH];
2413 memcpy(state_tokens, tokens, sizeof(state_tokens));
2415 param_var->type = at_param;
2416 param_var->param_binding_type = PROGRAM_STATE_VAR;
2418 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2419 * unroll it and call add_state_reference() for each row
2421 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2422 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2423 state_tokens[0] == STATE_MVP_MATRIX ||
2424 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2425 state_tokens[0] == STATE_PROGRAM_MATRIX)
2426 && (state_tokens[2] != state_tokens[3])) {
2428 const int first_row = state_tokens[2];
2429 const int last_row = state_tokens[3];
2431 for (row = first_row; row <= last_row; row++) {
2432 state_tokens[2] = state_tokens[3] = row;
2434 idx = add_state_reference(prog->Parameters, state_tokens);
2435 if (param_var->param_binding_begin == ~0U)
2436 param_var->param_binding_begin = idx;
2437 param_var->param_binding_length++;
2441 idx = add_state_reference(prog->Parameters, state_tokens);
2442 if (param_var->param_binding_begin == ~0U)
2443 param_var->param_binding_begin = idx;
2444 param_var->param_binding_length++;
2452 initialize_symbol_from_param(struct gl_program *prog,
2453 struct asm_symbol *param_var,
2454 const gl_state_index tokens[STATE_LENGTH])
2457 gl_state_index state_tokens[STATE_LENGTH];
2460 memcpy(state_tokens, tokens, sizeof(state_tokens));
2462 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2463 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2464 assert((state_tokens[1] == STATE_ENV)
2465 || (state_tokens[1] == STATE_LOCAL));
2467 param_var->type = at_param;
2468 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2469 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
2471 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2472 * we need to unroll it and call add_state_reference() for each row
2474 if (state_tokens[2] != state_tokens[3]) {
2476 const int first_row = state_tokens[2];
2477 const int last_row = state_tokens[3];
2479 for (row = first_row; row <= last_row; row++) {
2480 state_tokens[2] = state_tokens[3] = row;
2482 idx = add_state_reference(prog->Parameters, state_tokens);
2483 if (param_var->param_binding_begin == ~0U)
2484 param_var->param_binding_begin = idx;
2485 param_var->param_binding_length++;
2489 idx = add_state_reference(prog->Parameters, state_tokens);
2490 if (param_var->param_binding_begin == ~0U)
2491 param_var->param_binding_begin = idx;
2492 param_var->param_binding_length++;
2500 initialize_symbol_from_const(struct gl_program *prog,
2501 struct asm_symbol *param_var,
2502 const struct asm_vector *vec)
2504 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2505 NULL, vec->count, GL_NONE, vec->data,
2508 param_var->type = at_param;
2509 param_var->param_binding_type = PROGRAM_CONSTANT;
2511 if (param_var->param_binding_begin == ~0U)
2512 param_var->param_binding_begin = idx;
2513 param_var->param_binding_length++;
2520 make_error_string(const char *fmt, ...)
2526 va_start(args, fmt);
2528 /* Call vsnprintf once to determine how large the final string is. Call it
2529 * again to do the actual formatting. from the vsnprintf manual page:
2531 * Upon successful return, these functions return the number of
2532 * characters printed (not including the trailing '\0' used to end
2533 * output to strings).
2535 length = 1 + vsnprintf(NULL, 0, fmt, args);
2537 str = _mesa_malloc(length);
2539 vsnprintf(str, length, fmt, args);
2549 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2554 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2556 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2557 _mesa_free(err_str);
2560 err_str = make_error_string("line %u, char %u: error: %s\n",
2561 locp->first_line, locp->first_column, s);
2562 _mesa_set_program_error(state->ctx, locp->position, err_str);
2565 _mesa_free(err_str);
2571 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2572 GLsizei len, struct asm_parser_state *state)
2574 struct asm_instruction *inst;
2577 GLboolean result = GL_FALSE;
2579 struct asm_symbol *sym;
2582 state->prog->Target = target;
2583 state->prog->Parameters = _mesa_new_parameter_list();
2585 /* Make a copy of the program string and force it to be NUL-terminated.
2587 strz = (GLubyte *) _mesa_malloc(len + 1);
2589 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2592 _mesa_memcpy (strz, str, len);
2595 if (state->prog->String != NULL) {
2596 _mesa_free(state->prog->String);
2597 state->prog->String = NULL;
2600 state->prog->String = strz;
2602 state->st = _mesa_symbol_table_ctor();
2604 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2605 ? & ctx->Const.VertexProgram
2606 : & ctx->Const.FragmentProgram;
2608 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2609 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2610 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2611 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2612 state->MaxLights = ctx->Const.MaxLights;
2613 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2615 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2616 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2618 _mesa_set_program_error(ctx, -1, NULL);
2620 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2622 _mesa_program_lexer_dtor(state->scanner);
2625 if (ctx->Program.ErrorPos != -1) {
2629 if (! _mesa_layout_parameters(state)) {
2633 loc.first_column = 0;
2636 yyerror(& loc, state, "invalid PARAM usage");
2642 /* Add one instruction to store the "END" instruction.
2644 state->prog->Instructions =
2645 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2646 inst = state->inst_head;
2647 for (i = 0; i < state->prog->NumInstructions; i++) {
2648 struct asm_instruction *const temp = inst->next;
2650 state->prog->Instructions[i] = inst->Base;
2654 /* Finally, tag on an OPCODE_END instruction */
2656 const GLuint numInst = state->prog->NumInstructions;
2657 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2658 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2660 state->prog->NumInstructions++;
2662 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2663 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
2666 * Initialize native counts to logical counts. The device driver may
2667 * change them if program is translated into a hardware program.
2669 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2670 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2671 state->prog->NumNativeParameters = state->prog->NumParameters;
2672 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2673 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2678 for (inst = state->inst_head; inst != NULL; inst = temp) {
2683 state->inst_head = NULL;
2684 state->inst_tail = NULL;
2686 for (sym = state->sym; sym != NULL; sym = temp) {
2693 _mesa_symbol_table_dtor(state->st);
2696 if (state->string_dumpster != NULL) {
2697 _mesa_free(state->string_dumpster);
2698 state->dumpster_size = 0;