mesa: more glsl function renaming
[mesa.git] / src / mesa / shader / slang / slang_codegen.c
index 02260d3422e516713d449f22a1f27dc6368401bd..3f31f956fa550b6a3d004513795ff3d6e3e5b19a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5.3
+ * Version:  7.1
  *
  * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
  *
 
 
 
-#include "imports.h"
-#include "macros.h"
-#include "mtypes.h"
-#include "program.h"
-#include "prog_instruction.h"
-#include "prog_parameter.h"
-#include "prog_statevars.h"
+#include "main/imports.h"
+#include "main/macros.h"
+#include "main/mtypes.h"
+#include "shader/program.h"
+#include "shader/prog_instruction.h"
+#include "shader/prog_parameter.h"
+#include "shader/prog_print.h"
+#include "shader/prog_statevars.h"
 #include "slang_typeinfo.h"
 #include "slang_codegen.h"
 #include "slang_compile.h"
@@ -60,6 +61,20 @@ static slang_ir_node *
 _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper);
 
 
+/**
+ * Retrieves type information about an operation.
+ * Returns GL_TRUE on success.
+ * Returns GL_FALSE otherwise.
+ */
+static GLboolean
+typeof_operation(const struct slang_assemble_ctx_ * A,
+                        slang_operation * op,
+                        slang_typeinfo * ti)
+{
+   return _slang_typeof_operation(op, &A->space, ti, A->atoms, A->log);
+}
+
+
 static GLboolean
 is_sampler_type(const slang_fully_specified_type *t)
 {
@@ -237,16 +252,21 @@ _slang_attach_storage(slang_ir_node *n, slang_variable *var)
 
    if (!n->Store) {
       /* need to setup storage */
-      if (n->Var && n->Var->aux) {
+      if (n->Var && n->Var->store) {
          /* node storage info = var storage info */
-         n->Store = (slang_ir_storage *) n->Var->aux;
+         n->Store = n->Var->store;
       }
       else {
          /* alloc new storage info */
-         n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -1, -5);
+         n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -7, -5);
+#if 0
+         printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__,
+                (char*) var->a_name,
+                (void*) n->Store, n->Store->Size);
+#endif
          if (n->Var)
-            n->Var->aux = n->Store;
-         assert(n->Var->aux);
+            n->Var->store = n->Store;
+         assert(n->Var->store);
       }
    }
 }
@@ -282,6 +302,8 @@ sampler_to_texture_index(const slang_type_specifier_type type)
 }
 
 
+#define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
+
 /**
  * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
  * a vertex or fragment program input variable.  Return -1 if the input
@@ -316,9 +338,11 @@ _slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
       { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
       { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
       { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
-      { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
       { "gl_TexCoord", FRAG_ATTRIB_TEX0, SWIZZLE_NOOP },
+      /* note: we're packing several quantities into the fogcoord vector */
+      { "gl_FogFragCoord", FRAG_ATTRIB_FOGC, SWIZZLE_XXXX },
       { "gl_FrontFacing", FRAG_ATTRIB_FOGC, SWIZZLE_YYYY }, /*XXX*/
+      { "gl_PointCoord", FRAG_ATTRIB_FOGC, SWIZZLE_ZWWW },
       { NULL, 0, SWIZZLE_NOOP }
    };
    GLuint i;
@@ -403,6 +427,9 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_multiply", IR_MUL, 1, 2 },
    { "vec4_dot", IR_DOT4, 1, 2 },
    { "vec3_dot", IR_DOT3, 1, 2 },
+   { "vec2_dot", IR_DOT2, 1, 2 },
+   { "vec3_nrm", IR_NRM3, 1, 1 },
+   { "vec4_nrm", IR_NRM4, 1, 1 },
    { "vec3_cross", IR_CROSS, 1, 2 },
    { "vec4_lrp", IR_LRP, 1, 3 },
    { "vec4_min", IR_MIN, 1, 2 },
@@ -415,6 +442,7 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_sle", IR_SLE, 1, 2 },
    { "vec4_slt", IR_SLT, 1, 2 },
    /* vec4 unary */
+   { "vec4_move", IR_MOVE, 1, 1 },
    { "vec4_floor", IR_FLOOR, 1, 1 },
    { "vec4_frac", IR_FRAC, 1, 1 },
    { "vec4_abs", IR_ABS, 1, 1 },
@@ -438,8 +466,8 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_texp_rect", IR_TEX, 1, 2 },/* rectangle w/ projection */
 
    /* unary op */
-   { "int_to_float", IR_I_TO_F, 1, 1 },
-   { "float_to_int", IR_F_TO_I, 1, 1 },
+   { "ivec4_to_vec4", IR_I_TO_F, 1, 1 }, /* int[4] to float[4] */
+   { "vec4_to_ivec4", IR_F_TO_I, 1, 1 },  /* float[4] to int[4] */
    { "float_exp", IR_EXP, 1, 1 },
    { "float_exp2", IR_EXP2, 1, 1 },
    { "float_log2", IR_LOG2, 1, 1 },
@@ -466,7 +494,6 @@ new_node3(slang_ir_opcode op,
       n->Children[0] = c0;
       n->Children[1] = c1;
       n->Children[2] = c2;
-      n->Writemask = WRITEMASK_XYZW;
       n->InstLocation = -1;
    }
    return n;
@@ -491,6 +518,9 @@ new_node0(slang_ir_opcode op)
 }
 
 
+/**
+ * Create sequence of two nodes.
+ */
 static slang_ir_node *
 new_seq(slang_ir_node *left, slang_ir_node *right)
 {
@@ -531,12 +561,12 @@ new_not(slang_ir_node *n)
 
 
 /**
- * Inlined subroutine.
+ * Non-inlined function call.
  */
 static slang_ir_node *
-new_inlined_function_call(slang_ir_node *code, slang_label *name)
+new_function_call(slang_ir_node *code, slang_label *name)
 {
-   slang_ir_node *n = new_node1(IR_FUNC, code);
+   slang_ir_node *n = new_node1(IR_CALL, code);
    assert(name);
    if (n)
       n->Label = name;
@@ -637,18 +667,20 @@ new_if(slang_ir_node *cond, slang_ir_node *ifPart, slang_ir_node *elsePart)
  * New IR_VAR node - a reference to a previously declared variable.
  */
 static slang_ir_node *
-new_var(slang_assemble_ctx *A, slang_operation *oper, slang_atom name)
+new_var(slang_assemble_ctx *A, slang_variable *var)
 {
    slang_ir_node *n;
-   slang_variable *var = _slang_locate_variable(oper->locals, name, GL_TRUE);
    if (!var)
       return NULL;
 
-   assert(!oper->var || oper->var == var);
+   assert(var->declared);
 
    n = new_node0(IR_VAR);
    if (n) {
       _slang_attach_storage(n, var);
+      /*
+      printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
+      */
    }
    return n;
 }
@@ -700,11 +732,69 @@ _slang_find_node_type(slang_operation *oper, slang_operation_type type)
 }
 
 
+/**
+ * Count the number of operations of the given time rooted at 'oper'.
+ */
+static GLuint
+_slang_count_node_type(slang_operation *oper, slang_operation_type type)
+{
+   GLuint i, count = 0;
+   if (oper->type == type) {
+      return 1;
+   }
+   for (i = 0; i < oper->num_children; i++) {
+      count += _slang_count_node_type(&oper->children[i], type);
+   }
+   return count;
+}
+
+
+/**
+ * Check if the 'return' statement found under 'oper' is a "tail return"
+ * that can be no-op'd.  For example:
+ *
+ * void func(void)
+ * {
+ *    .. do something ..
+ *    return;   // this is a no-op
+ * }
+ *
+ * This is used when determining if a function can be inlined.  If the
+ * 'return' is not the last statement, we can't inline the function since
+ * we still need the semantic behaviour of the 'return' but we don't want
+ * to accidentally return from the _calling_ function.  We'd need to use an
+ * unconditional branch, but we don't have such a GPU instruction (not
+ * always, at least).
+ */
+static GLboolean
+_slang_is_tail_return(const slang_operation *oper)
+{
+   GLuint k = oper->num_children;
+
+   while (k > 0) {
+      const slang_operation *last = &oper->children[k - 1];
+      if (last->type == SLANG_OPER_RETURN)
+         return GL_TRUE;
+      else if (last->type == SLANG_OPER_IDENTIFIER ||
+               last->type == SLANG_OPER_LABEL)
+         k--; /* try prev child */
+      else if (last->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
+               last->type == SLANG_OPER_BLOCK_NEW_SCOPE)
+         /* try sub-children */
+         return _slang_is_tail_return(last);
+      else
+         break;
+   }
+
+   return GL_FALSE;
+}
+
+
 static void
 slang_resolve_variable(slang_operation *oper)
 {
    if (oper->type == SLANG_OPER_IDENTIFIER && !oper->var) {
-      oper->var = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
+      oper->var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
    }
 }
 
@@ -720,7 +810,7 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
    switch (oper->type) {
    case SLANG_OPER_VARIABLE_DECL:
       {
-         slang_variable *v = _slang_locate_variable(oper->locals,
+         slang_variable *v = _slang_variable_locate(oper->locals,
                                                     oper->a_id, GL_TRUE);
          assert(v);
          if (v->initializer && oper->num_children == 0) {
@@ -742,7 +832,7 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
          slang_atom id = oper->a_id;
          slang_variable *v;
         GLuint i;
-         v = _slang_locate_variable(oper->locals, id, GL_TRUE);
+         v = _slang_variable_locate(oper->locals, id, GL_TRUE);
         if (!v) {
             _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
             return;
@@ -859,7 +949,6 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
 }
 
 
-
 /**
  * Produce inline code for a call to an assembly instruction.
  * This is typically used to compile a call to a built-in function like this:
@@ -869,6 +958,18 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
  *    __asm vec4_lrp __retVal, a, y, x;
  * }
  *
+ *
+ * A call to
+ *     r = mix(p1, p2, p3);
+ *
+ * Becomes:
+ *
+ *              mov
+ *             /   \
+ *            r   vec4_lrp
+ *                 /  |  \
+ *                p3  p2  p1
+ *
  * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
  */
 static slang_operation *
@@ -908,10 +1009,10 @@ slang_inline_asm_function(slang_assemble_ctx *A,
    slang_operation_copy(inlined, &fun->body->children[0]);
    if (haveRetValue) {
       /* get rid of the __retVal child */
-      for (i = 0; i < numArgs; i++) {
+      inlined->num_children--;
+      for (i = 0; i < inlined->num_children; i++) {
          inlined->children[i] = inlined->children[i + 1];
       }
-      inlined->num_children--;
    }
 
    /* now do formal->actual substitutions */
@@ -920,6 +1021,13 @@ slang_inline_asm_function(slang_assemble_ctx *A,
    _slang_free(substOld);
    _slang_free(substNew);
 
+#if 0
+   printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
+          (char *) fun->header.a_name);
+   slang_print_tree(inlined, 3);
+   printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+#endif
+
    return inlined;
 }
 
@@ -947,6 +1055,7 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
    slang_operation **substNew;
    GLuint substCount, numCopyIn, i;
    slang_function *prevFunction;
+   slang_variable_scope *newScope = NULL;
 
    /* save / push */
    prevFunction = A->CurFunction;
@@ -964,7 +1073,7 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
       _slang_alloc(totalArgs * sizeof(slang_operation *));
 
 #if 0
-   printf("Inline call to %s  (total vars=%d  nparams=%d)\n",
+   printf("\nInline call to %s  (total vars=%d  nparams=%d)\n",
          (char *) fun->header.a_name,
          fun->parameters->num_variables, numArgs);
 #endif
@@ -1083,7 +1192,8 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
    slang_operation_copy(inlined, fun->body);
 
    /*** XXX review this */
-   assert(inlined->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE);
+   assert(inlined->type == SLANG_OPER_BLOCK_NO_NEW_SCOPE ||
+          inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE);
    inlined->type = SLANG_OPER_BLOCK_NEW_SCOPE;
 
 #if 0
@@ -1114,9 +1224,7 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
         slang_operation *decl = slang_operation_insert(&inlined->num_children,
                                                        &inlined->children,
                                                        numCopyIn);
-         /*
-         printf("COPY_IN %s from expr\n", (char*)p->a_name);
-         */
+
         decl->type = SLANG_OPER_VARIABLE_DECL;
          assert(decl->locals);
          decl->locals->outer_scope = inlined->locals;
@@ -1127,10 +1235,29 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
          /* child[0] is the var's initializer */
          slang_operation_copy(&decl->children[0], args + i);
 
+         /* add parameter 'p' to the local variable scope here */
+         {
+            slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
+            pCopy->type = p->type;
+            pCopy->a_name = p->a_name;
+            pCopy->array_len = p->array_len;
+         }
+
+         newScope = inlined->locals;
         numCopyIn++;
       }
    }
 
+   /* Now add copies of the function's local vars to the new variable scope */
+   for (i = totalArgs; i < fun->parameters->num_variables; i++) {
+      slang_variable *p = fun->parameters->variables[i];
+      slang_variable *pCopy = slang_variable_scope_grow(inlined->locals);
+      pCopy->type = p->type;
+      pCopy->a_name = p->a_name;
+      pCopy->array_len = p->array_len;
+   }
+
+
    /* New epilog statements:
     * 1. Create end of function label to jump to from return statements.
     * 2. Copy the 'out' parameter vars
@@ -1166,8 +1293,15 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
    _slang_free(substOld);
    _slang_free(substNew);
 
+   /* Update scoping to use the new local vars instead of the
+    * original function's vars.  This is especially important
+    * for nested inlining.
+    */
+   if (newScope)
+      slang_replace_scope(inlined, fun->parameters, newScope);
+
 #if 0
-   printf("Done Inline call to %s  (total vars=%d  nparams=%d)\n",
+   printf("Done Inline call to %s  (total vars=%d  nparams=%d)\n\n",
          (char *) fun->header.a_name,
          fun->parameters->num_variables, numArgs);
    slang_print_tree(top, 0);
@@ -1200,26 +1334,62 @@ _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
    }
    else {
       /* non-assembly function */
+      /* We always generate an "inline-able" block of code here.
+       * We may either:
+       *  1. insert the inline code
+       *  2. Generate a call to the "inline" code as a subroutine
+       */
+
+
+      slang_operation *ret = NULL;
+
       inlined = slang_inline_function_call(A, fun, oper, dest);
-      if (inlined && _slang_find_node_type(inlined, SLANG_OPER_RETURN)) {
-         /* This inlined function has one or more 'return' statements.
-          * So, we can't truly inline this function because we need to
-          * implement 'return' with RET (and CAL).
-          * XXX check if there's one 'return' and if it's the very last
-          * statement in the function - we can optimize that case.
-          */
-         assert(inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
-                inlined->type == SLANG_OPER_SEQUENCE);
-         inlined->type = SLANG_OPER_INLINED_CALL;
-         inlined->fun = fun;
-         inlined->label = _slang_label_new_unique((char*) fun->header.a_name);
+      if (!inlined)
+         return NULL;
+
+      ret = _slang_find_node_type(inlined, SLANG_OPER_RETURN);
+      if (ret) {
+         /* check if this is a "tail" return */
+         if (_slang_count_node_type(inlined, SLANG_OPER_RETURN) == 1 &&
+             _slang_is_tail_return(inlined)) {
+            /* The only RETURN is the last stmt in the function, no-op it
+             * and inline the function body.
+             */
+            ret->type = SLANG_OPER_NONE;
+         }
+         else {
+            slang_operation *callOper;
+            /* The function we're calling has one or more 'return' statements.
+             * So, we can't truly inline this function because we need to
+             * implement 'return' with RET (and CAL).
+             * Nevertheless, we performed "inlining" to make a new instance
+             * of the function body to deal with static register allocation.
+             *
+             * XXX check if there's one 'return' and if it's the very last
+             * statement in the function - we can optimize that case.
+             */
+            assert(inlined->type == SLANG_OPER_BLOCK_NEW_SCOPE ||
+                   inlined->type == SLANG_OPER_SEQUENCE);
+
+            if (_slang_function_has_return_value(fun) && !dest) {
+               assert(inlined->children[0].type == SLANG_OPER_VARIABLE_DECL);
+               assert(inlined->children[2].type == SLANG_OPER_IDENTIFIER);
+               callOper = &inlined->children[1];
+            }
+            else {
+               callOper = inlined;
+            }
+            callOper->type = SLANG_OPER_NON_INLINED_CALL;
+            callOper->fun = fun;
+            callOper->label = _slang_label_new_unique((char*) fun->header.a_name);
+         }
       }
    }
 
    if (!inlined)
       return NULL;
 
-   /* Replace the function call with the inlined block */
+   /* Replace the function call with the inlined block (or new CALL stmt) */
    slang_operation_destruct(oper);
    *oper = *inlined;
    _slang_free(inlined);
@@ -1254,37 +1424,209 @@ slang_find_asm_info(const char *name)
 }
 
 
+/**
+ * Return the default swizzle mask for accessing a variable of the
+ * given size (in floats).  If size = 1, comp is used to identify
+ * which component [0..3] of the register holds the variable.
+ */
+static GLuint
+_slang_var_swizzle(GLint size, GLint comp)
+{
+   switch (size) {
+   case 1:
+      return MAKE_SWIZZLE4(comp, comp, comp, comp);
+   case 2:
+      return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_NIL, SWIZZLE_NIL);
+   case 3:
+      return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_NIL);
+   default:
+      return SWIZZLE_XYZW;
+   }
+}
+
+
+/**
+ * Some write-masked assignments are simple, but others are hard.
+ * Simple example:
+ *    vec3 v;
+ *    v.xy = vec2(a, b);
+ * Hard example:
+ *    vec3 v;
+ *    v.zy = vec2(a, b);
+ * this gets transformed/swizzled into:
+ *    v.zy = vec2(a, b).*yx*         (* = don't care)
+ * This function helps to determine simple vs. non-simple.
+ */
+static GLboolean
+_slang_simple_writemask(GLuint writemask, GLuint swizzle)
+{
+   switch (writemask) {
+   case WRITEMASK_X:
+      return GET_SWZ(swizzle, 0) == SWIZZLE_X;
+   case WRITEMASK_Y:
+      return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
+   case WRITEMASK_Z:
+      return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
+   case WRITEMASK_W:
+      return GET_SWZ(swizzle, 3) == SWIZZLE_W;
+   case WRITEMASK_XY:
+      return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
+         && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
+   case WRITEMASK_XYZ:
+      return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
+         && (GET_SWZ(swizzle, 1) == SWIZZLE_Y)
+         && (GET_SWZ(swizzle, 2) == SWIZZLE_Z);
+   case WRITEMASK_XYZW:
+      return swizzle == SWIZZLE_NOOP;
+   default:
+      return GL_FALSE;
+   }
+}
+
+
+/**
+ * Convert the given swizzle into a writemask.  In some cases this
+ * is trivial, in other cases, we'll need to also swizzle the right
+ * hand side to put components in the right places.
+ * See comment above for more info.
+ * XXX this function could be simplified and should probably be renamed.
+ * \param swizzle  the incoming swizzle
+ * \param writemaskOut  returns the writemask
+ * \param swizzleOut  swizzle to apply to the right-hand-side
+ * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
+ */
+static GLboolean
+swizzle_to_writemask(slang_assemble_ctx *A, GLuint swizzle,
+                     GLuint *writemaskOut, GLuint *swizzleOut)
+{
+   GLuint mask = 0x0, newSwizzle[4];
+   GLint i, size;
+
+   /* make new dst writemask, compute size */
+   for (i = 0; i < 4; i++) {
+      const GLuint swz = GET_SWZ(swizzle, i);
+      if (swz == SWIZZLE_NIL) {
+         /* end */
+         break;
+      }
+      assert(swz >= 0 && swz <= 3);
+
+      if (swizzle != SWIZZLE_XXXX &&
+          swizzle != SWIZZLE_YYYY &&
+          swizzle != SWIZZLE_ZZZZ &&
+          swizzle != SWIZZLE_WWWW &&
+          (mask & (1 << swz))) {
+         /* a channel can't be specified twice (ex: ".xyyz") */
+         slang_info_log_error(A->log, "Invalid writemask '%s'",
+                              _mesa_swizzle_string(swizzle, 0, 0));
+         return GL_FALSE;
+      }
+
+      mask |= (1 << swz);
+   }
+   assert(mask <= 0xf);
+   size = i;  /* number of components in mask/swizzle */
+
+   *writemaskOut = mask;
+
+   /* make new src swizzle, by inversion */
+   for (i = 0; i < 4; i++) {
+      newSwizzle[i] = i; /*identity*/
+   }
+   for (i = 0; i < size; i++) {
+      const GLuint swz = GET_SWZ(swizzle, i);
+      newSwizzle[swz] = i;
+   }
+   *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
+                               newSwizzle[1],
+                               newSwizzle[2],
+                               newSwizzle[3]);
+
+   if (_slang_simple_writemask(mask, *swizzleOut)) {
+      if (size >= 1)
+         assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
+      if (size >= 2)
+         assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
+      if (size >= 3)
+         assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
+      if (size >= 4)
+         assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
+      return GL_TRUE;
+   }
+   else
+      return GL_FALSE;
+}
+
+
+/**
+ * Recursively traverse 'oper' to produce a swizzle mask in the event
+ * of any vector subscripts and swizzle suffixes.
+ * Ex:  for "vec4 v",  "v[2].x" resolves to v.z
+ */
 static GLuint
-make_writemask(const char *field)
+resolve_swizzle(const slang_operation *oper)
 {
-   GLuint mask = 0x0;
-   while (*field) {
-      switch (*field) {
-      case 'x':
-         mask |= WRITEMASK_X;
+   if (oper->type == SLANG_OPER_FIELD) {
+      /* writemask from .xyzw suffix */
+      slang_swizzle swz;
+      if (_slang_is_swizzle((char*) oper->a_id, 4, &swz)) {
+         GLuint swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
+                                        swz.swizzle[1],
+                                        swz.swizzle[2],
+                                        swz.swizzle[3]);
+         GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
+         GLuint s = _slang_swizzle_swizzle(child_swizzle, swizzle);
+         return s;
+      }
+      else
+         return SWIZZLE_XYZW;
+   }
+   else if (oper->type == SLANG_OPER_SUBSCRIPT &&
+            oper->children[1].type == SLANG_OPER_LITERAL_INT) {
+      /* writemask from [index] */
+      GLuint child_swizzle = resolve_swizzle(&oper->children[0]);
+      GLuint i = (GLuint) oper->children[1].literal[0];
+      GLuint swizzle;
+      GLuint s;
+      switch (i) {
+      case 0:
+         swizzle = SWIZZLE_XXXX;
          break;
-      case 'y':
-         mask |= WRITEMASK_Y;
+      case 1:
+         swizzle = SWIZZLE_YYYY;
          break;
-      case 'z':
-         mask |= WRITEMASK_Z;
+      case 2:
+         swizzle = SWIZZLE_ZZZZ;
          break;
-      case 'w':
-         mask |= WRITEMASK_W;
+      case 3:
+         swizzle = SWIZZLE_WWWW;
          break;
       default:
-         _mesa_problem(NULL, "invalid writemask in make_writemask()");
-         return 0;
+         swizzle = SWIZZLE_XYZW;
       }
-      field++;
+      s = _slang_swizzle_swizzle(child_swizzle, swizzle);
+      return s;
    }
-   if (mask == 0x0)
-      return WRITEMASK_XYZW;
-   else
-      return mask;
+   else {
+      return SWIZZLE_XYZW;
+   }
+}
+
+
+/**
+ * Recursively descend through swizzle nodes to find the node's storage info.
+ */
+static slang_ir_storage *
+get_store(const slang_ir_node *n)
+{
+   if (n->Opcode == IR_SWIZZLE) {
+      return get_store(n->Children[0]);
+   }
+   return n->Store;
 }
 
 
+
 /**
  * Generate IR tree for an asm instruction/operation such as:
  *    __asm vec4_dot __retVal.x, v1, v2;
@@ -1309,13 +1651,13 @@ _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
 
    if (info->NumParams == oper->num_children) {
       /* Storage for result is not specified.
-       * Children[0], [1] are the operands.
+       * Children[0], [1], [2] are the operands.
        */
       firstOperand = 0;
    }
    else {
       /* Storage for result (child[0]) is specified.
-       * Children[1], [2] are the operands.
+       * Children[1], [2], [3] are the operands.
        */
       firstOperand = 1;
    }
@@ -1334,23 +1676,19 @@ _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
       /* Setup n->Store to be a particular location.  Otherwise, storage
        * for the result (a temporary) will be allocated later.
        */
-      GLuint writemask = WRITEMASK_XYZW;
       slang_operation *dest_oper;
       slang_ir_node *n0;
 
       dest_oper = &oper->children[0];
-      while (dest_oper->type == SLANG_OPER_FIELD) {
-         /* writemask */
-         writemask &= make_writemask((char*) dest_oper->a_id);
-         dest_oper = &dest_oper->children[0];
-      }
 
       n0 = _slang_gen_operation(A, dest_oper);
-      assert(n0->Var);
-      assert(n0->Store);
+      if (!n0)
+         return NULL;
+
       assert(!n->Store);
       n->Store = n0->Store;
-      n->Writemask = writemask;
+
+      assert(n->Store->File != PROGRAM_UNDEFINED || n->Store->Parent);
 
       _slang_free(n0);
    }
@@ -1375,7 +1713,7 @@ print_funcs(struct slang_function_scope_ *scope, const char *name)
 
 
 /**
- * Return first function in the scope that has the given name.
+ * Find a function of the given name, taking 'numArgs' arguments.
  * This is the function we'll try to call when there is no exact match
  * between function parameters and call arguments.
  *
@@ -1383,57 +1721,411 @@ print_funcs(struct slang_function_scope_ *scope, const char *name)
  * all of them...
  */
 static slang_function *
-_slang_first_function(struct slang_function_scope_ *scope, const char *name)
+_slang_find_function_by_argc(slang_function_scope *scope,
+                             const char *name, int numArgs)
 {
-   GLuint i;
-   for (i = 0; i < scope->num_functions; i++) {
-      slang_function *f = &scope->functions[i];
-      if (strcmp(name, (char*) f->header.a_name) == 0)
-         return f;
+   while (scope) {
+      GLuint i;
+      for (i = 0; i < scope->num_functions; i++) {
+         slang_function *f = &scope->functions[i];
+         if (strcmp(name, (char*) f->header.a_name) == 0) {
+            int haveRetValue = _slang_function_has_return_value(f);
+            if (numArgs == f->param_count - haveRetValue)
+               return f;
+         }
+      }
+      scope = scope->outer_scope;
    }
-   if (scope->outer_scope)
-      return _slang_first_function(scope->outer_scope, name);
+
    return NULL;
 }
 
 
+static slang_function *
+_slang_find_function_by_max_argc(slang_function_scope *scope,
+                                 const char *name)
+{
+   slang_function *maxFunc = NULL;
+   GLuint maxArgs = 0;
+
+   while (scope) {
+      GLuint i;
+      for (i = 0; i < scope->num_functions; i++) {
+         slang_function *f = &scope->functions[i];
+         if (strcmp(name, (char*) f->header.a_name) == 0) {
+            if (f->param_count > maxArgs) {
+               maxArgs = f->param_count;
+               maxFunc = f;
+            }
+         }
+      }
+      scope = scope->outer_scope;
+   }
+
+   return maxFunc;
+}
+
 
 /**
- * Assemble a function call, given a particular function name.
- * \param name  the function's name (operators like '*' are possible).
+ * Generate a new slang_function which is a constructor for a user-defined
+ * struct type.
  */
-static slang_ir_node *
-_slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
-                              slang_operation *oper, slang_operation *dest)
+static slang_function *
+_slang_make_struct_constructor(slang_assemble_ctx *A, slang_struct *str)
 {
-   slang_operation *params = oper->children;
-   const GLuint param_count = oper->num_children;
-   slang_atom atom;
-   slang_function *fun;
+   const GLint numFields = str->fields->num_variables;
+   slang_function *fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
 
-   atom = slang_atom_pool_atom(A->atoms, name);
-   if (atom == SLANG_ATOM_NULL)
-      return NULL;
+   /* function header (name, return type) */
+   fun->header.a_name = str->a_name;
+   fun->header.type.qualifier = SLANG_QUAL_NONE;
+   fun->header.type.specifier.type = SLANG_SPEC_STRUCT;
+   fun->header.type.specifier._struct = str;
 
-   /*
-    * Use 'name' to find the function to call
-    */
-   fun = _slang_locate_function(A->space.funcs, atom, params, param_count,
-                               &A->space, A->atoms, A->log);
-   if (!fun) {
-      /* A function with exactly the right parameters/types was not found.
-       * Try adapting the parameters.
-       */
-      fun = _slang_first_function(A->space.funcs, name);
-      if (!fun || !_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
-         slang_info_log_error(A->log, "Function '%s' not found (check argument types)", name);
-         return NULL;
+   /* function parameters (= struct's fields) */
+   {
+      GLint i;
+      for (i = 0; i < numFields; i++) {
+         /*
+         printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
+         */
+         slang_variable *p = slang_variable_scope_grow(fun->parameters);
+         *p = *str->fields->variables[i]; /* copy the type */
+         p->type.qualifier = SLANG_QUAL_CONST;
       }
-      assert(fun);
+      fun->param_count = fun->parameters->num_variables;
    }
 
-   return _slang_gen_function_call(A, fun, oper, dest);
-}
+   /* Add __retVal to params */
+   {
+      slang_variable *p = slang_variable_scope_grow(fun->parameters);
+      slang_atom a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
+      assert(a_retVal);
+      p->a_name = a_retVal;
+      p->type = fun->header.type;
+      p->type.qualifier = SLANG_QUAL_OUT;
+      fun->param_count++;
+   }
+
+   /* function body is:
+    *    block:
+    *       declare T;
+    *       T.f1 = p1;
+    *       T.f2 = p2;
+    *       ...
+    *       T.fn = pn;
+    *       return T;
+    */
+   {
+      slang_variable_scope *scope;
+      slang_variable *var;
+      GLint i;
+
+      fun->body = slang_operation_new(1);
+      fun->body->type = SLANG_OPER_BLOCK_NEW_SCOPE;
+      fun->body->num_children = numFields + 2;
+      fun->body->children = slang_operation_new(numFields + 2);
+
+      scope = fun->body->locals;
+      scope->outer_scope = fun->parameters;
+
+      /* create local var 't' */
+      var = slang_variable_scope_grow(scope);
+      var->a_name = slang_atom_pool_atom(A->atoms, "t");
+      var->type = fun->header.type;
+
+      /* declare t */
+      {
+         slang_operation *decl;
+
+         decl = &fun->body->children[0];
+         decl->type = SLANG_OPER_VARIABLE_DECL;
+         decl->locals = _slang_variable_scope_new(scope);
+         decl->a_id = var->a_name;
+      }
+
+      /* assign params to fields of t */
+      for (i = 0; i < numFields; i++) {
+         slang_operation *assign = &fun->body->children[1 + i];
+
+         assign->type = SLANG_OPER_ASSIGN;
+         assign->locals = _slang_variable_scope_new(scope);
+         assign->num_children = 2;
+         assign->children = slang_operation_new(2);
+         
+         {
+            slang_operation *lhs = &assign->children[0];
+
+            lhs->type = SLANG_OPER_FIELD;
+            lhs->locals = _slang_variable_scope_new(scope);
+            lhs->num_children = 1;
+            lhs->children = slang_operation_new(1);
+            lhs->a_id = str->fields->variables[i]->a_name;
+
+            lhs->children[0].type = SLANG_OPER_IDENTIFIER;
+            lhs->children[0].a_id = var->a_name;
+            lhs->children[0].locals = _slang_variable_scope_new(scope);
+
+#if 0
+            lhs->children[1].num_children = 1;
+            lhs->children[1].children = slang_operation_new(1);
+            lhs->children[1].children[0].type = SLANG_OPER_IDENTIFIER;
+            lhs->children[1].children[0].a_id = str->fields->variables[i]->a_name;
+            lhs->children[1].children->locals = _slang_variable_scope_new(scope);
+#endif
+         }
+
+         {
+            slang_operation *rhs = &assign->children[1];
+
+            rhs->type = SLANG_OPER_IDENTIFIER;
+            rhs->locals = _slang_variable_scope_new(scope);
+            rhs->a_id = str->fields->variables[i]->a_name;
+         }         
+      }
+
+      /* return t; */
+      {
+         slang_operation *ret = &fun->body->children[numFields + 1];
+
+         ret->type = SLANG_OPER_RETURN;
+         ret->locals = _slang_variable_scope_new(scope);
+         ret->num_children = 1;
+         ret->children = slang_operation_new(1);
+         ret->children[0].type = SLANG_OPER_IDENTIFIER;
+         ret->children[0].a_id = var->a_name;
+         ret->children[0].locals = _slang_variable_scope_new(scope);
+      }
+   }
+   /*
+   slang_print_function(fun, 1);
+   */
+   return fun;
+}
+
+
+/**
+ * Find/create a function (constructor) for the given structure name.
+ */
+static slang_function *
+_slang_locate_struct_constructor(slang_assemble_ctx *A, const char *name)
+{
+   unsigned int i;
+   for (i = 0; i < A->space.structs->num_structs; i++) {
+      slang_struct *str = &A->space.structs->structs[i];
+      if (strcmp(name, (const char *) str->a_name) == 0) {
+         /* found a structure type that matches the function name */
+         if (!str->constructor) {
+            /* create the constructor function now */
+            str->constructor = _slang_make_struct_constructor(A, str);
+         }
+         return str->constructor;
+      }
+   }
+   return NULL;
+}
+
+
+/**
+ * Generate a new slang_function to satisfy a call to an array constructor.
+ * Ex:  float[3](1., 2., 3.)
+ */
+static slang_function *
+_slang_make_array_constructor(slang_assemble_ctx *A, slang_operation *oper)
+{
+   slang_function *fun = slang_function_new(SLANG_FUNC_CONSTRUCTOR);
+   if (fun) {
+      slang_type_specifier_type baseType =
+         slang_type_specifier_type_from_string((char *) oper->a_id);
+
+      fun->header.a_name = oper->a_id;
+      fun->header.type.qualifier = SLANG_QUAL_NONE;
+      fun->header.type.specifier.type = SLANG_SPEC_ARRAY;
+      fun->header.type.specifier._array =
+         slang_type_specifier_new(baseType, NULL, NULL);
+
+
+   }
+   return fun;
+}
+
+
+static GLboolean
+_slang_is_vec_mat_type(const char *name)
+{
+   static const char *vecmat_types[] = {
+      "float", "int", "bool",
+      "vec2", "vec3", "vec4",
+      "ivec2", "ivec3", "ivec4",
+      "bvec2", "bvec3", "bvec4",
+      "mat2", "mat3", "mat4",
+      "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
+      NULL
+   };
+   int i;
+   for (i = 0; vecmat_types[i]; i++)
+      if (_mesa_strcmp(name, vecmat_types[i]) == 0)
+         return GL_TRUE;
+   return GL_FALSE;
+}
+
+
+/**
+ * Assemble a function call, given a particular function name.
+ * \param name  the function's name (operators like '*' are possible).
+ */
+static slang_ir_node *
+_slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
+                              slang_operation *oper, slang_operation *dest)
+{
+   slang_operation *params = oper->children;
+   const GLuint param_count = oper->num_children;
+   slang_atom atom;
+   slang_function *fun;
+   GLboolean error;
+   slang_ir_node *n;
+
+   atom = slang_atom_pool_atom(A->atoms, name);
+   if (atom == SLANG_ATOM_NULL)
+      return NULL;
+
+   if (oper->array_constructor) {
+      /* this needs special handling */
+      fun = _slang_make_array_constructor(A, oper);
+   }
+   else {
+      /* Try to find function by name and exact argument type matching */
+      fun = _slang_function_locate(A->space.funcs, atom, params, param_count,
+                                   &A->space, A->atoms, A->log, &error);
+   }
+
+   if (error) {
+      slang_info_log_error(A->log,
+                           "Function '%s' not found (check argument types)",
+                           name);
+      return NULL;
+   }
+
+   if (!fun) {
+      /* Next, try locating a constructor function for a user-defined type */
+      fun = _slang_locate_struct_constructor(A, name);
+   }
+
+   /*
+    * At this point, some heuristics are used to try to find a function
+    * that matches the calling signature by means of casting or "unrolling"
+    * of constructors.
+    */
+
+   if (!fun && _slang_is_vec_mat_type(name)) {
+      /* Next, if this call looks like a vec() or mat() constructor call,
+       * try "unwinding" the args to satisfy a constructor.
+       */
+      fun = _slang_find_function_by_max_argc(A->space.funcs, name);
+      if (fun) {
+         if (!_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
+            slang_info_log_error(A->log,
+                                 "Function '%s' not found (check argument types)",
+                                 name);
+            return NULL;
+         }
+      }
+   }
+
+   if (!fun && _slang_is_vec_mat_type(name)) {
+      /* Next, try casting args to the types of the formal parameters */
+      int numArgs = oper->num_children;
+      fun = _slang_find_function_by_argc(A->space.funcs, name, numArgs);
+      if (!fun || !_slang_cast_func_params(oper, fun, &A->space, A->atoms, A->log)) {
+         slang_info_log_error(A->log,
+                              "Function '%s' not found (check argument types)",
+                              name);
+         return NULL;
+      }
+      assert(fun);
+   }
+
+   if (!fun) {
+      slang_info_log_error(A->log,
+                           "Function '%s' not found (check argument types)",
+                           name);
+      return NULL;
+   }
+   if (!fun->body) {
+      slang_info_log_error(A->log,
+                           "Function '%s' prototyped but not defined.  "
+                           "Separate compilation units not supported.",
+                           name);
+      return NULL;
+   }
+
+   /* type checking to be sure function's return type matches 'dest' type */
+   if (dest) {
+      slang_typeinfo t0;
+
+      slang_typeinfo_construct(&t0);
+      typeof_operation(A, dest, &t0);
+
+      if (!slang_type_specifier_equal(&t0.spec, &fun->header.type.specifier)) {
+         slang_info_log_error(A->log,
+                              "Incompatible type returned by call to '%s'",
+                              name);
+         return NULL;
+      }
+   }
+
+   n = _slang_gen_function_call(A, fun, oper, dest);
+
+   if (n && !n->Store && !dest
+       && fun->header.type.specifier.type != SLANG_SPEC_VOID) {
+      /* setup n->Store for the result of the function call */
+      GLint size = _slang_sizeof_type_specifier(&fun->header.type.specifier);
+      n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
+      /*printf("Alloc storage for function result, size %d \n", size);*/
+   }
+
+   return n;
+}
+
+
+static slang_ir_node *
+_slang_gen_method_call(slang_assemble_ctx *A, slang_operation *oper)
+{
+   slang_atom *a_length = slang_atom_pool_atom(A->atoms, "length");
+   slang_ir_node *n;
+   slang_variable *var;
+
+   /* NOTE: In GLSL 1.20, there's only one kind of method
+    * call: array.length().  Anything else is an error.
+    */
+   if (oper->a_id != a_length) {
+      slang_info_log_error(A->log,
+                           "Undefined method call '%s'", (char *) oper->a_id);
+      return NULL;
+   }
+
+   /* length() takes no arguments */
+   if (oper->num_children > 0) {
+      slang_info_log_error(A->log, "Invalid arguments to length() method");
+      return NULL;
+   }
+
+   /* lookup the object/variable */
+   var = _slang_variable_locate(oper->locals, oper->a_obj, GL_TRUE);
+   if (!var || var->type.specifier.type != SLANG_SPEC_ARRAY) {
+      slang_info_log_error(A->log,
+                           "Undefined object '%s'", (char *) oper->a_obj);
+      return NULL;
+   }
+
+   /* Create a float/literal IR node encoding the array length */
+   n = new_node0(IR_FLOAT);
+   if (n) {
+      n->Value[0] = (float) var->array_len;
+      n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, 1);
+   }
+   return n;
+}
 
 
 static GLboolean
@@ -1466,13 +2158,30 @@ _slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
    GLint size;
 
    slang_typeinfo_construct(&type);
-   _slang_typeof_operation(A, oper, &type);
+   typeof_operation(A, oper, &type);
    size = _slang_sizeof_type_specifier(&type.spec);
    slang_typeinfo_destruct(&type);
    return size == 1;
 }
 
 
+/**
+ * Test if an operation is boolean.
+ */
+static GLboolean
+_slang_is_boolean(slang_assemble_ctx *A, slang_operation *oper)
+{
+   slang_typeinfo type;
+   GLboolean isBool;
+
+   slang_typeinfo_construct(&type);
+   typeof_operation(A, oper, &type);
+   isBool = (type.spec.type == SLANG_SPEC_BOOL);
+   slang_typeinfo_destruct(&type);
+   return isBool;
+}
+
+
 /**
  * Generate loop code using high-level IR_LOOP instruction
  */
@@ -1488,7 +2197,7 @@ _slang_gen_while(slang_assemble_ctx * A, const slang_operation *oper)
    GLboolean isConst, constTrue;
 
    /* type-check expression */
-   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+   if (!_slang_is_boolean(A, &oper->children[0])) {
       slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
       return NULL;
    }
@@ -1551,7 +2260,7 @@ _slang_gen_do(slang_assemble_ctx * A, const slang_operation *oper)
    GLboolean isConst, constTrue;
 
    /* type-check expression */
-   if (!_slang_is_scalar_or_boolean(A, &oper->children[1])) {
+   if (!_slang_is_boolean(A, &oper->children[1])) {
       slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
       return NULL;
    }
@@ -1680,6 +2389,11 @@ _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
    GLboolean isConst, constTrue;
 
    /* type-check expression */
+   if (!_slang_is_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log, "boolean expression expected for 'if'");
+      return NULL;
+   }
+
    if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
       slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
       return NULL;
@@ -1700,22 +2414,16 @@ _slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
    cond = _slang_gen_operation(A, &oper->children[0]);
    cond = new_cond(cond);
 
-   if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)) {
+   if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)
+       && !haveElseClause) {
       /* Special case: generate a conditional break */
       ifBody = new_break_if_true(A->CurLoop, cond);
-      if (haveElseClause) {
-         elseBody = _slang_gen_operation(A, &oper->children[2]);
-         return new_seq(ifBody, elseBody);
-      }
       return ifBody;
    }
-   else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)) {
+   else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)
+            && !haveElseClause) {
       /* Special case: generate a conditional break */
       ifBody = new_cont_if_true(A->CurLoop, cond);
-      if (haveElseClause) {
-         elseBody = _slang_gen_operation(A, &oper->children[2]);
-         return new_seq(ifBody, elseBody);
-      }
       return ifBody;
    }
    else {
@@ -1787,7 +2495,7 @@ _slang_gen_temporary(GLint size)
    slang_ir_storage *store;
    slang_ir_node *n = NULL;
 
-   store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
+   store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -2, size);
    if (store) {
       n = new_node0(IR_VAR_DECL);
       if (n) {
@@ -1803,25 +2511,128 @@ _slang_gen_temporary(GLint size)
 
 /**
  * Generate IR node for allocating/declaring a variable.
+ * \param initializer  Optional initializer expression for the variable.
  */
 static slang_ir_node *
-_slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var)
+_slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var,
+                    slang_operation *initializer)
 {
-   slang_ir_node *n;
-   assert(!is_sampler_type(&var->type));
-   n = new_node0(IR_VAR_DECL);
-   if (n) {
-      _slang_attach_storage(n, var);
+   slang_ir_node *varDecl, *n;
+   slang_ir_storage *store;
 
-      assert(var->aux);
-      assert(n->Store == var->aux);
-      assert(n->Store);
-      assert(n->Store->Index < 0);
+   /*assert(!var->declared);*/
+   var->declared = GL_TRUE;
+
+   varDecl = new_node0(IR_VAR_DECL);
+   if (!varDecl)
+      return NULL;
+
+   _slang_attach_storage(varDecl, var);
+   assert(var->store);
+   assert(varDecl->Store == var->store);
+   assert(varDecl->Store);
+   assert(varDecl->Store->Index < 0);
+   store = var->store;
+
+   assert(store == varDecl->Store);
+
+   /* determine GPU storage file */
+   /* XXX if the variable is const, use PROGRAM_CONSTANT */
+   if (is_sampler_type(&var->type)) {
+      store->File = PROGRAM_SAMPLER;
+   }
+   else {
+      store->File = PROGRAM_TEMPORARY;
+   }
 
-      n->Store->File = PROGRAM_TEMPORARY;
-      n->Store->Size = _slang_sizeof_type_specifier(&n->Var->type.specifier);
-      assert(n->Store->Size > 0);
+   store->Size = _slang_sizeof_type_specifier(&varDecl->Var->type.specifier);
+
+   if (store->Size <= 0) {
+      slang_info_log_error(A->log, "invalid declaration for '%s'",
+                           (char*) var->a_name);
+      return NULL;
+   }
+
+#if 0
+   printf("%s var %p %s  store=%p index=%d size=%d\n",
+          __FUNCTION__, (void *) var, (char *) var->a_name,
+          (void *) store, store->Index, store->Size);
+#endif
+
+   if (var->array_len > 0) {
+      /* this is an array */
+      /* round up the element size to a multiple of 4 */
+      GLint sz = (store->Size + 3) & ~3;
+      /* total size = element size * array length */
+      sz *= var->array_len;
+      store->Size = sz;
+   }
+
+   /* setup default swizzle for storing the variable */
+   /* XXX this may not be needed anymore - remove & test */
+   switch (store->Size) {
+   case 2:
+      store->Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
+                                     SWIZZLE_NIL, SWIZZLE_NIL);
+      break;
+   case 3:
+      store->Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
+                                     SWIZZLE_Z, SWIZZLE_NIL);
+      break;
+   default:
+      /* Note that float-sized vars may be allocated in any x/y/z/w
+       * slot, but that won't be determined until code emit time.
+       */
+      store->Swizzle = SWIZZLE_NOOP;
+   }
+
+   /* if there's an initializer, generate IR for the expression */
+   if (initializer) {
+      const char *varName = (const char *) var->a_name;
+      slang_ir_node *varRef, *init;
+
+      varRef = new_var(A, var);
+      if (!varRef) {
+         slang_info_log_error(A->log, "undefined variable '%s'", varName);
+         return NULL;
+      }
+
+      if (var->type.qualifier == SLANG_QUAL_CONST) {
+         /* if the variable is const, the initializer must be a const
+          * expression as well.
+          */
+#if 0
+         if (!_slang_is_constant_expr(initializer)) {
+            slang_info_log_error(A->log,
+                                 "initializer for %s not constant", varName);
+            return NULL;
+         }
+#endif
+      }
+
+      /* constant-folding, etc here */
+      _slang_simplify(initializer, &A->space, A->atoms); 
+
+      init = _slang_gen_operation(A, initializer);
+      if (!init)
+         return NULL;
+
+      /*assert(init->Store);*/
+
+      /* XXX remove this when type checking is added above */
+      if (init->Store && varRef->Store->Size != init->Store->Size) {
+         slang_info_log_error(A->log, "invalid assignment (wrong types)");
+         return NULL;
+      }
+
+      n = new_node2(IR_COPY, varRef, init);
+      n = new_seq(varDecl, n);
    }
+   else {
+      /* no initializer */
+      n = varDecl;
+   }
+
    return n;
 }
 
@@ -1837,16 +2648,36 @@ _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
 {
    slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
    slang_ir_node *tmpDecl, *tmpVar, *tree;
-   slang_typeinfo type;
-   int size;
+   slang_typeinfo type0, type1, type2;
+   int size, isBool, isEqual;
 
    assert(oper->type == SLANG_OPER_SELECT);
    assert(oper->num_children == 3);
 
+   /* type of children[0] must be boolean */
+   slang_typeinfo_construct(&type0);
+   typeof_operation(A, &oper->children[0], &type0);
+   isBool = (type0.spec.type == SLANG_SPEC_BOOL);
+   slang_typeinfo_destruct(&type0);
+   if (!isBool) {
+      slang_info_log_error(A->log, "selector type is not boolean");
+      return NULL;
+   }
+
+   slang_typeinfo_construct(&type1);
+   slang_typeinfo_construct(&type2);
+   typeof_operation(A, &oper->children[1], &type1);
+   typeof_operation(A, &oper->children[2], &type2);
+   isEqual = slang_type_specifier_equal(&type1.spec, &type2.spec);
+   slang_typeinfo_destruct(&type1);
+   slang_typeinfo_destruct(&type2);
+   if (!isEqual) {
+      slang_info_log_error(A->log, "incompatible types for ?: operator");
+      return NULL;
+   }
+
    /* size of x or y's type */
-   slang_typeinfo_construct(&type);
-   _slang_typeof_operation(A, &oper->children[1], &type);
-   size = _slang_sizeof_type_specifier(&type.spec);
+   size = _slang_sizeof_type_specifier(&type1.spec);
    assert(size > 0);
 
    /* temporary var */
@@ -1860,13 +2691,13 @@ _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
    tmpVar = new_node0(IR_VAR);
    tmpVar->Store = tmpDecl->Store;
    trueExpr = _slang_gen_operation(A, &oper->children[1]);
-   trueNode = new_node2(IR_MOVE, tmpVar, trueExpr);
+   trueNode = new_node2(IR_COPY, tmpVar, trueExpr);
 
    /* if-false body (child 2) */
    tmpVar = new_node0(IR_VAR);
    tmpVar->Store = tmpDecl->Store;
    falseExpr = _slang_gen_operation(A, &oper->children[2]);
-   falseNode = new_node2(IR_MOVE, tmpVar, falseExpr);
+   falseNode = new_node2(IR_COPY, tmpVar, falseExpr);
 
    ifNode = new_if(cond, trueNode, falseNode);
 
@@ -1941,8 +2772,7 @@ static slang_ir_node *
 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
 {
    const GLboolean haveReturnValue
-      = (oper->num_children == 1 &&
-         oper->children[0].type != SLANG_OPER_VOID);
+      = (oper->num_children == 1 && oper->children[0].type != SLANG_OPER_VOID);
 
    /* error checking */
    assert(A->CurFunction);
@@ -1952,7 +2782,7 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
       return NULL;
    }
    else if (!haveReturnValue &&
-       A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
+            A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
       slang_info_log_error(A->log, "return statement requires an expression");
       return NULL;
    }
@@ -1977,8 +2807,8 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
 
 #if 1 /* DEBUG */
       {
-         slang_variable *v
-            = _slang_locate_variable(oper->locals, a_retVal, GL_TRUE);
+         slang_variable *v =
+            _slang_variable_locate(oper->locals, a_retVal, GL_TRUE);
          if (!v) {
             /* trying to return a value in a void-valued function */
             return NULL;
@@ -2009,73 +2839,160 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
 
 
 /**
- * Generate IR tree for a variable declaration.
+ * Determine if the given operation/expression is const-valued.
+ */
+static GLboolean
+_slang_is_constant_expr(const slang_operation *oper)
+{
+   slang_variable *var;
+   GLuint i;
+
+   switch (oper->type) {
+   case SLANG_OPER_IDENTIFIER:
+      var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
+      if (var && var->type.qualifier == SLANG_QUAL_CONST)
+         return GL_TRUE;
+      return GL_FALSE;
+   default:
+      for (i = 0; i < oper->num_children; i++) {
+         if (!_slang_is_constant_expr(&oper->children[i]))
+            return GL_FALSE;
+      }
+      return GL_TRUE;
+   }
+}
+
+
+/**
+ * Check if an assignment of type t1 to t0 is legal.
+ * XXX more cases needed.
+ */
+static GLboolean
+_slang_assignment_compatible(slang_assemble_ctx *A,
+                             slang_operation *op0,
+                             slang_operation *op1)
+{
+   slang_typeinfo t0, t1;
+   GLuint sz0, sz1;
+
+   if (op0->type == SLANG_OPER_POSTINCREMENT ||
+       op0->type == SLANG_OPER_POSTDECREMENT) {
+      return GL_FALSE;
+   }
+
+   slang_typeinfo_construct(&t0);
+   typeof_operation(A, op0, &t0);
+
+   slang_typeinfo_construct(&t1);
+   typeof_operation(A, op1, &t1);
+
+   sz0 = _slang_sizeof_type_specifier(&t0.spec);
+   sz1 = _slang_sizeof_type_specifier(&t1.spec);
+
+#if 1
+   if (sz0 != sz1) {
+      /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
+      return GL_FALSE;
+   }
+#endif
+
+   if (t0.spec.type == SLANG_SPEC_STRUCT &&
+       t1.spec.type == SLANG_SPEC_STRUCT &&
+       t0.spec._struct->a_name != t1.spec._struct->a_name)
+      return GL_FALSE;
+
+   if (t0.spec.type == SLANG_SPEC_FLOAT &&
+       t1.spec.type == SLANG_SPEC_BOOL)
+      return GL_FALSE;
+
+#if 0 /* not used just yet - causes problems elsewhere */
+   if (t0.spec.type == SLANG_SPEC_INT &&
+       t1.spec.type == SLANG_SPEC_FLOAT)
+      return GL_FALSE;
+#endif
+
+   if (t0.spec.type == SLANG_SPEC_BOOL &&
+       t1.spec.type == SLANG_SPEC_FLOAT)
+      return GL_FALSE;
+
+   if (t0.spec.type == SLANG_SPEC_BOOL &&
+       t1.spec.type == SLANG_SPEC_INT)
+      return GL_FALSE;
+
+   return GL_TRUE;
+}
+
+
+
+/**
+ * Generate IR tree for a local variable declaration.
  */
 static slang_ir_node *
 _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
 {
-   slang_ir_node *n;
-   slang_ir_node *varDecl;
-   slang_variable *v;
    const char *varName = (char *) oper->a_id;
+   slang_variable *var;
+   slang_ir_node *varDecl;
+   slang_operation *initializer;
 
-   assert(oper->num_children == 0 || oper->num_children == 1);
-
-   v = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
-   assert(v);
+   assert(oper->type == SLANG_OPER_VARIABLE_DECL);
+   assert(oper->num_children <= 1);
 
-   varDecl = _slang_gen_var_decl(A, v);
+   /* lookup the variable by name */
+   var = _slang_variable_locate(oper->locals, oper->a_id, GL_TRUE);
+   if (!var)
+      return NULL;  /* "shouldn't happen" */
 
-   if (oper->num_children > 0) {
-      /* child is initializer */
-      slang_ir_node *var, *init, *rhs;
-      assert(oper->num_children == 1);
-      var = new_var(A, oper, oper->a_id);
-      if (!var) {
-         slang_info_log_error(A->log, "undefined variable '%s'", varName);
-         return NULL;
-      }
-      /* XXX make copy of this initializer? */
-      rhs = _slang_gen_operation(A, &oper->children[0]);
-      assert(rhs);
-      init = new_node2(IR_MOVE, var, rhs);
-      /*assert(rhs->Opcode != IR_SEQ);*/
-      n = new_seq(varDecl, init);
+   if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
+       var->type.qualifier == SLANG_QUAL_VARYING ||
+       var->type.qualifier == SLANG_QUAL_UNIFORM) {
+      /* can't declare attribute/uniform vars inside functions */
+      slang_info_log_error(A->log,
+                "local variable '%s' cannot be an attribute/uniform/varying",
+                varName);
+      return NULL;
    }
-   else if (v->initializer) {
-      slang_ir_node *var, *init, *rhs;
-      var = new_var(A, oper, oper->a_id);
-      if (!var) {
-         slang_info_log_error(A->log, "undefined variable '%s'", varName);
-         return NULL;
-      }
+
 #if 0
-      /* XXX make copy of this initializer? */
-      {
-         slang_operation dup;
-         slang_operation_construct(&dup);
-         slang_operation_copy(&dup, v->initializer);
-         _slang_simplify(&dup, &A->space, A->atoms); 
-         rhs = _slang_gen_operation(A, &dup);
-      }
-#else
-      _slang_simplify(v->initializer, &A->space, A->atoms); 
-      rhs = _slang_gen_operation(A, v->initializer);
+   if (v->declared) {
+      slang_info_log_error(A->log, "variable '%s' redeclared", varName);
+      return NULL;
+   }
 #endif
-      if (!rhs)
-         return NULL;
 
-      assert(rhs);
-      init = new_node2(IR_MOVE, var, rhs);
-      /*
-        assert(rhs->Opcode != IR_SEQ);
-      */
-      n = new_seq(varDecl, init);
+   /* check if the var has an initializer */
+   if (oper->num_children > 0) {
+      assert(oper->num_children == 1);
+      initializer = &oper->children[0];
+   }
+   else if (var->initializer) {
+      initializer = var->initializer;
    }
    else {
-      n = varDecl;
+      initializer = NULL;
    }
-   return n;
+
+   if (initializer) {
+      /* check/compare var type and initializer type */
+      if (!_slang_assignment_compatible(A, oper, initializer)) {
+         slang_info_log_error(A->log, "incompatible types in assignment");
+         return NULL;
+      }         
+   }
+
+   /* Generate IR node */
+   varDecl = _slang_gen_var_decl(A, var, initializer);
+   if (!varDecl)
+      return NULL;
+
+   if (var->type.qualifier == SLANG_QUAL_CONST && !initializer) {
+      slang_info_log_error(A->log,
+                           "const-qualified variable '%s' requires initializer",
+                           varName);
+      return NULL;
+   }
+
+   return varDecl;
 }
 
 
@@ -2088,112 +3005,31 @@ _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
    /* If there's a variable associated with this oper (from inlining)
     * use it.  Otherwise, use the oper's var id.
     */
-   slang_atom aVar = oper->var ? oper->var->a_name : oper->a_id;
-   slang_ir_node *n = new_var(A, oper, aVar);
+   slang_atom name = oper->var ? oper->var->a_name : oper->a_id;
+   slang_variable *var = _slang_variable_locate(oper->locals, name, GL_TRUE);
+   slang_ir_node *n = new_var(A, var);
    if (!n) {
-      slang_info_log_error(A->log, "undefined variable '%s'", (char *) aVar);
+      slang_info_log_error(A->log, "undefined variable '%s'", (char *) name);
       return NULL;
    }
    return n;
 }
 
 
-/**
- * Some write-masked assignments are simple, but others are hard.
- * Simple example:
- *    vec3 v;
- *    v.xy = vec2(a, b);
- * Hard example:
- *    vec3 v;
- *    v.zy = vec2(a, b);
- * this gets transformed/swizzled into:
- *    v.zy = vec2(a, b).*yx*         (* = don't care)
- * This function helps to determine simple vs. non-simple.
- */
-static GLboolean
-_slang_simple_writemask(GLuint writemask, GLuint swizzle)
-{
-   switch (writemask) {
-   case WRITEMASK_X:
-      return GET_SWZ(swizzle, 0) == SWIZZLE_X;
-   case WRITEMASK_Y:
-      return GET_SWZ(swizzle, 1) == SWIZZLE_Y;
-   case WRITEMASK_Z:
-      return GET_SWZ(swizzle, 2) == SWIZZLE_Z;
-   case WRITEMASK_W:
-      return GET_SWZ(swizzle, 3) == SWIZZLE_W;
-   case WRITEMASK_XY:
-      return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
-         && (GET_SWZ(swizzle, 1) == SWIZZLE_Y);
-   case WRITEMASK_XYZ:
-      return (GET_SWZ(swizzle, 0) == SWIZZLE_X)
-         && (GET_SWZ(swizzle, 1) == SWIZZLE_Y)
-         && (GET_SWZ(swizzle, 2) == SWIZZLE_Z);
-   case WRITEMASK_XYZW:
-      return swizzle == SWIZZLE_NOOP;
-   default:
-      return GL_FALSE;
-   }
-}
-
 
 /**
- * Convert the given swizzle into a writemask.  In some cases this
- * is trivial, in other cases, we'll need to also swizzle the right
- * hand side to put components in the right places.
- * \param swizzle  the incoming swizzle
- * \param writemaskOut  returns the writemask
- * \param swizzleOut  swizzle to apply to the right-hand-side
- * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
+ * Return the number of components actually named by the swizzle.
+ * Recall that swizzles may have undefined/don't-care values.
  */
-static GLboolean
-swizzle_to_writemask(GLuint swizzle,
-                     GLuint *writemaskOut, GLuint *swizzleOut)
+static GLuint
+swizzle_size(GLuint swizzle)
 {
-   GLuint mask = 0x0, newSwizzle[4];
-   GLint i, size;
-
-   /* make new dst writemask, compute size */
-   for (i = 0; i < 4; i++) {
-      const GLuint swz = GET_SWZ(swizzle, i);
-      if (swz == SWIZZLE_NIL) {
-         /* end */
-         break;
-      }
-      assert(swz >= 0 && swz <= 3);
-      mask |= (1 << swz);
-   }
-   assert(mask <= 0xf);
-   size = i;  /* number of components in mask/swizzle */
-
-   *writemaskOut = mask;
-
-   /* make new src swizzle, by inversion */
+   GLuint size = 0, i;
    for (i = 0; i < 4; i++) {
-      newSwizzle[i] = i; /*identity*/
-   }
-   for (i = 0; i < size; i++) {
-      const GLuint swz = GET_SWZ(swizzle, i);
-      newSwizzle[swz] = i;
-   }
-   *swizzleOut = MAKE_SWIZZLE4(newSwizzle[0],
-                               newSwizzle[1],
-                               newSwizzle[2],
-                               newSwizzle[3]);
-
-   if (_slang_simple_writemask(mask, *swizzleOut)) {
-      if (size >= 1)
-         assert(GET_SWZ(*swizzleOut, 0) == SWIZZLE_X);
-      if (size >= 2)
-         assert(GET_SWZ(*swizzleOut, 1) == SWIZZLE_Y);
-      if (size >= 3)
-         assert(GET_SWZ(*swizzleOut, 2) == SWIZZLE_Z);
-      if (size >= 4)
-         assert(GET_SWZ(*swizzleOut, 3) == SWIZZLE_W);
-      return GL_TRUE;
+      GLuint swz = GET_SWZ(swizzle, i);
+      size += (swz >= 0 && swz <= 3);
    }
-   else
-      return GL_FALSE;
+   return size;
 }
 
 
@@ -2203,13 +3039,34 @@ _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
    slang_ir_node *n = new_node1(IR_SWIZZLE, child);
    assert(child);
    if (n) {
-      n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -1, -1);
+      assert(!n->Store);
+      n->Store = _slang_new_ir_storage_relative(0,
+                                                swizzle_size(swizzle),
+                                                child->Store);
       n->Store->Swizzle = swizzle;
    }
    return n;
 }
 
 
+static GLboolean
+is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
+{
+   while (store->Parent)
+      store = store->Parent;
+
+   if (!(store->File == PROGRAM_OUTPUT ||
+         store->File == PROGRAM_TEMPORARY ||
+         (store->File == PROGRAM_VARYING &&
+          A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
+      return GL_FALSE;
+   }
+   else {
+      return GL_TRUE;
+   }
+}
+
+
 /**
  * Generate IR tree for an assignment (=).
  */
@@ -2219,7 +3076,7 @@ _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
    if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
       /* Check that var is writeable */
       slang_variable *var
-         = _slang_locate_variable(oper->children[0].locals,
+         = _slang_variable_locate(oper->children[0].locals,
                                   oper->children[0].a_id, GL_TRUE);
       if (!var) {
          slang_info_log_error(A->log, "undefined variable '%s'",
@@ -2228,7 +3085,9 @@ _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
       }
       if (var->type.qualifier == SLANG_QUAL_CONST ||
           var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
-          var->type.qualifier == SLANG_QUAL_UNIFORM) {
+          var->type.qualifier == SLANG_QUAL_UNIFORM ||
+          (var->type.qualifier == SLANG_QUAL_VARYING &&
+           A->program->Target == GL_FRAGMENT_PROGRAM_ARB)) {
          slang_info_log_error(A->log,
                               "illegal assignment to read-only variable '%s'",
                               (char *) oper->children[0].a_id);
@@ -2253,32 +3112,45 @@ _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
    }
    else {
       slang_ir_node *n, *lhs, *rhs;
+
+      /* lhs and rhs type checking */
+      if (!_slang_assignment_compatible(A,
+                                        &oper->children[0],
+                                        &oper->children[1])) {
+         slang_info_log_error(A->log, "incompatible types in assignment");
+         return NULL;
+      }
+
       lhs = _slang_gen_operation(A, &oper->children[0]);
+      if (!lhs) {
+         return NULL;
+      }
 
-      if (lhs) {
-         if (lhs->Store->File != PROGRAM_OUTPUT &&
-             lhs->Store->File != PROGRAM_TEMPORARY &&
-             lhs->Store->File != PROGRAM_VARYING &&
-             lhs->Store->File != PROGRAM_UNDEFINED) {
-            slang_info_log_error(A->log,
-                                 "illegal assignment to read-only l-value");
-            return NULL;
-         }
+      if (!lhs->Store) {
+         slang_info_log_error(A->log,
+                              "invalid left hand side for assignment");
+         return NULL;
+      }
+
+      /* check that lhs is writable */
+      if (!is_store_writable(A, lhs->Store)) {
+         slang_info_log_error(A->log,
+                              "illegal assignment to read-only l-value");
+         return NULL;
       }
 
       rhs = _slang_gen_operation(A, &oper->children[1]);
       if (lhs && rhs) {
          /* convert lhs swizzle into writemask */
          GLuint writemask, newSwizzle;
-         if (!swizzle_to_writemask(lhs->Store->Swizzle,
+         if (!swizzle_to_writemask(A, lhs->Store->Swizzle,
                                    &writemask, &newSwizzle)) {
             /* Non-simple writemask, need to swizzle right hand side in
              * order to put components into the right place.
              */
             rhs = _slang_gen_swizzle(rhs, newSwizzle);
          }
-         n = new_node2(IR_MOVE, lhs, rhs);
-         n->Writemask = writemask;
+         n = new_node2(IR_COPY, lhs, rhs);
          return n;
       }
       else {
@@ -2292,13 +3164,13 @@ _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
  * Generate IR tree for referencing a field in a struct (or basic vector type)
  */
 static slang_ir_node *
-_slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
+_slang_gen_struct_field(slang_assemble_ctx * A, slang_operation *oper)
 {
    slang_typeinfo ti;
 
    /* type of struct */
    slang_typeinfo_construct(&ti);
-   _slang_typeof_operation(A, &oper->children[0], &ti);
+   typeof_operation(A, &oper->children[0], &ti);
 
    if (_slang_type_is_vector(ti.spec.type)) {
       /* the field should be a swizzle */
@@ -2308,6 +3180,7 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
       GLuint swizzle;
       if (!_slang_is_swizzle((char *) oper->a_id, rows, &swz)) {
          slang_info_log_error(A->log, "Bad swizzle");
+         return NULL;
       }
       swizzle = MAKE_SWIZZLE4(swz.swizzle[0],
                               swz.swizzle[1],
@@ -2320,7 +3193,9 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
          n = _slang_gen_swizzle(n, swizzle);
       return n;
    }
-   else if (ti.spec.type == SLANG_SPEC_FLOAT) {
+   else if (   ti.spec.type == SLANG_SPEC_FLOAT
+            || ti.spec.type == SLANG_SPEC_INT
+            || ti.spec.type == SLANG_SPEC_BOOL) {
       const GLuint rows = 1;
       slang_swizzle swz;
       slang_ir_node *n;
@@ -2344,19 +3219,24 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
       slang_ir_node *base, *n;
       slang_typeinfo field_ti;
       GLint fieldSize, fieldOffset = -1;
+
       /* type of field */
       slang_typeinfo_construct(&field_ti);
-      _slang_typeof_operation(A, oper, &field_ti);
+      typeof_operation(A, oper, &field_ti);
 
       fieldSize = _slang_sizeof_type_specifier(&field_ti.spec);
       if (fieldSize > 0)
          fieldOffset = _slang_field_offset(&ti.spec, oper->a_id);
 
       if (fieldSize == 0 || fieldOffset < 0) {
+         const char *structName;
+         if (ti.spec._struct)
+            structName = (char *) ti.spec._struct->a_name;
+         else
+            structName = "unknown";
          slang_info_log_error(A->log,
                               "\"%s\" is not a member of struct \"%s\"",
-                              (char *) oper->a_id,
-                              (char *) ti.spec._struct->a_name);
+                              (char *) oper->a_id, structName);
          return NULL;
       }
       assert(fieldSize >= 0);
@@ -2368,20 +3248,17 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
       }
 
       n = new_node1(IR_FIELD, base);
-      if (n) {
-         n->Field = (char *) oper->a_id;
-         n->FieldOffset = fieldOffset;
-         assert(n->FieldOffset >= 0);
-         n->Store = _slang_new_ir_storage(base->Store->File,
-                                          base->Store->Index,
-                                          fieldSize);
-      }
-      return n;
+      if (!n)
+         return NULL;
 
-#if 0
-      _mesa_problem(NULL, "glsl structs/fields not supported yet");
-      return NULL;
-#endif
+      n->Field = (char *) oper->a_id;
+
+      /* Store the field's offset in storage->Index */
+      n->Store = _slang_new_ir_storage(base->Store->File,
+                                       fieldOffset,
+                                       fieldSize);
+
+      return n;
    }
 }
 
@@ -2390,13 +3267,13 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
  * Gen code for array indexing.
  */
 static slang_ir_node *
-_slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
+_slang_gen_array_element(slang_assemble_ctx * A, slang_operation *oper)
 {
    slang_typeinfo array_ti;
 
    /* get array's type info */
    slang_typeinfo_construct(&array_ti);
-   _slang_typeof_operation(A, &oper->children[0], &array_ti);
+   typeof_operation(A, &oper->children[0], &array_ti);
 
    if (_slang_type_is_vector(array_ti.spec.type)) {
       /* indexing a simple vector type: "vec4 v; v[0]=p;" */
@@ -2407,7 +3284,7 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
 
       index = (GLint) oper->children[1].literal[0];
       if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
-          index >= max) {
+          index >= (GLint) max) {
          slang_info_log_error(A->log, "Invalid array index for vector type");
          return NULL;
       }
@@ -2420,9 +3297,8 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
                                         SWIZZLE_NIL,
                                         SWIZZLE_NIL);
          n = _slang_gen_swizzle(n, swizzle);
-         /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
-         n->Writemask = WRITEMASK_X << index;
       }
+      assert(n->Store);
       return n;
    }
    else {
@@ -2433,7 +3309,7 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
 
       /* size of array element */
       slang_typeinfo_construct(&elem_ti);
-      _slang_typeof_operation(A, oper, &elem_ti);
+      typeof_operation(A, oper, &elem_ti);
       elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
 
       if (_slang_type_is_matrix(array_ti.spec.type))
@@ -2454,22 +3330,31 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
       index = _slang_gen_operation(A, &oper->children[1]);
       if (array && index) {
          /* bounds check */
-         if (index->Opcode == IR_FLOAT &&
-             ((int) index->Value[0] < 0 ||
-              (int) index->Value[0] >= arrayLen)) {
-            slang_info_log_error(A->log,
+         GLint constIndex = -1;
+         if (index->Opcode == IR_FLOAT) {
+            constIndex = (int) index->Value[0];
+            if (constIndex < 0 || constIndex >= arrayLen) {
+               slang_info_log_error(A->log,
                                 "Array index out of bounds (index=%d size=%d)",
-                                 (int) index->Value[0], arrayLen);
-            _slang_free_ir_tree(array);
-            _slang_free_ir_tree(index);
+                                 constIndex, arrayLen);
+               _slang_free_ir_tree(array);
+               _slang_free_ir_tree(index);
+               return NULL;
+            }
+         }
+
+         if (!array->Store) {
+            slang_info_log_error(A->log, "Invalid array");
             return NULL;
          }
 
          elem = new_node2(IR_ELEMENT, array, index);
+
+         /* The storage info here will be updated during code emit */
          elem->Store = _slang_new_ir_storage(array->Store->File,
                                              array->Store->Index,
                                              elemSize);
-         /* XXX try to do some array bounds checking here */
+
          return elem;
       }
       else {
@@ -2481,47 +3366,79 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
 }
 
 
-/**
- * Look for expressions such as:  gl_ModelviewMatrix * gl_Vertex
- * and replace with this:  gl_Vertex * gl_ModelviewMatrixTranpose
- * Since matrices are stored in column-major order, the second form of
- * multiplication is much more efficient (just 4 dot products).
- */
+static slang_ir_node *
+_slang_gen_compare(slang_assemble_ctx *A, slang_operation *oper,
+                   slang_ir_opcode opcode)
+{
+   slang_typeinfo t0, t1;
+   slang_ir_node *n;
+   
+   slang_typeinfo_construct(&t0);
+   typeof_operation(A, &oper->children[0], &t0);
+
+   slang_typeinfo_construct(&t1);
+   typeof_operation(A, &oper->children[0], &t1);
+
+   if (t0.spec.type == SLANG_SPEC_ARRAY ||
+       t1.spec.type == SLANG_SPEC_ARRAY) {
+      slang_info_log_error(A->log, "Illegal array comparison");
+      return NULL;
+   }
+
+   if (oper->type != SLANG_OPER_EQUAL &&
+       oper->type != SLANG_OPER_NOTEQUAL) {
+      /* <, <=, >, >= can only be used with scalars */
+      if ((t0.spec.type != SLANG_SPEC_INT &&
+           t0.spec.type != SLANG_SPEC_FLOAT) ||
+          (t1.spec.type != SLANG_SPEC_INT &&
+           t1.spec.type != SLANG_SPEC_FLOAT)) {
+         slang_info_log_error(A->log, "Incompatible type(s) for inequality operator");
+         return NULL;
+      }
+   }
+
+   n =  new_node2(opcode,
+                  _slang_gen_operation(A, &oper->children[0]),
+                  _slang_gen_operation(A, &oper->children[1]));
+
+   /* result is a bool (size 1) */
+   n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, 1);
+
+   return n;
+}
+
+
+#if 0
 static void
-_slang_check_matmul_optimization(slang_assemble_ctx *A, slang_operation *oper)
-{
-   static const struct {
-      const char *orig;
-      const char *tranpose;
-   } matrices[] = {
-      {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
-      {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
-      {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
-      {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
-      {"gl_NormalMatrix", "__NormalMatrixTranspose"},
-      { NULL, NULL }
-   };
+print_vars(slang_variable_scope *s)
+{
+   int i;
+   printf("vars: ");
+   for (i = 0; i < s->num_variables; i++) {
+      printf("%s %d, \n",
+             (char*) s->variables[i]->a_name,
+             s->variables[i]->declared);
+   }
 
-   assert(oper->type == SLANG_OPER_MULTIPLY);
-   if (oper->children[0].type == SLANG_OPER_IDENTIFIER) {
-      GLuint i;
-      for (i = 0; matrices[i].orig; i++) {
-         if (oper->children[0].a_id
-             == slang_atom_pool_atom(A->atoms, matrices[i].orig)) {
-            /*
-            _mesa_printf("Replace %s with %s\n",
-                         matrices[i].orig, matrices[i].tranpose);
-            */
-            assert(oper->children[0].type == SLANG_OPER_IDENTIFIER);
-            oper->children[0].a_id
-               = slang_atom_pool_atom(A->atoms, matrices[i].tranpose);
-            /* finally, swap the operands */
-            _slang_operation_swap(&oper->children[0], &oper->children[1]);
-            return;
-         }
+   printf("\n");
+}
+#endif
+
+
+#if 0
+static void
+_slang_undeclare_vars(slang_variable_scope *locals)
+{
+   if (locals->num_variables > 0) {
+      int i;
+      for (i = 0; i < locals->num_variables; i++) {
+         slang_variable *v = locals->variables[i];
+         printf("undeclare %s at %p\n", (char*) v->a_name, v);
+         v->declared = GL_FALSE;
       }
    }
 }
+#endif
 
 
 /**
@@ -2543,6 +3460,9 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
 
          _slang_pop_var_table(A->vartable);
 
+         /*_slang_undeclare_vars(oper->locals);*/
+         /*print_vars(oper->locals);*/
+
          if (n)
             n = new_node1(IR_SCOPE, n);
          return n;
@@ -2562,29 +3482,9 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
                _slang_free_ir_tree(tree);
                return NULL; /* error must have occured */
             }
-            tree = tree ? new_seq(tree, n) : n;
+            tree = new_seq(tree, n);
          }
 
-#if 00
-         if (oper->locals->num_variables > 0) {
-            int i;
-            /*
-            printf("\n****** Deallocate vars in scope!\n");
-            */
-            for (i = 0; i < oper->locals->num_variables; i++) {
-               slang_variable *v = oper->locals->variables + i;
-               if (v->aux) {
-                  slang_ir_storage *store = (slang_ir_storage *) v->aux;
-                  /*
-                  printf("  Deallocate var %s\n", (char*) v->a_name);
-                  */
-                  assert(store->File == PROGRAM_TEMPORARY);
-                  assert(store->Index >= 0);
-                  _slang_free_temp(A->vartable, store->Index, store->Size);
-               }
-            }
-         }
-#endif
          return tree;
       }
       else {
@@ -2616,29 +3516,17 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
       return new_node0(IR_KILL);
 
    case SLANG_OPER_EQUAL:
-      return new_node2(IR_EQUAL,
-                      _slang_gen_operation(A, &oper->children[0]),
-                      _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_EQUAL);
    case SLANG_OPER_NOTEQUAL:
-      return new_node2(IR_NOTEQUAL,
-                      _slang_gen_operation(A, &oper->children[0]),
-                      _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_NOTEQUAL);
    case SLANG_OPER_GREATER:
-      return new_node2(IR_SGT,
-                      _slang_gen_operation(A, &oper->children[0]),
-                      _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_SGT);
    case SLANG_OPER_LESS:
-      return new_node2(IR_SLT,
-                      _slang_gen_operation(A, &oper->children[0]),
-                      _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_SLT);
    case SLANG_OPER_GREATEREQUAL:
-      return new_node2(IR_SGE,
-                      _slang_gen_operation(A, &oper->children[0]),
-                      _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_SGE);
    case SLANG_OPER_LESSEQUAL:
-      return new_node2(IR_SLE,
-                       _slang_gen_operation(A, &oper->children[0]),
-                       _slang_gen_operation(A, &oper->children[1]));
+      return _slang_gen_compare(A, oper, IR_SLE);
    case SLANG_OPER_ADD:
       {
         slang_ir_node *n;
@@ -2657,7 +3545,6 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
       {
         slang_ir_node *n;
          assert(oper->num_children == 2);
-         _slang_check_matmul_optimization(A, oper);
          n = _slang_gen_function_call_name(A, "*", oper, NULL);
         return n;
       }
@@ -2686,14 +3573,14 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
       {
         slang_ir_node *n;
          assert(oper->num_children == 2);
-        n = _slang_gen_function_call_name(A, "+=", oper, &oper->children[0]);
+        n = _slang_gen_function_call_name(A, "+=", oper, NULL);
         return n;
       }
    case SLANG_OPER_SUBASSIGN:
       {
         slang_ir_node *n;
          assert(oper->num_children == 2);
-        n = _slang_gen_function_call_name(A, "-=", oper, &oper->children[0]);
+        n = _slang_gen_function_call_name(A, "-=", oper, NULL);
         return n;
       }
       break;
@@ -2701,14 +3588,14 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
       {
         slang_ir_node *n;
          assert(oper->num_children == 2);
-        n = _slang_gen_function_call_name(A, "*=", oper, &oper->children[0]);
+        n = _slang_gen_function_call_name(A, "*=", oper, NULL);
         return n;
       }
    case SLANG_OPER_DIVASSIGN:
       {
         slang_ir_node *n;
          assert(oper->num_children == 2);
-        n = _slang_gen_function_call_name(A, "/=", oper, &oper->children[0]);
+        n = _slang_gen_function_call_name(A, "/=", oper, NULL);
         return n;
       }
    case SLANG_OPER_LOGICALAND:
@@ -2742,6 +3629,8 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
    case SLANG_OPER_CALL:
       return _slang_gen_function_call_name(A, (const char *) oper->a_id,
                                            oper, NULL);
+   case SLANG_OPER_METHOD:
+      return _slang_gen_method_call(A, oper);
    case SLANG_OPER_RETURN:
       return _slang_gen_return(A, oper);
    case SLANG_OPER_LABEL:
@@ -2751,9 +3640,9 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
    case SLANG_OPER_IF:
       return _slang_gen_if(A, oper);
    case SLANG_OPER_FIELD:
-      return _slang_gen_field(A, oper);
+      return _slang_gen_struct_field(A, oper);
    case SLANG_OPER_SUBSCRIPT:
-      return _slang_gen_subscript(A, oper);
+      return _slang_gen_array_element(A, oper);
    case SLANG_OPER_LITERAL_FLOAT:
       /* fall-through */
    case SLANG_OPER_LITERAL_INT:
@@ -2790,17 +3679,19 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
         return n;
       }
 
-   case SLANG_OPER_INLINED_CALL:
+   case SLANG_OPER_NON_INLINED_CALL:
    case SLANG_OPER_SEQUENCE:
       {
          slang_ir_node *tree = NULL;
          GLuint i;
          for (i = 0; i < oper->num_children; i++) {
             slang_ir_node *n = _slang_gen_operation(A, &oper->children[i]);
-            tree = tree ? new_seq(tree, n) : n;
+            tree = new_seq(tree, n);
+            if (n)
+               tree->Store = n->Store;
          }
-         if (oper->type == SLANG_OPER_INLINED_CALL) {
-            tree = new_inlined_function_call(tree, oper->label);
+         if (oper->type == SLANG_OPER_NON_INLINED_CALL) {
+            tree = new_function_call(tree, oper->label);
          }
          return tree;
       }
@@ -2820,6 +3711,23 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
 }
 
 
+/**
+ * Compute total size of array give size of element, number of elements.
+ */
+static GLint
+array_size(GLint baseSize, GLint arrayLen)
+{
+   GLint total;
+   if (arrayLen > 1) {
+      /* round up base type to multiple of 4 */
+      total = ((baseSize + 3) & ~0x3) * MAX2(arrayLen, 1);
+   }
+   else {
+      total = baseSize;
+   }
+   return total;
+}
+
 
 /**
  * Called by compiler when a global variable has been parsed/compiled.
@@ -2845,27 +3753,48 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
    int dbg = 0;
    const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
    const GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
+   const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
 
    if (texIndex != -1) {
-      /* Texture sampler:
+      /* This is a texture sampler variable...
        * store->File = PROGRAM_SAMPLER
-       * store->Index = sampler uniform location
+       * store->Index = sampler number (0..7, typically)
        * store->Size = texture type index (1D, 2D, 3D, cube, etc)
        */
-      GLint samplerUniform
-         = _mesa_add_sampler(prog->Parameters, varName, datatype);
-      store = _slang_new_ir_storage(PROGRAM_SAMPLER, samplerUniform, texIndex);
+      if (var->initializer) {
+         slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
+         return GL_FALSE;
+      }
+#if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
+      /* disallow rect samplers */
+      if (var->type.specifier.type == SLANG_SPEC_SAMPLER2DRECT ||
+          var->type.specifier.type == SLANG_SPEC_SAMPLER2DRECTSHADOW) {
+         slang_info_log_error(A->log, "invalid sampler type for '%s'", varName);
+         return GL_FALSE;
+      }
+#endif
+      {
+         GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
+         store = _slang_new_ir_storage(PROGRAM_SAMPLER, sampNum, texIndex);
+      }
       if (dbg) printf("SAMPLER ");
    }
    else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
       /* Uniform variable */
-      const GLint size = _slang_sizeof_type_specifier(&var->type.specifier)
-                         * MAX2(var->array_len, 1);
+      const GLint totalSize = array_size(size, var->array_len);
+      const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
+
       if (prog) {
          /* user-defined uniform */
          if (datatype == GL_NONE) {
             if (var->type.specifier.type == SLANG_SPEC_STRUCT) {
-               _mesa_problem(NULL, "user-declared uniform structs not supported yet");
+               /* temporary work-around */
+               GLenum datatype = GL_FLOAT;
+               GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+                                                    totalSize, datatype, NULL);
+               store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
+                                                 totalSize, swizzle);
+
                /* XXX what we need to do is unroll the struct into its
                 * basic types, creating a uniform variable for each.
                 * For example:
@@ -2879,18 +3808,42 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
                 * "f.a"  (GL_FLOAT_VEC3)
                 * "f.b"  (GL_FLOAT_VEC4)
                 */
+
+               if (var->initializer) {
+                  slang_info_log_error(A->log,
+                     "unsupported initializer for uniform '%s'", varName);
+                  return GL_FALSE;
+               }
             }
             else {
                slang_info_log_error(A->log,
                                     "invalid datatype for uniform variable %s",
-                                    (char *) var->a_name);
+                                    varName);
+               return GL_FALSE;
             }
-            return GL_FALSE;
          }
          else {
-            GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
-                                                 size, datatype);
-            store = _slang_new_ir_storage(PROGRAM_UNIFORM, uniformLoc, size);
+            GLint uniformLoc;
+            const GLfloat *initialValues = NULL;
+            if (var->initializer) {
+               _slang_simplify(var->initializer, &A->space, A->atoms);
+               if (var->initializer->type == SLANG_OPER_LITERAL_FLOAT ||
+                   var->initializer->type == SLANG_OPER_LITERAL_INT) {
+                  /* simple float/vector initializer */
+                  initialValues = var->initializer->literal;
+               }
+               else {
+                  /* complex initializer */
+                  slang_info_log_error(A->log,
+                     "unsupported initializer for uniform '%s'", varName);
+                  return GL_FALSE;
+               }
+            }
+
+            uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+                                           totalSize, datatype, initialValues);
+            store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
+                                              totalSize, swizzle);
          }
       }
       else {
@@ -2898,88 +3851,123 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
          /* We know it's a uniform, but don't allocate storage unless
           * it's really used.
           */
-         store = _slang_new_ir_storage(PROGRAM_STATE_VAR, -1, size);
+         store = _slang_new_ir_storage_swz(PROGRAM_STATE_VAR, -1,
+                                           totalSize, swizzle);
       }
-      if (dbg) printf("UNIFORM (sz %d) ", size);
+      if (dbg) printf("UNIFORM (sz %d) ", totalSize);
    }
    else if (var->type.qualifier == SLANG_QUAL_VARYING) {
-      const GLint size = 4; /* XXX fix */
+      const GLint totalSize = array_size(size, var->array_len);
+
+      /* varyings must be float, vec or mat */
+      if (!_slang_type_is_float_vec_mat(var->type.specifier.type) &&
+          var->type.specifier.type != SLANG_SPEC_ARRAY) {
+         slang_info_log_error(A->log,
+                              "varying '%s' must be float/vector/matrix",
+                              varName);
+         return GL_FALSE;
+      }
+
+      if (var->initializer) {
+         slang_info_log_error(A->log, "illegal initializer for varying '%s'",
+                              varName);
+         return GL_FALSE;
+      }
+
       if (prog) {
          /* user-defined varying */
-         GLint varyingLoc = _mesa_add_varying(prog->Varying, varName, size);
-         store = _slang_new_ir_storage(PROGRAM_VARYING, varyingLoc, size);
+         GLbitfield flags;
+         GLint varyingLoc;
+         GLuint swizzle;
+
+         flags = 0x0;
+         if (var->type.centroid == SLANG_CENTROID)
+            flags |= PROG_PARAM_BIT_CENTROID;
+         if (var->type.variant == SLANG_INVARIANT)
+            flags |= PROG_PARAM_BIT_INVARIANT;
+
+         varyingLoc = _mesa_add_varying(prog->Varying, varName,
+                                        totalSize, flags);
+         swizzle = _slang_var_swizzle(size, 0);
+         store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
+                                           totalSize, swizzle);
       }
       else {
          /* pre-defined varying, like gl_Color or gl_TexCoord */
          if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
+            /* fragment program input */
             GLuint swizzle;
             GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
                                              &swizzle);
             assert(index >= 0);
-            store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
-            store->Swizzle = swizzle;
             assert(index < FRAG_ATTRIB_MAX);
+            store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index,
+                                              size, swizzle);
          }
          else {
+            /* vertex program output */
             GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
+            GLuint swizzle = _slang_var_swizzle(size, 0);
             assert(index >= 0);
-            assert(type == SLANG_UNIT_VERTEX_BUILTIN);
-            store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
             assert(index < VERT_RESULT_MAX);
+            assert(type == SLANG_UNIT_VERTEX_BUILTIN);
+            store = _slang_new_ir_storage_swz(PROGRAM_OUTPUT, index,
+                                              size, swizzle);
          }
          if (dbg) printf("V/F ");
       }
       if (dbg) printf("VARYING ");
    }
    else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
+      GLuint swizzle;
+      GLint index;
+      /* attributes must be float, vec or mat */
+      if (!_slang_type_is_float_vec_mat(var->type.specifier.type)) {
+         slang_info_log_error(A->log,
+                              "attribute '%s' must be float/vector/matrix",
+                              varName);
+         return GL_FALSE;
+      }
+
       if (prog) {
          /* user-defined vertex attribute */
-         const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
          const GLint attr = -1; /* unknown */
-         GLint index = _mesa_add_attribute(prog->Attributes, varName,
-                                           size, attr);
+         swizzle = _slang_var_swizzle(size, 0);
+         index = _mesa_add_attribute(prog->Attributes, varName,
+                                     size, datatype, attr);
          assert(index >= 0);
-         store = _slang_new_ir_storage(PROGRAM_INPUT,
-                                       VERT_ATTRIB_GENERIC0 + index, size);
+         index = VERT_ATTRIB_GENERIC0 + index;
       }
       else {
          /* pre-defined vertex attrib */
-         GLuint swizzle;
-         GLint index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB,
-                                          &swizzle);
-         GLint size = 4; /* XXX? */
+         index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
          assert(index >= 0);
-         store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
-         store->Swizzle = swizzle;
       }
+      store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
       if (dbg) printf("ATTRIB ");
    }
    else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
       GLuint swizzle = SWIZZLE_XYZW; /* silence compiler warning */
       GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB,
                                        &swizzle);
-      GLint size = 4; /* XXX? */
-      store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
-      store->Swizzle = swizzle;
+      store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
       if (dbg) printf("INPUT ");
    }
    else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
       if (type == SLANG_UNIT_VERTEX_BUILTIN) {
          GLint index = _slang_output_index(varName, GL_VERTEX_PROGRAM_ARB);
-         GLint size = 4; /* XXX? */
          store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
       }
       else {
          GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
-         GLint size = 4; /* XXX? */
+         GLint specialSize = 4; /* treat all fragment outputs as float[4] */
          assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
-         store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
+         store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, specialSize);
       }
       if (dbg) printf("OUTPUT ");
    }
    else if (var->type.qualifier == SLANG_QUAL_CONST && !prog) {
       /* pre-defined global constant, like gl_MaxLights */
-      const GLint size = _slang_sizeof_type_specifier(&var->type.specifier);
       store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
       if (dbg) printf("CONST ");
    }
@@ -2988,26 +3976,9 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
       slang_ir_node *n;
 
       /* IR node to declare the variable */
-      n = _slang_gen_var_decl(A, var);
-
-      /* IR code for the var's initializer, if present */
-      if (var->initializer) {
-         slang_ir_node *lhs, *rhs, *init;
-
-         /* Generate IR_MOVE instruction to initialize the variable */
-         lhs = new_node0(IR_VAR);
-         lhs->Var = var;
-         lhs->Store = n->Store;
-
-         /* constant folding, etc */
-         _slang_simplify(var->initializer, &A->space, A->atoms);
-
-         rhs = _slang_gen_operation(A, var->initializer);
-         assert(rhs);
-         init = new_node2(IR_MOVE, lhs, rhs);
-         n = new_seq(n, init);
-      }
+      n = _slang_gen_var_decl(A, var, var->initializer);
 
+      /* emit GPU instructions */
       success = _slang_emit_code(n, A->vartable, A->program, GL_FALSE, A->log);
 
       _slang_free_ir_tree(n);
@@ -3017,7 +3988,9 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
                    store ? store->Index : -2);
 
    if (store)
-      var->aux = store;  /* save var's storage info */
+      var->store = store;  /* save var's storage info */
+
+   var->declared = GL_TRUE;
 
    return success;
 }
@@ -3036,7 +4009,7 @@ _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
 
    if (_mesa_strcmp((char *) fun->header.a_name, "main") != 0) {
       /* we only really generate code for main, all other functions get
-       * inlined.
+       * inlined or codegen'd upon an actual call.
        */
 #if 0
       /* do some basic error checking though */