blob: c7e36096e0bd11e0f534337205aa33cd0ccb9b12 [file] [log] [blame]
Ian Romanick770cebb2009-07-20 17:44:36 -07001%{
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"
Ian Romanickef80c202009-07-22 17:13:08 -070029#include "main/imports.h"
30#include "program.h"
Ian Romanick770cebb2009-07-20 17:44:36 -070031#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
39extern void *yy_scan_string(char *);
40extern void yy_delete_buffer(void *);
41
42static struct asm_symbol *declare_variable(struct asm_parser_state *state,
43 char *name, enum asm_type t, struct YYLTYPE *locp);
44
Ian Romanickef80c202009-07-22 17:13:08 -070045static int add_state_reference(struct gl_program_parameter_list *param_list,
46 const gl_state_index tokens[STATE_LENGTH]);
47
Ian Romanick770cebb2009-07-20 17:44:36 -070048static int initialize_symbol_from_state(struct gl_program *prog,
49 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
50
51static int initialize_symbol_from_param(struct gl_program *prog,
52 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
53
54static int initialize_symbol_from_const(struct gl_program *prog,
55 struct asm_symbol *param_var, const struct asm_vector *vec);
56
57static int yyparse(struct asm_parser_state *state);
58
Ian Romanickef80c202009-07-22 17:13:08 -070059static char *make_error_string(const char *fmt, ...);
60
Ian Romanick770cebb2009-07-20 17:44:36 -070061static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
62 const char *s);
63
64static int validate_inputs(struct YYLTYPE *locp,
65 struct asm_parser_state *state);
66
67static void init_dst_reg(struct prog_dst_register *r);
68
69static void init_src_reg(struct asm_src_register *r);
70
71static 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
Ian Romanick1edd13b2009-07-27 16:24:49 -0700160%token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
Ian Romanickaafd5762009-07-27 17:22:21 -0700161%token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
Ian Romanick770cebb2009-07-20 17:44:36 -0700162%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
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%{
243extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
244 void *yyscanner);
245%}
246
247%%
248
249program: language optionSequence statementSequence END
250 ;
251
252language: 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;
Ian Romanick88018e22009-07-27 15:47:52 -0700266
267 state->option.TexRect =
268 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
Ian Romanick770cebb2009-07-20 17:44:36 -0700269 }
270 ;
271
272optionSequence: optionSequence option
273 |
274 ;
275
276option: 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) {
Ian Romanick41d56962009-07-27 16:33:16 -0700288 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);
Ian Romanick770cebb2009-07-20 17:44:36 -0700293 YYERROR;
294 }
295 }
296 ;
297
298statementSequence: statementSequence statement
299 |
300 ;
301
302statement: 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
320instruction: ALU_instruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700321 {
322 $$ = $1;
323 state->prog->NumAluInstructions++;
324 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700325 | TexInstruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700326 {
327 $$ = $1;
328 state->prog->NumTexInstructions++;
329 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700330 ;
331
332ALU_instruction: ARL_instruction
333 | VECTORop_instruction
334 | SCALARop_instruction
335 | BINSCop_instruction
336 | BINop_instruction
337 | TRIop_instruction
338 | SWZ_instruction
339 ;
340
341TexInstruction: SAMPLE_instruction
342 | KIL_instruction
343 ;
344
345ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
346 {
347 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
348 }
349 ;
350
351VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
352 {
353 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
354 $$->Base.SaturateMode = $1.SaturateMode;
355 }
356 ;
357
358SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
359 {
360 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
361 $$->Base.SaturateMode = $1.SaturateMode;
362 }
363 ;
364
365BINSCop_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
373BINop_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
380TRIop_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
388SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
389 {
390 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
391 if ($$ != NULL) {
Ian Romanick1edd13b2009-07-27 16:24:49 -0700392 const GLbitfield tex_mask = (1U << $6);
393 GLbitfield shadow_tex = 0;
394 GLbitfield target_mask = 0;
395
396
Ian Romanick770cebb2009-07-20 17:44:36 -0700397 $$->Base.SaturateMode = $1.SaturateMode;
398 $$->Base.TexSrcUnit = $6;
Ian Romanick0db5ef02009-07-22 16:21:54 -0700399
Ian Romanick1edd13b2009-07-27 16:24:49 -0700400 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;
Ian Romanick770cebb2009-07-20 17:44:36 -0700429 }
430 }
431 ;
432
433KIL_instruction: KIL swizzleSrcReg
434 {
435 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
Ian Romanick0db5ef02009-07-22 16:21:54 -0700436 state->fragment.UsesKill = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700437 }
438 ;
439
440texImageUnit: TEXTURE_UNIT optTexImageUnitNum
441 {
442 $$ = $2;
443 }
444 ;
445
446texTarget: 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; }
Ian Romanick1edd13b2009-07-27 16:24:49 -0700451 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
452 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
453 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
Ian Romanickaafd5762009-07-27 17:22:21 -0700454 | 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; }
Ian Romanick770cebb2009-07-20 17:44:36 -0700458 ;
459
460SWZ_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;
Ian Romanick648dac42009-07-28 21:57:28 -0700466 $4.Base.Negate = $6.mask;
Ian Romanick770cebb2009-07-20 17:44:36 -0700467
468 $$ = asm_instruction_ctor(OPCODE_SWZ, & $2, & $4, NULL, NULL);
469 $$->Base.SaturateMode = $1.SaturateMode;
470 }
471 ;
472
473scalarSrcReg: 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
486swizzleSrcReg: 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
499maskedDstReg: 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
521maskedAddrReg: addrReg addrWriteMask
522 {
523 init_dst_reg(& $$);
524 $$.File = PROGRAM_ADDRESS;
525 $$.Index = 0;
526 $$.WriteMask = $2.mask;
527 }
528 ;
529
530extendedSwizzle: 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
539extSwizComp: optionalSign extSwizSel
540 {
541 $$.swizzle = $2;
542 $$.mask = ($1) ? 1 : 0;
543 }
544 ;
545
546extSwizSel: 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
583srcReg: 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
Ian Romanickef80c202009-07-22 17:13:08 -0700639 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700640 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(& $$);
Ian Romanick69d3d192009-07-22 10:51:18 -0700660 $$.Base.File = ($1.name != NULL)
661 ? $1.param_binding_type
662 : PROGRAM_CONSTANT;
Ian Romanick770cebb2009-07-20 17:44:36 -0700663 $$.Base.Index = $1.param_binding_begin;
664 }
665 ;
666
667dstReg: 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(& $$);
Ian Romanick86b33b52009-07-28 21:56:42 -0700687 switch (s->type) {
688 case at_temp:
Ian Romanick770cebb2009-07-20 17:44:36 -0700689 $$.File = PROGRAM_TEMPORARY;
690 $$.Index = s->temp_binding;
Ian Romanick86b33b52009-07-28 21:56:42 -0700691 break;
692 case at_output:
693 $$.File = PROGRAM_OUTPUT;
694 $$.Index = s->output_binding;
695 break;
696 default:
Ian Romanick770cebb2009-07-20 17:44:36 -0700697 $$.File = s->param_binding_type;
698 $$.Index = s->param_binding_begin;
Ian Romanick86b33b52009-07-28 21:56:42 -0700699 break;
Ian Romanick770cebb2009-07-20 17:44:36 -0700700 }
701 }
702 ;
703
704progParamArray: 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
721progParamArrayMem: progParamArrayAbs | progParamArrayRel;
722
723progParamArrayAbs: INTEGER
724 {
725 init_src_reg(& $$);
726 $$.Base.Index = $1;
727 }
728 ;
729
730progParamArrayRel: 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
742addrRegRelOffset: { $$ = 0; }
743 | '+' addrRegPosOffset { $$ = $2; }
744 | '-' addrRegNegOffset { $$ = -$2; }
745 ;
746
747addrRegPosOffset: 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
759addrRegNegOffset: 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
771addrReg: 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
789addrComponent: 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
800addrWriteMask: 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
812scalarSuffix: MASK1;
813
814swizzleSuffix: MASK1
815 | MASK4
816 | SWIZZLE
817 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
818 ;
819
820optionalMask: MASK4 | MASK3 | MASK2 | MASK1
821 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
822 ;
823
824namingStatement: ATTRIB_statement
825 | PARAM_statement
826 | TEMP_statement
827 | ADDRESS_statement
828 | OUTPUT_statement
829 | ALIAS_statement
830 ;
831
832ATTRIB_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
850attribBinding: VERTEX vtxAttribItem
851 {
852 $$ = $2;
853 }
854 | FRAGMENT fragAttribItem
855 {
856 $$ = $2;
857 }
858 ;
859
860vtxAttribItem: 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 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700874 if (!state->ctx->Extensions.EXT_secondary_color) {
875 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
876 YYERROR;
877 }
878
Ian Romanick770cebb2009-07-20 17:44:36 -0700879 $$ = VERT_ATTRIB_COLOR0 + $2;
880 }
881 | FOGCOORD
882 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700883 if (!state->ctx->Extensions.EXT_fog_coord) {
884 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
885 YYERROR;
886 }
887
Ian Romanick770cebb2009-07-20 17:44:36 -0700888 $$ = VERT_ATTRIB_FOG;
889 }
890 | TEXCOORD optTexCoordUnitNum
891 {
892 $$ = VERT_ATTRIB_TEX0 + $2;
893 }
894 | MATRIXINDEX '[' vtxWeightNum ']'
895 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700896 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -0700897 YYERROR;
898 }
899 | VTXATTRIB '[' vtxAttribNum ']'
900 {
901 $$ = VERT_ATTRIB_GENERIC0 + $3;
902 }
903 ;
904
905vtxAttribNum: INTEGER
906 {
Ian Romanickef80c202009-07-22 17:13:08 -0700907 if ((unsigned) $1 >= state->limits->MaxAttribs) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700908 yyerror(& @1, state, "invalid vertex attribute reference");
909 YYERROR;
910 }
911
912 $$ = $1;
913 }
914 ;
915
916vtxOptWeightNum: | '[' vtxWeightNum ']';
917vtxWeightNum: INTEGER;
918
919fragAttribItem: 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
937PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
938
939PARAM_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
955PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
956 {
Ian Romanickef80c202009-07-22 17:13:08 -0700957 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700958 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
977optArraySize:
978 {
979 $$ = 0;
980 }
981 | INTEGER
982 {
Ian Romanickef80c202009-07-22 17:13:08 -0700983 if (($1 < 1) || ((unsigned) $1 >= state->limits->MaxParameters)) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700984 yyerror(& @1, state, "invalid parameter array size");
985 YYERROR;
986 } else {
987 $$ = $1;
988 }
989 }
990 ;
991
992paramSingleInit: '=' paramSingleItemDecl
993 {
994 $$ = $2;
995 }
996 ;
997
998paramMultipleInit: '=' '{' paramMultInitList '}'
999 {
1000 $$ = $3;
1001 }
1002 ;
1003
1004paramMultInitList: paramMultipleItem
1005 | paramMultInitList ',' paramMultipleItem
1006 {
1007 $1.param_binding_length += $3.param_binding_length;
1008 $$ = $1;
1009 }
1010 ;
1011
1012paramSingleItemDecl: 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
1032paramSingleItemUse: 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
1052paramMultipleItem: 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
1072stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1073 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1074 ;
1075
1076stateSingleItem: 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 ;
1087
1088stateMaterialItem: MATERIAL optFaceType stateMatProperty
1089 {
1090 memset($$, 0, sizeof($$));
1091 $$[0] = STATE_MATERIAL;
1092 $$[1] = $2;
1093 $$[2] = $3;
1094 }
1095 ;
1096
1097stateMatProperty: ambDiffSpecProperty
1098 {
1099 $$ = $1;
1100 }
1101 | EMISSION
1102 {
1103 $$ = STATE_EMISSION;
1104 }
1105 | SHININESS
1106 {
1107 $$ = STATE_SHININESS;
1108 }
1109 ;
1110
1111stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1112 {
1113 memset($$, 0, sizeof($$));
1114 $$[0] = STATE_LIGHT;
1115 $$[1] = $3;
1116 $$[2] = $5;
1117 }
1118 ;
1119
1120stateLightProperty: ambDiffSpecProperty
1121 {
1122 $$ = $1;
1123 }
1124 | POSITION
1125 {
1126 $$ = STATE_POSITION;
1127 }
1128 | ATTENUATION
1129 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001130 if (!state->ctx->Extensions.EXT_point_parameters) {
1131 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1132 YYERROR;
1133 }
1134
Ian Romanick770cebb2009-07-20 17:44:36 -07001135 $$ = STATE_ATTENUATION;
1136 }
1137 | SPOT stateSpotProperty
1138 {
1139 $$ = $2;
1140 }
1141 | HALF
1142 {
1143 $$ = STATE_HALF_VECTOR;
1144 }
1145 ;
1146
1147stateSpotProperty: DIRECTION
1148 {
1149 $$ = STATE_SPOT_DIRECTION;
1150 }
1151 ;
1152
1153stateLightModelItem: LIGHTMODEL stateLModProperty
1154 {
1155 $$[0] = $2[0];
1156 $$[1] = $2[1];
1157 }
1158 ;
1159
1160stateLModProperty: AMBIENT
1161 {
1162 memset($$, 0, sizeof($$));
1163 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1164 }
1165 | optFaceType SCENECOLOR
1166 {
1167 memset($$, 0, sizeof($$));
1168 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1169 $$[1] = $1;
1170 }
1171 ;
1172
1173stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1174 {
1175 memset($$, 0, sizeof($$));
1176 $$[0] = STATE_LIGHTPROD;
1177 $$[1] = $3;
1178 $$[2] = $5;
1179 $$[3] = $6;
1180 }
1181 ;
1182
1183stateLProdProperty: ambDiffSpecProperty;
1184
1185stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1186 {
1187 memset($$, 0, sizeof($$));
1188 $$[0] = $3;
1189 $$[1] = $2;
1190 }
1191 ;
1192
1193stateTexEnvProperty: COLOR
1194 {
1195 $$ = STATE_TEXENV_COLOR;
1196 }
1197 ;
1198
1199ambDiffSpecProperty: AMBIENT
1200 {
1201 $$ = STATE_AMBIENT;
1202 }
1203 | DIFFUSE
1204 {
1205 $$ = STATE_DIFFUSE;
1206 }
1207 | SPECULAR
1208 {
1209 $$ = STATE_SPECULAR;
1210 }
1211 ;
1212
1213stateLightNumber: INTEGER
1214 {
Ian Romanickef80c202009-07-22 17:13:08 -07001215 if ((unsigned) $1 >= state->MaxLights) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001216 yyerror(& @1, state, "invalid light selector");
1217 YYERROR;
1218 }
1219
1220 $$ = $1;
1221 }
1222 ;
1223
1224stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1225 {
1226 memset($$, 0, sizeof($$));
1227 $$[0] = STATE_TEXGEN;
1228 $$[1] = $2;
1229 $$[2] = $3 + $4;
1230 }
1231 ;
1232
1233stateTexGenType: EYE
1234 {
1235 $$ = STATE_TEXGEN_EYE_S;
1236 }
1237 | OBJECT
1238 {
1239 $$ = STATE_TEXGEN_OBJECT_S;
1240 }
1241 ;
1242stateTexGenCoord: TEXGEN_S
1243 {
1244 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1245 }
1246 | TEXGEN_T
1247 {
1248 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1249 }
1250 | TEXGEN_R
1251 {
1252 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1253 }
1254 | TEXGEN_Q
1255 {
1256 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1257 }
1258 ;
1259
1260stateFogItem: FOG stateFogProperty
1261 {
1262 memset($$, 0, sizeof($$));
1263 $$[0] = $2;
1264 }
1265 ;
1266
1267stateFogProperty: COLOR
1268 {
1269 $$ = STATE_FOG_COLOR;
1270 }
1271 | PARAMS
1272 {
1273 $$ = STATE_FOG_PARAMS;
1274 }
1275 ;
1276
1277stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1278 {
1279 memset($$, 0, sizeof($$));
1280 $$[0] = STATE_CLIPPLANE;
1281 $$[1] = $3;
1282 }
1283 ;
1284
1285stateClipPlaneNum: INTEGER
1286 {
Ian Romanickef80c202009-07-22 17:13:08 -07001287 if ((unsigned) $1 >= state->MaxClipPlanes) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001288 yyerror(& @1, state, "invalid clip plane selector");
1289 YYERROR;
1290 }
1291
1292 $$ = $1;
1293 }
1294 ;
1295
1296statePointItem: POINT statePointProperty
1297 {
1298 memset($$, 0, sizeof($$));
1299 $$[0] = $2;
1300 }
1301 ;
1302
1303statePointProperty: SIZE
1304 {
1305 $$ = STATE_POINT_SIZE;
1306 }
1307 | ATTENUATION
1308 {
1309 $$ = STATE_POINT_ATTENUATION;
1310 }
1311 ;
1312
1313stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1314 {
1315 $$[0] = $1[0];
1316 $$[1] = $1[1];
1317 $$[2] = $4;
1318 $$[3] = $4;
1319 $$[4] = $1[2];
1320 }
1321 ;
1322
1323stateMatrixRows: stateMatrixItem optMatrixRows
1324 {
1325 $$[0] = $1[0];
1326 $$[1] = $1[1];
1327 $$[2] = $2[2];
1328 $$[3] = $2[3];
1329 $$[4] = $1[2];
1330 }
1331 ;
1332
1333optMatrixRows:
1334 {
1335 $$[2] = 0;
1336 $$[3] = 3;
1337 }
1338 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1339 {
1340 /* It seems logical that the matrix row range specifier would have
1341 * to specify a range or more than one row (i.e., $5 > $3).
1342 * However, the ARB_vertex_program spec says "a program will fail
1343 * to load if <a> is greater than <b>." This means that $3 == $5
1344 * is valid.
1345 */
1346 if ($3 > $5) {
1347 yyerror(& @3, state, "invalid matrix row range");
1348 YYERROR;
1349 }
1350
1351 $$[2] = $3;
1352 $$[3] = $5;
1353 }
1354 ;
1355
1356stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1357 {
1358 $$[0] = $2[0];
1359 $$[1] = $2[1];
1360 $$[2] = $3;
1361 }
1362 ;
1363
1364stateOptMatModifier:
1365 {
1366 $$ = 0;
1367 }
1368 | stateMatModifier
1369 {
1370 $$ = $1;
1371 }
1372 ;
1373
1374stateMatModifier: INVERSE
1375 {
1376 $$ = STATE_MATRIX_INVERSE;
1377 }
1378 | TRANSPOSE
1379 {
1380 $$ = STATE_MATRIX_TRANSPOSE;
1381 }
1382 | INVTRANS
1383 {
1384 $$ = STATE_MATRIX_INVTRANS;
1385 }
1386 ;
1387
1388stateMatrixRowNum: INTEGER
1389 {
1390 if ($1 > 3) {
1391 yyerror(& @1, state, "invalid matrix row reference");
1392 YYERROR;
1393 }
1394
1395 $$ = $1;
1396 }
1397 ;
1398
1399stateMatrixName: MODELVIEW stateOptModMatNum
1400 {
1401 $$[0] = STATE_MODELVIEW_MATRIX;
1402 $$[1] = $2;
1403 }
1404 | PROJECTION
1405 {
1406 $$[0] = STATE_PROJECTION_MATRIX;
1407 $$[1] = 0;
1408 }
1409 | MVP
1410 {
1411 $$[0] = STATE_MVP_MATRIX;
1412 $$[1] = 0;
1413 }
1414 | TEXTURE optTexCoordUnitNum
1415 {
1416 $$[0] = STATE_TEXTURE_MATRIX;
1417 $$[1] = $2;
1418 }
1419 | PALETTE '[' statePaletteMatNum ']'
1420 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001421 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -07001422 YYERROR;
1423 }
1424 | MAT_PROGRAM '[' stateProgramMatNum ']'
1425 {
1426 $$[0] = STATE_PROGRAM_MATRIX;
1427 $$[1] = $3;
1428 }
1429 ;
1430
1431stateOptModMatNum:
1432 {
1433 $$ = 0;
1434 }
1435 | stateModMatNum
1436 {
1437 $$ = $1;
1438 }
1439 ;
1440stateModMatNum: INTEGER
1441 {
1442 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1443 * zero is valid.
1444 */
1445 if ($1 != 0) {
1446 yyerror(& @1, state, "invalid modelview matrix index");
1447 YYERROR;
1448 }
1449
1450 $$ = $1;
1451 }
1452 ;
1453statePaletteMatNum: INTEGER
1454 {
1455 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1456 * through here. The error will be generated later.
1457 */
1458 $$ = $1;
1459 }
1460 ;
1461stateProgramMatNum: INTEGER
1462 {
Ian Romanickef80c202009-07-22 17:13:08 -07001463 if ((unsigned) $1 >= state->MaxProgramMatrices) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001464 yyerror(& @1, state, "invalid program matrix selector");
1465 YYERROR;
1466 }
1467
1468 $$ = $1;
1469 }
1470 ;
1471
1472
1473
1474programSingleItem: progEnvParam | progLocalParam;
1475
1476programMultipleItem: progEnvParams | progLocalParams;
1477
1478progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1479 {
1480 memset($$, 0, sizeof($$));
1481 $$[0] = state->state_param_enum;
1482 $$[1] = STATE_ENV;
1483 $$[2] = $4[0];
1484 $$[3] = $4[1];
1485 }
1486 ;
1487
1488progEnvParamNums: progEnvParamNum
1489 {
1490 $$[0] = $1;
1491 $$[1] = $1;
1492 }
1493 | progEnvParamNum DOT_DOT progEnvParamNum
1494 {
1495 $$[0] = $1;
1496 $$[1] = $3;
1497 }
1498 ;
1499
1500progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1501 {
1502 memset($$, 0, sizeof($$));
1503 $$[0] = state->state_param_enum;
1504 $$[1] = STATE_ENV;
1505 $$[2] = $4;
1506 $$[3] = $4;
1507 }
1508 ;
1509
1510progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1511 {
1512 memset($$, 0, sizeof($$));
1513 $$[0] = state->state_param_enum;
1514 $$[1] = STATE_LOCAL;
1515 $$[2] = $4[0];
1516 $$[3] = $4[1];
1517 }
1518
1519progLocalParamNums: progLocalParamNum
1520 {
1521 $$[0] = $1;
1522 $$[1] = $1;
1523 }
1524 | progLocalParamNum DOT_DOT progLocalParamNum
1525 {
1526 $$[0] = $1;
1527 $$[1] = $3;
1528 }
1529 ;
1530
1531progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1532 {
1533 memset($$, 0, sizeof($$));
1534 $$[0] = state->state_param_enum;
1535 $$[1] = STATE_LOCAL;
1536 $$[2] = $4;
1537 $$[3] = $4;
1538 }
1539 ;
1540
1541progEnvParamNum: INTEGER
1542 {
Ian Romanickef80c202009-07-22 17:13:08 -07001543 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001544 yyerror(& @1, state, "invalid environment parameter reference");
1545 YYERROR;
1546 }
1547 $$ = $1;
1548 }
1549 ;
1550
1551progLocalParamNum: INTEGER
1552 {
Ian Romanickef80c202009-07-22 17:13:08 -07001553 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001554 yyerror(& @1, state, "invalid local parameter reference");
1555 YYERROR;
1556 }
1557 $$ = $1;
1558 }
1559 ;
1560
1561
1562
1563paramConstDecl: paramConstScalarDecl | paramConstVector;
1564paramConstUse: paramConstScalarUse | paramConstVector;
1565
1566paramConstScalarDecl: signedFloatConstant
1567 {
1568 $$.count = 1;
1569 $$.data[0] = $1;
1570 }
1571 ;
1572
1573paramConstScalarUse: REAL
1574 {
1575 $$.count = 1;
1576 $$.data[0] = $1;
1577 }
1578 | INTEGER
1579 {
1580 $$.count = 1;
1581 $$.data[0] = (float) $1;
1582 }
1583 ;
1584
1585paramConstVector: '{' signedFloatConstant '}'
1586 {
1587 $$.count = 1;
1588 $$.data[0] = $2;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001589 $$.data[1] = 0.0f;
1590 $$.data[2] = 0.0f;
1591 $$.data[3] = 0.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001592 }
1593 | '{' signedFloatConstant ',' signedFloatConstant '}'
1594 {
1595 $$.count = 2;
1596 $$.data[0] = $2;
1597 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001598 $$.data[2] = 0.0f;
1599 $$.data[3] = 0.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001600 }
1601 | '{' signedFloatConstant ',' signedFloatConstant ','
1602 signedFloatConstant '}'
1603 {
1604 $$.count = 3;
1605 $$.data[0] = $2;
1606 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001607 $$.data[2] = $6;
1608 $$.data[3] = 0.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001609 }
1610 | '{' signedFloatConstant ',' signedFloatConstant ','
1611 signedFloatConstant ',' signedFloatConstant '}'
1612 {
1613 $$.count = 4;
1614 $$.data[0] = $2;
1615 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001616 $$.data[2] = $6;
1617 $$.data[3] = $8;
Ian Romanick770cebb2009-07-20 17:44:36 -07001618 }
1619 ;
1620
1621signedFloatConstant: optionalSign REAL
1622 {
1623 $$ = ($1) ? -$2 : $2;
1624 }
1625 | optionalSign INTEGER
1626 {
1627 $$ = (float)(($1) ? -$2 : $2);
1628 }
1629 ;
1630
1631optionalSign: '+' { $$ = FALSE; }
1632 | '-' { $$ = TRUE; }
1633 | { $$ = FALSE; }
1634 ;
1635
1636TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1637 ;
1638
1639ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1640 ;
1641
1642varNameList: varNameList ',' IDENTIFIER
1643 {
1644 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1645 YYERROR;
1646 }
1647 }
1648 | IDENTIFIER
1649 {
1650 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1651 YYERROR;
1652 }
1653 }
1654 ;
1655
1656OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1657 {
1658 struct asm_symbol *const s =
1659 declare_variable(state, $2, at_output, & @2);
1660
1661 if (s == NULL) {
1662 YYERROR;
1663 } else {
1664 s->output_binding = $4;
1665 }
1666 }
1667 ;
1668
1669resultBinding: RESULT POSITION
1670 {
1671 if (state->mode == ARB_vertex) {
1672 $$ = VERT_RESULT_HPOS;
1673 } else {
1674 yyerror(& @2, state, "invalid program result name");
1675 YYERROR;
1676 }
1677 }
1678 | RESULT FOGCOORD
1679 {
1680 if (state->mode == ARB_vertex) {
1681 $$ = VERT_RESULT_FOGC;
1682 } else {
1683 yyerror(& @2, state, "invalid program result name");
1684 YYERROR;
1685 }
1686 }
1687 | RESULT resultColBinding
1688 {
1689 $$ = $2;
1690 }
1691 | RESULT POINTSIZE
1692 {
1693 if (state->mode == ARB_vertex) {
1694 $$ = VERT_RESULT_PSIZ;
1695 } else {
1696 yyerror(& @2, state, "invalid program result name");
1697 YYERROR;
1698 }
1699 }
1700 | RESULT TEXCOORD optTexCoordUnitNum
1701 {
1702 if (state->mode == ARB_vertex) {
1703 $$ = VERT_RESULT_TEX0 + $3;
1704 } else {
1705 yyerror(& @2, state, "invalid program result name");
1706 YYERROR;
1707 }
1708 }
1709 | RESULT DEPTH
1710 {
1711 if (state->mode == ARB_fragment) {
1712 $$ = FRAG_RESULT_DEPTH;
1713 } else {
1714 yyerror(& @2, state, "invalid program result name");
1715 YYERROR;
1716 }
1717 }
1718 ;
1719
1720resultColBinding: COLOR optResultFaceType optResultColorType
1721 {
1722 $$ = $2 + $3;
1723 }
1724 ;
1725
1726optResultFaceType:
1727 {
1728 $$ = (state->mode == ARB_vertex)
1729 ? VERT_RESULT_COL0
1730 : FRAG_RESULT_COLOR;
1731 }
1732 | FRONT
1733 {
1734 if (state->mode == ARB_vertex) {
1735 $$ = VERT_RESULT_COL0;
1736 } else {
1737 yyerror(& @1, state, "invalid program result name");
1738 YYERROR;
1739 }
1740 }
1741 | BACK
1742 {
1743 if (state->mode == ARB_vertex) {
1744 $$ = VERT_RESULT_BFC0;
1745 } else {
1746 yyerror(& @1, state, "invalid program result name");
1747 YYERROR;
1748 }
1749 }
1750 ;
1751
1752optResultColorType:
1753 {
1754 $$ = 0;
1755 }
1756 | PRIMARY
1757 {
1758 if (state->mode == ARB_vertex) {
1759 $$ = 0;
1760 } else {
1761 yyerror(& @1, state, "invalid program result name");
1762 YYERROR;
1763 }
1764 }
1765 | SECONDARY
1766 {
1767 if (state->mode == ARB_vertex) {
1768 $$ = 1;
1769 } else {
1770 yyerror(& @1, state, "invalid program result name");
1771 YYERROR;
1772 }
1773 }
1774 ;
1775
1776optFaceType: { $$ = 0; }
1777 | FRONT { $$ = 0; }
1778 | BACK { $$ = 1; }
1779 ;
1780
1781optColorType: { $$ = 0; }
1782 | PRIMARY { $$ = 0; }
1783 | SECONDARY { $$ = 1; }
1784 ;
1785
1786optTexCoordUnitNum: { $$ = 0; }
1787 | '[' texCoordUnitNum ']' { $$ = $2; }
1788 ;
1789
1790optTexImageUnitNum: { $$ = 0; }
1791 | '[' texImageUnitNum ']' { $$ = $2; }
1792 ;
1793
1794optLegacyTexUnitNum: { $$ = 0; }
1795 | '[' legacyTexUnitNum ']' { $$ = $2; }
1796 ;
1797
1798texCoordUnitNum: INTEGER
1799 {
Ian Romanickef80c202009-07-22 17:13:08 -07001800 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001801 yyerror(& @1, state, "invalid texture coordinate unit selector");
1802 YYERROR;
1803 }
1804
1805 $$ = $1;
1806 }
1807 ;
1808
1809texImageUnitNum: INTEGER
1810 {
Ian Romanickef80c202009-07-22 17:13:08 -07001811 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001812 yyerror(& @1, state, "invalid texture image unit selector");
1813 YYERROR;
1814 }
1815
1816 $$ = $1;
1817 }
1818 ;
1819
1820legacyTexUnitNum: INTEGER
1821 {
Ian Romanickef80c202009-07-22 17:13:08 -07001822 if ((unsigned) $1 >= state->MaxTextureUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001823 yyerror(& @1, state, "invalid texture unit selector");
1824 YYERROR;
1825 }
1826
1827 $$ = $1;
1828 }
1829 ;
1830
1831ALIAS_statement: ALIAS IDENTIFIER '=' IDENTIFIER
1832 {
1833 struct asm_symbol *exist = (struct asm_symbol *)
1834 _mesa_symbol_table_find_symbol(state->st, 0, $2);
1835 struct asm_symbol *target = (struct asm_symbol *)
1836 _mesa_symbol_table_find_symbol(state->st, 0, $4);
1837
1838
1839 if (exist != NULL) {
1840 yyerror(& @2, state, "redeclared identifier");
1841 YYERROR;
1842 } else if (target == NULL) {
1843 yyerror(& @4, state,
1844 "undefined variable binding in ALIAS statement");
1845 YYERROR;
1846 } else {
1847 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
1848 }
1849 }
1850 ;
1851
1852%%
1853
1854struct asm_instruction *
1855asm_instruction_ctor(gl_inst_opcode op,
1856 const struct prog_dst_register *dst,
1857 const struct asm_src_register *src0,
1858 const struct asm_src_register *src1,
1859 const struct asm_src_register *src2)
1860{
1861 struct asm_instruction *inst = calloc(1, sizeof(struct asm_instruction));
1862
1863 if (inst) {
Ian Romanickef80c202009-07-22 17:13:08 -07001864 _mesa_init_instructions(& inst->Base, 1);
Ian Romanick770cebb2009-07-20 17:44:36 -07001865 inst->Base.Opcode = op;
1866 inst->Base.DstReg = *dst;
1867
1868 inst->Base.SrcReg[0] = src0->Base;
1869 inst->SrcReg[0] = *src0;
1870
1871 if (src1 != NULL) {
1872 inst->Base.SrcReg[1] = src1->Base;
1873 inst->SrcReg[1] = *src1;
Ian Romanickaec42912009-07-22 12:29:48 -07001874 } else {
1875 init_src_reg(& inst->SrcReg[1]);
Ian Romanick770cebb2009-07-20 17:44:36 -07001876 }
1877
1878 if (src2 != NULL) {
1879 inst->Base.SrcReg[2] = src2->Base;
1880 inst->SrcReg[2] = *src2;
Ian Romanickaec42912009-07-22 12:29:48 -07001881 } else {
1882 init_src_reg(& inst->SrcReg[2]);
Ian Romanick770cebb2009-07-20 17:44:36 -07001883 }
1884 }
1885
1886 return inst;
1887}
1888
1889
1890void
1891init_dst_reg(struct prog_dst_register *r)
1892{
1893 memset(r, 0, sizeof(*r));
1894 r->File = PROGRAM_UNDEFINED;
1895 r->WriteMask = WRITEMASK_XYZW;
1896 r->CondMask = COND_TR;
1897 r->CondSwizzle = SWIZZLE_NOOP;
1898}
1899
1900
1901void
1902init_src_reg(struct asm_src_register *r)
1903{
1904 memset(r, 0, sizeof(*r));
1905 r->Base.File = PROGRAM_UNDEFINED;
1906 r->Base.Swizzle = SWIZZLE_NOOP;
1907 r->Symbol = NULL;
1908}
1909
1910
1911/**
1912 * Validate the set of inputs used by a program
1913 *
1914 * Validates that legal sets of inputs are used by the program. In this case
1915 * "used" included both reading the input or binding the input to a name using
1916 * the \c ATTRIB command.
1917 *
1918 * \return
1919 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
1920 */
1921int
1922validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
1923{
1924 const int inputs = state->prog->InputsRead | state->InputsBound;
1925
1926 if (((inputs & 0x0ffff) & (inputs >> 16)) != 0) {
1927 yyerror(locp, state, "illegal use of generic attribute and name attribute");
1928 return 0;
1929 }
1930
1931 return 1;
1932}
1933
1934
1935struct asm_symbol *
1936declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
1937 struct YYLTYPE *locp)
1938{
1939 struct asm_symbol *s = NULL;
1940 struct asm_symbol *exist = (struct asm_symbol *)
1941 _mesa_symbol_table_find_symbol(state->st, 0, name);
1942
1943
1944 if (exist != NULL) {
1945 yyerror(locp, state, "redeclared identifier");
1946 } else {
1947 s = calloc(1, sizeof(struct asm_symbol));
1948 s->name = name;
1949 s->type = t;
1950
1951 switch (t) {
1952 case at_temp:
1953 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
1954 yyerror(locp, state, "too many temporaries declared");
1955 free(s);
1956 return NULL;
1957 }
1958
1959 s->temp_binding = state->prog->NumTemporaries;
1960 state->prog->NumTemporaries++;
1961 break;
1962
1963 case at_address:
1964 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
1965 yyerror(locp, state, "too many address registers declared");
1966 free(s);
1967 return NULL;
1968 }
1969
1970 /* FINISHME: Add support for multiple address registers.
1971 */
1972 state->prog->NumAddressRegs++;
1973 break;
1974
1975 default:
1976 break;
1977 }
1978
1979 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
Ian Romanick94b45562009-07-27 12:21:26 -07001980 s->next = state->sym;
1981 state->sym = s;
Ian Romanick770cebb2009-07-20 17:44:36 -07001982 }
1983
1984 return s;
1985}
1986
1987
1988int add_state_reference(struct gl_program_parameter_list *param_list,
1989 const gl_state_index tokens[STATE_LENGTH])
1990{
1991 const GLuint size = 4; /* XXX fix */
1992 char *name;
1993 GLint index;
1994
1995 name = _mesa_program_state_string(tokens);
1996 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
1997 size, GL_NONE,
1998 NULL, (gl_state_index *) tokens, 0x0);
1999 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2000
2001 /* free name string here since we duplicated it in add_parameter() */
2002 _mesa_free(name);
2003
2004 return index;
2005}
2006
2007
2008int
2009initialize_symbol_from_state(struct gl_program *prog,
2010 struct asm_symbol *param_var,
2011 const gl_state_index tokens[STATE_LENGTH])
2012{
2013 int idx = -1;
2014 gl_state_index state_tokens[STATE_LENGTH];
2015
2016
2017 memcpy(state_tokens, tokens, sizeof(state_tokens));
2018
2019 param_var->type = at_param;
Ian Romanick28b13032009-07-22 16:03:32 -07002020 param_var->param_binding_type = PROGRAM_STATE_VAR;
Ian Romanick770cebb2009-07-20 17:44:36 -07002021
2022 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2023 * unroll it and call add_state_reference() for each row
2024 */
2025 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2026 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2027 state_tokens[0] == STATE_MVP_MATRIX ||
2028 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2029 state_tokens[0] == STATE_PROGRAM_MATRIX)
2030 && (state_tokens[2] != state_tokens[3])) {
2031 int row;
2032 const int first_row = state_tokens[2];
2033 const int last_row = state_tokens[3];
2034
2035 for (row = first_row; row <= last_row; row++) {
2036 state_tokens[2] = state_tokens[3] = row;
2037
2038 idx = add_state_reference(prog->Parameters, state_tokens);
2039 if (param_var->param_binding_begin == ~0U)
2040 param_var->param_binding_begin = idx;
2041 param_var->param_binding_length++;
2042 }
2043 }
2044 else {
2045 idx = add_state_reference(prog->Parameters, state_tokens);
2046 if (param_var->param_binding_begin == ~0U)
2047 param_var->param_binding_begin = idx;
2048 param_var->param_binding_length++;
2049 }
2050
2051 return idx;
2052}
2053
2054
2055int
2056initialize_symbol_from_param(struct gl_program *prog,
2057 struct asm_symbol *param_var,
2058 const gl_state_index tokens[STATE_LENGTH])
2059{
2060 int idx = -1;
2061 gl_state_index state_tokens[STATE_LENGTH];
2062
2063
2064 memcpy(state_tokens, tokens, sizeof(state_tokens));
2065
2066 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2067 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2068 assert((state_tokens[1] == STATE_ENV)
2069 || (state_tokens[1] == STATE_LOCAL));
2070
2071 param_var->type = at_param;
Ian Romanick28b13032009-07-22 16:03:32 -07002072 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2073 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
Ian Romanick770cebb2009-07-20 17:44:36 -07002074
2075 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2076 * we need to unroll it and call add_state_reference() for each row
2077 */
2078 if (state_tokens[2] != state_tokens[3]) {
2079 int row;
2080 const int first_row = state_tokens[2];
2081 const int last_row = state_tokens[3];
2082
2083 for (row = first_row; row <= last_row; row++) {
2084 state_tokens[2] = state_tokens[3] = row;
2085
2086 idx = add_state_reference(prog->Parameters, state_tokens);
2087 if (param_var->param_binding_begin == ~0U)
2088 param_var->param_binding_begin = idx;
2089 param_var->param_binding_length++;
2090 }
2091 }
2092 else {
2093 idx = add_state_reference(prog->Parameters, state_tokens);
2094 if (param_var->param_binding_begin == ~0U)
2095 param_var->param_binding_begin = idx;
2096 param_var->param_binding_length++;
2097 }
2098
2099 return idx;
2100}
2101
2102
2103int
2104initialize_symbol_from_const(struct gl_program *prog,
2105 struct asm_symbol *param_var,
2106 const struct asm_vector *vec)
2107{
2108 const int idx = _mesa_add_parameter(prog->Parameters, PROGRAM_CONSTANT,
2109 NULL, vec->count, GL_NONE, vec->data,
2110 NULL, 0x0);
2111
2112 param_var->type = at_param;
Ian Romanick28b13032009-07-22 16:03:32 -07002113 param_var->param_binding_type = PROGRAM_CONSTANT;
Ian Romanick770cebb2009-07-20 17:44:36 -07002114
2115 if (param_var->param_binding_begin == ~0U)
2116 param_var->param_binding_begin = idx;
2117 param_var->param_binding_length++;
2118
2119 return idx;
2120}
2121
2122
Ian Romanick44843c72009-07-22 15:06:49 -07002123char *
2124make_error_string(const char *fmt, ...)
2125{
2126 int length;
2127 char *str;
2128 va_list args;
2129
2130 va_start(args, fmt);
2131
2132 /* Call vsnprintf once to determine how large the final string is. Call it
2133 * again to do the actual formatting. from the vsnprintf manual page:
2134 *
2135 * Upon successful return, these functions return the number of
2136 * characters printed (not including the trailing '\0' used to end
2137 * output to strings).
2138 */
2139 length = 1 + vsnprintf(NULL, 0, fmt, args);
2140
2141 str = _mesa_malloc(length);
2142 if (str) {
2143 vsnprintf(str, length, fmt, args);
2144 }
2145
2146 va_end(args);
2147
2148 return str;
2149}
2150
2151
Ian Romanick770cebb2009-07-20 17:44:36 -07002152void
2153yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2154{
Ian Romanick44843c72009-07-22 15:06:49 -07002155 char *err_str;
Ian Romanick770cebb2009-07-20 17:44:36 -07002156
Ian Romanick44843c72009-07-22 15:06:49 -07002157
2158 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2159 if (err_str) {
2160 _mesa_error(state->ctx, GL_INVALID_OPERATION, err_str);
2161 _mesa_free(err_str);
2162 }
2163
2164 err_str = make_error_string("line %u, char %u: error: %s\n",
2165 locp->first_line, locp->first_column, s);
2166 _mesa_set_program_error(state->ctx, locp->position, err_str);
2167
2168 if (err_str) {
2169 _mesa_free(err_str);
2170 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002171}
2172
Ian Romanick44843c72009-07-22 15:06:49 -07002173
Ian Romanick770cebb2009-07-20 17:44:36 -07002174GLboolean
2175_mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2176 GLsizei len, struct asm_parser_state *state)
2177{
2178 struct gl_program_constants limits;
2179 struct asm_instruction *inst;
2180 unsigned i;
2181 GLubyte *strz;
Ian Romanick94b45562009-07-27 12:21:26 -07002182 GLboolean result = GL_FALSE;
2183 void *temp;
2184 struct asm_symbol *sym;
Ian Romanick770cebb2009-07-20 17:44:36 -07002185
Ian Romanick44843c72009-07-22 15:06:49 -07002186 state->ctx = ctx;
Ian Romanick770cebb2009-07-20 17:44:36 -07002187 state->prog->Target = target;
2188 state->prog->Parameters = _mesa_new_parameter_list();
2189
2190 /* Make a copy of the program string and force it to be NUL-terminated.
2191 */
2192 strz = (GLubyte *) _mesa_malloc(len + 1);
2193 if (strz == NULL) {
2194 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2195 return GL_FALSE;
2196 }
2197 _mesa_memcpy (strz, str, len);
2198 strz[len] = '\0';
2199
2200 state->prog->String = strz;
2201
2202 state->st = _mesa_symbol_table_ctor();
2203
2204 /* All of these limits should come from ctx.
2205 */
2206 limits.MaxInstructions = 128;
2207 limits.MaxAluInstructions = 128;
2208 limits.MaxTexInstructions = 128;
2209 limits.MaxTexIndirections = 128;
2210 limits.MaxAttribs = 16;
2211 limits.MaxTemps = 128;
2212 limits.MaxAddressRegs = 1;
2213 limits.MaxParameters = 128;
2214 limits.MaxLocalParams = 256;
2215 limits.MaxEnvParams = 128;
2216 limits.MaxNativeInstructions = 128;
2217 limits.MaxNativeAluInstructions = 128;
2218 limits.MaxNativeTexInstructions = 128;
2219 limits.MaxNativeTexIndirections = 128;
2220 limits.MaxNativeAttribs = 16;
2221 limits.MaxNativeTemps = 128;
2222 limits.MaxNativeAddressRegs = 1;
2223 limits.MaxNativeParameters = 128;
2224 limits.MaxUniformComponents = 0;
2225
2226 state->limits = & limits;
2227
2228 state->MaxTextureImageUnits = 16;
2229 state->MaxTextureCoordUnits = 8;
2230 state->MaxTextureUnits = 8;
2231 state->MaxClipPlanes = 6;
2232 state->MaxLights = 8;
2233 state->MaxProgramMatrices = 8;
2234
2235 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2236 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2237
2238 _mesa_set_program_error(ctx, -1, NULL);
2239
2240 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2241 yyparse(state);
2242 _mesa_program_lexer_dtor(state->scanner);
2243
2244
Ian Romanick44843c72009-07-22 15:06:49 -07002245 if (ctx->Program.ErrorPos != -1) {
Ian Romanick94b45562009-07-27 12:21:26 -07002246 goto error;
Ian Romanick44843c72009-07-22 15:06:49 -07002247 }
2248
Ian Romanick770cebb2009-07-20 17:44:36 -07002249 if (! _mesa_layout_parameters(state)) {
Ian Romanick44843c72009-07-22 15:06:49 -07002250 struct YYLTYPE loc;
2251
2252 loc.first_line = 0;
2253 loc.first_column = 0;
2254 loc.position = len;
2255
2256 yyerror(& loc, state, "invalid PARAM usage");
Ian Romanick94b45562009-07-27 12:21:26 -07002257 goto error;
Ian Romanick770cebb2009-07-20 17:44:36 -07002258 }
2259
2260
2261
2262 /* Add one instruction to store the "END" instruction.
2263 */
2264 state->prog->Instructions =
2265 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2266 inst = state->inst_head;
2267 for (i = 0; i < state->prog->NumInstructions; i++) {
2268 struct asm_instruction *const temp = inst->next;
2269
2270 state->prog->Instructions[i] = inst->Base;
Ian Romanick770cebb2009-07-20 17:44:36 -07002271 inst = temp;
2272 }
2273
2274 /* Finally, tag on an OPCODE_END instruction */
2275 {
2276 const GLuint numInst = state->prog->NumInstructions;
2277 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2278 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2279 }
2280 state->prog->NumInstructions++;
2281
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002282 state->prog->NumParameters = state->prog->Parameters->NumParameters;
Ian Romanick4c5879f2009-07-29 09:47:14 -07002283 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002284
Ian Romanick770cebb2009-07-20 17:44:36 -07002285 /*
2286 * Initialize native counts to logical counts. The device driver may
2287 * change them if program is translated into a hardware program.
2288 */
2289 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2290 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2291 state->prog->NumNativeParameters = state->prog->NumParameters;
2292 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2293 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2294
Ian Romanick94b45562009-07-27 12:21:26 -07002295 result = GL_TRUE;
2296
2297error:
2298 for (inst = state->inst_head; inst != NULL; inst = temp) {
2299 temp = inst->next;
2300 _mesa_free(inst);
2301 }
2302
2303 state->inst_head = NULL;
2304 state->inst_tail = NULL;
2305
2306 for (sym = state->sym; sym != NULL; sym = temp) {
2307 temp = sym->next;
2308
2309 _mesa_free((void *) sym->name);
2310 _mesa_free(sym);
2311 }
2312 state->sym = NULL;
2313
2314 _mesa_symbol_table_dtor(state->st);
2315 state->st = NULL;
2316
2317 return result;
Ian Romanick770cebb2009-07-20 17:44:36 -07002318}