chmod a-x
[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 "imports.h"
32 #include "slang_compile.h"
33
34 /* slang_fixup_table */
35
36 void
37 slang_fixup_table_init(slang_fixup_table * fix)
38 {
39 fix->table = NULL;
40 fix->count = 0;
41 }
42
43 void
44 slang_fixup_table_free(slang_fixup_table * fix)
45 {
46 slang_alloc_free(fix->table);
47 slang_fixup_table_init(fix);
48 }
49
50 /**
51 * Add a new fixup address to the table.
52 */
53 GLboolean
54 slang_fixup_save(slang_fixup_table *fixups, GLuint address)
55 {
56 fixups->table = (GLuint *)
57 slang_alloc_realloc(fixups->table,
58 fixups->count * sizeof(GLuint),
59 (fixups->count + 1) * sizeof(GLuint));
60 if (fixups->table == NULL)
61 return GL_FALSE;
62 fixups->table[fixups->count] = address;
63 fixups->count++;
64 return GL_TRUE;
65 }
66
67
68
69 /* slang_function */
70
71 int
72 slang_function_construct(slang_function * func)
73 {
74 func->kind = slang_func_ordinary;
75 if (!slang_variable_construct(&func->header))
76 return 0;
77
78 func->parameters = (slang_variable_scope *)
79 slang_alloc_malloc(sizeof(slang_variable_scope));
80 if (func->parameters == NULL) {
81 slang_variable_destruct(&func->header);
82 return 0;
83 }
84
85 _slang_variable_scope_ctr(func->parameters);
86 func->param_count = 0;
87 func->body = NULL;
88 func->address = ~0;
89 func->end_label = 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_alloc_free(func->parameters);
100 if (func->body != NULL) {
101 slang_operation_destruct(func->body);
102 slang_alloc_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_alloc_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 }
217
218 /*
219 * _slang_build_export_code_table()
220 */
221
222 GLboolean
223 _slang_build_export_code_table(slang_export_code_table * tbl,
224 slang_function_scope * funs,
225 slang_code_unit * unit)
226 {
227 slang_atom mainAtom;
228 GLuint i;
229
230 mainAtom = slang_atom_pool_atom(tbl->atoms, "main");
231 if (mainAtom == SLANG_ATOM_NULL)
232 return GL_FALSE;
233
234 for (i = 0; i < funs->num_functions; i++) {
235 if (funs->functions[i].header.a_name == mainAtom) {
236 slang_function *fun = &funs->functions[i];
237 slang_export_code_entry *e;
238 slang_assemble_ctx A;
239
240 e = slang_export_code_table_add(tbl);
241 if (e == NULL)
242 return GL_FALSE;
243 e->address = unit->object->assembly.count;
244 e->name = slang_atom_pool_atom(tbl->atoms, "@main");
245 if (e->name == SLANG_ATOM_NULL)
246 return GL_FALSE;
247
248 A.file = &unit->object->assembly;
249 A.mach = &unit->object->machine;
250 A.atoms = &unit->object->atompool;
251 A.space.funcs = &unit->funs;
252 A.space.structs = &unit->structs;
253 A.space.vars = &unit->vars;
254 slang_assembly_file_push_label(&unit->object->assembly,
255 slang_asm_local_alloc, 20);
256 slang_assembly_file_push_label(&unit->object->assembly,
257 slang_asm_enter, 20);
258 _slang_assemble_function_call(&A, fun, NULL, 0, GL_FALSE);
259 slang_assembly_file_push(&unit->object->assembly, slang_asm_exit);
260 }
261 }
262 return GL_TRUE;
263 }