program: Remove OPCODE_KIL_NV.
[mesa.git] / src / mesa / program / program_parse.y
1 %{
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "main/mtypes.h"
31 #include "main/imports.h"
32 #include "program/program.h"
33 #include "program/prog_parameter.h"
34 #include "program/prog_parameter_layout.h"
35 #include "program/prog_statevars.h"
36 #include "program/prog_instruction.h"
37
38 #include "program/symbol_table.h"
39 #include "program/program_parser.h"
40
41 extern void *yy_scan_string(char *);
42 extern void yy_delete_buffer(void *);
43
44 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
45 char *name, enum asm_type t, struct YYLTYPE *locp);
46
47 static int add_state_reference(struct gl_program_parameter_list *param_list,
48 const gl_state_index tokens[STATE_LENGTH]);
49
50 static int initialize_symbol_from_state(struct gl_program *prog,
51 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
52
53 static int initialize_symbol_from_param(struct gl_program *prog,
54 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
55
56 static int initialize_symbol_from_const(struct gl_program *prog,
57 struct asm_symbol *param_var, const struct asm_vector *vec,
58 GLboolean allowSwizzle);
59
60 static int yyparse(struct asm_parser_state *state);
61
62 static char *make_error_string(const char *fmt, ...);
63
64 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
65 const char *s);
66
67 static int validate_inputs(struct YYLTYPE *locp,
68 struct asm_parser_state *state);
69
70 static void init_dst_reg(struct prog_dst_register *r);
71
72 static void set_dst_reg(struct prog_dst_register *r,
73 gl_register_file file, GLint index);
74
75 static void init_src_reg(struct asm_src_register *r);
76
77 static void set_src_reg(struct asm_src_register *r,
78 gl_register_file file, GLint index);
79
80 static void set_src_reg_swz(struct asm_src_register *r,
81 gl_register_file file, GLint index, GLuint swizzle);
82
83 static void asm_instruction_set_operands(struct asm_instruction *inst,
84 const struct prog_dst_register *dst, const struct asm_src_register *src0,
85 const struct asm_src_register *src1, const struct asm_src_register *src2);
86
87 static struct asm_instruction *asm_instruction_ctor(enum prog_opcode op,
88 const struct prog_dst_register *dst, const struct asm_src_register *src0,
89 const struct asm_src_register *src1, const struct asm_src_register *src2);
90
91 static struct asm_instruction *asm_instruction_copy_ctor(
92 const struct prog_instruction *base, const struct prog_dst_register *dst,
93 const struct asm_src_register *src0, const struct asm_src_register *src1,
94 const struct asm_src_register *src2);
95
96 #ifndef FALSE
97 #define FALSE 0
98 #define TRUE (!FALSE)
99 #endif
100
101 #define YYLLOC_DEFAULT(Current, Rhs, N) \
102 do { \
103 if (N) { \
104 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
105 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
106 (Current).position = YYRHSLOC(Rhs, 1).position; \
107 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
108 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
109 } else { \
110 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
111 (Current).last_line = (Current).first_line; \
112 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
113 (Current).last_column = (Current).first_column; \
114 (Current).position = YYRHSLOC(Rhs, 0).position \
115 + (Current).first_column; \
116 } \
117 } while(0)
118 %}
119
120 %pure-parser
121 %locations
122 %lex-param { struct asm_parser_state *state }
123 %parse-param { struct asm_parser_state *state }
124 %error-verbose
125
126 %union {
127 struct asm_instruction *inst;
128 struct asm_symbol *sym;
129 struct asm_symbol temp_sym;
130 struct asm_swizzle_mask swiz_mask;
131 struct asm_src_register src_reg;
132 struct prog_dst_register dst_reg;
133 struct prog_instruction temp_inst;
134 char *string;
135 unsigned result;
136 unsigned attrib;
137 int integer;
138 float real;
139 gl_state_index state[STATE_LENGTH];
140 int negate;
141 struct asm_vector vector;
142 enum prog_opcode opcode;
143
144 struct {
145 unsigned swz;
146 unsigned rgba_valid:1;
147 unsigned xyzw_valid:1;
148 unsigned negate:1;
149 } ext_swizzle;
150 }
151
152 %token ARBvp_10 ARBfp_10
153
154 /* Tokens for assembler pseudo-ops */
155 %token <integer> ADDRESS
156 %token ALIAS ATTRIB
157 %token OPTION OUTPUT
158 %token PARAM
159 %token <integer> TEMP
160 %token END
161
162 /* Tokens for instructions */
163 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
164 %token <temp_inst> ARL KIL SWZ TXD_OP
165
166 %token <integer> INTEGER
167 %token <real> REAL
168
169 %token AMBIENT ATTENUATION
170 %token BACK
171 %token CLIP COLOR
172 %token DEPTH DIFFUSE DIRECTION
173 %token EMISSION ENV EYE
174 %token FOG FOGCOORD FRAGMENT FRONT
175 %token HALF
176 %token INVERSE INVTRANS
177 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
178 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
179 %token NORMAL
180 %token OBJECT
181 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
182 %token RANGE RESULT ROW
183 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
184 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
185 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
186 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
187 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
188 %token VERTEX VTXATTRIB
189 %token WEIGHT
190
191 %token <string> IDENTIFIER USED_IDENTIFIER
192 %type <string> string
193 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
194 %token DOT_DOT
195 %token DOT
196
197 %type <inst> instruction ALU_instruction TexInstruction
198 %type <inst> ARL_instruction VECTORop_instruction
199 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
200 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
201 %type <inst> KIL_instruction
202
203 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
204 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
205 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
206 %type <ext_swizzle> extSwizComp extSwizSel
207 %type <swiz_mask> optionalMask
208
209 %type <sym> progParamArray
210 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
211 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
212 %type <sym> addrReg
213 %type <swiz_mask> addrComponent addrWriteMask
214
215 %type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
216
217 %type <result> resultBinding resultColBinding
218 %type <integer> optFaceType optColorType
219 %type <integer> optResultFaceType optResultColorType
220
221 %type <integer> optTexImageUnitNum texImageUnitNum
222 %type <integer> optTexCoordUnitNum texCoordUnitNum
223 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
224 %type <integer> texImageUnit texTarget
225 %type <integer> vtxAttribNum
226
227 %type <attrib> attribBinding vtxAttribItem fragAttribItem
228
229 %type <temp_sym> paramSingleInit paramSingleItemDecl
230 %type <integer> optArraySize
231
232 %type <state> stateSingleItem stateMultipleItem
233 %type <state> stateMaterialItem
234 %type <state> stateLightItem stateLightModelItem stateLightProdItem
235 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
236 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
237 %type <state> stateTexEnvItem stateDepthItem
238
239 %type <state> stateLModProperty
240 %type <state> stateMatrixName optMatrixRows
241
242 %type <integer> stateMatProperty
243 %type <integer> stateLightProperty stateSpotProperty
244 %type <integer> stateLightNumber stateLProdProperty
245 %type <integer> stateTexGenType stateTexGenCoord
246 %type <integer> stateTexEnvProperty
247 %type <integer> stateFogProperty
248 %type <integer> stateClipPlaneNum
249 %type <integer> statePointProperty
250
251 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
252 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
253 %type <integer> stateProgramMatNum
254
255 %type <integer> ambDiffSpecProperty
256
257 %type <state> programSingleItem progEnvParam progLocalParam
258 %type <state> programMultipleItem progEnvParams progLocalParams
259
260 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
261 %type <temp_sym> paramSingleItemUse
262
263 %type <integer> progEnvParamNum progLocalParamNum
264 %type <state> progEnvParamNums progLocalParamNums
265
266 %type <vector> paramConstDecl paramConstUse
267 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
268 %type <real> signedFloatConstant
269 %type <negate> optionalSign
270
271 %{
272 extern int
273 _mesa_program_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
274 void *yyscanner);
275
276 static int
277 yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
278 struct asm_parser_state *state)
279 {
280 return _mesa_program_lexer_lex(yylval_param, yylloc_param, state->scanner);
281 }
282 %}
283
284 %%
285
286 program: language optionSequence statementSequence END
287 ;
288
289 language: ARBvp_10
290 {
291 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
292 yyerror(& @1, state, "invalid fragment program header");
293
294 }
295 state->mode = ARB_vertex;
296 }
297 | ARBfp_10
298 {
299 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
300 yyerror(& @1, state, "invalid vertex program header");
301 }
302 state->mode = ARB_fragment;
303
304 state->option.TexRect =
305 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
306 }
307 ;
308
309 optionSequence: optionSequence option
310 |
311 ;
312
313 option: OPTION string ';'
314 {
315 int valid = 0;
316
317 if (state->mode == ARB_vertex) {
318 valid = _mesa_ARBvp_parse_option(state, $2);
319 } else if (state->mode == ARB_fragment) {
320 valid = _mesa_ARBfp_parse_option(state, $2);
321 }
322
323
324 free($2);
325
326 if (!valid) {
327 const char *const err_str = (state->mode == ARB_vertex)
328 ? "invalid ARB vertex program option"
329 : "invalid ARB fragment program option";
330
331 yyerror(& @2, state, err_str);
332 YYERROR;
333 }
334 }
335 ;
336
337 statementSequence: statementSequence statement
338 |
339 ;
340
341 statement: instruction ';'
342 {
343 if ($1 != NULL) {
344 if (state->inst_tail == NULL) {
345 state->inst_head = $1;
346 } else {
347 state->inst_tail->next = $1;
348 }
349
350 state->inst_tail = $1;
351 $1->next = NULL;
352
353 state->prog->NumInstructions++;
354 }
355 }
356 | namingStatement ';'
357 ;
358
359 instruction: ALU_instruction
360 {
361 $$ = $1;
362 state->prog->NumAluInstructions++;
363 }
364 | TexInstruction
365 {
366 $$ = $1;
367 state->prog->NumTexInstructions++;
368 }
369 ;
370
371 ALU_instruction: ARL_instruction
372 | VECTORop_instruction
373 | SCALARop_instruction
374 | BINSCop_instruction
375 | BINop_instruction
376 | TRIop_instruction
377 | SWZ_instruction
378 ;
379
380 TexInstruction: SAMPLE_instruction
381 | KIL_instruction
382 | TXD_instruction
383 ;
384
385 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
386 {
387 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
388 }
389 ;
390
391 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
392 {
393 if ($1.Opcode == OPCODE_DDY)
394 state->fragment.UsesDFdy = 1;
395 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
396 }
397 ;
398
399 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
400 {
401 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
402 }
403 ;
404
405 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
406 {
407 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
408 }
409 ;
410
411
412 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
413 {
414 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
415 }
416 ;
417
418 TRIop_instruction: TRI_OP maskedDstReg ','
419 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
420 {
421 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
422 }
423 ;
424
425 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
426 {
427 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
428 if ($$ != NULL) {
429 const GLbitfield tex_mask = (1U << $6);
430 GLbitfield shadow_tex = 0;
431 GLbitfield target_mask = 0;
432
433
434 $$->Base.TexSrcUnit = $6;
435
436 if ($8 < 0) {
437 shadow_tex = tex_mask;
438
439 $$->Base.TexSrcTarget = -$8;
440 $$->Base.TexShadow = 1;
441 } else {
442 $$->Base.TexSrcTarget = $8;
443 }
444
445 target_mask = (1U << $$->Base.TexSrcTarget);
446
447 /* If this texture unit was previously accessed and that access
448 * had a different texture target, generate an error.
449 *
450 * If this texture unit was previously accessed and that access
451 * had a different shadow mode, generate an error.
452 */
453 if ((state->prog->TexturesUsed[$6] != 0)
454 && ((state->prog->TexturesUsed[$6] != target_mask)
455 || ((state->prog->ShadowSamplers & tex_mask)
456 != shadow_tex))) {
457 yyerror(& @8, state,
458 "multiple targets used on one texture image unit");
459 YYERROR;
460 }
461
462
463 state->prog->TexturesUsed[$6] |= target_mask;
464 state->prog->ShadowSamplers |= shadow_tex;
465 }
466 }
467 ;
468
469 KIL_instruction: KIL swizzleSrcReg
470 {
471 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
472 state->fragment.UsesKill = 1;
473 }
474 ;
475
476 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
477 {
478 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
479 if ($$ != NULL) {
480 const GLbitfield tex_mask = (1U << $10);
481 GLbitfield shadow_tex = 0;
482 GLbitfield target_mask = 0;
483
484
485 $$->Base.TexSrcUnit = $10;
486
487 if ($12 < 0) {
488 shadow_tex = tex_mask;
489
490 $$->Base.TexSrcTarget = -$12;
491 $$->Base.TexShadow = 1;
492 } else {
493 $$->Base.TexSrcTarget = $12;
494 }
495
496 target_mask = (1U << $$->Base.TexSrcTarget);
497
498 /* If this texture unit was previously accessed and that access
499 * had a different texture target, generate an error.
500 *
501 * If this texture unit was previously accessed and that access
502 * had a different shadow mode, generate an error.
503 */
504 if ((state->prog->TexturesUsed[$10] != 0)
505 && ((state->prog->TexturesUsed[$10] != target_mask)
506 || ((state->prog->ShadowSamplers & tex_mask)
507 != shadow_tex))) {
508 yyerror(& @12, state,
509 "multiple targets used on one texture image unit");
510 YYERROR;
511 }
512
513
514 state->prog->TexturesUsed[$10] |= target_mask;
515 state->prog->ShadowSamplers |= shadow_tex;
516 }
517 }
518 ;
519
520 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
521 {
522 $$ = $2;
523 }
524 ;
525
526 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
527 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
528 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
529 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
530 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
531 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
532 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
533 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
534 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
535 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
536 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
537 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
538 ;
539
540 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
541 {
542 /* FIXME: Is this correct? Should the extenedSwizzle be applied
543 * FIXME: to the existing swizzle?
544 */
545 $4.Base.Swizzle = $6.swizzle;
546 $4.Base.Negate = $6.mask;
547
548 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
549 }
550 ;
551
552 scalarSrcReg: optionalSign scalarUse
553 {
554 $$ = $2;
555
556 if ($1) {
557 $$.Base.Negate = ~$$.Base.Negate;
558 }
559 }
560 | optionalSign '|' scalarUse '|'
561 {
562 $$ = $3;
563
564 if (!state->option.NV_fragment) {
565 yyerror(& @2, state, "unexpected character '|'");
566 YYERROR;
567 }
568
569 if ($1) {
570 $$.Base.Negate = ~$$.Base.Negate;
571 }
572
573 $$.Base.Abs = 1;
574 }
575 ;
576
577 scalarUse: srcReg scalarSuffix
578 {
579 $$ = $1;
580
581 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
582 $2.swizzle);
583 }
584 | paramConstScalarUse
585 {
586 struct asm_symbol temp_sym;
587
588 if (!state->option.NV_fragment) {
589 yyerror(& @1, state, "expected scalar suffix");
590 YYERROR;
591 }
592
593 memset(& temp_sym, 0, sizeof(temp_sym));
594 temp_sym.param_binding_begin = ~0;
595 initialize_symbol_from_const(state->prog, & temp_sym, & $1, GL_TRUE);
596
597 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
598 temp_sym.param_binding_begin,
599 temp_sym.param_binding_swizzle);
600 }
601 ;
602
603 swizzleSrcReg: optionalSign srcReg swizzleSuffix
604 {
605 $$ = $2;
606
607 if ($1) {
608 $$.Base.Negate = ~$$.Base.Negate;
609 }
610
611 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
612 $3.swizzle);
613 }
614 | optionalSign '|' srcReg swizzleSuffix '|'
615 {
616 $$ = $3;
617
618 if (!state->option.NV_fragment) {
619 yyerror(& @2, state, "unexpected character '|'");
620 YYERROR;
621 }
622
623 if ($1) {
624 $$.Base.Negate = ~$$.Base.Negate;
625 }
626
627 $$.Base.Abs = 1;
628 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
629 $4.swizzle);
630 }
631
632 ;
633
634 maskedDstReg: dstReg optionalMask optionalCcMask
635 {
636 $$ = $1;
637 $$.WriteMask = $2.mask;
638 $$.CondMask = $3.CondMask;
639 $$.CondSwizzle = $3.CondSwizzle;
640
641 if ($$.File == PROGRAM_OUTPUT) {
642 /* Technically speaking, this should check that it is in
643 * vertex program mode. However, PositionInvariant can never be
644 * set in fragment program mode, so it is somewhat irrelevant.
645 */
646 if (state->option.PositionInvariant
647 && ($$.Index == VARYING_SLOT_POS)) {
648 yyerror(& @1, state, "position-invariant programs cannot "
649 "write position");
650 YYERROR;
651 }
652
653 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
654 }
655 }
656 ;
657
658 maskedAddrReg: addrReg addrWriteMask
659 {
660 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
661 $$.WriteMask = $2.mask;
662 }
663 ;
664
665 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
666 {
667 const unsigned xyzw_valid =
668 ($1.xyzw_valid << 0)
669 | ($3.xyzw_valid << 1)
670 | ($5.xyzw_valid << 2)
671 | ($7.xyzw_valid << 3);
672 const unsigned rgba_valid =
673 ($1.rgba_valid << 0)
674 | ($3.rgba_valid << 1)
675 | ($5.rgba_valid << 2)
676 | ($7.rgba_valid << 3);
677
678 /* All of the swizzle components have to be valid in either RGBA
679 * or XYZW. Note that 0 and 1 are valid in both, so both masks
680 * can have some bits set.
681 *
682 * We somewhat deviate from the spec here. It would be really hard
683 * to figure out which component is the error, and there probably
684 * isn't a lot of benefit.
685 */
686 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
687 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
688 "components");
689 YYERROR;
690 }
691
692 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
693 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
694 | ($7.negate << 3);
695 }
696 ;
697
698 extSwizComp: optionalSign extSwizSel
699 {
700 $$ = $2;
701 $$.negate = ($1) ? 1 : 0;
702 }
703 ;
704
705 extSwizSel: INTEGER
706 {
707 if (($1 != 0) && ($1 != 1)) {
708 yyerror(& @1, state, "invalid extended swizzle selector");
709 YYERROR;
710 }
711
712 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
713 $$.negate = 0;
714
715 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
716 * swizzle names.
717 */
718 $$.xyzw_valid = 1;
719 $$.rgba_valid = 1;
720 }
721 | string
722 {
723 char s;
724
725 if (strlen($1) > 1) {
726 yyerror(& @1, state, "invalid extended swizzle selector");
727 YYERROR;
728 }
729
730 s = $1[0];
731 free($1);
732
733 $$.rgba_valid = 0;
734 $$.xyzw_valid = 0;
735 $$.negate = 0;
736
737 switch (s) {
738 case 'x':
739 $$.swz = SWIZZLE_X;
740 $$.xyzw_valid = 1;
741 break;
742 case 'y':
743 $$.swz = SWIZZLE_Y;
744 $$.xyzw_valid = 1;
745 break;
746 case 'z':
747 $$.swz = SWIZZLE_Z;
748 $$.xyzw_valid = 1;
749 break;
750 case 'w':
751 $$.swz = SWIZZLE_W;
752 $$.xyzw_valid = 1;
753 break;
754
755 case 'r':
756 $$.swz = SWIZZLE_X;
757 $$.rgba_valid = 1;
758 break;
759 case 'g':
760 $$.swz = SWIZZLE_Y;
761 $$.rgba_valid = 1;
762 break;
763 case 'b':
764 $$.swz = SWIZZLE_Z;
765 $$.rgba_valid = 1;
766 break;
767 case 'a':
768 $$.swz = SWIZZLE_W;
769 $$.rgba_valid = 1;
770 break;
771
772 default:
773 yyerror(& @1, state, "invalid extended swizzle selector");
774 YYERROR;
775 break;
776 }
777 }
778 ;
779
780 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
781 {
782 struct asm_symbol *const s = (struct asm_symbol *)
783 _mesa_symbol_table_find_symbol(state->st, 0, $1);
784
785 free($1);
786
787 if (s == NULL) {
788 yyerror(& @1, state, "invalid operand variable");
789 YYERROR;
790 } else if ((s->type != at_param) && (s->type != at_temp)
791 && (s->type != at_attrib)) {
792 yyerror(& @1, state, "invalid operand variable");
793 YYERROR;
794 } else if ((s->type == at_param) && s->param_is_array) {
795 yyerror(& @1, state, "non-array access to array PARAM");
796 YYERROR;
797 }
798
799 init_src_reg(& $$);
800 switch (s->type) {
801 case at_temp:
802 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
803 break;
804 case at_param:
805 set_src_reg_swz(& $$, s->param_binding_type,
806 s->param_binding_begin,
807 s->param_binding_swizzle);
808 break;
809 case at_attrib:
810 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
811 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
812
813 if (!validate_inputs(& @1, state)) {
814 YYERROR;
815 }
816 break;
817
818 default:
819 YYERROR;
820 break;
821 }
822 }
823 | attribBinding
824 {
825 set_src_reg(& $$, PROGRAM_INPUT, $1);
826 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
827
828 if (!validate_inputs(& @1, state)) {
829 YYERROR;
830 }
831 }
832 | progParamArray '[' progParamArrayMem ']'
833 {
834 if (! $3.Base.RelAddr
835 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
836 yyerror(& @3, state, "out of bounds array access");
837 YYERROR;
838 }
839
840 init_src_reg(& $$);
841 $$.Base.File = $1->param_binding_type;
842
843 if ($3.Base.RelAddr) {
844 state->prog->IndirectRegisterFiles |= (1 << $$.Base.File);
845 $1->param_accessed_indirectly = 1;
846
847 $$.Base.RelAddr = 1;
848 $$.Base.Index = $3.Base.Index;
849 $$.Symbol = $1;
850 } else {
851 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
852 }
853 }
854 | paramSingleItemUse
855 {
856 gl_register_file file = ($1.name != NULL)
857 ? $1.param_binding_type
858 : PROGRAM_CONSTANT;
859 set_src_reg_swz(& $$, file, $1.param_binding_begin,
860 $1.param_binding_swizzle);
861 }
862 ;
863
864 dstReg: resultBinding
865 {
866 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
867 }
868 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
869 {
870 struct asm_symbol *const s = (struct asm_symbol *)
871 _mesa_symbol_table_find_symbol(state->st, 0, $1);
872
873 free($1);
874
875 if (s == NULL) {
876 yyerror(& @1, state, "invalid operand variable");
877 YYERROR;
878 } else if ((s->type != at_output) && (s->type != at_temp)) {
879 yyerror(& @1, state, "invalid operand variable");
880 YYERROR;
881 }
882
883 switch (s->type) {
884 case at_temp:
885 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
886 break;
887 case at_output:
888 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
889 break;
890 default:
891 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
892 break;
893 }
894 }
895 ;
896
897 progParamArray: USED_IDENTIFIER
898 {
899 struct asm_symbol *const s = (struct asm_symbol *)
900 _mesa_symbol_table_find_symbol(state->st, 0, $1);
901
902 free($1);
903
904 if (s == NULL) {
905 yyerror(& @1, state, "invalid operand variable");
906 YYERROR;
907 } else if ((s->type != at_param) || !s->param_is_array) {
908 yyerror(& @1, state, "array access to non-PARAM variable");
909 YYERROR;
910 } else {
911 $$ = s;
912 }
913 }
914 ;
915
916 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
917
918 progParamArrayAbs: INTEGER
919 {
920 init_src_reg(& $$);
921 $$.Base.Index = $1;
922 }
923 ;
924
925 progParamArrayRel: addrReg addrComponent addrRegRelOffset
926 {
927 /* FINISHME: Add support for multiple address registers.
928 */
929 /* FINISHME: Add support for 4-component address registers.
930 */
931 init_src_reg(& $$);
932 $$.Base.RelAddr = 1;
933 $$.Base.Index = $3;
934 }
935 ;
936
937 addrRegRelOffset: { $$ = 0; }
938 | '+' addrRegPosOffset { $$ = $2; }
939 | '-' addrRegNegOffset { $$ = -$2; }
940 ;
941
942 addrRegPosOffset: INTEGER
943 {
944 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
945 char s[100];
946 _mesa_snprintf(s, sizeof(s),
947 "relative address offset too large (%d)", $1);
948 yyerror(& @1, state, s);
949 YYERROR;
950 } else {
951 $$ = $1;
952 }
953 }
954 ;
955
956 addrRegNegOffset: INTEGER
957 {
958 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
959 char s[100];
960 _mesa_snprintf(s, sizeof(s),
961 "relative address offset too large (%d)", $1);
962 yyerror(& @1, state, s);
963 YYERROR;
964 } else {
965 $$ = $1;
966 }
967 }
968 ;
969
970 addrReg: USED_IDENTIFIER
971 {
972 struct asm_symbol *const s = (struct asm_symbol *)
973 _mesa_symbol_table_find_symbol(state->st, 0, $1);
974
975 free($1);
976
977 if (s == NULL) {
978 yyerror(& @1, state, "invalid array member");
979 YYERROR;
980 } else if (s->type != at_address) {
981 yyerror(& @1, state,
982 "invalid variable for indexed array access");
983 YYERROR;
984 } else {
985 $$ = s;
986 }
987 }
988 ;
989
990 addrComponent: MASK1
991 {
992 if ($1.mask != WRITEMASK_X) {
993 yyerror(& @1, state, "invalid address component selector");
994 YYERROR;
995 } else {
996 $$ = $1;
997 }
998 }
999 ;
1000
1001 addrWriteMask: MASK1
1002 {
1003 if ($1.mask != WRITEMASK_X) {
1004 yyerror(& @1, state,
1005 "address register write mask must be \".x\"");
1006 YYERROR;
1007 } else {
1008 $$ = $1;
1009 }
1010 }
1011 ;
1012
1013 scalarSuffix: MASK1;
1014
1015 swizzleSuffix: MASK1
1016 | MASK4
1017 | SWIZZLE
1018 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1019 ;
1020
1021 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1022 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1023 ;
1024
1025 optionalCcMask: '(' ccTest ')'
1026 {
1027 $$ = $2;
1028 }
1029 | '(' ccTest2 ')'
1030 {
1031 $$ = $2;
1032 }
1033 |
1034 {
1035 $$.CondMask = COND_TR;
1036 $$.CondSwizzle = SWIZZLE_NOOP;
1037 }
1038 ;
1039
1040 ccTest: ccMaskRule swizzleSuffix
1041 {
1042 $$ = $1;
1043 $$.CondSwizzle = $2.swizzle;
1044 }
1045 ;
1046
1047 ccTest2: ccMaskRule2 swizzleSuffix
1048 {
1049 $$ = $1;
1050 $$.CondSwizzle = $2.swizzle;
1051 }
1052 ;
1053
1054 ccMaskRule: IDENTIFIER
1055 {
1056 const int cond = _mesa_parse_cc($1);
1057 if ((cond == 0) || ($1[2] != '\0')) {
1058 char *const err_str =
1059 make_error_string("invalid condition code \"%s\"", $1);
1060
1061 yyerror(& @1, state, (err_str != NULL)
1062 ? err_str : "invalid condition code");
1063
1064 if (err_str != NULL) {
1065 free(err_str);
1066 }
1067
1068 YYERROR;
1069 }
1070
1071 $$.CondMask = cond;
1072 $$.CondSwizzle = SWIZZLE_NOOP;
1073 }
1074 ;
1075
1076 ccMaskRule2: USED_IDENTIFIER
1077 {
1078 const int cond = _mesa_parse_cc($1);
1079 if ((cond == 0) || ($1[2] != '\0')) {
1080 char *const err_str =
1081 make_error_string("invalid condition code \"%s\"", $1);
1082
1083 yyerror(& @1, state, (err_str != NULL)
1084 ? err_str : "invalid condition code");
1085
1086 if (err_str != NULL) {
1087 free(err_str);
1088 }
1089
1090 YYERROR;
1091 }
1092
1093 $$.CondMask = cond;
1094 $$.CondSwizzle = SWIZZLE_NOOP;
1095 }
1096 ;
1097
1098 namingStatement: ATTRIB_statement
1099 | PARAM_statement
1100 | TEMP_statement
1101 | ADDRESS_statement
1102 | OUTPUT_statement
1103 | ALIAS_statement
1104 ;
1105
1106 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1107 {
1108 struct asm_symbol *const s =
1109 declare_variable(state, $2, at_attrib, & @2);
1110
1111 if (s == NULL) {
1112 free($2);
1113 YYERROR;
1114 } else {
1115 s->attrib_binding = $4;
1116 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
1117
1118 if (!validate_inputs(& @4, state)) {
1119 YYERROR;
1120 }
1121 }
1122 }
1123 ;
1124
1125 attribBinding: VERTEX vtxAttribItem
1126 {
1127 $$ = $2;
1128 }
1129 | FRAGMENT fragAttribItem
1130 {
1131 $$ = $2;
1132 }
1133 ;
1134
1135 vtxAttribItem: POSITION
1136 {
1137 $$ = VERT_ATTRIB_POS;
1138 }
1139 | WEIGHT vtxOptWeightNum
1140 {
1141 $$ = VERT_ATTRIB_WEIGHT;
1142 }
1143 | NORMAL
1144 {
1145 $$ = VERT_ATTRIB_NORMAL;
1146 }
1147 | COLOR optColorType
1148 {
1149 $$ = VERT_ATTRIB_COLOR0 + $2;
1150 }
1151 | FOGCOORD
1152 {
1153 $$ = VERT_ATTRIB_FOG;
1154 }
1155 | TEXCOORD optTexCoordUnitNum
1156 {
1157 $$ = VERT_ATTRIB_TEX0 + $2;
1158 }
1159 | MATRIXINDEX '[' vtxWeightNum ']'
1160 {
1161 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1162 YYERROR;
1163 }
1164 | VTXATTRIB '[' vtxAttribNum ']'
1165 {
1166 $$ = VERT_ATTRIB_GENERIC0 + $3;
1167 }
1168 ;
1169
1170 vtxAttribNum: INTEGER
1171 {
1172 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1173 yyerror(& @1, state, "invalid vertex attribute reference");
1174 YYERROR;
1175 }
1176
1177 $$ = $1;
1178 }
1179 ;
1180
1181 vtxOptWeightNum: | '[' vtxWeightNum ']';
1182 vtxWeightNum: INTEGER;
1183
1184 fragAttribItem: POSITION
1185 {
1186 $$ = VARYING_SLOT_POS;
1187 }
1188 | COLOR optColorType
1189 {
1190 $$ = VARYING_SLOT_COL0 + $2;
1191 }
1192 | FOGCOORD
1193 {
1194 $$ = VARYING_SLOT_FOGC;
1195 }
1196 | TEXCOORD optTexCoordUnitNum
1197 {
1198 $$ = VARYING_SLOT_TEX0 + $2;
1199 }
1200 ;
1201
1202 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1203
1204 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1205 {
1206 struct asm_symbol *const s =
1207 declare_variable(state, $2, at_param, & @2);
1208
1209 if (s == NULL) {
1210 free($2);
1211 YYERROR;
1212 } else {
1213 s->param_binding_type = $3.param_binding_type;
1214 s->param_binding_begin = $3.param_binding_begin;
1215 s->param_binding_length = $3.param_binding_length;
1216 s->param_binding_swizzle = $3.param_binding_swizzle;
1217 s->param_is_array = 0;
1218 }
1219 }
1220 ;
1221
1222 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1223 {
1224 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1225 free($2);
1226 yyerror(& @4, state,
1227 "parameter array size and number of bindings must match");
1228 YYERROR;
1229 } else {
1230 struct asm_symbol *const s =
1231 declare_variable(state, $2, $6.type, & @2);
1232
1233 if (s == NULL) {
1234 free($2);
1235 YYERROR;
1236 } else {
1237 s->param_binding_type = $6.param_binding_type;
1238 s->param_binding_begin = $6.param_binding_begin;
1239 s->param_binding_length = $6.param_binding_length;
1240 s->param_binding_swizzle = SWIZZLE_XYZW;
1241 s->param_is_array = 1;
1242 }
1243 }
1244 }
1245 ;
1246
1247 optArraySize:
1248 {
1249 $$ = 0;
1250 }
1251 | INTEGER
1252 {
1253 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1254 char msg[100];
1255 _mesa_snprintf(msg, sizeof(msg),
1256 "invalid parameter array size (size=%d max=%u)",
1257 $1, state->limits->MaxParameters);
1258 yyerror(& @1, state, msg);
1259 YYERROR;
1260 } else {
1261 $$ = $1;
1262 }
1263 }
1264 ;
1265
1266 paramSingleInit: '=' paramSingleItemDecl
1267 {
1268 $$ = $2;
1269 }
1270 ;
1271
1272 paramMultipleInit: '=' '{' paramMultInitList '}'
1273 {
1274 $$ = $3;
1275 }
1276 ;
1277
1278 paramMultInitList: paramMultipleItem
1279 | paramMultInitList ',' paramMultipleItem
1280 {
1281 $1.param_binding_length += $3.param_binding_length;
1282 $$ = $1;
1283 }
1284 ;
1285
1286 paramSingleItemDecl: stateSingleItem
1287 {
1288 memset(& $$, 0, sizeof($$));
1289 $$.param_binding_begin = ~0;
1290 initialize_symbol_from_state(state->prog, & $$, $1);
1291 }
1292 | programSingleItem
1293 {
1294 memset(& $$, 0, sizeof($$));
1295 $$.param_binding_begin = ~0;
1296 initialize_symbol_from_param(state->prog, & $$, $1);
1297 }
1298 | paramConstDecl
1299 {
1300 memset(& $$, 0, sizeof($$));
1301 $$.param_binding_begin = ~0;
1302 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1303 }
1304 ;
1305
1306 paramSingleItemUse: stateSingleItem
1307 {
1308 memset(& $$, 0, sizeof($$));
1309 $$.param_binding_begin = ~0;
1310 initialize_symbol_from_state(state->prog, & $$, $1);
1311 }
1312 | programSingleItem
1313 {
1314 memset(& $$, 0, sizeof($$));
1315 $$.param_binding_begin = ~0;
1316 initialize_symbol_from_param(state->prog, & $$, $1);
1317 }
1318 | paramConstUse
1319 {
1320 memset(& $$, 0, sizeof($$));
1321 $$.param_binding_begin = ~0;
1322 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1323 }
1324 ;
1325
1326 paramMultipleItem: stateMultipleItem
1327 {
1328 memset(& $$, 0, sizeof($$));
1329 $$.param_binding_begin = ~0;
1330 initialize_symbol_from_state(state->prog, & $$, $1);
1331 }
1332 | programMultipleItem
1333 {
1334 memset(& $$, 0, sizeof($$));
1335 $$.param_binding_begin = ~0;
1336 initialize_symbol_from_param(state->prog, & $$, $1);
1337 }
1338 | paramConstDecl
1339 {
1340 memset(& $$, 0, sizeof($$));
1341 $$.param_binding_begin = ~0;
1342 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1343 }
1344 ;
1345
1346 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1347 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1348 ;
1349
1350 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1351 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1352 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1353 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1354 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1355 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1356 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1357 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1358 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1359 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1360 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1361 ;
1362
1363 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1364 {
1365 memset($$, 0, sizeof($$));
1366 $$[0] = STATE_MATERIAL;
1367 $$[1] = $2;
1368 $$[2] = $3;
1369 }
1370 ;
1371
1372 stateMatProperty: ambDiffSpecProperty
1373 {
1374 $$ = $1;
1375 }
1376 | EMISSION
1377 {
1378 $$ = STATE_EMISSION;
1379 }
1380 | SHININESS
1381 {
1382 $$ = STATE_SHININESS;
1383 }
1384 ;
1385
1386 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1387 {
1388 memset($$, 0, sizeof($$));
1389 $$[0] = STATE_LIGHT;
1390 $$[1] = $3;
1391 $$[2] = $5;
1392 }
1393 ;
1394
1395 stateLightProperty: ambDiffSpecProperty
1396 {
1397 $$ = $1;
1398 }
1399 | POSITION
1400 {
1401 $$ = STATE_POSITION;
1402 }
1403 | ATTENUATION
1404 {
1405 if (!state->ctx->Extensions.EXT_point_parameters) {
1406 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1407 YYERROR;
1408 }
1409
1410 $$ = STATE_ATTENUATION;
1411 }
1412 | SPOT stateSpotProperty
1413 {
1414 $$ = $2;
1415 }
1416 | HALF
1417 {
1418 $$ = STATE_HALF_VECTOR;
1419 }
1420 ;
1421
1422 stateSpotProperty: DIRECTION
1423 {
1424 $$ = STATE_SPOT_DIRECTION;
1425 }
1426 ;
1427
1428 stateLightModelItem: LIGHTMODEL stateLModProperty
1429 {
1430 $$[0] = $2[0];
1431 $$[1] = $2[1];
1432 }
1433 ;
1434
1435 stateLModProperty: AMBIENT
1436 {
1437 memset($$, 0, sizeof($$));
1438 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1439 }
1440 | optFaceType SCENECOLOR
1441 {
1442 memset($$, 0, sizeof($$));
1443 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1444 $$[1] = $1;
1445 }
1446 ;
1447
1448 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1449 {
1450 memset($$, 0, sizeof($$));
1451 $$[0] = STATE_LIGHTPROD;
1452 $$[1] = $3;
1453 $$[2] = $5;
1454 $$[3] = $6;
1455 }
1456 ;
1457
1458 stateLProdProperty: ambDiffSpecProperty;
1459
1460 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1461 {
1462 memset($$, 0, sizeof($$));
1463 $$[0] = $3;
1464 $$[1] = $2;
1465 }
1466 ;
1467
1468 stateTexEnvProperty: COLOR
1469 {
1470 $$ = STATE_TEXENV_COLOR;
1471 }
1472 ;
1473
1474 ambDiffSpecProperty: AMBIENT
1475 {
1476 $$ = STATE_AMBIENT;
1477 }
1478 | DIFFUSE
1479 {
1480 $$ = STATE_DIFFUSE;
1481 }
1482 | SPECULAR
1483 {
1484 $$ = STATE_SPECULAR;
1485 }
1486 ;
1487
1488 stateLightNumber: INTEGER
1489 {
1490 if ((unsigned) $1 >= state->MaxLights) {
1491 yyerror(& @1, state, "invalid light selector");
1492 YYERROR;
1493 }
1494
1495 $$ = $1;
1496 }
1497 ;
1498
1499 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1500 {
1501 memset($$, 0, sizeof($$));
1502 $$[0] = STATE_TEXGEN;
1503 $$[1] = $2;
1504 $$[2] = $3 + $4;
1505 }
1506 ;
1507
1508 stateTexGenType: EYE
1509 {
1510 $$ = STATE_TEXGEN_EYE_S;
1511 }
1512 | OBJECT
1513 {
1514 $$ = STATE_TEXGEN_OBJECT_S;
1515 }
1516 ;
1517 stateTexGenCoord: TEXGEN_S
1518 {
1519 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1520 }
1521 | TEXGEN_T
1522 {
1523 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1524 }
1525 | TEXGEN_R
1526 {
1527 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1528 }
1529 | TEXGEN_Q
1530 {
1531 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1532 }
1533 ;
1534
1535 stateFogItem: FOG stateFogProperty
1536 {
1537 memset($$, 0, sizeof($$));
1538 $$[0] = $2;
1539 }
1540 ;
1541
1542 stateFogProperty: COLOR
1543 {
1544 $$ = STATE_FOG_COLOR;
1545 }
1546 | PARAMS
1547 {
1548 $$ = STATE_FOG_PARAMS;
1549 }
1550 ;
1551
1552 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1553 {
1554 memset($$, 0, sizeof($$));
1555 $$[0] = STATE_CLIPPLANE;
1556 $$[1] = $3;
1557 }
1558 ;
1559
1560 stateClipPlaneNum: INTEGER
1561 {
1562 if ((unsigned) $1 >= state->MaxClipPlanes) {
1563 yyerror(& @1, state, "invalid clip plane selector");
1564 YYERROR;
1565 }
1566
1567 $$ = $1;
1568 }
1569 ;
1570
1571 statePointItem: POINT_TOK statePointProperty
1572 {
1573 memset($$, 0, sizeof($$));
1574 $$[0] = $2;
1575 }
1576 ;
1577
1578 statePointProperty: SIZE_TOK
1579 {
1580 $$ = STATE_POINT_SIZE;
1581 }
1582 | ATTENUATION
1583 {
1584 $$ = STATE_POINT_ATTENUATION;
1585 }
1586 ;
1587
1588 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1589 {
1590 $$[0] = $1[0];
1591 $$[1] = $1[1];
1592 $$[2] = $4;
1593 $$[3] = $4;
1594 $$[4] = $1[2];
1595 }
1596 ;
1597
1598 stateMatrixRows: stateMatrixItem optMatrixRows
1599 {
1600 $$[0] = $1[0];
1601 $$[1] = $1[1];
1602 $$[2] = $2[2];
1603 $$[3] = $2[3];
1604 $$[4] = $1[2];
1605 }
1606 ;
1607
1608 optMatrixRows:
1609 {
1610 $$[2] = 0;
1611 $$[3] = 3;
1612 }
1613 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1614 {
1615 /* It seems logical that the matrix row range specifier would have
1616 * to specify a range or more than one row (i.e., $5 > $3).
1617 * However, the ARB_vertex_program spec says "a program will fail
1618 * to load if <a> is greater than <b>." This means that $3 == $5
1619 * is valid.
1620 */
1621 if ($3 > $5) {
1622 yyerror(& @3, state, "invalid matrix row range");
1623 YYERROR;
1624 }
1625
1626 $$[2] = $3;
1627 $$[3] = $5;
1628 }
1629 ;
1630
1631 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1632 {
1633 $$[0] = $2[0];
1634 $$[1] = $2[1];
1635 $$[2] = $3;
1636 }
1637 ;
1638
1639 stateOptMatModifier:
1640 {
1641 $$ = 0;
1642 }
1643 | stateMatModifier
1644 {
1645 $$ = $1;
1646 }
1647 ;
1648
1649 stateMatModifier: INVERSE
1650 {
1651 $$ = STATE_MATRIX_INVERSE;
1652 }
1653 | TRANSPOSE
1654 {
1655 $$ = STATE_MATRIX_TRANSPOSE;
1656 }
1657 | INVTRANS
1658 {
1659 $$ = STATE_MATRIX_INVTRANS;
1660 }
1661 ;
1662
1663 stateMatrixRowNum: INTEGER
1664 {
1665 if ($1 > 3) {
1666 yyerror(& @1, state, "invalid matrix row reference");
1667 YYERROR;
1668 }
1669
1670 $$ = $1;
1671 }
1672 ;
1673
1674 stateMatrixName: MODELVIEW stateOptModMatNum
1675 {
1676 $$[0] = STATE_MODELVIEW_MATRIX;
1677 $$[1] = $2;
1678 }
1679 | PROJECTION
1680 {
1681 $$[0] = STATE_PROJECTION_MATRIX;
1682 $$[1] = 0;
1683 }
1684 | MVP
1685 {
1686 $$[0] = STATE_MVP_MATRIX;
1687 $$[1] = 0;
1688 }
1689 | TEXTURE optTexCoordUnitNum
1690 {
1691 $$[0] = STATE_TEXTURE_MATRIX;
1692 $$[1] = $2;
1693 }
1694 | PALETTE '[' statePaletteMatNum ']'
1695 {
1696 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1697 YYERROR;
1698 }
1699 | MAT_PROGRAM '[' stateProgramMatNum ']'
1700 {
1701 $$[0] = STATE_PROGRAM_MATRIX;
1702 $$[1] = $3;
1703 }
1704 ;
1705
1706 stateOptModMatNum:
1707 {
1708 $$ = 0;
1709 }
1710 | '[' stateModMatNum ']'
1711 {
1712 $$ = $2;
1713 }
1714 ;
1715 stateModMatNum: INTEGER
1716 {
1717 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1718 * zero is valid.
1719 */
1720 if ($1 != 0) {
1721 yyerror(& @1, state, "invalid modelview matrix index");
1722 YYERROR;
1723 }
1724
1725 $$ = $1;
1726 }
1727 ;
1728 statePaletteMatNum: INTEGER
1729 {
1730 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1731 * through here. The error will be generated later.
1732 */
1733 $$ = $1;
1734 }
1735 ;
1736 stateProgramMatNum: INTEGER
1737 {
1738 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1739 yyerror(& @1, state, "invalid program matrix selector");
1740 YYERROR;
1741 }
1742
1743 $$ = $1;
1744 }
1745 ;
1746
1747 stateDepthItem: DEPTH RANGE
1748 {
1749 memset($$, 0, sizeof($$));
1750 $$[0] = STATE_DEPTH_RANGE;
1751 }
1752 ;
1753
1754
1755 programSingleItem: progEnvParam | progLocalParam;
1756
1757 programMultipleItem: progEnvParams | progLocalParams;
1758
1759 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1760 {
1761 memset($$, 0, sizeof($$));
1762 $$[0] = state->state_param_enum;
1763 $$[1] = STATE_ENV;
1764 $$[2] = $4[0];
1765 $$[3] = $4[1];
1766 }
1767 ;
1768
1769 progEnvParamNums: progEnvParamNum
1770 {
1771 $$[0] = $1;
1772 $$[1] = $1;
1773 }
1774 | progEnvParamNum DOT_DOT progEnvParamNum
1775 {
1776 $$[0] = $1;
1777 $$[1] = $3;
1778 }
1779 ;
1780
1781 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1782 {
1783 memset($$, 0, sizeof($$));
1784 $$[0] = state->state_param_enum;
1785 $$[1] = STATE_ENV;
1786 $$[2] = $4;
1787 $$[3] = $4;
1788 }
1789 ;
1790
1791 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1792 {
1793 memset($$, 0, sizeof($$));
1794 $$[0] = state->state_param_enum;
1795 $$[1] = STATE_LOCAL;
1796 $$[2] = $4[0];
1797 $$[3] = $4[1];
1798 }
1799
1800 progLocalParamNums: progLocalParamNum
1801 {
1802 $$[0] = $1;
1803 $$[1] = $1;
1804 }
1805 | progLocalParamNum DOT_DOT progLocalParamNum
1806 {
1807 $$[0] = $1;
1808 $$[1] = $3;
1809 }
1810 ;
1811
1812 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1813 {
1814 memset($$, 0, sizeof($$));
1815 $$[0] = state->state_param_enum;
1816 $$[1] = STATE_LOCAL;
1817 $$[2] = $4;
1818 $$[3] = $4;
1819 }
1820 ;
1821
1822 progEnvParamNum: INTEGER
1823 {
1824 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1825 yyerror(& @1, state, "invalid environment parameter reference");
1826 YYERROR;
1827 }
1828 $$ = $1;
1829 }
1830 ;
1831
1832 progLocalParamNum: INTEGER
1833 {
1834 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1835 yyerror(& @1, state, "invalid local parameter reference");
1836 YYERROR;
1837 }
1838 $$ = $1;
1839 }
1840 ;
1841
1842
1843
1844 paramConstDecl: paramConstScalarDecl | paramConstVector;
1845 paramConstUse: paramConstScalarUse | paramConstVector;
1846
1847 paramConstScalarDecl: signedFloatConstant
1848 {
1849 $$.count = 4;
1850 $$.data[0].f = $1;
1851 $$.data[1].f = $1;
1852 $$.data[2].f = $1;
1853 $$.data[3].f = $1;
1854 }
1855 ;
1856
1857 paramConstScalarUse: REAL
1858 {
1859 $$.count = 1;
1860 $$.data[0].f = $1;
1861 $$.data[1].f = $1;
1862 $$.data[2].f = $1;
1863 $$.data[3].f = $1;
1864 }
1865 | INTEGER
1866 {
1867 $$.count = 1;
1868 $$.data[0].f = (float) $1;
1869 $$.data[1].f = (float) $1;
1870 $$.data[2].f = (float) $1;
1871 $$.data[3].f = (float) $1;
1872 }
1873 ;
1874
1875 paramConstVector: '{' signedFloatConstant '}'
1876 {
1877 $$.count = 4;
1878 $$.data[0].f = $2;
1879 $$.data[1].f = 0.0f;
1880 $$.data[2].f = 0.0f;
1881 $$.data[3].f = 1.0f;
1882 }
1883 | '{' signedFloatConstant ',' signedFloatConstant '}'
1884 {
1885 $$.count = 4;
1886 $$.data[0].f = $2;
1887 $$.data[1].f = $4;
1888 $$.data[2].f = 0.0f;
1889 $$.data[3].f = 1.0f;
1890 }
1891 | '{' signedFloatConstant ',' signedFloatConstant ','
1892 signedFloatConstant '}'
1893 {
1894 $$.count = 4;
1895 $$.data[0].f = $2;
1896 $$.data[1].f = $4;
1897 $$.data[2].f = $6;
1898 $$.data[3].f = 1.0f;
1899 }
1900 | '{' signedFloatConstant ',' signedFloatConstant ','
1901 signedFloatConstant ',' signedFloatConstant '}'
1902 {
1903 $$.count = 4;
1904 $$.data[0].f = $2;
1905 $$.data[1].f = $4;
1906 $$.data[2].f = $6;
1907 $$.data[3].f = $8;
1908 }
1909 ;
1910
1911 signedFloatConstant: optionalSign REAL
1912 {
1913 $$ = ($1) ? -$2 : $2;
1914 }
1915 | optionalSign INTEGER
1916 {
1917 $$ = (float)(($1) ? -$2 : $2);
1918 }
1919 ;
1920
1921 optionalSign: '+' { $$ = FALSE; }
1922 | '-' { $$ = TRUE; }
1923 | { $$ = FALSE; }
1924 ;
1925
1926 TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1927 ;
1928
1929 optVarSize: string
1930 {
1931 /* NV_fragment_program_option defines the size qualifiers in a
1932 * fairly broken way. "SHORT" or "LONG" can optionally be used
1933 * before TEMP or OUTPUT. However, neither is a reserved word!
1934 * This means that we have to parse it as an identifier, then check
1935 * to make sure it's one of the valid values. *sigh*
1936 *
1937 * In addition, the grammar in the extension spec does *not* allow
1938 * the size specifier to be optional, but all known implementations
1939 * do.
1940 */
1941 if (!state->option.NV_fragment) {
1942 yyerror(& @1, state, "unexpected IDENTIFIER");
1943 YYERROR;
1944 }
1945
1946 if (strcmp("SHORT", $1) == 0) {
1947 } else if (strcmp("LONG", $1) == 0) {
1948 } else {
1949 char *const err_str =
1950 make_error_string("invalid storage size specifier \"%s\"",
1951 $1);
1952
1953 yyerror(& @1, state, (err_str != NULL)
1954 ? err_str : "invalid storage size specifier");
1955
1956 if (err_str != NULL) {
1957 free(err_str);
1958 }
1959
1960 YYERROR;
1961 }
1962 }
1963 |
1964 {
1965 }
1966 ;
1967
1968 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1969 ;
1970
1971 varNameList: varNameList ',' IDENTIFIER
1972 {
1973 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1974 free($3);
1975 YYERROR;
1976 }
1977 }
1978 | IDENTIFIER
1979 {
1980 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1981 free($1);
1982 YYERROR;
1983 }
1984 }
1985 ;
1986
1987 OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
1988 {
1989 struct asm_symbol *const s =
1990 declare_variable(state, $3, at_output, & @3);
1991
1992 if (s == NULL) {
1993 free($3);
1994 YYERROR;
1995 } else {
1996 s->output_binding = $5;
1997 }
1998 }
1999 ;
2000
2001 resultBinding: RESULT POSITION
2002 {
2003 if (state->mode == ARB_vertex) {
2004 $$ = VARYING_SLOT_POS;
2005 } else {
2006 yyerror(& @2, state, "invalid program result name");
2007 YYERROR;
2008 }
2009 }
2010 | RESULT FOGCOORD
2011 {
2012 if (state->mode == ARB_vertex) {
2013 $$ = VARYING_SLOT_FOGC;
2014 } else {
2015 yyerror(& @2, state, "invalid program result name");
2016 YYERROR;
2017 }
2018 }
2019 | RESULT resultColBinding
2020 {
2021 $$ = $2;
2022 }
2023 | RESULT POINTSIZE
2024 {
2025 if (state->mode == ARB_vertex) {
2026 $$ = VARYING_SLOT_PSIZ;
2027 } else {
2028 yyerror(& @2, state, "invalid program result name");
2029 YYERROR;
2030 }
2031 }
2032 | RESULT TEXCOORD optTexCoordUnitNum
2033 {
2034 if (state->mode == ARB_vertex) {
2035 $$ = VARYING_SLOT_TEX0 + $3;
2036 } else {
2037 yyerror(& @2, state, "invalid program result name");
2038 YYERROR;
2039 }
2040 }
2041 | RESULT DEPTH
2042 {
2043 if (state->mode == ARB_fragment) {
2044 $$ = FRAG_RESULT_DEPTH;
2045 } else {
2046 yyerror(& @2, state, "invalid program result name");
2047 YYERROR;
2048 }
2049 }
2050 ;
2051
2052 resultColBinding: COLOR optResultFaceType optResultColorType
2053 {
2054 $$ = $2 + $3;
2055 }
2056 ;
2057
2058 optResultFaceType:
2059 {
2060 if (state->mode == ARB_vertex) {
2061 $$ = VARYING_SLOT_COL0;
2062 } else {
2063 if (state->option.DrawBuffers)
2064 $$ = FRAG_RESULT_DATA0;
2065 else
2066 $$ = FRAG_RESULT_COLOR;
2067 }
2068 }
2069 | '[' INTEGER ']'
2070 {
2071 if (state->mode == ARB_vertex) {
2072 yyerror(& @1, state, "invalid program result name");
2073 YYERROR;
2074 } else {
2075 if (!state->option.DrawBuffers) {
2076 /* From the ARB_draw_buffers spec (same text exists
2077 * for ATI_draw_buffers):
2078 *
2079 * If this option is not specified, a fragment
2080 * program that attempts to bind
2081 * "result.color[n]" will fail to load, and only
2082 * "result.color" will be allowed.
2083 */
2084 yyerror(& @1, state,
2085 "result.color[] used without "
2086 "`OPTION ARB_draw_buffers' or "
2087 "`OPTION ATI_draw_buffers'");
2088 YYERROR;
2089 } else if ($2 >= state->MaxDrawBuffers) {
2090 yyerror(& @1, state,
2091 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
2092 YYERROR;
2093 }
2094 $$ = FRAG_RESULT_DATA0 + $2;
2095 }
2096 }
2097 | FRONT
2098 {
2099 if (state->mode == ARB_vertex) {
2100 $$ = VARYING_SLOT_COL0;
2101 } else {
2102 yyerror(& @1, state, "invalid program result name");
2103 YYERROR;
2104 }
2105 }
2106 | BACK
2107 {
2108 if (state->mode == ARB_vertex) {
2109 $$ = VARYING_SLOT_BFC0;
2110 } else {
2111 yyerror(& @1, state, "invalid program result name");
2112 YYERROR;
2113 }
2114 }
2115 ;
2116
2117 optResultColorType:
2118 {
2119 $$ = 0;
2120 }
2121 | PRIMARY
2122 {
2123 if (state->mode == ARB_vertex) {
2124 $$ = 0;
2125 } else {
2126 yyerror(& @1, state, "invalid program result name");
2127 YYERROR;
2128 }
2129 }
2130 | SECONDARY
2131 {
2132 if (state->mode == ARB_vertex) {
2133 $$ = 1;
2134 } else {
2135 yyerror(& @1, state, "invalid program result name");
2136 YYERROR;
2137 }
2138 }
2139 ;
2140
2141 optFaceType: { $$ = 0; }
2142 | FRONT { $$ = 0; }
2143 | BACK { $$ = 1; }
2144 ;
2145
2146 optColorType: { $$ = 0; }
2147 | PRIMARY { $$ = 0; }
2148 | SECONDARY { $$ = 1; }
2149 ;
2150
2151 optTexCoordUnitNum: { $$ = 0; }
2152 | '[' texCoordUnitNum ']' { $$ = $2; }
2153 ;
2154
2155 optTexImageUnitNum: { $$ = 0; }
2156 | '[' texImageUnitNum ']' { $$ = $2; }
2157 ;
2158
2159 optLegacyTexUnitNum: { $$ = 0; }
2160 | '[' legacyTexUnitNum ']' { $$ = $2; }
2161 ;
2162
2163 texCoordUnitNum: INTEGER
2164 {
2165 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2166 yyerror(& @1, state, "invalid texture coordinate unit selector");
2167 YYERROR;
2168 }
2169
2170 $$ = $1;
2171 }
2172 ;
2173
2174 texImageUnitNum: INTEGER
2175 {
2176 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2177 yyerror(& @1, state, "invalid texture image unit selector");
2178 YYERROR;
2179 }
2180
2181 $$ = $1;
2182 }
2183 ;
2184
2185 legacyTexUnitNum: INTEGER
2186 {
2187 if ((unsigned) $1 >= state->MaxTextureUnits) {
2188 yyerror(& @1, state, "invalid texture unit selector");
2189 YYERROR;
2190 }
2191
2192 $$ = $1;
2193 }
2194 ;
2195
2196 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2197 {
2198 struct asm_symbol *exist = (struct asm_symbol *)
2199 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2200 struct asm_symbol *target = (struct asm_symbol *)
2201 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2202
2203 free($4);
2204
2205 if (exist != NULL) {
2206 char m[1000];
2207 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2208 free($2);
2209 yyerror(& @2, state, m);
2210 YYERROR;
2211 } else if (target == NULL) {
2212 free($2);
2213 yyerror(& @4, state,
2214 "undefined variable binding in ALIAS statement");
2215 YYERROR;
2216 } else {
2217 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2218 }
2219 }
2220 ;
2221
2222 string: IDENTIFIER
2223 | USED_IDENTIFIER
2224 ;
2225
2226 %%
2227
2228 void
2229 asm_instruction_set_operands(struct asm_instruction *inst,
2230 const struct prog_dst_register *dst,
2231 const struct asm_src_register *src0,
2232 const struct asm_src_register *src1,
2233 const struct asm_src_register *src2)
2234 {
2235 /* In the core ARB extensions only the KIL instruction doesn't have a
2236 * destination register.
2237 */
2238 if (dst == NULL) {
2239 init_dst_reg(& inst->Base.DstReg);
2240 } else {
2241 inst->Base.DstReg = *dst;
2242 }
2243
2244 /* The only instruction that doesn't have any source registers is the
2245 * condition-code based KIL instruction added by NV_fragment_program_option.
2246 */
2247 if (src0 != NULL) {
2248 inst->Base.SrcReg[0] = src0->Base;
2249 inst->SrcReg[0] = *src0;
2250 } else {
2251 init_src_reg(& inst->SrcReg[0]);
2252 }
2253
2254 if (src1 != NULL) {
2255 inst->Base.SrcReg[1] = src1->Base;
2256 inst->SrcReg[1] = *src1;
2257 } else {
2258 init_src_reg(& inst->SrcReg[1]);
2259 }
2260
2261 if (src2 != NULL) {
2262 inst->Base.SrcReg[2] = src2->Base;
2263 inst->SrcReg[2] = *src2;
2264 } else {
2265 init_src_reg(& inst->SrcReg[2]);
2266 }
2267 }
2268
2269
2270 struct asm_instruction *
2271 asm_instruction_ctor(enum prog_opcode op,
2272 const struct prog_dst_register *dst,
2273 const struct asm_src_register *src0,
2274 const struct asm_src_register *src1,
2275 const struct asm_src_register *src2)
2276 {
2277 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2278
2279 if (inst) {
2280 _mesa_init_instructions(& inst->Base, 1);
2281 inst->Base.Opcode = op;
2282
2283 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2284 }
2285
2286 return inst;
2287 }
2288
2289
2290 struct asm_instruction *
2291 asm_instruction_copy_ctor(const struct prog_instruction *base,
2292 const struct prog_dst_register *dst,
2293 const struct asm_src_register *src0,
2294 const struct asm_src_register *src1,
2295 const struct asm_src_register *src2)
2296 {
2297 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2298
2299 if (inst) {
2300 _mesa_init_instructions(& inst->Base, 1);
2301 inst->Base.Opcode = base->Opcode;
2302 inst->Base.CondUpdate = base->CondUpdate;
2303 inst->Base.CondDst = base->CondDst;
2304 inst->Base.Saturate = base->Saturate;
2305 inst->Base.Precision = base->Precision;
2306
2307 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2308 }
2309
2310 return inst;
2311 }
2312
2313
2314 void
2315 init_dst_reg(struct prog_dst_register *r)
2316 {
2317 memset(r, 0, sizeof(*r));
2318 r->File = PROGRAM_UNDEFINED;
2319 r->WriteMask = WRITEMASK_XYZW;
2320 r->CondMask = COND_TR;
2321 r->CondSwizzle = SWIZZLE_NOOP;
2322 }
2323
2324
2325 /** Like init_dst_reg() but set the File and Index fields. */
2326 void
2327 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2328 {
2329 const GLint maxIndex = 1 << INST_INDEX_BITS;
2330 const GLint minIndex = 0;
2331 assert(index >= minIndex);
2332 (void) minIndex;
2333 assert(index <= maxIndex);
2334 (void) maxIndex;
2335 assert(file == PROGRAM_TEMPORARY ||
2336 file == PROGRAM_ADDRESS ||
2337 file == PROGRAM_OUTPUT);
2338 memset(r, 0, sizeof(*r));
2339 r->File = file;
2340 r->Index = index;
2341 r->WriteMask = WRITEMASK_XYZW;
2342 r->CondMask = COND_TR;
2343 r->CondSwizzle = SWIZZLE_NOOP;
2344 }
2345
2346
2347 void
2348 init_src_reg(struct asm_src_register *r)
2349 {
2350 memset(r, 0, sizeof(*r));
2351 r->Base.File = PROGRAM_UNDEFINED;
2352 r->Base.Swizzle = SWIZZLE_NOOP;
2353 r->Symbol = NULL;
2354 }
2355
2356
2357 /** Like init_src_reg() but set the File and Index fields.
2358 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2359 */
2360 void
2361 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2362 {
2363 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2364 }
2365
2366
2367 void
2368 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2369 GLuint swizzle)
2370 {
2371 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2372 const GLint minIndex = -(1 << INST_INDEX_BITS);
2373 assert(file < PROGRAM_FILE_MAX);
2374 assert(index >= minIndex);
2375 (void) minIndex;
2376 assert(index <= maxIndex);
2377 (void) maxIndex;
2378 memset(r, 0, sizeof(*r));
2379 r->Base.File = file;
2380 r->Base.Index = index;
2381 r->Base.Swizzle = swizzle;
2382 r->Symbol = NULL;
2383 }
2384
2385
2386 /**
2387 * Validate the set of inputs used by a program
2388 *
2389 * Validates that legal sets of inputs are used by the program. In this case
2390 * "used" included both reading the input or binding the input to a name using
2391 * the \c ATTRIB command.
2392 *
2393 * \return
2394 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2395 */
2396 int
2397 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2398 {
2399 const GLbitfield64 inputs = state->prog->InputsRead | state->InputsBound;
2400
2401 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2402 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2403 return 0;
2404 }
2405
2406 return 1;
2407 }
2408
2409
2410 struct asm_symbol *
2411 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2412 struct YYLTYPE *locp)
2413 {
2414 struct asm_symbol *s = NULL;
2415 struct asm_symbol *exist = (struct asm_symbol *)
2416 _mesa_symbol_table_find_symbol(state->st, 0, name);
2417
2418
2419 if (exist != NULL) {
2420 yyerror(locp, state, "redeclared identifier");
2421 } else {
2422 s = calloc(1, sizeof(struct asm_symbol));
2423 s->name = name;
2424 s->type = t;
2425
2426 switch (t) {
2427 case at_temp:
2428 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2429 yyerror(locp, state, "too many temporaries declared");
2430 free(s);
2431 return NULL;
2432 }
2433
2434 s->temp_binding = state->prog->NumTemporaries;
2435 state->prog->NumTemporaries++;
2436 break;
2437
2438 case at_address:
2439 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2440 yyerror(locp, state, "too many address registers declared");
2441 free(s);
2442 return NULL;
2443 }
2444
2445 /* FINISHME: Add support for multiple address registers.
2446 */
2447 state->prog->NumAddressRegs++;
2448 break;
2449
2450 default:
2451 break;
2452 }
2453
2454 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2455 s->next = state->sym;
2456 state->sym = s;
2457 }
2458
2459 return s;
2460 }
2461
2462
2463 int add_state_reference(struct gl_program_parameter_list *param_list,
2464 const gl_state_index tokens[STATE_LENGTH])
2465 {
2466 const GLuint size = 4; /* XXX fix */
2467 char *name;
2468 GLint index;
2469
2470 name = _mesa_program_state_string(tokens);
2471 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2472 size, GL_NONE, NULL, tokens);
2473 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2474
2475 /* free name string here since we duplicated it in add_parameter() */
2476 free(name);
2477
2478 return index;
2479 }
2480
2481
2482 int
2483 initialize_symbol_from_state(struct gl_program *prog,
2484 struct asm_symbol *param_var,
2485 const gl_state_index tokens[STATE_LENGTH])
2486 {
2487 int idx = -1;
2488 gl_state_index state_tokens[STATE_LENGTH];
2489
2490
2491 memcpy(state_tokens, tokens, sizeof(state_tokens));
2492
2493 param_var->type = at_param;
2494 param_var->param_binding_type = PROGRAM_STATE_VAR;
2495
2496 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2497 * unroll it and call add_state_reference() for each row
2498 */
2499 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2500 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2501 state_tokens[0] == STATE_MVP_MATRIX ||
2502 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2503 state_tokens[0] == STATE_PROGRAM_MATRIX)
2504 && (state_tokens[2] != state_tokens[3])) {
2505 int row;
2506 const int first_row = state_tokens[2];
2507 const int last_row = state_tokens[3];
2508
2509 for (row = first_row; row <= last_row; row++) {
2510 state_tokens[2] = state_tokens[3] = row;
2511
2512 idx = add_state_reference(prog->Parameters, state_tokens);
2513 if (param_var->param_binding_begin == ~0U) {
2514 param_var->param_binding_begin = idx;
2515 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2516 }
2517
2518 param_var->param_binding_length++;
2519 }
2520 }
2521 else {
2522 idx = add_state_reference(prog->Parameters, state_tokens);
2523 if (param_var->param_binding_begin == ~0U) {
2524 param_var->param_binding_begin = idx;
2525 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2526 }
2527 param_var->param_binding_length++;
2528 }
2529
2530 return idx;
2531 }
2532
2533
2534 int
2535 initialize_symbol_from_param(struct gl_program *prog,
2536 struct asm_symbol *param_var,
2537 const gl_state_index tokens[STATE_LENGTH])
2538 {
2539 int idx = -1;
2540 gl_state_index state_tokens[STATE_LENGTH];
2541
2542
2543 memcpy(state_tokens, tokens, sizeof(state_tokens));
2544
2545 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2546 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2547 assert((state_tokens[1] == STATE_ENV)
2548 || (state_tokens[1] == STATE_LOCAL));
2549
2550 /*
2551 * The param type is STATE_VAR. The program parameter entry will
2552 * effectively be a pointer into the LOCAL or ENV parameter array.
2553 */
2554 param_var->type = at_param;
2555 param_var->param_binding_type = PROGRAM_STATE_VAR;
2556
2557 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2558 * we need to unroll it and call add_state_reference() for each row
2559 */
2560 if (state_tokens[2] != state_tokens[3]) {
2561 int row;
2562 const int first_row = state_tokens[2];
2563 const int last_row = state_tokens[3];
2564
2565 for (row = first_row; row <= last_row; row++) {
2566 state_tokens[2] = state_tokens[3] = row;
2567
2568 idx = add_state_reference(prog->Parameters, state_tokens);
2569 if (param_var->param_binding_begin == ~0U) {
2570 param_var->param_binding_begin = idx;
2571 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2572 }
2573 param_var->param_binding_length++;
2574 }
2575 }
2576 else {
2577 idx = add_state_reference(prog->Parameters, state_tokens);
2578 if (param_var->param_binding_begin == ~0U) {
2579 param_var->param_binding_begin = idx;
2580 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2581 }
2582 param_var->param_binding_length++;
2583 }
2584
2585 return idx;
2586 }
2587
2588
2589 /**
2590 * Put a float/vector constant/literal into the parameter list.
2591 * \param param_var returns info about the parameter/constant's location,
2592 * binding, type, etc.
2593 * \param vec the vector/constant to add
2594 * \param allowSwizzle if true, try to consolidate constants which only differ
2595 * by a swizzle. We don't want to do this when building
2596 * arrays of constants that may be indexed indirectly.
2597 * \return index of the constant in the parameter list.
2598 */
2599 int
2600 initialize_symbol_from_const(struct gl_program *prog,
2601 struct asm_symbol *param_var,
2602 const struct asm_vector *vec,
2603 GLboolean allowSwizzle)
2604 {
2605 unsigned swizzle;
2606 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2607 vec->data, vec->count,
2608 allowSwizzle ? &swizzle : NULL);
2609
2610 param_var->type = at_param;
2611 param_var->param_binding_type = PROGRAM_CONSTANT;
2612
2613 if (param_var->param_binding_begin == ~0U) {
2614 param_var->param_binding_begin = idx;
2615 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2616 }
2617 param_var->param_binding_length++;
2618
2619 return idx;
2620 }
2621
2622
2623 char *
2624 make_error_string(const char *fmt, ...)
2625 {
2626 int length;
2627 char *str;
2628 va_list args;
2629
2630
2631 /* Call vsnprintf once to determine how large the final string is. Call it
2632 * again to do the actual formatting. from the vsnprintf manual page:
2633 *
2634 * Upon successful return, these functions return the number of
2635 * characters printed (not including the trailing '\0' used to end
2636 * output to strings).
2637 */
2638 va_start(args, fmt);
2639 length = 1 + vsnprintf(NULL, 0, fmt, args);
2640 va_end(args);
2641
2642 str = malloc(length);
2643 if (str) {
2644 va_start(args, fmt);
2645 vsnprintf(str, length, fmt, args);
2646 va_end(args);
2647 }
2648
2649 return str;
2650 }
2651
2652
2653 void
2654 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2655 {
2656 char *err_str;
2657
2658
2659 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2660 if (err_str) {
2661 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2662 free(err_str);
2663 }
2664
2665 err_str = make_error_string("line %u, char %u: error: %s\n",
2666 locp->first_line, locp->first_column, s);
2667 _mesa_set_program_error(state->ctx, locp->position, err_str);
2668
2669 if (err_str) {
2670 free(err_str);
2671 }
2672 }
2673
2674
2675 GLboolean
2676 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2677 GLsizei len, struct asm_parser_state *state)
2678 {
2679 struct asm_instruction *inst;
2680 unsigned i;
2681 GLubyte *strz;
2682 GLboolean result = GL_FALSE;
2683 void *temp;
2684 struct asm_symbol *sym;
2685
2686 state->ctx = ctx;
2687 state->prog->Target = target;
2688 state->prog->Parameters = _mesa_new_parameter_list();
2689
2690 /* Make a copy of the program string and force it to be NUL-terminated.
2691 */
2692 strz = (GLubyte *) malloc(len + 1);
2693 if (strz == NULL) {
2694 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2695 return GL_FALSE;
2696 }
2697 memcpy (strz, str, len);
2698 strz[len] = '\0';
2699
2700 state->prog->String = strz;
2701
2702 state->st = _mesa_symbol_table_ctor();
2703
2704 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2705 ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2706 : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2707
2708 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2709 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2710 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2711 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2712 state->MaxLights = ctx->Const.MaxLights;
2713 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2714 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2715
2716 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2717 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2718
2719 _mesa_set_program_error(ctx, -1, NULL);
2720
2721 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2722 yyparse(state);
2723 _mesa_program_lexer_dtor(state->scanner);
2724
2725
2726 if (ctx->Program.ErrorPos != -1) {
2727 goto error;
2728 }
2729
2730 if (! _mesa_layout_parameters(state)) {
2731 struct YYLTYPE loc;
2732
2733 loc.first_line = 0;
2734 loc.first_column = 0;
2735 loc.position = len;
2736
2737 yyerror(& loc, state, "invalid PARAM usage");
2738 goto error;
2739 }
2740
2741
2742
2743 /* Add one instruction to store the "END" instruction.
2744 */
2745 state->prog->Instructions =
2746 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2747
2748 if (state->prog->Instructions == NULL) {
2749 goto error;
2750 }
2751
2752 inst = state->inst_head;
2753 for (i = 0; i < state->prog->NumInstructions; i++) {
2754 struct asm_instruction *const temp = inst->next;
2755
2756 state->prog->Instructions[i] = inst->Base;
2757 inst = temp;
2758 }
2759
2760 /* Finally, tag on an OPCODE_END instruction */
2761 {
2762 const GLuint numInst = state->prog->NumInstructions;
2763 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2764 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2765 }
2766 state->prog->NumInstructions++;
2767
2768 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2769 state->prog->NumAttributes = _mesa_bitcount_64(state->prog->InputsRead);
2770
2771 /*
2772 * Initialize native counts to logical counts. The device driver may
2773 * change them if program is translated into a hardware program.
2774 */
2775 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2776 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2777 state->prog->NumNativeParameters = state->prog->NumParameters;
2778 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2779 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2780
2781 result = GL_TRUE;
2782
2783 error:
2784 for (inst = state->inst_head; inst != NULL; inst = temp) {
2785 temp = inst->next;
2786 free(inst);
2787 }
2788
2789 state->inst_head = NULL;
2790 state->inst_tail = NULL;
2791
2792 for (sym = state->sym; sym != NULL; sym = temp) {
2793 temp = sym->next;
2794
2795 free((void *) sym->name);
2796 free(sym);
2797 }
2798 state->sym = NULL;
2799
2800 _mesa_symbol_table_dtor(state->st);
2801 state->st = NULL;
2802
2803 return result;
2804 }