gallium: Fix typo in define name.
[mesa.git] / src / mesa / shader / slang / slang_compile.c
index dc2e680e9914a76b9bc30ea3274fb8627e4ceb33..294e46235c20a58d4d960bc73edc9bd5c48dd20f 100644 (file)
  * \author Michal Krol
  */
 
-#include "imports.h"
-#include "context.h"
-#include "program.h"
-#include "prog_parameter.h"
-#include "grammar_mesa.h"
+#include "main/imports.h"
+#include "main/context.h"
+#include "shader/program.h"
+#include "shader/programopt.h"
+#include "shader/prog_print.h"
+#include "shader/prog_parameter.h"
+#include "shader/grammar/grammar_mesa.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"
+#include "slang_mem.h"
 #include "slang_vartable.h"
 #include "slang_simplify.h"
 
  */
 
 
+/** re-defined below, should be the same though */
+#define TYPE_SPECIFIER_COUNT 32
+
+
+/**
+ * Check if the given identifier is legal.
+ */
+static GLboolean
+legal_identifier(slang_atom name)
+{
+   /* "gl_" is a reserved prefix */
+   if (_mesa_strncmp((char *) name, "gl_", 3) == 0) {
+      return GL_FALSE;
+   }
+   return GL_TRUE;
+}
+
 
 /**
  * Allocate storage for a variable of 'size' bytes from given pool.
@@ -126,6 +146,7 @@ typedef struct slang_parse_ctx_
    GLboolean global_scope;   /**< Is object being declared a global? */
    slang_atom_pool *atoms;
    slang_unit_type type;     /**< Vertex vs. Fragment */
+   GLuint version;           /**< user-specified (or default) #version */
 } slang_parse_ctx;
 
 /* slang_output_ctx */
@@ -138,6 +159,7 @@ typedef struct slang_output_ctx_
    slang_var_pool *global_pool;
    struct gl_program *program;
    slang_var_table *vartable;
+   GLuint default_precision[TYPE_SPECIFIER_COUNT];
 } slang_output_ctx;
 
 /* _slang_compile() */
@@ -193,9 +215,9 @@ parse_float(slang_parse_ctx * C, float *number)
    parse_identifier_str(C, &fractional);
    parse_identifier_str(C, &exponent);
 
-   whole = (char *) (slang_alloc_malloc((_mesa_strlen(integral) +
-                                         _mesa_strlen(fractional) +
-                                         _mesa_strlen(exponent) + 3) * sizeof(char)));
+   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);
       return 0;
@@ -209,12 +231,13 @@ parse_float(slang_parse_ctx * C, float *number)
 
    *number = (float) (_mesa_strtod(whole, (char **) NULL));
 
-   slang_alloc_free(whole);
+   _slang_free(whole);
+
    return 1;
 }
 
 /* revision number - increment after each change affecting emitted output */
-#define REVISION 3
+#define REVISION 4
 
 static int
 check_revision(slang_parse_ctx * C)
@@ -254,9 +277,33 @@ parse_array_len(slang_parse_ctx * C, slang_output_ctx * O, GLuint * len)
 
    /* evaluate compile-time expression which is array size */
    _slang_simplify(&array_size, &space, C->atoms);
-   result = (array_size.type == SLANG_OPER_LITERAL_INT);
 
-   *len = (GLint) array_size.literal[0];
+   if (array_size.type == SLANG_OPER_LITERAL_INT) {
+      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);
+      if (!var) {
+         slang_info_log_error(C->L, "undefined variable '%s'",
+                              (char *) array_size.a_id);
+         result = GL_FALSE;
+      } else if (var->type.qualifier == SLANG_QUAL_CONST &&
+                 var->type.specifier.type == SLANG_SPEC_INT) {
+         if (var->initializer &&
+             var->initializer->type == SLANG_OPER_LITERAL_INT) {
+            *len = (GLint) var->initializer->literal[0];
+            result = GL_TRUE;
+         } else {
+            slang_info_log_error(C->L, "unable to parse array size declaration");
+            result = GL_FALSE;
+         }
+      } else {
+         slang_info_log_error(C->L, "unable to parse array size declaration");
+         result = GL_FALSE;
+      }
+   } else {
+      result = GL_FALSE;
+   }
 
    slang_operation_destruct(&array_size);
    return result;
@@ -288,7 +335,7 @@ convert_to_array(slang_parse_ctx * C, slang_variable * var,
     * parse the expression */
    var->type.specifier.type = SLANG_SPEC_ARRAY;
    var->type.specifier._array = (slang_type_specifier *)
-      slang_alloc_malloc(sizeof(slang_type_specifier));
+      _slang_alloc(sizeof(slang_type_specifier));
    if (var->type.specifier._array == NULL) {
       slang_info_log_memory(C->L);
       return GL_FALSE;
@@ -304,9 +351,10 @@ 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, const slang_type_specifier * sp)
+                       slang_variable * var, slang_atom a_name,
+                       const slang_type_specifier * sp)
 {
-   var->a_name = parse_identifier(C);
+   var->a_name = a_name;
    if (var->a_name == SLANG_ATOM_NULL)
       return GL_FALSE;
 
@@ -339,12 +387,19 @@ parse_struct_field(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
 
    do {
+      slang_atom a_name;
       slang_variable *var = slang_variable_scope_grow(st->fields);
       if (!var) {
          slang_info_log_memory(C->L);
          return 0;
       }
-      if (!parse_struct_field_var(C, &o, var, sp))
+      a_name = parse_identifier(C);
+      if (_slang_locate_variable(st->fields, a_name, GL_FALSE)) {
+         slang_info_log_error(C->L, "duplicate field '%s'", (char *) a_name);
+         return 0;
+      }
+
+      if (!parse_struct_field_var(C, &o, var, a_name, sp))
          return 0;
    }
    while (*C->I++ != FIELD_NONE);
@@ -371,13 +426,13 @@ parse_struct(slang_parse_ctx * C, slang_output_ctx * O, slang_struct ** st)
    }
 
    /* set-up a new struct */
-   *st = (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+   *st = (slang_struct *) _slang_alloc(sizeof(slang_struct));
    if (*st == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    if (!slang_struct_construct(*st)) {
-      slang_alloc_free(*st);
+      _slang_free(*st);
       *st = NULL;
       slang_info_log_memory(C->L);
       return 0;
@@ -403,11 +458,11 @@ parse_struct(slang_parse_ctx * C, slang_output_ctx * O, slang_struct ** st)
       slang_struct *s;
 
       O->structs->structs =
-         (slang_struct *) slang_alloc_realloc(O->structs->structs,
-                                              O->structs->num_structs *
-                                              sizeof(slang_struct),
-                                              (O->structs->num_structs +
-                                               1) * sizeof(slang_struct));
+         (slang_struct *) _slang_realloc(O->structs->structs,
+                                         O->structs->num_structs
+                                         * sizeof(slang_struct),
+                                         (O->structs->num_structs + 1)
+                                         * sizeof(slang_struct));
       if (O->structs->structs == NULL) {
          slang_info_log_memory(C->L);
          return 0;
@@ -487,8 +542,17 @@ parse_type_qualifier(slang_parse_ctx * C, slang_type_qualifier * qual)
 #define TYPE_SPECIFIER_SAMPLERCUBE 19
 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
-#define TYPE_SPECIFIER_STRUCT 22
-#define TYPE_SPECIFIER_TYPENAME 23
+#define TYPE_SPECIFIER_SAMPLER2DRECT 22
+#define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
+#define TYPE_SPECIFIER_STRUCT 24
+#define TYPE_SPECIFIER_TYPENAME 25
+#define TYPE_SPECIFIER_MAT23 26
+#define TYPE_SPECIFIER_MAT32 27
+#define TYPE_SPECIFIER_MAT24 28
+#define TYPE_SPECIFIER_MAT42 29
+#define TYPE_SPECIFIER_MAT34 30
+#define TYPE_SPECIFIER_MAT43 31
+#define TYPE_SPECIFIER_COUNT 32
 
 static int
 parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
@@ -543,6 +607,24 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    case TYPE_SPECIFIER_MAT4:
       spec->type = SLANG_SPEC_MAT4;
       break;
+   case TYPE_SPECIFIER_MAT23:
+      spec->type = SLANG_SPEC_MAT23;
+      break;
+   case TYPE_SPECIFIER_MAT32:
+      spec->type = SLANG_SPEC_MAT32;
+      break;
+   case TYPE_SPECIFIER_MAT24:
+      spec->type = SLANG_SPEC_MAT24;
+      break;
+   case TYPE_SPECIFIER_MAT42:
+      spec->type = SLANG_SPEC_MAT42;
+      break;
+   case TYPE_SPECIFIER_MAT34:
+      spec->type = SLANG_SPEC_MAT34;
+      break;
+   case TYPE_SPECIFIER_MAT43:
+      spec->type = SLANG_SPEC_MAT43;
+      break;
    case TYPE_SPECIFIER_SAMPLER1D:
       spec->type = SLANG_SPEC_SAMPLER1D;
       break;
@@ -555,12 +637,18 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    case TYPE_SPECIFIER_SAMPLERCUBE:
       spec->type = SLANG_SPEC_SAMPLERCUBE;
       break;
+   case TYPE_SPECIFIER_SAMPLER2DRECT:
+      spec->type = SLANG_SPEC_SAMPLER2DRECT;
+      break;
    case TYPE_SPECIFIER_SAMPLER1DSHADOW:
       spec->type = SLANG_SPEC_SAMPLER1DSHADOW;
       break;
    case TYPE_SPECIFIER_SAMPLER2DSHADOW:
       spec->type = SLANG_SPEC_SAMPLER2DSHADOW;
       break;
+   case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
+      spec->type = SLANG_SPEC_SAMPLER2DRECTSHADOW;
+      break;
    case TYPE_SPECIFIER_STRUCT:
       spec->type = SLANG_SPEC_STRUCT;
       if (!parse_struct(C, O, &spec->_struct))
@@ -583,14 +671,13 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
             return 0;
          }
 
-         spec->_struct =
-            (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+         spec->_struct = (slang_struct *) _slang_alloc(sizeof(slang_struct));
          if (spec->_struct == NULL) {
             slang_info_log_memory(C->L);
             return 0;
          }
          if (!slang_struct_construct(spec->_struct)) {
-            slang_alloc_free(spec->_struct);
+            _slang_free(spec->_struct);
             spec->_struct = NULL;
             return 0;
          }
@@ -604,14 +691,49 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    return 1;
 }
 
+#define PRECISION_DEFAULT 0
+#define PRECISION_LOW     1
+#define PRECISION_MEDIUM  2
+#define PRECISION_HIGH    3
+
 static int
 parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
                            slang_fully_specified_type * type)
 {
+   GLuint precision;
+
    if (!parse_type_qualifier(C, &type->qualifier))
       return 0;
+   precision = *C->I++;
    if (!parse_type_specifier(C, O, &type->specifier))
       return 0;
+
+   switch (precision) {
+   case PRECISION_DEFAULT:
+      assert(type->specifier.type < TYPE_SPECIFIER_COUNT);
+      if (type->specifier.type < TYPE_SPECIFIER_COUNT)
+         type->precision = O->default_precision[type->specifier.type];
+      break;
+   case PRECISION_LOW:
+      type->precision = SLANG_PREC_LOW;
+      break;
+   case PRECISION_MEDIUM:
+      type->precision = SLANG_PREC_MEDIUM;
+      break;
+   case PRECISION_HIGH:
+      type->precision = SLANG_PREC_HIGH;
+      break;
+   default:
+      return 0;
+   }
+
+#if !FEATURE_es2_glsl
+   if (precision != PRECISION_DEFAULT) {
+      slang_info_log_error(C->L, "precision qualifiers not allowed");
+      return 0;
+   }
+#endif
+
    return 1;
 }
 
@@ -678,12 +800,13 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
 #define OP_FIELD 59
 #define OP_POSTINCREMENT 60
 #define OP_POSTDECREMENT 61
+#define OP_PRECISION 62
 
 
 /**
  * When parsing a compound production, this function is used to parse the
  * children.
- * For example, a while-loop compound will have two children, the
+ * For example, a while-loop compound will have two children, the
  * while condition expression and the loop body.  So, this function will
  * be called twice to parse those two sub-expressions.
  * \param C  the parsing context
@@ -750,7 +873,7 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
          if (first_var < O->vars->num_variables) {
             const unsigned int num_vars = O->vars->num_variables - first_var;
             unsigned int i;
-
+            assert(oper->num_children == 0);
             oper->num_children = num_vars;
             oper->children = slang_operation_new(num_vars);
             if (oper->children == NULL) {
@@ -762,6 +885,12 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
                o->type = SLANG_OPER_VARIABLE_DECL;
                o->locals->outer_scope = O->vars;
                o->a_id = O->vars->variables[i]->a_name;
+
+               if (!legal_identifier(o->a_id)) {
+                  slang_info_log_error(C->L, "illegal variable name '%s'",
+                                       (char *) o->a_id);
+                  return 0;
+               }
             }
          }
       }
@@ -843,6 +972,16 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
             return 0;
       }
       break;
+   case OP_PRECISION:
+      {
+         /* set default precision for a type in this scope */
+         /* ignored at this time */
+         int prec_qual = *C->I++;
+         int datatype = *C->I++;
+         (void) prec_qual;
+         (void) datatype;
+      }
+      break;
    default:
       return 0;
    }
@@ -856,23 +995,25 @@ handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
 {
    unsigned int i;
 
-   op->children =
-      (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
+   op->children = slang_operation_new(n);
    if (op->children == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    op->num_children = n;
 
-   for (i = 0; i < n; i++)
+   for (i = 0; i < n; i++) {
+      slang_operation_destruct(&op->children[i]);
       op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
+   }
+
    (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
    *total_ops -= n;
 
    *ops = (slang_operation *)
-      slang_alloc_realloc(*ops,
-                          (*total_ops + n) * sizeof(slang_operation),
-                          *total_ops * sizeof(slang_operation));
+      _slang_realloc(*ops,
+                     (*total_ops + n) * sizeof(slang_operation),
+                     *total_ops * sizeof(slang_operation));
    if (*ops == NULL) {
       slang_info_log_memory(C->L);
       return 0;
@@ -903,9 +1044,9 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
 
       /* allocate default operation, becomes a no-op if not used  */
       ops = (slang_operation *)
-         slang_alloc_realloc(ops,
-                             num_ops * sizeof(slang_operation),
-                             (num_ops + 1) * sizeof(slang_operation));
+         _slang_realloc(ops,
+                        num_ops * sizeof(slang_operation),
+                        (num_ops + 1) * sizeof(slang_operation));
       if (ops == NULL) {
          slang_info_log_memory(C->L);
          return 0;
@@ -1037,12 +1178,12 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
             return 0;
          break;
       case OP_LESSEQUAL:
-         op->type = SLANG_OPER_LESSequal;
+         op->type = SLANG_OPER_LESSEQUAL;
          if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
             return 0;
          break;
       case OP_GREATEREQUAL:
-         op->type = SLANG_OPER_GREATERequal;
+         op->type = SLANG_OPER_GREATEREQUAL;
          if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
             return 0;
          break;
@@ -1145,8 +1286,9 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
    }
    C->I++;
 
-   *oper = *ops;
-   slang_alloc_free(ops);
+   slang_operation_destruct(oper);
+   *oper = *ops; /* struct copy */
+   _slang_free(ops);
 
    return 1;
 }
@@ -1164,12 +1306,16 @@ static int
 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
                             slang_variable * param)
 {
+   int param_qual, precision_qual;
+
    /* parse and validate the parameter's type qualifiers (there can be
     * two at most) because not all combinations are valid
     */
    if (!parse_type_qualifier(C, &param->type.qualifier))
       return 0;
-   switch (*C->I++) {
+
+   param_qual = *C->I++;
+   switch (param_qual) {
    case PARAM_QUALIFIER_IN:
       if (param->type.qualifier != SLANG_QUAL_CONST
           && param->type.qualifier != SLANG_QUAL_NONE) {
@@ -1197,6 +1343,11 @@ parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
    }
 
+   /* parse precision qualifier (lowp, mediump, highp */
+   precision_qual = *C->I++;
+   /* ignored at this time */
+   (void) precision_qual;
+
    /* parse parameter's type specifier and name */
    if (!parse_type_specifier(C, O, &param->type.specifier))
       return 0;
@@ -1328,6 +1479,7 @@ parse_operator_name(slang_parse_ctx * C)
    return 0;
 }
 
+
 static int
 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
                          slang_function * func)
@@ -1365,6 +1517,12 @@ parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
    }
 
+   if (!legal_identifier(func->header.a_name)) {
+      slang_info_log_error(C->L, "illegal function name '%s'",
+                           (char *) func->header.a_name);
+      return 0;
+   }
+
    /* parse function parameters */
    while (*C->I++ == PARAMETER_NEXT) {
       slang_variable *p = slang_variable_scope_grow(func->parameters);
@@ -1410,14 +1568,13 @@ parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
 
    /* create function's body operation */
-   func->body =
-      (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+   func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
    if (func->body == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    if (!slang_operation_construct(func->body)) {
-      slang_alloc_free(func->body);
+      _slang_free(func->body);
       func->body = NULL;
       slang_info_log_memory(C->L);
       return 0;
@@ -1449,7 +1606,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
 
    /* put the variable into operation's scope */
    op_id.locals->variables =
-      (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
+      (slang_variable **) _slang_alloc(sizeof(slang_variable *));
    if (op_id.locals->variables == NULL) {
       slang_operation_destruct(&op_id);
       return GL_FALSE;
@@ -1465,7 +1622,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
    }
    op_assign.type = SLANG_OPER_ASSIGN;
    op_assign.children =
-      (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
+      (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
    if (op_assign.children == NULL) {
       slang_operation_destruct(&op_assign);
       op_id.locals->num_variables = 0;
@@ -1480,7 +1637,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
 
    /* carefully destroy the operations */
    op_assign.num_children = 0;
-   slang_alloc_free(op_assign.children);
+   _slang_free(op_assign.children);
    op_assign.children = NULL;
    slang_operation_destruct(&op_assign);
    op_id.locals->num_variables = 0;
@@ -1512,11 +1669,22 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
                       const slang_fully_specified_type * type)
 {
    slang_variable *var;
+   slang_atom a_name;
 
    /* empty init declatator (without name, e.g. "float ;") */
    if (*C->I++ == VARIABLE_NONE)
       return 1;
 
+   a_name = parse_identifier(C);
+
+   /* check if name is already in this scope */
+   if (_slang_locate_variable(O->vars, a_name, GL_FALSE)) {
+      slang_info_log_error(C->L,
+                   "declaration of '%s' conflicts with previous declaration",
+                   (char *) a_name);
+      return 0;
+   }
+
    /* make room for the new variable and initialize it */
    var = slang_variable_scope_grow(O->vars);
    if (!var) {
@@ -1526,7 +1694,7 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
 
    /* copy the declarator qualifier type, parse the identifier */
    var->type.qualifier = type->qualifier;
-   var->a_name = parse_identifier(C);
+   var->a_name = a_name;
    if (var->a_name == SLANG_ATOM_NULL)
       return 0;
 
@@ -1541,13 +1709,13 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
          return 0;
       var->initializer =
-         (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+         (slang_operation *) _slang_alloc(sizeof(slang_operation));
       if (var->initializer == NULL) {
          slang_info_log_memory(C->L);
          return 0;
       }
       if (!slang_operation_construct(var->initializer)) {
-         slang_alloc_free(var->initializer);
+         _slang_free(var->initializer);
          var->initializer = NULL;
          slang_info_log_memory(C->L);
          return 0;
@@ -1572,6 +1740,15 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
    }
 
+   /* allocate global address space for a variable with a known size */
+   if (C->global_scope
+       && !(var->type.specifier.type == SLANG_SPEC_ARRAY
+            && 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;
@@ -1581,16 +1758,10 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       A.space.vars = O->vars;
       A.program = O->program;
       A.vartable = O->vartable;
-      _slang_codegen_global_variable(&A, var, C->type);
-   }
-
-   /* allocate global address space for a variable with a known size */
-   if (C->global_scope
-       && !(var->type.specifier.type == SLANG_SPEC_ARRAY
-            && 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);
+      A.log = C->L;
+      A.curFuncEndLabel = NULL;
+      if (!_slang_codegen_global_variable(&A, var, C->type))
+         return 0;
    }
 
    /* initialize global variable */
@@ -1678,11 +1849,11 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
    if (found_func == NULL) {
       /* New function, add it to the function list */
       O->funs->functions =
-         (slang_function *) slang_alloc_realloc(O->funs->functions,
-                                                O->funs->num_functions *
-                                                sizeof(slang_function),
-                                                (O->funs->num_functions +
-                                                 1) * sizeof(slang_function));
+         (slang_function *) _slang_realloc(O->funs->functions,
+                                           O->funs->num_functions
+                                           * sizeof(slang_function),
+                                           (O->funs->num_functions + 1)
+                                           * sizeof(slang_function));
       if (O->funs->functions == NULL) {
          slang_info_log_memory(C->L);
          slang_function_destruct(&parsed_func);
@@ -1724,20 +1895,6 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
       *parsed_func_ret = found_func;
    }
 
-   /* assemble the parsed function */
-   {
-      slang_assemble_ctx A;
-
-      A.atoms = C->atoms;
-      A.space.funcs = O->funs;
-      A.space.structs = O->structs;
-      A.space.vars = O->vars;
-      A.program = O->program;
-      A.vartable = O->vartable;
-      A.log = C->L;
-
-      _slang_codegen_function(&A, *parsed_func_ret);
-   }
    return GL_TRUE;
 }
 
@@ -1767,19 +1924,116 @@ parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
    return 1;
 }
 
-/* external declaration */
+static int
+parse_default_precision(slang_parse_ctx * C, slang_output_ctx * O)
+{
+#if FEATURE_es2_glsl
+   int precision, type;
+
+   precision = *C->I++;
+   switch (precision) {
+   case PRECISION_LOW:
+   case PRECISION_MEDIUM:
+   case PRECISION_HIGH:
+      /* OK */
+      break;
+   default:
+      _mesa_problem(NULL, "unexpected precision %d at %s:%d\n",
+                    precision, __FILE__, __LINE__);
+      return 0;
+   }
+
+   type = *C->I++;
+   switch (type) {
+   case TYPE_SPECIFIER_FLOAT:
+   case TYPE_SPECIFIER_INT:
+   case TYPE_SPECIFIER_SAMPLER1D:
+   case TYPE_SPECIFIER_SAMPLER2D:
+   case TYPE_SPECIFIER_SAMPLER3D:
+   case TYPE_SPECIFIER_SAMPLERCUBE:
+   case TYPE_SPECIFIER_SAMPLER1DSHADOW:
+   case TYPE_SPECIFIER_SAMPLER2DSHADOW:
+   case TYPE_SPECIFIER_SAMPLER2DRECT:
+   case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
+      /* OK */
+      break;
+   default:
+      _mesa_problem(NULL, "unexpected type %d at %s:%d\n",
+                    type, __FILE__, __LINE__);
+      return 0;
+   }
+
+   assert(type < TYPE_SPECIFIER_COUNT);
+   O->default_precision[type] = precision;
+
+   return 1;
+#else
+   slang_info_log_error(C->L, "syntax error at \"precision\"");
+   return 0;
+#endif
+}
+
+
+/**
+ * Initialize the default precision for all types.
+ * XXX this info isn't used yet.
+ */
+static void
+init_default_precision(slang_output_ctx *O, slang_unit_type type)
+{
+   GLuint i;
+   for (i = 0; i < TYPE_SPECIFIER_COUNT; i++) {
+#if FEATURE_es2_glsl
+      O->default_precision[i] = PRECISION_LOW;
+#else
+      O->default_precision[i] = PRECISION_HIGH;
+#endif
+   }
+#if FEATURE_es2_glsl
+   if (type == SLANG_UNIT_VERTEX_SHADER) {
+      O->default_precision[TYPE_SPECIFIER_FLOAT] = PRECISION_HIGH;
+      O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_HIGH;
+   }
+   else {
+      O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_MEDIUM;
+   }
+#endif
+}
+
+
+static int
+parse_invariant(slang_parse_ctx * C, slang_output_ctx * O)
+{
+   if (C->version >= 120 || FEATURE_es2_glsl) {
+      slang_atom *a = parse_identifier(C);
+      /* XXX not doing anything with this var yet */
+      /*printf("ID: %s\n", (char*) a);*/
+      return a ? 1 : 0;
+   }
+   else {
+      slang_info_log_error(C->L, "syntax error at \"invariant\"");
+      return 0;
+   }
+}
+      
+
+/* external declaration or default precision specifier */
 #define EXTERNAL_NULL 0
 #define EXTERNAL_FUNCTION_DEFINITION 1
 #define EXTERNAL_DECLARATION 2
+#define DEFAULT_PRECISION 3
+#define INVARIANT_STMT 4
+
 
 static GLboolean
 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
-                struct gl_program *program)
+                struct gl_shader *shader)
 {
    GET_CURRENT_CONTEXT(ctx);
    slang_output_ctx o;
    GLboolean success;
    GLuint maxRegs;
+   slang_function *mainFunc = NULL;
 
    if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
        unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
@@ -1792,11 +2046,12 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    }
 
    /* setup output context */
+   init_default_precision(&o, unit->type);
    o.funs = &unit->funs;
    o.structs = &unit->structs;
    o.vars = &unit->vars;
    o.global_pool = &unit->object->varpool;
-   o.program = program;
+   o.program = shader ? shader->Program : NULL;
    o.vartable = _slang_new_var_table(maxRegs);
    _slang_push_var_table(o.vartable);
 
@@ -1807,11 +2062,22 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
          {
             slang_function *func;
             success = parse_function(C, &o, 1, &func);
+            if (success &&
+                _mesa_strcmp((char *) func->header.a_name, "main") == 0) {
+               /* found main() */
+               mainFunc = func;
+            }
          }
          break;
       case EXTERNAL_DECLARATION:
          success = parse_declaration(C, &o);
          break;
+      case DEFAULT_PRECISION:
+         success = parse_default_precision(C, &o);
+         break;
+      case INVARIANT_STMT:
+         success = parse_invariant(C, &o);
+         break;
       default:
          success = GL_FALSE;
       }
@@ -1824,15 +2090,41 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    }
    C->I++;
 
+   if (mainFunc) {
+      /* assemble (generate code) for main() */
+      slang_assemble_ctx A;
+
+      A.atoms = C->atoms;
+      A.space.funcs = o.funs;
+      A.space.structs = o.structs;
+      A.space.vars = o.vars;
+      A.program = o.program;
+      A.vartable = o.vartable;
+      A.log = C->L;
+
+      /* main() takes no parameters */
+      if (mainFunc->param_count > 0) {
+         slang_info_log_error(A.log, "main() takes no arguments");
+         return GL_FALSE;
+      }
+
+      _slang_codegen_function(&A, mainFunc);
+
+      shader->Main = GL_TRUE; /* this shader defines main() */
+   }
+
    _slang_pop_var_table(o.vartable);
+   _slang_delete_var_table(o.vartable);
+
    return GL_TRUE;
 }
 
 static GLboolean
 compile_binary(const byte * prod, slang_code_unit * unit,
+               GLuint version,
                slang_unit_type type, slang_info_log * infolog,
                slang_code_unit * builtin, slang_code_unit * downlink,
-               struct gl_program *program)
+               struct gl_shader *shader)
 {
    slang_parse_ctx C;
 
@@ -1845,6 +2137,7 @@ compile_binary(const byte * prod, slang_code_unit * unit,
    C.global_scope = GL_TRUE;
    C.atoms = &unit->object->atompool;
    C.type = type;
+   C.version = version;
 
    if (!check_revision(&C))
       return GL_FALSE;
@@ -1856,26 +2149,36 @@ compile_binary(const byte * prod, slang_code_unit * unit,
    }
 
    /* parse translation unit */
-   return parse_code_unit(&C, unit, program);
+   return parse_code_unit(&C, unit, shader);
 }
 
 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_program *program)
+                     struct gl_shader *shader)
 {
    byte *prod;
    GLuint size, start, version;
    slang_string preprocessed;
+   GLuint maxVersion;
+
+#if FEATURE_ARB_shading_language_120
+   maxVersion = 120;
+#elif FEATURE_es2_glsl
+   maxVersion = 100;
+#else
+   maxVersion = 110;
+#endif
 
    /* First retrieve the version number. */
    if (!_slang_preprocess_version(source, &version, &start, infolog))
       return GL_FALSE;
 
-   if (version > 110) {
+   if (version > maxVersion) {
       slang_info_log_error(infolog,
-                           "language version specified is not supported.");
+                           "language version %.2f is not supported.",
+                           version * 0.01);
       return GL_FALSE;
    }
 
@@ -1898,13 +2201,24 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
       grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
       slang_info_log_error(infolog, buf);
       /* syntax error (possibly in library code) */
+#if 0
+      {
+         int line, col;
+         char *s;
+         s = (char *) _mesa_find_line_column((const GLubyte *) source,
+                                             (const GLubyte *) source + pos,
+                                             &line, &col);
+         printf("Error on line %d, col %d: %s\n", line, col, s);
+      }
+#endif
+      return GL_FALSE;
    }
    slang_string_free(&preprocessed);
 
    /* Syntax is okay - translate it to internal representation. */
-   if (!compile_binary(prod, unit, type, infolog, builtin,
+   if (!compile_binary(prod, unit, version, type, infolog, builtin,
                        &builtin[SLANG_BUILTIN_TOTAL - 1],
-                       program)) {
+                       shader)) {
       grammar_alloc_free(prod);
       return GL_FALSE;
    }
@@ -1920,6 +2234,14 @@ static const byte slang_core_gc[] = {
 #include "library/slang_core_gc.h"
 };
 
+static const byte slang_120_core_gc[] = {
+#include "library/slang_120_core_gc.h"
+};
+
+static const byte slang_120_fragment_gc[] = {
+#include "library/slang_builtin_120_fragment_gc.h"
+};
+
 static const byte slang_common_builtin_gc[] = {
 #include "library/slang_common_builtin_gc.h"
 };
@@ -1935,9 +2257,10 @@ static const byte slang_vertex_builtin_gc[] = {
 static GLboolean
 compile_object(grammar * id, const char *source, slang_code_object * object,
                slang_unit_type type, slang_info_log * infolog,
-               struct gl_program *program)
+               struct gl_shader *shader)
 {
    slang_code_unit *builtins = NULL;
+   GLuint base_version = 110;
 
    /* load GLSL grammar */
    *id = grammar_load_from_text((const byte *) (slang_shader_syn));
@@ -1965,28 +2288,58 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
       /* compile core functionality first */
       if (!compile_binary(slang_core_gc,
                           &object->builtin[SLANG_BUILTIN_CORE],
+                          base_version,
                           SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
                           NULL, NULL, NULL))
          return GL_FALSE;
 
+#if FEATURE_ARB_shading_language_120
+      if (!compile_binary(slang_120_core_gc,
+                          &object->builtin[SLANG_BUILTIN_120_CORE],
+                          120,
+                          SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
+                          NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
+         return GL_FALSE;
+#endif
+
       /* compile common functions and variables, link to core */
       if (!compile_binary(slang_common_builtin_gc,
                           &object->builtin[SLANG_BUILTIN_COMMON],
+#if FEATURE_ARB_shading_language_120
+                          120,
+#else
+                          base_version,
+#endif
                           SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
-                          &object->builtin[SLANG_BUILTIN_CORE], NULL))
+#if FEATURE_ARB_shading_language_120
+                          &object->builtin[SLANG_BUILTIN_120_CORE],
+#else
+                          &object->builtin[SLANG_BUILTIN_CORE],
+#endif
+                          NULL))
          return GL_FALSE;
 
       /* compile target-specific functions and variables, link to common */
       if (type == SLANG_UNIT_FRAGMENT_SHADER) {
          if (!compile_binary(slang_fragment_builtin_gc,
                              &object->builtin[SLANG_BUILTIN_TARGET],
+                             base_version,
                              SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
                              &object->builtin[SLANG_BUILTIN_COMMON], NULL))
             return GL_FALSE;
+#if FEATURE_ARB_shading_language_120
+         if (!compile_binary(slang_120_fragment_gc,
+                             &object->builtin[SLANG_BUILTIN_TARGET],
+                             120,
+                             SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
+                             &object->builtin[SLANG_BUILTIN_COMMON], NULL))
+            return GL_FALSE;
+#endif
       }
       else if (type == SLANG_UNIT_VERTEX_SHADER) {
          if (!compile_binary(slang_vertex_builtin_gc,
                              &object->builtin[SLANG_BUILTIN_TARGET],
+                             base_version,
                              SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
                              &object->builtin[SLANG_BUILTIN_COMMON], NULL))
             return GL_FALSE;
@@ -2003,7 +2356,7 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
 
    /* compile the actual shader - pass-in built-in library for external shader */
    return compile_with_grammar(*id, source, &object->unit, type, infolog,
-                               builtins, program);
+                               builtins, shader);
 }
 
 
@@ -2012,16 +2365,21 @@ compile_shader(GLcontext *ctx, slang_code_object * object,
                slang_unit_type type, slang_info_log * infolog,
                struct gl_shader *shader)
 {
-   struct gl_program *program = shader->Programs[0];
    GLboolean success;
    grammar id = 0;
 
-   assert(program);
+#if 0 /* for debug */
+   _mesa_printf("********* COMPILE SHADER ***********\n");
+   _mesa_printf("%s\n", shader->Source);
+   _mesa_printf("************************************\n");
+#endif
+
+   assert(shader->Program);
 
    _slang_code_object_dtr(object);
    _slang_code_object_ctr(object);
 
-   success = compile_object(&id, shader->Source, object, type, infolog, program);
+   success = compile_object(&id, shader->Source, object, type, infolog, shader);
    if (id != 0)
       grammar_destroy(id);
    if (!success)
@@ -2048,21 +2406,23 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
       type = SLANG_UNIT_FRAGMENT_SHADER;
    }
 
-   /* XXX temporary hack */
-   if (!shader->Programs) {
+   if (!shader->Source)
+      return GL_FALSE;
+
+   ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
+
+   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->Programs
-         = (struct gl_program **) malloc(sizeof(struct gl_program*));
-      shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
-      shader->NumPrograms = 1;
-
-      shader->Programs[0]->Parameters = _mesa_new_parameter_list();
-      shader->Programs[0]->Varying = _mesa_new_parameter_list();
-      shader->Programs[0]->Attributes = _mesa_new_parameter_list();
+      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);
@@ -2070,21 +2430,42 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
 
    success = compile_shader(ctx, &obj, type, &info_log, shader);
 
-   if (!success || info_log.error_flag) {
-      success = GL_FALSE;
+   /* free shader's prev info log */
+   if (shader->InfoLog) {
+      _mesa_free(shader->InfoLog);
+      shader->InfoLog = NULL;
+   }
+
+   if (info_log.text) {
       /* copy info-log string to shader object */
-      if (info_log.text) {
-         if (shader->InfoLog) {
-            free(shader->InfoLog);
-            shader->InfoLog = NULL;
-         }
-         shader->InfoLog = strdup(info_log.text);
-      }
+      shader->InfoLog = _mesa_strdup(info_log.text);
+   }
+
+   if (info_log.error_flag) {
+      success = GL_FALSE;
    }
 
    slang_info_log_destruct(&info_log);
    _slang_code_object_dtr(&obj);
 
+   _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
+   ctx->Shader.MemPool = NULL;
+
+   /* remove any reads of output registers */
+#if 0
+   printf("Pre-remove output reads:\n");
+   _mesa_print_program(shader->Program);
+#endif
+   _mesa_remove_output_reads(shader->Program, PROGRAM_OUTPUT);
+   if (shader->Type == GL_VERTEX_SHADER) {
+      /* and remove writes to varying vars in vertex programs */
+      _mesa_remove_output_reads(shader->Program, PROGRAM_VARYING);
+   }
+#if 0
+   printf("Post-remove output reads:\n");
+   _mesa_print_program(shader->Program);
+#endif
+
    return success;
 }