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 "compiler/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
parameter_lists_match(_mesa_glsl_parse_state * state,const exec_list * list_a,const exec_list * list_b)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->get_head_raw();
47 const exec_node *node_b = list_b->get_head_raw();
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_shader_storage:
76 case ir_var_temporary:
77 /* These are all error conditions. It is invalid for a parameter to
78 * a function to be declared as auto (not in, out, or inout) or
79 * as uniform.
80 */
81 assert(0);
82 return PARAMETER_LIST_NO_MATCH;
83
84 case ir_var_const_in:
85 case ir_var_function_in:
86 if (param->data.implicit_conversion_prohibited ||
87 !actual->type->can_implicitly_convert_to(param->type, state))
88 return PARAMETER_LIST_NO_MATCH;
89 break;
90
91 case ir_var_function_out:
92 if (!param->type->can_implicitly_convert_to(actual->type, state))
93 return PARAMETER_LIST_NO_MATCH;
94 break;
95
96 case ir_var_function_inout:
97 /* Since there are no bi-directional automatic conversions (e.g.,
98 * there is int -> float but no float -> int), inout parameters must
99 * be exact matches.
100 */
101 return PARAMETER_LIST_NO_MATCH;
102
103 default:
104 assert(false);
105 return PARAMETER_LIST_NO_MATCH;
106 }
107 }
108
109 /* If all of the parameters from the other parameter list have been
110 * exhausted, the lists have different length and, by definition, do not
111 * match.
112 */
113 if (!node_b->is_tail_sentinel())
114 return PARAMETER_LIST_NO_MATCH;
115
116 if (inexact_match)
117 return PARAMETER_LIST_INEXACT_MATCH;
118 else
119 return PARAMETER_LIST_EXACT_MATCH;
120 }
121
122
123 /* Classes of parameter match, sorted (mostly) best matches first.
124 * See is_better_parameter_match() below for the exceptions.
125 * */
126 typedef enum {
127 PARAMETER_EXACT_MATCH,
128 PARAMETER_FLOAT_TO_DOUBLE,
129 PARAMETER_INT_TO_FLOAT,
130 PARAMETER_INT_TO_DOUBLE,
131 PARAMETER_OTHER_CONVERSION,
132 } parameter_match_t;
133
134
135 static parameter_match_t
get_parameter_match_type(const ir_variable * param,const ir_rvalue * actual)136 get_parameter_match_type(const ir_variable *param,
137 const ir_rvalue *actual)
138 {
139 const glsl_type *from_type;
140 const glsl_type *to_type;
141
142 if (param->data.mode == ir_var_function_out) {
143 from_type = param->type;
144 to_type = actual->type;
145 } else {
146 from_type = actual->type;
147 to_type = param->type;
148 }
149
150 if (from_type == to_type)
151 return PARAMETER_EXACT_MATCH;
152
153 if (to_type->is_double()) {
154 if (from_type->is_float())
155 return PARAMETER_FLOAT_TO_DOUBLE;
156 return PARAMETER_INT_TO_DOUBLE;
157 }
158
159 if (to_type->is_float())
160 return PARAMETER_INT_TO_FLOAT;
161
162 /* int -> uint and any other oddball conversions */
163 return PARAMETER_OTHER_CONVERSION;
164 }
165
166
167 static bool
is_better_parameter_match(parameter_match_t a_match,parameter_match_t b_match)168 is_better_parameter_match(parameter_match_t a_match,
169 parameter_match_t b_match)
170 {
171 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
172 *
173 * 1. An exact match is better than a match involving any implicit
174 * conversion.
175 *
176 * 2. A match involving an implicit conversion from float to double
177 * is better than match involving any other implicit conversion.
178 *
179 * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
180 * 3. A match involving an implicit conversion from either int or uint
181 * to float is better than a match involving an implicit conversion
182 * from either int or uint to double.]
183 *
184 * If none of the rules above apply to a particular pair of conversions,
185 * neither conversion is considered better than the other.
186 *
187 * --
188 *
189 * Notably, the int->uint conversion is *not* considered to be better
190 * or worse than int/uint->float or int/uint->double.
191 */
192
193 if (a_match >= PARAMETER_INT_TO_FLOAT && b_match == PARAMETER_OTHER_CONVERSION)
194 return false;
195
196 return a_match < b_match;
197 }
198
199
200 static bool
is_best_inexact_overload(const exec_list * actual_parameters,ir_function_signature ** matches,int num_matches,ir_function_signature * sig)201 is_best_inexact_overload(const exec_list *actual_parameters,
202 ir_function_signature **matches,
203 int num_matches,
204 ir_function_signature *sig)
205 {
206 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
207 *
208 * "A function definition A is considered a better
209 * match than function definition B if:
210 *
211 * * for at least one function argument, the conversion for that argument
212 * in A is better than the corresponding conversion in B; and
213 *
214 * * there is no function argument for which the conversion in B is better
215 * than the corresponding conversion in A.
216 *
217 * If a single function definition is considered a better match than every
218 * other matching function definition, it will be used. Otherwise, a
219 * semantic error occurs and the shader will fail to compile."
220 */
221 for (ir_function_signature **other = matches;
222 other < matches + num_matches; other++) {
223 if (*other == sig)
224 continue;
225
226 const exec_node *node_a = sig->parameters.get_head_raw();
227 const exec_node *node_b = (*other)->parameters.get_head_raw();
228 const exec_node *node_p = actual_parameters->get_head_raw();
229
230 bool better_for_some_parameter = false;
231
232 for (/* empty */
233 ; !node_a->is_tail_sentinel()
234 ; node_a = node_a->next,
235 node_b = node_b->next,
236 node_p = node_p->next) {
237 parameter_match_t a_match = get_parameter_match_type(
238 (const ir_variable *)node_a,
239 (const ir_rvalue *)node_p);
240 parameter_match_t b_match = get_parameter_match_type(
241 (const ir_variable *)node_b,
242 (const ir_rvalue *)node_p);
243
244 if (is_better_parameter_match(a_match, b_match))
245 better_for_some_parameter = true;
246
247 if (is_better_parameter_match(b_match, a_match))
248 return false; /* B is better for this parameter */
249 }
250
251 if (!better_for_some_parameter)
252 return false; /* A must be better than B for some parameter */
253
254 }
255
256 return true;
257 }
258
259
260 static ir_function_signature *
choose_best_inexact_overload(_mesa_glsl_parse_state * state,const exec_list * actual_parameters,ir_function_signature ** matches,int num_matches)261 choose_best_inexact_overload(_mesa_glsl_parse_state *state,
262 const exec_list *actual_parameters,
263 ir_function_signature **matches,
264 int num_matches)
265 {
266 if (num_matches == 0)
267 return NULL;
268
269 if (num_matches == 1)
270 return *matches;
271
272 /* Without GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions,
273 * there is no overload resolution among multiple inexact matches. Note
274 * that state may be NULL here if called from the linker; in that case we
275 * assume everything supported in any GLSL version is available.
276 */
277 if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
278 state->MESA_shader_integer_functions_enable ||
279 state->EXT_shader_implicit_conversions_enable) {
280 for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) {
281 if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig))
282 return *sig;
283 }
284 }
285
286 return NULL; /* no best candidate */
287 }
288
289
290 ir_function_signature *
matching_signature(_mesa_glsl_parse_state * state,const exec_list * actual_parameters,bool allow_builtins)291 ir_function::matching_signature(_mesa_glsl_parse_state *state,
292 const exec_list *actual_parameters,
293 bool allow_builtins)
294 {
295 bool is_exact;
296 return matching_signature(state, actual_parameters, allow_builtins,
297 &is_exact);
298 }
299
300 ir_function_signature *
matching_signature(_mesa_glsl_parse_state * state,const exec_list * actual_parameters,bool allow_builtins,bool * is_exact)301 ir_function::matching_signature(_mesa_glsl_parse_state *state,
302 const exec_list *actual_parameters,
303 bool allow_builtins,
304 bool *is_exact)
305 {
306 ir_function_signature **inexact_matches = NULL;
307 ir_function_signature **inexact_matches_temp;
308 ir_function_signature *match = NULL;
309 int num_inexact_matches = 0;
310
311 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
312 *
313 * "If an exact match is found, the other signatures are ignored, and
314 * the exact match is used. Otherwise, if no exact match is found, then
315 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
316 * be applied to the calling arguments if this can make their types match
317 * a signature. In this case, it is a semantic error if there are
318 * multiple ways to apply these conversions to the actual arguments of a
319 * call such that the call can be made to match multiple signatures."
320 */
321 foreach_in_list(ir_function_signature, sig, &this->signatures) {
322 /* Skip over any built-ins that aren't available in this shader. */
323 if (sig->is_builtin() && (!allow_builtins ||
324 !sig->is_builtin_available(state)))
325 continue;
326
327 switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) {
328 case PARAMETER_LIST_EXACT_MATCH:
329 *is_exact = true;
330 free(inexact_matches);
331 return sig;
332 case PARAMETER_LIST_INEXACT_MATCH:
333 /* Subroutine signatures must match exactly */
334 if (this->is_subroutine)
335 continue;
336 inexact_matches_temp = (ir_function_signature **)
337 realloc(inexact_matches,
338 sizeof(*inexact_matches) *
339 (num_inexact_matches + 1));
340 if (inexact_matches_temp == NULL) {
341 _mesa_error_no_memory(__func__);
342 free(inexact_matches);
343 return NULL;
344 }
345 inexact_matches = inexact_matches_temp;
346 inexact_matches[num_inexact_matches++] = sig;
347 continue;
348 case PARAMETER_LIST_NO_MATCH:
349 continue;
350 default:
351 assert(false);
352 return NULL;
353 }
354 }
355
356 /* There is no exact match (we would have returned it by now). If there
357 * are multiple inexact matches, the call is ambiguous, which is an error.
358 *
359 * FINISHME: Report a decent error. Returning NULL will likely result in
360 * FINISHME: a "no matching signature" error; it should report that the
361 * FINISHME: call is ambiguous. But reporting errors from here is hard.
362 */
363 *is_exact = false;
364
365 match = choose_best_inexact_overload(state, actual_parameters,
366 inexact_matches, num_inexact_matches);
367
368 free(inexact_matches);
369 return match;
370 }
371
372
373 static bool
parameter_lists_match_exact(const exec_list * list_a,const exec_list * list_b)374 parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
375 {
376 const exec_node *node_a = list_a->get_head_raw();
377 const exec_node *node_b = list_b->get_head_raw();
378
379 for (/* empty */
380 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
381 ; node_a = node_a->next, node_b = node_b->next) {
382 ir_variable *a = (ir_variable *) node_a;
383 ir_variable *b = (ir_variable *) node_b;
384
385 /* If the types of the parameters do not match, the parameters lists
386 * are different.
387 */
388 if (a->type != b->type)
389 return false;
390 }
391
392 /* Unless both lists are exhausted, they differ in length and, by
393 * definition, do not match.
394 */
395 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
396 }
397
398 ir_function_signature *
exact_matching_signature(_mesa_glsl_parse_state * state,const exec_list * actual_parameters)399 ir_function::exact_matching_signature(_mesa_glsl_parse_state *state,
400 const exec_list *actual_parameters)
401 {
402 foreach_in_list(ir_function_signature, sig, &this->signatures) {
403 /* Skip over any built-ins that aren't available in this shader. */
404 if (sig->is_builtin() && !sig->is_builtin_available(state))
405 continue;
406
407 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
408 return sig;
409 }
410 return NULL;
411 }
412