blob: 025b54706daa0d6dca9cb2ba684875b833b18272 [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"
Brian Paulec2b92f2010-06-10 23:02:41 -060030#include "program/program.h"
31#include "program/prog_parameter.h"
32#include "program/prog_parameter_layout.h"
33#include "program/prog_statevars.h"
34#include "program/prog_instruction.h"
Ian Romanick770cebb2009-07-20 17:44:36 -070035
Brian Paulec2b92f2010-06-10 23:02:41 -060036#include "program/symbol_table.h"
37#include "program/program_parser.h"
Ian Romanick770cebb2009-07-20 17:44:36 -070038
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,
Brian Paul7c6ae4c2010-01-05 10:01:20 -070055 struct asm_symbol *param_var, const struct asm_vector *vec,
56 GLboolean allowSwizzle);
Ian Romanick770cebb2009-07-20 17:44:36 -070057
58static int yyparse(struct asm_parser_state *state);
59
Ian Romanickef80c202009-07-22 17:13:08 -070060static char *make_error_string(const char *fmt, ...);
61
Ian Romanick770cebb2009-07-20 17:44:36 -070062static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
63 const char *s);
64
65static int validate_inputs(struct YYLTYPE *locp,
66 struct asm_parser_state *state);
67
68static void init_dst_reg(struct prog_dst_register *r);
69
Brian Pauldf5615d2009-10-30 19:05:50 -060070static void set_dst_reg(struct prog_dst_register *r,
71 gl_register_file file, GLint index);
72
Ian Romanick770cebb2009-07-20 17:44:36 -070073static void init_src_reg(struct asm_src_register *r);
74
Brian Pauldf5615d2009-10-30 19:05:50 -060075static void set_src_reg(struct asm_src_register *r,
76 gl_register_file file, GLint index);
77
Brian Paul639e7a12009-12-29 09:06:00 -070078static void set_src_reg_swz(struct asm_src_register *r,
79 gl_register_file file, GLint index, GLuint swizzle);
80
Ian Romanick9ea43192009-09-04 16:35:50 -070081static void asm_instruction_set_operands(struct asm_instruction *inst,
82 const struct prog_dst_register *dst, const struct asm_src_register *src0,
83 const struct asm_src_register *src1, const struct asm_src_register *src2);
84
Ian Romanick770cebb2009-07-20 17:44:36 -070085static struct asm_instruction *asm_instruction_ctor(gl_inst_opcode op,
86 const struct prog_dst_register *dst, const struct asm_src_register *src0,
87 const struct asm_src_register *src1, const struct asm_src_register *src2);
88
Ian Romanick9ea43192009-09-04 16:35:50 -070089static struct asm_instruction *asm_instruction_copy_ctor(
90 const struct prog_instruction *base, const struct prog_dst_register *dst,
91 const struct asm_src_register *src0, const struct asm_src_register *src1,
92 const struct asm_src_register *src2);
93
Ian Romanick770cebb2009-07-20 17:44:36 -070094#ifndef FALSE
95#define FALSE 0
96#define TRUE (!FALSE)
97#endif
98
99#define YYLLOC_DEFAULT(Current, Rhs, N) \
100 do { \
101 if (YYID(N)) { \
102 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
103 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
104 (Current).position = YYRHSLOC(Rhs, 1).position; \
105 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
106 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
107 } else { \
108 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
109 (Current).last_line = (Current).first_line; \
110 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
111 (Current).last_column = (Current).first_column; \
112 (Current).position = YYRHSLOC(Rhs, 0).position \
113 + (Current).first_column; \
114 } \
115 } while(YYID(0))
116
117#define YYLEX_PARAM state->scanner
118%}
119
120%pure-parser
121%locations
122%parse-param { struct asm_parser_state *state }
123%error-verbose
124%lex-param { void *scanner }
125
126%union {
127 struct asm_instruction *inst;
128 struct asm_symbol *sym;
129 struct asm_symbol temp_sym;
130 struct asm_swizzle_mask swiz_mask;
131 struct asm_src_register src_reg;
132 struct prog_dst_register dst_reg;
133 struct prog_instruction temp_inst;
134 char *string;
135 unsigned result;
136 unsigned attrib;
137 int integer;
138 float real;
Brian Paul592a6642009-09-04 09:15:35 -0600139 gl_state_index state[STATE_LENGTH];
Ian Romanick770cebb2009-07-20 17:44:36 -0700140 int negate;
141 struct asm_vector vector;
142 gl_inst_opcode opcode;
Ian Romanick565a2a82009-07-30 10:51:43 -0700143
144 struct {
145 unsigned swz;
146 unsigned rgba_valid:1;
147 unsigned xyzw_valid:1;
148 unsigned negate:1;
149 } ext_swizzle;
Ian Romanick770cebb2009-07-20 17:44:36 -0700150}
151
152%token ARBvp_10 ARBfp_10
153
154/* Tokens for assembler pseudo-ops */
155%token <integer> ADDRESS
156%token ALIAS ATTRIB
157%token OPTION OUTPUT
158%token PARAM
159%token <integer> TEMP
160%token END
161
162 /* Tokens for instructions */
163%token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
Ian Romanickede0cd42009-08-31 17:00:31 -0700164%token <temp_inst> ARL KIL SWZ TXD_OP
Ian Romanick770cebb2009-07-20 17:44:36 -0700165
166%token <integer> INTEGER
167%token <real> REAL
168
169%token AMBIENT ATTENUATION
170%token BACK
171%token CLIP COLOR
172%token DEPTH DIFFUSE DIRECTION
173%token EMISSION ENV EYE
174%token FOG FOGCOORD FRAGMENT FRONT
175%token HALF
176%token INVERSE INVTRANS
177%token LIGHT LIGHTMODEL LIGHTPROD LOCAL
178%token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
179%token NORMAL
180%token OBJECT
Brian Paul3fedd082009-09-04 09:06:40 -0600181%token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
Ian Romanick770cebb2009-07-20 17:44:36 -0700182%token RANGE RESULT ROW
Brian Paul3fedd082009-09-04 09:06:40 -0600183%token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
Ian Romanick770cebb2009-07-20 17:44:36 -0700184%token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
185%token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
Ian Romanick1edd13b2009-07-27 16:24:49 -0700186%token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
Ian Romanickaafd5762009-07-27 17:22:21 -0700187%token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
Ian Romanick770cebb2009-07-20 17:44:36 -0700188%token VERTEX VTXATTRIB
189%token WEIGHT
190
Ian Romanick0e795332009-09-10 14:35:33 -0700191%token <string> IDENTIFIER USED_IDENTIFIER
192%type <string> string
Ian Romanick770cebb2009-07-20 17:44:36 -0700193%token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
194%token DOT_DOT
195%token DOT
196
197%type <inst> instruction ALU_instruction TexInstruction
198%type <inst> ARL_instruction VECTORop_instruction
199%type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
Ian Romanick8ca6fd82009-09-01 14:16:03 -0700200%type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
Ian Romanick770cebb2009-07-20 17:44:36 -0700201%type <inst> KIL_instruction
202
203%type <dst_reg> dstReg maskedDstReg maskedAddrReg
Ian Romanickeeb14022009-09-03 14:32:48 -0700204%type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
Ian Romanick565a2a82009-07-30 10:51:43 -0700205%type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
206%type <ext_swizzle> extSwizComp extSwizSel
Ian Romanick770cebb2009-07-20 17:44:36 -0700207%type <swiz_mask> optionalMask
Ian Romanick770cebb2009-07-20 17:44:36 -0700208
209%type <sym> progParamArray
210%type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
211%type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
212%type <sym> addrReg
213%type <swiz_mask> addrComponent addrWriteMask
214
Ian Romanick81722c52009-09-10 15:04:24 -0700215%type <dst_reg> ccMaskRule ccTest ccMaskRule2 ccTest2 optionalCcMask
216
Ian Romanick770cebb2009-07-20 17:44:36 -0700217%type <result> resultBinding resultColBinding
218%type <integer> optFaceType optColorType
219%type <integer> optResultFaceType optResultColorType
220
221%type <integer> optTexImageUnitNum texImageUnitNum
222%type <integer> optTexCoordUnitNum texCoordUnitNum
223%type <integer> optLegacyTexUnitNum legacyTexUnitNum
224%type <integer> texImageUnit texTarget
225%type <integer> vtxAttribNum
226
227%type <attrib> attribBinding vtxAttribItem fragAttribItem
228
229%type <temp_sym> paramSingleInit paramSingleItemDecl
230%type <integer> optArraySize
231
232%type <state> stateSingleItem stateMultipleItem
233%type <state> stateMaterialItem
234%type <state> stateLightItem stateLightModelItem stateLightProdItem
235%type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
236%type <state> stateMatrixItem stateMatrixRow stateMatrixRows
Ian Romanick333bb4f2009-07-29 20:41:48 -0700237%type <state> stateTexEnvItem stateDepthItem
Ian Romanick770cebb2009-07-20 17:44:36 -0700238
239%type <state> stateLModProperty
240%type <state> stateMatrixName optMatrixRows
241
242%type <integer> stateMatProperty
243%type <integer> stateLightProperty stateSpotProperty
244%type <integer> stateLightNumber stateLProdProperty
245%type <integer> stateTexGenType stateTexGenCoord
246%type <integer> stateTexEnvProperty
247%type <integer> stateFogProperty
248%type <integer> stateClipPlaneNum
249%type <integer> statePointProperty
250
251%type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
252%type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
253%type <integer> stateProgramMatNum
254
255%type <integer> ambDiffSpecProperty
256
257%type <state> programSingleItem progEnvParam progLocalParam
258%type <state> programMultipleItem progEnvParams progLocalParams
259
260%type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
261%type <temp_sym> paramSingleItemUse
262
263%type <integer> progEnvParamNum progLocalParamNum
264%type <state> progEnvParamNums progLocalParamNums
265
266%type <vector> paramConstDecl paramConstUse
267%type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
268%type <real> signedFloatConstant
269%type <negate> optionalSign
270
271%{
272extern int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
273 void *yyscanner);
274%}
275
276%%
277
278program: language optionSequence statementSequence END
279 ;
280
281language: ARBvp_10
282 {
283 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
284 yyerror(& @1, state, "invalid fragment program header");
285
286 }
287 state->mode = ARB_vertex;
288 }
289 | ARBfp_10
290 {
291 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
292 yyerror(& @1, state, "invalid vertex program header");
293 }
294 state->mode = ARB_fragment;
Ian Romanick88018e22009-07-27 15:47:52 -0700295
296 state->option.TexRect =
297 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
Ian Romanick770cebb2009-07-20 17:44:36 -0700298 }
299 ;
300
301optionSequence: optionSequence option
302 |
303 ;
304
Ian Romanick0e795332009-09-10 14:35:33 -0700305option: OPTION string ';'
Ian Romanick770cebb2009-07-20 17:44:36 -0700306 {
307 int valid = 0;
308
309 if (state->mode == ARB_vertex) {
310 valid = _mesa_ARBvp_parse_option(state, $2);
311 } else if (state->mode == ARB_fragment) {
312 valid = _mesa_ARBfp_parse_option(state, $2);
313 }
314
315
Ian Romanick301a9b72009-11-05 14:15:56 -0800316 free($2);
317
Ian Romanick770cebb2009-07-20 17:44:36 -0700318 if (!valid) {
Ian Romanick41d56962009-07-27 16:33:16 -0700319 const char *const err_str = (state->mode == ARB_vertex)
320 ? "invalid ARB vertex program option"
321 : "invalid ARB fragment program option";
322
323 yyerror(& @2, state, err_str);
Ian Romanick770cebb2009-07-20 17:44:36 -0700324 YYERROR;
325 }
326 }
327 ;
328
329statementSequence: statementSequence statement
330 |
331 ;
332
333statement: instruction ';'
334 {
335 if ($1 != NULL) {
336 if (state->inst_tail == NULL) {
337 state->inst_head = $1;
338 } else {
339 state->inst_tail->next = $1;
340 }
341
342 state->inst_tail = $1;
343 $1->next = NULL;
344
345 state->prog->NumInstructions++;
346 }
347 }
348 | namingStatement ';'
349 ;
350
351instruction: ALU_instruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700352 {
353 $$ = $1;
354 state->prog->NumAluInstructions++;
355 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700356 | TexInstruction
Ian Romanick0db5ef02009-07-22 16:21:54 -0700357 {
358 $$ = $1;
359 state->prog->NumTexInstructions++;
360 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700361 ;
362
363ALU_instruction: ARL_instruction
364 | VECTORop_instruction
365 | SCALARop_instruction
366 | BINSCop_instruction
367 | BINop_instruction
368 | TRIop_instruction
369 | SWZ_instruction
370 ;
371
372TexInstruction: SAMPLE_instruction
373 | KIL_instruction
Ian Romanick8ca6fd82009-09-01 14:16:03 -0700374 | TXD_instruction
Ian Romanick770cebb2009-07-20 17:44:36 -0700375 ;
376
377ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
378 {
379 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
380 }
381 ;
382
383VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
384 {
Paul Berrya0f7b862012-06-20 12:33:06 -0700385 if ($1.Opcode == OPCODE_DDY)
386 state->fragment.UsesDFdy = 1;
Ian Romanick9ea43192009-09-04 16:35:50 -0700387 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700388 }
389 ;
390
391SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
392 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700393 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700394 }
395 ;
396
397BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
398 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700399 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700400 }
401 ;
402
403
404BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
405 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700406 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700407 }
408 ;
409
410TRIop_instruction: TRI_OP maskedDstReg ','
411 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
412 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700413 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
Ian Romanick770cebb2009-07-20 17:44:36 -0700414 }
415 ;
416
417SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
418 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700419 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700420 if ($$ != NULL) {
Ian Romanick1edd13b2009-07-27 16:24:49 -0700421 const GLbitfield tex_mask = (1U << $6);
422 GLbitfield shadow_tex = 0;
423 GLbitfield target_mask = 0;
424
425
Ian Romanick770cebb2009-07-20 17:44:36 -0700426 $$->Base.TexSrcUnit = $6;
Ian Romanick0db5ef02009-07-22 16:21:54 -0700427
Ian Romanick1edd13b2009-07-27 16:24:49 -0700428 if ($8 < 0) {
429 shadow_tex = tex_mask;
430
431 $$->Base.TexSrcTarget = -$8;
432 $$->Base.TexShadow = 1;
433 } else {
434 $$->Base.TexSrcTarget = $8;
435 }
436
437 target_mask = (1U << $$->Base.TexSrcTarget);
438
439 /* If this texture unit was previously accessed and that access
440 * had a different texture target, generate an error.
441 *
442 * If this texture unit was previously accessed and that access
443 * had a different shadow mode, generate an error.
444 */
445 if ((state->prog->TexturesUsed[$6] != 0)
446 && ((state->prog->TexturesUsed[$6] != target_mask)
447 || ((state->prog->ShadowSamplers & tex_mask)
448 != shadow_tex))) {
449 yyerror(& @8, state,
450 "multiple targets used on one texture image unit");
451 YYERROR;
452 }
453
454
455 state->prog->TexturesUsed[$6] |= target_mask;
456 state->prog->ShadowSamplers |= shadow_tex;
Ian Romanick770cebb2009-07-20 17:44:36 -0700457 }
458 }
459 ;
460
461KIL_instruction: KIL swizzleSrcReg
462 {
463 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
Ian Romanick0db5ef02009-07-22 16:21:54 -0700464 state->fragment.UsesKill = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700465 }
Ian Romanick81722c52009-09-10 15:04:24 -0700466 | KIL ccTest
467 {
468 $$ = asm_instruction_ctor(OPCODE_KIL_NV, NULL, NULL, NULL, NULL);
469 $$->Base.DstReg.CondMask = $2.CondMask;
470 $$->Base.DstReg.CondSwizzle = $2.CondSwizzle;
Ian Romanick81722c52009-09-10 15:04:24 -0700471 state->fragment.UsesKill = 1;
472 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700473 ;
474
Ian Romanick8ca6fd82009-09-01 14:16:03 -0700475TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
476 {
Ian Romanick9ea43192009-09-04 16:35:50 -0700477 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
Ian Romanick8ca6fd82009-09-01 14:16:03 -0700478 if ($$ != NULL) {
479 const GLbitfield tex_mask = (1U << $10);
480 GLbitfield shadow_tex = 0;
481 GLbitfield target_mask = 0;
482
483
Ian Romanick8ca6fd82009-09-01 14:16:03 -0700484 $$->Base.TexSrcUnit = $10;
485
486 if ($12 < 0) {
487 shadow_tex = tex_mask;
488
489 $$->Base.TexSrcTarget = -$12;
490 $$->Base.TexShadow = 1;
491 } else {
492 $$->Base.TexSrcTarget = $12;
493 }
494
495 target_mask = (1U << $$->Base.TexSrcTarget);
496
497 /* If this texture unit was previously accessed and that access
498 * had a different texture target, generate an error.
499 *
500 * If this texture unit was previously accessed and that access
501 * had a different shadow mode, generate an error.
502 */
503 if ((state->prog->TexturesUsed[$10] != 0)
504 && ((state->prog->TexturesUsed[$10] != target_mask)
505 || ((state->prog->ShadowSamplers & tex_mask)
506 != shadow_tex))) {
507 yyerror(& @12, state,
508 "multiple targets used on one texture image unit");
509 YYERROR;
510 }
511
512
513 state->prog->TexturesUsed[$10] |= target_mask;
514 state->prog->ShadowSamplers |= shadow_tex;
515 }
516 }
Ian Romanick770cebb2009-07-20 17:44:36 -0700517 ;
518
519texImageUnit: TEXTURE_UNIT optTexImageUnitNum
520 {
521 $$ = $2;
522 }
523 ;
524
525texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
526 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
527 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
528 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
529 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
Ian Romanick1edd13b2009-07-27 16:24:49 -0700530 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
531 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
532 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
Ian Romanickaafd5762009-07-27 17:22:21 -0700533 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
534 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
535 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
536 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
Ian Romanick770cebb2009-07-20 17:44:36 -0700537 ;
538
539SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
540 {
541 /* FIXME: Is this correct? Should the extenedSwizzle be applied
542 * FIXME: to the existing swizzle?
543 */
544 $4.Base.Swizzle = $6.swizzle;
Ian Romanick648dac42009-07-28 21:57:28 -0700545 $4.Base.Negate = $6.mask;
Ian Romanick770cebb2009-07-20 17:44:36 -0700546
Ian Romanick9ea43192009-09-04 16:35:50 -0700547 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -0700548 }
549 ;
550
Ian Romanickeeb14022009-09-03 14:32:48 -0700551scalarSrcReg: optionalSign scalarUse
Ian Romanick770cebb2009-07-20 17:44:36 -0700552 {
553 $$ = $2;
554
555 if ($1) {
556 $$.Base.Negate = ~$$.Base.Negate;
557 }
Ian Romanickeeb14022009-09-03 14:32:48 -0700558 }
559 | optionalSign '|' scalarUse '|'
560 {
561 $$ = $3;
562
563 if (!state->option.NV_fragment) {
564 yyerror(& @2, state, "unexpected character '|'");
565 YYERROR;
566 }
567
568 if ($1) {
569 $$.Base.Negate = ~$$.Base.Negate;
570 }
571
572 $$.Base.Abs = 1;
573 }
574 ;
575
576scalarUse: srcReg scalarSuffix
577 {
578 $$ = $1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700579
580 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
Ian Romanickeeb14022009-09-03 14:32:48 -0700581 $2.swizzle);
Ian Romanick770cebb2009-07-20 17:44:36 -0700582 }
Ian Romanickeeb14022009-09-03 14:32:48 -0700583 | paramConstScalarUse
Ian Romanickb8e389b2009-09-03 14:05:18 -0700584 {
585 struct asm_symbol temp_sym;
586
587 if (!state->option.NV_fragment) {
Ian Romanickeeb14022009-09-03 14:32:48 -0700588 yyerror(& @1, state, "expected scalar suffix");
Ian Romanickb8e389b2009-09-03 14:05:18 -0700589 YYERROR;
590 }
591
592 memset(& temp_sym, 0, sizeof(temp_sym));
593 temp_sym.param_binding_begin = ~0;
Brian Paul7c6ae4c2010-01-05 10:01:20 -0700594 initialize_symbol_from_const(state->prog, & temp_sym, & $1, GL_TRUE);
Ian Romanickb8e389b2009-09-03 14:05:18 -0700595
Brian Paul639e7a12009-12-29 09:06:00 -0700596 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
597 temp_sym.param_binding_begin,
598 temp_sym.param_binding_swizzle);
Ian Romanick770cebb2009-07-20 17:44:36 -0700599 }
600 ;
601
602swizzleSrcReg: optionalSign srcReg swizzleSuffix
603 {
604 $$ = $2;
605
606 if ($1) {
607 $$.Base.Negate = ~$$.Base.Negate;
608 }
609
610 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
611 $3.swizzle);
612 }
Ian Romanickeeb14022009-09-03 14:32:48 -0700613 | optionalSign '|' srcReg swizzleSuffix '|'
614 {
615 $$ = $3;
616
617 if (!state->option.NV_fragment) {
618 yyerror(& @2, state, "unexpected character '|'");
619 YYERROR;
620 }
621
622 if ($1) {
623 $$.Base.Negate = ~$$.Base.Negate;
624 }
625
626 $$.Base.Abs = 1;
627 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
628 $4.swizzle);
629 }
630
Ian Romanick770cebb2009-07-20 17:44:36 -0700631 ;
632
Ian Romanick81722c52009-09-10 15:04:24 -0700633maskedDstReg: dstReg optionalMask optionalCcMask
Ian Romanick770cebb2009-07-20 17:44:36 -0700634 {
635 $$ = $1;
636 $$.WriteMask = $2.mask;
Ian Romanick81722c52009-09-10 15:04:24 -0700637 $$.CondMask = $3.CondMask;
638 $$.CondSwizzle = $3.CondSwizzle;
Ian Romanick770cebb2009-07-20 17:44:36 -0700639
640 if ($$.File == PROGRAM_OUTPUT) {
641 /* Technically speaking, this should check that it is in
642 * vertex program mode. However, PositionInvariant can never be
643 * set in fragment program mode, so it is somewhat irrelevant.
644 */
645 if (state->option.PositionInvariant
646 && ($$.Index == VERT_RESULT_HPOS)) {
647 yyerror(& @1, state, "position-invariant programs cannot "
648 "write position");
649 YYERROR;
650 }
651
Ian Romanick5606dfb2009-11-17 16:10:24 -0800652 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
Ian Romanick770cebb2009-07-20 17:44:36 -0700653 }
654 }
655 ;
656
657maskedAddrReg: addrReg addrWriteMask
658 {
Brian Pauldf5615d2009-10-30 19:05:50 -0600659 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
Ian Romanick770cebb2009-07-20 17:44:36 -0700660 $$.WriteMask = $2.mask;
661 }
662 ;
663
664extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
665 {
Ian Romanick565a2a82009-07-30 10:51:43 -0700666 const unsigned xyzw_valid =
667 ($1.xyzw_valid << 0)
668 | ($3.xyzw_valid << 1)
669 | ($5.xyzw_valid << 2)
670 | ($7.xyzw_valid << 3);
671 const unsigned rgba_valid =
672 ($1.rgba_valid << 0)
673 | ($3.rgba_valid << 1)
674 | ($5.rgba_valid << 2)
675 | ($7.rgba_valid << 3);
676
677 /* All of the swizzle components have to be valid in either RGBA
678 * or XYZW. Note that 0 and 1 are valid in both, so both masks
679 * can have some bits set.
680 *
681 * We somewhat deviate from the spec here. It would be really hard
682 * to figure out which component is the error, and there probably
683 * isn't a lot of benefit.
684 */
685 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
686 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
687 "components");
688 YYERROR;
689 }
690
691 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
692 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
693 | ($7.negate << 3);
Ian Romanick770cebb2009-07-20 17:44:36 -0700694 }
695 ;
696
697extSwizComp: optionalSign extSwizSel
698 {
Ian Romanick565a2a82009-07-30 10:51:43 -0700699 $$ = $2;
700 $$.negate = ($1) ? 1 : 0;
Ian Romanick770cebb2009-07-20 17:44:36 -0700701 }
702 ;
703
704extSwizSel: INTEGER
705 {
706 if (($1 != 0) && ($1 != 1)) {
707 yyerror(& @1, state, "invalid extended swizzle selector");
708 YYERROR;
709 }
710
Ian Romanick565a2a82009-07-30 10:51:43 -0700711 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
712
713 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
714 * swizzle names.
715 */
716 $$.xyzw_valid = 1;
717 $$.rgba_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700718 }
Ian Romanick0e795332009-09-10 14:35:33 -0700719 | string
Ian Romanick770cebb2009-07-20 17:44:36 -0700720 {
Ian Romanick301a9b72009-11-05 14:15:56 -0800721 char s;
722
Ian Romanick770cebb2009-07-20 17:44:36 -0700723 if (strlen($1) > 1) {
724 yyerror(& @1, state, "invalid extended swizzle selector");
725 YYERROR;
726 }
727
Ian Romanick301a9b72009-11-05 14:15:56 -0800728 s = $1[0];
729 free($1);
730
731 switch (s) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700732 case 'x':
Ian Romanick565a2a82009-07-30 10:51:43 -0700733 $$.swz = SWIZZLE_X;
734 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700735 break;
736 case 'y':
Ian Romanick565a2a82009-07-30 10:51:43 -0700737 $$.swz = SWIZZLE_Y;
738 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700739 break;
740 case 'z':
Ian Romanick565a2a82009-07-30 10:51:43 -0700741 $$.swz = SWIZZLE_Z;
742 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700743 break;
744 case 'w':
Ian Romanick565a2a82009-07-30 10:51:43 -0700745 $$.swz = SWIZZLE_W;
746 $$.xyzw_valid = 1;
Ian Romanick770cebb2009-07-20 17:44:36 -0700747 break;
Ian Romanick565a2a82009-07-30 10:51:43 -0700748
749 case 'r':
750 $$.swz = SWIZZLE_X;
751 $$.rgba_valid = 1;
752 break;
753 case 'g':
754 $$.swz = SWIZZLE_Y;
755 $$.rgba_valid = 1;
756 break;
757 case 'b':
758 $$.swz = SWIZZLE_Z;
759 $$.rgba_valid = 1;
760 break;
761 case 'a':
762 $$.swz = SWIZZLE_W;
763 $$.rgba_valid = 1;
764 break;
765
Ian Romanick770cebb2009-07-20 17:44:36 -0700766 default:
767 yyerror(& @1, state, "invalid extended swizzle selector");
768 YYERROR;
769 break;
770 }
771 }
772 ;
773
Ian Romanick0e795332009-09-10 14:35:33 -0700774srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
Ian Romanick770cebb2009-07-20 17:44:36 -0700775 {
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->type != at_temp)
785 && (s->type != at_attrib)) {
786 yyerror(& @1, state, "invalid operand variable");
787 YYERROR;
788 } else if ((s->type == at_param) && s->param_is_array) {
789 yyerror(& @1, state, "non-array access to array PARAM");
790 YYERROR;
791 }
792
793 init_src_reg(& $$);
794 switch (s->type) {
795 case at_temp:
Brian Pauldf5615d2009-10-30 19:05:50 -0600796 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
Ian Romanick770cebb2009-07-20 17:44:36 -0700797 break;
798 case at_param:
Brian Paul639e7a12009-12-29 09:06:00 -0700799 set_src_reg_swz(& $$, s->param_binding_type,
800 s->param_binding_begin,
801 s->param_binding_swizzle);
Ian Romanick770cebb2009-07-20 17:44:36 -0700802 break;
803 case at_attrib:
Brian Pauldf5615d2009-10-30 19:05:50 -0600804 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100805 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
Ian Romanick770cebb2009-07-20 17:44:36 -0700806
Ian Romanick8a430dd2009-07-29 09:50:06 -0700807 if (!validate_inputs(& @1, state)) {
808 YYERROR;
809 }
810 break;
Ian Romanick770cebb2009-07-20 17:44:36 -0700811
812 default:
813 YYERROR;
814 break;
815 }
816 }
817 | attribBinding
818 {
Brian Pauldf5615d2009-10-30 19:05:50 -0600819 set_src_reg(& $$, PROGRAM_INPUT, $1);
Mathias Fröhlichdca6a282011-10-31 18:32:59 +0100820 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
Ian Romanick770cebb2009-07-20 17:44:36 -0700821
822 if (!validate_inputs(& @1, state)) {
823 YYERROR;
824 }
825 }
826 | progParamArray '[' progParamArrayMem ']'
827 {
828 if (! $3.Base.RelAddr
Ian Romanickef80c202009-07-22 17:13:08 -0700829 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
Ian Romanick770cebb2009-07-20 17:44:36 -0700830 yyerror(& @3, state, "out of bounds array access");
831 YYERROR;
832 }
833
834 init_src_reg(& $$);
835 $$.Base.File = $1->param_binding_type;
836
837 if ($3.Base.RelAddr) {
Brian Paul56643092010-07-30 14:24:23 -0600838 state->prog->IndirectRegisterFiles |= (1 << $$.Base.File);
Ian Romanick770cebb2009-07-20 17:44:36 -0700839 $1->param_accessed_indirectly = 1;
840
841 $$.Base.RelAddr = 1;
842 $$.Base.Index = $3.Base.Index;
843 $$.Symbol = $1;
844 } else {
845 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
846 }
847 }
848 | paramSingleItemUse
849 {
Brian Pauldf5615d2009-10-30 19:05:50 -0600850 gl_register_file file = ($1.name != NULL)
Ian Romanick69d3d192009-07-22 10:51:18 -0700851 ? $1.param_binding_type
852 : PROGRAM_CONSTANT;
Brian Paul639e7a12009-12-29 09:06:00 -0700853 set_src_reg_swz(& $$, file, $1.param_binding_begin,
854 $1.param_binding_swizzle);
Ian Romanick770cebb2009-07-20 17:44:36 -0700855 }
856 ;
857
858dstReg: resultBinding
859 {
Brian Pauldf5615d2009-10-30 19:05:50 -0600860 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
Ian Romanick770cebb2009-07-20 17:44:36 -0700861 }
Ian Romanick0e795332009-09-10 14:35:33 -0700862 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
Ian Romanick770cebb2009-07-20 17:44:36 -0700863 {
864 struct asm_symbol *const s = (struct asm_symbol *)
865 _mesa_symbol_table_find_symbol(state->st, 0, $1);
866
Ian Romanick301a9b72009-11-05 14:15:56 -0800867 free($1);
868
Ian Romanick770cebb2009-07-20 17:44:36 -0700869 if (s == NULL) {
870 yyerror(& @1, state, "invalid operand variable");
871 YYERROR;
872 } else if ((s->type != at_output) && (s->type != at_temp)) {
873 yyerror(& @1, state, "invalid operand variable");
874 YYERROR;
875 }
876
Ian Romanick86b33b52009-07-28 21:56:42 -0700877 switch (s->type) {
878 case at_temp:
Brian Pauldf5615d2009-10-30 19:05:50 -0600879 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
Ian Romanick86b33b52009-07-28 21:56:42 -0700880 break;
881 case at_output:
Brian Pauldf5615d2009-10-30 19:05:50 -0600882 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
Ian Romanick86b33b52009-07-28 21:56:42 -0700883 break;
884 default:
Brian Pauldf5615d2009-10-30 19:05:50 -0600885 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
Ian Romanick86b33b52009-07-28 21:56:42 -0700886 break;
Ian Romanick770cebb2009-07-20 17:44:36 -0700887 }
888 }
889 ;
890
Ian Romanick0e795332009-09-10 14:35:33 -0700891progParamArray: USED_IDENTIFIER
Ian Romanick770cebb2009-07-20 17:44:36 -0700892 {
893 struct asm_symbol *const s = (struct asm_symbol *)
894 _mesa_symbol_table_find_symbol(state->st, 0, $1);
895
Ian Romanick301a9b72009-11-05 14:15:56 -0800896 free($1);
897
Ian Romanick770cebb2009-07-20 17:44:36 -0700898 if (s == NULL) {
899 yyerror(& @1, state, "invalid operand variable");
900 YYERROR;
901 } else if ((s->type != at_param) || !s->param_is_array) {
902 yyerror(& @1, state, "array access to non-PARAM variable");
903 YYERROR;
904 } else {
905 $$ = s;
906 }
907 }
908 ;
909
910progParamArrayMem: progParamArrayAbs | progParamArrayRel;
911
912progParamArrayAbs: INTEGER
913 {
914 init_src_reg(& $$);
915 $$.Base.Index = $1;
916 }
917 ;
918
919progParamArrayRel: addrReg addrComponent addrRegRelOffset
920 {
921 /* FINISHME: Add support for multiple address registers.
922 */
923 /* FINISHME: Add support for 4-component address registers.
924 */
925 init_src_reg(& $$);
926 $$.Base.RelAddr = 1;
927 $$.Base.Index = $3;
928 }
929 ;
930
931addrRegRelOffset: { $$ = 0; }
932 | '+' addrRegPosOffset { $$ = $2; }
933 | '-' addrRegNegOffset { $$ = -$2; }
934 ;
935
936addrRegPosOffset: INTEGER
937 {
Brian Paul8ad821d2011-03-02 09:32:45 -0700938 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
Brian Paul2aad3a82009-09-14 17:34:09 -0600939 char s[100];
Brian Paul78a0c352010-02-19 12:56:49 -0700940 _mesa_snprintf(s, sizeof(s),
Brian Paul2aad3a82009-09-14 17:34:09 -0600941 "relative address offset too large (%d)", $1);
942 yyerror(& @1, state, s);
Ian Romanick770cebb2009-07-20 17:44:36 -0700943 YYERROR;
944 } else {
945 $$ = $1;
946 }
947 }
948 ;
949
950addrRegNegOffset: INTEGER
951 {
Brian Paul8ad821d2011-03-02 09:32:45 -0700952 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
Brian Paul2aad3a82009-09-14 17:34:09 -0600953 char s[100];
Brian Paul78a0c352010-02-19 12:56:49 -0700954 _mesa_snprintf(s, sizeof(s),
Brian Paul2aad3a82009-09-14 17:34:09 -0600955 "relative address offset too large (%d)", $1);
956 yyerror(& @1, state, s);
Ian Romanick770cebb2009-07-20 17:44:36 -0700957 YYERROR;
958 } else {
959 $$ = $1;
960 }
961 }
962 ;
963
Ian Romanick0e795332009-09-10 14:35:33 -0700964addrReg: USED_IDENTIFIER
Ian Romanick770cebb2009-07-20 17:44:36 -0700965 {
966 struct asm_symbol *const s = (struct asm_symbol *)
967 _mesa_symbol_table_find_symbol(state->st, 0, $1);
968
Ian Romanick301a9b72009-11-05 14:15:56 -0800969 free($1);
970
Ian Romanick770cebb2009-07-20 17:44:36 -0700971 if (s == NULL) {
972 yyerror(& @1, state, "invalid array member");
973 YYERROR;
974 } else if (s->type != at_address) {
975 yyerror(& @1, state,
976 "invalid variable for indexed array access");
977 YYERROR;
978 } else {
979 $$ = s;
980 }
981 }
982 ;
983
984addrComponent: MASK1
985 {
986 if ($1.mask != WRITEMASK_X) {
987 yyerror(& @1, state, "invalid address component selector");
988 YYERROR;
989 } else {
990 $$ = $1;
991 }
992 }
993 ;
994
995addrWriteMask: MASK1
996 {
997 if ($1.mask != WRITEMASK_X) {
998 yyerror(& @1, state,
999 "address register write mask must be \".x\"");
1000 YYERROR;
1001 } else {
1002 $$ = $1;
1003 }
1004 }
1005 ;
1006
1007scalarSuffix: MASK1;
1008
1009swizzleSuffix: MASK1
1010 | MASK4
1011 | SWIZZLE
1012 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1013 ;
1014
1015optionalMask: MASK4 | MASK3 | MASK2 | MASK1
1016 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
1017 ;
1018
Ian Romanick81722c52009-09-10 15:04:24 -07001019optionalCcMask: '(' ccTest ')'
1020 {
1021 $$ = $2;
1022 }
1023 | '(' ccTest2 ')'
1024 {
1025 $$ = $2;
1026 }
1027 |
1028 {
1029 $$.CondMask = COND_TR;
1030 $$.CondSwizzle = SWIZZLE_NOOP;
Ian Romanick81722c52009-09-10 15:04:24 -07001031 }
1032 ;
1033
1034ccTest: ccMaskRule swizzleSuffix
1035 {
1036 $$ = $1;
1037 $$.CondSwizzle = $2.swizzle;
1038 }
1039 ;
1040
1041ccTest2: ccMaskRule2 swizzleSuffix
1042 {
1043 $$ = $1;
1044 $$.CondSwizzle = $2.swizzle;
1045 }
1046 ;
1047
1048ccMaskRule: IDENTIFIER
1049 {
1050 const int cond = _mesa_parse_cc($1);
1051 if ((cond == 0) || ($1[2] != '\0')) {
1052 char *const err_str =
1053 make_error_string("invalid condition code \"%s\"", $1);
1054
1055 yyerror(& @1, state, (err_str != NULL)
1056 ? err_str : "invalid condition code");
1057
1058 if (err_str != NULL) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05001059 free(err_str);
Ian Romanick81722c52009-09-10 15:04:24 -07001060 }
1061
1062 YYERROR;
1063 }
1064
1065 $$.CondMask = cond;
1066 $$.CondSwizzle = SWIZZLE_NOOP;
Ian Romanick81722c52009-09-10 15:04:24 -07001067 }
1068 ;
1069
1070ccMaskRule2: USED_IDENTIFIER
1071 {
1072 const int cond = _mesa_parse_cc($1);
1073 if ((cond == 0) || ($1[2] != '\0')) {
1074 char *const err_str =
1075 make_error_string("invalid condition code \"%s\"", $1);
1076
1077 yyerror(& @1, state, (err_str != NULL)
1078 ? err_str : "invalid condition code");
1079
1080 if (err_str != NULL) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05001081 free(err_str);
Ian Romanick81722c52009-09-10 15:04:24 -07001082 }
1083
1084 YYERROR;
1085 }
1086
1087 $$.CondMask = cond;
1088 $$.CondSwizzle = SWIZZLE_NOOP;
Ian Romanick81722c52009-09-10 15:04:24 -07001089 }
1090 ;
1091
Ian Romanick770cebb2009-07-20 17:44:36 -07001092namingStatement: ATTRIB_statement
1093 | PARAM_statement
1094 | TEMP_statement
1095 | ADDRESS_statement
1096 | OUTPUT_statement
1097 | ALIAS_statement
1098 ;
1099
1100ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
1101 {
1102 struct asm_symbol *const s =
1103 declare_variable(state, $2, at_attrib, & @2);
1104
1105 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001106 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001107 YYERROR;
1108 } else {
1109 s->attrib_binding = $4;
Mathias Fröhlichb14c06e2011-12-02 08:08:07 +01001110 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
Ian Romanick770cebb2009-07-20 17:44:36 -07001111
1112 if (!validate_inputs(& @4, state)) {
1113 YYERROR;
1114 }
1115 }
1116 }
1117 ;
1118
1119attribBinding: VERTEX vtxAttribItem
1120 {
1121 $$ = $2;
1122 }
1123 | FRAGMENT fragAttribItem
1124 {
1125 $$ = $2;
1126 }
1127 ;
1128
1129vtxAttribItem: POSITION
1130 {
1131 $$ = VERT_ATTRIB_POS;
1132 }
1133 | WEIGHT vtxOptWeightNum
1134 {
1135 $$ = VERT_ATTRIB_WEIGHT;
1136 }
1137 | NORMAL
1138 {
1139 $$ = VERT_ATTRIB_NORMAL;
1140 }
1141 | COLOR optColorType
1142 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001143 if (!state->ctx->Extensions.EXT_secondary_color) {
1144 yyerror(& @2, state, "GL_EXT_secondary_color not supported");
1145 YYERROR;
1146 }
1147
Ian Romanick770cebb2009-07-20 17:44:36 -07001148 $$ = VERT_ATTRIB_COLOR0 + $2;
1149 }
1150 | FOGCOORD
1151 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001152 if (!state->ctx->Extensions.EXT_fog_coord) {
1153 yyerror(& @1, state, "GL_EXT_fog_coord not supported");
1154 YYERROR;
1155 }
1156
Ian Romanick770cebb2009-07-20 17:44:36 -07001157 $$ = VERT_ATTRIB_FOG;
1158 }
1159 | TEXCOORD optTexCoordUnitNum
1160 {
1161 $$ = VERT_ATTRIB_TEX0 + $2;
1162 }
1163 | MATRIXINDEX '[' vtxWeightNum ']'
1164 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001165 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -07001166 YYERROR;
1167 }
1168 | VTXATTRIB '[' vtxAttribNum ']'
1169 {
1170 $$ = VERT_ATTRIB_GENERIC0 + $3;
1171 }
1172 ;
1173
1174vtxAttribNum: INTEGER
1175 {
Ian Romanickef80c202009-07-22 17:13:08 -07001176 if ((unsigned) $1 >= state->limits->MaxAttribs) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001177 yyerror(& @1, state, "invalid vertex attribute reference");
1178 YYERROR;
1179 }
1180
1181 $$ = $1;
1182 }
1183 ;
1184
1185vtxOptWeightNum: | '[' vtxWeightNum ']';
1186vtxWeightNum: INTEGER;
1187
1188fragAttribItem: POSITION
1189 {
1190 $$ = FRAG_ATTRIB_WPOS;
1191 }
1192 | COLOR optColorType
1193 {
1194 $$ = FRAG_ATTRIB_COL0 + $2;
1195 }
1196 | FOGCOORD
1197 {
1198 $$ = FRAG_ATTRIB_FOGC;
1199 }
1200 | TEXCOORD optTexCoordUnitNum
1201 {
1202 $$ = FRAG_ATTRIB_TEX0 + $2;
1203 }
1204 ;
1205
1206PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1207
1208PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1209 {
1210 struct asm_symbol *const s =
1211 declare_variable(state, $2, at_param, & @2);
1212
1213 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001214 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001215 YYERROR;
1216 } else {
1217 s->param_binding_type = $3.param_binding_type;
1218 s->param_binding_begin = $3.param_binding_begin;
1219 s->param_binding_length = $3.param_binding_length;
Eric Anholt84db95b2010-06-02 13:56:39 -07001220 s->param_binding_swizzle = $3.param_binding_swizzle;
Ian Romanick770cebb2009-07-20 17:44:36 -07001221 s->param_is_array = 0;
1222 }
1223 }
1224 ;
1225
1226PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1227 {
Ian Romanickef80c202009-07-22 17:13:08 -07001228 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001229 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001230 yyerror(& @4, state,
1231 "parameter array size and number of bindings must match");
1232 YYERROR;
1233 } else {
1234 struct asm_symbol *const s =
1235 declare_variable(state, $2, $6.type, & @2);
1236
1237 if (s == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001238 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07001239 YYERROR;
1240 } else {
1241 s->param_binding_type = $6.param_binding_type;
1242 s->param_binding_begin = $6.param_binding_begin;
1243 s->param_binding_length = $6.param_binding_length;
Brian Paul639e7a12009-12-29 09:06:00 -07001244 s->param_binding_swizzle = SWIZZLE_XYZW;
Ian Romanick770cebb2009-07-20 17:44:36 -07001245 s->param_is_array = 1;
1246 }
1247 }
1248 }
1249 ;
1250
1251optArraySize:
1252 {
1253 $$ = 0;
1254 }
1255 | INTEGER
1256 {
Ian Romanickdd245012009-10-20 10:58:14 -07001257 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
Brian Paulf3c92c22011-04-27 09:23:16 -06001258 char msg[100];
1259 _mesa_snprintf(msg, sizeof(msg),
1260 "invalid parameter array size (size=%d max=%u)",
1261 $1, state->limits->MaxParameters);
1262 yyerror(& @1, state, msg);
Ian Romanick770cebb2009-07-20 17:44:36 -07001263 YYERROR;
1264 } else {
1265 $$ = $1;
1266 }
1267 }
1268 ;
1269
1270paramSingleInit: '=' paramSingleItemDecl
1271 {
1272 $$ = $2;
1273 }
1274 ;
1275
1276paramMultipleInit: '=' '{' paramMultInitList '}'
1277 {
1278 $$ = $3;
1279 }
1280 ;
1281
1282paramMultInitList: paramMultipleItem
1283 | paramMultInitList ',' paramMultipleItem
1284 {
1285 $1.param_binding_length += $3.param_binding_length;
1286 $$ = $1;
1287 }
1288 ;
1289
1290paramSingleItemDecl: stateSingleItem
1291 {
1292 memset(& $$, 0, sizeof($$));
1293 $$.param_binding_begin = ~0;
1294 initialize_symbol_from_state(state->prog, & $$, $1);
1295 }
1296 | programSingleItem
1297 {
1298 memset(& $$, 0, sizeof($$));
1299 $$.param_binding_begin = ~0;
1300 initialize_symbol_from_param(state->prog, & $$, $1);
1301 }
1302 | paramConstDecl
1303 {
1304 memset(& $$, 0, sizeof($$));
1305 $$.param_binding_begin = ~0;
Brian Paul7c6ae4c2010-01-05 10:01:20 -07001306 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
Ian Romanick770cebb2009-07-20 17:44:36 -07001307 }
1308 ;
1309
1310paramSingleItemUse: stateSingleItem
1311 {
1312 memset(& $$, 0, sizeof($$));
1313 $$.param_binding_begin = ~0;
1314 initialize_symbol_from_state(state->prog, & $$, $1);
1315 }
1316 | programSingleItem
1317 {
1318 memset(& $$, 0, sizeof($$));
1319 $$.param_binding_begin = ~0;
1320 initialize_symbol_from_param(state->prog, & $$, $1);
1321 }
1322 | paramConstUse
1323 {
1324 memset(& $$, 0, sizeof($$));
1325 $$.param_binding_begin = ~0;
Brian Paul7c6ae4c2010-01-05 10:01:20 -07001326 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
Ian Romanick770cebb2009-07-20 17:44:36 -07001327 }
1328 ;
1329
1330paramMultipleItem: stateMultipleItem
1331 {
1332 memset(& $$, 0, sizeof($$));
1333 $$.param_binding_begin = ~0;
1334 initialize_symbol_from_state(state->prog, & $$, $1);
1335 }
1336 | programMultipleItem
1337 {
1338 memset(& $$, 0, sizeof($$));
1339 $$.param_binding_begin = ~0;
1340 initialize_symbol_from_param(state->prog, & $$, $1);
1341 }
1342 | paramConstDecl
1343 {
1344 memset(& $$, 0, sizeof($$));
1345 $$.param_binding_begin = ~0;
Brian Paul7c6ae4c2010-01-05 10:01:20 -07001346 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
Ian Romanick770cebb2009-07-20 17:44:36 -07001347 }
1348 ;
1349
1350stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1351 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1352 ;
1353
1354stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1355 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1356 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1357 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1358 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1359 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1360 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1361 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1362 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1363 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
Ian Romanick333bb4f2009-07-29 20:41:48 -07001364 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
Ian Romanick770cebb2009-07-20 17:44:36 -07001365 ;
1366
1367stateMaterialItem: MATERIAL optFaceType stateMatProperty
1368 {
1369 memset($$, 0, sizeof($$));
1370 $$[0] = STATE_MATERIAL;
1371 $$[1] = $2;
1372 $$[2] = $3;
1373 }
1374 ;
1375
1376stateMatProperty: ambDiffSpecProperty
1377 {
1378 $$ = $1;
1379 }
1380 | EMISSION
1381 {
1382 $$ = STATE_EMISSION;
1383 }
1384 | SHININESS
1385 {
1386 $$ = STATE_SHININESS;
1387 }
1388 ;
1389
1390stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1391 {
1392 memset($$, 0, sizeof($$));
1393 $$[0] = STATE_LIGHT;
1394 $$[1] = $3;
1395 $$[2] = $5;
1396 }
1397 ;
1398
1399stateLightProperty: ambDiffSpecProperty
1400 {
1401 $$ = $1;
1402 }
1403 | POSITION
1404 {
1405 $$ = STATE_POSITION;
1406 }
1407 | ATTENUATION
1408 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001409 if (!state->ctx->Extensions.EXT_point_parameters) {
1410 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1411 YYERROR;
1412 }
1413
Ian Romanick770cebb2009-07-20 17:44:36 -07001414 $$ = STATE_ATTENUATION;
1415 }
1416 | SPOT stateSpotProperty
1417 {
1418 $$ = $2;
1419 }
1420 | HALF
1421 {
1422 $$ = STATE_HALF_VECTOR;
1423 }
1424 ;
1425
1426stateSpotProperty: DIRECTION
1427 {
1428 $$ = STATE_SPOT_DIRECTION;
1429 }
1430 ;
1431
1432stateLightModelItem: LIGHTMODEL stateLModProperty
1433 {
1434 $$[0] = $2[0];
1435 $$[1] = $2[1];
1436 }
1437 ;
1438
1439stateLModProperty: AMBIENT
1440 {
1441 memset($$, 0, sizeof($$));
1442 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1443 }
1444 | optFaceType SCENECOLOR
1445 {
1446 memset($$, 0, sizeof($$));
1447 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1448 $$[1] = $1;
1449 }
1450 ;
1451
1452stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1453 {
1454 memset($$, 0, sizeof($$));
1455 $$[0] = STATE_LIGHTPROD;
1456 $$[1] = $3;
1457 $$[2] = $5;
1458 $$[3] = $6;
1459 }
1460 ;
1461
1462stateLProdProperty: ambDiffSpecProperty;
1463
1464stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1465 {
1466 memset($$, 0, sizeof($$));
1467 $$[0] = $3;
1468 $$[1] = $2;
1469 }
1470 ;
1471
1472stateTexEnvProperty: COLOR
1473 {
1474 $$ = STATE_TEXENV_COLOR;
1475 }
1476 ;
1477
1478ambDiffSpecProperty: AMBIENT
1479 {
1480 $$ = STATE_AMBIENT;
1481 }
1482 | DIFFUSE
1483 {
1484 $$ = STATE_DIFFUSE;
1485 }
1486 | SPECULAR
1487 {
1488 $$ = STATE_SPECULAR;
1489 }
1490 ;
1491
1492stateLightNumber: INTEGER
1493 {
Ian Romanickef80c202009-07-22 17:13:08 -07001494 if ((unsigned) $1 >= state->MaxLights) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001495 yyerror(& @1, state, "invalid light selector");
1496 YYERROR;
1497 }
1498
1499 $$ = $1;
1500 }
1501 ;
1502
1503stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1504 {
1505 memset($$, 0, sizeof($$));
1506 $$[0] = STATE_TEXGEN;
1507 $$[1] = $2;
1508 $$[2] = $3 + $4;
1509 }
1510 ;
1511
1512stateTexGenType: EYE
1513 {
1514 $$ = STATE_TEXGEN_EYE_S;
1515 }
1516 | OBJECT
1517 {
1518 $$ = STATE_TEXGEN_OBJECT_S;
1519 }
1520 ;
1521stateTexGenCoord: TEXGEN_S
1522 {
1523 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1524 }
1525 | TEXGEN_T
1526 {
1527 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1528 }
1529 | TEXGEN_R
1530 {
1531 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1532 }
1533 | TEXGEN_Q
1534 {
1535 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1536 }
1537 ;
1538
1539stateFogItem: FOG stateFogProperty
1540 {
1541 memset($$, 0, sizeof($$));
1542 $$[0] = $2;
1543 }
1544 ;
1545
1546stateFogProperty: COLOR
1547 {
1548 $$ = STATE_FOG_COLOR;
1549 }
1550 | PARAMS
1551 {
1552 $$ = STATE_FOG_PARAMS;
1553 }
1554 ;
1555
1556stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1557 {
1558 memset($$, 0, sizeof($$));
1559 $$[0] = STATE_CLIPPLANE;
1560 $$[1] = $3;
1561 }
1562 ;
1563
1564stateClipPlaneNum: INTEGER
1565 {
Ian Romanickef80c202009-07-22 17:13:08 -07001566 if ((unsigned) $1 >= state->MaxClipPlanes) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001567 yyerror(& @1, state, "invalid clip plane selector");
1568 YYERROR;
1569 }
1570
1571 $$ = $1;
1572 }
1573 ;
1574
Brian Paul3fedd082009-09-04 09:06:40 -06001575statePointItem: POINT_TOK statePointProperty
Ian Romanick770cebb2009-07-20 17:44:36 -07001576 {
1577 memset($$, 0, sizeof($$));
1578 $$[0] = $2;
1579 }
1580 ;
1581
Brian Paul3fedd082009-09-04 09:06:40 -06001582statePointProperty: SIZE_TOK
Ian Romanick770cebb2009-07-20 17:44:36 -07001583 {
1584 $$ = STATE_POINT_SIZE;
1585 }
1586 | ATTENUATION
1587 {
1588 $$ = STATE_POINT_ATTENUATION;
1589 }
1590 ;
1591
1592stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1593 {
1594 $$[0] = $1[0];
1595 $$[1] = $1[1];
1596 $$[2] = $4;
1597 $$[3] = $4;
1598 $$[4] = $1[2];
1599 }
1600 ;
1601
1602stateMatrixRows: stateMatrixItem optMatrixRows
1603 {
1604 $$[0] = $1[0];
1605 $$[1] = $1[1];
1606 $$[2] = $2[2];
1607 $$[3] = $2[3];
1608 $$[4] = $1[2];
1609 }
1610 ;
1611
1612optMatrixRows:
1613 {
1614 $$[2] = 0;
1615 $$[3] = 3;
1616 }
1617 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1618 {
1619 /* It seems logical that the matrix row range specifier would have
1620 * to specify a range or more than one row (i.e., $5 > $3).
1621 * However, the ARB_vertex_program spec says "a program will fail
1622 * to load if <a> is greater than <b>." This means that $3 == $5
1623 * is valid.
1624 */
1625 if ($3 > $5) {
1626 yyerror(& @3, state, "invalid matrix row range");
1627 YYERROR;
1628 }
1629
1630 $$[2] = $3;
1631 $$[3] = $5;
1632 }
1633 ;
1634
1635stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1636 {
1637 $$[0] = $2[0];
1638 $$[1] = $2[1];
1639 $$[2] = $3;
1640 }
1641 ;
1642
1643stateOptMatModifier:
1644 {
1645 $$ = 0;
1646 }
1647 | stateMatModifier
1648 {
1649 $$ = $1;
1650 }
1651 ;
1652
1653stateMatModifier: INVERSE
1654 {
1655 $$ = STATE_MATRIX_INVERSE;
1656 }
1657 | TRANSPOSE
1658 {
1659 $$ = STATE_MATRIX_TRANSPOSE;
1660 }
1661 | INVTRANS
1662 {
1663 $$ = STATE_MATRIX_INVTRANS;
1664 }
1665 ;
1666
1667stateMatrixRowNum: INTEGER
1668 {
1669 if ($1 > 3) {
1670 yyerror(& @1, state, "invalid matrix row reference");
1671 YYERROR;
1672 }
1673
1674 $$ = $1;
1675 }
1676 ;
1677
1678stateMatrixName: MODELVIEW stateOptModMatNum
1679 {
1680 $$[0] = STATE_MODELVIEW_MATRIX;
1681 $$[1] = $2;
1682 }
1683 | PROJECTION
1684 {
1685 $$[0] = STATE_PROJECTION_MATRIX;
1686 $$[1] = 0;
1687 }
1688 | MVP
1689 {
1690 $$[0] = STATE_MVP_MATRIX;
1691 $$[1] = 0;
1692 }
1693 | TEXTURE optTexCoordUnitNum
1694 {
1695 $$[0] = STATE_TEXTURE_MATRIX;
1696 $$[1] = $2;
1697 }
1698 | PALETTE '[' statePaletteMatNum ']'
1699 {
Ian Romanick054ab5a2009-07-27 14:11:38 -07001700 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
Ian Romanick770cebb2009-07-20 17:44:36 -07001701 YYERROR;
1702 }
1703 | MAT_PROGRAM '[' stateProgramMatNum ']'
1704 {
1705 $$[0] = STATE_PROGRAM_MATRIX;
1706 $$[1] = $3;
1707 }
1708 ;
1709
1710stateOptModMatNum:
1711 {
1712 $$ = 0;
1713 }
Ian Romanick847bc5c2009-09-01 11:10:05 -07001714 | '[' stateModMatNum ']'
Ian Romanick770cebb2009-07-20 17:44:36 -07001715 {
Ian Romanick847bc5c2009-09-01 11:10:05 -07001716 $$ = $2;
Ian Romanick770cebb2009-07-20 17:44:36 -07001717 }
1718 ;
1719stateModMatNum: INTEGER
1720 {
1721 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1722 * zero is valid.
1723 */
1724 if ($1 != 0) {
1725 yyerror(& @1, state, "invalid modelview matrix index");
1726 YYERROR;
1727 }
1728
1729 $$ = $1;
1730 }
1731 ;
1732statePaletteMatNum: INTEGER
1733 {
1734 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1735 * through here. The error will be generated later.
1736 */
1737 $$ = $1;
1738 }
1739 ;
1740stateProgramMatNum: INTEGER
1741 {
Ian Romanickef80c202009-07-22 17:13:08 -07001742 if ((unsigned) $1 >= state->MaxProgramMatrices) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001743 yyerror(& @1, state, "invalid program matrix selector");
1744 YYERROR;
1745 }
1746
1747 $$ = $1;
1748 }
1749 ;
1750
Ian Romanick333bb4f2009-07-29 20:41:48 -07001751stateDepthItem: DEPTH RANGE
1752 {
1753 memset($$, 0, sizeof($$));
1754 $$[0] = STATE_DEPTH_RANGE;
1755 }
1756 ;
Ian Romanick770cebb2009-07-20 17:44:36 -07001757
1758
1759programSingleItem: progEnvParam | progLocalParam;
1760
1761programMultipleItem: progEnvParams | progLocalParams;
1762
1763progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1764 {
1765 memset($$, 0, sizeof($$));
1766 $$[0] = state->state_param_enum;
1767 $$[1] = STATE_ENV;
1768 $$[2] = $4[0];
1769 $$[3] = $4[1];
1770 }
1771 ;
1772
1773progEnvParamNums: progEnvParamNum
1774 {
1775 $$[0] = $1;
1776 $$[1] = $1;
1777 }
1778 | progEnvParamNum DOT_DOT progEnvParamNum
1779 {
1780 $$[0] = $1;
1781 $$[1] = $3;
1782 }
1783 ;
1784
1785progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1786 {
1787 memset($$, 0, sizeof($$));
1788 $$[0] = state->state_param_enum;
1789 $$[1] = STATE_ENV;
1790 $$[2] = $4;
1791 $$[3] = $4;
1792 }
1793 ;
1794
1795progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1796 {
1797 memset($$, 0, sizeof($$));
1798 $$[0] = state->state_param_enum;
1799 $$[1] = STATE_LOCAL;
1800 $$[2] = $4[0];
1801 $$[3] = $4[1];
1802 }
1803
1804progLocalParamNums: progLocalParamNum
1805 {
1806 $$[0] = $1;
1807 $$[1] = $1;
1808 }
1809 | progLocalParamNum DOT_DOT progLocalParamNum
1810 {
1811 $$[0] = $1;
1812 $$[1] = $3;
1813 }
1814 ;
1815
1816progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1817 {
1818 memset($$, 0, sizeof($$));
1819 $$[0] = state->state_param_enum;
1820 $$[1] = STATE_LOCAL;
1821 $$[2] = $4;
1822 $$[3] = $4;
1823 }
1824 ;
1825
1826progEnvParamNum: INTEGER
1827 {
Ian Romanickef80c202009-07-22 17:13:08 -07001828 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001829 yyerror(& @1, state, "invalid environment parameter reference");
1830 YYERROR;
1831 }
1832 $$ = $1;
1833 }
1834 ;
1835
1836progLocalParamNum: INTEGER
1837 {
Ian Romanickef80c202009-07-22 17:13:08 -07001838 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
Ian Romanick770cebb2009-07-20 17:44:36 -07001839 yyerror(& @1, state, "invalid local parameter reference");
1840 YYERROR;
1841 }
1842 $$ = $1;
1843 }
1844 ;
1845
1846
1847
1848paramConstDecl: paramConstScalarDecl | paramConstVector;
1849paramConstUse: paramConstScalarUse | paramConstVector;
1850
1851paramConstScalarDecl: signedFloatConstant
1852 {
Ian Romanick60071092009-07-29 21:07:41 -07001853 $$.count = 4;
Brian Paul32485752011-08-04 16:00:06 -06001854 $$.data[0].f = $1;
1855 $$.data[1].f = $1;
1856 $$.data[2].f = $1;
1857 $$.data[3].f = $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001858 }
1859 ;
1860
1861paramConstScalarUse: REAL
1862 {
Ian Romanickac5551f2009-08-20 18:29:41 -07001863 $$.count = 1;
Brian Paul32485752011-08-04 16:00:06 -06001864 $$.data[0].f = $1;
1865 $$.data[1].f = $1;
1866 $$.data[2].f = $1;
1867 $$.data[3].f = $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001868 }
1869 | INTEGER
1870 {
Ian Romanickac5551f2009-08-20 18:29:41 -07001871 $$.count = 1;
Brian Paul32485752011-08-04 16:00:06 -06001872 $$.data[0].f = (float) $1;
1873 $$.data[1].f = (float) $1;
1874 $$.data[2].f = (float) $1;
1875 $$.data[3].f = (float) $1;
Ian Romanick770cebb2009-07-20 17:44:36 -07001876 }
1877 ;
1878
1879paramConstVector: '{' signedFloatConstant '}'
1880 {
Ian Romanick60071092009-07-29 21:07:41 -07001881 $$.count = 4;
Brian Paul32485752011-08-04 16:00:06 -06001882 $$.data[0].f = $2;
1883 $$.data[1].f = 0.0f;
1884 $$.data[2].f = 0.0f;
1885 $$.data[3].f = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001886 }
1887 | '{' signedFloatConstant ',' signedFloatConstant '}'
1888 {
Ian Romanick60071092009-07-29 21:07:41 -07001889 $$.count = 4;
Brian Paul32485752011-08-04 16:00:06 -06001890 $$.data[0].f = $2;
1891 $$.data[1].f = $4;
1892 $$.data[2].f = 0.0f;
1893 $$.data[3].f = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001894 }
1895 | '{' signedFloatConstant ',' signedFloatConstant ','
1896 signedFloatConstant '}'
1897 {
Ian Romanick60071092009-07-29 21:07:41 -07001898 $$.count = 4;
Brian Paul32485752011-08-04 16:00:06 -06001899 $$.data[0].f = $2;
1900 $$.data[1].f = $4;
1901 $$.data[2].f = $6;
1902 $$.data[3].f = 1.0f;
Ian Romanick770cebb2009-07-20 17:44:36 -07001903 }
1904 | '{' signedFloatConstant ',' signedFloatConstant ','
1905 signedFloatConstant ',' signedFloatConstant '}'
1906 {
1907 $$.count = 4;
Brian Paul32485752011-08-04 16:00:06 -06001908 $$.data[0].f = $2;
1909 $$.data[1].f = $4;
1910 $$.data[2].f = $6;
1911 $$.data[3].f = $8;
Ian Romanick770cebb2009-07-20 17:44:36 -07001912 }
1913 ;
1914
1915signedFloatConstant: optionalSign REAL
1916 {
1917 $$ = ($1) ? -$2 : $2;
1918 }
1919 | optionalSign INTEGER
1920 {
1921 $$ = (float)(($1) ? -$2 : $2);
1922 }
1923 ;
1924
1925optionalSign: '+' { $$ = FALSE; }
1926 | '-' { $$ = TRUE; }
1927 | { $$ = FALSE; }
1928 ;
1929
Ian Romanickd0adebb2009-09-04 17:27:27 -07001930TEMP_statement: optVarSize TEMP { $<integer>$ = $2; } varNameList
1931 ;
1932
Ian Romanick0e795332009-09-10 14:35:33 -07001933optVarSize: string
Ian Romanickd0adebb2009-09-04 17:27:27 -07001934 {
1935 /* NV_fragment_program_option defines the size qualifiers in a
1936 * fairly broken way. "SHORT" or "LONG" can optionally be used
1937 * before TEMP or OUTPUT. However, neither is a reserved word!
1938 * This means that we have to parse it as an identifier, then check
1939 * to make sure it's one of the valid values. *sigh*
1940 *
1941 * In addition, the grammar in the extension spec does *not* allow
1942 * the size specifier to be optional, but all known implementations
1943 * do.
1944 */
1945 if (!state->option.NV_fragment) {
1946 yyerror(& @1, state, "unexpected IDENTIFIER");
1947 YYERROR;
1948 }
1949
1950 if (strcmp("SHORT", $1) == 0) {
1951 } else if (strcmp("LONG", $1) == 0) {
1952 } else {
1953 char *const err_str =
1954 make_error_string("invalid storage size specifier \"%s\"",
1955 $1);
1956
1957 yyerror(& @1, state, (err_str != NULL)
1958 ? err_str : "invalid storage size specifier");
1959
1960 if (err_str != NULL) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05001961 free(err_str);
Ian Romanickd0adebb2009-09-04 17:27:27 -07001962 }
1963
1964 YYERROR;
1965 }
1966 }
1967 |
1968 {
1969 }
Ian Romanick770cebb2009-07-20 17:44:36 -07001970 ;
1971
1972ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1973 ;
1974
1975varNameList: varNameList ',' IDENTIFIER
1976 {
1977 if (!declare_variable(state, $3, $<integer>0, & @3)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001978 free($3);
Ian Romanick770cebb2009-07-20 17:44:36 -07001979 YYERROR;
1980 }
1981 }
1982 | IDENTIFIER
1983 {
1984 if (!declare_variable(state, $1, $<integer>0, & @1)) {
Ian Romanick301a9b72009-11-05 14:15:56 -08001985 free($1);
Ian Romanick770cebb2009-07-20 17:44:36 -07001986 YYERROR;
1987 }
1988 }
1989 ;
1990
Ian Romanickd0adebb2009-09-04 17:27:27 -07001991OUTPUT_statement: optVarSize OUTPUT IDENTIFIER '=' resultBinding
Ian Romanick770cebb2009-07-20 17:44:36 -07001992 {
1993 struct asm_symbol *const s =
Ian Romanickd0adebb2009-09-04 17:27:27 -07001994 declare_variable(state, $3, at_output, & @3);
Ian Romanick770cebb2009-07-20 17:44:36 -07001995
1996 if (s == NULL) {
Ian Romanick4e4c2ee2009-11-06 11:17:00 -08001997 free($3);
Ian Romanick770cebb2009-07-20 17:44:36 -07001998 YYERROR;
1999 } else {
Ian Romanickd0adebb2009-09-04 17:27:27 -07002000 s->output_binding = $5;
Ian Romanick770cebb2009-07-20 17:44:36 -07002001 }
2002 }
2003 ;
2004
2005resultBinding: RESULT POSITION
2006 {
2007 if (state->mode == ARB_vertex) {
2008 $$ = VERT_RESULT_HPOS;
2009 } else {
2010 yyerror(& @2, state, "invalid program result name");
2011 YYERROR;
2012 }
2013 }
2014 | RESULT FOGCOORD
2015 {
2016 if (state->mode == ARB_vertex) {
2017 $$ = VERT_RESULT_FOGC;
2018 } else {
2019 yyerror(& @2, state, "invalid program result name");
2020 YYERROR;
2021 }
2022 }
2023 | RESULT resultColBinding
2024 {
2025 $$ = $2;
2026 }
2027 | RESULT POINTSIZE
2028 {
2029 if (state->mode == ARB_vertex) {
2030 $$ = VERT_RESULT_PSIZ;
2031 } else {
2032 yyerror(& @2, state, "invalid program result name");
2033 YYERROR;
2034 }
2035 }
2036 | RESULT TEXCOORD optTexCoordUnitNum
2037 {
2038 if (state->mode == ARB_vertex) {
2039 $$ = VERT_RESULT_TEX0 + $3;
2040 } else {
2041 yyerror(& @2, state, "invalid program result name");
2042 YYERROR;
2043 }
2044 }
2045 | RESULT DEPTH
2046 {
2047 if (state->mode == ARB_fragment) {
2048 $$ = FRAG_RESULT_DEPTH;
2049 } else {
2050 yyerror(& @2, state, "invalid program result name");
2051 YYERROR;
2052 }
2053 }
2054 ;
2055
2056resultColBinding: COLOR optResultFaceType optResultColorType
2057 {
2058 $$ = $2 + $3;
2059 }
2060 ;
2061
2062optResultFaceType:
2063 {
Eric Anholtd7f4c4e2011-04-20 16:49:19 -07002064 if (state->mode == ARB_vertex) {
2065 $$ = VERT_RESULT_COL0;
2066 } else {
2067 if (state->option.DrawBuffers)
2068 $$ = FRAG_RESULT_DATA0;
2069 else
2070 $$ = FRAG_RESULT_COLOR;
2071 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002072 }
Eric Anholt28cec9e2011-04-13 11:09:55 -07002073 | '[' INTEGER ']'
2074 {
2075 if (state->mode == ARB_vertex) {
2076 yyerror(& @1, state, "invalid program result name");
2077 YYERROR;
2078 } else {
2079 if (!state->option.DrawBuffers) {
2080 /* From the ARB_draw_buffers spec (same text exists
2081 * for ATI_draw_buffers):
2082 *
2083 * If this option is not specified, a fragment
2084 * program that attempts to bind
2085 * "result.color[n]" will fail to load, and only
2086 * "result.color" will be allowed.
2087 */
2088 yyerror(& @1, state,
2089 "result.color[] used without "
2090 "`OPTION ARB_draw_buffers' or "
2091 "`OPTION ATI_draw_buffers'");
2092 YYERROR;
2093 } else if ($2 >= state->MaxDrawBuffers) {
2094 yyerror(& @1, state,
2095 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
2096 YYERROR;
2097 }
2098 $$ = FRAG_RESULT_DATA0 + $2;
2099 }
2100 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002101 | FRONT
2102 {
2103 if (state->mode == ARB_vertex) {
2104 $$ = VERT_RESULT_COL0;
2105 } else {
2106 yyerror(& @1, state, "invalid program result name");
2107 YYERROR;
2108 }
2109 }
2110 | BACK
2111 {
2112 if (state->mode == ARB_vertex) {
2113 $$ = VERT_RESULT_BFC0;
2114 } else {
2115 yyerror(& @1, state, "invalid program result name");
2116 YYERROR;
2117 }
2118 }
2119 ;
2120
2121optResultColorType:
2122 {
2123 $$ = 0;
2124 }
2125 | PRIMARY
2126 {
2127 if (state->mode == ARB_vertex) {
2128 $$ = 0;
2129 } else {
2130 yyerror(& @1, state, "invalid program result name");
2131 YYERROR;
2132 }
2133 }
2134 | SECONDARY
2135 {
2136 if (state->mode == ARB_vertex) {
2137 $$ = 1;
2138 } else {
2139 yyerror(& @1, state, "invalid program result name");
2140 YYERROR;
2141 }
2142 }
2143 ;
2144
2145optFaceType: { $$ = 0; }
2146 | FRONT { $$ = 0; }
2147 | BACK { $$ = 1; }
2148 ;
2149
2150optColorType: { $$ = 0; }
2151 | PRIMARY { $$ = 0; }
2152 | SECONDARY { $$ = 1; }
2153 ;
2154
2155optTexCoordUnitNum: { $$ = 0; }
2156 | '[' texCoordUnitNum ']' { $$ = $2; }
2157 ;
2158
2159optTexImageUnitNum: { $$ = 0; }
2160 | '[' texImageUnitNum ']' { $$ = $2; }
2161 ;
2162
2163optLegacyTexUnitNum: { $$ = 0; }
2164 | '[' legacyTexUnitNum ']' { $$ = $2; }
2165 ;
2166
2167texCoordUnitNum: INTEGER
2168 {
Ian Romanickef80c202009-07-22 17:13:08 -07002169 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002170 yyerror(& @1, state, "invalid texture coordinate unit selector");
2171 YYERROR;
2172 }
2173
2174 $$ = $1;
2175 }
2176 ;
2177
2178texImageUnitNum: INTEGER
2179 {
Ian Romanickef80c202009-07-22 17:13:08 -07002180 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002181 yyerror(& @1, state, "invalid texture image unit selector");
2182 YYERROR;
2183 }
2184
2185 $$ = $1;
2186 }
2187 ;
2188
2189legacyTexUnitNum: INTEGER
2190 {
Ian Romanickef80c202009-07-22 17:13:08 -07002191 if ((unsigned) $1 >= state->MaxTextureUnits) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002192 yyerror(& @1, state, "invalid texture unit selector");
2193 YYERROR;
2194 }
2195
2196 $$ = $1;
2197 }
2198 ;
2199
Ian Romanick0e795332009-09-10 14:35:33 -07002200ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
Ian Romanick770cebb2009-07-20 17:44:36 -07002201 {
2202 struct asm_symbol *exist = (struct asm_symbol *)
2203 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2204 struct asm_symbol *target = (struct asm_symbol *)
2205 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2206
Ian Romanick301a9b72009-11-05 14:15:56 -08002207 free($4);
2208
Ian Romanick770cebb2009-07-20 17:44:36 -07002209 if (exist != NULL) {
Brian Paulfe86f8d2009-11-04 17:26:48 -07002210 char m[1000];
Brian Paul78a0c352010-02-19 12:56:49 -07002211 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
Ian Romanick301a9b72009-11-05 14:15:56 -08002212 free($2);
Brian Paulfe86f8d2009-11-04 17:26:48 -07002213 yyerror(& @2, state, m);
Ian Romanick770cebb2009-07-20 17:44:36 -07002214 YYERROR;
2215 } else if (target == NULL) {
Ian Romanick301a9b72009-11-05 14:15:56 -08002216 free($2);
Ian Romanick770cebb2009-07-20 17:44:36 -07002217 yyerror(& @4, state,
2218 "undefined variable binding in ALIAS statement");
2219 YYERROR;
2220 } else {
Ian Romanick1c7337d2009-11-04 12:03:44 -08002221 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
Ian Romanick770cebb2009-07-20 17:44:36 -07002222 }
2223 }
2224 ;
2225
Ian Romanick0e795332009-09-10 14:35:33 -07002226string: IDENTIFIER
2227 | USED_IDENTIFIER
2228 ;
2229
Ian Romanick770cebb2009-07-20 17:44:36 -07002230%%
2231
Ian Romanick9ea43192009-09-04 16:35:50 -07002232void
2233asm_instruction_set_operands(struct asm_instruction *inst,
2234 const struct prog_dst_register *dst,
2235 const struct asm_src_register *src0,
2236 const struct asm_src_register *src1,
2237 const struct asm_src_register *src2)
2238{
2239 /* In the core ARB extensions only the KIL instruction doesn't have a
2240 * destination register.
2241 */
2242 if (dst == NULL) {
2243 init_dst_reg(& inst->Base.DstReg);
2244 } else {
2245 inst->Base.DstReg = *dst;
2246 }
2247
Ian Romanick81722c52009-09-10 15:04:24 -07002248 /* The only instruction that doesn't have any source registers is the
2249 * condition-code based KIL instruction added by NV_fragment_program_option.
2250 */
2251 if (src0 != NULL) {
2252 inst->Base.SrcReg[0] = src0->Base;
2253 inst->SrcReg[0] = *src0;
2254 } else {
2255 init_src_reg(& inst->SrcReg[0]);
2256 }
Ian Romanick9ea43192009-09-04 16:35:50 -07002257
2258 if (src1 != NULL) {
2259 inst->Base.SrcReg[1] = src1->Base;
2260 inst->SrcReg[1] = *src1;
2261 } else {
2262 init_src_reg(& inst->SrcReg[1]);
2263 }
2264
2265 if (src2 != NULL) {
2266 inst->Base.SrcReg[2] = src2->Base;
2267 inst->SrcReg[2] = *src2;
2268 } else {
2269 init_src_reg(& inst->SrcReg[2]);
2270 }
2271}
2272
2273
Ian Romanick770cebb2009-07-20 17:44:36 -07002274struct asm_instruction *
2275asm_instruction_ctor(gl_inst_opcode op,
2276 const struct prog_dst_register *dst,
2277 const struct asm_src_register *src0,
2278 const struct asm_src_register *src1,
2279 const struct asm_src_register *src2)
2280{
Ian Romanick9ea43192009-09-04 16:35:50 -07002281 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
Ian Romanick770cebb2009-07-20 17:44:36 -07002282
2283 if (inst) {
Ian Romanickef80c202009-07-22 17:13:08 -07002284 _mesa_init_instructions(& inst->Base, 1);
Ian Romanick770cebb2009-07-20 17:44:36 -07002285 inst->Base.Opcode = op;
Ian Romanick17534ab2009-07-30 09:41:35 -07002286
Ian Romanick9ea43192009-09-04 16:35:50 -07002287 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2288 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002289
Ian Romanick9ea43192009-09-04 16:35:50 -07002290 return inst;
2291}
Ian Romanick770cebb2009-07-20 17:44:36 -07002292
Ian Romanick770cebb2009-07-20 17:44:36 -07002293
Ian Romanick9ea43192009-09-04 16:35:50 -07002294struct asm_instruction *
2295asm_instruction_copy_ctor(const struct prog_instruction *base,
2296 const struct prog_dst_register *dst,
2297 const struct asm_src_register *src0,
2298 const struct asm_src_register *src1,
2299 const struct asm_src_register *src2)
2300{
2301 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2302
2303 if (inst) {
2304 _mesa_init_instructions(& inst->Base, 1);
2305 inst->Base.Opcode = base->Opcode;
Ian Romanickd0adebb2009-09-04 17:27:27 -07002306 inst->Base.CondUpdate = base->CondUpdate;
2307 inst->Base.CondDst = base->CondDst;
Ian Romanick9ea43192009-09-04 16:35:50 -07002308 inst->Base.SaturateMode = base->SaturateMode;
Ian Romanickd0adebb2009-09-04 17:27:27 -07002309 inst->Base.Precision = base->Precision;
Ian Romanick9ea43192009-09-04 16:35:50 -07002310
2311 asm_instruction_set_operands(inst, dst, src0, src1, src2);
Ian Romanick770cebb2009-07-20 17:44:36 -07002312 }
2313
2314 return inst;
2315}
2316
2317
2318void
2319init_dst_reg(struct prog_dst_register *r)
2320{
2321 memset(r, 0, sizeof(*r));
2322 r->File = PROGRAM_UNDEFINED;
2323 r->WriteMask = WRITEMASK_XYZW;
2324 r->CondMask = COND_TR;
2325 r->CondSwizzle = SWIZZLE_NOOP;
2326}
2327
2328
Brian Pauldf5615d2009-10-30 19:05:50 -06002329/** Like init_dst_reg() but set the File and Index fields. */
2330void
2331set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2332{
2333 const GLint maxIndex = 1 << INST_INDEX_BITS;
2334 const GLint minIndex = 0;
2335 ASSERT(index >= minIndex);
Vinson Lee88bd3232010-01-01 15:12:58 -08002336 (void) minIndex;
Brian Pauldf5615d2009-10-30 19:05:50 -06002337 ASSERT(index <= maxIndex);
Vinson Lee88bd3232010-01-01 15:12:58 -08002338 (void) maxIndex;
Brian Pauldf5615d2009-10-30 19:05:50 -06002339 ASSERT(file == PROGRAM_TEMPORARY ||
2340 file == PROGRAM_ADDRESS ||
2341 file == PROGRAM_OUTPUT);
2342 memset(r, 0, sizeof(*r));
2343 r->File = file;
2344 r->Index = index;
2345 r->WriteMask = WRITEMASK_XYZW;
2346 r->CondMask = COND_TR;
2347 r->CondSwizzle = SWIZZLE_NOOP;
2348}
2349
2350
Ian Romanick770cebb2009-07-20 17:44:36 -07002351void
2352init_src_reg(struct asm_src_register *r)
2353{
2354 memset(r, 0, sizeof(*r));
2355 r->Base.File = PROGRAM_UNDEFINED;
2356 r->Base.Swizzle = SWIZZLE_NOOP;
2357 r->Symbol = NULL;
2358}
2359
2360
Brian Paul639e7a12009-12-29 09:06:00 -07002361/** Like init_src_reg() but set the File and Index fields.
2362 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2363 */
Brian Pauldf5615d2009-10-30 19:05:50 -06002364void
2365set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2366{
Brian Paul639e7a12009-12-29 09:06:00 -07002367 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2368}
2369
2370
2371void
2372set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2373 GLuint swizzle)
2374{
Brian Pauldf5615d2009-10-30 19:05:50 -06002375 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2376 const GLint minIndex = -(1 << INST_INDEX_BITS);
Brian Paul639e7a12009-12-29 09:06:00 -07002377 ASSERT(file < PROGRAM_FILE_MAX);
Brian Pauldf5615d2009-10-30 19:05:50 -06002378 ASSERT(index >= minIndex);
Vinson Lee88bd3232010-01-01 15:12:58 -08002379 (void) minIndex;
Brian Pauldf5615d2009-10-30 19:05:50 -06002380 ASSERT(index <= maxIndex);
Vinson Lee88bd3232010-01-01 15:12:58 -08002381 (void) maxIndex;
Brian Pauldf5615d2009-10-30 19:05:50 -06002382 memset(r, 0, sizeof(*r));
2383 r->Base.File = file;
2384 r->Base.Index = index;
Brian Paul639e7a12009-12-29 09:06:00 -07002385 r->Base.Swizzle = swizzle;
Brian Pauldf5615d2009-10-30 19:05:50 -06002386 r->Symbol = NULL;
2387}
2388
2389
Ian Romanick770cebb2009-07-20 17:44:36 -07002390/**
2391 * Validate the set of inputs used by a program
2392 *
2393 * Validates that legal sets of inputs are used by the program. In this case
2394 * "used" included both reading the input or binding the input to a name using
2395 * the \c ATTRIB command.
2396 *
2397 * \return
2398 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2399 */
2400int
2401validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2402{
Mathias Fröhlichb14c06e2011-12-02 08:08:07 +01002403 const GLbitfield64 inputs = state->prog->InputsRead | state->InputsBound;
Ian Romanick770cebb2009-07-20 17:44:36 -07002404
Mathias Fröhlichb14c06e2011-12-02 08:08:07 +01002405 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002406 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2407 return 0;
2408 }
2409
2410 return 1;
2411}
2412
2413
2414struct asm_symbol *
2415declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2416 struct YYLTYPE *locp)
2417{
2418 struct asm_symbol *s = NULL;
2419 struct asm_symbol *exist = (struct asm_symbol *)
2420 _mesa_symbol_table_find_symbol(state->st, 0, name);
2421
2422
2423 if (exist != NULL) {
2424 yyerror(locp, state, "redeclared identifier");
2425 } else {
Ian Romanick1c7337d2009-11-04 12:03:44 -08002426 s = calloc(1, sizeof(struct asm_symbol));
2427 s->name = name;
Ian Romanick770cebb2009-07-20 17:44:36 -07002428 s->type = t;
2429
2430 switch (t) {
2431 case at_temp:
2432 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2433 yyerror(locp, state, "too many temporaries declared");
2434 free(s);
2435 return NULL;
2436 }
2437
2438 s->temp_binding = state->prog->NumTemporaries;
2439 state->prog->NumTemporaries++;
2440 break;
2441
2442 case at_address:
2443 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2444 yyerror(locp, state, "too many address registers declared");
2445 free(s);
2446 return NULL;
2447 }
2448
2449 /* FINISHME: Add support for multiple address registers.
2450 */
2451 state->prog->NumAddressRegs++;
2452 break;
2453
2454 default:
2455 break;
2456 }
2457
2458 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
Ian Romanick94b45562009-07-27 12:21:26 -07002459 s->next = state->sym;
2460 state->sym = s;
Ian Romanick770cebb2009-07-20 17:44:36 -07002461 }
2462
2463 return s;
2464}
2465
2466
2467int add_state_reference(struct gl_program_parameter_list *param_list,
2468 const gl_state_index tokens[STATE_LENGTH])
2469{
2470 const GLuint size = 4; /* XXX fix */
2471 char *name;
2472 GLint index;
2473
2474 name = _mesa_program_state_string(tokens);
2475 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
Kenneth Graunkef7cfe3f2012-10-10 17:02:45 -07002476 size, GL_NONE, NULL, tokens);
Ian Romanick770cebb2009-07-20 17:44:36 -07002477 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2478
2479 /* free name string here since we duplicated it in add_parameter() */
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002480 free(name);
Ian Romanick770cebb2009-07-20 17:44:36 -07002481
2482 return index;
2483}
2484
2485
2486int
2487initialize_symbol_from_state(struct gl_program *prog,
2488 struct asm_symbol *param_var,
2489 const gl_state_index tokens[STATE_LENGTH])
2490{
2491 int idx = -1;
2492 gl_state_index state_tokens[STATE_LENGTH];
2493
2494
2495 memcpy(state_tokens, tokens, sizeof(state_tokens));
2496
2497 param_var->type = at_param;
Ian Romanick28b13032009-07-22 16:03:32 -07002498 param_var->param_binding_type = PROGRAM_STATE_VAR;
Ian Romanick770cebb2009-07-20 17:44:36 -07002499
2500 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2501 * unroll it and call add_state_reference() for each row
2502 */
2503 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2504 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2505 state_tokens[0] == STATE_MVP_MATRIX ||
2506 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2507 state_tokens[0] == STATE_PROGRAM_MATRIX)
2508 && (state_tokens[2] != state_tokens[3])) {
2509 int row;
2510 const int first_row = state_tokens[2];
2511 const int last_row = state_tokens[3];
2512
2513 for (row = first_row; row <= last_row; row++) {
2514 state_tokens[2] = state_tokens[3] = row;
2515
2516 idx = add_state_reference(prog->Parameters, state_tokens);
Brian Paul639e7a12009-12-29 09:06:00 -07002517 if (param_var->param_binding_begin == ~0U) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002518 param_var->param_binding_begin = idx;
Brian Paul639e7a12009-12-29 09:06:00 -07002519 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2520 }
2521
Ian Romanick770cebb2009-07-20 17:44:36 -07002522 param_var->param_binding_length++;
2523 }
2524 }
2525 else {
2526 idx = add_state_reference(prog->Parameters, state_tokens);
Brian Paul639e7a12009-12-29 09:06:00 -07002527 if (param_var->param_binding_begin == ~0U) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002528 param_var->param_binding_begin = idx;
Brian Paul639e7a12009-12-29 09:06:00 -07002529 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2530 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002531 param_var->param_binding_length++;
2532 }
2533
2534 return idx;
2535}
2536
2537
2538int
2539initialize_symbol_from_param(struct gl_program *prog,
2540 struct asm_symbol *param_var,
2541 const gl_state_index tokens[STATE_LENGTH])
2542{
2543 int idx = -1;
2544 gl_state_index state_tokens[STATE_LENGTH];
2545
2546
2547 memcpy(state_tokens, tokens, sizeof(state_tokens));
2548
2549 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2550 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2551 assert((state_tokens[1] == STATE_ENV)
2552 || (state_tokens[1] == STATE_LOCAL));
2553
Brian Paulf0f68b12010-01-05 10:53:44 -07002554 /*
2555 * The param type is STATE_VAR. The program parameter entry will
2556 * effectively be a pointer into the LOCAL or ENV parameter array.
2557 */
Ian Romanick770cebb2009-07-20 17:44:36 -07002558 param_var->type = at_param;
Brian Paulf0f68b12010-01-05 10:53:44 -07002559 param_var->param_binding_type = PROGRAM_STATE_VAR;
Ian Romanick770cebb2009-07-20 17:44:36 -07002560
2561 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2562 * we need to unroll it and call add_state_reference() for each row
2563 */
2564 if (state_tokens[2] != state_tokens[3]) {
2565 int row;
2566 const int first_row = state_tokens[2];
2567 const int last_row = state_tokens[3];
2568
2569 for (row = first_row; row <= last_row; row++) {
2570 state_tokens[2] = state_tokens[3] = row;
2571
2572 idx = add_state_reference(prog->Parameters, state_tokens);
Brian Paul639e7a12009-12-29 09:06:00 -07002573 if (param_var->param_binding_begin == ~0U) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002574 param_var->param_binding_begin = idx;
Brian Paul639e7a12009-12-29 09:06:00 -07002575 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2576 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002577 param_var->param_binding_length++;
2578 }
2579 }
2580 else {
2581 idx = add_state_reference(prog->Parameters, state_tokens);
Brian Paul639e7a12009-12-29 09:06:00 -07002582 if (param_var->param_binding_begin == ~0U) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002583 param_var->param_binding_begin = idx;
Brian Paul639e7a12009-12-29 09:06:00 -07002584 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2585 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002586 param_var->param_binding_length++;
2587 }
2588
2589 return idx;
2590}
2591
2592
Brian Paul639e7a12009-12-29 09:06:00 -07002593/**
2594 * Put a float/vector constant/literal into the parameter list.
2595 * \param param_var returns info about the parameter/constant's location,
2596 * binding, type, etc.
2597 * \param vec the vector/constant to add
Brian Paul7c6ae4c2010-01-05 10:01:20 -07002598 * \param allowSwizzle if true, try to consolidate constants which only differ
2599 * by a swizzle. We don't want to do this when building
2600 * arrays of constants that may be indexed indirectly.
Brian Paul639e7a12009-12-29 09:06:00 -07002601 * \return index of the constant in the parameter list.
2602 */
Ian Romanick770cebb2009-07-20 17:44:36 -07002603int
2604initialize_symbol_from_const(struct gl_program *prog,
2605 struct asm_symbol *param_var,
Brian Paul7c6ae4c2010-01-05 10:01:20 -07002606 const struct asm_vector *vec,
2607 GLboolean allowSwizzle)
Ian Romanick770cebb2009-07-20 17:44:36 -07002608{
Brian Paul639e7a12009-12-29 09:06:00 -07002609 unsigned swizzle;
2610 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
Brian Paul7c6ae4c2010-01-05 10:01:20 -07002611 vec->data, vec->count,
2612 allowSwizzle ? &swizzle : NULL);
Ian Romanick770cebb2009-07-20 17:44:36 -07002613
2614 param_var->type = at_param;
Ian Romanick28b13032009-07-22 16:03:32 -07002615 param_var->param_binding_type = PROGRAM_CONSTANT;
Ian Romanick770cebb2009-07-20 17:44:36 -07002616
Brian Paul639e7a12009-12-29 09:06:00 -07002617 if (param_var->param_binding_begin == ~0U) {
Ian Romanick770cebb2009-07-20 17:44:36 -07002618 param_var->param_binding_begin = idx;
Brian Paul7c6ae4c2010-01-05 10:01:20 -07002619 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
Brian Paul639e7a12009-12-29 09:06:00 -07002620 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002621 param_var->param_binding_length++;
2622
2623 return idx;
2624}
2625
2626
Ian Romanick44843c72009-07-22 15:06:49 -07002627char *
2628make_error_string(const char *fmt, ...)
2629{
2630 int length;
2631 char *str;
2632 va_list args;
2633
Ian Romanick44843c72009-07-22 15:06:49 -07002634
2635 /* Call vsnprintf once to determine how large the final string is. Call it
José Fonseca6aae0622010-04-17 14:25:05 +02002636 * again to do the actual formatting. from the vsnprintf manual page:
Ian Romanick44843c72009-07-22 15:06:49 -07002637 *
2638 * Upon successful return, these functions return the number of
2639 * characters printed (not including the trailing '\0' used to end
2640 * output to strings).
2641 */
José Fonsecacca8abe2010-04-24 20:31:30 +01002642 va_start(args, fmt);
Ian Romanick44843c72009-07-22 15:06:49 -07002643 length = 1 + vsnprintf(NULL, 0, fmt, args);
José Fonsecacca8abe2010-04-24 20:31:30 +01002644 va_end(args);
Ian Romanick44843c72009-07-22 15:06:49 -07002645
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002646 str = malloc(length);
Ian Romanick44843c72009-07-22 15:06:49 -07002647 if (str) {
José Fonsecacca8abe2010-04-24 20:31:30 +01002648 va_start(args, fmt);
Brian Paul150cce62010-02-19 13:44:12 -07002649 vsnprintf(str, length, fmt, args);
José Fonsecacca8abe2010-04-24 20:31:30 +01002650 va_end(args);
Ian Romanick44843c72009-07-22 15:06:49 -07002651 }
2652
Ian Romanick44843c72009-07-22 15:06:49 -07002653 return str;
2654}
2655
2656
Ian Romanick770cebb2009-07-20 17:44:36 -07002657void
2658yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2659{
Ian Romanick44843c72009-07-22 15:06:49 -07002660 char *err_str;
Ian Romanick770cebb2009-07-20 17:44:36 -07002661
Ian Romanick44843c72009-07-22 15:06:49 -07002662
2663 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2664 if (err_str) {
Vinson Lee3888c382010-09-02 16:12:58 -07002665 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002666 free(err_str);
Ian Romanick44843c72009-07-22 15:06:49 -07002667 }
2668
2669 err_str = make_error_string("line %u, char %u: error: %s\n",
2670 locp->first_line, locp->first_column, s);
2671 _mesa_set_program_error(state->ctx, locp->position, err_str);
2672
2673 if (err_str) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002674 free(err_str);
Ian Romanick44843c72009-07-22 15:06:49 -07002675 }
Ian Romanick770cebb2009-07-20 17:44:36 -07002676}
2677
Ian Romanick44843c72009-07-22 15:06:49 -07002678
Ian Romanick770cebb2009-07-20 17:44:36 -07002679GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002680_mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
Ian Romanick770cebb2009-07-20 17:44:36 -07002681 GLsizei len, struct asm_parser_state *state)
2682{
Ian Romanick770cebb2009-07-20 17:44:36 -07002683 struct asm_instruction *inst;
2684 unsigned i;
2685 GLubyte *strz;
Ian Romanick94b45562009-07-27 12:21:26 -07002686 GLboolean result = GL_FALSE;
2687 void *temp;
2688 struct asm_symbol *sym;
Ian Romanick770cebb2009-07-20 17:44:36 -07002689
Ian Romanick44843c72009-07-22 15:06:49 -07002690 state->ctx = ctx;
Ian Romanick770cebb2009-07-20 17:44:36 -07002691 state->prog->Target = target;
2692 state->prog->Parameters = _mesa_new_parameter_list();
2693
2694 /* Make a copy of the program string and force it to be NUL-terminated.
2695 */
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002696 strz = (GLubyte *) malloc(len + 1);
Ian Romanick770cebb2009-07-20 17:44:36 -07002697 if (strz == NULL) {
2698 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2699 return GL_FALSE;
2700 }
Kenneth Graunkec7ac48622010-02-18 23:50:59 -08002701 memcpy (strz, str, len);
Ian Romanick770cebb2009-07-20 17:44:36 -07002702 strz[len] = '\0';
2703
2704 state->prog->String = strz;
2705
2706 state->st = _mesa_symbol_table_ctor();
2707
Ian Romanick48183ca2009-07-29 20:51:38 -07002708 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2709 ? & ctx->Const.VertexProgram
2710 : & ctx->Const.FragmentProgram;
Ian Romanick770cebb2009-07-20 17:44:36 -07002711
Brian Paul4cf27602009-08-24 11:05:11 -06002712 state->MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
2713 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2714 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2715 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2716 state->MaxLights = ctx->Const.MaxLights;
2717 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
Eric Anholt28cec9e2011-04-13 11:09:55 -07002718 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
Ian Romanick770cebb2009-07-20 17:44:36 -07002719
2720 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2721 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2722
2723 _mesa_set_program_error(ctx, -1, NULL);
2724
2725 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2726 yyparse(state);
2727 _mesa_program_lexer_dtor(state->scanner);
2728
2729
Ian Romanick44843c72009-07-22 15:06:49 -07002730 if (ctx->Program.ErrorPos != -1) {
Ian Romanick94b45562009-07-27 12:21:26 -07002731 goto error;
Ian Romanick44843c72009-07-22 15:06:49 -07002732 }
2733
Ian Romanick770cebb2009-07-20 17:44:36 -07002734 if (! _mesa_layout_parameters(state)) {
Ian Romanick44843c72009-07-22 15:06:49 -07002735 struct YYLTYPE loc;
2736
2737 loc.first_line = 0;
2738 loc.first_column = 0;
2739 loc.position = len;
2740
2741 yyerror(& loc, state, "invalid PARAM usage");
Ian Romanick94b45562009-07-27 12:21:26 -07002742 goto error;
Ian Romanick770cebb2009-07-20 17:44:36 -07002743 }
2744
2745
2746
2747 /* Add one instruction to store the "END" instruction.
2748 */
2749 state->prog->Instructions =
2750 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2751 inst = state->inst_head;
2752 for (i = 0; i < state->prog->NumInstructions; i++) {
2753 struct asm_instruction *const temp = inst->next;
2754
2755 state->prog->Instructions[i] = inst->Base;
Ian Romanick770cebb2009-07-20 17:44:36 -07002756 inst = temp;
2757 }
2758
2759 /* Finally, tag on an OPCODE_END instruction */
2760 {
2761 const GLuint numInst = state->prog->NumInstructions;
2762 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2763 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2764 }
2765 state->prog->NumInstructions++;
2766
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002767 state->prog->NumParameters = state->prog->Parameters->NumParameters;
Mathias Fröhlichdca6a282011-10-31 18:32:59 +01002768 state->prog->NumAttributes = _mesa_bitcount_64(state->prog->InputsRead);
Ian Romanickc2ee82d2009-07-22 15:27:31 -07002769
Ian Romanick770cebb2009-07-20 17:44:36 -07002770 /*
2771 * Initialize native counts to logical counts. The device driver may
2772 * change them if program is translated into a hardware program.
2773 */
2774 state->prog->NumNativeInstructions = state->prog->NumInstructions;
2775 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2776 state->prog->NumNativeParameters = state->prog->NumParameters;
2777 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2778 state->prog->NumNativeAddressRegs = state->prog->NumAddressRegs;
2779
Ian Romanick94b45562009-07-27 12:21:26 -07002780 result = GL_TRUE;
2781
2782error:
2783 for (inst = state->inst_head; inst != NULL; inst = temp) {
2784 temp = inst->next;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002785 free(inst);
Ian Romanick94b45562009-07-27 12:21:26 -07002786 }
2787
2788 state->inst_head = NULL;
2789 state->inst_tail = NULL;
2790
2791 for (sym = state->sym; sym != NULL; sym = temp) {
2792 temp = sym->next;
2793
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -05002794 free((void *) sym->name);
2795 free(sym);
Ian Romanick94b45562009-07-27 12:21:26 -07002796 }
2797 state->sym = NULL;
2798
2799 _mesa_symbol_table_dtor(state->st);
2800 state->st = NULL;
2801
2802 return result;
Ian Romanick770cebb2009-07-20 17:44:36 -07002803}