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