mesa: fix array storage allocation bug
[mesa.git] / src / mesa / shader / slang / slang_codegen.c
index efd330106bee71519188a95cb69e5db46ef2388c..14fa38c3cdabba8b8dedf5313fd1d2a55545c9f9 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_statevars.h"
 #include "slang_typeinfo.h"
 #include "slang_codegen.h"
 #include "slang_compile.h"
 #include "slang_label.h"
+#include "slang_mem.h"
 #include "slang_simplify.h"
 #include "slang_emit.h"
 #include "slang_vartable.h"
@@ -69,6 +70,8 @@ is_sampler_type(const slang_fully_specified_type *t)
    case SLANG_SPEC_SAMPLERCUBE:
    case SLANG_SPEC_SAMPLER1DSHADOW:
    case SLANG_SPEC_SAMPLER2DSHADOW:
+   case SLANG_SPEC_SAMPLER2DRECT:
+   case SLANG_SPEC_SAMPLER2DRECTSHADOW:
       return GL_TRUE;
    default:
       return GL_FALSE;
@@ -76,69 +79,142 @@ is_sampler_type(const slang_fully_specified_type *t)
 }
 
 
+/**
+ * Return the offset (in floats or ints) of the named field within
+ * the given struct.  Return -1 if field not found.
+ * If field is NULL, return the size of the struct instead.
+ */
+static GLint
+_slang_field_offset(const slang_type_specifier *spec, slang_atom field)
+{
+   GLint offset = 0;
+   GLuint i;
+   for (i = 0; i < spec->_struct->fields->num_variables; i++) {
+      const slang_variable *v = spec->_struct->fields->variables[i];
+      const GLuint sz = _slang_sizeof_type_specifier(&v->type.specifier);
+      if (sz > 1) {
+         /* types larger than 1 float are register (4-float) aligned */
+         offset = (offset + 3) & ~3;
+      }
+      if (field && v->a_name == field) {
+         return offset;
+      }
+      offset += sz;
+   }
+   if (field)
+      return -1; /* field not found */
+   else
+      return offset;  /* struct size */
+}
+
+
+/**
+ * Return the size (in floats) of the given type specifier.
+ * If the size is greater than 4, the size should be a multiple of 4
+ * so that the correct number of 4-float registers are allocated.
+ * For example, a mat3x2 is size 12 because we want to store the
+ * 3 columns in 3 float[4] registers.
+ */
 GLuint
 _slang_sizeof_type_specifier(const slang_type_specifier *spec)
 {
+   GLuint sz;
    switch (spec->type) {
    case SLANG_SPEC_VOID:
-      return 0;
+      sz = 0;
+      break;
    case SLANG_SPEC_BOOL:
-      return 1;
+      sz = 1;
+      break;
    case SLANG_SPEC_BVEC2:
-      return 2;
+      sz = 2;
+      break;
    case SLANG_SPEC_BVEC3:
-      return 3;
+      sz = 3;
+      break;
    case SLANG_SPEC_BVEC4:
-      return 4;
+      sz = 4;
+      break;
    case SLANG_SPEC_INT:
-      return 1;
+      sz = 1;
+      break;
    case SLANG_SPEC_IVEC2:
-      return 2;
+      sz = 2;
+      break;
    case SLANG_SPEC_IVEC3:
-      return 3;
+      sz = 3;
+      break;
    case SLANG_SPEC_IVEC4:
-      return 4;
+      sz = 4;
+      break;
    case SLANG_SPEC_FLOAT:
-      return 1;
+      sz = 1;
+      break;
    case SLANG_SPEC_VEC2:
-      return 2;
+      sz = 2;
+      break;
    case SLANG_SPEC_VEC3:
-      return 3;
+      sz = 3;
+      break;
    case SLANG_SPEC_VEC4:
-      return 4;
+      sz = 4;
+      break;
    case SLANG_SPEC_MAT2:
-      return 2 * 2;
+      sz = 2 * 4; /* 2 columns (regs) */
+      break;
    case SLANG_SPEC_MAT3:
-      return 3 * 3;
+      sz = 3 * 4;
+      break;
    case SLANG_SPEC_MAT4:
-      return 4 * 4;
+      sz = 4 * 4;
+      break;
+   case SLANG_SPEC_MAT23:
+      sz = 2 * 4; /* 2 columns (regs) */
+      break;
+   case SLANG_SPEC_MAT32:
+      sz = 3 * 4; /* 3 columns (regs) */
+      break;
+   case SLANG_SPEC_MAT24:
+      sz = 2 * 4;
+      break;
+   case SLANG_SPEC_MAT42:
+      sz = 4 * 4; /* 4 columns (regs) */
+      break;
+   case SLANG_SPEC_MAT34:
+      sz = 3 * 4;
+      break;
+   case SLANG_SPEC_MAT43:
+      sz = 4 * 4; /* 4 columns (regs) */
+      break;
    case SLANG_SPEC_SAMPLER1D:
    case SLANG_SPEC_SAMPLER2D:
    case SLANG_SPEC_SAMPLER3D:
    case SLANG_SPEC_SAMPLERCUBE:
    case SLANG_SPEC_SAMPLER1DSHADOW:
    case SLANG_SPEC_SAMPLER2DSHADOW:
-      return 1; /* special case */
+   case SLANG_SPEC_SAMPLER2DRECT:
+   case SLANG_SPEC_SAMPLER2DRECTSHADOW:
+      sz = 1; /* a sampler is basically just an integer index */
+      break;
    case SLANG_SPEC_STRUCT:
-      {
-         GLuint sum = 0, i;
-         for (i = 0; i < spec->_struct->fields->num_variables; i++) {
-            slang_variable *v = spec->_struct->fields->variables[i];
-            GLuint sz = _slang_sizeof_type_specifier(&v->type.specifier);
-            /* XXX verify padding */
-            if (sz < 4)
-               sz = 4;
-            sum += sz;
-         }
-         return sum;
+      sz = _slang_field_offset(spec, 0); /* special use */
+      if (sz > 4) {
+         sz = (sz + 3) & ~0x3; /* round up to multiple of four */
       }
+      break;
    case SLANG_SPEC_ARRAY:
-      return _slang_sizeof_type_specifier(spec->_array);
+      sz = _slang_sizeof_type_specifier(spec->_array);
+      break;
    default:
       _mesa_problem(NULL, "Unexpected type in _slang_sizeof_type_specifier()");
-      return 0;
+      sz = 0;
    }
-   return 0;
+
+   if (sz > 4) {
+      /* if size is > 4, it should be a multiple of four */
+      assert((sz & 0x3) == 0);
+   }
+   return sz;
 }
 
 
@@ -196,12 +272,18 @@ sampler_to_texture_index(const slang_type_specifier_type type)
       return TEXTURE_1D_INDEX; /* XXX fix */
    case SLANG_SPEC_SAMPLER2DSHADOW:
       return TEXTURE_2D_INDEX; /* XXX fix */
+   case SLANG_SPEC_SAMPLER2DRECT:
+      return TEXTURE_RECT_INDEX;
+   case SLANG_SPEC_SAMPLER2DRECTSHADOW:
+      return TEXTURE_RECT_INDEX; /* XXX fix */
    default:
       return -1;
    }
 }
 
 
+#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
@@ -209,35 +291,39 @@ sampler_to_texture_index(const slang_type_specifier_type type)
  * XXX return size too
  */
 static GLint
-_slang_input_index(const char *name, GLenum target)
+_slang_input_index(const char *name, GLenum target, GLuint *swizzleOut)
 {
    struct input_info {
       const char *Name;
       GLuint Attrib;
+      GLuint Swizzle;
    };
    static const struct input_info vertInputs[] = {
-      { "gl_Vertex", VERT_ATTRIB_POS },
-      { "gl_Normal", VERT_ATTRIB_NORMAL },
-      { "gl_Color", VERT_ATTRIB_COLOR0 },
-      { "gl_SecondaryColor", VERT_ATTRIB_COLOR1 },
-      { "gl_FogCoord", VERT_ATTRIB_FOG },
-      { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0 },
-      { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1 },
-      { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2 },
-      { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3 },
-      { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4 },
-      { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5 },
-      { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6 },
-      { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7 },
-      { NULL, 0 }
+      { "gl_Vertex", VERT_ATTRIB_POS, SWIZZLE_NOOP },
+      { "gl_Normal", VERT_ATTRIB_NORMAL, SWIZZLE_NOOP },
+      { "gl_Color", VERT_ATTRIB_COLOR0, SWIZZLE_NOOP },
+      { "gl_SecondaryColor", VERT_ATTRIB_COLOR1, SWIZZLE_NOOP },
+      { "gl_FogCoord", VERT_ATTRIB_FOG, SWIZZLE_XXXX },
+      { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6, SWIZZLE_NOOP },
+      { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7, SWIZZLE_NOOP },
+      { NULL, 0, SWIZZLE_NOOP }
    };
    static const struct input_info fragInputs[] = {
-      { "gl_FragCoord", FRAG_ATTRIB_WPOS },
-      { "gl_Color", FRAG_ATTRIB_COL0 },
-      { "gl_SecondaryColor", FRAG_ATTRIB_COL1 },
-      { "gl_FogFragCoord", FRAG_ATTRIB_FOGC },
-      { "gl_TexCoord", FRAG_ATTRIB_TEX0 },
-      { NULL, 0 }
+      { "gl_FragCoord", FRAG_ATTRIB_WPOS, SWIZZLE_NOOP },
+      { "gl_Color", FRAG_ATTRIB_COL0, SWIZZLE_NOOP },
+      { "gl_SecondaryColor", FRAG_ATTRIB_COL1, SWIZZLE_NOOP },
+      { "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;
    const struct input_info *inputs
@@ -248,6 +334,7 @@ _slang_input_index(const char *name, GLenum target)
    for (i = 0; inputs[i].Name; i++) {
       if (strcmp(inputs[i].Name, name) == 0) {
          /* found */
+         *swizzleOut = inputs[i].Swizzle;
          return inputs[i].Attrib;
       }
    }
@@ -273,7 +360,7 @@ _slang_output_index(const char *name, GLenum target)
       { "gl_BackColor", VERT_RESULT_BFC0 },
       { "gl_FrontSecondaryColor", VERT_RESULT_COL1 },
       { "gl_BackSecondaryColor", VERT_RESULT_BFC1 },
-      { "gl_TexCoord", VERT_RESULT_TEX0 }, /* XXX indexed */
+      { "gl_TexCoord", VERT_RESULT_TEX0 },
       { "gl_FogFragCoord", VERT_RESULT_FOGC },
       { "gl_PointSize", VERT_RESULT_PSIZ },
       { NULL, 0 }
@@ -281,6 +368,7 @@ _slang_output_index(const char *name, GLenum target)
    static const struct output_info fragOutputs[] = {
       { "gl_FragColor", FRAG_RESULT_COLR },
       { "gl_FragDepth", FRAG_RESULT_DEPR },
+      { "gl_FragData", FRAG_RESULT_DATA0 },
       { NULL, 0 }
    };
    GLuint i;
@@ -324,9 +412,12 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_min", IR_MIN, 1, 2 },
    { "vec4_max", IR_MAX, 1, 2 },
    { "vec4_clamp", IR_CLAMP, 1, 3 },
-   { "vec4_seq", IR_SEQ, 1, 2 },
+   { "vec4_seq", IR_SEQUAL, 1, 2 },
+   { "vec4_sne", IR_SNEQUAL, 1, 2 },
    { "vec4_sge", IR_SGE, 1, 2 },
    { "vec4_sgt", IR_SGT, 1, 2 },
+   { "vec4_sle", IR_SLE, 1, 2 },
+   { "vec4_slt", IR_SLT, 1, 2 },
    /* vec4 unary */
    { "vec4_floor", IR_FLOOR, 1, 1 },
    { "vec4_frac", IR_FRAC, 1, 1 },
@@ -335,9 +426,6 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_ddx", IR_DDX, 1, 1 },
    { "vec4_ddy", IR_DDY, 1, 1 },
    /* float binary op */
-   { "float_add", IR_ADD, 1, 2 },
-   { "float_multiply", IR_MUL, 1, 2 },
-   { "float_divide", IR_DIV, 1, 2 },
    { "float_power", IR_POW, 1, 2 },
    /* texture / sampler */
    { "vec4_tex1d", IR_TEX, 1, 2 },
@@ -350,6 +438,8 @@ static slang_asm_info AsmInfo[] = {
    { "vec4_texb3d", IR_TEXB, 1, 2 },  /* 3d w/ bias */
    { "vec4_texp3d", IR_TEXP, 1, 2 },  /* 3d w/ projection */
    { "vec4_texcube", IR_TEX, 1, 2 },  /* cubemap */
+   { "vec4_tex_rect", IR_TEX, 1, 2 }, /* rectangle */
+   { "vec4_texp_rect", IR_TEX, 1, 2 },/* rectangle w/ projection */
 
    /* unary op */
    { "int_to_float", IR_I_TO_F, 1, 1 },
@@ -370,29 +460,11 @@ static slang_asm_info AsmInfo[] = {
 };
 
 
-/**
- * Recursively free an IR tree.
- */
-static void
-_slang_free_ir_tree(slang_ir_node *n)
-{
-#if 1
-   GLuint i;
-   if (!n)
-      return;
-   for (i = 0; i < 3; i++)
-      _slang_free_ir_tree(n->Children[i]);
-   /* Do not free n->BranchNode since it's a child elsewhere */
-   free(n);
-#endif
-}
-
-
 static slang_ir_node *
 new_node3(slang_ir_opcode op,
           slang_ir_node *c0, slang_ir_node *c1, slang_ir_node *c2)
 {
-   slang_ir_node *n = (slang_ir_node *) calloc(1, sizeof(slang_ir_node));
+   slang_ir_node *n = (slang_ir_node *) _slang_alloc(sizeof(slang_ir_node));
    if (n) {
       n->Opcode = op;
       n->Children[0] = c0;
@@ -423,6 +495,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)
 {
@@ -437,45 +512,53 @@ static slang_ir_node *
 new_label(slang_label *label)
 {
    slang_ir_node *n = new_node0(IR_LABEL);
+   assert(label);
    if (n)
       n->Label = label;
    return n;
 }
 
 static slang_ir_node *
-new_float_literal(const float v[4])
+new_float_literal(const float v[4], GLuint size)
 {
-   const GLuint size = (v[0] == v[1] && v[0] == v[2] && v[0] == v[3]) ? 1 : 4;
    slang_ir_node *n = new_node0(IR_FLOAT);
+   assert(size <= 4);
    COPY_4V(n->Value, v);
    /* allocate a storage object, but compute actual location (Index) later */
    n->Store = _slang_new_ir_storage(PROGRAM_CONSTANT, -1, size);
    return n;
 }
 
+
+static slang_ir_node *
+new_not(slang_ir_node *n)
+{
+   return new_node1(IR_NOT, n);
+}
+
+
 /**
- * Conditional jump.
- * \param zeroOrOne indicates if the jump is to be taken on zero, or non-zero
- *                  condition code state.
- * XXX maybe pass an IR node as second param to indicate the jump target???
+ * Non-inlined function call.
  */
 static slang_ir_node *
-new_cjump(slang_label *dest, GLuint zeroOrOne)
+new_function_call(slang_ir_node *code, slang_label *name)
 {
-   slang_ir_node *n = new_node0(zeroOrOne ? IR_CJUMP1 : IR_CJUMP0);
+   slang_ir_node *n = new_node1(IR_CALL, code);
+   assert(name);
    if (n)
-      n->Label = dest;
+      n->Label = name;
    return n;
 }
 
+
 /**
  * Unconditional jump.
- * XXX maybe pass an IR node as second param to indicate the jump target???
  */
 static slang_ir_node *
-new_jump(slang_label *dest)
+new_return(slang_label *dest)
 {
-   slang_ir_node *n = new_node0(IR_JUMP);
+   slang_ir_node *n = new_node0(IR_RETURN);
+   assert(dest);
    if (n)
       n->Label = dest;
    return n;
@@ -497,61 +580,46 @@ new_break(slang_ir_node *loopNode)
    assert(loopNode->Opcode == IR_LOOP);
    if (n) {
       /* insert this node at head of linked list */
-      n->BranchNode = loopNode->BranchNode;
-      loopNode->BranchNode = n;
+      n->List = loopNode->List;
+      loopNode->List = n;
    }
    return n;
 }
 
 
 /**
- * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
+ * Make new IR_BREAK_IF_TRUE.
  */
 static slang_ir_node *
-new_break_if(slang_ir_node *loopNode, slang_ir_node *cond, GLboolean breakTrue)
+new_break_if_true(slang_ir_node *loopNode, slang_ir_node *cond)
 {
    slang_ir_node *n;
    assert(loopNode);
    assert(loopNode->Opcode == IR_LOOP);
-   n = new_node1(breakTrue ? IR_BREAK_IF_TRUE : IR_BREAK_IF_FALSE, cond);
+   n = new_node1(IR_BREAK_IF_TRUE, cond);
    if (n) {
       /* insert this node at head of linked list */
-      n->BranchNode = loopNode->BranchNode;
-      loopNode->BranchNode = n;
+      n->List = loopNode->List;
+      loopNode->List = n;
    }
    return n;
 }
 
 
 /**
- * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
+ * Make new IR_CONT_IF_TRUE node.
  */
 static slang_ir_node *
-new_cont_if(slang_ir_node *loopNode, slang_ir_node *cond, GLboolean contTrue)
+new_cont_if_true(slang_ir_node *loopNode, slang_ir_node *cond)
 {
    slang_ir_node *n;
    assert(loopNode);
    assert(loopNode->Opcode == IR_LOOP);
-   n = new_node1(contTrue ? IR_CONT_IF_TRUE : IR_CONT_IF_FALSE, cond);
+   n = new_node1(IR_CONT_IF_TRUE, cond);
    if (n) {
       /* insert this node at head of linked list */
-      n->BranchNode = loopNode->BranchNode;
-      loopNode->BranchNode = n;
-   }
-   return n;
-}
-
-
-static slang_ir_node *
-new_cont(slang_ir_node *loopNode)
-{
-   slang_ir_node *n = new_node0(IR_CONT);
-   assert(loopNode);
-   assert(loopNode->Opcode == IR_LOOP);
-   if (n) {
-      /* insert this node at head of linked list */
-      n->BranchNode = loopNode->BranchNode;
-      loopNode->BranchNode = n;
+      n->List = loopNode->List;
+      loopNode->List = n;
    }
    return n;
 }
@@ -622,37 +690,78 @@ _slang_is_noop(const slang_operation *oper)
 
 
 /**
- * Produce inline code for a call to an assembly instruction.
+ * Recursively search tree for a node of the given type.
  */
 static slang_operation *
-slang_inline_asm_function(slang_assemble_ctx *A,
-                          slang_function *fun, slang_operation *oper)
+_slang_find_node_type(slang_operation *oper, slang_operation_type type)
 {
-   const GLuint numArgs = oper->num_children;
-   const slang_operation *args = oper->children;
    GLuint i;
-   slang_operation *inlined = slang_operation_new(1);
+   if (oper->type == type)
+      return oper;
+   for (i = 0; i < oper->num_children; i++) {
+      slang_operation *p = _slang_find_node_type(&oper->children[i], type);
+      if (p)
+         return p;
+   }
+   return NULL;
+}
 
-   /*assert(oper->type == SLANG_OPER_CALL);  or vec4_add, etc */
-   /*
-   printf("Inline asm %s\n", (char*) fun->header.a_name);
-   */
-   inlined->type = fun->body->children[0].type;
-   inlined->a_id = fun->body->children[0].a_id;
-   inlined->num_children = numArgs;
-   inlined->children = slang_operation_new(numArgs);
-#if 0
-   inlined->locals = slang_variable_scope_copy(oper->locals);
-#else
-   assert(inlined->locals);
-   inlined->locals->outer_scope = oper->locals->outer_scope;
-#endif
 
-   for (i = 0; i < numArgs; i++) {
-      slang_operation_copy(inlined->children + i, args + i);
+/**
+ * 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;
+}
 
-   return inlined;
+
+/**
+ * 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;
 }
 
 
@@ -700,11 +809,8 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
         GLuint i;
          v = _slang_locate_variable(oper->locals, id, GL_TRUE);
         if (!v) {
-           printf("var %s not found!\n", (char *) oper->a_id);
-            _slang_print_var_scope(oper->locals, 6);
-
-            abort();
-           break;
+            _mesa_problem(NULL, "var %s not found!\n", (char *) oper->a_id);
+            return;
         }
 
         /* look for a substitution */
@@ -735,7 +841,7 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
    case SLANG_OPER_RETURN:
       /* do return replacement here too */
       assert(oper->num_children == 0 || oper->num_children == 1);
-      if (!_slang_is_noop(oper)) {
+      if (oper->num_children == 1 && !_slang_is_noop(&oper->children[0])) {
          /* replace:
           *   return expr;
           * with:
@@ -744,24 +850,34 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
           * then do substitutions on the assignment.
           */
          slang_operation *blockOper, *assignOper, *returnOper;
+
+         /* check if function actually has a return type */
+         assert(A->CurFunction);
+         if (A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
+            slang_info_log_error(A->log, "illegal return expression");
+            return;
+         }
+
          blockOper = slang_operation_new(1);
          blockOper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
          blockOper->num_children = 2;
+         blockOper->locals->outer_scope = oper->locals->outer_scope;
          blockOper->children = slang_operation_new(2);
          assignOper = blockOper->children + 0;
          returnOper = blockOper->children + 1;
 
          assignOper->type = SLANG_OPER_ASSIGN;
          assignOper->num_children = 2;
+         assignOper->locals->outer_scope = blockOper->locals;
          assignOper->children = slang_operation_new(2);
          assignOper->children[0].type = SLANG_OPER_IDENTIFIER;
          assignOper->children[0].a_id = slang_atom_pool_atom(A->atoms, "__retVal");
-         assignOper->children[0].locals->outer_scope = oper->locals;
-         assignOper->locals = oper->locals;
+         assignOper->children[0].locals->outer_scope = assignOper->locals;
+
          slang_operation_copy(&assignOper->children[1],
                               &oper->children[0]);
 
-         returnOper->type = SLANG_OPER_RETURN;
+         returnOper->type = SLANG_OPER_RETURN; /* return w/ no value */
          assert(returnOper->num_children == 0);
 
          /* do substitutions on the "__retVal = expr" sub-tree */
@@ -772,6 +888,14 @@ slang_substitute(slang_assemble_ctx *A, slang_operation *oper,
          slang_operation_copy(oper, blockOper);
          slang_operation_destruct(blockOper);
       }
+      else {
+         /* check if return value was expected */
+         assert(A->CurFunction);
+         if (A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
+            slang_info_log_error(A->log, "return statement requires an expression");
+            return;
+         }
+      }
       break;
 
    case SLANG_OPER_ASSIGN:
@@ -801,6 +925,70 @@ 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:
+ *
+ * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
+ * {
+ *    __asm vec4_lrp __retVal, a, y, x;
+ * }
+ *
+ * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
+ */
+static slang_operation *
+slang_inline_asm_function(slang_assemble_ctx *A,
+                          slang_function *fun, slang_operation *oper)
+{
+   const GLuint numArgs = oper->num_children;
+   GLuint i;
+   slang_operation *inlined;
+   const GLboolean haveRetValue = _slang_function_has_return_value(fun);
+   slang_variable **substOld;
+   slang_operation **substNew;
+
+   ASSERT(slang_is_asm_function(fun));
+   ASSERT(fun->param_count == numArgs + haveRetValue);
+
+   /*
+   printf("Inline %s as %s\n",
+          (char*) fun->header.a_name,
+          (char*) fun->body->children[0].a_id);
+   */
+
+   /*
+    * We'll substitute formal params with actual args in the asm call.
+    */
+   substOld = (slang_variable **)
+      _slang_alloc(numArgs * sizeof(slang_variable *));
+   substNew = (slang_operation **)
+      _slang_alloc(numArgs * sizeof(slang_operation *));
+   for (i = 0; i < numArgs; i++) {
+      substOld[i] = fun->parameters->variables[i];
+      substNew[i] = oper->children + i;
+   }
+
+   /* make a copy of the code to inline */
+   inlined = slang_operation_new(1);
+   slang_operation_copy(inlined, &fun->body->children[0]);
+   if (haveRetValue) {
+      /* get rid of the __retVal child */
+      for (i = 0; i < numArgs; i++) {
+         inlined->children[i] = inlined->children[i + 1];
+      }
+      inlined->num_children--;
+   }
+
+   /* now do formal->actual substitutions */
+   slang_substitute(A, inlined, numArgs, substOld, substNew, GL_FALSE);
+
+   _slang_free(substOld);
+   _slang_free(substNew);
+
+   return inlined;
+}
+
+
 /**
  * Inline the given function call operation.
  * Return a new slang_operation that corresponds to the inlined code.
@@ -823,17 +1011,22 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
    slang_variable **substOld;
    slang_operation **substNew;
    GLuint substCount, numCopyIn, i;
+   slang_function *prevFunction;
+
+   /* save / push */
+   prevFunction = A->CurFunction;
+   A->CurFunction = fun;
 
    /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
    assert(fun->param_count == totalArgs);
 
    /* allocate temporary arrays */
    paramMode = (ParamMode *)
-      _mesa_calloc(totalArgs * sizeof(ParamMode));
+      _slang_alloc(totalArgs * sizeof(ParamMode));
    substOld = (slang_variable **)
-      _mesa_calloc(totalArgs * sizeof(slang_variable *));
+      _slang_alloc(totalArgs * sizeof(slang_variable *));
    substNew = (slang_operation **)
-      _mesa_calloc(totalArgs * sizeof(slang_operation *));
+      _slang_alloc(totalArgs * sizeof(slang_operation *));
 
 #if 0
    printf("Inline call to %s  (total vars=%d  nparams=%d)\n",
@@ -872,11 +1065,10 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
       declOper = &commaSeq->children[0];
       declOper->type = SLANG_OPER_VARIABLE_DECL;
       declOper->a_id = resultVar->a_name;
-      declOper->locals->outer_scope = commaSeq->locals; /*** ??? **/
+      declOper->locals->outer_scope = commaSeq->locals;
 
       /* child[1] = function body */
       inlined = &commaSeq->children[1];
-      /* XXXX this may be inappropriate!!!!: */
       inlined->locals->outer_scope = commaSeq->locals;
 
       /* child[2] = __resultTmp reference */
@@ -884,7 +1076,6 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
       returnOper->type = SLANG_OPER_IDENTIFIER;
       returnOper->a_id = resultVar->a_name;
       returnOper->locals->outer_scope = commaSeq->locals;
-      declOper->locals->outer_scope = commaSeq->locals;
 
       top = commaSeq;
    }
@@ -957,7 +1148,7 @@ 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;
 
 #if 0
@@ -993,7 +1184,7 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
          */
         decl->type = SLANG_OPER_VARIABLE_DECL;
          assert(decl->locals);
-        decl->locals = fun->parameters;
+         decl->locals->outer_scope = inlined->locals;
         decl->a_id = p->a_name;
         decl->num_children = 1;
         decl->children = slang_operation_new(1);
@@ -1014,7 +1205,7 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
                                                     &inlined->children,
                                                     inlined->num_children);
       lab->type = SLANG_OPER_LABEL;
-      lab->label = A->CurFunction->end_label;
+      lab->label = A->curFuncEndLabel;
    }
 
    for (i = 0; i < totalArgs; i++) {
@@ -1027,19 +1218,18 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
                                                       inlined->num_children);
         ass->type = SLANG_OPER_ASSIGN;
         ass->num_children = 2;
-        ass->locals = _slang_variable_scope_new(inlined->locals);
-        assert(ass->locals);
+         ass->locals->outer_scope = inlined->locals;
         ass->children = slang_operation_new(2);
         ass->children[0] = args[i]; /*XXX copy */
         ass->children[1].type = SLANG_OPER_IDENTIFIER;
         ass->children[1].a_id = p->a_name;
-        ass->children[1].locals = _slang_variable_scope_new(ass->locals);
+         ass->children[1].locals->outer_scope = ass->locals;
       }
    }
 
-   _mesa_free(paramMode);
-   _mesa_free(substOld);
-   _mesa_free(substNew);
+   _slang_free(paramMode);
+   _slang_free(substOld);
+   _slang_free(substNew);
 
 #if 0
    printf("Done Inline call to %s  (total vars=%d  nparams=%d)\n",
@@ -1047,6 +1237,10 @@ slang_inline_function_call(slang_assemble_ctx * A, slang_function *fun,
          fun->parameters->num_variables, numArgs);
    slang_print_tree(top, 0);
 #endif
+
+   /* pop */
+   A->CurFunction = prevFunction;
+
    return top;
 }
 
@@ -1057,16 +1251,13 @@ _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
 {
    slang_ir_node *n;
    slang_operation *inlined;
-   slang_function *prevFunc;
-
-   prevFunc = A->CurFunction;
-   A->CurFunction = fun;
+   slang_label *prevFuncEndLabel;
+   char name[200];
 
-   if (!A->CurFunction->end_label) {
-      char name[200];
-      sprintf(name, "__endOfFunc_%s_", (char *) A->CurFunction->header.a_name);
-      A->CurFunction->end_label = _slang_label_new(name);
-   }
+   prevFuncEndLabel = A->curFuncEndLabel;
+   sprintf(name, "__endOfFunc_%s_", (char *) fun->header.a_name);
+   A->curFuncEndLabel = _slang_label_new(name);
+   assert(A->curFuncEndLabel);
 
    if (slang_is_asm_function(fun) && !dest) {
       /* assemble assembly function - tree style */
@@ -1074,17 +1265,65 @@ _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)
+         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);
+         }
+      }
    }
 
-   /* Replace the function call with the inlined block */
-#if 0
-   slang_operation_construct(oper);
-   slang_operation_copy(oper, inlined);
-#else
-   *oper = *inlined;
-#endif
+   if (!inlined)
+      return NULL;
 
+   /* Replace the function call with the inlined block (or new CALL stmt) */
+   slang_operation_destruct(oper);
+   *oper = *inlined;
+   _slang_free(inlined);
 
 #if 0
    assert(inlined->locals);
@@ -1096,9 +1335,8 @@ _slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
 
    n = _slang_gen_operation(A, oper);
 
-   A->CurFunction->end_label = NULL; /* XXX delete/free? */
-
-   A->CurFunction = prevFunc;
+   /*_slang_label_delete(A->curFuncEndLabel);*/
+   A->curFuncEndLabel = prevFuncEndLabel;
 
    return n;
 }
@@ -1117,36 +1355,187 @@ slang_find_asm_info(const char *name)
 }
 
 
+/**
+ * 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
+ */
+static GLboolean
+swizzle_to_writemask(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);
+      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:
-         abort();
+         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;
+   }
+}
+
+
+/**
+ * As above, but produce a writemask.
+ */
+static GLuint
+resolve_writemask(const slang_operation *oper)
+{
+   GLuint swizzle = resolve_swizzle(oper);
+   GLuint writemask, swizzleOut;
+   swizzle_to_writemask(swizzle, &writemask, &swizzleOut);
+   return writemask;
+}
+
+
+/**
+ * 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;
@@ -1186,6 +1575,8 @@ _slang_gen_asm(slang_assemble_ctx *A, slang_operation *oper,
    kids[0] = kids[1] = kids[2] = NULL;
    for (j = 0; j < info->NumParams; j++) {
       kids[j] = _slang_gen_operation(A, &oper->children[firstOperand + j]);
+      if (!kids[j])
+         return NULL;
    }
 
    n = new_node3(info->Opcode, kids[0], kids[1], kids[2]);
@@ -1199,20 +1590,20 @@ _slang_gen_asm(slang_assemble_ctx *A, slang_operation *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];
-      }
+
+      writemask = resolve_writemask(dest_oper);
 
       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->Store = get_store(n0);
       n->Writemask = writemask;
 
-      free(n0);
+      assert(n->Store->File != PROGRAM_UNDEFINED);
+
+      _slang_free(n0);
    }
 
    return n;
@@ -1285,8 +1676,8 @@ _slang_gen_function_call_name(slang_assemble_ctx *A, const char *name,
        * Try adapting the parameters.
        */
       fun = _slang_first_function(A->space.funcs, name);
-      if (!_slang_adapt_call(oper, fun, &A->space, A->atoms, A->log)) {
-         slang_info_log_error(A->log, "Undefined function '%s'", 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;
       }
       assert(fun);
@@ -1316,6 +1707,22 @@ _slang_is_constant_cond(const slang_operation *oper, GLboolean *value)
 }
 
 
+/**
+ * Test if an operation is a scalar or boolean.
+ */
+static GLboolean
+_slang_is_scalar_or_boolean(slang_assemble_ctx *A, slang_operation *oper)
+{
+   slang_typeinfo type;
+   GLint size;
+
+   slang_typeinfo_construct(&type);
+   _slang_typeof_operation(A, oper, &type);
+   size = _slang_sizeof_type_specifier(&type.spec);
+   slang_typeinfo_destruct(&type);
+   return size == 1;
+}
+
 
 /**
  * Generate loop code using high-level IR_LOOP instruction
@@ -1328,9 +1735,15 @@ _slang_gen_while(slang_assemble_ctx * A, const slang_operation *oper)
     *    BREAK if !expr (child[0])
     *    body code (child[1])
     */
-   slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body;
+   slang_ir_node *prevLoop, *loop, *breakIf, *body;
    GLboolean isConst, constTrue;
 
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'while'");
+      return NULL;
+   }
+
    /* Check if loop condition is a constant */
    isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
 
@@ -1345,19 +1758,21 @@ _slang_gen_while(slang_assemble_ctx * A, const slang_operation *oper)
    prevLoop = A->CurLoop;
    A->CurLoop = loop;
 
-   cond = new_cond(_slang_gen_operation(A, &oper->children[0]));
    if (isConst && constTrue) {
       /* while(nonzero constant), no conditional break */
       breakIf = NULL;
    }
    else {
-      breakIf = new_break_if(A->CurLoop, cond, GL_FALSE);
+      slang_ir_node *cond
+         = new_cond(new_not(_slang_gen_operation(A, &oper->children[0])));
+      breakIf = new_break_if_true(A->CurLoop, cond);
    }
    body = _slang_gen_operation(A, &oper->children[1]);
    loop->Children[0] = new_seq(breakIf, body);
 
    /* Do infinite loop detection */
-   if (loop->BranchNode == 0 && isConst && constTrue) {
+   /* loop->List is head of linked list of break/continue nodes */
+   if (!loop->List && isConst && constTrue) {
       /* infinite loop detected */
       A->CurLoop = prevLoop; /* clean-up */
       slang_info_log_error(A->log, "Infinite loop detected!");
@@ -1380,13 +1795,17 @@ _slang_gen_do(slang_assemble_ctx * A, const slang_operation *oper)
    /*
     * LOOP:
     *    body code (child[0])
-    *    BREAK if !expr (child[1])
+    *    tail code:
+    *       BREAK if !expr (child[1])
     */
-   slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body;
+   slang_ir_node *prevLoop, *loop;
    GLboolean isConst, constTrue;
 
-   /* Check if loop condition is a constant */
-   isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[1])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'do/while'");
+      return NULL;
+   }
 
    loop = new_loop(NULL);
 
@@ -1394,16 +1813,22 @@ _slang_gen_do(slang_assemble_ctx * A, const slang_operation *oper)
    prevLoop = A->CurLoop;
    A->CurLoop = loop;
 
-   body = _slang_gen_operation(A, &oper->children[0]);
-   cond = new_cond(_slang_gen_operation(A, &oper->children[1]));
+   /* loop body: */
+   loop->Children[0] = _slang_gen_operation(A, &oper->children[0]);
+
+   /* Check if loop condition is a constant */
+   isConst = _slang_is_constant_cond(&oper->children[1], &constTrue);
    if (isConst && constTrue) {
-      /* while(nonzero constant), no conditional break */
-      breakIf = NULL;
+      /* do { } while(1)   ==> no conditional break */
+      loop->Children[1] = NULL; /* no tail code */
    }
    else {
-      breakIf = new_break_if(A->CurLoop, cond, GL_FALSE);
+      slang_ir_node *cond
+         = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
+      loop->Children[1] = new_break_if_true(A->CurLoop, cond);
    }
-   loop->Children[0] = new_seq(body, breakIf);
+
+   /* XXX we should do infinite loop detection, as above */
 
    /* pop loop, restore prev */
    A->CurLoop = prevLoop;
@@ -1419,11 +1844,12 @@ static slang_ir_node *
 _slang_gen_for(slang_assemble_ctx * A, const slang_operation *oper)
 {
    /*
-    * init (child[0])
+    * init code (child[0])
     * LOOP:
     *    BREAK if !expr (child[1])
     *    body code (child[3])
-    *    incr code (child[2])   // XXX continue here
+    *    tail code:
+    *       incr code (child[2])   // XXX continue here
     */
    slang_ir_node *prevLoop, *loop, *cond, *breakIf, *body, *init, *incr;
 
@@ -1434,12 +1860,13 @@ _slang_gen_for(slang_assemble_ctx * A, const slang_operation *oper)
    prevLoop = A->CurLoop;
    A->CurLoop = loop;
 
-   cond = new_cond(_slang_gen_operation(A, &oper->children[1]));
-   breakIf = new_break_if(A->CurLoop, cond, GL_FALSE);
+   cond = new_cond(new_not(_slang_gen_operation(A, &oper->children[1])));
+   breakIf = new_break_if_true(A->CurLoop, cond);
    body = _slang_gen_operation(A, &oper->children[3]);
    incr = _slang_gen_operation(A, &oper->children[2]);
-   loop->Children[0] = new_seq(breakIf,
-                               new_seq(body, incr));
+
+   loop->Children[0] = new_seq(breakIf, body);
+   loop->Children[1] = incr;  /* tail code */
 
    /* pop loop, restore prev */
    A->CurLoop = prevLoop;
@@ -1448,11 +1875,30 @@ _slang_gen_for(slang_assemble_ctx * A, const slang_operation *oper)
 }
 
 
+static slang_ir_node *
+_slang_gen_continue(slang_assemble_ctx * A, const slang_operation *oper)
+{
+   slang_ir_node *n, *loopNode;
+   assert(oper->type == SLANG_OPER_CONTINUE);
+   loopNode = A->CurLoop;
+   assert(loopNode);
+   assert(loopNode->Opcode == IR_LOOP);
+   n = new_node0(IR_CONT);
+   if (n) {
+      n->Parent = loopNode;
+      /* insert this node at head of linked list */
+      n->List = loopNode->List;
+      loopNode->List = n;
+   }
+   return n;
+}
+
+
 /**
  * Determine if the given operation is of a specific type.
  */
 static GLboolean
-is_operation_type(const const slang_operation *oper, slang_operation_type type)
+is_operation_type(const slang_operation *oper, slang_operation_type type)
 {
    if (oper->type == type)
       return GL_TRUE;
@@ -1470,10 +1916,10 @@ is_operation_type(const const slang_operation *oper, slang_operation_type type)
  * IR_IF instruction.
  */
 static slang_ir_node *
-_slang_gen_hl_if(slang_assemble_ctx * A, const slang_operation *oper)
+_slang_gen_if(slang_assemble_ctx * A, const slang_operation *oper)
 {
    /*
-    * eval expr (child[0]), updating condcodes
+    * eval expr (child[0])
     * IF expr THEN
     *    if-body code
     * ELSE
@@ -1482,13 +1928,32 @@ _slang_gen_hl_if(slang_assemble_ctx * A, const slang_operation *oper)
     */
    const GLboolean haveElseClause = !_slang_is_noop(&oper->children[2]);
    slang_ir_node *ifNode, *cond, *ifBody, *elseBody;
+   GLboolean isConst, constTrue;
+
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log, "scalar/boolean expression expected for 'if'");
+      return NULL;
+   }
+
+   isConst = _slang_is_constant_cond(&oper->children[0], &constTrue);
+   if (isConst) {
+      if (constTrue) {
+         /* if (true) ... */
+         return _slang_gen_operation(A, &oper->children[1]);
+      }
+      else {
+         /* if (false) ... */
+         return _slang_gen_operation(A, &oper->children[2]);
+      }
+   }
 
    cond = _slang_gen_operation(A, &oper->children[0]);
    cond = new_cond(cond);
 
    if (is_operation_type(&oper->children[1], SLANG_OPER_BREAK)) {
       /* Special case: generate a conditional break */
-      ifBody = new_break_if(A->CurLoop, cond, GL_TRUE);
+      ifBody = new_break_if_true(A->CurLoop, cond);
       if (haveElseClause) {
          elseBody = _slang_gen_operation(A, &oper->children[2]);
          return new_seq(ifBody, elseBody);
@@ -1497,7 +1962,7 @@ _slang_gen_hl_if(slang_assemble_ctx * A, const slang_operation *oper)
    }
    else if (is_operation_type(&oper->children[1], SLANG_OPER_CONTINUE)) {
       /* Special case: generate a conditional break */
-      ifBody = new_cont_if(A->CurLoop, cond, GL_TRUE);
+      ifBody = new_cont_if_true(A->CurLoop, cond);
       if (haveElseClause) {
          elseBody = _slang_gen_operation(A, &oper->children[2]);
          return new_seq(ifBody, elseBody);
@@ -1518,6 +1983,52 @@ _slang_gen_hl_if(slang_assemble_ctx * A, const slang_operation *oper)
 
 
 
+static slang_ir_node *
+_slang_gen_not(slang_assemble_ctx * A, const slang_operation *oper)
+{
+   slang_ir_node *n;
+
+   assert(oper->type == SLANG_OPER_NOT);
+
+   /* type-check expression */
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log,
+                           "scalar/boolean expression expected for '!'");
+      return NULL;
+   }
+
+   n = _slang_gen_operation(A, &oper->children[0]);
+   if (n)
+      return new_not(n);
+   else
+      return NULL;
+}
+
+
+static slang_ir_node *
+_slang_gen_xor(slang_assemble_ctx * A, const slang_operation *oper)
+{
+   slang_ir_node *n1, *n2;
+
+   assert(oper->type == SLANG_OPER_LOGICALXOR);
+
+   if (!_slang_is_scalar_or_boolean(A, &oper->children[0]) ||
+       !_slang_is_scalar_or_boolean(A, &oper->children[0])) {
+      slang_info_log_error(A->log,
+                           "scalar/boolean expressions expected for '^^'");
+      return NULL;
+   }
+
+   n1 = _slang_gen_operation(A, &oper->children[0]);
+   if (!n1)
+      return NULL;
+   n2 = _slang_gen_operation(A, &oper->children[1]);
+   if (!n2)
+      return NULL;
+   return new_node2(IR_NOTEQUAL, n1, n2);
+}
+
+
 /**
  * Generate IR node for storage of a temporary of given size.
  */
@@ -1525,7 +2036,7 @@ static slang_ir_node *
 _slang_gen_temporary(GLint size)
 {
    slang_ir_storage *store;
-   slang_ir_node *n;
+   slang_ir_node *n = NULL;
 
    store = _slang_new_ir_storage(PROGRAM_TEMPORARY, -1, size);
    if (store) {
@@ -1534,7 +2045,7 @@ _slang_gen_temporary(GLint size)
          n->Store = store;
       }
       else {
-         free(store);
+         _slang_free(store);
       }
    }
    return n;
@@ -1560,6 +2071,15 @@ _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var)
 
       n->Store->File = PROGRAM_TEMPORARY;
       n->Store->Size = _slang_sizeof_type_specifier(&n->Var->type.specifier);
+      if (var->array_len > 0) {
+         /* this is an array */
+         /* round up element size to mult of 4 */
+         GLint sz = (n->Store->Size + 3) & ~3;
+         /* mult by array size */
+         sz *= var->array_len;
+         n->Store->Size = sz;
+      }
+      A->program->NumTemporaries++;
       assert(n->Store->Size > 0);
    }
    return n;
@@ -1568,25 +2088,21 @@ _slang_gen_var_decl(slang_assemble_ctx *A, slang_variable *var)
 
 /**
  * Generate code for a selection expression:   b ? x : y
- * XXX in some cases we could implement a selection expression
+ * XXX In some cases we could implement a selection expression
  * with an LRP instruction (use the boolean as the interpolant).
+ * Otherwise, we use an IF/ELSE/ENDIF construct.
  */
 static slang_ir_node *
 _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
 {
-   slang_label *altLabel, *endLabel;
-   slang_ir_node *altLab, *endLab;
-   slang_ir_node *tree, *tmpDecl, *tmpVar, *cond, *cjump, *jump;
-   slang_ir_node *bodx, *body, *assignx, *assigny;
+   slang_ir_node *cond, *ifNode, *trueExpr, *falseExpr, *trueNode, *falseNode;
+   slang_ir_node *tmpDecl, *tmpVar, *tree;
    slang_typeinfo type;
    int size;
 
    assert(oper->type == SLANG_OPER_SELECT);
    assert(oper->num_children == 3);
 
-   altLabel = _slang_label_new("selectAlt");
-   endLabel = _slang_label_new("selectEnd");
-
    /* size of x or y's type */
    slang_typeinfo_construct(&type);
    _slang_typeof_operation(A, &oper->children[1], &type);
@@ -1596,46 +2112,32 @@ _slang_gen_select(slang_assemble_ctx *A, slang_operation *oper)
    /* temporary var */
    tmpDecl = _slang_gen_temporary(size);
 
-   /* eval condition */
+   /* the condition (child 0) */
    cond = _slang_gen_operation(A, &oper->children[0]);
    cond = new_cond(cond);
-   tree = new_seq(tmpDecl, cond);
 
-   /* jump if false to "alt" label */
-   cjump = new_cjump(altLabel, 0);
-   tree = new_seq(tree, cjump);
-
-   /* evaluate child 1 (x) and assign to tmp */
+   /* if-true body (child 1) */
    tmpVar = new_node0(IR_VAR);
    tmpVar->Store = tmpDecl->Store;
-   body = _slang_gen_operation(A, &oper->children[1]);
-   assigny = new_node2(IR_MOVE, tmpVar, body);
-   tree = new_seq(tree, assigny);
-
-   /* jump to "end" label */
-   jump = new_jump(endLabel);
-   tree = new_seq(tree, jump);
-
-   /* "alt" label */
-   altLab = new_label(altLabel);
-   tree = new_seq(tree, altLab);
+   trueExpr = _slang_gen_operation(A, &oper->children[1]);
+   trueNode = new_node2(IR_MOVE, tmpVar, trueExpr);
 
-   /* evaluate child 2 (y) and assign to tmp */
+   /* if-false body (child 2) */
    tmpVar = new_node0(IR_VAR);
    tmpVar->Store = tmpDecl->Store;
-   bodx = _slang_gen_operation(A, &oper->children[2]);
-   assignx = new_node2(IR_MOVE, tmpVar, bodx);
-   tree = new_seq(tree, assignx);
-
-   /* "end" label */
-   endLab = new_label(endLabel);
-   tree = new_seq(tree, endLab);
-   
+   falseExpr = _slang_gen_operation(A, &oper->children[2]);
+   falseNode = new_node2(IR_MOVE, tmpVar, falseExpr);
+
+   ifNode = new_if(cond, trueNode, falseNode);
+
    /* tmp var value */
    tmpVar = new_node0(IR_VAR);
    tmpVar->Store = tmpDecl->Store;
-   tree = new_seq(tree, tmpVar);
 
+   tree = new_seq(ifNode, tmpVar);
+   tree = new_seq(tmpDecl, tree);
+
+   /*_slang_print_ir_tree(tree, 10);*/
    return tree;
 }
 
@@ -1658,15 +2160,10 @@ _slang_gen_logical_and(slang_assemble_ctx *A, slang_operation *oper)
    slang_operation_copy(&select->children[0], &oper->children[0]);
    slang_operation_copy(&select->children[1], &oper->children[1]);
    select->children[2].type = SLANG_OPER_LITERAL_BOOL;
-   ASSIGN_4V(select->children[2].literal, 0, 0, 0, 0);
-   select->children[2].literal_size = 2;
+   ASSIGN_4V(select->children[2].literal, 0, 0, 0, 0); /* false */
+   select->children[2].literal_size = 1;
 
    n = _slang_gen_select(A, select);
-
-   /* xxx wrong */
-   free(select->children);
-   free(select);
-
    return n;
 }
 
@@ -1688,16 +2185,11 @@ _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
 
    slang_operation_copy(&select->children[0], &oper->children[0]);
    select->children[1].type = SLANG_OPER_LITERAL_BOOL;
-   ASSIGN_4V(select->children[2].literal, 1, 1, 1, 1);
+   ASSIGN_4V(select->children[1].literal, 1, 1, 1, 1); /* true */
+   select->children[1].literal_size = 1;
    slang_operation_copy(&select->children[2], &oper->children[1]);
-   select->children[2].literal_size = 2;
 
    n = _slang_gen_select(A, select);
-
-   /* xxx wrong */
-   free(select->children);
-   free(select);
-
    return n;
 }
 
@@ -1708,25 +2200,24 @@ _slang_gen_logical_or(slang_assemble_ctx *A, slang_operation *oper)
 static slang_ir_node *
 _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
 {
-   if (oper->num_children == 0 ||
-       (oper->num_children == 1 &&
-        oper->children[0].type == SLANG_OPER_VOID)) {
-      /* Convert from:
-       *   return;
-       * To:
-       *   goto __endOfFunction;
-       */
-      slang_ir_node *n;
-      slang_operation gotoOp;
-      slang_operation_construct(&gotoOp);
-      gotoOp.type = SLANG_OPER_GOTO;
-      gotoOp.label = A->CurFunction->end_label;
+   const GLboolean haveReturnValue
+      = (oper->num_children == 1 && oper->children[0].type != SLANG_OPER_VOID);
+
+   /* error checking */
+   assert(A->CurFunction);
+   if (haveReturnValue &&
+       A->CurFunction->header.type.specifier.type == SLANG_SPEC_VOID) {
+      slang_info_log_error(A->log, "illegal return expression");
+      return NULL;
+   }
+   else if (!haveReturnValue &&
+            A->CurFunction->header.type.specifier.type != SLANG_SPEC_VOID) {
+      slang_info_log_error(A->log, "return statement requires an expression");
+      return NULL;
+   }
 
-      /* assemble the new code */
-      n = _slang_gen_operation(A, &gotoOp);
-      /* destroy temp code */
-      slang_operation_destruct(&gotoOp);
-      return n;
+   if (!haveReturnValue) {
+      return new_return(A->curFuncEndLabel);
    }
    else {
       /*
@@ -1734,9 +2225,9 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
        *   return expr;
        * To:
        *   __retVal = expr;
-       *   goto __endOfFunction;
+       *   return;  // goto __endOfFunction
        */
-      slang_operation *block, *assign, *jump;
+      slang_operation *assign;
       slang_atom a_retVal;
       slang_ir_node *n;
 
@@ -1747,21 +2238,15 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
       {
          slang_variable *v
             = _slang_locate_variable(oper->locals, a_retVal, GL_TRUE);
-         assert(v);
+         if (!v) {
+            /* trying to return a value in a void-valued function */
+            return NULL;
+         }
       }
 #endif
 
-      block = slang_operation_new(1);
-      block->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
-      block->num_children = 2;
-      block->children = slang_operation_new(2);
-      assert(block->locals);
-      block->locals->outer_scope = oper->locals->outer_scope;
-
-      /* child[0]: __retVal = expr; */
-      assign = &block->children[0];
+      assign = slang_operation_new(1);
       assign->type = SLANG_OPER_ASSIGN;
-      assign->locals->outer_scope = block->locals;
       assign->num_children = 2;
       assign->children = slang_operation_new(2);
       /* lhs (__retVal) */
@@ -1772,21 +2257,11 @@ _slang_gen_return(slang_assemble_ctx * A, slang_operation *oper)
       /* XXX we might be able to avoid this copy someday */
       slang_operation_copy(&assign->children[1], &oper->children[0]);
 
-      /* child[1]: goto __endOfFunction */
-      jump = &block->children[1];
-      jump->type = SLANG_OPER_GOTO;
-      assert(A->CurFunction->end_label);
-      /* XXX don't call function? */
-      jump->label = A->CurFunction->end_label;
-
-#if 0 /* debug */
-      printf("NEW RETURN:\n");
-      slang_print_tree(block, 0);
-#endif
-
       /* assemble the new code */
-      n = _slang_gen_operation(A, block);
-      slang_operation_delete(block);
+      n = new_seq(_slang_gen_operation(A, assign),
+                  new_return(A->curFuncEndLabel));
+
+      slang_operation_delete(assign);
       return n;
    }
 }
@@ -1821,7 +2296,8 @@ _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
       }
       /* XXX make copy of this initializer? */
       rhs = _slang_gen_operation(A, &oper->children[0]);
-      assert(rhs);
+      if (!rhs)
+         return NULL;  /* must have found an error */
       init = new_node2(IR_MOVE, var, rhs);
       /*assert(rhs->Opcode != IR_SEQ);*/
       n = new_seq(varDecl, init);
@@ -1846,6 +2322,9 @@ _slang_gen_declaration(slang_assemble_ctx *A, slang_operation *oper)
       _slang_simplify(v->initializer, &A->space, A->atoms); 
       rhs = _slang_gen_operation(A, v->initializer);
 #endif
+      if (!rhs)
+         return NULL;
+
       assert(rhs);
       init = new_node2(IR_MOVE, var, rhs);
       /*
@@ -1879,103 +2358,11 @@ _slang_gen_variable(slang_assemble_ctx * A, slang_operation *oper)
 }
 
 
-/**
- * Some write-masked assignments are simple, but others are hard.
- * Simple example:
- *    vec3 v;
- *    v.xy = vec2(a, b);
- * Hard example:
- *    vec3 v;
- *    v.yz = vec2(a, b);
- * this would have to be transformed/swizzled into:
- *    v.yz = vec2(a, b).*xy*         (* = don't care)
- * Instead, we'll effectively do this:
- *    v.y = vec2(a, b).xxxx;
- *    v.z = vec2(a, b).yyyy;
- *
- */
-static GLboolean
-_slang_simple_writemask(GLuint writemask)
-{
-   switch (writemask) {
-   case WRITEMASK_X:
-   case WRITEMASK_Y:
-   case WRITEMASK_Z:
-   case WRITEMASK_W:
-   case WRITEMASK_XY:
-   case WRITEMASK_XYZ:
-   case WRITEMASK_XYZW:
-      return GL_TRUE;
-   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
- */
-static GLboolean
-swizzle_to_writemask(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);
-      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)) {
-      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;
-}
-
-
 static slang_ir_node *
 _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);
       n->Store->Swizzle = swizzle;
@@ -1990,6 +2377,28 @@ _slang_gen_swizzle(slang_ir_node *child, GLuint swizzle)
 static slang_ir_node *
 _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,
+                                  oper->children[0].a_id, GL_TRUE);
+      if (!var) {
+         slang_info_log_error(A->log, "undefined variable '%s'",
+                              (char *) oper->children[0].a_id);
+         return NULL;
+      }
+      if (var->type.qualifier == SLANG_QUAL_CONST ||
+          var->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
+          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);
+         return NULL;
+      }
+   }
+
    if (oper->children[0].type == SLANG_OPER_IDENTIFIER &&
        oper->children[1].type == SLANG_OPER_CALL) {
       /* Special case of:  x = f(a, b)
@@ -2008,6 +2417,19 @@ _slang_gen_assignment(slang_assemble_ctx * A, slang_operation *oper)
    else {
       slang_ir_node *n, *lhs, *rhs;
       lhs = _slang_gen_operation(A, &oper->children[0]);
+
+      if (lhs) {
+         if (!(lhs->Store->File == PROGRAM_OUTPUT ||
+               lhs->Store->File == PROGRAM_TEMPORARY ||
+               (lhs->Store->File == PROGRAM_VARYING &&
+                A->program->Target == GL_VERTEX_PROGRAM_ARB) ||
+               lhs->Store->File == PROGRAM_UNDEFINED)) {
+            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 */
@@ -2038,6 +2460,7 @@ _slang_gen_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);
 
@@ -2057,10 +2480,13 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
 
       n = _slang_gen_operation(A, &oper->children[0]);
       /* create new parent node with swizzle */
-      n = _slang_gen_swizzle(n, swizzle);
+      if (n)
+         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;
@@ -2082,16 +2508,39 @@ _slang_gen_field(slang_assemble_ctx * A, slang_operation *oper)
       /* oper->children[0] is the base */
       /* oper->a_id is the field name */
       slang_ir_node *base, *n;
-      GLint size = 4; /* XXX fix? */
+      slang_typeinfo field_ti;
+      GLint fieldSize, fieldOffset = -1;
+      /* type of field */
+      slang_typeinfo_construct(&field_ti);
+      _slang_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) {
+         slang_info_log_error(A->log,
+                              "\"%s\" is not a member of struct \"%s\"",
+                              (char *) oper->a_id,
+                              (char *) ti.spec._struct->a_name);
+         return NULL;
+      }
+      assert(fieldSize >= 0);
 
       base = _slang_gen_operation(A, &oper->children[0]);
+      if (!base) {
+         /* error msg should have already been logged */
+         return NULL;
+      }
 
       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,
-                                          size);
+                                          fieldSize);
       }
       return n;
 
@@ -2126,6 +2575,7 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
       if (oper->children[1].type != SLANG_OPER_LITERAL_INT ||
           index >= max) {
          slang_info_log_error(A->log, "Invalid array index for vector type");
+         return NULL;
       }
 
       n = _slang_gen_operation(A, &oper->children[0]);
@@ -2145,17 +2595,42 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
       /* conventional array */
       slang_typeinfo elem_ti;
       slang_ir_node *elem, *array, *index;
-      GLint elemSize;
+      GLint elemSize, arrayLen;
 
       /* size of array element */
       slang_typeinfo_construct(&elem_ti);
       _slang_typeof_operation(A, oper, &elem_ti);
       elemSize = _slang_sizeof_type_specifier(&elem_ti.spec);
-      assert(elemSize >= 1);
+
+      if (_slang_type_is_matrix(array_ti.spec.type))
+         arrayLen = _slang_type_dim(array_ti.spec.type);
+      else
+         arrayLen = array_ti.array_len;
+
+      slang_typeinfo_destruct(&array_ti);
+      slang_typeinfo_destruct(&elem_ti);
+
+      if (elemSize <= 0) {
+         /* unknown var or type */
+         slang_info_log_error(A->log, "Undefined variable or type");
+         return NULL;
+      }
 
       array = _slang_gen_operation(A, &oper->children[0]);
       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,
+                                "Array index out of bounds (index=%d size=%d)",
+                                 (int) index->Value[0], arrayLen);
+            _slang_free_ir_tree(array);
+            _slang_free_ir_tree(index);
+            return NULL;
+         }
+
          elem = new_node2(IR_ELEMENT, array, index);
          elem->Store = _slang_new_ir_storage(array->Store->File,
                                              array->Store->Index,
@@ -2164,13 +2639,14 @@ _slang_gen_subscript(slang_assemble_ctx * A, slang_operation *oper)
          return elem;
       }
       else {
+         _slang_free_ir_tree(array);
+         _slang_free_ir_tree(index);
          return NULL;
       }
    }
 }
 
 
-
 /**
  * Generate IR tree for a slang_operation (AST node)
  */
@@ -2209,7 +2685,7 @@ _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
@@ -2234,7 +2710,10 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
 #endif
          return tree;
       }
-      break;
+      else {
+         return new_node0(IR_NOP);
+      }
+
    case SLANG_OPER_EXPRESSION:
       return _slang_gen_operation(A, &oper->children[0]);
 
@@ -2247,22 +2726,24 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
    case SLANG_OPER_BREAK:
       if (!A->CurLoop) {
          slang_info_log_error(A->log, "'break' not in loop");
+         return NULL;
       }
       return new_break(A->CurLoop);
    case SLANG_OPER_CONTINUE:
       if (!A->CurLoop) {
          slang_info_log_error(A->log, "'continue' not in loop");
+         return NULL;
       }
-      return new_cont(A->CurLoop);
+      return _slang_gen_continue(A, oper);
    case SLANG_OPER_DISCARD:
       return new_node0(IR_KILL);
 
    case SLANG_OPER_EQUAL:
-      return new_node2(IR_SEQUAL,
+      return new_node2(IR_EQUAL,
                       _slang_gen_operation(A, &oper->children[0]),
                       _slang_gen_operation(A, &oper->children[1]));
    case SLANG_OPER_NOTEQUAL:
-      return new_node2(IR_SNEQUAL,
+      return new_node2(IR_NOTEQUAL,
                       _slang_gen_operation(A, &oper->children[0]),
                       _slang_gen_operation(A, &oper->children[1]));
    case SLANG_OPER_GREATER:
@@ -2270,19 +2751,17 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
                       _slang_gen_operation(A, &oper->children[0]),
                       _slang_gen_operation(A, &oper->children[1]));
    case SLANG_OPER_LESS:
-      /* child[0] < child[1]  ---->   child[1] > child[0] */
-      return new_node2(IR_SGT,
-                      _slang_gen_operation(A, &oper->children[1]),
-                      _slang_gen_operation(A, &oper->children[0]));
-   case SLANG_OPER_GREATERequal:
-      return new_node2(IR_SGE,
+      return new_node2(IR_SLT,
                       _slang_gen_operation(A, &oper->children[0]),
                       _slang_gen_operation(A, &oper->children[1]));
-   case SLANG_OPER_LESSequal:
-      /* child[0] <= child[1]  ---->   child[1] >= child[0] */
+   case SLANG_OPER_GREATEREQUAL:
       return new_node2(IR_SGE,
-                      _slang_gen_operation(A, &oper->children[1]),
-                      _slang_gen_operation(A, &oper->children[0]));
+                      _slang_gen_operation(A, &oper->children[0]),
+                      _slang_gen_operation(A, &oper->children[1]));
+   case SLANG_OPER_LESSEQUAL:
+      return new_node2(IR_SLE,
+                       _slang_gen_operation(A, &oper->children[0]),
+                       _slang_gen_operation(A, &oper->children[1]));
    case SLANG_OPER_ADD:
       {
         slang_ir_node *n;
@@ -2369,20 +2848,9 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
         return n;
       }
    case SLANG_OPER_LOGICALXOR:
-      {
-        slang_ir_node *n;
-         assert(oper->num_children == 2);
-        n = _slang_gen_function_call_name(A, "__logicalXor", oper, NULL);
-        return n;
-      }
+      return _slang_gen_xor(A, oper);
    case SLANG_OPER_NOT:
-      {
-        slang_ir_node *n;
-         assert(oper->num_children == 1);
-        n = _slang_gen_function_call_name(A, "__logicalNot", oper, NULL);
-        return n;
-      }
-
+      return _slang_gen_not(A, oper);
    case SLANG_OPER_SELECT:  /* b ? x : y */
       {
         slang_ir_node *n;
@@ -2398,14 +2866,12 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
                                            oper, NULL);
    case SLANG_OPER_RETURN:
       return _slang_gen_return(A, oper);
-   case SLANG_OPER_GOTO:
-      return new_jump(oper->label);
    case SLANG_OPER_LABEL:
       return new_label(oper->label);
    case SLANG_OPER_IDENTIFIER:
       return _slang_gen_variable(A, oper);
    case SLANG_OPER_IF:
-      return _slang_gen_hl_if(A, oper);
+      return _slang_gen_if(A, oper);
    case SLANG_OPER_FIELD:
       return _slang_gen_field(A, oper);
    case SLANG_OPER_SUBSCRIPT:
@@ -2415,7 +2881,7 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
    case SLANG_OPER_LITERAL_INT:
       /* fall-through */
    case SLANG_OPER_LITERAL_BOOL:
-      return new_float_literal(oper->literal);
+      return new_float_literal(oper->literal, oper->literal_size);
 
    case SLANG_OPER_POSTINCREMENT:   /* var++ */
       {
@@ -2446,25 +2912,29 @@ _slang_gen_operation(slang_assemble_ctx * A, slang_operation *oper)
         return n;
       }
 
+   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 (oper->type == SLANG_OPER_NON_INLINED_CALL) {
+            tree = new_function_call(tree, oper->label);
          }
          return tree;
       }
 
    case SLANG_OPER_NONE:
-      return NULL;
    case SLANG_OPER_VOID:
-      return NULL;
+      /* returning NULL here would generate an error */
+      return new_node0(IR_NOP);
 
    default:
-      printf("Unhandled node type %d\n", oper->type);
-      abort();
+      _mesa_problem(NULL, "bad node type %d in _slang_gen_operation",
+                    oper->type);
       return new_node0(IR_NOP);
    }
 
@@ -2493,20 +2963,19 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
    struct gl_program *prog = A->program;
    const char *varName = (char *) var->a_name;
    GLboolean success = GL_TRUE;
-   GLint texIndex;
    slang_ir_storage *store = NULL;
    int dbg = 0;
-
-   texIndex = sampler_to_texture_index(var->type.specifier.type);
+   const GLenum datatype = _slang_gltype_from_specifier(&var->type.specifier);
+   const GLint texIndex = sampler_to_texture_index(var->type.specifier.type);
 
    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);
-      store = _slang_new_ir_storage(PROGRAM_SAMPLER, samplerUniform, texIndex);
+      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) {
@@ -2515,8 +2984,35 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
                          * MAX2(var->array_len, 1);
       if (prog) {
          /* user-defined uniform */
-         GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName, size);
-         store = _slang_new_ir_storage(PROGRAM_UNIFORM, uniformLoc, size);
+         if (datatype == GL_NONE) {
+            if (var->type.specifier.type == SLANG_SPEC_STRUCT) {
+               _mesa_problem(NULL, "user-declared uniform structs not supported yet");
+               /* XXX what we need to do is unroll the struct into its
+                * basic types, creating a uniform variable for each.
+                * For example:
+                * struct foo {
+                *   vec3 a;
+                *   vec4 b;
+                * };
+                * uniform foo f;
+                *
+                * Should produce uniforms:
+                * "f.a"  (GL_FLOAT_VEC3)
+                * "f.b"  (GL_FLOAT_VEC4)
+                */
+            }
+            else {
+               slang_info_log_error(A->log,
+                                    "invalid datatype for uniform variable %s",
+                                    (char *) var->a_name);
+            }
+            return GL_FALSE;
+         }
+         else {
+            GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+                                                 size, datatype);
+            store = _slang_new_ir_storage(PROGRAM_UNIFORM, uniformLoc, size);
+         }
       }
       else {
          /* pre-defined uniform, like gl_ModelviewMatrix */
@@ -2537,9 +3033,12 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
       else {
          /* pre-defined varying, like gl_Color or gl_TexCoord */
          if (type == SLANG_UNIT_FRAGMENT_BUILTIN) {
-            GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB);
+            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);
          }
          else {
@@ -2566,17 +3065,23 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
       }
       else {
          /* pre-defined vertex attrib */
-         GLint index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB);
+         GLuint swizzle;
+         GLint index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB,
+                                          &swizzle);
          GLint size = 4; /* XXX? */
          assert(index >= 0);
          store = _slang_new_ir_storage(PROGRAM_INPUT, index, size);
+         store->Swizzle = swizzle;
       }
       if (dbg) printf("ATTRIB ");
    }
    else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {
-      GLint index = _slang_input_index(varName, GL_FRAGMENT_PROGRAM_ARB);
+      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;
       if (dbg) printf("INPUT ");
    }
    else if (var->type.qualifier == SLANG_QUAL_FIXEDOUTPUT) {
@@ -2586,9 +3091,9 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
          store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
       }
       else {
-         assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
          GLint index = _slang_output_index(varName, GL_FRAGMENT_PROGRAM_ARB);
          GLint size = 4; /* XXX? */
+         assert(type == SLANG_UNIT_FRAGMENT_BUILTIN);
          store = _slang_new_ir_storage(PROGRAM_OUTPUT, index, size);
       }
       if (dbg) printf("OUTPUT ");
@@ -2652,15 +3157,30 @@ _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 */
+      if (fun->header.type.specifier.type != SLANG_SPEC_VOID) {
+         /* check that non-void functions actually return something */
+         slang_operation *op
+            = _slang_find_node_type(fun->body, SLANG_OPER_RETURN);
+         if (!op) {
+            slang_info_log_error(A->log,
+                                 "function \"%s\" has no return statement",
+                                 (char *) fun->header.a_name);
+            printf(
+                   "function \"%s\" has no return statement\n",
+                   (char *) fun->header.a_name);
+            return GL_FALSE;
+         }
+      }
+#endif
       return GL_TRUE;  /* not an error */
    }
 
-#if 1
-   printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
-#endif
 #if 0
+   printf("\n*********** codegen_function %s\n", (char *) fun->header.a_name);
    slang_print_function(fun, 1);
 #endif
 
@@ -2668,15 +3188,19 @@ _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
    assert(A->program->Parameters );
    assert(A->program->Varying);
    assert(A->vartable);
+   A->CurLoop = NULL;
+   A->CurFunction = fun;
 
    /* fold constant expressions, etc. */
    _slang_simplify(fun->body, &A->space, A->atoms);
 
-   A->CurFunction = fun;
+#if 0
+   printf("\n*********** simplified %s\n", (char *) fun->header.a_name);
+   slang_print_function(fun, 1);
+#endif
 
    /* Create an end-of-function label */
-   if (!A->CurFunction->end_label)
-      A->CurFunction->end_label = _slang_label_new("__endOfFunc__main");
+   A->curFuncEndLabel = _slang_label_new("__endOfFunc__main");
 
    /* push new vartable scope */
    _slang_push_var_table(A->vartable);
@@ -2695,9 +3219,10 @@ _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
    }
 
    /* append an end-of-function-label to IR tree */
-   n = new_seq(n, new_label(fun->end_label));
+   n = new_seq(n, new_label(A->curFuncEndLabel));
 
-   A->CurFunction = NULL;
+   /*_slang_label_delete(A->curFuncEndLabel);*/
+   A->curFuncEndLabel = NULL;
 
 #if 0
    printf("************* New AST for %s *****\n", (char*)fun->header.a_name);
@@ -2705,9 +3230,9 @@ _slang_codegen_function(slang_assemble_ctx * A, slang_function * fun)
 #endif
 #if 0
    printf("************* IR for %s *******\n", (char*)fun->header.a_name);
-   slang_print_ir(n, 0);
+   _slang_print_ir_tree(n, 0);
 #endif
-#if 1
+#if 0
    printf("************* End codegen function ************\n\n");
 #endif