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
)
49 static const char * const names
[] =
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 BOOL check_type_modifiers
(DWORD modifiers
, struct source_location
*loc
)
180 if
(modifiers
& ~HLSL_TYPE_MODIFIERS_MASK
)
182 hlsl_report_message
(loc
->file
, loc
->line
, loc
->col
, HLSL_LEVEL_ERROR
,
183 "modifier not allowed on typedefs");
189 static BOOL add_type_to_scope
(struct hlsl_scope
*scope
, struct hlsl_type
*def
)
191 if
(get_type
(scope
, def
->name
, FALSE
))
194 wine_rb_put
(&scope
->types
, def
->name
, &def
->scope_entry
);
198 static void declare_predefined_types
(struct hlsl_scope
*scope
)
200 struct hlsl_type
*type
;
201 unsigned int x
, y
, bt
;
202 static const char * const names
[] =
213 for
(bt
= 0; bt
<= HLSL_TYPE_LAST_SCALAR
; ++bt
)
215 for
(y
= 1; y
<= 4; ++y
)
217 for
(x
= 1; x
<= 4; ++x
)
219 sprintf
(name
, "%s%ux%u", names
[bt
], x
, y
);
220 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_MATRIX
, bt
, x
, y
);
221 add_type_to_scope
(scope
, type
);
225 sprintf
(name
, "%s%u", names
[bt
], x
);
226 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_VECTOR
, bt
, x
, y
);
227 add_type_to_scope
(scope
, type
);
231 sprintf
(name
, "%s", names
[bt
]);
232 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_SCALAR
, bt
, x
, y
);
233 add_type_to_scope
(scope
, type
);
240 /* DX8 effects predefined types */
241 type
= new_hlsl_type
(d3dcompiler_strdup
("DWORD"), HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
242 add_type_to_scope
(scope
, type
);
243 type
= new_hlsl_type
(d3dcompiler_strdup
("FLOAT"), HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
244 add_type_to_scope
(scope
, type
);
245 type
= new_hlsl_type
(d3dcompiler_strdup
("VECTOR"), HLSL_CLASS_VECTOR
, HLSL_TYPE_FLOAT
, 4, 1);
246 add_type_to_scope
(scope
, type
);
247 type
= new_hlsl_type
(d3dcompiler_strdup
("MATRIX"), HLSL_CLASS_MATRIX
, HLSL_TYPE_FLOAT
, 4, 4);
248 add_type_to_scope
(scope
, type
);
249 type
= new_hlsl_type
(d3dcompiler_strdup
("STRING"), HLSL_CLASS_OBJECT
, HLSL_TYPE_STRING
, 1, 1);
250 add_type_to_scope
(scope
, type
);
251 type
= new_hlsl_type
(d3dcompiler_strdup
("TEXTURE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_TEXTURE
, 1, 1);
252 add_type_to_scope
(scope
, type
);
253 type
= new_hlsl_type
(d3dcompiler_strdup
("PIXELSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_PIXELSHADER
, 1, 1);
254 add_type_to_scope
(scope
, type
);
255 type
= new_hlsl_type
(d3dcompiler_strdup
("VERTEXSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VERTEXSHADER
, 1, 1);
256 add_type_to_scope
(scope
, type
);
259 static struct hlsl_ir_if
*loop_condition
(struct list
*cond_list
)
261 struct hlsl_ir_if
*out_cond
;
262 struct hlsl_ir_expr
*not_cond
;
263 struct hlsl_ir_node
*cond
, *operands
[3];
264 struct hlsl_ir_jump
*jump
;
265 unsigned int count
= list_count
(cond_list
);
270 ERR
("Got multiple expressions in a for condition.\n");
272 cond
= LIST_ENTRY
(list_head
(cond_list
), struct hlsl_ir_node
, entry
);
273 out_cond
= d3dcompiler_alloc
(sizeof
(*out_cond
));
276 ERR
("Out of memory.\n");
279 out_cond
->node.type
= HLSL_IR_IF
;
281 operands
[1] = operands
[2] = NULL
;
282 not_cond
= new_expr
(HLSL_IR_UNOP_LOGIC_NOT
, operands
, &cond
->loc
);
285 ERR
("Out of memory.\n");
286 d3dcompiler_free
(out_cond
);
289 out_cond
->condition
= ¬_cond
->node
;
290 jump
= d3dcompiler_alloc
(sizeof
(*jump
));
293 ERR
("Out of memory.\n");
294 d3dcompiler_free
(out_cond
);
295 d3dcompiler_free
(not_cond
);
298 jump
->node.type
= HLSL_IR_JUMP
;
299 jump
->type
= HLSL_IR_JUMP_BREAK
;
300 out_cond
->then_instrs
= d3dcompiler_alloc
(sizeof
(*out_cond
->then_instrs
));
301 if
(!out_cond
->then_instrs
)
303 ERR
("Out of memory.\n");
304 d3dcompiler_free
(out_cond
);
305 d3dcompiler_free
(not_cond
);
306 d3dcompiler_free
(jump
);
309 list_init
(out_cond
->then_instrs
);
310 list_add_head
(out_cond
->then_instrs
, &jump
->node.entry
);
322 static struct list
*create_loop
(enum loop_type type
, struct list
*init
, struct list
*cond
,
323 struct hlsl_ir_node
*iter
, struct list
*body
, struct source_location
*loc
)
325 struct list
*list
= NULL
;
326 struct hlsl_ir_loop
*loop
= NULL
;
327 struct hlsl_ir_if
*cond_jump
= NULL
;
329 list
= d3dcompiler_alloc
(sizeof
(*list
));
335 list_move_head
(list
, init
);
337 loop
= d3dcompiler_alloc
(sizeof
(*loop
));
340 loop
->node.type
= HLSL_IR_LOOP
;
341 loop
->node.loc
= *loc
;
342 list_add_tail
(list
, &loop
->node.entry
);
343 loop
->body
= d3dcompiler_alloc
(sizeof
(*loop
->body
));
346 list_init
(loop
->body
);
348 cond_jump
= loop_condition
(cond
);
352 if
(type
!= LOOP_DO_WHILE
)
353 list_add_tail
(loop
->body
, &cond_jump
->node.entry
);
355 list_move_tail
(loop
->body
, body
);
358 list_add_tail
(loop
->body
, &iter
->entry
);
360 if
(type
== LOOP_DO_WHILE
)
361 list_add_tail
(loop
->body
, &cond_jump
->node.entry
);
363 d3dcompiler_free
(init
);
364 d3dcompiler_free
(cond
);
365 d3dcompiler_free
(body
);
369 ERR
("Out of memory.\n");
371 d3dcompiler_free
(loop
->body
);
372 d3dcompiler_free
(loop
);
373 d3dcompiler_free
(cond_jump
);
374 d3dcompiler_free
(list
);
375 free_instr_list
(init
);
376 free_instr_list
(cond
);
378 free_instr_list
(body
);
382 static unsigned int initializer_size
(struct list
*initializer
)
384 unsigned int count
= 0;
385 struct hlsl_ir_node
*node
;
387 LIST_FOR_EACH_ENTRY
(node
, initializer
, struct hlsl_ir_node
, entry
)
389 count
+= components_count_type
(node
->data_type
);
391 TRACE
("Initializer size = %u\n", count
);
395 static unsigned int components_count_expr_list
(struct list
*list
)
397 struct hlsl_ir_node
*node
;
398 unsigned int count
= 0;
400 LIST_FOR_EACH_ENTRY
(node
, list
, struct hlsl_ir_node
, entry
)
402 count
+= components_count_type
(node
->data_type
);
407 static struct hlsl_ir_swizzle
*new_swizzle
(DWORD s
, unsigned int components
,
408 struct hlsl_ir_node
*val
, struct source_location
*loc
)
410 struct hlsl_ir_swizzle
*swizzle
= d3dcompiler_alloc
(sizeof
(*swizzle
));
414 swizzle
->node.type
= HLSL_IR_SWIZZLE
;
415 swizzle
->node.loc
= *loc
;
416 swizzle
->node.data_type
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, val
->data_type
->base_type
, components
, 1);
418 swizzle
->swizzle
= s
;
422 static struct hlsl_ir_swizzle
*get_swizzle
(struct hlsl_ir_node
*value
, const char *swizzle
,
423 struct source_location
*loc
)
425 unsigned int len
= strlen
(swizzle
), component
= 0;
426 unsigned int i
, set
, swiz
= 0;
429 if
(value
->data_type
->type
== HLSL_CLASS_MATRIX
)
433 unsigned int inc
, x
, y
;
435 if
(len
< 3 || swizzle
[0] != '_')
437 m_swizzle
= swizzle
[1] == 'm';
438 inc
= m_swizzle ?
4 : 3;
440 if
(len % inc || len
> inc
* 4)
443 for
(i
= 0; i
< len
; i
+= inc
)
445 if
(swizzle
[i
] != '_')
449 if
(swizzle
[i
+ 1] != 'm')
451 x
= swizzle
[i
+ 2] - '0';
452 y
= swizzle
[i
+ 3] - '0';
456 x
= swizzle
[i
+ 1] - '1';
457 y
= swizzle
[i
+ 2] - '1';
460 if
(x
>= value
->data_type
->dimx || y
>= value
->data_type
->dimy
)
462 swiz |
= (y
<< 4 | x
) << component
* 8;
465 return new_swizzle
(swiz
, component
, value
, loc
);
472 for
(set
= 0; set
< 2; ++set
)
476 for
(i
= 0; i
< len
; ++i
)
478 char c
[2][4] = {{'x', 'y', 'z', 'w'}, {'r', 'g', 'b', 'a'}};
481 for
(s
= 0; s
< 4; ++s
)
483 if
(swizzle
[i
] == c
[set
][s
])
492 if
(s
>= value
->data_type
->dimx
)
494 swiz |
= s
<< component
* 2;
498 return new_swizzle
(swiz
, component
, value
, loc
);
504 static void struct_var_initializer
(struct list
*list
, struct hlsl_ir_var
*var
, struct list
*initializer
)
506 struct hlsl_type
*type
= var
->node.data_type
;
507 struct hlsl_ir_node
*node
;
508 struct hlsl_struct_field
*field
;
509 struct list
*cur_node
;
510 struct hlsl_ir_node
*assignment
;
511 struct hlsl_ir_deref
*deref
;
513 if
(initializer_size
(initializer
) != components_count_type
(type
))
515 hlsl_report_message
(var
->node.loc.file
, var
->node.loc.line
, var
->node.loc.col
, HLSL_LEVEL_ERROR
,
516 "structure initializer mismatch");
517 free_instr_list
(initializer
);
520 cur_node
= list_head
(initializer
);
522 node
= LIST_ENTRY
(cur_node
, struct hlsl_ir_node
, entry
);
523 LIST_FOR_EACH_ENTRY
(field
, type
->e.elements
, struct hlsl_struct_field
, entry
)
527 d3dcompiler_free
(initializer
);
530 if
(components_count_type
(field
->type
) == components_count_type
(node
->data_type
))
532 deref
= new_record_deref
(&var
->node
, field
);
535 ERR
("Out of memory.\n");
538 deref
->node.loc
= node
->loc
;
539 assignment
= make_assignment
(&deref
->node
, ASSIGN_OP_ASSIGN
, BWRITERSP_WRITEMASK_ALL
, node
);
540 list_add_tail
(list
, &assignment
->entry
);
543 FIXME
("Initializing with \"mismatched\" fields is not supported yet.\n");
544 cur_node
= list_next
(initializer
, cur_node
);
545 node
= LIST_ENTRY
(cur_node
, struct hlsl_ir_node
, entry
);
548 /* Free initializer elements in excess. */
551 struct list
*next
= list_next
(initializer
, cur_node
);
554 node
= LIST_ENTRY
(cur_node
, struct hlsl_ir_node
, entry
);
556 d3dcompiler_free
(initializer
);
559 static struct list
*declare_vars
(struct hlsl_type
*basic_type
, DWORD modifiers
, struct list
*var_list
)
561 struct hlsl_type
*type
;
562 struct parse_variable_def
*v
, *v_next
;
563 struct hlsl_ir_var
*var
;
564 struct hlsl_ir_node
*assignment
;
565 BOOL ret
, local
= TRUE
;
566 struct list
*statements_list
= d3dcompiler_alloc
(sizeof
(*statements_list
));
568 if
(!statements_list
)
570 ERR
("Out of memory.\n");
571 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, var_list
, struct parse_variable_def
, entry
)
573 d3dcompiler_free
(var_list
);
576 list_init
(statements_list
);
579 return statements_list
;
581 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, var_list
, struct parse_variable_def
, entry
)
583 var
= d3dcompiler_alloc
(sizeof
(*var
));
586 ERR
("Out of memory.\n");
590 var
->node.type
= HLSL_IR_VAR
;
592 type
= new_array_type
(basic_type
, v
->array_size
);
595 var
->node.data_type
= type
;
596 var
->node.loc
= v
->loc
;
598 var
->modifiers
= modifiers
;
599 var
->semantic
= v
->semantic
;
600 debug_dump_decl
(type
, modifiers
, v
->name
, v
->loc.line
);
602 if
(hlsl_ctx.cur_scope
== hlsl_ctx.globals
)
604 var
->modifiers |
= HLSL_STORAGE_UNIFORM
;
608 if
(var
->modifiers
& HLSL_MODIFIER_CONST
&& !(var
->modifiers
& HLSL_STORAGE_UNIFORM
) && !v
->initializer
)
610 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
,
611 HLSL_LEVEL_ERROR
, "const variable without initializer");
612 free_declaration
(var
);
617 ret
= declare_variable
(var
, local
);
620 free_declaration
(var
);
624 TRACE
("Declared variable %s.\n", var
->name
);
628 unsigned int size
= initializer_size
(v
->initializer
);
629 struct hlsl_ir_node
*node
;
631 TRACE
("Variable with initializer.\n");
632 if
(type
->type
<= HLSL_CLASS_LAST_NUMERIC
633 && type
->dimx
* type
->dimy
!= size
&& size
!= 1)
635 if
(size
< type
->dimx
* type
->dimy
)
637 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
, HLSL_LEVEL_ERROR
,
638 "'%s' initializer does not match", v
->name
);
639 free_instr_list
(v
->initializer
);
644 if
((type
->type
== HLSL_CLASS_STRUCT || type
->type
== HLSL_CLASS_ARRAY
)
645 && components_count_type
(type
) != size
)
647 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
, HLSL_LEVEL_ERROR
,
648 "'%s' initializer does not match", v
->name
);
649 free_instr_list
(v
->initializer
);
654 if
(type
->type
== HLSL_CLASS_STRUCT
)
656 struct_var_initializer
(statements_list
, var
, v
->initializer
);
660 if
(type
->type
> HLSL_CLASS_LAST_NUMERIC
)
662 FIXME
("Initializers for non scalar/struct variables not supported yet.\n");
663 free_instr_list
(v
->initializer
);
667 if
(v
->array_size
> 0)
669 FIXME
("Initializing arrays is not supported yet.\n");
670 free_instr_list
(v
->initializer
);
674 if
(list_count
(v
->initializer
) > 1)
676 FIXME
("Complex initializers are not supported yet.\n");
677 free_instr_list
(v
->initializer
);
681 node
= LIST_ENTRY
(list_head
(v
->initializer
), struct hlsl_ir_node
, entry
);
682 assignment
= make_assignment
(&var
->node
, ASSIGN_OP_ASSIGN
,
683 BWRITERSP_WRITEMASK_ALL
, node
);
684 list_add_tail
(statements_list
, &assignment
->entry
);
685 d3dcompiler_free
(v
->initializer
);
689 d3dcompiler_free
(var_list
);
690 return statements_list
;
693 static BOOL add_struct_field
(struct list
*fields
, struct hlsl_struct_field
*field
)
695 struct hlsl_struct_field
*f
;
697 LIST_FOR_EACH_ENTRY
(f
, fields
, struct hlsl_struct_field
, entry
)
699 if
(!strcmp
(f
->name
, field
->name
))
702 list_add_tail
(fields
, &field
->entry
);
706 static struct list
*gen_struct_fields
(struct hlsl_type
*type
, DWORD modifiers
, struct list
*fields
)
708 struct parse_variable_def
*v
, *v_next
;
709 struct hlsl_struct_field
*field
;
712 list
= d3dcompiler_alloc
(sizeof
(*list
));
715 ERR
("Out of memory.\n");
719 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, fields
, struct parse_variable_def
, entry
)
721 debug_dump_decl
(type
, 0, v
->name
, v
->loc.line
);
722 field
= d3dcompiler_alloc
(sizeof
(*field
));
725 ERR
("Out of memory.\n");
730 field
->name
= v
->name
;
731 field
->modifiers
= modifiers
;
732 field
->semantic
= v
->semantic
;
735 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
, HLSL_LEVEL_ERROR
,
736 "struct field with an initializer.\n");
737 free_instr_list
(v
->initializer
);
739 list_add_tail
(list
, &field
->entry
);
742 d3dcompiler_free
(fields
);
746 static struct hlsl_type
*new_struct_type
(const char *name
, DWORD modifiers
, struct list
*fields
)
748 struct hlsl_type
*type
= d3dcompiler_alloc
(sizeof
(*type
));
752 ERR
("Out of memory.\n");
755 type
->type
= HLSL_CLASS_STRUCT
;
757 type
->dimx
= type
->dimy
= 1;
758 type
->modifiers
= modifiers
;
759 type
->e.elements
= fields
;
761 list_add_tail
(&hlsl_ctx.types
, &type
->entry
);
766 static BOOL add_typedef
(DWORD modifiers
, struct hlsl_type
*orig_type
, struct list
*list
,
767 struct source_location
*loc
)
770 struct hlsl_type
*type
;
771 struct parse_variable_def
*v
, *v_next
;
773 if
(!check_type_modifiers
(modifiers
, loc
))
775 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, list
, struct parse_variable_def
, entry
)
777 d3dcompiler_free
(list
);
781 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, list
, struct parse_variable_def
, entry
)
784 type
= new_array_type
(orig_type
, v
->array_size
);
786 type
= clone_hlsl_type
(orig_type
);
789 ERR
("Out of memory\n");
792 d3dcompiler_free
((void *)type
->name
);
793 type
->name
= v
->name
;
794 type
->modifiers |
= modifiers
;
796 if
(type
->type
!= HLSL_CLASS_MATRIX
)
797 check_invalid_matrix_modifiers
(type
->modifiers
, &v
->loc
);
799 ret
= add_type_to_scope
(hlsl_ctx.cur_scope
, type
);
802 hlsl_report_message
(v
->loc.file
, v
->loc.line
, v
->loc.col
, HLSL_LEVEL_ERROR
,
803 "redefinition of custom type '%s'", v
->name
);
807 d3dcompiler_free
(list
);
811 static BOOL add_func_parameter
(struct list
*list
, struct parse_parameter
*param
, const struct source_location
*loc
)
813 struct hlsl_ir_var
*decl
= d3dcompiler_alloc
(sizeof
(*decl
));
817 ERR
("Out of memory.\n");
820 decl
->node.type
= HLSL_IR_VAR
;
821 decl
->node.data_type
= param
->type
;
822 decl
->node.loc
= *loc
;
823 decl
->name
= param
->name
;
824 decl
->semantic
= param
->semantic
;
825 decl
->modifiers
= param
->modifiers
;
827 if
(!add_declaration
(hlsl_ctx.cur_scope
, decl
, FALSE
))
829 free_declaration
(decl
);
832 list_add_tail
(list
, &decl
->node.entry
);
836 static const struct hlsl_ir_function_decl
*get_overloaded_func
(struct wine_rb_tree
*funcs
, char *name
,
837 struct list
*params
, BOOL exact_signature
)
839 struct hlsl_ir_function
*func
;
840 struct wine_rb_entry
*entry
;
842 entry
= wine_rb_get
(funcs
, name
);
845 func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function
, entry
);
847 entry
= wine_rb_get
(&func
->overloads
, params
);
850 if
(!exact_signature
)
851 FIXME
("No exact match, search for a compatible overloaded function (if any).\n");
854 return WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function_decl
, entry
);
867 struct hlsl_type
*type
;
873 struct hlsl_ir_var
*var
;
874 struct hlsl_ir_node
*instr
;
876 struct parse_function function
;
877 struct parse_parameter parameter
;
878 struct parse_variable_def
*variable_def
;
879 struct parse_if_body if_body
;
880 enum parse_unary_op unary_op
;
881 enum parse_assign_op assign_op
;
888 %token KW_COLUMN_MAJOR
892 %token KW_DEPTHSTENCILSTATE
893 %token KW_DEPTHSTENCILVIEW
901 %token KW_GEOMETRYSHADER
902 %token KW_GROUPSHARED
909 %token KW_NOINTERPOLATION
912 %token KW_PIXELSHADER
914 %token KW_RASTERIZERSTATE
915 %token KW_RENDERTARGETVIEW
923 %token KW_SAMPLERCUBE
924 %token KW_SAMPLER_STATE
925 %token KW_SAMPLERCOMPARISONSTATE
928 %token KW_STATEBLOCK_STATE
935 %token KW_TECHNIQUE10
938 %token KW_TEXTURE1DARRAY
940 %token KW_TEXTURE2DARRAY
941 %token KW_TEXTURE2DMS
942 %token KW_TEXTURE2DMSARRAY
944 %token KW_TEXTURE3DARRAY
945 %token KW_TEXTURECUBE
950 %token KW_VERTEXSHADER
961 %token OP_LEFTSHIFTASSIGN
963 %token OP_RIGHTSHIFTASSIGN
981 %token
<intval
> PRE_LINE
983 %token
<name
> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
984 %type
<name
> any_identifier var_identifier
986 %token
<floatval
> C_FLOAT
987 %token
<intval
> C_INTEGER
988 %type
<boolval
> boolean
989 %type
<type
> base_type
991 %type
<list
> declaration_statement
992 %type
<list
> declaration
993 %type
<list
> struct_declaration
994 %type
<type
> struct_spec
995 %type
<type
> named_struct_spec
996 %type
<type
> unnamed_struct_spec
997 %type
<list
> type_specs
998 %type
<variable_def
> type_spec
999 %type
<list
> complex_initializer
1000 %type
<list
> initializer_expr_list
1001 %type
<instr
> initializer_expr
1002 %type
<modifiers
> var_modifiers
1004 %type
<list
> parameters
1005 %type
<list
> param_list
1007 %type
<var
> variable
1008 %type
<intval
> array
1009 %type
<list
> statement
1010 %type
<list
> statement_list
1011 %type
<list
> compound_statement
1012 %type
<list
> jump_statement
1013 %type
<list
> selection_statement
1014 %type
<list
> loop_statement
1015 %type
<function
> func_declaration
1016 %type
<function
> func_prototype
1017 %type
<list
> fields_list
1018 %type
<parameter
> parameter
1019 %type
<name
> semantic
1020 %type
<variable_def
> variable_def
1021 %type
<list
> variables_def
1022 %type
<list
> variables_def_optional
1023 %type
<if_body
> if_body
1024 %type
<instr
> primary_expr
1025 %type
<instr
> postfix_expr
1026 %type
<instr
> unary_expr
1027 %type
<instr
> mul_expr
1028 %type
<instr
> add_expr
1029 %type
<instr
> shift_expr
1030 %type
<instr
> relational_expr
1031 %type
<instr
> equality_expr
1032 %type
<instr
> bitand_expr
1033 %type
<instr
> bitxor_expr
1034 %type
<instr
> bitor_expr
1035 %type
<instr
> logicand_expr
1036 %type
<instr
> logicor_expr
1037 %type
<instr
> conditional_expr
1038 %type
<instr
> assignment_expr
1039 %type
<list
> expr_statement
1040 %type
<unary_op
> unary_op
1041 %type
<assign_op
> assign_op
1042 %type
<modifiers
> input_mods
1043 %type
<modifiers
> input_mod
1046 hlsl_prog: /* empty */
1049 | hlsl_prog func_declaration
1051 const struct hlsl_ir_function_decl
*decl
;
1053 decl
= get_overloaded_func
(&hlsl_ctx.functions
, $2.name
, $2.decl
->parameters
, TRUE
);
1054 if
(decl
&& !decl
->func
->intrinsic
)
1056 if
(decl
->body
&& $2.decl
->body
)
1058 hlsl_report_message
($2.decl
->node.loc.file
, $2.decl
->node.loc.line
,
1059 $2.decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
1060 "redefinition of function %s", debugstr_a
($2.name
));
1063 else if
(!compare_hlsl_types
(decl
->node.data_type
, $2.decl
->node.data_type
))
1065 hlsl_report_message
($2.decl
->node.loc.file
, $2.decl
->node.loc.line
,
1066 $2.decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
1067 "redefining function %s with a different return type",
1068 debugstr_a
($2.name
));
1069 hlsl_report_message
(decl
->node.loc.file
, decl
->node.loc.line
, decl
->node.loc.col
, HLSL_LEVEL_NOTE
,
1070 "%s previously declared here",
1071 debugstr_a
($2.name
));
1076 if
($2.decl
->node.data_type
->base_type
== HLSL_TYPE_VOID
&& $2.decl
->semantic
)
1078 hlsl_report_message
($2.decl
->node.loc.file
, $2.decl
->node.loc.line
,
1079 $2.decl
->node.loc.col
, HLSL_LEVEL_ERROR
,
1080 "void function with a semantic");
1083 TRACE
("Adding function '%s' to the function list.\n", $2.name
);
1084 add_function_decl
(&hlsl_ctx.functions
, $2.name
, $2.decl
, FALSE
);
1086 | hlsl_prog declaration_statement
1088 TRACE
("Declaration statement parsed.\n");
1090 | hlsl_prog preproc_directive
1095 TRACE
("Skipping stray semicolon.\n");
1098 preproc_directive: PRE_LINE STRING
1100 const char **new_array
= NULL
;
1102 TRACE
("Updating line information to file %s, line %u\n", debugstr_a
($2), $1);
1103 hlsl_ctx.line_no
= $1;
1104 if
(strcmp
($2, hlsl_ctx.source_file
))
1105 new_array
= d3dcompiler_realloc
(hlsl_ctx.source_files
,
1106 sizeof
(*hlsl_ctx.source_files
) * hlsl_ctx.source_files_count
+ 1);
1110 hlsl_ctx.source_files
= new_array
;
1111 hlsl_ctx.source_files
[hlsl_ctx.source_files_count
++] = $2;
1112 hlsl_ctx.source_file
= $2;
1116 d3dcompiler_free
($2);
1120 struct_declaration: struct_spec variables_def_optional
';'
1122 struct source_location loc
;
1124 set_location
(&loc
, &@
3);
1129 hlsl_report_message
(loc.file
, loc.line
, loc.col
,
1130 HLSL_LEVEL_ERROR
, "anonymous struct declaration with no variables");
1132 check_type_modifiers
($1->modifiers
, &loc
);
1134 $$
= declare_vars
($1, 0, $2);
1137 struct_spec: named_struct_spec
1138 | unnamed_struct_spec
1140 named_struct_spec: var_modifiers KW_STRUCT any_identifier
'{' fields_list
'}'
1143 struct source_location loc
;
1145 TRACE
("Structure %s declaration.\n", debugstr_a
($3));
1146 set_location
(&loc
, &@
1);
1147 check_invalid_matrix_modifiers
($1, &loc
);
1148 $$
= new_struct_type
($3, $1, $5);
1150 if
(get_variable
(hlsl_ctx.cur_scope
, $3))
1152 hlsl_report_message
(hlsl_ctx.source_file
, @
3.first_line
, @
3.first_column
,
1153 HLSL_LEVEL_ERROR
, "redefinition of '%s'", $3);
1157 ret
= add_type_to_scope
(hlsl_ctx.cur_scope
, $$
);
1160 hlsl_report_message
(hlsl_ctx.source_file
, @
3.first_line
, @
3.first_column
,
1161 HLSL_LEVEL_ERROR
, "redefinition of struct '%s'", $3);
1166 unnamed_struct_spec: var_modifiers KW_STRUCT
'{' fields_list
'}'
1168 struct source_location loc
;
1170 TRACE
("Anonymous structure declaration.\n");
1171 set_location
(&loc
, &@
1);
1172 check_invalid_matrix_modifiers
($1, &loc
);
1173 $$
= new_struct_type
(NULL
, $1, $4);
1176 any_identifier: VAR_IDENTIFIER
1180 fields_list: /* Empty */
1182 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1188 struct hlsl_struct_field
*field
, *next
;
1191 LIST_FOR_EACH_ENTRY_SAFE
(field
, next
, $2, struct hlsl_struct_field
, entry
)
1193 ret
= add_struct_field
($$
, field
);
1196 hlsl_report_message
(hlsl_ctx.source_file
, @
2.first_line
, @
2.first_column
,
1197 HLSL_LEVEL_ERROR
, "redefinition of '%s'", field
->name
);
1198 d3dcompiler_free
(field
);
1201 d3dcompiler_free
($2);
1204 field: var_modifiers type variables_def
';'
1206 $$
= gen_struct_fields
($2, $1, $3);
1208 | unnamed_struct_spec variables_def
';'
1210 $$
= gen_struct_fields
($1, 0, $2);
1213 func_declaration: func_prototype compound_statement
1215 TRACE
("Function %s parsed.\n", $1.name
);
1218 pop_scope
(&hlsl_ctx
);
1220 | func_prototype
';'
1222 TRACE
("Function prototype for %s.\n", $1.name
);
1224 pop_scope
(&hlsl_ctx
);
1227 func_prototype: var_modifiers type var_identifier
'(' parameters
')' semantic
1229 if
(get_variable
(hlsl_ctx.globals
, $3))
1231 hlsl_report_message
(hlsl_ctx.source_file
, @
3.first_line
, @
3.first_column
,
1232 HLSL_LEVEL_ERROR
, "redefinition of '%s'\n", $3);
1235 if
($2->base_type
== HLSL_TYPE_VOID
&& $7)
1237 hlsl_report_message
(hlsl_ctx.source_file
, @
7.first_line
, @
7.first_column
,
1238 HLSL_LEVEL_ERROR
, "void function with a semantic");
1241 $$.decl
= new_func_decl
($2, $5);
1244 ERR
("Out of memory.\n");
1248 $$.decl
->semantic
= $7;
1249 set_location
(&$$.decl
->node.loc
, &@
3);
1252 compound_statement: '{' '}'
1254 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1257 |
'{' scope_start statement_list
'}'
1259 pop_scope
(&hlsl_ctx
);
1263 scope_start: /* Empty */
1265 push_scope
(&hlsl_ctx
);
1268 var_identifier: VAR_IDENTIFIER
1271 semantic: /* Empty */
1275 |
':' any_identifier
1280 parameters: scope_start
1282 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1285 | scope_start param_list
1290 param_list: parameter
1292 struct source_location loc
;
1294 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1296 set_location
(&loc
, &@
1);
1297 if
(!add_func_parameter
($$
, &$1, &loc
))
1299 ERR
("Error adding function parameter %s.\n", $1.name
);
1300 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1304 | param_list
',' parameter
1306 struct source_location loc
;
1309 set_location
(&loc
, &@
3);
1310 if
(!add_func_parameter
($$
, &$3, &loc
))
1312 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1313 "duplicate parameter %s", $3.name
);
1318 parameter: input_mods var_modifiers type any_identifier semantic
1320 $$.modifiers
= $1 ?
$1 : HLSL_MODIFIER_IN
;
1327 input_mods: /* Empty */
1331 | input_mods input_mod
1335 hlsl_report_message
(hlsl_ctx.source_file
, @
2.first_line
, @
2.first_column
,
1336 HLSL_LEVEL_ERROR
, "duplicate input-output modifiers");
1344 $$
= HLSL_MODIFIER_IN
;
1348 $$
= HLSL_MODIFIER_OUT
;
1352 $$
= HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT
;
1359 | KW_VECTOR
'<' base_type
',' C_INTEGER
'>'
1361 if
($3->type
!= HLSL_CLASS_SCALAR
)
1363 hlsl_message
("Line %u: vectors of non-scalar types are not allowed.\n",
1365 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1368 if
($5 < 1 ||
$5 > 4)
1370 hlsl_message
("Line %u: vector size must be between 1 and 4.\n",
1372 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1376 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, $3->base_type
, $5, 1);
1378 | KW_MATRIX
'<' base_type
',' C_INTEGER
',' C_INTEGER
'>'
1380 if
($3->type
!= HLSL_CLASS_SCALAR
)
1382 hlsl_message
("Line %u: matrices of non-scalar types are not allowed.\n",
1384 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1387 if
($5 < 1 ||
$5 > 4 ||
$7 < 1 ||
$7 > 4)
1389 hlsl_message
("Line %u: matrix dimensions must be between 1 and 4.\n",
1391 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1395 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_MATRIX
, $3->base_type
, $5, $7);
1400 $$
= new_hlsl_type
(d3dcompiler_strdup
("void"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VOID
, 1, 1);
1404 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
1405 $$
->sampler_dim
= HLSL_SAMPLER_DIM_GENERIC
;
1409 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler1D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
1410 $$
->sampler_dim
= HLSL_SAMPLER_DIM_1D
;
1414 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler2D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
1415 $$
->sampler_dim
= HLSL_SAMPLER_DIM_2D
;
1419 $$
= new_hlsl_type
(d3dcompiler_strdup
("sampler3D"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
1420 $$
->sampler_dim
= HLSL_SAMPLER_DIM_3D
;
1424 $$
= new_hlsl_type
(d3dcompiler_strdup
("samplerCUBE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
1425 $$
->sampler_dim
= HLSL_SAMPLER_DIM_CUBE
;
1429 struct hlsl_type
*type
;
1431 type
= get_type
(hlsl_ctx.cur_scope
, $1, TRUE
);
1433 d3dcompiler_free
($1);
1435 | KW_STRUCT TYPE_IDENTIFIER
1437 struct hlsl_type
*type
;
1439 type
= get_type
(hlsl_ctx.cur_scope
, $2, TRUE
);
1440 if
(type
->type
!= HLSL_CLASS_STRUCT
)
1442 hlsl_message
("Line %u: redefining %s as a structure.\n",
1443 hlsl_ctx.line_no
, $2);
1444 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1450 d3dcompiler_free
($2);
1453 declaration_statement: declaration
1454 | struct_declaration
1457 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1460 ERR
("Out of memory\n");
1466 typedef: KW_TYPEDEF var_modifiers type type_specs
';'
1468 struct source_location loc
;
1470 set_location
(&loc
, &@
1);
1471 if
(!add_typedef
($2, $3, $4, &loc
))
1474 | KW_TYPEDEF struct_spec type_specs
';'
1476 struct source_location loc
;
1478 set_location
(&loc
, &@
1);
1479 if
(!add_typedef
(0, $2, $3, &loc
))
1483 type_specs: type_spec
1485 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1487 list_add_head
($$
, &$1->entry
);
1489 | type_specs
',' type_spec
1492 list_add_tail
($$
, &$3->entry
);
1495 type_spec: any_identifier array
1497 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1498 set_location
(&$$
->loc
, &@
1);
1500 $$
->array_size
= $2;
1503 declaration: var_modifiers type variables_def
';'
1505 $$
= declare_vars
($2, $1, $3);
1508 variables_def_optional: /* Empty */
1517 variables_def: variable_def
1519 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1521 list_add_head
($$
, &$1->entry
);
1523 | variables_def
',' variable_def
1526 list_add_tail
($$
, &$3->entry
);
1529 variable_def: any_identifier array semantic
1531 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1532 set_location
(&$$
->loc
, &@
1);
1534 $$
->array_size
= $2;
1537 | any_identifier array semantic
'=' complex_initializer
1539 TRACE
("Declaration with initializer.\n");
1540 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1541 set_location
(&$$
->loc
, &@
1);
1543 $$
->array_size
= $2;
1545 $$
->initializer
= $5;
1559 var_modifiers: /* Empty */
1563 | KW_EXTERN var_modifiers
1565 $$
= add_modifier
($2, HLSL_STORAGE_EXTERN
, &@
1);
1567 | KW_NOINTERPOLATION var_modifiers
1569 $$
= add_modifier
($2, HLSL_STORAGE_NOINTERPOLATION
, &@
1);
1571 | KW_PRECISE var_modifiers
1573 $$
= add_modifier
($2, HLSL_MODIFIER_PRECISE
, &@
1);
1575 | KW_SHARED var_modifiers
1577 $$
= add_modifier
($2, HLSL_STORAGE_SHARED
, &@
1);
1579 | KW_GROUPSHARED var_modifiers
1581 $$
= add_modifier
($2, HLSL_STORAGE_GROUPSHARED
, &@
1);
1583 | KW_STATIC var_modifiers
1585 $$
= add_modifier
($2, HLSL_STORAGE_STATIC
, &@
1);
1587 | KW_UNIFORM var_modifiers
1589 $$
= add_modifier
($2, HLSL_STORAGE_UNIFORM
, &@
1);
1591 | KW_VOLATILE var_modifiers
1593 $$
= add_modifier
($2, HLSL_STORAGE_VOLATILE
, &@
1);
1595 | KW_CONST var_modifiers
1597 $$
= add_modifier
($2, HLSL_MODIFIER_CONST
, &@
1);
1599 | KW_ROW_MAJOR var_modifiers
1601 $$
= add_modifier
($2, HLSL_MODIFIER_ROW_MAJOR
, &@
1);
1603 | KW_COLUMN_MAJOR var_modifiers
1605 $$
= add_modifier
($2, HLSL_MODIFIER_COLUMN_MAJOR
, &@
1);
1608 complex_initializer: initializer_expr
1610 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1612 list_add_head
($$
, &$1->entry
);
1614 |
'{' initializer_expr_list
'}'
1618 |
'{' initializer_expr_list
',' '}'
1623 initializer_expr: assignment_expr
1628 initializer_expr_list: initializer_expr
1630 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1632 list_add_head
($$
, &$1->entry
);
1634 | initializer_expr_list
',' initializer_expr
1637 list_add_tail
($$
, &$3->entry
);
1649 statement_list: statement
1653 | statement_list statement
1656 list_move_tail
($$
, $2);
1657 d3dcompiler_free
($2);
1660 statement: declaration_statement
1662 | compound_statement
1664 | selection_statement
1667 /* FIXME: add rule for return with no value */
1668 jump_statement: KW_RETURN expr
';'
1670 struct hlsl_ir_jump
*jump
= d3dcompiler_alloc
(sizeof
(*jump
));
1673 ERR
("Out of memory\n");
1676 jump
->node.type
= HLSL_IR_JUMP
;
1677 set_location
(&jump
->node.loc
, &@
1);
1678 jump
->type
= HLSL_IR_JUMP_RETURN
;
1679 jump
->node.data_type
= $2->data_type
;
1680 jump
->return_value
= $2;
1682 FIXME
("Check for valued return on void function.\n");
1683 FIXME
("Implicit conversion to the return type if needed, "
1684 "error out if conversion not possible.\n");
1686 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1688 list_add_tail
($$
, &jump
->node.entry
);
1691 selection_statement: KW_IF
'(' expr
')' if_body
1693 struct hlsl_ir_if
*instr
= d3dcompiler_alloc
(sizeof
(*instr
));
1696 ERR
("Out of memory\n");
1699 instr
->node.type
= HLSL_IR_IF
;
1700 set_location
(&instr
->node.loc
, &@
1);
1701 instr
->condition
= $3;
1702 instr
->then_instrs
= $5.then_instrs
;
1703 instr
->else_instrs
= $5.else_instrs
;
1704 if
($3->data_type
->dimx
> 1 ||
$3->data_type
->dimy
> 1)
1706 hlsl_report_message
(instr
->node.loc.file
, instr
->node.loc.line
,
1707 instr
->node.loc.col
, HLSL_LEVEL_ERROR
,
1708 "if condition requires a scalar");
1710 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1712 list_add_head
($$
, &instr
->node.entry
);
1717 $$.then_instrs
= $1;
1718 $$.else_instrs
= NULL
;
1720 | statement KW_ELSE statement
1722 $$.then_instrs
= $1;
1723 $$.else_instrs
= $3;
1726 loop_statement: KW_WHILE
'(' expr
')' statement
1728 struct source_location loc
;
1729 struct list
*cond
= d3dcompiler_alloc
(sizeof
(*cond
));
1733 ERR
("Out of memory.\n");
1737 list_add_head
(cond
, &$3->entry
);
1738 set_location
(&loc
, &@
1);
1739 $$
= create_loop
(LOOP_WHILE
, NULL
, cond
, NULL
, $5, &loc
);
1741 | KW_DO statement KW_WHILE
'(' expr
')' ';'
1743 struct source_location loc
;
1744 struct list
*cond
= d3dcompiler_alloc
(sizeof
(*cond
));
1748 ERR
("Out of memory.\n");
1752 list_add_head
(cond
, &$5->entry
);
1753 set_location
(&loc
, &@
1);
1754 $$
= create_loop
(LOOP_DO_WHILE
, NULL
, cond
, NULL
, $2, &loc
);
1756 | KW_FOR
'(' scope_start expr_statement expr_statement expr
')' statement
1758 struct source_location loc
;
1760 set_location
(&loc
, &@
1);
1761 $$
= create_loop
(LOOP_FOR
, $4, $5, $6, $8, &loc
);
1762 pop_scope
(&hlsl_ctx
);
1764 | KW_FOR
'(' scope_start declaration expr_statement expr
')' statement
1766 struct source_location loc
;
1768 set_location
(&loc
, &@
1);
1770 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_WARNING
,
1771 "no expressions in for loop initializer");
1772 $$
= create_loop
(LOOP_FOR
, $4, $5, $6, $8, &loc
);
1773 pop_scope
(&hlsl_ctx
);
1778 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1783 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1786 list_add_head
($$
, &$1->entry
);
1789 primary_expr: C_FLOAT
1791 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1794 ERR
("Out of memory.\n");
1797 c
->node.type
= HLSL_IR_CONSTANT
;
1798 set_location
(&c
->node.loc
, &yylloc);
1799 c
->node.data_type
= new_hlsl_type
(d3dcompiler_strdup
("float"), HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
1800 c
->v.value.f
[0] = $1;
1805 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1808 ERR
("Out of memory.\n");
1811 c
->node.type
= HLSL_IR_CONSTANT
;
1812 set_location
(&c
->node.loc
, &yylloc);
1813 c
->node.data_type
= new_hlsl_type
(d3dcompiler_strdup
("int"), HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
1814 c
->v.value.i
[0] = $1;
1819 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
1822 ERR
("Out of memory.\n");
1825 c
->node.type
= HLSL_IR_CONSTANT
;
1826 set_location
(&c
->node.loc
, &yylloc);
1827 c
->node.data_type
= new_hlsl_type
(d3dcompiler_strdup
("bool"), HLSL_CLASS_SCALAR
, HLSL_TYPE_BOOL
, 1, 1);
1828 c
->v.value.b
[0] = $1;
1833 struct hlsl_ir_deref
*deref
= new_var_deref
($1);
1837 set_location
(&$$
->loc
, &@
1);
1847 variable: VAR_IDENTIFIER
1849 struct hlsl_ir_var
*var
;
1850 var
= get_variable
(hlsl_ctx.cur_scope
, $1);
1853 hlsl_message
("Line %d: variable '%s' not declared\n",
1854 hlsl_ctx.line_no
, $1);
1855 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1861 postfix_expr: primary_expr
1865 | postfix_expr OP_INC
1867 struct hlsl_ir_node
*operands
[3];
1868 struct source_location loc
;
1870 set_location
(&loc
, &@
2);
1871 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1873 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1874 "modifying a const expression");
1878 operands
[1] = operands
[2] = NULL
;
1879 $$
= &new_expr
(HLSL_IR_UNOP_POSTINC
, operands
, &loc
)->node
;
1880 /* Post increment/decrement expressions are considered const */
1881 $$
->data_type
= clone_hlsl_type
($$
->data_type
);
1882 $$
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
1884 | postfix_expr OP_DEC
1886 struct hlsl_ir_node
*operands
[3];
1887 struct source_location loc
;
1889 set_location
(&loc
, &@
2);
1890 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
1892 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1893 "modifying a const expression");
1897 operands
[1] = operands
[2] = NULL
;
1898 $$
= &new_expr
(HLSL_IR_UNOP_POSTDEC
, operands
, &loc
)->node
;
1899 /* Post increment/decrement expressions are considered const */
1900 $$
->data_type
= clone_hlsl_type
($$
->data_type
);
1901 $$
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
1903 | postfix_expr
'.' any_identifier
1905 struct source_location loc
;
1907 set_location
(&loc
, &@
2);
1908 if
($1->data_type
->type
== HLSL_CLASS_STRUCT
)
1910 struct hlsl_type
*type
= $1->data_type
;
1911 struct hlsl_struct_field
*field
;
1914 LIST_FOR_EACH_ENTRY
(field
, type
->e.elements
, struct hlsl_struct_field
, entry
)
1916 if
(!strcmp
($3, field
->name
))
1918 struct hlsl_ir_deref
*deref
= new_record_deref
($1, field
);
1922 ERR
("Out of memory\n");
1925 deref
->node.loc
= loc
;
1932 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1933 "invalid subscript %s", debugstr_a
($3));
1937 else if
($1->data_type
->type
<= HLSL_CLASS_LAST_NUMERIC
)
1939 struct hlsl_ir_swizzle
*swizzle
;
1941 swizzle
= get_swizzle
($1, $3, &loc
);
1944 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1945 "invalid swizzle %s", debugstr_a
($3));
1948 $$
= &swizzle
->node
;
1952 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1953 "invalid subscript %s", debugstr_a
($3));
1957 | postfix_expr
'[' expr
']'
1959 /* This may be an array dereference or a vector/matrix
1960 * subcomponent access.
1961 * We store it as an array dereference in any case. */
1962 struct hlsl_ir_deref
*deref
= d3dcompiler_alloc
(sizeof
(*deref
));
1963 struct hlsl_type
*expr_type
= $1->data_type
;
1964 struct source_location loc
;
1966 TRACE
("Array dereference from type %s\n", debug_hlsl_type
(expr_type
));
1969 ERR
("Out of memory\n");
1972 deref
->node.type
= HLSL_IR_DEREF
;
1973 set_location
(&loc
, &@
2);
1974 deref
->node.loc
= loc
;
1975 if
(expr_type
->type
== HLSL_CLASS_ARRAY
)
1977 deref
->node.data_type
= expr_type
->e.array.type
;
1979 else if
(expr_type
->type
== HLSL_CLASS_MATRIX
)
1981 deref
->node.data_type
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, expr_type
->base_type
, expr_type
->dimx
, 1);
1983 else if
(expr_type
->type
== HLSL_CLASS_VECTOR
)
1985 deref
->node.data_type
= new_hlsl_type
(NULL
, HLSL_CLASS_SCALAR
, expr_type
->base_type
, 1, 1);
1989 if
(expr_type
->type
== HLSL_CLASS_SCALAR
)
1990 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1991 "array-indexed expression is scalar");
1993 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
1994 "expression is not array-indexable");
1995 d3dcompiler_free
(deref
);
2000 if
($3->data_type
->type
!= HLSL_CLASS_SCALAR
)
2002 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2003 "array index is not scalar");
2004 d3dcompiler_free
(deref
);
2009 deref
->type
= HLSL_IR_DEREF_ARRAY
;
2010 deref
->v.array.array
= $1;
2011 deref
->v.array.index
= $3;
2015 /* "var_modifiers" doesn't make sense in this case, but it's needed
2016 in the grammar to avoid shift/reduce conflicts. */
2017 | var_modifiers type
'(' initializer_expr_list
')'
2019 struct hlsl_ir_constructor
*constructor
;
2021 TRACE
("%s constructor.\n", debug_hlsl_type
($2));
2024 hlsl_message
("Line %u: unexpected modifier in a constructor.\n",
2026 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
2029 if
($2->type
> HLSL_CLASS_LAST_NUMERIC
)
2031 hlsl_message
("Line %u: constructors are allowed only for numeric data types.\n",
2033 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
2036 if
($2->dimx
* $2->dimy
!= components_count_expr_list
($4))
2038 hlsl_message
("Line %u: wrong number of components in constructor.\n",
2040 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
2044 constructor
= d3dcompiler_alloc
(sizeof
(*constructor
));
2045 constructor
->node.type
= HLSL_IR_CONSTRUCTOR
;
2046 set_location
(&constructor
->node.loc
, &@
3);
2047 constructor
->node.data_type
= $2;
2048 constructor
->arguments
= $4;
2050 $$
= &constructor
->node
;
2053 unary_expr: postfix_expr
2059 struct hlsl_ir_node
*operands
[3];
2060 struct source_location loc
;
2062 set_location
(&loc
, &@
1);
2063 if
($2->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2065 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2066 "modifying a const expression");
2070 operands
[1] = operands
[2] = NULL
;
2071 $$
= &new_expr
(HLSL_IR_UNOP_PREINC
, operands
, &loc
)->node
;
2075 struct hlsl_ir_node
*operands
[3];
2076 struct source_location loc
;
2078 set_location
(&loc
, &@
1);
2079 if
($2->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2081 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2082 "modifying a const expression");
2086 operands
[1] = operands
[2] = NULL
;
2087 $$
= &new_expr
(HLSL_IR_UNOP_PREDEC
, operands
, &loc
)->node
;
2089 | unary_op unary_expr
2091 enum hlsl_ir_expr_op ops
[] = {0, HLSL_IR_UNOP_NEG
,
2092 HLSL_IR_UNOP_LOGIC_NOT
, HLSL_IR_UNOP_BIT_NOT
};
2093 struct hlsl_ir_node
*operands
[3];
2094 struct source_location loc
;
2096 if
($1 == UNARY_OP_PLUS
)
2103 operands
[1] = operands
[2] = NULL
;
2104 set_location
(&loc
, &@
1);
2105 $$
= &new_expr
(ops
[$1], operands
, &loc
)->node
;
2108 /* var_modifiers just to avoid shift/reduce conflicts */
2109 |
'(' var_modifiers type array
')' unary_expr
2111 struct hlsl_ir_expr
*expr
;
2112 struct hlsl_type
*src_type
= $6->data_type
;
2113 struct hlsl_type
*dst_type
;
2114 struct source_location loc
;
2116 set_location
(&loc
, &@
3);
2119 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2120 "unexpected modifier in a cast");
2125 dst_type
= new_array_type
($3, $4);
2129 if
(!compatible_data_types
(src_type
, dst_type
))
2131 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2132 "can't cast from %s to %s",
2133 debug_hlsl_type
(src_type
), debug_hlsl_type
(dst_type
));
2137 expr
= new_cast
($6, dst_type
, &loc
);
2138 $$
= expr ?
&expr
->node
: NULL
;
2147 $$
= UNARY_OP_MINUS
;
2151 $$
= UNARY_OP_LOGICNOT
;
2155 $$
= UNARY_OP_BITNOT
;
2158 mul_expr: unary_expr
2162 | mul_expr
'*' unary_expr
2164 struct source_location loc
;
2166 set_location
(&loc
, &@
2);
2167 $$
= &hlsl_mul
($1, $3, &loc
)->node
;
2169 | mul_expr
'/' unary_expr
2171 struct source_location loc
;
2173 set_location
(&loc
, &@
2);
2174 $$
= &hlsl_div
($1, $3, &loc
)->node
;
2176 | mul_expr
'%' unary_expr
2178 struct source_location loc
;
2180 set_location
(&loc
, &@
2);
2181 $$
= &hlsl_mod
($1, $3, &loc
)->node
;
2188 | add_expr
'+' mul_expr
2190 struct source_location loc
;
2192 set_location
(&loc
, &@
2);
2193 $$
= &hlsl_add
($1, $3, &loc
)->node
;
2195 | add_expr
'-' mul_expr
2197 struct source_location loc
;
2199 set_location
(&loc
, &@
2);
2200 $$
= &hlsl_sub
($1, $3, &loc
)->node
;
2203 shift_expr: add_expr
2207 | shift_expr OP_LEFTSHIFT add_expr
2209 FIXME
("Left shift\n");
2211 | shift_expr OP_RIGHTSHIFT add_expr
2213 FIXME
("Right shift\n");
2216 relational_expr: shift_expr
2220 | relational_expr
'<' shift_expr
2222 struct source_location loc
;
2224 set_location
(&loc
, &@
2);
2225 $$
= &hlsl_lt
($1, $3, &loc
)->node
;
2227 | relational_expr
'>' shift_expr
2229 struct source_location loc
;
2231 set_location
(&loc
, &@
2);
2232 $$
= &hlsl_gt
($1, $3, &loc
)->node
;
2234 | relational_expr OP_LE shift_expr
2236 struct source_location loc
;
2238 set_location
(&loc
, &@
2);
2239 $$
= &hlsl_le
($1, $3, &loc
)->node
;
2241 | relational_expr OP_GE shift_expr
2243 struct source_location loc
;
2245 set_location
(&loc
, &@
2);
2246 $$
= &hlsl_ge
($1, $3, &loc
)->node
;
2249 equality_expr: relational_expr
2253 | equality_expr OP_EQ relational_expr
2255 struct source_location loc
;
2257 set_location
(&loc
, &@
2);
2258 $$
= &hlsl_eq
($1, $3, &loc
)->node
;
2260 | equality_expr OP_NE relational_expr
2262 struct source_location loc
;
2264 set_location
(&loc
, &@
2);
2265 $$
= &hlsl_ne
($1, $3, &loc
)->node
;
2268 bitand_expr: equality_expr
2272 | bitand_expr
'&' equality_expr
2274 FIXME
("bitwise AND\n");
2277 bitxor_expr: bitand_expr
2281 | bitxor_expr
'^' bitand_expr
2283 FIXME
("bitwise XOR\n");
2286 bitor_expr: bitxor_expr
2290 | bitor_expr
'|' bitxor_expr
2292 FIXME
("bitwise OR\n");
2295 logicand_expr: bitor_expr
2299 | logicand_expr OP_AND bitor_expr
2301 FIXME
("logic AND\n");
2304 logicor_expr: logicand_expr
2308 | logicor_expr OP_OR logicand_expr
2310 FIXME
("logic OR\n");
2313 conditional_expr: logicor_expr
2317 | logicor_expr
'?' expr
':' assignment_expr
2319 FIXME
("ternary operator\n");
2322 assignment_expr: conditional_expr
2326 | unary_expr assign_op assignment_expr
2328 struct source_location loc
;
2330 set_location
(&loc
, &@
2);
2331 if
($1->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2333 hlsl_report_message
(loc.file
, loc.line
, loc.col
, HLSL_LEVEL_ERROR
,
2334 "l-value is const");
2337 $$
= make_assignment
($1, $2, BWRITERSP_WRITEMASK_ALL
, $3);
2345 $$
= ASSIGN_OP_ASSIGN
;
2367 | OP_LEFTSHIFTASSIGN
2369 $$
= ASSIGN_OP_LSHIFT
;
2371 | OP_RIGHTSHIFTASSIGN
2373 $$
= ASSIGN_OP_RSHIFT
;
2388 expr: assignment_expr
2392 | expr
',' assignment_expr
2394 FIXME
("Comma expression\n");
2399 static void set_location
(struct source_location
*loc
, const struct YYLTYPE *l
)
2401 loc
->file
= hlsl_ctx.source_file
;
2402 loc
->line
= l
->first_line
;
2403 loc
->col
= l
->first_column
;
2406 static DWORD add_modifier
(DWORD modifiers
, DWORD mod
, const struct YYLTYPE *loc
)
2408 if
(modifiers
& mod
)
2410 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
2411 "modifier '%s' already specified", debug_modifiers
(mod
));
2414 if
(mod
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
)
2415 && modifiers
& (HLSL_MODIFIER_ROW_MAJOR | HLSL_MODIFIER_COLUMN_MAJOR
))
2417 hlsl_report_message
(hlsl_ctx.source_file
, loc
->first_line
, loc
->first_column
, HLSL_LEVEL_ERROR
,
2418 "more than one matrix majority keyword");
2421 return modifiers | mod
;
2424 static void dump_function_decl
(struct wine_rb_entry
*entry
, void *context
)
2426 struct hlsl_ir_function_decl
*func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function_decl
, entry
);
2428 debug_dump_ir_function_decl
(func
);
2431 static void dump_function
(struct wine_rb_entry
*entry
, void *context
)
2433 struct hlsl_ir_function
*func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function
, entry
);
2434 wine_rb_for_each_entry
(&func
->overloads
, dump_function_decl
, NULL
);
2437 struct bwriter_shader
*parse_hlsl
(enum shader_type type
, DWORD major
, DWORD minor
,
2438 const char *entrypoint
, char **messages
)
2440 struct hlsl_scope
*scope
, *next_scope
;
2441 struct hlsl_type
*hlsl_type
, *next_type
;
2442 struct hlsl_ir_var
*var
, *next_var
;
2445 hlsl_ctx.status
= PARSE_SUCCESS
;
2446 hlsl_ctx.messages.size
= hlsl_ctx.messages.capacity
= 0;
2447 hlsl_ctx.line_no
= hlsl_ctx.column
= 1;
2448 hlsl_ctx.source_file
= d3dcompiler_strdup
("");
2449 hlsl_ctx.source_files
= d3dcompiler_alloc
(sizeof
(*hlsl_ctx.source_files
));
2450 if
(hlsl_ctx.source_files
)
2451 hlsl_ctx.source_files
[0] = hlsl_ctx.source_file
;
2452 hlsl_ctx.source_files_count
= 1;
2453 hlsl_ctx.cur_scope
= NULL
;
2454 hlsl_ctx.matrix_majority
= HLSL_COLUMN_MAJOR
;
2455 list_init
(&hlsl_ctx.scopes
);
2456 list_init
(&hlsl_ctx.types
);
2457 init_functions_tree
(&hlsl_ctx.functions
);
2459 push_scope
(&hlsl_ctx
);
2460 hlsl_ctx.globals
= hlsl_ctx.cur_scope
;
2461 declare_predefined_types
(hlsl_ctx.globals
);
2465 if
(TRACE_ON
(hlsl_parser
))
2467 TRACE
("IR dump.\n");
2468 wine_rb_for_each_entry
(&hlsl_ctx.functions
, dump_function
, NULL
);
2471 TRACE
("Compilation status = %d\n", hlsl_ctx.status
);
2474 if
(hlsl_ctx.messages.size
)
2475 *messages
= hlsl_ctx.messages.
string;
2481 if
(hlsl_ctx.messages.capacity
)
2482 d3dcompiler_free
(hlsl_ctx.messages.
string);
2485 for
(i
= 0; i
< hlsl_ctx.source_files_count
; ++i
)
2486 d3dcompiler_free
((void *)hlsl_ctx.source_files
[i
]);
2487 d3dcompiler_free
(hlsl_ctx.source_files
);
2489 TRACE
("Freeing functions IR.\n");
2490 wine_rb_destroy
(&hlsl_ctx.functions
, free_function_rb
, NULL
);
2492 TRACE
("Freeing variables.\n");
2493 LIST_FOR_EACH_ENTRY_SAFE
(scope
, next_scope
, &hlsl_ctx.scopes
, struct hlsl_scope
, entry
)
2495 LIST_FOR_EACH_ENTRY_SAFE
(var
, next_var
, &scope
->vars
, struct hlsl_ir_var
, scope_entry
)
2497 free_declaration
(var
);
2499 wine_rb_destroy
(&scope
->types
, NULL
, NULL
);
2500 d3dcompiler_free
(scope
);
2503 TRACE
("Freeing types.\n");
2504 LIST_FOR_EACH_ENTRY_SAFE
(hlsl_type
, next_type
, &hlsl_ctx.types
, struct hlsl_type
, entry
)
2506 free_hlsl_type
(hlsl_type
);