Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright © 2010 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.  
  24. /**
  25.  * \file ir_constant_expression.cpp
  26.  * Evaluate and process constant valued expressions
  27.  *
  28.  * In GLSL, constant valued expressions are used in several places.  These
  29.  * must be processed and evaluated very early in the compilation process.
  30.  *
  31.  *    * Sizes of arrays
  32.  *    * Initializers for uniforms
  33.  *    * Initializers for \c const variables
  34.  */
  35.  
  36. #include <math.h>
  37. #include "main/core.h" /* for MAX2, MIN2, CLAMP */
  38. #include "ir.h"
  39. #include "ir_visitor.h"
  40. #include "glsl_types.h"
  41. #include "program/hash_table.h"
  42.  
  43. static float
  44. dot(ir_constant *op0, ir_constant *op1)
  45. {
  46.    assert(op0->type->is_float() && op1->type->is_float());
  47.  
  48.    float result = 0;
  49.    for (unsigned c = 0; c < op0->type->components(); c++)
  50.       result += op0->value.f[c] * op1->value.f[c];
  51.  
  52.    return result;
  53. }
  54.  
  55. /* This method is the only one supported by gcc.  Unions in particular
  56.  * are iffy, and read-through-converted-pointer is killed by strict
  57.  * aliasing.  OTOH, the compiler sees through the memcpy, so the
  58.  * resulting asm is reasonable.
  59.  */
  60. static float
  61. bitcast_u2f(unsigned int u)
  62. {
  63.    assert(sizeof(float) == sizeof(unsigned int));
  64.    float f;
  65.    memcpy(&f, &u, sizeof(f));
  66.    return f;
  67. }
  68.  
  69. static unsigned int
  70. bitcast_f2u(float f)
  71. {
  72.    assert(sizeof(float) == sizeof(unsigned int));
  73.    unsigned int u;
  74.    memcpy(&u, &f, sizeof(f));
  75.    return u;
  76. }
  77.  
  78. /**
  79.  * Evaluate one component of a floating-point 4x8 unpacking function.
  80.  */
  81. typedef uint8_t
  82. (*pack_1x8_func_t)(float);
  83.  
  84. /**
  85.  * Evaluate one component of a floating-point 2x16 unpacking function.
  86.  */
  87. typedef uint16_t
  88. (*pack_1x16_func_t)(float);
  89.  
  90. /**
  91.  * Evaluate one component of a floating-point 4x8 unpacking function.
  92.  */
  93. typedef float
  94. (*unpack_1x8_func_t)(uint8_t);
  95.  
  96. /**
  97.  * Evaluate one component of a floating-point 2x16 unpacking function.
  98.  */
  99. typedef float
  100. (*unpack_1x16_func_t)(uint16_t);
  101.  
  102. /**
  103.  * Evaluate a 2x16 floating-point packing function.
  104.  */
  105. static uint32_t
  106. pack_2x16(pack_1x16_func_t pack_1x16,
  107.           float x, float y)
  108. {
  109.    /* From section 8.4 of the GLSL ES 3.00 spec:
  110.     *
  111.     *    packSnorm2x16
  112.     *    -------------
  113.     *    The first component of the vector will be written to the least
  114.     *    significant bits of the output; the last component will be written to
  115.     *    the most significant bits.
  116.     *
  117.     * The specifications for the other packing functions contain similar
  118.     * language.
  119.     */
  120.    uint32_t u = 0;
  121.    u |= ((uint32_t) pack_1x16(x) << 0);
  122.    u |= ((uint32_t) pack_1x16(y) << 16);
  123.    return u;
  124. }
  125.  
  126. /**
  127.  * Evaluate a 4x8 floating-point packing function.
  128.  */
  129. static uint32_t
  130. pack_4x8(pack_1x8_func_t pack_1x8,
  131.          float x, float y, float z, float w)
  132. {
  133.    /* From section 8.4 of the GLSL 4.30 spec:
  134.     *
  135.     *    packSnorm4x8
  136.     *    ------------
  137.     *    The first component of the vector will be written to the least
  138.     *    significant bits of the output; the last component will be written to
  139.     *    the most significant bits.
  140.     *
  141.     * The specifications for the other packing functions contain similar
  142.     * language.
  143.     */
  144.    uint32_t u = 0;
  145.    u |= ((uint32_t) pack_1x8(x) << 0);
  146.    u |= ((uint32_t) pack_1x8(y) << 8);
  147.    u |= ((uint32_t) pack_1x8(z) << 16);
  148.    u |= ((uint32_t) pack_1x8(w) << 24);
  149.    return u;
  150. }
  151.  
  152. /**
  153.  * Evaluate a 2x16 floating-point unpacking function.
  154.  */
  155. static void
  156. unpack_2x16(unpack_1x16_func_t unpack_1x16,
  157.             uint32_t u,
  158.             float *x, float *y)
  159. {
  160.     /* From section 8.4 of the GLSL ES 3.00 spec:
  161.      *
  162.      *    unpackSnorm2x16
  163.      *    ---------------
  164.      *    The first component of the returned vector will be extracted from
  165.      *    the least significant bits of the input; the last component will be
  166.      *    extracted from the most significant bits.
  167.      *
  168.      * The specifications for the other unpacking functions contain similar
  169.      * language.
  170.      */
  171.    *x = unpack_1x16((uint16_t) (u & 0xffff));
  172.    *y = unpack_1x16((uint16_t) (u >> 16));
  173. }
  174.  
  175. /**
  176.  * Evaluate a 4x8 floating-point unpacking function.
  177.  */
  178. static void
  179. unpack_4x8(unpack_1x8_func_t unpack_1x8, uint32_t u,
  180.            float *x, float *y, float *z, float *w)
  181. {
  182.     /* From section 8.4 of the GLSL 4.30 spec:
  183.      *
  184.      *    unpackSnorm4x8
  185.      *    --------------
  186.      *    The first component of the returned vector will be extracted from
  187.      *    the least significant bits of the input; the last component will be
  188.      *    extracted from the most significant bits.
  189.      *
  190.      * The specifications for the other unpacking functions contain similar
  191.      * language.
  192.      */
  193.    *x = unpack_1x8((uint8_t) (u & 0xff));
  194.    *y = unpack_1x8((uint8_t) (u >> 8));
  195.    *z = unpack_1x8((uint8_t) (u >> 16));
  196.    *w = unpack_1x8((uint8_t) (u >> 24));
  197. }
  198.  
  199. /**
  200.  * Evaluate one component of packSnorm4x8.
  201.  */
  202. static uint8_t
  203. pack_snorm_1x8(float x)
  204. {
  205.     /* From section 8.4 of the GLSL 4.30 spec:
  206.      *
  207.      *    packSnorm4x8
  208.      *    ------------
  209.      *    The conversion for component c of v to fixed point is done as
  210.      *    follows:
  211.      *
  212.      *      packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
  213.      *
  214.      * We must first cast the float to an int, because casting a negative
  215.      * float to a uint is undefined.
  216.      */
  217.    return (uint8_t) (int8_t)
  218.           _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 127.0f);
  219. }
  220.  
  221. /**
  222.  * Evaluate one component of packSnorm2x16.
  223.  */
  224. static uint16_t
  225. pack_snorm_1x16(float x)
  226. {
  227.     /* From section 8.4 of the GLSL ES 3.00 spec:
  228.      *
  229.      *    packSnorm2x16
  230.      *    -------------
  231.      *    The conversion for component c of v to fixed point is done as
  232.      *    follows:
  233.      *
  234.      *      packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
  235.      *
  236.      * We must first cast the float to an int, because casting a negative
  237.      * float to a uint is undefined.
  238.      */
  239.    return (uint16_t) (int16_t)
  240.           _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
  241. }
  242.  
  243. /**
  244.  * Evaluate one component of unpackSnorm4x8.
  245.  */
  246. static float
  247. unpack_snorm_1x8(uint8_t u)
  248. {
  249.     /* From section 8.4 of the GLSL 4.30 spec:
  250.      *
  251.      *    unpackSnorm4x8
  252.      *    --------------
  253.      *    The conversion for unpacked fixed-point value f to floating point is
  254.      *    done as follows:
  255.      *
  256.      *       unpackSnorm4x8: clamp(f / 127.0, -1, +1)
  257.      */
  258.    return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
  259. }
  260.  
  261. /**
  262.  * Evaluate one component of unpackSnorm2x16.
  263.  */
  264. static float
  265. unpack_snorm_1x16(uint16_t u)
  266. {
  267.     /* From section 8.4 of the GLSL ES 3.00 spec:
  268.      *
  269.      *    unpackSnorm2x16
  270.      *    ---------------
  271.      *    The conversion for unpacked fixed-point value f to floating point is
  272.      *    done as follows:
  273.      *
  274.      *       unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
  275.      */
  276.    return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
  277. }
  278.  
  279. /**
  280.  * Evaluate one component packUnorm4x8.
  281.  */
  282. static uint8_t
  283. pack_unorm_1x8(float x)
  284. {
  285.     /* From section 8.4 of the GLSL 4.30 spec:
  286.      *
  287.      *    packUnorm4x8
  288.      *    ------------
  289.      *    The conversion for component c of v to fixed point is done as
  290.      *    follows:
  291.      *
  292.      *       packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
  293.      */
  294.    return (uint8_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 255.0f);
  295. }
  296.  
  297. /**
  298.  * Evaluate one component packUnorm2x16.
  299.  */
  300. static uint16_t
  301. pack_unorm_1x16(float x)
  302. {
  303.     /* From section 8.4 of the GLSL ES 3.00 spec:
  304.      *
  305.      *    packUnorm2x16
  306.      *    -------------
  307.      *    The conversion for component c of v to fixed point is done as
  308.      *    follows:
  309.      *
  310.      *       packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
  311.      */
  312.    return (uint16_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
  313. }
  314.  
  315. /**
  316.  * Evaluate one component of unpackUnorm4x8.
  317.  */
  318. static float
  319. unpack_unorm_1x8(uint8_t u)
  320. {
  321.     /* From section 8.4 of the GLSL 4.30 spec:
  322.      *
  323.      *    unpackUnorm4x8
  324.      *    --------------
  325.      *    The conversion for unpacked fixed-point value f to floating point is
  326.      *    done as follows:
  327.      *
  328.      *       unpackUnorm4x8: f / 255.0
  329.      */
  330.    return (float) u / 255.0f;
  331. }
  332.  
  333. /**
  334.  * Evaluate one component of unpackUnorm2x16.
  335.  */
  336. static float
  337. unpack_unorm_1x16(uint16_t u)
  338. {
  339.     /* From section 8.4 of the GLSL ES 3.00 spec:
  340.      *
  341.      *    unpackUnorm2x16
  342.      *    ---------------
  343.      *    The conversion for unpacked fixed-point value f to floating point is
  344.      *    done as follows:
  345.      *
  346.      *       unpackUnorm2x16: f / 65535.0
  347.      */
  348.    return (float) u / 65535.0f;
  349. }
  350.  
  351. /**
  352.  * Evaluate one component of packHalf2x16.
  353.  */
  354. static uint16_t
  355. pack_half_1x16(float x)
  356. {
  357.    return _mesa_float_to_half(x);
  358. }
  359.  
  360. /**
  361.  * Evaluate one component of unpackHalf2x16.
  362.  */
  363. static float
  364. unpack_half_1x16(uint16_t u)
  365. {
  366.    return _mesa_half_to_float(u);
  367. }
  368.  
  369. ir_constant *
  370. ir_rvalue::constant_expression_value(struct hash_table *variable_context)
  371. {
  372.    assert(this->type->is_error());
  373.    return NULL;
  374. }
  375.  
  376. ir_constant *
  377. ir_expression::constant_expression_value(struct hash_table *variable_context)
  378. {
  379.    if (this->type->is_error())
  380.       return NULL;
  381.  
  382.    ir_constant *op[Elements(this->operands)] = { NULL, };
  383.    ir_constant_data data;
  384.  
  385.    memset(&data, 0, sizeof(data));
  386.  
  387.    for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
  388.       op[operand] = this->operands[operand]->constant_expression_value(variable_context);
  389.       if (!op[operand])
  390.          return NULL;
  391.    }
  392.  
  393.    if (op[1] != NULL)
  394.       switch (this->operation) {
  395.       case ir_binop_lshift:
  396.       case ir_binop_rshift:
  397.       case ir_binop_vector_extract:
  398.       case ir_triop_bitfield_extract:
  399.          break;
  400.  
  401.       default:
  402.          assert(op[0]->type->base_type == op[1]->type->base_type);
  403.          break;
  404.       }
  405.  
  406.    bool op0_scalar = op[0]->type->is_scalar();
  407.    bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
  408.  
  409.    /* When iterating over a vector or matrix's components, we want to increase
  410.     * the loop counter.  However, for scalars, we want to stay at 0.
  411.     */
  412.    unsigned c0_inc = op0_scalar ? 0 : 1;
  413.    unsigned c1_inc = op1_scalar ? 0 : 1;
  414.    unsigned components;
  415.    if (op1_scalar || !op[1]) {
  416.       components = op[0]->type->components();
  417.    } else {
  418.       components = op[1]->type->components();
  419.    }
  420.  
  421.    void *ctx = ralloc_parent(this);
  422.  
  423.    /* Handle array operations here, rather than below. */
  424.    if (op[0]->type->is_array()) {
  425.       assert(op[1] != NULL && op[1]->type->is_array());
  426.       switch (this->operation) {
  427.       case ir_binop_all_equal:
  428.          return new(ctx) ir_constant(op[0]->has_value(op[1]));
  429.       case ir_binop_any_nequal:
  430.          return new(ctx) ir_constant(!op[0]->has_value(op[1]));
  431.       default:
  432.          break;
  433.       }
  434.       return NULL;
  435.    }
  436.  
  437.    switch (this->operation) {
  438.    case ir_unop_bit_not:
  439.        switch (op[0]->type->base_type) {
  440.        case GLSL_TYPE_INT:
  441.            for (unsigned c = 0; c < components; c++)
  442.                data.i[c] = ~ op[0]->value.i[c];
  443.            break;
  444.        case GLSL_TYPE_UINT:
  445.            for (unsigned c = 0; c < components; c++)
  446.                data.u[c] = ~ op[0]->value.u[c];
  447.            break;
  448.        default:
  449.            assert(0);
  450.        }
  451.        break;
  452.  
  453.    case ir_unop_logic_not:
  454.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  455.       for (unsigned c = 0; c < op[0]->type->components(); c++)
  456.          data.b[c] = !op[0]->value.b[c];
  457.       break;
  458.  
  459.    case ir_unop_f2i:
  460.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  461.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  462.          data.i[c] = (int) op[0]->value.f[c];
  463.       }
  464.       break;
  465.    case ir_unop_f2u:
  466.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  467.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  468.          data.i[c] = (unsigned) op[0]->value.f[c];
  469.       }
  470.       break;
  471.    case ir_unop_i2f:
  472.       assert(op[0]->type->base_type == GLSL_TYPE_INT);
  473.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  474.          data.f[c] = (float) op[0]->value.i[c];
  475.       }
  476.       break;
  477.    case ir_unop_u2f:
  478.       assert(op[0]->type->base_type == GLSL_TYPE_UINT);
  479.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  480.          data.f[c] = (float) op[0]->value.u[c];
  481.       }
  482.       break;
  483.    case ir_unop_b2f:
  484.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  485.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  486.          data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
  487.       }
  488.       break;
  489.    case ir_unop_f2b:
  490.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  491.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  492.          data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
  493.       }
  494.       break;
  495.    case ir_unop_b2i:
  496.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  497.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  498.          data.u[c] = op[0]->value.b[c] ? 1 : 0;
  499.       }
  500.       break;
  501.    case ir_unop_i2b:
  502.       assert(op[0]->type->is_integer());
  503.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  504.          data.b[c] = op[0]->value.u[c] ? true : false;
  505.       }
  506.       break;
  507.    case ir_unop_u2i:
  508.       assert(op[0]->type->base_type == GLSL_TYPE_UINT);
  509.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  510.          data.i[c] = op[0]->value.u[c];
  511.       }
  512.       break;
  513.    case ir_unop_i2u:
  514.       assert(op[0]->type->base_type == GLSL_TYPE_INT);
  515.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  516.          data.u[c] = op[0]->value.i[c];
  517.       }
  518.       break;
  519.    case ir_unop_bitcast_i2f:
  520.       assert(op[0]->type->base_type == GLSL_TYPE_INT);
  521.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  522.          data.f[c] = bitcast_u2f(op[0]->value.i[c]);
  523.       }
  524.       break;
  525.    case ir_unop_bitcast_f2i:
  526.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  527.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  528.          data.i[c] = bitcast_f2u(op[0]->value.f[c]);
  529.       }
  530.       break;
  531.    case ir_unop_bitcast_u2f:
  532.       assert(op[0]->type->base_type == GLSL_TYPE_UINT);
  533.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  534.          data.f[c] = bitcast_u2f(op[0]->value.u[c]);
  535.       }
  536.       break;
  537.    case ir_unop_bitcast_f2u:
  538.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  539.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  540.          data.u[c] = bitcast_f2u(op[0]->value.f[c]);
  541.       }
  542.       break;
  543.    case ir_unop_any:
  544.       assert(op[0]->type->is_boolean());
  545.       data.b[0] = false;
  546.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  547.          if (op[0]->value.b[c])
  548.             data.b[0] = true;
  549.       }
  550.       break;
  551.  
  552.    case ir_unop_trunc:
  553.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  554.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  555.          data.f[c] = truncf(op[0]->value.f[c]);
  556.       }
  557.       break;
  558.  
  559.    case ir_unop_round_even:
  560.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  561.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  562.          data.f[c] = _mesa_round_to_even(op[0]->value.f[c]);
  563.       }
  564.       break;
  565.  
  566.    case ir_unop_ceil:
  567.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  568.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  569.          data.f[c] = ceilf(op[0]->value.f[c]);
  570.       }
  571.       break;
  572.  
  573.    case ir_unop_floor:
  574.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  575.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  576.          data.f[c] = floorf(op[0]->value.f[c]);
  577.       }
  578.       break;
  579.  
  580.    case ir_unop_fract:
  581.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  582.          switch (this->type->base_type) {
  583.          case GLSL_TYPE_UINT:
  584.             data.u[c] = 0;
  585.             break;
  586.          case GLSL_TYPE_INT:
  587.             data.i[c] = 0;
  588.             break;
  589.          case GLSL_TYPE_FLOAT:
  590.             data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
  591.             break;
  592.          default:
  593.             assert(0);
  594.          }
  595.       }
  596.       break;
  597.  
  598.    case ir_unop_sin:
  599.    case ir_unop_sin_reduced:
  600.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  601.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  602.          data.f[c] = sinf(op[0]->value.f[c]);
  603.       }
  604.       break;
  605.  
  606.    case ir_unop_cos:
  607.    case ir_unop_cos_reduced:
  608.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  609.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  610.          data.f[c] = cosf(op[0]->value.f[c]);
  611.       }
  612.       break;
  613.  
  614.    case ir_unop_neg:
  615.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  616.          switch (this->type->base_type) {
  617.          case GLSL_TYPE_UINT:
  618.             data.u[c] = -((int) op[0]->value.u[c]);
  619.             break;
  620.          case GLSL_TYPE_INT:
  621.             data.i[c] = -op[0]->value.i[c];
  622.             break;
  623.          case GLSL_TYPE_FLOAT:
  624.             data.f[c] = -op[0]->value.f[c];
  625.             break;
  626.          default:
  627.             assert(0);
  628.          }
  629.       }
  630.       break;
  631.  
  632.    case ir_unop_abs:
  633.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  634.          switch (this->type->base_type) {
  635.          case GLSL_TYPE_UINT:
  636.             data.u[c] = op[0]->value.u[c];
  637.             break;
  638.          case GLSL_TYPE_INT:
  639.             data.i[c] = op[0]->value.i[c];
  640.             if (data.i[c] < 0)
  641.                data.i[c] = -data.i[c];
  642.             break;
  643.          case GLSL_TYPE_FLOAT:
  644.             data.f[c] = fabs(op[0]->value.f[c]);
  645.             break;
  646.          default:
  647.             assert(0);
  648.          }
  649.       }
  650.       break;
  651.  
  652.    case ir_unop_sign:
  653.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  654.          switch (this->type->base_type) {
  655.          case GLSL_TYPE_UINT:
  656.             data.u[c] = op[0]->value.i[c] > 0;
  657.             break;
  658.          case GLSL_TYPE_INT:
  659.             data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
  660.             break;
  661.          case GLSL_TYPE_FLOAT:
  662.             data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
  663.             break;
  664.          default:
  665.             assert(0);
  666.          }
  667.       }
  668.       break;
  669.  
  670.    case ir_unop_rcp:
  671.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  672.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  673.          switch (this->type->base_type) {
  674.          case GLSL_TYPE_UINT:
  675.             if (op[0]->value.u[c] != 0.0)
  676.                data.u[c] = 1 / op[0]->value.u[c];
  677.             break;
  678.          case GLSL_TYPE_INT:
  679.             if (op[0]->value.i[c] != 0.0)
  680.                data.i[c] = 1 / op[0]->value.i[c];
  681.             break;
  682.          case GLSL_TYPE_FLOAT:
  683.             if (op[0]->value.f[c] != 0.0)
  684.                data.f[c] = 1.0F / op[0]->value.f[c];
  685.             break;
  686.          default:
  687.             assert(0);
  688.          }
  689.       }
  690.       break;
  691.  
  692.    case ir_unop_rsq:
  693.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  694.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  695.          data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
  696.       }
  697.       break;
  698.  
  699.    case ir_unop_sqrt:
  700.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  701.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  702.          data.f[c] = sqrtf(op[0]->value.f[c]);
  703.       }
  704.       break;
  705.  
  706.    case ir_unop_exp:
  707.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  708.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  709.          data.f[c] = expf(op[0]->value.f[c]);
  710.       }
  711.       break;
  712.  
  713.    case ir_unop_exp2:
  714.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  715.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  716.          data.f[c] = exp2f(op[0]->value.f[c]);
  717.       }
  718.       break;
  719.  
  720.    case ir_unop_log:
  721.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  722.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  723.          data.f[c] = logf(op[0]->value.f[c]);
  724.       }
  725.       break;
  726.  
  727.    case ir_unop_log2:
  728.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  729.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  730.          data.f[c] = log2f(op[0]->value.f[c]);
  731.       }
  732.       break;
  733.  
  734.    case ir_unop_dFdx:
  735.    case ir_unop_dFdy:
  736.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  737.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  738.          data.f[c] = 0.0;
  739.       }
  740.       break;
  741.  
  742.    case ir_unop_pack_snorm_2x16:
  743.       assert(op[0]->type == glsl_type::vec2_type);
  744.       data.u[0] = pack_2x16(pack_snorm_1x16,
  745.                             op[0]->value.f[0],
  746.                             op[0]->value.f[1]);
  747.       break;
  748.    case ir_unop_pack_snorm_4x8:
  749.       assert(op[0]->type == glsl_type::vec4_type);
  750.       data.u[0] = pack_4x8(pack_snorm_1x8,
  751.                            op[0]->value.f[0],
  752.                            op[0]->value.f[1],
  753.                            op[0]->value.f[2],
  754.                            op[0]->value.f[3]);
  755.       break;
  756.    case ir_unop_unpack_snorm_2x16:
  757.       assert(op[0]->type == glsl_type::uint_type);
  758.       unpack_2x16(unpack_snorm_1x16,
  759.                   op[0]->value.u[0],
  760.                   &data.f[0], &data.f[1]);
  761.       break;
  762.    case ir_unop_unpack_snorm_4x8:
  763.       assert(op[0]->type == glsl_type::uint_type);
  764.       unpack_4x8(unpack_snorm_1x8,
  765.                  op[0]->value.u[0],
  766.                  &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
  767.       break;
  768.    case ir_unop_pack_unorm_2x16:
  769.       assert(op[0]->type == glsl_type::vec2_type);
  770.       data.u[0] = pack_2x16(pack_unorm_1x16,
  771.                             op[0]->value.f[0],
  772.                             op[0]->value.f[1]);
  773.       break;
  774.    case ir_unop_pack_unorm_4x8:
  775.       assert(op[0]->type == glsl_type::vec4_type);
  776.       data.u[0] = pack_4x8(pack_unorm_1x8,
  777.                            op[0]->value.f[0],
  778.                            op[0]->value.f[1],
  779.                            op[0]->value.f[2],
  780.                            op[0]->value.f[3]);
  781.       break;
  782.    case ir_unop_unpack_unorm_2x16:
  783.       assert(op[0]->type == glsl_type::uint_type);
  784.       unpack_2x16(unpack_unorm_1x16,
  785.                   op[0]->value.u[0],
  786.                   &data.f[0], &data.f[1]);
  787.       break;
  788.    case ir_unop_unpack_unorm_4x8:
  789.       assert(op[0]->type == glsl_type::uint_type);
  790.       unpack_4x8(unpack_unorm_1x8,
  791.                  op[0]->value.u[0],
  792.                  &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
  793.       break;
  794.    case ir_unop_pack_half_2x16:
  795.       assert(op[0]->type == glsl_type::vec2_type);
  796.       data.u[0] = pack_2x16(pack_half_1x16,
  797.                             op[0]->value.f[0],
  798.                             op[0]->value.f[1]);
  799.       break;
  800.    case ir_unop_unpack_half_2x16:
  801.       assert(op[0]->type == glsl_type::uint_type);
  802.       unpack_2x16(unpack_half_1x16,
  803.                   op[0]->value.u[0],
  804.                   &data.f[0], &data.f[1]);
  805.       break;
  806.    case ir_binop_pow:
  807.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  808.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  809.          data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
  810.       }
  811.       break;
  812.  
  813.    case ir_binop_dot:
  814.       data.f[0] = dot(op[0], op[1]);
  815.       break;
  816.  
  817.    case ir_binop_min:
  818.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  819.       for (unsigned c = 0, c0 = 0, c1 = 0;
  820.            c < components;
  821.            c0 += c0_inc, c1 += c1_inc, c++) {
  822.  
  823.          switch (op[0]->type->base_type) {
  824.          case GLSL_TYPE_UINT:
  825.             data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
  826.             break;
  827.          case GLSL_TYPE_INT:
  828.             data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
  829.             break;
  830.          case GLSL_TYPE_FLOAT:
  831.             data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
  832.             break;
  833.          default:
  834.             assert(0);
  835.          }
  836.       }
  837.  
  838.       break;
  839.    case ir_binop_max:
  840.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  841.       for (unsigned c = 0, c0 = 0, c1 = 0;
  842.            c < components;
  843.            c0 += c0_inc, c1 += c1_inc, c++) {
  844.  
  845.          switch (op[0]->type->base_type) {
  846.          case GLSL_TYPE_UINT:
  847.             data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
  848.             break;
  849.          case GLSL_TYPE_INT:
  850.             data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
  851.             break;
  852.          case GLSL_TYPE_FLOAT:
  853.             data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
  854.             break;
  855.          default:
  856.             assert(0);
  857.          }
  858.       }
  859.       break;
  860.  
  861.    case ir_binop_add:
  862.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  863.       for (unsigned c = 0, c0 = 0, c1 = 0;
  864.            c < components;
  865.            c0 += c0_inc, c1 += c1_inc, c++) {
  866.  
  867.          switch (op[0]->type->base_type) {
  868.          case GLSL_TYPE_UINT:
  869.             data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
  870.             break;
  871.          case GLSL_TYPE_INT:
  872.             data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
  873.             break;
  874.          case GLSL_TYPE_FLOAT:
  875.             data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
  876.             break;
  877.          default:
  878.             assert(0);
  879.          }
  880.       }
  881.  
  882.       break;
  883.    case ir_binop_sub:
  884.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  885.       for (unsigned c = 0, c0 = 0, c1 = 0;
  886.            c < components;
  887.            c0 += c0_inc, c1 += c1_inc, c++) {
  888.  
  889.          switch (op[0]->type->base_type) {
  890.          case GLSL_TYPE_UINT:
  891.             data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
  892.             break;
  893.          case GLSL_TYPE_INT:
  894.             data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
  895.             break;
  896.          case GLSL_TYPE_FLOAT:
  897.             data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
  898.             break;
  899.          default:
  900.             assert(0);
  901.          }
  902.       }
  903.  
  904.       break;
  905.    case ir_binop_mul:
  906.       /* Check for equal types, or unequal types involving scalars */
  907.       if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
  908.           || op0_scalar || op1_scalar) {
  909.          for (unsigned c = 0, c0 = 0, c1 = 0;
  910.               c < components;
  911.               c0 += c0_inc, c1 += c1_inc, c++) {
  912.  
  913.             switch (op[0]->type->base_type) {
  914.             case GLSL_TYPE_UINT:
  915.                data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
  916.                break;
  917.             case GLSL_TYPE_INT:
  918.                data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
  919.                break;
  920.             case GLSL_TYPE_FLOAT:
  921.                data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
  922.                break;
  923.             default:
  924.                assert(0);
  925.             }
  926.          }
  927.       } else {
  928.          assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
  929.  
  930.          /* Multiply an N-by-M matrix with an M-by-P matrix.  Since either
  931.           * matrix can be a GLSL vector, either N or P can be 1.
  932.           *
  933.           * For vec*mat, the vector is treated as a row vector.  This
  934.           * means the vector is a 1-row x M-column matrix.
  935.           *
  936.           * For mat*vec, the vector is treated as a column vector.  Since
  937.           * matrix_columns is 1 for vectors, this just works.
  938.           */
  939.          const unsigned n = op[0]->type->is_vector()
  940.             ? 1 : op[0]->type->vector_elements;
  941.          const unsigned m = op[1]->type->vector_elements;
  942.          const unsigned p = op[1]->type->matrix_columns;
  943.          for (unsigned j = 0; j < p; j++) {
  944.             for (unsigned i = 0; i < n; i++) {
  945.                for (unsigned k = 0; k < m; k++) {
  946.                   data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
  947.                }
  948.             }
  949.          }
  950.       }
  951.  
  952.       break;
  953.    case ir_binop_div:
  954.       /* FINISHME: Emit warning when division-by-zero is detected. */
  955.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  956.       for (unsigned c = 0, c0 = 0, c1 = 0;
  957.            c < components;
  958.            c0 += c0_inc, c1 += c1_inc, c++) {
  959.  
  960.          switch (op[0]->type->base_type) {
  961.          case GLSL_TYPE_UINT:
  962.             if (op[1]->value.u[c1] == 0) {
  963.                data.u[c] = 0;
  964.             } else {
  965.                data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
  966.             }
  967.             break;
  968.          case GLSL_TYPE_INT:
  969.             if (op[1]->value.i[c1] == 0) {
  970.                data.i[c] = 0;
  971.             } else {
  972.                data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
  973.             }
  974.             break;
  975.          case GLSL_TYPE_FLOAT:
  976.             data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
  977.             break;
  978.          default:
  979.             assert(0);
  980.          }
  981.       }
  982.  
  983.       break;
  984.    case ir_binop_mod:
  985.       /* FINISHME: Emit warning when division-by-zero is detected. */
  986.       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
  987.       for (unsigned c = 0, c0 = 0, c1 = 0;
  988.            c < components;
  989.            c0 += c0_inc, c1 += c1_inc, c++) {
  990.  
  991.          switch (op[0]->type->base_type) {
  992.          case GLSL_TYPE_UINT:
  993.             if (op[1]->value.u[c1] == 0) {
  994.                data.u[c] = 0;
  995.             } else {
  996.                data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
  997.             }
  998.             break;
  999.          case GLSL_TYPE_INT:
  1000.             if (op[1]->value.i[c1] == 0) {
  1001.                data.i[c] = 0;
  1002.             } else {
  1003.                data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
  1004.             }
  1005.             break;
  1006.          case GLSL_TYPE_FLOAT:
  1007.             /* We don't use fmod because it rounds toward zero; GLSL specifies
  1008.              * the use of floor.
  1009.              */
  1010.             data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
  1011.                * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
  1012.             break;
  1013.          default:
  1014.             assert(0);
  1015.          }
  1016.       }
  1017.  
  1018.       break;
  1019.  
  1020.    case ir_binop_logic_and:
  1021.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  1022.       for (unsigned c = 0; c < op[0]->type->components(); c++)
  1023.          data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
  1024.       break;
  1025.    case ir_binop_logic_xor:
  1026.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  1027.       for (unsigned c = 0; c < op[0]->type->components(); c++)
  1028.          data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
  1029.       break;
  1030.    case ir_binop_logic_or:
  1031.       assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  1032.       for (unsigned c = 0; c < op[0]->type->components(); c++)
  1033.          data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
  1034.       break;
  1035.  
  1036.    case ir_binop_less:
  1037.       assert(op[0]->type == op[1]->type);
  1038.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  1039.          switch (op[0]->type->base_type) {
  1040.          case GLSL_TYPE_UINT:
  1041.             data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
  1042.             break;
  1043.          case GLSL_TYPE_INT:
  1044.             data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
  1045.             break;
  1046.          case GLSL_TYPE_FLOAT:
  1047.             data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
  1048.             break;
  1049.          default:
  1050.             assert(0);
  1051.          }
  1052.       }
  1053.       break;
  1054.    case ir_binop_greater:
  1055.       assert(op[0]->type == op[1]->type);
  1056.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  1057.          switch (op[0]->type->base_type) {
  1058.          case GLSL_TYPE_UINT:
  1059.             data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
  1060.             break;
  1061.          case GLSL_TYPE_INT:
  1062.             data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
  1063.             break;
  1064.          case GLSL_TYPE_FLOAT:
  1065.             data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
  1066.             break;
  1067.          default:
  1068.             assert(0);
  1069.          }
  1070.       }
  1071.       break;
  1072.    case ir_binop_lequal:
  1073.       assert(op[0]->type == op[1]->type);
  1074.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  1075.          switch (op[0]->type->base_type) {
  1076.          case GLSL_TYPE_UINT:
  1077.             data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
  1078.             break;
  1079.          case GLSL_TYPE_INT:
  1080.             data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
  1081.             break;
  1082.          case GLSL_TYPE_FLOAT:
  1083.             data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
  1084.             break;
  1085.          default:
  1086.             assert(0);
  1087.          }
  1088.       }
  1089.       break;
  1090.    case ir_binop_gequal:
  1091.       assert(op[0]->type == op[1]->type);
  1092.       for (unsigned c = 0; c < op[0]->type->components(); c++) {
  1093.          switch (op[0]->type->base_type) {
  1094.          case GLSL_TYPE_UINT:
  1095.             data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
  1096.             break;
  1097.          case GLSL_TYPE_INT:
  1098.             data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
  1099.             break;
  1100.          case GLSL_TYPE_FLOAT:
  1101.             data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
  1102.             break;
  1103.          default:
  1104.             assert(0);
  1105.          }
  1106.       }
  1107.       break;
  1108.    case ir_binop_equal:
  1109.       assert(op[0]->type == op[1]->type);
  1110.       for (unsigned c = 0; c < components; c++) {
  1111.          switch (op[0]->type->base_type) {
  1112.          case GLSL_TYPE_UINT:
  1113.             data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
  1114.             break;
  1115.          case GLSL_TYPE_INT:
  1116.             data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
  1117.             break;
  1118.          case GLSL_TYPE_FLOAT:
  1119.             data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
  1120.             break;
  1121.          case GLSL_TYPE_BOOL:
  1122.             data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
  1123.             break;
  1124.          default:
  1125.             assert(0);
  1126.          }
  1127.       }
  1128.       break;
  1129.    case ir_binop_nequal:
  1130.       assert(op[0]->type == op[1]->type);
  1131.       for (unsigned c = 0; c < components; c++) {
  1132.          switch (op[0]->type->base_type) {
  1133.          case GLSL_TYPE_UINT:
  1134.             data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
  1135.             break;
  1136.          case GLSL_TYPE_INT:
  1137.             data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
  1138.             break;
  1139.          case GLSL_TYPE_FLOAT:
  1140.             data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
  1141.             break;
  1142.          case GLSL_TYPE_BOOL:
  1143.             data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
  1144.             break;
  1145.          default:
  1146.             assert(0);
  1147.          }
  1148.       }
  1149.       break;
  1150.    case ir_binop_all_equal:
  1151.       data.b[0] = op[0]->has_value(op[1]);
  1152.       break;
  1153.    case ir_binop_any_nequal:
  1154.       data.b[0] = !op[0]->has_value(op[1]);
  1155.       break;
  1156.  
  1157.    case ir_binop_lshift:
  1158.       for (unsigned c = 0, c0 = 0, c1 = 0;
  1159.            c < components;
  1160.            c0 += c0_inc, c1 += c1_inc, c++) {
  1161.  
  1162.           if (op[0]->type->base_type == GLSL_TYPE_INT &&
  1163.               op[1]->type->base_type == GLSL_TYPE_INT) {
  1164.               data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
  1165.  
  1166.           } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
  1167.                      op[1]->type->base_type == GLSL_TYPE_UINT) {
  1168.               data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
  1169.  
  1170.           } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
  1171.                      op[1]->type->base_type == GLSL_TYPE_INT) {
  1172.               data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
  1173.  
  1174.           } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
  1175.                      op[1]->type->base_type == GLSL_TYPE_UINT) {
  1176.               data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
  1177.           }
  1178.       }
  1179.       break;
  1180.  
  1181.    case ir_binop_rshift:
  1182.        for (unsigned c = 0, c0 = 0, c1 = 0;
  1183.             c < components;
  1184.             c0 += c0_inc, c1 += c1_inc, c++) {
  1185.  
  1186.            if (op[0]->type->base_type == GLSL_TYPE_INT &&
  1187.                op[1]->type->base_type == GLSL_TYPE_INT) {
  1188.                data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
  1189.  
  1190.            } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
  1191.                       op[1]->type->base_type == GLSL_TYPE_UINT) {
  1192.                data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
  1193.  
  1194.            } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
  1195.                       op[1]->type->base_type == GLSL_TYPE_INT) {
  1196.                data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
  1197.  
  1198.            } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
  1199.                       op[1]->type->base_type == GLSL_TYPE_UINT) {
  1200.                data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
  1201.            }
  1202.        }
  1203.        break;
  1204.  
  1205.    case ir_binop_bit_and:
  1206.       for (unsigned c = 0, c0 = 0, c1 = 0;
  1207.            c < components;
  1208.            c0 += c0_inc, c1 += c1_inc, c++) {
  1209.  
  1210.           switch (op[0]->type->base_type) {
  1211.           case GLSL_TYPE_INT:
  1212.               data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
  1213.               break;
  1214.           case GLSL_TYPE_UINT:
  1215.               data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
  1216.               break;
  1217.           default:
  1218.               assert(0);
  1219.           }
  1220.       }
  1221.       break;
  1222.  
  1223.    case ir_binop_bit_or:
  1224.       for (unsigned c = 0, c0 = 0, c1 = 0;
  1225.            c < components;
  1226.            c0 += c0_inc, c1 += c1_inc, c++) {
  1227.  
  1228.           switch (op[0]->type->base_type) {
  1229.           case GLSL_TYPE_INT:
  1230.               data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
  1231.               break;
  1232.           case GLSL_TYPE_UINT:
  1233.               data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
  1234.               break;
  1235.           default:
  1236.               assert(0);
  1237.           }
  1238.       }
  1239.       break;
  1240.  
  1241.    case ir_binop_vector_extract: {
  1242.       const int c = CLAMP(op[1]->value.i[0], 0,
  1243.                           (int) op[0]->type->vector_elements - 1);
  1244.  
  1245.       switch (op[0]->type->base_type) {
  1246.       case GLSL_TYPE_UINT:
  1247.          data.u[0] = op[0]->value.u[c];
  1248.          break;
  1249.       case GLSL_TYPE_INT:
  1250.          data.i[0] = op[0]->value.i[c];
  1251.          break;
  1252.       case GLSL_TYPE_FLOAT:
  1253.          data.f[0] = op[0]->value.f[c];
  1254.          break;
  1255.       case GLSL_TYPE_BOOL:
  1256.          data.b[0] = op[0]->value.b[c];
  1257.          break;
  1258.       default:
  1259.          assert(0);
  1260.       }
  1261.       break;
  1262.    }
  1263.  
  1264.    case ir_binop_bit_xor:
  1265.       for (unsigned c = 0, c0 = 0, c1 = 0;
  1266.            c < components;
  1267.            c0 += c0_inc, c1 += c1_inc, c++) {
  1268.  
  1269.           switch (op[0]->type->base_type) {
  1270.           case GLSL_TYPE_INT:
  1271.               data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
  1272.               break;
  1273.           case GLSL_TYPE_UINT:
  1274.               data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
  1275.               break;
  1276.           default:
  1277.               assert(0);
  1278.           }
  1279.       }
  1280.       break;
  1281.  
  1282.    case ir_unop_bitfield_reverse:
  1283.       /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
  1284.       for (unsigned c = 0; c < components; c++) {
  1285.          unsigned int v = op[0]->value.u[c]; // input bits to be reversed
  1286.          unsigned int r = v; // r will be reversed bits of v; first get LSB of v
  1287.          int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
  1288.  
  1289.          for (v >>= 1; v; v >>= 1) {
  1290.             r <<= 1;
  1291.             r |= v & 1;
  1292.             s--;
  1293.          }
  1294.          r <<= s; // shift when v's highest bits are zero
  1295.  
  1296.          data.u[c] = r;
  1297.       }
  1298.       break;
  1299.  
  1300.    case ir_unop_bit_count:
  1301.       for (unsigned c = 0; c < components; c++) {
  1302.          unsigned count = 0;
  1303.          unsigned v = op[0]->value.u[c];
  1304.  
  1305.          for (; v; count++) {
  1306.             v &= v - 1;
  1307.          }
  1308.          data.u[c] = count;
  1309.       }
  1310.       break;
  1311.  
  1312.    case ir_unop_find_msb:
  1313.       for (unsigned c = 0; c < components; c++) {
  1314.          int v = op[0]->value.i[c];
  1315.  
  1316.          if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
  1317.             data.i[c] = -1;
  1318.          else {
  1319.             int count = 0;
  1320.             int top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
  1321.                           ? 0 : v & (1 << 31);
  1322.  
  1323.             while (((v & (1 << 31)) == top_bit) && count != 32) {
  1324.                count++;
  1325.                v <<= 1;
  1326.             }
  1327.  
  1328.             data.i[c] = 31 - count;
  1329.          }
  1330.       }
  1331.       break;
  1332.  
  1333.    case ir_unop_find_lsb:
  1334.       for (unsigned c = 0; c < components; c++) {
  1335.          if (op[0]->value.i[c] == 0)
  1336.             data.i[c] = -1;
  1337.          else {
  1338.             unsigned pos = 0;
  1339.             unsigned v = op[0]->value.u[c];
  1340.  
  1341.             for (; !(v & 1); v >>= 1) {
  1342.                pos++;
  1343.             }
  1344.             data.u[c] = pos;
  1345.          }
  1346.       }
  1347.       break;
  1348.  
  1349.    case ir_triop_bitfield_extract: {
  1350.       int offset = op[1]->value.i[0];
  1351.       int bits = op[2]->value.i[0];
  1352.  
  1353.       for (unsigned c = 0; c < components; c++) {
  1354.          if (bits == 0)
  1355.             data.u[c] = 0;
  1356.          else if (offset < 0 || bits < 0)
  1357.             data.u[c] = 0; /* Undefined, per spec. */
  1358.          else if (offset + bits > 32)
  1359.             data.u[c] = 0; /* Undefined, per spec. */
  1360.          else {
  1361.             if (op[0]->type->base_type == GLSL_TYPE_INT) {
  1362.                /* int so that the right shift will sign-extend. */
  1363.                int value = op[0]->value.i[c];
  1364.                value <<= 32 - bits - offset;
  1365.                value >>= 32 - bits;
  1366.                data.i[c] = value;
  1367.             } else {
  1368.                unsigned value = op[0]->value.u[c];
  1369.                value <<= 32 - bits - offset;
  1370.                value >>= 32 - bits;
  1371.                data.u[c] = value;
  1372.             }
  1373.          }
  1374.       }
  1375.       break;
  1376.    }
  1377.  
  1378.    case ir_triop_lrp: {
  1379.       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  1380.       assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
  1381.       assert(op[2]->type->base_type == GLSL_TYPE_FLOAT);
  1382.  
  1383.       unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
  1384.       for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
  1385.          data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
  1386.                      (op[1]->value.f[c] * op[2]->value.f[c2]);
  1387.       }
  1388.       break;
  1389.    }
  1390.  
  1391.    case ir_triop_vector_insert: {
  1392.       const unsigned idx = op[2]->value.u[0];
  1393.  
  1394.       memcpy(&data, &op[0]->value, sizeof(data));
  1395.  
  1396.       switch (this->type->base_type) {
  1397.       case GLSL_TYPE_INT:
  1398.          data.i[idx] = op[1]->value.i[0];
  1399.          break;
  1400.       case GLSL_TYPE_UINT:
  1401.          data.u[idx] = op[1]->value.u[0];
  1402.          break;
  1403.       case GLSL_TYPE_FLOAT:
  1404.          data.f[idx] = op[1]->value.f[0];
  1405.          break;
  1406.       case GLSL_TYPE_BOOL:
  1407.          data.b[idx] = op[1]->value.b[0];
  1408.          break;
  1409.       default:
  1410.          assert(!"Should not get here.");
  1411.          break;
  1412.       }
  1413.       break;
  1414.    }
  1415.  
  1416.    case ir_quadop_bitfield_insert: {
  1417.       int offset = op[2]->value.i[0];
  1418.       int bits = op[3]->value.i[0];
  1419.  
  1420.       for (unsigned c = 0; c < components; c++) {
  1421.          if (bits == 0)
  1422.             data.u[c] = op[0]->value.u[c];
  1423.          else if (offset < 0 || bits < 0)
  1424.             data.u[c] = 0; /* Undefined, per spec. */
  1425.          else if (offset + bits > 32)
  1426.             data.u[c] = 0; /* Undefined, per spec. */
  1427.          else {
  1428.             unsigned insert_mask = ((1 << bits) - 1) << offset;
  1429.  
  1430.             unsigned insert = op[1]->value.u[c];
  1431.             insert <<= offset;
  1432.             insert &= insert_mask;
  1433.  
  1434.             unsigned base = op[0]->value.u[c];
  1435.             base &= ~insert_mask;
  1436.  
  1437.             data.u[c] = base | insert;
  1438.          }
  1439.       }
  1440.       break;
  1441.    }
  1442.  
  1443.    case ir_quadop_vector:
  1444.       for (unsigned c = 0; c < this->type->vector_elements; c++) {
  1445.          switch (this->type->base_type) {
  1446.          case GLSL_TYPE_INT:
  1447.             data.i[c] = op[c]->value.i[0];
  1448.             break;
  1449.          case GLSL_TYPE_UINT:
  1450.             data.u[c] = op[c]->value.u[0];
  1451.             break;
  1452.          case GLSL_TYPE_FLOAT:
  1453.             data.f[c] = op[c]->value.f[0];
  1454.             break;
  1455.          default:
  1456.             assert(0);
  1457.          }
  1458.       }
  1459.       break;
  1460.  
  1461.    default:
  1462.       /* FINISHME: Should handle all expression types. */
  1463.       return NULL;
  1464.    }
  1465.  
  1466.    return new(ctx) ir_constant(this->type, &data);
  1467. }
  1468.  
  1469.  
  1470. ir_constant *
  1471. ir_texture::constant_expression_value(struct hash_table *variable_context)
  1472. {
  1473.    /* texture lookups aren't constant expressions */
  1474.    return NULL;
  1475. }
  1476.  
  1477.  
  1478. ir_constant *
  1479. ir_swizzle::constant_expression_value(struct hash_table *variable_context)
  1480. {
  1481.    ir_constant *v = this->val->constant_expression_value(variable_context);
  1482.  
  1483.    if (v != NULL) {
  1484.       ir_constant_data data = { { 0 } };
  1485.  
  1486.       const unsigned swiz_idx[4] = {
  1487.          this->mask.x, this->mask.y, this->mask.z, this->mask.w
  1488.       };
  1489.  
  1490.       for (unsigned i = 0; i < this->mask.num_components; i++) {
  1491.          switch (v->type->base_type) {
  1492.          case GLSL_TYPE_UINT:
  1493.          case GLSL_TYPE_INT:   data.u[i] = v->value.u[swiz_idx[i]]; break;
  1494.          case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
  1495.          case GLSL_TYPE_BOOL:  data.b[i] = v->value.b[swiz_idx[i]]; break;
  1496.          default:              assert(!"Should not get here."); break;
  1497.          }
  1498.       }
  1499.  
  1500.       void *ctx = ralloc_parent(this);
  1501.       return new(ctx) ir_constant(this->type, &data);
  1502.    }
  1503.    return NULL;
  1504. }
  1505.  
  1506.  
  1507. void
  1508. ir_dereference_variable::constant_referenced(struct hash_table *variable_context,
  1509.                                              ir_constant *&store, int &offset) const
  1510. {
  1511.    if (variable_context) {
  1512.       store = (ir_constant *)hash_table_find(variable_context, var);
  1513.       offset = 0;
  1514.    } else {
  1515.       store = NULL;
  1516.       offset = 0;
  1517.    }
  1518. }
  1519.  
  1520. ir_constant *
  1521. ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
  1522. {
  1523.    /* This may occur during compile and var->type is glsl_type::error_type */
  1524.    if (!var)
  1525.       return NULL;
  1526.  
  1527.    /* Give priority to the context hashtable, if it exists */
  1528.    if (variable_context) {
  1529.       ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
  1530.       if(value)
  1531.          return value;
  1532.    }
  1533.  
  1534.    /* The constant_value of a uniform variable is its initializer,
  1535.     * not the lifetime constant value of the uniform.
  1536.     */
  1537.    if (var->mode == ir_var_uniform)
  1538.       return NULL;
  1539.  
  1540.    if (!var->constant_value)
  1541.       return NULL;
  1542.  
  1543.    return var->constant_value->clone(ralloc_parent(var), NULL);
  1544. }
  1545.  
  1546.  
  1547. void
  1548. ir_dereference_array::constant_referenced(struct hash_table *variable_context,
  1549.                                           ir_constant *&store, int &offset) const
  1550. {
  1551.    ir_constant *index_c = array_index->constant_expression_value(variable_context);
  1552.  
  1553.    if (!index_c || !index_c->type->is_scalar() || !index_c->type->is_integer()) {
  1554.       store = 0;
  1555.       offset = 0;
  1556.       return;
  1557.    }
  1558.  
  1559.    int index = index_c->type->base_type == GLSL_TYPE_INT ?
  1560.       index_c->get_int_component(0) :
  1561.       index_c->get_uint_component(0);
  1562.  
  1563.    ir_constant *substore;
  1564.    int suboffset;
  1565.    const ir_dereference *deref = array->as_dereference();
  1566.    if (!deref) {
  1567.       store = 0;
  1568.       offset = 0;
  1569.       return;
  1570.    }
  1571.  
  1572.    deref->constant_referenced(variable_context, substore, suboffset);
  1573.  
  1574.    if (!substore) {
  1575.       store = 0;
  1576.       offset = 0;
  1577.       return;
  1578.    }
  1579.  
  1580.    const glsl_type *vt = array->type;
  1581.    if (vt->is_array()) {
  1582.       store = substore->get_array_element(index);
  1583.       offset = 0;
  1584.       return;
  1585.    }
  1586.    if (vt->is_matrix()) {
  1587.       store = substore;
  1588.       offset = index * vt->vector_elements;
  1589.       return;
  1590.    }
  1591.    if (vt->is_vector()) {
  1592.       store = substore;
  1593.       offset = suboffset + index;
  1594.       return;
  1595.    }
  1596.  
  1597.    store = 0;
  1598.    offset = 0;
  1599. }
  1600.  
  1601. ir_constant *
  1602. ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
  1603. {
  1604.    ir_constant *array = this->array->constant_expression_value(variable_context);
  1605.    ir_constant *idx = this->array_index->constant_expression_value(variable_context);
  1606.  
  1607.    if ((array != NULL) && (idx != NULL)) {
  1608.       void *ctx = ralloc_parent(this);
  1609.       if (array->type->is_matrix()) {
  1610.          /* Array access of a matrix results in a vector.
  1611.           */
  1612.          const unsigned column = idx->value.u[0];
  1613.  
  1614.          const glsl_type *const column_type = array->type->column_type();
  1615.  
  1616.          /* Offset in the constant matrix to the first element of the column
  1617.           * to be extracted.
  1618.           */
  1619.          const unsigned mat_idx = column * column_type->vector_elements;
  1620.  
  1621.          ir_constant_data data = { { 0 } };
  1622.  
  1623.          switch (column_type->base_type) {
  1624.          case GLSL_TYPE_UINT:
  1625.          case GLSL_TYPE_INT:
  1626.             for (unsigned i = 0; i < column_type->vector_elements; i++)
  1627.                data.u[i] = array->value.u[mat_idx + i];
  1628.  
  1629.             break;
  1630.  
  1631.          case GLSL_TYPE_FLOAT:
  1632.             for (unsigned i = 0; i < column_type->vector_elements; i++)
  1633.                data.f[i] = array->value.f[mat_idx + i];
  1634.  
  1635.             break;
  1636.  
  1637.          default:
  1638.             assert(!"Should not get here.");
  1639.             break;
  1640.          }
  1641.  
  1642.          return new(ctx) ir_constant(column_type, &data);
  1643.       } else if (array->type->is_vector()) {
  1644.          const unsigned component = idx->value.u[0];
  1645.  
  1646.          return new(ctx) ir_constant(array, component);
  1647.       } else {
  1648.          const unsigned index = idx->value.u[0];
  1649.          return array->get_array_element(index)->clone(ctx, NULL);
  1650.       }
  1651.    }
  1652.    return NULL;
  1653. }
  1654.  
  1655.  
  1656. void
  1657. ir_dereference_record::constant_referenced(struct hash_table *variable_context,
  1658.                                            ir_constant *&store, int &offset) const
  1659. {
  1660.    ir_constant *substore;
  1661.    int suboffset;
  1662.    const ir_dereference *deref = record->as_dereference();
  1663.    if (!deref) {
  1664.       store = 0;
  1665.       offset = 0;
  1666.       return;
  1667.    }
  1668.  
  1669.    deref->constant_referenced(variable_context, substore, suboffset);
  1670.  
  1671.    if (!substore) {
  1672.       store = 0;
  1673.       offset = 0;
  1674.       return;
  1675.    }
  1676.  
  1677.    store = substore->get_record_field(field);
  1678.    offset = 0;
  1679. }
  1680.  
  1681. ir_constant *
  1682. ir_dereference_record::constant_expression_value(struct hash_table *variable_context)
  1683. {
  1684.    ir_constant *v = this->record->constant_expression_value();
  1685.  
  1686.    return (v != NULL) ? v->get_record_field(this->field) : NULL;
  1687. }
  1688.  
  1689.  
  1690. ir_constant *
  1691. ir_assignment::constant_expression_value(struct hash_table *variable_context)
  1692. {
  1693.    /* FINISHME: Handle CEs involving assignment (return RHS) */
  1694.    return NULL;
  1695. }
  1696.  
  1697.  
  1698. ir_constant *
  1699. ir_constant::constant_expression_value(struct hash_table *variable_context)
  1700. {
  1701.    return this;
  1702. }
  1703.  
  1704.  
  1705. ir_constant *
  1706. ir_call::constant_expression_value(struct hash_table *variable_context)
  1707. {
  1708.    return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
  1709. }
  1710.  
  1711.  
  1712. bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
  1713.                                                                          struct hash_table *variable_context,
  1714.                                                                          ir_constant **result)
  1715. {
  1716.    foreach_list(n, &body) {
  1717.       ir_instruction *inst = (ir_instruction *)n;
  1718.       switch(inst->ir_type) {
  1719.  
  1720.          /* (declare () type symbol) */
  1721.       case ir_type_variable: {
  1722.          ir_variable *var = inst->as_variable();
  1723.          hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
  1724.          break;
  1725.       }
  1726.  
  1727.          /* (assign [condition] (write-mask) (ref) (value)) */
  1728.       case ir_type_assignment: {
  1729.          ir_assignment *asg = inst->as_assignment();
  1730.          if (asg->condition) {
  1731.             ir_constant *cond = asg->condition->constant_expression_value(variable_context);
  1732.             if (!cond)
  1733.                return false;
  1734.             if (!cond->get_bool_component(0))
  1735.                break;
  1736.          }
  1737.  
  1738.          ir_constant *store = NULL;
  1739.          int offset = 0;
  1740.          asg->lhs->constant_referenced(variable_context, store, offset);
  1741.  
  1742.          if (!store)
  1743.             return false;
  1744.  
  1745.          ir_constant *value = asg->rhs->constant_expression_value(variable_context);
  1746.  
  1747.          if (!value)
  1748.             return false;
  1749.  
  1750.          store->copy_masked_offset(value, offset, asg->write_mask);
  1751.          break;
  1752.       }
  1753.  
  1754.          /* (return (expression)) */
  1755.       case ir_type_return:
  1756.          assert (result);
  1757.          *result = inst->as_return()->value->constant_expression_value(variable_context);
  1758.          return *result != NULL;
  1759.  
  1760.          /* (call name (ref) (params))*/
  1761.       case ir_type_call: {
  1762.          ir_call *call = inst->as_call();
  1763.  
  1764.          /* Just say no to void functions in constant expressions.  We
  1765.           * don't need them at that point.
  1766.           */
  1767.  
  1768.          if (!call->return_deref)
  1769.             return false;
  1770.  
  1771.          ir_constant *store = NULL;
  1772.          int offset = 0;
  1773.          call->return_deref->constant_referenced(variable_context, store, offset);
  1774.  
  1775.          if (!store)
  1776.             return false;
  1777.  
  1778.          ir_constant *value = call->constant_expression_value(variable_context);
  1779.  
  1780.          if(!value)
  1781.             return false;
  1782.  
  1783.          store->copy_offset(value, offset);
  1784.          break;
  1785.       }
  1786.  
  1787.          /* (if condition (then-instructions) (else-instructions)) */
  1788.       case ir_type_if: {
  1789.          ir_if *iif = inst->as_if();
  1790.  
  1791.          ir_constant *cond = iif->condition->constant_expression_value(variable_context);
  1792.          if (!cond || !cond->type->is_boolean())
  1793.             return false;
  1794.  
  1795.          exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
  1796.  
  1797.          *result = NULL;
  1798.          if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
  1799.             return false;
  1800.  
  1801.          /* If there was a return in the branch chosen, drop out now. */
  1802.          if (*result)
  1803.             return true;
  1804.  
  1805.          break;
  1806.       }
  1807.  
  1808.          /* Every other expression type, we drop out. */
  1809.       default:
  1810.          return false;
  1811.       }
  1812.    }
  1813.  
  1814.    /* Reaching the end of the block is not an error condition */
  1815.    if (result)
  1816.       *result = NULL;
  1817.  
  1818.    return true;
  1819. }
  1820.  
  1821. ir_constant *
  1822. ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
  1823. {
  1824.    const glsl_type *type = this->return_type;
  1825.    if (type == glsl_type::void_type)
  1826.       return NULL;
  1827.  
  1828.    /* From the GLSL 1.20 spec, page 23:
  1829.     * "Function calls to user-defined functions (non-built-in functions)
  1830.     *  cannot be used to form constant expressions."
  1831.     */
  1832.    if (!this->is_builtin)
  1833.       return NULL;
  1834.  
  1835.    /*
  1836.     * Of the builtin functions, only the texture lookups and the noise
  1837.     * ones must not be used in constant expressions.  They all include
  1838.     * specific opcodes so they don't need to be special-cased at this
  1839.     * point.
  1840.     */
  1841.  
  1842.    /* Initialize the table of dereferencable names with the function
  1843.     * parameters.  Verify their const-ness on the way.
  1844.     *
  1845.     * We expect the correctness of the number of parameters to have
  1846.     * been checked earlier.
  1847.     */
  1848.    hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
  1849.                                             hash_table_pointer_compare);
  1850.  
  1851.    /* If "origin" is non-NULL, then the function body is there.  So we
  1852.     * have to use the variable objects from the object with the body,
  1853.     * but the parameter instanciation on the current object.
  1854.     */
  1855.    const exec_node *parameter_info = origin ? origin->parameters.head : parameters.head;
  1856.  
  1857.    foreach_list(n, actual_parameters) {
  1858.       ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(variable_context);
  1859.       if (constant == NULL) {
  1860.          hash_table_dtor(deref_hash);
  1861.          return NULL;
  1862.       }
  1863.  
  1864.  
  1865.       ir_variable *var = (ir_variable *)parameter_info;
  1866.       hash_table_insert(deref_hash, constant, var);
  1867.  
  1868.       parameter_info = parameter_info->next;
  1869.    }
  1870.  
  1871.    ir_constant *result = NULL;
  1872.  
  1873.    /* Now run the builtin function until something non-constant
  1874.     * happens or we get the result.
  1875.     */
  1876.    if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
  1877.       result = result->clone(ralloc_parent(this), NULL);
  1878.  
  1879.    hash_table_dtor(deref_hash);
  1880.  
  1881.    return result;
  1882. }
  1883.