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