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