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