glsl: regenerated file
[mesa.git] / src / mesa / shader / slang / slang_compile_operation.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
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_operation.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 /**
37 * Init a slang_operation object
38 */
39 GLboolean
40 slang_operation_construct(slang_operation * oper)
41 {
42 oper->type = SLANG_OPER_NONE;
43 oper->children = NULL;
44 oper->num_children = 0;
45 oper->literal[0] = 0.0;
46 oper->literal_size = 1;
47 oper->array_constructor = GL_FALSE;
48 oper->a_id = SLANG_ATOM_NULL;
49 oper->locals = _slang_variable_scope_new(NULL);
50 if (oper->locals == NULL)
51 return GL_FALSE;
52 _slang_variable_scope_ctr(oper->locals);
53 oper->fun = NULL;
54 oper->var = NULL;
55 return GL_TRUE;
56 }
57
58 void
59 slang_operation_destruct(slang_operation * oper)
60 {
61 GLuint i;
62
63 for (i = 0; i < oper->num_children; i++)
64 slang_operation_destruct(oper->children + i);
65 _slang_free(oper->children);
66 slang_variable_scope_destruct(oper->locals);
67 _slang_free(oper->locals);
68 oper->children = NULL;
69 oper->num_children = 0;
70 oper->locals = NULL;
71 }
72
73
74 /**
75 * Recursively traverse 'oper', replacing occurances of 'oldScope' with
76 * 'newScope' in the oper->locals->outer_scope field.
77 */
78 void
79 slang_replace_scope(slang_operation *oper,
80 slang_variable_scope *oldScope,
81 slang_variable_scope *newScope)
82 {
83 GLuint i;
84
85 if (oper->locals != newScope &&
86 oper->locals->outer_scope == oldScope) {
87 /* found. replace old w/ new */
88 oper->locals->outer_scope = newScope;
89 }
90
91 if (oper->type == SLANG_OPER_VARIABLE_DECL) {
92 /* search/replace in the initializer */
93 slang_variable *var;
94 var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
95 if (var && var->initializer) {
96 slang_replace_scope(var->initializer, oldScope, newScope);
97 }
98 }
99
100 /* search/replace in children */
101 for (i = 0; i < oper->num_children; i++) {
102 slang_replace_scope(&oper->children[i], oldScope, newScope);
103 }
104 }
105
106
107 /**
108 * Recursively copy a slang_operation node.
109 * \param x copy target
110 * \param y copy source
111 * \return GL_TRUE for success, GL_FALSE if failure
112 */
113 GLboolean
114 slang_operation_copy(slang_operation * x, const slang_operation * y)
115 {
116 slang_operation z;
117 GLuint i;
118
119 if (!slang_operation_construct(&z))
120 return GL_FALSE;
121 z.type = y->type;
122 if (y->num_children > 0) {
123 z.children = (slang_operation *)
124 _slang_alloc(y->num_children * sizeof(slang_operation));
125 if (z.children == NULL) {
126 slang_operation_destruct(&z);
127 return GL_FALSE;
128 }
129 }
130 for (z.num_children = 0; z.num_children < y->num_children;
131 z.num_children++) {
132 if (!slang_operation_construct(&z.children[z.num_children])) {
133 slang_operation_destruct(&z);
134 return GL_FALSE;
135 }
136 }
137 for (i = 0; i < z.num_children; i++) {
138 if (!slang_operation_copy(&z.children[i], &y->children[i])) {
139 slang_operation_destruct(&z);
140 return GL_FALSE;
141 }
142 }
143 z.literal[0] = y->literal[0];
144 z.literal[1] = y->literal[1];
145 z.literal[2] = y->literal[2];
146 z.literal[3] = y->literal[3];
147 z.literal_size = y->literal_size;
148 assert(y->literal_size >= 1);
149 assert(y->literal_size <= 4);
150 z.a_id = y->a_id;
151 if (y->locals) {
152 if (!slang_variable_scope_copy(z.locals, y->locals)) {
153 slang_operation_destruct(&z);
154 return GL_FALSE;
155 }
156 }
157
158 /* update scoping for children */
159 for (i = 0; i < y->num_children; i++) {
160 if (y->children[i].locals &&
161 y->children[i].locals->outer_scope == y->locals) {
162 z.children[i].locals->outer_scope = z.locals;
163 }
164 }
165
166 #if 0
167 z.var = y->var;
168 z.fun = y->fun;
169 #endif
170 slang_operation_destruct(x);
171 *x = z;
172
173 /* If this operation declares a new scope, we need to make sure
174 * all children point to it, not the original operation's scope!
175 */
176 if (x->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
177 x->type == SLANG_OPER_WHILE ||
178 x->type == SLANG_OPER_FOR) {
179 slang_replace_scope(x, y->locals, x->locals);
180 }
181
182 return GL_TRUE;
183 }
184
185
186 slang_operation *
187 slang_operation_new(GLuint count)
188 {
189 slang_operation *ops
190 = (slang_operation *) _slang_alloc(count * sizeof(slang_operation));
191 assert(count > 0);
192 if (ops) {
193 GLuint i;
194 for (i = 0; i < count; i++)
195 slang_operation_construct(ops + i);
196 }
197 return ops;
198 }
199
200
201 /**
202 * Delete operation and all children
203 */
204 void
205 slang_operation_delete(slang_operation *oper)
206 {
207 slang_operation_destruct(oper);
208 _slang_free(oper);
209 }
210
211
212 void
213 slang_operation_free_children(slang_operation *oper)
214 {
215 GLuint i;
216 for (i = 0; i < slang_oper_num_children(oper); i++) {
217 slang_operation *child = slang_oper_child(oper, i);
218 slang_operation_destruct(child);
219 }
220 _slang_free(oper->children);
221 oper->children = NULL;
222 oper->num_children = 0;
223 }
224
225
226 slang_operation *
227 slang_operation_grow(GLuint *numChildren, slang_operation **children)
228 {
229 slang_operation *ops;
230
231 ops = (slang_operation *)
232 _slang_realloc(*children,
233 *numChildren * sizeof(slang_operation),
234 (*numChildren + 1) * sizeof(slang_operation));
235 if (ops) {
236 slang_operation *newOp = ops + *numChildren;
237 if (!slang_operation_construct(newOp)) {
238 _slang_free(ops);
239 *children = NULL;
240 return NULL;
241 }
242 *children = ops;
243 (*numChildren)++;
244 return newOp;
245 }
246 return NULL;
247 }
248
249 /**
250 * Insert a new slang_operation into an array.
251 * \param numElements pointer to current array size (in/out)
252 * \param array address of the array (in/out)
253 * \param pos position to insert new element
254 * \return pointer to the new operation/element
255 */
256 slang_operation *
257 slang_operation_insert(GLuint *numElements, slang_operation **array,
258 GLuint pos)
259 {
260 slang_operation *ops;
261
262 assert(pos <= *numElements);
263
264 ops = (slang_operation *)
265 _slang_alloc((*numElements + 1) * sizeof(slang_operation));
266 if (ops) {
267 slang_operation *newOp;
268 newOp = ops + pos;
269 if (pos > 0)
270 _mesa_memcpy(ops, *array, pos * sizeof(slang_operation));
271 if (pos < *numElements)
272 _mesa_memcpy(newOp + 1, (*array) + pos,
273 (*numElements - pos) * sizeof(slang_operation));
274
275 if (!slang_operation_construct(newOp)) {
276 _slang_free(ops);
277 *numElements = 0;
278 *array = NULL;
279 return NULL;
280 }
281 if (*array)
282 _slang_free(*array);
283 *array = ops;
284 (*numElements)++;
285 return newOp;
286 }
287 return NULL;
288 }
289
290
291 /**
292 * Add/insert new child into given node at given position.
293 * \return pointer to the new child node
294 */
295 slang_operation *
296 slang_operation_insert_child(slang_operation *oper, GLuint pos)
297 {
298 slang_operation *newOp;
299
300 newOp = slang_operation_insert(&oper->num_children,
301 &oper->children,
302 pos);
303 if (newOp) {
304 newOp->locals->outer_scope = oper->locals;
305 }
306
307 return newOp;
308 }
309
310
311 void
312 _slang_operation_swap(slang_operation *oper0, slang_operation *oper1)
313 {
314 slang_operation tmp = *oper0;
315 *oper0 = *oper1;
316 *oper1 = tmp;
317 }
318
319
320 void
321 slang_operation_add_children(slang_operation *oper, GLuint num_children)
322 {
323 GLuint i;
324 assert(oper->num_children == 0);
325 assert(oper->children == NULL);
326 oper->num_children = num_children;
327 oper->children = slang_operation_new(num_children);
328 for (i = 0; i < num_children; i++) {
329 oper->children[i].locals = _slang_variable_scope_new(oper->locals);
330 }
331 }
332