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