summaryrefslogtreecommitdiff
path: root/src/mesa/shader/slang/slang_compile_variable.c
blob: ae37aed514537df1c0591e6fe8f7e351ee6988d0 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Mesa 3-D graphics library
 * Version:  6.5.3
 *
 * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file slang_compile_variable.c
 * slang front-end compiler
 * \author Michal Krol
 */

#include "imports.h"
#include "slang_compile.h"

/* slang_type_specifier_type */

typedef struct
{
   const char *name;
   slang_type_specifier_type type;
} type_specifier_type_name;

static const type_specifier_type_name type_specifier_type_names[] = {
   {"void", SLANG_SPEC_VOID},
   {"bool", SLANG_SPEC_BOOL},
   {"bvec2", SLANG_SPEC_BVEC2},
   {"bvec3", SLANG_SPEC_BVEC3},
   {"bvec4", SLANG_SPEC_BVEC4},
   {"int", SLANG_SPEC_INT},
   {"ivec2", SLANG_SPEC_IVEC2},
   {"ivec3", SLANG_SPEC_IVEC3},
   {"ivec4", SLANG_SPEC_IVEC4},
   {"float", SLANG_SPEC_FLOAT},
   {"vec2", SLANG_SPEC_VEC2},
   {"vec3", SLANG_SPEC_VEC3},
   {"vec4", SLANG_SPEC_VEC4},
   {"mat2", SLANG_SPEC_MAT2},
   {"mat3", SLANG_SPEC_MAT3},
   {"mat4", SLANG_SPEC_MAT4},
   {"sampler1D", SLANG_SPEC_SAMPLER1D},
   {"sampler2D", SLANG_SPEC_SAMPLER2D},
   {"sampler3D", SLANG_SPEC_SAMPLER3D},
   {"samplerCube", SLANG_SPEC_SAMPLERCUBE},
   {"sampler1DShadow", SLANG_SPEC_SAMPLER1DSHADOW},
   {"sampler2DShadow", SLANG_SPEC_SAMPLER2DSHADOW},
   {"sampler2DRect", SLANG_SPEC_SAMPLER2DRECT},
   {"sampler2DRectShadow", SLANG_SPEC_SAMPLER2DRECTSHADOW},
   {NULL, SLANG_SPEC_VOID}
};

slang_type_specifier_type
slang_type_specifier_type_from_string(const char *name)
{
   const type_specifier_type_name *p = type_specifier_type_names;
   while (p->name != NULL) {
      if (slang_string_compare(p->name, name) == 0)
         break;
      p++;
   }
   return p->type;
}

const char *
slang_type_specifier_type_to_string(slang_type_specifier_type type)
{
   const type_specifier_type_name *p = type_specifier_type_names;
   while (p->name != NULL) {
      if (p->type == type)
         break;
      p++;
   }
   return p->name;
}

/* slang_fully_specified_type */

int
slang_fully_specified_type_construct(slang_fully_specified_type * type)
{
   type->qualifier = SLANG_QUAL_NONE;
   slang_type_specifier_ctr(&type->specifier);
   return 1;
}

void
slang_fully_specified_type_destruct(slang_fully_specified_type * type)
{
   slang_type_specifier_dtr(&type->specifier);
}

int
slang_fully_specified_type_copy(slang_fully_specified_type * x,
                                const slang_fully_specified_type * y)
{
   slang_fully_specified_type z;

   if (!slang_fully_specified_type_construct(&z))
      return 0;
   z.qualifier = y->qualifier;
   if (!slang_type_specifier_copy(&z.specifier, &y->specifier)) {
      slang_fully_specified_type_destruct(&z);
      return 0;
   }
   slang_fully_specified_type_destruct(x);
   *x = z;
   return 1;
}


static slang_variable *
slang_variable_new(void)
{
   slang_variable *v = (slang_variable *) malloc(sizeof(slang_variable));
   if (v) {
      if (!slang_variable_construct(v)) {
         free(v);
         v = NULL;
      }
   }
   return v;
}


static void
slang_variable_delete(slang_variable * var)
{
   slang_variable_destruct(var);
   free(var);
}


/*
 * slang_variable_scope
 */

slang_variable_scope *
_slang_variable_scope_new(slang_variable_scope *parent)
{
   slang_variable_scope *s;
   s = (slang_variable_scope *) _mesa_calloc(sizeof(slang_variable_scope));
   s->outer_scope = parent;
   return s;
}


GLvoid
_slang_variable_scope_ctr(slang_variable_scope * self)
{
   self->variables = NULL;
   self->num_variables = 0;
   self->outer_scope = NULL;
}

void
slang_variable_scope_destruct(slang_variable_scope * scope)
{
   unsigned int i;

   if (!scope)
      return;
   for (i = 0; i < scope->num_variables; i++) {
      if (scope->variables[i])
         slang_variable_delete(scope->variables[i]);
   }
   slang_alloc_free(scope->variables);
   /* do not free scope->outer_scope */
}

int
slang_variable_scope_copy(slang_variable_scope * x,
                          const slang_variable_scope * y)
{
   slang_variable_scope z;
   unsigned int i;

   _slang_variable_scope_ctr(&z);
   z.variables = (slang_variable **)
      _mesa_calloc(y->num_variables * sizeof(slang_variable *));
   if (z.variables == NULL) {
      slang_variable_scope_destruct(&z);
      return 0;
   }
   for (z.num_variables = 0; z.num_variables < y->num_variables;
        z.num_variables++) {
      z.variables[z.num_variables] = slang_variable_new();
      if (!z.variables[z.num_variables]) {
         slang_variable_scope_destruct(&z);
         return 0;
      }
   }
   for (i = 0; i < z.num_variables; i++) {
      if (!slang_variable_copy(z.variables[i], y->variables[i])) {
         slang_variable_scope_destruct(&z);
         return 0;
      }
   }
   z.outer_scope = y->outer_scope;
   slang_variable_scope_destruct(x);
   *x = z;
   return 1;
}


/**
 * Grow the variable list by one.
 * \return  pointer to space for the new variable (will be initialized)
 */
slang_variable *
slang_variable_scope_grow(slang_variable_scope *scope)
{
   const int n = scope->num_variables;
   scope->variables = (slang_variable **)
         slang_alloc_realloc(scope->variables,
                             n * sizeof(slang_variable *),
                             (n + 1) * sizeof(slang_variable *));
   if (!scope->variables)
      return NULL;

   scope->num_variables++;

   scope->variables[n] = slang_variable_new();
   if (!scope->variables[n])
      return NULL;

   return scope->variables[n];
}



/* slang_variable */

int
slang_variable_construct(slang_variable * var)
{
   if (!slang_fully_specified_type_construct(&var->type))
      return 0;
   var->a_name = SLANG_ATOM_NULL;
   var->array_len = 0;
   var->initializer = NULL;
   var->address = ~0;
   var->size = 0;
   var->isTemp = GL_FALSE;
   var->aux = NULL;
   return 1;
}


void
slang_variable_destruct(slang_variable * var)
{
   slang_fully_specified_type_destruct(&var->type);
   if (var->initializer != NULL) {
      slang_operation_destruct(var->initializer);
      slang_alloc_free(var->initializer);
   }
}


int
slang_variable_copy(slang_variable * x, const slang_variable * y)
{
   slang_variable z;

   if (!slang_variable_construct(&z))
      return 0;
   if (!slang_fully_specified_type_copy(&z.type, &y->type)) {
      slang_variable_destruct(&z);
      return 0;
   }
   z.a_name = y->a_name;
   z.array_len = y->array_len;
   if (y->initializer != NULL) {
      z.initializer
         = (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
      if (z.initializer == NULL) {
         slang_variable_destruct(&z);
         return 0;
      }
      if (!slang_operation_construct(z.initializer)) {
         slang_alloc_free(z.initializer);
         slang_variable_destruct(&z);
         return 0;
      }
      if (!slang_operation_copy(z.initializer, y->initializer)) {
         slang_variable_destruct(&z);
         return 0;
      }
   }
   z.address = y->address;
   z.size = y->size;
   slang_variable_destruct(x);
   *x = z;
   return 1;
}


slang_variable *
_slang_locate_variable(const slang_variable_scope * scope,
                       const slang_atom a_name, GLboolean all)
{
   GLuint i;

   for (i = 0; i < scope->num_variables; i++)
      if (a_name == scope->variables[i]->a_name)
         return scope->variables[i];
   if (all && scope->outer_scope != NULL)
      return _slang_locate_variable(scope->outer_scope, a_name, 1);
   return NULL;
}

#if 0
static GLenum
gl_type_from_specifier(const slang_type_specifier * type)
{
   switch (type->type) {
   case SLANG_SPEC_BOOL:
      return GL_BOOL_ARB;
   case SLANG_SPEC_BVEC2:
      return GL_BOOL_VEC2_ARB;
   case SLANG_SPEC_BVEC3:
      return GL_BOOL_VEC3_ARB;
   case SLANG_SPEC_BVEC4:
      return GL_BOOL_VEC4_ARB;
   case SLANG_SPEC_INT:
      return GL_INT;
   case SLANG_SPEC_IVEC2:
      return GL_INT_VEC2_ARB;
   case SLANG_SPEC_IVEC3:
      return GL_INT_VEC3_ARB;
   case SLANG_SPEC_IVEC4:
      return GL_INT_VEC4_ARB;
   case SLANG_SPEC_FLOAT:
      return GL_FLOAT;
   case SLANG_SPEC_VEC2:
      return GL_FLOAT_VEC2_ARB;
   case SLANG_SPEC_VEC3:
      return GL_FLOAT_VEC3_ARB;
   case SLANG_SPEC_VEC4:
      return GL_FLOAT_VEC4_ARB;
   case SLANG_SPEC_MAT2:
      return GL_FLOAT_MAT2_ARB;
   case SLANG_SPEC_MAT3:
      return GL_FLOAT_MAT3_ARB;
   case SLANG_SPEC_MAT4:
      return GL_FLOAT_MAT4_ARB;
   case SLANG_SPEC_SAMPLER1D:
      return GL_SAMPLER_1D_ARB;
   case SLANG_SPEC_SAMPLER2D:
      return GL_SAMPLER_2D_ARB;
   case SLANG_SPEC_SAMPLER3D:
      return GL_SAMPLER_3D_ARB;
   case SLANG_SPEC_SAMPLERCUBE:
      return GL_SAMPLER_CUBE_ARB;
   case SLANG_SPEC_SAMPLER1DShadow:
      return GL_SAMPLER_1D_SHADOW_ARB;
   case SLANG_SPEC_SAMPLER2DShadow:
      return GL_SAMPLER_2D_SHADOW_ARB;
   case SLANG_SPEC_ARRAy:
      return gl_type_from_specifier(type->_array);
   default:
      return GL_FLOAT;
   }
}
#endif