mesa: Prefix main includes with dir to avoid conflicts.
[mesa.git] / src / mesa / shader / 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 /* slang_fixup_table */
36
37 void
38 slang_fixup_table_init(slang_fixup_table * fix)
39 {
40 fix->table = NULL;
41 fix->count = 0;
42 }
43
44 void
45 slang_fixup_table_free(slang_fixup_table * fix)
46 {
47 _slang_free(fix->table);
48 slang_fixup_table_init(fix);
49 }
50
51 /**
52 * Add a new fixup address to the table.
53 */
54 GLboolean
55 slang_fixup_save(slang_fixup_table *fixups, GLuint address)
56 {
57 fixups->table = (GLuint *)
58 _slang_realloc(fixups->table,
59 fixups->count * sizeof(GLuint),
60 (fixups->count + 1) * sizeof(GLuint));
61 if (fixups->table == NULL)
62 return GL_FALSE;
63 fixups->table[fixups->count] = address;
64 fixups->count++;
65 return GL_TRUE;
66 }
67
68
69
70 /* slang_function */
71
72 int
73 slang_function_construct(slang_function * func)
74 {
75 func->kind = SLANG_FUNC_ORDINARY;
76 if (!slang_variable_construct(&func->header))
77 return 0;
78
79 func->parameters = (slang_variable_scope *)
80 _slang_alloc(sizeof(slang_variable_scope));
81 if (func->parameters == NULL) {
82 slang_variable_destruct(&func->header);
83 return 0;
84 }
85
86 _slang_variable_scope_ctr(func->parameters);
87 func->param_count = 0;
88 func->body = NULL;
89 func->address = ~0;
90 slang_fixup_table_init(&func->fixups);
91 return 1;
92 }
93
94 void
95 slang_function_destruct(slang_function * func)
96 {
97 slang_variable_destruct(&func->header);
98 slang_variable_scope_destruct(func->parameters);
99 _slang_free(func->parameters);
100 if (func->body != NULL) {
101 slang_operation_destruct(func->body);
102 _slang_free(func->body);
103 }
104 slang_fixup_table_free(&func->fixups);
105 }
106
107 /*
108 * slang_function_scope
109 */
110
111 GLvoid
112 _slang_function_scope_ctr(slang_function_scope * self)
113 {
114 self->functions = NULL;
115 self->num_functions = 0;
116 self->outer_scope = NULL;
117 }
118
119 void
120 slang_function_scope_destruct(slang_function_scope * scope)
121 {
122 unsigned int i;
123
124 for (i = 0; i < scope->num_functions; i++)
125 slang_function_destruct(scope->functions + i);
126 _slang_free(scope->functions);
127 }
128
129
130 /**
131 * Does this function have a non-void return value?
132 */
133 GLboolean
134 _slang_function_has_return_value(const slang_function *fun)
135 {
136 return fun->header.type.specifier.type != SLANG_SPEC_VOID;
137 }
138
139
140 /**
141 * Search a list of functions for a particular function by name.
142 * \param funcs the list of functions to search
143 * \param a_name the name to search for
144 * \param all_scopes if non-zero, search containing scopes too.
145 * \return pointer to found function, or NULL.
146 */
147 int
148 slang_function_scope_find_by_name(slang_function_scope * funcs,
149 slang_atom a_name, int all_scopes)
150 {
151 unsigned int i;
152
153 for (i = 0; i < funcs->num_functions; i++)
154 if (a_name == funcs->functions[i].header.a_name)
155 return 1;
156 if (all_scopes && funcs->outer_scope != NULL)
157 return slang_function_scope_find_by_name(funcs->outer_scope, a_name, 1);
158 return 0;
159 }
160
161
162 /**
163 * Search a list of functions for a particular function (for implementing
164 * function calls. Matching is done by first comparing the function's name,
165 * then the function's parameter list.
166 *
167 * \param funcs the list of functions to search
168 * \param fun the function to search for
169 * \param all_scopes if non-zero, search containing scopes too.
170 * \return pointer to found function, or NULL.
171 */
172 slang_function *
173 slang_function_scope_find(slang_function_scope * funcs, slang_function * fun,
174 int all_scopes)
175 {
176 unsigned int i;
177
178 for (i = 0; i < funcs->num_functions; i++) {
179 slang_function *f = &funcs->functions[i];
180 const GLuint haveRetValue = 0;
181 #if 0
182 = (f->header.type.specifier.type != SLANG_SPEC_VOID);
183 #endif
184 unsigned int j;
185
186 /*
187 printf("Compare name %s to %s (ret %u, %d, %d)\n",
188 (char *) fun->header.a_name, (char *) f->header.a_name,
189 haveRetValue,
190 fun->param_count, f->param_count);
191 */
192
193 if (fun->header.a_name != f->header.a_name)
194 continue;
195 if (fun->param_count != f->param_count)
196 continue;
197 for (j = haveRetValue; j < fun->param_count; j++) {
198 if (!slang_type_specifier_equal
199 (&fun->parameters->variables[j]->type.specifier,
200 &f->parameters->variables[j]->type.specifier))
201 break;
202 }
203 if (j == fun->param_count) {
204 /*
205 printf("Found match\n");
206 */
207 return f;
208 }
209 }
210 /*
211 printf("Not found\n");
212 */
213 if (all_scopes && funcs->outer_scope != NULL)
214 return slang_function_scope_find(funcs->outer_scope, fun, 1);
215 return NULL;
216 }