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