Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / shader / slang / slang_compile.c
index f684f6cc1d5fd211c8d235568f447d6b481603d7..19b8e5172598eca7fd6d5f53bbbe12e9b1371ff6 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5.2
  *
  * Copyright (C) 2005-2006  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2008 VMware, Inc.  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"),
 #include "main/context.h"
 #include "shader/program.h"
 #include "shader/programopt.h"
+#include "shader/prog_optimize.h"
 #include "shader/prog_print.h"
 #include "shader/prog_parameter.h"
-#include "shader/grammar/grammar_mesa.h"
+#include "../../glsl/pp/sl_pp_public.h"
+#include "../../glsl/cl/sl_cl_parse.h"
 #include "slang_codegen.h"
 #include "slang_compile.h"
-#include "slang_preprocess.h"
 #include "slang_storage.h"
 #include "slang_emit.h"
 #include "slang_log.h"
@@ -74,18 +75,6 @@ legal_identifier(slang_atom name)
 }
 
 
-/**
- * Allocate storage for a variable of 'size' bytes from given pool.
- * Return the allocated address for the variable.
- */
-static GLuint
-slang_var_pool_alloc(slang_var_pool * pool, unsigned int size)
-{
-   const GLuint addr = pool->next_addr;
-   pool->next_addr += size;
-   return addr;
-}
-
 /*
  * slang_code_unit
  */
@@ -120,7 +109,6 @@ _slang_code_object_ctr(slang_code_object * self)
    for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
       _slang_code_unit_ctr(&self->builtin[i], self);
    _slang_code_unit_ctr(&self->unit, self);
-   self->varpool.next_addr = 0;
    slang_atom_pool_construct(&self->atompool);
 }
 
@@ -140,7 +128,7 @@ _slang_code_object_dtr(slang_code_object * self)
 
 typedef struct slang_parse_ctx_
 {
-   const byte *I;
+   const unsigned char *I;
    slang_info_log *L;
    int parsing_builtin;
    GLboolean global_scope;   /**< Is object being declared a global? */
@@ -156,13 +144,14 @@ typedef struct slang_output_ctx_
    slang_variable_scope *vars;
    slang_function_scope *funs;
    slang_struct_scope *structs;
-   slang_var_pool *global_pool;
    struct gl_program *program;
+   struct gl_sl_pragmas *pragmas;
    slang_var_table *vartable;
    GLuint default_precision[TYPE_SPECIFIER_COUNT];
    GLboolean allow_precision;
    GLboolean allow_invariant;
    GLboolean allow_centroid;
+   GLboolean allow_array_types;  /* float[] syntax */
 } slang_output_ctx;
 
 /* _slang_compile() */
@@ -194,23 +183,103 @@ parse_identifier(slang_parse_ctx * C)
    return slang_atom_pool_atom(C->atoms, id);
 }
 
+static int
+is_hex_digit(char c)
+{
+   return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+}
+
+static int
+parse_general_number(slang_parse_ctx *ctx, float *number)
+{
+   char *flt = NULL;
+
+   if (*ctx->I == '0') {
+      int value = 0;
+      const unsigned char *pi;
+
+      if (ctx->I[1] == 'x' || ctx->I[1] == 'X') {
+         ctx->I += 2;
+         if (!is_hex_digit(*ctx->I)) {
+            return 0;
+         }
+         do {
+            int digit;
+
+            if (*ctx->I >= '0' && *ctx->I <= '9') {
+               digit = (int)(*ctx->I - '0');
+            } else if (*ctx->I >= 'a' && *ctx->I <= 'f') {
+               digit = (int)(*ctx->I - 'a') + 10;
+            } else {
+               digit = (int)(*ctx->I - 'A') + 10;
+            }
+            value = value * 0x10 + digit;
+            ctx->I++;
+         } while (is_hex_digit(*ctx->I));
+         if (*ctx->I != '\0') {
+            return 0;
+         }
+         ctx->I++;
+         *number = (float)value;
+         return 1;
+      }
+
+      pi = ctx->I;
+      pi++;
+      while (*pi >= '0' && *pi <= '7') {
+         int digit;
+
+         digit = (int)(*pi - '0');
+         value = value * 010 + digit;
+         pi++;
+      }
+      if (*pi == '\0') {
+         pi++;
+         ctx->I = pi;
+         *number = (float)value;
+         return 1;
+      }
+   }
+
+   parse_identifier_str(ctx, &flt);
+   flt = strdup(flt);
+   if (!flt) {
+      return 0;
+   }
+   if (flt[strlen(flt) - 1] == 'f' || flt[strlen(flt) - 1] == 'F') {
+      flt[strlen(flt) - 1] = '\0';
+   }
+   *number = (float)_mesa_strtod(flt, (char **)NULL);
+   free(flt);
+
+   return 1;
+}
+
 static int
 parse_number(slang_parse_ctx * C, int *number)
 {
    const int radix = (int) (*C->I++);
-   *number = 0;
-   while (*C->I != '\0') {
-      int digit;
-      if (*C->I >= '0' && *C->I <= '9')
-         digit = (int) (*C->I - '0');
-      else if (*C->I >= 'A' && *C->I <= 'Z')
-         digit = (int) (*C->I - 'A') + 10;
-      else
-         digit = (int) (*C->I - 'a') + 10;
-      *number = *number * radix + digit;
+
+   if (radix == 1) {
+      float f = 0.0f;
+
+      parse_general_number(C, &f);
+      *number = (int)f;
+   } else {
+      *number = 0;
+      while (*C->I != '\0') {
+         int digit;
+         if (*C->I >= '0' && *C->I <= '9')
+            digit = (int) (*C->I - '0');
+         else if (*C->I >= 'A' && *C->I <= 'Z')
+            digit = (int) (*C->I - 'A') + 10;
+         else
+            digit = (int) (*C->I - 'a') + 10;
+         *number = *number * radix + digit;
+         C->I++;
+      }
       C->I++;
    }
-   C->I++;
    if (*number > 65535)
       slang_info_log_warning(C->L, "%d: literal integer overflow.", *number);
    return 1;
@@ -219,38 +288,43 @@ parse_number(slang_parse_ctx * C, int *number)
 static int
 parse_float(slang_parse_ctx * C, float *number)
 {
-   char *integral = NULL;
-   char *fractional = NULL;
-   char *exponent = NULL;
-   char *whole = NULL;
-
-   parse_identifier_str(C, &integral);
-   parse_identifier_str(C, &fractional);
-   parse_identifier_str(C, &exponent);
-
-   whole = (char *) _slang_alloc((_mesa_strlen(integral) +
-                                  _mesa_strlen(fractional) +
-                                  _mesa_strlen(exponent) + 3) * sizeof(char));
-   if (whole == NULL) {
-      slang_info_log_memory(C->L);
-      RETURN0;
-   }
+   if (*C->I == 1) {
+      C->I++;
+      parse_general_number(C, number);
+   } else {
+      char *integral = NULL;
+      char *fractional = NULL;
+      char *exponent = NULL;
+      char *whole = NULL;
+
+      parse_identifier_str(C, &integral);
+      parse_identifier_str(C, &fractional);
+      parse_identifier_str(C, &exponent);
+
+      whole = (char *) _slang_alloc((_mesa_strlen(integral) +
+                                     _mesa_strlen(fractional) +
+                                     _mesa_strlen(exponent) + 3) * sizeof(char));
+      if (whole == NULL) {
+         slang_info_log_memory(C->L);
+         RETURN0;
+      }
 
-   slang_string_copy(whole, integral);
-   slang_string_concat(whole, ".");
-   slang_string_concat(whole, fractional);
-   slang_string_concat(whole, "E");
-   slang_string_concat(whole, exponent);
+      slang_string_copy(whole, integral);
+      slang_string_concat(whole, ".");
+      slang_string_concat(whole, fractional);
+      slang_string_concat(whole, "E");
+      slang_string_concat(whole, exponent);
 
-   *number = (float) (_mesa_strtod(whole, (char **) NULL));
+      *number = (float) (_mesa_strtod(whole, (char **) NULL));
 
-   _slang_free(whole);
+      _slang_free(whole);
+   }
 
    return 1;
 }
 
 /* revision number - increment after each change affecting emitted output */
-#define REVISION 4
+#define REVISION 5
 
 static int
 check_revision(slang_parse_ctx * C)
@@ -269,6 +343,10 @@ static int parse_expression(slang_parse_ctx *, slang_output_ctx *,
                             slang_operation *);
 static int parse_type_specifier(slang_parse_ctx *, slang_output_ctx *,
                                 slang_type_specifier *);
+static int
+parse_type_array_size(slang_parse_ctx *C,
+                      slang_output_ctx *O,
+                      GLint *array_len);
 
 static GLboolean
 parse_array_len(slang_parse_ctx * C, slang_output_ctx * O, GLuint * len)
@@ -295,7 +373,7 @@ parse_array_len(slang_parse_ctx * C, slang_output_ctx * O, GLuint * len)
       result = GL_TRUE;
       *len = (GLint) array_size.literal[0];
    } else if (array_size.type == SLANG_OPER_IDENTIFIER) {
-      slang_variable *var = _slang_locate_variable(array_size.locals, array_size.a_id, GL_TRUE);
+      slang_variable *var = _slang_variable_locate(array_size.locals, array_size.a_id, GL_TRUE);
       if (!var) {
          slang_info_log_error(C->L, "undefined variable '%s'",
                               (char *) array_size.a_id);
@@ -340,12 +418,26 @@ calculate_var_size(slang_parse_ctx * C, slang_output_ctx * O,
    return GL_TRUE;
 }
 
+static void
+promote_type_to_array(slang_parse_ctx *C,
+                      slang_fully_specified_type *type,
+                      GLint array_len)
+{
+   slang_type_specifier *baseType =
+      slang_type_specifier_new(type->specifier.type, NULL, NULL);
+
+   type->specifier.type = SLANG_SPEC_ARRAY;
+   type->specifier._array = baseType;
+   type->array_len = array_len;
+}
+
+
 static GLboolean
 convert_to_array(slang_parse_ctx * C, slang_variable * var,
                  const slang_type_specifier * sp)
 {
-   /* sized array - mark it as array, copy the specifier to the array element and
-    * parse the expression */
+   /* sized array - mark it as array, copy the specifier to the array element
+    * and parse the expression */
    var->type.specifier.type = SLANG_SPEC_ARRAY;
    var->type.specifier._array = (slang_type_specifier *)
       _slang_alloc(sizeof(slang_type_specifier));
@@ -365,7 +457,8 @@ convert_to_array(slang_parse_ctx * C, slang_variable * var,
 static GLboolean
 parse_struct_field_var(slang_parse_ctx * C, slang_output_ctx * O,
                        slang_variable * var, slang_atom a_name,
-                       const slang_type_specifier * sp)
+                       const slang_type_specifier * sp,
+                       GLuint array_len)
 {
    var->a_name = a_name;
    if (var->a_name == SLANG_ATOM_NULL)
@@ -373,10 +466,19 @@ parse_struct_field_var(slang_parse_ctx * C, slang_output_ctx * O,
 
    switch (*C->I++) {
    case FIELD_NONE:
-      if (!slang_type_specifier_copy(&var->type.specifier, sp))
-         return GL_FALSE;
+      if (array_len != -1) {
+         if (!convert_to_array(C, var, sp))
+            return GL_FALSE;
+         var->array_len = array_len;
+      }
+      else {
+         if (!slang_type_specifier_copy(&var->type.specifier, sp))
+            return GL_FALSE;
+      }
       break;
    case FIELD_ARRAY:
+      if (array_len != -1)
+         return GL_FALSE;
       if (!convert_to_array(C, var, sp))
          return GL_FALSE;
       if (!parse_array_len(C, O, &var->array_len))
@@ -394,10 +496,13 @@ parse_struct_field(slang_parse_ctx * C, slang_output_ctx * O,
                    slang_struct * st, slang_type_specifier * sp)
 {
    slang_output_ctx o = *O;
+   GLint array_len;
 
    o.structs = st->structs;
    if (!parse_type_specifier(C, &o, sp))
       RETURN0;
+   if (!parse_type_array_size(C, &o, &array_len))
+      RETURN0;
 
    do {
       slang_atom a_name;
@@ -407,12 +512,12 @@ parse_struct_field(slang_parse_ctx * C, slang_output_ctx * O,
          RETURN0;
       }
       a_name = parse_identifier(C);
-      if (_slang_locate_variable(st->fields, a_name, GL_FALSE)) {
+      if (_slang_variable_locate(st->fields, a_name, GL_FALSE)) {
          slang_info_log_error(C->L, "duplicate field '%s'", (char *) a_name);
          RETURN0;
       }
 
-      if (!parse_struct_field_var(C, &o, var, a_name, sp))
+      if (!parse_struct_field_var(C, &o, var, a_name, sp, array_len))
          RETURN0;
    }
    while (*C->I++ != FIELD_NONE);
@@ -747,6 +852,31 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    return 1;
 }
 
+#define TYPE_SPECIFIER_NONARRAY 0
+#define TYPE_SPECIFIER_ARRAY    1
+
+static int
+parse_type_array_size(slang_parse_ctx *C,
+                      slang_output_ctx *O,
+                      GLint *array_len)
+{
+   GLuint size;
+
+   switch (*C->I++) {
+   case TYPE_SPECIFIER_NONARRAY:
+      *array_len = -1; /* -1 = not an array */
+      break;
+   case TYPE_SPECIFIER_ARRAY:
+      if (!parse_array_len(C, O, &size))
+         RETURN0;
+      *array_len = (GLint) size;
+      break;
+   default:
+      assert(0);
+      RETURN0;
+   }
+   return 1;
+}
 
 #define PRECISION_DEFAULT 0
 #define PRECISION_LOW     1
@@ -795,6 +925,9 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
    if (!parse_type_specifier(C, O, &type->specifier))
       RETURN0;
 
+   if (!parse_type_array_size(C, O, &type->array_len))
+      RETURN0;
+
    if (!O->allow_invariant && type->variant == SLANG_INVARIANT) {
       slang_info_log_error(C->L,
          "'invariant' keyword not allowed (perhaps set #version 120)");
@@ -838,6 +971,16 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
       }
    }
 
+   if (!O->allow_array_types && type->array_len >= 0) {
+      slang_info_log_error(C->L, "first-class array types not allowed");
+      RETURN0;
+   }
+
+   if (type->array_len >= 0) {
+      /* convert type to array type (ex: convert "int" to "array of int" */
+      promote_type_to_array(C, type, type->array_len);
+   }
+
    return 1;
 }
 
@@ -905,6 +1048,7 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
 #define OP_POSTINCREMENT 60
 #define OP_POSTDECREMENT 61
 #define OP_PRECISION 62
+#define OP_METHOD 63
 
 
 /**
@@ -938,13 +1082,17 @@ static int
 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
                 slang_operation * oper)
 {
+   int op;
+
    oper->locals->outer_scope = O->vars;
-   switch (*C->I++) {
+
+   op = *C->I++;
+   switch (op) {
    case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
       /* parse child statements, do not create new variable scope */
       oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
       while (*C->I != OP_END)
-         if (!parse_child_operation(C, O, oper, 1))
+         if (!parse_child_operation(C, O, oper, GL_TRUE))
             RETURN0;
       C->I++;
       break;
@@ -956,7 +1104,7 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
          oper->type = SLANG_OPER_BLOCK_NEW_SCOPE;
          o.vars = oper->locals;
          while (*C->I != OP_END)
-            if (!parse_child_operation(C, &o, oper, 1))
+            if (!parse_child_operation(C, &o, oper, GL_TRUE))
                RETURN0;
          C->I++;
       }
@@ -986,9 +1134,14 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
             }
             for (i = first_var; i < O->vars->num_variables; i++) {
                slang_operation *o = &oper->children[i - first_var];
+               slang_variable *var = O->vars->variables[i];
                o->type = SLANG_OPER_VARIABLE_DECL;
                o->locals->outer_scope = O->vars;
-               o->a_id = O->vars->variables[i]->a_name;
+               o->a_id = var->a_name;
+
+               /* new/someday...
+               calculate_var_size(C, O, var);
+               */
 
                if (!legal_identifier(o->a_id)) {
                   slang_info_log_error(C->L, "illegal variable name '%s'",
@@ -1008,7 +1161,7 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
       if (oper->a_id == SLANG_ATOM_NULL)
          RETURN0;
       while (*C->I != OP_END) {
-         if (!parse_child_operation(C, O, oper, 0))
+         if (!parse_child_operation(C, O, oper, GL_FALSE))
             RETURN0;
       }
       C->I++;
@@ -1024,21 +1177,21 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
       break;
    case OP_RETURN:
       oper->type = SLANG_OPER_RETURN;
-      if (!parse_child_operation(C, O, oper, 0))
+      if (!parse_child_operation(C, O, oper, GL_FALSE))
          RETURN0;
       break;
    case OP_EXPRESSION:
       oper->type = SLANG_OPER_EXPRESSION;
-      if (!parse_child_operation(C, O, oper, 0))
+      if (!parse_child_operation(C, O, oper, GL_FALSE))
          RETURN0;
       break;
    case OP_IF:
       oper->type = SLANG_OPER_IF;
-      if (!parse_child_operation(C, O, oper, 0))
+      if (!parse_child_operation(C, O, oper, GL_FALSE))
          RETURN0;
-      if (!parse_child_operation(C, O, oper, 1))
+      if (!parse_child_operation(C, O, oper, GL_TRUE))
          RETURN0;
-      if (!parse_child_operation(C, O, oper, 1))
+      if (!parse_child_operation(C, O, oper, GL_TRUE))
          RETURN0;
       break;
    case OP_WHILE:
@@ -1047,17 +1200,17 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
 
          oper->type = SLANG_OPER_WHILE;
          o.vars = oper->locals;
-         if (!parse_child_operation(C, &o, oper, 1))
+         if (!parse_child_operation(C, &o, oper, GL_TRUE))
             RETURN0;
-         if (!parse_child_operation(C, &o, oper, 1))
+         if (!parse_child_operation(C, &o, oper, GL_TRUE))
             RETURN0;
       }
       break;
    case OP_DO:
       oper->type = SLANG_OPER_DO;
-      if (!parse_child_operation(C, O, oper, 1))
+      if (!parse_child_operation(C, O, oper, GL_TRUE))
          RETURN0;
-      if (!parse_child_operation(C, O, oper, 0))
+      if (!parse_child_operation(C, O, oper, GL_FALSE))
          RETURN0;
       break;
    case OP_FOR:
@@ -1066,13 +1219,13 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
 
          oper->type = SLANG_OPER_FOR;
          o.vars = oper->locals;
-         if (!parse_child_operation(C, &o, oper, 1))
+         if (!parse_child_operation(C, &o, oper, GL_TRUE))
             RETURN0;
-         if (!parse_child_operation(C, &o, oper, 1))
+         if (!parse_child_operation(C, &o, oper, GL_TRUE))
             RETURN0;
-         if (!parse_child_operation(C, &o, oper, 0))
+         if (!parse_child_operation(C, &o, oper, GL_FALSE))
             RETURN0;
-         if (!parse_child_operation(C, &o, oper, 1))
+         if (!parse_child_operation(C, &o, oper, GL_TRUE))
             RETURN0;
       }
       break;
@@ -1087,6 +1240,7 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
       }
       break;
    default:
+      /*printf("Unexpected operation %d\n", op);*/
       RETURN0;
    }
    return 1;
@@ -1134,6 +1288,9 @@ is_constructor_name(const char *name, slang_atom a_name,
    return slang_struct_scope_find(structs, a_name, 1) != NULL;
 }
 
+#define FUNCTION_CALL_NONARRAY 0
+#define FUNCTION_CALL_ARRAY    1
+
 static int
 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
                  slang_operation * oper)
@@ -1345,16 +1502,25 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
          if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
             RETURN0;
          break;
-      case OP_CALL:
-         op->type = SLANG_OPER_CALL;
+      case OP_METHOD:
+         op->type = SLANG_OPER_METHOD;
+         op->a_obj = parse_identifier(C);
+         if (op->a_obj == SLANG_ATOM_NULL)
+            RETURN0;
+
          op->a_id = parse_identifier(C);
          if (op->a_id == SLANG_ATOM_NULL)
             RETURN0;
+
+         assert(*C->I == OP_END);
+         C->I++;
+
          while (*C->I != OP_END)
-            if (!parse_child_operation(C, O, op, 0))
+            if (!parse_child_operation(C, O, op, GL_FALSE))
                RETURN0;
          C->I++;
-
+#if 0
+         /* don't lookup the method (not yet anyway) */
          if (!C->parsing_builtin
              && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
             const char *id;
@@ -1365,6 +1531,78 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
                RETURN0;
             }
          }
+#endif
+         break;
+      case OP_CALL:
+         {
+            GLboolean array_constructor = GL_FALSE;
+            GLint array_constructor_size = 0;
+
+            op->type = SLANG_OPER_CALL;
+            op->a_id = parse_identifier(C);
+            if (op->a_id == SLANG_ATOM_NULL)
+               RETURN0;
+            switch (*C->I++) {
+            case FUNCTION_CALL_NONARRAY:
+               /* Nothing to do. */
+               break;
+            case FUNCTION_CALL_ARRAY:
+               /* Calling an array constructor. For example:
+                *   float[3](1.1, 2.2, 3.3);
+                */
+               if (!O->allow_array_types) {
+                  slang_info_log_error(C->L,
+                                       "array constructors not allowed "
+                                       "in this GLSL version");
+                  RETURN0;
+               }
+               else {
+                  /* parse the array constructor size */
+                  slang_operation array_size;
+                  array_constructor = GL_TRUE;
+                  slang_operation_construct(&array_size);
+                  if (!parse_expression(C, O, &array_size)) {
+                     slang_operation_destruct(&array_size);
+                     return GL_FALSE;
+                  }
+                  if (array_size.type != SLANG_OPER_LITERAL_INT) {
+                     slang_info_log_error(C->L,
+                        "constructor array size is not an integer");
+                     slang_operation_destruct(&array_size);
+                     RETURN0;
+                  }
+                  array_constructor_size = (int) array_size.literal[0];
+                  op->array_constructor = GL_TRUE;
+                  slang_operation_destruct(&array_size);
+               }
+               break;
+            default:
+               assert(0);
+               RETURN0;
+            }
+            while (*C->I != OP_END)
+               if (!parse_child_operation(C, O, op, GL_FALSE))
+                  RETURN0;
+            C->I++;
+
+            if (array_constructor &&
+                array_constructor_size != op->num_children) {
+               slang_info_log_error(C->L, "number of parameters to array"
+                                    " constructor does not match array size");
+               RETURN0;
+            }
+
+            if (!C->parsing_builtin
+                && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
+               const char *id;
+
+               id = slang_atom_pool_id(C->atoms, op->a_id);
+               if (!is_constructor_name(id, op->a_id, O->structs)) {
+                  slang_info_log_error(C->L, "%s: undeclared function name.", id);
+                  RETURN0;
+               }
+            }
+         }
          break;
       case OP_FIELD:
          op->type = SLANG_OPER_FIELD;
@@ -1455,34 +1693,59 @@ parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
    /* parse parameter's type specifier and name */
    if (!parse_type_specifier(C, O, &param->type.specifier))
       RETURN0;
+   if (!parse_type_array_size(C, O, &param->type.array_len))
+      RETURN0;
    param->a_name = parse_identifier(C);
    if (param->a_name == SLANG_ATOM_NULL)
       RETURN0;
 
+   /* first-class array
+    */
+   if (param->type.array_len >= 0) {
+      slang_type_specifier p;
+
+      slang_type_specifier_ctr(&p);
+      if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
+         slang_type_specifier_dtr(&p);
+         RETURN0;
+      }
+      if (!convert_to_array(C, param, &p)) {
+         slang_type_specifier_dtr(&p);
+         RETURN0;
+      }
+      slang_type_specifier_dtr(&p);
+      param->array_len = param->type.array_len;
+   }
+
    /* if the parameter is an array, parse its size (the size must be
     * explicitly defined
     */
    if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
       slang_type_specifier p;
 
+      if (param->type.array_len >= 0) {
+         slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
+         RETURN0;
+      }
       slang_type_specifier_ctr(&p);
       if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
          slang_type_specifier_dtr(&p);
-         return GL_FALSE;
+         RETURN0;
       }
       if (!convert_to_array(C, param, &p)) {
          slang_type_specifier_dtr(&p);
-         return GL_FALSE;
+         RETURN0;
       }
       slang_type_specifier_dtr(&p);
       if (!parse_array_len(C, O, &param->array_len))
-         return GL_FALSE;
+         RETURN0;
    }
 
+#if 0
    /* calculate the parameter size */
    if (!calculate_var_size(C, O, param))
-      return GL_FALSE;
-
+      RETURN0;
+#endif
    /* TODO: allocate the local address here? */
    return 1;
 }
@@ -1775,6 +2038,7 @@ static int
 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
                       const slang_fully_specified_type * type)
 {
+   GET_CURRENT_CONTEXT(ctx); /* a hack */
    slang_variable *var;
    slang_atom a_name;
 
@@ -1785,7 +2049,7 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
    a_name = parse_identifier(C);
 
    /* check if name is already in this scope */
-   if (_slang_locate_variable(O->vars, a_name, GL_FALSE)) {
+   if (_slang_variable_locate(O->vars, a_name, GL_FALSE)) {
       slang_info_log_error(C->L,
                    "declaration of '%s' conflicts with previous declaration",
                    (char *) a_name);
@@ -1804,6 +2068,7 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
    var->type.centroid = type->centroid;
    var->type.precision = type->precision;
    var->type.variant = type->variant;
+   var->type.array_len = type->array_len;
    var->a_name = a_name;
    if (var->a_name == SLANG_ATOM_NULL)
       RETURN0;
@@ -1816,8 +2081,15 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       break;
    case VARIABLE_INITIALIZER:
       /* initialized variable - copy the specifier and parse the expression */
-      if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
-         RETURN0;
+      if (0 && type->array_len >= 0) {
+         /* The type was something like "float[4]" */
+         convert_to_array(C, var, &type->specifier);
+         var->array_len = type->array_len;
+      }
+      else {
+         if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
+            RETURN0;
+      }
       var->initializer =
          (slang_operation *) _slang_alloc(sizeof(slang_operation));
       if (var->initializer == NULL) {
@@ -1835,12 +2107,21 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       break;
    case VARIABLE_ARRAY_UNKNOWN:
       /* unsized array - mark it as array and copy the specifier to
-         the array element
-      */
+       * the array element
+       */
+      if (type->array_len >= 0) {
+         slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
+         RETURN0;
+      }
       if (!convert_to_array(C, var, &type->specifier))
          return GL_FALSE;
       break;
    case VARIABLE_ARRAY_EXPLICIT:
+      if (type->array_len >= 0) {
+         /* the user is trying to do something like: float[2] x[3]; */
+         slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
+         RETURN0;
+      }
       if (!convert_to_array(C, var, &type->specifier))
          return GL_FALSE;
       if (!parse_array_len(C, O, &var->array_len))
@@ -1856,20 +2137,22 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
             && var->array_len == 0)) {
       if (!calculate_var_size(C, O, var))
          return GL_FALSE;
-      var->address = slang_var_pool_alloc(O->global_pool, var->size);
    }
 
    /* emit code for global var decl */
    if (C->global_scope) {
       slang_assemble_ctx A;
+      memset(&A, 0, sizeof(slang_assemble_ctx));
       A.atoms = C->atoms;
       A.space.funcs = O->funs;
       A.space.structs = O->structs;
       A.space.vars = O->vars;
       A.program = O->program;
+      A.pragmas = O->pragmas;
       A.vartable = O->vartable;
       A.log = C->L;
       A.curFuncEndLabel = NULL;
+      A.EmitContReturn = ctx->Shader.EmitContReturn;
       if (!_slang_codegen_global_variable(&A, var, C->type))
          RETURN0;
    }
@@ -1878,7 +2161,7 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
    if (C->global_scope) {
       if (var->initializer != NULL) {
          slang_assemble_ctx A;
-
+         memset(&A, 0, sizeof(slang_assemble_ctx));
          A.atoms = C->atoms;
          A.space.funcs = O->funs;
          A.space.structs = O->structs;
@@ -1965,6 +2248,12 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
                                            (O->funs->num_functions + 1)
                                            * sizeof(slang_function));
       if (O->funs->functions == NULL) {
+         /* Make sure that there are no functions marked, as the
+          * allocation is currently NULL, in order to avoid
+          * a potental segfault as we clean up later.
+          */
+         O->funs->num_functions = 0;
+
          slang_info_log_memory(C->L);
          slang_function_destruct(&parsed_func);
          return GL_FALSE;
@@ -1989,10 +2278,8 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
          }
 
          /* destroy the existing function declaration and replace it
-          * with the new one, remember to save the fixup table
+          * with the new one
           */
-         parsed_func.fixups = found_func->fixups;
-         slang_fixup_table_init(&found_func->fixups);
          slang_function_destruct(found_func);
          *found_func = parsed_func;
       }
@@ -2158,8 +2445,8 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    o.funs = &unit->funs;
    o.structs = &unit->structs;
    o.vars = &unit->vars;
-   o.global_pool = &unit->object->varpool;
    o.program = shader ? shader->Program : NULL;
+   o.pragmas = shader ? &shader->Pragmas : NULL;
    o.vartable = _slang_new_var_table(maxRegs);
    _slang_push_var_table(o.vartable);
 
@@ -2181,6 +2468,8 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
 #endif
    init_default_precision(&o, unit->type);
 
+   /* allow 'float[]' keyword? */
+   o.allow_array_types = (C->version >= 120) ? GL_TRUE : GL_FALSE;
 
    /* parse individual functions and declarations */
    while (*C->I != EXTERNAL_NULL) {
@@ -2220,13 +2509,15 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    if (mainFunc) {
       /* assemble (generate code) for main() */
       slang_assemble_ctx A;
-
+      memset(&A, 0, sizeof(slang_assemble_ctx));
       A.atoms = C->atoms;
       A.space.funcs = o.funs;
       A.space.structs = o.structs;
       A.space.vars = o.vars;
       A.program = o.program;
+      A.pragmas = &shader->Pragmas;
       A.vartable = o.vartable;
+      A.EmitContReturn = ctx->Shader.EmitContReturn;
       A.log = C->L;
 
       /* main() takes no parameters */
@@ -2238,6 +2529,8 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
       _slang_codegen_function(&A, mainFunc);
 
       shader->Main = GL_TRUE; /* this shader defines main() */
+
+      shader->UnresolvedRefs = A.UnresolvedRefs;
    }
 
    _slang_pop_var_table(o.vartable);
@@ -2247,7 +2540,7 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
 }
 
 static GLboolean
-compile_binary(const byte * prod, slang_code_unit * unit,
+compile_binary(const unsigned char * prod, slang_code_unit * unit,
                GLuint version,
                slang_unit_type type, slang_info_log * infolog,
                slang_code_unit * builtin, slang_code_unit * downlink,
@@ -2280,15 +2573,56 @@ compile_binary(const byte * prod, slang_code_unit * unit,
 }
 
 static GLboolean
-compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
-                     slang_unit_type type, slang_info_log * infolog,
-                     slang_code_unit * builtin,
-                     struct gl_shader *shader)
+compile_with_grammar(const char *source,
+                     slang_code_unit *unit,
+                     slang_unit_type type,
+                     slang_info_log *infolog,
+                     slang_code_unit *builtin,
+                     struct gl_shader *shader,
+                     const struct gl_extensions *extensions,
+                     struct gl_sl_pragmas *pragmas,
+                     unsigned int shader_type,
+                     unsigned int parsing_builtin)
 {
-   byte *prod;
-   GLuint size, start, version;
-   slang_string preprocessed;
-   GLuint maxVersion;
+   struct sl_pp_purify_options options;
+   struct sl_pp_context *context;
+   unsigned char *prod;
+   GLuint size;
+   unsigned int version;
+   unsigned int maxVersion;
+   int result;
+   char errmsg[200] = "";
+   unsigned int errline = 0;
+
+   memset(&options, 0, sizeof(options));
+
+   context = sl_pp_context_create(source, &options);
+   if (!context) {
+      slang_info_log_error(infolog, "out of memory");
+      return GL_FALSE;
+   }
+
+   if (sl_pp_version(context, &version)) {
+      slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
+      sl_pp_context_destroy(context);
+      return GL_FALSE;
+   }
+
+   if (sl_pp_context_add_extension(context, "ARB_draw_buffers", "GL_ARB_draw_buffers") ||
+       sl_pp_context_add_extension(context, "ARB_texture_rectangle", "GL_ARB_texture_rectangle")) {
+      slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
+      sl_pp_context_destroy(context);
+      return GL_FALSE;
+   }
+
+#if FEATURE_es2_glsl
+   if (sl_pp_context_add_predefined(context, "GL_ES", "1") ||
+       sl_pp_context_add_predefined(context, "GL_FRAGMENT_PRECISION_HIGH", "1")) {
+      slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
+      sl_pp_context_destroy(context);
+      return GL_FALSE;
+   }
+#endif
 
 #if FEATURE_ARB_shading_language_120
    maxVersion = 120;
@@ -2298,35 +2632,30 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
    maxVersion = 110;
 #endif
 
-   /* First retrieve the version number. */
-   if (!_slang_preprocess_version(source, &version, &start, infolog))
-      return GL_FALSE;
-
-   if (version > maxVersion) {
+   if (version > maxVersion ||
+       (version != 100 && version != 110 && version != 120)) {
       slang_info_log_error(infolog,
                            "language version %.2f is not supported.",
                            version * 0.01);
-      return GL_FALSE;
-   }
-
-   /* Now preprocess the source string. */
-   slang_string_init(&preprocessed);
-   if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
-      slang_string_free(&preprocessed);
-      slang_info_log_error(infolog, "failed to preprocess the source.");
+      sl_pp_context_destroy(context);
       return GL_FALSE;
    }
 
    /* Finally check the syntax and generate its binary representation. */
-   if (!grammar_fast_check(id,
-                           (const byte *) (slang_string_cstr(&preprocessed)),
-                           &prod, &size, 65536)) {
-      char buf[1024];
-      GLint pos;
-
-      slang_string_free(&preprocessed);
-      grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
-      slang_info_log_error(infolog, buf);
+   result = sl_cl_compile(context,
+                          shader_type,
+                          parsing_builtin,
+                          &prod,
+                          &size,
+                          errmsg,
+                          sizeof(errmsg));
+
+   sl_pp_context_destroy(context);
+
+   if (result) {
+      /*GLint pos;*/
+
+      slang_info_log_error(infolog, errmsg);
       /* syntax error (possibly in library code) */
 #if 0
       {
@@ -2340,75 +2669,65 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
 #endif
       return GL_FALSE;
    }
-   slang_string_free(&preprocessed);
 
    /* Syntax is okay - translate it to internal representation. */
    if (!compile_binary(prod, unit, version, type, infolog, builtin,
                        &builtin[SLANG_BUILTIN_TOTAL - 1],
                        shader)) {
-      grammar_alloc_free(prod);
+      free(prod);
       return GL_FALSE;
    }
-   grammar_alloc_free(prod);
+   free(prod);
    return GL_TRUE;
 }
 
-LONGSTRING static const char *slang_shader_syn =
-#include "library/slang_shader_syn.h"
-   ;
-
-static const byte slang_core_gc[] = {
+static const unsigned char slang_core_gc[] = {
 #include "library/slang_core_gc.h"
 };
 
-static const byte slang_120_core_gc[] = {
+static const unsigned char slang_120_core_gc[] = {
 #include "library/slang_120_core_gc.h"
 };
 
-static const byte slang_120_fragment_gc[] = {
+static const unsigned char slang_120_fragment_gc[] = {
 #include "library/slang_builtin_120_fragment_gc.h"
 };
 
-static const byte slang_common_builtin_gc[] = {
+static const unsigned char slang_common_builtin_gc[] = {
 #include "library/slang_common_builtin_gc.h"
 };
 
-static const byte slang_fragment_builtin_gc[] = {
+static const unsigned char slang_fragment_builtin_gc[] = {
 #include "library/slang_fragment_builtin_gc.h"
 };
 
-static const byte slang_vertex_builtin_gc[] = {
+static const unsigned char slang_vertex_builtin_gc[] = {
 #include "library/slang_vertex_builtin_gc.h"
 };
 
 static GLboolean
-compile_object(grammar * id, const char *source, slang_code_object * object,
-               slang_unit_type type, slang_info_log * infolog,
-               struct gl_shader *shader)
+compile_object(const char *source,
+               slang_code_object *object,
+               slang_unit_type type,
+               slang_info_log *infolog,
+               struct gl_shader *shader,
+               const struct gl_extensions *extensions,
+               struct gl_sl_pragmas *pragmas)
 {
    slang_code_unit *builtins = NULL;
    GLuint base_version = 110;
-
-   /* load GLSL grammar */
-   *id = grammar_load_from_text((const byte *) (slang_shader_syn));
-   if (*id == 0) {
-      byte buf[1024];
-      int pos;
-
-      grammar_get_last_error(buf, 1024, &pos);
-      slang_info_log_error(infolog, (const char *) (buf));
-      return GL_FALSE;
-   }
+   unsigned int shader_type;
+   unsigned int parsing_builtin;
 
    /* set shader type - the syntax is slightly different for different shaders */
-   if (type == SLANG_UNIT_FRAGMENT_SHADER
-       || type == SLANG_UNIT_FRAGMENT_BUILTIN)
-      grammar_set_reg8(*id, (const byte *) "shader_type", 1);
-   else
-      grammar_set_reg8(*id, (const byte *) "shader_type", 2);
+   if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_FRAGMENT_BUILTIN) {
+      shader_type = 1;
+   } else {
+      shader_type = 2;
+   }
 
    /* enable language extensions */
-   grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
+   parsing_builtin = 1;
 
    /* if parsing user-specified shader, load built-in library */
    if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
@@ -2474,16 +2793,24 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
 
       /* disable language extensions */
 #if NEW_SLANG /* allow-built-ins */
-      grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
+      parsing_builtin = 1;
 #else
-      grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
+      parsing_builtin = 0;
 #endif
       builtins = object->builtin;
    }
 
    /* compile the actual shader - pass-in built-in library for external shader */
-   return compile_with_grammar(*id, source, &object->unit, type, infolog,
-                               builtins, shader);
+   return compile_with_grammar(source,
+                               &object->unit,
+                               type,
+                               infolog,
+                               builtins,
+                               shader,
+                               extensions,
+                               pragmas,
+                               shader_type,
+                               parsing_builtin);
 }
 
 
@@ -2492,9 +2819,6 @@ compile_shader(GLcontext *ctx, slang_code_object * object,
                slang_unit_type type, slang_info_log * infolog,
                struct gl_shader *shader)
 {
-   GLboolean success;
-   grammar id = 0;
-
 #if 0 /* for debug */
    _mesa_printf("********* COMPILE SHADER ***********\n");
    _mesa_printf("%s\n", shader->Source);
@@ -2506,13 +2830,13 @@ compile_shader(GLcontext *ctx, slang_code_object * object,
    _slang_code_object_dtr(object);
    _slang_code_object_ctr(object);
 
-   success = compile_object(&id, shader->Source, object, type, infolog, shader);
-   if (id != 0)
-      grammar_destroy(id);
-   if (!success)
-      return GL_FALSE;
-
-   return GL_TRUE;
+   return compile_object(shader->Source,
+                         object,
+                         type,
+                         infolog,
+                         shader,
+                         &ctx->Extensions,
+                         &shader->Pragmas);
 }
 
 
@@ -2524,6 +2848,7 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
    slang_info_log info_log;
    slang_code_object obj;
    slang_unit_type type;
+   GLenum progTarget;
 
    if (shader->Type == GL_VERTEX_SHADER) {
       type = SLANG_UNIT_VERTEX_SHADER;
@@ -2540,17 +2865,18 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
 
    shader->Main = GL_FALSE;
 
-   if (!shader->Program) {
-      GLenum progTarget;
-      if (shader->Type == GL_VERTEX_SHADER)
-         progTarget = GL_VERTEX_PROGRAM_ARB;
-      else
-         progTarget = GL_FRAGMENT_PROGRAM_ARB;
-      shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
-      shader->Program->Parameters = _mesa_new_parameter_list();
-      shader->Program->Varying = _mesa_new_parameter_list();
-      shader->Program->Attributes = _mesa_new_parameter_list();
-   }
+   /* free the shader's old instructions, etc */
+   _mesa_reference_program(ctx, &shader->Program, NULL);
+
+   /* allocate new GPU program, parameter lists, etc. */
+   if (shader->Type == GL_VERTEX_SHADER)
+      progTarget = GL_VERTEX_PROGRAM_ARB;
+   else
+      progTarget = GL_FRAGMENT_PROGRAM_ARB;
+   shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
+   shader->Program->Parameters = _mesa_new_parameter_list();
+   shader->Program->Varying = _mesa_new_parameter_list();
+   shader->Program->Attributes = _mesa_new_parameter_list();
 
    slang_info_log_construct(&info_log);
    _slang_code_object_ctr(&obj);
@@ -2593,6 +2919,29 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
    _mesa_print_program(shader->Program);
 #endif
 
+   shader->CompileStatus = success;
+
+   if (success) {
+      if (shader->Pragmas.Optimize &&
+          (ctx->Shader.Flags & GLSL_NO_OPT) == 0) {
+         _mesa_optimize_program(ctx, shader->Program);
+      }
+      if ((ctx->Shader.Flags & GLSL_NOP_VERT) &&
+          shader->Program->Target == GL_VERTEX_PROGRAM_ARB) {
+         _mesa_nop_vertex_program(ctx,
+                                  (struct gl_vertex_program *) shader->Program);
+      }
+      if ((ctx->Shader.Flags & GLSL_NOP_FRAG) &&
+          shader->Program->Target == GL_FRAGMENT_PROGRAM_ARB) {
+         _mesa_nop_fragment_program(ctx,
+                                (struct gl_fragment_program *) shader->Program);
+      }
+   }
+
+   if (ctx->Shader.Flags & GLSL_LOG) {
+      _mesa_write_shader_to_file(shader);
+   }
+
    return success;
 }