r600g: add POW instruction
[mesa.git] / src / mesa / slang / slang_compile_function.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_compile_function.c
27 * slang front-end compiler
28 * \author Michal Krol
29 */
30
31 #include "main/imports.h"
32 #include "slang_compile.h"
33 #include "slang_mem.h"
34
35
36 int
37 slang_function_construct(slang_function * func)
38 {
39 func->kind = SLANG_FUNC_ORDINARY;
40 if (!slang_variable_construct(&func->header))
41 return 0;
42
43 func->parameters = (slang_variable_scope *)
44 _slang_alloc(sizeof(slang_variable_scope));
45 if (func->parameters == NULL) {
46 slang_variable_destruct(&func->header);
47 return 0;
48 }
49
50 _slang_variable_scope_ctr(func->parameters);
51 func->param_count = 0;
52 func->body = NULL;
53 return 1;
54 }
55
56 void
57 slang_function_destruct(slang_function * func)
58 {
59 slang_variable_destruct(&func->header);
60 slang_variable_scope_destruct(func->parameters);
61 _slang_free(func->parameters);
62 if (func->body != NULL) {
63 slang_operation_destruct(func->body);
64 _slang_free(func->body);
65 }
66 }
67
68
69 slang_function *
70 slang_function_new(slang_function_kind kind)
71 {
72 slang_function *fun = (slang_function *)
73 _slang_alloc(sizeof(slang_function));
74 if (fun) {
75 slang_function_construct(fun);
76 fun->kind = kind;
77 }
78 return fun;
79 }
80
81
82 /*
83 * slang_function_scope
84 */
85
86 GLvoid
87 _slang_function_scope_ctr(slang_function_scope * self)
88 {
89 self->functions = NULL;
90 self->num_functions = 0;
91 self->outer_scope = NULL;
92 }
93
94 void
95 slang_function_scope_destruct(slang_function_scope * scope)
96 {
97 unsigned int i;
98
99 for (i = 0; i < scope->num_functions; i++)
100 slang_function_destruct(scope->functions + i);
101 _slang_free(scope->functions);
102 }
103
104
105 /**
106 * Does this function have a non-void return value?
107 */
108 GLboolean
109 _slang_function_has_return_value(const slang_function *fun)
110 {
111 return fun->header.type.specifier.type != SLANG_SPEC_VOID;
112 }
113
114
115 /**
116 * Search a list of functions for a particular function by name.
117 * \param funcs the list of functions to search
118 * \param a_name the name to search for
119 * \param all_scopes if non-zero, search containing scopes too.
120 * \return pointer to found function, or NULL.
121 */
122 int
123 slang_function_scope_find_by_name(slang_function_scope * funcs,
124 slang_atom a_name, int all_scopes)
125 {
126 unsigned int i;
127
128 for (i = 0; i < funcs->num_functions; i++)
129 if (a_name == funcs->functions[i].header.a_name)
130 return 1;
131 if (all_scopes && funcs->outer_scope != NULL)
132 return slang_function_scope_find_by_name(funcs->outer_scope, a_name, 1);
133 return 0;
134 }
135
136
137 /**
138 * Search a list of functions for a particular function (for implementing
139 * function calls. Matching is done by first comparing the function's name,
140 * then the function's parameter list.
141 *
142 * \param funcs the list of functions to search
143 * \param fun the function to search for
144 * \param all_scopes if non-zero, search containing scopes too.
145 * \return pointer to found function, or NULL.
146 */
147 slang_function *
148 slang_function_scope_find(slang_function_scope * funcs, slang_function * fun,
149 int all_scopes)
150 {
151 unsigned int i;
152
153 for (i = 0; i < funcs->num_functions; i++) {
154 slang_function *f = &funcs->functions[i];
155 const GLuint haveRetValue = 0;
156 #if 0
157 = (f->header.type.specifier.type != SLANG_SPEC_VOID);
158 #endif
159 unsigned int j;
160
161 /*
162 printf("Compare name %s to %s (ret %u, %d, %d)\n",
163 (char *) fun->header.a_name, (char *) f->header.a_name,
164 haveRetValue,
165 fun->param_count, f->param_count);
166 */
167
168 if (fun->header.a_name != f->header.a_name)
169 continue;
170 if (fun->param_count != f->param_count)
171 continue;
172 for (j = haveRetValue; j < fun->param_count; j++) {
173 if (!slang_type_specifier_equal
174 (&fun->parameters->variables[j]->type.specifier,
175 &f->parameters->variables[j]->type.specifier))
176 break;
177 }
178 if (j == fun->param_count) {
179 /*
180 printf("Found match\n");
181 */
182 return f;
183 }
184 }
185 /*
186 printf("Not found\n");
187 */
188 if (all_scopes && funcs->outer_scope != NULL)
189 return slang_function_scope_find(funcs->outer_scope, fun, 1);
190 return NULL;
191 }
192
193
194 /**
195 * Lookup a function according to name and parameter count/types.
196 */
197 slang_function *
198 _slang_function_locate(const slang_function_scope * funcs, slang_atom a_name,
199 slang_operation * args, GLuint num_args,
200 const slang_name_space * space, slang_atom_pool * atoms,
201 slang_info_log *log, GLboolean *error)
202 {
203 slang_typeinfo arg_ti[100];
204 GLuint i;
205
206 *error = GL_FALSE;
207
208 /* determine type of each argument */
209 assert(num_args < 100);
210 for (i = 0; i < num_args; i++) {
211 if (!slang_typeinfo_construct(&arg_ti[i]))
212 return NULL;
213 if (!_slang_typeof_operation(&args[i], space, &arg_ti[i], atoms, log)) {
214 return NULL;
215 }
216 }
217
218 /* loop over function scopes */
219 while (funcs) {
220
221 /* look for function with matching name and argument/param types */
222 for (i = 0; i < funcs->num_functions; i++) {
223 slang_function *f = &funcs->functions[i];
224 const GLuint haveRetValue = _slang_function_has_return_value(f);
225 GLuint j;
226
227 if (a_name != f->header.a_name)
228 continue;
229 if (f->param_count - haveRetValue != num_args)
230 continue;
231
232 /* compare parameter / argument types */
233 for (j = 0; j < num_args; j++) {
234 if (!slang_type_specifier_compatible(&arg_ti[j].spec,
235 &f->parameters->variables[j]->type.specifier)) {
236 /* param/arg types don't match */
237 break;
238 }
239
240 /* "out" and "inout" formal parameter requires the actual
241 * argument to be an l-value.
242 */
243 if (!arg_ti[j].can_be_referenced &&
244 (f->parameters->variables[j]->type.qualifier == SLANG_QUAL_OUT ||
245 f->parameters->variables[j]->type.qualifier == SLANG_QUAL_INOUT)) {
246 /* param is not an lvalue! */
247 *error = GL_TRUE;
248 return NULL;
249 }
250 }
251
252 if (j == num_args) {
253 /* name and args match! */
254 return f;
255 }
256 }
257
258 funcs = funcs->outer_scope;
259 }
260
261 return NULL;
262 }