mesa: do scope replacement for variable initializers too
[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->a_id = SLANG_ATOM_NULL;
48 oper->locals = _slang_variable_scope_new(NULL);
49 if (oper->locals == NULL)
50 return GL_FALSE;
51 _slang_variable_scope_ctr(oper->locals);
52 oper->fun = NULL;
53 oper->var = NULL;
54 return GL_TRUE;
55 }
56
57 void
58 slang_operation_destruct(slang_operation * oper)
59 {
60 GLuint i;
61
62 for (i = 0; i < oper->num_children; i++)
63 slang_operation_destruct(oper->children + i);
64 _slang_free(oper->children);
65 slang_variable_scope_destruct(oper->locals);
66 _slang_free(oper->locals);
67 oper->children = NULL;
68 oper->num_children = 0;
69 oper->locals = NULL;
70 }
71
72
73 /**
74 * Recursively traverse 'oper', replacing occurances of 'oldScope' with
75 * 'newScope' in the oper->locals->outer_scope field.
76 */
77 void
78 slang_replace_scope(slang_operation *oper,
79 slang_variable_scope *oldScope,
80 slang_variable_scope *newScope)
81 {
82 GLuint i;
83 if (oper->locals != newScope &&
84 oper->locals->outer_scope == oldScope) {
85 oper->locals->outer_scope = newScope;
86 }
87
88 if (oper->type == SLANG_OPER_VARIABLE_DECL) {
89 slang_variable *var;
90 var = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
91 if (var && var->initializer) {
92 printf("replace scope for %s initializer\n",
93 (char *) var->a_name);
94 slang_replace_scope(var->initializer, oldScope, newScope);
95 }
96 }
97
98 for (i = 0; i < oper->num_children; i++) {
99 slang_replace_scope(&oper->children[i], oldScope, newScope);
100 }
101 }
102
103
104 /**
105 * Recursively copy a slang_operation node.
106 * \param x copy target
107 * \param y copy source
108 * \return GL_TRUE for success, GL_FALSE if failure
109 */
110 GLboolean
111 slang_operation_copy(slang_operation * x, const slang_operation * y)
112 {
113 slang_operation z;
114 GLuint i;
115
116 if (!slang_operation_construct(&z))
117 return GL_FALSE;
118 z.type = y->type;
119 z.children = (slang_operation *)
120 _slang_alloc(y->num_children * sizeof(slang_operation));
121 if (z.children == NULL) {
122 slang_operation_destruct(&z);
123 return GL_FALSE;
124 }
125 for (z.num_children = 0; z.num_children < y->num_children;
126 z.num_children++) {
127 if (!slang_operation_construct(&z.children[z.num_children])) {
128 slang_operation_destruct(&z);
129 return GL_FALSE;
130 }
131 }
132 for (i = 0; i < z.num_children; i++) {
133 if (!slang_operation_copy(&z.children[i], &y->children[i])) {
134 slang_operation_destruct(&z);
135 return GL_FALSE;
136 }
137 }
138 z.literal[0] = y->literal[0];
139 z.literal[1] = y->literal[1];
140 z.literal[2] = y->literal[2];
141 z.literal[3] = y->literal[3];
142 z.literal_size = y->literal_size;
143 assert(y->literal_size >= 1);
144 assert(y->literal_size <= 4);
145 z.a_id = y->a_id;
146 if (y->locals) {
147 if (!slang_variable_scope_copy(z.locals, y->locals)) {
148 slang_operation_destruct(&z);
149 return GL_FALSE;
150 }
151 }
152 #if 0
153 z.var = y->var;
154 z.fun = y->fun;
155 #endif
156 slang_operation_destruct(x);
157 *x = z;
158
159 /* If this operation declares a new scope, we need to make sure
160 * all children point to it, not the original operation's scope!
161 */
162 if (x->type == SLANG_OPER_BLOCK_NEW_SCOPE) {
163 slang_replace_scope(x, y->locals, x->locals);
164 }
165
166 return GL_TRUE;
167 }
168
169
170 slang_operation *
171 slang_operation_new(GLuint count)
172 {
173 slang_operation *ops
174 = (slang_operation *) _slang_alloc(count * sizeof(slang_operation));
175 assert(count > 0);
176 if (ops) {
177 GLuint i;
178 for (i = 0; i < count; i++)
179 slang_operation_construct(ops + i);
180 }
181 return ops;
182 }
183
184
185 /**
186 * Delete operation and all children
187 */
188 void
189 slang_operation_delete(slang_operation *oper)
190 {
191 slang_operation_destruct(oper);
192 _slang_free(oper);
193 }
194
195
196 slang_operation *
197 slang_operation_grow(GLuint *numChildren, slang_operation **children)
198 {
199 slang_operation *ops;
200
201 ops = (slang_operation *)
202 _slang_realloc(*children,
203 *numChildren * sizeof(slang_operation),
204 (*numChildren + 1) * sizeof(slang_operation));
205 if (ops) {
206 slang_operation *newOp = ops + *numChildren;
207 if (!slang_operation_construct(newOp)) {
208 _slang_free(ops);
209 *children = NULL;
210 return NULL;
211 }
212 *children = ops;
213 (*numChildren)++;
214 return newOp;
215 }
216 return NULL;
217 }
218
219 /**
220 * Insert a new slang_operation into an array.
221 * \param numElements pointer to current array size (in/out)
222 * \param array address of the array (in/out)
223 * \param pos position to insert new element
224 * \return pointer to the new operation/element
225 */
226 slang_operation *
227 slang_operation_insert(GLuint *numElements, slang_operation **array,
228 GLuint pos)
229 {
230 slang_operation *ops;
231
232 assert(pos <= *numElements);
233
234 ops = (slang_operation *)
235 _slang_alloc((*numElements + 1) * sizeof(slang_operation));
236 if (ops) {
237 slang_operation *newOp;
238 newOp = ops + pos;
239 if (pos > 0)
240 _mesa_memcpy(ops, *array, pos * sizeof(slang_operation));
241 if (pos < *numElements)
242 _mesa_memcpy(newOp + 1, (*array) + pos,
243 (*numElements - pos) * sizeof(slang_operation));
244
245 if (!slang_operation_construct(newOp)) {
246 _slang_free(ops);
247 *numElements = 0;
248 *array = NULL;
249 return NULL;
250 }
251 if (*array)
252 _slang_free(*array);
253 *array = ops;
254 (*numElements)++;
255 return newOp;
256 }
257 return NULL;
258 }
259
260
261 void
262 _slang_operation_swap(slang_operation *oper0, slang_operation *oper1)
263 {
264 slang_operation tmp = *oper0;
265 *oper0 = *oper1;
266 *oper1 = tmp;
267 }
268
269