4 * Copyright 2008 Stefan Dösinger
5 * Copyright 2012 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/debug.h"
27 #include "d3dcompiler_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL
(hlsl_parser
);
33 struct hlsl_parse_ctx hlsl_ctx
;
36 static void set_location
(struct source_location
*loc
, const struct YYLTYPE *l
);
38 void hlsl_message
(const char *fmt
, ...
)
43 compilation_message
(&hlsl_ctx.messages
, fmt
, args
);
47 static const char *hlsl_get_error_level_name
(enum hlsl_error_level level
)
58 void hlsl_report_message
(const char *filename
, DWORD line
, DWORD column
,
59 enum hlsl_error_level level
, const char *fmt
, ...
)
68 rc
= vsnprintf
(string, size
, fmt
, args
);
71 if
(rc
>= 0 && rc
< size
)
77 size
= size ? size
* 2 : 32;
80 string = d3dcompiler_alloc
(size
);
82 string = d3dcompiler_realloc
(string, size
);
85 ERR
("Error reallocating memory for a string.\n");
90 hlsl_message
("%s:%u:%u: %s: %s\n", filename
, line
, column
, hlsl_get_error_level_name
(level
), string);
91 d3dcompiler_free
(string);
93 if
(level
== HLSL_LEVEL_ERROR
)
94 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
95 else if
(level
== HLSL_LEVEL_WARNING
)
96 set_parse_status
(&hlsl_ctx.status
, PARSE_WARN
);
99 static void hlsl_error
(const char *s
)
101 hlsl_report_message
(hlsl_ctx.source_file
, hlsl_ctx.line_no
, hlsl_ctx.column
, HLSL_LEVEL_ERROR
, "%s", s
);
104 static void debug_dump_decl
(struct hlsl_type
*type
, DWORD modifiers
, const char *declname
, unsigned int line_no
)
106 TRACE
("Line %u: ", line_no
);
108 TRACE
("%s ", debug_modifiers
(modifiers
));
109 TRACE
("%s %s;\n", debug_hlsl_type
(type
), declname
);
112 static void check_invalid_matrix_modifiers
(DWORD modifiers
, struct source_location
*loc
)
114 if
(modifiers
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
))
116 hlsl_report_message
(loc
->file
, loc
->line
, loc
->col
, HLSL_LEVEL_ERROR
,
117 "'row_major' or 'column_major' modifiers are only allowed for matrices");
121 static BOOL declare_variable
(struct hlsl_ir_var
*decl
, BOOL local
)
125 TRACE
("Declaring variable %s.\n", decl
->name
);
126 if
(decl
->node.data_type
->type
== HLSL_CLASS_MATRIX
)
128 if
(!(decl
->modifiers
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
)))
130 decl
->modifiers |
= hlsl_ctx.matrix_majority
== HLSL_ROW_MAJOR
131 ? HLSL_MODIFIER_ROW_MAJOR
: HLSL_MODIFIER_COLUMN_MAJOR
;
135 check_invalid_matrix_modifiers
(decl
->modifiers
, &decl
->node.loc
);
139 DWORD invalid
= decl
->modifiers
& (HLSL_STORAGE_EXTERN | HLSL_STORAGE_SHARED
140 | HLSL_STORAGE_GROUPSHARED | HLSL_STORAGE_UNIFORM
);
143 hlsl_report_message
(decl
->node.loc.file
, decl
->node.loc.line
, decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
144 "modifier '%s' invalid for local variables", debug_modifiers
(invalid
));
148 hlsl_report_message
(decl
->node.loc.file
, decl
->node.loc.line
, decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
149 "semantics are not allowed on local variables");
155 if
(find_function
(decl
->name
))
157 hlsl_report_message
(decl
->node.loc.file
, decl
->node.loc.line
, decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
158 "redefinition of '%s'", decl
->name
);
162 ret
= add_declaration
(hlsl_ctx.cur_scope
, decl
, local
);
165 struct hlsl_ir_var
*old
= get_variable
(hlsl_ctx.cur_scope
, decl
->name
);
167 hlsl_report_message
(decl
->node.loc.file
, decl
->node.loc.line
, decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
168 "\"%s\" already declared", decl
->name
);
169 hlsl_report_message
(old
->node.loc.file
, old
->node.loc.line
, old
->node.loc.col
, HLSL_LEVEL_NOTE
,
170 "\"%s\" was previously declared here", old
->name
);
176 static DWORD add_modifier
(DWORD modifiers
, DWORD mod
, const struct YYLTYPE *loc
);
178 BOOL add_type_to_scope
(struct hlsl_scope
*scope
, struct hlsl_type
*def
)
180 if
(get_type
(scope
, def
->name
, FALSE
))
183 list_add_tail
(&scope
->types
, &def
->scope_entry
);
187 static void declare_predefined_types
(struct hlsl_scope
*scope
)
189 struct hlsl_type
*type
;
190 unsigned int x
, y
, bt
;
191 static const char *names
[] =
202 for
(bt
= 0; bt
<= HLSL_TYPE_LAST_SCALAR
; ++bt
)
204 for
(y
= 1; y
<= 4; ++y
)
206 for
(x
= 1; x
<= 4; ++x
)
208 sprintf
(name
, "%s%ux%u", names
[bt
], x
, y
);
209 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_MATRIX
, bt
, x
, y
);
210 add_type_to_scope
(scope
, type
);
214 sprintf
(name
, "%s%u", names
[bt
], x
);
215 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_VECTOR
, bt
, x
, y
);
216 add_type_to_scope
(scope
, type
);
220 sprintf
(name
, "%s", names
[bt
]);
221 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_SCALAR
, bt
, x
, y
);
222 add_type_to_scope
(scope
, type
);
229 /* DX8 effects predefined types */
230 type
= new_hlsl_type
(d3dcompiler_strdup
("DWORD"), HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
231 add_type_to_scope
(scope
, type
);
232 type
= new_hlsl_type
(d3dcompiler_strdup
("FLOAT"), HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
233 add_type_to_scope
(scope
, type
);
234 type
= new_hlsl_type
(d3dcompiler_strdup
("VECTOR"), HLSL_CLASS_VECTOR
, HLSL_TYPE_FLOAT
, 4, 1);
235 add_type_to_scope
(scope
, type
);
236 type
= new_hlsl_type
(d3dcompiler_strdup
("MATRIX"), HLSL_CLASS_MATRIX
, HLSL_TYPE_FLOAT
, 4, 4);
237 add_type_to_scope
(scope
, type
);
238 type
= new_hlsl_type
(d3dcompiler_strdup
("STRING"), HLSL_CLASS_OBJECT
, HLSL_TYPE_STRING
, 1, 1);
239 add_type_to_scope
(scope
, type
);
240 type
= new_hlsl_type
(d3dcompiler_strdup
("TEXTURE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_TEXTURE
, 1, 1);
241 add_type_to_scope
(scope
, type
);
242 type
= new_hlsl_type
(d3dcompiler_strdup
("PIXELSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_PIXELSHADER
, 1, 1);
243 add_type_to_scope
(scope
, type
);
244 type
= new_hlsl_type
(d3dcompiler_strdup
("VERTEXSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VERTEXSHADER
, 1, 1);
245 add_type_to_scope
(scope
, type
);
248 static unsigned int components_count_expr_list
(struct list
*list
)
250 struct hlsl_ir_node
*node
;
251 unsigned int count
= 0;
253 LIST_FOR_EACH_ENTRY
(node
, list
, struct hlsl_ir_node
, entry
)
255 count
+= components_count_type
(node
->data_type
);
260 static struct hlsl_ir_swizzle
*new_swizzle
(DWORD s
, unsigned int components
,
261 struct hlsl_ir_node
*val
, struct source_location
*loc
)
263 struct hlsl_ir_swizzle
*swizzle
= d3dcompiler_alloc
(sizeof
(*swizzle
));
267 swizzle
->node.type
= HLSL_IR_SWIZZLE
;
268 swizzle
->node.loc
= *loc
;
269 swizzle
->node.data_type
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, val
->data_type
->base_type
, components
, 1);
271 swizzle
->swizzle
= s
;
275 static struct hlsl_ir_swizzle
*get_swizzle
(struct hlsl_ir_node
*value
, const char *swizzle
,
276 struct source_location
*loc
)
278 unsigned int len
= strlen
(swizzle
), component
= 0;
279 unsigned int i
, set
, swiz
= 0;
282 if
(value
->data_type
->type
== HLSL_CLASS_MATRIX
)
286 unsigned int inc
, x
, y
;
288 if
(len
< 3 || swizzle
[0] != '_')
290 m_swizzle
= swizzle
[1] == 'm';
291 inc
= m_swizzle ?
4 : 3;
293 if
(len % inc || len
> inc
* 4)
296 for
(i
= 0; i
< len
; i
+= inc
)
298 if
(swizzle
[i
] != '_')
302 if
(swizzle
[i
+ 1] != 'm')
304 x
= swizzle
[i
+ 2] - '0';
305 y
= swizzle
[i
+ 3] - '0';
309 x
= swizzle
[i
+ 1] - '1';
310 y
= swizzle
[i
+ 2] - '1';
313 if
(x
>= value
->data_type
->dimx || y
>= value
->data_type
->dimy
)
315 swiz |
= (y
<< 4 | x
) << component
* 8;
318 return new_swizzle
(swiz
, component
, value
, loc
);
325 for
(set
= 0; set
< 2; ++set
)
329 for
(i
= 0; i
< len
; ++i
)
331 char c
[2][4] = {{'x', 'y', 'z', 'w'}, {'r', 'g', 'b', 'a'}};
334 for
(s
= 0; s
< 4; ++s
)
336 if
(swizzle
[i
] == c
[set
][s
])
345 if
(s
>= value
->data_type
->dimx
)
347 swiz |
= s
<< component
* 2;
351 return new_swizzle
(swiz
, component
, value
, loc
);
364 struct hlsl_type
*type
;
370 struct hlsl_ir_var
*var
;
371 struct hlsl_ir_node
*instr
;
373 struct hlsl_ir_function_decl
*function
;
374 struct parse_parameter parameter
;
375 struct parse_variable_def
*variable_def
;
376 enum parse_unary_op unary_op
;
377 enum parse_assign_op assign_op
;
384 %token KW_COLUMN_MAJOR
388 %token KW_DEPTHSTENCILSTATE
389 %token KW_DEPTHSTENCILVIEW
397 %token KW_GEOMETRYSHADER
398 %token KW_GROUPSHARED
405 %token KW_NOINTERPOLATION
408 %token KW_PIXELSHADER
410 %token KW_RASTERIZERSTATE
411 %token KW_RENDERTARGETVIEW
419 %token KW_SAMPLERCUBE
420 %token KW_SAMPLER_STATE
421 %token KW_SAMPLERCOMPARISONSTATE
424 %token KW_STATEBLOCK_STATE
431 %token KW_TECHNIQUE10
434 %token KW_TEXTURE1DARRAY
436 %token KW_TEXTURE2DARRAY
437 %token KW_TEXTURE2DMS
438 %token KW_TEXTURE2DMSARRAY
440 %token KW_TEXTURE3DARRAY
441 %token KW_TEXTURECUBE
446 %token KW_VERTEXSHADER
457 %token OP_LEFTSHIFTASSIGN
459 %token OP_RIGHTSHIFTASSIGN
477 %token
<intval
> PRE_LINE
479 %token
<name
> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
480 %type
<name
> any_identifier var_identifier
482 %token
<floatval
> C_FLOAT
483 %token
<intval
> C_INTEGER
484 %type
<boolval
> boolean
485 %type
<type
> base_type
487 %type
<list
> declaration_statement
488 %type
<list
> complex_initializer
489 %type
<list
> initializer_expr_list
490 %type
<instr
> initializer_expr
491 %type
<modifiers
> var_modifiers
492 %type
<list
> parameters
493 %type
<list
> param_list
497 %type
<list
> statement
498 %type
<list
> statement_list
499 %type
<list
> compound_statement
500 %type
<list
> jump_statement
501 %type
<function
> func_declaration
502 %type
<function
> func_prototype
503 %type
<parameter
> parameter
504 %type
<name
> semantic
505 %type
<variable_def
> variable_def
506 %type
<list
> variables_def
507 %type
<instr
> primary_expr
508 %type
<instr
> postfix_expr
509 %type
<instr
> unary_expr
510 %type
<instr
> mul_expr
511 %type
<instr
> add_expr
512 %type
<instr
> shift_expr
513 %type
<instr
> relational_expr
514 %type
<instr
> equality_expr
515 %type
<instr
> bitand_expr
516 %type
<instr
> bitxor_expr
517 %type
<instr
> bitor_expr
518 %type
<instr
> logicand_expr
519 %type
<instr
> logicor_expr
520 %type
<instr
> conditional_expr
521 %type
<instr
> assignment_expr
522 %type
<list
> expr_statement
523 %type
<unary_op
> unary_op
524 %type
<assign_op
> assign_op
525 %type
<modifiers
> input_mod
528 hlsl_prog: /* empty */
531 | hlsl_prog func_declaration
533 FIXME
("Check that the function doesn't conflict with an already declared one.\n");
534 list_add_tail
(&hlsl_ctx.functions
, &$2->node.entry
);
536 | hlsl_prog declaration_statement
538 TRACE
("Declaration statement parsed.\n");
540 | hlsl_prog preproc_directive
544 preproc_directive: PRE_LINE STRING
546 TRACE
("Updating line information to file %s, line %u\n", debugstr_a
($2), $1);
547 hlsl_ctx.line_no
= $1;
548 if
(strcmp
($2, hlsl_ctx.source_file
))
550 const char **new_array
;
552 hlsl_ctx.source_file
= $2;
553 new_array
= d3dcompiler_realloc
(hlsl_ctx.source_files
,
554 sizeof
(*hlsl_ctx.source_files
) * hlsl_ctx.source_files_count
+ 1);
557 hlsl_ctx.source_files
= new_array
;
558 hlsl_ctx.source_files
[hlsl_ctx.source_files_count
++] = $2;
563 any_identifier: VAR_IDENTIFIER
567 func_declaration: func_prototype compound_statement
569 TRACE
("Function %s parsed.\n", $1->name
);
572 pop_scope
(&hlsl_ctx
);
576 TRACE
("Function prototype for %s.\n", $1->name
);
578 pop_scope
(&hlsl_ctx
);
581 func_prototype: var_modifiers type var_identifier
'(' parameters
')' semantic
583 if
(get_variable
(hlsl_ctx.globals
, $3))
585 hlsl_report_message
(hlsl_ctx.source_file
, @
3.first_line
, @
3.first_column
,
586 HLSL_LEVEL_ERROR
, "redefinition of '%s'\n", $3);
589 if
($2->base_type
== HLSL_TYPE_VOID
&& $7)
591 hlsl_report_message
(hlsl_ctx.source_file
, @
7.first_line
, @
7.first_column
,
592 HLSL_LEVEL_ERROR
, "void function with a semantic");
595 $$
= new_func_decl
($3, $2, $5);
598 ERR
("Out of memory.\n");
604 compound_statement: '{' '}'
606 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
609 |
'{' scope_start statement_list
'}'
611 pop_scope
(&hlsl_ctx
);
615 scope_start: /* Empty */
617 push_scope
(&hlsl_ctx
);
620 var_identifier: VAR_IDENTIFIER
623 semantic: /* Empty */
632 parameters: scope_start
634 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
637 | scope_start param_list
642 param_list: parameter
644 struct source_location loc
;
646 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
648 set_location
(&loc
, &@
1);
649 if
(!add_func_parameter
($$
, &$1, &loc
))
651 ERR
("Error adding function parameter %s.\n", $1.name
);
652 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
656 | param_list
',' parameter
658 struct source_location loc
;
661 set_location
(&loc
, &@
3);
662 if
(!add_func_parameter
($$
, &$3, &loc
))
664 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
665 "duplicate parameter %s", $3.name
);
670 parameter: input_mod var_modifiers type any_identifier semantic
679 input_mod: /* Empty */
681 $$
= HLSL_MODIFIER_IN
;
685 $$
= HLSL_MODIFIER_IN
;
689 $$
= HLSL_MODIFIER_OUT
;
693 $$
= HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT
;
700 | KW_VECTOR
'<' base_type
',' C_INTEGER
'>'
702 if
($3->type
!= HLSL_CLASS_SCALAR
)
704 hlsl_message
("Line %u: vectors of non-scalar types are not allowed.\n",
706 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
709 if
($5 < 1 ||
$5 > 4)
711 hlsl_message
("Line %u: vector size must be between 1 and 4.\n",
713 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
717 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, $3->base_type
, $5, 1);
719 | KW_MATRIX
'<' base_type
',' C_INTEGER
',' C_INTEGER
'>'
721 if
($3->type
!= HLSL_CLASS_SCALAR
)
723 hlsl_message
("Line %u: matrices of non-scalar types are not allowed.\n",
725 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
728 if
($5 < 1 ||
$5 > 4 ||
$7 < 1 ||
$7 > 4)
730 hlsl_message
("Line %u: matrix dimensions must be between 1 and 4.\n",
732 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
736 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_MATRIX
, $3->base_type
, $5, $7);
741 $$
= new_hlsl_type
(d3dcompiler_strdup
("void"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VOID
, 1, 1);
745 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
746 $$
->sampler_dim
= HLSL_SAMPLER_DIM_GENERIC
;
750 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler1D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
751 $$
->sampler_dim
= HLSL_SAMPLER_DIM_1D
;
755 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler2D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
756 $$
->sampler_dim
= HLSL_SAMPLER_DIM_2D
;
760 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler3D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
761 $$
->sampler_dim
= HLSL_SAMPLER_DIM_3D
;
765 $$
= new_hlsl_type
(d3dcompiler_strdup
("samplerCUBE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
766 $$
->sampler_dim
= HLSL_SAMPLER_DIM_CUBE
;
770 struct hlsl_type
*type
;
772 TRACE
("Type %s.\n", $1);
773 type
= get_type
(hlsl_ctx.cur_scope
, $1, TRUE
);
775 d3dcompiler_free
($1);
777 | KW_STRUCT TYPE_IDENTIFIER
779 struct hlsl_type
*type
;
781 TRACE
("Struct type %s.\n", $2);
782 type
= get_type
(hlsl_ctx.cur_scope
, $2, TRUE
);
783 if
(type
->type
!= HLSL_CLASS_STRUCT
)
785 hlsl_message
("Line %u: redefining %s as a structure.\n",
786 hlsl_ctx.line_no
, $2);
787 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
793 d3dcompiler_free
($2);
796 declaration_statement: declaration
798 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
802 declaration: var_modifiers type variables_def
';'
804 struct parse_variable_def
*v
, *v_next
;
805 struct hlsl_ir_var
*var
;
806 BOOL ret
, local
= TRUE
;
808 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, $3, struct parse_variable_def
, entry
)
810 debug_dump_decl
($2, $1, v
->name
, hlsl_ctx.line_no
);
811 var
= d3dcompiler_alloc
(sizeof
(*var
));
812 var
->node.type
= HLSL_IR_VAR
;
814 var
->node.data_type
= new_array_type
($2, v
->array_size
);
816 var
->node.data_type
= $2;
817 var
->node.loc
= v
->loc
;
820 var
->semantic
= v
->semantic
;
822 if
(hlsl_ctx.cur_scope
== hlsl_ctx.globals
)
824 var
->modifiers |
= HLSL_STORAGE_UNIFORM
;
828 if
(var
->modifiers
& HLSL_MODIFIER_CONST
&& !v
->initializer
)
830 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
,
831 HLSL_LEVEL_ERROR
, "const variable without initializer");
832 free_declaration
(var
);
837 ret
= declare_variable
(var
, local
);
839 free_declaration
(var
);
841 TRACE
("Declared variable %s.\n", var
->name
);
845 FIXME
("Variable with an initializer.\n");
846 free_instr_list
(v
->initializer
);
851 d3dcompiler_free
($3);
854 variables_def: variable_def
856 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
858 list_add_head
($$
, &$1->entry
);
860 | variables_def
',' variable_def
863 list_add_tail
($$
, &$3->entry
);
866 variable_def: any_identifier array semantic
868 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
869 set_location
(&$$
->loc
, &@
1);
874 | any_identifier array semantic
'=' complex_initializer
876 TRACE
("Declaration with initializer.\n");
877 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
878 set_location
(&$$
->loc
, &@
1);
882 $$
->initializer
= $5;
896 var_modifiers: /* Empty */
900 | KW_EXTERN var_modifiers
902 $$
= add_modifier
($2, HLSL_STORAGE_EXTERN
, &@
1);
904 | KW_NOINTERPOLATION var_modifiers
906 $$
= add_modifier
($2, HLSL_STORAGE_NOINTERPOLATION
, &@
1);
908 | KW_PRECISE var_modifiers
910 $$
= add_modifier
($2, HLSL_MODIFIER_PRECISE
, &@
1);
912 | KW_SHARED var_modifiers
914 $$
= add_modifier
($2, HLSL_STORAGE_SHARED
, &@
1);
916 | KW_GROUPSHARED var_modifiers
918 $$
= add_modifier
($2, HLSL_STORAGE_GROUPSHARED
, &@
1);
920 | KW_STATIC var_modifiers
922 $$
= add_modifier
($2, HLSL_STORAGE_STATIC
, &@
1);
924 | KW_UNIFORM var_modifiers
926 $$
= add_modifier
($2, HLSL_STORAGE_UNIFORM
, &@
1);
928 | KW_VOLATILE var_modifiers
930 $$
= add_modifier
($2, HLSL_STORAGE_VOLATILE
, &@
1);
932 | KW_CONST var_modifiers
934 $$
= add_modifier
($2, HLSL_MODIFIER_CONST
, &@
1);
936 | KW_ROW_MAJOR var_modifiers
938 $$
= add_modifier
($2, HLSL_MODIFIER_ROW_MAJOR
, &@
1);
940 | KW_COLUMN_MAJOR var_modifiers
942 $$
= add_modifier
($2, HLSL_MODIFIER_COLUMN_MAJOR
, &@
1);
945 complex_initializer: initializer_expr
947 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
949 list_add_head
($$
, &$1->entry
);
951 |
'{' initializer_expr_list
'}'
956 initializer_expr: assignment_expr
961 initializer_expr_list: initializer_expr
963 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
965 list_add_head
($$
, &$1->entry
);
967 | initializer_expr_list
',' initializer_expr
970 list_add_tail
($$
, &$3->entry
);
982 statement_list: statement
986 | statement_list statement
989 list_move_tail
($$
, $2);
990 d3dcompiler_free
($2);
993 statement: declaration_statement
998 /* FIXME: add rule for return with no value */
999 jump_statement: KW_RETURN expr
';'
1001 struct hlsl_ir_jump
*jump
= d3dcompiler_alloc
(sizeof
(*jump
));
1004 ERR
("Out of memory\n");
1007 jump
->node.type
= HLSL_IR_JUMP
;
1008 set_location
(&jump
->node.loc
, &@
1);
1009 jump
->type
= HLSL_IR_JUMP_RETURN
;
1010 jump
->node.data_type
= $2->data_type
;
1011 jump
->return_value
= $2;
1013 FIXME
("Check for valued return on void function.\n");
1014 FIXME
("Implicit conversion to the return type if needed, "
1015 "error out if conversion not possible.\n");
1017 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1019 list_add_tail
($$
, &jump
->node.entry
);
1024 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1029 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1032 list_add_head
($$
, &$1->entry
);
1035 primary_expr: C_FLOAT
1037 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1040 ERR
("Out of memory.\n");
1043 c
->node.type
= HLSL_IR_CONSTANT
;
1044 set_location
(&c
->node.loc
, &yylloc);
1045 c
->node.data_type
= new_hlsl_type
("float", HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
1046 c
->v.value.f
[0] = $1;
1051 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1054 ERR
("Out of memory.\n");
1057 c
->node.type
= HLSL_IR_CONSTANT
;
1058 set_location
(&c
->node.loc
, &yylloc);
1059 c
->node.data_type
= new_hlsl_type
("int", HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
1060 c
->v.value.i
[0] = $1;
1065 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1068 ERR
("Out of memory.\n");
1071 c
->node.type
= HLSL_IR_CONSTANT
;
1072 set_location
(&c
->node.loc
, &yylloc);
1073 c
->node.data_type
= new_hlsl_type
("bool", HLSL_CLASS_SCALAR
, HLSL_TYPE_BOOL
, 1, 1);
1074 c
->v.value.b
[0] = $1;
1079 struct hlsl_ir_deref
*deref
= new_var_deref
($1);
1083 set_location
(&$$
->loc
, &@
1);
1093 variable: VAR_IDENTIFIER
1095 struct hlsl_ir_var
*var
;
1096 var
= get_variable
(hlsl_ctx.cur_scope
, $1);
1099 hlsl_message
("Line %d: variable '%s' not declared\n",
1100 hlsl_ctx.line_no
, $1);
1101 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1107 postfix_expr: primary_expr
1111 | postfix_expr OP_INC
1113 struct hlsl_ir_node
*operands
[3];
1114 struct source_location loc
;
1116 set_location
(&loc
, &@
2);
1117 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1119 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1120 "modifying a const expression");
1124 operands
[1] = operands
[2] = NULL
;
1125 $$
= &new_expr
(HLSL_IR_BINOP_POSTINC
, operands
, &loc
)->node
;
1126 /* Post increment/decrement expressions are considered const */
1127 $$
->data_type
= clone_hlsl_type
($$
->data_type
);
1128 $$
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
1130 | postfix_expr OP_DEC
1132 struct hlsl_ir_node
*operands
[3];
1133 struct source_location loc
;
1135 set_location
(&loc
, &@
2);
1136 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1138 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1139 "modifying a const expression");
1143 operands
[1] = operands
[2] = NULL
;
1144 $$
= &new_expr
(HLSL_IR_BINOP_POSTDEC
, operands
, &loc
)->node
;
1145 /* Post increment/decrement expressions are considered const */
1146 $$
->data_type
= clone_hlsl_type
($$
->data_type
);
1147 $$
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
1149 | postfix_expr
'.' any_identifier
1151 struct source_location loc
;
1153 set_location
(&loc
, &@
2);
1154 if
($1->data_type
->type
<= HLSL_CLASS_LAST_NUMERIC
)
1156 struct hlsl_ir_swizzle
*swizzle
;
1158 swizzle
= get_swizzle
($1, $3, &loc
);
1161 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1162 "invalid swizzle %s", debugstr_a
($3));
1165 $$
= &swizzle
->node
;
1169 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1170 "invalid subscript %s", debugstr_a
($3));
1174 /* "var_modifiers" doesn't make sense in this case, but it's needed
1175 in the grammar to avoid shift/reduce conflicts. */
1176 | var_modifiers type
'(' initializer_expr_list
')'
1178 struct hlsl_ir_constructor
*constructor
;
1180 TRACE
("%s constructor.\n", debug_hlsl_type
($2));
1183 hlsl_message
("Line %u: unexpected modifier in a constructor.\n",
1185 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1188 if
($2->type
> HLSL_CLASS_LAST_NUMERIC
)
1190 hlsl_message
("Line %u: constructors are allowed only for numeric data types.\n",
1192 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1195 if
($2->dimx
* $2->dimy
!= components_count_expr_list
($4))
1197 hlsl_message
("Line %u: wrong number of components in constructor.\n",
1199 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1203 constructor
= d3dcompiler_alloc
(sizeof
(*constructor
));
1204 constructor
->node.type
= HLSL_IR_CONSTRUCTOR
;
1205 set_location
(&constructor
->node.loc
, &@
3);
1206 constructor
->node.data_type
= $2;
1207 constructor
->arguments
= $4;
1209 $$
= &constructor
->node
;
1212 unary_expr: postfix_expr
1218 struct hlsl_ir_node
*operands
[3];
1219 struct source_location loc
;
1221 set_location
(&loc
, &@
1);
1222 if
($2->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1224 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1225 "modifying a const expression");
1229 operands
[1] = operands
[2] = NULL
;
1230 $$
= &new_expr
(HLSL_IR_BINOP_PREINC
, operands
, &loc
)->node
;
1234 struct hlsl_ir_node
*operands
[3];
1235 struct source_location loc
;
1237 set_location
(&loc
, &@
1);
1238 if
($2->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1240 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1241 "modifying a const expression");
1245 operands
[1] = operands
[2] = NULL
;
1246 $$
= &new_expr
(HLSL_IR_BINOP_PREDEC
, operands
, &loc
)->node
;
1248 | unary_op unary_expr
1250 enum hlsl_ir_expr_op ops
[] = {0, HLSL_IR_UNOP_NEG
,
1251 HLSL_IR_UNOP_LOGIC_NOT
, HLSL_IR_UNOP_BIT_NOT
};
1252 struct hlsl_ir_node
*operands
[3];
1253 struct source_location loc
;
1255 if
($1 == UNARY_OP_PLUS
)
1262 operands
[1] = operands
[2] = NULL
;
1263 set_location
(&loc
, &@
1);
1264 $$
= &new_expr
(ops
[$1], operands
, &loc
)->node
;
1274 $$
= UNARY_OP_MINUS
;
1278 $$
= UNARY_OP_LOGICNOT
;
1282 $$
= UNARY_OP_BITNOT
;
1285 mul_expr: unary_expr
1289 | mul_expr
'*' unary_expr
1291 struct source_location loc
;
1293 set_location
(&loc
, &@
2);
1294 $$
= &hlsl_mul
($1, $3, &loc
)->node
;
1296 | mul_expr
'/' unary_expr
1298 struct source_location loc
;
1300 set_location
(&loc
, &@
2);
1301 $$
= &hlsl_div
($1, $3, &loc
)->node
;
1303 | mul_expr
'%' unary_expr
1305 struct source_location loc
;
1307 set_location
(&loc
, &@
2);
1308 $$
= &hlsl_mod
($1, $3, &loc
)->node
;
1315 | add_expr
'+' mul_expr
1317 struct source_location loc
;
1319 set_location
(&loc
, &@
2);
1320 $$
= &hlsl_add
($1, $3, &loc
)->node
;
1322 | add_expr
'-' mul_expr
1324 struct source_location loc
;
1326 set_location
(&loc
, &@
2);
1327 $$
= &hlsl_sub
($1, $3, &loc
)->node
;
1330 shift_expr: add_expr
1334 | shift_expr OP_LEFTSHIFT add_expr
1336 FIXME
("Left shift\n");
1338 | shift_expr OP_RIGHTSHIFT add_expr
1340 FIXME
("Right shift\n");
1343 relational_expr: shift_expr
1347 | relational_expr
'<' shift_expr
1349 struct source_location loc
;
1351 set_location
(&loc
, &@
2);
1352 $$
= &hlsl_lt
($1, $3, &loc
)->node
;
1354 | relational_expr
'>' shift_expr
1356 struct source_location loc
;
1358 set_location
(&loc
, &@
2);
1359 $$
= &hlsl_gt
($1, $3, &loc
)->node
;
1361 | relational_expr OP_LE shift_expr
1363 struct source_location loc
;
1365 set_location
(&loc
, &@
2);
1366 $$
= &hlsl_le
($1, $3, &loc
)->node
;
1368 | relational_expr OP_GE shift_expr
1370 struct source_location loc
;
1372 set_location
(&loc
, &@
2);
1373 $$
= &hlsl_ge
($1, $3, &loc
)->node
;
1376 equality_expr: relational_expr
1380 | equality_expr OP_EQ relational_expr
1382 struct source_location loc
;
1384 set_location
(&loc
, &@
2);
1385 $$
= &hlsl_eq
($1, $3, &loc
)->node
;
1387 | equality_expr OP_NE relational_expr
1389 struct source_location loc
;
1391 set_location
(&loc
, &@
2);
1392 $$
= &hlsl_ne
($1, $3, &loc
)->node
;
1395 bitand_expr: equality_expr
1399 | bitand_expr
'&' equality_expr
1401 FIXME
("bitwise AND\n");
1404 bitxor_expr: bitand_expr
1408 | bitxor_expr
'^' bitand_expr
1410 FIXME
("bitwise XOR\n");
1413 bitor_expr: bitxor_expr
1417 | bitor_expr
'|' bitxor_expr
1419 FIXME
("bitwise OR\n");
1422 logicand_expr: bitor_expr
1426 | logicand_expr OP_AND bitor_expr
1428 FIXME
("logic AND\n");
1431 logicor_expr: logicand_expr
1435 | logicor_expr OP_OR logicand_expr
1437 FIXME
("logic OR\n");
1440 conditional_expr: logicor_expr
1444 | logicor_expr
'?' expr
':' assignment_expr
1446 FIXME
("ternary operator\n");
1449 assignment_expr: conditional_expr
1453 | unary_expr assign_op assignment_expr
1455 struct source_location loc
;
1457 set_location
(&loc
, &@
2);
1458 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1460 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1461 "l-value is const");
1464 $$
= make_assignment
($1, $2, BWRITERSP_WRITEMASK_ALL
, $3);
1472 $$
= ASSIGN_OP_ASSIGN
;
1494 | OP_LEFTSHIFTASSIGN
1496 $$
= ASSIGN_OP_LSHIFT
;
1498 | OP_RIGHTSHIFTASSIGN
1500 $$
= ASSIGN_OP_RSHIFT
;
1515 expr: assignment_expr
1519 | expr
',' assignment_expr
1521 FIXME
("Comma expression\n");
1526 static void set_location
(struct source_location
*loc
, const struct YYLTYPE *l
)
1528 loc
->file
= hlsl_ctx.source_file
;
1529 loc
->line
= l
->first_line
;
1530 loc
->col
= l
->first_column
;
1533 static DWORD add_modifier
(DWORD modifiers
, DWORD mod
, const struct YYLTYPE *loc
)
1535 if
(modifiers
& mod
)
1537 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
1538 "modifier '%s' already specified", debug_modifiers
(mod
));
1541 if
(mod
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
)
1542 && modifiers
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
))
1544 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
1545 "more than one matrix majority keyword");
1548 return modifiers | mod
;
1551 struct bwriter_shader
*parse_hlsl
(enum shader_type type
, DWORD major
, DWORD minor
,
1552 const char *entrypoint
, char **messages
)
1554 struct hlsl_ir_function_decl
*function
;
1555 struct hlsl_scope
*scope
, *next_scope
;
1556 struct hlsl_type
*hlsl_type
, *next_type
;
1557 struct hlsl_ir_var
*var
, *next_var
;
1560 hlsl_ctx.status
= PARSE_SUCCESS
;
1561 hlsl_ctx.messages.size
= hlsl_ctx.messages.capacity
= 0;
1562 hlsl_ctx.line_no
= hlsl_ctx.column
= 1;
1563 hlsl_ctx.source_file
= d3dcompiler_strdup
("");
1564 hlsl_ctx.source_files
= d3dcompiler_alloc
(sizeof
(*hlsl_ctx.source_files
));
1565 if
(hlsl_ctx.source_files
)
1566 hlsl_ctx.source_files
[0] = hlsl_ctx.source_file
;
1567 hlsl_ctx.source_files_count
= 1;
1568 hlsl_ctx.cur_scope
= NULL
;
1569 hlsl_ctx.matrix_majority
= HLSL_COLUMN_MAJOR
;
1570 list_init
(&hlsl_ctx.scopes
);
1571 list_init
(&hlsl_ctx.types
);
1572 list_init
(&hlsl_ctx.functions
);
1574 push_scope
(&hlsl_ctx
);
1575 hlsl_ctx.globals
= hlsl_ctx.cur_scope
;
1576 declare_predefined_types
(hlsl_ctx.globals
);
1580 if
(TRACE_ON
(hlsl_parser
))
1582 struct hlsl_ir_function_decl
*func
;
1584 TRACE
("IR dump.\n");
1585 LIST_FOR_EACH_ENTRY
(func
, &hlsl_ctx.functions
, struct hlsl_ir_function_decl
, node.entry
)
1588 debug_dump_ir_function
(func
);
1592 TRACE
("Compilation status = %d\n", hlsl_ctx.status
);
1595 if
(hlsl_ctx.messages.size
)
1596 *messages
= hlsl_ctx.messages.
string;
1602 if
(hlsl_ctx.messages.capacity
)
1603 d3dcompiler_free
(hlsl_ctx.messages.
string);
1606 for
(i
= 0; i
< hlsl_ctx.source_files_count
; ++i
)
1607 d3dcompiler_free
((void *)hlsl_ctx.source_files
[i
]);
1608 d3dcompiler_free
(hlsl_ctx.source_files
);
1610 TRACE
("Freeing functions IR.\n");
1611 LIST_FOR_EACH_ENTRY
(function
, &hlsl_ctx.functions
, struct hlsl_ir_function_decl
, node.entry
)
1612 free_function
(function
);
1614 TRACE
("Freeing variables.\n");
1615 LIST_FOR_EACH_ENTRY_SAFE
(scope
, next_scope
, &hlsl_ctx.scopes
, struct hlsl_scope
, entry
)
1617 LIST_FOR_EACH_ENTRY_SAFE
(var
, next_var
, &scope
->vars
, struct hlsl_ir_var
, scope_entry
)
1619 free_declaration
(var
);
1621 d3dcompiler_free
(scope
);
1624 TRACE
("Freeing types.\n");
1625 LIST_FOR_EACH_ENTRY_SAFE
(hlsl_type
, next_type
, &hlsl_ctx.types
, struct hlsl_type
, entry
)
1627 free_hlsl_type
(hlsl_type
);