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