summaryrefslogtreecommitdiffstats
path: root/src/glsl/ir_function.cpp
blob: ef8d4fcfcd4dbd8a90776d93eb6a66352a2f4480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright © 2010 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "glsl_types.h"
#include "ir.h"

int
type_compare(const glsl_type *a, const glsl_type *b)
{
   /* If the types are the same, they trivially match.
    */
   if (a == b)
      return 0;

   switch (a->base_type) {
   case GLSL_TYPE_UINT:
   case GLSL_TYPE_INT:
   case GLSL_TYPE_BOOL:
      /* There is no implicit conversion to or from integer types or bool.
       */
      if ((a->is_integer() != b->is_integer())
	  || (a->is_boolean() != b->is_boolean()))
	 return -1;

      /* FALLTHROUGH */

   case GLSL_TYPE_FLOAT:
      if ((a->vector_elements != b->vector_elements)
	  || (a->matrix_columns != b->matrix_columns))
	 return -1;

      return 1;

   case GLSL_TYPE_SAMPLER:
   case GLSL_TYPE_STRUCT:
      /* Samplers and structures must match exactly.
       */
      return -1;

   case GLSL_TYPE_ARRAY:
      if ((b->base_type != GLSL_TYPE_ARRAY)
	  || (a->length != b->length))
	 return -1;

      /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
       *    "There are no implicit array or structure conversions."
       *
       * If the comparison of the array element types detects that a conversion
       * would be required, the array types do not match.
       */
      return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;

   case GLSL_TYPE_VOID:
   case GLSL_TYPE_ERROR:
   default:
      /* These are all error conditions.  It is invalid for a parameter to
       * a function to be declared as error, void, or a function.
       */
      return -1;
   }

   /* This point should be unreachable.
    */
   assert(0);
}


static int
parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
{
   const exec_node *node_a = list_a->head;
   const exec_node *node_b = list_b->head;
   int total_score = 0;

   for (/* empty */
	; !node_a->is_tail_sentinel()
	; node_a = node_a->next, node_b = node_b->next) {
      /* If all of the parameters from the other parameter list have been
       * exhausted, the lists have different length and, by definition,
       * do not match.
       */
      if (node_b->is_tail_sentinel())
	 return -1;


      const ir_variable *const param = (ir_variable *) node_a;
      const ir_instruction *const actual = (ir_instruction *) node_b;

      /* Determine whether or not the types match.  If the types are an
       * exact match, the match score is zero.  If the types don't match
       * but the actual parameter can be coerced to the type of the declared
       * parameter, the match score is one.
       */
      int score;
      switch ((enum ir_variable_mode)(param->mode)) {
      case ir_var_auto:
      case ir_var_uniform:
      case ir_var_temporary:
	 /* These are all error conditions.  It is invalid for a parameter to
	  * a function to be declared as auto (not in, out, or inout) or
	  * as uniform.
	  */
	 assert(0);
	 return -1;

      case ir_var_const_in:
      case ir_var_in:
	 score = type_compare(param->type, actual->type);
	 break;

      case ir_var_out:
	 score = type_compare(actual->type, param->type);
	 break;

      case ir_var_inout:
	 /* Since there are no bi-directional automatic conversions (e.g.,
	  * there is int -> float but no float -> int), inout parameters must
	  * be exact matches.
	  */
	 score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
	 break;

      default:
	 assert(false);
      }

      if (score < 0)
	 return -1;

      total_score += score;
   }

   /* If all of the parameters from the other parameter list have been
    * exhausted, the lists have different length and, by definition, do not
    * match.
    */
   if (!node_b->is_tail_sentinel())
      return -1;

   return total_score;
}


ir_function_signature *
ir_function::matching_signature(const exec_list *actual_parameters)
{
   ir_function_signature *match = NULL;
   int matched_score;

   foreach_iter(exec_list_iterator, iter, signatures) {
      ir_function_signature *const sig =
	 (ir_function_signature *) iter.get();

      const int score = parameter_lists_match(& sig->parameters,
					      actual_parameters);

      /* If we found an exact match, simply return it */
      if (score == 0)
	 return sig;

      /* If we found a match with fewer conversions, use that instead */
      if (score > 0 && (match == NULL || score < matched_score)) {
	 match = sig;
	 matched_score = score;
      }
   }

   return match;
}


static bool
parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
{
   const exec_node *node_a = list_a->head;
   const exec_node *node_b = list_b->head;

   for (/* empty */
	; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
	; node_a = node_a->next, node_b = node_b->next) {
      ir_variable *a = (ir_variable *) node_a;
      ir_variable *b = (ir_variable *) node_b;

      /* If the types of the parameters do not match, the parameters lists
       * are different.
       */
      if (a->type != b->type)
         return false;
   }

   /* Unless both lists are exhausted, they differ in length and, by
    * definition, do not match.
    */
   return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
}

ir_function_signature *
ir_function::exact_matching_signature(const exec_list *actual_parameters)
{
   foreach_iter(exec_list_iterator, iter, signatures) {
      ir_function_signature *const sig =
	 (ir_function_signature *) iter.get();

      if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
	 return sig;
   }
   return NULL;
}