implement full reference counting for vertex/fragment programs
[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 "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 * Recursively copy a slang_operation node.
74 * \return GL_TRUE for success, GL_FALSE if failure
75 */
76 GLboolean
77 slang_operation_copy(slang_operation * x, const slang_operation * y)
78 {
79 slang_operation z;
80 GLuint i;
81
82 if (!slang_operation_construct(&z))
83 return GL_FALSE;
84 z.type = y->type;
85 z.children = (slang_operation *)
86 _slang_alloc(y->num_children * sizeof(slang_operation));
87 if (z.children == NULL) {
88 slang_operation_destruct(&z);
89 return GL_FALSE;
90 }
91 for (z.num_children = 0; z.num_children < y->num_children;
92 z.num_children++) {
93 if (!slang_operation_construct(&z.children[z.num_children])) {
94 slang_operation_destruct(&z);
95 return GL_FALSE;
96 }
97 }
98 for (i = 0; i < z.num_children; i++) {
99 if (!slang_operation_copy(&z.children[i], &y->children[i])) {
100 slang_operation_destruct(&z);
101 return GL_FALSE;
102 }
103 }
104 z.literal[0] = y->literal[0];
105 z.literal[1] = y->literal[1];
106 z.literal[2] = y->literal[2];
107 z.literal[3] = y->literal[3];
108 z.literal_size = y->literal_size;
109 assert(y->literal_size >= 1);
110 assert(y->literal_size <= 4);
111 z.a_id = y->a_id;
112 if (y->locals) {
113 if (!slang_variable_scope_copy(z.locals, y->locals)) {
114 slang_operation_destruct(&z);
115 return GL_FALSE;
116 }
117 }
118 #if 0
119 z.var = y->var;
120 z.fun = y->fun;
121 #endif
122 slang_operation_destruct(x);
123 *x = z;
124 return GL_TRUE;
125 }
126
127
128 slang_operation *
129 slang_operation_new(GLuint count)
130 {
131 slang_operation *ops
132 = (slang_operation *) _slang_alloc(count * sizeof(slang_operation));
133 assert(count > 0);
134 if (ops) {
135 GLuint i;
136 for (i = 0; i < count; i++)
137 slang_operation_construct(ops + i);
138 }
139 return ops;
140 }
141
142
143 /**
144 * Delete operation and all children
145 */
146 void
147 slang_operation_delete(slang_operation *oper)
148 {
149 slang_operation_destruct(oper);
150 _slang_free(oper);
151 }
152
153
154 slang_operation *
155 slang_operation_grow(GLuint *numChildren, slang_operation **children)
156 {
157 slang_operation *ops;
158
159 ops = (slang_operation *)
160 _slang_realloc(*children,
161 *numChildren * sizeof(slang_operation),
162 (*numChildren + 1) * sizeof(slang_operation));
163 if (ops) {
164 slang_operation *newOp = ops + *numChildren;
165 if (!slang_operation_construct(newOp)) {
166 _slang_free(ops);
167 *children = NULL;
168 return NULL;
169 }
170 *children = ops;
171 (*numChildren)++;
172 return newOp;
173 }
174 return NULL;
175 }
176
177 /**
178 * Insert a new slang_operation into an array.
179 * \param numElements pointer to current array size (in/out)
180 * \param array address of the array (in/out)
181 * \param pos position to insert new element
182 * \return pointer to the new operation/element
183 */
184 slang_operation *
185 slang_operation_insert(GLuint *numElements, slang_operation **array,
186 GLuint pos)
187 {
188 slang_operation *ops;
189
190 assert(pos <= *numElements);
191
192 ops = (slang_operation *)
193 _slang_alloc((*numElements + 1) * sizeof(slang_operation));
194 if (ops) {
195 slang_operation *newOp;
196 newOp = ops + pos;
197 if (pos > 0)
198 _mesa_memcpy(ops, *array, pos * sizeof(slang_operation));
199 if (pos < *numElements)
200 _mesa_memcpy(newOp + 1, (*array) + pos,
201 (*numElements - pos) * sizeof(slang_operation));
202
203 if (!slang_operation_construct(newOp)) {
204 _slang_free(ops);
205 *numElements = 0;
206 *array = NULL;
207 return NULL;
208 }
209 if (*array)
210 _slang_free(*array);
211 *array = ops;
212 (*numElements)++;
213 return newOp;
214 }
215 return NULL;
216 }
217
218
219 void
220 _slang_operation_swap(slang_operation *oper0, slang_operation *oper1)
221 {
222 slang_operation tmp = *oper0;
223 *oper0 = *oper1;
224 *oper1 = tmp;
225 }
226
227