Browse Source

Applied VS2012 / ShaderCompiler patch from Colin Barrett.

Lasse Öörni 13 years ago
parent
commit
48da9e9093

+ 4 - 3
CMakeLists.txt

@@ -96,11 +96,11 @@ endmacro ()
 # Macro for exe finalization
 macro (finalize_exe)
     if (MSVC)
-        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different \"$(TARGETPATH)\" ${PROJECT_BINARY_DIR}/Bin)
-        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different \"$(TARGETDIR)$(TARGETNAME).pdb\" ${PROJECT_BINARY_DIR}/Bin)
+        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different \"$(TARGETPATH)\" ${PROJECT_SOURCE_DIR}/Bin)
+        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different \"$(TARGETDIR)$(TARGETNAME).pdb\" ${PROJECT_SOURCE_DIR}/Bin)
     elseif (NOT IOS)
         get_target_property (EXECUTABLE_NAME ${TARGET_NAME} LOCATION)
-        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${EXECUTABLE_NAME} ${PROJECT_BINARY_DIR}/Bin)
+        add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${EXECUTABLE_NAME} ${PROJECT_SOURCE_DIR}/Bin)
     endif ()
 endmacro ()
 
@@ -149,5 +149,6 @@ if (NOT IOS)
 endif ()
 
 if (NOT USE_OPENGL)
+    add_subdirectory (ThirdParty/MojoShader)
     add_subdirectory (Tools/ShaderCompiler)
 endif ()

+ 1 - 0
Docs/GettingStarted.dox

@@ -250,6 +250,7 @@ The third-party libraries are used for the following functionality:
 - GLee: OpenGL extensions handling
 - kNet: UDP networking
 - libcpuid: CPU properties detection
+- MojoShader: parsing HLSL bytecode after shader compilation
 - Open Asset Import Library: reading various 3D file formats
 - pugixml: parsing XML files
 - SDL: window and OpenGL context creation, input and sound output

+ 2 - 0
Docs/Urho3D.dox

@@ -69,6 +69,7 @@ Urho3D uses the following third-party libraries:
 - GLee 5.4 (http://elf-stone.com/)
 - kNet (https://github.com/juj/kNet)
 - libcpuid 0.2.0 (http://libcpuid.sourceforge.net/)
+- MojoShader (http://icculus.org/mojoshader/)
 - Open Asset Import Library 3.0 (http://assimp.sourceforge.net/)
 - pugixml 1.0 (http://pugixml.org/)
 - SDL 2.0 (http://www.libsdl.org/)
@@ -78,6 +79,7 @@ Urho3D uses the following third-party libraries:
 
 Contributions and bugfixes from:
 
+- Colin Barrett
 - Miika Santala
 - Magic.Lixin
 - skaiware

+ 26 - 1
License.txt

@@ -197,6 +197,32 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
+MojoShader license
+------------------
+
+Copyright (c) 2008-2011 Ryan C. Gordon.
+
+This software is provided 'as-is', without any express or implied warranty.
+In no event will the authors be held liable for any damages arising from
+the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must not
+claim that you wrote the original software. If you use this software in a
+product, an acknowledgment in the product documentation would be
+appreciated but is not required.
+
+2. Altered source versions must be plainly marked as such, and must not be
+misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any source distribution.
+
+   Ryan C. Gordon <[email protected]>
+
+
 Oolong Engine license
 ---------------------
 
@@ -326,4 +352,3 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-

+ 2 - 0
Readme.txt

@@ -42,6 +42,7 @@ Urho3D uses the following third-party libraries:
 - GLee 5.4 (http://elf-stone.com/)
 - kNet (https://github.com/juj/kNet)
 - libcpuid 0.2.0 (http://libcpuid.sourceforge.net/)
+- MojoShader (http://icculus.org/mojoshader/)
 - Open Asset Import Library 3.0 (http://assimp.sourceforge.net/)
 - pugixml 1.0 (http://pugixml.org/)
 - SDL 2.0 (http://www.libsdl.org/)
@@ -51,6 +52,7 @@ Urho3D uses the following third-party libraries:
 - stb_vorbis 0.99996 (http://nothings.org/)
 
 Contributions and bugfixes from:
+- Colin Barrett
 - Miika Santala
 - Magic.Lixin
 - skaiware

+ 1 - 0
ThirdParty/Assimp/code/AssimpPCH.h

@@ -118,6 +118,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <cstdio>
 #include <limits.h>
 #include <memory>
+#include <functional>
 
 // Boost headers
 #include <boost/pointer_cast.hpp>

+ 16 - 0
ThirdParty/MojoShader/CMakeLists.txt

@@ -0,0 +1,16 @@
+# Define target name
+set (TARGET_NAME MojoShader)
+
+# Define source files
+file (GLOB C_FILES *.c)
+file (GLOB H_FILES *.h)
+set (SOURCE_FILES ${C_FILES} ${H_FILES})
+
+add_definitions(-DMOJOSHADER_NO_VERSION_INCLUDE)
+if(MSVC)
+    add_definitions(-D_CRT_SECURE_NO_WARNINGS=1 -TP)
+endif(MSVC)
+
+# Define target & libraries to link
+add_library (${TARGET_NAME} STATIC ${SOURCE_FILES})
+finalize_lib ()

+ 22 - 0
ThirdParty/MojoShader/LICENSE.txt

@@ -0,0 +1,22 @@
+
+   Copyright (c) 2008-2011 Ryan C. Gordon.
+
+   This software is provided 'as-is', without any express or implied warranty.
+   In no event will the authors be held liable for any damages arising from
+   the use of this software.
+
+   Permission is granted to anyone to use this software for any purpose,
+   including commercial applications, and to alter it and redistribute it
+   freely, subject to the following restrictions:
+
+   1. The origin of this software must not be misrepresented; you must not
+   claim that you wrote the original software. If you use this software in a
+   product, an acknowledgment in the product documentation would be
+   appreciated but is not required.
+
+   2. Altered source versions must be plainly marked as such, and must not be
+   misrepresented as being the original software.
+
+   3. This notice may not be removed or altered from any source distribution.
+
+       Ryan C. Gordon <[email protected]>

+ 12 - 0
ThirdParty/MojoShader/README.txt

@@ -0,0 +1,12 @@
+To use this in your project:
+
+- Add mojoshader*.c and mojoshader*.h to your project.
+- Compile mojoshader*.c
+- If you don't have a C99-compliant compiler, like Microsoft Visual Studio,
+  you'll need to compile the .c files as C++ to get them to build.
+- If you don't have cmake to generate mojoshader_version.h, you can either
+  add a blank file with that name, or add MOJOSHADER_NO_VERSION_INCLUDE to
+  your preprocessor definitions.
+
+// end of README.txt ...
+

+ 9545 - 0
ThirdParty/MojoShader/mojoshader.c

@@ -0,0 +1,9545 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+// !!! FIXME: this file really needs to be split up.
+// !!! FIXME: I keep changing coding styles for symbols and typedefs.
+
+// !!! FIXME: rules from MSDN about temp registers we probably don't check.
+// - There are limited temporaries: vs_1_1 has 12 (ps_1_1 has _2_!).
+// - SM2 apparently was variable, between 12 and 32. Shader Model 3 has 32.
+// - A maximum of three temp registers can be used in a single instruction.
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+typedef struct ConstantsList
+{
+    MOJOSHADER_constant constant;
+    struct ConstantsList *next;
+} ConstantsList;
+
+typedef struct VariableList
+{
+    MOJOSHADER_uniformType type;
+    int index;
+    int count;
+    ConstantsList *constant;
+    int used;
+    int emit_position;  // used in some profiles.
+    struct VariableList *next;
+} VariableList;
+
+typedef struct RegisterList
+{
+    RegisterType regtype;
+    int regnum;
+    MOJOSHADER_usage usage;
+    unsigned int index;
+    int writemask;
+    int misc;
+    int written;
+    const VariableList *array;
+    struct RegisterList *next;
+} RegisterList;
+
+typedef struct
+{
+    const uint32 *token;   // this is the unmolested token in the stream.
+    int regnum;
+    int swizzle;  // xyzw (all four, not split out).
+    int swizzle_x;
+    int swizzle_y;
+    int swizzle_z;
+    int swizzle_w;
+    SourceMod src_mod;
+    RegisterType regtype;
+    int relative;
+    RegisterType relative_regtype;
+    int relative_regnum;
+    int relative_component;
+    const VariableList *relative_array;
+} SourceArgInfo;
+
+struct Profile;  // predeclare.
+
+typedef struct CtabData
+{
+    int have_ctab;
+    int symbol_count;
+    MOJOSHADER_symbol *symbols;
+} CtabData;
+
+// Context...this is state that changes as we parse through a shader...
+typedef struct Context
+{
+    int isfail;
+    int out_of_memory;
+    MOJOSHADER_malloc malloc;
+    MOJOSHADER_free free;
+    void *malloc_data;
+    int current_position;
+    const uint32 *orig_tokens;
+    const uint32 *tokens;
+    uint32 tokencount;
+    const MOJOSHADER_swizzle *swizzles;
+    unsigned int swizzles_count;
+    const MOJOSHADER_samplerMap *samplermap;
+    unsigned int samplermap_count;
+    Buffer *output;
+    Buffer *preflight;
+    Buffer *globals;
+    Buffer *helpers;
+    Buffer *subroutines;
+    Buffer *mainline_intro;
+    Buffer *mainline;
+    Buffer *ignore;
+    Buffer *output_stack[2];
+    int indent_stack[2];
+    int output_stack_len;
+    int indent;
+    const char *shader_type_str;
+    const char *endline;
+    int endline_len;
+    int profileid;
+    const struct Profile *profile;
+    MOJOSHADER_shaderType shader_type;
+    uint8 major_ver;
+    uint8 minor_ver;
+    DestArgInfo dest_arg;
+    SourceArgInfo source_args[5];
+    SourceArgInfo predicate_arg;  // for predicated instructions.
+    uint32 dwords[4];
+    uint32 version_token;
+    int instruction_count;
+    uint32 instruction_controls;
+    uint32 previous_opcode;
+    int coissue;
+    int loops;
+    int reps;
+    int max_reps;
+    int cmps;
+    int scratch_registers;
+    int max_scratch_registers;
+    int branch_labels_stack_index;
+    int branch_labels_stack[32];
+    int assigned_branch_labels;
+    int assigned_vertex_attributes;
+    int last_address_reg_component;
+    RegisterList used_registers;
+    RegisterList defined_registers;
+    ErrorList *errors;
+    int constant_count;
+    ConstantsList *constants;
+    int uniform_count;
+    int uniform_float4_count;
+    int uniform_int4_count;
+    int uniform_bool_count;
+    RegisterList uniforms;
+    int attribute_count;
+    RegisterList attributes;
+    int sampler_count;
+    RegisterList samplers;
+    VariableList *variables;  // variables to register mapping.
+    int centroid_allowed;
+    CtabData ctab;
+    int have_relative_input_registers;
+    int have_multi_color_outputs;
+    int determined_constants_arrays;
+    int predicated;
+    int uses_pointsize;
+    int uses_fog;
+    int glsl_generated_lit_helper;
+    int glsl_generated_texldd_setup;
+    int glsl_generated_texm3x3spec_helper;
+    int arb1_wrote_position;
+    int have_preshader;
+    int ignores_ctab;
+    int reset_texmpad;
+    int texm3x2pad_dst0;
+    int texm3x2pad_src0;
+    int texm3x3pad_dst0;
+    int texm3x3pad_src0;
+    int texm3x3pad_dst1;
+    int texm3x3pad_src1;
+    MOJOSHADER_preshader *preshader;
+
+#if SUPPORT_PROFILE_ARB1_NV
+    int profile_supports_nv2;
+    int profile_supports_nv3;
+    int profile_supports_nv4;
+#endif
+#if SUPPORT_PROFILE_GLSL120
+    int profile_supports_glsl120;
+#endif
+} Context;
+
+
+// Use these macros so we can remove all bits of these profiles from the build.
+#if SUPPORT_PROFILE_ARB1_NV
+#define support_nv2(ctx) ((ctx)->profile_supports_nv2)
+#define support_nv3(ctx) ((ctx)->profile_supports_nv3)
+#define support_nv4(ctx) ((ctx)->profile_supports_nv4)
+#else
+#define support_nv2(ctx) (0)
+#define support_nv3(ctx) (0)
+#define support_nv4(ctx) (0)
+#endif
+
+#if SUPPORT_PROFILE_GLSL120
+#define support_glsl120(ctx) ((ctx)->profile_supports_glsl120)
+#else
+#define support_glsl120(ctx) (0)
+#endif
+
+
+// Profile entry points...
+
+// one emit function for each opcode in each profile.
+typedef void (*emit_function)(Context *ctx);
+
+// one emit function for starting output in each profile.
+typedef void (*emit_start)(Context *ctx, const char *profilestr);
+
+// one emit function for ending output in each profile.
+typedef void (*emit_end)(Context *ctx);
+
+// one emit function for phase opcode output in each profile.
+typedef void (*emit_phase)(Context *ctx);
+
+// one emit function for finalizing output in each profile.
+typedef void (*emit_finalize)(Context *ctx);
+
+// one emit function for global definitions in each profile.
+typedef void (*emit_global)(Context *ctx, RegisterType regtype, int regnum);
+
+// one emit function for relative uniform arrays in each profile.
+typedef void (*emit_array)(Context *ctx, VariableList *var);
+
+// one emit function for relative constants arrays in each profile.
+typedef void (*emit_const_array)(Context *ctx,
+                                 const struct ConstantsList *constslist,
+                                 int base, int size);
+
+// one emit function for uniforms in each profile.
+typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum,
+                             const VariableList *var);
+
+// one emit function for samplers in each profile.
+typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype,
+                             int texbem);
+
+// one emit function for attributes in each profile.
+typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum,
+                               MOJOSHADER_usage usage, int index, int wmask,
+                               int flags);
+
+// one args function for each possible sequence of opcode arguments.
+typedef int (*args_function)(Context *ctx);
+
+// one state function for each opcode where we have state machine updates.
+typedef void (*state_function)(Context *ctx);
+
+// one function for varnames in each profile.
+typedef const char *(*varname_function)(Context *c, RegisterType t, int num);
+
+// one function for const var array in each profile.
+typedef const char *(*const_array_varname_function)(Context *c, int base, int size);
+
+typedef struct Profile
+{
+    const char *name;
+    emit_start start_emitter;
+    emit_end end_emitter;
+    emit_phase phase_emitter;
+    emit_global global_emitter;
+    emit_array array_emitter;
+    emit_const_array const_array_emitter;
+    emit_uniform uniform_emitter;
+    emit_sampler sampler_emitter;
+    emit_attribute attribute_emitter;
+    emit_finalize finalize_emitter;
+    varname_function get_varname;
+    const_array_varname_function get_const_array_varname;
+} Profile;
+
+
+// Convenience functions for allocators...
+#if !MOJOSHADER_FORCE_ALLOCATOR
+void *MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
+void MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }
+#endif
+
+MOJOSHADER_error MOJOSHADER_out_of_mem_error = {
+    "Out of memory", NULL, MOJOSHADER_POSITION_NONE
+};
+
+MOJOSHADER_parseData MOJOSHADER_out_of_mem_data = {
+    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0,
+    MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
+};
+
+
+// !!! FIXME: cut and paste between every damned source file follows...
+// !!! FIXME: We need to make some sort of ContextBase that applies to all
+// !!! FIXME:  files and move this stuff to mojoshader_common.c ...
+
+static inline void out_of_memory(Context *ctx)
+{
+    ctx->isfail = ctx->out_of_memory = 1;
+} // out_of_memory
+
+static inline void *Malloc(Context *ctx, const size_t len)
+{
+    void *retval = ctx->malloc((int) len, ctx->malloc_data);
+    if (retval == NULL)
+        out_of_memory(ctx);
+    return retval;
+} // Malloc
+
+static inline char *StrDup(Context *ctx, const char *str)
+{
+    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
+    if (retval != NULL)
+        strcpy(retval, str);
+    return retval;
+} // StrDup
+
+static inline void Free(Context *ctx, void *ptr)
+{
+    ctx->free(ptr, ctx->malloc_data);
+} // Free
+
+static void *MallocBridge(int bytes, void *data)
+{
+    return Malloc((Context *) data, (size_t) bytes);
+} // MallocBridge
+
+static void FreeBridge(void *ptr, void *data)
+{
+    Free((Context *) data, ptr);
+} // FreeBridge
+
+
+// jump between output sections in the context...
+
+static int set_output(Context *ctx, Buffer **section)
+{
+    // only create output sections on first use.
+    if (*section == NULL)
+    {
+        *section = buffer_create(256, MallocBridge, FreeBridge, ctx);
+        if (*section == NULL)
+            return 0;
+    } // if
+
+    ctx->output = *section;
+    return 1;
+} // set_output
+
+static void push_output(Context *ctx, Buffer **section)
+{
+    assert(ctx->output_stack_len < (int) (STATICARRAYLEN(ctx->output_stack)));
+    ctx->output_stack[ctx->output_stack_len] = ctx->output;
+    ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
+    ctx->output_stack_len++;
+    if (!set_output(ctx, section))
+        return;
+    ctx->indent = 0;
+} // push_output
+
+static inline void pop_output(Context *ctx)
+{
+    assert(ctx->output_stack_len > 0);
+    ctx->output_stack_len--;
+    ctx->output = ctx->output_stack[ctx->output_stack_len];
+    ctx->indent = ctx->indent_stack[ctx->output_stack_len];
+} // pop_output
+
+
+
+// Shader model version magic...
+
+static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
+{
+    return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 1 : (minor)) );
+} // version_ui32
+
+static inline int shader_version_supported(const uint8 maj, const uint8 min)
+{
+    return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
+} // shader_version_supported
+
+static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
+                                         const uint8 min)
+{
+    return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
+} // shader_version_atleast
+
+static inline int shader_version_exactly(const Context *ctx, const uint8 maj,
+                                         const uint8 min)
+{
+    return ((ctx->major_ver == maj) && (ctx->minor_ver == min));
+} // shader_version_exactly
+
+static inline int shader_is_pixel(const Context *ctx)
+{
+    return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
+} // shader_is_pixel
+
+static inline int shader_is_vertex(const Context *ctx)
+{
+    return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
+} // shader_is_vertex
+
+
+static inline int isfail(const Context *ctx)
+{
+    return ctx->isfail;
+} // isfail
+
+
+static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void failf(Context *ctx, const char *fmt, ...)
+{
+    ctx->isfail = 1;
+    if (ctx->out_of_memory)
+        return;
+
+    // no filename at this level (we pass a NULL to errorlist_add_va()...)
+    va_list ap;
+    va_start(ap, fmt);
+    errorlist_add_va(ctx->errors, NULL, ctx->current_position, fmt, ap);
+    va_end(ap);
+} // failf
+
+
+static inline void fail(Context *ctx, const char *reason)
+{
+    failf(ctx, "%s", reason);
+} // fail
+
+
+static void output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void output_line(Context *ctx, const char *fmt, ...)
+{
+    assert(ctx->output != NULL);
+    if (isfail(ctx))
+        return;  // we failed previously, don't go on...
+
+    const int indent = ctx->indent;
+    if (indent > 0)
+    {
+        char *indentbuf = (char *) alloca(indent);
+        memset(indentbuf, '\t', indent);
+        buffer_append(ctx->output, indentbuf, indent);
+    } // if
+
+    va_list ap;
+    va_start(ap, fmt);
+    buffer_append_va(ctx->output, fmt, ap);
+    va_end(ap);
+
+    buffer_append(ctx->output, ctx->endline, ctx->endline_len);
+} // output_line
+
+
+static inline void output_blank_line(Context *ctx)
+{
+    assert(ctx->output != NULL);
+    if (!isfail(ctx))
+        buffer_append(ctx->output, ctx->endline, ctx->endline_len);
+} // output_blank_line
+
+
+// !!! FIXME: this is sort of nasty.
+static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
+                     int leavedecimal)
+{
+    const size_t len = snprintf(buf, bufsize, "%f", f);
+    if ((len+2) >= bufsize)
+        fail(ctx, "BUG: internal buffer is too small");
+    else
+    {
+        char *end = buf + len;
+        char *ptr = strchr(buf, '.');
+        if (ptr == NULL)
+        {
+            if (leavedecimal)
+                strcat(buf, ".0");
+            return;  // done.
+        } // if
+
+        while (--end != ptr)
+        {
+            if (*end != '0')
+            {
+                end++;
+                break;
+            } // if
+        } // while
+        if ((leavedecimal) && (end == ptr))
+            end += 2;
+        *end = '\0';  // chop extra '0' or all decimal places off.
+    } // else
+} // floatstr
+
+static inline TextureType cvtMojoToD3DSamplerType(const MOJOSHADER_samplerType type)
+{
+    return (TextureType) (((int) type) + 2);
+} // cvtMojoToD3DSamplerType
+
+static inline MOJOSHADER_samplerType cvtD3DToMojoSamplerType(const TextureType type)
+{
+    return (MOJOSHADER_samplerType) (((int) type) - 2);
+} // cvtD3DToMojoSamplerType
+
+
+// Deal with register lists...  !!! FIXME: I sort of hate this.
+
+static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
+{
+    while (item != NULL)
+    {
+        RegisterList *next = item->next;
+        f(item, d);
+        item = next;
+    } // while
+} // free_reglist
+
+static inline uint32 reg_to_ui32(const RegisterType regtype, const int regnum)
+{
+    return ( ((uint32) regtype) | (((uint32) regnum) << 16) );
+} // reg_to_uint32
+
+// !!! FIXME: ditch this for a hash table.
+static RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
+                                    const RegisterType regtype,
+                                    const int regnum)
+{
+    const uint32 newval = reg_to_ui32(regtype, regnum);
+    RegisterList *item = prev->next;
+    while (item != NULL)
+    {
+        const uint32 val = reg_to_ui32(item->regtype, item->regnum);
+        if (newval == val)
+            return item;  // already set, so we're done.
+        else if (newval < val)  // insert it here.
+            break;
+        else // if (newval > val)
+        {
+            // keep going, we're not to the insertion point yet.
+            prev = item;
+            item = item->next;
+        } // else
+    } // while
+
+    // we need to insert an entry after (prev).
+    item = (RegisterList *) Malloc(ctx, sizeof (RegisterList));
+    if (item != NULL)
+    {
+        item->regtype = regtype;
+        item->regnum = regnum;
+        item->usage = MOJOSHADER_USAGE_UNKNOWN;
+        item->index = 0;
+        item->writemask = 0;
+        item->misc = 0;
+        item->array = NULL;
+        item->next = prev->next;
+        prev->next = item;
+    } // if
+
+    return item;
+} // reglist_insert
+
+static RegisterList *reglist_find(const RegisterList *prev,
+                                  const RegisterType rtype, const int regnum)
+{
+    const uint32 newval = reg_to_ui32(rtype, regnum);
+    RegisterList *item = prev->next;
+    while (item != NULL)
+    {
+        const uint32 val = reg_to_ui32(item->regtype, item->regnum);
+        if (newval == val)
+            return item;  // here it is.
+        else if (newval < val)  // should have been here if it existed.
+            return NULL;
+        else // if (newval > val)
+            item = item->next;
+    } // while
+
+    return NULL;  // wasn't in the list.
+} // reglist_find
+
+static inline const RegisterList *reglist_exists(RegisterList *prev,
+                                                 const RegisterType regtype,
+                                                 const int regnum)
+{
+    return (reglist_find(prev, regtype, regnum));
+} // reglist_exists
+
+static inline int register_was_written(Context *ctx, const RegisterType rtype,
+                                       const int regnum)
+{
+    RegisterList *reg = reglist_find(&ctx->used_registers, rtype, regnum);
+    return (reg && reg->written);
+} // register_was_written
+
+static inline RegisterList *set_used_register(Context *ctx,
+                                              const RegisterType regtype,
+                                              const int regnum,
+                                              const int written)
+{
+    RegisterList *reg = NULL;
+    if ((regtype == REG_TYPE_COLOROUT) && (regnum > 0))
+        ctx->have_multi_color_outputs = 1;
+
+    reg = reglist_insert(ctx, &ctx->used_registers, regtype, regnum);
+    if (reg && written)
+        reg->written = 1;
+    return reg;
+} // set_used_register
+
+static inline int get_used_register(Context *ctx, const RegisterType regtype,
+                                    const int regnum)
+{
+    return (reglist_exists(&ctx->used_registers, regtype, regnum) != NULL);
+} // get_used_register
+
+static inline void set_defined_register(Context *ctx, const RegisterType rtype,
+                                        const int regnum)
+{
+    reglist_insert(ctx, &ctx->defined_registers, rtype, regnum);
+} // set_defined_register
+
+static inline int get_defined_register(Context *ctx, const RegisterType rtype,
+                                       const int regnum)
+{
+    return (reglist_exists(&ctx->defined_registers, rtype, regnum) != NULL);
+} // get_defined_register
+
+static void add_attribute_register(Context *ctx, const RegisterType rtype,
+                                const int regnum, const MOJOSHADER_usage usage,
+                                const int index, const int writemask, int flags)
+{
+    RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
+    item->usage = usage;
+    item->index = index;
+    item->writemask = writemask;
+    item->misc = flags;
+
+    if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_POINTSIZE))
+        ctx->uses_pointsize = 1;  // note that we have to check this later.
+    else if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_FOG))
+        ctx->uses_fog = 1;  // note that we have to check this later.
+} // add_attribute_register
+
+static inline void add_sampler(Context *ctx, const int regnum,
+                               TextureType ttype, const int texbem)
+{
+    const RegisterType rtype = REG_TYPE_SAMPLER;
+
+    // !!! FIXME: make sure it doesn't exist?
+    // !!! FIXME:  (ps_1_1 assume we can add it multiple times...)
+    RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
+
+    if (ctx->samplermap != NULL)
+    {
+        unsigned int i;
+        for (i = 0; i < ctx->samplermap_count; i++)
+        {
+            if (ctx->samplermap[i].index == regnum)
+            {
+                ttype = cvtMojoToD3DSamplerType(ctx->samplermap[i].type);
+                break;
+            } // if
+        } // for
+    } // if
+
+    item->index = (int) ttype;
+    item->misc |= texbem;
+} // add_sampler
+
+
+static inline int writemask_xyzw(const int writemask)
+{
+    return (writemask == 0xF);  // 0xF == 1111. No explicit mask (full!).
+} // writemask_xyzw
+
+
+static inline int writemask_xyz(const int writemask)
+{
+    return (writemask == 0x7);  // 0x7 == 0111. (that is: xyz)
+} // writemask_xyz
+
+
+static inline int writemask_xy(const int writemask)
+{
+    return (writemask == 0x3);  // 0x3 == 0011. (that is: xy)
+} // writemask_xy
+
+
+static inline int writemask_x(const int writemask)
+{
+    return (writemask == 0x1);  // 0x1 == 0001. (that is: x)
+} // writemask_x
+
+
+static inline int writemask_y(const int writemask)
+{
+    return (writemask == 0x2);  // 0x1 == 0010. (that is: y)
+} // writemask_y
+
+
+static inline int replicate_swizzle(const int swizzle)
+{
+    return ( (((swizzle >> 0) & 0x3) == ((swizzle >> 2) & 0x3)) &&
+             (((swizzle >> 2) & 0x3) == ((swizzle >> 4) & 0x3)) &&
+             (((swizzle >> 4) & 0x3) == ((swizzle >> 6) & 0x3)) );
+} // replicate_swizzle
+
+
+static inline int no_swizzle(const int swizzle)
+{
+    return (swizzle == 0xE4);  // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
+} // no_swizzle
+
+
+static inline int vecsize_from_writemask(const int m)
+{
+    return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
+} // vecsize_from_writemask
+
+
+static inline void set_dstarg_writemask(DestArgInfo *dst, const int mask)
+{
+    dst->writemask = mask;
+    dst->writemask0 = ((mask >> 0) & 1);
+    dst->writemask1 = ((mask >> 1) & 1);
+    dst->writemask2 = ((mask >> 2) & 1);
+    dst->writemask3 = ((mask >> 3) & 1);
+} // set_dstarg_writemask
+
+
+static int allocate_scratch_register(Context *ctx)
+{
+    const int retval = ctx->scratch_registers++;
+    if (retval >= ctx->max_scratch_registers)
+        ctx->max_scratch_registers = retval + 1;
+    return retval;
+} // allocate_scratch_register
+
+static int allocate_branch_label(Context *ctx)
+{
+    return ctx->assigned_branch_labels++;
+} // allocate_branch_label
+
+static inline void adjust_token_position(Context *ctx, const int incr)
+{
+    ctx->tokens += incr;
+    ctx->tokencount -= incr;
+    ctx->current_position += incr * sizeof (uint32);
+} // adjust_token_position
+
+
+// D3D stuff that's used in more than just the d3d profile...
+
+static int isscalar(Context *ctx, const MOJOSHADER_shaderType shader_type,
+                    const RegisterType rtype, const int rnum)
+{
+    const int uses_psize = ctx->uses_pointsize;
+    const int uses_fog = ctx->uses_fog;
+    if ( (rtype == REG_TYPE_OUTPUT) && ((uses_psize) || (uses_fog)) )
+    {
+        const RegisterList *reg = reglist_find(&ctx->attributes, rtype, rnum);
+        if (reg != NULL)
+        {
+            const MOJOSHADER_usage usage = reg->usage;
+            return ( (uses_psize && (usage == MOJOSHADER_USAGE_POINTSIZE)) ||
+                     (uses_fog && (usage == MOJOSHADER_USAGE_FOG)) );
+        } // if
+    } // if
+
+    return scalar_register(shader_type, rtype, rnum);
+} // isscalar
+
+static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
+
+
+static const char *usagestrs[] = {
+    "_position", "_blendweight", "_blendindices", "_normal", "_psize",
+    "_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
+    "_color", "_fog", "_depth", "_sample"
+};
+
+static const char *get_D3D_register_string(Context *ctx,
+                                           RegisterType regtype,
+                                           int regnum, char *regnum_str,
+                                           size_t regnum_size)
+{
+    const char *retval = NULL;
+    int has_number = 1;
+
+    switch (regtype)
+    {
+        case REG_TYPE_TEMP:
+            retval = "r";
+            break;
+
+        case REG_TYPE_INPUT:
+            retval = "v";
+            break;
+
+        case REG_TYPE_CONST:
+            retval = "c";
+            break;
+
+        case REG_TYPE_ADDRESS:  // (or REG_TYPE_TEXTURE, same value.)
+            retval = shader_is_vertex(ctx) ? "a" : "t";
+            break;
+
+        case REG_TYPE_RASTOUT:
+            switch ((RastOutType) regnum)
+            {
+                case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
+                case RASTOUT_TYPE_FOG: retval = "oFog"; break;
+                case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
+            } // switch
+            has_number = 0;
+            break;
+
+        case REG_TYPE_ATTROUT:
+            retval = "oD";
+            break;
+
+        case REG_TYPE_OUTPUT: // (or REG_TYPE_TEXCRDOUT, same value.)
+            if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
+                retval = "o";
+            else
+                retval = "oT";
+            break;
+
+        case REG_TYPE_CONSTINT:
+            retval = "i";
+            break;
+
+        case REG_TYPE_COLOROUT:
+            retval = "oC";
+            break;
+
+        case REG_TYPE_DEPTHOUT:
+            retval = "oDepth";
+            has_number = 0;
+            break;
+
+        case REG_TYPE_SAMPLER:
+            retval = "s";
+            break;
+
+        case REG_TYPE_CONSTBOOL:
+            retval = "b";
+            break;
+
+        case REG_TYPE_LOOP:
+            retval = "aL";
+            has_number = 0;
+            break;
+
+        case REG_TYPE_MISCTYPE:
+            switch ((const MiscTypeType) regnum)
+            {
+                case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
+                case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
+            } // switch
+            has_number = 0;
+            break;
+
+        case REG_TYPE_LABEL:
+            retval = "l";
+            break;
+
+        case REG_TYPE_PREDICATE:
+            retval = "p";
+            break;
+
+        //case REG_TYPE_TEMPFLOAT16:  // !!! FIXME: don't know this asm string
+        default:
+            fail(ctx, "unknown register type");
+            retval = "???";
+            has_number = 0;
+            break;
+    } // switch
+
+    if (has_number)
+        snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
+    else
+        regnum_str[0] = '\0';
+
+    return retval;
+} // get_D3D_register_string
+
+
+// !!! FIXME: can we split the profile code out to separate source files?
+
+#define AT_LEAST_ONE_PROFILE 0
+
+#if !SUPPORT_PROFILE_D3D
+#define PROFILE_EMITTER_D3D(op)
+#else
+#undef AT_LEAST_ONE_PROFILE
+#define AT_LEAST_ONE_PROFILE 1
+#define PROFILE_EMITTER_D3D(op) emit_D3D_##op,
+
+static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
+                                                 const SourceArgInfo *arg,
+                                                 char *buf, size_t buflen)
+{
+    const char *premod_str = "";
+    const char *postmod_str = "";
+    switch (arg->src_mod)
+    {
+        case SRCMOD_NEGATE:
+            premod_str = "-";
+            break;
+
+        case SRCMOD_BIASNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_BIAS:
+            postmod_str = "_bias";
+            break;
+
+        case SRCMOD_SIGNNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_SIGN:
+            postmod_str = "_bx2";
+            break;
+
+        case SRCMOD_COMPLEMENT:
+            premod_str = "1-";
+            break;
+
+        case SRCMOD_X2NEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_X2:
+            postmod_str = "_x2";
+            break;
+
+        case SRCMOD_DZ:
+            postmod_str = "_dz";
+            break;
+
+        case SRCMOD_DW:
+            postmod_str = "_dw";
+            break;
+
+        case SRCMOD_ABSNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_ABS:
+            postmod_str = "_abs";
+            break;
+
+        case SRCMOD_NOT:
+            premod_str = "!";
+            break;
+
+        case SRCMOD_NONE:
+        case SRCMOD_TOTAL:
+             break;  // stop compiler whining.
+    } // switch
+
+
+    char regnum_str[16];
+    const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
+                                                      arg->regnum, regnum_str,
+                                                      sizeof (regnum_str));
+
+    if (regtype_str == NULL)
+    {
+        fail(ctx, "Unknown source register type.");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const char *rel_lbracket = "";
+    const char *rel_rbracket = "";
+    char rel_swizzle[4] = { '\0' };
+    char rel_regnum_str[16] = { '\0' };
+    const char *rel_regtype_str = "";
+    if (arg->relative)
+    {
+        rel_swizzle[0] = '.';
+        rel_swizzle[1] = swizzle_channels[arg->relative_component];
+        rel_swizzle[2] = '\0';
+        rel_lbracket = "[";
+        rel_rbracket = "]";
+        rel_regtype_str = get_D3D_register_string(ctx, arg->relative_regtype,
+                                                  arg->relative_regnum,
+                                                  rel_regnum_str,
+                                                  sizeof (rel_regnum_str));
+
+        if (regtype_str == NULL)
+        {
+            fail(ctx, "Unknown relative source register type.");
+            *buf = '\0';
+            return buf;
+        } // if
+    } // if
+
+    char swizzle_str[6];
+    size_t i = 0;
+    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
+    if (!scalar && !no_swizzle(arg->swizzle))
+    {
+        swizzle_str[i++] = '.';
+        swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
+        swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
+        swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
+        swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
+
+        // .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
+        while (swizzle_str[i-1] == swizzle_str[i-2])
+            i--;
+    } // if
+    swizzle_str[i] = '\0';
+    assert(i < sizeof (swizzle_str));
+
+    // !!! FIXME: c12[a0.x] actually needs to be c[a0.x + 12]
+    snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s",
+             premod_str, regtype_str, regnum_str, postmod_str,
+             rel_lbracket, rel_regtype_str, rel_regnum_str, rel_swizzle,
+             rel_rbracket, swizzle_str);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_D3D_srcarg_string_in_buf
+
+
+static const char *make_D3D_destarg_string(Context *ctx, char *buf,
+                                           const size_t buflen)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+
+    const char *result_shift_str = "";
+    switch (arg->result_shift)
+    {
+        case 0x1: result_shift_str = "_x2"; break;
+        case 0x2: result_shift_str = "_x4"; break;
+        case 0x3: result_shift_str = "_x8"; break;
+        case 0xD: result_shift_str = "_d8"; break;
+        case 0xE: result_shift_str = "_d4"; break;
+        case 0xF: result_shift_str = "_d2"; break;
+    } // switch
+
+    const char *sat_str = (arg->result_mod & MOD_SATURATE) ? "_sat" : "";
+    const char *pp_str = (arg->result_mod & MOD_PP) ? "_pp" : "";
+    const char *cent_str = (arg->result_mod & MOD_CENTROID) ? "_centroid" : "";
+
+    char regnum_str[16];
+    const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
+                                                      arg->regnum, regnum_str,
+                                                      sizeof (regnum_str));
+    if (regtype_str == NULL)
+    {
+        fail(ctx, "Unknown destination register type.");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    char writemask_str[6];
+    size_t i = 0;
+    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
+    if (!scalar && !writemask_xyzw(arg->writemask))
+    {
+        writemask_str[i++] = '.';
+        if (arg->writemask0) writemask_str[i++] = 'x';
+        if (arg->writemask1) writemask_str[i++] = 'y';
+        if (arg->writemask2) writemask_str[i++] = 'z';
+        if (arg->writemask3) writemask_str[i++] = 'w';
+    } // if
+    writemask_str[i] = '\0';
+    assert(i < sizeof (writemask_str));
+
+    const char *pred_left = "";
+    const char *pred_right = "";
+    char pred[32] = { '\0' };
+    if (ctx->predicated)
+    {
+        pred_left = "(";
+        pred_right = ") ";
+        make_D3D_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
+                                      pred, sizeof (pred));
+    } // if
+
+    // may turn out something like "_x2_sat_pp_centroid (!p0.x) r0.xyzw" ...
+    snprintf(buf, buflen, "%s%s%s%s %s%s%s%s%s%s",
+             result_shift_str, sat_str, pp_str, cent_str,
+             pred_left, pred, pred_right,
+             regtype_str, regnum_str, writemask_str);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_D3D_destarg_string
+
+
+static const char *make_D3D_srcarg_string(Context *ctx, const size_t idx,
+                                          char *buf, size_t buflen)
+{
+    if (idx >= STATICARRAYLEN(ctx->source_args))
+    {
+        fail(ctx, "Too many source args");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const SourceArgInfo *arg = &ctx->source_args[idx];
+    return make_D3D_srcarg_string_in_buf(ctx, arg, buf, buflen);
+} // make_D3D_srcarg_string
+
+static const char *get_D3D_varname_in_buf(Context *ctx, RegisterType rt,
+                                           int regnum, char *buf,
+                                           const size_t len)
+{
+    char regnum_str[16];
+    const char *regtype_str = get_D3D_register_string(ctx, rt, regnum,
+                                              regnum_str, sizeof (regnum_str));
+    snprintf(buf,len,"%s%s", regtype_str, regnum_str);
+    return buf;
+} // get_D3D_varname_in_buf
+
+
+static const char *get_D3D_varname(Context *ctx, RegisterType rt, int regnum)
+{
+    char buf[64];
+    get_D3D_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf));
+    return StrDup(ctx, buf);
+} // get_D3D_varname
+
+
+static const char *get_D3D_const_array_varname(Context *ctx, int base, int size)
+{
+    char buf[64];
+    snprintf(buf, sizeof (buf), "c_array_%d_%d", base, size);
+    return StrDup(ctx, buf);
+} // get_D3D_const_array_varname
+
+
+static void emit_D3D_start(Context *ctx, const char *profilestr)
+{
+    const uint major = (uint) ctx->major_ver;
+    const uint minor = (uint) ctx->minor_ver;
+    char minor_str[16];
+
+    ctx->ignores_ctab = 1;
+
+    if (minor == 0xFF)
+        strcpy(minor_str, "sw");
+    else if ((major > 1) && (minor == 1))
+        strcpy(minor_str, "x");  // for >= SM2, apparently this is "x". Weird.
+    else
+        snprintf(minor_str, sizeof (minor_str), "%u", (uint) minor);
+
+    output_line(ctx, "%s_%u_%s", ctx->shader_type_str, major, minor_str);
+} // emit_D3D_start
+
+
+static void emit_D3D_end(Context *ctx)
+{
+    output_line(ctx, "end");
+} // emit_D3D_end
+
+
+static void emit_D3D_phase(Context *ctx)
+{
+    output_line(ctx, "phase");
+} // emit_D3D_phase
+
+
+static void emit_D3D_finalize(Context *ctx)
+{
+    // no-op.
+} // emit_D3D_finalize
+
+
+static void emit_D3D_global(Context *ctx, RegisterType regtype, int regnum)
+{
+    // no-op.
+} // emit_D3D_global
+
+
+static void emit_D3D_array(Context *ctx, VariableList *var)
+{
+    // no-op.
+} // emit_D3D_array
+
+
+static void emit_D3D_const_array(Context *ctx, const ConstantsList *clist,
+                                 int base, int size)
+{
+    // no-op.
+} // emit_D3D_const_array
+
+
+static void emit_D3D_uniform(Context *ctx, RegisterType regtype, int regnum,
+                             const VariableList *var)
+{
+    // no-op.
+} // emit_D3D_uniform
+
+
+static void emit_D3D_sampler(Context *ctx, int s, TextureType ttype, int tb)
+{
+    // no-op.
+} // emit_D3D_sampler
+
+
+static void emit_D3D_attribute(Context *ctx, RegisterType regtype, int regnum,
+                               MOJOSHADER_usage usage, int index, int wmask,
+                               int flags)
+{
+    // no-op.
+} // emit_D3D_attribute
+
+
+static void emit_D3D_RESERVED(Context *ctx)
+{
+    // do nothing; fails in the state machine.
+} // emit_D3D_RESERVED
+
+
+// Generic D3D opcode emitters. A list of macros generate all the entry points
+//  that call into these...
+
+static char *lowercase(char *dst, const char *src)
+{
+    int i = 0;
+    do
+    {
+        const char ch = src[i];
+        dst[i] = (((ch >= 'A') && (ch <= 'Z')) ? (ch - ('A' - 'a')) : ch);
+    } while (src[i++]);
+    return dst;
+} // lowercase
+
+
+static void emit_D3D_opcode_d(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s%s", ctx->coissue ? "+" : "", opcode, dst);
+} // emit_D3D_opcode_d
+
+
+static void emit_D3D_opcode_s(Context *ctx, const char *opcode)
+{
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s %s", ctx->coissue ? "+" : "", opcode, src0);
+} // emit_D3D_opcode_s
+
+
+static void emit_D3D_opcode_ss(Context *ctx, const char *opcode)
+{
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s %s, %s", ctx->coissue ? "+" : "", opcode, src0, src1);
+} // emit_D3D_opcode_ss
+
+
+static void emit_D3D_opcode_ds(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s%s, %s", ctx->coissue ? "+" : "", opcode, dst, src0);
+} // emit_D3D_opcode_ds
+
+
+static void emit_D3D_opcode_dss(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s%s, %s, %s", ctx->coissue ? "+" : "",
+                opcode, dst, src0, src1);
+} // emit_D3D_opcode_dss
+
+
+static void emit_D3D_opcode_dsss(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_D3D_srcarg_string(ctx, 2, src2, sizeof (src2));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s%s, %s, %s, %s", ctx->coissue ? "+" : "", 
+                opcode, dst, src0, src1, src2);
+} // emit_D3D_opcode_dsss
+
+
+static void emit_D3D_opcode_dssss(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_D3D_srcarg_string(ctx, 2, src2, sizeof (src2));
+    char src3[64]; make_D3D_srcarg_string(ctx, 3, src3, sizeof (src3));
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx,"%s%s%s, %s, %s, %s, %s", ctx->coissue ? "+" : "",
+                opcode, dst, src0, src1, src2, src3);
+} // emit_D3D_opcode_dssss
+
+
+static void emit_D3D_opcode(Context *ctx, const char *opcode)
+{
+    opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
+    output_line(ctx, "%s%s", ctx->coissue ? "+" : "", opcode);
+} // emit_D3D_opcode
+
+
+#define EMIT_D3D_OPCODE_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_D_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_d(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_S_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_s(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_SS_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_ss(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_DS_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_ds(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_DSS_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_dss(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_DSSS_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_dsss(ctx, #op); \
+    }
+#define EMIT_D3D_OPCODE_DSSSS_FUNC(op) \
+    static void emit_D3D_##op(Context *ctx) { \
+        emit_D3D_opcode_dssss(ctx, #op); \
+    }
+
+EMIT_D3D_OPCODE_FUNC(NOP)
+EMIT_D3D_OPCODE_DS_FUNC(MOV)
+EMIT_D3D_OPCODE_DSS_FUNC(ADD)
+EMIT_D3D_OPCODE_DSS_FUNC(SUB)
+EMIT_D3D_OPCODE_DSSS_FUNC(MAD)
+EMIT_D3D_OPCODE_DSS_FUNC(MUL)
+EMIT_D3D_OPCODE_DS_FUNC(RCP)
+EMIT_D3D_OPCODE_DS_FUNC(RSQ)
+EMIT_D3D_OPCODE_DSS_FUNC(DP3)
+EMIT_D3D_OPCODE_DSS_FUNC(DP4)
+EMIT_D3D_OPCODE_DSS_FUNC(MIN)
+EMIT_D3D_OPCODE_DSS_FUNC(MAX)
+EMIT_D3D_OPCODE_DSS_FUNC(SLT)
+EMIT_D3D_OPCODE_DSS_FUNC(SGE)
+EMIT_D3D_OPCODE_DS_FUNC(EXP)
+EMIT_D3D_OPCODE_DS_FUNC(LOG)
+EMIT_D3D_OPCODE_DS_FUNC(LIT)
+EMIT_D3D_OPCODE_DSS_FUNC(DST)
+EMIT_D3D_OPCODE_DSSS_FUNC(LRP)
+EMIT_D3D_OPCODE_DS_FUNC(FRC)
+EMIT_D3D_OPCODE_DSS_FUNC(M4X4)
+EMIT_D3D_OPCODE_DSS_FUNC(M4X3)
+EMIT_D3D_OPCODE_DSS_FUNC(M3X4)
+EMIT_D3D_OPCODE_DSS_FUNC(M3X3)
+EMIT_D3D_OPCODE_DSS_FUNC(M3X2)
+EMIT_D3D_OPCODE_S_FUNC(CALL)
+EMIT_D3D_OPCODE_SS_FUNC(CALLNZ)
+EMIT_D3D_OPCODE_SS_FUNC(LOOP)
+EMIT_D3D_OPCODE_FUNC(RET)
+EMIT_D3D_OPCODE_FUNC(ENDLOOP)
+EMIT_D3D_OPCODE_S_FUNC(LABEL)
+EMIT_D3D_OPCODE_DSS_FUNC(POW)
+EMIT_D3D_OPCODE_DSS_FUNC(CRS)
+EMIT_D3D_OPCODE_DSSS_FUNC(SGN)
+EMIT_D3D_OPCODE_DS_FUNC(ABS)
+EMIT_D3D_OPCODE_DS_FUNC(NRM)
+EMIT_D3D_OPCODE_S_FUNC(REP)
+EMIT_D3D_OPCODE_FUNC(ENDREP)
+EMIT_D3D_OPCODE_S_FUNC(IF)
+EMIT_D3D_OPCODE_FUNC(ELSE)
+EMIT_D3D_OPCODE_FUNC(ENDIF)
+EMIT_D3D_OPCODE_FUNC(BREAK)
+EMIT_D3D_OPCODE_DS_FUNC(MOVA)
+EMIT_D3D_OPCODE_D_FUNC(TEXKILL)
+EMIT_D3D_OPCODE_DS_FUNC(TEXBEM)
+EMIT_D3D_OPCODE_DS_FUNC(TEXBEML)
+EMIT_D3D_OPCODE_DS_FUNC(TEXREG2AR)
+EMIT_D3D_OPCODE_DS_FUNC(TEXREG2GB)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2PAD)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2TEX)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3PAD)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3TEX)
+EMIT_D3D_OPCODE_DSS_FUNC(TEXM3X3SPEC)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3VSPEC)
+EMIT_D3D_OPCODE_DS_FUNC(EXPP)
+EMIT_D3D_OPCODE_DS_FUNC(LOGP)
+EMIT_D3D_OPCODE_DSSS_FUNC(CND)
+EMIT_D3D_OPCODE_DS_FUNC(TEXREG2RGB)
+EMIT_D3D_OPCODE_DS_FUNC(TEXDP3TEX)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2DEPTH)
+EMIT_D3D_OPCODE_DS_FUNC(TEXDP3)
+EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3)
+EMIT_D3D_OPCODE_D_FUNC(TEXDEPTH)
+EMIT_D3D_OPCODE_DSSS_FUNC(CMP)
+EMIT_D3D_OPCODE_DSS_FUNC(BEM)
+EMIT_D3D_OPCODE_DSSS_FUNC(DP2ADD)
+EMIT_D3D_OPCODE_DS_FUNC(DSX)
+EMIT_D3D_OPCODE_DS_FUNC(DSY)
+EMIT_D3D_OPCODE_DSSSS_FUNC(TEXLDD)
+EMIT_D3D_OPCODE_DSS_FUNC(TEXLDL)
+EMIT_D3D_OPCODE_S_FUNC(BREAKP)
+
+// special cases for comparison opcodes...
+static const char *get_D3D_comparison_string(Context *ctx)
+{
+    static const char *comps[] = {
+        "", "_gt", "_eq", "_ge", "_lt", "_ne", "_le"
+    };
+
+    if (ctx->instruction_controls >= STATICARRAYLEN(comps))
+    {
+        fail(ctx, "unknown comparison control");
+        return "";
+    } // if
+
+    return comps[ctx->instruction_controls];
+} // get_D3D_comparison_string
+
+static void emit_D3D_BREAKC(Context *ctx)
+{
+    char op[16];
+    snprintf(op, sizeof (op), "break%s", get_D3D_comparison_string(ctx));
+    emit_D3D_opcode_ss(ctx, op);
+} // emit_D3D_BREAKC
+
+static void emit_D3D_IFC(Context *ctx)
+{
+    char op[16];
+    snprintf(op, sizeof (op), "if%s", get_D3D_comparison_string(ctx));
+    emit_D3D_opcode_ss(ctx, op);
+} // emit_D3D_IFC
+
+static void emit_D3D_SETP(Context *ctx)
+{
+    char op[16];
+    snprintf(op, sizeof (op), "setp%s", get_D3D_comparison_string(ctx));
+    emit_D3D_opcode_dss(ctx, op);
+} // emit_D3D_SETP
+
+static void emit_D3D_DEF(Context *ctx)
+{
+    char dst[64];
+    make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
+    char val0[32];
+    char val1[32];
+    char val2[32];
+    char val3[32];
+    floatstr(ctx, val0, sizeof (val0), val[0], 0);
+    floatstr(ctx, val1, sizeof (val1), val[1], 0);
+    floatstr(ctx, val2, sizeof (val2), val[2], 0);
+    floatstr(ctx, val3, sizeof (val3), val[3], 0);
+    output_line(ctx, "def%s, %s, %s, %s, %s", dst, val0, val1, val2, val3);
+} // emit_D3D_DEF
+
+static void emit_D3D_DEFI(Context *ctx)
+{
+    char dst[64];
+    make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    const int32 *x = (const int32 *) ctx->dwords;
+    output_line(ctx, "defi%s, %d, %d, %d, %d", dst,
+                (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
+} // emit_D3D_DEFI
+
+static void emit_D3D_DEFB(Context *ctx)
+{
+    char dst[64];
+    make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    output_line(ctx, "defb%s, %s", dst, ctx->dwords[0] ? "true" : "false");
+} // emit_D3D_DEFB
+
+
+static void emit_D3D_DCL(Context *ctx)
+{
+    char dst[64];
+    make_D3D_destarg_string(ctx, dst, sizeof (dst));
+    const DestArgInfo *arg = &ctx->dest_arg;
+    const char *usage_str = "";
+    char index_str[16] = { '\0' };
+
+    if (arg->regtype == REG_TYPE_SAMPLER)
+    {
+        switch ((const TextureType) ctx->dwords[0])
+        {
+            case TEXTURE_TYPE_2D: usage_str = "_2d"; break;
+            case TEXTURE_TYPE_CUBE: usage_str = "_cube"; break;
+            case TEXTURE_TYPE_VOLUME: usage_str = "_volume"; break;
+            default: fail(ctx, "unknown sampler texture type"); return;
+        } // switch
+    } // if
+
+    else if (arg->regtype == REG_TYPE_MISCTYPE)
+    {
+        switch ((const MiscTypeType) arg->regnum)
+        {
+            case MISCTYPE_TYPE_POSITION:
+            case MISCTYPE_TYPE_FACE:
+                usage_str = "";  // just become "dcl vFace" or whatever.
+                break;
+            default: fail(ctx, "unknown misc register type"); return;
+        } // switch
+    } // else if
+
+    else
+    {
+        const uint32 usage = ctx->dwords[0];
+        const uint32 index = ctx->dwords[1];
+        usage_str = usagestrs[usage];
+        if (index != 0)
+            snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+    } // else
+
+    output_line(ctx, "dcl%s%s%s", usage_str, index_str, dst);
+} // emit_D3D_DCL
+
+
+static void emit_D3D_TEXCRD(Context *ctx)
+{
+    // this opcode looks and acts differently depending on the shader model.
+    if (shader_version_atleast(ctx, 1, 4))
+        emit_D3D_opcode_ds(ctx, "texcrd");
+    else
+        emit_D3D_opcode_d(ctx, "texcoord");
+} // emit_D3D_TEXCOORD
+
+static void emit_D3D_TEXLD(Context *ctx)
+{
+    // this opcode looks and acts differently depending on the shader model.
+    if (shader_version_atleast(ctx, 2, 0))
+    {
+        if (ctx->instruction_controls == CONTROL_TEXLD)
+           emit_D3D_opcode_dss(ctx, "texld");
+        else if (ctx->instruction_controls == CONTROL_TEXLDP)
+           emit_D3D_opcode_dss(ctx, "texldp");
+        else if (ctx->instruction_controls == CONTROL_TEXLDB)
+           emit_D3D_opcode_dss(ctx, "texldb");
+    } // if
+
+    else if (shader_version_atleast(ctx, 1, 4))
+    {
+        emit_D3D_opcode_ds(ctx, "texld");
+    } // else if
+
+    else
+    {
+        emit_D3D_opcode_d(ctx, "tex");
+    } // else
+} // emit_D3D_TEXLD
+
+static void emit_D3D_SINCOS(Context *ctx)
+{
+    // this opcode needs extra registers for sm2 and lower.
+    if (!shader_version_atleast(ctx, 3, 0))
+        emit_D3D_opcode_dsss(ctx, "sincos");
+    else
+        emit_D3D_opcode_ds(ctx, "sincos");
+} // emit_D3D_SINCOS
+
+
+#undef EMIT_D3D_OPCODE_FUNC
+#undef EMIT_D3D_OPCODE_D_FUNC
+#undef EMIT_D3D_OPCODE_S_FUNC
+#undef EMIT_D3D_OPCODE_SS_FUNC
+#undef EMIT_D3D_OPCODE_DS_FUNC
+#undef EMIT_D3D_OPCODE_DSS_FUNC
+#undef EMIT_D3D_OPCODE_DSSS_FUNC
+#undef EMIT_D3D_OPCODE_DSSSS_FUNC
+
+#endif  // SUPPORT_PROFILE_D3D
+
+
+#if !SUPPORT_PROFILE_BYTECODE
+#define PROFILE_EMITTER_BYTECODE(op)
+#else
+#undef AT_LEAST_ONE_PROFILE
+#define AT_LEAST_ONE_PROFILE 1
+#define PROFILE_EMITTER_BYTECODE(op) emit_BYTECODE_##op,
+
+static void emit_BYTECODE_start(Context *ctx, const char *profilestr)
+{
+    ctx->ignores_ctab = 1;
+
+    // just copy the whole token stream and make all other emitters no-ops.
+    if (set_output(ctx, &ctx->mainline))
+    {
+        const size_t len = ctx->tokencount * sizeof (uint32);
+        buffer_append(ctx->mainline, (const char *) ctx->tokens, len);
+    } // if
+} // emit_BYTECODE_start
+
+static void emit_BYTECODE_end(Context *ctx) {}
+static void emit_BYTECODE_phase(Context *ctx) {}
+static void emit_BYTECODE_finalize(Context *ctx) {}
+static void emit_BYTECODE_global(Context *ctx, RegisterType t, int n) {}
+static void emit_BYTECODE_array(Context *ctx, VariableList *var) {}
+static void emit_BYTECODE_sampler(Context *c, int s, TextureType t, int tb) {}
+static void emit_BYTECODE_const_array(Context *ctx, const ConstantsList *c,
+                                         int base, int size) {}
+static void emit_BYTECODE_uniform(Context *ctx, RegisterType t, int n,
+                                  const VariableList *var) {}
+static void emit_BYTECODE_attribute(Context *ctx, RegisterType t, int n,
+                                       MOJOSHADER_usage u, int i, int w,
+                                       int f) {}
+
+static const char *get_BYTECODE_varname(Context *ctx, RegisterType rt, int regnum)
+{
+    char regnum_str[16];
+    const char *regtype_str = get_D3D_register_string(ctx, rt, regnum,
+                                              regnum_str, sizeof (regnum_str));
+    char buf[64];
+    snprintf(buf, sizeof (buf), "%s%s", regtype_str, regnum_str);
+    return StrDup(ctx, buf);
+} // get_BYTECODE_varname
+
+static const char *get_BYTECODE_const_array_varname(Context *ctx, int base, int size)
+{
+    char buf[64];
+    snprintf(buf, sizeof (buf), "c_array_%d_%d", base, size);
+    return StrDup(ctx, buf);
+} // get_BYTECODE_const_array_varname
+
+#define EMIT_BYTECODE_OPCODE_FUNC(op) \
+    static void emit_BYTECODE_##op(Context *ctx) {}
+
+EMIT_BYTECODE_OPCODE_FUNC(RESERVED)
+EMIT_BYTECODE_OPCODE_FUNC(NOP)
+EMIT_BYTECODE_OPCODE_FUNC(MOV)
+EMIT_BYTECODE_OPCODE_FUNC(ADD)
+EMIT_BYTECODE_OPCODE_FUNC(SUB)
+EMIT_BYTECODE_OPCODE_FUNC(MAD)
+EMIT_BYTECODE_OPCODE_FUNC(MUL)
+EMIT_BYTECODE_OPCODE_FUNC(RCP)
+EMIT_BYTECODE_OPCODE_FUNC(RSQ)
+EMIT_BYTECODE_OPCODE_FUNC(DP3)
+EMIT_BYTECODE_OPCODE_FUNC(DP4)
+EMIT_BYTECODE_OPCODE_FUNC(MIN)
+EMIT_BYTECODE_OPCODE_FUNC(MAX)
+EMIT_BYTECODE_OPCODE_FUNC(SLT)
+EMIT_BYTECODE_OPCODE_FUNC(SGE)
+EMIT_BYTECODE_OPCODE_FUNC(EXP)
+EMIT_BYTECODE_OPCODE_FUNC(LOG)
+EMIT_BYTECODE_OPCODE_FUNC(LIT)
+EMIT_BYTECODE_OPCODE_FUNC(DST)
+EMIT_BYTECODE_OPCODE_FUNC(LRP)
+EMIT_BYTECODE_OPCODE_FUNC(FRC)
+EMIT_BYTECODE_OPCODE_FUNC(M4X4)
+EMIT_BYTECODE_OPCODE_FUNC(M4X3)
+EMIT_BYTECODE_OPCODE_FUNC(M3X4)
+EMIT_BYTECODE_OPCODE_FUNC(M3X3)
+EMIT_BYTECODE_OPCODE_FUNC(M3X2)
+EMIT_BYTECODE_OPCODE_FUNC(CALL)
+EMIT_BYTECODE_OPCODE_FUNC(CALLNZ)
+EMIT_BYTECODE_OPCODE_FUNC(LOOP)
+EMIT_BYTECODE_OPCODE_FUNC(RET)
+EMIT_BYTECODE_OPCODE_FUNC(ENDLOOP)
+EMIT_BYTECODE_OPCODE_FUNC(LABEL)
+EMIT_BYTECODE_OPCODE_FUNC(POW)
+EMIT_BYTECODE_OPCODE_FUNC(CRS)
+EMIT_BYTECODE_OPCODE_FUNC(SGN)
+EMIT_BYTECODE_OPCODE_FUNC(ABS)
+EMIT_BYTECODE_OPCODE_FUNC(NRM)
+EMIT_BYTECODE_OPCODE_FUNC(SINCOS)
+EMIT_BYTECODE_OPCODE_FUNC(REP)
+EMIT_BYTECODE_OPCODE_FUNC(ENDREP)
+EMIT_BYTECODE_OPCODE_FUNC(IF)
+EMIT_BYTECODE_OPCODE_FUNC(ELSE)
+EMIT_BYTECODE_OPCODE_FUNC(ENDIF)
+EMIT_BYTECODE_OPCODE_FUNC(BREAK)
+EMIT_BYTECODE_OPCODE_FUNC(MOVA)
+EMIT_BYTECODE_OPCODE_FUNC(TEXKILL)
+EMIT_BYTECODE_OPCODE_FUNC(TEXBEM)
+EMIT_BYTECODE_OPCODE_FUNC(TEXBEML)
+EMIT_BYTECODE_OPCODE_FUNC(TEXREG2AR)
+EMIT_BYTECODE_OPCODE_FUNC(TEXREG2GB)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X2PAD)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X2TEX)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X3PAD)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X3TEX)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X3SPEC)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X3VSPEC)
+EMIT_BYTECODE_OPCODE_FUNC(EXPP)
+EMIT_BYTECODE_OPCODE_FUNC(LOGP)
+EMIT_BYTECODE_OPCODE_FUNC(CND)
+EMIT_BYTECODE_OPCODE_FUNC(TEXREG2RGB)
+EMIT_BYTECODE_OPCODE_FUNC(TEXDP3TEX)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X2DEPTH)
+EMIT_BYTECODE_OPCODE_FUNC(TEXDP3)
+EMIT_BYTECODE_OPCODE_FUNC(TEXM3X3)
+EMIT_BYTECODE_OPCODE_FUNC(TEXDEPTH)
+EMIT_BYTECODE_OPCODE_FUNC(CMP)
+EMIT_BYTECODE_OPCODE_FUNC(BEM)
+EMIT_BYTECODE_OPCODE_FUNC(DP2ADD)
+EMIT_BYTECODE_OPCODE_FUNC(DSX)
+EMIT_BYTECODE_OPCODE_FUNC(DSY)
+EMIT_BYTECODE_OPCODE_FUNC(TEXLDD)
+EMIT_BYTECODE_OPCODE_FUNC(TEXLDL)
+EMIT_BYTECODE_OPCODE_FUNC(BREAKP)
+EMIT_BYTECODE_OPCODE_FUNC(BREAKC)
+EMIT_BYTECODE_OPCODE_FUNC(IFC)
+EMIT_BYTECODE_OPCODE_FUNC(SETP)
+EMIT_BYTECODE_OPCODE_FUNC(DEF)
+EMIT_BYTECODE_OPCODE_FUNC(DEFI)
+EMIT_BYTECODE_OPCODE_FUNC(DEFB)
+EMIT_BYTECODE_OPCODE_FUNC(DCL)
+EMIT_BYTECODE_OPCODE_FUNC(TEXCRD)
+EMIT_BYTECODE_OPCODE_FUNC(TEXLD)
+
+#undef EMIT_BYTECODE_OPCODE_FUNC
+
+#endif  // SUPPORT_PROFILE_BYTECODE
+
+
+#if !SUPPORT_PROFILE_GLSL
+#define PROFILE_EMITTER_GLSL(op)
+#else
+#undef AT_LEAST_ONE_PROFILE
+#define AT_LEAST_ONE_PROFILE 1
+#define PROFILE_EMITTER_GLSL(op) emit_GLSL_##op,
+
+#define EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(op) \
+    static void emit_GLSL_##op(Context *ctx) { \
+        fail(ctx, #op " unimplemented in glsl profile"); \
+    }
+
+static inline const char *get_GLSL_register_string(Context *ctx,
+                        const RegisterType regtype, const int regnum,
+                        char *regnum_str, const size_t regnum_size)
+{
+    // turns out these are identical at the moment.
+    return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
+} // get_GLSL_register_string
+
+static const char *get_GLSL_uniform_type(Context *ctx, const RegisterType rtype)
+{
+    switch (rtype)
+    {
+        case REG_TYPE_CONST: return "vec4";
+        case REG_TYPE_CONSTINT: return "ivec4";
+        case REG_TYPE_CONSTBOOL: return "bool";
+        default: fail(ctx, "BUG: used a uniform we don't know how to define.");
+    } // switch
+
+    return NULL;
+} // get_GLSL_uniform_type
+
+static const char *get_GLSL_varname_in_buf(Context *ctx, RegisterType rt,
+                                           int regnum, char *buf,
+                                           const size_t len)
+{
+    char regnum_str[16];
+    const char *regtype_str = get_GLSL_register_string(ctx, rt, regnum,
+                                              regnum_str, sizeof (regnum_str));
+    snprintf(buf,len,"%s_%s%s", ctx->shader_type_str, regtype_str, regnum_str);
+    return buf;
+} // get_GLSL_varname_in_buf
+
+
+static const char *get_GLSL_varname(Context *ctx, RegisterType rt, int regnum)
+{
+    char buf[64];
+    get_GLSL_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf));
+    return StrDup(ctx, buf);
+} // get_GLSL_varname
+
+
+static inline const char *get_GLSL_const_array_varname_in_buf(Context *ctx,
+                                                const int base, const int size,
+                                                char *buf, const size_t buflen)
+{
+    const char *type = ctx->shader_type_str;
+    snprintf(buf, buflen, "%s_const_array_%d_%d", type, base, size);
+    return buf;
+} // get_GLSL_const_array_varname_in_buf
+
+static const char *get_GLSL_const_array_varname(Context *ctx, int base, int size)
+{
+    char buf[64];
+    get_GLSL_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
+    return StrDup(ctx, buf);
+} // get_GLSL_const_array_varname
+
+
+static inline const char *get_GLSL_input_array_varname(Context *ctx,
+                                                char *buf, const size_t buflen)
+{
+    snprintf(buf, buflen, "%s", "vertex_input_array");
+    return buf;
+} // get_GLSL_input_array_varname
+
+
+static const char *get_GLSL_uniform_array_varname(Context *ctx,
+                                                  const RegisterType regtype,
+                                                  char *buf, const size_t len)
+{
+    const char *shadertype = ctx->shader_type_str;
+    const char *type = get_GLSL_uniform_type(ctx, regtype);
+    snprintf(buf, len, "%s_uniforms_%s", shadertype, type);
+    return buf;
+} // get_GLSL_uniform_array_varname
+
+static const char *get_GLSL_destarg_varname(Context *ctx, char *buf, size_t len)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+    return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
+} // get_GLSL_destarg_varname
+
+static const char *get_GLSL_srcarg_varname(Context *ctx, const size_t idx,
+                                           char *buf, size_t len)
+{
+    if (idx >= STATICARRAYLEN(ctx->source_args))
+    {
+        fail(ctx, "Too many source args");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const SourceArgInfo *arg = &ctx->source_args[idx];
+    return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
+} // get_GLSL_srcarg_varname
+
+
+static const char *make_GLSL_destarg_assign(Context *, char *, const size_t,
+                                            const char *, ...) ISPRINTF(4,5);
+
+static const char *make_GLSL_destarg_assign(Context *ctx, char *buf,
+                                            const size_t buflen,
+                                            const char *fmt, ...)
+{
+    int need_parens = 0;
+    const DestArgInfo *arg = &ctx->dest_arg;
+
+    if (arg->writemask == 0)
+    {
+        *buf = '\0';
+        return buf;  // no writemask? It's a no-op.
+    } // if
+
+    char clampbuf[32] = { '\0' };
+    const char *clampleft = "";
+    const char *clampright = "";
+    if (arg->result_mod & MOD_SATURATE)
+    {
+        const int vecsize = vecsize_from_writemask(arg->writemask);
+        clampleft = "clamp(";
+        if (vecsize == 1)
+            clampright = ", 0.0, 1.0)";
+        else
+        {
+            snprintf(clampbuf, sizeof (clampbuf),
+                     ", vec%d(0.0), vec%d(1.0))", vecsize, vecsize);
+            clampright = clampbuf;
+        } // else
+    } // if
+
+    // MSDN says MOD_PP is a hint and many implementations ignore it. So do we.
+
+    // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
+    assert((arg->result_mod & MOD_CENTROID) == 0);
+
+    if (ctx->predicated)
+    {
+        fail(ctx, "predicated destinations unsupported");  // !!! FIXME
+        *buf = '\0';
+        return buf;
+    } // if
+
+    char operation[256];
+    va_list ap;
+    va_start(ap, fmt);
+    const int len = vsnprintf(operation, sizeof (operation), fmt, ap);
+    va_end(ap);
+    if (len >= sizeof (operation))
+    {
+        fail(ctx, "operation string too large");  // I'm lazy.  :P
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const char *result_shift_str = "";
+    switch (arg->result_shift)
+    {
+        case 0x1: result_shift_str = " * 2.0"; break;
+        case 0x2: result_shift_str = " * 4.0"; break;
+        case 0x3: result_shift_str = " * 8.0"; break;
+        case 0xD: result_shift_str = " / 8.0"; break;
+        case 0xE: result_shift_str = " / 4.0"; break;
+        case 0xF: result_shift_str = " / 2.0"; break;
+    } // switch
+    need_parens |= (result_shift_str[0] != '\0');
+
+    char regnum_str[16];
+    const char *regtype_str = get_GLSL_register_string(ctx, arg->regtype,
+                                                       arg->regnum, regnum_str,
+                                                       sizeof (regnum_str));
+    char writemask_str[6];
+    size_t i = 0;
+    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
+    if (!scalar && !writemask_xyzw(arg->writemask))
+    {
+        writemask_str[i++] = '.';
+        if (arg->writemask0) writemask_str[i++] = 'x';
+        if (arg->writemask1) writemask_str[i++] = 'y';
+        if (arg->writemask2) writemask_str[i++] = 'z';
+        if (arg->writemask3) writemask_str[i++] = 'w';
+    } // if
+    writemask_str[i] = '\0';
+    assert(i < sizeof (writemask_str));
+
+    const char *leftparen = (need_parens) ? "(" : "";
+    const char *rightparen = (need_parens) ? ")" : "";
+
+    snprintf(buf, buflen, "%s_%s%s%s = %s%s%s%s%s%s;",
+             ctx->shader_type_str, regtype_str, regnum_str, writemask_str,
+             clampleft, leftparen, operation, rightparen, result_shift_str,
+             clampright);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_GLSL_destarg_assign
+
+
+static char *make_GLSL_swizzle_string(char *swiz_str, const size_t strsize,
+                                      const int swizzle, const int writemask)
+{
+    size_t i = 0;
+    if ( (!no_swizzle(swizzle)) || (!writemask_xyzw(writemask)) )
+    {
+        const int writemask0 = (writemask >> 0) & 0x1;
+        const int writemask1 = (writemask >> 1) & 0x1;
+        const int writemask2 = (writemask >> 2) & 0x1;
+        const int writemask3 = (writemask >> 3) & 0x1;
+
+        const int swizzle_x = (swizzle >> 0) & 0x3;
+        const int swizzle_y = (swizzle >> 2) & 0x3;
+        const int swizzle_z = (swizzle >> 4) & 0x3;
+        const int swizzle_w = (swizzle >> 6) & 0x3;
+
+        swiz_str[i++] = '.';
+        if (writemask0) swiz_str[i++] = swizzle_channels[swizzle_x];
+        if (writemask1) swiz_str[i++] = swizzle_channels[swizzle_y];
+        if (writemask2) swiz_str[i++] = swizzle_channels[swizzle_z];
+        if (writemask3) swiz_str[i++] = swizzle_channels[swizzle_w];
+    } // if
+    assert(i < strsize);
+    swiz_str[i] = '\0';
+    return swiz_str;
+} // make_GLSL_swizzle_string
+
+
+static const char *make_GLSL_srcarg_string(Context *ctx, const size_t idx,
+                                           const int writemask, char *buf,
+                                           const size_t buflen)
+{
+    *buf = '\0';
+
+    if (idx >= STATICARRAYLEN(ctx->source_args))
+    {
+        fail(ctx, "Too many source args");
+        return buf;
+    } // if
+
+    const SourceArgInfo *arg = &ctx->source_args[idx];
+
+    const char *premod_str = "";
+    const char *postmod_str = "";
+    switch (arg->src_mod)
+    {
+        case SRCMOD_NEGATE:
+            premod_str = "-";
+            break;
+
+        case SRCMOD_BIASNEGATE:
+            premod_str = "-(";
+            postmod_str = " - 0.5)";
+            break;
+
+        case SRCMOD_BIAS:
+            premod_str = "(";
+            postmod_str = " - 0.5)";
+            break;
+
+        case SRCMOD_SIGNNEGATE:
+            premod_str = "-((";
+            postmod_str = " - 0.5) * 2.0)";
+            break;
+
+        case SRCMOD_SIGN:
+            premod_str = "((";
+            postmod_str = " - 0.5) * 2.0)";
+            break;
+
+        case SRCMOD_COMPLEMENT:
+            premod_str = "(1.0 - ";
+            postmod_str = ")";
+            break;
+
+        case SRCMOD_X2NEGATE:
+            premod_str = "-(";
+            postmod_str = " * 2.0)";
+            break;
+
+        case SRCMOD_X2:
+            premod_str = "(";
+            postmod_str = " * 2.0)";
+            break;
+
+        case SRCMOD_DZ:
+            fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME
+            postmod_str = "_dz";
+            break;
+
+        case SRCMOD_DW:
+            fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME
+            postmod_str = "_dw";
+            break;
+
+        case SRCMOD_ABSNEGATE:
+            premod_str = "-abs(";
+            postmod_str = ")";
+            break;
+
+        case SRCMOD_ABS:
+            premod_str = "abs(";
+            postmod_str = ")";
+            break;
+
+        case SRCMOD_NOT:
+            premod_str = "!";
+            break;
+
+        case SRCMOD_NONE:
+        case SRCMOD_TOTAL:
+             break;  // stop compiler whining.
+    } // switch
+
+    const char *regtype_str = NULL;
+
+    if (!arg->relative)
+    {
+        regtype_str = get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum,
+                                              (char *) alloca(64), 64);
+    } // if
+
+    const char *rel_lbracket = "";
+    char rel_offset[32] = { '\0' };
+    const char *rel_rbracket = "";
+    char rel_swizzle[4] = { '\0' };
+    const char *rel_regtype_str = "";
+    if (arg->relative)
+    {
+        if (arg->regtype == REG_TYPE_INPUT)
+            regtype_str=get_GLSL_input_array_varname(ctx,(char*)alloca(64),64);
+        else
+        {
+            assert(arg->regtype == REG_TYPE_CONST);
+            const int arrayidx = arg->relative_array->index;
+            const int offset = arg->regnum - arrayidx;
+            assert(offset >= 0);
+            if (arg->relative_array->constant)
+            {
+                const int arraysize = arg->relative_array->count;
+                regtype_str = get_GLSL_const_array_varname_in_buf(ctx,
+                                arrayidx, arraysize, (char *) alloca(64), 64);
+                if (offset != 0)
+                    snprintf(rel_offset, sizeof (rel_offset), "%d + ", offset);
+            } // if
+            else
+            {
+                regtype_str = get_GLSL_uniform_array_varname(ctx, arg->regtype,
+                                                      (char *) alloca(64), 64);
+                if (offset == 0)
+                {
+                    snprintf(rel_offset, sizeof (rel_offset),
+                             "ARRAYBASE_%d + ", arrayidx);
+                } // if
+                else
+                {
+                    snprintf(rel_offset, sizeof (rel_offset),
+                             "(ARRAYBASE_%d + %d) + ", arrayidx, offset);
+                } // else
+            } // else
+        } // else
+
+        rel_lbracket = "[";
+
+        rel_regtype_str = get_GLSL_varname_in_buf(ctx, arg->relative_regtype,
+                                                  arg->relative_regnum,
+                                                  (char *) alloca(64), 64);
+        rel_swizzle[0] = '.';
+        rel_swizzle[1] = swizzle_channels[arg->relative_component];
+        rel_swizzle[2] = '\0';
+        rel_rbracket = "]";
+    } // if
+
+    char swiz_str[6] = { '\0' };
+    if (!isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum))
+    {
+        make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
+                                 arg->swizzle, writemask);
+    } // if
+
+    if (regtype_str == NULL)
+    {
+        fail(ctx, "Unknown source register type.");
+        return buf;
+    } // if
+
+    snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s",
+             premod_str, regtype_str, rel_lbracket, rel_offset,
+             rel_regtype_str, rel_swizzle, rel_rbracket, swiz_str,
+             postmod_str);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_GLSL_srcarg_string
+
+// generate some convenience functions.
+#define MAKE_GLSL_SRCARG_STRING_(mask, bitmask) \
+    static inline const char *make_GLSL_srcarg_string_##mask(Context *ctx, \
+                                                const size_t idx, char *buf, \
+                                                const size_t buflen) { \
+        return make_GLSL_srcarg_string(ctx, idx, bitmask, buf, buflen); \
+    }
+MAKE_GLSL_SRCARG_STRING_(x, (1 << 0))
+MAKE_GLSL_SRCARG_STRING_(y, (1 << 1))
+MAKE_GLSL_SRCARG_STRING_(z, (1 << 2))
+MAKE_GLSL_SRCARG_STRING_(w, (1 << 3))
+MAKE_GLSL_SRCARG_STRING_(scalar, (1 << 0))
+MAKE_GLSL_SRCARG_STRING_(full, 0xF)
+MAKE_GLSL_SRCARG_STRING_(masked, ctx->dest_arg.writemask)
+MAKE_GLSL_SRCARG_STRING_(vec3, 0x7)
+MAKE_GLSL_SRCARG_STRING_(vec2, 0x3)
+#undef MAKE_GLSL_SRCARG_STRING_
+
+// special cases for comparison opcodes...
+
+static const char *get_GLSL_comparison_string_scalar(Context *ctx)
+{
+    static const char *comps[] = { "", ">", "==", ">=", "<", "!=", "<=" };
+    if (ctx->instruction_controls >= STATICARRAYLEN(comps))
+    {
+        fail(ctx, "unknown comparison control");
+        return "";
+    } // if
+
+    return comps[ctx->instruction_controls];
+} // get_GLSL_comparison_string_scalar
+
+static const char *get_GLSL_comparison_string_vector(Context *ctx)
+{
+    static const char *comps[] = {
+        "", "greaterThan", "equal", "greaterThanEqual", "lessThan",
+        "notEqual", "lessThanEqual"
+    };
+
+    if (ctx->instruction_controls >= STATICARRAYLEN(comps))
+    {
+        fail(ctx, "unknown comparison control");
+        return "";
+    } // if
+
+    return comps[ctx->instruction_controls];
+} // get_GLSL_comparison_string_vector
+
+
+static void emit_GLSL_start(Context *ctx, const char *profilestr)
+{
+    if (!shader_is_vertex(ctx) && !shader_is_pixel(ctx))
+    {
+        failf(ctx, "Shader type %u unsupported in this profile.",
+              (uint) ctx->shader_type);
+        return;
+    } // if
+
+    else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL) == 0)
+    {
+        // No gl_FragData[] before GLSL 1.10, so we have to force the version.
+        push_output(ctx, &ctx->preflight);
+        output_line(ctx, "#version 110");
+        pop_output(ctx);
+    } // else if
+
+    #if SUPPORT_PROFILE_GLSL120
+    else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL120) == 0)
+    {
+        ctx->profile_supports_glsl120 = 1;
+        push_output(ctx, &ctx->preflight);
+        output_line(ctx, "#version 120");
+        pop_output(ctx);
+    } // else if
+    #endif
+
+    else
+    {
+        failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
+        return;
+    } // else
+
+    push_output(ctx, &ctx->mainline_intro);
+    output_line(ctx, "void main()");
+    output_line(ctx, "{");
+    pop_output(ctx);
+
+    set_output(ctx, &ctx->mainline);
+    ctx->indent++;
+} // emit_GLSL_start
+
+static void emit_GLSL_RET(Context *ctx);
+static void emit_GLSL_end(Context *ctx)
+{
+    // ps_1_* writes color to r0 instead oC0. We move it to the right place.
+    // We don't have to worry about a RET opcode messing this up, since
+    //  RET isn't available before ps_2_0.
+    if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
+    {
+        const char *shstr = ctx->shader_type_str;
+        set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
+        output_line(ctx, "%s_oC0 = %s_r0;", shstr, shstr);
+    } // if
+
+    // force a RET opcode if we're at the end of the stream without one.
+    if (ctx->previous_opcode != OPCODE_RET)
+        emit_GLSL_RET(ctx);
+} // emit_GLSL_end
+
+static void emit_GLSL_phase(Context *ctx)
+{
+    // no-op in GLSL.
+} // emit_GLSL_phase
+
+static void output_GLSL_uniform_array(Context *ctx, const RegisterType regtype,
+                                      const int size)
+{
+    if (size > 0)
+    {
+        char buf[64];
+        get_GLSL_uniform_array_varname(ctx, regtype, buf, sizeof (buf));
+        output_line(ctx, "uniform vec4 %s[%d];", buf, size);
+    } // if
+} // output_GLSL_uniform_array
+
+static void emit_GLSL_finalize(Context *ctx)
+{
+    // throw some blank lines around to make source more readable.
+    push_output(ctx, &ctx->globals);
+    output_blank_line(ctx);
+    pop_output(ctx);
+
+    // If we had a relative addressing of REG_TYPE_INPUT, we need to build
+    //  an array for it at the start of main(). GLSL doesn't let you specify
+    //  arrays of attributes.
+    //vec4 blah_array[BIGGEST_ARRAY];
+    if (ctx->have_relative_input_registers) // !!! FIXME
+        fail(ctx, "Relative addressing of input registers not supported.");
+
+    push_output(ctx, &ctx->preflight);
+    output_GLSL_uniform_array(ctx, REG_TYPE_CONST, ctx->uniform_float4_count);
+    output_GLSL_uniform_array(ctx, REG_TYPE_CONSTINT, ctx->uniform_int4_count);
+    output_GLSL_uniform_array(ctx, REG_TYPE_CONSTBOOL, ctx->uniform_bool_count);
+    pop_output(ctx);
+} // emit_GLSL_finalize
+
+static void emit_GLSL_global(Context *ctx, RegisterType regtype, int regnum)
+{
+    char varname[64];
+    get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
+
+    push_output(ctx, &ctx->globals);
+    switch (regtype)
+    {
+        case REG_TYPE_ADDRESS:
+            if (shader_is_vertex(ctx))
+                output_line(ctx, "ivec4 %s;", varname);
+            else if (shader_is_pixel(ctx))  // actually REG_TYPE_TEXTURE.
+            {
+                // We have to map texture registers to temps for ps_1_1, since
+                //  they work like temps, initialize with tex coords, and the
+                //  ps_1_1 TEX opcode expects to overwrite it.
+                if (!shader_version_atleast(ctx, 1, 4))
+                {
+                    output_line(ctx, "vec4 %s = gl_TexCoord[%d];",
+                                varname, regnum);
+                } // if
+            } // else if
+            break;
+        case REG_TYPE_PREDICATE:
+            output_line(ctx, "bvec4 %s;", varname);
+            break;
+        case REG_TYPE_TEMP:
+            output_line(ctx, "vec4 %s;", varname);
+            break;
+        case REG_TYPE_LOOP:
+            break; // no-op. We declare these in for loops at the moment.
+        case REG_TYPE_LABEL:
+            break; // no-op. If we see it here, it means we optimized it out.
+        default:
+            fail(ctx, "BUG: we used a register we don't know how to define.");
+            break;
+    } // switch
+    pop_output(ctx);
+} // emit_GLSL_global
+
+static void emit_GLSL_array(Context *ctx, VariableList *var)
+{
+    // All uniforms (except constant arrays, which only get pushed once at
+    //  compile time) are now packed into a single array, so we can batch
+    //  the uniform transfers. So this is doesn't actually define an array
+    //  here; the one, big array is emitted during finalization instead.
+    // However, we need to #define the offset into the one, big array here,
+    //  and let dereferences use that #define.
+    const int base = var->index;
+    const int glslbase = ctx->uniform_float4_count;
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "#define ARRAYBASE_%d %d", base, glslbase);
+    pop_output(ctx);
+    var->emit_position = glslbase;
+} // emit_GLSL_array
+
+static void emit_GLSL_const_array(Context *ctx, const ConstantsList *clist,
+                                  int base, int size)
+{
+    char varname[64];
+    get_GLSL_const_array_varname_in_buf(ctx,base,size,varname,sizeof(varname));
+
+#if 0
+    // !!! FIXME: fails on Nvidia's and Apple's GL, even with #version 120.
+    // !!! FIXME:  (the 1.20 spec says it should work, though, I think...)
+    if (support_glsl120(ctx))
+    {
+        // GLSL 1.20 can do constant arrays.
+        const char *cstr = NULL;
+        push_output(ctx, &ctx->globals);
+        output_line(ctx, "const vec4 %s[%d] = vec4[%d](", varname, size, size);
+        ctx->indent++;
+
+        int i;
+        for (i = 0; i < size; i++)
+        {
+            while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
+                clist = clist->next;
+            assert(clist->constant.index == (base + i));
+
+            char val0[32];
+            char val1[32];
+            char val2[32];
+            char val3[32];
+            floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
+            floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
+            floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
+            floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
+
+            output_line(ctx, "vec4(%s, %s, %s, %s)%s", val0, val1, val2, val3,
+                        (i < (size-1)) ? "," : "");
+
+            clist = clist->next;
+        } // for
+
+        ctx->indent--;
+        output_line(ctx, ");");
+        pop_output(ctx);
+    } // if
+
+    else
+#endif
+    {
+        // stock GLSL 1.0 can't do constant arrays, so make a uniform array
+        //  and have the OpenGL glue assign it at link time. Lame!
+        push_output(ctx, &ctx->globals);
+        output_line(ctx, "uniform vec4 %s[%d];", varname, size);
+        pop_output(ctx);
+    } // else
+} // emit_GLSL_const_array
+
+static void emit_GLSL_uniform(Context *ctx, RegisterType regtype, int regnum,
+                              const VariableList *var)
+{
+    // Now that we're pushing all the uniforms as one big array, pack these
+    //  down, so if we only use register c439, it'll actually map to
+    //  glsl_uniforms_vec4[0]. As we push one big array, this will prevent
+    //  uploading unused data.
+
+    char varname[64];
+    char name[64];
+    int index = 0;
+
+    get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
+
+    push_output(ctx, &ctx->globals);
+
+    if (var == NULL)
+    {
+        get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
+
+        if (regtype == REG_TYPE_CONST)
+            index = ctx->uniform_float4_count;
+        else if (regtype == REG_TYPE_CONSTINT)
+            index = ctx->uniform_int4_count;
+        else if (regtype == REG_TYPE_CONSTBOOL)
+            index = ctx->uniform_bool_count;
+        else  // get_GLSL_uniform_array_varname() would have called fail().
+            assert(isfail(ctx));
+
+        output_line(ctx, "#define %s %s[%d]", varname, name, index);
+    } // if
+
+    else
+    {
+        const int arraybase = var->index;
+        if (var->constant)
+        {
+            get_GLSL_const_array_varname_in_buf(ctx, arraybase, var->count,
+                                                name, sizeof (name));
+            index = (regnum - arraybase);
+        } // if
+        else
+        {
+            assert(var->emit_position != -1);
+            get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
+            index = (regnum - arraybase) + var->emit_position;
+        } // else
+
+        output_line(ctx, "#define %s %s[%d]", varname, name, index);
+    } // else
+
+    pop_output(ctx);
+} // emit_GLSL_uniform
+
+static void emit_GLSL_sampler(Context *ctx,int stage,TextureType ttype,int tb)
+{
+    const char *type = "";
+    switch (ttype)
+    {
+        case TEXTURE_TYPE_2D: type = "sampler2D"; break;
+        case TEXTURE_TYPE_CUBE: type = "samplerCube"; break;
+        case TEXTURE_TYPE_VOLUME: type = "sampler3D"; break;
+        default: fail(ctx, "BUG: used a sampler we don't know how to define.");
+    } // switch
+
+    char var[64];
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof (var));
+
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "uniform %s %s;", type, var);
+    if (tb)  // This sampler used a ps_1_1 TEXBEM opcode?
+    {
+        char name[64];
+        const int index = ctx->uniform_float4_count;
+        ctx->uniform_float4_count += 2;
+        get_GLSL_uniform_array_varname(ctx, REG_TYPE_CONST, name, sizeof (name));
+        output_line(ctx, "#define %s_texbem %s[%d]", var, name, index);
+        output_line(ctx, "#define %s_texbeml %s[%d]", var, name, index+1);
+    } // if
+    pop_output(ctx);
+} // emit_GLSL_sampler
+
+static void emit_GLSL_attribute(Context *ctx, RegisterType regtype, int regnum,
+                                MOJOSHADER_usage usage, int index, int wmask,
+                                int flags)
+{
+    // !!! FIXME: this function doesn't deal with write masks at all yet!
+    const char *usage_str = NULL;
+    const char *arrayleft = "";
+    const char *arrayright = "";
+    char index_str[16] = { '\0' };
+    char var[64];
+
+    get_GLSL_varname_in_buf(ctx, regtype, regnum, var, sizeof (var));
+
+    //assert((flags & MOD_PP) == 0);  // !!! FIXME: is PP allowed?
+
+    if (index != 0)  // !!! FIXME: a lot of these MUST be zero.
+        snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+
+    if (shader_is_vertex(ctx))
+    {
+        // pre-vs3 output registers.
+        // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
+        //  output registers.
+        if (!shader_version_atleast(ctx, 3, 0))
+        {
+            if (regtype == REG_TYPE_RASTOUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                index = regnum;
+                switch ((const RastOutType) regnum)
+                {
+                    case RASTOUT_TYPE_POSITION:
+                        usage = MOJOSHADER_USAGE_POSITION;
+                        break;
+                    case RASTOUT_TYPE_FOG:
+                        usage = MOJOSHADER_USAGE_FOG;
+                        break;
+                    case RASTOUT_TYPE_POINT_SIZE:
+                        usage = MOJOSHADER_USAGE_POINTSIZE;
+                        break;
+                } // switch
+            } // if
+
+            else if (regtype == REG_TYPE_ATTROUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                usage = MOJOSHADER_USAGE_COLOR;
+                index = regnum;
+            } // else if
+
+            else if (regtype == REG_TYPE_TEXCRDOUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                usage = MOJOSHADER_USAGE_TEXCOORD;
+                index = regnum;
+            } // else if
+        } // if
+
+        // to avoid limitations of various GL entry points for input
+        // attributes (glSecondaryColorPointer() can only take 3 component
+        // items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
+        // issues), we set up all inputs as generic vertex attributes, so we
+        // can pass data in just about any form, and ignore the built-in GLSL
+        // attributes like gl_SecondaryColor. Output needs to use the the
+        // built-ins, though, but we don't have to worry about the GL entry
+        // point limitations there.
+
+        if (regtype == REG_TYPE_INPUT)
+        {
+            push_output(ctx, &ctx->globals);
+            output_line(ctx, "attribute vec4 %s;", var);
+            pop_output(ctx);
+        } // if
+
+        else if (regtype == REG_TYPE_OUTPUT)
+        {
+            switch (usage)
+            {
+                case MOJOSHADER_USAGE_POSITION:
+                    usage_str = "gl_Position";
+                    break;
+                case MOJOSHADER_USAGE_POINTSIZE:
+                    usage_str = "gl_PointSize";
+                    break;
+                case MOJOSHADER_USAGE_COLOR:
+                    index_str[0] = '\0';  // no explicit number.
+                    if (index == 0)
+                        usage_str = "gl_FrontColor";
+                    else if (index == 1)
+                        usage_str = "gl_FrontSecondaryColor";
+                    break;
+                case MOJOSHADER_USAGE_FOG:
+                    usage_str = "gl_FogFragCoord";
+                    break;
+                case MOJOSHADER_USAGE_TEXCOORD:
+                    snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+                    usage_str = "gl_TexCoord";
+                    arrayleft = "[";
+                    arrayright = "]";
+                    break;
+                default:
+                    // !!! FIXME: we need to deal with some more built-in varyings here.
+                    break;
+            } // switch
+
+            // !!! FIXME: the #define is a little hacky, but it means we don't
+            // !!! FIXME:  have to track these separately if this works.
+            push_output(ctx, &ctx->globals);
+            // no mapping to built-in var? Just make it a regular global, pray.
+            if (usage_str == NULL)
+                output_line(ctx, "vec4 %s;", var);
+            else
+            {
+                output_line(ctx, "#define %s %s%s%s%s", var, usage_str,
+                            arrayleft, index_str, arrayright);
+            } // else
+            pop_output(ctx);
+        } // else if
+
+        else
+        {
+            fail(ctx, "unknown vertex shader attribute register");
+        } // else
+    } // if
+
+    else if (shader_is_pixel(ctx))
+    {
+        // samplers DCLs get handled in emit_GLSL_sampler().
+
+        if (flags & MOD_CENTROID)  // !!! FIXME
+        {
+            failf(ctx, "centroid unsupported in %s profile", ctx->profile->name);
+            return;
+        } // if
+
+        if (regtype == REG_TYPE_COLOROUT)
+        {
+            if (!ctx->have_multi_color_outputs)
+                usage_str = "gl_FragColor";  // maybe faster?
+            else
+            {
+                snprintf(index_str, sizeof (index_str), "%u", (uint) regnum);
+                usage_str = "gl_FragData";
+                arrayleft = "[";
+                arrayright = "]";
+            } // else
+        } // if
+
+        else if (regtype == REG_TYPE_DEPTHOUT)
+            usage_str = "gl_FragDepth";
+
+        // !!! FIXME: can you actualy have a texture register with COLOR usage?
+        else if ((regtype == REG_TYPE_TEXTURE) || (regtype == REG_TYPE_INPUT))
+        {
+            if (usage == MOJOSHADER_USAGE_TEXCOORD)
+            {
+                // ps_1_1 does a different hack for this attribute.
+                //  Refer to emit_GLSL_global()'s REG_TYPE_TEXTURE code.
+                if (shader_version_atleast(ctx, 1, 4))
+                {
+                    snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+                    usage_str = "gl_TexCoord";
+                    arrayleft = "[";
+                    arrayright = "]";
+                } // if
+            } // if
+
+            else if (usage == MOJOSHADER_USAGE_COLOR)
+            {
+                index_str[0] = '\0';  // no explicit number.
+                if (index == 0)
+                    usage_str = "gl_Color";
+                else if (index == 1)
+                    usage_str = "gl_SecondaryColor";
+                else
+                    fail(ctx, "unsupported color index");
+            } // else if
+        } // else if
+
+        else if (regtype == REG_TYPE_MISCTYPE)
+        {
+            const MiscTypeType mt = (MiscTypeType) regnum;
+            if (mt == MISCTYPE_TYPE_FACE)
+            {
+                push_output(ctx, &ctx->globals);
+                output_line(ctx, "float %s = gl_FrontFacing ? 1.0 : -1.0;", var);
+                pop_output(ctx);
+            } // if
+            else if (mt == MISCTYPE_TYPE_POSITION)
+            {
+                index_str[0] = '\0';  // no explicit number.
+                usage_str = "gl_FragCoord";  // !!! FIXME: is this the same coord space as D3D?
+            } // else if
+            else
+            {
+                fail(ctx, "BUG: unhandled misc register");
+            } // else
+        } // else if
+
+        else
+        {
+            fail(ctx, "unknown pixel shader attribute register");
+        } // else
+
+        if (usage_str != NULL)
+        {
+            push_output(ctx, &ctx->globals);
+            output_line(ctx, "#define %s %s%s%s%s", var, usage_str,
+                        arrayleft, index_str, arrayright);
+            pop_output(ctx);
+        } // if
+    } // else if
+
+    else
+    {
+        fail(ctx, "Unknown shader type");  // state machine should catch this.
+    } // else
+} // emit_GLSL_attribute
+
+static void emit_GLSL_NOP(Context *ctx)
+{
+    // no-op is a no-op.  :)
+} // emit_GLSL_NOP
+
+static void emit_GLSL_MOV(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MOV
+
+static void emit_GLSL_ADD(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s + %s", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_ADD
+
+static void emit_GLSL_SUB(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s - %s", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SUB
+
+static void emit_GLSL_MAD(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_GLSL_srcarg_string_masked(ctx, 2, src2, sizeof (src2));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "(%s * %s) + %s", src0, src1, src2);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MAD
+
+static void emit_GLSL_MUL(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s * %s", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MUL
+
+static void emit_GLSL_RCP(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "1.0 / %s", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_RCP
+
+static void emit_GLSL_RSQ(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "inversesqrt(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_RSQ
+
+static void emit_GLSL_dotprod(Context *ctx, const char *src0, const char *src1,
+                              const char *extra)
+{
+    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
+    char castleft[16] = { '\0' };
+    const char *castright = "";
+    if (vecsize != 1)
+    {
+        snprintf(castleft, sizeof (castleft), "vec%d(", vecsize);
+        castright = ")";
+    } // if
+
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "%sdot(%s, %s)%s%s",
+                             castleft, src0, src1, extra, castright);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_dotprod
+
+static void emit_GLSL_DP3(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1));
+    emit_GLSL_dotprod(ctx, src0, src1, "");
+} // emit_GLSL_DP3
+
+static void emit_GLSL_DP4(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_full(ctx, 1, src1, sizeof (src1));
+    emit_GLSL_dotprod(ctx, src0, src1, "");
+} // emit_GLSL_DP4
+
+static void emit_GLSL_MIN(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "min(%s, %s)", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MIN
+
+static void emit_GLSL_MAX(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "max(%s, %s)", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MAX
+
+static void emit_GLSL_SLT(Context *ctx)
+{
+    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+
+    // float(bool) or vec(bvec) results in 0.0 or 1.0, like SLT wants.
+    if (vecsize == 1)
+        make_GLSL_destarg_assign(ctx, code, sizeof (code), "float(%s < %s)", src0, src1);
+    else
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "vec%d(lessThan(%s, %s))",
+                                 vecsize, src0, src1);
+    } // else
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SLT
+
+static void emit_GLSL_SGE(Context *ctx)
+{
+    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+
+    // float(bool) or vec(bvec) results in 0.0 or 1.0, like SGE wants.
+    if (vecsize == 1)
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "float(%s >= %s)", src0, src1);
+    } // if
+    else
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "vec%d(greaterThanEqual(%s, %s))",
+                                 vecsize, src0, src1);
+    } // else
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SGE
+
+static void emit_GLSL_EXP(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "exp2(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_EXP
+
+static void emit_GLSL_LOG(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "log2(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_LOG
+
+static void emit_GLSL_LIT_helper(Context *ctx)
+{
+    const char *maxp = "127.9961"; // value from the dx9 reference.
+
+    if (ctx->glsl_generated_lit_helper)
+        return;
+
+    ctx->glsl_generated_lit_helper = 1;
+
+    push_output(ctx, &ctx->helpers);
+    output_line(ctx, "vec4 LIT(const vec4 src)");
+    output_line(ctx, "{"); ctx->indent++;
+    output_line(ctx,   "float power = clamp(src.w, -%s, %s);",maxp,maxp);
+    output_line(ctx,   "vec4 retval = vec4(1.0, 0.0, 0.0, 1.0);");
+    output_line(ctx,   "if (src.x > 0.0) {"); ctx->indent++;
+    output_line(ctx,     "retval.y = src.x;");
+    output_line(ctx,     "if (src.y > 0.0) {"); ctx->indent++;
+    output_line(ctx,       "retval.z = pow(src.y, power);"); ctx->indent--;
+    output_line(ctx,     "}"); ctx->indent--;
+    output_line(ctx,   "}");
+    output_line(ctx,   "return retval;"); ctx->indent--;
+    output_line(ctx, "}");
+    output_blank_line(ctx);
+    pop_output(ctx);
+} // emit_GLSL_LIT_helper
+
+static void emit_GLSL_LIT(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    emit_GLSL_LIT_helper(ctx);
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "LIT(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_LIT
+
+static void emit_GLSL_DST(Context *ctx)
+{
+    // !!! FIXME: needs to take ctx->dst_arg.writemask into account.
+    char src0_y[64]; make_GLSL_srcarg_string_y(ctx, 0, src0_y, sizeof (src0_y));
+    char src1_y[64]; make_GLSL_srcarg_string_y(ctx, 1, src1_y, sizeof (src1_y));
+    char src0_z[64]; make_GLSL_srcarg_string_z(ctx, 0, src0_z, sizeof (src0_z));
+    char src1_w[64]; make_GLSL_srcarg_string_w(ctx, 1, src1_w, sizeof (src1_w));
+
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                             "vec4(1.0, %s * %s, %s, %s)",
+                             src0_y, src1_y, src0_z, src1_w);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_DST
+
+static void emit_GLSL_LRP(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_GLSL_srcarg_string_masked(ctx, 2, src2, sizeof (src2));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "mix(%s, %s, %s)",
+                             src2, src1, src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_LRP
+
+static void emit_GLSL_FRC(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "fract(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_FRC
+
+static void emit_GLSL_M4X4(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+    char row0[64]; make_GLSL_srcarg_string_full(ctx, 1, row0, sizeof (row0));
+    char row1[64]; make_GLSL_srcarg_string_full(ctx, 2, row1, sizeof (row1));
+    char row2[64]; make_GLSL_srcarg_string_full(ctx, 3, row2, sizeof (row2));
+    char row3[64]; make_GLSL_srcarg_string_full(ctx, 4, row3, sizeof (row3));
+    char code[256];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                    "vec4(dot(%s, %s), dot(%s, %s), dot(%s, %s), dot(%s, %s))",
+                    src0, row0, src0, row1, src0, row2, src0, row3);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_M4X4
+
+static void emit_GLSL_M4X3(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+    char row0[64]; make_GLSL_srcarg_string_full(ctx, 1, row0, sizeof (row0));
+    char row1[64]; make_GLSL_srcarg_string_full(ctx, 2, row1, sizeof (row1));
+    char row2[64]; make_GLSL_srcarg_string_full(ctx, 3, row2, sizeof (row2));
+    char code[256];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                "vec3(dot(%s, %s), dot(%s, %s), dot(%s, %s))",
+                                src0, row0, src0, row1, src0, row2);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_M4X3
+
+static void emit_GLSL_M3X4(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+    char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
+    char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
+    char row2[64]; make_GLSL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2));
+    char row3[64]; make_GLSL_srcarg_string_vec3(ctx, 4, row3, sizeof (row3));
+
+    char code[256];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                "vec4(dot(%s, %s), dot(%s, %s), "
+                                     "dot(%s, %s), dot(%s, %s))",
+                                src0, row0, src0, row1,
+                                src0, row2, src0, row3);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_M3X4
+
+static void emit_GLSL_M3X3(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+    char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
+    char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
+    char row2[64]; make_GLSL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2));
+    char code[256];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                "vec3(dot(%s, %s), dot(%s, %s), dot(%s, %s))",
+                                src0, row0, src0, row1, src0, row2);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_M3X3
+
+static void emit_GLSL_M3X2(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+    char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
+    char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
+
+    char code[256];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                "vec2(dot(%s, %s), dot(%s, %s))",
+                                src0, row0, src0, row1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_M3X2
+
+static void emit_GLSL_CALL(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    if (ctx->loops > 0)
+        output_line(ctx, "%s(aL);", src0);
+    else
+        output_line(ctx, "%s();", src0);
+} // emit_GLSL_CALL
+
+static void emit_GLSL_CALLNZ(Context *ctx)
+{
+    // !!! FIXME: if src1 is a constbool that's true, we can remove the
+    // !!! FIXME:  if. If it's false, we can make this a no-op.
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+
+    if (ctx->loops > 0)
+        output_line(ctx, "if (%s) { %s(aL); }", src1, src0);
+    else
+        output_line(ctx, "if (%s) { %s(); }", src1, src0);
+} // emit_GLSL_CALLNZ
+
+static void emit_GLSL_LOOP(Context *ctx)
+{
+    // !!! FIXME: swizzle?
+    char var[64]; get_GLSL_srcarg_varname(ctx, 1, var, sizeof (var));
+    assert(ctx->source_args[0].regnum == 0);  // in case they add aL1 someday.
+    output_line(ctx, "{");
+    ctx->indent++;
+    output_line(ctx, "const int aLend = %s.x + %s.y;", var, var);
+    output_line(ctx, "for (int aL = %s.y; aL < aLend; aL += %s.z) {", var, var);
+    ctx->indent++;
+} // emit_GLSL_LOOP
+
+static void emit_GLSL_RET(Context *ctx)
+{
+    // thankfully, the MSDN specs say a RET _has_ to end a function...no
+    //  early returns. So if you hit one, you know you can safely close
+    //  a high-level function.
+    ctx->indent--;
+    output_line(ctx, "}");
+    output_blank_line(ctx);
+    set_output(ctx, &ctx->subroutines);
+} // emit_GLSL_RET
+
+static void emit_GLSL_ENDLOOP(Context *ctx)
+{
+    ctx->indent--;
+    output_line(ctx, "}");
+    ctx->indent--;
+    output_line(ctx, "}");
+} // emit_GLSL_ENDLOOP
+
+static void emit_GLSL_LABEL(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    const int label = ctx->source_args[0].regnum;
+    RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, label);
+    assert(ctx->output == ctx->subroutines);  // not mainline, etc.
+    assert(ctx->indent == 0);  // we shouldn't be in the middle of a function.
+
+    // MSDN specs say CALL* has to come before the LABEL, so we know if we
+    //  can ditch the entire function here as unused.
+    if (reg == NULL)
+        set_output(ctx, &ctx->ignore);  // Func not used. Parse, but don't output.
+
+    // !!! FIXME: it would be nice if we could determine if a function is
+    // !!! FIXME:  only called once and, if so, forcibly inline it.
+
+    const char *uses_loopreg = ((reg) && (reg->misc == 1)) ? "int aL" : "";
+    output_line(ctx, "void %s(%s)", src0, uses_loopreg);
+    output_line(ctx, "{");
+    ctx->indent++;
+} // emit_GLSL_LABEL
+
+static void emit_GLSL_DCL(Context *ctx)
+{
+    // no-op. We do this in our emit_attribute() and emit_uniform().
+} // emit_GLSL_DCL
+
+static void emit_GLSL_POW(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                             "pow(abs(%s), %s)", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_POW
+
+static void emit_GLSL_CRS(Context *ctx)
+{
+    // !!! FIXME: needs to take ctx->dst_arg.writemask into account.
+    char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                             "cross(%s, %s)", src0, src1);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_CRS
+
+static void emit_GLSL_SGN(Context *ctx)
+{
+    // (we don't need the temporary registers specified for the D3D opcode.)
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "sign(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SGN
+
+static void emit_GLSL_ABS(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "abs(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_ABS
+
+static void emit_GLSL_NRM(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "normalize(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_NRM
+
+static void emit_GLSL_SINCOS(Context *ctx)
+{
+    // we don't care about the temp registers that <= sm2 demands; ignore them.
+    //  sm2 also talks about what components are left untouched vs. undefined,
+    //  but we just leave those all untouched with GLSL write masks (which
+    //  would fulfill the "undefined" requirement, too).
+    const int mask = ctx->dest_arg.writemask;
+    char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
+    char code[128] = { '\0' };
+
+    if (writemask_x(mask))
+        make_GLSL_destarg_assign(ctx, code, sizeof (code), "cos(%s)", src0);
+    else if (writemask_y(mask))
+        make_GLSL_destarg_assign(ctx, code, sizeof (code), "sin(%s)", src0);
+    else if (writemask_xy(mask))
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "vec2(cos(%s), sin(%s))", src0, src0);
+    } // else if
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SINCOS
+
+static void emit_GLSL_REP(Context *ctx)
+{
+    // !!! FIXME:
+    // msdn docs say legal loop values are 0 to 255. We can check DEFI values
+    //  at parse time, but if they are pulling a value from a uniform, do
+    //  we clamp here?
+    // !!! FIXME: swizzle is legal here, right?
+    char src0[64]; make_GLSL_srcarg_string_x(ctx, 0, src0, sizeof (src0));
+    const uint rep = (uint) ctx->reps;
+    output_line(ctx, "for (int rep%u = 0; rep%u < %s; rep%u++) {",
+                rep, rep, src0, rep);
+    ctx->indent++;
+} // emit_GLSL_REP
+
+static void emit_GLSL_ENDREP(Context *ctx)
+{
+    ctx->indent--;
+    output_line(ctx, "}");
+} // emit_GLSL_ENDREP
+
+static void emit_GLSL_IF(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
+    output_line(ctx, "if (%s) {", src0);
+    ctx->indent++;
+} // emit_GLSL_IF
+
+static void emit_GLSL_IFC(Context *ctx)
+{
+    const char *comp = get_GLSL_comparison_string_scalar(ctx);
+    char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1));
+    output_line(ctx, "if (%s %s %s) {", src0, comp, src1);
+    ctx->indent++;
+} // emit_GLSL_IFC
+
+static void emit_GLSL_ELSE(Context *ctx)
+{
+    ctx->indent--;
+    output_line(ctx, "} else {");
+    ctx->indent++;
+} // emit_GLSL_ELSE
+
+static void emit_GLSL_ENDIF(Context *ctx)
+{
+    ctx->indent--;
+    output_line(ctx, "}");
+} // emit_GLSL_ENDIF
+
+static void emit_GLSL_BREAK(Context *ctx)
+{
+    output_line(ctx, "break;");
+} // emit_GLSL_BREAK
+
+static void emit_GLSL_BREAKC(Context *ctx)
+{
+    const char *comp = get_GLSL_comparison_string_scalar(ctx);
+    char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1));
+    output_line(ctx, "if (%s %s %s) { break; }", src0, comp, src1);
+} // emit_GLSL_BREAKC
+
+static void emit_GLSL_MOVA(Context *ctx)
+{
+    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+
+    if (vecsize == 1)
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "int(floor(abs(%s) + 0.5) * sign(%s))",
+                                 src0, src0);
+    } // if
+
+    else
+    {
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                            "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s))",
+                            vecsize, src0, vecsize, src0);
+    } // else
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_MOVA
+
+static void emit_GLSL_DEFB(Context *ctx)
+{
+    char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "const bool %s = %s;",
+                varname, ctx->dwords[0] ? "true" : "false");
+    pop_output(ctx);
+} // emit_GLSL_DEFB
+
+static void emit_GLSL_DEFI(Context *ctx)
+{
+    char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
+    const int32 *x = (const int32 *) ctx->dwords;
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "const ivec4 %s = ivec4(%d, %d, %d, %d);",
+                varname, (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
+    pop_output(ctx);
+} // emit_GLSL_DEFI
+
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD)
+
+static void emit_GLSL_TEXKILL(Context *ctx)
+{
+    char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+    output_line(ctx, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;", dst);
+} // emit_GLSL_TEXKILL
+
+static void glsl_texld(Context *ctx, const int texldd)
+{
+    if (!shader_version_atleast(ctx, 1, 4))
+    {
+        DestArgInfo *info = &ctx->dest_arg;
+        char dst[64];
+        char sampler[64];
+        char code[128] = {0};
+
+        assert(!texldd);
+
+        RegisterList *sreg;
+        sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, info->regnum);
+        const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+
+        // !!! FIXME: this code counts on the register not having swizzles, etc.
+        get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+        get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                                sampler, sizeof (sampler));
+
+        if (ttype == TEXTURE_TYPE_2D)
+        {
+            make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                     "texture2D(%s, %s.xy)",
+                                     sampler, dst);
+        }
+        else if (ttype == TEXTURE_TYPE_CUBE)
+        {
+            make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                     "textureCube(%s, %s.xyz)",
+                                     sampler, dst);
+        }
+        else if (ttype == TEXTURE_TYPE_VOLUME)
+        {
+            make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                     "texture3D(%s, %s.xyz)",
+                                     sampler, dst);
+        }
+        else
+        {
+            fail(ctx, "unexpected texture type");
+        } // else
+        output_line(ctx, "%s", code);
+    } // if
+
+    else if (!shader_version_atleast(ctx, 2, 0))
+    {
+        // ps_1_4 is different, too!
+        fail(ctx, "TEXLD == Shader Model 1.4 unimplemented.");  // !!! FIXME
+        return;
+    } // else if
+
+    else
+    {
+        const SourceArgInfo *samp_arg = &ctx->source_args[1];
+        RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                          samp_arg->regnum);
+        const char *funcname = NULL;
+        char src0[64] = { '\0' };
+        char src1[64]; get_GLSL_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD?
+        char src2[64] = { '\0' };
+        char src3[64] = { '\0' };
+
+        if (sreg == NULL)
+        {
+            fail(ctx, "TEXLD using undeclared sampler");
+            return;
+        } // if
+
+        if (texldd)
+        {
+            make_GLSL_srcarg_string_vec2(ctx, 2, src2, sizeof (src2));
+            make_GLSL_srcarg_string_vec2(ctx, 3, src3, sizeof (src3));
+        } // if
+
+        // !!! FIXME: can TEXLDD set instruction_controls?
+        // !!! FIXME: does the d3d bias value map directly to GLSL?
+        const char *biassep = "";
+        char bias[64] = { '\0' };
+        if (ctx->instruction_controls == CONTROL_TEXLDB)
+        {
+            biassep = ", ";
+            make_GLSL_srcarg_string_w(ctx, 0, bias, sizeof (bias));
+        } // if
+
+        switch ((const TextureType) sreg->index)
+        {
+            case TEXTURE_TYPE_2D:
+                if (ctx->instruction_controls == CONTROL_TEXLDP)
+                {
+                    funcname = "texture2DProj";
+                    make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+                } // if
+                else  // texld/texldb
+                {
+                    funcname = "texture2D";
+                    make_GLSL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0));
+                } // else
+                break;
+            case TEXTURE_TYPE_CUBE:
+                if (ctx->instruction_controls == CONTROL_TEXLDP)
+                    fail(ctx, "TEXLDP on a cubemap");  // !!! FIXME: is this legal?
+                funcname = "textureCube";
+                make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+                break;
+            case TEXTURE_TYPE_VOLUME:
+                if (ctx->instruction_controls == CONTROL_TEXLDP)
+                {
+                    funcname = "texture3DProj";
+                    make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
+                } // if
+                else  // texld/texldb
+                {
+                    funcname = "texture3D";
+                    make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
+                } // else
+                break;
+            default:
+                fail(ctx, "unknown texture type");
+                return;
+        } // switch
+
+        assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
+        char swiz_str[6] = { '\0' };
+        make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
+                                 samp_arg->swizzle, ctx->dest_arg.writemask);
+
+        char code[128];
+        if (texldd)
+        {
+            make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                     "%sGrad(%s, %s, %s, %s)%s", funcname,
+                                     src1, src0, src2, src3, swiz_str);
+        } // if
+        else
+        {
+            make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                     "%s(%s, %s%s%s)%s", funcname,
+                                     src1, src0, biassep, bias, swiz_str);
+        } // else
+
+        output_line(ctx, "%s", code);
+    } // else
+} // glsl_texld
+
+static void emit_GLSL_TEXLD(Context *ctx)
+{
+     glsl_texld(ctx, 0);
+} // emit_GLSL_TEXLD
+    
+
+static void emit_GLSL_TEXBEM(Context *ctx)
+{
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+    char src[64]; get_GLSL_srcarg_varname(ctx, 0, src, sizeof (src));
+    char sampler[64];
+    char code[512];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "texture2D(%s, vec2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y),"
+        " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))",
+        sampler,
+        dst, sampler, src, sampler, src,
+        dst, sampler, src, sampler, src);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXBEM
+
+
+static void emit_GLSL_TEXBEML(Context *ctx)
+{
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+    char src[64]; get_GLSL_srcarg_varname(ctx, 0, src, sizeof (src));
+    char sampler[64];
+    char code[512];
+
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "(texture2D(%s, vec2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y),"
+        " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))) *"
+        " ((%s.z * %s_texbeml.x) + %s_texbem.y)",
+        sampler,
+        dst, sampler, src, sampler, src,
+        dst, sampler, src, sampler, src,
+        src, sampler, sampler);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXBEML
+
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR) // !!! FIXME
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB) // !!! FIXME
+
+
+static void emit_GLSL_TEXM3X2PAD(Context *ctx)
+{
+    // no-op ... work happens in emit_GLSL_TEXM3X2TEX().
+} // emit_GLSL_TEXM3X2PAD
+
+static void emit_GLSL_TEXM3X2TEX(Context *ctx)
+{
+    if (ctx->texm3x2pad_src0 == -1)
+        return;
+
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char sampler[64];
+    char code[512];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0,
+                            src0, sizeof (src0));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0,
+                            src1, sizeof (src1));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src2, sizeof (src2));
+    get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "texture2D(%s, vec2(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz)))",
+        sampler, src0, src1, src2, dst);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXM3X2TEX
+
+static void emit_GLSL_TEXM3X3PAD(Context *ctx)
+{
+    // no-op ... work happens in emit_GLSL_TEXM3X3*().
+} // emit_GLSL_TEXM3X3PAD
+
+static void emit_GLSL_TEXM3X3TEX(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char sampler[64];
+    char code[512];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                      info->regnum);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "texture%s(%s,"
+            " vec3(dot(%s.xyz, %s.xyz),"
+            " dot(%s.xyz, %s.xyz),"
+            " dot(%s.xyz, %s.xyz)))",
+        ttypestr, sampler, src0, src1, src2, src3, dst, src4);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXM3X3TEX
+
+static void emit_GLSL_TEXM3X3SPEC_helper(Context *ctx)
+{
+    if (ctx->glsl_generated_texm3x3spec_helper)
+        return;
+
+    ctx->glsl_generated_texm3x3spec_helper = 1;
+
+    push_output(ctx, &ctx->helpers);
+    output_line(ctx, "vec3 TEXM3X3SPEC_reflection(const vec3 normal, const vec3 eyeray)");
+    output_line(ctx, "{"); ctx->indent++;
+    output_line(ctx,   "return (2.0 * ((normal * eyeray) / (normal * normal)) * normal) - eyeray;"); ctx->indent--;
+    output_line(ctx, "}");
+    output_blank_line(ctx);
+    pop_output(ctx);
+} // emit_GLSL_TEXM3X3SPEC_helper
+
+static void emit_GLSL_TEXM3X3SPEC(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char src5[64];
+    char sampler[64];
+    char code[512];
+
+    emit_GLSL_TEXM3X3SPEC_helper(ctx);
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[1].regnum,
+                            src5, sizeof (src5));
+    get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                      info->regnum);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "texture%s(%s, "
+            "TEXM3X3SPEC_reflection("
+                "vec3("
+                    "dot(%s.xyz, %s.xyz), "
+                    "dot(%s.xyz, %s.xyz), "
+                    "dot(%s.xyz, %s.xyz)"
+                "),"
+                "%s.xyz,"
+            ")"
+        ")",
+        ttypestr, sampler, src0, src1, src2, src3, dst, src4, src5);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXM3X3SPEC
+
+static void emit_GLSL_TEXM3X3VSPEC(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    DestArgInfo *info = &ctx->dest_arg;
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char sampler[64];
+    char code[512];
+
+    emit_GLSL_TEXM3X3SPEC_helper(ctx);
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
+                            sampler, sizeof (sampler));
+
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                      info->regnum);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "texture%s(%s, "
+            "TEXM3X3SPEC_reflection("
+                "vec3("
+                    "dot(%s.xyz, %s.xyz), "
+                    "dot(%s.xyz, %s.xyz), "
+                    "dot(%s.xyz, %s.xyz)"
+                "), "
+                "vec3(%s.w, %s.w, %s.w)"
+            ")"
+        ")",
+        ttypestr, sampler, src0, src1, src2, src3, dst, src4, src0, src2, dst);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXM3X3VSPEC
+
+static void emit_GLSL_EXPP(Context *ctx)
+{
+    // !!! FIXME: msdn's asm docs don't list this opcode, I'll have to check the driver documentation.
+    emit_GLSL_EXP(ctx);  // I guess this is just partial precision EXP?
+} // emit_GLSL_EXPP
+
+static void emit_GLSL_LOGP(Context *ctx)
+{
+    // LOGP is just low-precision LOG, but we'll take the higher precision.
+    emit_GLSL_LOG(ctx);
+} // emit_GLSL_LOGP
+
+// common code between CMP and CND.
+static void emit_GLSL_comparison_operations(Context *ctx, const char *cmp)
+{
+    int i, j;
+    DestArgInfo *dst = &ctx->dest_arg;
+    const SourceArgInfo *srcarg0 = &ctx->source_args[0];
+    const int origmask = dst->writemask;
+    int used_swiz[4] = { 0, 0, 0, 0 };
+    const int writemask[4] = { dst->writemask0, dst->writemask1,
+                               dst->writemask2, dst->writemask3 };
+    const int src0swiz[4] = { srcarg0->swizzle_x, srcarg0->swizzle_y,
+                              srcarg0->swizzle_z, srcarg0->swizzle_w };
+
+    for (i = 0; i < 4; i++)
+    {
+        int mask = (1 << i);
+
+        if (!writemask[i]) continue;
+        if (used_swiz[i]) continue;
+
+        // This is a swizzle we haven't checked yet.
+        used_swiz[i] = 1;
+
+        // see if there are any other elements swizzled to match (.yyyy)
+        for (j = i + 1; j < 4; j++)
+        {
+            if (!writemask[j]) continue;
+            if (src0swiz[i] != src0swiz[j]) continue;
+            mask |= (1 << j);
+            used_swiz[j] = 1;
+        } // for
+
+        // okay, (mask) should be the writemask of swizzles we like.
+
+        //return make_GLSL_srcarg_string(ctx, idx, (1 << 0));
+
+        char src0[64];
+        char src1[64];
+        char src2[64];
+        make_GLSL_srcarg_string(ctx, 0, (1 << i), src0, sizeof (src0));
+        make_GLSL_srcarg_string(ctx, 1, mask, src1, sizeof (src1));
+        make_GLSL_srcarg_string(ctx, 2, mask, src2, sizeof (src2));
+
+        set_dstarg_writemask(dst, mask);
+
+        char code[128];
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "((%s %s) ? %s : %s)",
+                                 src0, cmp, src1, src2);
+        output_line(ctx, "%s", code);
+    } // for
+
+    set_dstarg_writemask(dst, origmask);
+} // emit_GLSL_comparison_operations
+
+static void emit_GLSL_CND(Context *ctx)
+{
+    emit_GLSL_comparison_operations(ctx, "> 0.5");
+} // emit_GLSL_CND
+
+static void emit_GLSL_DEF(Context *ctx)
+{
+    const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
+    char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
+    char val0[32]; floatstr(ctx, val0, sizeof (val0), val[0], 1);
+    char val1[32]; floatstr(ctx, val1, sizeof (val1), val[1], 1);
+    char val2[32]; floatstr(ctx, val2, sizeof (val2), val[2], 1);
+    char val3[32]; floatstr(ctx, val3, sizeof (val3), val[3], 1);
+
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "const vec4 %s = vec4(%s, %s, %s, %s);",
+                varname, val0, val1, val2, val3);
+    pop_output(ctx);
+} // emit_GLSL_DEF
+
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB) // !!! FIXME
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX) // !!! FIXME
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH) // !!! FIXME
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3) // !!! FIXME
+
+static void emit_GLSL_TEXM3X3(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char code[512];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
+
+    make_GLSL_destarg_assign(ctx, code, sizeof (code),
+        "vec4(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), 1.0)",
+        src0, src1, src2, src3, dst, src4);
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_TEXM3X3
+
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH) // !!! FIXME
+
+static void emit_GLSL_CMP(Context *ctx)
+{
+    emit_GLSL_comparison_operations(ctx, ">= 0.0");
+} // emit_GLSL_CMP
+
+EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(BEM) // !!! FIXME
+
+static void emit_GLSL_DP2ADD(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_vec2(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_GLSL_srcarg_string_scalar(ctx, 2, src2, sizeof (src2));
+    char extra[64]; snprintf(extra, sizeof (extra), " + %s", src2);
+    emit_GLSL_dotprod(ctx, src0, src1, extra);
+} // emit_GLSL_DP2ADD
+
+static void emit_GLSL_DSX(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "dFdx(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_DSX
+
+static void emit_GLSL_DSY(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char code[128];
+    make_GLSL_destarg_assign(ctx, code, sizeof (code), "dFdy(%s)", src0);
+    output_line(ctx, "%s", code);
+} // emit_GLSL_DSY
+
+static void emit_GLSL_TEXLDD(Context *ctx)
+{
+    // !!! FIXME:
+    // GLSL 1.30 introduced textureGrad() for this, but it looks like the
+    //  functions are overloaded instead of texture2DGrad() (etc).
+
+    // GL_shader_texture_lod and GL_EXT_gpu_shader4 added texture2DGrad*(),
+    //  so we'll use them if available. Failing that, we'll just fallback
+    //  to a regular texture2D call and hope the mipmap it chooses is close
+    //  enough.
+    if (!ctx->glsl_generated_texldd_setup)
+    {
+        ctx->glsl_generated_texldd_setup = 1;
+        push_output(ctx, &ctx->preflight);
+        output_line(ctx, "#if GL_ARB_shader_texture_lod");
+        output_line(ctx, "#extension GL_ARB_shader_texture_lod : enable");
+        output_line(ctx, "#define texture2DGrad texture2DGradARB");
+        output_line(ctx, "#define texture2DProjGrad texture2DProjARB");
+        output_line(ctx, "#elif GL_EXT_gpu_shader4");
+        output_line(ctx, "#extension GL_EXT_gpu_shader4 : enable");
+        output_line(ctx, "#else");
+        output_line(ctx, "#define texture2DGrad(a,b,c,d) texture2D(a,b)");
+        output_line(ctx, "#define texture2DProjGrad(a,b,c,d) texture2DProj(a,b)");
+        output_line(ctx, "#endif");
+        output_blank_line(ctx);
+        pop_output(ctx);
+    } // if
+
+    glsl_texld(ctx, 1);
+} // emit_GLSL_TEXLDD
+
+static void emit_GLSL_SETP(Context *ctx)
+{
+    const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
+    char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
+    char code[128];
+
+    // destination is always predicate register (which is type bvec4).
+    if (vecsize == 1)
+    {
+        const char *comp = get_GLSL_comparison_string_scalar(ctx);
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "(%s %s %s)", src0, comp, src1);
+    } // if
+    else
+    {
+        const char *comp = get_GLSL_comparison_string_vector(ctx);
+        make_GLSL_destarg_assign(ctx, code, sizeof (code),
+                                 "%s(%s, %s)", comp, src0, src1);
+    } // else
+
+    output_line(ctx, "%s", code);
+} // emit_GLSL_SETP
+
+static void emit_GLSL_TEXLDL(Context *ctx)
+{
+    // !!! FIXME: The spec says we can't use GLSL's texture*Lod() built-ins
+    // !!! FIXME:  from fragment shaders for some inexplicable reason.
+    // !!! FIXME:  For now, you'll just have to suffer with the potentially
+    // !!! FIXME:  wrong mipmap until I can figure something out.
+    emit_GLSL_TEXLD(ctx);
+} // emit_GLSL_TEXLDL
+
+static void emit_GLSL_BREAKP(Context *ctx)
+{
+    char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
+    output_line(ctx, "if (%s) { break; }", src0);
+} // emit_GLSL_BREAKP
+
+static void emit_GLSL_RESERVED(Context *ctx)
+{
+    // do nothing; fails in the state machine.
+} // emit_GLSL_RESERVED
+
+#endif  // SUPPORT_PROFILE_GLSL
+
+
+
+#if !SUPPORT_PROFILE_ARB1
+#define PROFILE_EMITTER_ARB1(op)
+#else
+#undef AT_LEAST_ONE_PROFILE
+#define AT_LEAST_ONE_PROFILE 1
+#define PROFILE_EMITTER_ARB1(op) emit_ARB1_##op,
+
+static inline const char *get_ARB1_register_string(Context *ctx,
+                        const RegisterType regtype, const int regnum,
+                        char *regnum_str, const size_t regnum_size)
+{
+    // turns out these are identical at the moment.
+    return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
+} // get_ARB1_register_string
+
+static const char *allocate_ARB1_scratch_reg_name(Context *ctx, char *buf,
+                                                  const size_t buflen)
+{
+    const int scratch = allocate_scratch_register(ctx);
+    snprintf(buf, buflen, "scratch%d", scratch);
+    return buf;
+} // allocate_ARB1_scratch_reg_name
+
+static inline const char *get_ARB1_branch_label_name(Context *ctx, const int id,
+                                                char *buf, const size_t buflen)
+{
+    snprintf(buf, buflen, "branch_label%d", id);
+    return buf;
+} // get_ARB1_branch_label_name
+
+static const char *get_ARB1_varname_in_buf(Context *ctx, const RegisterType rt,
+                                           const int regnum, char *buf,
+                                           const size_t buflen)
+{
+    // turns out these are identical at the moment.
+    return get_D3D_varname_in_buf(ctx, rt, regnum, buf, buflen);
+} // get_ARB1_varname_in_buf
+
+static const char *get_ARB1_varname(Context *ctx, const RegisterType rt,
+                                    const int regnum)
+{
+    // turns out these are identical at the moment.
+    return get_D3D_varname(ctx, rt, regnum);
+} // get_ARB1_varname
+
+
+static inline const char *get_ARB1_const_array_varname_in_buf(Context *ctx,
+                                                const int base, const int size,
+                                                char *buf, const size_t buflen)
+{
+    snprintf(buf, buflen, "c_array_%d_%d", base, size);
+    return buf;
+} // get_ARB1_const_array_varname_in_buf
+
+
+static const char *get_ARB1_const_array_varname(Context *ctx, int base, int size)
+{
+    char buf[64];
+    get_ARB1_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
+    return StrDup(ctx, buf);
+} // get_ARB1_const_array_varname
+
+
+static const char *make_ARB1_srcarg_string_in_buf(Context *ctx,
+                                                  const SourceArgInfo *arg,
+                                                  char *buf, size_t buflen)
+{
+    // !!! FIXME: this can hit pathological cases where we look like this...
+    //
+    //    dp3 r1.xyz, t0_bx2, t0_bx2
+    //    mad r1.xyz, t0_bias, 1-r1, t0_bx2
+    //
+    // ...which do a lot of duplicate work in arb1...
+    //
+    //    SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
+    //    MUL scratch0, scratch0, { 2.0, 2.0, 2.0, 2.0 };
+    //    SUB scratch1, t0, { 0.5, 0.5, 0.5, 0.5 };
+    //    MUL scratch1, scratch1, { 2.0, 2.0, 2.0, 2.0 };
+    //    DP3 r1.xyz, scratch0, scratch1;
+    //    SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
+    //    SUB scratch1, { 1.0, 1.0, 1.0, 1.0 }, r1;
+    //    SUB scratch2, t0, { 0.5, 0.5, 0.5, 0.5 };
+    //    MUL scratch2, scratch2, { 2.0, 2.0, 2.0, 2.0 };
+    //    MAD r1.xyz, scratch0, scratch1, scratch2;
+    //
+    // ...notice that the dp3 calculates the same value into two scratch
+    //  registers. This case is easier to handle; just see if multiple
+    //  source args are identical, build it up once, and use the same
+    //  scratch register for multiple arguments in that opcode.
+    //  Even better still, only calculate things once across instructions,
+    //  and be smart about letting it linger in a scratch register until we
+    //  definitely don't need the calculation anymore. That's harder to
+    //  write, though.
+
+    char regnum_str[16] = { '\0' };
+
+    // !!! FIXME: use get_ARB1_varname_in_buf() instead?
+    const char *regtype_str = NULL;
+    if (!arg->relative)
+    {
+        regtype_str = get_ARB1_register_string(ctx, arg->regtype,
+                                               arg->regnum, regnum_str,
+                                               sizeof (regnum_str));
+    } // if
+
+    const char *rel_lbracket = "";
+    char rel_offset[32] = { '\0' };
+    const char *rel_rbracket = "";
+    char rel_swizzle[4] = { '\0' };
+    const char *rel_regtype_str = "";
+    if (arg->relative)
+    {
+        rel_regtype_str = get_ARB1_varname_in_buf(ctx, arg->relative_regtype,
+                                                  arg->relative_regnum,
+                                                  (char *) alloca(64), 64);
+
+        rel_swizzle[0] = '.';
+        rel_swizzle[1] = swizzle_channels[arg->relative_component];
+        rel_swizzle[2] = '\0';
+
+        if (!support_nv2(ctx))
+        {
+            // The address register in ARB1 only allows the '.x' component, so
+            //  we need to load the component we need from a temp vector
+            //  register into .x as needed.
+            assert(arg->relative_regtype == REG_TYPE_ADDRESS);
+            assert(arg->relative_regnum == 0);
+            if (ctx->last_address_reg_component != arg->relative_component)
+            {
+                output_line(ctx, "ARL %s.x, addr%d.%c;", rel_regtype_str,
+                            arg->relative_regnum,
+                            swizzle_channels[arg->relative_component]);
+                ctx->last_address_reg_component = arg->relative_component;
+            } // if
+
+            rel_swizzle[1] = 'x';
+        } // if
+
+        if (arg->regtype == REG_TYPE_INPUT)
+            regtype_str = "vertex.attrib";
+        else
+        {
+            assert(arg->regtype == REG_TYPE_CONST);
+            const int arrayidx = arg->relative_array->index;
+            const int arraysize = arg->relative_array->count;
+            const int offset = arg->regnum - arrayidx;
+            assert(offset >= 0);
+            regtype_str = get_ARB1_const_array_varname_in_buf(ctx, arrayidx,
+                                           arraysize, (char *) alloca(64), 64);
+            if (offset != 0)
+                snprintf(rel_offset, sizeof (rel_offset), " + %d", offset);
+        } // else
+
+        rel_lbracket = "[";
+        rel_rbracket = "]";
+    } // if
+
+    // This is the source register with everything but swizzle and source mods.
+    snprintf(buf, buflen, "%s%s%s%s%s%s%s", regtype_str, regnum_str,
+             rel_lbracket, rel_regtype_str, rel_swizzle, rel_offset,
+             rel_rbracket);
+
+    // Some of the source mods need to generate instructions to a temp
+    //  register, in which case we'll replace the register name.
+    const SourceMod mod = arg->src_mod;
+    const int inplace = ( (mod == SRCMOD_NONE) || (mod == SRCMOD_NEGATE) ||
+                          ((mod == SRCMOD_ABS) && support_nv2(ctx)) );
+
+    if (!inplace)
+    {
+        const size_t len = 64;
+        char *stackbuf = (char *) alloca(len);
+        regtype_str = allocate_ARB1_scratch_reg_name(ctx, stackbuf, len);
+        regnum_str[0] = '\0'; // move value to scratch register.
+        rel_lbracket = "";   // scratch register won't use array.
+        rel_rbracket = "";
+        rel_offset[0] = '\0';
+        rel_swizzle[0] = '\0';
+        rel_regtype_str = "";
+    } // if
+
+    const char *premod_str = "";
+    const char *postmod_str = "";
+    switch (mod)
+    {
+        case SRCMOD_NEGATE:
+            premod_str = "-";
+            break;
+
+        case SRCMOD_BIASNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_BIAS:
+            output_line(ctx, "SUB %s, %s, { 0.5, 0.5, 0.5, 0.5 };",
+                        regtype_str, buf);
+            break;
+
+        case SRCMOD_SIGNNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_SIGN:
+            output_line(ctx,
+                "MAD %s, %s, { 2.0, 2.0, 2.0, 2.0 }, { -1.0, -1.0, -1.0, -1.0 };",
+                regtype_str, buf);
+            break;
+
+        case SRCMOD_COMPLEMENT:
+            output_line(ctx, "SUB %s, { 1.0, 1.0, 1.0, 1.0 }, %s;",
+                        regtype_str, buf);
+            break;
+
+        case SRCMOD_X2NEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_X2:
+            output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };",
+                        regtype_str, buf);
+            break;
+
+        case SRCMOD_DZ:
+            fail(ctx, "SRCMOD_DZ currently unsupported in arb1");
+            postmod_str = "_dz";
+            break;
+
+        case SRCMOD_DW:
+            fail(ctx, "SRCMOD_DW currently unsupported in arb1");
+            postmod_str = "_dw";
+            break;
+
+        case SRCMOD_ABSNEGATE:
+            premod_str = "-";
+            // fall through.
+        case SRCMOD_ABS:
+            if (!support_nv2(ctx))  // GL_NV_vertex_program2_option adds this.
+                output_line(ctx, "ABS %s, %s;", regtype_str, buf);
+            else
+            {
+                premod_str = (mod == SRCMOD_ABSNEGATE) ? "-|" : "|";
+                postmod_str = "|";
+            } // else
+            break;
+
+        case SRCMOD_NOT:
+            fail(ctx, "SRCMOD_NOT currently unsupported in arb1");
+            premod_str = "!";
+            break;
+
+        case SRCMOD_NONE:
+        case SRCMOD_TOTAL:
+             break;  // stop compiler whining.
+    } // switch
+
+    char swizzle_str[6];
+    size_t i = 0;
+
+    if (support_nv4(ctx))  // vFace must be output as "vFace.x" in nv4.
+    {
+        if (arg->regtype == REG_TYPE_MISCTYPE)
+        {
+            if ( ((const MiscTypeType) arg->regnum) == MISCTYPE_TYPE_FACE )
+            {
+                swizzle_str[i++] = '.';
+                swizzle_str[i++] = 'x';
+            } // if
+        } // if
+    } // if
+
+    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
+    if (!scalar && !no_swizzle(arg->swizzle))
+    {
+        swizzle_str[i++] = '.';
+
+        // .xxxx is the same as .x, but .xx is illegal...scalar or full!
+        if (replicate_swizzle(arg->swizzle))
+            swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
+        else
+        {
+            swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
+            swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
+            swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
+            swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
+        } // else
+    } // if
+    swizzle_str[i] = '\0';
+    assert(i < sizeof (swizzle_str));
+
+    snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s", premod_str,
+             regtype_str, regnum_str, rel_lbracket,
+             rel_regtype_str, rel_swizzle, rel_offset, rel_rbracket,
+             swizzle_str, postmod_str);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_ARB1_srcarg_string_in_buf
+
+static const char *get_ARB1_destarg_varname(Context *ctx, char *buf,
+                                            const size_t buflen)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+    return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
+} // get_ARB1_destarg_varname
+
+static const char *get_ARB1_srcarg_varname(Context *ctx, const size_t idx,
+                                           char *buf, const size_t buflen)
+{
+    if (idx >= STATICARRAYLEN(ctx->source_args))
+    {
+        fail(ctx, "Too many source args");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const SourceArgInfo *arg = &ctx->source_args[idx];
+    return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
+} // get_ARB1_srcarg_varname
+
+
+static const char *make_ARB1_destarg_string(Context *ctx, char *buf,
+                                            const size_t buflen)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+
+    *buf = '\0';
+
+    const char *sat_str = "";
+    if (arg->result_mod & MOD_SATURATE)
+    {
+        // nv4 can use ".SAT" in all program types.
+        // For less than nv4, the "_SAT" modifier is only available in
+        //  fragment shaders. Every thing else will fake it later in
+        //  emit_ARB1_dest_modifiers() ...
+        if (support_nv4(ctx))
+            sat_str = ".SAT";
+        else if (shader_is_pixel(ctx))
+            sat_str = "_SAT";
+    } // if
+
+    const char *pp_str = "";
+    if (arg->result_mod & MOD_PP)
+    {
+        // Most ARB1 profiles can't do partial precision (MOD_PP), but that's
+        //  okay. The spec says lots of Direct3D implementations ignore the
+        //  flag anyhow.
+        if (support_nv4(ctx))
+            pp_str = "H";
+    } // if
+
+    // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
+    assert((arg->result_mod & MOD_CENTROID) == 0);
+
+    char regnum_str[16];
+    const char *regtype_str = get_ARB1_register_string(ctx, arg->regtype,
+                                                       arg->regnum, regnum_str,
+                                                       sizeof (regnum_str));
+    if (regtype_str == NULL)
+    {
+        fail(ctx, "Unknown destination register type.");
+        return buf;
+    } // if
+
+    char writemask_str[6];
+    size_t i = 0;
+    const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
+    if (!scalar && !writemask_xyzw(arg->writemask))
+    {
+        writemask_str[i++] = '.';
+        if (arg->writemask0) writemask_str[i++] = 'x';
+        if (arg->writemask1) writemask_str[i++] = 'y';
+        if (arg->writemask2) writemask_str[i++] = 'z';
+        if (arg->writemask3) writemask_str[i++] = 'w';
+    } // if
+    writemask_str[i] = '\0';
+    assert(i < sizeof (writemask_str));
+
+    const char *pred_left = "";
+    const char *pred_right = "";
+    char pred[32] = { '\0' };
+    if (ctx->predicated)
+    {
+        fail(ctx, "dest register predication currently unsupported in arb1");
+        return buf;
+        pred_left = "(";
+        pred_right = ") ";
+        make_ARB1_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
+                                       pred, sizeof (pred));
+    } // if
+
+    snprintf(buf, buflen, "%s%s %s%s%s", pp_str, sat_str,
+             regtype_str, regnum_str, writemask_str);
+    // !!! FIXME: make sure the scratch buffer was large enough.
+    return buf;
+} // make_ARB1_destarg_string
+
+
+static void emit_ARB1_dest_modifiers(Context *ctx)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+
+    if (arg->result_shift != 0x0)
+    {
+        char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        const char *multiplier = NULL;
+
+        switch (arg->result_shift)
+        {
+            case 0x1: multiplier = "2.0"; break;
+            case 0x2: multiplier = "4.0"; break;
+            case 0x3: multiplier = "8.0"; break;
+            case 0xD: multiplier = "0.125"; break;
+            case 0xE: multiplier = "0.25"; break;
+            case 0xF: multiplier = "0.5"; break;
+        } // switch
+
+        if (multiplier != NULL)
+        {
+            char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
+            output_line(ctx, "MUL%s, %s, %s;", dst, var, multiplier);
+        } // if
+    } // if
+
+    if (arg->result_mod & MOD_SATURATE)
+    {
+        // nv4 and/or pixel shaders just used the "SAT" modifier, instead.
+        if ( (!support_nv4(ctx)) && (!shader_is_pixel(ctx)) )
+        {
+            char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
+            char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+            output_line(ctx, "MIN%s, %s, 1.0;", dst, var);
+            output_line(ctx, "MAX%s, %s, 0.0;", dst, var);
+        } // if
+    } // if
+} // emit_ARB1_dest_modifiers
+
+
+static const char *make_ARB1_srcarg_string(Context *ctx, const size_t idx,
+                                           char *buf, const size_t buflen)
+{
+    if (idx >= STATICARRAYLEN(ctx->source_args))
+    {
+        fail(ctx, "Too many source args");
+        *buf = '\0';
+        return buf;
+    } // if
+
+    const SourceArgInfo *arg = &ctx->source_args[idx];
+    return make_ARB1_srcarg_string_in_buf(ctx, arg, buf, buflen);
+} // make_ARB1_srcarg_string
+
+static void emit_ARB1_opcode_ds(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    output_line(ctx, "%s%s, %s;", opcode, dst, src0);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_opcode_ds
+
+static void emit_ARB1_opcode_dss(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+    output_line(ctx, "%s%s, %s, %s;", opcode, dst, src0, src1);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_opcode_dss
+
+static void emit_ARB1_opcode_dsss(Context *ctx, const char *opcode)
+{
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+    output_line(ctx, "%s%s, %s, %s, %s;", opcode, dst, src0, src1, src2);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_opcode_dsss
+
+
+#define EMIT_ARB1_OPCODE_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_D_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_d(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_S_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_s(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_SS_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_ss(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_DS_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_ds(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_DSS_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_dss(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_DSSS_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_dsss(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_DSSSS_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        emit_ARB1_opcode_dssss(ctx, #op); \
+    }
+#define EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(op) \
+    static void emit_ARB1_##op(Context *ctx) { \
+        failf(ctx, #op " unimplemented in %s profile", ctx->profile->name); \
+    }
+
+
+static void emit_ARB1_start(Context *ctx, const char *profilestr)
+{
+    const char *shader_str = NULL;
+    const char *shader_full_str = NULL;
+    if (shader_is_vertex(ctx))
+    {
+        shader_str = "vp";
+        shader_full_str = "vertex";
+    } // if
+    else if (shader_is_pixel(ctx))
+    {
+        shader_str = "fp";
+        shader_full_str = "fragment";
+    } // else if
+    else
+    {
+        failf(ctx, "Shader type %u unsupported in this profile.",
+              (uint) ctx->shader_type);
+        return;
+    } // if
+
+    set_output(ctx, &ctx->preflight);
+
+    if (strcmp(profilestr, MOJOSHADER_PROFILE_ARB1) == 0)
+        output_line(ctx, "!!ARB%s1.0", shader_str);
+
+    #if SUPPORT_PROFILE_ARB1_NV
+    else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV2) == 0)
+    {
+        ctx->profile_supports_nv2 = 1;
+        output_line(ctx, "!!ARB%s1.0", shader_str);
+        output_line(ctx, "OPTION NV_%s_program2;", shader_full_str);
+    } // else if
+
+    else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV3) == 0)
+    {
+        // there's no NV_fragment_program3, so just use 2.
+        const int ver = shader_is_pixel(ctx) ? 2 : 3;
+        ctx->profile_supports_nv2 = 1;
+        ctx->profile_supports_nv3 = 1;
+        output_line(ctx, "!!ARB%s1.0", shader_str);
+        output_line(ctx, "OPTION NV_%s_program%d;", shader_full_str, ver);
+    } // else if
+
+    else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV4) == 0)
+    {
+        ctx->profile_supports_nv2 = 1;
+        ctx->profile_supports_nv3 = 1;
+        ctx->profile_supports_nv4 = 1;
+        output_line(ctx, "!!NV%s4.0", shader_str);
+    } // else if
+    #endif
+
+    else
+    {
+        failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
+    } // else
+
+    set_output(ctx, &ctx->mainline);
+} // emit_ARB1_start
+
+static void emit_ARB1_end(Context *ctx)
+{
+    // ps_1_* writes color to r0 instead oC0. We move it to the right place.
+    // We don't have to worry about a RET opcode messing this up, since
+    //  RET isn't available before ps_2_0.
+    if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
+    {
+        set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
+        output_line(ctx, "MOV oC0, r0;");
+    } // if
+
+    output_line(ctx, "END");
+} // emit_ARB1_end
+
+static void emit_ARB1_phase(Context *ctx)
+{
+    // no-op in arb1.
+} // emit_ARB1_phase
+
+static inline const char *arb1_float_temp(const Context *ctx)
+{
+    // nv4 lets you specify data type.
+    return (support_nv4(ctx)) ? "FLOAT TEMP" : "TEMP";
+} // arb1_float_temp
+
+static void emit_ARB1_finalize(Context *ctx)
+{
+    push_output(ctx, &ctx->preflight);
+
+    if (shader_is_vertex(ctx) && !ctx->arb1_wrote_position)
+        output_line(ctx, "OPTION ARB_position_invariant;");
+
+    if (shader_is_pixel(ctx) && ctx->have_multi_color_outputs)
+        output_line(ctx, "OPTION ARB_draw_buffers;");
+
+    pop_output(ctx);
+
+    const char *tmpstr = arb1_float_temp(ctx);
+    int i;
+    push_output(ctx, &ctx->globals);
+    for (i = 0; i < ctx->max_scratch_registers; i++)
+    {
+        char buf[64];
+        allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        output_line(ctx, "%s %s;", tmpstr, buf);
+    } // for
+
+    // nv2 fragment programs (and anything nv4) have a real REP/ENDREP.
+    if ( (support_nv2(ctx)) && (!shader_is_pixel(ctx)) && (!support_nv4(ctx)) )
+    {
+        // set up temps for nv2 REP/ENDREP emulation through branching.
+        for (i = 0; i < ctx->max_reps; i++)
+            output_line(ctx, "TEMP rep%d;", i);
+    } // if
+
+    pop_output(ctx);
+    assert(ctx->scratch_registers == ctx->max_scratch_registers);
+} // emit_ARB1_finalize
+
+static void emit_ARB1_global(Context *ctx, RegisterType regtype, int regnum)
+{
+    // !!! FIXME: dependency on ARB1 profile.  // !!! FIXME about FIXME: huh?
+    char varname[64];
+    get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
+
+    push_output(ctx, &ctx->globals);
+    switch (regtype)
+    {
+        case REG_TYPE_ADDRESS:
+            if (shader_is_pixel(ctx))  // actually REG_TYPE_TEXTURE.
+            {
+                // We have to map texture registers to temps for ps_1_1, since
+                //  they work like temps, initialize with tex coords, and the
+                //  ps_1_1 TEX opcode expects to overwrite it.
+                if (!shader_version_atleast(ctx, 1, 4))
+                {
+                    output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
+                    push_output(ctx, &ctx->mainline_intro);
+                    output_line(ctx, "MOV %s, fragment.texcoord[%d];",
+                                varname, regnum);
+                    pop_output(ctx);
+                } // if
+                break;
+            } // if
+
+            // nv4 replaced address registers with generic int registers.
+            if (support_nv4(ctx))
+                output_line(ctx, "INT TEMP %s;", varname);
+            else
+            {
+                // nv2 has four-component address already, but stock arb1 has
+                //  to emulate it in a temporary, and move components to the
+                //  scalar ADDRESS register on demand.
+                output_line(ctx, "ADDRESS %s;", varname);
+                if (!support_nv2(ctx))
+                    output_line(ctx, "TEMP addr%d;", regnum);
+            } // else
+            break;
+
+        //case REG_TYPE_PREDICATE:
+        //    output_line(ctx, "bvec4 %s;", varname);
+        //    break;
+        case REG_TYPE_TEMP:
+            output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
+            break;
+        //case REG_TYPE_LOOP:
+        //    break; // no-op. We declare these in for loops at the moment.
+        //case REG_TYPE_LABEL:
+        //    break; // no-op. If we see it here, it means we optimized it out.
+        default:
+            fail(ctx, "BUG: we used a register we don't know how to define.");
+            break;
+    } // switch
+    pop_output(ctx);
+} // emit_ARB1_global
+
+static void emit_ARB1_array(Context *ctx, VariableList *var)
+{
+    // All uniforms are now packed tightly into the program.local array,
+    //  instead of trying to map them to the d3d registers. So this needs to
+    //  map to the next piece of the array we haven't used yet. Thankfully,
+    //  arb1 lets you make a PARAM array that maps to a subset of another
+    //  array; we don't need to do offsets, since myarray[0] can map to
+    //  program.local[5] without any extra math from us.
+    const int base = var->index;
+    const int size = var->count;
+    const int arb1base = ctx->uniform_float4_count +
+                         ctx->uniform_int4_count +
+                         ctx->uniform_bool_count;
+    char varname[64];
+    get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "PARAM %s[%d] = { program.local[%d..%d] };", varname,
+                size, arb1base, (arb1base + size) - 1);
+    pop_output(ctx);
+    var->emit_position = arb1base;
+} // emit_ARB1_array
+
+static void emit_ARB1_const_array(Context *ctx, const ConstantsList *clist,
+                                  int base, int size)
+{
+    char varname[64];
+    get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
+    int i;
+
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "PARAM %s[%d] = {", varname, size);
+    ctx->indent++;
+
+    for (i = 0; i < size; i++)
+    {
+        while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
+            clist = clist->next;
+        assert(clist->constant.index == (base + i));
+
+        char val0[32];
+        char val1[32];
+        char val2[32];
+        char val3[32];
+        floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
+        floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
+        floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
+        floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
+
+        output_line(ctx, "{ %s, %s, %s, %s }%s", val0, val1, val2, val3,
+                    (i < (size-1)) ? "," : "");
+
+        clist = clist->next;
+    } // for
+
+    ctx->indent--;
+    output_line(ctx, "};");
+    pop_output(ctx);
+} // emit_ARB1_const_array
+
+static void emit_ARB1_uniform(Context *ctx, RegisterType regtype, int regnum,
+                              const VariableList *var)
+{
+    // We pack these down into the program.local array, so if we only use
+    //  register c439, it'll actually map to program.local[0]. This will
+    //  prevent overflows when we actually have enough resources to run.
+
+    const char *arrayname = "program.local";
+    int index = 0;
+
+    char varname[64];
+    get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
+
+    push_output(ctx, &ctx->globals);
+
+    if (var == NULL)
+    {
+        // all types share one array (rather, all types convert to float4).
+        index = ctx->uniform_float4_count + ctx->uniform_int4_count +
+                ctx->uniform_bool_count;
+    } // if
+
+    else
+    {
+        const int arraybase = var->index;
+        if (var->constant)
+        {
+            const int arraysize = var->count;
+            arrayname = get_ARB1_const_array_varname_in_buf(ctx, arraybase,
+                                        arraysize, (char *) alloca(64), 64);
+            index = (regnum - arraybase);
+        } // if
+        else
+        {
+            assert(var->emit_position != -1);
+            index = (regnum - arraybase) + var->emit_position;
+        } // else
+    } // else
+
+    output_line(ctx, "PARAM %s = %s[%d];", varname, arrayname, index);
+    pop_output(ctx);
+} // emit_ARB1_uniform
+
+static void emit_ARB1_sampler(Context *ctx,int stage,TextureType ttype,int tb)
+{
+    // this is mostly a no-op...you don't predeclare samplers in arb1.
+
+    if (tb)  // This sampler used a ps_1_1 TEXBEM opcode?
+    {
+        const int index = ctx->uniform_float4_count + ctx->uniform_int4_count +
+                          ctx->uniform_bool_count;
+        char var[64];
+        get_ARB1_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof(var));
+        push_output(ctx, &ctx->globals);
+        output_line(ctx, "PARAM %s_texbem = program.local[%d];", var, index);
+        output_line(ctx, "PARAM %s_texbeml = program.local[%d];", var, index+1);
+        pop_output(ctx);
+        ctx->uniform_float4_count += 2;
+    } // if
+} // emit_ARB1_sampler
+
+// !!! FIXME: a lot of cut-and-paste here from emit_GLSL_attribute().
+static void emit_ARB1_attribute(Context *ctx, RegisterType regtype, int regnum,
+                                MOJOSHADER_usage usage, int index, int wmask,
+                                int flags)
+{
+    // !!! FIXME: this function doesn't deal with write masks at all yet!
+    const char *usage_str = NULL;
+    const char *arrayleft = "";
+    const char *arrayright = "";
+    char index_str[16] = { '\0' };
+
+    char varname[64];
+    get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
+
+    //assert((flags & MOD_PP) == 0);  // !!! FIXME: is PP allowed?
+
+    if (index != 0)  // !!! FIXME: a lot of these MUST be zero.
+        snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+
+    if (shader_is_vertex(ctx))
+    {
+        // pre-vs3 output registers.
+        // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
+        //  output registers.
+        if (!shader_version_atleast(ctx, 3, 0))
+        {
+            if (regtype == REG_TYPE_RASTOUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                index = regnum;
+                switch ((const RastOutType) regnum)
+                {
+                    case RASTOUT_TYPE_POSITION:
+                        usage = MOJOSHADER_USAGE_POSITION;
+                        break;
+                    case RASTOUT_TYPE_FOG:
+                        usage = MOJOSHADER_USAGE_FOG;
+                        break;
+                    case RASTOUT_TYPE_POINT_SIZE:
+                        usage = MOJOSHADER_USAGE_POINTSIZE;
+                        break;
+                } // switch
+            } // if
+
+            else if (regtype == REG_TYPE_ATTROUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                usage = MOJOSHADER_USAGE_COLOR;
+                index = regnum;
+            } // else if
+
+            else if (regtype == REG_TYPE_TEXCRDOUT)
+            {
+                regtype = REG_TYPE_OUTPUT;
+                usage = MOJOSHADER_USAGE_TEXCOORD;
+                index = regnum;
+            } // else if
+        } // if
+
+        // to avoid limitations of various GL entry points for input
+        // attributes (glSecondaryColorPointer() can only take 3 component
+        // items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
+        // issues), we set up all inputs as generic vertex attributes, so we
+        // can pass data in just about any form, and ignore the built-in GLSL
+        // attributes like gl_SecondaryColor. Output needs to use the the
+        // built-ins, though, but we don't have to worry about the GL entry
+        // point limitations there.
+
+        if (regtype == REG_TYPE_INPUT)
+        {
+            const int attr = ctx->assigned_vertex_attributes++;
+            push_output(ctx, &ctx->globals);
+            output_line(ctx, "ATTRIB %s = vertex.attrib[%d];", varname, attr);
+            pop_output(ctx);
+        } // if
+
+        else if (regtype == REG_TYPE_OUTPUT)
+        {
+            switch (usage)
+            {
+                case MOJOSHADER_USAGE_POSITION:
+                    ctx->arb1_wrote_position = 1;
+                    usage_str = "result.position";
+                    break;
+                case MOJOSHADER_USAGE_POINTSIZE:
+                    usage_str = "result.pointsize";
+                    break;
+                case MOJOSHADER_USAGE_COLOR:
+                    index_str[0] = '\0';  // no explicit number.
+                    if (index == 0)
+                        usage_str = "result.color.primary";
+                    else if (index == 1)
+                        usage_str = "result.color.secondary";
+                    break;
+                case MOJOSHADER_USAGE_FOG:
+                    usage_str = "result.fogcoord";
+                    break;
+                case MOJOSHADER_USAGE_TEXCOORD:
+                    snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+                    usage_str = "result.texcoord";
+                    arrayleft = "[";
+                    arrayright = "]";
+                    break;
+                default:
+                    // !!! FIXME: we need to deal with some more built-in varyings here.
+                    break;
+            } // switch
+
+            // !!! FIXME: the #define is a little hacky, but it means we don't
+            // !!! FIXME:  have to track these separately if this works.
+            push_output(ctx, &ctx->globals);
+            // no mapping to built-in var? Just make it a regular global, pray.
+            if (usage_str == NULL)
+                output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
+            else
+            {
+                output_line(ctx, "OUTPUT %s = %s%s%s%s;", varname, usage_str,
+                            arrayleft, index_str, arrayright);
+            } // else
+            pop_output(ctx);
+        } // else if
+
+        else
+        {
+            fail(ctx, "unknown vertex shader attribute register");
+        } // else
+    } // if
+
+    else if (shader_is_pixel(ctx))
+    {
+        const char *paramtype_str = "ATTRIB";
+
+        // samplers DCLs get handled in emit_ARB1_sampler().
+
+        if (flags & MOD_CENTROID)
+        {
+            if (!support_nv4(ctx))  // GL_NV_fragment_program4 adds centroid.
+            {
+                // !!! FIXME: should we just wing it without centroid here?
+                failf(ctx, "centroid unsupported in %s profile",
+                      ctx->profile->name);
+                return;
+            } // if
+
+            paramtype_str = "CENTROID ATTRIB";
+        } // if
+
+        if (regtype == REG_TYPE_COLOROUT)
+        {
+            paramtype_str = "OUTPUT";
+            usage_str = "result.color";
+            if (ctx->have_multi_color_outputs)
+            {
+                // We have to gamble that you have GL_ARB_draw_buffers.
+                // You probably do at this point if you have a sane setup.
+                snprintf(index_str, sizeof (index_str), "%u", (uint) regnum);
+                arrayleft = "[";
+                arrayright = "]";
+            } // if
+        } // if
+
+        else if (regtype == REG_TYPE_DEPTHOUT)
+        {
+            paramtype_str = "OUTPUT";
+            usage_str = "result.depth";
+        } // else if
+
+        // !!! FIXME: can you actualy have a texture register with COLOR usage?
+        else if ((regtype == REG_TYPE_TEXTURE) || (regtype == REG_TYPE_INPUT))
+        {
+            if (usage == MOJOSHADER_USAGE_TEXCOORD)
+            {
+                // ps_1_1 does a different hack for this attribute.
+                //  Refer to emit_ARB1_global()'s REG_TYPE_TEXTURE code.
+                if (shader_version_atleast(ctx, 1, 4))
+                {
+                    snprintf(index_str, sizeof (index_str), "%u", (uint) index);
+                    usage_str = "fragment.texcoord";
+                    arrayleft = "[";
+                    arrayright = "]";
+                } // if
+            } // if
+
+            else if (usage == MOJOSHADER_USAGE_COLOR)
+            {
+                index_str[0] = '\0';  // no explicit number.
+                if (index == 0)
+                    usage_str = "fragment.color.primary";
+                else if (index == 1)
+                    usage_str = "fragment.color.secondary";
+                else
+                    fail(ctx, "unsupported color index");
+            } // else if
+        } // else if
+
+        else if (regtype == REG_TYPE_MISCTYPE)
+        {
+            const MiscTypeType mt = (MiscTypeType) regnum;
+            if (mt == MISCTYPE_TYPE_FACE)
+            {
+                if (support_nv4(ctx))  // FINALLY, a vFace equivalent in nv4!
+                {
+                    index_str[0] = '\0';  // no explicit number.
+                    usage_str = "fragment.facing";
+                } // if
+                else
+                {
+                    failf(ctx, "vFace unsupported in %s profile",
+                          ctx->profile->name);
+                } // else
+            } // if
+            else if (mt == MISCTYPE_TYPE_POSITION)
+            {
+                index_str[0] = '\0';  // no explicit number.
+                usage_str = "fragment.position";  // !!! FIXME: is this the same coord space as D3D?
+            } // else if
+            else
+            {
+                fail(ctx, "BUG: unhandled misc register");
+            } // else
+        } // else if
+
+        else
+        {
+            fail(ctx, "unknown pixel shader attribute register");
+        } // else
+
+        if (usage_str != NULL)
+        {
+            push_output(ctx, &ctx->globals);
+            output_line(ctx, "%s %s = %s%s%s%s;", paramtype_str, varname,
+                        usage_str, arrayleft, index_str, arrayright);
+            pop_output(ctx);
+        } // if
+    } // else if
+
+    else
+    {
+        fail(ctx, "Unknown shader type");  // state machine should catch this.
+    } // else
+} // emit_ARB1_attribute
+
+static void emit_ARB1_RESERVED(Context *ctx) { /* no-op. */ }
+
+static void emit_ARB1_NOP(Context *ctx)
+{
+    // There is no NOP in arb1. Just don't output anything here.
+} // emit_ARB1_NOP
+
+EMIT_ARB1_OPCODE_DS_FUNC(MOV)
+EMIT_ARB1_OPCODE_DSS_FUNC(ADD)
+EMIT_ARB1_OPCODE_DSS_FUNC(SUB)
+EMIT_ARB1_OPCODE_DSSS_FUNC(MAD)
+EMIT_ARB1_OPCODE_DSS_FUNC(MUL)
+EMIT_ARB1_OPCODE_DS_FUNC(RCP)
+
+static void emit_ARB1_RSQ(Context *ctx)
+{
+    // nv4 doesn't force abs() on this, so negative values will generate NaN.
+    // The spec says you should force the abs() yourself.
+    if (!support_nv4(ctx))
+    {
+        emit_ARB1_opcode_ds(ctx, "RSQ");  // pre-nv4 implies ABS.
+        return;
+    } // if
+
+    // we can optimize this to use nv2's |abs| construct in some cases.
+    if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
+         (ctx->source_args[0].src_mod == SRCMOD_NEGATE) ||
+         (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
+        ctx->source_args[0].src_mod = SRCMOD_ABS;
+
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+
+    if (ctx->source_args[0].src_mod == SRCMOD_ABS)
+        output_line(ctx, "RSQ%s, %s;", dst, src0);
+    else
+    {
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        output_line(ctx, "ABS %s, %s;", buf, src0);
+        output_line(ctx, "RSQ%s, %s.x;", dst, buf);
+    } // else
+
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_RSQ
+
+EMIT_ARB1_OPCODE_DSS_FUNC(DP3)
+EMIT_ARB1_OPCODE_DSS_FUNC(DP4)
+EMIT_ARB1_OPCODE_DSS_FUNC(MIN)
+EMIT_ARB1_OPCODE_DSS_FUNC(MAX)
+EMIT_ARB1_OPCODE_DSS_FUNC(SLT)
+EMIT_ARB1_OPCODE_DSS_FUNC(SGE)
+
+static void emit_ARB1_EXP(Context *ctx) { emit_ARB1_opcode_ds(ctx, "EX2"); }
+
+static void arb1_log(Context *ctx, const char *opcode)
+{
+    // !!! FIXME: SRCMOD_NEGATE can be made into SRCMOD_ABS here, too
+    // we can optimize this to use nv2's |abs| construct in some cases.
+    if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
+         (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
+        ctx->source_args[0].src_mod = SRCMOD_ABS;
+
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+
+    if (ctx->source_args[0].src_mod == SRCMOD_ABS)
+        output_line(ctx, "%s%s, %s;", opcode, dst, src0);
+    else
+    {
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        output_line(ctx, "ABS %s, %s;", buf, src0);
+        output_line(ctx, "%s%s, %s.x;", opcode, dst, buf);
+    } // else
+
+    emit_ARB1_dest_modifiers(ctx);
+} // arb1_log
+
+
+static void emit_ARB1_LOG(Context *ctx)
+{
+    arb1_log(ctx, "LG2");
+} // emit_ARB1_LOG
+
+
+EMIT_ARB1_OPCODE_DS_FUNC(LIT)
+EMIT_ARB1_OPCODE_DSS_FUNC(DST)
+
+static void emit_ARB1_LRP(Context *ctx)
+{
+    if (shader_is_pixel(ctx))  // fragment shaders have a matching LRP opcode.
+        emit_ARB1_opcode_dsss(ctx, "LRP");
+    else
+    {
+        char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+        char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+
+        // LRP is: dest = src2 + src0 * (src1 - src2)
+        output_line(ctx, "SUB %s, %s, %s;", buf, src1, src2);
+        output_line(ctx, "MAD%s, %s, %s, %s;", dst, buf, src0, src2);
+        emit_ARB1_dest_modifiers(ctx);
+    } // else
+} // emit_ARB1_LRP
+
+EMIT_ARB1_OPCODE_DS_FUNC(FRC)
+
+static void arb1_MxXy(Context *ctx, const int x, const int y)
+{
+    DestArgInfo *dstarg = &ctx->dest_arg;
+    const int origmask = dstarg->writemask;
+    char src0[64];
+    int i;
+
+    make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+
+    for (i = 0; i < y; i++)
+    {
+        char dst[64];
+        char row[64];
+        make_ARB1_srcarg_string(ctx, i + 1, row, sizeof (row));
+        set_dstarg_writemask(dstarg, 1 << i);
+        make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        output_line(ctx, "DP%d%s, %s, %s;", x, dst, src0, row);
+    } // for
+
+    set_dstarg_writemask(dstarg, origmask);
+    emit_ARB1_dest_modifiers(ctx);
+} // arb1_MxXy
+
+static void emit_ARB1_M4X4(Context *ctx) { arb1_MxXy(ctx, 4, 4); }
+static void emit_ARB1_M4X3(Context *ctx) { arb1_MxXy(ctx, 4, 3); }
+static void emit_ARB1_M3X4(Context *ctx) { arb1_MxXy(ctx, 3, 4); }
+static void emit_ARB1_M3X3(Context *ctx) { arb1_MxXy(ctx, 3, 3); }
+static void emit_ARB1_M3X2(Context *ctx) { arb1_MxXy(ctx, 3, 2); }
+
+static void emit_ARB1_CALL(Context *ctx)
+{
+    if (!support_nv2(ctx))  // no branching in stock ARB1.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+        return;
+    } // if
+
+    char labelstr[64];
+    get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
+    output_line(ctx, "CAL %s;", labelstr);
+} // emit_ARB1_CALL
+
+static void emit_ARB1_CALLNZ(Context *ctx)
+{
+    // !!! FIXME: if src1 is a constbool that's true, we can remove the
+    // !!! FIXME:  if. If it's false, we can make this a no-op.
+
+    if (!support_nv2(ctx))  // no branching in stock ARB1.
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    else
+    {
+        // !!! FIXME: double-check this.
+        char labelstr[64];
+        char scratch[64];
+        char src1[64];
+        get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
+        get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1));
+        allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
+        output_line(ctx, "MOVC %s, %s;", scratch, src1);
+        output_line(ctx, "CAL %s (NE.x);", labelstr);
+    } // else
+} // emit_ARB1_CALLNZ
+
+// !!! FIXME: needs BRA in nv2, LOOP in nv2 fragment progs, and REP in nv4.
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(LOOP)
+
+static void emit_ARB1_RET(Context *ctx)
+{
+    // don't fail() if no nv2...maybe we're just ending the mainline?
+    //  if we're ending a LABEL that had no CALL, this would all be written
+    //  to ctx->ignore anyhow, so this should be "safe" ... arb1 profile will
+    //  just end up throwing all this code out.
+    if (support_nv2(ctx))  // no branching in stock ARB1.
+        output_line(ctx, "RET;");
+    set_output(ctx, &ctx->mainline); // in case we were ignoring this function.
+} // emit_ARB1_RET
+
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(ENDLOOP)
+
+static void emit_ARB1_LABEL(Context *ctx)
+{
+    if (!support_nv2(ctx))  // no branching in stock ARB1.
+        return;  // don't fail()...maybe we never use it, but do fail in CALL.
+
+    const int label = ctx->source_args[0].regnum;
+    RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, label);
+
+    // MSDN specs say CALL* has to come before the LABEL, so we know if we
+    //  can ditch the entire function here as unused.
+    if (reg == NULL)
+        set_output(ctx, &ctx->ignore);  // Func not used. Parse, but don't output.
+
+    // !!! FIXME: it would be nice if we could determine if a function is
+    // !!! FIXME:  only called once and, if so, forcibly inline it.
+
+    //const char *uses_loopreg = ((reg) && (reg->misc == 1)) ? "int aL" : "";
+    char labelstr[64];
+    get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
+    output_line(ctx, "%s:", labelstr);
+} // emit_ARB1_LABEL
+
+
+static void emit_ARB1_POW(Context *ctx)
+{
+    // we can optimize this to use nv2's |abs| construct in some cases.
+    if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
+         (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
+        ctx->source_args[0].src_mod = SRCMOD_ABS;
+
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+
+    if (ctx->source_args[0].src_mod == SRCMOD_ABS)
+        output_line(ctx, "POW%s, %s, %s;", dst, src0, src1);
+    else
+    {
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        output_line(ctx, "ABS %s, %s;", buf, src0);
+        output_line(ctx, "POW%s, %s.x, %s;", dst, buf, src1);
+    } // else
+
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_POW
+
+static void emit_ARB1_CRS(Context *ctx) { emit_ARB1_opcode_dss(ctx, "XPD"); }
+
+static void emit_ARB1_SGN(Context *ctx)
+{
+    if (support_nv2(ctx))
+        emit_ARB1_opcode_ds(ctx, "SSG");
+    else
+    {
+        char dst[64];
+        char src0[64];
+        char scratch1[64];
+        char scratch2[64];
+        make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        allocate_ARB1_scratch_reg_name(ctx, scratch1, sizeof (scratch1));
+        allocate_ARB1_scratch_reg_name(ctx, scratch2, sizeof (scratch2));
+        output_line(ctx, "SLT %s, %s, 0.0;", scratch1, src0);
+        output_line(ctx, "SLT %s, -%s, 0.0;", scratch2, src0);
+        output_line(ctx, "ADD%s -%s, %s;", dst, scratch1, scratch2);
+        emit_ARB1_dest_modifiers(ctx);
+    } // else
+} // emit_ARB1_SGN
+
+EMIT_ARB1_OPCODE_DS_FUNC(ABS)
+
+static void emit_ARB1_NRM(Context *ctx)
+{
+    // nv2 fragment programs (and anything nv4) have a real NRM.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        emit_ARB1_opcode_ds(ctx, "NRM");
+    else
+    {
+        char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        output_line(ctx, "DP3 %s.w, %s, %s;", buf, src0, src0);
+        output_line(ctx, "RSQ %s.w, %s.w;", buf, buf);
+        output_line(ctx, "MUL%s, %s.w, %s;", dst, buf, src0);
+        emit_ARB1_dest_modifiers(ctx);
+    } // else
+} // emit_ARB1_NRM
+
+
+static void emit_ARB1_SINCOS(Context *ctx)
+{
+    // we don't care about the temp registers that <= sm2 demands; ignore them.
+    const int mask = ctx->dest_arg.writemask;
+
+    // arb1 fragment programs and everything nv4 have sin/cos/sincos opcodes.
+    if ((shader_is_pixel(ctx)) || (support_nv4(ctx)))
+    {
+        char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        if (writemask_x(mask))
+            output_line(ctx, "COS%s, %s;", dst, src0);
+        else if (writemask_y(mask))
+            output_line(ctx, "SIN%s, %s;", dst, src0);
+        else if (writemask_xy(mask))
+            output_line(ctx, "SCS%s, %s;", dst, src0);
+    } // if
+
+    // nv2+ profiles have sin and cos opcodes.
+    else if (support_nv2(ctx))
+    {
+        char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+        char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        if (writemask_x(mask))
+            output_line(ctx, "COS %s.x, %s;", dst, src0);
+        else if (writemask_y(mask))
+            output_line(ctx, "SIN %s.y, %s;", dst, src0);
+        else if (writemask_xy(mask))
+        {
+            output_line(ctx, "SIN %s.x, %s;", dst, src0);
+            output_line(ctx, "COS %s.y, %s;", dst, src0);
+        } // else if
+    } // if
+
+    else  // big nasty.
+    {
+        char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+        char src0[64]; get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
+        const int need_sin = (writemask_x(mask) || writemask_xy(mask));
+        const int need_cos = (writemask_y(mask) || writemask_xy(mask));
+        char scratch[64];
+
+        if (need_sin || need_cos)
+            allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
+
+        // These sin() and cos() approximations originally found here:
+        //    http://www.devmaster.net/forums/showthread.php?t=5784
+        //
+        // const float B = 4.0f / M_PI;
+        // const float C = -4.0f / (M_PI * M_PI);
+        // float y = B * x + C * x * fabs(x);
+        //
+        // // optional better precision...
+        // const float P = 0.225f;
+        // y = P * (y * fabs(y) - y) + y;
+        //
+        //
+        // That first thing can be reduced to:
+        // const float y = ((1.2732395447351626861510701069801f * x) +
+        //             ((-0.40528473456935108577551785283891f * x) * fabs(x)));
+
+        if (need_sin)
+        {
+            // !!! FIXME: use SRCMOD_ABS here?
+            output_line(ctx, "ABS %s.x, %s.x;", dst, src0);
+            output_line(ctx, "MUL %s.x, %s.x, -0.40528473456935108577551785283891;", dst, dst);
+            output_line(ctx, "MUL %s.x, %s.x, 1.2732395447351626861510701069801;", scratch, src0);
+            output_line(ctx, "MAD %s.x, %s.x, %s.x, %s.x;", dst, dst, src0, scratch);
+        } // if
+
+        // cosine is sin(x + M_PI/2), but you have to wrap x to pi:
+        //  if (x+(M_PI/2) > M_PI)
+        //      x -= 2 * M_PI;
+        //
+        // which is...
+        //  if (x+(1.57079637050628662109375) > 3.1415927410125732421875)
+        //      x += -6.283185482025146484375;
+
+        if (need_cos)
+        {
+            output_line(ctx, "ADD %s.x, %s.x, 1.57079637050628662109375;", scratch, src0);
+            output_line(ctx, "SGE %s.y, %s.x, 3.1415927410125732421875;", scratch, scratch);
+            output_line(ctx, "MAD %s.x, %s.y, -6.283185482025146484375, %s.x;", scratch, scratch, scratch);
+            output_line(ctx, "ABS %s.x, %s.x;", dst, src0);
+            output_line(ctx, "MUL %s.x, %s.x, -0.40528473456935108577551785283891;", dst, dst);
+            output_line(ctx, "MUL %s.x, %s.x, 1.2732395447351626861510701069801;", scratch, src0);
+            output_line(ctx, "MAD %s.y, %s.x, %s.x, %s.x;", dst, dst, src0, scratch);
+        } // if
+    } // else
+
+    // !!! FIXME: might not have done anything. Don't emit if we didn't.
+    if (!isfail(ctx))
+        emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_SINCOS
+
+
+static void emit_ARB1_REP(Context *ctx)
+{
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+
+    // nv2 fragment programs (and everything nv4) have a real REP.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        output_line(ctx, "REP %s;", src0);
+
+    else if (support_nv2(ctx))
+    {
+        // no REP, but we can use branches.
+        char failbranch[32];
+        char topbranch[32];
+        const int toplabel = allocate_branch_label(ctx);
+        const int faillabel = allocate_branch_label(ctx);
+        get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
+        get_ARB1_branch_label_name(ctx,toplabel,topbranch,sizeof(topbranch));
+
+        assert(((size_t) ctx->branch_labels_stack_index) <
+                STATICARRAYLEN(ctx->branch_labels_stack)-1);
+
+        ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = toplabel;
+        ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = faillabel;
+
+        char scratch[32];
+        snprintf(scratch, sizeof (scratch), "rep%d", ctx->reps);
+        output_line(ctx, "MOVC %s.x, %s;", scratch, src0);
+        output_line(ctx, "BRA %s (LE.x);", failbranch);
+        output_line(ctx, "%s:", topbranch);
+    } // else if
+
+    else  // stock ARB1 has no branching.
+    {
+        fail(ctx, "branching unsupported in this profile");
+    } // else
+} // emit_ARB1_REP
+
+
+static void emit_ARB1_ENDREP(Context *ctx)
+{
+    // nv2 fragment programs (and everything nv4) have a real ENDREP.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        output_line(ctx, "ENDREP;");
+
+    else if (support_nv2(ctx))
+    {
+        // no ENDREP, but we can use branches.
+        assert(ctx->branch_labels_stack_index >= 2);
+
+        char failbranch[32];
+        char topbranch[32];
+        const int faillabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
+        const int toplabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
+        get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
+        get_ARB1_branch_label_name(ctx,toplabel,topbranch,sizeof(topbranch));
+
+        char scratch[32];
+        snprintf(scratch, sizeof (scratch), "rep%d", ctx->reps);
+        output_line(ctx, "SUBC %s.x, %s.x, 1.0;", scratch, scratch);
+        output_line(ctx, "BRA %s (GT.x);", topbranch);
+        output_line(ctx, "%s:", failbranch);
+    } // else if
+
+    else  // stock ARB1 has no branching.
+    {
+        fail(ctx, "branching unsupported in this profile");
+    } // else
+} // emit_ARB1_ENDREP
+
+
+static void nv2_if(Context *ctx)
+{
+    // The condition code register MUST be set up before this!
+    // nv2 fragment programs (and everything nv4) have a real IF.
+    if ( (support_nv4(ctx)) || (shader_is_pixel(ctx)) )
+        output_line(ctx, "IF EQ.x;");
+    else
+    {
+        // there's no IF construct, but we can use a branch to a label.
+        char failbranch[32];
+        const int label = allocate_branch_label(ctx);
+        get_ARB1_branch_label_name(ctx, label, failbranch, sizeof (failbranch));
+
+        assert(((size_t) ctx->branch_labels_stack_index)
+                 < STATICARRAYLEN(ctx->branch_labels_stack));
+
+        ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = label;
+
+        // !!! FIXME: should this be NE? (EQ would jump to the ELSE for the IF condition, right?).
+        output_line(ctx, "BRA %s (EQ.x);", failbranch);
+    } // else
+} // nv2_if
+
+
+static void emit_ARB1_IF(Context *ctx)
+{
+    if (support_nv2(ctx))
+    {
+        char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
+        char src0[64]; get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
+        output_line(ctx, "MOVC %s.x, %s;", buf, src0);
+        nv2_if(ctx);
+    } // if
+
+    else  // stock ARB1 has no branching.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    } // else
+} // emit_ARB1_IF
+
+
+static void emit_ARB1_ELSE(Context *ctx)
+{
+    // nv2 fragment programs (and everything nv4) have a real ELSE.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        output_line(ctx, "ELSE;");
+
+    else if (support_nv2(ctx))
+    {
+        // there's no ELSE construct, but we can use a branch to a label.
+        assert(ctx->branch_labels_stack_index > 0);
+
+        // At the end of the IF block, unconditionally jump to the ENDIF.
+        const int endlabel = allocate_branch_label(ctx);
+        char endbranch[32];
+        get_ARB1_branch_label_name(ctx,endlabel,endbranch,sizeof (endbranch));
+        output_line(ctx, "BRA %s;", endbranch);
+
+        // Now mark the ELSE section with a lable.
+        const int elselabel = ctx->branch_labels_stack[ctx->branch_labels_stack_index-1];
+        char elsebranch[32];
+        get_ARB1_branch_label_name(ctx,elselabel,elsebranch,sizeof(elsebranch));
+        output_line(ctx, "%s:", elsebranch);
+
+        // Replace the ELSE label with the ENDIF on the label stack.
+        ctx->branch_labels_stack[ctx->branch_labels_stack_index-1] = endlabel;
+    } // else if
+
+    else  // stock ARB1 has no branching.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    } // else
+} // emit_ARB1_ELSE
+
+
+static void emit_ARB1_ENDIF(Context *ctx)
+{
+    // nv2 fragment programs (and everything nv4) have a real ENDIF.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        output_line(ctx, "ENDIF;");
+
+    else if (support_nv2(ctx))
+    {
+        // there's no ENDIF construct, but we can use a branch to a label.
+        assert(ctx->branch_labels_stack_index > 0);
+        const int endlabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
+        char endbranch[32];
+        get_ARB1_branch_label_name(ctx,endlabel,endbranch,sizeof (endbranch));
+        output_line(ctx, "%s:", endbranch);
+    } // if
+
+    else  // stock ARB1 has no branching.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    } // else
+} // emit_ARB1_ENDIF
+
+
+static void emit_ARB1_BREAK(Context *ctx)
+{
+    // nv2 fragment programs (and everything nv4) have a real BREAK.
+    if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
+        output_line(ctx, "BRK;");
+
+    else if (support_nv2(ctx))
+    {
+        // no BREAK, but we can use branches.
+        assert(ctx->branch_labels_stack_index >= 2);
+        const int faillabel = ctx->branch_labels_stack[ctx->branch_labels_stack_index];
+        char failbranch[32];
+        get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
+        output_line(ctx, "BRA %s;", failbranch);
+    } // else if
+
+    else  // stock ARB1 has no branching.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    } // else
+} // emit_ARB1_BREAK
+
+
+static void emit_ARB1_MOVA(Context *ctx)
+{
+    // nv2 and nv3 can use the ARR opcode.
+    // But nv4 removed ARR (and ADDRESS registers!). Just ROUND to an INT.
+    if (support_nv4(ctx))
+        emit_ARB1_opcode_ds(ctx, "ROUND.S");  // !!! FIXME: don't use a modifier here.
+    else if ((support_nv2(ctx)) || (support_nv3(ctx)))
+        emit_ARB1_opcode_ds(ctx, "ARR");
+    else
+    {
+        char src0[64];
+        char scratch[64];
+        char addr[32];
+
+        make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
+        snprintf(addr, sizeof (addr), "addr%d", ctx->dest_arg.regnum);
+
+        // !!! FIXME: we can optimize this if src_mod is ABS or ABSNEGATE.
+
+        // ARL uses floor(), but D3D expects round-to-nearest.
+        // There is probably a more efficient way to do this.
+        if (shader_is_pixel(ctx))  // CMP only exists in fragment programs.  :/
+            output_line(ctx, "CMP %s, %s, -1.0, 1.0;", scratch, src0);
+        else
+        {
+            output_line(ctx, "SLT %s, %s, 0.0;", scratch, src0);
+            output_line(ctx, "MAD %s, %s, -2.0, 1.0;", scratch, scratch);
+        } // else
+
+        output_line(ctx, "ABS %s, %s;", addr, src0);
+        output_line(ctx, "ADD %s, %s, 0.5;", addr, addr);
+        output_line(ctx, "FLR %s, %s;", addr, addr);
+        output_line(ctx, "MUL %s, %s, %s;", addr, addr, scratch);
+
+        // we don't handle these right now, since emit_ARB1_dest_modifiers(ctx)
+        //  wants to look at dest_arg, not our temp register.
+        assert(ctx->dest_arg.result_mod == 0);
+        assert(ctx->dest_arg.result_shift == 0);
+
+        // we assign to the actual address register as needed.
+        ctx->last_address_reg_component = -1;
+    } // else
+} // emit_ARB1_MOVA
+
+
+static void emit_ARB1_TEXKILL(Context *ctx)
+{
+    // d3d kills on xyz, arb1 kills on xyzw. Fix the swizzle.
+    //  We just map the x component to w. If it's negative, the fragment
+    //  would discard anyhow, otherwise, it'll pass through okay. This saves
+    //  us a temp register.
+    char dst[64];
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+    output_line(ctx, "KIL %s.xyzx;", dst);
+} // emit_ARB1_TEXKILL
+
+static void arb1_texbem(Context *ctx, const int luminance)
+{
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    const int stage = ctx->dest_arg.regnum;
+    char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+    char src[64]; get_ARB1_srcarg_varname(ctx, 0, src, sizeof (src));
+    char tmp[64]; allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
+    char sampler[64];
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage,
+                            sampler, sizeof (sampler));
+
+    output_line(ctx, "MUL %s, %s_texbem.xzyw, %s.xyxy;", tmp, sampler, src);
+    output_line(ctx, "ADD %s.xy, %s.xzxx, %s.ywxx;", tmp, tmp, tmp);
+    output_line(ctx, "ADD %s.xy, %s, %s;", tmp, tmp, dst);
+    output_line(ctx, "TEX %s, %s, texture[%d], 2D;", dst, tmp, stage);
+
+    if (luminance)  // TEXBEML, not just TEXBEM?
+    {
+        output_line(ctx, "MAD %s, %s.zzzz, %s_texbeml.xxxx, %s_texbeml.yyyy;",
+                    tmp, src, sampler, sampler);
+        output_line(ctx, "MUL %s, %s, %s;", dst, dst, tmp);
+    } // if
+
+    emit_ARB1_dest_modifiers(ctx);
+} // arb1_texbem
+
+static void emit_ARB1_TEXBEM(Context *ctx)
+{
+    arb1_texbem(ctx, 0);
+} // emit_ARB1_TEXBEM
+
+static void emit_ARB1_TEXBEML(Context *ctx)
+{
+    arb1_texbem(ctx, 1);
+} // emit_ARB1_TEXBEML
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR)
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB)
+
+
+static void emit_ARB1_TEXM3X2PAD(Context *ctx)
+{
+    // no-op ... work happens in emit_ARB1_TEXM3X2TEX().
+} // emit_ARB1_TEXM3X2PAD
+
+static void emit_ARB1_TEXM3X2TEX(Context *ctx)
+{
+    if (ctx->texm3x2pad_src0 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    const int stage = ctx->dest_arg.regnum;
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0,
+                            src0, sizeof (src0));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0,
+                            src1, sizeof (src1));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src2, sizeof (src2));
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+
+    output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, dst);
+    output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
+    output_line(ctx, "TEX %s, %s, texture[%d], 2D;", dst, dst, stage);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_TEXM3X2TEX
+
+
+static void emit_ARB1_TEXM3X3PAD(Context *ctx)
+{
+    // no-op ... work happens in emit_ARB1_TEXM3X3*().
+} // emit_ARB1_TEXM3X3PAD
+
+
+static void emit_ARB1_TEXM3X3TEX(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    const int stage = ctx->dest_arg.regnum;
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
+
+    output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
+    output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
+    output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
+    output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, dst, stage, ttypestr);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_TEXM3X3TEX
+
+static void emit_ARB1_TEXM3X3SPEC(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char src5[64];
+    char tmp[64];
+    char tmp2[64];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    const int stage = ctx->dest_arg.regnum;
+    allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
+    allocate_ARB1_scratch_reg_name(ctx, tmp2, sizeof (tmp2));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[1].regnum,
+                            src5, sizeof (src5));
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
+
+    output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
+    output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
+    output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
+    output_line(ctx, "MUL %s, %s, %s;", tmp, dst, dst);    // normal * normal
+    output_line(ctx, "MUL %s, %s, %s;", tmp2, dst, src5);  // normal * eyeray
+
+    // !!! FIXME: This is goofy. There's got to be a way to do vector-wide
+    // !!! FIXME:  divides or reciprocals...right?
+    output_line(ctx, "RCP %s.x, %s.x;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.y, %s.y;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.z, %s.z;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.w, %s.w;", tmp2, tmp2);
+    output_line(ctx, "MUL %s, %s, %s;", tmp, tmp, tmp2);
+
+    output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };", tmp, tmp);
+    output_line(ctx, "MAD %s, %s, %s, -%s;", tmp, tmp, dst, src5);
+    output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, tmp, stage, ttypestr);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_TEXM3X3SPEC
+
+static void emit_ARB1_TEXM3X3VSPEC(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+    char tmp[64];
+    char tmp2[64];
+    char tmp3[64];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    const int stage = ctx->dest_arg.regnum;
+    allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
+    allocate_ARB1_scratch_reg_name(ctx, tmp2, sizeof (tmp2));
+    allocate_ARB1_scratch_reg_name(ctx, tmp3, sizeof (tmp3));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+    const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
+
+    output_line(ctx, "MOV %s.x, %s.w;", tmp3, src0);
+    output_line(ctx, "MOV %s.y, %s.w;", tmp3, src2);
+    output_line(ctx, "MOV %s.z, %s.w;", tmp3, dst);
+    output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
+    output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
+    output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
+    output_line(ctx, "MUL %s, %s, %s;", tmp, dst, dst);    // normal * normal
+    output_line(ctx, "MUL %s, %s, %s;", tmp2, dst, tmp3);  // normal * eyeray
+
+    // !!! FIXME: This is goofy. There's got to be a way to do vector-wide
+    // !!! FIXME:  divides or reciprocals...right?
+    output_line(ctx, "RCP %s.x, %s.x;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.y, %s.y;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.z, %s.z;", tmp2, tmp2);
+    output_line(ctx, "RCP %s.w, %s.w;", tmp2, tmp2);
+    output_line(ctx, "MUL %s, %s, %s;", tmp, tmp, tmp2);
+
+    output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };", tmp, tmp);
+    output_line(ctx, "MAD %s, %s, %s, -%s;", tmp, tmp, dst, tmp3);
+    output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, tmp, stage, ttypestr);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_TEXM3X3VSPEC
+
+static void emit_ARB1_EXPP(Context *ctx) { emit_ARB1_opcode_ds(ctx, "EX2"); }
+static void emit_ARB1_LOGP(Context *ctx) { arb1_log(ctx, "LG2"); }
+
+static void emit_ARB1_CND(Context *ctx)
+{
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+    char tmp[64]; allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
+
+    // CND compares against 0.5, but we need to compare against 0.0...
+    //  ...subtract to make up the difference.
+    output_line(ctx, "SUB %s, %s, { 0.5, 0.5, 0.5, 0.5 };", tmp, src0);
+    // D3D tests (src0 >= 0.0), but ARB1 tests (src0 < 0.0) ... so just
+    //  switch src1 and src2 to get the same results.
+    output_line(ctx, "CMP%s, %s, %s, %s;", dst, tmp, src2, src1);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_CND
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB)
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX)
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH)
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3)
+
+static void emit_ARB1_TEXM3X3(Context *ctx)
+{
+    if (ctx->texm3x3pad_src1 == -1)
+        return;
+
+    char dst[64];
+    char src0[64];
+    char src1[64];
+    char src2[64];
+    char src3[64];
+    char src4[64];
+
+    // !!! FIXME: this code counts on the register not having swizzles, etc.
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
+                            src0, sizeof (src0));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
+                            src1, sizeof (src1));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
+                            src2, sizeof (src2));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
+                            src3, sizeof (src3));
+    get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
+                            src4, sizeof (src4));
+    get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+
+    output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
+    output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
+    output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
+    output_line(ctx, "MOV %s.w, { 1.0, 1.0, 1.0, 1.0 };", dst);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_TEXM3X3
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH)
+
+static void emit_ARB1_CMP(Context *ctx)
+{
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+    char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+    char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+    char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+    // D3D tests (src0 >= 0.0), but ARB1 tests (src0 < 0.0) ... so just
+    //  switch src1 and src2 to get the same results.
+    output_line(ctx, "CMP%s, %s, %s, %s;", dst, src0, src2, src1);
+    emit_ARB1_dest_modifiers(ctx);
+} // emit_ARB1_CMP
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BEM)
+
+
+static void emit_ARB1_DP2ADD(Context *ctx)
+{
+    if (support_nv4(ctx))  // nv4 has a built-in equivalent to DP2ADD.
+        emit_ARB1_opcode_dsss(ctx, "DP2A");
+    else
+    {
+        char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+        char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
+        char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
+        char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+        char scratch[64];
+
+        // DP2ADD is:
+        //  dst = (src0.r * src1.r) + (src0.g * src1.g) + src2.replicate_swiz
+        allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
+        output_line(ctx, "MUL %s, %s, %s;", scratch, src0, src1);
+        output_line(ctx, "ADD %s, %s.x, %s.y;", scratch, scratch, scratch);
+        output_line(ctx, "ADD%s, %s.x, %s;", dst, scratch, src2);
+        emit_ARB1_dest_modifiers(ctx);
+    } // else
+} // emit_ARB1_DP2ADD
+
+
+static void emit_ARB1_DSX(Context *ctx)
+{
+    if (support_nv2(ctx))  // nv2 has a built-in equivalent to DSX.
+        emit_ARB1_opcode_ds(ctx, "DDX");
+    else
+        failf(ctx, "DSX unsupported in %s profile", ctx->profile->name);
+} // emit_ARB1_DSX
+
+
+static void emit_ARB1_DSY(Context *ctx)
+{
+    if (support_nv2(ctx))  // nv2 has a built-in equivalent to DSY.
+        emit_ARB1_opcode_ds(ctx, "DDY");
+    else
+        failf(ctx, "DSY unsupported in %s profile", ctx->profile->name);
+} // emit_ARB1_DSY
+
+static void arb1_texld(Context *ctx, const char *opcode, const int texldd)
+{
+    // !!! FIXME: Hack: "TEXH" is invalid in nv4. Fix this more cleanly.
+    if ((ctx->dest_arg.result_mod & MOD_PP) && (support_nv4(ctx)))
+        ctx->dest_arg.result_mod &= ~MOD_PP;
+
+    char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
+
+    const int sm1 = !shader_version_atleast(ctx, 1, 4);
+    const int regnum = sm1 ? ctx->dest_arg.regnum : ctx->source_args[1].regnum;
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, regnum);
+
+    const char *ttype = NULL;
+    char src0[64];
+    if (sm1)
+        get_ARB1_destarg_varname(ctx, src0, sizeof (src0));
+    else
+        get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
+    //char src1[64]; get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1));  // !!! FIXME: SRC_MOD?
+
+    char src2[64] = { 0 };
+    char src3[64] = { 0 };
+
+    if (texldd)
+    {
+        make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
+        make_ARB1_srcarg_string(ctx, 3, src3, sizeof (src3));
+    } // if
+
+    // !!! FIXME: this should be in state_TEXLD, not in the arb1/glsl emitters.
+    if (sreg == NULL)
+    {
+        fail(ctx, "TEXLD using undeclared sampler");
+        return;
+    } // if
+
+    // SM1 only specifies dst, so don't check swizzle there.
+    if ( !sm1 && (!no_swizzle(ctx->source_args[1].swizzle)) )
+    {
+        // !!! FIXME: does this ever actually happen?
+        fail(ctx, "BUG: can't handle TEXLD with sampler swizzle at the moment");
+    } // if
+
+    switch ((const TextureType) sreg->index)
+    {
+        case TEXTURE_TYPE_2D: ttype = "2D"; break; // !!! FIXME: "RECT"?
+        case TEXTURE_TYPE_CUBE: ttype = "CUBE"; break;
+        case TEXTURE_TYPE_VOLUME: ttype = "3D"; break;
+        default: fail(ctx, "unknown texture type"); return;
+    } // switch
+
+    if (texldd)
+    {
+        output_line(ctx, "%s%s, %s, %s, %s, texture[%d], %s;", opcode, dst,
+                    src0, src2, src3, regnum, ttype);
+    } // if
+    else
+    {
+        output_line(ctx, "%s%s, %s, texture[%d], %s;", opcode, dst, src0,
+                    regnum, ttype);
+    } // else
+} // arb1_texld
+
+
+static void emit_ARB1_TEXLDD(Context *ctx)
+{
+    // With GL_NV_fragment_program2, we can use the TXD opcode.
+    //  In stock arb1, we can settle for a standard texld, which isn't
+    //  perfect, but oh well.
+    if (support_nv2(ctx))
+        arb1_texld(ctx, "TXD", 1);
+    else
+        arb1_texld(ctx, "TEX", 0);
+} // emit_ARB1_TEXLDD
+
+
+static void emit_ARB1_TEXLDL(Context *ctx)
+{
+    if ((shader_is_vertex(ctx)) && (!support_nv3(ctx)))
+    {
+        failf(ctx, "Vertex shader TEXLDL unsupported in %s profile",
+              ctx->profile->name);
+        return;
+    } // if
+
+    else if ((shader_is_pixel(ctx)) && (!support_nv2(ctx)))
+    {
+        failf(ctx, "Pixel shader TEXLDL unsupported in %s profile",
+              ctx->profile->name);
+        return;
+    } // if
+
+    // !!! FIXME: this doesn't map exactly to TEXLDL. Review this.
+    arb1_texld(ctx, "TXL", 0);
+} // emit_ARB1_TEXLDL
+
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BREAKP)
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BREAKC)
+
+static void emit_ARB1_IFC(Context *ctx)
+{
+    if (support_nv2(ctx))
+    {
+        static const char *comps[] = {
+            "", "SGTC", "SEQC", "SGEC", "SGTC", "SNEC", "SLEC"
+        };
+
+        if (ctx->instruction_controls >= STATICARRAYLEN(comps))
+        {
+            fail(ctx, "unknown comparison control");
+            return;
+        } // if
+
+        char src0[64];
+        char src1[64];
+        char scratch[64];
+
+        const char *comp = comps[ctx->instruction_controls];
+        get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
+        get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1));
+        allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
+        output_line(ctx, "%s %s.x, %s, %s;", comp, scratch, src0, src1);
+        nv2_if(ctx);
+    } // if
+
+    else  // stock ARB1 has no branching.
+    {
+        failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
+    } // else
+} // emit_ARB1_IFC
+
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(SETP)
+
+static void emit_ARB1_DEF(Context *ctx)
+{
+    const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
+    char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+    char val0[32]; floatstr(ctx, val0, sizeof (val0), val[0], 1);
+    char val1[32]; floatstr(ctx, val1, sizeof (val1), val[1], 1);
+    char val2[32]; floatstr(ctx, val2, sizeof (val2), val[2], 1);
+    char val3[32]; floatstr(ctx, val3, sizeof (val3), val[3], 1);
+
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "PARAM %s = { %s, %s, %s, %s };",
+                dst, val0, val1, val2, val3);
+    pop_output(ctx);
+} // emit_ARB1_DEF
+
+static void emit_ARB1_DEFI(Context *ctx)
+{
+    char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+    const int32 *x = (const int32 *) ctx->dwords;
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "PARAM %s = { %d, %d, %d, %d };",
+                dst, (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
+    pop_output(ctx);
+} // emit_ARB1_DEFI
+
+static void emit_ARB1_DEFB(Context *ctx)
+{
+    char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
+    push_output(ctx, &ctx->globals);
+    output_line(ctx, "PARAM %s = %d;", dst, ctx->dwords[0] ? 1 : 0);
+    pop_output(ctx);
+} // emit_ARB1_DEFB
+
+static void emit_ARB1_DCL(Context *ctx)
+{
+    // no-op. We do this in our emit_attribute() and emit_uniform().
+} // emit_ARB1_DCL
+
+EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD)
+
+static void emit_ARB1_TEXLD(Context *ctx)
+{
+    if (!shader_version_atleast(ctx, 1, 4))
+    {
+        arb1_texld(ctx, "TEX", 0);
+        return;
+    } // if
+
+    else if (!shader_version_atleast(ctx, 2, 0))
+    {
+        // ps_1_4 is different, too!
+        fail(ctx, "TEXLD == Shader Model 1.4 unimplemented.");  // !!! FIXME
+        return;
+    } // if
+
+    // !!! FIXME: do texldb and texldp map between OpenGL and D3D correctly?
+    if (ctx->instruction_controls == CONTROL_TEXLD)
+        arb1_texld(ctx, "TEX", 0);
+    else if (ctx->instruction_controls == CONTROL_TEXLDP)
+        arb1_texld(ctx, "TXP", 0);
+    else if (ctx->instruction_controls == CONTROL_TEXLDB)
+        arb1_texld(ctx, "TXB", 0);
+} // emit_ARB1_TEXLD
+
+#endif  // SUPPORT_PROFILE_ARB1
+
+
+#if !AT_LEAST_ONE_PROFILE
+#error No profiles are supported. Fix your build.
+#endif
+
+#define DEFINE_PROFILE(prof) { \
+    MOJOSHADER_PROFILE_##prof, \
+    emit_##prof##_start, \
+    emit_##prof##_end, \
+    emit_##prof##_phase, \
+    emit_##prof##_global, \
+    emit_##prof##_array, \
+    emit_##prof##_const_array, \
+    emit_##prof##_uniform, \
+    emit_##prof##_sampler, \
+    emit_##prof##_attribute, \
+    emit_##prof##_finalize, \
+    get_##prof##_varname, \
+    get_##prof##_const_array_varname, \
+},
+
+static const Profile profiles[] =
+{
+#if SUPPORT_PROFILE_D3D
+    DEFINE_PROFILE(D3D)
+#endif
+#if SUPPORT_PROFILE_BYTECODE
+    DEFINE_PROFILE(BYTECODE)
+#endif
+#if SUPPORT_PROFILE_GLSL
+    DEFINE_PROFILE(GLSL)
+#endif
+#if SUPPORT_PROFILE_ARB1
+    DEFINE_PROFILE(ARB1)
+#endif
+};
+
+#undef DEFINE_PROFILE
+
+// This is for profiles that extend other profiles...
+static const struct { const char *from; const char *to; } profileMap[] =
+{
+    { MOJOSHADER_PROFILE_GLSL120, MOJOSHADER_PROFILE_GLSL },
+    { MOJOSHADER_PROFILE_NV2, MOJOSHADER_PROFILE_ARB1 },
+    { MOJOSHADER_PROFILE_NV3, MOJOSHADER_PROFILE_ARB1 },
+    { MOJOSHADER_PROFILE_NV4, MOJOSHADER_PROFILE_ARB1 },
+};
+
+
+// The PROFILE_EMITTER_* items MUST be in the same order as profiles[]!
+#define PROFILE_EMITTERS(op) { \
+     PROFILE_EMITTER_D3D(op) \
+     PROFILE_EMITTER_BYTECODE(op) \
+     PROFILE_EMITTER_GLSL(op) \
+     PROFILE_EMITTER_ARB1(op) \
+}
+
+static int parse_destination_token(Context *ctx, DestArgInfo *info)
+{
+    // !!! FIXME: recheck against the spec for ranges (like RASTOUT values, etc).
+    if (ctx->tokencount == 0)
+    {
+        fail(ctx, "Out of tokens in destination parameter");
+        return 0;
+    } // if
+
+    const uint32 token = SWAP32(*(ctx->tokens));
+    const int reserved1 = (int) ((token >> 14) & 0x3); // bits 14 through 15
+    const int reserved2 = (int) ((token >> 31) & 0x1); // bit 31
+
+    info->token = ctx->tokens;
+    info->regnum = (int) (token & 0x7ff);  // bits 0 through 10
+    info->relative = (int) ((token >> 13) & 0x1); // bit 13
+    info->orig_writemask = (int) ((token >> 16) & 0xF); // bits 16 through 19
+    info->result_mod = (int) ((token >> 20) & 0xF); // bits 20 through 23
+    info->result_shift = (int) ((token >> 24) & 0xF); // bits 24 through 27      abc
+    info->regtype = (RegisterType) (((token >> 28) & 0x7) | ((token >> 8) & 0x18));  // bits 28-30, 11-12
+
+    int writemask;
+    if (isscalar(ctx, ctx->shader_type, info->regtype, info->regnum))
+        writemask = 0x1;  // just x.
+    else
+        writemask = info->orig_writemask;
+
+    set_dstarg_writemask(info, writemask);  // bits 16 through 19.
+
+    // all the REG_TYPE_CONSTx types are the same register type, it's just
+    //  split up so its regnum can be > 2047 in the bytecode. Clean it up.
+    if (info->regtype == REG_TYPE_CONST2)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 2048;
+    } // else if
+    else if (info->regtype == REG_TYPE_CONST3)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 4096;
+    } // else if
+    else if (info->regtype == REG_TYPE_CONST4)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 6144;
+    } // else if
+
+    // swallow token for now, for multiple calls in a row.
+    adjust_token_position(ctx, 1);
+
+    if (reserved1 != 0x0)
+        fail(ctx, "Reserved bit #1 in destination token must be zero");
+
+    if (reserved2 != 0x1)
+        fail(ctx, "Reserved bit #2 in destination token must be one");
+
+    if (info->relative)
+    {
+        if (!shader_is_vertex(ctx))
+            fail(ctx, "Relative addressing in non-vertex shader");
+        if (!shader_version_atleast(ctx, 3, 0))
+            fail(ctx, "Relative addressing in vertex shader version < 3.0");
+        if ((!ctx->ctab.have_ctab) && (!ctx->ignores_ctab))
+        {
+            // it's hard to do this efficiently without!
+            fail(ctx, "relative addressing unsupported without a CTAB");
+        } // if
+
+        // !!! FIXME: I don't have a shader that has a relative dest currently.
+        fail(ctx, "Relative addressing of dest tokens is unsupported");
+        return 2;
+    } // if
+
+    const int s = info->result_shift;
+    if (s != 0)
+    {
+        if (!shader_is_pixel(ctx))
+            fail(ctx, "Result shift scale in non-pixel shader");
+        if (shader_version_atleast(ctx, 2, 0))
+            fail(ctx, "Result shift scale in pixel shader version >= 2.0");
+        if ( ! (((s >= 1) && (s <= 3)) || ((s >= 0xD) && (s <= 0xF))) )
+            fail(ctx, "Result shift scale isn't 1 to 3, or 13 to 15.");
+    } // if
+
+    if (info->result_mod & MOD_PP)  // Partial precision (pixel shaders only)
+    {
+        if (!shader_is_pixel(ctx))
+            fail(ctx, "Partial precision result mod in non-pixel shader");
+    } // if
+
+    if (info->result_mod & MOD_CENTROID)  // Centroid (pixel shaders only)
+    {
+        if (!shader_is_pixel(ctx))
+            fail(ctx, "Centroid result mod in non-pixel shader");
+        else if (!ctx->centroid_allowed)  // only on DCL opcodes!
+            fail(ctx, "Centroid modifier not allowed here");
+    } // if
+
+    if ((info->regtype < 0) || (info->regtype > REG_TYPE_MAX))
+        fail(ctx, "Register type is out of range");
+
+    if (!isfail(ctx))
+        set_used_register(ctx, info->regtype, info->regnum, 1);
+
+    return 1;
+} // parse_destination_token
+
+
+static void determine_constants_arrays(Context *ctx)
+{
+    // Only process this stuff once. This is called after all DEF* opcodes
+    //  could have been parsed.
+    if (ctx->determined_constants_arrays)
+        return;
+
+    ctx->determined_constants_arrays = 1;
+
+    if (ctx->constant_count <= 1)
+        return;  // nothing to sort or group.
+
+    // Sort the linked list into an array for easier tapdancing...
+    ConstantsList **array = (ConstantsList **) alloca(sizeof (ConstantsList *) * (ctx->constant_count + 1));
+    ConstantsList *item = ctx->constants;
+    int i;
+
+    for (i = 0; i < ctx->constant_count; i++)
+    {
+        if (item == NULL)
+        {
+            fail(ctx, "BUG: mismatched constant list and count");
+            return;
+        } // if
+
+        array[i] = item;
+        item = item->next;
+    } // for
+
+    array[ctx->constant_count] = NULL;
+
+    // bubble sort ftw.
+    int sorted;
+    do
+    {
+        sorted = 1;
+        for (i = 0; i < ctx->constant_count-1; i++)
+        {
+            if (array[i]->constant.index > array[i+1]->constant.index)
+            {
+                ConstantsList *tmp = array[i];
+                array[i] = array[i+1];
+                array[i+1] = tmp;
+                sorted = 0;
+            } // if
+        } // for
+    } while (!sorted);
+
+    // okay, sorted. While we're here, let's redo the linked list in order...
+    for (i = 0; i < ctx->constant_count; i++)
+        array[i]->next = array[i+1];
+    ctx->constants = array[0];
+
+    // now figure out the groupings of constants and add to ctx->variables...
+    int start = -1;
+    int prev = -1;
+    int count = 0;
+    const int hi = ctx->constant_count;
+    for (i = 0; i <= hi; i++)
+    {
+        if (array[i] && (array[i]->constant.type != MOJOSHADER_UNIFORM_FLOAT))
+            continue;  // we only care about REG_TYPE_CONST for array groups.
+
+        if (start == -1)
+        {
+            prev = start = i;  // first REG_TYPE_CONST we've seen. Mark it!
+            continue;
+        } // if
+
+        // not a match (or last item in the array)...see if we had a
+        //  contiguous set before this point...
+        if ( (array[i]) && (array[i]->constant.index == (array[prev]->constant.index + 1)) )
+            count++;
+        else
+        {
+            if (count > 0)  // multiple constants in the set?
+            {
+                VariableList *var;
+                var = (VariableList *) Malloc(ctx, sizeof (VariableList));
+                if (var == NULL)
+                    break;
+
+                var->type = MOJOSHADER_UNIFORM_FLOAT;
+                var->index = array[start]->constant.index;
+                var->count = (array[prev]->constant.index - var->index) + 1;
+                var->constant = array[start];
+                var->used = 0;
+                var->emit_position = -1;
+                var->next = ctx->variables;
+                ctx->variables = var;
+            } // else
+
+            start = i;   // set this as new start of sequence.
+        } // if
+
+        prev = i;
+    } // for
+} // determine_constants_arrays
+
+
+static int adjust_swizzle(const Context *ctx, const RegisterType regtype,
+                          const int regnum, const int swizzle)
+{
+    if (regtype != REG_TYPE_INPUT)  // !!! FIXME: maybe lift this later?
+        return swizzle;
+    else if (ctx->swizzles_count == 0)
+        return swizzle;
+
+    const RegisterList *reg = reglist_find(&ctx->attributes, regtype, regnum);
+    if (reg == NULL)
+        return swizzle;
+
+    size_t i;
+    for (i = 0; i < ctx->swizzles_count; i++)
+    {
+        const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
+        if ((swiz->usage == reg->usage) && (swiz->index == reg->index))
+        {
+            return ( (((int)(swiz->swizzles[((swizzle >> 0) & 0x3)])) << 0) |
+                     (((int)(swiz->swizzles[((swizzle >> 2) & 0x3)])) << 2) |
+                     (((int)(swiz->swizzles[((swizzle >> 4) & 0x3)])) << 4) |
+                     (((int)(swiz->swizzles[((swizzle >> 6) & 0x3)])) << 6) );
+        } // if
+    } // for
+
+    return swizzle;
+} // adjust_swizzle
+
+
+static int parse_source_token(Context *ctx, SourceArgInfo *info)
+{
+    int retval = 1;
+
+    if (ctx->tokencount == 0)
+    {
+        fail(ctx, "Out of tokens in source parameter");
+        return 0;
+    } // if
+
+    const uint32 token = SWAP32(*(ctx->tokens));
+    const int reserved1 = (int) ((token >> 14) & 0x3); // bits 14 through 15
+    const int reserved2 = (int) ((token >> 31) & 0x1); // bit 31
+
+    info->token = ctx->tokens;
+    info->regnum = (int) (token & 0x7ff);  // bits 0 through 10
+    info->relative = (int) ((token >> 13) & 0x1); // bit 13
+    const int swizzle = (int) ((token >> 16) & 0xFF); // bits 16 through 23
+    info->src_mod = (SourceMod) ((token >> 24) & 0xF); // bits 24 through 27
+    info->regtype = (RegisterType) (((token >> 28) & 0x7) | ((token >> 8) & 0x18));  // bits 28-30, 11-12
+
+    // all the REG_TYPE_CONSTx types are the same register type, it's just
+    //  split up so its regnum can be > 2047 in the bytecode. Clean it up.
+    if (info->regtype == REG_TYPE_CONST2)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 2048;
+    } // else if
+    else if (info->regtype == REG_TYPE_CONST3)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 4096;
+    } // else if
+    else if (info->regtype == REG_TYPE_CONST4)
+    {
+        info->regtype = REG_TYPE_CONST;
+        info->regnum += 6144;
+    } // else if
+
+    info->swizzle = adjust_swizzle(ctx, info->regtype, info->regnum, swizzle);
+    info->swizzle_x = ((info->swizzle >> 0) & 0x3);
+    info->swizzle_y = ((info->swizzle >> 2) & 0x3);
+    info->swizzle_z = ((info->swizzle >> 4) & 0x3);
+    info->swizzle_w = ((info->swizzle >> 6) & 0x3);
+
+    // swallow token for now, for multiple calls in a row.
+    adjust_token_position(ctx, 1);
+
+    if (reserved1 != 0x0)
+        fail(ctx, "Reserved bits #1 in source token must be zero");
+
+    if (reserved2 != 0x1)
+        fail(ctx, "Reserved bit #2 in source token must be one");
+
+    if ((info->relative) && (ctx->tokencount == 0))
+    {
+        fail(ctx, "Out of tokens in relative source parameter");
+        info->relative = 0;  // don't try to process it.
+    } // if
+
+    if (info->relative)
+    {
+        if ( (shader_is_pixel(ctx)) && (!shader_version_atleast(ctx, 3, 0)) )
+            fail(ctx, "Relative addressing in pixel shader version < 3.0");
+
+        const uint32 reltoken = SWAP32(*(ctx->tokens));
+        // swallow token for now, for multiple calls in a row.
+        adjust_token_position(ctx, 1);
+
+        const int relswiz = (int) ((reltoken >> 16) & 0xFF);
+        info->relative_regnum = (int) (reltoken & 0x7ff);
+        info->relative_regtype = (RegisterType)
+                                    (((reltoken >> 28) & 0x7) |
+                                    ((reltoken >> 8) & 0x18));
+
+        if (((reltoken >> 31) & 0x1) == 0)
+            fail(ctx, "bit #31 in relative address must be set");
+
+        if ((reltoken & 0xF00E000) != 0)  // usused bits.
+            fail(ctx, "relative address reserved bit must be zero");
+
+        switch (info->relative_regtype)
+        {
+            case REG_TYPE_LOOP:
+            case REG_TYPE_ADDRESS:
+                break;
+            default:
+                fail(ctx, "invalid register for relative address");
+                break;
+        } // switch
+
+        if (info->relative_regnum != 0)  // true for now.
+            fail(ctx, "invalid register for relative address");
+
+        if (!replicate_swizzle(relswiz))
+            fail(ctx, "relative address needs replicate swizzle");
+
+        info->relative_component = (relswiz & 0x3);
+
+        if (info->regtype == REG_TYPE_INPUT)
+        {
+            if ( (shader_is_pixel(ctx)) || (!shader_version_atleast(ctx, 3, 0)) )
+                fail(ctx, "relative addressing of input registers not supported in this shader model");
+            ctx->have_relative_input_registers = 1;
+        } // if
+        else if (info->regtype == REG_TYPE_CONST)
+        {
+            // figure out what array we're in...
+            if (!ctx->ignores_ctab)
+            {
+                if (!ctx->ctab.have_ctab)  // hard to do efficiently without!
+                    fail(ctx, "relative addressing unsupported without a CTAB");
+                else
+                {
+                    determine_constants_arrays(ctx);
+
+                    VariableList *var;
+                    const int reltarget = info->regnum;
+                    for (var = ctx->variables; var != NULL; var = var->next)
+                    {
+                        const int lo = var->index;
+                        if ( (reltarget >= lo) && (reltarget < (lo + var->count)) )
+                            break;  // match!
+                    } // for
+
+                    if (var == NULL)
+                        fail(ctx, "relative addressing of indeterminate array");
+                    else
+                    {
+                        var->used = 1;
+                        info->relative_array = var;
+                        set_used_register(ctx, info->relative_regtype, info->relative_regnum, 0);
+                    } // else
+                } // else
+            } // if
+        } // else if
+        else
+        {
+            fail(ctx, "relative addressing of invalid register");
+        } // else
+
+        retval++;
+    } // if
+
+    switch (info->src_mod)
+    {
+        case SRCMOD_NONE:
+        case SRCMOD_ABSNEGATE:
+        case SRCMOD_ABS:
+        case SRCMOD_NEGATE:
+            break; // okay in any shader model.
+
+        // apparently these are only legal in Shader Model 1.x ...
+        case SRCMOD_BIASNEGATE:
+        case SRCMOD_BIAS:
+        case SRCMOD_SIGNNEGATE:
+        case SRCMOD_SIGN:
+        case SRCMOD_COMPLEMENT:
+        case SRCMOD_X2NEGATE:
+        case SRCMOD_X2:
+        case SRCMOD_DZ:
+        case SRCMOD_DW:
+            if (shader_version_atleast(ctx, 2, 0))
+                fail(ctx, "illegal source mod for this Shader Model.");
+            break;
+
+        case SRCMOD_NOT:  // !!! FIXME: I _think_ this is right...
+            if (shader_version_atleast(ctx, 2, 0))
+            {
+                if (info->regtype != REG_TYPE_PREDICATE)
+                    fail(ctx, "NOT only allowed on predicate register.");
+            } // if
+            break;
+
+        default:
+            fail(ctx, "Unknown source modifier");
+    } // switch
+
+    // !!! FIXME: docs say this for sm3 ... check these!
+    //  "The negate modifier cannot be used on second source register of these
+    //   instructions: m3x2 - ps, m3x3 - ps, m3x4 - ps, m4x3 - ps, and
+    //   m4x4 - ps."
+    //  "If any version 3 shader reads from one or more constant float
+    //   registers (c#), one of the following must be true.
+    //    All of the constant floating-point registers must use the abs modifier.
+    //    None of the constant floating-point registers can use the abs modifier.
+
+    if (!isfail(ctx))
+    {
+        RegisterList *reg;
+        reg = set_used_register(ctx, info->regtype, info->regnum, 0);
+        // !!! FIXME: this test passes if you write to the register
+        // !!! FIXME:  in this same instruction, because we parse the
+        // !!! FIXME:  destination token first.
+        // !!! FIXME: Microsoft's shader validation explicitly checks temp
+        // !!! FIXME:  registers for this...do they check other writable ones?
+        if ((info->regtype == REG_TYPE_TEMP) && (reg) && (!reg->written))
+            failf(ctx, "Temp register r%d used uninitialized", info->regnum);
+    } // if
+
+    return retval;
+} // parse_source_token
+
+
+static int parse_predicated_token(Context *ctx)
+{
+    SourceArgInfo *arg = &ctx->predicate_arg;
+    parse_source_token(ctx, arg);
+    if (arg->regtype != REG_TYPE_PREDICATE)
+        fail(ctx, "Predicated instruction but not predicate register!");
+    if ((arg->src_mod != SRCMOD_NONE) && (arg->src_mod != SRCMOD_NOT))
+        fail(ctx, "Predicated instruction register is not NONE or NOT");
+    if ( !no_swizzle(arg->swizzle) && !replicate_swizzle(arg->swizzle) )
+        fail(ctx, "Predicated instruction register has wrong swizzle");
+    if (arg->relative)  // I'm pretty sure this is illegal...?
+        fail(ctx, "relative addressing in predicated token");
+
+    return 1;
+} // parse_predicated_token
+
+
+static int parse_args_NULL(Context *ctx)
+{
+    return 1;
+} // parse_args_NULL
+
+
+static int parse_args_DEF(Context *ctx)
+{
+    parse_destination_token(ctx, &ctx->dest_arg);
+    if (ctx->dest_arg.regtype != REG_TYPE_CONST)
+        fail(ctx, "DEF using non-CONST register");
+    if (ctx->dest_arg.relative)  // I'm pretty sure this is illegal...?
+        fail(ctx, "relative addressing in DEF");
+
+    ctx->dwords[0] = SWAP32(ctx->tokens[0]);
+    ctx->dwords[1] = SWAP32(ctx->tokens[1]);
+    ctx->dwords[2] = SWAP32(ctx->tokens[2]);
+    ctx->dwords[3] = SWAP32(ctx->tokens[3]);
+
+    return 6;
+} // parse_args_DEF
+
+
+static int parse_args_DEFI(Context *ctx)
+{
+    parse_destination_token(ctx, &ctx->dest_arg);
+    if (ctx->dest_arg.regtype != REG_TYPE_CONSTINT)
+        fail(ctx, "DEFI using non-CONSTING register");
+    if (ctx->dest_arg.relative)  // I'm pretty sure this is illegal...?
+        fail(ctx, "relative addressing in DEFI");
+
+    ctx->dwords[0] = SWAP32(ctx->tokens[0]);
+    ctx->dwords[1] = SWAP32(ctx->tokens[1]);
+    ctx->dwords[2] = SWAP32(ctx->tokens[2]);
+    ctx->dwords[3] = SWAP32(ctx->tokens[3]);
+
+    return 6;
+} // parse_args_DEFI
+
+
+static int parse_args_DEFB(Context *ctx)
+{
+    parse_destination_token(ctx, &ctx->dest_arg);
+    if (ctx->dest_arg.regtype != REG_TYPE_CONSTBOOL)
+        fail(ctx, "DEFB using non-CONSTBOOL register");
+    if (ctx->dest_arg.relative)  // I'm pretty sure this is illegal...?
+        fail(ctx, "relative addressing in DEFB");
+
+    ctx->dwords[0] = *(ctx->tokens) ? 1 : 0;
+
+    return 3;
+} // parse_args_DEFB
+
+
+static int valid_texture_type(const uint32 ttype)
+{
+    switch ((const TextureType) ttype)
+    {
+        case TEXTURE_TYPE_2D:
+        case TEXTURE_TYPE_CUBE:
+        case TEXTURE_TYPE_VOLUME:
+            return 1;  // it's okay.
+    } // switch
+
+    return 0;
+} // valid_texture_type
+
+
+// !!! FIXME: this function is kind of a mess.
+static int parse_args_DCL(Context *ctx)
+{
+    int unsupported = 0;
+    const uint32 token = SWAP32(*(ctx->tokens));
+    const int reserved1 = (int) ((token >> 31) & 0x1); // bit 31
+    uint32 reserved_mask = 0x00000000;
+
+    if (reserved1 != 0x1)
+        fail(ctx, "Bit #31 in DCL token must be one");
+
+    ctx->centroid_allowed = 1;
+    adjust_token_position(ctx, 1);
+    parse_destination_token(ctx, &ctx->dest_arg);
+    ctx->centroid_allowed = 0;
+
+    if (ctx->dest_arg.result_shift != 0)  // I'm pretty sure this is illegal...?
+        fail(ctx, "shift scale in DCL");
+    if (ctx->dest_arg.relative)  // I'm pretty sure this is illegal...?
+        fail(ctx, "relative addressing in DCL");
+
+    const RegisterType regtype = ctx->dest_arg.regtype;
+    const int regnum = ctx->dest_arg.regnum;
+    if ( (shader_is_pixel(ctx)) && (shader_version_atleast(ctx, 3, 0)) )
+    {
+        if (regtype == REG_TYPE_INPUT)
+        {
+            const uint32 usage = (token & 0xF);
+            const uint32 index = ((token >> 16) & 0xF);
+            reserved_mask = 0x7FF0FFE0;
+            ctx->dwords[0] = usage;
+            ctx->dwords[1] = index;
+        } // if
+
+        else if (regtype == REG_TYPE_MISCTYPE)
+        {
+            const MiscTypeType mt = (MiscTypeType) regnum;
+            if (mt == MISCTYPE_TYPE_POSITION)
+                reserved_mask = 0x7FFFFFFF;
+            else if (mt == MISCTYPE_TYPE_FACE)
+            {
+                reserved_mask = 0x7FFFFFFF;
+                if (!writemask_xyzw(ctx->dest_arg.orig_writemask))
+                    fail(ctx, "DCL face writemask must be full");
+                if (ctx->dest_arg.result_mod != 0)
+                    fail(ctx, "DCL face result modifier must be zero");
+                if (ctx->dest_arg.result_shift != 0)
+                    fail(ctx, "DCL face shift scale must be zero");
+            } // else if
+            else
+            {
+                unsupported = 1;
+            } // else
+
+            ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_UNKNOWN;
+            ctx->dwords[1] = 0;
+        } // else if
+
+        else if (regtype == REG_TYPE_TEXTURE)
+        {
+            const uint32 usage = (token & 0xF);
+            const uint32 index = ((token >> 16) & 0xF);
+            if (usage == MOJOSHADER_USAGE_TEXCOORD)
+            {
+                if (index > 7)
+                    fail(ctx, "DCL texcoord usage must have 0-7 index");
+            } // if
+            else if (usage == MOJOSHADER_USAGE_COLOR)
+            {
+                if (index != 0)
+                    fail(ctx, "DCL color usage must have 0 index");
+            } // else if
+            else
+            {
+                fail(ctx, "Invalid DCL texture usage");
+            } // else
+
+            reserved_mask = 0x7FF0FFE0;
+            ctx->dwords[0] = usage;
+            ctx->dwords[1] = index;
+        } // else if
+
+        else if (regtype == REG_TYPE_SAMPLER)
+        {
+            const uint32 ttype = ((token >> 27) & 0xF);
+            if (!valid_texture_type(ttype))
+                fail(ctx, "unknown sampler texture type");
+            reserved_mask = 0x7FFFFFF;
+            ctx->dwords[0] = ttype;
+        } // else if
+
+        else
+        {
+            unsupported = 1;
+        } // else
+    } // if
+
+    else if ( (shader_is_pixel(ctx)) && (shader_version_atleast(ctx, 2, 0)) )
+    {
+        if (regtype == REG_TYPE_INPUT)
+        {
+            ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_COLOR;
+            ctx->dwords[1] = regnum;
+            reserved_mask = 0x7FFFFFFF;
+        } // if
+        else if (regtype == REG_TYPE_TEXTURE)
+        {
+            ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_TEXCOORD;
+            ctx->dwords[1] = regnum;
+            reserved_mask = 0x7FFFFFFF;
+        } // else if
+        else if (regtype == REG_TYPE_SAMPLER)
+        {
+            const uint32 ttype = ((token >> 27) & 0xF);
+            if (!valid_texture_type(ttype))
+                fail(ctx, "unknown sampler texture type");
+            reserved_mask = 0x7FFFFFF;
+            ctx->dwords[0] = ttype;
+        } // else if
+        else
+        {
+            unsupported = 1;
+        } // else
+    } // if
+
+    else if ( (shader_is_vertex(ctx)) && (shader_version_atleast(ctx, 3, 0)) )
+    {
+        if ((regtype == REG_TYPE_INPUT) || (regtype == REG_TYPE_OUTPUT))
+        {
+            const uint32 usage = (token & 0xF);
+            const uint32 index = ((token >> 16) & 0xF);
+            reserved_mask = 0x7FF0FFE0;
+            ctx->dwords[0] = usage;
+            ctx->dwords[1] = index;
+        } // if
+        else
+        {
+            unsupported = 1;
+        } // else
+    } // else if
+
+    else if ( (shader_is_vertex(ctx)) && (shader_version_atleast(ctx, 1, 1)) )
+    {
+        if (regtype == REG_TYPE_INPUT)
+        {
+            const uint32 usage = (token & 0xF);
+            const uint32 index = ((token >> 16) & 0xF);
+            reserved_mask = 0x7FF0FFE0;
+            ctx->dwords[0] = usage;
+            ctx->dwords[1] = index;
+        } // if
+        else
+        {
+            unsupported = 1;
+        } // else
+    } // else if
+
+    else
+    {
+        unsupported = 1;
+    } // else
+
+    if (unsupported)
+        fail(ctx, "invalid DCL register type for this shader model");
+
+    if ((token & reserved_mask) != 0)
+        fail(ctx, "reserved bits in DCL dword aren't zero");
+
+    return 3;
+} // parse_args_DCL
+
+
+static int parse_args_D(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx, &ctx->dest_arg);
+    return retval;
+} // parse_args_D
+
+
+static int parse_args_S(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    return retval;
+} // parse_args_S
+
+
+static int parse_args_SS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    retval += parse_source_token(ctx, &ctx->source_args[1]);
+    return retval;
+} // parse_args_SS
+
+
+static int parse_args_DS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx, &ctx->dest_arg);
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    return retval;
+} // parse_args_DS
+
+
+static int parse_args_DSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx, &ctx->dest_arg);
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    retval += parse_source_token(ctx, &ctx->source_args[1]);
+    return retval;
+} // parse_args_DSS
+
+
+static int parse_args_DSSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx, &ctx->dest_arg);
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    retval += parse_source_token(ctx, &ctx->source_args[1]);
+    retval += parse_source_token(ctx, &ctx->source_args[2]);
+    return retval;
+} // parse_args_DSSS
+
+
+static int parse_args_DSSSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx, &ctx->dest_arg);
+    retval += parse_source_token(ctx, &ctx->source_args[0]);
+    retval += parse_source_token(ctx, &ctx->source_args[1]);
+    retval += parse_source_token(ctx, &ctx->source_args[2]);
+    retval += parse_source_token(ctx, &ctx->source_args[3]);
+    return retval;
+} // parse_args_DSSSS
+
+
+static int parse_args_SINCOS(Context *ctx)
+{
+    // this opcode needs extra registers for sm2 and lower.
+    if (!shader_version_atleast(ctx, 3, 0))
+        return parse_args_DSSS(ctx);
+    return parse_args_DS(ctx);
+} // parse_args_SINCOS
+
+
+static int parse_args_TEXCRD(Context *ctx)
+{
+    // added extra register in ps_1_4.
+    if (shader_version_atleast(ctx, 1, 4))
+        return parse_args_DS(ctx);
+    return parse_args_D(ctx);
+} // parse_args_TEXCRD
+
+
+static int parse_args_TEXLD(Context *ctx)
+{
+    // different registers in px_1_3, ps_1_4, and ps_2_0!
+    if (shader_version_atleast(ctx, 2, 0))
+        return parse_args_DSS(ctx);
+    else if (shader_version_atleast(ctx, 1, 4))
+        return parse_args_DS(ctx);
+    return parse_args_D(ctx);
+} // parse_args_TEXLD
+
+
+// State machine functions...
+
+static ConstantsList *alloc_constant_listitem(Context *ctx)
+{
+    ConstantsList *item = (ConstantsList *) Malloc(ctx, sizeof (ConstantsList));
+    if (item == NULL)
+        return NULL;
+
+    memset(&item->constant, '\0', sizeof (MOJOSHADER_constant));
+    item->next = ctx->constants;
+    ctx->constants = item;
+    ctx->constant_count++;
+
+    return item;
+} // alloc_constant_listitem
+
+
+static void state_DEF(Context *ctx)
+{
+    const RegisterType regtype = ctx->dest_arg.regtype;
+    const int regnum = ctx->dest_arg.regnum;
+
+    // !!! FIXME: fail if same register is defined twice.
+
+    if (ctx->instruction_count != 0)
+        fail(ctx, "DEF token must come before any instructions");
+    else if (regtype != REG_TYPE_CONST)
+        fail(ctx, "DEF token using invalid register");
+    else
+    {
+        ConstantsList *item = alloc_constant_listitem(ctx);
+        if (item != NULL)
+        {
+            item->constant.index = regnum;
+            item->constant.type = MOJOSHADER_UNIFORM_FLOAT;
+            memcpy(item->constant.value.f, ctx->dwords,
+                   sizeof (item->constant.value.f));
+            set_defined_register(ctx, regtype, regnum);
+        } // if
+    } // else
+} // state_DEF
+
+static void state_DEFI(Context *ctx)
+{
+    const RegisterType regtype = ctx->dest_arg.regtype;
+    const int regnum = ctx->dest_arg.regnum;
+
+    // !!! FIXME: fail if same register is defined twice.
+
+    if (ctx->instruction_count != 0)
+        fail(ctx, "DEFI token must come before any instructions");
+    else if (regtype != REG_TYPE_CONSTINT)
+        fail(ctx, "DEFI token using invalid register");
+    else
+    {
+        ConstantsList *item = alloc_constant_listitem(ctx);
+        if (item != NULL)
+        {
+            item->constant.index = regnum;
+            item->constant.type = MOJOSHADER_UNIFORM_INT;
+            memcpy(item->constant.value.i, ctx->dwords,
+                   sizeof (item->constant.value.i));
+
+            set_defined_register(ctx, regtype, regnum);
+        } // if
+    } // else
+} // state_DEFI
+
+static void state_DEFB(Context *ctx)
+{
+    const RegisterType regtype = ctx->dest_arg.regtype;
+    const int regnum = ctx->dest_arg.regnum;
+
+    // !!! FIXME: fail if same register is defined twice.
+
+    if (ctx->instruction_count != 0)
+        fail(ctx, "DEFB token must come before any instructions");
+    else if (regtype != REG_TYPE_CONSTBOOL)
+        fail(ctx, "DEFB token using invalid register");
+    else
+    {
+        ConstantsList *item = alloc_constant_listitem(ctx);
+        if (item != NULL)
+        {
+            item->constant.index = regnum;
+            item->constant.type = MOJOSHADER_UNIFORM_BOOL;
+            item->constant.value.b = ctx->dwords[0] ? 1 : 0;
+            set_defined_register(ctx, regtype, regnum);
+        } // if
+    } // else
+} // state_DEFB
+
+static void state_DCL(Context *ctx)
+{
+    const DestArgInfo *arg = &ctx->dest_arg;
+    const RegisterType regtype = arg->regtype;
+    const int regnum = arg->regnum;
+    const int wmask = arg->writemask;
+    const int mods = arg->result_mod;
+
+    // parse_args_DCL() does a lot of state checking before we get here.
+
+    // !!! FIXME: apparently vs_3_0 can use sampler registers now.
+    // !!! FIXME:  (but only s0 through s3, not all 16 of them.)
+
+    if (ctx->instruction_count != 0)
+        fail(ctx, "DCL token must come before any instructions");
+
+    else if (shader_is_vertex(ctx))
+    {
+        const MOJOSHADER_usage usage = (const MOJOSHADER_usage) ctx->dwords[0];
+        const int index = ctx->dwords[1];
+        if (usage >= MOJOSHADER_USAGE_TOTAL)
+        {
+            fail(ctx, "unknown DCL usage");
+            return;
+        } // if
+        add_attribute_register(ctx, regtype, regnum, usage, index, wmask, mods);
+    } // if
+
+    else if (shader_is_pixel(ctx))
+    {
+        if (regtype == REG_TYPE_SAMPLER)
+            add_sampler(ctx, regnum, (TextureType) ctx->dwords[0], 0);
+        else
+        {
+            const MOJOSHADER_usage usage = (MOJOSHADER_usage) ctx->dwords[0];
+            const int index = ctx->dwords[1];
+            add_attribute_register(ctx, regtype, regnum, usage, index, wmask, mods);
+        } // else
+    } // else if
+
+    else
+    {
+        fail(ctx, "unsupported shader type."); // should be caught elsewhere.
+        return;
+    } // else
+
+    set_defined_register(ctx, regtype, regnum);
+} // state_DCL
+
+static void state_TEXCRD(Context *ctx)
+{
+    if (shader_version_atleast(ctx, 2, 0))
+        fail(ctx, "TEXCRD in Shader Model >= 2.0");  // apparently removed.
+} // state_TEXCRD
+
+static void state_FRC(Context *ctx)
+{
+    const DestArgInfo *dst = &ctx->dest_arg;
+
+    if (dst->result_mod & MOD_SATURATE)  // according to msdn...
+        fail(ctx, "FRC destination can't use saturate modifier");
+
+    else if (!shader_version_atleast(ctx, 2, 0))
+    {
+        if (!writemask_y(dst->writemask) && !writemask_xy(dst->writemask))
+            fail(ctx, "FRC writemask must be .y or .xy for shader model 1.x");
+    } // else if
+} // state_FRC
+
+
+// replicate the matrix registers to source args. The D3D profile will
+//  only use the one legitimate argument, but this saves other profiles
+//  from having to build this.
+static void srcarg_matrix_replicate(Context *ctx, const int idx,
+                                       const int rows)
+{
+    int i;
+    SourceArgInfo *src = &ctx->source_args[idx];
+    SourceArgInfo *dst = &ctx->source_args[idx+1];
+    for (i = 0; i < (rows-1); i++, dst++)
+    {
+        memcpy(dst, src, sizeof (SourceArgInfo));
+        dst->regnum += (i + 1);
+        set_used_register(ctx, dst->regtype, dst->regnum, 0);
+    } // for
+} // srcarg_matrix_replicate
+
+static void state_M4X4(Context *ctx)
+{
+    const DestArgInfo *info = &ctx->dest_arg;
+    if (!writemask_xyzw(info->writemask))
+        fail(ctx, "M4X4 writemask must be full");
+
+// !!! FIXME: MSDN:
+//The xyzw (default) mask is required for the destination register. Negate and swizzle modifiers are allowed for src0, but not for src1.
+//Swizzle and negate modifiers are invalid for the src0 register. The dest and src0 registers cannot be the same.
+
+    srcarg_matrix_replicate(ctx, 1, 4);
+} // state_M4X4
+
+static void state_M4X3(Context *ctx)
+{
+    const DestArgInfo *info = &ctx->dest_arg;
+    if (!writemask_xyz(info->writemask))
+        fail(ctx, "M4X3 writemask must be .xyz");
+
+// !!! FIXME: MSDN stuff
+
+    srcarg_matrix_replicate(ctx, 1, 3);
+} // state_M4X3
+
+static void state_M3X4(Context *ctx)
+{
+    const DestArgInfo *info = &ctx->dest_arg;
+    if (!writemask_xyzw(info->writemask))
+        fail(ctx, "M3X4 writemask must be .xyzw");
+
+// !!! FIXME: MSDN stuff
+
+    srcarg_matrix_replicate(ctx, 1, 4);
+} // state_M3X4
+
+static void state_M3X3(Context *ctx)
+{
+    const DestArgInfo *info = &ctx->dest_arg;
+    if (!writemask_xyz(info->writemask))
+        fail(ctx, "M3X3 writemask must be .xyz");
+
+// !!! FIXME: MSDN stuff
+
+    srcarg_matrix_replicate(ctx, 1, 3);
+} // state_M3X3
+
+static void state_M3X2(Context *ctx)
+{
+    const DestArgInfo *info = &ctx->dest_arg;
+    if (!writemask_xy(info->writemask))
+        fail(ctx, "M3X2 writemask must be .xy");
+
+// !!! FIXME: MSDN stuff
+
+    srcarg_matrix_replicate(ctx, 1, 2);
+} // state_M3X2
+
+static void state_RET(Context *ctx)
+{
+    // MSDN all but says that assembly shaders are more or less serialized
+    //  HLSL functions, and a RET means you're at the end of one, unlike how
+    //  most CPUs would behave. This is actually really helpful,
+    //  since we can use high-level constructs and not a mess of GOTOs,
+    //  which is a godsend for GLSL...this also means we can consider things
+    //  like a LOOP without a matching ENDLOOP within a label's section as
+    //  an error.
+    if (ctx->loops > 0)
+        fail(ctx, "LOOP without ENDLOOP");
+    if (ctx->reps > 0)
+        fail(ctx, "REP without ENDREP");
+} // state_RET
+
+static void check_label_register(Context *ctx, int arg, const char *opcode)
+{
+    const SourceArgInfo *info = &ctx->source_args[arg];
+    const RegisterType regtype = info->regtype;
+    const int regnum = info->regnum;
+
+    if (regtype != REG_TYPE_LABEL)
+        failf(ctx, "%s with a non-label register specified", opcode);
+    if (!shader_version_atleast(ctx, 2, 0))
+        failf(ctx, "%s not supported in Shader Model 1", opcode);
+    if ((shader_version_atleast(ctx, 2, 255)) && (regnum > 2047))
+        fail(ctx, "label register number must be <= 2047");
+    if (regnum > 15)
+        fail(ctx, "label register number must be <= 15");
+} // check_label_register
+
+static void state_LABEL(Context *ctx)
+{
+    if (ctx->previous_opcode != OPCODE_RET)
+        fail(ctx, "LABEL not followed by a RET");
+    check_label_register(ctx, 0, "LABEL");
+    set_defined_register(ctx, REG_TYPE_LABEL, ctx->source_args[0].regnum);
+} // state_LABEL
+
+static void check_call_loop_wrappage(Context *ctx, const int regnum)
+{
+    // msdn says subroutines inherit aL register if you're in a loop when
+    //  you call, and further more _if you ever call this function in a loop,
+    //  it must always be called in a loop_. So we'll just pass our loop
+    //  variable as a function parameter in those cases.
+
+    const int current_usage = (ctx->loops > 0) ? 1 : -1;
+    RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, regnum);
+    assert(reg != NULL);
+
+    if (reg->misc == 0)
+        reg->misc = current_usage;
+    else if (reg->misc != current_usage)
+    {
+        if (current_usage == 1)
+            fail(ctx, "CALL to this label must be wrapped in LOOP/ENDLOOP");
+        else
+            fail(ctx, "CALL to this label must not be wrapped in LOOP/ENDLOOP");
+    } // else if
+} // check_call_loop_wrappage
+
+static void state_CALL(Context *ctx)
+{
+    check_label_register(ctx, 0, "CALL");
+    check_call_loop_wrappage(ctx, ctx->source_args[0].regnum);
+} // state_CALL
+
+static void state_CALLNZ(Context *ctx)
+{
+    const RegisterType regtype = ctx->source_args[1].regtype;
+    if ((regtype != REG_TYPE_CONSTBOOL) && (regtype != REG_TYPE_PREDICATE))
+        fail(ctx, "CALLNZ argument isn't constbool or predicate register");
+    check_label_register(ctx, 0, "CALLNZ");
+    check_call_loop_wrappage(ctx, ctx->source_args[0].regnum);
+} // state_CALLNZ
+
+static void state_MOVA(Context *ctx)
+{
+    if (ctx->dest_arg.regtype != REG_TYPE_ADDRESS)
+        fail(ctx, "MOVA argument isn't address register");
+} // state_MOVA
+
+static void state_RCP(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "RCP without replicate swizzzle");
+} // state_RCP
+
+static void state_LOOP(Context *ctx)
+{
+    if (ctx->source_args[0].regtype != REG_TYPE_LOOP)
+        fail(ctx, "LOOP argument isn't loop register");
+    else if (ctx->source_args[1].regtype != REG_TYPE_CONSTINT)
+        fail(ctx, "LOOP argument isn't constint register");
+    else
+        ctx->loops++;
+} // state_LOOP
+
+static void state_ENDLOOP(Context *ctx)
+{
+    // !!! FIXME: check that we aren't straddling an IF block.
+    if (ctx->loops <= 0)
+        fail(ctx, "ENDLOOP without LOOP");
+    ctx->loops--;
+} // state_ENDLOOP
+
+static void state_BREAKP(Context *ctx)
+{
+    const RegisterType regtype = ctx->source_args[0].regtype;
+    if (regtype != REG_TYPE_PREDICATE)
+        fail(ctx, "BREAKP argument isn't predicate register");
+    else if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "BREAKP without replicate swizzzle");
+    else if ((ctx->loops == 0) && (ctx->reps == 0))
+        fail(ctx, "BREAKP outside LOOP/ENDLOOP or REP/ENDREP");
+} // state_BREAKP
+
+static void state_BREAK(Context *ctx)
+{
+    if ((ctx->loops == 0) && (ctx->reps == 0))
+        fail(ctx, "BREAK outside LOOP/ENDLOOP or REP/ENDREP");
+} // state_BREAK
+
+static void state_SETP(Context *ctx)
+{
+    const RegisterType regtype = ctx->dest_arg.regtype;
+    if (regtype != REG_TYPE_PREDICATE)
+        fail(ctx, "SETP argument isn't predicate register");
+} // state_SETP
+
+static void state_REP(Context *ctx)
+{
+    const RegisterType regtype = ctx->source_args[0].regtype;
+    if (regtype != REG_TYPE_CONSTINT)
+        fail(ctx, "REP argument isn't constint register");
+
+    ctx->reps++;
+    if (ctx->reps > ctx->max_reps)
+        ctx->max_reps = ctx->reps;
+} // state_REP
+
+static void state_ENDREP(Context *ctx)
+{
+    // !!! FIXME: check that we aren't straddling an IF block.
+    if (ctx->reps <= 0)
+        fail(ctx, "ENDREP without REP");
+    ctx->reps--;
+} // state_ENDREP
+
+static void state_CMP(Context *ctx)
+{
+    ctx->cmps++;
+
+    // extra limitations for ps <= 1.4 ...
+    if (!shader_version_atleast(ctx, 1, 4))
+    {
+        int i;
+        const DestArgInfo *dst = &ctx->dest_arg;
+        const RegisterType dregtype = dst->regtype;
+        const int dregnum = dst->regnum;
+
+        if (ctx->cmps > 3)
+            fail(ctx, "only 3 CMP instructions allowed in this shader model");
+
+        for (i = 0; i < 3; i++)
+        {
+            const SourceArgInfo *src = &ctx->source_args[i];
+            const RegisterType sregtype = src->regtype;
+            const int sregnum = src->regnum;
+            if ((dregtype == sregtype) && (dregnum == sregnum))
+                fail(ctx, "CMP dest can't match sources in this shader model");
+        } // for
+
+        ctx->instruction_count++;  // takes an extra slot in ps_1_2 and _3.
+    } // if
+} // state_CMP
+
+static void state_DP4(Context *ctx)
+{
+    // extra limitations for ps <= 1.4 ...
+    if (!shader_version_atleast(ctx, 1, 4))
+        ctx->instruction_count++;  // takes an extra slot in ps_1_2 and _3.
+} // state_DP4
+
+static void state_CND(Context *ctx)
+{
+    // apparently it was removed...it's not in the docs past ps_1_4 ...
+    if (shader_version_atleast(ctx, 2, 0))
+        fail(ctx, "CND not allowed in this shader model");
+
+    // extra limitations for ps <= 1.4 ...
+    else if (!shader_version_atleast(ctx, 1, 4))
+    {
+        const SourceArgInfo *src = &ctx->source_args[0];
+        if ((src->regtype != REG_TYPE_TEMP) || (src->regnum != 0) ||
+            (src->swizzle != 0xFF))
+        {
+            fail(ctx, "CND src must be r0.a in this shader model");
+        } // if
+    } // if
+} // state_CND
+
+static void state_POW(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "POW src0 must have replicate swizzle");
+    else if (!replicate_swizzle(ctx->source_args[1].swizzle))
+        fail(ctx, "POW src1 must have replicate swizzle");
+} // state_POW
+
+static void state_LOG(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "LOG src0 must have replicate swizzle");
+} // state_LOG
+
+static void state_LOGP(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "LOGP src0 must have replicate swizzle");
+} // state_LOGP
+
+static void state_SINCOS(Context *ctx)
+{
+    const DestArgInfo *dst = &ctx->dest_arg;
+    const int mask = dst->writemask;
+    if (!writemask_x(mask) && !writemask_y(mask) && !writemask_xy(mask))
+        fail(ctx, "SINCOS write mask must be .x or .y or .xy");
+
+    else if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "SINCOS src0 must have replicate swizzle");
+
+    else if (dst->result_mod & MOD_SATURATE)  // according to msdn...
+        fail(ctx, "SINCOS destination can't use saturate modifier");
+
+    // this opcode needs extra registers, with extra limitations, for <= sm2.
+    else if (!shader_version_atleast(ctx, 3, 0))
+    {
+        int i;
+        for (i = 1; i < 3; i++)
+        {
+            if (ctx->source_args[i].regtype != REG_TYPE_CONST)
+            {
+                failf(ctx, "SINCOS src%d must be constfloat", i);
+                return;
+            } // if
+        } // for
+
+        if (ctx->source_args[1].regnum == ctx->source_args[2].regnum)
+            fail(ctx, "SINCOS src1 and src2 must be different registers");
+    } // if
+} // state_SINCOS
+
+static void state_IF(Context *ctx)
+{
+    const RegisterType regtype = ctx->source_args[0].regtype;
+    if ((regtype != REG_TYPE_PREDICATE) && (regtype != REG_TYPE_CONSTBOOL))
+        fail(ctx, "IF src0 must be CONSTBOOL or PREDICATE");
+    else if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "IF src0 must have replicate swizzle");
+    // !!! FIXME: track if nesting depth.
+} // state_IF
+
+static void state_IFC(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "IFC src0 must have replicate swizzle");
+    else if (!replicate_swizzle(ctx->source_args[1].swizzle))
+        fail(ctx, "IFC src1 must have replicate swizzle");
+    // !!! FIXME: track if nesting depth.
+} // state_IFC
+
+static void state_BREAKC(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[0].swizzle))
+        fail(ctx, "BREAKC src1 must have replicate swizzle");
+    else if (!replicate_swizzle(ctx->source_args[1].swizzle))
+        fail(ctx, "BREAKC src2 must have replicate swizzle");
+    else if ((ctx->loops == 0) && (ctx->reps == 0))
+        fail(ctx, "BREAKC outside LOOP/ENDLOOP or REP/ENDREP");
+} // state_BREAKC
+
+static void state_TEXKILL(Context *ctx)
+{
+    // The MSDN docs say this should be a source arg, but the driver docs
+    //  say it's a dest arg. That's annoying.
+    const DestArgInfo *info = &ctx->dest_arg;
+    const RegisterType regtype = info->regtype;
+    if (!writemask_xyzw(info->writemask))
+        fail(ctx, "TEXKILL writemask must be .xyzw");
+    else if ((regtype != REG_TYPE_TEMP) && (regtype != REG_TYPE_TEXTURE))
+        fail(ctx, "TEXKILL must use a temp or texture register");
+
+    // !!! FIXME: "If a temporary register is used, all components must have been previously written."
+    // !!! FIXME: "If a texture register is used, all components that are read must have been declared."
+    // !!! FIXME: there are further limitations in ps_1_3 and earlier.
+} // state_TEXKILL
+
+// Some rules that apply to some of the fruity ps_1_1 texture opcodes...
+static void state_texops(Context *ctx, const char *opcode,
+                         const int dims, const int texbem)
+{
+    const DestArgInfo *dst = &ctx->dest_arg;
+    const SourceArgInfo *src = &ctx->source_args[0];
+    if (dst->regtype != REG_TYPE_TEXTURE)
+        failf(ctx, "%s destination must be a texture register", opcode);
+    if (src->regtype != REG_TYPE_TEXTURE)
+        failf(ctx, "%s source must be a texture register", opcode);
+    if (src->regnum >= dst->regnum)  // so says MSDN.
+        failf(ctx, "%s dest must be a higher register than source", opcode);
+
+    if (dims)
+    {
+        TextureType ttyp = (dims == 2) ? TEXTURE_TYPE_2D : TEXTURE_TYPE_CUBE;
+        add_sampler(ctx, dst->regnum, ttyp, texbem);
+    } // if
+
+    add_attribute_register(ctx, REG_TYPE_TEXTURE, dst->regnum,
+                           MOJOSHADER_USAGE_TEXCOORD, dst->regnum, 0xF, 0);
+
+    // Strictly speaking, there should be a TEX opcode prior to this call that
+    //  should fill in this metadata, but I'm not sure that's required for the
+    //  shader to assemble in D3D, so we'll do this so we don't fail with a
+    //  cryptic error message even if the developer didn't do the TEX.
+    add_attribute_register(ctx, REG_TYPE_TEXTURE, src->regnum,
+                           MOJOSHADER_USAGE_TEXCOORD, src->regnum, 0xF, 0);
+} // state_texops
+
+static void state_texbem(Context *ctx, const char *opcode)
+{
+    // The TEXBEM equasion, according to MSDN:
+    //u' = TextureCoordinates(stage m)u + D3DTSS_BUMPENVMAT00(stage m)*t(n)R
+    //         + D3DTSS_BUMPENVMAT10(stage m)*t(n)G
+    //v' = TextureCoordinates(stage m)v + D3DTSS_BUMPENVMAT01(stage m)*t(n)R
+    //         + D3DTSS_BUMPENVMAT11(stage m)*t(n)G
+    //t(m)RGBA = TextureSample(stage m)
+    //
+    // ...TEXBEML adds this at the end:
+    //t(m)RGBA = t(m)RGBA * [(t(n)B * D3DTSS_BUMPENVLSCALE(stage m)) +
+    //           D3DTSS_BUMPENVLOFFSET(stage m)]
+
+    if (shader_version_atleast(ctx, 1, 4))
+        failf(ctx, "%s opcode not available after Shader Model 1.3", opcode);
+
+    if (!shader_version_atleast(ctx, 1, 2))
+    {
+        if (ctx->source_args[0].src_mod == SRCMOD_SIGN)
+            failf(ctx, "%s forbids _bx2 on source reg before ps_1_2", opcode);
+    } // if
+
+    // !!! FIXME: MSDN:
+    // !!! FIXME: Register data that has been read by a texbem
+    // !!! FIXME:  or texbeml instruction cannot be read later,
+    // !!! FIXME:  except by another texbem or texbeml.
+
+    state_texops(ctx, opcode, 2, 1);
+} // state_texbem
+
+static void state_TEXBEM(Context *ctx)
+{
+    state_texbem(ctx, "TEXBEM");
+} // state_TEXBEM
+
+static void state_TEXBEML(Context *ctx)
+{
+    state_texbem(ctx, "TEXBEML");
+} // state_TEXBEML
+
+static void state_TEXM3X2PAD(Context *ctx)
+{
+    if (shader_version_atleast(ctx, 1, 4))
+        fail(ctx, "TEXM3X2PAD opcode not available after Shader Model 1.3");
+    state_texops(ctx, "TEXM3X2PAD", 0, 0);
+    // !!! FIXME: check for correct opcode existance and order more rigorously?
+    ctx->texm3x2pad_src0 = ctx->source_args[0].regnum;
+    ctx->texm3x2pad_dst0 = ctx->dest_arg.regnum;
+} // state_TEXM3X2PAD
+
+static void state_TEXM3X2TEX(Context *ctx)
+{
+    if (shader_version_atleast(ctx, 1, 4))
+        fail(ctx, "TEXM3X2TEX opcode not available after Shader Model 1.3");
+    if (ctx->texm3x2pad_dst0 == -1)
+        fail(ctx, "TEXM3X2TEX opcode without matching TEXM3X2PAD");
+    // !!! FIXME: check for correct opcode existance and order more rigorously?
+    state_texops(ctx, "TEXM3X2TEX", 2, 0);
+    ctx->reset_texmpad = 1;
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                      ctx->dest_arg.regnum);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+
+    // A samplermap might change this to something nonsensical.
+    if (ttype != TEXTURE_TYPE_2D)
+        fail(ctx, "TEXM3X2TEX needs a 2D sampler");
+} // state_TEXM3X2TEX
+
+static void state_TEXM3X3PAD(Context *ctx)
+{
+    if (shader_version_atleast(ctx, 1, 4))
+        fail(ctx, "TEXM3X2TEX opcode not available after Shader Model 1.3");
+    state_texops(ctx, "TEXM3X3PAD", 0, 0);
+
+    // !!! FIXME: check for correct opcode existance and order more rigorously?
+    if (ctx->texm3x3pad_dst0 == -1)
+    {
+        ctx->texm3x3pad_src0 = ctx->source_args[0].regnum;
+        ctx->texm3x3pad_dst0 = ctx->dest_arg.regnum;
+    } // if
+    else if (ctx->texm3x3pad_dst1 == -1)
+    {
+        ctx->texm3x3pad_src1 = ctx->source_args[0].regnum;
+        ctx->texm3x3pad_dst1 = ctx->dest_arg.regnum;
+    } // else
+} // state_TEXM3X3PAD
+
+static void state_texm3x3(Context *ctx, const char *opcode, const int dims)
+{
+    // !!! FIXME: check for correct opcode existance and order more rigorously?
+    if (shader_version_atleast(ctx, 1, 4))
+        failf(ctx, "%s opcode not available after Shader Model 1.3", opcode);
+    if (ctx->texm3x3pad_dst1 == -1)
+        failf(ctx, "%s opcode without matching TEXM3X3PADs", opcode);
+    state_texops(ctx, opcode, dims, 0);
+    ctx->reset_texmpad = 1;
+
+    RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
+                                      ctx->dest_arg.regnum);
+    const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
+
+    // A samplermap might change this to something nonsensical.
+    if ((ttype != TEXTURE_TYPE_VOLUME) && (ttype != TEXTURE_TYPE_CUBE))
+        failf(ctx, "%s needs a 3D or Cubemap sampler", opcode);
+} // state_texm3x3
+
+static void state_TEXM3X3(Context *ctx)
+{
+    if (!shader_version_atleast(ctx, 1, 2))
+        fail(ctx, "TEXM3X3 opcode not available in Shader Model 1.1");
+    state_texm3x3(ctx, "TEXM3X3", 0);
+} // state_TEXM3X3
+
+static void state_TEXM3X3TEX(Context *ctx)
+{
+    state_texm3x3(ctx, "TEXM3X3TEX", 3);
+} // state_TEXM3X3TEX
+
+static void state_TEXM3X3SPEC(Context *ctx)
+{
+    state_texm3x3(ctx, "TEXM3X3SPEC", 3);
+    if (ctx->source_args[1].regtype != REG_TYPE_CONST)
+        fail(ctx, "TEXM3X3SPEC final arg must be a constant register");
+} // state_TEXM3X3SPEC
+
+static void state_TEXM3X3VSPEC(Context *ctx)
+{
+    state_texm3x3(ctx, "TEXM3X3VSPEC", 3);
+} // state_TEXM3X3VSPEC
+
+
+static void state_TEXLD(Context *ctx)
+{
+    if (shader_version_atleast(ctx, 2, 0))
+    {
+        const SourceArgInfo *src0 = &ctx->source_args[0];
+        const SourceArgInfo *src1 = &ctx->source_args[1];
+
+        // !!! FIXME: verify texldp restrictions:
+        //http://msdn.microsoft.com/en-us/library/bb206221(VS.85).aspx
+        // !!! FIXME: ...and texldb, too.
+        //http://msdn.microsoft.com/en-us/library/bb206217(VS.85).aspx
+
+        //const RegisterType rt0 = src0->regtype;
+
+        // !!! FIXME: msdn says it has to be temp, but Microsoft's HLSL
+        // !!! FIXME:  compiler is generating code that uses oC0 for a dest.
+        //if (ctx->dest_arg.regtype != REG_TYPE_TEMP)
+        //    fail(ctx, "TEXLD dest must be a temp register");
+
+        // !!! FIXME: this can be an REG_TYPE_INPUT, DCL'd to TEXCOORD.
+        //else if ((rt0 != REG_TYPE_TEXTURE) && (rt0 != REG_TYPE_TEMP))
+        //    fail(ctx, "TEXLD src0 must be texture or temp register");
+        //else
+
+        if (src0->src_mod != SRCMOD_NONE)
+            fail(ctx, "TEXLD src0 must have no modifiers");
+        else if (src1->regtype != REG_TYPE_SAMPLER)
+            fail(ctx, "TEXLD src1 must be sampler register");
+        else if (src1->src_mod != SRCMOD_NONE)
+            fail(ctx, "TEXLD src1 must have no modifiers");
+        else if ( (ctx->instruction_controls != CONTROL_TEXLD) &&
+                  (ctx->instruction_controls != CONTROL_TEXLDP) &&
+                  (ctx->instruction_controls != CONTROL_TEXLDB) )
+        {
+            fail(ctx, "TEXLD has unknown control bits");
+        } // else if
+
+        // Shader Model 3 added swizzle support to this opcode.
+        if (!shader_version_atleast(ctx, 3, 0))
+        {
+            if (!no_swizzle(src0->swizzle))
+                fail(ctx, "TEXLD src0 must not swizzle");
+            else if (!no_swizzle(src1->swizzle))
+                fail(ctx, "TEXLD src1 must not swizzle");
+        } // if
+
+        if ( ((TextureType) ctx->source_args[1].regnum) == TEXTURE_TYPE_CUBE )
+            ctx->instruction_count += 3;
+    } // if
+
+    else if (shader_version_atleast(ctx, 1, 4))
+    {
+        // !!! FIXME: checks for ps_1_4 version here...
+    } // else if
+
+    else
+    {
+        // !!! FIXME: add (other?) checks for ps_1_1 version here...
+        const DestArgInfo *info = &ctx->dest_arg;
+        const int sampler = info->regnum;
+        if (info->regtype != REG_TYPE_TEXTURE)
+            fail(ctx, "TEX param must be a texture register");
+        add_sampler(ctx, sampler, TEXTURE_TYPE_2D, 0);
+        add_attribute_register(ctx, REG_TYPE_TEXTURE, sampler,
+                               MOJOSHADER_USAGE_TEXCOORD, sampler, 0xF, 0);
+    } // else
+} // state_TEXLD
+
+static void state_TEXLDL(Context *ctx)
+{
+    if (!shader_version_atleast(ctx, 3, 0))
+        fail(ctx, "TEXLDL in version < Shader Model 3.0");
+    else if (ctx->source_args[1].regtype != REG_TYPE_SAMPLER)
+        fail(ctx, "TEXLDL src1 must be sampler register");
+    else
+    {
+        if ( ((TextureType) ctx->source_args[1].regnum) == TEXTURE_TYPE_CUBE )
+            ctx->instruction_count += 3;
+    } // else
+} // state_TEXLDL
+
+static void state_DP2ADD(Context *ctx)
+{
+    if (!replicate_swizzle(ctx->source_args[2].swizzle))
+        fail(ctx, "DP2ADD src2 must have replicate swizzle");
+} // state_DP2ADD
+
+
+// Lookup table for instruction opcodes...
+typedef struct
+{
+    const char *opcode_string;
+    int slots;  // number of instruction slots this opcode eats.
+    MOJOSHADER_shaderType shader_types;  // mask of types that can use opcode.
+    args_function parse_args;
+    state_function state;
+    emit_function emitter[STATICARRAYLEN(profiles)];
+} Instruction;
+
+// These have to be in the right order! This array is indexed by the value
+//  of the instruction token.
+static const Instruction instructions[] =
+{
+    #define INSTRUCTION_STATE(op, opstr, slots, a, t) { \
+        opstr, slots, t, parse_args_##a, state_##op, PROFILE_EMITTERS(op) \
+    },
+
+    #define INSTRUCTION(op, opstr, slots, a, t) { \
+        opstr, slots, t, parse_args_##a, 0, PROFILE_EMITTERS(op) \
+    },
+
+    #define MOJOSHADER_DO_INSTRUCTION_TABLE 1
+    #include "mojoshader_internal.h"
+    #undef MOJOSHADER_DO_INSTRUCTION_TABLE
+
+    #undef INSTRUCTION
+    #undef INSTRUCTION_STATE
+};
+
+
+// parse various token types...
+
+static int parse_instruction_token(Context *ctx)
+{
+    int retval = 0;
+    const int start_position = ctx->current_position;
+    const uint32 *start_tokens = ctx->tokens;
+    const uint32 start_tokencount = ctx->tokencount;
+    const uint32 token = SWAP32(*(ctx->tokens));
+    const uint32 opcode = (token & 0xFFFF);
+    const uint32 controls = ((token >> 16) & 0xFF);
+    const uint32 insttoks = ((token >> 24) & 0x0F);
+    const int coissue = (token & 0x40000000) ? 1 : 0;
+    const int predicated = (token & 0x10000000) ? 1 : 0;
+
+    if ( opcode >= (sizeof (instructions) / sizeof (instructions[0])) )
+        return 0;  // not an instruction token, or just not handled here.
+
+    const Instruction *instruction = &instructions[opcode];
+    const emit_function emitter = instruction->emitter[ctx->profileid];
+
+    if ((token & 0x80000000) != 0)
+        fail(ctx, "instruction token high bit must be zero.");  // so says msdn.
+
+    if (instruction->opcode_string == NULL)
+    {
+        fail(ctx, "Unknown opcode.");
+        return insttoks + 1;  // pray that you resync later.
+    } // if
+
+    ctx->coissue = coissue;
+    if (coissue)
+    {
+        if (!shader_is_pixel(ctx))
+            fail(ctx, "coissue instruction on non-pixel shader");
+        if (shader_version_atleast(ctx, 2, 0))
+            fail(ctx, "coissue instruction in Shader Model >= 2.0");
+    } // if
+
+    if ((ctx->shader_type & instruction->shader_types) == 0)
+    {
+        failf(ctx, "opcode '%s' not available in this shader type.",
+                instruction->opcode_string);
+    } // if
+
+    memset(ctx->dwords, '\0', sizeof (ctx->dwords));
+    ctx->instruction_controls = controls;
+    ctx->predicated = predicated;
+
+    // Update the context with instruction's arguments.
+    adjust_token_position(ctx, 1);
+    retval = instruction->parse_args(ctx);
+
+    if (predicated)
+        retval += parse_predicated_token(ctx);
+
+    // parse_args() moves these forward for convenience...reset them.
+    ctx->tokens = start_tokens;
+    ctx->tokencount = start_tokencount;
+    ctx->current_position = start_position;
+
+    if (instruction->state != NULL)
+        instruction->state(ctx);
+
+    ctx->instruction_count += instruction->slots;
+
+    if (!isfail(ctx))
+        emitter(ctx);  // call the profile's emitter.
+
+    if (ctx->reset_texmpad)
+    {
+        ctx->texm3x2pad_dst0 = -1;
+        ctx->texm3x2pad_src0 = -1;
+        ctx->texm3x3pad_dst0 = -1;
+        ctx->texm3x3pad_src0 = -1;
+        ctx->texm3x3pad_dst1 = -1;
+        ctx->texm3x3pad_src1 = -1;
+        ctx->reset_texmpad = 0;
+    } // if
+
+    ctx->previous_opcode = opcode;
+    ctx->scratch_registers = 0;  // reset after every instruction.
+
+    if (!shader_version_atleast(ctx, 2, 0))
+    {
+        if (insttoks != 0)  // reserved field in shaders < 2.0 ...
+            fail(ctx, "instruction token count must be zero");
+    } // if
+    else
+    {
+        if (((uint32)retval) != (insttoks+1))
+        {
+            failf(ctx, "wrong token count (%u, not %u) for opcode '%s'.",
+                    (uint) retval, (uint) (insttoks+1),
+                    instruction->opcode_string);
+            retval = insttoks + 1;  // try to keep sync.
+        } // if
+    } // else
+
+    return retval;
+} // parse_instruction_token
+
+
+static int parse_version_token(Context *ctx, const char *profilestr)
+{
+    if (ctx->tokencount == 0)
+    {
+        fail(ctx, "Expected version token, got none at all.");
+        return 0;
+    } // if
+
+    const uint32 token = SWAP32(*(ctx->tokens));
+    const uint32 shadertype = ((token >> 16) & 0xFFFF);
+    const uint8 major = (uint8) ((token >> 8) & 0xFF);
+    const uint8 minor = (uint8) (token & 0xFF);
+
+    ctx->version_token = token;
+
+    // 0xFFFF == pixel shader, 0xFFFE == vertex shader
+    if (shadertype == 0xFFFF)
+    {
+        ctx->shader_type = MOJOSHADER_TYPE_PIXEL;
+        ctx->shader_type_str = "ps";
+    } // if
+    else if (shadertype == 0xFFFE)
+    {
+        ctx->shader_type = MOJOSHADER_TYPE_VERTEX;
+        ctx->shader_type_str = "vs";
+    } // else if
+    else  // geometry shader? Bogus data?
+    {
+        fail(ctx, "Unsupported shader type or not a shader at all");
+        return -1;
+    } // else
+
+    ctx->major_ver = major;
+    ctx->minor_ver = minor;
+
+    if (!shader_version_supported(major, minor))
+    {
+        failf(ctx, "Shader Model %u.%u is currently unsupported.",
+                (uint) major, (uint) minor);
+    } // if
+
+    if (!isfail(ctx))
+        ctx->profile->start_emitter(ctx, profilestr);
+
+    return 1;  // ate one token.
+} // parse_version_token
+
+
+static int parse_ctab_string(const uint8 *start, const uint32 bytes,
+                             const uint32 name)
+{
+    // Make sure strings don't overflow the CTAB buffer...
+    if (name < bytes)
+    {
+        int i;
+        const int slenmax = bytes - name;
+        const char *namestr = (const char *) (start + name);
+        for (i = 0; i < slenmax; i++)
+        {
+            if (namestr[i] == '\0')
+                return 1;  // it's okay.
+        } // for
+    } // if
+
+    return 0;  // overflowed.
+} // parse_ctab_string
+
+
+static int parse_ctab_typeinfo(Context *ctx, const uint8 *start,
+                               const uint32 bytes, const uint32 pos,
+                               MOJOSHADER_symbolTypeInfo *info)
+{
+    if ((pos + 16) >= bytes)
+        return 0;  // corrupt CTAB.
+
+    const uint16 *typeptr = (const uint16 *) (start + pos);
+
+    info->parameter_class = (MOJOSHADER_symbolClass) SWAP16(typeptr[0]);
+    info->parameter_type = (MOJOSHADER_symbolType) SWAP16(typeptr[1]);
+    info->rows = (unsigned int) SWAP16(typeptr[2]);
+    info->columns = (unsigned int) SWAP16(typeptr[3]);
+    info->elements = (unsigned int) SWAP16(typeptr[4]);
+    info->member_count = (unsigned int) SWAP16(typeptr[5]);
+
+    if ((pos + 16 + (info->member_count * 8)) >= bytes)
+        return 0;  // corrupt CTAB.
+
+    if (info->member_count == 0)
+        info->members = NULL;
+    else
+    {
+        const size_t len = sizeof (MOJOSHADER_symbolStructMember) *
+                            info->member_count;
+        info->members = (MOJOSHADER_symbolStructMember *) Malloc(ctx, len);
+        if (info->members == NULL)
+            return 1;  // we'll check ctx->out_of_memory later.
+        memset(info->members, '\0', len);
+    } // else
+
+    int i;
+    const uint32 *member = (const uint32 *)((const uint8 *) (&typeptr[6]));
+    for (i = 0; i < info->member_count; i++)
+    {
+        MOJOSHADER_symbolStructMember *mbr = &info->members[i];
+        const uint32 name = SWAP32(member[0]);
+        const uint32 memberinfopos = SWAP32(member[1]);
+        member += 2;
+
+        if (!parse_ctab_string(start, bytes, name))
+            return 0;  // info->members will be free()'d elsewhere.
+
+        mbr->name = StrDup(ctx, (const char *) (start + name));
+        if (mbr->name == NULL)
+            return 1;  // we'll check ctx->out_of_memory later.
+        if (!parse_ctab_typeinfo(ctx, start, bytes, memberinfopos, &mbr->info))
+            return 0;
+        if (ctx->out_of_memory)
+            return 1;  // drop out now.
+    } // for
+
+    return 1;
+} // parse_ctab_typeinfo
+
+
+// Microsoft's tools add a CTAB comment to all shaders. This is the
+//  "constant table," or specifically: D3DXSHADER_CONSTANTTABLE:
+//  http://msdn.microsoft.com/en-us/library/bb205440(VS.85).aspx
+// This may tell us high-level truths about an otherwise generic low-level
+//  registers, for instance, how large an array actually is, etc.
+static void parse_constant_table(Context *ctx, const uint32 *tokens,
+                                 const uint32 bytes, const uint32 okay_version,
+                                 const int setvariables, CtabData *ctab)
+{
+    const uint32 id = SWAP32(tokens[1]);
+    if (id != CTAB_ID)
+        return;  // not the constant table.
+
+    assert(ctab->have_ctab == 0);  // !!! FIXME: can you have more than one?
+    ctab->have_ctab = 1;
+
+    const uint8 *start = (uint8 *) &tokens[2];
+
+    if (bytes < 32)
+    {
+        fail(ctx, "Truncated CTAB data");
+        return;
+    } // if
+
+    const uint32 size = SWAP32(tokens[2]);
+    const uint32 creator = SWAP32(tokens[3]);
+    const uint32 version = SWAP32(tokens[4]);
+    const uint32 constants = SWAP32(tokens[5]);
+    const uint32 constantinfo = SWAP32(tokens[6]);
+    const uint32 target = SWAP32(tokens[8]);
+
+    if (size != CTAB_SIZE)
+        goto corrupt_ctab;
+
+    if (version != okay_version) goto corrupt_ctab;
+    if (creator >= bytes) goto corrupt_ctab;
+    if ((constantinfo + (constants * CINFO_SIZE)) >= bytes) goto corrupt_ctab;
+    if (target >= bytes) goto corrupt_ctab;
+    if (!parse_ctab_string(start, bytes, target)) goto corrupt_ctab;
+    // !!! FIXME: check that (start+target) points to "ps_3_0", etc.
+
+    ctab->symbol_count = constants;
+    ctab->symbols = (MOJOSHADER_symbol *)Malloc(ctx, sizeof (MOJOSHADER_symbol) * constants);
+    if (ctab->symbols == NULL)
+        return;
+    memset(ctab->symbols, '\0', sizeof (MOJOSHADER_symbol) * constants);
+
+    uint32 i = 0;
+    for (i = 0; i < constants; i++)
+    {
+        const uint8 *ptr = start + constantinfo + (i * CINFO_SIZE);
+        const uint32 name = SWAP32(*((uint32 *) (ptr + 0)));
+        const uint16 regset = SWAP16(*((uint16 *) (ptr + 4)));
+        const uint16 regidx = SWAP16(*((uint16 *) (ptr + 6)));
+        const uint16 regcnt = SWAP16(*((uint16 *) (ptr + 8)));
+        const uint32 typeinf = SWAP32(*((uint32 *) (ptr + 12)));
+        const uint32 defval = SWAP32(*((uint32 *) (ptr + 16)));
+        MOJOSHADER_uniformType mojotype = MOJOSHADER_UNIFORM_UNKNOWN;
+
+        if (!parse_ctab_string(start, bytes, name)) goto corrupt_ctab;
+        if (defval >= bytes) goto corrupt_ctab;
+
+        switch (regset)
+        {
+            case 0: mojotype = MOJOSHADER_UNIFORM_BOOL; break;
+            case 1: mojotype = MOJOSHADER_UNIFORM_INT; break;
+            case 2: mojotype = MOJOSHADER_UNIFORM_FLOAT; break;
+            case 3: /* SAMPLER */ break;
+            default: goto corrupt_ctab;
+        } // switch
+
+        if ((setvariables) && (mojotype != MOJOSHADER_UNIFORM_UNKNOWN))
+        {
+            VariableList *item;
+            item = (VariableList *) Malloc(ctx, sizeof (VariableList));
+            if (item != NULL)
+            {
+                item->type = mojotype;
+                item->index = regidx;
+                item->count = regcnt;
+                item->constant = NULL;
+                item->used = 0;
+                item->emit_position = -1;
+                item->next = ctx->variables;
+                ctx->variables = item;
+            } // if
+        } // if
+
+        // Add the symbol.
+        const char *namecpy = StrDup(ctx, (const char *) (start + name));
+        if (namecpy == NULL)
+            return;
+
+        MOJOSHADER_symbol *sym = &ctab->symbols[i];
+        sym->name = namecpy;
+        sym->register_set = (MOJOSHADER_symbolRegisterSet) regset;
+        sym->register_index = (unsigned int) regidx;
+        sym->register_count = (unsigned int) regcnt;
+        if (!parse_ctab_typeinfo(ctx, start, bytes, typeinf, &sym->info))
+            goto corrupt_ctab;  // sym->name will get free()'d later.
+        else if (ctx->out_of_memory)
+            return;  // just bail now.
+    } // for
+
+    return;
+
+corrupt_ctab:
+    fail(ctx, "Shader has corrupt CTAB data");
+} // parse_constant_table
+
+
+static void free_symbols(MOJOSHADER_free f, void *d, MOJOSHADER_symbol *syms,
+                         const int symcount);
+
+
+static int is_comment_token(Context *ctx, const uint32 tok, uint32 *tokcount)
+{
+    const uint32 token = SWAP32(tok);
+    if ((token & 0xFFFF) == 0xFFFE)  // actually a comment token?
+    {
+        if ((token & 0x80000000) != 0)
+            fail(ctx, "comment token high bit must be zero.");  // so says msdn.
+        *tokcount = ((token >> 16) & 0xFFFF);
+        return 1;
+    } // if
+
+    return 0;
+} // is_comment_token
+
+
+typedef struct PreshaderBlockInfo
+{
+    const uint32 *tokens;
+    uint32 tokcount;
+    int seen;
+} PreshaderBlockInfo;
+
+// Preshaders only show up in compiled Effect files. The format is
+//  undocumented, and even the instructions aren't the same opcodes as you
+//  would find in a regular shader. These things show up because the HLSL
+//  compiler can detect work that sets up constant registers that could
+//  be moved out of the shader itself. Preshaders run once, then the shader
+//  itself runs many times, using the constant registers the preshader has set
+//  up. There are cases where the preshaders are 3+ times as many instructions
+//  as the shader itself, so this can be a big performance win.
+// My presumption is that Microsoft's Effects framework runs the preshaders on
+//  the CPU, then loads the constant register file appropriately before handing
+//  off to the GPU. As such, we do the same.
+static void parse_preshader(Context *ctx, uint32 tokcount)
+{
+    const uint32 *tokens = ctx->tokens;
+    if ((tokcount < 2) || (SWAP32(tokens[1]) != PRES_ID))
+        return;  // not a preshader.
+
+#if !SUPPORT_PRESHADERS
+    fail(ctx, "Preshader found, but preshader support is disabled!");
+#else
+
+    assert(ctx->have_preshader == 0);  // !!! FIXME: can you have more than one?
+    ctx->have_preshader = 1;
+
+    // !!! FIXME: I don't know what specific versions signify, but we need to
+    // !!! FIXME:  save this to test against the CTAB version field, if
+    // !!! FIXME:  nothing else.
+    // !!! FIXME: 0x02 0x01 is probably the version (fx_2_1),
+    // !!! FIXME:  and 0x4658 is the magic, like a real shader's version token.
+    const uint32 okay_version = 0x46580201;
+    if (SWAP32(tokens[2]) != okay_version)
+    {
+        fail(ctx, "Unsupported preshader version.");
+        return;  // fail because the shader will malfunction w/o this.
+    } // if
+
+    tokens += 3;
+    tokcount -= 3;
+
+    // All sections of a preshader are packed into separate comment tokens,
+    //  inside the containing comment token block. Find them all before
+    //  we start, so we don't care about the order they appear in the file.
+    PreshaderBlockInfo ctab = { 0, 0, 0 };
+    PreshaderBlockInfo prsi = { 0, 0, 0 };
+    PreshaderBlockInfo fxlc = { 0, 0, 0 };
+    PreshaderBlockInfo clit = { 0, 0, 0 };
+
+    while (tokcount > 0)
+    {
+        uint32 subtokcount = 0;
+        if ( (!is_comment_token(ctx, *tokens, &subtokcount)) ||
+             (subtokcount > tokcount) )
+        {
+            fail(ctx, "Bogus preshader data.");
+            return;
+        } // if
+
+        tokens++;
+        tokcount--;
+
+        const uint32 *nexttokens = tokens + subtokcount;
+        const uint32 nexttokcount = tokcount - subtokcount;
+
+        if (subtokcount > 0)
+        {
+            switch (SWAP32(*tokens))
+            {
+                #define PRESHADER_BLOCK_CASE(id, var) \
+                    case id##_ID: { \
+                        if (var.seen) { \
+                            fail(ctx, "Multiple " #id " preshader blocks."); \
+                            return; \
+                        } \
+                        var.tokens = tokens; \
+                        var.tokcount = subtokcount; \
+                        var.seen = 1; \
+                        break; \
+                    }
+                PRESHADER_BLOCK_CASE(CTAB, ctab);
+                PRESHADER_BLOCK_CASE(PRSI, prsi);
+                PRESHADER_BLOCK_CASE(FXLC, fxlc);
+                PRESHADER_BLOCK_CASE(CLIT, clit);
+                default: fail(ctx, "Bogus preshader section."); return;
+                #undef PRESHADER_BLOCK_CASE
+            } // switch
+        } // if
+
+        tokens = nexttokens;
+        tokcount = nexttokcount;
+    } // while
+
+    if (!ctab.seen) { fail(ctx, "No CTAB block in preshader."); return; }
+    if (!prsi.seen) { fail(ctx, "No PRSI block in preshader."); return; }
+    if (!fxlc.seen) { fail(ctx, "No FXLC block in preshader."); return; }
+    if (!clit.seen) { fail(ctx, "No CLIT block in preshader."); return; }
+
+    MOJOSHADER_preshader *preshader = (MOJOSHADER_preshader *)
+                                    Malloc(ctx, sizeof (MOJOSHADER_preshader));
+    if (preshader == NULL)
+        return;
+    memset(preshader, '\0', sizeof (MOJOSHADER_preshader));
+    ctx->preshader = preshader;
+
+    // Let's set up the constant literals first...
+    if (clit.tokcount == 0)
+        fail(ctx, "Bogus CLIT block in preshader.");
+    else
+    {
+        const uint32 lit_count = SWAP32(clit.tokens[1]);
+        if (lit_count > ((clit.tokcount - 2) / 2))
+        {
+            fail(ctx, "Bogus CLIT block in preshader.");
+            return;
+        } // if
+        else if (lit_count > 0)
+        {
+            preshader->literal_count = (unsigned int) lit_count;
+            assert(sizeof (double) == 8);  // just in case.
+            const size_t len = sizeof (double) * lit_count;
+            preshader->literals = (double *) Malloc(ctx, len);
+            if (preshader->literals == NULL)
+                return;  // oh well.
+            const double *litptr = (const double *) (clit.tokens + 2);
+            int i;
+            for (i = 0; i < lit_count; i++)
+                preshader->literals[i] = SWAPDBL(litptr[i]);
+        } // else if
+    } // else
+
+    // Parse out the PRSI block. This is used to map the output registers.
+    if (prsi.tokcount < 8)
+    {
+        fail(ctx, "Bogus preshader PRSI data");
+        return;
+    } // if
+
+    //const uint32 first_output_reg = SWAP32(prsi.tokens[1]);
+    // !!! FIXME: there are a lot of fields here I don't know about.
+    // !!! FIXME:  maybe [2] and [3] are for int4 and bool registers?
+    //const uint32 output_reg_count = SWAP32(prsi.tokens[4]);
+    // !!! FIXME:  maybe [5] and [6] are for int4 and bool registers?
+    const uint32 output_map_count = SWAP32(prsi.tokens[7]);
+
+    prsi.tokcount -= 8;
+    prsi.tokens += 8;
+
+    if (prsi.tokcount < ((output_map_count + 1) * 2))
+    {
+        fail(ctx, "Bogus preshader PRSI data");
+        return;
+    } // if
+
+    const uint32 *output_map = prsi.tokens;
+
+    // Now we'll figure out the CTAB...
+    CtabData ctabdata = { 0, 0, 0 };
+    parse_constant_table(ctx, ctab.tokens - 1, ctab.tokcount * 4,
+                         okay_version, 0, &ctabdata);
+
+    // preshader owns this now. Don't free it in this function.
+    preshader->symbol_count = ctabdata.symbol_count;
+    preshader->symbols = ctabdata.symbols;
+
+    if (!ctabdata.have_ctab)
+    {
+        fail(ctx, "Bogus preshader CTAB data");
+        return;
+    } // if
+
+    // The FXLC block has the actual instructions...
+    uint32 opcode_count = SWAP32(fxlc.tokens[1]);
+
+    size_t len = sizeof (MOJOSHADER_preshaderInstruction) * opcode_count;
+    preshader->instruction_count = (unsigned int) opcode_count;
+    preshader->instructions = (MOJOSHADER_preshaderInstruction *)
+                                Malloc(ctx, len);
+    if (preshader->instructions == NULL)
+        return;
+    memset(preshader->instructions, '\0', len);
+
+    fxlc.tokens += 2;
+    fxlc.tokcount -= 2;
+    if (opcode_count > (fxlc.tokcount / 2))
+    {
+        fail(ctx, "Bogus preshader FXLC block.");
+        return;
+    } // if
+
+    MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
+    while (opcode_count--)
+    {
+        const uint32 opcodetok = SWAP32(fxlc.tokens[0]);
+        MOJOSHADER_preshaderOpcode opcode = MOJOSHADER_PRESHADEROP_NOP;
+        switch ((opcodetok >> 16) & 0xFFFF)
+        {
+            case 0x1000: opcode = MOJOSHADER_PRESHADEROP_MOV; break;
+            case 0x1010: opcode = MOJOSHADER_PRESHADEROP_NEG; break;
+            case 0x1030: opcode = MOJOSHADER_PRESHADEROP_RCP; break;
+            case 0x1040: opcode = MOJOSHADER_PRESHADEROP_FRC; break;
+            case 0x1050: opcode = MOJOSHADER_PRESHADEROP_EXP; break;
+            case 0x1060: opcode = MOJOSHADER_PRESHADEROP_LOG; break;
+            case 0x1070: opcode = MOJOSHADER_PRESHADEROP_RSQ; break;
+            case 0x1080: opcode = MOJOSHADER_PRESHADEROP_SIN; break;
+            case 0x1090: opcode = MOJOSHADER_PRESHADEROP_COS; break;
+            case 0x10A0: opcode = MOJOSHADER_PRESHADEROP_ASIN; break;
+            case 0x10B0: opcode = MOJOSHADER_PRESHADEROP_ACOS; break;
+            case 0x10C0: opcode = MOJOSHADER_PRESHADEROP_ATAN; break;
+            case 0x2000: opcode = MOJOSHADER_PRESHADEROP_MIN; break;
+            case 0x2010: opcode = MOJOSHADER_PRESHADEROP_MAX; break;
+            case 0x2020: opcode = MOJOSHADER_PRESHADEROP_LT; break;
+            case 0x2030: opcode = MOJOSHADER_PRESHADEROP_GE; break;
+            case 0x2040: opcode = MOJOSHADER_PRESHADEROP_ADD; break;
+            case 0x2050: opcode = MOJOSHADER_PRESHADEROP_MUL; break;
+            case 0x2060: opcode = MOJOSHADER_PRESHADEROP_ATAN2; break;
+            case 0x2080: opcode = MOJOSHADER_PRESHADEROP_DIV; break;
+            case 0x3000: opcode = MOJOSHADER_PRESHADEROP_CMP; break;
+            case 0x3010: opcode = MOJOSHADER_PRESHADEROP_MOVC; break;
+            case 0x5000: opcode = MOJOSHADER_PRESHADEROP_DOT; break;
+            case 0x5020: opcode = MOJOSHADER_PRESHADEROP_NOISE; break;
+            case 0xA000: opcode = MOJOSHADER_PRESHADEROP_MIN_SCALAR; break;
+            case 0xA010: opcode = MOJOSHADER_PRESHADEROP_MAX_SCALAR; break;
+            case 0xA020: opcode = MOJOSHADER_PRESHADEROP_LT_SCALAR; break;
+            case 0xA030: opcode = MOJOSHADER_PRESHADEROP_GE_SCALAR; break;
+            case 0xA040: opcode = MOJOSHADER_PRESHADEROP_ADD_SCALAR; break;
+            case 0xA050: opcode = MOJOSHADER_PRESHADEROP_MUL_SCALAR; break;
+            case 0xA060: opcode = MOJOSHADER_PRESHADEROP_ATAN2_SCALAR; break;
+            case 0xA080: opcode = MOJOSHADER_PRESHADEROP_DIV_SCALAR; break;
+            case 0xD000: opcode = MOJOSHADER_PRESHADEROP_DOT_SCALAR; break;
+            case 0xD020: opcode = MOJOSHADER_PRESHADEROP_NOISE_SCALAR; break;
+            default: fail(ctx, "Unknown preshader opcode."); break;
+        } // switch
+
+        uint32 operand_count = SWAP32(fxlc.tokens[1]) + 1;  // +1 for dest.
+
+        inst->opcode = opcode;
+        inst->element_count = (unsigned int) (opcodetok & 0xFF);
+        inst->operand_count = (unsigned int) operand_count;
+
+        fxlc.tokens += 2;
+        fxlc.tokcount -= 2;
+        if ((operand_count * 3) > fxlc.tokcount)
+        {
+            fail(ctx, "Bogus preshader FXLC block.");
+            return;
+        } // if
+
+        MOJOSHADER_preshaderOperand *operand = inst->operands;
+        while (operand_count--)
+        {
+            const unsigned int item = (unsigned int) SWAP32(fxlc.tokens[2]);
+
+            // !!! FIXME: don't know what first token does.
+            switch (SWAP32(fxlc.tokens[1]))
+            {
+                case 1:  // literal from CLIT block.
+                {
+                    if (item >= preshader->literal_count)
+                    {
+                        fail(ctx, "Bogus preshader literal index.");
+                        break;
+                    } // if
+                    operand->type = MOJOSHADER_PRESHADEROPERAND_LITERAL;
+                    break;
+                } // case
+
+                case 2:  // item from ctabdata.
+                {
+                    int i;
+                    MOJOSHADER_symbol *sym = ctabdata.symbols;
+                    for (i = 0; i < ctabdata.symbol_count; i++, sym++)
+                    {
+                        const uint32 base = sym->register_index * 4;
+                        const uint32 count = sym->register_count * 4;
+                        assert(sym->register_set==MOJOSHADER_SYMREGSET_FLOAT4);
+                        if ( (base <= item) && ((base + count) > item) )
+                            break;
+                    } // for
+                    if (i == ctabdata.symbol_count)
+                    {
+                        fail(ctx, "Bogus preshader input index.");
+                        break;
+                    } // if
+                    operand->type = MOJOSHADER_PRESHADEROPERAND_INPUT;
+                    break;
+                } // case
+
+                case 4:
+                {
+                    int i;
+                    for (i = 0; i < output_map_count; i++)
+                    {
+                        const uint32 base = output_map[(i*2)] * 4;
+                        const uint32 count = output_map[(i*2)+1] * 4;
+                        if ( (base <= item) && ((base + count) > item) )
+                            break;
+                    } // for
+                    if (i == output_map_count)
+                    {
+                        fail(ctx, "Bogus preshader output index.");
+                        break;
+                    } // if
+
+                    operand->type = MOJOSHADER_PRESHADEROPERAND_OUTPUT;
+                    break;
+                } // case
+
+                case 7:
+                {
+                    operand->type = MOJOSHADER_PRESHADEROPERAND_TEMP;
+                    if (item >= preshader->temp_count)
+                        preshader->temp_count = item + 1;
+                    break;
+                } // case
+            } // switch
+
+            operand->index = item;
+
+            fxlc.tokens += 3;
+            fxlc.tokcount -= 3;
+            operand++;
+        } // while
+
+        inst++;
+    } // while
+#endif
+} // parse_preshader
+
+
+static int parse_comment_token(Context *ctx)
+{
+    uint32 commenttoks = 0;
+    if (is_comment_token(ctx, *ctx->tokens, &commenttoks))
+    {
+        if ((commenttoks >= 1) && (commenttoks < ctx->tokencount))
+        {
+            const uint32 id = SWAP32(ctx->tokens[1]);
+            if (id == PRES_ID)
+                parse_preshader(ctx, commenttoks);
+            else if (id == CTAB_ID)
+            {
+                parse_constant_table(ctx, ctx->tokens, commenttoks * 4,
+                                     ctx->version_token, 1, &ctx->ctab);
+            } // else if
+        } // if
+        return commenttoks + 1;  // comment data plus the initial token.
+    } // if
+
+    return 0;  // not a comment token.
+} // parse_comment_token
+
+
+static int parse_end_token(Context *ctx)
+{
+    if (SWAP32(*(ctx->tokens)) != 0x0000FFFF)   // end token always 0x0000FFFF.
+        return 0;  // not us, eat no tokens.
+
+    if (ctx->tokencount != 1)  // we _must_ be last. If not: fail.
+        fail(ctx, "end token before end of stream");
+
+    if (!isfail(ctx))
+        ctx->profile->end_emitter(ctx);
+
+    return 1;
+} // parse_end_token
+
+
+static int parse_phase_token(Context *ctx)
+{
+    // !!! FIXME: needs state; allow only one phase token per shader, I think?
+    if (SWAP32(*(ctx->tokens)) != 0x0000FFFD) // phase token always 0x0000FFFD.
+        return 0;  // not us, eat no tokens.
+
+    if ( (!shader_is_pixel(ctx)) || (!shader_version_exactly(ctx, 1, 4)) )
+        fail(ctx, "phase token only available in 1.4 pixel shaders");
+
+    if (!isfail(ctx))
+        ctx->profile->phase_emitter(ctx);
+
+    return 1;
+} // parse_phase_token
+
+
+static int parse_token(Context *ctx)
+{
+    int rc = 0;
+
+    assert(ctx->output_stack_len == 0);
+
+    if (ctx->tokencount == 0)
+        fail(ctx, "unexpected end of shader.");
+
+    else if ((rc = parse_comment_token(ctx)) != 0)
+        return rc;
+
+    else if ((rc = parse_end_token(ctx)) != 0)
+        return rc;
+
+    else if ((rc = parse_phase_token(ctx)) != 0)
+        return rc;
+
+    else if ((rc = parse_instruction_token(ctx)) != 0)
+        return rc;
+
+    failf(ctx, "unknown token (0x%x)", (uint) *ctx->tokens);
+    return 1;  // good luck!
+} // parse_token
+
+
+static int find_profile_id(const char *profile)
+{
+    size_t i;
+    for (i = 0; i < STATICARRAYLEN(profileMap); i++)
+    {
+        const char *name = profileMap[i].from;
+        if (strcmp(name, profile) == 0)
+        {
+            profile = profileMap[i].to;
+            break;
+        } // if
+    } // for
+
+    for (i = 0; i < STATICARRAYLEN(profiles); i++)
+    {
+        const char *name = profiles[i].name;
+        if (strcmp(name, profile) == 0)
+            return i;
+    } // for
+
+    return -1;  // no match.
+} // find_profile_id
+
+
+static Context *build_context(const char *profile,
+                              const unsigned char *tokenbuf,
+                              const unsigned int bufsize,
+                              const MOJOSHADER_swizzle *swiz,
+                              const unsigned int swizcount,
+                              const MOJOSHADER_samplerMap *smap,
+                              const unsigned int smapcount,
+                              MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    if (m == NULL) m = MOJOSHADER_internal_malloc;
+    if (f == NULL) f = MOJOSHADER_internal_free;
+
+    Context *ctx = (Context *) m(sizeof (Context), d);
+    if (ctx == NULL)
+        return NULL;
+
+    memset(ctx, '\0', sizeof (Context));
+    ctx->malloc = m;
+    ctx->free = f;
+    ctx->malloc_data = d;
+    ctx->tokens = (const uint32 *) tokenbuf;
+    ctx->orig_tokens = (const uint32 *) tokenbuf;
+    ctx->tokencount = bufsize / sizeof (uint32);
+    ctx->swizzles = swiz;
+    ctx->swizzles_count = swizcount;
+    ctx->samplermap = smap;
+    ctx->samplermap_count = smapcount;
+    ctx->endline = ENDLINE_STR;
+    ctx->endline_len = strlen(ctx->endline);
+    ctx->last_address_reg_component = -1;
+    ctx->current_position = MOJOSHADER_POSITION_BEFORE;
+    ctx->texm3x2pad_dst0 = -1;
+    ctx->texm3x2pad_src0 = -1;
+    ctx->texm3x3pad_dst0 = -1;
+    ctx->texm3x3pad_src0 = -1;
+    ctx->texm3x3pad_dst1 = -1;
+    ctx->texm3x3pad_src1 = -1;
+
+    ctx->errors = errorlist_create(MallocBridge, FreeBridge, ctx);
+    if (ctx->errors == NULL)
+    {
+        f(ctx, d);
+        return NULL;
+    } // if
+
+    if (!set_output(ctx, &ctx->mainline))
+    {
+        errorlist_destroy(ctx->errors);
+        f(ctx, d);
+        return NULL;
+    } // if
+
+    const int profileid = find_profile_id(profile);
+    ctx->profileid = profileid;
+    if (profileid >= 0)
+        ctx->profile = &profiles[profileid];
+    else
+        failf(ctx, "Profile '%s' is unknown or unsupported", profile);
+
+    return ctx;
+} // build_context
+
+
+static void free_constants_list(MOJOSHADER_free f, void *d, ConstantsList *item)
+{
+    while (item != NULL)
+    {
+        ConstantsList *next = item->next;
+        f(item, d);
+        item = next;
+    } // while
+} // free_constants_list
+
+
+static void free_variable_list(MOJOSHADER_free f, void *d, VariableList *item)
+{
+    while (item != NULL)
+    {
+        VariableList *next = item->next;
+        f(item, d);
+        item = next;
+    } // while
+} // free_variable_list
+
+
+static void free_sym_typeinfo(MOJOSHADER_free f, void *d,
+                              MOJOSHADER_symbolTypeInfo *typeinfo)
+{
+    int i;
+    for (i = 0; i < typeinfo->member_count; i++)
+    {
+        f((void *) typeinfo->members[i].name, d);
+        free_sym_typeinfo(f, d, &typeinfo->members[i].info);
+    } // for
+    f((void *) typeinfo->members, d);
+} // free_sym_members
+
+
+static void free_symbols(MOJOSHADER_free f, void *d, MOJOSHADER_symbol *syms,
+                         const int symcount)
+{
+    int i;
+    for (i = 0; i < symcount; i++)
+    {
+        f((void *) syms[i].name, d);
+        free_sym_typeinfo(f, d, &syms[i].info);
+    } // for
+    f((void *) syms, d);
+} // free_symbols
+
+
+static void free_preshader(MOJOSHADER_free f, void *d,
+                           MOJOSHADER_preshader *preshader)
+{
+    if (preshader != NULL)
+    {
+        f((void *) preshader->literals, d);
+        f((void *) preshader->instructions, d);
+        free_symbols(f, d, preshader->symbols, preshader->symbol_count);
+        f((void *) preshader, d);
+    } // if
+} // free_preshader
+
+
+static void destroy_context(Context *ctx)
+{
+    if (ctx != NULL)
+    {
+        MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
+        void *d = ctx->malloc_data;
+        buffer_destroy(ctx->preflight);
+        buffer_destroy(ctx->globals);
+        buffer_destroy(ctx->helpers);
+        buffer_destroy(ctx->subroutines);
+        buffer_destroy(ctx->mainline_intro);
+        buffer_destroy(ctx->mainline);
+        buffer_destroy(ctx->ignore);
+        free_constants_list(f, d, ctx->constants);
+        free_reglist(f, d, ctx->used_registers.next);
+        free_reglist(f, d, ctx->defined_registers.next);
+        free_reglist(f, d, ctx->uniforms.next);
+        free_reglist(f, d, ctx->attributes.next);
+        free_reglist(f, d, ctx->samplers.next);
+        free_variable_list(f, d, ctx->variables);
+        errorlist_destroy(ctx->errors);
+        free_symbols(f, d, ctx->ctab.symbols, ctx->ctab.symbol_count);
+        free_preshader(f, d, ctx->preshader);
+        f(ctx, d);
+    } // if
+} // destroy_context
+
+
+static char *build_output(Context *ctx, size_t *len)
+{
+    // add a byte for a null terminator.
+    Buffer *buffers[] = {
+        ctx->preflight, ctx->globals, ctx->helpers,
+        ctx->subroutines, ctx->mainline_intro, ctx->mainline
+        // don't append ctx->ignore ... that's why it's called "ignore"
+    };
+    char *retval = buffer_merge(buffers, STATICARRAYLEN(buffers), len);
+    return retval;
+} // build_output
+
+
+static inline const char *alloc_varname(Context *ctx, const RegisterList *reg)
+{
+    return ctx->profile->get_varname(ctx, reg->regtype, reg->regnum);
+} // alloc_varname
+
+
+// !!! FIXME: this code is sort of hard to follow:
+// !!! FIXME:  "var->used" only applies to arrays (at the moment, at least,
+// !!! FIXME:  but this might be buggy at a later time?), and this code
+// !!! FIXME:  relies on that.
+// !!! FIXME: "variables" means "things we found in a CTAB" but it's not
+// !!! FIXME:  all registers, etc.
+// !!! FIXME: "const_array" means an array for d3d "const" registers (c0, c1,
+// !!! FIXME:  etc), but not a constant array, although they _can_ be.
+// !!! FIXME: It's just a mess.  :/
+static MOJOSHADER_uniform *build_uniforms(Context *ctx)
+{
+    const size_t len = sizeof (MOJOSHADER_uniform) * ctx->uniform_count;
+    MOJOSHADER_uniform *retval = (MOJOSHADER_uniform *) Malloc(ctx, len);
+
+    if (retval != NULL)
+    {
+        MOJOSHADER_uniform *wptr = retval;
+        memset(wptr, '\0', len);
+
+        VariableList *var;
+        int written = 0;
+        for (var = ctx->variables; var != NULL; var = var->next)
+        {
+            if (var->used)
+            {
+                const char *name = ctx->profile->get_const_array_varname(ctx,
+                                                      var->index, var->count);
+                if (name != NULL)
+                {
+                    wptr->type = MOJOSHADER_UNIFORM_FLOAT;
+                    wptr->index = var->index;
+                    wptr->array_count = var->count;
+                    wptr->constant = (var->constant != NULL) ? 1 : 0;
+                    wptr->name = name;
+                    wptr++;
+                    written++;
+                } // if
+            } // if
+        } // for
+
+        RegisterList *item = ctx->uniforms.next;
+        MOJOSHADER_uniformType type = MOJOSHADER_UNIFORM_FLOAT;
+        while (written < ctx->uniform_count)
+        {
+            int skip = 0;
+
+            // !!! FIXME: does this fail if written > ctx->uniform_count?
+            if (item == NULL)
+            {
+                fail(ctx, "BUG: mismatched uniform list and count");
+                break;
+            } // if
+
+            int index = item->regnum;
+            switch (item->regtype)
+            {
+                case REG_TYPE_CONST:
+                    skip = (item->array != NULL);
+                    type = MOJOSHADER_UNIFORM_FLOAT;
+                    break;
+
+                case REG_TYPE_CONSTINT:
+                    type = MOJOSHADER_UNIFORM_INT;
+                    break;
+
+                case REG_TYPE_CONSTBOOL:
+                    type = MOJOSHADER_UNIFORM_BOOL;
+                    break;
+
+                default:
+                    fail(ctx, "unknown uniform datatype");
+                    break;
+            } // switch
+
+            if (!skip)
+            {
+                wptr->type = type;
+                wptr->index = index;
+                wptr->array_count = 0;
+                wptr->name = alloc_varname(ctx, item);
+                wptr++;
+                written++;
+            } // if
+
+            item = item->next;
+        } // for
+    } // if
+
+    return retval;
+} // build_uniforms
+
+
+static MOJOSHADER_constant *build_constants(Context *ctx)
+{
+    const size_t len = sizeof (MOJOSHADER_constant) * ctx->constant_count;
+    MOJOSHADER_constant *retval = (MOJOSHADER_constant *) Malloc(ctx, len);
+
+    if (retval != NULL)
+    {
+        ConstantsList *item = ctx->constants;
+        int i;
+
+        for (i = 0; i < ctx->constant_count; i++)
+        {
+            if (item == NULL)
+            {
+                fail(ctx, "BUG: mismatched constant list and count");
+                break;
+            } // if
+
+            memcpy(&retval[i], &item->constant, sizeof (MOJOSHADER_constant));
+            item = item->next;
+        } // for
+    } // if
+
+    return retval;
+} // build_constants
+
+
+static MOJOSHADER_sampler *build_samplers(Context *ctx)
+{
+    const size_t len = sizeof (MOJOSHADER_sampler) * ctx->sampler_count;
+    MOJOSHADER_sampler *retval = (MOJOSHADER_sampler *) Malloc(ctx, len);
+
+    if (retval != NULL)
+    {
+        RegisterList *item = ctx->samplers.next;
+        int i;
+
+        memset(retval, '\0', len);
+
+        for (i = 0; i < ctx->sampler_count; i++)
+        {
+            if (item == NULL)
+            {
+                fail(ctx, "BUG: mismatched sampler list and count");
+                break;
+            } // if
+
+            assert(item->regtype == REG_TYPE_SAMPLER);
+            retval[i].type = cvtD3DToMojoSamplerType((TextureType) item->index);
+            retval[i].index = item->regnum;
+            retval[i].name = alloc_varname(ctx, item);
+            retval[i].texbem = (item->misc != 0) ? 1 : 0;
+            item = item->next;
+        } // for
+    } // if
+
+    return retval;
+} // build_samplers
+
+
+static MOJOSHADER_attribute *build_attributes(Context *ctx, int *_count)
+{
+    int count = 0;
+
+    if (ctx->attribute_count == 0)
+    {
+        *_count = 0;
+        return NULL;  // nothing to do.
+    } // if
+
+    const size_t len = sizeof (MOJOSHADER_attribute) * ctx->attribute_count;
+    MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *) Malloc(ctx, len);
+
+    if (retval != NULL)
+    {
+        RegisterList *item = ctx->attributes.next;
+        MOJOSHADER_attribute *wptr = retval;
+        int ignore = 0;
+        int i;
+
+        memset(retval, '\0', len);
+
+        for (i = 0; i < ctx->attribute_count; i++)
+        {
+            if (item == NULL)
+            {
+                fail(ctx, "BUG: mismatched attribute list and count");
+                break;
+            } // if
+
+            switch (item->regtype)
+            {
+                case REG_TYPE_RASTOUT:
+                case REG_TYPE_ATTROUT:
+                case REG_TYPE_TEXCRDOUT:
+                case REG_TYPE_COLOROUT:
+                case REG_TYPE_DEPTHOUT:
+                    ignore = 1;
+                    break;
+                case REG_TYPE_TEXTURE:
+                case REG_TYPE_MISCTYPE:
+                case REG_TYPE_INPUT:
+                    ignore = shader_is_pixel(ctx);
+                    break;
+                default:
+                    ignore = 0;
+                    break;
+            } // switch
+
+            if (!ignore)
+            {
+                if (shader_is_pixel(ctx))
+                    fail(ctx, "BUG: pixel shader with vertex attributes");
+                else
+                {
+                    wptr->usage = item->usage;
+                    wptr->index = item->index;
+                    wptr->name = alloc_varname(ctx, item);
+                    wptr++;
+                    count++;
+                } // else
+            } // if
+
+            item = item->next;
+        } // for
+    } // if
+
+    *_count = count;
+    return retval;
+} // build_attributes
+
+static MOJOSHADER_attribute *build_outputs(Context *ctx, int *_count)
+{
+    int count = 0;
+
+    if (ctx->attribute_count == 0)
+    {
+        *_count = 0;
+        return NULL;  // nothing to do.
+    } // if
+
+    const size_t len = sizeof (MOJOSHADER_attribute) * ctx->attribute_count;
+    MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *) Malloc(ctx, len);
+
+    if (retval != NULL)
+    {
+        RegisterList *item = ctx->attributes.next;
+        MOJOSHADER_attribute *wptr = retval;
+        int i;
+
+        memset(retval, '\0', len);
+
+        for (i = 0; i < ctx->attribute_count; i++)
+        {
+            if (item == NULL)
+            {
+                fail(ctx, "BUG: mismatched attribute list and count");
+                break;
+            } // if
+
+            switch (item->regtype)
+            {
+                case REG_TYPE_RASTOUT:
+                case REG_TYPE_ATTROUT:
+                case REG_TYPE_TEXCRDOUT:
+                case REG_TYPE_COLOROUT:
+                case REG_TYPE_DEPTHOUT:
+                    wptr->usage = item->usage;
+                    wptr->index = item->index;
+                    wptr->name = alloc_varname(ctx, item);
+                    wptr++;
+                    count++;
+                    break;
+                default:
+                    break;
+            } // switch
+
+
+            item = item->next;
+        } // for
+    } // if
+
+    *_count = count;
+    return retval;
+} // build_outputs
+
+
+static MOJOSHADER_parseData *build_parsedata(Context *ctx)
+{
+    char *output = NULL;
+    MOJOSHADER_constant *constants = NULL;
+    MOJOSHADER_uniform *uniforms = NULL;
+    MOJOSHADER_attribute *attributes = NULL;
+    MOJOSHADER_attribute *outputs = NULL;
+    MOJOSHADER_sampler *samplers = NULL;
+    MOJOSHADER_swizzle *swizzles = NULL;
+    MOJOSHADER_error *errors = NULL;
+    MOJOSHADER_parseData *retval = NULL;
+    size_t output_len = 0;
+    int attribute_count = 0;
+    int output_count = 0;
+
+    if (ctx->out_of_memory)
+        return &MOJOSHADER_out_of_mem_data;
+
+    retval = (MOJOSHADER_parseData*) Malloc(ctx, sizeof(MOJOSHADER_parseData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_parseData));
+
+    if (!isfail(ctx))
+        output = build_output(ctx, &output_len);
+
+    if (!isfail(ctx))
+        constants = build_constants(ctx);
+
+    if (!isfail(ctx))
+        uniforms = build_uniforms(ctx);
+
+    if (!isfail(ctx))
+        attributes = build_attributes(ctx, &attribute_count);
+
+    if (!isfail(ctx))
+        outputs = build_outputs(ctx, &output_count);
+
+    if (!isfail(ctx))
+        samplers = build_samplers(ctx);
+
+    const int error_count = errorlist_count(ctx->errors);
+    errors = errorlist_flatten(ctx->errors);
+
+    if (!isfail(ctx))
+    {
+        if (ctx->swizzles_count > 0)
+        {
+            const int len = ctx->swizzles_count * sizeof (MOJOSHADER_swizzle);
+            swizzles = (MOJOSHADER_swizzle *) Malloc(ctx, len);
+            if (swizzles != NULL)
+                memcpy(swizzles, ctx->swizzles, len);
+        } // if
+    } // if
+
+    // check again, in case build_output, etc, ran out of memory.
+    if (isfail(ctx))
+    {
+        int i;
+
+        Free(ctx, output);
+        Free(ctx, constants);
+        Free(ctx, swizzles);
+
+        if (uniforms != NULL)
+        {
+            for (i = 0; i < ctx->uniform_count; i++)
+                Free(ctx, (void *) uniforms[i].name);
+            Free(ctx, uniforms);
+        } // if
+
+        if (attributes != NULL)
+        {
+            for (i = 0; i < attribute_count; i++)
+                Free(ctx, (void *) attributes[i].name);
+            Free(ctx, attributes);
+        } // if
+
+        if (outputs != NULL)
+        {
+            for (i = 0; i < output_count; i++)
+                Free(ctx, (void *) outputs[i].name);
+            Free(ctx, outputs);
+        } // if
+
+        if (samplers != NULL)
+        {
+            for (i = 0; i < ctx->sampler_count; i++)
+                Free(ctx, (void *) samplers[i].name);
+            Free(ctx, samplers);
+        } // if
+
+        if (ctx->out_of_memory)
+        {
+            for (i = 0; i < error_count; i++)
+            {
+                Free(ctx, (void *) errors[i].filename);
+                Free(ctx, (void *) errors[i].error);
+            } // for
+            Free(ctx, errors);
+            Free(ctx, retval);
+            return &MOJOSHADER_out_of_mem_data;
+        } // if
+    } // if
+    else
+    {
+        retval->profile = ctx->profile->name;
+        retval->output = output;
+        retval->output_len = (int) output_len;
+        retval->instruction_count = ctx->instruction_count;
+        retval->shader_type = ctx->shader_type;
+        retval->major_ver = (int) ctx->major_ver;
+        retval->minor_ver = (int) ctx->minor_ver;
+        retval->uniform_count = ctx->uniform_count;
+        retval->uniforms = uniforms;
+        retval->constant_count = ctx->constant_count;
+        retval->constants = constants;
+        retval->sampler_count = ctx->sampler_count;
+        retval->samplers = samplers;
+        retval->attribute_count = attribute_count;
+        retval->attributes = attributes;
+        retval->output_count = output_count;
+        retval->outputs = outputs;
+        retval->swizzle_count = ctx->swizzles_count;
+        retval->swizzles = swizzles;
+        retval->symbol_count = ctx->ctab.symbol_count;
+        retval->symbols = ctx->ctab.symbols;
+        retval->preshader = ctx->preshader;
+
+        // we don't own these now, retval does.
+        ctx->ctab.symbols = NULL;
+        ctx->preshader = NULL;
+        ctx->ctab.symbol_count = 0;
+    } // else
+
+    retval->error_count = error_count;
+    retval->errors = errors;
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+
+    return retval;
+} // build_parsedata
+
+
+static void process_definitions(Context *ctx)
+{
+    // !!! FIXME: apparently, pre ps_3_0, sampler registers don't need to be
+    // !!! FIXME:  DCL'd before use (default to 2d?). We aren't checking
+    // !!! FIXME:  this at the moment, though.
+
+    determine_constants_arrays(ctx);  // in case this hasn't been called yet.
+
+    RegisterList *uitem = &ctx->uniforms;
+    RegisterList *prev = &ctx->used_registers;
+    RegisterList *item = prev->next;
+
+    while (item != NULL)
+    {
+        RegisterList *next = item->next;
+        const RegisterType regtype = item->regtype;
+        const int regnum = item->regnum;
+
+        if (!get_defined_register(ctx, regtype, regnum))
+        {
+            // haven't already dealt with this one.
+            switch (regtype)
+            {
+                // !!! FIXME: I'm not entirely sure this is right...
+                case REG_TYPE_RASTOUT:
+                case REG_TYPE_ATTROUT:
+                case REG_TYPE_TEXCRDOUT:
+                case REG_TYPE_COLOROUT:
+                case REG_TYPE_DEPTHOUT:
+                    if (shader_is_vertex(ctx)&&shader_version_atleast(ctx,3,0))
+                    {
+                        fail(ctx, "vs_3 can't use output registers"
+                                  " without declaring them first.");
+                        return;
+                    } // if
+
+                    // Apparently this is an attribute that wasn't DCL'd.
+                    //  Add it to the attribute list; deal with it later.
+                    add_attribute_register(ctx, regtype, regnum,
+                                           MOJOSHADER_USAGE_UNKNOWN, 0, 0xF, 0);
+                    break;
+
+                case REG_TYPE_ADDRESS:
+                case REG_TYPE_PREDICATE:
+                case REG_TYPE_TEMP:
+                case REG_TYPE_LOOP:
+                case REG_TYPE_LABEL:
+                    ctx->profile->global_emitter(ctx, regtype, regnum);
+                    break;
+
+                case REG_TYPE_CONST:
+                case REG_TYPE_CONSTINT:
+                case REG_TYPE_CONSTBOOL:
+                    // separate uniforms into a different list for now.
+                    prev->next = next;
+                    item->next = NULL;
+                    uitem->next = item;
+                    uitem = item;
+                    item = prev;
+                    break;
+
+                case REG_TYPE_INPUT:
+                    // You don't have to dcl_ your inputs in Shader Model 1.
+                    if (shader_is_pixel(ctx)&&!shader_version_atleast(ctx,2,0))
+                    {
+                        add_attribute_register(ctx, regtype, regnum,
+                                               MOJOSHADER_USAGE_COLOR, regnum,
+                                               0xF, 0);
+                        break;
+                    } // if
+                    // fall through...
+
+                default:
+                    fail(ctx, "BUG: we used a register we don't know how to define.");
+            } // switch
+        } // if
+
+        prev = item;
+        item = next;
+    } // while
+
+    // okay, now deal with uniform/constant arrays...
+    VariableList *var;
+    for (var = ctx->variables; var != NULL; var = var->next)
+    {
+        if (var->used)
+        {
+            if (var->constant)
+            {
+                ctx->profile->const_array_emitter(ctx, var->constant,
+                                                  var->index, var->count);
+            } // if
+            else
+            {
+                ctx->profile->array_emitter(ctx, var);
+                ctx->uniform_float4_count += var->count;
+                ctx->uniform_count++;
+            } // else
+        } // if
+    } // for
+
+    // ...and uniforms...
+    for (item = ctx->uniforms.next; item != NULL; item = item->next)
+    {
+        int arraysize = -1;
+
+        // check if this is a register contained in an array...
+        if (item->regtype == REG_TYPE_CONST)
+        {
+            for (var = ctx->variables; var != NULL; var = var->next)
+            {
+                if (!var->used)
+                    continue;
+
+                const int regnum = item->regnum;
+                const int lo = var->index;
+                if ( (regnum >= lo) && (regnum < (lo + var->count)) )
+                {
+                    assert(!var->constant);
+                    item->array = var;  // used when building parseData.
+                    arraysize = var->count;
+                    break;
+                } // if
+            } // for
+        } // if
+
+        ctx->profile->uniform_emitter(ctx, item->regtype, item->regnum, var);
+
+        if (arraysize < 0)  // not part of an array?
+        {
+            ctx->uniform_count++;
+            switch (item->regtype)
+            {
+                case REG_TYPE_CONST: ctx->uniform_float4_count++; break;
+                case REG_TYPE_CONSTINT: ctx->uniform_int4_count++; break;
+                case REG_TYPE_CONSTBOOL: ctx->uniform_bool_count++; break;
+                default: break;
+            } // switch
+        } // if
+    } // for
+
+    // ...and samplers...
+    for (item = ctx->samplers.next; item != NULL; item = item->next)
+    {
+        ctx->sampler_count++;
+        ctx->profile->sampler_emitter(ctx, item->regnum,
+                                      (TextureType) item->index,
+                                      item->misc != 0);
+    } // for
+
+    // ...and attributes...
+    for (item = ctx->attributes.next; item != NULL; item = item->next)
+    {
+        ctx->attribute_count++;
+        ctx->profile->attribute_emitter(ctx, item->regtype, item->regnum,
+                                        item->usage, item->index,
+                                        item->writemask, item->misc);
+    } // for
+} // process_definitions
+
+
+static void verify_swizzles(Context *ctx)
+{
+    size_t i;
+    const char *failmsg = "invalid swizzle";
+    for (i = 0; i < ctx->swizzles_count; i++)
+    {
+        const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
+        if (swiz->swizzles[0] > 3) { fail(ctx, failmsg); return; }
+        if (swiz->swizzles[1] > 3) { fail(ctx, failmsg); return; }
+        if (swiz->swizzles[2] > 3) { fail(ctx, failmsg); return; }
+        if (swiz->swizzles[3] > 3) { fail(ctx, failmsg); return; }
+    } // for
+} // verify_swizzles
+
+
+// API entry point...
+
+// !!! FIXME:
+// MSDN: "Shader validation will fail CreatePixelShader on any shader that
+//  attempts to read from a temporary register that has not been written by a
+//  previous instruction."  (true for ps_1_*, maybe others). Check this.
+
+const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
+                                             const unsigned char *tokenbuf,
+                                             const unsigned int bufsize,
+                                             const MOJOSHADER_swizzle *swiz,
+                                             const unsigned int swizcount,
+                                             const MOJOSHADER_samplerMap *smap,
+                                             const unsigned int smapcount,
+                                             MOJOSHADER_malloc m,
+                                             MOJOSHADER_free f, void *d)
+{
+    MOJOSHADER_parseData *retval = NULL;
+    Context *ctx = NULL;
+    int rc = 0;
+    int failed = 0;
+
+    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
+        return &MOJOSHADER_out_of_mem_data;  // supply both or neither.
+
+    ctx = build_context(profile, tokenbuf, bufsize, swiz, swizcount,
+                        smap, smapcount, m, f, d);
+    if (ctx == NULL)
+        return &MOJOSHADER_out_of_mem_data;
+	
+    if (isfail(ctx))
+    {
+        retval = build_parsedata(ctx);
+        destroy_context(ctx);
+        return retval;
+    } // if
+
+    verify_swizzles(ctx);
+
+    // Version token always comes first.
+    ctx->current_position = 0;
+    rc = parse_version_token(ctx, profile);
+
+    // drop out now if this definitely isn't bytecode. Saves lots of
+    //  meaningless errors flooding through.
+    if (rc < 0)
+    {
+        retval = build_parsedata(ctx);
+        destroy_context(ctx);
+        return retval;
+    } // if
+
+    if ( ((uint32) rc) > ctx->tokencount )
+    {
+        fail(ctx, "Corrupted or truncated shader");
+        ctx->tokencount = rc;
+    } // if
+
+    adjust_token_position(ctx, rc);
+
+    // parse out the rest of the tokens after the version token...
+    while (ctx->tokencount > 0)
+    {
+        // reset for each token.
+        if (isfail(ctx))
+        {
+            failed = 1;
+            ctx->isfail = 0;
+        } // if
+
+        rc = parse_token(ctx);
+        if ( ((uint32) rc) > ctx->tokencount )
+        {
+            fail(ctx, "Corrupted or truncated shader");
+            break;
+        } // if
+
+        adjust_token_position(ctx, rc);
+    } // while
+
+    ctx->current_position = MOJOSHADER_POSITION_AFTER;
+
+    // for ps_1_*, the output color is written to r0...throw an
+    //  error if this register was never written. This isn't
+    //  important for vertex shaders, or shader model 2+.
+    if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
+    {
+        if (!register_was_written(ctx, REG_TYPE_TEMP, 0))
+            fail(ctx, "r0 (pixel shader 1.x color output) never written to");
+    } // if
+
+    if (!failed)
+    {
+        process_definitions(ctx);
+        failed = isfail(ctx);
+    } // if
+
+    if (!failed)
+        ctx->profile->finalize_emitter(ctx);
+
+    ctx->isfail = failed;
+    retval = build_parsedata(ctx);
+    destroy_context(ctx);
+    return retval;
+} // MOJOSHADER_parse
+
+
+void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *_data)
+{
+    MOJOSHADER_parseData *data = (MOJOSHADER_parseData *) _data;
+    if ((data == NULL) || (data == &MOJOSHADER_out_of_mem_data))
+        return;  // no-op.
+
+    MOJOSHADER_free f = (data->free == NULL) ? MOJOSHADER_internal_free : data->free;
+    void *d = data->malloc_data;
+    int i;
+
+    // we don't f(data->profile), because that's internal static data.
+
+    f((void *) data->output, d);
+    f((void *) data->constants, d);
+    f((void *) data->swizzles, d);
+
+    for (i = 0; i < data->error_count; i++)
+    {
+        f((void *) data->errors[i].error, d);
+        f((void *) data->errors[i].filename, d);
+    } // for
+    f((void *) data->errors, d);
+
+    for (i = 0; i < data->uniform_count; i++)
+        f((void *) data->uniforms[i].name, d);
+    f((void *) data->uniforms, d);
+
+    for (i = 0; i < data->attribute_count; i++)
+        f((void *) data->attributes[i].name, d);
+    f((void *) data->attributes, d);
+
+    for (i = 0; i < data->output_count; i++)
+        f((void *) data->outputs[i].name, d);
+    f((void *) data->outputs, d);
+
+    for (i = 0; i < data->sampler_count; i++)
+        f((void *) data->samplers[i].name, d);
+    f((void *) data->samplers, d);
+
+    free_symbols(f, d, data->symbols, data->symbol_count);
+    free_preshader(f, d, data->preshader);
+
+    f(data, d);
+} // MOJOSHADER_freeParseData
+
+
+int MOJOSHADER_version(void)
+{
+    return MOJOSHADER_VERSION;
+} // MOJOSHADER_version
+
+
+const char *MOJOSHADER_changeset(void)
+{
+    return MOJOSHADER_CHANGESET;
+} // MOJOSHADER_changeset
+
+
+int MOJOSHADER_maxShaderModel(const char *profile)
+{
+    #define PROFILE_SHADER_MODEL(p,v) if (strcmp(profile, p) == 0) return v;
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_D3D, 3);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_BYTECODE, 3);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_GLSL, 3);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_GLSL120, 3);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_ARB1, 2);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_NV2, 2);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_NV3, 2);
+    PROFILE_SHADER_MODEL(MOJOSHADER_PROFILE_NV4, 3);
+    #undef PROFILE_SHADER_MODEL
+    return -1;  // unknown profile?
+} // MOJOSHADER_maxShaderModel
+
+// end of mojoshader.c ...
+

+ 3227 - 0
ThirdParty/MojoShader/mojoshader.h

@@ -0,0 +1,3227 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+#ifndef _INCL_MOJOSHADER_H_
+#define _INCL_MOJOSHADER_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* You can define this if you aren't generating mojoshader_version.h */
+#ifndef MOJOSHADER_NO_VERSION_INCLUDE
+#include "mojoshader_version.h"
+#endif
+
+#ifndef MOJOSHADER_VERSION
+#define MOJOSHADER_VERSION -1
+#endif
+
+#ifndef MOJOSHADER_CHANGESET
+#define MOJOSHADER_CHANGESET "???"
+#endif
+
+/*
+ * For determining the version of MojoShader you are using:
+ *    const int compiled_against = MOJOSHADER_VERSION;
+ *    const int linked_against = MOJOSHADER_version();
+ *
+ * The version is a single integer that increments, not a major/minor value.
+ */
+int MOJOSHADER_version(void);
+
+/*
+ * For determining the revision control changeset of MojoShader you are using:
+ *    const char *compiled_against = MOJOSHADER_CHANGESET;
+ *    const char *linked_against = MOJOSHADER_changeset();
+ *
+ * The version is an arbitrary, null-terminated ASCII string. It is probably
+ *  a hash that represents a revision control changeset, and can't be
+ *  compared to any other string to determine chronology.
+ *
+ * Do not attempt to free this string; it's statically allocated.
+ */
+const char *MOJOSHADER_changeset(void);
+
+/*
+ * These allocators work just like the C runtime's malloc() and free()
+ *  (in fact, they probably use malloc() and free() internally if you don't
+ *  specify your own allocator, but don't rely on that behaviour).
+ * (data) is the pointer you supplied when specifying these allocator
+ *  callbacks, in case you need instance-specific data...it is passed through
+ *  to your allocator unmolested, and can be NULL if you like.
+ */
+typedef void *(*MOJOSHADER_malloc)(int bytes, void *data);
+typedef void (*MOJOSHADER_free)(void *ptr, void *data);
+
+
+/*
+ * These are enum values, but they also can be used in bitmasks, so we can
+ *  test if an opcode is acceptable: if (op->shader_types & ourtype) {} ...
+ */
+typedef enum
+{
+    MOJOSHADER_TYPE_UNKNOWN  = 0,
+    MOJOSHADER_TYPE_PIXEL    = (1 << 0),
+    MOJOSHADER_TYPE_VERTEX   = (1 << 1),
+    MOJOSHADER_TYPE_GEOMETRY = (1 << 2),  /* (not supported yet.) */
+    MOJOSHADER_TYPE_ANY = 0xFFFFFFFF   /* used for bitmasks */
+} MOJOSHADER_shaderType;
+
+/*
+ * Data types for vertex attribute streams.
+ */
+typedef enum
+{
+    MOJOSHADER_ATTRIBUTE_UNKNOWN = -1,  /* housekeeping; not returned. */
+    MOJOSHADER_ATTRIBUTE_BYTE,
+    MOJOSHADER_ATTRIBUTE_UBYTE,
+    MOJOSHADER_ATTRIBUTE_SHORT,
+    MOJOSHADER_ATTRIBUTE_USHORT,
+    MOJOSHADER_ATTRIBUTE_INT,
+    MOJOSHADER_ATTRIBUTE_UINT,
+    MOJOSHADER_ATTRIBUTE_FLOAT,
+    MOJOSHADER_ATTRIBUTE_DOUBLE,
+    MOJOSHADER_ATTRIBUTE_HALF_FLOAT,  /* MAYBE available in your OpenGL! */
+} MOJOSHADER_attributeType;
+
+/*
+ * Data types for uniforms. See MOJOSHADER_uniform for more information.
+ */
+typedef enum
+{
+    MOJOSHADER_UNIFORM_UNKNOWN = -1, /* housekeeping value; never returned. */
+    MOJOSHADER_UNIFORM_FLOAT,
+    MOJOSHADER_UNIFORM_INT,
+    MOJOSHADER_UNIFORM_BOOL,
+} MOJOSHADER_uniformType;
+
+/*
+ * These are the uniforms to be set for a shader. "Uniforms" are what Direct3D
+ *  calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would
+ *  need this data, for example. These integers are register indexes. So if
+ *  index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
+ *  4-float vector to be specified for what would be register "c6" in D3D
+ *  assembly language, before drawing with the shader.
+ * (array_count) means this is an array of uniforms...this happens in some
+ *  profiles when we see a relative address ("c0[a0.x]", not the usual "c0").
+ *  In those cases, the shader was built to set some range of constant
+ *  registers as an array. You should set this array with (array_count)
+ *  elements from the constant register file, starting at (index) instead of
+ *  just a single uniform. To be extra difficult, you'll need to fill in the
+ *  correct values from the MOJOSHADER_constant data into the appropriate
+ *  parts of the array, overriding the constant register file. Fun!
+ * (constant) says whether this is a constant array; these need to be loaded
+ *  once at creation time, from the constant list and not ever updated from
+ *  the constant register file. This is a workaround for limitations in some
+ *  profiles.
+ * (name) is a profile-specific variable name; it may be NULL if it isn't
+ *  applicable to the requested profile.
+ */
+typedef struct MOJOSHADER_uniform
+{
+    MOJOSHADER_uniformType type;
+    int index;
+    int array_count;
+    int constant;
+    const char *name;
+} MOJOSHADER_uniform;
+
+/*
+ * These are the constants defined in a shader. These are data values
+ *  hardcoded in a shader (with the DEF, DEFI, DEFB instructions), which
+ *  override your Uniforms. This data is largely for informational purposes,
+ *  since they are compiled in and can't be changed, like Uniforms can be.
+ * These integers are register indexes. So if index==6 and
+ *  type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a 4-float vector
+ *  to be specified for what would be register "c6" in D3D assembly language,
+ *  before drawing with the shader.
+ * (value) is the value of the constant, unioned by type.
+ */
+typedef struct MOJOSHADER_constant
+{
+    MOJOSHADER_uniformType type;
+    int index;
+    union
+    {
+        float f[4];  /* if type==MOJOSHADER_UNIFORM_FLOAT */
+        int i[4];    /* if type==MOJOSHADER_UNIFORM_INT */
+        int b;       /* if type==MOJOSHADER_UNIFORM_BOOL */
+    } value;
+} MOJOSHADER_constant;
+
+/*
+ * Data types for samplers. See MOJOSHADER_sampler for more information.
+ */
+typedef enum
+{
+    MOJOSHADER_SAMPLER_UNKNOWN = -1, /* housekeeping value; never returned. */
+    MOJOSHADER_SAMPLER_2D,
+    MOJOSHADER_SAMPLER_CUBE,
+    MOJOSHADER_SAMPLER_VOLUME,
+} MOJOSHADER_samplerType;
+
+/*
+ * These are the samplers to be set for a shader. ...
+ *  IDirect3DDevice::SetTexture() would need this data, for example.
+ * These integers are the sampler "stage". So if index==6 and
+ *  type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
+ *  to be specified for what would be register "s6" in D3D assembly language,
+ *  before drawing with the shader.
+ * (name) is a profile-specific variable name; it may be NULL if it isn't
+ *  applicable to the requested profile.
+ * (texbem) will be non-zero if a TEXBEM opcode references this sampler. This
+ *  is only used in legacy shaders (ps_1_1 through ps_1_3), but it needs some
+ *  special support to work, as we have to load a magic uniform behind the
+ *  scenes to support it. Most code can ignore this field in general, and no
+ *  one has to touch it unless they really know what they're doing.
+ */
+typedef struct MOJOSHADER_sampler
+{
+    MOJOSHADER_samplerType type;
+    int index;
+    const char *name;
+    int texbem;
+} MOJOSHADER_sampler;
+
+
+/*
+ * This struct is used if you have to force a sampler to a specific type.
+ *  Generally, you can ignore this, but if you have, say, a ps_1_1
+ *  shader, you might need to specify what the samplers are meant to be
+ *  to get correct results, as Shader Model 1 samples textures according
+ *  to what is bound to a sampler at the moment instead of what the shader
+ *  is hardcoded to expect.
+ */
+typedef struct MOJOSHADER_samplerMap
+{
+    int index;
+    MOJOSHADER_samplerType type;
+} MOJOSHADER_samplerMap;
+
+/*
+ * Data types for attributes. See MOJOSHADER_attribute for more information.
+ */
+typedef enum
+{
+    MOJOSHADER_USAGE_UNKNOWN = -1,  /* housekeeping value; never returned. */
+    MOJOSHADER_USAGE_POSITION,
+    MOJOSHADER_USAGE_BLENDWEIGHT,
+    MOJOSHADER_USAGE_BLENDINDICES,
+    MOJOSHADER_USAGE_NORMAL,
+    MOJOSHADER_USAGE_POINTSIZE,
+    MOJOSHADER_USAGE_TEXCOORD,
+    MOJOSHADER_USAGE_TANGENT,
+    MOJOSHADER_USAGE_BINORMAL,
+    MOJOSHADER_USAGE_TESSFACTOR,
+    MOJOSHADER_USAGE_POSITIONT,
+    MOJOSHADER_USAGE_COLOR,
+    MOJOSHADER_USAGE_FOG,
+    MOJOSHADER_USAGE_DEPTH,
+    MOJOSHADER_USAGE_SAMPLE,
+    MOJOSHADER_USAGE_TOTAL,  /* housekeeping value; never returned. */
+} MOJOSHADER_usage;
+
+/*
+ * These are the attributes to be set for a shader. "Attributes" are what
+ *  Direct3D calls "Vertex Declarations Usages" ...
+ *  IDirect3DDevice::CreateVertexDeclaration() would need this data, for
+ *  example. Each attribute is associated with an array of data that uses one
+ *  element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
+ *  means we'd expect a secondary color array to be bound to this shader
+ *  before drawing.
+ * (name) is a profile-specific variable name; it may be NULL if it isn't
+ *  applicable to the requested profile.
+ */
+typedef struct MOJOSHADER_attribute
+{
+    MOJOSHADER_usage usage;
+    int index;
+    const char *name;
+} MOJOSHADER_attribute;
+
+/*
+ * Use this if you want to specify newly-parsed code to swizzle incoming
+ *  data. This can be useful if you know, at parse time, that a shader
+ *  will be processing data on COLOR0 that should be RGBA, but you'll
+ *  be passing it a vertex array full of ARGB instead.
+ */
+typedef struct MOJOSHADER_swizzle
+{
+    MOJOSHADER_usage usage;
+    unsigned int index;
+    unsigned char swizzles[4];  /* {0,1,2,3} == .xyzw, {2,2,2,2} == .zzzz */
+} MOJOSHADER_swizzle;
+
+
+/*
+ * MOJOSHADER_symbol data.
+ *
+ * These are used to expose high-level information in shader bytecode.
+ *  They associate HLSL variables with registers. This data is used for both
+ *  debugging and optimization.
+ */
+
+typedef enum
+{
+    MOJOSHADER_SYMREGSET_BOOL,
+    MOJOSHADER_SYMREGSET_INT4,
+    MOJOSHADER_SYMREGSET_FLOAT4,
+    MOJOSHADER_SYMREGSET_SAMPLER,
+} MOJOSHADER_symbolRegisterSet;
+
+typedef enum
+{
+    MOJOSHADER_SYMCLASS_SCALAR,
+    MOJOSHADER_SYMCLASS_VECTOR,
+    MOJOSHADER_SYMCLASS_MATRIX_ROWS,
+    MOJOSHADER_SYMCLASS_MATRIX_COLUMNS,
+    MOJOSHADER_SYMCLASS_OBJECT,
+    MOJOSHADER_SYMCLASS_STRUCT,
+} MOJOSHADER_symbolClass;
+
+typedef enum
+{
+    MOJOSHADER_SYMTYPE_VOID,
+    MOJOSHADER_SYMTYPE_BOOL,
+    MOJOSHADER_SYMTYPE_INT,
+    MOJOSHADER_SYMTYPE_FLOAT,
+    MOJOSHADER_SYMTYPE_STRING,
+    MOJOSHADER_SYMTYPE_TEXTURE,
+    MOJOSHADER_SYMTYPE_TEXTURE1D,
+    MOJOSHADER_SYMTYPE_TEXTURE2D,
+    MOJOSHADER_SYMTYPE_TEXTURE3D,
+    MOJOSHADER_SYMTYPE_TEXTURECUBE,
+    MOJOSHADER_SYMTYPE_SAMPLER,
+    MOJOSHADER_SYMTYPE_SAMPLER1D,
+    MOJOSHADER_SYMTYPE_SAMPLER2D,
+    MOJOSHADER_SYMTYPE_SAMPLER3D,
+    MOJOSHADER_SYMTYPE_SAMPLERCUBE,
+    MOJOSHADER_SYMTYPE_PIXELSHADER,
+    MOJOSHADER_SYMTYPE_VERTEXSHADER,
+    MOJOSHADER_SYMTYPE_PIXELFRAGMENT,
+    MOJOSHADER_SYMTYPE_VERTEXFRAGMENT,
+    MOJOSHADER_SYMTYPE_UNSUPPORTED,
+} MOJOSHADER_symbolType;
+
+typedef struct MOJOSHADER_symbolStructMember MOJOSHADER_symbolStructMember;
+
+typedef struct MOJOSHADER_symbolTypeInfo
+{
+    MOJOSHADER_symbolClass parameter_class;
+    MOJOSHADER_symbolType parameter_type;
+    unsigned int rows;
+    unsigned int columns;
+    unsigned int elements;
+    unsigned int member_count;
+    MOJOSHADER_symbolStructMember *members;
+} MOJOSHADER_symbolTypeInfo;
+
+struct MOJOSHADER_symbolStructMember
+{
+    const char *name;
+    MOJOSHADER_symbolTypeInfo info;
+};
+
+typedef struct MOJOSHADER_symbol
+{
+    const char *name;
+    MOJOSHADER_symbolRegisterSet register_set;
+    unsigned int register_index;
+    unsigned int register_count;
+    MOJOSHADER_symbolTypeInfo info;
+} MOJOSHADER_symbol;
+
+
+/*
+ * These are used with MOJOSHADER_error as special case positions.
+ */
+#define MOJOSHADER_POSITION_NONE (-3)
+#define MOJOSHADER_POSITION_BEFORE (-2)
+#define MOJOSHADER_POSITION_AFTER (-1)
+
+typedef struct MOJOSHADER_error
+{
+    /*
+     * Human-readable error, if there is one. Will be NULL if there was no
+     *  error. The string will be UTF-8 encoded, and English only. Most of
+     *  these shouldn't be shown to the end-user anyhow.
+     */
+    const char *error;
+
+    /*
+     * Filename where error happened. This can be NULL if the information
+     *  isn't available.
+     */
+    const char *filename;
+
+    /*
+     * Position of error, if there is one. Will be MOJOSHADER_POSITION_NONE if
+     *  there was no error, MOJOSHADER_POSITION_BEFORE if there was an error
+     *  before processing started, and MOJOSHADER_POSITION_AFTER if there was
+     *  an error during final processing. If >= 0, MOJOSHADER_parse() sets
+     *  this to the byte offset (starting at zero) into the bytecode you
+     *  supplied, and MOJOSHADER_assemble(), MOJOSHADER_parseAst(), and
+     *  MOJOSHADER_compile() sets this to a a line number in the source code
+     *  you supplied (starting at one).
+     */
+    int error_position;
+} MOJOSHADER_error;
+
+
+/* !!! FIXME: document me. */
+typedef enum MOJOSHADER_preshaderOpcode
+{
+    MOJOSHADER_PRESHADEROP_NOP,
+    MOJOSHADER_PRESHADEROP_MOV,
+    MOJOSHADER_PRESHADEROP_NEG,
+    MOJOSHADER_PRESHADEROP_RCP,
+    MOJOSHADER_PRESHADEROP_FRC,
+    MOJOSHADER_PRESHADEROP_EXP,
+    MOJOSHADER_PRESHADEROP_LOG,
+    MOJOSHADER_PRESHADEROP_RSQ,
+    MOJOSHADER_PRESHADEROP_SIN,
+    MOJOSHADER_PRESHADEROP_COS,
+    MOJOSHADER_PRESHADEROP_ASIN,
+    MOJOSHADER_PRESHADEROP_ACOS,
+    MOJOSHADER_PRESHADEROP_ATAN,
+    MOJOSHADER_PRESHADEROP_MIN,
+    MOJOSHADER_PRESHADEROP_MAX,
+    MOJOSHADER_PRESHADEROP_LT,
+    MOJOSHADER_PRESHADEROP_GE,
+    MOJOSHADER_PRESHADEROP_ADD,
+    MOJOSHADER_PRESHADEROP_MUL,
+    MOJOSHADER_PRESHADEROP_ATAN2,
+    MOJOSHADER_PRESHADEROP_DIV,
+    MOJOSHADER_PRESHADEROP_CMP,
+    MOJOSHADER_PRESHADEROP_MOVC,
+    MOJOSHADER_PRESHADEROP_DOT,
+    MOJOSHADER_PRESHADEROP_NOISE,
+    MOJOSHADER_PRESHADEROP_SCALAR_OPS,
+    MOJOSHADER_PRESHADEROP_MIN_SCALAR = MOJOSHADER_PRESHADEROP_SCALAR_OPS,
+    MOJOSHADER_PRESHADEROP_MAX_SCALAR,
+    MOJOSHADER_PRESHADEROP_LT_SCALAR,
+    MOJOSHADER_PRESHADEROP_GE_SCALAR,
+    MOJOSHADER_PRESHADEROP_ADD_SCALAR,
+    MOJOSHADER_PRESHADEROP_MUL_SCALAR,
+    MOJOSHADER_PRESHADEROP_ATAN2_SCALAR,
+    MOJOSHADER_PRESHADEROP_DIV_SCALAR,
+    MOJOSHADER_PRESHADEROP_DOT_SCALAR,
+    MOJOSHADER_PRESHADEROP_NOISE_SCALAR,
+} MOJOSHADER_preshaderOpcode;
+
+typedef enum MOJOSHADER_preshaderOperandType
+{
+    MOJOSHADER_PRESHADEROPERAND_INPUT,
+    MOJOSHADER_PRESHADEROPERAND_OUTPUT,
+    MOJOSHADER_PRESHADEROPERAND_LITERAL,
+    MOJOSHADER_PRESHADEROPERAND_TEMP,
+} MOJOSHADER_preshaderOperandType;
+
+typedef struct MOJOSHADER_preshaderOperand
+{
+    MOJOSHADER_preshaderOperandType type;
+    unsigned int index;
+} MOJOSHADER_preshaderOperand;
+
+typedef struct MOJOSHADER_preshaderInstruction
+{
+    MOJOSHADER_preshaderOpcode opcode;
+    unsigned int element_count;
+    unsigned int operand_count;
+    MOJOSHADER_preshaderOperand operands[3];
+} MOJOSHADER_preshaderInstruction;
+
+typedef struct MOJOSHADER_preshader
+{
+    unsigned int literal_count;
+    double *literals;
+    unsigned int temp_count;  /* scalar, not vector! */
+    unsigned int symbol_count;
+    MOJOSHADER_symbol *symbols;
+    unsigned int instruction_count;
+    MOJOSHADER_preshaderInstruction *instructions;
+} MOJOSHADER_preshader;
+
+/*
+ * Structure used to return data from parsing of a shader...
+ */
+/* !!! FIXME: most of these ints should be unsigned. */
+typedef struct MOJOSHADER_parseData
+{
+    /*
+     * The number of elements pointed to by (errors).
+     */
+    int error_count;
+
+    /*
+     * (error_count) elements of data that specify errors that were generated
+     *  by parsing this shader.
+     * This can be NULL if there were no errors or if (error_count) is zero.
+     */
+    MOJOSHADER_error *errors;
+
+    /*
+     * The name of the profile used to parse the shader. Will be NULL on error.
+     */
+    const char *profile;
+
+    /*
+     * Bytes of output from parsing. Most profiles produce a string of source
+     *  code, but profiles that do binary output may not be text at all.
+     *  Will be NULL on error.
+     */
+    const char *output;
+
+    /*
+     * Byte count for output, not counting any null terminator. Most profiles
+     *  produce an ASCII string of source code (which will be null-terminated
+     *  even though that null char isn't included in output_len), but profiles
+     *  that do binary output may not be text at all. Will be 0 on error.
+     */
+    int output_len;
+
+    /*
+     * Count of Direct3D instruction slots used. This is meaningless in terms
+     *  of the actual output, as the profile will probably grow or reduce
+     *  the count (or for high-level languages, not have that information at
+     *  all). Also, as with Microsoft's own assembler, this value is just a
+     *  rough estimate, as unpredicable real-world factors make the actual
+     *  value vary at least a little from this count. Still, it can give you
+     *  a rough idea of the size of your shader. Will be zero on error.
+     */
+    int instruction_count;
+
+    /*
+     * The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error.
+     */
+    MOJOSHADER_shaderType shader_type;
+
+    /*
+     * The shader's major version. If this was a "vs_3_0", this would be 3.
+     */
+    int major_ver;
+
+    /*
+     * The shader's minor version. If this was a "ps_1_4", this would be 4.
+     *  Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255.
+     */
+    int minor_ver;
+
+    /*
+     * The number of elements pointed to by (uniforms).
+     */
+    int uniform_count;
+
+    /*
+     * (uniform_count) elements of data that specify Uniforms to be set for
+     *  this shader. See discussion on MOJOSHADER_uniform for details.
+     * This can be NULL on error or if (uniform_count) is zero.
+     */
+    MOJOSHADER_uniform *uniforms;
+
+    /*
+     * The number of elements pointed to by (constants).
+     */
+    int constant_count;
+
+    /*
+     * (constant_count) elements of data that specify constants used in
+     *  this shader. See discussion on MOJOSHADER_constant for details.
+     * This can be NULL on error or if (constant_count) is zero.
+     *  This is largely informational: constants are hardcoded into a shader.
+     *  The constants that you can set like parameters are in the "uniforms"
+     *  list.
+     */
+    MOJOSHADER_constant *constants;
+
+    /*
+     * The number of elements pointed to by (samplers).
+     */
+    int sampler_count;
+
+    /*
+     * (sampler_count) elements of data that specify Samplers to be set for
+     *  this shader. See discussion on MOJOSHADER_sampler for details.
+     * This can be NULL on error or if (sampler_count) is zero.
+     */
+    MOJOSHADER_sampler *samplers;
+
+    /* !!! FIXME: this should probably be "input" and not "attribute" */
+    /*
+     * The number of elements pointed to by (attributes).
+     */
+    int attribute_count;
+
+    /* !!! FIXME: this should probably be "input" and not "attribute" */
+    /*
+     * (attribute_count) elements of data that specify Attributes to be set
+     *  for this shader. See discussion on MOJOSHADER_attribute for details.
+     * This can be NULL on error or if (attribute_count) is zero.
+     */
+    MOJOSHADER_attribute *attributes;
+
+    /*
+     * The number of elements pointed to by (outputs).
+     */
+    int output_count;
+
+    /*
+     * (output_count) elements of data that specify outputs this shader
+     *  writes to. See discussion on MOJOSHADER_attribute for details.
+     * This can be NULL on error or if (output_count) is zero.
+     */
+    MOJOSHADER_attribute *outputs;
+
+    /*
+     * The number of elements pointed to by (swizzles).
+     */
+    int swizzle_count;
+
+    /* !!! FIXME: this should probably be "input" and not "attribute" */
+    /*
+     * (swizzle_count) elements of data that specify swizzles the shader will
+     *  apply to incoming attributes. This is a copy of what was passed to
+     *  MOJOSHADER_parseData().
+     * This can be NULL on error or if (swizzle_count) is zero.
+     */
+    MOJOSHADER_swizzle *swizzles;
+
+    /*
+     * The number of elements pointed to by (symbols).
+     */
+    int symbol_count;
+
+    /*
+     * (symbol_count) elements of data that specify high-level symbol data
+     *  for the shader. This will be parsed from the CTAB section
+     *  in bytecode, and will be a copy of what you provide to
+     *  MOJOSHADER_assemble(). This data is optional.
+     * This can be NULL on error or if (symbol_count) is zero.
+     */
+    MOJOSHADER_symbol *symbols;
+
+    /*
+     * !!! FIXME: document me.
+     * This can be NULL on error or if no preshader was available.
+     */
+    MOJOSHADER_preshader *preshader;
+
+    /*
+     * This is the malloc implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_malloc malloc;
+
+    /*
+     * This is the free implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_free free;
+
+    /*
+     * This is the pointer you passed as opaque data for your allocator.
+     */
+    void *malloc_data;
+} MOJOSHADER_parseData;
+
+
+/*
+ * Profile string for Direct3D assembly language output.
+ */
+#define MOJOSHADER_PROFILE_D3D "d3d"
+
+/*
+ * Profile string for passthrough of the original bytecode, unchanged.
+ */
+#define MOJOSHADER_PROFILE_BYTECODE "bytecode"
+
+/*
+ * Profile string for GLSL: OpenGL high-level shader language output.
+ */
+#define MOJOSHADER_PROFILE_GLSL "glsl"
+
+/*
+ * Profile string for GLSL 1.20: minor improvements to base GLSL spec.
+ */
+#define MOJOSHADER_PROFILE_GLSL120 "glsl120"
+
+/*
+ * Profile string for OpenGL ARB 1.0 shaders: GL_ARB_(vertex|fragment)_program.
+ */
+#define MOJOSHADER_PROFILE_ARB1 "arb1"
+
+/*
+ * Profile string for OpenGL ARB 1.0 shaders with Nvidia 2.0 extensions:
+ *  GL_NV_vertex_program2_option and GL_NV_fragment_program2
+ */
+#define MOJOSHADER_PROFILE_NV2 "nv2"
+
+/*
+ * Profile string for OpenGL ARB 1.0 shaders with Nvidia 3.0 extensions:
+ *  GL_NV_vertex_program3 and GL_NV_fragment_program2
+ */
+#define MOJOSHADER_PROFILE_NV3 "nv3"
+
+/*
+ * Profile string for OpenGL ARB 1.0 shaders with Nvidia 4.0 extensions:
+ *  GL_NV_gpu_program4
+ */
+#define MOJOSHADER_PROFILE_NV4 "nv4"
+
+/*
+ * Determine the highest supported Shader Model for a profile.
+ */
+int MOJOSHADER_maxShaderModel(const char *profile);
+
+
+/*
+ * Parse a compiled Direct3D shader's bytecode.
+ *
+ * This is your primary entry point into MojoShader. You need to pass it
+ *  a compiled D3D shader and tell it which "profile" you want to use to
+ *  convert it into useful data.
+ *
+ * The available profiles are the set of MOJOSHADER_PROFILE_* defines.
+ *  Note that MojoShader may be built without support for all listed
+ *  profiles (in which case using one here will return with an error).
+ *
+ * As parsing requires some memory to be allocated, you may provide a custom
+ *  allocator to this function, which will be used to allocate/free memory.
+ *  They function just like malloc() and free(). We do not use realloc().
+ *  If you don't care, pass NULL in for the allocator functions. If your
+ *  allocator needs instance-specific data, you may supply it with the
+ *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
+ *
+ * This function returns a MOJOSHADER_parseData.
+ *
+ * This function will never return NULL, even if the system is completely
+ *  out of memory upon entry (in which case, this function returns a static
+ *  MOJOSHADER_parseData object, which is still safe to pass to
+ *  MOJOSHADER_freeParseData()).
+ *
+ * You can tell the generated program to swizzle certain inputs. If you know
+ *  that COLOR0 should be RGBA but you're passing in ARGB, you can specify
+ *  a swizzle of { MOJOSHADER_USAGE_COLOR, 0, {1,2,3,0} } to (swiz). If the
+ *  input register in the code would produce reg.ywzx, that swizzle would
+ *  change it to reg.wzxy ... (swiz) can be NULL.
+ *
+ * You can force the shader to expect samplers of certain types. Generally
+ *  you don't need this, as Shader Model 2 and later always specify what they
+ *  expect samplers to be (2D, cubemap, etc). Shader Model 1, however, just
+ *  uses whatever is bound to a given sampler at draw time, but this doesn't
+ *  work in OpenGL, etc. In these cases, MojoShader will default to
+ *  2D texture sampling (or cubemap sampling, in cases where it makes sense,
+ *  like the TEXM3X3TEX opcode), which works 75% of the time, but if you
+ *  really needed something else, you'll need to specify it here. This can
+ *  also be used, at your own risk, to override DCL opcodes in shaders: if
+ *  the shader explicit says 2D, but you want Cubemap, for example, you can
+ *  use this to override. If you aren't sure about any of this stuff, you can
+ *  (and should) almost certainly ignore it: (smap) can be NULL.
+ *
+ * This function is thread safe, so long as (m) and (f) are too, and that
+ *  (tokenbuf) remains intact for the duration of the call. This allows you
+ *  to parse several shaders on separate CPU cores at the same time.
+ */
+const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
+                                             const unsigned char *tokenbuf,
+                                             const unsigned int bufsize,
+                                             const MOJOSHADER_swizzle *swiz,
+                                             const unsigned int swizcount,
+                                             const MOJOSHADER_samplerMap *smap,
+                                             const unsigned int smapcount,
+                                             MOJOSHADER_malloc m,
+                                             MOJOSHADER_free f,
+                                             void *d);
+
+/*
+ * Call this to dispose of parsing results when you are done with them.
+ *  This will call the MOJOSHADER_free function you provided to
+ *  MOJOSHADER_parse multiple times, if you provided one.
+ *  Passing a NULL here is a safe no-op.
+ *
+ * This function is thread safe, so long as any allocator you passed into
+ *  MOJOSHADER_parse() is, too.
+ */
+void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);
+
+
+/* Effects interface... */  /* !!! FIXME: THIS API IS NOT STABLE YET! */
+
+typedef struct MOJOSHADER_effectParam
+{
+    const char *name;
+    const char *semantic;
+} MOJOSHADER_effectParam;
+
+typedef struct MOJOSHADER_effectState
+{
+    unsigned int type;
+} MOJOSHADER_effectState;
+
+typedef struct MOJOSHADER_effectPass
+{
+    const char *name;
+    unsigned int state_count;
+    MOJOSHADER_effectState *states;
+} MOJOSHADER_effectPass;
+
+typedef struct MOJOSHADER_effectTechnique
+{
+    const char *name;
+    unsigned int pass_count;
+    MOJOSHADER_effectPass *passes;
+} MOJOSHADER_effectTechnique;
+
+typedef struct MOJOSHADER_effectTexture
+{
+    unsigned int param;
+    const char *name;
+} MOJOSHADER_effectTexture;
+
+typedef struct MOJOSHADER_effectShader
+{
+    unsigned int technique;
+    unsigned int pass;
+    const MOJOSHADER_parseData *shader;
+} MOJOSHADER_effectShader;
+
+/*
+ * Structure used to return data from parsing of an effect file...
+ */
+/* !!! FIXME: most of these ints should be unsigned. */
+typedef struct MOJOSHADER_effect
+{
+    /*
+     * The number of elements pointed to by (errors).
+     */
+    int error_count;
+
+    /*
+     * (error_count) elements of data that specify errors that were generated
+     *  by parsing this shader.
+     * This can be NULL if there were no errors or if (error_count) is zero.
+     */
+    MOJOSHADER_error *errors;
+
+    /*
+     * The name of the profile used to parse the shader. Will be NULL on error.
+     */
+    const char *profile;
+
+    /*
+     * The number of params pointed to by (params).
+     */
+    int param_count;
+
+    /*
+     * (param_count) elements of data that specify parameter bind points for
+     *  this effect.
+     * This can be NULL on error or if (param_count) is zero.
+     */
+    MOJOSHADER_effectParam *params;
+
+    /*
+     * The number of elements pointed to by (techniques).
+     */
+    int technique_count;
+
+    /*
+     * (technique_count) elements of data that specify techniques used in
+     *  this effect. Each technique contains a series of passes, and each pass
+     *  specifies state and shaders that affect rendering.
+     * This can be NULL on error or if (technique_count) is zero.
+     */
+    MOJOSHADER_effectTechnique *techniques;
+
+    /*
+     * The number of elements pointed to by (textures).
+     */
+    int texture_count;
+
+    /*
+     * (texture_count) elements of data that specify textures used in
+     *  this effect.
+     * This can be NULL on error or if (texture_count) is zero.
+     */
+    MOJOSHADER_effectTexture *textures;
+
+    /*
+     * The number of elements pointed to by (shaders).
+     */
+    int shader_count;
+
+    /*
+     * (shader_count) elements of data that specify shaders used in
+     *  this effect.
+     * This can be NULL on error or if (shader_count) is zero.
+     */
+    MOJOSHADER_effectShader *shaders;
+
+    /*
+     * This is the malloc implementation you passed to MOJOSHADER_parseEffect().
+     */
+    MOJOSHADER_malloc malloc;
+
+    /*
+     * This is the free implementation you passed to MOJOSHADER_parseEffect().
+     */
+    MOJOSHADER_free free;
+
+    /*
+     * This is the pointer you passed as opaque data for your allocator.
+     */
+    void *malloc_data;
+} MOJOSHADER_effect;
+
+/* !!! FIXME: document me. */
+const MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
+                                                const unsigned char *buf,
+                                                const unsigned int _len,
+                                                const MOJOSHADER_swizzle *swiz,
+                                                const unsigned int swizcount,
+                                                const MOJOSHADER_samplerMap *smap,
+                                                const unsigned int smapcount,
+                                                MOJOSHADER_malloc m,
+                                                MOJOSHADER_free f,
+                                                void *d);
+
+
+/* !!! FIXME: document me. */
+void MOJOSHADER_freeEffect(const MOJOSHADER_effect *effect);
+
+
+/* Preprocessor interface... */
+
+/*
+ * Structure used to pass predefined macros. Maps to D3DXMACRO.
+ *  You can have macro arguments: set identifier to "a(b, c)" or whatever.
+ */
+typedef struct MOJOSHADER_preprocessorDefine
+{
+    const char *identifier;
+    const char *definition;
+} MOJOSHADER_preprocessorDefine;
+
+/*
+ * Used with the MOJOSHADER_includeOpen callback. Maps to D3DXINCLUDE_TYPE.
+ */
+typedef enum
+{
+    MOJOSHADER_INCLUDETYPE_LOCAL,   /* local header: #include "blah.h" */
+    MOJOSHADER_INCLUDETYPE_SYSTEM   /* system header: #include <blah.h> */
+} MOJOSHADER_includeType;
+
+
+/*
+ * Structure used to return data from preprocessing of a shader...
+ */
+/* !!! FIXME: most of these ints should be unsigned. */
+typedef struct MOJOSHADER_preprocessData
+{
+    /*
+     * The number of elements pointed to by (errors).
+     */
+    int error_count;
+
+    /*
+     * (error_count) elements of data that specify errors that were generated
+     *  by parsing this shader.
+     * This can be NULL if there were no errors or if (error_count) is zero.
+     */
+    MOJOSHADER_error *errors;
+
+    /*
+     * Bytes of output from preprocessing. This is a UTF-8 string. We
+     *  guarantee it to be NULL-terminated. Will be NULL on error.
+     */
+    const char *output;
+
+    /*
+     * Byte count for output, not counting any null terminator.
+     *  Will be 0 on error.
+     */
+    int output_len;
+
+    /*
+     * This is the malloc implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_malloc malloc;
+
+    /*
+     * This is the free implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_free free;
+
+    /*
+     * This is the pointer you passed as opaque data for your allocator.
+     */
+    void *malloc_data;
+} MOJOSHADER_preprocessData;
+
+
+/*
+ * This callback allows an app to handle #include statements for the
+ *  preprocessor. When the preprocessor sees an #include, it will call this
+ *  function to obtain the contents of the requested file. This is optional;
+ *  the preprocessor will open files directly if no callback is supplied, but
+ *  this allows an app to retrieve data from something other than the
+ *  traditional filesystem (for example, headers packed in a .zip file or
+ *  headers generated on-the-fly).
+ *
+ * This function maps to ID3DXInclude::Open()
+ *
+ * (inctype) specifies the type of header we wish to include.
+ * (fname) specifies the name of the file specified on the #include line.
+ * (parent) is a string of the entire source file containing the include, in
+ *  its original, not-yet-preprocessed state. Note that this is just the
+ *  contents of the specific file, not all source code that the preprocessor
+ *  has seen through other includes, etc.
+ * (outdata) will be set by the callback to a pointer to the included file's
+ *  contents. The callback is responsible for allocating this however they
+ *  see fit (we provide allocator functions, but you may ignore them). This
+ *  pointer must remain valid until the includeClose callback runs. This
+ *  string does not need to be NULL-terminated.
+ * (outbytes) will be set by the callback to the number of bytes pointed to
+ *  by (outdata).
+ * (m),(f), and (d) are the allocator details that the application passed to
+ *  MojoShader. If these were NULL, MojoShader may have replaced them with its
+ *  own internal allocators.
+ *
+ * The callback returns zero on error, non-zero on success.
+ *
+ * If you supply an includeOpen callback, you must supply includeClose, too.
+ */
+typedef int (*MOJOSHADER_includeOpen)(MOJOSHADER_includeType inctype,
+                            const char *fname, const char *parent,
+                            const char **outdata, unsigned int *outbytes,
+                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+
+/*
+ * This callback allows an app to clean up the results of a previous
+ *  includeOpen callback.
+ *
+ * This function maps to ID3DXInclude::Close()
+ *
+ * (data) is the data that was returned from a previous call to includeOpen.
+ *  It is now safe to deallocate this data.
+ * (m),(f), and (d) are the same allocator details that were passed to your
+ *  includeOpen callback.
+ *
+ * If you supply an includeClose callback, you must supply includeOpen, too.
+ */
+typedef void (*MOJOSHADER_includeClose)(const char *data,
+                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+
+
+/*
+ * This function is optional. Even if you are dealing with shader source
+ *  code, you don't need to explicitly use the preprocessor, as the compiler
+ *  and assembler will use it behind the scenes. In fact, you probably never
+ *  need this function unless you are debugging a custom tool (or debugging
+ *  MojoShader itself).
+ *
+ * Preprocessing roughly follows the syntax of an ANSI C preprocessor, as
+ *  Microsoft's Direct3D assembler and HLSL compiler use this syntax. Please
+ *  note that we try to match the output you'd get from Direct3D's
+ *  preprocessor, which has some quirks if you're expecting output that matches
+ *  a generic C preprocessor.
+ *
+ * This function maps to D3DXPreprocessShader().
+ *
+ * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
+ *  actually access this file, as we obtain our data from (source). This
+ *  string is copied when we need to report errors while processing (source),
+ *  as opposed to errors in a file referenced via the #include directive in
+ *  (source). If this is NULL, then errors will report the filename as NULL,
+ *  too.
+ *
+ * (source) is an string of UTF-8 text to preprocess. It does not need to be
+ *  NULL-terminated.
+ *
+ * (sourcelen) is the length of the string pointed to by (source), in bytes.
+ *
+ * (defines) points to (define_count) preprocessor definitions, and can be
+ *  NULL. These are treated by the preprocessor as if the source code started
+ *  with one #define for each entry you pass in here.
+ *
+ * (include_open) and (include_close) let the app control the preprocessor's
+ *  behaviour for #include statements. Both are optional and can be NULL, but
+ *  both must be specified if either is specified.
+ *
+ * This will return a MOJOSHADER_preprocessorData. You should pass this
+ *  return value to MOJOSHADER_freePreprocessData() when you are done with
+ *  it.
+ *
+ * This function will never return NULL, even if the system is completely
+ *  out of memory upon entry (in which case, this function returns a static
+ *  MOJOSHADER_preprocessData object, which is still safe to pass to
+ *  MOJOSHADER_freePreprocessData()).
+ *
+ * As preprocessing requires some memory to be allocated, you may provide a
+ *  custom allocator to this function, which will be used to allocate/free
+ *  memory. They function just like malloc() and free(). We do not use
+ *  realloc(). If you don't care, pass NULL in for the allocator functions.
+ *  If your allocator needs instance-specific data, you may supply it with the
+ *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
+ *
+ * This function is thread safe, so long as the various callback functions
+ *  are, too, and that the parameters remains intact for the duration of the
+ *  call. This allows you to preprocess several shaders on separate CPU cores
+ *  at the same time.
+ */
+const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
+                             const char *source, unsigned int sourcelen,
+                             const MOJOSHADER_preprocessorDefine *defines,
+                             unsigned int define_count,
+                             MOJOSHADER_includeOpen include_open,
+                             MOJOSHADER_includeClose include_close,
+                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+
+
+/*
+ * Call this to dispose of preprocessing results when you are done with them.
+ *  This will call the MOJOSHADER_free function you provided to
+ *  MOJOSHADER_preprocess() multiple times, if you provided one.
+ *  Passing a NULL here is a safe no-op.
+ *
+ * This function is thread safe, so long as any allocator you passed into
+ *  MOJOSHADER_preprocess() is, too.
+ */
+void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
+
+
+/* Assembler interface... */
+
+/*
+ * This function is optional. Use this to convert Direct3D shader assembly
+ *  language into bytecode, which can be handled by MOJOSHADER_parse().
+ *
+ * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
+ *  actually access this file, as we obtain our data from (source). This
+ *  string is copied when we need to report errors while processing (source),
+ *  as opposed to errors in a file referenced via the #include directive in
+ *  (source). If this is NULL, then errors will report the filename as NULL,
+ *  too.
+ *
+ * (source) is an UTF-8 string of valid Direct3D shader assembly source code.
+ *  It does not need to be NULL-terminated.
+ *
+ * (sourcelen) is the length of the string pointed to by (source), in bytes.
+ *
+ * (comments) points to (comment_count) NULL-terminated UTF-8 strings, and
+ *  can be NULL. These strings are inserted as comments in the bytecode.
+ *
+ * (symbols) points to (symbol_count) symbol structs, and can be NULL. These
+ *  become a CTAB field in the bytecode. This is optional, but
+ *  MOJOSHADER_parse() needs CTAB data for all arrays used in a program, or
+ *  relative addressing will not be permitted, so you'll want to at least
+ *  provide symbol information for those. The symbol data is 100% trusted
+ *  at this time; it will not be checked to see if it matches what was
+ *  assembled in any way whatsoever.
+ *
+ * (defines) points to (define_count) preprocessor definitions, and can be
+ *  NULL. These are treated by the preprocessor as if the source code started
+ *  with one #define for each entry you pass in here.
+ *
+ * (include_open) and (include_close) let the app control the preprocessor's
+ *  behaviour for #include statements. Both are optional and can be NULL, but
+ *  both must be specified if either is specified.
+ *
+ * This will return a MOJOSHADER_parseData, like MOJOSHADER_parse() would,
+ *  except the profile will be MOJOSHADER_PROFILE_BYTECODE and the output
+ *  will be the assembled bytecode instead of some other language. This output
+ *  can be pushed back through MOJOSHADER_parseData() with a different profile.
+ *
+ * This function will never return NULL, even if the system is completely
+ *  out of memory upon entry (in which case, this function returns a static
+ *  MOJOSHADER_parseData object, which is still safe to pass to
+ *  MOJOSHADER_freeParseData()).
+ *
+ * As assembling requires some memory to be allocated, you may provide a
+ *  custom allocator to this function, which will be used to allocate/free
+ *  memory. They function just like malloc() and free(). We do not use
+ *  realloc(). If you don't care, pass NULL in for the allocator functions.
+ *  If your allocator needs instance-specific data, you may supply it with the
+ *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
+ *
+ * This function is thread safe, so long as the various callback functions
+ *  are, too, and that the parameters remains intact for the duration of the
+ *  call. This allows you to assemble several shaders on separate CPU cores
+ *  at the same time.
+ */
+const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
+                             const char *source, unsigned int sourcelen,
+                             const char **comments, unsigned int comment_count,
+                             const MOJOSHADER_symbol *symbols,
+                             unsigned int symbol_count,
+                             const MOJOSHADER_preprocessorDefine *defines,
+                             unsigned int define_count,
+                             MOJOSHADER_includeOpen include_open,
+                             MOJOSHADER_includeClose include_close,
+                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+
+
+/* High level shading language support... */
+
+/*
+ * Source profile strings for HLSL: Direct3D High Level Shading Language.
+ */
+#define MOJOSHADER_SRC_PROFILE_HLSL_VS_1_1 "hlsl_vs_1_1"
+#define MOJOSHADER_SRC_PROFILE_HLSL_VS_2_0 "hlsl_vs_2_0"
+#define MOJOSHADER_SRC_PROFILE_HLSL_VS_3_0 "hlsl_vs_3_0"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1 "hlsl_ps_1_1"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_2 "hlsl_ps_1_2"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_3 "hlsl_ps_1_3"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_4 "hlsl_ps_1_4"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_2_0 "hlsl_ps_2_0"
+#define MOJOSHADER_SRC_PROFILE_HLSL_PS_3_0 "hlsl_ps_3_0"
+
+
+/* Abstract Syntax Tree interface... */
+
+/*
+ * ATTENTION: This adds a lot of stuff to the API, but almost everyone can
+ *  ignore this section. Seriously, go ahead and skip over anything that has
+ *  "AST" in it, unless you know why you'd want to use it.
+ *
+ * ALSO: This API is still evolving! We make no promises at this time to keep
+ *  source or binary compatibility for the AST pieces.
+ *
+ * Important notes:
+ *  - ASTs are the result of parsing the source code: a program that fails to
+ *    compile will often parse successfully. Undeclared variables,
+ *    type incompatibilities, etc, aren't detected at this point.
+ *  - Vector swizzles (the ".xyzw" part of "MyVec4.xyzw") will look like
+ *    structure dereferences. We don't realize these are actually swizzles
+ *    until semantic analysis.
+ *  - MOJOSHADER_astDataType info is not reliable when returned from
+ *    MOJOSHADER_parseAst()! Most of the datatype info will be missing or have
+ *    inaccurate data types. We sort these out during semantic analysis, which
+ *    happens after the AST parsing is complete. A few are filled in, or can
+ *    be deduced fairly trivially by processing several pieces into one.
+ *    It's enough that you can reproduce the original source code, more or
+ *    less, from the AST.
+ */
+
+/* High-level datatypes for AST nodes. */
+typedef enum MOJOSHADER_astDataTypeType
+{
+    MOJOSHADER_AST_DATATYPE_NONE,
+    MOJOSHADER_AST_DATATYPE_BOOL,
+    MOJOSHADER_AST_DATATYPE_INT,
+    MOJOSHADER_AST_DATATYPE_UINT,
+    MOJOSHADER_AST_DATATYPE_FLOAT,
+    MOJOSHADER_AST_DATATYPE_FLOAT_SNORM,
+    MOJOSHADER_AST_DATATYPE_FLOAT_UNORM,
+    MOJOSHADER_AST_DATATYPE_HALF,
+    MOJOSHADER_AST_DATATYPE_DOUBLE,
+    MOJOSHADER_AST_DATATYPE_STRING,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_1D,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_2D,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_3D,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_STATE,
+    MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE,
+    MOJOSHADER_AST_DATATYPE_STRUCT,
+    MOJOSHADER_AST_DATATYPE_ARRAY,
+    MOJOSHADER_AST_DATATYPE_VECTOR,
+    MOJOSHADER_AST_DATATYPE_MATRIX,
+    MOJOSHADER_AST_DATATYPE_BUFFER,
+    MOJOSHADER_AST_DATATYPE_FUNCTION,
+    MOJOSHADER_AST_DATATYPE_USER,
+} MOJOSHADER_astDataTypeType;
+#define MOJOSHADER_AST_DATATYPE_CONST (1 << 31)
+
+typedef union MOJOSHADER_astDataType MOJOSHADER_astDataType;
+
+// This is just part of DataTypeStruct, never appears outside of it.
+typedef struct MOJOSHADER_astDataTypeStructMember
+{
+    const MOJOSHADER_astDataType *datatype;
+    const char *identifier;
+} MOJOSHADER_astDataTypeStructMember;
+
+typedef struct MOJOSHADER_astDataTypeStruct
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataTypeStructMember *members;
+    int member_count;
+} MOJOSHADER_astDataTypeStruct;
+
+typedef struct MOJOSHADER_astDataTypeArray
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataType *base;
+    int elements;
+} MOJOSHADER_astDataTypeArray;
+
+typedef MOJOSHADER_astDataTypeArray MOJOSHADER_astDataTypeVector;
+
+typedef struct MOJOSHADER_astDataTypeMatrix
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataType *base;
+    int rows;
+    int columns;
+} MOJOSHADER_astDataTypeMatrix;
+
+typedef struct MOJOSHADER_astDataTypeBuffer
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataType *base;
+} MOJOSHADER_astDataTypeBuffer;
+
+typedef struct MOJOSHADER_astDataTypeFunction
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataType *retval;
+    const MOJOSHADER_astDataType **params;
+    int num_params;
+    int intrinsic;  /* non-zero for built-in functions */
+} MOJOSHADER_astDataTypeFunction;
+
+typedef struct MOJOSHADER_astDataTypeUser
+{
+    MOJOSHADER_astDataTypeType type;
+    const MOJOSHADER_astDataType *details;
+    const char *name;
+} MOJOSHADER_astDataTypeUser;
+
+union MOJOSHADER_astDataType
+{
+    MOJOSHADER_astDataTypeType type;
+    MOJOSHADER_astDataTypeArray array;
+    MOJOSHADER_astDataTypeStruct structure;
+    MOJOSHADER_astDataTypeVector vector;
+    MOJOSHADER_astDataTypeMatrix matrix;
+    MOJOSHADER_astDataTypeBuffer buffer;
+    MOJOSHADER_astDataTypeUser user;
+    MOJOSHADER_astDataTypeFunction function;
+};
+
+/* Structures that make up the parse tree... */
+
+typedef enum MOJOSHADER_astNodeType
+{
+    MOJOSHADER_AST_OP_START_RANGE,         /* expression operators. */
+
+    MOJOSHADER_AST_OP_START_RANGE_UNARY,   /* unary operators. */
+    MOJOSHADER_AST_OP_PREINCREMENT,
+    MOJOSHADER_AST_OP_PREDECREMENT,
+    MOJOSHADER_AST_OP_NEGATE,
+    MOJOSHADER_AST_OP_COMPLEMENT,
+    MOJOSHADER_AST_OP_NOT,
+    MOJOSHADER_AST_OP_POSTINCREMENT,
+    MOJOSHADER_AST_OP_POSTDECREMENT,
+    MOJOSHADER_AST_OP_CAST,
+    MOJOSHADER_AST_OP_END_RANGE_UNARY,
+
+    MOJOSHADER_AST_OP_START_RANGE_BINARY,  /* binary operators. */
+    MOJOSHADER_AST_OP_COMMA,
+    MOJOSHADER_AST_OP_MULTIPLY,
+    MOJOSHADER_AST_OP_DIVIDE,
+    MOJOSHADER_AST_OP_MODULO,
+    MOJOSHADER_AST_OP_ADD,
+    MOJOSHADER_AST_OP_SUBTRACT,
+    MOJOSHADER_AST_OP_LSHIFT,
+    MOJOSHADER_AST_OP_RSHIFT,
+    MOJOSHADER_AST_OP_LESSTHAN,
+    MOJOSHADER_AST_OP_GREATERTHAN,
+    MOJOSHADER_AST_OP_LESSTHANOREQUAL,
+    MOJOSHADER_AST_OP_GREATERTHANOREQUAL,
+    MOJOSHADER_AST_OP_EQUAL,
+    MOJOSHADER_AST_OP_NOTEQUAL,
+    MOJOSHADER_AST_OP_BINARYAND,
+    MOJOSHADER_AST_OP_BINARYXOR,
+    MOJOSHADER_AST_OP_BINARYOR,
+    MOJOSHADER_AST_OP_LOGICALAND,
+    MOJOSHADER_AST_OP_LOGICALOR,
+    MOJOSHADER_AST_OP_ASSIGN,
+    MOJOSHADER_AST_OP_MULASSIGN,
+    MOJOSHADER_AST_OP_DIVASSIGN,
+    MOJOSHADER_AST_OP_MODASSIGN,
+    MOJOSHADER_AST_OP_ADDASSIGN,
+    MOJOSHADER_AST_OP_SUBASSIGN,
+    MOJOSHADER_AST_OP_LSHIFTASSIGN,
+    MOJOSHADER_AST_OP_RSHIFTASSIGN,
+    MOJOSHADER_AST_OP_ANDASSIGN,
+    MOJOSHADER_AST_OP_XORASSIGN,
+    MOJOSHADER_AST_OP_ORASSIGN,
+    MOJOSHADER_AST_OP_DEREF_ARRAY,
+    MOJOSHADER_AST_OP_END_RANGE_BINARY,
+
+    MOJOSHADER_AST_OP_START_RANGE_TERNARY,  /* ternary operators. */
+    MOJOSHADER_AST_OP_CONDITIONAL,
+    MOJOSHADER_AST_OP_END_RANGE_TERNARY,
+
+    MOJOSHADER_AST_OP_START_RANGE_DATA,     /* expression operands. */
+    MOJOSHADER_AST_OP_IDENTIFIER,
+    MOJOSHADER_AST_OP_INT_LITERAL,
+    MOJOSHADER_AST_OP_FLOAT_LITERAL,
+    MOJOSHADER_AST_OP_STRING_LITERAL,
+    MOJOSHADER_AST_OP_BOOLEAN_LITERAL,
+    MOJOSHADER_AST_OP_END_RANGE_DATA,
+
+    MOJOSHADER_AST_OP_START_RANGE_MISC,     /* other expression things. */
+    MOJOSHADER_AST_OP_DEREF_STRUCT,
+    MOJOSHADER_AST_OP_CALLFUNC,
+    MOJOSHADER_AST_OP_CONSTRUCTOR,
+    MOJOSHADER_AST_OP_END_RANGE_MISC,
+    MOJOSHADER_AST_OP_END_RANGE,
+
+    MOJOSHADER_AST_COMPUNIT_START_RANGE,    /* things in global scope. */
+    MOJOSHADER_AST_COMPUNIT_FUNCTION,
+    MOJOSHADER_AST_COMPUNIT_TYPEDEF,
+    MOJOSHADER_AST_COMPUNIT_STRUCT,
+    MOJOSHADER_AST_COMPUNIT_VARIABLE,
+    MOJOSHADER_AST_COMPUNIT_END_RANGE,
+
+    MOJOSHADER_AST_STATEMENT_START_RANGE,   /* statements in function scope. */
+    MOJOSHADER_AST_STATEMENT_EMPTY,
+    MOJOSHADER_AST_STATEMENT_BREAK,
+    MOJOSHADER_AST_STATEMENT_CONTINUE,
+    MOJOSHADER_AST_STATEMENT_DISCARD,
+    MOJOSHADER_AST_STATEMENT_BLOCK,
+    MOJOSHADER_AST_STATEMENT_EXPRESSION,
+    MOJOSHADER_AST_STATEMENT_IF,
+    MOJOSHADER_AST_STATEMENT_SWITCH,
+    MOJOSHADER_AST_STATEMENT_FOR,
+    MOJOSHADER_AST_STATEMENT_DO,
+    MOJOSHADER_AST_STATEMENT_WHILE,
+    MOJOSHADER_AST_STATEMENT_RETURN,
+    MOJOSHADER_AST_STATEMENT_TYPEDEF,
+    MOJOSHADER_AST_STATEMENT_STRUCT,
+    MOJOSHADER_AST_STATEMENT_VARDECL,
+    MOJOSHADER_AST_STATEMENT_END_RANGE,
+
+    MOJOSHADER_AST_MISC_START_RANGE,        /* misc. syntactic glue. */
+    MOJOSHADER_AST_FUNCTION_PARAMS,
+    MOJOSHADER_AST_FUNCTION_SIGNATURE,
+    MOJOSHADER_AST_SCALAR_OR_ARRAY,
+    MOJOSHADER_AST_TYPEDEF,
+    MOJOSHADER_AST_PACK_OFFSET,
+    MOJOSHADER_AST_VARIABLE_LOWLEVEL,
+    MOJOSHADER_AST_ANNOTATION,
+    MOJOSHADER_AST_VARIABLE_DECLARATION,
+    MOJOSHADER_AST_STRUCT_DECLARATION,
+    MOJOSHADER_AST_STRUCT_MEMBER,
+    MOJOSHADER_AST_SWITCH_CASE,
+    MOJOSHADER_AST_ARGUMENTS,
+    MOJOSHADER_AST_MISC_END_RANGE,
+
+    MOJOSHADER_AST_END_RANGE
+} MOJOSHADER_astNodeType;
+
+typedef struct MOJOSHADER_astNodeInfo
+{
+    MOJOSHADER_astNodeType type;
+    const char *filename;
+    unsigned int line;
+} MOJOSHADER_astNodeInfo;
+
+typedef enum MOJOSHADER_astVariableAttributes
+{
+    MOJOSHADER_AST_VARATTR_EXTERN = (1 << 0),
+    MOJOSHADER_AST_VARATTR_NOINTERPOLATION = (1 << 1),
+    MOJOSHADER_AST_VARATTR_SHARED = (1 << 2),
+    MOJOSHADER_AST_VARATTR_STATIC = (1 << 3),
+    MOJOSHADER_AST_VARATTR_UNIFORM = (1 << 4),
+    MOJOSHADER_AST_VARATTR_VOLATILE = (1 << 5),
+    MOJOSHADER_AST_VARATTR_CONST = (1 << 6),
+    MOJOSHADER_AST_VARATTR_ROWMAJOR = (1 << 7),
+    MOJOSHADER_AST_VARATTR_COLUMNMAJOR = (1 << 8)
+} MOJOSHADER_astVariableAttributes;
+
+typedef enum MOJOSHADER_astIfAttributes
+{
+    MOJOSHADER_AST_IFATTR_NONE,
+    MOJOSHADER_AST_IFATTR_BRANCH,
+    MOJOSHADER_AST_IFATTR_FLATTEN,
+    MOJOSHADER_AST_IFATTR_IFALL,
+    MOJOSHADER_AST_IFATTR_IFANY,
+    MOJOSHADER_AST_IFATTR_PREDICATE,
+    MOJOSHADER_AST_IFATTR_PREDICATEBLOCK,
+} MOJOSHADER_astIfAttributes;
+
+typedef enum MOJOSHADER_astSwitchAttributes
+{
+    MOJOSHADER_AST_SWITCHATTR_NONE,
+    MOJOSHADER_AST_SWITCHATTR_FLATTEN,
+    MOJOSHADER_AST_SWITCHATTR_BRANCH,
+    MOJOSHADER_AST_SWITCHATTR_FORCECASE,
+    MOJOSHADER_AST_SWITCHATTR_CALL
+} MOJOSHADER_astSwitchAttributes;
+
+/* You can cast any AST node pointer to this. */
+typedef struct MOJOSHADER_astGeneric
+{
+    MOJOSHADER_astNodeInfo ast;
+} MOJOSHADER_astGeneric;
+
+typedef struct MOJOSHADER_astExpression
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+} MOJOSHADER_astExpression;
+
+typedef struct MOJOSHADER_astArguments
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_ARGUMENTS */
+    MOJOSHADER_astExpression *argument;
+    struct MOJOSHADER_astArguments *next;
+} MOJOSHADER_astArguments;
+
+typedef struct MOJOSHADER_astExpressionUnary
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpression *operand;
+} MOJOSHADER_astExpressionUnary;
+
+typedef struct MOJOSHADER_astExpressionBinary
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpression *left;
+    MOJOSHADER_astExpression *right;
+} MOJOSHADER_astExpressionBinary;
+
+typedef struct MOJOSHADER_astExpressionTernary
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpression *left;
+    MOJOSHADER_astExpression *center;
+    MOJOSHADER_astExpression *right;
+} MOJOSHADER_astExpressionTernary;
+
+/* Identifier indexes aren't available until semantic analysis phase completes.
+ *  It provides a unique id for this identifier's variable.
+ *  It will be negative for global scope, positive for function scope
+ *  (global values are globally unique, function values are only
+ *  unique within the scope of the given function). There's a different
+ *  set of indices if this identifier is a function (positive for
+ *  user-defined functions, negative for intrinsics).
+ *  May be zero for various reasons (unknown identifier, etc).
+ */
+typedef struct MOJOSHADER_astExpressionIdentifier
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_IDENTIFIER */
+    const MOJOSHADER_astDataType *datatype;
+    const char *identifier;
+    int index;
+} MOJOSHADER_astExpressionIdentifier;
+
+typedef struct MOJOSHADER_astExpressionIntLiteral
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_INT_LITERAL */
+    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_INT */
+    int value;
+} MOJOSHADER_astExpressionIntLiteral;
+
+typedef struct MOJOSHADER_astExpressionFloatLiteral
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_FLOAT_LITERAL */
+    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_FLOAT */
+    double value;
+} MOJOSHADER_astExpressionFloatLiteral;
+
+typedef struct MOJOSHADER_astExpressionStringLiteral
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_STRING_LITERAL */
+    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_STRING */
+    const char *string;
+} MOJOSHADER_astExpressionStringLiteral;
+
+typedef struct MOJOSHADER_astExpressionBooleanLiteral
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_BOOLEAN_LITERAL */
+    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_BOOL */
+    int value;  /* Always 1 or 0. */
+} MOJOSHADER_astExpressionBooleanLiteral;
+
+typedef struct MOJOSHADER_astExpressionConstructor
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CONSTRUCTOR */
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astArguments *args;
+} MOJOSHADER_astExpressionConstructor;
+
+typedef struct MOJOSHADER_astExpressionDerefStruct
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_DEREF_STRUCT */
+    const MOJOSHADER_astDataType *datatype;
+    /* !!! FIXME:
+     *  "identifier" is misnamed; this might not be an identifier at all:
+     *    x = FunctionThatReturnsAStruct().SomeMember;
+     */
+    MOJOSHADER_astExpression *identifier;
+    const char *member;
+    int isswizzle;  /* Always 1 or 0. Never set by parseAst()! */
+    int member_index;  /* Never set by parseAst()! */
+} MOJOSHADER_astExpressionDerefStruct;
+
+typedef struct MOJOSHADER_astExpressionCallFunction
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CALLFUNC */
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpressionIdentifier *identifier;
+    MOJOSHADER_astArguments *args;
+} MOJOSHADER_astExpressionCallFunction;
+
+typedef struct MOJOSHADER_astExpressionCast
+{
+    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CAST */
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpression *operand;
+} MOJOSHADER_astExpressionCast;
+
+typedef struct MOJOSHADER_astCompilationUnit
+{
+    MOJOSHADER_astNodeInfo ast;
+    struct MOJOSHADER_astCompilationUnit *next;
+} MOJOSHADER_astCompilationUnit;
+
+typedef enum MOJOSHADER_astFunctionStorageClass
+{
+    MOJOSHADER_AST_FNSTORECLS_NONE,
+    MOJOSHADER_AST_FNSTORECLS_INLINE
+} MOJOSHADER_astFunctionStorageClass;
+
+typedef enum MOJOSHADER_astInputModifier
+{
+    MOJOSHADER_AST_INPUTMOD_NONE,
+    MOJOSHADER_AST_INPUTMOD_IN,
+    MOJOSHADER_AST_INPUTMOD_OUT,
+    MOJOSHADER_AST_INPUTMOD_INOUT,
+    MOJOSHADER_AST_INPUTMOD_UNIFORM
+} MOJOSHADER_astInputModifier;
+
+typedef enum MOJOSHADER_astInterpolationModifier
+{
+    MOJOSHADER_AST_INTERPMOD_NONE,
+    MOJOSHADER_AST_INTERPMOD_LINEAR,
+    MOJOSHADER_AST_INTERPMOD_CENTROID,
+    MOJOSHADER_AST_INTERPMOD_NOINTERPOLATION,
+    MOJOSHADER_AST_INTERPMOD_NOPERSPECTIVE,
+    MOJOSHADER_AST_INTERPMOD_SAMPLE
+} MOJOSHADER_astInterpolationModifier;
+
+typedef struct MOJOSHADER_astFunctionParameters
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astInputModifier input_modifier;
+    const char *identifier;
+    const char *semantic;
+    MOJOSHADER_astInterpolationModifier interpolation_modifier;
+    MOJOSHADER_astExpression *initializer;
+    struct MOJOSHADER_astFunctionParameters *next;
+} MOJOSHADER_astFunctionParameters;
+
+typedef struct MOJOSHADER_astFunctionSignature
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    const char *identifier;
+    MOJOSHADER_astFunctionParameters *params;
+    MOJOSHADER_astFunctionStorageClass storage_class;
+    const char *semantic;
+} MOJOSHADER_astFunctionSignature;
+
+typedef struct MOJOSHADER_astScalarOrArray
+{
+    MOJOSHADER_astNodeInfo ast;
+    const char *identifier;
+    int isarray;  /* boolean: 1 or 0 */
+    MOJOSHADER_astExpression *dimension;
+} MOJOSHADER_astScalarOrArray;
+
+typedef struct MOJOSHADER_astAnnotations
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astExpression *initializer;
+    struct MOJOSHADER_astAnnotations *next;
+} MOJOSHADER_astAnnotations;
+
+typedef struct MOJOSHADER_astPackOffset
+{
+    MOJOSHADER_astNodeInfo ast;
+    const char *ident1;   /* !!! FIXME: rename this. */
+    const char *ident2;
+} MOJOSHADER_astPackOffset;
+
+typedef struct MOJOSHADER_astVariableLowLevel
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astPackOffset *packoffset;
+    const char *register_name;
+} MOJOSHADER_astVariableLowLevel;
+
+typedef struct MOJOSHADER_astStructMembers
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    const char *semantic;
+    MOJOSHADER_astScalarOrArray *details;
+    MOJOSHADER_astInterpolationModifier interpolation_mod;
+    struct MOJOSHADER_astStructMembers *next;
+} MOJOSHADER_astStructMembers;
+
+typedef struct MOJOSHADER_astStructDeclaration
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    const char *name;
+    MOJOSHADER_astStructMembers *members;
+} MOJOSHADER_astStructDeclaration;
+
+typedef struct MOJOSHADER_astVariableDeclaration
+{
+    MOJOSHADER_astNodeInfo ast;
+    int attributes;
+    const MOJOSHADER_astDataType *datatype;
+    MOJOSHADER_astStructDeclaration *anonymous_datatype;
+    MOJOSHADER_astScalarOrArray *details;
+    const char *semantic;
+    MOJOSHADER_astAnnotations *annotations;
+    MOJOSHADER_astExpression *initializer;
+    MOJOSHADER_astVariableLowLevel *lowlevel;
+    struct MOJOSHADER_astVariableDeclaration *next;
+} MOJOSHADER_astVariableDeclaration;
+
+typedef struct MOJOSHADER_astStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    struct MOJOSHADER_astStatement *next;
+} MOJOSHADER_astStatement;
+
+typedef MOJOSHADER_astStatement MOJOSHADER_astEmptyStatement;
+typedef MOJOSHADER_astStatement MOJOSHADER_astBreakStatement;
+typedef MOJOSHADER_astStatement MOJOSHADER_astContinueStatement;
+typedef MOJOSHADER_astStatement MOJOSHADER_astDiscardStatement;
+
+/* something enclosed in "{}" braces. */
+typedef struct MOJOSHADER_astBlockStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astStatement *statements;  /* list of child statements. */
+} MOJOSHADER_astBlockStatement;
+
+typedef struct MOJOSHADER_astReturnStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astExpression *expr;
+} MOJOSHADER_astReturnStatement;
+
+typedef struct MOJOSHADER_astExpressionStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astExpression *expr;
+} MOJOSHADER_astExpressionStatement;
+
+typedef struct MOJOSHADER_astIfStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    int attributes;
+    MOJOSHADER_astExpression *expr;
+    MOJOSHADER_astStatement *statement;
+    MOJOSHADER_astStatement *else_statement;
+} MOJOSHADER_astIfStatement;
+
+typedef struct MOJOSHADER_astSwitchCases
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astExpression *expr;
+    MOJOSHADER_astStatement *statement;
+    struct MOJOSHADER_astSwitchCases *next;
+} MOJOSHADER_astSwitchCases;
+
+typedef struct MOJOSHADER_astSwitchStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    int attributes;
+    MOJOSHADER_astExpression *expr;
+    MOJOSHADER_astSwitchCases *cases;
+} MOJOSHADER_astSwitchStatement;
+
+typedef struct MOJOSHADER_astWhileStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    int unroll;  /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
+    MOJOSHADER_astExpression *expr;
+    MOJOSHADER_astStatement *statement;
+} MOJOSHADER_astWhileStatement;
+
+typedef MOJOSHADER_astWhileStatement MOJOSHADER_astDoStatement;
+
+typedef struct MOJOSHADER_astForStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    int unroll;  /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
+    MOJOSHADER_astVariableDeclaration *var_decl;  /* either this ... */
+    MOJOSHADER_astExpression *initializer;        /*  ... or this will used. */
+    MOJOSHADER_astExpression *looptest;
+    MOJOSHADER_astExpression *counter;
+    MOJOSHADER_astStatement *statement;
+} MOJOSHADER_astForStatement;
+
+typedef struct MOJOSHADER_astTypedef
+{
+    MOJOSHADER_astNodeInfo ast;
+    const MOJOSHADER_astDataType *datatype;
+    int isconst;  /* boolean: 1 or 0 */
+    MOJOSHADER_astScalarOrArray *details;
+} MOJOSHADER_astTypedef;
+
+typedef struct MOJOSHADER_astTypedefStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astTypedef *type_info;
+} MOJOSHADER_astTypedefStatement;
+
+typedef struct MOJOSHADER_astVarDeclStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astVariableDeclaration *declaration;
+} MOJOSHADER_astVarDeclStatement;
+
+typedef struct MOJOSHADER_astStructStatement
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astStatement *next;
+    MOJOSHADER_astStructDeclaration *struct_info;
+} MOJOSHADER_astStructStatement;
+
+typedef struct MOJOSHADER_astCompilationUnitFunction
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astCompilationUnit *next;
+    MOJOSHADER_astFunctionSignature *declaration;
+    MOJOSHADER_astStatement *definition;
+    int index;  /* unique id. Will be 0 until semantic analysis runs. */
+} MOJOSHADER_astCompilationUnitFunction;
+
+typedef struct MOJOSHADER_astCompilationUnitTypedef
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astCompilationUnit *next;
+    MOJOSHADER_astTypedef *type_info;
+} MOJOSHADER_astCompilationUnitTypedef;
+
+typedef struct MOJOSHADER_astCompilationUnitStruct
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astCompilationUnit *next;
+    MOJOSHADER_astStructDeclaration *struct_info;
+} MOJOSHADER_astCompilationUnitStruct;
+
+typedef struct MOJOSHADER_astCompilationUnitVariable
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astCompilationUnit *next;
+    MOJOSHADER_astVariableDeclaration *declaration;
+} MOJOSHADER_astCompilationUnitVariable;
+
+
+/* this is way cleaner than all the nasty typecasting. */
+typedef union MOJOSHADER_astNode
+{
+    MOJOSHADER_astNodeInfo ast;
+    MOJOSHADER_astGeneric generic;
+    MOJOSHADER_astExpression expression;
+    MOJOSHADER_astArguments arguments;
+    MOJOSHADER_astExpressionUnary unary;
+    MOJOSHADER_astExpressionBinary binary;
+    MOJOSHADER_astExpressionTernary ternary;
+    MOJOSHADER_astExpressionIdentifier identifier;
+    MOJOSHADER_astExpressionIntLiteral intliteral;
+    MOJOSHADER_astExpressionFloatLiteral floatliteral;
+    MOJOSHADER_astExpressionStringLiteral stringliteral;
+    MOJOSHADER_astExpressionBooleanLiteral boolliteral;
+    MOJOSHADER_astExpressionConstructor constructor;
+    MOJOSHADER_astExpressionDerefStruct derefstruct;
+    MOJOSHADER_astExpressionCallFunction callfunc;
+    MOJOSHADER_astExpressionCast cast;
+    MOJOSHADER_astCompilationUnit compunit;
+    MOJOSHADER_astFunctionParameters params;
+    MOJOSHADER_astFunctionSignature funcsig;
+    MOJOSHADER_astScalarOrArray soa;
+    MOJOSHADER_astAnnotations annotations;
+    MOJOSHADER_astPackOffset packoffset;
+    MOJOSHADER_astVariableLowLevel varlowlevel;
+    MOJOSHADER_astStructMembers structmembers;
+    MOJOSHADER_astStructDeclaration structdecl;
+    MOJOSHADER_astVariableDeclaration vardecl;
+    MOJOSHADER_astStatement stmt;
+    MOJOSHADER_astEmptyStatement emptystmt;
+    MOJOSHADER_astBreakStatement breakstmt;
+    MOJOSHADER_astContinueStatement contstmt;
+    MOJOSHADER_astDiscardStatement discardstmt;
+    MOJOSHADER_astBlockStatement blockstmt;
+    MOJOSHADER_astReturnStatement returnstmt;
+    MOJOSHADER_astExpressionStatement exprstmt;
+    MOJOSHADER_astIfStatement ifstmt;
+    MOJOSHADER_astSwitchCases cases;
+    MOJOSHADER_astSwitchStatement switchstmt;
+    MOJOSHADER_astWhileStatement whilestmt;
+    MOJOSHADER_astDoStatement dostmt;
+    MOJOSHADER_astForStatement forstmt;
+    MOJOSHADER_astTypedef typdef;
+    MOJOSHADER_astTypedefStatement typedefstmt;
+    MOJOSHADER_astVarDeclStatement vardeclstmt;
+    MOJOSHADER_astStructStatement structstmt;
+    MOJOSHADER_astCompilationUnitFunction funcunit;
+    MOJOSHADER_astCompilationUnitTypedef typedefunit;
+    MOJOSHADER_astCompilationUnitStruct structunit;
+    MOJOSHADER_astCompilationUnitVariable varunit;
+} MOJOSHADER_astNode;
+
+
+/*
+ * Structure used to return data from parsing of a shader into an AST...
+ */
+/* !!! FIXME: most of these ints should be unsigned. */
+typedef struct MOJOSHADER_astData
+{
+    /*
+     * The number of elements pointed to by (errors).
+     */
+    int error_count;
+
+    /*
+     * (error_count) elements of data that specify errors that were generated
+     *  by parsing this shader.
+     * This can be NULL if there were no errors or if (error_count) is zero.
+     *  Note that this will only produce errors for syntax problems. Most of
+     *  the things we expect a compiler to produce errors for--incompatible
+     *  types, unknown identifiers, etc--are not checked at all during
+     *  initial generation of the syntax tree...bogus programs that would
+     *  fail to compile will pass here without error, if they are syntactically
+     *  correct!
+     */
+    MOJOSHADER_error *errors;
+
+    /*
+     * The name of the source profile used to parse the shader. Will be NULL
+     *  on error.
+     */
+    const char *source_profile;
+
+    /*
+     * The actual syntax tree. You are responsible for walking it yourself.
+     *  CompilationUnits are always the top of the tree (functions, typedefs,
+     *  global variables, etc). Will be NULL on error.
+     */
+    const MOJOSHADER_astNode *ast;
+
+    /*
+     * This is the malloc implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_malloc malloc;
+
+    /*
+     * This is the free implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_free free;
+
+    /*
+     * This is the pointer you passed as opaque data for your allocator.
+     */
+    void *malloc_data;
+
+    /*
+     * This is internal data, and not for the application to touch.
+     */
+    void *opaque;
+} MOJOSHADER_astData;
+
+
+/*
+ * You almost certainly don't need this function, unless you absolutely know
+ *  why you need it without hesitation. This is almost certainly only good for
+ *  building code analysis tools on top of.
+ *
+ * This is intended to parse HLSL source code, turning it into an abstract
+ *  syntax tree.
+ *
+ * (srcprofile) specifies the source language of the shader. You can specify
+ *  a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
+ *
+ * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
+ *  actually access this file, as we obtain our data from (source). This
+ *  string is copied when we need to report errors while processing (source),
+ *  as opposed to errors in a file referenced via the #include directive in
+ *  (source). If this is NULL, then errors will report the filename as NULL,
+ *  too.
+ *
+ * (source) is an UTF-8 string of valid high-level shader source code.
+ *  It does not need to be NULL-terminated.
+ *
+ * (sourcelen) is the length of the string pointed to by (source), in bytes.
+ *
+ * (defines) points to (define_count) preprocessor definitions, and can be
+ *  NULL. These are treated by the preprocessor as if the source code started
+ *  with one #define for each entry you pass in here.
+ *
+ * (include_open) and (include_close) let the app control the preprocessor's
+ *  behaviour for #include statements. Both are optional and can be NULL, but
+ *  both must be specified if either is specified.
+ *
+ * This will return a MOJOSHADER_astData. The data supplied here gives the
+ *  application a tree-like structure they can walk to see the layout of
+ *  a given program. When you are done with this data, pass it to
+ *  MOJOSHADER_freeCompileData() to deallocate resources.
+ *
+ * This function will never return NULL, even if the system is completely
+ *  out of memory upon entry (in which case, this function returns a static
+ *  MOJOSHADER_astData object, which is still safe to pass to
+ *  MOJOSHADER_freeAstData()).
+ *
+ * As parsing requires some memory to be allocated, you may provide a
+ *  custom allocator to this function, which will be used to allocate/free
+ *  memory. They function just like malloc() and free(). We do not use
+ *  realloc(). If you don't care, pass NULL in for the allocator functions.
+ *  If your allocator needs instance-specific data, you may supply it with the
+ *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
+ *
+ * This function is thread safe, so long as the various callback functions
+ *  are, too, and that the parameters remains intact for the duration of the
+ *  call. This allows you to parse several shaders on separate CPU cores
+ *  at the same time.
+ */
+const MOJOSHADER_astData *MOJOSHADER_parseAst(const char *srcprofile,
+                                    const char *filename, const char *source,
+                                    unsigned int sourcelen,
+                                    const MOJOSHADER_preprocessorDefine *defs,
+                                    unsigned int define_count,
+                                    MOJOSHADER_includeOpen include_open,
+                                    MOJOSHADER_includeClose include_close,
+                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                    void *d);
+
+
+/* !!! FIXME: expose semantic analysis to the public API? */
+
+
+/*
+ * Call this to dispose of AST parsing results when you are done with them.
+ *  This will call the MOJOSHADER_free function you provided to
+ *  MOJOSHADER_parseAst() multiple times, if you provided one.
+ *  Passing a NULL here is a safe no-op.
+ *
+ * This function is thread safe, so long as any allocator you passed into
+ *  MOJOSHADER_parseAst() is, too.
+ */
+void MOJOSHADER_freeAstData(const MOJOSHADER_astData *data);
+
+
+/* Intermediate Representation interface... */
+/* !!! FIXME: there is currently no way to access the IR via the public API. */
+typedef enum MOJOSHADER_irNodeType
+{
+    MOJOSHADER_IR_START_RANGE_EXPR,
+    MOJOSHADER_IR_CONSTANT,
+    MOJOSHADER_IR_TEMP,
+    MOJOSHADER_IR_BINOP,
+    MOJOSHADER_IR_MEMORY,
+    MOJOSHADER_IR_CALL,
+    MOJOSHADER_IR_ESEQ,
+    MOJOSHADER_IR_ARRAY,
+    MOJOSHADER_IR_CONVERT,
+    MOJOSHADER_IR_SWIZZLE,
+    MOJOSHADER_IR_CONSTRUCT,
+    MOJOSHADER_IR_END_RANGE_EXPR,
+
+    MOJOSHADER_IR_START_RANGE_STMT,
+    MOJOSHADER_IR_MOVE,
+    MOJOSHADER_IR_EXPR_STMT,
+    MOJOSHADER_IR_JUMP,
+    MOJOSHADER_IR_CJUMP,
+    MOJOSHADER_IR_SEQ,
+    MOJOSHADER_IR_LABEL,
+    MOJOSHADER_IR_DISCARD,
+    MOJOSHADER_IR_END_RANGE_STMT,
+
+    MOJOSHADER_IR_START_RANGE_MISC,
+    MOJOSHADER_IR_EXPRLIST,
+    MOJOSHADER_IR_END_RANGE_MISC,
+
+    MOJOSHADER_IR_END_RANGE
+} MOJOSHADER_irNodeType;
+
+typedef struct MOJOSHADER_irNodeInfo
+{
+    MOJOSHADER_irNodeType type;
+    const char *filename;
+    unsigned int line;
+} MOJOSHADER_irNodeInfo;
+
+typedef struct MOJOSHADER_irExprList MOJOSHADER_irExprList;
+
+/*
+ * IR nodes are categorized into Expressions, Statements, and Everything Else.
+ *  You can cast any of them to MOJOSHADER_irGeneric, but this split is
+ *  useful for slightly better type-checking (you can't cleanly assign
+ *  something that doesn't return a value to something that wants one, etc).
+ * These broader categories are just unions of the simpler types, so the
+ *  real definitions are below all the things they contain (but these
+ *  predeclarations are because the simpler types refer to the broader
+ *  categories).
+ */
+typedef union MOJOSHADER_irExpression MOJOSHADER_irExpression;  /* returns a value. */
+typedef union MOJOSHADER_irStatement MOJOSHADER_irStatement;   /* no returned value. */
+typedef union MOJOSHADER_irMisc MOJOSHADER_irMisc;        /* Everything Else. */
+typedef union MOJOSHADER_irNode MOJOSHADER_irNode;        /* Generic uber-wrapper. */
+
+/* You can cast any IR node pointer to this. */
+typedef struct MOJOSHADER_irGeneric
+{
+    MOJOSHADER_irNodeInfo ir;
+} MOJOSHADER_irGeneric;
+
+
+/* These are used for MOJOSHADER_irBinOp */
+typedef enum MOJOSHADER_irBinOpType
+{
+    MOJOSHADER_IR_BINOP_ADD,
+    MOJOSHADER_IR_BINOP_SUBTRACT,
+    MOJOSHADER_IR_BINOP_MULTIPLY,
+    MOJOSHADER_IR_BINOP_DIVIDE,
+    MOJOSHADER_IR_BINOP_MODULO,
+    MOJOSHADER_IR_BINOP_AND,
+    MOJOSHADER_IR_BINOP_OR,
+    MOJOSHADER_IR_BINOP_XOR,
+    MOJOSHADER_IR_BINOP_LSHIFT,
+    MOJOSHADER_IR_BINOP_RSHIFT,
+    MOJOSHADER_IR_BINOP_UNKNOWN
+} MOJOSHADER_irBinOpType;
+
+typedef enum MOJOSHADER_irConditionType
+{
+    MOJOSHADER_IR_COND_EQL,
+    MOJOSHADER_IR_COND_NEQ,
+    MOJOSHADER_IR_COND_LT,
+    MOJOSHADER_IR_COND_GT,
+    MOJOSHADER_IR_COND_LEQ,
+    MOJOSHADER_IR_COND_GEQ,
+    MOJOSHADER_IR_COND_UNKNOWN
+} MOJOSHADER_irConditionType;
+
+
+/* MOJOSHADER_irExpression types... */
+
+typedef struct MOJOSHADER_irExprInfo
+{
+    MOJOSHADER_irNodeInfo ir;
+    MOJOSHADER_astDataTypeType type;
+    int elements;
+} MOJOSHADER_irExprInfo;
+
+typedef struct MOJOSHADER_irConstant    /* Constant value */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONSTANT */
+    union
+    {
+        int ival[16];
+        float fval[16];
+    } value;
+} MOJOSHADER_irConstant;
+
+typedef struct MOJOSHADER_irTemp /* temp value (not necessarily a register). */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_TEMP */
+    int index;
+} MOJOSHADER_irTemp;
+
+typedef struct MOJOSHADER_irBinOp  /* binary operator (+, -, etc) */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_BINOP */
+    MOJOSHADER_irBinOpType op;
+    MOJOSHADER_irExpression *left;
+    MOJOSHADER_irExpression *right;
+} MOJOSHADER_irBinOp;
+
+typedef struct MOJOSHADER_irMemory
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_MEMORY */
+    int index;  /* not final addresses, just a unique identifier. */
+} MOJOSHADER_irMemory;
+
+typedef struct MOJOSHADER_irCall
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CALL */
+    int index;
+    MOJOSHADER_irExprList *args;
+} MOJOSHADER_irCall;
+
+typedef struct MOJOSHADER_irESeq  /* statement with result */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_ESEQ */
+    MOJOSHADER_irStatement *stmt;  /* execute this for side-effects, then... */
+    MOJOSHADER_irExpression *expr; /* ...use this for the result. */
+} MOJOSHADER_irESeq;
+
+typedef struct MOJOSHADER_irArray  /* Array dereference. */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_ARRAY */
+    MOJOSHADER_irExpression *array;
+    MOJOSHADER_irExpression *element;
+} MOJOSHADER_irArray;
+
+typedef struct MOJOSHADER_irConvert  /* casting between datatypes */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONVERT */
+    MOJOSHADER_irExpression *expr;
+} MOJOSHADER_irConvert;
+
+typedef struct MOJOSHADER_irSwizzle  /* vector swizzle */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_SWIZZLE */
+    MOJOSHADER_irExpression *expr;
+    char channels[4];
+} MOJOSHADER_irSwizzle;
+
+typedef struct MOJOSHADER_irConstruct  /* vector construct from discrete items */
+{
+    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONTSTRUCT */
+    MOJOSHADER_irExprList *args;
+} MOJOSHADER_irConstruct;
+
+/* Wrap the whole category in a union for type "safety." */
+union MOJOSHADER_irExpression
+{
+    MOJOSHADER_irNodeInfo ir;
+    MOJOSHADER_irExprInfo info;
+    MOJOSHADER_irConstant constant;
+    MOJOSHADER_irTemp temp;
+    MOJOSHADER_irBinOp binop;
+    MOJOSHADER_irMemory memory;
+    MOJOSHADER_irCall call;
+    MOJOSHADER_irESeq eseq;
+    MOJOSHADER_irArray array;
+    MOJOSHADER_irConvert convert;
+    MOJOSHADER_irSwizzle swizzle;
+    MOJOSHADER_irConstruct construct;
+};
+
+/* MOJOSHADER_irStatement types. */
+
+typedef struct MOJOSHADER_irMove  /* load/store. */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_MOVE */
+    MOJOSHADER_irExpression *dst; /* must result in a temp or mem! */
+    MOJOSHADER_irExpression *src;
+    int writemask;  // for write-masking vector channels.
+} MOJOSHADER_irMove;
+
+typedef struct MOJOSHADER_irExprStmt  /* evaluate expression, throw it away. */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_EXPR_STMT */
+    MOJOSHADER_irExpression *expr;
+} MOJOSHADER_irExprStmt;
+
+typedef struct MOJOSHADER_irJump  /* unconditional jump */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_JUMP */
+    int label;
+    // !!! FIXME: possible label list, for further optimization passes.
+} MOJOSHADER_irJump;
+
+typedef struct MOJOSHADER_irCJump  /* conditional jump */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_CJUMP */
+    MOJOSHADER_irConditionType cond;
+    MOJOSHADER_irExpression *left;  /* if (left cond right) */
+    MOJOSHADER_irExpression *right;
+    int iftrue;  /* label id for true case. */
+    int iffalse; /* label id for false case. */
+} MOJOSHADER_irCJump;
+
+typedef struct MOJOSHADER_irSeq  /* statement without side effects */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_SEQ */
+    MOJOSHADER_irStatement *first;
+    MOJOSHADER_irStatement *next;
+} MOJOSHADER_irSeq;
+
+typedef struct MOJOSHADER_irLabel  /* like a label in assembly language. */
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_LABEL */
+    int index;
+} MOJOSHADER_irLabel;
+
+typedef MOJOSHADER_irGeneric MOJOSHADER_irDiscard;  /* discard statement. */
+
+
+/* Wrap the whole category in a union for type "safety." */
+union MOJOSHADER_irStatement
+{
+    MOJOSHADER_irNodeInfo ir;
+    MOJOSHADER_irGeneric generic;
+    MOJOSHADER_irMove move;
+    MOJOSHADER_irExprStmt expr;
+    MOJOSHADER_irJump jump;
+    MOJOSHADER_irCJump cjump;
+    MOJOSHADER_irSeq seq;
+    MOJOSHADER_irLabel label;
+    MOJOSHADER_irDiscard discard;
+};
+
+/* MOJOSHADER_irMisc types. */
+
+struct MOJOSHADER_irExprList
+{
+    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_EXPRLIST */
+    MOJOSHADER_irExpression *expr;
+    MOJOSHADER_irExprList *next;
+};
+
+/* Wrap the whole category in a union for type "safety." */
+union MOJOSHADER_irMisc
+{
+    MOJOSHADER_irNodeInfo ir;
+    MOJOSHADER_irGeneric generic;
+    MOJOSHADER_irExprList exprlist;
+};
+
+/* This is a catchall for all your needs. :) */
+union MOJOSHADER_irNode
+{
+    MOJOSHADER_irNodeInfo ir;
+    MOJOSHADER_irGeneric generic;
+    MOJOSHADER_irExpression expr;
+    MOJOSHADER_irStatement stmt;
+    MOJOSHADER_irMisc misc;
+};
+
+
+/* Compiler interface... */
+
+/*
+ * Structure used to return data from parsing of a shader...
+ */
+/* !!! FIXME: most of these ints should be unsigned. */
+typedef struct MOJOSHADER_compileData
+{
+    /*
+     * The number of elements pointed to by (errors).
+     */
+    int error_count;
+
+    /*
+     * (error_count) elements of data that specify errors that were generated
+     *  by compiling this shader.
+     * This can be NULL if there were no errors or if (error_count) is zero.
+     */
+    MOJOSHADER_error *errors;
+
+    /*
+     * The number of elements pointed to by (warnings).
+     */
+    int warning_count;
+
+    /*
+     * (warning_count) elements of data that specify errors that were
+     *  generated by compiling this shader.
+     * This can be NULL if there were no errors or if (warning_count) is zero.
+     */
+    MOJOSHADER_error *warnings;
+
+    /*
+     * The name of the source profile used to compile the shader. Will be NULL
+     *  on error.
+     */
+    const char *source_profile;
+
+    /*
+     * Bytes of output from compiling. This will be a null-terminated ASCII
+     *  string of D3D assembly source code.
+     */
+    const char *output;
+
+    /*
+     * Byte count for output, not counting any null terminator.
+     *  Will be 0 on error.
+     */
+    int output_len;
+
+    /*
+     * The number of elements pointed to by (symbols).
+     */
+    int symbol_count;
+
+    /*
+     * (symbol_count) elements of data that specify high-level symbol data
+     *  for the shader. This can be used by MOJOSHADER_assemble() to
+     *  generate a CTAB section in bytecode, which is needed by
+     *  MOJOSHADER_parseData() to handle some shaders. This can be NULL on
+     *  error or if (symbol_count) is zero.
+     */
+    MOJOSHADER_symbol *symbols;
+
+    /*
+     * This is the malloc implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_malloc malloc;
+
+    /*
+     * This is the free implementation you passed to MOJOSHADER_parse().
+     */
+    MOJOSHADER_free free;
+
+    /*
+     * This is the pointer you passed as opaque data for your allocator.
+     */
+    void *malloc_data;
+} MOJOSHADER_compileData;
+
+
+/*
+ * This function is optional. Use this to compile high-level shader programs.
+ *
+ * This is intended to turn HLSL source code into D3D assembly code, which
+ *  can then be passed to MOJOSHADER_assemble() to convert it to D3D bytecode
+ *  (which can then be used with MOJOSHADER_parseData() to support other
+ *  shading targets).
+ *
+ * (srcprofile) specifies the source language of the shader. You can specify
+ *  a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
+ *
+ * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
+ *  actually access this file, as we obtain our data from (source). This
+ *  string is copied when we need to report errors while processing (source),
+ *  as opposed to errors in a file referenced via the #include directive in
+ *  (source). If this is NULL, then errors will report the filename as NULL,
+ *  too.
+ *
+ * (source) is an UTF-8 string of valid high-level shader source code.
+ *  It does not need to be NULL-terminated.
+ *
+ * (sourcelen) is the length of the string pointed to by (source), in bytes.
+ *
+ * (defines) points to (define_count) preprocessor definitions, and can be
+ *  NULL. These are treated by the preprocessor as if the source code started
+ *  with one #define for each entry you pass in here.
+ *
+ * (include_open) and (include_close) let the app control the preprocessor's
+ *  behaviour for #include statements. Both are optional and can be NULL, but
+ *  both must be specified if either is specified.
+ *
+ * This will return a MOJOSHADER_compileData. The data supplied here is
+ *  sufficient to supply to MOJOSHADER_assemble() for further processing.
+ *  When you are done with this data, pass it to MOJOSHADER_freeCompileData()
+ *  to deallocate resources.
+ *
+ * This function will never return NULL, even if the system is completely
+ *  out of memory upon entry (in which case, this function returns a static
+ *  MOJOSHADER_compileData object, which is still safe to pass to
+ *  MOJOSHADER_freeCompileData()).
+ *
+ * As compiling requires some memory to be allocated, you may provide a
+ *  custom allocator to this function, which will be used to allocate/free
+ *  memory. They function just like malloc() and free(). We do not use
+ *  realloc(). If you don't care, pass NULL in for the allocator functions.
+ *  If your allocator needs instance-specific data, you may supply it with the
+ *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
+ *
+ * This function is thread safe, so long as the various callback functions
+ *  are, too, and that the parameters remains intact for the duration of the
+ *  call. This allows you to compile several shaders on separate CPU cores
+ *  at the same time.
+ */
+const MOJOSHADER_compileData *MOJOSHADER_compile(const char *srcprofile,
+                                    const char *filename, const char *source,
+                                    unsigned int sourcelen,
+                                    const MOJOSHADER_preprocessorDefine *defs,
+                                    unsigned int define_count,
+                                    MOJOSHADER_includeOpen include_open,
+                                    MOJOSHADER_includeClose include_close,
+                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                    void *d);
+
+
+/*
+ * Call this to dispose of compile results when you are done with them.
+ *  This will call the MOJOSHADER_free function you provided to
+ *  MOJOSHADER_compile() multiple times, if you provided one.
+ *  Passing a NULL here is a safe no-op.
+ *
+ * This function is thread safe, so long as any allocator you passed into
+ *  MOJOSHADER_compile() is, too.
+ */
+void MOJOSHADER_freeCompileData(const MOJOSHADER_compileData *data);
+
+
+/* OpenGL interface... */
+
+/*
+ * Signature for function lookup callbacks. MojoShader will call a function
+ *  you provide to get OpenGL entry points (both standard functions and
+ *  extensions). Through this, MojoShader never links directly to OpenGL,
+ *  but relies on you to provide the implementation. This means you can
+ *  swap in different drivers, or hook functions (log every GL call MojoShader
+ *  makes, etc).
+ *
+ * (fnname) is the function name we want the address for ("glBegin" or
+ *  whatever. (data) is a void pointer you provide, if this callback needs
+ *  extra information. If you don't need it, you may specify NULL.
+ *
+ * Return the entry point on success, NULL if it couldn't be found.
+ *  Note that this could ask for standard entry points like glEnable(), or
+ *  extensions like glProgramLocalParameterI4ivNV(), so you might need
+ *  to check two places to find the desired entry point, depending on your
+ *  platform (Windows might need to look in OpenGL32.dll and use WGL, etc).
+ */
+typedef void *(*MOJOSHADER_glGetProcAddress)(const char *fnname, void *data);
+
+
+/*
+ * "Contexts" map to OpenGL contexts...you need one per window, or whatever,
+ *  and need to inform MojoShader when you make a new one current.
+ *
+ * "Shaders" refer to individual vertex or pixel programs, and are created
+ *  by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
+ *  "linked" into a "Program" before you can use them to render.
+ *
+ * To the calling application, these are all opaque handles.
+ */
+typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
+typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
+typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
+
+
+/*
+ * Get a list of available profiles. This will fill in the array (profs)
+ *  with up to (size) pointers of profiles that the current system can handle;
+ *  that is, the profiles are built into MojoShader and the OpenGL extensions
+ *  required for them exist at runtime. This function returns the number of
+ *  available profiles, which may be more, less, or equal to (size).
+ *
+ * If there are more than (size) profiles, the (profs) buffer will not
+ *  overflow. You can check the return value for the total number of
+ *  available profiles, allocate more space, and try again if necessary.
+ *  Calling this function with (size) == 0 is legal.
+ *
+ * You can only call this AFTER you have successfully built your GL context
+ *  and made it current. This function will lookup the GL functions it needs
+ *  through the callback you supply, via (lookup) and (d). The lookup function
+ *  is neither stored nor used by MojoShader after this function returns, nor
+ *  are the functions it might look up.
+ *
+ * You should not free any strings returned from this function; they are
+ *  pointers to internal, probably static, memory.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ */
+int MOJOSHADER_glAvailableProfiles(MOJOSHADER_glGetProcAddress lookup, void *d,
+                                   const char **profs, const int size);
+
+
+/*
+ * Determine the best profile to use for the current system.
+ *
+ * You can only call this AFTER you have successfully built your GL context
+ *  and made it current. This function will lookup the GL functions it needs
+ *  through the callback you supply via (lookup) and (d). The lookup function
+ *  is neither stored nor used by MojoShader after this function returns, nor
+ *  are the functions it might look up.
+ *
+ * Returns the name of the "best" profile on success, NULL if none of the
+ *  available profiles will work on this system. "Best" is a relative term,
+ *  but it generally means the best trade off between feature set and
+ *  performance. The selection algorithm may be arbitrary and complex.
+ *
+ * The returned value is an internal static string, and should not be free()'d
+ *  by the caller. If you get a NULL, calling MOJOSHADER_glGetError() might
+ *  shed some light on why.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ */
+const char *MOJOSHADER_glBestProfile(MOJOSHADER_glGetProcAddress lookup, void *d);
+
+
+/*
+ * Prepare MojoShader to manage OpenGL shaders.
+ *
+ * You do not need to call this if all you want is MOJOSHADER_parse().
+ *
+ * You must call this once AFTER you have successfully built your GL context
+ *  and made it current. This function will lookup the GL functions it needs
+ *  through the callback you supply via (lookup) and (lookup_d), after which
+ *  it may call them at any time up until you call
+ *  MOJOSHADER_glDestroyContext(). The lookup function is neither stored nor
+ *  used by MojoShader after this function returns.
+ *
+ * (profile) is an OpenGL-specific MojoShader profile, which decides how
+ *  Direct3D bytecode shaders get turned into OpenGL programs, and how they
+ *  are fed to the GL.
+ *
+ * (lookup) is a callback that is used to load GL entry points. This callback
+ *  has to look up base GL functions and extension entry points. The pointer
+ *  you supply in (lookup_d) is passed as-is to the callback.
+ *
+ * As MojoShader requires some memory to be allocated, you may provide a
+ *  custom allocator to this function, which will be used to allocate/free
+ *  memory. They function just like malloc() and free(). We do not use
+ *  realloc(). If you don't care, pass NULL in for the allocator functions.
+ *  If your allocator needs instance-specific data, you may supply it with the
+ *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
+ *  functions.
+ *
+ * Returns a new context on success, NULL on error. If you get a new context,
+ *  you need to make it current before using it with
+ *  MOJOSHADER_glMakeContextCurrent().
+ *
+ * This call is NOT thread safe! It must return success before you may call
+ *  any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
+ *  are not thread safe, you should probably only call this from the same
+ *  thread that created the GL context.
+ */
+MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
+                                        MOJOSHADER_glGetProcAddress lookup,
+                                        void *lookup_d,
+                                        MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                        void *malloc_d);
+
+/*
+ * You must call this before using the context that you got from
+ *  MOJOSHADER_glCreateContext(), and must use it when you switch to a new GL
+ *  context.
+ *
+ * You can only have one MOJOSHADER_glContext per actual GL context, or
+ *  undefined behaviour will result.
+ *
+ * It is legal to call this with a NULL pointer to make no context current,
+ *  but you need a valid context to be current to use most of MojoShader.
+ */
+void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);
+
+/*
+ * Get any error state we might have picked up. MojoShader will NOT call
+ *  glGetError() internally, but there are other errors we can pick up,
+ *  such as failed shader compilation, etc.
+ *
+ * Returns a human-readable string. This string is for debugging purposes, and
+ *  not guaranteed to be localized, coherent, or user-friendly in any way.
+ *  It's for programmers!
+ *
+ * The latest error may remain between calls. New errors replace any existing
+ *  error. Don't check this string for a sign that an error happened, check
+ *  return codes instead and use this for explanation when debugging.
+ *
+ * Do not free the returned string: it's a pointer to a static internal
+ *  buffer. Do not keep the pointer around, either, as it's likely to become
+ *  invalid as soon as you call into MojoShader again.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call does NOT require a valid MOJOSHADER_glContext to have been made
+ *  current. The error buffer is shared between contexts, so you can get
+ *  error results from a failed MOJOSHADER_glCreateContext().
+ */
+const char *MOJOSHADER_glGetError(void);
+
+/*
+ * Get the maximum uniforms a shader can support for the current GL context,
+ *  MojoShader profile, and shader type. You can use this to make decisions
+ *  about what shaders you want to use (for example, a less complicated
+ *  shader may be swapped in for lower-end systems).
+ *
+ * Returns the number, or -1 on error.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type);
+
+/*
+ * Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
+ *  You still need to link the shader before you may render with it.
+ *
+ *   (tokenbuf) is a buffer of Direct3D shader bytecode.
+ *   (bufsize) is the size, in bytes, of the bytecode buffer.
+ *   (swiz), (swizcount), (smap), and (smapcount) are passed to
+ *   MOJOSHADER_parse() unmolested.
+ *
+ * Returns NULL on error, or a shader handle on success.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Compiled shaders from this function may not be shared between contexts.
+ */
+MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
+                                                const unsigned int bufsize,
+                                                const MOJOSHADER_swizzle *swiz,
+                                                const unsigned int swizcount,
+                                                const MOJOSHADER_samplerMap *smap,
+                                                const unsigned int smapcount);
+
+
+/*
+ * Get the MOJOSHADER_parseData structure that was produced from the
+ *  call to MOJOSHADER_glCompileShader().
+ *
+ * This data is read-only, and you should NOT attempt to free it. This
+ *  pointer remains valid until the shader is deleted.
+ */
+const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
+                                                MOJOSHADER_glShader *shader);
+/*
+ * Link a vertex and pixel shader into an OpenGL program.
+ *  (vshader) or (pshader) can be NULL, to specify that the GL should use the
+ *  fixed-function pipeline instead of the programmable pipeline for that
+ *  portion of the work. You can reuse shaders in various combinations across
+ *  multiple programs, by relinking different pairs.
+ *
+ * It is illegal to give a vertex shader for (pshader) or a pixel shader
+ *  for (vshader).
+ *
+ * Once you have successfully linked a program, you may render with it.
+ *
+ * Returns NULL on error, or a program handle on success.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Linked programs from this function may not be shared between contexts.
+ */
+MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
+                                               MOJOSHADER_glShader *pshader);
+
+/*
+ * This binds the program (using, for example, glUseProgramObjectARB()), and
+ *  disables all the client-side arrays so we can reset them with new values
+ *  if appropriate.
+ *
+ * Call with NULL to disable the programmable pipeline and all enabled
+ *  client-side arrays.
+ *
+ * After binding a program, you should update any uniforms you care about
+ *  with MOJOSHADER_glSetVertexShaderUniformF() (etc), set any vertex arrays
+ *  you want to use with MOJOSHADER_glSetVertexAttribute(), and finally call
+ *  MOJOSHADER_glProgramReady() to commit everything to the GL. Then you may
+ *  begin drawing through standard GL entry points.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);
+
+/*
+ * This binds individual shaders as if you had linked them with
+ *  MOJOSHADER_glLinkProgram(), and used MOJOSHADER_glBindProgram() on the
+ *  linked result.
+ *
+ * MojoShader will handle linking behind the scenes, and keep a cache of
+ *  programs linked here. Programs are removed from this cache when one of the
+ *  invidual shaders in it is deleted, otherwise they remain cached so future
+ *  calls to this function don't need to relink a previously-used shader
+ *  grouping.
+ *
+ * This function is for convenience, as the API is closer to how Direct3D
+ *  works, and retrofitting linking into your app can be difficult;
+ *  frequently, you just end up building your own cache, anyhow.
+ *
+ * Calling with all shaders set to NULL is equivalent to calling
+ *  MOJOSHADER_glBindProgram(NULL).
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+void MOJOSHADER_glBindShaders(MOJOSHADER_glShader *vshader,
+                              MOJOSHADER_glShader *pshader);
+
+/*
+ * Set a floating-point uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of 4-float "registers" shared by all vertex shaders.
+ *  This is the "c" register file in Direct3D (c0, c1, c2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * (idx) is the index into the internal array: 0 is the first four floats,
+ *  1 is the next four, etc.
+ * (data) is a pointer to (vec4count*4) floats.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
+                                          unsigned int vec4count);
+
+/*
+ * Retrieve a floating-point uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of 4-float "registers" shared by all vertex shaders.
+ *  This is the "c" register file in Direct3D (c0, c1, c2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * (idx) is the index into the internal array: 0 is the first four floats,
+ *  1 is the next four, etc.
+ * (data) is a pointer to space for (vec4count*4) floats.
+ *  (data) will be filled will current values in the register file. Results
+ *  are undefined if you request data past the end of the register file or
+ *  previously uninitialized registers.
+ *
+ * This is a "fast" call; we're just reading memory from internal memory. We
+ *  do not query the GPU or the GL for this information.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetVertexShaderUniformF(unsigned int idx, float *data,
+                                          unsigned int vec4count);
+
+
+/*
+ * Set an integer uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of 4-int "registers" shared by all vertex shaders.
+ *  This is the "i" register file in Direct3D (i0, i1, i2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * (idx) is the index into the internal array: 0 is the first four ints,
+ *  1 is the next four, etc.
+ * (data) is a pointer to (ivec4count*4) ints.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
+                                          unsigned int ivec4count);
+
+/*
+ * Retrieve an integer uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of 4-int "registers" shared by all vertex shaders.
+ *  This is the "i" register file in Direct3D (i0, i1, i2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * (idx) is the index into the internal array: 0 is the first four ints,
+ *  1 is the next four, etc.
+ * (data) is a pointer to space for (ivec4count*4) ints.
+ *  (data) will be filled will current values in the register file. Results
+ *  are undefined if you request data past the end of the register file or
+ *  previously uninitialized registers.
+ *
+ * This is a "fast" call; we're just reading memory from internal memory. We
+ *  do not query the GPU or the GL for this information.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetVertexShaderUniformI(unsigned int idx, int *data,
+                                          unsigned int ivec4count);
+
+/*
+ * Set a boolean uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of "registers" shared by all vertex shaders.
+ *  This is the "b" register file in Direct3D (b0, b1, b2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * Unlike the float and int counterparts, booleans are single values, not
+ *  four-element vectors...so idx==1 is the second boolean in the internal
+ *  array, not the fifth.
+ *
+ * Non-zero values are considered "true" and zero is considered "false".
+ *
+ * (idx) is the index into the internal array.
+ * (data) is a pointer to (bcount) ints.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
+                                          unsigned int bcount);
+
+/*
+ * Retrieve a boolean uniform value (what Direct3D calls a "constant").
+ *
+ * There is a single array of "registers" shared by all vertex shaders.
+ *  This is the "b" register file in Direct3D (b0, b1, b2, etc...)
+ *  MojoShader will take care of synchronizing this internal array with the
+ *  appropriate variables in the GL shaders.
+ *
+ * Unlike the float and int counterparts, booleans are single values, not
+ *  four-element vectors...so idx==1 is the second boolean in the internal
+ *  array, not the fifth.
+ *
+ * Non-zero values are considered "true" and zero is considered "false".
+ *  This function will always return true values as 1, regardless of what
+ *  non-zero integer you originally used to set the registers.
+ *
+ * (idx) is the index into the internal array.
+ * (data) is a pointer to space for (bcount) ints.
+ *  (data) will be filled will current values in the register file. Results
+ *  are undefined if you request data past the end of the register file or
+ *  previously uninitialized registers.
+ *
+ * This is a "fast" call; we're just reading memory from internal memory. We
+ *  do not query the GPU or the GL for this information.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetVertexShaderUniformB(unsigned int idx, int *data,
+                                          unsigned int bcount);
+
+/*
+ * The equivalent of MOJOSHADER_glSetVertexShaderUniformF() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
+                                         unsigned int vec4count);
+
+
+/*
+ * The equivalent of MOJOSHADER_glGetVertexShaderUniformF() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetPixelShaderUniformF(unsigned int idx, float *data,
+                                         unsigned int vec4count);
+
+
+/*
+ * The equivalent of MOJOSHADER_glSetVertexShaderUniformI() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
+                                         unsigned int ivec4count);
+
+
+/*
+ * The equivalent of MOJOSHADER_glGetVertexShaderUniformI() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetPixelShaderUniformI(unsigned int idx, int *data,
+                                         unsigned int ivec4count);
+
+/*
+ * The equivalent of MOJOSHADER_glSetVertexShaderUniformB() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
+                                         unsigned int bcount);
+
+/*
+ * The equivalent of MOJOSHADER_glGetVertexShaderUniformB() for pixel
+ *  shaders. Other than using a different internal array that is specific
+ *  to pixel shaders, this functions just like its vertex array equivalent.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Uniforms are not shared between contexts.
+ */
+void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data,
+                                         unsigned int bcount);
+
+/*
+ * Set up the vector for the TEXBEM opcode. Most apps can ignore this API.
+ *
+ * Shader Model 1.1 through 1.3 had an instruction for "fake bump mapping"
+ *  called TEXBEM. To use it, you had to set some sampler states,
+ *  D3DTSS_BUMPENVMATxx, which would be referenced by the opcode.
+ *
+ * This functionality was removed from Shader Model 1.4 and later, because
+ *  it was special-purpose and limited. The functionality could be built on
+ *  more general opcodes, and the sampler state could be supplied in a more
+ *  general uniform.
+ *
+ * However, to support this opcode, we supply a way to specify that sampler
+ *  state, and the OpenGL glue code does the right thing to pass that
+ *  information to the shader.
+ *
+ * This call maps to IDirect3DDevice::SetTextureStageState() with the
+ *  D3DTSS_BUMPENVMAT00, D3DTSS_BUMPENVMAT01, D3DTSS_BUMPENVMAT10,
+ *  D3DTSS_BUMPENVMAT11, D3DTSS_BUMPENVLSCALE, and D3DTSS_BUMPENVLOFFSET
+ *  targets. This is only useful for Shader Model < 1.4 pixel shaders, if
+ *  they use the TEXBEM or TEXBEML opcode. If you aren't sure, you don't need
+ *  this function.
+ *
+ * Like the rest of your uniforms, you must call MOJOSHADER_glProgramReady()
+ *  between setting new values and drawing with them.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * These values are not shared between contexts.
+ */
+void MOJOSHADER_glSetLegacyBumpMapEnv(unsigned int sampler, float mat00,
+                                      float mat01, float mat10, float mat11,
+                                      float lscale, float loffset);
+
+/*
+ * Connect a client-side array to the currently-bound program.
+ *
+ * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
+ *  be MOJOSHADER_USAGE_COLOR and 1.
+ *
+ * The caller should bind VBOs before this call and treat (ptr) as an offset,
+ *  if appropriate.
+ *
+ * MojoShader will figure out where to plug this stream into the
+ *  currently-bound program, and enable the appropriate client-side array.
+ *
+ * (size), (type), (normalized), (stride), and (ptr) correspond to
+ *  glVertexAttribPointer()'s parameters (in most cases, these get passed
+ *  unmolested to that very entry point during this function).
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ *
+ * Vertex attributes are not shared between contexts.
+ */
+ /* !!! FIXME: this should probably be "input" and not "attribute" */
+ /* !!! FIXME: or maybe "vertex array" or something. */
+void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
+                                     int index, unsigned int size,
+                                     MOJOSHADER_attributeType type,
+                                     int normalized, unsigned int stride,
+                                     const void *ptr);
+
+
+
+
+/* These below functions are temporary and will be removed from the API once
+    the real Effects API is written. Do not use! */
+void MOJOSHADER_glSetVertexPreshaderUniformF(unsigned int idx, const float *data,
+                                             unsigned int vec4n);
+void MOJOSHADER_glGetVertexPreshaderUniformF(unsigned int idx, float *data,
+                                             unsigned int vec4n);
+void MOJOSHADER_glSetPixelPreshaderUniformF(unsigned int idx, const float *data,
+                                            unsigned int vec4n);
+void MOJOSHADER_glGetPixelPreshaderUniformF(unsigned int idx, float *data,
+                                            unsigned int vec4n);
+/* These above functions are temporary and will be removed from the API once
+    the real Effects API is written. Do not use! */
+
+
+
+
+/*
+ * Inform MojoShader that it should commit any pending state to the GL. This
+ *  must be called after you bind a program and update any inputs, right
+ *  before you start drawing, so any outstanding changes made to the shared
+ *  constants array (etc) can propagate to the shader during this call.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+void MOJOSHADER_glProgramReady(void);
+
+/*
+ * Free the resources of a linked program. This will delete the GL object
+ *  and free memory.
+ *
+ * If the program is currently bound by MOJOSHADER_glBindProgram(), it will
+ *  be deleted as soon as it becomes unbound.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);
+
+/*
+ * Free the resources of a compiled shader. This will delete the GL object
+ *  and free memory.
+ *
+ * If the shader is currently referenced by a linked program (or is currently
+ *  bound with MOJOSHADER_glBindShaders()), it will be deleted as soon as all
+ *  referencing programs are deleted and it is no longer bound, too.
+ *
+ * This call is NOT thread safe! As most OpenGL implementations are not thread
+ *  safe, you should probably only call this from the same thread that created
+ *  the GL context.
+ *
+ * This call requires a valid MOJOSHADER_glContext to have been made current,
+ *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
+ */
+void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
+
+/*
+ * Deinitialize MojoShader's OpenGL shader management.
+ *
+ * You must call this once, while your GL context (not MojoShader context) is
+ *  still current, if you previously had a successful call to
+ *  MOJOSHADER_glCreateContext(). This should be the last MOJOSHADER_gl*
+ *  function you call until you've prepared a context again.
+ *
+ * This will clean up resources previously allocated, and may call into the GL.
+ *
+ * This will not clean up shaders and programs you created! Please call
+ *  MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
+ *  those up before calling this function!
+ *
+ * This function destroys the MOJOSHADER_glContext you pass it. If it's the
+ *  current context, then no context will be current upon return.
+ *
+ * This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
+ *  functions running when this is called. Also, as most OpenGL implementations
+ *  are not thread safe, you should probably only call this from the same
+ *  thread that created the GL context.
+ */
+void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* include-once blocker. */
+
+/* end of mojoshader.h ... */
+

+ 1770 - 0
ThirdParty/MojoShader/mojoshader_assembler.c

@@ -0,0 +1,1770 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+// !!! FIXME: this should probably use a formal grammar and not a hand-written
+// !!! FIXME:  pile of C code.
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+#if !SUPPORT_PROFILE_BYTECODE
+#error Shader assembler needs bytecode profile. Fix your build.
+#endif
+
+#if DEBUG_ASSEMBLER_PARSER
+    #define print_debug_token(token, len, val) \
+        MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
+#else
+    #define print_debug_token(token, len, val)
+#endif
+
+
+typedef struct SourcePos
+{
+    const char *filename;
+    uint32 line;
+} SourcePos;
+
+
+// Context...this is state that changes as we assemble a shader...
+typedef struct Context
+{
+    int isfail;
+    int out_of_memory;
+    MOJOSHADER_malloc malloc;
+    MOJOSHADER_free free;
+    void *malloc_data;
+    const char *current_file;
+    int current_position;
+    ErrorList *errors;
+    Preprocessor *preprocessor;
+    MOJOSHADER_shaderType shader_type;
+    uint8 major_ver;
+    uint8 minor_ver;
+    int pushedback;
+    const char *token;      // assembler token!
+    unsigned int tokenlen;  // assembler token!
+    Token tokenval;         // assembler token!
+    uint32 version_token;   // bytecode token!
+    uint32 tokenbuf[16];    // bytecode tokens!
+    int tokenbufpos;        // bytecode tokens!
+    DestArgInfo dest_arg;
+    Buffer *output;
+    Buffer *token_to_source;
+    Buffer *ctab;
+} Context;
+
+
+// !!! FIXME: cut and paste between every damned source file follows...
+// !!! FIXME: We need to make some sort of ContextBase that applies to all
+// !!! FIXME:  files and move this stuff to mojoshader_common.c ...
+
+// Convenience functions for allocators...
+
+static inline void out_of_memory(Context *ctx)
+{
+    ctx->isfail = ctx->out_of_memory = 1;
+} // out_of_memory
+
+static inline void *Malloc(Context *ctx, const size_t len)
+{
+    void *retval = ctx->malloc((int) len, ctx->malloc_data);
+    if (retval == NULL)
+        out_of_memory(ctx);
+    return retval;
+} // Malloc
+
+static inline char *StrDup(Context *ctx, const char *str)
+{
+    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
+    if (retval != NULL)
+        strcpy(retval, str);
+    return retval;
+} // StrDup
+
+static inline void Free(Context *ctx, void *ptr)
+{
+    ctx->free(ptr, ctx->malloc_data);
+} // Free
+
+static void *MallocBridge(int bytes, void *data)
+{
+    return Malloc((Context *) data, (size_t) bytes);
+} // MallocBridge
+
+static void FreeBridge(void *ptr, void *data)
+{
+    Free((Context *) data, ptr);
+} // FreeBridge
+
+
+static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void failf(Context *ctx, const char *fmt, ...)
+{
+    ctx->isfail = 1;
+    if (ctx->out_of_memory)
+        return;
+
+    va_list ap;
+    va_start(ap, fmt);
+    errorlist_add_va(ctx->errors, ctx->current_file, ctx->current_position, fmt, ap);
+    va_end(ap);
+} // failf
+
+static inline void fail(Context *ctx, const char *reason)
+{
+    failf(ctx, "%s", reason);
+} // fail
+
+static inline int isfail(const Context *ctx)
+{
+    return ctx->isfail;
+} // isfail
+
+
+// Shader model version magic...
+
+static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
+{
+    return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
+} // version_ui32
+
+static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
+                                         const uint8 min)
+{
+    return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
+} // shader_version_atleast
+
+static inline int shader_is_pixel(const Context *ctx)
+{
+    return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
+} // shader_is_pixel
+
+static inline int shader_is_vertex(const Context *ctx)
+{
+    return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
+} // shader_is_vertex
+
+static inline void pushback(Context *ctx)
+{
+    #if DEBUG_ASSEMBLER_PARSER
+    printf("ASSEMBLER PUSHBACK\n");
+    #endif
+    assert(!ctx->pushedback);
+    ctx->pushedback = 1;
+} // pushback
+
+
+static Token nexttoken(Context *ctx)
+{
+    if (ctx->pushedback)
+        ctx->pushedback = 0;
+    else
+    {
+        while (1)
+        {
+            ctx->token = preprocessor_nexttoken(ctx->preprocessor,
+                                                &ctx->tokenlen,
+                                                &ctx->tokenval);
+
+            if (preprocessor_outofmemory(ctx->preprocessor))
+            {
+                ctx->tokenval = TOKEN_EOI;
+                ctx->token = NULL;
+                ctx->tokenlen = 0;
+                break;
+            } // if
+
+            unsigned int line;
+            ctx->current_file = preprocessor_sourcepos(ctx->preprocessor,&line);
+            ctx->current_position = (int) line;
+
+            if (ctx->tokenval == TOKEN_BAD_CHARS)
+            {
+                fail(ctx, "Bad characters in source file");
+                continue;
+            } // else if
+
+            else if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR)
+            {
+                fail(ctx, ctx->token);
+                continue;
+            } // else if
+
+            break;
+        } // while
+    } // else
+
+    print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
+    return ctx->tokenval;
+} // nexttoken
+
+
+static void output_token_noswap(Context *ctx, const uint32 token)
+{
+    if (!isfail(ctx))
+    {
+        buffer_append(ctx->output, &token, sizeof (token));
+
+        // We only need a list of these that grows throughout processing, and
+        //  is flattened for reference at the end of the run, so we use a
+        //  Buffer. It's sneaky!
+        unsigned int pos = 0;
+        const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
+        SourcePos srcpos;
+        memset(&srcpos, '\0', sizeof (SourcePos));
+        srcpos.line = pos;
+        srcpos.filename = fname;  // cached in preprocessor!
+        buffer_append(ctx->token_to_source, &srcpos, sizeof (SourcePos));
+    } // if
+} // output_token_noswap
+
+
+static inline void output_token(Context *ctx, const uint32 token)
+{
+    output_token_noswap(ctx, SWAP32(token));
+} // output_token
+
+
+static void output_comment_bytes(Context *ctx, const uint8 *buf, size_t len)
+{
+    if (len > (0xFFFF * 4))  // length is stored as token count, in 16 bits.
+        fail(ctx, "Comment field is too big");
+    else if (!isfail(ctx))
+    {
+        const uint32 tokencount = (len / 4) + ((len % 4) ? 1 : 0);
+        output_token(ctx, 0xFFFE | (tokencount << 16));
+        while (len >= 4)
+        {
+            output_token_noswap(ctx, *((const uint32 *) buf));
+            len -= 4;
+            buf += 4;
+        } // while
+
+        if (len > 0)  // handle spillover...
+        {
+            union { uint8 ui8[4]; uint32 ui32; } overflow;
+            overflow.ui32 = 0;
+            memcpy(overflow.ui8, buf, len);
+            output_token_noswap(ctx, overflow.ui32);
+        } // if
+    } // else if
+} // output_comment_bytes
+
+
+static inline void output_comment_string(Context *ctx, const char *str)
+{
+    output_comment_bytes(ctx, (const uint8 *) str, strlen(str));
+} // output_comment_string
+
+
+static int require_comma(Context *ctx)
+{
+    const Token token = nexttoken(ctx);
+    if (token != ((Token) ','))
+    {
+        fail(ctx, "Comma expected");
+        return 0;
+    } // if
+    return 1;
+} // require_comma
+
+
+static int check_token_segment(Context *ctx, const char *str)
+{
+    // !!! FIXME: these are case-insensitive, right?
+    const size_t len = strlen(str);
+    if ( (ctx->tokenlen < len) || (strncasecmp(ctx->token, str, len) != 0) )
+        return 0;
+    ctx->token += len;
+    ctx->tokenlen -= len;
+    return 1;
+} // check_token_segment
+
+
+static int check_token(Context *ctx, const char *str)
+{
+    const size_t len = strlen(str);
+    if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
+        return 0;
+    ctx->token += len;
+    ctx->tokenlen = 0;
+    return 1;
+} // check_token
+
+
+static int ui32fromtoken(Context *ctx, uint32 *_val)
+{
+    unsigned int i;
+    for (i = 0; i < ctx->tokenlen; i++)
+    {
+        if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
+            break;
+    } // for
+
+    if (i == 0)
+    {
+        *_val = 0;
+        return 0;
+    } // if
+
+    const unsigned int len = i;
+    uint32 val = 0;
+    uint32 mult = 1;
+    while (i--)
+    {
+        val += ((uint32) (ctx->token[i] - '0')) * mult;
+        mult *= 10;
+    } // while
+
+    ctx->token += len;
+    ctx->tokenlen -= len;
+
+    *_val = val;
+    return 1;
+} // ui32fromtoken
+
+
+static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
+{
+    if (nexttoken(ctx) != TOKEN_IDENTIFIER)
+    {
+        fail(ctx, "Expected register");
+        return 0;
+    } // if
+
+    int neednum = 1;
+    int regnum = 0;
+    RegisterType regtype = REG_TYPE_TEMP;
+
+    // Watch out for substrings! oDepth must be checked before oD, since
+    //  the latter will match either case.
+    if (check_token_segment(ctx, "oDepth"))
+    {
+        regtype = REG_TYPE_DEPTHOUT;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "vFace"))
+    {
+        regtype = REG_TYPE_MISCTYPE;
+        regnum = (int) MISCTYPE_TYPE_FACE;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "vPos"))
+    {
+        regtype = REG_TYPE_MISCTYPE;
+        regnum = (int) MISCTYPE_TYPE_POSITION;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "oPos"))
+    {
+        regtype = REG_TYPE_RASTOUT;
+        regnum = (int) RASTOUT_TYPE_POSITION;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "oFog"))
+    {
+        regtype = REG_TYPE_RASTOUT;
+        regnum = (int) RASTOUT_TYPE_FOG;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "oPts"))
+    {
+        regtype = REG_TYPE_RASTOUT;
+        regnum = (int) RASTOUT_TYPE_POINT_SIZE;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "aL"))
+    {
+        regtype = REG_TYPE_LOOP;
+        neednum = 0;
+    } // else if
+    else if (check_token_segment(ctx, "oC"))
+        regtype = REG_TYPE_COLOROUT;
+    else if (check_token_segment(ctx, "oT"))
+        regtype = REG_TYPE_OUTPUT;
+    else if (check_token_segment(ctx, "oD"))
+        regtype = REG_TYPE_ATTROUT;
+    else if (check_token_segment(ctx, "r"))
+        regtype = REG_TYPE_TEMP;
+    else if (check_token_segment(ctx, "v"))
+        regtype = REG_TYPE_INPUT;
+    else if (check_token_segment(ctx, "c"))
+        regtype = REG_TYPE_CONST;
+    else if (check_token_segment(ctx, "i"))
+        regtype = REG_TYPE_CONSTINT;
+    else if (check_token_segment(ctx, "b"))
+        regtype = REG_TYPE_CONSTBOOL;
+    else if (check_token_segment(ctx, "s"))
+        regtype = REG_TYPE_SAMPLER;
+    else if (check_token_segment(ctx, "l"))
+        regtype = REG_TYPE_LABEL;
+    else if (check_token_segment(ctx, "p"))
+        regtype = REG_TYPE_PREDICATE;
+    else if (check_token_segment(ctx, "o"))
+        regtype = REG_TYPE_OUTPUT;
+    else if (check_token_segment(ctx, "a"))
+        regtype = REG_TYPE_ADDRESS;
+    else if (check_token_segment(ctx, "t"))
+        regtype = REG_TYPE_ADDRESS;
+        
+    //case REG_TYPE_TEMPFLOAT16:  // !!! FIXME: don't know this asm string
+
+    else
+    {
+        fail(ctx, "expected register type");
+        regtype = REG_TYPE_CONST;
+        regnum = 0;
+        neednum = 0;
+    } // else
+
+    // "c[5]" is the same as "c5", so if the token is done, see if next is '['.
+    if ((neednum) && (ctx->tokenlen == 0))
+    {
+        const int tlen = ctx->tokenlen;  // we need to protect this for later.
+        if (nexttoken(ctx) == ((Token) '['))
+            neednum = 0;  // don't need a number on register name itself.
+        pushback(ctx);
+        ctx->tokenlen = tlen;
+    } // if
+
+    if (neednum)
+    {
+        uint32 ui32 = 0;
+        if (!ui32fromtoken(ctx, &ui32))
+            fail(ctx, "Invalid register index");
+        regnum = (int) ui32;
+    } // if
+
+    // split up REG_TYPE_CONST
+    if (regtype == REG_TYPE_CONST)
+    {
+        if (regnum < 2048)
+        {
+            regtype = REG_TYPE_CONST;
+            regnum -= 0;
+        } // if
+        else if (regnum < 4096)
+        {
+            regtype = REG_TYPE_CONST2;
+            regnum -= 2048;
+        } // if
+        else if (regnum < 6144)
+        {
+            regtype = REG_TYPE_CONST3;
+            regnum -= 4096;
+        } // if
+        else if (regnum < 8192)
+        {
+            regtype = REG_TYPE_CONST4;
+            regnum -= 6144;
+        } // if
+        else
+        {
+            fail(ctx, "Invalid const register index");
+        } // else
+    } // if
+
+    *rtype = regtype;
+    *rnum = regnum;
+
+    return 1;
+} // parse_register_name
+
+
+static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
+{
+    if (info->result_shift != 0)
+        fail(ctx, "Multiple result shift modifiers");
+    info->result_shift = val;
+} // set_result_shift
+
+
+static inline int tokenbuf_overflow(Context *ctx)
+{
+    if ( ctx->tokenbufpos >= ((int) (STATICARRAYLEN(ctx->tokenbuf))) )
+    {
+        fail(ctx, "Too many tokens");
+        return 1;
+    } // if
+
+    return 0;
+} // tokenbuf_overflow
+
+
+static int parse_destination_token(Context *ctx)
+{
+    DestArgInfo *info = &ctx->dest_arg;
+    memset(info, '\0', sizeof (DestArgInfo));
+
+    // parse_instruction_token() sets ctx->token to the end of the instruction
+    //  so we can see if there are destination modifiers on the instruction
+    //  itself...
+
+    int invalid_modifier = 0;
+
+    while ((ctx->tokenlen > 0) && (!invalid_modifier))
+    {
+        if (check_token_segment(ctx, "_x2"))
+            set_result_shift(ctx, info, 0x1);
+        else if (check_token_segment(ctx, "_x4"))
+            set_result_shift(ctx, info, 0x2);
+        else if (check_token_segment(ctx, "_x8"))
+            set_result_shift(ctx, info, 0x3);
+        else if (check_token_segment(ctx, "_d8"))
+            set_result_shift(ctx, info, 0xD);
+        else if (check_token_segment(ctx, "_d4"))
+            set_result_shift(ctx, info, 0xE);
+        else if (check_token_segment(ctx, "_d2"))
+            set_result_shift(ctx, info, 0xF);
+        else if (check_token_segment(ctx, "_sat"))
+            info->result_mod |= MOD_SATURATE;
+        else if (check_token_segment(ctx, "_pp"))
+            info->result_mod |= MOD_PP;
+        else if (check_token_segment(ctx, "_centroid"))
+            info->result_mod |= MOD_CENTROID;
+        else
+            invalid_modifier = 1;
+    } // while
+
+    if (invalid_modifier)
+        fail(ctx, "Invalid destination modifier");
+
+    // !!! FIXME: predicates.
+    if (nexttoken(ctx) == ((Token) '('))
+        fail(ctx, "Predicates unsupported at this time");  // !!! FIXME: ...
+
+    pushback(ctx);  // parse_register_name calls nexttoken().
+
+    parse_register_name(ctx, &info->regtype, &info->regnum);
+    // parse_register_name() can't check this: dest regs might have modifiers.
+    if (ctx->tokenlen > 0)
+        fail(ctx, "invalid register name");
+
+    // !!! FIXME: can dest registers do relative addressing?
+
+    int invalid_writemask = 0;
+    int implicit_writemask = 0;
+    if (nexttoken(ctx) != ((Token) '.'))
+    {
+        implicit_writemask = 1;
+        info->writemask = 0xF;
+        info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
+        pushback(ctx);  // no explicit writemask; do full mask.
+    } // if
+
+    // !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
+    //else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
+    else if ( (scalar_register(ctx->shader_type, info->regtype, info->regnum)) && (info->regtype != REG_TYPE_DEPTHOUT) )
+        fail(ctx, "Writemask specified for scalar register");
+    else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
+        invalid_writemask = 1;
+    else
+    {
+        char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
+        const unsigned int tokenlen = ctx->tokenlen;
+        memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
+        char *ptr = tokenbytes;
+
+        if ((*ptr == 'r') || (*ptr == 'x')) { info->writemask0 = 1; ptr++; }
+        if ((*ptr == 'g') || (*ptr == 'y')) { info->writemask1 = 1; ptr++; }
+        if ((*ptr == 'b') || (*ptr == 'z')) { info->writemask2 = 1; ptr++; }
+        if ((*ptr == 'a') || (*ptr == 'w')) { info->writemask3 = 1; ptr++; }
+
+        if (*ptr != '\0')
+            invalid_writemask = 1;
+
+        info->writemask = ( ((info->writemask0 & 0x1) << 0) |
+                            ((info->writemask1 & 0x1) << 1) |
+                            ((info->writemask2 & 0x1) << 2) |
+                            ((info->writemask3 & 0x1) << 3) );
+    } // else
+
+    if (invalid_writemask)
+        fail(ctx, "Invalid writemask");
+
+    // !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
+    if (info->regtype == REG_TYPE_DEPTHOUT)
+    {
+        if ( (!implicit_writemask) && ((info->writemask0 + info->writemask1 +
+               info->writemask2 + info->writemask3) > 1) )
+            fail(ctx, "Writemask specified for scalar register");
+    } // if
+
+    info->orig_writemask = info->writemask;
+
+    if (tokenbuf_overflow(ctx))
+        return 1;
+
+    ctx->tokenbuf[ctx->tokenbufpos++] =
+            ( ((((uint32) 1)) << 31) |
+              ((((uint32) info->regnum) & 0x7ff) << 0) |
+              ((((uint32) info->relative) & 0x1) << 13) |
+              ((((uint32) info->result_mod) & 0xF) << 20) |
+              ((((uint32) info->result_shift) & 0xF) << 24) |
+              ((((uint32) info->writemask) & 0xF) << 16) |
+              ((((uint32) info->regtype) & 0x7) << 28) |
+              ((((uint32) info->regtype) & 0x18) << 8) );
+
+    return 1;
+} // parse_destination_token
+
+
+static void set_source_mod(Context *ctx, const int negate,
+                           const SourceMod norm, const SourceMod negated,
+                           SourceMod *srcmod)
+{
+    if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
+        fail(ctx, "Incompatible source modifiers");
+    else
+        *srcmod = ((negate) ? negated : norm);
+} // set_source_mod
+
+
+static int parse_source_token_maybe_relative(Context *ctx, const int relok)
+{
+    int retval = 1;
+
+    if (tokenbuf_overflow(ctx))
+        return 0;
+
+    // mark this now, so optional relative addressing token is placed second.
+    uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
+    *outtoken = 0;
+
+    SourceMod srcmod = SRCMOD_NONE;
+    int negate = 0;
+    Token token = nexttoken(ctx);
+
+    if (token == ((Token) '!'))
+        srcmod = SRCMOD_NOT;
+    else if (token == ((Token) '-'))
+        negate = 1;
+    else if ( (token == TOKEN_INT_LITERAL) && (check_token(ctx, "1")) )
+    {
+        if (nexttoken(ctx) != ((Token) '-'))
+            fail(ctx, "Unexpected token");
+        else
+            srcmod = SRCMOD_COMPLEMENT;
+    } // else
+    else
+    {
+        pushback(ctx);
+    } // else
+
+    RegisterType regtype;
+    int regnum;
+    parse_register_name(ctx, &regtype, &regnum);
+
+    if (ctx->tokenlen == 0)
+    {
+        if (negate)
+            set_source_mod(ctx, negate, SRCMOD_NONE, SRCMOD_NEGATE, &srcmod);
+    } // if
+    else
+    {
+        assert(ctx->tokenlen > 0);
+        if (check_token_segment(ctx, "_bias"))
+            set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
+        else if (check_token_segment(ctx, "_bx2"))
+            set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
+        else if (check_token_segment(ctx, "_x2"))
+            set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
+        else if (check_token_segment(ctx, "_dz"))
+            set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
+        else if (check_token_segment(ctx, "_db"))
+            set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
+        else if (check_token_segment(ctx, "_dw"))
+            set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
+        else if (check_token_segment(ctx, "_da"))
+            set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
+        else if (check_token_segment(ctx, "_abs"))
+            set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
+        else
+            fail(ctx, "Invalid source modifier");
+    } // else
+
+    uint32 relative = 0;
+    if (nexttoken(ctx) != ((Token) '['))
+        pushback(ctx);  // not relative addressing?
+    else
+    {
+        if (!relok)
+            fail(ctx, "Relative addressing not permitted here.");
+        else
+            retval++;
+
+        parse_source_token_maybe_relative(ctx, 0);
+        relative = 1;
+
+        if (nexttoken(ctx) != ((Token) '+'))
+            pushback(ctx);
+        else
+        {
+            // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
+            if (regnum != 0)
+                fail(ctx, "Relative addressing with explicit register number.");
+
+            uint32 ui32 = 0;
+            if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
+                 (!ui32fromtoken(ctx, &ui32)) ||
+                 (ctx->tokenlen != 0) )
+            {
+                fail(ctx, "Invalid relative addressing offset");
+            } // if
+            regnum += (int) ui32;
+        } // else
+
+        if (nexttoken(ctx) != ((Token) ']'))
+            fail(ctx, "Expected ']'");
+    } // else
+
+    int invalid_swizzle = 0;
+    uint32 swizzle = 0;
+    if (nexttoken(ctx) != ((Token) '.'))
+    {
+        swizzle = 0xE4;  // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
+        pushback(ctx);  // no explicit writemask; do full mask.
+    } // if
+    else if (scalar_register(ctx->shader_type, regtype, regnum))
+        fail(ctx, "Swizzle specified for scalar register");
+    else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
+        invalid_swizzle = 1;
+    else
+    {
+        char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
+        const unsigned int tokenlen = ctx->tokenlen;
+        memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
+
+        // deal with shortened form (.x = .xxxx, etc).
+        if (tokenlen == 1)
+            tokenbytes[1] = tokenbytes[2] = tokenbytes[3] = tokenbytes[0];
+        else if (tokenlen == 2)
+            tokenbytes[2] = tokenbytes[3] = tokenbytes[1];
+        else if (tokenlen == 3)
+            tokenbytes[3] = tokenbytes[2];
+        else if (tokenlen != 4)
+            invalid_swizzle = 1;
+        tokenbytes[4] = '\0';
+
+        uint32 val = 0;
+        int i;
+        for (i = 0; i < 4; i++)
+        {
+            const int component = (int) tokenbytes[i];
+            switch (component)
+            {
+                case 'r': case 'x': val = 0; break;
+                case 'g': case 'y': val = 1; break;
+                case 'b': case 'z': val = 2; break;
+                case 'a': case 'w': val = 3; break;
+                default: invalid_swizzle = 1; break;
+            } // switch
+            swizzle |= (val << (i * 2));
+        } // for
+    } // else
+
+    if (invalid_swizzle)
+        fail(ctx, "Invalid swizzle");
+
+    *outtoken = ( ((((uint32) 1)) << 31) |
+                  ((((uint32) regnum) & 0x7ff) << 0) |
+                  ((((uint32) relative) & 0x1) << 13) |
+                  ((((uint32) swizzle) & 0xFF) << 16) |
+                  ((((uint32) srcmod) & 0xF) << 24) |
+                  ((((uint32) regtype) & 0x7) << 28) |
+                  ((((uint32) regtype) & 0x18) << 8) );
+
+    return retval;
+} // parse_source_token_maybe_relative
+
+
+static inline int parse_source_token(Context *ctx)
+{
+    return parse_source_token_maybe_relative(ctx, 1);
+} // parse_source_token
+
+
+static int parse_args_NULL(Context *ctx)
+{
+    return 1;
+} // parse_args_NULL
+
+
+static int parse_num(Context *ctx, const int floatok, uint32 *value)
+{
+    union { float f; int32 si32; uint32 ui32; } cvt;
+    int negative = 0;
+    Token token = nexttoken(ctx);
+
+    if (token == ((Token) '-'))
+    {
+        negative = 1;
+        token = nexttoken(ctx);
+    } // if
+
+    if (token == TOKEN_INT_LITERAL)
+    {
+        int d = 0;
+        sscanf(ctx->token, "%d", &d);
+        if (floatok)
+            cvt.f = (float) ((negative) ? -d : d);
+        else
+            cvt.si32 = (int32) ((negative) ? -d : d);
+    } // if
+    else if (token == TOKEN_FLOAT_LITERAL)
+    {
+        if (!floatok)
+        {
+            fail(ctx, "Expected whole number");
+            *value = 0;
+            return 0;
+        } // if
+        sscanf(ctx->token, "%f", &cvt.f);
+        if (negative)
+            cvt.f = -cvt.f;
+    } // if
+    else
+    {
+        fail(ctx, "Expected number");
+        *value = 0;
+        return 0;
+    } // else
+
+    *value = cvt.ui32;
+    return 1;
+} // parse_num
+
+
+static int parse_args_DEFx(Context *ctx, const int isflt)
+{
+    parse_destination_token(ctx);
+    require_comma(ctx);
+    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
+    require_comma(ctx);
+    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
+    require_comma(ctx);
+    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
+    require_comma(ctx);
+    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
+    return 6;
+} // parse_args_DEFx
+
+
+static int parse_args_DEF(Context *ctx)
+{
+    return parse_args_DEFx(ctx, 1);
+} // parse_args_DEF
+
+
+static int parse_args_DEFI(Context *ctx)
+{
+    return parse_args_DEFx(ctx, 0);
+} // parse_args_DEFI
+
+
+static int parse_args_DEFB(Context *ctx)
+{
+    parse_destination_token(ctx);
+    require_comma(ctx);
+
+    // !!! FIXME: do a TOKEN_TRUE and TOKEN_FALSE? Is this case-sensitive?
+    const Token token = nexttoken(ctx);
+
+    int bad = 0;
+    if (token != TOKEN_IDENTIFIER)
+        bad = 1;
+    else if (check_token_segment(ctx, "true"))
+        ctx->tokenbuf[ctx->tokenbufpos++] = 1;
+    else if (check_token_segment(ctx, "false"))
+        ctx->tokenbuf[ctx->tokenbufpos++] = 0;
+    else
+        bad = 1;
+
+    if (ctx->tokenlen != 0)
+        bad = 1;
+
+    if (bad)
+        fail(ctx, "Expected 'true' or 'false'");
+
+    return 3;
+} // parse_args_DEFB
+
+
+static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
+{
+    size_t i;
+    static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
+    static const char *usagestrs[] = {
+        "_position", "_blendweight", "_blendindices", "_normal", "_psize",
+        "_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
+        "_color", "_fog", "_depth", "_sample"
+    };
+
+    for (i = 0; i < STATICARRAYLEN(usagestrs); i++)
+    {
+        if (check_token_segment(ctx, usagestrs[i]))
+        {
+            *issampler = 0;
+            *val = i;
+            return 1;
+        } // if
+    } // for
+
+    for (i = 0; i < STATICARRAYLEN(samplerusagestrs); i++)
+    {
+        if (check_token_segment(ctx, samplerusagestrs[i]))
+        {
+            *issampler = 1;
+            *val = i + 2;
+            return 1;
+        } // if
+    } // for
+
+    *issampler = 0;
+    *val = 0;
+    return 0;
+} // parse_dcl_usage
+
+
+static int parse_args_DCL(Context *ctx)
+{
+    int issampler = 0;
+    uint32 usage = 0;
+    uint32 index = 0;
+
+    ctx->tokenbufpos++;  // save a spot for the usage/index token.
+    ctx->tokenbuf[0] = 0;
+
+    // parse_instruction_token() sets ctx->token to the end of the instruction
+    //  so we can see if there are destination modifiers on the instruction
+    //  itself...
+
+    if (parse_dcl_usage(ctx, &usage, &issampler))
+    {
+        if ((ctx->tokenlen > 0) && (*ctx->token != '_'))
+        {
+            if (!ui32fromtoken(ctx, &index))
+                fail(ctx, "Expected usage index");
+        } // if
+    } // if
+
+    parse_destination_token(ctx);
+
+    const int samplerreg = (ctx->dest_arg.regtype == REG_TYPE_SAMPLER);
+    if (issampler != samplerreg)
+        fail(ctx, "Invalid usage");
+    else if (samplerreg)
+        ctx->tokenbuf[0] = (usage << 27) | 0x80000000;
+    else
+        ctx->tokenbuf[0] = usage | (index << 16) | 0x80000000;
+
+    return 3;
+} // parse_args_DCL
+
+
+static int parse_args_D(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx);
+    return retval;
+} // parse_args_D
+
+
+static int parse_args_S(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_S
+
+
+static int parse_args_SS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_SS
+
+
+static int parse_args_DS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_DS
+
+
+static int parse_args_DSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_DSS
+
+
+static int parse_args_DSSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_DSSS
+
+
+static int parse_args_DSSSS(Context *ctx)
+{
+    int retval = 1;
+    retval += parse_destination_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    require_comma(ctx);
+    retval += parse_source_token(ctx);
+    return retval;
+} // parse_args_DSSSS
+
+
+static int parse_args_SINCOS(Context *ctx)
+{
+    // this opcode needs extra registers for sm2 and lower.
+    if (!shader_version_atleast(ctx, 3, 0))
+        return parse_args_DSSS(ctx);
+    return parse_args_DS(ctx);
+} // parse_args_SINCOS
+
+
+static int parse_args_TEXCRD(Context *ctx)
+{
+    // added extra register in ps_1_4.
+    if (shader_version_atleast(ctx, 1, 4))
+        return parse_args_DS(ctx);
+    return parse_args_D(ctx);
+} // parse_args_TEXCRD
+
+
+static int parse_args_TEXLD(Context *ctx)
+{
+    // different registers in px_1_3, ps_1_4, and ps_2_0!
+    if (shader_version_atleast(ctx, 2, 0))
+        return parse_args_DSS(ctx);
+    else if (shader_version_atleast(ctx, 1, 4))
+        return parse_args_DS(ctx);
+    return parse_args_D(ctx);
+} // parse_args_TEXLD
+
+
+
+// one args function for each possible sequence of opcode arguments.
+typedef int (*args_function)(Context *ctx);
+
+// Lookup table for instruction opcodes...
+typedef struct
+{
+    const char *opcode_string;
+    args_function parse_args;
+} Instruction;
+
+
+static const Instruction instructions[] =
+{
+    #define INSTRUCTION_STATE(op, opstr, s, a, t) { opstr, parse_args_##a },
+    #define INSTRUCTION(op, opstr, slots, a, t) { opstr, parse_args_##a },
+    #define MOJOSHADER_DO_INSTRUCTION_TABLE 1
+    #include "mojoshader_internal.h"
+    #undef MOJOSHADER_DO_INSTRUCTION_TABLE
+    #undef INSTRUCTION
+    #undef INSTRUCTION_STATE
+};
+
+
+static int parse_condition(Context *ctx, uint32 *controls)
+{
+    static const char *comps[] = { "_gt", "_eq", "_ge", "_lt", "_ne", "_le" };
+    size_t i;
+
+    if (ctx->tokenlen >= 3)
+    {
+        for (i = 0; i < STATICARRAYLEN(comps); i++)
+        {
+            if (check_token_segment(ctx, comps[i]))
+            {
+                *controls = (uint32) (i + 1);
+                return 1;
+            } // if
+        } // for
+    } // if
+
+    return 0;
+} // parse_condition
+
+
+static int parse_instruction_token(Context *ctx, Token token)
+{
+    int coissue = 0;
+    int predicated = 0;
+
+    if (token == ((Token) '+'))
+    {
+        coissue = 1;
+        token = nexttoken(ctx);
+    } // if
+
+    if (token != TOKEN_IDENTIFIER)
+    {
+        fail(ctx, "Expected instruction");
+        return 0;
+    } // if
+
+    uint32 controls = 0;
+    uint32 opcode = OPCODE_TEXLD;
+    const char *origtoken = ctx->token;
+    const unsigned int origtokenlen = ctx->tokenlen;
+
+    // This might need to be TEXLD instead of TEXLDP.
+    if (check_token_segment(ctx, "TEXLDP"))
+        controls = CONTROL_TEXLDP;
+
+    // This might need to be TEXLD instead of TEXLDB.
+    else if (check_token_segment(ctx, "TEXLDB"))
+        controls = CONTROL_TEXLDB;
+
+    else  // find the instruction.
+    {
+        size_t i;
+        for (i = 0; i < STATICARRAYLEN(instructions); i++)
+        {
+            const char *opcode_string = instructions[i].opcode_string;
+            if (opcode_string == NULL)
+                continue;  // skip this.
+            else if (!check_token_segment(ctx, opcode_string))
+                continue;  // not us.
+            else if ((ctx->tokenlen > 0) && (*ctx->token != '_'))
+            {
+                ctx->token = origtoken;
+                ctx->tokenlen = origtokenlen;
+                continue;  // not the match: TEXLD when we wanted TEXLDL, etc.
+            } // if
+
+            break;  // found it!
+        } // for
+
+        opcode = (uint32) i;
+
+        // This might need to be IFC instead of IF.
+        if (opcode == OPCODE_IF)
+        {
+            if (parse_condition(ctx, &controls))
+                opcode = OPCODE_IFC;
+        } // if
+
+        // This might need to be BREAKC instead of BREAK.
+        else if (opcode == OPCODE_BREAK)
+        {
+            if (parse_condition(ctx, &controls))
+                opcode = OPCODE_BREAKC;
+        } // else if
+
+        // SETP has a conditional code, always.
+        else if (opcode == OPCODE_SETP)
+        {
+            if (!parse_condition(ctx, &controls))
+                fail(ctx, "SETP requires a condition");
+        } // else if
+    } // else
+
+    if ( (opcode == STATICARRAYLEN(instructions)) ||
+         ((ctx->tokenlen > 0) && (ctx->token[0] != '_')) )
+    {
+        char opstr[32];
+        const int len = Min(sizeof (opstr) - 1, origtokenlen);
+        memcpy(opstr, origtoken, len);
+        opstr[len] = '\0';
+        failf(ctx, "Unknown instruction '%s'", opstr);
+        return 0;
+    } // if
+
+    const Instruction *instruction = &instructions[opcode];
+
+    // !!! FIXME: predicated instructions
+
+    ctx->tokenbufpos = 0;
+
+    const int tokcount = instruction->parse_args(ctx);
+
+    // insttoks bits are reserved and should be zero if < SM2.
+    const uint32 insttoks = shader_version_atleast(ctx, 2, 0) ? tokcount-1 : 0;
+
+    // write out the instruction token.
+    output_token(ctx, ((opcode & 0xFFFF) << 0) |
+                      ((controls & 0xFF) << 16) |
+                      ((insttoks & 0xF) << 24) |
+                      ((coissue) ? 0x40000000 : 0x00000000) |
+                      ((predicated) ? 0x10000000 : 0x00000000) );
+
+    // write out the argument tokens.
+    int i;
+    for (i = 0; i < (tokcount-1); i++)
+        output_token(ctx, ctx->tokenbuf[i]);
+
+    return 1;
+} // parse_instruction_token
+
+
+static void parse_version_token(Context *ctx)
+{
+    int bad = 0;
+    int dot_form = 0;
+    uint32 shader_type = 0;
+
+    if (nexttoken(ctx) != TOKEN_IDENTIFIER)
+        bad = 1;
+    else if (check_token_segment(ctx, "vs"))
+    {
+        ctx->shader_type = MOJOSHADER_TYPE_VERTEX;
+        shader_type = 0xFFFE;
+    } // if
+    else if (check_token_segment(ctx, "ps"))
+    {
+        ctx->shader_type = MOJOSHADER_TYPE_PIXEL;
+        shader_type = 0xFFFF;
+    } // if
+    else
+    {
+        // !!! FIXME: geometry shaders?
+        bad = 1;
+    } // else
+
+    dot_form = ((!bad) && (ctx->tokenlen == 0));  // it's in xs.x.x form?
+
+    uint32 major = 0;
+    uint32 minor = 0;
+
+    if (dot_form)
+    {
+        Token t = TOKEN_UNKNOWN;
+
+        if (!bad)
+        {
+            t = nexttoken(ctx);
+            // stupid lexer sees "vs.2.0" and makes the ".2" into a float.
+            if (t == ((Token) '.'))
+                t = nexttoken(ctx);
+            else
+            {
+                if ((t != TOKEN_FLOAT_LITERAL) || (ctx->token[0] != '.'))
+                    bad = 1;
+                else
+                {
+                    ctx->tokenval = t = TOKEN_INT_LITERAL;
+                    ctx->token++;
+                    ctx->tokenlen--;
+                } // else
+            } // else
+        } // if
+
+        if (!bad)
+        {
+            if (t != TOKEN_INT_LITERAL)
+                bad = 1;
+            else if (!ui32fromtoken(ctx, &major))
+                bad = 1;
+        } // if
+
+        if (!bad)
+        {
+            t = nexttoken(ctx);
+            // stupid lexer sees "vs.2.0" and makes the ".2" into a float.
+            if (t == ((Token) '.'))
+                t = nexttoken(ctx);
+            else
+            {
+                if ((t != TOKEN_FLOAT_LITERAL) || (ctx->token[0] != '.'))
+                    bad = 1;
+                else
+                {
+                    ctx->tokenval = t = TOKEN_INT_LITERAL;
+                    ctx->token++;
+                    ctx->tokenlen--;
+                } // else
+            } // else
+        } // if
+
+        if (!bad)
+        {
+            if ((t == TOKEN_INT_LITERAL) && (ui32fromtoken(ctx, &minor)))
+                ;  // good to go.
+            else if ((t == TOKEN_IDENTIFIER) && (check_token_segment(ctx, "x")))
+                minor = 1;
+            else if ((t == TOKEN_IDENTIFIER) && (check_token_segment(ctx, "sw")))
+                minor = 255;
+            else
+                bad = 1;
+        } // if
+    } // if
+    else
+    {
+        if (!check_token_segment(ctx, "_"))
+            bad = 1;
+        else if (!ui32fromtoken(ctx, &major))
+            bad = 1;
+        else if (!check_token_segment(ctx, "_"))
+            bad = 1;
+        else if (check_token_segment(ctx, "x"))
+            minor = 1;
+        else if (check_token_segment(ctx, "sw"))
+            minor = 255;
+        else if (!ui32fromtoken(ctx, &minor))
+            bad = 1;
+    } // else
+
+    if ((!bad) && (ctx->tokenlen != 0))
+        bad = 1;
+
+    if (bad)
+        fail(ctx, "Expected valid version string");
+
+    ctx->major_ver = major;
+    ctx->minor_ver = minor;
+
+    ctx->version_token = (shader_type << 16) | (major << 8) | (minor << 0);
+    output_token(ctx, ctx->version_token);
+} // parse_version_token
+
+
+static void parse_phase_token(Context *ctx)
+{
+    output_token(ctx, 0x0000FFFD); // phase token always 0x0000FFFD.
+} // parse_phase_token
+
+
+static void parse_end_token(Context *ctx)
+{
+    // We don't emit the end token bits here, since it's valid for a shader
+    //  to not specify an "end" string at all; it's implicit, in that case.
+    // Instead, we make sure if we see "end" that it's the last thing we see.
+    if (nexttoken(ctx) != TOKEN_EOI)
+        fail(ctx, "Content after END");
+} // parse_end_token
+
+
+static void parse_token(Context *ctx, const Token token)
+{
+    if (token != TOKEN_IDENTIFIER)
+        parse_instruction_token(ctx, token);  // might be a coissue '+', etc.
+    else
+    {
+        if (check_token(ctx, "end"))
+            parse_end_token(ctx);
+        else if (check_token(ctx, "phase"))
+            parse_phase_token(ctx);
+        else
+            parse_instruction_token(ctx, token);
+    } // if
+} // parse_token
+
+
+static void destroy_context(Context *ctx)
+{
+    if (ctx != NULL)
+    {
+        MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
+        void *d = ctx->malloc_data;
+        preprocessor_end(ctx->preprocessor);
+        errorlist_destroy(ctx->errors);
+        buffer_destroy(ctx->ctab);
+        buffer_destroy(ctx->token_to_source);
+        buffer_destroy(ctx->output);
+        f(ctx, d);
+    } // if
+} // destroy_context
+
+
+static Context *build_context(const char *filename,
+                              const char *source, unsigned int sourcelen,
+                              const MOJOSHADER_preprocessorDefine *defines,
+                              unsigned int define_count,
+                              MOJOSHADER_includeOpen include_open,
+                              MOJOSHADER_includeClose include_close,
+                              MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    if (!m) m = MOJOSHADER_internal_malloc;
+    if (!f) f = MOJOSHADER_internal_free;
+    if (!include_open) include_open = MOJOSHADER_internal_include_open;
+    if (!include_close) include_close = MOJOSHADER_internal_include_close;
+
+    Context *ctx = (Context *) m(sizeof (Context), d);
+    if (ctx == NULL)
+        return NULL;
+
+    memset(ctx, '\0', sizeof (Context));
+    ctx->malloc = m;
+    ctx->free = f;
+    ctx->malloc_data = d;
+    ctx->current_position = MOJOSHADER_POSITION_BEFORE;
+
+    const size_t outblk = sizeof (uint32) * 4 * 64; // 64 4-token instrs.
+    ctx->output = buffer_create(outblk, MallocBridge, FreeBridge, ctx);
+    if (ctx->output == NULL)
+        goto build_context_failed;
+
+    const size_t mapblk = sizeof (SourcePos) * 4 * 64; // 64 * 4-tokens.
+    ctx->token_to_source = buffer_create(mapblk, MallocBridge, FreeBridge, ctx);
+    if (ctx->token_to_source == NULL)
+        goto build_context_failed;
+
+    ctx->errors = errorlist_create(MallocBridge, FreeBridge, ctx);
+    if (ctx->errors == NULL)
+        goto build_context_failed;
+
+    ctx->preprocessor = preprocessor_start(filename, source, sourcelen,
+                                           include_open, include_close,
+                                           defines, define_count, 1,
+                                           MallocBridge, FreeBridge, ctx);
+
+    if (ctx->preprocessor == NULL)
+        goto build_context_failed;
+
+    return ctx;
+
+build_context_failed:  // ctx is allocated and zeroed before this is called.
+    destroy_context(ctx);
+    return NULL;
+} // build_context
+
+
+static const MOJOSHADER_parseData *build_failed_assembly(Context *ctx)
+{
+    assert(isfail(ctx));
+
+    if (ctx->out_of_memory)
+        return &MOJOSHADER_out_of_mem_data;
+        
+    MOJOSHADER_parseData *retval = NULL;
+    retval = (MOJOSHADER_parseData*) Malloc(ctx, sizeof(MOJOSHADER_parseData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_parseData));
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+
+    retval->error_count = errorlist_count(ctx->errors);
+    retval->errors = errorlist_flatten(ctx->errors);
+
+    if (ctx->out_of_memory)
+    {
+        Free(ctx, retval->errors);
+        Free(ctx, retval);
+        return &MOJOSHADER_out_of_mem_data;
+    } // if
+
+    return retval;
+} // build_failed_assembly
+
+
+static uint32 add_ctab_bytes(Context *ctx, const uint8 *bytes, const size_t len)
+{
+    if (isfail(ctx))
+        return 0;
+
+    const size_t extra = CTAB_SIZE + sizeof (uint32);
+    const ssize_t pos = buffer_find(ctx->ctab, extra, bytes, len);
+    if (pos >= 0)  // blob is already in here.
+        return ((uint32) pos) - sizeof (uint32);
+
+    // add it to the byte pile...
+    const uint32 retval = ((uint32) buffer_size(ctx->ctab)) - sizeof (uint32);
+    buffer_append(ctx->ctab, bytes, len);
+    return retval;
+} // add_ctab_bytes
+
+
+static inline uint32 add_ctab_string(Context *ctx, const char *str)
+{
+    return add_ctab_bytes(ctx, (const uint8 *) str, strlen(str) + 1);
+} // add_ctab_string
+
+
+static uint32 add_ctab_typeinfo(Context *ctx, const MOJOSHADER_symbolTypeInfo *info);
+
+static uint32 add_ctab_members(Context *ctx, const MOJOSHADER_symbolTypeInfo *info)
+{
+    unsigned int i;
+    const size_t len = info->member_count * CMEMBERINFO_SIZE;
+    uint8 *bytes = (uint8 *) Malloc(ctx, len);
+    if (bytes == NULL)
+        return 0;
+
+    union { uint8 *ui8; uint16 *ui16; uint32 *ui32; } ptr;
+    ptr.ui8 = bytes;
+    for (i = 0; i < info->member_count; i++)
+    {
+        const MOJOSHADER_symbolStructMember *member = &info->members[i];
+        *(ptr.ui32++) = SWAP32(add_ctab_string(ctx, member->name));
+        *(ptr.ui32++) = SWAP32(add_ctab_typeinfo(ctx, &member->info));
+    } // for
+
+    const uint32 retval = add_ctab_bytes(ctx, bytes, len);
+    Free(ctx, bytes);
+    return retval;
+} // add_ctab_members
+
+
+static uint32 add_ctab_typeinfo(Context *ctx, const MOJOSHADER_symbolTypeInfo *info)
+{
+    uint8 bytes[CTYPEINFO_SIZE];
+    union { uint8 *ui8; uint16 *ui16; uint32 *ui32; } ptr;
+    ptr.ui8 = bytes;
+
+    *(ptr.ui16++) = SWAP16((uint16) info->parameter_class);
+    *(ptr.ui16++) = SWAP16((uint16) info->parameter_type);
+    *(ptr.ui16++) = SWAP16((uint16) info->rows);
+    *(ptr.ui16++) = SWAP16((uint16) info->columns);
+    *(ptr.ui16++) = SWAP16((uint16) info->elements);
+    *(ptr.ui16++) = SWAP16((uint16) info->member_count);
+    *(ptr.ui32++) = SWAP32(add_ctab_members(ctx, info));
+
+    return add_ctab_bytes(ctx, bytes, sizeof (bytes));
+} // add_ctab_typeinfo
+
+
+static uint32 add_ctab_info(Context *ctx, const MOJOSHADER_symbol *symbols,
+                            const unsigned int symbol_count)
+{
+    unsigned int i;
+    const size_t len = symbol_count * CINFO_SIZE;
+    uint8 *bytes = (uint8 *) Malloc(ctx, len);
+    if (bytes == NULL)
+        return 0;
+
+    union { uint8 *ui8; uint16 *ui16; uint32 *ui32; } ptr;
+    ptr.ui8 = bytes;
+    for (i = 0; i < symbol_count; i++)
+    {
+        const MOJOSHADER_symbol *sym = &symbols[i];
+        *(ptr.ui32++) = SWAP32(add_ctab_string(ctx, sym->name));
+        *(ptr.ui16++) = SWAP16((uint16) sym->register_set);
+        *(ptr.ui16++) = SWAP16((uint16) sym->register_index);
+        *(ptr.ui16++) = SWAP16((uint16) sym->register_count);
+        *(ptr.ui16++) = SWAP16(0);  // reserved
+        *(ptr.ui32++) = SWAP32(add_ctab_typeinfo(ctx, &sym->info));
+        *(ptr.ui32++) = SWAP32(0);  // !!! FIXME: default value.
+    } // for
+
+    const uint32 retval = add_ctab_bytes(ctx, bytes, len);
+    Free(ctx, bytes);
+    return retval;
+} // add_ctab_info
+
+
+static void output_ctab(Context *ctx, const MOJOSHADER_symbol *symbols,
+                        unsigned int symbol_count, const char *creator)
+{
+    const size_t tablelen = CTAB_SIZE + sizeof (uint32);
+
+    ctx->ctab = buffer_create(256, MallocBridge, FreeBridge, ctx);
+    if (ctx->ctab == NULL)
+        return;  // out of memory.
+
+    uint32 *table = (uint32 *) buffer_reserve(ctx->ctab, tablelen);
+    if (table == NULL)
+    {
+        buffer_destroy(ctx->ctab);
+        ctx->ctab = NULL;
+        return;  // out of memory.
+    } // if
+
+    *(table++) = SWAP32(CTAB_ID);
+    *(table++) = SWAP32(CTAB_SIZE);
+    *(table++) = SWAP32(add_ctab_string(ctx, creator));
+    *(table++) = SWAP32(ctx->version_token);
+    *(table++) = SWAP32(((uint32) symbol_count));
+    *(table++) = SWAP32(add_ctab_info(ctx, symbols, symbol_count));
+    *(table++) = SWAP32(0);  // build flags.
+    *(table++) = SWAP32(add_ctab_string(ctx, ""));  // !!! FIXME: target?
+
+    const size_t ctablen = buffer_size(ctx->ctab);
+    uint8 *buf = (uint8 *) buffer_flatten(ctx->ctab);
+    if (buf != NULL)
+    {
+        output_comment_bytes(ctx, buf, ctablen);
+        Free(ctx, buf);
+    } // if
+
+    buffer_destroy(ctx->ctab);
+    ctx->ctab = NULL;
+} // output_ctab
+
+
+static void output_comments(Context *ctx, const char **comments,
+                            unsigned int comment_count,
+                            const MOJOSHADER_symbol *symbols,
+                            unsigned int symbol_count)
+{
+    if (isfail(ctx))
+        return;
+
+    // make error messages sane if CTAB fails, etc.
+    const char *prev_fname = ctx->current_file;
+    const int prev_position = ctx->current_position;
+    ctx->current_file = NULL;
+    ctx->current_position = MOJOSHADER_POSITION_BEFORE;
+
+    const char *creator = "MojoShader revision " MOJOSHADER_CHANGESET;
+    if (symbol_count > 0)
+        output_ctab(ctx, symbols, symbol_count, creator);
+    else
+        output_comment_string(ctx, creator);
+
+    unsigned int i;
+    for (i = 0; i < comment_count; i++)
+        output_comment_string(ctx, comments[i]);
+
+    ctx->current_file = prev_fname;
+    ctx->current_position = prev_position;
+} // output_comments
+
+
+static const MOJOSHADER_parseData *build_final_assembly(Context *ctx)
+{
+    if (isfail(ctx))
+        return build_failed_assembly(ctx);
+
+    // get the final bytecode!
+    const unsigned int output_len = (unsigned int) buffer_size(ctx->output);
+    unsigned char *bytecode = (unsigned char *) buffer_flatten(ctx->output);
+    buffer_destroy(ctx->output);
+    ctx->output = NULL;
+
+    if (bytecode == NULL)
+        return build_failed_assembly(ctx);
+
+    // This validates the shader; there are lots of things that are
+    //  invalid, but will successfully parse in the assembler,
+    //  generating bad bytecode; this will catch them without us
+    //  having to duplicate most of the validation here.
+    // It also saves us the trouble of duplicating all the other work,
+    //  like setting up the uniforms list, etc.
+    MOJOSHADER_parseData *retval = (MOJOSHADER_parseData *)
+                            MOJOSHADER_parse(MOJOSHADER_PROFILE_BYTECODE,
+                                    bytecode, output_len, NULL, 0, NULL, 0,
+                                    ctx->malloc, ctx->free, ctx->malloc_data);
+    Free(ctx, bytecode);
+
+    SourcePos *token_to_src = NULL;
+    if (retval->error_count > 0)
+        token_to_src = (SourcePos *) buffer_flatten(ctx->token_to_source);
+    buffer_destroy(ctx->token_to_source);
+    ctx->token_to_source = NULL;
+
+    if (retval->error_count > 0)
+    {
+        if (token_to_src == NULL)
+        {
+            assert(ctx->out_of_memory);
+            MOJOSHADER_freeParseData(retval);
+            return build_failed_assembly(ctx);
+        } // if
+
+        // on error, map the bytecode back to a line number.
+        int i;
+        for (i = 0; i < retval->error_count; i++)
+        {
+            MOJOSHADER_error *error = &retval->errors[i];
+            if (error->error_position >= 0)
+            {
+                assert(retval != &MOJOSHADER_out_of_mem_data);
+                assert((error->error_position % sizeof (uint32)) == 0);
+
+                const size_t pos = error->error_position / sizeof(uint32);
+                if (pos >= output_len)
+                    error->error_position = -1;  // oh well.
+                else
+                {
+                    const SourcePos *srcpos = &token_to_src[pos];
+                    Free(ctx, (void *) error->filename);
+                    char *fname = NULL;
+                    if (srcpos->filename != NULL)
+                        fname = StrDup(ctx, srcpos->filename);
+                    error->error_position = srcpos->line;
+                    error->filename = fname;  // may be NULL, that's okay.
+                } // else
+            } // if
+        } // for
+
+        Free(ctx, token_to_src);
+    } // if
+
+    return retval;
+} // build_final_assembly
+
+
+// API entry point...
+
+const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
+                             const char *source, unsigned int sourcelen,
+                             const char **comments, unsigned int comment_count,
+                             const MOJOSHADER_symbol *symbols,
+                             unsigned int symbol_count,
+                             const MOJOSHADER_preprocessorDefine *defines,
+                             unsigned int define_count,
+                             MOJOSHADER_includeOpen include_open,
+                             MOJOSHADER_includeClose include_close,
+                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    const MOJOSHADER_parseData *retval = NULL;
+    Context *ctx = NULL;
+
+    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
+        return &MOJOSHADER_out_of_mem_data;  // supply both or neither.
+
+    ctx = build_context(filename, source, sourcelen, defines, define_count,
+                        include_open, include_close, m, f, d);
+    if (ctx == NULL)
+        return &MOJOSHADER_out_of_mem_data;
+
+    // Version token always comes first.
+    parse_version_token(ctx);
+    output_comments(ctx, comments, comment_count, symbols, symbol_count);
+
+    // parse out the rest of the tokens after the version token...
+    Token token;
+    while ((token = nexttoken(ctx)) != TOKEN_EOI)
+        parse_token(ctx, token);
+
+    ctx->current_file = NULL;
+    ctx->current_position = MOJOSHADER_POSITION_AFTER;
+
+    output_token(ctx, 0x0000FFFF);   // end token always 0x0000FFFF.
+
+    retval = build_final_assembly(ctx);
+    destroy_context(ctx);
+    return retval;
+} // MOJOSHADER_assemble
+
+// end of mojoshader_assembler.c ...
+

+ 987 - 0
ThirdParty/MojoShader/mojoshader_common.c

@@ -0,0 +1,987 @@
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+typedef struct HashItem
+{
+    const void *key;
+    const void *value;
+    struct HashItem *next;
+} HashItem;
+
+struct HashTable
+{
+    HashItem **table;
+    uint32 table_len;
+    int stackable;
+    void *data;
+    HashTable_HashFn hash;
+    HashTable_KeyMatchFn keymatch;
+    HashTable_NukeFn nuke;
+    MOJOSHADER_malloc m;
+    MOJOSHADER_free f;
+    void *d;
+};
+
+static inline uint32 calc_hash(const HashTable *table, const void *key)
+{
+    return table->hash(key, table->data) & (table->table_len-1);
+} // calc_hash
+
+int hash_find(const HashTable *table, const void *key, const void **_value)
+{
+    HashItem *i;
+    void *data = table->data;
+    const uint32 hash = calc_hash(table, key);
+    HashItem *prev = NULL;
+    for (i = table->table[hash]; i != NULL; i = i->next)
+    {
+        if (table->keymatch(key, i->key, data))
+        {
+            if (_value != NULL)
+                *_value = i->value;
+
+            // Matched! Move to the front of list for faster lookup next time.
+            //  (stackable tables have to remain in the same order, though!)
+            if ((!table->stackable) && (prev != NULL))
+            {
+                assert(prev->next == i);
+                prev->next = i->next;
+                i->next = table->table[hash];
+                table->table[hash] = i;
+            } // if
+
+            return 1;
+        } // if
+
+        prev = i;
+    } // for
+
+    return 0;
+} // hash_find
+
+int hash_iter(const HashTable *table, const void *key,
+              const void **_value, void **iter)
+{
+    HashItem *item = (HashItem *)*iter;
+    if (item == NULL)
+        item = table->table[calc_hash(table, key)];
+    else
+        item = item->next;
+
+    while (item != NULL)
+    {
+        if (table->keymatch(key, item->key, table->data))
+        {
+            *_value = item->value;
+            *iter = item;
+            return 1;
+        } // if
+        item = item->next;
+    } // while
+
+    // no more matches.
+    *_value = NULL;
+    *iter = NULL;
+    return 0;
+} // hash_iter
+
+int hash_iter_keys(const HashTable *table, const void **_key, void **iter)
+{
+    HashItem *item = (HashItem *)*iter;
+    int idx = 0;
+
+    if (item != NULL)
+    {
+        const HashItem *orig = item;
+        item = item->next;
+        if (item == NULL)
+            idx = calc_hash(table, orig->key) + 1;
+    } // if
+
+    while (!item && (idx < table->table_len))
+        item = table->table[idx++];  // skip empty buckets...
+
+    if (item == NULL)  // no more matches?
+    {
+        *_key = NULL;
+        *iter = NULL;
+        return 0;
+    } // if
+
+    *_key = item->key;
+    *iter = item;
+    return 1;
+} // hash_iter_keys
+
+int hash_insert(HashTable *table, const void *key, const void *value)
+{
+    HashItem *item = NULL;
+    const uint32 hash = calc_hash(table, key);
+    if ( (!table->stackable) && (hash_find(table, key, NULL)) )
+        return 0;
+
+    // !!! FIXME: grow and rehash table if it gets too saturated.
+    item = (HashItem *) table->m(sizeof (HashItem), table->d);
+    if (item == NULL)
+        return -1;
+
+    item->key = key;
+    item->value = value;
+    item->next = table->table[hash];
+    table->table[hash] = item;
+
+    return 1;
+} // hash_insert
+
+HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
+              const HashTable_KeyMatchFn keymatchfn,
+              const HashTable_NukeFn nukefn,
+              const int stackable,
+              MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    const uint32 initial_table_size = 256;
+    const uint32 alloc_len = sizeof (HashItem *) * initial_table_size;
+    HashTable *table = (HashTable *) m(sizeof (HashTable), d);
+    if (table == NULL)
+        return NULL;
+    memset(table, '\0', sizeof (HashTable));
+
+    table->table = (HashItem **) m(alloc_len, d);
+    if (table->table == NULL)
+    {
+        f(table, d);
+        return NULL;
+    } // if
+
+    memset(table->table, '\0', alloc_len);
+    table->table_len = initial_table_size;
+    table->stackable = stackable;
+    table->data = data;
+    table->hash = hashfn;
+    table->keymatch = keymatchfn;
+    table->nuke = nukefn;
+    table->m = m;
+    table->f = f;
+    table->d = d;
+    return table;
+} // hash_create
+
+void hash_destroy(HashTable *table)
+{
+    uint32 i;
+    void *data = table->data;
+    MOJOSHADER_free f = table->f;
+    void *d = table->d;
+    for (i = 0; i < table->table_len; i++)
+    {
+        HashItem *item = table->table[i];
+        while (item != NULL)
+        {
+            HashItem *next = item->next;
+            table->nuke(item->key, item->value, data);
+            f(item, d);
+            item = next;
+        } // while
+    } // for
+
+    f(table->table, d);
+    f(table, d);
+} // hash_destroy
+
+int hash_remove(HashTable *table, const void *key)
+{
+    HashItem *item = NULL;
+    HashItem *prev = NULL;
+    void *data = table->data;
+    const uint32 hash = calc_hash(table, key);
+    for (item = table->table[hash]; item != NULL; item = item->next)
+    {
+        if (table->keymatch(key, item->key, data))
+        {
+            if (prev != NULL)
+                prev->next = item->next;
+            else
+                table->table[hash] = item->next;
+
+            table->nuke(item->key, item->value, data);
+            table->f(item, table->d);
+            return 1;
+        } // if
+
+        prev = item;
+    } // for
+
+    return 0;
+} // hash_remove
+
+
+// this is djb's xor hashing function.
+static inline uint32 hash_string_djbxor(const char *str, size_t len)
+{
+    register uint32 hash = 5381;
+    while (len--)
+        hash = ((hash << 5) + hash) ^ *(str++);
+    return hash;
+} // hash_string_djbxor
+
+static inline uint32 hash_string(const char *str, size_t len)
+{
+    return hash_string_djbxor(str, len);
+} // hash_string
+
+uint32 hash_hash_string(const void *sym, void *data)
+{
+    (void) data;
+    return hash_string((const char*) sym, strlen((const char *) sym));
+} // hash_hash_string
+
+int hash_keymatch_string(const void *a, const void *b, void *data)
+{
+    (void) data;
+    return (strcmp((const char *) a, (const char *) b) == 0);
+} // hash_keymatch_string
+
+
+// string -> string map...
+
+static void stringmap_nuke_noop(const void *key, const void *val, void *d) {}
+
+static void stringmap_nuke(const void *key, const void *val, void *d)
+{
+    StringMap *smap = (StringMap *) d;
+    smap->f((void *) key, smap->d);
+    smap->f((void *) val, smap->d);
+} // stringmap_nuke
+
+StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
+                            MOJOSHADER_free f, void *d)
+{
+    HashTable_NukeFn nuke = copy ? stringmap_nuke : stringmap_nuke_noop;
+    StringMap *smap;
+    smap = hash_create(0,hash_hash_string,hash_keymatch_string,nuke,0,m,f,d);
+    if (smap != NULL)
+        smap->data = smap;
+    return smap;
+} // stringmap_create
+
+void stringmap_destroy(StringMap *smap)
+{
+    hash_destroy(smap);
+} // stringmap_destroy
+
+int stringmap_insert(StringMap *smap, const char *key, const char *value)
+{
+    assert(key != NULL);
+    if (smap->nuke == stringmap_nuke_noop)  // no copy?
+        return hash_insert(smap, key, value);
+
+    int rc = -1;
+    char *k = (char *) smap->m(strlen(key) + 1, smap->d);
+    char *v = (char *) (value ? smap->m(strlen(value) + 1, smap->d) : NULL);
+    int failed = ( (!k) || ((!v) && (value)) );
+
+    if (!failed)
+    {
+        strcpy(k, key);
+        if (value != NULL)
+            strcpy(v, value);
+        failed = ((rc = hash_insert(smap, k, v)) <= 0);
+    } // if
+
+    if (failed)
+    {
+        smap->f(k, smap->d);
+        smap->f(v, smap->d);
+    } // if
+
+    return rc;
+} // stringmap_insert
+
+int stringmap_remove(StringMap *smap, const char *key)
+{
+    return hash_remove(smap, key);
+} // stringmap_remove
+
+int stringmap_find(const StringMap *smap, const char *key, const char **_value)
+{
+    const void *value = NULL;
+    const int retval = hash_find(smap, key, &value);
+    *_value = (const char *) value;
+    return retval;
+} // stringmap_find
+
+
+// The string cache...   !!! FIXME: use StringMap internally for this.
+
+typedef struct StringBucket
+{
+    char *string;
+    struct StringBucket *next;
+} StringBucket;
+
+struct StringCache
+{
+    StringBucket **hashtable;
+    uint32 table_size;
+    MOJOSHADER_malloc m;
+    MOJOSHADER_free f;
+    void *d;
+};
+
+const char *stringcache(StringCache *cache, const char *str)
+{
+    return stringcache_len(cache, str, strlen(str));
+} // stringcache
+
+const char *stringcache_len(StringCache *cache, const char *str,
+                             const unsigned int len)
+{
+    const uint8 hash = hash_string(str, len) & (cache->table_size-1);
+    StringBucket *bucket = cache->hashtable[hash];
+    StringBucket *prev = NULL;
+    while (bucket)
+    {
+        const char *bstr = bucket->string;
+        if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
+        {
+            // Matched! Move this to the front of the list.
+            if (prev != NULL)
+            {
+                assert(prev->next == bucket);
+                prev->next = bucket->next;
+                bucket->next = cache->hashtable[hash];
+                cache->hashtable[hash] = bucket;
+            } // if
+            return bstr; // already cached
+        } // if
+        prev = bucket;
+        bucket = bucket->next;
+    } // while
+
+    // no match, add to the table.
+    bucket = (StringBucket *) cache->m(sizeof (StringBucket), cache->d);
+    if (bucket == NULL)
+        return NULL;
+    bucket->string = (char *) cache->m(len + 1, cache->d);
+    if (bucket->string == NULL)
+    {
+        cache->f(bucket, cache->d);
+        return NULL;
+    } // if
+    memcpy(bucket->string, str, len);
+    bucket->string[len] = '\0';
+    bucket->next = cache->hashtable[hash];
+    cache->hashtable[hash] = bucket;
+    return bucket->string;
+} // stringcache_len
+
+const char *stringcache_fmt(StringCache *cache, const char *fmt, ...)
+{
+    char buf[128];  // use the stack if reasonable.
+    char *ptr = NULL;
+    int len = 0;  // number of chars, NOT counting null-terminator!
+    va_list ap;
+
+    va_start(ap, fmt);
+    len = vsnprintf(buf, sizeof (buf), fmt, ap);
+    va_end(ap);
+
+    if (len > sizeof (buf))
+    {
+        ptr = (char *) cache->m(len, cache->d);
+        if (ptr == NULL)
+            return NULL;
+
+        va_start(ap, fmt);
+        vsnprintf(ptr, len, fmt, ap);
+        va_end(ap);
+    } // if
+
+    const char *retval = stringcache_len(cache, ptr ? ptr : buf, len);
+    if (ptr != NULL)
+        cache->f(ptr, cache->d);
+
+    return retval;
+} // stringcache_fmt
+
+StringCache *stringcache_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    const uint32 initial_table_size = 256;
+    const size_t tablelen = sizeof (StringBucket *) * initial_table_size;
+    StringCache *cache = (StringCache *) m(sizeof (StringCache), d);
+    if (!cache)
+        return NULL;
+    memset(cache, '\0', sizeof (StringCache));
+
+    cache->hashtable = (StringBucket **) m(tablelen, d);
+    if (!cache->hashtable)
+    {
+        f(cache, d);
+        return NULL;
+    } // if
+    memset(cache->hashtable, '\0', tablelen);
+
+    cache->table_size = initial_table_size;
+    cache->m = m;
+    cache->f = f;
+    cache->d = d;
+    return cache;
+} // stringcache_create
+
+void stringcache_destroy(StringCache *cache)
+{
+    if (cache == NULL)
+        return;
+
+    MOJOSHADER_free f = cache->f;
+    void *d = cache->d;
+    size_t i;
+
+    for (i = 0; i < cache->table_size; i++)
+    {
+        StringBucket *bucket = cache->hashtable[i];
+        cache->hashtable[i] = NULL;
+        while (bucket)
+        {
+            StringBucket *next = bucket->next;
+            f(bucket->string, d);
+            f(bucket, d);
+            bucket = next;
+        } // while
+    } // for
+
+    f(cache->hashtable, d);
+    f(cache, d);
+} // stringcache_destroy
+
+
+// We chain errors as a linked list with a head/tail for easy appending.
+//  These get flattened before passing to the application.
+typedef struct ErrorItem
+{
+    MOJOSHADER_error error;
+    struct ErrorItem *next;
+} ErrorItem;
+
+struct ErrorList
+{
+    ErrorItem head;
+    ErrorItem *tail;
+    int count;
+    MOJOSHADER_malloc m;
+    MOJOSHADER_free f;
+    void *d;
+};
+
+ErrorList *errorlist_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    ErrorList *retval = (ErrorList *) m(sizeof (ErrorList), d);
+    if (retval != NULL)
+    {
+        memset(retval, '\0', sizeof (ErrorList));
+        retval->tail = &retval->head;
+        retval->m = m;
+        retval->f = f;
+        retval->d = d;
+    } // if
+
+    return retval;
+} // errorlist_create
+
+
+int errorlist_add(ErrorList *list, const char *fname,
+                  const int errpos, const char *str)
+{
+    return errorlist_add_fmt(list, fname, errpos, "%s", str);
+} // errorlist_add
+
+
+int errorlist_add_fmt(ErrorList *list, const char *fname,
+                      const int errpos, const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    const int retval = errorlist_add_va(list, fname, errpos, fmt, ap);
+    va_end(ap);
+    return retval;
+} // errorlist_add_fmt
+
+
+int errorlist_add_va(ErrorList *list, const char *_fname,
+                     const int errpos, const char *fmt, va_list va)
+{
+    ErrorItem *error = (ErrorItem *) list->m(sizeof (ErrorItem), list->d);
+    if (error == NULL)
+        return 0;
+
+    char *fname = NULL;
+    if (_fname != NULL)
+    {
+        fname = (char *) list->m(strlen(_fname) + 1, list->d);
+        if (fname == NULL)
+        {
+            list->f(error, list->d);
+            return 0;
+        } // if
+        strcpy(fname, _fname);
+    } // if
+
+    char scratch[128];
+    va_list ap;
+    va_copy(ap, va);
+    const int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
+    va_end(ap);
+
+    char *failstr = (char *) list->m(len + 1, list->d);
+    if (failstr == NULL)
+    {
+        list->f(error, list->d);
+        list->f(fname, list->d);
+        return 0;
+    } // if
+
+    // If we overflowed our scratch buffer, that's okay. We were going to
+    //  allocate anyhow...the scratch buffer just lets us avoid a second
+    //  run of vsnprintf().
+    if (len < sizeof (scratch))
+        strcpy(failstr, scratch);  // copy it over.
+    else
+    {
+        va_copy(ap, va);
+        vsnprintf(failstr, len + 1, fmt, ap);  // rebuild it.
+        va_end(ap);
+    } // else
+
+    error->error.error = failstr;
+    error->error.filename = fname;
+    error->error.error_position = errpos;
+    error->next = NULL;
+
+    list->tail->next = error;
+    list->tail = error;
+
+    list->count++;
+    return 1;
+} // errorlist_add_va
+
+
+int errorlist_count(ErrorList *list)
+{
+    return list->count;
+} // errorlist_count
+
+
+MOJOSHADER_error *errorlist_flatten(ErrorList *list)
+{
+    if (list->count == 0)
+        return NULL;
+
+    int total = 0;
+    MOJOSHADER_error *retval = (MOJOSHADER_error *)
+            list->m(sizeof (MOJOSHADER_error) * list->count, list->d);
+    if (retval == NULL)
+        return NULL;
+
+    ErrorItem *item = list->head.next;
+    while (item != NULL)
+    {
+        ErrorItem *next = item->next;
+        // reuse the string allocations
+        memcpy(&retval[total], &item->error, sizeof (MOJOSHADER_error));
+        list->f(item, list->d);
+        item = next;
+        total++;
+    } // while
+
+    assert(total == list->count);
+    list->count = 0;
+    list->head.next = NULL;
+    list->tail = &list->head;
+    return retval;
+} // errorlist_flatten
+
+
+void errorlist_destroy(ErrorList *list)
+{
+    if (list == NULL)
+        return;
+
+    MOJOSHADER_free f = list->f;
+    void *d = list->d;
+    ErrorItem *item = list->head.next;
+    while (item != NULL)
+    {
+        ErrorItem *next = item->next;
+        f((void *) item->error.error, d);
+        f((void *) item->error.filename, d);
+        f(item, d);
+        item = next;
+    } // while
+    f(list, d);
+} // errorlist_destroy
+
+
+typedef struct BufferBlock
+{
+    uint8 *data;
+    size_t bytes;
+    struct BufferBlock *next;
+} BufferBlock;
+
+struct Buffer
+{
+    size_t total_bytes;
+    BufferBlock *head;
+    BufferBlock *tail;
+    size_t block_size;
+    MOJOSHADER_malloc m;
+    MOJOSHADER_free f;
+    void *d;
+};
+
+Buffer *buffer_create(size_t blksz, MOJOSHADER_malloc m,
+                      MOJOSHADER_free f, void *d)
+{
+    Buffer *buffer = (Buffer *) m(sizeof (Buffer), d);
+    if (buffer != NULL)
+    {
+        memset(buffer, '\0', sizeof (Buffer));
+        buffer->block_size = blksz;
+        buffer->m = m;
+        buffer->f = f;
+        buffer->d = d;
+    } // if
+    return buffer;
+} // buffer_create
+
+char *buffer_reserve(Buffer *buffer, const size_t len)
+{
+    // note that we make the blocks bigger than blocksize when we have enough
+    //  data to overfill a fresh block, to reduce allocations.
+    const size_t blocksize = buffer->block_size;
+
+    if (len == 0)
+        return NULL;
+
+    if (buffer->tail != NULL)
+    {
+        const size_t tailbytes = buffer->tail->bytes;
+        const size_t avail = (tailbytes >= blocksize) ? 0 : blocksize - tailbytes;
+        if (len <= avail)
+        {
+            buffer->tail->bytes += len;
+            buffer->total_bytes += len;
+            assert(buffer->tail->bytes <= blocksize);
+            return (char *) buffer->tail->data + tailbytes;
+        } // if
+    } // if
+
+    // need to allocate a new block (even if a previous block wasn't filled,
+    //  so this buffer is contiguous).
+    const size_t bytecount = len > blocksize ? len : blocksize;
+    const size_t malloc_len = sizeof (BufferBlock) + bytecount;
+    BufferBlock *item = (BufferBlock *) buffer->m(malloc_len, buffer->d);
+    if (item == NULL)
+        return NULL;
+
+    item->data = ((uint8 *) item) + sizeof (BufferBlock);
+    item->bytes = len;
+    item->next = NULL;
+    if (buffer->tail != NULL)
+        buffer->tail->next = item;
+    else
+        buffer->head = item;
+    buffer->tail = item;
+
+    buffer->total_bytes += len;
+
+    return (char *) item->data;
+} // buffer_reserve
+
+int buffer_append(Buffer *buffer, const void *_data, size_t len)
+{
+    const uint8 *data = (const uint8 *) _data;
+
+    // note that we make the blocks bigger than blocksize when we have enough
+    //  data to overfill a fresh block, to reduce allocations.
+    const size_t blocksize = buffer->block_size;
+
+    if (len == 0)
+        return 1;
+
+    if (buffer->tail != NULL)
+    {
+        const size_t tailbytes = buffer->tail->bytes;
+        const size_t avail = (tailbytes >= blocksize) ? 0 : blocksize - tailbytes;
+        const size_t cpy = (avail > len) ? len : avail;
+        if (cpy > 0)
+        {
+            memcpy(buffer->tail->data + tailbytes, data, cpy);
+            len -= cpy;
+            data += cpy;
+            buffer->tail->bytes += cpy;
+            buffer->total_bytes += cpy;
+            assert(buffer->tail->bytes <= blocksize);
+        } // if
+    } // if
+
+    if (len > 0)
+    {
+        assert((!buffer->tail) || (buffer->tail->bytes == blocksize));
+        const size_t bytecount = len > blocksize ? len : blocksize;
+        const size_t malloc_len = sizeof (BufferBlock) + bytecount;
+        BufferBlock *item = (BufferBlock *) buffer->m(malloc_len, buffer->d);
+        if (item == NULL)
+            return 0;
+
+        item->data = ((uint8 *) item) + sizeof (BufferBlock);
+        item->bytes = len;
+        item->next = NULL;
+        if (buffer->tail != NULL)
+            buffer->tail->next = item;
+        else
+            buffer->head = item;
+        buffer->tail = item;
+
+        memcpy(item->data, data, len);
+        buffer->total_bytes += len;
+    } // if
+
+    return 1;
+} // buffer_append
+
+int buffer_append_fmt(Buffer *buffer, const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    const int retval = buffer_append_va(buffer, fmt, ap);
+    va_end(ap);
+    return retval;
+} // buffer_append_fmt
+
+int buffer_append_va(Buffer *buffer, const char *fmt, va_list va)
+{
+    char scratch[256];
+
+    va_list ap;
+    va_copy(ap, va);
+    const int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
+    va_end(ap);
+
+    // If we overflowed our scratch buffer, heap allocate and try again.
+
+    if (len == 0)
+        return 1;  // nothing to do.
+    else if (len < sizeof (scratch))
+        return buffer_append(buffer, scratch, len);
+
+    char *buf = (char *) buffer->m(len + 1, buffer->d);
+    if (buf == NULL)
+        return 0;
+    va_copy(ap, va);
+    vsnprintf(buf, len + 1, fmt, ap);  // rebuild it.
+    va_end(ap);
+    const int retval = buffer_append(buffer, scratch, len);
+    buffer->f(buf, buffer->d);
+    return retval;
+} // buffer_append_va
+
+size_t buffer_size(Buffer *buffer)
+{
+    return buffer->total_bytes;
+} // buffer_size
+
+void buffer_empty(Buffer *buffer)
+{
+    BufferBlock *item = buffer->head;
+    while (item != NULL)
+    {
+        BufferBlock *next = item->next;
+        buffer->f(item, buffer->d);
+        item = next;
+    } // while
+    buffer->head = buffer->tail = NULL;
+    buffer->total_bytes = 0;
+} // buffer_empty
+
+char *buffer_flatten(Buffer *buffer)
+{
+    char *retval = (char *) buffer->m(buffer->total_bytes + 1, buffer->d);
+    if (retval == NULL)
+        return NULL;
+    BufferBlock *item = buffer->head;
+    char *ptr = retval;
+    while (item != NULL)
+    {
+        BufferBlock *next = item->next;
+        memcpy(ptr, item->data, item->bytes);
+        ptr += item->bytes;
+        buffer->f(item, buffer->d);
+        item = next;
+    } // while
+    *ptr = '\0';
+
+    assert(ptr == (retval + buffer->total_bytes));
+
+    buffer->head = buffer->tail = NULL;
+    buffer->total_bytes = 0;
+
+    return retval;
+} // buffer_flatten
+
+char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len)
+{
+    Buffer *first = NULL;
+    size_t len = 0;
+    size_t i;
+    for (i = 0; i < n; i++)
+    {
+        Buffer *buffer = buffers[i];
+        if (buffer == NULL)
+            continue;
+        if (first == NULL)
+            first = buffer;
+        len += buffer->total_bytes;
+    } // for
+
+    char *retval = (char *) (first ? first->m(len + 1, first->d) : NULL);
+    if (retval == NULL)
+    {
+        *_len = 0;
+        return NULL;
+    } // if
+
+    *_len = len;
+    char *ptr = retval;
+    for (i = 0; i < n; i++)
+    {
+        Buffer *buffer = buffers[i];
+        if (buffer == NULL)
+            continue;
+        BufferBlock *item = buffer->head;
+        while (item != NULL)
+        {
+            BufferBlock *next = item->next;
+            memcpy(ptr, item->data, item->bytes);
+            ptr += item->bytes;
+            buffer->f(item, buffer->d);
+            item = next;
+        } // while
+
+        buffer->head = buffer->tail = NULL;
+        buffer->total_bytes = 0;
+    } // for
+    *ptr = '\0';
+
+    assert(ptr == (retval + len));
+
+    return retval;
+} // buffer_merge
+
+void buffer_destroy(Buffer *buffer)
+{
+    if (buffer != NULL)
+    {
+        MOJOSHADER_free f = buffer->f;
+        void *d = buffer->d;
+        buffer_empty(buffer);
+        f(buffer, d);
+    } // if
+} // buffer_destroy
+
+static int blockscmp(BufferBlock *item, const uint8 *data, size_t len)
+{
+    if (len == 0)
+        return 1;  // "match"
+
+    while (item != NULL)
+    {
+        const size_t itemremain = item->bytes;
+        const size_t avail = len < itemremain ? len : itemremain;
+        if (memcmp(item->data, data, avail) != 0)
+            return 0;  // not a match.
+
+        if (len == avail)
+            return 1;   // complete match!
+
+        len -= avail;
+        data += avail;
+        item = item->next;
+    } // while
+
+    return 0;  // not a complete match.
+} // blockscmp
+
+ssize_t buffer_find(Buffer *buffer, const size_t start,
+                    const void *_data, const size_t len)
+{
+    if (len == 0)
+        return 0;  // I guess that's right.
+
+    if (start >= buffer->total_bytes)
+        return -1;  // definitely can't match.
+
+    if (len > (buffer->total_bytes - start))
+        return -1;  // definitely can't match.
+
+    // Find the start point somewhere in the center of a buffer.
+    BufferBlock *item = buffer->head;
+    const uint8 *ptr = item->data;
+    size_t pos = 0;
+    if (start > 0)
+    {
+        while (1)
+        {
+            assert(item != NULL);
+            if ((pos + item->bytes) > start)  // start is in this block.
+            {
+                ptr = item->data + (start - pos);
+                break;
+            } // if
+
+            pos += item->bytes;
+            item = item->next;
+        } // while
+    } // if
+
+    // okay, we're at the origin of the search.
+    assert(item != NULL);
+    assert(ptr != NULL);
+
+    const uint8 *data = (const uint8 *) _data;
+    const uint8 first = *data;
+    while (item != NULL)
+    {
+        const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
+        ptr = (uint8 *) memchr(ptr, first, itemremain);
+        while (ptr != NULL)
+        {
+            const size_t retval = pos + ((size_t) (ptr - item->data));
+            if (len == 1)
+                return retval;  // we're done, here it is!
+
+            const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
+            const size_t avail = len < itemremain ? len : itemremain;
+            if ((avail == 0) || (memcmp(ptr, data, avail) == 0))
+            {
+                // okay, we've got a (sub)string match! Move to the next block.
+                // check all blocks until we get a complete match or a failure.
+                if (blockscmp(item->next, data+avail, len-avail))
+                    return (ssize_t) retval;
+            } // if
+
+            // try again, further in this block.
+            ptr = (uint8 *) memchr(ptr + 1, first, itemremain - 1);
+        } // while
+
+        pos += item->bytes;
+        item = item->next;
+        if (item != NULL)
+            ptr = item->data;
+    } // while
+
+    return -1;  // no match found.
+} // buffer_find
+
+// end of mojoshader_common.c ...
+

+ 6296 - 0
ThirdParty/MojoShader/mojoshader_compiler.c

@@ -0,0 +1,6296 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+// !!! FIXME: this needs to be split into separate source files:
+// !!! FIXME:  parse, AST, IR, etc. The problem is we need to deal with the
+// !!! FIXME:  "Context" struct being passed around everywhere.
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+#if DEBUG_COMPILER_PARSER
+#define LEMON_SUPPORT_TRACING 1
+#endif
+
+// !!! FIXME: I'd like to lose this. It's really inefficient. Just keep a
+// !!! FIXME:  (tail) on these list structures instead?
+#define REVERSE_LINKED_LIST(typ, head) { \
+    if ((head) && (head->next)) { \
+        typ *tmp = NULL; \
+        typ *tmp1 = NULL; \
+        while (head != NULL) { \
+            tmp = head; \
+            head = head->next; \
+            tmp->next = tmp1; \
+            tmp1 = tmp; \
+        } \
+        head = tmp; \
+    } \
+}
+
+static inline int operator_is_unary(const MOJOSHADER_astNodeType op)
+{
+    return ( (op > MOJOSHADER_AST_OP_START_RANGE_UNARY) &&
+             (op < MOJOSHADER_AST_OP_END_RANGE_UNARY) );
+} // operator_is_unary
+
+static inline int operator_is_binary(const MOJOSHADER_astNodeType op)
+{
+    return ( (op > MOJOSHADER_AST_OP_START_RANGE_BINARY) &&
+             (op < MOJOSHADER_AST_OP_END_RANGE_BINARY) );
+} // operator_is_binary
+
+static inline int operator_is_ternary(const MOJOSHADER_astNodeType op)
+{
+    return ( (op > MOJOSHADER_AST_OP_START_RANGE_TERNARY) &&
+             (op < MOJOSHADER_AST_OP_END_RANGE_TERNARY) );
+} // operator_is_ternary
+
+
+typedef union TokenData
+{
+    int64 i64;
+    double dbl;
+    const char *string;
+    const MOJOSHADER_astDataType *datatype;
+} TokenData;
+
+
+// This tracks data types and variables, and notes when they enter/leave scope.
+
+typedef struct SymbolScope
+{
+    const char *symbol;
+    const MOJOSHADER_astDataType *datatype;
+    int index;  // unique positive value within a function, negative if global.
+    int referenced;  // non-zero if something looked for this symbol (so we know it's used).
+    struct SymbolScope *next;
+} SymbolScope;
+
+typedef struct SymbolMap
+{
+    HashTable *hash;
+    SymbolScope *scope;
+} SymbolMap;
+
+typedef struct LoopLabels
+{
+    int start;  // loop's start label during IR build.
+    int end;    // loop's end label during IR build.
+    struct LoopLabels *prev;
+} LoopLabels;
+
+// Compile state, passed around all over the place.
+
+typedef struct Context
+{
+    int isfail;
+    int out_of_memory;
+    MOJOSHADER_malloc malloc;
+    MOJOSHADER_free free;
+    void *malloc_data;
+    ErrorList *errors;
+    ErrorList *warnings;
+    StringCache *strcache;
+    const char *sourcefile;  // current source file that we're parsing.
+    unsigned int sourceline; // current line in sourcefile that we're parsing.
+    SymbolMap usertypes;
+    SymbolMap variables;
+    MOJOSHADER_astNode *ast;  // Abstract Syntax Tree
+    const char *source_profile;
+    int is_func_scope; // non-zero if semantic analysis is in function scope.
+    int loop_count;
+    int switch_count;
+    int var_index;  // next variable index for current function.
+    int global_var_index;  // next variable index for global scope.
+    int user_func_index;  // next function index for user-defined functions.
+    int intrinsic_func_index;  // next function index for intrinsic functions.
+
+    MOJOSHADER_irStatement **ir;  // intermediate representation.
+    int ir_label_count;  // next unused IR label index.
+    int ir_temp_count;  // next unused IR temporary value index.
+    int ir_end; // current function's end label during IR build.
+    int ir_ret; // temp that holds current function's retval during IR build.
+    LoopLabels *ir_loop;  // nested loop boundary labels during IR build.
+
+    // Cache intrinsic types for fast lookup and consistent pointer values.
+    MOJOSHADER_astDataType dt_none;
+    MOJOSHADER_astDataType dt_bool;
+    MOJOSHADER_astDataType dt_int;
+    MOJOSHADER_astDataType dt_uint;
+    MOJOSHADER_astDataType dt_float;
+    MOJOSHADER_astDataType dt_float_snorm;
+    MOJOSHADER_astDataType dt_float_unorm;
+    MOJOSHADER_astDataType dt_half;
+    MOJOSHADER_astDataType dt_double;
+    MOJOSHADER_astDataType dt_string;
+    MOJOSHADER_astDataType dt_sampler1d;
+    MOJOSHADER_astDataType dt_sampler2d;
+    MOJOSHADER_astDataType dt_sampler3d;
+    MOJOSHADER_astDataType dt_samplercube;
+    MOJOSHADER_astDataType dt_samplerstate;
+    MOJOSHADER_astDataType dt_samplercompstate;
+    MOJOSHADER_astDataType dt_buf_bool;
+    MOJOSHADER_astDataType dt_buf_int;
+    MOJOSHADER_astDataType dt_buf_uint;
+    MOJOSHADER_astDataType dt_buf_half;
+    MOJOSHADER_astDataType dt_buf_float;
+    MOJOSHADER_astDataType dt_buf_double;
+    MOJOSHADER_astDataType dt_buf_float_snorm;
+    MOJOSHADER_astDataType dt_buf_float_unorm;
+
+    Buffer *garbage;  // this is sort of hacky.
+} Context;
+
+
+// !!! FIXME: cut and paste between every damned source file follows...
+// !!! FIXME: We need to make some sort of ContextBase that applies to all
+// !!! FIXME:  files and move this stuff to mojoshader_common.c ...
+
+// Convenience functions for allocators...
+
+static inline void out_of_memory(Context *ctx)
+{
+    ctx->isfail = ctx->out_of_memory = 1;
+} // out_of_memory
+
+static inline void *Malloc(Context *ctx, const size_t len)
+{
+    void *retval = ctx->malloc((int) len, ctx->malloc_data);
+    if (retval == NULL)
+        out_of_memory(ctx);
+    return retval;
+} // Malloc
+
+static inline char *StrDup(Context *ctx, const char *str)
+{
+    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
+    if (retval != NULL)
+        strcpy(retval, str);
+    return retval;
+} // StrDup
+
+static inline void Free(Context *ctx, void *ptr)
+{
+    ctx->free(ptr, ctx->malloc_data);
+} // Free
+
+static void *MallocBridge(int bytes, void *data)
+{
+    return Malloc((Context *) data, (size_t) bytes);
+} // MallocBridge
+
+static void FreeBridge(void *ptr, void *data)
+{
+    Free((Context *) data, ptr);
+} // FreeBridge
+
+static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void failf(Context *ctx, const char *fmt, ...)
+{
+    ctx->isfail = 1;
+    if (ctx->out_of_memory)
+        return;
+
+    va_list ap;
+    va_start(ap, fmt);
+    errorlist_add_va(ctx->errors, ctx->sourcefile, ctx->sourceline, fmt, ap);
+    va_end(ap);
+} // failf
+
+static inline void fail(Context *ctx, const char *reason)
+{
+    failf(ctx, "%s", reason);
+} // fail
+
+static void warnf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void warnf(Context *ctx, const char *fmt, ...)
+{
+    if (ctx->out_of_memory)
+        return;
+
+    va_list ap;
+    va_start(ap, fmt);
+    errorlist_add_va(ctx->warnings, ctx->sourcefile, ctx->sourceline, fmt, ap);
+    va_end(ap);
+} // warnf
+
+static inline void warn(Context *ctx, const char *reason)
+{
+    warnf(ctx, "%s", reason);
+} // warn
+
+static inline int isfail(const Context *ctx)
+{
+    return ctx->isfail;
+} // isfail
+
+
+static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
+
+static int create_symbolmap(Context *ctx, SymbolMap *map)
+{
+    // !!! FIXME: should compare string pointer, with string in cache.
+    map->scope = NULL;
+    map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
+                            symbolmap_nuke, 1, MallocBridge, FreeBridge, ctx);
+    return (map->hash != NULL);
+} // create_symbolmap
+
+static int datatypes_match(const MOJOSHADER_astDataType *a,
+                           const MOJOSHADER_astDataType *b)
+{
+    int i;
+
+    if (a == b)
+        return 1;
+    else if (a->type != b->type)
+        return 0;
+
+    switch (a->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_STRUCT:
+            if (a->structure.member_count != b->structure.member_count)
+                return 0;
+            for (i = 0; i < a->structure.member_count; i++)
+            {
+                if (!datatypes_match(a->structure.members[i].datatype,
+                                     b->structure.members[i].datatype))
+                    return 0;
+                // stringcache'd, pointer compare is safe.
+                else if (a->structure.members[i].identifier !=
+                         b->structure.members[i].identifier)
+                    return 0;
+            } // for
+            return 1;
+
+        case MOJOSHADER_AST_DATATYPE_ARRAY:
+            if (a->array.elements != b->array.elements)
+                return 0;
+            else if (!datatypes_match(a->array.base, b->array.base))
+                return 0;
+            return 1;
+
+        case MOJOSHADER_AST_DATATYPE_VECTOR:
+            if (a->vector.elements != b->vector.elements)
+                return 0;
+            else if (!datatypes_match(a->vector.base, b->vector.base))
+                return 0;
+            return 1;
+
+        case MOJOSHADER_AST_DATATYPE_MATRIX:
+            if (a->matrix.rows != b->matrix.rows)
+                return 0;
+            else if (a->matrix.columns != b->matrix.columns)
+                return 0;
+            else if (!datatypes_match(a->matrix.base, b->matrix.base))
+                return 0;
+            return 1;
+
+        case MOJOSHADER_AST_DATATYPE_BUFFER:
+            return datatypes_match(a->buffer.base, b->buffer.base);
+
+        case MOJOSHADER_AST_DATATYPE_FUNCTION:
+            if (a->function.num_params != b->function.num_params)
+                return 0;
+            else if (a->function.intrinsic != b->function.intrinsic)
+                return 0;
+            else if (!datatypes_match(a->function.retval, b->function.retval))
+                return 0;
+            for (i = 0; i < a->function.num_params; i++)
+            {
+                if (!datatypes_match(a->function.params[i], b->function.params[i]))
+                    return 0;
+            } // for
+            return 1;
+
+        case MOJOSHADER_AST_DATATYPE_USER:
+            return 0;  // pointers must match, this clearly didn't.
+
+        default:
+            assert(0 && "unexpected case");
+            return 0;
+    } // switch
+
+    return 0;
+} // datatypes_match
+
+static void push_symbol(Context *ctx, SymbolMap *map, const char *sym,
+                        const MOJOSHADER_astDataType *dt, const int index,
+                        const int check_dupes)
+{
+    if (ctx->out_of_memory)
+        return;
+
+    // Decide if this symbol is defined, and if it's in the current scope.
+    SymbolScope *item = NULL;
+    const void *value = NULL;
+    if ((check_dupes) && (sym != NULL) && (hash_find(map->hash, sym, &value)))
+    {
+        // check the current scope for a dupe.
+        // !!! FIXME: note current scope's starting index, see if found
+        // !!! FIXME:  item is < index (and thus, a previous scope).
+        item = map->scope;
+        while ((item) && (item->symbol))
+        {
+            if ( ((const void *) item) == value )
+            {
+                failf(ctx, "Symbol '%s' already defined", sym);
+                return;
+            } // if
+            item = item->next;
+        } // while
+    } // if
+
+    // Add the symbol to our map and scope stack.
+    item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
+    if (item == NULL)
+        return;
+
+    if (sym != NULL)  // sym can be NULL if we're pushing a new scope.
+    {
+        if (hash_insert(map->hash, sym, item) == -1)
+        {
+            Free(ctx, item);
+            return;
+        } // if
+    } // if
+
+    item->symbol = sym;  // cached strings, don't copy.
+    item->index = index;
+    item->datatype = dt;
+    item->referenced = 0;
+    item->next = map->scope;
+    map->scope = item;
+} // push_symbol
+
+static void push_usertype(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
+{
+    if (sym != NULL)
+    {
+        MOJOSHADER_astDataType *userdt;
+        userdt = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*userdt));
+        if (userdt != NULL)
+        {
+            // !!! FIXME: this is hacky.
+            if (!buffer_append(ctx->garbage, &userdt, sizeof (userdt)))
+            {
+                Free(ctx, userdt);
+                return;
+            } // if
+
+            userdt->type = MOJOSHADER_AST_DATATYPE_USER;
+            userdt->user.details = dt;
+            userdt->user.name = sym;
+
+            dt = userdt;
+        } // if
+    } // if
+
+    push_symbol(ctx, &ctx->usertypes, sym, dt, 0, 1);
+} // push_usertype
+
+static inline void push_variable(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
+{
+    int idx = 0;
+    if (sym != NULL)
+    {
+        // leave space for individual member indexes. The IR will need this.
+        int additional = 0;
+        if (dt->type == MOJOSHADER_AST_DATATYPE_STRUCT)
+            additional = dt->structure.member_count;
+        if (ctx->is_func_scope)
+        {
+            idx = ++ctx->var_index;  // these are positive.
+            ctx->var_index += additional;
+        } // if
+        else
+        {
+            idx = --ctx->global_var_index;  // these are negative.
+            ctx->global_var_index -= additional;
+        } // else
+    } // if
+
+    push_symbol(ctx, &ctx->variables, sym, dt, idx, 1);
+} // push_variable
+
+static int push_function(Context *ctx, const char *sym,
+                          const MOJOSHADER_astDataType *dt,
+                          const int just_declare)
+{
+    // we don't have any reason to support nested functions at the moment,
+    //  so this would be a bug.
+    assert(!ctx->is_func_scope);
+    assert(dt->type == MOJOSHADER_AST_DATATYPE_FUNCTION);
+
+    // Functions are always global, so no need to search scopes.
+    //  Functions overload, though, so we have to continue iterating to
+    //  see if it matches anything.
+    const void *value = NULL;
+    void *iter = NULL;
+    while (hash_iter(ctx->variables.hash, sym, &value, &iter))
+    {
+        // !!! FIXME: this breaks if you predeclare a function.
+        // !!! FIXME:  (a declare AFTER defining works, though.)
+        // there's already something called this.
+        SymbolScope *item = (SymbolScope *) value;
+        if (datatypes_match(dt, item->datatype))
+        {
+            if (!just_declare)
+                failf(ctx, "Function '%s' already defined.", sym);
+            return item->index;
+        } // if
+    } // while
+
+    int idx = 0;
+    if ((sym != NULL) && (dt != NULL))
+    {
+        if (!dt->function.intrinsic)
+            idx = ++ctx->user_func_index;  // these are positive.
+        else
+            idx = --ctx->intrinsic_func_index;  // these are negative.
+    } // if
+
+    // push_symbol() doesn't check dupes, because we just did.
+    push_symbol(ctx, &ctx->variables, sym, dt, idx, 0);
+
+    return idx;
+} // push_function
+
+static inline void push_scope(Context *ctx)
+{
+    push_usertype(ctx, NULL, NULL);
+    push_variable(ctx, NULL, NULL);
+} // push_scope
+
+static void pop_symbol(Context *ctx, SymbolMap *map)
+{
+    SymbolScope *item = map->scope;
+    if (!item)
+        return;
+    if (item->symbol)
+        hash_remove(map->hash, item->symbol);
+    map->scope = item->next;
+    Free(ctx, item);
+} // pop_symbol
+
+static void pop_symbol_scope(Context *ctx, SymbolMap *map)
+{
+    while ((map->scope) && (map->scope->symbol))
+        pop_symbol(ctx, map);
+
+    assert(map->scope != NULL);
+    assert(map->scope->symbol == NULL);
+    pop_symbol(ctx, map);
+} // pop_symbol_scope
+
+static inline void pop_scope(Context *ctx)
+{
+    pop_symbol_scope(ctx, &ctx->usertypes);
+    pop_symbol_scope(ctx, &ctx->variables);
+} // push_scope
+
+static const MOJOSHADER_astDataType *find_symbol(Context *ctx, SymbolMap *map, const char *sym, int *_index)
+{
+    const void *_item = NULL;
+    hash_find(map->hash, sym, &_item);
+    SymbolScope *item = (SymbolScope *) _item;
+    if (item != NULL)
+    {
+        item->referenced++;
+        if (_index != NULL)
+            *_index = item->index;
+    } // if
+    return item ? item->datatype : NULL;
+} // find_symbol
+
+static inline const MOJOSHADER_astDataType *find_usertype(Context *ctx, const char *sym)
+{
+    return find_symbol(ctx, &ctx->usertypes, sym, NULL);
+} // find_usertype
+
+static inline const MOJOSHADER_astDataType *find_variable(Context *ctx, const char *sym, int *_index)
+{
+    return find_symbol(ctx, &ctx->variables, sym, _index);
+} // find_variable
+
+static void destroy_symbolmap(Context *ctx, SymbolMap *map)
+{
+    while (map->scope)
+        pop_symbol(ctx, map);
+    hash_destroy(map->hash);
+} // destroy_symbolmap
+
+
+static const MOJOSHADER_astDataType *new_datatype_vector(Context *ctx,
+                                            const MOJOSHADER_astDataType *dt,
+                                            const int columns)
+{
+    MOJOSHADER_astDataType *retval;
+    retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
+    if (retval == NULL)
+        return NULL;
+
+    // !!! FIXME: this is hacky.
+    // !!! FIXME:  I'd like to cache these anyhow and reuse types.
+    if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
+    {
+        Free(ctx, retval);
+        return NULL;
+    } // if
+
+    if ((columns < 1) || (columns > 4))
+        fail(ctx, "Vector must have between 1 and 4 elements");
+
+    retval->type = MOJOSHADER_AST_DATATYPE_VECTOR;
+    retval->vector.base = dt;
+    retval->vector.elements = columns;
+    return retval;
+} // new_datatype_vector
+
+static const MOJOSHADER_astDataType *new_datatype_matrix(Context *ctx,
+                                            const MOJOSHADER_astDataType *dt,
+                                            const int rows, const int columns)
+{
+    MOJOSHADER_astDataType *retval;
+    // !!! FIXME: allocate enough for a matrix, but we need to cleanup things that copy without checking for subsize.
+    retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
+    if (retval == NULL)
+        return NULL;
+
+    // !!! FIXME: this is hacky.
+    // !!! FIXME:  I'd like to cache these anyhow and reuse types.
+    if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
+    {
+        Free(ctx, retval);
+        return NULL;
+    } // if
+
+    if ((rows < 1) || (rows > 4))
+        fail(ctx, "Matrix must have between 1 and 4 rows");
+    if ((columns < 1) || (columns > 4))
+        fail(ctx, "Matrix must have between 1 and 4 columns");
+
+    retval->type = MOJOSHADER_AST_DATATYPE_MATRIX;
+    retval->matrix.base = dt;
+    retval->matrix.rows = rows;
+    retval->matrix.columns = columns;
+    return retval;
+} // new_datatype_matrix
+
+
+// !!! FIXME: move this to mojoshader_ast.c
+// !!! FIXME: new_* and delete_* should take an allocator, not a context.
+
+// These functions are mostly for construction and cleanup of nodes in the
+//  parse tree. Mostly this is simple allocation and initialization, so we
+//  can do as little in the lemon code as possible, and then sort it all out
+//  afterwards.
+
+#define NEW_AST_NODE(retval, cls, typ) \
+    cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
+    do { \
+        if (retval == NULL) { return NULL; } \
+        retval->ast.type = typ; \
+        retval->ast.filename = ctx->sourcefile; \
+        retval->ast.line = ctx->sourceline; \
+    } while (0)
+
+#define DELETE_AST_NODE(cls) do { \
+    if (!cls) return; \
+} while (0)
+
+
+static void delete_compilation_unit(Context*, MOJOSHADER_astCompilationUnit*);
+static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt);
+
+static MOJOSHADER_astExpression *new_identifier_expr(Context *ctx,
+                                                     const char *string)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionIdentifier,
+                 MOJOSHADER_AST_OP_IDENTIFIER);
+    retval->datatype = NULL;
+    retval->identifier = string;  // cached; don't copy string.
+    retval->index = 0;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_identifier_expr
+
+static MOJOSHADER_astExpression *new_callfunc_expr(Context *ctx,
+                                        const char *identifier,
+                                        MOJOSHADER_astArguments *args)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionCallFunction,
+                 MOJOSHADER_AST_OP_CALLFUNC);
+    MOJOSHADER_astExpression *expr = new_identifier_expr(ctx, identifier);
+    retval->datatype = NULL;
+    retval->identifier = (MOJOSHADER_astExpressionIdentifier *) expr;
+    retval->args = args;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_callfunc_expr
+
+static MOJOSHADER_astExpression *new_constructor_expr(Context *ctx,
+                                            const MOJOSHADER_astDataType *dt,
+                                            MOJOSHADER_astArguments *args)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionConstructor,
+                 MOJOSHADER_AST_OP_CONSTRUCTOR);
+    retval->datatype = dt;
+    retval->args = args;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_constructor_expr
+
+static MOJOSHADER_astExpression *new_cast_expr(Context *ctx,
+                                            const MOJOSHADER_astDataType *dt,
+                                            MOJOSHADER_astExpression *operand)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionCast, MOJOSHADER_AST_OP_CAST);
+    retval->datatype = dt;
+    retval->operand = operand;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_cast_expr
+
+static MOJOSHADER_astExpression *new_unary_expr(Context *ctx,
+                                            const MOJOSHADER_astNodeType op,
+                                            MOJOSHADER_astExpression *operand)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionUnary, op);
+    assert(operator_is_unary(op));
+    retval->datatype = NULL;
+    retval->operand = operand;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_unary_expr
+
+static MOJOSHADER_astExpression *new_binary_expr(Context *ctx,
+                                            const MOJOSHADER_astNodeType op,
+                                            MOJOSHADER_astExpression *left,
+                                            MOJOSHADER_astExpression *right)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionBinary, op);
+    assert(operator_is_binary(op));
+    retval->datatype = NULL;
+    retval->left = left;
+    retval->right = right;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_binary_expr
+
+static MOJOSHADER_astExpression *new_ternary_expr(Context *ctx,
+                                            const MOJOSHADER_astNodeType op,
+                                            MOJOSHADER_astExpression *left,
+                                            MOJOSHADER_astExpression *center,
+                                            MOJOSHADER_astExpression *right)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionTernary, op);
+    assert(operator_is_ternary(op));
+    assert(op == MOJOSHADER_AST_OP_CONDITIONAL);
+    retval->datatype = &ctx->dt_bool;
+    retval->left = left;
+    retval->center = center;
+    retval->right = right;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_ternary_expr
+
+static MOJOSHADER_astExpression *new_deref_struct_expr(Context *ctx,
+                                        MOJOSHADER_astExpression *identifier,
+                                        const char *member)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionDerefStruct,
+                 MOJOSHADER_AST_OP_DEREF_STRUCT);
+    retval->datatype = NULL;
+    retval->identifier = identifier;
+    retval->member = member;  // cached; don't copy string.
+    retval->isswizzle = 0;  // may change during semantic analysis.
+    retval->member_index = 0;  // set during semantic analysis.
+    return (MOJOSHADER_astExpression *) retval;
+} // new_deref_struct_expr
+
+static MOJOSHADER_astExpression *new_literal_int_expr(Context *ctx,
+                                                       const int value)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionIntLiteral,
+                 MOJOSHADER_AST_OP_INT_LITERAL);
+    retval->datatype = &ctx->dt_int;
+    retval->value = value;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_literal_int_expr
+
+static MOJOSHADER_astExpression *new_literal_float_expr(Context *ctx,
+                                                        const double dbl)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionFloatLiteral,
+                 MOJOSHADER_AST_OP_FLOAT_LITERAL);
+    retval->datatype = &ctx->dt_float;
+    retval->value = dbl;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_literal_float_expr
+
+static MOJOSHADER_astExpression *new_literal_string_expr(Context *ctx,
+                                                         const char *string)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionStringLiteral,
+                 MOJOSHADER_AST_OP_STRING_LITERAL);
+    retval->datatype = &ctx->dt_string;
+    retval->string = string;  // cached; don't copy string.
+    return (MOJOSHADER_astExpression *) retval;
+} // new_literal_string_expr
+
+static MOJOSHADER_astExpression *new_literal_boolean_expr(Context *ctx,
+                                                          const int value)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionBooleanLiteral,
+                 MOJOSHADER_AST_OP_BOOLEAN_LITERAL);
+    retval->datatype = &ctx->dt_bool;
+    retval->value = value;
+    return (MOJOSHADER_astExpression *) retval;
+} // new_literal_boolean_expr
+
+static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args);
+
+static void delete_expr(Context *ctx, MOJOSHADER_astExpression *_expr)
+{
+    MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
+
+    DELETE_AST_NODE(expr);
+
+    if (expr->ast.type == MOJOSHADER_AST_OP_CAST)
+        delete_expr(ctx, expr->cast.operand);
+
+    else if (expr->ast.type == MOJOSHADER_AST_OP_CONSTRUCTOR)
+        delete_arguments(ctx, expr->constructor.args);
+
+    else if (expr->ast.type == MOJOSHADER_AST_OP_DEREF_STRUCT)
+        delete_expr(ctx, expr->derefstruct.identifier);
+
+    else if (operator_is_unary(expr->ast.type))
+        delete_expr(ctx, expr->unary.operand);
+
+    else if (operator_is_binary(expr->ast.type))
+    {
+        delete_expr(ctx, expr->binary.left);
+        delete_expr(ctx, expr->binary.right);
+    } // else if
+
+    else if (operator_is_ternary(expr->ast.type))
+    {
+        delete_expr(ctx, expr->ternary.left);
+        delete_expr(ctx, expr->ternary.center);
+        delete_expr(ctx, expr->ternary.right);
+    } // else if
+
+    else if (expr->ast.type == MOJOSHADER_AST_OP_CALLFUNC)
+    {
+        delete_expr(ctx, (MOJOSHADER_astExpression*)expr->callfunc.identifier);
+        delete_arguments(ctx, expr->callfunc.args);
+    } // else if
+
+    // rest of operators don't have extra data to free.
+
+    Free(ctx, expr);
+} // delete_expr
+
+static MOJOSHADER_astArguments *new_argument(Context *ctx,
+                                             MOJOSHADER_astExpression *arg)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astArguments, MOJOSHADER_AST_ARGUMENTS);
+    retval->argument = arg;
+    retval->next = NULL;
+    return retval;
+} // new_argument
+
+static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args)
+{
+    DELETE_AST_NODE(args);
+    delete_arguments(ctx, args->next);
+    delete_expr(ctx, args->argument);
+    Free(ctx, args);
+} // delete_arguments
+
+static MOJOSHADER_astFunctionParameters *new_function_param(Context *ctx,
+                        const MOJOSHADER_astInputModifier inputmod,
+                        const MOJOSHADER_astDataType *dt,
+                        const char *identifier, const char *semantic,
+                        const MOJOSHADER_astInterpolationModifier interpmod,
+                        MOJOSHADER_astExpression *initializer)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astFunctionParameters,
+                 MOJOSHADER_AST_FUNCTION_PARAMS);
+    retval->datatype = dt;
+    retval->input_modifier = inputmod;
+    retval->identifier = identifier;
+    retval->semantic = semantic;
+    retval->interpolation_modifier = interpmod;
+    retval->initializer = initializer;
+    retval->next = NULL;
+    return retval;
+} // new_function_param
+
+static void delete_function_params(Context *ctx,
+                                   MOJOSHADER_astFunctionParameters *params)
+{
+    DELETE_AST_NODE(params);
+    delete_function_params(ctx, params->next);
+    delete_expr(ctx, params->initializer);
+    Free(ctx, params);
+} // delete_function_params
+
+static MOJOSHADER_astFunctionSignature *new_function_signature(Context *ctx,
+                                    const MOJOSHADER_astDataType *dt,
+                                    const char *identifier,
+                                    MOJOSHADER_astFunctionParameters *params)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astFunctionSignature,
+                 MOJOSHADER_AST_FUNCTION_SIGNATURE);
+    retval->datatype = dt;
+    retval->identifier = identifier;
+    retval->params = params;
+    retval->storage_class = MOJOSHADER_AST_FNSTORECLS_NONE;
+    retval->semantic = NULL;
+    return retval;
+} // new_function_signature
+
+static void delete_function_signature(Context *ctx,
+                                      MOJOSHADER_astFunctionSignature *sig)
+{
+    DELETE_AST_NODE(sig);
+    delete_function_params(ctx, sig->params);
+    Free(ctx, sig);
+} // delete_function_signature
+
+static MOJOSHADER_astCompilationUnit *new_function(Context *ctx,
+                                MOJOSHADER_astFunctionSignature *declaration,
+                                MOJOSHADER_astStatement *definition)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitFunction,
+                 MOJOSHADER_AST_COMPUNIT_FUNCTION);
+    retval->next = NULL;
+    retval->declaration = declaration;
+    retval->definition = definition;
+    retval->index = 0;
+    return (MOJOSHADER_astCompilationUnit *) retval;
+} // new_function
+
+static void delete_function(Context *ctx,
+                            MOJOSHADER_astCompilationUnitFunction *unitfn)
+{
+    DELETE_AST_NODE(unitfn);
+    delete_compilation_unit(ctx, unitfn->next);
+    delete_function_signature(ctx, unitfn->declaration);
+    delete_statement(ctx, unitfn->definition);
+    Free(ctx, unitfn);
+} // delete_function
+
+static MOJOSHADER_astScalarOrArray *new_scalar_or_array(Context *ctx,
+                                          const char *ident, const int isvec,
+                                          MOJOSHADER_astExpression *dim)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astScalarOrArray,
+                 MOJOSHADER_AST_SCALAR_OR_ARRAY);
+    retval->identifier = ident;
+    retval->isarray = isvec;
+    retval->dimension = dim;
+    return retval;
+} // new_scalar_or_array
+
+static void delete_scalar_or_array(Context *ctx,MOJOSHADER_astScalarOrArray *s)
+{
+    DELETE_AST_NODE(s);
+    delete_expr(ctx, s->dimension);
+    Free(ctx, s);
+} // delete_scalar_or_array
+
+static MOJOSHADER_astTypedef *new_typedef(Context *ctx, const int isconst,
+                                          const MOJOSHADER_astDataType *dt,
+                                          MOJOSHADER_astScalarOrArray *soa)
+{
+    // we correct this datatype to the final version during semantic analysis.
+    NEW_AST_NODE(retval, MOJOSHADER_astTypedef, MOJOSHADER_AST_TYPEDEF);
+    retval->datatype = dt;
+    retval->isconst = isconst;
+    retval->details = soa;
+    return retval;
+} // new_typedef
+
+static void delete_typedef(Context *ctx, MOJOSHADER_astTypedef *td)
+{
+    DELETE_AST_NODE(td);
+    delete_scalar_or_array(ctx, td->details);
+    Free(ctx, td);
+} // delete_typedef
+
+static MOJOSHADER_astPackOffset *new_pack_offset(Context *ctx,
+                                                 const char *a, const char *b)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astPackOffset, MOJOSHADER_AST_PACK_OFFSET);
+    retval->ident1 = a;
+    retval->ident2 = b;
+    return retval;
+} // new_pack_offset
+
+static void delete_pack_offset(Context *ctx, MOJOSHADER_astPackOffset *o)
+{
+    DELETE_AST_NODE(o);
+    Free(ctx, o);
+} // delete_pack_offset
+
+static MOJOSHADER_astVariableLowLevel *new_variable_lowlevel(Context *ctx,
+                                               MOJOSHADER_astPackOffset *po,
+                                               const char *reg)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astVariableLowLevel,
+                 MOJOSHADER_AST_VARIABLE_LOWLEVEL);
+    retval->packoffset = po;
+    retval->register_name = reg;
+    return retval;
+} // new_variable_lowlevel
+
+static void delete_variable_lowlevel(Context *ctx,
+                                     MOJOSHADER_astVariableLowLevel *vll)
+{
+    DELETE_AST_NODE(vll);
+    delete_pack_offset(ctx, vll->packoffset);
+    Free(ctx, vll);
+} // delete_variable_lowlevel
+
+static MOJOSHADER_astAnnotations *new_annotation(Context *ctx,
+                                        const MOJOSHADER_astDataType *dt,
+                                        MOJOSHADER_astExpression *initializer)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astAnnotations, MOJOSHADER_AST_ANNOTATION);
+    retval->datatype = dt;
+    retval->initializer = initializer;
+    retval->next = NULL;
+    return retval;
+} // new_annotation
+
+static void delete_annotation(Context *ctx, MOJOSHADER_astAnnotations *annos)
+{
+    DELETE_AST_NODE(annos);
+    delete_annotation(ctx, annos->next);
+    delete_expr(ctx, annos->initializer);
+    Free(ctx, annos);
+} // delete_annotation
+
+static MOJOSHADER_astVariableDeclaration *new_variable_declaration(
+                            Context *ctx, MOJOSHADER_astScalarOrArray *soa,
+                            const char *semantic,
+                            MOJOSHADER_astAnnotations *annotations,
+                            MOJOSHADER_astExpression *init,
+                            MOJOSHADER_astVariableLowLevel *vll)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astVariableDeclaration,
+                 MOJOSHADER_AST_VARIABLE_DECLARATION);
+    retval->datatype = NULL;
+    retval->attributes = 0;
+    retval->anonymous_datatype = NULL;
+    retval->details = soa;
+    retval->semantic = semantic;
+    retval->annotations = annotations;
+    retval->initializer = init;
+    retval->lowlevel = vll;
+    retval->next = NULL;
+    return retval;
+} // new_variable_declaration
+
+static void delete_variable_declaration(Context *ctx,
+                                        MOJOSHADER_astVariableDeclaration *dcl)
+{
+    DELETE_AST_NODE(dcl);
+    delete_variable_declaration(ctx, dcl->next);
+    delete_scalar_or_array(ctx, dcl->details);
+    delete_annotation(ctx, dcl->annotations);
+    delete_expr(ctx, dcl->initializer);
+    delete_variable_lowlevel(ctx, dcl->lowlevel);
+    Free(ctx, dcl);
+} // delete_variable_declaration
+
+static MOJOSHADER_astCompilationUnit *new_global_variable(Context *ctx,
+                                      MOJOSHADER_astVariableDeclaration *decl)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitVariable,
+                 MOJOSHADER_AST_COMPUNIT_VARIABLE);
+    retval->next = NULL;
+    retval->declaration = decl;
+    return (MOJOSHADER_astCompilationUnit *) retval;
+} // new_global_variable
+
+static void delete_global_variable(Context *ctx,
+                                   MOJOSHADER_astCompilationUnitVariable *var)
+{
+    DELETE_AST_NODE(var);
+    delete_compilation_unit(ctx, var->next);
+    delete_variable_declaration(ctx, var->declaration);
+    Free(ctx, var);
+} // delete_global_variable
+
+static MOJOSHADER_astCompilationUnit *new_global_typedef(Context *ctx,
+                                                     MOJOSHADER_astTypedef *td)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitTypedef,
+                 MOJOSHADER_AST_COMPUNIT_TYPEDEF);
+    retval->next = NULL;
+    retval->type_info = td;
+    return (MOJOSHADER_astCompilationUnit *) retval;
+} // new_global_typedef
+
+static void delete_global_typedef(Context *ctx,
+                                  MOJOSHADER_astCompilationUnitTypedef *unit)
+{
+    DELETE_AST_NODE(unit);
+    delete_compilation_unit(ctx, unit->next);
+    delete_typedef(ctx, unit->type_info);
+    Free(ctx, unit);
+} // delete_global_typedef
+
+static MOJOSHADER_astStructMembers *new_struct_member(Context *ctx,
+                                            MOJOSHADER_astScalarOrArray *soa,
+                                            const char *semantic)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astStructMembers,
+                 MOJOSHADER_AST_STRUCT_MEMBER);
+    retval->datatype = NULL;
+    retval->semantic = semantic;
+    retval->details = soa;
+    retval->interpolation_mod = MOJOSHADER_AST_INTERPMOD_NONE;
+    retval->next = NULL;
+    return retval;
+} // new_struct_member
+
+static void delete_struct_member(Context *ctx,
+                                 MOJOSHADER_astStructMembers *member)
+{
+    DELETE_AST_NODE(member);
+    delete_struct_member(ctx, member->next);
+    delete_scalar_or_array(ctx, member->details);
+    Free(ctx, member);
+} // delete_struct_member
+
+static MOJOSHADER_astStructDeclaration *new_struct_declaration(Context *ctx,
+                                        const char *name,
+                                        MOJOSHADER_astStructMembers *members)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astStructDeclaration,
+                 MOJOSHADER_AST_STRUCT_DECLARATION);
+    retval->datatype = NULL;
+    retval->name = name;
+    retval->members = members;
+    return retval;
+} // new_struct_declaration
+
+static void delete_struct_declaration(Context *ctx,
+                                      MOJOSHADER_astStructDeclaration *decl)
+{
+    DELETE_AST_NODE(decl);
+    delete_struct_member(ctx, decl->members);
+    Free(ctx, decl);
+} // delete_struct_declaration
+
+static MOJOSHADER_astCompilationUnit *new_global_struct(Context *ctx,
+                                           MOJOSHADER_astStructDeclaration *sd)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitStruct,
+                 MOJOSHADER_AST_COMPUNIT_STRUCT);
+    retval->next = NULL;
+    retval->struct_info = sd;
+    return (MOJOSHADER_astCompilationUnit *) retval;
+} // new_global_struct
+
+static void delete_global_struct(Context *ctx,
+                                 MOJOSHADER_astCompilationUnitStruct *unit)
+{
+    DELETE_AST_NODE(unit);
+    delete_compilation_unit(ctx, unit->next);
+    delete_struct_declaration(ctx, unit->struct_info);
+    Free(ctx, unit);
+} // delete_global_struct
+
+static void delete_compilation_unit(Context *ctx,
+                                    MOJOSHADER_astCompilationUnit *unit)
+{
+    if (!unit) return;
+
+    // it's important to not recurse too deeply here, since you may have
+    //  thousands of items in this linked list (each line of a massive
+    //  function, for example). To avoid this, we iterate the list here,
+    //  deleting all children and making them think they have no reason
+    //  to recurse in their own delete methods.
+    // Please note that everyone should _try_ to delete their "next" member,
+    //  just in case, but hopefully this cleaned it out.
+
+    MOJOSHADER_astCompilationUnit *i = unit->next;
+    unit->next = NULL;
+    while (i)
+    {
+        MOJOSHADER_astCompilationUnit *next = i->next;
+        i->next = NULL;
+        delete_compilation_unit(ctx, i);
+        i = next;
+    } // while
+
+    switch (unit->ast.type)
+    {
+        #define DELETE_UNIT(typ, cls, fn) \
+            case MOJOSHADER_AST_COMPUNIT_##typ: delete_##fn(ctx, (cls *) unit); break;
+        DELETE_UNIT(FUNCTION, MOJOSHADER_astCompilationUnitFunction, function);
+        DELETE_UNIT(TYPEDEF, MOJOSHADER_astCompilationUnitTypedef, global_typedef);
+        DELETE_UNIT(VARIABLE, MOJOSHADER_astCompilationUnitVariable, global_variable);
+        DELETE_UNIT(STRUCT, MOJOSHADER_astCompilationUnitStruct, global_struct);
+        #undef DELETE_UNIT
+        default: assert(0 && "missing cleanup code"); break;
+    } // switch
+
+    // don't free (unit) here, the class-specific functions do it.
+} // delete_compilation_unit
+
+static MOJOSHADER_astStatement *new_typedef_statement(Context *ctx,
+                                                      MOJOSHADER_astTypedef *td)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astTypedefStatement,
+                 MOJOSHADER_AST_STATEMENT_TYPEDEF);
+    retval->next = NULL;
+    retval->type_info = td;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_typedef_statement
+
+static void delete_typedef_statement(Context *ctx,
+                                     MOJOSHADER_astTypedefStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_typedef(ctx, stmt->type_info);
+    Free(ctx, stmt);
+} // delete_typedef_statement
+
+static MOJOSHADER_astStatement *new_return_statement(Context *ctx,
+                                                MOJOSHADER_astExpression *expr)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astReturnStatement,
+                 MOJOSHADER_AST_STATEMENT_RETURN);
+    retval->next = NULL;
+    retval->expr = expr;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_return_statement
+
+static void delete_return_statement(Context *ctx,
+                                    MOJOSHADER_astReturnStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_expr(ctx, stmt->expr);
+    Free(ctx, stmt);
+} // delete_return_statement
+
+static MOJOSHADER_astStatement *new_block_statement(Context *ctx,
+                                               MOJOSHADER_astStatement *stmts)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astBlockStatement,
+                 MOJOSHADER_AST_STATEMENT_BLOCK);
+    retval->next = NULL;
+    retval->statements = stmts;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_block_statement
+
+static void delete_block_statement(Context *ctx,
+                                   MOJOSHADER_astBlockStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->statements);
+    delete_statement(ctx, stmt->next);
+    Free(ctx, stmt);
+} // delete_statement_block
+
+static MOJOSHADER_astStatement *new_for_statement(Context *ctx,
+                                    MOJOSHADER_astVariableDeclaration *decl,
+                                    MOJOSHADER_astExpression *initializer,
+                                    MOJOSHADER_astExpression *looptest,
+                                    MOJOSHADER_astExpression *counter,
+                                    MOJOSHADER_astStatement *statement)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astForStatement,
+                 MOJOSHADER_AST_STATEMENT_FOR);
+    retval->next = NULL;
+    retval->unroll = -1;
+    retval->var_decl = decl;
+    retval->initializer = initializer;
+    retval->looptest = looptest;
+    retval->counter = counter;
+    retval->statement = statement;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_for_statement
+
+static void delete_for_statement(Context *ctx,MOJOSHADER_astForStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_variable_declaration(ctx, stmt->var_decl);
+    delete_expr(ctx, stmt->initializer);
+    delete_expr(ctx, stmt->looptest);
+    delete_expr(ctx, stmt->counter);
+    delete_statement(ctx, stmt->statement);
+    Free(ctx, stmt);
+} // delete_for_statement
+
+static MOJOSHADER_astStatement *new_do_statement(Context *ctx,
+                                                const int unroll,
+                                                MOJOSHADER_astStatement *stmt,
+                                                MOJOSHADER_astExpression *expr)
+{
+    NEW_AST_NODE(retval,MOJOSHADER_astDoStatement,MOJOSHADER_AST_STATEMENT_DO);
+    retval->next = NULL;
+    retval->unroll = unroll;
+    retval->expr = expr;
+    retval->statement = stmt;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_do_statement
+
+static void delete_do_statement(Context *ctx, MOJOSHADER_astDoStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_statement(ctx, stmt->statement);
+    delete_expr(ctx, stmt->expr);
+    Free(ctx, stmt);
+} // delete_do_statement
+
+static MOJOSHADER_astStatement *new_while_statement(Context *ctx,
+                                                const int unroll,
+                                                MOJOSHADER_astExpression *expr,
+                                                MOJOSHADER_astStatement *stmt)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astWhileStatement,
+                 MOJOSHADER_AST_STATEMENT_WHILE);
+    retval->next = NULL;
+    retval->unroll = unroll;
+    retval->expr = expr;
+    retval->statement = stmt;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_while_statement
+
+static void delete_while_statement(Context *ctx,
+                                   MOJOSHADER_astWhileStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_statement(ctx, stmt->statement);
+    delete_expr(ctx, stmt->expr);
+    Free(ctx, stmt);
+} // delete_while_statement
+
+static MOJOSHADER_astStatement *new_if_statement(Context *ctx,
+                                            const int attr,
+                                            MOJOSHADER_astExpression *expr,
+                                            MOJOSHADER_astStatement *stmt,
+                                            MOJOSHADER_astStatement *elsestmt)
+{
+    NEW_AST_NODE(retval,MOJOSHADER_astIfStatement,MOJOSHADER_AST_STATEMENT_IF);
+    retval->next = NULL;
+    retval->attributes = attr;
+    retval->expr = expr;
+    retval->statement = stmt;
+    retval->else_statement = elsestmt;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_if_statement
+
+static void delete_if_statement(Context *ctx, MOJOSHADER_astIfStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_expr(ctx, stmt->expr);
+    delete_statement(ctx, stmt->statement);
+    delete_statement(ctx, stmt->else_statement);
+    Free(ctx, stmt);
+} // delete_if_statement
+
+static MOJOSHADER_astSwitchCases *new_switch_case(Context *ctx,
+                                                MOJOSHADER_astExpression *expr,
+                                                MOJOSHADER_astStatement *stmt)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astSwitchCases, MOJOSHADER_AST_SWITCH_CASE);
+    retval->expr = expr;
+    retval->statement = stmt;
+    retval->next = NULL;
+    return retval;
+} // new_switch_case
+
+static void delete_switch_case(Context *ctx, MOJOSHADER_astSwitchCases *sc)
+{
+    DELETE_AST_NODE(sc);
+    delete_switch_case(ctx, sc->next);
+    delete_expr(ctx, sc->expr);
+    delete_statement(ctx, sc->statement);
+    Free(ctx, sc);
+} // delete_switch_case
+
+static MOJOSHADER_astStatement *new_empty_statement(Context *ctx)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astEmptyStatement,
+                 MOJOSHADER_AST_STATEMENT_EMPTY);
+    retval->next = NULL;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_empty_statement
+
+static void delete_empty_statement(Context *ctx,
+                                   MOJOSHADER_astEmptyStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    Free(ctx, stmt);
+} // delete_empty_statement
+
+static MOJOSHADER_astStatement *new_break_statement(Context *ctx)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astBreakStatement,
+                 MOJOSHADER_AST_STATEMENT_BREAK);
+    retval->next = NULL;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_break_statement
+
+static void delete_break_statement(Context *ctx,
+                                   MOJOSHADER_astBreakStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    Free(ctx, stmt);
+} // delete_break_statement
+
+static MOJOSHADER_astStatement *new_continue_statement(Context *ctx)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astContinueStatement,
+                 MOJOSHADER_AST_STATEMENT_CONTINUE);
+    retval->next = NULL;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_continue_statement
+
+static void delete_continue_statement(Context *ctx,
+                                      MOJOSHADER_astContinueStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    Free(ctx, stmt);
+} // delete_continue_statement
+
+static MOJOSHADER_astStatement *new_discard_statement(Context *ctx)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astDiscardStatement,
+                 MOJOSHADER_AST_STATEMENT_DISCARD);
+    retval->next = NULL;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_discard_statement
+
+static void delete_discard_statement(Context *ctx,
+                                     MOJOSHADER_astDiscardStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    Free(ctx, stmt);
+} // delete_discard_statement
+
+static MOJOSHADER_astStatement *new_expr_statement(Context *ctx,
+                                                MOJOSHADER_astExpression *expr)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astExpressionStatement,
+                 MOJOSHADER_AST_STATEMENT_EXPRESSION);
+    retval->next = NULL;
+    retval->expr = expr;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_expr_statement
+
+static void delete_expr_statement(Context *ctx,
+                                  MOJOSHADER_astExpressionStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_expr(ctx, stmt->expr);
+    Free(ctx, stmt);
+} // delete_expr_statement
+
+static MOJOSHADER_astStatement *new_switch_statement(Context *ctx,
+                                            const int attr,
+                                            MOJOSHADER_astExpression *expr,
+                                            MOJOSHADER_astSwitchCases *cases)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astSwitchStatement,
+                 MOJOSHADER_AST_STATEMENT_SWITCH);
+    retval->next = NULL;
+    retval->attributes = attr;
+    retval->expr = expr;
+    retval->cases = cases;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_switch_statement
+
+static void delete_switch_statement(Context *ctx,
+                                    MOJOSHADER_astSwitchStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_expr(ctx, stmt->expr);
+    delete_switch_case(ctx, stmt->cases);
+    Free(ctx, stmt);
+} // delete_switch_statement
+
+static MOJOSHADER_astStatement *new_struct_statement(Context *ctx,
+                                        MOJOSHADER_astStructDeclaration *sd)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astStructStatement,
+                 MOJOSHADER_AST_STATEMENT_STRUCT);
+    retval->next = NULL;
+    retval->struct_info = sd;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_struct_statement
+
+static void delete_struct_statement(Context *ctx,
+                                    MOJOSHADER_astStructStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_struct_declaration(ctx, stmt->struct_info);
+    Free(ctx, stmt);
+} // delete_struct_statement
+
+static MOJOSHADER_astStatement *new_vardecl_statement(Context *ctx,
+                                        MOJOSHADER_astVariableDeclaration *vd)
+{
+    NEW_AST_NODE(retval, MOJOSHADER_astVarDeclStatement,
+                 MOJOSHADER_AST_STATEMENT_VARDECL);
+    retval->next = NULL;
+    retval->declaration = vd;
+    return (MOJOSHADER_astStatement *) retval;
+} // new_vardecl_statement
+
+static void delete_vardecl_statement(Context *ctx,
+                                     MOJOSHADER_astVarDeclStatement *stmt)
+{
+    DELETE_AST_NODE(stmt);
+    delete_statement(ctx, stmt->next);
+    delete_variable_declaration(ctx, stmt->declaration);
+    Free(ctx, stmt);
+} // delete_vardecl_statement
+
+static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt)
+{
+    if (!stmt) return;
+
+    // it's important to not recurse too deeply here, since you may have
+    //  thousands of items in this linked list (each line of a massive
+    //  function, for example). To avoid this, we iterate the list here,
+    //  deleting all children and making them think they have no reason
+    //  to recurse in their own delete methods.
+    // Please note that everyone should _try_ to delete their "next" member,
+    //  just in case, but hopefully this cleaned it out.
+
+    MOJOSHADER_astStatement *i = stmt->next;
+    stmt->next = NULL;
+    while (i)
+    {
+        MOJOSHADER_astStatement *next = i->next;
+        i->next = NULL;
+        delete_statement(ctx, i);
+        i = next;
+    } // while
+
+    switch (stmt->ast.type)
+    {
+        #define DELETE_STATEMENT(typ, cls, fn) \
+            case MOJOSHADER_AST_STATEMENT_##typ: \
+                delete_##fn##_statement(ctx, (cls *) stmt); break;
+        DELETE_STATEMENT(BLOCK, MOJOSHADER_astBlockStatement, block);
+        DELETE_STATEMENT(EMPTY, MOJOSHADER_astEmptyStatement, empty);
+        DELETE_STATEMENT(IF, MOJOSHADER_astIfStatement, if);
+        DELETE_STATEMENT(SWITCH, MOJOSHADER_astSwitchStatement, switch);
+        DELETE_STATEMENT(EXPRESSION, MOJOSHADER_astExpressionStatement, expr);
+        DELETE_STATEMENT(FOR, MOJOSHADER_astForStatement, for);
+        DELETE_STATEMENT(DO, MOJOSHADER_astDoStatement, do);
+        DELETE_STATEMENT(WHILE, MOJOSHADER_astWhileStatement, while);
+        DELETE_STATEMENT(RETURN, MOJOSHADER_astReturnStatement, return);
+        DELETE_STATEMENT(BREAK, MOJOSHADER_astBreakStatement, break);
+        DELETE_STATEMENT(CONTINUE, MOJOSHADER_astContinueStatement, continue);
+        DELETE_STATEMENT(DISCARD, MOJOSHADER_astDiscardStatement, discard);
+        DELETE_STATEMENT(TYPEDEF, MOJOSHADER_astTypedefStatement, typedef);
+        DELETE_STATEMENT(STRUCT, MOJOSHADER_astStructStatement, struct);
+        DELETE_STATEMENT(VARDECL, MOJOSHADER_astVarDeclStatement, vardecl);
+        #undef DELETE_STATEMENT
+        default: assert(0 && "missing cleanup code"); break;
+    } // switch
+    // don't free (stmt) here, the class-specific functions do it.
+} // delete_statement
+
+
+static const MOJOSHADER_astDataType *get_usertype(const Context *ctx,
+                                                  const char *token)
+{
+    const void *value;  // search all scopes.
+    if (!hash_find(ctx->usertypes.hash, token, &value))
+        return NULL;
+    return value ? ((SymbolScope *) value)->datatype : NULL;
+} // get_usertype
+
+
+// This is where the actual parsing happens. It's Lemon-generated!
+#define __MOJOSHADER_HLSL_COMPILER__ 1
+#include "mojoshader_parser_hlsl.h"
+
+
+#if 0
+static int expr_is_constant(MOJOSHADER_astExpression *expr)
+{
+    const MOJOSHADER_astNodeType op = expr->ast.type;
+    if (operator_is_unary(op))
+        return expr_is_constant(expr->unary.operand);
+    else if (operator_is_binary(op))
+    {
+        return ( expr_is_constant(expr->binary.left) &&
+                 expr_is_constant(expr->binary.right) );
+    } // else if
+    else if (operator_is_ternary(op))
+    {
+        return ( expr_is_constant(expr->ternary.left) &&
+                 expr_is_constant(expr->ternary.center) &&
+                 expr_is_constant(expr->ternary.right) );
+    } // else if
+
+    return ( (op == MOJOSHADER_AST_OP_INT_LITERAL) ||
+             (op == MOJOSHADER_AST_OP_FLOAT_LITERAL) ||
+             (op == MOJOSHADER_AST_OP_STRING_LITERAL) ||
+             (op == MOJOSHADER_AST_OP_BOOLEAN_LITERAL) );
+} // expr_is_constant
+#endif
+
+typedef struct AstCalcData
+{
+    int isflt;
+    union
+    {
+        double f;
+        int64 i;
+    } value;
+} AstCalcData;
+
+// returns 0 if this expression is non-constant, 1 if it is.
+//  calculation results land in (data).
+static int calc_ast_const_expr(Context *ctx, void *_expr, AstCalcData *data)
+{
+    const MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
+    const MOJOSHADER_astNodeType op = expr->ast.type;
+
+    ctx->sourcefile = expr->ast.filename;
+    ctx->sourceline = expr->ast.line;
+
+    if (operator_is_unary(op))
+    {
+        if (!calc_ast_const_expr(ctx, expr->unary.operand, data))
+            return 0;
+
+        if (data->isflt)
+        {
+            switch (op)
+            {
+                case MOJOSHADER_AST_OP_NEGATE:
+                    data->value.f = -data->value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_NOT:
+                    data->value.f = !data->value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_COMPLEMENT:
+                    fail(ctx, "integer operation on floating point value");
+                    return 0;
+                case MOJOSHADER_AST_OP_CAST:
+                    // !!! FIXME: this should work, but it's complicated.
+                    assert(0 && "write me");
+                    return 0;
+                default: break;
+            } // switch
+        } // if
+
+        else  // integer version
+        {
+            switch (op)
+            {
+                case MOJOSHADER_AST_OP_NEGATE:
+                    data->value.i = -data->value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_NOT:
+                    data->value.i = !data->value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_COMPLEMENT:
+                    data->value.i = ~data->value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_CAST:
+                    // !!! FIXME: this should work, but it's complicated.
+                    assert(0 && "write me");
+                    return 0;
+                default: break;
+            } // switch
+        } // else
+        assert(0 && "unhandled operation?");
+        return 0;
+    } // if
+
+    else if (operator_is_binary(op))
+    {
+        AstCalcData subdata2;
+        if ( (!calc_ast_const_expr(ctx, expr->binary.left, data)) ||
+             (!calc_ast_const_expr(ctx, expr->binary.right, &subdata2)) )
+            return 0;
+
+        // upgrade to float if either operand is float.
+        if ((data->isflt) || (subdata2.isflt))
+        {
+            if (!data->isflt) data->value.f = (double) data->value.i;
+            if (!subdata2.isflt) subdata2.value.f = (double) subdata2.value.i;
+            data->isflt = subdata2.isflt = 1;
+        } // if
+
+        switch (op)
+        {
+            // gcc doesn't handle commas here, either (fails to parse!).
+            case MOJOSHADER_AST_OP_COMMA:
+            case MOJOSHADER_AST_OP_ASSIGN:
+            case MOJOSHADER_AST_OP_MULASSIGN:
+            case MOJOSHADER_AST_OP_DIVASSIGN:
+            case MOJOSHADER_AST_OP_MODASSIGN:
+            case MOJOSHADER_AST_OP_ADDASSIGN:
+            case MOJOSHADER_AST_OP_SUBASSIGN:
+            case MOJOSHADER_AST_OP_LSHIFTASSIGN:
+            case MOJOSHADER_AST_OP_RSHIFTASSIGN:
+            case MOJOSHADER_AST_OP_ANDASSIGN:
+            case MOJOSHADER_AST_OP_XORASSIGN:
+            case MOJOSHADER_AST_OP_ORASSIGN:
+                return 0;  // assignment is non-constant.
+            default: break;
+        } // switch
+
+        if (data->isflt)
+        {
+            switch (op)
+            {
+                case MOJOSHADER_AST_OP_MULTIPLY:
+                    data->value.f *= subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_DIVIDE:
+                    data->value.f /= subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_ADD:
+                    data->value.f += subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_SUBTRACT:
+                    data->value.f -= subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_LESSTHAN:
+                    data->isflt = 0;
+                    data->value.i = data->value.f < subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_GREATERTHAN:
+                    data->isflt = 0;
+                    data->value.i = data->value.f > subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_LESSTHANOREQUAL:
+                    data->isflt = 0;
+                    data->value.i = data->value.f <= subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_GREATERTHANOREQUAL:
+                    data->isflt = 0;
+                    data->value.i = data->value.f >= subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_EQUAL:
+                    data->isflt = 0;
+                    data->value.i = data->value.f == subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_NOTEQUAL:
+                    data->isflt = 0;
+                    data->value.i = data->value.f != subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_LOGICALAND:
+                    data->isflt = 0;
+                    data->value.i = data->value.f && subdata2.value.f;
+                    return 1;
+                case MOJOSHADER_AST_OP_LOGICALOR:
+                    data->isflt = 0;
+                    data->value.i = data->value.f || subdata2.value.f;
+                    return 1;
+
+                case MOJOSHADER_AST_OP_LSHIFT:
+                case MOJOSHADER_AST_OP_RSHIFT:
+                case MOJOSHADER_AST_OP_MODULO:
+                case MOJOSHADER_AST_OP_BINARYAND:
+                case MOJOSHADER_AST_OP_BINARYXOR:
+                case MOJOSHADER_AST_OP_BINARYOR:
+                    fail(ctx, "integer operation on floating point value");
+                    return 0;
+                default: break;
+            } // switch
+        } // if
+
+        else   // integer version.
+        {
+            switch (op)
+            {
+                case MOJOSHADER_AST_OP_MULTIPLY:
+                    data->value.i *= subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_DIVIDE:
+                    data->value.i /= subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_ADD:
+                    data->value.i += subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_SUBTRACT:
+                    data->value.i -= subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_LESSTHAN:
+                    data->value.i = data->value.i < subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_GREATERTHAN:
+                    data->value.i = data->value.i > subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_LESSTHANOREQUAL:
+                    data->value.i = data->value.i <= subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_GREATERTHANOREQUAL:
+                    data->value.i = data->value.i >= subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_EQUAL:
+                    data->value.i = data->value.i == subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_NOTEQUAL:
+                    data->value.i = data->value.i != subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_LOGICALAND:
+                    data->value.i = data->value.i && subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_LOGICALOR:
+                    data->value.i = data->value.i || subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_LSHIFT:
+                    data->value.i = data->value.i << subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_RSHIFT:
+                    data->value.i = data->value.i >> subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_MODULO:
+                    data->value.i = data->value.i % subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_BINARYAND:
+                    data->value.i = data->value.i & subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_BINARYXOR:
+                    data->value.i = data->value.i ^ subdata2.value.i;
+                    return 1;
+                case MOJOSHADER_AST_OP_BINARYOR:
+                    data->value.i = data->value.i | subdata2.value.i;
+                    return 1;
+                default: break;
+            } // switch
+        } // else
+
+        assert(0 && "unhandled operation?");
+        return 0;
+    } // else if
+
+    else if (operator_is_ternary(op))
+    {
+        AstCalcData subdata2;
+        AstCalcData subdata3;
+
+        assert(op == MOJOSHADER_AST_OP_CONDITIONAL);  // only one we have.
+
+        if ( (!calc_ast_const_expr(ctx, expr->ternary.left, data)) ||
+             (!calc_ast_const_expr(ctx, expr->ternary.center, &subdata2)) ||
+             (!calc_ast_const_expr(ctx, expr->ternary.right, &subdata3)) )
+            return 0;
+
+        // first operand should be bool (for the one ternary operator we have).
+        if (data->isflt)
+        {
+            data->isflt = 0;
+            data->value.i = (int64) subdata3.value.f;
+        } // if
+
+        // upgrade to float if either operand is float.
+        if ((subdata2.isflt) || (subdata3.isflt))
+        {
+            if (!subdata2.isflt) subdata2.value.f = (double) subdata2.value.i;
+            if (!subdata3.isflt) subdata3.value.f = (double) subdata3.value.i;
+            subdata2.isflt = subdata3.isflt = 1;
+        } // if
+
+        data->isflt = subdata2.isflt;
+        if (data->isflt)
+            data->value.f = data->value.i ? subdata2.value.f : subdata3.value.f;
+        else
+            data->value.i = data->value.i ? subdata2.value.i : subdata3.value.i;
+        return 1;
+    } // else if
+
+    else  // not an operator? See if this is a literal value.
+    {
+        switch (op)
+        {
+            case MOJOSHADER_AST_OP_INT_LITERAL:
+                data->isflt = 0;
+                data->value.i = expr->intliteral.value;
+                return 1;
+
+            case MOJOSHADER_AST_OP_FLOAT_LITERAL:
+                data->isflt = 1;
+                data->value.f = expr->floatliteral.value;
+                return 1;
+
+            case MOJOSHADER_AST_OP_BOOLEAN_LITERAL:
+                data->isflt = 0;
+                data->value.i = expr->boolliteral.value ? 1 : 0;
+                return 1;
+
+            default: break;
+        } // switch
+    } // switch
+
+    return 0;  // not constant, or unhandled.
+} // calc_ast_const_expr
+
+
+static const MOJOSHADER_astDataType *reduce_datatype(Context *ctx, const MOJOSHADER_astDataType *dt)
+{
+    const MOJOSHADER_astDataType *retval = dt;
+    while (retval && retval->type == MOJOSHADER_AST_DATATYPE_USER)
+    {
+        // !!! FIXME: Ugh, const removal.
+        MOJOSHADER_astDataTypeUser *user = (MOJOSHADER_astDataTypeUser *) &retval->user;
+        if (user->details->type == MOJOSHADER_AST_DATATYPE_NONE)
+        {
+            // Take this opportunity to fix up some usertype stubs that were
+            //  left over from the parse phase. You HAVE to catch these in the
+            //  right scope, so be aggressive about calling reduce_datatype()
+            //  as soon as things come into view!
+            user->details = get_usertype(ctx, user->name);
+            assert(user->details != NULL);
+        } // if
+
+        retval = user->details;
+    } // while
+
+    return retval;
+} // reduce_datatype
+
+
+static inline const MOJOSHADER_astDataType *sanitize_datatype(Context *ctx, const MOJOSHADER_astDataType *dt)
+{
+    reduce_datatype(ctx, dt);
+    return dt;
+} // sanitize_datatype
+
+
+static const MOJOSHADER_astDataType *build_function_datatype(Context *ctx,
+                                        const MOJOSHADER_astDataType *rettype,
+                                        const int paramcount,
+                                        const MOJOSHADER_astDataType **params,
+                                        const int intrinsic)
+{
+    if ( ((paramcount > 0) && (params == NULL)) ||
+         ((paramcount == 0) && (params != NULL)) )
+        return NULL;
+
+    // !!! FIXME: this is hacky.
+    const MOJOSHADER_astDataType **dtparams;
+    void *ptr = Malloc(ctx, sizeof (*params) * paramcount);
+    if (ptr == NULL)
+        return NULL;
+    if (!buffer_append(ctx->garbage, &ptr, sizeof (ptr)))
+    {
+        Free(ctx, ptr);
+        return NULL;
+    } // if
+    dtparams = (const MOJOSHADER_astDataType **) ptr;
+    memcpy(dtparams, params, sizeof (*params) * paramcount);
+
+    ptr = Malloc(ctx, sizeof (MOJOSHADER_astDataType));
+    if (ptr == NULL)
+        return NULL;
+    if (!buffer_append(ctx->garbage, &ptr, sizeof (ptr)))
+    {
+        Free(ctx, ptr);
+        return NULL;
+    } // if
+
+    MOJOSHADER_astDataType *dt = (MOJOSHADER_astDataType *) ptr;
+    dt->type = MOJOSHADER_AST_DATATYPE_FUNCTION;
+    dt->function.retval = rettype;
+    dt->function.params = dtparams;
+    dt->function.num_params = paramcount;
+    dt->function.intrinsic = intrinsic;
+    return dt;
+} // build_function_datatype
+
+
+static const MOJOSHADER_astDataType *build_datatype(Context *ctx,
+                                            const int isconst,
+                                            const MOJOSHADER_astDataType *dt,
+                                            MOJOSHADER_astScalarOrArray *soa)
+{
+    MOJOSHADER_astDataType *retval = NULL;
+
+    assert( (soa->isarray && soa->dimension) ||
+            (!soa->isarray && !soa->dimension) );
+
+    sanitize_datatype(ctx, dt);
+
+    // see if we can just reuse the exist datatype.
+    if (!soa->isarray)
+    {
+        const int c1 = (dt->type & MOJOSHADER_AST_DATATYPE_CONST) != 0;
+        const int c2 = (isconst != 0);
+        if (c1 == c2)
+            return dt;  // reuse existing datatype!
+    } // if
+
+    retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
+    if (retval == NULL)
+        return NULL;
+
+    // !!! FIXME: this is hacky.
+    if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
+    {
+        Free(ctx, retval);
+        return NULL;
+    } // if
+
+    if (!soa->isarray)
+    {
+        assert(soa->dimension == NULL);
+        memcpy(retval, dt, sizeof (MOJOSHADER_astDataType));
+        if (isconst)
+            retval->type = (MOJOSHADER_astDataTypeType)((unsigned)retval->type | MOJOSHADER_AST_DATATYPE_CONST);
+        else
+            retval->type = (MOJOSHADER_astDataTypeType)((unsigned)retval->type & ~MOJOSHADER_AST_DATATYPE_CONST);
+        return retval;
+    } // if
+
+    retval->type = MOJOSHADER_AST_DATATYPE_ARRAY;
+    retval->array.base = dt;
+    if (soa->dimension == NULL)
+    {
+        retval->array.elements = -1;
+        return retval;
+    } // if
+
+    // Run the expression to verify it's constant and produces a positive int.
+    AstCalcData data;
+    data.isflt = 0;
+    data.value.i = 0;
+    retval->array.elements = 16;  // sane default for failure.
+    const int ok = calc_ast_const_expr(ctx, soa->dimension, &data);
+
+    // reset error position.
+    ctx->sourcefile = soa->ast.filename;
+    ctx->sourceline = soa->ast.line;
+
+    if (!ok)
+        fail(ctx, "array dimensions not constant");
+    else if (data.isflt)
+        fail(ctx, "array dimensions not integer");
+    else if (data.value.i < 0)
+        fail(ctx, "array dimensions negative");
+    else
+        retval->array.elements = data.value.i;
+
+    return retval;
+} // build_datatype
+
+
+static void require_numeric_datatype(Context *ctx,
+                                     const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    if (datatype->type == MOJOSHADER_AST_DATATYPE_VECTOR)
+        datatype = reduce_datatype(ctx, datatype->vector.base);
+    else if (datatype->type == MOJOSHADER_AST_DATATYPE_MATRIX)
+        datatype = reduce_datatype(ctx, datatype->matrix.base);
+
+    switch (datatype->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL:
+        case MOJOSHADER_AST_DATATYPE_INT:
+        case MOJOSHADER_AST_DATATYPE_UINT:
+        case MOJOSHADER_AST_DATATYPE_HALF:
+        case MOJOSHADER_AST_DATATYPE_FLOAT:
+        case MOJOSHADER_AST_DATATYPE_DOUBLE:
+            return;
+        default: break;
+    } // switch
+
+    fail(ctx, "Expected numeric type");  // !!! FIXME: fmt.
+    // !!! FIXME: replace AST node with an AST_OP_INT_LITERAL zero, keep going.
+} // require_numeric_datatype
+
+static void require_integer_datatype(Context *ctx,
+                                     const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    switch (datatype->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_INT:
+        case MOJOSHADER_AST_DATATYPE_UINT:
+            return;
+        default: break;
+    } // switch
+
+    fail(ctx, "Expected integer type");  // !!! FIXME: fmt.
+    // !!! FIXME: replace AST node with an AST_OP_INT_LITERAL zero, keep going.
+} // require_integer_datatype
+
+static void require_boolean_datatype(Context *ctx,
+                                     const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    switch (datatype->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL:
+        case MOJOSHADER_AST_DATATYPE_INT:
+        case MOJOSHADER_AST_DATATYPE_UINT:
+            return;
+        default: break;
+    } // switch
+
+    fail(ctx, "Expected boolean type");  // !!! FIXME: fmt.
+    // !!! FIXME: replace AST node with an AST_OP_BOOLEAN_LITERAL false, keep going.
+} // require_numeric_datatype
+
+
+static void require_array_datatype(Context *ctx,
+                                   const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    if (datatype->type == MOJOSHADER_AST_DATATYPE_ARRAY)
+        return;
+
+    fail(ctx, "expected array");
+    // !!! FIXME: delete array dereference for further processing.
+} // require_array_datatype
+
+
+static void require_struct_datatype(Context *ctx,
+                                    const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    if (datatype->type == MOJOSHADER_AST_DATATYPE_STRUCT)
+        return;
+
+    fail(ctx, "expected struct");
+    // !!! FIXME: delete struct dereference for further processing.
+} // require_struct_datatype
+
+
+static int require_function_datatype(Context *ctx,
+                                     const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    if ((!datatype) || (datatype->type != MOJOSHADER_AST_DATATYPE_FUNCTION))
+    {
+        fail(ctx, "expected function");
+        return 0;
+    } // if
+
+    return 1;
+} // require_function_datatype
+
+
+// Extract the individual element type from an array datatype.
+static const MOJOSHADER_astDataType *array_element_datatype(Context *ctx,
+                                        const MOJOSHADER_astDataType *datatype)
+{
+    datatype = reduce_datatype(ctx, datatype);
+    assert(datatype->type == MOJOSHADER_AST_DATATYPE_ARRAY);
+    return datatype->array.base;
+} // array_element_datatype
+
+
+// This tests two datatypes to see if they are compatible, and adds cast
+//  operator nodes to the AST if the program was relying on implicit
+//  casts between then. Will fail() if the datatypes can't be coerced
+//  with a cast at all. (left) can be NULL to say that its datatype is
+//  set in stone (an lvalue, for example). No other NULLs are allowed.
+// Returns final datatype used once implicit casting is complete.
+// The datatypes must be pointers from the string cache.
+static const MOJOSHADER_astDataType *add_type_coercion(Context *ctx,
+                                     MOJOSHADER_astExpression **left,
+                                     const MOJOSHADER_astDataType *_ldatatype,
+                                     MOJOSHADER_astExpression **right,
+                                     const MOJOSHADER_astDataType *_rdatatype)
+{
+    // !!! FIXME: this whole function is probably naive at best.
+    const MOJOSHADER_astDataType *ldatatype = reduce_datatype(ctx, _ldatatype);
+    const MOJOSHADER_astDataType *rdatatype = reduce_datatype(ctx, _rdatatype);
+
+    if (ldatatype == rdatatype)
+        return ldatatype;   // they already match, so we're done.
+
+    struct {
+        const MOJOSHADER_astDataTypeType type;
+        const int bits;
+        const int is_unsigned;
+        const int floating;
+    } typeinf[] = {
+        { MOJOSHADER_AST_DATATYPE_BOOL,    1, 1, 0 },
+        { MOJOSHADER_AST_DATATYPE_HALF,   16, 0, 1 },
+        { MOJOSHADER_AST_DATATYPE_INT,    32, 0, 0 },
+        { MOJOSHADER_AST_DATATYPE_UINT,   32, 1, 0 },
+        { MOJOSHADER_AST_DATATYPE_FLOAT,  32, 0, 1 },
+        { MOJOSHADER_AST_DATATYPE_DOUBLE, 64, 0, 1 },
+    };
+
+    int lvector = 0;
+    int lmatrix = 0;
+    int l = STATICARRAYLEN(typeinf);
+    if (ldatatype != NULL)
+    {
+        MOJOSHADER_astDataTypeType type = ldatatype->type;
+        if (type == MOJOSHADER_AST_DATATYPE_VECTOR)
+        {
+            lvector = 1;
+            type = ldatatype->vector.base->type;
+        } // if
+        else if (type == MOJOSHADER_AST_DATATYPE_MATRIX)
+        {
+            lmatrix = 1;
+            type = ldatatype->matrix.base->type;
+        } // if
+
+        for (l = 0; l < STATICARRAYLEN(typeinf); l++)
+        {
+            if (typeinf[l].type == type)
+                break;
+        } // for
+    } // if
+
+    int rvector = 0;
+    int rmatrix = 0;
+    int r = STATICARRAYLEN(typeinf);
+    if (rdatatype != NULL)
+    {
+        MOJOSHADER_astDataTypeType type = rdatatype->type;
+        if (type == MOJOSHADER_AST_DATATYPE_VECTOR)
+        {
+            rvector = 1;
+            type = rdatatype->vector.base->type;
+        } // if
+        else if (type == MOJOSHADER_AST_DATATYPE_MATRIX)
+        {
+            rmatrix = 1;
+            type = rdatatype->matrix.base->type;
+        } // if
+
+        for (r = 0; r < STATICARRAYLEN(typeinf); r++)
+        {
+            if (typeinf[r].type == type)
+                break;
+        } // for
+    } // if
+
+    enum { CHOOSE_NEITHER, CHOOSE_LEFT, CHOOSE_RIGHT } choice = CHOOSE_NEITHER;
+    if ((l < STATICARRAYLEN(typeinf)) && (r < STATICARRAYLEN(typeinf)))
+    {
+        if (left == NULL)
+            choice = CHOOSE_LEFT;  // we need to force to the lvalue.
+        else if (lmatrix && !rmatrix)
+            choice = CHOOSE_LEFT;
+        else if (!lmatrix && rmatrix)
+            choice = CHOOSE_RIGHT;
+        else if (lvector && !rvector)
+            choice = CHOOSE_LEFT;
+        else if (!lvector && rvector)
+            choice = CHOOSE_RIGHT;
+        else if (typeinf[l].bits > typeinf[r].bits)
+            choice = CHOOSE_LEFT;
+        else if (typeinf[l].bits < typeinf[r].bits)
+            choice = CHOOSE_RIGHT;
+        else if (typeinf[l].floating && !typeinf[r].floating)
+            choice = CHOOSE_LEFT;
+        else if (!typeinf[l].floating && typeinf[r].floating)
+            choice = CHOOSE_RIGHT;
+        else if (typeinf[l].is_unsigned && !typeinf[r].is_unsigned)
+            choice = CHOOSE_LEFT;
+        else if (!typeinf[l].is_unsigned && typeinf[r].is_unsigned)
+            choice = CHOOSE_RIGHT;
+    } // if
+
+    if (choice == CHOOSE_LEFT)
+    {
+        *right = new_cast_expr(ctx, _ldatatype, *right);
+        return _ldatatype;
+    } // if
+    else if (choice == CHOOSE_RIGHT)
+    {
+        *left = new_cast_expr(ctx, _rdatatype, *left);
+        return _rdatatype;
+    } // else if
+
+    assert(choice == CHOOSE_NEITHER);
+    fail(ctx, "incompatible data types");
+    // Ditch original (*right), force a literal value that matches
+    //  ldatatype, so further processing is normalized.
+    // !!! FIXME: force (right) to match (left).
+    delete_expr(ctx, *right);
+    *right = new_cast_expr(ctx, _ldatatype, new_literal_int_expr(ctx, 0));
+    return ldatatype;
+} // add_type_coercion
+
+static int is_swizzle_str(const char *str, const int veclen)
+{
+    int i;
+    int is_xyzw = 0;
+    int is_rgba = 0;
+
+    assert(*str != '\0');  // can this actually happen?
+
+    for (i = 0; i < veclen; i++, str++)
+    {
+        const char ch = *str;
+        if (ch == '\0')
+            break;
+        else if ((ch == 'x') || (ch == 'y') || (ch == 'z') || (ch == 'w'))
+            is_xyzw = 1;
+        else if ((ch == 'r') || (ch == 'g') || (ch == 'b') || (ch == 'a'))
+            is_rgba = 1;
+    } // for
+
+    if (*str != '\0')  // must be end of string here.
+        return 0;  // not a swizzle.
+    return ((is_rgba + is_xyzw) == 1);  // can only be one or the other.
+} // is_swizzle_str
+
+static int datatype_size(const MOJOSHADER_astDataType *dt)
+{
+    switch (dt->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL: return 1;
+        case MOJOSHADER_AST_DATATYPE_INT: return 4;
+        case MOJOSHADER_AST_DATATYPE_UINT: return 4;
+        case MOJOSHADER_AST_DATATYPE_FLOAT: return 4;
+        case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM: return 4;
+        case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM: return 4;
+        case MOJOSHADER_AST_DATATYPE_HALF: return 2;
+        case MOJOSHADER_AST_DATATYPE_DOUBLE: return 8;
+            return 1;
+        default:
+            assert(0 && "Maybe should have used reduce_datatype()?");
+            return 0;
+    } // switch
+} // datatype_size
+
+static inline int is_scalar_datatype(const MOJOSHADER_astDataType *dt)
+{
+    switch (dt->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL:
+        case MOJOSHADER_AST_DATATYPE_INT:
+        case MOJOSHADER_AST_DATATYPE_UINT:
+        case MOJOSHADER_AST_DATATYPE_FLOAT:
+        case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
+        case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
+        case MOJOSHADER_AST_DATATYPE_HALF:
+        case MOJOSHADER_AST_DATATYPE_DOUBLE:
+            return 1;
+        default:
+            return 0;
+    } // switch
+} // is_scalar_datatype
+
+static inline int is_float_datatype(const MOJOSHADER_astDataType *dt)
+{
+    switch (dt->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_FLOAT: return 1;
+        case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM: return 1;
+        case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM: return 1;
+        default: return 0;
+    } // switch
+} // is_float_datatype
+
+static int datatype_elems(Context *ctx, const MOJOSHADER_astDataType *dt)
+{
+    dt = reduce_datatype(ctx, dt);
+    switch (dt->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_VECTOR:
+            return dt->vector.elements;
+        case MOJOSHADER_AST_DATATYPE_MATRIX:
+            return dt->matrix.rows * dt->matrix.columns;
+        default:
+            return 1;
+    } // switch
+} // datatype_elems
+
+static const MOJOSHADER_astDataType *datatype_base(Context *ctx, const MOJOSHADER_astDataType *dt)
+{
+    dt = reduce_datatype(ctx, dt);
+    if (dt == NULL)
+        return dt;
+
+    switch (dt->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_VECTOR:
+            dt = dt->vector.base;
+            break;
+        case MOJOSHADER_AST_DATATYPE_MATRIX:
+            dt = dt->matrix.base;
+            break;
+        case MOJOSHADER_AST_DATATYPE_BUFFER:
+            dt = dt->buffer.base;
+            break;
+        case MOJOSHADER_AST_DATATYPE_ARRAY:
+            dt = dt->array.base;
+            break;
+        default: break;
+    } // switch
+
+    return dt;
+} // datatype_base
+
+typedef enum
+{
+    DT_MATCH_INCOMPATIBLE,         // flatly incompatible
+    DT_MATCH_COMPATIBLE_DOWNCAST,  // would have to lose precision
+    DT_MATCH_COMPATIBLE_UPCAST,    // would have to gain precision
+    DT_MATCH_COMPATIBLE,           // can cast to without serious change.
+    DT_MATCH_PERFECT               // identical datatype.
+} DatatypeMatch;
+
+static DatatypeMatch compatible_arg_datatype(Context *ctx,
+                                   const MOJOSHADER_astDataType *arg,
+                                   const MOJOSHADER_astDataType *param)
+{
+    // The matching rules for HLSL function overloading, as far as I can
+    //  tell from experimenting with Microsoft's compiler, seem to be this:
+    //
+    // - All parameters of a function must match what the caller specified
+    //   after possible type promotion via the following rules.
+    // - If the number of arguments and the number of parameters don't match,
+    //   that overload is immediately rejected.
+    // - Each overloaded function is given a score that is the sum of the
+    //   "worth" of each parameter vs the caller's arguments
+    //   (see DatatypeMatch). The higher the score, the more favorable this
+    //   function overload would be.
+    // - If there is a tie for highest score between two or more function
+    //   overloads, we declare that function call to be ambiguous and fail().
+    // - Scalars can be promoted to vectors to make a parameter match.
+    // - Scalars can promote to other scalars (short to int, etc).
+    // - Datatypes can downcast, but should generate a warning.
+    //   (calling void fn(float x); as fn((double)1.0) should warn).
+    // - Vectors may NOT be extend (a float2 can't implicity extend to a
+    //   float4).
+    // - Vectors with the same elements can promote (a half2 can become
+    //   a float2). Downcasting between vectors with the same number of
+    //   elements is allowed.
+    // - A perfect match of all params will be favored over any functions
+    //   that only match if type promotion is applied (given a perfect match
+    //   of all parameters, we'll stop looking for other matches).
+
+    if (datatypes_match(arg, param))
+        return DT_MATCH_PERFECT;  // that was easy.
+
+    arg = reduce_datatype(ctx, arg);
+    param = reduce_datatype(ctx, param);
+
+    int do_base_test = 0;
+
+    if (is_scalar_datatype(arg))
+        do_base_test = 1; // we let these all go through for now.
+
+    else if (arg->type == param->type)
+    {
+        if (arg->type == MOJOSHADER_AST_DATATYPE_VECTOR)
+            do_base_test = (arg->vector.elements == param->vector.elements);
+        else if (arg->type == MOJOSHADER_AST_DATATYPE_MATRIX)
+        {
+            do_base_test =
+                ((arg->matrix.rows == param->matrix.rows) &&
+                 (arg->matrix.columns == param->matrix.columns));
+        } // if
+    } // if
+
+    if (do_base_test)
+    {
+        arg = datatype_base(ctx, arg);
+        param = datatype_base(ctx, param);
+
+        const int argsize = datatype_size(arg);
+        const int paramsize = datatype_size(param);
+        const int argfloat = is_float_datatype(arg);
+        const int paramfloat = is_float_datatype(param);
+
+        if (argfloat && !paramfloat)
+            return DT_MATCH_COMPATIBLE_DOWNCAST;  // always loss of precision.
+        else if (argfloat && !paramfloat)
+        {
+            if (argsize < paramsize)
+                return DT_MATCH_COMPATIBLE_UPCAST;
+            else
+                return DT_MATCH_COMPATIBLE_DOWNCAST;  // loss of precision.
+        } // else if
+        else if (argsize == paramsize)
+            return DT_MATCH_COMPATIBLE;
+        else if (argsize < paramsize)
+            return DT_MATCH_COMPATIBLE_UPCAST;
+        else /* if (argsize > paramsize) */
+            return DT_MATCH_COMPATIBLE_DOWNCAST;
+    } // if
+
+    return DT_MATCH_INCOMPATIBLE;
+} // compatible_arg_datatype
+
+
+static const MOJOSHADER_astDataType *type_check_ast(Context *ctx, void *_ast);
+
+// !!! FIXME: this function sucks.
+static const MOJOSHADER_astDataType *match_func_to_call(Context *ctx,
+                                    MOJOSHADER_astExpressionCallFunction *ast)
+{
+    SymbolScope *best = NULL;  // best choice we find.
+    int best_score = 0;
+    MOJOSHADER_astExpressionIdentifier *ident = ast->identifier;
+    const char *sym = ident->identifier;
+    const void *value = NULL;
+    void *iter = NULL;
+
+    int argcount = 0;
+    MOJOSHADER_astArguments *args = ast->args;
+    while (args != NULL)
+    {
+        argcount++;
+        type_check_ast(ctx, args->argument);
+        args = args->next;
+    } // while;
+
+    // we do some tapdancing to handle function overloading here.
+    int match = 0;
+    while (hash_iter(ctx->variables.hash, sym, &value, &iter))
+    {
+        SymbolScope *item = (SymbolScope *) value;
+        const MOJOSHADER_astDataType *dt = item->datatype;
+        dt = reduce_datatype(ctx, dt);
+        // there's a locally-scoped symbol with this name? It takes precedence.
+        if (dt->type != MOJOSHADER_AST_DATATYPE_FUNCTION)
+            return dt;
+
+        const MOJOSHADER_astDataTypeFunction *dtfn = (MOJOSHADER_astDataTypeFunction *) dt;
+        const int perfect = argcount * ((int) DT_MATCH_PERFECT);
+        int score = 0;
+
+        if (argcount == dtfn->num_params)  // !!! FIXME: default args.
+        {
+            args = ast->args;
+            int i;
+            for (i = 0; i < argcount; i++)
+            {
+                assert(args != NULL);
+                dt = args->argument->datatype;
+                args = args->next;
+                const DatatypeMatch compatible = compatible_arg_datatype(ctx, dt, dtfn->params[i]);
+                if (compatible == DT_MATCH_INCOMPATIBLE)
+                {
+                    args = NULL;
+                    score = 0;
+                    break;
+                } // if
+
+                score += (int) compatible;
+            } // for
+
+            if (args != NULL)
+                score = 0;  // too many arguments supplied. No match.
+        } // else
+
+        if (score == 0)  // incompatible.
+            continue;
+
+        else if (score == perfect)  // perfection! stop looking!
+        {
+            match = 1;  // ignore all other compatible matches.
+            best = item;
+            break;
+        } // if
+
+        else if (score >= best_score)  // compatible, but not perfect, match.
+        {
+            if (score == best_score)
+            {
+                match++;
+                // !!! FIXME: list each possible function in a fail(),
+                // !!! FIXME:  but you can't actually fail() here, since
+                // !!! FIXME:  this may cease to be ambiguous if we get
+                // !!! FIXME:  a better match on a later overload.
+            } // if
+
+            else if (score > best_score)
+            {
+                match = 1;  // reset the ambiguousness count.
+                best = item;
+                best_score = score;
+            } // if
+        } // else if
+    } // while
+
+    if (match > 1)
+    {
+        assert(best != NULL);
+        failf(ctx, "Ambiguous function call to '%s'", sym);
+    } // if
+
+    if (best == NULL)
+    {
+        assert(match == 0);
+        assert(best_score == 0);
+        // !!! FIXME: ident->datatype = ?
+        failf(ctx, "No matching function named '%s'", sym);
+    } // if
+    else
+    {
+        ident->datatype = reduce_datatype(ctx, best->datatype);
+        ident->index = best->index;
+    } // else
+
+    return ident->datatype;
+} // match_func_to_call
+
+
+static const MOJOSHADER_astDataType *vectype_from_base(Context *ctx,
+                                            const MOJOSHADER_astDataType *base,
+                                            const int len)
+{
+    assert(len > 0);
+    assert(len <= 4);
+
+    if (len == 1)  // return "float" and not "float1"
+        return base;
+
+    const char *typestr = NULL;
+    switch (base->type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL: typestr = "bool"; break;
+        case MOJOSHADER_AST_DATATYPE_INT: typestr = "int"; break;
+        case MOJOSHADER_AST_DATATYPE_UINT: typestr = "uint"; break;
+        case MOJOSHADER_AST_DATATYPE_HALF: typestr = "half"; break;
+        case MOJOSHADER_AST_DATATYPE_FLOAT: typestr = "float"; break;
+        case MOJOSHADER_AST_DATATYPE_DOUBLE: typestr = "double"; break;
+        default: assert(0 && "This shouldn't happen"); break;
+    } // switch
+
+    char buf[32];
+    snprintf(buf, sizeof (buf), "%s%d", typestr, len);
+    const MOJOSHADER_astDataType *datatype = get_usertype(ctx, buf);
+    assert(datatype != NULL);
+    return datatype;
+} // vectype_from_base
+
+
+// Go through the AST and make sure all datatypes check out okay. For datatypes
+//  that are compatible but are relying on an implicit cast, we add explicit
+//  casts to the AST here, so further processing doesn't have to worry about
+//  type coercion.
+// For things that are incompatible, we generate errors and
+//  then replace them with reasonable defaults so further processing can
+//  continue (but code generation will be skipped due to errors).
+// This means further processing can assume the AST is sane and not have to
+//  spend effort verifying it again.
+// This stage will also set every AST node's datatype field, if it is
+//  meaningful to do so. This will allow conversion to IR to know what
+//  type/size a given node is.
+static const MOJOSHADER_astDataType *type_check_ast(Context *ctx, void *_ast)
+{
+    MOJOSHADER_astNode *ast = (MOJOSHADER_astNode *) _ast;
+    const MOJOSHADER_astDataType *datatype = NULL;
+    const MOJOSHADER_astDataType *datatype2 = NULL;
+    const MOJOSHADER_astDataType *datatype3 = NULL;
+
+    if ((!ast) || (ctx->out_of_memory))
+        return NULL;
+
+    // upkeep so we report correct error locations...
+    ctx->sourcefile = ast->ast.filename;
+    ctx->sourceline = ast->ast.line;
+
+    switch (ast->ast.type)
+    {
+        case MOJOSHADER_AST_OP_POSTINCREMENT:
+        case MOJOSHADER_AST_OP_POSTDECREMENT:
+        case MOJOSHADER_AST_OP_PREINCREMENT:
+        case MOJOSHADER_AST_OP_PREDECREMENT:
+        case MOJOSHADER_AST_OP_COMPLEMENT:
+        case MOJOSHADER_AST_OP_NEGATE:
+            // !!! FIXME: must be lvalue.
+            // !!! FIXME: bools must type-promote to ...int?
+            // !!! FIXME: complement must not be float (...right?)
+            datatype = type_check_ast(ctx, ast->unary.operand);
+            require_numeric_datatype(ctx, datatype);
+            ast->unary.datatype = datatype;
+            return datatype;
+
+        case MOJOSHADER_AST_OP_NOT:
+            datatype = type_check_ast(ctx, ast->unary.operand);
+            require_boolean_datatype(ctx, datatype);
+            // !!! FIXME: coerce to bool here.
+            ast->unary.datatype = &ctx->dt_bool;
+            return datatype;
+
+        case MOJOSHADER_AST_OP_DEREF_ARRAY:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            require_integer_datatype(ctx, datatype2);
+            add_type_coercion(ctx, NULL, &ctx->dt_int, &ast->binary.right, datatype2);
+
+            datatype = reduce_datatype(ctx, datatype);
+            if (datatype->type == MOJOSHADER_AST_DATATYPE_VECTOR)
+            {
+                // !!! FIXME: if constant int, fail if not 0 >= value <= vecsize.
+                ast->binary.datatype = datatype->vector.base;
+            } // if
+            else if (datatype->type == MOJOSHADER_AST_DATATYPE_MATRIX)
+            {
+                // !!! FIXME: if constant int, fail if not 0 >= value <= rowsize (colsize?).
+                ast->binary.datatype = vectype_from_base(ctx, datatype->matrix.base, datatype->matrix.columns);  // !!! FIXME: rows?
+            }
+            else
+            {
+                require_array_datatype(ctx, datatype);
+                ast->binary.datatype = array_element_datatype(ctx, datatype);
+            } // else
+
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_DEREF_STRUCT:
+        {
+            const char *member = ast->derefstruct.member;
+            datatype = type_check_ast(ctx, ast->derefstruct.identifier);
+            const MOJOSHADER_astDataType *reduced = reduce_datatype(ctx, datatype);
+
+            // Is this a swizzle and not a struct deref?
+            if (reduced->type == MOJOSHADER_AST_DATATYPE_VECTOR)
+            {
+                const int veclen = reduced->vector.elements;
+                ast->derefstruct.isswizzle = 1;
+                if (!is_swizzle_str(member, veclen))
+                {
+                    fail(ctx, "invalid swizzle on vector");
+                    // force this to be sane for further processing.
+                    const char *sane_swiz = stringcache(ctx->strcache, "xyzw");
+                    member = ast->derefstruct.member = sane_swiz;
+                } // if
+
+                const int swizlen = (int) strlen(member);
+                if (swizlen != veclen)
+                    datatype = vectype_from_base(ctx, reduced->vector.base, swizlen);
+
+                ast->derefstruct.datatype = datatype;
+                return ast->derefstruct.datatype;
+            } // if
+
+            // maybe this is an actual struct?
+            // !!! FIXME: replace with an int or something if not.
+            require_struct_datatype(ctx, reduced);
+
+            // map member to datatype
+            assert(ast->derefstruct.datatype == NULL);
+            const MOJOSHADER_astDataTypeStructMember *mbrs = reduced->structure.members;
+            int i;
+            for (i = 0; i < reduced->structure.member_count; i++)
+            {
+                if (strcmp(mbrs[i].identifier, member) == 0)
+                {
+                    ast->derefstruct.datatype = mbrs[i].datatype;
+                    ast->derefstruct.member_index = i;
+                    break;
+                } // if
+            } // for
+
+            if (ast->derefstruct.datatype == NULL)
+            {
+                // !!! FIXME: replace with an int or something.
+                failf(ctx, "Struct has no member named '%s'", member);
+            } // if
+
+            return ast->derefstruct.datatype;
+        } // case
+
+        case MOJOSHADER_AST_OP_COMMA:
+            // evaluate and throw away left, return right.
+            type_check_ast(ctx, ast->binary.left);
+            ast->binary.datatype = type_check_ast(ctx, ast->binary.right);
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_MULTIPLY:
+        case MOJOSHADER_AST_OP_DIVIDE:
+        case MOJOSHADER_AST_OP_ADD:
+        case MOJOSHADER_AST_OP_SUBTRACT:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            require_numeric_datatype(ctx, datatype);
+            require_numeric_datatype(ctx, datatype2);
+            ast->binary.datatype = add_type_coercion(ctx, &ast->binary.left,
+                                      datatype, &ast->binary.right, datatype2);
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_LSHIFT:
+        case MOJOSHADER_AST_OP_RSHIFT:
+        case MOJOSHADER_AST_OP_MODULO:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            require_integer_datatype(ctx, datatype);
+            require_integer_datatype(ctx, datatype2);
+            ast->binary.datatype = add_type_coercion(ctx, &ast->binary.left,
+                                     datatype,  &ast->binary.right, datatype2);
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_LESSTHAN:
+        case MOJOSHADER_AST_OP_GREATERTHAN:
+        case MOJOSHADER_AST_OP_LESSTHANOREQUAL:
+        case MOJOSHADER_AST_OP_GREATERTHANOREQUAL:
+        case MOJOSHADER_AST_OP_NOTEQUAL:
+        case MOJOSHADER_AST_OP_EQUAL:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            add_type_coercion(ctx, &ast->binary.left, datatype,
+                              &ast->binary.right, datatype2);
+            ast->binary.datatype = &ctx->dt_bool;
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_BINARYAND:
+        case MOJOSHADER_AST_OP_BINARYXOR:
+        case MOJOSHADER_AST_OP_BINARYOR:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            require_integer_datatype(ctx, datatype);
+            require_integer_datatype(ctx, datatype2);
+            ast->binary.datatype = add_type_coercion(ctx, &ast->binary.left,
+                                      datatype, &ast->binary.right, datatype2);
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_LOGICALAND:
+        case MOJOSHADER_AST_OP_LOGICALOR:
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            require_boolean_datatype(ctx, datatype);
+            require_boolean_datatype(ctx, datatype2);
+            // !!! FIXME: coerce each to bool here, separately.
+            add_type_coercion(ctx, &ast->binary.left, datatype,
+                              &ast->binary.right, datatype2);
+            ast->binary.datatype = &ctx->dt_bool;
+
+        case MOJOSHADER_AST_OP_ASSIGN:
+        case MOJOSHADER_AST_OP_MULASSIGN:
+        case MOJOSHADER_AST_OP_DIVASSIGN:
+        case MOJOSHADER_AST_OP_MODASSIGN:
+        case MOJOSHADER_AST_OP_ADDASSIGN:
+        case MOJOSHADER_AST_OP_SUBASSIGN:
+        case MOJOSHADER_AST_OP_LSHIFTASSIGN:
+        case MOJOSHADER_AST_OP_RSHIFTASSIGN:
+        case MOJOSHADER_AST_OP_ANDASSIGN:
+        case MOJOSHADER_AST_OP_XORASSIGN:
+        case MOJOSHADER_AST_OP_ORASSIGN:
+            // !!! FIXME: verify binary.left is an lvalue, or fail()!
+            datatype = type_check_ast(ctx, ast->binary.left);
+            datatype2 = type_check_ast(ctx, ast->binary.right);
+            ast->binary.datatype = add_type_coercion(ctx, NULL, datatype,
+                                                &ast->binary.right, datatype2);
+            return ast->binary.datatype;
+
+        case MOJOSHADER_AST_OP_CONDITIONAL:
+            datatype = type_check_ast(ctx, ast->ternary.left);
+            datatype2 = type_check_ast(ctx, ast->ternary.center);
+            datatype3 = type_check_ast(ctx, ast->ternary.right);
+            require_numeric_datatype(ctx, datatype);
+            ast->ternary.datatype = add_type_coercion(ctx, &ast->ternary.center,
+                                    datatype2, &ast->ternary.right, datatype3);
+            return ast->ternary.datatype;
+
+        case MOJOSHADER_AST_OP_IDENTIFIER:
+            datatype = find_variable(ctx, ast->identifier.identifier, &ast->identifier.index);
+            if (datatype == NULL)
+            {
+                fail(ctx, "Unknown identifier");
+                // !!! FIXME: replace with a sane default, move on.
+                datatype = &ctx->dt_int;
+            } // if
+            ast->identifier.datatype = datatype;
+            return ast->identifier.datatype;
+
+        case MOJOSHADER_AST_OP_INT_LITERAL:
+        case MOJOSHADER_AST_OP_FLOAT_LITERAL:
+        case MOJOSHADER_AST_OP_STRING_LITERAL:
+        case MOJOSHADER_AST_OP_BOOLEAN_LITERAL:
+            assert(ast->expression.datatype != NULL);
+            return ast->expression.datatype;  // already set up during parsing.
+
+        case MOJOSHADER_AST_ARGUMENTS:
+            assert(0 && "Should be done by MOJOSHADER_AST_OP_CALLFUNC/CONSTRUCTOR");
+            return NULL;
+
+        case MOJOSHADER_AST_OP_CALLFUNC:
+        {
+            datatype = match_func_to_call(ctx, &ast->callfunc);
+            const MOJOSHADER_astDataType *reduced = reduce_datatype(ctx, datatype);
+            // !!! FIXME: replace AST node with an int if this isn't a func.
+            if (!require_function_datatype(ctx, reduced))
+            {
+                ast->callfunc.datatype = &ctx->dt_int;
+                return ast->callfunc.datatype;
+            } // if
+
+            MOJOSHADER_astArguments *arg = ast->callfunc.args;
+            int i;
+            for (i = 0; i < reduced->function.num_params; i++)
+            {
+                if (arg == NULL)  // !!! FIXME: check for default parameters, fill them in.
+                {
+                    fail(ctx, "Too few arguments");
+                    // !!! FIXME: replace AST here.
+                    break;
+                } // if
+                datatype2 = arg->argument->datatype;  // already type-checked.
+                add_type_coercion(ctx, NULL, reduced->function.params[i],
+                                  &arg->argument, datatype2);
+                arg = arg->next;
+            } // for
+
+            assert(arg == NULL);  // shouldn't have chosen func if too many args.
+
+            ast->callfunc.datatype = reduced->function.retval;
+            return ast->callfunc.datatype;
+        } // case
+
+        case MOJOSHADER_AST_OP_CONSTRUCTOR:
+        {
+            const MOJOSHADER_astDataType *reduced = reduce_datatype(ctx, ast->constructor.datatype);
+            const MOJOSHADER_astDataType *base_dt = reduced;
+            int num_params = 1;
+
+            assert(reduced != NULL);
+            switch (reduced->type)
+            {
+                case MOJOSHADER_AST_DATATYPE_VECTOR:
+                    num_params = reduced->vector.elements;
+                    base_dt = reduced->vector.base;
+                    break;
+                case MOJOSHADER_AST_DATATYPE_MATRIX:
+                    num_params = reduced->matrix.rows * reduced->matrix.columns;
+                    base_dt = reduced->matrix.base;
+                    break;
+
+                case MOJOSHADER_AST_DATATYPE_BOOL:
+                case MOJOSHADER_AST_DATATYPE_INT:
+                case MOJOSHADER_AST_DATATYPE_UINT:
+                case MOJOSHADER_AST_DATATYPE_FLOAT:
+                case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
+                case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
+                case MOJOSHADER_AST_DATATYPE_HALF:
+                case MOJOSHADER_AST_DATATYPE_DOUBLE:
+                case MOJOSHADER_AST_DATATYPE_STRING:
+                    num_params = 1;
+                    break;
+
+                // !!! FIXME: can you construct a MOJOSHADER_AST_DATATYPE_STRUCT?
+                // !!! FIXME: can you construct a MOJOSHADER_AST_DATATYPE_ARRAY?
+                // !!! FIXME: can you construct a MOJOSHADER_AST_DATATYPE_BUFFER?
+
+                default:
+                    fail(ctx, "Invalid type for constructor");
+                    delete_arguments(ctx, ast->constructor.args);
+                    ast->constructor.args = new_argument(ctx, new_literal_int_expr(ctx, 0));
+                    ast->constructor.datatype = &ctx->dt_int;
+                    return ast->constructor.datatype;
+            } // switch
+
+            assert(num_params > 0);
+
+            MOJOSHADER_astArguments *arg = ast->constructor.args;
+            MOJOSHADER_astArguments *prev = NULL;
+            int i;
+            for (i = 0; i < num_params; i++)
+            {
+                if (arg == NULL)  // !!! FIXME: check for default parameters.
+                {
+                    fail(ctx, "Too few arguments");
+                    // !!! FIXME: replace AST here.
+                    break;
+                } // if
+                datatype2 = type_check_ast(ctx, arg->argument);
+
+                // "float4(float3(1,2,3),4)" is legal, so we need to see if
+                //  we're a vector, and jump that number of parameters instead
+                //  of doing type coercion.
+                reduced = reduce_datatype(ctx, datatype2);
+                if (reduced->type == MOJOSHADER_AST_DATATYPE_VECTOR)
+                {
+                    // make sure things like float4(half3(1,2,3),1) convert that half3 to float3.
+                    const int count = reduced->vector.elements;
+                    datatype3 = vectype_from_base(ctx, base_dt, count);
+                    add_type_coercion(ctx, NULL, datatype3, &arg->argument, datatype2);
+                    i += count - 1;
+                } // else
+                else
+                {
+                    add_type_coercion(ctx, NULL, base_dt, &arg->argument, datatype2);
+                } // else
+                prev = arg;
+                arg = arg->next;
+            } // for
+
+            if (arg != NULL)
+            {
+                fail(ctx, "Too many arguments");
+                // Process extra arguments then chop them out.
+                MOJOSHADER_astArguments *argi;
+                for (argi = arg; argi != NULL; argi = argi->next)
+                    type_check_ast(ctx, argi->argument);
+                if (prev != NULL)
+                    prev->next = NULL;
+                delete_arguments(ctx, arg);
+            } // if
+
+            return ast->constructor.datatype;
+        } // case
+
+        case MOJOSHADER_AST_OP_CAST:
+            datatype = sanitize_datatype(ctx, ast->cast.datatype);
+            datatype2 = type_check_ast(ctx, ast->cast.operand);
+            // you still need type coercion, since you could do a wrong cast,
+            //  like "int x = (short) mychar;"
+            add_type_coercion(ctx, NULL, datatype, &ast->cast.operand, datatype2);
+            return datatype;
+
+        case MOJOSHADER_AST_STATEMENT_BREAK:
+            if ((ctx->loop_count == 0) && (ctx->switch_count == 0))
+                fail(ctx, "Break outside loop or switch");
+            // !!! FIXME: warn if unreachable statements follow?
+            type_check_ast(ctx, ast->stmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_CONTINUE:
+            if (ctx->loop_count == 0)
+                fail(ctx, "Continue outside loop");
+            // !!! FIXME: warn if unreachable statements follow?
+            type_check_ast(ctx, ast->stmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_DISCARD:
+            // !!! FIXME: warn if unreachable statements follow?
+            type_check_ast(ctx, ast->stmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_EMPTY:
+            type_check_ast(ctx, ast->stmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_EXPRESSION:
+            // !!! FIXME: warn about expressions without a side-effect here?
+            type_check_ast(ctx, ast->exprstmt.expr);  // !!! FIXME: This is named badly...
+            type_check_ast(ctx, ast->exprstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_IF:
+            push_scope(ctx);  // new scope for "if ((int x = blah()) != 0)"
+            type_check_ast(ctx, ast->ifstmt.expr);
+            type_check_ast(ctx, ast->ifstmt.statement);
+            pop_scope(ctx);
+            type_check_ast(ctx, ast->ifstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_TYPEDEF:
+            type_check_ast(ctx, ast->typedefstmt.type_info);
+            type_check_ast(ctx, ast->typedefstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_SWITCH:
+        {
+            ctx->switch_count++;
+            MOJOSHADER_astSwitchCases *cases = ast->switchstmt.cases;
+            // !!! FIXME: expr must be POD (no structs, arrays, etc!).
+            datatype = type_check_ast(ctx, ast->switchstmt.expr);
+            while (cases)
+            {
+                // !!! FIXME: case must be POD (no structs, arrays, etc!).
+                datatype2 = type_check_ast(ctx, cases->expr);
+                add_type_coercion(ctx, NULL, datatype,
+                                  &cases->expr, datatype2);
+                type_check_ast(ctx, cases->statement);
+                cases = cases->next;
+            } // while
+            ctx->switch_count--;
+            type_check_ast(ctx, ast->switchstmt.next);
+            return NULL;
+        } // case
+
+        case MOJOSHADER_AST_SWITCH_CASE:
+            assert(0 && "Should be done by MOJOSHADER_AST_STATEMENT_SWITCH.");
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_STRUCT:
+            type_check_ast(ctx, ast->structstmt.struct_info);
+            type_check_ast(ctx, ast->structstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_VARDECL:
+            type_check_ast(ctx, ast->vardeclstmt.declaration);
+            type_check_ast(ctx, ast->vardeclstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_BLOCK:
+            push_scope(ctx);  // new vars declared here live until '}'.
+            type_check_ast(ctx, ast->blockstmt.statements);
+            pop_scope(ctx);
+            type_check_ast(ctx, ast->blockstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_FOR:
+            ctx->loop_count++;
+            push_scope(ctx);  // new scope for "for (int x = 0; ...)"
+            type_check_ast(ctx, ast->forstmt.var_decl);
+            type_check_ast(ctx, ast->forstmt.initializer);
+            type_check_ast(ctx, ast->forstmt.looptest);
+            type_check_ast(ctx, ast->forstmt.counter);
+            type_check_ast(ctx, ast->forstmt.statement);
+            pop_scope(ctx);
+            ctx->loop_count--;
+            type_check_ast(ctx, ast->forstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_DO:
+            ctx->loop_count++;
+            // !!! FIXME: should there be a push_scope() here?
+            type_check_ast(ctx, ast->dostmt.statement);
+            push_scope(ctx);  // new scope for "while ((int x = blah()) != 0)"
+            type_check_ast(ctx, ast->dostmt.expr);
+            pop_scope(ctx);
+            ctx->loop_count--;
+            type_check_ast(ctx, ast->dostmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_WHILE:
+            ctx->loop_count++;
+            push_scope(ctx);  // new scope for "while ((int x = blah()) != 0)"
+            type_check_ast(ctx, ast->whilestmt.expr);
+            type_check_ast(ctx, ast->whilestmt.statement);
+            pop_scope(ctx);
+            ctx->loop_count--;
+            type_check_ast(ctx, ast->whilestmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_STATEMENT_RETURN:
+            // !!! FIXME: type coercion to outer function's return type.
+            // !!! FIXME: warn if unreachable statements follow?
+            type_check_ast(ctx, ast->returnstmt.expr);
+            type_check_ast(ctx, ast->returnstmt.next);
+            return NULL;
+
+        case MOJOSHADER_AST_COMPUNIT_FUNCTION:
+            assert(!ctx->is_func_scope);
+
+            // We have to tapdance here to make sure the function is in
+            //  the global scope, but it's parameters are pushed as variables
+            //  in the function's scope.
+            datatype = type_check_ast(ctx, ast->funcunit.declaration);
+            ast->funcunit.index = push_function(ctx,
+                                ast->funcunit.declaration->identifier,
+                                datatype, ast->funcunit.definition == NULL);
+
+            // not just a declaration, but a full function definition?
+            if (ast->funcunit.definition != NULL)
+            {
+                assert(ctx->loop_count == 0);
+                assert(ctx->switch_count == 0);
+                ctx->is_func_scope = 1;
+                ctx->var_index = 0;  // reset this every function.
+                push_scope(ctx);  // so function params are in function scope.
+                // repush the parameters before checking the actual function.
+                MOJOSHADER_astFunctionParameters *param;
+                for (param = ast->funcunit.declaration->params; param; param = param->next)
+                    push_variable(ctx, param->identifier, param->datatype);
+                type_check_ast(ctx, ast->funcunit.definition);
+                pop_scope(ctx);
+                ctx->is_func_scope = 0;
+                assert(ctx->loop_count == 0);
+                assert(ctx->switch_count == 0);
+            } // else
+
+            type_check_ast(ctx, ast->funcunit.next);
+            return NULL;
+
+        case MOJOSHADER_AST_COMPUNIT_TYPEDEF:
+            type_check_ast(ctx, ast->typedefunit.type_info);
+            type_check_ast(ctx, ast->typedefunit.next);
+            return NULL;
+
+        case MOJOSHADER_AST_COMPUNIT_STRUCT:
+            type_check_ast(ctx, ast->structunit.struct_info);
+            type_check_ast(ctx, ast->structunit.next);
+            return NULL;
+
+        case MOJOSHADER_AST_COMPUNIT_VARIABLE:
+            type_check_ast(ctx, ast->varunit.declaration);
+            type_check_ast(ctx, ast->varunit.next);
+            return NULL;
+
+        case MOJOSHADER_AST_SCALAR_OR_ARRAY:
+            assert(0 && "Should be done by other AST nodes.");
+            return NULL;
+
+        case MOJOSHADER_AST_TYPEDEF:
+        {
+            MOJOSHADER_astScalarOrArray *soa = ast->typdef.details;
+            datatype = get_usertype(ctx, soa->identifier);
+            if (datatype != NULL)
+            {
+                fail(ctx, "typedef already defined");
+                ast->typdef.datatype = datatype;
+                return datatype;
+            } // if
+
+            datatype = build_datatype(ctx, ast->typdef.isconst,
+                                      ast->typdef.datatype, soa);
+            if (datatype == NULL)
+                return NULL;  // out of memory?
+
+            push_usertype(ctx, soa->identifier, datatype);
+            ast->typdef.datatype = datatype;
+            return ast->typdef.datatype;
+        } // case
+
+        case MOJOSHADER_AST_FUNCTION_PARAMS:
+            assert(0 && "Should be done by MOJOSHADER_AST_FUNCTION_SIGNATURE");
+
+        case MOJOSHADER_AST_FUNCTION_SIGNATURE:
+        {
+            MOJOSHADER_astFunctionParameters *param;
+            const MOJOSHADER_astDataType *dtparams[64];
+
+            int i = 0;
+            for (param = ast->funcsig.params; param; param = param->next)
+            {
+                assert(i <= STATICARRAYLEN(dtparams));  // laziness.
+                sanitize_datatype(ctx, param->datatype);
+                if (param->initializer != NULL)
+                {
+                    datatype2 = type_check_ast(ctx, param->initializer);
+                    add_type_coercion(ctx, NULL, param->datatype,
+                                      &param->initializer, datatype2);
+                } // if
+                dtparams[i] = param->datatype;
+                i++;
+            } // for
+
+            ast->funcsig.datatype = build_function_datatype(ctx,
+                                                        ast->funcsig.datatype,
+                                                        i, dtparams, 0);
+            return ast->funcsig.datatype;
+        } // case
+
+        case MOJOSHADER_AST_STRUCT_DECLARATION:
+        {
+            // !!! FIXME: We don't handle struct predeclaration at all right now
+            // !!! FIXME:  (neither does the grammar)...not only does that mean
+            // !!! FIXME:  you need to know the struct definition up front, but
+            // !!! FIXME:  you can't do "struct XXX *next;" for a self-referencing
+            // !!! FIXME:  linked list struct thing. This probably isn't a big
+            // !!! FIXME:  deal, as there aren't (CURRENTLY!) pointers in HLSL,
+            // !!! FIXME:  but you never know.
+
+            const MOJOSHADER_astStructMembers *mbrs;
+
+            // !!! FIXME: count this during parsing?
+            int count = 0;
+            mbrs = ast->structdecl.members;
+            while (mbrs != NULL)
+            {
+                count++;
+                mbrs = mbrs->next;
+            } // while
+
+            // !!! FIXME: this is hacky.
+            MOJOSHADER_astDataTypeStructMember *dtmbrs;
+            void *ptr = Malloc(ctx, sizeof (*dtmbrs) * count);
+            if (ptr == NULL)
+                return NULL;
+            if (!buffer_append(ctx->garbage, &ptr, sizeof (ptr)))
+            {
+                Free(ctx, ptr);
+                return NULL;
+            } // if
+            dtmbrs = (MOJOSHADER_astDataTypeStructMember *) ptr;
+
+            ptr = Malloc(ctx, sizeof (MOJOSHADER_astDataType));
+            if (ptr == NULL)
+                return NULL;
+            if (!buffer_append(ctx->garbage, &ptr, sizeof (ptr)))
+            {
+                Free(ctx, ptr);
+                return NULL;
+            } // if
+            MOJOSHADER_astDataType *dt = (MOJOSHADER_astDataType *) ptr;
+
+            mbrs = ast->structdecl.members;
+            int i;
+            for (i = 0; i < count; i++)
+            {
+                // !!! FIXME: current grammar forbids const keyword on struct members!
+                dtmbrs[i].datatype = build_datatype(ctx, 0, mbrs->datatype, mbrs->details);
+                dtmbrs[i].identifier = mbrs->details->identifier;  // cached!
+                mbrs = mbrs->next;
+            } // for
+
+            dt->structure.type = MOJOSHADER_AST_DATATYPE_STRUCT;
+            dt->structure.members = dtmbrs;
+            dt->structure.member_count = count;
+            ast->structdecl.datatype = dt;
+
+            // !!! FIXME: this shouldn't push for anonymous structs: "struct { int x; } myvar;"
+            // !!! FIXME:  but right now, the grammar is wrong and requires a name for the struct.
+            push_usertype(ctx, ast->structdecl.name, ast->structdecl.datatype);
+            return ast->structdecl.datatype;
+        } // case
+
+        case MOJOSHADER_AST_STRUCT_MEMBER:
+            assert(0 && "Should be done by MOJOSHADER_AST_STRUCT_DECLARATION.");
+            return NULL;
+
+        case MOJOSHADER_AST_VARIABLE_DECLARATION:
+        {
+            MOJOSHADER_astVariableDeclaration *decl = &ast->vardecl;
+
+            // this is true now, but we'll fill in ->datatype no matter what.
+            assert((decl->datatype && !decl->anonymous_datatype) ||
+                   (!decl->datatype && decl->anonymous_datatype));
+
+            // An anonymous struct? That AST node does the heavy lifting.
+            if (decl->anonymous_datatype != NULL)
+                datatype = type_check_ast(ctx, decl->anonymous_datatype);
+            else
+            {
+                datatype = build_datatype(ctx, (decl->attributes & MOJOSHADER_AST_VARATTR_CONST) != 0,
+                                          decl->datatype, decl->details);
+            } // else
+
+            while (decl != NULL)
+            {
+                decl->datatype = datatype;
+                push_variable(ctx, decl->details->identifier, datatype);
+                if (decl->initializer != NULL)
+                {
+                    datatype2 = type_check_ast(ctx, decl->initializer);
+                    add_type_coercion(ctx, NULL, datatype, &decl->initializer, datatype2);
+                } // if
+
+                type_check_ast(ctx, decl->annotations);
+                type_check_ast(ctx, decl->lowlevel);
+                decl = decl->next;
+            } // while
+
+            return datatype;
+        } // case
+
+        case MOJOSHADER_AST_ANNOTATION:
+        {
+            MOJOSHADER_astAnnotations *anno = &ast->annotations;
+            while (anno)
+            {
+                type_check_ast(ctx, anno->initializer);
+                anno = anno->next;
+            } // while
+            return NULL;
+        } // case
+
+        case MOJOSHADER_AST_PACK_OFFSET:
+        case MOJOSHADER_AST_VARIABLE_LOWLEVEL:
+            return NULL;  // no-op (for now, at least).
+
+        default:
+            assert(0 && "unexpected type");
+    } // switch
+
+    return NULL;
+} // type_check_ast
+
+
+static inline void semantic_analysis(Context *ctx)
+{
+    type_check_ast(ctx, ctx->ast);
+} // semantic_analysis
+
+// !!! FIXME: isn't this a cut-and-paste of somewhere else?
+static inline int64 strtoi64(const char *str, unsigned int len)
+{
+    int64 retval = 0;
+    int64 mult = 1;
+    int i = 0;
+
+    while ((len) && (*str == ' '))
+    {
+        str++;
+        len--;
+    } // while
+
+    if ((len) && (*str == '-'))
+    {
+        mult = -1;
+        str++;
+        len--;
+    } // if
+
+    while (i < len)
+    {
+        const char ch = str[i];
+        if ((ch < '0') || (ch > '9'))
+            break;
+        i++;
+    } // while
+
+    while (--i >= 0)
+    {
+        const char ch = str[i];
+        retval += ((int64) (ch - '0')) * mult;
+        mult *= 10;
+    } // while
+
+    return retval;
+} // strtoi64
+
+// !!! FIXME: isn't this a cut-and-paste of somewhere else?
+static inline double strtodouble(const char *_str, unsigned int len)
+{
+    // !!! FIXME: laziness prevails.
+    char *str = (char *) alloca(len+1);
+    memcpy(str, _str, len);
+    str[len] = '\0';
+    return strtod(str, NULL);
+} // strtodouble
+
+#if 0
+// This does not check correctness (POSITIONT993842 passes, etc).
+static int is_semantic(const Context *ctx, const char *token,
+                       const unsigned int tokenlen)
+{
+    static const char *names[] = {
+        "BINORMAL", "BLENDINDICES", "BLENDWEIGHT",
+        "COLOR", "NORMAL", "POSITION", "POSITIONT", "PSIZE", "TANGENT",
+        "TEXCOORD", "FOG", "TESSFACTOR", "TEXCOORD", "VFACE", "VPOS",
+        "DEPTH", NULL
+    };
+
+    // !!! FIXME: DX10 has SV_* ("System Value Semantics").
+    const char **i;
+    for (i = names; *i; i++)
+    {
+        const char *name = *i;
+        const size_t namelen = strlen(name);
+        if (tokenlen < namelen)
+            continue;
+        else if (memcmp(token, name, namelen) != 0)
+            continue;
+
+        for (name += namelen; *name; name++)
+        {
+            if ((*name < '0') || (*name > '9'))
+                break;
+        } // for
+
+        if (*name == '\0')
+            return 1;
+    } // for
+
+    return 0;
+} // is_semantic
+#endif
+
+static int convert_to_lemon_token(Context *ctx, const char *token,
+                                  unsigned int tokenlen, const Token tokenval)
+{
+    switch (tokenval)
+    {
+        case ((Token) ','): return TOKEN_HLSL_COMMA;
+        case ((Token) '='): return TOKEN_HLSL_ASSIGN;
+        case ((Token) TOKEN_ADDASSIGN): return TOKEN_HLSL_ADDASSIGN;
+        case ((Token) TOKEN_SUBASSIGN): return TOKEN_HLSL_SUBASSIGN;
+        case ((Token) TOKEN_MULTASSIGN): return TOKEN_HLSL_MULASSIGN;
+        case ((Token) TOKEN_DIVASSIGN): return TOKEN_HLSL_DIVASSIGN;
+        case ((Token) TOKEN_MODASSIGN): return TOKEN_HLSL_MODASSIGN;
+        case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_HLSL_LSHIFTASSIGN;
+        case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_HLSL_RSHIFTASSIGN;
+        case ((Token) TOKEN_ANDASSIGN): return TOKEN_HLSL_ANDASSIGN;
+        case ((Token) TOKEN_ORASSIGN): return TOKEN_HLSL_ORASSIGN;
+        case ((Token) TOKEN_XORASSIGN): return TOKEN_HLSL_XORASSIGN;
+        case ((Token) '?'): return TOKEN_HLSL_QUESTION;
+        case ((Token) TOKEN_OROR): return TOKEN_HLSL_OROR;
+        case ((Token) TOKEN_ANDAND): return TOKEN_HLSL_ANDAND;
+        case ((Token) '|'): return TOKEN_HLSL_OR;
+        case ((Token) '^'): return TOKEN_HLSL_XOR;
+        case ((Token) '&'): return TOKEN_HLSL_AND;
+        case ((Token) TOKEN_EQL): return TOKEN_HLSL_EQL;
+        case ((Token) TOKEN_NEQ): return TOKEN_HLSL_NEQ;
+        case ((Token) '<'): return TOKEN_HLSL_LT;
+        case ((Token) TOKEN_LEQ): return TOKEN_HLSL_LEQ;
+        case ((Token) '>'): return TOKEN_HLSL_GT;
+        case ((Token) TOKEN_GEQ): return TOKEN_HLSL_GEQ;
+        case ((Token) TOKEN_LSHIFT): return TOKEN_HLSL_LSHIFT;
+        case ((Token) TOKEN_RSHIFT): return TOKEN_HLSL_RSHIFT;
+        case ((Token) '+'): return TOKEN_HLSL_PLUS;
+        case ((Token) '-'): return TOKEN_HLSL_MINUS;
+        case ((Token) '*'): return TOKEN_HLSL_STAR;
+        case ((Token) '/'): return TOKEN_HLSL_SLASH;
+        case ((Token) '%'): return TOKEN_HLSL_PERCENT;
+        case ((Token) '!'): return TOKEN_HLSL_EXCLAMATION;
+        case ((Token) '~'): return TOKEN_HLSL_COMPLEMENT;
+        case ((Token) TOKEN_DECREMENT): return TOKEN_HLSL_MINUSMINUS;
+        case ((Token) TOKEN_INCREMENT): return TOKEN_HLSL_PLUSPLUS;
+        case ((Token) '.'): return TOKEN_HLSL_DOT;
+        case ((Token) '['): return TOKEN_HLSL_LBRACKET;
+        case ((Token) ']'): return TOKEN_HLSL_RBRACKET;
+        case ((Token) '('): return TOKEN_HLSL_LPAREN;
+        case ((Token) ')'): return TOKEN_HLSL_RPAREN;
+        case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_INT_CONSTANT;
+        case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_FLOAT_CONSTANT;
+        case ((Token) TOKEN_STRING_LITERAL): return TOKEN_HLSL_STRING_LITERAL;
+        case ((Token) ':'): return TOKEN_HLSL_COLON;
+        case ((Token) ';'): return TOKEN_HLSL_SEMICOLON;
+        case ((Token) '{'): return TOKEN_HLSL_LBRACE;
+        case ((Token) '}'): return TOKEN_HLSL_RBRACE;
+        //case ((Token) TOKEN_PP_PRAGMA): return TOKEN_HLSL_PRAGMA;
+        //case ((Token) '\n'): return TOKEN_HLSL_NEWLINE;
+
+        case ((Token) TOKEN_IDENTIFIER):
+            #define tokencmp(t) ((tokenlen == strlen(t)) && (memcmp(token, t, tokenlen) == 0))
+            //case ((Token) ''): return TOKEN_HLSL_TYPECAST
+            //if (tokencmp("")) return TOKEN_HLSL_TYPE_NAME
+            //if (tokencmp("...")) return TOKEN_HLSL_ELIPSIS
+            if (tokencmp("else")) return TOKEN_HLSL_ELSE;
+            if (tokencmp("inline")) return TOKEN_HLSL_INLINE;
+            if (tokencmp("void")) return TOKEN_HLSL_VOID;
+            if (tokencmp("in")) return TOKEN_HLSL_IN;
+            if (tokencmp("inout")) return TOKEN_HLSL_INOUT;
+            if (tokencmp("out")) return TOKEN_HLSL_OUT;
+            if (tokencmp("uniform")) return TOKEN_HLSL_UNIFORM;
+            if (tokencmp("linear")) return TOKEN_HLSL_LINEAR;
+            if (tokencmp("centroid")) return TOKEN_HLSL_CENTROID;
+            if (tokencmp("nointerpolation")) return TOKEN_HLSL_NOINTERPOLATION;
+            if (tokencmp("noperspective")) return TOKEN_HLSL_NOPERSPECTIVE;
+            if (tokencmp("sample")) return TOKEN_HLSL_SAMPLE;
+            if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
+            if (tokencmp("typedef")) return TOKEN_HLSL_TYPEDEF;
+            if (tokencmp("const")) return TOKEN_HLSL_CONST;
+            if (tokencmp("packoffset")) return TOKEN_HLSL_PACKOFFSET;
+            if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
+            if (tokencmp("extern")) return TOKEN_HLSL_EXTERN;
+            if (tokencmp("shared")) return TOKEN_HLSL_SHARED;
+            if (tokencmp("static")) return TOKEN_HLSL_STATIC;
+            if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
+            if (tokencmp("row_major")) return TOKEN_HLSL_ROWMAJOR;
+            if (tokencmp("column_major")) return TOKEN_HLSL_COLUMNMAJOR;
+            if (tokencmp("bool")) return TOKEN_HLSL_BOOL;
+            if (tokencmp("int")) return TOKEN_HLSL_INT;
+            if (tokencmp("uint")) return TOKEN_HLSL_UINT;
+            if (tokencmp("half")) return TOKEN_HLSL_HALF;
+            if (tokencmp("float")) return TOKEN_HLSL_FLOAT;
+            if (tokencmp("double")) return TOKEN_HLSL_DOUBLE;
+            if (tokencmp("string")) return TOKEN_HLSL_STRING;
+            if (tokencmp("snorm")) return TOKEN_HLSL_SNORM;
+            if (tokencmp("unorm")) return TOKEN_HLSL_UNORM;
+            if (tokencmp("buffer")) return TOKEN_HLSL_BUFFER;
+            if (tokencmp("vector")) return TOKEN_HLSL_VECTOR;
+            if (tokencmp("matrix")) return TOKEN_HLSL_MATRIX;
+            if (tokencmp("break")) return TOKEN_HLSL_BREAK;
+            if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
+            if (tokencmp("discard")) return TOKEN_HLSL_DISCARD;
+            if (tokencmp("return")) return TOKEN_HLSL_RETURN;
+            if (tokencmp("while")) return TOKEN_HLSL_WHILE;
+            if (tokencmp("for")) return TOKEN_HLSL_FOR;
+            if (tokencmp("unroll")) return TOKEN_HLSL_UNROLL;
+            if (tokencmp("loop")) return TOKEN_HLSL_LOOP;
+            if (tokencmp("do")) return TOKEN_HLSL_DO;
+            if (tokencmp("if")) return TOKEN_HLSL_IF;
+            if (tokencmp("branch")) return TOKEN_HLSL_BRANCH;
+            if (tokencmp("flatten")) return TOKEN_HLSL_FLATTEN;
+            if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
+            if (tokencmp("forcecase")) return TOKEN_HLSL_FORCECASE;
+            if (tokencmp("call")) return TOKEN_HLSL_CALL;
+            if (tokencmp("case")) return TOKEN_HLSL_CASE;
+            if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
+            if (tokencmp("sampler")) return TOKEN_HLSL_SAMPLER;
+            if (tokencmp("sampler1D")) return TOKEN_HLSL_SAMPLER1D;
+            if (tokencmp("sampler2D")) return TOKEN_HLSL_SAMPLER2D;
+            if (tokencmp("sampler3D")) return TOKEN_HLSL_SAMPLER3D;
+            if (tokencmp("samplerCUBE")) return TOKEN_HLSL_SAMPLERCUBE;
+            if (tokencmp("sampler_state")) return TOKEN_HLSL_SAMPLER_STATE;
+            if (tokencmp("SamplerState")) return TOKEN_HLSL_SAMPLERSTATE;
+            if (tokencmp("true")) return TOKEN_HLSL_TRUE;
+            if (tokencmp("false")) return TOKEN_HLSL_FALSE;
+            if (tokencmp("SamplerComparisonState")) return TOKEN_HLSL_SAMPLERCOMPARISONSTATE;
+            if (tokencmp("isolate")) return TOKEN_HLSL_ISOLATE;
+            if (tokencmp("maxInstructionCount")) return TOKEN_HLSL_MAXINSTRUCTIONCOUNT;
+            if (tokencmp("noExpressionOptimizations")) return TOKEN_HLSL_NOEXPRESSIONOPTIMIZATIONS;
+            if (tokencmp("unused")) return TOKEN_HLSL_UNUSED;
+            if (tokencmp("xps")) return TOKEN_HLSL_XPS;
+            #undef tokencmp
+
+            // get a canonical copy of the string now, as we'll need it.
+            token = stringcache_len(ctx->strcache, token, tokenlen);
+            if (get_usertype(ctx, token) != NULL)
+                return TOKEN_HLSL_USERTYPE;
+            return TOKEN_HLSL_IDENTIFIER;
+
+        case TOKEN_EOI: return 0;
+        default: assert(0 && "unexpected token from lexer\n"); return 0;
+    } // switch
+
+    return 0;
+} // convert_to_lemon_token
+
+
+static void delete_ir(Context *ctx, void *_ir);  // !!! FIXME: move this code around.
+
+static void destroy_context(Context *ctx)
+{
+    if (ctx != NULL)
+    {
+        MOJOSHADER_free f = ((ctx->free != NULL) ? ctx->free : MOJOSHADER_internal_free);
+        void *d = ctx->malloc_data;
+        size_t i = 0;
+
+        // !!! FIXME: this is kinda hacky.
+        const size_t count = buffer_size(ctx->garbage) / sizeof (void *);
+        if (count > 0)
+        {
+            void **garbage = (void **) buffer_flatten(ctx->garbage);
+            if (garbage != NULL)
+            {
+                for (i = 0; i < count; i++)
+                    f(garbage[i], d);
+                f(garbage, d);
+            } // if
+        } // if
+        buffer_destroy(ctx->garbage);
+
+        delete_compilation_unit(ctx, (MOJOSHADER_astCompilationUnit*)ctx->ast);
+        destroy_symbolmap(ctx, &ctx->usertypes);
+        destroy_symbolmap(ctx, &ctx->variables);
+        stringcache_destroy(ctx->strcache);
+        errorlist_destroy(ctx->errors);
+        errorlist_destroy(ctx->warnings);
+
+        if (ctx->ir != NULL)
+        {
+            for (i = 0; i <= ctx->user_func_index; i++)
+                delete_ir(ctx, ctx->ir[i]);
+            f(ctx->ir, d);
+        } // if
+
+        // !!! FIXME: more to clean up here, now.
+
+        f(ctx, d);
+    } // if
+} // destroy_context
+
+static Context *build_context(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    if (!m) m = MOJOSHADER_internal_malloc;
+    if (!f) f = MOJOSHADER_internal_free;
+
+    Context *ctx = (Context *) m(sizeof (Context), d);
+    if (ctx == NULL)
+        return NULL;
+
+    memset(ctx, '\0', sizeof (Context));
+    ctx->malloc = m;
+    ctx->free = f;
+    ctx->malloc_data = d;
+    //ctx->parse_phase = MOJOSHADER_PARSEPHASE_NOTSTARTED;
+    create_symbolmap(ctx, &ctx->usertypes); // !!! FIXME: check for failure.
+    create_symbolmap(ctx, &ctx->variables); // !!! FIXME: check for failure.
+    ctx->strcache = stringcache_create(MallocBridge, FreeBridge, ctx);  // !!! FIXME: check for failure.
+    ctx->errors = errorlist_create(MallocBridge, FreeBridge, ctx);  // !!! FIXME: check for failure.
+    ctx->warnings = errorlist_create(MallocBridge, FreeBridge, ctx);  // !!! FIXME: check for failure.
+
+    // !!! FIXME: this feels hacky.
+    ctx->garbage = buffer_create(256*sizeof(void*),MallocBridge,FreeBridge,ctx);  // !!! FIXME: check for failure.
+
+    ctx->dt_none.type = MOJOSHADER_AST_DATATYPE_NONE;
+    ctx->dt_bool.type = MOJOSHADER_AST_DATATYPE_BOOL;
+    ctx->dt_int.type = MOJOSHADER_AST_DATATYPE_INT;
+    ctx->dt_uint.type = MOJOSHADER_AST_DATATYPE_UINT;
+    ctx->dt_float.type = MOJOSHADER_AST_DATATYPE_FLOAT;
+    ctx->dt_float_snorm.type = MOJOSHADER_AST_DATATYPE_FLOAT_SNORM;
+    ctx->dt_float_unorm.type = MOJOSHADER_AST_DATATYPE_FLOAT_UNORM;
+    ctx->dt_half.type = MOJOSHADER_AST_DATATYPE_HALF;
+    ctx->dt_double.type = MOJOSHADER_AST_DATATYPE_DOUBLE;
+    ctx->dt_string.type = MOJOSHADER_AST_DATATYPE_STRING;
+    ctx->dt_sampler1d.type = MOJOSHADER_AST_DATATYPE_SAMPLER_1D;
+    ctx->dt_sampler2d.type = MOJOSHADER_AST_DATATYPE_SAMPLER_2D;
+    ctx->dt_sampler3d.type = MOJOSHADER_AST_DATATYPE_SAMPLER_3D;
+    ctx->dt_samplercube.type = MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE;
+    ctx->dt_samplerstate.type = MOJOSHADER_AST_DATATYPE_SAMPLER_STATE;
+    ctx->dt_samplercompstate.type = MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE;
+
+    #define INIT_DT_BUFFER(t) \
+        ctx->dt_buf_##t.type = MOJOSHADER_AST_DATATYPE_BUFFER; \
+        ctx->dt_buf_##t.buffer.base = &ctx->dt_##t;
+    INIT_DT_BUFFER(bool);
+    INIT_DT_BUFFER(int);
+    INIT_DT_BUFFER(uint);
+    INIT_DT_BUFFER(half);
+    INIT_DT_BUFFER(float);
+    INIT_DT_BUFFER(double);
+    INIT_DT_BUFFER(float_snorm);
+    INIT_DT_BUFFER(float_unorm);
+    #undef INIT_DT_BUFFER
+
+    return ctx;
+} // build_context
+
+
+// This macro salsa is kinda nasty, but it's the smallest, least error-prone
+//  way I can find to do this well in C.  :/
+
+#define ADD_INTRINSIC(fn, ret, params) do { \
+    push_function(ctx, fn, \
+        build_function_datatype(ctx, ret, STATICARRAYLEN(params), params, 1), \
+        0); \
+} while (0)
+
+#define ADD_INTRINSIC_VECTOR(typestr, code) do { \
+    const MOJOSHADER_astDataType *dt; \
+    dt = get_usertype(ctx, typestr "1"); code; \
+    dt = get_usertype(ctx, typestr "2"); code; \
+    dt = get_usertype(ctx, typestr "3"); code; \
+    dt = get_usertype(ctx, typestr "4"); code; \
+} while (0)
+
+#define ADD_INTRINSIC_VECTOR_FLOAT(code) { \
+    ADD_INTRINSIC_VECTOR("float", code); \
+    ADD_INTRINSIC_VECTOR("half", code); \
+    ADD_INTRINSIC_VECTOR("double", code); \
+}
+#define ADD_INTRINSIC_VECTOR_INT(code) { \
+    ADD_INTRINSIC_VECTOR("int", code); \
+    ADD_INTRINSIC_VECTOR("uint", code); \
+}
+#define ADD_INTRINSIC_VECTOR_BOOL(code) { \
+    ADD_INTRINSIC_VECTOR("bool", code); \
+}
+
+#define ADD_INTRINSIC_MATRIX(typestr, code) do { \
+    const MOJOSHADER_astDataType *dt; \
+    dt = get_usertype(ctx, typestr "1x1"); code; \
+    dt = get_usertype(ctx, typestr "1x2"); code; \
+    dt = get_usertype(ctx, typestr "1x3"); code; \
+    dt = get_usertype(ctx, typestr "1x4"); code; \
+    dt = get_usertype(ctx, typestr "2x1"); code; \
+    dt = get_usertype(ctx, typestr "2x2"); code; \
+    dt = get_usertype(ctx, typestr "2x3"); code; \
+    dt = get_usertype(ctx, typestr "2x4"); code; \
+    dt = get_usertype(ctx, typestr "3x1"); code; \
+    dt = get_usertype(ctx, typestr "3x2"); code; \
+    dt = get_usertype(ctx, typestr "3x3"); code; \
+    dt = get_usertype(ctx, typestr "3x4"); code; \
+    dt = get_usertype(ctx, typestr "4x1"); code; \
+    dt = get_usertype(ctx, typestr "4x2"); code; \
+    dt = get_usertype(ctx, typestr "4x3"); code; \
+    dt = get_usertype(ctx, typestr "4x4"); code; \
+} while (0)
+
+#define ADD_INTRINSIC_MATRIX_FLOAT(code) { \
+    ADD_INTRINSIC_MATRIX("float", code); \
+    ADD_INTRINSIC_MATRIX("half", code); \
+    ADD_INTRINSIC_MATRIX("double", code); \
+}
+#define ADD_INTRINSIC_MATRIX_INT(code) { \
+    ADD_INTRINSIC_MATRIX("int", code); \
+    ADD_INTRINSIC_MATRIX("uint", code); \
+}
+#define ADD_INTRINSIC_MATRIX_BOOL(code) { \
+    ADD_INTRINSIC_MATRIX("bool", code); \
+}
+
+#define ADD_INTRINSIC_ANY(scalar, typestr, code) do { \
+    { const MOJOSHADER_astDataType *dt = scalar; code; } \
+    ADD_INTRINSIC_VECTOR(typestr, code); \
+    ADD_INTRINSIC_MATRIX(typestr, code); \
+} while (0)
+
+#define ADD_INTRINSIC_ANY_FLOAT(code) do { \
+    ADD_INTRINSIC_ANY(&ctx->dt_double, "double", code); \
+    ADD_INTRINSIC_ANY(&ctx->dt_half, "half", code); \
+    ADD_INTRINSIC_ANY(&ctx->dt_float, "float", code); \
+} while (0)
+#define ADD_INTRINSIC_ANY_INT(code) do { \
+    ADD_INTRINSIC_ANY(&ctx->dt_uint, "uint", code); \
+    ADD_INTRINSIC_ANY(&ctx->dt_int, "int", code); \
+} while (0)
+
+#define ADD_INTRINSIC_ANY_BOOL(code) ADD_INTRINSIC_ANY(&ctx->dt_bool, "bool", code)
+
+static void add_intrinsic1(Context *ctx, const char *fn,
+                           const MOJOSHADER_astDataType *ret,
+                           const MOJOSHADER_astDataType *dt1)
+{
+    const MOJOSHADER_astDataType *params[] = { dt1 };
+    ADD_INTRINSIC(fn, ret, params);
+} // add_intrinsic1
+
+static void add_intrinsic2(Context *ctx, const char *fn,
+                           const MOJOSHADER_astDataType *ret,
+                           const MOJOSHADER_astDataType *dt1,
+                           const MOJOSHADER_astDataType *dt2)
+{
+    const MOJOSHADER_astDataType *params[] = { dt1, dt2 };
+    ADD_INTRINSIC(fn, ret, params);
+} // add_intrinsic2
+
+static void add_intrinsic3(Context *ctx, const char *fn,
+                           const MOJOSHADER_astDataType *ret,
+                           const MOJOSHADER_astDataType *dt1,
+                           const MOJOSHADER_astDataType *dt2,
+                           const MOJOSHADER_astDataType *dt3)
+{
+    const MOJOSHADER_astDataType *params[] = { dt1, dt2, dt3 };
+    ADD_INTRINSIC(fn, ret, params);
+} // add_intrinsic3
+
+static void add_intrinsic4(Context *ctx, const char *fn,
+                           const MOJOSHADER_astDataType *ret,
+                           const MOJOSHADER_astDataType *dt1,
+                           const MOJOSHADER_astDataType *dt2,
+                           const MOJOSHADER_astDataType *dt3,
+                           const MOJOSHADER_astDataType *dt4)
+{
+    const MOJOSHADER_astDataType *params[] = { dt1, dt2, dt3, dt4 };
+    ADD_INTRINSIC(fn, ret, params);
+} // add_intrinsic4
+
+// PLEASE NOTE that add_intrinsic*() is called AFTER the various
+//  ADD_INTRINSIC_* macros, even though these look like functions that
+//  should be called first. They might be called multiple times by the macro.
+//  The variable "dt" is defined by the macro for use by your code.
+static void add_intrinsic_SAME1_ANYf(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic1(ctx, fn, dt, dt));
+} // add_intrinsic_SAME1_ANYf
+
+static void add_intrinsic_SAME1_ANYfi(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_INT(add_intrinsic1(ctx, fn, dt, dt));
+    add_intrinsic_SAME1_ANYf(ctx, fn);
+} // add_intrinsic_SAME1_ANYfi
+
+static void add_intrinsic_BOOL_ANYf(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic1(ctx, fn, &ctx->dt_bool, dt));
+} // add_intrinsic_BOOL_ANYf
+
+static void add_intrinsic_BOOL_ANYfib(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_BOOL(add_intrinsic1(ctx, fn, &ctx->dt_bool, dt));
+    ADD_INTRINSIC_ANY_INT(add_intrinsic1(ctx, fn, &ctx->dt_bool, dt));
+    add_intrinsic_BOOL_ANYf(ctx, fn);
+} // add_intrinsic_BOOL_ANYfib
+
+static void add_intrinsic_SAME1_ANYf_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic2(ctx, fn, dt, dt, dt));
+} // add_intrinsic_SAME1_ANYf_SAME1
+
+static void add_intrinsic_SAME1_ANYfi_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_INT(add_intrinsic2(ctx, fn, dt, dt, dt));
+    add_intrinsic_SAME1_ANYf_SAME1(ctx, fn);
+} // add_intrinsic_SAME1_ANYfi_SAME1
+
+static void add_intrinsic_SAME1_ANYf_SAME1_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic3(ctx, fn, dt, dt, dt, dt));
+} // add_intrinsic_SAME1_ANYf_SAME1_SAME1
+
+static void add_intrinsic_SAME1_ANYfi_SAME1_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_INT(add_intrinsic3(ctx, fn, dt, dt, dt, dt));
+    add_intrinsic_SAME1_ANYf_SAME1_SAME1(ctx, fn);
+} // add_intrinsic_SAME1_ANYfi_SAME1_SAME1
+
+static void add_intrinsic_SAME1_Mfib(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_MATRIX_BOOL(add_intrinsic1(ctx, fn, dt, dt));
+    ADD_INTRINSIC_MATRIX_INT(add_intrinsic1(ctx, fn, dt, dt));
+    ADD_INTRINSIC_MATRIX_FLOAT(add_intrinsic1(ctx, fn, dt, dt));
+} // add_intrinsic_SAME1_Mfib
+
+static void add_intrinsic_SAME1_Vf(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic1(ctx, fn, dt, dt));
+} // add_intrinsic_SAME1_Vf
+
+static void add_intrinsic_SAME1_Vf_SAME1_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic3(ctx, fn, dt, dt, dt, dt));
+} // add_intrinsic_SAME1_Vf_SAME1_SAME1
+
+static void add_intrinsic_SAME1_Vf_SAME1_f(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic3(ctx, fn, dt, dt, dt, dt->user.details->vector.base));
+} // add_intrinsic_SAME1_Vf_SAME1_f
+
+static void add_intrinsic_VOID_ANYf(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic1(ctx, fn, NULL, dt));
+} // add_intrinsic_VOID_ANYf
+
+static void add_intrinsic_VOID_ANYf_SAME1_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_ANY_FLOAT(add_intrinsic3(ctx, fn, NULL, dt, dt, dt));
+} // add_intrinsic_VOID_ANYf_SAME1_SAME1
+
+static void add_intrinsic_f_SQUAREMATRIXf(Context *ctx, const char *fn)
+{
+    add_intrinsic1(ctx, fn, &ctx->dt_float, get_usertype(ctx, "float1x1"));
+    add_intrinsic1(ctx, fn, &ctx->dt_float, get_usertype(ctx, "float2x2"));
+    add_intrinsic1(ctx, fn, &ctx->dt_float, get_usertype(ctx, "float3x3"));
+    add_intrinsic1(ctx, fn, &ctx->dt_float, get_usertype(ctx, "float4x4"));
+} // add_intrinsic_f_SQUAREMATRIXf
+
+static void add_intrinsic_f_Vf(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic1(ctx, fn, dt->user.details->vector.base, dt));
+} // add_intrinsic_f_Vf
+
+static void add_intrinsic_fi_Vfi_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_INT(add_intrinsic2(ctx, fn, dt->user.details->vector.base, dt, dt));
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic2(ctx, fn, dt->user.details->vector.base, dt, dt));
+} // add_intrinsic_fi_Vfi_SAME1
+
+static void add_intrinsic_f_Vf_SAME1(Context *ctx, const char *fn)
+{
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic2(ctx, fn, dt->user.details->vector.base, dt, dt));
+} // add_intrinsic_f_Vf_SAME1
+
+static void add_intrinsic_3f_3f_3f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *dt = get_usertype(ctx, "float3");
+    add_intrinsic2(ctx, fn, dt, dt, dt);
+} // add_intrinsic_3f_3f_3f
+
+static void add_intrinsic_4f_f_f_f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f = &ctx->dt_float;
+    add_intrinsic3(ctx, fn, f4, f, f, f);
+} // add_intrinsic_4f_f_f_f
+
+static void add_intrinsic_4f_s1_4f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *dt = get_usertype(ctx, "float4");
+    add_intrinsic2(ctx, fn, dt, &ctx->dt_sampler1d, dt);
+} // add_intrinsic_4f_s1_4f
+
+static void add_intrinsic_4f_s1_f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *dt = get_usertype(ctx, "float4");
+    add_intrinsic2(ctx, fn, dt, &ctx->dt_sampler1d, &ctx->dt_float);
+} // add_intrinsic_4f_s1_f
+
+static void add_intrinsic_4f_s1_f_f_f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *dt = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f = &ctx->dt_float;
+    add_intrinsic4(ctx, fn, dt, &ctx->dt_sampler1d, f, f, f);
+} // add_intrinsic_4f_s1_f_f_f
+
+static void add_intrinsic_4f_s2_2f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f2 = get_usertype(ctx, "float2");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_sampler2d, f2);
+} // add_intrinsic_4f_s2_2f
+
+static void add_intrinsic_4f_s2_2f_2f_2f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f2 = get_usertype(ctx, "float2");
+    add_intrinsic4(ctx, fn, f4, &ctx->dt_sampler2d, f2, f2, f2);
+} // add_intrinsic_4f_s2_2f_2f_2f
+
+static void add_intrinsic_4f_s2_4f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_sampler2d, f4);
+} // add_intrinsic_4f_s2_4f
+
+static void add_intrinsic_4f_s3_3f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f3 = get_usertype(ctx, "float3");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_sampler3d, f3);
+} // add_intrinsic_4f_s3_3f
+
+static void add_intrinsic_4f_s3_3f_3f_3f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f3 = get_usertype(ctx, "float3");
+    add_intrinsic4(ctx, fn, f4, &ctx->dt_sampler3d, f3, f3, f3);
+} // add_intrinsic_4f_s3_3f_3f_3f
+
+static void add_intrinsic_4f_s3_4f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_sampler3d, f4);
+} // add_intrinsic_4f_s3_4f
+
+static void add_intrinsic_4f_sc_3f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f3 = get_usertype(ctx, "float3");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_samplercube, f3);
+} // add_intrinsic_4f_sc_3f
+
+static void add_intrinsic_4f_sc_3f_3f_3f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *f3 = get_usertype(ctx, "float3");
+    add_intrinsic4(ctx, fn, f4, &ctx->dt_samplercube, f3, f3, f3);
+} // add_intrinsic_4f_sc_3f_3f_3f
+
+static void add_intrinsic_4f_sc_4f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    add_intrinsic2(ctx, fn, f4, &ctx->dt_samplercube, f4);
+} // add_intrinsic_4f_sc_4f
+
+static void add_intrinsic_4i_4f(Context *ctx, const char *fn)
+{
+    const MOJOSHADER_astDataType *i4 = get_usertype(ctx, "int4");
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    add_intrinsic1(ctx, fn, i4, f4);
+} // add_intrinsic_4i_4f
+
+static void add_intrinsic_mul(Context *ctx, const char *fn)
+{
+    // mul() is nasty, since there's a bunch of overloads that aren't just
+    //  related to vector size.
+    // !!! FIXME: needs half, double, uint...
+    const MOJOSHADER_astDataType *dtf = &ctx->dt_float;
+    const MOJOSHADER_astDataType *dti = &ctx->dt_int;
+    const MOJOSHADER_astDataType *f1 = get_usertype(ctx, "float1");
+    const MOJOSHADER_astDataType *f2 = get_usertype(ctx, "float2");
+    const MOJOSHADER_astDataType *f3 = get_usertype(ctx, "float3");
+    const MOJOSHADER_astDataType *f4 = get_usertype(ctx, "float4");
+    const MOJOSHADER_astDataType *i1 = get_usertype(ctx, "int1");
+    const MOJOSHADER_astDataType *i2 = get_usertype(ctx, "int2");
+    const MOJOSHADER_astDataType *i3 = get_usertype(ctx, "int3");
+    const MOJOSHADER_astDataType *i4 = get_usertype(ctx, "int4");
+    const MOJOSHADER_astDataType *f1x1 = get_usertype(ctx, "float1x1");
+    const MOJOSHADER_astDataType *f1x2 = get_usertype(ctx, "float1x2");
+    const MOJOSHADER_astDataType *f1x3 = get_usertype(ctx, "float1x3");
+    const MOJOSHADER_astDataType *f1x4 = get_usertype(ctx, "float1x4");
+    const MOJOSHADER_astDataType *f2x1 = get_usertype(ctx, "float2x1");
+    const MOJOSHADER_astDataType *f2x2 = get_usertype(ctx, "float2x2");
+    const MOJOSHADER_astDataType *f2x3 = get_usertype(ctx, "float2x3");
+    const MOJOSHADER_astDataType *f2x4 = get_usertype(ctx, "float2x4");
+    const MOJOSHADER_astDataType *f3x1 = get_usertype(ctx, "float3x1");
+    const MOJOSHADER_astDataType *f3x2 = get_usertype(ctx, "float3x2");
+    const MOJOSHADER_astDataType *f3x3 = get_usertype(ctx, "float3x3");
+    const MOJOSHADER_astDataType *f3x4 = get_usertype(ctx, "float3x4");
+    const MOJOSHADER_astDataType *f4x1 = get_usertype(ctx, "float4x1");
+    const MOJOSHADER_astDataType *f4x2 = get_usertype(ctx, "float4x2");
+    const MOJOSHADER_astDataType *f4x3 = get_usertype(ctx, "float4x3");
+    const MOJOSHADER_astDataType *f4x4 = get_usertype(ctx, "float4x4");
+    const MOJOSHADER_astDataType *i1x1 = get_usertype(ctx, "int1x1");
+    const MOJOSHADER_astDataType *i1x2 = get_usertype(ctx, "int1x2");
+    const MOJOSHADER_astDataType *i1x3 = get_usertype(ctx, "int1x3");
+    const MOJOSHADER_astDataType *i1x4 = get_usertype(ctx, "int1x4");
+    const MOJOSHADER_astDataType *i2x1 = get_usertype(ctx, "int2x1");
+    const MOJOSHADER_astDataType *i2x2 = get_usertype(ctx, "int2x2");
+    const MOJOSHADER_astDataType *i2x3 = get_usertype(ctx, "int2x3");
+    const MOJOSHADER_astDataType *i2x4 = get_usertype(ctx, "int2x4");
+    const MOJOSHADER_astDataType *i3x1 = get_usertype(ctx, "int3x1");
+    const MOJOSHADER_astDataType *i3x2 = get_usertype(ctx, "int3x2");
+    const MOJOSHADER_astDataType *i3x3 = get_usertype(ctx, "int3x3");
+    const MOJOSHADER_astDataType *i3x4 = get_usertype(ctx, "int3x4");
+    const MOJOSHADER_astDataType *i4x1 = get_usertype(ctx, "int4x1");
+    const MOJOSHADER_astDataType *i4x2 = get_usertype(ctx, "int4x2");
+    const MOJOSHADER_astDataType *i4x3 = get_usertype(ctx, "int4x3");
+    const MOJOSHADER_astDataType *i4x4 = get_usertype(ctx, "int4x4");
+
+    // scalar * scalar
+    add_intrinsic2(ctx, fn, dti, dti, dti);
+    add_intrinsic2(ctx, fn, dtf, dtf, dtf);
+
+    // scalar * vector
+    ADD_INTRINSIC_VECTOR_INT(add_intrinsic2(ctx, fn, dt, dti, dt));
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic2(ctx, fn, dt, dtf, dt));
+
+    // scalar * matrix
+    ADD_INTRINSIC_MATRIX_INT(add_intrinsic2(ctx, fn, dt, dti, dt));
+    ADD_INTRINSIC_MATRIX_FLOAT(add_intrinsic2(ctx, fn, dt, dtf, dt));
+
+    // vector * scalar
+    ADD_INTRINSIC_VECTOR_INT(add_intrinsic2(ctx, fn, dt, dt, dti));
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic2(ctx, fn, dt, dt, dtf));
+
+    // vector * vector
+    ADD_INTRINSIC_VECTOR_INT(add_intrinsic2(ctx, fn, dti, dt, dt));
+    ADD_INTRINSIC_VECTOR_FLOAT(add_intrinsic2(ctx, fn, dtf, dt, dt));
+
+    // vector * matrix
+    add_intrinsic2(ctx, fn, i1, i1, i1x1);
+    add_intrinsic2(ctx, fn, i2, i1, i1x2);
+    add_intrinsic2(ctx, fn, i3, i1, i1x3);
+    add_intrinsic2(ctx, fn, i4, i1, i1x4);
+    add_intrinsic2(ctx, fn, i1, i2, i2x1);
+    add_intrinsic2(ctx, fn, i2, i2, i2x2);
+    add_intrinsic2(ctx, fn, i3, i2, i2x3);
+    add_intrinsic2(ctx, fn, i4, i2, i2x4);
+    add_intrinsic2(ctx, fn, i1, i3, i3x1);
+    add_intrinsic2(ctx, fn, i2, i3, i3x2);
+    add_intrinsic2(ctx, fn, i3, i3, i3x3);
+    add_intrinsic2(ctx, fn, i4, i3, i3x4);
+    add_intrinsic2(ctx, fn, i1, i4, i4x1);
+    add_intrinsic2(ctx, fn, i2, i4, i4x2);
+    add_intrinsic2(ctx, fn, i3, i4, i4x3);
+    add_intrinsic2(ctx, fn, i4, i4, i4x4);
+    add_intrinsic2(ctx, fn, f1, f1, f1x1);
+    add_intrinsic2(ctx, fn, f2, f1, f1x2);
+    add_intrinsic2(ctx, fn, f3, f1, f1x3);
+    add_intrinsic2(ctx, fn, f4, f1, f1x4);
+    add_intrinsic2(ctx, fn, f1, f2, f2x1);
+    add_intrinsic2(ctx, fn, f2, f2, f2x2);
+    add_intrinsic2(ctx, fn, f3, f2, f2x3);
+    add_intrinsic2(ctx, fn, f4, f2, f2x4);
+    add_intrinsic2(ctx, fn, f1, f3, f3x1);
+    add_intrinsic2(ctx, fn, f2, f3, f3x2);
+    add_intrinsic2(ctx, fn, f3, f3, f3x3);
+    add_intrinsic2(ctx, fn, f4, f3, f3x4);
+    add_intrinsic2(ctx, fn, f1, f4, f4x1);
+    add_intrinsic2(ctx, fn, f2, f4, f4x2);
+    add_intrinsic2(ctx, fn, f3, f4, f4x3);
+    add_intrinsic2(ctx, fn, f4, f4, f4x4);
+
+    // matrix * scalar
+    ADD_INTRINSIC_MATRIX_INT(add_intrinsic2(ctx, fn, dt, dt, dti));
+    ADD_INTRINSIC_MATRIX_FLOAT(add_intrinsic2(ctx, fn, dt, dt, dtf));
+
+    // matrix * vector
+    add_intrinsic2(ctx, fn, i1, i1x1, i1);
+    add_intrinsic2(ctx, fn, i1, i1x2, i2);
+    add_intrinsic2(ctx, fn, i1, i1x3, i3);
+    add_intrinsic2(ctx, fn, i1, i1x4, i4);
+    add_intrinsic2(ctx, fn, i2, i2x1, i1);
+    add_intrinsic2(ctx, fn, i2, i2x2, i2);
+    add_intrinsic2(ctx, fn, i2, i2x3, i3);
+    add_intrinsic2(ctx, fn, i2, i2x4, i4);
+    add_intrinsic2(ctx, fn, i3, i3x1, i1);
+    add_intrinsic2(ctx, fn, i3, i3x2, i2);
+    add_intrinsic2(ctx, fn, i3, i3x3, i3);
+    add_intrinsic2(ctx, fn, i3, i3x4, i4);
+    add_intrinsic2(ctx, fn, i4, i4x1, i1);
+    add_intrinsic2(ctx, fn, i4, i4x2, i2);
+    add_intrinsic2(ctx, fn, i4, i4x3, i3);
+    add_intrinsic2(ctx, fn, i4, i4x4, i4);
+    add_intrinsic2(ctx, fn, f1, f1x1, f1);
+    add_intrinsic2(ctx, fn, f1, f1x2, f2);
+    add_intrinsic2(ctx, fn, f1, f1x3, f3);
+    add_intrinsic2(ctx, fn, f1, f1x4, f4);
+    add_intrinsic2(ctx, fn, f2, f2x1, f1);
+    add_intrinsic2(ctx, fn, f2, f2x2, f2);
+    add_intrinsic2(ctx, fn, f2, f2x3, f3);
+    add_intrinsic2(ctx, fn, f2, f2x4, f4);
+    add_intrinsic2(ctx, fn, f3, f3x1, f1);
+    add_intrinsic2(ctx, fn, f3, f3x2, f2);
+    add_intrinsic2(ctx, fn, f3, f3x3, f3);
+    add_intrinsic2(ctx, fn, f3, f3x4, f4);
+    add_intrinsic2(ctx, fn, f4, f4x1, f1);
+    add_intrinsic2(ctx, fn, f4, f4x2, f2);
+    add_intrinsic2(ctx, fn, f4, f4x3, f3);
+    add_intrinsic2(ctx, fn, f4, f4x4, f4);
+
+    // matrix * matrix
+    add_intrinsic2(ctx, fn, i1x1, i1x1, i1x1);
+    add_intrinsic2(ctx, fn, i1x2, i1x1, i1x2);
+    add_intrinsic2(ctx, fn, i1x3, i1x1, i1x3);
+    add_intrinsic2(ctx, fn, i1x4, i1x1, i1x4);
+    add_intrinsic2(ctx, fn, i1x1, i1x2, i2x1);
+    add_intrinsic2(ctx, fn, i1x2, i1x2, i2x2);
+    add_intrinsic2(ctx, fn, i1x3, i1x2, i2x3);
+    add_intrinsic2(ctx, fn, i1x4, i1x2, i2x4);
+    add_intrinsic2(ctx, fn, i1x1, i1x3, i3x1);
+    add_intrinsic2(ctx, fn, i1x2, i1x3, i3x2);
+    add_intrinsic2(ctx, fn, i1x3, i1x3, i3x3);
+    add_intrinsic2(ctx, fn, i1x4, i1x3, i3x4);
+    add_intrinsic2(ctx, fn, i1x1, i1x4, i4x1);
+    add_intrinsic2(ctx, fn, i1x2, i1x4, i4x2);
+    add_intrinsic2(ctx, fn, i1x3, i1x4, i4x3);
+    add_intrinsic2(ctx, fn, i1x4, i1x4, i4x4);
+    add_intrinsic2(ctx, fn, i2x1, i2x1, i1x1);
+    add_intrinsic2(ctx, fn, i2x2, i2x1, i1x2);
+    add_intrinsic2(ctx, fn, i2x3, i2x1, i1x3);
+    add_intrinsic2(ctx, fn, i2x4, i2x1, i1x4);
+    add_intrinsic2(ctx, fn, i2x1, i2x2, i2x1);
+    add_intrinsic2(ctx, fn, i2x2, i2x2, i2x2);
+    add_intrinsic2(ctx, fn, i2x3, i2x2, i2x3);
+    add_intrinsic2(ctx, fn, i2x4, i2x2, i2x4);
+    add_intrinsic2(ctx, fn, i2x1, i2x3, i3x1);
+    add_intrinsic2(ctx, fn, i2x2, i2x3, i3x2);
+    add_intrinsic2(ctx, fn, i2x3, i2x3, i3x3);
+    add_intrinsic2(ctx, fn, i2x4, i2x3, i3x4);
+    add_intrinsic2(ctx, fn, i2x1, i2x4, i4x1);
+    add_intrinsic2(ctx, fn, i2x2, i2x4, i4x2);
+    add_intrinsic2(ctx, fn, i2x3, i2x4, i4x3);
+    add_intrinsic2(ctx, fn, i2x4, i2x4, i4x4);
+    add_intrinsic2(ctx, fn, i3x1, i3x1, i1x1);
+    add_intrinsic2(ctx, fn, i3x2, i3x1, i1x2);
+    add_intrinsic2(ctx, fn, i3x3, i3x1, i1x3);
+    add_intrinsic2(ctx, fn, i3x4, i3x1, i1x4);
+    add_intrinsic2(ctx, fn, i3x1, i3x2, i2x1);
+    add_intrinsic2(ctx, fn, i3x2, i3x2, i2x2);
+    add_intrinsic2(ctx, fn, i3x3, i3x2, i2x3);
+    add_intrinsic2(ctx, fn, i3x4, i3x2, i2x4);
+    add_intrinsic2(ctx, fn, i3x1, i3x3, i3x1);
+    add_intrinsic2(ctx, fn, i3x2, i3x3, i3x2);
+    add_intrinsic2(ctx, fn, i3x3, i3x3, i3x3);
+    add_intrinsic2(ctx, fn, i3x4, i3x3, i3x4);
+    add_intrinsic2(ctx, fn, i3x1, i3x4, i4x1);
+    add_intrinsic2(ctx, fn, i3x2, i3x4, i4x2);
+    add_intrinsic2(ctx, fn, i3x3, i3x4, i4x3);
+    add_intrinsic2(ctx, fn, i3x4, i3x4, i4x4);
+    add_intrinsic2(ctx, fn, i4x1, i4x1, i1x1);
+    add_intrinsic2(ctx, fn, i4x2, i4x1, i1x2);
+    add_intrinsic2(ctx, fn, i4x3, i4x1, i1x3);
+    add_intrinsic2(ctx, fn, i4x4, i4x1, i1x4);
+    add_intrinsic2(ctx, fn, i4x1, i4x2, i2x1);
+    add_intrinsic2(ctx, fn, i4x2, i4x2, i2x2);
+    add_intrinsic2(ctx, fn, i4x3, i4x2, i2x3);
+    add_intrinsic2(ctx, fn, i4x4, i4x2, i2x4);
+    add_intrinsic2(ctx, fn, i4x1, i4x3, i3x1);
+    add_intrinsic2(ctx, fn, i4x2, i4x3, i3x2);
+    add_intrinsic2(ctx, fn, i4x3, i4x3, i3x3);
+    add_intrinsic2(ctx, fn, i4x4, i4x3, i3x4);
+    add_intrinsic2(ctx, fn, i4x1, i4x4, i4x1);
+    add_intrinsic2(ctx, fn, i4x2, i4x4, i4x2);
+    add_intrinsic2(ctx, fn, i4x3, i4x4, i4x3);
+    add_intrinsic2(ctx, fn, i4x4, i4x4, i4x4);
+    add_intrinsic2(ctx, fn, f1x1, f1x1, f1x1);
+    add_intrinsic2(ctx, fn, f1x2, f1x1, f1x2);
+    add_intrinsic2(ctx, fn, f1x3, f1x1, f1x3);
+    add_intrinsic2(ctx, fn, f1x4, f1x1, f1x4);
+    add_intrinsic2(ctx, fn, f1x1, f1x2, f2x1);
+    add_intrinsic2(ctx, fn, f1x2, f1x2, f2x2);
+    add_intrinsic2(ctx, fn, f1x3, f1x2, f2x3);
+    add_intrinsic2(ctx, fn, f1x4, f1x2, f2x4);
+    add_intrinsic2(ctx, fn, f1x1, f1x3, f3x1);
+    add_intrinsic2(ctx, fn, f1x2, f1x3, f3x2);
+    add_intrinsic2(ctx, fn, f1x3, f1x3, f3x3);
+    add_intrinsic2(ctx, fn, f1x4, f1x3, f3x4);
+    add_intrinsic2(ctx, fn, f1x1, f1x4, f4x1);
+    add_intrinsic2(ctx, fn, f1x2, f1x4, f4x2);
+    add_intrinsic2(ctx, fn, f1x3, f1x4, f4x3);
+    add_intrinsic2(ctx, fn, f1x4, f1x4, f4x4);
+    add_intrinsic2(ctx, fn, f2x1, f2x1, f1x1);
+    add_intrinsic2(ctx, fn, f2x2, f2x1, f1x2);
+    add_intrinsic2(ctx, fn, f2x3, f2x1, f1x3);
+    add_intrinsic2(ctx, fn, f2x4, f2x1, f1x4);
+    add_intrinsic2(ctx, fn, f2x1, f2x2, f2x1);
+    add_intrinsic2(ctx, fn, f2x2, f2x2, f2x2);
+    add_intrinsic2(ctx, fn, f2x3, f2x2, f2x3);
+    add_intrinsic2(ctx, fn, f2x4, f2x2, f2x4);
+    add_intrinsic2(ctx, fn, f2x1, f2x3, f3x1);
+    add_intrinsic2(ctx, fn, f2x2, f2x3, f3x2);
+    add_intrinsic2(ctx, fn, f2x3, f2x3, f3x3);
+    add_intrinsic2(ctx, fn, f2x4, f2x3, f3x4);
+    add_intrinsic2(ctx, fn, f2x1, f2x4, f4x1);
+    add_intrinsic2(ctx, fn, f2x2, f2x4, f4x2);
+    add_intrinsic2(ctx, fn, f2x3, f2x4, f4x3);
+    add_intrinsic2(ctx, fn, f2x4, f2x4, f4x4);
+    add_intrinsic2(ctx, fn, f3x1, f3x1, f1x1);
+    add_intrinsic2(ctx, fn, f3x2, f3x1, f1x2);
+    add_intrinsic2(ctx, fn, f3x3, f3x1, f1x3);
+    add_intrinsic2(ctx, fn, f3x4, f3x1, f1x4);
+    add_intrinsic2(ctx, fn, f3x1, f3x2, f2x1);
+    add_intrinsic2(ctx, fn, f3x2, f3x2, f2x2);
+    add_intrinsic2(ctx, fn, f3x3, f3x2, f2x3);
+    add_intrinsic2(ctx, fn, f3x4, f3x2, f2x4);
+    add_intrinsic2(ctx, fn, f3x1, f3x3, f3x1);
+    add_intrinsic2(ctx, fn, f3x2, f3x3, f3x2);
+    add_intrinsic2(ctx, fn, f3x3, f3x3, f3x3);
+    add_intrinsic2(ctx, fn, f3x4, f3x3, f3x4);
+    add_intrinsic2(ctx, fn, f3x1, f3x4, f4x1);
+    add_intrinsic2(ctx, fn, f3x2, f3x4, f4x2);
+    add_intrinsic2(ctx, fn, f3x3, f3x4, f4x3);
+    add_intrinsic2(ctx, fn, f3x4, f3x4, f4x4);
+    add_intrinsic2(ctx, fn, f4x1, f4x1, f1x1);
+    add_intrinsic2(ctx, fn, f4x2, f4x1, f1x2);
+    add_intrinsic2(ctx, fn, f4x3, f4x1, f1x3);
+    add_intrinsic2(ctx, fn, f4x4, f4x1, f1x4);
+    add_intrinsic2(ctx, fn, f4x1, f4x2, f2x1);
+    add_intrinsic2(ctx, fn, f4x2, f4x2, f2x2);
+    add_intrinsic2(ctx, fn, f4x3, f4x2, f2x3);
+    add_intrinsic2(ctx, fn, f4x4, f4x2, f2x4);
+    add_intrinsic2(ctx, fn, f4x1, f4x3, f3x1);
+    add_intrinsic2(ctx, fn, f4x2, f4x3, f3x2);
+    add_intrinsic2(ctx, fn, f4x3, f4x3, f3x3);
+    add_intrinsic2(ctx, fn, f4x4, f4x3, f3x4);
+    add_intrinsic2(ctx, fn, f4x1, f4x4, f4x1);
+    add_intrinsic2(ctx, fn, f4x2, f4x4, f4x2);
+    add_intrinsic2(ctx, fn, f4x3, f4x4, f4x3);
+    add_intrinsic2(ctx, fn, f4x4, f4x4, f4x4);
+} // add_intrinsic_mul
+
+static void init_builtins(Context *ctx)
+{
+    // add in standard typedefs...
+    const struct
+    {
+        const char *str;
+        const MOJOSHADER_astDataType *datatype;
+    } types[] = {
+        { "bool", &ctx->dt_bool },
+        { "int", &ctx->dt_int },
+        { "uint", &ctx->dt_uint },
+        { "half", &ctx->dt_half },
+        { "float", &ctx->dt_float },
+        { "double", &ctx->dt_double },
+    };
+
+    int i, j, k;
+    for (i = 0; i < STATICARRAYLEN(types); i++)
+    {
+        char buf[32];
+        int len;
+        const MOJOSHADER_astDataType *dt;
+
+        for (j = 1; j <= 4; j++)
+        {
+            // "float2"
+            dt = new_datatype_vector(ctx, types[i].datatype, j);
+            len = snprintf(buf, sizeof (buf), "%s%d", types[i].str, j);
+            push_usertype(ctx, stringcache_len(ctx->strcache, buf, len), dt);
+            for (k = 1; k <= 4; k++)
+            {
+                // "float2x2"
+                dt = new_datatype_matrix(ctx, types[i].datatype, j, k);
+                len = snprintf(buf, sizeof (buf), "%s%dx%d", types[i].str,j,k);
+                push_usertype(ctx, stringcache_len(ctx->strcache,buf,len), dt);
+            } // for
+        } // for
+    } // for
+
+    // !!! FIXME: block these out by pixel/vertex/etc shader.
+    // !!! FIXME: calculate actual shader model (or maybe just let bytecode verifier throw up?).
+    const int shader_model = 3;
+    if (shader_model >= 1)
+    {
+        add_intrinsic_SAME1_ANYfi(ctx, stringcache(ctx->strcache, "abs"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "acos"));
+        add_intrinsic_BOOL_ANYfib(ctx, stringcache(ctx->strcache, "all"));
+        add_intrinsic_BOOL_ANYfib(ctx, stringcache(ctx->strcache, "any"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "asin"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "atan"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "atan2"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "ceil"));
+        add_intrinsic_SAME1_ANYfi_SAME1_SAME1(ctx, stringcache(ctx->strcache, "clamp"));
+        add_intrinsic_VOID_ANYf(ctx, stringcache(ctx->strcache, "clip"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "cos"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "cosh"));
+        add_intrinsic_3f_3f_3f(ctx, stringcache(ctx->strcache, "cross"));
+        add_intrinsic_4i_4f(ctx, stringcache(ctx->strcache, "D3DCOLORtoUBYTE4"));
+        add_intrinsic_f_Vf_SAME1(ctx, stringcache(ctx->strcache, "distance"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "degrees"));
+        add_intrinsic_f_SQUAREMATRIXf(ctx, stringcache(ctx->strcache, "determinant"));
+        add_intrinsic_fi_Vfi_SAME1(ctx, stringcache(ctx->strcache, "dot"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "exp"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "exp2"));
+        add_intrinsic_SAME1_Vf_SAME1_SAME1(ctx, stringcache(ctx->strcache, "faceforward"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "floor"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "fmod"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "frac"));
+        add_intrinsic_BOOL_ANYf(ctx, stringcache(ctx->strcache, "isfinite"));
+        add_intrinsic_BOOL_ANYf(ctx, stringcache(ctx->strcache, "isinf"));
+        add_intrinsic_BOOL_ANYf(ctx, stringcache(ctx->strcache, "isnan"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "ldexp"));
+        add_intrinsic_f_Vf(ctx, stringcache(ctx->strcache, "length"));
+        add_intrinsic_SAME1_ANYf_SAME1_SAME1(ctx, stringcache(ctx->strcache, "lerp"));
+        add_intrinsic_4f_f_f_f(ctx, stringcache(ctx->strcache, "lit"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "log"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "log10"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "log2"));
+        add_intrinsic_SAME1_ANYfi_SAME1(ctx, stringcache(ctx->strcache, "max"));
+        add_intrinsic_SAME1_ANYfi_SAME1(ctx, stringcache(ctx->strcache, "min"));
+        add_intrinsic_SAME1_ANYfi_SAME1(ctx, stringcache(ctx->strcache, "modf"));  // !!! FIXME: out var?
+        add_intrinsic_mul(ctx, stringcache(ctx->strcache, "mul"));
+        add_intrinsic_f_Vf(ctx, stringcache(ctx->strcache, "noise"));
+        add_intrinsic_SAME1_Vf(ctx, stringcache(ctx->strcache, "normalize"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "pow"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "radians"));
+        add_intrinsic_SAME1_ANYfi_SAME1(ctx, stringcache(ctx->strcache, "reflect"));
+        add_intrinsic_SAME1_Vf_SAME1_f(ctx, stringcache(ctx->strcache, "refract"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "round"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "rsqrt"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "saturate"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "sign"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "sin"));
+        add_intrinsic_VOID_ANYf_SAME1_SAME1(ctx, stringcache(ctx->strcache, "sincos"));  // !!! FIXME: out var?
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "sinh"));
+        add_intrinsic_SAME1_ANYf_SAME1_SAME1(ctx, stringcache(ctx->strcache, "smoothstep"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "sqrt"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "step"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "tan"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "tanh"));
+        add_intrinsic_4f_s1_f(ctx, stringcache(ctx->strcache, "tex1D"));
+        add_intrinsic_4f_s2_2f(ctx, stringcache(ctx->strcache, "tex2D"));
+        add_intrinsic_4f_s3_3f(ctx, stringcache(ctx->strcache, "tex3D"));
+        add_intrinsic_4f_sc_3f(ctx, stringcache(ctx->strcache, "texCUBE"));
+        add_intrinsic_SAME1_Mfib(ctx, stringcache(ctx->strcache, "transpose"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "trunc"));
+    } // if
+
+    if (shader_model >= 2)
+    {
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "ddx"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "ddy"));
+        add_intrinsic_SAME1_ANYf_SAME1(ctx, stringcache(ctx->strcache, "frexp"));
+        add_intrinsic_SAME1_ANYf(ctx, stringcache(ctx->strcache, "fwidth"));
+        add_intrinsic_4f_s1_f_f_f(ctx, stringcache(ctx->strcache, "tex1D"));
+        add_intrinsic_4f_s1_4f(ctx, stringcache(ctx->strcache, "tex1Dbias"));
+        add_intrinsic_4f_s1_f_f_f(ctx, stringcache(ctx->strcache, "tex1Dgrad"));
+        add_intrinsic_4f_s1_4f(ctx, stringcache(ctx->strcache, "tex1Dproj"));
+        add_intrinsic_4f_s2_2f_2f_2f(ctx, stringcache(ctx->strcache, "tex2D"));
+        add_intrinsic_4f_s2_4f(ctx, stringcache(ctx->strcache, "tex2Dbias"));
+        add_intrinsic_4f_s2_2f_2f_2f(ctx, stringcache(ctx->strcache, "tex2Dgrad"));
+        add_intrinsic_4f_s2_4f(ctx, stringcache(ctx->strcache, "tex2Dproj"));
+        add_intrinsic_4f_s3_3f_3f_3f(ctx, stringcache(ctx->strcache, "tex3D"));
+        add_intrinsic_4f_s3_4f(ctx, stringcache(ctx->strcache, "tex3Dbias"));
+        add_intrinsic_4f_s3_3f_3f_3f(ctx, stringcache(ctx->strcache, "tex3Dgrad"));
+        add_intrinsic_4f_s3_4f(ctx, stringcache(ctx->strcache, "tex3Dproj"));
+        add_intrinsic_4f_sc_3f_3f_3f(ctx, stringcache(ctx->strcache, "texCUBE"));
+        add_intrinsic_4f_sc_4f(ctx, stringcache(ctx->strcache, "texCUBEbias"));
+        add_intrinsic_4f_sc_3f_3f_3f(ctx, stringcache(ctx->strcache, "texCUBEgrad"));
+        add_intrinsic_4f_sc_4f(ctx, stringcache(ctx->strcache, "texCUBEproj"));
+    } // if
+
+    if (shader_model >= 3)
+    {
+        add_intrinsic_4f_s1_4f(ctx, stringcache(ctx->strcache, "tex1Dlod"));
+        add_intrinsic_4f_s2_4f(ctx, stringcache(ctx->strcache, "tex2Dlod"));
+        add_intrinsic_4f_s3_4f(ctx, stringcache(ctx->strcache, "tex3Dlod"));
+        add_intrinsic_4f_sc_4f(ctx, stringcache(ctx->strcache, "texCUBElod"));
+    } // if
+} // init_builtins
+
+
+// parse the source code into an AST.
+static void parse_source(Context *ctx, const char *filename,
+                         const char *source, unsigned int sourcelen,
+                         const MOJOSHADER_preprocessorDefine *defines,
+                         unsigned int define_count,
+                         MOJOSHADER_includeOpen include_open,
+                         MOJOSHADER_includeClose include_close)
+{
+    TokenData data;
+    unsigned int tokenlen;
+    Token tokenval;
+    const char *token;
+    int lemon_token;
+    const char *fname;
+    Preprocessor *pp;
+    void *parser;
+
+    if (!include_open) include_open = MOJOSHADER_internal_include_open;
+    if (!include_close) include_close = MOJOSHADER_internal_include_close;
+
+    pp = preprocessor_start(filename, source, sourcelen, include_open,
+                            include_close, defines, define_count, 0,
+                            MallocBridge, FreeBridge, ctx);
+    if (pp == NULL)
+    {
+        assert(ctx->out_of_memory);  // shouldn't fail for any other reason.
+        return;
+    } // if
+
+    parser = ParseHLSLAlloc(ctx->malloc, ctx->malloc_data);
+    if (parser == NULL)
+    {
+        assert(ctx->out_of_memory);  // shouldn't fail for any other reason.
+        preprocessor_end(pp);
+        return;
+    } // if
+
+    // !!! FIXME: check if (parser == NULL)...
+
+    init_builtins(ctx);
+
+    SymbolScope *start_scope = ctx->usertypes.scope;
+
+    #if DEBUG_COMPILER_PARSER
+    ParseHLSLTrace(stdout, "COMPILER: ");
+    #endif
+
+    // Run the preprocessor/lexer/parser...
+    int is_pragma = 0;   // !!! FIXME: remove this later when we can parse #pragma.
+    int skipping = 0; // !!! FIXME: remove this later when we can parse #pragma.
+    do {
+        token = preprocessor_nexttoken(pp, &tokenlen, &tokenval);
+
+        if (ctx->out_of_memory)
+            break;
+
+        fname = preprocessor_sourcepos(pp, &ctx->sourceline);
+        ctx->sourcefile = fname ? stringcache(ctx->strcache, fname) : 0;
+
+        if ((tokenval == TOKEN_HASH) || (tokenval == TOKEN_HASHHASH))
+            tokenval = TOKEN_BAD_CHARS;
+
+        if (tokenval == TOKEN_BAD_CHARS)
+        {
+            fail(ctx, "Bad characters in source file");
+            continue;
+        } // else if
+
+        else if (tokenval == TOKEN_PREPROCESSING_ERROR)
+        {
+            fail(ctx, token);  // this happens to be null-terminated.
+            continue;
+        } // else if
+
+        else if (tokenval == TOKEN_PP_PRAGMA)
+        {
+            assert(!is_pragma);
+            is_pragma = 1;
+            skipping = 1;
+            continue;
+        }
+
+        else if (tokenval == ((Token) '\n'))
+        {
+            assert(is_pragma);
+            is_pragma = 0;
+            skipping = 0;
+            continue;
+        }
+
+        else if (skipping)
+        {
+            continue;
+        }
+
+        // !!! FIXME: this is a mess, decide who should be doing this stuff, and only do it once.
+        lemon_token = convert_to_lemon_token(ctx, token, tokenlen, tokenval);
+        switch (lemon_token)
+        {
+            case TOKEN_HLSL_INT_CONSTANT:
+                data.i64 = strtoi64(token, tokenlen);
+                break;
+
+            case TOKEN_HLSL_FLOAT_CONSTANT:
+                data.dbl = strtodouble(token, tokenlen);
+                break;
+
+            case TOKEN_HLSL_USERTYPE:
+                data.string = stringcache_len(ctx->strcache, token, tokenlen);
+                data.datatype = get_usertype(ctx, data.string);  // !!! FIXME: do we need this? It's kind of useless during parsing.
+                assert(data.datatype != NULL);
+                break;
+
+            case TOKEN_HLSL_STRING_LITERAL:
+            case TOKEN_HLSL_IDENTIFIER:
+                data.string = stringcache_len(ctx->strcache, token, tokenlen);
+                break;
+
+            default:
+                data.i64 = 0;
+                break;
+        } // switch
+
+        ParseHLSL(parser, lemon_token, data, ctx);
+
+        // this probably isn't perfect, but it's good enough for surviving
+        //  the parse. We'll sort out correctness once we have a tree.
+        if (lemon_token == TOKEN_HLSL_LBRACE)
+            push_scope(ctx);
+        else if (lemon_token == TOKEN_HLSL_RBRACE)
+            pop_scope(ctx);
+    } while (tokenval != TOKEN_EOI);
+
+    // Clean out extra usertypes; they are dummies until semantic analysis.
+    while (ctx->usertypes.scope != start_scope)
+        pop_symbol(ctx, &ctx->usertypes);
+
+    ParseHLSLFree(parser, ctx->free, ctx->malloc_data);
+    preprocessor_end(pp);
+} // parse_source
+
+
+/* Intermediate representation... */
+
+static inline int generate_ir_label(Context *ctx)
+{
+    return ctx->ir_label_count++;
+} // generate_ir_label
+
+static inline int generate_ir_temp(Context *ctx)
+{
+    return ctx->ir_temp_count++;
+} // generate_ir_temp
+
+static const LoopLabels *push_ir_loop(Context *ctx, const int isswitch)
+{
+    // !!! FIXME: cache these allocations?
+    LoopLabels *retval = (LoopLabels *)Malloc(ctx, sizeof (LoopLabels));
+    if (retval)
+    {
+        retval->start = (isswitch) ? -1 : generate_ir_label(ctx);
+        retval->end = generate_ir_label(ctx);
+        retval->prev = ctx->ir_loop;
+        ctx->ir_loop = retval;
+    } // if
+
+    return retval;
+} // push_ir_loop
+
+static void pop_ir_loop(Context *ctx)
+{
+    assert(ctx->ir_loop != NULL);
+    LoopLabels *labels = ctx->ir_loop;
+    ctx->ir_loop = ctx->ir_loop->prev;
+    Free(ctx, labels);
+} // pop_ir_loop
+
+
+#define NEW_IR_NODE(retval, cls, typ) \
+    cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
+    do { \
+        if (retval == NULL) { return NULL; } \
+        retval->ir.type = typ; \
+        retval->ir.filename = ctx->sourcefile; \
+        retval->ir.line = ctx->sourceline; \
+    } while (0)
+
+#define NEW_IR_EXPR(retval, cls, typ, dt, numelems) \
+    cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
+    do { \
+        if (retval == NULL) { return NULL; } \
+        retval->info.ir.type = typ; \
+        retval->info.ir.filename = ctx->sourcefile; \
+        retval->info.ir.line = ctx->sourceline; \
+        retval->info.type = dt; \
+        retval->info.elements = numelems; \
+    } while (0)
+
+// syntactic sugar.
+static inline MOJOSHADER_irNode *build_ir(Context *ctx, void *_ast);
+static inline MOJOSHADER_irExpression *build_ir_expr(Context *ctx, void *_ast)
+{
+    MOJOSHADER_irNode *retval = build_ir(ctx, _ast);
+    assert(!retval || (retval->ir.type > MOJOSHADER_IR_START_RANGE_EXPR));
+    assert(!retval || (retval->ir.type < MOJOSHADER_IR_END_RANGE_EXPR));
+    return (MOJOSHADER_irExpression *) retval;
+} // build_ir_expr
+
+static inline MOJOSHADER_irStatement *build_ir_stmt(Context *ctx, void *_ast)
+{
+    MOJOSHADER_irNode *retval = build_ir(ctx, _ast);
+    assert(!retval || (retval->ir.type > MOJOSHADER_IR_START_RANGE_STMT));
+    assert(!retval || (retval->ir.type < MOJOSHADER_IR_END_RANGE_STMT));
+    return (MOJOSHADER_irStatement *) retval;
+} // build_ir_stmt
+
+
+static MOJOSHADER_irExpression *new_ir_binop(Context *ctx,
+                                       const MOJOSHADER_irBinOpType op,
+                                       MOJOSHADER_irExpression *left,
+                                       MOJOSHADER_irExpression *right)
+{
+    if ((!left) || (!right)) return NULL;
+    NEW_IR_EXPR(retval, MOJOSHADER_irBinOp, MOJOSHADER_IR_BINOP, left->info.type, left->info.elements);
+    assert(left->info.type == right->info.type);
+    assert(left->info.elements == right->info.elements);
+    retval->op = op;
+    retval->left = left;
+    retval->right = right;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_binop
+
+static MOJOSHADER_irExpression *new_ir_eseq(Context *ctx,
+                                      MOJOSHADER_irStatement *stmt,
+                                      MOJOSHADER_irExpression *expr)
+{
+    if (!expr) return NULL;
+    NEW_IR_EXPR(retval, MOJOSHADER_irESeq, MOJOSHADER_IR_ESEQ, expr->info.type, expr->info.elements);
+    retval->stmt = stmt;
+    retval->expr = expr;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_eseq
+
+static MOJOSHADER_irExpression *new_ir_temp(Context *ctx, const int index,
+                                            const MOJOSHADER_astDataTypeType type,
+                                            const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irTemp, MOJOSHADER_IR_TEMP, type, elements);
+    retval->index = index;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_temp
+
+
+
+#define NEW_IR_BINOP(op,l,r) new_ir_binop(ctx, MOJOSHADER_IR_BINOP_##op, l, r)
+#define EASY_IR_BINOP(op) \
+    NEW_IR_BINOP(op, build_ir_expr(ctx, ast->binary.left), \
+                 build_ir_expr(ctx, ast->binary.right))
+
+// You have to fill in ->value yourself!
+static MOJOSHADER_irExpression *new_ir_constant(Context *ctx,
+                                                const MOJOSHADER_astDataTypeType type,
+                                                const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irConstant, MOJOSHADER_IR_CONSTANT, type, elements);
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_constant
+
+static MOJOSHADER_irExpression *new_ir_constint(Context *ctx, const int val)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irConstant, MOJOSHADER_IR_CONSTANT, MOJOSHADER_AST_DATATYPE_INT, 1);
+    retval->value.ival[0] = val;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_constint
+
+static MOJOSHADER_irExpression *new_ir_constfloat(Context *ctx, const float val)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irConstant, MOJOSHADER_IR_CONSTANT, MOJOSHADER_AST_DATATYPE_FLOAT, 1);
+    retval->value.fval[0] = val;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_constfloat
+
+static MOJOSHADER_irExpression *new_ir_constbool(Context *ctx, const int val)
+{
+    // !!! FIXME: cache true and false in (ctx), ignore in delete_ir().
+    NEW_IR_EXPR(retval, MOJOSHADER_irConstant, MOJOSHADER_IR_CONSTANT, MOJOSHADER_AST_DATATYPE_BOOL, 1);
+    retval->value.ival[0] = val;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_constbool
+
+static MOJOSHADER_irExpression *new_ir_convert(Context *ctx, MOJOSHADER_irExpression *expr,
+                                               const MOJOSHADER_astDataTypeType type,
+                                               const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irConvert, MOJOSHADER_IR_CONVERT, type, elements);
+    retval->expr = expr;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_convert
+
+static MOJOSHADER_irExpression *new_ir_construct(Context *ctx, MOJOSHADER_irExprList *args,
+                                               const MOJOSHADER_astDataTypeType type,
+                                               const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irConstruct, MOJOSHADER_IR_CONSTRUCT, type, elements);
+    retval->args = args;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_construct
+
+static MOJOSHADER_irExpression *new_ir_call(Context *ctx, const int index,
+                                            MOJOSHADER_irExprList *args,
+                                            const MOJOSHADER_astDataTypeType type,
+                                            const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irCall, MOJOSHADER_IR_CALL, type, elements);
+    retval->args = args;
+    retval->index = index;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_call
+
+static MOJOSHADER_irExpression *new_ir_swizzle(Context *ctx,
+                                               MOJOSHADER_irExpression *expr,
+                                               const char *channels,
+                                               const MOJOSHADER_astDataTypeType type,
+                                               const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irSwizzle, MOJOSHADER_IR_SWIZZLE, type, elements);
+    retval->expr = expr;
+    memcpy(retval->channels, channels, sizeof (retval->channels));
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_swizzle
+
+static MOJOSHADER_irExpression *new_ir_memory(Context *ctx, const int index,
+                                              const MOJOSHADER_astDataTypeType type,
+                                              const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irMemory, MOJOSHADER_IR_MEMORY, type, elements);
+    retval->index = index;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_memory
+
+static MOJOSHADER_irExpression *new_ir_array(Context *ctx,
+                                             MOJOSHADER_irExpression *array,
+                                             MOJOSHADER_irExpression *element,
+                                             const MOJOSHADER_astDataTypeType type,
+                                             const int elements)
+{
+    NEW_IR_EXPR(retval, MOJOSHADER_irArray, MOJOSHADER_IR_ARRAY, type, elements);
+    retval->array = array;
+    retval->element = element;
+    return (MOJOSHADER_irExpression *) retval;
+} // new_ir_array
+
+static MOJOSHADER_irStatement *new_ir_seq(Context *ctx,
+                                     MOJOSHADER_irStatement *first,
+                                     MOJOSHADER_irStatement *next)
+{
+    assert((first != NULL) || (next != NULL));
+    if (first == NULL)  // don't generate a SEQ if unnecessary.
+        return next;
+    else if (next == NULL)
+        return first;
+
+    NEW_IR_NODE(retval, MOJOSHADER_irSeq, MOJOSHADER_IR_SEQ);
+    retval->first = first;
+    retval->next = next;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_seq
+
+static MOJOSHADER_irStatement *new_ir_jump(Context *ctx, const int label)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irJump, MOJOSHADER_IR_JUMP);
+    retval->label = label;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_jump
+
+static MOJOSHADER_irStatement *new_ir_cjump(Context *ctx,
+                                       const MOJOSHADER_irConditionType cond,
+                                       MOJOSHADER_irExpression *left,
+                                       MOJOSHADER_irExpression *right,
+                                       const int iftrue, const int iffalse)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irCJump, MOJOSHADER_IR_CJUMP);
+    retval->cond = cond;
+    retval->left = left;
+    retval->right = right;
+    retval->iftrue = iftrue;
+    retval->iffalse = iffalse;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_cjump
+
+static MOJOSHADER_irStatement *new_ir_label(Context *ctx, const int index)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irLabel, MOJOSHADER_IR_LABEL);
+    retval->index = index;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_label
+
+static MOJOSHADER_irStatement *new_ir_move(Context *ctx,
+                                      MOJOSHADER_irExpression *dst,
+                                      MOJOSHADER_irExpression *src,
+                                      const int writemask)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irMove, MOJOSHADER_IR_MOVE);
+    assert(dst && src && (dst->info.type == src->info.type));
+    assert(dst && src && (dst->info.elements == src->info.elements));
+    retval->dst = dst;
+    retval->src = src;
+    retval->writemask = writemask;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_move
+
+static MOJOSHADER_irStatement *new_ir_expr_stmt(Context *ctx, MOJOSHADER_irExpression *expr)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irExprStmt, MOJOSHADER_IR_EXPR_STMT);
+    retval->expr = expr;
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_expr_stmt
+
+static MOJOSHADER_irStatement *new_ir_discard(Context *ctx)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irDiscard, MOJOSHADER_IR_DISCARD);
+    return (MOJOSHADER_irStatement *) retval;
+} // new_ir_discard
+
+static MOJOSHADER_irExprList *new_ir_exprlist(Context *ctx, MOJOSHADER_irExpression *expr)
+{
+    NEW_IR_NODE(retval, MOJOSHADER_irExprList, MOJOSHADER_IR_EXPRLIST);
+    retval->expr = expr;
+    retval->next = NULL;
+    return (MOJOSHADER_irExprList *) retval;
+} // new_ir_exprlist
+
+
+// This handles most comparison operators (less-than, equals, etc...)
+static MOJOSHADER_irExpression *build_ir_compare(Context *ctx,
+                                    const MOJOSHADER_irConditionType operation,
+                                    MOJOSHADER_irExpression *left,
+                                    MOJOSHADER_irExpression *right,
+                                    MOJOSHADER_irExpression *tval,
+                                    MOJOSHADER_irExpression *fval)
+{
+    /* The gist...
+            cjump x < y, t, f  // '<' is whatever operation
+        t:
+            move tmp, tval
+            jump join
+        f:
+            move tmp, fval
+        join:
+    */
+
+    const int t = generate_ir_label(ctx);
+    const int f = generate_ir_label(ctx);
+    const int join = generate_ir_label(ctx);
+    const int tmp = generate_ir_temp(ctx);
+
+    assert(tval && fval && (tval->info.type == fval->info.type));
+    assert(tval && fval && (tval->info.elements == fval->info.elements));
+
+    const MOJOSHADER_astDataTypeType dt = tval->info.type;
+    const int elements = tval->info.elements;
+
+    return new_ir_eseq(ctx,
+                new_ir_seq(ctx, new_ir_cjump(ctx, operation, left, right, t, f),
+                new_ir_seq(ctx, new_ir_label(ctx, t),
+                new_ir_seq(ctx, new_ir_move(ctx, new_ir_temp(ctx, tmp, dt, elements), tval, -1),
+                new_ir_seq(ctx, new_ir_jump(ctx, join),
+                new_ir_seq(ctx, new_ir_label(ctx, f),
+                new_ir_seq(ctx, new_ir_move(ctx, new_ir_temp(ctx, tmp, dt, elements), fval, -1),
+                                new_ir_label(ctx, join))))))),
+                    new_ir_temp(ctx, tmp, dt, elements));
+} // build_ir_compare
+
+#define EASY_IR_COMPARE(op) \
+    build_ir_compare(ctx, MOJOSHADER_IR_COND_##op, \
+                   build_ir_expr(ctx, ast->binary.left), \
+                   build_ir_expr(ctx, ast->binary.right), \
+                   new_ir_constbool(ctx, 1), \
+                   new_ir_constbool(ctx, 0))
+
+
+// This handles && and || operators.
+static MOJOSHADER_irExpression *build_ir_logical_and_or(Context *ctx,
+                                    const MOJOSHADER_astExpressionBinary *ast,
+                                    const int left_testval)
+{
+    /* The gist...
+            cjump left == left_testval, maybe, f
+        maybe:
+            cjump right == true, t, f
+        t:
+            move tmp, 1
+            jump join
+        f:
+            move tmp, 0
+        join:
+    */
+
+    assert(ast->left->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+    assert(ast->right->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+
+    const int t = generate_ir_label(ctx);
+    const int f = generate_ir_label(ctx);
+    const int maybe = generate_ir_label(ctx);
+    const int join = generate_ir_label(ctx);
+    const int tmp = generate_ir_temp(ctx);
+
+    return new_ir_eseq(ctx,
+                new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->left), new_ir_constbool(ctx, left_testval), maybe, f),
+                new_ir_seq(ctx, new_ir_label(ctx, maybe),
+                new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->right), new_ir_constbool(ctx, 1), t, f),
+                new_ir_seq(ctx, new_ir_label(ctx, t),
+                new_ir_seq(ctx, new_ir_move(ctx, new_ir_temp(ctx, tmp, MOJOSHADER_AST_DATATYPE_BOOL, 1), new_ir_constbool(ctx, 1), -1),
+                new_ir_seq(ctx, new_ir_jump(ctx, join),
+                new_ir_seq(ctx, new_ir_label(ctx, f),
+                new_ir_seq(ctx, new_ir_move(ctx, new_ir_temp(ctx, tmp, MOJOSHADER_AST_DATATYPE_BOOL, 1), new_ir_constbool(ctx, 0), -1),
+                                new_ir_label(ctx, join))))))))),
+                    new_ir_temp(ctx, tmp, MOJOSHADER_AST_DATATYPE_BOOL, 1));
+} // build_ir_logical_and_or
+
+static inline MOJOSHADER_irExpression *build_ir_logical_and(Context *ctx,
+                                    const MOJOSHADER_astExpressionBinary *ast)
+{
+    // this needs to not evaluate (right) if (left) is false!
+    return build_ir_logical_and_or(ctx, ast, 1);
+} // build_ir_logical_and
+
+static inline MOJOSHADER_irExpression *build_ir_logical_or(Context *ctx,
+                                    const MOJOSHADER_astExpressionBinary *ast)
+{
+    // this needs to not evaluate (right) if (left) is true!
+    return build_ir_logical_and_or(ctx, ast, 0);
+} // build_ir_logical_or
+
+static inline MOJOSHADER_irStatement *build_ir_no_op(Context *ctx)
+{
+    return new_ir_label(ctx, generate_ir_label(ctx));
+} // build_ir_no_op
+
+static MOJOSHADER_irStatement *build_ir_ifstmt(Context *ctx,
+                                          const MOJOSHADER_astIfStatement *ast)
+{
+    assert(ast->expr->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+
+    // !!! FIXME: ast->attributes?
+
+    // IF statement without an ELSE.
+    if (ast->else_statement == NULL)
+    {
+        /* The gist...
+                cjump expr, t, join
+            t:
+                statement
+            join:
+        */
+
+        const int t = generate_ir_label(ctx);
+        const int join = generate_ir_label(ctx);
+
+        return new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->expr), new_ir_constbool(ctx, 1), t, join),
+               new_ir_seq(ctx, new_ir_label(ctx, t),
+               new_ir_seq(ctx, build_ir_stmt(ctx, ast->statement),
+               new_ir_seq(ctx, new_ir_label(ctx, join),
+                               build_ir_stmt(ctx, ast->next)))));
+    } // if
+
+    // IF statement _with_ an ELSE.
+    /* The gist...
+            cjump expr, t, f
+        t:
+            statement
+            jump join
+        f:
+            elsestatement
+        join:
+    */
+
+    const int t = generate_ir_label(ctx);
+    const int f = generate_ir_label(ctx);
+    const int join = generate_ir_label(ctx);
+
+    return new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->expr), new_ir_constbool(ctx, 1), t, f),
+           new_ir_seq(ctx, new_ir_label(ctx, t),
+           new_ir_seq(ctx, build_ir_stmt(ctx, ast->statement),
+           new_ir_seq(ctx, new_ir_jump(ctx, join),
+           new_ir_seq(ctx, new_ir_label(ctx, f),
+           new_ir_seq(ctx, build_ir_stmt(ctx, ast->else_statement),
+           new_ir_seq(ctx, new_ir_label(ctx, join),
+                           build_ir_stmt(ctx, ast->next))))))));
+} // build_ir_ifstmt
+
+
+static MOJOSHADER_irStatement *build_ir_forstmt(Context *ctx,
+                                       const MOJOSHADER_astForStatement *ast)
+{
+    // !!! FIXME: ast->unroll
+
+    assert(ast->looptest->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+
+    /* The gist...
+            initializer  // (or var_decl->initializer!)
+        test:
+            cjump looptest == true, loop, join
+        loop:
+            statement
+        increment:  // needs to be here; this is where "continue" jumps!
+            counter
+            jump test
+        join:
+    */
+
+    const int test = generate_ir_label(ctx);
+    const int loop = generate_ir_label(ctx);
+
+    const LoopLabels *labels = push_ir_loop(ctx, 0);
+    if (labels == NULL)
+        return NULL;  // out of memory...
+
+    const int increment = labels->start;
+    const int join = labels->end;
+
+    assert( (ast->var_decl && !ast->initializer) ||
+            (!ast->var_decl && ast->initializer) );
+
+    MOJOSHADER_irStatement *init = NULL;
+    if (ast->var_decl != NULL)
+    {
+//sdfsdf
+        // !!! FIXME: map the initializer to the variable? Need fix to var_decl parsing.
+//        new_ir_move(ctx, FIXME MAP TO REGISTER ast->var_decl->index, build_ir_expr(ctx, ast->fsdf));
+//        FIXME
+//        init = build_ir_vardecl(ctx, ast->var_decl);
+    } // if
+    else
+    {
+//        init = build_ir_expr(ctx, ast->initializer);
+    } // else
+
+    MOJOSHADER_irStatement *retval =
+        new_ir_seq(ctx, init,
+        new_ir_seq(ctx, new_ir_label(ctx, test),
+        new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->looptest), new_ir_constbool(ctx, 1), loop, join),
+        new_ir_seq(ctx, new_ir_label(ctx, loop),
+        new_ir_seq(ctx, build_ir_stmt(ctx, ast->statement),
+        new_ir_seq(ctx, new_ir_label(ctx, increment),
+        new_ir_seq(ctx, new_ir_expr_stmt(ctx, build_ir_expr(ctx, ast->counter)),
+        new_ir_seq(ctx, new_ir_jump(ctx, test),
+                        new_ir_label(ctx, join)))))))));
+
+    pop_ir_loop(ctx);
+
+    return new_ir_seq(ctx, retval, build_ir_stmt(ctx, ast->next));
+} // build_ir_forstmt
+
+static MOJOSHADER_irStatement *build_ir_whilestmt(Context *ctx,
+                                          const MOJOSHADER_astWhileStatement *ast)
+{
+    // !!! FIXME: ast->unroll
+
+    assert(ast->expr->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+
+    /* The gist...
+        loop:
+            cjump expr == true, t, join
+        t:
+            statement
+            jump loop
+        join:
+    */
+
+    const LoopLabels *labels = push_ir_loop(ctx, 0);
+    if (labels == NULL)
+        return NULL;  // out of memory...
+
+    const int loop = labels->start;
+    const int t = generate_ir_label(ctx);
+    const int join = labels->end;
+
+    MOJOSHADER_irStatement *retval =
+        new_ir_seq(ctx, new_ir_label(ctx, loop),
+        new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->expr), new_ir_constbool(ctx, 1), t, join),
+        new_ir_seq(ctx, new_ir_label(ctx, t),
+        new_ir_seq(ctx, build_ir_stmt(ctx, ast->statement),
+        new_ir_seq(ctx, new_ir_jump(ctx, loop),
+                        new_ir_label(ctx, join))))));
+
+    pop_ir_loop(ctx);
+
+    return new_ir_seq(ctx, retval, build_ir_stmt(ctx, ast->next));
+} // build_ir_whilestmt
+
+static MOJOSHADER_irStatement *build_ir_dostmt(Context *ctx,
+                                          const MOJOSHADER_astDoStatement *ast)
+{
+    // !!! FIXME: ast->unroll
+
+    assert(ast->expr->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+
+    /* The gist...
+        loop:
+            statement
+            cjump expr == true, loop, join
+        join:
+    */
+
+    const LoopLabels *labels = push_ir_loop(ctx, 0);
+    if (labels == NULL)
+        return NULL;  // out of memory...
+
+    const int loop = labels->start;
+    const int join = labels->end;
+
+    MOJOSHADER_irStatement *retval =
+        new_ir_seq(ctx, new_ir_label(ctx, loop),
+        new_ir_seq(ctx, build_ir_stmt(ctx, ast->statement),
+        new_ir_seq(ctx, new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, ast->expr), new_ir_constbool(ctx, 1), loop, join),
+                        new_ir_label(ctx, join))));
+
+    pop_ir_loop(ctx);
+
+    return new_ir_seq(ctx, retval, build_ir_stmt(ctx, ast->next));
+} // build_ir_dostmt
+
+static MOJOSHADER_irStatement *build_ir_switch(Context *ctx, const MOJOSHADER_astSwitchStatement *ast)
+{
+    // Dithering down to a list of if-statements in all cases
+    //  isn't ideal, but we can't do jumptables in D3D bytecode.
+
+    // !!! FIXME: attributes?
+
+    /* The gist...
+            move tmp, expr
+            cjump tmp == case1expr, case1, testcase2
+        testcase2:  // etc
+            cjump tmp == case2expr, case2, join
+        case1:
+            case1stmt  // might have a break in it somewhere.
+        case2:
+            case2stmt
+        join:
+    */
+
+    const LoopLabels *labels = push_ir_loop(ctx, 1);
+    if (labels == NULL)
+        return NULL;  // out of memory...
+
+    const int join = labels->end;
+    const int elems = datatype_elems(ctx, ast->expr->datatype);
+    const MOJOSHADER_astDataTypeType dt = datatype_base(ctx, ast->expr->datatype)->type;
+
+    const MOJOSHADER_astSwitchCases *cases = ast->cases;
+    const int tmp = generate_ir_temp(ctx);
+    MOJOSHADER_irStatement *startseqs = new_ir_move(ctx, new_ir_temp(ctx, tmp, dt, elems), build_ir_expr(ctx, ast->expr), -1);
+    MOJOSHADER_irStatement *testseqs = startseqs;
+    MOJOSHADER_irStatement *startcaseseqs = NULL;
+    MOJOSHADER_irStatement *caseseqs = NULL;
+    while (cases)
+    {
+        const int t = generate_ir_label(ctx);
+        const int f = (cases->next == NULL) ? join : generate_ir_label(ctx);
+        MOJOSHADER_irStatement *cjump = new_ir_cjump(ctx, MOJOSHADER_IR_COND_EQL, build_ir_expr(ctx, cases->expr), new_ir_temp(ctx, tmp, dt, elems), t, f);
+
+        if (cases->next == NULL)  // last one, do the join label.
+        {
+            testseqs = new_ir_seq(ctx, testseqs, cjump);
+            caseseqs = new_ir_seq(ctx, caseseqs, new_ir_seq(ctx, new_ir_label(ctx, t), build_ir_stmt(ctx, cases->statement)));
+            caseseqs = new_ir_seq(ctx, caseseqs, new_ir_label(ctx, f));
+        } // if
+        else
+        {
+            testseqs = new_ir_seq(ctx, testseqs, new_ir_seq(ctx, cjump, new_ir_label(ctx, f)));
+            caseseqs = new_ir_seq(ctx, caseseqs, new_ir_seq(ctx, new_ir_label(ctx, t), build_ir_stmt(ctx, cases->statement)));
+        } // else
+
+        if (startcaseseqs == NULL)
+            startcaseseqs = caseseqs;
+
+        cases = cases->next;
+    } // while
+
+    pop_ir_loop(ctx);
+
+    return new_ir_seq(ctx, startseqs, new_ir_seq(ctx, startcaseseqs, build_ir_stmt(ctx, ast->next)));
+} // build_ir_switch
+
+static MOJOSHADER_irExpression *build_ir_increxpr(Context *ctx, const MOJOSHADER_astDataType *_dt,
+                                                  const int val)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, _dt);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    MOJOSHADER_irConstant *retval = (MOJOSHADER_irConstant *) new_ir_constant(ctx, type, elems);
+    int i;
+
+    switch (type)
+    {
+        case MOJOSHADER_AST_DATATYPE_BOOL:
+        case MOJOSHADER_AST_DATATYPE_INT:
+        case MOJOSHADER_AST_DATATYPE_UINT:
+            for (i = 0; i < elems; i++)
+                retval->value.ival[i] = (int) val;
+            break;
+
+        case MOJOSHADER_AST_DATATYPE_FLOAT:
+        case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
+        case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
+        case MOJOSHADER_AST_DATATYPE_HALF:
+        case MOJOSHADER_AST_DATATYPE_DOUBLE:
+            for (i = 0; i < elems; i++)
+                retval->value.fval[i] = (float) val;
+            break;
+
+        default:
+            assert(0 && "Semantic analysis should have caught this!");
+    } // switch
+
+    return (MOJOSHADER_irExpression *) retval;
+} // build_ir_increxpr
+
+static MOJOSHADER_irExpression *build_ir_preincdec(Context *ctx, MOJOSHADER_astExpressionUnary *ast, const MOJOSHADER_irBinOpType binop)
+{
+    /* The gist...
+        move expr, expr + 1
+        return expr
+    */
+    // !!! FIXME: can you writemask an increment operator?
+    MOJOSHADER_irExpression *constant = build_ir_increxpr(ctx, ast->datatype, 1);
+    return new_ir_eseq(ctx,
+                new_ir_move(ctx,
+                    build_ir_expr(ctx, ast->operand),
+                    new_ir_binop(ctx, binop, build_ir_expr(ctx, ast->operand), constant), -1),
+                build_ir_expr(ctx, ast->operand));
+} // build_ir_preincdec
+
+static MOJOSHADER_irExpression *build_ir_postincdec(Context *ctx, MOJOSHADER_astExpressionUnary *ast, const MOJOSHADER_irBinOpType binop)
+{
+    /* The gist...
+        move tmp, expr
+        move expr, expr + 1
+        return tmp
+    */
+
+    // !!! FIXME: can you writemask an increment operator?
+    MOJOSHADER_irExpression *constant = build_ir_increxpr(ctx, ast->datatype, 1);
+    const int tmp = generate_ir_temp(ctx);
+    return new_ir_eseq(ctx,
+                new_ir_seq(ctx,
+                    new_ir_move(ctx, new_ir_temp(ctx, tmp, constant->info.type, constant->info.elements), build_ir_expr(ctx, ast->operand), -1),
+                    new_ir_move(ctx, build_ir_expr(ctx, ast->operand),
+                        new_ir_binop(ctx, binop, build_ir_expr(ctx, ast->operand), constant), -1)),
+                new_ir_temp(ctx, tmp, constant->info.type, constant->info.elements));
+} // build_ir_postincdec
+
+static MOJOSHADER_irExpression *build_ir_convert(Context *ctx, const MOJOSHADER_astExpressionCast *ast)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    return new_ir_convert(ctx, build_ir_expr(ctx, ast->operand), type, elems);
+} // build_ir_convert
+
+static MOJOSHADER_irExprList *build_ir_exprlist(Context *ctx, MOJOSHADER_astArguments *args)
+{
+    MOJOSHADER_irExprList *retval = NULL;
+    MOJOSHADER_irExprList *prev = NULL;
+
+    while (args != NULL)
+    {
+        assert((retval && prev) || ((!retval) && (!prev)));
+
+        MOJOSHADER_irExprList *item = new_ir_exprlist(ctx, build_ir_expr(ctx, args->argument));
+        if (prev == NULL)
+            prev = retval = item;
+        else
+            prev->next = item;
+
+        args = args->next;
+    } // while
+
+    return retval;
+} // build_ir_exprlist
+
+static MOJOSHADER_irExpression *build_ir_constructor(Context *ctx, const MOJOSHADER_astExpressionConstructor *ast)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    assert(elems <= 16);  // just in case (matrix4x4 constructor is largest).
+    return new_ir_construct(ctx, build_ir_exprlist(ctx, ast->args), type, elems);
+} // build_ir_constructor
+
+static MOJOSHADER_irExpression *build_ir_call(Context *ctx, const MOJOSHADER_astExpressionCallFunction *ast)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    return new_ir_call(ctx, ast->identifier->index, build_ir_exprlist(ctx, ast->args), type, elems);
+} // build_ir_call
+
+static char swiz_to_channel(const char swiz)
+{
+    if ((swiz == 'r') || (swiz == 'x')) return 0;
+    if ((swiz == 'g') || (swiz == 'y')) return 1;
+    if ((swiz == 'b') || (swiz == 'z')) return 2;
+    if ((swiz == 'a') || (swiz == 'w')) return 3;
+    assert(0 && "Should have been caught by semantic analysis.");
+    return 0;
+} // swiz_to_channel
+
+static MOJOSHADER_irExpression *build_ir_swizzle(Context *ctx, const MOJOSHADER_astExpressionDerefStruct *ast)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    char chans[4] = { 0, 0, 0, 0 };
+    const char *swizstr = ast->member;
+    int i;
+
+    for (i = 0; swizstr[i]; i++)
+        chans[i] = swiz_to_channel(swizstr[i]);
+
+    return new_ir_swizzle(ctx, build_ir_expr(ctx, ast->identifier), chans, type, elems);
+} // build_ir_swizzle
+
+static MOJOSHADER_irExpression *build_ir_identifier(Context *ctx, const MOJOSHADER_astExpressionIdentifier *ast)
+{
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    return new_ir_memory(ctx, ast->index, type, elems);
+} // build_ir_identifier
+
+static MOJOSHADER_irExpression *build_ir_derefstruct(Context *ctx, const MOJOSHADER_astExpressionDerefStruct *ast)
+{
+    // There are only three possible IR nodes that contain a struct:
+    //  an irTemp, an irMemory, or an irESeq that results in a temp or memory.
+    //  As such, we figure out which it is, and offset appropriately for the
+    //  member.
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+    MOJOSHADER_irExpression *expr = build_ir_expr(ctx, ast->identifier);
+    MOJOSHADER_irExpression *finalexpr = expr;
+
+    if (expr == NULL)
+        return NULL;
+
+    assert(!ast->isswizzle);
+
+    while (finalexpr->ir.type == MOJOSHADER_IR_ESEQ)
+        finalexpr = finalexpr->eseq.expr;
+
+    if (finalexpr->ir.type == MOJOSHADER_IR_TEMP)
+        finalexpr->temp.index += ast->member_index;
+    else if (finalexpr->ir.type == MOJOSHADER_IR_MEMORY)
+        finalexpr->memory.index += ast->member_index;
+    else
+        assert(0 && "Unexpected condition");
+
+    // Replace the struct type with the type of the member.
+    expr->info.type = type;
+    expr->info.elements = elems;
+
+    return expr;
+} // build_ir_derefstruct
+
+static MOJOSHADER_irExpression *build_ir_derefarray(Context *ctx, const MOJOSHADER_astExpressionBinary *ast)
+{
+    // In most compilers, arrays dither down to offsets into memory, but
+    //  they're somewhat special in D3D, since they might have to deal with
+    //  vectors, etc...so we keep them as first-class citizens of the IR,
+    //  and let the optimizer/codegen sort it out.
+    // !!! FIXME: this might be the wrong move. Maybe remove this IR node type?
+    const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->datatype);
+    const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+    const int elems = datatype_elems(ctx, dt);
+
+    // !!! FIXME: Array dereference of a vector can become a simple swizzle operation, if we have a constant index.
+    // !!! FIXME: Matrix dereference of a vector can become a simple reference to a temp/memory, if we have a constant index.
+    return new_ir_array(ctx, build_ir_expr(ctx, ast->left), build_ir_expr(ctx, ast->right), type, elems);
+} // build_ir_derefarray
+
+static MOJOSHADER_irExpression *build_ir_assign_binop(Context *ctx,
+                                                const MOJOSHADER_irBinOpType op,
+                                                const MOJOSHADER_astExpressionBinary *ast)
+{
+    MOJOSHADER_irExpression *lvalue = build_ir_expr(ctx, ast->left);
+    MOJOSHADER_irExpression *rvalue = build_ir_expr(ctx, ast->right);
+    const MOJOSHADER_astDataTypeType type = lvalue->info.type;
+    const int elems = lvalue->info.elements;
+    const int tmp = generate_ir_temp(ctx);
+
+    // Semantic analysis should have inserted casts if necessary.
+    assert(type == rvalue->info.type);
+    assert(elems == rvalue->info.elements);
+
+    // The destination must eventually be lvalue, which means memory or temp.
+    MOJOSHADER_irExpression *dst = lvalue;
+    while (dst->ir.type == MOJOSHADER_IR_ESEQ)
+        dst = dst->eseq.expr;
+
+    if (dst->ir.type == MOJOSHADER_IR_TEMP)
+        dst = new_ir_temp(ctx, dst->temp.index, dst->info.type, dst->info.elements);
+    else if (dst->ir.type == MOJOSHADER_IR_MEMORY)
+        dst = new_ir_memory(ctx, dst->memory.index, dst->info.type, dst->info.elements);
+    else
+        assert(0 && "Unexpected condition");
+
+    // !!! FIXME: write masking!
+    return new_ir_eseq(ctx,
+                new_ir_seq(ctx,
+                    new_ir_move(ctx, new_ir_temp(ctx, tmp, type, elems), new_ir_binop(ctx, op, lvalue, rvalue), -1),
+                    new_ir_move(ctx, dst, new_ir_temp(ctx, tmp, type, elems), -1)),
+                new_ir_temp(ctx, tmp, type, elems));
+} // build_ir_assign_binop
+
+static MOJOSHADER_irExpression *build_ir_assign(Context *ctx,
+                                                const MOJOSHADER_astExpressionBinary *ast)
+{
+    MOJOSHADER_irExpression *lvalue = build_ir_expr(ctx, ast->left);
+    MOJOSHADER_irExpression *rvalue = build_ir_expr(ctx, ast->right);
+    const MOJOSHADER_astDataTypeType type = lvalue->info.type;
+    const int elems = lvalue->info.elements;
+    const int tmp = generate_ir_temp(ctx);
+
+    // Semantic analysis should have inserted casts if necessary.
+    assert(type == rvalue->info.type);
+    assert(elems == rvalue->info.elements);
+
+    // !!! FIXME: write masking!
+    // !!! FIXME: whole array/struct assignments need to become a sequence of moves.
+    return new_ir_eseq(ctx,
+                new_ir_seq(ctx,
+                    new_ir_move(ctx, new_ir_temp(ctx, tmp, type, elems), rvalue, -1),
+                    new_ir_move(ctx, lvalue, new_ir_temp(ctx, tmp, type, elems), -1)),
+                new_ir_temp(ctx, tmp, type, elems));
+} // build_ir_assign
+
+
+// The AST must be perfect and normalized and sane here. If there are any
+//  strange corner cases, you should strive to handle them in semantic
+//  analysis, so conversion to IR can proceed with a minimum of drama.
+static void *build_ir_internal(Context *ctx, void *_ast);
+static inline MOJOSHADER_irNode *build_ir(Context *ctx, void *_ast)
+{
+    return (MOJOSHADER_irNode *) build_ir_internal(ctx, _ast);
+} // build_ir
+
+static void *build_ir_internal(Context *ctx, void *_ast)
+{
+    if ((_ast == NULL) || (ctx->out_of_memory))
+        return NULL;
+
+    MOJOSHADER_astNode *ast = (MOJOSHADER_astNode *) _ast;
+
+    // upkeep so we report correct error locations...
+    ctx->sourcefile = ast->ast.filename;
+    ctx->sourceline = ast->ast.line;
+
+    switch (ast->ast.type)
+    {
+        case MOJOSHADER_AST_OP_PREINCREMENT:  // !!! FIXME: sequence points?
+            return build_ir_preincdec(ctx, &ast->unary, MOJOSHADER_IR_BINOP_ADD);
+
+        case MOJOSHADER_AST_OP_POSTINCREMENT: // !!! FIXME: sequence points?
+            return build_ir_postincdec(ctx, &ast->unary, MOJOSHADER_IR_BINOP_ADD);
+
+        case MOJOSHADER_AST_OP_PREDECREMENT:  // !!! FIXME: sequence points?
+            return build_ir_preincdec(ctx, &ast->unary, MOJOSHADER_IR_BINOP_SUBTRACT);
+
+        case MOJOSHADER_AST_OP_POSTDECREMENT: // !!! FIXME: sequence points?
+            return build_ir_postincdec(ctx, &ast->unary, MOJOSHADER_IR_BINOP_SUBTRACT);
+
+        case MOJOSHADER_AST_OP_COMPLEMENT:
+            return NEW_IR_BINOP(XOR, build_ir_expr(ctx, ast->unary.operand),
+                                new_ir_constint(ctx, 0xFFFFFFFF));
+
+        case MOJOSHADER_AST_OP_NEGATE:  // !!! FIXME: -0.0f != +0.0f
+            return NEW_IR_BINOP(SUBTRACT, build_ir_increxpr(ctx, ast->unary.datatype, -1),
+                                build_ir_expr(ctx, ast->unary.operand));
+
+        case MOJOSHADER_AST_OP_NOT:  // operand must be bool here!
+            assert(ast->unary.operand->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+            return NEW_IR_BINOP(XOR, build_ir_expr(ctx, ast->unary.operand),
+                                new_ir_constint(ctx, 1));
+
+        case MOJOSHADER_AST_OP_DEREF_ARRAY:
+            return build_ir_derefarray(ctx, &ast->binary);
+
+        case MOJOSHADER_AST_OP_DEREF_STRUCT:
+            if (ast->derefstruct.isswizzle)
+                return build_ir_swizzle(ctx, &ast->derefstruct);
+            return build_ir_derefstruct(ctx, &ast->derefstruct);
+
+        case MOJOSHADER_AST_OP_COMMA:
+            // evaluate and throw away left, return right.
+            return new_ir_eseq(ctx, new_ir_expr_stmt(ctx, build_ir_expr(ctx, ast->binary.left)),
+                               build_ir_expr(ctx, ast->binary.right));
+
+        case MOJOSHADER_AST_OP_LESSTHAN: return EASY_IR_COMPARE(LT);
+        case MOJOSHADER_AST_OP_GREATERTHAN: return EASY_IR_COMPARE(GT);
+        case MOJOSHADER_AST_OP_LESSTHANOREQUAL: return EASY_IR_COMPARE(LEQ);
+        case MOJOSHADER_AST_OP_GREATERTHANOREQUAL: return EASY_IR_COMPARE(GEQ);
+        case MOJOSHADER_AST_OP_NOTEQUAL: return EASY_IR_COMPARE(NEQ);
+        case MOJOSHADER_AST_OP_EQUAL: return EASY_IR_COMPARE(EQL);
+
+        case MOJOSHADER_AST_OP_MULTIPLY: return EASY_IR_BINOP(MULTIPLY);
+        case MOJOSHADER_AST_OP_DIVIDE: return EASY_IR_BINOP(DIVIDE);
+        case MOJOSHADER_AST_OP_MODULO: return EASY_IR_BINOP(MODULO);
+        case MOJOSHADER_AST_OP_ADD: return EASY_IR_BINOP(ADD);
+        case MOJOSHADER_AST_OP_SUBTRACT: return EASY_IR_BINOP(SUBTRACT);
+        case MOJOSHADER_AST_OP_LSHIFT: return EASY_IR_BINOP(LSHIFT);
+        case MOJOSHADER_AST_OP_RSHIFT: return EASY_IR_BINOP(RSHIFT);
+        case MOJOSHADER_AST_OP_BINARYAND: return EASY_IR_BINOP(AND);
+        case MOJOSHADER_AST_OP_BINARYXOR: return EASY_IR_BINOP(XOR);
+        case MOJOSHADER_AST_OP_BINARYOR: return EASY_IR_BINOP(OR);
+
+        case MOJOSHADER_AST_OP_LOGICALAND:
+            return build_ir_logical_and(ctx, &ast->binary);
+
+        case MOJOSHADER_AST_OP_LOGICALOR:
+            return build_ir_logical_or(ctx, &ast->binary);
+
+        case MOJOSHADER_AST_OP_ASSIGN:
+            return build_ir_assign(ctx, &ast->binary);
+
+        case MOJOSHADER_AST_OP_MULASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_MULTIPLY, &ast->binary);
+        case MOJOSHADER_AST_OP_DIVASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_DIVIDE, &ast->binary);
+        case MOJOSHADER_AST_OP_MODASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_MODULO, &ast->binary);
+        case MOJOSHADER_AST_OP_ADDASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_ADD, &ast->binary);
+        case MOJOSHADER_AST_OP_SUBASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_SUBTRACT, &ast->binary);
+        case MOJOSHADER_AST_OP_LSHIFTASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_LSHIFT, &ast->binary);
+        case MOJOSHADER_AST_OP_RSHIFTASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_RSHIFT, &ast->binary);
+        case MOJOSHADER_AST_OP_ANDASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_AND, &ast->binary);
+        case MOJOSHADER_AST_OP_XORASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_XOR, &ast->binary);
+        case MOJOSHADER_AST_OP_ORASSIGN: return build_ir_assign_binop(ctx, MOJOSHADER_IR_BINOP_OR, &ast->binary);
+
+        case MOJOSHADER_AST_OP_CONDITIONAL:
+            assert(ast->binary.left->datatype->type == MOJOSHADER_AST_DATATYPE_BOOL);
+            return build_ir_compare(ctx, MOJOSHADER_IR_COND_EQL,
+                                  build_ir_expr(ctx, ast->ternary.left),
+                                  new_ir_constbool(ctx, 1),
+                                  build_ir_expr(ctx, ast->ternary.center),
+                                  build_ir_expr(ctx, ast->ternary.right));
+
+        case MOJOSHADER_AST_OP_IDENTIFIER:
+            return build_ir_identifier(ctx, &ast->identifier);
+
+        case MOJOSHADER_AST_OP_INT_LITERAL:
+            return new_ir_constint(ctx, ast->intliteral.value);
+
+        case MOJOSHADER_AST_OP_FLOAT_LITERAL:
+            return new_ir_constfloat(ctx, ast->floatliteral.value);
+
+        case MOJOSHADER_AST_OP_BOOLEAN_LITERAL:
+            return new_ir_constbool(ctx, ast->boolliteral.value);
+
+        case MOJOSHADER_AST_OP_CALLFUNC:
+            return build_ir_call(ctx, &ast->callfunc);
+
+        case MOJOSHADER_AST_OP_CONSTRUCTOR:
+            return build_ir_constructor(ctx, &ast->constructor);
+
+        case MOJOSHADER_AST_OP_CAST:
+            return build_ir_convert(ctx, &ast->cast);
+
+        case MOJOSHADER_AST_STATEMENT_BREAK:
+        {
+            const LoopLabels *labels = ctx->ir_loop;
+            assert(labels != NULL);  // semantic analysis should catch this.
+            return new_ir_jump(ctx, labels->end);
+        } // case
+
+        case MOJOSHADER_AST_STATEMENT_CONTINUE:
+        {
+            const LoopLabels *labels = ctx->ir_loop;
+            assert(labels != NULL);  // semantic analysis should catch this.
+            return new_ir_jump(ctx, labels->start);
+        } // case
+
+        case MOJOSHADER_AST_STATEMENT_DISCARD:
+            return new_ir_seq(ctx, new_ir_discard(ctx), build_ir_stmt(ctx, ast->discardstmt.next));
+
+        case MOJOSHADER_AST_STATEMENT_EMPTY:
+            return build_ir(ctx, ast->stmt.next);  // skip it, do next thing.
+
+        case MOJOSHADER_AST_STATEMENT_EXPRESSION:
+            return new_ir_seq(ctx, new_ir_expr_stmt(ctx, build_ir_expr(ctx, ast->exprstmt.expr)), build_ir_stmt(ctx, ast->exprstmt.next));
+
+        case MOJOSHADER_AST_STATEMENT_IF:
+            return build_ir_ifstmt(ctx, &ast->ifstmt);
+
+        case MOJOSHADER_AST_STATEMENT_TYPEDEF:  // ignore this, move on.
+            return build_ir(ctx, ast->typedefstmt.next);
+
+        case MOJOSHADER_AST_STATEMENT_SWITCH:
+            return build_ir_switch(ctx, &ast->switchstmt);
+
+        case MOJOSHADER_AST_STATEMENT_STRUCT:  // ignore this, move on.
+            return build_ir(ctx, ast->structstmt.next);
+
+        case MOJOSHADER_AST_STATEMENT_VARDECL: // ignore this, move on.
+            return build_ir(ctx, ast->vardeclstmt.next);
+
+        case MOJOSHADER_AST_STATEMENT_BLOCK:
+            return new_ir_seq(ctx, build_ir_stmt(ctx, ast->blockstmt.statements), build_ir_stmt(ctx, ast->blockstmt.next));
+
+        case MOJOSHADER_AST_STATEMENT_FOR:
+            return build_ir_forstmt(ctx, &ast->forstmt);
+
+        case MOJOSHADER_AST_STATEMENT_DO:
+            return build_ir_dostmt(ctx, &ast->dostmt);
+
+        case MOJOSHADER_AST_STATEMENT_WHILE:
+            return build_ir_whilestmt(ctx, &ast->whilestmt);
+
+        case MOJOSHADER_AST_STATEMENT_RETURN:
+        {
+            const int label = ctx->ir_end;
+            assert(label >= 0);  // parser should have caught this!
+            MOJOSHADER_irStatement *retval = NULL;
+            if (ast->returnstmt.expr != NULL)
+            {
+                // !!! FIXME: whole array/struct returns need to move more into the temp.
+                const MOJOSHADER_astDataType *dt = reduce_datatype(ctx, ast->returnstmt.expr->datatype);
+                const MOJOSHADER_astDataTypeType type = datatype_base(ctx, dt)->type;
+                const int elems = datatype_elems(ctx, dt);
+                assert(ctx->ir_ret >= 0);
+                retval = new_ir_move(ctx, new_ir_temp(ctx, ctx->ir_ret, type, elems), build_ir_expr(ctx, ast->returnstmt.expr), -1);
+            } // if
+            return new_ir_seq(ctx, retval, new_ir_jump(ctx, label));
+        } // case
+
+        case MOJOSHADER_AST_COMPUNIT_TYPEDEF:
+        case MOJOSHADER_AST_COMPUNIT_STRUCT:
+        case MOJOSHADER_AST_COMPUNIT_VARIABLE:
+        case MOJOSHADER_AST_COMPUNIT_FUNCTION:
+        case MOJOSHADER_AST_ARGUMENTS:
+        case MOJOSHADER_AST_OP_STRING_LITERAL:
+        case MOJOSHADER_AST_SWITCH_CASE:
+        case MOJOSHADER_AST_SCALAR_OR_ARRAY:
+        case MOJOSHADER_AST_TYPEDEF:
+        case MOJOSHADER_AST_FUNCTION_PARAMS:
+        case MOJOSHADER_AST_FUNCTION_SIGNATURE:
+        case MOJOSHADER_AST_STRUCT_DECLARATION:
+        case MOJOSHADER_AST_STRUCT_MEMBER:
+        case MOJOSHADER_AST_VARIABLE_DECLARATION:
+        case MOJOSHADER_AST_ANNOTATION:
+        case MOJOSHADER_AST_PACK_OFFSET:
+        case MOJOSHADER_AST_VARIABLE_LOWLEVEL:
+            assert(0 && "Shouldn't hit this in build_ir.");
+            return NULL;
+
+        default:
+            assert(0 && "unexpected type");
+            return NULL;
+    } // switch
+} // build_ir
+
+static void print_ir(FILE *io, unsigned int depth, void *_ir)
+{
+    MOJOSHADER_irNode *ir = (MOJOSHADER_irNode *) _ir;
+    if (ir == NULL)
+        return;
+
+    const char *fname = strrchr(ir->ir.filename, '/');
+    if (fname != NULL)
+        fname++;
+    else
+    {
+        fname = strrchr(ir->ir.filename, '\\');
+        if (fname != NULL)
+            fname++;
+        else
+            fname = ir->ir.filename;
+    } // else
+
+    int i;
+    for (i = 0; i < depth; i++)
+        fprintf(io, "  ");
+    depth++;
+
+    fprintf(io, "[ %s:%d ", fname, ir->ir.line);
+
+    switch (ir->ir.type)
+    {
+        case MOJOSHADER_IR_LABEL:
+            fprintf(io, "LABEL %d ]\n", ir->stmt.label.index);
+            break;
+
+        case MOJOSHADER_IR_CONSTANT:
+            fprintf(io, "CONSTANT ");
+            switch (ir->expr.constant.info.type)
+            {
+                case MOJOSHADER_AST_DATATYPE_BOOL:
+                case MOJOSHADER_AST_DATATYPE_INT:
+                case MOJOSHADER_AST_DATATYPE_UINT:
+                    for (i = 0; i < ir->expr.constant.info.elements-1; i++)
+                        fprintf(io, "%d, ", ir->expr.constant.value.ival[i]);
+                    if (ir->expr.constant.info.elements > 0)
+                        fprintf(io, "%d", ir->expr.constant.value.ival[i]);
+                    break;
+
+                case MOJOSHADER_AST_DATATYPE_FLOAT:
+                case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
+                case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
+                case MOJOSHADER_AST_DATATYPE_HALF:
+                case MOJOSHADER_AST_DATATYPE_DOUBLE:
+                    for (i = 0; i < ir->expr.constant.info.elements-1; i++)
+                        fprintf(io, "%ff, ", ir->expr.constant.value.fval[i]);
+                    if (ir->expr.constant.info.elements > 0)
+                        fprintf(io, "%ff", ir->expr.constant.value.fval[i]);
+                    break;
+
+                default: assert(0 && "shouldn't happen");
+            } // switch
+            fprintf(io, " ]\n");
+            break;
+
+        case MOJOSHADER_IR_TEMP:
+            fprintf(io, "TEMP %d ]\n", ir->expr.temp.index);
+            break;
+
+        case MOJOSHADER_IR_DISCARD:
+            fprintf(io, "DISCARD ]\n");
+            break;
+
+        case MOJOSHADER_IR_SWIZZLE:
+            fprintf(io, "SWIZZLE");
+            for (i = 0; i < ir->expr.swizzle.info.elements-1; i++)
+                fprintf(io, " %d", (int) ir->expr.swizzle.channels[i]);
+            fprintf(io, " ]\n");
+            print_ir(io, depth, ir->expr.swizzle.expr);
+            break;
+
+        case MOJOSHADER_IR_CONSTRUCT:
+            fprintf(io, "CONSTRUCT ]\n");
+            print_ir(io, depth, ir->expr.construct.args);
+            break;
+
+        case MOJOSHADER_IR_CONVERT:
+            fprintf(io, "CONVERT ]\n");
+            print_ir(io, depth, ir->expr.convert.expr);
+            break;
+
+        case MOJOSHADER_IR_BINOP:
+            fprintf(io, "BINOP ");
+            switch (ir->expr.binop.op)
+            {
+                #define PRINT_IR_BINOP(x) \
+                    case MOJOSHADER_IR_BINOP_##x: fprintf(io, #x); break;
+                PRINT_IR_BINOP(ADD)
+                PRINT_IR_BINOP(SUBTRACT)
+                PRINT_IR_BINOP(MULTIPLY)
+                PRINT_IR_BINOP(DIVIDE)
+                PRINT_IR_BINOP(MODULO)
+                PRINT_IR_BINOP(AND)
+                PRINT_IR_BINOP(OR)
+                PRINT_IR_BINOP(XOR)
+                PRINT_IR_BINOP(LSHIFT)
+                PRINT_IR_BINOP(RSHIFT)
+                PRINT_IR_BINOP(UNKNOWN)
+                #undef PRINT_IR_BINOP
+                default: assert(0 && "unexpected case"); break;
+            } // switch
+            fprintf(io, " ]\n");
+            print_ir(io, depth, ir->expr.binop.left);
+            print_ir(io, depth, ir->expr.binop.right);
+            break;
+
+        case MOJOSHADER_IR_MEMORY:
+            fprintf(io, "MEMORY %d ]\n", ir->expr.memory.index);
+            break;
+
+        case MOJOSHADER_IR_CALL:
+            fprintf(io, "CALL %d ]\n", ir->expr.call.index);
+            print_ir(io, depth, ir->expr.call.args);
+            break;
+
+        case MOJOSHADER_IR_ESEQ:
+            fprintf(io, "ESEQ ]\n");
+            print_ir(io, depth, ir->expr.eseq.stmt);
+            break;
+
+        case MOJOSHADER_IR_ARRAY:
+            fprintf(io, "ARRAY ]\n");
+            print_ir(io, depth, ir->expr.array.array);
+            print_ir(io, depth, ir->expr.array.element);
+            break;
+
+        case MOJOSHADER_IR_MOVE:
+            fprintf(io, "MOVE ]\n");
+            print_ir(io, depth, ir->stmt.move.dst);
+            print_ir(io, depth, ir->stmt.move.src);
+            break;
+
+        case MOJOSHADER_IR_EXPR_STMT:
+            fprintf(io, "EXPRSTMT ]\n");
+            print_ir(io, depth, ir->stmt.expr.expr);
+            break;
+
+        case MOJOSHADER_IR_JUMP:
+            fprintf(io, "JUMP %d ]\n", ir->stmt.jump.label);
+            break;
+
+        case MOJOSHADER_IR_CJUMP:
+            fprintf(io, "CJUMP ");
+            switch (ir->stmt.cjump.cond)
+            {
+                #define PRINT_IR_COND(x) \
+                    case MOJOSHADER_IR_COND_##x: fprintf(io, #x); break;
+                PRINT_IR_COND(EQL)
+                PRINT_IR_COND(NEQ)
+                PRINT_IR_COND(LT)
+                PRINT_IR_COND(GT)
+                PRINT_IR_COND(LEQ)
+                PRINT_IR_COND(GEQ)
+                PRINT_IR_COND(UNKNOWN)
+                #undef PRINT_IR_COND
+                default: assert(0 && "unexpected case"); break;
+            } // switch
+            fprintf(io, " %d %d ]\n", ir->stmt.cjump.iftrue, ir->stmt.cjump.iffalse);
+            print_ir(io, depth, ir->stmt.cjump.left);
+            print_ir(io, depth, ir->stmt.cjump.right);
+            break;
+
+        case MOJOSHADER_IR_SEQ:
+            fprintf(io, "SEQ ]\n");
+            print_ir(io, depth, ir->stmt.seq.first);
+            print_ir(io, depth, ir->stmt.seq.next);  // !!! FIXME: don't recurse?
+            break;
+
+        case MOJOSHADER_IR_EXPRLIST:
+            fprintf(io, "EXPRLIST ]\n");
+            print_ir(io, depth, ir->misc.exprlist.expr);
+            print_ir(io, depth, ir->misc.exprlist.next);  // !!! FIXME: don't recurse?
+            break;
+
+        default: assert(0 && "unexpected IR node"); break;
+    } // switch
+} // print_ir
+
+static void print_whole_ir(Context *ctx, FILE *io)
+{
+    if (ctx->ir != NULL)
+    {
+        int i;
+        for (i = 0; i <= ctx->user_func_index; i++)
+        {
+            printf("[FUNCTION %d ]\n", i);
+            print_ir(io, 1, ctx->ir[i]);
+        } // for
+    } // if
+} // print_whole_ir
+
+static void delete_ir(Context *ctx, void *_ir)
+{
+    MOJOSHADER_irNode *ir = (MOJOSHADER_irNode *) _ir;
+    if (ir == NULL)
+        return;
+
+    switch (ir->ir.type)
+    {
+        case MOJOSHADER_IR_JUMP:
+        case MOJOSHADER_IR_LABEL:
+        case MOJOSHADER_IR_CONSTANT:
+        case MOJOSHADER_IR_TEMP:
+        case MOJOSHADER_IR_DISCARD:
+        case MOJOSHADER_IR_MEMORY:
+            break;  // nothing extra to free here.
+
+        case MOJOSHADER_IR_BINOP:
+            delete_ir(ctx, ir->expr.binop.left);
+            delete_ir(ctx, ir->expr.binop.right);
+            break;
+
+        case MOJOSHADER_IR_CALL:
+            delete_ir(ctx, ir->expr.call.args);
+            break;
+
+        case MOJOSHADER_IR_ESEQ:
+            delete_ir(ctx, ir->expr.eseq.stmt);
+            delete_ir(ctx, ir->expr.eseq.expr);
+            break;
+
+        case MOJOSHADER_IR_ARRAY:
+            delete_ir(ctx, ir->expr.array.array);
+            delete_ir(ctx, ir->expr.array.element);
+            break;
+
+        case MOJOSHADER_IR_MOVE:
+            delete_ir(ctx, ir->stmt.move.dst);
+            delete_ir(ctx, ir->stmt.move.src);
+            break;
+
+        case MOJOSHADER_IR_EXPR_STMT:
+            delete_ir(ctx, ir->stmt.expr.expr);
+            break;
+
+        case MOJOSHADER_IR_CJUMP:
+            delete_ir(ctx, ir->stmt.cjump.left);
+            delete_ir(ctx, ir->stmt.cjump.right);
+            break;
+
+        case MOJOSHADER_IR_SEQ:
+            delete_ir(ctx, ir->stmt.seq.first);
+            delete_ir(ctx, ir->stmt.seq.next);  // !!! FIXME: don't recurse?
+            break;
+
+        case MOJOSHADER_IR_EXPRLIST:
+            delete_ir(ctx, ir->misc.exprlist.expr);
+            delete_ir(ctx, ir->misc.exprlist.next);  // !!! FIXME: don't recurse?
+            break;
+
+        case MOJOSHADER_IR_SWIZZLE:
+            delete_ir(ctx, ir->expr.swizzle.expr);
+            break;
+
+        case MOJOSHADER_IR_CONSTRUCT:
+            delete_ir(ctx, ir->expr.construct.args);
+            break;
+
+        case MOJOSHADER_IR_CONVERT:
+            delete_ir(ctx, ir->expr.convert.expr);
+            break;
+
+        default: assert(0 && "unexpected IR node"); break;
+    } // switch
+
+    Free(ctx, ir);
+} // delete_ir
+
+static void intermediate_representation(Context *ctx)
+{
+    const MOJOSHADER_astCompilationUnit *ast = NULL;
+    const MOJOSHADER_astCompilationUnitFunction *astfn = NULL;
+    const size_t arraylen = (ctx->user_func_index+1) * sizeof (MOJOSHADER_irStatement *);
+
+    ctx->ir = (MOJOSHADER_irStatement **)Malloc(ctx, arraylen);
+    if (ctx->ir == NULL)
+        return;
+    memset(ctx->ir, '\0', arraylen);
+
+    ctx->ir_end = -1;
+    ctx->ir_ret = -1;
+
+    for (ast = &ctx->ast->compunit; ast != NULL; ast = ast->next)
+    {
+        assert(ast->ast.type > MOJOSHADER_AST_COMPUNIT_START_RANGE);
+        assert(ast->ast.type < MOJOSHADER_AST_COMPUNIT_END_RANGE);
+        if (ast->ast.type != MOJOSHADER_AST_COMPUNIT_FUNCTION)
+            continue;  // only care about functions right now.
+
+        astfn = (MOJOSHADER_astCompilationUnitFunction *) ast;
+        if (astfn->definition == NULL)  // just a predeclare; skip.
+            continue;
+
+        assert(ctx->ir_loop == NULL);  // parser should have caught this!
+        assert(ctx->ir_end < 0);  // parser should have caught this!
+        assert(ctx->ir_ret < 0);  // parser should have caught this!
+        const int start = generate_ir_label(ctx);  // !!! FIXME: store somewhere.
+        const int end = generate_ir_label(ctx);
+        ctx->ir_end = end;
+
+        if (astfn->declaration->datatype != NULL)
+            ctx->ir_ret = generate_ir_temp(ctx);
+
+        MOJOSHADER_irStatement *funcseq = new_ir_seq(ctx, new_ir_label(ctx, start), build_ir_stmt(ctx, astfn->definition));
+        funcseq = new_ir_seq(ctx, funcseq, new_ir_label(ctx, end));
+        assert(ctx->ir_loop == NULL);  // parser should have caught this!
+        ctx->ir_end = -1;
+        ctx->ir_ret = -1;
+
+        assert(astfn->index <= ctx->user_func_index);
+        assert(ctx->ir[astfn->index] == NULL);
+        ctx->ir[astfn->index] = funcseq;
+    } // for
+
+    print_whole_ir(ctx, stdout);
+
+    // done with the AST, nuke it.
+    // !!! FIXME: we're going to need CTAB data from this at some point.
+    delete_compilation_unit(ctx, (MOJOSHADER_astCompilationUnit *) ctx->ast);
+    ctx->ast = NULL;
+} // intermediate_representation
+
+
+
+static MOJOSHADER_astData MOJOSHADER_out_of_mem_ast_data = {
+    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0
+};
+
+
+// !!! FIXME: cut and paste from assembler.
+static const MOJOSHADER_astData *build_failed_ast(Context *ctx)
+{
+    assert(isfail(ctx));
+
+    if (ctx->out_of_memory)
+        return &MOJOSHADER_out_of_mem_ast_data;
+        
+    MOJOSHADER_astData *retval = NULL;
+    retval = (MOJOSHADER_astData *) Malloc(ctx, sizeof (MOJOSHADER_astData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_ast_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_astData));
+    retval->source_profile = ctx->source_profile;
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+    retval->error_count = errorlist_count(ctx->errors);
+    retval->errors = errorlist_flatten(ctx->errors);
+
+    if (ctx->out_of_memory)
+    {
+        Free(ctx, retval);
+        return &MOJOSHADER_out_of_mem_ast_data;
+    } // if
+
+    return retval;
+} // build_failed_ast
+
+
+static const MOJOSHADER_astData *build_astdata(Context *ctx)
+{
+    MOJOSHADER_astData *retval = NULL;
+
+    if (ctx->out_of_memory)
+        return &MOJOSHADER_out_of_mem_ast_data;
+
+    retval = (MOJOSHADER_astData *) Malloc(ctx, sizeof (MOJOSHADER_astData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_ast_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_astData));
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+
+    if (!isfail(ctx))
+    {
+        retval->source_profile = ctx->source_profile;
+        retval->ast = ctx->ast;
+    } // if
+
+    retval->error_count = errorlist_count(ctx->errors);
+    retval->errors = errorlist_flatten(ctx->errors);
+    if (ctx->out_of_memory)
+    {
+        Free(ctx, retval);
+        return &MOJOSHADER_out_of_mem_ast_data;
+    } // if
+
+    retval->opaque = ctx;
+
+    return retval;
+} // build_astdata
+
+
+static void choose_src_profile(Context *ctx, const char *srcprofile)
+{
+    ctx->source_profile = srcprofile;
+
+    #define TEST_PROFILE(x) if (strcmp(srcprofile, x) == 0) { return; }
+
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_VS_1_1);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_VS_2_0);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_VS_3_0);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_2);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_3);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_4);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_2_0);
+    TEST_PROFILE(MOJOSHADER_SRC_PROFILE_HLSL_PS_3_0);
+
+    #undef TEST_PROFILE
+
+    fail(ctx, "Unknown profile");
+} // choose_src_profile
+
+
+static MOJOSHADER_compileData MOJOSHADER_out_of_mem_compile_data = {
+    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+
+// !!! FIXME: cut and paste from assembler.
+static const MOJOSHADER_compileData *build_failed_compile(Context *ctx)
+{
+    assert(isfail(ctx));
+
+    MOJOSHADER_compileData *retval = NULL;
+    retval = (MOJOSHADER_compileData *) Malloc(ctx, sizeof (MOJOSHADER_compileData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_compile_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_compileData));
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+    retval->source_profile = ctx->source_profile;
+    retval->error_count = errorlist_count(ctx->errors);
+    retval->errors = errorlist_flatten(ctx->errors);
+    retval->warning_count = errorlist_count(ctx->warnings);
+    retval->warnings = errorlist_flatten(ctx->warnings);
+
+    if (ctx->out_of_memory)  // in case something failed up there.
+    {
+        MOJOSHADER_freeCompileData(retval);
+        return &MOJOSHADER_out_of_mem_compile_data;
+    } // if
+
+    return retval;
+} // build_failed_compile
+
+
+static const MOJOSHADER_compileData *build_compiledata(Context *ctx)
+{
+    assert(!isfail(ctx));
+
+    MOJOSHADER_compileData *retval = NULL;
+
+    retval = (MOJOSHADER_compileData *) Malloc(ctx, sizeof (MOJOSHADER_compileData));
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_compile_data;
+
+    memset(retval, '\0', sizeof (MOJOSHADER_compileData));
+    retval->malloc = (ctx->malloc == MOJOSHADER_internal_malloc) ? NULL : ctx->malloc;
+    retval->free = (ctx->free == MOJOSHADER_internal_free) ? NULL : ctx->free;
+    retval->malloc_data = ctx->malloc_data;
+    retval->source_profile = ctx->source_profile;
+
+    if (!isfail(ctx))
+    {
+        // !!! FIXME: build output and output_len here.
+    } // if
+
+    if (!isfail(ctx))
+    {
+        // !!! FIXME: build symbols and symbol_count here.
+    } // if
+
+    retval->error_count = errorlist_count(ctx->errors);
+    retval->errors = errorlist_flatten(ctx->errors);
+    retval->warning_count = errorlist_count(ctx->warnings);
+    retval->warnings = errorlist_flatten(ctx->warnings);
+
+    if (ctx->out_of_memory)  // in case something failed up there.
+    {
+        MOJOSHADER_freeCompileData(retval);
+        return &MOJOSHADER_out_of_mem_compile_data;
+    } // if
+
+    return retval;
+} // build_compiledata
+
+
+// API entry point...
+
+// !!! FIXME: move this (and a lot of other things) to mojoshader_ast.c.
+const MOJOSHADER_astData *MOJOSHADER_parseAst(const char *srcprofile,
+                                    const char *filename, const char *source,
+                                    unsigned int sourcelen,
+                                    const MOJOSHADER_preprocessorDefine *defs,
+                                    unsigned int define_count,
+                                    MOJOSHADER_includeOpen include_open,
+                                    MOJOSHADER_includeClose include_close,
+                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                    void *d)
+{
+    const MOJOSHADER_astData *retval = NULL;
+    Context *ctx = NULL;
+
+    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
+        return &MOJOSHADER_out_of_mem_ast_data;  // supply both or neither.
+
+    ctx = build_context(m, f, d);
+    if (ctx == NULL)
+        return &MOJOSHADER_out_of_mem_ast_data;
+
+    choose_src_profile(ctx, srcprofile);
+
+    if (!isfail(ctx))
+    {
+        parse_source(ctx, filename, source, sourcelen, defs, define_count,
+                     include_open, include_close);
+    } // if
+
+    if (!isfail(ctx))
+        retval = build_astdata(ctx);  // ctx isn't destroyed yet!
+    else
+    {
+        retval = (MOJOSHADER_astData *) build_failed_ast(ctx);
+        destroy_context(ctx);
+    } // else
+
+    return retval;
+} // MOJOSHADER_parseAst
+
+
+void MOJOSHADER_freeAstData(const MOJOSHADER_astData *_data)
+{
+    MOJOSHADER_astData *data = (MOJOSHADER_astData *) _data;
+    if ((data == NULL) || (data == &MOJOSHADER_out_of_mem_ast_data))
+        return;  // no-op.
+
+    // !!! FIXME: this needs to live for deleting the stringcache and the ast.
+    Context *ctx = (Context *) data->opaque;
+    MOJOSHADER_free f = (data->free == NULL) ? MOJOSHADER_internal_free : data->free;
+    void *d = data->malloc_data;
+    int i;
+
+    // we don't f(data->source_profile), because that's internal static data.
+
+    for (i = 0; i < data->error_count; i++)
+    {
+        f((void *) data->errors[i].error, d);
+        f((void *) data->errors[i].filename, d);
+    } // for
+    f((void *) data->errors, d);
+
+    // don't delete data->ast (it'll delete with the context).
+    f(data, d);
+
+    destroy_context(ctx);  // finally safe to destroy this.
+} // MOJOSHADER_freeAstData
+
+
+const MOJOSHADER_compileData *MOJOSHADER_compile(const char *srcprofile,
+                                    const char *filename, const char *source,
+                                    unsigned int sourcelen,
+                                    const MOJOSHADER_preprocessorDefine *defs,
+                                    unsigned int define_count,
+                                    MOJOSHADER_includeOpen include_open,
+                                    MOJOSHADER_includeClose include_close,
+                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                    void *d)
+{
+    // !!! FIXME: cut and paste from MOJOSHADER_parseAst().
+    MOJOSHADER_compileData *retval = NULL;
+    Context *ctx = NULL;
+
+    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
+        return &MOJOSHADER_out_of_mem_compile_data;  // supply both or neither.
+
+    ctx = build_context(m, f, d);
+    if (ctx == NULL)
+        return &MOJOSHADER_out_of_mem_compile_data;
+
+    choose_src_profile(ctx, srcprofile);
+
+    if (!isfail(ctx))
+    {
+        parse_source(ctx, filename, source, sourcelen, defs, define_count,
+                     include_open, include_close);
+    } // if
+
+    if (!isfail(ctx))
+        semantic_analysis(ctx);
+
+    if (!isfail(ctx))
+        intermediate_representation(ctx);
+
+    if (isfail(ctx))
+        retval = (MOJOSHADER_compileData *) build_failed_compile(ctx);
+    else
+        retval = (MOJOSHADER_compileData *) build_compiledata(ctx);
+
+    destroy_context(ctx);
+    return retval;
+} // MOJOSHADER_compile
+
+
+void MOJOSHADER_freeCompileData(const MOJOSHADER_compileData *_data)
+{
+    MOJOSHADER_compileData *data = (MOJOSHADER_compileData *) _data;
+    if ((data == NULL) || (data == &MOJOSHADER_out_of_mem_compile_data))
+        return;  // no-op.
+
+    MOJOSHADER_free f = (data->free == NULL) ? MOJOSHADER_internal_free : data->free;
+    void *d = data->malloc_data;
+    int i;
+
+    // we don't f(data->source_profile), because that's internal static data.
+
+    for (i = 0; i < data->error_count; i++)
+    {
+        f((void *) data->errors[i].error, d);
+        f((void *) data->errors[i].filename, d);
+    } // for
+    f((void *) data->errors, d);
+
+    for (i = 0; i < data->warning_count; i++)
+    {
+        f((void *) data->warnings[i].error, d);
+        f((void *) data->warnings[i].filename, d);
+    } // for
+    f((void *) data->warnings, d);
+
+    for (i = 0; i < data->symbol_count; i++)
+    {
+        f((void *) data->symbols[i].name, d);
+        // !!! FIXME: this is missing stuff (including freeing substructs).
+    } // for
+    f((void *) data->symbols, d);
+
+    f((void *) data->output, d);
+    f(data, d);
+} // MOJOSHADER_freeCompileData
+
+// end of mojoshader_compiler.c ...
+

+ 625 - 0
ThirdParty/MojoShader/mojoshader_effects.c

@@ -0,0 +1,625 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+#include <math.h>
+
+#if SUPPORT_PRESHADERS
+void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
+                             const float *inregs, float *outregs)
+{
+    // this is fairly straightforward, as there aren't any branching
+    //  opcodes in the preshader instruction set (at the moment, at least).
+    const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS;
+
+    double *temps = NULL;
+    if (preshader->temp_count > 0)
+    {
+        temps = (double *) alloca(sizeof (double) * preshader->temp_count);
+        memset(temps, '\0', sizeof (double) * preshader->temp_count);
+    } // if
+
+    double dst[4] = { 0, 0, 0, 0 };
+    double src[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
+    const double *src0 = &src[0][0];
+    const double *src1 = &src[1][0];
+    const double *src2 = &src[2][0];
+
+    MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
+    int instit;
+
+    for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
+    {
+        const MOJOSHADER_preshaderOperand *operand = inst->operands;
+        const int elems = inst->element_count;
+        const int elemsbytes = sizeof (double) * elems;
+        const int isscalarop = (inst->opcode >= scalarstart);
+
+        assert(elems >= 0);
+        assert(elems <= 4);
+
+        // load up our operands...
+        int opiter, elemiter;
+        for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
+        {
+            const int isscalar = ((isscalarop) && (opiter == 0));
+            const unsigned int index = operand->index;
+            switch (operand->type)
+            {
+                case MOJOSHADER_PRESHADEROPERAND_LITERAL:
+                {
+                    const double *lit = &preshader->literals[index];
+                    assert((index + elems) <= preshader->literal_count);
+                    if (!isscalar)
+                        memcpy(&src[opiter][0], lit, elemsbytes);
+                    else
+                    {
+                        const double val = *lit;
+                        for (elemiter = 0; elemiter < elems; elemiter++)
+                            src[opiter][elemiter] = val;
+                    } // else
+                    break;
+                } // case
+
+                case MOJOSHADER_PRESHADEROPERAND_INPUT:
+                    if (isscalar)
+                        src[opiter][0] = inregs[index];
+                    else
+                    {
+                        int cpy;
+                        for (cpy = 0; cpy < elems; cpy++)
+                            src[opiter][cpy] = inregs[index+cpy];
+                    } // else
+                    break;
+
+                case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
+                    if (isscalar)
+                        src[opiter][0] = outregs[index];
+                    else
+                    {
+                        int cpy;
+                        for (cpy = 0; cpy < elems; cpy++)
+                            src[opiter][cpy] = outregs[index+cpy];
+                    } // else
+                    break;
+
+                case MOJOSHADER_PRESHADEROPERAND_TEMP:
+                    if (temps != NULL)
+                    {
+                        if (isscalar)
+                            src[opiter][0] = temps[index];
+                        else
+                            memcpy(src[opiter], temps + index, elemsbytes);
+                    } // if
+                    break;
+
+                default:
+                    assert(0 && "unexpected preshader operand type.");
+                    return;
+            } // switch
+        } // for
+
+        // run the actual instruction, store result to dst.
+        int i;
+        switch (inst->opcode)
+        {
+            #define OPCODE_CASE(op, val) \
+                case MOJOSHADER_PRESHADEROP_##op: \
+                    for (i = 0; i < elems; i++) { dst[i] = val; } \
+                    break;
+
+            //OPCODE_CASE(NOP, 0.0)  // not a real instruction.
+            OPCODE_CASE(MOV, src0[i])
+            OPCODE_CASE(NEG, -src0[i])
+            OPCODE_CASE(RCP, 1.0 / src0[i])
+            OPCODE_CASE(FRC, src0[i] - floor(src0[i]))
+            OPCODE_CASE(EXP, exp(src0[i]))
+            OPCODE_CASE(LOG, log(src0[i]))
+            OPCODE_CASE(RSQ, 1.0 / sqrt(src0[i]))
+            OPCODE_CASE(SIN, sin(src0[i]))
+            OPCODE_CASE(COS, cos(src0[i]))
+            OPCODE_CASE(ASIN, asin(src0[i]))
+            OPCODE_CASE(ACOS, acos(src0[i]))
+            OPCODE_CASE(ATAN, atan(src0[i]))
+            OPCODE_CASE(MIN, (src0[i] < src1[i]) ? src0[i] : src1[i])
+            OPCODE_CASE(MAX, (src0[i] > src1[i]) ? src0[i] : src1[i])
+            OPCODE_CASE(LT, (src0[i] < src1[i]) ? 1.0 : 0.0)
+            OPCODE_CASE(GE, (src0[i] >= src1[i]) ? 1.0 : 0.0)
+            OPCODE_CASE(ADD, src0[i] + src1[i])
+            OPCODE_CASE(MUL,  src0[i] * src1[i])
+            OPCODE_CASE(ATAN2, atan2(src0[i], src1[i]))
+            OPCODE_CASE(DIV, src0[i] / src1[i])
+            OPCODE_CASE(CMP, (src0[i] >= 0.0) ? src1[i] : src2[i])
+            //OPCODE_CASE(NOISE, ???)  // !!! FIXME: don't know what this does
+            //OPCODE_CASE(MOVC, ???)  // !!! FIXME: don't know what this does
+            OPCODE_CASE(MIN_SCALAR, (src0[0] < src1[i]) ? src0[0] : src1[i])
+            OPCODE_CASE(MAX_SCALAR, (src0[0] > src1[i]) ? src0[0] : src1[i])
+            OPCODE_CASE(LT_SCALAR, (src0[0] < src1[i]) ? 1.0 : 0.0)
+            OPCODE_CASE(GE_SCALAR, (src0[0] >= src1[i]) ? 1.0 : 0.0)
+            OPCODE_CASE(ADD_SCALAR, src0[0] + src1[i])
+            OPCODE_CASE(MUL_SCALAR, src0[0] * src1[i])
+            OPCODE_CASE(ATAN2_SCALAR, atan2(src0[0], src1[i]))
+            OPCODE_CASE(DIV_SCALAR, src0[0] / src1[i])
+            //OPCODE_CASE(DOT_SCALAR)  // !!! FIXME: isn't this just a MUL?
+            //OPCODE_CASE(NOISE_SCALAR, ???)  // !!! FIXME: ?
+            #undef OPCODE_CASE
+
+            case MOJOSHADER_PRESHADEROP_DOT:
+            {
+                double final = 0.0;
+                for (i = 0; i < elems; i++)
+                    final += src0[i] * src1[i];
+                for (i = 0; i < elems; i++)
+                    dst[i] = final;  // !!! FIXME: is this right?
+            } // case
+
+            default:
+                assert(0 && "Unhandled preshader opcode!");
+                break;
+        } // switch
+
+        // Figure out where dst wants to be stored.
+        if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
+        {
+            assert(preshader->temp_count >=
+                    operand->index + (elemsbytes / sizeof (double)));
+            memcpy(temps + operand->index, dst, elemsbytes);
+        } // if
+        else
+        {
+            assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
+            for (i = 0; i < elems; i++)
+                outregs[operand->index + i] = (float) dst[i];
+        } // else
+    } // for
+} // MOJOSHADER_runPreshader
+#endif
+
+static MOJOSHADER_effect MOJOSHADER_out_of_mem_effect = {
+    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+static uint32 readui32(const uint8 **_ptr, uint32 *_len)
+{
+    uint32 retval = 0;
+    if (*_len < sizeof (retval))
+        *_len = 0;
+    else
+    {
+        const uint32 *ptr = (const uint32 *) *_ptr;
+        retval = SWAP32(*ptr);
+        *_ptr += sizeof (retval);
+        *_len -= sizeof (retval);
+    } // else
+    return retval;
+} // readui32
+
+// !!! FIXME: this is sort of a big, ugly function.
+const MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
+                                                const unsigned char *buf,
+                                                const unsigned int _len,
+                                                const MOJOSHADER_swizzle *swiz,
+                                                const unsigned int swizcount,
+                                                const MOJOSHADER_samplerMap *smap,
+                                                const unsigned int smapcount,
+                                                MOJOSHADER_malloc m,
+                                                MOJOSHADER_free f,
+                                                void *d)
+{
+    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
+        return &MOJOSHADER_out_of_mem_effect;  // supply both or neither.
+
+    if (m == NULL) m = MOJOSHADER_internal_malloc;
+    if (f == NULL) f = MOJOSHADER_internal_free;
+
+    MOJOSHADER_effect *retval = (MOJOSHADER_effect *)m(sizeof (MOJOSHADER_effect), d);
+    if (retval == NULL)
+        return &MOJOSHADER_out_of_mem_effect;  // supply both or neither.
+    memset(retval, '\0', sizeof (*retval));
+
+    retval->malloc = m;
+    retval->free = f;
+    retval->malloc_data = d;
+
+    const uint8 *ptr = (const uint8 *) buf;
+    uint32 len = (uint32) _len;
+    size_t siz = 0;
+    int i, j, k;
+
+    if (len < 8)
+        goto parseEffect_unexpectedEOF;
+
+    const uint8 *base = NULL;
+    if (readui32(&ptr, &len) != 0xFEFF0901) // !!! FIXME: is this always magic?
+        goto parseEffect_notAnEffectsFile;
+    else
+    {
+        const uint32 offset = readui32(&ptr, &len);
+        base = ptr;
+//printf("base offset == %u\n", offset);
+        if (offset > len)
+            goto parseEffect_unexpectedEOF;
+        ptr += offset;
+        len -= offset;
+    } // else
+
+    // params...
+
+    if (len < 16)
+        goto parseEffect_unexpectedEOF;
+
+    const uint32 numparams = readui32(&ptr, &len);
+    const uint32 numtechniques = readui32(&ptr, &len);
+
+    readui32(&ptr, &len); // !!! FIXME: there are 8 unknown bytes here. Annotations?
+    /*const uint32 numobjects = */ readui32(&ptr, &len);
+
+    if (numparams > 0)
+    {
+        siz = sizeof (MOJOSHADER_effectParam) * numparams;
+        retval->params = (MOJOSHADER_effectParam *) m(siz, d);
+        if (retval->params == NULL)
+            goto parseEffect_outOfMemory;
+        memset(retval->params, '\0', siz);
+
+        retval->param_count = numparams;
+
+        for (i = 0; i < numparams; i++)
+        {
+            if (len < 16)
+                goto parseEffect_unexpectedEOF;
+
+            const uint32 typeoffset = readui32(&ptr, &len);
+            /*const uint32 valoffset =*/ readui32(&ptr, &len);
+            /*const uint32 flags =*/ readui32(&ptr, &len);
+            const uint32 numannos = readui32(&ptr, &len);
+            for (j = 0; j < numannos; j++)
+            {
+                if (len < 8)
+                    goto parseEffect_unexpectedEOF;
+                // !!! FIXME: parse annotations.
+                readui32(&ptr, &len);
+                readui32(&ptr, &len);
+            } // for
+
+            const uint8 *typeptr = base + typeoffset;
+            unsigned int typelen = 9999999;  // !!! FIXME
+            /*const uint32 paramtype =*/ readui32(&typeptr, &typelen);
+            /*const uint32 paramclass =*/ readui32(&typeptr, &typelen);
+            const uint32 paramname = readui32(&typeptr, &typelen);
+            const uint32 paramsemantic = readui32(&typeptr, &typelen);
+
+            // !!! FIXME: sanity checks!
+            const char *namestr = ((const char *) base) + paramname;
+            const char *semstr = ((const char *) base) + paramsemantic;
+            uint32 len;
+            char *strptr;
+            len = *((const uint32 *) namestr);
+            strptr = (char *) m(len + 1, d);
+            memcpy(strptr, namestr + 4, len);
+            strptr[len] = '\0';
+            retval->params[i].name = strptr;
+            len = *((const uint32 *) semstr);
+            strptr = (char *) m(len + 1, d);
+            memcpy(strptr, semstr + 4, len);
+            strptr[len] = '\0';
+            retval->params[i].semantic = strptr;
+        } // for
+    } // if
+
+    uint32 numshaders = 0;  // we'll calculate this later.
+
+    // techniques...
+
+    if (numtechniques > 0)
+    {
+        siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
+        retval->techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
+        if (retval->techniques == NULL)
+            goto parseEffect_outOfMemory;
+        memset(retval->techniques, '\0', siz);
+
+        retval->technique_count = numtechniques;
+
+        for (i = 0; i < numtechniques; i++)
+        {
+            if (len < 12)
+                goto parseEffect_unexpectedEOF;
+            
+            MOJOSHADER_effectTechnique *technique = &retval->techniques[i];
+
+            const uint32 nameoffset = readui32(&ptr, &len);
+            const uint32 numannos = readui32(&ptr, &len);
+            const uint32 numpasses = readui32(&ptr, &len);
+
+            if (nameoffset >= _len)
+                goto parseEffect_unexpectedEOF;
+
+            if (numannos > 0)
+            {
+                // !!! FIXME: expose these to the caller?
+                for (j = 0; j < numannos; j++)
+                {
+                    if (len < 8)
+                        goto parseEffect_unexpectedEOF;
+                    readui32(&ptr, &len);  // typedef offset
+                    readui32(&ptr, &len);  // value offset
+                } // for
+            } // if
+
+            // !!! FIXME: verify this doesn't go past EOF looking for a null.
+            {
+                const char *namestr = ((char *) base) + nameoffset;
+                uint32 len = *((const uint32 *) namestr);
+                char *strptr = (char *) m(len + 1, d);
+                memcpy(strptr, namestr + 4, len);
+                strptr[len] = '\0';
+                technique->name = strptr;
+            }
+
+            if (numpasses > 0)
+            {
+                technique->pass_count = numpasses;
+
+                siz = sizeof (MOJOSHADER_effectPass) * numpasses;
+                technique->passes = (MOJOSHADER_effectPass *) m(siz, d);
+                if (technique->passes == NULL)
+                    goto parseEffect_outOfMemory;
+                memset(technique->passes, '\0', siz);
+
+                for (j = 0; j < numpasses; j++)
+                {
+                    if (len < 12)
+                        goto parseEffect_unexpectedEOF;
+
+                    MOJOSHADER_effectPass *pass = &technique->passes[j];
+
+                    const uint32 passnameoffset = readui32(&ptr, &len);
+                    const uint32 numannos = readui32(&ptr, &len);
+                    const uint32 numstates = readui32(&ptr, &len);
+
+                    if (passnameoffset >= _len)
+                        goto parseEffect_unexpectedEOF;
+
+                    // !!! FIXME: verify this doesn't go past EOF looking for a null.
+                    {
+                        const char *namestr = ((char *) base) + passnameoffset;
+                        uint32 len = *((const uint32 *) namestr);
+                        char *strptr = (char *) m(len + 1, d);
+                        memcpy(strptr, namestr + 4, len);
+                        strptr[len] = '\0';
+                        pass->name = strptr;
+                    }
+
+                    if (numannos > 0)
+                    {
+                        for (k = 0; k < numannos; k++)
+                        {
+                            if (len < 8)
+                                goto parseEffect_unexpectedEOF;
+                            // !!! FIXME: do something with this.
+                            readui32(&ptr, &len);
+                            readui32(&ptr, &len);
+                        } // for
+                    } // if
+
+                    if (numstates > 0)
+                    {
+                        pass->state_count = numstates;
+
+                        siz = sizeof (MOJOSHADER_effectState) * numstates;
+                        pass->states = (MOJOSHADER_effectState *) m(siz, d);
+                        if (pass->states == NULL)
+                            goto parseEffect_outOfMemory;
+                        memset(pass->states, '\0', siz);
+
+                        for (k = 0; k < numstates; k++)
+                        {
+                            if (len < 16)
+                                goto parseEffect_unexpectedEOF;
+
+                            MOJOSHADER_effectState *state = &pass->states[k];
+                            const uint32 type = readui32(&ptr, &len);
+                            readui32(&ptr, &len);  // !!! FIXME: don't know what this field does.
+                            /*const uint32 offsetend = */ readui32(&ptr, &len);
+                            /*const uint32 offsetstart = */ readui32(&ptr, &len);
+                            state->type = type;
+
+                            if ((type == 0x92) || (type == 0x93))
+                                numshaders++;
+                        } // for
+                    } // if
+                } // for
+            } // if
+        } // for
+    } // if
+
+    // textures...
+
+    if (len < 8)
+        goto parseEffect_unexpectedEOF;
+
+    const int numtextures = readui32(&ptr, &len);
+    const int numobjects = readui32(&ptr, &len);  // !!! FIXME: "objects" for lack of a better word.
+
+    if (numtextures > 0)
+    {
+        siz = sizeof (MOJOSHADER_effectTexture) * numtextures;
+        retval->textures = (MOJOSHADER_effectTexture *)m(siz, d);
+        if (retval->textures == NULL)
+            goto parseEffect_outOfMemory;
+        memset(retval->textures, '\0', siz);
+
+        for (i = 0; i < numtextures; i++)
+        {
+            if (len < 8)
+                goto parseEffect_unexpectedEOF;
+
+            MOJOSHADER_effectTexture *texture = &retval->textures[i];
+            const uint32 texparam = readui32(&ptr, &len);
+            const uint32 texsize = readui32(&ptr, &len);
+            // apparently texsize will pad out to 32 bits.
+            const uint32 readsize = (((texsize + 3) / 4) * 4);
+            if (len < readsize)
+                goto parseEffect_unexpectedEOF;
+
+            texture->param = texparam;
+            char *str = (char *)m(texsize + 1, d);
+            if (str == NULL)
+                goto parseEffect_outOfMemory;
+            memcpy(str, ptr, texsize);
+            str[texsize] = '\0';
+            texture->name = str;
+
+            ptr += readsize;
+            len -= readsize;
+        } // for
+    } // if
+
+    // shaders...
+
+    if (numshaders > 0)
+    {
+        siz = sizeof (MOJOSHADER_effectShader) * numshaders;
+        retval->shaders = (MOJOSHADER_effectShader *) m(siz, d);
+        if (retval->shaders == NULL)
+            goto parseEffect_outOfMemory;
+        memset(retval->shaders, '\0', siz);
+
+        retval->shader_count = numshaders;
+
+        // !!! FIXME: I wonder if we should pull these from offsets and not
+        // !!! FIXME:  count on them all being in a line like this.
+        for (i = 0; i < numshaders; i++)
+        {
+            if (len < 24)
+                goto parseEffect_unexpectedEOF;
+
+            MOJOSHADER_effectShader *shader = &retval->shaders[i];
+            const uint32 technique = readui32(&ptr, &len);
+            const uint32 pass = readui32(&ptr, &len);
+            readui32(&ptr, &len);  // !!! FIXME: don't know what this does.
+            readui32(&ptr, &len);  // !!! FIXME: don't know what this does (vertex/pixel/geometry?)
+            readui32(&ptr, &len);  // !!! FIXME: don't know what this does.
+            const uint32 shadersize = readui32(&ptr, &len);
+
+            if (len < shadersize)
+                goto parseEffect_unexpectedEOF;
+
+            shader->technique = technique;
+            shader->pass = pass;
+            shader->shader = MOJOSHADER_parse(profile, ptr, shadersize,
+                                              swiz, swizcount, smap, smapcount,
+                                              m, f, d);
+
+            // !!! FIXME: check for errors.
+
+            ptr += shadersize;
+            len -= shadersize;
+        } // for
+    } // if
+
+    // !!! FIXME: we parse this, but don't expose the data, yet.
+    // mappings ...
+    assert(numshaders <= numobjects);
+    const uint32 nummappings = numobjects - numshaders;
+    if (nummappings > 0)
+    {
+        for (i = 0; i < nummappings; i++)
+        {
+            if (len < 24)
+                goto parseEffect_unexpectedEOF;
+
+            /*const uint32 magic = */ readui32(&ptr, &len);
+            /*const uint32 index = */ readui32(&ptr, &len);
+            readui32(&ptr, &len);  // !!! FIXME: what is this field?
+            readui32(&ptr, &len);  // !!! FIXME: what is this field?
+            /*const uint32 type = */ readui32(&ptr, &len);
+            const uint32 mapsize = readui32(&ptr, &len);
+            if (mapsize > 0)
+            {
+                const uint32 readsize = (((mapsize + 3) / 4) * 4);
+                if (len < readsize)
+                    goto parseEffect_unexpectedEOF;
+            } // if
+        } // for
+    } // if
+
+    retval->profile = (char *) m(strlen(profile) + 1, d);
+    if (retval->profile == NULL)
+        goto parseEffect_outOfMemory;
+    strcpy((char *) retval->profile, profile);
+
+    return retval;
+
+
+// !!! FIXME: do something with this.
+parseEffect_notAnEffectsFile:
+parseEffect_unexpectedEOF:
+parseEffect_outOfMemory:
+    MOJOSHADER_freeEffect(retval);
+    return &MOJOSHADER_out_of_mem_effect;
+} // MOJOSHADER_parseEffect
+
+
+void MOJOSHADER_freeEffect(const MOJOSHADER_effect *_effect)
+{
+    MOJOSHADER_effect *effect = (MOJOSHADER_effect *) _effect;
+    if ((effect == NULL) || (effect == &MOJOSHADER_out_of_mem_effect))
+        return;  // no-op.
+
+    MOJOSHADER_free f = effect->free;
+    void *d = effect->malloc_data;
+    int i, j;
+
+    for (i = 0; i < effect->error_count; i++)
+    {
+        f((void *) effect->errors[i].error, d);
+        f((void *) effect->errors[i].filename, d);
+    } // for
+    f((void *) effect->errors, d);
+
+    f((void *) effect->profile, d);
+
+    for (i = 0; i < effect->param_count; i++)
+    {
+        f((void *) effect->params[i].name, d);
+        f((void *) effect->params[i].semantic, d);
+    } // for
+    f(effect->params, d);
+
+    for (i = 0; i < effect->technique_count; i++)
+    {
+        MOJOSHADER_effectTechnique *technique = &effect->techniques[i];
+        f((void *) technique->name, d);
+        for (j = 0; j < technique->pass_count; j++)
+        {
+            f((void *) technique->passes[j].name, d);
+            f(technique->passes[j].states, d);
+        } // for
+        f(technique->passes, d);
+    } // for
+
+    f(effect->techniques, d);
+
+    for (i = 0; i < effect->texture_count; i++)
+        f((void *) effect->textures[i].name, d);
+    f(effect->textures, d);
+
+    for (i = 0; i < effect->shader_count; i++)
+        MOJOSHADER_freeParseData(effect->shaders[i].shader);
+    f(effect->shaders, d);
+
+    f(effect, d);
+} // MOJOSHADER_freeEffect
+
+// end of mojoshader_effects.c ...
+

+ 691 - 0
ThirdParty/MojoShader/mojoshader_internal.h

@@ -0,0 +1,691 @@
+#ifndef _INCLUDE_MOJOSHADER_INTERNAL_H_
+#define _INCLUDE_MOJOSHADER_INTERNAL_H_
+
+#ifndef __MOJOSHADER_INTERNAL__
+#error Do not include this header from your applications.
+#endif
+
+// Shader bytecode format is described at MSDN:
+//  http://msdn.microsoft.com/en-us/library/ff569705.aspx
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <assert.h>
+
+#include "mojoshader.h"
+
+#define DEBUG_LEXER 0
+#define DEBUG_PREPROCESSOR 0
+#define DEBUG_ASSEMBLER_PARSER 0
+#define DEBUG_COMPILER_PARSER 0
+#define DEBUG_TOKENIZER \
+    (DEBUG_PREPROCESSOR || DEBUG_ASSEMBLER_PARSER || DEBUG_LEXER)
+
+#if (defined(__APPLE__) && defined(__MACH__))
+#define PLATFORM_MACOSX 1
+#endif
+
+// This is the highest shader version we currently support.
+
+#define MAX_SHADER_MAJOR 3
+#define MAX_SHADER_MINOR 255  // vs_3_sw
+
+
+// If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
+//  You get all the profiles unless you go out of your way to disable them.
+
+#ifndef SUPPORT_PROFILE_D3D
+#define SUPPORT_PROFILE_D3D 1
+#endif
+
+#ifndef SUPPORT_PROFILE_BYTECODE
+#define SUPPORT_PROFILE_BYTECODE 1
+#endif
+
+#ifndef SUPPORT_PROFILE_GLSL
+#define SUPPORT_PROFILE_GLSL 1
+#endif
+
+#ifndef SUPPORT_PROFILE_GLSL120
+#define SUPPORT_PROFILE_GLSL120 1
+#endif
+
+#ifndef SUPPORT_PROFILE_ARB1
+#define SUPPORT_PROFILE_ARB1 1
+#endif
+
+#ifndef SUPPORT_PROFILE_ARB1_NV
+#define SUPPORT_PROFILE_ARB1_NV 1
+#endif
+
+#if SUPPORT_PROFILE_ARB1_NV && !SUPPORT_PROFILE_ARB1
+#error nv profiles require arb1 profile. Fix your build.
+#endif
+
+#if SUPPORT_PROFILE_GLSL120 && !SUPPORT_PROFILE_GLSL
+#error glsl120 profile requires glsl profile. Fix your build.
+#endif
+
+
+// Other stuff you can disable...
+
+// This removes the preshader parsing and execution code. You can save some
+//  bytes if you have normal shaders and not Effect files.
+#ifndef SUPPORT_PRESHADERS
+#define SUPPORT_PRESHADERS 1
+#endif
+
+#if SUPPORT_PRESHADERS
+void MOJOSHADER_runPreshader(const MOJOSHADER_preshader*, const float*, float*);
+#else
+#define MOJOSHADER_runPreshader(a, b)
+#endif
+
+
+// Get basic wankery out of the way here...
+
+#ifdef _WINDOWS
+#define ENDLINE_STR "\r\n"
+#else
+#define ENDLINE_STR "\n"
+#endif
+
+typedef unsigned int uint;  // this is a printf() helper. don't use for code.
+
+#ifdef _MSC_VER
+#include <malloc.h>
+#define va_copy(a, b) a = b
+#define snprintf _snprintf  // !!! FIXME: not a safe replacement!
+#define vsnprintf _vsnprintf  // !!! FIXME: not a safe replacement!
+#define strcasecmp stricmp
+#define strncasecmp strnicmp
+typedef unsigned __int8 uint8;
+typedef unsigned __int16 uint16;
+typedef unsigned __int32 uint32;
+typedef unsigned __int64 uint64;
+typedef __int32 int32;
+typedef __int64 int64;
+#ifdef _WIN64
+typedef __int64 ssize_t;
+#elif defined _WIN32
+typedef __int32 ssize_t;
+#else
+#error Please define your platform.
+#endif
+// Warning Level 4 considered harmful.  :)
+#pragma warning(disable: 4100)  // "unreferenced formal parameter"
+#pragma warning(disable: 4389)  // "signed/unsigned mismatch"
+#else
+#include <stdint.h>
+typedef uint8_t uint8;
+typedef uint16_t uint16;
+typedef uint32_t uint32;
+typedef int32_t int32;
+typedef int64_t int64;
+typedef uint64_t uint64;
+#endif
+
+#ifdef sun
+#include <alloca.h>
+#endif
+
+#ifdef __GNUC__
+#define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
+#else
+#define ISPRINTF(x,y)
+#endif
+
+#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
+
+
+// Byteswap magic...
+
+#if ((defined __GNUC__) && (defined __POWERPC__))
+    static inline uint32 SWAP32(uint32 x)
+    {
+        __asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
+        return x;
+    } // SWAP32
+    static inline uint16 SWAP16(uint16 x)
+    {
+        __asm__ __volatile__("lhbrx %0,0,%1" : "=r" (x) : "r" (&x));
+        return x;
+    } // SWAP16
+#elif defined(__POWERPC__)
+    static inline uint32 SWAP32(uint32 x)
+    {
+        return ( (((x) >> 24) & 0x000000FF) | (((x) >>  8) & 0x0000FF00) |
+                 (((x) <<  8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
+    } // SWAP32
+    static inline uint16 SWAP16(uint16 x)
+    {
+        return ( (((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00) );
+    } // SWAP16
+#else
+#   define SWAP16(x) (x)
+#   define SWAP32(x) (x)
+#endif
+
+#define SWAPDBL(x) (x)  // !!! FIXME
+
+static inline int Min(const int a, const int b)
+{
+    return ((a < b) ? a : b);
+} // Min
+
+
+// Hashtables...
+
+typedef struct HashTable HashTable;
+typedef uint32 (*HashTable_HashFn)(const void *key, void *data);
+typedef int (*HashTable_KeyMatchFn)(const void *a, const void *b, void *data);
+typedef void (*HashTable_NukeFn)(const void *key, const void *value, void *data);
+
+HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
+                       const HashTable_KeyMatchFn keymatchfn,
+                       const HashTable_NukeFn nukefn,
+                       const int stackable,
+                       MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+void hash_destroy(HashTable *table);
+int hash_insert(HashTable *table, const void *key, const void *value);
+int hash_remove(HashTable *table, const void *key);
+int hash_find(const HashTable *table, const void *key, const void **_value);
+int hash_iter(const HashTable *table, const void *key, const void **_value, void **iter);
+int hash_iter_keys(const HashTable *table, const void **_key, void **iter);
+
+uint32 hash_hash_string(const void *sym, void *unused);
+int hash_keymatch_string(const void *a, const void *b, void *unused);
+
+
+// String -> String map ...
+typedef HashTable StringMap;
+StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
+                            MOJOSHADER_free f, void *d);
+void stringmap_destroy(StringMap *smap);
+int stringmap_insert(StringMap *smap, const char *key, const char *value);
+int stringmap_remove(StringMap *smap, const char *key);
+int stringmap_find(const StringMap *smap, const char *key, const char **_val);
+
+
+// String caching...
+
+typedef struct StringCache StringCache;
+StringCache *stringcache_create(MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
+const char *stringcache(StringCache *cache, const char *str);
+const char *stringcache_len(StringCache *cache, const char *str,
+                            const unsigned int len);
+const char *stringcache_fmt(StringCache *cache, const char *fmt, ...);
+void stringcache_destroy(StringCache *cache);
+
+
+// Error lists...
+
+typedef struct ErrorList ErrorList;
+ErrorList *errorlist_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+int errorlist_add(ErrorList *list, const char *fname,
+                      const int errpos, const char *str);
+int errorlist_add_fmt(ErrorList *list, const char *fname,
+                      const int errpos, const char *fmt, ...) ISPRINTF(4,5);
+int errorlist_add_va(ErrorList *list, const char *_fname,
+                     const int errpos, const char *fmt, va_list va);
+int errorlist_count(ErrorList *list);
+MOJOSHADER_error *errorlist_flatten(ErrorList *list); // resets the list!
+void errorlist_destroy(ErrorList *list);
+
+
+
+// Dynamic buffers...
+
+typedef struct Buffer Buffer;
+Buffer *buffer_create(size_t blksz,MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
+char *buffer_reserve(Buffer *buffer, const size_t len);
+int buffer_append(Buffer *buffer, const void *_data, size_t len);
+int buffer_append_fmt(Buffer *buffer, const char *fmt, ...) ISPRINTF(2,3);
+int buffer_append_va(Buffer *buffer, const char *fmt, va_list va);
+size_t buffer_size(Buffer *buffer);
+void buffer_empty(Buffer *buffer);
+char *buffer_flatten(Buffer *buffer);
+char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len);
+void buffer_destroy(Buffer *buffer);
+ssize_t buffer_find(Buffer *buffer, const size_t start,
+                    const void *data, const size_t len);
+
+
+
+// This is the ID for a D3DXSHADER_CONSTANTTABLE in the bytecode comments.
+#define CTAB_ID 0x42415443  // 0x42415443 == 'CTAB'
+#define CTAB_SIZE 28  // sizeof (D3DXSHADER_CONSTANTTABLE).
+#define CINFO_SIZE 20  // sizeof (D3DXSHADER_CONSTANTINFO).
+#define CTYPEINFO_SIZE 16  // sizeof (D3DXSHADER_TYPEINFO).
+#define CMEMBERINFO_SIZE 8  // sizeof (D3DXSHADER_STRUCTMEMBERINFO)
+
+// Preshader magic values...
+#define PRES_ID 0x53455250  // 0x53455250 == 'PRES'
+#define PRSI_ID 0x49535250  // 0x49535250 == 'PRSI'
+#define CLIT_ID 0x54494C43  // 0x54494C43 == 'CLIT'
+#define FXLC_ID 0x434C5846  // 0x434C5846 == 'FXLC'
+
+// we need to reference these by explicit value occasionally...
+#define OPCODE_RET 28
+#define OPCODE_IF 40
+#define OPCODE_IFC 41
+#define OPCODE_BREAK 44
+#define OPCODE_BREAKC 45
+#define OPCODE_TEXLD 66
+#define OPCODE_SETP 94
+
+// TEXLD becomes a different instruction with these instruction controls.
+#define CONTROL_TEXLD  0
+#define CONTROL_TEXLDP 1
+#define CONTROL_TEXLDB 2
+
+// #define this to force app to supply an allocator, so there's no reference
+//  to the C runtime's malloc() and free()...
+#if MOJOSHADER_FORCE_ALLOCATOR
+#define MOJOSHADER_internal_malloc NULL
+#define MOJOSHADER_internal_free NULL
+#else
+void *MOJOSHADER_internal_malloc(int bytes, void *d);
+void MOJOSHADER_internal_free(void *ptr, void *d);
+#endif
+
+#if MOJOSHADER_FORCE_INCLUDE_CALLBACKS
+#define MOJOSHADER_internal_include_open NULL
+#define MOJOSHADER_internal_include_close NULL
+#else
+int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype,
+                                     const char *fname, const char *parent,
+                                     const char **outdata,
+                                     unsigned int *outbytes,
+                                     MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                     void *d);
+
+void MOJOSHADER_internal_include_close(const char *data, MOJOSHADER_malloc m,
+                                       MOJOSHADER_free f, void *d);
+#endif
+
+
+// result modifiers.
+// !!! FIXME: why isn't this an enum?
+#define MOD_SATURATE 0x01
+#define MOD_PP 0x02
+#define MOD_CENTROID 0x04
+
+typedef enum
+{
+    REG_TYPE_TEMP = 0,
+    REG_TYPE_INPUT = 1,
+    REG_TYPE_CONST = 2,
+    REG_TYPE_ADDRESS = 3,
+    REG_TYPE_TEXTURE = 3,  // ALSO 3!
+    REG_TYPE_RASTOUT = 4,
+    REG_TYPE_ATTROUT = 5,
+    REG_TYPE_TEXCRDOUT = 6,
+    REG_TYPE_OUTPUT = 6,  // ALSO 6!
+    REG_TYPE_CONSTINT = 7,
+    REG_TYPE_COLOROUT = 8,
+    REG_TYPE_DEPTHOUT = 9,
+    REG_TYPE_SAMPLER = 10,
+    REG_TYPE_CONST2 = 11,
+    REG_TYPE_CONST3 = 12,
+    REG_TYPE_CONST4 = 13,
+    REG_TYPE_CONSTBOOL = 14,
+    REG_TYPE_LOOP = 15,
+    REG_TYPE_TEMPFLOAT16 = 16,
+    REG_TYPE_MISCTYPE = 17,
+    REG_TYPE_LABEL = 18,
+    REG_TYPE_PREDICATE = 19,
+    REG_TYPE_MAX = 19
+} RegisterType;
+
+typedef enum
+{
+    TEXTURE_TYPE_2D = 2,
+    TEXTURE_TYPE_CUBE = 3,
+    TEXTURE_TYPE_VOLUME = 4,
+} TextureType;
+
+typedef enum
+{
+    RASTOUT_TYPE_POSITION = 0,
+    RASTOUT_TYPE_FOG = 1,
+    RASTOUT_TYPE_POINT_SIZE = 2,
+    RASTOUT_TYPE_MAX = 2
+} RastOutType;
+
+typedef enum
+{
+    MISCTYPE_TYPE_POSITION = 0,
+    MISCTYPE_TYPE_FACE = 1,
+    MISCTYPE_TYPE_MAX = 1
+} MiscTypeType;
+
+// source modifiers.
+typedef enum
+{
+    SRCMOD_NONE,
+    SRCMOD_NEGATE,
+    SRCMOD_BIAS,
+    SRCMOD_BIASNEGATE,
+    SRCMOD_SIGN,
+    SRCMOD_SIGNNEGATE,
+    SRCMOD_COMPLEMENT,
+    SRCMOD_X2,
+    SRCMOD_X2NEGATE,
+    SRCMOD_DZ,
+    SRCMOD_DW,
+    SRCMOD_ABS,
+    SRCMOD_ABSNEGATE,
+    SRCMOD_NOT,
+    SRCMOD_TOTAL
+} SourceMod;
+
+
+typedef struct
+{
+    const uint32 *token;   // this is the unmolested token in the stream.
+    int regnum;
+    int relative;
+    int writemask;   // xyzw or rgba (all four, not split out).
+    int writemask0;  // x or red
+    int writemask1;  // y or green
+    int writemask2;  // z or blue
+    int writemask3;  // w or alpha
+    int orig_writemask;   // writemask before mojoshader tweaks it.
+    int result_mod;
+    int result_shift;
+    RegisterType regtype;
+} DestArgInfo;
+
+// NOTE: This will NOT know a dcl_psize or dcl_fog output register should be
+//        scalar! This function doesn't have access to that information.
+static inline int scalar_register(const MOJOSHADER_shaderType shader_type,
+                                  const RegisterType regtype, const int regnum)
+{
+    switch (regtype)
+    {
+        case REG_TYPE_RASTOUT:
+            if (((const RastOutType) regnum) == RASTOUT_TYPE_FOG)
+                return 1;
+            else if (((const RastOutType) regnum) == RASTOUT_TYPE_POINT_SIZE)
+                return 1;
+            return 0;
+
+        case REG_TYPE_DEPTHOUT:
+        case REG_TYPE_CONSTBOOL:
+        case REG_TYPE_LOOP:
+            return 1;
+
+        case REG_TYPE_MISCTYPE:
+            if ( ((const MiscTypeType) regnum) == MISCTYPE_TYPE_FACE )
+                return 1;
+            return 0;
+
+        case REG_TYPE_PREDICATE:
+            return (shader_type == MOJOSHADER_TYPE_PIXEL) ? 1 : 0;
+
+        default: break;
+    } // switch
+
+    return 0;
+} // scalar_register
+
+
+extern MOJOSHADER_error MOJOSHADER_out_of_mem_error;
+extern MOJOSHADER_parseData MOJOSHADER_out_of_mem_data;
+
+
+// preprocessor stuff.
+
+typedef enum
+{
+    TOKEN_UNKNOWN = 256,  // start past ASCII character values.
+
+    // These are all C-like constructs. Tokens < 256 may be single
+    //  chars (like '+' or whatever). These are just multi-char sequences
+    //  (like "+=" or whatever).
+    TOKEN_IDENTIFIER,
+    TOKEN_INT_LITERAL,
+    TOKEN_FLOAT_LITERAL,
+    TOKEN_STRING_LITERAL,
+    TOKEN_RSHIFTASSIGN,
+    TOKEN_LSHIFTASSIGN,
+    TOKEN_ADDASSIGN,
+    TOKEN_SUBASSIGN,
+    TOKEN_MULTASSIGN,
+    TOKEN_DIVASSIGN,
+    TOKEN_MODASSIGN,
+    TOKEN_XORASSIGN,
+    TOKEN_ANDASSIGN,
+    TOKEN_ORASSIGN,
+    TOKEN_INCREMENT,
+    TOKEN_DECREMENT,
+    TOKEN_RSHIFT,
+    TOKEN_LSHIFT,
+    TOKEN_ANDAND,
+    TOKEN_OROR,
+    TOKEN_LEQ,
+    TOKEN_GEQ,
+    TOKEN_EQL,
+    TOKEN_NEQ,
+    TOKEN_HASH,
+    TOKEN_HASHHASH,
+
+    // This is returned at the end of input...no more to process.
+    TOKEN_EOI,
+
+    // This is returned for char sequences we think are bogus. You'll have
+    //  to judge for yourself. In most cases, you'll probably just fail with
+    //  bogus syntax without explicitly checking for this token.
+    TOKEN_BAD_CHARS,
+
+    // This is returned if there's an error condition (the error is returned
+    //  as a NULL-terminated string from preprocessor_nexttoken(), instead
+    //  of actual token data). You can continue getting tokens after this
+    //  is reported. It happens for things like missing #includes, etc.
+    TOKEN_PREPROCESSING_ERROR,
+
+    // These are all caught by the preprocessor. Caller won't ever see them,
+    //  except TOKEN_PP_PRAGMA.
+    //  They control the preprocessor (#includes new files, etc).
+    TOKEN_PP_INCLUDE,
+    TOKEN_PP_LINE,
+    TOKEN_PP_DEFINE,
+    TOKEN_PP_UNDEF,
+    TOKEN_PP_IF,
+    TOKEN_PP_IFDEF,
+    TOKEN_PP_IFNDEF,
+    TOKEN_PP_ELSE,
+    TOKEN_PP_ELIF,
+    TOKEN_PP_ENDIF,
+    TOKEN_PP_ERROR,  // caught, becomes TOKEN_PREPROCESSING_ERROR
+    TOKEN_PP_PRAGMA,
+    TOKEN_INCOMPLETE_COMMENT,  // caught, becomes TOKEN_PREPROCESSING_ERROR
+    TOKEN_PP_UNARY_MINUS,  // used internally, never returned.
+    TOKEN_PP_UNARY_PLUS,   // used internally, never returned.
+} Token;
+
+
+// This is opaque.
+struct Preprocessor;
+typedef struct Preprocessor Preprocessor;
+
+typedef struct Conditional
+{
+    Token type;
+    int linenum;
+    int skipping;
+    int chosen;
+    struct Conditional *next;
+} Conditional;
+
+typedef struct Define
+{
+    const char *identifier;
+    const char *definition;
+    const char *original;
+    const char **parameters;
+    int paramcount;
+    struct Define *next;
+} Define;
+
+typedef struct IncludeState
+{
+    const char *filename;
+    const char *source_base;
+    const char *source;
+    const char *token;
+    unsigned int tokenlen;
+    Token tokenval;
+    int pushedback;
+    const unsigned char *lexer_marker;
+    int report_whitespace;
+    int asm_comments;
+    unsigned int orig_length;
+    unsigned int bytes_left;
+    unsigned int line;
+    Conditional *conditional_stack;
+    MOJOSHADER_includeClose close_callback;
+    struct IncludeState *next;
+} IncludeState;
+
+Token preprocessor_lexer(IncludeState *s);
+
+// This will only fail if the allocator fails, so it doesn't return any
+//  error code...NULL on failure.
+Preprocessor *preprocessor_start(const char *fname, const char *source,
+                            unsigned int sourcelen,
+                            MOJOSHADER_includeOpen open_callback,
+                            MOJOSHADER_includeClose close_callback,
+                            const MOJOSHADER_preprocessorDefine *defines,
+                            unsigned int define_count, int asm_comments,
+                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
+
+void preprocessor_end(Preprocessor *pp);
+int preprocessor_outofmemory(Preprocessor *pp);
+const char *preprocessor_nexttoken(Preprocessor *_ctx,
+                                   unsigned int *_len, Token *_token);
+const char *preprocessor_sourcepos(Preprocessor *pp, unsigned int *pos);
+
+
+void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
+                                  const unsigned int tokenlen,
+                                  const Token tokenval);
+
+#endif  // _INCLUDE_MOJOSHADER_INTERNAL_H_
+
+
+#if MOJOSHADER_DO_INSTRUCTION_TABLE
+// These have to be in the right order! Arrays are indexed by the value
+//  of the instruction token.
+
+// INSTRUCTION_STATE means this opcode has to update the state machine
+//  (we're entering an ELSE block, etc). INSTRUCTION means there's no
+//  state, just go straight to the emitters.
+
+// !!! FIXME: Some of these MOJOSHADER_TYPE_ANYs need to have their scope
+// !!! FIXME:  reduced to just PIXEL or VERTEX.
+
+INSTRUCTION(NOP, "NOP", 1, NULL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(MOV, "MOV", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(ADD, "ADD", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(SUB, "SUB", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(MAD, "MAD", 1, DSSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(MUL, "MUL", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(RCP, "RCP", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(RSQ, "RSQ", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(DP3, "DP3", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(DP4, "DP4", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(MIN, "MIN", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(MAX, "MAX", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(SLT, "SLT", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(SGE, "SGE", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(EXP, "EXP", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(LOG, "LOG", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(LIT, "LIT", 3, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(DST, "DST", 1, DSS, MOJOSHADER_TYPE_VERTEX)
+INSTRUCTION(LRP, "LRP", 2, DSSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(FRC, "FRC", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(M4X4, "M4X4", 4, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(M4X3, "M4X3", 3, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(M3X4, "M3X4", 4, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(M3X3, "M3X3", 3, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(M3X2, "M3X2", 2, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(CALL, "CALL", 2, S, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(CALLNZ, "CALLNZ", 3, SS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(LOOP, "LOOP", 3, SS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(RET, "RET", 1, NULL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(ENDLOOP, "ENDLOOP", 2, NULL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(LABEL, "LABEL", 0, S, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(DCL, "DCL", 0, DCL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(POW, "POW", 3, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(CRS, "CRS", 2, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(SGN, "SGN", 3, DSSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(ABS, "ABS", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(NRM, "NRM", 3, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(SINCOS, "SINCOS", 8, SINCOS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(REP, "REP", 3, S, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(ENDREP, "ENDREP", 2, NULL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(IF, "IF", 3, S, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(IFC, "IF", 3, SS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(ELSE, "ELSE", 1, NULL, MOJOSHADER_TYPE_ANY)  // !!! FIXME: state!
+INSTRUCTION(ENDIF, "ENDIF", 1, NULL, MOJOSHADER_TYPE_ANY) // !!! FIXME: state!
+INSTRUCTION_STATE(BREAK, "BREAK", 1, NULL, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(BREAKC, "BREAK", 3, SS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(MOVA, "MOVA", 1, DS, MOJOSHADER_TYPE_VERTEX)
+INSTRUCTION_STATE(DEFB, "DEFB", 0, DEFB, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(DEFI, "DEFI", 0, DEFI, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION_STATE(TEXCRD, "TEXCRD", 1, TEXCRD, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXKILL, "TEXKILL", 2, D, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXLD, "TEXLD", 1, TEXLD, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXBEM, "TEXBEM", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXBEML, "TEXBEML", 2, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXREG2AR, "TEXREG2AR", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXREG2GB, "TEXREG2GB", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X2PAD, "TEXM3X2PAD", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X2TEX, "TEXM3X2TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X3PAD, "TEXM3X3PAD", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X3TEX, "TEXM3X3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(RESERVED, 0, 0, NULL, MOJOSHADER_TYPE_UNKNOWN)
+INSTRUCTION_STATE(TEXM3X3SPEC, "TEXM3X3SPEC", 1, DSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X3VSPEC, "TEXM3X3VSPEC", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(EXPP, "EXPP", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(LOGP, "LOGP", 1, DS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(CND, "CND", 1, DSSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(DEF, "DEF", 0, DEF, MOJOSHADER_TYPE_ANY)
+INSTRUCTION(TEXREG2RGB, "TEXREG2RGB", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXDP3TEX, "TEXDP3TEX", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXM3X2DEPTH, "TEXM3X2DEPTH", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXDP3, "TEXDP3", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(TEXM3X3, "TEXM3X3", 1, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXDEPTH, "TEXDEPTH", 1, D, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(CMP, "CMP", 1, DSSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(BEM, "BEM", 2, DSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(DP2ADD, "DP2ADD", 2, DSSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(DSX, "DSX", 2, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(DSY, "DSY", 2, DS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION(TEXLDD, "TEXLDD", 3, DSSSS, MOJOSHADER_TYPE_PIXEL)
+INSTRUCTION_STATE(SETP, "SETP", 1, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(TEXLDL, "TEXLDL", 2, DSS, MOJOSHADER_TYPE_ANY)
+INSTRUCTION_STATE(BREAKP, "BREAKP", 3, S, MOJOSHADER_TYPE_ANY)
+#endif
+
+// end of mojoshader_internal.h ...
+

+ 1504 - 0
ThirdParty/MojoShader/mojoshader_lexer.c

@@ -0,0 +1,1504 @@
+/* Generated by re2c 0.13.5 */
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+// This was originally based on examples/pp-c.re from re2c: http://re2c.org/
+//   re2c is public domain code.
+//
+// You build mojoshader_lexer_preprocessor.c from the .re file with re2c...
+// re2c -is -o mojoshader_lexer_preprocessor.c mojoshader_lexer_preprocessor.re
+//
+// Changes to the lexer are done to the .re file, not the C code!
+//
+// Please note that this isn't a perfect C lexer, since it is used for both
+//  HLSL and shader assembly language, and follows the quirks of Microsoft's
+//  tools.
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+typedef unsigned char uchar;
+
+#define YYMAXFILL 8
+#define RET(t) return update_state(s, eoi, cursor, token, (Token) t)
+#define YYCTYPE uchar
+#define YYCURSOR cursor
+#define YYLIMIT limit
+#define YYMARKER s->lexer_marker
+#define YYFILL(n) { if ((n) == 1) { cursor = sentinel; limit = cursor + YYMAXFILL; eoi = 1; } }
+
+static uchar sentinel[YYMAXFILL];
+
+static Token update_state(IncludeState *s, int eoi, const uchar *cur,
+                          const uchar *tok, const Token val)
+{
+    if (eoi)
+    {
+        s->bytes_left = 0;
+        s->source = (const char *) s->source_base + s->orig_length;
+        if ( (tok >= sentinel) && (tok < (sentinel+YYMAXFILL)) )
+            s->token = s->source;
+        else
+            s->token = (const char *) tok;
+    } // if
+    else
+    {
+        s->bytes_left -= (unsigned int) (cur - ((const uchar *) s->source));
+        s->source = (const char *) cur;
+        s->token = (const char *) tok;
+    } // else
+    s->tokenlen = (unsigned int) (s->source - s->token);
+    s->tokenval = val;
+    return val;
+} // update_state
+
+Token preprocessor_lexer(IncludeState *s)
+{
+    const uchar *cursor = (const uchar *) s->source;
+    const uchar *token = cursor;
+    const uchar *matchptr;
+    const uchar *limit = cursor + s->bytes_left;
+    int eoi = 0;
+    int saw_newline = 0;
+
+
+
+    // preprocessor directives are only valid at start of line.
+    if (s->tokenval == ((Token) '\n'))
+        goto ppdirective;  // may jump back to scanner_loop.
+
+scanner_loop:
+    if (YYLIMIT == YYCURSOR) YYFILL(1);
+    token = cursor;
+
+
+{
+	YYCTYPE yych;
+	unsigned int yyaccept = 0;
+
+	if ((YYLIMIT - YYCURSOR) < 5) YYFILL(5);
+	yych = *YYCURSOR;
+	switch (yych) {
+	case 0x00:	goto yy61;
+	case '\t':
+	case '\v':
+	case '\f':
+	case ' ':	goto yy63;
+	case '\n':	goto yy65;
+	case '\r':	goto yy67;
+	case '!':	goto yy35;
+	case '"':	goto yy14;
+	case '#':	goto yy37;
+	case '%':	goto yy25;
+	case '&':	goto yy29;
+	case '\'':	goto yy11;
+	case '(':	goto yy39;
+	case ')':	goto yy41;
+	case '*':	goto yy23;
+	case '+':	goto yy19;
+	case ',':	goto yy47;
+	case '-':	goto yy21;
+	case '.':	goto yy12;
+	case '/':	goto yy4;
+	case '0':	goto yy8;
+	case '1':
+	case '2':
+	case '3':
+	case '4':
+	case '5':
+	case '6':
+	case '7':
+	case '8':
+	case '9':	goto yy10;
+	case ':':	goto yy51;
+	case ';':	goto yy59;
+	case '<':	goto yy17;
+	case '=':	goto yy33;
+	case '>':	goto yy15;
+	case '?':	goto yy57;
+	case 'A':
+	case 'B':
+	case 'C':
+	case 'D':
+	case 'E':
+	case 'F':
+	case 'G':
+	case 'H':
+	case 'I':
+	case 'J':
+	case 'K':
+	case 'L':
+	case 'M':
+	case 'N':
+	case 'O':
+	case 'P':
+	case 'Q':
+	case 'R':
+	case 'S':
+	case 'T':
+	case 'U':
+	case 'V':
+	case 'W':
+	case 'X':
+	case 'Y':
+	case 'Z':
+	case '_':
+	case 'a':
+	case 'b':
+	case 'c':
+	case 'd':
+	case 'e':
+	case 'f':
+	case 'g':
+	case 'h':
+	case 'i':
+	case 'j':
+	case 'k':
+	case 'l':
+	case 'm':
+	case 'n':
+	case 'o':
+	case 'p':
+	case 'q':
+	case 'r':
+	case 's':
+	case 't':
+	case 'u':
+	case 'v':
+	case 'w':
+	case 'x':
+	case 'y':
+	case 'z':	goto yy6;
+	case '[':	goto yy43;
+	case '\\':	goto yy2;
+	case ']':	goto yy45;
+	case '^':	goto yy27;
+	case '{':	goto yy53;
+	case '|':	goto yy31;
+	case '}':	goto yy55;
+	case '~':	goto yy49;
+	default:	goto yy68;
+	}
+yy2:
+	yyaccept = 0;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych <= 0x08) goto yy3;
+	if (yych <= '\r') goto yy177;
+	if (yych == ' ') goto yy177;
+yy3:
+	{ goto bad_chars; }
+yy4:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) <= '.') {
+		if (yych == '*') goto yy174;
+	} else {
+		if (yych <= '/') goto yy172;
+		if (yych == '=') goto yy170;
+	}
+	{ RET('/'); }
+yy6:
+	++YYCURSOR;
+	yych = *YYCURSOR;
+	goto yy169;
+yy7:
+	{ RET(TOKEN_IDENTIFIER); }
+yy8:
+	yyaccept = 1;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych <= 'X') {
+		if (yych <= 'T') {
+			if (yych == 'L') goto yy144;
+			goto yy160;
+		} else {
+			if (yych <= 'U') goto yy144;
+			if (yych <= 'W') goto yy160;
+			goto yy161;
+		}
+	} else {
+		if (yych <= 't') {
+			if (yych == 'l') goto yy144;
+			goto yy160;
+		} else {
+			if (yych <= 'u') goto yy144;
+			if (yych == 'x') goto yy161;
+			goto yy160;
+		}
+	}
+yy9:
+	{ RET(TOKEN_INT_LITERAL); }
+yy10:
+	yyaccept = 1;
+	yych = *(YYMARKER = ++YYCURSOR);
+	goto yy142;
+yy11:
+	yyaccept = 0;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych == '\n') goto yy3;
+	if (yych == '\r') goto yy3;
+	goto yy132;
+yy12:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) <= '/') goto yy13;
+	if (yych <= '9') goto yy123;
+yy13:
+	{ RET('.'); }
+yy14:
+	yyaccept = 0;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych == '\n') goto yy3;
+	if (yych == '\r') goto yy3;
+	goto yy113;
+yy15:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) <= '<') goto yy16;
+	if (yych <= '=') goto yy106;
+	if (yych <= '>') goto yy108;
+yy16:
+	{ RET('>'); }
+yy17:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) <= ';') goto yy18;
+	if (yych <= '<') goto yy102;
+	if (yych <= '=') goto yy100;
+yy18:
+	{ RET('<'); }
+yy19:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '+') goto yy96;
+	if (yych == '=') goto yy98;
+	{ RET('+'); }
+yy21:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '-') goto yy92;
+	if (yych == '=') goto yy94;
+	{ RET('-'); }
+yy23:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy90;
+	{ RET('*'); }
+yy25:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy88;
+	{ RET('%'); }
+yy27:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy86;
+	{ RET('^'); }
+yy29:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '&') goto yy82;
+	if (yych == '=') goto yy84;
+	{ RET('&'); }
+yy31:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy80;
+	if (yych == '|') goto yy78;
+	{ RET('|'); }
+yy33:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy76;
+	{ RET('='); }
+yy35:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy74;
+	{ RET('!'); }
+yy37:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '#') goto yy72;
+	{ RET(TOKEN_HASH); }
+yy39:
+	++YYCURSOR;
+	{ RET('('); }
+yy41:
+	++YYCURSOR;
+	{ RET(')'); }
+yy43:
+	++YYCURSOR;
+	{ RET('['); }
+yy45:
+	++YYCURSOR;
+	{ RET(']'); }
+yy47:
+	++YYCURSOR;
+	{ RET(','); }
+yy49:
+	++YYCURSOR;
+	{ RET('~'); }
+yy51:
+	++YYCURSOR;
+	{ RET(':'); }
+yy53:
+	++YYCURSOR;
+	{ RET('{'); }
+yy55:
+	++YYCURSOR;
+	{ RET('}'); }
+yy57:
+	++YYCURSOR;
+	{ RET('?'); }
+yy59:
+	++YYCURSOR;
+	{ if (s->asm_comments) goto singlelinecomment; RET(';'); }
+yy61:
+	++YYCURSOR;
+	{ if (eoi) { RET(TOKEN_EOI); } goto bad_chars; }
+yy63:
+	++YYCURSOR;
+	yych = *YYCURSOR;
+	goto yy71;
+yy64:
+	{ if (s->report_whitespace) RET(' '); goto scanner_loop; }
+yy65:
+	++YYCURSOR;
+yy66:
+	{ s->line++; RET('\n'); }
+yy67:
+	yych = *++YYCURSOR;
+	if (yych == '\n') goto yy69;
+	goto yy66;
+yy68:
+	yych = *++YYCURSOR;
+	goto yy3;
+yy69:
+	yych = *++YYCURSOR;
+	goto yy66;
+yy70:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+yy71:
+	if (yych <= '\n') {
+		if (yych == '\t') goto yy70;
+		goto yy64;
+	} else {
+		if (yych <= '\f') goto yy70;
+		if (yych == ' ') goto yy70;
+		goto yy64;
+	}
+yy72:
+	++YYCURSOR;
+	{ RET(TOKEN_HASHHASH); }
+yy74:
+	++YYCURSOR;
+	{ RET(TOKEN_NEQ); }
+yy76:
+	++YYCURSOR;
+	{ RET(TOKEN_EQL); }
+yy78:
+	++YYCURSOR;
+	{ RET(TOKEN_OROR); }
+yy80:
+	++YYCURSOR;
+	{ RET(TOKEN_ORASSIGN); }
+yy82:
+	++YYCURSOR;
+	{ RET(TOKEN_ANDAND); }
+yy84:
+	++YYCURSOR;
+	{ RET(TOKEN_ANDASSIGN); }
+yy86:
+	++YYCURSOR;
+	{ RET(TOKEN_XORASSIGN); }
+yy88:
+	++YYCURSOR;
+	{ RET(TOKEN_MODASSIGN); }
+yy90:
+	++YYCURSOR;
+	{ RET(TOKEN_MULTASSIGN); }
+yy92:
+	++YYCURSOR;
+	{ RET(TOKEN_DECREMENT); }
+yy94:
+	++YYCURSOR;
+	{ RET(TOKEN_SUBASSIGN); }
+yy96:
+	++YYCURSOR;
+	{ RET(TOKEN_INCREMENT); }
+yy98:
+	++YYCURSOR;
+	{ RET(TOKEN_ADDASSIGN); }
+yy100:
+	++YYCURSOR;
+	{ RET(TOKEN_LEQ); }
+yy102:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy104;
+	{ RET(TOKEN_LSHIFT); }
+yy104:
+	++YYCURSOR;
+	{ RET(TOKEN_LSHIFTASSIGN); }
+yy106:
+	++YYCURSOR;
+	{ RET(TOKEN_GEQ); }
+yy108:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '=') goto yy110;
+	{ RET(TOKEN_RSHIFT); }
+yy110:
+	++YYCURSOR;
+	{ RET(TOKEN_RSHIFTASSIGN); }
+yy112:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+yy113:
+	if (yych <= '\r') {
+		if (yych == '\n') goto yy114;
+		if (yych <= '\f') goto yy112;
+	} else {
+		if (yych <= '"') {
+			if (yych <= '!') goto yy112;
+			goto yy116;
+		} else {
+			if (yych == '\\') goto yy115;
+			goto yy112;
+		}
+	}
+yy114:
+	YYCURSOR = YYMARKER;
+	if (yyaccept <= 1) {
+		if (yyaccept <= 0) {
+			goto yy3;
+		} else {
+			goto yy9;
+		}
+	} else {
+		goto yy125;
+	}
+yy115:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'b') {
+		if (yych <= '7') {
+			if (yych <= '&') {
+				if (yych == '"') goto yy112;
+				goto yy114;
+			} else {
+				if (yych <= '\'') goto yy112;
+				if (yych <= '/') goto yy114;
+				goto yy119;
+			}
+		} else {
+			if (yych <= '[') {
+				if (yych == '?') goto yy112;
+				goto yy114;
+			} else {
+				if (yych <= '\\') goto yy112;
+				if (yych <= '`') goto yy114;
+				goto yy112;
+			}
+		}
+	} else {
+		if (yych <= 'r') {
+			if (yych <= 'm') {
+				if (yych == 'f') goto yy112;
+				goto yy114;
+			} else {
+				if (yych <= 'n') goto yy112;
+				if (yych <= 'q') goto yy114;
+				goto yy112;
+			}
+		} else {
+			if (yych <= 'u') {
+				if (yych == 't') goto yy112;
+				goto yy114;
+			} else {
+				if (yych <= 'v') goto yy112;
+				if (yych == 'x') goto yy118;
+				goto yy114;
+			}
+		}
+	}
+yy116:
+	++YYCURSOR;
+	{ RET(TOKEN_STRING_LITERAL); }
+yy118:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '@') {
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy121;
+		goto yy114;
+	} else {
+		if (yych <= 'F') goto yy121;
+		if (yych <= '`') goto yy114;
+		if (yych <= 'f') goto yy121;
+		goto yy114;
+	}
+yy119:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '!') {
+		if (yych <= '\n') {
+			if (yych <= '\t') goto yy112;
+			goto yy114;
+		} else {
+			if (yych == '\r') goto yy114;
+			goto yy112;
+		}
+	} else {
+		if (yych <= '7') {
+			if (yych <= '"') goto yy116;
+			if (yych <= '/') goto yy112;
+			goto yy119;
+		} else {
+			if (yych == '\\') goto yy115;
+			goto yy112;
+		}
+	}
+yy121:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '/') {
+		if (yych <= '\f') {
+			if (yych == '\n') goto yy114;
+			goto yy112;
+		} else {
+			if (yych <= '\r') goto yy114;
+			if (yych == '"') goto yy116;
+			goto yy112;
+		}
+	} else {
+		if (yych <= '[') {
+			if (yych <= '9') goto yy121;
+			if (yych <= '@') goto yy112;
+			if (yych <= 'F') goto yy121;
+			goto yy112;
+		} else {
+			if (yych <= '\\') goto yy115;
+			if (yych <= '`') goto yy112;
+			if (yych <= 'f') goto yy121;
+			goto yy112;
+		}
+	}
+yy123:
+	yyaccept = 2;
+	YYMARKER = ++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
+	yych = *YYCURSOR;
+	if (yych <= 'G') {
+		if (yych <= 'D') {
+			if (yych <= '/') goto yy125;
+			if (yych <= '9') goto yy123;
+		} else {
+			if (yych <= 'E') goto yy126;
+			if (yych <= 'F') goto yy127;
+		}
+	} else {
+		if (yych <= 'e') {
+			if (yych <= 'H') goto yy127;
+			if (yych >= 'e') goto yy126;
+		} else {
+			if (yych == 'g') goto yy125;
+			if (yych <= 'h') goto yy127;
+		}
+	}
+yy125:
+	{ RET(TOKEN_FLOAT_LITERAL); }
+yy126:
+	yych = *++YYCURSOR;
+	if (yych <= ',') {
+		if (yych == '+') goto yy128;
+		goto yy114;
+	} else {
+		if (yych <= '-') goto yy128;
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy129;
+		goto yy114;
+	}
+yy127:
+	yych = *++YYCURSOR;
+	goto yy125;
+yy128:
+	yych = *++YYCURSOR;
+	if (yych <= '/') goto yy114;
+	if (yych >= ':') goto yy114;
+yy129:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'G') {
+		if (yych <= '9') {
+			if (yych <= '/') goto yy125;
+			goto yy129;
+		} else {
+			if (yych == 'F') goto yy127;
+			goto yy125;
+		}
+	} else {
+		if (yych <= 'f') {
+			if (yych <= 'H') goto yy127;
+			if (yych <= 'e') goto yy125;
+			goto yy127;
+		} else {
+			if (yych == 'h') goto yy127;
+			goto yy125;
+		}
+	}
+yy131:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+yy132:
+	if (yych <= '\r') {
+		if (yych == '\n') goto yy114;
+		if (yych <= '\f') goto yy131;
+		goto yy114;
+	} else {
+		if (yych <= '\'') {
+			if (yych <= '&') goto yy131;
+			goto yy134;
+		} else {
+			if (yych != '\\') goto yy131;
+		}
+	}
+yy133:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'b') {
+		if (yych <= '7') {
+			if (yych <= '&') {
+				if (yych == '"') goto yy131;
+				goto yy114;
+			} else {
+				if (yych <= '\'') goto yy131;
+				if (yych <= '/') goto yy114;
+				goto yy136;
+			}
+		} else {
+			if (yych <= '[') {
+				if (yych == '?') goto yy131;
+				goto yy114;
+			} else {
+				if (yych <= '\\') goto yy131;
+				if (yych <= '`') goto yy114;
+				goto yy131;
+			}
+		}
+	} else {
+		if (yych <= 'r') {
+			if (yych <= 'm') {
+				if (yych == 'f') goto yy131;
+				goto yy114;
+			} else {
+				if (yych <= 'n') goto yy131;
+				if (yych <= 'q') goto yy114;
+				goto yy131;
+			}
+		} else {
+			if (yych <= 'u') {
+				if (yych == 't') goto yy131;
+				goto yy114;
+			} else {
+				if (yych <= 'v') goto yy131;
+				if (yych == 'x') goto yy135;
+				goto yy114;
+			}
+		}
+	}
+yy134:
+	yych = *++YYCURSOR;
+	goto yy9;
+yy135:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '@') {
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy138;
+		goto yy114;
+	} else {
+		if (yych <= 'F') goto yy138;
+		if (yych <= '`') goto yy114;
+		if (yych <= 'f') goto yy138;
+		goto yy114;
+	}
+yy136:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '&') {
+		if (yych <= '\n') {
+			if (yych <= '\t') goto yy131;
+			goto yy114;
+		} else {
+			if (yych == '\r') goto yy114;
+			goto yy131;
+		}
+	} else {
+		if (yych <= '7') {
+			if (yych <= '\'') goto yy134;
+			if (yych <= '/') goto yy131;
+			goto yy136;
+		} else {
+			if (yych == '\\') goto yy133;
+			goto yy131;
+		}
+	}
+yy138:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '/') {
+		if (yych <= '\f') {
+			if (yych == '\n') goto yy114;
+			goto yy131;
+		} else {
+			if (yych <= '\r') goto yy114;
+			if (yych == '\'') goto yy134;
+			goto yy131;
+		}
+	} else {
+		if (yych <= '[') {
+			if (yych <= '9') goto yy138;
+			if (yych <= '@') goto yy131;
+			if (yych <= 'F') goto yy138;
+			goto yy131;
+		} else {
+			if (yych <= '\\') goto yy133;
+			if (yych <= '`') goto yy131;
+			if (yych <= 'f') goto yy138;
+			goto yy131;
+		}
+	}
+yy140:
+	yyaccept = 2;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych == 'E') goto yy151;
+	if (yych == 'e') goto yy151;
+	goto yy150;
+yy141:
+	yyaccept = 1;
+	YYMARKER = ++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
+	yych = *YYCURSOR;
+yy142:
+	if (yych <= 'L') {
+		if (yych <= '9') {
+			if (yych == '.') goto yy140;
+			if (yych <= '/') goto yy9;
+			goto yy141;
+		} else {
+			if (yych == 'E') goto yy143;
+			if (yych <= 'K') goto yy9;
+			goto yy144;
+		}
+	} else {
+		if (yych <= 'e') {
+			if (yych == 'U') goto yy144;
+			if (yych <= 'd') goto yy9;
+		} else {
+			if (yych <= 'l') {
+				if (yych <= 'k') goto yy9;
+				goto yy144;
+			} else {
+				if (yych == 'u') goto yy144;
+				goto yy9;
+			}
+		}
+	}
+yy143:
+	yych = *++YYCURSOR;
+	if (yych <= ',') {
+		if (yych == '+') goto yy146;
+		goto yy114;
+	} else {
+		if (yych <= '-') goto yy146;
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy147;
+		goto yy114;
+	}
+yy144:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'U') {
+		if (yych == 'L') goto yy144;
+		if (yych <= 'T') goto yy9;
+		goto yy144;
+	} else {
+		if (yych <= 'l') {
+			if (yych <= 'k') goto yy9;
+			goto yy144;
+		} else {
+			if (yych == 'u') goto yy144;
+			goto yy9;
+		}
+	}
+yy146:
+	yych = *++YYCURSOR;
+	if (yych <= '/') goto yy114;
+	if (yych >= ':') goto yy114;
+yy147:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'G') {
+		if (yych <= '9') {
+			if (yych <= '/') goto yy125;
+			goto yy147;
+		} else {
+			if (yych == 'F') goto yy127;
+			goto yy125;
+		}
+	} else {
+		if (yych <= 'f') {
+			if (yych <= 'H') goto yy127;
+			if (yych <= 'e') goto yy125;
+			goto yy127;
+		} else {
+			if (yych == 'h') goto yy127;
+			goto yy125;
+		}
+	}
+yy149:
+	yyaccept = 2;
+	YYMARKER = ++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
+	yych = *YYCURSOR;
+yy150:
+	if (yych <= 'G') {
+		if (yych <= 'D') {
+			if (yych <= '/') goto yy125;
+			if (yych <= '9') goto yy149;
+			goto yy125;
+		} else {
+			if (yych <= 'E') goto yy155;
+			if (yych <= 'F') goto yy127;
+			goto yy125;
+		}
+	} else {
+		if (yych <= 'e') {
+			if (yych <= 'H') goto yy127;
+			if (yych <= 'd') goto yy125;
+			goto yy155;
+		} else {
+			if (yych == 'g') goto yy125;
+			if (yych <= 'h') goto yy127;
+			goto yy125;
+		}
+	}
+yy151:
+	yych = *++YYCURSOR;
+	if (yych <= ',') {
+		if (yych != '+') goto yy114;
+	} else {
+		if (yych <= '-') goto yy152;
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy153;
+		goto yy114;
+	}
+yy152:
+	yych = *++YYCURSOR;
+	if (yych <= '/') goto yy114;
+	if (yych >= ':') goto yy114;
+yy153:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'G') {
+		if (yych <= '9') {
+			if (yych <= '/') goto yy125;
+			goto yy153;
+		} else {
+			if (yych == 'F') goto yy127;
+			goto yy125;
+		}
+	} else {
+		if (yych <= 'f') {
+			if (yych <= 'H') goto yy127;
+			if (yych <= 'e') goto yy125;
+			goto yy127;
+		} else {
+			if (yych == 'h') goto yy127;
+			goto yy125;
+		}
+	}
+yy155:
+	yych = *++YYCURSOR;
+	if (yych <= ',') {
+		if (yych != '+') goto yy114;
+	} else {
+		if (yych <= '-') goto yy156;
+		if (yych <= '/') goto yy114;
+		if (yych <= '9') goto yy157;
+		goto yy114;
+	}
+yy156:
+	yych = *++YYCURSOR;
+	if (yych <= '/') goto yy114;
+	if (yych >= ':') goto yy114;
+yy157:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'G') {
+		if (yych <= '9') {
+			if (yych <= '/') goto yy125;
+			goto yy157;
+		} else {
+			if (yych == 'F') goto yy127;
+			goto yy125;
+		}
+	} else {
+		if (yych <= 'f') {
+			if (yych <= 'H') goto yy127;
+			if (yych <= 'e') goto yy125;
+			goto yy127;
+		} else {
+			if (yych == 'h') goto yy127;
+			goto yy125;
+		}
+	}
+yy159:
+	yyaccept = 1;
+	YYMARKER = ++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
+	yych = *YYCURSOR;
+yy160:
+	if (yych <= 'L') {
+		if (yych <= '9') {
+			if (yych == '.') goto yy140;
+			if (yych <= '/') goto yy9;
+			goto yy159;
+		} else {
+			if (yych == 'E') goto yy143;
+			if (yych <= 'K') goto yy9;
+			goto yy166;
+		}
+	} else {
+		if (yych <= 'e') {
+			if (yych == 'U') goto yy166;
+			if (yych <= 'd') goto yy9;
+			goto yy143;
+		} else {
+			if (yych <= 'l') {
+				if (yych <= 'k') goto yy9;
+				goto yy166;
+			} else {
+				if (yych == 'u') goto yy166;
+				goto yy9;
+			}
+		}
+	}
+yy161:
+	yych = *++YYCURSOR;
+	if (yych <= '@') {
+		if (yych <= '/') goto yy114;
+		if (yych >= ':') goto yy114;
+	} else {
+		if (yych <= 'F') goto yy162;
+		if (yych <= '`') goto yy114;
+		if (yych >= 'g') goto yy114;
+	}
+yy162:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'T') {
+		if (yych <= '@') {
+			if (yych <= '/') goto yy9;
+			if (yych <= '9') goto yy162;
+			goto yy9;
+		} else {
+			if (yych <= 'F') goto yy162;
+			if (yych != 'L') goto yy9;
+		}
+	} else {
+		if (yych <= 'k') {
+			if (yych <= 'U') goto yy164;
+			if (yych <= '`') goto yy9;
+			if (yych <= 'f') goto yy162;
+			goto yy9;
+		} else {
+			if (yych <= 'l') goto yy164;
+			if (yych != 'u') goto yy9;
+		}
+	}
+yy164:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'U') {
+		if (yych == 'L') goto yy164;
+		if (yych <= 'T') goto yy9;
+		goto yy164;
+	} else {
+		if (yych <= 'l') {
+			if (yych <= 'k') goto yy9;
+			goto yy164;
+		} else {
+			if (yych == 'u') goto yy164;
+			goto yy9;
+		}
+	}
+yy166:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= 'U') {
+		if (yych == 'L') goto yy166;
+		if (yych <= 'T') goto yy9;
+		goto yy166;
+	} else {
+		if (yych <= 'l') {
+			if (yych <= 'k') goto yy9;
+			goto yy166;
+		} else {
+			if (yych == 'u') goto yy166;
+			goto yy9;
+		}
+	}
+yy168:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+yy169:
+	if (yych <= 'Z') {
+		if (yych <= '/') goto yy7;
+		if (yych <= '9') goto yy168;
+		if (yych <= '@') goto yy7;
+		goto yy168;
+	} else {
+		if (yych <= '_') {
+			if (yych <= '^') goto yy7;
+			goto yy168;
+		} else {
+			if (yych <= '`') goto yy7;
+			if (yych <= 'z') goto yy168;
+			goto yy7;
+		}
+	}
+yy170:
+	++YYCURSOR;
+	{ RET(TOKEN_DIVASSIGN); }
+yy172:
+	++YYCURSOR;
+	{ goto singlelinecomment; }
+yy174:
+	++YYCURSOR;
+	{ goto multilinecomment; }
+yy176:
+	++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
+	yych = *YYCURSOR;
+yy177:
+	if (yych <= '\f') {
+		if (yych <= 0x08) goto yy114;
+		if (yych != '\n') goto yy176;
+	} else {
+		if (yych <= '\r') goto yy180;
+		if (yych == ' ') goto yy176;
+		goto yy114;
+	}
+yy178:
+	++YYCURSOR;
+yy179:
+	{ s->line++; goto scanner_loop; }
+yy180:
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '\n') goto yy178;
+	goto yy179;
+}
+
+
+multilinecomment:
+    if (YYLIMIT == YYCURSOR) YYFILL(1);
+    matchptr = cursor;
+// The "*\/" is just to avoid screwing up text editor syntax highlighting.
+
+{
+	YYCTYPE yych;
+	if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
+	yych = *YYCURSOR;
+	if (yych <= '\f') {
+		if (yych <= 0x00) goto yy188;
+		if (yych == '\n') goto yy185;
+		goto yy190;
+	} else {
+		if (yych <= '\r') goto yy187;
+		if (yych != '*') goto yy190;
+	}
+	++YYCURSOR;
+	if ((yych = *YYCURSOR) == '/') goto yy192;
+yy184:
+	{ goto multilinecomment; }
+yy185:
+	++YYCURSOR;
+yy186:
+	{
+                        s->line++;
+                        token = matchptr;
+                        saw_newline = 1;
+                        goto multilinecomment;
+                    }
+yy187:
+	yych = *++YYCURSOR;
+	if (yych == '\n') goto yy191;
+	goto yy186;
+yy188:
+	++YYCURSOR;
+	{
+                        if (eoi)
+                            RET(TOKEN_INCOMPLETE_COMMENT);
+                        goto multilinecomment;
+                    }
+yy190:
+	yych = *++YYCURSOR;
+	goto yy184;
+yy191:
+	yych = *++YYCURSOR;
+	goto yy186;
+yy192:
+	++YYCURSOR;
+	{
+                        if (saw_newline)
+                            RET('\n');
+                        else if (s->report_whitespace)
+                            RET(' ');
+                        goto scanner_loop;
+                    }
+}
+
+
+singlelinecomment:
+    if (YYLIMIT == YYCURSOR) YYFILL(1);
+    matchptr = cursor;
+
+{
+	YYCTYPE yych;
+	if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
+	yych = *YYCURSOR;
+	if (yych <= '\n') {
+		if (yych <= 0x00) goto yy199;
+		if (yych <= '\t') goto yy201;
+	} else {
+		if (yych == '\r') goto yy198;
+		goto yy201;
+	}
+	++YYCURSOR;
+yy197:
+	{ s->line++; token = matchptr; RET('\n'); }
+yy198:
+	yych = *++YYCURSOR;
+	if (yych == '\n') goto yy203;
+	goto yy197;
+yy199:
+	++YYCURSOR;
+	{ if (eoi) { RET(TOKEN_EOI); } goto singlelinecomment; }
+yy201:
+	++YYCURSOR;
+	{ goto singlelinecomment; }
+yy203:
+	++YYCURSOR;
+	yych = *YYCURSOR;
+	goto yy197;
+}
+
+
+ppdirective:
+    if (YYLIMIT == YYCURSOR) YYFILL(1);
+
+{
+	YYCTYPE yych;
+	unsigned int yyaccept = 0;
+	if ((YYLIMIT - YYCURSOR) < 8) YYFILL(8);
+	yych = *YYCURSOR;
+	if (yych <= '\f') {
+		if (yych == '\t') goto yy208;
+		if (yych <= '\n') goto yy210;
+		goto yy208;
+	} else {
+		if (yych <= ' ') {
+			if (yych <= 0x1F) goto yy210;
+			goto yy208;
+		} else {
+			if (yych != '#') goto yy210;
+		}
+	}
+	yyaccept = 0;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych <= 'h') {
+		if (yych <= 0x1F) {
+			if (yych == '\t') goto yy214;
+		} else {
+			if (yych <= ' ') goto yy214;
+			if (yych <= 'c') goto yy207;
+			if (yych <= 'e') goto yy214;
+		}
+	} else {
+		if (yych <= 'o') {
+			if (yych <= 'i') goto yy214;
+			if (yych == 'l') goto yy214;
+		} else {
+			if (yych <= 'p') goto yy214;
+			if (yych == 'u') goto yy214;
+		}
+	}
+yy207:
+	{
+                            token = cursor = (const uchar *) s->source;
+                            limit = cursor + s->bytes_left;
+                            goto scanner_loop;
+                        }
+yy208:
+	++YYCURSOR;
+	yych = *YYCURSOR;
+	goto yy212;
+yy209:
+	{ goto ppdirective; }
+yy210:
+	yych = *++YYCURSOR;
+	goto yy207;
+yy211:
+	++YYCURSOR;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+yy212:
+	if (yych <= '\n') {
+		if (yych == '\t') goto yy211;
+		goto yy209;
+	} else {
+		if (yych <= '\f') goto yy211;
+		if (yych == ' ') goto yy211;
+		goto yy209;
+	}
+yy213:
+	++YYCURSOR;
+	if ((YYLIMIT - YYCURSOR) < 7) YYFILL(7);
+	yych = *YYCURSOR;
+yy214:
+	if (yych <= 'h') {
+		if (yych <= ' ') {
+			if (yych == '\t') goto yy213;
+			if (yych >= ' ') goto yy213;
+		} else {
+			if (yych <= 'c') goto yy215;
+			if (yych <= 'd') goto yy220;
+			if (yych <= 'e') goto yy217;
+		}
+	} else {
+		if (yych <= 'o') {
+			if (yych <= 'i') goto yy218;
+			if (yych == 'l') goto yy221;
+		} else {
+			if (yych <= 'p') goto yy216;
+			if (yych == 'u') goto yy219;
+		}
+	}
+yy215:
+	YYCURSOR = YYMARKER;
+	if (yyaccept <= 0) {
+		goto yy207;
+	} else {
+		goto yy239;
+	}
+yy216:
+	yych = *++YYCURSOR;
+	if (yych == 'r') goto yy272;
+	goto yy215;
+yy217:
+	yych = *++YYCURSOR;
+	if (yych <= 'm') {
+		if (yych == 'l') goto yy255;
+		goto yy215;
+	} else {
+		if (yych <= 'n') goto yy256;
+		if (yych == 'r') goto yy257;
+		goto yy215;
+	}
+yy218:
+	yych = *++YYCURSOR;
+	if (yych == 'f') goto yy238;
+	if (yych == 'n') goto yy237;
+	goto yy215;
+yy219:
+	yych = *++YYCURSOR;
+	if (yych == 'n') goto yy232;
+	goto yy215;
+yy220:
+	yych = *++YYCURSOR;
+	if (yych == 'e') goto yy226;
+	goto yy215;
+yy221:
+	yych = *++YYCURSOR;
+	if (yych != 'i') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'n') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_LINE); }
+yy226:
+	yych = *++YYCURSOR;
+	if (yych != 'f') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'i') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'n') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_DEFINE); }
+yy232:
+	yych = *++YYCURSOR;
+	if (yych != 'd') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'f') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_UNDEF); }
+yy237:
+	yych = *++YYCURSOR;
+	if (yych == 'c') goto yy249;
+	goto yy215;
+yy238:
+	yyaccept = 1;
+	yych = *(YYMARKER = ++YYCURSOR);
+	if (yych == 'd') goto yy241;
+	if (yych == 'n') goto yy240;
+yy239:
+	{ RET(TOKEN_PP_IF); }
+yy240:
+	yych = *++YYCURSOR;
+	if (yych == 'd') goto yy245;
+	goto yy215;
+yy241:
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'f') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_IFDEF); }
+yy245:
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'f') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_IFNDEF); }
+yy249:
+	yych = *++YYCURSOR;
+	if (yych != 'l') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'u') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'd') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_INCLUDE); }
+yy255:
+	yych = *++YYCURSOR;
+	if (yych == 'i') goto yy266;
+	if (yych == 's') goto yy267;
+	goto yy215;
+yy256:
+	yych = *++YYCURSOR;
+	if (yych == 'd') goto yy262;
+	goto yy215;
+yy257:
+	yych = *++YYCURSOR;
+	if (yych != 'r') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'o') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'r') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_ERROR); }
+yy262:
+	yych = *++YYCURSOR;
+	if (yych != 'i') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'f') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_ENDIF); }
+yy266:
+	yych = *++YYCURSOR;
+	if (yych == 'f') goto yy270;
+	goto yy215;
+yy267:
+	yych = *++YYCURSOR;
+	if (yych != 'e') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_ELSE); }
+yy270:
+	++YYCURSOR;
+	{ RET(TOKEN_PP_ELIF); }
+yy272:
+	yych = *++YYCURSOR;
+	if (yych != 'a') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'g') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'm') goto yy215;
+	yych = *++YYCURSOR;
+	if (yych != 'a') goto yy215;
+	++YYCURSOR;
+	{ RET(TOKEN_PP_PRAGMA); }
+}
+
+
+bad_chars:
+    if (YYLIMIT == YYCURSOR) YYFILL(1);
+
+{
+	YYCTYPE yych;
+	if (YYLIMIT <= YYCURSOR) YYFILL(1);
+	yych = *YYCURSOR;
+	if (yych <= '#') {
+		if (yych <= '\r') {
+			if (yych <= 0x00) goto yy282;
+			if (yych <= 0x08) goto yy284;
+		} else {
+			if (yych <= 0x1F) goto yy284;
+			if (yych == '"') goto yy284;
+		}
+	} else {
+		if (yych <= '@') {
+			if (yych <= '$') goto yy284;
+			if (yych >= '@') goto yy284;
+		} else {
+			if (yych == '`') goto yy284;
+			if (yych >= 0x7F) goto yy284;
+		}
+	}
+	++YYCURSOR;
+	{ cursor--; RET(TOKEN_BAD_CHARS); }
+yy282:
+	++YYCURSOR;
+	{
+                        if (eoi)
+                        {
+                            assert( !((token >= sentinel) &&
+                                     (token < sentinel+YYMAXFILL)) );
+                            eoi = 0;
+                            cursor = (uchar *) s->source_base + s->orig_length;
+                            RET(TOKEN_BAD_CHARS);  // next call will be EOI.
+                        }
+                        goto bad_chars;
+                    }
+yy284:
+	++YYCURSOR;
+	{ goto bad_chars; }
+}
+
+
+    assert(0 && "Shouldn't hit this code");
+    RET(TOKEN_UNKNOWN);
+} // preprocessor_lexer
+
+// end of mojoshader_lexer_preprocessor.re (or .c) ...
+

+ 4272 - 0
ThirdParty/MojoShader/mojoshader_parser_hlsl.h

@@ -0,0 +1,4272 @@
+/*
+ * My changes over the original lempar.c from SQLite are encased in
+ *  #if __MOJOSHADER__ blocks.  --ryan.
+ */
+#ifndef __MOJOSHADER__
+#define __MOJOSHADER__ 1
+#endif
+
+#if !__MOJOSHADER__
+#define LEMON_SUPPORT_TRACING (!defined(NDEBUG))
+#endif
+
+/* Driver template for the LEMON parser generator.
+** The original author(s) of lempar.c disclaim copyright to this source code.
+** However, changes made for MojoShader fall under the same license as the
+** rest of MojoShader. Please see the file LICENSE.txt in the source's root
+** directory.
+*/
+/* First off, code is included that follows the "include" declaration
+** in the input grammar file. */
+#include <stdio.h>
+#line 31 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+
+#ifndef __MOJOSHADER_HLSL_COMPILER__
+#error Do not compile this file directly.
+#endif
+#line 28 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+/* Next is all token values, in a form suitable for use by makeheaders.
+** This section will be null unless lemon is run with the -m switch.
+*/
+/* 
+** These constants (all generated automatically by the parser generator)
+** specify the various kinds of tokens (terminals) that the parser
+** understands. 
+**
+** Each symbol here is a terminal symbol in the grammar.
+*/
+#define TOKEN_HLSL_COMMA                           1
+#define TOKEN_HLSL_ASSIGN                          2
+#define TOKEN_HLSL_ADDASSIGN                       3
+#define TOKEN_HLSL_SUBASSIGN                       4
+#define TOKEN_HLSL_MULASSIGN                       5
+#define TOKEN_HLSL_DIVASSIGN                       6
+#define TOKEN_HLSL_MODASSIGN                       7
+#define TOKEN_HLSL_LSHIFTASSIGN                    8
+#define TOKEN_HLSL_RSHIFTASSIGN                    9
+#define TOKEN_HLSL_ANDASSIGN                      10
+#define TOKEN_HLSL_ORASSIGN                       11
+#define TOKEN_HLSL_XORASSIGN                      12
+#define TOKEN_HLSL_QUESTION                       13
+#define TOKEN_HLSL_OROR                           14
+#define TOKEN_HLSL_ANDAND                         15
+#define TOKEN_HLSL_OR                             16
+#define TOKEN_HLSL_XOR                            17
+#define TOKEN_HLSL_AND                            18
+#define TOKEN_HLSL_EQL                            19
+#define TOKEN_HLSL_NEQ                            20
+#define TOKEN_HLSL_LT                             21
+#define TOKEN_HLSL_LEQ                            22
+#define TOKEN_HLSL_GT                             23
+#define TOKEN_HLSL_GEQ                            24
+#define TOKEN_HLSL_LSHIFT                         25
+#define TOKEN_HLSL_RSHIFT                         26
+#define TOKEN_HLSL_PLUS                           27
+#define TOKEN_HLSL_MINUS                          28
+#define TOKEN_HLSL_STAR                           29
+#define TOKEN_HLSL_SLASH                          30
+#define TOKEN_HLSL_PERCENT                        31
+#define TOKEN_HLSL_TYPECAST                       32
+#define TOKEN_HLSL_EXCLAMATION                    33
+#define TOKEN_HLSL_COMPLEMENT                     34
+#define TOKEN_HLSL_MINUSMINUS                     35
+#define TOKEN_HLSL_PLUSPLUS                       36
+#define TOKEN_HLSL_DOT                            37
+#define TOKEN_HLSL_LBRACKET                       38
+#define TOKEN_HLSL_RBRACKET                       39
+#define TOKEN_HLSL_LPAREN                         40
+#define TOKEN_HLSL_RPAREN                         41
+#define TOKEN_HLSL_ELSE                           42
+#define TOKEN_HLSL_SEMICOLON                      43
+#define TOKEN_HLSL_TYPEDEF                        44
+#define TOKEN_HLSL_CONST                          45
+#define TOKEN_HLSL_IDENTIFIER                     46
+#define TOKEN_HLSL_VOID                           47
+#define TOKEN_HLSL_INLINE                         48
+#define TOKEN_HLSL_IN                             49
+#define TOKEN_HLSL_INOUT                          50
+#define TOKEN_HLSL_OUT                            51
+#define TOKEN_HLSL_UNIFORM                        52
+#define TOKEN_HLSL_COLON                          53
+#define TOKEN_HLSL_LINEAR                         54
+#define TOKEN_HLSL_CENTROID                       55
+#define TOKEN_HLSL_NOINTERPOLATION                56
+#define TOKEN_HLSL_NOPERSPECTIVE                  57
+#define TOKEN_HLSL_SAMPLE                         58
+#define TOKEN_HLSL_EXTERN                         59
+#define TOKEN_HLSL_SHARED                         60
+#define TOKEN_HLSL_STATIC                         61
+#define TOKEN_HLSL_VOLATILE                       62
+#define TOKEN_HLSL_ROWMAJOR                       63
+#define TOKEN_HLSL_COLUMNMAJOR                    64
+#define TOKEN_HLSL_LBRACE                         65
+#define TOKEN_HLSL_RBRACE                         66
+#define TOKEN_HLSL_STRUCT                         67
+#define TOKEN_HLSL_PACKOFFSET                     68
+#define TOKEN_HLSL_REGISTER                       69
+#define TOKEN_HLSL_USERTYPE                       70
+#define TOKEN_HLSL_SAMPLER                        71
+#define TOKEN_HLSL_SAMPLER1D                      72
+#define TOKEN_HLSL_SAMPLER2D                      73
+#define TOKEN_HLSL_SAMPLER3D                      74
+#define TOKEN_HLSL_SAMPLERCUBE                    75
+#define TOKEN_HLSL_SAMPLER_STATE                  76
+#define TOKEN_HLSL_SAMPLERSTATE                   77
+#define TOKEN_HLSL_SAMPLERCOMPARISONSTATE         78
+#define TOKEN_HLSL_BOOL                           79
+#define TOKEN_HLSL_INT                            80
+#define TOKEN_HLSL_UINT                           81
+#define TOKEN_HLSL_HALF                           82
+#define TOKEN_HLSL_FLOAT                          83
+#define TOKEN_HLSL_DOUBLE                         84
+#define TOKEN_HLSL_STRING                         85
+#define TOKEN_HLSL_SNORM                          86
+#define TOKEN_HLSL_UNORM                          87
+#define TOKEN_HLSL_BUFFER                         88
+#define TOKEN_HLSL_VECTOR                         89
+#define TOKEN_HLSL_INT_CONSTANT                   90
+#define TOKEN_HLSL_MATRIX                         91
+#define TOKEN_HLSL_ISOLATE                        92
+#define TOKEN_HLSL_MAXINSTRUCTIONCOUNT            93
+#define TOKEN_HLSL_NOEXPRESSIONOPTIMIZATIONS      94
+#define TOKEN_HLSL_REMOVEUNUSEDINPUTS             95
+#define TOKEN_HLSL_UNUSED                         96
+#define TOKEN_HLSL_XPS                            97
+#define TOKEN_HLSL_BREAK                          98
+#define TOKEN_HLSL_CONTINUE                       99
+#define TOKEN_HLSL_DISCARD                        100
+#define TOKEN_HLSL_DO                             101
+#define TOKEN_HLSL_WHILE                          102
+#define TOKEN_HLSL_RETURN                         103
+#define TOKEN_HLSL_UNROLL                         104
+#define TOKEN_HLSL_LOOP                           105
+#define TOKEN_HLSL_FOR                            106
+#define TOKEN_HLSL_BRANCH                         107
+#define TOKEN_HLSL_IF                             108
+#define TOKEN_HLSL_FLATTEN                        109
+#define TOKEN_HLSL_IFALL                          110
+#define TOKEN_HLSL_IFANY                          111
+#define TOKEN_HLSL_PREDICATE                      112
+#define TOKEN_HLSL_PREDICATEBLOCK                 113
+#define TOKEN_HLSL_SWITCH                         114
+#define TOKEN_HLSL_FORCECASE                      115
+#define TOKEN_HLSL_CALL                           116
+#define TOKEN_HLSL_CASE                           117
+#define TOKEN_HLSL_DEFAULT                        118
+#define TOKEN_HLSL_FLOAT_CONSTANT                 119
+#define TOKEN_HLSL_STRING_LITERAL                 120
+#define TOKEN_HLSL_TRUE                           121
+#define TOKEN_HLSL_FALSE                          122
+/* Make sure the INTERFACE macro is defined.
+*/
+#ifndef INTERFACE
+# define INTERFACE 1
+#endif
+/* The next thing included is series of defines which control
+** various aspects of the generated parser.
+**    YYCODETYPE         is the data type used for storing terminal
+**                       and nonterminal numbers.  "unsigned char" is
+**                       used if there are fewer than 250 terminals
+**                       and nonterminals.  "int" is used otherwise.
+**    YYNOCODE           is a number of type YYCODETYPE which corresponds
+**                       to no legal terminal or nonterminal number.  This
+**                       number is used to fill in empty slots of the hash 
+**                       table.
+**    YYFALLBACK         If defined, this indicates that one or more tokens
+**                       have fall-back values which should be used if the
+**                       original value of the token will not parse.
+**    YYACTIONTYPE       is the data type used for storing terminal
+**                       and nonterminal numbers.  "unsigned char" is
+**                       used if there are fewer than 250 rules and
+**                       states combined.  "int" is used otherwise.
+**    ParseHLSLTOKENTYPE     is the data type used for minor tokens given 
+**                       directly to the parser from the tokenizer.
+**    YYMINORTYPE        is the data type used for all minor tokens.
+**                       This is typically a union of many types, one of
+**                       which is ParseHLSLTOKENTYPE.  The entry in the union
+**                       for base tokens is called "yy0".
+**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
+**                       zero the stack is dynamically sized using realloc()
+**    ParseHLSLARG_SDECL     A static variable declaration for the %extra_argument
+**    ParseHLSLARG_PDECL     A parameter declaration for the %extra_argument
+**    ParseHLSLARG_STORE     Code to store %extra_argument into yypParser
+**    ParseHLSLARG_FETCH     Code to extract %extra_argument from yypParser
+**    YYNSTATE           the combined number of states.
+**    YYNRULE            the number of rules in the grammar
+**    YYERRORSYMBOL      is the code number of the error symbol.  If not
+**                       defined, then do no error processing.
+*/
+#define YYCODETYPE unsigned char
+#define YYNOCODE 198
+#define YYACTIONTYPE unsigned short int
+#define ParseHLSLTOKENTYPE  TokenData 
+typedef union {
+  int yyinit;
+  ParseHLSLTOKENTYPE yy0;
+  MOJOSHADER_astPackOffset * yy8;
+  MOJOSHADER_astVariableDeclaration * yy24;
+  MOJOSHADER_astArguments * yy26;
+  const MOJOSHADER_astDataType * yy37;
+  MOJOSHADER_astTypedef * yy71;
+  MOJOSHADER_astInputModifier yy75;
+  MOJOSHADER_astVariableLowLevel * yy82;
+  MOJOSHADER_astInterpolationModifier yy111;
+  MOJOSHADER_astCompilationUnit * yy139;
+  MOJOSHADER_astSwitchCases * yy165;
+  MOJOSHADER_astFunctionStorageClass yy175;
+  MOJOSHADER_astStatement * yy233;
+  MOJOSHADER_astStructDeclaration * yy249;
+  MOJOSHADER_astAnnotations * yy268;
+  int yy270;
+  const char * yy306;
+  MOJOSHADER_astFunctionParameters * yy307;
+  MOJOSHADER_astExpression * yy322;
+  MOJOSHADER_astStructMembers * yy346;
+  MOJOSHADER_astFunctionSignature * yy364;
+  MOJOSHADER_astScalarOrArray * yy380;
+} YYMINORTYPE;
+#ifndef YYSTACKDEPTH
+#define YYSTACKDEPTH 100
+#endif
+#define ParseHLSLARG_SDECL  Context *ctx ;
+#define ParseHLSLARG_PDECL , Context *ctx 
+#define ParseHLSLARG_FETCH  Context *ctx  = yypParser->ctx 
+#define ParseHLSLARG_STORE yypParser->ctx  = ctx 
+#define YYNSTATE 525
+#define YYNRULE 288
+#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
+#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
+#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
+
+/* The yyzerominor constant is used to initialize instances of
+** YYMINORTYPE objects to zero. */
+static const YYMINORTYPE yyzerominor = { 0 };
+
+/* Define the yytestcase() macro to be a no-op if is not already defined
+** otherwise.
+**
+** Applications can choose to define yytestcase() in the %include section
+** to a macro that can assist in verifying code coverage.  For production
+** code the yytestcase() macro should be turned off.  But it is useful
+** for testing.
+*/
+#ifndef yytestcase
+# define yytestcase(X)
+#endif
+
+
+/* Next are the tables used to determine what action to take based on the
+** current state and lookahead token.  These tables are used to implement
+** functions that take a state number and lookahead value and return an
+** action integer.  
+**
+** Suppose the action integer is N.  Then the action is determined as
+** follows
+**
+**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
+**                                      token onto the stack and goto state N.
+**
+**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
+**
+**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
+**
+**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
+**
+**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
+**                                      slots in the yy_action[] table.
+**
+** The action table is constructed as a single large table named yy_action[].
+** Given state S and lookahead X, the action is computed as
+**
+**      yy_action[ yy_shift_ofst[S] + X ]
+**
+** If the index value yy_shift_ofst[S]+X is out of range or if the value
+** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
+** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
+** and that yy_default[S] should be used instead.  
+**
+** The formula above is for computing the action when the lookahead is
+** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
+** a reduce action) then the yy_reduce_ofst[] array is used in place of
+** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
+** YY_SHIFT_USE_DFLT.
+**
+** The following are the tables generated in this section:
+**
+**  yy_action[]        A single table containing all actions.
+**  yy_lookahead[]     A table containing the lookahead for each entry in
+**                     yy_action.  Used to detect hash collisions.
+**  yy_shift_ofst[]    For each state, the offset into yy_action for
+**                     shifting terminals.
+**  yy_reduce_ofst[]   For each state, the offset into yy_action for
+**                     shifting non-terminals after a reduce.
+**  yy_default[]       Default action for each state.
+*/
+#define YY_ACTTAB_COUNT (5407)
+static const YYACTIONTYPE yy_action[] = {
+ /*     0 */    86,   85,  446,  447,  264,   46,   83,   84,   90,   91,
+ /*    10 */    39,  123,   88,   47,   27,  114,  344,   97,  405,  143,
+ /*    20 */   104,  422,   82,   81,   80,  407,  348,  140,  139,  410,
+ /*    30 */    78,   77,  411,  409,  408,  406,  404,  403,    1,  383,
+ /*    40 */   249,  495,   60,  511,  510,  509,  508,  507,  506,  505,
+ /*    50 */   504,  503,  502,  501,  500,  499,  498,  497,  496,  295,
+ /*    60 */   294,  293,  282,  482,  278,  250,  398,  397,  396,  395,
+ /*    70 */   394,  248,  247,  246,  334,  338,   37,   40,  214,  336,
+ /*    80 */   518,  333,   74,   72,   73,   71,   39,  332,  494,  100,
+ /*    90 */    86,   85,  481,  480,  479,  478,   83,   84,   90,   91,
+ /*   100 */   186,  123,  413,   47,   27,  115,  344,   97,  405,  143,
+ /*   110 */   362,  429,   70,   69,  432,  407,  361,  140,  139,  410,
+ /*   120 */   140,  139,  411,  409,  408,  406,  404,  403,    1,  384,
+ /*   130 */   249,   76,   75,  511,  510,  509,  508,  507,  506,  505,
+ /*   140 */   504,  503,  502,  501,  500,  499,  498,  497,  496,  295,
+ /*   150 */   294,  293,  282,  482,  278,  146,  398,  397,  396,  395,
+ /*   160 */   394,  248,  247,  246,  334,  338,   37,  369,  371,  336,
+ /*   170 */   119,  333,  370,  366,  368,  132,  310,  332,  367,  100,
+ /*   180 */    86,   85,  481,  480,  479,  478,   83,   84,   90,   91,
+ /*   190 */   180,  123,  413,   47,  113,  423,  344,   97,  405,  143,
+ /*   200 */   416,  140,  139,  493,  417,  407,  140,  139,  100,  410,
+ /*   210 */   140,  139,  411,  409,  408,  406,  404,  403,    1,  181,
+ /*   220 */   249,  413,   60,  511,  510,  509,  508,  507,  506,  505,
+ /*   230 */   504,  503,  502,  501,  500,  499,  498,  497,  496,  295,
+ /*   240 */   294,  293,  282,  482,  278,   63,  436,  117,  418,  138,
+ /*   250 */   492,  248,  247,  246,  334,  338,   37,  415,  122,  336,
+ /*   260 */   121,  333,  477,  140,  139,   62,   63,  332,  263,  259,
+ /*   270 */    86,   85,  481,  480,  479,  478,   83,   84,   90,   91,
+ /*   280 */   391,  261,   24,   47,   61,  434,   33,  515,  405,  143,
+ /*   290 */   512,  517,  516,  514,  513,  407,  363,  365,  100,  410,
+ /*   300 */   438,  364,  411,  409,  408,  406,  404,  403,   48,  181,
+ /*   310 */   249,  413,   60,  511,  510,  509,  508,  507,  506,  505,
+ /*   320 */   504,  503,  502,  501,  500,  499,  498,  497,  496,  295,
+ /*   330 */   294,  293,  282,  482,  278,  263,  259,  352,  118,  340,
+ /*   340 */   345,  109,  102,  130,  306,  122,  474,  428,  443,  122,
+ /*   350 */   475,  152,  241,   94,  360,  399,  491,  421,  400,  252,
+ /*   360 */   359,  296,  481,  480,  479,  478,  170,   24,   60,  515,
+ /*   370 */    38,   79,  512,  517,  516,  514,  513,   18,  346,   60,
+ /*   380 */   223,  219,  218,  216,  251,  339,  135,  402,   60,  476,
+ /*   390 */   150,  401,   60,  450,  145,  467,  157,  199,  198,  149,
+ /*   400 */   191,  272,  271,  269,  267,  189,  462,  352,  220,  340,
+ /*   410 */   345,  109,  102,  502,  501,  500,  499,  498,  497,  496,
+ /*   420 */   295,  294,   60,   94,  100,  399,  390,  522,   21,  252,
+ /*   430 */   490,    3,  426,  489,  343,   38,  170,  437,   60,  515,
+ /*   440 */    60,  296,  512,  517,  516,  514,  513,    5,  346,    1,
+ /*   450 */   223,  219,  218,  216,  318,  339,  135,  296,   27,  476,
+ /*   460 */   150,   27,   20,  450,  145,  467,  157,  199,  198,  149,
+ /*   470 */   191,  272,  271,  269,  267,  189,  462,  105,  215,  285,
+ /*   480 */   105,  352,  341,  340,  345,  109,  102,  502,  501,  500,
+ /*   490 */   499,  498,  497,  496,  295,  294,  101,   94,  488,  399,
+ /*   500 */   126,  487,  112,  252,  320,   40,  214,  103,  414,  184,
+ /*   510 */   170,  283,  154,  515,  140,  139,  512,  517,  516,  514,
+ /*   520 */   513,    4,  346,  486,  223,  219,  218,  216,   60,  339,
+ /*   530 */   135,   60,  213,  476,  150,  134,  125,  450,  145,  467,
+ /*   540 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*   550 */   462,  352,  137,  340,  345,  109,  102,  280,   60,  515,
+ /*   560 */    60,   60,  512,  517,  516,  514,  513,   94,  485,  399,
+ /*   570 */    36,   17,  179,  252,  292,  291,  290,  289,  288,  287,
+ /*   580 */   170,  286,  284,  515,   60,  124,  512,  517,  516,  514,
+ /*   590 */   513,   60,  222,   60,  223,  219,  218,  216,   14,  339,
+ /*   600 */   135,   12,   31,  476,  150,  144,   60,  450,  145,  467,
+ /*   610 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*   620 */   462,   27,  133,  312,  279,  352,   35,  340,  345,  109,
+ /*   630 */   102,   10,  515,    9,  276,  512,  517,  516,  514,  513,
+ /*   640 */    60,   94,  275,  399,  131,  308,  274,  252,   29,  420,
+ /*   650 */   138,  273,  484,   68,  170,   67,   65,  515,   64,  448,
+ /*   660 */   512,  517,  516,  514,  513,  262,  350,  185,  223,  219,
+ /*   670 */   218,  216,  184,  339,  135,  209,  260,  476,  150,  259,
+ /*   680 */     6,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*   690 */   271,  269,  267,  189,  462,  352,  435,  340,  345,  109,
+ /*   700 */   102,  258,  515,  257,  433,  512,  517,  516,  514,  513,
+ /*   710 */    60,   94,  263,  399,  419,   89,  389,  252,  436,  385,
+ /*   720 */   381,  380,  379,  244,  170,  377,  243,  515,  136,  239,
+ /*   730 */   512,  517,  516,  514,  513,  153,  217,  240,  223,  219,
+ /*   740 */   218,  216,  151,  339,  135,  177,  176,  476,  150,  141,
+ /*   750 */   234,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*   760 */   271,  269,  267,  189,  462,  232,  358,  357,  230,  352,
+ /*   770 */   356,  340,  345,  109,  102,  228,  515,  355,  226,  512,
+ /*   780 */   517,  516,  514,  513,  224,   94,   22,  399,  354,  353,
+ /*   790 */    44,  252,  221,   43,  351,   42,   19,   41,  170,  120,
+ /*   800 */     2,  515,  212,   88,  512,  517,  516,  514,  513,  316,
+ /*   810 */   349,   93,  223,  219,  218,  216,  107,  339,  135,  106,
+ /*   820 */   305,  476,  150,  303,  204,  450,  145,  467,  157,  199,
+ /*   830 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  352,
+ /*   840 */    87,  340,  345,  109,  102,  300,  520,  296,  483,   38,
+ /*   850 */   439,  256,  431,  430,   27,   94,  255,  399,  253,  388,
+ /*   860 */   250,  252,  474,  337,  372,   23,  317,    1,  170,  313,
+ /*   870 */   311,  515,  521,  309,  512,  517,  516,  514,  513,  307,
+ /*   880 */   382,  281,  223,  219,  218,  216,  347,  339,  135,  242,
+ /*   890 */   184,  476,  150,  158,  299,  450,  145,  467,  157,  199,
+ /*   900 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  815,
+ /*   910 */   815,  277,  815,  352,  815,  340,  345,  109,  102,  815,
+ /*   920 */   815,  815,  815,  815,  815,  815,  815,  815,  815,   94,
+ /*   930 */   815,  399,  815,  815,  815,  252,  815,  815,  815,  815,
+ /*   940 */   815,  815,  170,  815,  815,  515,  815,  815,  512,  517,
+ /*   950 */   516,  514,  513,  815,  335,  815,  223,  219,  218,  216,
+ /*   960 */   815,  339,  135,  815,  815,  476,  150,  815,  815,  450,
+ /*   970 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*   980 */   267,  189,  462,  352,  815,  340,  345,  109,  102,  815,
+ /*   990 */   815,  815,  815,  815,  815,  815,  815,  815,  815,   94,
+ /*  1000 */   815,  399,  815,  815,  815,  252,  815,  815,  815,  815,
+ /*  1010 */   815,  815,  170,  815,  815,  515,  815,  815,  512,  517,
+ /*  1020 */   516,  514,  513,  815,  331,  815,  223,  219,  218,  216,
+ /*  1030 */   815,  339,  135,  815,  815,  476,  150,  815,  815,  450,
+ /*  1040 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  1050 */   267,  189,  462,  815,  815,  815,  815,  352,  815,  340,
+ /*  1060 */   345,  109,  102,  815,  815,  815,  815,  815,  815,  815,
+ /*  1070 */   815,  815,  815,   94,  815,  399,  815,  815,  815,  252,
+ /*  1080 */   815,  815,  815,  815,  815,  815,  170,  815,  815,  515,
+ /*  1090 */   815,  815,  512,  517,  516,  514,  513,  815,  330,  815,
+ /*  1100 */   223,  219,  218,  216,  815,  339,  135,  815,  815,  476,
+ /*  1110 */   150,  815,  815,  450,  145,  467,  157,  199,  198,  149,
+ /*  1120 */   191,  272,  271,  269,  267,  189,  462,  352,  815,  340,
+ /*  1130 */   345,  109,  102,  815,  815,  815,  815,  815,  815,  815,
+ /*  1140 */   815,  815,  815,   94,  815,  399,  815,  815,  815,  252,
+ /*  1150 */   815,  815,  815,  815,  815,  815,  170,  815,  815,  515,
+ /*  1160 */   815,  815,  512,  517,  516,  514,  513,  815,  329,  815,
+ /*  1170 */   223,  219,  218,  216,  815,  339,  135,  815,  815,  476,
+ /*  1180 */   150,  815,  815,  450,  145,  467,  157,  199,  198,  149,
+ /*  1190 */   191,  272,  271,  269,  267,  189,  462,  815,  815,  815,
+ /*  1200 */   815,  352,  815,  340,  345,  109,  102,  815,  815,  815,
+ /*  1210 */   815,  815,  815,  815,  815,  815,  815,   94,  815,  399,
+ /*  1220 */   815,  815,  815,  252,  815,  815,  815,  815,  815,  815,
+ /*  1230 */   170,  815,  815,  515,  815,  815,  512,  517,  516,  514,
+ /*  1240 */   513,  815,  328,  815,  223,  219,  218,  216,  815,  339,
+ /*  1250 */   135,  815,  815,  476,  150,  815,  815,  450,  145,  467,
+ /*  1260 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  1270 */   462,  352,  815,  340,  345,  109,  102,  815,  815,  815,
+ /*  1280 */   815,  815,  815,  815,  815,  815,  815,   94,  815,  399,
+ /*  1290 */   815,  815,  815,  252,  815,  815,  815,  815,  815,  815,
+ /*  1300 */   170,  815,  815,  515,  815,  815,  512,  517,  516,  514,
+ /*  1310 */   513,  815,  327,  815,  223,  219,  218,  216,  815,  339,
+ /*  1320 */   135,  815,  815,  476,  150,  815,  815,  450,  145,  467,
+ /*  1330 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  1340 */   462,  815,  815,  815,  815,  352,  815,  340,  345,  109,
+ /*  1350 */   102,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  1360 */   815,   94,  815,  399,  815,  815,  815,  252,  815,  815,
+ /*  1370 */   815,  815,  815,  815,  170,  815,  815,  515,  815,  815,
+ /*  1380 */   512,  517,  516,  514,  513,  815,  326,  815,  223,  219,
+ /*  1390 */   218,  216,  815,  339,  135,  815,  815,  476,  150,  815,
+ /*  1400 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  1410 */   271,  269,  267,  189,  462,  352,  815,  340,  345,  109,
+ /*  1420 */   102,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  1430 */   815,   94,  815,  399,  815,  815,  815,  252,  815,  815,
+ /*  1440 */   815,  815,  815,  815,  170,  815,  815,  515,  815,  815,
+ /*  1450 */   512,  517,  516,  514,  513,  815,  325,  815,  223,  219,
+ /*  1460 */   218,  216,  815,  339,  135,  815,  815,  476,  150,  815,
+ /*  1470 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  1480 */   271,  269,  267,  189,  462,  815,  815,  815,  815,  352,
+ /*  1490 */   815,  340,  345,  109,  102,  815,  815,  815,  815,  815,
+ /*  1500 */   815,  815,  815,  815,  815,   94,  815,  399,  815,  815,
+ /*  1510 */   815,  252,  815,  815,  815,  815,  815,  815,  170,  815,
+ /*  1520 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  1530 */   324,  815,  223,  219,  218,  216,  815,  339,  135,  815,
+ /*  1540 */   815,  476,  150,  815,  815,  450,  145,  467,  157,  199,
+ /*  1550 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  352,
+ /*  1560 */   815,  340,  345,  109,  102,  815,  815,  815,  815,  815,
+ /*  1570 */   815,  815,  815,  815,  815,   94,  815,  399,  815,  815,
+ /*  1580 */   815,  252,  815,  815,  815,  815,  815,  815,  170,  815,
+ /*  1590 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  1600 */   323,  815,  223,  219,  218,  216,  815,  339,  135,  815,
+ /*  1610 */   815,  476,  150,  815,  815,  450,  145,  467,  157,  199,
+ /*  1620 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  815,
+ /*  1630 */   815,  815,  815,  352,  815,  340,  345,  109,  102,  815,
+ /*  1640 */   815,  815,  815,  815,  815,  815,  815,  815,  815,   94,
+ /*  1650 */   815,  399,  815,  815,  815,  252,  815,  815,  815,  815,
+ /*  1660 */   815,  815,  170,  815,  815,  515,  815,  815,  512,  517,
+ /*  1670 */   516,  514,  513,  815,  322,  815,  223,  219,  218,  216,
+ /*  1680 */   815,  339,  135,  815,  815,  476,  150,  815,  815,  450,
+ /*  1690 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  1700 */   267,  189,  462,  352,  815,  340,  345,  109,  102,  815,
+ /*  1710 */   815,  815,  815,  815,  815,  815,  815,  815,  815,   94,
+ /*  1720 */   815,  399,  815,  815,  815,  252,  815,  815,  815,  815,
+ /*  1730 */   815,  815,  170,  525,  815,  515,  815,  815,  512,  517,
+ /*  1740 */   516,  514,  513,  815,  321,  815,  223,  219,  218,  216,
+ /*  1750 */   815,  339,  135,  815,  815,  476,  150,  815,  815,  450,
+ /*  1760 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  1770 */   267,  189,  462,  815,  815,  815,  815,   97,  405,  815,
+ /*  1780 */   205,  298,  815,  815,  815,  407,  815,  815,  815,  410,
+ /*  1790 */   815,  815,  411,  409,  408,  406,  404,  403,  815,  815,
+ /*  1800 */   249,  815,  815,  511,  510,  509,  508,  507,  506,  505,
+ /*  1810 */   504,  503,  502,  501,  500,  499,  498,  497,  496,  295,
+ /*  1820 */   294,  293,  282,  815,  278,   86,   85,  815,  815,  815,
+ /*  1830 */   815,   83,   84,   90,   91,  815,  815,  519,   47,  815,
+ /*  1840 */   815,  815,  815,  815,  143,  815,  815,   86,   85,  815,
+ /*  1850 */   815,  815,  815,   83,   84,   90,   91,  815,  815,  815,
+ /*  1860 */    47,  444,  815,  815,  815,  815,  143,  815,  511,  510,
+ /*  1870 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  1880 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  1890 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  1900 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  1910 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  1920 */   478,   83,   84,   90,   91,  815,  815,  815,   47,  815,
+ /*  1930 */   815,  342,  815,  815,  143,  815,  815,   86,   85,  481,
+ /*  1940 */   480,  479,  478,   83,   84,   90,   91,  815,  815,  815,
+ /*  1950 */    47,  815,  815,   34,  815,  815,  143,  815,  511,  510,
+ /*  1960 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  1970 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  1980 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  1990 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2000 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  2010 */   478,   83,   84,   90,   91,  815,  815,  815,   47,   16,
+ /*  2020 */   815,  815,  815,  815,  143,  815,  815,   86,   85,  481,
+ /*  2030 */   480,  479,  478,   83,   84,   90,   91,  815,  815,  815,
+ /*  2040 */    47,   15,  815,  815,  815,  815,  143,  815,  511,  510,
+ /*  2050 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2060 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  2070 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2080 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2090 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  2100 */   478,   83,   84,   90,   91,  815,  815,  815,   47,  815,
+ /*  2110 */   815,   32,  815,  815,  143,  815,  815,   86,   85,  481,
+ /*  2120 */   480,  479,  478,   83,   84,   90,   91,  815,  815,  815,
+ /*  2130 */    47,   13,  815,  815,  815,  815,  143,  815,  511,  510,
+ /*  2140 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2150 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  2160 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2170 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2180 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  2190 */   478,   83,   84,   90,   91,  815,  815,  815,   47,   11,
+ /*  2200 */   815,  815,  815,  815,  143,  815,  815,   86,   85,  481,
+ /*  2210 */   480,  479,  478,   83,   84,   90,   91,  815,  815,  815,
+ /*  2220 */    47,  815,  815,   28,  815,  815,  143,  815,  511,  510,
+ /*  2230 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2240 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  2250 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2260 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2270 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  2280 */   478,   83,   84,   90,   91,  815,  815,  815,   47,    8,
+ /*  2290 */   815,  815,  815,  815,  143,  815,  815,   86,   85,  481,
+ /*  2300 */   480,  479,  478,   83,   84,   90,   91,  815,  815,  815,
+ /*  2310 */    47,    7,  815,  815,  815,  815,  143,  815,  511,  510,
+ /*  2320 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2330 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  2340 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2350 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2360 */   482,  278,  815,  815,  815,   86,   85,  481,  480,  479,
+ /*  2370 */   478,   83,   84,   90,   91,  815,  815,  815,   47,  815,
+ /*  2380 */   815,  815,  815,  815,  143,  815,  815,  815,  815,  481,
+ /*  2390 */   480,  479,  478,  815,  815,  815,  815,  815,  815,  815,
+ /*  2400 */   815,  815,  815,   26,  815,  815,  815,  815,  511,  510,
+ /*  2410 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2420 */   499,  498,  497,  496,  295,  294,  293,  282,  482,  278,
+ /*  2430 */   815,   86,   85,  815,  121,  815,  815,   83,   84,   90,
+ /*  2440 */    91,  815,  815,   96,   47,  815,  815,  815,  815,  815,
+ /*  2450 */   143,  815,   92,  386,  387,  815,  815,  481,  480,  479,
+ /*  2460 */   478,  515,  815,  815,  512,  517,  516,  514,  513,   25,
+ /*  2470 */   815,  815,  815,  815,  511,  510,  509,  508,  507,  506,
+ /*  2480 */   505,  504,  503,  502,  501,  500,  499,  498,  497,  496,
+ /*  2490 */   295,  294,  293,  282,  482,  278,  815,   86,   85,  815,
+ /*  2500 */   815,  815,  815,   83,   84,   90,   91,  815,  815,  815,
+ /*  2510 */    45,  815,  815,  815,  815,  815,  143,  815,  815,  815,
+ /*  2520 */   815,  815,  815,  481,  480,  479,  478,   59,   55,   54,
+ /*  2530 */    58,   57,   56,   53,   52,   51,   49,   50,  815,  815,
+ /*  2540 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2550 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2560 */   482,  278,  815,  815,  815,  815,  815,  815,   86,   85,
+ /*  2570 */   815,  208,  815,  815,   83,   84,   90,   91,  314,   98,
+ /*  2580 */   815,   47,  815,  815,  815,  815,  815,  143,  815,  481,
+ /*  2590 */   480,  479,  478,  815,  815,  815,  815,  815,  515,  815,
+ /*  2600 */   815,  512,  517,  516,  514,  513,  815,  815,  815,  815,
+ /*  2610 */   815,  511,  510,  509,  508,  507,  506,  505,  504,  503,
+ /*  2620 */   502,  501,  500,  499,  498,  497,  496,  295,  294,  293,
+ /*  2630 */   282,  482,  278,  815,  378,  245,  376,  375,  374,  373,
+ /*  2640 */   815,  815,  815,  815,  815,  815,  178,  238,  815,  237,
+ /*  2650 */   815,  236,  235,  233,  231,  229,  815,  227,  225,  815,
+ /*  2660 */   481,  480,  479,  478,   97,  405,  815,  205,  298,  815,
+ /*  2670 */   815,  815,  407,  815,  815,  815,  410,  815,  815,  411,
+ /*  2680 */   409,  408,  406,  404,  403,  815,  815,  249,  815,  815,
+ /*  2690 */   511,  510,  509,  508,  507,  506,  505,  504,  503,  502,
+ /*  2700 */   501,  500,  499,  498,  497,  496,  295,  294,  293,  282,
+ /*  2710 */   405,  278,  815,  815,  815,  815,  815,  407,  815,  815,
+ /*  2720 */   815,  410,  815,  815,  411,  409,  408,  406,  404,  403,
+ /*  2730 */   815,  815,  815,  815,  815,  511,  510,  509,  508,  507,
+ /*  2740 */   506,  505,  504,  503,  502,  501,  500,  499,  498,  497,
+ /*  2750 */   496,  295,  294,  293,  282,   30,  278,  815,  815,  111,
+ /*  2760 */   102,  815,  815,  815,  815,  815,  815,  815,  815,  121,
+ /*  2770 */   815,   94,  815,  399,  815,  815,  815,  252,   96,  815,
+ /*  2780 */   815,  815,  815,  815,  168,  815,  815,  515,  392,  387,
+ /*  2790 */   512,  517,  516,  514,  513,  815,  515,  815,  815,  512,
+ /*  2800 */   517,  516,  514,  513,  815,  815,  815,  476,  150,  815,
+ /*  2810 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  2820 */   271,  269,  267,  189,  462,  815,  398,  397,  396,  395,
+ /*  2830 */   394,  815,  815,  815,  815,  815,  815,  815,  393,  815,
+ /*  2840 */   815,  815,  511,  510,  509,  508,  507,  506,  505,  504,
+ /*  2850 */   503,  502,  501,  500,  499,  498,  497,  496,  295,  294,
+ /*  2860 */   293,  282,  815,  278,  815,  815,  398,  397,  396,  395,
+ /*  2870 */   394,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  2880 */   815,  815,  511,  510,  509,  508,  507,  506,  505,  504,
+ /*  2890 */   503,  502,  501,  500,  499,  498,  497,  496,  295,  294,
+ /*  2900 */   293,  282,  315,  278,  207,  304,  206,  302,  815,  815,
+ /*  2910 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  2920 */   815,  815,  815,  815,  815,  511,  510,  509,  508,  507,
+ /*  2930 */   506,  505,  504,  503,  502,  501,  500,  499,  498,  497,
+ /*  2940 */   496,  295,  294,  293,  282,  815,  278,  207,  304,  206,
+ /*  2950 */   302,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  2960 */   815,  815,  815,  815,  815,  815,  815,  815,  511,  510,
+ /*  2970 */   509,  508,  507,  506,  505,  504,  503,  502,  501,  500,
+ /*  2980 */   499,  498,  497,  496,  295,  294,  293,  282,  142,  278,
+ /*  2990 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  3000 */   208,  815,  815,  815,  815,  211,  210,  301,   98,  815,
+ /*  3010 */   815,  815,  424,  815,  815,  515,  815,  425,  512,  517,
+ /*  3020 */   516,  514,  513,  815,  815,  815,  815,  515,  815,  142,
+ /*  3030 */   512,  517,  516,  514,  513,  476,  150,  815,  815,  450,
+ /*  3040 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  3050 */   267,  189,  462,  254,  815,  815,  515,  182,  815,  512,
+ /*  3060 */   517,  516,  514,  513,  815,  815,  815,  815,  815,  815,
+ /*  3070 */   142,  815,  815,  815,  815,  815,  476,  150,  815,  815,
+ /*  3080 */   450,  145,  467,  157,  199,  198,  149,  191,  272,  271,
+ /*  3090 */   269,  267,  189,  462,  254,  815,  815,  515,  183,  815,
+ /*  3100 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  3110 */   815,  142,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  3120 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  3130 */   271,  269,  267,  189,  462,  254,  815,  815,  515,  427,
+ /*  3140 */   815,  512,  517,  516,  514,  513,  815,  815,  815,  815,
+ /*  3150 */   815,  815,  127,  815,  815,  815,  815,  815,  476,  150,
+ /*  3160 */   815,  815,  450,  145,  467,  157,  199,  198,  149,  191,
+ /*  3170 */   272,  271,  269,  267,  189,  462,  201,  815,  815,  515,
+ /*  3180 */   815,  815,  512,  517,  516,  514,  513,  815,  815,  815,
+ /*  3190 */   815,  815,  815,  142,  815,  815,  815,  815,  815,  476,
+ /*  3200 */   150,  815,  815,  450,  145,  467,  157,  199,  198,  149,
+ /*  3210 */   191,  272,  271,  269,  267,  189,  462,  200,  815,  815,
+ /*  3220 */   515,  815,  815,  512,  517,  516,  514,  513,  815,  815,
+ /*  3230 */   815,  815,  815,  815,  142,  815,  815,  815,  815,  815,
+ /*  3240 */   476,  150,  815,  815,  450,  145,  467,  157,  199,  198,
+ /*  3250 */   149,  191,  272,  271,  269,  267,  189,  462,  201,  815,
+ /*  3260 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  3270 */   815,  815,  815,  815,  815,  142,  815,  815,  815,  815,
+ /*  3280 */   815,  476,  150,  815,  815,  450,  145,  467,  157,  199,
+ /*  3290 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  175,
+ /*  3300 */   815,  815,  515,  815,  815,  512,  517,  516,  514,  513,
+ /*  3310 */   815,  815,  815,  815,  815,  815,  142,  815,  815,  815,
+ /*  3320 */   815,  815,  476,  150,  815,  815,  450,  145,  467,  157,
+ /*  3330 */   199,  198,  149,  191,  272,  271,  269,  267,  189,  462,
+ /*  3340 */   174,  815,  815,  515,  815,  815,  512,  517,  516,  514,
+ /*  3350 */   513,  815,  815,  815,  815,  815,  815,  142,  815,  815,
+ /*  3360 */   815,  815,  815,  476,  150,  815,  815,  450,  145,  467,
+ /*  3370 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  3380 */   462,  173,  815,  815,  515,  815,  815,  512,  517,  516,
+ /*  3390 */   514,  513,  815,  815,  815,  815,  815,  815,  142,  815,
+ /*  3400 */   815,  815,  815,  815,  476,  150,  815,  815,  450,  145,
+ /*  3410 */   467,  157,  199,  198,  149,  191,  272,  271,  269,  267,
+ /*  3420 */   189,  462,  172,  815,  815,  515,  815,  815,  512,  517,
+ /*  3430 */   516,  514,  513,  815,  815,  815,  815,  815,  815,  142,
+ /*  3440 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  450,
+ /*  3450 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  3460 */   267,  189,  462,  171,  815,  815,  515,  815,  815,  512,
+ /*  3470 */   517,  516,  514,  513,  815,  815,  815,  815,  815,  815,
+ /*  3480 */   142,  815,  815,  815,  815,  815,  476,  150,  815,  815,
+ /*  3490 */   450,  145,  467,  157,  199,  198,  149,  191,  272,  271,
+ /*  3500 */   269,  267,  189,  462,  202,  815,  815,  515,  815,  815,
+ /*  3510 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  3520 */   142,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  3530 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  3540 */   271,  269,  267,  189,  462,  815,  815,  515,  815,  815,
+ /*  3550 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  3560 */   815,  142,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  3570 */   187,  441,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  3580 */   271,  269,  267,  189,  462,  169,  815,  815,  515,  815,
+ /*  3590 */   815,  512,  517,  516,  514,  513,  815,  815,  815,  815,
+ /*  3600 */   815,  815,  142,  815,  815,  815,  815,  815,  476,  150,
+ /*  3610 */   815,  815,  450,  145,  467,  157,  199,  198,  149,  191,
+ /*  3620 */   272,  271,  269,  267,  189,  462,  167,  815,  815,  515,
+ /*  3630 */   815,  815,  512,  517,  516,  514,  513,  815,  815,  815,
+ /*  3640 */   815,  815,  815,  142,  815,  815,  815,  815,  815,  476,
+ /*  3650 */   150,  815,  815,  450,  145,  467,  157,  199,  198,  149,
+ /*  3660 */   191,  272,  271,  269,  267,  189,  462,  166,  815,  815,
+ /*  3670 */   515,  815,  815,  512,  517,  516,  514,  513,  815,  815,
+ /*  3680 */   815,  815,  815,  815,  142,  815,  815,  815,  815,  815,
+ /*  3690 */   476,  150,  815,  815,  450,  145,  467,  157,  199,  198,
+ /*  3700 */   149,  191,  272,  271,  269,  267,  189,  462,  165,  815,
+ /*  3710 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  3720 */   815,  815,  815,  815,  815,  142,  815,  815,  815,  815,
+ /*  3730 */   815,  476,  150,  815,  815,  450,  145,  467,  157,  199,
+ /*  3740 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  163,
+ /*  3750 */   815,  815,  515,  815,  815,  512,  517,  516,  514,  513,
+ /*  3760 */   815,  815,  815,  815,  815,  815,  142,  815,  815,  815,
+ /*  3770 */   815,  815,  476,  150,  815,  815,  450,  145,  467,  157,
+ /*  3780 */   199,  198,  149,  191,  272,  271,  269,  267,  189,  462,
+ /*  3790 */   164,  815,  815,  515,  815,  815,  512,  517,  516,  514,
+ /*  3800 */   513,  815,  815,  815,  815,  815,  815,  142,  815,  815,
+ /*  3810 */   815,  815,  815,  476,  150,  815,  815,  450,  145,  467,
+ /*  3820 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  3830 */   462,  162,  815,  815,  515,  815,  815,  512,  517,  516,
+ /*  3840 */   514,  513,  815,  815,  815,  815,  815,  815,  142,  815,
+ /*  3850 */   815,  815,  815,  815,  476,  150,  815,  815,  450,  145,
+ /*  3860 */   467,  157,  199,  198,  149,  191,  272,  271,  269,  267,
+ /*  3870 */   189,  462,  161,  815,  815,  515,  815,  815,  512,  517,
+ /*  3880 */   516,  514,  513,  815,  815,  815,  815,  815,  815,  142,
+ /*  3890 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  450,
+ /*  3900 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  3910 */   267,  189,  462,  160,  815,  815,  515,  815,  815,  512,
+ /*  3920 */   517,  516,  514,  513,  815,  815,  815,  815,  815,  815,
+ /*  3930 */   142,  815,  815,  815,  815,  815,  476,  150,  815,  815,
+ /*  3940 */   450,  145,  467,  157,  199,  198,  149,  191,  272,  271,
+ /*  3950 */   269,  267,  189,  462,  159,  815,  815,  515,  815,  815,
+ /*  3960 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  3970 */   142,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  3980 */   815,  450,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  3990 */   271,  269,  267,  189,  462,  815,  815,  515,  815,  815,
+ /*  4000 */   512,  517,  516,  514,  513,  815,  815,  815,  142,  815,
+ /*  4010 */   815,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  4020 */   815,  440,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  4030 */   271,  269,  267,  189,  462,  515,  815,  815,  512,  517,
+ /*  4040 */   516,  514,  513,  815,  815,  815,  142,  815,  815,  815,
+ /*  4050 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  461,
+ /*  4060 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  4070 */   267,  189,  462,  515,  815,  815,  512,  517,  516,  514,
+ /*  4080 */   513,  815,  815,  815,  142,  815,  815,  815,  815,  815,
+ /*  4090 */   815,  815,  815,  476,  150,  815,  815,  460,  145,  467,
+ /*  4100 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  4110 */   462,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  4120 */   815,  815,  142,  815,  815,  815,  815,  815,  815,  815,
+ /*  4130 */   815,  476,  150,  815,  815,  459,  145,  467,  157,  199,
+ /*  4140 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  515,
+ /*  4150 */   815,  815,  512,  517,  516,  514,  513,  815,  815,  815,
+ /*  4160 */   142,  815,  815,  815,  815,  815,  815,  815,  815,  476,
+ /*  4170 */   150,  815,  815,  458,  145,  467,  157,  199,  198,  149,
+ /*  4180 */   191,  272,  271,  269,  267,  189,  462,  515,  815,  815,
+ /*  4190 */   512,  517,  516,  514,  513,  815,  815,  815,  142,  815,
+ /*  4200 */   815,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  4210 */   815,  457,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  4220 */   271,  269,  267,  189,  462,  515,  815,  815,  512,  517,
+ /*  4230 */   516,  514,  513,  815,  815,  815,  142,  815,  815,  815,
+ /*  4240 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  456,
+ /*  4250 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  4260 */   267,  189,  462,  515,  815,  815,  512,  517,  516,  514,
+ /*  4270 */   513,  815,  815,  815,  142,  815,  815,  815,  815,  815,
+ /*  4280 */   815,  815,  815,  476,  150,  815,  815,  455,  145,  467,
+ /*  4290 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  4300 */   462,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  4310 */   815,  815,  142,  815,  815,  815,  815,  815,  815,  815,
+ /*  4320 */   815,  476,  150,  815,  815,  454,  145,  467,  157,  199,
+ /*  4330 */   198,  149,  191,  272,  271,  269,  267,  189,  462,  515,
+ /*  4340 */   815,  815,  512,  517,  516,  514,  513,  815,  815,  815,
+ /*  4350 */   142,  815,  815,  815,  815,  815,  815,  815,  815,  476,
+ /*  4360 */   150,  815,  815,  453,  145,  467,  157,  199,  198,  149,
+ /*  4370 */   191,  272,  271,  269,  267,  189,  462,  515,  815,  815,
+ /*  4380 */   512,  517,  516,  514,  513,  815,  815,  815,  142,  815,
+ /*  4390 */   815,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  4400 */   815,  452,  145,  467,  157,  199,  198,  149,  191,  272,
+ /*  4410 */   271,  269,  267,  189,  462,  515,  815,  815,  512,  517,
+ /*  4420 */   516,  514,  513,  815,  815,  815,  142,  815,  815,  815,
+ /*  4430 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  451,
+ /*  4440 */   145,  467,  157,  199,  198,  149,  191,  272,  271,  269,
+ /*  4450 */   267,  189,  462,  515,  815,  815,  512,  517,  516,  514,
+ /*  4460 */   513,  815,  815,  815,  815,  815,   99,  815,  815,  815,
+ /*  4470 */   815,  815,  815,  476,  150,  815,  815,  442,  145,  467,
+ /*  4480 */   157,  199,  198,  149,  191,  272,  271,  269,  267,  189,
+ /*  4490 */   462,  511,  510,  509,  508,  507,  506,  505,  504,  503,
+ /*  4500 */   502,  501,  500,  499,  498,  497,  496,  295,  294,  293,
+ /*  4510 */   282,  815,  278,  815,  205,  815,  815,  815,  815,  815,
+ /*  4520 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  4530 */   142,  815,  815,  815,  815,  815,  815,  511,  510,  509,
+ /*  4540 */   508,  507,  506,  505,  504,  503,  502,  501,  500,  499,
+ /*  4550 */   498,  497,  496,  295,  294,  293,  282,  515,  278,  815,
+ /*  4560 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  4570 */   815,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  4580 */   815,  815,  468,  467,  157,  199,  198,  149,  191,  272,
+ /*  4590 */   271,  269,  267,  189,  463,  815,  815,  815,  815,  815,
+ /*  4600 */   815,  511,  510,  509,  508,  507,  506,  505,  504,  503,
+ /*  4610 */   502,  501,  500,  499,  498,  497,  496,  295,  294,  293,
+ /*  4620 */   282,  142,  278,  815,  814,   66,  297,  523,  128,  815,
+ /*  4630 */   319,  108,  110,  815,   95,  129,  815,  815,  815,  815,
+ /*  4640 */   815,  815,  815,   94,  815,  399,  815,  815,  515,  252,
+ /*  4650 */   815,  512,  517,  516,  514,  513,  815,  815,  815,  515,
+ /*  4660 */   815,  815,  512,  517,  516,  514,  513,  815,  476,  150,
+ /*  4670 */   142,  815,  815,  468,  467,  157,  199,  198,  149,  191,
+ /*  4680 */   272,  271,  269,  267,  188,  815,  815,  815,  815,  815,
+ /*  4690 */   815,  815,  815,  815,  815,  815,  815,  515,  815,  815,
+ /*  4700 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  4710 */   142,  815,  815,  815,  815,  815,  815,  476,  150,  815,
+ /*  4720 */   116,  815,  468,  467,  157,  199,  198,  149,  191,  272,
+ /*  4730 */   271,  269,  265,  412,  815,  815,  815,  515,  815,  815,
+ /*  4740 */   512,  517,  516,  514,  513,  142,  815,  515,  815,  815,
+ /*  4750 */   512,  517,  516,  514,  513,  815,  815,  476,  150,  815,
+ /*  4760 */   815,  815,  468,  467,  157,  199,  198,  149,  191,  272,
+ /*  4770 */   271,  266,  515,  815,  815,  512,  517,  516,  514,  513,
+ /*  4780 */   815,  142,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  4790 */   815,  815,  476,  150,  815,  815,  815,  468,  467,  157,
+ /*  4800 */   199,  198,  149,  191,  272,  268,  815,  815,  515,  815,
+ /*  4810 */   815,  512,  517,  516,  514,  513,  815,  815,  815,  815,
+ /*  4820 */   815,  815,  815,  815,  815,  815,  815,  815,  476,  150,
+ /*  4830 */   815,  815,  815,  468,  467,  157,  199,  198,  149,  191,
+ /*  4840 */   270,  815,  815,  815,  524,  523,  128,  815,  319,  108,
+ /*  4850 */   110,  815,   95,  129,  815,  815,  815,  815,  142,  815,
+ /*  4860 */   815,   94,  815,  399,  815,  815,  815,  252,  815,  815,
+ /*  4870 */   142,  815,  815,  815,  815,  815,  815,  515,  815,  815,
+ /*  4880 */   512,  517,  516,  514,  513,  515,  815,  815,  512,  517,
+ /*  4890 */   516,  514,  513,  815,  815,  815,  815,  515,  815,  815,
+ /*  4900 */   512,  517,  516,  514,  513,  476,  150,  815,  142,  815,
+ /*  4910 */   468,  467,  157,  199,  198,  149,  190,  476,  150,  815,
+ /*  4920 */   815,  815,  468,  467,  157,  199,  198,  148,  815,  815,
+ /*  4930 */   815,  815,  815,  815,  815,  515,  815,  815,  512,  517,
+ /*  4940 */   516,  514,  513,  815,  142,  815,  815,  815,  815,  815,
+ /*  4950 */   815,  815,  815,  815,  815,  476,  150,  815,  815,  815,
+ /*  4960 */   468,  467,  157,  199,  198,  147,  815,  815,  815,  815,
+ /*  4970 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  142,
+ /*  4980 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  4990 */   815,  476,  150,  815,  815,  815,  468,  467,  157,  199,
+ /*  5000 */   195,  815,  815,  815,  815,  815,  515,  815,  815,  512,
+ /*  5010 */   517,  516,  514,  513,  142,  815,  815,  815,  815,  815,
+ /*  5020 */   815,  815,  815,  815,  142,  815,  476,  150,  815,  815,
+ /*  5030 */   815,  468,  467,  157,  199,  194,  815,  815,  815,  815,
+ /*  5040 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  5050 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  142,
+ /*  5060 */   815,  476,  150,  815,  815,  815,  468,  467,  157,  199,
+ /*  5070 */   193,  476,  150,  815,  815,  815,  468,  467,  157,  199,
+ /*  5080 */   192,  815,  815,  815,  815,  815,  515,  815,  142,  512,
+ /*  5090 */   517,  516,  514,  513,  815,  815,  815,  142,  815,  815,
+ /*  5100 */   815,  815,  815,  815,  815,  815,  476,  150,  815,  815,
+ /*  5110 */   815,  468,  467,  157,  197,  515,  815,  815,  512,  517,
+ /*  5120 */   516,  514,  513,  815,  515,  142,  815,  512,  517,  516,
+ /*  5130 */   514,  513,  815,  815,  815,  476,  150,  815,  815,  815,
+ /*  5140 */   468,  467,  157,  196,  476,  150,  815,  815,  815,  468,
+ /*  5150 */   467,  156,  515,  815,  815,  512,  517,  516,  514,  513,
+ /*  5160 */   208,  815,  815,  815,  815,  203,  210,  301,   98,  815,
+ /*  5170 */   815,  815,  476,  150,  142,  815,  815,  468,  467,  155,
+ /*  5180 */   815,  815,  815,  815,  142,  815,  815,  515,  815,  815,
+ /*  5190 */   512,  517,  516,  514,  513,  815,  815,  815,  815,  815,
+ /*  5200 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  5210 */   815,  515,  142,  815,  512,  517,  516,  514,  513,  815,
+ /*  5220 */   815,  476,  150,  142,  815,  815,  468,  472,  815,  815,
+ /*  5230 */   815,  476,  150,  815,  815,  815,  468,  471,  815,  515,
+ /*  5240 */   815,  815,  512,  517,  516,  514,  513,  142,  815,  815,
+ /*  5250 */   515,  815,  815,  512,  517,  516,  514,  513,  142,  476,
+ /*  5260 */   150,  815,  815,  815,  468,  470,  815,  815,  815,  815,
+ /*  5270 */   476,  150,  815,  815,  515,  468,  469,  512,  517,  516,
+ /*  5280 */   514,  513,  142,  815,  815,  515,  815,  815,  512,  517,
+ /*  5290 */   516,  514,  513,  142,  476,  150,  815,  815,  815,  468,
+ /*  5300 */   466,  815,  815,  815,  815,  476,  150,  815,  815,  515,
+ /*  5310 */   468,  465,  512,  517,  516,  514,  513,  142,  815,  815,
+ /*  5320 */   515,  815,  815,  512,  517,  516,  514,  513,  815,  476,
+ /*  5330 */   150,  815,  815,  815,  468,  464,  815,  815,  815,  815,
+ /*  5340 */   476,  150,  815,  815,  515,  468,  445,  512,  517,  516,
+ /*  5350 */   514,  513,  815,  815,  142,  815,  815,  815,  815,  815,
+ /*  5360 */   815,  815,  815,  815,  476,  150,  815,  815,  815,  449,
+ /*  5370 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  5380 */   815,  515,  815,  815,  512,  517,  516,  514,  513,  815,
+ /*  5390 */   815,  815,  815,  815,  815,  815,  815,  815,  815,  815,
+ /*  5400 */   815,  476,  150,  815,  815,  815,  473,
+};
+static const YYCODETYPE yy_lookahead[] = {
+ /*     0 */    27,   28,   35,   36,   37,   38,   33,   34,   35,   36,
+ /*    10 */    38,   38,   40,   40,    2,  142,   43,   44,   45,   46,
+ /*    20 */   147,  148,   29,   30,   31,   52,   66,  154,  155,   56,
+ /*    30 */    27,   28,   59,   60,   61,   62,   63,   64,   65,   66,
+ /*    40 */    67,   83,    1,   70,   71,   72,   73,   74,   75,   76,
+ /*    50 */    77,   78,   79,   80,   81,   82,   83,   84,   85,   86,
+ /*    60 */    87,   88,   89,   90,   91,   53,   54,   55,   56,   57,
+ /*    70 */    58,   98,   99,  100,  101,  102,  103,  117,  118,  106,
+ /*    80 */    39,  108,   21,   22,   23,   24,   38,  114,   83,  133,
+ /*    90 */    27,   28,  119,  120,  121,  122,   33,   34,   35,   36,
+ /*   100 */   144,   38,  146,   40,    2,  142,   43,   44,   45,   46,
+ /*   110 */   108,  148,   19,   20,  148,   52,  114,  154,  155,   56,
+ /*   120 */   154,  155,   59,   60,   61,   62,   63,   64,   65,   66,
+ /*   130 */    67,   25,   26,   70,   71,   72,   73,   74,   75,   76,
+ /*   140 */    77,   78,   79,   80,   81,   82,   83,   84,   85,   86,
+ /*   150 */    87,   88,   89,   90,   91,   21,   54,   55,   56,   57,
+ /*   160 */    58,   98,   99,  100,  101,  102,  103,  101,  102,  106,
+ /*   170 */   136,  108,  106,  101,  102,  141,  142,  114,  106,  133,
+ /*   180 */    27,   28,  119,  120,  121,  122,   33,   34,   35,   36,
+ /*   190 */   144,   38,  146,   40,  142,  148,   43,   44,   45,   46,
+ /*   200 */   148,  154,  155,   23,  148,   52,  154,  155,  133,   56,
+ /*   210 */   154,  155,   59,   60,   61,   62,   63,   64,   65,  144,
+ /*   220 */    67,  146,    1,   70,   71,   72,   73,   74,   75,   76,
+ /*   230 */    77,   78,   79,   80,   81,   82,   83,   84,   85,   86,
+ /*   240 */    87,   88,   89,   90,   91,   14,   46,  157,  158,  159,
+ /*   250 */    23,   98,   99,  100,  101,  102,  103,  148,    1,  106,
+ /*   260 */   132,  108,   41,  154,  155,   13,   14,  114,   68,   69,
+ /*   270 */    27,   28,  119,  120,  121,  122,   33,   34,   35,   36,
+ /*   280 */   152,   37,    1,   40,   53,   41,   43,  159,   45,   46,
+ /*   290 */   162,  163,  164,  165,  166,   52,  101,  102,  133,   56,
+ /*   300 */    43,  106,   59,   60,   61,   62,   63,   64,    1,  144,
+ /*   310 */    67,  146,    1,   70,   71,   72,   73,   74,   75,   76,
+ /*   320 */    77,   78,   79,   80,   81,   82,   83,   84,   85,   86,
+ /*   330 */    87,   88,   89,   90,   91,   68,   69,  127,  136,  129,
+ /*   340 */   130,  131,  132,  141,  142,    1,  181,   66,   41,    1,
+ /*   350 */    39,   39,   40,  143,  108,  145,   23,   23,   43,  149,
+ /*   360 */   114,   46,  119,  120,  121,  122,  156,    1,    1,  159,
+ /*   370 */    40,   41,  162,  163,  164,  165,  166,  167,  168,    1,
+ /*   380 */   170,  171,  172,  173,    1,  175,  176,   43,    1,  179,
+ /*   390 */   180,   43,    1,  183,  184,  185,  186,  187,  188,  189,
+ /*   400 */   190,  191,  192,  193,  194,  195,  196,  127,   41,  129,
+ /*   410 */   130,  131,  132,   79,   80,   81,   82,   83,   84,   85,
+ /*   420 */    86,   87,    1,  143,  133,  145,   43,   43,   41,  149,
+ /*   430 */    23,   53,   66,   23,   43,   40,  156,  146,    1,  159,
+ /*   440 */     1,   46,  162,  163,  164,  165,  166,  167,  168,   65,
+ /*   450 */   170,  171,  172,  173,   43,  175,  176,   46,    2,  179,
+ /*   460 */   180,    2,   41,  183,  184,  185,  186,  187,  188,  189,
+ /*   470 */   190,  191,  192,  193,  194,  195,  196,   21,   41,   83,
+ /*   480 */    21,  127,   43,  129,  130,  131,  132,   79,   80,   81,
+ /*   490 */    82,   83,   84,   85,   86,   87,  136,  143,   23,  145,
+ /*   500 */   174,   23,  142,  149,  178,  117,  118,  147,  148,   53,
+ /*   510 */   156,   83,   53,  159,  154,  155,  162,  163,  164,  165,
+ /*   520 */   166,  167,  168,   23,  170,  171,  172,  173,    1,  175,
+ /*   530 */   176,    1,  132,  179,  180,  135,   21,  183,  184,  185,
+ /*   540 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*   550 */   196,  127,  133,  129,  130,  131,  132,    1,    1,  159,
+ /*   560 */     1,    1,  162,  163,  164,  165,  166,  143,   23,  145,
+ /*   570 */    43,   41,  153,  149,   79,   80,   81,   82,   83,   84,
+ /*   580 */   156,   86,   87,  159,    1,   21,  162,  163,  164,  165,
+ /*   590 */   166,    1,  168,    1,  170,  171,  172,  173,   41,  175,
+ /*   600 */   176,   41,   43,  179,  180,  132,    1,  183,  184,  185,
+ /*   610 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*   620 */   196,    2,  141,  142,   90,  127,   43,  129,  130,  131,
+ /*   630 */   132,   41,  159,   41,    1,  162,  163,  164,  165,  166,
+ /*   640 */     1,  143,   90,  145,  141,  142,    1,  149,   43,  158,
+ /*   650 */   159,   90,   23,   18,  156,   17,   16,  159,   15,   46,
+ /*   660 */   162,  163,  164,  165,  166,   40,  168,   46,  170,  171,
+ /*   670 */   172,  173,   53,  175,  176,  132,   46,  179,  180,   69,
+ /*   680 */    41,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*   690 */   192,  193,  194,  195,  196,  127,   41,  129,  130,  131,
+ /*   700 */   132,   40,  159,   46,   41,  162,  163,  164,  165,  166,
+ /*   710 */     1,  143,   68,  145,   43,   65,   46,  149,   46,   46,
+ /*   720 */    43,   43,   43,   40,  156,   41,   90,  159,   39,   41,
+ /*   730 */   162,  163,  164,  165,  166,   39,  168,   90,  170,  171,
+ /*   740 */   172,  173,   39,  175,  176,   39,   39,  179,  180,  132,
+ /*   750 */    39,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*   760 */   192,  193,  194,  195,  196,   39,  108,  108,   39,  127,
+ /*   770 */   108,  129,  130,  131,  132,   39,  159,  108,   39,  162,
+ /*   780 */   163,  164,  165,  166,   39,  143,  101,  145,  114,  114,
+ /*   790 */    40,  149,  102,   40,   43,   40,   42,   40,  156,   65,
+ /*   800 */    53,  159,   46,   40,  162,  163,  164,  165,  166,   41,
+ /*   810 */   168,    1,  170,  171,  172,  173,   46,  175,  176,   46,
+ /*   820 */    51,  179,  180,   49,   46,  183,  184,  185,  186,  187,
+ /*   830 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  127,
+ /*   840 */    40,  129,  130,  131,  132,   41,  133,   46,  181,   40,
+ /*   850 */   133,   53,  155,  154,    2,  143,   53,  145,  142,  136,
+ /*   860 */    53,  149,  181,  177,  129,   40,  136,   65,  156,  142,
+ /*   870 */   142,  159,  129,  142,  162,  163,  164,  165,  166,  142,
+ /*   880 */   168,  159,  170,  171,  172,  173,  178,  175,  176,  169,
+ /*   890 */    53,  179,  180,   46,  136,  183,  184,  185,  186,  187,
+ /*   900 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  197,
+ /*   910 */   197,  159,  197,  127,  197,  129,  130,  131,  132,  197,
+ /*   920 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  143,
+ /*   930 */   197,  145,  197,  197,  197,  149,  197,  197,  197,  197,
+ /*   940 */   197,  197,  156,  197,  197,  159,  197,  197,  162,  163,
+ /*   950 */   164,  165,  166,  197,  168,  197,  170,  171,  172,  173,
+ /*   960 */   197,  175,  176,  197,  197,  179,  180,  197,  197,  183,
+ /*   970 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*   980 */   194,  195,  196,  127,  197,  129,  130,  131,  132,  197,
+ /*   990 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  143,
+ /*  1000 */   197,  145,  197,  197,  197,  149,  197,  197,  197,  197,
+ /*  1010 */   197,  197,  156,  197,  197,  159,  197,  197,  162,  163,
+ /*  1020 */   164,  165,  166,  197,  168,  197,  170,  171,  172,  173,
+ /*  1030 */   197,  175,  176,  197,  197,  179,  180,  197,  197,  183,
+ /*  1040 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  1050 */   194,  195,  196,  197,  197,  197,  197,  127,  197,  129,
+ /*  1060 */   130,  131,  132,  197,  197,  197,  197,  197,  197,  197,
+ /*  1070 */   197,  197,  197,  143,  197,  145,  197,  197,  197,  149,
+ /*  1080 */   197,  197,  197,  197,  197,  197,  156,  197,  197,  159,
+ /*  1090 */   197,  197,  162,  163,  164,  165,  166,  197,  168,  197,
+ /*  1100 */   170,  171,  172,  173,  197,  175,  176,  197,  197,  179,
+ /*  1110 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  1120 */   190,  191,  192,  193,  194,  195,  196,  127,  197,  129,
+ /*  1130 */   130,  131,  132,  197,  197,  197,  197,  197,  197,  197,
+ /*  1140 */   197,  197,  197,  143,  197,  145,  197,  197,  197,  149,
+ /*  1150 */   197,  197,  197,  197,  197,  197,  156,  197,  197,  159,
+ /*  1160 */   197,  197,  162,  163,  164,  165,  166,  197,  168,  197,
+ /*  1170 */   170,  171,  172,  173,  197,  175,  176,  197,  197,  179,
+ /*  1180 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  1190 */   190,  191,  192,  193,  194,  195,  196,  197,  197,  197,
+ /*  1200 */   197,  127,  197,  129,  130,  131,  132,  197,  197,  197,
+ /*  1210 */   197,  197,  197,  197,  197,  197,  197,  143,  197,  145,
+ /*  1220 */   197,  197,  197,  149,  197,  197,  197,  197,  197,  197,
+ /*  1230 */   156,  197,  197,  159,  197,  197,  162,  163,  164,  165,
+ /*  1240 */   166,  197,  168,  197,  170,  171,  172,  173,  197,  175,
+ /*  1250 */   176,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  1260 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  1270 */   196,  127,  197,  129,  130,  131,  132,  197,  197,  197,
+ /*  1280 */   197,  197,  197,  197,  197,  197,  197,  143,  197,  145,
+ /*  1290 */   197,  197,  197,  149,  197,  197,  197,  197,  197,  197,
+ /*  1300 */   156,  197,  197,  159,  197,  197,  162,  163,  164,  165,
+ /*  1310 */   166,  197,  168,  197,  170,  171,  172,  173,  197,  175,
+ /*  1320 */   176,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  1330 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  1340 */   196,  197,  197,  197,  197,  127,  197,  129,  130,  131,
+ /*  1350 */   132,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  1360 */   197,  143,  197,  145,  197,  197,  197,  149,  197,  197,
+ /*  1370 */   197,  197,  197,  197,  156,  197,  197,  159,  197,  197,
+ /*  1380 */   162,  163,  164,  165,  166,  197,  168,  197,  170,  171,
+ /*  1390 */   172,  173,  197,  175,  176,  197,  197,  179,  180,  197,
+ /*  1400 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  1410 */   192,  193,  194,  195,  196,  127,  197,  129,  130,  131,
+ /*  1420 */   132,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  1430 */   197,  143,  197,  145,  197,  197,  197,  149,  197,  197,
+ /*  1440 */   197,  197,  197,  197,  156,  197,  197,  159,  197,  197,
+ /*  1450 */   162,  163,  164,  165,  166,  197,  168,  197,  170,  171,
+ /*  1460 */   172,  173,  197,  175,  176,  197,  197,  179,  180,  197,
+ /*  1470 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  1480 */   192,  193,  194,  195,  196,  197,  197,  197,  197,  127,
+ /*  1490 */   197,  129,  130,  131,  132,  197,  197,  197,  197,  197,
+ /*  1500 */   197,  197,  197,  197,  197,  143,  197,  145,  197,  197,
+ /*  1510 */   197,  149,  197,  197,  197,  197,  197,  197,  156,  197,
+ /*  1520 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  1530 */   168,  197,  170,  171,  172,  173,  197,  175,  176,  197,
+ /*  1540 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  1550 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  127,
+ /*  1560 */   197,  129,  130,  131,  132,  197,  197,  197,  197,  197,
+ /*  1570 */   197,  197,  197,  197,  197,  143,  197,  145,  197,  197,
+ /*  1580 */   197,  149,  197,  197,  197,  197,  197,  197,  156,  197,
+ /*  1590 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  1600 */   168,  197,  170,  171,  172,  173,  197,  175,  176,  197,
+ /*  1610 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  1620 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  197,
+ /*  1630 */   197,  197,  197,  127,  197,  129,  130,  131,  132,  197,
+ /*  1640 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  143,
+ /*  1650 */   197,  145,  197,  197,  197,  149,  197,  197,  197,  197,
+ /*  1660 */   197,  197,  156,  197,  197,  159,  197,  197,  162,  163,
+ /*  1670 */   164,  165,  166,  197,  168,  197,  170,  171,  172,  173,
+ /*  1680 */   197,  175,  176,  197,  197,  179,  180,  197,  197,  183,
+ /*  1690 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  1700 */   194,  195,  196,  127,  197,  129,  130,  131,  132,  197,
+ /*  1710 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  143,
+ /*  1720 */   197,  145,  197,  197,  197,  149,  197,  197,  197,  197,
+ /*  1730 */   197,  197,  156,    0,  197,  159,  197,  197,  162,  163,
+ /*  1740 */   164,  165,  166,  197,  168,  197,  170,  171,  172,  173,
+ /*  1750 */   197,  175,  176,  197,  197,  179,  180,  197,  197,  183,
+ /*  1760 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  1770 */   194,  195,  196,  197,  197,  197,  197,   44,   45,  197,
+ /*  1780 */    47,   48,  197,  197,  197,   52,  197,  197,  197,   56,
+ /*  1790 */   197,  197,   59,   60,   61,   62,   63,   64,  197,  197,
+ /*  1800 */    67,  197,  197,   70,   71,   72,   73,   74,   75,   76,
+ /*  1810 */    77,   78,   79,   80,   81,   82,   83,   84,   85,   86,
+ /*  1820 */    87,   88,   89,  197,   91,   27,   28,  197,  197,  197,
+ /*  1830 */   197,   33,   34,   35,   36,  197,  197,   39,   40,  197,
+ /*  1840 */   197,  197,  197,  197,   46,  197,  197,   27,   28,  197,
+ /*  1850 */   197,  197,  197,   33,   34,   35,   36,  197,  197,  197,
+ /*  1860 */    40,   41,  197,  197,  197,  197,   46,  197,   70,   71,
+ /*  1870 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  1880 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  1890 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  1900 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  1910 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  1920 */   122,   33,   34,   35,   36,  197,  197,  197,   40,  197,
+ /*  1930 */   197,   43,  197,  197,   46,  197,  197,   27,   28,  119,
+ /*  1940 */   120,  121,  122,   33,   34,   35,   36,  197,  197,  197,
+ /*  1950 */    40,  197,  197,   43,  197,  197,   46,  197,   70,   71,
+ /*  1960 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  1970 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  1980 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  1990 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2000 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  2010 */   122,   33,   34,   35,   36,  197,  197,  197,   40,   41,
+ /*  2020 */   197,  197,  197,  197,   46,  197,  197,   27,   28,  119,
+ /*  2030 */   120,  121,  122,   33,   34,   35,   36,  197,  197,  197,
+ /*  2040 */    40,   41,  197,  197,  197,  197,   46,  197,   70,   71,
+ /*  2050 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2060 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  2070 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2080 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2090 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  2100 */   122,   33,   34,   35,   36,  197,  197,  197,   40,  197,
+ /*  2110 */   197,   43,  197,  197,   46,  197,  197,   27,   28,  119,
+ /*  2120 */   120,  121,  122,   33,   34,   35,   36,  197,  197,  197,
+ /*  2130 */    40,   41,  197,  197,  197,  197,   46,  197,   70,   71,
+ /*  2140 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2150 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  2160 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2170 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2180 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  2190 */   122,   33,   34,   35,   36,  197,  197,  197,   40,   41,
+ /*  2200 */   197,  197,  197,  197,   46,  197,  197,   27,   28,  119,
+ /*  2210 */   120,  121,  122,   33,   34,   35,   36,  197,  197,  197,
+ /*  2220 */    40,  197,  197,   43,  197,  197,   46,  197,   70,   71,
+ /*  2230 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2240 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  2250 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2260 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2270 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  2280 */   122,   33,   34,   35,   36,  197,  197,  197,   40,   41,
+ /*  2290 */   197,  197,  197,  197,   46,  197,  197,   27,   28,  119,
+ /*  2300 */   120,  121,  122,   33,   34,   35,   36,  197,  197,  197,
+ /*  2310 */    40,   41,  197,  197,  197,  197,   46,  197,   70,   71,
+ /*  2320 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2330 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  2340 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2350 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2360 */    90,   91,  197,  197,  197,   27,   28,  119,  120,  121,
+ /*  2370 */   122,   33,   34,   35,   36,  197,  197,  197,   40,  197,
+ /*  2380 */   197,  197,  197,  197,   46,  197,  197,  197,  197,  119,
+ /*  2390 */   120,  121,  122,  197,  197,  197,  197,  197,  197,  197,
+ /*  2400 */   197,  197,  197,   65,  197,  197,  197,  197,   70,   71,
+ /*  2410 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2420 */    82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
+ /*  2430 */   197,   27,   28,  197,  132,  197,  197,   33,   34,   35,
+ /*  2440 */    36,  197,  197,  141,   40,  197,  197,  197,  197,  197,
+ /*  2450 */    46,  197,  150,  151,  152,  197,  197,  119,  120,  121,
+ /*  2460 */   122,  159,  197,  197,  162,  163,  164,  165,  166,   65,
+ /*  2470 */   197,  197,  197,  197,   70,   71,   72,   73,   74,   75,
+ /*  2480 */    76,   77,   78,   79,   80,   81,   82,   83,   84,   85,
+ /*  2490 */    86,   87,   88,   89,   90,   91,  197,   27,   28,  197,
+ /*  2500 */   197,  197,  197,   33,   34,   35,   36,  197,  197,  197,
+ /*  2510 */    40,  197,  197,  197,  197,  197,   46,  197,  197,  197,
+ /*  2520 */   197,  197,  197,  119,  120,  121,  122,    2,    3,    4,
+ /*  2530 */     5,    6,    7,    8,    9,   10,   11,   12,  197,  197,
+ /*  2540 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2550 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2560 */    90,   91,  197,  197,  197,  197,  197,  197,   27,   28,
+ /*  2570 */   197,  132,  197,  197,   33,   34,   35,   36,  139,  140,
+ /*  2580 */   197,   40,  197,  197,  197,  197,  197,   46,  197,  119,
+ /*  2590 */   120,  121,  122,  197,  197,  197,  197,  197,  159,  197,
+ /*  2600 */   197,  162,  163,  164,  165,  166,  197,  197,  197,  197,
+ /*  2610 */   197,   70,   71,   72,   73,   74,   75,   76,   77,   78,
+ /*  2620 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
+ /*  2630 */    89,   90,   91,  197,   92,   93,   94,   95,   96,   97,
+ /*  2640 */   197,  197,  197,  197,  197,  197,  104,  105,  197,  107,
+ /*  2650 */   197,  109,  110,  111,  112,  113,  197,  115,  116,  197,
+ /*  2660 */   119,  120,  121,  122,   44,   45,  197,   47,   48,  197,
+ /*  2670 */   197,  197,   52,  197,  197,  197,   56,  197,  197,   59,
+ /*  2680 */    60,   61,   62,   63,   64,  197,  197,   67,  197,  197,
+ /*  2690 */    70,   71,   72,   73,   74,   75,   76,   77,   78,   79,
+ /*  2700 */    80,   81,   82,   83,   84,   85,   86,   87,   88,   89,
+ /*  2710 */    45,   91,  197,  197,  197,  197,  197,   52,  197,  197,
+ /*  2720 */   197,   56,  197,  197,   59,   60,   61,   62,   63,   64,
+ /*  2730 */   197,  197,  197,  197,  197,   70,   71,   72,   73,   74,
+ /*  2740 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   84,
+ /*  2750 */    85,   86,   87,   88,   89,  127,   91,  197,  197,  131,
+ /*  2760 */   132,  197,  197,  197,  197,  197,  197,  197,  197,  132,
+ /*  2770 */   197,  143,  197,  145,  197,  197,  197,  149,  141,  197,
+ /*  2780 */   197,  197,  197,  197,  156,  197,  197,  159,  151,  152,
+ /*  2790 */   162,  163,  164,  165,  166,  197,  159,  197,  197,  162,
+ /*  2800 */   163,  164,  165,  166,  197,  197,  197,  179,  180,  197,
+ /*  2810 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  2820 */   192,  193,  194,  195,  196,  197,   54,   55,   56,   57,
+ /*  2830 */    58,  197,  197,  197,  197,  197,  197,  197,   66,  197,
+ /*  2840 */   197,  197,   70,   71,   72,   73,   74,   75,   76,   77,
+ /*  2850 */    78,   79,   80,   81,   82,   83,   84,   85,   86,   87,
+ /*  2860 */    88,   89,  197,   91,  197,  197,   54,   55,   56,   57,
+ /*  2870 */    58,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  2880 */   197,  197,   70,   71,   72,   73,   74,   75,   76,   77,
+ /*  2890 */    78,   79,   80,   81,   82,   83,   84,   85,   86,   87,
+ /*  2900 */    88,   89,   47,   91,   49,   50,   51,   52,  197,  197,
+ /*  2910 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  2920 */   197,  197,  197,  197,  197,   70,   71,   72,   73,   74,
+ /*  2930 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   84,
+ /*  2940 */    85,   86,   87,   88,   89,  197,   91,   49,   50,   51,
+ /*  2950 */    52,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  2960 */   197,  197,  197,  197,  197,  197,  197,  197,   70,   71,
+ /*  2970 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
+ /*  2980 */    82,   83,   84,   85,   86,   87,   88,   89,  132,   91,
+ /*  2990 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  3000 */   132,  197,  197,  197,  197,  137,  138,  139,  140,  197,
+ /*  3010 */   197,  197,  156,  197,  197,  159,  197,  161,  162,  163,
+ /*  3020 */   164,  165,  166,  197,  197,  197,  197,  159,  197,  132,
+ /*  3030 */   162,  163,  164,  165,  166,  179,  180,  197,  197,  183,
+ /*  3040 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  3050 */   194,  195,  196,  156,  197,  197,  159,  160,  197,  162,
+ /*  3060 */   163,  164,  165,  166,  197,  197,  197,  197,  197,  197,
+ /*  3070 */   132,  197,  197,  197,  197,  197,  179,  180,  197,  197,
+ /*  3080 */   183,  184,  185,  186,  187,  188,  189,  190,  191,  192,
+ /*  3090 */   193,  194,  195,  196,  156,  197,  197,  159,  160,  197,
+ /*  3100 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  3110 */   197,  132,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  3120 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  3130 */   192,  193,  194,  195,  196,  156,  197,  197,  159,  160,
+ /*  3140 */   197,  162,  163,  164,  165,  166,  197,  197,  197,  197,
+ /*  3150 */   197,  197,  132,  197,  197,  197,  197,  197,  179,  180,
+ /*  3160 */   197,  197,  183,  184,  185,  186,  187,  188,  189,  190,
+ /*  3170 */   191,  192,  193,  194,  195,  196,  156,  197,  197,  159,
+ /*  3180 */   197,  197,  162,  163,  164,  165,  166,  197,  197,  197,
+ /*  3190 */   197,  197,  197,  132,  197,  197,  197,  197,  197,  179,
+ /*  3200 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  3210 */   190,  191,  192,  193,  194,  195,  196,  156,  197,  197,
+ /*  3220 */   159,  197,  197,  162,  163,  164,  165,  166,  197,  197,
+ /*  3230 */   197,  197,  197,  197,  132,  197,  197,  197,  197,  197,
+ /*  3240 */   179,  180,  197,  197,  183,  184,  185,  186,  187,  188,
+ /*  3250 */   189,  190,  191,  192,  193,  194,  195,  196,  156,  197,
+ /*  3260 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  3270 */   197,  197,  197,  197,  197,  132,  197,  197,  197,  197,
+ /*  3280 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  3290 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  156,
+ /*  3300 */   197,  197,  159,  197,  197,  162,  163,  164,  165,  166,
+ /*  3310 */   197,  197,  197,  197,  197,  197,  132,  197,  197,  197,
+ /*  3320 */   197,  197,  179,  180,  197,  197,  183,  184,  185,  186,
+ /*  3330 */   187,  188,  189,  190,  191,  192,  193,  194,  195,  196,
+ /*  3340 */   156,  197,  197,  159,  197,  197,  162,  163,  164,  165,
+ /*  3350 */   166,  197,  197,  197,  197,  197,  197,  132,  197,  197,
+ /*  3360 */   197,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  3370 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  3380 */   196,  156,  197,  197,  159,  197,  197,  162,  163,  164,
+ /*  3390 */   165,  166,  197,  197,  197,  197,  197,  197,  132,  197,
+ /*  3400 */   197,  197,  197,  197,  179,  180,  197,  197,  183,  184,
+ /*  3410 */   185,  186,  187,  188,  189,  190,  191,  192,  193,  194,
+ /*  3420 */   195,  196,  156,  197,  197,  159,  197,  197,  162,  163,
+ /*  3430 */   164,  165,  166,  197,  197,  197,  197,  197,  197,  132,
+ /*  3440 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  183,
+ /*  3450 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  3460 */   194,  195,  196,  156,  197,  197,  159,  197,  197,  162,
+ /*  3470 */   163,  164,  165,  166,  197,  197,  197,  197,  197,  197,
+ /*  3480 */   132,  197,  197,  197,  197,  197,  179,  180,  197,  197,
+ /*  3490 */   183,  184,  185,  186,  187,  188,  189,  190,  191,  192,
+ /*  3500 */   193,  194,  195,  196,  156,  197,  197,  159,  197,  197,
+ /*  3510 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  3520 */   132,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  3530 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  3540 */   192,  193,  194,  195,  196,  197,  197,  159,  197,  197,
+ /*  3550 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  3560 */   197,  132,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  3570 */   182,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  3580 */   192,  193,  194,  195,  196,  156,  197,  197,  159,  197,
+ /*  3590 */   197,  162,  163,  164,  165,  166,  197,  197,  197,  197,
+ /*  3600 */   197,  197,  132,  197,  197,  197,  197,  197,  179,  180,
+ /*  3610 */   197,  197,  183,  184,  185,  186,  187,  188,  189,  190,
+ /*  3620 */   191,  192,  193,  194,  195,  196,  156,  197,  197,  159,
+ /*  3630 */   197,  197,  162,  163,  164,  165,  166,  197,  197,  197,
+ /*  3640 */   197,  197,  197,  132,  197,  197,  197,  197,  197,  179,
+ /*  3650 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  3660 */   190,  191,  192,  193,  194,  195,  196,  156,  197,  197,
+ /*  3670 */   159,  197,  197,  162,  163,  164,  165,  166,  197,  197,
+ /*  3680 */   197,  197,  197,  197,  132,  197,  197,  197,  197,  197,
+ /*  3690 */   179,  180,  197,  197,  183,  184,  185,  186,  187,  188,
+ /*  3700 */   189,  190,  191,  192,  193,  194,  195,  196,  156,  197,
+ /*  3710 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  3720 */   197,  197,  197,  197,  197,  132,  197,  197,  197,  197,
+ /*  3730 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  3740 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  156,
+ /*  3750 */   197,  197,  159,  197,  197,  162,  163,  164,  165,  166,
+ /*  3760 */   197,  197,  197,  197,  197,  197,  132,  197,  197,  197,
+ /*  3770 */   197,  197,  179,  180,  197,  197,  183,  184,  185,  186,
+ /*  3780 */   187,  188,  189,  190,  191,  192,  193,  194,  195,  196,
+ /*  3790 */   156,  197,  197,  159,  197,  197,  162,  163,  164,  165,
+ /*  3800 */   166,  197,  197,  197,  197,  197,  197,  132,  197,  197,
+ /*  3810 */   197,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  3820 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  3830 */   196,  156,  197,  197,  159,  197,  197,  162,  163,  164,
+ /*  3840 */   165,  166,  197,  197,  197,  197,  197,  197,  132,  197,
+ /*  3850 */   197,  197,  197,  197,  179,  180,  197,  197,  183,  184,
+ /*  3860 */   185,  186,  187,  188,  189,  190,  191,  192,  193,  194,
+ /*  3870 */   195,  196,  156,  197,  197,  159,  197,  197,  162,  163,
+ /*  3880 */   164,  165,  166,  197,  197,  197,  197,  197,  197,  132,
+ /*  3890 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  183,
+ /*  3900 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  3910 */   194,  195,  196,  156,  197,  197,  159,  197,  197,  162,
+ /*  3920 */   163,  164,  165,  166,  197,  197,  197,  197,  197,  197,
+ /*  3930 */   132,  197,  197,  197,  197,  197,  179,  180,  197,  197,
+ /*  3940 */   183,  184,  185,  186,  187,  188,  189,  190,  191,  192,
+ /*  3950 */   193,  194,  195,  196,  156,  197,  197,  159,  197,  197,
+ /*  3960 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  3970 */   132,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  3980 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  3990 */   192,  193,  194,  195,  196,  197,  197,  159,  197,  197,
+ /*  4000 */   162,  163,  164,  165,  166,  197,  197,  197,  132,  197,
+ /*  4010 */   197,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  4020 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4030 */   192,  193,  194,  195,  196,  159,  197,  197,  162,  163,
+ /*  4040 */   164,  165,  166,  197,  197,  197,  132,  197,  197,  197,
+ /*  4050 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  183,
+ /*  4060 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  4070 */   194,  195,  196,  159,  197,  197,  162,  163,  164,  165,
+ /*  4080 */   166,  197,  197,  197,  132,  197,  197,  197,  197,  197,
+ /*  4090 */   197,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  4100 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  4110 */   196,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  4120 */   197,  197,  132,  197,  197,  197,  197,  197,  197,  197,
+ /*  4130 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  4140 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  159,
+ /*  4150 */   197,  197,  162,  163,  164,  165,  166,  197,  197,  197,
+ /*  4160 */   132,  197,  197,  197,  197,  197,  197,  197,  197,  179,
+ /*  4170 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  4180 */   190,  191,  192,  193,  194,  195,  196,  159,  197,  197,
+ /*  4190 */   162,  163,  164,  165,  166,  197,  197,  197,  132,  197,
+ /*  4200 */   197,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  4210 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4220 */   192,  193,  194,  195,  196,  159,  197,  197,  162,  163,
+ /*  4230 */   164,  165,  166,  197,  197,  197,  132,  197,  197,  197,
+ /*  4240 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  183,
+ /*  4250 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  4260 */   194,  195,  196,  159,  197,  197,  162,  163,  164,  165,
+ /*  4270 */   166,  197,  197,  197,  132,  197,  197,  197,  197,  197,
+ /*  4280 */   197,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  4290 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  4300 */   196,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  4310 */   197,  197,  132,  197,  197,  197,  197,  197,  197,  197,
+ /*  4320 */   197,  179,  180,  197,  197,  183,  184,  185,  186,  187,
+ /*  4330 */   188,  189,  190,  191,  192,  193,  194,  195,  196,  159,
+ /*  4340 */   197,  197,  162,  163,  164,  165,  166,  197,  197,  197,
+ /*  4350 */   132,  197,  197,  197,  197,  197,  197,  197,  197,  179,
+ /*  4360 */   180,  197,  197,  183,  184,  185,  186,  187,  188,  189,
+ /*  4370 */   190,  191,  192,  193,  194,  195,  196,  159,  197,  197,
+ /*  4380 */   162,  163,  164,  165,  166,  197,  197,  197,  132,  197,
+ /*  4390 */   197,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  4400 */   197,  183,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4410 */   192,  193,  194,  195,  196,  159,  197,  197,  162,  163,
+ /*  4420 */   164,  165,  166,  197,  197,  197,  132,  197,  197,  197,
+ /*  4430 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  183,
+ /*  4440 */   184,  185,  186,  187,  188,  189,  190,  191,  192,  193,
+ /*  4450 */   194,  195,  196,  159,  197,  197,  162,  163,  164,  165,
+ /*  4460 */   166,  197,  197,  197,  197,  197,   45,  197,  197,  197,
+ /*  4470 */   197,  197,  197,  179,  180,  197,  197,  183,  184,  185,
+ /*  4480 */   186,  187,  188,  189,  190,  191,  192,  193,  194,  195,
+ /*  4490 */   196,   70,   71,   72,   73,   74,   75,   76,   77,   78,
+ /*  4500 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
+ /*  4510 */    89,  197,   91,  197,   47,  197,  197,  197,  197,  197,
+ /*  4520 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  4530 */   132,  197,  197,  197,  197,  197,  197,   70,   71,   72,
+ /*  4540 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
+ /*  4550 */    83,   84,   85,   86,   87,   88,   89,  159,   91,  197,
+ /*  4560 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  4570 */   197,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  4580 */   197,  197,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4590 */   192,  193,  194,  195,  196,  197,  197,  197,  197,  197,
+ /*  4600 */   197,   70,   71,   72,   73,   74,   75,   76,   77,   78,
+ /*  4610 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
+ /*  4620 */    89,  132,   91,  197,  124,  125,  126,  127,  128,  197,
+ /*  4630 */   130,  131,  132,  197,  134,  135,  197,  197,  197,  197,
+ /*  4640 */   197,  197,  197,  143,  197,  145,  197,  197,  159,  149,
+ /*  4650 */   197,  162,  163,  164,  165,  166,  197,  197,  197,  159,
+ /*  4660 */   197,  197,  162,  163,  164,  165,  166,  197,  179,  180,
+ /*  4670 */   132,  197,  197,  184,  185,  186,  187,  188,  189,  190,
+ /*  4680 */   191,  192,  193,  194,  195,  197,  197,  197,  197,  197,
+ /*  4690 */   197,  197,  197,  197,  197,  197,  197,  159,  197,  197,
+ /*  4700 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  4710 */   132,  197,  197,  197,  197,  197,  197,  179,  180,  197,
+ /*  4720 */   132,  197,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4730 */   192,  193,  194,  145,  197,  197,  197,  159,  197,  197,
+ /*  4740 */   162,  163,  164,  165,  166,  132,  197,  159,  197,  197,
+ /*  4750 */   162,  163,  164,  165,  166,  197,  197,  179,  180,  197,
+ /*  4760 */   197,  197,  184,  185,  186,  187,  188,  189,  190,  191,
+ /*  4770 */   192,  193,  159,  197,  197,  162,  163,  164,  165,  166,
+ /*  4780 */   197,  132,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  4790 */   197,  197,  179,  180,  197,  197,  197,  184,  185,  186,
+ /*  4800 */   187,  188,  189,  190,  191,  192,  197,  197,  159,  197,
+ /*  4810 */   197,  162,  163,  164,  165,  166,  197,  197,  197,  197,
+ /*  4820 */   197,  197,  197,  197,  197,  197,  197,  197,  179,  180,
+ /*  4830 */   197,  197,  197,  184,  185,  186,  187,  188,  189,  190,
+ /*  4840 */   191,  197,  197,  197,  126,  127,  128,  197,  130,  131,
+ /*  4850 */   132,  197,  134,  135,  197,  197,  197,  197,  132,  197,
+ /*  4860 */   197,  143,  197,  145,  197,  197,  197,  149,  197,  197,
+ /*  4870 */   132,  197,  197,  197,  197,  197,  197,  159,  197,  197,
+ /*  4880 */   162,  163,  164,  165,  166,  159,  197,  197,  162,  163,
+ /*  4890 */   164,  165,  166,  197,  197,  197,  197,  159,  197,  197,
+ /*  4900 */   162,  163,  164,  165,  166,  179,  180,  197,  132,  197,
+ /*  4910 */   184,  185,  186,  187,  188,  189,  190,  179,  180,  197,
+ /*  4920 */   197,  197,  184,  185,  186,  187,  188,  189,  197,  197,
+ /*  4930 */   197,  197,  197,  197,  197,  159,  197,  197,  162,  163,
+ /*  4940 */   164,  165,  166,  197,  132,  197,  197,  197,  197,  197,
+ /*  4950 */   197,  197,  197,  197,  197,  179,  180,  197,  197,  197,
+ /*  4960 */   184,  185,  186,  187,  188,  189,  197,  197,  197,  197,
+ /*  4970 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  132,
+ /*  4980 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  4990 */   197,  179,  180,  197,  197,  197,  184,  185,  186,  187,
+ /*  5000 */   188,  197,  197,  197,  197,  197,  159,  197,  197,  162,
+ /*  5010 */   163,  164,  165,  166,  132,  197,  197,  197,  197,  197,
+ /*  5020 */   197,  197,  197,  197,  132,  197,  179,  180,  197,  197,
+ /*  5030 */   197,  184,  185,  186,  187,  188,  197,  197,  197,  197,
+ /*  5040 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  5050 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  132,
+ /*  5060 */   197,  179,  180,  197,  197,  197,  184,  185,  186,  187,
+ /*  5070 */   188,  179,  180,  197,  197,  197,  184,  185,  186,  187,
+ /*  5080 */   188,  197,  197,  197,  197,  197,  159,  197,  132,  162,
+ /*  5090 */   163,  164,  165,  166,  197,  197,  197,  132,  197,  197,
+ /*  5100 */   197,  197,  197,  197,  197,  197,  179,  180,  197,  197,
+ /*  5110 */   197,  184,  185,  186,  187,  159,  197,  197,  162,  163,
+ /*  5120 */   164,  165,  166,  197,  159,  132,  197,  162,  163,  164,
+ /*  5130 */   165,  166,  197,  197,  197,  179,  180,  197,  197,  197,
+ /*  5140 */   184,  185,  186,  187,  179,  180,  197,  197,  197,  184,
+ /*  5150 */   185,  186,  159,  197,  197,  162,  163,  164,  165,  166,
+ /*  5160 */   132,  197,  197,  197,  197,  137,  138,  139,  140,  197,
+ /*  5170 */   197,  197,  179,  180,  132,  197,  197,  184,  185,  186,
+ /*  5180 */   197,  197,  197,  197,  132,  197,  197,  159,  197,  197,
+ /*  5190 */   162,  163,  164,  165,  166,  197,  197,  197,  197,  197,
+ /*  5200 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  5210 */   197,  159,  132,  197,  162,  163,  164,  165,  166,  197,
+ /*  5220 */   197,  179,  180,  132,  197,  197,  184,  185,  197,  197,
+ /*  5230 */   197,  179,  180,  197,  197,  197,  184,  185,  197,  159,
+ /*  5240 */   197,  197,  162,  163,  164,  165,  166,  132,  197,  197,
+ /*  5250 */   159,  197,  197,  162,  163,  164,  165,  166,  132,  179,
+ /*  5260 */   180,  197,  197,  197,  184,  185,  197,  197,  197,  197,
+ /*  5270 */   179,  180,  197,  197,  159,  184,  185,  162,  163,  164,
+ /*  5280 */   165,  166,  132,  197,  197,  159,  197,  197,  162,  163,
+ /*  5290 */   164,  165,  166,  132,  179,  180,  197,  197,  197,  184,
+ /*  5300 */   185,  197,  197,  197,  197,  179,  180,  197,  197,  159,
+ /*  5310 */   184,  185,  162,  163,  164,  165,  166,  132,  197,  197,
+ /*  5320 */   159,  197,  197,  162,  163,  164,  165,  166,  197,  179,
+ /*  5330 */   180,  197,  197,  197,  184,  185,  197,  197,  197,  197,
+ /*  5340 */   179,  180,  197,  197,  159,  184,  185,  162,  163,  164,
+ /*  5350 */   165,  166,  197,  197,  132,  197,  197,  197,  197,  197,
+ /*  5360 */   197,  197,  197,  197,  179,  180,  197,  197,  197,  184,
+ /*  5370 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  5380 */   197,  159,  197,  197,  162,  163,  164,  165,  166,  197,
+ /*  5390 */   197,  197,  197,  197,  197,  197,  197,  197,  197,  197,
+ /*  5400 */   197,  179,  180,  197,  197,  197,  184,
+};
+#define YY_SHIFT_USE_DFLT (-43)
+#define YY_SHIFT_COUNT (296)
+#define YY_SHIFT_MIN   (-42)
+#define YY_SHIFT_MAX   (4531)
+static const short yy_shift_ofst[] = {
+ /*     0 */  2620,   63,  153,  153,  -27,  153,  153,  153,  153,  153,
+ /*    10 */   153,  153,  153,  153,  153,  153,  153,  153,  153,  153,
+ /*    20 */   153,  153,  153,  243, 2404, 2404, 2404, 2338, 2270, 2248,
+ /*    30 */  2180, 2158, 2090, 2068, 2000, 1978, 1910, 1888, 1820, 1798,
+ /*    40 */  2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541,
+ /*    50 */  2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541,
+ /*    60 */  2541, 2541, 2541, 2541, 2541, 2541, 1733, 2541, 2541, 2541,
+ /*    70 */  2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541, 2541,
+ /*    80 */  2541, 2541, 2541, 2541, 2541, 2541, 2541, 2855, 2855, 2812,
+ /*    90 */  2470, 2470, 2772, 2898, 2665, 4467, 4531, 4421, 4531, 4531,
+ /*   100 */   459,  456,  395,  619,  619,  408,   12,   12,  411,  315,
+ /*   110 */   847,  801,  837,  837,  837,  837,  801,  334,  102,  102,
+ /*   120 */   388,  801,  801, 2542,  408,  408,  -40,  330,  384,  807,
+ /*   130 */   852,  852,  852,  852,  807,  825,  802,  807,  852,  803,
+ /*   140 */   798,  801,  809,  809,  801, 2525,  495,   61,   61,   61,
+ /*   150 */   -33,  195,   72,   66,  200,   -7,   -7,   -7,  -28,  639,
+ /*   160 */   592,  605,  590,  559,  560,  557,  530,  583,  527,  439,
+ /*   170 */   391,  378,  437,  421,  387,  367,  246,    2,  312,  383,
+ /*   180 */   348,  344,  366,  281,  267,  244,  257,  307,  231,  252,
+ /*   190 */    93,   93,  106,  106,  106,  106,    3,    3,  106,    3,
+ /*   200 */   311,  221,   41,  804,  800,  778,  774,  769,  773,  770,
+ /*   210 */   810,  768,  763,  756,  747,  734,  757,  754,  755,  753,
+ /*   220 */   751,  750,  690,  685,  675,  745,  674,  739,  669,  736,
+ /*   230 */   662,  729,  659,  726,  658,  711,  707,  706,  703,  696,
+ /*   240 */   688,  647,  689,  684,  636,  683,  679,  678,  677,  673,
+ /*   250 */   672,  670,  650,  671,  709,  644,  610,  663,  657,  661,
+ /*   260 */   655,  630,  621,  625,  613,  643,  640,  643,  638,  640,
+ /*   270 */   635,  638,  635,  629,  561,  645,  552,  633,  564,  545,
+ /*   280 */   534,  556,  515,  500,  428,  478,  396,  475,  410,  407,
+ /*   290 */   333,  227,  180,  134,    5,  -42,   48,
+};
+#define YY_REDUCE_USE_DFLT (-128)
+#define YY_REDUCE_COUNT (144)
+#define YY_REDUCE_MIN   (-127)
+#define YY_REDUCE_MAX   (5222)
+static const short yy_reduce_ofst[] = {
+ /*     0 */  4500,  354,  280,  210,  712,  712, 1576, 1506, 1432, 1362,
+ /*    10 */  1288, 1218, 1144, 1074, 1000,  930,  856,  786,  712,  642,
+ /*    20 */   568,  498,  424, 2628, 2979, 2938, 2897, 2856, 3798, 3757,
+ /*    30 */  3716, 3675, 3634, 3593, 3552, 3511, 3470, 3429, 3388, 3348,
+ /*    40 */  3307, 3266, 3225, 3184, 3143, 3102, 3061, 3020, 4294, 4256,
+ /*    50 */  4218, 4180, 4142, 4104, 4066, 4028, 3990, 3952, 3914, 3876,
+ /*    60 */  3838, 4398, 4489, 4538, 4578, 4613, 4718, 4649, 4726, 4776,
+ /*    70 */  4738, 4892, 4882, 4847, 4812, 4956, 4927, 4993, 4965, 5161,
+ /*    80 */  5150, 5126, 5115, 5091, 5080, 5052, 5042, 5028, 2868, 2302,
+ /*    90 */  5222, 5185, 2637, 2439, 4588,  400,  128,  617,  543,  473,
+ /*   100 */   360, -127,  165,   52,  -37,   90,  202,   34,   46,   46,
+ /*   110 */    75,   46,  109,   56,   47,  -34,  -44,  491,  503,  481,
+ /*   120 */   326,  419,  291,  720,  752,  722,  708,  681,  743,  758,
+ /*   130 */   737,  731,  728,  727,  730,  686,  735,  723,  716,  699,
+ /*   140 */   697,  717,  681,  667,  713,
+};
+static const YYACTIONTYPE yy_default[] = {
+ /*     0 */   813,  813,  741,  739,  813,  740,  813,  813,  813,  813,
+ /*    10 */   813,  813,  813,  813,  813,  813,  813,  813,  738,  813,
+ /*    20 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    30 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    40 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    50 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    60 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    70 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*    80 */   813,  813,  813,  813,  813,  813,  813,  544,  544,  813,
+ /*    90 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   100 */   606,  598,  813,  602,  594,  813,  562,  554,  813,  813,
+ /*   110 */   813,  813,  604,  600,  596,  592,  813,  813,  558,  550,
+ /*   120 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  538,
+ /*   130 */   560,  556,  552,  548,  536,  813,  813,  614,  813,  620,
+ /*   140 */   619,  813,  813,  742,  813,  767,  813,  786,  785,  784,
+ /*   150 */   760,  813,  813,  813,  813,  775,  774,  773,  623,  813,
+ /*   160 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   170 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   180 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  797,
+ /*   190 */   788,  787,  783,  782,  781,  780,  778,  777,  779,  776,
+ /*   200 */   813,  813,  813,  813,  813,  813,  565,  563,  813,  813,
+ /*   210 */   543,  813,  813,  813,  813,  813,  813,  689,  813,  813,
+ /*   220 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   230 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   240 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   250 */   813,  813,  813,  813,  631,  813,  813,  813,  813,  813,
+ /*   260 */   813,  813,  813,  813,  813,  796,  794,  795,  792,  793,
+ /*   270 */   790,  791,  789,  813,  813,  813,  813,  813,  813,  813,
+ /*   280 */   813,  813,  813,  813,  813,  813,  813,  813,  813,  813,
+ /*   290 */   813,  813,  813,  813,  813,  813,  623,  526,  541,  537,
+ /*   300 */   540,  545,  568,  567,  564,  566,  561,  559,  557,  555,
+ /*   310 */   553,  551,  549,  547,  546,  542,  539,  535,  532,  531,
+ /*   320 */   736,  718,  717,  719,  716,  712,  711,  710,  709,  714,
+ /*   330 */   713,  715,  735,  730,  723,  708,  707,  703,  702,  698,
+ /*   340 */   697,  696,  695,  694,  693,  692,  673,  737,  691,  690,
+ /*   350 */   688,  687,  685,  734,  733,  729,  728,  727,  726,  731,
+ /*   360 */   725,  732,  724,  722,  706,  701,  721,  705,  700,  720,
+ /*   370 */   704,  699,  684,  680,  679,  678,  677,  676,  675,  683,
+ /*   380 */   682,  681,  674,  672,  671,  608,  609,  612,  615,  616,
+ /*   390 */   613,  611,  610,  607,  574,  573,  572,  571,  570,  578,
+ /*   400 */   686,  577,  576,  588,  587,  586,  585,  584,  583,  582,
+ /*   410 */   581,  580,  579,  589,  605,  603,  601,  599,  628,  630,
+ /*   420 */   629,  627,  597,  595,  636,  635,  634,  633,  632,  593,
+ /*   430 */   618,  617,  591,  626,  625,  624,  569,  590,  575,  534,
+ /*   440 */   812,  758,  759,  757,  756,  768,  755,  754,  753,  761,
+ /*   450 */   811,  810,  809,  808,  807,  806,  805,  804,  803,  802,
+ /*   460 */   801,  800,  799,  798,  772,  771,  770,  769,  767,  766,
+ /*   470 */   765,  764,  763,  762,  752,  750,  749,  748,  747,  746,
+ /*   480 */   745,  744,  743,  751,  670,  669,  668,  667,  666,  665,
+ /*   490 */   664,  663,  662,  661,  660,  659,  658,  657,  656,  655,
+ /*   500 */   654,  653,  652,  651,  650,  649,  648,  647,  646,  645,
+ /*   510 */   644,  643,  642,  641,  640,  639,  638,  637,  622,  621,
+ /*   520 */   533,  530,  529,  528,  527,
+};
+
+/* The next table maps tokens into fallback tokens.  If a construct
+** like the following:
+** 
+**      %fallback ID X Y Z.
+**
+** appears in the grammar, then ID becomes a fallback token for X, Y,
+** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
+** but it does not parse, the type of the token is changed to ID and
+** the parse is retried before an error is thrown.
+*/
+#ifdef YYFALLBACK
+static const YYCODETYPE yyFallback[] = {
+};
+#endif /* YYFALLBACK */
+
+/* The following structure represents a single element of the
+** parser's stack.  Information stored includes:
+**
+**   +  The state number for the parser at this level of the stack.
+**
+**   +  The value of the token stored at this level of the stack.
+**      (In other words, the "major" token.)
+**
+**   +  The semantic value stored at this level of the stack.  This is
+**      the information used by the action routines in the grammar.
+**      It is sometimes called the "minor" token.
+*/
+struct yyStackEntry {
+  YYACTIONTYPE stateno;  /* The state-number */
+  YYCODETYPE major;      /* The major token value.  This is the code
+                         ** number for the token at this stack level */
+  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
+                         ** is the value of the token  */
+};
+typedef struct yyStackEntry yyStackEntry;
+
+/* The state of the parser is completely contained in an instance of
+** the following structure */
+struct yyParser {
+  int yyidx;                    /* Index of top element in stack */
+#ifdef YYTRACKMAXSTACKDEPTH
+  int yyidxMax;                 /* Maximum value of yyidx */
+#endif
+  int yyerrcnt;                 /* Shifts left before out of the error */
+  ParseHLSLARG_SDECL                /* A place to hold %extra_argument */
+#if YYSTACKDEPTH<=0
+  int yystksz;                  /* Current side of the stack */
+  yyStackEntry *yystack;        /* The parser's stack */
+#else
+  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
+#endif
+};
+typedef struct yyParser yyParser;
+
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+#include <stdio.h>
+static FILE *yyTraceFILE = 0;
+static char *yyTracePrompt = 0;
+#endif /* NDEBUG */
+
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+/*
+** Turn parser tracing on by giving a stream to which to write the trace
+** and a prompt to preface each trace message.  Tracing is turned off
+** by making either argument NULL 
+**
+** Inputs:
+** <ul>
+** <li> A FILE* to which trace output should be written.
+**      If NULL, then tracing is turned off.
+** <li> A prefix string written at the beginning of every
+**      line of trace output.  If NULL, then tracing is
+**      turned off.
+** </ul>
+**
+** Outputs:
+** None.
+*/
+#if __MOJOSHADER__
+static
+#endif
+void ParseHLSLTrace(FILE *TraceFILE, char *zTracePrompt){
+  yyTraceFILE = TraceFILE;
+  yyTracePrompt = zTracePrompt;
+  if( yyTraceFILE==0 ) yyTracePrompt = 0;
+  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
+}
+#endif /* NDEBUG */
+
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+/* For tracing shifts, the names of all terminals and nonterminals
+** are required.  The following table supplies these names */
+static const char *const yyTokenName[] = { 
+  "$",             "COMMA",         "ASSIGN",        "ADDASSIGN",   
+  "SUBASSIGN",     "MULASSIGN",     "DIVASSIGN",     "MODASSIGN",   
+  "LSHIFTASSIGN",  "RSHIFTASSIGN",  "ANDASSIGN",     "ORASSIGN",    
+  "XORASSIGN",     "QUESTION",      "OROR",          "ANDAND",      
+  "OR",            "XOR",           "AND",           "EQL",         
+  "NEQ",           "LT",            "LEQ",           "GT",          
+  "GEQ",           "LSHIFT",        "RSHIFT",        "PLUS",        
+  "MINUS",         "STAR",          "SLASH",         "PERCENT",     
+  "TYPECAST",      "EXCLAMATION",   "COMPLEMENT",    "MINUSMINUS",  
+  "PLUSPLUS",      "DOT",           "LBRACKET",      "RBRACKET",    
+  "LPAREN",        "RPAREN",        "ELSE",          "SEMICOLON",   
+  "TYPEDEF",       "CONST",         "IDENTIFIER",    "VOID",        
+  "INLINE",        "IN",            "INOUT",         "OUT",         
+  "UNIFORM",       "COLON",         "LINEAR",        "CENTROID",    
+  "NOINTERPOLATION",  "NOPERSPECTIVE",  "SAMPLE",        "EXTERN",      
+  "SHARED",        "STATIC",        "VOLATILE",      "ROWMAJOR",    
+  "COLUMNMAJOR",   "LBRACE",        "RBRACE",        "STRUCT",      
+  "PACKOFFSET",    "REGISTER",      "USERTYPE",      "SAMPLER",     
+  "SAMPLER1D",     "SAMPLER2D",     "SAMPLER3D",     "SAMPLERCUBE", 
+  "SAMPLER_STATE",  "SAMPLERSTATE",  "SAMPLERCOMPARISONSTATE",  "BOOL",        
+  "INT",           "UINT",          "HALF",          "FLOAT",       
+  "DOUBLE",        "STRING",        "SNORM",         "UNORM",       
+  "BUFFER",        "VECTOR",        "INT_CONSTANT",  "MATRIX",      
+  "ISOLATE",       "MAXINSTRUCTIONCOUNT",  "NOEXPRESSIONOPTIMIZATIONS",  "REMOVEUNUSEDINPUTS",
+  "UNUSED",        "XPS",           "BREAK",         "CONTINUE",    
+  "DISCARD",       "DO",            "WHILE",         "RETURN",      
+  "UNROLL",        "LOOP",          "FOR",           "BRANCH",      
+  "IF",            "FLATTEN",       "IFALL",         "IFANY",       
+  "PREDICATE",     "PREDICATEBLOCK",  "SWITCH",        "FORCECASE",   
+  "CALL",          "CASE",          "DEFAULT",       "FLOAT_CONSTANT",
+  "STRING_LITERAL",  "TRUE",          "FALSE",         "error",       
+  "shader",        "compilation_units",  "compilation_unit",  "variable_declaration",
+  "function_signature",  "statement_block",  "typedef",       "struct_declaration",
+  "datatype",      "scalar_or_array",  "function_storageclass",  "function_details",
+  "semantic",      "function_parameters",  "function_parameter_list",  "function_parameter",
+  "input_modifier",  "interpolation_mod",  "initializer",   "variable_attribute_list",
+  "variable_declaration_details_list",  "variable_attribute",  "variable_declaration_details",  "annotations", 
+  "variable_lowlevel",  "struct_intro",  "struct_member_list",  "struct_member",
+  "struct_member_details",  "struct_member_item_list",  "packoffset",    "register",    
+  "expression",    "annotation_list",  "annotation",    "datatype_scalar",
+  "initializer_block_list",  "initializer_block",  "intrinsic_datatype",  "datatype_vector",
+  "datatype_matrix",  "datatype_sampler",  "datatype_buffer",  "statement_list",
+  "statement",     "statement_attribute",  "do_intro",      "while_intro", 
+  "if_intro",      "switch_intro",  "switch_case_list",  "for_statement",
+  "for_intro",     "for_details",   "switch_case",   "primary_expr",
+  "postfix_expr",  "arguments",     "argument_list",  "assignment_expr",
+  "unary_expr",    "cast_expr",     "multiplicative_expr",  "additive_expr",
+  "shift_expr",    "relational_expr",  "equality_expr",  "and_expr",    
+  "exclusive_or_expr",  "inclusive_or_expr",  "logical_and_expr",  "logical_or_expr",
+  "conditional_expr",
+};
+#endif /* NDEBUG */
+
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+/* For tracing reduce actions, the names of all rules are required.
+*/
+static const char *const yyRuleName[] = {
+ /*   0 */ "shader ::= compilation_units",
+ /*   1 */ "compilation_units ::= compilation_unit",
+ /*   2 */ "compilation_units ::= compilation_units compilation_unit",
+ /*   3 */ "compilation_unit ::= variable_declaration",
+ /*   4 */ "compilation_unit ::= function_signature SEMICOLON",
+ /*   5 */ "compilation_unit ::= function_signature statement_block",
+ /*   6 */ "compilation_unit ::= typedef",
+ /*   7 */ "compilation_unit ::= struct_declaration SEMICOLON",
+ /*   8 */ "typedef ::= TYPEDEF CONST datatype scalar_or_array",
+ /*   9 */ "typedef ::= TYPEDEF datatype scalar_or_array",
+ /*  10 */ "function_signature ::= function_storageclass function_details semantic",
+ /*  11 */ "function_signature ::= function_storageclass function_details",
+ /*  12 */ "function_signature ::= function_details semantic",
+ /*  13 */ "function_signature ::= function_details",
+ /*  14 */ "function_details ::= datatype IDENTIFIER LPAREN function_parameters RPAREN",
+ /*  15 */ "function_details ::= VOID IDENTIFIER LPAREN function_parameters RPAREN",
+ /*  16 */ "function_storageclass ::= INLINE",
+ /*  17 */ "function_parameters ::= VOID",
+ /*  18 */ "function_parameters ::= function_parameter_list",
+ /*  19 */ "function_parameters ::=",
+ /*  20 */ "function_parameter_list ::= function_parameter",
+ /*  21 */ "function_parameter_list ::= function_parameter_list COMMA function_parameter",
+ /*  22 */ "function_parameter ::= input_modifier datatype IDENTIFIER semantic interpolation_mod initializer",
+ /*  23 */ "function_parameter ::= input_modifier datatype IDENTIFIER semantic interpolation_mod",
+ /*  24 */ "function_parameter ::= input_modifier datatype IDENTIFIER semantic initializer",
+ /*  25 */ "function_parameter ::= input_modifier datatype IDENTIFIER semantic",
+ /*  26 */ "function_parameter ::= input_modifier datatype IDENTIFIER interpolation_mod initializer",
+ /*  27 */ "function_parameter ::= input_modifier datatype IDENTIFIER interpolation_mod",
+ /*  28 */ "function_parameter ::= input_modifier datatype IDENTIFIER initializer",
+ /*  29 */ "function_parameter ::= input_modifier datatype IDENTIFIER",
+ /*  30 */ "function_parameter ::= datatype IDENTIFIER semantic interpolation_mod initializer",
+ /*  31 */ "function_parameter ::= datatype IDENTIFIER semantic interpolation_mod",
+ /*  32 */ "function_parameter ::= datatype IDENTIFIER semantic initializer",
+ /*  33 */ "function_parameter ::= datatype IDENTIFIER semantic",
+ /*  34 */ "function_parameter ::= datatype IDENTIFIER interpolation_mod initializer",
+ /*  35 */ "function_parameter ::= datatype IDENTIFIER interpolation_mod",
+ /*  36 */ "function_parameter ::= datatype IDENTIFIER initializer",
+ /*  37 */ "function_parameter ::= datatype IDENTIFIER",
+ /*  38 */ "input_modifier ::= IN",
+ /*  39 */ "input_modifier ::= INOUT",
+ /*  40 */ "input_modifier ::= OUT",
+ /*  41 */ "input_modifier ::= IN OUT",
+ /*  42 */ "input_modifier ::= OUT IN",
+ /*  43 */ "input_modifier ::= UNIFORM",
+ /*  44 */ "semantic ::= COLON IDENTIFIER",
+ /*  45 */ "interpolation_mod ::= LINEAR",
+ /*  46 */ "interpolation_mod ::= CENTROID",
+ /*  47 */ "interpolation_mod ::= NOINTERPOLATION",
+ /*  48 */ "interpolation_mod ::= NOPERSPECTIVE",
+ /*  49 */ "interpolation_mod ::= SAMPLE",
+ /*  50 */ "variable_declaration ::= variable_attribute_list datatype variable_declaration_details_list SEMICOLON",
+ /*  51 */ "variable_declaration ::= datatype variable_declaration_details_list SEMICOLON",
+ /*  52 */ "variable_declaration ::= struct_declaration variable_declaration_details_list SEMICOLON",
+ /*  53 */ "variable_attribute_list ::= variable_attribute",
+ /*  54 */ "variable_attribute_list ::= variable_attribute_list variable_attribute",
+ /*  55 */ "variable_attribute ::= EXTERN",
+ /*  56 */ "variable_attribute ::= NOINTERPOLATION",
+ /*  57 */ "variable_attribute ::= SHARED",
+ /*  58 */ "variable_attribute ::= STATIC",
+ /*  59 */ "variable_attribute ::= UNIFORM",
+ /*  60 */ "variable_attribute ::= VOLATILE",
+ /*  61 */ "variable_attribute ::= CONST",
+ /*  62 */ "variable_attribute ::= ROWMAJOR",
+ /*  63 */ "variable_attribute ::= COLUMNMAJOR",
+ /*  64 */ "variable_declaration_details_list ::= variable_declaration_details",
+ /*  65 */ "variable_declaration_details_list ::= variable_declaration_details_list COMMA variable_declaration_details",
+ /*  66 */ "variable_declaration_details ::= scalar_or_array semantic annotations initializer variable_lowlevel",
+ /*  67 */ "variable_declaration_details ::= scalar_or_array semantic annotations initializer",
+ /*  68 */ "variable_declaration_details ::= scalar_or_array semantic annotations variable_lowlevel",
+ /*  69 */ "variable_declaration_details ::= scalar_or_array semantic annotations",
+ /*  70 */ "variable_declaration_details ::= scalar_or_array semantic initializer variable_lowlevel",
+ /*  71 */ "variable_declaration_details ::= scalar_or_array semantic initializer",
+ /*  72 */ "variable_declaration_details ::= scalar_or_array semantic variable_lowlevel",
+ /*  73 */ "variable_declaration_details ::= scalar_or_array semantic",
+ /*  74 */ "variable_declaration_details ::= scalar_or_array annotations initializer variable_lowlevel",
+ /*  75 */ "variable_declaration_details ::= scalar_or_array annotations initializer",
+ /*  76 */ "variable_declaration_details ::= scalar_or_array annotations variable_lowlevel",
+ /*  77 */ "variable_declaration_details ::= scalar_or_array annotations",
+ /*  78 */ "variable_declaration_details ::= scalar_or_array initializer variable_lowlevel",
+ /*  79 */ "variable_declaration_details ::= scalar_or_array initializer",
+ /*  80 */ "variable_declaration_details ::= scalar_or_array variable_lowlevel",
+ /*  81 */ "variable_declaration_details ::= scalar_or_array",
+ /*  82 */ "struct_declaration ::= struct_intro LBRACE struct_member_list RBRACE",
+ /*  83 */ "struct_intro ::= STRUCT IDENTIFIER",
+ /*  84 */ "struct_member_list ::= struct_member",
+ /*  85 */ "struct_member_list ::= struct_member_list struct_member",
+ /*  86 */ "struct_member ::= interpolation_mod struct_member_details",
+ /*  87 */ "struct_member ::= struct_member_details",
+ /*  88 */ "struct_member_details ::= datatype struct_member_item_list SEMICOLON",
+ /*  89 */ "struct_member_item_list ::= scalar_or_array",
+ /*  90 */ "struct_member_item_list ::= scalar_or_array semantic",
+ /*  91 */ "struct_member_item_list ::= struct_member_item_list COMMA IDENTIFIER",
+ /*  92 */ "variable_lowlevel ::= packoffset register",
+ /*  93 */ "variable_lowlevel ::= register packoffset",
+ /*  94 */ "variable_lowlevel ::= packoffset",
+ /*  95 */ "variable_lowlevel ::= register",
+ /*  96 */ "scalar_or_array ::= IDENTIFIER LBRACKET RBRACKET",
+ /*  97 */ "scalar_or_array ::= IDENTIFIER LBRACKET expression RBRACKET",
+ /*  98 */ "scalar_or_array ::= IDENTIFIER",
+ /*  99 */ "packoffset ::= COLON PACKOFFSET LPAREN IDENTIFIER DOT IDENTIFIER RPAREN",
+ /* 100 */ "packoffset ::= COLON PACKOFFSET LPAREN IDENTIFIER RPAREN",
+ /* 101 */ "register ::= COLON REGISTER LPAREN IDENTIFIER RPAREN",
+ /* 102 */ "annotations ::= LT annotation_list GT",
+ /* 103 */ "annotation_list ::= annotation",
+ /* 104 */ "annotation_list ::= annotation_list annotation",
+ /* 105 */ "annotation ::= datatype_scalar initializer SEMICOLON",
+ /* 106 */ "initializer_block_list ::= expression",
+ /* 107 */ "initializer_block_list ::= LBRACE initializer_block_list RBRACE",
+ /* 108 */ "initializer_block_list ::= initializer_block_list COMMA initializer_block_list",
+ /* 109 */ "initializer_block ::= LBRACE initializer_block_list RBRACE",
+ /* 110 */ "initializer ::= ASSIGN initializer_block",
+ /* 111 */ "initializer ::= ASSIGN expression",
+ /* 112 */ "intrinsic_datatype ::= datatype_vector",
+ /* 113 */ "intrinsic_datatype ::= datatype_matrix",
+ /* 114 */ "intrinsic_datatype ::= datatype_scalar",
+ /* 115 */ "intrinsic_datatype ::= datatype_sampler",
+ /* 116 */ "intrinsic_datatype ::= datatype_buffer",
+ /* 117 */ "datatype ::= intrinsic_datatype",
+ /* 118 */ "datatype ::= USERTYPE",
+ /* 119 */ "datatype_sampler ::= SAMPLER",
+ /* 120 */ "datatype_sampler ::= SAMPLER1D",
+ /* 121 */ "datatype_sampler ::= SAMPLER2D",
+ /* 122 */ "datatype_sampler ::= SAMPLER3D",
+ /* 123 */ "datatype_sampler ::= SAMPLERCUBE",
+ /* 124 */ "datatype_sampler ::= SAMPLER_STATE",
+ /* 125 */ "datatype_sampler ::= SAMPLERSTATE",
+ /* 126 */ "datatype_sampler ::= SAMPLERCOMPARISONSTATE",
+ /* 127 */ "datatype_scalar ::= BOOL",
+ /* 128 */ "datatype_scalar ::= INT",
+ /* 129 */ "datatype_scalar ::= UINT",
+ /* 130 */ "datatype_scalar ::= HALF",
+ /* 131 */ "datatype_scalar ::= FLOAT",
+ /* 132 */ "datatype_scalar ::= DOUBLE",
+ /* 133 */ "datatype_scalar ::= STRING",
+ /* 134 */ "datatype_scalar ::= SNORM FLOAT",
+ /* 135 */ "datatype_scalar ::= UNORM FLOAT",
+ /* 136 */ "datatype_buffer ::= BUFFER LT BOOL GT",
+ /* 137 */ "datatype_buffer ::= BUFFER LT INT GT",
+ /* 138 */ "datatype_buffer ::= BUFFER LT UINT GT",
+ /* 139 */ "datatype_buffer ::= BUFFER LT HALF GT",
+ /* 140 */ "datatype_buffer ::= BUFFER LT FLOAT GT",
+ /* 141 */ "datatype_buffer ::= BUFFER LT DOUBLE GT",
+ /* 142 */ "datatype_buffer ::= BUFFER LT SNORM FLOAT GT",
+ /* 143 */ "datatype_buffer ::= BUFFER LT UNORM FLOAT GT",
+ /* 144 */ "datatype_vector ::= VECTOR LT datatype_scalar COMMA INT_CONSTANT GT",
+ /* 145 */ "datatype_matrix ::= MATRIX LT datatype_scalar COMMA INT_CONSTANT COMMA INT_CONSTANT GT",
+ /* 146 */ "statement_block ::= LBRACE RBRACE",
+ /* 147 */ "statement_block ::= LBRACE statement_list RBRACE",
+ /* 148 */ "statement_list ::= statement",
+ /* 149 */ "statement_list ::= statement_list statement",
+ /* 150 */ "statement_attribute ::= ISOLATE",
+ /* 151 */ "statement_attribute ::= MAXINSTRUCTIONCOUNT LPAREN INT_CONSTANT RPAREN",
+ /* 152 */ "statement_attribute ::= NOEXPRESSIONOPTIMIZATIONS",
+ /* 153 */ "statement_attribute ::= REMOVEUNUSEDINPUTS",
+ /* 154 */ "statement_attribute ::= UNUSED",
+ /* 155 */ "statement_attribute ::= XPS",
+ /* 156 */ "statement ::= BREAK SEMICOLON",
+ /* 157 */ "statement ::= CONTINUE SEMICOLON",
+ /* 158 */ "statement ::= DISCARD SEMICOLON",
+ /* 159 */ "statement ::= LBRACKET statement_attribute RBRACKET statement_block",
+ /* 160 */ "statement ::= variable_declaration",
+ /* 161 */ "statement ::= struct_declaration SEMICOLON",
+ /* 162 */ "statement ::= do_intro DO statement WHILE LPAREN expression RPAREN SEMICOLON",
+ /* 163 */ "statement ::= while_intro LPAREN expression RPAREN statement",
+ /* 164 */ "statement ::= if_intro LPAREN expression RPAREN statement",
+ /* 165 */ "statement ::= if_intro LPAREN expression RPAREN statement ELSE statement",
+ /* 166 */ "statement ::= switch_intro LPAREN expression RPAREN LBRACE switch_case_list RBRACE",
+ /* 167 */ "statement ::= typedef",
+ /* 168 */ "statement ::= SEMICOLON",
+ /* 169 */ "statement ::= expression SEMICOLON",
+ /* 170 */ "statement ::= RETURN SEMICOLON",
+ /* 171 */ "statement ::= RETURN expression SEMICOLON",
+ /* 172 */ "statement ::= statement_block",
+ /* 173 */ "statement ::= for_statement",
+ /* 174 */ "while_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET WHILE",
+ /* 175 */ "while_intro ::= LBRACKET UNROLL RBRACKET WHILE",
+ /* 176 */ "while_intro ::= LBRACKET LOOP RBRACKET WHILE",
+ /* 177 */ "while_intro ::= WHILE",
+ /* 178 */ "for_statement ::= for_intro for_details",
+ /* 179 */ "for_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET FOR",
+ /* 180 */ "for_intro ::= LBRACKET UNROLL RBRACKET FOR",
+ /* 181 */ "for_intro ::= LBRACKET LOOP RBRACKET FOR",
+ /* 182 */ "for_intro ::= FOR",
+ /* 183 */ "for_details ::= LPAREN expression SEMICOLON expression SEMICOLON expression RPAREN statement",
+ /* 184 */ "for_details ::= LPAREN SEMICOLON SEMICOLON RPAREN statement",
+ /* 185 */ "for_details ::= LPAREN SEMICOLON SEMICOLON expression RPAREN statement",
+ /* 186 */ "for_details ::= LPAREN SEMICOLON expression SEMICOLON RPAREN statement",
+ /* 187 */ "for_details ::= LPAREN SEMICOLON expression SEMICOLON expression RPAREN statement",
+ /* 188 */ "for_details ::= LPAREN expression SEMICOLON SEMICOLON RPAREN statement",
+ /* 189 */ "for_details ::= LPAREN expression SEMICOLON SEMICOLON expression RPAREN statement",
+ /* 190 */ "for_details ::= LPAREN expression SEMICOLON expression SEMICOLON RPAREN statement",
+ /* 191 */ "for_details ::= LPAREN variable_declaration expression SEMICOLON expression RPAREN statement",
+ /* 192 */ "for_details ::= LPAREN variable_declaration SEMICOLON RPAREN statement",
+ /* 193 */ "for_details ::= LPAREN variable_declaration SEMICOLON expression RPAREN statement",
+ /* 194 */ "for_details ::= LPAREN variable_declaration expression SEMICOLON RPAREN statement",
+ /* 195 */ "do_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET DO",
+ /* 196 */ "do_intro ::= LBRACKET UNROLL RBRACKET DO",
+ /* 197 */ "do_intro ::= LBRACKET LOOP RBRACKET DO",
+ /* 198 */ "do_intro ::= DO",
+ /* 199 */ "if_intro ::= LBRACKET BRANCH RBRACKET IF",
+ /* 200 */ "if_intro ::= LBRACKET FLATTEN RBRACKET IF",
+ /* 201 */ "if_intro ::= LBRACKET IFALL RBRACKET IF",
+ /* 202 */ "if_intro ::= LBRACKET IFANY RBRACKET IF",
+ /* 203 */ "if_intro ::= LBRACKET PREDICATE RBRACKET IF",
+ /* 204 */ "if_intro ::= LBRACKET PREDICATEBLOCK RBRACKET IF",
+ /* 205 */ "if_intro ::= IF",
+ /* 206 */ "switch_intro ::= LBRACKET FLATTEN RBRACKET SWITCH",
+ /* 207 */ "switch_intro ::= LBRACKET BRANCH RBRACKET SWITCH",
+ /* 208 */ "switch_intro ::= LBRACKET FORCECASE RBRACKET SWITCH",
+ /* 209 */ "switch_intro ::= LBRACKET CALL RBRACKET SWITCH",
+ /* 210 */ "switch_intro ::= SWITCH",
+ /* 211 */ "switch_case_list ::= switch_case",
+ /* 212 */ "switch_case_list ::= switch_case_list switch_case",
+ /* 213 */ "switch_case ::= CASE expression COLON statement_list",
+ /* 214 */ "switch_case ::= CASE expression COLON",
+ /* 215 */ "switch_case ::= DEFAULT COLON statement_list",
+ /* 216 */ "switch_case ::= DEFAULT COLON",
+ /* 217 */ "primary_expr ::= IDENTIFIER",
+ /* 218 */ "primary_expr ::= INT_CONSTANT",
+ /* 219 */ "primary_expr ::= FLOAT_CONSTANT",
+ /* 220 */ "primary_expr ::= STRING_LITERAL",
+ /* 221 */ "primary_expr ::= TRUE",
+ /* 222 */ "primary_expr ::= FALSE",
+ /* 223 */ "primary_expr ::= LPAREN expression RPAREN",
+ /* 224 */ "postfix_expr ::= primary_expr",
+ /* 225 */ "postfix_expr ::= postfix_expr LBRACKET expression RBRACKET",
+ /* 226 */ "postfix_expr ::= IDENTIFIER arguments",
+ /* 227 */ "postfix_expr ::= datatype arguments",
+ /* 228 */ "postfix_expr ::= postfix_expr DOT IDENTIFIER",
+ /* 229 */ "postfix_expr ::= postfix_expr PLUSPLUS",
+ /* 230 */ "postfix_expr ::= postfix_expr MINUSMINUS",
+ /* 231 */ "arguments ::= LPAREN RPAREN",
+ /* 232 */ "arguments ::= LPAREN argument_list RPAREN",
+ /* 233 */ "argument_list ::= assignment_expr",
+ /* 234 */ "argument_list ::= argument_list COMMA assignment_expr",
+ /* 235 */ "unary_expr ::= postfix_expr",
+ /* 236 */ "unary_expr ::= PLUSPLUS unary_expr",
+ /* 237 */ "unary_expr ::= MINUSMINUS unary_expr",
+ /* 238 */ "unary_expr ::= PLUS cast_expr",
+ /* 239 */ "unary_expr ::= MINUS cast_expr",
+ /* 240 */ "unary_expr ::= COMPLEMENT cast_expr",
+ /* 241 */ "unary_expr ::= EXCLAMATION cast_expr",
+ /* 242 */ "cast_expr ::= unary_expr",
+ /* 243 */ "cast_expr ::= LPAREN datatype RPAREN cast_expr",
+ /* 244 */ "multiplicative_expr ::= cast_expr",
+ /* 245 */ "multiplicative_expr ::= multiplicative_expr STAR cast_expr",
+ /* 246 */ "multiplicative_expr ::= multiplicative_expr SLASH cast_expr",
+ /* 247 */ "multiplicative_expr ::= multiplicative_expr PERCENT cast_expr",
+ /* 248 */ "additive_expr ::= multiplicative_expr",
+ /* 249 */ "additive_expr ::= additive_expr PLUS multiplicative_expr",
+ /* 250 */ "additive_expr ::= additive_expr MINUS multiplicative_expr",
+ /* 251 */ "shift_expr ::= additive_expr",
+ /* 252 */ "shift_expr ::= shift_expr LSHIFT additive_expr",
+ /* 253 */ "shift_expr ::= shift_expr RSHIFT additive_expr",
+ /* 254 */ "relational_expr ::= shift_expr",
+ /* 255 */ "relational_expr ::= relational_expr LT shift_expr",
+ /* 256 */ "relational_expr ::= relational_expr GT shift_expr",
+ /* 257 */ "relational_expr ::= relational_expr LEQ shift_expr",
+ /* 258 */ "relational_expr ::= relational_expr GEQ shift_expr",
+ /* 259 */ "equality_expr ::= relational_expr",
+ /* 260 */ "equality_expr ::= equality_expr EQL relational_expr",
+ /* 261 */ "equality_expr ::= equality_expr NEQ relational_expr",
+ /* 262 */ "and_expr ::= equality_expr",
+ /* 263 */ "and_expr ::= and_expr AND equality_expr",
+ /* 264 */ "exclusive_or_expr ::= and_expr",
+ /* 265 */ "exclusive_or_expr ::= exclusive_or_expr XOR and_expr",
+ /* 266 */ "inclusive_or_expr ::= exclusive_or_expr",
+ /* 267 */ "inclusive_or_expr ::= inclusive_or_expr OR exclusive_or_expr",
+ /* 268 */ "logical_and_expr ::= inclusive_or_expr",
+ /* 269 */ "logical_and_expr ::= logical_and_expr ANDAND inclusive_or_expr",
+ /* 270 */ "logical_or_expr ::= logical_and_expr",
+ /* 271 */ "logical_or_expr ::= logical_or_expr OROR logical_and_expr",
+ /* 272 */ "conditional_expr ::= logical_or_expr",
+ /* 273 */ "conditional_expr ::= logical_or_expr QUESTION logical_or_expr COLON conditional_expr",
+ /* 274 */ "assignment_expr ::= conditional_expr",
+ /* 275 */ "assignment_expr ::= unary_expr ASSIGN assignment_expr",
+ /* 276 */ "assignment_expr ::= unary_expr MULASSIGN assignment_expr",
+ /* 277 */ "assignment_expr ::= unary_expr DIVASSIGN assignment_expr",
+ /* 278 */ "assignment_expr ::= unary_expr MODASSIGN assignment_expr",
+ /* 279 */ "assignment_expr ::= unary_expr ADDASSIGN assignment_expr",
+ /* 280 */ "assignment_expr ::= unary_expr SUBASSIGN assignment_expr",
+ /* 281 */ "assignment_expr ::= unary_expr LSHIFTASSIGN assignment_expr",
+ /* 282 */ "assignment_expr ::= unary_expr RSHIFTASSIGN assignment_expr",
+ /* 283 */ "assignment_expr ::= unary_expr ANDASSIGN assignment_expr",
+ /* 284 */ "assignment_expr ::= unary_expr XORASSIGN assignment_expr",
+ /* 285 */ "assignment_expr ::= unary_expr ORASSIGN assignment_expr",
+ /* 286 */ "expression ::= assignment_expr",
+ /* 287 */ "expression ::= expression COMMA assignment_expr",
+};
+#endif /* NDEBUG */
+
+
+#if YYSTACKDEPTH<=0
+/*
+** Try to increase the size of the parser stack.
+*/
+static void yyGrowStack(yyParser *p){
+  int newSize;
+  yyStackEntry *pNew;
+
+  newSize = p->yystksz*2 + 100;
+  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
+  if( pNew ){
+    p->yystack = pNew;
+    p->yystksz = newSize;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+    if( yyTraceFILE ){
+      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
+              yyTracePrompt, p->yystksz);
+    }
+#endif
+  }
+}
+#endif
+
+/* 
+** This function allocates a new parser.
+** The only argument is a pointer to a function which works like
+** malloc.
+**
+** Inputs:
+** A pointer to the function used to allocate memory.
+**
+** Outputs:
+** A pointer to a parser.  This pointer is used in subsequent calls
+** to ParseHLSL and ParseHLSLFree.
+*/
+#if __MOJOSHADER__
+static void *ParseHLSLAlloc(void *(*mallocProc)(int,void *), void *malloc_data){
+  yyParser *pParser;
+  pParser = (yyParser*)(*mallocProc)( (int)sizeof(yyParser), malloc_data );
+#else
+void *ParseHLSLAlloc(void *(*mallocProc)(size_t)){
+  yyParser *pParser;
+  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
+#endif
+  if( pParser ){
+    pParser->yyidx = -1;
+#ifdef YYTRACKMAXSTACKDEPTH
+    pParser->yyidxMax = 0;
+#endif
+#if YYSTACKDEPTH<=0
+    pParser->yystack = NULL;
+    pParser->yystksz = 0;
+    yyGrowStack(pParser);
+#endif
+  }
+  return pParser;
+}
+
+/* The following function deletes the value associated with a
+** symbol.  The symbol can be either a terminal or nonterminal.
+** "yymajor" is the symbol code, and "yypminor" is a pointer to
+** the value.
+*/
+static void yy_destructor(
+  yyParser *yypParser,    /* The parser */
+  YYCODETYPE yymajor,     /* Type code for object to destroy */
+  YYMINORTYPE *yypminor   /* The object to be destroyed */
+){
+  ParseHLSLARG_FETCH;
+  switch( yymajor ){
+    /* Here is inserted the actions which take place when a
+    ** terminal or non-terminal is destroyed.  This can happen
+    ** when the symbol is popped from the stack during a
+    ** reduce or during error processing or when a parser is 
+    ** being destroyed before it is finished parsing.
+    **
+    ** Note: during a reduce, the only symbols destroyed are those
+    ** which appear on the RHS of the rule, but which are not used
+    ** inside the C code.
+    */
+    case 125: /* compilation_units */
+    case 126: /* compilation_unit */
+{
+#line 81 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_compilation_unit(ctx, (yypminor->yy139)); 
+#line 2033 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 127: /* variable_declaration */
+    case 144: /* variable_declaration_details_list */
+    case 146: /* variable_declaration_details */
+{
+#line 175 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_variable_declaration(ctx, (yypminor->yy24)); 
+#line 2042 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 128: /* function_signature */
+    case 135: /* function_details */
+{
+#line 102 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_function_signature(ctx, (yypminor->yy364)); 
+#line 2050 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 129: /* statement_block */
+    case 167: /* statement_list */
+    case 168: /* statement */
+    case 175: /* for_statement */
+    case 177: /* for_details */
+{
+#line 354 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_statement(ctx, (yypminor->yy233)); 
+#line 2061 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 130: /* typedef */
+{
+#line 96 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_typedef(ctx, (yypminor->yy71)); 
+#line 2068 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 131: /* struct_declaration */
+{
+#line 224 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_struct_declaration(ctx, (yypminor->yy249)); 
+#line 2075 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 133: /* scalar_or_array */
+{
+#line 260 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_scalar_or_array(ctx, (yypminor->yy380)); 
+#line 2082 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 137: /* function_parameters */
+    case 138: /* function_parameter_list */
+    case 139: /* function_parameter */
+{
+#line 124 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_function_params(ctx, (yypminor->yy307)); 
+#line 2091 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 142: /* initializer */
+    case 156: /* expression */
+    case 160: /* initializer_block_list */
+    case 161: /* initializer_block */
+    case 179: /* primary_expr */
+    case 180: /* postfix_expr */
+    case 183: /* assignment_expr */
+    case 184: /* unary_expr */
+    case 185: /* cast_expr */
+    case 186: /* multiplicative_expr */
+    case 187: /* additive_expr */
+    case 188: /* shift_expr */
+    case 189: /* relational_expr */
+    case 190: /* equality_expr */
+    case 191: /* and_expr */
+    case 192: /* exclusive_or_expr */
+    case 193: /* inclusive_or_expr */
+    case 194: /* logical_and_expr */
+    case 195: /* logical_or_expr */
+    case 196: /* conditional_expr */
+{
+#line 301 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_expr(ctx, (yypminor->yy322)); 
+#line 2117 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 147: /* annotations */
+    case 157: /* annotation_list */
+    case 158: /* annotation */
+{
+#line 277 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_annotation(ctx, (yypminor->yy268)); 
+#line 2126 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 148: /* variable_lowlevel */
+{
+#line 252 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_variable_lowlevel(ctx, (yypminor->yy82)); 
+#line 2133 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 150: /* struct_member_list */
+    case 151: /* struct_member */
+    case 152: /* struct_member_details */
+    case 153: /* struct_member_item_list */
+{
+#line 232 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_struct_member(ctx, (yypminor->yy346)); 
+#line 2143 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 154: /* packoffset */
+{
+#line 266 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_pack_offset(ctx, (yypminor->yy8)); 
+#line 2150 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 174: /* switch_case_list */
+    case 178: /* switch_case */
+{
+#line 450 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_switch_case(ctx, (yypminor->yy165)); 
+#line 2158 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    case 181: /* arguments */
+    case 182: /* argument_list */
+{
+#line 485 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+ delete_arguments(ctx, (yypminor->yy26)); 
+#line 2166 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+}
+      break;
+    default:  break;   /* If no destructor action specified: do nothing */
+  }
+}
+
+/*
+** Pop the parser's stack once.
+**
+** If there is a destructor routine associated with the token which
+** is popped from the stack, then call it.
+**
+** Return the major token number for the symbol popped.
+*/
+static int yy_pop_parser_stack(yyParser *pParser){
+  YYCODETYPE yymajor;
+  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
+
+  if( pParser->yyidx<0 ) return 0;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE && pParser->yyidx>=0 ){
+    fprintf(yyTraceFILE,"%sPopping %s\n",
+      yyTracePrompt,
+      yyTokenName[yytos->major]);
+  }
+#endif
+  yymajor = yytos->major;
+  yy_destructor(pParser, yymajor, &yytos->minor);
+  pParser->yyidx--;
+  return yymajor;
+}
+
+/* 
+** Deallocate and destroy a parser.  Destructors are all called for
+** all stack elements before shutting the parser down.
+**
+** Inputs:
+** <ul>
+** <li>  A pointer to the parser.  This should be a pointer
+**       obtained from ParseHLSLAlloc.
+** <li>  A pointer to a function used to reclaim memory obtained
+**       from malloc.
+** </ul>
+*/
+#if __MOJOSHADER__
+static
+#endif
+void ParseHLSLFree(
+  void *p,                    /* The parser to be deleted */
+#if __MOJOSHADER__
+  void (*freeProc)(void*,void*),     /* Function used to reclaim memory */
+  void *malloc_data
+#else
+  void (*freeProc)(void*)     /* Function used to reclaim memory */
+#endif
+){
+  yyParser *pParser = (yyParser*)p;
+  if( pParser==0 ) return;
+  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
+#if YYSTACKDEPTH<=0
+  free(pParser->yystack);
+#endif
+#if __MOJOSHADER__
+  (*freeProc)((void*)pParser, malloc_data);
+#else
+  (*freeProc)((void*)pParser);
+#endif
+}
+
+/*
+** Return the peak depth of the stack for a parser.
+*/
+#ifdef YYTRACKMAXSTACKDEPTH
+static int ParseHLSLStackPeak(void *p){
+  yyParser *pParser = (yyParser*)p;
+  return pParser->yyidxMax;
+}
+#endif
+
+/*
+** Find the appropriate action for a parser given the terminal
+** look-ahead token iLookAhead.
+**
+** If the look-ahead token is YYNOCODE, then check to see if the action is
+** independent of the look-ahead.  If it is, return the action, otherwise
+** return YY_NO_ACTION.
+*/
+static int yy_find_shift_action(
+  yyParser *pParser,        /* The parser */
+  YYCODETYPE iLookAhead     /* The look-ahead token */
+){
+  int i;
+  int stateno = pParser->yystack[pParser->yyidx].stateno;
+ 
+  if( stateno>YY_SHIFT_COUNT
+   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
+    return yy_default[stateno];
+  }
+  assert( iLookAhead!=YYNOCODE );
+  i += iLookAhead;
+  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
+    if( iLookAhead>0 ){
+#ifdef YYFALLBACK
+      YYCODETYPE iFallback;            /* Fallback token */
+      if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
+             && (iFallback = yyFallback[iLookAhead])!=0 ){
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+        if( yyTraceFILE ){
+          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
+             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
+        }
+#endif
+        return yy_find_shift_action(pParser, iFallback);
+      }
+#endif
+#ifdef YYWILDCARD
+      {
+        int j = i - iLookAhead + YYWILDCARD;
+        if( 
+#if YY_SHIFT_MIN+YYWILDCARD<0
+          j>=0 &&
+#endif
+#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
+          j<YY_ACTTAB_COUNT &&
+#endif
+          yy_lookahead[j]==YYWILDCARD
+        ){
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+          if( yyTraceFILE ){
+            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
+               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
+          }
+#endif /* NDEBUG */
+          return yy_action[j];
+        }
+      }
+#endif /* YYWILDCARD */
+    }
+    return yy_default[stateno];
+  }else{
+    return yy_action[i];
+  }
+}
+
+/*
+** Find the appropriate action for a parser given the non-terminal
+** look-ahead token iLookAhead.
+**
+** If the look-ahead token is YYNOCODE, then check to see if the action is
+** independent of the look-ahead.  If it is, return the action, otherwise
+** return YY_NO_ACTION.
+*/
+static int yy_find_reduce_action(
+  int stateno,              /* Current state number */
+  YYCODETYPE iLookAhead     /* The look-ahead token */
+){
+  int i;
+#ifdef YYERRORSYMBOL
+  if( stateno>YY_REDUCE_COUNT ){
+    return yy_default[stateno];
+  }
+#else
+  assert( stateno<=YY_REDUCE_COUNT );
+#endif
+  i = yy_reduce_ofst[stateno];
+  assert( i!=YY_REDUCE_USE_DFLT );
+  assert( iLookAhead!=YYNOCODE );
+  i += iLookAhead;
+#ifdef YYERRORSYMBOL
+  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
+    return yy_default[stateno];
+  }
+#else
+  assert( i>=0 && i<YY_ACTTAB_COUNT );
+  assert( yy_lookahead[i]==iLookAhead );
+#endif
+  return yy_action[i];
+}
+
+/*
+** The following routine is called if the stack overflows.
+*/
+static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
+   ParseHLSLARG_FETCH;
+   yypParser->yyidx--;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+   if( yyTraceFILE ){
+     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
+   }
+#endif
+   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
+   /* Here code is inserted which will execute if the parser
+   ** stack every overflows */
+#line 47 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+
+    // !!! FIXME: make this a proper fail() function.
+    fail(ctx, "Giving up. Parser stack overflow");
+#line 2364 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+   ParseHLSLARG_STORE; /* Suppress warning about unused %extra_argument var */
+}
+
+/*
+** Perform a shift action.
+*/
+static void yy_shift(
+  yyParser *yypParser,          /* The parser to be shifted */
+  int yyNewState,               /* The new state to shift in */
+  int yyMajor,                  /* The major token to shift in */
+  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
+){
+  yyStackEntry *yytos;
+  yypParser->yyidx++;
+#ifdef YYTRACKMAXSTACKDEPTH
+  if( yypParser->yyidx>yypParser->yyidxMax ){
+    yypParser->yyidxMax = yypParser->yyidx;
+  }
+#endif
+#if YYSTACKDEPTH>0 
+  if( yypParser->yyidx>=YYSTACKDEPTH ){
+    yyStackOverflow(yypParser, yypMinor);
+    return;
+  }
+#else
+  if( yypParser->yyidx>=yypParser->yystksz ){
+    yyGrowStack(yypParser);
+    if( yypParser->yyidx>=yypParser->yystksz ){
+      yyStackOverflow(yypParser, yypMinor);
+      return;
+    }
+  }
+#endif
+  yytos = &yypParser->yystack[yypParser->yyidx];
+  yytos->stateno = (YYACTIONTYPE)yyNewState;
+  yytos->major = (YYCODETYPE)yyMajor;
+  yytos->minor = *yypMinor;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE && yypParser->yyidx>0 ){
+    int i;
+    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
+    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
+    for(i=1; i<=yypParser->yyidx; i++)
+      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
+    fprintf(yyTraceFILE,"\n");
+  }
+#endif
+}
+
+/* The following table contains information about every rule that
+** is used during the reduce.
+*/
+static const struct {
+  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
+  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
+} yyRuleInfo[] = {
+  { 124, 1 },
+  { 125, 1 },
+  { 125, 2 },
+  { 126, 1 },
+  { 126, 2 },
+  { 126, 2 },
+  { 126, 1 },
+  { 126, 2 },
+  { 130, 4 },
+  { 130, 3 },
+  { 128, 3 },
+  { 128, 2 },
+  { 128, 2 },
+  { 128, 1 },
+  { 135, 5 },
+  { 135, 5 },
+  { 134, 1 },
+  { 137, 1 },
+  { 137, 1 },
+  { 137, 0 },
+  { 138, 1 },
+  { 138, 3 },
+  { 139, 6 },
+  { 139, 5 },
+  { 139, 5 },
+  { 139, 4 },
+  { 139, 5 },
+  { 139, 4 },
+  { 139, 4 },
+  { 139, 3 },
+  { 139, 5 },
+  { 139, 4 },
+  { 139, 4 },
+  { 139, 3 },
+  { 139, 4 },
+  { 139, 3 },
+  { 139, 3 },
+  { 139, 2 },
+  { 140, 1 },
+  { 140, 1 },
+  { 140, 1 },
+  { 140, 2 },
+  { 140, 2 },
+  { 140, 1 },
+  { 136, 2 },
+  { 141, 1 },
+  { 141, 1 },
+  { 141, 1 },
+  { 141, 1 },
+  { 141, 1 },
+  { 127, 4 },
+  { 127, 3 },
+  { 127, 3 },
+  { 143, 1 },
+  { 143, 2 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 145, 1 },
+  { 144, 1 },
+  { 144, 3 },
+  { 146, 5 },
+  { 146, 4 },
+  { 146, 4 },
+  { 146, 3 },
+  { 146, 4 },
+  { 146, 3 },
+  { 146, 3 },
+  { 146, 2 },
+  { 146, 4 },
+  { 146, 3 },
+  { 146, 3 },
+  { 146, 2 },
+  { 146, 3 },
+  { 146, 2 },
+  { 146, 2 },
+  { 146, 1 },
+  { 131, 4 },
+  { 149, 2 },
+  { 150, 1 },
+  { 150, 2 },
+  { 151, 2 },
+  { 151, 1 },
+  { 152, 3 },
+  { 153, 1 },
+  { 153, 2 },
+  { 153, 3 },
+  { 148, 2 },
+  { 148, 2 },
+  { 148, 1 },
+  { 148, 1 },
+  { 133, 3 },
+  { 133, 4 },
+  { 133, 1 },
+  { 154, 7 },
+  { 154, 5 },
+  { 155, 5 },
+  { 147, 3 },
+  { 157, 1 },
+  { 157, 2 },
+  { 158, 3 },
+  { 160, 1 },
+  { 160, 3 },
+  { 160, 3 },
+  { 161, 3 },
+  { 142, 2 },
+  { 142, 2 },
+  { 162, 1 },
+  { 162, 1 },
+  { 162, 1 },
+  { 162, 1 },
+  { 162, 1 },
+  { 132, 1 },
+  { 132, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 165, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 1 },
+  { 159, 2 },
+  { 159, 2 },
+  { 166, 4 },
+  { 166, 4 },
+  { 166, 4 },
+  { 166, 4 },
+  { 166, 4 },
+  { 166, 4 },
+  { 166, 5 },
+  { 166, 5 },
+  { 163, 6 },
+  { 164, 8 },
+  { 129, 2 },
+  { 129, 3 },
+  { 167, 1 },
+  { 167, 2 },
+  { 169, 1 },
+  { 169, 4 },
+  { 169, 1 },
+  { 169, 1 },
+  { 169, 1 },
+  { 169, 1 },
+  { 168, 2 },
+  { 168, 2 },
+  { 168, 2 },
+  { 168, 4 },
+  { 168, 1 },
+  { 168, 2 },
+  { 168, 8 },
+  { 168, 5 },
+  { 168, 5 },
+  { 168, 7 },
+  { 168, 7 },
+  { 168, 1 },
+  { 168, 1 },
+  { 168, 2 },
+  { 168, 2 },
+  { 168, 3 },
+  { 168, 1 },
+  { 168, 1 },
+  { 171, 7 },
+  { 171, 4 },
+  { 171, 4 },
+  { 171, 1 },
+  { 175, 2 },
+  { 176, 7 },
+  { 176, 4 },
+  { 176, 4 },
+  { 176, 1 },
+  { 177, 8 },
+  { 177, 5 },
+  { 177, 6 },
+  { 177, 6 },
+  { 177, 7 },
+  { 177, 6 },
+  { 177, 7 },
+  { 177, 7 },
+  { 177, 7 },
+  { 177, 5 },
+  { 177, 6 },
+  { 177, 6 },
+  { 170, 7 },
+  { 170, 4 },
+  { 170, 4 },
+  { 170, 1 },
+  { 172, 4 },
+  { 172, 4 },
+  { 172, 4 },
+  { 172, 4 },
+  { 172, 4 },
+  { 172, 4 },
+  { 172, 1 },
+  { 173, 4 },
+  { 173, 4 },
+  { 173, 4 },
+  { 173, 4 },
+  { 173, 1 },
+  { 174, 1 },
+  { 174, 2 },
+  { 178, 4 },
+  { 178, 3 },
+  { 178, 3 },
+  { 178, 2 },
+  { 179, 1 },
+  { 179, 1 },
+  { 179, 1 },
+  { 179, 1 },
+  { 179, 1 },
+  { 179, 1 },
+  { 179, 3 },
+  { 180, 1 },
+  { 180, 4 },
+  { 180, 2 },
+  { 180, 2 },
+  { 180, 3 },
+  { 180, 2 },
+  { 180, 2 },
+  { 181, 2 },
+  { 181, 3 },
+  { 182, 1 },
+  { 182, 3 },
+  { 184, 1 },
+  { 184, 2 },
+  { 184, 2 },
+  { 184, 2 },
+  { 184, 2 },
+  { 184, 2 },
+  { 184, 2 },
+  { 185, 1 },
+  { 185, 4 },
+  { 186, 1 },
+  { 186, 3 },
+  { 186, 3 },
+  { 186, 3 },
+  { 187, 1 },
+  { 187, 3 },
+  { 187, 3 },
+  { 188, 1 },
+  { 188, 3 },
+  { 188, 3 },
+  { 189, 1 },
+  { 189, 3 },
+  { 189, 3 },
+  { 189, 3 },
+  { 189, 3 },
+  { 190, 1 },
+  { 190, 3 },
+  { 190, 3 },
+  { 191, 1 },
+  { 191, 3 },
+  { 192, 1 },
+  { 192, 3 },
+  { 193, 1 },
+  { 193, 3 },
+  { 194, 1 },
+  { 194, 3 },
+  { 195, 1 },
+  { 195, 3 },
+  { 196, 1 },
+  { 196, 5 },
+  { 183, 1 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 183, 3 },
+  { 156, 1 },
+  { 156, 3 },
+};
+
+static void yy_accept(yyParser*);  /* Forward Declaration */
+
+/*
+** Perform a reduce action and the shift that must immediately
+** follow the reduce.
+*/
+static void yy_reduce(
+  yyParser *yypParser,         /* The parser */
+  int yyruleno                 /* Number of the rule by which to reduce */
+){
+  int yygoto;                     /* The next state */
+  int yyact;                      /* The next action */
+  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
+  yyStackEntry *yymsp;            /* The top of the parser's stack */
+  int yysize;                     /* Amount to pop the stack */
+  ParseHLSLARG_FETCH;
+  yymsp = &yypParser->yystack[yypParser->yyidx];
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE && yyruleno>=0
+        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
+    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
+      yyRuleName[yyruleno]);
+  }
+#endif /* NDEBUG */
+
+  /* Silence complaints from purify about yygotominor being uninitialized
+  ** in some cases when it is copied into the stack after the following
+  ** switch.  yygotominor is uninitialized when a rule reduces that does
+  ** not set the value of its left-hand side nonterminal.  Leaving the
+  ** value of the nonterminal uninitialized is utterly harmless as long
+  ** as the value is never used.  So really the only thing this code
+  ** accomplishes is to quieten purify.  
+  **
+  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
+  ** without this code, their parser segfaults.  I'm not sure what there
+  ** parser is doing to make this happen.  This is the second bug report
+  ** from wireshark this week.  Clearly they are stressing Lemon in ways
+  ** that it has not been previously stressed...  (SQLite ticket #2172)
+  */
+  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
+  yygotominor = yyzerominor;
+
+
+  switch( yyruleno ){
+  /* Beginning here are the reduction cases.  A typical example
+  ** follows:
+  **   case 0:
+  **  #line <lineno> <grammarfile>
+  **     { ... }           // User supplied code
+  **  #line <lineno> <thisfile>
+  **     break;
+  */
+      case 0: /* shader ::= compilation_units */
+#line 78 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ assert(ctx->ast == NULL); REVERSE_LINKED_LIST(MOJOSHADER_astCompilationUnit, yymsp[0].minor.yy139); ctx->ast = (MOJOSHADER_astNode *) yymsp[0].minor.yy139; }
+#line 2766 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 1: /* compilation_units ::= compilation_unit */
+#line 82 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = yymsp[0].minor.yy139; }
+#line 2771 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 2: /* compilation_units ::= compilation_units compilation_unit */
+#line 83 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ if (yymsp[0].minor.yy139) { yymsp[0].minor.yy139->next = yymsp[-1].minor.yy139; yygotominor.yy139 = yymsp[0].minor.yy139; } }
+#line 2776 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 3: /* compilation_unit ::= variable_declaration */
+#line 88 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = new_global_variable(ctx, yymsp[0].minor.yy24); }
+#line 2781 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 4: /* compilation_unit ::= function_signature SEMICOLON */
+#line 89 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = new_function(ctx, yymsp[-1].minor.yy364, NULL); }
+#line 2786 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 5: /* compilation_unit ::= function_signature statement_block */
+#line 90 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = new_function(ctx, yymsp[-1].minor.yy364, yymsp[0].minor.yy233); }
+#line 2791 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 6: /* compilation_unit ::= typedef */
+#line 91 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = new_global_typedef(ctx, yymsp[0].minor.yy71); }
+#line 2796 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 7: /* compilation_unit ::= struct_declaration SEMICOLON */
+#line 92 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy139 = new_global_struct(ctx, yymsp[-1].minor.yy249); }
+#line 2801 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 8: /* typedef ::= TYPEDEF CONST datatype scalar_or_array */
+#line 98 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy71 = new_typedef(ctx, 1, yymsp[-1].minor.yy37, yymsp[0].minor.yy380); push_usertype(ctx, yymsp[0].minor.yy380->identifier, yygotominor.yy71->datatype); }
+#line 2806 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 9: /* typedef ::= TYPEDEF datatype scalar_or_array */
+#line 99 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy71 = new_typedef(ctx, 0, yymsp[-1].minor.yy37, yymsp[0].minor.yy380); push_usertype(ctx, yymsp[0].minor.yy380->identifier, yygotominor.yy71->datatype); }
+#line 2811 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 10: /* function_signature ::= function_storageclass function_details semantic */
+#line 103 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = yymsp[-1].minor.yy364; yygotominor.yy364->storage_class = yymsp[-2].minor.yy175; yygotominor.yy364->semantic = yymsp[0].minor.yy306; }
+#line 2816 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 11: /* function_signature ::= function_storageclass function_details */
+#line 104 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = yymsp[0].minor.yy364; yygotominor.yy364->storage_class = yymsp[-1].minor.yy175; }
+#line 2821 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 12: /* function_signature ::= function_details semantic */
+#line 105 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = yymsp[-1].minor.yy364; yygotominor.yy364->semantic = yymsp[0].minor.yy306; }
+#line 2826 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 13: /* function_signature ::= function_details */
+#line 106 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = yymsp[0].minor.yy364; }
+#line 2831 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 14: /* function_details ::= datatype IDENTIFIER LPAREN function_parameters RPAREN */
+#line 110 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = new_function_signature(ctx, yymsp[-4].minor.yy37, yymsp[-3].minor.yy0.string, yymsp[-1].minor.yy307); }
+#line 2836 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 15: /* function_details ::= VOID IDENTIFIER LPAREN function_parameters RPAREN */
+#line 111 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy364 = new_function_signature(ctx, NULL, yymsp[-3].minor.yy0.string, yymsp[-1].minor.yy307); }
+#line 2841 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 16: /* function_storageclass ::= INLINE */
+#line 121 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy175 = MOJOSHADER_AST_FNSTORECLS_INLINE; }
+#line 2846 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 17: /* function_parameters ::= VOID */
+      case 19: /* function_parameters ::= */ yytestcase(yyruleno==19);
+#line 125 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = NULL; }
+#line 2852 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 18: /* function_parameters ::= function_parameter_list */
+#line 126 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astFunctionParameters, yymsp[0].minor.yy307); yygotominor.yy307 = yymsp[0].minor.yy307; }
+#line 2857 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 20: /* function_parameter_list ::= function_parameter */
+#line 131 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = yymsp[0].minor.yy307; }
+#line 2862 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 21: /* function_parameter_list ::= function_parameter_list COMMA function_parameter */
+#line 132 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yymsp[0].minor.yy307->next = yymsp[-2].minor.yy307; yygotominor.yy307 = yymsp[0].minor.yy307; }
+#line 2867 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 22: /* function_parameter ::= input_modifier datatype IDENTIFIER semantic interpolation_mod initializer */
+#line 138 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-5].minor.yy75, yymsp[-4].minor.yy37, yymsp[-3].minor.yy0.string, yymsp[-2].minor.yy306, yymsp[-1].minor.yy111, yymsp[0].minor.yy322); }
+#line 2872 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 23: /* function_parameter ::= input_modifier datatype IDENTIFIER semantic interpolation_mod */
+#line 139 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-4].minor.yy75, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, yymsp[-1].minor.yy306, yymsp[0].minor.yy111, NULL); }
+#line 2877 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 24: /* function_parameter ::= input_modifier datatype IDENTIFIER semantic initializer */
+#line 140 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-4].minor.yy75, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, yymsp[-1].minor.yy306, MOJOSHADER_AST_INTERPMOD_NONE, yymsp[0].minor.yy322); }
+#line 2882 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 25: /* function_parameter ::= input_modifier datatype IDENTIFIER semantic */
+#line 141 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-3].minor.yy75, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, yymsp[0].minor.yy306, MOJOSHADER_AST_INTERPMOD_NONE, NULL); }
+#line 2887 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 26: /* function_parameter ::= input_modifier datatype IDENTIFIER interpolation_mod initializer */
+#line 142 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-4].minor.yy75, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, NULL, yymsp[-1].minor.yy111, yymsp[0].minor.yy322); }
+#line 2892 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 27: /* function_parameter ::= input_modifier datatype IDENTIFIER interpolation_mod */
+#line 143 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-3].minor.yy75, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, NULL, yymsp[0].minor.yy111, NULL); }
+#line 2897 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 28: /* function_parameter ::= input_modifier datatype IDENTIFIER initializer */
+#line 144 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-3].minor.yy75, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, NULL, MOJOSHADER_AST_INTERPMOD_NONE, yymsp[0].minor.yy322); }
+#line 2902 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 29: /* function_parameter ::= input_modifier datatype IDENTIFIER */
+#line 145 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, yymsp[-2].minor.yy75, yymsp[-1].minor.yy37, yymsp[0].minor.yy0.string, NULL, MOJOSHADER_AST_INTERPMOD_NONE, NULL); }
+#line 2907 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 30: /* function_parameter ::= datatype IDENTIFIER semantic interpolation_mod initializer */
+#line 146 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-4].minor.yy37, yymsp[-3].minor.yy0.string, yymsp[-2].minor.yy306, yymsp[-1].minor.yy111, yymsp[0].minor.yy322); }
+#line 2912 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 31: /* function_parameter ::= datatype IDENTIFIER semantic interpolation_mod */
+#line 147 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, yymsp[-1].minor.yy306, yymsp[0].minor.yy111, NULL); }
+#line 2917 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 32: /* function_parameter ::= datatype IDENTIFIER semantic initializer */
+#line 148 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, yymsp[-1].minor.yy306, MOJOSHADER_AST_INTERPMOD_NONE, yymsp[0].minor.yy322); }
+#line 2922 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 33: /* function_parameter ::= datatype IDENTIFIER semantic */
+#line 149 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, yymsp[0].minor.yy306, MOJOSHADER_AST_INTERPMOD_NONE, NULL); }
+#line 2927 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 34: /* function_parameter ::= datatype IDENTIFIER interpolation_mod initializer */
+#line 150 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-3].minor.yy37, yymsp[-2].minor.yy0.string, NULL, yymsp[-1].minor.yy111, yymsp[0].minor.yy322); }
+#line 2932 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 35: /* function_parameter ::= datatype IDENTIFIER interpolation_mod */
+#line 151 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, NULL, yymsp[0].minor.yy111, NULL); }
+#line 2937 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 36: /* function_parameter ::= datatype IDENTIFIER initializer */
+#line 152 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-2].minor.yy37, yymsp[-1].minor.yy0.string, NULL, MOJOSHADER_AST_INTERPMOD_NONE, yymsp[0].minor.yy322); }
+#line 2942 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 37: /* function_parameter ::= datatype IDENTIFIER */
+#line 153 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy307 = new_function_param(ctx, MOJOSHADER_AST_INPUTMOD_NONE, yymsp[-1].minor.yy37, yymsp[0].minor.yy0.string, NULL, MOJOSHADER_AST_INTERPMOD_NONE, NULL); }
+#line 2947 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 38: /* input_modifier ::= IN */
+#line 156 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy75 = MOJOSHADER_AST_INPUTMOD_IN; }
+#line 2952 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 39: /* input_modifier ::= INOUT */
+      case 41: /* input_modifier ::= IN OUT */ yytestcase(yyruleno==41);
+      case 42: /* input_modifier ::= OUT IN */ yytestcase(yyruleno==42);
+#line 157 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy75 = MOJOSHADER_AST_INPUTMOD_INOUT; }
+#line 2959 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 40: /* input_modifier ::= OUT */
+#line 158 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy75 = MOJOSHADER_AST_INPUTMOD_OUT; }
+#line 2964 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 43: /* input_modifier ::= UNIFORM */
+#line 161 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy75 = MOJOSHADER_AST_INPUTMOD_UNIFORM; }
+#line 2969 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 44: /* semantic ::= COLON IDENTIFIER */
+#line 164 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy306 = yymsp[0].minor.yy0.string; }
+#line 2974 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 45: /* interpolation_mod ::= LINEAR */
+#line 168 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy111 = MOJOSHADER_AST_INTERPMOD_LINEAR; }
+#line 2979 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 46: /* interpolation_mod ::= CENTROID */
+#line 169 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy111 = MOJOSHADER_AST_INTERPMOD_CENTROID; }
+#line 2984 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 47: /* interpolation_mod ::= NOINTERPOLATION */
+#line 170 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy111 = MOJOSHADER_AST_INTERPMOD_NOINTERPOLATION; }
+#line 2989 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 48: /* interpolation_mod ::= NOPERSPECTIVE */
+#line 171 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy111 = MOJOSHADER_AST_INTERPMOD_NOPERSPECTIVE; }
+#line 2994 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 49: /* interpolation_mod ::= SAMPLE */
+#line 172 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy111 = MOJOSHADER_AST_INTERPMOD_SAMPLE; }
+#line 2999 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 50: /* variable_declaration ::= variable_attribute_list datatype variable_declaration_details_list SEMICOLON */
+#line 176 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astVariableDeclaration, yymsp[-1].minor.yy24); yygotominor.yy24 = yymsp[-1].minor.yy24; yygotominor.yy24->attributes = yymsp[-3].minor.yy270; yygotominor.yy24->datatype = yymsp[-2].minor.yy37; }
+#line 3004 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 51: /* variable_declaration ::= datatype variable_declaration_details_list SEMICOLON */
+#line 177 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astVariableDeclaration, yymsp[-1].minor.yy24); yygotominor.yy24 = yymsp[-1].minor.yy24; yygotominor.yy24->datatype = yymsp[-2].minor.yy37; }
+#line 3009 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 52: /* variable_declaration ::= struct_declaration variable_declaration_details_list SEMICOLON */
+#line 179 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astVariableDeclaration, yymsp[-1].minor.yy24); yygotominor.yy24 = yymsp[-1].minor.yy24; yygotominor.yy24->anonymous_datatype = yymsp[-2].minor.yy249; }
+#line 3014 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 53: /* variable_attribute_list ::= variable_attribute */
+#line 182 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = yymsp[0].minor.yy270; }
+#line 3019 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 54: /* variable_attribute_list ::= variable_attribute_list variable_attribute */
+#line 183 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = yymsp[-1].minor.yy270 | yymsp[0].minor.yy270; }
+#line 3024 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 55: /* variable_attribute ::= EXTERN */
+#line 186 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_EXTERN; }
+#line 3029 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 56: /* variable_attribute ::= NOINTERPOLATION */
+#line 187 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_NOINTERPOLATION; }
+#line 3034 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 57: /* variable_attribute ::= SHARED */
+#line 188 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_SHARED; }
+#line 3039 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 58: /* variable_attribute ::= STATIC */
+#line 189 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_STATIC; }
+#line 3044 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 59: /* variable_attribute ::= UNIFORM */
+#line 190 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_UNIFORM; }
+#line 3049 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 60: /* variable_attribute ::= VOLATILE */
+#line 191 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_VOLATILE; }
+#line 3054 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 61: /* variable_attribute ::= CONST */
+#line 192 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_CONST; }
+#line 3059 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 62: /* variable_attribute ::= ROWMAJOR */
+#line 193 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_ROWMAJOR; }
+#line 3064 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 63: /* variable_attribute ::= COLUMNMAJOR */
+#line 194 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_VARATTR_COLUMNMAJOR; }
+#line 3069 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 64: /* variable_declaration_details_list ::= variable_declaration_details */
+#line 198 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = yymsp[0].minor.yy24; }
+#line 3074 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 65: /* variable_declaration_details_list ::= variable_declaration_details_list COMMA variable_declaration_details */
+#line 199 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = yymsp[0].minor.yy24; yygotominor.yy24->next = yymsp[-2].minor.yy24; }
+#line 3079 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 66: /* variable_declaration_details ::= scalar_or_array semantic annotations initializer variable_lowlevel */
+#line 203 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-4].minor.yy380, yymsp[-3].minor.yy306, yymsp[-2].minor.yy268, yymsp[-1].minor.yy322, yymsp[0].minor.yy82); }
+#line 3084 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 67: /* variable_declaration_details ::= scalar_or_array semantic annotations initializer */
+#line 204 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-3].minor.yy380, yymsp[-2].minor.yy306, yymsp[-1].minor.yy268, yymsp[0].minor.yy322, NULL); }
+#line 3089 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 68: /* variable_declaration_details ::= scalar_or_array semantic annotations variable_lowlevel */
+#line 205 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-3].minor.yy380, yymsp[-2].minor.yy306, yymsp[-1].minor.yy268, NULL, yymsp[0].minor.yy82); }
+#line 3094 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 69: /* variable_declaration_details ::= scalar_or_array semantic annotations */
+#line 206 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, yymsp[-1].minor.yy306, yymsp[0].minor.yy268, NULL, NULL); }
+#line 3099 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 70: /* variable_declaration_details ::= scalar_or_array semantic initializer variable_lowlevel */
+#line 207 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-3].minor.yy380, yymsp[-2].minor.yy306, NULL, yymsp[-1].minor.yy322, yymsp[0].minor.yy82); }
+#line 3104 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 71: /* variable_declaration_details ::= scalar_or_array semantic initializer */
+#line 208 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, yymsp[-1].minor.yy306, NULL, yymsp[0].minor.yy322, NULL); }
+#line 3109 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 72: /* variable_declaration_details ::= scalar_or_array semantic variable_lowlevel */
+#line 209 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, yymsp[-1].minor.yy306, NULL, NULL, yymsp[0].minor.yy82); }
+#line 3114 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 73: /* variable_declaration_details ::= scalar_or_array semantic */
+#line 210 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-1].minor.yy380, yymsp[0].minor.yy306, NULL, NULL, NULL); }
+#line 3119 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 74: /* variable_declaration_details ::= scalar_or_array annotations initializer variable_lowlevel */
+#line 211 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-3].minor.yy380, NULL, yymsp[-2].minor.yy268, yymsp[-1].minor.yy322, yymsp[0].minor.yy82); }
+#line 3124 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 75: /* variable_declaration_details ::= scalar_or_array annotations initializer */
+#line 212 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, NULL, yymsp[-1].minor.yy268, yymsp[0].minor.yy322, NULL); }
+#line 3129 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 76: /* variable_declaration_details ::= scalar_or_array annotations variable_lowlevel */
+#line 213 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, NULL, yymsp[-1].minor.yy268, NULL, yymsp[0].minor.yy82); }
+#line 3134 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 77: /* variable_declaration_details ::= scalar_or_array annotations */
+#line 214 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-1].minor.yy380, NULL, yymsp[0].minor.yy268, NULL, NULL); }
+#line 3139 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 78: /* variable_declaration_details ::= scalar_or_array initializer variable_lowlevel */
+#line 215 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-2].minor.yy380, NULL, NULL, yymsp[-1].minor.yy322, yymsp[0].minor.yy82); }
+#line 3144 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 79: /* variable_declaration_details ::= scalar_or_array initializer */
+#line 216 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-1].minor.yy380, NULL, NULL, yymsp[0].minor.yy322, NULL); }
+#line 3149 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 80: /* variable_declaration_details ::= scalar_or_array variable_lowlevel */
+#line 217 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[-1].minor.yy380, NULL, NULL, NULL, yymsp[0].minor.yy82); }
+#line 3154 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 81: /* variable_declaration_details ::= scalar_or_array */
+#line 218 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy24 = new_variable_declaration(ctx, yymsp[0].minor.yy380, NULL, NULL, NULL, NULL); }
+#line 3159 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 82: /* struct_declaration ::= struct_intro LBRACE struct_member_list RBRACE */
+#line 225 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astStructMembers, yymsp[-1].minor.yy346); yygotominor.yy249 = new_struct_declaration(ctx, yymsp[-3].minor.yy306, yymsp[-1].minor.yy346); }
+#line 3164 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 83: /* struct_intro ::= STRUCT IDENTIFIER */
+#line 229 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy306 = yymsp[0].minor.yy0.string; push_usertype(ctx, yygotominor.yy306, &ctx->dt_none); }
+#line 3169 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 84: /* struct_member_list ::= struct_member */
+      case 87: /* struct_member ::= struct_member_details */ yytestcase(yyruleno==87);
+#line 233 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy346 = yymsp[0].minor.yy346; }
+#line 3175 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 85: /* struct_member_list ::= struct_member_list struct_member */
+#line 234 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy346 = yymsp[0].minor.yy346; MOJOSHADER_astStructMembers *i = yygotominor.yy346; while (i->next) { i = i->next; } i->next = yymsp[-1].minor.yy346; }
+#line 3180 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 86: /* struct_member ::= interpolation_mod struct_member_details */
+#line 238 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ MOJOSHADER_astStructMembers *i = yymsp[0].minor.yy346; yygotominor.yy346 = yymsp[0].minor.yy346; while (i) { i->interpolation_mod = yymsp[-1].minor.yy111; i = i->next; } }
+#line 3185 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 88: /* struct_member_details ::= datatype struct_member_item_list SEMICOLON */
+#line 243 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ MOJOSHADER_astStructMembers *i = yymsp[-1].minor.yy346; yygotominor.yy346 = yymsp[-1].minor.yy346; while (i) { i->datatype = yymsp[-2].minor.yy37; i = i->next; } }
+#line 3190 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 89: /* struct_member_item_list ::= scalar_or_array */
+#line 247 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy346 = new_struct_member(ctx, yymsp[0].minor.yy380, NULL); }
+#line 3195 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 90: /* struct_member_item_list ::= scalar_or_array semantic */
+#line 248 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy346 = new_struct_member(ctx, yymsp[-1].minor.yy380, yymsp[0].minor.yy306); }
+#line 3200 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 91: /* struct_member_item_list ::= struct_member_item_list COMMA IDENTIFIER */
+#line 249 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy346 = new_struct_member(ctx, new_scalar_or_array(ctx, yymsp[0].minor.yy0.string, 0, NULL), NULL); yygotominor.yy346->next = yymsp[-2].minor.yy346; yygotominor.yy346->semantic = yymsp[-2].minor.yy346->semantic; }
+#line 3205 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 92: /* variable_lowlevel ::= packoffset register */
+#line 253 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy82 = new_variable_lowlevel(ctx, yymsp[-1].minor.yy8, yymsp[0].minor.yy306); }
+#line 3210 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 93: /* variable_lowlevel ::= register packoffset */
+#line 254 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy82 = new_variable_lowlevel(ctx, yymsp[0].minor.yy8, yymsp[-1].minor.yy306); }
+#line 3215 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 94: /* variable_lowlevel ::= packoffset */
+#line 255 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy82 = new_variable_lowlevel(ctx, yymsp[0].minor.yy8, NULL); }
+#line 3220 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 95: /* variable_lowlevel ::= register */
+#line 256 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy82 = new_variable_lowlevel(ctx, NULL, yymsp[0].minor.yy306); }
+#line 3225 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 96: /* scalar_or_array ::= IDENTIFIER LBRACKET RBRACKET */
+#line 261 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy380 = new_scalar_or_array(ctx, yymsp[-2].minor.yy0.string, 1, NULL); }
+#line 3230 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 97: /* scalar_or_array ::= IDENTIFIER LBRACKET expression RBRACKET */
+#line 262 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy380 = new_scalar_or_array(ctx, yymsp[-3].minor.yy0.string, 1, yymsp[-1].minor.yy322); }
+#line 3235 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 98: /* scalar_or_array ::= IDENTIFIER */
+#line 263 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy380 = new_scalar_or_array(ctx, yymsp[0].minor.yy0.string, 0, NULL); }
+#line 3240 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 99: /* packoffset ::= COLON PACKOFFSET LPAREN IDENTIFIER DOT IDENTIFIER RPAREN */
+#line 267 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy8 = new_pack_offset(ctx, yymsp[-3].minor.yy0.string, yymsp[-1].minor.yy0.string); }
+#line 3245 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 100: /* packoffset ::= COLON PACKOFFSET LPAREN IDENTIFIER RPAREN */
+#line 268 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy8 = new_pack_offset(ctx, yymsp[-1].minor.yy0.string, NULL); }
+#line 3250 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 101: /* register ::= COLON REGISTER LPAREN IDENTIFIER RPAREN */
+#line 274 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy306 = yymsp[-1].minor.yy0.string; }
+#line 3255 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 102: /* annotations ::= LT annotation_list GT */
+#line 278 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astAnnotations, yymsp[-1].minor.yy268); yygotominor.yy268 = yymsp[-1].minor.yy268; }
+#line 3260 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 103: /* annotation_list ::= annotation */
+#line 282 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy268 = yymsp[0].minor.yy268; }
+#line 3265 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 104: /* annotation_list ::= annotation_list annotation */
+#line 283 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy268 = yymsp[0].minor.yy268; yygotominor.yy268->next = yymsp[-1].minor.yy268; }
+#line 3270 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 105: /* annotation ::= datatype_scalar initializer SEMICOLON */
+#line 288 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy268 = new_annotation(ctx, yymsp[-2].minor.yy37, yymsp[-1].minor.yy322); }
+#line 3275 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 106: /* initializer_block_list ::= expression */
+      case 110: /* initializer ::= ASSIGN initializer_block */ yytestcase(yyruleno==110);
+      case 111: /* initializer ::= ASSIGN expression */ yytestcase(yyruleno==111);
+      case 224: /* postfix_expr ::= primary_expr */ yytestcase(yyruleno==224);
+      case 235: /* unary_expr ::= postfix_expr */ yytestcase(yyruleno==235);
+      case 238: /* unary_expr ::= PLUS cast_expr */ yytestcase(yyruleno==238);
+      case 242: /* cast_expr ::= unary_expr */ yytestcase(yyruleno==242);
+      case 244: /* multiplicative_expr ::= cast_expr */ yytestcase(yyruleno==244);
+      case 248: /* additive_expr ::= multiplicative_expr */ yytestcase(yyruleno==248);
+      case 251: /* shift_expr ::= additive_expr */ yytestcase(yyruleno==251);
+      case 254: /* relational_expr ::= shift_expr */ yytestcase(yyruleno==254);
+      case 259: /* equality_expr ::= relational_expr */ yytestcase(yyruleno==259);
+      case 262: /* and_expr ::= equality_expr */ yytestcase(yyruleno==262);
+      case 264: /* exclusive_or_expr ::= and_expr */ yytestcase(yyruleno==264);
+      case 266: /* inclusive_or_expr ::= exclusive_or_expr */ yytestcase(yyruleno==266);
+      case 268: /* logical_and_expr ::= inclusive_or_expr */ yytestcase(yyruleno==268);
+      case 270: /* logical_or_expr ::= logical_and_expr */ yytestcase(yyruleno==270);
+      case 272: /* conditional_expr ::= logical_or_expr */ yytestcase(yyruleno==272);
+      case 274: /* assignment_expr ::= conditional_expr */ yytestcase(yyruleno==274);
+      case 286: /* expression ::= assignment_expr */ yytestcase(yyruleno==286);
+#line 292 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = yymsp[0].minor.yy322; }
+#line 3299 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 107: /* initializer_block_list ::= LBRACE initializer_block_list RBRACE */
+      case 109: /* initializer_block ::= LBRACE initializer_block_list RBRACE */ yytestcase(yyruleno==109);
+      case 223: /* primary_expr ::= LPAREN expression RPAREN */ yytestcase(yyruleno==223);
+#line 293 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = yymsp[-1].minor.yy322; }
+#line 3306 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 108: /* initializer_block_list ::= initializer_block_list COMMA initializer_block_list */
+      case 287: /* expression ::= expression COMMA assignment_expr */ yytestcase(yyruleno==287);
+#line 294 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_COMMA, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3312 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 112: /* intrinsic_datatype ::= datatype_vector */
+      case 113: /* intrinsic_datatype ::= datatype_matrix */ yytestcase(yyruleno==113);
+      case 114: /* intrinsic_datatype ::= datatype_scalar */ yytestcase(yyruleno==114);
+      case 115: /* intrinsic_datatype ::= datatype_sampler */ yytestcase(yyruleno==115);
+      case 116: /* intrinsic_datatype ::= datatype_buffer */ yytestcase(yyruleno==116);
+      case 117: /* datatype ::= intrinsic_datatype */ yytestcase(yyruleno==117);
+#line 306 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = yymsp[0].minor.yy37; }
+#line 3322 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 118: /* datatype ::= USERTYPE */
+#line 314 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = yymsp[0].minor.yy0.datatype; }
+#line 3327 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 119: /* datatype_sampler ::= SAMPLER */
+      case 121: /* datatype_sampler ::= SAMPLER2D */ yytestcase(yyruleno==121);
+#line 317 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_sampler2d; }
+#line 3333 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 120: /* datatype_sampler ::= SAMPLER1D */
+#line 318 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_sampler1d; }
+#line 3338 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 122: /* datatype_sampler ::= SAMPLER3D */
+#line 320 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_sampler3d; }
+#line 3343 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 123: /* datatype_sampler ::= SAMPLERCUBE */
+#line 321 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_samplercube; }
+#line 3348 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 124: /* datatype_sampler ::= SAMPLER_STATE */
+      case 125: /* datatype_sampler ::= SAMPLERSTATE */ yytestcase(yyruleno==125);
+#line 322 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_samplerstate; }
+#line 3354 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 126: /* datatype_sampler ::= SAMPLERCOMPARISONSTATE */
+#line 324 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_samplercompstate; }
+#line 3359 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 127: /* datatype_scalar ::= BOOL */
+#line 327 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_bool; }
+#line 3364 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 128: /* datatype_scalar ::= INT */
+#line 328 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_int; }
+#line 3369 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 129: /* datatype_scalar ::= UINT */
+#line 329 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_uint; }
+#line 3374 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 130: /* datatype_scalar ::= HALF */
+#line 330 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_half; }
+#line 3379 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 131: /* datatype_scalar ::= FLOAT */
+#line 331 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_float; }
+#line 3384 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 132: /* datatype_scalar ::= DOUBLE */
+#line 332 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_double; }
+#line 3389 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 133: /* datatype_scalar ::= STRING */
+#line 333 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_string; }
+#line 3394 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 134: /* datatype_scalar ::= SNORM FLOAT */
+#line 334 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_float_snorm; }
+#line 3399 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 135: /* datatype_scalar ::= UNORM FLOAT */
+#line 335 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_float_unorm; }
+#line 3404 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 136: /* datatype_buffer ::= BUFFER LT BOOL GT */
+#line 338 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_bool; }
+#line 3409 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 137: /* datatype_buffer ::= BUFFER LT INT GT */
+#line 339 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_int; }
+#line 3414 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 138: /* datatype_buffer ::= BUFFER LT UINT GT */
+#line 340 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_uint; }
+#line 3419 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 139: /* datatype_buffer ::= BUFFER LT HALF GT */
+#line 341 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_half; }
+#line 3424 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 140: /* datatype_buffer ::= BUFFER LT FLOAT GT */
+#line 342 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_float; }
+#line 3429 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 141: /* datatype_buffer ::= BUFFER LT DOUBLE GT */
+#line 343 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_double; }
+#line 3434 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 142: /* datatype_buffer ::= BUFFER LT SNORM FLOAT GT */
+#line 344 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_float_snorm; }
+#line 3439 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 143: /* datatype_buffer ::= BUFFER LT UNORM FLOAT GT */
+#line 345 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = &ctx->dt_buf_float_unorm; }
+#line 3444 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 144: /* datatype_vector ::= VECTOR LT datatype_scalar COMMA INT_CONSTANT GT */
+#line 348 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = new_datatype_vector(ctx, yymsp[-3].minor.yy37, (int) yymsp[-1].minor.yy0.i64); }
+#line 3449 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 145: /* datatype_matrix ::= MATRIX LT datatype_scalar COMMA INT_CONSTANT COMMA INT_CONSTANT GT */
+#line 351 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy37 = new_datatype_matrix(ctx, yymsp[-5].minor.yy37, (int) yymsp[-3].minor.yy0.i64, (int) yymsp[-1].minor.yy0.i64); }
+#line 3454 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 146: /* statement_block ::= LBRACE RBRACE */
+#line 355 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_block_statement(ctx, NULL); }
+#line 3459 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 147: /* statement_block ::= LBRACE statement_list RBRACE */
+#line 356 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astStatement, yymsp[-1].minor.yy233); yygotominor.yy233 = new_block_statement(ctx, yymsp[-1].minor.yy233); }
+#line 3464 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 148: /* statement_list ::= statement */
+      case 172: /* statement ::= statement_block */ yytestcase(yyruleno==172);
+      case 173: /* statement ::= for_statement */ yytestcase(yyruleno==173);
+#line 360 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = yymsp[0].minor.yy233; }
+#line 3471 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 149: /* statement_list ::= statement_list statement */
+#line 361 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = yymsp[0].minor.yy233; yygotominor.yy233->next = yymsp[-1].minor.yy233; }
+#line 3476 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 150: /* statement_attribute ::= ISOLATE */
+      case 151: /* statement_attribute ::= MAXINSTRUCTIONCOUNT LPAREN INT_CONSTANT RPAREN */ yytestcase(yyruleno==151);
+      case 152: /* statement_attribute ::= NOEXPRESSIONOPTIMIZATIONS */ yytestcase(yyruleno==152);
+      case 153: /* statement_attribute ::= REMOVEUNUSEDINPUTS */ yytestcase(yyruleno==153);
+      case 154: /* statement_attribute ::= UNUSED */ yytestcase(yyruleno==154);
+      case 155: /* statement_attribute ::= XPS */ yytestcase(yyruleno==155);
+      case 176: /* while_intro ::= LBRACKET LOOP RBRACKET WHILE */ yytestcase(yyruleno==176);
+      case 181: /* for_intro ::= LBRACKET LOOP RBRACKET FOR */ yytestcase(yyruleno==181);
+      case 197: /* do_intro ::= LBRACKET LOOP RBRACKET DO */ yytestcase(yyruleno==197);
+#line 367 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = 0; }
+#line 3489 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 156: /* statement ::= BREAK SEMICOLON */
+#line 376 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_break_statement(ctx); }
+#line 3494 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 157: /* statement ::= CONTINUE SEMICOLON */
+#line 377 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_continue_statement(ctx); }
+#line 3499 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 158: /* statement ::= DISCARD SEMICOLON */
+#line 378 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_discard_statement(ctx); }
+#line 3504 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 159: /* statement ::= LBRACKET statement_attribute RBRACKET statement_block */
+#line 379 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = yymsp[0].minor.yy233; /* !!! FIXME: yygotominor.yy233->attributes = yymsp[-2].minor.yy270;*/ yymsp[-2].minor.yy270 = 0; }
+#line 3509 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 160: /* statement ::= variable_declaration */
+#line 380 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_vardecl_statement(ctx, yymsp[0].minor.yy24); }
+#line 3514 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 161: /* statement ::= struct_declaration SEMICOLON */
+#line 381 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_struct_statement(ctx, yymsp[-1].minor.yy249); }
+#line 3519 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 162: /* statement ::= do_intro DO statement WHILE LPAREN expression RPAREN SEMICOLON */
+#line 382 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_do_statement(ctx, yymsp[-7].minor.yy270, yymsp[-5].minor.yy233, yymsp[-2].minor.yy322); }
+#line 3524 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 163: /* statement ::= while_intro LPAREN expression RPAREN statement */
+#line 383 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_while_statement(ctx, yymsp[-4].minor.yy270, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3529 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 164: /* statement ::= if_intro LPAREN expression RPAREN statement */
+#line 384 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_if_statement(ctx, yymsp[-4].minor.yy270, yymsp[-2].minor.yy322, yymsp[0].minor.yy233, NULL); }
+#line 3534 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 165: /* statement ::= if_intro LPAREN expression RPAREN statement ELSE statement */
+#line 385 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_if_statement(ctx, yymsp[-6].minor.yy270, yymsp[-4].minor.yy322, yymsp[-2].minor.yy233, yymsp[0].minor.yy233); }
+#line 3539 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 166: /* statement ::= switch_intro LPAREN expression RPAREN LBRACE switch_case_list RBRACE */
+#line 386 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astSwitchCases, yymsp[-1].minor.yy165); yygotominor.yy233 = new_switch_statement(ctx, yymsp[-6].minor.yy270, yymsp[-4].minor.yy322, yymsp[-1].minor.yy165); }
+#line 3544 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 167: /* statement ::= typedef */
+#line 387 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_typedef_statement(ctx, yymsp[0].minor.yy71); }
+#line 3549 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 168: /* statement ::= SEMICOLON */
+#line 388 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_empty_statement(ctx); }
+#line 3554 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 169: /* statement ::= expression SEMICOLON */
+#line 389 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_expr_statement(ctx, yymsp[-1].minor.yy322); }
+#line 3559 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 170: /* statement ::= RETURN SEMICOLON */
+#line 390 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_return_statement(ctx, NULL); }
+#line 3564 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 171: /* statement ::= RETURN expression SEMICOLON */
+#line 391 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_return_statement(ctx, yymsp[-1].minor.yy322); }
+#line 3569 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 174: /* while_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET WHILE */
+      case 179: /* for_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET FOR */ yytestcase(yyruleno==179);
+#line 397 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = (yymsp[-3].minor.yy0.i64 < 0) ? 0 : yymsp[-3].minor.yy0.i64; }
+#line 3575 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 175: /* while_intro ::= LBRACKET UNROLL RBRACKET WHILE */
+      case 180: /* for_intro ::= LBRACKET UNROLL RBRACKET FOR */ yytestcase(yyruleno==180);
+      case 196: /* do_intro ::= LBRACKET UNROLL RBRACKET DO */ yytestcase(yyruleno==196);
+#line 398 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = -1; }
+#line 3582 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 177: /* while_intro ::= WHILE */
+      case 182: /* for_intro ::= FOR */ yytestcase(yyruleno==182);
+      case 198: /* do_intro ::= DO */ yytestcase(yyruleno==198);
+#line 400 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = -2; }
+#line 3589 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 178: /* for_statement ::= for_intro for_details */
+#line 404 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = yymsp[0].minor.yy233; ((MOJOSHADER_astForStatement *) yygotominor.yy233)->unroll = yymsp[-1].minor.yy270; }
+#line 3594 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 183: /* for_details ::= LPAREN expression SEMICOLON expression SEMICOLON expression RPAREN statement */
+#line 414 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, yymsp[-6].minor.yy322, yymsp[-4].minor.yy322, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3599 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 184: /* for_details ::= LPAREN SEMICOLON SEMICOLON RPAREN statement */
+#line 415 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, NULL, NULL, NULL, yymsp[0].minor.yy233); }
+#line 3604 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 185: /* for_details ::= LPAREN SEMICOLON SEMICOLON expression RPAREN statement */
+#line 416 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, NULL, NULL, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3609 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 186: /* for_details ::= LPAREN SEMICOLON expression SEMICOLON RPAREN statement */
+#line 417 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, NULL, yymsp[-3].minor.yy322, NULL, yymsp[0].minor.yy233); }
+#line 3614 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 187: /* for_details ::= LPAREN SEMICOLON expression SEMICOLON expression RPAREN statement */
+#line 418 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, NULL, yymsp[-4].minor.yy322, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3619 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 188: /* for_details ::= LPAREN expression SEMICOLON SEMICOLON RPAREN statement */
+#line 419 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, yymsp[-4].minor.yy322, NULL, NULL, yymsp[0].minor.yy233); }
+#line 3624 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 189: /* for_details ::= LPAREN expression SEMICOLON SEMICOLON expression RPAREN statement */
+#line 420 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, yymsp[-5].minor.yy322, NULL, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3629 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 190: /* for_details ::= LPAREN expression SEMICOLON expression SEMICOLON RPAREN statement */
+#line 421 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, NULL, yymsp[-5].minor.yy322, yymsp[-3].minor.yy322, NULL, yymsp[0].minor.yy233); }
+#line 3634 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 191: /* for_details ::= LPAREN variable_declaration expression SEMICOLON expression RPAREN statement */
+#line 422 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, yymsp[-5].minor.yy24, NULL, yymsp[-4].minor.yy322, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3639 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 192: /* for_details ::= LPAREN variable_declaration SEMICOLON RPAREN statement */
+#line 423 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, yymsp[-3].minor.yy24, NULL, NULL, NULL, yymsp[0].minor.yy233); }
+#line 3644 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 193: /* for_details ::= LPAREN variable_declaration SEMICOLON expression RPAREN statement */
+#line 424 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, yymsp[-4].minor.yy24, NULL, yymsp[-2].minor.yy322, NULL, yymsp[0].minor.yy233); }
+#line 3649 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 194: /* for_details ::= LPAREN variable_declaration expression SEMICOLON RPAREN statement */
+#line 425 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy233 = new_for_statement(ctx, yymsp[-4].minor.yy24, NULL, yymsp[-3].minor.yy322, NULL, yymsp[0].minor.yy233); }
+#line 3654 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 195: /* do_intro ::= LBRACKET UNROLL LPAREN INT_CONSTANT RPAREN RBRACKET DO */
+#line 428 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = (yymsp[-3].minor.yy0.i64 < 0) ? 0 : (int) yymsp[-3].minor.yy0.i64; }
+#line 3659 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 199: /* if_intro ::= LBRACKET BRANCH RBRACKET IF */
+#line 434 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_BRANCH; }
+#line 3664 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 200: /* if_intro ::= LBRACKET FLATTEN RBRACKET IF */
+#line 435 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_FLATTEN; }
+#line 3669 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 201: /* if_intro ::= LBRACKET IFALL RBRACKET IF */
+#line 436 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_IFALL; }
+#line 3674 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 202: /* if_intro ::= LBRACKET IFANY RBRACKET IF */
+#line 437 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_IFANY; }
+#line 3679 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 203: /* if_intro ::= LBRACKET PREDICATE RBRACKET IF */
+#line 438 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_PREDICATE; }
+#line 3684 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 204: /* if_intro ::= LBRACKET PREDICATEBLOCK RBRACKET IF */
+#line 439 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_PREDICATEBLOCK; }
+#line 3689 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 205: /* if_intro ::= IF */
+#line 440 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_IFATTR_NONE; }
+#line 3694 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 206: /* switch_intro ::= LBRACKET FLATTEN RBRACKET SWITCH */
+#line 443 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_SWITCHATTR_FLATTEN; }
+#line 3699 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 207: /* switch_intro ::= LBRACKET BRANCH RBRACKET SWITCH */
+#line 444 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_SWITCHATTR_BRANCH; }
+#line 3704 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 208: /* switch_intro ::= LBRACKET FORCECASE RBRACKET SWITCH */
+#line 445 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_SWITCHATTR_FORCECASE; }
+#line 3709 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 209: /* switch_intro ::= LBRACKET CALL RBRACKET SWITCH */
+#line 446 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_SWITCHATTR_CALL; }
+#line 3714 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 210: /* switch_intro ::= SWITCH */
+#line 447 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy270 = MOJOSHADER_AST_SWITCHATTR_NONE; }
+#line 3719 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 211: /* switch_case_list ::= switch_case */
+#line 451 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy165 = yymsp[0].minor.yy165; }
+#line 3724 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 212: /* switch_case_list ::= switch_case_list switch_case */
+#line 452 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy165 = yymsp[0].minor.yy165; yygotominor.yy165->next = yymsp[-1].minor.yy165; }
+#line 3729 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 213: /* switch_case ::= CASE expression COLON statement_list */
+#line 458 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astStatement, yymsp[0].minor.yy233); yygotominor.yy165 = new_switch_case(ctx, yymsp[-2].minor.yy322, yymsp[0].minor.yy233); }
+#line 3734 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 214: /* switch_case ::= CASE expression COLON */
+#line 459 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy165 = new_switch_case(ctx, yymsp[-1].minor.yy322, NULL); }
+#line 3739 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 215: /* switch_case ::= DEFAULT COLON statement_list */
+#line 460 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astStatement, yymsp[0].minor.yy233); yygotominor.yy165 = new_switch_case(ctx, NULL, yymsp[0].minor.yy233); }
+#line 3744 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 216: /* switch_case ::= DEFAULT COLON */
+#line 461 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy165 = new_switch_case(ctx, NULL, NULL); }
+#line 3749 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 217: /* primary_expr ::= IDENTIFIER */
+#line 466 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_identifier_expr(ctx, yymsp[0].minor.yy0.string); }
+#line 3754 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 218: /* primary_expr ::= INT_CONSTANT */
+#line 467 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_literal_int_expr(ctx, yymsp[0].minor.yy0.i64); }
+#line 3759 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 219: /* primary_expr ::= FLOAT_CONSTANT */
+#line 468 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_literal_float_expr(ctx, yymsp[0].minor.yy0.dbl); }
+#line 3764 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 220: /* primary_expr ::= STRING_LITERAL */
+#line 469 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_literal_string_expr(ctx, yymsp[0].minor.yy0.string); }
+#line 3769 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 221: /* primary_expr ::= TRUE */
+#line 470 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_literal_boolean_expr(ctx, 1); }
+#line 3774 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 222: /* primary_expr ::= FALSE */
+#line 471 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_literal_boolean_expr(ctx, 0); }
+#line 3779 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 225: /* postfix_expr ::= postfix_expr LBRACKET expression RBRACKET */
+#line 477 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_DEREF_ARRAY, yymsp[-3].minor.yy322, yymsp[-1].minor.yy322); }
+#line 3784 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 226: /* postfix_expr ::= IDENTIFIER arguments */
+#line 478 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_callfunc_expr(ctx, yymsp[-1].minor.yy0.string, yymsp[0].minor.yy26); }
+#line 3789 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 227: /* postfix_expr ::= datatype arguments */
+#line 479 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_constructor_expr(ctx, yymsp[-1].minor.yy37, yymsp[0].minor.yy26); }
+#line 3794 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 228: /* postfix_expr ::= postfix_expr DOT IDENTIFIER */
+#line 480 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_deref_struct_expr(ctx, yymsp[-2].minor.yy322, yymsp[0].minor.yy0.string); }
+#line 3799 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 229: /* postfix_expr ::= postfix_expr PLUSPLUS */
+#line 481 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_POSTINCREMENT, yymsp[-1].minor.yy322); }
+#line 3804 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 230: /* postfix_expr ::= postfix_expr MINUSMINUS */
+#line 482 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_POSTDECREMENT, yymsp[-1].minor.yy322); }
+#line 3809 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 231: /* arguments ::= LPAREN RPAREN */
+#line 486 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy26 = NULL; }
+#line 3814 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 232: /* arguments ::= LPAREN argument_list RPAREN */
+#line 487 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ REVERSE_LINKED_LIST(MOJOSHADER_astArguments, yymsp[-1].minor.yy26); yygotominor.yy26 = yymsp[-1].minor.yy26; }
+#line 3819 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 233: /* argument_list ::= assignment_expr */
+#line 491 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy26 = new_argument(ctx, yymsp[0].minor.yy322); }
+#line 3824 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 234: /* argument_list ::= argument_list COMMA assignment_expr */
+#line 492 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy26 = new_argument(ctx, yymsp[0].minor.yy322); yygotominor.yy26->next = yymsp[-2].minor.yy26; }
+#line 3829 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 236: /* unary_expr ::= PLUSPLUS unary_expr */
+#line 497 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_PREINCREMENT, yymsp[0].minor.yy322); }
+#line 3834 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 237: /* unary_expr ::= MINUSMINUS unary_expr */
+#line 498 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_PREDECREMENT, yymsp[0].minor.yy322); }
+#line 3839 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 239: /* unary_expr ::= MINUS cast_expr */
+#line 500 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_NEGATE, yymsp[0].minor.yy322); }
+#line 3844 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 240: /* unary_expr ::= COMPLEMENT cast_expr */
+#line 501 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_COMPLEMENT, yymsp[0].minor.yy322); }
+#line 3849 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 241: /* unary_expr ::= EXCLAMATION cast_expr */
+#line 502 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_unary_expr(ctx, MOJOSHADER_AST_OP_NOT, yymsp[0].minor.yy322); }
+#line 3854 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 243: /* cast_expr ::= LPAREN datatype RPAREN cast_expr */
+#line 507 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_cast_expr(ctx, yymsp[-2].minor.yy37, yymsp[0].minor.yy322); }
+#line 3859 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 245: /* multiplicative_expr ::= multiplicative_expr STAR cast_expr */
+#line 512 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_MULTIPLY, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3864 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 246: /* multiplicative_expr ::= multiplicative_expr SLASH cast_expr */
+#line 513 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_DIVIDE, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3869 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 247: /* multiplicative_expr ::= multiplicative_expr PERCENT cast_expr */
+#line 514 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_MODULO, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3874 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 249: /* additive_expr ::= additive_expr PLUS multiplicative_expr */
+#line 519 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_ADD, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3879 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 250: /* additive_expr ::= additive_expr MINUS multiplicative_expr */
+#line 520 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_SUBTRACT, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3884 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 252: /* shift_expr ::= shift_expr LSHIFT additive_expr */
+#line 525 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LSHIFT, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3889 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 253: /* shift_expr ::= shift_expr RSHIFT additive_expr */
+#line 526 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_RSHIFT, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3894 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 255: /* relational_expr ::= relational_expr LT shift_expr */
+#line 531 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LESSTHAN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3899 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 256: /* relational_expr ::= relational_expr GT shift_expr */
+#line 532 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_GREATERTHAN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3904 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 257: /* relational_expr ::= relational_expr LEQ shift_expr */
+#line 533 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LESSTHANOREQUAL, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3909 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 258: /* relational_expr ::= relational_expr GEQ shift_expr */
+#line 534 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_GREATERTHANOREQUAL, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3914 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 260: /* equality_expr ::= equality_expr EQL relational_expr */
+#line 539 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_EQUAL, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3919 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 261: /* equality_expr ::= equality_expr NEQ relational_expr */
+#line 540 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_NOTEQUAL, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3924 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 263: /* and_expr ::= and_expr AND equality_expr */
+#line 545 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_BINARYAND, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3929 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 265: /* exclusive_or_expr ::= exclusive_or_expr XOR and_expr */
+#line 550 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_BINARYXOR, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3934 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 267: /* inclusive_or_expr ::= inclusive_or_expr OR exclusive_or_expr */
+#line 555 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_BINARYOR, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3939 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 269: /* logical_and_expr ::= logical_and_expr ANDAND inclusive_or_expr */
+#line 560 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LOGICALAND, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3944 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 271: /* logical_or_expr ::= logical_or_expr OROR logical_and_expr */
+#line 565 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LOGICALOR, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3949 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 273: /* conditional_expr ::= logical_or_expr QUESTION logical_or_expr COLON conditional_expr */
+#line 570 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_ternary_expr(ctx, MOJOSHADER_AST_OP_CONDITIONAL, yymsp[-4].minor.yy322, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3954 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 275: /* assignment_expr ::= unary_expr ASSIGN assignment_expr */
+#line 575 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_ASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3959 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 276: /* assignment_expr ::= unary_expr MULASSIGN assignment_expr */
+#line 576 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_MULASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3964 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 277: /* assignment_expr ::= unary_expr DIVASSIGN assignment_expr */
+#line 577 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_DIVASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3969 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 278: /* assignment_expr ::= unary_expr MODASSIGN assignment_expr */
+#line 578 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_MODASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3974 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 279: /* assignment_expr ::= unary_expr ADDASSIGN assignment_expr */
+#line 579 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_ADDASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3979 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 280: /* assignment_expr ::= unary_expr SUBASSIGN assignment_expr */
+#line 580 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_SUBASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3984 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 281: /* assignment_expr ::= unary_expr LSHIFTASSIGN assignment_expr */
+#line 581 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_LSHIFTASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3989 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 282: /* assignment_expr ::= unary_expr RSHIFTASSIGN assignment_expr */
+#line 582 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_RSHIFTASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3994 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 283: /* assignment_expr ::= unary_expr ANDASSIGN assignment_expr */
+#line 583 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_ANDASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 3999 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 284: /* assignment_expr ::= unary_expr XORASSIGN assignment_expr */
+#line 584 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_XORASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 4004 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      case 285: /* assignment_expr ::= unary_expr ORASSIGN assignment_expr */
+#line 585 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+{ yygotominor.yy322 = new_binary_expr(ctx, MOJOSHADER_AST_OP_ORASSIGN, yymsp[-2].minor.yy322, yymsp[0].minor.yy322); }
+#line 4009 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+        break;
+      default:
+        break;
+  };
+  yygoto = yyRuleInfo[yyruleno].lhs;
+  yysize = yyRuleInfo[yyruleno].nrhs;
+  yypParser->yyidx -= yysize;
+  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
+  if( yyact < YYNSTATE ){
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+    /* If we are not debugging and the reduce action popped at least
+    ** one element off the stack, then we can push the new element back
+    ** onto the stack here, and skip the stack overflow test in yy_shift().
+    ** That gives a significant speed improvement. */
+    if( yysize ){
+      yypParser->yyidx++;
+      yymsp -= yysize-1;
+      yymsp->stateno = (YYACTIONTYPE)yyact;
+      yymsp->major = (YYCODETYPE)yygoto;
+      yymsp->minor = yygotominor;
+    }else
+#endif
+    {
+      yy_shift(yypParser,yyact,yygoto,&yygotominor);
+    }
+  }else{
+    assert( yyact == YYNSTATE + YYNRULE + 1 );
+    yy_accept(yypParser);
+  }
+}
+
+/*
+** The following code executes when the parse fails
+*/
+#ifndef YYNOERRORRECOVERY
+static void yy_parse_failed(
+  yyParser *yypParser           /* The parser */
+){
+  ParseHLSLARG_FETCH;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE ){
+    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
+  }
+#endif
+  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
+  /* Here code is inserted which will be executed whenever the
+  ** parser fails */
+#line 42 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+
+    // !!! FIXME: make this a proper fail() function.
+    fail(ctx, "Giving up. Parser is hopelessly lost...");
+#line 4061 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+  ParseHLSLARG_STORE; /* Suppress warning about unused %extra_argument variable */
+}
+#endif /* YYNOERRORRECOVERY */
+
+/*
+** The following code executes when a syntax error first occurs.
+*/
+static void yy_syntax_error(
+  yyParser *yypParser,           /* The parser */
+  int yymajor,                   /* The major type of the error token */
+  YYMINORTYPE yyminor            /* The minor type of the error token */
+){
+  ParseHLSLARG_FETCH;
+#define TOKEN (yyminor.yy0)
+#line 37 "D:/dev/mojoshader/mojoshader_parser_hlsl.lemon"
+
+    // !!! FIXME: make this a proper fail() function.
+    fail(ctx, "Syntax error");
+#line 4080 "D:/dev/mojoshader/mojoshader_parser_hlsl.h"
+  ParseHLSLARG_STORE; /* Suppress warning about unused %extra_argument variable */
+}
+
+/*
+** The following is executed when the parser accepts
+*/
+static void yy_accept(
+  yyParser *yypParser           /* The parser */
+){
+  ParseHLSLARG_FETCH;
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE ){
+    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
+  }
+#endif
+  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
+  /* Here code is inserted which will be executed whenever the
+  ** parser accepts */
+  ParseHLSLARG_STORE; /* Suppress warning about unused %extra_argument variable */
+}
+
+/* The main parser program.
+** The first argument is a pointer to a structure obtained from
+** "ParseHLSLAlloc" which describes the current state of the parser.
+** The second argument is the major token number.  The third is
+** the minor token.  The fourth optional argument is whatever the
+** user wants (and specified in the grammar) and is available for
+** use by the action routines.
+**
+** Inputs:
+** <ul>
+** <li> A pointer to the parser (an opaque structure.)
+** <li> The major token number.
+** <li> The minor token number.
+** <li> An option argument of a grammar-specified type.
+** </ul>
+**
+** Outputs:
+** None.
+*/
+#if __MOJOSHADER__
+static
+#endif
+void ParseHLSL(
+  void *yyp,                   /* The parser */
+  int yymajor,                 /* The major token code number */
+  ParseHLSLTOKENTYPE yyminor       /* The value for the token */
+  ParseHLSLARG_PDECL               /* Optional %extra_argument parameter */
+){
+  YYMINORTYPE yyminorunion;
+  int yyact;            /* The parser action. */
+  int yyendofinput;     /* True if we are at the end of input */
+#ifdef YYERRORSYMBOL
+  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
+#endif
+  yyParser *yypParser;  /* The parser */
+
+  /* (re)initialize the parser, if necessary */
+  yypParser = (yyParser*)yyp;
+  if( yypParser->yyidx<0 ){
+#if YYSTACKDEPTH<=0
+    if( yypParser->yystksz <=0 ){
+      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
+      yyminorunion = yyzerominor;
+      yyStackOverflow(yypParser, &yyminorunion);
+      return;
+    }
+#endif
+    yypParser->yyidx = 0;
+    yypParser->yyerrcnt = -1;
+    yypParser->yystack[0].stateno = 0;
+    yypParser->yystack[0].major = 0;
+  }
+  yyminorunion.yy0 = yyminor;
+  yyendofinput = (yymajor==0);
+  ParseHLSLARG_STORE;
+
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+  if( yyTraceFILE ){
+    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
+  }
+#endif
+
+  do{
+    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
+    if( yyact<YYNSTATE ){
+      assert( !yyendofinput );  /* Impossible to shift the $ token */
+      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
+      yypParser->yyerrcnt--;
+      yymajor = YYNOCODE;
+    }else if( yyact < YYNSTATE + YYNRULE ){
+      yy_reduce(yypParser,yyact-YYNSTATE);
+    }else{
+      assert( yyact == YY_ERROR_ACTION );
+#ifdef YYERRORSYMBOL
+      int yymx;
+#endif
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+      if( yyTraceFILE ){
+        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
+      }
+#endif
+#ifdef YYERRORSYMBOL
+      /* A syntax error has occurred.
+      ** The response to an error depends upon whether or not the
+      ** grammar defines an error token "ERROR".  
+      **
+      ** This is what we do if the grammar does define ERROR:
+      **
+      **  * Call the %syntax_error function.
+      **
+      **  * Begin popping the stack until we enter a state where
+      **    it is legal to shift the error symbol, then shift
+      **    the error symbol.
+      **
+      **  * Set the error count to three.
+      **
+      **  * Begin accepting and shifting new tokens.  No new error
+      **    processing will occur until three tokens have been
+      **    shifted successfully.
+      **
+      */
+      if( yypParser->yyerrcnt<0 ){
+        yy_syntax_error(yypParser,yymajor,yyminorunion);
+      }
+      yymx = yypParser->yystack[yypParser->yyidx].major;
+      if( yymx==YYERRORSYMBOL || yyerrorhit ){
+#if LEMON_SUPPORT_TRACING   /* __MOJOSHADER__ */
+        if( yyTraceFILE ){
+          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
+             yyTracePrompt,yyTokenName[yymajor]);
+        }
+#endif
+        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
+        yymajor = YYNOCODE;
+      }else{
+         while(
+          yypParser->yyidx >= 0 &&
+          yymx != YYERRORSYMBOL &&
+          (yyact = yy_find_reduce_action(
+                        yypParser->yystack[yypParser->yyidx].stateno,
+                        YYERRORSYMBOL)) >= YYNSTATE
+        ){
+          yy_pop_parser_stack(yypParser);
+        }
+        if( yypParser->yyidx < 0 || yymajor==0 ){
+          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
+          yy_parse_failed(yypParser);
+          yymajor = YYNOCODE;
+        }else if( yymx!=YYERRORSYMBOL ){
+          YYMINORTYPE u2;
+          u2.YYERRSYMDT = 0;
+          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
+        }
+      }
+      yypParser->yyerrcnt = 3;
+      yyerrorhit = 1;
+#elif defined(YYNOERRORRECOVERY)
+      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
+      ** do any kind of error recovery.  Instead, simply invoke the syntax
+      ** error routine and continue going as if nothing had happened.
+      **
+      ** Applications can set this macro (for example inside %include) if
+      ** they intend to abandon the parse upon the first syntax error seen.
+      */
+      yy_syntax_error(yypParser,yymajor,yyminorunion);
+      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
+      yymajor = YYNOCODE;
+      
+#else  /* YYERRORSYMBOL is not defined */
+      /* This is what we do if the grammar does not define ERROR:
+      **
+      **  * Report an error message, and throw away the input token.
+      **
+      **  * If the input token is $, then fail the parse.
+      **
+      ** As before, subsequent error messages are suppressed until
+      ** three input tokens have been successfully shifted.
+      */
+      if( yypParser->yyerrcnt<=0 ){
+        yy_syntax_error(yypParser,yymajor,yyminorunion);
+      }
+      yypParser->yyerrcnt = 3;
+      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
+      if( yyendofinput ){
+        yy_parse_failed(yypParser);
+      }
+      yymajor = YYNOCODE;
+#endif
+    }
+  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
+  return;
+}

+ 2362 - 0
ThirdParty/MojoShader/mojoshader_preprocessor.c

@@ -0,0 +1,2362 @@
+/**
+ * MojoShader; generate shader programs from bytecode of compiled
+ *  Direct3D shaders.
+ *
+ * Please see the file LICENSE.txt in the source's root directory.
+ *
+ *  This file written by Ryan C. Gordon.
+ */
+
+#define __MOJOSHADER_INTERNAL__ 1
+#include "mojoshader_internal.h"
+
+#if DEBUG_PREPROCESSOR
+    #define print_debug_token(token, len, val) \
+        MOJOSHADER_print_debug_token("PREPROCESSOR", token, len, val)
+#else
+    #define print_debug_token(token, len, val)
+#endif
+
+#if DEBUG_LEXER
+static Token debug_preprocessor_lexer(IncludeState *s)
+{
+    const Token retval = preprocessor_lexer(s);
+    MOJOSHADER_print_debug_token("LEXER", s->token, s->tokenlen, retval);
+    return retval;
+} // debug_preprocessor_lexer
+#define preprocessor_lexer(s) debug_preprocessor_lexer(s)
+#endif
+
+#if DEBUG_TOKENIZER
+static void print_debug_lexing_position(IncludeState *s)
+{
+    if (s != NULL)
+        printf("NOW LEXING %s:%d ...\n", s->filename, s->line);
+} // print_debug_lexing_position
+#else
+#define print_debug_lexing_position(s)
+#endif
+
+typedef struct Context
+{
+    int isfail;
+    int out_of_memory;
+    char failstr[256];
+    int recursion_count;
+    int asm_comments;
+    int parsing_pragma;
+    Conditional *conditional_pool;
+    IncludeState *include_stack;
+    IncludeState *include_pool;
+    Define *define_hashtable[256];
+    Define *define_pool;
+    Define *file_macro;
+    Define *line_macro;
+    StringCache *filename_cache;
+    MOJOSHADER_includeOpen open_callback;
+    MOJOSHADER_includeClose close_callback;
+    MOJOSHADER_malloc malloc;
+    MOJOSHADER_free free;
+    void *malloc_data;
+} Context;
+
+
+// Convenience functions for allocators...
+
+static inline void out_of_memory(Context *ctx)
+{
+    ctx->out_of_memory = 1;
+} // out_of_memory
+
+static inline void *Malloc(Context *ctx, const size_t len)
+{
+    void *retval = ctx->malloc((int) len, ctx->malloc_data);
+    if (retval == NULL)
+        out_of_memory(ctx);
+    return retval;
+} // Malloc
+
+static inline void Free(Context *ctx, void *ptr)
+{
+    ctx->free(ptr, ctx->malloc_data);
+} // Free
+
+static void *MallocBridge(int bytes, void *data)
+{
+    return Malloc((Context *) data, (size_t) bytes);
+} // MallocBridge
+
+static void FreeBridge(void *ptr, void *data)
+{
+    Free((Context *) data, ptr);
+} // FreeBridge
+
+static inline char *StrDup(Context *ctx, const char *str)
+{
+    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
+    if (retval != NULL)
+        strcpy(retval, str);
+    return retval;
+} // StrDup
+
+static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
+static void failf(Context *ctx, const char *fmt, ...)
+{
+    ctx->isfail = 1;
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(ctx->failstr, sizeof (ctx->failstr), fmt, ap);
+    va_end(ap);
+} // failf
+
+static inline void fail(Context *ctx, const char *reason)
+{
+    failf(ctx, "%s", reason);
+} // fail
+
+
+#if DEBUG_TOKENIZER
+void MOJOSHADER_print_debug_token(const char *subsystem, const char *token,
+                                  const unsigned int tokenlen,
+                                  const Token tokenval)
+{
+    printf("%s TOKEN: \"", subsystem);
+    unsigned int i;
+    for (i = 0; i < tokenlen; i++)
+    {
+        if (token[i] == '\n')
+            printf("\\n");
+        else if (token[i] == '\\')
+            printf("\\\\");
+        else
+            printf("%c", token[i]);
+    } // for
+    printf("\" (");
+    switch (tokenval)
+    {
+        #define TOKENCASE(x) case x: printf("%s", #x); break
+        TOKENCASE(TOKEN_UNKNOWN);
+        TOKENCASE(TOKEN_IDENTIFIER);
+        TOKENCASE(TOKEN_INT_LITERAL);
+        TOKENCASE(TOKEN_FLOAT_LITERAL);
+        TOKENCASE(TOKEN_STRING_LITERAL);
+        TOKENCASE(TOKEN_ADDASSIGN);
+        TOKENCASE(TOKEN_SUBASSIGN);
+        TOKENCASE(TOKEN_MULTASSIGN);
+        TOKENCASE(TOKEN_DIVASSIGN);
+        TOKENCASE(TOKEN_MODASSIGN);
+        TOKENCASE(TOKEN_XORASSIGN);
+        TOKENCASE(TOKEN_ANDASSIGN);
+        TOKENCASE(TOKEN_ORASSIGN);
+        TOKENCASE(TOKEN_INCREMENT);
+        TOKENCASE(TOKEN_DECREMENT);
+        TOKENCASE(TOKEN_RSHIFT);
+        TOKENCASE(TOKEN_LSHIFT);
+        TOKENCASE(TOKEN_ANDAND);
+        TOKENCASE(TOKEN_OROR);
+        TOKENCASE(TOKEN_LEQ);
+        TOKENCASE(TOKEN_GEQ);
+        TOKENCASE(TOKEN_EQL);
+        TOKENCASE(TOKEN_NEQ);
+        TOKENCASE(TOKEN_HASH);
+        TOKENCASE(TOKEN_HASHHASH);
+        TOKENCASE(TOKEN_PP_INCLUDE);
+        TOKENCASE(TOKEN_PP_LINE);
+        TOKENCASE(TOKEN_PP_DEFINE);
+        TOKENCASE(TOKEN_PP_UNDEF);
+        TOKENCASE(TOKEN_PP_IF);
+        TOKENCASE(TOKEN_PP_IFDEF);
+        TOKENCASE(TOKEN_PP_IFNDEF);
+        TOKENCASE(TOKEN_PP_ELSE);
+        TOKENCASE(TOKEN_PP_ELIF);
+        TOKENCASE(TOKEN_PP_ENDIF);
+        TOKENCASE(TOKEN_PP_ERROR);
+        TOKENCASE(TOKEN_PP_PRAGMA);
+        TOKENCASE(TOKEN_INCOMPLETE_COMMENT);
+        TOKENCASE(TOKEN_BAD_CHARS);
+        TOKENCASE(TOKEN_EOI);
+        TOKENCASE(TOKEN_PREPROCESSING_ERROR);
+        #undef TOKENCASE
+
+        case ((Token) '\n'):
+            printf("'\\n'");
+            break;
+
+        case ((Token) '\\'):
+            printf("'\\\\'");
+            break;
+
+        default:
+            assert(((int)tokenval) < 256);
+            printf("'%c'", (char) tokenval);
+            break;
+    } // switch
+    printf(")\n");
+} // MOJOSHADER_print_debug_token
+#endif
+
+
+
+#if !MOJOSHADER_FORCE_INCLUDE_CALLBACKS
+
+// !!! FIXME: most of these _MSC_VER should probably be _WINDOWS?
+#ifdef _MSC_VER
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>  // GL headers need this for WINGDIAPI definition.
+#else
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#endif
+
+int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype,
+                                     const char *fname, const char *parent,
+                                     const char **outdata,
+                                     unsigned int *outbytes,
+                                     MOJOSHADER_malloc m, MOJOSHADER_free f,
+                                     void *d)
+{
+#ifdef _MSC_VER
+    WCHAR wpath[MAX_PATH];
+    if (!MultiByteToWideChar(CP_UTF8, 0, fname, -1, wpath, MAX_PATH))
+        return 0;
+
+    const DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+    const HANDLE handle = CreateFileW(wpath, FILE_GENERIC_READ, share,
+                                      NULL, OPEN_EXISTING, NULL, NULL);
+    if (handle == INVALID_HANDLE_VALUE)
+        return 0;
+
+    const DWORD fileSize = GetFileSize(handle, NULL);
+    if (fileSize == INVALID_FILE_SIZE)
+    {
+        CloseHandle(handle);
+        return 0;
+    } // if
+
+    char *data = (char *) m(fileSize, d);
+    if (data == NULL)
+    {
+        CloseHandle(handle);
+        return 0;
+    } // if
+
+    DWORD readLength = 0;
+    if (!ReadFile(handle, data, fileSize, &readLength, NULL))
+    {
+        CloseHandle(handle);
+        f(data, d);
+        return 0;
+    } // if
+
+    CloseHandle(handle);
+
+    if (readLength != fileSize)
+    {
+        f(data, d);
+        return 0;
+    } // if
+    *outdata = data;
+    *outbytes = fileSize;
+    return 1;
+#else
+    struct stat statbuf;
+    if (stat(fname, &statbuf) == -1)
+        return 0;
+    char *data = (char *) m(statbuf.st_size, d);
+    if (data == NULL)
+        return 0;
+    const int fd = open(fname, O_RDONLY);
+    if (fd == -1)
+    {
+        f(data, d);
+        return 0;
+    } // if
+    if (read(fd, data, statbuf.st_size) != statbuf.st_size)
+    {
+        f(data, d);
+        close(fd);
+        return 0;
+    } // if
+    close(fd);
+    *outdata = data;
+    *outbytes = (unsigned int) statbuf.st_size;
+    return 1;
+#endif
+} // MOJOSHADER_internal_include_open
+
+
+void MOJOSHADER_internal_include_close(const char *data, MOJOSHADER_malloc m,
+                                       MOJOSHADER_free f, void *d)
+{
+    f((void *) data, d);
+} // MOJOSHADER_internal_include_close
+#endif  // !MOJOSHADER_FORCE_INCLUDE_CALLBACKS
+
+
+// !!! FIXME: maybe use these pool magic elsewhere?
+// !!! FIXME: maybe just get rid of this? (maybe the fragmentation isn't a big deal?)
+
+// Pool stuff...
+// ugh, I hate this macro salsa.
+#define FREE_POOL(type, poolname) \
+    static void free_##poolname##_pool(Context *ctx) { \
+        type *item = ctx->poolname##_pool; \
+        while (item != NULL) { \
+            type *next = item->next; \
+            Free(ctx, item); \
+            item = next; \
+        } \
+    }
+
+#define GET_POOL(type, poolname) \
+    static type *get_##poolname(Context *ctx) { \
+        type *retval = ctx->poolname##_pool; \
+        if (retval != NULL) \
+            ctx->poolname##_pool = retval->next; \
+        else \
+            retval = (type *) Malloc(ctx, sizeof (type)); \
+        if (retval != NULL) \
+            memset(retval, '\0', sizeof (type)); \
+        return retval; \
+    }
+
+#define PUT_POOL(type, poolname) \
+    static void put_##poolname(Context *ctx, type *item) { \
+        item->next = ctx->poolname##_pool; \
+        ctx->poolname##_pool = item; \
+    }
+
+#define IMPLEMENT_POOL(type, poolname) \
+    FREE_POOL(type, poolname) \
+    GET_POOL(type, poolname) \
+    PUT_POOL(type, poolname)
+
+IMPLEMENT_POOL(Conditional, conditional)
+IMPLEMENT_POOL(IncludeState, include)
+IMPLEMENT_POOL(Define, define)
+
+
+// Preprocessor define hashtable stuff...
+
+// !!! FIXME: why isn't this using mojoshader_common.c's code?
+
+// this is djb's xor hashing function.
+static inline uint32 hash_string_djbxor(const char *sym)
+{
+    register uint32 hash = 5381;
+    while (*sym)
+        hash = ((hash << 5) + hash) ^ *(sym++);
+    return hash;
+} // hash_string_djbxor
+
+static inline uint8 hash_define(const char *sym)
+{
+    return (uint8) hash_string_djbxor(sym);
+} // hash_define
+
+
+static int add_define(Context *ctx, const char *sym, const char *val,
+                      char **parameters, int paramcount)
+{
+    const uint8 hash = hash_define(sym);
+    Define *bucket = ctx->define_hashtable[hash];
+    while (bucket)
+    {
+        if (strcmp(bucket->identifier, sym) == 0)
+        {
+            failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning?
+            // !!! FIXME: gcc reports the location of previous #define here.
+            return 0;
+        } // if
+        bucket = bucket->next;
+    } // while
+
+    bucket = get_define(ctx);
+    if (bucket == NULL)
+        return 0;
+
+    bucket->definition = val;
+    bucket->original = NULL;
+    bucket->identifier = sym;
+    bucket->parameters = (const char **) parameters;
+    bucket->paramcount = paramcount;
+    bucket->next = ctx->define_hashtable[hash];
+    ctx->define_hashtable[hash] = bucket;
+    return 1;
+} // add_define
+
+
+static void free_define(Context *ctx, Define *def)
+{
+    if (def != NULL)
+    {
+        int i;
+        for (i = 0; i < def->paramcount; i++)
+            Free(ctx, (void *) def->parameters[i]);
+        Free(ctx, (void *) def->parameters);
+        Free(ctx, (void *) def->identifier);
+        Free(ctx, (void *) def->definition);
+        Free(ctx, (void *) def->original);
+        put_define(ctx, def);
+    } // if
+} // free_define
+
+
+static int remove_define(Context *ctx, const char *sym)
+{
+    const uint8 hash = hash_define(sym);
+    Define *bucket = ctx->define_hashtable[hash];
+    Define *prev = NULL;
+    while (bucket)
+    {
+        if (strcmp(bucket->identifier, sym) == 0)
+        {
+            if (prev == NULL)
+                ctx->define_hashtable[hash] = bucket->next;
+            else
+                prev->next = bucket->next;
+            free_define(ctx, bucket);
+            return 1;
+        } // if
+        prev = bucket;
+        bucket = bucket->next;
+    } // while
+
+    return 0;
+} // remove_define
+
+
+static const Define *find_define(Context *ctx, const char *sym)
+{
+    if ( (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) )
+    {
+        Free(ctx, (char *) ctx->file_macro->definition);
+        const IncludeState *state = ctx->include_stack;
+        const char *fname = state ? state->filename : "";
+        const size_t len = strlen(fname) + 2;
+        char *str = (char *) Malloc(ctx, len);
+        if (!str)
+            return NULL;
+        str[0] = '\"';
+        memcpy(str + 1, fname, len - 2);
+        str[len - 1] = '\"';
+        ctx->file_macro->definition = str;
+        return ctx->file_macro;
+    } // if
+
+    else if ( (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) )
+    {
+        Free(ctx, (char *) ctx->line_macro->definition);
+        const IncludeState *state = ctx->include_stack;
+        const size_t bufsize = 32;
+        char *str = (char *) Malloc(ctx, bufsize);
+        if (!str)
+            return 0;
+
+        const size_t len = snprintf(str, bufsize, "%u", state->line);
+        assert(len < bufsize);
+        ctx->line_macro->definition = str;
+        return ctx->line_macro;
+    } // else
+
+    const uint8 hash = hash_define(sym);
+    Define *bucket = ctx->define_hashtable[hash];
+    while (bucket)
+    {
+        if (strcmp(bucket->identifier, sym) == 0)
+            return bucket;
+        bucket = bucket->next;
+    } // while
+    return NULL;
+} // find_define
+
+
+static const Define *find_define_by_token(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    assert(state->tokenval == TOKEN_IDENTIFIER);
+    char *sym = (char *) alloca(state->tokenlen+1);
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+    return find_define(ctx, sym);
+} // find_define_by_token
+
+
+static const Define *find_macro_arg(const IncludeState *state,
+                                    const Define *defines)
+{
+    const Define *def = NULL;
+    char *sym = (char *) alloca(state->tokenlen + 1);
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+
+    for (def = defines; def != NULL; def = def->next)
+    {
+        assert(def->parameters == NULL);  // args can't have args!
+        assert(def->paramcount == 0);  // args can't have args!
+        if (strcmp(def->identifier, sym) == 0)
+            break;
+    } // while
+
+    return def;
+} // find_macro_arg
+
+
+static void put_all_defines(Context *ctx)
+{
+    size_t i;
+    for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
+    {
+        Define *bucket = ctx->define_hashtable[i];
+        ctx->define_hashtable[i] = NULL;
+        while (bucket)
+        {
+            Define *next = bucket->next;
+            free_define(ctx, bucket);
+            bucket = next;
+        } // while
+    } // for
+} // put_all_defines
+
+
+static int push_source(Context *ctx, const char *fname, const char *source,
+                       unsigned int srclen, unsigned int linenum,
+                       MOJOSHADER_includeClose close_callback)
+{
+    IncludeState *state = get_include(ctx);
+    if (state == NULL)
+        return 0;
+
+    if (fname != NULL)
+    {
+        state->filename = stringcache(ctx->filename_cache, fname);
+        if (state->filename == NULL)
+        {
+            put_include(ctx, state);
+            return 0;
+        } // if
+    } // if
+
+    state->close_callback = close_callback;
+    state->source_base = source;
+    state->source = source;
+    state->token = source;
+    state->tokenval = ((Token) '\n');
+    state->orig_length = srclen;
+    state->bytes_left = srclen;
+    state->line = linenum;
+    state->next = ctx->include_stack;
+    state->asm_comments = ctx->asm_comments;
+
+    print_debug_lexing_position(state);
+
+    ctx->include_stack = state;
+
+    return 1;
+} // push_source
+
+
+static void pop_source(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    assert(state != NULL);  // more pops than pushes!
+    if (state == NULL)
+        return;
+
+    if (state->close_callback)
+    {
+        state->close_callback(state->source_base, ctx->malloc,
+                              ctx->free, ctx->malloc_data);
+    } // if
+
+    // state->filename is a pointer to the filename cache; don't free it here!
+
+    Conditional *cond = state->conditional_stack;
+    while (cond)
+    {
+        Conditional *next = cond->next;
+        put_conditional(ctx, cond);
+        cond = next;
+    } // while
+
+    ctx->include_stack = state->next;
+
+    print_debug_lexing_position(ctx->include_stack);
+
+    put_include(ctx, state);
+} // pop_source
+
+
+static void close_define_include(const char *data, MOJOSHADER_malloc m,
+                                 MOJOSHADER_free f, void *d)
+{
+    f((void *) data, d);
+} // close_define_include
+
+
+Preprocessor *preprocessor_start(const char *fname, const char *source,
+                            unsigned int sourcelen,
+                            MOJOSHADER_includeOpen open_callback,
+                            MOJOSHADER_includeClose close_callback,
+                            const MOJOSHADER_preprocessorDefine *defines,
+                            unsigned int define_count, int asm_comments,
+                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    int okay = 1;
+    unsigned int i = 0;
+
+    // the preprocessor is internal-only, so we verify all these are != NULL.
+    assert(m != NULL);
+    assert(f != NULL);
+
+    Context *ctx = (Context *) m(sizeof (Context), d);
+    if (ctx == NULL)
+        return NULL;
+
+    memset(ctx, '\0', sizeof (Context));
+    ctx->malloc = m;
+    ctx->free = f;
+    ctx->malloc_data = d;
+    ctx->open_callback = open_callback;
+    ctx->close_callback = close_callback;
+    ctx->asm_comments = asm_comments;
+
+    ctx->filename_cache = stringcache_create(MallocBridge, FreeBridge, ctx);
+    okay = ((okay) && (ctx->filename_cache != NULL));
+
+    ctx->file_macro = get_define(ctx);
+    okay = ((okay) && (ctx->file_macro != NULL));
+    if ((okay) && (ctx->file_macro))
+        okay = ((ctx->file_macro->identifier = StrDup(ctx, "__FILE__")) != 0);
+
+    ctx->line_macro = get_define(ctx);
+    okay = ((okay) && (ctx->line_macro != NULL));
+    if ((okay) && (ctx->line_macro))
+        okay = ((ctx->line_macro->identifier = StrDup(ctx, "__LINE__")) != 0);
+
+    // let the usual preprocessor parser sort these out.
+    char *define_include = NULL;
+    unsigned int define_include_len = 0;
+    if ((okay) && (define_count > 0))
+    {
+        Buffer *predefbuf = buffer_create(256, MallocBridge, FreeBridge, ctx);
+        okay = okay && (predefbuf != NULL);
+        for (i = 0; okay && (i < define_count); i++)
+        {
+            okay = okay && buffer_append_fmt(predefbuf, "#define %s %s\n",
+                                 defines[i].identifier, defines[i].definition);
+        } // for
+
+        define_include_len = buffer_size(predefbuf);
+        if (define_include_len > 0)
+        {
+            define_include = buffer_flatten(predefbuf);
+            okay = okay && (define_include != NULL);
+        } // if
+        buffer_destroy(predefbuf);
+    } // if
+
+    if ((okay) && (!push_source(ctx,fname,source,sourcelen,1,NULL)))
+        okay = 0;
+
+    if ((okay) && (define_include_len > 0))
+    {
+        assert(define_include != NULL);
+        okay = push_source(ctx, "<predefined macros>", define_include,
+                           define_include_len, 1, close_define_include);
+    } // if
+
+    if (!okay)
+    {
+        preprocessor_end((Preprocessor *) ctx);
+        return NULL;
+    } // if
+
+    return (Preprocessor *) ctx;
+} // preprocessor_start
+
+
+void preprocessor_end(Preprocessor *_ctx)
+{
+    Context *ctx = (Context *) _ctx;
+    if (ctx == NULL)
+        return;
+
+    while (ctx->include_stack != NULL)
+        pop_source(ctx);
+
+    put_all_defines(ctx);
+
+    if (ctx->filename_cache != NULL)
+        stringcache_destroy(ctx->filename_cache);
+
+    free_define(ctx, ctx->file_macro);
+    free_define(ctx, ctx->line_macro);
+    free_define_pool(ctx);
+    free_conditional_pool(ctx);
+    free_include_pool(ctx);
+
+    Free(ctx, ctx);
+} // preprocessor_end
+
+
+int preprocessor_outofmemory(Preprocessor *_ctx)
+{
+    Context *ctx = (Context *) _ctx;
+    return ctx->out_of_memory;
+} // preprocessor_outofmemory
+
+
+static inline void pushback(IncludeState *state)
+{
+    #if DEBUG_PREPROCESSOR
+    printf("PREPROCESSOR PUSHBACK\n");
+    #endif
+    assert(!state->pushedback);
+    state->pushedback = 1;
+} // pushback
+
+
+static Token lexer(IncludeState *state)
+{
+    if (!state->pushedback)
+        return preprocessor_lexer(state);
+    state->pushedback = 0;
+    return state->tokenval;
+} // lexer
+
+
+// !!! FIXME: parsing fails on preprocessor directives should skip rest of line.
+static int require_newline(IncludeState *state)
+{
+    const Token token = lexer(state);
+    pushback(state);  // rewind no matter what.
+    return ( (token == TOKEN_INCOMPLETE_COMMENT) || // call it an eol.
+             (token == ((Token) '\n')) || (token == TOKEN_EOI) );
+} // require_newline
+
+// !!! FIXME: didn't we implement this by hand elsewhere?
+static int token_to_int(IncludeState *state)
+{
+    assert(state->tokenval == TOKEN_INT_LITERAL);
+    char *buf = (char *) alloca(state->tokenlen+1);
+    memcpy(buf, state->token, state->tokenlen);
+    buf[state->tokenlen] = '\0';
+    return atoi(buf);
+} // token_to_int
+
+
+static void handle_pp_include(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    Token token = lexer(state);
+    MOJOSHADER_includeType incltype;
+    char *filename = NULL;
+    int bogus = 0;
+
+    if (token == TOKEN_STRING_LITERAL)
+        incltype = MOJOSHADER_INCLUDETYPE_LOCAL;
+    else if (token == ((Token) '<'))
+    {
+        incltype = MOJOSHADER_INCLUDETYPE_SYSTEM;
+        // can't use lexer, since every byte between the < > pair is
+        //  considered part of the filename.  :/
+        while (!bogus)
+        {
+            if ( !(bogus = (state->bytes_left == 0)) )
+            {
+                const char ch = *state->source;
+                if ( !(bogus = ((ch == '\r') || (ch == '\n'))) )
+                {
+                    state->source++;
+                    state->bytes_left--;
+
+                    if (ch == '>')
+                        break;
+                } // if
+            } // if
+        } // while
+    } // else if
+    else
+    {
+        bogus = 1;
+    } // else
+
+    if (!bogus)
+    {
+        state->token++;  // skip '<' or '\"'...
+        const unsigned int len = ((unsigned int) (state->source-state->token));
+        filename = (char *) alloca(len);
+        memcpy(filename, state->token, len-1);
+        filename[len-1] = '\0';
+        bogus = !require_newline(state);
+    } // if
+
+    if (bogus)
+    {
+        fail(ctx, "Invalid #include directive");
+        return;
+    } // else
+
+    const char *newdata = NULL;
+    unsigned int newbytes = 0;
+    if ((ctx->open_callback == NULL) || (ctx->close_callback == NULL))
+    {
+        fail(ctx, "Saw #include, but no include callbacks defined");
+        return;
+    } // if
+
+    if (!ctx->open_callback(incltype, filename, state->source_base,
+                            &newdata, &newbytes, ctx->malloc,
+                            ctx->free, ctx->malloc_data))
+    {
+        fail(ctx, "Include callback failed");  // !!! FIXME: better error
+        return;
+    } // if
+
+    MOJOSHADER_includeClose callback = ctx->close_callback;
+    if (!push_source(ctx, filename, newdata, newbytes, 1, callback))
+    {
+        assert(ctx->out_of_memory);
+        ctx->close_callback(newdata, ctx->malloc, ctx->free, ctx->malloc_data);
+    } // if
+} // handle_pp_include
+
+
+static void handle_pp_line(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    char *filename = NULL;
+    int linenum = 0;
+    int bogus = 0;
+
+    if (lexer(state) != TOKEN_INT_LITERAL)
+        bogus = 1;
+    else
+        linenum = token_to_int(state);
+
+    if (!bogus)
+    {
+        Token t = lexer(state);
+        if (t == ((Token) '\n'))
+        {
+            state->line = linenum;
+            return;
+        }
+        bogus = (t != TOKEN_STRING_LITERAL);
+    }
+
+    if (!bogus)
+    {
+        state->token++;  // skip '\"'...
+        filename = (char *) alloca(state->tokenlen);
+        memcpy(filename, state->token, state->tokenlen-1);
+        filename[state->tokenlen-1] = '\0';
+        bogus = !require_newline(state);
+    } // if
+
+    if (bogus)
+    {
+        fail(ctx, "Invalid #line directive");
+        return;
+    } // if
+
+    const char *cached = stringcache(ctx->filename_cache, filename);
+    state->filename = cached;  // may be NULL if stringcache() failed.
+    state->line = linenum;
+} // handle_pp_line
+
+
+static void handle_pp_error(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    char *ptr = ctx->failstr;
+    int avail = sizeof (ctx->failstr) - 1;
+    int cpy = 0;
+    int done = 0;
+
+    const char *prefix = "#error";
+    const size_t prefixlen = strlen(prefix);
+    strcpy(ctx->failstr, prefix);
+    avail -= prefixlen;
+    ptr += prefixlen;
+
+    state->report_whitespace = 1;
+    while (!done)
+    {
+        const Token token = lexer(state);
+        switch (token)
+        {
+            case ((Token) '\n'):
+                state->line--;  // make sure error is on the right line.
+                // fall through!
+            case TOKEN_INCOMPLETE_COMMENT:
+            case TOKEN_EOI:
+                pushback(state);  // move back so we catch this later.
+                done = 1;
+                break;
+
+            case ((Token) ' '):
+                if (!avail)
+                    break;
+                *(ptr++) = ' ';
+                avail--;
+                break;
+
+            default:
+                cpy = Min(avail, (int) state->tokenlen);
+                if (cpy)
+                    memcpy(ptr, state->token, cpy);
+                ptr += cpy;
+                avail -= cpy;
+                break;
+        } // switch
+    } // while
+
+    *ptr = '\0';
+    state->report_whitespace = 0;
+    ctx->isfail = 1;
+} // handle_pp_error
+
+
+static void handle_pp_define(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    int done = 0;
+
+    if (lexer(state) != TOKEN_IDENTIFIER)
+    {
+        fail(ctx, "Macro names must be identifiers");
+        return;
+    } // if
+
+    char *definition = NULL;
+    char *sym = (char *) Malloc(ctx, state->tokenlen+1);
+    if (sym == NULL)
+        return;
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+
+    if (strcmp(sym, "defined") == 0)
+    {
+        Free(ctx, sym);
+        fail(ctx, "'defined' cannot be used as a macro name");
+        return;
+    } // if
+
+    // Don't treat these symbols as special anymore if they get (re)#defined.
+    if (strcmp(sym, "__FILE__") == 0)
+    {
+        if (ctx->file_macro)
+        {
+            failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning?
+            free_define(ctx, ctx->file_macro);
+            ctx->file_macro = NULL;
+        } // if
+    } // if
+    else if (strcmp(sym, "__LINE__") == 0)
+    {
+        if (ctx->line_macro)
+        {
+            failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning?
+            free_define(ctx, ctx->line_macro);
+            ctx->line_macro = NULL;
+        } // if
+    } // else if
+
+    // #define a(b) is different than #define a (b)    :(
+    state->report_whitespace = 1;
+    lexer(state);
+    state->report_whitespace = 0;
+
+    int params = 0;
+    char **idents = NULL;
+    static const char space = ' ';
+
+    if (state->tokenval == ((Token) ' '))
+        lexer(state);  // skip it.
+    else if (state->tokenval == ((Token) '('))
+    {
+        IncludeState saved;
+        memcpy(&saved, state, sizeof (IncludeState));
+        while (1)
+        {
+            if (lexer(state) != TOKEN_IDENTIFIER)
+                break;
+            params++;
+            if (lexer(state) != ((Token) ','))
+                break;
+        } // while
+
+        if (state->tokenval != ((Token) ')'))
+        {
+            fail(ctx, "syntax error in macro parameter list");
+            goto handle_pp_define_failed;
+        } // if
+
+        if (params == 0)  // special case for void args: "#define a() b"
+            params = -1;
+        else
+        {
+            idents = (char **) Malloc(ctx, sizeof (char *) * params);
+            if (idents == NULL)
+                goto handle_pp_define_failed;
+
+            // roll all the way back, do it again.
+            memcpy(state, &saved, sizeof (IncludeState));
+            memset(idents, '\0', sizeof (char *) * params);
+
+            int i;
+            for (i = 0; i < params; i++)
+            {
+                lexer(state);
+                assert(state->tokenval == TOKEN_IDENTIFIER);
+
+                char *dst = (char *) Malloc(ctx, state->tokenlen+1);
+                if (dst == NULL)
+                    break;
+
+                memcpy(dst, state->token, state->tokenlen);
+                dst[state->tokenlen] = '\0';
+                idents[i] = dst;
+
+                if (i < (params-1))
+                {
+                    lexer(state);
+                    assert(state->tokenval == ((Token) ','));
+                } // if
+            } // for
+
+            if (i != params)
+            {
+                assert(ctx->out_of_memory);
+                goto handle_pp_define_failed;
+            } // if
+
+            lexer(state);
+            assert(state->tokenval == ((Token) ')'));
+        } // else
+
+        lexer(state);
+    } // else if
+
+    pushback(state);
+
+    Buffer *buffer = buffer_create(128, MallocBridge, FreeBridge, ctx);
+
+    state->report_whitespace = 1;
+    while ((!done) && (!ctx->out_of_memory))
+    {
+        const Token token = lexer(state);
+        switch (token)
+        {
+            case TOKEN_INCOMPLETE_COMMENT:
+            case TOKEN_EOI:
+                pushback(state);  // move back so we catch this later.
+                done = 1;
+                break;
+
+            case ((Token) '\n'):
+                done = 1;
+                break;
+
+            case ((Token) ' '):  // may not actually point to ' '.
+                assert(buffer_size(buffer) > 0);
+                buffer_append(buffer, &space, 1);
+                break;
+
+            default:
+                buffer_append(buffer, state->token, state->tokenlen);
+                break;
+        } // switch
+    } // while
+    state->report_whitespace = 0;
+
+    size_t buflen = buffer_size(buffer) + 1;
+    if (!ctx->out_of_memory)
+        definition = buffer_flatten(buffer);
+
+    buffer_destroy(buffer);
+
+    if (ctx->out_of_memory)
+        goto handle_pp_define_failed;
+
+    int hashhash_error = 0;
+    if ((buflen > 2) && (definition[0] == '#') && (definition[1] == '#'))
+    {
+        hashhash_error = 1;
+        buflen -= 2;
+        memmove(definition, definition + 2, buflen);
+    } // if
+
+    if (buflen > 2)
+    {
+        char *ptr = (definition + buflen) - 2;
+        if (*ptr == ' ')
+        {
+            ptr--;
+            buflen--;
+        } // if
+        if ((buflen > 2) && (ptr[0] == '#') && (ptr[-1] == '#'))
+        {
+            hashhash_error = 1;
+            buflen -= 2;
+            ptr[-1] = '\0';
+        } // if
+    } // if
+
+    if (hashhash_error)
+        fail(ctx, "'##' cannot appear at either end of a macro expansion");
+
+    assert(done);
+
+    if (!add_define(ctx, sym, definition, idents, params))
+        goto handle_pp_define_failed;
+
+    return;
+
+handle_pp_define_failed:
+    Free(ctx, sym);
+    Free(ctx, definition);
+    if (idents != NULL)
+    {
+        while (params--)
+            Free(ctx, idents[params]);
+    } // if
+    Free(ctx, idents);
+} // handle_pp_define
+
+
+static void handle_pp_undef(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+
+    if (lexer(state) != TOKEN_IDENTIFIER)
+    {
+        fail(ctx, "Macro names must be indentifiers");
+        return;
+    } // if
+
+    char *sym = (char *) alloca(state->tokenlen+1);
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+
+    if (!require_newline(state))
+    {
+        fail(ctx, "Invalid #undef directive");
+        return;
+    } // if
+
+    if (strcmp(sym, "__FILE__") == 0)
+    {
+        if (ctx->file_macro)
+        {
+            failf(ctx, "undefining \"%s\"", sym);  // !!! FIXME: should be warning.
+            free_define(ctx, ctx->file_macro);
+            ctx->file_macro = NULL;
+        } // if
+    } // if
+    else if (strcmp(sym, "__LINE__") == 0)
+    {
+        if (ctx->line_macro)
+        {
+            failf(ctx, "undefining \"%s\"", sym);  // !!! FIXME: should be warning.
+            free_define(ctx, ctx->line_macro);
+            ctx->line_macro = NULL;
+        } // if
+    } // if
+
+    remove_define(ctx, sym);
+} // handle_pp_undef
+
+
+static Conditional *_handle_pp_ifdef(Context *ctx, const Token type)
+{
+    IncludeState *state = ctx->include_stack;
+
+    assert((type == TOKEN_PP_IFDEF) || (type == TOKEN_PP_IFNDEF));
+
+    if (lexer(state) != TOKEN_IDENTIFIER)
+    {
+        fail(ctx, "Macro names must be indentifiers");
+        return NULL;
+    } // if
+
+    char *sym = (char *) alloca(state->tokenlen+1);
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+
+    if (!require_newline(state))
+    {
+        if (type == TOKEN_PP_IFDEF)
+            fail(ctx, "Invalid #ifdef directive");
+        else
+            fail(ctx, "Invalid #ifndef directive");
+        return NULL;
+    } // if
+
+    Conditional *conditional = get_conditional(ctx);
+    assert((conditional != NULL) || (ctx->out_of_memory));
+    if (conditional == NULL)
+        return NULL;
+
+    Conditional *parent = state->conditional_stack;
+    const int found = (find_define(ctx, sym) != NULL);
+    const int chosen = (type == TOKEN_PP_IFDEF) ? found : !found;
+    const int skipping = ( (((parent) && (parent->skipping))) || (!chosen) );
+
+    conditional->type = type;
+    conditional->linenum = state->line - 1;
+    conditional->skipping = skipping;
+    conditional->chosen = chosen;
+    conditional->next = parent;
+    state->conditional_stack = conditional;
+    return conditional;
+} // _handle_pp_ifdef
+
+
+static inline void handle_pp_ifdef(Context *ctx)
+{
+    _handle_pp_ifdef(ctx, TOKEN_PP_IFDEF);
+} // handle_pp_ifdef
+
+
+static inline void handle_pp_ifndef(Context *ctx)
+{
+    _handle_pp_ifdef(ctx, TOKEN_PP_IFNDEF);
+} // handle_pp_ifndef
+
+
+static int replace_and_push_macro(Context *ctx, const Define *def,
+                                  const Define *params)
+{
+    char *final = NULL;
+
+    // We push the #define and lex it, building a buffer with argument
+    //  replacement, stringification, and concatenation.
+    Buffer *buffer = buffer_create(128, MallocBridge, FreeBridge, ctx);
+    if (buffer == NULL)
+        return 0;
+
+    IncludeState *state = ctx->include_stack;
+    if (!push_source(ctx, state->filename, def->definition,
+                     strlen(def->definition), state->line, NULL))
+    {
+        buffer_destroy(buffer);
+        return 0;
+    } // if
+
+    state = ctx->include_stack;
+    while (lexer(state) != TOKEN_EOI)
+    {
+        int wantorig = 0;
+        const Define *arg = NULL;
+
+        // put a space between tokens if we're not concatenating.
+        if (state->tokenval == TOKEN_HASHHASH)  // concatenate?
+        {
+            wantorig = 1;
+            lexer(state);
+            assert(state->tokenval != TOKEN_EOI);
+        } // if
+        else
+        {
+            if (buffer_size(buffer) > 0)
+            {
+                if (!buffer_append(buffer, " ", 1))
+                    goto replace_and_push_macro_failed;
+            } // if
+        } // else
+
+        const char *data = state->token;
+        unsigned int len = state->tokenlen;
+
+        if (state->tokenval == TOKEN_HASH)  // stringify?
+        {
+            lexer(state);
+            assert(state->tokenval != TOKEN_EOI);  // we checked for this.
+
+            if (!buffer_append(buffer, "\"", 1))
+                goto replace_and_push_macro_failed;
+
+            if (state->tokenval == TOKEN_IDENTIFIER)
+            {
+                arg = find_macro_arg(state, params);
+                if (arg != NULL)
+                {
+                    data = arg->original;
+                    len = strlen(data);
+                } // if
+            } // if
+
+            if (!buffer_append(buffer, data, len))
+                goto replace_and_push_macro_failed;
+
+            if (!buffer_append(buffer, "\"", 1))
+                goto replace_and_push_macro_failed;
+
+            continue;
+        } // if
+
+        if (state->tokenval == TOKEN_IDENTIFIER)
+        {
+            arg = find_macro_arg(state, params);
+            if (arg != NULL)
+            {
+                if (!wantorig)
+                {
+                    wantorig = (lexer(state) == TOKEN_HASHHASH);
+                    pushback(state);
+                } // if
+                data = wantorig ? arg->original : arg->definition;
+                len = strlen(data);
+            } // if
+        } // if
+
+        if (!buffer_append(buffer, data, len))
+            goto replace_and_push_macro_failed;
+    } // while
+
+    final = buffer_flatten(buffer);
+    if (!final)
+        goto replace_and_push_macro_failed;
+
+    buffer_destroy(buffer);
+    pop_source(ctx);  // ditch the macro.
+    state = ctx->include_stack;
+    if (!push_source(ctx, state->filename, final, strlen(final), state->line,
+                     close_define_include))
+    {
+        Free(ctx, final);
+        return 0;
+    } // if
+
+    return 1;
+
+replace_and_push_macro_failed:
+    pop_source(ctx);
+    buffer_destroy(buffer);
+    return 0;
+} // replace_and_push_macro
+
+
+static int handle_macro_args(Context *ctx, const char *sym, const Define *def)
+{
+    int retval = 0;
+    IncludeState *state = ctx->include_stack;
+    Define *params = NULL;
+    const int expected = (def->paramcount < 0) ? 0 : def->paramcount;
+    int saw_params = 0;
+    IncludeState saved;  // can't pushback, we need the original token.
+    memcpy(&saved, state, sizeof (IncludeState));
+    if (lexer(state) != ((Token) '('))
+    {
+        memcpy(state, &saved, sizeof (IncludeState));
+        goto handle_macro_args_failed;  // gcc abandons replacement, too.
+    } // if
+
+    state->report_whitespace = 1;
+
+    int void_call = 0;
+    int paren = 1;
+    while (paren > 0)
+    {
+        Buffer *buffer = buffer_create(128, MallocBridge, FreeBridge, ctx);
+        Buffer *origbuffer = buffer_create(128, MallocBridge, FreeBridge, ctx);
+
+        Token t = lexer(state);
+
+        assert(!void_call);
+
+        while (1)
+        {
+            const char *origexpr = state->token;
+            unsigned int origexprlen = state->tokenlen;
+            const char *expr = state->token;
+            unsigned int exprlen = state->tokenlen;
+
+            if (t == ((Token) '('))
+                paren++;
+
+            else if (t == ((Token) ')'))
+            {
+                paren--;
+                if (paren < 1)  // end of macro?
+                    break;
+            } // else if
+
+            else if (t == ((Token) ','))
+            {
+                if (paren == 1)  // new macro arg?
+                    break;
+            } // else if
+
+            else if (t == ((Token) ' '))
+            {
+                // don't add whitespace to the start, so we recognize
+                //  void calls correctly.
+                origexpr = expr = " ";
+                origexprlen = (buffer_size(origbuffer) == 0) ? 0 : 1;
+                exprlen = (buffer_size(buffer) == 0) ? 0 : 1;
+            } // else if
+
+            else if (t == TOKEN_IDENTIFIER)
+            {
+                const Define *def = find_define_by_token(ctx);
+                // don't replace macros with arguments so they replace correctly, later.
+                if ((def) && (def->paramcount == 0))
+                {
+                    expr = def->definition;
+                    exprlen = strlen(def->definition);
+                } // if
+            } // else if
+
+            else if ((t == TOKEN_INCOMPLETE_COMMENT) || (t == TOKEN_EOI))
+            {
+                pushback(state);
+                fail(ctx, "Unterminated macro list");
+                goto handle_macro_args_failed;
+            } // else if
+
+            assert(expr != NULL);
+
+            if (!buffer_append(buffer, expr, exprlen))
+                goto handle_macro_args_failed;
+
+            if (!buffer_append(origbuffer, origexpr, origexprlen))
+                goto handle_macro_args_failed;
+
+            t = lexer(state);
+        } // while
+
+        if (buffer_size(buffer) == 0)
+            void_call = ((saw_params == 0) && (paren == 0));
+
+        if (saw_params < expected)
+        {
+            const int origdeflen = (int) buffer_size(origbuffer);
+            char *origdefinition = buffer_flatten(origbuffer);
+            const int deflen = (int) buffer_size(buffer);
+            char *definition = buffer_flatten(buffer);
+            Define *p = get_define(ctx);
+            if ((!origdefinition) || (!definition) || (!p))
+            {
+                Free(ctx, origdefinition);
+                Free(ctx, definition);
+                buffer_destroy(origbuffer);
+                buffer_destroy(buffer);
+                free_define(ctx, p);
+                goto handle_macro_args_failed;
+            } // if
+
+            // trim any whitespace from the end of the string...
+            int i;
+            for (i = deflen - 1; i >= 0; i--)
+            {
+                if (definition[i] == ' ')
+                    definition[i] = '\0';
+                else
+                    break;
+            } // for
+
+            for (i = origdeflen - 1; i >= 0; i--)
+            {
+                if (origdefinition[i] == ' ')
+                    origdefinition[i] = '\0';
+                else
+                    break;
+            } // for
+
+            p->identifier = def->parameters[saw_params];
+            p->definition = definition;
+            p->original = origdefinition;
+            p->next = params;
+            params = p;
+        } // if
+
+        buffer_destroy(buffer);
+        buffer_destroy(origbuffer);
+        saw_params++;
+    } // while
+
+    assert(paren == 0);
+
+    // "a()" should match "#define a()" ...
+    if ((expected == 0) && (saw_params == 1) && (void_call))
+    {
+        assert(params == NULL);
+        saw_params = 0;
+    } // if
+
+    if (saw_params != expected)
+    {
+        failf(ctx, "macro '%s' passed %d arguments, but requires %d",
+              sym, saw_params, expected);
+        goto handle_macro_args_failed;
+    } // if
+
+    // this handles arg replacement and the '##' and '#' operators.
+    retval = replace_and_push_macro(ctx, def, params);
+
+handle_macro_args_failed:
+    while (params)
+    {
+        Define *next = params->next;
+        params->identifier = NULL;
+        free_define(ctx, params);
+        params = next;
+    } // while
+
+    state->report_whitespace = 0;
+    return retval;
+} // handle_macro_args
+
+
+static int handle_pp_identifier(Context *ctx)
+{
+    if (ctx->recursion_count++ >= 256)  // !!! FIXME: gcc can figure this out.
+    {
+        fail(ctx, "Recursing macros");
+        return 0;
+    } // if
+
+    IncludeState *state = ctx->include_stack;
+    const char *fname = state->filename;
+    const unsigned int line = state->line;
+    char *sym = (char *) alloca(state->tokenlen+1);
+    memcpy(sym, state->token, state->tokenlen);
+    sym[state->tokenlen] = '\0';
+
+    // Is this identifier #defined?
+    const Define *def = find_define(ctx, sym);
+    if (def == NULL)
+        return 0;   // just send the token through unchanged.
+    else if (def->paramcount != 0)
+        return handle_macro_args(ctx, sym, def);
+
+    const size_t deflen = strlen(def->definition);
+    return push_source(ctx, fname, def->definition, deflen, line, NULL);
+} // handle_pp_identifier
+
+
+static int find_precedence(const Token token)
+{
+    // operator precedence, left and right associative...
+    typedef struct { int precedence; Token token; } Precedence;
+    static const Precedence ops[] = {
+        { 0, TOKEN_OROR }, { 1, TOKEN_ANDAND }, { 2, ((Token) '|') },
+        { 3, ((Token) '^') }, { 4, ((Token) '&') }, { 5, TOKEN_NEQ },
+        { 6, TOKEN_EQL }, { 7, ((Token) '<') }, { 7, ((Token) '>') },
+        { 7, TOKEN_LEQ }, { 7, TOKEN_GEQ }, { 8, TOKEN_LSHIFT },
+        { 8, TOKEN_RSHIFT }, { 9, ((Token) '-') }, { 9, ((Token) '+') },
+        { 10, ((Token) '%') }, { 10, ((Token) '/') }, { 10, ((Token) '*') },
+        { 11, TOKEN_PP_UNARY_PLUS }, { 11, TOKEN_PP_UNARY_MINUS },
+        { 11, ((Token) '!') }, { 11, ((Token) '~') },
+    };
+
+    size_t i;
+    for (i = 0; i < STATICARRAYLEN(ops); i++)
+    {
+        if (ops[i].token == token)
+            return ops[i].precedence;
+    } // for
+
+    return -1;
+} // find_precedence
+
+// !!! FIXME: we're using way too much stack space here...
+typedef struct RpnTokens
+{
+    int isoperator;
+    int value;
+} RpnTokens;
+
+static long interpret_rpn(const RpnTokens *tokens, int tokencount, int *error)
+{
+    long stack[128];
+    size_t stacksize = 0;
+
+    *error = 1;
+
+    #define NEED_X_TOKENS(x) do { if (stacksize < x) return 0; } while (0)
+
+    #define BINARY_OPERATION(op) do { \
+        NEED_X_TOKENS(2); \
+        stack[stacksize-2] = stack[stacksize-2] op stack[stacksize-1]; \
+        stacksize--; \
+    } while (0)
+
+    #define UNARY_OPERATION(op) do { \
+        NEED_X_TOKENS(1); \
+        stack[stacksize-1] = op stack[stacksize-1]; \
+    } while (0)
+
+    while (tokencount-- > 0)
+    {
+        if (!tokens->isoperator)
+        {
+            assert(stacksize < STATICARRAYLEN(stack));
+            stack[stacksize++] = (long) tokens->value;
+            tokens++;
+            continue;
+        } // if
+
+        // operators.
+        switch (tokens->value)
+        {
+            case '!': UNARY_OPERATION(!); break;
+            case '~': UNARY_OPERATION(~); break;
+            case TOKEN_PP_UNARY_MINUS: UNARY_OPERATION(-); break;
+            case TOKEN_PP_UNARY_PLUS: UNARY_OPERATION(+); break;
+            case TOKEN_OROR: BINARY_OPERATION(||); break;
+            case TOKEN_ANDAND: BINARY_OPERATION(&&); break;
+            case '|': BINARY_OPERATION(|); break;
+            case '^': BINARY_OPERATION(^); break;
+            case '&': BINARY_OPERATION(&); break;
+            case TOKEN_NEQ: BINARY_OPERATION(!=); break;
+            case TOKEN_EQL: BINARY_OPERATION(==); break;
+            case '<': BINARY_OPERATION(<); break;
+            case '>': BINARY_OPERATION(>); break;
+            case TOKEN_LEQ: BINARY_OPERATION(<=); break;
+            case TOKEN_GEQ: BINARY_OPERATION(>=); break;
+            case TOKEN_LSHIFT: BINARY_OPERATION(<<); break;
+            case TOKEN_RSHIFT: BINARY_OPERATION(>>); break;
+            case '-': BINARY_OPERATION(-); break;
+            case '+': BINARY_OPERATION(+); break;
+            case '%': BINARY_OPERATION(%); break;
+            case '/': BINARY_OPERATION(/); break;
+            case '*': BINARY_OPERATION(*); break;
+            default: return 0;
+        } // switch
+
+        tokens++;
+    } // while
+
+    #undef NEED_X_TOKENS
+    #undef BINARY_OPERATION
+    #undef UNARY_OPERATION
+
+    if (stacksize != 1)
+        return 0;
+
+    *error = 0;
+    return stack[0];
+} // interpret_rpn
+
+// http://en.wikipedia.org/wiki/Shunting_yard_algorithm
+//  Convert from infix to postfix, then use this for constant folding.
+//  Everything that parses should fold down to a constant value: any
+//  identifiers that aren't resolved as macros become zero. Anything we
+//  don't explicitly expect becomes a parsing error.
+// returns 1 (true), 0 (false), or -1 (error)
+static int reduce_pp_expression(Context *ctx)
+{
+    IncludeState *orig_state = ctx->include_stack;
+    RpnTokens output[128];
+    Token stack[64];
+    Token previous_token = TOKEN_UNKNOWN;
+    size_t outputsize = 0;
+    size_t stacksize = 0;
+    int matched = 0;
+    int done = 0;
+
+    #define ADD_TO_OUTPUT(op, val) \
+        assert(outputsize < STATICARRAYLEN(output)); \
+        output[outputsize].isoperator = op; \
+        output[outputsize].value = val; \
+        outputsize++;
+
+    #define PUSH_TO_STACK(t) \
+        assert(stacksize < STATICARRAYLEN(stack)); \
+        stack[stacksize] = t; \
+        stacksize++;
+
+    while (!done)
+    {
+        IncludeState *state = ctx->include_stack;
+        Token token = lexer(state);
+        int isleft = 1;
+        int precedence = -1;
+
+        if ( (token == ((Token) '!')) || (token == ((Token) '~')) )
+            isleft = 0;
+        else if (token == ((Token) '-'))
+        {
+            if ((isleft = (previous_token == TOKEN_INT_LITERAL)) == 0)
+                token = TOKEN_PP_UNARY_MINUS;
+        } // else if
+        else if (token == ((Token) '+'))
+        {
+            if ((isleft = (previous_token == TOKEN_INT_LITERAL)) == 0)
+                token = TOKEN_PP_UNARY_PLUS;
+        } // else if
+
+        if (token != TOKEN_IDENTIFIER)
+            ctx->recursion_count = 0;
+
+        switch (token)
+        {
+            case TOKEN_EOI:
+                if (state != orig_state)  // end of a substate, or the expr?
+                {
+                    pop_source(ctx);
+                    continue;  // substate, go again with the parent state.
+                } // if
+                done = 1;  // the expression itself is done.
+                break;
+
+            case ((Token) '\n'):
+                done = 1;
+                break;  // we're done!
+
+            case TOKEN_IDENTIFIER:
+                if (handle_pp_identifier(ctx))
+                    continue;  // go again with new IncludeState.
+
+                if ( (state->tokenlen == 7) &&
+                     (memcmp(state->token, "defined", 7) == 0) )
+                {
+                    token = lexer(state);
+                    const int paren = (token == ((Token) '('));
+                    if (paren)  // gcc doesn't let us nest parens here, either.
+                        token = lexer(state);
+                    if (token != TOKEN_IDENTIFIER)
+                    {
+                        fail(ctx, "operator 'defined' requires an identifier");
+                        return -1;
+                    } // if
+                    const int found = (find_define_by_token(ctx) != NULL);
+
+                    if (paren)
+                    {
+                        if (lexer(state) != ((Token) ')'))
+                        {
+                            fail(ctx, "Unmatched ')'");
+                            return -1;
+                        } // if
+                    } // if
+
+                    ADD_TO_OUTPUT(0, found);
+                    continue;
+                } // if
+
+                // can't replace identifier with a number? It becomes zero.
+                token = TOKEN_INT_LITERAL;
+                ADD_TO_OUTPUT(0, 0);
+                break;
+
+            case TOKEN_INT_LITERAL:
+                ADD_TO_OUTPUT(0, token_to_int(state));
+                break;
+
+            case ((Token) '('):
+                PUSH_TO_STACK((Token) '(');
+                break;
+
+            case ((Token) ')'):
+                matched = 0;
+                while (stacksize > 0)
+                {
+                    const Token t = stack[--stacksize];
+                    if (t == ((Token) '('))
+                    {
+                        matched = 1;
+                        break;
+                    } // if
+                    ADD_TO_OUTPUT(1, t);
+                } // while
+
+                if (!matched)
+                {
+                    fail(ctx, "Unmatched ')'");
+                    return -1;
+                } // if
+                break;
+
+            default:
+                precedence = find_precedence(token);
+                // bogus token, or two operators together.
+                if (precedence < 0)
+                {
+                    pushback(state);
+                    fail(ctx, "Invalid expression");
+                    return -1;
+                } // if
+
+                else  // it's an operator.
+                {
+                    while (stacksize > 0)
+                    {
+                        const Token t = stack[stacksize-1];
+                        const int p = find_precedence(t);
+                        if ( (p >= 0) &&
+                             ( ((isleft) && (precedence <= p)) ||
+                               ((!isleft) && (precedence < p)) ) )
+                        {
+                            stacksize--;
+                            ADD_TO_OUTPUT(1, t);
+                        } // if
+                        else
+                        {
+                            break;
+                        } // else
+                    } // while
+                    PUSH_TO_STACK(token);
+                } // else
+                break;
+        } // switch
+        previous_token = token;
+    } // while
+
+    while (stacksize > 0)
+    {
+        const Token t = stack[--stacksize];
+        if (t == ((Token) '('))
+        {
+            fail(ctx, "Unmatched ')'");
+            return -1;
+        } // if
+        ADD_TO_OUTPUT(1, t);
+    } // while
+
+    #undef ADD_TO_OUTPUT
+    #undef PUSH_TO_STACK
+
+    // okay, you now have some validated data in reverse polish notation.
+    #if DEBUG_PREPROCESSOR
+    printf("PREPROCESSOR EXPRESSION RPN:");
+    int i = 0;
+    for (i = 0; i < outputsize; i++)
+    {
+        if (!output[i].isoperator)
+            printf(" %d", output[i].value);
+        else
+        {
+            switch (output[i].value)
+            {
+                case TOKEN_OROR: printf(" ||"); break;
+                case TOKEN_ANDAND: printf(" &&"); break;
+                case TOKEN_NEQ: printf(" !="); break;
+                case TOKEN_EQL: printf(" =="); break;
+                case TOKEN_LEQ: printf(" <="); break;
+                case TOKEN_GEQ: printf(" >="); break;
+                case TOKEN_LSHIFT: printf(" <<"); break;
+                case TOKEN_RSHIFT: printf(" >>"); break;
+                case TOKEN_PP_UNARY_PLUS: printf(" +"); break;
+                case TOKEN_PP_UNARY_MINUS: printf(" -"); break;
+                default: printf(" %c", output[i].value); break;
+            } // switch
+        } // else
+    } // for
+    printf("\n");
+    #endif
+
+    int error = 0;
+    const long val = interpret_rpn(output, outputsize, &error);
+
+    #if DEBUG_PREPROCESSOR
+    printf("PREPROCESSOR RPN RESULT: %ld%s\n", val, error ? " (ERROR)" : "");
+    #endif
+
+    if (error)
+    {
+        fail(ctx, "Invalid expression");
+        return -1;
+    } // if
+
+    return ((val) ? 1 : 0);
+} // reduce_pp_expression
+
+
+static Conditional *handle_pp_if(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    const int result = reduce_pp_expression(ctx);
+    if (result == -1)
+        return NULL;
+
+    Conditional *conditional = get_conditional(ctx);
+    assert((conditional != NULL) || (ctx->out_of_memory));
+    if (conditional == NULL)
+        return NULL;
+
+    Conditional *parent = state->conditional_stack;
+    const int chosen = result;
+    const int skipping = ( (((parent) && (parent->skipping))) || (!chosen) );
+
+    conditional->type = TOKEN_PP_IF;
+    conditional->linenum = state->line - 1;
+    conditional->skipping = skipping;
+    conditional->chosen = chosen;
+    conditional->next = parent;
+    state->conditional_stack = conditional;
+    return conditional;
+} // handle_pp_if
+
+
+static void handle_pp_elif(Context *ctx)
+{
+    const int rc = reduce_pp_expression(ctx);
+    if (rc == -1)
+        return;
+
+    IncludeState *state = ctx->include_stack;
+    Conditional *cond = state->conditional_stack;
+    if (cond == NULL)
+        fail(ctx, "#elif without #if");
+    else if (cond->type == TOKEN_PP_ELSE)
+        fail(ctx, "#elif after #else");
+    else
+    {
+        const Conditional *parent = cond->next;
+        cond->type = TOKEN_PP_ELIF;
+        cond->skipping = (parent && parent->skipping) || cond->chosen || !rc;
+        if (!cond->chosen)
+            cond->chosen = rc;
+    } // else
+} // handle_pp_elif
+
+
+static void handle_pp_else(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    Conditional *cond = state->conditional_stack;
+
+    if (!require_newline(state))
+        fail(ctx, "Invalid #else directive");
+    else if (cond == NULL)
+        fail(ctx, "#else without #if");
+    else if (cond->type == TOKEN_PP_ELSE)
+        fail(ctx, "#else after #else");
+    else
+    {
+        const Conditional *parent = cond->next;
+        cond->type = TOKEN_PP_ELSE;
+        cond->skipping = (parent && parent->skipping) || cond->chosen;
+        if (!cond->chosen)
+            cond->chosen = 1;
+    } // else
+} // handle_pp_else
+
+
+static void handle_pp_endif(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    Conditional *cond = state->conditional_stack;
+
+    if (!require_newline(state))
+        fail(ctx, "Invalid #endif directive");
+    else if (cond == NULL)
+        fail(ctx, "Unmatched #endif");
+    else
+    {
+        state->conditional_stack = cond->next;  // pop it.
+        put_conditional(ctx, cond);
+    } // else
+} // handle_pp_endif
+
+
+static void unterminated_pp_condition(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    Conditional *cond = state->conditional_stack;
+
+    // !!! FIXME: report the line number where the #if is, not the EOI.
+    switch (cond->type)
+    {
+        case TOKEN_PP_IF: fail(ctx, "Unterminated #if"); break;
+        case TOKEN_PP_IFDEF: fail(ctx, "Unterminated #ifdef"); break;
+        case TOKEN_PP_IFNDEF: fail(ctx, "Unterminated #ifndef"); break;
+        case TOKEN_PP_ELSE: fail(ctx, "Unterminated #else"); break;
+        case TOKEN_PP_ELIF: fail(ctx, "Unterminated #elif"); break;
+        default: assert(0 && "Shouldn't hit this case"); break;
+    } // switch
+
+    // pop this conditional, we'll report the next error next time...
+
+    state->conditional_stack = cond->next;  // pop it.
+    put_conditional(ctx, cond);
+} // unterminated_pp_condition
+
+
+static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
+                                             unsigned int *_len, Token *_token)
+{
+    Context *ctx = (Context *) _ctx;
+
+    while (1)
+    {
+        if (ctx->isfail)
+        {
+            ctx->isfail = 0;
+            *_token = TOKEN_PREPROCESSING_ERROR;
+            *_len = strlen(ctx->failstr);
+            return ctx->failstr;
+        } // if
+
+        IncludeState *state = ctx->include_stack;
+        if (state == NULL)
+        {
+            *_token = TOKEN_EOI;
+            *_len = 0;
+            return NULL;  // we're done!
+        } // if
+
+        const Conditional *cond = state->conditional_stack;
+        const int skipping = ((cond != NULL) && (cond->skipping));
+
+        const Token token = lexer(state);
+
+        if (token != TOKEN_IDENTIFIER)
+            ctx->recursion_count = 0;
+
+        if (token == TOKEN_EOI)
+        {
+            assert(state->bytes_left == 0);
+            if (state->conditional_stack != NULL)
+            {
+                unterminated_pp_condition(ctx);
+                continue;  // returns an error.
+            } // if
+
+            pop_source(ctx);
+            continue;  // pick up again after parent's #include line.
+        } // if
+
+        else if (token == TOKEN_INCOMPLETE_COMMENT)
+        {
+            fail(ctx, "Incomplete multiline comment");
+            continue;  // will return at top of loop.
+        } // else if
+
+        else if (token == TOKEN_PP_IFDEF)
+        {
+            handle_pp_ifdef(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_IFNDEF)
+        {
+            handle_pp_ifndef(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_IF)
+        {
+            handle_pp_if(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_ELIF)
+        {
+            handle_pp_elif(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_ENDIF)
+        {
+            handle_pp_endif(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_ELSE)
+        {
+            handle_pp_else(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        // NOTE: Conditionals must be above (skipping) test.
+        else if (skipping)
+            continue;  // just keep dumping tokens until we get end of block.
+
+        else if (token == TOKEN_PP_INCLUDE)
+        {
+            handle_pp_include(ctx);
+            continue;  // will return error or use new top of include_stack.
+        } // else if
+
+        else if (token == TOKEN_PP_LINE)
+        {
+            handle_pp_line(ctx);
+            continue;  // get the next thing.
+        } // else if
+
+        else if (token == TOKEN_PP_ERROR)
+        {
+            handle_pp_error(ctx);
+            continue;  // will return at top of loop.
+        } // else if
+
+        else if (token == TOKEN_PP_DEFINE)
+        {
+            handle_pp_define(ctx);
+            continue;  // will return at top of loop.
+        } // else if
+
+        else if (token == TOKEN_PP_UNDEF)
+        {
+            handle_pp_undef(ctx);
+            continue;  // will return at top of loop.
+        } // else if
+
+        else if (token == TOKEN_PP_PRAGMA)
+        {
+            ctx->parsing_pragma = 1;
+        } // else if
+
+        if (token == TOKEN_IDENTIFIER)
+        {
+            if (handle_pp_identifier(ctx))
+                continue;  // pushed the include_stack.
+        } // else if
+
+        else if (token == ((Token) '\n'))
+        {
+            print_debug_lexing_position(state);
+            if (ctx->parsing_pragma)  // let this one through.
+                ctx->parsing_pragma = 0;
+            else
+            {
+                // preprocessor is line-oriented, nothing else gets newlines.
+                continue;  // get the next thing.
+            } // else
+        } // else if
+
+        assert(!skipping);
+        *_token = token;
+        *_len = state->tokenlen;
+        return state->token;
+    } // while
+
+    assert(0 && "shouldn't hit this code");
+    *_token = TOKEN_UNKNOWN;
+    *_len = 0;
+    return NULL;
+} // _preprocessor_nexttoken
+
+
+const char *preprocessor_nexttoken(Preprocessor *ctx, unsigned int *len,
+                                   Token *token)
+{
+    const char *retval = _preprocessor_nexttoken(ctx, len, token);
+    print_debug_token(retval, *len, *token);
+    return retval;
+} // preprocessor_nexttoken
+
+
+const char *preprocessor_sourcepos(Preprocessor *_ctx, unsigned int *pos)
+{
+    Context *ctx = (Context *) _ctx;
+    if (ctx->include_stack == NULL)
+    {
+        *pos = 0;
+        return NULL;
+    } // if
+
+    *pos = ctx->include_stack->line;
+    return ctx->include_stack->filename;
+} // preprocessor_sourcepos
+
+
+static void indent_buffer(Buffer *buffer, int n, const int newline)
+{
+    static char spaces[4] = { ' ', ' ', ' ', ' ' };
+    if (newline)
+    {
+        while (n--)
+        {
+            if (!buffer_append(buffer, spaces, sizeof (spaces)))
+                return;
+        } // while
+    } // if
+    else
+    {
+        if (!buffer_append(buffer, spaces, 1))
+            return;
+    } // else
+} // indent_buffer
+
+
+static const MOJOSHADER_preprocessData out_of_mem_data_preprocessor = {
+    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0
+};
+
+
+// public API...
+
+const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
+                             const char *source, unsigned int sourcelen,
+                             const MOJOSHADER_preprocessorDefine *defines,
+                             unsigned int define_count,
+                             MOJOSHADER_includeOpen include_open,
+                             MOJOSHADER_includeClose include_close,
+                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
+{
+    MOJOSHADER_preprocessData *retval = NULL;
+    Preprocessor *pp = NULL;
+    ErrorList *errors = NULL;
+    Buffer *buffer = NULL;
+    Token token = TOKEN_UNKNOWN;
+    const char *tokstr = NULL;
+    int nl = 1;
+    int indent = 0;
+    unsigned int len = 0;
+    char *output = NULL;
+    int errcount = 0;
+    size_t total_bytes = 0;
+
+    // !!! FIXME: what's wrong with ENDLINE_STR?
+    #ifdef _WINDOWS
+    static const char endline[] = { '\r', '\n' };
+    #else
+    static const char endline[] = { '\n' };
+    #endif
+
+    if (!m) m = MOJOSHADER_internal_malloc;
+    if (!f) f = MOJOSHADER_internal_free;
+    if (!include_open) include_open = MOJOSHADER_internal_include_open;
+    if (!include_close) include_close = MOJOSHADER_internal_include_close;
+
+    pp = preprocessor_start(filename, source, sourcelen,
+                            include_open, include_close,
+                            defines, define_count, 0, m, f, d);
+    if (pp == NULL)
+        goto preprocess_out_of_mem;
+
+    errors = errorlist_create(MallocBridge, FreeBridge, pp);
+    if (errors == NULL)
+        goto preprocess_out_of_mem;
+
+    buffer = buffer_create(4096, MallocBridge, FreeBridge, pp);
+    if (buffer == NULL)
+        goto preprocess_out_of_mem;
+
+    while ((tokstr = preprocessor_nexttoken(pp, &len, &token)) != NULL)
+    {
+        int isnewline = 0;
+
+        assert(token != TOKEN_EOI);
+
+        if (preprocessor_outofmemory(pp))
+            goto preprocess_out_of_mem;
+
+        // Microsoft's preprocessor is weird.
+        // It ignores newlines, and then inserts its own around certain
+        //  tokens. For example, after a semicolon. This allows HLSL code to
+        //  be mostly readable, instead of a stream of tokens.
+        if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
+        {
+            if ( (token == ((Token) '}')) && (indent > 0) )
+                indent--;
+
+            indent_buffer(buffer, indent, nl);
+            buffer_append(buffer, tokstr, len);
+            buffer_append(buffer, endline, sizeof (endline));
+
+            isnewline = 1;
+        } // if
+
+        else if (token == ((Token) '\n'))
+        {
+            buffer_append(buffer, endline, sizeof (endline));
+            isnewline = 1;
+        } // else if
+
+        else if (token == ((Token) '{'))
+        {
+            buffer_append(buffer, endline, sizeof (endline));
+            indent_buffer(buffer, indent, 1);
+            buffer_append(buffer, "{", 1);
+            buffer_append(buffer, endline, sizeof (endline));
+            indent++;
+            isnewline = 1;
+        } // else if
+
+        else if (token == TOKEN_PREPROCESSING_ERROR)
+        {
+            unsigned int pos = 0;
+            const char *fname = preprocessor_sourcepos(pp, &pos);
+            errorlist_add(errors, fname, (int) pos, tokstr);
+        } // else if
+
+        else
+        {
+            indent_buffer(buffer, indent, nl);
+            buffer_append(buffer, tokstr, len);
+        } // else
+
+        nl = isnewline;
+    } // while
+    
+    assert(token == TOKEN_EOI);
+
+    total_bytes = buffer_size(buffer);
+    output = buffer_flatten(buffer);
+    buffer_destroy(buffer);
+    buffer = NULL;  // don't free this pointer again.
+
+    if (output == NULL)
+        goto preprocess_out_of_mem;
+
+    retval = (MOJOSHADER_preprocessData *) m(sizeof (*retval), d);
+    if (retval == NULL)
+        goto preprocess_out_of_mem;
+
+    memset(retval, '\0', sizeof (*retval));
+    errcount = errorlist_count(errors);
+    if (errcount > 0)
+    {
+        retval->error_count = errcount;
+        retval->errors = errorlist_flatten(errors);
+        if (retval->errors == NULL)
+            goto preprocess_out_of_mem;
+    } // if
+
+    retval->output = output;
+    retval->output_len = total_bytes;
+    retval->malloc = m;
+    retval->free = f;
+    retval->malloc_data = d;
+
+    errorlist_destroy(errors);
+    preprocessor_end(pp);
+    return retval;
+
+preprocess_out_of_mem:
+    if (retval != NULL)
+        f(retval->errors, d);
+    f(retval, d);
+    f(output, d);
+    buffer_destroy(buffer);
+    errorlist_destroy(errors);
+    preprocessor_end(pp);
+    return &out_of_mem_data_preprocessor;
+} // MOJOSHADER_preprocess
+
+
+void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *_data)
+{
+    MOJOSHADER_preprocessData *data = (MOJOSHADER_preprocessData *) _data;
+    if ((data == NULL) || (data == &out_of_mem_data_preprocessor))
+        return;
+
+    MOJOSHADER_free f = (data->free == NULL) ? MOJOSHADER_internal_free : data->free;
+    void *d = data->malloc_data;
+    int i;
+
+    f((void *) data->output, d);
+
+    for (i = 0; i < data->error_count; i++)
+    {
+        f((void *) data->errors[i].error, d);
+        f((void *) data->errors[i].filename, d);
+    } // for
+    f(data->errors, d);
+
+    f(data, d);
+} // MOJOSHADER_freePreprocessData
+
+
+// end of mojoshader_preprocessor.c ...
+

+ 4 - 1
Tools/ShaderCompiler/CMakeLists.txt

@@ -9,9 +9,12 @@ set (SOURCE_FILES ${CPP_FILES} ${H_FILES})
 # Include directories
 include_directories (
     ../../Engine/Container ../../Engine/Core ../../Engine/Graphics ../../Engine/IO ../../Engine/Math ../../Engine/Resource ../../Engine/Scene
+    ../../ThirdParty/MojoShader
 )
 
+add_definitions(-DMOJOSHADER_NO_VERSION_INCLUDE)
+
 # Define target & libraries to link
 add_executable (${TARGET_NAME} ${SOURCE_FILES})
-target_link_libraries (${TARGET_NAME} Container Core Graphics IO Math Resource Scene d3dx9.lib)
+target_link_libraries (${TARGET_NAME} Container Core Graphics IO Math Resource Scene MojoShader d3dcompiler.lib)
 finalize_exe ()

+ 33 - 34
Tools/ShaderCompiler/ShaderCompiler.cpp

@@ -37,8 +37,8 @@
 #include <cstring>
 
 #include <windows.h>
-#include <d3d9.h>
-#include <d3dx9shader.h>
+#include <d3dcompiler.h>
+#include <mojoshader.h>
 
 #include "DebugNew.h"
 
@@ -149,10 +149,10 @@ public:
     }
 };
 
-class IncludeHandler : public ID3DXInclude
+class IncludeHandler : public ID3DInclude
 {
 public:
-    STDMETHOD(Open)(D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
+    STDMETHOD(Open)(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
     {
         String fileName = inDir_ + String((const char*)pFileName);
         File file(context_, fileName);
@@ -408,37 +408,36 @@ void CompileShader(const String& fileName)
 void CompileVariation(CompiledVariation* variation)
 {
     IncludeHandler includeHandler;
-    PODVector<D3DXMACRO> macros;
+    PODVector<D3D_SHADER_MACRO> macros;
     
     // Insert variation-specific and global defines
     for (unsigned i = 0; i < variation->defines_.Size(); ++i)
     {
-        D3DXMACRO macro;
+        D3D_SHADER_MACRO macro;
         macro.Name = variation->defines_[i].CString();
         macro.Definition = variation->defineValues_[i].CString();
         macros.Push(macro);
     }
     for (unsigned i = 0; i < defines_.Size(); ++i)
     {
-        D3DXMACRO macro;
+        D3D_SHADER_MACRO macro;
         macro.Name = defines_[i].CString();
         macro.Definition = defineValues_[i].CString();
         macros.Push(macro);
     }
     
-    D3DXMACRO endMacro;
+    D3D_SHADER_MACRO endMacro;
     endMacro.Name = 0;
     endMacro.Definition = 0;
     macros.Push(endMacro);
     
-    LPD3DXBUFFER shaderCode = 0;
-    LPD3DXBUFFER errorMsgs = 0;
-    LPD3DXCONSTANTTABLE constantTable = 0;
+    LPD3DBLOB shaderCode = NULL;
+    LPD3DBLOB errorMsgs = NULL;
     
     // Set the profile, entrypoint and flags according to the shader being compiled
     String profile;
     String entryPoint;
-    unsigned flags = D3DXSHADER_OPTIMIZATION_LEVEL3;
+    unsigned flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
     
     if (variation->type_ == VS)
     {
@@ -456,13 +455,14 @@ void CompileVariation(CompiledVariation* variation)
         else
         {
             profile = "ps_3_0";
-            flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
+            flags |= D3DCOMPILE_PREFER_FLOW_CONTROL;
         }
     }
     
-    // Compile using D3DX
-    HRESULT hr = D3DXCompileShader(hlslCode_.CString(), hlslCode_.Length(), &macros.Front(), &includeHandler, 
-        entryPoint.CString(), profile.CString(), flags, &shaderCode, &errorMsgs, &constantTable);
+    // Compile using D3DCompiler
+    HRESULT hr = D3DCompile(hlslCode_.CString(), hlslCode_.Length(), NULL, &macros.Front(), &includeHandler,
+        entryPoint.CString(), profile.CString(), flags, 0, &shaderCode, &errorMsgs);
+
     if (FAILED(hr))
     {
         variation->errorMsg_ = String((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
@@ -470,6 +470,10 @@ void CompileVariation(CompiledVariation* variation)
     }
     else
     {
+        BYTE const *const bufData = static_cast<BYTE *>(shaderCode->GetBufferPointer());
+        SIZE_T const bufSize = shaderCode->GetBufferSize();
+        MOJOSHADER_parseData const *parseData = MOJOSHADER_parse("bytecode", bufData, bufSize, NULL, 0, NULL, 0, NULL, NULL, NULL);
+
         CopyStrippedCode(variation->byteCode_, shaderCode->GetBufferPointer(), shaderCode->GetBufferSize());
         
         if (!variation->name_.Empty())
@@ -483,23 +487,17 @@ void CompileVariation(CompiledVariation* variation)
             String warning((const char*)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize());
             PrintLine("WARNING: " + warning);
         }
-        
-        // Parse the constant table for constants and texture units
-        D3DXCONSTANTTABLE_DESC desc;
-        constantTable->GetDesc(&desc);
-        for (unsigned i = 0; i < desc.Constants; ++i)
+
+        for(int i = 0; i < parseData->symbol_count; i++)
         {
-            D3DXHANDLE handle = constantTable->GetConstant(NULL, i);
-            D3DXCONSTANT_DESC constantDesc;
-            unsigned numElements = 1;
-            constantTable->GetConstantDesc(handle, &constantDesc, &numElements);
-            
-            String name(constantDesc.Name);
-            unsigned reg = constantDesc.RegisterIndex;
-            unsigned regCount = constantDesc.RegisterCount;
-            
+            MOJOSHADER_symbol const& symbol = parseData->symbols[i];
+
+            String name(symbol.name);
+            unsigned const reg = symbol.register_index;
+            unsigned const regCount = symbol.register_count;
+
             // Check if the parameter is a constant or a texture sampler
-            bool isSampler = (name[0] == 's');
+            bool const isSampler = (name[0] == 's');
             name = name.Substring(1);
             
             if (isSampler)
@@ -517,7 +515,9 @@ void CompileVariation(CompiledVariation* variation)
                 variation->constants_.Push(newParam);
             }
         }
-        
+
+        MOJOSHADER_freeParseData(parseData);
+
         File outFile(context_);
         if (!outFile.Open(variation->outFileName_, FILE_WRITE))
         {
@@ -554,14 +554,13 @@ void CompileVariation(CompiledVariation* variation)
     
     if (shaderCode)
         shaderCode->Release();
-    if (constantTable)
-        constantTable->Release();
     if (errorMsgs)
         errorMsgs->Release();
 }
 
 void CopyStrippedCode(PODVector<unsigned char>& dest, void* src, unsigned srcSize)
 {
+    unsigned const D3DSIO_COMMENT = 0xFFFE;
     unsigned* srcWords = (unsigned*)src;
     unsigned srcWordSize = srcSize >> 2;