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