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 static unsigned int components_count_expr_list
(struct list
*list
)
180 struct hlsl_ir_node
*node
;
181 unsigned int count
= 0;
183 LIST_FOR_EACH_ENTRY
(node
, list
, struct hlsl_ir_node
, entry
)
185 count
+= components_count_type
(node
->data_type
);
197 struct hlsl_type
*type
;
203 struct hlsl_ir_var
*var
;
204 struct hlsl_ir_node
*instr
;
206 struct hlsl_ir_function_decl
*function
;
207 struct parse_parameter parameter
;
208 struct parse_variable_def
*variable_def
;
209 enum parse_unary_op unary_op
;
216 %token KW_COLUMN_MAJOR
220 %token KW_DEPTHSTENCILSTATE
221 %token KW_DEPTHSTENCILVIEW
229 %token KW_GEOMETRYSHADER
230 %token KW_GROUPSHARED
237 %token KW_NOINTERPOLATION
240 %token KW_PIXELSHADER
242 %token KW_RASTERIZERSTATE
243 %token KW_RENDERTARGETVIEW
251 %token KW_SAMPLERCUBE
252 %token KW_SAMPLER_STATE
253 %token KW_SAMPLERCOMPARISONSTATE
256 %token KW_STATEBLOCK_STATE
263 %token KW_TECHNIQUE10
266 %token KW_TEXTURE1DARRAY
268 %token KW_TEXTURE2DARRAY
269 %token KW_TEXTURE2DMS
270 %token KW_TEXTURE2DMSARRAY
272 %token KW_TEXTURE3DARRAY
273 %token KW_TEXTURECUBE
278 %token KW_VERTEXSHADER
289 %token OP_LEFTSHIFTASSIGN
291 %token OP_RIGHTSHIFTASSIGN
309 %token
<intval
> PRE_LINE
311 %token
<name
> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
312 %type
<name
> any_identifier var_identifier
314 %token
<floatval
> C_FLOAT
315 %token
<intval
> C_INTEGER
316 %type
<boolval
> boolean
317 %type
<type
> base_type
319 %type
<list
> declaration_statement
320 %type
<list
> complex_initializer
321 %type
<list
> initializer_expr_list
322 %type
<instr
> initializer_expr
323 %type
<modifiers
> var_modifiers
324 %type
<list
> parameters
325 %type
<list
> param_list
329 %type
<list
> statement
330 %type
<list
> statement_list
331 %type
<list
> compound_statement
332 %type
<function
> func_declaration
333 %type
<function
> func_prototype
334 %type
<parameter
> parameter
335 %type
<name
> semantic
336 %type
<variable_def
> variable_def
337 %type
<list
> variables_def
338 %type
<instr
> primary_expr
339 %type
<instr
> postfix_expr
340 %type
<instr
> unary_expr
341 %type
<instr
> mul_expr
342 %type
<instr
> add_expr
343 %type
<instr
> shift_expr
344 %type
<instr
> relational_expr
345 %type
<instr
> equality_expr
346 %type
<instr
> bitand_expr
347 %type
<instr
> bitxor_expr
348 %type
<instr
> bitor_expr
349 %type
<instr
> logicand_expr
350 %type
<instr
> logicor_expr
351 %type
<instr
> conditional_expr
352 %type
<instr
> assignment_expr
353 %type
<list
> expr_statement
354 %type
<unary_op
> unary_op
355 %type
<modifiers
> input_mod
358 hlsl_prog: /* empty */
361 | hlsl_prog func_declaration
363 FIXME
("Check that the function doesn't conflict with an already declared one.\n");
364 list_add_tail
(&hlsl_ctx.functions
, &$2->node.entry
);
366 | hlsl_prog declaration_statement
368 TRACE
("Declaration statement parsed.\n");
370 | hlsl_prog preproc_directive
374 preproc_directive: PRE_LINE STRING
376 TRACE
("Updating line information to file %s, line %u\n", debugstr_a
($2), $1);
377 hlsl_ctx.line_no
= $1;
378 if
(strcmp
($2, hlsl_ctx.source_file
))
380 const char **new_array
;
382 hlsl_ctx.source_file
= $2;
383 new_array
= d3dcompiler_realloc
(hlsl_ctx.source_files
,
384 sizeof
(*hlsl_ctx.source_files
) * hlsl_ctx.source_files_count
+ 1);
387 hlsl_ctx.source_files
= new_array
;
388 hlsl_ctx.source_files
[hlsl_ctx.source_files_count
++] = $2;
393 any_identifier: VAR_IDENTIFIER
397 func_declaration: func_prototype compound_statement
399 TRACE
("Function %s parsed.\n", $1->name
);
402 pop_scope
(&hlsl_ctx
);
406 TRACE
("Function prototype for %s.\n", $1->name
);
408 pop_scope
(&hlsl_ctx
);
411 func_prototype: var_modifiers type var_identifier
'(' parameters
')' semantic
413 if
(get_variable
(hlsl_ctx.globals
, $3))
415 hlsl_report_message
(hlsl_ctx.source_file
, @
3.first_line
, @
3.first_column
,
416 HLSL_LEVEL_ERROR
, "redefinition of '%s'\n", $3);
419 if
($2->base_type
== HLSL_TYPE_VOID
&& $7)
421 hlsl_report_message
(hlsl_ctx.source_file
, @
7.first_line
, @
7.first_column
,
422 HLSL_LEVEL_ERROR
, "void function with a semantic");
425 $$
= new_func_decl
($3, $2, $5);
428 ERR
("Out of memory.\n");
434 compound_statement: '{' '}'
436 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
439 |
'{' scope_start statement_list
'}'
441 pop_scope
(&hlsl_ctx
);
445 scope_start: /* Empty */
447 push_scope
(&hlsl_ctx
);
450 var_identifier: VAR_IDENTIFIER
453 semantic: /* Empty */
462 parameters: scope_start
464 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
467 | scope_start param_list
472 param_list: parameter
474 struct source_location loc
;
476 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
478 set_location
(&loc
, &@
1);
479 if
(!add_func_parameter
($$
, &$1, &loc
))
481 ERR
("Error adding function parameter %s.\n", $1.name
);
482 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
486 | param_list
',' parameter
488 struct source_location loc
;
491 set_location
(&loc
, &@
3);
492 if
(!add_func_parameter
($$
, &$3, &loc
))
494 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
495 "duplicate parameter %s", $3.name
);
500 parameter: input_mod var_modifiers type any_identifier semantic
509 input_mod: /* Empty */
511 $$
= HLSL_MODIFIER_IN
;
515 $$
= HLSL_MODIFIER_IN
;
519 $$
= HLSL_MODIFIER_OUT
;
523 $$
= HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT
;
530 | KW_VECTOR
'<' base_type
',' C_INTEGER
'>'
532 if
($3->type
!= HLSL_CLASS_SCALAR
)
534 hlsl_message
("Line %u: vectors of non-scalar types are not allowed.\n",
536 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
539 if
($5 < 1 ||
$5 > 4)
541 hlsl_message
("Line %u: vector size must be between 1 and 4.\n",
543 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
547 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, $3->base_type
, $5, 1);
549 | KW_MATRIX
'<' base_type
',' C_INTEGER
',' C_INTEGER
'>'
551 if
($3->type
!= HLSL_CLASS_SCALAR
)
553 hlsl_message
("Line %u: matrices of non-scalar types are not allowed.\n",
555 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
558 if
($5 < 1 ||
$5 > 4 ||
$7 < 1 ||
$7 > 4)
560 hlsl_message
("Line %u: matrix dimensions must be between 1 and 4.\n",
562 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
566 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_MATRIX
, $3->base_type
, $5, $7);
571 $$
= new_hlsl_type
(d3dcompiler_strdup
("void"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VOID
, 1, 1);
575 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
576 $$
->sampler_dim
= HLSL_SAMPLER_DIM_GENERIC
;
580 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler1D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
581 $$
->sampler_dim
= HLSL_SAMPLER_DIM_1D
;
585 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler2D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
586 $$
->sampler_dim
= HLSL_SAMPLER_DIM_2D
;
590 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler3D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
591 $$
->sampler_dim
= HLSL_SAMPLER_DIM_3D
;
595 $$
= new_hlsl_type
(d3dcompiler_strdup
("samplerCUBE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
596 $$
->sampler_dim
= HLSL_SAMPLER_DIM_CUBE
;
600 struct hlsl_type
*type
;
602 TRACE
("Type %s.\n", $1);
603 type
= get_type
(hlsl_ctx.cur_scope
, $1, TRUE
);
605 d3dcompiler_free
($1);
607 | KW_STRUCT TYPE_IDENTIFIER
609 struct hlsl_type
*type
;
611 TRACE
("Struct type %s.\n", $2);
612 type
= get_type
(hlsl_ctx.cur_scope
, $2, TRUE
);
613 if
(type
->type
!= HLSL_CLASS_STRUCT
)
615 hlsl_message
("Line %u: redefining %s as a structure.\n",
616 hlsl_ctx.line_no
, $2);
617 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
623 d3dcompiler_free
($2);
626 declaration_statement: declaration
628 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
632 declaration: var_modifiers type variables_def
';'
634 struct parse_variable_def
*v
, *v_next
;
635 struct hlsl_ir_var
*var
;
636 BOOL ret
, local
= TRUE
;
638 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, $3, struct parse_variable_def
, entry
)
640 debug_dump_decl
($2, $1, v
->name
, hlsl_ctx.line_no
);
641 var
= d3dcompiler_alloc
(sizeof
(*var
));
642 var
->node.type
= HLSL_IR_VAR
;
644 var
->node.data_type
= new_array_type
($2, v
->array_size
);
646 var
->node.data_type
= $2;
647 var
->node.loc
= v
->loc
;
650 var
->semantic
= v
->semantic
;
652 if
(hlsl_ctx.cur_scope
== hlsl_ctx.globals
)
654 var
->modifiers |
= HLSL_STORAGE_UNIFORM
;
658 if
(var
->modifiers
& HLSL_MODIFIER_CONST
&& !v
->initializer
)
660 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
,
661 HLSL_LEVEL_ERROR
, "const variable without initializer");
662 free_declaration
(var
);
667 ret
= declare_variable
(var
, local
);
669 free_declaration
(var
);
671 TRACE
("Declared variable %s.\n", var
->name
);
675 FIXME
("Variable with an initializer.\n");
676 free_instr_list
(v
->initializer
);
681 d3dcompiler_free
($3);
684 variables_def: variable_def
686 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
688 list_add_head
($$
, &$1->entry
);
690 | variables_def
',' variable_def
693 list_add_tail
($$
, &$3->entry
);
696 variable_def: any_identifier array semantic
698 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
699 set_location
(&$$
->loc
, &@
1);
704 | any_identifier array semantic
'=' complex_initializer
706 TRACE
("Declaration with initializer.\n");
707 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
708 set_location
(&$$
->loc
, &@
1);
712 $$
->initializer
= $5;
726 var_modifiers: /* Empty */
730 | KW_EXTERN var_modifiers
732 $$
= add_modifier
($2, HLSL_STORAGE_EXTERN
, &@
1);
734 | KW_NOINTERPOLATION var_modifiers
736 $$
= add_modifier
($2, HLSL_STORAGE_NOINTERPOLATION
, &@
1);
738 | KW_PRECISE var_modifiers
740 $$
= add_modifier
($2, HLSL_MODIFIER_PRECISE
, &@
1);
742 | KW_SHARED var_modifiers
744 $$
= add_modifier
($2, HLSL_STORAGE_SHARED
, &@
1);
746 | KW_GROUPSHARED var_modifiers
748 $$
= add_modifier
($2, HLSL_STORAGE_GROUPSHARED
, &@
1);
750 | KW_STATIC var_modifiers
752 $$
= add_modifier
($2, HLSL_STORAGE_STATIC
, &@
1);
754 | KW_UNIFORM var_modifiers
756 $$
= add_modifier
($2, HLSL_STORAGE_UNIFORM
, &@
1);
758 | KW_VOLATILE var_modifiers
760 $$
= add_modifier
($2, HLSL_STORAGE_VOLATILE
, &@
1);
762 | KW_CONST var_modifiers
764 $$
= add_modifier
($2, HLSL_MODIFIER_CONST
, &@
1);
766 | KW_ROW_MAJOR var_modifiers
768 $$
= add_modifier
($2, HLSL_MODIFIER_ROW_MAJOR
, &@
1);
770 | KW_COLUMN_MAJOR var_modifiers
772 $$
= add_modifier
($2, HLSL_MODIFIER_COLUMN_MAJOR
, &@
1);
775 complex_initializer: initializer_expr
777 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
779 list_add_head
($$
, &$1->entry
);
781 |
'{' initializer_expr_list
'}'
786 initializer_expr: assignment_expr
791 initializer_expr_list: initializer_expr
793 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
795 list_add_head
($$
, &$1->entry
);
797 | initializer_expr_list
',' initializer_expr
800 list_add_tail
($$
, &$3->entry
);
812 statement_list: statement
816 | statement_list statement
819 list_move_tail
($$
, $2);
820 d3dcompiler_free
($2);
823 statement: declaration_statement
838 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
843 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
846 list_add_head
($$
, &$1->entry
);
849 primary_expr: C_FLOAT
851 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
854 ERR
("Out of memory.\n");
857 c
->node.type
= HLSL_IR_CONSTANT
;
858 set_location
(&c
->node.loc
, &yylloc);
859 c
->node.data_type
= new_hlsl_type
("float", HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
860 c
->v.value.f
[0] = $1;
865 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
868 ERR
("Out of memory.\n");
871 c
->node.type
= HLSL_IR_CONSTANT
;
872 set_location
(&c
->node.loc
, &yylloc);
873 c
->node.data_type
= new_hlsl_type
("int", HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
874 c
->v.value.i
[0] = $1;
879 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
882 ERR
("Out of memory.\n");
885 c
->node.type
= HLSL_IR_CONSTANT
;
886 set_location
(&c
->node.loc
, &yylloc);
887 c
->node.data_type
= new_hlsl_type
("bool", HLSL_CLASS_SCALAR
, HLSL_TYPE_BOOL
, 1, 1);
888 c
->v.value.b
[0] = $1;
893 struct hlsl_ir_deref
*deref
= new_var_deref
($1);
897 set_location
(&$$
->loc
, &@
1);
907 variable: VAR_IDENTIFIER
909 struct hlsl_ir_var
*var
;
910 var
= get_variable
(hlsl_ctx.cur_scope
, $1);
913 hlsl_message
("Line %d: variable '%s' not declared\n",
914 hlsl_ctx.line_no
, $1);
915 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
921 postfix_expr: primary_expr
925 | postfix_expr OP_INC
927 struct hlsl_ir_node
*operands
[3];
928 struct source_location loc
;
931 operands
[1] = operands
[2] = NULL
;
932 set_location
(&loc
, &@
2);
933 $$
= &new_expr
(HLSL_IR_BINOP_POSTINC
, operands
, &loc
)->node
;
935 | postfix_expr OP_DEC
937 struct hlsl_ir_node
*operands
[3];
938 struct source_location loc
;
941 operands
[1] = operands
[2] = NULL
;
942 set_location
(&loc
, &@
2);
943 $$
= &new_expr
(HLSL_IR_BINOP_POSTDEC
, operands
, &loc
)->node
;
945 /* "var_modifiers" doesn't make sense in this case, but it's needed
946 in the grammar to avoid shift/reduce conflicts. */
947 | var_modifiers type
'(' initializer_expr_list
')'
949 struct hlsl_ir_constructor
*constructor
;
951 TRACE
("%s constructor.\n", debug_hlsl_type
($2));
954 hlsl_message
("Line %u: unexpected modifier in a constructor.\n",
956 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
959 if
($2->type
> HLSL_CLASS_LAST_NUMERIC
)
961 hlsl_message
("Line %u: constructors are allowed only for numeric data types.\n",
963 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
966 if
($2->dimx
* $2->dimy
!= components_count_expr_list
($4))
968 hlsl_message
("Line %u: wrong number of components in constructor.\n",
970 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
974 constructor
= d3dcompiler_alloc
(sizeof
(*constructor
));
975 constructor
->node.type
= HLSL_IR_CONSTRUCTOR
;
976 set_location
(&constructor
->node.loc
, &@
3);
977 constructor
->node.data_type
= $2;
978 constructor
->arguments
= $4;
980 $$
= &constructor
->node
;
983 unary_expr: postfix_expr
989 struct hlsl_ir_node
*operands
[3];
990 struct source_location loc
;
993 operands
[1] = operands
[2] = NULL
;
994 set_location
(&loc
, &@
1);
995 $$
= &new_expr
(HLSL_IR_BINOP_PREINC
, operands
, &loc
)->node
;
999 struct hlsl_ir_node
*operands
[3];
1000 struct source_location loc
;
1003 operands
[1] = operands
[2] = NULL
;
1004 set_location
(&loc
, &@
1);
1005 $$
= &new_expr
(HLSL_IR_BINOP_PREDEC
, operands
, &loc
)->node
;
1007 | unary_op unary_expr
1009 enum hlsl_ir_expr_op ops
[] = {0, HLSL_IR_UNOP_NEG
,
1010 HLSL_IR_UNOP_LOGIC_NOT
, HLSL_IR_UNOP_BIT_NOT
};
1011 struct hlsl_ir_node
*operands
[3];
1012 struct source_location loc
;
1014 if
($1 == UNARY_OP_PLUS
)
1021 operands
[1] = operands
[2] = NULL
;
1022 set_location
(&loc
, &@
1);
1023 $$
= &new_expr
(ops
[$1], operands
, &loc
)->node
;
1033 $$
= UNARY_OP_MINUS
;
1037 $$
= UNARY_OP_LOGICNOT
;
1041 $$
= UNARY_OP_BITNOT
;
1044 mul_expr: unary_expr
1048 | mul_expr
'*' unary_expr
1050 struct source_location loc
;
1052 set_location
(&loc
, &@
2);
1053 $$
= &hlsl_mul
($1, $3, &loc
)->node
;
1055 | mul_expr
'/' unary_expr
1057 struct source_location loc
;
1059 set_location
(&loc
, &@
2);
1060 $$
= &hlsl_div
($1, $3, &loc
)->node
;
1062 | mul_expr
'%' unary_expr
1064 struct source_location loc
;
1066 set_location
(&loc
, &@
2);
1067 $$
= &hlsl_mod
($1, $3, &loc
)->node
;
1074 | add_expr
'+' mul_expr
1076 struct source_location loc
;
1078 set_location
(&loc
, &@
2);
1079 $$
= &hlsl_add
($1, $3, &loc
)->node
;
1081 | add_expr
'-' mul_expr
1083 struct source_location loc
;
1085 set_location
(&loc
, &@
2);
1086 $$
= &hlsl_sub
($1, $3, &loc
)->node
;
1089 shift_expr: add_expr
1093 | shift_expr OP_LEFTSHIFT add_expr
1095 FIXME
("Left shift\n");
1097 | shift_expr OP_RIGHTSHIFT add_expr
1099 FIXME
("Right shift\n");
1102 relational_expr: shift_expr
1106 | relational_expr
'<' shift_expr
1108 struct source_location loc
;
1110 set_location
(&loc
, &@
2);
1111 $$
= &hlsl_lt
($1, $3, &loc
)->node
;
1113 | relational_expr
'>' shift_expr
1115 struct source_location loc
;
1117 set_location
(&loc
, &@
2);
1118 $$
= &hlsl_gt
($1, $3, &loc
)->node
;
1120 | relational_expr OP_LE shift_expr
1122 struct source_location loc
;
1124 set_location
(&loc
, &@
2);
1125 $$
= &hlsl_le
($1, $3, &loc
)->node
;
1127 | relational_expr OP_GE shift_expr
1129 struct source_location loc
;
1131 set_location
(&loc
, &@
2);
1132 $$
= &hlsl_ge
($1, $3, &loc
)->node
;
1135 equality_expr: relational_expr
1139 | equality_expr OP_EQ relational_expr
1141 struct source_location loc
;
1143 set_location
(&loc
, &@
2);
1144 $$
= &hlsl_eq
($1, $3, &loc
)->node
;
1146 | equality_expr OP_NE relational_expr
1148 struct source_location loc
;
1150 set_location
(&loc
, &@
2);
1151 $$
= &hlsl_ne
($1, $3, &loc
)->node
;
1154 bitand_expr: equality_expr
1158 | bitand_expr
'&' equality_expr
1160 FIXME
("bitwise AND\n");
1163 bitxor_expr: bitand_expr
1167 | bitxor_expr
'^' bitand_expr
1169 FIXME
("bitwise XOR\n");
1172 bitor_expr: bitxor_expr
1176 | bitor_expr
'|' bitxor_expr
1178 FIXME
("bitwise OR\n");
1181 logicand_expr: bitor_expr
1185 | logicand_expr OP_AND bitor_expr
1187 FIXME
("logic AND\n");
1190 logicor_expr: logicand_expr
1194 | logicor_expr OP_OR logicand_expr
1196 FIXME
("logic OR\n");
1199 conditional_expr: logicor_expr
1203 | logicor_expr
'?' expr
':' assignment_expr
1205 FIXME
("ternary operator\n");
1208 assignment_expr: conditional_expr
1213 expr: assignment_expr
1217 | expr
',' assignment_expr
1219 FIXME
("Comma expression\n");
1224 static void set_location
(struct source_location
*loc
, const struct YYLTYPE *l
)
1226 loc
->file
= hlsl_ctx.source_file
;
1227 loc
->line
= l
->first_line
;
1228 loc
->col
= l
->first_column
;
1231 static DWORD add_modifier
(DWORD modifiers
, DWORD mod
, const struct YYLTYPE *loc
)
1233 if
(modifiers
& mod
)
1235 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
1236 "modifier '%s' already specified", debug_modifiers
(mod
));
1239 if
(mod
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
)
1240 && modifiers
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
))
1242 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
1243 "more than one matrix majority keyword");
1246 return modifiers | mod
;
1249 struct bwriter_shader
*parse_hlsl
(enum shader_type type
, DWORD major
, DWORD minor
,
1250 const char *entrypoint
, char **messages
)
1252 struct hlsl_ir_function_decl
*function
;
1253 struct hlsl_scope
*scope
, *next_scope
;
1254 struct hlsl_type
*hlsl_type
, *next_type
;
1255 struct hlsl_ir_var
*var
, *next_var
;
1258 hlsl_ctx.status
= PARSE_SUCCESS
;
1259 hlsl_ctx.messages.size
= hlsl_ctx.messages.capacity
= 0;
1260 hlsl_ctx.line_no
= hlsl_ctx.column
= 1;
1261 hlsl_ctx.source_file
= d3dcompiler_strdup
("");
1262 hlsl_ctx.source_files
= d3dcompiler_alloc
(sizeof
(*hlsl_ctx.source_files
));
1263 if
(hlsl_ctx.source_files
)
1264 hlsl_ctx.source_files
[0] = hlsl_ctx.source_file
;
1265 hlsl_ctx.source_files_count
= 1;
1266 hlsl_ctx.cur_scope
= NULL
;
1267 hlsl_ctx.matrix_majority
= HLSL_COLUMN_MAJOR
;
1268 list_init
(&hlsl_ctx.scopes
);
1269 list_init
(&hlsl_ctx.types
);
1270 list_init
(&hlsl_ctx.functions
);
1272 push_scope
(&hlsl_ctx
);
1273 hlsl_ctx.globals
= hlsl_ctx.cur_scope
;
1277 if
(TRACE_ON
(hlsl_parser
))
1279 struct hlsl_ir_function_decl
*func
;
1281 TRACE
("IR dump.\n");
1282 LIST_FOR_EACH_ENTRY
(func
, &hlsl_ctx.functions
, struct hlsl_ir_function_decl
, node.entry
)
1285 debug_dump_ir_function
(func
);
1289 TRACE
("Compilation status = %d\n", hlsl_ctx.status
);
1292 if
(hlsl_ctx.messages.size
)
1293 *messages
= hlsl_ctx.messages.
string;
1299 if
(hlsl_ctx.messages.capacity
)
1300 d3dcompiler_free
(hlsl_ctx.messages.
string);
1303 for
(i
= 0; i
< hlsl_ctx.source_files_count
; ++i
)
1304 d3dcompiler_free
((void *)hlsl_ctx.source_files
[i
]);
1305 d3dcompiler_free
(hlsl_ctx.source_files
);
1307 TRACE
("Freeing functions IR.\n");
1308 LIST_FOR_EACH_ENTRY
(function
, &hlsl_ctx.functions
, struct hlsl_ir_function_decl
, node.entry
)
1309 free_function
(function
);
1311 TRACE
("Freeing variables.\n");
1312 LIST_FOR_EACH_ENTRY_SAFE
(scope
, next_scope
, &hlsl_ctx.scopes
, struct hlsl_scope
, entry
)
1314 LIST_FOR_EACH_ENTRY_SAFE
(var
, next_var
, &scope
->vars
, struct hlsl_ir_var
, scope_entry
)
1316 free_declaration
(var
);
1318 d3dcompiler_free
(scope
);
1321 TRACE
("Freeing types.\n");
1322 LIST_FOR_EACH_ENTRY_SAFE
(hlsl_type
, next_type
, &hlsl_ctx.types
, struct hlsl_type
, entry
)
1324 free_hlsl_type
(hlsl_type
);