summaryrefslogtreecommitdiffstats
path: root/src/mesa/shader/slang/slang_vartable.c
blob: 0271455428200a6efa37363f41b8050167effeed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

#include "imports.h"
#include "slang_compile.h"
#include "slang_compile_variable.h"
#include "slang_vartable.h"
#include "slang_ir.h"
#include "prog_instruction.h"


static int dbg = 0;


typedef enum {
   FREE,
   VAR,
   TEMP
} TempState;

static int Level = 0;

struct slang_var_table_
{
   int level;
   int num_entries;
   slang_variable **vars;  /* array [num_entries] */

   TempState temps[MAX_PROGRAM_TEMPS * 4];  /* per-component state */
   int size[MAX_PROGRAM_TEMPS];     /* For debug only */

   struct slang_var_table_ *parent;
};



/**
 * Create new table, put at head, return ptr to it.
 * XXX we should take a maxTemps parameter to indicate how many temporaries
 * are available for the current shader/program target.
 */
slang_var_table *
_slang_push_var_table(slang_var_table *parent)
{
   slang_var_table *t
      = (slang_var_table *) _mesa_calloc(sizeof(slang_var_table));
   if (t) {
      t->level = Level++;
      t->parent = parent;
      if (parent) {
         /* copy the info indicating which temp regs are in use */
         memcpy(t->temps, parent->temps, sizeof(t->temps));
         memcpy(t->size, parent->size, sizeof(t->size));
      }
      if (dbg) printf("Pushing level %d\n", t->level);
   }
   return t;
}


/**
 * Destroy given table, return ptr to parent
 */
slang_var_table *
_slang_pop_var_table(slang_var_table *t)
{
   slang_var_table *parent = t->parent;
   int i;

   if (dbg) printf("Popping level %d\n", t->level);

   /* free the storage allocated for each variable */
   for (i = 0; i < t->num_entries; i++) {
      slang_ir_storage *store = (slang_ir_storage *) t->vars[i]->aux;
      GLint j;
      const GLuint sz = store->Size;
      GLuint comp;
      if (dbg) printf("  Free var %s, size %d at %d\n",
                      (char*) t->vars[i]->a_name, store->Size,
                      store->Index);

      if (sz == 1)
         comp = GET_SWZ(store->Swizzle, 0);
      else
         comp = 0;

      assert(store->Index >= 0);
      for (j = 0; j < sz; j++) {
         assert(t->temps[store->Index * 4 + j + comp] == VAR);
         t->temps[store->Index * 4 + j + comp] = FREE;
      }
      store->Index = -1;
   }
   if (t->parent) {
      /* just verify that any remaining allocations in this scope 
       * were for temps
       */
      for (i = 0; i < MAX_PROGRAM_TEMPS * 4; i++) {
         if (t->temps[i] != FREE && t->parent->temps[i] == FREE) {
            if (dbg) printf("  Free reg %d\n", i/4);
            assert(t->temps[i] == TEMP);
         }
      }
   }

   if (t->vars)
      free(t->vars);
   free(t);
   Level--;
   return parent;
}


/**
 * Add a new variable to the given symbol table.
 */
void
_slang_add_variable(slang_var_table *t, slang_variable *v)
{
   assert(t);
   if (dbg) printf("Adding var %s\n", (char *) v->a_name);
   t->vars = realloc(t->vars, (t->num_entries + 1) * sizeof(slang_variable *));
   t->vars[t->num_entries] = v;
   t->num_entries++;
}


/**
 * Look for variable by name in given table.
 * If not found, parent table will be searched.
 */
slang_variable *
_slang_find_variable(const slang_var_table *t, slang_atom name)
{
   while (1) {
      int i;
      for (i = 0; i < t->num_entries; i++) {
         if (t->vars[i]->a_name == name)
            return t->vars[i];
      }
      if (t->parent)
         t = t->parent;
      else
         return NULL;
   }
}


/**
 * Allocation helper.
 * \param size  var size in floats
 * \return  position for var, measured in floats
 */
static GLint
alloc_reg(slang_var_table *t, GLint size, GLboolean isTemp)
{
   /* if size == 1, allocate anywhere, else, pos must be multiple of 4 */
   const GLuint step = (size == 1) ? 1 : 4;
   GLuint i, j;
   assert(size > 0); /* number of floats */

   for (i = 0; i < MAX_PROGRAM_TEMPS - size; i += step) {
      GLuint found = 0;
      for (j = 0; j < size; j++) {
         if (i + j < MAX_PROGRAM_TEMPS && t->temps[i + j] == FREE) {
            found++;
         }
         else {
            break;
         }
      }
      if (found == size) {
         /* found block of size free regs */
         if (size > 1)
            assert(i % 4 == 0);
         for (j = 0; j < size; j++)
            t->temps[i + j] = isTemp ? TEMP : VAR;
         printf("t->size[%d] = %d\n", i, size);
         t->size[i] = size;
         return i;
      }
   }
   return -1;
}


/**
 * Allocate temp register(s) for storing a variable.
 * \param size  size needed, in floats
 * \param swizzle  returns swizzle mask for accessing var in register
 * \return  register allocated, or -1
 */
GLboolean
_slang_alloc_var(slang_var_table *t, slang_ir_storage *store)
{
   const int i = alloc_reg(t, store->Size, GL_FALSE);
   if (i < 0)
      return GL_FALSE;

   store->Index = i / 4;
   if (store->Size == 1) {
      const GLuint comp = i % 4;
      store->Swizzle = MAKE_SWIZZLE4(comp, comp, comp, comp);
      if (dbg) printf("Alloc var sz %d at %d.%c (level %d)\n",
                      store->Size, store->Index, "xyzw"[comp], t->level);
   }
   else {
      store->Swizzle = SWIZZLE_NOOP;
      if (dbg) printf("Alloc var sz %d at %d.xyzw (level %d)\n",
                      store->Size, store->Index, t->level);
   }
   return GL_TRUE;
}



/**
 * Allocate temp register(s) for storing an unnamed intermediate value.
 */
GLboolean
_slang_alloc_temp(slang_var_table *t, slang_ir_storage *store)
{
   const int i = alloc_reg(t, store->Size, GL_TRUE);
   if (i < 0)
      return GL_FALSE;

   store->Index = i / 4;
   if (store->Size == 1) {
      const GLuint comp = i % 4;
      store->Swizzle = MAKE_SWIZZLE4(comp, comp, comp, comp);
      if (dbg) printf("Alloc temp sz %d at %d.%c (level %d)\n",
                      store->Size, store->Index, "xyzw"[comp], t->level);
   }
   else {
      store->Swizzle = SWIZZLE_NOOP;
      if (dbg) printf("Alloc temp sz %d at %d.xyzw (level %d)\n",
                      store->Size, store->Index, t->level);
   }
   return GL_TRUE;
}


void
_slang_free_temp(slang_var_table *t, slang_ir_storage *store)
{
   GLuint i;
   GLuint r = store->Index;
   assert(store->Size > 0);
   assert(r >= 0);
   assert(r + store->Size <= MAX_PROGRAM_TEMPS);
   if (dbg) printf("Free temp sz %d at %d (level %d)\n", store->Size, r, t->level);
   if (store->Size == 1) {
      const GLuint comp = GET_SWZ(store->Swizzle, 0);
      assert(store->Swizzle == MAKE_SWIZZLE4(comp, comp, comp, comp));
      assert(comp < 4);
      assert(t->size[r * 4 + comp] == 1);
      assert(t->temps[r * 4 + comp] == TEMP);
      t->temps[r * 4 + comp] = FREE;
   }
   else {
      assert(store->Swizzle == SWIZZLE_NOOP);
      assert(t->size[r*4] == store->Size);
      for (i = 0; i < store->Size; i++) {
         assert(t->temps[r * 4 + i] == TEMP);
         t->temps[r * 4 + i] = FREE;
      }
   }
}


GLboolean
_slang_is_temp(slang_var_table *t, slang_ir_storage *store)
{
   assert(store->Index >= 0);
   assert(store->Index < MAX_PROGRAM_TEMPS);
   GLuint comp;
   if (store->Swizzle == SWIZZLE_NOOP)
      comp = 0;
   else
      comp = GET_SWZ(store->Swizzle, 0);

   if (t->temps[store->Index * 4 + comp] == TEMP)
      return GL_TRUE;
   else
      return GL_FALSE;
}