blob: b2db2958be81fcbde52347e41482eef02fdb3db7 [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;
Brian Paul592a6642009-09-04 09:15:35 -0600120 gl_state_index state[STATE_LENGTH];
Ian Romanick770cebb2009-07-20 17:44:36 -0700121 int negate;
122 struct asm_vector vector;
123 gl_inst_opcode opcode;
Ian Romanick565a2a82009-07-30 10:51:43 -0700124
125 struct {
126 unsigned swz;
127 unsigned rgba_valid:1;
128 unsigned xyzw_valid:1;
129 unsigned negate:1;
130 } ext_swizzle;
Ian Romanick770cebb2009-07-20 17:44:36 -0700131}
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
Brian Paul3fedd082009-09-04 09:06:40 -0600162%token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
Ian Romanick770cebb2009-07-20 17:44:36 -0700163%token RANGE RESULT ROW
Brian Paul3fedd082009-09-04 09:06:40 -0600164%token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
Ian Romanick770cebb2009-07-20 17:44:36 -0700165%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
Ian Romanick1edd13b2009-07-27 16:24:49 -0700167%token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
Ian Romanickaafd5762009-07-27 17:22:21 -0700168%token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
Ian Romanick770cebb2009-07-20 17:44:36 -0700169%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
Ian Romanick565a2a82009-07-30 10:51:43 -0700185%type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
186%type <ext_swizzle> extSwizComp extSwizSel
Ian Romanick770cebb2009-07-20 17:44:36 -0700187%type <swiz_mask> optionalMask
Ian Romanick770cebb2009-07-20 17:44:36 -0700188
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
Ian Romanick333bb4f2009-07-29 20:41:48 -0700215%type <state> stateTexEnvItem stateDepthItem
Ian Romanick770cebb2009-07-20 17:44:36 -0700216
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%{
250extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
251 void *yyscanner);
252%}
253
254%%
255
256program: language optionSequence statementSequence END
257 ;
258
259language: 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;
Ian Romanick88018e22009-07-27 15:47:52 -0700273
274 state->option.TexRect =
275 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
Ian Romanick770cebb2009-07-20 17:44:36 -0700276 }
277 ;
278
279optionSequence: optionSequence option
280 |
281 ;
282
283option: 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
Ian Romanick301a9b72009-11-05 14:15:56 -0800294 free($2);
295
Ian Romanick770cebb2009-07-20 17:44:36 -0700296 if (!valid) {
Ian Romanick41d56962009-07-27 16:33:16 -0700297 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);
Ian Romanick770cebb2009-07-20 17:44:36 -0700302 YYERROR;
303 }
304 }
305 ;
306
307statementSequence: statementSequence statement
308 |
309 ;
310
311statement: 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
329instruction: ALU_instruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700330 {
331 $$ = $1;
332 state->prog->NumAluInstructions++;
333 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700334 | TexInstruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700335 {
336 $$ = $1;
337 state->prog->NumTexInstructions++;
338 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700339 ;
340
341ALU_instruction: ARL_instruction
342 | VECTORop_instruction
343 | SCALARop_instruction
344 | BINSCop_instruction
345 | BINop_instruction
346 | TRIop_instruction
347 | SWZ_instruction
348 ;
349
350TexInstruction: SAMPLE_instruction
351 | KIL_instruction
352 ;
353
354ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
355 {
356 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
357 }
358 ;
359
360VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
361 {
362 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
363 $$->Base.SaturateMode = $1.SaturateMode;
364 }
365 ;
366
367SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
368 {
369 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
370 $$->Base.SaturateMode = $1.SaturateMode;
371 }
372 ;
373
374BINSCop_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
382BINop_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
389TRIop_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
397SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
398 {
399 $$ = asm_instruction_ctor($1.Opcode, & $2, & $4, NULL, NULL);
400 if ($$ != NULL) {
Ian Romanick1edd13b2009-07-27 16:24:49 -0700401 const GLbitfield tex_mask = (1U << $6);
402 GLbitfield shadow_tex = 0;
403 GLbitfield target_mask = 0;
404
405
Ian Romanick770cebb2009-07-20 17:44:36 -0700406 $$->Base.SaturateMode = $1.SaturateMode;
407 $$->Base.TexSrcUnit = $6;
Ian Romanick0db5ef02009-07-22 16:21:54 -0700408
Ian Romanick1edd13b2009-07-27 16:24:49 -0700409 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;
Ian Romanick770cebb2009-07-20 17:44:36 -0700438 }
439 }
440 ;
441
442KIL_instruction: KIL swizzleSrcReg
443 {
444 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
Ian Romanick0db5ef02009-07-22 16:21:54 -0700445 state->fragment.UsesKill = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700446 }
447 ;
448
449texImageUnit: TEXTURE_UNIT optTexImageUnitNum
450 {
451 $$ = $2;
452 }
453 ;
454
455texTarget: 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; }
Ian Romanick1edd13b2009-07-27 16:24:49 -0700460 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
461 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
462 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
Ian Romanickaafd5762009-07-27 17:22:21 -0700463 | 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; }
Ian Romanick770cebb2009-07-20 17:44:36 -0700467 ;
468
469SWZ_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;
Ian Romanick648dac42009-07-28 21:57:28 -0700475 $4.Base.Negate = $6.mask;
Ian Romanick770cebb2009-07-20 17:44:36 -0700476
477 $$ = asm_instruction_ctor(OPCODE_SWZ, & $2, & $4, NULL, NULL);
478 $$->Base.SaturateMode = $1.SaturateMode;
479 }
480 ;
481
482scalarSrcReg: 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
495swizzleSrcReg: 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
508maskedDstReg: 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
530maskedAddrReg: addrReg addrWriteMask
531 {
532 init_dst_reg(& $$);
533 $$.File = PROGRAM_ADDRESS;
534 $$.Index = 0;
535 $$.WriteMask = $2.mask;
536 }
537 ;
538
539extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
540 {
Ian Romanick565a2a82009-07-30 10:51:43 -0700541 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);
Ian Romanick770cebb2009-07-20 17:44:36 -0700569 }
570 ;
571
572extSwizComp: optionalSign extSwizSel
573 {
Ian Romanick565a2a82009-07-30 10:51:43 -0700574 $$ = $2;
575 $$.negate = ($1) ? 1 : 0;
Ian Romanick770cebb2009-07-20 17:44:36 -0700576 }
577 ;
578
579extSwizSel: INTEGER
580 {
581 if (($1 != 0) && ($1 != 1)) {
582 yyerror(& @1, state, "invalid extended swizzle selector");
583 YYERROR;
584 }
585
Ian Romanick565a2a82009-07-30 10:51:43 -0700586 $$.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;
Ian Romanick770cebb2009-07-20 17:44:36 -0700593 }
594 | IDENTIFIER
595 {
Ian Romanick301a9b72009-11-05 14:15:56 -0800596 char s;
597
Ian Romanick770cebb2009-07-20 17:44:36 -0700598 if (strlen($1) > 1) {
599 yyerror(& @1, state, "invalid extended swizzle selector");
600 YYERROR;
601 }
602
Ian Romanick301a9b72009-11-05 14:15:56 -0800603 s = $1[0];
604 free($1);
605
606 switch (s) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700607 case 'x':
Ian Romanick565a2a82009-07-30 10:51:43 -0700608 $$.swz = SWIZZLE_X;
609 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700610 break;
611 case 'y':
Ian Romanick565a2a82009-07-30 10:51:43 -0700612 $$.swz = SWIZZLE_Y;
613 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700614 break;
615 case 'z':
Ian Romanick565a2a82009-07-30 10:51:43 -0700616 $$.swz = SWIZZLE_Z;
617 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700618 break;
619 case 'w':
Ian Romanick565a2a82009-07-30 10:51:43 -0700620 $$.swz = SWIZZLE_W;
621 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700622 break;
Ian Romanick565a2a82009-07-30 10:51:43 -0700623
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
Ian Romanick770cebb2009-07-20 17:44:36 -0700641 default:
642 yyerror(& @1, state, "invalid extended swizzle selector");
643 YYERROR;
644 break;
645 }
646 }
647 ;
648
649srcReg: IDENTIFIER /* temporaryReg | progParamSingle */
650 {
651 struct asm_symbol *const s = (struct asm_symbol *)
652 _mesa_symbol_table_find_symbol(state->st, 0, $1);
653
Ian Romanick301a9b72009-11-05 14:15:56 -0800654 free($1);
655
Ian Romanick770cebb2009-07-20 17:44:36 -0700656 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:
Ian Romanick8a430dd2009-07-29 09:50:06 -0700679 $$.Base.File = PROGRAM_INPUT;
680 $$.Base.Index = s->attrib_binding;
681 state->prog->InputsRead |= (1U << $$.Base.Index);
Ian Romanick770cebb2009-07-20 17:44:36 -0700682
Ian Romanick8a430dd2009-07-29 09:50:06 -0700683 if (!validate_inputs(& @1, state)) {
684 YYERROR;
685 }
686 break;
Ian Romanick770cebb2009-07-20 17:44:36 -0700687
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
Ian Romanickef80c202009-07-22 17:13:08 -0700707 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700708 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(& $$);
Ian Romanick69d3d192009-07-22 10:51:18 -0700728 $$.Base.File = ($1.name != NULL)
729 ? $1.param_binding_type
730 : PROGRAM_CONSTANT;
Ian Romanick770cebb2009-07-20 17:44:36 -0700731 $$.Base.Index = $1.param_binding_begin;
732 }
733 ;
734
735dstReg: 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
Ian Romanick301a9b72009-11-05 14:15:56 -0800746 free($1);
747
Ian Romanick770cebb2009-07-20 17:44:36 -0700748 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(& $$);
Ian Romanick86b33b52009-07-28 21:56:42 -0700757 switch (s->type) {
758 case at_temp:
Ian Romanick770cebb2009-07-20 17:44:36 -0700759 $$.File = PROGRAM_TEMPORARY;
760 $$.Index = s->temp_binding;
Ian Romanick86b33b52009-07-28 21:56:42 -0700761 break;
762 case at_output:
763 $$.File = PROGRAM_OUTPUT;
764 $$.Index = s->output_binding;
765 break;
766 default:
Ian Romanick770cebb2009-07-20 17:44:36 -0700767 $$.File = s->param_binding_type;
768 $$.Index = s->param_binding_begin;
Ian Romanick86b33b52009-07-28 21:56:42 -0700769 break;
Ian Romanick770cebb2009-07-20 17:44:36 -0700770 }
771 }
772 ;
773
774progParamArray: IDENTIFIER
775 {
776 struct asm_symbol *const s = (struct asm_symbol *)
777 _mesa_symbol_table_find_symbol(state->st, 0, $1);
778
Ian Romanick301a9b72009-11-05 14:15:56 -0800779 free($1);
780
Ian Romanick770cebb2009-07-20 17:44:36 -0700781 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
793progParamArrayMem: progParamArrayAbs | progParamArrayRel;
794
795progParamArrayAbs: INTEGER
796 {
797 init_src_reg(& $$);
798 $$.Base.Index = $1;
799 }
800 ;
801
802progParamArrayRel: 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
814addrRegRelOffset: { $$ = 0; }
815 | '+' addrRegPosOffset { $$ = $2; }
816 | '-' addrRegNegOffset { $$ = -$2; }
817 ;
818
819addrRegPosOffset: 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
831addrRegNegOffset: 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
843addrReg: IDENTIFIER
844 {
845 struct asm_symbol *const s = (struct asm_symbol *)
846 _mesa_symbol_table_find_symbol(state->st, 0, $1);
847
Ian Romanick301a9b72009-11-05 14:15:56 -0800848 free($1);
849
Ian Romanick770cebb2009-07-20 17:44:36 -0700850 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
863addrComponent: 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
874addrWriteMask: 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
886scalarSuffix: MASK1;
887
888swizzleSuffix: MASK1
889 | MASK4
890 | SWIZZLE
891 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
892 ;
893
894optionalMask: MASK4 | MASK3 | MASK2 | MASK1
895 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
896 ;
897
898namingStatement: ATTRIB_statement
899 | PARAM_statement
900 | TEMP_statement
901 | ADDRESS_statement
902 | OUTPUT_statement
903 | ALIAS_statement
904 ;
905
906ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
907 {
908 struct asm_symbol *const s =
909 declare_variable(state, $2, at_attrib, & @2);
910
911 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -0800912 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -0700913 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
925attribBinding: VERTEX vtxAttribItem
926 {
927 $$ = $2;
928 }
929 | FRAGMENT fragAttribItem
930 {
931 $$ = $2;
932 }
933 ;
934
935vtxAttribItem: 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 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700949 if (!state->ctx->Extensions.EXT_secondary_color) {
950 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
951 YYERROR;
952 }
953
Ian Romanick770cebb2009-07-20 17:44:36 -0700954 $$ = VERT_ATTRIB_COLOR0 + $2;
955 }
956 | FOGCOORD
957 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700958 if (!state->ctx->Extensions.EXT_fog_coord) {
959 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
960 YYERROR;
961 }
962
Ian Romanick770cebb2009-07-20 17:44:36 -0700963 $$ = VERT_ATTRIB_FOG;
964 }
965 | TEXCOORD optTexCoordUnitNum
966 {
967 $$ = VERT_ATTRIB_TEX0 + $2;
968 }
969 | MATRIXINDEX '[' vtxWeightNum ']'
970 {
Ian Romanick054ab5a2009-07-27 14:11:38 -0700971 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -0700972 YYERROR;
973 }
974 | VTXATTRIB '[' vtxAttribNum ']'
975 {
976 $$ = VERT_ATTRIB_GENERIC0 + $3;
977 }
978 ;
979
980vtxAttribNum: INTEGER
981 {
Ian Romanickef80c202009-07-22 17:13:08 -0700982 if ((unsigned) $1 >= state->limits->MaxAttribs) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700983 yyerror(& @1, state, "invalid vertex attribute reference");
984 YYERROR;
985 }
986
987 $$ = $1;
988 }
989 ;
990
991vtxOptWeightNum: | '[' vtxWeightNum ']';
992vtxWeightNum: INTEGER;
993
994fragAttribItem: 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
1012PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1013
1014PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1015 {
1016 struct asm_symbol *const s =
1017 declare_variable(state, $2, at_param, & @2);
1018
1019 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001020 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001021 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
1031PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1032 {
Ian Romanickef80c202009-07-22 17:13:08 -07001033 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001034 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001035 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) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001043 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001044 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
1055optArraySize:
1056 {
1057 $$ = 0;
1058 }
1059 | INTEGER
1060 {
Ian Romanickdd245012009-10-20 10:58:14 -07001061 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001062 yyerror(& @1, state, "invalid parameter array size");
1063 YYERROR;
1064 } else {
1065 $$ = $1;
1066 }
1067 }
1068 ;
1069
1070paramSingleInit: '=' paramSingleItemDecl
1071 {
1072 $$ = $2;
1073 }
1074 ;
1075
1076paramMultipleInit: '=' '{' paramMultInitList '}'
1077 {
1078 $$ = $3;
1079 }
1080 ;
1081
1082paramMultInitList: paramMultipleItem
1083 | paramMultInitList ',' paramMultipleItem
1084 {
1085 $1.param_binding_length += $3.param_binding_length;
1086 $$ = $1;
1087 }
1088 ;
1089
1090paramSingleItemDecl: 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
1110paramSingleItemUse: 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
1130paramMultipleItem: 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
1150stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1151 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1152 ;
1153
1154stateSingleItem: 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($$)); }
Ian Romanick333bb4f2009-07-29 20:41:48 -07001164 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
Ian Romanick770cebb2009-07-20 17:44:36 -07001165 ;
1166
1167stateMaterialItem: MATERIAL optFaceType stateMatProperty
1168 {
1169 memset($$, 0, sizeof($$));
1170 $$[0] = STATE_MATERIAL;
1171 $$[1] = $2;
1172 $$[2] = $3;
1173 }
1174 ;
1175
1176stateMatProperty: ambDiffSpecProperty
1177 {
1178 $$ = $1;
1179 }
1180 | EMISSION
1181 {
1182 $$ = STATE_EMISSION;
1183 }
1184 | SHININESS
1185 {
1186 $$ = STATE_SHININESS;
1187 }
1188 ;
1189
1190stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1191 {
1192 memset($$, 0, sizeof($$));
1193 $$[0] = STATE_LIGHT;
1194 $$[1] = $3;
1195 $$[2] = $5;
1196 }
1197 ;
1198
1199stateLightProperty: ambDiffSpecProperty
1200 {
1201 $$ = $1;
1202 }
1203 | POSITION
1204 {
1205 $$ = STATE_POSITION;
1206 }
1207 | ATTENUATION
1208 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001209 if (!state->ctx->Extensions.EXT_point_parameters) {
1210 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1211 YYERROR;
1212 }
1213
Ian Romanick770cebb2009-07-20 17:44:36 -07001214 $$ = STATE_ATTENUATION;
1215 }
1216 | SPOT stateSpotProperty
1217 {
1218 $$ = $2;
1219 }
1220 | HALF
1221 {
1222 $$ = STATE_HALF_VECTOR;
1223 }
1224 ;
1225
1226stateSpotProperty: DIRECTION
1227 {
1228 $$ = STATE_SPOT_DIRECTION;
1229 }
1230 ;
1231
1232stateLightModelItem: LIGHTMODEL stateLModProperty
1233 {
1234 $$[0] = $2[0];
1235 $$[1] = $2[1];
1236 }
1237 ;
1238
1239stateLModProperty: 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
1252stateLightProdItem: 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
1262stateLProdProperty: ambDiffSpecProperty;
1263
1264stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1265 {
1266 memset($$, 0, sizeof($$));
1267 $$[0] = $3;
1268 $$[1] = $2;
1269 }
1270 ;
1271
1272stateTexEnvProperty: COLOR
1273 {
1274 $$ = STATE_TEXENV_COLOR;
1275 }
1276 ;
1277
1278ambDiffSpecProperty: AMBIENT
1279 {
1280 $$ = STATE_AMBIENT;
1281 }
1282 | DIFFUSE
1283 {
1284 $$ = STATE_DIFFUSE;
1285 }
1286 | SPECULAR
1287 {
1288 $$ = STATE_SPECULAR;
1289 }
1290 ;
1291
1292stateLightNumber: INTEGER
1293 {
Ian Romanickef80c202009-07-22 17:13:08 -07001294 if ((unsigned) $1 >= state->MaxLights) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001295 yyerror(& @1, state, "invalid light selector");
1296 YYERROR;
1297 }
1298
1299 $$ = $1;
1300 }
1301 ;
1302
1303stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1304 {
1305 memset($$, 0, sizeof($$));
1306 $$[0] = STATE_TEXGEN;
1307 $$[1] = $2;
1308 $$[2] = $3 + $4;
1309 }
1310 ;
1311
1312stateTexGenType: EYE
1313 {
1314 $$ = STATE_TEXGEN_EYE_S;
1315 }
1316 | OBJECT
1317 {
1318 $$ = STATE_TEXGEN_OBJECT_S;
1319 }
1320 ;
1321stateTexGenCoord: 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
1339stateFogItem: FOG stateFogProperty
1340 {
1341 memset($$, 0, sizeof($$));
1342 $$[0] = $2;
1343 }
1344 ;
1345
1346stateFogProperty: COLOR
1347 {
1348 $$ = STATE_FOG_COLOR;
1349 }
1350 | PARAMS
1351 {
1352 $$ = STATE_FOG_PARAMS;
1353 }
1354 ;
1355
1356stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1357 {
1358 memset($$, 0, sizeof($$));
1359 $$[0] = STATE_CLIPPLANE;
1360 $$[1] = $3;
1361 }
1362 ;
1363
1364stateClipPlaneNum: INTEGER
1365 {
Ian Romanickef80c202009-07-22 17:13:08 -07001366 if ((unsigned) $1 >= state->MaxClipPlanes) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001367 yyerror(& @1, state, "invalid clip plane selector");
1368 YYERROR;
1369 }
1370
1371 $$ = $1;
1372 }
1373 ;
1374
Brian Paul3fedd082009-09-04 09:06:40 -06001375statePointItem: POINT_TOK statePointProperty
Ian Romanick770cebb2009-07-20 17:44:36 -07001376 {
1377 memset($$, 0, sizeof($$));
1378 $$[0] = $2;
1379 }
1380 ;
1381
Brian Paul3fedd082009-09-04 09:06:40 -06001382statePointProperty: SIZE_TOK
Ian Romanick770cebb2009-07-20 17:44:36 -07001383 {
1384 $$ = STATE_POINT_SIZE;
1385 }
1386 | ATTENUATION
1387 {
1388 $$ = STATE_POINT_ATTENUATION;
1389 }
1390 ;
1391
1392stateMatrixRow: 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
1402stateMatrixRows: 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
1412optMatrixRows:
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
1435stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1436 {
1437 $$[0] = $2[0];
1438 $$[1] = $2[1];
1439 $$[2] = $3;
1440 }
1441 ;
1442
1443stateOptMatModifier:
1444 {
1445 $$ = 0;
1446 }
1447 | stateMatModifier
1448 {
1449 $$ = $1;
1450 }
1451 ;
1452
1453stateMatModifier: 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
1467stateMatrixRowNum: INTEGER
1468 {
1469 if ($1 > 3) {
1470 yyerror(& @1, state, "invalid matrix row reference");
1471 YYERROR;
1472 }
1473
1474 $$ = $1;
1475 }
1476 ;
1477
1478stateMatrixName: 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 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001500 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -07001501 YYERROR;
1502 }
1503 | MAT_PROGRAM '[' stateProgramMatNum ']'
1504 {
1505 $$[0] = STATE_PROGRAM_MATRIX;
1506 $$[1] = $3;
1507 }
1508 ;
1509
1510stateOptModMatNum:
1511 {
1512 $$ = 0;
1513 }
Ian Romanick847bc5c2009-09-01 11:10:05 -07001514 | '[' stateModMatNum ']'
Ian Romanick770cebb2009-07-20 17:44:36 -07001515 {
Ian Romanick847bc5c2009-09-01 11:10:05 -07001516 $$ = $2;
Ian Romanick770cebb2009-07-20 17:44:36 -07001517 }
1518 ;
1519stateModMatNum: 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 ;
1532statePaletteMatNum: 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 ;
1540stateProgramMatNum: INTEGER
1541 {
Ian Romanickef80c202009-07-22 17:13:08 -07001542 if ((unsigned) $1 >= state->MaxProgramMatrices) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001543 yyerror(& @1, state, "invalid program matrix selector");
1544 YYERROR;
1545 }
1546
1547 $$ = $1;
1548 }
1549 ;
1550
Ian Romanick333bb4f2009-07-29 20:41:48 -07001551stateDepthItem: DEPTH RANGE
1552 {
1553 memset($$, 0, sizeof($$));
1554 $$[0] = STATE_DEPTH_RANGE;
1555 }
1556 ;
Ian Romanick770cebb2009-07-20 17:44:36 -07001557
1558
1559programSingleItem: progEnvParam | progLocalParam;
1560
1561programMultipleItem: progEnvParams | progLocalParams;
1562
1563progEnvParams: 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
1573progEnvParamNums: progEnvParamNum
1574 {
1575 $$[0] = $1;
1576 $$[1] = $1;
1577 }
1578 | progEnvParamNum DOT_DOT progEnvParamNum
1579 {
1580 $$[0] = $1;
1581 $$[1] = $3;
1582 }
1583 ;
1584
1585progEnvParam: 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
1595progLocalParams: 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
1604progLocalParamNums: progLocalParamNum
1605 {
1606 $$[0] = $1;
1607 $$[1] = $1;
1608 }
1609 | progLocalParamNum DOT_DOT progLocalParamNum
1610 {
1611 $$[0] = $1;
1612 $$[1] = $3;
1613 }
1614 ;
1615
1616progLocalParam: 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
1626progEnvParamNum: INTEGER
1627 {
Ian Romanickef80c202009-07-22 17:13:08 -07001628 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001629 yyerror(& @1, state, "invalid environment parameter reference");
1630 YYERROR;
1631 }
1632 $$ = $1;
1633 }
1634 ;
1635
1636progLocalParamNum: INTEGER
1637 {
Ian Romanickef80c202009-07-22 17:13:08 -07001638 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001639 yyerror(& @1, state, "invalid local parameter reference");
1640 YYERROR;
1641 }
1642 $$ = $1;
1643 }
1644 ;
1645
1646
1647
1648paramConstDecl: paramConstScalarDecl | paramConstVector;
1649paramConstUse: paramConstScalarUse | paramConstVector;
1650
1651paramConstScalarDecl: signedFloatConstant
1652 {
Ian Romanick60071092009-07-29 21:07:41 -07001653 $$.count = 4;
Ian Romanick770cebb2009-07-20 17:44:36 -07001654 $$.data[0] = $1;
Ian Romanick60071092009-07-29 21:07:41 -07001655 $$.data[1] = $1;
1656 $$.data[2] = $1;
1657 $$.data[3] = $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001658 }
1659 ;
1660
1661paramConstScalarUse: REAL
1662 {
Ian Romanickac5551f2009-08-20 18:29:41 -07001663 $$.count = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001664 $$.data[0] = $1;
Ian Romanickac5551f2009-08-20 18:29:41 -07001665 $$.data[1] = $1;
1666 $$.data[2] = $1;
1667 $$.data[3] = $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001668 }
1669 | INTEGER
1670 {
Ian Romanickac5551f2009-08-20 18:29:41 -07001671 $$.count = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001672 $$.data[0] = (float) $1;
Ian Romanickac5551f2009-08-20 18:29:41 -07001673 $$.data[1] = (float) $1;
1674 $$.data[2] = (float) $1;
1675 $$.data[3] = (float) $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001676 }
1677 ;
1678
1679paramConstVector: '{' signedFloatConstant '}'
1680 {
Ian Romanick60071092009-07-29 21:07:41 -07001681 $$.count = 4;
Ian Romanick770cebb2009-07-20 17:44:36 -07001682 $$.data[0] = $2;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001683 $$.data[1] = 0.0f;
1684 $$.data[2] = 0.0f;
Ian Romanick60071092009-07-29 21:07:41 -07001685 $$.data[3] = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001686 }
1687 | '{' signedFloatConstant ',' signedFloatConstant '}'
1688 {
Ian Romanick60071092009-07-29 21:07:41 -07001689 $$.count = 4;
Ian Romanick770cebb2009-07-20 17:44:36 -07001690 $$.data[0] = $2;
1691 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001692 $$.data[2] = 0.0f;
Ian Romanick60071092009-07-29 21:07:41 -07001693 $$.data[3] = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001694 }
1695 | '{' signedFloatConstant ',' signedFloatConstant ','
1696 signedFloatConstant '}'
1697 {
Ian Romanick60071092009-07-29 21:07:41 -07001698 $$.count = 4;
Ian Romanick770cebb2009-07-20 17:44:36 -07001699 $$.data[0] = $2;
1700 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001701 $$.data[2] = $6;
Ian Romanick60071092009-07-29 21:07:41 -07001702 $$.data[3] = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001703 }
1704 | '{' signedFloatConstant ',' signedFloatConstant ','
1705 signedFloatConstant ',' signedFloatConstant '}'
1706 {
1707 $$.count = 4;
1708 $$.data[0] = $2;
1709 $$.data[1] = $4;
Ian Romanickf3cba9d2009-07-24 18:01:59 -07001710 $$.data[2] = $6;
1711 $$.data[3] = $8;
Ian Romanick770cebb2009-07-20 17:44:36 -07001712 }
1713 ;
1714
1715signedFloatConstant: optionalSign REAL
1716 {
1717 $$ = ($1) ? -$2 : $2;
1718 }
1719 | optionalSign INTEGER
1720 {
1721 $$ = (float)(($1) ? -$2 : $2);
1722 }
1723 ;
1724
1725optionalSign: '+' { $$ = FALSE; }
1726 | '-' { $$ = TRUE; }
1727 | { $$ = FALSE; }
1728 ;
1729
1730TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1731 ;
1732
1733ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1734 ;
1735
1736varNameList: varNameList ',' IDENTIFIER
1737 {
1738 if (!declare_variable(state, $3, $<integer>0, & @3)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001739 free($3);
Ian Romanick770cebb2009-07-20 17:44:36 -07001740 YYERROR;
1741 }
1742 }
1743 | IDENTIFIER
1744 {
1745 if (!declare_variable(state, $1, $<integer>0, & @1)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001746 free($1);
Ian Romanick770cebb2009-07-20 17:44:36 -07001747 YYERROR;
1748 }
1749 }
1750 ;
1751
1752OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1753 {
1754 struct asm_symbol *const s =
1755 declare_variable(state, $2, at_output, & @2);
1756
1757 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001758 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001759 YYERROR;
1760 } else {
1761 s->output_binding = $4;
1762 }
1763 }
1764 ;
1765
1766resultBinding: 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
1817resultColBinding: COLOR optResultFaceType optResultColorType
1818 {
1819 $$ = $2 + $3;
1820 }
1821 ;
1822
1823optResultFaceType:
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
1849optResultColorType:
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
1873optFaceType: { $$ = 0; }
1874 | FRONT { $$ = 0; }
1875 | BACK { $$ = 1; }
1876 ;
1877
1878optColorType: { $$ = 0; }
1879 | PRIMARY { $$ = 0; }
1880 | SECONDARY { $$ = 1; }
1881 ;
1882
1883optTexCoordUnitNum: { $$ = 0; }
1884 | '[' texCoordUnitNum ']' { $$ = $2; }
1885 ;
1886
1887optTexImageUnitNum: { $$ = 0; }
1888 | '[' texImageUnitNum ']' { $$ = $2; }
1889 ;
1890
1891optLegacyTexUnitNum: { $$ = 0; }
1892 | '[' legacyTexUnitNum ']' { $$ = $2; }
1893 ;
1894
1895texCoordUnitNum: INTEGER
1896 {
Ian Romanickef80c202009-07-22 17:13:08 -07001897 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001898 yyerror(& @1, state, "invalid texture coordinate unit selector");
1899 YYERROR;
1900 }
1901
1902 $$ = $1;
1903 }
1904 ;
1905
1906texImageUnitNum: INTEGER
1907 {
Ian Romanickef80c202009-07-22 17:13:08 -07001908 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001909 yyerror(& @1, state, "invalid texture image unit selector");
1910 YYERROR;
1911 }
1912
1913 $$ = $1;
1914 }
1915 ;
1916
1917legacyTexUnitNum: INTEGER
1918 {
Ian Romanickef80c202009-07-22 17:13:08 -07001919 if ((unsigned) $1 >= state->MaxTextureUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001920 yyerror(& @1, state, "invalid texture unit selector");
1921 YYERROR;
1922 }
1923
1924 $$ = $1;
1925 }
1926 ;
1927
1928ALIAS_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
Ian Romanick301a9b72009-11-05 14:15:56 -08001936 free($4);
1937
Ian Romanick770cebb2009-07-20 17:44:36 -07001938 if (exist != NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001939 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001940 yyerror(& @2, state, "redeclared identifier");
1941 YYERROR;
1942 } else if (target == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001943 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001944 yyerror(& @4, state,
1945 "undefined variable binding in ALIAS statement");
1946 YYERROR;
1947 } else {
Ian Romanick1c7337d2009-11-04 12:03:44 -08001948 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
Ian Romanick770cebb2009-07-20 17:44:36 -07001949 }
1950 }
1951 ;
1952
1953%%
1954
1955struct asm_instruction *
1956asm_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) {
Ian Romanickef80c202009-07-22 17:13:08 -07001965 _mesa_init_instructions(& inst->Base, 1);
Ian Romanick770cebb2009-07-20 17:44:36 -07001966 inst->Base.Opcode = op;
Ian Romanick17534ab2009-07-30 09:41:35 -07001967
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 }
Ian Romanick770cebb2009-07-20 17:44:36 -07001976
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;
Ian Romanickaec42912009-07-22 12:29:48 -07001983 } else {
1984 init_src_reg(& inst->SrcReg[1]);
Ian Romanick770cebb2009-07-20 17:44:36 -07001985 }
1986
1987 if (src2 != NULL) {
1988 inst->Base.SrcReg[2] = src2->Base;
1989 inst->SrcReg[2] = *src2;
Ian Romanickaec42912009-07-22 12:29:48 -07001990 } else {
1991 init_src_reg(& inst->SrcReg[2]);
Ian Romanick770cebb2009-07-20 17:44:36 -07001992 }
1993 }
1994
1995 return inst;
1996}
1997
1998
1999void
2000init_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
2010void
2011init_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 */
2030int
2031validate_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
2044struct asm_symbol *
2045declare_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 {
Ian Romanick1c7337d2009-11-04 12:03:44 -08002056 s = calloc(1, sizeof(struct asm_symbol));
2057 s->name = name;
Ian Romanick770cebb2009-07-20 17:44:36 -07002058 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);
Ian Romanick94b45562009-07-27 12:21:26 -07002089 s->next = state->sym;
2090 state->sym = s;
Ian Romanick770cebb2009-07-20 17:44:36 -07002091 }
2092
2093 return s;
2094}
2095
2096
2097int 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,
Brian Paul592a6642009-09-04 09:15:35 -06002106 size, GL_NONE, NULL, tokens, 0x0);
Ian Romanick770cebb2009-07-20 17:44:36 -07002107 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
2116int
2117initialize_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;
Ian Romanick28b13032009-07-22 16:03:32 -07002128 param_var->param_binding_type = PROGRAM_STATE_VAR;
Ian Romanick770cebb2009-07-20 17:44:36 -07002129
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
2163int
2164initialize_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;
Ian Romanick28b13032009-07-22 16:03:32 -07002180 param_var->param_binding_type = (state_tokens[1] == STATE_ENV)
2181 ? PROGRAM_ENV_PARAM : PROGRAM_LOCAL_PARAM;
Ian Romanick770cebb2009-07-20 17:44:36 -07002182
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
2211int
2212initialize_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;
Ian Romanick28b13032009-07-22 16:03:32 -07002221 param_var->param_binding_type = PROGRAM_CONSTANT;
Ian Romanick770cebb2009-07-20 17:44:36 -07002222
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
Ian Romanick44843c72009-07-22 15:06:49 -07002231char *
2232make_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
Ian Romanick770cebb2009-07-20 17:44:36 -07002260void
2261yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2262{
Ian Romanick44843c72009-07-22 15:06:49 -07002263 char *err_str;
Ian Romanick770cebb2009-07-20 17:44:36 -07002264
Ian Romanick44843c72009-07-22 15:06:49 -07002265
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 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002279}
2280
Ian Romanick44843c72009-07-22 15:06:49 -07002281
Ian Romanick770cebb2009-07-20 17:44:36 -07002282GLboolean
2283_mesa_parse_arb_program(GLcontext *ctx, GLenum target, const GLubyte *str,
2284 GLsizei len, struct asm_parser_state *state)
2285{
Ian Romanick770cebb2009-07-20 17:44:36 -07002286 struct asm_instruction *inst;
2287 unsigned i;
2288 GLubyte *strz;
Ian Romanick94b45562009-07-27 12:21:26 -07002289 GLboolean result = GL_FALSE;
2290 void *temp;
2291 struct asm_symbol *sym;
Ian Romanick770cebb2009-07-20 17:44:36 -07002292
Ian Romanick44843c72009-07-22 15:06:49 -07002293 state->ctx = ctx;
Ian Romanick770cebb2009-07-20 17:44:36 -07002294 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
Ian Romanick8df95872009-10-27 11:46:29 -07002307 if (state->prog->String != NULL) {
2308 _mesa_free(state->prog->String);
2309 }
2310
Ian Romanick770cebb2009-07-20 17:44:36 -07002311 state->prog->String = strz;
2312
2313 state->st = _mesa_symbol_table_ctor();
2314
Ian Romanick48183ca2009-07-29 20:51:38 -07002315 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2316 ? & ctx->Const.VertexProgram
2317 : & ctx->Const.FragmentProgram;
Ian Romanick770cebb2009-07-20 17:44:36 -07002318
Brian Paul4cf27602009-08-24 11:05:11 -06002319 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2320 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2321 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2322 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2323 state->MaxLights = ctx->Const.MaxLights;
2324 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
Ian Romanick770cebb2009-07-20 17:44:36 -07002325
2326 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2327 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2328
2329 _mesa_set_program_error(ctx, -1, NULL);
2330
2331 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2332 yyparse(state);
2333 _mesa_program_lexer_dtor(state->scanner);
2334
2335
Ian Romanick44843c72009-07-22 15:06:49 -07002336 if (ctx->Program.ErrorPos != -1) {
Ian Romanick94b45562009-07-27 12:21:26 -07002337 goto error;
Ian Romanick44843c72009-07-22 15:06:49 -07002338 }
2339
Ian Romanick770cebb2009-07-20 17:44:36 -07002340 if (! _mesa_layout_parameters(state)) {
Ian Romanick44843c72009-07-22 15:06:49 -07002341 struct YYLTYPE loc;
2342
2343 loc.first_line = 0;
2344 loc.first_column = 0;
2345 loc.position = len;
2346
2347 yyerror(& loc, state, "invalid PARAM usage");
Ian Romanick94b45562009-07-27 12:21:26 -07002348 goto error;
Ian Romanick770cebb2009-07-20 17:44:36 -07002349 }
2350
2351
2352
2353 /* Add one instruction to store the "END" instruction.
2354 */
2355 state->prog->Instructions =
2356 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2357 inst = state->inst_head;
2358 for (i = 0; i < state->prog->NumInstructions; i++) {
2359 struct asm_instruction *const temp = inst->next;
2360
2361 state->prog->Instructions[i] = inst->Base;
Ian Romanick770cebb2009-07-20 17:44:36 -07002362 inst = temp;
2363 }
2364
2365 /* Finally, tag on an OPCODE_END instruction */
2366 {
2367 const GLuint numInst = state->prog->NumInstructions;
2368 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2369 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2370 }
2371 state->prog->NumInstructions++;
2372
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002373 state->prog->NumParameters = state->prog->Parameters->NumParameters;
Ian Romanick4c5879f2009-07-29 09:47:14 -07002374 state->prog->NumAttributes = _mesa_bitcount(state->prog->InputsRead);
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002375
Ian Romanick770cebb2009-07-20 17:44:36 -07002376 /*
2377 * Initialize native counts to logical counts. The device driver may
2378 * change them if program is translated into a hardware program.
2379 */
2380 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2381 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2382 state->prog->NumNativeParameters = state->prog->NumParameters;
2383 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2384 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2385
Ian Romanick94b45562009-07-27 12:21:26 -07002386 result = GL_TRUE;
2387
2388error:
2389 for (inst = state->inst_head; inst != NULL; inst = temp) {
2390 temp = inst->next;
2391 _mesa_free(inst);
2392 }
2393
2394 state->inst_head = NULL;
2395 state->inst_tail = NULL;
2396
2397 for (sym = state->sym; sym != NULL; sym = temp) {
2398 temp = sym->next;
2399
Ian Romanick1c7337d2009-11-04 12:03:44 -08002400 _mesa_free((void *) sym->name);
Ian Romanick94b45562009-07-27 12:21:26 -07002401 _mesa_free(sym);
2402 }
2403 state->sym = NULL;
2404
2405 _mesa_symbol_table_dtor(state->st);
2406 state->st = NULL;
2407
2408 return result;
Ian Romanick770cebb2009-07-20 17:44:36 -07002409}