summaryrefslogtreecommitdiffstats
path: root/src/mesa/shader/slang/slang_vartable.c
blob: d4ec848a0a1a8ab3d8dbefef0f519b33b127ecf9 (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

#include "imports.h"
#include "slang_compile.h"
#include "slang_compile_variable.h"
#include "slang_vartable.h"
#include "slang_ir.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];

   struct slang_var_table_ *parent;
};



/**
 * Create new table, put at head, return ptr to it.
 */
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));
      }
      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;
      if (dbg) printf("  Free var %s\n", (char*) t->vars[i]->a_name);
      assert(t->temps[store->Index] == VAR);
      t->temps[store->Index] = 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; i++) {
         if (t->temps[i] && !t->parent->temps[i]) {
            if (dbg) printf("  Free reg %d\n", i);
            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;
   }
}


static GLint
alloc_reg(slang_var_table *t, GLint size, GLboolean isTemp)
{
   const GLuint sz4 = (size + 3) / 4;
   GLuint i, j;
   assert(size > 0); /* number of floats */

   for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
      GLuint found = 0;
      for (j = 0; j < sz4; j++) {
         if (!t->temps[i + j]) {
            found++;
         }
         else {
            break;
         }
      }
      if (found == sz4) {
         /* found block of size/4 free regs */
         for (j = 0; j < sz4; j++)
            t->temps[i + j] = isTemp ? TEMP : VAR;
         return i;
      }
   }
   return -1;
}


/**
 * Allocate temp register(s) for storing a variable.
 */
GLint
_slang_alloc_var(slang_var_table *t, GLint size)
{
   int i = alloc_reg(t, size, GL_FALSE);
   if (dbg) printf("Alloc var %d (level %d)\n", i, t->level);
   return i;
}


void
_slang_reserve_var(slang_var_table *t, GLint r, GLint size)
{
   const GLint sz4 = (size + 3) / 4;
   GLint i;
   for (i = 0; i < sz4; i++) {
      t->temps[r + i] = VAR;
   }
}


/**
 * Allocate temp register(s) for storing an unnamed intermediate value.
 */
GLint
_slang_alloc_temp(slang_var_table *t, GLint size)
{
   int i = alloc_reg(t, size, GL_TRUE);
   if (dbg) printf("Alloc temp %d (level %d)\n", i, t->level);
   return i;
}


void
_slang_free_temp(slang_var_table *t, GLint r, GLint size)
{
   const GLuint sz4 = (size + 3) / 4;
   GLuint i;
   assert(size > 0);
   assert(r >= 0);
   assert(r < MAX_PROGRAM_TEMPS);
   if (dbg) printf("Free temp %d (level %d)\n", r, t->level);
   for (i = 0; i < sz4; i++) {
      assert(t->temps[r + i] == TEMP);
      t->temps[r + i] = FREE;
   }
}


GLboolean
_slang_is_temp(slang_var_table *t, GLint r)
{
   assert(r >= 0);
   assert(r < MAX_PROGRAM_TEMPS);
   if (t->temps[r] == TEMP)
      return GL_TRUE;
   else
      return GL_FALSE;
}