remove stray tab
[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
34
35 /**
36 * Init a slang_operation object
37 */
38 GLboolean
39 slang_operation_construct(slang_operation * oper)
40 {
41 oper->type = slang_oper_none;
42 oper->children = NULL;
43 oper->num_children = 0;
44 oper->literal[0] = 0.0;
45 oper->a_id = SLANG_ATOM_NULL;
46 oper->locals = _slang_variable_scope_new(NULL);
47 if (oper->locals == NULL)
48 return GL_FALSE;
49 _slang_variable_scope_ctr(oper->locals);
50 oper->fun = NULL;
51 oper->var = NULL;
52 return GL_TRUE;
53 }
54
55 void
56 slang_operation_destruct(slang_operation * oper)
57 {
58 GLuint i;
59
60 for (i = 0; i < oper->num_children; i++)
61 slang_operation_destruct(oper->children + i);
62 slang_alloc_free(oper->children);
63 #ifdef FREE_MEMORY
64 /* XXX revisit and fix memory coruption here ! */
65 slang_variable_scope_destruct(oper->locals);
66 slang_alloc_free(oper->locals);
67 #endif
68 oper->children = NULL;
69 oper->num_children = 0;
70 oper->locals = NULL;
71 }
72
73 /**
74 * Recursively copy a slang_operation node.
75 * \return GL_TRUE for success, GL_FALSE if failure
76 */
77 GLboolean
78 slang_operation_copy(slang_operation * x, const slang_operation * y)
79 {
80 slang_operation z;
81 GLuint i;
82
83 if (!slang_operation_construct(&z))
84 return GL_FALSE;
85 z.type = y->type;
86 z.children = (slang_operation *)
87 slang_alloc_malloc(y->num_children * sizeof(slang_operation));
88 if (z.children == NULL) {
89 slang_operation_destruct(&z);
90 return GL_FALSE;
91 }
92 for (z.num_children = 0; z.num_children < y->num_children;
93 z.num_children++) {
94 if (!slang_operation_construct(&z.children[z.num_children])) {
95 slang_operation_destruct(&z);
96 return GL_FALSE;
97 }
98 }
99 for (i = 0; i < z.num_children; i++) {
100 if (!slang_operation_copy(&z.children[i], &y->children[i])) {
101 slang_operation_destruct(&z);
102 return GL_FALSE;
103 }
104 }
105 z.literal[0] = y->literal[0];
106 z.literal[1] = y->literal[1];
107 z.literal[2] = y->literal[2];
108 z.literal[3] = y->literal[3];
109 z.a_id = y->a_id;
110 if (y->locals) {
111 if (!slang_variable_scope_copy(z.locals, y->locals)) {
112 slang_operation_destruct(&z);
113 return GL_FALSE;
114 }
115 }
116 #if 0
117 z.var = y->var;
118 z.fun = y->fun;
119 #endif
120 slang_operation_destruct(x);
121 *x = z;
122 return GL_TRUE;
123 }
124
125
126 slang_operation *
127 slang_operation_new(GLuint count)
128 {
129 slang_operation *ops
130 = (slang_operation *) _mesa_malloc(count * sizeof(slang_operation));
131 assert(count > 0);
132 if (ops) {
133 GLuint i;
134 for (i = 0; i < count; i++)
135 slang_operation_construct(ops + i);
136 }
137 return ops;
138 }
139
140
141 /**
142 * Delete operation and all children
143 */
144 void
145 slang_operation_delete(slang_operation *oper)
146 {
147 slang_operation_destruct(oper);
148 _mesa_free(oper);
149 }
150
151
152 slang_operation *
153 slang_operation_grow(GLuint *numChildren, slang_operation **children)
154 {
155 slang_operation *ops;
156
157 ops = (slang_operation *)
158 slang_alloc_realloc(*children,
159 *numChildren * sizeof(slang_operation),
160 (*numChildren + 1) * sizeof(slang_operation));
161 if (ops) {
162 slang_operation *newOp = ops + *numChildren;
163 if (!slang_operation_construct(newOp)) {
164 _mesa_free(ops);
165 *children = NULL;
166 return NULL;
167 }
168 *children = ops;
169 (*numChildren)++;
170 return newOp;
171 }
172 return NULL;
173 }
174
175 /**
176 * Insert a new slang_operation into an array.
177 * \param numChildren pointer to current number of children (in/out)
178 * \param children address of array (in/out)
179 * \param pos position to insert
180 * \return pointer to the new operation
181 */
182 slang_operation *
183 slang_operation_insert(GLuint *numChildren, slang_operation **children,
184 GLuint pos)
185 {
186 slang_operation *ops;
187
188 assert(pos <= *numChildren);
189
190 ops = (slang_operation *)
191 _mesa_malloc((*numChildren + 1) * sizeof(slang_operation));
192 if (ops) {
193 slang_operation *newOp;
194 newOp = ops + pos;
195 if (pos > 0)
196 _mesa_memcpy(ops, *children, pos * sizeof(slang_operation));
197 if (pos < *numChildren)
198 _mesa_memcpy(newOp + 1, (*children) + pos,
199 (*numChildren - pos) * sizeof(slang_operation));
200
201 if (!slang_operation_construct(newOp)) {
202 _mesa_free(ops);
203 *numChildren = 0;
204 *children = NULL;
205 return NULL;
206 }
207 *children = ops;
208 (*numChildren)++;
209 return newOp;
210 }
211 return NULL;
212 }
213