1 /*
2  * Copyright 2008 Stefan Dösinger
3  * Copyright 2009 Matteo Bruni
4  * Copyright 2008-2009 Henri Verbeet for CodeWeavers
5  * Copyright 2010 Rico Schüller
6  * Copyright 2012 Matteo Bruni for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23 
24 #include "config.h"
25 #include "wine/port.h"
26 
27 #include <stdio.h>
28 
29 #include "d3dcompiler_private.h"
30 
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
32 
33 #define WINE_D3DCOMPILER_TO_STR(x) case x: return #x
34 
35 const char *debug_d3dcompiler_shader_variable_class(D3D_SHADER_VARIABLE_CLASS c)
36 {
37     switch (c)
38     {
39         WINE_D3DCOMPILER_TO_STR(D3D_SVC_SCALAR);
40         WINE_D3DCOMPILER_TO_STR(D3D_SVC_VECTOR);
41         WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_ROWS);
42         WINE_D3DCOMPILER_TO_STR(D3D_SVC_MATRIX_COLUMNS);
43         WINE_D3DCOMPILER_TO_STR(D3D_SVC_OBJECT);
44         WINE_D3DCOMPILER_TO_STR(D3D_SVC_STRUCT);
45         WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_CLASS);
46         WINE_D3DCOMPILER_TO_STR(D3D_SVC_INTERFACE_POINTER);
47         default:
48             FIXME("Unrecognized D3D_SHADER_VARIABLE_CLASS %#x.\n", c);
49             return "unrecognized";
50     }
51 }
52 
53 const char *debug_d3dcompiler_shader_variable_type(D3D_SHADER_VARIABLE_TYPE t)
54 {
55     switch (t)
56     {
57         WINE_D3DCOMPILER_TO_STR(D3D_SVT_VOID);
58         WINE_D3DCOMPILER_TO_STR(D3D_SVT_BOOL);
59         WINE_D3DCOMPILER_TO_STR(D3D_SVT_INT);
60         WINE_D3DCOMPILER_TO_STR(D3D_SVT_FLOAT);
61         WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRING);
62         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE);
63         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1D);
64         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2D);
65         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE3D);
66         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBE);
67         WINE_D3DCOMPILER_TO_STR(D3D_SVT_SAMPLER);
68         WINE_D3DCOMPILER_TO_STR(D3D_SVT_PIXELSHADER);
69         WINE_D3DCOMPILER_TO_STR(D3D_SVT_VERTEXSHADER);
70         WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT);
71         WINE_D3DCOMPILER_TO_STR(D3D_SVT_UINT8);
72         WINE_D3DCOMPILER_TO_STR(D3D_SVT_GEOMETRYSHADER);
73         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RASTERIZER);
74         WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCIL);
75         WINE_D3DCOMPILER_TO_STR(D3D_SVT_BLEND);
76         WINE_D3DCOMPILER_TO_STR(D3D_SVT_BUFFER);
77         WINE_D3DCOMPILER_TO_STR(D3D_SVT_CBUFFER);
78         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TBUFFER);
79         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE1DARRAY);
80         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DARRAY);
81         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RENDERTARGETVIEW);
82         WINE_D3DCOMPILER_TO_STR(D3D_SVT_DEPTHSTENCILVIEW);
83         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMS);
84         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURE2DMSARRAY);
85         WINE_D3DCOMPILER_TO_STR(D3D_SVT_TEXTURECUBEARRAY);
86         WINE_D3DCOMPILER_TO_STR(D3D_SVT_HULLSHADER);
87         WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOMAINSHADER);
88         WINE_D3DCOMPILER_TO_STR(D3D_SVT_INTERFACE_POINTER);
89         WINE_D3DCOMPILER_TO_STR(D3D_SVT_COMPUTESHADER);
90         WINE_D3DCOMPILER_TO_STR(D3D_SVT_DOUBLE);
91         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1D);
92         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE1DARRAY);
93         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2D);
94         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE2DARRAY);
95         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWTEXTURE3D);
96         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBUFFER);
97         WINE_D3DCOMPILER_TO_STR(D3D_SVT_BYTEADDRESS_BUFFER);
98         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWBYTEADDRESS_BUFFER);
99         WINE_D3DCOMPILER_TO_STR(D3D_SVT_STRUCTURED_BUFFER);
100         WINE_D3DCOMPILER_TO_STR(D3D_SVT_RWSTRUCTURED_BUFFER);
101         WINE_D3DCOMPILER_TO_STR(D3D_SVT_APPEND_STRUCTURED_BUFFER);
102         WINE_D3DCOMPILER_TO_STR(D3D_SVT_CONSUME_STRUCTURED_BUFFER);
103         default:
104             FIXME("Unrecognized D3D_SHADER_VARIABLE_TYPE %#x.\n", t);
105             return "unrecognized";
106     }
107 }
108 
109 const char *debug_d3dcompiler_d3d_blob_part(D3D_BLOB_PART part)
110 {
111     switch(part)
112     {
113         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_SIGNATURE_BLOB);
114         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_OUTPUT_SIGNATURE_BLOB);
115         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB);
116         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB);
117         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_ALL_SIGNATURE_BLOB);
118         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_DEBUG_INFO);
119         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_LEGACY_SHADER);
120         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_PREPASS_SHADER);
121         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_XNA_SHADER);
122         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_ALTERNATE_SHADER);
123         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_DETAILS);
124         WINE_D3DCOMPILER_TO_STR(D3D_BLOB_TEST_COMPILE_PERF);
125         default:
126             FIXME("Unrecognized D3D_BLOB_PART %#x\n", part);
127             return "unrecognized";
128     }
129 }
130 
131 const char *debug_print_srcmod(DWORD mod)
132 {
133     switch (mod)
134     {
135         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NEG);
136         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIAS);
137         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_BIASNEG);
138         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGN);
139         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_SIGNNEG);
140         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_COMP);
141         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2);
142         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_X2NEG);
143         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DZ);
144         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_DW);
145         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABS);
146         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_ABSNEG);
147         WINE_D3DCOMPILER_TO_STR(BWRITERSPSM_NOT);
148         default:
149             FIXME("Unrecognized source modifier %#x.\n", mod);
150             return "unrecognized_src_mod";
151     }
152 }
153 
154 #undef WINE_D3DCOMPILER_TO_STR
155 
156 const char *debug_print_dstmod(DWORD mod)
157 {
158     switch (mod)
159     {
160         case 0:
161             return "";
162         case BWRITERSPDM_SATURATE:
163             return "_sat";
164         case BWRITERSPDM_PARTIALPRECISION:
165             return "_pp";
166         case BWRITERSPDM_MSAMPCENTROID:
167             return "_centroid";
168         case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION:
169             return "_sat_pp";
170         case BWRITERSPDM_SATURATE | BWRITERSPDM_MSAMPCENTROID:
171             return "_sat_centroid";
172         case BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
173             return "_pp_centroid";
174         case BWRITERSPDM_SATURATE | BWRITERSPDM_PARTIALPRECISION | BWRITERSPDM_MSAMPCENTROID:
175             return "_sat_pp_centroid";
176         default:
177             return "Unexpected modifier\n";
178     }
179 }
180 
181 const char *debug_print_shift(DWORD shift)
182 {
183     static const char * const shiftstrings[] =
184     {
185         "",
186         "_x2",
187         "_x4",
188         "_x8",
189         "_x16",
190         "_x32",
191         "",
192         "",
193         "",
194         "",
195         "",
196         "",
197         "_d16",
198         "_d8",
199         "_d4",
200         "_d2",
201     };
202     return shiftstrings[shift];
203 }
204 
205 static const char *get_regname(const struct shader_reg *reg)
206 {
207     switch (reg->type)
208     {
209         case BWRITERSPR_TEMP:
210             return wine_dbg_sprintf("r%u", reg->regnum);
211         case BWRITERSPR_INPUT:
212             return wine_dbg_sprintf("v%u", reg->regnum);
213         case BWRITERSPR_CONST:
214             return wine_dbg_sprintf("c%u", reg->regnum);
215         case BWRITERSPR_ADDR:
216             return wine_dbg_sprintf("a%u", reg->regnum);
217         case BWRITERSPR_TEXTURE:
218             return wine_dbg_sprintf("t%u", reg->regnum);
219         case BWRITERSPR_RASTOUT:
220             switch (reg->regnum)
221             {
222                 case BWRITERSRO_POSITION:   return "oPos";
223                 case BWRITERSRO_FOG:        return "oFog";
224                 case BWRITERSRO_POINT_SIZE: return "oPts";
225                 default: return "Unexpected RASTOUT";
226             }
227         case BWRITERSPR_ATTROUT:
228             return wine_dbg_sprintf("oD%u", reg->regnum);
229         case BWRITERSPR_TEXCRDOUT:
230             return wine_dbg_sprintf("oT%u", reg->regnum);
231         case BWRITERSPR_OUTPUT:
232             return wine_dbg_sprintf("o%u", reg->regnum);
233         case BWRITERSPR_CONSTINT:
234             return wine_dbg_sprintf("i%u", reg->regnum);
235         case BWRITERSPR_COLOROUT:
236             return wine_dbg_sprintf("oC%u", reg->regnum);
237         case BWRITERSPR_DEPTHOUT:
238             return "oDepth";
239         case BWRITERSPR_SAMPLER:
240             return wine_dbg_sprintf("s%u", reg->regnum);
241         case BWRITERSPR_CONSTBOOL:
242             return wine_dbg_sprintf("b%u", reg->regnum);
243         case BWRITERSPR_LOOP:
244             return "aL";
245         case BWRITERSPR_MISCTYPE:
246             switch (reg->regnum)
247             {
248                 case 0: return "vPos";
249                 case 1: return "vFace";
250                 default: return "unexpected misctype";
251             }
252         case BWRITERSPR_LABEL:
253             return wine_dbg_sprintf("l%u", reg->regnum);
254         case BWRITERSPR_PREDICATE:
255             return wine_dbg_sprintf("p%u", reg->regnum);
256         default:
257             return wine_dbg_sprintf("unknown regname %#x", reg->type);
258     }
259 }
260 
261 static const char *debug_print_writemask(DWORD mask)
262 {
263     char ret[6];
264     unsigned char pos = 1;
265 
266     if(mask == BWRITERSP_WRITEMASK_ALL) return "";
267     ret[0] = '.';
268     if(mask & BWRITERSP_WRITEMASK_0) ret[pos++] = 'x';
269     if(mask & BWRITERSP_WRITEMASK_1) ret[pos++] = 'y';
270     if(mask & BWRITERSP_WRITEMASK_2) ret[pos++] = 'z';
271     if(mask & BWRITERSP_WRITEMASK_3) ret[pos++] = 'w';
272     ret[pos] = 0;
273 
274     return wine_dbg_sprintf("%s", ret);
275 }
276 
277 static const char *debug_print_swizzle(DWORD arg)
278 {
279     char ret[6];
280     unsigned int i;
281     DWORD swizzle[4];
282 
283     switch (arg)
284     {
285         case BWRITERVS_NOSWIZZLE:
286             return "";
287         case BWRITERVS_SWIZZLE_X:
288             return ".x";
289         case BWRITERVS_SWIZZLE_Y:
290             return ".y";
291         case BWRITERVS_SWIZZLE_Z:
292             return ".z";
293         case BWRITERVS_SWIZZLE_W:
294             return ".w";
295     }
296 
297     swizzle[0] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 0)) & 0x03;
298     swizzle[1] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 2)) & 0x03;
299     swizzle[2] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 4)) & 0x03;
300     swizzle[3] = (arg >> (BWRITERVS_SWIZZLE_SHIFT + 6)) & 0x03;
301 
302     ret[0] = '.';
303     for (i = 0; i < 4; ++i)
304     {
305         switch (swizzle[i])
306         {
307             case 0: ret[1 + i] = 'x'; break;
308             case 1: ret[1 + i] = 'y'; break;
309             case 2: ret[1 + i] = 'z'; break;
310             case 3: ret[1 + i] = 'w'; break;
311         }
312     }
313     ret[5] = '\0';
314 
315     return wine_dbg_sprintf("%s", ret);
316 }
317 
318 static const char *debug_print_relarg(const struct shader_reg *reg)
319 {
320     const char *short_swizzle;
321     if (!reg->rel_reg) return "";
322 
323     short_swizzle = debug_print_swizzle(reg->rel_reg->u.swizzle);
324 
325     if (reg->rel_reg->type == BWRITERSPR_ADDR)
326         return wine_dbg_sprintf("[a%u%s]", reg->rel_reg->regnum, short_swizzle);
327     else if(reg->rel_reg->type == BWRITERSPR_LOOP && reg->rel_reg->regnum == 0)
328         return wine_dbg_sprintf("[aL%s]", short_swizzle);
329     else
330         return "Unexpected relative addressing argument";
331 }
332 
333 const char *debug_print_dstreg(const struct shader_reg *reg)
334 {
335     return wine_dbg_sprintf("%s%s%s", get_regname(reg),
336             debug_print_relarg(reg),
337             debug_print_writemask(reg->u.writemask));
338 }
339 
340 const char *debug_print_srcreg(const struct shader_reg *reg)
341 {
342     switch (reg->srcmod)
343     {
344         case BWRITERSPSM_NONE:
345             return wine_dbg_sprintf("%s%s%s", get_regname(reg),
346                     debug_print_relarg(reg),
347                     debug_print_swizzle(reg->u.swizzle));
348         case BWRITERSPSM_NEG:
349             return wine_dbg_sprintf("-%s%s%s", get_regname(reg),
350                     debug_print_relarg(reg),
351                     debug_print_swizzle(reg->u.swizzle));
352         case BWRITERSPSM_BIAS:
353             return wine_dbg_sprintf("%s%s_bias%s", get_regname(reg),
354                     debug_print_relarg(reg),
355                     debug_print_swizzle(reg->u.swizzle));
356         case BWRITERSPSM_BIASNEG:
357             return wine_dbg_sprintf("-%s%s_bias%s", get_regname(reg),
358                     debug_print_relarg(reg),
359                     debug_print_swizzle(reg->u.swizzle));
360         case BWRITERSPSM_SIGN:
361             return wine_dbg_sprintf("%s%s_bx2%s", get_regname(reg),
362                     debug_print_relarg(reg),
363                     debug_print_swizzle(reg->u.swizzle));
364         case BWRITERSPSM_SIGNNEG:
365             return wine_dbg_sprintf("-%s%s_bx2%s", get_regname(reg),
366                     debug_print_relarg(reg),
367                     debug_print_swizzle(reg->u.swizzle));
368         case BWRITERSPSM_COMP:
369             return wine_dbg_sprintf("1 - %s%s%s", get_regname(reg),
370                     debug_print_relarg(reg),
371                     debug_print_swizzle(reg->u.swizzle));
372         case BWRITERSPSM_X2:
373             return wine_dbg_sprintf("%s%s_x2%s", get_regname(reg),
374                     debug_print_relarg(reg),
375                     debug_print_swizzle(reg->u.swizzle));
376         case BWRITERSPSM_X2NEG:
377             return wine_dbg_sprintf("-%s%s_x2%s", get_regname(reg),
378                     debug_print_relarg(reg),
379                     debug_print_swizzle(reg->u.swizzle));
380         case BWRITERSPSM_DZ:
381             return wine_dbg_sprintf("%s%s_dz%s", get_regname(reg),
382                     debug_print_relarg(reg),
383                     debug_print_swizzle(reg->u.swizzle));
384         case BWRITERSPSM_DW:
385             return wine_dbg_sprintf("%s%s_dw%s", get_regname(reg),
386                     debug_print_relarg(reg),
387                     debug_print_swizzle(reg->u.swizzle));
388         case BWRITERSPSM_ABS:
389             return wine_dbg_sprintf("%s%s_abs%s", get_regname(reg),
390                     debug_print_relarg(reg),
391                     debug_print_swizzle(reg->u.swizzle));
392         case BWRITERSPSM_ABSNEG:
393             return wine_dbg_sprintf("-%s%s_abs%s", get_regname(reg),
394                     debug_print_relarg(reg),
395                     debug_print_swizzle(reg->u.swizzle));
396         case BWRITERSPSM_NOT:
397             return wine_dbg_sprintf("!%s%s%s", get_regname(reg),
398                     debug_print_relarg(reg),
399                     debug_print_swizzle(reg->u.swizzle));
400     }
401     return "Unknown modifier";
402 }
403 
404 const char *debug_print_comp(DWORD comp)
405 {
406     switch (comp)
407     {
408         case BWRITER_COMPARISON_NONE: return "";
409         case BWRITER_COMPARISON_GT:   return "_gt";
410         case BWRITER_COMPARISON_EQ:   return "_eq";
411         case BWRITER_COMPARISON_GE:   return "_ge";
412         case BWRITER_COMPARISON_LT:   return "_lt";
413         case BWRITER_COMPARISON_NE:   return "_ne";
414         case BWRITER_COMPARISON_LE:   return "_le";
415         default: return "_unknown";
416     }
417 }
418 
419 const char *debug_print_opcode(DWORD opcode)
420 {
421     switch (opcode)
422     {
423         case BWRITERSIO_NOP:          return "nop";
424         case BWRITERSIO_MOV:          return "mov";
425         case BWRITERSIO_ADD:          return "add";
426         case BWRITERSIO_SUB:          return "sub";
427         case BWRITERSIO_MAD:          return "mad";
428         case BWRITERSIO_MUL:          return "mul";
429         case BWRITERSIO_RCP:          return "rcp";
430         case BWRITERSIO_RSQ:          return "rsq";
431         case BWRITERSIO_DP3:          return "dp3";
432         case BWRITERSIO_DP4:          return "dp4";
433         case BWRITERSIO_MIN:          return "min";
434         case BWRITERSIO_MAX:          return "max";
435         case BWRITERSIO_SLT:          return "slt";
436         case BWRITERSIO_SGE:          return "sge";
437         case BWRITERSIO_EXP:          return "exp";
438         case BWRITERSIO_LOG:          return "log";
439         case BWRITERSIO_LIT:          return "lit";
440         case BWRITERSIO_DST:          return "dst";
441         case BWRITERSIO_LRP:          return "lrp";
442         case BWRITERSIO_FRC:          return "frc";
443         case BWRITERSIO_M4x4:         return "m4x4";
444         case BWRITERSIO_M4x3:         return "m4x3";
445         case BWRITERSIO_M3x4:         return "m3x4";
446         case BWRITERSIO_M3x3:         return "m3x3";
447         case BWRITERSIO_M3x2:         return "m3x2";
448         case BWRITERSIO_CALL:         return "call";
449         case BWRITERSIO_CALLNZ:       return "callnz";
450         case BWRITERSIO_LOOP:         return "loop";
451         case BWRITERSIO_RET:          return "ret";
452         case BWRITERSIO_ENDLOOP:      return "endloop";
453         case BWRITERSIO_LABEL:        return "label";
454         case BWRITERSIO_DCL:          return "dcl";
455         case BWRITERSIO_POW:          return "pow";
456         case BWRITERSIO_CRS:          return "crs";
457         case BWRITERSIO_SGN:          return "sgn";
458         case BWRITERSIO_ABS:          return "abs";
459         case BWRITERSIO_NRM:          return "nrm";
460         case BWRITERSIO_SINCOS:       return "sincos";
461         case BWRITERSIO_REP:          return "rep";
462         case BWRITERSIO_ENDREP:       return "endrep";
463         case BWRITERSIO_IF:           return "if";
464         case BWRITERSIO_IFC:          return "ifc";
465         case BWRITERSIO_ELSE:         return "else";
466         case BWRITERSIO_ENDIF:        return "endif";
467         case BWRITERSIO_BREAK:        return "break";
468         case BWRITERSIO_BREAKC:       return "breakc";
469         case BWRITERSIO_MOVA:         return "mova";
470         case BWRITERSIO_DEFB:         return "defb";
471         case BWRITERSIO_DEFI:         return "defi";
472         case BWRITERSIO_TEXCOORD:     return "texcoord";
473         case BWRITERSIO_TEXKILL:      return "texkill";
474         case BWRITERSIO_TEX:          return "tex";
475         case BWRITERSIO_TEXBEM:       return "texbem";
476         case BWRITERSIO_TEXBEML:      return "texbeml";
477         case BWRITERSIO_TEXREG2AR:    return "texreg2ar";
478         case BWRITERSIO_TEXREG2GB:    return "texreg2gb";
479         case BWRITERSIO_TEXM3x2PAD:   return "texm3x2pad";
480         case BWRITERSIO_TEXM3x2TEX:   return "texm3x2tex";
481         case BWRITERSIO_TEXM3x3PAD:   return "texm3x3pad";
482         case BWRITERSIO_TEXM3x3TEX:   return "texm3x3tex";
483         case BWRITERSIO_TEXM3x3SPEC:  return "texm3x3vspec";
484         case BWRITERSIO_TEXM3x3VSPEC: return "texm3x3vspec";
485         case BWRITERSIO_EXPP:         return "expp";
486         case BWRITERSIO_LOGP:         return "logp";
487         case BWRITERSIO_CND:          return "cnd";
488         case BWRITERSIO_DEF:          return "def";
489         case BWRITERSIO_TEXREG2RGB:   return "texreg2rgb";
490         case BWRITERSIO_TEXDP3TEX:    return "texdp3tex";
491         case BWRITERSIO_TEXM3x2DEPTH: return "texm3x2depth";
492         case BWRITERSIO_TEXDP3:       return "texdp3";
493         case BWRITERSIO_TEXM3x3:      return "texm3x3";
494         case BWRITERSIO_TEXDEPTH:     return "texdepth";
495         case BWRITERSIO_CMP:          return "cmp";
496         case BWRITERSIO_BEM:          return "bem";
497         case BWRITERSIO_DP2ADD:       return "dp2add";
498         case BWRITERSIO_DSX:          return "dsx";
499         case BWRITERSIO_DSY:          return "dsy";
500         case BWRITERSIO_TEXLDD:       return "texldd";
501         case BWRITERSIO_SETP:         return "setp";
502         case BWRITERSIO_TEXLDL:       return "texldl";
503         case BWRITERSIO_BREAKP:       return "breakp";
504         case BWRITERSIO_PHASE:        return "phase";
505 
506         case BWRITERSIO_TEXLDP:       return "texldp";
507         case BWRITERSIO_TEXLDB:       return "texldb";
508 
509         default:                      return "unknown";
510     }
511 }
512 
513 void skip_dword_unknown(const char **ptr, unsigned int count)
514 {
515     unsigned int i;
516     DWORD d;
517 
518     FIXME("Skipping %u unknown DWORDs:\n", count);
519     for (i = 0; i < count; ++i)
520     {
521         read_dword(ptr, &d);
522         FIXME("\t0x%08x\n", d);
523     }
524 }
525 
526 static void write_dword_unknown(char **ptr, DWORD d)
527 {
528     FIXME("Writing unknown DWORD 0x%08x\n", d);
529     write_dword(ptr, d);
530 }
531 
532 HRESULT dxbc_add_section(struct dxbc *dxbc, DWORD tag, const char *data, DWORD data_size)
533 {
534     TRACE("dxbc %p, tag %s, size %#x.\n", dxbc, debugstr_an((const char *)&tag, 4), data_size);
535 
536     if (dxbc->count >= dxbc->size)
537     {
538         struct dxbc_section *new_sections;
539         DWORD new_size = dxbc->size << 1;
540 
541         new_sections = HeapReAlloc(GetProcessHeap(), 0, dxbc->sections, new_size * sizeof(*dxbc->sections));
542         if (!new_sections)
543         {
544             ERR("Failed to allocate dxbc section memory\n");
545             return E_OUTOFMEMORY;
546         }
547 
548         dxbc->sections = new_sections;
549         dxbc->size = new_size;
550     }
551 
552     dxbc->sections[dxbc->count].tag = tag;
553     dxbc->sections[dxbc->count].data_size = data_size;
554     dxbc->sections[dxbc->count].data = data;
555     ++dxbc->count;
556 
557     return S_OK;
558 }
559 
560 HRESULT dxbc_init(struct dxbc *dxbc, UINT size)
561 {
562     TRACE("dxbc %p, size %u.\n", dxbc, size);
563 
564     /* use a good starting value for the size if none specified */
565     if (!size) size = 2;
566 
567     dxbc->sections = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*dxbc->sections));
568     if (!dxbc->sections)
569     {
570         ERR("Failed to allocate dxbc section memory\n");
571         return E_OUTOFMEMORY;
572     }
573 
574     dxbc->size = size;
575     dxbc->count = 0;
576 
577     return S_OK;
578 }
579 
580 HRESULT dxbc_parse(const char *data, SIZE_T data_size, struct dxbc *dxbc)
581 {
582     const char *ptr = data;
583     HRESULT hr;
584     unsigned int i;
585     DWORD tag, total_size, chunk_count;
586 
587     if (!data)
588     {
589         WARN("No data supplied.\n");
590         return E_FAIL;
591     }
592 
593     read_dword(&ptr, &tag);
594     TRACE("tag: %s.\n", debugstr_an((const char *)&tag, 4));
595 
596     if (tag != TAG_DXBC)
597     {
598         WARN("Wrong tag.\n");
599         return E_FAIL;
600     }
601 
602     /* checksum? */
603     skip_dword_unknown(&ptr, 4);
604 
605     skip_dword_unknown(&ptr, 1);
606 
607     read_dword(&ptr, &total_size);
608     TRACE("total size: %#x\n", total_size);
609 
610     if (data_size != total_size)
611     {
612         WARN("Wrong size supplied.\n");
613         return D3DERR_INVALIDCALL;
614     }
615 
616     read_dword(&ptr, &chunk_count);
617     TRACE("chunk count: %#x\n", chunk_count);
618 
619     hr = dxbc_init(dxbc, chunk_count);
620     if (FAILED(hr))
621     {
622         WARN("Failed to init dxbc\n");
623         return hr;
624     }
625 
626     for (i = 0; i < chunk_count; ++i)
627     {
628         DWORD chunk_tag, chunk_size;
629         const char *chunk_ptr;
630         DWORD chunk_offset;
631 
632         read_dword(&ptr, &chunk_offset);
633         TRACE("chunk %u at offset %#x\n", i, chunk_offset);
634 
635         chunk_ptr = data + chunk_offset;
636 
637         read_dword(&chunk_ptr, &chunk_tag);
638         read_dword(&chunk_ptr, &chunk_size);
639 
640         hr = dxbc_add_section(dxbc, chunk_tag, chunk_ptr, chunk_size);
641         if (FAILED(hr))
642         {
643             WARN("Failed to add section to dxbc\n");
644             return hr;
645         }
646     }
647 
648     return hr;
649 }
650 
651 void dxbc_destroy(struct dxbc *dxbc)
652 {
653     TRACE("dxbc %p.\n", dxbc);
654 
655     HeapFree(GetProcessHeap(), 0, dxbc->sections);
656 }
657 
658 HRESULT dxbc_write_blob(struct dxbc *dxbc, ID3DBlob **blob)
659 {
660     DWORD size = 32, offset = size + 4 * dxbc->count;
661     ID3DBlob *object;
662     HRESULT hr;
663     char *ptr;
664     unsigned int i;
665 
666     TRACE("dxbc %p, blob %p.\n", dxbc, blob);
667 
668     for (i = 0; i < dxbc->count; ++i)
669     {
670         size += 12 + dxbc->sections[i].data_size;
671     }
672 
673     hr = D3DCreateBlob(size, &object);
674     if (FAILED(hr))
675     {
676         WARN("Failed to create blob\n");
677         return hr;
678     }
679 
680     ptr = ID3D10Blob_GetBufferPointer(object);
681 
682     write_dword(&ptr, TAG_DXBC);
683 
684     /* signature(?) */
685     write_dword_unknown(&ptr, 0);
686     write_dword_unknown(&ptr, 0);
687     write_dword_unknown(&ptr, 0);
688     write_dword_unknown(&ptr, 0);
689 
690     /* seems to be always 1 */
691     write_dword_unknown(&ptr, 1);
692 
693     /* DXBC size */
694     write_dword(&ptr, size);
695 
696     /* chunk count */
697     write_dword(&ptr, dxbc->count);
698 
699     /* write the chunk offsets */
700     for (i = 0; i < dxbc->count; ++i)
701     {
702         write_dword(&ptr, offset);
703         offset += 8 + dxbc->sections[i].data_size;
704     }
705 
706     /* write the chunks */
707     for (i = 0; i < dxbc->count; ++i)
708     {
709         write_dword(&ptr, dxbc->sections[i].tag);
710         write_dword(&ptr, dxbc->sections[i].data_size);
711         memcpy(ptr, dxbc->sections[i].data, dxbc->sections[i].data_size);
712         ptr += dxbc->sections[i].data_size;
713     }
714 
715     TRACE("Created ID3DBlob %p\n", object);
716 
717     *blob = object;
718 
719     return S_OK;
720 }
721 
722 void compilation_message(struct compilation_messages *msg, const char *fmt, va_list args)
723 {
724     char* buffer;
725     int rc, size;
726 
727     if (msg->capacity == 0)
728     {
729         msg->string = d3dcompiler_alloc(MESSAGEBUFFER_INITIAL_SIZE);
730         if (msg->string == NULL)
731         {
732             ERR("Error allocating memory for parser messages\n");
733             return;
734         }
735         msg->capacity = MESSAGEBUFFER_INITIAL_SIZE;
736     }
737 
738     while (1)
739     {
740         rc = vsnprintf(msg->string + msg->size,
741                 msg->capacity - msg->size, fmt, args);
742 
743         if (rc < 0 || rc >= msg->capacity - msg->size)
744         {
745             size = msg->capacity * 2;
746             buffer = d3dcompiler_realloc(msg->string, size);
747             if (buffer == NULL)
748             {
749                 ERR("Error reallocating memory for parser messages\n");
750                 return;
751             }
752             msg->string = buffer;
753             msg->capacity = size;
754         }
755         else
756         {
757             TRACE("%s", msg->string + msg->size);
758             msg->size += rc;
759             return;
760         }
761     }
762 }
763 
764 BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var)
765 {
766     struct hlsl_ir_var *var;
767 
768     LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
769     {
770         if (!strcmp(decl->name, var->name))
771             return FALSE;
772     }
773     if (local_var && scope->upper->upper == hlsl_ctx.globals)
774     {
775         /* Check whether the variable redefines a function parameter. */
776         LIST_FOR_EACH_ENTRY(var, &scope->upper->vars, struct hlsl_ir_var, scope_entry)
777         {
778             if (!strcmp(decl->name, var->name))
779                 return FALSE;
780         }
781     }
782 
783     list_add_tail(&scope->vars, &decl->scope_entry);
784     return TRUE;
785 }
786 
787 struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name)
788 {
789     struct hlsl_ir_var *var;
790 
791     LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
792     {
793         if (!strcmp(name, var->name))
794             return var;
795     }
796     if (!scope->upper)
797         return NULL;
798     return get_variable(scope->upper, name);
799 }
800 
801 void free_declaration(struct hlsl_ir_var *decl)
802 {
803     d3dcompiler_free((void *)decl->name);
804     d3dcompiler_free((void *)decl->semantic);
805     d3dcompiler_free((void *)decl->reg_reservation);
806     d3dcompiler_free(decl);
807 }
808 
809 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_type_class type_class,
810         enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
811 {
812     struct hlsl_type *type;
813 
814     type = d3dcompiler_alloc(sizeof(*type));
815     if (!type)
816     {
817         ERR("Out of memory\n");
818         return NULL;
819     }
820     type->name = name;
821     type->type = type_class;
822     type->base_type = base_type;
823     type->dimx = dimx;
824     type->dimy = dimy;
825 
826     list_add_tail(&hlsl_ctx.types, &type->entry);
827 
828     return type;
829 }
830 
831 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
832 {
833     struct hlsl_type *type = new_hlsl_type(NULL, HLSL_CLASS_ARRAY, HLSL_TYPE_FLOAT, 1, 1);
834 
835     if (!type)
836         return NULL;
837 
838     type->modifiers = basic_type->modifiers;
839     type->e.array.elements_count = array_size;
840     type->e.array.type = basic_type;
841     return type;
842 }
843 
844 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive)
845 {
846     struct wine_rb_entry *entry = wine_rb_get(&scope->types, name);
847     if (entry)
848         return WINE_RB_ENTRY_VALUE(entry, struct hlsl_type, scope_entry);
849 
850     if (recursive && scope->upper)
851         return get_type(scope->upper, name, recursive);
852     return NULL;
853 }
854 
855 BOOL find_function(const char *name)
856 {
857     return wine_rb_get(&hlsl_ctx.functions, name) != NULL;
858 }
859 
860 unsigned int components_count_type(struct hlsl_type *type)
861 {
862     unsigned int count = 0;
863     struct hlsl_struct_field *field;
864 
865     if (type->type <= HLSL_CLASS_LAST_NUMERIC)
866     {
867         return type->dimx * type->dimy;
868     }
869     if (type->type == HLSL_CLASS_ARRAY)
870     {
871         return components_count_type(type->e.array.type) * type->e.array.elements_count;
872     }
873     if (type->type != HLSL_CLASS_STRUCT)
874     {
875         ERR("Unexpected data type %s.\n", debug_hlsl_type(type));
876         return 0;
877     }
878 
879     LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
880     {
881         count += components_count_type(field->type);
882     }
883     return count;
884 }
885 
886 BOOL compare_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
887 {
888     if (t1 == t2)
889         return TRUE;
890 
891     if (t1->type != t2->type)
892         return FALSE;
893     if (t1->base_type != t2->base_type)
894         return FALSE;
895     if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
896         return FALSE;
897     if ((t1->modifiers & HLSL_MODIFIERS_COMPARISON_MASK)
898             != (t2->modifiers & HLSL_MODIFIERS_COMPARISON_MASK))
899         return FALSE;
900     if (t1->dimx != t2->dimx)
901         return FALSE;
902     if (t1->dimy != t2->dimy)
903         return FALSE;
904     if (t1->type == HLSL_CLASS_STRUCT)
905     {
906         struct list *t1cur, *t2cur;
907         struct hlsl_struct_field *t1field, *t2field;
908 
909         t1cur = list_head(t1->e.elements);
910         t2cur = list_head(t2->e.elements);
911         while (t1cur && t2cur)
912         {
913             t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
914             t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
915             if (!compare_hlsl_types(t1field->type, t2field->type))
916                 return FALSE;
917             if (strcmp(t1field->name, t2field->name))
918                 return FALSE;
919             t1cur = list_next(t1->e.elements, t1cur);
920             t2cur = list_next(t2->e.elements, t2cur);
921         }
922         if (t1cur != t2cur)
923             return FALSE;
924     }
925     if (t1->type == HLSL_CLASS_ARRAY)
926         return t1->e.array.elements_count == t2->e.array.elements_count
927                 && compare_hlsl_types(t1->e.array.type, t2->e.array.type);
928 
929     return TRUE;
930 }
931 
932 struct hlsl_type *clone_hlsl_type(struct hlsl_type *old)
933 {
934     struct hlsl_type *type;
935     struct hlsl_struct_field *old_field, *field;
936 
937     type = d3dcompiler_alloc(sizeof(*type));
938     if (!type)
939     {
940         ERR("Out of memory\n");
941         return NULL;
942     }
943     if (old->name)
944     {
945         type->name = d3dcompiler_strdup(old->name);
946         if (!type->name)
947         {
948             d3dcompiler_free(type);
949             return NULL;
950         }
951     }
952     type->type = old->type;
953     type->base_type = old->base_type;
954     type->dimx = old->dimx;
955     type->dimy = old->dimy;
956     type->modifiers = old->modifiers;
957     type->sampler_dim = old->sampler_dim;
958     switch (old->type)
959     {
960         case HLSL_CLASS_ARRAY:
961             type->e.array.type = old->e.array.type;
962             type->e.array.elements_count = old->e.array.elements_count;
963             break;
964         case HLSL_CLASS_STRUCT:
965             type->e.elements = d3dcompiler_alloc(sizeof(*type->e.elements));
966             if (!type->e.elements)
967             {
968                 d3dcompiler_free((void *)type->name);
969                 d3dcompiler_free(type);
970                 return NULL;
971             }
972             list_init(type->e.elements);
973             LIST_FOR_EACH_ENTRY(old_field, old->e.elements, struct hlsl_struct_field, entry)
974             {
975                 field = d3dcompiler_alloc(sizeof(*field));
976                 if (!field)
977                 {
978                     LIST_FOR_EACH_ENTRY_SAFE(field, old_field, type->e.elements, struct hlsl_struct_field, entry)
979                     {
980                         d3dcompiler_free((void *)field->semantic);
981                         d3dcompiler_free((void *)field->name);
982                         d3dcompiler_free(field);
983                     }
984                     d3dcompiler_free(type->e.elements);
985                     d3dcompiler_free((void *)type->name);
986                     d3dcompiler_free(type);
987                     return NULL;
988                 }
989                 field->type = clone_hlsl_type(old_field->type);
990                 field->name = d3dcompiler_strdup(old_field->name);
991                 if (old_field->semantic)
992                     field->semantic = d3dcompiler_strdup(old_field->semantic);
993                 field->modifiers = old_field->modifiers;
994                 list_add_tail(type->e.elements, &field->entry);
995             }
996             break;
997         default:
998             break;
999     }
1000 
1001     list_add_tail(&hlsl_ctx.types, &type->entry);
1002     return type;
1003 }
1004 
1005 static BOOL convertible_data_type(struct hlsl_type *type)
1006 {
1007     return type->type != HLSL_CLASS_OBJECT;
1008 }
1009 
1010 BOOL compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1011 {
1012    if (!convertible_data_type(t1) || !convertible_data_type(t2))
1013         return FALSE;
1014 
1015     if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1016     {
1017         /* Scalar vars can be cast to pretty much everything */
1018         if (t1->dimx == 1 && t1->dimy == 1)
1019             return TRUE;
1020 
1021         if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1022             return t1->dimx >= t2->dimx;
1023     }
1024 
1025     /* The other way around is true too i.e. whatever to scalar */
1026     if (t2->type <= HLSL_CLASS_LAST_NUMERIC && t2->dimx == 1 && t2->dimy == 1)
1027         return TRUE;
1028 
1029     if (t1->type == HLSL_CLASS_ARRAY)
1030     {
1031         if (compare_hlsl_types(t1->e.array.type, t2))
1032             /* e.g. float4[3] to float4 is allowed */
1033             return TRUE;
1034 
1035         if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1036             return components_count_type(t1) >= components_count_type(t2);
1037         else
1038             return components_count_type(t1) == components_count_type(t2);
1039     }
1040 
1041     if (t1->type == HLSL_CLASS_STRUCT)
1042         return components_count_type(t1) >= components_count_type(t2);
1043 
1044     if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1045         return components_count_type(t1) == components_count_type(t2);
1046 
1047     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1048     {
1049         if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1050             return TRUE;
1051 
1052         /* Matrix-vector conversion is apparently allowed if they have the same components count */
1053         if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1054                 && components_count_type(t1) == components_count_type(t2))
1055             return TRUE;
1056         return FALSE;
1057     }
1058 
1059     if (components_count_type(t1) >= components_count_type(t2))
1060         return TRUE;
1061     return FALSE;
1062 }
1063 
1064 static BOOL implicit_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1065 {
1066     if (!convertible_data_type(t1) || !convertible_data_type(t2))
1067         return FALSE;
1068 
1069     if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1070     {
1071         /* Scalar vars can be converted to any other numeric data type */
1072         if (t1->dimx == 1 && t1->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1073             return TRUE;
1074         /* The other way around is true too */
1075         if (t2->dimx == 1 && t2->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1076             return TRUE;
1077     }
1078 
1079     if (t1->type == HLSL_CLASS_ARRAY && t2->type == HLSL_CLASS_ARRAY)
1080     {
1081         return components_count_type(t1) == components_count_type(t2);
1082     }
1083 
1084     if ((t1->type == HLSL_CLASS_ARRAY && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1085             || (t1->type <= HLSL_CLASS_LAST_NUMERIC && t2->type == HLSL_CLASS_ARRAY))
1086     {
1087         /* e.g. float4[3] to float4 is allowed */
1088         if (t1->type == HLSL_CLASS_ARRAY && compare_hlsl_types(t1->e.array.type, t2))
1089             return TRUE;
1090         if (components_count_type(t1) == components_count_type(t2))
1091             return TRUE;
1092         return FALSE;
1093     }
1094 
1095     if (t1->type <= HLSL_CLASS_VECTOR && t2->type <= HLSL_CLASS_VECTOR)
1096     {
1097         if (t1->dimx >= t2->dimx)
1098             return TRUE;
1099         return FALSE;
1100     }
1101 
1102     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1103     {
1104         if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX
1105                 && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1106             return TRUE;
1107 
1108         /* Matrix-vector conversion is apparently allowed if they have the same components count */
1109         if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1110                 && components_count_type(t1) == components_count_type(t2))
1111             return TRUE;
1112         return FALSE;
1113     }
1114 
1115     if (t1->type == HLSL_CLASS_STRUCT && t2->type == HLSL_CLASS_STRUCT)
1116         return compare_hlsl_types(t1, t2);
1117 
1118     return FALSE;
1119 }
1120 
1121 static BOOL expr_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1122 {
1123     if (t1->base_type > HLSL_TYPE_LAST_SCALAR || t2->base_type > HLSL_TYPE_LAST_SCALAR)
1124         return FALSE;
1125 
1126     /* Scalar vars can be converted to pretty much everything */
1127     if ((t1->dimx == 1 && t1->dimy == 1) || (t2->dimx == 1 && t2->dimy == 1))
1128         return TRUE;
1129 
1130     if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1131         return TRUE;
1132 
1133     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1134     {
1135         /* Matrix-vector conversion is apparently allowed if either they have the same components
1136            count or the matrix is nx1 or 1xn */
1137         if (t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1138         {
1139             if (components_count_type(t1) == components_count_type(t2))
1140                 return TRUE;
1141 
1142             return (t1->type == HLSL_CLASS_MATRIX && (t1->dimx == 1 || t1->dimy == 1))
1143                     || (t2->type == HLSL_CLASS_MATRIX && (t2->dimx == 1 || t2->dimy == 1));
1144         }
1145 
1146         /* Both matrices */
1147         if ((t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1148                 || (t1->dimx <= t2->dimx && t1->dimy <= t2->dimy))
1149             return TRUE;
1150     }
1151 
1152     return FALSE;
1153 }
1154 
1155 static enum hlsl_base_type expr_common_base_type(enum hlsl_base_type t1, enum hlsl_base_type t2)
1156 {
1157     static const enum hlsl_base_type types[] =
1158     {
1159         HLSL_TYPE_BOOL,
1160         HLSL_TYPE_INT,
1161         HLSL_TYPE_UINT,
1162         HLSL_TYPE_HALF,
1163         HLSL_TYPE_FLOAT,
1164         HLSL_TYPE_DOUBLE,
1165     };
1166     int t1_idx = -1, t2_idx = -1, i;
1167 
1168     for (i = 0; i < ARRAY_SIZE(types); ++i)
1169     {
1170         /* Always convert away from HLSL_TYPE_HALF */
1171         if (t1 == types[i])
1172             t1_idx = t1 == HLSL_TYPE_HALF ? i + 1 : i;
1173         if (t2 == types[i])
1174             t2_idx = t2 == HLSL_TYPE_HALF ? i + 1 : i;
1175 
1176         if (t1_idx != -1 && t2_idx != -1)
1177             break;
1178     }
1179     if (t1_idx == -1 || t2_idx == -1)
1180     {
1181         FIXME("Unexpected base type.\n");
1182         return HLSL_TYPE_FLOAT;
1183     }
1184     return t1_idx >= t2_idx ? t1 : t2;
1185 }
1186 
1187 static struct hlsl_type *expr_common_type(struct hlsl_type *t1, struct hlsl_type *t2,
1188         struct source_location *loc)
1189 {
1190     enum hlsl_type_class type;
1191     enum hlsl_base_type base;
1192     unsigned int dimx, dimy;
1193 
1194     if (t1->type > HLSL_CLASS_LAST_NUMERIC || t2->type > HLSL_CLASS_LAST_NUMERIC)
1195     {
1196         hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1197                 "non scalar/vector/matrix data type in expression");
1198         return NULL;
1199     }
1200 
1201     if (compare_hlsl_types(t1, t2))
1202         return t1;
1203 
1204     if (!expr_compatible_data_types(t1, t2))
1205     {
1206         hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1207                 "expression data types are incompatible");
1208         return NULL;
1209     }
1210 
1211     if (t1->base_type == t2->base_type)
1212         base = t1->base_type;
1213     else
1214         base = expr_common_base_type(t1->base_type, t2->base_type);
1215 
1216     if (t1->dimx == 1 && t1->dimy == 1)
1217     {
1218         type = t2->type;
1219         dimx = t2->dimx;
1220         dimy = t2->dimy;
1221     }
1222     else if (t2->dimx == 1 && t2->dimy == 1)
1223     {
1224         type = t1->type;
1225         dimx = t1->dimx;
1226         dimy = t1->dimy;
1227     }
1228     else if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX)
1229     {
1230         type = HLSL_CLASS_MATRIX;
1231         dimx = min(t1->dimx, t2->dimx);
1232         dimy = min(t1->dimy, t2->dimy);
1233     }
1234     else
1235     {
1236         /* Two vectors or a vector and a matrix (matrix must be 1xn or nx1) */
1237         unsigned int max_dim_1, max_dim_2;
1238 
1239         max_dim_1 = max(t1->dimx, t1->dimy);
1240         max_dim_2 = max(t2->dimx, t2->dimy);
1241         if (t1->dimx * t1->dimy == t2->dimx * t2->dimy)
1242         {
1243             type = HLSL_CLASS_VECTOR;
1244             dimx = max(t1->dimx, t2->dimx);
1245             dimy = 1;
1246         }
1247         else if (max_dim_1 <= max_dim_2)
1248         {
1249             type = t1->type;
1250             if (type == HLSL_CLASS_VECTOR)
1251             {
1252                 dimx = max_dim_1;
1253                 dimy = 1;
1254             }
1255             else
1256             {
1257                 dimx = t1->dimx;
1258                 dimy = t1->dimy;
1259             }
1260         }
1261         else
1262         {
1263             type = t2->type;
1264             if (type == HLSL_CLASS_VECTOR)
1265             {
1266                 dimx = max_dim_2;
1267                 dimy = 1;
1268             }
1269             else
1270             {
1271                 dimx = t2->dimx;
1272                 dimy = t2->dimy;
1273             }
1274         }
1275     }
1276 
1277     return new_hlsl_type(NULL, type, base, dimx, dimy);
1278 }
1279 
1280 static struct hlsl_ir_node *implicit_conversion(struct hlsl_ir_node *node, struct hlsl_type *type,
1281         struct source_location *loc)
1282 {
1283     struct hlsl_ir_expr *cast;
1284     struct hlsl_ir_node *operands[3];
1285 
1286     if (compare_hlsl_types(node->data_type, type))
1287         return node;
1288     TRACE("Implicit conversion of expression to %s\n", debug_hlsl_type(type));
1289     operands[0] = node;
1290     operands[1] = operands[2] = NULL;
1291     cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1292     if (!cast)
1293         return NULL;
1294     cast->node.data_type = type;
1295     return &cast->node;
1296 }
1297 
1298 struct hlsl_ir_expr *new_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node **operands,
1299         struct source_location *loc)
1300 {
1301     struct hlsl_ir_expr *expr = d3dcompiler_alloc(sizeof(*expr));
1302     struct hlsl_type *type;
1303     unsigned int i;
1304 
1305     if (!expr)
1306     {
1307         ERR("Out of memory\n");
1308         return NULL;
1309     }
1310     expr->node.type = HLSL_IR_EXPR;
1311     expr->node.loc = *loc;
1312     type = operands[0]->data_type;
1313     for (i = 1; i <= 2; ++i)
1314     {
1315         if (!operands[i])
1316             break;
1317         type = expr_common_type(type, operands[i]->data_type, loc);
1318         if (!type)
1319         {
1320             d3dcompiler_free(expr);
1321             return NULL;
1322         }
1323     }
1324     for (i = 0; i <= 2; ++i)
1325     {
1326         if (!operands[i])
1327             break;
1328         if (compare_hlsl_types(operands[i]->data_type, type))
1329             continue;
1330         TRACE("Implicitly converting %s into %s in an expression\n", debug_hlsl_type(operands[i]->data_type), debug_hlsl_type(type));
1331         if (operands[i]->data_type->dimx * operands[i]->data_type->dimy != 1
1332                 && operands[i]->data_type->dimx * operands[i]->data_type->dimy != type->dimx * type->dimy)
1333         {
1334             hlsl_report_message(operands[i]->loc.file,
1335                     operands[i]->loc.line, operands[i]->loc.col, HLSL_LEVEL_WARNING,
1336                     "implicit truncation of vector/matrix type");
1337         }
1338         operands[i] = implicit_conversion(operands[i], type, &operands[i]->loc);
1339         if (!operands[i])
1340         {
1341             ERR("Impossible to convert expression operand %u to %s\n", i + 1, debug_hlsl_type(type));
1342             d3dcompiler_free(expr);
1343             return NULL;
1344         }
1345     }
1346     expr->node.data_type = type;
1347     expr->op = op;
1348     expr->operands[0] = operands[0];
1349     expr->operands[1] = operands[1];
1350     expr->operands[2] = operands[2];
1351 
1352     return expr;
1353 }
1354 
1355 struct hlsl_ir_expr *new_cast(struct hlsl_ir_node *node, struct hlsl_type *type,
1356         struct source_location *loc)
1357 {
1358     struct hlsl_ir_expr *cast;
1359     struct hlsl_ir_node *operands[3];
1360 
1361     operands[0] = node;
1362     operands[1] = operands[2] = NULL;
1363     cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1364     if (cast)
1365         cast->node.data_type = type;
1366     return cast;
1367 }
1368 
1369 struct hlsl_ir_expr *hlsl_mul(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1370         struct source_location *loc)
1371 {
1372     struct hlsl_ir_expr *expr;
1373     struct hlsl_ir_node *ops[3];
1374 
1375     ops[0] = op1;
1376     ops[1] = op2;
1377     ops[2] = NULL;
1378     expr = new_expr(HLSL_IR_BINOP_MUL, ops, loc);
1379     return expr;
1380 }
1381 
1382 struct hlsl_ir_expr *hlsl_div(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1383         struct source_location *loc)
1384 {
1385     struct hlsl_ir_expr *expr;
1386     struct hlsl_ir_node *ops[3];
1387 
1388     ops[0] = op1;
1389     ops[1] = op2;
1390     ops[2] = NULL;
1391     expr = new_expr(HLSL_IR_BINOP_DIV, ops, loc);
1392     return expr;
1393 }
1394 
1395 struct hlsl_ir_expr *hlsl_mod(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1396         struct source_location *loc)
1397 {
1398     struct hlsl_ir_expr *expr;
1399     struct hlsl_ir_node *ops[3];
1400 
1401     ops[0] = op1;
1402     ops[1] = op2;
1403     ops[2] = NULL;
1404     expr = new_expr(HLSL_IR_BINOP_MOD, ops, loc);
1405     return expr;
1406 }
1407 
1408 struct hlsl_ir_expr *hlsl_add(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1409         struct source_location *loc)
1410 {
1411     struct hlsl_ir_expr *expr;
1412     struct hlsl_ir_node *ops[3];
1413 
1414     ops[0] = op1;
1415     ops[1] = op2;
1416     ops[2] = NULL;
1417     expr = new_expr(HLSL_IR_BINOP_ADD, ops, loc);
1418     return expr;
1419 }
1420 
1421 struct hlsl_ir_expr *hlsl_sub(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1422         struct source_location *loc)
1423 {
1424     struct hlsl_ir_expr *expr;
1425     struct hlsl_ir_node *ops[3];
1426 
1427     ops[0] = op1;
1428     ops[1] = op2;
1429     ops[2] = NULL;
1430     expr = new_expr(HLSL_IR_BINOP_SUB, ops, loc);
1431     return expr;
1432 }
1433 
1434 struct hlsl_ir_expr *hlsl_lt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1435         struct source_location *loc)
1436 {
1437     struct hlsl_ir_expr *expr;
1438     struct hlsl_ir_node *ops[3];
1439 
1440     ops[0] = op1;
1441     ops[1] = op2;
1442     ops[2] = NULL;
1443     expr = new_expr(HLSL_IR_BINOP_LESS, ops, loc);
1444     return expr;
1445 }
1446 
1447 struct hlsl_ir_expr *hlsl_gt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1448         struct source_location *loc)
1449 {
1450     struct hlsl_ir_expr *expr;
1451     struct hlsl_ir_node *ops[3];
1452 
1453     ops[0] = op1;
1454     ops[1] = op2;
1455     ops[2] = NULL;
1456     expr = new_expr(HLSL_IR_BINOP_GREATER, ops, loc);
1457     return expr;
1458 }
1459 
1460 struct hlsl_ir_expr *hlsl_le(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1461         struct source_location *loc)
1462 {
1463     struct hlsl_ir_expr *expr;
1464     struct hlsl_ir_node *ops[3];
1465 
1466     ops[0] = op1;
1467     ops[1] = op2;
1468     ops[2] = NULL;
1469     expr = new_expr(HLSL_IR_BINOP_LEQUAL, ops, loc);
1470     return expr;
1471 }
1472 
1473 struct hlsl_ir_expr *hlsl_ge(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1474         struct source_location *loc)
1475 {
1476     struct hlsl_ir_expr *expr;
1477     struct hlsl_ir_node *ops[3];
1478 
1479     ops[0] = op1;
1480     ops[1] = op2;
1481     ops[2] = NULL;
1482     expr = new_expr(HLSL_IR_BINOP_GEQUAL, ops, loc);
1483     return expr;
1484 }
1485 
1486 struct hlsl_ir_expr *hlsl_eq(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1487         struct source_location *loc)
1488 {
1489     struct hlsl_ir_expr *expr;
1490     struct hlsl_ir_node *ops[3];
1491 
1492     ops[0] = op1;
1493     ops[1] = op2;
1494     ops[2] = NULL;
1495     expr = new_expr(HLSL_IR_BINOP_EQUAL, ops, loc);
1496     return expr;
1497 }
1498 
1499 struct hlsl_ir_expr *hlsl_ne(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1500         struct source_location *loc)
1501 {
1502     struct hlsl_ir_expr *expr;
1503     struct hlsl_ir_node *ops[3];
1504 
1505     ops[0] = op1;
1506     ops[1] = op2;
1507     ops[2] = NULL;
1508     expr = new_expr(HLSL_IR_BINOP_NEQUAL, ops, loc);
1509     return expr;
1510 }
1511 
1512 struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var)
1513 {
1514     struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1515 
1516     if (!deref)
1517     {
1518         ERR("Out of memory.\n");
1519         return NULL;
1520     }
1521     deref->node.type = HLSL_IR_DEREF;
1522     deref->node.data_type = var->node.data_type;
1523     deref->type = HLSL_IR_DEREF_VAR;
1524     deref->v.var = var;
1525     return deref;
1526 }
1527 
1528 struct hlsl_ir_deref *new_record_deref(struct hlsl_ir_node *record, struct hlsl_struct_field *field)
1529 {
1530     struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1531 
1532     if (!deref)
1533     {
1534         ERR("Out of memory.\n");
1535         return NULL;
1536     }
1537     deref->node.type = HLSL_IR_DEREF;
1538     deref->node.data_type = field->type;
1539     deref->type = HLSL_IR_DEREF_RECORD;
1540     if (record->type == HLSL_IR_VAR)
1541         deref->v.record.record = &new_var_deref(var_from_node(record))->node;
1542     else
1543         deref->v.record.record = record;
1544     deref->v.record.field = field;
1545     return deref;
1546 }
1547 
1548 static enum hlsl_ir_expr_op op_from_assignment(enum parse_assign_op op)
1549 {
1550     static const enum hlsl_ir_expr_op ops[] =
1551     {
1552         0,
1553         HLSL_IR_BINOP_ADD,
1554         HLSL_IR_BINOP_SUB,
1555         HLSL_IR_BINOP_MUL,
1556         HLSL_IR_BINOP_DIV,
1557         HLSL_IR_BINOP_MOD,
1558         HLSL_IR_BINOP_LSHIFT,
1559         HLSL_IR_BINOP_RSHIFT,
1560         HLSL_IR_BINOP_BIT_AND,
1561         HLSL_IR_BINOP_BIT_OR,
1562         HLSL_IR_BINOP_BIT_XOR,
1563     };
1564 
1565     return ops[op];
1566 }
1567 
1568 struct hlsl_ir_node *make_assignment(struct hlsl_ir_node *left, enum parse_assign_op assign_op,
1569         DWORD writemask, struct hlsl_ir_node *right)
1570 {
1571     struct hlsl_ir_expr *expr;
1572     struct hlsl_ir_assignment *assign = d3dcompiler_alloc(sizeof(*assign));
1573     struct hlsl_type *type;
1574     struct hlsl_ir_node *lhs, *rhs;
1575 
1576     if (!assign)
1577     {
1578         ERR("Out of memory\n");
1579         return NULL;
1580     }
1581 
1582     TRACE("Creating proper assignment expression.\n");
1583     rhs = right;
1584     if (writemask == BWRITERSP_WRITEMASK_ALL)
1585         type = left->data_type;
1586     else
1587     {
1588         unsigned int dimx = 0;
1589         DWORD bitmask;
1590         enum hlsl_type_class type_class;
1591 
1592         if (left->data_type->type > HLSL_CLASS_LAST_NUMERIC)
1593         {
1594             hlsl_report_message(left->loc.file, left->loc.line, left->loc.col, HLSL_LEVEL_ERROR,
1595                     "writemask on a non scalar/vector/matrix type");
1596             d3dcompiler_free(assign);
1597             return NULL;
1598         }
1599         bitmask = writemask & ((1 << left->data_type->dimx) - 1);
1600         while (bitmask)
1601         {
1602             if (bitmask & 1)
1603                 dimx++;
1604             bitmask >>= 1;
1605         }
1606         if (left->data_type->type == HLSL_CLASS_MATRIX)
1607             FIXME("Assignments with writemasks and matrices on lhs are not supported yet.\n");
1608         if (dimx == 1)
1609             type_class = HLSL_CLASS_SCALAR;
1610         else
1611             type_class = left->data_type->type;
1612         type = new_hlsl_type(NULL, type_class, left->data_type->base_type, dimx, 1);
1613     }
1614     assign->node.type = HLSL_IR_ASSIGNMENT;
1615     assign->node.loc = left->loc;
1616     assign->node.data_type = type;
1617     assign->writemask = writemask;
1618     FIXME("Check for casts in the lhs.\n");
1619 
1620     lhs = left;
1621     if (lhs->type == HLSL_IR_VAR)
1622     {
1623         struct hlsl_ir_deref *lhs_deref = new_var_deref(var_from_node(lhs));
1624         lhs = &lhs_deref->node;
1625     }
1626     /* FIXME: check for invalid writemasks on the lhs. */
1627 
1628     if (!compare_hlsl_types(type, rhs->data_type))
1629     {
1630         struct hlsl_ir_node *converted_rhs;
1631 
1632         if (!implicit_compatible_data_types(rhs->data_type, type))
1633         {
1634             hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_ERROR,
1635                     "can't implicitly convert %s to %s",
1636                     debug_hlsl_type(rhs->data_type), debug_hlsl_type(type));
1637             free_instr(lhs);
1638             free_instr(rhs);
1639             d3dcompiler_free(assign);
1640             return NULL;
1641         }
1642         if (lhs->data_type->dimx * lhs->data_type->dimy < rhs->data_type->dimx * rhs->data_type->dimy)
1643             hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_WARNING,
1644                     "implicit truncation of vector type");
1645 
1646         converted_rhs = implicit_conversion(rhs, type, &rhs->loc);
1647         if (!converted_rhs)
1648         {
1649             ERR("Couldn't implicitly convert expression to %s.\n", debug_hlsl_type(type));
1650             free_instr(lhs);
1651             free_instr(rhs);
1652             d3dcompiler_free(assign);
1653             return NULL;
1654         }
1655         rhs = converted_rhs;
1656     }
1657 
1658     assign->lhs = lhs;
1659     if (assign_op != ASSIGN_OP_ASSIGN)
1660     {
1661         struct hlsl_ir_node *operands[3];
1662         enum hlsl_ir_expr_op op = op_from_assignment(assign_op);
1663 
1664         if (lhs->type != HLSL_IR_DEREF || deref_from_node(lhs)->type != HLSL_IR_DEREF_VAR)
1665         {
1666             FIXME("LHS expression not supported in compound assignments yet.\n");
1667             assign->rhs = rhs;
1668         }
1669         else
1670         {
1671             struct hlsl_ir_deref *lhs_deref = deref_from_node(lhs), *new_deref;
1672 
1673             TRACE("Adding an expression for the compound assignment.\n");
1674             new_deref = new_var_deref(lhs_deref->v.var);
1675             operands[0] = &new_deref->node;
1676             operands[1] = rhs;
1677             operands[2] = NULL;
1678             expr = new_expr(op, operands, &left->loc);
1679             assign->rhs = &expr->node;
1680         }
1681     }
1682     else
1683         assign->rhs = rhs;
1684 
1685     return &assign->node;
1686 }
1687 
1688 static int compare_hlsl_types_rb(const void *key, const struct wine_rb_entry *entry)
1689 {
1690     const char *name = key;
1691     const struct hlsl_type *type = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_type, scope_entry);
1692 
1693     if (name == type->name)
1694         return 0;
1695 
1696     if (!name || !type->name)
1697     {
1698         ERR("hlsl_type without a name in a scope?\n");
1699         return -1;
1700     }
1701     return strcmp(name, type->name);
1702 }
1703 
1704 void push_scope(struct hlsl_parse_ctx *ctx)
1705 {
1706     struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
1707 
1708     if (!new_scope)
1709     {
1710         ERR("Out of memory!\n");
1711         return;
1712     }
1713     TRACE("Pushing a new scope\n");
1714     list_init(&new_scope->vars);
1715     wine_rb_init(&new_scope->types, compare_hlsl_types_rb);
1716     new_scope->upper = ctx->cur_scope;
1717     ctx->cur_scope = new_scope;
1718     list_add_tail(&ctx->scopes, &new_scope->entry);
1719 }
1720 
1721 BOOL pop_scope(struct hlsl_parse_ctx *ctx)
1722 {
1723     struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
1724     if (!prev_scope)
1725         return FALSE;
1726 
1727     TRACE("Popping current scope\n");
1728     ctx->cur_scope = prev_scope;
1729     return TRUE;
1730 }
1731 
1732 struct hlsl_ir_function_decl *new_func_decl(struct hlsl_type *return_type, struct list *parameters)
1733 {
1734     struct hlsl_ir_function_decl *decl;
1735 
1736     decl = d3dcompiler_alloc(sizeof(*decl));
1737     if (!decl)
1738     {
1739         ERR("Out of memory.\n");
1740         return NULL;
1741     }
1742     decl->node.type = HLSL_IR_FUNCTION_DECL;
1743     decl->node.data_type = return_type;
1744     decl->parameters = parameters;
1745 
1746     return decl;
1747 }
1748 
1749 static int compare_param_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
1750 {
1751     if (t1->type != t2->type)
1752     {
1753         if (!((t1->type == HLSL_CLASS_SCALAR && t2->type == HLSL_CLASS_VECTOR)
1754                 || (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_SCALAR)))
1755             return t1->type - t2->type;
1756     }
1757     if (t1->base_type != t2->base_type)
1758         return t1->base_type - t2->base_type;
1759     if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
1760         return t1->sampler_dim - t2->sampler_dim;
1761     if (t1->dimx != t2->dimx)
1762         return t1->dimx - t2->dimx;
1763     if (t1->dimy != t2->dimy)
1764         return t1->dimx - t2->dimx;
1765     if (t1->type == HLSL_CLASS_STRUCT)
1766     {
1767         struct list *t1cur, *t2cur;
1768         struct hlsl_struct_field *t1field, *t2field;
1769         int r;
1770 
1771         t1cur = list_head(t1->e.elements);
1772         t2cur = list_head(t2->e.elements);
1773         while (t1cur && t2cur)
1774         {
1775             t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
1776             t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
1777             if ((r = compare_param_hlsl_types(t1field->type, t2field->type)))
1778                 return r;
1779             if ((r = strcmp(t1field->name, t2field->name)))
1780                 return r;
1781             t1cur = list_next(t1->e.elements, t1cur);
1782             t2cur = list_next(t2->e.elements, t2cur);
1783         }
1784         if (t1cur != t2cur)
1785             return t1cur ? 1 : -1;
1786         return 0;
1787     }
1788     if (t1->type == HLSL_CLASS_ARRAY)
1789     {
1790         if (t1->e.array.elements_count != t2->e.array.elements_count)
1791             return t1->e.array.elements_count - t2->e.array.elements_count;
1792         return compare_param_hlsl_types(t1->e.array.type, t2->e.array.type);
1793     }
1794 
1795     return 0;
1796 }
1797 
1798 static int compare_function_decl_rb(const void *key, const struct wine_rb_entry *entry)
1799 {
1800     const struct list *params = key;
1801     const struct hlsl_ir_function_decl *decl = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function_decl, entry);
1802     int params_count = params ? list_count(params) : 0;
1803     int decl_params_count = decl->parameters ? list_count(decl->parameters) : 0;
1804     int r;
1805     struct list *p1cur, *p2cur;
1806 
1807     if (params_count != decl_params_count)
1808         return params_count - decl_params_count;
1809 
1810     p1cur = params ? list_head(params) : NULL;
1811     p2cur = decl->parameters ? list_head(decl->parameters) : NULL;
1812     while (p1cur && p2cur)
1813     {
1814         struct hlsl_ir_var *p1, *p2;
1815         p1 = LIST_ENTRY(p1cur, struct hlsl_ir_var, node.entry);
1816         p2 = LIST_ENTRY(p2cur, struct hlsl_ir_var, node.entry);
1817         if ((r = compare_param_hlsl_types(p1->node.data_type, p2->node.data_type)))
1818             return r;
1819         p1cur = list_next(params, p1cur);
1820         p2cur = list_next(decl->parameters, p2cur);
1821     }
1822     return 0;
1823 }
1824 
1825 static int compare_function_rb(const void *key, const struct wine_rb_entry *entry)
1826 {
1827     const char *name = key;
1828     const struct hlsl_ir_function *func = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function,entry);
1829 
1830     return strcmp(name, func->name);
1831 }
1832 
1833 void init_functions_tree(struct wine_rb_tree *funcs)
1834 {
1835     wine_rb_init(&hlsl_ctx.functions, compare_function_rb);
1836 }
1837 
1838 static const char *debug_base_type(const struct hlsl_type *type)
1839 {
1840     const char *name = "(unknown)";
1841 
1842     switch (type->base_type)
1843     {
1844         case HLSL_TYPE_FLOAT:        name = "float";         break;
1845         case HLSL_TYPE_HALF:         name = "half";          break;
1846         case HLSL_TYPE_DOUBLE:       name = "double";        break;
1847         case HLSL_TYPE_INT:          name = "int";           break;
1848         case HLSL_TYPE_UINT:         name = "uint";          break;
1849         case HLSL_TYPE_BOOL:         name = "bool";          break;
1850         case HLSL_TYPE_SAMPLER:
1851             switch (type->sampler_dim)
1852             {
1853                 case HLSL_SAMPLER_DIM_GENERIC: name = "sampler";       break;
1854                 case HLSL_SAMPLER_DIM_1D:      name = "sampler1D";     break;
1855                 case HLSL_SAMPLER_DIM_2D:      name = "sampler2D";     break;
1856                 case HLSL_SAMPLER_DIM_3D:      name = "sampler3D";     break;
1857                 case HLSL_SAMPLER_DIM_CUBE:    name = "samplerCUBE";   break;
1858             }
1859             break;
1860         default:
1861             FIXME("Unhandled case %u\n", type->base_type);
1862     }
1863     return name;
1864 }
1865 
1866 const char *debug_hlsl_type(const struct hlsl_type *type)
1867 {
1868     const char *name;
1869 
1870     if (type->name)
1871         return debugstr_a(type->name);
1872 
1873     if (type->type == HLSL_CLASS_STRUCT)
1874         return "<anonymous struct>";
1875 
1876     if (type->type == HLSL_CLASS_ARRAY)
1877     {
1878         name = debug_base_type(type->e.array.type);
1879         return wine_dbg_sprintf("%s[%u]", name, type->e.array.elements_count);
1880     }
1881 
1882     name = debug_base_type(type);
1883 
1884     if (type->type == HLSL_CLASS_SCALAR)
1885         return wine_dbg_sprintf("%s", name);
1886     if (type->type == HLSL_CLASS_VECTOR)
1887         return wine_dbg_sprintf("%s%u", name, type->dimx);
1888     if (type->type == HLSL_CLASS_MATRIX)
1889         return wine_dbg_sprintf("%s%ux%u", name, type->dimx, type->dimy);
1890     return "unexpected_type";
1891 }
1892 
1893 const char *debug_modifiers(DWORD modifiers)
1894 {
1895     char string[110];
1896 
1897     string[0] = 0;
1898     if (modifiers & HLSL_STORAGE_EXTERN)
1899         strcat(string, " extern");                       /* 7 */
1900     if (modifiers & HLSL_STORAGE_NOINTERPOLATION)
1901         strcat(string, " nointerpolation");              /* 16 */
1902     if (modifiers & HLSL_MODIFIER_PRECISE)
1903         strcat(string, " precise");                      /* 8 */
1904     if (modifiers & HLSL_STORAGE_SHARED)
1905         strcat(string, " shared");                       /* 7 */
1906     if (modifiers & HLSL_STORAGE_GROUPSHARED)
1907         strcat(string, " groupshared");                  /* 12 */
1908     if (modifiers & HLSL_STORAGE_STATIC)
1909         strcat(string, " static");                       /* 7 */
1910     if (modifiers & HLSL_STORAGE_UNIFORM)
1911         strcat(string, " uniform");                      /* 8 */
1912     if (modifiers & HLSL_STORAGE_VOLATILE)
1913         strcat(string, " volatile");                     /* 9 */
1914     if (modifiers & HLSL_MODIFIER_CONST)
1915         strcat(string, " const");                        /* 6 */
1916     if (modifiers & HLSL_MODIFIER_ROW_MAJOR)
1917         strcat(string, " row_major");                    /* 10 */
1918     if (modifiers & HLSL_MODIFIER_COLUMN_MAJOR)
1919         strcat(string, " column_major");                 /* 13 */
1920     if ((modifiers & (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT)) == (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT))
1921         strcat(string, " inout");                        /* 6 */
1922     else if (modifiers & HLSL_MODIFIER_IN)
1923         strcat(string, " in");                           /* 3 */
1924     else if (modifiers & HLSL_MODIFIER_OUT)
1925         strcat(string, " out");                          /* 4 */
1926 
1927     return wine_dbg_sprintf("%s", string[0] ? string + 1 : "");
1928 }
1929 
1930 static const char *debug_node_type(enum hlsl_ir_node_type type)
1931 {
1932     static const char * const names[] =
1933     {
1934         "HLSL_IR_VAR",
1935         "HLSL_IR_ASSIGNMENT",
1936         "HLSL_IR_CONSTANT",
1937         "HLSL_IR_CONSTRUCTOR",
1938         "HLSL_IR_DEREF",
1939         "HLSL_IR_EXPR",
1940         "HLSL_IR_FUNCTION_DECL",
1941         "HLSL_IR_IF",
1942         "HLSL_IR_JUMP",
1943         "HLSL_IR_SWIZZLE",
1944     };
1945 
1946     if (type >= ARRAY_SIZE(names))
1947         return "Unexpected node type";
1948     return names[type];
1949 }
1950 
1951 static void debug_dump_instr(const struct hlsl_ir_node *instr);
1952 
1953 static void debug_dump_instr_list(const struct list *list)
1954 {
1955     struct hlsl_ir_node *instr;
1956 
1957     LIST_FOR_EACH_ENTRY(instr, list, struct hlsl_ir_node, entry)
1958     {
1959         debug_dump_instr(instr);
1960         TRACE("\n");
1961     }
1962 }
1963 
1964 static void debug_dump_ir_var(const struct hlsl_ir_var *var)
1965 {
1966     if (var->modifiers)
1967         TRACE("%s ", debug_modifiers(var->modifiers));
1968     TRACE("%s %s", debug_hlsl_type(var->node.data_type), var->name);
1969     if (var->semantic)
1970         TRACE(" : %s", debugstr_a(var->semantic));
1971 }
1972 
1973 static void debug_dump_ir_deref(const struct hlsl_ir_deref *deref)
1974 {
1975     switch (deref->type)
1976     {
1977         case HLSL_IR_DEREF_VAR:
1978             TRACE("deref(");
1979             debug_dump_ir_var(deref->v.var);
1980             TRACE(")");
1981             break;
1982         case HLSL_IR_DEREF_ARRAY:
1983             debug_dump_instr(deref->v.array.array);
1984             TRACE("[");
1985             debug_dump_instr(deref->v.array.index);
1986             TRACE("]");
1987             break;
1988         case HLSL_IR_DEREF_RECORD:
1989             debug_dump_instr(deref->v.record.record);
1990             TRACE(".%s", debugstr_a(deref->v.record.field->name));
1991             break;
1992     }
1993 }
1994 
1995 static void debug_dump_ir_constant(const struct hlsl_ir_constant *constant)
1996 {
1997     struct hlsl_type *type = constant->node.data_type;
1998     unsigned int x, y;
1999 
2000     if (type->dimy != 1)
2001         TRACE("{");
2002     for (y = 0; y < type->dimy; ++y)
2003     {
2004         if (type->dimx != 1)
2005             TRACE("{");
2006         for (x = 0; x < type->dimx; ++x)
2007         {
2008             switch (type->base_type)
2009             {
2010                 case HLSL_TYPE_FLOAT:
2011                     TRACE("%g ", (double)constant->v.value.f[y * type->dimx + x]);
2012                     break;
2013                 case HLSL_TYPE_DOUBLE:
2014                     TRACE("%g ", constant->v.value.d[y * type->dimx + x]);
2015                     break;
2016                 case HLSL_TYPE_INT:
2017                     TRACE("%d ", constant->v.value.i[y * type->dimx + x]);
2018                     break;
2019                 case HLSL_TYPE_UINT:
2020                     TRACE("%u ", constant->v.value.u[y * type->dimx + x]);
2021                     break;
2022                 case HLSL_TYPE_BOOL:
2023                     TRACE("%s ", constant->v.value.b[y * type->dimx + x] == FALSE ? "false" : "true");
2024                     break;
2025                 default:
2026                     TRACE("Constants of type %s not supported\n", debug_base_type(type));
2027             }
2028         }
2029         if (type->dimx != 1)
2030             TRACE("}");
2031     }
2032     if (type->dimy != 1)
2033         TRACE("}");
2034 }
2035 
2036 static const char *debug_expr_op(const struct hlsl_ir_expr *expr)
2037 {
2038     static const char * const op_names[] =
2039     {
2040         "~",
2041         "!",
2042         "-",
2043         "abs",
2044         "sign",
2045         "rcp",
2046         "rsq",
2047         "sqrt",
2048         "nrm",
2049         "exp2",
2050         "log2",
2051 
2052         "cast",
2053 
2054         "fract",
2055 
2056         "sin",
2057         "cos",
2058         "sin_reduced",
2059         "cos_reduced",
2060 
2061         "dsx",
2062         "dsy",
2063 
2064         "sat",
2065 
2066         "pre++",
2067         "pre--",
2068         "post++",
2069         "post--",
2070 
2071         "+",
2072         "-",
2073         "*",
2074         "/",
2075 
2076         "%",
2077 
2078         "<",
2079         ">",
2080         "<=",
2081         ">=",
2082         "==",
2083         "!=",
2084 
2085         "&&",
2086         "||",
2087 
2088         "<<",
2089         ">>",
2090         "&",
2091         "|",
2092         "^",
2093 
2094         "dot",
2095         "crs",
2096         "min",
2097         "max",
2098 
2099         "pow",
2100 
2101         "lerp",
2102 
2103         ",",
2104     };
2105 
2106     if (expr->op == HLSL_IR_UNOP_CAST)
2107         return debug_hlsl_type(expr->node.data_type);
2108 
2109     return op_names[expr->op];
2110 }
2111 
2112 /* Dumps the expression in a prefix "operator (operands)" form */
2113 static void debug_dump_ir_expr(const struct hlsl_ir_expr *expr)
2114 {
2115     unsigned int i;
2116 
2117     TRACE("%s (", debug_expr_op(expr));
2118     for (i = 0; i < 3 && expr->operands[i]; ++i)
2119     {
2120         debug_dump_instr(expr->operands[i]);
2121         TRACE(" ");
2122     }
2123     TRACE(")");
2124 }
2125 
2126 static void debug_dump_ir_constructor(const struct hlsl_ir_constructor *constructor)
2127 {
2128     struct hlsl_ir_node *arg;
2129 
2130     TRACE("%s (", debug_hlsl_type(constructor->node.data_type));
2131     LIST_FOR_EACH_ENTRY(arg, constructor->arguments, struct hlsl_ir_node, entry)
2132     {
2133         debug_dump_instr(arg);
2134         TRACE(" ");
2135     }
2136     TRACE(")");
2137 }
2138 
2139 static const char *debug_writemask(DWORD writemask)
2140 {
2141     char string[5], components[] = {'x', 'y', 'z', 'w'};
2142     unsigned int i = 0, pos = 0;
2143 
2144     while (writemask)
2145     {
2146         if (writemask & 1)
2147             string[pos++] = components[i];
2148         writemask >>= 1;
2149         i++;
2150     }
2151     string[pos] = '\0';
2152     return wine_dbg_sprintf(".%s", string);
2153 }
2154 
2155 static void debug_dump_ir_assignment(const struct hlsl_ir_assignment *assign)
2156 {
2157     TRACE("= (");
2158     debug_dump_instr(assign->lhs);
2159     if (assign->writemask != BWRITERSP_WRITEMASK_ALL)
2160         TRACE("%s", debug_writemask(assign->writemask));
2161     TRACE(" ");
2162     debug_dump_instr(assign->rhs);
2163     TRACE(")");
2164 }
2165 
2166 static void debug_dump_ir_swizzle(const struct hlsl_ir_swizzle *swizzle)
2167 {
2168     unsigned int i;
2169 
2170     debug_dump_instr(swizzle->val);
2171     TRACE(".");
2172     if (swizzle->val->data_type->dimy > 1)
2173     {
2174         for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2175             TRACE("_m%u%u", (swizzle->swizzle >> i * 8) & 0xf, (swizzle->swizzle >> (i * 8 + 4)) & 0xf);
2176     }
2177     else
2178     {
2179         char c[] = {'x', 'y', 'z', 'w'};
2180 
2181         for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2182             TRACE("%c", c[(swizzle->swizzle >> i * 2) & 0x3]);
2183     }
2184 }
2185 
2186 static void debug_dump_ir_jump(const struct hlsl_ir_jump *jump)
2187 {
2188     switch (jump->type)
2189     {
2190         case HLSL_IR_JUMP_BREAK:
2191             TRACE("break");
2192             break;
2193         case HLSL_IR_JUMP_CONTINUE:
2194             TRACE("continue");
2195             break;
2196         case HLSL_IR_JUMP_DISCARD:
2197             TRACE("discard");
2198             break;
2199         case HLSL_IR_JUMP_RETURN:
2200             TRACE("return ");
2201             if (jump->return_value)
2202                 debug_dump_instr(jump->return_value);
2203             TRACE(";");
2204             break;
2205     }
2206 }
2207 
2208 static void debug_dump_ir_if(const struct hlsl_ir_if *if_node)
2209 {
2210     TRACE("if (");
2211     debug_dump_instr(if_node->condition);
2212     TRACE(")\n{\n");
2213     debug_dump_instr_list(if_node->then_instrs);
2214     TRACE("}\n");
2215     if (if_node->else_instrs)
2216     {
2217         TRACE("else\n{\n");
2218         debug_dump_instr_list(if_node->else_instrs);
2219         TRACE("}\n");
2220     }
2221 }
2222 
2223 static void debug_dump_instr(const struct hlsl_ir_node *instr)
2224 {
2225     switch (instr->type)
2226     {
2227         case HLSL_IR_EXPR:
2228             debug_dump_ir_expr(expr_from_node(instr));
2229             break;
2230         case HLSL_IR_DEREF:
2231             debug_dump_ir_deref(deref_from_node(instr));
2232             break;
2233         case HLSL_IR_CONSTANT:
2234             debug_dump_ir_constant(constant_from_node(instr));
2235             break;
2236         case HLSL_IR_ASSIGNMENT:
2237             debug_dump_ir_assignment(assignment_from_node(instr));
2238             break;
2239         case HLSL_IR_SWIZZLE:
2240             debug_dump_ir_swizzle(swizzle_from_node(instr));
2241             break;
2242         case HLSL_IR_CONSTRUCTOR:
2243             debug_dump_ir_constructor(constructor_from_node(instr));
2244             break;
2245         case HLSL_IR_JUMP:
2246             debug_dump_ir_jump(jump_from_node(instr));
2247             break;
2248         case HLSL_IR_IF:
2249             debug_dump_ir_if(if_from_node(instr));
2250             break;
2251         default:
2252             TRACE("<No dump function for %s>", debug_node_type(instr->type));
2253     }
2254 }
2255 
2256 void debug_dump_ir_function_decl(const struct hlsl_ir_function_decl *func)
2257 {
2258     struct hlsl_ir_var *param;
2259 
2260     TRACE("Dumping function %s.\n", debugstr_a(func->func->name));
2261     TRACE("Function parameters:\n");
2262     LIST_FOR_EACH_ENTRY(param, func->parameters, struct hlsl_ir_var, node.entry)
2263     {
2264         debug_dump_ir_var(param);
2265         TRACE("\n");
2266     }
2267     if (func->semantic)
2268         TRACE("Function semantic: %s\n", debugstr_a(func->semantic));
2269     if (func->body)
2270     {
2271         debug_dump_instr_list(func->body);
2272     }
2273 }
2274 
2275 void free_hlsl_type(struct hlsl_type *type)
2276 {
2277     struct hlsl_struct_field *field, *next_field;
2278 
2279     d3dcompiler_free((void *)type->name);
2280     if (type->type == HLSL_CLASS_STRUCT)
2281     {
2282         LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
2283         {
2284             d3dcompiler_free((void *)field->name);
2285             d3dcompiler_free((void *)field->semantic);
2286             d3dcompiler_free(field);
2287         }
2288     }
2289     d3dcompiler_free(type);
2290 }
2291 
2292 void free_instr_list(struct list *list)
2293 {
2294     struct hlsl_ir_node *node, *next_node;
2295 
2296     if (!list)
2297         return;
2298     LIST_FOR_EACH_ENTRY_SAFE(node, next_node, list, struct hlsl_ir_node, entry)
2299         free_instr(node);
2300     d3dcompiler_free(list);
2301 }
2302 
2303 static void free_ir_constant(struct hlsl_ir_constant *constant)
2304 {
2305     struct hlsl_type *type = constant->node.data_type;
2306     unsigned int i;
2307     struct hlsl_ir_constant *field, *next_field;
2308 
2309     switch (type->type)
2310     {
2311         case HLSL_CLASS_ARRAY:
2312             for (i = 0; i < type->e.array.elements_count; ++i)
2313                 free_ir_constant(&constant->v.array_elements[i]);
2314             d3dcompiler_free(constant->v.array_elements);
2315             break;
2316         case HLSL_CLASS_STRUCT:
2317             LIST_FOR_EACH_ENTRY_SAFE(field, next_field, constant->v.struct_elements, struct hlsl_ir_constant, node.entry)
2318                 free_ir_constant(field);
2319             break;
2320         default:
2321             break;
2322     }
2323     d3dcompiler_free(constant);
2324 }
2325 
2326 static void free_ir_deref(struct hlsl_ir_deref *deref)
2327 {
2328     switch (deref->type)
2329     {
2330         case HLSL_IR_DEREF_VAR:
2331             /* Variables are shared among nodes in the tree. */
2332             break;
2333         case HLSL_IR_DEREF_ARRAY:
2334             free_instr(deref->v.array.array);
2335             free_instr(deref->v.array.index);
2336             break;
2337         case HLSL_IR_DEREF_RECORD:
2338             free_instr(deref->v.record.record);
2339             break;
2340     }
2341     d3dcompiler_free(deref);
2342 }
2343 
2344 static void free_ir_swizzle(struct hlsl_ir_swizzle *swizzle)
2345 {
2346     free_instr(swizzle->val);
2347     d3dcompiler_free(swizzle);
2348 }
2349 
2350 static void free_ir_constructor(struct hlsl_ir_constructor *constructor)
2351 {
2352     free_instr_list(constructor->arguments);
2353     d3dcompiler_free(constructor);
2354 }
2355 
2356 static void free_ir_expr(struct hlsl_ir_expr *expr)
2357 {
2358     unsigned int i;
2359 
2360     for (i = 0; i < 3; ++i)
2361     {
2362         if (!expr->operands[i])
2363             break;
2364         free_instr(expr->operands[i]);
2365     }
2366     free_instr_list(expr->subexpressions);
2367     d3dcompiler_free(expr);
2368 }
2369 
2370 static void free_ir_assignment(struct hlsl_ir_assignment *assignment)
2371 {
2372     free_instr(assignment->lhs);
2373     free_instr(assignment->rhs);
2374     d3dcompiler_free(assignment);
2375 }
2376 
2377 static void free_ir_if(struct hlsl_ir_if *if_node)
2378 {
2379     free_instr(if_node->condition);
2380     free_instr_list(if_node->then_instrs);
2381     free_instr_list(if_node->else_instrs);
2382     d3dcompiler_free(if_node);
2383 }
2384 
2385 static void free_ir_jump(struct hlsl_ir_jump *jump)
2386 {
2387     if (jump->type == HLSL_IR_JUMP_RETURN)
2388         free_instr(jump->return_value);
2389     d3dcompiler_free(jump);
2390 }
2391 
2392 void free_instr(struct hlsl_ir_node *node)
2393 {
2394     switch (node->type)
2395     {
2396         case HLSL_IR_VAR:
2397             /* These are freed later on from the scopes. */
2398             break;
2399         case HLSL_IR_CONSTANT:
2400             free_ir_constant(constant_from_node(node));
2401             break;
2402         case HLSL_IR_DEREF:
2403             free_ir_deref(deref_from_node(node));
2404             break;
2405         case HLSL_IR_SWIZZLE:
2406             free_ir_swizzle(swizzle_from_node(node));
2407             break;
2408         case HLSL_IR_CONSTRUCTOR:
2409             free_ir_constructor(constructor_from_node(node));
2410             break;
2411         case HLSL_IR_EXPR:
2412             free_ir_expr(expr_from_node(node));
2413             break;
2414         case HLSL_IR_ASSIGNMENT:
2415             free_ir_assignment(assignment_from_node(node));
2416             break;
2417         case HLSL_IR_IF:
2418             free_ir_if(if_from_node(node));
2419             break;
2420         case HLSL_IR_JUMP:
2421             free_ir_jump(jump_from_node(node));
2422             break;
2423         default:
2424             FIXME("Unsupported node type %s\n", debug_node_type(node->type));
2425     }
2426 }
2427 
2428 static void free_function_decl(struct hlsl_ir_function_decl *decl)
2429 {
2430     d3dcompiler_free((void *)decl->semantic);
2431     d3dcompiler_free(decl->parameters);
2432     free_instr_list(decl->body);
2433     d3dcompiler_free(decl);
2434 }
2435 
2436 static void free_function_decl_rb(struct wine_rb_entry *entry, void *context)
2437 {
2438     free_function_decl(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry));
2439 }
2440 
2441 static void free_function(struct hlsl_ir_function *func)
2442 {
2443     wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2444     d3dcompiler_free((void *)func->name);
2445     d3dcompiler_free(func);
2446 }
2447 
2448 void free_function_rb(struct wine_rb_entry *entry, void *context)
2449 {
2450     free_function(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry));
2451 }
2452 
2453 void add_function_decl(struct wine_rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, BOOL intrinsic)
2454 {
2455     struct hlsl_ir_function *func;
2456     struct wine_rb_entry *func_entry, *old_entry;
2457 
2458     func_entry = wine_rb_get(funcs, name);
2459     if (func_entry)
2460     {
2461         func = WINE_RB_ENTRY_VALUE(func_entry, struct hlsl_ir_function, entry);
2462         if (intrinsic != func->intrinsic)
2463         {
2464             if (intrinsic)
2465             {
2466                 ERR("Redeclaring a user defined function as an intrinsic.\n");
2467                 return;
2468             }
2469             TRACE("Function %s redeclared as a user defined function.\n", debugstr_a(name));
2470             func->intrinsic = intrinsic;
2471             wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2472             wine_rb_init(&func->overloads, compare_function_decl_rb);
2473         }
2474         decl->func = func;
2475         if ((old_entry = wine_rb_get(&func->overloads, decl->parameters)))
2476         {
2477             struct hlsl_ir_function_decl *old_decl =
2478                     WINE_RB_ENTRY_VALUE(old_entry, struct hlsl_ir_function_decl, entry);
2479 
2480             if (!decl->body)
2481             {
2482                 free_function_decl(decl);
2483                 d3dcompiler_free(name);
2484                 return;
2485             }
2486             wine_rb_remove(&func->overloads, old_entry);
2487             free_function_decl(old_decl);
2488         }
2489         wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2490         d3dcompiler_free(name);
2491         return;
2492     }
2493     func = d3dcompiler_alloc(sizeof(*func));
2494     func->name = name;
2495     wine_rb_init(&func->overloads, compare_function_decl_rb);
2496     decl->func = func;
2497     wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2498     func->intrinsic = intrinsic;
2499     wine_rb_put(funcs, func->name, &func->entry);
2500 }
2501