d3d9/tests: Add r500 fp_special results.
[wine] / dlls / d3dcompiler_43 / utils.c
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(decl);
806 }
807
808 BOOL add_func_parameter(struct list *list, struct parse_parameter *param, const struct source_location *loc)
809 {
810     struct hlsl_ir_var *decl = d3dcompiler_alloc(sizeof(*decl));
811
812     if (!decl)
813     {
814         ERR("Out of memory.\n");
815         return FALSE;
816     }
817     decl->node.type = HLSL_IR_VAR;
818     decl->node.data_type = param->type;
819     decl->node.loc = *loc;
820     decl->name = param->name;
821     decl->semantic = param->semantic;
822     decl->modifiers = param->modifiers;
823
824     if (!add_declaration(hlsl_ctx.cur_scope, decl, FALSE))
825     {
826         free_declaration(decl);
827         return FALSE;
828     }
829     list_add_tail(list, &decl->node.entry);
830     return TRUE;
831 }
832
833 struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_type_class type_class,
834         enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
835 {
836     struct hlsl_type *type;
837
838     type = d3dcompiler_alloc(sizeof(*type));
839     if (!type)
840     {
841         ERR("Out of memory\n");
842         return NULL;
843     }
844     type->name = name;
845     type->type = type_class;
846     type->base_type = base_type;
847     type->dimx = dimx;
848     type->dimy = dimy;
849
850     list_add_tail(&hlsl_ctx.types, &type->entry);
851
852     return type;
853 }
854
855 struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
856 {
857     struct hlsl_type *type = new_hlsl_type(NULL, HLSL_CLASS_ARRAY, HLSL_TYPE_FLOAT, 1, 1);
858
859     if (!type)
860         return NULL;
861
862     type->modifiers = basic_type->modifiers;
863     type->e.array.elements_count = array_size;
864     type->e.array.type = basic_type;
865     return type;
866 }
867
868 struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive)
869 {
870     struct wine_rb_entry *entry = wine_rb_get(&scope->types, name);
871     if (entry)
872         return WINE_RB_ENTRY_VALUE(entry, struct hlsl_type, scope_entry);
873
874     if (recursive && scope->upper)
875         return get_type(scope->upper, name, recursive);
876     return NULL;
877 }
878
879 BOOL find_function(const char *name)
880 {
881     return wine_rb_get(&hlsl_ctx.functions, name) != NULL;
882 }
883
884 unsigned int components_count_type(struct hlsl_type *type)
885 {
886     unsigned int count = 0;
887     struct hlsl_struct_field *field;
888
889     if (type->type <= HLSL_CLASS_LAST_NUMERIC)
890     {
891         return type->dimx * type->dimy;
892     }
893     if (type->type == HLSL_CLASS_ARRAY)
894     {
895         return components_count_type(type->e.array.type) * type->e.array.elements_count;
896     }
897     if (type->type != HLSL_CLASS_STRUCT)
898     {
899         ERR("Unexpected data type %s.\n", debug_hlsl_type(type));
900         return 0;
901     }
902
903     LIST_FOR_EACH_ENTRY(field, type->e.elements, struct hlsl_struct_field, entry)
904     {
905         count += components_count_type(field->type);
906     }
907     return count;
908 }
909
910 BOOL compare_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
911 {
912     if (t1 == t2)
913         return TRUE;
914
915     if (t1->type != t2->type)
916         return FALSE;
917     if (t1->base_type != t2->base_type)
918         return FALSE;
919     if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
920         return FALSE;
921     if ((t1->modifiers & HLSL_MODIFIERS_COMPARISON_MASK)
922             != (t2->modifiers & HLSL_MODIFIERS_COMPARISON_MASK))
923         return FALSE;
924     if (t1->dimx != t2->dimx)
925         return FALSE;
926     if (t1->dimy != t2->dimy)
927         return FALSE;
928     if (t1->type == HLSL_CLASS_STRUCT)
929     {
930         struct list *t1cur, *t2cur;
931         struct hlsl_struct_field *t1field, *t2field;
932
933         t1cur = list_head(t1->e.elements);
934         t2cur = list_head(t2->e.elements);
935         while (t1cur && t2cur)
936         {
937             t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
938             t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
939             if (!compare_hlsl_types(t1field->type, t2field->type))
940                 return FALSE;
941             if (strcmp(t1field->name, t2field->name))
942                 return FALSE;
943             t1cur = list_next(t1->e.elements, t1cur);
944             t2cur = list_next(t2->e.elements, t2cur);
945         }
946         if (t1cur != t2cur)
947             return FALSE;
948     }
949     if (t1->type == HLSL_CLASS_ARRAY)
950         return t1->e.array.elements_count == t2->e.array.elements_count
951                 && compare_hlsl_types(t1->e.array.type, t2->e.array.type);
952
953     return TRUE;
954 }
955
956 struct hlsl_type *clone_hlsl_type(struct hlsl_type *old)
957 {
958     struct hlsl_type *type;
959     struct hlsl_struct_field *old_field, *field;
960
961     type = d3dcompiler_alloc(sizeof(*type));
962     if (!type)
963     {
964         ERR("Out of memory\n");
965         return NULL;
966     }
967     if (old->name)
968     {
969         type->name = d3dcompiler_strdup(old->name);
970         if (!type->name)
971         {
972             d3dcompiler_free(type);
973             return NULL;
974         }
975     }
976     type->type = old->type;
977     type->base_type = old->base_type;
978     type->dimx = old->dimx;
979     type->dimy = old->dimy;
980     type->modifiers = old->modifiers;
981     type->sampler_dim = old->sampler_dim;
982     switch (old->type)
983     {
984         case HLSL_CLASS_ARRAY:
985             type->e.array.type = old->e.array.type;
986             type->e.array.elements_count = old->e.array.elements_count;
987             break;
988         case HLSL_CLASS_STRUCT:
989             type->e.elements = d3dcompiler_alloc(sizeof(*type->e.elements));
990             if (!type->e.elements)
991             {
992                 d3dcompiler_free((void *)type->name);
993                 d3dcompiler_free(type);
994                 return NULL;
995             }
996             list_init(type->e.elements);
997             LIST_FOR_EACH_ENTRY(old_field, old->e.elements, struct hlsl_struct_field, entry)
998             {
999                 field = d3dcompiler_alloc(sizeof(*field));
1000                 if (!field)
1001                 {
1002                     LIST_FOR_EACH_ENTRY_SAFE(field, old_field, type->e.elements, struct hlsl_struct_field, entry)
1003                     {
1004                         d3dcompiler_free((void *)field->semantic);
1005                         d3dcompiler_free((void *)field->name);
1006                         d3dcompiler_free(field);
1007                     }
1008                     d3dcompiler_free(type->e.elements);
1009                     d3dcompiler_free((void *)type->name);
1010                     d3dcompiler_free(type);
1011                     return NULL;
1012                 }
1013                 field->type = clone_hlsl_type(old_field->type);
1014                 field->name = d3dcompiler_strdup(old_field->name);
1015                 if (old_field->semantic)
1016                     field->semantic = d3dcompiler_strdup(old_field->semantic);
1017                 field->modifiers = old_field->modifiers;
1018                 list_add_tail(type->e.elements, &field->entry);
1019             }
1020             break;
1021         default:
1022             break;
1023     }
1024
1025     list_add_tail(&hlsl_ctx.types, &type->entry);
1026     return type;
1027 }
1028
1029 static BOOL convertible_data_type(struct hlsl_type *type)
1030 {
1031     return type->type != HLSL_CLASS_OBJECT;
1032 }
1033
1034 BOOL compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1035 {
1036    if (!convertible_data_type(t1) || !convertible_data_type(t2))
1037         return FALSE;
1038
1039     if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1040     {
1041         /* Scalar vars can be cast to pretty much everything */
1042         if (t1->dimx == 1 && t1->dimy == 1)
1043             return TRUE;
1044
1045         if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1046             return t1->dimx >= t2->dimx;
1047     }
1048
1049     /* The other way around is true too i.e. whatever to scalar */
1050     if (t2->type <= HLSL_CLASS_LAST_NUMERIC && t2->dimx == 1 && t2->dimy == 1)
1051         return TRUE;
1052
1053     if (t1->type == HLSL_CLASS_ARRAY)
1054     {
1055         if (compare_hlsl_types(t1->e.array.type, t2))
1056             /* e.g. float4[3] to float4 is allowed */
1057             return TRUE;
1058
1059         if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1060             return components_count_type(t1) >= components_count_type(t2);
1061         else
1062             return components_count_type(t1) == components_count_type(t2);
1063     }
1064
1065     if (t1->type == HLSL_CLASS_STRUCT)
1066         return components_count_type(t1) >= components_count_type(t2);
1067
1068     if (t2->type == HLSL_CLASS_ARRAY || t2->type == HLSL_CLASS_STRUCT)
1069         return components_count_type(t1) == components_count_type(t2);
1070
1071     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1072     {
1073         if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1074             return TRUE;
1075
1076         /* Matrix-vector conversion is apparently allowed if they have the same components count */
1077         if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1078                 && components_count_type(t1) == components_count_type(t2))
1079             return TRUE;
1080         return FALSE;
1081     }
1082
1083     if (components_count_type(t1) >= components_count_type(t2))
1084         return TRUE;
1085     return FALSE;
1086 }
1087
1088 static BOOL implicit_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1089 {
1090     if (!convertible_data_type(t1) || !convertible_data_type(t2))
1091         return FALSE;
1092
1093     if (t1->type <= HLSL_CLASS_LAST_NUMERIC)
1094     {
1095         /* Scalar vars can be converted to any other numeric data type */
1096         if (t1->dimx == 1 && t1->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1097             return TRUE;
1098         /* The other way around is true too */
1099         if (t2->dimx == 1 && t2->dimy == 1 && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1100             return TRUE;
1101     }
1102
1103     if (t1->type == HLSL_CLASS_ARRAY && t2->type == HLSL_CLASS_ARRAY)
1104     {
1105         return components_count_type(t1) == components_count_type(t2);
1106     }
1107
1108     if ((t1->type == HLSL_CLASS_ARRAY && t2->type <= HLSL_CLASS_LAST_NUMERIC)
1109             || (t1->type <= HLSL_CLASS_LAST_NUMERIC && t2->type == HLSL_CLASS_ARRAY))
1110     {
1111         /* e.g. float4[3] to float4 is allowed */
1112         if (t1->type == HLSL_CLASS_ARRAY && compare_hlsl_types(t1->e.array.type, t2))
1113             return TRUE;
1114         if (components_count_type(t1) == components_count_type(t2))
1115             return TRUE;
1116         return FALSE;
1117     }
1118
1119     if (t1->type <= HLSL_CLASS_VECTOR && t2->type <= HLSL_CLASS_VECTOR)
1120     {
1121         if (t1->dimx >= t2->dimx)
1122             return TRUE;
1123         return FALSE;
1124     }
1125
1126     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1127     {
1128         if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX
1129                 && t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1130             return TRUE;
1131
1132         /* Matrix-vector conversion is apparently allowed if they have the same components count */
1133         if ((t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1134                 && components_count_type(t1) == components_count_type(t2))
1135             return TRUE;
1136         return FALSE;
1137     }
1138
1139     if (t1->type == HLSL_CLASS_STRUCT && t2->type == HLSL_CLASS_STRUCT)
1140         return compare_hlsl_types(t1, t2);
1141
1142     return FALSE;
1143 }
1144
1145 static BOOL expr_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2)
1146 {
1147     if (t1->base_type > HLSL_TYPE_LAST_SCALAR || t2->base_type > HLSL_TYPE_LAST_SCALAR)
1148         return FALSE;
1149
1150     /* Scalar vars can be converted to pretty much everything */
1151     if ((t1->dimx == 1 && t1->dimy == 1) || (t2->dimx == 1 && t2->dimy == 1))
1152         return TRUE;
1153
1154     if (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_VECTOR)
1155         return TRUE;
1156
1157     if (t1->type == HLSL_CLASS_MATRIX || t2->type == HLSL_CLASS_MATRIX)
1158     {
1159         /* Matrix-vector conversion is apparently allowed if either they have the same components
1160            count or the matrix is nx1 or 1xn */
1161         if (t1->type == HLSL_CLASS_VECTOR || t2->type == HLSL_CLASS_VECTOR)
1162         {
1163             if (components_count_type(t1) == components_count_type(t2))
1164                 return TRUE;
1165
1166             return (t1->type == HLSL_CLASS_MATRIX && (t1->dimx == 1 || t1->dimy == 1))
1167                     || (t2->type == HLSL_CLASS_MATRIX && (t2->dimx == 1 || t2->dimy == 1));
1168         }
1169
1170         /* Both matrices */
1171         if ((t1->dimx >= t2->dimx && t1->dimy >= t2->dimy)
1172                 || (t1->dimx <= t2->dimx && t1->dimy <= t2->dimy))
1173             return TRUE;
1174     }
1175
1176     return FALSE;
1177 }
1178
1179 static enum hlsl_base_type expr_common_base_type(enum hlsl_base_type t1, enum hlsl_base_type t2)
1180 {
1181     enum hlsl_base_type types[] =
1182     {
1183         HLSL_TYPE_BOOL,
1184         HLSL_TYPE_INT,
1185         HLSL_TYPE_UINT,
1186         HLSL_TYPE_HALF,
1187         HLSL_TYPE_FLOAT,
1188         HLSL_TYPE_DOUBLE,
1189     };
1190     int t1_idx = -1, t2_idx = -1, i;
1191
1192     for (i = 0; i < sizeof(types) / sizeof(types[0]); ++i)
1193     {
1194         /* Always convert away from HLSL_TYPE_HALF */
1195         if (t1 == types[i])
1196             t1_idx = t1 == HLSL_TYPE_HALF ? i + 1 : i;
1197         if (t2 == types[i])
1198             t2_idx = t2 == HLSL_TYPE_HALF ? i + 1 : i;
1199
1200         if (t1_idx != -1 && t2_idx != -1)
1201             break;
1202     }
1203     if (t1_idx == -1 || t2_idx == -1)
1204     {
1205         FIXME("Unexpected base type.\n");
1206         return HLSL_TYPE_FLOAT;
1207     }
1208     return t1_idx >= t2_idx ? t1 : t2;
1209 }
1210
1211 static struct hlsl_type *expr_common_type(struct hlsl_type *t1, struct hlsl_type *t2,
1212         struct source_location *loc)
1213 {
1214     enum hlsl_type_class type;
1215     enum hlsl_base_type base;
1216     unsigned int dimx, dimy;
1217
1218     if (t1->type > HLSL_CLASS_LAST_NUMERIC || t2->type > HLSL_CLASS_LAST_NUMERIC)
1219     {
1220         hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1221                 "non scalar/vector/matrix data type in expression");
1222         return NULL;
1223     }
1224
1225     if (compare_hlsl_types(t1, t2))
1226         return t1;
1227
1228     if (!expr_compatible_data_types(t1, t2))
1229     {
1230         hlsl_report_message(loc->file, loc->line, loc->col, HLSL_LEVEL_ERROR,
1231                 "expression data types are incompatible");
1232         return NULL;
1233     }
1234
1235     if (t1->base_type == t2->base_type)
1236         base = t1->base_type;
1237     else
1238         base = expr_common_base_type(t1->base_type, t2->base_type);
1239
1240     if (t1->dimx == 1 && t1->dimy == 1)
1241     {
1242         type = t2->type;
1243         dimx = t2->dimx;
1244         dimy = t2->dimy;
1245     }
1246     else if (t2->dimx == 1 && t2->dimy == 1)
1247     {
1248         type = t1->type;
1249         dimx = t1->dimx;
1250         dimy = t1->dimy;
1251     }
1252     else if (t1->type == HLSL_CLASS_MATRIX && t2->type == HLSL_CLASS_MATRIX)
1253     {
1254         type = HLSL_CLASS_MATRIX;
1255         dimx = min(t1->dimx, t2->dimx);
1256         dimy = min(t1->dimy, t2->dimy);
1257     }
1258     else
1259     {
1260         /* Two vectors or a vector and a matrix (matrix must be 1xn or nx1) */
1261         unsigned int max_dim_1, max_dim_2;
1262
1263         max_dim_1 = max(t1->dimx, t1->dimy);
1264         max_dim_2 = max(t2->dimx, t2->dimy);
1265         if (t1->dimx * t1->dimy == t2->dimx * t2->dimy)
1266         {
1267             type = HLSL_CLASS_VECTOR;
1268             dimx = max(t1->dimx, t2->dimx);
1269             dimy = 1;
1270         }
1271         else if (max_dim_1 <= max_dim_2)
1272         {
1273             type = t1->type;
1274             if (type == HLSL_CLASS_VECTOR)
1275             {
1276                 dimx = max_dim_1;
1277                 dimy = 1;
1278             }
1279             else
1280             {
1281                 dimx = t1->dimx;
1282                 dimy = t1->dimy;
1283             }
1284         }
1285         else
1286         {
1287             type = t2->type;
1288             if (type == HLSL_CLASS_VECTOR)
1289             {
1290                 dimx = max_dim_2;
1291                 dimy = 1;
1292             }
1293             else
1294             {
1295                 dimx = t2->dimx;
1296                 dimy = t2->dimy;
1297             }
1298         }
1299     }
1300
1301     return new_hlsl_type(NULL, type, base, dimx, dimy);
1302 }
1303
1304 static struct hlsl_ir_node *implicit_conversion(struct hlsl_ir_node *node, struct hlsl_type *type,
1305         struct source_location *loc)
1306 {
1307     struct hlsl_ir_expr *cast;
1308     struct hlsl_ir_node *operands[3];
1309
1310     if (compare_hlsl_types(node->data_type, type))
1311         return node;
1312     TRACE("Implicit conversion of expression to %s\n", debug_hlsl_type(type));
1313     operands[0] = node;
1314     operands[1] = operands[2] = NULL;
1315     cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1316     if (!cast)
1317         return NULL;
1318     cast->node.data_type = type;
1319     return &cast->node;
1320 }
1321
1322 struct hlsl_ir_expr *new_expr(enum hlsl_ir_expr_op op, struct hlsl_ir_node **operands,
1323         struct source_location *loc)
1324 {
1325     struct hlsl_ir_expr *expr = d3dcompiler_alloc(sizeof(*expr));
1326     struct hlsl_type *type;
1327     unsigned int i;
1328
1329     if (!expr)
1330     {
1331         ERR("Out of memory\n");
1332         return NULL;
1333     }
1334     expr->node.type = HLSL_IR_EXPR;
1335     expr->node.loc = *loc;
1336     type = operands[0]->data_type;
1337     for (i = 1; i <= 2; ++i)
1338     {
1339         if (!operands[i])
1340             break;
1341         type = expr_common_type(type, operands[i]->data_type, loc);
1342         if (!type)
1343         {
1344             d3dcompiler_free(expr);
1345             return NULL;
1346         }
1347     }
1348     for (i = 0; i <= 2; ++i)
1349     {
1350         if (!operands[i])
1351             break;
1352         if (compare_hlsl_types(operands[i]->data_type, type))
1353             continue;
1354         TRACE("Implicitly converting %s into %s in an expression\n", debug_hlsl_type(operands[i]->data_type), debug_hlsl_type(type));
1355         if (operands[i]->data_type->dimx * operands[i]->data_type->dimy != 1
1356                 && operands[i]->data_type->dimx * operands[i]->data_type->dimy != type->dimx * type->dimy)
1357         {
1358             hlsl_report_message(operands[i]->loc.file,
1359                     operands[i]->loc.line, operands[i]->loc.col, HLSL_LEVEL_WARNING,
1360                     "implicit truncation of vector/matrix type");
1361         }
1362         operands[i] = implicit_conversion(operands[i], type, &operands[i]->loc);
1363         if (!operands[i])
1364         {
1365             ERR("Impossible to convert expression operand %u to %s\n", i + 1, debug_hlsl_type(type));
1366             d3dcompiler_free(expr);
1367             return NULL;
1368         }
1369     }
1370     expr->node.data_type = type;
1371     expr->op = op;
1372     expr->operands[0] = operands[0];
1373     expr->operands[1] = operands[1];
1374     expr->operands[2] = operands[2];
1375
1376     return expr;
1377 }
1378
1379 struct hlsl_ir_expr *new_cast(struct hlsl_ir_node *node, struct hlsl_type *type,
1380         struct source_location *loc)
1381 {
1382     struct hlsl_ir_expr *cast;
1383     struct hlsl_ir_node *operands[3];
1384
1385     operands[0] = node;
1386     operands[1] = operands[2] = NULL;
1387     cast = new_expr(HLSL_IR_UNOP_CAST, operands, loc);
1388     if (cast)
1389         cast->node.data_type = type;
1390     return cast;
1391 }
1392
1393 struct hlsl_ir_expr *hlsl_mul(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1394         struct source_location *loc)
1395 {
1396     struct hlsl_ir_expr *expr;
1397     struct hlsl_ir_node *ops[3];
1398
1399     ops[0] = op1;
1400     ops[1] = op2;
1401     ops[2] = NULL;
1402     expr = new_expr(HLSL_IR_BINOP_MUL, ops, loc);
1403     return expr;
1404 }
1405
1406 struct hlsl_ir_expr *hlsl_div(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1407         struct source_location *loc)
1408 {
1409     struct hlsl_ir_expr *expr;
1410     struct hlsl_ir_node *ops[3];
1411
1412     ops[0] = op1;
1413     ops[1] = op2;
1414     ops[2] = NULL;
1415     expr = new_expr(HLSL_IR_BINOP_DIV, ops, loc);
1416     return expr;
1417 }
1418
1419 struct hlsl_ir_expr *hlsl_mod(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1420         struct source_location *loc)
1421 {
1422     struct hlsl_ir_expr *expr;
1423     struct hlsl_ir_node *ops[3];
1424
1425     ops[0] = op1;
1426     ops[1] = op2;
1427     ops[2] = NULL;
1428     expr = new_expr(HLSL_IR_BINOP_MOD, ops, loc);
1429     return expr;
1430 }
1431
1432 struct hlsl_ir_expr *hlsl_add(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1433         struct source_location *loc)
1434 {
1435     struct hlsl_ir_expr *expr;
1436     struct hlsl_ir_node *ops[3];
1437
1438     ops[0] = op1;
1439     ops[1] = op2;
1440     ops[2] = NULL;
1441     expr = new_expr(HLSL_IR_BINOP_ADD, ops, loc);
1442     return expr;
1443 }
1444
1445 struct hlsl_ir_expr *hlsl_sub(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1446         struct source_location *loc)
1447 {
1448     struct hlsl_ir_expr *expr;
1449     struct hlsl_ir_node *ops[3];
1450
1451     ops[0] = op1;
1452     ops[1] = op2;
1453     ops[2] = NULL;
1454     expr = new_expr(HLSL_IR_BINOP_SUB, ops, loc);
1455     return expr;
1456 }
1457
1458 struct hlsl_ir_expr *hlsl_lt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1459         struct source_location *loc)
1460 {
1461     struct hlsl_ir_expr *expr;
1462     struct hlsl_ir_node *ops[3];
1463
1464     ops[0] = op1;
1465     ops[1] = op2;
1466     ops[2] = NULL;
1467     expr = new_expr(HLSL_IR_BINOP_LESS, ops, loc);
1468     return expr;
1469 }
1470
1471 struct hlsl_ir_expr *hlsl_gt(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1472         struct source_location *loc)
1473 {
1474     struct hlsl_ir_expr *expr;
1475     struct hlsl_ir_node *ops[3];
1476
1477     ops[0] = op1;
1478     ops[1] = op2;
1479     ops[2] = NULL;
1480     expr = new_expr(HLSL_IR_BINOP_GREATER, ops, loc);
1481     return expr;
1482 }
1483
1484 struct hlsl_ir_expr *hlsl_le(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1485         struct source_location *loc)
1486 {
1487     struct hlsl_ir_expr *expr;
1488     struct hlsl_ir_node *ops[3];
1489
1490     ops[0] = op1;
1491     ops[1] = op2;
1492     ops[2] = NULL;
1493     expr = new_expr(HLSL_IR_BINOP_LEQUAL, ops, loc);
1494     return expr;
1495 }
1496
1497 struct hlsl_ir_expr *hlsl_ge(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1498         struct source_location *loc)
1499 {
1500     struct hlsl_ir_expr *expr;
1501     struct hlsl_ir_node *ops[3];
1502
1503     ops[0] = op1;
1504     ops[1] = op2;
1505     ops[2] = NULL;
1506     expr = new_expr(HLSL_IR_BINOP_GEQUAL, ops, loc);
1507     return expr;
1508 }
1509
1510 struct hlsl_ir_expr *hlsl_eq(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1511         struct source_location *loc)
1512 {
1513     struct hlsl_ir_expr *expr;
1514     struct hlsl_ir_node *ops[3];
1515
1516     ops[0] = op1;
1517     ops[1] = op2;
1518     ops[2] = NULL;
1519     expr = new_expr(HLSL_IR_BINOP_EQUAL, ops, loc);
1520     return expr;
1521 }
1522
1523 struct hlsl_ir_expr *hlsl_ne(struct hlsl_ir_node *op1, struct hlsl_ir_node *op2,
1524         struct source_location *loc)
1525 {
1526     struct hlsl_ir_expr *expr;
1527     struct hlsl_ir_node *ops[3];
1528
1529     ops[0] = op1;
1530     ops[1] = op2;
1531     ops[2] = NULL;
1532     expr = new_expr(HLSL_IR_BINOP_NEQUAL, ops, loc);
1533     return expr;
1534 }
1535
1536 struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var)
1537 {
1538     struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1539
1540     if (!deref)
1541     {
1542         ERR("Out of memory.\n");
1543         return NULL;
1544     }
1545     deref->node.type = HLSL_IR_DEREF;
1546     deref->node.data_type = var->node.data_type;
1547     deref->type = HLSL_IR_DEREF_VAR;
1548     deref->v.var = var;
1549     return deref;
1550 }
1551
1552 struct hlsl_ir_deref *new_record_deref(struct hlsl_ir_node *record, struct hlsl_struct_field *field)
1553 {
1554     struct hlsl_ir_deref *deref = d3dcompiler_alloc(sizeof(*deref));
1555
1556     if (!deref)
1557     {
1558         ERR("Out of memory.\n");
1559         return NULL;
1560     }
1561     deref->node.type = HLSL_IR_DEREF;
1562     deref->node.data_type = field->type;
1563     deref->type = HLSL_IR_DEREF_RECORD;
1564     if (record->type == HLSL_IR_VAR)
1565         deref->v.record.record = &new_var_deref(var_from_node(record))->node;
1566     else
1567         deref->v.record.record = record;
1568     deref->v.record.field = field;
1569     return deref;
1570 }
1571
1572 static enum hlsl_ir_expr_op op_from_assignment(enum parse_assign_op op)
1573 {
1574     static const enum hlsl_ir_expr_op ops[] =
1575     {
1576         0,
1577         HLSL_IR_BINOP_ADD,
1578         HLSL_IR_BINOP_SUB,
1579         HLSL_IR_BINOP_MUL,
1580         HLSL_IR_BINOP_DIV,
1581         HLSL_IR_BINOP_MOD,
1582         HLSL_IR_BINOP_LSHIFT,
1583         HLSL_IR_BINOP_RSHIFT,
1584         HLSL_IR_BINOP_BIT_AND,
1585         HLSL_IR_BINOP_BIT_OR,
1586         HLSL_IR_BINOP_BIT_XOR,
1587     };
1588
1589     return ops[op];
1590 }
1591
1592 struct hlsl_ir_node *make_assignment(struct hlsl_ir_node *left, enum parse_assign_op assign_op,
1593         DWORD writemask, struct hlsl_ir_node *right)
1594 {
1595     struct hlsl_ir_expr *expr;
1596     struct hlsl_ir_assignment *assign = d3dcompiler_alloc(sizeof(*assign));
1597     struct hlsl_type *type;
1598     struct hlsl_ir_node *lhs, *rhs;
1599
1600     if (!assign)
1601     {
1602         ERR("Out of memory\n");
1603         return NULL;
1604     }
1605
1606     TRACE("Creating proper assignment expression.\n");
1607     rhs = right;
1608     if (writemask == BWRITERSP_WRITEMASK_ALL)
1609         type = left->data_type;
1610     else
1611     {
1612         FIXME("Assignments with writemasks not supported yet.\n");
1613         type = NULL;
1614     }
1615     assign->node.type = HLSL_IR_ASSIGNMENT;
1616     assign->node.loc = left->loc;
1617     assign->node.data_type = type;
1618     assign->writemask = writemask;
1619     FIXME("Check for casts in the lhs.\n");
1620
1621     lhs = left;
1622     if (lhs->type == HLSL_IR_VAR)
1623     {
1624         struct hlsl_ir_deref *lhs_deref = new_var_deref(var_from_node(lhs));
1625         lhs = &lhs_deref->node;
1626     }
1627     /* FIXME: check for invalid writemasks on the lhs. */
1628
1629     if (!compare_hlsl_types(type, rhs->data_type))
1630     {
1631         struct hlsl_ir_node *converted_rhs;
1632
1633         if (!implicit_compatible_data_types(rhs->data_type, type))
1634         {
1635             hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_ERROR,
1636                     "can't implicitly convert %s to %s",
1637                     debug_hlsl_type(rhs->data_type), debug_hlsl_type(type));
1638             free_instr(lhs);
1639             free_instr(rhs);
1640             d3dcompiler_free(assign);
1641             return NULL;
1642         }
1643         if (lhs->data_type->dimx * lhs->data_type->dimy < rhs->data_type->dimx * rhs->data_type->dimy)
1644             hlsl_report_message(rhs->loc.file, rhs->loc.line, rhs->loc.col, HLSL_LEVEL_WARNING,
1645                     "implicit truncation of vector type");
1646
1647         converted_rhs = implicit_conversion(rhs, type, &rhs->loc);
1648         if (!converted_rhs)
1649         {
1650             ERR("Couldn't implicitly convert expression to %s.\n", debug_hlsl_type(type));
1651             free_instr(lhs);
1652             free_instr(rhs);
1653             d3dcompiler_free(assign);
1654             return NULL;
1655         }
1656         rhs = converted_rhs;
1657     }
1658
1659     assign->lhs = lhs;
1660     if (assign_op != ASSIGN_OP_ASSIGN)
1661     {
1662         struct hlsl_ir_node *operands[3];
1663         enum hlsl_ir_expr_op op = op_from_assignment(assign_op);
1664
1665         if (lhs->type != HLSL_IR_DEREF || deref_from_node(lhs)->type != HLSL_IR_DEREF_VAR)
1666         {
1667             FIXME("LHS expression not supported in compound assignments yet.\n");
1668             assign->rhs = rhs;
1669         }
1670         else
1671         {
1672             struct hlsl_ir_deref *lhs_deref = deref_from_node(lhs), *new_deref;
1673
1674             TRACE("Adding an expression for the compound assignment.\n");
1675             new_deref = new_var_deref(lhs_deref->v.var);
1676             operands[0] = &new_deref->node;
1677             operands[1] = rhs;
1678             operands[2] = NULL;
1679             expr = new_expr(op, operands, &left->loc);
1680             assign->rhs = &expr->node;
1681         }
1682     }
1683     else
1684         assign->rhs = rhs;
1685
1686     return &assign->node;
1687 }
1688
1689 static int compare_hlsl_types_rb(const void *key, const struct wine_rb_entry *entry)
1690 {
1691     const char *name = (const char *)key;
1692     const struct hlsl_type *type = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_type, scope_entry);
1693
1694     if (name == type->name)
1695         return 0;
1696
1697     if (!name || !type->name)
1698     {
1699         ERR("hlsl_type without a name in a scope?\n");
1700         return -1;
1701     }
1702     return strcmp(name, type->name);
1703 }
1704
1705 static inline void *d3dcompiler_alloc_rb(size_t size)
1706 {
1707     return d3dcompiler_alloc(size);
1708 }
1709
1710 static inline void *d3dcompiler_realloc_rb(void *ptr, size_t size)
1711 {
1712     return d3dcompiler_realloc(ptr, size);
1713 }
1714
1715 static inline void d3dcompiler_free_rb(void *ptr)
1716 {
1717     d3dcompiler_free(ptr);
1718 }
1719 static const struct wine_rb_functions hlsl_type_rb_funcs =
1720 {
1721     d3dcompiler_alloc_rb,
1722     d3dcompiler_realloc_rb,
1723     d3dcompiler_free_rb,
1724     compare_hlsl_types_rb,
1725 };
1726
1727 void push_scope(struct hlsl_parse_ctx *ctx)
1728 {
1729     struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
1730
1731     if (!new_scope)
1732     {
1733         ERR("Out of memory!\n");
1734         return;
1735     }
1736     TRACE("Pushing a new scope\n");
1737     list_init(&new_scope->vars);
1738     if (wine_rb_init(&new_scope->types, &hlsl_type_rb_funcs) == -1)
1739     {
1740         ERR("Failed to initialize types rbtree.\n");
1741         d3dcompiler_free(new_scope);
1742         return;
1743     }
1744     new_scope->upper = ctx->cur_scope;
1745     ctx->cur_scope = new_scope;
1746     list_add_tail(&ctx->scopes, &new_scope->entry);
1747 }
1748
1749 BOOL pop_scope(struct hlsl_parse_ctx *ctx)
1750 {
1751     struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
1752     if (!prev_scope)
1753         return FALSE;
1754
1755     TRACE("Popping current scope\n");
1756     ctx->cur_scope = prev_scope;
1757     return TRUE;
1758 }
1759
1760 struct hlsl_ir_function_decl *new_func_decl(struct hlsl_type *return_type, struct list *parameters)
1761 {
1762     struct hlsl_ir_function_decl *decl;
1763
1764     decl = d3dcompiler_alloc(sizeof(*decl));
1765     if (!decl)
1766     {
1767         ERR("Out of memory.\n");
1768         return NULL;
1769     }
1770     decl->node.type = HLSL_IR_FUNCTION_DECL;
1771     decl->node.data_type = return_type;
1772     decl->parameters = parameters;
1773
1774     return decl;
1775 }
1776
1777 static int compare_param_hlsl_types(const struct hlsl_type *t1, const struct hlsl_type *t2)
1778 {
1779     if (t1->type != t2->type)
1780     {
1781         if (!((t1->type == HLSL_CLASS_SCALAR && t2->type == HLSL_CLASS_VECTOR)
1782                 || (t1->type == HLSL_CLASS_VECTOR && t2->type == HLSL_CLASS_SCALAR)))
1783             return t1->type - t2->type;
1784     }
1785     if (t1->base_type != t2->base_type)
1786         return t1->base_type - t2->base_type;
1787     if (t1->base_type == HLSL_TYPE_SAMPLER && t1->sampler_dim != t2->sampler_dim)
1788         return t1->sampler_dim - t2->sampler_dim;
1789     if (t1->dimx != t2->dimx)
1790         return t1->dimx - t2->dimx;
1791     if (t1->dimy != t2->dimy)
1792         return t1->dimx - t2->dimx;
1793     if (t1->type == HLSL_CLASS_STRUCT)
1794     {
1795         struct list *t1cur, *t2cur;
1796         struct hlsl_struct_field *t1field, *t2field;
1797         int r;
1798
1799         t1cur = list_head(t1->e.elements);
1800         t2cur = list_head(t2->e.elements);
1801         while (t1cur && t2cur)
1802         {
1803             t1field = LIST_ENTRY(t1cur, struct hlsl_struct_field, entry);
1804             t2field = LIST_ENTRY(t2cur, struct hlsl_struct_field, entry);
1805             if ((r = compare_param_hlsl_types(t1field->type, t2field->type)))
1806                 return r;
1807             if ((r = strcmp(t1field->name, t2field->name)))
1808                 return r;
1809             t1cur = list_next(t1->e.elements, t1cur);
1810             t2cur = list_next(t2->e.elements, t2cur);
1811         }
1812         if (t1cur != t2cur)
1813             return t1cur ? 1 : -1;
1814         return 0;
1815     }
1816     if (t1->type == HLSL_CLASS_ARRAY)
1817     {
1818         if (t1->e.array.elements_count != t2->e.array.elements_count)
1819             return t1->e.array.elements_count - t2->e.array.elements_count;
1820         return compare_param_hlsl_types(t1->e.array.type, t2->e.array.type);
1821     }
1822
1823     return 0;
1824 }
1825
1826 static int compare_function_decl_rb(const void *key, const struct wine_rb_entry *entry)
1827 {
1828     const struct list *params = (const struct list *)key;
1829     const struct hlsl_ir_function_decl *decl = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function_decl, entry);
1830     int params_count = params ? list_count(params) : 0;
1831     int decl_params_count = decl->parameters ? list_count(decl->parameters) : 0;
1832     int r;
1833     struct list *p1cur, *p2cur;
1834
1835     if (params_count != decl_params_count)
1836         return params_count - decl_params_count;
1837
1838     p1cur = params ? list_head(params) : NULL;
1839     p2cur = decl->parameters ? list_head(decl->parameters) : NULL;
1840     while (p1cur && p2cur)
1841     {
1842         struct hlsl_ir_var *p1, *p2;
1843         p1 = LIST_ENTRY(p1cur, struct hlsl_ir_var, node.entry);
1844         p2 = LIST_ENTRY(p2cur, struct hlsl_ir_var, node.entry);
1845         if ((r = compare_param_hlsl_types(p1->node.data_type, p2->node.data_type)))
1846             return r;
1847         p1cur = list_next(params, p1cur);
1848         p2cur = list_next(decl->parameters, p2cur);
1849     }
1850     return 0;
1851 }
1852
1853 static const struct wine_rb_functions hlsl_ir_function_decl_rb_funcs =
1854 {
1855     d3dcompiler_alloc_rb,
1856     d3dcompiler_realloc_rb,
1857     d3dcompiler_free_rb,
1858     compare_function_decl_rb,
1859 };
1860
1861 static int compare_function_rb(const void *key, const struct wine_rb_entry *entry)
1862 {
1863     const char *name = (const char *)key;
1864     const struct hlsl_ir_function *func = WINE_RB_ENTRY_VALUE(entry, const struct hlsl_ir_function,entry);
1865
1866     return strcmp(name, func->name);
1867 }
1868
1869 static const struct wine_rb_functions function_rb_funcs =
1870 {
1871     d3dcompiler_alloc_rb,
1872     d3dcompiler_realloc_rb,
1873     d3dcompiler_free_rb,
1874     compare_function_rb,
1875 };
1876
1877 void init_functions_tree(struct wine_rb_tree *funcs)
1878 {
1879     if (wine_rb_init(&hlsl_ctx.functions, &function_rb_funcs) == -1)
1880         ERR("Failed to initialize functions rbtree.\n");
1881 }
1882
1883 static const char *debug_base_type(const struct hlsl_type *type)
1884 {
1885     const char *name = "(unknown)";
1886
1887     switch (type->base_type)
1888     {
1889         case HLSL_TYPE_FLOAT:        name = "float";         break;
1890         case HLSL_TYPE_HALF:         name = "half";          break;
1891         case HLSL_TYPE_DOUBLE:       name = "double";        break;
1892         case HLSL_TYPE_INT:          name = "int";           break;
1893         case HLSL_TYPE_UINT:         name = "uint";          break;
1894         case HLSL_TYPE_BOOL:         name = "bool";          break;
1895         case HLSL_TYPE_SAMPLER:
1896             switch (type->sampler_dim)
1897             {
1898                 case HLSL_SAMPLER_DIM_GENERIC: name = "sampler";       break;
1899                 case HLSL_SAMPLER_DIM_1D:      name = "sampler1D";     break;
1900                 case HLSL_SAMPLER_DIM_2D:      name = "sampler2D";     break;
1901                 case HLSL_SAMPLER_DIM_3D:      name = "sampler3D";     break;
1902                 case HLSL_SAMPLER_DIM_CUBE:    name = "samplerCUBE";   break;
1903             }
1904             break;
1905         default:
1906             FIXME("Unhandled case %u\n", type->base_type);
1907     }
1908     return name;
1909 }
1910
1911 const char *debug_hlsl_type(const struct hlsl_type *type)
1912 {
1913     const char *name;
1914
1915     if (type->name)
1916         return debugstr_a(type->name);
1917
1918     if (type->type == HLSL_CLASS_STRUCT)
1919         return "<anonymous struct>";
1920
1921     if (type->type == HLSL_CLASS_ARRAY)
1922     {
1923         name = debug_base_type(type->e.array.type);
1924         return wine_dbg_sprintf("%s[%u]", name, type->e.array.elements_count);
1925     }
1926
1927     name = debug_base_type(type);
1928
1929     if (type->type == HLSL_CLASS_SCALAR)
1930         return wine_dbg_sprintf("%s", name);
1931     if (type->type == HLSL_CLASS_VECTOR)
1932         return wine_dbg_sprintf("%s%u", name, type->dimx);
1933     if (type->type == HLSL_CLASS_MATRIX)
1934         return wine_dbg_sprintf("%s%ux%u", name, type->dimx, type->dimy);
1935     return "unexpected_type";
1936 }
1937
1938 const char *debug_modifiers(DWORD modifiers)
1939 {
1940     char string[110];
1941
1942     string[0] = 0;
1943     if (modifiers & HLSL_STORAGE_EXTERN)
1944         strcat(string, " extern");                       /* 7 */
1945     if (modifiers & HLSL_STORAGE_NOINTERPOLATION)
1946         strcat(string, " nointerpolation");              /* 16 */
1947     if (modifiers & HLSL_MODIFIER_PRECISE)
1948         strcat(string, " precise");                      /* 8 */
1949     if (modifiers & HLSL_STORAGE_SHARED)
1950         strcat(string, " shared");                       /* 7 */
1951     if (modifiers & HLSL_STORAGE_GROUPSHARED)
1952         strcat(string, " groupshared");                  /* 12 */
1953     if (modifiers & HLSL_STORAGE_STATIC)
1954         strcat(string, " static");                       /* 7 */
1955     if (modifiers & HLSL_STORAGE_UNIFORM)
1956         strcat(string, " uniform");                      /* 8 */
1957     if (modifiers & HLSL_STORAGE_VOLATILE)
1958         strcat(string, " volatile");                     /* 9 */
1959     if (modifiers & HLSL_MODIFIER_CONST)
1960         strcat(string, " const");                        /* 6 */
1961     if (modifiers & HLSL_MODIFIER_ROW_MAJOR)
1962         strcat(string, " row_major");                    /* 10 */
1963     if (modifiers & HLSL_MODIFIER_COLUMN_MAJOR)
1964         strcat(string, " column_major");                 /* 13 */
1965     if ((modifiers & (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT)) == (HLSL_MODIFIER_IN | HLSL_MODIFIER_OUT))
1966         strcat(string, " inout");                        /* 6 */
1967     else if (modifiers & HLSL_MODIFIER_IN)
1968         strcat(string, " in");                           /* 3 */
1969     else if (modifiers & HLSL_MODIFIER_OUT)
1970         strcat(string, " out");                          /* 4 */
1971
1972     return wine_dbg_sprintf("%s", string[0] ? string + 1 : "");
1973 }
1974
1975 static const char *debug_node_type(enum hlsl_ir_node_type type)
1976 {
1977     const char *names[] =
1978     {
1979         "HLSL_IR_VAR",
1980         "HLSL_IR_ASSIGNMENT",
1981         "HLSL_IR_CONSTANT",
1982         "HLSL_IR_CONSTRUCTOR",
1983         "HLSL_IR_DEREF",
1984         "HLSL_IR_EXPR",
1985         "HLSL_IR_FUNCTION_DECL",
1986         "HLSL_IR_IF",
1987         "HLSL_IR_JUMP",
1988         "HLSL_IR_SWIZZLE",
1989     };
1990
1991     if (type >= sizeof(names) / sizeof(names[0]))
1992         return "Unexpected node type";
1993     return names[type];
1994 }
1995
1996 static void debug_dump_instr(const struct hlsl_ir_node *instr);
1997
1998 static void debug_dump_instr_list(const struct list *list)
1999 {
2000     struct hlsl_ir_node *instr;
2001
2002     LIST_FOR_EACH_ENTRY(instr, list, struct hlsl_ir_node, entry)
2003     {
2004         debug_dump_instr(instr);
2005         TRACE("\n");
2006     }
2007 }
2008
2009 static void debug_dump_ir_var(const struct hlsl_ir_var *var)
2010 {
2011     if (var->modifiers)
2012         TRACE("%s ", debug_modifiers(var->modifiers));
2013     TRACE("%s %s", debug_hlsl_type(var->node.data_type), var->name);
2014     if (var->semantic)
2015         TRACE(" : %s", debugstr_a(var->semantic));
2016 }
2017
2018 static void debug_dump_ir_deref(const struct hlsl_ir_deref *deref)
2019 {
2020     switch (deref->type)
2021     {
2022         case HLSL_IR_DEREF_VAR:
2023             TRACE("deref(");
2024             debug_dump_ir_var(deref->v.var);
2025             TRACE(")");
2026             break;
2027         case HLSL_IR_DEREF_ARRAY:
2028             debug_dump_instr(deref->v.array.array);
2029             TRACE("[");
2030             debug_dump_instr(deref->v.array.index);
2031             TRACE("]");
2032             break;
2033         case HLSL_IR_DEREF_RECORD:
2034             debug_dump_instr(deref->v.record.record);
2035             TRACE(".%s", debugstr_a(deref->v.record.field->name));
2036             break;
2037     }
2038 }
2039
2040 static void debug_dump_ir_constant(const struct hlsl_ir_constant *constant)
2041 {
2042     struct hlsl_type *type = constant->node.data_type;
2043     unsigned int x, y;
2044
2045     if (type->dimy != 1)
2046         TRACE("{");
2047     for (y = 0; y < type->dimy; ++y)
2048     {
2049         if (type->dimx != 1)
2050             TRACE("{");
2051         for (x = 0; x < type->dimx; ++x)
2052         {
2053             switch (type->base_type)
2054             {
2055                 case HLSL_TYPE_FLOAT:
2056                     TRACE("%g ", (double)constant->v.value.f[y * type->dimx + x]);
2057                     break;
2058                 case HLSL_TYPE_DOUBLE:
2059                     TRACE("%g ", constant->v.value.d[y * type->dimx + x]);
2060                     break;
2061                 case HLSL_TYPE_INT:
2062                     TRACE("%d ", constant->v.value.i[y * type->dimx + x]);
2063                     break;
2064                 case HLSL_TYPE_UINT:
2065                     TRACE("%u ", constant->v.value.u[y * type->dimx + x]);
2066                     break;
2067                 case HLSL_TYPE_BOOL:
2068                     TRACE("%s ", constant->v.value.b[y * type->dimx + x] == FALSE ? "false" : "true");
2069                     break;
2070                 default:
2071                     TRACE("Constants of type %s not supported\n", debug_base_type(type));
2072             }
2073         }
2074         if (type->dimx != 1)
2075             TRACE("}");
2076     }
2077     if (type->dimy != 1)
2078         TRACE("}");
2079 }
2080
2081 static const char *debug_expr_op(const struct hlsl_ir_expr *expr)
2082 {
2083     static const char *op_names[] =
2084     {
2085         "~",
2086         "!",
2087         "-",
2088         "abs",
2089         "sign",
2090         "rcp",
2091         "rsq",
2092         "sqrt",
2093         "nrm",
2094         "exp2",
2095         "log2",
2096
2097         "cast",
2098
2099         "fract",
2100
2101         "sin",
2102         "cos",
2103         "sin_reduced",
2104         "cos_reduced",
2105
2106         "dsx",
2107         "dsy",
2108
2109         "sat",
2110
2111         "+",
2112         "-",
2113         "*",
2114         "/",
2115
2116         "%",
2117
2118         "<",
2119         ">",
2120         "<=",
2121         ">=",
2122         "==",
2123         "!=",
2124
2125         "&&",
2126         "||",
2127
2128         "<<",
2129         ">>",
2130         "&",
2131         "|",
2132         "^",
2133
2134         "dot",
2135         "crs",
2136         "min",
2137         "max",
2138
2139         "pow",
2140
2141         "pre++",
2142         "pre--",
2143         "post++",
2144         "post--",
2145
2146         "lerp",
2147
2148         ",",
2149     };
2150
2151     if (expr->op == HLSL_IR_UNOP_CAST)
2152         return debug_hlsl_type(expr->node.data_type);
2153
2154     return op_names[expr->op];
2155 }
2156
2157 /* Dumps the expression in a prefix "operator (operands)" form */
2158 static void debug_dump_ir_expr(const struct hlsl_ir_expr *expr)
2159 {
2160     unsigned int i;
2161
2162     TRACE("%s (", debug_expr_op(expr));
2163     for (i = 0; i < 3 && expr->operands[i]; ++i)
2164     {
2165         debug_dump_instr(expr->operands[i]);
2166         TRACE(" ");
2167     }
2168     TRACE(")");
2169 }
2170
2171 static void debug_dump_ir_constructor(const struct hlsl_ir_constructor *constructor)
2172 {
2173     struct hlsl_ir_node *arg;
2174
2175     TRACE("%s (", debug_hlsl_type(constructor->node.data_type));
2176     LIST_FOR_EACH_ENTRY(arg, constructor->arguments, struct hlsl_ir_node, entry)
2177     {
2178         debug_dump_instr(arg);
2179         TRACE(" ");
2180     }
2181     TRACE(")");
2182 }
2183
2184 static const char *debug_writemask(DWORD writemask)
2185 {
2186     char string[5], components[] = {'x', 'y', 'z', 'w'};
2187     unsigned int i = 0, pos = 0;
2188
2189     while (writemask)
2190     {
2191         if (writemask & 1)
2192             string[pos++] = components[i];
2193         writemask >>= 1;
2194         i++;
2195     }
2196     string[pos] = '\0';
2197     return wine_dbg_sprintf(".%s", string);
2198 }
2199
2200 static void debug_dump_ir_assignment(const struct hlsl_ir_assignment *assign)
2201 {
2202     TRACE("= (");
2203     debug_dump_instr(assign->lhs);
2204     if (assign->writemask != BWRITERSP_WRITEMASK_ALL)
2205         TRACE("%s", debug_writemask(assign->writemask));
2206     TRACE(" ");
2207     debug_dump_instr(assign->rhs);
2208     TRACE(")");
2209 }
2210
2211 static void debug_dump_ir_swizzle(const struct hlsl_ir_swizzle *swizzle)
2212 {
2213     unsigned int i;
2214
2215     debug_dump_instr(swizzle->val);
2216     TRACE(".");
2217     if (swizzle->val->data_type->dimy > 1)
2218     {
2219         for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2220             TRACE("_m%u%u", (swizzle->swizzle >> i * 8) & 0xf, (swizzle->swizzle >> (i * 8 + 4)) & 0xf);
2221     }
2222     else
2223     {
2224         char c[] = {'x', 'y', 'z', 'w'};
2225
2226         for (i = 0; i < swizzle->node.data_type->dimx; ++i)
2227             TRACE("%c", c[(swizzle->swizzle >> i * 2) & 0x3]);
2228     }
2229 }
2230
2231 static void debug_dump_ir_jump(const struct hlsl_ir_jump *jump)
2232 {
2233     switch (jump->type)
2234     {
2235         case HLSL_IR_JUMP_BREAK:
2236             TRACE("break");
2237             break;
2238         case HLSL_IR_JUMP_CONTINUE:
2239             TRACE("continue");
2240             break;
2241         case HLSL_IR_JUMP_DISCARD:
2242             TRACE("discard");
2243             break;
2244         case HLSL_IR_JUMP_RETURN:
2245             TRACE("return ");
2246             if (jump->return_value)
2247                 debug_dump_instr(jump->return_value);
2248             TRACE(";");
2249             break;
2250     }
2251 }
2252
2253 static void debug_dump_ir_if(const struct hlsl_ir_if *if_node)
2254 {
2255     TRACE("if (");
2256     debug_dump_instr(if_node->condition);
2257     TRACE(")\n{\n");
2258     debug_dump_instr_list(if_node->then_instrs);
2259     TRACE("}\n");
2260     if (if_node->else_instrs)
2261     {
2262         TRACE("else\n{\n");
2263         debug_dump_instr_list(if_node->else_instrs);
2264         TRACE("}\n");
2265     }
2266 }
2267
2268 static void debug_dump_instr(const struct hlsl_ir_node *instr)
2269 {
2270     switch (instr->type)
2271     {
2272         case HLSL_IR_EXPR:
2273             debug_dump_ir_expr(expr_from_node(instr));
2274             break;
2275         case HLSL_IR_DEREF:
2276             debug_dump_ir_deref(deref_from_node(instr));
2277             break;
2278         case HLSL_IR_CONSTANT:
2279             debug_dump_ir_constant(constant_from_node(instr));
2280             break;
2281         case HLSL_IR_ASSIGNMENT:
2282             debug_dump_ir_assignment(assignment_from_node(instr));
2283             break;
2284         case HLSL_IR_SWIZZLE:
2285             debug_dump_ir_swizzle(swizzle_from_node(instr));
2286             break;
2287         case HLSL_IR_CONSTRUCTOR:
2288             debug_dump_ir_constructor(constructor_from_node(instr));
2289             break;
2290         case HLSL_IR_JUMP:
2291             debug_dump_ir_jump(jump_from_node(instr));
2292             break;
2293         case HLSL_IR_IF:
2294             debug_dump_ir_if(if_from_node(instr));
2295             break;
2296         default:
2297             TRACE("<No dump function for %s>", debug_node_type(instr->type));
2298     }
2299 }
2300
2301 void debug_dump_ir_function_decl(const struct hlsl_ir_function_decl *func)
2302 {
2303     struct hlsl_ir_var *param;
2304
2305     TRACE("Dumping function %s.\n", debugstr_a(func->func->name));
2306     TRACE("Function parameters:\n");
2307     LIST_FOR_EACH_ENTRY(param, func->parameters, struct hlsl_ir_var, node.entry)
2308     {
2309         debug_dump_ir_var(param);
2310         TRACE("\n");
2311     }
2312     if (func->semantic)
2313         TRACE("Function semantic: %s\n", debugstr_a(func->semantic));
2314     if (func->body)
2315     {
2316         debug_dump_instr_list(func->body);
2317     }
2318 }
2319
2320 void free_hlsl_type(struct hlsl_type *type)
2321 {
2322     struct hlsl_struct_field *field, *next_field;
2323
2324     d3dcompiler_free((void *)type->name);
2325     if (type->type == HLSL_CLASS_STRUCT)
2326     {
2327         LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
2328         {
2329             d3dcompiler_free((void *)field->name);
2330             d3dcompiler_free((void *)field->semantic);
2331             d3dcompiler_free(field);
2332         }
2333     }
2334     d3dcompiler_free(type);
2335 }
2336
2337 void free_instr_list(struct list *list)
2338 {
2339     struct hlsl_ir_node *node, *next_node;
2340
2341     if (!list)
2342         return;
2343     LIST_FOR_EACH_ENTRY_SAFE(node, next_node, list, struct hlsl_ir_node, entry)
2344         free_instr(node);
2345     d3dcompiler_free(list);
2346 }
2347
2348 static void free_ir_constant(struct hlsl_ir_constant *constant)
2349 {
2350     struct hlsl_type *type = constant->node.data_type;
2351     unsigned int i;
2352     struct hlsl_ir_constant *field, *next_field;
2353
2354     switch (type->type)
2355     {
2356         case HLSL_CLASS_ARRAY:
2357             for (i = 0; i < type->e.array.elements_count; ++i)
2358                 free_ir_constant(&constant->v.array_elements[i]);
2359             d3dcompiler_free(constant->v.array_elements);
2360             break;
2361         case HLSL_CLASS_STRUCT:
2362             LIST_FOR_EACH_ENTRY_SAFE(field, next_field, constant->v.struct_elements, struct hlsl_ir_constant, node.entry)
2363                 free_ir_constant(field);
2364             break;
2365         default:
2366             break;
2367     }
2368     d3dcompiler_free(constant);
2369 }
2370
2371 static void free_ir_deref(struct hlsl_ir_deref *deref)
2372 {
2373     switch (deref->type)
2374     {
2375         case HLSL_IR_DEREF_VAR:
2376             /* Variables are shared among nodes in the tree. */
2377             break;
2378         case HLSL_IR_DEREF_ARRAY:
2379             free_instr(deref->v.array.array);
2380             free_instr(deref->v.array.index);
2381             break;
2382         case HLSL_IR_DEREF_RECORD:
2383             free_instr(deref->v.record.record);
2384             break;
2385     }
2386     d3dcompiler_free(deref);
2387 }
2388
2389 static void free_ir_swizzle(struct hlsl_ir_swizzle *swizzle)
2390 {
2391     free_instr(swizzle->val);
2392     d3dcompiler_free(swizzle);
2393 }
2394
2395 static void free_ir_constructor(struct hlsl_ir_constructor *constructor)
2396 {
2397     free_instr_list(constructor->arguments);
2398     d3dcompiler_free(constructor);
2399 }
2400
2401 static void free_ir_expr(struct hlsl_ir_expr *expr)
2402 {
2403     unsigned int i;
2404
2405     for (i = 0; i < 3; ++i)
2406     {
2407         if (!expr->operands[i])
2408             break;
2409         free_instr(expr->operands[i]);
2410     }
2411     free_instr_list(expr->subexpressions);
2412     d3dcompiler_free(expr);
2413 }
2414
2415 static void free_ir_assignment(struct hlsl_ir_assignment *assignment)
2416 {
2417     free_instr(assignment->lhs);
2418     free_instr(assignment->rhs);
2419     d3dcompiler_free(assignment);
2420 }
2421
2422 static void free_ir_if(struct hlsl_ir_if *if_node)
2423 {
2424     free_instr(if_node->condition);
2425     free_instr_list(if_node->then_instrs);
2426     free_instr_list(if_node->else_instrs);
2427     d3dcompiler_free(if_node);
2428 }
2429
2430 static void free_ir_jump(struct hlsl_ir_jump *jump)
2431 {
2432     if (jump->type == HLSL_IR_JUMP_RETURN)
2433         free_instr(jump->return_value);
2434     d3dcompiler_free(jump);
2435 }
2436
2437 void free_instr(struct hlsl_ir_node *node)
2438 {
2439     switch (node->type)
2440     {
2441         case HLSL_IR_VAR:
2442             /* These are freed later on from the scopes. */
2443             break;
2444         case HLSL_IR_CONSTANT:
2445             free_ir_constant(constant_from_node(node));
2446             break;
2447         case HLSL_IR_DEREF:
2448             free_ir_deref(deref_from_node(node));
2449             break;
2450         case HLSL_IR_SWIZZLE:
2451             free_ir_swizzle(swizzle_from_node(node));
2452             break;
2453         case HLSL_IR_CONSTRUCTOR:
2454             free_ir_constructor(constructor_from_node(node));
2455             break;
2456         case HLSL_IR_EXPR:
2457             free_ir_expr(expr_from_node(node));
2458             break;
2459         case HLSL_IR_ASSIGNMENT:
2460             free_ir_assignment(assignment_from_node(node));
2461             break;
2462         case HLSL_IR_IF:
2463             free_ir_if(if_from_node(node));
2464             break;
2465         case HLSL_IR_JUMP:
2466             free_ir_jump(jump_from_node(node));
2467             break;
2468         default:
2469             FIXME("Unsupported node type %s\n", debug_node_type(node->type));
2470     }
2471 }
2472
2473 static void free_function_decl(struct hlsl_ir_function_decl *func)
2474 {
2475     d3dcompiler_free((void *)func->semantic);
2476     d3dcompiler_free(func->parameters);
2477     free_instr_list(func->body);
2478     d3dcompiler_free(func);
2479 }
2480
2481 static void free_function_decl_rb(struct wine_rb_entry *entry, void *context)
2482 {
2483     free_function_decl(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry));
2484 }
2485
2486 static void free_function(struct hlsl_ir_function *func)
2487 {
2488     wine_rb_destroy(&func->overloads, free_function_decl_rb, NULL);
2489     d3dcompiler_free((void *)func->name);
2490 }
2491
2492 void free_function_rb(struct wine_rb_entry *entry, void *context)
2493 {
2494     free_function(WINE_RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry));
2495 }
2496
2497 void add_function_decl(struct wine_rb_tree *funcs, char *name, struct hlsl_ir_function_decl *decl, BOOL intrinsic)
2498 {
2499     struct hlsl_ir_function *func;
2500     struct wine_rb_entry *func_entry, *old_entry;
2501
2502     func_entry = wine_rb_get(funcs, name);
2503     if (func_entry)
2504     {
2505         func = WINE_RB_ENTRY_VALUE(func_entry, struct hlsl_ir_function, entry);
2506         decl->func = func;
2507         if ((old_entry = wine_rb_get(&func->overloads, decl->parameters)))
2508         {
2509             struct hlsl_ir_function_decl *old_decl =
2510                     WINE_RB_ENTRY_VALUE(old_entry, struct hlsl_ir_function_decl, entry);
2511
2512             if (!decl->body)
2513             {
2514                 free_function_decl(decl);
2515                 d3dcompiler_free(name);
2516                 return;
2517             }
2518             wine_rb_remove(&func->overloads, decl->parameters);
2519             free_function_decl(old_decl);
2520         }
2521         wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2522         d3dcompiler_free(name);
2523         return;
2524     }
2525     func = d3dcompiler_alloc(sizeof(*func));
2526     func->name = name;
2527     if (wine_rb_init(&func->overloads, &hlsl_ir_function_decl_rb_funcs) == -1)
2528     {
2529         ERR("Failed to initialize function rbtree.\n");
2530         d3dcompiler_free(name);
2531         d3dcompiler_free(func);
2532         return;
2533     }
2534     decl->func = func;
2535     wine_rb_put(&func->overloads, decl->parameters, &decl->entry);
2536     func->intrinsic = intrinsic;
2537     wine_rb_put(funcs, func->name, &func->entry);
2538 }