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 "prog_parameter.h"
30 #include "prog_parameter_layout.h"
31 #include "prog_statevars.h"
32 #include "prog_instruction.h"
34 #include "symbol_table.h"
35 #include "program_parser.h"
37 extern void *yy_scan_string(char *);
38 extern void yy_delete_buffer(void *);
40 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
41 char *name, enum asm_type t, struct YYLTYPE *locp);
43 static int initialize_symbol_from_state(struct gl_program *prog,
44 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
46 static int initialize_symbol_from_param(struct gl_program *prog,
47 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
49 static int initialize_symbol_from_const(struct gl_program *prog,
50 struct asm_symbol *param_var, const struct asm_vector *vec);
52 static int yyparse(struct asm_parser_state *state);
54 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
57 static int validate_inputs(struct YYLTYPE *locp,
58 struct asm_parser_state *state);
60 static void init_dst_reg(struct prog_dst_register *r);
62 static void init_src_reg(struct asm_src_register *r);
64 static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
65 const struct prog_dst_register *dst, const struct asm_src_register *src0,
66 const struct asm_src_register *src1, const struct asm_src_register *src2);
73 #define YYLLOC_DEFAULT(Current, Rhs, N) \
76 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
77 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
78 (Current).position = YYRHSLOC(Rhs, 1).position; \
79 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
80 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
82 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
83 (Current).last_line = (Current).first_line; \
84 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
85 (Current).last_column = (Current).first_column; \
86 (Current).position = YYRHSLOC(Rhs, 0).position \
87 + (Current).first_column; \
91 #define YYLEX_PARAM state->scanner
96 %parse-param { struct asm_parser_state *state }
98 %lex-param { void *scanner }
101 struct asm_instruction *inst;
102 struct asm_symbol *sym;
103 struct asm_symbol temp_sym;
104 struct asm_swizzle_mask swiz_mask;
105 struct asm_src_register src_reg;
106 struct prog_dst_register dst_reg;
107 struct prog_instruction temp_inst;
115 struct asm_vector vector;
116 gl_inst_opcode opcode;
119 %token ARBvp_10 ARBfp_10
121 /* Tokens for assembler pseudo-ops */
122 %token <integer> ADDRESS
126 %token <integer> TEMP
129 /* Tokens for instructions */
130 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
131 %token <temp_inst> ARL KIL SWZ
133 %token <integer> INTEGER
136 %token AMBIENT ATTENUATION
139 %token DEPTH DIFFUSE DIRECTION
140 %token EMISSION ENV EYE
141 %token FOG FOGCOORD FRAGMENT FRONT
143 %token INVERSE INVTRANS
144 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
145 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
148 %token PALETTE PARAMS PLANE POINT POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
149 %token RANGE RESULT ROW
150 %token SCENECOLOR SECONDARY SHININESS SIZE SPECULAR SPOT STATE
151 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
152 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
153 %token VERTEX VTXATTRIB
156 %token <string> IDENTIFIER
157 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
161 %type <inst> instruction ALU_instruction TexInstruction
162 %type <inst> ARL_instruction VECTORop_instruction
163 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
164 %type <inst> TRIop_instruction SWZ_instruction SAMPLE_instruction
165 %type <inst> KIL_instruction
167 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
168 %type <src_reg> srcReg scalarSrcReg swizzleSrcReg
169 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle extSwizComp
170 %type <swiz_mask> optionalMask
171 %type <integer> extSwizSel
173 %type <sym> progParamArray
174 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
175 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
177 %type <swiz_mask> addrComponent addrWriteMask
179 %type <result> resultBinding resultColBinding
180 %type <integer> optFaceType optColorType
181 %type <integer> optResultFaceType optResultColorType
183 %type <integer> optTexImageUnitNum texImageUnitNum
184 %type <integer> optTexCoordUnitNum texCoordUnitNum
185 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
186 %type <integer> texImageUnit texTarget
187 %type <integer> vtxAttribNum
189 %type <attrib> attribBinding vtxAttribItem fragAttribItem
191 %type <temp_sym> paramSingleInit paramSingleItemDecl
192 %type <integer> optArraySize
194 %type <state> stateSingleItem stateMultipleItem
195 %type <state> stateMaterialItem
196 %type <state> stateLightItem stateLightModelItem stateLightProdItem
197 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
198 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
199 %type <state> stateTexEnvItem
201 %type <state> stateLModProperty
202 %type <state> stateMatrixName optMatrixRows
204 %type <integer> stateMatProperty
205 %type <integer> stateLightProperty stateSpotProperty
206 %type <integer> stateLightNumber stateLProdProperty
207 %type <integer> stateTexGenType stateTexGenCoord
208 %type <integer> stateTexEnvProperty
209 %type <integer> stateFogProperty
210 %type <integer> stateClipPlaneNum
211 %type <integer> statePointProperty
213 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
214 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
215 %type <integer> stateProgramMatNum
217 %type <integer> ambDiffSpecProperty
219 %type <state> programSingleItem progEnvParam progLocalParam
220 %type <state> programMultipleItem progEnvParams progLocalParams
222 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
223 %type <temp_sym> paramSingleItemUse
225 %type <integer> progEnvParamNum progLocalParamNum
226 %type <state> progEnvParamNums progLocalParamNums
228 %type <vector> paramConstDecl paramConstUse
229 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
230 %type <real> signedFloatConstant
231 %type <negate> optionalSign
234 extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
240 program: language optionSequence statementSequence END
245 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
246 yyerror(& @1, state, "invalid fragment program header");
249 state->mode = ARB_vertex;
253 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
254 yyerror(& @1, state, "invalid vertex program header");
256 state->mode = ARB_fragment;
260 optionSequence: optionSequence option
264 option: OPTION IDENTIFIER ';'
268 if (state->mode == ARB_vertex) {
269 valid = _mesa_ARBvp_parse_option(state, $2);
270 } else if (state->mode == ARB_fragment) {
271 valid = _mesa_ARBfp_parse_option(state, $2);
276 yyerror(& @2, state, "invalid option string");
282 statementSequence: statementSequence statement
286 statement: instruction ';'
289 if (state->inst_tail == NULL) {
290 state->inst_head = $1;
292 state->inst_tail->next = $1;
295 state->inst_tail = $1;
298 state->prog->NumInstructions++;
301 | namingStatement ';'
304 instruction: ALU_instruction
308 ALU_instruction: ARL_instruction
309 | VECTORop_instruction
310 | SCALARop_instruction
311 | BINSCop_instruction
317 TexInstruction: SAMPLE_instruction
321 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
323 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
327 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
329 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
330 $$->Base.SaturateMode = $1.SaturateMode;
334 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
336 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
337 $$->Base.SaturateMode = $1.SaturateMode;
341 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
343 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, NULL);
344 $$->Base.SaturateMode = $1.SaturateMode;
349 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
351 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, NULL);
352 $$->Base.SaturateMode = $1.SaturateMode;
356 TRIop_instruction: TRI_OP maskedDstReg ','
357 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
359 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, & $6, & $8);
360 $$->Base.SaturateMode = $1.SaturateMode;
364 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
366 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
368 $$->Base.SaturateMode = $1.SaturateMode;
369 $$->Base.TexSrcUnit = $6;
370 $$->Base.TexSrcTarget = $8;
375 KIL_instruction: KIL swizzleSrcReg
377 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
381 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
387 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
388 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
389 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
390 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
391 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
394 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
396 /* FIXME: Is this correct? Should the extenedSwizzle be applied
397 * FIXME: to the existing swizzle?
399 $4.Base.Swizzle = $6.swizzle;
401 $$ = asm_instruction_ctor(OPCODE_SWZ, & $2, & $4, NULL, NULL);
402 $$->Base.SaturateMode = $1.SaturateMode;
406 scalarSrcReg: optionalSign srcReg scalarSuffix
411 $$.Base.Negate = ~$$.Base.Negate;
414 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
419 swizzleSrcReg: optionalSign srcReg swizzleSuffix
424 $$.Base.Negate = ~$$.Base.Negate;
427 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
432 maskedDstReg: dstReg optionalMask
435 $$.WriteMask = $2.mask;
437 if ($$.File == PROGRAM_OUTPUT) {
438 /* Technically speaking, this should check that it is in
439 * vertex program mode. However, PositionInvariant can never be
440 * set in fragment program mode, so it is somewhat irrelevant.
442 if (state->option.PositionInvariant
443 && ($$.Index == VERT_RESULT_HPOS)) {
444 yyerror(& @1, state, "position-invariant programs cannot "
449 state->prog->OutputsWritten |= (1U << $$.Index);
454 maskedAddrReg: addrReg addrWriteMask
457 $$.File = PROGRAM_ADDRESS;
459 $$.WriteMask = $2.mask;
463 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
465 $$.swizzle = MAKE_SWIZZLE4($1.swizzle, $3.swizzle,
466 $5.swizzle, $7.swizzle);
467 $$.mask = ($1.mask) | ($3.mask << 1) | ($5.mask << 2)
472 extSwizComp: optionalSign extSwizSel
475 $$.mask = ($1) ? 1 : 0;
481 if (($1 != 0) && ($1 != 1)) {
482 yyerror(& @1, state, "invalid extended swizzle selector");
486 $$ = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
490 if (strlen($1) > 1) {
491 yyerror(& @1, state, "invalid extended swizzle selector");
509 yyerror(& @1, state, "invalid extended swizzle selector");
516 srcReg: IDENTIFIER /* temporaryReg | progParamSingle */
518 struct asm_symbol *const s = (struct asm_symbol *)
519 _mesa_symbol_table_find_symbol(state->st, 0, $1);
522 yyerror(& @1, state, "invalid operand variable");
524 } else if ((s->type != at_param) && (s->type != at_temp)
525 && (s->type != at_attrib)) {
526 yyerror(& @1, state, "invalid operand variable");
528 } else if ((s->type == at_param) && s->param_is_array) {
529 yyerror(& @1, state, "non-array access to array PARAM");
536 $$.Base.File = PROGRAM_TEMPORARY;
537 $$.Base.Index = s->temp_binding;
540 $$.Base.File = s->param_binding_type;
541 $$.Base.Index = s->param_binding_begin;
544 $$.Base.File = PROGRAM_INPUT;
545 $$.Base.Index = s->attrib_binding;
546 state->prog->InputsRead |= (1U << $$.Base.Index);
548 if (!validate_inputs(& @1, state)) {
561 $$.Base.File = PROGRAM_INPUT;
563 state->prog->InputsRead |= (1U << $$.Base.Index);
565 if (!validate_inputs(& @1, state)) {
569 | progParamArray '[' progParamArrayMem ']'
571 if (! $3.Base.RelAddr
572 && ($3.Base.Index >= $1->param_binding_length)) {
573 yyerror(& @3, state, "out of bounds array access");
578 $$.Base.File = $1->param_binding_type;
580 if ($3.Base.RelAddr) {
581 $1->param_accessed_indirectly = 1;
584 $$.Base.Index = $3.Base.Index;
587 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
593 $$.Base.File = ($1.name != NULL)
594 ? $1.param_binding_type
596 $$.Base.Index = $1.param_binding_begin;
600 dstReg: resultBinding
603 $$.File = PROGRAM_OUTPUT;
606 | IDENTIFIER /* temporaryReg | vertexResultReg */
608 struct asm_symbol *const s = (struct asm_symbol *)
609 _mesa_symbol_table_find_symbol(state->st, 0, $1);
612 yyerror(& @1, state, "invalid operand variable");
614 } else if ((s->type != at_output) && (s->type != at_temp)) {
615 yyerror(& @1, state, "invalid operand variable");
620 if (s->type == at_temp) {
621 $$.File = PROGRAM_TEMPORARY;
622 $$.Index = s->temp_binding;
624 $$.File = s->param_binding_type;
625 $$.Index = s->param_binding_begin;
630 progParamArray: IDENTIFIER
632 struct asm_symbol *const s = (struct asm_symbol *)
633 _mesa_symbol_table_find_symbol(state->st, 0, $1);
636 yyerror(& @1, state, "invalid operand variable");
638 } else if ((s->type != at_param) || !s->param_is_array) {
639 yyerror(& @1, state, "array access to non-PARAM variable");
647 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
649 progParamArrayAbs: INTEGER
656 progParamArrayRel: addrReg addrComponent addrRegRelOffset
658 /* FINISHME: Add support for multiple address registers.
660 /* FINISHME: Add support for 4-component address registers.
668 addrRegRelOffset: { $$ = 0; }
669 | '+' addrRegPosOffset { $$ = $2; }
670 | '-' addrRegNegOffset { $$ = -$2; }
673 addrRegPosOffset: INTEGER
675 if (($1 < 0) || ($1 > 63)) {
677 "relative address offset too large (positive)");
685 addrRegNegOffset: INTEGER
687 if (($1 < 0) || ($1 > 64)) {
689 "relative address offset too large (negative)");
699 struct asm_symbol *const s = (struct asm_symbol *)
700 _mesa_symbol_table_find_symbol(state->st, 0, $1);
703 yyerror(& @1, state, "invalid array member");
705 } else if (s->type != at_address) {
707 "invalid variable for indexed array access");
717 if ($1.mask != WRITEMASK_X) {
718 yyerror(& @1, state, "invalid address component selector");
728 if ($1.mask != WRITEMASK_X) {
730 "address register write mask must be \".x\"");
743 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
746 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
747 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
750 namingStatement: ATTRIB_statement
758 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
760 struct asm_symbol *const s =
761 declare_variable(state, $2, at_attrib, & @2);
766 s->attrib_binding = $4;
767 state->InputsBound |= (1U << s->attrib_binding);
769 if (!validate_inputs(& @4, state)) {
776 attribBinding: VERTEX vtxAttribItem
780 | FRAGMENT fragAttribItem
786 vtxAttribItem: POSITION
788 $$ = VERT_ATTRIB_POS;
790 | WEIGHT vtxOptWeightNum
792 $$ = VERT_ATTRIB_WEIGHT;
796 $$ = VERT_ATTRIB_NORMAL;
800 $$ = VERT_ATTRIB_COLOR0 + $2;
804 $$ = VERT_ATTRIB_FOG;
806 | TEXCOORD optTexCoordUnitNum
808 $$ = VERT_ATTRIB_TEX0 + $2;
810 | MATRIXINDEX '[' vtxWeightNum ']'
814 | VTXATTRIB '[' vtxAttribNum ']'
816 $$ = VERT_ATTRIB_GENERIC0 + $3;
820 vtxAttribNum: INTEGER
822 if ($1 >= state->limits->MaxAttribs) {
823 yyerror(& @1, state, "invalid vertex attribute reference");
831 vtxOptWeightNum: | '[' vtxWeightNum ']';
832 vtxWeightNum: INTEGER;
834 fragAttribItem: POSITION
836 $$ = FRAG_ATTRIB_WPOS;
840 $$ = FRAG_ATTRIB_COL0 + $2;
844 $$ = FRAG_ATTRIB_FOGC;
846 | TEXCOORD optTexCoordUnitNum
848 $$ = FRAG_ATTRIB_TEX0 + $2;
852 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
854 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
856 struct asm_symbol *const s =
857 declare_variable(state, $2, at_param, & @2);
862 s->param_binding_type = $3.param_binding_type;
863 s->param_binding_begin = $3.param_binding_begin;
864 s->param_binding_length = $3.param_binding_length;
865 s->param_is_array = 0;
870 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
872 if (($4 != 0) && ($4 != $6.param_binding_length)) {
874 "parameter array size and number of bindings must match");
877 struct asm_symbol *const s =
878 declare_variable(state, $2, $6.type, & @2);
883 s->param_binding_type = $6.param_binding_type;
884 s->param_binding_begin = $6.param_binding_begin;
885 s->param_binding_length = $6.param_binding_length;
886 s->param_is_array = 1;
898 if (($1 < 1) || ($1 >= state->limits->MaxParameters)) {
899 yyerror(& @1, state, "invalid parameter array size");
907 paramSingleInit: '=' paramSingleItemDecl
913 paramMultipleInit: '=' '{' paramMultInitList '}'
919 paramMultInitList: paramMultipleItem
920 | paramMultInitList ',' paramMultipleItem
922 $1.param_binding_length += $3.param_binding_length;
927 paramSingleItemDecl: stateSingleItem
929 memset(& $$, 0, sizeof($$));
930 $$.param_binding_begin = ~0;
931 initialize_symbol_from_state(state->prog, & $$, $1);
935 memset(& $$, 0, sizeof($$));
936 $$.param_binding_begin = ~0;
937 initialize_symbol_from_param(state->prog, & $$, $1);
941 memset(& $$, 0, sizeof($$));
942 $$.param_binding_begin = ~0;
943 initialize_symbol_from_const(state->prog, & $$, & $1);
947 paramSingleItemUse: stateSingleItem
949 memset(& $$, 0, sizeof($$));
950 $$.param_binding_begin = ~0;
951 initialize_symbol_from_state(state->prog, & $$, $1);
955 memset(& $$, 0, sizeof($$));
956 $$.param_binding_begin = ~0;
957 initialize_symbol_from_param(state->prog, & $$, $1);
961 memset(& $$, 0, sizeof($$));
962 $$.param_binding_begin = ~0;
963 initialize_symbol_from_const(state->prog, & $$, & $1);
967 paramMultipleItem: stateMultipleItem
969 memset(& $$, 0, sizeof($$));
970 $$.param_binding_begin = ~0;
971 initialize_symbol_from_state(state->prog, & $$, $1);
973 | programMultipleItem
975 memset(& $$, 0, sizeof($$));
976 $$.param_binding_begin = ~0;
977 initialize_symbol_from_param(state->prog, & $$, $1);
981 memset(& $$, 0, sizeof($$));
982 $$.param_binding_begin = ~0;
983 initialize_symbol_from_const(state->prog, & $$, & $1);
987 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
988 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
991 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
992 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
993 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
994 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
995 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
996 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
997 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
998 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
999 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1000 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1003 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1005 memset($$, 0, sizeof($$));
1006 $$[0] = STATE_MATERIAL;
1012 stateMatProperty: ambDiffSpecProperty
1018 $$ = STATE_EMISSION;
1022 $$ = STATE_SHININESS;
1026 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1028 memset($$, 0, sizeof($$));
1029 $$[0] = STATE_LIGHT;
1035 stateLightProperty: ambDiffSpecProperty
1041 $$ = STATE_POSITION;
1045 $$ = STATE_ATTENUATION;
1047 | SPOT stateSpotProperty
1053 $$ = STATE_HALF_VECTOR;
1057 stateSpotProperty: DIRECTION
1059 $$ = STATE_SPOT_DIRECTION;
1063 stateLightModelItem: LIGHTMODEL stateLModProperty
1070 stateLModProperty: AMBIENT
1072 memset($$, 0, sizeof($$));
1073 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1075 | optFaceType SCENECOLOR
1077 memset($$, 0, sizeof($$));
1078 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1083 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1085 memset($$, 0, sizeof($$));
1086 $$[0] = STATE_LIGHTPROD;
1093 stateLProdProperty: ambDiffSpecProperty;
1095 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1097 memset($$, 0, sizeof($$));
1103 stateTexEnvProperty: COLOR
1105 $$ = STATE_TEXENV_COLOR;
1109 ambDiffSpecProperty: AMBIENT
1119 $$ = STATE_SPECULAR;
1123 stateLightNumber: INTEGER
1125 if ($1 >= state->MaxLights) {
1126 yyerror(& @1, state, "invalid light selector");
1134 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1136 memset($$, 0, sizeof($$));
1137 $$[0] = STATE_TEXGEN;
1143 stateTexGenType: EYE
1145 $$ = STATE_TEXGEN_EYE_S;
1149 $$ = STATE_TEXGEN_OBJECT_S;
1152 stateTexGenCoord: TEXGEN_S
1154 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1158 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1162 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1166 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1170 stateFogItem: FOG stateFogProperty
1172 memset($$, 0, sizeof($$));
1177 stateFogProperty: COLOR
1179 $$ = STATE_FOG_COLOR;
1183 $$ = STATE_FOG_PARAMS;
1187 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1189 memset($$, 0, sizeof($$));
1190 $$[0] = STATE_CLIPPLANE;
1195 stateClipPlaneNum: INTEGER
1197 if ($1 >= state->MaxClipPlanes) {
1198 yyerror(& @1, state, "invalid clip plane selector");
1206 statePointItem: POINT statePointProperty
1208 memset($$, 0, sizeof($$));
1213 statePointProperty: SIZE
1215 $$ = STATE_POINT_SIZE;
1219 $$ = STATE_POINT_ATTENUATION;
1223 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1233 stateMatrixRows: stateMatrixItem optMatrixRows
1248 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1250 /* It seems logical that the matrix row range specifier would have
1251 * to specify a range or more than one row (i.e., $5 > $3).
1252 * However, the ARB_vertex_program spec says "a program will fail
1253 * to load if <a> is greater than <b>." This means that $3 == $5
1257 yyerror(& @3, state, "invalid matrix row range");
1266 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1274 stateOptMatModifier:
1284 stateMatModifier: INVERSE
1286 $$ = STATE_MATRIX_INVERSE;
1290 $$ = STATE_MATRIX_TRANSPOSE;
1294 $$ = STATE_MATRIX_INVTRANS;
1298 stateMatrixRowNum: INTEGER
1301 yyerror(& @1, state, "invalid matrix row reference");
1309 stateMatrixName: MODELVIEW stateOptModMatNum
1311 $$[0] = STATE_MODELVIEW_MATRIX;
1316 $$[0] = STATE_PROJECTION_MATRIX;
1321 $$[0] = STATE_MVP_MATRIX;
1324 | TEXTURE optTexCoordUnitNum
1326 $$[0] = STATE_TEXTURE_MATRIX;
1329 | PALETTE '[' statePaletteMatNum ']'
1333 | MAT_PROGRAM '[' stateProgramMatNum ']'
1335 $$[0] = STATE_PROGRAM_MATRIX;
1349 stateModMatNum: INTEGER
1351 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1355 yyerror(& @1, state, "invalid modelview matrix index");
1362 statePaletteMatNum: INTEGER
1364 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1365 * through here. The error will be generated later.
1370 stateProgramMatNum: INTEGER
1372 if ($1 >= state->MaxProgramMatrices) {
1373 yyerror(& @1, state, "invalid program matrix selector");
1383 programSingleItem: progEnvParam | progLocalParam;
1385 programMultipleItem: progEnvParams | progLocalParams;
1387 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1389 memset($$, 0, sizeof($$));
1390 $$[0] = state->state_param_enum;
1397 progEnvParamNums: progEnvParamNum
1402 | progEnvParamNum DOT_DOT progEnvParamNum
1409 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1411 memset($$, 0, sizeof($$));
1412 $$[0] = state->state_param_enum;
1419 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1421 memset($$, 0, sizeof($$));
1422 $$[0] = state->state_param_enum;
1423 $$[1] = STATE_LOCAL;
1428 progLocalParamNums: progLocalParamNum
1433 | progLocalParamNum DOT_DOT progLocalParamNum
1440 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1442 memset($$, 0, sizeof($$));
1443 $$[0] = state->state_param_enum;
1444 $$[1] = STATE_LOCAL;
1450 progEnvParamNum: INTEGER
1452 if ($1 >= state->limits->MaxEnvParams) {
1453 yyerror(& @1, state, "invalid environment parameter reference");
1460 progLocalParamNum: INTEGER
1462 if ($1 >= state->limits->MaxLocalParams) {
1463 yyerror(& @1, state, "invalid local parameter reference");
1472 paramConstDecl: paramConstScalarDecl | paramConstVector;
1473 paramConstUse: paramConstScalarUse | paramConstVector;
1475 paramConstScalarDecl: signedFloatConstant
1482 paramConstScalarUse: REAL
1490 $$.data[0] = (float) $1;
1494 paramConstVector: '{' signedFloatConstant '}'
1499 | '{' signedFloatConstant ',' signedFloatConstant '}'
1505 | '{' signedFloatConstant ',' signedFloatConstant ','
1506 signedFloatConstant '}'
1513 | '{' signedFloatConstant ',' signedFloatConstant ','
1514 signedFloatConstant ',' signedFloatConstant '}'
1524 signedFloatConstant: optionalSign REAL
1526 $$ = ($1) ? -$2 : $2;
1528 | optionalSign INTEGER
1530 $$ = (float)(($1) ? -$2 : $2);
1534 optionalSign: '+' { $$ = FALSE; }
1535 | '-' { $$ = TRUE; }
1539 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1542 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1545 varNameList: varNameList ',' IDENTIFIER
1547 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1553 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1559 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1561 struct asm_symbol *const s =
1562 declare_variable(state, $2, at_output, & @2);
1567 s->output_binding = $4;
1572 resultBinding: RESULT POSITION
1574 if (state->mode == ARB_vertex) {
1575 $$ = VERT_RESULT_HPOS;
1577 yyerror(& @2, state, "invalid program result name");
1583 if (state->mode == ARB_vertex) {
1584 $$ = VERT_RESULT_FOGC;
1586 yyerror(& @2, state, "invalid program result name");
1590 | RESULT resultColBinding
1596 if (state->mode == ARB_vertex) {
1597 $$ = VERT_RESULT_PSIZ;
1599 yyerror(& @2, state, "invalid program result name");
1603 | RESULT TEXCOORD optTexCoordUnitNum
1605 if (state->mode == ARB_vertex) {
1606 $$ = VERT_RESULT_TEX0 + $3;
1608 yyerror(& @2, state, "invalid program result name");
1614 if (state->mode == ARB_fragment) {
1615 $$ = FRAG_RESULT_DEPTH;
1617 yyerror(& @2, state, "invalid program result name");
1623 resultColBinding: COLOR optResultFaceType optResultColorType
1631 $$ = (state->mode == ARB_vertex)
1633 : FRAG_RESULT_COLOR;
1637 if (state->mode == ARB_vertex) {
1638 $$ = VERT_RESULT_COL0;
1640 yyerror(& @1, state, "invalid program result name");
1646 if (state->mode == ARB_vertex) {
1647 $$ = VERT_RESULT_BFC0;
1649 yyerror(& @1, state, "invalid program result name");
1661 if (state->mode == ARB_vertex) {
1664 yyerror(& @1, state, "invalid program result name");
1670 if (state->mode == ARB_vertex) {
1673 yyerror(& @1, state, "invalid program result name");
1679 optFaceType: { $$ = 0; }
1684 optColorType: { $$ = 0; }
1685 | PRIMARY { $$ = 0; }
1686 | SECONDARY { $$ = 1; }
1689 optTexCoordUnitNum: { $$ = 0; }
1690 | '[' texCoordUnitNum ']' { $$ = $2; }
1693 optTexImageUnitNum: { $$ = 0; }
1694 | '[' texImageUnitNum ']' { $$ = $2; }
1697 optLegacyTexUnitNum: { $$ = 0; }
1698 | '[' legacyTexUnitNum ']' { $$ = $2; }
1701 texCoordUnitNum: INTEGER
1703 if ($1 >= state->MaxTextureCoordUnits) {
1704 yyerror(& @1, state, "invalid texture coordinate unit selector");
1712 texImageUnitNum: INTEGER
1714 if ($1 >= state->MaxTextureImageUnits) {
1715 yyerror(& @1, state, "invalid texture image unit selector");
1723 legacyTexUnitNum: INTEGER
1725 if ($1 >= state->MaxTextureUnits) {
1726 yyerror(& @1, state, "invalid texture unit selector");
1734 ALIAS_statement: ALIAS IDENTIFIER '=' IDENTIFIER
1736 struct asm_symbol *exist = (struct asm_symbol *)
1737 _mesa_symbol_table_find_symbol(state->st, 0, $2);
1738 struct asm_symbol *target = (struct asm_symbol *)
1739 _mesa_symbol_table_find_symbol(state->st, 0, $4);
1742 if (exist != NULL) {
1743 yyerror(& @2, state, "redeclared identifier");
1745 } else if (target == NULL) {
1746 yyerror(& @4, state,
1747 "undefined variable binding in ALIAS statement");
1750 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
1757 struct asm_instruction *
1758 asm_instruction_ctor(gl_inst_opcode op,
1759 const struct prog_dst_register *dst,
1760 const struct asm_src_register *src0,
1761 const struct asm_src_register *src1,
1762 const struct asm_src_register *src2)
1764 struct asm_instruction *inst = calloc(1, sizeof(struct asm_instruction));
1767 _mesa_init_instructions(inst, 1);
1768 inst->Base.Opcode = op;
1769 inst->Base.DstReg = *dst;
1771 inst->Base.SrcReg[0] = src0->Base;
1772 inst->SrcReg[0] = *src0;
1775 inst->Base.SrcReg[1] = src1->Base;
1776 inst->SrcReg[1] = *src1;
1778 init_src_reg(& inst->SrcReg[1]);
1782 inst->Base.SrcReg[2] = src2->Base;
1783 inst->SrcReg[2] = *src2;
1785 init_src_reg(& inst->SrcReg[2]);
1794 init_dst_reg(struct prog_dst_register *r)
1796 memset(r, 0, sizeof(*r));
1797 r->File = PROGRAM_UNDEFINED;
1798 r->WriteMask = WRITEMASK_XYZW;
1799 r->CondMask = COND_TR;
1800 r->CondSwizzle = SWIZZLE_NOOP;
1805 init_src_reg(struct asm_src_register *r)
1807 memset(r, 0, sizeof(*r));
1808 r->Base.File = PROGRAM_UNDEFINED;
1809 r->Base.Swizzle = SWIZZLE_NOOP;
1815 * Validate the set of inputs used by a program
1817 * Validates that legal sets of inputs are used by the program. In this case
1818 * "used" included both reading the input or binding the input to a name using
1819 * the \c ATTRIB command.
1822 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
1825 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
1827 const int inputs = state->prog->InputsRead | state->InputsBound;
1829 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
1830 yyerror(locp, state, "illegal use of generic attribute and name attribute");
1839 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
1840 struct YYLTYPE *locp)
1842 struct asm_symbol *s = NULL;
1843 struct asm_symbol *exist = (struct asm_symbol *)
1844 _mesa_symbol_table_find_symbol(state->st, 0, name);
1847 if (exist != NULL) {
1848 yyerror(locp, state, "redeclared identifier");
1850 s = calloc(1, sizeof(struct asm_symbol));
1856 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
1857 yyerror(locp, state, "too many temporaries declared");
1862 s->temp_binding = state->prog->NumTemporaries;
1863 state->prog->NumTemporaries++;
1867 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
1868 yyerror(locp, state, "too many address registers declared");
1873 /* FINISHME: Add support for multiple address registers.
1875 state->prog->NumAddressRegs++;
1882 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
1889 int add_state_reference(struct gl_program_parameter_list *param_list,
1890 const gl_state_index tokens[STATE_LENGTH])
1892 const GLuint size = 4; /* XXX fix */
1896 name = _mesa_program_state_string(tokens);
1897 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
1899 NULL, (gl_state_index *) tokens, 0x0);
1900 param_list->StateFlags |= _mesa_program_state_flags(tokens);
1902 /* free name string here since we duplicated it in add_parameter() */
1910 initialize_symbol_from_state(struct gl_program *prog,
1911 struct asm_symbol *param_var,
1912 const gl_state_index tokens[STATE_LENGTH])
1915 gl_state_index state_tokens[STATE_LENGTH];
1918 memcpy(state_tokens, tokens, sizeof(state_tokens));
1920 param_var->type = at_param;
1922 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
1923 * unroll it and call add_state_reference() for each row
1925 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1926 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1927 state_tokens[0] == STATE_MVP_MATRIX ||
1928 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1929 state_tokens[0] == STATE_PROGRAM_MATRIX)
1930 && (state_tokens[2] != state_tokens[3])) {
1932 const int first_row = state_tokens[2];
1933 const int last_row = state_tokens[3];
1935 for (row = first_row; row <= last_row; row++) {
1936 state_tokens[2] = state_tokens[3] = row;
1938 idx = add_state_reference(prog->Parameters, state_tokens);
1939 if (param_var->param_binding_begin == ~0U)
1940 param_var->param_binding_begin = idx;
1941 param_var->param_binding_length++;
1945 idx = add_state_reference(prog->Parameters, state_tokens);
1946 if (param_var->param_binding_begin == ~0U)
1947 param_var->param_binding_begin = idx;
1948 param_var->param_binding_length++;
1956 initialize_symbol_from_param(struct gl_program *prog,
1957 struct asm_symbol *param_var,
1958 const gl_state_index tokens[STATE_LENGTH])
1961 gl_state_index state_tokens[STATE_LENGTH];
1964 memcpy(state_tokens, tokens, sizeof(state_tokens));
1966 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
1967 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
1968 assert((state_tokens[1] == STATE_ENV)
1969 || (state_tokens[1] == STATE_LOCAL));
1971 param_var->type = at_param;
1973 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
1974 * we need to unroll it and call add_state_reference() for each row
1976 if (state_tokens[2] != state_tokens[3]) {
1978 const int first_row = state_tokens[2];
1979 const int last_row = state_tokens[3];
1981 for (row = first_row; row <= last_row; row++) {
1982 state_tokens[2] = state_tokens[3] = row;
1984 idx = add_state_reference(prog->Parameters, state_tokens);
1985 if (param_var->param_binding_begin == ~0U)
1986 param_var->param_binding_begin = idx;
1987 param_var->param_binding_length++;
1991 idx = add_state_reference(prog->Parameters, state_tokens);
1992 if (param_var->param_binding_begin == ~0U)
1993 param_var->param_binding_begin = idx;
1994 param_var->param_binding_length++;
2002 initialize_symbol_from_const(struct gl_program *prog,
2003 struct asm_symbol *param_var,
2004 const struct asm_vector *vec)
2006 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2007 NULL, vec->count, GL_NONE, vec->data,
2010 param_var->type = at_param;
2012 if (param_var->param_binding_begin == ~0U)
2013 param_var->param_binding_begin = idx;
2014 param_var->param_binding_length++;
2021 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2025 fprintf(stderr, "line %u, char %u: error: %s\n", locp->first_line,
2026 locp->first_column, s);
2030 _mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2031 GLsizei len, struct asm_parser_state *state)
2033 struct gl_program_constants limits;
2034 struct asm_instruction *inst;
2038 state->prog->Target = target;
2039 state->prog->Parameters = _mesa_new_parameter_list();
2041 /* Make a copy of the program string and force it to be NUL-terminated.
2043 strz = (GLubyte *) _mesa_malloc(len + 1);
2045 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2048 _mesa_memcpy (strz, str, len);
2051 state->prog->String = strz;
2053 state->st = _mesa_symbol_table_ctor();
2055 /* All of these limits should come from ctx.
2057 limits.MaxInstructions = 128;
2058 limits.MaxAluInstructions = 128;
2059 limits.MaxTexInstructions = 128;
2060 limits.MaxTexIndirections = 128;
2061 limits.MaxAttribs = 16;
2062 limits.MaxTemps = 128;
2063 limits.MaxAddressRegs = 1;
2064 limits.MaxParameters = 128;
2065 limits.MaxLocalParams = 256;
2066 limits.MaxEnvParams = 128;
2067 limits.MaxNativeInstructions = 128;
2068 limits.MaxNativeAluInstructions = 128;
2069 limits.MaxNativeTexInstructions = 128;
2070 limits.MaxNativeTexIndirections = 128;
2071 limits.MaxNativeAttribs = 16;
2072 limits.MaxNativeTemps = 128;
2073 limits.MaxNativeAddressRegs = 1;
2074 limits.MaxNativeParameters = 128;
2075 limits.MaxUniformComponents = 0;
2077 state->limits = & limits;
2079 state->MaxTextureImageUnits = 16;
2080 state->MaxTextureCoordUnits = 8;
2081 state->MaxTextureUnits = 8;
2082 state->MaxClipPlanes = 6;
2083 state->MaxLights = 8;
2084 state->MaxProgramMatrices = 8;
2086 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2087 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2089 _mesa_set_program_error(ctx, -1, NULL);
2091 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2093 _mesa_program_lexer_dtor(state->scanner);
2096 if (! _mesa_layout_parameters(state)) {
2097 fprintf(stderr, "error: layout\n");
2103 /* Add one instruction to store the "END" instruction.
2105 state->prog->Instructions =
2106 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2107 inst = state->inst_head;
2108 for (i = 0; i < state->prog->NumInstructions; i++) {
2109 struct asm_instruction *const temp = inst->next;
2111 state->prog->Instructions[i] = inst->Base;
2117 /* Finally, tag on an OPCODE_END instruction */
2119 const GLuint numInst = state->prog->NumInstructions;
2120 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2121 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2123 state->prog->NumInstructions++;
2126 * Initialize native counts to logical counts. The device driver may
2127 * change them if program is translated into a hardware program.
2129 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2130 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2131 state->prog->NumNativeParameters = state->prog->NumParameters;
2132 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2133 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;