Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2008, 2009 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <assert.h>
  27.  
  28. extern "C" {
  29. #include "main/core.h" /* for struct gl_context */
  30. #include "main/context.h"
  31. #include "main/shaderobj.h"
  32. }
  33.  
  34. #include "ralloc.h"
  35. #include "ast.h"
  36. #include "glsl_parser_extras.h"
  37. #include "glsl_parser.h"
  38. #include "ir_optimization.h"
  39. #include "loop_analysis.h"
  40.  
  41. /**
  42.  * Format a short human-readable description of the given GLSL version.
  43.  */
  44. const char *
  45. glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
  46. {
  47.    return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
  48.                           version / 100, version % 100);
  49. }
  50.  
  51.  
  52. static unsigned known_desktop_glsl_versions[] =
  53.    { 110, 120, 130, 140, 150, 330, 400, 410, 420, 430 };
  54.  
  55.  
  56. _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
  57.                                                GLenum target, void *mem_ctx)
  58.  : ctx(_ctx)
  59. {
  60.    switch (target) {
  61.    case GL_VERTEX_SHADER:   this->target = vertex_shader; break;
  62.    case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
  63.    case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
  64.    }
  65.  
  66.    this->scanner = NULL;
  67.    this->translation_unit.make_empty();
  68.    this->symbols = new(mem_ctx) glsl_symbol_table;
  69.    this->info_log = ralloc_strdup(mem_ctx, "");
  70.    this->error = false;
  71.    this->loop_nesting_ast = NULL;
  72.    this->switch_state.switch_nesting_ast = NULL;
  73.  
  74.    this->struct_specifier_depth = 0;
  75.    this->num_builtins_to_link = 0;
  76.  
  77.    /* Set default language version and extensions */
  78.    this->language_version = ctx->Const.ForceGLSLVersion ?
  79.                             ctx->Const.ForceGLSLVersion : 110;
  80.    this->es_shader = false;
  81.    this->ARB_texture_rectangle_enable = true;
  82.  
  83.    /* OpenGL ES 2.0 has different defaults from desktop GL. */
  84.    if (ctx->API == API_OPENGLES2) {
  85.       this->language_version = 100;
  86.       this->es_shader = true;
  87.       this->ARB_texture_rectangle_enable = false;
  88.    }
  89.  
  90.    this->extensions = &ctx->Extensions;
  91.  
  92.    this->Const.MaxLights = ctx->Const.MaxLights;
  93.    this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
  94.    this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
  95.    this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
  96.    this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
  97.    this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
  98.    this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
  99.    this->Const.MaxVertexTextureImageUnits = ctx->Const.VertexProgram.MaxTextureImageUnits;
  100.    this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
  101.    this->Const.MaxTextureImageUnits = ctx->Const.FragmentProgram.MaxTextureImageUnits;
  102.    this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
  103.    this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
  104.    this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
  105.  
  106.    this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
  107.  
  108.    /* Populate the list of supported GLSL versions */
  109.    /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
  110.     * the OpenGL 3.2 Core context is supported, this logic will need
  111.     * change.  Older versions of GLSL are no longer supported
  112.     * outside the compatibility contexts of 3.x.
  113.     */
  114.    this->num_supported_versions = 0;
  115.    if (_mesa_is_desktop_gl(ctx)) {
  116.       for (unsigned i = 0; i < ARRAY_SIZE(known_desktop_glsl_versions); i++) {
  117.          if (known_desktop_glsl_versions[i] <= ctx->Const.GLSLVersion) {
  118.             this->supported_versions[this->num_supported_versions].ver
  119.                = known_desktop_glsl_versions[i];
  120.             this->supported_versions[this->num_supported_versions].es = false;
  121.             this->num_supported_versions++;
  122.          }
  123.       }
  124.    }
  125.    if (ctx->API == API_OPENGLES2 || ctx->Extensions.ARB_ES2_compatibility) {
  126.       this->supported_versions[this->num_supported_versions].ver = 100;
  127.       this->supported_versions[this->num_supported_versions].es = true;
  128.       this->num_supported_versions++;
  129.    }
  130.    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
  131.       this->supported_versions[this->num_supported_versions].ver = 300;
  132.       this->supported_versions[this->num_supported_versions].es = true;
  133.       this->num_supported_versions++;
  134.    }
  135.    assert(this->num_supported_versions
  136.           <= ARRAY_SIZE(this->supported_versions));
  137.  
  138.    /* Create a string for use in error messages to tell the user which GLSL
  139.     * versions are supported.
  140.     */
  141.    char *supported = ralloc_strdup(this, "");
  142.    for (unsigned i = 0; i < this->num_supported_versions; i++) {
  143.       unsigned ver = this->supported_versions[i].ver;
  144.       const char *const prefix = (i == 0)
  145.          ? ""
  146.          : ((i == this->num_supported_versions - 1) ? ", and " : ", ");
  147.       const char *const suffix = (this->supported_versions[i].es) ? " ES" : "";
  148.  
  149.       ralloc_asprintf_append(& supported, "%s%u.%02u%s",
  150.                              prefix,
  151.                              ver / 100, ver % 100,
  152.                              suffix);
  153.    }
  154.  
  155.    this->supported_version_string = supported;
  156.  
  157.    if (ctx->Const.ForceGLSLExtensionsWarn)
  158.       _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
  159.  
  160.    this->default_uniform_qualifier = new(this) ast_type_qualifier();
  161.    this->default_uniform_qualifier->flags.q.shared = 1;
  162.    this->default_uniform_qualifier->flags.q.column_major = 1;
  163. }
  164.  
  165. /**
  166.  * Determine whether the current GLSL version is sufficiently high to support
  167.  * a certain feature, and generate an error message if it isn't.
  168.  *
  169.  * \param required_glsl_version and \c required_glsl_es_version are
  170.  * interpreted as they are in _mesa_glsl_parse_state::is_version().
  171.  *
  172.  * \param locp is the parser location where the error should be reported.
  173.  *
  174.  * \param fmt (and additional arguments) constitute a printf-style error
  175.  * message to report if the version check fails.  Information about the
  176.  * current and required GLSL versions will be appended.  So, for example, if
  177.  * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
  178.  * "foo unsupported") is called, the error message will be "foo unsupported in
  179.  * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
  180.  */
  181. bool
  182. _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
  183.                                       unsigned required_glsl_es_version,
  184.                                       YYLTYPE *locp, const char *fmt, ...)
  185. {
  186.    if (this->is_version(required_glsl_version, required_glsl_es_version))
  187.       return true;
  188.  
  189.    va_list args;
  190.    va_start(args, fmt);
  191.    char *problem = ralloc_vasprintf(this, fmt, args);
  192.    va_end(args);
  193.    const char *glsl_version_string
  194.       = glsl_compute_version_string(this, false, required_glsl_version);
  195.    const char *glsl_es_version_string
  196.       = glsl_compute_version_string(this, true, required_glsl_es_version);
  197.    const char *requirement_string = "";
  198.    if (required_glsl_version && required_glsl_es_version) {
  199.       requirement_string = ralloc_asprintf(this, " (%s or %s required)",
  200.                                            glsl_version_string,
  201.                                            glsl_es_version_string);
  202.    } else if (required_glsl_version) {
  203.       requirement_string = ralloc_asprintf(this, " (%s required)",
  204.                                            glsl_version_string);
  205.    } else if (required_glsl_es_version) {
  206.       requirement_string = ralloc_asprintf(this, " (%s required)",
  207.                                            glsl_es_version_string);
  208.    }
  209.    _mesa_glsl_error(locp, this, "%s in %s%s.",
  210.                     problem, this->get_version_string(),
  211.                     requirement_string);
  212.  
  213.    return false;
  214. }
  215.  
  216. /**
  217.  * Process a GLSL #version directive.
  218.  *
  219.  * \param version is the integer that follows the #version token.
  220.  *
  221.  * \param ident is a string identifier that follows the integer, if any is
  222.  * present.  Otherwise NULL.
  223.  */
  224. void
  225. _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
  226.                                                   const char *ident)
  227. {
  228.    bool es_token_present = false;
  229.    if (ident) {
  230.       if (strcmp(ident, "es") == 0) {
  231.          es_token_present = true;
  232.       } else if (version >= 150) {
  233.          if (strcmp(ident, "core") == 0) {
  234.             /* Accept the token.  There's no need to record that this is
  235.              * a core profile shader since that's the only profile we support.
  236.              */
  237.          } else if (strcmp(ident, "compatibility") == 0) {
  238.             _mesa_glsl_error(locp, this,
  239.                              "The compatibility profile is not supported.\n");
  240.          } else {
  241.             _mesa_glsl_error(locp, this,
  242.                              "\"%s\" is not a valid shading language profile; "
  243.                              "if present, it must be \"core\".\n", ident);
  244.          }
  245.       } else {
  246.          _mesa_glsl_error(locp, this,
  247.                           "Illegal text following version number\n");
  248.       }
  249.    }
  250.  
  251.    this->es_shader = es_token_present;
  252.    if (version == 100) {
  253.       if (es_token_present) {
  254.          _mesa_glsl_error(locp, this,
  255.                           "GLSL 1.00 ES should be selected using "
  256.                           "`#version 100'\n");
  257.       } else {
  258.          this->es_shader = true;
  259.       }
  260.    }
  261.  
  262.    this->language_version = version;
  263.  
  264.    bool supported = false;
  265.    for (unsigned i = 0; i < this->num_supported_versions; i++) {
  266.       if (this->supported_versions[i].ver == (unsigned) version
  267.           && this->supported_versions[i].es == this->es_shader) {
  268.          supported = true;
  269.          break;
  270.       }
  271.    }
  272.  
  273.    if (!supported) {
  274.       _mesa_glsl_error(locp, this, "%s is not supported. "
  275.                        "Supported versions are: %s\n",
  276.                        this->get_version_string(),
  277.                        this->supported_version_string);
  278.  
  279.       /* On exit, the language_version must be set to a valid value.
  280.        * Later calls to _mesa_glsl_initialize_types will misbehave if
  281.        * the version is invalid.
  282.        */
  283.       switch (this->ctx->API) {
  284.       case API_OPENGL_COMPAT:
  285.       case API_OPENGL_CORE:
  286.          this->language_version = this->ctx->Const.GLSLVersion;
  287.          break;
  288.  
  289.       case API_OPENGLES:
  290.          assert(!"Should not get here.");
  291.          /* FALLTHROUGH */
  292.  
  293.       case API_OPENGLES2:
  294.          this->language_version = 100;
  295.          break;
  296.       }
  297.    }
  298.  
  299.    if (this->language_version >= 140) {
  300.       this->ARB_uniform_buffer_object_enable = true;
  301.    }
  302.  
  303.    if (this->language_version == 300 && this->es_shader) {
  304.       this->ARB_explicit_attrib_location_enable = true;
  305.    }
  306. }
  307.  
  308. extern "C" {
  309.  
  310. /**
  311.  * The most common use of _mesa_glsl_shader_target_name(), which is
  312.  * shared with C code in Mesa core to translate a GLenum to a short
  313.  * shader stage name in debug printouts.
  314.  *
  315.  * It recognizes the PROGRAM variants of the names so it can be used
  316.  * with a struct gl_program->Target, not just a struct
  317.  * gl_shader->Type.
  318.  */
  319. const char *
  320. _mesa_glsl_shader_target_name(GLenum type)
  321. {
  322.    switch (type) {
  323.    case GL_VERTEX_SHADER:
  324.    case GL_VERTEX_PROGRAM_ARB:
  325.       return "vertex";
  326.    case GL_FRAGMENT_SHADER:
  327.    case GL_FRAGMENT_PROGRAM_ARB:
  328.       return "fragment";
  329.    case GL_GEOMETRY_SHADER:
  330.       return "geometry";
  331.    default:
  332.       assert(!"Should not get here.");
  333.       return "unknown";
  334.    }
  335. }
  336.  
  337. } /* extern "C" */
  338.  
  339. /**
  340.  * Overloaded C++ variant usable within the compiler for translating
  341.  * our internal enum into short stage names.
  342.  */
  343. const char *
  344. _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
  345. {
  346.    switch (target) {
  347.    case vertex_shader:   return "vertex";
  348.    case fragment_shader: return "fragment";
  349.    case geometry_shader: return "geometry";
  350.    }
  351.  
  352.    assert(!"Should not get here.");
  353.    return "unknown";
  354. }
  355.  
  356. /* This helper function will append the given message to the shader's
  357.    info log and report it via GL_ARB_debug_output. Per that extension,
  358.    'type' is one of the enum values classifying the message, and
  359.    'id' is the implementation-defined ID of the given message. */
  360. static void
  361. _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
  362.                GLenum type, const char *fmt, va_list ap)
  363. {
  364.    bool error = (type == MESA_DEBUG_TYPE_ERROR);
  365.    GLuint msg_id = 0;
  366.  
  367.    assert(state->info_log != NULL);
  368.  
  369.    /* Get the offset that the new message will be written to. */
  370.    int msg_offset = strlen(state->info_log);
  371.  
  372.    ralloc_asprintf_append(&state->info_log, "%u:%u(%u): %s: ",
  373.                                             locp->source,
  374.                                             locp->first_line,
  375.                                             locp->first_column,
  376.                                             error ? "error" : "warning");
  377.    ralloc_vasprintf_append(&state->info_log, fmt, ap);
  378.  
  379.    const char *const msg = &state->info_log[msg_offset];
  380.    struct gl_context *ctx = state->ctx;
  381.  
  382.    /* Report the error via GL_ARB_debug_output. */
  383.    _mesa_shader_debug(ctx, type, &msg_id, msg, strlen(msg));
  384.  
  385.    ralloc_strcat(&state->info_log, "\n");
  386. }
  387.  
  388. void
  389. _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
  390.                  const char *fmt, ...)
  391. {
  392.    va_list ap;
  393.  
  394.    state->error = true;
  395.  
  396.    va_start(ap, fmt);
  397.    _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_ERROR, fmt, ap);
  398.    va_end(ap);
  399. }
  400.  
  401.  
  402. void
  403. _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
  404.                    const char *fmt, ...)
  405. {
  406.    va_list ap;
  407.  
  408.    va_start(ap, fmt);
  409.    _mesa_glsl_msg(locp, state, MESA_DEBUG_TYPE_OTHER, fmt, ap);
  410.    va_end(ap);
  411. }
  412.  
  413.  
  414. /**
  415.  * Enum representing the possible behaviors that can be specified in
  416.  * an #extension directive.
  417.  */
  418. enum ext_behavior {
  419.    extension_disable,
  420.    extension_enable,
  421.    extension_require,
  422.    extension_warn
  423. };
  424.  
  425. /**
  426.  * Element type for _mesa_glsl_supported_extensions
  427.  */
  428. struct _mesa_glsl_extension {
  429.    /**
  430.     * Name of the extension when referred to in a GLSL extension
  431.     * statement
  432.     */
  433.    const char *name;
  434.  
  435.    /** True if this extension is available to vertex shaders */
  436.    bool avail_in_VS;
  437.  
  438.    /** True if this extension is available to geometry shaders */
  439.    bool avail_in_GS;
  440.  
  441.    /** True if this extension is available to fragment shaders */
  442.    bool avail_in_FS;
  443.  
  444.    /** True if this extension is available to desktop GL shaders */
  445.    bool avail_in_GL;
  446.  
  447.    /** True if this extension is available to GLES shaders */
  448.    bool avail_in_ES;
  449.  
  450.    /**
  451.     * Flag in the gl_extensions struct indicating whether this
  452.     * extension is supported by the driver, or
  453.     * &gl_extensions::dummy_true if supported by all drivers.
  454.     *
  455.     * Note: the type (GLboolean gl_extensions::*) is a "pointer to
  456.     * member" type, the type-safe alternative to the "offsetof" macro.
  457.     * In a nutshell:
  458.     *
  459.     * - foo bar::* p declares p to be an "offset" to a field of type
  460.     *   foo that exists within struct bar
  461.     * - &bar::baz computes the "offset" of field baz within struct bar
  462.     * - x.*p accesses the field of x that exists at "offset" p
  463.     * - x->*p is equivalent to (*x).*p
  464.     */
  465.    const GLboolean gl_extensions::* supported_flag;
  466.  
  467.    /**
  468.     * Flag in the _mesa_glsl_parse_state struct that should be set
  469.     * when this extension is enabled.
  470.     *
  471.     * See note in _mesa_glsl_extension::supported_flag about "pointer
  472.     * to member" types.
  473.     */
  474.    bool _mesa_glsl_parse_state::* enable_flag;
  475.  
  476.    /**
  477.     * Flag in the _mesa_glsl_parse_state struct that should be set
  478.     * when the shader requests "warn" behavior for this extension.
  479.     *
  480.     * See note in _mesa_glsl_extension::supported_flag about "pointer
  481.     * to member" types.
  482.     */
  483.    bool _mesa_glsl_parse_state::* warn_flag;
  484.  
  485.  
  486.    bool compatible_with_state(const _mesa_glsl_parse_state *state) const;
  487.    void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
  488. };
  489.  
  490. #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG)                   \
  491.    { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG,   \
  492.          &_mesa_glsl_parse_state::NAME##_enable,                        \
  493.          &_mesa_glsl_parse_state::NAME##_warn }
  494.  
  495. /**
  496.  * Table of extensions that can be enabled/disabled within a shader,
  497.  * and the conditions under which they are supported.
  498.  */
  499. static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
  500.    /*                                  target availability  API availability */
  501.    /* name                             VS     GS     FS     GL     ES         supported flag */
  502.    EXT(ARB_conservative_depth,         false, false, true,  true,  false,     ARB_conservative_depth),
  503.    EXT(ARB_draw_buffers,               false, false, true,  true,  false,     dummy_true),
  504.    EXT(ARB_draw_instanced,             true,  false, false, true,  false,     ARB_draw_instanced),
  505.    EXT(ARB_explicit_attrib_location,   true,  false, true,  true,  false,     ARB_explicit_attrib_location),
  506.    EXT(ARB_fragment_coord_conventions, true,  false, true,  true,  false,     ARB_fragment_coord_conventions),
  507.    EXT(ARB_texture_rectangle,          true,  false, true,  true,  false,     dummy_true),
  508.    EXT(EXT_texture_array,              true,  false, true,  true,  false,     EXT_texture_array),
  509.    EXT(ARB_shader_texture_lod,         true,  false, true,  true,  false,     ARB_shader_texture_lod),
  510.    EXT(ARB_shader_stencil_export,      false, false, true,  true,  false,     ARB_shader_stencil_export),
  511.    EXT(AMD_conservative_depth,         false, false, true,  true,  false,     ARB_conservative_depth),
  512.    EXT(AMD_shader_stencil_export,      false, false, true,  true,  false,     ARB_shader_stencil_export),
  513.    EXT(OES_texture_3D,                 true,  false, true,  false, true,      EXT_texture3D),
  514.    EXT(OES_EGL_image_external,         true,  false, true,  false, true,      OES_EGL_image_external),
  515.    EXT(ARB_shader_bit_encoding,        true,  true,  true,  true,  false,     ARB_shader_bit_encoding),
  516.    EXT(ARB_uniform_buffer_object,      true,  false, true,  true,  false,     ARB_uniform_buffer_object),
  517.    EXT(OES_standard_derivatives,       false, false, true,  false,  true,     OES_standard_derivatives),
  518.    EXT(ARB_texture_cube_map_array,     true,  false, true,  true,  false,     ARB_texture_cube_map_array),
  519.    EXT(ARB_shading_language_packing,   true,  false, true,  true,  false,     ARB_shading_language_packing),
  520.    EXT(ARB_shading_language_420pack,   true,  true,  true,  true,  false,     ARB_shading_language_420pack),
  521.    EXT(ARB_texture_multisample,        true,  false, true,  true,  false,     ARB_texture_multisample),
  522.    EXT(ARB_texture_query_lod,          false, false, true,  true,  false,     ARB_texture_query_lod),
  523.    EXT(ARB_gpu_shader5,                true,  true,  true,  true,  false,     ARB_gpu_shader5),
  524.    EXT(AMD_vertex_shader_layer,        true,  false, false, true,  false,     AMD_vertex_shader_layer),
  525. };
  526.  
  527. #undef EXT
  528.  
  529.  
  530. /**
  531.  * Determine whether a given extension is compatible with the target,
  532.  * API, and extension information in the current parser state.
  533.  */
  534. bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state *
  535.                                                  state) const
  536. {
  537.    /* Check that this extension matches the type of shader we are
  538.     * compiling to.
  539.     */
  540.    switch (state->target) {
  541.    case vertex_shader:
  542.       if (!this->avail_in_VS) {
  543.          return false;
  544.       }
  545.       break;
  546.    case geometry_shader:
  547.       if (!this->avail_in_GS) {
  548.          return false;
  549.       }
  550.       break;
  551.    case fragment_shader:
  552.       if (!this->avail_in_FS) {
  553.          return false;
  554.       }
  555.       break;
  556.    default:
  557.       assert (!"Unrecognized shader target");
  558.       return false;
  559.    }
  560.  
  561.    /* Check that this extension matches whether we are compiling
  562.     * for desktop GL or GLES.
  563.     */
  564.    if (state->es_shader) {
  565.       if (!this->avail_in_ES) return false;
  566.    } else {
  567.       if (!this->avail_in_GL) return false;
  568.    }
  569.  
  570.    /* Check that this extension is supported by the OpenGL
  571.     * implementation.
  572.     *
  573.     * Note: the ->* operator indexes into state->extensions by the
  574.     * offset this->supported_flag.  See
  575.     * _mesa_glsl_extension::supported_flag for more info.
  576.     */
  577.    return state->extensions->*(this->supported_flag);
  578. }
  579.  
  580. /**
  581.  * Set the appropriate flags in the parser state to establish the
  582.  * given behavior for this extension.
  583.  */
  584. void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
  585.                                      ext_behavior behavior) const
  586. {
  587.    /* Note: the ->* operator indexes into state by the
  588.     * offsets this->enable_flag and this->warn_flag.  See
  589.     * _mesa_glsl_extension::supported_flag for more info.
  590.     */
  591.    state->*(this->enable_flag) = (behavior != extension_disable);
  592.    state->*(this->warn_flag)   = (behavior == extension_warn);
  593. }
  594.  
  595. /**
  596.  * Find an extension by name in _mesa_glsl_supported_extensions.  If
  597.  * the name is not found, return NULL.
  598.  */
  599. static const _mesa_glsl_extension *find_extension(const char *name)
  600. {
  601.    for (unsigned i = 0; i < Elements(_mesa_glsl_supported_extensions); ++i) {
  602.       if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
  603.          return &_mesa_glsl_supported_extensions[i];
  604.       }
  605.    }
  606.    return NULL;
  607. }
  608.  
  609.  
  610. bool
  611. _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
  612.                              const char *behavior_string, YYLTYPE *behavior_locp,
  613.                              _mesa_glsl_parse_state *state)
  614. {
  615.    ext_behavior behavior;
  616.    if (strcmp(behavior_string, "warn") == 0) {
  617.       behavior = extension_warn;
  618.    } else if (strcmp(behavior_string, "require") == 0) {
  619.       behavior = extension_require;
  620.    } else if (strcmp(behavior_string, "enable") == 0) {
  621.       behavior = extension_enable;
  622.    } else if (strcmp(behavior_string, "disable") == 0) {
  623.       behavior = extension_disable;
  624.    } else {
  625.       _mesa_glsl_error(behavior_locp, state,
  626.                        "Unknown extension behavior `%s'",
  627.                        behavior_string);
  628.       return false;
  629.    }
  630.  
  631.    if (strcmp(name, "all") == 0) {
  632.       if ((behavior == extension_enable) || (behavior == extension_require)) {
  633.          _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
  634.                           (behavior == extension_enable)
  635.                           ? "enable" : "require");
  636.          return false;
  637.       } else {
  638.          for (unsigned i = 0;
  639.               i < Elements(_mesa_glsl_supported_extensions); ++i) {
  640.             const _mesa_glsl_extension *extension
  641.                = &_mesa_glsl_supported_extensions[i];
  642.             if (extension->compatible_with_state(state)) {
  643.                _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
  644.             }
  645.          }
  646.       }
  647.    } else {
  648.       const _mesa_glsl_extension *extension = find_extension(name);
  649.       if (extension && extension->compatible_with_state(state)) {
  650.          extension->set_flags(state, behavior);
  651.       } else {
  652.          static const char *const fmt = "extension `%s' unsupported in %s shader";
  653.  
  654.          if (behavior == extension_require) {
  655.             _mesa_glsl_error(name_locp, state, fmt,
  656.                              name, _mesa_glsl_shader_target_name(state->target));
  657.             return false;
  658.          } else {
  659.             _mesa_glsl_warning(name_locp, state, fmt,
  660.                                name, _mesa_glsl_shader_target_name(state->target));
  661.          }
  662.       }
  663.    }
  664.  
  665.    return true;
  666. }
  667.  
  668.  
  669. /**
  670.  * Returns the name of the type of a column of a matrix. E.g.,
  671.  *
  672.  *    "mat3"   -> "vec3"
  673.  *    "mat4x2" -> "vec2"
  674.  */
  675. static const char *
  676. _mesa_ast_get_matrix_column_type_name(const char *matrix_type_name)
  677. {
  678.    static const char *vec_name[] = { "vec2", "vec3", "vec4" };
  679.  
  680.    /* The number of elements in a row of a matrix is specified by the last
  681.     * character of the matrix type name.
  682.     */
  683.    long rows = strtol(matrix_type_name + strlen(matrix_type_name) - 1,
  684.                       NULL, 10);
  685.    return vec_name[rows - 2];
  686. }
  687.  
  688. /**
  689.  * Recurses through <type> and <expr> if <expr> is an aggregate initializer
  690.  * and sets <expr>'s <constructor_type> field to <type>. Gives later functions
  691.  * (process_array_constructor, et al) sufficient information to do type
  692.  * checking.
  693.  *
  694.  * Operates on assignments involving an aggregate initializer. E.g.,
  695.  *
  696.  * vec4 pos = {1.0, -1.0, 0.0, 1.0};
  697.  *
  698.  * or more ridiculously,
  699.  *
  700.  * struct S {
  701.  *     vec4 v[2];
  702.  * };
  703.  *
  704.  * struct {
  705.  *     S a[2], b;
  706.  *     int c;
  707.  * } aggregate = {
  708.  *     {
  709.  *         {
  710.  *             {
  711.  *                 {1.0, 2.0, 3.0, 4.0}, // a[0].v[0]
  712.  *                 {5.0, 6.0, 7.0, 8.0}  // a[0].v[1]
  713.  *             } // a[0].v
  714.  *         }, // a[0]
  715.  *         {
  716.  *             {
  717.  *                 {1.0, 2.0, 3.0, 4.0}, // a[1].v[0]
  718.  *                 {5.0, 6.0, 7.0, 8.0}  // a[1].v[1]
  719.  *             } // a[1].v
  720.  *         } // a[1]
  721.  *     }, // a
  722.  *     {
  723.  *         {
  724.  *             {1.0, 2.0, 3.0, 4.0}, // b.v[0]
  725.  *             {5.0, 6.0, 7.0, 8.0}  // b.v[1]
  726.  *         } // b.v
  727.  *     }, // b
  728.  *     4 // c
  729.  * };
  730.  *
  731.  * This pass is necessary because the right-hand side of <type> e = { ... }
  732.  * doesn't contain sufficient information to determine if the types match.
  733.  */
  734. void
  735. _mesa_ast_set_aggregate_type(const ast_type_specifier *type,
  736.                              ast_expression *expr,
  737.                              _mesa_glsl_parse_state *state)
  738. {
  739.    void *ctx = state;
  740.    ast_aggregate_initializer *ai = (ast_aggregate_initializer *)expr;
  741.    ai->constructor_type = (ast_type_specifier *)type;
  742.  
  743.    bool is_declaration = ai->constructor_type->structure != NULL;
  744.    if (!is_declaration) {
  745.       /* Look up <type> name in the symbol table to see if it's a struct. */
  746.       const ast_type_specifier *struct_type =
  747.          state->symbols->get_type_ast(type->type_name);
  748.       ai->constructor_type->structure =
  749.          struct_type ? new(ctx) ast_struct_specifier(*struct_type->structure)
  750.                      : NULL;
  751.    }
  752.  
  753.    /* If the aggregate is an array, recursively set its elements' types. */
  754.    if (type->is_array) {
  755.       /* We want to set the element type which is not an array itself, so make
  756.        * a copy of the array type and set its is_array field to false.
  757.        *
  758.        * E.g., if <type> if struct S[2] we want to set each element's type to
  759.        * struct S.
  760.        *
  761.        * FINISHME: Update when ARB_array_of_arrays is supported.
  762.        */
  763.       const ast_type_specifier *non_array_type =
  764.          new(ctx) ast_type_specifier(type, false, NULL);
  765.  
  766.       for (exec_node *expr_node = ai->expressions.head;
  767.            !expr_node->is_tail_sentinel();
  768.            expr_node = expr_node->next) {
  769.          ast_expression *expr = exec_node_data(ast_expression, expr_node,
  770.                                                link);
  771.  
  772.          if (expr->oper == ast_aggregate)
  773.             _mesa_ast_set_aggregate_type(non_array_type, expr, state);
  774.       }
  775.  
  776.    /* If the aggregate is a struct, recursively set its fields' types. */
  777.    } else if (ai->constructor_type->structure) {
  778.       ai->constructor_type->structure->is_declaration = is_declaration;
  779.       exec_node *expr_node = ai->expressions.head;
  780.  
  781.       /* Iterate through the struct's fields' declarations. E.g., iterate from
  782.        * "float a, b" to "int c" in the struct below.
  783.        *
  784.        *     struct {
  785.        *         float a, b;
  786.        *         int c;
  787.        *     } s;
  788.        */
  789.       for (exec_node *decl_list_node =
  790.               ai->constructor_type->structure->declarations.head;
  791.            !decl_list_node->is_tail_sentinel();
  792.            decl_list_node = decl_list_node->next) {
  793.          ast_declarator_list *decl_list = exec_node_data(ast_declarator_list,
  794.                                                          decl_list_node, link);
  795.  
  796.          for (exec_node *decl_node = decl_list->declarations.head;
  797.               !decl_node->is_tail_sentinel() && !expr_node->is_tail_sentinel();
  798.               decl_node = decl_node->next, expr_node = expr_node->next) {
  799.             ast_declaration *decl = exec_node_data(ast_declaration, decl_node,
  800.                                                    link);
  801.             ast_expression *expr = exec_node_data(ast_expression, expr_node,
  802.                                                   link);
  803.  
  804.             bool is_array = decl_list->type->specifier->is_array;
  805.             ast_expression *array_size = decl_list->type->specifier->array_size;
  806.  
  807.             /* Recognize variable declarations with the bracketed size attached
  808.              * to the type rather than the variable name as arrays. E.g.,
  809.              *
  810.              *     float a[2];
  811.              *     float[2] b;
  812.              *
  813.              * are both arrays, but <a>'s array_size is decl->array_size, while
  814.              * <b>'s array_size is decl_list->type->specifier->array_size.
  815.              */
  816.             if (!is_array) {
  817.                /* FINISHME: Update when ARB_array_of_arrays is supported. */
  818.                is_array = decl->is_array;
  819.                array_size = decl->array_size;
  820.             }
  821.  
  822.             /* Declaration shadows the <type> parameter. */
  823.             ast_type_specifier *type =
  824.                new(ctx) ast_type_specifier(decl_list->type->specifier,
  825.                                            is_array, array_size);
  826.  
  827.             if (expr->oper == ast_aggregate)
  828.                _mesa_ast_set_aggregate_type(type, expr, state);
  829.          }
  830.       }
  831.    } else {
  832.       /* If the aggregate is a matrix, set its columns' types. */
  833.       const char *name;
  834.       const glsl_type *const constructor_type =
  835.          ai->constructor_type->glsl_type(&name, state);
  836.  
  837.       if (constructor_type->is_matrix()) {
  838.          for (exec_node *expr_node = ai->expressions.head;
  839.               !expr_node->is_tail_sentinel();
  840.               expr_node = expr_node->next) {
  841.             ast_expression *expr = exec_node_data(ast_expression, expr_node,
  842.                                                   link);
  843.  
  844.             /* Declaration shadows the <type> parameter. */
  845.             ast_type_specifier *type = new(ctx)
  846.                ast_type_specifier(_mesa_ast_get_matrix_column_type_name(name));
  847.  
  848.             if (expr->oper == ast_aggregate)
  849.                _mesa_ast_set_aggregate_type(type, expr, state);
  850.          }
  851.       }
  852.    }
  853. }
  854.  
  855.  
  856. void
  857. _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
  858. {
  859.    if (q->flags.q.constant)
  860.       printf("const ");
  861.  
  862.    if (q->flags.q.invariant)
  863.       printf("invariant ");
  864.  
  865.    if (q->flags.q.attribute)
  866.       printf("attribute ");
  867.  
  868.    if (q->flags.q.varying)
  869.       printf("varying ");
  870.  
  871.    if (q->flags.q.in && q->flags.q.out)
  872.       printf("inout ");
  873.    else {
  874.       if (q->flags.q.in)
  875.          printf("in ");
  876.  
  877.       if (q->flags.q.out)
  878.          printf("out ");
  879.    }
  880.  
  881.    if (q->flags.q.centroid)
  882.       printf("centroid ");
  883.    if (q->flags.q.uniform)
  884.       printf("uniform ");
  885.    if (q->flags.q.smooth)
  886.       printf("smooth ");
  887.    if (q->flags.q.flat)
  888.       printf("flat ");
  889.    if (q->flags.q.noperspective)
  890.       printf("noperspective ");
  891. }
  892.  
  893.  
  894. void
  895. ast_node::print(void) const
  896. {
  897.    printf("unhandled node ");
  898. }
  899.  
  900.  
  901. ast_node::ast_node(void)
  902. {
  903.    this->location.source = 0;
  904.    this->location.line = 0;
  905.    this->location.column = 0;
  906. }
  907.  
  908.  
  909. static void
  910. ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
  911. {
  912.    if (is_array) {
  913.       printf("[ ");
  914.  
  915.       if (array_size)
  916.          array_size->print();
  917.  
  918.       printf("] ");
  919.    }
  920. }
  921.  
  922.  
  923. void
  924. ast_compound_statement::print(void) const
  925. {
  926.    printf("{\n");
  927.    
  928.    foreach_list_const(n, &this->statements) {
  929.       ast_node *ast = exec_node_data(ast_node, n, link);
  930.       ast->print();
  931.    }
  932.  
  933.    printf("}\n");
  934. }
  935.  
  936.  
  937. ast_compound_statement::ast_compound_statement(int new_scope,
  938.                                                ast_node *statements)
  939. {
  940.    this->new_scope = new_scope;
  941.  
  942.    if (statements != NULL) {
  943.       this->statements.push_degenerate_list_at_head(&statements->link);
  944.    }
  945. }
  946.  
  947.  
  948. void
  949. ast_expression::print(void) const
  950. {
  951.    switch (oper) {
  952.    case ast_assign:
  953.    case ast_mul_assign:
  954.    case ast_div_assign:
  955.    case ast_mod_assign:
  956.    case ast_add_assign:
  957.    case ast_sub_assign:
  958.    case ast_ls_assign:
  959.    case ast_rs_assign:
  960.    case ast_and_assign:
  961.    case ast_xor_assign:
  962.    case ast_or_assign:
  963.       subexpressions[0]->print();
  964.       printf("%s ", operator_string(oper));
  965.       subexpressions[1]->print();
  966.       break;
  967.  
  968.    case ast_field_selection:
  969.       subexpressions[0]->print();
  970.       printf(". %s ", primary_expression.identifier);
  971.       break;
  972.  
  973.    case ast_plus:
  974.    case ast_neg:
  975.    case ast_bit_not:
  976.    case ast_logic_not:
  977.    case ast_pre_inc:
  978.    case ast_pre_dec:
  979.       printf("%s ", operator_string(oper));
  980.       subexpressions[0]->print();
  981.       break;
  982.  
  983.    case ast_post_inc:
  984.    case ast_post_dec:
  985.       subexpressions[0]->print();
  986.       printf("%s ", operator_string(oper));
  987.       break;
  988.  
  989.    case ast_conditional:
  990.       subexpressions[0]->print();
  991.       printf("? ");
  992.       subexpressions[1]->print();
  993.       printf(": ");
  994.       subexpressions[2]->print();
  995.       break;
  996.  
  997.    case ast_array_index:
  998.       subexpressions[0]->print();
  999.       printf("[ ");
  1000.       subexpressions[1]->print();
  1001.       printf("] ");
  1002.       break;
  1003.  
  1004.    case ast_function_call: {
  1005.       subexpressions[0]->print();
  1006.       printf("( ");
  1007.  
  1008.       foreach_list_const (n, &this->expressions) {
  1009.          if (n != this->expressions.get_head())
  1010.             printf(", ");
  1011.  
  1012.          ast_node *ast = exec_node_data(ast_node, n, link);
  1013.          ast->print();
  1014.       }
  1015.  
  1016.       printf(") ");
  1017.       break;
  1018.    }
  1019.  
  1020.    case ast_identifier:
  1021.       printf("%s ", primary_expression.identifier);
  1022.       break;
  1023.  
  1024.    case ast_int_constant:
  1025.       printf("%d ", primary_expression.int_constant);
  1026.       break;
  1027.  
  1028.    case ast_uint_constant:
  1029.       printf("%u ", primary_expression.uint_constant);
  1030.       break;
  1031.  
  1032.    case ast_float_constant:
  1033.       printf("%f ", primary_expression.float_constant);
  1034.       break;
  1035.  
  1036.    case ast_bool_constant:
  1037.       printf("%s ",
  1038.              primary_expression.bool_constant
  1039.              ? "true" : "false");
  1040.       break;
  1041.  
  1042.    case ast_sequence: {
  1043.       printf("( ");
  1044.       foreach_list_const(n, & this->expressions) {
  1045.          if (n != this->expressions.get_head())
  1046.             printf(", ");
  1047.  
  1048.          ast_node *ast = exec_node_data(ast_node, n, link);
  1049.          ast->print();
  1050.       }
  1051.       printf(") ");
  1052.       break;
  1053.    }
  1054.  
  1055.    case ast_aggregate: {
  1056.       printf("{ ");
  1057.       foreach_list_const(n, & this->expressions) {
  1058.          if (n != this->expressions.get_head())
  1059.             printf(", ");
  1060.  
  1061.          ast_node *ast = exec_node_data(ast_node, n, link);
  1062.          ast->print();
  1063.       }
  1064.       printf("} ");
  1065.       break;
  1066.    }
  1067.  
  1068.    default:
  1069.       assert(0);
  1070.       break;
  1071.    }
  1072. }
  1073.  
  1074. ast_expression::ast_expression(int oper,
  1075.                                ast_expression *ex0,
  1076.                                ast_expression *ex1,
  1077.                                ast_expression *ex2)
  1078. {
  1079.    this->oper = ast_operators(oper);
  1080.    this->subexpressions[0] = ex0;
  1081.    this->subexpressions[1] = ex1;
  1082.    this->subexpressions[2] = ex2;
  1083.    this->non_lvalue_description = NULL;
  1084. }
  1085.  
  1086.  
  1087. void
  1088. ast_expression_statement::print(void) const
  1089. {
  1090.    if (expression)
  1091.       expression->print();
  1092.  
  1093.    printf("; ");
  1094. }
  1095.  
  1096.  
  1097. ast_expression_statement::ast_expression_statement(ast_expression *ex) :
  1098.    expression(ex)
  1099. {
  1100.    /* empty */
  1101. }
  1102.  
  1103.  
  1104. void
  1105. ast_function::print(void) const
  1106. {
  1107.    return_type->print();
  1108.    printf(" %s (", identifier);
  1109.  
  1110.    foreach_list_const(n, & this->parameters) {
  1111.       ast_node *ast = exec_node_data(ast_node, n, link);
  1112.       ast->print();
  1113.    }
  1114.  
  1115.    printf(")");
  1116. }
  1117.  
  1118.  
  1119. ast_function::ast_function(void)
  1120.    : is_definition(false), signature(NULL)
  1121. {
  1122.    /* empty */
  1123. }
  1124.  
  1125.  
  1126. void
  1127. ast_fully_specified_type::print(void) const
  1128. {
  1129.    _mesa_ast_type_qualifier_print(& qualifier);
  1130.    specifier->print();
  1131. }
  1132.  
  1133.  
  1134. void
  1135. ast_parameter_declarator::print(void) const
  1136. {
  1137.    type->print();
  1138.    if (identifier)
  1139.       printf("%s ", identifier);
  1140.    ast_opt_array_size_print(is_array, array_size);
  1141. }
  1142.  
  1143.  
  1144. void
  1145. ast_function_definition::print(void) const
  1146. {
  1147.    prototype->print();
  1148.    body->print();
  1149. }
  1150.  
  1151.  
  1152. void
  1153. ast_declaration::print(void) const
  1154. {
  1155.    printf("%s ", identifier);
  1156.    ast_opt_array_size_print(is_array, array_size);
  1157.  
  1158.    if (initializer) {
  1159.       printf("= ");
  1160.       initializer->print();
  1161.    }
  1162. }
  1163.  
  1164.  
  1165. ast_declaration::ast_declaration(const char *identifier, bool is_array,
  1166.                                  ast_expression *array_size,
  1167.                                  ast_expression *initializer)
  1168. {
  1169.    this->identifier = identifier;
  1170.    this->is_array = is_array;
  1171.    this->array_size = array_size;
  1172.    this->initializer = initializer;
  1173. }
  1174.  
  1175.  
  1176. void
  1177. ast_declarator_list::print(void) const
  1178. {
  1179.    assert(type || invariant);
  1180.  
  1181.    if (type)
  1182.       type->print();
  1183.    else
  1184.       printf("invariant ");
  1185.  
  1186.    foreach_list_const (ptr, & this->declarations) {
  1187.       if (ptr != this->declarations.get_head())
  1188.          printf(", ");
  1189.  
  1190.       ast_node *ast = exec_node_data(ast_node, ptr, link);
  1191.       ast->print();
  1192.    }
  1193.  
  1194.    printf("; ");
  1195. }
  1196.  
  1197.  
  1198. ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
  1199. {
  1200.    this->type = type;
  1201.    this->invariant = false;
  1202. }
  1203.  
  1204. void
  1205. ast_jump_statement::print(void) const
  1206. {
  1207.    switch (mode) {
  1208.    case ast_continue:
  1209.       printf("continue; ");
  1210.       break;
  1211.    case ast_break:
  1212.       printf("break; ");
  1213.       break;
  1214.    case ast_return:
  1215.       printf("return ");
  1216.       if (opt_return_value)
  1217.          opt_return_value->print();
  1218.  
  1219.       printf("; ");
  1220.       break;
  1221.    case ast_discard:
  1222.       printf("discard; ");
  1223.       break;
  1224.    }
  1225. }
  1226.  
  1227.  
  1228. ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
  1229.    : opt_return_value(NULL)
  1230. {
  1231.    this->mode = ast_jump_modes(mode);
  1232.  
  1233.    if (mode == ast_return)
  1234.       opt_return_value = return_value;
  1235. }
  1236.  
  1237.  
  1238. void
  1239. ast_selection_statement::print(void) const
  1240. {
  1241.    printf("if ( ");
  1242.    condition->print();
  1243.    printf(") ");
  1244.  
  1245.    then_statement->print();
  1246.  
  1247.    if (else_statement) {
  1248.       printf("else ");
  1249.       else_statement->print();
  1250.    }
  1251.    
  1252. }
  1253.  
  1254.  
  1255. ast_selection_statement::ast_selection_statement(ast_expression *condition,
  1256.                                                  ast_node *then_statement,
  1257.                                                  ast_node *else_statement)
  1258. {
  1259.    this->condition = condition;
  1260.    this->then_statement = then_statement;
  1261.    this->else_statement = else_statement;
  1262. }
  1263.  
  1264.  
  1265. void
  1266. ast_switch_statement::print(void) const
  1267. {
  1268.    printf("switch ( ");
  1269.    test_expression->print();
  1270.    printf(") ");
  1271.  
  1272.    body->print();
  1273. }
  1274.  
  1275.  
  1276. ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
  1277.                                            ast_node *body)
  1278. {
  1279.    this->test_expression = test_expression;
  1280.    this->body = body;
  1281. }
  1282.  
  1283.  
  1284. void
  1285. ast_switch_body::print(void) const
  1286. {
  1287.    printf("{\n");
  1288.    if (stmts != NULL) {
  1289.       stmts->print();
  1290.    }
  1291.    printf("}\n");
  1292. }
  1293.  
  1294.  
  1295. ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
  1296. {
  1297.    this->stmts = stmts;
  1298. }
  1299.  
  1300.  
  1301. void ast_case_label::print(void) const
  1302. {
  1303.    if (test_value != NULL) {
  1304.       printf("case ");
  1305.       test_value->print();
  1306.       printf(": ");
  1307.    } else {
  1308.       printf("default: ");
  1309.    }
  1310. }
  1311.  
  1312.  
  1313. ast_case_label::ast_case_label(ast_expression *test_value)
  1314. {
  1315.    this->test_value = test_value;
  1316. }
  1317.  
  1318.  
  1319. void ast_case_label_list::print(void) const
  1320. {
  1321.    foreach_list_const(n, & this->labels) {
  1322.       ast_node *ast = exec_node_data(ast_node, n, link);
  1323.       ast->print();
  1324.    }
  1325.    printf("\n");
  1326. }
  1327.  
  1328.  
  1329. ast_case_label_list::ast_case_label_list(void)
  1330. {
  1331. }
  1332.  
  1333.  
  1334. void ast_case_statement::print(void) const
  1335. {
  1336.    labels->print();
  1337.    foreach_list_const(n, & this->stmts) {
  1338.       ast_node *ast = exec_node_data(ast_node, n, link);
  1339.       ast->print();
  1340.       printf("\n");
  1341.    }
  1342. }
  1343.  
  1344.  
  1345. ast_case_statement::ast_case_statement(ast_case_label_list *labels)
  1346. {
  1347.    this->labels = labels;
  1348. }
  1349.  
  1350.  
  1351. void ast_case_statement_list::print(void) const
  1352. {
  1353.    foreach_list_const(n, & this->cases) {
  1354.       ast_node *ast = exec_node_data(ast_node, n, link);
  1355.       ast->print();
  1356.    }
  1357. }
  1358.  
  1359.  
  1360. ast_case_statement_list::ast_case_statement_list(void)
  1361. {
  1362. }
  1363.  
  1364.  
  1365. void
  1366. ast_iteration_statement::print(void) const
  1367. {
  1368.    switch (mode) {
  1369.    case ast_for:
  1370.       printf("for( ");
  1371.       if (init_statement)
  1372.          init_statement->print();
  1373.       printf("; ");
  1374.  
  1375.       if (condition)
  1376.          condition->print();
  1377.       printf("; ");
  1378.  
  1379.       if (rest_expression)
  1380.          rest_expression->print();
  1381.       printf(") ");
  1382.  
  1383.       body->print();
  1384.       break;
  1385.  
  1386.    case ast_while:
  1387.       printf("while ( ");
  1388.       if (condition)
  1389.          condition->print();
  1390.       printf(") ");
  1391.       body->print();
  1392.       break;
  1393.  
  1394.    case ast_do_while:
  1395.       printf("do ");
  1396.       body->print();
  1397.       printf("while ( ");
  1398.       if (condition)
  1399.          condition->print();
  1400.       printf("); ");
  1401.       break;
  1402.    }
  1403. }
  1404.  
  1405.  
  1406. ast_iteration_statement::ast_iteration_statement(int mode,
  1407.                                                  ast_node *init,
  1408.                                                  ast_node *condition,
  1409.                                                  ast_expression *rest_expression,
  1410.                                                  ast_node *body)
  1411. {
  1412.    this->mode = ast_iteration_modes(mode);
  1413.    this->init_statement = init;
  1414.    this->condition = condition;
  1415.    this->rest_expression = rest_expression;
  1416.    this->body = body;
  1417. }
  1418.  
  1419.  
  1420. void
  1421. ast_struct_specifier::print(void) const
  1422. {
  1423.    printf("struct %s { ", name);
  1424.    foreach_list_const(n, &this->declarations) {
  1425.       ast_node *ast = exec_node_data(ast_node, n, link);
  1426.       ast->print();
  1427.    }
  1428.    printf("} ");
  1429. }
  1430.  
  1431.  
  1432. ast_struct_specifier::ast_struct_specifier(const char *identifier,
  1433.                                            ast_declarator_list *declarator_list)
  1434. {
  1435.    if (identifier == NULL) {
  1436.       static unsigned anon_count = 1;
  1437.       identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
  1438.       anon_count++;
  1439.    }
  1440.    name = identifier;
  1441.    this->declarations.push_degenerate_list_at_head(&declarator_list->link);
  1442.    is_declaration = true;
  1443. }
  1444.  
  1445. extern "C" {
  1446.  
  1447. void
  1448. _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
  1449.                           bool dump_ast, bool dump_hir)
  1450. {
  1451.    struct _mesa_glsl_parse_state *state =
  1452.       new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
  1453.    const char *source = shader->Source;
  1454.  
  1455.    state->error = glcpp_preprocess(state, &source, &state->info_log,
  1456.                              &ctx->Extensions, ctx);
  1457.  
  1458.    if (!state->error) {
  1459.      _mesa_glsl_lexer_ctor(state, source);
  1460.      _mesa_glsl_parse(state);
  1461.      _mesa_glsl_lexer_dtor(state);
  1462.    }
  1463.  
  1464.    if (dump_ast) {
  1465.       foreach_list_const(n, &state->translation_unit) {
  1466.          ast_node *ast = exec_node_data(ast_node, n, link);
  1467.          ast->print();
  1468.       }
  1469.       printf("\n\n");
  1470.    }
  1471.  
  1472.    ralloc_free(shader->ir);
  1473.    shader->ir = new(shader) exec_list;
  1474.    if (!state->error && !state->translation_unit.is_empty())
  1475.       _mesa_ast_to_hir(shader->ir, state);
  1476.  
  1477.    if (!state->error) {
  1478.       validate_ir_tree(shader->ir);
  1479.  
  1480.       /* Print out the unoptimized IR. */
  1481.       if (dump_hir) {
  1482.          _mesa_print_ir(shader->ir, state);
  1483.       }
  1484.    }
  1485.  
  1486.  
  1487.    if (!state->error && !shader->ir->is_empty()) {
  1488.       struct gl_shader_compiler_options *options =
  1489.          &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
  1490.  
  1491.       /* Do some optimization at compile time to reduce shader IR size
  1492.        * and reduce later work if the same shader is linked multiple times
  1493.        */
  1494.       while (do_common_optimization(shader->ir, false, false, 32, options))
  1495.          ;
  1496.  
  1497.       validate_ir_tree(shader->ir);
  1498.    }
  1499.  
  1500.    if (shader->InfoLog)
  1501.       ralloc_free(shader->InfoLog);
  1502.  
  1503.    shader->symbols = state->symbols;
  1504.    shader->CompileStatus = !state->error;
  1505.    shader->InfoLog = state->info_log;
  1506.    shader->Version = state->language_version;
  1507.    shader->InfoLog = state->info_log;
  1508.    shader->IsES = state->es_shader;
  1509.  
  1510.    memcpy(shader->builtins_to_link, state->builtins_to_link,
  1511.           sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
  1512.    shader->num_builtins_to_link = state->num_builtins_to_link;
  1513.  
  1514.    if (shader->UniformBlocks)
  1515.       ralloc_free(shader->UniformBlocks);
  1516.    shader->NumUniformBlocks = state->num_uniform_blocks;
  1517.    shader->UniformBlocks = state->uniform_blocks;
  1518.    ralloc_steal(shader, shader->UniformBlocks);
  1519.  
  1520.    /* Retain any live IR, but trash the rest. */
  1521.    reparent_ir(shader->ir, shader->ir);
  1522.  
  1523.    ralloc_free(state);
  1524. }
  1525.  
  1526. } /* extern "C" */
  1527. /**
  1528.  * Do the set of common optimizations passes
  1529.  *
  1530.  * \param ir                          List of instructions to be optimized
  1531.  * \param linked                      Is the shader linked?  This enables
  1532.  *                                    optimizations passes that remove code at
  1533.  *                                    global scope and could cause linking to
  1534.  *                                    fail.
  1535.  * \param uniform_locations_assigned  Have locations already been assigned for
  1536.  *                                    uniforms?  This prevents the declarations
  1537.  *                                    of unused uniforms from being removed.
  1538.  *                                    The setting of this flag only matters if
  1539.  *                                    \c linked is \c true.
  1540.  * \param max_unroll_iterations       Maximum number of loop iterations to be
  1541.  *                                    unrolled.  Setting to 0 disables loop
  1542.  *                                    unrolling.
  1543.  * \param options                     The driver's preferred shader options.
  1544.  */
  1545. bool
  1546. do_common_optimization(exec_list *ir, bool linked,
  1547.                        bool uniform_locations_assigned,
  1548.                        unsigned max_unroll_iterations,
  1549.                        const struct gl_shader_compiler_options *options)
  1550. {
  1551.    GLboolean progress = GL_FALSE;
  1552.  
  1553.    progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
  1554.  
  1555.    if (linked) {
  1556.       progress = do_function_inlining(ir) || progress;
  1557.       progress = do_dead_functions(ir) || progress;
  1558.       progress = do_structure_splitting(ir) || progress;
  1559.    }
  1560.    progress = do_if_simplification(ir) || progress;
  1561.    progress = opt_flatten_nested_if_blocks(ir) || progress;
  1562.    progress = do_copy_propagation(ir) || progress;
  1563.    progress = do_copy_propagation_elements(ir) || progress;
  1564.  
  1565.    if (options->PreferDP4 && !linked)
  1566.       progress = opt_flip_matrices(ir) || progress;
  1567.  
  1568.    if (linked)
  1569.       progress = do_dead_code(ir, uniform_locations_assigned) || progress;
  1570.    else
  1571.       progress = do_dead_code_unlinked(ir) || progress;
  1572.    progress = do_dead_code_local(ir) || progress;
  1573.    progress = do_tree_grafting(ir) || progress;
  1574.    progress = do_constant_propagation(ir) || progress;
  1575.    if (linked)
  1576.       progress = do_constant_variable(ir) || progress;
  1577.    else
  1578.       progress = do_constant_variable_unlinked(ir) || progress;
  1579.    progress = do_constant_folding(ir) || progress;
  1580.    progress = do_algebraic(ir) || progress;
  1581.    progress = do_lower_jumps(ir) || progress;
  1582.    progress = do_vec_index_to_swizzle(ir) || progress;
  1583.    progress = lower_vector_insert(ir, false) || progress;
  1584.    progress = do_swizzle_swizzle(ir) || progress;
  1585.    progress = do_noop_swizzle(ir) || progress;
  1586.  
  1587.    progress = optimize_split_arrays(ir, linked) || progress;
  1588.    progress = optimize_redundant_jumps(ir) || progress;
  1589.  
  1590.    loop_state *ls = analyze_loop_variables(ir);
  1591.    if (ls->loop_found) {
  1592.       progress = set_loop_controls(ir, ls) || progress;
  1593.       progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
  1594.    }
  1595.    delete ls;
  1596.  
  1597.    return progress;
  1598. }
  1599.  
  1600. extern "C" {
  1601.  
  1602. /**
  1603.  * To be called at GL teardown time, this frees compiler datastructures.
  1604.  *
  1605.  * After calling this, any previously compiled shaders and shader
  1606.  * programs would be invalid.  So this should happen at approximately
  1607.  * program exit.
  1608.  */
  1609. void
  1610. _mesa_destroy_shader_compiler(void)
  1611. {
  1612.    _mesa_destroy_shader_compiler_caches();
  1613.  
  1614.    _mesa_glsl_release_types();
  1615. }
  1616.  
  1617. /**
  1618.  * Releases compiler caches to trade off performance for memory.
  1619.  *
  1620.  * Intended to be used with glReleaseShaderCompiler().
  1621.  */
  1622. void
  1623. _mesa_destroy_shader_compiler_caches(void)
  1624. {
  1625.    _mesa_glsl_release_functions();
  1626. }
  1627.  
  1628. }
  1629.