3 * Copyright © 2009 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
28 #include "main/mtypes.h"
29 #include "main/imports.h"
31 #include "prog_parameter.h"
32 #include "prog_parameter_layout.h"
33 #include "prog_statevars.h"
34 #include "prog_instruction.h"
36 #include "symbol_table.h"
37 #include "program_parser.h"
39 extern void *yy_scan_string(char *);
40 extern void yy_delete_buffer(void *);
42 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
43 char *name, enum asm_type t, struct YYLTYPE *locp);
45 static int add_state_reference(struct gl_program_parameter_list *param_list,
46 const gl_state_index tokens[STATE_LENGTH]);
48 static int initialize_symbol_from_state(struct gl_program *prog,
49 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
51 static int initialize_symbol_from_param(struct gl_program *prog,
52 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
54 static int initialize_symbol_from_const(struct gl_program *prog,
55 struct asm_symbol *param_var, const struct asm_vector *vec);
57 static int yyparse(struct asm_parser_state *state);
59 static char *make_error_string(const char *fmt, ...);
61 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
64 static int validate_inputs(struct YYLTYPE *locp,
65 struct asm_parser_state *state);
67 static void init_dst_reg(struct prog_dst_register *r);
69 static void init_src_reg(struct asm_src_register *r);
71 static void asm_instruction_set_operands(struct asm_instruction *inst,
72 const struct prog_dst_register *dst, const struct asm_src_register *src0,
73 const struct asm_src_register *src1, const struct asm_src_register *src2);
75 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
76 const struct prog_dst_register *dst, const struct asm_src_register *src0,
77 const struct asm_src_register *src1, const struct asm_src_register *src2);
79 static struct asm_instruction *asm_instruction_copy_ctor(
80 const struct prog_instruction *base, const struct prog_dst_register *dst,
81 const struct asm_src_register *src0, const struct asm_src_register *src1,
82 const struct asm_src_register *src2);
89 #define YYLLOC_DEFAULT(Current, Rhs, N) \
92 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
93 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
94 (Current).position = YYRHSLOC(Rhs, 1).position; \
95 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
96 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
98 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
99 (Current).last_line = (Current).first_line; \
100 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
101 (Current).last_column = (Current).first_column; \
102 (Current).position = YYRHSLOC(Rhs, 0).position \
103 + (Current).first_column; \
107 #define YYLEX_PARAM state->scanner
112 %parse-param { struct asm_parser_state *state }
114 %lex-param { void *scanner }
117 struct asm_instruction *inst;
118 struct asm_symbol *sym;
119 struct asm_symbol temp_sym;
120 struct asm_swizzle_mask swiz_mask;
121 struct asm_src_register src_reg;
122 struct prog_dst_register dst_reg;
123 struct prog_instruction temp_inst;
131 struct asm_vector vector;
132 gl_inst_opcode opcode;
136 unsigned rgba_valid:1;
137 unsigned xyzw_valid:1;
142 %token ARBvp_10 ARBfp_10
144 /* Tokens for assembler pseudo-ops */
145 %token <integer> ADDRESS
149 %token <integer> TEMP
152 /* Tokens for instructions */
153 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
154 %token <temp_inst> ARL KIL SWZ TXD_OP
156 %token <integer> INTEGER
159 %token AMBIENT ATTENUATION
162 %token DEPTH DIFFUSE DIRECTION
163 %token EMISSION ENV EYE
164 %token FOG FOGCOORD FRAGMENT FRONT
166 %token INVERSE INVTRANS
167 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
168 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
171 %token PALETTE PARAMS PLANE POINT POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
172 %token RANGE RESULT ROW
173 %token SCENECOLOR SECONDARY SHININESS SIZE SPECULAR SPOT STATE
174 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
175 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
176 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
177 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
178 %token VERTEX VTXATTRIB
181 %token <string> IDENTIFIER
182 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
186 %type <inst> instruction ALU_instruction TexInstruction
187 %type <inst> ARL_instruction VECTORop_instruction
188 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
189 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
190 %type <inst> KIL_instruction
192 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
193 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
194 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
195 %type <ext_swizzle> extSwizComp extSwizSel
196 %type <swiz_mask> optionalMask
198 %type <sym> progParamArray
199 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
200 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
202 %type <swiz_mask> addrComponent addrWriteMask
204 %type <result> resultBinding resultColBinding
205 %type <integer> optFaceType optColorType
206 %type <integer> optResultFaceType optResultColorType
208 %type <integer> optTexImageUnitNum texImageUnitNum
209 %type <integer> optTexCoordUnitNum texCoordUnitNum
210 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
211 %type <integer> texImageUnit texTarget
212 %type <integer> vtxAttribNum
214 %type <attrib> attribBinding vtxAttribItem fragAttribItem
216 %type <temp_sym> paramSingleInit paramSingleItemDecl
217 %type <integer> optArraySize
219 %type <state> stateSingleItem stateMultipleItem
220 %type <state> stateMaterialItem
221 %type <state> stateLightItem stateLightModelItem stateLightProdItem
222 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
223 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
224 %type <state> stateTexEnvItem stateDepthItem
226 %type <state> stateLModProperty
227 %type <state> stateMatrixName optMatrixRows
229 %type <integer> stateMatProperty
230 %type <integer> stateLightProperty stateSpotProperty
231 %type <integer> stateLightNumber stateLProdProperty
232 %type <integer> stateTexGenType stateTexGenCoord
233 %type <integer> stateTexEnvProperty
234 %type <integer> stateFogProperty
235 %type <integer> stateClipPlaneNum
236 %type <integer> statePointProperty
238 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
239 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
240 %type <integer> stateProgramMatNum
242 %type <integer> ambDiffSpecProperty
244 %type <state> programSingleItem progEnvParam progLocalParam
245 %type <state> programMultipleItem progEnvParams progLocalParams
247 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
248 %type <temp_sym> paramSingleItemUse
250 %type <integer> progEnvParamNum progLocalParamNum
251 %type <state> progEnvParamNums progLocalParamNums
253 %type <vector> paramConstDecl paramConstUse
254 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
255 %type <real> signedFloatConstant
256 %type <negate> optionalSign
259 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
265 program: language optionSequence statementSequence END
270 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
271 yyerror(& @1, state, "invalid fragment program header");
274 state->mode = ARB_vertex;
278 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
279 yyerror(& @1, state, "invalid vertex program header");
281 state->mode = ARB_fragment;
283 state->option.TexRect =
284 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
288 optionSequence: optionSequence option
292 option: OPTION IDENTIFIER ';'
296 if (state->mode == ARB_vertex) {
297 valid = _mesa_ARBvp_parse_option(state, $2);
298 } else if (state->mode == ARB_fragment) {
299 valid = _mesa_ARBfp_parse_option(state, $2);
304 const char *const err_str = (state->mode == ARB_vertex)
305 ? "invalid ARB vertex program option"
306 : "invalid ARB fragment program option";
308 yyerror(& @2, state, err_str);
314 statementSequence: statementSequence statement
318 statement: instruction ';'
321 if (state->inst_tail == NULL) {
322 state->inst_head = $1;
324 state->inst_tail->next = $1;
327 state->inst_tail = $1;
330 state->prog->NumInstructions++;
333 | namingStatement ';'
336 instruction: ALU_instruction
339 state->prog->NumAluInstructions++;
344 state->prog->NumTexInstructions++;
348 ALU_instruction: ARL_instruction
349 | VECTORop_instruction
350 | SCALARop_instruction
351 | BINSCop_instruction
357 TexInstruction: SAMPLE_instruction
362 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
364 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
368 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
370 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
374 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
376 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
380 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
382 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
387 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
389 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
393 TRIop_instruction: TRI_OP maskedDstReg ','
394 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
396 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
400 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
402 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
404 const GLbitfield tex_mask = (1U << $6);
405 GLbitfield shadow_tex = 0;
406 GLbitfield target_mask = 0;
409 $$->Base.TexSrcUnit = $6;
412 shadow_tex = tex_mask;
414 $$->Base.TexSrcTarget = -$8;
415 $$->Base.TexShadow = 1;
417 $$->Base.TexSrcTarget = $8;
420 target_mask = (1U << $$->Base.TexSrcTarget);
422 /* If this texture unit was previously accessed and that access
423 * had a different texture target, generate an error.
425 * If this texture unit was previously accessed and that access
426 * had a different shadow mode, generate an error.
428 if ((state->prog->TexturesUsed[$6] != 0)
429 && ((state->prog->TexturesUsed[$6] != target_mask)
430 || ((state->prog->ShadowSamplers & tex_mask)
433 "multiple targets used on one texture image unit");
438 state->prog->TexturesUsed[$6] |= target_mask;
439 state->prog->ShadowSamplers |= shadow_tex;
444 KIL_instruction: KIL swizzleSrcReg
446 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
447 state->fragment.UsesKill = 1;
451 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
453 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
455 const GLbitfield tex_mask = (1U << $10);
456 GLbitfield shadow_tex = 0;
457 GLbitfield target_mask = 0;
460 $$->Base.TexSrcUnit = $10;
463 shadow_tex = tex_mask;
465 $$->Base.TexSrcTarget = -$12;
466 $$->Base.TexShadow = 1;
468 $$->Base.TexSrcTarget = $12;
471 target_mask = (1U << $$->Base.TexSrcTarget);
473 /* If this texture unit was previously accessed and that access
474 * had a different texture target, generate an error.
476 * If this texture unit was previously accessed and that access
477 * had a different shadow mode, generate an error.
479 if ((state->prog->TexturesUsed[$10] != 0)
480 && ((state->prog->TexturesUsed[$10] != target_mask)
481 || ((state->prog->ShadowSamplers & tex_mask)
483 yyerror(& @12, state,
484 "multiple targets used on one texture image unit");
489 state->prog->TexturesUsed[$10] |= target_mask;
490 state->prog->ShadowSamplers |= shadow_tex;
495 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
501 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
502 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
503 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
504 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
505 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
506 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
507 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
508 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
509 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
510 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
511 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
512 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
515 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
517 /* FIXME: Is this correct? Should the extenedSwizzle be applied
518 * FIXME: to the existing swizzle?
520 $4.Base.Swizzle = $6.swizzle;
521 $4.Base.Negate = $6.mask;
523 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
527 scalarSrcReg: optionalSign scalarUse
532 $$.Base.Negate = ~$$.Base.Negate;
535 | optionalSign '|' scalarUse '|'
539 if (!state->option.NV_fragment) {
540 yyerror(& @2, state, "unexpected character '|'");
545 $$.Base.Negate = ~$$.Base.Negate;
552 scalarUse: srcReg scalarSuffix
556 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
559 | paramConstScalarUse
561 struct asm_symbol temp_sym;
563 if (!state->option.NV_fragment) {
564 yyerror(& @1, state, "expected scalar suffix");
568 memset(& temp_sym, 0, sizeof(temp_sym));
569 temp_sym.param_binding_begin = ~0;
570 initialize_symbol_from_const(state->prog, & temp_sym, & $1);
573 $$.Base.File = PROGRAM_CONSTANT;
574 $$.Base.Index = temp_sym.param_binding_begin;
578 swizzleSrcReg: optionalSign srcReg swizzleSuffix
583 $$.Base.Negate = ~$$.Base.Negate;
586 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
589 | optionalSign '|' srcReg swizzleSuffix '|'
593 if (!state->option.NV_fragment) {
594 yyerror(& @2, state, "unexpected character '|'");
599 $$.Base.Negate = ~$$.Base.Negate;
603 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
609 maskedDstReg: dstReg optionalMask
612 $$.WriteMask = $2.mask;
614 if ($$.File == PROGRAM_OUTPUT) {
615 /* Technically speaking, this should check that it is in
616 * vertex program mode. However, PositionInvariant can never be
617 * set in fragment program mode, so it is somewhat irrelevant.
619 if (state->option.PositionInvariant
620 && ($$.Index == VERT_RESULT_HPOS)) {
621 yyerror(& @1, state, "position-invariant programs cannot "
626 state->prog->OutputsWritten |= (1U << $$.Index);
631 maskedAddrReg: addrReg addrWriteMask
634 $$.File = PROGRAM_ADDRESS;
636 $$.WriteMask = $2.mask;
640 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
642 const unsigned xyzw_valid =
644 | ($3.xyzw_valid << 1)
645 | ($5.xyzw_valid << 2)
646 | ($7.xyzw_valid << 3);
647 const unsigned rgba_valid =
649 | ($3.rgba_valid << 1)
650 | ($5.rgba_valid << 2)
651 | ($7.rgba_valid << 3);
653 /* All of the swizzle components have to be valid in either RGBA
654 * or XYZW. Note that 0 and 1 are valid in both, so both masks
655 * can have some bits set.
657 * We somewhat deviate from the spec here. It would be really hard
658 * to figure out which component is the error, and there probably
659 * isn't a lot of benefit.
661 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
662 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
667 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
668 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
673 extSwizComp: optionalSign extSwizSel
676 $$.negate = ($1) ? 1 : 0;
682 if (($1 != 0) && ($1 != 1)) {
683 yyerror(& @1, state, "invalid extended swizzle selector");
687 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
689 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
697 if (strlen($1) > 1) {
698 yyerror(& @1, state, "invalid extended swizzle selector");
738 yyerror(& @1, state, "invalid extended swizzle selector");
745 srcReg: IDENTIFIER /* temporaryReg | progParamSingle */
747 struct asm_symbol *const s = (struct asm_symbol *)
748 _mesa_symbol_table_find_symbol(state->st, 0, $1);
751 yyerror(& @1, state, "invalid operand variable");
753 } else if ((s->type != at_param) && (s->type != at_temp)
754 && (s->type != at_attrib)) {
755 yyerror(& @1, state, "invalid operand variable");
757 } else if ((s->type == at_param) && s->param_is_array) {
758 yyerror(& @1, state, "non-array access to array PARAM");
765 $$.Base.File = PROGRAM_TEMPORARY;
766 $$.Base.Index = s->temp_binding;
769 $$.Base.File = s->param_binding_type;
770 $$.Base.Index = s->param_binding_begin;
773 $$.Base.File = PROGRAM_INPUT;
774 $$.Base.Index = s->attrib_binding;
775 state->prog->InputsRead |= (1U << $$.Base.Index);
777 if (!validate_inputs(& @1, state)) {
790 $$.Base.File = PROGRAM_INPUT;
792 state->prog->InputsRead |= (1U << $$.Base.Index);
794 if (!validate_inputs(& @1, state)) {
798 | progParamArray '[' progParamArrayMem ']'
800 if (! $3.Base.RelAddr
801 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
802 yyerror(& @3, state, "out of bounds array access");
807 $$.Base.File = $1->param_binding_type;
809 if ($3.Base.RelAddr) {
810 $1->param_accessed_indirectly = 1;
813 $$.Base.Index = $3.Base.Index;
816 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
822 $$.Base.File = ($1.name != NULL)
823 ? $1.param_binding_type
825 $$.Base.Index = $1.param_binding_begin;
829 dstReg: resultBinding
832 $$.File = PROGRAM_OUTPUT;
835 | IDENTIFIER /* temporaryReg | vertexResultReg */
837 struct asm_symbol *const s = (struct asm_symbol *)
838 _mesa_symbol_table_find_symbol(state->st, 0, $1);
841 yyerror(& @1, state, "invalid operand variable");
843 } else if ((s->type != at_output) && (s->type != at_temp)) {
844 yyerror(& @1, state, "invalid operand variable");
851 $$.File = PROGRAM_TEMPORARY;
852 $$.Index = s->temp_binding;
855 $$.File = PROGRAM_OUTPUT;
856 $$.Index = s->output_binding;
859 $$.File = s->param_binding_type;
860 $$.Index = s->param_binding_begin;
866 progParamArray: IDENTIFIER
868 struct asm_symbol *const s = (struct asm_symbol *)
869 _mesa_symbol_table_find_symbol(state->st, 0, $1);
872 yyerror(& @1, state, "invalid operand variable");
874 } else if ((s->type != at_param) || !s->param_is_array) {
875 yyerror(& @1, state, "array access to non-PARAM variable");
883 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
885 progParamArrayAbs: INTEGER
892 progParamArrayRel: addrReg addrComponent addrRegRelOffset
894 /* FINISHME: Add support for multiple address registers.
896 /* FINISHME: Add support for 4-component address registers.
904 addrRegRelOffset: { $$ = 0; }
905 | '+' addrRegPosOffset { $$ = $2; }
906 | '-' addrRegNegOffset { $$ = -$2; }
909 addrRegPosOffset: INTEGER
911 if (($1 < 0) || ($1 > 63)) {
913 "relative address offset too large (positive)");
921 addrRegNegOffset: INTEGER
923 if (($1 < 0) || ($1 > 64)) {
925 "relative address offset too large (negative)");
935 struct asm_symbol *const s = (struct asm_symbol *)
936 _mesa_symbol_table_find_symbol(state->st, 0, $1);
939 yyerror(& @1, state, "invalid array member");
941 } else if (s->type != at_address) {
943 "invalid variable for indexed array access");
953 if ($1.mask != WRITEMASK_X) {
954 yyerror(& @1, state, "invalid address component selector");
964 if ($1.mask != WRITEMASK_X) {
966 "address register write mask must be \".x\"");
979 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
982 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
983 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
986 namingStatement: ATTRIB_statement
994 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
996 struct asm_symbol *const s =
997 declare_variable(state, $2, at_attrib, & @2);
1002 s->attrib_binding = $4;
1003 state->InputsBound |= (1U << s->attrib_binding);
1005 if (!validate_inputs(& @4, state)) {
1012 attribBinding: VERTEX vtxAttribItem
1016 | FRAGMENT fragAttribItem
1022 vtxAttribItem: POSITION
1024 $$ = VERT_ATTRIB_POS;
1026 | WEIGHT vtxOptWeightNum
1028 $$ = VERT_ATTRIB_WEIGHT;
1032 $$ = VERT_ATTRIB_NORMAL;
1034 | COLOR optColorType
1036 if (!state->ctx->Extensions.EXT_secondary_color) {
1037 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1041 $$ = VERT_ATTRIB_COLOR0 + $2;
1045 if (!state->ctx->Extensions.EXT_fog_coord) {
1046 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1050 $$ = VERT_ATTRIB_FOG;
1052 | TEXCOORD optTexCoordUnitNum
1054 $$ = VERT_ATTRIB_TEX0 + $2;
1056 | MATRIXINDEX '[' vtxWeightNum ']'
1058 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1061 | VTXATTRIB '[' vtxAttribNum ']'
1063 $$ = VERT_ATTRIB_GENERIC0 + $3;
1067 vtxAttribNum: INTEGER
1069 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1070 yyerror(& @1, state, "invalid vertex attribute reference");
1078 vtxOptWeightNum: | '[' vtxWeightNum ']';
1079 vtxWeightNum: INTEGER;
1081 fragAttribItem: POSITION
1083 $$ = FRAG_ATTRIB_WPOS;
1085 | COLOR optColorType
1087 $$ = FRAG_ATTRIB_COL0 + $2;
1091 $$ = FRAG_ATTRIB_FOGC;
1093 | TEXCOORD optTexCoordUnitNum
1095 $$ = FRAG_ATTRIB_TEX0 + $2;
1099 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1101 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1103 struct asm_symbol *const s =
1104 declare_variable(state, $2, at_param, & @2);
1109 s->param_binding_type = $3.param_binding_type;
1110 s->param_binding_begin = $3.param_binding_begin;
1111 s->param_binding_length = $3.param_binding_length;
1112 s->param_is_array = 0;
1117 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1119 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1120 yyerror(& @4, state,
1121 "parameter array size and number of bindings must match");
1124 struct asm_symbol *const s =
1125 declare_variable(state, $2, $6.type, & @2);
1130 s->param_binding_type = $6.param_binding_type;
1131 s->param_binding_begin = $6.param_binding_begin;
1132 s->param_binding_length = $6.param_binding_length;
1133 s->param_is_array = 1;
1145 if (($1 < 1) || ((unsigned) $1 >= state->limits->MaxParameters)) {
1146 yyerror(& @1, state, "invalid parameter array size");
1154 paramSingleInit: '=' paramSingleItemDecl
1160 paramMultipleInit: '=' '{' paramMultInitList '}'
1166 paramMultInitList: paramMultipleItem
1167 | paramMultInitList ',' paramMultipleItem
1169 $1.param_binding_length += $3.param_binding_length;
1174 paramSingleItemDecl: stateSingleItem
1176 memset(& $$, 0, sizeof($$));
1177 $$.param_binding_begin = ~0;
1178 initialize_symbol_from_state(state->prog, & $$, $1);
1182 memset(& $$, 0, sizeof($$));
1183 $$.param_binding_begin = ~0;
1184 initialize_symbol_from_param(state->prog, & $$, $1);
1188 memset(& $$, 0, sizeof($$));
1189 $$.param_binding_begin = ~0;
1190 initialize_symbol_from_const(state->prog, & $$, & $1);
1194 paramSingleItemUse: stateSingleItem
1196 memset(& $$, 0, sizeof($$));
1197 $$.param_binding_begin = ~0;
1198 initialize_symbol_from_state(state->prog, & $$, $1);
1202 memset(& $$, 0, sizeof($$));
1203 $$.param_binding_begin = ~0;
1204 initialize_symbol_from_param(state->prog, & $$, $1);
1208 memset(& $$, 0, sizeof($$));
1209 $$.param_binding_begin = ~0;
1210 initialize_symbol_from_const(state->prog, & $$, & $1);
1214 paramMultipleItem: stateMultipleItem
1216 memset(& $$, 0, sizeof($$));
1217 $$.param_binding_begin = ~0;
1218 initialize_symbol_from_state(state->prog, & $$, $1);
1220 | programMultipleItem
1222 memset(& $$, 0, sizeof($$));
1223 $$.param_binding_begin = ~0;
1224 initialize_symbol_from_param(state->prog, & $$, $1);
1228 memset(& $$, 0, sizeof($$));
1229 $$.param_binding_begin = ~0;
1230 initialize_symbol_from_const(state->prog, & $$, & $1);
1234 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1235 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1238 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1239 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1240 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1241 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1242 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1243 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1244 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1245 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1246 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1247 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1248 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1251 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1253 memset($$, 0, sizeof($$));
1254 $$[0] = STATE_MATERIAL;
1260 stateMatProperty: ambDiffSpecProperty
1266 $$ = STATE_EMISSION;
1270 $$ = STATE_SHININESS;
1274 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1276 memset($$, 0, sizeof($$));
1277 $$[0] = STATE_LIGHT;
1283 stateLightProperty: ambDiffSpecProperty
1289 $$ = STATE_POSITION;
1293 if (!state->ctx->Extensions.EXT_point_parameters) {
1294 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1298 $$ = STATE_ATTENUATION;
1300 | SPOT stateSpotProperty
1306 $$ = STATE_HALF_VECTOR;
1310 stateSpotProperty: DIRECTION
1312 $$ = STATE_SPOT_DIRECTION;
1316 stateLightModelItem: LIGHTMODEL stateLModProperty
1323 stateLModProperty: AMBIENT
1325 memset($$, 0, sizeof($$));
1326 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1328 | optFaceType SCENECOLOR
1330 memset($$, 0, sizeof($$));
1331 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1336 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1338 memset($$, 0, sizeof($$));
1339 $$[0] = STATE_LIGHTPROD;
1346 stateLProdProperty: ambDiffSpecProperty;
1348 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1350 memset($$, 0, sizeof($$));
1356 stateTexEnvProperty: COLOR
1358 $$ = STATE_TEXENV_COLOR;
1362 ambDiffSpecProperty: AMBIENT
1372 $$ = STATE_SPECULAR;
1376 stateLightNumber: INTEGER
1378 if ((unsigned) $1 >= state->MaxLights) {
1379 yyerror(& @1, state, "invalid light selector");
1387 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1389 memset($$, 0, sizeof($$));
1390 $$[0] = STATE_TEXGEN;
1396 stateTexGenType: EYE
1398 $$ = STATE_TEXGEN_EYE_S;
1402 $$ = STATE_TEXGEN_OBJECT_S;
1405 stateTexGenCoord: TEXGEN_S
1407 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1411 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1415 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1419 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1423 stateFogItem: FOG stateFogProperty
1425 memset($$, 0, sizeof($$));
1430 stateFogProperty: COLOR
1432 $$ = STATE_FOG_COLOR;
1436 $$ = STATE_FOG_PARAMS;
1440 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1442 memset($$, 0, sizeof($$));
1443 $$[0] = STATE_CLIPPLANE;
1448 stateClipPlaneNum: INTEGER
1450 if ((unsigned) $1 >= state->MaxClipPlanes) {
1451 yyerror(& @1, state, "invalid clip plane selector");
1459 statePointItem: POINT statePointProperty
1461 memset($$, 0, sizeof($$));
1466 statePointProperty: SIZE
1468 $$ = STATE_POINT_SIZE;
1472 $$ = STATE_POINT_ATTENUATION;
1476 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1486 stateMatrixRows: stateMatrixItem optMatrixRows
1501 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1503 /* It seems logical that the matrix row range specifier would have
1504 * to specify a range or more than one row (i.e., $5 > $3).
1505 * However, the ARB_vertex_program spec says "a program will fail
1506 * to load if <a> is greater than <b>." This means that $3 == $5
1510 yyerror(& @3, state, "invalid matrix row range");
1519 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1527 stateOptMatModifier:
1537 stateMatModifier: INVERSE
1539 $$ = STATE_MATRIX_INVERSE;
1543 $$ = STATE_MATRIX_TRANSPOSE;
1547 $$ = STATE_MATRIX_INVTRANS;
1551 stateMatrixRowNum: INTEGER
1554 yyerror(& @1, state, "invalid matrix row reference");
1562 stateMatrixName: MODELVIEW stateOptModMatNum
1564 $$[0] = STATE_MODELVIEW_MATRIX;
1569 $$[0] = STATE_PROJECTION_MATRIX;
1574 $$[0] = STATE_MVP_MATRIX;
1577 | TEXTURE optTexCoordUnitNum
1579 $$[0] = STATE_TEXTURE_MATRIX;
1582 | PALETTE '[' statePaletteMatNum ']'
1584 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1587 | MAT_PROGRAM '[' stateProgramMatNum ']'
1589 $$[0] = STATE_PROGRAM_MATRIX;
1603 stateModMatNum: INTEGER
1605 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1609 yyerror(& @1, state, "invalid modelview matrix index");
1616 statePaletteMatNum: INTEGER
1618 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1619 * through here. The error will be generated later.
1624 stateProgramMatNum: INTEGER
1626 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1627 yyerror(& @1, state, "invalid program matrix selector");
1635 stateDepthItem: DEPTH RANGE
1637 memset($$, 0, sizeof($$));
1638 $$[0] = STATE_DEPTH_RANGE;
1643 programSingleItem: progEnvParam | progLocalParam;
1645 programMultipleItem: progEnvParams | progLocalParams;
1647 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1649 memset($$, 0, sizeof($$));
1650 $$[0] = state->state_param_enum;
1657 progEnvParamNums: progEnvParamNum
1662 | progEnvParamNum DOT_DOT progEnvParamNum
1669 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1671 memset($$, 0, sizeof($$));
1672 $$[0] = state->state_param_enum;
1679 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1681 memset($$, 0, sizeof($$));
1682 $$[0] = state->state_param_enum;
1683 $$[1] = STATE_LOCAL;
1688 progLocalParamNums: progLocalParamNum
1693 | progLocalParamNum DOT_DOT progLocalParamNum
1700 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1702 memset($$, 0, sizeof($$));
1703 $$[0] = state->state_param_enum;
1704 $$[1] = STATE_LOCAL;
1710 progEnvParamNum: INTEGER
1712 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1713 yyerror(& @1, state, "invalid environment parameter reference");
1720 progLocalParamNum: INTEGER
1722 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1723 yyerror(& @1, state, "invalid local parameter reference");
1732 paramConstDecl: paramConstScalarDecl | paramConstVector;
1733 paramConstUse: paramConstScalarUse | paramConstVector;
1735 paramConstScalarDecl: signedFloatConstant
1745 paramConstScalarUse: REAL
1756 $$.data[0] = (float) $1;
1757 $$.data[1] = (float) $1;
1758 $$.data[2] = (float) $1;
1759 $$.data[3] = (float) $1;
1763 paramConstVector: '{' signedFloatConstant '}'
1771 | '{' signedFloatConstant ',' signedFloatConstant '}'
1779 | '{' signedFloatConstant ',' signedFloatConstant ','
1780 signedFloatConstant '}'
1788 | '{' signedFloatConstant ',' signedFloatConstant ','
1789 signedFloatConstant ',' signedFloatConstant '}'
1799 signedFloatConstant: optionalSign REAL
1801 $$ = ($1) ? -$2 : $2;
1803 | optionalSign INTEGER
1805 $$ = (float)(($1) ? -$2 : $2);
1809 optionalSign: '+' { $$ = FALSE; }
1810 | '-' { $$ = TRUE; }
1814 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1817 optVarSize: IDENTIFIER
1819 /* NV_fragment_program_option defines the size qualifiers in a
1820 * fairly broken way. "SHORT" or "LONG" can optionally be used
1821 * before TEMP or OUTPUT. However, neither is a reserved word!
1822 * This means that we have to parse it as an identifier, then check
1823 * to make sure it's one of the valid values. *sigh*
1825 * In addition, the grammar in the extension spec does *not* allow
1826 * the size specifier to be optional, but all known implementations
1829 if (!state->option.NV_fragment) {
1830 yyerror(& @1, state, "unexpected IDENTIFIER");
1834 if (strcmp("SHORT", $1) == 0) {
1835 } else if (strcmp("LONG", $1) == 0) {
1837 char *const err_str =
1838 make_error_string("invalid storage size specifier \"%s\"",
1841 yyerror(& @1, state, (err_str != NULL)
1842 ? err_str : "invalid storage size specifier");
1844 if (err_str != NULL) {
1845 _mesa_free(err_str);
1856 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1859 varNameList: varNameList ',' IDENTIFIER
1861 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1867 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1873 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1875 struct asm_symbol *const s =
1876 declare_variable(state, $3, at_output, & @3);
1881 s->output_binding = $5;
1886 resultBinding: RESULT POSITION
1888 if (state->mode == ARB_vertex) {
1889 $$ = VERT_RESULT_HPOS;
1891 yyerror(& @2, state, "invalid program result name");
1897 if (state->mode == ARB_vertex) {
1898 $$ = VERT_RESULT_FOGC;
1900 yyerror(& @2, state, "invalid program result name");
1904 | RESULT resultColBinding
1910 if (state->mode == ARB_vertex) {
1911 $$ = VERT_RESULT_PSIZ;
1913 yyerror(& @2, state, "invalid program result name");
1917 | RESULT TEXCOORD optTexCoordUnitNum
1919 if (state->mode == ARB_vertex) {
1920 $$ = VERT_RESULT_TEX0 + $3;
1922 yyerror(& @2, state, "invalid program result name");
1928 if (state->mode == ARB_fragment) {
1929 $$ = FRAG_RESULT_DEPTH;
1931 yyerror(& @2, state, "invalid program result name");
1937 resultColBinding: COLOR optResultFaceType optResultColorType
1945 $$ = (state->mode == ARB_vertex)
1947 : FRAG_RESULT_COLOR;
1951 if (state->mode == ARB_vertex) {
1952 $$ = VERT_RESULT_COL0;
1954 yyerror(& @1, state, "invalid program result name");
1960 if (state->mode == ARB_vertex) {
1961 $$ = VERT_RESULT_BFC0;
1963 yyerror(& @1, state, "invalid program result name");
1975 if (state->mode == ARB_vertex) {
1978 yyerror(& @1, state, "invalid program result name");
1984 if (state->mode == ARB_vertex) {
1987 yyerror(& @1, state, "invalid program result name");
1993 optFaceType: { $$ = 0; }
1998 optColorType: { $$ = 0; }
1999 | PRIMARY { $$ = 0; }
2000 | SECONDARY { $$ = 1; }
2003 optTexCoordUnitNum: { $$ = 0; }
2004 | '[' texCoordUnitNum ']' { $$ = $2; }
2007 optTexImageUnitNum: { $$ = 0; }
2008 | '[' texImageUnitNum ']' { $$ = $2; }
2011 optLegacyTexUnitNum: { $$ = 0; }
2012 | '[' legacyTexUnitNum ']' { $$ = $2; }
2015 texCoordUnitNum: INTEGER
2017 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2018 yyerror(& @1, state, "invalid texture coordinate unit selector");
2026 texImageUnitNum: INTEGER
2028 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2029 yyerror(& @1, state, "invalid texture image unit selector");
2037 legacyTexUnitNum: INTEGER
2039 if ((unsigned) $1 >= state->MaxTextureUnits) {
2040 yyerror(& @1, state, "invalid texture unit selector");
2048 ALIAS_statement: ALIAS IDENTIFIER '=' IDENTIFIER
2050 struct asm_symbol *exist = (struct asm_symbol *)
2051 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2052 struct asm_symbol *target = (struct asm_symbol *)
2053 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2056 if (exist != NULL) {
2057 yyerror(& @2, state, "redeclared identifier");
2059 } else if (target == NULL) {
2060 yyerror(& @4, state,
2061 "undefined variable binding in ALIAS statement");
2064 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2072 asm_instruction_set_operands(struct asm_instruction *inst,
2073 const struct prog_dst_register *dst,
2074 const struct asm_src_register *src0,
2075 const struct asm_src_register *src1,
2076 const struct asm_src_register *src2)
2078 /* In the core ARB extensions only the KIL instruction doesn't have a
2079 * destination register.
2082 init_dst_reg(& inst->Base.DstReg);
2084 inst->Base.DstReg = *dst;
2087 inst->Base.SrcReg[0] = src0->Base;
2088 inst->SrcReg[0] = *src0;
2091 inst->Base.SrcReg[1] = src1->Base;
2092 inst->SrcReg[1] = *src1;
2094 init_src_reg(& inst->SrcReg[1]);
2098 inst->Base.SrcReg[2] = src2->Base;
2099 inst->SrcReg[2] = *src2;
2101 init_src_reg(& inst->SrcReg[2]);
2106 struct asm_instruction *
2107 asm_instruction_ctor(gl_inst_opcode op,
2108 const struct prog_dst_register *dst,
2109 const struct asm_src_register *src0,
2110 const struct asm_src_register *src1,
2111 const struct asm_src_register *src2)
2113 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2116 _mesa_init_instructions(& inst->Base, 1);
2117 inst->Base.Opcode = op;
2119 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2126 struct asm_instruction *
2127 asm_instruction_copy_ctor(const struct prog_instruction *base,
2128 const struct prog_dst_register *dst,
2129 const struct asm_src_register *src0,
2130 const struct asm_src_register *src1,
2131 const struct asm_src_register *src2)
2133 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2136 _mesa_init_instructions(& inst->Base, 1);
2137 inst->Base.Opcode = base->Opcode;
2138 inst->Base.CondUpdate = base->CondUpdate;
2139 inst->Base.CondDst = base->CondDst;
2140 inst->Base.SaturateMode = base->SaturateMode;
2141 inst->Base.Precision = base->Precision;
2143 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2151 init_dst_reg(struct prog_dst_register *r)
2153 memset(r, 0, sizeof(*r));
2154 r->File = PROGRAM_UNDEFINED;
2155 r->WriteMask = WRITEMASK_XYZW;
2156 r->CondMask = COND_TR;
2157 r->CondSwizzle = SWIZZLE_NOOP;
2162 init_src_reg(struct asm_src_register *r)
2164 memset(r, 0, sizeof(*r));
2165 r->Base.File = PROGRAM_UNDEFINED;
2166 r->Base.Swizzle = SWIZZLE_NOOP;
2172 * Validate the set of inputs used by a program
2174 * Validates that legal sets of inputs are used by the program. In this case
2175 * "used" included both reading the input or binding the input to a name using
2176 * the \c ATTRIB command.
2179 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2182 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2184 const int inputs = state->prog->InputsRead | state->InputsBound;
2186 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
2187 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2196 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2197 struct YYLTYPE *locp)
2199 struct asm_symbol *s = NULL;
2200 struct asm_symbol *exist = (struct asm_symbol *)
2201 _mesa_symbol_table_find_symbol(state->st, 0, name);
2204 if (exist != NULL) {
2205 yyerror(locp, state, "redeclared identifier");
2207 s = calloc(1, sizeof(struct asm_symbol));
2213 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2214 yyerror(locp, state, "too many temporaries declared");
2219 s->temp_binding = state->prog->NumTemporaries;
2220 state->prog->NumTemporaries++;
2224 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2225 yyerror(locp, state, "too many address registers declared");
2230 /* FINISHME: Add support for multiple address registers.
2232 state->prog->NumAddressRegs++;
2239 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2240 s->next = state->sym;
2248 int add_state_reference(struct gl_program_parameter_list *param_list,
2249 const gl_state_index tokens[STATE_LENGTH])
2251 const GLuint size = 4; /* XXX fix */
2255 name = _mesa_program_state_string(tokens);
2256 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2258 NULL, (gl_state_index *) tokens, 0x0);
2259 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2261 /* free name string here since we duplicated it in add_parameter() */
2269 initialize_symbol_from_state(struct gl_program *prog,
2270 struct asm_symbol *param_var,
2271 const gl_state_index tokens[STATE_LENGTH])
2274 gl_state_index state_tokens[STATE_LENGTH];
2277 memcpy(state_tokens, tokens, sizeof(state_tokens));
2279 param_var->type = at_param;
2280 param_var->param_binding_type = PROGRAM_STATE_VAR;
2282 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2283 * unroll it and call add_state_reference() for each row
2285 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2286 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2287 state_tokens[0] == STATE_MVP_MATRIX ||
2288 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2289 state_tokens[0] == STATE_PROGRAM_MATRIX)
2290 && (state_tokens[2] != state_tokens[3])) {
2292 const int first_row = state_tokens[2];
2293 const int last_row = state_tokens[3];
2295 for (row = first_row; row <= last_row; row++) {
2296 state_tokens[2] = state_tokens[3] = row;
2298 idx = add_state_reference(prog->Parameters, state_tokens);
2299 if (param_var->param_binding_begin == ~0U)
2300 param_var->param_binding_begin = idx;
2301 param_var->param_binding_length++;
2305 idx = add_state_reference(prog->Parameters, state_tokens);
2306 if (param_var->param_binding_begin == ~0U)
2307 param_var->param_binding_begin = idx;
2308 param_var->param_binding_length++;
2316 initialize_symbol_from_param(struct gl_program *prog,
2317 struct asm_symbol *param_var,
2318 const gl_state_index tokens[STATE_LENGTH])
2321 gl_state_index state_tokens[STATE_LENGTH];
2324 memcpy(state_tokens, tokens, sizeof(state_tokens));
2326 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2327 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2328 assert((state_tokens[1] == STATE_ENV)
2329 || (state_tokens[1] == STATE_LOCAL));
2331 param_var->type = at_param;
2332 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2333 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
2335 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2336 * we need to unroll it and call add_state_reference() for each row
2338 if (state_tokens[2] != state_tokens[3]) {
2340 const int first_row = state_tokens[2];
2341 const int last_row = state_tokens[3];
2343 for (row = first_row; row <= last_row; row++) {
2344 state_tokens[2] = state_tokens[3] = row;
2346 idx = add_state_reference(prog->Parameters, state_tokens);
2347 if (param_var->param_binding_begin == ~0U)
2348 param_var->param_binding_begin = idx;
2349 param_var->param_binding_length++;
2353 idx = add_state_reference(prog->Parameters, state_tokens);
2354 if (param_var->param_binding_begin == ~0U)
2355 param_var->param_binding_begin = idx;
2356 param_var->param_binding_length++;
2364 initialize_symbol_from_const(struct gl_program *prog,
2365 struct asm_symbol *param_var,
2366 const struct asm_vector *vec)
2368 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2369 NULL, vec->count, GL_NONE, vec->data,
2372 param_var->type = at_param;
2373 param_var->param_binding_type = PROGRAM_CONSTANT;
2375 if (param_var->param_binding_begin == ~0U)
2376 param_var->param_binding_begin = idx;
2377 param_var->param_binding_length++;
2384 make_error_string(const char *fmt, ...)
2390 va_start(args, fmt);
2392 /* Call vsnprintf once to determine how large the final string is. Call it
2393 * again to do the actual formatting. from the vsnprintf manual page:
2395 * Upon successful return, these functions return the number of
2396 * characters printed (not including the trailing '\0' used to end
2397 * output to strings).
2399 length = 1 + vsnprintf(NULL, 0, fmt, args);
2401 str = _mesa_malloc(length);
2403 vsnprintf(str, length, fmt, args);
2413 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2418 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2420 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2421 _mesa_free(err_str);
2424 err_str = make_error_string("line %u, char %u: error: %s\n",
2425 locp->first_line, locp->first_column, s);
2426 _mesa_set_program_error(state->ctx, locp->position, err_str);
2429 _mesa_free(err_str);
2435 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2436 GLsizei len, struct asm_parser_state *state)
2438 struct asm_instruction *inst;
2441 GLboolean result = GL_FALSE;
2443 struct asm_symbol *sym;
2446 state->prog->Target = target;
2447 state->prog->Parameters = _mesa_new_parameter_list();
2449 /* Make a copy of the program string and force it to be NUL-terminated.
2451 strz = (GLubyte *) _mesa_malloc(len + 1);
2453 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2456 _mesa_memcpy (strz, str, len);
2459 state->prog->String = strz;
2461 state->st = _mesa_symbol_table_ctor();
2463 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2464 ? & ctx->Const.VertexProgram
2465 : & ctx->Const.FragmentProgram;
2467 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2468 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2469 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2470 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2471 state->MaxLights = ctx->Const.MaxLights;
2472 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2474 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2475 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2477 _mesa_set_program_error(ctx, -1, NULL);
2479 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2481 _mesa_program_lexer_dtor(state->scanner);
2484 if (ctx->Program.ErrorPos != -1) {
2488 if (! _mesa_layout_parameters(state)) {
2492 loc.first_column = 0;
2495 yyerror(& loc, state, "invalid PARAM usage");
2501 /* Add one instruction to store the "END" instruction.
2503 state->prog->Instructions =
2504 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2505 inst = state->inst_head;
2506 for (i = 0; i < state->prog->NumInstructions; i++) {
2507 struct asm_instruction *const temp = inst->next;
2509 state->prog->Instructions[i] = inst->Base;
2513 /* Finally, tag on an OPCODE_END instruction */
2515 const GLuint numInst = state->prog->NumInstructions;
2516 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2517 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2519 state->prog->NumInstructions++;
2521 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2522 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
2525 * Initialize native counts to logical counts. The device driver may
2526 * change them if program is translated into a hardware program.
2528 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2529 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2530 state->prog->NumNativeParameters = state->prog->NumParameters;
2531 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2532 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2537 for (inst = state->inst_head; inst != NULL; inst = temp) {
2542 state->inst_head = NULL;
2543 state->inst_tail = NULL;
2545 for (sym = state->sym; sym != NULL; sym = temp) {
2548 _mesa_free((void *) sym->name);
2553 _mesa_symbol_table_dtor(state->st);