Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 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 | |
| 27 | int |
| 28 | type_compare(const glsl_type *a, const glsl_type *b) |
| 29 | { |
Ian Romanick | 80b5ed6 | 2010-03-25 13:05:43 -0700 | [diff] [blame] | 30 | /* If the types are the same, they trivially match. |
| 31 | */ |
| 32 | if (a == b) |
| 33 | return 0; |
| 34 | |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 35 | switch (a->base_type) { |
| 36 | case GLSL_TYPE_UINT: |
| 37 | case GLSL_TYPE_INT: |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 38 | case GLSL_TYPE_BOOL: |
Ian Romanick | fe1c7ff | 2010-04-02 11:45:06 -0700 | [diff] [blame] | 39 | /* 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 Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 43 | return -1; |
| 44 | |
Ian Romanick | fe1c7ff | 2010-04-02 11:45:06 -0700 | [diff] [blame] | 45 | /* FALLTHROUGH */ |
| 46 | |
| 47 | case GLSL_TYPE_FLOAT: |
| 48 | if ((a->vector_elements != b->vector_elements) |
| 49 | || (a->matrix_columns != b->matrix_columns)) |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 50 | return -1; |
| 51 | |
| 52 | return 1; |
| 53 | |
| 54 | case GLSL_TYPE_SAMPLER: |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 55 | case GLSL_TYPE_STRUCT: |
Ian Romanick | 80b5ed6 | 2010-03-25 13:05:43 -0700 | [diff] [blame] | 56 | /* Samplers and structures must match exactly. |
| 57 | */ |
| 58 | return -1; |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 59 | |
| 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 | |
| 89 | static int |
| 90 | parameter_lists_match(exec_list *list_a, exec_list *list_b) |
| 91 | { |
| 92 | exec_list_iterator iter_a = list_a->iterator(); |
| 93 | exec_list_iterator iter_b = list_b->iterator(); |
| 94 | int total_score = 0; |
| 95 | |
| 96 | for (/* empty */ ; iter_a.has_next(); iter_a.next(), iter_b.next()) { |
| 97 | /* If all of the parameters from the other parameter list have been |
| 98 | * exhausted, the lists have different length and, by definition, |
| 99 | * do not match. |
| 100 | */ |
| 101 | if (!iter_b.has_next()) |
| 102 | return -1; |
| 103 | |
| 104 | |
| 105 | const ir_variable *const param = (ir_variable *) iter_a.get(); |
| 106 | const ir_instruction *const actual = (ir_instruction *) iter_b.get(); |
| 107 | |
| 108 | /* Determine whether or not the types match. If the types are an |
| 109 | * exact match, the match score is zero. If the types don't match |
| 110 | * but the actual parameter can be coerced to the type of the declared |
| 111 | * parameter, the match score is one. |
| 112 | */ |
| 113 | int score; |
| 114 | switch ((enum ir_variable_mode)(param->mode)) { |
| 115 | case ir_var_auto: |
| 116 | case ir_var_uniform: |
| 117 | /* These are all error conditions. It is invalid for a parameter to |
| 118 | * a function to be declared as auto (not in, out, or inout) or |
| 119 | * as uniform. |
| 120 | */ |
| 121 | assert(0); |
| 122 | return -1; |
| 123 | |
| 124 | case ir_var_in: |
| 125 | score = type_compare(param->type, actual->type); |
| 126 | break; |
| 127 | |
| 128 | case ir_var_out: |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 129 | score = type_compare(actual->type, param->type); |
| 130 | break; |
| 131 | |
| 132 | case ir_var_inout: |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 133 | /* Since there are no bi-directional automatic conversions (e.g., |
| 134 | * there is int -> float but no float -> int), inout parameters must |
| 135 | * be exact matches. |
| 136 | */ |
| 137 | score = (type_compare(actual->type, param->type) == 0) ? 0 : -1; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if (score < 0) |
| 142 | return -1; |
| 143 | |
| 144 | total_score += score; |
| 145 | } |
| 146 | |
| 147 | /* If all of the parameters from the other parameter list have been |
| 148 | * exhausted, the lists have different length and, by definition, do not |
| 149 | * match. |
| 150 | */ |
| 151 | if (iter_b.has_next()) |
| 152 | return -1; |
| 153 | |
| 154 | return total_score; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | const ir_function_signature * |
| 159 | ir_function::matching_signature(exec_list *actual_parameters) |
| 160 | { |
| 161 | ir_function_signature *match = NULL; |
| 162 | |
| 163 | foreach_iter(exec_list_iterator, iter, signatures) { |
| 164 | ir_function_signature *const sig = |
| 165 | (ir_function_signature *) iter.get(); |
| 166 | |
| 167 | const int score = parameter_lists_match(& sig->parameters, |
| 168 | actual_parameters); |
| 169 | |
| 170 | if (score == 0) |
| 171 | return sig; |
| 172 | |
| 173 | if (score > 0) { |
| 174 | if (match != NULL) |
| 175 | return NULL; |
| 176 | |
| 177 | match = sig; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return match; |
| 182 | } |