Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5564 | serge | 1 | /* |
2 | * Copyright © 2010 Intel Corporation |
||
3 | * |
||
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
||
5 | * copy of this software and associated documentation files (the "Software"), |
||
6 | * to deal in the Software without restriction, including without limitation |
||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||
8 | * and/or sell copies of the Software, and to permit persons to whom the |
||
9 | * Software is furnished to do so, subject to the following conditions: |
||
10 | * |
||
11 | * The above copyright notice and this permission notice (including the next |
||
12 | * paragraph) shall be included in all copies or substantial portions of the |
||
13 | * Software. |
||
14 | * |
||
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||
21 | * DEALINGS IN THE SOFTWARE. |
||
22 | */ |
||
23 | |||
24 | #include "glsl_types.h" |
||
25 | #include "ir.h" |
||
26 | #include "glsl_parser_extras.h" |
||
27 | #include "main/errors.h" |
||
28 | |||
29 | typedef enum { |
||
30 | PARAMETER_LIST_NO_MATCH, |
||
31 | PARAMETER_LIST_EXACT_MATCH, |
||
32 | PARAMETER_LIST_INEXACT_MATCH /*< Match requires implicit conversion. */ |
||
33 | } parameter_list_match_t; |
||
34 | |||
35 | /** |
||
36 | * \brief Check if two parameter lists match. |
||
37 | * |
||
38 | * \param list_a Parameters of the function definition. |
||
39 | * \param list_b Actual parameters passed to the function. |
||
40 | * \see matching_signature() |
||
41 | */ |
||
42 | static parameter_list_match_t |
||
43 | parameter_lists_match(_mesa_glsl_parse_state *state, |
||
44 | const exec_list *list_a, const exec_list *list_b) |
||
45 | { |
||
46 | const exec_node *node_a = list_a->head; |
||
47 | const exec_node *node_b = list_b->head; |
||
48 | |||
49 | /* This is set to true if there is an inexact match requiring an implicit |
||
50 | * conversion. */ |
||
51 | bool inexact_match = false; |
||
52 | |||
53 | for (/* empty */ |
||
54 | ; !node_a->is_tail_sentinel() |
||
55 | ; node_a = node_a->next, node_b = node_b->next) { |
||
56 | /* If all of the parameters from the other parameter list have been |
||
57 | * exhausted, the lists have different length and, by definition, |
||
58 | * do not match. |
||
59 | */ |
||
60 | if (node_b->is_tail_sentinel()) |
||
61 | return PARAMETER_LIST_NO_MATCH; |
||
62 | |||
63 | |||
64 | const ir_variable *const param = (ir_variable *) node_a; |
||
65 | const ir_rvalue *const actual = (ir_rvalue *) node_b; |
||
66 | |||
67 | if (param->type == actual->type) |
||
68 | continue; |
||
69 | |||
70 | /* Try to find an implicit conversion from actual to param. */ |
||
71 | inexact_match = true; |
||
72 | switch ((enum ir_variable_mode)(param->data.mode)) { |
||
73 | case ir_var_auto: |
||
74 | case ir_var_uniform: |
||
75 | case ir_var_temporary: |
||
76 | /* These are all error conditions. It is invalid for a parameter to |
||
77 | * a function to be declared as auto (not in, out, or inout) or |
||
78 | * as uniform. |
||
79 | */ |
||
80 | assert(0); |
||
81 | return PARAMETER_LIST_NO_MATCH; |
||
82 | |||
83 | case ir_var_const_in: |
||
84 | case ir_var_function_in: |
||
85 | if (!actual->type->can_implicitly_convert_to(param->type, state)) |
||
86 | return PARAMETER_LIST_NO_MATCH; |
||
87 | break; |
||
88 | |||
89 | case ir_var_function_out: |
||
90 | if (!param->type->can_implicitly_convert_to(actual->type, state)) |
||
91 | return PARAMETER_LIST_NO_MATCH; |
||
92 | break; |
||
93 | |||
94 | case ir_var_function_inout: |
||
95 | /* Since there are no bi-directional automatic conversions (e.g., |
||
96 | * there is int -> float but no float -> int), inout parameters must |
||
97 | * be exact matches. |
||
98 | */ |
||
99 | return PARAMETER_LIST_NO_MATCH; |
||
100 | |||
101 | default: |
||
102 | assert(false); |
||
103 | return PARAMETER_LIST_NO_MATCH; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /* If all of the parameters from the other parameter list have been |
||
108 | * exhausted, the lists have different length and, by definition, do not |
||
109 | * match. |
||
110 | */ |
||
111 | if (!node_b->is_tail_sentinel()) |
||
112 | return PARAMETER_LIST_NO_MATCH; |
||
113 | |||
114 | if (inexact_match) |
||
115 | return PARAMETER_LIST_INEXACT_MATCH; |
||
116 | else |
||
117 | return PARAMETER_LIST_EXACT_MATCH; |
||
118 | } |
||
119 | |||
120 | |||
121 | /* Classes of parameter match, sorted (mostly) best matches first. |
||
122 | * See is_better_parameter_match() below for the exceptions. |
||
123 | * */ |
||
124 | typedef enum { |
||
125 | PARAMETER_EXACT_MATCH, |
||
126 | PARAMETER_FLOAT_TO_DOUBLE, |
||
127 | PARAMETER_INT_TO_FLOAT, |
||
128 | PARAMETER_INT_TO_DOUBLE, |
||
129 | PARAMETER_OTHER_CONVERSION, |
||
130 | } parameter_match_t; |
||
131 | |||
132 | |||
133 | static parameter_match_t |
||
134 | get_parameter_match_type(const ir_variable *param, |
||
135 | const ir_rvalue *actual) |
||
136 | { |
||
137 | const glsl_type *from_type; |
||
138 | const glsl_type *to_type; |
||
139 | |||
140 | if (param->data.mode == ir_var_function_out) { |
||
141 | from_type = param->type; |
||
142 | to_type = actual->type; |
||
143 | } else { |
||
144 | from_type = actual->type; |
||
145 | to_type = param->type; |
||
146 | } |
||
147 | |||
148 | if (from_type == to_type) |
||
149 | return PARAMETER_EXACT_MATCH; |
||
150 | |||
151 | /* XXX: When ARB_gpu_shader_fp64 support is added, check for float->double, |
||
152 | * and int/uint->double conversions |
||
153 | */ |
||
154 | |||
155 | if (to_type->base_type == GLSL_TYPE_FLOAT) |
||
156 | return PARAMETER_INT_TO_FLOAT; |
||
157 | |||
158 | /* int -> uint and any other oddball conversions */ |
||
159 | return PARAMETER_OTHER_CONVERSION; |
||
160 | } |
||
161 | |||
162 | |||
163 | static bool |
||
164 | is_better_parameter_match(parameter_match_t a_match, |
||
165 | parameter_match_t b_match) |
||
166 | { |
||
167 | /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec): |
||
168 | * |
||
169 | * 1. An exact match is better than a match involving any implicit |
||
170 | * conversion. |
||
171 | * |
||
172 | * 2. A match involving an implicit conversion from float to double |
||
173 | * is better than match involving any other implicit conversion. |
||
174 | * |
||
175 | * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5: |
||
176 | * 3. A match involving an implicit conversion from either int or uint |
||
177 | * to float is better than a match involving an implicit conversion |
||
178 | * from either int or uint to double.] |
||
179 | * |
||
180 | * If none of the rules above apply to a particular pair of conversions, |
||
181 | * neither conversion is considered better than the other. |
||
182 | * |
||
183 | * -- |
||
184 | * |
||
185 | * Notably, the int->uint conversion is *not* considered to be better |
||
186 | * or worse than int/uint->float or int/uint->double. |
||
187 | */ |
||
188 | |||
189 | if (a_match >= PARAMETER_INT_TO_FLOAT && b_match == PARAMETER_OTHER_CONVERSION) |
||
190 | return false; |
||
191 | |||
192 | return a_match < b_match; |
||
193 | } |
||
194 | |||
195 | |||
196 | static bool |
||
197 | is_best_inexact_overload(const exec_list *actual_parameters, |
||
198 | ir_function_signature **matches, |
||
199 | int num_matches, |
||
200 | ir_function_signature *sig) |
||
201 | { |
||
202 | /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec): |
||
203 | * |
||
204 | * "A function definition A is considered a better |
||
205 | * match than function definition B if: |
||
206 | * |
||
207 | * * for at least one function argument, the conversion for that argument |
||
208 | * in A is better than the corresponding conversion in B; and |
||
209 | * |
||
210 | * * there is no function argument for which the conversion in B is better |
||
211 | * than the corresponding conversion in A. |
||
212 | * |
||
213 | * If a single function definition is considered a better match than every |
||
214 | * other matching function definition, it will be used. Otherwise, a |
||
215 | * semantic error occurs and the shader will fail to compile." |
||
216 | */ |
||
217 | for (ir_function_signature **other = matches; |
||
218 | other < matches + num_matches; other++) { |
||
219 | if (*other == sig) |
||
220 | continue; |
||
221 | |||
222 | const exec_node *node_a = sig->parameters.head; |
||
223 | const exec_node *node_b = (*other)->parameters.head; |
||
224 | const exec_node *node_p = actual_parameters->head; |
||
225 | |||
226 | bool better_for_some_parameter = false; |
||
227 | |||
228 | for (/* empty */ |
||
229 | ; !node_a->is_tail_sentinel() |
||
230 | ; node_a = node_a->next, |
||
231 | node_b = node_b->next, |
||
232 | node_p = node_p->next) { |
||
233 | parameter_match_t a_match = get_parameter_match_type( |
||
234 | (const ir_variable *)node_a, |
||
235 | (const ir_rvalue *)node_p); |
||
236 | parameter_match_t b_match = get_parameter_match_type( |
||
237 | (const ir_variable *)node_b, |
||
238 | (const ir_rvalue *)node_p); |
||
239 | |||
240 | if (is_better_parameter_match(a_match, b_match)) |
||
241 | better_for_some_parameter = true; |
||
242 | |||
243 | if (is_better_parameter_match(b_match, a_match)) |
||
244 | return false; /* B is better for this parameter */ |
||
245 | } |
||
246 | |||
247 | if (!better_for_some_parameter) |
||
248 | return false; /* A must be better than B for some parameter */ |
||
249 | |||
250 | } |
||
251 | |||
252 | return true; |
||
253 | } |
||
254 | |||
255 | |||
256 | static ir_function_signature * |
||
257 | choose_best_inexact_overload(_mesa_glsl_parse_state *state, |
||
258 | const exec_list *actual_parameters, |
||
259 | ir_function_signature **matches, |
||
260 | int num_matches) |
||
261 | { |
||
262 | if (num_matches == 0) |
||
263 | return NULL; |
||
264 | |||
265 | if (num_matches == 1) |
||
266 | return *matches; |
||
267 | |||
268 | /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution |
||
269 | * among multiple inexact matches. Note that state may be NULL here if |
||
270 | * called from the linker; in that case we assume everything supported in |
||
271 | * any GLSL version is available. */ |
||
272 | if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) { |
||
273 | for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) { |
||
274 | if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig)) |
||
275 | return *sig; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return NULL; /* no best candidate */ |
||
280 | } |
||
281 | |||
282 | |||
283 | ir_function_signature * |
||
284 | ir_function::matching_signature(_mesa_glsl_parse_state *state, |
||
285 | const exec_list *actual_parameters, |
||
286 | bool allow_builtins) |
||
287 | { |
||
288 | bool is_exact; |
||
289 | return matching_signature(state, actual_parameters, allow_builtins, |
||
290 | &is_exact); |
||
291 | } |
||
292 | |||
293 | ir_function_signature * |
||
294 | ir_function::matching_signature(_mesa_glsl_parse_state *state, |
||
295 | const exec_list *actual_parameters, |
||
296 | bool allow_builtins, |
||
297 | bool *is_exact) |
||
298 | { |
||
299 | ir_function_signature **inexact_matches = NULL; |
||
300 | ir_function_signature **inexact_matches_temp; |
||
301 | ir_function_signature *match = NULL; |
||
302 | int num_inexact_matches = 0; |
||
303 | |||
304 | /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec: |
||
305 | * |
||
306 | * "If an exact match is found, the other signatures are ignored, and |
||
307 | * the exact match is used. Otherwise, if no exact match is found, then |
||
308 | * the implicit conversions in Section 4.1.10 "Implicit Conversions" will |
||
309 | * be applied to the calling arguments if this can make their types match |
||
310 | * a signature. In this case, it is a semantic error if there are |
||
311 | * multiple ways to apply these conversions to the actual arguments of a |
||
312 | * call such that the call can be made to match multiple signatures." |
||
313 | */ |
||
314 | foreach_in_list(ir_function_signature, sig, &this->signatures) { |
||
315 | /* Skip over any built-ins that aren't available in this shader. */ |
||
316 | if (sig->is_builtin() && (!allow_builtins || |
||
317 | !sig->is_builtin_available(state))) |
||
318 | continue; |
||
319 | |||
320 | switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) { |
||
321 | case PARAMETER_LIST_EXACT_MATCH: |
||
322 | *is_exact = true; |
||
323 | free(inexact_matches); |
||
324 | return sig; |
||
325 | case PARAMETER_LIST_INEXACT_MATCH: |
||
326 | inexact_matches_temp = (ir_function_signature **) |
||
327 | realloc(inexact_matches, |
||
328 | sizeof(*inexact_matches) * |
||
329 | (num_inexact_matches + 1)); |
||
330 | if (inexact_matches_temp == NULL) { |
||
331 | _mesa_error_no_memory(__func__); |
||
332 | free(inexact_matches); |
||
333 | return NULL; |
||
334 | } |
||
335 | inexact_matches = inexact_matches_temp; |
||
336 | inexact_matches[num_inexact_matches++] = sig; |
||
337 | continue; |
||
338 | case PARAMETER_LIST_NO_MATCH: |
||
339 | continue; |
||
340 | default: |
||
341 | assert(false); |
||
342 | return NULL; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /* There is no exact match (we would have returned it by now). If there |
||
347 | * are multiple inexact matches, the call is ambiguous, which is an error. |
||
348 | * |
||
349 | * FINISHME: Report a decent error. Returning NULL will likely result in |
||
350 | * FINISHME: a "no matching signature" error; it should report that the |
||
351 | * FINISHME: call is ambiguous. But reporting errors from here is hard. |
||
352 | */ |
||
353 | *is_exact = false; |
||
354 | |||
355 | match = choose_best_inexact_overload(state, actual_parameters, |
||
356 | inexact_matches, num_inexact_matches); |
||
357 | |||
358 | free(inexact_matches); |
||
359 | return match; |
||
360 | } |
||
361 | |||
362 | |||
363 | static bool |
||
364 | parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b) |
||
365 | { |
||
366 | const exec_node *node_a = list_a->head; |
||
367 | const exec_node *node_b = list_b->head; |
||
368 | |||
369 | for (/* empty */ |
||
370 | ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel() |
||
371 | ; node_a = node_a->next, node_b = node_b->next) { |
||
372 | ir_variable *a = (ir_variable *) node_a; |
||
373 | ir_variable *b = (ir_variable *) node_b; |
||
374 | |||
375 | /* If the types of the parameters do not match, the parameters lists |
||
376 | * are different. |
||
377 | */ |
||
378 | if (a->type != b->type) |
||
379 | return false; |
||
380 | } |
||
381 | |||
382 | /* Unless both lists are exhausted, they differ in length and, by |
||
383 | * definition, do not match. |
||
384 | */ |
||
385 | return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel()); |
||
386 | } |
||
387 | |||
388 | ir_function_signature * |
||
389 | ir_function::exact_matching_signature(_mesa_glsl_parse_state *state, |
||
390 | const exec_list *actual_parameters) |
||
391 | { |
||
392 | foreach_in_list(ir_function_signature, sig, &this->signatures) { |
||
393 | /* Skip over any built-ins that aren't available in this shader. */ |
||
394 | if (sig->is_builtin() && !sig->is_builtin_available(state)) |
||
395 | continue; |
||
396 | |||
397 | if (parameter_lists_match_exact(&sig->parameters, actual_parameters)) |
||
398 | return sig; |
||
399 | } |
||
400 | return NULL; |
||
401 | }>>>> |