Merge remote branch 'origin/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / mesa / shader / slang / slang_vartable.c
1
2 #include "main/imports.h"
3 #include "shader/program.h"
4 #include "shader/prog_print.h"
5 #include "slang_compile.h"
6 #include "slang_compile_variable.h"
7 #include "slang_mem.h"
8 #include "slang_vartable.h"
9 #include "slang_ir.h"
10
11
12 static int dbg = 0;
13
14
15 typedef enum {
16 FREE,
17 VAR,
18 TEMP
19 } TempState;
20
21
22 /**
23 * Variable/register info for one variable scope.
24 */
25 struct table
26 {
27 int Level;
28 int NumVars;
29 slang_variable **Vars; /* array [NumVars] */
30
31 TempState Temps[MAX_PROGRAM_TEMPS * 4]; /* per-component state */
32 int ValSize[MAX_PROGRAM_TEMPS * 4]; /**< For debug only */
33
34 struct table *Parent; /** Parent scope table */
35 };
36
37
38 /**
39 * A variable table is a stack of tables, one per scope.
40 */
41 struct slang_var_table_
42 {
43 GLint CurLevel;
44 GLuint MaxRegisters;
45 struct table *Top; /**< Table at top of stack */
46 };
47
48
49
50 slang_var_table *
51 _slang_new_var_table(GLuint maxRegisters)
52 {
53 slang_var_table *vt
54 = (slang_var_table *) _slang_alloc(sizeof(slang_var_table));
55 if (vt) {
56 vt->MaxRegisters = maxRegisters;
57 }
58 return vt;
59 }
60
61
62 void
63 _slang_delete_var_table(slang_var_table *vt)
64 {
65 if (vt->Top) {
66 _mesa_problem(NULL, "non-empty var table in _slang_delete_var_table()");
67 return;
68 }
69 _slang_free(vt);
70 }
71
72
73
74 /**
75 * Create new table, put at head, return ptr to it.
76 * XXX we should take a maxTemps parameter to indicate how many temporaries
77 * are available for the current shader/program target.
78 */
79 void
80 _slang_push_var_table(slang_var_table *vt)
81 {
82 struct table *t = (struct table *) _slang_alloc(sizeof(struct table));
83 if (t) {
84 t->Level = vt->CurLevel++;
85 t->Parent = vt->Top;
86 if (t->Parent) {
87 /* copy the info indicating which temp regs are in use */
88 memcpy(t->Temps, t->Parent->Temps, sizeof(t->Temps));
89 memcpy(t->ValSize, t->Parent->ValSize, sizeof(t->ValSize));
90 }
91 vt->Top = t;
92 if (dbg) printf("Pushing level %d\n", t->Level);
93 }
94 }
95
96
97 /**
98 * Destroy given table, return ptr to Parent
99 */
100 void
101 _slang_pop_var_table(slang_var_table *vt)
102 {
103 struct table *t = vt->Top;
104 int i;
105
106 if (dbg) printf("Popping level %d\n", t->Level);
107
108 /* free the storage allocated for each variable */
109 for (i = 0; i < t->NumVars; i++) {
110 slang_ir_storage *store = (slang_ir_storage *) t->Vars[i]->aux;
111 GLint j;
112 GLuint comp;
113 if (dbg) printf(" Free var %s, size %d at %d.%s\n",
114 (char*) t->Vars[i]->a_name, store->Size,
115 store->Index,
116 _mesa_swizzle_string(store->Swizzle, 0, 0));
117
118 if (store->Size == 1)
119 comp = GET_SWZ(store->Swizzle, 0);
120 else
121 comp = 0;
122
123 assert(store->Index >= 0);
124 for (j = 0; j < store->Size; j++) {
125 assert(t->Temps[store->Index * 4 + j + comp] == VAR);
126 t->Temps[store->Index * 4 + j + comp] = FREE;
127 }
128 store->Index = -1;
129 }
130 if (t->Parent) {
131 /* just verify that any remaining allocations in this scope
132 * were for temps
133 */
134 for (i = 0; i < (int) vt->MaxRegisters * 4; i++) {
135 if (t->Temps[i] != FREE && t->Parent->Temps[i] == FREE) {
136 if (dbg) printf(" Free reg %d\n", i/4);
137 assert(t->Temps[i] == TEMP);
138 }
139 }
140 }
141
142 if (t->Vars) {
143 _slang_free(t->Vars);
144 t->Vars = NULL;
145 }
146
147 vt->Top = t->Parent;
148 _slang_free(t);
149 vt->CurLevel--;
150 }
151
152
153 /**
154 * Add a new variable to the given symbol table.
155 */
156 void
157 _slang_add_variable(slang_var_table *vt, slang_variable *v)
158 {
159 struct table *t;
160 assert(vt);
161 t = vt->Top;
162 assert(t);
163 if (dbg) printf("Adding var %s, store %p\n", (char *) v->a_name, v->aux);
164 t->Vars = (slang_variable **)
165 _slang_realloc(t->Vars,
166 t->NumVars * sizeof(slang_variable *),
167 (t->NumVars + 1) * sizeof(slang_variable *));
168 t->Vars[t->NumVars] = v;
169 t->NumVars++;
170 }
171
172
173 /**
174 * Look for variable by name in given table.
175 * If not found, Parent table will be searched.
176 */
177 slang_variable *
178 _slang_find_variable(const slang_var_table *vt, slang_atom name)
179 {
180 struct table *t = vt->Top;
181 while (1) {
182 int i;
183 for (i = 0; i < t->NumVars; i++) {
184 if (t->Vars[i]->a_name == name)
185 return t->Vars[i];
186 }
187 if (t->Parent)
188 t = t->Parent;
189 else
190 return NULL;
191 }
192 }
193
194
195 /**
196 * Allocation helper.
197 * \param size var size in floats
198 * \return position for var, measured in floats
199 */
200 static GLint
201 alloc_reg(slang_var_table *vt, GLint size, GLboolean isTemp)
202 {
203 struct table *t = vt->Top;
204 /* if size == 1, allocate anywhere, else, pos must be multiple of 4 */
205 const GLuint step = (size == 1) ? 1 : 4;
206 GLuint i, j;
207 assert(size > 0); /* number of floats */
208
209 for (i = 0; i <= vt->MaxRegisters * 4 - size; i += step) {
210 GLuint found = 0;
211 for (j = 0; j < (GLuint) size; j++) {
212 if (i + j < vt->MaxRegisters * 4 && t->Temps[i + j] == FREE) {
213 found++;
214 }
215 else {
216 break;
217 }
218 }
219 if (found == size) {
220 /* found block of size free regs */
221 if (size > 1)
222 assert(i % 4 == 0);
223 for (j = 0; j < (GLuint) size; j++)
224 t->Temps[i + j] = isTemp ? TEMP : VAR;
225 assert(i < MAX_PROGRAM_TEMPS * 4);
226 t->ValSize[i] = size;
227 return i;
228 }
229 }
230 return -1;
231 }
232
233
234 /**
235 * Allocate temp register(s) for storing a variable.
236 * \param size size needed, in floats
237 * \param swizzle returns swizzle mask for accessing var in register
238 * \return register allocated, or -1
239 */
240 GLboolean
241 _slang_alloc_var(slang_var_table *vt, slang_ir_storage *store)
242 {
243 struct table *t = vt->Top;
244 const int i = alloc_reg(vt, store->Size, GL_FALSE);
245 if (i < 0)
246 return GL_FALSE;
247
248 store->Index = i / 4;
249 if (store->Size == 1) {
250 const GLuint comp = i % 4;
251 store->Swizzle = MAKE_SWIZZLE4(comp, comp, comp, comp);
252 }
253 else if (store->Size == 2) {
254 store->Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
255 SWIZZLE_NIL, SWIZZLE_NIL);
256 }
257 else if (store->Size == 3) {
258 store->Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
259 SWIZZLE_Z, SWIZZLE_NIL);
260 }
261 else {
262 store->Swizzle = SWIZZLE_NOOP;
263 }
264
265 if (dbg)
266 printf("Alloc var storage sz %d at %d.%s (level %d) store %p\n",
267 store->Size, store->Index,
268 _mesa_swizzle_string(store->Swizzle, 0, 0),
269 t->Level,
270 (void*) store);
271
272 return GL_TRUE;
273 }
274
275
276
277 /**
278 * Allocate temp register(s) for storing an unnamed intermediate value.
279 */
280 GLboolean
281 _slang_alloc_temp(slang_var_table *vt, slang_ir_storage *store)
282 {
283 struct table *t = vt->Top;
284 const int i = alloc_reg(vt, store->Size, GL_TRUE);
285 if (i < 0)
286 return GL_FALSE;
287
288 assert(store->Index < 0);
289
290 store->Index = i / 4;
291 if (store->Size == 1) {
292 const GLuint comp = i % 4;
293 store->Swizzle = MAKE_SWIZZLE4(comp, comp, comp, comp);
294 }
295 else {
296 /* XXX improve swizzled for size=2/3, use for writemask... */
297 #if 1
298 if (store->Size == 2) {
299 store->Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
300 SWIZZLE_NIL, SWIZZLE_NIL);
301 }
302 #endif
303 store->Swizzle = SWIZZLE_NOOP;
304 }
305
306 if (dbg) printf("Alloc temp sz %d at %d.%s (level %d) store %p\n",
307 store->Size, store->Index,
308 _mesa_swizzle_string(store->Swizzle, 0, 0), t->Level,
309 (void *) store);
310
311 return GL_TRUE;
312 }
313
314
315 void
316 _slang_free_temp(slang_var_table *vt, slang_ir_storage *store)
317 {
318 struct table *t = vt->Top;
319 GLuint i;
320 GLuint r = store->Index;
321 assert(store->Size > 0);
322 assert(r >= 0);
323 assert(r + store->Size <= vt->MaxRegisters * 4);
324 if (dbg) printf("Free temp sz %d at %d.%s (level %d) store %p\n",
325 store->Size, r,
326 _mesa_swizzle_string(store->Swizzle, 0, 0),
327 t->Level, (void *) store);
328 if (store->Size == 1) {
329 const GLuint comp = GET_SWZ(store->Swizzle, 0);
330 /* we can actually fail some of these assertions because of the
331 * troublesome IR_SWIZZLE handling.
332 */
333 #if 0
334 assert(store->Swizzle == MAKE_SWIZZLE4(comp, comp, comp, comp));
335 assert(comp < 4);
336 assert(t->ValSize[r * 4 + comp] == 1);
337 #endif
338 assert(t->Temps[r * 4 + comp] == TEMP);
339 t->Temps[r * 4 + comp] = FREE;
340 }
341 else {
342 /*assert(store->Swizzle == SWIZZLE_NOOP);*/
343 assert(t->ValSize[r*4] == store->Size);
344 for (i = 0; i < (GLuint) store->Size; i++) {
345 assert(t->Temps[r * 4 + i] == TEMP);
346 t->Temps[r * 4 + i] = FREE;
347 }
348 }
349 }
350
351
352 GLboolean
353 _slang_is_temp(const slang_var_table *vt, const slang_ir_storage *store)
354 {
355 struct table *t = vt->Top;
356 GLuint comp;
357 assert(store->Index >= 0);
358 assert(store->Index < (int) vt->MaxRegisters);
359 if (store->Swizzle == SWIZZLE_NOOP)
360 comp = 0;
361 else
362 comp = GET_SWZ(store->Swizzle, 0);
363
364 if (t->Temps[store->Index * 4 + comp] == TEMP)
365 return GL_TRUE;
366 else
367 return GL_FALSE;
368 }