linker: look up function signatures during linking instead of using callee
[mesa.git] / src / glsl / link_functions.cpp
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 <cstdlib>
25 #include <cstdio>
26 #include <cstdarg>
27
28 extern "C" {
29 #include <talloc.h>
30 }
31
32 #include "main/mtypes.h"
33 #include "glsl_symbol_table.h"
34 #include "glsl_parser_extras.h"
35 #include "ir.h"
36 #include "program.h"
37 #include "hash_table.h"
38 #include "linker.h"
39
40 static ir_function_signature *
41 find_matching_signature(const char *name, const exec_list *actual_parameters,
42 gl_shader **shader_list, unsigned num_shaders);
43
44 class call_link_visitor : public ir_hierarchical_visitor {
45 public:
46 call_link_visitor(gl_shader_program *prog, gl_shader *linked,
47 gl_shader **shader_list, unsigned num_shaders)
48 {
49 this->prog = prog;
50 this->shader_list = shader_list;
51 this->num_shaders = num_shaders;
52 this->success = true;
53 this->linked = linked;
54 }
55
56 virtual ir_visitor_status visit_enter(ir_call *ir)
57 {
58 /* If ir is an ir_call from a function that was imported from another
59 * shader callee will point to an ir_function_signature in the original
60 * shader. In this case the function signature MUST NOT BE MODIFIED.
61 * Doing so will modify the original shader. This may prevent that
62 * shader from being linkable in other programs.
63 */
64 const ir_function_signature *const callee = ir->get_callee();
65 assert(callee != NULL);
66 const char *const name = callee->function_name();
67
68 /* Determine if the requested function signature already exists in the
69 * final linked shader. If it does, use it as the target of the call.
70 */
71 ir_function_signature *sig =
72 find_matching_signature(name, &callee->parameters, &linked, 1);
73 if (sig != NULL) {
74 ir->set_callee(sig);
75 return visit_continue;
76 }
77
78 /* Try to find the signature in one of the other shaders that is being
79 * linked. If it's not found there, return an error.
80 */
81 sig = find_matching_signature(name, &ir->actual_parameters, shader_list,
82 num_shaders);
83 if (sig == NULL) {
84 /* FINISHME: Log the full signature of unresolved function.
85 */
86 linker_error_printf(this->prog, "unresolved reference to function "
87 "`%s'\n", name);
88 this->success = false;
89 return visit_stop;
90 }
91
92 /* Find the prototype information in the linked shader. Generate any
93 * details that may be missing.
94 */
95 ir_function *f = linked->symbols->get_function(name);
96 if (f == NULL)
97 f = new(linked) ir_function(name);
98
99 ir_function_signature *linked_sig =
100 f->matching_signature(&callee->parameters);
101 if (linked_sig == NULL) {
102 linked_sig = new(linked) ir_function_signature(callee->return_type);
103 f->add_signature(linked_sig);
104 }
105
106 /* At this point linked_sig and called may be the same. If ir is an
107 * ir_call from linked then linked_sig and callee will be
108 * ir_function_signatures that have no definitions (is_defined is false).
109 */
110 assert(!linked_sig->is_defined);
111 assert(linked_sig->body.is_empty());
112
113 /* Create an in-place clone of the function definition. This multistep
114 * process introduces some complexity here, but it has some advantages.
115 * The parameter list and the and function body are cloned separately.
116 * The clone of the parameter list is used to prime the hashtable used
117 * to replace variable references in the cloned body.
118 *
119 * The big advantage is that the ir_function_signature does not change.
120 * This means that we don't have to process the rest of the IR tree to
121 * patch ir_call nodes. In addition, there is no way to remove or
122 * replace signature stored in a function. One could easily be added,
123 * but this avoids the need.
124 */
125 struct hash_table *ht = hash_table_ctor(0, hash_table_pointer_hash,
126 hash_table_pointer_compare);
127 exec_list formal_parameters;
128 foreach_list_const(node, &sig->parameters) {
129 const ir_instruction *const original = (ir_instruction *) node;
130 assert(const_cast<ir_instruction *>(original)->as_variable());
131
132 ir_instruction *copy = original->clone(ht);
133 formal_parameters.push_tail(copy);
134 }
135
136 linked_sig->replace_parameters(&formal_parameters);
137
138 foreach_list_const(node, &sig->body) {
139 const ir_instruction *const original = (ir_instruction *) node;
140
141 ir_instruction *copy = original->clone(ht);
142 linked_sig->body.push_tail(copy);
143 }
144
145 linked_sig->is_defined = true;
146
147 /* FINISHME: Patch references inside the function to things outside the
148 * FINISHME: function (i.e., function calls and global variables).
149 */
150
151 hash_table_dtor(ht);
152
153 return visit_continue;
154 }
155
156 /** Was function linking successful? */
157 bool success;
158
159 private:
160 /**
161 * Shader program being linked
162 *
163 * This is only used for logging error messages.
164 */
165 gl_shader_program *prog;
166
167 /** List of shaders available for linking. */
168 gl_shader **shader_list;
169
170 /** Number of shaders available for linking. */
171 unsigned num_shaders;
172
173 /**
174 * Final linked shader
175 *
176 * This is used two ways. It is used to find global variables in the
177 * linked shader that are accessed by the function. It is also used to add
178 * global variables from the shader where the function originated.
179 */
180 gl_shader *linked;
181 };
182
183
184 /**
185 * Searches a list of shaders for a particular function definition
186 */
187 ir_function_signature *
188 find_matching_signature(const char *name, const exec_list *actual_parameters,
189 gl_shader **shader_list, unsigned num_shaders)
190 {
191 for (unsigned i = 0; i < num_shaders; i++) {
192 ir_function *const f = shader_list[i]->symbols->get_function(name);
193
194 if (f == NULL)
195 continue;
196
197 ir_function_signature *sig = f->matching_signature(actual_parameters);
198
199 if ((sig == NULL) || !sig->is_defined)
200 continue;
201
202 return sig;
203 }
204
205 return NULL;
206 }
207
208
209 bool
210 link_function_calls(gl_shader_program *prog, gl_shader *main,
211 gl_shader **shader_list, unsigned num_shaders)
212 {
213 call_link_visitor v(prog, main, shader_list, num_shaders);
214
215 v.run(main->ir);
216 return v.success;
217 }