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
22 #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 struct source_location get_location
(const struct YYLTYPE *l
);
38 void WINAPIV hlsl_message
(const char *fmt
, ...
)
42 __ms_va_start
(args
, 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 WINAPIV hlsl_report_message
(const struct source_location loc
,
59 enum hlsl_error_level level
, const char *fmt
, ...
)
67 __ms_va_start
(args
, 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", loc.file
, loc.line
, loc.col
,
91 hlsl_get_error_level_name
(level
), string);
92 d3dcompiler_free
(string);
94 if
(level
== HLSL_LEVEL_ERROR
)
95 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
96 else if
(level
== HLSL_LEVEL_WARNING
)
97 set_parse_status
(&hlsl_ctx.status
, PARSE_WARN
);
100 static void hlsl_error
(const char *s
)
102 const struct source_location loc
=
104 .file
= hlsl_ctx.source_file
,
105 .line
= hlsl_ctx.line_no
,
106 .col
= hlsl_ctx.column
,
108 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "%s", s
);
111 static void debug_dump_decl
(struct hlsl_type
*type
, DWORD modifiers
, const char *declname
, unsigned int line_no
)
113 TRACE
("Line %u: ", line_no
);
115 TRACE
("%s ", debug_modifiers
(modifiers
));
116 TRACE
("%s %s;\n", debug_hlsl_type
(type
), declname
);
119 static void check_invalid_matrix_modifiers
(DWORD modifiers
, struct source_location loc
)
121 if
(modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
)
123 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
,
124 "'row_major' or 'column_major' modifiers are only allowed for matrices");
128 static BOOL type_is_single_reg
(const struct hlsl_type
*type
)
130 return type
->type
== HLSL_CLASS_SCALAR || type
->type
== HLSL_CLASS_VECTOR
;
133 static BOOL declare_variable
(struct hlsl_ir_var
*decl
, BOOL local
)
137 TRACE
("Declaring variable %s.\n", decl
->name
);
138 if
(decl
->data_type
->type
!= HLSL_CLASS_MATRIX
)
139 check_invalid_matrix_modifiers
(decl
->modifiers
, decl
->loc
);
143 DWORD invalid
= decl
->modifiers
& (HLSL_STORAGE_EXTERN | HLSL_STORAGE_SHARED
144 | HLSL_STORAGE_GROUPSHARED | HLSL_STORAGE_UNIFORM
);
147 hlsl_report_message
(decl
->loc
, HLSL_LEVEL_ERROR
,
148 "modifier '%s' invalid for local variables", debug_modifiers
(invalid
));
152 hlsl_report_message
(decl
->loc
, HLSL_LEVEL_ERROR
,
153 "semantics are not allowed on local variables");
159 if
(find_function
(decl
->name
))
161 hlsl_report_message
(decl
->loc
, HLSL_LEVEL_ERROR
, "redefinition of '%s'", decl
->name
);
165 ret
= add_declaration
(hlsl_ctx.cur_scope
, decl
, local
);
168 struct hlsl_ir_var
*old
= get_variable
(hlsl_ctx.cur_scope
, decl
->name
);
170 hlsl_report_message
(decl
->loc
, HLSL_LEVEL_ERROR
, "\"%s\" already declared", decl
->name
);
171 hlsl_report_message
(old
->loc
, HLSL_LEVEL_NOTE
, "\"%s\" was previously declared here", old
->name
);
177 static DWORD add_modifiers
(DWORD modifiers
, DWORD mod
, const struct source_location loc
)
181 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "modifier '%s' already specified", debug_modifiers
(mod
));
184 if
((mod
& HLSL_MODIFIERS_MAJORITY_MASK
) && (modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
))
186 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "more than one matrix majority keyword");
189 return modifiers | mod
;
192 static BOOL add_type_to_scope
(struct hlsl_scope
*scope
, struct hlsl_type
*def
)
194 if
(get_type
(scope
, def
->name
, FALSE
))
197 wine_rb_put
(&scope
->types
, def
->name
, &def
->scope_entry
);
201 static void declare_predefined_types
(struct hlsl_scope
*scope
)
203 struct hlsl_type
*type
;
204 unsigned int x
, y
, bt
;
205 static const char * const names
[] =
216 static const char *const sampler_names
[] =
225 for
(bt
= 0; bt
<= HLSL_TYPE_LAST_SCALAR
; ++bt
)
227 for
(y
= 1; y
<= 4; ++y
)
229 for
(x
= 1; x
<= 4; ++x
)
231 sprintf
(name
, "%s%ux%u", names
[bt
], y
, x
);
232 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_MATRIX
, bt
, x
, y
);
233 add_type_to_scope
(scope
, type
);
237 sprintf
(name
, "%s%u", names
[bt
], x
);
238 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_VECTOR
, bt
, x
, y
);
239 add_type_to_scope
(scope
, type
);
240 hlsl_ctx.builtin_types.vector
[bt
][x
- 1] = type
;
244 sprintf
(name
, "%s", names
[bt
]);
245 type
= new_hlsl_type
(d3dcompiler_strdup
(name
), HLSL_CLASS_SCALAR
, bt
, x
, y
);
246 add_type_to_scope
(scope
, type
);
247 hlsl_ctx.builtin_types.scalar
[bt
] = type
;
254 for
(bt
= 0; bt
<= HLSL_SAMPLER_DIM_MAX
; ++bt
)
256 type
= new_hlsl_type
(d3dcompiler_strdup
(sampler_names
[bt
]), HLSL_CLASS_OBJECT
, HLSL_TYPE_SAMPLER
, 1, 1);
257 type
->sampler_dim
= bt
;
258 hlsl_ctx.builtin_types.sampler
[bt
] = type
;
261 hlsl_ctx.builtin_types.Void
= new_hlsl_type
(d3dcompiler_strdup
("void"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VOID
, 1, 1);
263 /* DX8 effects predefined types */
264 type
= new_hlsl_type
(d3dcompiler_strdup
("DWORD"), HLSL_CLASS_SCALAR
, HLSL_TYPE_INT
, 1, 1);
265 add_type_to_scope
(scope
, type
);
266 type
= new_hlsl_type
(d3dcompiler_strdup
("FLOAT"), HLSL_CLASS_SCALAR
, HLSL_TYPE_FLOAT
, 1, 1);
267 add_type_to_scope
(scope
, type
);
268 type
= new_hlsl_type
(d3dcompiler_strdup
("VECTOR"), HLSL_CLASS_VECTOR
, HLSL_TYPE_FLOAT
, 4, 1);
269 add_type_to_scope
(scope
, type
);
270 type
= new_hlsl_type
(d3dcompiler_strdup
("MATRIX"), HLSL_CLASS_MATRIX
, HLSL_TYPE_FLOAT
, 4, 4);
271 add_type_to_scope
(scope
, type
);
272 type
= new_hlsl_type
(d3dcompiler_strdup
("STRING"), HLSL_CLASS_OBJECT
, HLSL_TYPE_STRING
, 1, 1);
273 add_type_to_scope
(scope
, type
);
274 type
= new_hlsl_type
(d3dcompiler_strdup
("TEXTURE"), HLSL_CLASS_OBJECT
, HLSL_TYPE_TEXTURE
, 1, 1);
275 add_type_to_scope
(scope
, type
);
276 type
= new_hlsl_type
(d3dcompiler_strdup
("PIXELSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_PIXELSHADER
, 1, 1);
277 add_type_to_scope
(scope
, type
);
278 type
= new_hlsl_type
(d3dcompiler_strdup
("VERTEXSHADER"), HLSL_CLASS_OBJECT
, HLSL_TYPE_VERTEXSHADER
, 1, 1);
279 add_type_to_scope
(scope
, type
);
282 static BOOL type_is_void
(const struct hlsl_type
*type
)
284 return type
->type
== HLSL_CLASS_OBJECT
&& type
->base_type
== HLSL_TYPE_VOID
;
287 static struct hlsl_ir_if
*new_if
(struct hlsl_ir_node
*condition
, struct source_location loc
)
289 struct hlsl_ir_if
*iff
;
291 if
(!(iff
= d3dcompiler_alloc
(sizeof
(*iff
))))
293 init_node
(&iff
->node
, HLSL_IR_IF
, NULL
, loc
);
294 hlsl_src_from_node
(&iff
->condition
, condition
);
295 list_init
(&iff
->then_instrs
);
296 list_init
(&iff
->else_instrs
);
300 static BOOL append_conditional_break
(struct list
*cond_list
)
302 struct hlsl_ir_node
*condition
, *not
;
303 struct hlsl_ir_jump
*jump
;
304 struct hlsl_ir_if
*iff
;
306 /* E.g. "for (i = 0; ; ++i)". */
307 if
(!list_count
(cond_list
))
310 condition
= node_from_list
(cond_list
);
311 if
(!(not
= new_unary_expr
(HLSL_IR_UNOP_LOGIC_NOT
, condition
, condition
->loc
)))
313 ERR
("Out of memory.\n");
316 list_add_tail
(cond_list
, ¬
->entry
);
318 if
(!(iff
= new_if
(not
, condition
->loc
)))
320 ERR
("Out of memory.\n");
323 list_add_tail
(cond_list
, &iff
->node.entry
);
325 if
(!(jump
= d3dcompiler_alloc
(sizeof
(*jump
))))
327 ERR
("Out of memory.\n");
330 init_node
(&jump
->node
, HLSL_IR_JUMP
, NULL
, condition
->loc
);
331 jump
->type
= HLSL_IR_JUMP_BREAK
;
332 list_add_head
(&iff
->then_instrs
, &jump
->node.entry
);
343 static struct list
*create_loop
(enum loop_type type
, struct list
*init
, struct list
*cond
,
344 struct list
*iter
, struct list
*body
, struct source_location loc
)
346 struct list
*list
= NULL
;
347 struct hlsl_ir_loop
*loop
= NULL
;
348 struct hlsl_ir_if
*cond_jump
= NULL
;
350 list
= d3dcompiler_alloc
(sizeof
(*list
));
356 list_move_head
(list
, init
);
358 loop
= d3dcompiler_alloc
(sizeof
(*loop
));
361 init_node
(&loop
->node
, HLSL_IR_LOOP
, NULL
, loc
);
362 list_add_tail
(list
, &loop
->node.entry
);
363 list_init
(&loop
->body
);
365 if
(!append_conditional_break
(cond
))
368 if
(type
!= LOOP_DO_WHILE
)
369 list_move_tail
(&loop
->body
, cond
);
371 list_move_tail
(&loop
->body
, body
);
374 list_move_tail
(&loop
->body
, iter
);
376 if
(type
== LOOP_DO_WHILE
)
377 list_move_tail
(&loop
->body
, cond
);
379 d3dcompiler_free
(init
);
380 d3dcompiler_free
(cond
);
381 d3dcompiler_free
(body
);
385 ERR
("Out of memory.\n");
386 d3dcompiler_free
(loop
);
387 d3dcompiler_free
(cond_jump
);
388 d3dcompiler_free
(list
);
389 free_instr_list
(init
);
390 free_instr_list
(cond
);
391 free_instr_list
(iter
);
392 free_instr_list
(body
);
396 static unsigned int initializer_size
(const struct parse_initializer
*initializer
)
398 unsigned int count
= 0, i
;
400 for
(i
= 0; i
< initializer
->args_count
; ++i
)
402 count
+= components_count_type
(initializer
->args
[i
]->data_type
);
404 TRACE
("Initializer size = %u.\n", count
);
408 static void free_parse_initializer
(struct parse_initializer
*initializer
)
410 free_instr_list
(initializer
->instrs
);
411 d3dcompiler_free
(initializer
->args
);
414 static struct hlsl_ir_swizzle
*new_swizzle
(DWORD s
, unsigned int components
,
415 struct hlsl_ir_node
*val
, struct source_location
*loc
)
417 struct hlsl_ir_swizzle
*swizzle
= d3dcompiler_alloc
(sizeof
(*swizzle
));
421 init_node
(&swizzle
->node
, HLSL_IR_SWIZZLE
,
422 new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, val
->data_type
->base_type
, components
, 1), *loc
);
423 hlsl_src_from_node
(&swizzle
->val
, val
);
424 swizzle
->swizzle
= s
;
428 static struct hlsl_ir_swizzle
*get_swizzle
(struct hlsl_ir_node
*value
, const char *swizzle
,
429 struct source_location
*loc
)
431 unsigned int len
= strlen
(swizzle
), component
= 0;
432 unsigned int i
, set
, swiz
= 0;
435 if
(value
->data_type
->type
== HLSL_CLASS_MATRIX
)
439 unsigned int inc
, x
, y
;
441 if
(len
< 3 || swizzle
[0] != '_')
443 m_swizzle
= swizzle
[1] == 'm';
444 inc
= m_swizzle ?
4 : 3;
446 if
(len % inc || len
> inc
* 4)
449 for
(i
= 0; i
< len
; i
+= inc
)
451 if
(swizzle
[i
] != '_')
455 if
(swizzle
[i
+ 1] != 'm')
457 y
= swizzle
[i
+ 2] - '0';
458 x
= swizzle
[i
+ 3] - '0';
462 y
= swizzle
[i
+ 1] - '1';
463 x
= swizzle
[i
+ 2] - '1';
466 if
(x
>= value
->data_type
->dimx || y
>= value
->data_type
->dimy
)
468 swiz |
= (y
<< 4 | x
) << component
* 8;
471 return new_swizzle
(swiz
, component
, value
, loc
);
478 for
(set
= 0; set
< 2; ++set
)
482 for
(i
= 0; i
< len
; ++i
)
484 char c
[2][4] = {{'x', 'y', 'z', 'w'}, {'r', 'g', 'b', 'a'}};
487 for
(s
= 0; s
< 4; ++s
)
489 if
(swizzle
[i
] == c
[set
][s
])
498 if
(s
>= value
->data_type
->dimx
)
500 swiz |
= s
<< component
* 2;
504 return new_swizzle
(swiz
, component
, value
, loc
);
510 static struct hlsl_ir_var
*new_var
(const char *name
, struct hlsl_type
*type
, const struct source_location loc
,
511 const char *semantic
, unsigned int modifiers
, const struct reg_reservation
*reg_reservation
)
513 struct hlsl_ir_var
*var
;
515 if
(!(var
= d3dcompiler_alloc
(sizeof
(*var
))))
517 hlsl_ctx.status
= PARSE_ERR
;
522 var
->data_type
= type
;
524 var
->semantic
= semantic
;
525 var
->modifiers
= modifiers
;
526 var
->reg_reservation
= reg_reservation
;
530 static struct hlsl_ir_var
*new_synthetic_var
(const char *name
, struct hlsl_type
*type
,
531 const struct source_location loc
)
533 struct hlsl_ir_var
*var
= new_var
(strdup
(name
), type
, loc
, NULL
, 0, NULL
);
536 list_add_tail
(&hlsl_ctx.globals
->vars
, &var
->scope_entry
);
540 static struct hlsl_ir_assignment
*new_assignment
(struct hlsl_ir_var
*var
, struct hlsl_ir_node
*offset
,
541 struct hlsl_ir_node
*rhs
, unsigned int writemask
, struct source_location loc
)
543 struct hlsl_ir_assignment
*assign
;
545 if
(!writemask
&& type_is_single_reg
(rhs
->data_type
))
546 writemask
= (1 << rhs
->data_type
->dimx
) - 1;
548 if
(!(assign
= d3dcompiler_alloc
(sizeof
(*assign
))))
551 init_node
(&assign
->node
, HLSL_IR_ASSIGNMENT
, NULL
, loc
);
552 assign
->lhs.var
= var
;
553 hlsl_src_from_node
(&assign
->lhs.offset
, offset
);
554 hlsl_src_from_node
(&assign
->rhs
, rhs
);
555 assign
->writemask
= writemask
;
559 static struct hlsl_ir_assignment
*new_simple_assignment
(struct hlsl_ir_var
*lhs
, struct hlsl_ir_node
*rhs
)
561 return new_assignment
(lhs
, NULL
, rhs
, 0, rhs
->loc
);
564 static struct hlsl_ir_jump
*add_return
(struct list
*instrs
,
565 struct hlsl_ir_node
*return_value
, struct source_location loc
)
567 struct hlsl_type
*return_type
= hlsl_ctx.cur_function
->return_type
;
568 struct hlsl_ir_jump
*jump
;
572 struct hlsl_ir_assignment
*assignment
;
574 if
(!(return_value
= add_implicit_conversion
(instrs
, return_value
, return_type
, &loc
)))
577 if
(!(assignment
= new_simple_assignment
(hlsl_ctx.cur_function
->return_var
, return_value
)))
579 list_add_after
(&return_value
->entry
, &assignment
->node.entry
);
581 else if
(!type_is_void
(return_type
))
583 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "non-void function must return a value");
587 if
(!(jump
= d3dcompiler_alloc
(sizeof
(*jump
))))
589 ERR
("Out of memory\n");
592 init_node
(&jump
->node
, HLSL_IR_JUMP
, NULL
, loc
);
593 jump
->type
= HLSL_IR_JUMP_RETURN
;
594 list_add_tail
(instrs
, &jump
->node.entry
);
599 static struct hlsl_ir_constant
*new_uint_constant
(unsigned int n
, const struct source_location loc
)
601 struct hlsl_ir_constant
*c
;
603 if
(!(c
= d3dcompiler_alloc
(sizeof
(*c
))))
605 init_node
(&c
->node
, HLSL_IR_CONSTANT
, hlsl_ctx.builtin_types.scalar
[HLSL_TYPE_UINT
], loc
);
610 struct hlsl_ir_node
*new_unary_expr
(enum hlsl_ir_expr_op op
, struct hlsl_ir_node
*arg
, struct source_location loc
)
612 struct hlsl_ir_expr
*expr
;
614 if
(!(expr
= d3dcompiler_alloc
(sizeof
(*expr
))))
616 init_node
(&expr
->node
, HLSL_IR_EXPR
, arg
->data_type
, loc
);
618 hlsl_src_from_node
(&expr
->operands
[0], arg
);
622 struct hlsl_ir_node
*new_binary_expr
(enum hlsl_ir_expr_op op
,
623 struct hlsl_ir_node
*arg1
, struct hlsl_ir_node
*arg2
)
625 struct hlsl_ir_expr
*expr
;
627 assert
(compare_hlsl_types
(arg1
->data_type
, arg2
->data_type
));
629 if
(!(expr
= d3dcompiler_alloc
(sizeof
(*expr
))))
631 init_node
(&expr
->node
, HLSL_IR_EXPR
, arg1
->data_type
, arg1
->loc
);
633 hlsl_src_from_node
(&expr
->operands
[0], arg1
);
634 hlsl_src_from_node
(&expr
->operands
[1], arg2
);
638 static struct hlsl_ir_load
*new_var_load
(struct hlsl_ir_var
*var
, const struct source_location loc
)
640 struct hlsl_ir_load
*load
= d3dcompiler_alloc
(sizeof
(*load
));
644 ERR
("Out of memory.\n");
647 init_node
(&load
->node
, HLSL_IR_LOAD
, var
->data_type
, loc
);
652 static struct hlsl_ir_load
*add_load
(struct list
*instrs
, struct hlsl_ir_node
*var_node
, struct hlsl_ir_node
*offset
,
653 struct hlsl_type
*data_type
, const struct source_location loc
)
655 struct hlsl_ir_node
*add
= NULL
;
656 struct hlsl_ir_load
*load
;
657 struct hlsl_ir_var
*var
;
659 if
(var_node
->type
== HLSL_IR_LOAD
)
661 const struct hlsl_deref
*src
= &load_from_node
(var_node
)->src
;
664 if
(src
->offset.node
)
666 if
(!(add
= new_binary_expr
(HLSL_IR_BINOP_ADD
, src
->offset.node
, offset
)))
668 list_add_tail
(instrs
, &add
->entry
);
674 struct hlsl_ir_assignment
*assign
;
677 sprintf
(name
, "<deref-%p>", var_node
);
678 if
(!(var
= new_synthetic_var
(name
, var_node
->data_type
, var_node
->loc
)))
681 TRACE
("Synthesized variable %p for %s node.\n", var
, debug_node_type
(var_node
->type
));
683 if
(!(assign
= new_simple_assignment
(var
, var_node
)))
686 list_add_tail
(instrs
, &assign
->node.entry
);
689 if
(!(load
= d3dcompiler_alloc
(sizeof
(*load
))))
691 init_node
(&load
->node
, HLSL_IR_LOAD
, data_type
, loc
);
693 hlsl_src_from_node
(&load
->src.offset
, offset
);
694 list_add_tail
(instrs
, &load
->node.entry
);
698 static struct hlsl_ir_load
*add_record_load
(struct list
*instrs
, struct hlsl_ir_node
*record
,
699 const struct hlsl_struct_field
*field
, const struct source_location loc
)
701 struct hlsl_ir_constant
*c
;
703 if
(!(c
= new_uint_constant
(field
->reg_offset
* 4, loc
)))
705 list_add_tail
(instrs
, &c
->node.entry
);
707 return add_load
(instrs
, record
, &c
->node
, field
->type
, loc
);
710 static struct hlsl_ir_load
*add_array_load
(struct list
*instrs
, struct hlsl_ir_node
*array
,
711 struct hlsl_ir_node
*index
, const struct source_location loc
)
713 const struct hlsl_type
*expr_type
= array
->data_type
;
714 struct hlsl_type
*data_type
;
715 struct hlsl_ir_constant
*c
;
716 struct hlsl_ir_node
*mul
;
718 TRACE
("Array load from type %s.\n", debug_hlsl_type
(expr_type
));
720 if
(expr_type
->type
== HLSL_CLASS_ARRAY
)
722 data_type
= expr_type
->e.array.type
;
724 else if
(expr_type
->type
== HLSL_CLASS_MATRIX || expr_type
->type
== HLSL_CLASS_VECTOR
)
726 /* This needs to be lowered now, while we still have type information. */
727 FIXME
("Index of matrix or vector type.\n");
732 if
(expr_type
->type
== HLSL_CLASS_SCALAR
)
733 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "array-indexed expression is scalar");
735 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "expression is not array-indexable");
739 if
(!(c
= new_uint_constant
(data_type
->reg_size
* 4, loc
)))
741 list_add_tail
(instrs
, &c
->node.entry
);
742 if
(!(mul
= new_binary_expr
(HLSL_IR_BINOP_MUL
, index
, &c
->node
)))
744 list_add_tail
(instrs
, &mul
->entry
);
747 return add_load
(instrs
, array
, index
, data_type
, loc
);
750 static void struct_var_initializer
(struct list
*list
, struct hlsl_ir_var
*var
,
751 struct parse_initializer
*initializer
)
753 struct hlsl_type
*type
= var
->data_type
;
754 struct hlsl_struct_field
*field
;
757 if
(initializer_size
(initializer
) != components_count_type
(type
))
759 hlsl_report_message
(var
->loc
, HLSL_LEVEL_ERROR
, "structure initializer mismatch");
760 free_parse_initializer
(initializer
);
764 list_move_tail
(list
, initializer
->instrs
);
765 d3dcompiler_free
(initializer
->instrs
);
767 LIST_FOR_EACH_ENTRY
(field
, type
->e.elements
, struct hlsl_struct_field
, entry
)
769 struct hlsl_ir_node
*node
= initializer
->args
[i
];
770 struct hlsl_ir_assignment
*assign
;
771 struct hlsl_ir_constant
*c
;
773 if
(i
++ >= initializer
->args_count
)
776 if
(components_count_type
(field
->type
) == components_count_type
(node
->data_type
))
778 if
(!(c
= new_uint_constant
(field
->reg_offset
* 4, node
->loc
)))
780 list_add_tail
(list
, &c
->node.entry
);
782 if
(!(assign
= new_assignment
(var
, &c
->node
, node
, 0, node
->loc
)))
784 list_add_tail
(list
, &assign
->node.entry
);
787 FIXME
("Initializing with \"mismatched\" fields is not supported yet.\n");
790 d3dcompiler_free
(initializer
->args
);
793 static void free_parse_variable_def
(struct parse_variable_def
*v
)
795 free_parse_initializer
(&v
->initializer
);
796 d3dcompiler_free
(v
->name
);
797 d3dcompiler_free
((void *)v
->semantic
);
798 d3dcompiler_free
(v
->reg_reservation
);
802 static struct list
*declare_vars
(struct hlsl_type
*basic_type
, DWORD modifiers
, struct list
*var_list
)
804 struct hlsl_type
*type
;
805 struct parse_variable_def
*v
, *v_next
;
806 struct hlsl_ir_var
*var
;
807 BOOL ret
, local
= TRUE
;
808 struct list
*statements_list
= d3dcompiler_alloc
(sizeof
(*statements_list
));
810 if
(basic_type
->type
== HLSL_CLASS_MATRIX
)
811 assert
(basic_type
->modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
);
813 if
(!statements_list
)
815 ERR
("Out of memory.\n");
816 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, var_list
, struct parse_variable_def
, entry
)
817 free_parse_variable_def
(v
);
818 d3dcompiler_free
(var_list
);
821 list_init
(statements_list
);
824 return statements_list
;
826 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, var_list
, struct parse_variable_def
, entry
)
829 type
= new_array_type
(basic_type
, v
->array_size
);
833 if
(!(var
= new_var
(v
->name
, type
, v
->loc
, v
->semantic
, modifiers
, v
->reg_reservation
)))
835 free_parse_variable_def
(v
);
838 debug_dump_decl
(type
, modifiers
, v
->name
, v
->loc.line
);
840 if
(hlsl_ctx.cur_scope
== hlsl_ctx.globals
)
842 var
->modifiers |
= HLSL_STORAGE_UNIFORM
;
846 if
(type
->modifiers
& HLSL_MODIFIER_CONST
&& !(var
->modifiers
& HLSL_STORAGE_UNIFORM
) && !v
->initializer.args_count
)
848 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
, "const variable without initializer");
849 free_declaration
(var
);
854 ret
= declare_variable
(var
, local
);
857 free_declaration
(var
);
861 TRACE
("Declared variable %s.\n", var
->name
);
863 if
(v
->initializer.args_count
)
865 unsigned int size
= initializer_size
(&v
->initializer
);
866 struct hlsl_ir_load
*load
;
868 TRACE
("Variable with initializer.\n");
869 if
(type
->type
<= HLSL_CLASS_LAST_NUMERIC
870 && type
->dimx
* type
->dimy
!= size
&& size
!= 1)
872 if
(size
< type
->dimx
* type
->dimy
)
874 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
,
875 "'%s' initializer does not match", v
->name
);
876 free_parse_initializer
(&v
->initializer
);
881 if
((type
->type
== HLSL_CLASS_STRUCT || type
->type
== HLSL_CLASS_ARRAY
)
882 && components_count_type
(type
) != size
)
884 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
,
885 "'%s' initializer does not match", v
->name
);
886 free_parse_initializer
(&v
->initializer
);
891 if
(type
->type
== HLSL_CLASS_STRUCT
)
893 struct_var_initializer
(statements_list
, var
, &v
->initializer
);
897 if
(type
->type
> HLSL_CLASS_LAST_NUMERIC
)
899 FIXME
("Initializers for non scalar/struct variables not supported yet.\n");
900 free_parse_initializer
(&v
->initializer
);
904 if
(v
->array_size
> 0)
906 FIXME
("Initializing arrays is not supported yet.\n");
907 free_parse_initializer
(&v
->initializer
);
911 if
(v
->initializer.args_count
> 1)
913 FIXME
("Complex initializers are not supported yet.\n");
914 free_parse_initializer
(&v
->initializer
);
919 load
= new_var_load
(var
, var
->loc
);
920 list_add_tail
(v
->initializer.instrs
, &load
->node.entry
);
921 add_assignment
(v
->initializer.instrs
, &load
->node
, ASSIGN_OP_ASSIGN
, v
->initializer.args
[0]);
922 d3dcompiler_free
(v
->initializer.args
);
924 if
(modifiers
& HLSL_STORAGE_STATIC
)
925 list_move_tail
(&hlsl_ctx.static_initializers
, v
->initializer.instrs
);
927 list_move_tail
(statements_list
, v
->initializer.instrs
);
928 d3dcompiler_free
(v
->initializer.instrs
);
932 d3dcompiler_free
(var_list
);
933 return statements_list
;
936 static BOOL add_struct_field
(struct list
*fields
, struct hlsl_struct_field
*field
)
938 struct hlsl_struct_field
*f
;
940 LIST_FOR_EACH_ENTRY
(f
, fields
, struct hlsl_struct_field
, entry
)
942 if
(!strcmp
(f
->name
, field
->name
))
945 list_add_tail
(fields
, &field
->entry
);
949 BOOL is_row_major
(const struct hlsl_type
*type
)
951 /* Default to column-major if the majority isn't explicitly set, which can
952 * happen for anonymous nodes. */
953 return
!!(type
->modifiers
& HLSL_MODIFIER_ROW_MAJOR
);
956 static struct hlsl_type
*apply_type_modifiers
(struct hlsl_type
*type
,
957 unsigned int *modifiers
, struct source_location loc
)
959 unsigned int default_majority
= 0;
960 struct hlsl_type
*new_type
;
962 /* This function is only used for declarations (i.e. variables and struct
963 * fields), which should inherit the matrix majority. We only explicitly set
964 * the default majority for declarations—typedefs depend on this—but we
965 * want to always set it, so that an hlsl_type object is never used to
966 * represent two different majorities (and thus can be used to store its
967 * register size, etc.) */
968 if
(!(*modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
)
969 && !(type
->modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
)
970 && type
->type
== HLSL_CLASS_MATRIX
)
972 if
(hlsl_ctx.matrix_majority
== HLSL_COLUMN_MAJOR
)
973 default_majority
= HLSL_MODIFIER_COLUMN_MAJOR
;
975 default_majority
= HLSL_MODIFIER_ROW_MAJOR
;
978 if
(!default_majority
&& !(*modifiers
& HLSL_TYPE_MODIFIERS_MASK
))
981 if
(!(new_type
= clone_hlsl_type
(type
, default_majority
)))
984 new_type
->modifiers
= add_modifiers
(new_type
->modifiers
, *modifiers
, loc
);
985 *modifiers
&= ~HLSL_TYPE_MODIFIERS_MASK
;
987 if
(new_type
->type
== HLSL_CLASS_MATRIX
)
988 new_type
->reg_size
= is_row_major
(new_type
) ? new_type
->dimy
: new_type
->dimx
;
992 static struct list
*gen_struct_fields
(struct hlsl_type
*type
, DWORD modifiers
, struct list
*fields
)
994 struct parse_variable_def
*v
, *v_next
;
995 struct hlsl_struct_field
*field
;
998 if
(type
->type
== HLSL_CLASS_MATRIX
)
999 assert
(type
->modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
);
1001 list
= d3dcompiler_alloc
(sizeof
(*list
));
1004 ERR
("Out of memory.\n");
1008 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, fields
, struct parse_variable_def
, entry
)
1010 debug_dump_decl
(type
, 0, v
->name
, v
->loc.line
);
1011 field
= d3dcompiler_alloc
(sizeof
(*field
));
1014 ERR
("Out of memory.\n");
1015 d3dcompiler_free
(v
);
1019 field
->type
= new_array_type
(type
, v
->array_size
);
1022 field
->name
= v
->name
;
1023 field
->modifiers
= modifiers
;
1024 field
->semantic
= v
->semantic
;
1025 if
(v
->initializer.args_count
)
1027 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
, "struct field with an initializer.\n");
1028 free_parse_initializer
(&v
->initializer
);
1030 list_add_tail
(list
, &field
->entry
);
1031 d3dcompiler_free
(v
);
1033 d3dcompiler_free
(fields
);
1037 static DWORD get_array_size
(const struct hlsl_type
*type
)
1039 if
(type
->type
== HLSL_CLASS_ARRAY
)
1040 return get_array_size
(type
->e.array.type
) * type
->e.array.elements_count
;
1044 static struct hlsl_type
*new_struct_type
(const char *name
, struct list
*fields
)
1046 struct hlsl_type
*type
= d3dcompiler_alloc
(sizeof
(*type
));
1047 struct hlsl_struct_field
*field
;
1048 unsigned int reg_size
= 0;
1052 ERR
("Out of memory.\n");
1055 type
->type
= HLSL_CLASS_STRUCT
;
1056 type
->base_type
= HLSL_TYPE_VOID
;
1060 type
->e.elements
= fields
;
1062 LIST_FOR_EACH_ENTRY
(field
, fields
, struct hlsl_struct_field
, entry
)
1064 field
->reg_offset
= reg_size
;
1065 reg_size
+= field
->type
->reg_size
;
1066 type
->dimx
+= field
->type
->dimx
* field
->type
->dimy
* get_array_size
(field
->type
);
1068 type
->reg_size
= reg_size
;
1070 list_add_tail
(&hlsl_ctx.types
, &type
->entry
);
1075 static BOOL add_typedef
(DWORD modifiers
, struct hlsl_type
*orig_type
, struct list
*list
)
1078 struct hlsl_type
*type
;
1079 struct parse_variable_def
*v
, *v_next
;
1081 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, list
, struct parse_variable_def
, entry
)
1084 type
= new_array_type
(orig_type
, v
->array_size
);
1086 type
= clone_hlsl_type
(orig_type
, 0);
1089 ERR
("Out of memory\n");
1092 d3dcompiler_free
((void *)type
->name
);
1093 type
->name
= v
->name
;
1094 type
->modifiers |
= modifiers
;
1096 if
(type
->type
!= HLSL_CLASS_MATRIX
)
1097 check_invalid_matrix_modifiers
(type
->modifiers
, v
->loc
);
1099 type
->reg_size
= is_row_major
(type
) ? type
->dimy
: type
->dimx
;
1101 if
((type
->modifiers
& HLSL_MODIFIER_COLUMN_MAJOR
)
1102 && (type
->modifiers
& HLSL_MODIFIER_ROW_MAJOR
))
1103 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
, "more than one matrix majority keyword");
1105 ret
= add_type_to_scope
(hlsl_ctx.cur_scope
, type
);
1108 hlsl_report_message
(v
->loc
, HLSL_LEVEL_ERROR
,
1109 "redefinition of custom type '%s'", v
->name
);
1111 d3dcompiler_free
(v
);
1113 d3dcompiler_free
(list
);
1117 static BOOL add_func_parameter
(struct list
*list
, struct parse_parameter
*param
, const struct source_location loc
)
1119 struct hlsl_ir_var
*var
;
1121 if
(param
->type
->type
== HLSL_CLASS_MATRIX
)
1122 assert
(param
->type
->modifiers
& HLSL_MODIFIERS_MAJORITY_MASK
);
1124 if
(!(var
= new_var
(param
->name
, param
->type
, loc
, param
->semantic
, param
->modifiers
, param
->reg_reservation
)))
1127 if
(!add_declaration
(hlsl_ctx.cur_scope
, var
, FALSE
))
1129 free_declaration
(var
);
1132 list_add_tail
(list
, &var
->param_entry
);
1136 static struct reg_reservation
*parse_reg_reservation
(const char *reg_string
)
1138 struct reg_reservation
*reg_res
;
1139 enum bwritershader_param_register_type type
;
1142 switch
(reg_string
[0])
1145 type
= BWRITERSPR_CONST
;
1148 type
= BWRITERSPR_CONSTINT
;
1151 type
= BWRITERSPR_CONSTBOOL
;
1154 type
= BWRITERSPR_SAMPLER
;
1157 FIXME
("Unsupported register type.\n");
1161 if
(!sscanf
(reg_string
+ 1, "%u", ®num
))
1163 FIXME
("Unsupported register reservation syntax.\n");
1167 reg_res
= d3dcompiler_alloc
(sizeof
(*reg_res
));
1170 ERR
("Out of memory.\n");
1173 reg_res
->type
= type
;
1174 reg_res
->regnum
= regnum
;
1178 static const struct hlsl_ir_function_decl
*get_overloaded_func
(struct wine_rb_tree
*funcs
, char *name
,
1179 struct list
*params
, BOOL exact_signature
)
1181 struct hlsl_ir_function
*func
;
1182 struct wine_rb_entry
*entry
;
1184 entry
= wine_rb_get
(funcs
, name
);
1187 func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function
, entry
);
1189 entry
= wine_rb_get
(&func
->overloads
, params
);
1192 if
(!exact_signature
)
1193 FIXME
("No exact match, search for a compatible overloaded function (if any).\n");
1196 return WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function_decl
, entry
);
1201 static struct hlsl_ir_function_decl
*get_func_entry
(const char *name
)
1203 struct hlsl_ir_function_decl
*decl
;
1204 struct hlsl_ir_function
*func
;
1205 struct wine_rb_entry
*entry
;
1207 if
((entry
= wine_rb_get
(&hlsl_ctx.functions
, name
)))
1209 func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function
, entry
);
1210 WINE_RB_FOR_EACH_ENTRY
(decl
, &func
->overloads
, struct hlsl_ir_function_decl
, entry
)
1217 static struct list
*append_unop
(struct list
*list
, struct hlsl_ir_node
*node
)
1219 list_add_tail
(list
, &node
->entry
);
1223 static struct list
*add_binary_expr
(struct list
*list1
, struct list
*list2
,
1224 enum hlsl_ir_expr_op op
, struct source_location loc
)
1226 struct hlsl_ir_node
*args
[3] = {node_from_list
(list1
), node_from_list
(list2
)};
1227 list_move_tail
(list1
, list2
);
1228 d3dcompiler_free
(list2
);
1229 add_expr
(list1
, op
, args
, &loc
);
1233 static struct list
*make_list
(struct hlsl_ir_node
*node
)
1237 if
(!(list
= d3dcompiler_alloc
(sizeof
(*list
))))
1239 ERR
("Out of memory.\n");
1244 list_add_tail
(list
, &node
->entry
);
1248 static unsigned int evaluate_array_dimension
(struct hlsl_ir_node
*node
)
1250 if
(node
->data_type
->type
!= HLSL_CLASS_SCALAR
)
1255 case HLSL_IR_CONSTANT
:
1257 struct hlsl_ir_constant
*constant
= constant_from_node
(node
);
1259 switch
(constant
->node.data_type
->base_type
)
1261 case HLSL_TYPE_UINT
:
1262 return constant
->value.u
[0];
1264 return constant
->value.i
[0];
1265 case HLSL_TYPE_FLOAT
:
1266 return constant
->value.f
[0];
1267 case HLSL_TYPE_DOUBLE
:
1268 return constant
->value.d
[0];
1269 case HLSL_TYPE_BOOL
:
1270 return constant
->value.b
[0];
1272 WARN
("Invalid type %s.\n", debug_base_type
(constant
->node.data_type
));
1278 case HLSL_IR_SWIZZLE
:
1279 FIXME
("Unhandled type %s.\n", debug_node_type
(node
->type
));
1281 case HLSL_IR_ASSIGNMENT
:
1283 WARN
("Invalid node type %s.\n", debug_node_type
(node
->type
));
1288 static struct hlsl_ir_function_decl
*new_func_decl
(struct hlsl_type
*return_type
,
1289 struct list
*parameters
, const char *semantic
, struct source_location loc
)
1291 struct hlsl_ir_function_decl
*decl
;
1293 if
(!(decl
= d3dcompiler_alloc
(sizeof
(*decl
))))
1295 decl
->return_type
= return_type
;
1296 decl
->parameters
= parameters
;
1297 decl
->semantic
= semantic
;
1300 if
(!type_is_void
(return_type
))
1302 struct hlsl_ir_var
*return_var
;
1305 sprintf
(name
, "<retval-%p>", decl
);
1306 if
(!(return_var
= new_synthetic_var
(name
, return_type
, loc
)))
1308 d3dcompiler_free
(decl
);
1311 decl
->return_var
= return_var
;
1320 %define parse.
error verbose
1325 struct hlsl_type
*type
;
1331 struct hlsl_ir_node
*instr
;
1333 struct parse_function function
;
1334 struct parse_parameter parameter
;
1335 struct parse_initializer initializer
;
1336 struct parse_variable_def
*variable_def
;
1337 struct parse_if_body if_body
;
1338 enum parse_unary_op unary_op
;
1339 enum parse_assign_op assign_op
;
1340 struct reg_reservation
*reg_reservation
;
1341 struct parse_colon_attribute colon_attribute
;
1344 %token KW_BLENDSTATE
1348 %token KW_COLUMN_MAJOR
1352 %token KW_DEPTHSTENCILSTATE
1353 %token KW_DEPTHSTENCILVIEW
1361 %token KW_GEOMETRYSHADER
1362 %token KW_GROUPSHARED
1369 %token KW_NOINTERPOLATION
1372 %token KW_PIXELSHADER
1374 %token KW_RASTERIZERSTATE
1375 %token KW_RENDERTARGETVIEW
1383 %token KW_SAMPLERCUBE
1384 %token KW_SAMPLER_STATE
1385 %token KW_SAMPLERCOMPARISONSTATE
1387 %token KW_STATEBLOCK
1388 %token KW_STATEBLOCK_STATE
1395 %token KW_TECHNIQUE10
1398 %token KW_TEXTURE1DARRAY
1400 %token KW_TEXTURE2DARRAY
1401 %token KW_TEXTURE2DMS
1402 %token KW_TEXTURE2DMSARRAY
1404 %token KW_TEXTURE3DARRAY
1405 %token KW_TEXTURECUBE
1410 %token KW_VERTEXSHADER
1421 %token OP_LEFTSHIFTASSIGN
1422 %token OP_RIGHTSHIFT
1423 %token OP_RIGHTSHIFTASSIGN
1441 %token
<intval
> PRE_LINE
1443 %token
<name
> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
1444 %type
<name
> any_identifier var_identifier
1445 %token
<name
> STRING
1446 %token
<floatval
> C_FLOAT
1447 %token
<intval
> C_INTEGER
1448 %type
<boolval
> boolean
1449 %type
<type
> base_type
1451 %type
<list
> declaration_statement
1452 %type
<list
> declaration
1453 %type
<list
> struct_declaration
1454 %type
<type
> struct_spec
1455 %type
<type
> named_struct_spec
1456 %type
<type
> unnamed_struct_spec
1457 %type
<type
> field_type
1458 %type
<type
> typedef_type
1459 %type
<list
> type_specs
1460 %type
<variable_def
> type_spec
1461 %type
<initializer
> complex_initializer
1462 %type
<initializer
> initializer_expr_list
1463 %type
<list
> initializer_expr
1464 %type
<modifiers
> var_modifiers
1466 %type
<list
> parameters
1467 %type
<list
> param_list
1469 %type
<intval
> array
1470 %type
<list
> statement
1471 %type
<list
> statement_list
1472 %type
<list
> compound_statement
1473 %type
<list
> jump_statement
1474 %type
<list
> selection_statement
1475 %type
<list
> loop_statement
1476 %type
<function
> func_declaration
1477 %type
<function
> func_prototype
1478 %type
<list
> fields_list
1479 %type
<parameter
> parameter
1480 %type
<colon_attribute
> colon_attribute
1481 %type
<name
> semantic
1482 %type
<reg_reservation
> register_opt
1483 %type
<variable_def
> variable_def
1484 %type
<list
> variables_def
1485 %type
<list
> variables_def_optional
1486 %type
<if_body
> if_body
1487 %type
<list
> primary_expr
1488 %type
<list
> postfix_expr
1489 %type
<list
> unary_expr
1490 %type
<list
> mul_expr
1491 %type
<list
> add_expr
1492 %type
<list
> shift_expr
1493 %type
<list
> relational_expr
1494 %type
<list
> equality_expr
1495 %type
<list
> bitand_expr
1496 %type
<list
> bitxor_expr
1497 %type
<list
> bitor_expr
1498 %type
<list
> logicand_expr
1499 %type
<list
> logicor_expr
1500 %type
<list
> conditional_expr
1501 %type
<list
> assignment_expr
1502 %type
<list
> expr_statement
1503 %type
<unary_op
> unary_op
1504 %type
<assign_op
> assign_op
1505 %type
<modifiers
> input_mods
1506 %type
<modifiers
> input_mod
1509 hlsl_prog: /* empty */
1512 | hlsl_prog func_declaration
1514 const struct hlsl_ir_function_decl
*decl
;
1516 decl
= get_overloaded_func
(&hlsl_ctx.functions
, $2.name
, $2.decl
->parameters
, TRUE
);
1517 if
(decl
&& !decl
->func
->intrinsic
)
1519 if
(decl
->body
&& $2.decl
->body
)
1521 hlsl_report_message
($2.decl
->loc
, HLSL_LEVEL_ERROR
,
1522 "redefinition of function %s", debugstr_a
($2.name
));
1525 else if
(!compare_hlsl_types
(decl
->return_type
, $2.decl
->return_type
))
1527 hlsl_report_message
($2.decl
->loc
, HLSL_LEVEL_ERROR
,
1528 "redefining function %s with a different return type",
1529 debugstr_a
($2.name
));
1530 hlsl_report_message
(decl
->loc
, HLSL_LEVEL_NOTE
,
1531 "%s previously declared here",
1532 debugstr_a
($2.name
));
1537 if
(type_is_void
($2.decl
->return_type
) && $2.decl
->semantic
)
1539 hlsl_report_message
($2.decl
->loc
, HLSL_LEVEL_ERROR
,
1540 "void function with a semantic");
1543 TRACE
("Adding function '%s' to the function list.\n", $2.name
);
1544 add_function_decl
(&hlsl_ctx.functions
, $2.name
, $2.decl
, FALSE
);
1546 | hlsl_prog declaration_statement
1548 TRACE
("Declaration statement parsed.\n");
1550 if
(!list_empty
($2))
1551 FIXME
("Uniform initializer.\n");
1552 free_instr_list
($2);
1554 | hlsl_prog preproc_directive
1559 TRACE
("Skipping stray semicolon.\n");
1562 preproc_directive: PRE_LINE STRING
1564 const char **new_array
= NULL
;
1566 TRACE
("Updating line information to file %s, line %u\n", debugstr_a
($2), $1);
1567 hlsl_ctx.line_no
= $1;
1568 if
(strcmp
($2, hlsl_ctx.source_file
))
1569 new_array
= d3dcompiler_realloc
(hlsl_ctx.source_files
,
1570 sizeof
(*hlsl_ctx.source_files
) * (hlsl_ctx.source_files_count
+ 1));
1574 hlsl_ctx.source_files
= new_array
;
1575 hlsl_ctx.source_files
[hlsl_ctx.source_files_count
++] = $2;
1576 hlsl_ctx.source_file
= $2;
1580 d3dcompiler_free
($2);
1584 struct_declaration: var_modifiers struct_spec variables_def_optional
';'
1586 struct hlsl_type
*type
;
1587 DWORD modifiers
= $1;
1593 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
,
1594 "anonymous struct declaration with no variables");
1598 hlsl_report_message
(get_location
(&@
1), HLSL_LEVEL_ERROR
,
1599 "modifier not allowed on struct type declaration");
1603 if
(!(type
= apply_type_modifiers
($2, &modifiers
, get_location
(&@
1))))
1605 $$
= declare_vars
(type
, modifiers
, $3);
1608 struct_spec: named_struct_spec
1609 | unnamed_struct_spec
1611 named_struct_spec: KW_STRUCT any_identifier
'{' fields_list
'}'
1615 TRACE
("Structure %s declaration.\n", debugstr_a
($2));
1616 $$
= new_struct_type
($2, $4);
1618 if
(get_variable
(hlsl_ctx.cur_scope
, $2))
1620 hlsl_report_message
(get_location
(&@
2),
1621 HLSL_LEVEL_ERROR
, "redefinition of '%s'", $2);
1625 ret
= add_type_to_scope
(hlsl_ctx.cur_scope
, $$
);
1628 hlsl_report_message
(get_location
(&@
2),
1629 HLSL_LEVEL_ERROR
, "redefinition of struct '%s'", $2);
1634 unnamed_struct_spec: KW_STRUCT
'{' fields_list
'}'
1636 TRACE
("Anonymous structure declaration.\n");
1637 $$
= new_struct_type
(NULL
, $3);
1640 any_identifier: VAR_IDENTIFIER
1644 fields_list: /* Empty */
1646 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1652 struct hlsl_struct_field
*field
, *next
;
1655 LIST_FOR_EACH_ENTRY_SAFE
(field
, next
, $2, struct hlsl_struct_field
, entry
)
1657 ret
= add_struct_field
($$
, field
);
1660 hlsl_report_message
(get_location
(&@
2),
1661 HLSL_LEVEL_ERROR
, "redefinition of '%s'", field
->name
);
1662 d3dcompiler_free
(field
);
1665 d3dcompiler_free
($2);
1669 | unnamed_struct_spec
1671 field: var_modifiers field_type variables_def
';'
1673 struct hlsl_type
*type
;
1674 DWORD modifiers
= $1;
1676 if
(!(type
= apply_type_modifiers
($2, &modifiers
, get_location
(&@
1))))
1678 $$
= gen_struct_fields
(type
, modifiers
, $3);
1681 func_declaration: func_prototype compound_statement
1683 TRACE
("Function %s parsed.\n", $1.name
);
1686 pop_scope
(&hlsl_ctx
);
1688 | func_prototype
';'
1690 TRACE
("Function prototype for %s.\n", $1.name
);
1692 pop_scope
(&hlsl_ctx
);
1695 /* var_modifiers is necessary to avoid shift/reduce conflicts. */
1696 func_prototype: var_modifiers type var_identifier
'(' parameters
')' colon_attribute
1700 hlsl_report_message
(get_location
(&@
1), HLSL_LEVEL_ERROR
,
1701 "unexpected modifiers on a function");
1704 if
(get_variable
(hlsl_ctx.globals
, $3))
1706 hlsl_report_message
(get_location
(&@
3),
1707 HLSL_LEVEL_ERROR
, "redefinition of '%s'\n", $3);
1710 if
(type_is_void
($2) && $7.semantic
)
1712 hlsl_report_message
(get_location
(&@
7),
1713 HLSL_LEVEL_ERROR
, "void function with a semantic");
1716 if
($7.reg_reservation
)
1718 FIXME
("Unexpected register reservation for a function.\n");
1719 d3dcompiler_free
($7.reg_reservation
);
1721 if
(!($$.decl
= new_func_decl
($2, $5, $7.semantic
, get_location
(&@
3))))
1723 ERR
("Out of memory.\n");
1727 hlsl_ctx.cur_function
= $$.decl
;
1730 compound_statement: '{' '}'
1732 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1735 |
'{' scope_start statement_list
'}'
1737 pop_scope
(&hlsl_ctx
);
1741 scope_start: /* Empty */
1743 push_scope
(&hlsl_ctx
);
1746 var_identifier: VAR_IDENTIFIER
1749 colon_attribute: /* Empty */
1752 $$.reg_reservation
= NULL
;
1757 $$.reg_reservation
= NULL
;
1762 $$.reg_reservation
= $1;
1765 semantic: ':' any_identifier
1770 /* FIXME: Writemasks */
1771 register_opt: ':' KW_REGISTER
'(' any_identifier
')'
1773 $$
= parse_reg_reservation
($4);
1774 d3dcompiler_free
($4);
1776 |
':' KW_REGISTER
'(' any_identifier
',' any_identifier
')'
1778 FIXME
("Ignoring shader target %s in a register reservation.\n", debugstr_a
($4));
1779 d3dcompiler_free
($4);
1781 $$
= parse_reg_reservation
($6);
1782 d3dcompiler_free
($6);
1785 parameters: scope_start
1787 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1790 | scope_start param_list
1795 param_list: parameter
1797 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1799 if
(!add_func_parameter
($$
, &$1, get_location
(&@
1)))
1801 ERR
("Error adding function parameter %s.\n", $1.name
);
1802 set_parse_status
(&hlsl_ctx.status
, PARSE_ERR
);
1806 | param_list
',' parameter
1809 if
(!add_func_parameter
($$
, &$3, get_location
(&@
3)))
1811 hlsl_report_message
(get_location
(&@
3), HLSL_LEVEL_ERROR
,
1812 "duplicate parameter %s", $3.name
);
1817 parameter: input_mods var_modifiers type any_identifier colon_attribute
1819 struct hlsl_type
*type
;
1820 DWORD modifiers
= $2;
1822 if
(!(type
= apply_type_modifiers
($3, &modifiers
, get_location
(&@
2))))
1825 $$.modifiers
= $1 ?
$1 : HLSL_STORAGE_IN
;
1826 $$.modifiers |
= modifiers
;
1829 $$.semantic
= $5.semantic
;
1830 $$.reg_reservation
= $5.reg_reservation
;
1833 input_mods: /* Empty */
1837 | input_mods input_mod
1841 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
,
1842 "duplicate input-output modifiers");
1850 $$
= HLSL_STORAGE_IN
;
1854 $$
= HLSL_STORAGE_OUT
;
1858 $$
= HLSL_STORAGE_IN | HLSL_STORAGE_OUT
;
1867 | KW_VECTOR
'<' base_type
',' C_INTEGER
'>'
1869 if
($3->type
!= HLSL_CLASS_SCALAR
)
1871 hlsl_report_message
(get_location
(&@
3), HLSL_LEVEL_ERROR
,
1872 "vectors of non-scalar types are not allowed\n");
1875 if
($5 < 1 ||
$5 > 4)
1877 hlsl_report_message
(get_location
(&@
5), HLSL_LEVEL_ERROR
,
1878 "vector size must be between 1 and 4\n");
1882 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_VECTOR
, $3->base_type
, $5, 1);
1884 | KW_MATRIX
'<' base_type
',' C_INTEGER
',' C_INTEGER
'>'
1886 if
($3->type
!= HLSL_CLASS_SCALAR
)
1888 hlsl_report_message
(get_location
(&@
3), HLSL_LEVEL_ERROR
,
1889 "matrices of non-scalar types are not allowed\n");
1892 if
($5 < 1 ||
$5 > 4)
1894 hlsl_report_message
(get_location
(&@
5), HLSL_LEVEL_ERROR
,
1895 "matrix row count must be between 1 and 4\n");
1898 if
($7 < 1 ||
$7 > 4)
1900 hlsl_report_message
(get_location
(&@
7), HLSL_LEVEL_ERROR
,
1901 "matrix column count must be between 1 and 4\n");
1905 $$
= new_hlsl_type
(NULL
, HLSL_CLASS_MATRIX
, $3->base_type
, $7, $5);
1912 $$
= hlsl_ctx.builtin_types.Void
;
1916 $$
= hlsl_ctx.builtin_types.sampler
[HLSL_SAMPLER_DIM_GENERIC
];
1920 $$
= hlsl_ctx.builtin_types.sampler
[HLSL_SAMPLER_DIM_1D
];
1924 $$
= hlsl_ctx.builtin_types.sampler
[HLSL_SAMPLER_DIM_2D
];
1928 $$
= hlsl_ctx.builtin_types.sampler
[HLSL_SAMPLER_DIM_3D
];
1932 $$
= hlsl_ctx.builtin_types.sampler
[HLSL_SAMPLER_DIM_3D
];
1936 $$
= get_type
(hlsl_ctx.cur_scope
, $1, TRUE
);
1937 d3dcompiler_free
($1);
1939 | KW_STRUCT TYPE_IDENTIFIER
1941 $$
= get_type
(hlsl_ctx.cur_scope
, $2, TRUE
);
1942 if
($$
->type
!= HLSL_CLASS_STRUCT
)
1943 hlsl_report_message
(get_location
(&@
1), HLSL_LEVEL_ERROR
, "'%s' redefined as a structure\n", $2);
1944 d3dcompiler_free
($2);
1947 declaration_statement: declaration
1948 | struct_declaration
1951 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1954 ERR
("Out of memory\n");
1963 typedef: KW_TYPEDEF var_modifiers typedef_type type_specs
';'
1965 if
($2 & ~HLSL_TYPE_MODIFIERS_MASK
)
1967 struct parse_variable_def
*v
, *v_next
;
1968 hlsl_report_message
(get_location
(&@
1),
1969 HLSL_LEVEL_ERROR
, "modifier not allowed on typedefs");
1970 LIST_FOR_EACH_ENTRY_SAFE
(v
, v_next
, $4, struct parse_variable_def
, entry
)
1971 d3dcompiler_free
(v
);
1972 d3dcompiler_free
($4);
1975 if
(!add_typedef
($2, $3, $4))
1979 type_specs: type_spec
1981 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1983 list_add_head
($$
, &$1->entry
);
1985 | type_specs
',' type_spec
1988 list_add_tail
($$
, &$3->entry
);
1991 type_spec: any_identifier array
1993 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
1994 $$
->loc
= get_location
(&@
1);
1996 $$
->array_size
= $2;
1999 declaration: var_modifiers type variables_def
';'
2001 struct hlsl_type
*type
;
2002 DWORD modifiers
= $1;
2004 if
(!(type
= apply_type_modifiers
($2, &modifiers
, get_location
(&@
1))))
2006 $$
= declare_vars
(type
, modifiers
, $3);
2009 variables_def_optional: /* Empty */
2018 variables_def: variable_def
2020 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
2022 list_add_head
($$
, &$1->entry
);
2024 | variables_def
',' variable_def
2027 list_add_tail
($$
, &$3->entry
);
2030 variable_def: any_identifier array colon_attribute
2032 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
2033 $$
->loc
= get_location
(&@
1);
2035 $$
->array_size
= $2;
2036 $$
->semantic
= $3.semantic
;
2037 $$
->reg_reservation
= $3.reg_reservation
;
2039 | any_identifier array colon_attribute
'=' complex_initializer
2041 TRACE
("Declaration with initializer.\n");
2042 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
2043 $$
->loc
= get_location
(&@
1);
2045 $$
->array_size
= $2;
2046 $$
->semantic
= $3.semantic
;
2047 $$
->reg_reservation
= $3.reg_reservation
;
2048 $$
->initializer
= $5;
2057 unsigned int size
= evaluate_array_dimension
(node_from_list
($2));
2059 free_instr_list
($2);
2063 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
,
2064 "array size is not a positive integer constant\n");
2067 TRACE
("Array size %u.\n", size
);
2071 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
,
2072 "array size must be between 1 and 65536");
2078 var_modifiers: /* Empty */
2082 | KW_EXTERN var_modifiers
2084 $$
= add_modifiers
($2, HLSL_STORAGE_EXTERN
, get_location
(&@
1));
2086 | KW_NOINTERPOLATION var_modifiers
2088 $$
= add_modifiers
($2, HLSL_STORAGE_NOINTERPOLATION
, get_location
(&@
1));
2090 | KW_PRECISE var_modifiers
2092 $$
= add_modifiers
($2, HLSL_MODIFIER_PRECISE
, get_location
(&@
1));
2094 | KW_SHARED var_modifiers
2096 $$
= add_modifiers
($2, HLSL_STORAGE_SHARED
, get_location
(&@
1));
2098 | KW_GROUPSHARED var_modifiers
2100 $$
= add_modifiers
($2, HLSL_STORAGE_GROUPSHARED
, get_location
(&@
1));
2102 | KW_STATIC var_modifiers
2104 $$
= add_modifiers
($2, HLSL_STORAGE_STATIC
, get_location
(&@
1));
2106 | KW_UNIFORM var_modifiers
2108 $$
= add_modifiers
($2, HLSL_STORAGE_UNIFORM
, get_location
(&@
1));
2110 | KW_VOLATILE var_modifiers
2112 $$
= add_modifiers
($2, HLSL_STORAGE_VOLATILE
, get_location
(&@
1));
2114 | KW_CONST var_modifiers
2116 $$
= add_modifiers
($2, HLSL_MODIFIER_CONST
, get_location
(&@
1));
2118 | KW_ROW_MAJOR var_modifiers
2120 $$
= add_modifiers
($2, HLSL_MODIFIER_ROW_MAJOR
, get_location
(&@
1));
2122 | KW_COLUMN_MAJOR var_modifiers
2124 $$
= add_modifiers
($2, HLSL_MODIFIER_COLUMN_MAJOR
, get_location
(&@
1));
2127 complex_initializer: initializer_expr
2130 if
(!($$.args
= d3dcompiler_alloc
(sizeof
(*$$.args
))))
2132 $$.args
[0] = node_from_list
($1);
2135 |
'{' initializer_expr_list
'}'
2139 |
'{' initializer_expr_list
',' '}'
2144 initializer_expr: assignment_expr
2149 initializer_expr_list: initializer_expr
2152 if
(!($$.args
= d3dcompiler_alloc
(sizeof
(*$$.args
))))
2154 $$.args
[0] = node_from_list
($1);
2157 | initializer_expr_list
',' initializer_expr
2160 if
(!($$.args
= d3dcompiler_realloc
($$.args
, ($$.args_count
+ 1) * sizeof
(*$$.args
))))
2162 $$.args
[$$.args_count
++] = node_from_list
($3);
2163 list_move_tail
($$.instrs
, $3);
2164 d3dcompiler_free
($3);
2176 statement_list: statement
2180 | statement_list statement
2183 list_move_tail
($$
, $2);
2184 d3dcompiler_free
($2);
2187 statement: declaration_statement
2189 | compound_statement
2191 | selection_statement
2198 if
(!add_return
($2, node_from_list
($2), get_location
(&@
1)))
2204 if
(!($$
= d3dcompiler_alloc
(sizeof
(*$$
))))
2207 if
(!add_return
($$
, NULL
, get_location
(&@
1)))
2211 selection_statement: KW_IF
'(' expr
')' if_body
2213 struct hlsl_ir_node
*condition
= node_from_list
($3);
2214 struct hlsl_ir_if
*instr
;
2216 if
(!(instr
= new_if
(condition
, get_location
(&@
1))))
2218 list_move_tail
(&instr
->then_instrs
, $5.then_instrs
);
2219 list_move_tail
(&instr
->else_instrs
, $5.else_instrs
);
2220 d3dcompiler_free
($5.then_instrs
);
2221 d3dcompiler_free
($5.else_instrs
);
2222 if
(condition
->data_type
->dimx
> 1 || condition
->data_type
->dimy
> 1)
2224 hlsl_report_message
(instr
->node.loc
, HLSL_LEVEL_ERROR
,
2225 "if condition requires a scalar");
2228 list_add_tail
($$
, &instr
->node.entry
);
2233 $$.then_instrs
= $1;
2234 $$.else_instrs
= NULL
;
2236 | statement KW_ELSE statement
2238 $$.then_instrs
= $1;
2239 $$.else_instrs
= $3;
2242 loop_statement: KW_WHILE
'(' expr
')' statement
2244 $$
= create_loop
(LOOP_WHILE
, NULL
, $3, NULL
, $5, get_location
(&@
1));
2246 | KW_DO statement KW_WHILE
'(' expr
')' ';'
2248 $$
= create_loop
(LOOP_DO_WHILE
, NULL
, $5, NULL
, $2, get_location
(&@
1));
2250 | KW_FOR
'(' scope_start expr_statement expr_statement expr
')' statement
2252 $$
= create_loop
(LOOP_FOR
, $4, $5, $6, $8, get_location
(&@
1));
2253 pop_scope
(&hlsl_ctx
);
2255 | KW_FOR
'(' scope_start declaration expr_statement expr
')' statement
2258 hlsl_report_message
(get_location
(&@
4), HLSL_LEVEL_WARNING
,
2259 "no expressions in for loop initializer");
2260 $$
= create_loop
(LOOP_FOR
, $4, $5, $6, $8, get_location
(&@
1));
2261 pop_scope
(&hlsl_ctx
);
2266 $$
= d3dcompiler_alloc
(sizeof
(*$$
));
2274 primary_expr: C_FLOAT
2276 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
2279 ERR
("Out of memory.\n");
2282 init_node
(&c
->node
, HLSL_IR_CONSTANT
,
2283 hlsl_ctx.builtin_types.scalar
[HLSL_TYPE_FLOAT
], get_location
(&@
1));
2285 if
(!($$
= make_list
(&c
->node
)))
2290 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
2293 ERR
("Out of memory.\n");
2296 init_node
(&c
->node
, HLSL_IR_CONSTANT
,
2297 hlsl_ctx.builtin_types.scalar
[HLSL_TYPE_INT
], get_location
(&@
1));
2299 if
(!($$
= make_list
(&c
->node
)))
2304 struct hlsl_ir_constant
*c
= d3dcompiler_alloc
(sizeof
(*c
));
2307 ERR
("Out of memory.\n");
2310 init_node
(&c
->node
, HLSL_IR_CONSTANT
,
2311 hlsl_ctx.builtin_types.scalar
[HLSL_TYPE_BOOL
], get_location
(&@
1));
2313 if
(!($$
= make_list
(&c
->node
)))
2318 struct hlsl_ir_load
*load
;
2319 struct hlsl_ir_var
*var
;
2321 if
(!(var
= get_variable
(hlsl_ctx.cur_scope
, $1)))
2323 hlsl_report_message
(get_location
(&@
1), HLSL_LEVEL_ERROR
,
2324 "variable '%s' is not declared\n", $1);
2327 if
((load
= new_var_load
(var
, get_location
(&@
1))))
2329 if
(!($$
= make_list
(&load
->node
)))
2340 postfix_expr: primary_expr
2344 | postfix_expr OP_INC
2346 struct source_location loc
;
2347 struct hlsl_ir_node
*inc
;
2349 loc
= get_location
(&@
2);
2350 if
(node_from_list
($1)->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2352 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "modifying a const expression");
2355 inc
= new_unary_expr
(HLSL_IR_UNOP_POSTINC
, node_from_list
($1), loc
);
2356 /* Post increment/decrement expressions are considered const */
2357 inc
->data_type
= clone_hlsl_type
(inc
->data_type
, 0);
2358 inc
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
2359 $$
= append_unop
($1, inc
);
2361 | postfix_expr OP_DEC
2363 struct source_location loc
;
2364 struct hlsl_ir_node
*inc
;
2366 loc
= get_location
(&@
2);
2367 if
(node_from_list
($1)->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2369 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "modifying a const expression");
2372 inc
= new_unary_expr
(HLSL_IR_UNOP_POSTDEC
, node_from_list
($1), loc
);
2373 /* Post increment/decrement expressions are considered const */
2374 inc
->data_type
= clone_hlsl_type
(inc
->data_type
, 0);
2375 inc
->data_type
->modifiers |
= HLSL_MODIFIER_CONST
;
2376 $$
= append_unop
($1, inc
);
2378 | postfix_expr
'.' any_identifier
2380 struct hlsl_ir_node
*node
= node_from_list
($1);
2381 struct source_location loc
;
2383 loc
= get_location
(&@
2);
2384 if
(node
->data_type
->type
== HLSL_CLASS_STRUCT
)
2386 struct hlsl_type
*type
= node
->data_type
;
2387 struct hlsl_struct_field
*field
;
2390 LIST_FOR_EACH_ENTRY
(field
, type
->e.elements
, struct hlsl_struct_field
, entry
)
2392 if
(!strcmp
($3, field
->name
))
2394 if
(!add_record_load
($1, node
, field
, loc
))
2402 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
,
2403 "invalid subscript %s", debugstr_a
($3));
2407 else if
(node
->data_type
->type
<= HLSL_CLASS_LAST_NUMERIC
)
2409 struct hlsl_ir_swizzle
*swizzle
;
2411 swizzle
= get_swizzle
(node
, $3, &loc
);
2414 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
,
2415 "invalid swizzle %s", debugstr_a
($3));
2418 $$
= append_unop
($1, &swizzle
->node
);
2422 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
,
2423 "invalid subscript %s", debugstr_a
($3));
2427 | postfix_expr
'[' expr
']'
2429 struct hlsl_ir_node
*array
= node_from_list
($1), *index
= node_from_list
($3);
2431 list_move_tail
($1, $3);
2432 d3dcompiler_free
($3);
2434 if
(index
->data_type
->type
!= HLSL_CLASS_SCALAR
)
2436 hlsl_report_message
(get_location
(&@
3), HLSL_LEVEL_ERROR
, "array index is not scalar");
2437 free_instr_list
($1);
2441 if
(!add_array_load
($1, array
, index
, get_location
(&@
2)))
2443 free_instr_list
($1);
2449 /* "var_modifiers" doesn't make sense in this case, but it's needed
2450 in the grammar to avoid shift/reduce conflicts. */
2451 | var_modifiers type
'(' initializer_expr_list
')'
2453 struct hlsl_ir_assignment
*assignment
;
2454 unsigned int i
, writemask_offset
= 0;
2455 static unsigned int counter
;
2456 struct hlsl_ir_load
*load
;
2457 struct hlsl_ir_var
*var
;
2462 hlsl_report_message
(get_location
(&@
1), HLSL_LEVEL_ERROR
,
2463 "unexpected modifier on a constructor\n");
2466 if
($2->type
> HLSL_CLASS_LAST_NUMERIC
)
2468 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
,
2469 "constructors may only be used with numeric data types\n");
2472 if
($2->dimx
* $2->dimy
!= initializer_size
(&$4))
2474 hlsl_report_message
(get_location
(&@
4), HLSL_LEVEL_ERROR
,
2475 "expected %u components in constructor, but got %u\n",
2476 $2->dimx
* $2->dimy
, initializer_size
(&$4));
2480 if
($2->type
== HLSL_CLASS_MATRIX
)
2481 FIXME
("Matrix constructors are not supported yet.\n");
2483 sprintf
(name
, "<constructor-%x>", counter
++);
2484 if
(!(var
= new_synthetic_var
(name
, $2, get_location
(&@
2))))
2486 for
(i
= 0; i
< $4.args_count
; ++i
)
2488 struct hlsl_ir_node
*arg
= $4.args
[i
];
2491 if
(arg
->data_type
->type
== HLSL_CLASS_OBJECT
)
2493 hlsl_report_message
(arg
->loc
, HLSL_LEVEL_ERROR
,
2494 "invalid constructor argument");
2497 width
= components_count_type
(arg
->data_type
);
2501 FIXME
("Constructor argument with %u components.\n", width
);
2505 if
(!(arg
= add_implicit_conversion
($4.instrs
, arg
,
2506 hlsl_ctx.builtin_types.vector
[$2->base_type
][width
- 1], &arg
->loc
)))
2509 if
(!(assignment
= new_assignment
(var
, NULL
, arg
,
2510 ((1 << width
) - 1) << writemask_offset
, arg
->loc
)))
2512 writemask_offset
+= width
;
2513 list_add_tail
($4.instrs
, &assignment
->node.entry
);
2515 d3dcompiler_free
($4.args
);
2516 if
(!(load
= new_var_load
(var
, get_location
(&@
2))))
2518 $$
= append_unop
($4.instrs
, &load
->node
);
2521 unary_expr: postfix_expr
2527 struct source_location loc
;
2529 loc
= get_location
(&@
1);
2530 if
(node_from_list
($2)->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2532 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "modifying a const expression");
2535 $$
= append_unop
($2, new_unary_expr
(HLSL_IR_UNOP_PREINC
, node_from_list
($2), loc
));
2539 struct source_location loc
;
2541 loc
= get_location
(&@
1);
2542 if
(node_from_list
($2)->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2544 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "modifying a const expression");
2547 $$
= append_unop
($2, new_unary_expr
(HLSL_IR_UNOP_PREDEC
, node_from_list
($2), loc
));
2549 | unary_op unary_expr
2551 enum hlsl_ir_expr_op ops
[] = {0, HLSL_IR_UNOP_NEG
,
2552 HLSL_IR_UNOP_LOGIC_NOT
, HLSL_IR_UNOP_BIT_NOT
};
2554 if
($1 == UNARY_OP_PLUS
)
2560 $$
= append_unop
($2, new_unary_expr
(ops
[$1], node_from_list
($2), get_location
(&@
1)));
2563 /* var_modifiers just to avoid shift/reduce conflicts */
2564 |
'(' var_modifiers type array
')' unary_expr
2566 struct hlsl_type
*src_type
= node_from_list
($6)->data_type
;
2567 struct hlsl_type
*dst_type
;
2568 struct source_location loc
;
2570 loc
= get_location
(&@
3);
2573 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "unexpected modifier in a cast");
2578 dst_type
= new_array_type
($3, $4);
2582 if
(!compatible_data_types
(src_type
, dst_type
))
2584 hlsl_report_message
(loc
, HLSL_LEVEL_ERROR
, "can't cast from %s to %s",
2585 debug_hlsl_type
(src_type
), debug_hlsl_type
(dst_type
));
2589 $$
= append_unop
($6, &new_cast
(node_from_list
($6), dst_type
, &loc
)->node
);
2598 $$
= UNARY_OP_MINUS
;
2602 $$
= UNARY_OP_LOGICNOT
;
2606 $$
= UNARY_OP_BITNOT
;
2612 | mul_expr
'*' unary_expr
2614 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_MUL
, get_location
(&@
2));
2616 | mul_expr
'/' unary_expr
2618 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_DIV
, get_location
(&@
2));
2620 | mul_expr
'%' unary_expr
2622 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_MOD
, get_location
(&@
2));
2628 | add_expr
'+' mul_expr
2630 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_ADD
, get_location
(&@
2));
2632 | add_expr
'-' mul_expr
2634 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_SUB
, get_location
(&@
2));
2637 shift_expr: add_expr
2641 | shift_expr OP_LEFTSHIFT add_expr
2643 FIXME
("Left shift\n");
2645 | shift_expr OP_RIGHTSHIFT add_expr
2647 FIXME
("Right shift\n");
2653 | relational_expr
'<' shift_expr
2655 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_LESS
, get_location
(&@
2));
2657 | relational_expr
'>' shift_expr
2659 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_GREATER
, get_location
(&@
2));
2661 | relational_expr OP_LE shift_expr
2663 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_LEQUAL
, get_location
(&@
2));
2665 | relational_expr OP_GE shift_expr
2667 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_GEQUAL
, get_location
(&@
2));
2673 | equality_expr OP_EQ relational_expr
2675 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_EQUAL
, get_location
(&@
2));
2677 | equality_expr OP_NE relational_expr
2679 $$
= add_binary_expr
($1, $3, HLSL_IR_BINOP_NEQUAL
, get_location
(&@
2));
2682 bitand_expr: equality_expr
2686 | bitand_expr
'&' equality_expr
2688 FIXME
("bitwise AND\n");
2691 bitxor_expr: bitand_expr
2695 | bitxor_expr
'^' bitand_expr
2697 FIXME
("bitwise XOR\n");
2700 bitor_expr: bitxor_expr
2704 | bitor_expr
'|' bitxor_expr
2706 FIXME
("bitwise OR\n");
2709 logicand_expr: bitor_expr
2713 | logicand_expr OP_AND bitor_expr
2715 FIXME
("logic AND\n");
2718 logicor_expr: logicand_expr
2722 | logicor_expr OP_OR logicand_expr
2724 FIXME
("logic OR\n");
2727 conditional_expr: logicor_expr
2731 | logicor_expr
'?' expr
':' assignment_expr
2733 FIXME
("ternary operator\n");
2739 | unary_expr assign_op assignment_expr
2741 struct hlsl_ir_node
*lhs
= node_from_list
($1), *rhs
= node_from_list
($3);
2743 if
(lhs
->data_type
->modifiers
& HLSL_MODIFIER_CONST
)
2745 hlsl_report_message
(get_location
(&@
2), HLSL_LEVEL_ERROR
, "l-value is const");
2748 list_move_tail
($3, $1);
2749 d3dcompiler_free
($1);
2750 if
(!add_assignment
($3, lhs
, $2, rhs
))
2757 $$
= ASSIGN_OP_ASSIGN
;
2779 | OP_LEFTSHIFTASSIGN
2781 $$
= ASSIGN_OP_LSHIFT
;
2783 | OP_RIGHTSHIFTASSIGN
2785 $$
= ASSIGN_OP_RSHIFT
;
2800 expr: assignment_expr
2804 | expr
',' assignment_expr
2807 list_move_tail
($$
, $3);
2808 d3dcompiler_free
($3);
2813 static struct source_location get_location
(const struct YYLTYPE *l
)
2815 const struct source_location loc
=
2817 .file
= hlsl_ctx.source_file
,
2818 .line
= l
->first_line
,
2819 .col
= l
->first_column
,
2824 static void dump_function_decl
(struct wine_rb_entry
*entry
, void *context
)
2826 struct hlsl_ir_function_decl
*func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function_decl
, entry
);
2828 debug_dump_ir_function_decl
(func
);
2831 static void dump_function
(struct wine_rb_entry
*entry
, void *context
)
2833 struct hlsl_ir_function
*func
= WINE_RB_ENTRY_VALUE
(entry
, struct hlsl_ir_function
, entry
);
2834 wine_rb_for_each_entry
(&func
->overloads
, dump_function_decl
, NULL
);
2837 /* Allocate a unique, ordered index to each instruction, which will be used for
2838 * computing liveness ranges. */
2839 static unsigned int index_instructions
(struct list
*instrs
, unsigned int index
)
2841 struct hlsl_ir_node
*instr
;
2843 LIST_FOR_EACH_ENTRY
(instr
, instrs
, struct hlsl_ir_node
, entry
)
2845 instr
->index
= index
++;
2847 if
(instr
->type
== HLSL_IR_IF
)
2849 struct hlsl_ir_if
*iff
= if_from_node
(instr
);
2850 index
= index_instructions
(&iff
->then_instrs
, index
);
2851 index
= index_instructions
(&iff
->else_instrs
, index
);
2853 else if
(instr
->type
== HLSL_IR_LOOP
)
2855 index
= index_instructions
(&loop_from_node
(instr
)->body
, index
);
2856 loop_from_node
(instr
)->next_index
= index
;
2863 /* Compute the earliest and latest liveness for each variable. In the case that
2864 * a variable is accessed inside of a loop, we promote its liveness to extend
2865 * to at least the range of the entire loop. Note that we don't need to do this
2866 * for anonymous nodes, since there's currently no way to use a node which was
2867 * calculated in an earlier iteration of the loop. */
2868 static void compute_liveness_recurse
(struct list
*instrs
, unsigned int loop_first
, unsigned int loop_last
)
2870 struct hlsl_ir_node
*instr
;
2871 struct hlsl_ir_var
*var
;
2873 LIST_FOR_EACH_ENTRY
(instr
, instrs
, struct hlsl_ir_node
, entry
)
2875 switch
(instr
->type
)
2877 case HLSL_IR_ASSIGNMENT
:
2879 struct hlsl_ir_assignment
*assignment
= assignment_from_node
(instr
);
2880 var
= assignment
->lhs.var
;
2881 if
(!var
->first_write
)
2882 var
->first_write
= loop_first ? min
(instr
->index
, loop_first
) : instr
->index
;
2883 assignment
->rhs.node
->last_read
= instr
->index
;
2884 if
(assignment
->lhs.offset.node
)
2885 assignment
->lhs.offset.node
->last_read
= instr
->index
;
2890 struct hlsl_ir_expr
*expr
= expr_from_node
(instr
);
2893 for
(i
= 0; i
< ARRAY_SIZE
(expr
->operands
) && expr
->operands
[i
].node
; ++i
)
2894 expr
->operands
[i
].node
->last_read
= instr
->index
;
2899 struct hlsl_ir_if
*iff
= if_from_node
(instr
);
2900 compute_liveness_recurse
(&iff
->then_instrs
, loop_first
, loop_last
);
2901 compute_liveness_recurse
(&iff
->else_instrs
, loop_first
, loop_last
);
2902 iff
->condition.node
->last_read
= instr
->index
;
2907 struct hlsl_ir_load
*load
= load_from_node
(instr
);
2908 var
= load
->src.var
;
2909 var
->last_read
= loop_last ? max
(instr
->index
, loop_last
) : instr
->index
;
2910 if
(load
->src.offset.node
)
2911 load
->src.offset.node
->last_read
= instr
->index
;
2916 struct hlsl_ir_loop
*loop
= loop_from_node
(instr
);
2917 compute_liveness_recurse
(&loop
->body
, loop_first ? loop_first
: instr
->index
,
2918 loop_last ? loop_last
: loop
->next_index
);
2921 case HLSL_IR_SWIZZLE
:
2923 struct hlsl_ir_swizzle
*swizzle
= swizzle_from_node
(instr
);
2924 swizzle
->val.node
->last_read
= instr
->index
;
2927 case HLSL_IR_CONSTANT
:
2934 static void compute_liveness
(struct hlsl_ir_function_decl
*entry_func
)
2936 struct hlsl_ir_var
*var
;
2938 LIST_FOR_EACH_ENTRY
(var
, &hlsl_ctx.globals
->vars
, struct hlsl_ir_var
, scope_entry
)
2940 var
->first_write
= 1;
2943 LIST_FOR_EACH_ENTRY
(var
, entry_func
->parameters
, struct hlsl_ir_var
, param_entry
)
2945 if
(var
->modifiers
& HLSL_STORAGE_IN
)
2946 var
->first_write
= 1;
2947 if
(var
->modifiers
& HLSL_STORAGE_OUT
)
2948 var
->last_read
= UINT_MAX
;
2951 if
(entry_func
->return_var
)
2952 entry_func
->return_var
->last_read
= UINT_MAX
;
2954 compute_liveness_recurse
(entry_func
->body
, 0, 0);
2957 HRESULT parse_hlsl
(enum shader_type type
, DWORD major
, DWORD minor
,
2958 const char *entrypoint
, ID3D10Blob
**shader_blob
, char **messages
)
2960 struct hlsl_ir_function_decl
*entry_func
;
2961 struct hlsl_scope
*scope
, *next_scope
;
2962 struct hlsl_type
*hlsl_type
, *next_type
;
2963 struct hlsl_ir_var
*var
, *next_var
;
2964 HRESULT hr
= E_FAIL
;
2967 hlsl_ctx.status
= PARSE_SUCCESS
;
2968 hlsl_ctx.messages.size
= hlsl_ctx.messages.capacity
= 0;
2969 hlsl_ctx.line_no
= hlsl_ctx.column
= 1;
2970 hlsl_ctx.source_file
= d3dcompiler_strdup
("");
2971 hlsl_ctx.source_files
= d3dcompiler_alloc
(sizeof
(*hlsl_ctx.source_files
));
2972 if
(hlsl_ctx.source_files
)
2973 hlsl_ctx.source_files
[0] = hlsl_ctx.source_file
;
2974 hlsl_ctx.source_files_count
= 1;
2975 hlsl_ctx.cur_scope
= NULL
;
2976 hlsl_ctx.matrix_majority
= HLSL_COLUMN_MAJOR
;
2977 list_init
(&hlsl_ctx.scopes
);
2978 list_init
(&hlsl_ctx.types
);
2979 init_functions_tree
(&hlsl_ctx.functions
);
2980 list_init
(&hlsl_ctx.static_initializers
);
2982 push_scope
(&hlsl_ctx
);
2983 hlsl_ctx.globals
= hlsl_ctx.cur_scope
;
2984 declare_predefined_types
(hlsl_ctx.globals
);
2988 if
(hlsl_ctx.status
== PARSE_ERR
)
2991 if
(!(entry_func
= get_func_entry
(entrypoint
)))
2993 hlsl_message
("error: entry point %s is not defined\n", debugstr_a
(entrypoint
));
2997 if
(!type_is_void
(entry_func
->return_type
)
2998 && entry_func
->return_type
->type
!= HLSL_CLASS_STRUCT
&& !entry_func
->semantic
)
3000 hlsl_report_message
(entry_func
->loc
, HLSL_LEVEL_ERROR
,
3001 "entry point \"%s\" is missing a return value semantic", entry_func
->func
->name
);
3004 list_move_head
(entry_func
->body
, &hlsl_ctx.static_initializers
);
3006 /* Index 0 means unused; index 1 means function entry, so start at 2. */
3007 index_instructions
(entry_func
->body
, 2);
3009 if
(TRACE_ON
(hlsl_parser
))
3011 TRACE
("IR dump.\n");
3012 wine_rb_for_each_entry
(&hlsl_ctx.functions
, dump_function
, NULL
);
3015 compute_liveness
(entry_func
);
3017 if
(hlsl_ctx.status
!= PARSE_ERR
)
3023 if
(hlsl_ctx.messages.size
)
3024 *messages
= hlsl_ctx.messages.
string;
3030 if
(hlsl_ctx.messages.capacity
)
3031 d3dcompiler_free
(hlsl_ctx.messages.
string);
3034 for
(i
= 0; i
< hlsl_ctx.source_files_count
; ++i
)
3035 d3dcompiler_free
((void *)hlsl_ctx.source_files
[i
]);
3036 d3dcompiler_free
(hlsl_ctx.source_files
);
3038 TRACE
("Freeing functions IR.\n");
3039 wine_rb_destroy
(&hlsl_ctx.functions
, free_function_rb
, NULL
);
3041 TRACE
("Freeing variables.\n");
3042 LIST_FOR_EACH_ENTRY_SAFE
(scope
, next_scope
, &hlsl_ctx.scopes
, struct hlsl_scope
, entry
)
3044 LIST_FOR_EACH_ENTRY_SAFE
(var
, next_var
, &scope
->vars
, struct hlsl_ir_var
, scope_entry
)
3046 free_declaration
(var
);
3048 wine_rb_destroy
(&scope
->types
, NULL
, NULL
);
3049 d3dcompiler_free
(scope
);
3052 TRACE
("Freeing types.\n");
3053 LIST_FOR_EACH_ENTRY_SAFE
(hlsl_type
, next_type
, &hlsl_ctx.types
, struct hlsl_type
, entry
)
3055 free_hlsl_type
(hlsl_type
);