r600g: add POW instruction
[mesa.git] / src / mesa / slang / slang_compile_variable.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 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_variable.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 static slang_variable *
37 slang_variable_new(void)
38 {
39 slang_variable *v = (slang_variable *) _slang_alloc(sizeof(slang_variable));
40 if (v) {
41 if (!slang_variable_construct(v)) {
42 _slang_free(v);
43 v = NULL;
44 }
45 }
46 return v;
47 }
48
49
50 static void
51 slang_variable_delete(slang_variable * var)
52 {
53 slang_variable_destruct(var);
54 _slang_free(var);
55 }
56
57
58 /*
59 * slang_variable_scope
60 */
61
62 slang_variable_scope *
63 _slang_variable_scope_new(slang_variable_scope *parent)
64 {
65 slang_variable_scope *s;
66 s = (slang_variable_scope *) _slang_alloc(sizeof(slang_variable_scope));
67 if (s)
68 s->outer_scope = parent;
69 return s;
70 }
71
72
73 GLvoid
74 _slang_variable_scope_ctr(slang_variable_scope * self)
75 {
76 self->variables = NULL;
77 self->num_variables = 0;
78 self->outer_scope = NULL;
79 }
80
81 void
82 slang_variable_scope_destruct(slang_variable_scope * scope)
83 {
84 unsigned int i;
85
86 if (!scope)
87 return;
88 for (i = 0; i < scope->num_variables; i++) {
89 if (scope->variables[i])
90 slang_variable_delete(scope->variables[i]);
91 }
92 _slang_free(scope->variables);
93 /* do not free scope->outer_scope */
94 }
95
96 int
97 slang_variable_scope_copy(slang_variable_scope * x,
98 const slang_variable_scope * y)
99 {
100 slang_variable_scope z;
101 unsigned int i;
102
103 _slang_variable_scope_ctr(&z);
104 z.variables = (slang_variable **)
105 _slang_alloc(y->num_variables * sizeof(slang_variable *));
106 if (z.variables == NULL) {
107 slang_variable_scope_destruct(&z);
108 return 0;
109 }
110 for (z.num_variables = 0; z.num_variables < y->num_variables;
111 z.num_variables++) {
112 z.variables[z.num_variables] = slang_variable_new();
113 if (!z.variables[z.num_variables]) {
114 slang_variable_scope_destruct(&z);
115 return 0;
116 }
117 }
118 for (i = 0; i < z.num_variables; i++) {
119 if (!slang_variable_copy(z.variables[i], y->variables[i])) {
120 slang_variable_scope_destruct(&z);
121 return 0;
122 }
123 }
124 z.outer_scope = y->outer_scope;
125 slang_variable_scope_destruct(x);
126 *x = z;
127 return 1;
128 }
129
130
131 /**
132 * Grow the variable list by one.
133 * \return pointer to space for the new variable (will be initialized)
134 */
135 slang_variable *
136 slang_variable_scope_grow(slang_variable_scope *scope)
137 {
138 const int n = scope->num_variables;
139 scope->variables = (slang_variable **)
140 _slang_realloc(scope->variables,
141 n * sizeof(slang_variable *),
142 (n + 1) * sizeof(slang_variable *));
143 if (!scope->variables)
144 return NULL;
145
146 scope->num_variables++;
147
148 scope->variables[n] = slang_variable_new();
149 if (!scope->variables[n])
150 return NULL;
151
152 return scope->variables[n];
153 }
154
155
156
157 /* slang_variable */
158
159 int
160 slang_variable_construct(slang_variable * var)
161 {
162 if (!slang_fully_specified_type_construct(&var->type))
163 return 0;
164 var->a_name = SLANG_ATOM_NULL;
165 var->array_len = 0;
166 var->initializer = NULL;
167 var->size = 0;
168 var->isTemp = GL_FALSE;
169 var->store = NULL;
170 var->declared = 0;
171 return 1;
172 }
173
174
175 void
176 slang_variable_destruct(slang_variable * var)
177 {
178 slang_fully_specified_type_destruct(&var->type);
179 if (var->initializer != NULL) {
180 slang_operation_destruct(var->initializer);
181 _slang_free(var->initializer);
182 }
183 #if 0
184 if (var->aux) {
185 free(var->aux);
186 }
187 #endif
188 }
189
190
191 int
192 slang_variable_copy(slang_variable * x, const slang_variable * y)
193 {
194 slang_variable z;
195
196 if (!slang_variable_construct(&z))
197 return 0;
198 if (!slang_fully_specified_type_copy(&z.type, &y->type)) {
199 slang_variable_destruct(&z);
200 return 0;
201 }
202 z.a_name = y->a_name;
203 z.array_len = y->array_len;
204 if (y->initializer != NULL) {
205 z.initializer
206 = (slang_operation *) _slang_alloc(sizeof(slang_operation));
207 if (z.initializer == NULL) {
208 slang_variable_destruct(&z);
209 return 0;
210 }
211 if (!slang_operation_construct(z.initializer)) {
212 _slang_free(z.initializer);
213 slang_variable_destruct(&z);
214 return 0;
215 }
216 if (!slang_operation_copy(z.initializer, y->initializer)) {
217 slang_variable_destruct(&z);
218 return 0;
219 }
220 }
221 z.size = y->size;
222 slang_variable_destruct(x);
223 *x = z;
224 return 1;
225 }
226
227
228 /**
229 * Search for named variable in given scope.
230 * \param all if true, search parent scopes too.
231 */
232 slang_variable *
233 _slang_variable_locate(const slang_variable_scope * scope,
234 const slang_atom a_name, GLboolean all)
235 {
236 while (scope) {
237 GLuint i;
238 for (i = 0; i < scope->num_variables; i++)
239 if (a_name == scope->variables[i]->a_name)
240 return scope->variables[i];
241 if (all)
242 scope = scope->outer_scope;
243 else
244 scope = NULL;
245 }
246 return NULL;
247 }