blob: 0a97e014244501815916b19f3cbaeb4ccab82f3e [file] [log] [blame]
Ian Romanick471471f2010-03-11 14:50:30 -08001/*
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
27int
28type_compare(const glsl_type *a, const glsl_type *b)
29{
Ian Romanick80b5ed62010-03-25 13:05:43 -070030 /* If the types are the same, they trivially match.
31 */
32 if (a == b)
33 return 0;
34
Ian Romanick471471f2010-03-11 14:50:30 -080035 switch (a->base_type) {
36 case GLSL_TYPE_UINT:
37 case GLSL_TYPE_INT:
Ian Romanick471471f2010-03-11 14:50:30 -080038 case GLSL_TYPE_BOOL:
Ian Romanickfe1c7ff2010-04-02 11:45:06 -070039 /* There is no implicit conversion to or from integer types or bool.
40 */
41 if ((a->is_integer() != b->is_integer())
42 || (a->is_boolean() != b->is_boolean()))
Ian Romanick471471f2010-03-11 14:50:30 -080043 return -1;
44
Ian Romanickfe1c7ff2010-04-02 11:45:06 -070045 /* FALLTHROUGH */
46
47 case GLSL_TYPE_FLOAT:
48 if ((a->vector_elements != b->vector_elements)
49 || (a->matrix_columns != b->matrix_columns))
Ian Romanick471471f2010-03-11 14:50:30 -080050 return -1;
51
52 return 1;
53
54 case GLSL_TYPE_SAMPLER:
Ian Romanick471471f2010-03-11 14:50:30 -080055 case GLSL_TYPE_STRUCT:
Ian Romanick80b5ed62010-03-25 13:05:43 -070056 /* Samplers and structures must match exactly.
57 */
58 return -1;
Ian Romanick471471f2010-03-11 14:50:30 -080059
60 case GLSL_TYPE_ARRAY:
61 if ((b->base_type != GLSL_TYPE_ARRAY)
62 || (a->length != b->length))
63 return -1;
64
65 /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
66 * "There are no implicit array or structure conversions."
67 *
68 * If the comparison of the array element types detects that a conversion
69 * would be required, the array types do not match.
70 */
71 return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
72
73 case GLSL_TYPE_FUNCTION:
74 case GLSL_TYPE_VOID:
75 case GLSL_TYPE_ERROR:
76 default:
77 /* These are all error conditions. It is invalid for a parameter to
78 * a function to be declared as error, void, or a function.
79 */
80 return -1;
81 }
82
83 /* This point should be unreachable.
84 */
85 assert(0);
86}
87
88
89static int
Ian Romanickb95897b2010-07-15 13:09:25 -070090parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
Ian Romanick471471f2010-03-11 14:50:30 -080091{
Ian Romanickb95897b2010-07-15 13:09:25 -070092 const exec_node *node_a = list_a->head;
93 const exec_node *node_b = list_b->head;
Ian Romanick471471f2010-03-11 14:50:30 -080094 int total_score = 0;
95
Ian Romanickb95897b2010-07-15 13:09:25 -070096 for (/* empty */
Eric Anholt62c47632010-07-29 13:52:25 -070097 ; !node_a->is_tail_sentinel()
Ian Romanickb95897b2010-07-15 13:09:25 -070098 ; node_a = node_a->next, node_b = node_b->next) {
Ian Romanick471471f2010-03-11 14:50:30 -080099 /* If all of the parameters from the other parameter list have been
100 * exhausted, the lists have different length and, by definition,
101 * do not match.
102 */
Eric Anholt62c47632010-07-29 13:52:25 -0700103 if (node_b->is_tail_sentinel())
Ian Romanick471471f2010-03-11 14:50:30 -0800104 return -1;
105
106
Ian Romanickb95897b2010-07-15 13:09:25 -0700107 const ir_variable *const param = (ir_variable *) node_a;
108 const ir_instruction *const actual = (ir_instruction *) node_b;
Ian Romanick471471f2010-03-11 14:50:30 -0800109
110 /* Determine whether or not the types match. If the types are an
111 * exact match, the match score is zero. If the types don't match
112 * but the actual parameter can be coerced to the type of the declared
113 * parameter, the match score is one.
114 */
115 int score;
116 switch ((enum ir_variable_mode)(param->mode)) {
117 case ir_var_auto:
118 case ir_var_uniform:
Ian Romanick7e2aa912010-07-19 17:12:42 -0700119 case ir_var_temporary:
Ian Romanick471471f2010-03-11 14:50:30 -0800120 /* These are all error conditions. It is invalid for a parameter to
121 * a function to be declared as auto (not in, out, or inout) or
122 * as uniform.
123 */
124 assert(0);
125 return -1;
126
127 case ir_var_in:
128 score = type_compare(param->type, actual->type);
129 break;
130
131 case ir_var_out:
Ian Romanick471471f2010-03-11 14:50:30 -0800132 score = type_compare(actual->type, param->type);
133 break;
134
135 case ir_var_inout:
Ian Romanick471471f2010-03-11 14:50:30 -0800136 /* Since there are no bi-directional automatic conversions (e.g.,
137 * there is int -> float but no float -> int), inout parameters must
138 * be exact matches.
139 */
140 score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
141 break;
142 }
143
144 if (score < 0)
145 return -1;
146
147 total_score += score;
148 }
149
150 /* If all of the parameters from the other parameter list have been
151 * exhausted, the lists have different length and, by definition, do not
152 * match.
153 */
Eric Anholt62c47632010-07-29 13:52:25 -0700154 if (!node_b->is_tail_sentinel())
Ian Romanick471471f2010-03-11 14:50:30 -0800155 return -1;
156
157 return total_score;
158}
159
160
Ian Romanick11fc7be2010-07-12 18:35:20 -0700161ir_function_signature *
Ian Romanickb95897b2010-07-15 13:09:25 -0700162ir_function::matching_signature(const exec_list *actual_parameters)
Ian Romanick471471f2010-03-11 14:50:30 -0800163{
164 ir_function_signature *match = NULL;
165
166 foreach_iter(exec_list_iterator, iter, signatures) {
167 ir_function_signature *const sig =
168 (ir_function_signature *) iter.get();
169
170 const int score = parameter_lists_match(& sig->parameters,
171 actual_parameters);
172
173 if (score == 0)
174 return sig;
175
176 if (score > 0) {
177 if (match != NULL)
178 return NULL;
179
180 match = sig;
181 }
182 }
183
184 return match;
185}
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700186
187
188static bool
Ian Romanickb95897b2010-07-15 13:09:25 -0700189parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700190{
Ian Romanickb95897b2010-07-15 13:09:25 -0700191 const exec_node *node_a = list_a->head;
192 const exec_node *node_b = list_b->head;
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700193
Ian Romanickb95897b2010-07-15 13:09:25 -0700194 for (/* empty */
Eric Anholt62c47632010-07-29 13:52:25 -0700195 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
Ian Romanickb95897b2010-07-15 13:09:25 -0700196 ; node_a = node_a->next, node_b = node_b->next) {
197 ir_variable *a = (ir_variable *) node_a;
198 ir_variable *b = (ir_variable *) node_b;
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700199
200 /* If the types of the parameters do not match, the parameters lists
201 * are different.
202 */
203 if (a->type != b->type)
204 return false;
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700205 }
206
207 /* Unless both lists are exhausted, they differ in length and, by
208 * definition, do not match.
209 */
Eric Anholt62c47632010-07-29 13:52:25 -0700210 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700211}
212
213ir_function_signature *
Ian Romanickb95897b2010-07-15 13:09:25 -0700214ir_function::exact_matching_signature(const exec_list *actual_parameters)
Kenneth Graunke0d605cb2010-04-28 12:04:23 -0700215{
216 foreach_iter(exec_list_iterator, iter, signatures) {
217 ir_function_signature *const sig =
218 (ir_function_signature *) iter.get();
219
220 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
221 return sig;
222 }
223 return NULL;
224}