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 set_src_reg_swz(struct asm_src_register *r,
78 gl_register_file file, GLint index, GLuint swizzle);
80 static void asm_instruction_set_operands(struct asm_instruction *inst,
81 const struct prog_dst_register *dst, const struct asm_src_register *src0,
82 const struct asm_src_register *src1, const struct asm_src_register *src2);
84 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
85 const struct prog_dst_register *dst, const struct asm_src_register *src0,
86 const struct asm_src_register *src1, const struct asm_src_register *src2);
88 static struct asm_instruction *asm_instruction_copy_ctor(
89 const struct prog_instruction *base, const struct prog_dst_register *dst,
90 const struct asm_src_register *src0, const struct asm_src_register *src1,
91 const struct asm_src_register *src2);
98 #define YYLLOC_DEFAULT(Current, Rhs, N) \
101 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
102 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
103 (Current).position = YYRHSLOC(Rhs, 1).position; \
104 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
105 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
107 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
108 (Current).last_line = (Current).first_line; \
109 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
110 (Current).last_column = (Current).first_column; \
111 (Current).position = YYRHSLOC(Rhs, 0).position \
112 + (Current).first_column; \
116 #define YYLEX_PARAM state->scanner
121 %parse-param { struct asm_parser_state *state }
123 %lex-param { void *scanner }
126 struct asm_instruction *inst;
127 struct asm_symbol *sym;
128 struct asm_symbol temp_sym;
129 struct asm_swizzle_mask swiz_mask;
130 struct asm_src_register src_reg;
131 struct prog_dst_register dst_reg;
132 struct prog_instruction temp_inst;
138 gl_state_index state[STATE_LENGTH];
140 struct asm_vector vector;
141 gl_inst_opcode opcode;
145 unsigned rgba_valid:1;
146 unsigned xyzw_valid:1;
151 %token ARBvp_10 ARBfp_10
153 /* Tokens for assembler pseudo-ops */
154 %token <integer> ADDRESS
158 %token <integer> TEMP
161 /* Tokens for instructions */
162 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
163 %token <temp_inst> ARL KIL SWZ TXD_OP
165 %token <integer> INTEGER
168 %token AMBIENT ATTENUATION
171 %token DEPTH DIFFUSE DIRECTION
172 %token EMISSION ENV EYE
173 %token FOG FOGCOORD FRAGMENT FRONT
175 %token INVERSE INVTRANS
176 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
177 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
180 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
181 %token RANGE RESULT ROW
182 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
183 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
184 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
185 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
186 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
187 %token VERTEX VTXATTRIB
190 %token <string> IDENTIFIER USED_IDENTIFIER
191 %type <string> string
192 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
196 %type <inst> instruction ALU_instruction TexInstruction
197 %type <inst> ARL_instruction VECTORop_instruction
198 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
199 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
200 %type <inst> KIL_instruction
202 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
203 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
204 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
205 %type <ext_swizzle> extSwizComp extSwizSel
206 %type <swiz_mask> optionalMask
208 %type <sym> progParamArray
209 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
210 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
212 %type <swiz_mask> addrComponent addrWriteMask
214 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
216 %type <result> resultBinding resultColBinding
217 %type <integer> optFaceType optColorType
218 %type <integer> optResultFaceType optResultColorType
220 %type <integer> optTexImageUnitNum texImageUnitNum
221 %type <integer> optTexCoordUnitNum texCoordUnitNum
222 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
223 %type <integer> texImageUnit texTarget
224 %type <integer> vtxAttribNum
226 %type <attrib> attribBinding vtxAttribItem fragAttribItem
228 %type <temp_sym> paramSingleInit paramSingleItemDecl
229 %type <integer> optArraySize
231 %type <state> stateSingleItem stateMultipleItem
232 %type <state> stateMaterialItem
233 %type <state> stateLightItem stateLightModelItem stateLightProdItem
234 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
235 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
236 %type <state> stateTexEnvItem stateDepthItem
238 %type <state> stateLModProperty
239 %type <state> stateMatrixName optMatrixRows
241 %type <integer> stateMatProperty
242 %type <integer> stateLightProperty stateSpotProperty
243 %type <integer> stateLightNumber stateLProdProperty
244 %type <integer> stateTexGenType stateTexGenCoord
245 %type <integer> stateTexEnvProperty
246 %type <integer> stateFogProperty
247 %type <integer> stateClipPlaneNum
248 %type <integer> statePointProperty
250 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
251 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
252 %type <integer> stateProgramMatNum
254 %type <integer> ambDiffSpecProperty
256 %type <state> programSingleItem progEnvParam progLocalParam
257 %type <state> programMultipleItem progEnvParams progLocalParams
259 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
260 %type <temp_sym> paramSingleItemUse
262 %type <integer> progEnvParamNum progLocalParamNum
263 %type <state> progEnvParamNums progLocalParamNums
265 %type <vector> paramConstDecl paramConstUse
266 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
267 %type <real> signedFloatConstant
268 %type <negate> optionalSign
271 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
277 program: language optionSequence statementSequence END
282 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
283 yyerror(& @1, state, "invalid fragment program header");
286 state->mode = ARB_vertex;
290 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
291 yyerror(& @1, state, "invalid vertex program header");
293 state->mode = ARB_fragment;
295 state->option.TexRect =
296 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
300 optionSequence: optionSequence option
304 option: OPTION string ';'
308 if (state->mode == ARB_vertex) {
309 valid = _mesa_ARBvp_parse_option(state, $2);
310 } else if (state->mode == ARB_fragment) {
311 valid = _mesa_ARBfp_parse_option(state, $2);
318 const char *const err_str = (state->mode == ARB_vertex)
319 ? "invalid ARB vertex program option"
320 : "invalid ARB fragment program option";
322 yyerror(& @2, state, err_str);
328 statementSequence: statementSequence statement
332 statement: instruction ';'
335 if (state->inst_tail == NULL) {
336 state->inst_head = $1;
338 state->inst_tail->next = $1;
341 state->inst_tail = $1;
344 state->prog->NumInstructions++;
347 | namingStatement ';'
350 instruction: ALU_instruction
353 state->prog->NumAluInstructions++;
358 state->prog->NumTexInstructions++;
362 ALU_instruction: ARL_instruction
363 | VECTORop_instruction
364 | SCALARop_instruction
365 | BINSCop_instruction
371 TexInstruction: SAMPLE_instruction
376 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
378 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
382 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
384 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
388 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
390 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
394 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
396 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
401 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
403 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
407 TRIop_instruction: TRI_OP maskedDstReg ','
408 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
410 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
414 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
416 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
418 const GLbitfield tex_mask = (1U << $6);
419 GLbitfield shadow_tex = 0;
420 GLbitfield target_mask = 0;
423 $$->Base.TexSrcUnit = $6;
426 shadow_tex = tex_mask;
428 $$->Base.TexSrcTarget = -$8;
429 $$->Base.TexShadow = 1;
431 $$->Base.TexSrcTarget = $8;
434 target_mask = (1U << $$->Base.TexSrcTarget);
436 /* If this texture unit was previously accessed and that access
437 * had a different texture target, generate an error.
439 * If this texture unit was previously accessed and that access
440 * had a different shadow mode, generate an error.
442 if ((state->prog->TexturesUsed[$6] != 0)
443 && ((state->prog->TexturesUsed[$6] != target_mask)
444 || ((state->prog->ShadowSamplers & tex_mask)
447 "multiple targets used on one texture image unit");
452 state->prog->TexturesUsed[$6] |= target_mask;
453 state->prog->ShadowSamplers |= shadow_tex;
458 KIL_instruction: KIL swizzleSrcReg
460 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
461 state->fragment.UsesKill = 1;
465 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
466 $$->Base.DstReg.CondMask = $2.CondMask;
467 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
468 $$->Base.DstReg.CondSrc = $2.CondSrc;
469 state->fragment.UsesKill = 1;
473 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
475 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
477 const GLbitfield tex_mask = (1U << $10);
478 GLbitfield shadow_tex = 0;
479 GLbitfield target_mask = 0;
482 $$->Base.TexSrcUnit = $10;
485 shadow_tex = tex_mask;
487 $$->Base.TexSrcTarget = -$12;
488 $$->Base.TexShadow = 1;
490 $$->Base.TexSrcTarget = $12;
493 target_mask = (1U << $$->Base.TexSrcTarget);
495 /* If this texture unit was previously accessed and that access
496 * had a different texture target, generate an error.
498 * If this texture unit was previously accessed and that access
499 * had a different shadow mode, generate an error.
501 if ((state->prog->TexturesUsed[$10] != 0)
502 && ((state->prog->TexturesUsed[$10] != target_mask)
503 || ((state->prog->ShadowSamplers & tex_mask)
505 yyerror(& @12, state,
506 "multiple targets used on one texture image unit");
511 state->prog->TexturesUsed[$10] |= target_mask;
512 state->prog->ShadowSamplers |= shadow_tex;
517 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
523 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
524 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
525 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
526 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
527 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
528 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
529 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
530 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
531 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
532 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
533 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
534 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
537 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
539 /* FIXME: Is this correct? Should the extenedSwizzle be applied
540 * FIXME: to the existing swizzle?
542 $4.Base.Swizzle = $6.swizzle;
543 $4.Base.Negate = $6.mask;
545 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
549 scalarSrcReg: optionalSign scalarUse
554 $$.Base.Negate = ~$$.Base.Negate;
557 | optionalSign '|' scalarUse '|'
561 if (!state->option.NV_fragment) {
562 yyerror(& @2, state, "unexpected character '|'");
567 $$.Base.Negate = ~$$.Base.Negate;
574 scalarUse: srcReg scalarSuffix
578 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
581 | paramConstScalarUse
583 struct asm_symbol temp_sym;
585 if (!state->option.NV_fragment) {
586 yyerror(& @1, state, "expected scalar suffix");
590 memset(& temp_sym, 0, sizeof(temp_sym));
591 temp_sym.param_binding_begin = ~0;
592 initialize_symbol_from_const(state->prog, & temp_sym, & $1);
594 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
595 temp_sym.param_binding_begin,
596 temp_sym.param_binding_swizzle);
600 swizzleSrcReg: optionalSign srcReg swizzleSuffix
605 $$.Base.Negate = ~$$.Base.Negate;
608 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
611 | optionalSign '|' srcReg swizzleSuffix '|'
615 if (!state->option.NV_fragment) {
616 yyerror(& @2, state, "unexpected character '|'");
621 $$.Base.Negate = ~$$.Base.Negate;
625 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
631 maskedDstReg: dstReg optionalMask optionalCcMask
634 $$.WriteMask = $2.mask;
635 $$.CondMask = $3.CondMask;
636 $$.CondSwizzle = $3.CondSwizzle;
637 $$.CondSrc = $3.CondSrc;
639 if ($$.File == PROGRAM_OUTPUT) {
640 /* Technically speaking, this should check that it is in
641 * vertex program mode. However, PositionInvariant can never be
642 * set in fragment program mode, so it is somewhat irrelevant.
644 if (state->option.PositionInvariant
645 && ($$.Index == VERT_RESULT_HPOS)) {
646 yyerror(& @1, state, "position-invariant programs cannot "
651 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
656 maskedAddrReg: addrReg addrWriteMask
658 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
659 $$.WriteMask = $2.mask;
663 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
665 const unsigned xyzw_valid =
667 | ($3.xyzw_valid << 1)
668 | ($5.xyzw_valid << 2)
669 | ($7.xyzw_valid << 3);
670 const unsigned rgba_valid =
672 | ($3.rgba_valid << 1)
673 | ($5.rgba_valid << 2)
674 | ($7.rgba_valid << 3);
676 /* All of the swizzle components have to be valid in either RGBA
677 * or XYZW. Note that 0 and 1 are valid in both, so both masks
678 * can have some bits set.
680 * We somewhat deviate from the spec here. It would be really hard
681 * to figure out which component is the error, and there probably
682 * isn't a lot of benefit.
684 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
685 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
690 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
691 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
696 extSwizComp: optionalSign extSwizSel
699 $$.negate = ($1) ? 1 : 0;
705 if (($1 != 0) && ($1 != 1)) {
706 yyerror(& @1, state, "invalid extended swizzle selector");
710 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
712 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
722 if (strlen($1) > 1) {
723 yyerror(& @1, state, "invalid extended swizzle selector");
766 yyerror(& @1, state, "invalid extended swizzle selector");
773 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
775 struct asm_symbol *const s = (struct asm_symbol *)
776 _mesa_symbol_table_find_symbol(state->st, 0, $1);
781 yyerror(& @1, state, "invalid operand variable");
783 } else if ((s->type != at_param) && (s->type != at_temp)
784 && (s->type != at_attrib)) {
785 yyerror(& @1, state, "invalid operand variable");
787 } else if ((s->type == at_param) && s->param_is_array) {
788 yyerror(& @1, state, "non-array access to array PARAM");
795 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
798 set_src_reg_swz(& $$, s->param_binding_type,
799 s->param_binding_begin,
800 s->param_binding_swizzle);
803 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
804 state->prog->InputsRead |= (1U << $$.Base.Index);
806 if (!validate_inputs(& @1, state)) {
818 set_src_reg(& $$, PROGRAM_INPUT, $1);
819 state->prog->InputsRead |= (1U << $$.Base.Index);
821 if (!validate_inputs(& @1, state)) {
825 | progParamArray '[' progParamArrayMem ']'
827 if (! $3.Base.RelAddr
828 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
829 yyerror(& @3, state, "out of bounds array access");
834 $$.Base.File = $1->param_binding_type;
836 if ($3.Base.RelAddr) {
837 $1->param_accessed_indirectly = 1;
840 $$.Base.Index = $3.Base.Index;
843 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
848 gl_register_file file = ($1.name != NULL)
849 ? $1.param_binding_type
851 set_src_reg_swz(& $$, file, $1.param_binding_begin,
852 $1.param_binding_swizzle);
856 dstReg: resultBinding
858 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
860 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
862 struct asm_symbol *const s = (struct asm_symbol *)
863 _mesa_symbol_table_find_symbol(state->st, 0, $1);
868 yyerror(& @1, state, "invalid operand variable");
870 } else if ((s->type != at_output) && (s->type != at_temp)) {
871 yyerror(& @1, state, "invalid operand variable");
877 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
880 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
883 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
889 progParamArray: USED_IDENTIFIER
891 struct asm_symbol *const s = (struct asm_symbol *)
892 _mesa_symbol_table_find_symbol(state->st, 0, $1);
897 yyerror(& @1, state, "invalid operand variable");
899 } else if ((s->type != at_param) || !s->param_is_array) {
900 yyerror(& @1, state, "array access to non-PARAM variable");
908 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
910 progParamArrayAbs: INTEGER
917 progParamArrayRel: addrReg addrComponent addrRegRelOffset
919 /* FINISHME: Add support for multiple address registers.
921 /* FINISHME: Add support for 4-component address registers.
929 addrRegRelOffset: { $$ = 0; }
930 | '+' addrRegPosOffset { $$ = $2; }
931 | '-' addrRegNegOffset { $$ = -$2; }
934 addrRegPosOffset: INTEGER
936 if (($1 < 0) || ($1 > 63)) {
938 _mesa_snprintf(s, sizeof(s),
939 "relative address offset too large (%d)", $1);
940 yyerror(& @1, state, s);
948 addrRegNegOffset: INTEGER
950 if (($1 < 0) || ($1 > 64)) {
952 _mesa_snprintf(s, sizeof(s),
953 "relative address offset too large (%d)", $1);
954 yyerror(& @1, state, s);
962 addrReg: USED_IDENTIFIER
964 struct asm_symbol *const s = (struct asm_symbol *)
965 _mesa_symbol_table_find_symbol(state->st, 0, $1);
970 yyerror(& @1, state, "invalid array member");
972 } else if (s->type != at_address) {
974 "invalid variable for indexed array access");
984 if ($1.mask != WRITEMASK_X) {
985 yyerror(& @1, state, "invalid address component selector");
995 if ($1.mask != WRITEMASK_X) {
997 "address register write mask must be \".x\"");
1005 scalarSuffix: MASK1;
1007 swizzleSuffix: MASK1
1010 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1013 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1014 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1017 optionalCcMask: '(' ccTest ')'
1027 $$.CondMask = COND_TR;
1028 $$.CondSwizzle = SWIZZLE_NOOP;
1033 ccTest: ccMaskRule swizzleSuffix
1036 $$.CondSwizzle = $2.swizzle;
1040 ccTest2: ccMaskRule2 swizzleSuffix
1043 $$.CondSwizzle = $2.swizzle;
1047 ccMaskRule: 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 ccMaskRule2: USED_IDENTIFIER
1072 const int cond = _mesa_parse_cc($1);
1073 if ((cond == 0) || ($1[2] != '\0')) {
1074 char *const err_str =
1075 make_error_string("invalid condition code \"%s\"", $1);
1077 yyerror(& @1, state, (err_str != NULL)
1078 ? err_str : "invalid condition code");
1080 if (err_str != NULL) {
1081 _mesa_free(err_str);
1088 $$.CondSwizzle = SWIZZLE_NOOP;
1093 namingStatement: ATTRIB_statement
1101 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1103 struct asm_symbol *const s =
1104 declare_variable(state, $2, at_attrib, & @2);
1110 s->attrib_binding = $4;
1111 state->InputsBound |= (1U << s->attrib_binding);
1113 if (!validate_inputs(& @4, state)) {
1120 attribBinding: VERTEX vtxAttribItem
1124 | FRAGMENT fragAttribItem
1130 vtxAttribItem: POSITION
1132 $$ = VERT_ATTRIB_POS;
1134 | WEIGHT vtxOptWeightNum
1136 $$ = VERT_ATTRIB_WEIGHT;
1140 $$ = VERT_ATTRIB_NORMAL;
1142 | COLOR optColorType
1144 if (!state->ctx->Extensions.EXT_secondary_color) {
1145 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1149 $$ = VERT_ATTRIB_COLOR0 + $2;
1153 if (!state->ctx->Extensions.EXT_fog_coord) {
1154 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1158 $$ = VERT_ATTRIB_FOG;
1160 | TEXCOORD optTexCoordUnitNum
1162 $$ = VERT_ATTRIB_TEX0 + $2;
1164 | MATRIXINDEX '[' vtxWeightNum ']'
1166 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1169 | VTXATTRIB '[' vtxAttribNum ']'
1171 $$ = VERT_ATTRIB_GENERIC0 + $3;
1175 vtxAttribNum: INTEGER
1177 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1178 yyerror(& @1, state, "invalid vertex attribute reference");
1186 vtxOptWeightNum: | '[' vtxWeightNum ']';
1187 vtxWeightNum: INTEGER;
1189 fragAttribItem: POSITION
1191 $$ = FRAG_ATTRIB_WPOS;
1193 | COLOR optColorType
1195 $$ = FRAG_ATTRIB_COL0 + $2;
1199 $$ = FRAG_ATTRIB_FOGC;
1201 | TEXCOORD optTexCoordUnitNum
1203 $$ = FRAG_ATTRIB_TEX0 + $2;
1207 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1209 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1211 struct asm_symbol *const s =
1212 declare_variable(state, $2, at_param, & @2);
1218 s->param_binding_type = $3.param_binding_type;
1219 s->param_binding_begin = $3.param_binding_begin;
1220 s->param_binding_length = $3.param_binding_length;
1221 s->param_binding_swizzle = SWIZZLE_XYZW;
1222 s->param_is_array = 0;
1227 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1229 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1231 yyerror(& @4, state,
1232 "parameter array size and number of bindings must match");
1235 struct asm_symbol *const s =
1236 declare_variable(state, $2, $6.type, & @2);
1242 s->param_binding_type = $6.param_binding_type;
1243 s->param_binding_begin = $6.param_binding_begin;
1244 s->param_binding_length = $6.param_binding_length;
1245 s->param_binding_swizzle = SWIZZLE_XYZW;
1246 s->param_is_array = 1;
1258 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1259 yyerror(& @1, state, "invalid parameter array size");
1267 paramSingleInit: '=' paramSingleItemDecl
1273 paramMultipleInit: '=' '{' paramMultInitList '}'
1279 paramMultInitList: paramMultipleItem
1280 | paramMultInitList ',' paramMultipleItem
1282 $1.param_binding_length += $3.param_binding_length;
1287 paramSingleItemDecl: stateSingleItem
1289 memset(& $$, 0, sizeof($$));
1290 $$.param_binding_begin = ~0;
1291 initialize_symbol_from_state(state->prog, & $$, $1);
1295 memset(& $$, 0, sizeof($$));
1296 $$.param_binding_begin = ~0;
1297 initialize_symbol_from_param(state->prog, & $$, $1);
1301 memset(& $$, 0, sizeof($$));
1302 $$.param_binding_begin = ~0;
1303 initialize_symbol_from_const(state->prog, & $$, & $1);
1307 paramSingleItemUse: stateSingleItem
1309 memset(& $$, 0, sizeof($$));
1310 $$.param_binding_begin = ~0;
1311 initialize_symbol_from_state(state->prog, & $$, $1);
1315 memset(& $$, 0, sizeof($$));
1316 $$.param_binding_begin = ~0;
1317 initialize_symbol_from_param(state->prog, & $$, $1);
1321 memset(& $$, 0, sizeof($$));
1322 $$.param_binding_begin = ~0;
1323 initialize_symbol_from_const(state->prog, & $$, & $1);
1327 paramMultipleItem: stateMultipleItem
1329 memset(& $$, 0, sizeof($$));
1330 $$.param_binding_begin = ~0;
1331 initialize_symbol_from_state(state->prog, & $$, $1);
1333 | programMultipleItem
1335 memset(& $$, 0, sizeof($$));
1336 $$.param_binding_begin = ~0;
1337 initialize_symbol_from_param(state->prog, & $$, $1);
1341 memset(& $$, 0, sizeof($$));
1342 $$.param_binding_begin = ~0;
1343 initialize_symbol_from_const(state->prog, & $$, & $1);
1347 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1348 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1351 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1352 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1353 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1354 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1355 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1356 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1357 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1358 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1359 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1360 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1361 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1364 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1366 memset($$, 0, sizeof($$));
1367 $$[0] = STATE_MATERIAL;
1373 stateMatProperty: ambDiffSpecProperty
1379 $$ = STATE_EMISSION;
1383 $$ = STATE_SHININESS;
1387 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1389 memset($$, 0, sizeof($$));
1390 $$[0] = STATE_LIGHT;
1396 stateLightProperty: ambDiffSpecProperty
1402 $$ = STATE_POSITION;
1406 if (!state->ctx->Extensions.EXT_point_parameters) {
1407 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1411 $$ = STATE_ATTENUATION;
1413 | SPOT stateSpotProperty
1419 $$ = STATE_HALF_VECTOR;
1423 stateSpotProperty: DIRECTION
1425 $$ = STATE_SPOT_DIRECTION;
1429 stateLightModelItem: LIGHTMODEL stateLModProperty
1436 stateLModProperty: AMBIENT
1438 memset($$, 0, sizeof($$));
1439 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1441 | optFaceType SCENECOLOR
1443 memset($$, 0, sizeof($$));
1444 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1449 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1451 memset($$, 0, sizeof($$));
1452 $$[0] = STATE_LIGHTPROD;
1459 stateLProdProperty: ambDiffSpecProperty;
1461 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1463 memset($$, 0, sizeof($$));
1469 stateTexEnvProperty: COLOR
1471 $$ = STATE_TEXENV_COLOR;
1475 ambDiffSpecProperty: AMBIENT
1485 $$ = STATE_SPECULAR;
1489 stateLightNumber: INTEGER
1491 if ((unsigned) $1 >= state->MaxLights) {
1492 yyerror(& @1, state, "invalid light selector");
1500 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1502 memset($$, 0, sizeof($$));
1503 $$[0] = STATE_TEXGEN;
1509 stateTexGenType: EYE
1511 $$ = STATE_TEXGEN_EYE_S;
1515 $$ = STATE_TEXGEN_OBJECT_S;
1518 stateTexGenCoord: TEXGEN_S
1520 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1524 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1528 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1532 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1536 stateFogItem: FOG stateFogProperty
1538 memset($$, 0, sizeof($$));
1543 stateFogProperty: COLOR
1545 $$ = STATE_FOG_COLOR;
1549 $$ = STATE_FOG_PARAMS;
1553 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1555 memset($$, 0, sizeof($$));
1556 $$[0] = STATE_CLIPPLANE;
1561 stateClipPlaneNum: INTEGER
1563 if ((unsigned) $1 >= state->MaxClipPlanes) {
1564 yyerror(& @1, state, "invalid clip plane selector");
1572 statePointItem: POINT_TOK statePointProperty
1574 memset($$, 0, sizeof($$));
1579 statePointProperty: SIZE_TOK
1581 $$ = STATE_POINT_SIZE;
1585 $$ = STATE_POINT_ATTENUATION;
1589 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1599 stateMatrixRows: stateMatrixItem optMatrixRows
1614 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1616 /* It seems logical that the matrix row range specifier would have
1617 * to specify a range or more than one row (i.e., $5 > $3).
1618 * However, the ARB_vertex_program spec says "a program will fail
1619 * to load if <a> is greater than <b>." This means that $3 == $5
1623 yyerror(& @3, state, "invalid matrix row range");
1632 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1640 stateOptMatModifier:
1650 stateMatModifier: INVERSE
1652 $$ = STATE_MATRIX_INVERSE;
1656 $$ = STATE_MATRIX_TRANSPOSE;
1660 $$ = STATE_MATRIX_INVTRANS;
1664 stateMatrixRowNum: INTEGER
1667 yyerror(& @1, state, "invalid matrix row reference");
1675 stateMatrixName: MODELVIEW stateOptModMatNum
1677 $$[0] = STATE_MODELVIEW_MATRIX;
1682 $$[0] = STATE_PROJECTION_MATRIX;
1687 $$[0] = STATE_MVP_MATRIX;
1690 | TEXTURE optTexCoordUnitNum
1692 $$[0] = STATE_TEXTURE_MATRIX;
1695 | PALETTE '[' statePaletteMatNum ']'
1697 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1700 | MAT_PROGRAM '[' stateProgramMatNum ']'
1702 $$[0] = STATE_PROGRAM_MATRIX;
1711 | '[' stateModMatNum ']'
1716 stateModMatNum: INTEGER
1718 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1722 yyerror(& @1, state, "invalid modelview matrix index");
1729 statePaletteMatNum: INTEGER
1731 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1732 * through here. The error will be generated later.
1737 stateProgramMatNum: INTEGER
1739 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1740 yyerror(& @1, state, "invalid program matrix selector");
1748 stateDepthItem: DEPTH RANGE
1750 memset($$, 0, sizeof($$));
1751 $$[0] = STATE_DEPTH_RANGE;
1756 programSingleItem: progEnvParam | progLocalParam;
1758 programMultipleItem: progEnvParams | progLocalParams;
1760 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1762 memset($$, 0, sizeof($$));
1763 $$[0] = state->state_param_enum;
1770 progEnvParamNums: progEnvParamNum
1775 | progEnvParamNum DOT_DOT progEnvParamNum
1782 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1784 memset($$, 0, sizeof($$));
1785 $$[0] = state->state_param_enum;
1792 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1794 memset($$, 0, sizeof($$));
1795 $$[0] = state->state_param_enum;
1796 $$[1] = STATE_LOCAL;
1801 progLocalParamNums: progLocalParamNum
1806 | progLocalParamNum DOT_DOT progLocalParamNum
1813 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1815 memset($$, 0, sizeof($$));
1816 $$[0] = state->state_param_enum;
1817 $$[1] = STATE_LOCAL;
1823 progEnvParamNum: INTEGER
1825 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1826 yyerror(& @1, state, "invalid environment parameter reference");
1833 progLocalParamNum: INTEGER
1835 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1836 yyerror(& @1, state, "invalid local parameter reference");
1845 paramConstDecl: paramConstScalarDecl | paramConstVector;
1846 paramConstUse: paramConstScalarUse | paramConstVector;
1848 paramConstScalarDecl: signedFloatConstant
1858 paramConstScalarUse: REAL
1869 $$.data[0] = (float) $1;
1870 $$.data[1] = (float) $1;
1871 $$.data[2] = (float) $1;
1872 $$.data[3] = (float) $1;
1876 paramConstVector: '{' signedFloatConstant '}'
1884 | '{' signedFloatConstant ',' signedFloatConstant '}'
1892 | '{' signedFloatConstant ',' signedFloatConstant ','
1893 signedFloatConstant '}'
1901 | '{' signedFloatConstant ',' signedFloatConstant ','
1902 signedFloatConstant ',' signedFloatConstant '}'
1912 signedFloatConstant: optionalSign REAL
1914 $$ = ($1) ? -$2 : $2;
1916 | optionalSign INTEGER
1918 $$ = (float)(($1) ? -$2 : $2);
1922 optionalSign: '+' { $$ = FALSE; }
1923 | '-' { $$ = TRUE; }
1927 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1932 /* NV_fragment_program_option defines the size qualifiers in a
1933 * fairly broken way. "SHORT" or "LONG" can optionally be used
1934 * before TEMP or OUTPUT. However, neither is a reserved word!
1935 * This means that we have to parse it as an identifier, then check
1936 * to make sure it's one of the valid values. *sigh*
1938 * In addition, the grammar in the extension spec does *not* allow
1939 * the size specifier to be optional, but all known implementations
1942 if (!state->option.NV_fragment) {
1943 yyerror(& @1, state, "unexpected IDENTIFIER");
1947 if (strcmp("SHORT", $1) == 0) {
1948 } else if (strcmp("LONG", $1) == 0) {
1950 char *const err_str =
1951 make_error_string("invalid storage size specifier \"%s\"",
1954 yyerror(& @1, state, (err_str != NULL)
1955 ? err_str : "invalid storage size specifier");
1957 if (err_str != NULL) {
1958 _mesa_free(err_str);
1969 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1972 varNameList: varNameList ',' IDENTIFIER
1974 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1981 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1988 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1990 struct asm_symbol *const s =
1991 declare_variable(state, $3, at_output, & @3);
1997 s->output_binding = $5;
2002 resultBinding: RESULT POSITION
2004 if (state->mode == ARB_vertex) {
2005 $$ = VERT_RESULT_HPOS;
2007 yyerror(& @2, state, "invalid program result name");
2013 if (state->mode == ARB_vertex) {
2014 $$ = VERT_RESULT_FOGC;
2016 yyerror(& @2, state, "invalid program result name");
2020 | RESULT resultColBinding
2026 if (state->mode == ARB_vertex) {
2027 $$ = VERT_RESULT_PSIZ;
2029 yyerror(& @2, state, "invalid program result name");
2033 | RESULT TEXCOORD optTexCoordUnitNum
2035 if (state->mode == ARB_vertex) {
2036 $$ = VERT_RESULT_TEX0 + $3;
2038 yyerror(& @2, state, "invalid program result name");
2044 if (state->mode == ARB_fragment) {
2045 $$ = FRAG_RESULT_DEPTH;
2047 yyerror(& @2, state, "invalid program result name");
2053 resultColBinding: COLOR optResultFaceType optResultColorType
2061 $$ = (state->mode == ARB_vertex)
2063 : FRAG_RESULT_COLOR;
2067 if (state->mode == ARB_vertex) {
2068 $$ = VERT_RESULT_COL0;
2070 yyerror(& @1, state, "invalid program result name");
2076 if (state->mode == ARB_vertex) {
2077 $$ = VERT_RESULT_BFC0;
2079 yyerror(& @1, state, "invalid program result name");
2091 if (state->mode == ARB_vertex) {
2094 yyerror(& @1, state, "invalid program result name");
2100 if (state->mode == ARB_vertex) {
2103 yyerror(& @1, state, "invalid program result name");
2109 optFaceType: { $$ = 0; }
2114 optColorType: { $$ = 0; }
2115 | PRIMARY { $$ = 0; }
2116 | SECONDARY { $$ = 1; }
2119 optTexCoordUnitNum: { $$ = 0; }
2120 | '[' texCoordUnitNum ']' { $$ = $2; }
2123 optTexImageUnitNum: { $$ = 0; }
2124 | '[' texImageUnitNum ']' { $$ = $2; }
2127 optLegacyTexUnitNum: { $$ = 0; }
2128 | '[' legacyTexUnitNum ']' { $$ = $2; }
2131 texCoordUnitNum: INTEGER
2133 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2134 yyerror(& @1, state, "invalid texture coordinate unit selector");
2142 texImageUnitNum: INTEGER
2144 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2145 yyerror(& @1, state, "invalid texture image unit selector");
2153 legacyTexUnitNum: INTEGER
2155 if ((unsigned) $1 >= state->MaxTextureUnits) {
2156 yyerror(& @1, state, "invalid texture unit selector");
2164 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2166 struct asm_symbol *exist = (struct asm_symbol *)
2167 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2168 struct asm_symbol *target = (struct asm_symbol *)
2169 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2173 if (exist != NULL) {
2175 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2177 yyerror(& @2, state, m);
2179 } else if (target == NULL) {
2181 yyerror(& @4, state,
2182 "undefined variable binding in ALIAS statement");
2185 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2197 asm_instruction_set_operands(struct asm_instruction *inst,
2198 const struct prog_dst_register *dst,
2199 const struct asm_src_register *src0,
2200 const struct asm_src_register *src1,
2201 const struct asm_src_register *src2)
2203 /* In the core ARB extensions only the KIL instruction doesn't have a
2204 * destination register.
2207 init_dst_reg(& inst->Base.DstReg);
2209 inst->Base.DstReg = *dst;
2212 /* The only instruction that doesn't have any source registers is the
2213 * condition-code based KIL instruction added by NV_fragment_program_option.
2216 inst->Base.SrcReg[0] = src0->Base;
2217 inst->SrcReg[0] = *src0;
2219 init_src_reg(& inst->SrcReg[0]);
2223 inst->Base.SrcReg[1] = src1->Base;
2224 inst->SrcReg[1] = *src1;
2226 init_src_reg(& inst->SrcReg[1]);
2230 inst->Base.SrcReg[2] = src2->Base;
2231 inst->SrcReg[2] = *src2;
2233 init_src_reg(& inst->SrcReg[2]);
2238 struct asm_instruction *
2239 asm_instruction_ctor(gl_inst_opcode op,
2240 const struct prog_dst_register *dst,
2241 const struct asm_src_register *src0,
2242 const struct asm_src_register *src1,
2243 const struct asm_src_register *src2)
2245 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2248 _mesa_init_instructions(& inst->Base, 1);
2249 inst->Base.Opcode = op;
2251 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2258 struct asm_instruction *
2259 asm_instruction_copy_ctor(const struct prog_instruction *base,
2260 const struct prog_dst_register *dst,
2261 const struct asm_src_register *src0,
2262 const struct asm_src_register *src1,
2263 const struct asm_src_register *src2)
2265 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2268 _mesa_init_instructions(& inst->Base, 1);
2269 inst->Base.Opcode = base->Opcode;
2270 inst->Base.CondUpdate = base->CondUpdate;
2271 inst->Base.CondDst = base->CondDst;
2272 inst->Base.SaturateMode = base->SaturateMode;
2273 inst->Base.Precision = base->Precision;
2275 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2283 init_dst_reg(struct prog_dst_register *r)
2285 memset(r, 0, sizeof(*r));
2286 r->File = PROGRAM_UNDEFINED;
2287 r->WriteMask = WRITEMASK_XYZW;
2288 r->CondMask = COND_TR;
2289 r->CondSwizzle = SWIZZLE_NOOP;
2293 /** Like init_dst_reg() but set the File and Index fields. */
2295 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2297 const GLint maxIndex = 1 << INST_INDEX_BITS;
2298 const GLint minIndex = 0;
2299 ASSERT(index >= minIndex);
2300 ASSERT(index <= maxIndex);
2301 ASSERT(file == PROGRAM_TEMPORARY ||
2302 file == PROGRAM_ADDRESS ||
2303 file == PROGRAM_OUTPUT);
2304 memset(r, 0, sizeof(*r));
2307 r->WriteMask = WRITEMASK_XYZW;
2308 r->CondMask = COND_TR;
2309 r->CondSwizzle = SWIZZLE_NOOP;
2314 init_src_reg(struct asm_src_register *r)
2316 memset(r, 0, sizeof(*r));
2317 r->Base.File = PROGRAM_UNDEFINED;
2318 r->Base.Swizzle = SWIZZLE_NOOP;
2323 /** Like init_src_reg() but set the File and Index fields.
2324 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2327 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2329 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2334 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2337 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2338 const GLint minIndex = -(1 << INST_INDEX_BITS);
2339 ASSERT(file < PROGRAM_FILE_MAX);
2340 ASSERT(index >= minIndex);
2341 ASSERT(index <= maxIndex);
2342 memset(r, 0, sizeof(*r));
2343 r->Base.File = file;
2344 r->Base.Index = index;
2345 r->Base.Swizzle = swizzle;
2351 * Validate the set of inputs used by a program
2353 * Validates that legal sets of inputs are used by the program. In this case
2354 * "used" included both reading the input or binding the input to a name using
2355 * the \c ATTRIB command.
2358 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2361 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2363 const int inputs = state->prog->InputsRead | state->InputsBound;
2365 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
2366 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2375 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2376 struct YYLTYPE *locp)
2378 struct asm_symbol *s = NULL;
2379 struct asm_symbol *exist = (struct asm_symbol *)
2380 _mesa_symbol_table_find_symbol(state->st, 0, name);
2383 if (exist != NULL) {
2384 yyerror(locp, state, "redeclared identifier");
2386 s = calloc(1, sizeof(struct asm_symbol));
2392 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2393 yyerror(locp, state, "too many temporaries declared");
2398 s->temp_binding = state->prog->NumTemporaries;
2399 state->prog->NumTemporaries++;
2403 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2404 yyerror(locp, state, "too many address registers declared");
2409 /* FINISHME: Add support for multiple address registers.
2411 state->prog->NumAddressRegs++;
2418 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2419 s->next = state->sym;
2427 int add_state_reference(struct gl_program_parameter_list *param_list,
2428 const gl_state_index tokens[STATE_LENGTH])
2430 const GLuint size = 4; /* XXX fix */
2434 name = _mesa_program_state_string(tokens);
2435 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2436 size, GL_NONE, NULL, tokens, 0x0);
2437 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2439 /* free name string here since we duplicated it in add_parameter() */
2447 initialize_symbol_from_state(struct gl_program *prog,
2448 struct asm_symbol *param_var,
2449 const gl_state_index tokens[STATE_LENGTH])
2452 gl_state_index state_tokens[STATE_LENGTH];
2455 memcpy(state_tokens, tokens, sizeof(state_tokens));
2457 param_var->type = at_param;
2458 param_var->param_binding_type = PROGRAM_STATE_VAR;
2460 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2461 * unroll it and call add_state_reference() for each row
2463 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2464 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2465 state_tokens[0] == STATE_MVP_MATRIX ||
2466 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2467 state_tokens[0] == STATE_PROGRAM_MATRIX)
2468 && (state_tokens[2] != state_tokens[3])) {
2470 const int first_row = state_tokens[2];
2471 const int last_row = state_tokens[3];
2473 for (row = first_row; row <= last_row; row++) {
2474 state_tokens[2] = state_tokens[3] = row;
2476 idx = add_state_reference(prog->Parameters, state_tokens);
2477 if (param_var->param_binding_begin == ~0U) {
2478 param_var->param_binding_begin = idx;
2479 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2482 param_var->param_binding_length++;
2486 idx = add_state_reference(prog->Parameters, state_tokens);
2487 if (param_var->param_binding_begin == ~0U) {
2488 param_var->param_binding_begin = idx;
2489 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2491 param_var->param_binding_length++;
2499 initialize_symbol_from_param(struct gl_program *prog,
2500 struct asm_symbol *param_var,
2501 const gl_state_index tokens[STATE_LENGTH])
2504 gl_state_index state_tokens[STATE_LENGTH];
2507 memcpy(state_tokens, tokens, sizeof(state_tokens));
2509 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2510 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2511 assert((state_tokens[1] == STATE_ENV)
2512 || (state_tokens[1] == STATE_LOCAL));
2514 param_var->type = at_param;
2515 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2516 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
2518 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2519 * we need to unroll it and call add_state_reference() for each row
2521 if (state_tokens[2] != state_tokens[3]) {
2523 const int first_row = state_tokens[2];
2524 const int last_row = state_tokens[3];
2526 for (row = first_row; row <= last_row; row++) {
2527 state_tokens[2] = state_tokens[3] = row;
2529 idx = add_state_reference(prog->Parameters, state_tokens);
2530 if (param_var->param_binding_begin == ~0U) {
2531 param_var->param_binding_begin = idx;
2532 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2534 param_var->param_binding_length++;
2538 idx = add_state_reference(prog->Parameters, state_tokens);
2539 if (param_var->param_binding_begin == ~0U) {
2540 param_var->param_binding_begin = idx;
2541 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2543 param_var->param_binding_length++;
2551 * Put a float/vector constant/literal into the parameter list.
2552 * \param param_var returns info about the parameter/constant's location,
2553 * binding, type, etc.
2554 * \param vec the vector/constant to add
2555 * \return index of the constant in the parameter list.
2558 initialize_symbol_from_const(struct gl_program *prog,
2559 struct asm_symbol *param_var,
2560 const struct asm_vector *vec)
2563 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2564 vec->data, vec->count, &swizzle);
2566 param_var->type = at_param;
2567 param_var->param_binding_type = PROGRAM_CONSTANT;
2569 if (param_var->param_binding_begin == ~0U) {
2570 param_var->param_binding_begin = idx;
2571 param_var->param_binding_swizzle = swizzle;
2573 param_var->param_binding_length++;
2580 make_error_string(const char *fmt, ...)
2586 va_start(args, fmt);
2588 /* Call vsnprintf once to determine how large the final string is. Call it
2589 * again to do the actual formatting. from the vsnprintf manual page:
2591 * Upon successful return, these functions return the number of
2592 * characters printed (not including the trailing '\0' used to end
2593 * output to strings).
2595 length = 1 + vsnprintf(NULL, 0, fmt, args);
2597 str = _mesa_malloc(length);
2599 vsnprintf(str, length, fmt, args);
2609 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2614 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2616 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2617 _mesa_free(err_str);
2620 err_str = make_error_string("line %u, char %u: error: %s\n",
2621 locp->first_line, locp->first_column, s);
2622 _mesa_set_program_error(state->ctx, locp->position, err_str);
2625 _mesa_free(err_str);
2631 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2632 GLsizei len, struct asm_parser_state *state)
2634 struct asm_instruction *inst;
2637 GLboolean result = GL_FALSE;
2639 struct asm_symbol *sym;
2642 state->prog->Target = target;
2643 state->prog->Parameters = _mesa_new_parameter_list();
2645 /* Make a copy of the program string and force it to be NUL-terminated.
2647 strz = (GLubyte *) _mesa_malloc(len + 1);
2649 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2652 _mesa_memcpy (strz, str, len);
2655 state->prog->String = strz;
2657 state->st = _mesa_symbol_table_ctor();
2659 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2660 ? & ctx->Const.VertexProgram
2661 : & ctx->Const.FragmentProgram;
2663 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2664 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2665 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2666 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2667 state->MaxLights = ctx->Const.MaxLights;
2668 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2670 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2671 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2673 _mesa_set_program_error(ctx, -1, NULL);
2675 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2677 _mesa_program_lexer_dtor(state->scanner);
2680 if (ctx->Program.ErrorPos != -1) {
2684 if (! _mesa_layout_parameters(state)) {
2688 loc.first_column = 0;
2691 yyerror(& loc, state, "invalid PARAM usage");
2697 /* Add one instruction to store the "END" instruction.
2699 state->prog->Instructions =
2700 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2701 inst = state->inst_head;
2702 for (i = 0; i < state->prog->NumInstructions; i++) {
2703 struct asm_instruction *const temp = inst->next;
2705 state->prog->Instructions[i] = inst->Base;
2709 /* Finally, tag on an OPCODE_END instruction */
2711 const GLuint numInst = state->prog->NumInstructions;
2712 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2713 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2715 state->prog->NumInstructions++;
2717 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2718 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
2721 * Initialize native counts to logical counts. The device driver may
2722 * change them if program is translated into a hardware program.
2724 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2725 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2726 state->prog->NumNativeParameters = state->prog->NumParameters;
2727 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2728 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2733 for (inst = state->inst_head; inst != NULL; inst = temp) {
2738 state->inst_head = NULL;
2739 state->inst_tail = NULL;
2741 for (sym = state->sym; sym != NULL; sym = temp) {
2744 _mesa_free((void *) sym->name);
2749 _mesa_symbol_table_dtor(state->st);