Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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 for (i = 0; i < oper->num_children; i++) {
88 slang_replace_scope(&oper->children[i], oldScope, newScope);
89 }
90 }
91
92
93 /**
94 * Recursively copy a slang_operation node.
95 * \param x copy target
96 * \param y copy source
97 * \return GL_TRUE for success, GL_FALSE if failure
98 */
99 GLboolean
100 slang_operation_copy(slang_operation * x, const slang_operation * y)
101 {
102 slang_operation z;
103 GLuint i;
104
105 if (!slang_operation_construct(&z))
106 return GL_FALSE;
107 z.type = y->type;
108 z.children = (slang_operation *)
109 _slang_alloc(y->num_children * sizeof(slang_operation));
110 if (z.children == NULL) {
111 slang_operation_destruct(&z);
112 return GL_FALSE;
113 }
114 for (z.num_children = 0; z.num_children < y->num_children;
115 z.num_children++) {
116 if (!slang_operation_construct(&z.children[z.num_children])) {
117 slang_operation_destruct(&z);
118 return GL_FALSE;
119 }
120 }
121 for (i = 0; i < z.num_children; i++) {
122 if (!slang_operation_copy(&z.children[i], &y->children[i])) {
123 slang_operation_destruct(&z);
124 return GL_FALSE;
125 }
126 }
127 z.literal[0] = y->literal[0];
128 z.literal[1] = y->literal[1];
129 z.literal[2] = y->literal[2];
130 z.literal[3] = y->literal[3];
131 z.literal_size = y->literal_size;
132 assert(y->literal_size >= 1);
133 assert(y->literal_size <= 4);
134 z.a_id = y->a_id;
135 if (y->locals) {
136 if (!slang_variable_scope_copy(z.locals, y->locals)) {
137 slang_operation_destruct(&z);
138 return GL_FALSE;
139 }
140 }
141 #if 0
142 z.var = y->var;
143 z.fun = y->fun;
144 #endif
145 slang_operation_destruct(x);
146 *x = z;
147
148 /* If this operation declares a new scope, we need to make sure
149 * all children point to it, not the original operation's scope!
150 */
151 if (x->type == SLANG_OPER_BLOCK_NEW_SCOPE) {
152 slang_replace_scope(x, y->locals, x->locals);
153 }
154
155 return GL_TRUE;
156 }
157
158
159 slang_operation *
160 slang_operation_new(GLuint count)
161 {
162 slang_operation *ops
163 = (slang_operation *) _slang_alloc(count * sizeof(slang_operation));
164 assert(count > 0);
165 if (ops) {
166 GLuint i;
167 for (i = 0; i < count; i++)
168 slang_operation_construct(ops + i);
169 }
170 return ops;
171 }
172
173
174 /**
175 * Delete operation and all children
176 */
177 void
178 slang_operation_delete(slang_operation *oper)
179 {
180 slang_operation_destruct(oper);
181 _slang_free(oper);
182 }
183
184
185 slang_operation *
186 slang_operation_grow(GLuint *numChildren, slang_operation **children)
187 {
188 slang_operation *ops;
189
190 ops = (slang_operation *)
191 _slang_realloc(*children,
192 *numChildren * sizeof(slang_operation),
193 (*numChildren + 1) * sizeof(slang_operation));
194 if (ops) {
195 slang_operation *newOp = ops + *numChildren;
196 if (!slang_operation_construct(newOp)) {
197 _slang_free(ops);
198 *children = NULL;
199 return NULL;
200 }
201 *children = ops;
202 (*numChildren)++;
203 return newOp;
204 }
205 return NULL;
206 }
207
208 /**
209 * Insert a new slang_operation into an array.
210 * \param numElements pointer to current array size (in/out)
211 * \param array address of the array (in/out)
212 * \param pos position to insert new element
213 * \return pointer to the new operation/element
214 */
215 slang_operation *
216 slang_operation_insert(GLuint *numElements, slang_operation **array,
217 GLuint pos)
218 {
219 slang_operation *ops;
220
221 assert(pos <= *numElements);
222
223 ops = (slang_operation *)
224 _slang_alloc((*numElements + 1) * sizeof(slang_operation));
225 if (ops) {
226 slang_operation *newOp;
227 newOp = ops + pos;
228 if (pos > 0)
229 _mesa_memcpy(ops, *array, pos * sizeof(slang_operation));
230 if (pos < *numElements)
231 _mesa_memcpy(newOp + 1, (*array) + pos,
232 (*numElements - pos) * sizeof(slang_operation));
233
234 if (!slang_operation_construct(newOp)) {
235 _slang_free(ops);
236 *numElements = 0;
237 *array = NULL;
238 return NULL;
239 }
240 if (*array)
241 _slang_free(*array);
242 *array = ops;
243 (*numElements)++;
244 return newOp;
245 }
246 return NULL;
247 }
248
249
250 void
251 _slang_operation_swap(slang_operation *oper0, slang_operation *oper1)
252 {
253 slang_operation tmp = *oper0;
254 *oper0 = *oper1;
255 *oper1 = tmp;
256 }
257
258