Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1901 serge 1
/*
2
 * Copyright © 2010 Intel Corporation
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice (including the next
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
 
24
#include 
25
 
26
#include "ir_reader.h"
27
#include "glsl_parser_extras.h"
28
#include "glsl_types.h"
29
#include "s_expression.h"
30
 
31
const static bool debug = false;
32
 
33
static void ir_read_error(_mesa_glsl_parse_state *, s_expression *,
34
			  const char *fmt, ...);
35
static const glsl_type *read_type(_mesa_glsl_parse_state *, s_expression *);
36
 
37
static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
38
			        s_expression *);
39
static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
40
				  bool skip_body);
41
static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
42
			      s_list *, bool skip_body);
43
 
44
static void read_instructions(_mesa_glsl_parse_state *, exec_list *,
45
			      s_expression *, ir_loop *);
46
static ir_instruction *read_instruction(_mesa_glsl_parse_state *,
47
				        s_expression *, ir_loop *);
48
static ir_variable *read_declaration(_mesa_glsl_parse_state *, s_list *);
49
static ir_if *read_if(_mesa_glsl_parse_state *, s_list *, ir_loop *);
50
static ir_loop *read_loop(_mesa_glsl_parse_state *st, s_list *list);
51
static ir_return *read_return(_mesa_glsl_parse_state *, s_list *);
52
 
53
static ir_rvalue *read_rvalue(_mesa_glsl_parse_state *, s_expression *);
54
static ir_assignment *read_assignment(_mesa_glsl_parse_state *, s_list *);
55
static ir_expression *read_expression(_mesa_glsl_parse_state *, s_list *);
56
static ir_call *read_call(_mesa_glsl_parse_state *, s_list *);
57
static ir_swizzle *read_swizzle(_mesa_glsl_parse_state *, s_list *);
58
static ir_constant *read_constant(_mesa_glsl_parse_state *, s_list *);
59
static ir_texture *read_texture(_mesa_glsl_parse_state *, s_list *);
60
 
61
static ir_dereference *read_dereference(_mesa_glsl_parse_state *,
62
				        s_expression *);
63
static ir_dereference_variable *
64
read_var_ref(_mesa_glsl_parse_state *, s_list *);
65
static ir_dereference_array *
66
read_array_ref(_mesa_glsl_parse_state *, s_list *);
67
static ir_dereference_record *
68
read_record_ref(_mesa_glsl_parse_state *, s_list *);
69
 
70
void
71
_mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
72
		   const char *src, bool scan_for_protos)
73
{
74
   s_expression *expr = s_expression::read_expression(state, src);
75
   if (expr == NULL) {
76
      ir_read_error(state, NULL, "couldn't parse S-Expression.");
77
      return;
78
   }
79
 
80
   if (scan_for_protos) {
81
      scan_for_prototypes(state, instructions, expr);
82
      if (state->error)
83
	 return;
84
   }
85
 
86
   read_instructions(state, instructions, expr, NULL);
87
   ralloc_free(expr);
88
 
89
   if (debug)
90
      validate_ir_tree(instructions);
91
}
92
 
93
static void
94
ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
95
	      const char *fmt, ...)
96
{
97
   va_list ap;
98
 
99
   state->error = true;
100
 
101
   if (state->current_function != NULL)
102
      ralloc_asprintf_append(&state->info_log, "In function %s:\n",
103
			     state->current_function->function_name());
104
   ralloc_strcat(&state->info_log, "error: ");
105
 
106
   va_start(ap, fmt);
107
   ralloc_vasprintf_append(&state->info_log, fmt, ap);
108
   va_end(ap);
109
   ralloc_strcat(&state->info_log, "\n");
110
 
111
   if (expr != NULL) {
112
      ralloc_strcat(&state->info_log, "...in this context:\n   ");
113
      expr->print();
114
      ralloc_strcat(&state->info_log, "\n\n");
115
   }
116
}
117
 
118
static const glsl_type *
119
read_type(_mesa_glsl_parse_state *st, s_expression *expr)
120
{
121
   s_list *list = SX_AS_LIST(expr);
122
   if (list != NULL) {
123
      s_symbol *type_sym = SX_AS_SYMBOL(list->subexpressions.get_head());
124
      if (type_sym == NULL) {
125
	 ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
126
	 return NULL;
127
      }
128
      if (strcmp(type_sym->value(), "array") == 0) {
129
	 if (list->length() != 3) {
130
	    ir_read_error(st, expr, "expected type (array  )");
131
	    return NULL;
132
	 }
133
 
134
	 // Read base type
135
	 s_expression *base_expr = (s_expression*) type_sym->next;
136
	 const glsl_type *base_type = read_type(st, base_expr);
137
	 if (base_type == NULL) {
138
	    ir_read_error(st, NULL, "when reading base type of array");
139
	    return NULL;
140
	 }
141
 
142
	 // Read array size
143
	 s_int *size = SX_AS_INT(base_expr->next);
144
	 if (size == NULL) {
145
	    ir_read_error(st, expr, "found non-integer array size");
146
	    return NULL;
147
	 }
148
 
149
	 return glsl_type::get_array_instance(base_type, size->value());
150
      } else if (strcmp(type_sym->value(), "struct") == 0) {
151
	 assert(false); // FINISHME
152
      } else {
153
	 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
154
				 "found (%s ...)", type_sym->value());
155
	 return NULL;
156
      }
157
   }
158
 
159
   s_symbol *type_sym = SX_AS_SYMBOL(expr);
160
   if (type_sym == NULL) {
161
      ir_read_error(st, expr, "expected  (symbol or list)");
162
      return NULL;
163
   }
164
 
165
   const glsl_type *type = st->symbols->get_type(type_sym->value());
166
   if (type == NULL)
167
      ir_read_error(st, expr, "invalid type: %s", type_sym->value());
168
 
169
   return type;
170
}
171
 
172
 
173
static void
174
scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
175
		    s_expression *expr)
176
{
177
   s_list *list = SX_AS_LIST(expr);
178
   if (list == NULL) {
179
      ir_read_error(st, expr, "Expected ( ...); found an atom.");
180
      return;
181
   }
182
 
183
   foreach_iter(exec_list_iterator, it, list->subexpressions) {
184
      s_list *sub = SX_AS_LIST(it.get());
185
      if (sub == NULL)
186
	 continue; // not a (function ...); ignore it.
187
 
188
      s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
189
      if (tag == NULL || strcmp(tag->value(), "function") != 0)
190
	 continue; // not a (function ...); ignore it.
191
 
192
      ir_function *f = read_function(st, sub, true);
193
      if (f == NULL)
194
	 return;
195
      instructions->push_tail(f);
196
   }
197
}
198
 
199
static ir_function *
200
read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
201
{
202
   void *ctx = st;
203
   bool added = false;
204
   if (list->length() < 3) {
205
      ir_read_error(st, list, "Expected (function  (signature ...) ...)");
206
      return NULL;
207
   }
208
 
209
   s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
210
   if (name == NULL) {
211
      ir_read_error(st, list, "Expected (function  ...)");
212
      return NULL;
213
   }
214
 
215
   ir_function *f = st->symbols->get_function(name->value());
216
   if (f == NULL) {
217
      f = new(ctx) ir_function(name->value());
218
      added = st->symbols->add_function(f);
219
      assert(added);
220
   }
221
 
222
   exec_list_iterator it = list->subexpressions.iterator();
223
   it.next(); // skip "function" tag
224
   it.next(); // skip function name
225
   for (/* nothing */; it.has_next(); it.next()) {
226
      s_list *siglist = SX_AS_LIST(it.get());
227
      if (siglist == NULL) {
228
	 ir_read_error(st, list, "Expected (function (signature ...) ...)");
229
	 return NULL;
230
      }
231
 
232
      s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
233
      if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
234
	 ir_read_error(st, siglist, "Expected (signature ...)");
235
	 return NULL;
236
      }
237
 
238
      read_function_sig(st, f, siglist, skip_body);
239
   }
240
   return added ? f : NULL;
241
}
242
 
243
static void
244
read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
245
		  bool skip_body)
246
{
247
   void *ctx = st;
248
   if (list->length() != 4) {
249
      ir_read_error(st, list, "Expected (signature  (parameters ...) "
250
			      "( ...))");
251
      return;
252
   }
253
 
254
   s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
255
   const glsl_type *return_type = read_type(st, type_expr);
256
   if (return_type == NULL)
257
      return;
258
 
259
   s_list *paramlist = SX_AS_LIST(type_expr->next);
260
   s_list *body_list = SX_AS_LIST(type_expr->next->next);
261
   if (paramlist == NULL || body_list == NULL) {
262
      ir_read_error(st, list, "Expected (signature  (parameters ...) "
263
			      "( ...))");
264
      return;
265
   }
266
   s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
267
   if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
268
      ir_read_error(st, paramlist, "Expected (parameters ...)");
269
      return;
270
   }
271
 
272
   // Read the parameters list into a temporary place.
273
   exec_list hir_parameters;
274
   st->symbols->push_scope();
275
 
276
   exec_list_iterator it = paramlist->subexpressions.iterator();
277
   for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
278
      s_list *decl = SX_AS_LIST(it.get());
279
      ir_variable *var = read_declaration(st, decl);
280
      if (var == NULL)
281
	 return;
282
 
283
      hir_parameters.push_tail(var);
284
   }
285
 
286
   ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
287
   if (sig == NULL && skip_body) {
288
      /* If scanning for prototypes, generate a new signature. */
289
      sig = new(ctx) ir_function_signature(return_type);
290
      sig->is_builtin = true;
291
      f->add_signature(sig);
292
   } else if (sig != NULL) {
293
      const char *badvar = sig->qualifiers_match(&hir_parameters);
294
      if (badvar != NULL) {
295
	 ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
296
		       "don't match prototype", f->name, badvar);
297
	 return;
298
      }
299
 
300
      if (sig->return_type != return_type) {
301
	 ir_read_error(st, list, "function `%s' return type doesn't "
302
		       "match prototype", f->name);
303
	 return;
304
      }
305
   } else {
306
      /* No prototype for this body exists - skip it. */
307
      st->symbols->pop_scope();
308
      return;
309
   }
310
   assert(sig != NULL);
311
 
312
   sig->replace_parameters(&hir_parameters);
313
 
314
   if (!skip_body && !body_list->subexpressions.is_empty()) {
315
      if (sig->is_defined) {
316
	 ir_read_error(st, list, "function %s redefined", f->name);
317
	 return;
318
      }
319
      st->current_function = sig;
320
      read_instructions(st, &sig->body, body_list, NULL);
321
      st->current_function = NULL;
322
      sig->is_defined = true;
323
   }
324
 
325
   st->symbols->pop_scope();
326
}
327
 
328
static void
329
read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
330
		  s_expression *expr, ir_loop *loop_ctx)
331
{
332
   // Read in a list of instructions
333
   s_list *list = SX_AS_LIST(expr);
334
   if (list == NULL) {
335
      ir_read_error(st, expr, "Expected ( ...); found an atom.");
336
      return;
337
   }
338
 
339
   foreach_iter(exec_list_iterator, it, list->subexpressions) {
340
      s_expression *sub = (s_expression*) it.get();
341
      ir_instruction *ir = read_instruction(st, sub, loop_ctx);
342
      if (ir != NULL) {
343
	 /* Global variable declarations should be moved to the top, before
344
	  * any functions that might use them.  Functions are added to the
345
	  * instruction stream when scanning for prototypes, so without this
346
	  * hack, they always appear before variable declarations.
347
	  */
348
	 if (st->current_function == NULL && ir->as_variable() != NULL)
349
	    instructions->push_head(ir);
350
	 else
351
	    instructions->push_tail(ir);
352
      }
353
   }
354
}
355
 
356
 
357
static ir_instruction *
358
read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
359
	         ir_loop *loop_ctx)
360
{
361
   void *ctx = st;
362
   s_symbol *symbol = SX_AS_SYMBOL(expr);
363
   if (symbol != NULL) {
364
      if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
365
	 return new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
366
      if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
367
	 return new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
368
   }
369
 
370
   s_list *list = SX_AS_LIST(expr);
371
   if (list == NULL || list->subexpressions.is_empty()) {
372
      ir_read_error(st, expr, "Invalid instruction.\n");
373
      return NULL;
374
   }
375
 
376
   s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
377
   if (tag == NULL) {
378
      ir_read_error(st, expr, "expected instruction tag");
379
      return NULL;
380
   }
381
 
382
   ir_instruction *inst = NULL;
383
   if (strcmp(tag->value(), "declare") == 0) {
384
      inst = read_declaration(st, list);
385
   } else if (strcmp(tag->value(), "assign") == 0) {
386
      inst = read_assignment(st, list);
387
   } else if (strcmp(tag->value(), "if") == 0) {
388
      inst = read_if(st, list, loop_ctx);
389
   } else if (strcmp(tag->value(), "loop") == 0) {
390
      inst = read_loop(st, list);
391
   } else if (strcmp(tag->value(), "return") == 0) {
392
      inst = read_return(st, list);
393
   } else if (strcmp(tag->value(), "function") == 0) {
394
      inst = read_function(st, list, false);
395
   } else {
396
      inst = read_rvalue(st, list);
397
      if (inst == NULL)
398
	 ir_read_error(st, NULL, "when reading instruction");
399
   }
400
   return inst;
401
}
402
 
403
 
404
static ir_variable *
405
read_declaration(_mesa_glsl_parse_state *st, s_list *list)
406
{
407
   void *ctx = st;
408
   if (list->length() != 4) {
409
      ir_read_error(st, list, "expected (declare ()  "
410
			      ")");
411
      return NULL;
412
   }
413
 
414
   s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
415
   if (quals == NULL) {
416
      ir_read_error(st, list, "expected a list of variable qualifiers");
417
      return NULL;
418
   }
419
 
420
   s_expression *type_expr = (s_expression*) quals->next;
421
   const glsl_type *type = read_type(st, type_expr);
422
   if (type == NULL)
423
      return NULL;
424
 
425
   s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
426
   if (var_name == NULL) {
427
      ir_read_error(st, list, "expected variable name, found non-symbol");
428
      return NULL;
429
   }
430
 
431
   ir_variable *var = new(ctx) ir_variable(type, var_name->value(),
432
					   ir_var_auto);
433
 
434
   foreach_iter(exec_list_iterator, it, quals->subexpressions) {
435
      s_symbol *qualifier = SX_AS_SYMBOL(it.get());
436
      if (qualifier == NULL) {
437
	 ir_read_error(st, list, "qualifier list must contain only symbols");
438
	 delete var;
439
	 return NULL;
440
      }
441
 
442
      // FINISHME: Check for duplicate/conflicting qualifiers.
443
      if (strcmp(qualifier->value(), "centroid") == 0) {
444
	 var->centroid = 1;
445
      } else if (strcmp(qualifier->value(), "invariant") == 0) {
446
	 var->invariant = 1;
447
      } else if (strcmp(qualifier->value(), "uniform") == 0) {
448
	 var->mode = ir_var_uniform;
449
      } else if (strcmp(qualifier->value(), "auto") == 0) {
450
	 var->mode = ir_var_auto;
451
      } else if (strcmp(qualifier->value(), "in") == 0) {
452
	 var->mode = ir_var_in;
453
      } else if (strcmp(qualifier->value(), "out") == 0) {
454
	 var->mode = ir_var_out;
455
      } else if (strcmp(qualifier->value(), "inout") == 0) {
456
	 var->mode = ir_var_inout;
457
      } else if (strcmp(qualifier->value(), "smooth") == 0) {
458
	 var->interpolation = ir_var_smooth;
459
      } else if (strcmp(qualifier->value(), "flat") == 0) {
460
	 var->interpolation = ir_var_flat;
461
      } else if (strcmp(qualifier->value(), "noperspective") == 0) {
462
	 var->interpolation = ir_var_noperspective;
463
      } else {
464
	 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
465
	 delete var;
466
	 return NULL;
467
      }
468
   }
469
 
470
   // Add the variable to the symbol table
471
   st->symbols->add_variable(var);
472
 
473
   return var;
474
}
475
 
476
 
477
static ir_if *
478
read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
479
{
480
   void *ctx = st;
481
   if (list->length() != 4) {
482
      ir_read_error(st, list, "expected (if  ( ...) "
483
                          "( ...))");
484
      return NULL;
485
   }
486
 
487
   s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
488
   ir_rvalue *condition = read_rvalue(st, cond_expr);
489
   if (condition == NULL) {
490
      ir_read_error(st, NULL, "when reading condition of (if ...)");
491
      return NULL;
492
   }
493
 
494
   s_expression *then_expr = (s_expression*) cond_expr->next;
495
   s_expression *else_expr = (s_expression*) then_expr->next;
496
 
497
   ir_if *iff = new(ctx) ir_if(condition);
498
 
499
   read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
500
   read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
501
   if (st->error) {
502
      delete iff;
503
      iff = NULL;
504
   }
505
   return iff;
506
}
507
 
508
 
509
static ir_loop *
510
read_loop(_mesa_glsl_parse_state *st, s_list *list)
511
{
512
   void *ctx = st;
513
   if (list->length() != 6) {
514
      ir_read_error(st, list, "expected (loop    "
515
			      " )");
516
      return NULL;
517
   }
518
 
519
   s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
520
   s_expression *from_expr  = (s_expression*) count_expr->next;
521
   s_expression *to_expr    = (s_expression*) from_expr->next;
522
   s_expression *inc_expr   = (s_expression*) to_expr->next;
523
   s_expression *body_expr  = (s_expression*) inc_expr->next;
524
 
525
   // FINISHME: actually read the count/from/to fields.
526
 
527
   ir_loop *loop = new(ctx) ir_loop;
528
   read_instructions(st, &loop->body_instructions, body_expr, loop);
529
   if (st->error) {
530
      delete loop;
531
      loop = NULL;
532
   }
533
   return loop;
534
}
535
 
536
 
537
static ir_return *
538
read_return(_mesa_glsl_parse_state *st, s_list *list)
539
{
540
   void *ctx = st;
541
   if (list->length() != 2) {
542
      ir_read_error(st, list, "expected (return )");
543
      return NULL;
544
   }
545
 
546
   s_expression *expr = (s_expression*) list->subexpressions.head->next;
547
 
548
   ir_rvalue *retval = read_rvalue(st, expr);
549
   if (retval == NULL) {
550
      ir_read_error(st, NULL, "when reading return value");
551
      return NULL;
552
   }
553
 
554
   return new(ctx) ir_return(retval);
555
}
556
 
557
 
558
static ir_rvalue *
559
read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
560
{
561
   s_list *list = SX_AS_LIST(expr);
562
   if (list == NULL || list->subexpressions.is_empty())
563
      return NULL;
564
 
565
   s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
566
   if (tag == NULL) {
567
      ir_read_error(st, expr, "expected rvalue tag");
568
      return NULL;
569
   }
570
 
571
   ir_rvalue *rvalue = read_dereference(st, list);
572
   if (rvalue != NULL || st->error)
573
      return rvalue;
574
   else if (strcmp(tag->value(), "swiz") == 0) {
575
      rvalue = read_swizzle(st, list);
576
   } else if (strcmp(tag->value(), "expression") == 0) {
577
      rvalue = read_expression(st, list);
578
   } else if (strcmp(tag->value(), "call") == 0) {
579
      rvalue = read_call(st, list);
580
   } else if (strcmp(tag->value(), "constant") == 0) {
581
      rvalue = read_constant(st, list);
582
   } else {
583
      rvalue = read_texture(st, list);
584
      if (rvalue == NULL && !st->error)
585
	 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
586
   }
587
 
588
   return rvalue;
589
}
590
 
591
static ir_assignment *
592
read_assignment(_mesa_glsl_parse_state *st, s_list *list)
593
{
594
   void *ctx = st;
595
   if (list->length() != 5) {
596
      ir_read_error(st, list, "expected (assign  () "
597
			      " )");
598
      return NULL;
599
   }
600
 
601
   s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
602
   s_list       *mask_list = SX_AS_LIST(cond_expr->next);
603
   s_expression *lhs_expr  = (s_expression*) cond_expr->next->next;
604
   s_expression *rhs_expr  = (s_expression*) lhs_expr->next;
605
 
606
   ir_rvalue *condition = read_rvalue(st, cond_expr);
607
   if (condition == NULL) {
608
      ir_read_error(st, NULL, "when reading condition of assignment");
609
      return NULL;
610
   }
611
 
612
   if (mask_list == NULL || mask_list->length() > 1) {
613
      ir_read_error(st, mask_list, "expected () or ()");
614
      return NULL;
615
   }
616
 
617
   unsigned mask = 0;
618
   if (mask_list->length() == 1) {
619
      s_symbol *mask_symbol = SX_AS_SYMBOL(mask_list->subexpressions.head);
620
      if (mask_symbol == NULL) {
621
	 ir_read_error(st, list, "expected a write mask; found non-symbol");
622
	 return NULL;
623
      }
624
 
625
      const char *mask_str = mask_symbol->value();
626
      unsigned mask_length = strlen(mask_str);
627
      if (mask_length > 4) {
628
	 ir_read_error(st, list, "invalid write mask: %s", mask_str);
629
	 return NULL;
630
      }
631
 
632
      const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
633
 
634
      for (unsigned i = 0; i < mask_length; i++) {
635
	 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
636
	    ir_read_error(st, list, "write mask contains invalid character: %c",
637
			  mask_str[i]);
638
	    return NULL;
639
	 }
640
	 mask |= 1 << idx_map[mask_str[i] - 'w'];
641
      }
642
   }
643
 
644
   ir_dereference *lhs = read_dereference(st, lhs_expr);
645
   if (lhs == NULL) {
646
      ir_read_error(st, NULL, "when reading left-hand side of assignment");
647
      return NULL;
648
   }
649
 
650
   ir_rvalue *rhs = read_rvalue(st, rhs_expr);
651
   if (rhs == NULL) {
652
      ir_read_error(st, NULL, "when reading right-hand side of assignment");
653
      return NULL;
654
   }
655
 
656
   if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
657
      ir_read_error(st, list, "non-zero write mask required.");
658
      return NULL;
659
   }
660
 
661
   return new(ctx) ir_assignment(lhs, rhs, condition, mask);
662
}
663
 
664
static ir_call *
665
read_call(_mesa_glsl_parse_state *st, s_list *list)
666
{
667
   void *ctx = st;
668
   if (list->length() != 3) {
669
      ir_read_error(st, list, "expected (call  ( ...))");
670
      return NULL;
671
   }
672
 
673
   s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
674
   s_list *params = SX_AS_LIST(list->subexpressions.head->next->next);
675
   if (name == NULL || params == NULL) {
676
      ir_read_error(st, list, "expected (call  ( ...))");
677
      return NULL;
678
   }
679
 
680
   exec_list parameters;
681
 
682
   foreach_iter(exec_list_iterator, it, params->subexpressions) {
683
      s_expression *expr = (s_expression*) it.get();
684
      ir_rvalue *param = read_rvalue(st, expr);
685
      if (param == NULL) {
686
	 ir_read_error(st, list, "when reading parameter to function call");
687
	 return NULL;
688
      }
689
      parameters.push_tail(param);
690
   }
691
 
692
   ir_function *f = st->symbols->get_function(name->value());
693
   if (f == NULL) {
694
      ir_read_error(st, list, "found call to undefined function %s",
695
		    name->value());
696
      return NULL;
697
   }
698
 
699
   ir_function_signature *callee = f->matching_signature(¶meters);
700
   if (callee == NULL) {
701
      ir_read_error(st, list, "couldn't find matching signature for function "
702
                    "%s", name->value());
703
      return NULL;
704
   }
705
 
706
   return new(ctx) ir_call(callee, ¶meters);
707
}
708
 
709
static ir_expression *
710
read_expression(_mesa_glsl_parse_state *st, s_list *list)
711
{
712
   void *ctx = st;
713
   const unsigned list_length = list->length();
714
   if (list_length < 4) {
715
      ir_read_error(st, list, "expected (expression   "
716
			      " [])");
717
      return NULL;
718
   }
719
 
720
   s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
721
   const glsl_type *type = read_type(st, type_expr);
722
   if (type == NULL)
723
      return NULL;
724
 
725
   /* Read the operator */
726
   s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
727
   if (op_sym == NULL) {
728
      ir_read_error(st, list, "expected operator, found non-symbol");
729
      return NULL;
730
   }
731
 
732
   ir_expression_operation op = ir_expression::get_operator(op_sym->value());
733
   if (op == (ir_expression_operation) -1) {
734
      ir_read_error(st, list, "invalid operator: %s", op_sym->value());
735
      return NULL;
736
   }
737
 
738
   /* Now that we know the operator, check for the right number of operands */
739
   if (ir_expression::get_num_operands(op) == 2) {
740
      if (list_length != 5) {
741
	 ir_read_error(st, list, "expected (expression  %s  "
742
				 " )", op_sym->value());
743
	 return NULL;
744
      }
745
   } else {
746
      if (list_length != 4) {
747
	 ir_read_error(st, list, "expected (expression  %s )",
748
		       op_sym->value());
749
	 return NULL;
750
      }
751
   }
752
 
753
   s_expression *exp1 = (s_expression*) (op_sym->next);
754
   ir_rvalue *arg1 = read_rvalue(st, exp1);
755
   if (arg1 == NULL) {
756
      ir_read_error(st, NULL, "when reading first operand of %s",
757
		    op_sym->value());
758
      return NULL;
759
   }
760
 
761
   ir_rvalue *arg2 = NULL;
762
   if (ir_expression::get_num_operands(op) == 2) {
763
      s_expression *exp2 = (s_expression*) (exp1->next);
764
      arg2 = read_rvalue(st, exp2);
765
      if (arg2 == NULL) {
766
	 ir_read_error(st, NULL, "when reading second operand of %s",
767
		       op_sym->value());
768
	 return NULL;
769
      }
770
   }
771
 
772
   return new(ctx) ir_expression(op, type, arg1, arg2);
773
}
774
 
775
static ir_swizzle *
776
read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
777
{
778
   if (list->length() != 3) {
779
      ir_read_error(st, list, "expected (swiz  )");
780
      return NULL;
781
   }
782
 
783
   s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
784
   if (swiz == NULL) {
785
      ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
786
      return NULL;
787
   }
788
 
789
   if (strlen(swiz->value()) > 4) {
790
      ir_read_error(st, list, "expected a valid swizzle; found %s",
791
		    swiz->value());
792
      return NULL;
793
   }
794
 
795
   s_expression *sub = (s_expression*) swiz->next;
796
   ir_rvalue *rvalue = read_rvalue(st, sub);
797
   if (rvalue == NULL)
798
      return NULL;
799
 
800
   ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
801
				       rvalue->type->vector_elements);
802
   if (ir == NULL)
803
      ir_read_error(st, list, "invalid swizzle");
804
 
805
   return ir;
806
}
807
 
808
static ir_constant *
809
read_constant(_mesa_glsl_parse_state *st, s_list *list)
810
{
811
   void *ctx = st;
812
   if (list->length() != 3) {
813
      ir_read_error(st, list, "expected (constant  (...))");
814
      return NULL;
815
   }
816
 
817
   s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
818
   const glsl_type *type = read_type(st, type_expr);
819
   if (type == NULL)
820
      return NULL;
821
 
822
   s_list *values = SX_AS_LIST(type_expr->next);
823
   if (values == NULL) {
824
      ir_read_error(st, list, "expected (constant  (...))");
825
      return NULL;
826
   }
827
 
828
   if (type->is_array()) {
829
      const unsigned elements_supplied = values->length();
830
      if (elements_supplied != type->length) {
831
	 ir_read_error(st, values, "expected exactly %u array elements, "
832
		       "given %u", type->length, elements_supplied);
833
	 return NULL;
834
      }
835
 
836
      exec_list elements;
837
      foreach_iter(exec_list_iterator, it, values->subexpressions) {
838
	 s_expression *expr = (s_expression *) it.get();
839
	 s_list *elt = SX_AS_LIST(expr);
840
	 if (elt == NULL) {
841
	    ir_read_error(st, expr, "expected (constant ...) array element");
842
	    return NULL;
843
	 }
844
 
845
	 ir_constant *ir_elt = read_constant(st, elt);
846
	 if (ir_elt == NULL)
847
	    return NULL;
848
	 elements.push_tail(ir_elt);
849
      }
850
      return new(ctx) ir_constant(type, &elements);
851
   }
852
 
853
   const glsl_type *const base_type = type->get_base_type();
854
 
855
   ir_constant_data data = { { 0 } };
856
 
857
   // Read in list of values (at most 16).
858
   int k = 0;
859
   foreach_iter(exec_list_iterator, it, values->subexpressions) {
860
      if (k >= 16) {
861
	 ir_read_error(st, values, "expected at most 16 numbers");
862
	 return NULL;
863
      }
864
 
865
      s_expression *expr = (s_expression*) it.get();
866
 
867
      if (base_type->base_type == GLSL_TYPE_FLOAT) {
868
	 s_number *value = SX_AS_NUMBER(expr);
869
	 if (value == NULL) {
870
	    ir_read_error(st, values, "expected numbers");
871
	    return NULL;
872
	 }
873
	 data.f[k] = value->fvalue();
874
      } else {
875
	 s_int *value = SX_AS_INT(expr);
876
	 if (value == NULL) {
877
	    ir_read_error(st, values, "expected integers");
878
	    return NULL;
879
	 }
880
 
881
	 switch (base_type->base_type) {
882
	 case GLSL_TYPE_UINT: {
883
	    data.u[k] = value->value();
884
	    break;
885
	 }
886
	 case GLSL_TYPE_INT: {
887
	    data.i[k] = value->value();
888
	    break;
889
	 }
890
	 case GLSL_TYPE_BOOL: {
891
	    data.b[k] = value->value();
892
	    break;
893
	 }
894
	 default:
895
	    ir_read_error(st, values, "unsupported constant type");
896
	    return NULL;
897
	 }
898
      }
899
      ++k;
900
   }
901
 
902
   return new(ctx) ir_constant(type, &data);
903
}
904
 
905
static ir_dereference *
906
read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
907
{
908
   s_list *list = SX_AS_LIST(expr);
909
   if (list == NULL || list->subexpressions.is_empty())
910
      return NULL;
911
 
912
   s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
913
   assert(tag != NULL);
914
 
915
   if (strcmp(tag->value(), "var_ref") == 0)
916
      return read_var_ref(st, list);
917
   if (strcmp(tag->value(), "array_ref") == 0)
918
      return read_array_ref(st, list);
919
   if (strcmp(tag->value(), "record_ref") == 0)
920
      return read_record_ref(st, list);
921
   return NULL;
922
}
923
 
924
static ir_dereference_variable *
925
read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
926
{
927
   void *ctx = st;
928
   if (list->length() != 2) {
929
      ir_read_error(st, list, "expected (var_ref )");
930
      return NULL;
931
   }
932
   s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
933
   if (var_name == NULL) {
934
      ir_read_error(st, list, "expected (var_ref )");
935
      return NULL;
936
   }
937
 
938
   ir_variable *var = st->symbols->get_variable(var_name->value());
939
   if (var == NULL) {
940
      ir_read_error(st, list, "undeclared variable: %s", var_name->value());
941
      return NULL;
942
   }
943
 
944
   return new(ctx) ir_dereference_variable(var);
945
}
946
 
947
static ir_dereference_array *
948
read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
949
{
950
   void *ctx = st;
951
   if (list->length() != 3) {
952
      ir_read_error(st, list, "expected (array_ref  )");
953
      return NULL;
954
   }
955
 
956
   s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
957
   ir_rvalue *subject = read_rvalue(st, subj_expr);
958
   if (subject == NULL) {
959
      ir_read_error(st, NULL, "when reading the subject of an array_ref");
960
      return NULL;
961
   }
962
 
963
   s_expression *idx_expr = (s_expression*) subj_expr->next;
964
   ir_rvalue *idx = read_rvalue(st, idx_expr);
965
   return new(ctx) ir_dereference_array(subject, idx);
966
}
967
 
968
static ir_dereference_record *
969
read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
970
{
971
   void *ctx = st;
972
   if (list->length() != 3) {
973
      ir_read_error(st, list, "expected (record_ref  )");
974
      return NULL;
975
   }
976
 
977
   s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
978
   ir_rvalue *subject = read_rvalue(st, subj_expr);
979
   if (subject == NULL) {
980
      ir_read_error(st, NULL, "when reading the subject of a record_ref");
981
      return NULL;
982
   }
983
 
984
   s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
985
   if (field == NULL) {
986
      ir_read_error(st, list, "expected (record_ref ... )");
987
      return NULL;
988
   }
989
   return new(ctx) ir_dereference_record(subject, field->value());
990
}
991
 
992
static bool
993
valid_texture_list_length(ir_texture_opcode op, s_list *list)
994
{
995
   unsigned required_length = 7;
996
   if (op == ir_txf)
997
      required_length = 5;
998
   else if (op == ir_tex)
999
      required_length = 6;
1000
 
1001
   return list->length() == required_length;
1002
}
1003
 
1004
static ir_texture *
1005
read_texture(_mesa_glsl_parse_state *st, s_list *list)
1006
{
1007
   void *ctx = st;
1008
   s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
1009
   assert(tag != NULL);
1010
 
1011
   ir_texture_opcode op = ir_texture::get_opcode(tag->value());
1012
   if (op == (ir_texture_opcode) -1)
1013
      return NULL;
1014
 
1015
   if (!valid_texture_list_length(op, list)) {
1016
      ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
1017
      return NULL;
1018
   }
1019
 
1020
   ir_texture *tex = new(ctx) ir_texture(op);
1021
 
1022
   // Read sampler (must be a deref)
1023
   s_expression *sampler_expr = (s_expression *) tag->next;
1024
   ir_dereference *sampler = read_dereference(st, sampler_expr);
1025
   if (sampler == NULL) {
1026
      ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
1027
      return NULL;
1028
   }
1029
   tex->set_sampler(sampler);
1030
 
1031
   // Read coordinate (any rvalue)
1032
   s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
1033
   tex->coordinate = read_rvalue(st, coordinate_expr);
1034
   if (tex->coordinate == NULL) {
1035
      ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
1036
		    tag->value());
1037
      return NULL;
1038
   }
1039
 
1040
   // Read texel offset, i.e. (0 0 0)
1041
   s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
1042
   if (offset_list == NULL || offset_list->length() != 3) {
1043
      ir_read_error(st, offset_list, "expected (  )");
1044
      return NULL;
1045
   }
1046
   s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
1047
   s_int *offset_y = SX_AS_INT(offset_list->subexpressions.head->next);
1048
   s_int *offset_z = SX_AS_INT(offset_list->subexpressions.head->next->next);
1049
   if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
1050
      ir_read_error(st, offset_list, "expected (  )");
1051
      return NULL;
1052
   }
1053
   tex->offsets[0] = offset_x->value();
1054
   tex->offsets[1] = offset_y->value();
1055
   tex->offsets[2] = offset_z->value();
1056
 
1057
   if (op == ir_txf) {
1058
      s_expression *lod_expr = (s_expression *) offset_list->next;
1059
      tex->lod_info.lod = read_rvalue(st, lod_expr);
1060
      if (tex->lod_info.lod == NULL) {
1061
	 ir_read_error(st, NULL, "when reading LOD in (txf ...)");
1062
	 return NULL;
1063
      }
1064
   } else {
1065
      s_expression *proj_expr = (s_expression *) offset_list->next;
1066
      s_int *proj_as_int = SX_AS_INT(proj_expr);
1067
      if (proj_as_int && proj_as_int->value() == 1) {
1068
	 tex->projector = NULL;
1069
      } else {
1070
	 tex->projector = read_rvalue(st, proj_expr);
1071
	 if (tex->projector == NULL) {
1072
	    ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
1073
	                  tag->value());
1074
	    return NULL;
1075
	 }
1076
      }
1077
 
1078
      s_list *shadow_list = SX_AS_LIST(proj_expr->next);
1079
      if (shadow_list == NULL) {
1080
	 ir_read_error(st, NULL, "shadow comparitor must be a list");
1081
	 return NULL;
1082
      }
1083
      if (shadow_list->subexpressions.is_empty()) {
1084
	 tex->shadow_comparitor= NULL;
1085
      } else {
1086
	 tex->shadow_comparitor = read_rvalue(st, shadow_list);
1087
	 if (tex->shadow_comparitor == NULL) {
1088
	    ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
1089
			  tag->value());
1090
	    return NULL;
1091
	 }
1092
      }
1093
      s_expression *lod_expr = (s_expression *) shadow_list->next;
1094
 
1095
      switch (op) {
1096
      case ir_txb:
1097
	 tex->lod_info.bias = read_rvalue(st, lod_expr);
1098
	 if (tex->lod_info.bias == NULL) {
1099
	    ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
1100
	    return NULL;
1101
	 }
1102
	 break;
1103
      case ir_txl:
1104
	 tex->lod_info.lod = read_rvalue(st, lod_expr);
1105
	 if (tex->lod_info.lod == NULL) {
1106
	    ir_read_error(st, NULL, "when reading LOD in (txl ...)");
1107
	    return NULL;
1108
	 }
1109
	 break;
1110
      case ir_txd: {
1111
	 s_list *lod_list = SX_AS_LIST(lod_expr);
1112
	 if (lod_list->length() != 2) {
1113
	    ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
1114
	    return NULL;
1115
	 }
1116
	 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1117
	 s_expression *dy_expr = (s_expression *) dx_expr->next;
1118
 
1119
	 tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
1120
	 if (tex->lod_info.grad.dPdx == NULL) {
1121
	    ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
1122
	    return NULL;
1123
	 }
1124
	 tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
1125
	 if (tex->lod_info.grad.dPdy == NULL) {
1126
	    ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
1127
	    return NULL;
1128
	 }
1129
	 break;
1130
      }
1131
      default:
1132
	 // tex doesn't have any extra parameters and txf was handled earlier.
1133
	 break;
1134
      };
1135
   }
1136
   return tex;
1137
}