blob: 35bd22350d5d725167099f73c62b32f9218c81d5 [file] [log] [blame]
Ian Romanick8fe8a812010-07-13 17:36:13 -07001/*
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
28extern "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
40class call_link_visitor : public ir_hierarchical_visitor {
41public:
42 call_link_visitor(gl_shader_program *prog, gl_shader **shader_list,
43 unsigned num_shaders)
44 {
45 this->prog = prog;
46 this->shader_list = shader_list;
47 this->num_shaders = num_shaders;
48 this->success = true;
49 }
50
51 virtual ir_visitor_status visit_enter(ir_call *ir)
52 {
53 /* If the function call references a function signature that does not
54 * have a definition, try to find the definition in one of the other
55 * shaders.
56 */
57 ir_function_signature *callee =
58 const_cast<ir_function_signature *>(ir->get_callee());
59 assert(callee != NULL);
60
61 if (callee->is_defined)
62 /* FINISHME: Do children need to be processed, or are all parameters
63 * FINISHME: with function calls already flattend?
64 */
65 return visit_continue;
66
67 const char *const name = callee->function_name();
68
69 ir_function_signature *sig = const_cast<ir_function_signature *>
70 (this->find_matching_signature(name, &ir->actual_parameters));
71 if (sig == NULL) {
72 /* FINISHME: Log the full signature of unresolved function.
73 */
74 linker_error_printf(this->prog, "unresolved reference to function "
75 "`%s'\n", name);
76 this->success = false;
77 return visit_stop;
78 }
79
80 /* Create an in-place clone of the function definition. This multistep
81 * process introduces some complexity here, but it has some advantages.
82 * The parameter list and the and function body are cloned separately.
83 * The clone of the parameter list is used to prime the hashtable used
84 * to replace variable references in the cloned body.
85 *
86 * The big advantage is that the ir_function_signature does not change.
87 * This means that we don't have to process the rest of the IR tree to
88 * patch ir_call nodes. In addition, there is no way to remove or replace
89 * signature stored in a function. One could easily be added, but this
90 * avoids the need.
91 */
92 struct hash_table *ht = hash_table_ctor(0, hash_table_pointer_hash,
93 hash_table_pointer_compare);
94 exec_list formal_parameters;
95 foreach_list_const(node, &sig->parameters) {
96 const ir_instruction *const original = (ir_instruction *) node;
97 assert(const_cast<ir_instruction *>(original)->as_variable());
98
99 ir_instruction *copy = original->clone(ht);
100 formal_parameters.push_tail(copy);
101 }
102
103 callee->replace_parameters(&formal_parameters);
104
105 assert(callee->body.is_empty());
106 foreach_list_const(node, &sig->body) {
107 const ir_instruction *const original = (ir_instruction *) node;
108
109 ir_instruction *copy = original->clone(ht);
110 callee->body.push_tail(copy);
111 }
112
113 callee->is_defined = true;
114
115 /* FINISHME: Patch references inside the function to things outside the
116 * FINISHME: function (i.e., function calls and global variables).
117 */
118
119 hash_table_dtor(ht);
120
121 return visit_continue;
122 }
123
124 /** Was function linking successful? */
125 bool success;
126
127private:
128 /**
129 * Shader program being linked
130 *
131 * This is only used for logging error messages.
132 */
133 gl_shader_program *prog;
134
135 /** List of shaders available for linking. */
136 gl_shader **shader_list;
137
138 /** Number of shaders available for linking. */
139 unsigned num_shaders;
140
141 /**
142 * Searches all shaders for a particular function definition
143 */
144 const ir_function_signature *
145 find_matching_signature(const char *name, exec_list *actual_parameters)
146 {
147 for (unsigned i = 0; i < this->num_shaders; i++) {
148 ir_function *const f =
149 this->shader_list[i]->symbols->get_function(name);
150
151 if (f == NULL)
152 continue;
153
154 const ir_function_signature *sig =
155 f->matching_signature(actual_parameters);
156
157 if ((sig == NULL) || !sig->is_defined)
158 continue;
159
160 return sig;
161 }
162
163 return NULL;
164 }
165};
166
167
168bool
169link_function_calls(gl_shader_program *prog, gl_shader *main,
170 gl_shader **shader_list, unsigned num_shaders)
171{
172 call_link_visitor v(prog, shader_list, num_shaders);
173
174 v.run(main->ir);
175 return v.success;
176}