move fix-up code into new slang_fixup_save() function
[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 slang_fixup_table_init(&func->fixups);
90 return 1;
91 }
92
93 void
94 slang_function_destruct(slang_function * func)
95 {
96 slang_variable_destruct(&func->header);
97 slang_variable_scope_destruct(func->parameters);
98 slang_alloc_free(func->parameters);
99 if (func->body != NULL) {
100 slang_operation_destruct(func->body);
101 slang_alloc_free(func->body);
102 }
103 slang_fixup_table_free(&func->fixups);
104 }
105
106 /*
107 * slang_function_scope
108 */
109
110 GLvoid
111 _slang_function_scope_ctr(slang_function_scope * self)
112 {
113 self->functions = NULL;
114 self->num_functions = 0;
115 self->outer_scope = NULL;
116 }
117
118 void
119 slang_function_scope_destruct(slang_function_scope * scope)
120 {
121 unsigned int i;
122
123 for (i = 0; i < scope->num_functions; i++)
124 slang_function_destruct(scope->functions + i);
125 slang_alloc_free(scope->functions);
126 }
127
128
129 /**
130 * Search a list of functions for a particular function by name.
131 * \param funcs the list of functions to search
132 * \param a_name the name to search for
133 * \param all_scopes if non-zero, search containing scopes too.
134 * \return pointer to found function, or NULL.
135 */
136 int
137 slang_function_scope_find_by_name(slang_function_scope * funcs,
138 slang_atom a_name, int all_scopes)
139 {
140 unsigned int i;
141
142 for (i = 0; i < funcs->num_functions; i++)
143 if (a_name == funcs->functions[i].header.a_name)
144 return 1;
145 if (all_scopes && funcs->outer_scope != NULL)
146 return slang_function_scope_find_by_name(funcs->outer_scope, a_name, 1);
147 return 0;
148 }
149
150
151 /**
152 * Search a list of functions for a particular function (for implementing
153 * function calls. Matching is done by first comparing the function's name,
154 * then the function's parameter list.
155 *
156 * \param funcs the list of functions to search
157 * \param fun the function to search for
158 * \param all_scopes if non-zero, search containing scopes too.
159 * \return pointer to found function, or NULL.
160 */
161 slang_function *
162 slang_function_scope_find(slang_function_scope * funcs, slang_function * fun,
163 int all_scopes)
164 {
165 unsigned int i;
166
167 for (i = 0; i < funcs->num_functions; i++) {
168 slang_function *f = &funcs->functions[i];
169 unsigned int j;
170
171 if (fun->header.a_name != f->header.a_name)
172 continue;
173 if (fun->param_count != f->param_count)
174 continue;
175 for (j = 0; j < fun->param_count; j++) {
176 if (!slang_type_specifier_equal
177 (&fun->parameters->variables[j].type.specifier,
178 &f->parameters->variables[j].type.specifier))
179 break;
180 }
181 if (j == fun->param_count)
182 return f;
183 }
184 if (all_scopes && funcs->outer_scope != NULL)
185 return slang_function_scope_find(funcs->outer_scope, fun, 1);
186 return NULL;
187 }
188
189 /*
190 * _slang_build_export_code_table()
191 */
192
193 GLboolean
194 _slang_build_export_code_table(slang_export_code_table * tbl,
195 slang_function_scope * funs,
196 slang_code_unit * unit)
197 {
198 slang_atom mainAtom;
199 GLuint i;
200
201 mainAtom = slang_atom_pool_atom(tbl->atoms, "main");
202 if (mainAtom == SLANG_ATOM_NULL)
203 return GL_FALSE;
204
205 for (i = 0; i < funs->num_functions; i++) {
206 if (funs->functions[i].header.a_name == mainAtom) {
207 slang_function *fun = &funs->functions[i];
208 slang_export_code_entry *e;
209 slang_assemble_ctx A;
210
211 e = slang_export_code_table_add(tbl);
212 if (e == NULL)
213 return GL_FALSE;
214 e->address = unit->object->assembly.count;
215 e->name = slang_atom_pool_atom(tbl->atoms, "@main");
216 if (e->name == SLANG_ATOM_NULL)
217 return GL_FALSE;
218
219 A.file = &unit->object->assembly;
220 A.mach = &unit->object->machine;
221 A.atoms = &unit->object->atompool;
222 A.space.funcs = &unit->funs;
223 A.space.structs = &unit->structs;
224 A.space.vars = &unit->vars;
225 slang_assembly_file_push_label(&unit->object->assembly,
226 slang_asm_local_alloc, 20);
227 slang_assembly_file_push_label(&unit->object->assembly,
228 slang_asm_enter, 20);
229 _slang_assemble_function_call(&A, fun, NULL, 0, GL_FALSE);
230 slang_assembly_file_push(&unit->object->assembly, slang_asm_exit);
231 }
232 }
233 return GL_TRUE;
234 }